test.c 457 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210
  1. /* test.c
  2. *
  3. * Copyright (C) 2006-2016 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. #ifdef WOLFSSL_TEST_CERT
  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 WOLFSSL_ASYNC_CRYPT
  101. #include <wolfssl/wolfcrypt/async.h>
  102. #endif
  103. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  104. #include <wolfssl/wolfcrypt/logging.h>
  105. #endif
  106. /* only for stack size check */
  107. #ifdef HAVE_STACK_SIZE
  108. #include <wolfssl/ssl.h>
  109. #define err_sys err_sys_remap /* remap err_sys */
  110. #include <wolfssl/test.h>
  111. #undef err_sys
  112. #endif
  113. #ifdef _MSC_VER
  114. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  115. #pragma warning(disable: 4996)
  116. #endif
  117. #ifdef OPENSSL_EXTRA
  118. #ifndef WOLFCRYPT_ONLY
  119. #include <wolfssl/openssl/evp.h>
  120. #endif
  121. #include <wolfssl/openssl/rand.h>
  122. #include <wolfssl/openssl/hmac.h>
  123. #include <wolfssl/openssl/aes.h>
  124. #include <wolfssl/openssl/des.h>
  125. #endif
  126. #if defined(NO_FILESYSTEM)
  127. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  128. !defined(USE_CERT_BUFFERS_4096)
  129. #define USE_CERT_BUFFERS_2048
  130. #endif
  131. #if !defined(USE_CERT_BUFFERS_256)
  132. #define USE_CERT_BUFFERS_256
  133. #endif
  134. #endif
  135. #include <wolfssl/certs_test.h>
  136. #if defined(WOLFSSL_MDK_ARM)
  137. #include <stdio.h>
  138. #include <stdlib.h>
  139. extern FILE * wolfSSL_fopen(const char *fname, const char *mode) ;
  140. #define fopen wolfSSL_fopen
  141. #endif
  142. #ifdef HAVE_NTRU
  143. #include "libntruencrypt/ntru_crypto.h"
  144. #endif
  145. #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
  146. #include <mqx.h>
  147. #include <stdlib.h>
  148. #if MQX_USE_IO_OLD
  149. #include <fio.h>
  150. #else
  151. #include <nio.h>
  152. #endif
  153. #elif defined(FREESCALE_KSDK_BM)
  154. #include "fsl_debug_console.h"
  155. #undef printf
  156. #define printf PRINTF
  157. #else
  158. #include <stdio.h>
  159. #endif
  160. #if defined(WOLFSSL_CERT_GEN) && (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES))
  161. #define ENABLE_ECC384_CERT_GEN_TEST
  162. #endif
  163. #ifdef THREADX
  164. /* since just testing, use THREADX log printf instead */
  165. int dc_log_printf(char*, ...);
  166. #undef printf
  167. #define printf dc_log_printf
  168. #endif
  169. #ifdef MICRIUM
  170. #include <bsp_ser.h>
  171. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  172. #undef printf
  173. #define printf BSP_Ser_Printf
  174. #endif
  175. #include "wolfcrypt/test/test.h"
  176. /* for async devices */
  177. static int devId = INVALID_DEVID;
  178. #ifdef HAVE_WNR
  179. const char* wnrConfigFile = "wnr-example.conf";
  180. #endif
  181. typedef struct testVector {
  182. const char* input;
  183. const char* output;
  184. size_t inLen;
  185. size_t outLen;
  186. } testVector;
  187. int error_test(void);
  188. int base64_test(void);
  189. int asn_test(void);
  190. int md2_test(void);
  191. int md5_test(void);
  192. int md4_test(void);
  193. int sha_test(void);
  194. int sha224_test(void);
  195. int sha256_test(void);
  196. int sha512_test(void);
  197. int sha384_test(void);
  198. int sha3_test(void);
  199. int hash_test(void);
  200. int hmac_md5_test(void);
  201. int hmac_sha_test(void);
  202. int hmac_sha224_test(void);
  203. int hmac_sha256_test(void);
  204. int hmac_sha384_test(void);
  205. int hmac_sha512_test(void);
  206. int hmac_blake2b_test(void);
  207. int hkdf_test(void);
  208. int x963kdf_test(void);
  209. int arc4_test(void);
  210. int hc128_test(void);
  211. int rabbit_test(void);
  212. int chacha_test(void);
  213. int chacha20_poly1305_aead_test(void);
  214. int des_test(void);
  215. int des3_test(void);
  216. int aes_test(void);
  217. int aes192_test(void);
  218. int aes256_test(void);
  219. int cmac_test(void);
  220. int poly1305_test(void);
  221. int aesgcm_test(void);
  222. int gmac_test(void);
  223. int aesccm_test(void);
  224. int aeskeywrap_test(void);
  225. int camellia_test(void);
  226. int rsa_test(void);
  227. int dh_test(void);
  228. int dsa_test(void);
  229. int srp_test(void);
  230. #ifndef WC_NO_RNG
  231. int random_test(void);
  232. #endif /* WC_NO_RNG */
  233. int pwdbased_test(void);
  234. int ripemd_test(void);
  235. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  236. int openssl_test(void); /* test mini api */
  237. #endif
  238. int pbkdf1_test(void);
  239. int pkcs12_test(void);
  240. int pbkdf2_test(void);
  241. int scrypt_test(void);
  242. #ifdef HAVE_ECC
  243. int ecc_test(void);
  244. #ifdef HAVE_ECC_ENCRYPT
  245. int ecc_encrypt_test(void);
  246. #endif
  247. #ifdef USE_CERT_BUFFERS_256
  248. int ecc_test_buffers(void);
  249. #endif
  250. #endif
  251. #ifdef HAVE_CURVE25519
  252. int curve25519_test(void);
  253. #endif
  254. #ifdef HAVE_ED25519
  255. int ed25519_test(void);
  256. #endif
  257. #ifdef HAVE_BLAKE2
  258. int blake2b_test(void);
  259. #endif
  260. #ifdef HAVE_LIBZ
  261. int compress_test(void);
  262. #endif
  263. #ifdef HAVE_PKCS7
  264. int pkcs7enveloped_test(void);
  265. int pkcs7signed_test(void);
  266. int pkcs7encrypted_test(void);
  267. #endif
  268. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT)
  269. int cert_test(void);
  270. #endif
  271. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT)
  272. int certext_test(void);
  273. #endif
  274. #ifdef HAVE_IDEA
  275. int idea_test(void);
  276. #endif
  277. #ifdef WOLFSSL_STATIC_MEMORY
  278. int memory_test(void);
  279. #endif
  280. #ifdef HAVE_VALGRIND
  281. int mp_test(void);
  282. #endif
  283. int logging_test(void);
  284. int mutex_test(void);
  285. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  286. int memcb_test(void);
  287. #endif
  288. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND) && \
  289. !defined(OPENSSL_EXTRA) && !defined(HAVE_STACK_SIZE)
  290. #ifdef __cplusplus
  291. extern "C" {
  292. #endif
  293. WOLFSSL_API int wolfSSL_Debugging_ON(void);
  294. #ifdef __cplusplus
  295. } /* extern "C" */
  296. #endif
  297. #endif
  298. /* General big buffer size for many tests. */
  299. #define FOURK_BUF 4096
  300. #define ERROR_OUT(err, eLabel) { ret = (err); goto eLabel; }
  301. #ifdef HAVE_STACK_SIZE
  302. static THREAD_RETURN err_sys(const char* msg, int es)
  303. #else
  304. static int err_sys(const char* msg, int es)
  305. #endif
  306. {
  307. printf("%s error = %d\n", msg, es);
  308. EXIT_TEST(-1);
  309. }
  310. #ifndef HAVE_STACK_SIZE
  311. /* func_args from test.h, so don't have to pull in other stuff */
  312. typedef struct func_args {
  313. int argc;
  314. char** argv;
  315. int return_code;
  316. } func_args;
  317. #endif /* !HAVE_STACK_SIZE */
  318. #ifdef HAVE_FIPS
  319. static void myFipsCb(int ok, int err, const char* hash)
  320. {
  321. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  322. printf("message = %s\n", wc_GetErrorString(err));
  323. printf("hash = %s\n", hash);
  324. if (err == IN_CORE_FIPS_E) {
  325. printf("In core integrity hash check failure, copy above hash\n");
  326. printf("into verifyCore[] in fips_test.c and rebuild\n");
  327. }
  328. }
  329. #endif /* HAVE_FIPS */
  330. #ifdef WOLFSSL_STATIC_MEMORY
  331. #ifdef BENCH_EMBEDDED
  332. static byte gTestMemory[10000];
  333. #elif defined(USE_FAST_MATH) && !defined(ALT_ECC_SIZE)
  334. static byte gTestMemory[130000];
  335. #else
  336. static byte gTestMemory[80000];
  337. #endif
  338. #endif
  339. #ifdef HAVE_STACK_SIZE
  340. THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args)
  341. #else
  342. int wolfcrypt_test(void* args)
  343. #endif
  344. {
  345. int ret;
  346. ((func_args*)args)->return_code = -1; /* error state */
  347. #ifdef WOLFSSL_STATIC_MEMORY
  348. if (wc_LoadStaticMemory(&HEAP_HINT, gTestMemory, sizeof(gTestMemory),
  349. WOLFMEM_GENERAL, 1) != 0) {
  350. printf("unable to load static memory");
  351. exit(EXIT_FAILURE);
  352. }
  353. #endif
  354. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  355. wolfSSL_Debugging_ON();
  356. #endif
  357. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  358. wc_SetLoggingHeap(HEAP_HINT);
  359. #endif
  360. #ifdef HAVE_FIPS
  361. wolfCrypt_SetCb_fips(myFipsCb);
  362. #endif
  363. #if !defined(NO_BIG_INT)
  364. if (CheckCtcSettings() != 1)
  365. return err_sys("Build vs runtime math mismatch\n", -1000);
  366. #if defined(USE_FAST_MATH) && \
  367. (!defined(NO_RSA) || !defined(NO_DH) || defined(HAVE_ECC))
  368. if (CheckFastMathSettings() != 1)
  369. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  370. -1001);
  371. #endif /* USE_FAST_MATH */
  372. #endif /* !NO_BIG_INT */
  373. #ifdef WOLFSSL_ASYNC_CRYPT
  374. ret = wolfAsync_DevOpen(&devId);
  375. if (ret < 0) {
  376. printf("Async device open failed\nRunning without async\n");
  377. }
  378. #else
  379. (void)devId;
  380. #endif /* WOLFSSL_ASYNC_CRYPT */
  381. if ( (ret = error_test()) != 0)
  382. return err_sys("error test failed!\n", ret);
  383. else
  384. printf( "error test passed!\n");
  385. #if !defined(NO_CODING) && defined(WOLFSSL_BASE64_ENCODE)
  386. if ( (ret = base64_test()) != 0)
  387. return err_sys("base64 test failed!\n", ret);
  388. else
  389. printf( "base64 test passed!\n");
  390. #endif
  391. #ifndef NO_ASN
  392. if ( (ret = asn_test()) != 0)
  393. return err_sys("base64 test failed!\n", ret);
  394. else
  395. printf( "base64 test passed!\n");
  396. #endif
  397. #ifndef NO_MD5
  398. if ( (ret = md5_test()) != 0)
  399. return err_sys("MD5 test failed!\n", ret);
  400. else
  401. printf( "MD5 test passed!\n");
  402. #endif
  403. #ifdef WOLFSSL_MD2
  404. if ( (ret = md2_test()) != 0)
  405. return err_sys("MD2 test failed!\n", ret);
  406. else
  407. printf( "MD2 test passed!\n");
  408. #endif
  409. #ifndef NO_MD4
  410. if ( (ret = md4_test()) != 0)
  411. return err_sys("MD4 test failed!\n", ret);
  412. else
  413. printf( "MD4 test passed!\n");
  414. #endif
  415. #ifndef NO_SHA
  416. if ( (ret = sha_test()) != 0)
  417. return err_sys("SHA test failed!\n", ret);
  418. else
  419. printf( "SHA test passed!\n");
  420. #endif
  421. #ifdef WOLFSSL_SHA224
  422. if ( (ret = sha224_test()) != 0)
  423. return err_sys("SHA-224 test failed!\n", ret);
  424. else
  425. printf( "SHA-224 test passed!\n");
  426. #endif
  427. #ifndef NO_SHA256
  428. if ( (ret = sha256_test()) != 0)
  429. return err_sys("SHA-256 test failed!\n", ret);
  430. else
  431. printf( "SHA-256 test passed!\n");
  432. #endif
  433. #ifdef WOLFSSL_SHA384
  434. if ( (ret = sha384_test()) != 0)
  435. return err_sys("SHA-384 test failed!\n", ret);
  436. else
  437. printf( "SHA-384 test passed!\n");
  438. #endif
  439. #ifdef WOLFSSL_SHA512
  440. if ( (ret = sha512_test()) != 0)
  441. return err_sys("SHA-512 test failed!\n", ret);
  442. else
  443. printf( "SHA-512 test passed!\n");
  444. #endif
  445. #ifdef WOLFSSL_SHA3
  446. if ( (ret = sha3_test()) != 0)
  447. return err_sys("SHA-3 test failed!\n", ret);
  448. else
  449. printf( "SHA-3 test passed!\n");
  450. #endif
  451. if ( (ret = hash_test()) != 0)
  452. return err_sys("Hash test failed!\n", ret);
  453. else
  454. printf( "Hash test passed!\n");
  455. #ifdef WOLFSSL_RIPEMD
  456. if ( (ret = ripemd_test()) != 0)
  457. return err_sys("RIPEMD test failed!\n", ret);
  458. else
  459. printf( "RIPEMD test passed!\n");
  460. #endif
  461. #ifdef HAVE_BLAKE2
  462. if ( (ret = blake2b_test()) != 0)
  463. return err_sys("BLAKE2b test failed!\n", ret);
  464. else
  465. printf( "BLAKE2b test passed!\n");
  466. #endif
  467. #ifndef NO_HMAC
  468. #ifndef NO_MD5
  469. if ( (ret = hmac_md5_test()) != 0)
  470. return err_sys("HMAC-MD5 test failed!\n", ret);
  471. else
  472. printf( "HMAC-MD5 test passed!\n");
  473. #endif
  474. #ifndef NO_SHA
  475. if ( (ret = hmac_sha_test()) != 0)
  476. return err_sys("HMAC-SHA test failed!\n", ret);
  477. else
  478. printf( "HMAC-SHA test passed!\n");
  479. #endif
  480. #ifdef WOLFSSL_SHA224
  481. if ( (ret = hmac_sha224_test()) != 0)
  482. return err_sys("HMAC-SHA224 test failed!\n", ret);
  483. else
  484. printf( "HMAC-SHA224 test passed!\n");
  485. #endif
  486. #ifndef NO_SHA256
  487. if ( (ret = hmac_sha256_test()) != 0)
  488. return err_sys("HMAC-SHA256 test failed!\n", ret);
  489. else
  490. printf( "HMAC-SHA256 test passed!\n");
  491. #endif
  492. #ifdef WOLFSSL_SHA384
  493. if ( (ret = hmac_sha384_test()) != 0)
  494. return err_sys("HMAC-SHA384 test failed!\n", ret);
  495. else
  496. printf( "HMAC-SHA384 test passed!\n");
  497. #endif
  498. #ifdef WOLFSSL_SHA512
  499. if ( (ret = hmac_sha512_test()) != 0)
  500. return err_sys("HMAC-SHA512 test failed!\n", ret);
  501. else
  502. printf( "HMAC-SHA512 test passed!\n");
  503. #endif
  504. #ifdef HAVE_BLAKE2
  505. if ( (ret = hmac_blake2b_test()) != 0)
  506. return err_sys("HMAC-BLAKE2 test failed!\n", ret);
  507. else
  508. printf( "HMAC-BLAKE2 test passed!\n");
  509. #endif
  510. #ifdef HAVE_HKDF
  511. if ( (ret = hkdf_test()) != 0)
  512. return err_sys("HMAC-KDF test failed!\n", ret);
  513. else
  514. printf( "HMAC-KDF test passed!\n");
  515. #endif
  516. #endif /* !NO_HMAC */
  517. #if defined(HAVE_X963_KDF) && defined(HAVE_ECC)
  518. if ( (ret = x963kdf_test()) != 0)
  519. return err_sys("X963-KDF test failed!\n", ret);
  520. else
  521. printf( "X963-KDF test passed!\n");
  522. #endif
  523. #ifdef HAVE_AESGCM
  524. if ( (ret = gmac_test()) != 0)
  525. return err_sys("GMAC test failed!\n", ret);
  526. else
  527. printf( "GMAC test passed!\n");
  528. #endif
  529. #ifndef NO_RC4
  530. if ( (ret = arc4_test()) != 0)
  531. return err_sys("ARC4 test failed!\n", ret);
  532. else
  533. printf( "ARC4 test passed!\n");
  534. #endif
  535. #ifndef NO_HC128
  536. if ( (ret = hc128_test()) != 0)
  537. return err_sys("HC-128 test failed!\n", ret);
  538. else
  539. printf( "HC-128 test passed!\n");
  540. #endif
  541. #ifndef NO_RABBIT
  542. if ( (ret = rabbit_test()) != 0)
  543. return err_sys("Rabbit test failed!\n", ret);
  544. else
  545. printf( "Rabbit test passed!\n");
  546. #endif
  547. #ifdef HAVE_CHACHA
  548. if ( (ret = chacha_test()) != 0)
  549. return err_sys("Chacha test failed!\n", ret);
  550. else
  551. printf( "Chacha test passed!\n");
  552. #endif
  553. #ifdef HAVE_POLY1305
  554. if ( (ret = poly1305_test()) != 0)
  555. return err_sys("POLY1305 test failed!\n", ret);
  556. else
  557. printf( "POLY1305 test passed!\n");
  558. #endif
  559. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  560. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  561. return err_sys("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  562. else
  563. printf( "ChaCha20-Poly1305 AEAD test passed!\n");
  564. #endif
  565. #ifndef NO_DES3
  566. if ( (ret = des_test()) != 0)
  567. return err_sys("DES test failed!\n", ret);
  568. else
  569. printf( "DES test passed!\n");
  570. #endif
  571. #ifndef NO_DES3
  572. if ( (ret = des3_test()) != 0)
  573. return err_sys("DES3 test failed!\n", ret);
  574. else
  575. printf( "DES3 test passed!\n");
  576. #endif
  577. #ifndef NO_AES
  578. if ( (ret = aes_test()) != 0)
  579. return err_sys("AES test failed!\n", ret);
  580. else
  581. printf( "AES test passed!\n");
  582. if ( (ret = aes192_test()) != 0)
  583. return err_sys("AES192 test failed!\n", ret);
  584. else
  585. printf( "AES192 test passed!\n");
  586. if ( (ret = aes256_test()) != 0)
  587. return err_sys("AES256 test failed!\n", ret);
  588. else
  589. printf( "AES256 test passed!\n");
  590. #ifdef HAVE_AESGCM
  591. if ( (ret = aesgcm_test()) != 0)
  592. return err_sys("AES-GCM test failed!\n", ret);
  593. else
  594. printf( "AES-GCM test passed!\n");
  595. #endif
  596. #ifdef HAVE_AESCCM
  597. if ( (ret = aesccm_test()) != 0)
  598. return err_sys("AES-CCM test failed!\n", ret);
  599. else
  600. printf( "AES-CCM test passed!\n");
  601. #endif
  602. #ifdef HAVE_AES_KEYWRAP
  603. if ( (ret = aeskeywrap_test()) != 0)
  604. return err_sys("AES Key Wrap test failed!\n", ret);
  605. else
  606. printf( "AES Key Wrap test passed!\n");
  607. #endif
  608. #endif
  609. #ifdef HAVE_CAMELLIA
  610. if ( (ret = camellia_test()) != 0)
  611. return err_sys("CAMELLIA test failed!\n", ret);
  612. else
  613. printf( "CAMELLIA test passed!\n");
  614. #endif
  615. #ifdef HAVE_IDEA
  616. if ( (ret = idea_test()) != 0)
  617. return err_sys("IDEA test failed!\n", ret);
  618. else
  619. printf( "IDEA test passed!\n");
  620. #endif
  621. #ifndef WC_NO_RNG
  622. if ( (ret = random_test()) != 0)
  623. return err_sys("RANDOM test failed!\n", ret);
  624. else
  625. printf( "RANDOM test passed!\n");
  626. #endif /* WC_NO_RNG */
  627. #ifdef WOLFSSL_STATIC_MEMORY
  628. if ( (ret = memory_test()) != 0)
  629. return err_sys("MEMORY test failed!\n", ret);
  630. else
  631. printf( "MEMORY test passed!\n");
  632. #endif
  633. #ifndef NO_RSA
  634. if ( (ret = rsa_test()) != 0)
  635. return err_sys("RSA test failed!\n", ret);
  636. else
  637. printf( "RSA test passed!\n");
  638. #endif
  639. #ifndef NO_DH
  640. if ( (ret = dh_test()) != 0)
  641. return err_sys("DH test failed!\n", ret);
  642. else
  643. printf( "DH test passed!\n");
  644. #endif
  645. #ifndef NO_DSA
  646. if ( (ret = dsa_test()) != 0)
  647. return err_sys("DSA test failed!\n", ret);
  648. else
  649. printf( "DSA test passed!\n");
  650. #endif
  651. #ifdef WOLFCRYPT_HAVE_SRP
  652. if ( (ret = srp_test()) != 0)
  653. return err_sys("SRP test failed!\n", ret);
  654. else
  655. printf( "SRP test passed!\n");
  656. #endif
  657. #ifndef NO_PWDBASED
  658. if ( (ret = pwdbased_test()) != 0)
  659. return err_sys("PWDBASED test failed!\n", ret);
  660. else
  661. printf( "PWDBASED test passed!\n");
  662. #endif
  663. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  664. if ( (ret = openssl_test()) != 0)
  665. return err_sys("OPENSSL test failed!\n", ret);
  666. else
  667. printf( "OPENSSL test passed!\n");
  668. #endif
  669. #ifdef HAVE_ECC
  670. if ( (ret = ecc_test()) != 0)
  671. return err_sys("ECC test failed!\n", ret);
  672. else
  673. printf( "ECC test passed!\n");
  674. #ifdef HAVE_ECC_ENCRYPT
  675. if ( (ret = ecc_encrypt_test()) != 0)
  676. return err_sys("ECC Enc test failed!\n", ret);
  677. else
  678. printf( "ECC Enc test passed!\n");
  679. #endif
  680. #ifdef USE_CERT_BUFFERS_256
  681. if ( (ret = ecc_test_buffers()) != 0)
  682. return err_sys("ECC buffer test failed!\n", ret);
  683. else
  684. printf( "ECC buffer test passed!\n");
  685. #endif
  686. #endif
  687. #if !defined(NO_ASN_TIME) && !defined(NO_RSA) && defined(WOLFSSL_TEST_CERT)
  688. if ( (ret = cert_test()) != 0)
  689. return err_sys("CERT test failed!\n", ret);
  690. else
  691. printf( "CERT test passed!\n");
  692. #endif
  693. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT)
  694. if ( (ret = certext_test()) != 0)
  695. return err_sys("CERT EXT test failed!\n", ret);
  696. else
  697. printf( "CERT EXT test passed!\n");
  698. #endif
  699. #ifdef HAVE_CURVE25519
  700. if ( (ret = curve25519_test()) != 0)
  701. return err_sys("CURVE25519 test failed!\n", ret);
  702. else
  703. printf( "CURVE25519 test passed!\n");
  704. #endif
  705. #ifdef HAVE_ED25519
  706. if ( (ret = ed25519_test()) != 0)
  707. return err_sys("ED25519 test failed!\n", ret);
  708. else
  709. printf( "ED25519 test passed!\n");
  710. #endif
  711. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  712. if ( (ret = cmac_test()) != 0)
  713. return err_sys("CMAC test failed!\n", ret);
  714. else
  715. printf( "CMAC test passed!\n");
  716. #endif
  717. #ifdef HAVE_LIBZ
  718. if ( (ret = compress_test()) != 0)
  719. return err_sys("COMPRESS test failed!\n", ret);
  720. else
  721. printf( "COMPRESS test passed!\n");
  722. #endif
  723. #ifdef HAVE_PKCS7
  724. if ( (ret = pkcs7enveloped_test()) != 0)
  725. return err_sys("PKCS7enveloped test failed!\n", ret);
  726. else
  727. printf( "PKCS7enveloped test passed!\n");
  728. if ( (ret = pkcs7signed_test()) != 0)
  729. return err_sys("PKCS7signed test failed!\n", ret);
  730. else
  731. printf( "PKCS7signed test passed!\n");
  732. if ( (ret = pkcs7encrypted_test()) != 0)
  733. return err_sys("PKCS7encrypted test failed!\n", ret);
  734. else
  735. printf( "PKCS7encrypted test passed!\n");
  736. #endif
  737. #ifdef HAVE_VALGRIND
  738. if ( (ret = mp_test()) != 0)
  739. return err_sys("mp test failed!\n", ret);
  740. else
  741. printf( "mp test passed!\n");
  742. #endif
  743. #ifdef HAVE_VALGRIND
  744. if ( (ret = logging_test()) != 0)
  745. return err_sys("logging test failed!\n", ret);
  746. else
  747. printf( "logging test passed!\n");
  748. #endif
  749. if ( (ret = mutex_test()) != 0)
  750. return err_sys("mutex test failed!\n", ret);
  751. else
  752. printf( "mutex test passed!\n");
  753. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  754. if ( (ret = memcb_test()) != 0)
  755. return err_sys("memcb test failed!\n", ret);
  756. else
  757. printf( "memcb test passed!\n");
  758. #endif
  759. #ifdef WOLFSSL_ASYNC_CRYPT
  760. wolfAsync_DevClose(&devId);
  761. #endif
  762. ((func_args*)args)->return_code = ret;
  763. EXIT_TEST(ret);
  764. }
  765. #ifndef NO_MAIN_DRIVER
  766. /* so overall tests can pull in test function */
  767. int main(int argc, char** argv)
  768. {
  769. func_args args;
  770. #ifdef HAVE_WNR
  771. if (wc_InitNetRandom(wnrConfigFile, NULL, 5000) != 0) {
  772. err_sys("Whitewood netRandom global config failed", -1002);
  773. return -1002;
  774. }
  775. #endif
  776. args.argc = argc;
  777. args.argv = argv;
  778. wolfCrypt_Init();
  779. #ifdef HAVE_STACK_SIZE
  780. StackSizeCheck(&args, wolfcrypt_test);
  781. #else
  782. wolfcrypt_test(&args);
  783. #endif
  784. if (wolfCrypt_Cleanup() != 0) {
  785. err_sys("Error with wolfCrypt_Cleanup!\n", -1003);
  786. }
  787. #ifdef HAVE_WNR
  788. if (wc_FreeNetRandom() < 0)
  789. err_sys("Failed to free netRandom context", -1004);
  790. #endif /* HAVE_WNR */
  791. return args.return_code;
  792. }
  793. #endif /* NO_MAIN_DRIVER */
  794. /* helper to save DER, convert to PEM and save PEM */
  795. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  796. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  797. #define SaveDerAndPem(d, dSz, p, pSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, p, pSz, fD, fP, pT, eB)
  798. #else
  799. #define SaveDerAndPem(d, dSz, p, pSz, fD, fP, pT, eB) _SaveDerAndPem(d, dSz, p, pSz, NULL, NULL, pT, eB)
  800. #endif
  801. static int _SaveDerAndPem(const byte* der, int derSz,
  802. byte* pem, int pemSz, const char* fileDer,
  803. const char* filePem, int pemType, int errBase)
  804. {
  805. int ret;
  806. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  807. FILE* derFile;
  808. derFile = fopen(fileDer, "wb");
  809. if (!derFile) {
  810. return errBase + 0;
  811. }
  812. ret = (int)fwrite(der, 1, derSz, derFile);
  813. fclose(derFile);
  814. if (ret != derSz) {
  815. return errBase + 1;
  816. }
  817. #endif
  818. if (pem && filePem) {
  819. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  820. FILE* pemFile;
  821. #endif
  822. pemSz = wc_DerToPem(der, derSz, pem, pemSz, pemType);
  823. if (pemSz < 0) {
  824. return errBase + 2;
  825. }
  826. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  827. pemFile = fopen(filePem, "wb");
  828. if (!pemFile) {
  829. return errBase + 3;
  830. }
  831. ret = (int)fwrite(pem, 1, pemSz, pemFile);
  832. fclose(pemFile);
  833. if (ret != pemSz) {
  834. return errBase + 4;
  835. }
  836. #endif
  837. }
  838. /* suppress unused variable warnings */
  839. (void)filePem;
  840. (void)fileDer;
  841. return 0;
  842. }
  843. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  844. int error_test(void)
  845. {
  846. const char* errStr;
  847. char out[WOLFSSL_MAX_ERROR_SZ];
  848. const char* unknownStr = wc_GetErrorString(0);
  849. #ifdef NO_ERROR_STRINGS
  850. /* Ensure a valid error code's string matches an invalid code's.
  851. * The string is that error strings are not available.
  852. */
  853. errStr = wc_GetErrorString(OPEN_RAN_E);
  854. wc_ErrorString(OPEN_RAN_E, out);
  855. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  856. return -1100;
  857. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  858. return -1101;
  859. #else
  860. int i;
  861. int j = 0;
  862. /* Values that are not or no longer error codes. */
  863. int missing[] = { -122, -123, -124, -127, -128, -129,
  864. -161, -162, -163, -164, -165, -166, -167, -168, -169,
  865. -179, -233,
  866. 0 };
  867. /* Check that all errors have a string and it's the same through the two
  868. * APIs. Check that the values that are not errors map to the unknown
  869. * string.
  870. */
  871. for (i = MAX_CODE_E-1; i >= WC_LAST_E; i--) {
  872. errStr = wc_GetErrorString(i);
  873. wc_ErrorString(i, out);
  874. if (i != missing[j]) {
  875. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) == 0)
  876. return -1102;
  877. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) == 0)
  878. return -1103;
  879. if (XSTRNCMP(errStr, out, XSTRLEN(errStr)) != 0)
  880. return -1104;
  881. }
  882. else {
  883. j++;
  884. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  885. return -1105;
  886. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  887. return -1106;
  888. }
  889. }
  890. /* Check if the next possible value has been given a string. */
  891. errStr = wc_GetErrorString(i);
  892. wc_ErrorString(i, out);
  893. if (XSTRNCMP(errStr, unknownStr, XSTRLEN(unknownStr)) != 0)
  894. return -1107;
  895. if (XSTRNCMP(out, unknownStr, XSTRLEN(unknownStr)) != 0)
  896. return -1108;
  897. #endif
  898. return 0;
  899. }
  900. #if !defined(NO_CODING) && defined(WOLFSSL_BASE64_ENCODE)
  901. int base64_test(void)
  902. {
  903. int ret;
  904. const byte good[] = "A+Gd\0\0\0";
  905. const byte goodEnd[] = "A+Gd \r\n";
  906. byte out[128];
  907. word32 outLen;
  908. byte data[3];
  909. word32 dataLen;
  910. byte longData[79] = { 0 };
  911. const byte symbols[] = "+/A=";
  912. const byte badSmall[] = "AAA Gdj=";
  913. const byte badLarge[] = "AAA~Gdj=";
  914. const byte badEOL[] = "A+Gd ";
  915. int i;
  916. /* Good Base64 encodings. */
  917. outLen = sizeof(out);
  918. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  919. if (ret != 0)
  920. return -1200;
  921. outLen = sizeof(out);
  922. ret = Base64_Decode(goodEnd, sizeof(goodEnd), out, &outLen);
  923. if (ret != 0)
  924. return -1201;
  925. /* Bad parameters. */
  926. outLen = 1;
  927. ret = Base64_Decode(good, sizeof(good), out, &outLen);
  928. if (ret != BAD_FUNC_ARG)
  929. return -1202;
  930. outLen = sizeof(out);
  931. ret = Base64_Decode(badEOL, sizeof(badEOL), out, &outLen);
  932. if (ret != ASN_INPUT_E)
  933. return -1203;
  934. /* Bad character at each offset 0-3. */
  935. for (i = 0; i < 4; i++) {
  936. outLen = sizeof(out);
  937. ret = Base64_Decode(badSmall + i, 4, out, &outLen);
  938. if (ret != ASN_INPUT_E)
  939. return -1204 - i;
  940. ret = Base64_Decode(badLarge + i, 4, out, &outLen);
  941. if (ret != ASN_INPUT_E)
  942. return -1214 - i;
  943. }
  944. /* Decode and encode all symbols - non-alphanumeric. */
  945. dataLen = sizeof(data);
  946. ret = Base64_Decode(symbols, sizeof(symbols), data, &dataLen);
  947. if (ret != 0)
  948. return -1224;
  949. outLen = sizeof(out);
  950. ret = Base64_Encode(data, dataLen, NULL, &outLen);
  951. if (ret != LENGTH_ONLY_E)
  952. return -1225;
  953. outLen = sizeof(out);
  954. ret = Base64_Encode(data, dataLen, out, &outLen);
  955. if (ret != 0)
  956. return -1226;
  957. outLen = 7;
  958. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  959. if (ret != BUFFER_E)
  960. return -1227;
  961. outLen = sizeof(out);
  962. ret = Base64_EncodeEsc(data, dataLen, NULL, &outLen);
  963. if (ret != LENGTH_ONLY_E)
  964. return -1228;
  965. outLen = sizeof(out);
  966. ret = Base64_EncodeEsc(data, dataLen, out, &outLen);
  967. if (ret != 0)
  968. return -1229;
  969. outLen = sizeof(out);
  970. ret = Base64_Encode_NoNl(data, dataLen, out, &outLen);
  971. if (ret != 0)
  972. return -1230;
  973. /* Data that results in an encoding longer than one line. */
  974. outLen = sizeof(out);
  975. dataLen = sizeof(longData);
  976. ret = Base64_Encode(longData, dataLen, out, &outLen);
  977. if (ret != 0)
  978. return -1231;
  979. outLen = sizeof(out);
  980. ret = Base64_EncodeEsc(longData, dataLen, out, &outLen);
  981. if (ret != 0)
  982. return -1232;
  983. outLen = sizeof(out);
  984. ret = Base64_Encode_NoNl(longData, dataLen, out, &outLen);
  985. if (ret != 0)
  986. return -1233;
  987. return 0;
  988. }
  989. #endif
  990. #ifndef NO_ASN
  991. int asn_test(void)
  992. {
  993. #ifndef NO_ASN_TIME
  994. #ifdef WORD64_AVAILABLE
  995. word64 now;
  996. #else
  997. word32 now;
  998. #endif
  999. /* Parameter Validation tests. */
  1000. if (wc_GetTime(NULL, sizeof(now)) != BAD_FUNC_ARG)
  1001. return -1300;
  1002. if (wc_GetTime(&now, 0) != BUFFER_E)
  1003. return -1301;
  1004. now = 0;
  1005. if (wc_GetTime(&now, sizeof(now)) != 0) {
  1006. return -1302;
  1007. }
  1008. if (now == 0) {
  1009. printf("RTC/Time not set!\n");
  1010. return -1303;
  1011. }
  1012. #endif
  1013. return 0;
  1014. }
  1015. #endif
  1016. #ifdef WOLFSSL_MD2
  1017. int md2_test(void)
  1018. {
  1019. Md2 md2;
  1020. byte hash[MD2_DIGEST_SIZE];
  1021. testVector a, b, c, d, e, f, g;
  1022. testVector test_md2[7];
  1023. int times = sizeof(test_md2) / sizeof(testVector), i;
  1024. a.input = "";
  1025. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  1026. "\x27\x73";
  1027. a.inLen = XSTRLEN(a.input);
  1028. a.outLen = MD2_DIGEST_SIZE;
  1029. b.input = "a";
  1030. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  1031. "\xb5\xd1";
  1032. b.inLen = XSTRLEN(b.input);
  1033. b.outLen = MD2_DIGEST_SIZE;
  1034. c.input = "abc";
  1035. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  1036. "\xd6\xbb";
  1037. c.inLen = XSTRLEN(c.input);
  1038. c.outLen = MD2_DIGEST_SIZE;
  1039. d.input = "message digest";
  1040. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  1041. "\x06\xb0";
  1042. d.inLen = XSTRLEN(d.input);
  1043. d.outLen = MD2_DIGEST_SIZE;
  1044. e.input = "abcdefghijklmnopqrstuvwxyz";
  1045. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  1046. "\x94\x0b";
  1047. e.inLen = XSTRLEN(e.input);
  1048. e.outLen = MD2_DIGEST_SIZE;
  1049. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1050. "6789";
  1051. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  1052. "\x38\xcd";
  1053. f.inLen = XSTRLEN(f.input);
  1054. f.outLen = MD2_DIGEST_SIZE;
  1055. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1056. "9012345678901234567890";
  1057. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  1058. "\xef\xd8";
  1059. g.inLen = XSTRLEN(g.input);
  1060. g.outLen = MD2_DIGEST_SIZE;
  1061. test_md2[0] = a;
  1062. test_md2[1] = b;
  1063. test_md2[2] = c;
  1064. test_md2[3] = d;
  1065. test_md2[4] = e;
  1066. test_md2[5] = f;
  1067. test_md2[6] = g;
  1068. wc_InitMd2(&md2);
  1069. for (i = 0; i < times; ++i) {
  1070. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  1071. wc_Md2Final(&md2, hash);
  1072. if (XMEMCMP(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  1073. return -1400 - i;
  1074. }
  1075. return 0;
  1076. }
  1077. #endif
  1078. #ifndef NO_MD5
  1079. int md5_test(void)
  1080. {
  1081. int ret = 0;
  1082. wc_Md5 md5;
  1083. byte hash[WC_MD5_DIGEST_SIZE];
  1084. byte hashcopy[WC_MD5_DIGEST_SIZE];
  1085. testVector a, b, c, d, e, f;
  1086. testVector test_md5[6];
  1087. int times = sizeof(test_md5) / sizeof(testVector), i;
  1088. a.input = "";
  1089. a.output = "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42"
  1090. "\x7e";
  1091. a.inLen = XSTRLEN(a.input);
  1092. a.outLen = WC_MD5_DIGEST_SIZE;
  1093. b.input = "abc";
  1094. b.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  1095. "\x72";
  1096. b.inLen = XSTRLEN(b.input);
  1097. b.outLen = WC_MD5_DIGEST_SIZE;
  1098. c.input = "message digest";
  1099. c.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  1100. "\xd0";
  1101. c.inLen = XSTRLEN(c.input);
  1102. c.outLen = WC_MD5_DIGEST_SIZE;
  1103. d.input = "abcdefghijklmnopqrstuvwxyz";
  1104. d.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  1105. "\x3b";
  1106. d.inLen = XSTRLEN(d.input);
  1107. d.outLen = WC_MD5_DIGEST_SIZE;
  1108. e.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1109. "6789";
  1110. e.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  1111. "\x9f";
  1112. e.inLen = XSTRLEN(e.input);
  1113. e.outLen = WC_MD5_DIGEST_SIZE;
  1114. f.input = "1234567890123456789012345678901234567890123456789012345678"
  1115. "9012345678901234567890";
  1116. f.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  1117. "\x7a";
  1118. f.inLen = XSTRLEN(f.input);
  1119. f.outLen = WC_MD5_DIGEST_SIZE;
  1120. test_md5[0] = a;
  1121. test_md5[1] = b;
  1122. test_md5[2] = c;
  1123. test_md5[3] = d;
  1124. test_md5[4] = e;
  1125. test_md5[5] = f;
  1126. ret = wc_InitMd5_ex(&md5, HEAP_HINT, devId);
  1127. if (ret != 0)
  1128. return -1500;
  1129. for (i = 0; i < times; ++i) {
  1130. ret = wc_Md5Update(&md5, (byte*)test_md5[i].input,
  1131. (word32)test_md5[i].inLen);
  1132. if (ret != 0)
  1133. ERROR_OUT(-1510 - i, exit);
  1134. ret = wc_Md5GetHash(&md5, hashcopy);
  1135. if (ret != 0)
  1136. ERROR_OUT(-1520 - i, exit);
  1137. ret = wc_Md5Final(&md5, hash);
  1138. if (ret != 0)
  1139. ERROR_OUT(-1530 - i, exit);
  1140. if (XMEMCMP(hash, test_md5[i].output, WC_MD5_DIGEST_SIZE) != 0)
  1141. ERROR_OUT(-1540 - i, exit);
  1142. if (XMEMCMP(hash, hashcopy, WC_MD5_DIGEST_SIZE) != 0)
  1143. ERROR_OUT(-1550 - i, exit);
  1144. }
  1145. /* BEGIN LARGE HASH TEST */ {
  1146. byte large_input[1024];
  1147. const char* large_digest =
  1148. "\x44\xd0\x88\xce\xf1\x36\xd1\x78\xe9\xc8\xba\x84\xc3\xfd\xf6\xca";
  1149. for (i = 0; i < (int)sizeof(large_input); i++) {
  1150. large_input[i] = (byte)(i & 0xFF);
  1151. }
  1152. times = 100;
  1153. #ifdef WOLFSSL_PIC32MZ_HASH
  1154. wc_Md5SizeSet(&md5, times * sizeof(large_input));
  1155. #endif
  1156. for (i = 0; i < times; ++i) {
  1157. ret = wc_Md5Update(&md5, (byte*)large_input,
  1158. (word32)sizeof(large_input));
  1159. if (ret != 0)
  1160. ERROR_OUT(-1560, exit);
  1161. }
  1162. ret = wc_Md5Final(&md5, hash);
  1163. if (ret != 0)
  1164. ERROR_OUT(-1561, exit);
  1165. if (XMEMCMP(hash, large_digest, WC_MD5_DIGEST_SIZE) != 0)
  1166. ERROR_OUT(-1562, exit);
  1167. } /* END LARGE HASH TEST */
  1168. exit:
  1169. wc_Md5Free(&md5);
  1170. return ret;
  1171. }
  1172. #endif /* NO_MD5 */
  1173. #ifndef NO_MD4
  1174. int md4_test(void)
  1175. {
  1176. Md4 md4;
  1177. byte hash[MD4_DIGEST_SIZE];
  1178. testVector a, b, c, d, e, f, g;
  1179. testVector test_md4[7];
  1180. int times = sizeof(test_md4) / sizeof(testVector), i;
  1181. a.input = "";
  1182. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  1183. "\xc0";
  1184. a.inLen = XSTRLEN(a.input);
  1185. a.outLen = MD4_DIGEST_SIZE;
  1186. b.input = "a";
  1187. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  1188. "\x24";
  1189. b.inLen = XSTRLEN(b.input);
  1190. b.outLen = MD4_DIGEST_SIZE;
  1191. c.input = "abc";
  1192. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  1193. "\x9d";
  1194. c.inLen = XSTRLEN(c.input);
  1195. c.outLen = MD4_DIGEST_SIZE;
  1196. d.input = "message digest";
  1197. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  1198. "\x4b";
  1199. d.inLen = XSTRLEN(d.input);
  1200. d.outLen = MD4_DIGEST_SIZE;
  1201. e.input = "abcdefghijklmnopqrstuvwxyz";
  1202. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  1203. "\xa9";
  1204. e.inLen = XSTRLEN(e.input);
  1205. e.outLen = MD4_DIGEST_SIZE;
  1206. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  1207. "6789";
  1208. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  1209. "\xe4";
  1210. f.inLen = XSTRLEN(f.input);
  1211. f.outLen = MD4_DIGEST_SIZE;
  1212. g.input = "1234567890123456789012345678901234567890123456789012345678"
  1213. "9012345678901234567890";
  1214. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  1215. "\x36";
  1216. g.inLen = XSTRLEN(g.input);
  1217. g.outLen = MD4_DIGEST_SIZE;
  1218. test_md4[0] = a;
  1219. test_md4[1] = b;
  1220. test_md4[2] = c;
  1221. test_md4[3] = d;
  1222. test_md4[4] = e;
  1223. test_md4[5] = f;
  1224. test_md4[6] = g;
  1225. wc_InitMd4(&md4);
  1226. for (i = 0; i < times; ++i) {
  1227. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  1228. wc_Md4Final(&md4, hash);
  1229. if (XMEMCMP(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  1230. return -1600 - i;
  1231. }
  1232. return 0;
  1233. }
  1234. #endif /* NO_MD4 */
  1235. #ifndef NO_SHA
  1236. int sha_test(void)
  1237. {
  1238. int ret = 0;
  1239. wc_Sha sha;
  1240. byte hash[WC_SHA_DIGEST_SIZE];
  1241. byte hashcopy[WC_SHA_DIGEST_SIZE];
  1242. testVector a, b, c, d, e;
  1243. testVector test_sha[5];
  1244. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1245. a.input = "";
  1246. a.output = "\xda\x39\xa3\xee\x5e\x6b\x4b\x0d\x32\x55\xbf\xef\x95\x60\x18"
  1247. "\x90\xaf\xd8\x07\x09";
  1248. a.inLen = XSTRLEN(a.input);
  1249. a.outLen = WC_SHA_DIGEST_SIZE;
  1250. b.input = "abc";
  1251. b.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  1252. "\x6C\x9C\xD0\xD8\x9D";
  1253. b.inLen = XSTRLEN(b.input);
  1254. b.outLen = WC_SHA_DIGEST_SIZE;
  1255. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1256. c.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  1257. "\xE5\xE5\x46\x70\xF1";
  1258. c.inLen = XSTRLEN(c.input);
  1259. c.outLen = WC_SHA_DIGEST_SIZE;
  1260. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1261. "aaaaaa";
  1262. d.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  1263. "\x2A\x25\xEC\x64\x4D";
  1264. d.inLen = XSTRLEN(d.input);
  1265. d.outLen = WC_SHA_DIGEST_SIZE;
  1266. e.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1267. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  1268. "aaaaaaaaaa";
  1269. e.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  1270. "\x53\x99\x5E\x26\xA0";
  1271. e.inLen = XSTRLEN(e.input);
  1272. e.outLen = WC_SHA_DIGEST_SIZE;
  1273. test_sha[0] = a;
  1274. test_sha[1] = b;
  1275. test_sha[2] = c;
  1276. test_sha[3] = d;
  1277. test_sha[4] = e;
  1278. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  1279. if (ret != 0)
  1280. return -1700;
  1281. for (i = 0; i < times; ++i) {
  1282. ret = wc_ShaUpdate(&sha, (byte*)test_sha[i].input,
  1283. (word32)test_sha[i].inLen);
  1284. if (ret != 0)
  1285. ERROR_OUT(-1710 - i, exit);
  1286. ret = wc_ShaGetHash(&sha, hashcopy);
  1287. if (ret != 0)
  1288. ERROR_OUT(-1720 - i, exit);
  1289. ret = wc_ShaFinal(&sha, hash);
  1290. if (ret != 0)
  1291. ERROR_OUT(-1730 - i, exit);
  1292. if (XMEMCMP(hash, test_sha[i].output, WC_SHA_DIGEST_SIZE) != 0)
  1293. ERROR_OUT(-1740 - i, exit);
  1294. if (XMEMCMP(hash, hashcopy, WC_SHA_DIGEST_SIZE) != 0)
  1295. ERROR_OUT(-1750 - i, exit);
  1296. }
  1297. /* BEGIN LARGE HASH TEST */ {
  1298. byte large_input[1024];
  1299. const char* large_digest =
  1300. "\x8b\x77\x02\x48\x39\xe8\xdb\xd3\x9a\xf4\x05\x24\x66\x12\x2d\x9e"
  1301. "\xc5\xd9\x0a\xac";
  1302. for (i = 0; i < (int)sizeof(large_input); i++) {
  1303. large_input[i] = (byte)(i & 0xFF);
  1304. }
  1305. times = 100;
  1306. #ifdef WOLFSSL_PIC32MZ_HASH
  1307. wc_ShaSizeSet(&sha, times * sizeof(large_input));
  1308. #endif
  1309. for (i = 0; i < times; ++i) {
  1310. ret = wc_ShaUpdate(&sha, (byte*)large_input,
  1311. (word32)sizeof(large_input));
  1312. if (ret != 0)
  1313. ERROR_OUT(-1760, exit);
  1314. }
  1315. ret = wc_ShaFinal(&sha, hash);
  1316. if (ret != 0)
  1317. ERROR_OUT(-1761, exit);
  1318. if (XMEMCMP(hash, large_digest, WC_SHA_DIGEST_SIZE) != 0)
  1319. ERROR_OUT(-1762, exit);
  1320. } /* END LARGE HASH TEST */
  1321. exit:
  1322. wc_ShaFree(&sha);
  1323. return ret;
  1324. }
  1325. #endif /* NO_SHA */
  1326. #ifdef WOLFSSL_RIPEMD
  1327. int ripemd_test(void)
  1328. {
  1329. RipeMd ripemd;
  1330. int ret;
  1331. byte hash[RIPEMD_DIGEST_SIZE];
  1332. testVector a, b, c, d;
  1333. testVector test_ripemd[4];
  1334. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  1335. a.input = "abc";
  1336. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  1337. "\xb0\x87\xf1\x5a\x0b\xfc";
  1338. a.inLen = XSTRLEN(a.input);
  1339. a.outLen = RIPEMD_DIGEST_SIZE;
  1340. b.input = "message digest";
  1341. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  1342. "\x5f\xfa\x21\x59\x5f\x36";
  1343. b.inLen = XSTRLEN(b.input);
  1344. b.outLen = RIPEMD_DIGEST_SIZE;
  1345. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1346. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  1347. "\xf4\x9a\xda\x62\xeb\x2b";
  1348. c.inLen = XSTRLEN(c.input);
  1349. c.outLen = RIPEMD_DIGEST_SIZE;
  1350. d.input = "12345678901234567890123456789012345678901234567890123456"
  1351. "789012345678901234567890";
  1352. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  1353. "\x82\xbf\x63\x32\x6b\xfb";
  1354. d.inLen = XSTRLEN(d.input);
  1355. d.outLen = RIPEMD_DIGEST_SIZE;
  1356. test_ripemd[0] = a;
  1357. test_ripemd[1] = b;
  1358. test_ripemd[2] = c;
  1359. test_ripemd[3] = d;
  1360. ret = wc_InitRipeMd(&ripemd);
  1361. if (ret != 0) {
  1362. return -1800;
  1363. }
  1364. for (i = 0; i < times; ++i) {
  1365. ret = wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  1366. (word32)test_ripemd[i].inLen);
  1367. if (ret != 0) {
  1368. return -1810 - i;
  1369. }
  1370. ret = wc_RipeMdFinal(&ripemd, hash);
  1371. if (ret != 0) {
  1372. return -1820 - i;
  1373. }
  1374. if (XMEMCMP(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  1375. return -1830 - i;
  1376. }
  1377. return 0;
  1378. }
  1379. #endif /* WOLFSSL_RIPEMD */
  1380. #ifdef HAVE_BLAKE2
  1381. #define BLAKE2_TESTS 3
  1382. static const byte blake2b_vec[BLAKE2_TESTS][BLAKE2B_OUTBYTES] =
  1383. {
  1384. {
  1385. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  1386. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  1387. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  1388. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  1389. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  1390. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  1391. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  1392. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  1393. },
  1394. {
  1395. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  1396. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  1397. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  1398. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  1399. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  1400. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  1401. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  1402. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  1403. },
  1404. {
  1405. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  1406. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  1407. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  1408. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  1409. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  1410. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  1411. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  1412. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  1413. }
  1414. };
  1415. int blake2b_test(void)
  1416. {
  1417. Blake2b b2b;
  1418. byte digest[64];
  1419. byte input[64];
  1420. int i, ret;
  1421. for (i = 0; i < (int)sizeof(input); i++)
  1422. input[i] = (byte)i;
  1423. for (i = 0; i < BLAKE2_TESTS; i++) {
  1424. ret = wc_InitBlake2b(&b2b, 64);
  1425. if (ret != 0)
  1426. return -1900 - i;
  1427. ret = wc_Blake2bUpdate(&b2b, input, i);
  1428. if (ret != 0)
  1429. return -1910 - 1;
  1430. ret = wc_Blake2bFinal(&b2b, digest, 64);
  1431. if (ret != 0)
  1432. return -1920 - i;
  1433. if (XMEMCMP(digest, blake2b_vec[i], 64) != 0) {
  1434. return -1930 - i;
  1435. }
  1436. }
  1437. return 0;
  1438. }
  1439. #endif /* HAVE_BLAKE2 */
  1440. #ifdef WOLFSSL_SHA224
  1441. int sha224_test(void)
  1442. {
  1443. wc_Sha224 sha;
  1444. byte hash[WC_SHA224_DIGEST_SIZE];
  1445. byte hashcopy[WC_SHA224_DIGEST_SIZE];
  1446. int ret = 0;
  1447. testVector a, b, c;
  1448. testVector test_sha[3];
  1449. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1450. a.input = "";
  1451. a.output = "\xd1\x4a\x02\x8c\x2a\x3a\x2b\xc9\x47\x61\x02\xbb\x28\x82\x34"
  1452. "\xc4\x15\xa2\xb0\x1f\x82\x8e\xa6\x2a\xc5\xb3\xe4\x2f";
  1453. a.inLen = XSTRLEN(a.input);
  1454. a.outLen = WC_SHA224_DIGEST_SIZE;
  1455. b.input = "abc";
  1456. b.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  1457. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  1458. b.inLen = XSTRLEN(b.input);
  1459. b.outLen = WC_SHA224_DIGEST_SIZE;
  1460. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1461. c.output = "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  1462. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  1463. c.inLen = XSTRLEN(c.input);
  1464. c.outLen = WC_SHA224_DIGEST_SIZE;
  1465. test_sha[0] = a;
  1466. test_sha[1] = b;
  1467. test_sha[2] = c;
  1468. ret = wc_InitSha224_ex(&sha, HEAP_HINT, devId);
  1469. if (ret != 0)
  1470. return -2000;
  1471. for (i = 0; i < times; ++i) {
  1472. ret = wc_Sha224Update(&sha, (byte*)test_sha[i].input,
  1473. (word32)test_sha[i].inLen);
  1474. if (ret != 0)
  1475. ERROR_OUT(-2010 - i, exit);
  1476. ret = wc_Sha224GetHash(&sha, hashcopy);
  1477. if (ret != 0)
  1478. ERROR_OUT(-2020 - i, exit);
  1479. ret = wc_Sha224Final(&sha, hash);
  1480. if (ret != 0)
  1481. ERROR_OUT(-2030 - i, exit);
  1482. if (XMEMCMP(hash, test_sha[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  1483. ERROR_OUT(-2040 - i, exit);
  1484. if (XMEMCMP(hash, hashcopy, WC_SHA224_DIGEST_SIZE) != 0)
  1485. ERROR_OUT(-2050 - i, exit);
  1486. }
  1487. exit:
  1488. wc_Sha224Free(&sha);
  1489. return ret;
  1490. }
  1491. #endif
  1492. #ifndef NO_SHA256
  1493. int sha256_test(void)
  1494. {
  1495. wc_Sha256 sha;
  1496. byte hash[WC_SHA256_DIGEST_SIZE];
  1497. byte hashcopy[WC_SHA256_DIGEST_SIZE];
  1498. int ret = 0;
  1499. testVector a, b, c;
  1500. testVector test_sha[3];
  1501. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1502. a.input = "";
  1503. a.output = "\xe3\xb0\xc4\x42\x98\xfc\x1c\x14\x9a\xfb\xf4\xc8\x99\x6f\xb9"
  1504. "\x24\x27\xae\x41\xe4\x64\x9b\x93\x4c\xa4\x95\x99\x1b\x78\x52"
  1505. "\xb8\x55";
  1506. a.inLen = XSTRLEN(a.input);
  1507. a.outLen = WC_SHA256_DIGEST_SIZE;
  1508. b.input = "abc";
  1509. b.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  1510. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  1511. "\x15\xAD";
  1512. b.inLen = XSTRLEN(b.input);
  1513. b.outLen = WC_SHA256_DIGEST_SIZE;
  1514. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1515. c.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  1516. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  1517. "\x06\xC1";
  1518. c.inLen = XSTRLEN(c.input);
  1519. c.outLen = WC_SHA256_DIGEST_SIZE;
  1520. test_sha[0] = a;
  1521. test_sha[1] = b;
  1522. test_sha[2] = c;
  1523. ret = wc_InitSha256_ex(&sha, HEAP_HINT, devId);
  1524. if (ret != 0)
  1525. return -2100;
  1526. for (i = 0; i < times; ++i) {
  1527. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,
  1528. (word32)test_sha[i].inLen);
  1529. if (ret != 0)
  1530. ERROR_OUT(-2110 - i, exit);
  1531. ret = wc_Sha256GetHash(&sha, hashcopy);
  1532. if (ret != 0)
  1533. ERROR_OUT(-2120 - i, exit);
  1534. ret = wc_Sha256Final(&sha, hash);
  1535. if (ret != 0)
  1536. ERROR_OUT(-2130 - i, exit);
  1537. if (XMEMCMP(hash, test_sha[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  1538. ERROR_OUT(-2140 - i, exit);
  1539. if (XMEMCMP(hash, hashcopy, WC_SHA256_DIGEST_SIZE) != 0)
  1540. ERROR_OUT(-2150 - i, exit);
  1541. }
  1542. /* BEGIN LARGE HASH TEST */ {
  1543. byte large_input[1024];
  1544. const char* large_digest =
  1545. "\x27\x78\x3e\x87\x96\x3a\x4e\xfb\x68\x29\xb5\x31\xc9\xba\x57\xb4"
  1546. "\x4f\x45\x79\x7f\x67\x70\xbd\x63\x7f\xbf\x0d\x80\x7c\xbd\xba\xe0";
  1547. for (i = 0; i < (int)sizeof(large_input); i++) {
  1548. large_input[i] = (byte)(i & 0xFF);
  1549. }
  1550. times = 100;
  1551. #ifdef WOLFSSL_PIC32MZ_HASH
  1552. wc_Sha256SizeSet(&sha, times * sizeof(large_input));
  1553. #endif
  1554. for (i = 0; i < times; ++i) {
  1555. ret = wc_Sha256Update(&sha, (byte*)large_input,
  1556. (word32)sizeof(large_input));
  1557. if (ret != 0)
  1558. ERROR_OUT(-2160, exit);
  1559. }
  1560. ret = wc_Sha256Final(&sha, hash);
  1561. if (ret != 0)
  1562. ERROR_OUT(-2161, exit);
  1563. if (XMEMCMP(hash, large_digest, WC_SHA256_DIGEST_SIZE) != 0)
  1564. ERROR_OUT(-2162, exit);
  1565. } /* END LARGE HASH TEST */
  1566. exit:
  1567. wc_Sha256Free(&sha);
  1568. return ret;
  1569. }
  1570. #endif
  1571. #ifdef WOLFSSL_SHA512
  1572. int sha512_test(void)
  1573. {
  1574. wc_Sha512 sha;
  1575. byte hash[WC_SHA512_DIGEST_SIZE];
  1576. byte hashcopy[WC_SHA512_DIGEST_SIZE];
  1577. int ret = 0;
  1578. testVector a, b, c;
  1579. testVector test_sha[3];
  1580. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1581. a.input = "";
  1582. a.output = "\xcf\x83\xe1\x35\x7e\xef\xb8\xbd\xf1\x54\x28\x50\xd6\x6d\x80"
  1583. "\x07\xd6\x20\xe4\x05\x0b\x57\x15\xdc\x83\xf4\xa9\x21\xd3\x6c"
  1584. "\xe9\xce\x47\xd0\xd1\x3c\x5d\x85\xf2\xb0\xff\x83\x18\xd2\x87"
  1585. "\x7e\xec\x2f\x63\xb9\x31\xbd\x47\x41\x7a\x81\xa5\x38\x32\x7a"
  1586. "\xf9\x27\xda\x3e";
  1587. a.inLen = XSTRLEN(a.input);
  1588. a.outLen = WC_SHA512_DIGEST_SIZE;
  1589. b.input = "abc";
  1590. b.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  1591. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  1592. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  1593. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  1594. "\xa5\x4c\xa4\x9f";
  1595. b.inLen = XSTRLEN(b.input);
  1596. b.outLen = WC_SHA512_DIGEST_SIZE;
  1597. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  1598. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  1599. c.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  1600. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  1601. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  1602. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  1603. "\x87\x4b\xe9\x09";
  1604. c.inLen = XSTRLEN(c.input);
  1605. c.outLen = WC_SHA512_DIGEST_SIZE;
  1606. test_sha[0] = a;
  1607. test_sha[1] = b;
  1608. test_sha[2] = c;
  1609. ret = wc_InitSha512_ex(&sha, HEAP_HINT, devId);
  1610. if (ret != 0)
  1611. return -2200;
  1612. for (i = 0; i < times; ++i) {
  1613. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,
  1614. (word32)test_sha[i].inLen);
  1615. if (ret != 0)
  1616. ERROR_OUT(-2210 - i, exit);
  1617. ret = wc_Sha512GetHash(&sha, hashcopy);
  1618. if (ret != 0)
  1619. ERROR_OUT(-2220 - i, exit);
  1620. ret = wc_Sha512Final(&sha, hash);
  1621. if (ret != 0)
  1622. ERROR_OUT(-2230 - i, exit);
  1623. if (XMEMCMP(hash, test_sha[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  1624. ERROR_OUT(-2240 - i, exit);
  1625. if (XMEMCMP(hash, hashcopy, WC_SHA512_DIGEST_SIZE) != 0)
  1626. ERROR_OUT(-2250 - i, exit);
  1627. }
  1628. /* BEGIN LARGE HASH TEST */ {
  1629. byte large_input[1024];
  1630. const char* large_digest =
  1631. "\x5a\x1f\x73\x90\xbd\x8c\xe4\x63\x54\xce\xa0\x9b\xef\x32\x78\x2d"
  1632. "\x2e\xe7\x0d\x5e\x2f\x9d\x15\x1b\xdd\x2d\xde\x65\x0c\x7b\xfa\x83"
  1633. "\x5e\x80\x02\x13\x84\xb8\x3f\xff\x71\x62\xb5\x09\x89\x63\xe1\xdc"
  1634. "\xa5\xdc\xfc\xfa\x9d\x1a\x4d\xc0\xfa\x3a\x14\xf6\x01\x51\x90\xa4";
  1635. for (i = 0; i < (int)sizeof(large_input); i++) {
  1636. large_input[i] = (byte)(i & 0xFF);
  1637. }
  1638. times = 100;
  1639. for (i = 0; i < times; ++i) {
  1640. ret = wc_Sha512Update(&sha, (byte*)large_input,
  1641. (word32)sizeof(large_input));
  1642. if (ret != 0)
  1643. ERROR_OUT(-2260, exit);
  1644. }
  1645. ret = wc_Sha512Final(&sha, hash);
  1646. if (ret != 0)
  1647. ERROR_OUT(-2261, exit);
  1648. if (XMEMCMP(hash, large_digest, WC_SHA512_DIGEST_SIZE) != 0)
  1649. ERROR_OUT(-2262, exit);
  1650. } /* END LARGE HASH TEST */
  1651. exit:
  1652. wc_Sha512Free(&sha);
  1653. return ret;
  1654. }
  1655. #endif
  1656. #ifdef WOLFSSL_SHA384
  1657. int sha384_test(void)
  1658. {
  1659. wc_Sha384 sha;
  1660. byte hash[WC_SHA384_DIGEST_SIZE];
  1661. byte hashcopy[WC_SHA384_DIGEST_SIZE];
  1662. int ret = 0;
  1663. testVector a, b, c;
  1664. testVector test_sha[3];
  1665. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1666. a.input = "";
  1667. a.output = "\x38\xb0\x60\xa7\x51\xac\x96\x38\x4c\xd9\x32\x7e\xb1\xb1\xe3"
  1668. "\x6a\x21\xfd\xb7\x11\x14\xbe\x07\x43\x4c\x0c\xc7\xbf\x63\xf6"
  1669. "\xe1\xda\x27\x4e\xde\xbf\xe7\x6f\x65\xfb\xd5\x1a\xd2\xf1\x48"
  1670. "\x98\xb9\x5b";
  1671. a.inLen = XSTRLEN(a.input);
  1672. a.outLen = WC_SHA384_DIGEST_SIZE;
  1673. b.input = "abc";
  1674. b.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  1675. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  1676. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  1677. "\xc8\x25\xa7";
  1678. b.inLen = XSTRLEN(b.input);
  1679. b.outLen = WC_SHA384_DIGEST_SIZE;
  1680. c.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  1681. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  1682. c.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  1683. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  1684. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  1685. "\x74\x60\x39";
  1686. c.inLen = XSTRLEN(c.input);
  1687. c.outLen = WC_SHA384_DIGEST_SIZE;
  1688. test_sha[0] = a;
  1689. test_sha[1] = b;
  1690. test_sha[2] = c;
  1691. ret = wc_InitSha384_ex(&sha, HEAP_HINT, devId);
  1692. if (ret != 0)
  1693. return -2300;
  1694. for (i = 0; i < times; ++i) {
  1695. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,
  1696. (word32)test_sha[i].inLen);
  1697. if (ret != 0)
  1698. ERROR_OUT(-2310 - i, exit);
  1699. ret = wc_Sha384GetHash(&sha, hashcopy);
  1700. if (ret != 0)
  1701. ERROR_OUT(-2320 - i, exit);
  1702. ret = wc_Sha384Final(&sha, hash);
  1703. if (ret != 0)
  1704. ERROR_OUT(-2330 - i, exit);
  1705. if (XMEMCMP(hash, test_sha[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  1706. ERROR_OUT(-2340 - i, exit);
  1707. if (XMEMCMP(hash, hashcopy, WC_SHA384_DIGEST_SIZE) != 0)
  1708. ERROR_OUT(-2350 - i, exit);
  1709. }
  1710. /* BEGIN LARGE HASH TEST */ {
  1711. byte large_input[1024];
  1712. const char* large_digest =
  1713. "\x37\x01\xdb\xff\x1e\x40\x4f\xe1\xe2\xea\x0b\x40\xbb\x3b\x39\x9a"
  1714. "\xcc\xe8\x44\x8e\x7e\xe5\x64\xb5\x6b\x7f\x56\x64\xa7\x2b\x84\xe3"
  1715. "\xc5\xd7\x79\x03\x25\x90\xf7\xa4\x58\xcb\x97\xa8\x8b\xb1\xa4\x81";
  1716. for (i = 0; i < (int)sizeof(large_input); i++) {
  1717. large_input[i] = (byte)(i & 0xFF);
  1718. }
  1719. times = 100;
  1720. for (i = 0; i < times; ++i) {
  1721. ret = wc_Sha384Update(&sha, (byte*)large_input,
  1722. (word32)sizeof(large_input));
  1723. if (ret != 0)
  1724. ERROR_OUT(-2360, exit);
  1725. }
  1726. ret = wc_Sha384Final(&sha, hash);
  1727. if (ret != 0)
  1728. ERROR_OUT(-2361, exit);
  1729. if (XMEMCMP(hash, large_digest, WC_SHA384_DIGEST_SIZE) != 0)
  1730. ERROR_OUT(-2362, exit);
  1731. } /* END LARGE HASH TEST */
  1732. exit:
  1733. wc_Sha384Free(&sha);
  1734. return ret;
  1735. }
  1736. #endif /* WOLFSSL_SHA384 */
  1737. #ifdef WOLFSSL_SHA3
  1738. #ifndef WOLFSSL_NOSHA3_224
  1739. static int sha3_224_test(void)
  1740. {
  1741. Sha3 sha;
  1742. byte hash[SHA3_224_DIGEST_SIZE];
  1743. byte hashcopy[SHA3_224_DIGEST_SIZE];
  1744. testVector a, b, c;
  1745. testVector test_sha[3];
  1746. int ret = 0;
  1747. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1748. a.input = "";
  1749. a.output = "\x6b\x4e\x03\x42\x36\x67\xdb\xb7\x3b\x6e\x15\x45\x4f\x0e\xb1"
  1750. "\xab\xd4\x59\x7f\x9a\x1b\x07\x8e\x3f\x5b\x5a\x6b\xc7";
  1751. a.inLen = XSTRLEN(a.input);
  1752. a.outLen = SHA3_224_DIGEST_SIZE;
  1753. b.input = "abc";
  1754. b.output = "\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76"
  1755. "\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf";
  1756. b.inLen = XSTRLEN(b.input);
  1757. b.outLen = SHA3_224_DIGEST_SIZE;
  1758. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1759. c.output = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55\x74\x49\x44\x79"
  1760. "\xba\x5c\x7e\x7a\xb7\x6e\xf2\x64\xea\xd0\xfc\xce\x33";
  1761. c.inLen = XSTRLEN(c.input);
  1762. c.outLen = SHA3_224_DIGEST_SIZE;
  1763. test_sha[0] = a;
  1764. test_sha[1] = b;
  1765. test_sha[2] = c;
  1766. ret = wc_InitSha3_224(&sha, HEAP_HINT, devId);
  1767. if (ret != 0)
  1768. return -2000;
  1769. for (i = 0; i < times; ++i) {
  1770. ret = wc_Sha3_224_Update(&sha, (byte*)test_sha[i].input,
  1771. (word32)test_sha[i].inLen);
  1772. if (ret != 0)
  1773. ERROR_OUT(-2010 - i, exit);
  1774. ret = wc_Sha3_224_GetHash(&sha, hashcopy);
  1775. if (ret != 0)
  1776. ERROR_OUT(-2020 - i, exit);
  1777. ret = wc_Sha3_224_Final(&sha, hash);
  1778. if (ret != 0)
  1779. ERROR_OUT(-2030 - i, exit);
  1780. if (XMEMCMP(hash, test_sha[i].output, SHA3_224_DIGEST_SIZE) != 0)
  1781. ERROR_OUT(-2040 - i, exit);
  1782. if (XMEMCMP(hash, hashcopy, SHA3_224_DIGEST_SIZE) != 0)
  1783. ERROR_OUT(-2050 - i, exit);
  1784. }
  1785. /* BEGIN LARGE HASH TEST */ {
  1786. byte large_input[1024];
  1787. const char* large_digest =
  1788. "\x13\xe5\xd3\x98\x7b\x94\xda\x41\x12\xc7\x1e\x92\x3a\x19"
  1789. "\x21\x20\x86\x6f\x24\xbf\x0a\x31\xbc\xfd\xd6\x70\x36\xf3";
  1790. for (i = 0; i < (int)sizeof(large_input); i++) {
  1791. large_input[i] = (byte)(i & 0xFF);
  1792. }
  1793. times = 100;
  1794. for (i = 0; i < times; ++i) {
  1795. ret = wc_Sha3_224_Update(&sha, (byte*)large_input,
  1796. (word32)sizeof(large_input));
  1797. if (ret != 0)
  1798. ERROR_OUT(-2060, exit);
  1799. }
  1800. ret = wc_Sha3_224_Final(&sha, hash);
  1801. if (ret != 0)
  1802. ERROR_OUT(-2061, exit);
  1803. if (XMEMCMP(hash, large_digest, SHA3_224_DIGEST_SIZE) != 0)
  1804. ERROR_OUT(-2062, exit);
  1805. } /* END LARGE HASH TEST */
  1806. exit:
  1807. wc_Sha3_224_Free(&sha);
  1808. return ret;
  1809. }
  1810. #endif /* WOLFSSL_NOSHA3_224 */
  1811. #ifndef WOLFSSL_NOSHA3_256
  1812. static int sha3_256_test(void)
  1813. {
  1814. Sha3 sha;
  1815. byte hash[SHA3_256_DIGEST_SIZE];
  1816. byte hashcopy[SHA3_256_DIGEST_SIZE];
  1817. testVector a, b, c;
  1818. testVector test_sha[3];
  1819. int ret = 0;
  1820. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1821. a.input = "";
  1822. a.output = "\xa7\xff\xc6\xf8\xbf\x1e\xd7\x66\x51\xc1\x47\x56\xa0\x61\xd6"
  1823. "\x62\xf5\x80\xff\x4d\xe4\x3b\x49\xfa\x82\xd8\x0a\x4b\x80\xf8"
  1824. "\x43\x4a";
  1825. a.inLen = XSTRLEN(a.input);
  1826. a.outLen = SHA3_256_DIGEST_SIZE;
  1827. b.input = "abc";
  1828. b.output = "\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90"
  1829. "\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43"
  1830. "\x15\x32";
  1831. b.inLen = XSTRLEN(b.input);
  1832. b.outLen = SHA3_256_DIGEST_SIZE;
  1833. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1834. c.output = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8\x23\x5e"
  1835. "\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32\xdd\x97\x49\x6d"
  1836. "\x33\x76";
  1837. c.inLen = XSTRLEN(c.input);
  1838. c.outLen = SHA3_256_DIGEST_SIZE;
  1839. test_sha[0] = a;
  1840. test_sha[1] = b;
  1841. test_sha[2] = c;
  1842. ret = wc_InitSha3_256(&sha, HEAP_HINT, devId);
  1843. if (ret != 0)
  1844. return -2100;
  1845. for (i = 0; i < times; ++i) {
  1846. ret = wc_Sha3_256_Update(&sha, (byte*)test_sha[i].input,
  1847. (word32)test_sha[i].inLen);
  1848. if (ret != 0)
  1849. ERROR_OUT(-2110 - i, exit);
  1850. ret = wc_Sha3_256_GetHash(&sha, hashcopy);
  1851. if (ret != 0)
  1852. ERROR_OUT(-2120 - i, exit);
  1853. ret = wc_Sha3_256_Final(&sha, hash);
  1854. if (ret != 0)
  1855. ERROR_OUT(-2130 - i, exit);
  1856. if (XMEMCMP(hash, test_sha[i].output, SHA3_256_DIGEST_SIZE) != 0)
  1857. ERROR_OUT(-2140 - i, exit);
  1858. if (XMEMCMP(hash, hashcopy, SHA3_256_DIGEST_SIZE) != 0)
  1859. ERROR_OUT(-2150 - i, exit);
  1860. }
  1861. /* BEGIN LARGE HASH TEST */ {
  1862. byte large_input[1024];
  1863. const char* large_digest =
  1864. "\xdc\x90\xc0\xb1\x25\xdb\x2c\x34\x81\xa3\xff\xbc\x1e\x2e\x87\xeb"
  1865. "\x6d\x70\x85\x61\xe0\xe9\x63\x61\xff\xe5\x84\x4b\x1f\x68\x05\x15";
  1866. for (i = 0; i < (int)sizeof(large_input); i++) {
  1867. large_input[i] = (byte)(i & 0xFF);
  1868. }
  1869. times = 100;
  1870. for (i = 0; i < times; ++i) {
  1871. ret = wc_Sha3_256_Update(&sha, (byte*)large_input,
  1872. (word32)sizeof(large_input));
  1873. if (ret != 0)
  1874. ERROR_OUT(-2160, exit);
  1875. }
  1876. ret = wc_Sha3_256_Final(&sha, hash);
  1877. if (ret != 0)
  1878. ERROR_OUT(-2161, exit);
  1879. if (XMEMCMP(hash, large_digest, SHA3_256_DIGEST_SIZE) != 0)
  1880. ERROR_OUT(-2162, exit);
  1881. } /* END LARGE HASH TEST */
  1882. exit:
  1883. wc_Sha3_256_Free(&sha);
  1884. return ret;
  1885. }
  1886. #endif /* WOLFSSL_NOSHA3_256 */
  1887. #ifndef WOLFSSL_NOSHA3_384
  1888. static int sha3_384_test(void)
  1889. {
  1890. Sha3 sha;
  1891. byte hash[SHA3_384_DIGEST_SIZE];
  1892. byte hashcopy[SHA3_384_DIGEST_SIZE];
  1893. testVector a, b, c;
  1894. testVector test_sha[3];
  1895. int ret;
  1896. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1897. a.input = "";
  1898. a.output = "\x0c\x63\xa7\x5b\x84\x5e\x4f\x7d\x01\x10\x7d\x85\x2e\x4c\x24"
  1899. "\x85\xc5\x1a\x50\xaa\xaa\x94\xfc\x61\x99\x5e\x71\xbb\xee\x98"
  1900. "\x3a\x2a\xc3\x71\x38\x31\x26\x4a\xdb\x47\xfb\x6b\xd1\xe0\x58"
  1901. "\xd5\xf0\x04";
  1902. a.inLen = XSTRLEN(a.input);
  1903. a.outLen = SHA3_384_DIGEST_SIZE;
  1904. b.input = "abc";
  1905. b.output = "\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad"
  1906. "\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b"
  1907. "\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28"
  1908. "\x37\x6d\x25";
  1909. b.inLen = XSTRLEN(b.input);
  1910. b.outLen = SHA3_384_DIGEST_SIZE;
  1911. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1912. c.output = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7\x8a\x49"
  1913. "\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd\xbc\x32\xb9\xd4"
  1914. "\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe\xa1\x9e\xef\x51\xac\xd0"
  1915. "\x65\x7c\x22";
  1916. c.inLen = XSTRLEN(c.input);
  1917. c.outLen = SHA3_384_DIGEST_SIZE;
  1918. test_sha[0] = a;
  1919. test_sha[1] = b;
  1920. test_sha[2] = c;
  1921. ret = wc_InitSha3_384(&sha, HEAP_HINT, devId);
  1922. if (ret != 0)
  1923. return -2200;
  1924. for (i = 0; i < times; ++i) {
  1925. ret = wc_Sha3_384_Update(&sha, (byte*)test_sha[i].input,
  1926. (word32)test_sha[i].inLen);
  1927. if (ret != 0)
  1928. ERROR_OUT(-2210 - i, exit);
  1929. ret = wc_Sha3_384_GetHash(&sha, hashcopy);
  1930. if (ret != 0)
  1931. ERROR_OUT(-2220 - i, exit);
  1932. ret = wc_Sha3_384_Final(&sha, hash);
  1933. if (ret != 0)
  1934. ERROR_OUT(-2230 - i, exit);
  1935. if (XMEMCMP(hash, test_sha[i].output, SHA3_384_DIGEST_SIZE) != 0)
  1936. ERROR_OUT(-2240 - i, exit);
  1937. if (XMEMCMP(hash, hashcopy, SHA3_384_DIGEST_SIZE) != 0)
  1938. ERROR_OUT(-2250 - i, exit);
  1939. }
  1940. /* BEGIN LARGE HASH TEST */ {
  1941. byte large_input[1024];
  1942. const char* large_digest =
  1943. "\x30\x44\xec\x17\xef\x47\x9f\x55\x36\x11\xd6\x3f\x8a\x31\x5a\x71"
  1944. "\x8a\x71\xa7\x1d\x8e\x84\xe8\x6c\x24\x02\x2f\x7a\x08\x4e\xea\xd7"
  1945. "\x42\x36\x5d\xa8\xc2\xb7\x42\xad\xec\x19\xfb\xca\xc6\x64\xb3\xa4";
  1946. for (i = 0; i < (int)sizeof(large_input); i++) {
  1947. large_input[i] = (byte)(i & 0xFF);
  1948. }
  1949. times = 100;
  1950. for (i = 0; i < times; ++i) {
  1951. ret = wc_Sha3_384_Update(&sha, (byte*)large_input,
  1952. (word32)sizeof(large_input));
  1953. if (ret != 0)
  1954. ERROR_OUT(-2260, exit);
  1955. }
  1956. ret = wc_Sha3_384_Final(&sha, hash);
  1957. if (ret != 0)
  1958. ERROR_OUT(-2261, exit);
  1959. if (XMEMCMP(hash, large_digest, SHA3_384_DIGEST_SIZE) != 0)
  1960. ERROR_OUT(-2262, exit);
  1961. } /* END LARGE HASH TEST */
  1962. exit:
  1963. wc_Sha3_384_Free(&sha);
  1964. return ret;
  1965. }
  1966. #endif /* WOLFSSL_NOSHA3_384 */
  1967. #ifndef WOLFSSL_NOSHA3_512
  1968. static int sha3_512_test(void)
  1969. {
  1970. Sha3 sha;
  1971. byte hash[SHA3_512_DIGEST_SIZE];
  1972. byte hashcopy[SHA3_512_DIGEST_SIZE];
  1973. testVector a, b, c;
  1974. testVector test_sha[3];
  1975. int ret;
  1976. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  1977. a.input = "";
  1978. a.output = "\xa6\x9f\x73\xcc\xa2\x3a\x9a\xc5\xc8\xb5\x67\xdc\x18\x5a\x75"
  1979. "\x6e\x97\xc9\x82\x16\x4f\xe2\x58\x59\xe0\xd1\xdc\xc1\x47\x5c"
  1980. "\x80\xa6\x15\xb2\x12\x3a\xf1\xf5\xf9\x4c\x11\xe3\xe9\x40\x2c"
  1981. "\x3a\xc5\x58\xf5\x00\x19\x9d\x95\xb6\xd3\xe3\x01\x75\x85\x86"
  1982. "\x28\x1d\xcd\x26";
  1983. a.inLen = XSTRLEN(a.input);
  1984. a.outLen = SHA3_512_DIGEST_SIZE;
  1985. b.input = "abc";
  1986. b.output = "\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09"
  1987. "\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2"
  1988. "\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47"
  1989. "\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27"
  1990. "\x4e\xec\x53\xf0";
  1991. b.inLen = XSTRLEN(b.input);
  1992. b.outLen = SHA3_512_DIGEST_SIZE;
  1993. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  1994. c.output = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10\xfc\xa8"
  1995. "\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7\xec\x2f\x1e\x91"
  1996. "\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53\x02\xba\x1b\x0d\x8d\xc7"
  1997. "\x8c\x08\x63\x46\xb5\x33\xb4\x9c\x03\x0d\x99\xa2\x7d\xaf\x11"
  1998. "\x39\xd6\xe7\x5e";
  1999. c.inLen = XSTRLEN(c.input);
  2000. c.outLen = SHA3_512_DIGEST_SIZE;
  2001. test_sha[0] = a;
  2002. test_sha[1] = b;
  2003. test_sha[2] = c;
  2004. ret = wc_InitSha3_512(&sha, HEAP_HINT, devId);
  2005. if (ret != 0)
  2006. return -2300;
  2007. for (i = 0; i < times; ++i) {
  2008. ret = wc_Sha3_512_Update(&sha, (byte*)test_sha[i].input,
  2009. (word32)test_sha[i].inLen);
  2010. if (ret != 0)
  2011. ERROR_OUT(-2310 - i, exit);
  2012. ret = wc_Sha3_512_GetHash(&sha, hashcopy);
  2013. if (ret != 0)
  2014. ERROR_OUT(-2320 - i, exit);
  2015. ret = wc_Sha3_512_Final(&sha, hash);
  2016. if (ret != 0)
  2017. ERROR_OUT(-2330 - i, exit);
  2018. if (XMEMCMP(hash, test_sha[i].output, SHA3_512_DIGEST_SIZE) != 0)
  2019. ERROR_OUT(-2340 - i, exit);
  2020. if (XMEMCMP(hash, hashcopy, SHA3_512_DIGEST_SIZE) != 0)
  2021. ERROR_OUT(-2350 - i, exit);
  2022. }
  2023. /* BEGIN LARGE HASH TEST */ {
  2024. byte large_input[1024];
  2025. const char* large_digest =
  2026. "\x9c\x13\x26\xb6\x26\xb2\x94\x31\xbc\xf4\x34\xe9\x6f\xf2\xd6\x29"
  2027. "\x9a\xd0\x9b\x32\x63\x2f\x18\xa7\x5f\x23\xc9\x60\xc2\x32\x0c\xbc"
  2028. "\x57\x77\x33\xf1\x83\x81\x8a\xd3\x15\x7c\x93\xdc\x80\x9f\xed\x61"
  2029. "\x41\xa7\x5b\xfd\x32\x0e\x38\x15\xb0\x46\x3b\x7a\x4f\xfd\x44\x88";
  2030. for (i = 0; i < (int)sizeof(large_input); i++) {
  2031. large_input[i] = (byte)(i & 0xFF);
  2032. }
  2033. times = 100;
  2034. for (i = 0; i < times; ++i) {
  2035. ret = wc_Sha3_512_Update(&sha, (byte*)large_input,
  2036. (word32)sizeof(large_input));
  2037. if (ret != 0)
  2038. ERROR_OUT(-2360, exit);
  2039. }
  2040. ret = wc_Sha3_512_Final(&sha, hash);
  2041. if (ret != 0)
  2042. ERROR_OUT(-2361, exit);
  2043. if (XMEMCMP(hash, large_digest, SHA3_512_DIGEST_SIZE) != 0)
  2044. ERROR_OUT(-2362, exit);
  2045. } /* END LARGE HASH TEST */
  2046. exit:
  2047. wc_Sha3_512_Free(&sha);
  2048. return ret;
  2049. }
  2050. #endif /* WOLFSSL_NOSHA3_512 */
  2051. int sha3_test(void)
  2052. {
  2053. int ret;
  2054. #ifndef WOLFSSL_NOSHA3_224
  2055. if ((ret = sha3_224_test()) != 0)
  2056. return ret;
  2057. #endif
  2058. #ifndef WOLFSSL_NOSHA3_256
  2059. if ((ret = sha3_256_test()) != 0)
  2060. return ret;
  2061. #endif
  2062. #ifndef WOLFSSL_NOSHA3_384
  2063. if ((ret = sha3_384_test()) != 0)
  2064. return ret;
  2065. #endif
  2066. #ifndef WOLFSSL_NOSHA3_512
  2067. if ((ret = sha3_512_test()) != 0)
  2068. return ret;
  2069. #endif
  2070. return 0;
  2071. }
  2072. #endif
  2073. int hash_test(void)
  2074. {
  2075. wc_HashAlg hash;
  2076. int ret, exp_ret;
  2077. int i, j;
  2078. byte data[] = "0123456789abcdef0123456789abcdef012345";
  2079. byte out[MAX_DIGEST_SIZE];
  2080. enum wc_HashType typesGood[] = { WC_HASH_TYPE_MD5, WC_HASH_TYPE_SHA,
  2081. WC_HASH_TYPE_SHA224, WC_HASH_TYPE_SHA256,
  2082. WC_HASH_TYPE_SHA384, WC_HASH_TYPE_SHA512 };
  2083. enum wc_HashType typesNoImpl[] = {
  2084. #ifdef NO_MD5
  2085. WC_HASH_TYPE_MD5,
  2086. #endif
  2087. #ifdef NO_SHA
  2088. WC_HASH_TYPE_SHA,
  2089. #endif
  2090. #ifndef WOLFSSL_SHA224
  2091. WC_HASH_TYPE_SHA224,
  2092. #endif
  2093. #ifdef NO_SHA256
  2094. WC_HASH_TYPE_SHA256,
  2095. #endif
  2096. #ifndef WOLFSSL_SHA384
  2097. WC_HASH_TYPE_SHA384,
  2098. #endif
  2099. #ifndef WOLFSSL_SHA512
  2100. WC_HASH_TYPE_SHA512,
  2101. #endif
  2102. WC_HASH_TYPE_NONE
  2103. };
  2104. enum wc_HashType typesBad[] = { WC_HASH_TYPE_NONE, WC_HASH_TYPE_MD5_SHA,
  2105. WC_HASH_TYPE_MD2, WC_HASH_TYPE_MD4 };
  2106. /* Parameter Validation testing. */
  2107. ret = wc_HashInit(NULL, WC_HASH_TYPE_SHA256);
  2108. if (ret != BAD_FUNC_ARG)
  2109. return -2400;
  2110. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  2111. if (ret != BAD_FUNC_ARG)
  2112. return -2401;
  2113. ret = wc_HashUpdate(&hash, WC_HASH_TYPE_SHA256, NULL, sizeof(data));
  2114. if (ret != BAD_FUNC_ARG)
  2115. return -2402;
  2116. ret = wc_HashUpdate(NULL, WC_HASH_TYPE_SHA256, data, sizeof(data));
  2117. if (ret != BAD_FUNC_ARG)
  2118. return -2403;
  2119. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, NULL);
  2120. if (ret != BAD_FUNC_ARG)
  2121. return -2404;
  2122. ret = wc_HashFinal(&hash, WC_HASH_TYPE_SHA256, NULL);
  2123. if (ret != BAD_FUNC_ARG)
  2124. return -2405;
  2125. ret = wc_HashFinal(NULL, WC_HASH_TYPE_SHA256, out);
  2126. if (ret != BAD_FUNC_ARG)
  2127. return -2406;
  2128. /* Try invalid hash algorithms. */
  2129. for (i = 0; i < (int)(sizeof(typesBad)/sizeof(*typesBad)); i++) {
  2130. ret = wc_HashInit(&hash, typesBad[i]);
  2131. if (ret != BAD_FUNC_ARG)
  2132. return -2407 - i;
  2133. ret = wc_HashUpdate(&hash, typesBad[i], data, sizeof(data));
  2134. if (ret != BAD_FUNC_ARG)
  2135. return -2417 - i;
  2136. ret = wc_HashFinal(&hash, typesBad[i], out);
  2137. if (ret != BAD_FUNC_ARG)
  2138. return -2427 - i;
  2139. }
  2140. /* Try valid hash algorithms. */
  2141. for (i = 0, j = 0; i < (int)(sizeof(typesGood)/sizeof(*typesGood)); i++) {
  2142. exp_ret = 0;
  2143. if (typesGood[i] == typesNoImpl[j]) {
  2144. /* Recognized but no implementation compiled in. */
  2145. exp_ret = HASH_TYPE_E;
  2146. j++;
  2147. }
  2148. ret = wc_HashInit(&hash, typesGood[i]);
  2149. if (ret != exp_ret)
  2150. return -2437 - i;
  2151. ret = wc_HashUpdate(&hash, typesGood[i], data, sizeof(data));
  2152. if (ret != exp_ret)
  2153. return -2447 - i;
  2154. ret = wc_HashFinal(&hash, typesGood[i], out);
  2155. if (ret != exp_ret)
  2156. return -2457 - i;
  2157. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  2158. ret = wc_HashGetOID(typesGood[i]);
  2159. if (ret == BAD_FUNC_ARG ||
  2160. (exp_ret == 0 && ret == HASH_TYPE_E) ||
  2161. (exp_ret != 0 && ret != HASH_TYPE_E)) {
  2162. return -2467 - i;
  2163. }
  2164. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  2165. }
  2166. #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
  2167. ret = wc_HashGetOID(WC_HASH_TYPE_MD2);
  2168. #ifdef WOLFSSL_MD2
  2169. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2170. return -2477;
  2171. #else
  2172. if (ret != HASH_TYPE_E)
  2173. return -2478;
  2174. #endif
  2175. ret = wc_HashGetOID(WC_HASH_TYPE_MD5_SHA);
  2176. #ifndef NO_MD5
  2177. if (ret == HASH_TYPE_E || ret == BAD_FUNC_ARG)
  2178. return -2479;
  2179. #else
  2180. if (ret != HASH_TYPE_E)
  2181. return -2480;
  2182. #endif
  2183. ret = wc_HashGetOID(WC_HASH_TYPE_MD4);
  2184. if (ret != BAD_FUNC_ARG)
  2185. return -2481;
  2186. ret = wc_HashGetOID(WC_HASH_TYPE_NONE);
  2187. if (ret != BAD_FUNC_ARG)
  2188. return -2482;
  2189. #endif /* !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC) */
  2190. #ifndef NO_ASN
  2191. #ifdef WOLFSSL_MD2
  2192. ret = wc_GetCTC_HashOID(MD2);
  2193. if (ret == 0)
  2194. return -2483;
  2195. #endif
  2196. #ifndef NO_MD5
  2197. ret = wc_GetCTC_HashOID(WC_MD5);
  2198. if (ret == 0)
  2199. return -2484;
  2200. #endif
  2201. #ifndef NO_SHA
  2202. ret = wc_GetCTC_HashOID(WC_SHA);
  2203. if (ret == 0)
  2204. return -2485;
  2205. #endif
  2206. #ifdef WOLFSSL_SHA224
  2207. ret = wc_GetCTC_HashOID(WC_SHA224);
  2208. if (ret == 0)
  2209. return -2486;
  2210. #endif
  2211. #ifndef NO_SHA256
  2212. ret = wc_GetCTC_HashOID(WC_SHA256);
  2213. if (ret == 0)
  2214. return -2487;
  2215. #endif
  2216. #ifdef WOLFSSL_SHA384
  2217. ret = wc_GetCTC_HashOID(WC_SHA384);
  2218. if (ret == 0)
  2219. return -2488;
  2220. #endif
  2221. #ifdef WOLFSSL_SHA512
  2222. ret = wc_GetCTC_HashOID(WC_SHA512);
  2223. if (ret == 0)
  2224. return -2489;
  2225. #endif
  2226. ret = wc_GetCTC_HashOID(-1);
  2227. if (ret != 0)
  2228. return -2490;
  2229. #endif
  2230. return 0;
  2231. }
  2232. #if !defined(NO_HMAC) && !defined(NO_MD5)
  2233. int hmac_md5_test(void)
  2234. {
  2235. Hmac hmac;
  2236. byte hash[WC_MD5_DIGEST_SIZE];
  2237. const char* keys[]=
  2238. {
  2239. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  2240. "Jefe",
  2241. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  2242. };
  2243. testVector a, b, c;
  2244. testVector test_hmac[3];
  2245. int ret;
  2246. int times = sizeof(test_hmac) / sizeof(testVector), i;
  2247. a.input = "Hi There";
  2248. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  2249. "\x9d";
  2250. a.inLen = XSTRLEN(a.input);
  2251. a.outLen = WC_MD5_DIGEST_SIZE;
  2252. b.input = "what do ya want for nothing?";
  2253. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  2254. "\x38";
  2255. b.inLen = XSTRLEN(b.input);
  2256. b.outLen = WC_MD5_DIGEST_SIZE;
  2257. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2258. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2259. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2260. "\xDD\xDD\xDD\xDD\xDD\xDD";
  2261. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  2262. "\xf6";
  2263. c.inLen = XSTRLEN(c.input);
  2264. c.outLen = WC_MD5_DIGEST_SIZE;
  2265. test_hmac[0] = a;
  2266. test_hmac[1] = b;
  2267. test_hmac[2] = c;
  2268. for (i = 0; i < times; ++i) {
  2269. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  2270. if (i == 1) {
  2271. continue; /* cavium can't handle short keys, fips not allowed */
  2272. }
  2273. #endif
  2274. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0) {
  2275. return -2500;
  2276. }
  2277. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[i],
  2278. (word32)XSTRLEN(keys[i]));
  2279. if (ret != 0)
  2280. return -2501;
  2281. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  2282. (word32)test_hmac[i].inLen);
  2283. if (ret != 0)
  2284. return -2502;
  2285. ret = wc_HmacFinal(&hmac, hash);
  2286. if (ret != 0)
  2287. return -2503;
  2288. if (XMEMCMP(hash, test_hmac[i].output, WC_MD5_DIGEST_SIZE) != 0)
  2289. return -2504 - i;
  2290. wc_HmacFree(&hmac);
  2291. }
  2292. #ifndef HAVE_FIPS
  2293. if (wc_HmacSizeByType(WC_MD5) != WC_MD5_DIGEST_SIZE)
  2294. return -2514;
  2295. #endif
  2296. return 0;
  2297. }
  2298. #endif /* NO_HMAC && NO_MD5 */
  2299. #if !defined(NO_HMAC) && !defined(NO_SHA)
  2300. int hmac_sha_test(void)
  2301. {
  2302. Hmac hmac;
  2303. byte hash[WC_SHA_DIGEST_SIZE];
  2304. const char* keys[]=
  2305. {
  2306. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  2307. "\x0b\x0b\x0b",
  2308. "Jefe",
  2309. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  2310. "\xAA\xAA\xAA"
  2311. };
  2312. testVector a, b, c;
  2313. testVector test_hmac[3];
  2314. int ret;
  2315. int times = sizeof(test_hmac) / sizeof(testVector), i;
  2316. a.input = "Hi There";
  2317. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  2318. "\x8e\xf1\x46\xbe\x00";
  2319. a.inLen = XSTRLEN(a.input);
  2320. a.outLen = WC_SHA_DIGEST_SIZE;
  2321. b.input = "what do ya want for nothing?";
  2322. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  2323. "\x9c\x25\x9a\x7c\x79";
  2324. b.inLen = XSTRLEN(b.input);
  2325. b.outLen = WC_SHA_DIGEST_SIZE;
  2326. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2327. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2328. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2329. "\xDD\xDD\xDD\xDD\xDD\xDD";
  2330. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  2331. "\x4f\x63\xf1\x75\xd3";
  2332. c.inLen = XSTRLEN(c.input);
  2333. c.outLen = WC_SHA_DIGEST_SIZE;
  2334. test_hmac[0] = a;
  2335. test_hmac[1] = b;
  2336. test_hmac[2] = c;
  2337. for (i = 0; i < times; ++i) {
  2338. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  2339. if (i == 1)
  2340. continue; /* cavium can't handle short keys, fips not allowed */
  2341. #endif
  2342. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  2343. return -20010;
  2344. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[i],
  2345. (word32)XSTRLEN(keys[i]));
  2346. if (ret != 0)
  2347. return -2601;
  2348. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  2349. (word32)test_hmac[i].inLen);
  2350. if (ret != 0)
  2351. return -2602;
  2352. ret = wc_HmacFinal(&hmac, hash);
  2353. if (ret != 0)
  2354. return -2603;
  2355. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA_DIGEST_SIZE) != 0)
  2356. return -2604 - i;
  2357. wc_HmacFree(&hmac);
  2358. }
  2359. #ifndef HAVE_FIPS
  2360. if (wc_HmacSizeByType(WC_SHA) != WC_SHA_DIGEST_SIZE)
  2361. return -2614;
  2362. #endif
  2363. return 0;
  2364. }
  2365. #endif
  2366. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  2367. int hmac_sha224_test(void)
  2368. {
  2369. Hmac hmac;
  2370. byte hash[WC_SHA224_DIGEST_SIZE];
  2371. const char* keys[]=
  2372. {
  2373. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  2374. "\x0b\x0b\x0b",
  2375. "Jefe",
  2376. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  2377. "\xAA\xAA\xAA"
  2378. };
  2379. testVector a, b, c;
  2380. testVector test_hmac[3];
  2381. int ret;
  2382. int times = sizeof(test_hmac) / sizeof(testVector), i;
  2383. a.input = "Hi There";
  2384. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  2385. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  2386. a.inLen = XSTRLEN(a.input);
  2387. a.outLen = WC_SHA224_DIGEST_SIZE;
  2388. b.input = "what do ya want for nothing?";
  2389. b.output = "\xa3\x0e\x01\x09\x8b\xc6\xdb\xbf\x45\x69\x0f\x3a\x7e\x9e\x6d"
  2390. "\x0f\x8b\xbe\xa2\xa3\x9e\x61\x48\x00\x8f\xd0\x5e\x44";
  2391. b.inLen = XSTRLEN(b.input);
  2392. b.outLen = WC_SHA224_DIGEST_SIZE;
  2393. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2394. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2395. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2396. "\xDD\xDD\xDD\xDD\xDD\xDD";
  2397. c.output = "\x7f\xb3\xcb\x35\x88\xc6\xc1\xf6\xff\xa9\x69\x4d\x7d\x6a\xd2"
  2398. "\x64\x93\x65\xb0\xc1\xf6\x5d\x69\xd1\xec\x83\x33\xea";
  2399. c.inLen = XSTRLEN(c.input);
  2400. c.outLen = WC_SHA224_DIGEST_SIZE;
  2401. test_hmac[0] = a;
  2402. test_hmac[1] = b;
  2403. test_hmac[2] = c;
  2404. for (i = 0; i < times; ++i) {
  2405. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  2406. if (i == 1)
  2407. continue; /* cavium can't handle short keys, fips not allowed */
  2408. #endif
  2409. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  2410. return -2700;
  2411. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[i],
  2412. (word32)XSTRLEN(keys[i]));
  2413. if (ret != 0)
  2414. return -2701;
  2415. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  2416. (word32)test_hmac[i].inLen);
  2417. if (ret != 0)
  2418. return -2702;
  2419. ret = wc_HmacFinal(&hmac, hash);
  2420. if (ret != 0)
  2421. return -2703;
  2422. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA224_DIGEST_SIZE) != 0)
  2423. return -2704 - i;
  2424. wc_HmacFree(&hmac);
  2425. }
  2426. #ifndef HAVE_FIPS
  2427. if (wc_HmacSizeByType(WC_SHA224) != WC_SHA224_DIGEST_SIZE)
  2428. return -2714;
  2429. #endif
  2430. return 0;
  2431. }
  2432. #endif
  2433. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  2434. int hmac_sha256_test(void)
  2435. {
  2436. Hmac hmac;
  2437. byte hash[WC_SHA256_DIGEST_SIZE];
  2438. const char* keys[]=
  2439. {
  2440. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  2441. "\x0b\x0b\x0b",
  2442. "Jefe",
  2443. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  2444. "\xAA\xAA\xAA"
  2445. };
  2446. testVector a, b, c;
  2447. testVector test_hmac[3];
  2448. int ret;
  2449. int times = sizeof(test_hmac) / sizeof(testVector), i;
  2450. a.input = "Hi There";
  2451. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  2452. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  2453. "\xcf\xf7";
  2454. a.inLen = XSTRLEN(a.input);
  2455. a.outLen = WC_SHA256_DIGEST_SIZE;
  2456. b.input = "what do ya want for nothing?";
  2457. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  2458. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  2459. "\x38\x43";
  2460. b.inLen = XSTRLEN(b.input);
  2461. b.outLen = WC_SHA256_DIGEST_SIZE;
  2462. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2463. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2464. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2465. "\xDD\xDD\xDD\xDD\xDD\xDD";
  2466. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  2467. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  2468. "\x65\xfe";
  2469. c.inLen = XSTRLEN(c.input);
  2470. c.outLen = WC_SHA256_DIGEST_SIZE;
  2471. test_hmac[0] = a;
  2472. test_hmac[1] = b;
  2473. test_hmac[2] = c;
  2474. for (i = 0; i < times; ++i) {
  2475. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  2476. if (i == 1)
  2477. continue; /* cavium can't handle short keys, fips not allowed */
  2478. #endif
  2479. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  2480. return -2800;
  2481. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[i],
  2482. (word32)XSTRLEN(keys[i]));
  2483. if (ret != 0)
  2484. return -2801;
  2485. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  2486. (word32)test_hmac[i].inLen);
  2487. if (ret != 0)
  2488. return -2802;
  2489. ret = wc_HmacFinal(&hmac, hash);
  2490. if (ret != 0)
  2491. return -2803;
  2492. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA256_DIGEST_SIZE) != 0)
  2493. return -2804 - i;
  2494. wc_HmacFree(&hmac);
  2495. }
  2496. #ifndef HAVE_FIPS
  2497. if (wc_HmacSizeByType(WC_SHA256) != WC_SHA256_DIGEST_SIZE)
  2498. return -2814;
  2499. if (wc_HmacSizeByType(20) != BAD_FUNC_ARG)
  2500. return -2815;
  2501. #endif
  2502. if (wolfSSL_GetHmacMaxSize() != MAX_DIGEST_SIZE)
  2503. return -2816;
  2504. return 0;
  2505. }
  2506. #endif
  2507. #if !defined(NO_HMAC) && defined(HAVE_BLAKE2)
  2508. int hmac_blake2b_test(void)
  2509. {
  2510. Hmac hmac;
  2511. byte hash[BLAKE2B_256];
  2512. const char* keys[]=
  2513. {
  2514. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  2515. "\x0b\x0b\x0b",
  2516. "Jefe",
  2517. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  2518. "\xAA\xAA\xAA"
  2519. };
  2520. testVector a, b, c;
  2521. testVector test_hmac[3];
  2522. int ret;
  2523. int times = sizeof(test_hmac) / sizeof(testVector), i;
  2524. a.input = "Hi There";
  2525. a.output = "\x72\x93\x0d\xdd\xf5\xf7\xe1\x78\x38\x07\x44\x18\x0b\x3f\x51"
  2526. "\x37\x25\xb5\x82\xc2\x08\x83\x2f\x1c\x99\xfd\x03\xa0\x16\x75"
  2527. "\xac\xfd";
  2528. a.inLen = XSTRLEN(a.input);
  2529. a.outLen = BLAKE2B_256;
  2530. b.input = "what do ya want for nothing?";
  2531. b.output = "\x3d\x20\x50\x71\x05\xc0\x8c\x0c\x38\x44\x1e\xf7\xf9\xd1\x67"
  2532. "\x21\xff\x64\xf5\x94\x00\xcf\xf9\x75\x41\xda\x88\x61\x9d\x7c"
  2533. "\xda\x2b";
  2534. b.inLen = XSTRLEN(b.input);
  2535. b.outLen = BLAKE2B_256;
  2536. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2537. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2538. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2539. "\xDD\xDD\xDD\xDD\xDD\xDD";
  2540. c.output = "\xda\xfe\x2a\x24\xfc\xe7\xea\x36\x34\xbe\x41\x92\xc7\x11\xa7"
  2541. "\x00\xae\x53\x9c\x11\x9c\x80\x74\x55\x22\x25\x4a\xb9\x55\xd3"
  2542. "\x0f\x87";
  2543. c.inLen = XSTRLEN(c.input);
  2544. c.outLen = BLAKE2B_256;
  2545. test_hmac[0] = a;
  2546. test_hmac[1] = b;
  2547. test_hmac[2] = c;
  2548. for (i = 0; i < times; ++i) {
  2549. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  2550. if (i == 1)
  2551. continue; /* cavium can't handle short keys, fips not allowed */
  2552. #endif
  2553. #if defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V)
  2554. /* Blake2 only supported on Cavium Nitrox III */
  2555. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  2556. return -2900;
  2557. #endif
  2558. ret = wc_HmacSetKey(&hmac, BLAKE2B_ID, (byte*)keys[i],
  2559. (word32)XSTRLEN(keys[i]));
  2560. if (ret != 0)
  2561. return -2901;
  2562. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  2563. (word32)test_hmac[i].inLen);
  2564. if (ret != 0)
  2565. return -2902;
  2566. ret = wc_HmacFinal(&hmac, hash);
  2567. if (ret != 0)
  2568. return -2903;
  2569. if (XMEMCMP(hash, test_hmac[i].output, BLAKE2B_256) != 0)
  2570. return -2904 - i;
  2571. wc_HmacFree(&hmac);
  2572. }
  2573. #ifndef HAVE_FIPS
  2574. if (wc_HmacSizeByType(BLAKE2B_ID) != BLAKE2B_OUTBYTES)
  2575. return -2914;
  2576. #endif
  2577. return 0;
  2578. }
  2579. #endif
  2580. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  2581. int hmac_sha384_test(void)
  2582. {
  2583. Hmac hmac;
  2584. byte hash[WC_SHA384_DIGEST_SIZE];
  2585. const char* keys[]=
  2586. {
  2587. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  2588. "\x0b\x0b\x0b",
  2589. "Jefe",
  2590. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  2591. "\xAA\xAA\xAA"
  2592. };
  2593. testVector a, b, c;
  2594. testVector test_hmac[3];
  2595. int ret;
  2596. int times = sizeof(test_hmac) / sizeof(testVector), i;
  2597. a.input = "Hi There";
  2598. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  2599. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  2600. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  2601. "\xfa\x9c\xb6";
  2602. a.inLen = XSTRLEN(a.input);
  2603. a.outLen = WC_SHA384_DIGEST_SIZE;
  2604. b.input = "what do ya want for nothing?";
  2605. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  2606. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  2607. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  2608. "\xb2\x16\x49";
  2609. b.inLen = XSTRLEN(b.input);
  2610. b.outLen = WC_SHA384_DIGEST_SIZE;
  2611. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2612. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2613. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2614. "\xDD\xDD\xDD\xDD\xDD\xDD";
  2615. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  2616. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  2617. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  2618. "\xa3\x4f\x27";
  2619. c.inLen = XSTRLEN(c.input);
  2620. c.outLen = WC_SHA384_DIGEST_SIZE;
  2621. test_hmac[0] = a;
  2622. test_hmac[1] = b;
  2623. test_hmac[2] = c;
  2624. for (i = 0; i < times; ++i) {
  2625. #if defined(HAVE_FIPS)
  2626. if (i == 1)
  2627. continue; /* fips not allowed */
  2628. #endif
  2629. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  2630. return -3000;
  2631. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[i],
  2632. (word32)XSTRLEN(keys[i]));
  2633. if (ret != 0)
  2634. return -3001;
  2635. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  2636. (word32)test_hmac[i].inLen);
  2637. if (ret != 0)
  2638. return -3002;
  2639. ret = wc_HmacFinal(&hmac, hash);
  2640. if (ret != 0)
  2641. return -3003;
  2642. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA384_DIGEST_SIZE) != 0)
  2643. return -3004 - i;
  2644. wc_HmacFree(&hmac);
  2645. }
  2646. #ifndef HAVE_FIPS
  2647. if (wc_HmacSizeByType(WC_SHA384) != WC_SHA384_DIGEST_SIZE)
  2648. return -3013;
  2649. #endif
  2650. return 0;
  2651. }
  2652. #endif
  2653. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  2654. int hmac_sha512_test(void)
  2655. {
  2656. Hmac hmac;
  2657. byte hash[WC_SHA512_DIGEST_SIZE];
  2658. const char* keys[]=
  2659. {
  2660. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  2661. "\x0b\x0b\x0b",
  2662. "Jefe",
  2663. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  2664. "\xAA\xAA\xAA"
  2665. };
  2666. testVector a, b, c;
  2667. testVector test_hmac[3];
  2668. int ret;
  2669. int times = sizeof(test_hmac) / sizeof(testVector), i;
  2670. a.input = "Hi There";
  2671. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  2672. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  2673. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  2674. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  2675. "\x3a\x12\x68\x54";
  2676. a.inLen = XSTRLEN(a.input);
  2677. a.outLen = WC_SHA512_DIGEST_SIZE;
  2678. b.input = "what do ya want for nothing?";
  2679. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  2680. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  2681. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  2682. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  2683. "\x38\xbc\xe7\x37";
  2684. b.inLen = XSTRLEN(b.input);
  2685. b.outLen = WC_SHA512_DIGEST_SIZE;
  2686. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2687. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2688. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  2689. "\xDD\xDD\xDD\xDD\xDD\xDD";
  2690. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  2691. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  2692. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  2693. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  2694. "\xe1\x32\x92\xfb";
  2695. c.inLen = XSTRLEN(c.input);
  2696. c.outLen = WC_SHA512_DIGEST_SIZE;
  2697. test_hmac[0] = a;
  2698. test_hmac[1] = b;
  2699. test_hmac[2] = c;
  2700. for (i = 0; i < times; ++i) {
  2701. #if defined(HAVE_FIPS)
  2702. if (i == 1)
  2703. continue; /* fips not allowed */
  2704. #endif
  2705. if (wc_HmacInit(&hmac, HEAP_HINT, devId) != 0)
  2706. return -3100;
  2707. ret = wc_HmacSetKey(&hmac, WC_SHA512, (byte*)keys[i],
  2708. (word32)XSTRLEN(keys[i]));
  2709. if (ret != 0)
  2710. return -3101;
  2711. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  2712. (word32)test_hmac[i].inLen);
  2713. if (ret != 0)
  2714. return -3102;
  2715. ret = wc_HmacFinal(&hmac, hash);
  2716. if (ret != 0)
  2717. return -3103;
  2718. if (XMEMCMP(hash, test_hmac[i].output, WC_SHA512_DIGEST_SIZE) != 0)
  2719. return -3104 - i;
  2720. wc_HmacFree(&hmac);
  2721. }
  2722. #ifndef HAVE_FIPS
  2723. if (wc_HmacSizeByType(WC_SHA512) != WC_SHA512_DIGEST_SIZE)
  2724. return -3113;
  2725. #endif
  2726. return 0;
  2727. }
  2728. #endif
  2729. #ifndef NO_RC4
  2730. int arc4_test(void)
  2731. {
  2732. byte cipher[16];
  2733. byte plain[16];
  2734. const char* keys[] =
  2735. {
  2736. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  2737. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  2738. "\x00\x00\x00\x00\x00\x00\x00\x00",
  2739. "\xef\x01\x23\x45"
  2740. };
  2741. testVector a, b, c, d;
  2742. testVector test_arc4[4];
  2743. int times = sizeof(test_arc4) / sizeof(testVector), i;
  2744. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  2745. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  2746. a.inLen = 8;
  2747. a.outLen = 8;
  2748. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  2749. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  2750. b.inLen = 8;
  2751. b.outLen = 8;
  2752. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  2753. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  2754. c.inLen = 8;
  2755. c.outLen = 8;
  2756. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  2757. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  2758. d.inLen = 10;
  2759. d.outLen = 10;
  2760. test_arc4[0] = a;
  2761. test_arc4[1] = b;
  2762. test_arc4[2] = c;
  2763. test_arc4[3] = d;
  2764. for (i = 0; i < times; ++i) {
  2765. Arc4 enc;
  2766. Arc4 dec;
  2767. int keylen = 8; /* XSTRLEN with key 0x00 not good */
  2768. if (i == 3)
  2769. keylen = 4;
  2770. if (wc_Arc4Init(&enc, HEAP_HINT, devId) != 0)
  2771. return -3200;
  2772. if (wc_Arc4Init(&dec, HEAP_HINT, devId) != 0)
  2773. return -3201;
  2774. wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  2775. wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  2776. wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  2777. (word32)test_arc4[i].outLen);
  2778. wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  2779. if (XMEMCMP(plain, test_arc4[i].input, test_arc4[i].outLen))
  2780. return -3202 - i;
  2781. if (XMEMCMP(cipher, test_arc4[i].output, test_arc4[i].outLen))
  2782. return -3212 - i;
  2783. wc_Arc4Free(&enc);
  2784. wc_Arc4Free(&dec);
  2785. }
  2786. return 0;
  2787. }
  2788. #endif
  2789. int hc128_test(void)
  2790. {
  2791. #ifdef HAVE_HC128
  2792. byte cipher[16];
  2793. byte plain[16];
  2794. const char* keys[] =
  2795. {
  2796. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  2797. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  2798. "\x00\x53\xA6\xF9\x4C\x9F\xF2\x45\x98\xEB\x3E\x91\xE4\x37\x8A\xDD",
  2799. "\x0F\x62\xB5\x08\x5B\xAE\x01\x54\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC"
  2800. };
  2801. const char* ivs[] =
  2802. {
  2803. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  2804. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  2805. "\x0D\x74\xDB\x42\xA9\x10\x77\xDE\x45\xAC\x13\x7A\xE1\x48\xAF\x16",
  2806. "\x28\x8F\xF6\x5D\xC4\x2B\x92\xF9\x60\xC7\x2E\x95\xFC\x63\xCA\x31"
  2807. };
  2808. testVector a, b, c, d;
  2809. testVector test_hc128[4];
  2810. int times = sizeof(test_hc128) / sizeof(testVector), i;
  2811. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  2812. a.output = "\x37\x86\x02\xB9\x8F\x32\xA7\x48";
  2813. a.inLen = 8;
  2814. a.outLen = 8;
  2815. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  2816. b.output = "\x33\x7F\x86\x11\xC6\xED\x61\x5F";
  2817. b.inLen = 8;
  2818. b.outLen = 8;
  2819. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  2820. c.output = "\x2E\x1E\xD1\x2A\x85\x51\xC0\x5A";
  2821. c.inLen = 8;
  2822. c.outLen = 8;
  2823. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  2824. d.output = "\x1C\xD8\xAE\xDD\xFE\x52\xE2\x17\xE8\x35\xD0\xB7\xE8\x4E\x29";
  2825. d.inLen = 15;
  2826. d.outLen = 15;
  2827. test_hc128[0] = a;
  2828. test_hc128[1] = b;
  2829. test_hc128[2] = c;
  2830. test_hc128[3] = d;
  2831. for (i = 0; i < times; ++i) {
  2832. HC128 enc;
  2833. HC128 dec;
  2834. /* align keys/ivs in plain/cipher buffers */
  2835. XMEMCPY(plain, keys[i], 16);
  2836. XMEMCPY(cipher, ivs[i], 16);
  2837. wc_Hc128_SetKey(&enc, plain, cipher);
  2838. wc_Hc128_SetKey(&dec, plain, cipher);
  2839. /* align input */
  2840. XMEMCPY(plain, test_hc128[i].input, test_hc128[i].outLen);
  2841. if (wc_Hc128_Process(&enc, cipher, plain,
  2842. (word32)test_hc128[i].outLen) != 0) {
  2843. return -3300;
  2844. }
  2845. if (wc_Hc128_Process(&dec, plain, cipher,
  2846. (word32)test_hc128[i].outLen) != 0) {
  2847. return -3301;
  2848. }
  2849. if (XMEMCMP(plain, test_hc128[i].input, test_hc128[i].outLen))
  2850. return -3302 - i;
  2851. if (XMEMCMP(cipher, test_hc128[i].output, test_hc128[i].outLen))
  2852. return -3312 - i;
  2853. }
  2854. #endif /* HAVE_HC128 */
  2855. return 0;
  2856. }
  2857. #ifndef NO_RABBIT
  2858. int rabbit_test(void)
  2859. {
  2860. byte cipher[16];
  2861. byte plain[16];
  2862. const char* keys[] =
  2863. {
  2864. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  2865. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  2866. "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B\xFE\x36\x3D\x2E\x29\x13\x28\x91"
  2867. };
  2868. const char* ivs[] =
  2869. {
  2870. "\x00\x00\x00\x00\x00\x00\x00\x00",
  2871. "\x59\x7E\x26\xC1\x75\xF5\x73\xC3",
  2872. 0
  2873. };
  2874. testVector a, b, c;
  2875. testVector test_rabbit[3];
  2876. int times = sizeof(test_rabbit) / sizeof(testVector), i;
  2877. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  2878. a.output = "\xED\xB7\x05\x67\x37\x5D\xCD\x7C";
  2879. a.inLen = 8;
  2880. a.outLen = 8;
  2881. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  2882. b.output = "\x6D\x7D\x01\x22\x92\xCC\xDC\xE0";
  2883. b.inLen = 8;
  2884. b.outLen = 8;
  2885. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  2886. c.output = "\x04\xCE\xCA\x7A\x1A\x86\x6E\x77";
  2887. c.inLen = 8;
  2888. c.outLen = 8;
  2889. test_rabbit[0] = a;
  2890. test_rabbit[1] = b;
  2891. test_rabbit[2] = c;
  2892. for (i = 0; i < times; ++i) {
  2893. Rabbit enc;
  2894. Rabbit dec;
  2895. byte* iv;
  2896. /* align keys/ivs in plain/cipher buffers */
  2897. XMEMCPY(plain, keys[i], 16);
  2898. if (ivs[i]) {
  2899. XMEMCPY(cipher, ivs[i], 8);
  2900. iv = cipher;
  2901. } else
  2902. iv = NULL;
  2903. wc_RabbitSetKey(&enc, plain, iv);
  2904. wc_RabbitSetKey(&dec, plain, iv);
  2905. /* align input */
  2906. XMEMCPY(plain, test_rabbit[i].input, test_rabbit[i].outLen);
  2907. wc_RabbitProcess(&enc, cipher, plain, (word32)test_rabbit[i].outLen);
  2908. wc_RabbitProcess(&dec, plain, cipher, (word32)test_rabbit[i].outLen);
  2909. if (XMEMCMP(plain, test_rabbit[i].input, test_rabbit[i].outLen))
  2910. return -3400 - i;
  2911. if (XMEMCMP(cipher, test_rabbit[i].output, test_rabbit[i].outLen))
  2912. return -3410 - i;
  2913. }
  2914. return 0;
  2915. }
  2916. #endif /* NO_RABBIT */
  2917. #ifdef HAVE_CHACHA
  2918. int chacha_test(void)
  2919. {
  2920. ChaCha enc;
  2921. ChaCha dec;
  2922. byte cipher[128];
  2923. byte plain[128];
  2924. byte sliver[64];
  2925. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  2926. word32 keySz = 32;
  2927. int ret = 0;
  2928. int i;
  2929. int times = 4;
  2930. static const byte key1[] =
  2931. {
  2932. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2933. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2934. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2935. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  2936. };
  2937. static const byte key2[] =
  2938. {
  2939. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2940. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2941. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2942. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  2943. };
  2944. static const byte key3[] =
  2945. {
  2946. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2947. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2948. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2949. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  2950. };
  2951. /* 128 bit key */
  2952. static const byte key4[] =
  2953. {
  2954. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  2955. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  2956. };
  2957. const byte* keys[] = {key1, key2, key3, key4};
  2958. static const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  2959. static const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  2960. static const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
  2961. static const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  2962. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  2963. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  2964. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  2965. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  2966. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  2967. byte* test_chacha[4];
  2968. test_chacha[0] = a;
  2969. test_chacha[1] = b;
  2970. test_chacha[2] = c;
  2971. test_chacha[3] = d;
  2972. for (i = 0; i < times; ++i) {
  2973. if (i < 3) {
  2974. keySz = 32;
  2975. }
  2976. else {
  2977. keySz = 16;
  2978. }
  2979. XMEMCPY(plain, keys[i], keySz);
  2980. XMEMSET(cipher, 0, 32);
  2981. XMEMCPY(cipher + 4, ivs[i], 8);
  2982. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  2983. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  2984. if (ret != 0)
  2985. return ret;
  2986. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  2987. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  2988. if (ret != 0)
  2989. return ret;
  2990. XMEMCPY(plain, input, 8);
  2991. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  2992. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  2993. if (ret != 0)
  2994. return ret;
  2995. if (XMEMCMP(test_chacha[i], cipher, 8))
  2996. return -3500 - i;
  2997. if (XMEMCMP(plain, input, 8))
  2998. return -3510 - i;
  2999. }
  3000. /* test of starting at a different counter
  3001. encrypts all of the information and decrypts starting at 2nd chunk */
  3002. XMEMSET(plain, 0, sizeof(plain));
  3003. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  3004. XMEMSET(cipher, 0, sizeof(cipher));
  3005. XMEMCPY(cipher + 4, ivs[0], 8);
  3006. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  3007. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  3008. if (ret != 0)
  3009. return ret;
  3010. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  3011. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  3012. if (ret != 0)
  3013. return ret;
  3014. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  3015. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  3016. if (ret != 0)
  3017. return ret;
  3018. if (XMEMCMP(plain + 64, sliver, 64))
  3019. return -3520;
  3020. return 0;
  3021. }
  3022. #endif /* HAVE_CHACHA */
  3023. #ifdef HAVE_POLY1305
  3024. int poly1305_test(void)
  3025. {
  3026. int ret = 0;
  3027. int i;
  3028. byte tag[16];
  3029. Poly1305 enc;
  3030. static const byte msg1[] =
  3031. {
  3032. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  3033. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  3034. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  3035. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  3036. 0x75,0x70
  3037. };
  3038. static const byte msg2[] =
  3039. {
  3040. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  3041. 0x6c,0x64,0x21
  3042. };
  3043. static const byte msg3[] =
  3044. {
  3045. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3046. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3047. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3048. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  3049. };
  3050. static const byte msg4[] =
  3051. {
  3052. 0xd3,0x1a,0x8d,0x34,0x64,0x8e,0x60,0xdb,
  3053. 0x7b,0x86,0xaf,0xbc,0x53,0xef,0x7e,0xc2,
  3054. 0xa4,0xad,0xed,0x51,0x29,0x6e,0x08,0xfe,
  3055. 0xa9,0xe2,0xb5,0xa7,0x36,0xee,0x62,0xd6,
  3056. 0x3d,0xbe,0xa4,0x5e,0x8c,0xa9,0x67,0x12,
  3057. 0x82,0xfa,0xfb,0x69,0xda,0x92,0x72,0x8b,
  3058. 0x1a,0x71,0xde,0x0a,0x9e,0x06,0x0b,0x29,
  3059. 0x05,0xd6,0xa5,0xb6,0x7e,0xcd,0x3b,0x36,
  3060. 0x92,0xdd,0xbd,0x7f,0x2d,0x77,0x8b,0x8c,
  3061. 0x98,0x03,0xae,0xe3,0x28,0x09,0x1b,0x58,
  3062. 0xfa,0xb3,0x24,0xe4,0xfa,0xd6,0x75,0x94,
  3063. 0x55,0x85,0x80,0x8b,0x48,0x31,0xd7,0xbc,
  3064. 0x3f,0xf4,0xde,0xf0,0x8e,0x4b,0x7a,0x9d,
  3065. 0xe5,0x76,0xd2,0x65,0x86,0xce,0xc6,0x4b,
  3066. 0x61,0x16
  3067. };
  3068. static const byte msg5[] =
  3069. {
  3070. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  3071. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  3072. };
  3073. byte additional[] =
  3074. {
  3075. 0x50,0x51,0x52,0x53,0xc0,0xc1,0xc2,0xc3,
  3076. 0xc4,0xc5,0xc6,0xc7
  3077. };
  3078. static const byte correct0[] =
  3079. {
  3080. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  3081. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  3082. };
  3083. static const byte correct1[] =
  3084. {
  3085. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  3086. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  3087. };
  3088. static const byte correct2[] =
  3089. {
  3090. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  3091. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  3092. };
  3093. static const byte correct3[] =
  3094. {
  3095. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  3096. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  3097. };
  3098. static const byte correct4[] =
  3099. {
  3100. 0x1a,0xe1,0x0b,0x59,0x4f,0x09,0xe2,0x6a,
  3101. 0x7e,0x90,0x2e,0xcb,0xd0,0x60,0x06,0x91
  3102. };
  3103. static const byte correct5[] =
  3104. {
  3105. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3106. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3107. };
  3108. static const byte key[] = {
  3109. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  3110. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  3111. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  3112. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  3113. };
  3114. static const byte key2[] = {
  3115. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  3116. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  3117. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  3118. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  3119. };
  3120. static const byte key4[] = {
  3121. 0x7b,0xac,0x2b,0x25,0x2d,0xb4,0x47,0xaf,
  3122. 0x09,0xb6,0x7a,0x55,0xa4,0xe9,0x55,0x84,
  3123. 0x0a,0xe1,0xd6,0x73,0x10,0x75,0xd9,0xeb,
  3124. 0x2a,0x93,0x75,0x78,0x3e,0xd5,0x53,0xff
  3125. };
  3126. static const byte key5[] = {
  3127. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3128. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3129. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  3130. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  3131. };
  3132. const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5};
  3133. word32 szm[] = {0, sizeof(msg1), sizeof(msg2),
  3134. sizeof(msg3), sizeof(msg5)};
  3135. const byte* keys[] = {key, key, key2, key2, key5};
  3136. const byte* tests[] = {correct0, correct1, correct2, correct3, correct5};
  3137. for (i = 0; i < 5; i++) {
  3138. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  3139. if (ret != 0)
  3140. return -3600 + i;
  3141. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  3142. if (ret != 0)
  3143. return -3605 + i;
  3144. ret = wc_Poly1305Final(&enc, tag);
  3145. if (ret != 0)
  3146. return -36108 + i;
  3147. if (XMEMCMP(tag, tests[i], sizeof(tag)))
  3148. return -3615 + i;
  3149. }
  3150. /* Check TLS MAC function from 2.8.2 https://tools.ietf.org/html/rfc7539 */
  3151. XMEMSET(tag, 0, sizeof(tag));
  3152. ret = wc_Poly1305SetKey(&enc, key4, sizeof(key4));
  3153. if (ret != 0)
  3154. return -3614;
  3155. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  3156. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  3157. if (ret != 0)
  3158. return -3615;
  3159. if (XMEMCMP(tag, correct4, sizeof(tag)))
  3160. return -3616;
  3161. /* Check fail of TLS MAC function if altering additional data */
  3162. XMEMSET(tag, 0, sizeof(tag));
  3163. additional[0]++;
  3164. ret = wc_Poly1305_MAC(&enc, additional, sizeof(additional),
  3165. (byte*)msg4, sizeof(msg4), tag, sizeof(tag));
  3166. if (ret != 0)
  3167. return -3617;
  3168. if (XMEMCMP(tag, correct4, sizeof(tag)) == 0)
  3169. return -3618;
  3170. return 0;
  3171. }
  3172. #endif /* HAVE_POLY1305 */
  3173. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  3174. int chacha20_poly1305_aead_test(void)
  3175. {
  3176. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  3177. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  3178. const byte key1[] = {
  3179. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  3180. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  3181. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  3182. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  3183. };
  3184. const byte plaintext1[] = {
  3185. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  3186. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  3187. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  3188. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  3189. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  3190. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  3191. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  3192. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  3193. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  3194. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  3195. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  3196. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  3197. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  3198. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  3199. 0x74, 0x2e
  3200. };
  3201. const byte iv1[] = {
  3202. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  3203. 0x44, 0x45, 0x46, 0x47
  3204. };
  3205. const byte aad1[] = { /* additional data */
  3206. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  3207. 0xc4, 0xc5, 0xc6, 0xc7
  3208. };
  3209. const byte cipher1[] = { /* expected output from operation */
  3210. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  3211. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  3212. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  3213. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  3214. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  3215. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  3216. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  3217. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  3218. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  3219. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  3220. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  3221. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  3222. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  3223. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  3224. 0x61, 0x16
  3225. };
  3226. const byte authTag1[] = { /* expected output from operation */
  3227. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  3228. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  3229. };
  3230. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  3231. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  3232. const byte key2[] = {
  3233. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  3234. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  3235. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  3236. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  3237. };
  3238. const byte plaintext2[] = {
  3239. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  3240. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  3241. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  3242. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  3243. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  3244. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  3245. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  3246. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  3247. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  3248. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  3249. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  3250. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  3251. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  3252. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  3253. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  3254. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  3255. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  3256. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  3257. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  3258. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  3259. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  3260. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  3261. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  3262. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  3263. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  3264. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  3265. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  3266. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  3267. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  3268. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  3269. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  3270. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  3271. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  3272. 0x9d
  3273. };
  3274. const byte iv2[] = {
  3275. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  3276. 0x05, 0x06, 0x07, 0x08
  3277. };
  3278. const byte aad2[] = { /* additional data */
  3279. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  3280. 0x00, 0x00, 0x4e, 0x91
  3281. };
  3282. const byte cipher2[] = { /* expected output from operation */
  3283. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  3284. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  3285. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  3286. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  3287. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  3288. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  3289. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  3290. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  3291. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  3292. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  3293. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  3294. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  3295. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  3296. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  3297. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  3298. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  3299. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  3300. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  3301. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  3302. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  3303. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  3304. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  3305. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  3306. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  3307. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  3308. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  3309. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  3310. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  3311. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  3312. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  3313. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  3314. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  3315. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  3316. 0x9b
  3317. };
  3318. const byte authTag2[] = { /* expected output from operation */
  3319. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  3320. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  3321. };
  3322. byte generatedCiphertext[272];
  3323. byte generatedPlaintext[272];
  3324. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  3325. int err;
  3326. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  3327. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  3328. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  3329. /* Parameter Validation testing */
  3330. /* Encrypt */
  3331. err = wc_ChaCha20Poly1305_Encrypt(NULL, iv1, aad1, sizeof(aad1), plaintext1,
  3332. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  3333. if (err != BAD_FUNC_ARG)
  3334. return -3700;
  3335. err = wc_ChaCha20Poly1305_Encrypt(key1, NULL, aad1, sizeof(aad1),
  3336. plaintext1, sizeof(plaintext1), generatedCiphertext,
  3337. generatedAuthTag);
  3338. if (err != BAD_FUNC_ARG)
  3339. return -3701;
  3340. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), NULL,
  3341. sizeof(plaintext1), generatedCiphertext, generatedAuthTag);
  3342. if (err != BAD_FUNC_ARG)
  3343. return -3702;
  3344. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  3345. sizeof(plaintext1), NULL, generatedAuthTag);
  3346. if (err != BAD_FUNC_ARG)
  3347. return -3703;
  3348. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  3349. sizeof(plaintext1), generatedCiphertext, NULL);
  3350. if (err != BAD_FUNC_ARG)
  3351. return -3704;
  3352. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1, aad1, sizeof(aad1), plaintext1,
  3353. 0, generatedCiphertext, generatedAuthTag);
  3354. if (err != BAD_FUNC_ARG)
  3355. return -3705;
  3356. /* Decrypt */
  3357. err = wc_ChaCha20Poly1305_Decrypt(NULL, iv2, aad2, sizeof(aad2), cipher2,
  3358. sizeof(cipher2), authTag2, generatedPlaintext);
  3359. if (err != BAD_FUNC_ARG)
  3360. return -3706;
  3361. err = wc_ChaCha20Poly1305_Decrypt(key2, NULL, aad2, sizeof(aad2), cipher2,
  3362. sizeof(cipher2), authTag2, generatedPlaintext);
  3363. if (err != BAD_FUNC_ARG)
  3364. return -3707;
  3365. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), NULL,
  3366. sizeof(cipher2), authTag2, generatedPlaintext);
  3367. if (err != BAD_FUNC_ARG)
  3368. return -3708;
  3369. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  3370. sizeof(cipher2), NULL, generatedPlaintext);
  3371. if (err != BAD_FUNC_ARG)
  3372. return -3709;
  3373. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  3374. sizeof(cipher2), authTag2, NULL);
  3375. if (err != BAD_FUNC_ARG)
  3376. return -3710;
  3377. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2, aad2, sizeof(aad2), cipher2,
  3378. 0, authTag2, generatedPlaintext);
  3379. if (err != BAD_FUNC_ARG)
  3380. return -3711;
  3381. /* Test #1 */
  3382. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  3383. aad1, sizeof(aad1),
  3384. plaintext1, sizeof(plaintext1),
  3385. generatedCiphertext, generatedAuthTag);
  3386. if (err) {
  3387. return err;
  3388. }
  3389. /* -- Check the ciphertext and authtag */
  3390. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1))) {
  3391. return -3712;
  3392. }
  3393. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1))) {
  3394. return -3713;
  3395. }
  3396. /* -- Verify decryption works */
  3397. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  3398. aad1, sizeof(aad1),
  3399. cipher1, sizeof(cipher1),
  3400. authTag1, generatedPlaintext);
  3401. if (err) {
  3402. return err;
  3403. }
  3404. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof( plaintext1))) {
  3405. return -3714;
  3406. }
  3407. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  3408. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  3409. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  3410. /* Test #2 */
  3411. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  3412. aad2, sizeof(aad2),
  3413. plaintext2, sizeof(plaintext2),
  3414. generatedCiphertext, generatedAuthTag);
  3415. if (err) {
  3416. return err;
  3417. }
  3418. /* -- Check the ciphertext and authtag */
  3419. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2))) {
  3420. return -3715;
  3421. }
  3422. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2))) {
  3423. return -3716;
  3424. }
  3425. /* -- Verify decryption works */
  3426. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  3427. aad2, sizeof(aad2),
  3428. cipher2, sizeof(cipher2),
  3429. authTag2, generatedPlaintext);
  3430. if (err) {
  3431. return err;
  3432. }
  3433. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2))) {
  3434. return -3717;
  3435. }
  3436. return err;
  3437. }
  3438. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  3439. #ifndef NO_DES3
  3440. int des_test(void)
  3441. {
  3442. const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  3443. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  3444. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  3445. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  3446. };
  3447. byte plain[24];
  3448. byte cipher[24];
  3449. Des enc;
  3450. Des dec;
  3451. const byte key[] =
  3452. {
  3453. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  3454. };
  3455. const byte iv[] =
  3456. {
  3457. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  3458. };
  3459. const byte verify[] =
  3460. {
  3461. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  3462. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  3463. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  3464. };
  3465. int ret;
  3466. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  3467. if (ret != 0)
  3468. return -3800;
  3469. ret = wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  3470. if (ret != 0)
  3471. return -3801;
  3472. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  3473. if (ret != 0)
  3474. return -3802;
  3475. ret = wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  3476. if (ret != 0)
  3477. return -3803;
  3478. if (XMEMCMP(plain, vector, sizeof(plain)))
  3479. return -3804;
  3480. if (XMEMCMP(cipher, verify, sizeof(cipher)))
  3481. return -3805;
  3482. return 0;
  3483. }
  3484. #endif /* NO_DES3 */
  3485. #ifndef NO_DES3
  3486. int des3_test(void)
  3487. {
  3488. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  3489. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  3490. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  3491. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  3492. };
  3493. byte plain[24];
  3494. byte cipher[24];
  3495. Des3 enc;
  3496. Des3 dec;
  3497. const byte key3[] =
  3498. {
  3499. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  3500. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  3501. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  3502. };
  3503. const byte iv3[] =
  3504. {
  3505. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  3506. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  3507. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  3508. };
  3509. const byte verify3[] =
  3510. {
  3511. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  3512. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  3513. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  3514. };
  3515. int ret;
  3516. if (wc_Des3Init(&enc, HEAP_HINT, devId) != 0)
  3517. return -3900;
  3518. if (wc_Des3Init(&dec, HEAP_HINT, devId) != 0)
  3519. return -3901;
  3520. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  3521. if (ret != 0)
  3522. return -3902;
  3523. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  3524. if (ret != 0)
  3525. return -3903;
  3526. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  3527. #if defined(WOLFSSL_ASYNC_CRYPT)
  3528. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  3529. #endif
  3530. if (ret != 0)
  3531. return -3904;
  3532. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  3533. #if defined(WOLFSSL_ASYNC_CRYPT)
  3534. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  3535. #endif
  3536. if (ret != 0)
  3537. return -3905;
  3538. if (XMEMCMP(plain, vector, sizeof(plain)))
  3539. return -3906;
  3540. if (XMEMCMP(cipher, verify3, sizeof(cipher)))
  3541. return -3907;
  3542. wc_Des3Free(&enc);
  3543. wc_Des3Free(&dec);
  3544. return 0;
  3545. }
  3546. #endif /* NO_DES */
  3547. #ifndef NO_AES
  3548. static int aes_key_size_test(void)
  3549. {
  3550. int ret;
  3551. Aes aes;
  3552. byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  3553. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  3554. byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  3555. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  3556. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 };
  3557. byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  3558. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  3559. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  3560. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 };
  3561. byte iv[] = "1234567890abcdef";
  3562. #ifndef HAVE_FIPS
  3563. word32 keySize;
  3564. #endif
  3565. #ifdef WC_INITAES_H
  3566. ret = wc_InitAes_h(NULL, NULL);
  3567. if (ret != BAD_FUNC_ARG)
  3568. return -4000;
  3569. ret = wc_InitAes_h(&aes, NULL);
  3570. if (ret != 0)
  3571. return -4001;
  3572. #endif
  3573. #ifndef HAVE_FIPS
  3574. /* Parameter Validation testing. */
  3575. ret = wc_AesGetKeySize(NULL, NULL);
  3576. if (ret != BAD_FUNC_ARG)
  3577. return -4002;
  3578. ret = wc_AesGetKeySize(&aes, NULL);
  3579. if (ret != BAD_FUNC_ARG)
  3580. return -4003;
  3581. ret = wc_AesGetKeySize(NULL, &keySize);
  3582. if (ret != BAD_FUNC_ARG)
  3583. return -4004;
  3584. /* Crashes in FIPS */
  3585. ret = wc_AesSetKey(NULL, key16, sizeof(key16), iv, AES_ENCRYPTION);
  3586. if (ret != BAD_FUNC_ARG)
  3587. return -4005;
  3588. #endif
  3589. /* NULL IV indicates to use all zeros IV. */
  3590. ret = wc_AesSetKey(&aes, key16, sizeof(key16), NULL, AES_ENCRYPTION);
  3591. if (ret != 0)
  3592. return -4006;
  3593. ret = wc_AesSetKey(&aes, key32, sizeof(key32) - 1, iv, AES_ENCRYPTION);
  3594. if (ret != BAD_FUNC_ARG)
  3595. return -4007;
  3596. #ifndef HAVE_FIPS
  3597. /* Force invalid rounds */
  3598. aes.rounds = 16;
  3599. ret = wc_AesGetKeySize(&aes, &keySize);
  3600. if (ret != BAD_FUNC_ARG)
  3601. return -4008;
  3602. #endif
  3603. ret = wc_AesSetKey(&aes, key16, sizeof(key16), iv, AES_ENCRYPTION);
  3604. if (ret != 0)
  3605. return -4009;
  3606. #ifndef HAVE_FIPS
  3607. ret = wc_AesGetKeySize(&aes, &keySize);
  3608. if (ret != 0 || keySize != sizeof(key16))
  3609. return -4010;
  3610. #endif
  3611. ret = wc_AesSetKey(&aes, key24, sizeof(key24), iv, AES_ENCRYPTION);
  3612. if (ret != 0)
  3613. return -4011;
  3614. #ifndef HAVE_FIPS
  3615. ret = wc_AesGetKeySize(&aes, &keySize);
  3616. if (ret != 0 || keySize != sizeof(key24))
  3617. return -4012;
  3618. #endif
  3619. ret = wc_AesSetKey(&aes, key32, sizeof(key32), iv, AES_ENCRYPTION);
  3620. if (ret != 0)
  3621. return -4013;
  3622. #ifndef HAVE_FIPS
  3623. ret = wc_AesGetKeySize(&aes, &keySize);
  3624. if (ret != 0 || keySize != sizeof(key32))
  3625. return -4014;
  3626. #endif
  3627. return 0;
  3628. }
  3629. #if defined(WOLFSSL_AES_XTS)
  3630. /* test vectors from http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html */
  3631. static int aes_xts_128_test(void)
  3632. {
  3633. XtsAes aes;
  3634. int ret = 0;
  3635. unsigned char buf[AES_BLOCK_SIZE * 2];
  3636. unsigned char cipher[AES_BLOCK_SIZE * 2];
  3637. /* 128 key tests */
  3638. static unsigned char k1[] = {
  3639. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  3640. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  3641. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  3642. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  3643. };
  3644. static unsigned char i1[] = {
  3645. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  3646. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  3647. };
  3648. static unsigned char p1[] = {
  3649. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  3650. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c
  3651. };
  3652. /* plain text test of partial block is not from NIST test vector list */
  3653. static unsigned char pp[] = {
  3654. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  3655. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  3656. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  3657. };
  3658. static unsigned char c1[] = {
  3659. 0x77, 0x8a, 0xe8, 0xb4, 0x3c, 0xb9, 0x8d, 0x5a,
  3660. 0x82, 0x50, 0x81, 0xd5, 0xbe, 0x47, 0x1c, 0x63
  3661. };
  3662. static unsigned char k2[] = {
  3663. 0x39, 0x25, 0x79, 0x05, 0xdf, 0xcc, 0x77, 0x76,
  3664. 0x6c, 0x87, 0x0a, 0x80, 0x6a, 0x60, 0xe3, 0xc0,
  3665. 0x93, 0xd1, 0x2a, 0xcf, 0xcb, 0x51, 0x42, 0xfa,
  3666. 0x09, 0x69, 0x89, 0x62, 0x5b, 0x60, 0xdb, 0x16
  3667. };
  3668. static unsigned char i2[] = {
  3669. 0x5c, 0xf7, 0x9d, 0xb6, 0xc5, 0xcd, 0x99, 0x1a,
  3670. 0x1c, 0x78, 0x81, 0x42, 0x24, 0x95, 0x1e, 0x84
  3671. };
  3672. static unsigned char p2[] = {
  3673. 0xbd, 0xc5, 0x46, 0x8f, 0xbc, 0x8d, 0x50, 0xa1,
  3674. 0x0d, 0x1c, 0x85, 0x7f, 0x79, 0x1c, 0x5c, 0xba,
  3675. 0xb3, 0x81, 0x0d, 0x0d, 0x73, 0xcf, 0x8f, 0x20,
  3676. 0x46, 0xb1, 0xd1, 0x9e, 0x7d, 0x5d, 0x8a, 0x56
  3677. };
  3678. static unsigned char c2[] = {
  3679. 0xd6, 0xbe, 0x04, 0x6d, 0x41, 0xf2, 0x3b, 0x5e,
  3680. 0xd7, 0x0b, 0x6b, 0x3d, 0x5c, 0x8e, 0x66, 0x23,
  3681. 0x2b, 0xe6, 0xb8, 0x07, 0xd4, 0xdc, 0xc6, 0x0e,
  3682. 0xff, 0x8d, 0xbc, 0x1d, 0x9f, 0x7f, 0xc8, 0x22
  3683. };
  3684. XMEMSET(buf, 0, sizeof(buf));
  3685. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_ENCRYPTION,
  3686. HEAP_HINT, devId) != 0)
  3687. return -4000;
  3688. ret = wc_AesXtsEncrypt(&aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  3689. #if defined(WOLFSSL_ASYNC_CRYPT)
  3690. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3691. #endif
  3692. if (ret != 0)
  3693. return -4001;
  3694. if (XMEMCMP(c2, buf, sizeof(c2)))
  3695. return -4002;
  3696. XMEMSET(buf, 0, sizeof(buf));
  3697. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_ENCRYPTION,
  3698. HEAP_HINT, devId) != 0)
  3699. return -4003;
  3700. ret = wc_AesXtsEncrypt(&aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  3701. #if defined(WOLFSSL_ASYNC_CRYPT)
  3702. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3703. #endif
  3704. if (ret != 0)
  3705. return -4004;
  3706. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  3707. return -4005;
  3708. /* partial block encryption test */
  3709. XMEMSET(cipher, 0, sizeof(cipher));
  3710. ret = wc_AesXtsEncrypt(&aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  3711. #if defined(WOLFSSL_ASYNC_CRYPT)
  3712. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3713. #endif
  3714. if (ret != 0)
  3715. return -4006;
  3716. wc_AesXtsFree(&aes);
  3717. /* partial block decrypt test */
  3718. XMEMSET(buf, 0, sizeof(buf));
  3719. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_DECRYPTION,
  3720. HEAP_HINT, devId) != 0)
  3721. return -4007;
  3722. ret = wc_AesXtsDecrypt(&aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  3723. #if defined(WOLFSSL_ASYNC_CRYPT)
  3724. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3725. #endif
  3726. if (ret != 0)
  3727. return -4008;
  3728. if (XMEMCMP(pp, buf, sizeof(pp)))
  3729. return -4009;
  3730. /* NIST decrypt test vector */
  3731. XMEMSET(buf, 0, sizeof(buf));
  3732. ret = wc_AesXtsDecrypt(&aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  3733. #if defined(WOLFSSL_ASYNC_CRYPT)
  3734. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3735. #endif
  3736. if (ret != 0)
  3737. return -4010;
  3738. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  3739. return -4011;
  3740. /* fail case with decrypting using wrong key */
  3741. XMEMSET(buf, 0, sizeof(buf));
  3742. ret = wc_AesXtsDecrypt(&aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  3743. #if defined(WOLFSSL_ASYNC_CRYPT)
  3744. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3745. #endif
  3746. if (ret != 0)
  3747. return -4012;
  3748. if (XMEMCMP(p2, buf, sizeof(p2)) == 0) /* fail case with wrong key */
  3749. return -4013;
  3750. /* set correct key and retest */
  3751. XMEMSET(buf, 0, sizeof(buf));
  3752. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_DECRYPTION,
  3753. HEAP_HINT, devId) != 0)
  3754. return -4014;
  3755. ret = wc_AesXtsDecrypt(&aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  3756. #if defined(WOLFSSL_ASYNC_CRYPT)
  3757. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3758. #endif
  3759. if (ret != 0)
  3760. return -4015;
  3761. if (XMEMCMP(p2, buf, sizeof(p2)))
  3762. return -4016;
  3763. wc_AesXtsFree(&aes);
  3764. return ret;
  3765. }
  3766. static int aes_xts_256_test(void)
  3767. {
  3768. XtsAes aes;
  3769. int ret = 0;
  3770. unsigned char buf[AES_BLOCK_SIZE * 3];
  3771. unsigned char cipher[AES_BLOCK_SIZE * 3];
  3772. /* 256 key tests */
  3773. static unsigned char k1[] = {
  3774. 0x1e, 0xa6, 0x61, 0xc5, 0x8d, 0x94, 0x3a, 0x0e,
  3775. 0x48, 0x01, 0xe4, 0x2f, 0x4b, 0x09, 0x47, 0x14,
  3776. 0x9e, 0x7f, 0x9f, 0x8e, 0x3e, 0x68, 0xd0, 0xc7,
  3777. 0x50, 0x52, 0x10, 0xbd, 0x31, 0x1a, 0x0e, 0x7c,
  3778. 0xd6, 0xe1, 0x3f, 0xfd, 0xf2, 0x41, 0x8d, 0x8d,
  3779. 0x19, 0x11, 0xc0, 0x04, 0xcd, 0xa5, 0x8d, 0xa3,
  3780. 0xd6, 0x19, 0xb7, 0xe2, 0xb9, 0x14, 0x1e, 0x58,
  3781. 0x31, 0x8e, 0xea, 0x39, 0x2c, 0xf4, 0x1b, 0x08
  3782. };
  3783. static unsigned char i1[] = {
  3784. 0xad, 0xf8, 0xd9, 0x26, 0x27, 0x46, 0x4a, 0xd2,
  3785. 0xf0, 0x42, 0x8e, 0x84, 0xa9, 0xf8, 0x75, 0x64
  3786. };
  3787. static unsigned char p1[] = {
  3788. 0x2e, 0xed, 0xea, 0x52, 0xcd, 0x82, 0x15, 0xe1,
  3789. 0xac, 0xc6, 0x47, 0xe8, 0x10, 0xbb, 0xc3, 0x64,
  3790. 0x2e, 0x87, 0x28, 0x7f, 0x8d, 0x2e, 0x57, 0xe3,
  3791. 0x6c, 0x0a, 0x24, 0xfb, 0xc1, 0x2a, 0x20, 0x2e
  3792. };
  3793. /* plain text test of partial block is not from NIST test vector list */
  3794. static unsigned char pp[] = {
  3795. 0xeb, 0xab, 0xce, 0x95, 0xb1, 0x4d, 0x3c, 0x8d,
  3796. 0x6f, 0xb3, 0x50, 0x39, 0x07, 0x90, 0x31, 0x1c,
  3797. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  3798. };
  3799. static unsigned char c1[] = {
  3800. 0xcb, 0xaa, 0xd0, 0xe2, 0xf6, 0xce, 0xa3, 0xf5,
  3801. 0x0b, 0x37, 0xf9, 0x34, 0xd4, 0x6a, 0x9b, 0x13,
  3802. 0x0b, 0x9d, 0x54, 0xf0, 0x7e, 0x34, 0xf3, 0x6a,
  3803. 0xf7, 0x93, 0xe8, 0x6f, 0x73, 0xc6, 0xd7, 0xdb
  3804. };
  3805. static unsigned char k2[] = {
  3806. 0xad, 0x50, 0x4b, 0x85, 0xd7, 0x51, 0xbf, 0xba,
  3807. 0x69, 0x13, 0xb4, 0xcc, 0x79, 0xb6, 0x5a, 0x62,
  3808. 0xf7, 0xf3, 0x9d, 0x36, 0x0f, 0x35, 0xb5, 0xec,
  3809. 0x4a, 0x7e, 0x95, 0xbd, 0x9b, 0xa5, 0xf2, 0xec,
  3810. 0xc1, 0xd7, 0x7e, 0xa3, 0xc3, 0x74, 0xbd, 0x4b,
  3811. 0x13, 0x1b, 0x07, 0x83, 0x87, 0xdd, 0x55, 0x5a,
  3812. 0xb5, 0xb0, 0xc7, 0xe5, 0x2d, 0xb5, 0x06, 0x12,
  3813. 0xd2, 0xb5, 0x3a, 0xcb, 0x47, 0x8a, 0x53, 0xb4
  3814. };
  3815. static unsigned char i2[] = {
  3816. 0xe6, 0x42, 0x19, 0xed, 0xe0, 0xe1, 0xc2, 0xa0,
  3817. 0x0e, 0xf5, 0x58, 0x6a, 0xc4, 0x9b, 0xeb, 0x6f
  3818. };
  3819. static unsigned char p2[] = {
  3820. 0x24, 0xcb, 0x76, 0x22, 0x55, 0xb5, 0xa8, 0x00,
  3821. 0xf4, 0x6e, 0x80, 0x60, 0x56, 0x9e, 0x05, 0x53,
  3822. 0xbc, 0xfe, 0x86, 0x55, 0x3b, 0xca, 0xd5, 0x89,
  3823. 0xc7, 0x54, 0x1a, 0x73, 0xac, 0xc3, 0x9a, 0xbd,
  3824. 0x53, 0xc4, 0x07, 0x76, 0xd8, 0xe8, 0x22, 0x61,
  3825. 0x9e, 0xa9, 0xad, 0x77, 0xa0, 0x13, 0x4c, 0xfc
  3826. };
  3827. static unsigned char c2[] = {
  3828. 0xa3, 0xc6, 0xf3, 0xf3, 0x82, 0x79, 0x5b, 0x10,
  3829. 0x87, 0xd7, 0x02, 0x50, 0xdb, 0x2c, 0xd3, 0xb1,
  3830. 0xa1, 0x62, 0xa8, 0xb6, 0xdc, 0x12, 0x60, 0x61,
  3831. 0xc1, 0x0a, 0x84, 0xa5, 0x85, 0x3f, 0x3a, 0x89,
  3832. 0xe6, 0x6c, 0xdb, 0xb7, 0x9a, 0xb4, 0x28, 0x9b,
  3833. 0xc3, 0xea, 0xd8, 0x10, 0xe9, 0xc0, 0xaf, 0x92
  3834. };
  3835. XMEMSET(buf, 0, sizeof(buf));
  3836. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_ENCRYPTION,
  3837. HEAP_HINT, devId) != 0)
  3838. return -4017;
  3839. ret = wc_AesXtsEncrypt(&aes, buf, p2, sizeof(p2), i2, sizeof(i2));
  3840. #if defined(WOLFSSL_ASYNC_CRYPT)
  3841. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3842. #endif
  3843. if (ret != 0)
  3844. return -4018;
  3845. if (XMEMCMP(c2, buf, sizeof(c2)))
  3846. return -4019;
  3847. XMEMSET(buf, 0, sizeof(buf));
  3848. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_ENCRYPTION,
  3849. HEAP_HINT, devId) != 0)
  3850. return -4020;
  3851. ret = wc_AesXtsEncrypt(&aes, buf, p1, sizeof(p1), i1, sizeof(i1));
  3852. #if defined(WOLFSSL_ASYNC_CRYPT)
  3853. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3854. #endif
  3855. if (ret != 0)
  3856. return -4021;
  3857. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  3858. return -4022;
  3859. /* partial block encryption test */
  3860. XMEMSET(cipher, 0, sizeof(cipher));
  3861. ret = wc_AesXtsEncrypt(&aes, cipher, pp, sizeof(pp), i1, sizeof(i1));
  3862. #if defined(WOLFSSL_ASYNC_CRYPT)
  3863. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3864. #endif
  3865. if (ret != 0)
  3866. return -4023;
  3867. wc_AesXtsFree(&aes);
  3868. /* partial block decrypt test */
  3869. XMEMSET(buf, 0, sizeof(buf));
  3870. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_DECRYPTION,
  3871. HEAP_HINT, devId) != 0)
  3872. return -4024;
  3873. ret = wc_AesXtsDecrypt(&aes, buf, cipher, sizeof(pp), i1, sizeof(i1));
  3874. #if defined(WOLFSSL_ASYNC_CRYPT)
  3875. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3876. #endif
  3877. if (ret != 0)
  3878. return -4025;
  3879. if (XMEMCMP(pp, buf, sizeof(pp)))
  3880. return -4026;
  3881. /* NIST decrypt test vector */
  3882. XMEMSET(buf, 0, sizeof(buf));
  3883. ret = wc_AesXtsDecrypt(&aes, buf, c1, sizeof(c1), i1, sizeof(i1));
  3884. #if defined(WOLFSSL_ASYNC_CRYPT)
  3885. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3886. #endif
  3887. if (ret != 0)
  3888. return -4027;
  3889. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  3890. return -4028;
  3891. XMEMSET(buf, 0, sizeof(buf));
  3892. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_DECRYPTION,
  3893. HEAP_HINT, devId) != 0)
  3894. return -4029;
  3895. ret = wc_AesXtsDecrypt(&aes, buf, c2, sizeof(c2), i2, sizeof(i2));
  3896. #if defined(WOLFSSL_ASYNC_CRYPT)
  3897. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3898. #endif
  3899. if (ret != 0)
  3900. return -4030;
  3901. if (XMEMCMP(p2, buf, sizeof(p2)))
  3902. return -4031;
  3903. wc_AesXtsFree(&aes);
  3904. return ret;
  3905. }
  3906. /* both 128 and 256 bit key test */
  3907. static int aes_xts_sector_test(void)
  3908. {
  3909. XtsAes aes;
  3910. int ret = 0;
  3911. unsigned char buf[AES_BLOCK_SIZE * 2];
  3912. /* 128 key tests */
  3913. static unsigned char k1[] = {
  3914. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  3915. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  3916. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  3917. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  3918. };
  3919. static unsigned char p1[] = {
  3920. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  3921. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  3922. };
  3923. static unsigned char c1[] = {
  3924. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  3925. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  3926. };
  3927. word64 s1 = 141;
  3928. /* 256 key tests */
  3929. static unsigned char k2[] = {
  3930. 0xef, 0x01, 0x0c, 0xa1, 0xa3, 0x66, 0x3e, 0x32,
  3931. 0x53, 0x43, 0x49, 0xbc, 0x0b, 0xae, 0x62, 0x23,
  3932. 0x2a, 0x15, 0x73, 0x34, 0x85, 0x68, 0xfb, 0x9e,
  3933. 0xf4, 0x17, 0x68, 0xa7, 0x67, 0x4f, 0x50, 0x7a,
  3934. 0x72, 0x7f, 0x98, 0x75, 0x53, 0x97, 0xd0, 0xe0,
  3935. 0xaa, 0x32, 0xf8, 0x30, 0x33, 0x8c, 0xc7, 0xa9,
  3936. 0x26, 0xc7, 0x73, 0xf0, 0x9e, 0x57, 0xb3, 0x57,
  3937. 0xcd, 0x15, 0x6a, 0xfb, 0xca, 0x46, 0xe1, 0xa0
  3938. };
  3939. static unsigned char p2[] = {
  3940. 0xed, 0x98, 0xe0, 0x17, 0x70, 0xa8, 0x53, 0xb4,
  3941. 0x9d, 0xb9, 0xe6, 0xaa, 0xf8, 0x8f, 0x0a, 0x41,
  3942. 0xb9, 0xb5, 0x6e, 0x91, 0xa5, 0xa2, 0xb1, 0x1d,
  3943. 0x40, 0x52, 0x92, 0x54, 0xf5, 0x52, 0x3e, 0x75
  3944. };
  3945. static unsigned char c2[] = {
  3946. 0xca, 0x20, 0xc5, 0x5e, 0x8d, 0xc1, 0x49, 0x68,
  3947. 0x7d, 0x25, 0x41, 0xde, 0x39, 0xc3, 0xdf, 0x63,
  3948. 0x00, 0xbb, 0x5a, 0x16, 0x3c, 0x10, 0xce, 0xd3,
  3949. 0x66, 0x6b, 0x13, 0x57, 0xdb, 0x8b, 0xd3, 0x9d
  3950. };
  3951. word64 s2 = 187;
  3952. XMEMSET(buf, 0, sizeof(buf));
  3953. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_ENCRYPTION,
  3954. HEAP_HINT, devId) != 0)
  3955. return -4032;
  3956. ret = wc_AesXtsEncryptSector(&aes, buf, p1, sizeof(p1), s1);
  3957. #if defined(WOLFSSL_ASYNC_CRYPT)
  3958. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3959. #endif
  3960. if (ret != 0)
  3961. return -4033;
  3962. if (XMEMCMP(c1, buf, AES_BLOCK_SIZE))
  3963. return -4034;
  3964. /* decrypt test */
  3965. XMEMSET(buf, 0, sizeof(buf));
  3966. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_DECRYPTION,
  3967. HEAP_HINT, devId) != 0)
  3968. return -4035;
  3969. ret = wc_AesXtsDecryptSector(&aes, buf, c1, sizeof(c1), s1);
  3970. #if defined(WOLFSSL_ASYNC_CRYPT)
  3971. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3972. #endif
  3973. if (ret != 0)
  3974. return -4036;
  3975. if (XMEMCMP(p1, buf, AES_BLOCK_SIZE))
  3976. return -4037;
  3977. wc_AesXtsFree(&aes);
  3978. /* 256 bit key tests */
  3979. XMEMSET(buf, 0, sizeof(buf));
  3980. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_ENCRYPTION,
  3981. HEAP_HINT, devId) != 0)
  3982. return -4038;
  3983. ret = wc_AesXtsEncryptSector(&aes, buf, p2, sizeof(p2), s2);
  3984. #if defined(WOLFSSL_ASYNC_CRYPT)
  3985. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3986. #endif
  3987. if (ret != 0)
  3988. return -4039;
  3989. if (XMEMCMP(c2, buf, sizeof(c2)))
  3990. return -4040;
  3991. /* decrypt test */
  3992. XMEMSET(buf, 0, sizeof(buf));
  3993. if (wc_AesXtsSetKey(&aes, k2, sizeof(k2), AES_DECRYPTION,
  3994. HEAP_HINT, devId) != 0)
  3995. return -4041;
  3996. ret = wc_AesXtsDecryptSector(&aes, buf, c2, sizeof(c2), s2);
  3997. #if defined(WOLFSSL_ASYNC_CRYPT)
  3998. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  3999. #endif
  4000. if (ret != 0)
  4001. return -4042;
  4002. if (XMEMCMP(p2, buf, sizeof(p2)))
  4003. return -4043;
  4004. wc_AesXtsFree(&aes);
  4005. return ret;
  4006. }
  4007. /* testing of bad arguments */
  4008. static int aes_xts_args_test(void)
  4009. {
  4010. XtsAes aes;
  4011. int ret = 0;
  4012. unsigned char buf[AES_BLOCK_SIZE * 2];
  4013. /* 128 key tests */
  4014. static unsigned char k1[] = {
  4015. 0xa3, 0xe4, 0x0d, 0x5b, 0xd4, 0xb6, 0xbb, 0xed,
  4016. 0xb2, 0xd1, 0x8c, 0x70, 0x0a, 0xd2, 0xdb, 0x22,
  4017. 0x10, 0xc8, 0x11, 0x90, 0x64, 0x6d, 0x67, 0x3c,
  4018. 0xbc, 0xa5, 0x3f, 0x13, 0x3e, 0xab, 0x37, 0x3c
  4019. };
  4020. static unsigned char p1[] = {
  4021. 0x20, 0xe0, 0x71, 0x94, 0x05, 0x99, 0x3f, 0x09,
  4022. 0xa6, 0x6a, 0xe5, 0xbb, 0x50, 0x0e, 0x56, 0x2c
  4023. };
  4024. static unsigned char c1[] = {
  4025. 0x74, 0x62, 0x35, 0x51, 0x21, 0x02, 0x16, 0xac,
  4026. 0x92, 0x6b, 0x96, 0x50, 0xb6, 0xd3, 0xfa, 0x52
  4027. };
  4028. word64 s1 = 141;
  4029. if (wc_AesXtsSetKey(NULL, k1, sizeof(k1), AES_ENCRYPTION,
  4030. HEAP_HINT, devId) == 0)
  4031. return -4044;
  4032. if (wc_AesXtsSetKey(&aes, NULL, sizeof(k1), AES_ENCRYPTION,
  4033. HEAP_HINT, devId) == 0)
  4034. return -4045;
  4035. /* encryption operations */
  4036. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_ENCRYPTION,
  4037. HEAP_HINT, devId) != 0)
  4038. return -4046;
  4039. ret = wc_AesXtsEncryptSector(NULL, buf, p1, sizeof(p1), s1);
  4040. #if defined(WOLFSSL_ASYNC_CRYPT)
  4041. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4042. #endif
  4043. if (ret == 0)
  4044. return -4047;
  4045. ret = wc_AesXtsEncryptSector(&aes, NULL, p1, sizeof(p1), s1);
  4046. #if defined(WOLFSSL_ASYNC_CRYPT)
  4047. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4048. #endif
  4049. if (ret == 0)
  4050. return -4048;
  4051. wc_AesXtsFree(&aes);
  4052. /* decryption operations */
  4053. if (wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_DECRYPTION,
  4054. HEAP_HINT, devId) != 0)
  4055. return -4046;
  4056. ret = wc_AesXtsDecryptSector(NULL, buf, c1, sizeof(c1), s1);
  4057. #if defined(WOLFSSL_ASYNC_CRYPT)
  4058. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4059. #endif
  4060. if (ret == 0)
  4061. return -4049;
  4062. ret = wc_AesXtsDecryptSector(&aes, NULL, c1, sizeof(c1), s1);
  4063. #if defined(WOLFSSL_ASYNC_CRYPT)
  4064. ret = wc_AsyncWait(ret, &aes.aes.asyncDev, WC_ASYNC_FLAG_NONE);
  4065. #endif
  4066. if (ret == 0)
  4067. return -4050;
  4068. wc_AesXtsFree(&aes);
  4069. return 0;
  4070. }
  4071. #endif /* WOLFSSL_AES_XTS */
  4072. #if defined(HAVE_AES_CBC)
  4073. static int aes_cbc_test(void)
  4074. {
  4075. byte cipher[AES_BLOCK_SIZE];
  4076. byte plain[AES_BLOCK_SIZE];
  4077. int ret;
  4078. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  4079. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  4080. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  4081. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  4082. };
  4083. byte key[] = "0123456789abcdef "; /* align */
  4084. byte iv[] = "1234567890abcdef "; /* align */
  4085. /* Parameter Validation testing. */
  4086. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key, 17, NULL);
  4087. if (ret != BAD_FUNC_ARG)
  4088. return -4100;
  4089. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key, 17, NULL);
  4090. if (ret != BAD_FUNC_ARG)
  4091. return -4101;
  4092. ret = wc_AesCbcEncryptWithKey(cipher, msg, AES_BLOCK_SIZE, key,
  4093. AES_BLOCK_SIZE, iv);
  4094. if (ret != 0)
  4095. return -4102;
  4096. ret = wc_AesCbcDecryptWithKey(plain, cipher, AES_BLOCK_SIZE, key,
  4097. AES_BLOCK_SIZE, iv);
  4098. if (ret != 0)
  4099. return -4103;
  4100. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE) != 0)
  4101. return -4104;
  4102. return 0;
  4103. }
  4104. #endif
  4105. int aes_test(void)
  4106. {
  4107. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_COUNTER)
  4108. Aes enc;
  4109. byte cipher[AES_BLOCK_SIZE * 4];
  4110. #if defined(HAVE_AES_DECRYPT) || defined(WOLFSSL_AES_COUNTER)
  4111. Aes dec;
  4112. byte plain [AES_BLOCK_SIZE * 4];
  4113. #endif
  4114. #endif /* HAVE_AES_CBC || WOLFSSL_AES_COUNTER */
  4115. int ret = 0;
  4116. #ifdef HAVE_AES_CBC
  4117. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  4118. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  4119. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  4120. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  4121. };
  4122. const byte verify[] =
  4123. {
  4124. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  4125. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  4126. };
  4127. byte key[] = "0123456789abcdef "; /* align */
  4128. byte iv[] = "1234567890abcdef "; /* align */
  4129. #ifdef WOLFSSL_ASYNC_CRYPT
  4130. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0)
  4131. return -4200;
  4132. if (wc_AesInit(&dec, HEAP_HINT, devId) != 0)
  4133. return -4201;
  4134. #endif
  4135. ret = wc_AesSetKey(&enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  4136. if (ret != 0)
  4137. return -4202;
  4138. #ifdef HAVE_AES_DECRYPT
  4139. ret = wc_AesSetKey(&dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  4140. if (ret != 0)
  4141. return -4203;
  4142. #endif
  4143. ret = wc_AesCbcEncrypt(&enc, cipher, msg, AES_BLOCK_SIZE);
  4144. #if defined(WOLFSSL_ASYNC_CRYPT)
  4145. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4146. #endif
  4147. if (ret != 0)
  4148. return -4204;
  4149. #ifdef HAVE_AES_DECRYPT
  4150. ret = wc_AesCbcDecrypt(&dec, plain, cipher, AES_BLOCK_SIZE);
  4151. #if defined(WOLFSSL_ASYNC_CRYPT)
  4152. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  4153. #endif
  4154. if (ret != 0)
  4155. return -4205;
  4156. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  4157. return -4206;
  4158. #endif /* HAVE_AES_DECRYPT */
  4159. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  4160. return -4207;
  4161. #if defined(WOLFSSL_AESNI) && defined(HAVE_AES_DECRYPT)
  4162. {
  4163. const byte bigMsg[] = {
  4164. /* "All work and no play makes Jack a dull boy. " */
  4165. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  4166. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  4167. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  4168. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  4169. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  4170. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  4171. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  4172. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  4173. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  4174. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  4175. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  4176. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  4177. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  4178. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  4179. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  4180. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  4181. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  4182. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  4183. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  4184. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  4185. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  4186. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  4187. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  4188. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  4189. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  4190. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  4191. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  4192. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  4193. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  4194. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  4195. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  4196. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  4197. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  4198. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  4199. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  4200. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  4201. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20,
  4202. 0x61,0x20,0x64,0x75,0x6c,0x6c,0x20,0x62,
  4203. 0x6f,0x79,0x2e,0x20,0x41,0x6c,0x6c,0x20,
  4204. 0x77,0x6f,0x72,0x6b,0x20,0x61,0x6e,0x64,
  4205. 0x20,0x6e,0x6f,0x20,0x70,0x6c,0x61,0x79,
  4206. 0x20,0x6d,0x61,0x6b,0x65,0x73,0x20,0x4a,
  4207. 0x61,0x63,0x6b,0x20,0x61,0x20,0x64,0x75,
  4208. 0x6c,0x6c,0x20,0x62,0x6f,0x79,0x2e,0x20,
  4209. 0x41,0x6c,0x6c,0x20,0x77,0x6f,0x72,0x6b,
  4210. 0x20,0x61,0x6e,0x64,0x20,0x6e,0x6f,0x20,
  4211. 0x70,0x6c,0x61,0x79,0x20,0x6d,0x61,0x6b,
  4212. 0x65,0x73,0x20,0x4a,0x61,0x63,0x6b,0x20
  4213. };
  4214. const byte bigKey[] = "0123456789abcdeffedcba9876543210";
  4215. byte bigCipher[sizeof(bigMsg)];
  4216. byte bigPlain[sizeof(bigMsg)];
  4217. word32 keySz, msgSz;
  4218. /* Iterate from one AES_BLOCK_SIZE of bigMsg through the whole
  4219. * message by AES_BLOCK_SIZE for each size of AES key. */
  4220. for (keySz = 16; keySz <= 32; keySz += 8) {
  4221. for (msgSz = AES_BLOCK_SIZE;
  4222. msgSz <= sizeof(bigMsg);
  4223. msgSz += AES_BLOCK_SIZE) {
  4224. XMEMSET(bigCipher, 0, sizeof(bigCipher));
  4225. XMEMSET(bigPlain, 0, sizeof(bigPlain));
  4226. ret = wc_AesSetKey(&enc, bigKey, keySz, iv, AES_ENCRYPTION);
  4227. if (ret != 0)
  4228. return -4208;
  4229. ret = wc_AesSetKey(&dec, bigKey, keySz, iv, AES_DECRYPTION);
  4230. if (ret != 0)
  4231. return -4209;
  4232. ret = wc_AesCbcEncrypt(&enc, bigCipher, bigMsg, msgSz);
  4233. #if defined(WOLFSSL_ASYNC_CRYPT)
  4234. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4235. #endif
  4236. if (ret != 0)
  4237. return -4210;
  4238. ret = wc_AesCbcDecrypt(&dec, bigPlain, bigCipher, msgSz);
  4239. #if defined(WOLFSSL_ASYNC_CRYPT)
  4240. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  4241. #endif
  4242. if (ret != 0)
  4243. return -4211;
  4244. if (XMEMCMP(bigPlain, bigMsg, msgSz))
  4245. return -4212;
  4246. }
  4247. }
  4248. }
  4249. #endif /* WOLFSSL_AESNI HAVE_AES_DECRYPT */
  4250. #endif /* HAVE_AES_CBC */
  4251. #ifdef WOLFSSL_AES_COUNTER
  4252. {
  4253. /* test vectors from "Recommendation for Block Cipher Modes of
  4254. * Operation" NIST Special Publication 800-38A */
  4255. const byte ctrIv[] =
  4256. {
  4257. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  4258. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  4259. };
  4260. const byte ctrPlain[] =
  4261. {
  4262. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  4263. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  4264. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  4265. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  4266. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  4267. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  4268. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  4269. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  4270. };
  4271. const byte oddCipher[] =
  4272. {
  4273. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  4274. 0xc2
  4275. };
  4276. const byte ctr128Key[] =
  4277. {
  4278. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  4279. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  4280. };
  4281. const byte ctr128Cipher[] =
  4282. {
  4283. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  4284. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  4285. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  4286. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  4287. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  4288. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  4289. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  4290. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  4291. };
  4292. const byte ctr192Key[] =
  4293. {
  4294. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  4295. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  4296. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  4297. };
  4298. const byte ctr192Cipher[] =
  4299. {
  4300. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  4301. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b,
  4302. 0x09,0x03,0x39,0xec,0x0a,0xa6,0xfa,0xef,
  4303. 0xd5,0xcc,0xc2,0xc6,0xf4,0xce,0x8e,0x94,
  4304. 0x1e,0x36,0xb2,0x6b,0xd1,0xeb,0xc6,0x70,
  4305. 0xd1,0xbd,0x1d,0x66,0x56,0x20,0xab,0xf7,
  4306. 0x4f,0x78,0xa7,0xf6,0xd2,0x98,0x09,0x58,
  4307. 0x5a,0x97,0xda,0xec,0x58,0xc6,0xb0,0x50
  4308. };
  4309. const byte ctr256Key[] =
  4310. {
  4311. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  4312. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  4313. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  4314. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  4315. };
  4316. const byte ctr256Cipher[] =
  4317. {
  4318. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  4319. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28,
  4320. 0xf4,0x43,0xe3,0xca,0x4d,0x62,0xb5,0x9a,
  4321. 0xca,0x84,0xe9,0x90,0xca,0xca,0xf5,0xc5,
  4322. 0x2b,0x09,0x30,0xda,0xa2,0x3d,0xe9,0x4c,
  4323. 0xe8,0x70,0x17,0xba,0x2d,0x84,0x98,0x8d,
  4324. 0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
  4325. 0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
  4326. };
  4327. wc_AesSetKeyDirect(&enc, ctr128Key, sizeof(ctr128Key),
  4328. ctrIv, AES_ENCRYPTION);
  4329. /* Ctr only uses encrypt, even on key setup */
  4330. wc_AesSetKeyDirect(&dec, ctr128Key, sizeof(ctr128Key),
  4331. ctrIv, AES_ENCRYPTION);
  4332. ret = wc_AesCtrEncrypt(&enc, cipher, ctrPlain, sizeof(ctrPlain));
  4333. if (ret != 0) {
  4334. return -4227;
  4335. }
  4336. ret = wc_AesCtrEncrypt(&dec, plain, cipher, sizeof(ctrPlain));
  4337. if (ret != 0) {
  4338. return -4228;
  4339. }
  4340. if (XMEMCMP(plain, ctrPlain, sizeof(ctrPlain)))
  4341. return -4213;
  4342. if (XMEMCMP(cipher, ctr128Cipher, sizeof(ctr128Cipher)))
  4343. return -4214;
  4344. /* let's try with just 9 bytes, non block size test */
  4345. wc_AesSetKeyDirect(&enc, ctr128Key, AES_BLOCK_SIZE,
  4346. ctrIv, AES_ENCRYPTION);
  4347. /* Ctr only uses encrypt, even on key setup */
  4348. wc_AesSetKeyDirect(&dec, ctr128Key, AES_BLOCK_SIZE,
  4349. ctrIv, AES_ENCRYPTION);
  4350. ret = wc_AesCtrEncrypt(&enc, cipher, ctrPlain, sizeof(oddCipher));
  4351. if (ret != 0) {
  4352. return -4229;
  4353. }
  4354. ret = wc_AesCtrEncrypt(&dec, plain, cipher, sizeof(oddCipher));
  4355. if (ret != 0) {
  4356. return -4230;
  4357. }
  4358. if (XMEMCMP(plain, ctrPlain, sizeof(oddCipher)))
  4359. return -4215;
  4360. if (XMEMCMP(cipher, ctr128Cipher, sizeof(oddCipher)))
  4361. return -4216;
  4362. /* and an additional 9 bytes to reuse tmp left buffer */
  4363. ret = wc_AesCtrEncrypt(&enc, cipher, ctrPlain, sizeof(oddCipher));
  4364. if (ret != 0) {
  4365. return -4231;
  4366. }
  4367. ret = wc_AesCtrEncrypt(&dec, plain, cipher, sizeof(oddCipher));
  4368. if (ret != 0) {
  4369. return -4232;
  4370. }
  4371. if (XMEMCMP(plain, ctrPlain, sizeof(oddCipher)))
  4372. return -4217;
  4373. if (XMEMCMP(cipher, oddCipher, sizeof(oddCipher)))
  4374. return -4218;
  4375. /* 192 bit key */
  4376. wc_AesSetKeyDirect(&enc, ctr192Key, sizeof(ctr192Key),
  4377. ctrIv, AES_ENCRYPTION);
  4378. /* Ctr only uses encrypt, even on key setup */
  4379. wc_AesSetKeyDirect(&dec, ctr192Key, sizeof(ctr192Key),
  4380. ctrIv, AES_ENCRYPTION);
  4381. XMEMSET(plain, 0, sizeof(plain));
  4382. ret = wc_AesCtrEncrypt(&enc, plain, ctr192Cipher, sizeof(ctr192Cipher));
  4383. if (ret != 0) {
  4384. return -4233;
  4385. }
  4386. if (XMEMCMP(plain, ctrPlain, sizeof(ctr192Cipher)))
  4387. return -4219;
  4388. ret = wc_AesCtrEncrypt(&dec, cipher, ctrPlain, sizeof(ctrPlain));
  4389. if (ret != 0) {
  4390. return -4234;
  4391. }
  4392. if (XMEMCMP(ctr192Cipher, cipher, sizeof(ctr192Cipher)))
  4393. return -4220;
  4394. /* 256 bit key */
  4395. wc_AesSetKeyDirect(&enc, ctr256Key, sizeof(ctr256Key),
  4396. ctrIv, AES_ENCRYPTION);
  4397. /* Ctr only uses encrypt, even on key setup */
  4398. wc_AesSetKeyDirect(&dec, ctr256Key, sizeof(ctr256Key),
  4399. ctrIv, AES_ENCRYPTION);
  4400. XMEMSET(plain, 0, sizeof(plain));
  4401. ret = wc_AesCtrEncrypt(&enc, plain, ctr256Cipher, sizeof(ctr256Cipher));
  4402. if (ret != 0) {
  4403. return -4235;
  4404. }
  4405. if (XMEMCMP(plain, ctrPlain, sizeof(ctrPlain)))
  4406. return -4221;
  4407. ret = wc_AesCtrEncrypt(&dec, cipher, ctrPlain, sizeof(ctrPlain));
  4408. if (ret != 0) {
  4409. return -4236;
  4410. }
  4411. if (XMEMCMP(ctr256Cipher, cipher, sizeof(ctr256Cipher)))
  4412. return -4222;
  4413. }
  4414. #endif /* WOLFSSL_AES_COUNTER */
  4415. #ifdef WOLFSSL_AES_DIRECT
  4416. {
  4417. const byte niPlain[] =
  4418. {
  4419. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  4420. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  4421. };
  4422. const byte niCipher[] =
  4423. {
  4424. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  4425. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  4426. };
  4427. const byte niKey[] =
  4428. {
  4429. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  4430. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  4431. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  4432. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  4433. };
  4434. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  4435. ret = wc_AesSetKey(&enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  4436. if (ret != 0)
  4437. return -4223;
  4438. wc_AesEncryptDirect(&enc, cipher, niPlain);
  4439. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  4440. return -4224;
  4441. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  4442. ret = wc_AesSetKey(&dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  4443. if (ret != 0)
  4444. return -4225;
  4445. wc_AesDecryptDirect(&dec, plain, niCipher);
  4446. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  4447. return -4226;
  4448. }
  4449. #endif /* WOLFSSL_AES_DIRECT */
  4450. ret = aes_key_size_test();
  4451. if (ret != 0)
  4452. return ret;
  4453. #if defined(HAVE_AES_CBC)
  4454. ret = aes_cbc_test();
  4455. if (ret != 0)
  4456. return ret;
  4457. #endif
  4458. #if defined(WOLFSSL_AES_XTS)
  4459. ret = aes_xts_128_test();
  4460. if (ret != 0)
  4461. return ret;
  4462. ret = aes_xts_256_test();
  4463. if (ret != 0)
  4464. return ret;
  4465. ret = aes_xts_sector_test();
  4466. if (ret != 0)
  4467. return ret;
  4468. ret = aes_xts_args_test();
  4469. if (ret != 0)
  4470. return ret;
  4471. #endif
  4472. wc_AesFree(&enc);
  4473. #ifdef HAVE_AES_DECRYPT
  4474. wc_AesFree(&dec);
  4475. #endif
  4476. return ret;
  4477. }
  4478. int aes192_test(void)
  4479. {
  4480. #ifdef HAVE_AES_CBC
  4481. Aes enc;
  4482. byte cipher[AES_BLOCK_SIZE];
  4483. #ifdef HAVE_AES_DECRYPT
  4484. Aes dec;
  4485. byte plain[AES_BLOCK_SIZE];
  4486. #endif
  4487. #endif /* HAVE_AES_CBC */
  4488. int ret = 0;
  4489. #ifdef HAVE_AES_CBC
  4490. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  4491. * Appendix F.2.3 */
  4492. const byte msg[] = {
  4493. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  4494. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  4495. };
  4496. const byte verify[] =
  4497. {
  4498. 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  4499. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8
  4500. };
  4501. byte key[] = {
  4502. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  4503. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  4504. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  4505. };
  4506. byte iv[] = {
  4507. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  4508. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  4509. };
  4510. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0)
  4511. return -4230;
  4512. #ifdef HAVE_AES_DECRYPT
  4513. if (wc_AesInit(&dec, HEAP_HINT, devId) != 0)
  4514. return -4231;
  4515. #endif
  4516. ret = wc_AesSetKey(&enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  4517. if (ret != 0)
  4518. return -4232;
  4519. #ifdef HAVE_AES_DECRYPT
  4520. ret = wc_AesSetKey(&dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  4521. if (ret != 0)
  4522. return -4233;
  4523. #endif
  4524. ret = wc_AesCbcEncrypt(&enc, cipher, msg, (int) sizeof(msg));
  4525. #if defined(WOLFSSL_ASYNC_CRYPT)
  4526. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4527. #endif
  4528. if (ret != 0)
  4529. return -4234;
  4530. #ifdef HAVE_AES_DECRYPT
  4531. ret = wc_AesCbcDecrypt(&dec, plain, cipher, (int) sizeof(cipher));
  4532. #if defined(WOLFSSL_ASYNC_CRYPT)
  4533. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  4534. #endif
  4535. if (ret != 0)
  4536. return -4235;
  4537. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  4538. return -4236;
  4539. }
  4540. #endif
  4541. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  4542. return -4237;
  4543. wc_AesFree(&enc);
  4544. #ifdef HAVE_AES_DECRYPT
  4545. wc_AesFree(&dec);
  4546. #endif
  4547. #endif /* HAVE_AES_CBC */
  4548. return ret;
  4549. }
  4550. int aes256_test(void)
  4551. {
  4552. #ifdef HAVE_AES_CBC
  4553. Aes enc;
  4554. byte cipher[AES_BLOCK_SIZE];
  4555. #ifdef HAVE_AES_DECRYPT
  4556. Aes dec;
  4557. byte plain[AES_BLOCK_SIZE];
  4558. #endif
  4559. #endif /* HAVE_AES_CBC */
  4560. int ret = 0;
  4561. #ifdef HAVE_AES_CBC
  4562. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  4563. * Appendix F.2.5 */
  4564. const byte msg[] = {
  4565. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  4566. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  4567. };
  4568. const byte verify[] =
  4569. {
  4570. 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  4571. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6
  4572. };
  4573. byte key[] = {
  4574. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  4575. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  4576. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  4577. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  4578. };
  4579. byte iv[] = {
  4580. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  4581. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F
  4582. };
  4583. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0)
  4584. return -4240;
  4585. #ifdef HAVE_AES_DECRYPT
  4586. if (wc_AesInit(&dec, HEAP_HINT, devId) != 0)
  4587. return -4241;
  4588. #endif
  4589. ret = wc_AesSetKey(&enc, key, (int) sizeof(key), iv, AES_ENCRYPTION);
  4590. if (ret != 0)
  4591. return -4242;
  4592. #ifdef HAVE_AES_DECRYPT
  4593. ret = wc_AesSetKey(&dec, key, (int) sizeof(key), iv, AES_DECRYPTION);
  4594. if (ret != 0)
  4595. return -4243;
  4596. #endif
  4597. ret = wc_AesCbcEncrypt(&enc, cipher, msg, (int) sizeof(msg));
  4598. #if defined(WOLFSSL_ASYNC_CRYPT)
  4599. ret = wc_AsyncWait(ret, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4600. #endif
  4601. if (ret != 0)
  4602. return -4244;
  4603. #ifdef HAVE_AES_DECRYPT
  4604. ret = wc_AesCbcDecrypt(&dec, plain, cipher, (int) sizeof(cipher));
  4605. #if defined(WOLFSSL_ASYNC_CRYPT)
  4606. ret = wc_AsyncWait(ret, &dec.asyncDev, WC_ASYNC_FLAG_NONE);
  4607. #endif
  4608. if (ret != 0)
  4609. return -4245;
  4610. if (XMEMCMP(plain, msg, (int) sizeof(plain))) {
  4611. return -4246;
  4612. }
  4613. #endif
  4614. if (XMEMCMP(cipher, verify, (int) sizeof(cipher)))
  4615. return -4247;
  4616. wc_AesFree(&enc);
  4617. #ifdef HAVE_AES_DECRYPT
  4618. wc_AesFree(&dec);
  4619. #endif
  4620. #endif /* HAVE_AES_CBC */
  4621. return 0;
  4622. }
  4623. #ifdef HAVE_AESGCM
  4624. int aesgcm_test(void)
  4625. {
  4626. Aes enc;
  4627. /*
  4628. * This is Test Case 16 from the document Galois/
  4629. * Counter Mode of Operation (GCM) by McGrew and
  4630. * Viega.
  4631. */
  4632. const byte p[] =
  4633. {
  4634. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  4635. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  4636. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  4637. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  4638. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  4639. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  4640. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  4641. 0xba, 0x63, 0x7b, 0x39
  4642. };
  4643. const byte a[] =
  4644. {
  4645. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  4646. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  4647. 0xab, 0xad, 0xda, 0xd2
  4648. };
  4649. const byte k1[] =
  4650. {
  4651. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  4652. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  4653. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  4654. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  4655. };
  4656. const byte iv1[] =
  4657. {
  4658. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  4659. 0xde, 0xca, 0xf8, 0x88
  4660. };
  4661. const byte c1[] =
  4662. {
  4663. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  4664. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  4665. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  4666. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  4667. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  4668. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  4669. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  4670. 0xbc, 0xc9, 0xf6, 0x62
  4671. };
  4672. const byte t1[] =
  4673. {
  4674. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  4675. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  4676. };
  4677. /* FIPS, QAT and STM32F2/4 HW Crypto only support 12-byte IV */
  4678. #if !defined(HAVE_FIPS) && !defined(HAVE_INTEL_QA) && \
  4679. !defined(STM32_CRYPTO) && !defined(WOLFSSL_PIC32MZ_CRYPT) && \
  4680. !defined(WOLFSSL_XILINX_CRYPT)
  4681. #define ENABLE_NON_12BYTE_IV_TEST
  4682. /* Test Case 12, uses same plaintext and AAD data. */
  4683. const byte k2[] =
  4684. {
  4685. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  4686. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  4687. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c
  4688. };
  4689. const byte iv2[] =
  4690. {
  4691. 0x93, 0x13, 0x22, 0x5d, 0xf8, 0x84, 0x06, 0xe5,
  4692. 0x55, 0x90, 0x9c, 0x5a, 0xff, 0x52, 0x69, 0xaa,
  4693. 0x6a, 0x7a, 0x95, 0x38, 0x53, 0x4f, 0x7d, 0xa1,
  4694. 0xe4, 0xc3, 0x03, 0xd2, 0xa3, 0x18, 0xa7, 0x28,
  4695. 0xc3, 0xc0, 0xc9, 0x51, 0x56, 0x80, 0x95, 0x39,
  4696. 0xfc, 0xf0, 0xe2, 0x42, 0x9a, 0x6b, 0x52, 0x54,
  4697. 0x16, 0xae, 0xdb, 0xf5, 0xa0, 0xde, 0x6a, 0x57,
  4698. 0xa6, 0x37, 0xb3, 0x9b
  4699. };
  4700. const byte c2[] =
  4701. {
  4702. 0xd2, 0x7e, 0x88, 0x68, 0x1c, 0xe3, 0x24, 0x3c,
  4703. 0x48, 0x30, 0x16, 0x5a, 0x8f, 0xdc, 0xf9, 0xff,
  4704. 0x1d, 0xe9, 0xa1, 0xd8, 0xe6, 0xb4, 0x47, 0xef,
  4705. 0x6e, 0xf7, 0xb7, 0x98, 0x28, 0x66, 0x6e, 0x45,
  4706. 0x81, 0xe7, 0x90, 0x12, 0xaf, 0x34, 0xdd, 0xd9,
  4707. 0xe2, 0xf0, 0x37, 0x58, 0x9b, 0x29, 0x2d, 0xb3,
  4708. 0xe6, 0x7c, 0x03, 0x67, 0x45, 0xfa, 0x22, 0xe7,
  4709. 0xe9, 0xb7, 0x37, 0x3b
  4710. };
  4711. const byte t2[] =
  4712. {
  4713. 0xdc, 0xf5, 0x66, 0xff, 0x29, 0x1c, 0x25, 0xbb,
  4714. 0xb8, 0x56, 0x8f, 0xc3, 0xd3, 0x76, 0xa6, 0xd9
  4715. };
  4716. #endif
  4717. byte resultT[sizeof(t1)];
  4718. byte resultP[sizeof(p)];
  4719. byte resultC[sizeof(p)];
  4720. int result;
  4721. #if !defined(HAVE_FIPS) && !defined(STM32_CRYPTO)
  4722. int ivlen;
  4723. #endif
  4724. int alen, plen;
  4725. #if !defined(BENCH_EMBEDDED)
  4726. #ifndef BENCH_AESGCM_LARGE
  4727. #define BENCH_AESGCM_LARGE 1024
  4728. #endif
  4729. byte large_input[BENCH_AESGCM_LARGE];
  4730. byte large_output[BENCH_AESGCM_LARGE];
  4731. byte large_outdec[BENCH_AESGCM_LARGE];
  4732. XMEMSET(large_input, 0, sizeof(large_input));
  4733. XMEMSET(large_output, 0, sizeof(large_output));
  4734. XMEMSET(large_outdec, 0, sizeof(large_outdec));
  4735. #endif
  4736. XMEMSET(resultT, 0, sizeof(resultT));
  4737. XMEMSET(resultC, 0, sizeof(resultC));
  4738. XMEMSET(resultP, 0, sizeof(resultP));
  4739. if (wc_AesInit(&enc, HEAP_HINT, devId) != 0) {
  4740. return -4300;
  4741. }
  4742. result = wc_AesGcmSetKey(&enc, k1, sizeof(k1));
  4743. if (result != 0)
  4744. return -4301;
  4745. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  4746. result = wc_AesGcmEncrypt(&enc, resultC, p, sizeof(p), iv1, sizeof(iv1),
  4747. resultT, sizeof(resultT), a, sizeof(a));
  4748. #if defined(WOLFSSL_ASYNC_CRYPT)
  4749. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4750. #endif
  4751. if (result != 0)
  4752. return -4302;
  4753. if (XMEMCMP(c1, resultC, sizeof(resultC)))
  4754. return -4303;
  4755. if (XMEMCMP(t1, resultT, sizeof(resultT)))
  4756. return -4304;
  4757. result = wc_AesGcmDecrypt(&enc, resultP, resultC, sizeof(resultC),
  4758. iv1, sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  4759. #if defined(WOLFSSL_ASYNC_CRYPT)
  4760. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4761. #endif
  4762. if (result != 0)
  4763. return -4305;
  4764. if (XMEMCMP(p, resultP, sizeof(resultP)))
  4765. return -4306;
  4766. /* Large buffer test */
  4767. #ifdef BENCH_AESGCM_LARGE
  4768. /* setup test buffer */
  4769. for (alen=0; alen<BENCH_AESGCM_LARGE; alen++)
  4770. large_input[alen] = (byte)alen;
  4771. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  4772. result = wc_AesGcmEncrypt(&enc, large_output, large_input,
  4773. BENCH_AESGCM_LARGE, iv1, sizeof(iv1),
  4774. resultT, sizeof(resultT), a, sizeof(a));
  4775. #if defined(WOLFSSL_ASYNC_CRYPT)
  4776. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4777. #endif
  4778. if (result != 0)
  4779. return -4307;
  4780. result = wc_AesGcmDecrypt(&enc, large_outdec, large_output,
  4781. BENCH_AESGCM_LARGE, iv1, sizeof(iv1), resultT,
  4782. sizeof(resultT), a, sizeof(a));
  4783. #if defined(WOLFSSL_ASYNC_CRYPT)
  4784. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4785. #endif
  4786. if (result != 0)
  4787. return -4308;
  4788. if (XMEMCMP(large_input, large_outdec, BENCH_AESGCM_LARGE))
  4789. return -4309;
  4790. #endif /* BENCH_AESGCM_LARGE */
  4791. #if !defined(HAVE_FIPS) && !defined(STM32_CRYPTO)
  4792. /* Variable IV length test */
  4793. for (ivlen=0; ivlen<(int)sizeof(k1); ivlen++) {
  4794. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  4795. result = wc_AesGcmEncrypt(&enc, resultC, p, sizeof(p), k1,
  4796. (word32)ivlen, resultT, sizeof(resultT), a, sizeof(a));
  4797. #if defined(WOLFSSL_ASYNC_CRYPT)
  4798. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4799. #endif
  4800. if (result != 0)
  4801. return -4310;
  4802. result = wc_AesGcmDecrypt(&enc, resultP, resultC, sizeof(resultC), k1,
  4803. (word32)ivlen, resultT, sizeof(resultT), a, sizeof(a));
  4804. #if defined(WOLFSSL_ASYNC_CRYPT)
  4805. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4806. #endif
  4807. if (result != 0)
  4808. return -4311;
  4809. }
  4810. #endif
  4811. /* Variable authenticed data length test */
  4812. for (alen=0; alen<(int)sizeof(p); alen++) {
  4813. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  4814. result = wc_AesGcmEncrypt(&enc, resultC, p, sizeof(p), iv1,
  4815. sizeof(iv1), resultT, sizeof(resultT), p, (word32)alen);
  4816. #if defined(WOLFSSL_ASYNC_CRYPT)
  4817. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4818. #endif
  4819. if (result != 0)
  4820. return -4312;
  4821. result = wc_AesGcmDecrypt(&enc, resultP, resultC, sizeof(resultC), iv1,
  4822. sizeof(iv1), resultT, sizeof(resultT), p, (word32)alen);
  4823. #if defined(WOLFSSL_ASYNC_CRYPT)
  4824. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4825. #endif
  4826. if (result != 0)
  4827. return -4313;
  4828. }
  4829. /* Variable plain text length test */
  4830. for (plen=1; plen<(int)sizeof(p); plen++) {
  4831. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  4832. result = wc_AesGcmEncrypt(&enc, resultC, p, (word32)plen, iv1,
  4833. sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  4834. #if defined(WOLFSSL_ASYNC_CRYPT)
  4835. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4836. #endif
  4837. if (result != 0)
  4838. return -4314;
  4839. result = wc_AesGcmDecrypt(&enc, resultP, resultC, (word32)plen, iv1,
  4840. sizeof(iv1), resultT, sizeof(resultT), a, sizeof(a));
  4841. #if defined(WOLFSSL_ASYNC_CRYPT)
  4842. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4843. #endif
  4844. if (result != 0)
  4845. return -4315;
  4846. }
  4847. /* test with IV != 12 bytes */
  4848. #ifdef ENABLE_NON_12BYTE_IV_TEST
  4849. XMEMSET(resultT, 0, sizeof(resultT));
  4850. XMEMSET(resultC, 0, sizeof(resultC));
  4851. XMEMSET(resultP, 0, sizeof(resultP));
  4852. wc_AesGcmSetKey(&enc, k2, sizeof(k2));
  4853. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  4854. result = wc_AesGcmEncrypt(&enc, resultC, p, sizeof(p), iv2, sizeof(iv2),
  4855. resultT, sizeof(resultT), a, sizeof(a));
  4856. #if defined(WOLFSSL_ASYNC_CRYPT)
  4857. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4858. #endif
  4859. if (result != 0)
  4860. return -4316;
  4861. if (XMEMCMP(c2, resultC, sizeof(resultC)))
  4862. return -4317;
  4863. if (XMEMCMP(t2, resultT, sizeof(resultT)))
  4864. return -4318;
  4865. result = wc_AesGcmDecrypt(&enc, resultP, resultC, sizeof(resultC),
  4866. iv2, sizeof(iv2), resultT, sizeof(resultT), a, sizeof(a));
  4867. #if defined(WOLFSSL_ASYNC_CRYPT)
  4868. result = wc_AsyncWait(result, &enc.asyncDev, WC_ASYNC_FLAG_NONE);
  4869. #endif
  4870. if (result != 0)
  4871. return -4319;
  4872. if (XMEMCMP(p, resultP, sizeof(resultP)))
  4873. return -4320;
  4874. #endif /* ENABLE_NON_12BYTE_IV_TEST */
  4875. wc_AesFree(&enc);
  4876. return 0;
  4877. }
  4878. int gmac_test(void)
  4879. {
  4880. Gmac gmac;
  4881. const byte k1[] =
  4882. {
  4883. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  4884. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  4885. };
  4886. const byte iv1[] =
  4887. {
  4888. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  4889. 0xe2, 0x8c, 0x8f, 0x16
  4890. };
  4891. const byte a1[] =
  4892. {
  4893. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  4894. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  4895. };
  4896. const byte t1[] =
  4897. {
  4898. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  4899. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  4900. };
  4901. const byte k2[] =
  4902. {
  4903. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  4904. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  4905. };
  4906. const byte iv2[] =
  4907. {
  4908. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  4909. 0x1a, 0x60, 0x24, 0xa7
  4910. };
  4911. const byte a2[] =
  4912. {
  4913. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  4914. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  4915. };
  4916. const byte t2[] =
  4917. {
  4918. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  4919. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  4920. };
  4921. byte tag[16];
  4922. XMEMSET(&gmac, 0, sizeof(Gmac)); /* clear context */
  4923. XMEMSET(tag, 0, sizeof(tag));
  4924. wc_GmacSetKey(&gmac, k1, sizeof(k1));
  4925. wc_GmacUpdate(&gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  4926. if (XMEMCMP(t1, tag, sizeof(t1)) != 0)
  4927. return -4400;
  4928. XMEMSET(tag, 0, sizeof(tag));
  4929. wc_GmacSetKey(&gmac, k2, sizeof(k2));
  4930. wc_GmacUpdate(&gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  4931. if (XMEMCMP(t2, tag, sizeof(t2)) != 0)
  4932. return -4401;
  4933. return 0;
  4934. }
  4935. #endif /* HAVE_AESGCM */
  4936. #ifdef HAVE_AESCCM
  4937. int aesccm_test(void)
  4938. {
  4939. Aes enc;
  4940. /* key */
  4941. const byte k[] =
  4942. {
  4943. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  4944. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  4945. };
  4946. /* nonce */
  4947. const byte iv[] =
  4948. {
  4949. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  4950. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  4951. };
  4952. /* plaintext */
  4953. const byte p[] =
  4954. {
  4955. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  4956. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  4957. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  4958. };
  4959. const byte a[] =
  4960. {
  4961. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  4962. };
  4963. const byte c[] =
  4964. {
  4965. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  4966. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  4967. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  4968. };
  4969. const byte t[] =
  4970. {
  4971. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  4972. };
  4973. byte t2[sizeof(t)];
  4974. byte p2[sizeof(p)];
  4975. byte c2[sizeof(c)];
  4976. int result;
  4977. XMEMSET(&enc, 0, sizeof(Aes)); /* clear context */
  4978. XMEMSET(t2, 0, sizeof(t2));
  4979. XMEMSET(c2, 0, sizeof(c2));
  4980. XMEMSET(p2, 0, sizeof(p2));
  4981. result = wc_AesCcmSetKey(&enc, k, sizeof(k));
  4982. if (result != 0)
  4983. return -4500;
  4984. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  4985. result = wc_AesCcmEncrypt(&enc, c2, p, sizeof(c2), iv, sizeof(iv),
  4986. t2, sizeof(t2), a, sizeof(a));
  4987. if (result != 0)
  4988. return -4501;
  4989. if (XMEMCMP(c, c2, sizeof(c2)))
  4990. return -4502;
  4991. if (XMEMCMP(t, t2, sizeof(t2)))
  4992. return -4503;
  4993. result = wc_AesCcmDecrypt(&enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  4994. t2, sizeof(t2), a, sizeof(a));
  4995. if (result != 0)
  4996. return -4504;
  4997. if (XMEMCMP(p, p2, sizeof(p2)))
  4998. return -4505;
  4999. /* Test the authentication failure */
  5000. t2[0]++; /* Corrupt the authentication tag. */
  5001. result = wc_AesCcmDecrypt(&enc, p2, c, sizeof(p2), iv, sizeof(iv),
  5002. t2, sizeof(t2), a, sizeof(a));
  5003. if (result == 0)
  5004. return -4506;
  5005. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  5006. * authentication fail. */
  5007. XMEMSET(c2, 0, sizeof(c2));
  5008. if (XMEMCMP(p2, c2, sizeof(p2)))
  5009. return -4507;
  5010. return 0;
  5011. }
  5012. #endif /* HAVE_AESCCM */
  5013. #ifdef HAVE_AES_KEYWRAP
  5014. #define MAX_KEYWRAP_TEST_OUTLEN 40
  5015. #define MAX_KEYWRAP_TEST_PLAINLEN 32
  5016. typedef struct keywrapVector {
  5017. const byte* kek;
  5018. const byte* data;
  5019. const byte* verify;
  5020. word32 kekLen;
  5021. word32 dataLen;
  5022. word32 verifyLen;
  5023. } keywrapVector;
  5024. int aeskeywrap_test(void)
  5025. {
  5026. int wrapSz, plainSz, testSz, i;
  5027. /* test vectors from RFC 3394 (kek, data, verify) */
  5028. /* Wrap 128 bits of Key Data with a 128-bit KEK */
  5029. const byte k1[] = {
  5030. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  5031. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  5032. };
  5033. const byte d1[] = {
  5034. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  5035. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  5036. };
  5037. const byte v1[] = {
  5038. 0x1F, 0xA6, 0x8B, 0x0A, 0x81, 0x12, 0xB4, 0x47,
  5039. 0xAE, 0xF3, 0x4B, 0xD8, 0xFB, 0x5A, 0x7B, 0x82,
  5040. 0x9D, 0x3E, 0x86, 0x23, 0x71, 0xD2, 0xCF, 0xE5
  5041. };
  5042. /* Wrap 128 bits of Key Data with a 192-bit KEK */
  5043. const byte k2[] = {
  5044. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  5045. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  5046. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  5047. };
  5048. const byte d2[] = {
  5049. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  5050. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  5051. };
  5052. const byte v2[] = {
  5053. 0x96, 0x77, 0x8B, 0x25, 0xAE, 0x6C, 0xA4, 0x35,
  5054. 0xF9, 0x2B, 0x5B, 0x97, 0xC0, 0x50, 0xAE, 0xD2,
  5055. 0x46, 0x8A, 0xB8, 0xA1, 0x7A, 0xD8, 0x4E, 0x5D
  5056. };
  5057. /* Wrap 128 bits of Key Data with a 256-bit KEK */
  5058. const byte k3[] = {
  5059. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  5060. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  5061. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  5062. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  5063. };
  5064. const byte d3[] = {
  5065. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  5066. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
  5067. };
  5068. const byte v3[] = {
  5069. 0x64, 0xE8, 0xC3, 0xF9, 0xCE, 0x0F, 0x5B, 0xA2,
  5070. 0x63, 0xE9, 0x77, 0x79, 0x05, 0x81, 0x8A, 0x2A,
  5071. 0x93, 0xC8, 0x19, 0x1E, 0x7D, 0x6E, 0x8A, 0xE7
  5072. };
  5073. /* Wrap 192 bits of Key Data with a 192-bit KEK */
  5074. const byte k4[] = {
  5075. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  5076. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  5077. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
  5078. };
  5079. const byte d4[] = {
  5080. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  5081. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  5082. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  5083. };
  5084. const byte v4[] = {
  5085. 0x03, 0x1D, 0x33, 0x26, 0x4E, 0x15, 0xD3, 0x32,
  5086. 0x68, 0xF2, 0x4E, 0xC2, 0x60, 0x74, 0x3E, 0xDC,
  5087. 0xE1, 0xC6, 0xC7, 0xDD, 0xEE, 0x72, 0x5A, 0x93,
  5088. 0x6B, 0xA8, 0x14, 0x91, 0x5C, 0x67, 0x62, 0xD2
  5089. };
  5090. /* Wrap 192 bits of Key Data with a 256-bit KEK */
  5091. const byte k5[] = {
  5092. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  5093. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  5094. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  5095. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  5096. };
  5097. const byte d5[] = {
  5098. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  5099. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  5100. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  5101. };
  5102. const byte v5[] = {
  5103. 0xA8, 0xF9, 0xBC, 0x16, 0x12, 0xC6, 0x8B, 0x3F,
  5104. 0xF6, 0xE6, 0xF4, 0xFB, 0xE3, 0x0E, 0x71, 0xE4,
  5105. 0x76, 0x9C, 0x8B, 0x80, 0xA3, 0x2C, 0xB8, 0x95,
  5106. 0x8C, 0xD5, 0xD1, 0x7D, 0x6B, 0x25, 0x4D, 0xA1
  5107. };
  5108. /* Wrap 256 bits of Key Data with a 256-bit KEK */
  5109. const byte k6[] = {
  5110. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  5111. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  5112. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  5113. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
  5114. };
  5115. const byte d6[] = {
  5116. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  5117. 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
  5118. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  5119. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  5120. };
  5121. const byte v6[] = {
  5122. 0x28, 0xC9, 0xF4, 0x04, 0xC4, 0xB8, 0x10, 0xF4,
  5123. 0xCB, 0xCC, 0xB3, 0x5C, 0xFB, 0x87, 0xF8, 0x26,
  5124. 0x3F, 0x57, 0x86, 0xE2, 0xD8, 0x0E, 0xD3, 0x26,
  5125. 0xCB, 0xC7, 0xF0, 0xE7, 0x1A, 0x99, 0xF4, 0x3B,
  5126. 0xFB, 0x98, 0x8B, 0x9B, 0x7A, 0x02, 0xDD, 0x21
  5127. };
  5128. byte output[MAX_KEYWRAP_TEST_OUTLEN];
  5129. byte plain [MAX_KEYWRAP_TEST_PLAINLEN];
  5130. const keywrapVector test_wrap[] =
  5131. {
  5132. {k1, d1, v1, sizeof(k1), sizeof(d1), sizeof(v1)},
  5133. {k2, d2, v2, sizeof(k2), sizeof(d2), sizeof(v2)},
  5134. {k3, d3, v3, sizeof(k3), sizeof(d3), sizeof(v3)},
  5135. {k4, d4, v4, sizeof(k4), sizeof(d4), sizeof(v4)},
  5136. {k5, d5, v5, sizeof(k5), sizeof(d5), sizeof(v5)},
  5137. {k6, d6, v6, sizeof(k6), sizeof(d6), sizeof(v6)}
  5138. };
  5139. testSz = sizeof(test_wrap) / sizeof(keywrapVector);
  5140. XMEMSET(output, 0, sizeof(output));
  5141. XMEMSET(plain, 0, sizeof(plain));
  5142. for (i = 0; i < testSz; i++) {
  5143. wrapSz = wc_AesKeyWrap(test_wrap[i].kek, test_wrap[i].kekLen,
  5144. test_wrap[i].data, test_wrap[i].dataLen,
  5145. output, sizeof(output), NULL);
  5146. if ( (wrapSz < 0) || (wrapSz != (int)test_wrap[i].verifyLen) )
  5147. return -4600;
  5148. if (XMEMCMP(output, test_wrap[i].verify, test_wrap[i].verifyLen) != 0)
  5149. return -4601;
  5150. plainSz = wc_AesKeyUnWrap((byte*)test_wrap[i].kek, test_wrap[i].kekLen,
  5151. output, wrapSz,
  5152. plain, sizeof(plain), NULL);
  5153. if ( (plainSz < 0) || (plainSz != (int)test_wrap[i].dataLen) )
  5154. return -4602;
  5155. if (XMEMCMP(plain, test_wrap[i].data, test_wrap[i].dataLen) != 0)
  5156. return -4610 - i;
  5157. }
  5158. return 0;
  5159. }
  5160. #endif /* HAVE_AES_KEYWRAP */
  5161. #endif /* NO_AES */
  5162. #ifdef HAVE_CAMELLIA
  5163. enum {
  5164. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  5165. };
  5166. typedef struct {
  5167. int type;
  5168. const byte* plaintext;
  5169. const byte* iv;
  5170. const byte* ciphertext;
  5171. const byte* key;
  5172. word32 keySz;
  5173. int errorCode;
  5174. } test_vector_t;
  5175. int camellia_test(void)
  5176. {
  5177. /* Camellia ECB Test Plaintext */
  5178. static const byte pte[] =
  5179. {
  5180. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  5181. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  5182. };
  5183. /* Camellia ECB Test Initialization Vector */
  5184. static const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  5185. /* Test 1: Camellia ECB 128-bit key */
  5186. static const byte k1[] =
  5187. {
  5188. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  5189. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  5190. };
  5191. static const byte c1[] =
  5192. {
  5193. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  5194. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  5195. };
  5196. /* Test 2: Camellia ECB 192-bit key */
  5197. static const byte k2[] =
  5198. {
  5199. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  5200. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  5201. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  5202. };
  5203. static const byte c2[] =
  5204. {
  5205. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  5206. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  5207. };
  5208. /* Test 3: Camellia ECB 256-bit key */
  5209. static const byte k3[] =
  5210. {
  5211. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  5212. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  5213. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  5214. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  5215. };
  5216. static const byte c3[] =
  5217. {
  5218. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  5219. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  5220. };
  5221. /* Camellia CBC Test Plaintext */
  5222. static const byte ptc[] =
  5223. {
  5224. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  5225. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  5226. };
  5227. /* Camellia CBC Test Initialization Vector */
  5228. static const byte ivc[] =
  5229. {
  5230. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  5231. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  5232. };
  5233. /* Test 4: Camellia-CBC 128-bit key */
  5234. static const byte k4[] =
  5235. {
  5236. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  5237. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  5238. };
  5239. static const byte c4[] =
  5240. {
  5241. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  5242. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  5243. };
  5244. /* Test 5: Camellia-CBC 192-bit key */
  5245. static const byte k5[] =
  5246. {
  5247. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  5248. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  5249. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  5250. };
  5251. static const byte c5[] =
  5252. {
  5253. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  5254. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  5255. };
  5256. /* Test 6: CBC 256-bit key */
  5257. static const byte k6[] =
  5258. {
  5259. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  5260. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  5261. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  5262. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  5263. };
  5264. static const byte c6[] =
  5265. {
  5266. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  5267. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  5268. };
  5269. byte out[CAMELLIA_BLOCK_SIZE];
  5270. Camellia cam;
  5271. int i, testsSz, ret;
  5272. const test_vector_t testVectors[] =
  5273. {
  5274. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  5275. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  5276. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  5277. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  5278. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  5279. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  5280. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  5281. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  5282. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  5283. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  5284. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  5285. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  5286. };
  5287. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  5288. for (i = 0; i < testsSz; i++) {
  5289. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  5290. testVectors[i].iv) != 0)
  5291. return testVectors[i].errorCode;
  5292. switch (testVectors[i].type) {
  5293. case CAM_ECB_ENC:
  5294. ret = wc_CamelliaEncryptDirect(&cam, out,
  5295. testVectors[i].plaintext);
  5296. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  5297. CAMELLIA_BLOCK_SIZE))
  5298. return testVectors[i].errorCode;
  5299. break;
  5300. case CAM_ECB_DEC:
  5301. ret = wc_CamelliaDecryptDirect(&cam, out,
  5302. testVectors[i].ciphertext);
  5303. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  5304. CAMELLIA_BLOCK_SIZE))
  5305. return testVectors[i].errorCode;
  5306. break;
  5307. case CAM_CBC_ENC:
  5308. ret = wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  5309. CAMELLIA_BLOCK_SIZE);
  5310. if (ret != 0 || XMEMCMP(out, testVectors[i].ciphertext,
  5311. CAMELLIA_BLOCK_SIZE))
  5312. return testVectors[i].errorCode;
  5313. break;
  5314. case CAM_CBC_DEC:
  5315. ret = wc_CamelliaCbcDecrypt(&cam, out,
  5316. testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE);
  5317. if (ret != 0 || XMEMCMP(out, testVectors[i].plaintext,
  5318. CAMELLIA_BLOCK_SIZE))
  5319. return testVectors[i].errorCode;
  5320. break;
  5321. default:
  5322. break;
  5323. }
  5324. }
  5325. /* Setting the IV and checking it was actually set. */
  5326. ret = wc_CamelliaSetIV(&cam, ivc);
  5327. if (ret != 0 || XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  5328. return -4700;
  5329. /* Setting the IV to NULL should be same as all zeros IV */
  5330. if (wc_CamelliaSetIV(&cam, NULL) != 0 ||
  5331. XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
  5332. return -4701;
  5333. /* First parameter should never be null */
  5334. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  5335. return -4702;
  5336. /* First parameter should never be null, check it fails */
  5337. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  5338. return -4703;
  5339. /* Key should have a size of 16, 24, or 32 */
  5340. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  5341. return -4704;
  5342. return 0;
  5343. }
  5344. #endif /* HAVE_CAMELLIA */
  5345. #ifdef HAVE_IDEA
  5346. int idea_test(void)
  5347. {
  5348. int ret;
  5349. word16 i, j;
  5350. Idea idea;
  5351. byte data[IDEA_BLOCK_SIZE];
  5352. /* Project NESSIE test vectors */
  5353. #define IDEA_NB_TESTS 6
  5354. #define IDEA_NB_TESTS_EXTRA 4
  5355. const byte v_key[IDEA_NB_TESTS][IDEA_KEY_SIZE] = {
  5356. { 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
  5357. 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37 },
  5358. { 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57,
  5359. 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57 },
  5360. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  5361. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
  5362. { 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  5363. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 },
  5364. { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  5365. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
  5366. { 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
  5367. 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48 },
  5368. };
  5369. const byte v1_plain[IDEA_NB_TESTS][IDEA_BLOCK_SIZE] = {
  5370. { 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37 },
  5371. { 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57, 0x57 },
  5372. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
  5373. { 0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 },
  5374. { 0xDB, 0x2D, 0x4A, 0x92, 0xAA, 0x68, 0x27, 0x3F },
  5375. { 0xF1, 0x29, 0xA6, 0x60, 0x1E, 0xF6, 0x2A, 0x47 },
  5376. };
  5377. byte v1_cipher[IDEA_NB_TESTS][IDEA_BLOCK_SIZE] = {
  5378. { 0x54, 0xCF, 0x21, 0xE3, 0x89, 0xD8, 0x73, 0xEC },
  5379. { 0x85, 0x52, 0x4D, 0x41, 0x0E, 0xB4, 0x28, 0xAE },
  5380. { 0xF5, 0x26, 0xAB, 0x9A, 0x62, 0xC0, 0xD2, 0x58 },
  5381. { 0xC8, 0xFB, 0x51, 0xD3, 0x51, 0x66, 0x27, 0xA8 },
  5382. { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 },
  5383. { 0xEA, 0x02, 0x47, 0x14, 0xAD, 0x5C, 0x4D, 0x84 },
  5384. };
  5385. byte v1_cipher_100[IDEA_NB_TESTS_EXTRA][IDEA_BLOCK_SIZE] = {
  5386. { 0x12, 0x46, 0x2F, 0xD0, 0xFB, 0x3A, 0x63, 0x39 },
  5387. { 0x15, 0x61, 0xE8, 0xC9, 0x04, 0x54, 0x8B, 0xE9 },
  5388. { 0x42, 0x12, 0x2A, 0x94, 0xB0, 0xF6, 0xD2, 0x43 },
  5389. { 0x53, 0x4D, 0xCD, 0x48, 0xDD, 0xD5, 0xF5, 0x9C },
  5390. };
  5391. byte v1_cipher_1000[IDEA_NB_TESTS_EXTRA][IDEA_BLOCK_SIZE] = {
  5392. { 0x44, 0x1B, 0x38, 0x5C, 0x77, 0x29, 0x75, 0x34 },
  5393. { 0xF0, 0x4E, 0x58, 0x88, 0x44, 0x99, 0x22, 0x2D },
  5394. { 0xB3, 0x5F, 0x93, 0x7F, 0x6A, 0xA0, 0xCD, 0x1F },
  5395. { 0x9A, 0xEA, 0x46, 0x8F, 0x42, 0x9B, 0xBA, 0x15 },
  5396. };
  5397. /* CBC test */
  5398. const char *message = "International Data Encryption Algorithm";
  5399. byte msg_enc[40], msg_dec[40];
  5400. for (i = 0; i < IDEA_NB_TESTS; i++) {
  5401. /* Set encryption key */
  5402. XMEMSET(&idea, 0, sizeof(Idea));
  5403. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  5404. NULL, IDEA_ENCRYPTION);
  5405. if (ret != 0) {
  5406. printf("wc_IdeaSetKey (enc) failed\n");
  5407. return -4800;
  5408. }
  5409. /* Data encryption */
  5410. ret = wc_IdeaCipher(&idea, data, v1_plain[i]);
  5411. if (ret != 0 || XMEMCMP(&v1_cipher[i], data, IDEA_BLOCK_SIZE)) {
  5412. printf("Bad encryption\n");
  5413. return -4801;
  5414. }
  5415. /* Set decryption key */
  5416. XMEMSET(&idea, 0, sizeof(Idea));
  5417. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  5418. NULL, IDEA_DECRYPTION);
  5419. if (ret != 0) {
  5420. printf("wc_IdeaSetKey (dec) failed\n");
  5421. return -4802;
  5422. }
  5423. /* Data decryption */
  5424. ret = wc_IdeaCipher(&idea, data, data);
  5425. if (ret != 0 || XMEMCMP(v1_plain[i], data, IDEA_BLOCK_SIZE)) {
  5426. printf("Bad decryption\n");
  5427. return -4803;
  5428. }
  5429. /* Set encryption key */
  5430. XMEMSET(&idea, 0, sizeof(Idea));
  5431. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  5432. v_key[i], IDEA_ENCRYPTION);
  5433. if (ret != 0) {
  5434. printf("wc_IdeaSetKey (enc) failed\n");
  5435. return -4804;
  5436. }
  5437. XMEMSET(msg_enc, 0, sizeof(msg_enc));
  5438. ret = wc_IdeaCbcEncrypt(&idea, msg_enc, (byte *)message,
  5439. (word32)XSTRLEN(message)+1);
  5440. if (ret != 0) {
  5441. printf("wc_IdeaCbcEncrypt failed\n");
  5442. return -4805;
  5443. }
  5444. /* Set decryption key */
  5445. XMEMSET(&idea, 0, sizeof(Idea));
  5446. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  5447. v_key[i], IDEA_DECRYPTION);
  5448. if (ret != 0) {
  5449. printf("wc_IdeaSetKey (dec) failed\n");
  5450. return -4806;
  5451. }
  5452. XMEMSET(msg_dec, 0, sizeof(msg_dec));
  5453. ret = wc_IdeaCbcDecrypt(&idea, msg_dec, msg_enc,
  5454. (word32)XSTRLEN(message)+1);
  5455. if (ret != 0) {
  5456. printf("wc_IdeaCbcDecrypt failed\n");
  5457. return -4807;
  5458. }
  5459. if (XMEMCMP(message, msg_dec, (word32)XSTRLEN(message))) {
  5460. printf("Bad CBC decryption\n");
  5461. return -4808;
  5462. }
  5463. }
  5464. for (i = 0; i < IDEA_NB_TESTS_EXTRA; i++) {
  5465. /* Set encryption key */
  5466. XMEMSET(&idea, 0, sizeof(Idea));
  5467. ret = wc_IdeaSetKey(&idea, v_key[i], IDEA_KEY_SIZE,
  5468. NULL, IDEA_ENCRYPTION);
  5469. if (ret != 0) {
  5470. printf("wc_IdeaSetKey (enc) failed\n");
  5471. return -4809;
  5472. }
  5473. /* 100 times data encryption */
  5474. XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE);
  5475. for (j = 0; j < 100; j++) {
  5476. ret = wc_IdeaCipher(&idea, data, data);
  5477. if (ret != 0) {
  5478. return -4821;
  5479. }
  5480. }
  5481. if (XMEMCMP(v1_cipher_100[i], data, IDEA_BLOCK_SIZE)) {
  5482. printf("Bad encryption (100 times)\n");
  5483. return -4810;
  5484. }
  5485. /* 1000 times data encryption */
  5486. XMEMCPY(data, v1_plain[i], IDEA_BLOCK_SIZE);
  5487. for (j = 0; j < 1000; j++) {
  5488. ret = wc_IdeaCipher(&idea, data, data);
  5489. if (ret != 0) {
  5490. return -4822;
  5491. }
  5492. }
  5493. if (XMEMCMP(v1_cipher_1000[i], data, IDEA_BLOCK_SIZE)) {
  5494. printf("Bad encryption (100 times)\n");
  5495. return -4811;
  5496. }
  5497. }
  5498. #ifndef WC_NO_RNG
  5499. /* random test for CBC */
  5500. {
  5501. WC_RNG rng;
  5502. byte key[IDEA_KEY_SIZE], iv[IDEA_BLOCK_SIZE],
  5503. rnd[1000], enc[1000], dec[1000];
  5504. /* random values */
  5505. #ifndef HAVE_FIPS
  5506. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  5507. #else
  5508. ret = wc_InitRng(&rng);
  5509. #endif
  5510. if (ret != 0)
  5511. return -4812;
  5512. for (i = 0; i < 1000; i++) {
  5513. /* random key */
  5514. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  5515. if (ret != 0)
  5516. return -4813;
  5517. /* random iv */
  5518. ret = wc_RNG_GenerateBlock(&rng, iv, sizeof(iv));
  5519. if (ret != 0)
  5520. return -4814;
  5521. /* random data */
  5522. ret = wc_RNG_GenerateBlock(&rng, rnd, sizeof(rnd));
  5523. if (ret != 0)
  5524. return -4815;
  5525. /* Set encryption key */
  5526. XMEMSET(&idea, 0, sizeof(Idea));
  5527. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, iv, IDEA_ENCRYPTION);
  5528. if (ret != 0) {
  5529. printf("wc_IdeaSetKey (enc) failed\n");
  5530. return -4816;
  5531. }
  5532. /* Data encryption */
  5533. XMEMSET(enc, 0, sizeof(enc));
  5534. ret = wc_IdeaCbcEncrypt(&idea, enc, rnd, sizeof(rnd));
  5535. if (ret != 0) {
  5536. printf("wc_IdeaCbcEncrypt failed\n");
  5537. return -4817;
  5538. }
  5539. /* Set decryption key */
  5540. XMEMSET(&idea, 0, sizeof(Idea));
  5541. ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, iv, IDEA_DECRYPTION);
  5542. if (ret != 0) {
  5543. printf("wc_IdeaSetKey (enc) failed\n");
  5544. return -4818;
  5545. }
  5546. /* Data decryption */
  5547. XMEMSET(dec, 0, sizeof(dec));
  5548. ret = wc_IdeaCbcDecrypt(&idea, dec, enc, sizeof(enc));
  5549. if (ret != 0) {
  5550. printf("wc_IdeaCbcDecrypt failed\n");
  5551. return -4819;
  5552. }
  5553. if (XMEMCMP(rnd, dec, sizeof(rnd))) {
  5554. printf("Bad CBC decryption\n");
  5555. return -4820;
  5556. }
  5557. }
  5558. wc_FreeRng(&rng);
  5559. }
  5560. #endif /* WC_NO_RNG */
  5561. return 0;
  5562. }
  5563. #endif /* HAVE_IDEA */
  5564. #ifndef WC_NO_RNG
  5565. static int random_rng_test(void)
  5566. {
  5567. WC_RNG rng;
  5568. byte block[32];
  5569. int ret, i;
  5570. #ifndef HAVE_FIPS
  5571. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  5572. #else
  5573. ret = wc_InitRng(&rng);
  5574. #endif
  5575. if (ret != 0) return -4900;
  5576. XMEMSET(block, 0, sizeof(block));
  5577. ret = wc_RNG_GenerateBlock(&rng, block, sizeof(block));
  5578. if (ret != 0) {
  5579. ret = -4901;
  5580. goto exit;
  5581. }
  5582. /* Check for 0's */
  5583. for (i=0; i<(int)sizeof(block); i++) {
  5584. if (block[i] == 0) {
  5585. ret++;
  5586. }
  5587. }
  5588. /* All zeros count check */
  5589. if (ret >= (int)sizeof(block)) {
  5590. ret = -4902;
  5591. goto exit;
  5592. }
  5593. ret = wc_RNG_GenerateByte(&rng, block);
  5594. if (ret != 0) {
  5595. ret = -4903;
  5596. goto exit;
  5597. }
  5598. exit:
  5599. /* Make sure and free RNG */
  5600. wc_FreeRng(&rng);
  5601. return ret;
  5602. }
  5603. #if defined(HAVE_HASHDRBG) && !defined(CUSTOM_RAND_GENERATE_BLOCK)
  5604. int random_test(void)
  5605. {
  5606. const byte test1Entropy[] =
  5607. {
  5608. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  5609. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  5610. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  5611. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  5612. };
  5613. const byte test1Output[] =
  5614. {
  5615. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  5616. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  5617. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  5618. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  5619. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  5620. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  5621. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  5622. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  5623. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  5624. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  5625. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  5626. };
  5627. const byte test2EntropyA[] =
  5628. {
  5629. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  5630. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  5631. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  5632. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  5633. };
  5634. const byte test2EntropyB[] =
  5635. {
  5636. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  5637. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  5638. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  5639. };
  5640. const byte test2Output[] =
  5641. {
  5642. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  5643. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  5644. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  5645. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  5646. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  5647. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  5648. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  5649. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  5650. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  5651. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  5652. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  5653. };
  5654. byte output[WC_SHA256_DIGEST_SIZE * 4];
  5655. int ret;
  5656. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  5657. output, sizeof(output));
  5658. if (ret != 0)
  5659. return -5000;
  5660. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  5661. return -5001;
  5662. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  5663. test2EntropyB, sizeof(test2EntropyB),
  5664. output, sizeof(output));
  5665. if (ret != 0)
  5666. return -5002;
  5667. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  5668. return -5003;
  5669. /* Basic RNG generate block test */
  5670. if (random_rng_test() != 0)
  5671. return -5004;
  5672. return 0;
  5673. }
  5674. #else
  5675. int random_test(void)
  5676. {
  5677. /* Basic RNG generate block test */
  5678. return random_rng_test();
  5679. }
  5680. #endif /* HAVE_HASHDRBG && !CUSTOM_RAND_GENERATE_BLOCK */
  5681. #endif /* WC_NO_RNG */
  5682. #ifdef WOLFSSL_STATIC_MEMORY
  5683. int memory_test(void)
  5684. {
  5685. int ret = 0;
  5686. unsigned int i;
  5687. word32 size[] = { WOLFMEM_BUCKETS };
  5688. word32 dist[] = { WOLFMEM_DIST };
  5689. byte buffer[30000]; /* make large enough to involve many bucket sizes */
  5690. int pad = -(int)((wolfssl_word)&(buffer[0])) & (WOLFSSL_STATIC_ALIGN - 1);
  5691. /* pad to account for if head of buffer is not at set memory
  5692. * alignment when tests are ran */
  5693. /* check macro settings */
  5694. if (sizeof(size)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  5695. return -5100;
  5696. }
  5697. if (sizeof(dist)/sizeof(word32) != WOLFMEM_MAX_BUCKETS) {
  5698. return -5101;
  5699. }
  5700. for (i = 0; i < WOLFMEM_MAX_BUCKETS; i++) {
  5701. if ((size[i] % WOLFSSL_STATIC_ALIGN) != 0) {
  5702. /* each element in array should be divisable by alignment size */
  5703. return -5102;
  5704. }
  5705. }
  5706. for (i = 1; i < WOLFMEM_MAX_BUCKETS; i++) {
  5707. if (size[i - 1] >= size[i]) {
  5708. return -5103; /* sizes should be in increasing order */
  5709. }
  5710. }
  5711. /* check that padding size returned is possible */
  5712. if (wolfSSL_MemoryPaddingSz() < WOLFSSL_STATIC_ALIGN) {
  5713. return -5104; /* no room for wc_Memory struct */
  5714. }
  5715. if (wolfSSL_MemoryPaddingSz() < 0) {
  5716. return -5105;
  5717. }
  5718. if (wolfSSL_MemoryPaddingSz() % WOLFSSL_STATIC_ALIGN != 0) {
  5719. return -5106; /* not aligned! */
  5720. }
  5721. /* check function to return optimum buffer size (rounded down) */
  5722. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_GENERAL);
  5723. if ((ret - pad) % WOLFSSL_STATIC_ALIGN != 0) {
  5724. return -5107; /* not aligned! */
  5725. }
  5726. if (ret < 0) {
  5727. return -5108;
  5728. }
  5729. if ((unsigned int)ret > sizeof(buffer)) {
  5730. return -5109; /* did not round down as expected */
  5731. }
  5732. if (ret != wolfSSL_StaticBufferSz(buffer, ret, WOLFMEM_GENERAL)) {
  5733. return -5110; /* retrun value changed when using suggested value */
  5734. }
  5735. ret = wolfSSL_MemoryPaddingSz();
  5736. ret += pad; /* add space that is going to be needed if buffer not aligned */
  5737. if (wolfSSL_StaticBufferSz(buffer, size[0] + ret + 1, WOLFMEM_GENERAL) !=
  5738. (ret + (int)size[0])) {
  5739. return -5111; /* did not round down to nearest bucket value */
  5740. }
  5741. ret = wolfSSL_StaticBufferSz(buffer, sizeof(buffer), WOLFMEM_IO_POOL);
  5742. if ((ret - pad) < 0) {
  5743. return -5112;
  5744. }
  5745. if (((ret - pad) % (WOLFMEM_IO_SZ + wolfSSL_MemoryPaddingSz())) != 0) {
  5746. return -5113; /* not even chunks of memory for IO size */
  5747. }
  5748. if (((ret - pad) % WOLFSSL_STATIC_ALIGN) != 0) {
  5749. return -5114; /* memory not aligned */
  5750. }
  5751. /* check for passing bad or unknown argments to functions */
  5752. if (wolfSSL_StaticBufferSz(NULL, 1, WOLFMEM_GENERAL) > 0) {
  5753. return -5115;
  5754. }
  5755. if (wolfSSL_StaticBufferSz(buffer, 1, WOLFMEM_GENERAL) != 0) {
  5756. return -5116; /* should round to 0 since struct + bucket will not fit */
  5757. }
  5758. (void)dist; /* avoid static analysis warning of variable not used */
  5759. return 0;
  5760. }
  5761. #endif /* WOLFSSL_STATIC_MEMORY */
  5762. #ifdef HAVE_NTRU
  5763. byte GetEntropy(ENTROPY_CMD cmd, byte* out);
  5764. byte GetEntropy(ENTROPY_CMD cmd, byte* out)
  5765. {
  5766. static WC_RNG rng;
  5767. if (cmd == INIT)
  5768. return (wc_InitRng(&rng) == 0) ? 1 : 0;
  5769. if (out == NULL)
  5770. return 0;
  5771. if (cmd == GET_BYTE_OF_ENTROPY)
  5772. return (wc_RNG_GenerateBlock(&rng, out, 1) == 0) ? 1 : 0;
  5773. if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
  5774. *out = 1;
  5775. return 1;
  5776. }
  5777. return 0;
  5778. }
  5779. #endif /* HAVE_NTRU */
  5780. #ifndef NO_FILESYSTEM
  5781. /* Cert Paths */
  5782. #ifdef FREESCALE_MQX
  5783. #define CERT_PREFIX "a:\\"
  5784. #define CERT_PATH_SEP "\\"
  5785. #elif defined(WOLFSSL_MKD_SHELL)
  5786. #define CERT_PREFIX ""
  5787. #define CERT_PATH_SEP "/"
  5788. #elif defined(WOLFSSL_uTKERNEL2)
  5789. #define CERT_PREFIX "/uda/"
  5790. #define CERT_PATH_SEP "/"
  5791. #else
  5792. #define CERT_PREFIX "./"
  5793. #define CERT_PATH_SEP "/"
  5794. #endif
  5795. #define CERT_ROOT CERT_PREFIX "certs" CERT_PATH_SEP
  5796. /* Generated Test Certs */
  5797. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  5798. !defined(NO_ASN)
  5799. #ifndef NO_RSA
  5800. static const char* clientKey = CERT_ROOT "client-key.der";
  5801. static const char* clientCert = CERT_ROOT "client-cert.der";
  5802. #ifdef WOLFSSL_CERT_EXT
  5803. static const char* clientKeyPub = CERT_ROOT "client-keyPub.der";
  5804. #endif
  5805. #ifdef WOLFSSL_CERT_GEN
  5806. static const char* rsaCaKeyFile = CERT_ROOT "ca-key.der";
  5807. static const char* rsaCaCertFile = CERT_ROOT "ca-cert.pem";
  5808. #endif
  5809. #endif /* !NO_RSA */
  5810. #ifndef NO_DH
  5811. static const char* dhKey = CERT_ROOT "dh2048.der";
  5812. #endif
  5813. #ifndef NO_DSA
  5814. static const char* dsaKey = CERT_ROOT "dsa2048.der";
  5815. #endif
  5816. #endif /* !USE_CERT_BUFFER_* */
  5817. #if !defined(USE_CERT_BUFFERS_256) && !defined(NO_ASN)
  5818. #ifdef HAVE_ECC
  5819. /* cert files to be used in rsa cert gen test, check if RSA enabled */
  5820. #ifdef HAVE_ECC_KEY_IMPORT
  5821. static const char* eccKeyDerFile = CERT_ROOT "ecc-key.der";
  5822. #endif
  5823. #ifdef WOLFSSL_CERT_GEN
  5824. static const char* eccKeyPubFile = CERT_ROOT "ecc-keyPub.der";
  5825. static const char* eccCaKeyFile = CERT_ROOT "ca-ecc-key.der";
  5826. static const char* eccCaCertFile = CERT_ROOT "ca-ecc-cert.pem";
  5827. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  5828. static const char* eccCaKey384File =
  5829. CERT_ROOT "ca-ecc384-key.der";
  5830. static const char* eccCaCert384File =
  5831. CERT_ROOT "ca-ecc384-cert.pem";
  5832. #endif
  5833. #endif
  5834. #if defined(HAVE_PKCS7) && defined(HAVE_ECC)
  5835. static const char* eccClientKey = CERT_ROOT "ecc-client-key.der";
  5836. static const char* eccClientCert = CERT_ROOT "client-ecc-cert.der";
  5837. #endif
  5838. #endif /* HAVE_ECC */
  5839. #ifdef HAVE_ED25519
  5840. #ifdef WOLFSSL_TEST_CERT
  5841. static const char* serverEd25519Cert =
  5842. CERT_ROOT "ed25519/server-ed25519.der";
  5843. static const char* caEd25519Cert =
  5844. CERT_ROOT "ed25519/ca-ed25519.der";
  5845. #endif
  5846. #endif
  5847. #endif /* !USE_CERT_BUFFER_* */
  5848. #ifndef NO_WRITE_TEMP_FILES
  5849. #ifdef HAVE_ECC
  5850. #ifdef WOLFSSL_CERT_GEN
  5851. static const char* certEccPemFile = CERT_PREFIX "certecc.pem";
  5852. #endif
  5853. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  5854. static const char* certEccRsaPemFile = CERT_PREFIX "certeccrsa.pem";
  5855. static const char* certEccRsaDerFile = CERT_PREFIX "certeccrsa.der";
  5856. #endif
  5857. #ifdef WOLFSSL_KEY_GEN
  5858. static const char* eccCaKeyPemFile = CERT_PREFIX "ecc-key.pem";
  5859. static const char* eccPubKeyDerFile = CERT_PREFIX "ecc-public-key.der";
  5860. static const char* eccCaKeyTempFile = CERT_PREFIX "ecc-key.der";
  5861. #endif
  5862. #if defined(WOLFSSL_CERT_GEN) || \
  5863. (defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT))
  5864. static const char* certEccDerFile = CERT_PREFIX "certecc.der";
  5865. #endif
  5866. #endif /* HAVE_ECC */
  5867. #ifndef NO_RSA
  5868. #if defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_CERT_EXT)
  5869. static const char* otherCertDerFile = CERT_PREFIX "othercert.der";
  5870. static const char* certDerFile = CERT_PREFIX "cert.der";
  5871. #endif
  5872. #ifdef WOLFSSL_CERT_GEN
  5873. static const char* otherCertPemFile = CERT_PREFIX "othercert.pem";
  5874. static const char* certPemFile = CERT_PREFIX "cert.pem";
  5875. #endif
  5876. #ifdef WOLFSSL_CERT_REQ
  5877. static const char* certReqDerFile = CERT_PREFIX "certreq.der";
  5878. static const char* certReqPemFile = CERT_PREFIX "certreq.pem";
  5879. #endif
  5880. #endif /* !NO_RSA */
  5881. #if !defined(NO_RSA) || !defined(NO_DSA)
  5882. #ifdef WOLFSSL_KEY_GEN
  5883. static const char* keyDerFile = CERT_PREFIX "key.der";
  5884. static const char* keyPemFile = CERT_PREFIX "key.pem";
  5885. #endif
  5886. #endif
  5887. #endif /* !NO_WRITE_TEMP_FILES */
  5888. #endif /* !NO_FILESYSTEM */
  5889. #ifdef WOLFSSL_CERT_GEN
  5890. static const CertName certDefaultName = {
  5891. "US", CTC_PRINTABLE, /* country */
  5892. "Orgeon", CTC_UTF8, /* state */
  5893. "Portland", CTC_UTF8, /* locality */
  5894. "Test", CTC_UTF8, /* sur */
  5895. "wolfSSL", CTC_UTF8, /* org */
  5896. "Development", CTC_UTF8, /* unit */
  5897. "www.wolfssl.com", CTC_UTF8, /* commonName */
  5898. "info@wolfssl.com" /* email */
  5899. };
  5900. #ifdef WOLFSSL_CERT_EXT
  5901. static const char certKeyUsage[] =
  5902. "digitalSignature,nonRepudiation";
  5903. #if defined(WOLFSSL_CERT_REQ) || defined(HAVE_NTRU)
  5904. static const char certKeyUsage2[] =
  5905. "digitalSignature,nonRepudiation,keyEncipherment,keyAgreement";
  5906. #endif
  5907. #endif /* WOLFSSL_CERT_EXT */
  5908. #endif /* WOLFSSL_CERT_GEN */
  5909. #ifndef NO_RSA
  5910. #if !defined(NO_ASN_TIME) && defined(WOLFSSL_TEST_CERT)
  5911. int cert_test(void)
  5912. {
  5913. DecodedCert cert;
  5914. byte* tmp;
  5915. size_t bytes;
  5916. FILE *file;
  5917. int ret;
  5918. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5919. if (tmp == NULL)
  5920. return -5200;
  5921. /* Certificate with Name Constraints extension. */
  5922. #ifdef FREESCALE_MQX
  5923. file = fopen(".\\certs\\test\\cert-ext-nc.der", "rb");
  5924. #else
  5925. file = fopen("./certs/test/cert-ext-nc.der", "rb");
  5926. #endif
  5927. if (!file) {
  5928. ERROR_OUT(-5201, done);
  5929. }
  5930. bytes = fread(tmp, 1, FOURK_BUF, file);
  5931. fclose(file);
  5932. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  5933. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  5934. if (ret != 0) {
  5935. ERROR_OUT(-5202, done);
  5936. }
  5937. FreeDecodedCert(&cert);
  5938. /* Certificate with Inhibit Any Policy extension. */
  5939. #ifdef FREESCALE_MQX
  5940. file = fopen(".\\certs\\test\\cert-ext-ia.der", "rb");
  5941. #else
  5942. file = fopen("./certs/test/cert-ext-ia.der", "rb");
  5943. #endif
  5944. if (!file) {
  5945. ERROR_OUT(-5203, done);
  5946. }
  5947. bytes = fread(tmp, 1, FOURK_BUF, file);
  5948. fclose(file);
  5949. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  5950. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, NULL);
  5951. if (ret != 0) {
  5952. ERROR_OUT(-5204, done);
  5953. }
  5954. done:
  5955. FreeDecodedCert(&cert);
  5956. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5957. return ret;
  5958. }
  5959. #endif
  5960. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT)
  5961. int certext_test(void)
  5962. {
  5963. DecodedCert cert;
  5964. byte* tmp;
  5965. size_t bytes;
  5966. FILE *file;
  5967. int ret;
  5968. /* created from rsa_test : othercert.der */
  5969. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  5970. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  5971. /* created from rsa_test : othercert.der */
  5972. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  5973. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  5974. #ifdef HAVE_ECC
  5975. /* created from ecc_test_cert_gen : certecc.der */
  5976. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  5977. /* Authority key id from ./certs/ca-ecc384-cert.pem */
  5978. byte akid_ecc[] = "\xAB\xE0\xC3\x26\x4C\x18\xD4\x72\xBB\xD2"
  5979. "\x84\x8C\x9C\x0A\x05\x92\x80\x12\x53\x52";
  5980. #else
  5981. /* Authority key id from ./certs/ca-ecc-cert.pem */
  5982. byte akid_ecc[] = "\x56\x8E\x9A\xC3\xF0\x42\xDE\x18\xB9\x45"
  5983. "\x55\x6E\xF9\x93\xCF\xEA\xC3\xF3\xA5\x21";
  5984. #endif
  5985. #endif /* HAVE_ECC */
  5986. /* created from rsa_test : cert.der */
  5987. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  5988. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  5989. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5990. if (tmp == NULL)
  5991. return -5300;
  5992. /* load othercert.der (Cert signed by an authority) */
  5993. file = fopen(otherCertDerFile, "rb");
  5994. if (!file) {
  5995. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  5996. return -5301;
  5997. }
  5998. bytes = fread(tmp, 1, FOURK_BUF, file);
  5999. fclose(file);
  6000. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  6001. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  6002. if (ret != 0)
  6003. return -5302;
  6004. /* check the SKID from a RSA certificate */
  6005. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  6006. return -5303;
  6007. /* check the AKID from an RSA certificate */
  6008. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  6009. return -5304;
  6010. /* check the Key Usage from an RSA certificate */
  6011. if (!cert.extKeyUsageSet)
  6012. return -5305;
  6013. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  6014. return -5306;
  6015. /* check the CA Basic Constraints from an RSA certificate */
  6016. if (cert.isCA)
  6017. return -5307;
  6018. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  6019. /* check the Certificate Policies Id */
  6020. if (cert.extCertPoliciesNb != 1)
  6021. return -5308;
  6022. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  6023. return -5309;
  6024. #endif
  6025. FreeDecodedCert(&cert);
  6026. #ifdef HAVE_ECC
  6027. /* load certecc.der (Cert signed by our ECC CA test in ecc_test_cert_gen) */
  6028. file = fopen(certEccDerFile, "rb");
  6029. if (!file) {
  6030. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  6031. return -5310;
  6032. }
  6033. bytes = fread(tmp, 1, FOURK_BUF, file);
  6034. fclose(file);
  6035. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  6036. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  6037. if (ret != 0)
  6038. return -5311;
  6039. /* check the SKID from a ECC certificate - generated dynamically */
  6040. /* check the AKID from an ECC certificate */
  6041. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  6042. return -5313;
  6043. /* check the Key Usage from an ECC certificate */
  6044. if (!cert.extKeyUsageSet)
  6045. return -5314;
  6046. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  6047. return -5315;
  6048. /* check the CA Basic Constraints from an ECC certificate */
  6049. if (cert.isCA)
  6050. return -5316;
  6051. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  6052. /* check the Certificate Policies Id */
  6053. if (cert.extCertPoliciesNb != 2)
  6054. return -5317;
  6055. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  6056. return -5318;
  6057. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  6058. return -5319;
  6059. #endif
  6060. FreeDecodedCert(&cert);
  6061. #endif /* HAVE_ECC */
  6062. /* load cert.der (self signed certificate) */
  6063. file = fopen(certDerFile, "rb");
  6064. if (!file) {
  6065. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  6066. return -5320;
  6067. }
  6068. bytes = fread(tmp, 1, FOURK_BUF, file);
  6069. fclose(file);
  6070. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  6071. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  6072. if (ret != 0)
  6073. return -5321;
  6074. /* check the SKID from a CA certificate */
  6075. if (XMEMCMP(kid_ca, cert.extSubjKeyId, sizeof(cert.extSubjKeyId)))
  6076. return -5322;
  6077. /* check the AKID from an CA certificate */
  6078. if (XMEMCMP(kid_ca, cert.extAuthKeyId, sizeof(cert.extAuthKeyId)))
  6079. return -5323;
  6080. /* check the Key Usage from CA certificate */
  6081. if (!cert.extKeyUsageSet)
  6082. return -5324;
  6083. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  6084. return -5325;
  6085. /* check the CA Basic Constraints CA certificate */
  6086. if (!cert.isCA)
  6087. return -5326;
  6088. #ifndef WOLFSSL_SEP /* test only if not using SEP policies */
  6089. /* check the Certificate Policies Id */
  6090. if (cert.extCertPoliciesNb != 2)
  6091. return -5327;
  6092. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  6093. return -5328;
  6094. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  6095. return -5329;
  6096. #endif
  6097. FreeDecodedCert(&cert);
  6098. XFREE(tmp, HEAP_HINT ,DYNAMIC_TYPE_TMP_BUFFER);
  6099. return 0;
  6100. }
  6101. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_TEST_CERT */
  6102. static int rsa_flatten_test(RsaKey* key)
  6103. {
  6104. int ret;
  6105. byte e[256];
  6106. byte n[256];
  6107. word32 eSz = sizeof(e);
  6108. word32 nSz = sizeof(n);
  6109. /* Parameter Validation testing. */
  6110. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  6111. #ifdef HAVE_USER_RSA
  6112. /* Implementation using IPP Libraries returns:
  6113. * -101 = USER_CRYPTO_ERROR
  6114. */
  6115. if (ret == 0)
  6116. #else
  6117. if (ret != BAD_FUNC_ARG)
  6118. #endif
  6119. return -5330;
  6120. ret = wc_RsaFlattenPublicKey(key, NULL, &eSz, n, &nSz);
  6121. #ifdef HAVE_USER_RSA
  6122. /* Implementation using IPP Libraries returns:
  6123. * -101 = USER_CRYPTO_ERROR
  6124. */
  6125. if (ret == 0)
  6126. #else
  6127. if (ret != BAD_FUNC_ARG)
  6128. #endif
  6129. return -5331;
  6130. ret = wc_RsaFlattenPublicKey(key, e, NULL, n, &nSz);
  6131. #ifdef HAVE_USER_RSA
  6132. /* Implementation using IPP Libraries returns:
  6133. * -101 = USER_CRYPTO_ERROR
  6134. */
  6135. if (ret == 0)
  6136. #else
  6137. if (ret != BAD_FUNC_ARG)
  6138. #endif
  6139. return -5332;
  6140. ret = wc_RsaFlattenPublicKey(key, e, &eSz, NULL, &nSz);
  6141. #ifdef HAVE_USER_RSA
  6142. /* Implementation using IPP Libraries returns:
  6143. * -101 = USER_CRYPTO_ERROR
  6144. */
  6145. if (ret == 0)
  6146. #else
  6147. if (ret != BAD_FUNC_ARG)
  6148. #endif
  6149. return -5333;
  6150. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, NULL);
  6151. #ifdef HAVE_USER_RSA
  6152. /* Implementation using IPP Libraries returns:
  6153. * -101 = USER_CRYPTO_ERROR
  6154. */
  6155. if (ret == 0)
  6156. #else
  6157. if (ret != BAD_FUNC_ARG)
  6158. #endif
  6159. return -5334;
  6160. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  6161. if (ret != 0)
  6162. return -5335;
  6163. eSz = 0;
  6164. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  6165. #ifdef HAVE_USER_RSA
  6166. /* Implementation using IPP Libraries returns:
  6167. * -101 = USER_CRYPTO_ERROR
  6168. */
  6169. if (ret == 0)
  6170. #elif defined(HAVE_FIPS)
  6171. if (ret != 0)
  6172. #else
  6173. if (ret != RSA_BUFFER_E)
  6174. #endif
  6175. return -5336;
  6176. eSz = sizeof(e);
  6177. nSz = 0;
  6178. ret = wc_RsaFlattenPublicKey(key, e, &eSz, n, &nSz);
  6179. #ifdef HAVE_USER_RSA
  6180. /* Implementation using IPP Libraries returns:
  6181. * -101 = USER_CRYPTO_ERROR
  6182. */
  6183. if (ret == 0)
  6184. #else
  6185. if (ret != RSA_BUFFER_E)
  6186. #endif
  6187. return -5337;
  6188. return 0;
  6189. }
  6190. #ifndef NO_SIG_WRAPPER
  6191. static int rsa_sig_test(RsaKey* key, word32 keyLen, int modLen, WC_RNG* rng)
  6192. {
  6193. int ret;
  6194. word32 sigSz;
  6195. byte in[] = "Everyone gets Friday off.";
  6196. word32 inLen = (word32)XSTRLEN((char*)in);
  6197. byte out[256];
  6198. /* Parameter Validation testing. */
  6199. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_NONE, key, keyLen);
  6200. if (ret != BAD_FUNC_ARG)
  6201. return -5338;
  6202. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, 0);
  6203. if (ret != BAD_FUNC_ARG)
  6204. return -5339;
  6205. sigSz = (word32)modLen;
  6206. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  6207. inLen, out, &sigSz, key, keyLen, rng);
  6208. if (ret != BAD_FUNC_ARG)
  6209. return -5340;
  6210. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  6211. 0, out, &sigSz, key, keyLen, rng);
  6212. if (ret != BAD_FUNC_ARG)
  6213. return -5341;
  6214. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  6215. inLen, NULL, &sigSz, key, keyLen, rng);
  6216. if (ret != BAD_FUNC_ARG)
  6217. return -5342;
  6218. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  6219. inLen, out, NULL, key, keyLen, rng);
  6220. if (ret != BAD_FUNC_ARG)
  6221. return -5343;
  6222. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  6223. inLen, out, &sigSz, NULL, keyLen, rng);
  6224. if (ret != BAD_FUNC_ARG)
  6225. return -5344;
  6226. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  6227. inLen, out, &sigSz, key, 0, rng);
  6228. if (ret != BAD_FUNC_ARG)
  6229. return -5345;
  6230. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  6231. inLen, out, &sigSz, key, keyLen, NULL);
  6232. #ifdef HAVE_USER_RSA
  6233. /* Implementation using IPP Libraries returns:
  6234. * -101 = USER_CRYPTO_ERROR
  6235. */
  6236. if (ret == 0)
  6237. #elif defined(WOLFSSL_ASYNC_CRYPT)
  6238. /* async may not require RNG */
  6239. if (ret != 0 && ret != MISSING_RNG_E)
  6240. #elif defined(HAVE_FIPS) || defined(WOLFSSL_ASYNC_CRYPT) || \
  6241. !defined(WC_RSA_BLINDING)
  6242. /* FIPS140 implementation does not do blinding */
  6243. if (ret != 0)
  6244. #else
  6245. if (ret != MISSING_RNG_E)
  6246. #endif
  6247. return -5346;
  6248. sigSz = 0;
  6249. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  6250. inLen, out, &sigSz, key, keyLen, rng);
  6251. if (ret != BAD_FUNC_ARG)
  6252. return -5347;
  6253. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, NULL,
  6254. inLen, out, (word32)modLen, key, keyLen);
  6255. if (ret != BAD_FUNC_ARG)
  6256. return -5348;
  6257. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  6258. 0, out, (word32)modLen, key, keyLen);
  6259. if (ret != BAD_FUNC_ARG)
  6260. return -5349;
  6261. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  6262. inLen, NULL, (word32)modLen, key, keyLen);
  6263. if (ret != BAD_FUNC_ARG)
  6264. return -5350;
  6265. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  6266. inLen, out, 0, key, keyLen);
  6267. if (ret != BAD_FUNC_ARG)
  6268. return -5351;
  6269. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  6270. inLen, out, (word32)modLen, NULL, keyLen);
  6271. if (ret != BAD_FUNC_ARG)
  6272. return -5352;
  6273. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  6274. inLen, out, (word32)modLen, key, 0);
  6275. if (ret != BAD_FUNC_ARG)
  6276. return -5353;
  6277. #ifndef HAVE_ECC
  6278. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, keyLen);
  6279. if (ret != SIG_TYPE_E)
  6280. return -5354;
  6281. #endif
  6282. /* Use APIs. */
  6283. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA, key, keyLen);
  6284. if (ret != modLen)
  6285. return -5355;
  6286. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_RSA_W_ENC, key, keyLen);
  6287. if (ret != modLen)
  6288. return -5356;
  6289. sigSz = (word32)ret;
  6290. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  6291. inLen, out, &sigSz, key, keyLen, rng);
  6292. if (ret != 0)
  6293. return -5357;
  6294. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  6295. inLen, out, (word32)modLen, key, keyLen);
  6296. if (ret != 0)
  6297. return -5358;
  6298. sigSz = sizeof(out);
  6299. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  6300. in, inLen, out, &sigSz, key, keyLen, rng);
  6301. if (ret != 0)
  6302. return -5359;
  6303. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA_W_ENC,
  6304. in, inLen, out, (word32)modLen, key, keyLen);
  6305. if (ret != 0)
  6306. return -5360;
  6307. /* Wrong signature type. */
  6308. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, in,
  6309. inLen, out, (word32)modLen, key, keyLen);
  6310. if (ret == 0)
  6311. return -5361;
  6312. return 0;
  6313. }
  6314. #endif /* !NO_SIG_WRAPPER */
  6315. #ifndef HAVE_USER_RSA
  6316. static int rsa_decode_test(void)
  6317. {
  6318. int ret;
  6319. word32 inSz;
  6320. word32 inOutIdx;
  6321. RsaKey keyPub;
  6322. const byte n[2] = { 0x00, 0x23 };
  6323. const byte e[2] = { 0x00, 0x03 };
  6324. const byte good[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  6325. const byte goodAlgId[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  6326. 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  6327. const byte goodAlgIdNull[] = { 0x30, 0x11, 0x30, 0x0f, 0x06, 0x00,
  6328. 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  6329. 0x02, 0x1, 0x03 };
  6330. const byte badAlgIdNull[] = { 0x30, 0x12, 0x30, 0x10, 0x06, 0x00,
  6331. 0x05, 0x01, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23,
  6332. 0x02, 0x1, 0x03 };
  6333. const byte badNotBitString[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  6334. 0x04, 0x09, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  6335. const byte badBitStringLen[] = { 0x30, 0x0f, 0x30, 0x0d, 0x06, 0x00,
  6336. 0x03, 0x0a, 0x00, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  6337. const byte badNoSeq[] = { 0x30, 0x0d, 0x30, 0x0b, 0x06, 0x00, 0x03, 0x07,
  6338. 0x00, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  6339. const byte badNoObj[] = {
  6340. 0x30, 0x0f, 0x30, 0x0d, 0x05, 0x00, 0x03, 0x09, 0x00, 0x30, 0x06,
  6341. 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  6342. const byte badIntN[] = { 0x30, 0x06, 0x02, 0x05, 0x23, 0x02, 0x1, 0x03 };
  6343. const byte badNotIntE[] = { 0x30, 0x06, 0x02, 0x01, 0x23, 0x04, 0x1, 0x03 };
  6344. const byte badLength[] = { 0x30, 0x04, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  6345. const byte badBitStrNoZero[] = { 0x30, 0x0e, 0x30, 0x0c, 0x06, 0x00,
  6346. 0x03, 0x08, 0x30, 0x06, 0x02, 0x01, 0x23, 0x02, 0x1, 0x03 };
  6347. ret = wc_InitRsaKey(&keyPub, NULL);
  6348. if (ret != 0)
  6349. return -5400;
  6350. /* Parameter Validation testing. */
  6351. ret = wc_RsaPublicKeyDecodeRaw(NULL, sizeof(n), e, sizeof(e), &keyPub);
  6352. if (ret != BAD_FUNC_ARG) {
  6353. ret = -5401;
  6354. goto done;
  6355. }
  6356. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), NULL, sizeof(e), &keyPub);
  6357. if (ret != BAD_FUNC_ARG) {
  6358. ret = -5402;
  6359. goto done;
  6360. }
  6361. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), NULL);
  6362. if (ret != BAD_FUNC_ARG) {
  6363. ret = -5403;
  6364. goto done;
  6365. }
  6366. /* TODO: probably should fail when length is -1! */
  6367. ret = wc_RsaPublicKeyDecodeRaw(n, (word32)-1, e, sizeof(e), &keyPub);
  6368. if (ret != 0) {
  6369. ret = -5404;
  6370. goto done;
  6371. }
  6372. wc_FreeRsaKey(&keyPub);
  6373. ret = wc_InitRsaKey(&keyPub, NULL);
  6374. if (ret != 0)
  6375. return -5405;
  6376. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, (word32)-1, &keyPub);
  6377. if (ret != 0) {
  6378. ret = -5406;
  6379. goto done;
  6380. }
  6381. wc_FreeRsaKey(&keyPub);
  6382. ret = wc_InitRsaKey(&keyPub, NULL);
  6383. if (ret != 0)
  6384. return -5407;
  6385. /* Use API. */
  6386. ret = wc_RsaPublicKeyDecodeRaw(n, sizeof(n), e, sizeof(e), &keyPub);
  6387. if (ret != 0) {
  6388. ret = -5408;
  6389. goto done;
  6390. }
  6391. wc_FreeRsaKey(&keyPub);
  6392. ret = wc_InitRsaKey(&keyPub, NULL);
  6393. if (ret != 0)
  6394. return -5409;
  6395. /* Parameter Validation testing. */
  6396. inSz = sizeof(good);
  6397. ret = wc_RsaPublicKeyDecode(NULL, &inOutIdx, &keyPub, inSz);
  6398. if (ret != BAD_FUNC_ARG) {
  6399. ret = -5410;
  6400. goto done;
  6401. }
  6402. ret = wc_RsaPublicKeyDecode(good, NULL, &keyPub, inSz);
  6403. if (ret != BAD_FUNC_ARG) {
  6404. ret = -5411;
  6405. goto done;
  6406. }
  6407. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  6408. if (ret != BAD_FUNC_ARG) {
  6409. ret = -5412;
  6410. goto done;
  6411. }
  6412. /* Use good data and offest to bad data. */
  6413. inOutIdx = 2;
  6414. inSz = sizeof(good) - inOutIdx;
  6415. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, &keyPub, inSz);
  6416. if (ret != ASN_PARSE_E) {
  6417. ret = -5413;
  6418. goto done;
  6419. }
  6420. inOutIdx = 2;
  6421. inSz = sizeof(goodAlgId) - inOutIdx;
  6422. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, &keyPub, inSz);
  6423. if (ret != ASN_PARSE_E) {
  6424. ret = -5414;
  6425. goto done;
  6426. }
  6427. /* Try different bad data. */
  6428. inSz = sizeof(badAlgIdNull);
  6429. inOutIdx = 0;
  6430. ret = wc_RsaPublicKeyDecode(badAlgIdNull, &inOutIdx, &keyPub, inSz);
  6431. if (ret != ASN_EXPECT_0_E) {
  6432. ret = -5415;
  6433. goto done;
  6434. }
  6435. inSz = sizeof(badNotBitString);
  6436. inOutIdx = 0;
  6437. ret = wc_RsaPublicKeyDecode(badNotBitString, &inOutIdx, &keyPub, inSz);
  6438. if (ret != ASN_BITSTR_E) {
  6439. ret = -5416;
  6440. goto done;
  6441. }
  6442. inSz = sizeof(badBitStringLen);
  6443. inOutIdx = 0;
  6444. ret = wc_RsaPublicKeyDecode(badBitStringLen, &inOutIdx, &keyPub, inSz);
  6445. if (ret != ASN_PARSE_E) {
  6446. ret = -5417;
  6447. goto done;
  6448. }
  6449. inSz = sizeof(badNoSeq);
  6450. inOutIdx = 0;
  6451. ret = wc_RsaPublicKeyDecode(badNoSeq, &inOutIdx, &keyPub, inSz);
  6452. if (ret != ASN_PARSE_E) {
  6453. ret = -5418;
  6454. goto done;
  6455. }
  6456. inSz = sizeof(badNoObj);
  6457. inOutIdx = 0;
  6458. ret = wc_RsaPublicKeyDecode(badNoObj, &inOutIdx, &keyPub, inSz);
  6459. if (ret != ASN_PARSE_E) {
  6460. ret = -5419;
  6461. goto done;
  6462. }
  6463. inSz = sizeof(badIntN);
  6464. inOutIdx = 0;
  6465. ret = wc_RsaPublicKeyDecode(badIntN, &inOutIdx, &keyPub, inSz);
  6466. if (ret != ASN_RSA_KEY_E) {
  6467. ret = -5420;
  6468. goto done;
  6469. }
  6470. inSz = sizeof(badNotIntE);
  6471. inOutIdx = 0;
  6472. ret = wc_RsaPublicKeyDecode(badNotIntE, &inOutIdx, &keyPub, inSz);
  6473. if (ret != ASN_RSA_KEY_E) {
  6474. ret = -5421;
  6475. goto done;
  6476. }
  6477. /* TODO: Shouldn't pass as the sequence length is too small. */
  6478. inSz = sizeof(badLength);
  6479. inOutIdx = 0;
  6480. ret = wc_RsaPublicKeyDecode(badLength, &inOutIdx, &keyPub, inSz);
  6481. if (ret != 0) {
  6482. ret = -5422;
  6483. goto done;
  6484. }
  6485. /* TODO: Shouldn't ignore object id's data. */
  6486. wc_FreeRsaKey(&keyPub);
  6487. ret = wc_InitRsaKey(&keyPub, NULL);
  6488. if (ret != 0)
  6489. return -5423;
  6490. /* Valid data cases. */
  6491. inSz = sizeof(good);
  6492. inOutIdx = 0;
  6493. ret = wc_RsaPublicKeyDecode(good, &inOutIdx, &keyPub, inSz);
  6494. if (ret != 0) {
  6495. ret = -5424;
  6496. goto done;
  6497. }
  6498. if (inOutIdx != inSz) {
  6499. ret = -5425;
  6500. goto done;
  6501. }
  6502. wc_FreeRsaKey(&keyPub);
  6503. ret = wc_InitRsaKey(&keyPub, NULL);
  6504. if (ret != 0)
  6505. return -5426;
  6506. inSz = sizeof(goodAlgId);
  6507. inOutIdx = 0;
  6508. ret = wc_RsaPublicKeyDecode(goodAlgId, &inOutIdx, &keyPub, inSz);
  6509. if (ret != 0) {
  6510. ret = -5427;
  6511. goto done;
  6512. }
  6513. if (inOutIdx != inSz) {
  6514. ret = -5428;
  6515. goto done;
  6516. }
  6517. wc_FreeRsaKey(&keyPub);
  6518. ret = wc_InitRsaKey(&keyPub, NULL);
  6519. if (ret != 0)
  6520. return -5429;
  6521. inSz = sizeof(goodAlgIdNull);
  6522. inOutIdx = 0;
  6523. ret = wc_RsaPublicKeyDecode(goodAlgIdNull, &inOutIdx, &keyPub, inSz);
  6524. if (ret != 0) {
  6525. ret = -5430;
  6526. goto done;
  6527. }
  6528. if (inOutIdx != inSz) {
  6529. ret = -5431;
  6530. goto done;
  6531. }
  6532. wc_FreeRsaKey(&keyPub);
  6533. ret = wc_InitRsaKey(&keyPub, NULL);
  6534. if (ret != 0)
  6535. return -5432;
  6536. inSz = sizeof(badBitStrNoZero);
  6537. inOutIdx = 0;
  6538. ret = wc_RsaPublicKeyDecode(badBitStrNoZero, &inOutIdx, &keyPub, inSz);
  6539. if (ret != ASN_EXPECT_0_E) {
  6540. ret = -5433;
  6541. goto done;
  6542. }
  6543. ret = 0;
  6544. done:
  6545. wc_FreeRsaKey(&keyPub);
  6546. return ret;
  6547. }
  6548. #endif
  6549. #define RSA_TEST_BYTES 256
  6550. int rsa_test(void)
  6551. {
  6552. int ret;
  6553. byte* tmp = NULL;
  6554. byte* der = NULL;
  6555. byte* pem = NULL;
  6556. size_t bytes;
  6557. WC_RNG rng;
  6558. RsaKey key;
  6559. #ifdef WOLFSSL_CERT_EXT
  6560. RsaKey keypub;
  6561. #endif
  6562. #ifdef WOLFSSL_KEY_GEN
  6563. RsaKey genKey;
  6564. #endif
  6565. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_NTRU)
  6566. RsaKey caKey;
  6567. #endif
  6568. #ifdef HAVE_ECC
  6569. #ifdef WOLFSSL_CERT_GEN
  6570. ecc_key caEccKey;
  6571. ecc_key caEccKeyPub;
  6572. #endif
  6573. #endif /* HAVE_ECC */
  6574. word32 idx = 0;
  6575. byte* res;
  6576. const char* inStr = "Everyone gets Friday off.";
  6577. word32 inLen = (word32)XSTRLEN((char*)inStr);
  6578. const word32 outSz = RSA_TEST_BYTES;
  6579. const word32 plainSz = RSA_TEST_BYTES;
  6580. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) \
  6581. && !defined(NO_FILESYSTEM)
  6582. FILE *file, *file2;
  6583. #endif
  6584. #ifdef WOLFSSL_TEST_CERT
  6585. DecodedCert cert;
  6586. #endif
  6587. DECLARE_VAR_INIT(in, byte, inLen, inStr, HEAP_HINT);
  6588. DECLARE_VAR(out, byte, RSA_TEST_BYTES, HEAP_HINT);
  6589. DECLARE_VAR(plain, byte, RSA_TEST_BYTES, HEAP_HINT);
  6590. #ifdef WOLFSSL_ASYNC_CRYPT
  6591. if (in == NULL)
  6592. return MEMORY_E;
  6593. #endif
  6594. /* initialize stack structures */
  6595. XMEMSET(&rng, 0, sizeof(rng));
  6596. XMEMSET(&key, 0, sizeof(key));
  6597. #ifdef WOLFSSL_CERT_EXT
  6598. XMEMSET(&keypub, 0, sizeof(keypub));
  6599. #endif
  6600. #ifdef WOLFSSL_KEY_GEN
  6601. XMEMSET(&genKey, 0, sizeof(genKey));
  6602. #endif
  6603. #if defined(WOLFSSL_CERT_GEN) || defined(HAVE_NTRU)
  6604. XMEMSET(&caKey, 0, sizeof(caKey));
  6605. #endif
  6606. #ifdef HAVE_ECC
  6607. #ifdef WOLFSSL_CERT_GEN
  6608. XMEMSET(&caEccKey, 0, sizeof(caEccKey));
  6609. XMEMSET(&caEccKeyPub, 0, sizeof(caEccKeyPub));
  6610. #endif
  6611. #endif /* HAVE_ECC */
  6612. #ifndef HAVE_USER_RSA
  6613. ret = rsa_decode_test();
  6614. if (ret != 0)
  6615. return ret;
  6616. #endif
  6617. #ifdef USE_CERT_BUFFERS_1024
  6618. bytes = (size_t)sizeof_client_key_der_1024;
  6619. if (bytes < (size_t)sizeof_client_cert_der_1024)
  6620. bytes = (size_t)sizeof_client_cert_der_1024;
  6621. #elif defined(USE_CERT_BUFFERS_2048)
  6622. bytes = (size_t)sizeof_client_key_der_2048;
  6623. if (bytes < (size_t)sizeof_client_cert_der_2048)
  6624. bytes = (size_t)sizeof_client_cert_der_2048;
  6625. #else
  6626. bytes = FOURK_BUF;
  6627. #endif
  6628. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6629. if (tmp == NULL
  6630. #ifdef WOLFSSL_ASYNC_CRYPT
  6631. || out == NULL || plain == NULL
  6632. #endif
  6633. ) {
  6634. return -5500;
  6635. }
  6636. #ifdef USE_CERT_BUFFERS_1024
  6637. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  6638. #elif defined(USE_CERT_BUFFERS_2048)
  6639. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  6640. #elif !defined(NO_FILESYSTEM)
  6641. file = fopen(clientKey, "rb");
  6642. if (!file) {
  6643. err_sys("can't open ./certs/client-key.der, "
  6644. "Please run from wolfSSL home dir", -40);
  6645. ERROR_OUT(-5501, exit_rsa);
  6646. }
  6647. bytes = fread(tmp, 1, FOURK_BUF, file);
  6648. fclose(file);
  6649. #else
  6650. /* No key to use. */
  6651. ERROR_OUT(-5502, exit_rsa);
  6652. #endif /* USE_CERT_BUFFERS */
  6653. ret = wc_InitRsaKey_ex(&key, HEAP_HINT, devId);
  6654. if (ret != 0) {
  6655. ERROR_OUT(-5503, exit_rsa);
  6656. }
  6657. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  6658. if (ret != 0) {
  6659. ERROR_OUT(-5504, exit_rsa);
  6660. }
  6661. #ifndef HAVE_FIPS
  6662. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  6663. #else
  6664. ret = wc_InitRng(&rng);
  6665. #endif
  6666. if (ret != 0) {
  6667. ERROR_OUT(-5505, exit_rsa);
  6668. }
  6669. #ifndef NO_SIG_WRAPPER
  6670. ret = rsa_sig_test(&key, sizeof(RsaKey), wc_RsaEncryptSize(&key), &rng);
  6671. if (ret != 0)
  6672. goto exit_rsa;
  6673. #endif
  6674. do {
  6675. #if defined(WOLFSSL_ASYNC_CRYPT)
  6676. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6677. #endif
  6678. if (ret >= 0) {
  6679. ret = wc_RsaPublicEncrypt(in, inLen, out, outSz, &key, &rng);
  6680. }
  6681. } while (ret == WC_PENDING_E);
  6682. if (ret < 0) {
  6683. ERROR_OUT(-5506, exit_rsa);
  6684. }
  6685. #ifdef WC_RSA_BLINDING
  6686. {
  6687. int tmpret = ret;
  6688. ret = wc_RsaSetRNG(&key, &rng);
  6689. if (ret < 0) {
  6690. ERROR_OUT(-5507, exit_rsa);
  6691. }
  6692. ret = tmpret;
  6693. }
  6694. #endif
  6695. idx = (word32)ret; /* save off encrypted length */
  6696. do {
  6697. #if defined(WOLFSSL_ASYNC_CRYPT)
  6698. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6699. #endif
  6700. if (ret >= 0) {
  6701. ret = wc_RsaPrivateDecrypt(out, idx, plain, plainSz, &key);
  6702. }
  6703. } while (ret == WC_PENDING_E);
  6704. if (ret < 0) {
  6705. ERROR_OUT(-5508, exit_rsa);
  6706. }
  6707. if (XMEMCMP(plain, in, inLen)) {
  6708. ERROR_OUT(-5509, exit_rsa);
  6709. }
  6710. do {
  6711. #if defined(WOLFSSL_ASYNC_CRYPT)
  6712. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6713. #endif
  6714. if (ret >= 0) {
  6715. ret = wc_RsaPrivateDecryptInline(out, idx, &res, &key);
  6716. }
  6717. } while (ret == WC_PENDING_E);
  6718. if (ret < 0) {
  6719. ERROR_OUT(-5510, exit_rsa);
  6720. }
  6721. if (ret != (int)inLen) {
  6722. ERROR_OUT(-5511, exit_rsa);
  6723. }
  6724. if (XMEMCMP(res, in, inLen)) {
  6725. ERROR_OUT(-5512, exit_rsa);
  6726. }
  6727. do {
  6728. #if defined(WOLFSSL_ASYNC_CRYPT)
  6729. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6730. #endif
  6731. if (ret >= 0) {
  6732. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  6733. }
  6734. } while (ret == WC_PENDING_E);
  6735. if (ret < 0) {
  6736. ERROR_OUT(-5513, exit_rsa);
  6737. }
  6738. idx = (word32)ret;
  6739. XMEMSET(plain, 0, plainSz);
  6740. do {
  6741. #if defined(WOLFSSL_ASYNC_CRYPT)
  6742. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6743. #endif
  6744. if (ret >= 0) {
  6745. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  6746. }
  6747. } while (ret == WC_PENDING_E);
  6748. if (ret < 0) {
  6749. ERROR_OUT(-5514, exit_rsa);
  6750. }
  6751. if (XMEMCMP(plain, in, (size_t)ret)) {
  6752. ERROR_OUT(-5515, exit_rsa);
  6753. }
  6754. #ifndef WC_NO_RSA_OAEP
  6755. /* OAEP padding testing */
  6756. #if !defined(HAVE_FAST_RSA) && !defined(HAVE_USER_RSA) && \
  6757. !defined(HAVE_FIPS)
  6758. #ifndef NO_SHA
  6759. XMEMSET(plain, 0, plainSz);
  6760. do {
  6761. #if defined(WOLFSSL_ASYNC_CRYPT)
  6762. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6763. #endif
  6764. if (ret >= 0) {
  6765. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  6766. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  6767. }
  6768. } while (ret == WC_PENDING_E);
  6769. if (ret < 0) {
  6770. ERROR_OUT(-5516, exit_rsa);
  6771. }
  6772. idx = (word32)ret;
  6773. do {
  6774. #if defined(WOLFSSL_ASYNC_CRYPT)
  6775. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6776. #endif
  6777. if (ret >= 0) {
  6778. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  6779. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  6780. }
  6781. } while (ret == WC_PENDING_E);
  6782. if (ret < 0) {
  6783. ERROR_OUT(-5517, exit_rsa);
  6784. }
  6785. if (XMEMCMP(plain, in, inLen)) {
  6786. ERROR_OUT(-5518, exit_rsa);
  6787. }
  6788. #endif /* NO_SHA */
  6789. #ifndef NO_SHA256
  6790. XMEMSET(plain, 0, plainSz);
  6791. do {
  6792. #if defined(WOLFSSL_ASYNC_CRYPT)
  6793. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6794. #endif
  6795. if (ret >= 0) {
  6796. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  6797. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  6798. }
  6799. } while (ret == WC_PENDING_E);
  6800. if (ret < 0) {
  6801. ERROR_OUT(-5519, exit_rsa);
  6802. }
  6803. idx = (word32)ret;
  6804. do {
  6805. #if defined(WOLFSSL_ASYNC_CRYPT)
  6806. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6807. #endif
  6808. if (ret >= 0) {
  6809. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  6810. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  6811. }
  6812. } while (ret == WC_PENDING_E);
  6813. if (ret < 0) {
  6814. ERROR_OUT(-5520, exit_rsa);
  6815. }
  6816. if (XMEMCMP(plain, in, inLen)) {
  6817. ERROR_OUT(-5521, exit_rsa);
  6818. }
  6819. do {
  6820. #if defined(WOLFSSL_ASYNC_CRYPT)
  6821. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6822. #endif
  6823. if (ret >= 0) {
  6824. ret = wc_RsaPrivateDecryptInline_ex(out, idx, &res, &key,
  6825. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  6826. }
  6827. } while (ret == WC_PENDING_E);
  6828. if (ret < 0) {
  6829. ERROR_OUT(-5522, exit_rsa);
  6830. }
  6831. if (ret != (int)inLen) {
  6832. ERROR_OUT(-5523, exit_rsa);
  6833. }
  6834. if (XMEMCMP(res, in, inLen)) {
  6835. ERROR_OUT(-5524, exit_rsa);
  6836. }
  6837. /* check fails if not using the same optional label */
  6838. XMEMSET(plain, 0, plainSz);
  6839. do {
  6840. #if defined(WOLFSSL_ASYNC_CRYPT)
  6841. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6842. #endif
  6843. if (ret >= 0) {
  6844. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  6845. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
  6846. }
  6847. } while (ret == WC_PENDING_E);
  6848. if (ret < 0) {
  6849. ERROR_OUT(-5525, exit_rsa);
  6850. }
  6851. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  6852. #ifndef HAVE_CAVIUM
  6853. idx = (word32)ret;
  6854. do {
  6855. #if defined(WOLFSSL_ASYNC_CRYPT)
  6856. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6857. #endif
  6858. if (ret >= 0) {
  6859. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  6860. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  6861. }
  6862. } while (ret == WC_PENDING_E);
  6863. if (ret > 0) { /* in this case decrypt should fail */
  6864. ERROR_OUT(-5526, exit_rsa);
  6865. }
  6866. ret = 0;
  6867. #endif /* !HAVE_CAVIUM */
  6868. /* check using optional label with encrypt/decrypt */
  6869. XMEMSET(plain, 0, plainSz);
  6870. do {
  6871. #if defined(WOLFSSL_ASYNC_CRYPT)
  6872. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6873. #endif
  6874. if (ret >= 0) {
  6875. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  6876. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  6877. }
  6878. } while (ret == WC_PENDING_E);
  6879. if (ret < 0) {
  6880. ERROR_OUT(-5527, exit_rsa);
  6881. }
  6882. idx = (word32)ret;
  6883. do {
  6884. #if defined(WOLFSSL_ASYNC_CRYPT)
  6885. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6886. #endif
  6887. if (ret >= 0) {
  6888. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  6889. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, in, inLen);
  6890. }
  6891. } while (ret == WC_PENDING_E);
  6892. if (ret < 0) {
  6893. ERROR_OUT(-5528, exit_rsa);
  6894. }
  6895. if (XMEMCMP(plain, in, inLen)) {
  6896. ERROR_OUT(-5529, exit_rsa);
  6897. }
  6898. #ifndef NO_SHA
  6899. /* check fail using mismatch hash algorithms */
  6900. XMEMSET(plain, 0, plainSz);
  6901. do {
  6902. #if defined(WOLFSSL_ASYNC_CRYPT)
  6903. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6904. #endif
  6905. if (ret >= 0) {
  6906. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  6907. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, in, inLen);
  6908. }
  6909. } while (ret == WC_PENDING_E);
  6910. if (ret < 0) {
  6911. ERROR_OUT(-5530, exit_rsa);
  6912. }
  6913. /* TODO: investigate why Cavium Nitrox doesn't detect decrypt error here */
  6914. #ifndef HAVE_CAVIUM
  6915. idx = (word32)ret;
  6916. do {
  6917. #if defined(WOLFSSL_ASYNC_CRYPT)
  6918. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6919. #endif
  6920. if (ret >= 0) {
  6921. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  6922. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256,
  6923. in, inLen);
  6924. }
  6925. } while (ret == WC_PENDING_E);
  6926. if (ret > 0) { /* should fail */
  6927. ERROR_OUT(-5531, exit_rsa);
  6928. }
  6929. ret = 0;
  6930. #endif /* !HAVE_CAVIUM */
  6931. #endif /* NO_SHA*/
  6932. #endif /* NO_SHA256 */
  6933. #ifdef WOLFSSL_SHA512
  6934. /* Check valid RSA key size is used while using hash length of SHA512
  6935. If key size is less than (hash length * 2) + 2 then is invalid use
  6936. and test, since OAEP padding requires this.
  6937. BAD_FUNC_ARG is returned when this case is not met */
  6938. if (wc_RsaEncryptSize(&key) > ((int)WC_SHA512_DIGEST_SIZE * 2) + 2) {
  6939. XMEMSET(plain, 0, plainSz);
  6940. do {
  6941. #if defined(WOLFSSL_ASYNC_CRYPT)
  6942. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6943. #endif
  6944. if (ret >= 0) {
  6945. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  6946. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  6947. }
  6948. } while (ret == WC_PENDING_E);
  6949. if (ret < 0) {
  6950. ERROR_OUT(-5532, exit_rsa);
  6951. }
  6952. idx = ret;
  6953. do {
  6954. #if defined(WOLFSSL_ASYNC_CRYPT)
  6955. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6956. #endif
  6957. if (ret >= 0) {
  6958. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  6959. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA512, WC_MGF1SHA512, NULL, 0);
  6960. }
  6961. } while (ret == WC_PENDING_E);
  6962. if (ret < 0) {
  6963. ERROR_OUT(-5533, exit_rsa);
  6964. }
  6965. if (XMEMCMP(plain, in, inLen)) {
  6966. ERROR_OUT(-5534, exit_rsa);
  6967. }
  6968. }
  6969. #endif /* WOLFSSL_SHA512 */
  6970. /* check using pkcsv15 padding with _ex API */
  6971. XMEMSET(plain, 0, plainSz);
  6972. do {
  6973. #if defined(WOLFSSL_ASYNC_CRYPT)
  6974. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6975. #endif
  6976. if (ret >= 0) {
  6977. ret = wc_RsaPublicEncrypt_ex(in, inLen, out, outSz, &key, &rng,
  6978. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  6979. }
  6980. } while (ret == WC_PENDING_E);
  6981. if (ret < 0) {
  6982. ERROR_OUT(-5535, exit_rsa);
  6983. }
  6984. idx = (word32)ret;
  6985. do {
  6986. #if defined(WOLFSSL_ASYNC_CRYPT)
  6987. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  6988. #endif
  6989. if (ret >= 0) {
  6990. ret = wc_RsaPrivateDecrypt_ex(out, idx, plain, plainSz, &key,
  6991. WC_RSA_PKCSV15_PAD, WC_HASH_TYPE_NONE, 0, NULL, 0);
  6992. }
  6993. } while (ret == WC_PENDING_E);
  6994. if (ret < 0) {
  6995. ERROR_OUT(-5536, exit_rsa);
  6996. }
  6997. if (XMEMCMP(plain, in, inLen)) {
  6998. ERROR_OUT(-5537, exit_rsa);
  6999. }
  7000. #endif /* !HAVE_FAST_RSA && !HAVE_FIPS */
  7001. #endif /* WC_NO_RSA_OAEP */
  7002. ret = rsa_flatten_test(&key);
  7003. if (ret != 0)
  7004. return ret;
  7005. #if defined(WOLFSSL_MDK_ARM)
  7006. #define sizeof(s) XSTRLEN((char *)(s))
  7007. #endif
  7008. #ifdef USE_CERT_BUFFERS_1024
  7009. XMEMCPY(tmp, client_cert_der_1024, (size_t)sizeof_client_cert_der_1024);
  7010. bytes = (size_t)sizeof_client_cert_der_1024;
  7011. #elif defined(USE_CERT_BUFFERS_2048)
  7012. XMEMCPY(tmp, client_cert_der_2048, (size_t)sizeof_client_cert_der_2048);
  7013. bytes = (size_t)sizeof_client_cert_der_2048;
  7014. #elif !defined(NO_FILESYSTEM)
  7015. file2 = fopen(clientCert, "rb");
  7016. if (!file2) {
  7017. ERROR_OUT(-5538, exit_rsa);
  7018. }
  7019. bytes = fread(tmp, 1, FOURK_BUF, file2);
  7020. fclose(file2);
  7021. #else
  7022. /* No certificate to use. */
  7023. ERROR_OUT(-5539, exit_rsa);
  7024. #endif
  7025. #ifdef sizeof
  7026. #undef sizeof
  7027. #endif
  7028. #ifdef WOLFSSL_TEST_CERT
  7029. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  7030. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  7031. if (ret != 0) {
  7032. FreeDecodedCert(&cert);
  7033. ERROR_OUT(-5540, exit_rsa);
  7034. }
  7035. FreeDecodedCert(&cert);
  7036. #else
  7037. (void)bytes;
  7038. #endif
  7039. #ifdef WOLFSSL_CERT_EXT
  7040. #ifdef USE_CERT_BUFFERS_1024
  7041. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  7042. bytes = sizeof_client_keypub_der_1024;
  7043. #elif defined(USE_CERT_BUFFERS_2048)
  7044. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  7045. bytes = sizeof_client_keypub_der_2048;
  7046. #else
  7047. file = fopen(clientKeyPub, "rb");
  7048. if (!file) {
  7049. err_sys("can't open ./certs/client-keyPub.der, "
  7050. "Please run from wolfSSL home dir", -40);
  7051. ERROR_OUT(-5541, exit_rsa);
  7052. }
  7053. bytes = fread(tmp, 1, FOURK_BUF, file);
  7054. fclose(file);
  7055. #endif /* USE_CERT_BUFFERS */
  7056. ret = wc_InitRsaKey(&keypub, HEAP_HINT);
  7057. if (ret != 0) {
  7058. ERROR_OUT(-5542, exit_rsa);
  7059. }
  7060. idx = 0;
  7061. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keypub, (word32)bytes);
  7062. if (ret != 0) {
  7063. ERROR_OUT(-5543, exit_rsa);
  7064. }
  7065. #endif /* WOLFSSL_CERT_EXT */
  7066. #ifdef WOLFSSL_KEY_GEN
  7067. {
  7068. int derSz = 0;
  7069. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  7070. if (ret != 0) {
  7071. ERROR_OUT(-5550, exit_rsa);
  7072. }
  7073. ret = wc_MakeRsaKey(&genKey, 1024, 65537, &rng);
  7074. if (ret != 0) {
  7075. ERROR_OUT(-5551, exit_rsa);
  7076. }
  7077. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7078. if (der == NULL) {
  7079. ERROR_OUT(-5552, exit_rsa);
  7080. }
  7081. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7082. if (pem == NULL) {
  7083. ERROR_OUT(-5553, exit_rsa);
  7084. }
  7085. derSz = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  7086. if (derSz < 0) {
  7087. ERROR_OUT(-5554, exit_rsa);
  7088. }
  7089. ret = SaveDerAndPem(der, derSz, pem, FOURK_BUF, keyDerFile, keyPemFile,
  7090. PRIVATEKEY_TYPE, -5555);
  7091. if (ret != 0) {
  7092. goto exit_rsa;
  7093. }
  7094. wc_FreeRsaKey(&genKey);
  7095. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  7096. if (ret != 0) {
  7097. ERROR_OUT(-5560, exit_rsa);
  7098. }
  7099. idx = 0;
  7100. ret = wc_RsaPrivateKeyDecode(der, &idx, &genKey, derSz);
  7101. if (ret != 0) {
  7102. ERROR_OUT(-5561, exit_rsa);
  7103. }
  7104. wc_FreeRsaKey(&genKey);
  7105. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7106. pem = NULL;
  7107. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7108. der = NULL;
  7109. }
  7110. #endif /* WOLFSSL_KEY_GEN */
  7111. #ifdef WOLFSSL_CERT_GEN
  7112. /* self signed */
  7113. {
  7114. Cert myCert;
  7115. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  7116. int certSz;
  7117. #ifdef WOLFSSL_TEST_CERT
  7118. DecodedCert decode;
  7119. #endif
  7120. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7121. if (der == NULL) {
  7122. ERROR_OUT(-5570, exit_rsa);
  7123. }
  7124. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  7125. if (pem == NULL) {
  7126. ERROR_OUT(-5571, exit_rsa);
  7127. }
  7128. if (wc_InitCert(&myCert)) {
  7129. ERROR_OUT(-5572, exit_rsa);
  7130. }
  7131. XMEMCPY(&myCert.subject, &certDefaultName, sizeof(CertName));
  7132. XMEMCPY(myCert.serial, mySerial, sizeof(mySerial));
  7133. myCert.serialSz = (int)sizeof(mySerial);
  7134. myCert.isCA = 1;
  7135. #ifndef NO_SHA256
  7136. myCert.sigType = CTC_SHA256wRSA;
  7137. #else
  7138. myCert.sigType = CTC_SHAwRSA;
  7139. #endif
  7140. #ifdef WOLFSSL_CERT_EXT
  7141. /* add Policies */
  7142. XSTRNCPY(myCert.certPolicies[0], "2.16.840.1.101.3.4.1.42",
  7143. CTC_MAX_CERTPOL_SZ);
  7144. XSTRNCPY(myCert.certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  7145. CTC_MAX_CERTPOL_SZ);
  7146. myCert.certPoliciesNb = 2;
  7147. /* add SKID from the Public Key */
  7148. if (wc_SetSubjectKeyIdFromPublicKey(&myCert, &keypub, NULL) != 0) {
  7149. ERROR_OUT(-5573, exit_rsa);
  7150. }
  7151. /* add AKID from the Public Key */
  7152. if (wc_SetAuthKeyIdFromPublicKey(&myCert, &keypub, NULL) != 0) {
  7153. ERROR_OUT(-5574, exit_rsa);
  7154. }
  7155. /* add Key Usage */
  7156. if (wc_SetKeyUsage(&myCert,"cRLSign,keyCertSign") != 0) {
  7157. ERROR_OUT(-5575, exit_rsa);
  7158. }
  7159. #endif /* WOLFSSL_CERT_EXT */
  7160. ret = 0;
  7161. do {
  7162. #if defined(WOLFSSL_ASYNC_CRYPT)
  7163. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  7164. #endif
  7165. if (ret >= 0) {
  7166. ret = wc_MakeSelfCert(&myCert, der, FOURK_BUF, &key, &rng);
  7167. }
  7168. } while (ret == WC_PENDING_E);
  7169. if (ret < 0) {
  7170. ERROR_OUT(-5576, exit_rsa);
  7171. }
  7172. certSz = ret;
  7173. #ifdef WOLFSSL_TEST_CERT
  7174. InitDecodedCert(&decode, der, certSz, HEAP_HINT);
  7175. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  7176. if (ret != 0) {
  7177. FreeDecodedCert(&decode);
  7178. ERROR_OUT(-5577, exit_rsa);
  7179. }
  7180. FreeDecodedCert(&decode);
  7181. #endif
  7182. ret = SaveDerAndPem(der, certSz, pem, FOURK_BUF, certDerFile,
  7183. certPemFile, CERT_TYPE, -5578);
  7184. if (ret != 0) {
  7185. goto exit_rsa;
  7186. }
  7187. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7188. pem = NULL;
  7189. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7190. der = NULL;
  7191. }
  7192. /* Make Cert / Sign example for RSA cert and RSA CA */
  7193. {
  7194. Cert myCert;
  7195. int certSz;
  7196. size_t bytes3;
  7197. word32 idx3 = 0;
  7198. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  7199. FILE* file3;
  7200. #endif
  7201. #ifdef WOLFSSL_TEST_CERT
  7202. DecodedCert decode;
  7203. #endif
  7204. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7205. if (der == NULL) {
  7206. ERROR_OUT(-5580, exit_rsa);
  7207. }
  7208. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  7209. if (pem == NULL) {
  7210. ERROR_OUT(-5581, exit_rsa);
  7211. }
  7212. /* Get CA Key */
  7213. #ifdef USE_CERT_BUFFERS_1024
  7214. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  7215. bytes3 = sizeof_ca_key_der_1024;
  7216. #elif defined(USE_CERT_BUFFERS_2048)
  7217. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  7218. bytes3 = sizeof_ca_key_der_2048;
  7219. #else
  7220. file3 = fopen(rsaCaKeyFile, "rb");
  7221. if (!file3) {
  7222. ERROR_OUT(-5582, exit_rsa);
  7223. }
  7224. bytes3 = fread(tmp, 1, FOURK_BUF, file3);
  7225. fclose(file3);
  7226. #endif /* USE_CERT_BUFFERS */
  7227. ret = wc_InitRsaKey(&caKey, HEAP_HINT);
  7228. if (ret != 0) {
  7229. ERROR_OUT(-5583, exit_rsa);
  7230. }
  7231. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3);
  7232. if (ret != 0) {
  7233. ERROR_OUT(-5584, exit_rsa);
  7234. }
  7235. /* Setup Certificate */
  7236. if (wc_InitCert(&myCert)) {
  7237. ERROR_OUT(-5585, exit_rsa);
  7238. }
  7239. #ifndef NO_SHA256
  7240. myCert.sigType = CTC_SHA256wRSA;
  7241. #else
  7242. myCert.sigType = CTC_SHAwRSA;
  7243. #endif
  7244. XMEMCPY(&myCert.subject, &certDefaultName, sizeof(CertName));
  7245. #ifdef WOLFSSL_CERT_EXT
  7246. /* add Policies */
  7247. XSTRNCPY(myCert.certPolicies[0], "2.16.840.1.101.3.4.1.42",
  7248. CTC_MAX_CERTPOL_SZ);
  7249. myCert.certPoliciesNb =1;
  7250. /* add SKID from the Public Key */
  7251. if (wc_SetSubjectKeyIdFromPublicKey(&myCert, &key, NULL) != 0) {
  7252. ERROR_OUT(-5586, exit_rsa);
  7253. }
  7254. /* add AKID from the CA certificate */
  7255. #if defined(USE_CERT_BUFFERS_2048)
  7256. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_2048,
  7257. sizeof_ca_cert_der_2048);
  7258. #elif defined(USE_CERT_BUFFERS_1024)
  7259. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_1024,
  7260. sizeof_ca_cert_der_1024);
  7261. #else
  7262. ret = wc_SetAuthKeyId(&myCert, rsaCaCertFile);
  7263. #endif
  7264. if (ret != 0) {
  7265. ERROR_OUT(-5587, exit_rsa);
  7266. }
  7267. /* add Key Usage */
  7268. if (wc_SetKeyUsage(&myCert,"keyEncipherment,keyAgreement") != 0) {
  7269. ERROR_OUT(-5588, exit_rsa);
  7270. }
  7271. #endif /* WOLFSSL_CERT_EXT */
  7272. #if defined(USE_CERT_BUFFERS_2048)
  7273. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_2048,
  7274. sizeof_ca_cert_der_2048);
  7275. #elif defined(USE_CERT_BUFFERS_1024)
  7276. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_1024,
  7277. sizeof_ca_cert_der_1024);
  7278. #else
  7279. ret = wc_SetIssuer(&myCert, rsaCaCertFile);
  7280. #endif
  7281. if (ret < 0) {
  7282. ERROR_OUT(-5589, exit_rsa);
  7283. }
  7284. certSz = wc_MakeCert(&myCert, der, FOURK_BUF, &key, NULL, &rng);
  7285. if (certSz < 0) {
  7286. ERROR_OUT(-5590, exit_rsa);
  7287. }
  7288. ret = 0;
  7289. do {
  7290. #if defined(WOLFSSL_ASYNC_CRYPT)
  7291. ret = wc_AsyncWait(ret, &caKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  7292. #endif
  7293. if (ret >= 0) {
  7294. ret = wc_SignCert(myCert.bodySz, myCert.sigType, der, FOURK_BUF,
  7295. &caKey, NULL, &rng);
  7296. }
  7297. } while (ret == WC_PENDING_E);
  7298. if (ret < 0) {
  7299. ERROR_OUT(-5591, exit_rsa);
  7300. }
  7301. certSz = ret;
  7302. #ifdef WOLFSSL_TEST_CERT
  7303. InitDecodedCert(&decode, der, certSz, HEAP_HINT);
  7304. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  7305. if (ret != 0) {
  7306. FreeDecodedCert(&decode);
  7307. ERROR_OUT(-5592, exit_rsa);
  7308. }
  7309. FreeDecodedCert(&decode);
  7310. #endif
  7311. ret = SaveDerAndPem(der, certSz, pem, FOURK_BUF, otherCertDerFile,
  7312. otherCertPemFile, CERT_TYPE, -5593);
  7313. if (ret != 0) {
  7314. goto exit_rsa;
  7315. }
  7316. wc_FreeRsaKey(&caKey);
  7317. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7318. pem = NULL;
  7319. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7320. der = NULL;
  7321. }
  7322. #if !defined(NO_RSA) && defined(HAVE_ECC)
  7323. /* Make Cert / Sign example for ECC cert and RSA CA */
  7324. {
  7325. Cert myCert;
  7326. int certSz;
  7327. size_t bytes3;
  7328. word32 idx3 = 0;
  7329. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  7330. FILE* file3;
  7331. #endif
  7332. #ifdef WOLFSSL_TEST_CERT
  7333. DecodedCert decode;
  7334. #endif
  7335. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7336. if (der == NULL) {
  7337. ERROR_OUT(-5600, exit_rsa);
  7338. }
  7339. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  7340. if (pem == NULL) {
  7341. ERROR_OUT(-5601, exit_rsa);
  7342. }
  7343. /* Get CA Key */
  7344. #ifdef USE_CERT_BUFFERS_1024
  7345. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  7346. bytes3 = sizeof_ca_key_der_1024;
  7347. #elif defined(USE_CERT_BUFFERS_2048)
  7348. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  7349. bytes3 = sizeof_ca_key_der_2048;
  7350. #else
  7351. file3 = fopen(rsaCaKeyFile, "rb");
  7352. if (!file3) {
  7353. ERROR_OUT(-5602, exit_rsa);
  7354. }
  7355. bytes3 = fread(tmp, 1, FOURK_BUF, file3);
  7356. fclose(file3);
  7357. #endif /* USE_CERT_BUFFERS */
  7358. ret = wc_InitRsaKey(&caKey, HEAP_HINT);
  7359. if (ret != 0) {
  7360. ERROR_OUT(-5603, exit_rsa);
  7361. }
  7362. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3);
  7363. if (ret != 0) {
  7364. ERROR_OUT(-5604, exit_rsa);
  7365. }
  7366. /* Get Cert Key */
  7367. #ifdef USE_CERT_BUFFERS_256
  7368. XMEMCPY(tmp, ecc_key_pub_der_256, sizeof_ecc_key_pub_der_256);
  7369. bytes3 = sizeof_ecc_key_pub_der_256;
  7370. #else
  7371. file3 = fopen(eccKeyPubFile, "rb");
  7372. if (!file3) {
  7373. ERROR_OUT(-5605, exit_rsa);
  7374. }
  7375. bytes3 = fread(tmp, 1, FOURK_BUF, file3);
  7376. fclose(file3);
  7377. #endif
  7378. ret = wc_ecc_init_ex(&caEccKeyPub, HEAP_HINT, devId);
  7379. if (ret != 0) {
  7380. ERROR_OUT(-5606, exit_rsa);
  7381. }
  7382. idx3 = 0;
  7383. ret = wc_EccPublicKeyDecode(tmp, &idx3, &caEccKeyPub, (word32)bytes3);
  7384. if (ret != 0) {
  7385. ERROR_OUT(-5607, exit_rsa);
  7386. }
  7387. /* Setup Certificate */
  7388. if (wc_InitCert(&myCert)) {
  7389. ERROR_OUT(-5608, exit_rsa);
  7390. }
  7391. #ifndef NO_SHA256
  7392. myCert.sigType = CTC_SHA256wRSA;
  7393. #else
  7394. myCert.sigType = CTC_SHAwRSA;
  7395. #endif
  7396. XMEMCPY(&myCert.subject, &certDefaultName, sizeof(CertName));
  7397. #ifdef WOLFSSL_CERT_EXT
  7398. /* add Policies */
  7399. XSTRNCPY(myCert.certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  7400. CTC_MAX_CERTPOL_SZ);
  7401. XSTRNCPY(myCert.certPolicies[1], "1.2.13025.489.1.113549",
  7402. CTC_MAX_CERTPOL_SZ);
  7403. myCert.certPoliciesNb = 2;
  7404. /* add SKID from the Public Key */
  7405. if (wc_SetSubjectKeyIdFromPublicKey(&myCert, NULL, &caEccKeyPub) != 0) {
  7406. ERROR_OUT(-5609, exit_rsa);
  7407. }
  7408. /* add AKID from the CA certificate */
  7409. #if defined(USE_CERT_BUFFERS_2048)
  7410. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_2048,
  7411. sizeof_ca_cert_der_2048);
  7412. #elif defined(USE_CERT_BUFFERS_1024)
  7413. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_1024,
  7414. sizeof_ca_cert_der_1024);
  7415. #else
  7416. ret = wc_SetAuthKeyId(&myCert, rsaCaCertFile);
  7417. #endif
  7418. if (ret != 0) {
  7419. ERROR_OUT(-5610, exit_rsa);
  7420. }
  7421. /* add Key Usage */
  7422. if (wc_SetKeyUsage(&myCert, certKeyUsage) != 0) {
  7423. ERROR_OUT(-5611, exit_rsa);
  7424. }
  7425. #endif /* WOLFSSL_CERT_EXT */
  7426. #if defined(USE_CERT_BUFFERS_2048)
  7427. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_2048,
  7428. sizeof_ca_cert_der_2048);
  7429. #elif defined(USE_CERT_BUFFERS_1024)
  7430. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_1024,
  7431. sizeof_ca_cert_der_1024);
  7432. #else
  7433. ret = wc_SetIssuer(&myCert, rsaCaCertFile);
  7434. #endif
  7435. if (ret < 0) {
  7436. ERROR_OUT(-5612, exit_rsa);
  7437. }
  7438. certSz = wc_MakeCert(&myCert, der, FOURK_BUF, NULL, &caEccKeyPub, &rng);
  7439. if (certSz < 0) {
  7440. ERROR_OUT(-5613, exit_rsa);
  7441. }
  7442. ret = 0;
  7443. do {
  7444. #if defined(WOLFSSL_ASYNC_CRYPT)
  7445. ret = wc_AsyncWait(ret, &caEccKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  7446. #endif
  7447. if (ret >= 0) {
  7448. ret = wc_SignCert(myCert.bodySz, myCert.sigType, der,
  7449. FOURK_BUF, &caKey, NULL, &rng);
  7450. }
  7451. } while (ret == WC_PENDING_E);
  7452. if (ret < 0) {
  7453. ERROR_OUT(-5614, exit_rsa);
  7454. }
  7455. certSz = ret;
  7456. #ifdef WOLFSSL_TEST_CERT
  7457. InitDecodedCert(&decode, der, certSz, 0);
  7458. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  7459. if (ret != 0) {
  7460. FreeDecodedCert(&decode);
  7461. ERROR_OUT(-5615, exit_rsa);
  7462. }
  7463. FreeDecodedCert(&decode);
  7464. #endif
  7465. ret = SaveDerAndPem(der, certSz, pem, FOURK_BUF, certEccRsaDerFile,
  7466. certEccRsaPemFile, CERT_TYPE, -5616);
  7467. if (ret != 0) {
  7468. goto exit_rsa;
  7469. }
  7470. wc_ecc_free(&caEccKeyPub);
  7471. wc_FreeRsaKey(&caKey);
  7472. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7473. pem = NULL;
  7474. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7475. der = NULL;
  7476. }
  7477. #endif /* !NO_RSA && HAVE_ECC */
  7478. #ifdef HAVE_NTRU
  7479. {
  7480. Cert myCert;
  7481. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  7482. FILE* caFile;
  7483. #endif
  7484. FILE* ntruPrivFile;
  7485. int certSz;
  7486. word32 idx3 = 0;
  7487. #ifdef WOLFSSL_TEST_CERT
  7488. DecodedCert decode;
  7489. #endif
  7490. byte public_key[557]; /* sized for EES401EP2 */
  7491. word16 public_key_len; /* no. of octets in public key */
  7492. byte private_key[607]; /* sized for EES401EP2 */
  7493. word16 private_key_len; /* no. of octets in private key */
  7494. DRBG_HANDLE drbg;
  7495. static uint8_t const pers_str[] = {
  7496. 'C', 'y', 'a', 'S', 'S', 'L', ' ', 't', 'e', 's', 't'
  7497. };
  7498. word32 rc = ntru_crypto_drbg_instantiate(112, pers_str,
  7499. sizeof(pers_str), GetEntropy, &drbg);
  7500. if (rc != DRBG_OK) {
  7501. ERROR_OUT(-5620, exit_rsa);
  7502. }
  7503. rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
  7504. &public_key_len, NULL,
  7505. &private_key_len, NULL);
  7506. if (rc != NTRU_OK) {
  7507. ERROR_OUT(-5621, exit_rsa);
  7508. }
  7509. rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
  7510. &public_key_len, public_key,
  7511. &private_key_len, private_key);
  7512. if (rc != NTRU_OK) {
  7513. ERROR_OUT(-5622, exit_rsa);
  7514. }
  7515. rc = ntru_crypto_drbg_uninstantiate(drbg);
  7516. if (rc != NTRU_OK) {
  7517. ERROR_OUT(-5623, exit_rsa);
  7518. }
  7519. #ifdef USE_CERT_BUFFERS_1024
  7520. XMEMCPY(tmp, ca_key_der_1024, sizeof_ca_key_der_1024);
  7521. bytes = sizeof_ca_key_der_1024;
  7522. #elif defined(USE_CERT_BUFFERS_2048)
  7523. XMEMCPY(tmp, ca_key_der_2048, sizeof_ca_key_der_2048);
  7524. bytes = sizeof_ca_key_der_2048;
  7525. #else
  7526. caFile = fopen(rsaCaKeyFile, "rb");
  7527. if (!caFile) {
  7528. ERROR_OUT(-5624, exit_rsa);
  7529. }
  7530. bytes = fread(tmp, 1, FOURK_BUF, caFile);
  7531. fclose(caFile);
  7532. #endif /* USE_CERT_BUFFERS */
  7533. ret = wc_InitRsaKey(&caKey, HEAP_HINT);
  7534. if (ret != 0) {
  7535. ERROR_OUT(-5625, exit_rsa);
  7536. }
  7537. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes);
  7538. if (ret != 0) {
  7539. ERROR_OUT(-5626, exit_rsa);
  7540. }
  7541. if (wc_InitCert(&myCert)) {
  7542. ERROR_OUT(-5627, exit_rsa);
  7543. }
  7544. XMEMCPY(&myCert.subject, &certDefaultName, sizeof(CertName));
  7545. myCert.daysValid = 1000;
  7546. #ifdef WOLFSSL_CERT_EXT
  7547. /* add SKID from the Public Key */
  7548. if (wc_SetSubjectKeyIdFromNtruPublicKey(&myCert, public_key,
  7549. public_key_len) != 0) {
  7550. ERROR_OUT(-5628, exit_rsa);
  7551. }
  7552. /* add AKID from the CA certificate */
  7553. #if defined(USE_CERT_BUFFERS_2048)
  7554. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_2048,
  7555. sizeof_ca_cert_der_2048);
  7556. #elif defined(USE_CERT_BUFFERS_1024)
  7557. ret = wc_SetAuthKeyIdFromCert(&myCert, ca_cert_der_1024,
  7558. sizeof_ca_cert_der_1024);
  7559. #else
  7560. ret = wc_SetAuthKeyId(&myCert, rsaCaCertFile);
  7561. #endif
  7562. if (ret != 0) {
  7563. ERROR_OUT(-5629, exit_rsa);
  7564. }
  7565. /* add Key Usage */
  7566. if (wc_SetKeyUsage(&myCert, certKeyUsage2) != 0) {
  7567. ERROR_OUT(-5630, exit_rsa);
  7568. }
  7569. #endif /* WOLFSSL_CERT_EXT */
  7570. #if defined(USE_CERT_BUFFERS_2048)
  7571. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_2048,
  7572. sizeof_ca_cert_der_2048);
  7573. #elif defined(USE_CERT_BUFFERS_1024)
  7574. ret = wc_SetIssuerBuffer(&myCert, ca_cert_der_1024,
  7575. sizeof_ca_cert_der_1024);
  7576. #else
  7577. ret = wc_SetIssuer(&myCert, rsaCaCertFile);
  7578. #endif
  7579. if (ret < 0) {
  7580. ERROR_OUT(-5631, exit_rsa);
  7581. }
  7582. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7583. if (der == NULL) {
  7584. ERROR_OUT(-5632, exit_rsa);
  7585. }
  7586. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  7587. if (pem == NULL) {
  7588. ERROR_OUT(-5633, exit_rsa);
  7589. }
  7590. certSz = wc_MakeNtruCert(&myCert, der, FOURK_BUF, public_key,
  7591. public_key_len, &rng);
  7592. if (certSz < 0) {
  7593. ERROR_OUT(-5634, exit_rsa);
  7594. }
  7595. ret = 0;
  7596. do {
  7597. #if defined(WOLFSSL_ASYNC_CRYPT)
  7598. ret = wc_AsyncWait(ret, &caKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  7599. #endif
  7600. if (ret >= 0) {
  7601. ret = wc_SignCert(myCert.bodySz, myCert.sigType, der, FOURK_BUF,
  7602. &caKey, NULL, &rng);
  7603. }
  7604. } while (ret == WC_PENDING_E);
  7605. wc_FreeRsaKey(&caKey);
  7606. if (ret < 0) {
  7607. ERROR_OUT(-5635, exit_rsa);
  7608. }
  7609. certSz = ret;
  7610. #ifdef WOLFSSL_TEST_CERT
  7611. InitDecodedCert(&decode, der, certSz, HEAP_HINT);
  7612. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  7613. if (ret != 0) {
  7614. FreeDecodedCert(&decode);
  7615. ERROR_OUT(-5636, exit_rsa);
  7616. }
  7617. FreeDecodedCert(&decode);
  7618. #endif
  7619. ret = SaveDerAndPem(der, certSz, pem, FOURK_BUF, "./ntru-cert.der",
  7620. "./ntru-cert.pem", CERT_TYPE, -5637);
  7621. if (ret != 0) {
  7622. goto exit_rsa;
  7623. }
  7624. #if !defined(NO_FILESYSTEM) && !defined(NO_WRITE_TEMP_FILES)
  7625. ntruPrivFile = fopen("./ntru-key.raw", "wb");
  7626. if (!ntruPrivFile) {
  7627. ERROR_OUT(-5638, exit_rsa);
  7628. }
  7629. ret = (int)fwrite(private_key, 1, private_key_len, ntruPrivFile);
  7630. fclose(ntruPrivFile);
  7631. if (ret != private_key_len) {
  7632. ERROR_OUT(-5639, exit_rsa);
  7633. }
  7634. #endif
  7635. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7636. pem = NULL;
  7637. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7638. der = NULL;
  7639. }
  7640. #endif /* HAVE_NTRU */
  7641. #ifdef WOLFSSL_CERT_REQ
  7642. {
  7643. Cert req;
  7644. int derSz;
  7645. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  7646. if (der == NULL) {
  7647. ERROR_OUT(-5640, exit_rsa);
  7648. }
  7649. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,DYNAMIC_TYPE_TMP_BUFFER);
  7650. if (pem == NULL) {
  7651. ERROR_OUT(-5641, exit_rsa);
  7652. }
  7653. if (wc_InitCert(&req)) {
  7654. ERROR_OUT(-5642, exit_rsa);
  7655. }
  7656. req.version = 0;
  7657. req.isCA = 1;
  7658. XSTRNCPY(req.challengePw, "wolf123", CTC_NAME_SIZE);
  7659. XMEMCPY(&req.subject, &certDefaultName, sizeof(CertName));
  7660. #ifndef NO_SHA256
  7661. req.sigType = CTC_SHA256wRSA;
  7662. #else
  7663. req.sigType = CTC_SHAwRSA;
  7664. #endif
  7665. #ifdef WOLFSSL_CERT_EXT
  7666. /* add SKID from the Public Key */
  7667. if (wc_SetSubjectKeyIdFromPublicKey(&req, &keypub, NULL) != 0) {
  7668. ERROR_OUT(-5643, exit_rsa);
  7669. }
  7670. /* add Key Usage */
  7671. if (wc_SetKeyUsage(&req, certKeyUsage2) != 0) {
  7672. ERROR_OUT(-5644, exit_rsa);
  7673. }
  7674. /* add Extended Key Usage */
  7675. if (wc_SetExtKeyUsage(&req, "serverAuth,clientAuth,codeSigning,"
  7676. "emailProtection,timeStamping,OCSPSigning") != 0) {
  7677. ERROR_OUT(-5645, exit_rsa);
  7678. }
  7679. #endif /* WOLFSSL_CERT_EXT */
  7680. derSz = wc_MakeCertReq(&req, der, FOURK_BUF, &key, NULL);
  7681. if (derSz < 0) {
  7682. ERROR_OUT(-5646, exit_rsa);
  7683. }
  7684. #ifdef WOLFSSL_CERT_EXT
  7685. /* Try again with "any" flag set, will override all others */
  7686. if (wc_SetExtKeyUsage(&req, "any") != 0) {
  7687. ERROR_OUT(-5647, exit_rsa);
  7688. }
  7689. derSz = wc_MakeCertReq(&req, der, FOURK_BUF, &key, NULL);
  7690. if (derSz < 0) {
  7691. ERROR_OUT(-5648, exit_rsa);
  7692. }
  7693. #endif /* WOLFSSL_CERT_EXT */
  7694. ret = 0;
  7695. do {
  7696. #if defined(WOLFSSL_ASYNC_CRYPT)
  7697. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  7698. #endif
  7699. if (ret >= 0) {
  7700. ret = wc_SignCert(req.bodySz, req.sigType, der, FOURK_BUF,
  7701. &key, NULL, &rng);
  7702. }
  7703. } while (ret == WC_PENDING_E);
  7704. if (ret < 0) {
  7705. ERROR_OUT(-5649, exit_rsa);
  7706. }
  7707. derSz = ret;
  7708. ret = SaveDerAndPem(der, derSz, pem, FOURK_BUF, certReqDerFile,
  7709. certReqPemFile, CERTREQ_TYPE, -5650);
  7710. if (ret != 0) {
  7711. goto exit_rsa;
  7712. }
  7713. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7714. pem = NULL;
  7715. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7716. der = NULL;
  7717. }
  7718. #endif /* WOLFSSL_CERT_REQ */
  7719. #endif /* WOLFSSL_CERT_GEN */
  7720. exit_rsa:
  7721. wc_FreeRsaKey(&key);
  7722. #ifdef WOLFSSL_CERT_EXT
  7723. wc_FreeRsaKey(&keypub);
  7724. #endif
  7725. #ifdef WOLFSSL_KEY_GEN
  7726. wc_FreeRsaKey(&genKey);
  7727. #endif
  7728. #ifdef WOLFSSL_CERT_GEN
  7729. wc_FreeRsaKey(&caKey);
  7730. #ifdef HAVE_ECC
  7731. wc_ecc_free(&caEccKey);
  7732. #ifdef WOLFSSL_CERT_EXT
  7733. wc_ecc_free(&caEccKeyPub);
  7734. #endif
  7735. #endif
  7736. #endif
  7737. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7738. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7739. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7740. wc_FreeRng(&rng);
  7741. FREE_VAR(in, HEAP_HINT);
  7742. FREE_VAR(out, HEAP_HINT);
  7743. FREE_VAR(plain, HEAP_HINT);
  7744. /* ret can be greater then 0 with certgen but all negative values should
  7745. * be returned and treated as an error */
  7746. if (ret >= 0) {
  7747. return 0;
  7748. }
  7749. else {
  7750. return ret;
  7751. }
  7752. }
  7753. #endif
  7754. #ifndef NO_DH
  7755. static int dh_generate_test(WC_RNG *rng)
  7756. {
  7757. int ret = 0;
  7758. DhKey smallKey;
  7759. byte p[2] = { 0, 5 };
  7760. byte g[2] = { 0, 2 };
  7761. #ifdef WOLFSSL_DH_CONST
  7762. /* the table for constant DH lookup will round to the lowest byte size 21 */
  7763. byte priv[21];
  7764. byte pub[21];
  7765. #else
  7766. byte priv[2];
  7767. byte pub[2];
  7768. #endif
  7769. word32 privSz = sizeof(priv);
  7770. word32 pubSz = sizeof(pub);
  7771. ret = wc_InitDhKey_ex(&smallKey, HEAP_HINT, devId);
  7772. if (ret != 0)
  7773. return -5700;
  7774. /* Parameter Validation testing. */
  7775. ret = wc_DhSetKey(NULL, p, sizeof(p), g, sizeof(g));
  7776. if (ret != BAD_FUNC_ARG) {
  7777. ERROR_OUT(-5701, exit_gen_test);
  7778. }
  7779. ret = wc_DhSetKey(&smallKey, NULL, sizeof(p), g, sizeof(g));
  7780. if (ret != BAD_FUNC_ARG) {
  7781. ERROR_OUT(-5702, exit_gen_test);
  7782. }
  7783. ret = wc_DhSetKey(&smallKey, p, 0, g, sizeof(g));
  7784. if (ret != BAD_FUNC_ARG) {
  7785. ERROR_OUT(-5703, exit_gen_test);
  7786. }
  7787. ret = wc_DhSetKey(&smallKey, p, sizeof(p), NULL, sizeof(g));
  7788. if (ret != BAD_FUNC_ARG) {
  7789. ERROR_OUT(-5704, exit_gen_test);
  7790. }
  7791. ret = wc_DhSetKey(&smallKey, p, sizeof(p), g, 0);
  7792. if (ret != BAD_FUNC_ARG) {
  7793. ERROR_OUT(-5705, exit_gen_test);
  7794. }
  7795. ret = wc_DhSetKey(&smallKey, p, sizeof(p), g, sizeof(g));
  7796. if (ret != 0) {
  7797. ERROR_OUT(-5706, exit_gen_test);
  7798. }
  7799. /* Use API. */
  7800. ret = wc_DhGenerateKeyPair(&smallKey, rng, priv, &privSz, pub, &pubSz);
  7801. #if defined(WOLFSSL_ASYNC_CRYPT)
  7802. ret = wc_AsyncWait(ret, &smallKey.asyncDev, WC_ASYNC_FLAG_NONE);
  7803. #endif
  7804. if (ret != 0) {
  7805. ret = -5707;
  7806. }
  7807. exit_gen_test:
  7808. wc_FreeDhKey(&smallKey);
  7809. return ret;
  7810. }
  7811. int dh_test(void)
  7812. {
  7813. int ret;
  7814. word32 bytes;
  7815. word32 idx = 0, privSz, pubSz, privSz2, pubSz2, agreeSz, agreeSz2;
  7816. byte tmp[1024];
  7817. byte priv[256];
  7818. byte pub[256];
  7819. byte priv2[256];
  7820. byte pub2[256];
  7821. byte agree[256];
  7822. byte agree2[256];
  7823. DhKey key;
  7824. DhKey key2;
  7825. WC_RNG rng;
  7826. #ifdef USE_CERT_BUFFERS_1024
  7827. XMEMCPY(tmp, dh_key_der_1024, (size_t)sizeof_dh_key_der_1024);
  7828. bytes = (size_t)sizeof_dh_key_der_1024;
  7829. #elif defined(USE_CERT_BUFFERS_2048)
  7830. XMEMCPY(tmp, dh_key_der_2048, (size_t)sizeof_dh_key_der_2048);
  7831. bytes = (size_t)sizeof_dh_key_der_2048;
  7832. #elif defined(NO_ASN)
  7833. /* don't use file, no DER parsing */
  7834. #elif !defined(NO_FILESYSTEM)
  7835. FILE* file = fopen(dhKey, "rb");
  7836. if (!file)
  7837. return -5710;
  7838. bytes = (word32) fread(tmp, 1, sizeof(tmp), file);
  7839. fclose(file);
  7840. #else
  7841. /* No DH key to use. */
  7842. return -5711;
  7843. #endif /* USE_CERT_BUFFERS */
  7844. (void)idx;
  7845. (void)tmp;
  7846. (void)bytes;
  7847. ret = wc_InitDhKey_ex(&key, HEAP_HINT, devId);
  7848. if (ret != 0) {
  7849. ERROR_OUT(-5712, done);
  7850. }
  7851. ret = wc_InitDhKey_ex(&key2, HEAP_HINT, devId);
  7852. if (ret != 0) {
  7853. ERROR_OUT(-5713, done);
  7854. }
  7855. #ifdef NO_ASN
  7856. ret = wc_DhSetKey(&key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  7857. if (ret != 0) {
  7858. ERROR_OUT(-5714, done);
  7859. }
  7860. ret = wc_DhSetKey(&key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  7861. if (ret != 0) {
  7862. ERROR_OUT(-5715, done);
  7863. }
  7864. #else
  7865. ret = wc_DhKeyDecode(tmp, &idx, &key, bytes);
  7866. if (ret != 0) {
  7867. ERROR_OUT(-5716, done);
  7868. }
  7869. idx = 0;
  7870. ret = wc_DhKeyDecode(tmp, &idx, &key2, bytes);
  7871. if (ret != 0) {
  7872. ERROR_OUT(-5717, done);
  7873. }
  7874. #endif
  7875. #ifndef HAVE_FIPS
  7876. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  7877. #else
  7878. ret = wc_InitRng(&rng);
  7879. #endif
  7880. if (ret != 0) {
  7881. ERROR_OUT(-5718, done);
  7882. }
  7883. ret = wc_DhGenerateKeyPair(&key, &rng, priv, &privSz, pub, &pubSz);
  7884. #if defined(WOLFSSL_ASYNC_CRYPT)
  7885. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  7886. #endif
  7887. if (ret != 0) {
  7888. ERROR_OUT(-5719, done);
  7889. }
  7890. ret = wc_DhGenerateKeyPair(&key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  7891. #if defined(WOLFSSL_ASYNC_CRYPT)
  7892. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  7893. #endif
  7894. if (ret != 0) {
  7895. ERROR_OUT(-5720, done);
  7896. }
  7897. ret = wc_DhAgree(&key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  7898. #if defined(WOLFSSL_ASYNC_CRYPT)
  7899. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  7900. #endif
  7901. if (ret != 0) {
  7902. ERROR_OUT(-5721, done);
  7903. }
  7904. ret = wc_DhAgree(&key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  7905. #if defined(WOLFSSL_ASYNC_CRYPT)
  7906. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  7907. #endif
  7908. if (ret != 0) {
  7909. ERROR_OUT(-5722, done);
  7910. }
  7911. if (agreeSz != agreeSz2 || XMEMCMP(agree, agree2, agreeSz)) {
  7912. ERROR_OUT(-5723, done);
  7913. }
  7914. ret = dh_generate_test(&rng);
  7915. done:
  7916. wc_FreeDhKey(&key);
  7917. wc_FreeDhKey(&key2);
  7918. wc_FreeRng(&rng);
  7919. return ret;
  7920. }
  7921. #endif /* NO_DH */
  7922. #ifndef NO_DSA
  7923. int dsa_test(void)
  7924. {
  7925. int ret, answer;
  7926. word32 bytes;
  7927. word32 idx = 0;
  7928. byte tmp[1024];
  7929. DsaKey key;
  7930. WC_RNG rng;
  7931. wc_Sha sha;
  7932. byte hash[WC_SHA_DIGEST_SIZE];
  7933. byte signature[40];
  7934. #ifdef USE_CERT_BUFFERS_1024
  7935. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  7936. bytes = sizeof_dsa_key_der_1024;
  7937. #elif defined(USE_CERT_BUFFERS_2048)
  7938. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  7939. bytes = sizeof_dsa_key_der_2048;
  7940. #else
  7941. FILE* file = fopen(dsaKey, "rb");
  7942. if (!file)
  7943. return -5800;
  7944. bytes = (word32) fread(tmp, 1, sizeof(tmp), file);
  7945. fclose(file);
  7946. #endif /* USE_CERT_BUFFERS */
  7947. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  7948. if (ret != 0)
  7949. return -5801;
  7950. wc_ShaUpdate(&sha, tmp, bytes);
  7951. wc_ShaFinal(&sha, hash);
  7952. wc_ShaFree(&sha);
  7953. ret = wc_InitDsaKey(&key);
  7954. if (ret != 0) return -5802;
  7955. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  7956. if (ret != 0) return -5803;
  7957. #ifndef HAVE_FIPS
  7958. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  7959. #else
  7960. ret = wc_InitRng(&rng);
  7961. #endif
  7962. if (ret != 0) return -5804;
  7963. ret = wc_DsaSign(hash, signature, &key, &rng);
  7964. if (ret != 0) return -5805;
  7965. ret = wc_DsaVerify(hash, signature, &key, &answer);
  7966. if (ret != 0) return -5806;
  7967. if (answer != 1) return -5807;
  7968. wc_FreeDsaKey(&key);
  7969. #ifdef WOLFSSL_KEY_GEN
  7970. {
  7971. byte* der;
  7972. byte* pem;
  7973. int derSz = 0;
  7974. DsaKey derIn;
  7975. DsaKey genKey;
  7976. ret = wc_InitDsaKey(&genKey);
  7977. if (ret != 0) return -5808;
  7978. ret = wc_MakeDsaParameters(&rng, 1024, &genKey);
  7979. if (ret != 0) {
  7980. wc_FreeDsaKey(&genKey);
  7981. return -5809;
  7982. }
  7983. ret = wc_MakeDsaKey(&rng, &genKey);
  7984. if (ret != 0) {
  7985. wc_FreeDsaKey(&genKey);
  7986. return -5810;
  7987. }
  7988. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7989. if (der == NULL) {
  7990. wc_FreeDsaKey(&genKey);
  7991. return -5811;
  7992. }
  7993. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7994. if (pem == NULL) {
  7995. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7996. wc_FreeDsaKey(&genKey);
  7997. return -5812;
  7998. }
  7999. derSz = wc_DsaKeyToDer(&genKey, der, FOURK_BUF);
  8000. if (derSz < 0) {
  8001. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8002. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8003. return -5813;
  8004. }
  8005. ret = SaveDerAndPem(der, derSz, pem, FOURK_BUF, keyDerFile,
  8006. keyPemFile, DSA_PRIVATEKEY_TYPE, -5814);
  8007. if (ret != 0) {
  8008. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8009. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8010. wc_FreeDsaKey(&genKey);
  8011. return ret;
  8012. }
  8013. ret = wc_InitDsaKey(&derIn);
  8014. if (ret != 0) {
  8015. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8016. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8017. wc_FreeDsaKey(&genKey);
  8018. return -5819;
  8019. }
  8020. idx = 0;
  8021. ret = wc_DsaPrivateKeyDecode(der, &idx, &derIn, derSz);
  8022. if (ret != 0) {
  8023. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8024. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8025. wc_FreeDsaKey(&derIn);
  8026. wc_FreeDsaKey(&genKey);
  8027. return -5820;
  8028. }
  8029. wc_FreeDsaKey(&derIn);
  8030. wc_FreeDsaKey(&genKey);
  8031. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8032. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8033. }
  8034. #endif /* WOLFSSL_KEY_GEN */
  8035. wc_FreeRng(&rng);
  8036. return 0;
  8037. }
  8038. #endif /* NO_DSA */
  8039. #ifdef WOLFCRYPT_HAVE_SRP
  8040. static int generate_random_salt(byte *buf, word32 size)
  8041. {
  8042. int ret = -5821;
  8043. WC_RNG rng;
  8044. if(NULL == buf || !size)
  8045. return -5822;
  8046. if (buf && size && wc_InitRng_ex(&rng, HEAP_HINT, devId) == 0) {
  8047. ret = wc_RNG_GenerateBlock(&rng, (byte *)buf, size);
  8048. wc_FreeRng(&rng);
  8049. }
  8050. return ret;
  8051. }
  8052. int srp_test(void)
  8053. {
  8054. Srp cli, srv;
  8055. int r;
  8056. byte clientPubKey[80]; /* A */
  8057. byte serverPubKey[80]; /* B */
  8058. word32 clientPubKeySz = 80;
  8059. word32 serverPubKeySz = 80;
  8060. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  8061. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  8062. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  8063. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  8064. byte username[] = "user";
  8065. word32 usernameSz = 4;
  8066. byte password[] = "password";
  8067. word32 passwordSz = 8;
  8068. byte N[] = {
  8069. 0xC9, 0x4D, 0x67, 0xEB, 0x5B, 0x1A, 0x23, 0x46, 0xE8, 0xAB, 0x42, 0x2F,
  8070. 0xC6, 0xA0, 0xED, 0xAE, 0xDA, 0x8C, 0x7F, 0x89, 0x4C, 0x9E, 0xEE, 0xC4,
  8071. 0x2F, 0x9E, 0xD2, 0x50, 0xFD, 0x7F, 0x00, 0x46, 0xE5, 0xAF, 0x2C, 0xF7,
  8072. 0x3D, 0x6B, 0x2F, 0xA2, 0x6B, 0xB0, 0x80, 0x33, 0xDA, 0x4D, 0xE3, 0x22,
  8073. 0xE1, 0x44, 0xE7, 0xA8, 0xE9, 0xB1, 0x2A, 0x0E, 0x46, 0x37, 0xF6, 0x37,
  8074. 0x1F, 0x34, 0xA2, 0x07, 0x1C, 0x4B, 0x38, 0x36, 0xCB, 0xEE, 0xAB, 0x15,
  8075. 0x03, 0x44, 0x60, 0xFA, 0xA7, 0xAD, 0xF4, 0x83
  8076. };
  8077. byte g[] = {
  8078. 0x02
  8079. };
  8080. byte salt[10];
  8081. byte verifier[80];
  8082. word32 v_size = sizeof(verifier);
  8083. /* set as 0's so if second init on srv not called SrpTerm is not on
  8084. * garbage values */
  8085. XMEMSET(&srv, 0, sizeof(Srp));
  8086. XMEMSET(&cli, 0, sizeof(Srp));
  8087. /* generating random salt */
  8088. r = generate_random_salt(salt, sizeof(salt));
  8089. /* client knows username and password. */
  8090. /* server knows N, g, salt and verifier. */
  8091. if (!r) r = wc_SrpInit(&cli, SRP_TYPE_SHA, SRP_CLIENT_SIDE);
  8092. if (!r) r = wc_SrpSetUsername(&cli, username, usernameSz);
  8093. /* loading N, g and salt in advance to generate the verifier. */
  8094. if (!r) r = wc_SrpSetParams(&cli, N, sizeof(N),
  8095. g, sizeof(g),
  8096. salt, sizeof(salt));
  8097. if (!r) r = wc_SrpSetPassword(&cli, password, passwordSz);
  8098. if (!r) r = wc_SrpGetVerifier(&cli, verifier, &v_size);
  8099. /* client sends username to server */
  8100. if (!r) r = wc_SrpInit(&srv, SRP_TYPE_SHA, SRP_SERVER_SIDE);
  8101. if (!r) r = wc_SrpSetUsername(&srv, username, usernameSz);
  8102. if (!r) r = wc_SrpSetParams(&srv, N, sizeof(N),
  8103. g, sizeof(g),
  8104. salt, sizeof(salt));
  8105. if (!r) r = wc_SrpSetVerifier(&srv, verifier, v_size);
  8106. if (!r) r = wc_SrpGetPublic(&srv, serverPubKey, &serverPubKeySz);
  8107. /* server sends N, g, salt and B to client */
  8108. if (!r) r = wc_SrpGetPublic(&cli, clientPubKey, &clientPubKeySz);
  8109. if (!r) r = wc_SrpComputeKey(&cli, clientPubKey, clientPubKeySz,
  8110. serverPubKey, serverPubKeySz);
  8111. if (!r) r = wc_SrpGetProof(&cli, clientProof, &clientProofSz);
  8112. /* client sends A and M1 to server */
  8113. if (!r) r = wc_SrpComputeKey(&srv, clientPubKey, clientPubKeySz,
  8114. serverPubKey, serverPubKeySz);
  8115. if (!r) r = wc_SrpVerifyPeersProof(&srv, clientProof, clientProofSz);
  8116. if (!r) r = wc_SrpGetProof(&srv, serverProof, &serverProofSz);
  8117. /* server sends M2 to client */
  8118. if (!r) r = wc_SrpVerifyPeersProof(&cli, serverProof, serverProofSz);
  8119. wc_SrpTerm(&cli);
  8120. wc_SrpTerm(&srv);
  8121. return r;
  8122. }
  8123. #endif /* WOLFCRYPT_HAVE_SRP */
  8124. #if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
  8125. int openssl_test(void)
  8126. {
  8127. EVP_MD_CTX md_ctx;
  8128. testVector a, b, c, d, e, f;
  8129. byte hash[WC_SHA256_DIGEST_SIZE*2]; /* max size */
  8130. (void)a;
  8131. (void)b;
  8132. (void)c;
  8133. (void)e;
  8134. (void)f;
  8135. /* test malloc / free , 10 is an arbitrary amount of memory chosen */
  8136. {
  8137. byte* p;
  8138. p = (byte*)CRYPTO_malloc(10, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8139. if (p == NULL) {
  8140. return -5900;
  8141. }
  8142. XMEMSET(p, 0, 10);
  8143. CRYPTO_free(p, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8144. }
  8145. #ifndef NO_MD5
  8146. a.input = "1234567890123456789012345678901234567890123456789012345678"
  8147. "9012345678901234567890";
  8148. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  8149. "\x7a";
  8150. a.inLen = XSTRLEN(a.input);
  8151. a.outLen = WC_MD5_DIGEST_SIZE;
  8152. EVP_MD_CTX_init(&md_ctx);
  8153. EVP_DigestInit(&md_ctx, EVP_md5());
  8154. EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  8155. EVP_DigestFinal(&md_ctx, hash, 0);
  8156. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0)
  8157. return -5901;
  8158. #endif /* NO_MD5 */
  8159. #ifndef NO_SHA
  8160. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  8161. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  8162. "aaaaaaaaaa";
  8163. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  8164. "\x53\x99\x5E\x26\xA0";
  8165. b.inLen = XSTRLEN(b.input);
  8166. b.outLen = WC_SHA_DIGEST_SIZE;
  8167. EVP_MD_CTX_init(&md_ctx);
  8168. EVP_DigestInit(&md_ctx, EVP_sha1());
  8169. EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  8170. EVP_DigestFinal(&md_ctx, hash, 0);
  8171. if (XMEMCMP(hash, b.output, WC_SHA_DIGEST_SIZE) != 0)
  8172. return -5902;
  8173. #endif /* NO_SHA */
  8174. #ifdef WOLFSSL_SHA224
  8175. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  8176. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  8177. e.output = "\xc9\x7c\xa9\xa5\x59\x85\x0c\xe9\x7a\x04\xa9\x6d\xef\x6d\x99"
  8178. "\xa9\xe0\xe0\xe2\xab\x14\xe6\xb8\xdf\x26\x5f\xc0\xb3";
  8179. e.inLen = XSTRLEN(e.input);
  8180. e.outLen = WC_SHA224_DIGEST_SIZE;
  8181. EVP_MD_CTX_init(&md_ctx);
  8182. EVP_DigestInit(&md_ctx, EVP_sha224());
  8183. EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  8184. EVP_DigestFinal(&md_ctx, hash, 0);
  8185. if (XMEMCMP(hash, e.output, WC_SHA224_DIGEST_SIZE) != 0)
  8186. return -5903;
  8187. #endif /* WOLFSSL_SHA224 */
  8188. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  8189. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  8190. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  8191. "\x06\xC1";
  8192. d.inLen = XSTRLEN(d.input);
  8193. d.outLen = WC_SHA256_DIGEST_SIZE;
  8194. EVP_MD_CTX_init(&md_ctx);
  8195. EVP_DigestInit(&md_ctx, EVP_sha256());
  8196. EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  8197. EVP_DigestFinal(&md_ctx, hash, 0);
  8198. if (XMEMCMP(hash, d.output, WC_SHA256_DIGEST_SIZE) != 0)
  8199. return -5904;
  8200. #ifdef WOLFSSL_SHA384
  8201. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  8202. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  8203. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  8204. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  8205. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  8206. "\x74\x60\x39";
  8207. e.inLen = XSTRLEN(e.input);
  8208. e.outLen = WC_SHA384_DIGEST_SIZE;
  8209. EVP_MD_CTX_init(&md_ctx);
  8210. EVP_DigestInit(&md_ctx, EVP_sha384());
  8211. EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  8212. EVP_DigestFinal(&md_ctx, hash, 0);
  8213. if (XMEMCMP(hash, e.output, WC_SHA384_DIGEST_SIZE) != 0)
  8214. return -5905;
  8215. #endif /* WOLFSSL_SHA384 */
  8216. #ifdef WOLFSSL_SHA512
  8217. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  8218. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  8219. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  8220. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  8221. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  8222. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  8223. "\x87\x4b\xe9\x09";
  8224. f.inLen = XSTRLEN(f.input);
  8225. f.outLen = WC_SHA512_DIGEST_SIZE;
  8226. EVP_MD_CTX_init(&md_ctx);
  8227. EVP_DigestInit(&md_ctx, EVP_sha512());
  8228. EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  8229. EVP_DigestFinal(&md_ctx, hash, 0);
  8230. if (XMEMCMP(hash, f.output, WC_SHA512_DIGEST_SIZE) != 0)
  8231. return -5906;
  8232. #endif /* WOLFSSL_SHA512 */
  8233. #ifndef NO_MD5
  8234. if (RAND_bytes(hash, sizeof(hash)) != 1)
  8235. return -5907;
  8236. c.input = "what do ya want for nothing?";
  8237. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  8238. "\x76";
  8239. c.inLen = XSTRLEN(c.input);
  8240. c.outLen = WC_MD5_DIGEST_SIZE;
  8241. HMAC(EVP_md5(),
  8242. "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen, hash, 0);
  8243. if (XMEMCMP(hash, c.output, WC_MD5_DIGEST_SIZE) != 0)
  8244. return -5908;
  8245. #endif /* NO_MD5 */
  8246. #ifndef NO_DES3
  8247. { /* des test */
  8248. const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  8249. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  8250. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  8251. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  8252. };
  8253. byte plain[24];
  8254. byte cipher[24];
  8255. const_DES_cblock key =
  8256. {
  8257. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  8258. };
  8259. DES_cblock iv =
  8260. {
  8261. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  8262. };
  8263. DES_key_schedule sched;
  8264. const byte verify[] =
  8265. {
  8266. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  8267. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  8268. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  8269. };
  8270. DES_key_sched(&key, &sched);
  8271. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  8272. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  8273. if (XMEMCMP(plain, vector, sizeof(vector)) != 0)
  8274. return -5909;
  8275. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  8276. return -5910;
  8277. /* test changing iv */
  8278. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  8279. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  8280. if (XMEMCMP(cipher, verify, sizeof(verify)) != 0)
  8281. return -5911;
  8282. } /* end des test */
  8283. #endif /* NO_DES3 */
  8284. #ifndef NO_AES
  8285. { /* evp_cipher test: EVP_aes_128_cbc */
  8286. EVP_CIPHER_CTX ctx;
  8287. int idx, cipherSz, plainSz;
  8288. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  8289. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  8290. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  8291. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  8292. };
  8293. const byte verify[] =
  8294. {
  8295. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  8296. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  8297. 0x3b,0x5d,0x41,0x97,0x94,0x25,0xa4,0xb4,
  8298. 0xae,0x7b,0x34,0xd0,0x3f,0x0c,0xbc,0x06
  8299. };
  8300. const byte verify2[] =
  8301. {
  8302. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  8303. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb,
  8304. 0x7d,0x37,0x7b,0x0b,0x44,0xaa,0xb5,0xf0,
  8305. 0x5f,0x34,0xb4,0xde,0xb5,0xbd,0x2a,0xbb
  8306. };
  8307. byte key[] = "0123456789abcdef "; /* align */
  8308. byte iv[] = "1234567890abcdef "; /* align */
  8309. byte cipher[AES_BLOCK_SIZE * 4];
  8310. byte plain [AES_BLOCK_SIZE * 4];
  8311. EVP_CIPHER_CTX_init(&ctx);
  8312. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  8313. return -5912;
  8314. if (EVP_CipherUpdate(&ctx, cipher, &idx, (byte*)msg, sizeof(msg)) == 0)
  8315. return -5913;
  8316. cipherSz = idx;
  8317. if (EVP_CipherFinal(&ctx, cipher + cipherSz, &idx) == 0)
  8318. return -8107;
  8319. cipherSz += idx;
  8320. if ((cipherSz != (int)sizeof(verify)) &&
  8321. XMEMCMP(cipher, verify, cipherSz))
  8322. return -5914;
  8323. EVP_CIPHER_CTX_init(&ctx);
  8324. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  8325. return -5915;
  8326. if (EVP_CipherUpdate(&ctx, plain, &idx, cipher, cipherSz) == 0)
  8327. return -5916;
  8328. plainSz = idx;
  8329. if (EVP_CipherFinal(&ctx, plain + plainSz, &idx) == 0)
  8330. return -8108;
  8331. plainSz += idx;
  8332. if ((plainSz != sizeof(msg)) || XMEMCMP(plain, msg, sizeof(msg)))
  8333. return -5917;
  8334. EVP_CIPHER_CTX_init(&ctx);
  8335. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  8336. return -8109;
  8337. if (EVP_CipherUpdate(&ctx, cipher, &idx, msg, AES_BLOCK_SIZE) == 0)
  8338. return -8110;
  8339. cipherSz = idx;
  8340. if (EVP_CipherFinal(&ctx, cipher + cipherSz, &idx) == 0)
  8341. return -8111;
  8342. cipherSz += idx;
  8343. if ((cipherSz != (int)sizeof(verify2)) ||
  8344. XMEMCMP(cipher, verify2, cipherSz))
  8345. return -8112;
  8346. } /* end evp_cipher test: EVP_aes_128_cbc*/
  8347. #ifdef HAVE_AES_ECB
  8348. { /* evp_cipher test: EVP_aes_128_ecb*/
  8349. EVP_CIPHER_CTX ctx;
  8350. const byte msg[] =
  8351. {
  8352. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8353. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8354. };
  8355. const byte verify[] =
  8356. {
  8357. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  8358. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  8359. };
  8360. const byte key[] =
  8361. {
  8362. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8363. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8364. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8365. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8366. };
  8367. byte cipher[AES_BLOCK_SIZE * 4];
  8368. byte plain [AES_BLOCK_SIZE * 4];
  8369. EVP_CIPHER_CTX_init(&ctx);
  8370. if (EVP_CipherInit(&ctx, EVP_aes_256_ecb(), (unsigned char*)key,
  8371. NULL, 1) == 0)
  8372. return -5918;
  8373. if (EVP_Cipher(&ctx, cipher, (byte*)msg, 16) == 0)
  8374. return -5919;
  8375. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  8376. return -5920;
  8377. EVP_CIPHER_CTX_init(&ctx);
  8378. if (EVP_CipherInit(&ctx, EVP_aes_256_ecb(), (unsigned char*)key,
  8379. NULL, 0) == 0)
  8380. return -5921;
  8381. if (EVP_Cipher(&ctx, plain, cipher, 16) == 0)
  8382. return -5922;
  8383. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  8384. return -5923;
  8385. } /* end evp_cipher test */
  8386. #endif
  8387. #endif /* NO_AES */
  8388. #define OPENSSL_TEST_ERROR (-10000)
  8389. #ifndef NO_AES
  8390. #ifdef WOLFSSL_AES_DIRECT
  8391. /* enable HAVE_AES_DECRYPT for AES_encrypt/decrypt */
  8392. {
  8393. /* Test: AES_encrypt/decrypt/set Key */
  8394. AES_KEY enc;
  8395. #ifdef HAVE_AES_DECRYPT
  8396. AES_KEY dec;
  8397. #endif
  8398. const byte msg[] =
  8399. {
  8400. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8401. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8402. };
  8403. const byte verify[] =
  8404. {
  8405. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  8406. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  8407. };
  8408. const byte key[] =
  8409. {
  8410. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8411. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8412. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8413. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8414. };
  8415. byte plain[sizeof(msg)];
  8416. byte cipher[sizeof(msg)];
  8417. printf("openSSL extra test\n") ;
  8418. AES_set_encrypt_key(key, sizeof(key)*8, &enc);
  8419. AES_set_decrypt_key(key, sizeof(key)*8, &dec);
  8420. AES_encrypt(msg, cipher, &enc);
  8421. #ifdef HAVE_AES_DECRYPT
  8422. AES_decrypt(cipher, plain, &dec);
  8423. if (XMEMCMP(plain, msg, AES_BLOCK_SIZE))
  8424. return OPENSSL_TEST_ERROR-60;
  8425. #endif /* HAVE_AES_DECRYPT */
  8426. if (XMEMCMP(cipher, verify, AES_BLOCK_SIZE))
  8427. return OPENSSL_TEST_ERROR-61;
  8428. }
  8429. #endif
  8430. /* EVP_Cipher with EVP_aes_xxx_ctr() */
  8431. #ifdef WOLFSSL_AES_COUNTER
  8432. {
  8433. const byte ctrKey[] =
  8434. {
  8435. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  8436. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  8437. };
  8438. const byte ctrIv[] =
  8439. {
  8440. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  8441. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  8442. };
  8443. const byte ctrPlain[] =
  8444. {
  8445. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8446. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8447. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8448. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8449. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8450. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8451. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8452. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8453. };
  8454. const byte ctrCipher[] =
  8455. {
  8456. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  8457. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  8458. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  8459. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  8460. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  8461. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  8462. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  8463. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  8464. };
  8465. byte plainBuff [64];
  8466. byte cipherBuff[64];
  8467. const byte oddCipher[] =
  8468. {
  8469. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  8470. 0xc2
  8471. };
  8472. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  8473. * NIST Special Publication 800-38A */
  8474. const byte ctr192Key[] =
  8475. {
  8476. 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  8477. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  8478. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b
  8479. };
  8480. const byte ctr192Iv[] =
  8481. {
  8482. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  8483. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  8484. };
  8485. const byte ctr192Plain[] =
  8486. {
  8487. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8488. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8489. };
  8490. const byte ctr192Cipher[] =
  8491. {
  8492. 0x1a,0xbc,0x93,0x24,0x17,0x52,0x1c,0xa2,
  8493. 0x4f,0x2b,0x04,0x59,0xfe,0x7e,0x6e,0x0b
  8494. };
  8495. /* test vector from "Recommendation for Block Cipher Modes of Operation"
  8496. * NIST Special Publication 800-38A */
  8497. const byte ctr256Key[] =
  8498. {
  8499. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  8500. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  8501. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  8502. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  8503. };
  8504. const byte ctr256Iv[] =
  8505. {
  8506. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  8507. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  8508. };
  8509. const byte ctr256Plain[] =
  8510. {
  8511. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8512. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  8513. };
  8514. const byte ctr256Cipher[] =
  8515. {
  8516. 0x60,0x1e,0xc3,0x13,0x77,0x57,0x89,0xa5,
  8517. 0xb7,0xa7,0xf5,0x04,0xbb,0xf3,0xd2,0x28
  8518. };
  8519. EVP_CIPHER_CTX en;
  8520. EVP_CIPHER_CTX de;
  8521. EVP_CIPHER_CTX *p_en;
  8522. EVP_CIPHER_CTX *p_de;
  8523. EVP_CIPHER_CTX_init(&en);
  8524. if (EVP_CipherInit(&en, EVP_aes_128_ctr(),
  8525. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  8526. return -5924;
  8527. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctrPlain,
  8528. AES_BLOCK_SIZE*4) == 0)
  8529. return -5925;
  8530. EVP_CIPHER_CTX_init(&de);
  8531. if (EVP_CipherInit(&de, EVP_aes_128_ctr(),
  8532. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  8533. return -5926;
  8534. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff,
  8535. AES_BLOCK_SIZE*4) == 0)
  8536. return -5927;
  8537. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  8538. return -5928;
  8539. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  8540. return -5929;
  8541. p_en = wolfSSL_EVP_CIPHER_CTX_new();
  8542. if(p_en == NULL)return -5930;
  8543. p_de = wolfSSL_EVP_CIPHER_CTX_new();
  8544. if(p_de == NULL)return -5931;
  8545. if (EVP_CipherInit(p_en, EVP_aes_128_ctr(),
  8546. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  8547. return -5932;
  8548. if (EVP_Cipher(p_en, (byte*)cipherBuff, (byte*)ctrPlain,
  8549. AES_BLOCK_SIZE*4) == 0)
  8550. return -5933;
  8551. if (EVP_CipherInit(p_de, EVP_aes_128_ctr(),
  8552. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  8553. return -5934;
  8554. if (EVP_Cipher(p_de, (byte*)plainBuff, (byte*)cipherBuff,
  8555. AES_BLOCK_SIZE*4) == 0)
  8556. return -5935;
  8557. wolfSSL_EVP_CIPHER_CTX_free(p_en);
  8558. wolfSSL_EVP_CIPHER_CTX_free(p_de);
  8559. if (XMEMCMP(cipherBuff, ctrCipher, AES_BLOCK_SIZE*4))
  8560. return -5936;
  8561. if (XMEMCMP(plainBuff, ctrPlain, AES_BLOCK_SIZE*4))
  8562. return -5937;
  8563. EVP_CIPHER_CTX_init(&en);
  8564. if (EVP_CipherInit(&en, EVP_aes_128_ctr(),
  8565. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  8566. return -5938;
  8567. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctrPlain, 9) == 0)
  8568. return -5939;
  8569. EVP_CIPHER_CTX_init(&de);
  8570. if (EVP_CipherInit(&de, EVP_aes_128_ctr(),
  8571. (unsigned char*)ctrKey, (unsigned char*)ctrIv, 0) == 0)
  8572. return -5940;
  8573. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff, 9) == 0)
  8574. return -5941;
  8575. if (XMEMCMP(plainBuff, ctrPlain, 9))
  8576. return -5942;
  8577. if (XMEMCMP(cipherBuff, ctrCipher, 9))
  8578. return -5943;
  8579. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctrPlain, 9) == 0)
  8580. return -5944;
  8581. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff, 9) == 0)
  8582. return -5945;
  8583. if (XMEMCMP(plainBuff, ctrPlain, 9))
  8584. return -5946;
  8585. if (XMEMCMP(cipherBuff, oddCipher, 9))
  8586. return -5947;
  8587. EVP_CIPHER_CTX_init(&en);
  8588. if (EVP_CipherInit(&en, EVP_aes_192_ctr(),
  8589. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  8590. return -5948;
  8591. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctr192Plain,
  8592. AES_BLOCK_SIZE) == 0)
  8593. return -5949;
  8594. EVP_CIPHER_CTX_init(&de);
  8595. if (EVP_CipherInit(&de, EVP_aes_192_ctr(),
  8596. (unsigned char*)ctr192Key, (unsigned char*)ctr192Iv, 0) == 0)
  8597. return -5950;
  8598. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  8599. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff,
  8600. AES_BLOCK_SIZE) == 0)
  8601. return -5951;
  8602. if (XMEMCMP(plainBuff, ctr192Plain, sizeof(ctr192Plain)))
  8603. return -5952;
  8604. if (XMEMCMP(ctr192Cipher, cipherBuff, sizeof(ctr192Cipher)))
  8605. return -5953;
  8606. EVP_CIPHER_CTX_init(&en);
  8607. if (EVP_CipherInit(&en, EVP_aes_256_ctr(),
  8608. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  8609. return -5954;
  8610. if (EVP_Cipher(&en, (byte*)cipherBuff, (byte*)ctr256Plain,
  8611. AES_BLOCK_SIZE) == 0)
  8612. return -5955;
  8613. EVP_CIPHER_CTX_init(&de);
  8614. if (EVP_CipherInit(&de, EVP_aes_256_ctr(),
  8615. (unsigned char*)ctr256Key, (unsigned char*)ctr256Iv, 0) == 0)
  8616. return -5956;
  8617. XMEMSET(plainBuff, 0, sizeof(plainBuff));
  8618. if (EVP_Cipher(&de, (byte*)plainBuff, (byte*)cipherBuff,
  8619. AES_BLOCK_SIZE) == 0)
  8620. return -5957;
  8621. if (XMEMCMP(plainBuff, ctr256Plain, sizeof(ctr256Plain)))
  8622. return -5958;
  8623. if (XMEMCMP(ctr256Cipher, cipherBuff, sizeof(ctr256Cipher)))
  8624. return -5959;
  8625. }
  8626. #endif /* HAVE_AES_COUNTER */
  8627. {
  8628. /* EVP_CipherUpdate test */
  8629. const byte cbcPlain[] =
  8630. {
  8631. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  8632. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  8633. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  8634. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  8635. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  8636. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  8637. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  8638. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  8639. };
  8640. byte key[] = "0123456789abcdef "; /* align */
  8641. byte iv[] = "1234567890abcdef "; /* align */
  8642. byte cipher[AES_BLOCK_SIZE * 4];
  8643. byte plain [AES_BLOCK_SIZE * 4];
  8644. EVP_CIPHER_CTX en;
  8645. EVP_CIPHER_CTX de;
  8646. int outlen ;
  8647. int total = 0;
  8648. EVP_CIPHER_CTX_init(&en);
  8649. if (EVP_CipherInit(&en, EVP_aes_128_cbc(),
  8650. (unsigned char*)key, (unsigned char*)iv, 1) == 0)
  8651. return -5960;
  8652. if (EVP_CipherUpdate(&en, (byte*)cipher, &outlen,
  8653. (byte*)cbcPlain, 9) == 0)
  8654. return -5961;
  8655. if(outlen != 0)
  8656. return -5962;
  8657. total += outlen;
  8658. if (EVP_CipherUpdate(&en, (byte*)&cipher[total], &outlen,
  8659. (byte*)&cbcPlain[9] , 9) == 0)
  8660. return -5963;
  8661. if(outlen != 16)
  8662. return -5964;
  8663. total += outlen;
  8664. if (EVP_CipherFinal(&en, (byte*)&cipher[total], &outlen) == 0)
  8665. return -5965;
  8666. if(outlen != 16)
  8667. return -5966;
  8668. total += outlen;
  8669. if(total != 32)
  8670. return -5967;
  8671. total = 0;
  8672. EVP_CIPHER_CTX_init(&de);
  8673. if (EVP_CipherInit(&de, EVP_aes_128_cbc(),
  8674. (unsigned char*)key, (unsigned char*)iv, 0) == 0)
  8675. return -5968;
  8676. if (EVP_CipherUpdate(&de, (byte*)plain, &outlen, (byte*)cipher, 6) == 0)
  8677. return -5969;
  8678. if(outlen != 0)
  8679. return -5970;
  8680. total += outlen;
  8681. if (EVP_CipherUpdate(&de, (byte*)&plain[total], &outlen,
  8682. (byte*)&cipher[6], 12) == 0)
  8683. return -5971;
  8684. if(outlen != 0)
  8685. total += outlen;
  8686. if (EVP_CipherUpdate(&de, (byte*)&plain[total], &outlen,
  8687. (byte*)&cipher[6+12], 14) == 0)
  8688. return -5972;
  8689. if(outlen != 16)
  8690. return -5973;
  8691. total += outlen;
  8692. if (EVP_CipherFinal(&de, (byte*)&plain[total], &outlen) == 0)
  8693. return -5974;
  8694. if(outlen != 2)
  8695. return -5975;
  8696. total += outlen;
  8697. if(total != 18)
  8698. return -5976;
  8699. if (XMEMCMP(plain, cbcPlain, 18))
  8700. return -5977;
  8701. }
  8702. #endif /* ifndef NO_AES */
  8703. return 0;
  8704. }
  8705. #endif /* OPENSSL_EXTRA && !WOLFCRYPT_ONLY */
  8706. #ifndef NO_PWDBASED
  8707. #ifdef HAVE_SCRYPT
  8708. /* Test vectors taken from RFC 7914: scrypt PBKDF - Section 12. */
  8709. int scrypt_test(void)
  8710. {
  8711. int ret;
  8712. byte derived[64];
  8713. const byte verify1[] = {
  8714. 0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20,
  8715. 0x3b, 0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97,
  8716. 0xf1, 0x6b, 0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8,
  8717. 0xdf, 0xdf, 0xfa, 0x3f, 0xed, 0xe2, 0x14, 0x42,
  8718. 0xfc, 0xd0, 0x06, 0x9d, 0xed, 0x09, 0x48, 0xf8,
  8719. 0x32, 0x6a, 0x75, 0x3a, 0x0f, 0xc8, 0x1f, 0x17,
  8720. 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d, 0x36, 0x28,
  8721. 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89, 0x06
  8722. };
  8723. const byte verify2[] = {
  8724. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  8725. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  8726. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  8727. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  8728. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  8729. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  8730. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  8731. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  8732. };
  8733. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_INTEL_QA)
  8734. const byte verify3[] = {
  8735. 0x70, 0x23, 0xbd, 0xcb, 0x3a, 0xfd, 0x73, 0x48,
  8736. 0x46, 0x1c, 0x06, 0xcd, 0x81, 0xfd, 0x38, 0xeb,
  8737. 0xfd, 0xa8, 0xfb, 0xba, 0x90, 0x4f, 0x8e, 0x3e,
  8738. 0xa9, 0xb5, 0x43, 0xf6, 0x54, 0x5d, 0xa1, 0xf2,
  8739. 0xd5, 0x43, 0x29, 0x55, 0x61, 0x3f, 0x0f, 0xcf,
  8740. 0x62, 0xd4, 0x97, 0x05, 0x24, 0x2a, 0x9a, 0xf9,
  8741. 0xe6, 0x1e, 0x85, 0xdc, 0x0d, 0x65, 0x1e, 0x40,
  8742. 0xdf, 0xcf, 0x01, 0x7b, 0x45, 0x57, 0x58, 0x87
  8743. };
  8744. #endif
  8745. #ifdef SCRYPT_TEST_ALL
  8746. /* Test case is very slow.
  8747. * Use for confirmation after code change or new platform.
  8748. */
  8749. const byte verify4[] = {
  8750. 0x21, 0x01, 0xcb, 0x9b, 0x6a, 0x51, 0x1a, 0xae,
  8751. 0xad, 0xdb, 0xbe, 0x09, 0xcf, 0x70, 0xf8, 0x81,
  8752. 0xec, 0x56, 0x8d, 0x57, 0x4a, 0x2f, 0xfd, 0x4d,
  8753. 0xab, 0xe5, 0xee, 0x98, 0x20, 0xad, 0xaa, 0x47,
  8754. 0x8e, 0x56, 0xfd, 0x8f, 0x4b, 0xa5, 0xd0, 0x9f,
  8755. 0xfa, 0x1c, 0x6d, 0x92, 0x7c, 0x40, 0xf4, 0xc3,
  8756. 0x37, 0x30, 0x40, 0x49, 0xe8, 0xa9, 0x52, 0xfb,
  8757. 0xcb, 0xf4, 0x5c, 0x6f, 0xa7, 0x7a, 0x41, 0xa4
  8758. };
  8759. #endif
  8760. ret = wc_scrypt(derived, NULL, 0, NULL, 0, 4, 1, 1, sizeof(verify1));
  8761. if (ret != 0)
  8762. return -6000;
  8763. if (XMEMCMP(derived, verify1, sizeof(verify1)) != 0)
  8764. return -6001;
  8765. ret = wc_scrypt(derived, (byte*)"password", 8, (byte*)"NaCl", 4, 10, 8, 16,
  8766. sizeof(verify2));
  8767. if (ret != 0)
  8768. return -6002;
  8769. if (XMEMCMP(derived, verify2, sizeof(verify2)) != 0)
  8770. return -6003;
  8771. /* Don't run these test on embedded, since they use large mallocs */
  8772. #if !defined(BENCH_EMBEDDED) && !defined(HAVE_INTEL_QA)
  8773. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  8774. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(verify3));
  8775. if (ret != 0)
  8776. return -6004;
  8777. if (XMEMCMP(derived, verify3, sizeof(verify3)) != 0)
  8778. return -6005;
  8779. #ifdef SCRYPT_TEST_ALL
  8780. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  8781. (byte*)"SodiumChloride", 14, 20, 8, 1, sizeof(verify4));
  8782. if (ret != 0)
  8783. return -6006;
  8784. if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
  8785. return -6007;
  8786. #endif
  8787. #endif /* !BENCH_EMBEDDED && !HAVE_INTEL_QA */
  8788. return 0;
  8789. }
  8790. #endif
  8791. int pkcs12_test(void)
  8792. {
  8793. const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  8794. 0x00, 0x00 };
  8795. const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  8796. const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  8797. 0x00, 0x67, 0x00, 0x00 };
  8798. const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  8799. byte derived[64];
  8800. const byte verify[] = {
  8801. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  8802. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  8803. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  8804. };
  8805. const byte verify2[] = {
  8806. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  8807. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  8808. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  8809. };
  8810. int id = 1;
  8811. int kLen = 24;
  8812. int iterations = 1;
  8813. int ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  8814. iterations, kLen, WC_SHA256, id);
  8815. if (ret < 0)
  8816. return -6100;
  8817. if ( (ret = XMEMCMP(derived, verify, kLen)) != 0)
  8818. return -6101;
  8819. iterations = 1000;
  8820. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  8821. iterations, kLen, WC_SHA256, id);
  8822. if (ret < 0)
  8823. return -6102;
  8824. ret = wc_PKCS12_PBKDF_ex(derived, passwd2, sizeof(passwd2), salt2, 8,
  8825. iterations, kLen, WC_SHA256, id, HEAP_HINT);
  8826. if (ret < 0)
  8827. return -6103;
  8828. if ( (ret = XMEMCMP(derived, verify2, 24)) != 0)
  8829. return -6104;
  8830. return 0;
  8831. }
  8832. int pbkdf2_test(void)
  8833. {
  8834. char passwd[] = "passwordpassword";
  8835. const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  8836. int iterations = 2048;
  8837. int kLen = 24;
  8838. byte derived[64];
  8839. const byte verify[] = {
  8840. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  8841. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  8842. };
  8843. int ret = wc_PBKDF2(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt, 8,
  8844. iterations, kLen, WC_SHA256);
  8845. if (ret != 0)
  8846. return ret;
  8847. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  8848. return -6200;
  8849. return 0;
  8850. }
  8851. #ifndef NO_SHA
  8852. int pbkdf1_test(void)
  8853. {
  8854. char passwd[] = "password";
  8855. const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  8856. int iterations = 1000;
  8857. int kLen = 16;
  8858. byte derived[16];
  8859. const byte verify[] = {
  8860. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F, 0xAF, 0x10, 0xEB, 0xFB,
  8861. 0x4A, 0x3D, 0x2A, 0x20
  8862. };
  8863. wc_PBKDF1(derived, (byte*)passwd, (int)XSTRLEN(passwd), salt, 8, iterations,
  8864. kLen, WC_SHA);
  8865. if (XMEMCMP(derived, verify, sizeof(verify)) != 0)
  8866. return -6300;
  8867. return 0;
  8868. }
  8869. #endif
  8870. int pwdbased_test(void)
  8871. {
  8872. int ret = 0;
  8873. #ifndef NO_SHA
  8874. ret += pbkdf1_test();
  8875. #endif
  8876. ret += pbkdf2_test();
  8877. ret += pkcs12_test();
  8878. #ifdef HAVE_SCRYPT
  8879. ret += scrypt_test();
  8880. #endif
  8881. return ret;
  8882. }
  8883. #endif /* NO_PWDBASED */
  8884. #if defined(HAVE_HKDF) && (!defined(NO_SHA) || !defined(NO_SHA256))
  8885. int hkdf_test(void)
  8886. {
  8887. int ret;
  8888. int L = 42;
  8889. byte okm1[42];
  8890. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  8891. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  8892. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  8893. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  8894. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  8895. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  8896. 0xf8, 0xf9 };
  8897. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  8898. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  8899. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  8900. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  8901. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  8902. 0x49, 0x18 };
  8903. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  8904. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  8905. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  8906. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  8907. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  8908. 0xf8, 0x96 };
  8909. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  8910. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  8911. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  8912. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  8913. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  8914. 0x96, 0xc8 };
  8915. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  8916. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  8917. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  8918. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  8919. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  8920. 0x58, 0x65 };
  8921. (void)res1;
  8922. (void)res2;
  8923. (void)res3;
  8924. (void)res4;
  8925. (void)salt1;
  8926. (void)info1;
  8927. #ifndef NO_SHA
  8928. ret = wc_HKDF(WC_SHA, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
  8929. if (ret != 0)
  8930. return -6400;
  8931. if (XMEMCMP(okm1, res1, L) != 0)
  8932. return -6401;
  8933. #ifndef HAVE_FIPS
  8934. /* fips can't have key size under 14 bytes, salt is key too */
  8935. ret = wc_HKDF(WC_SHA, ikm1, 11, salt1, 13, info1, 10, okm1, L);
  8936. if (ret != 0)
  8937. return -6402;
  8938. if (XMEMCMP(okm1, res2, L) != 0)
  8939. return -6403;
  8940. #endif /* HAVE_FIPS */
  8941. #endif /* NO_SHA */
  8942. #ifndef NO_SHA256
  8943. ret = wc_HKDF(WC_SHA256, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
  8944. if (ret != 0)
  8945. return -6404;
  8946. if (XMEMCMP(okm1, res3, L) != 0)
  8947. return -6405;
  8948. #ifndef HAVE_FIPS
  8949. /* fips can't have key size under 14 bytes, salt is key too */
  8950. ret = wc_HKDF(WC_SHA256, ikm1, 22, salt1, 13, info1, 10, okm1, L);
  8951. if (ret != 0)
  8952. return -6406;
  8953. if (XMEMCMP(okm1, res4, L) != 0)
  8954. return -6407;
  8955. #endif /* HAVE_FIPS */
  8956. #endif /* NO_SHA256 */
  8957. return 0;
  8958. }
  8959. #endif /* HAVE_HKDF */
  8960. #if defined(HAVE_ECC) && defined(HAVE_X963_KDF)
  8961. int x963kdf_test(void)
  8962. {
  8963. int ret;
  8964. byte kek[128];
  8965. #ifndef NO_SHA
  8966. /* SHA-1, COUNT = 0
  8967. * shared secret length: 192
  8968. * SharedInfo length: 0
  8969. * key data length: 128
  8970. */
  8971. const byte Z[] = {
  8972. 0x1c, 0x7d, 0x7b, 0x5f, 0x05, 0x97, 0xb0, 0x3d,
  8973. 0x06, 0xa0, 0x18, 0x46, 0x6e, 0xd1, 0xa9, 0x3e,
  8974. 0x30, 0xed, 0x4b, 0x04, 0xdc, 0x64, 0xcc, 0xdd
  8975. };
  8976. const byte verify[] = {
  8977. 0xbf, 0x71, 0xdf, 0xfd, 0x8f, 0x4d, 0x99, 0x22,
  8978. 0x39, 0x36, 0xbe, 0xb4, 0x6f, 0xee, 0x8c, 0xcc
  8979. };
  8980. #endif
  8981. #ifndef NO_SHA256
  8982. /* SHA-256, COUNT = 3
  8983. * shared secret length: 192
  8984. * SharedInfo length: 0
  8985. * key data length: 128
  8986. */
  8987. const byte Z2[] = {
  8988. 0xd3, 0x8b, 0xdb, 0xe5, 0xc4, 0xfc, 0x16, 0x4c,
  8989. 0xdd, 0x96, 0x7f, 0x63, 0xc0, 0x4f, 0xe0, 0x7b,
  8990. 0x60, 0xcd, 0xe8, 0x81, 0xc2, 0x46, 0x43, 0x8c
  8991. };
  8992. const byte verify2[] = {
  8993. 0x5e, 0x67, 0x4d, 0xb9, 0x71, 0xba, 0xc2, 0x0a,
  8994. 0x80, 0xba, 0xd0, 0xd4, 0x51, 0x4d, 0xc4, 0x84
  8995. };
  8996. #endif
  8997. #ifdef WOLFSSL_SHA512
  8998. /* SHA-512, COUNT = 0
  8999. * shared secret length: 192
  9000. * SharedInfo length: 0
  9001. * key data length: 128
  9002. */
  9003. const byte Z3[] = {
  9004. 0x87, 0xfc, 0x0d, 0x8c, 0x44, 0x77, 0x48, 0x5b,
  9005. 0xb5, 0x74, 0xf5, 0xfc, 0xea, 0x26, 0x4b, 0x30,
  9006. 0x88, 0x5d, 0xc8, 0xd9, 0x0a, 0xd8, 0x27, 0x82
  9007. };
  9008. const byte verify3[] = {
  9009. 0x94, 0x76, 0x65, 0xfb, 0xb9, 0x15, 0x21, 0x53,
  9010. 0xef, 0x46, 0x02, 0x38, 0x50, 0x6a, 0x02, 0x45
  9011. };
  9012. /* SHA-512, COUNT = 0
  9013. * shared secret length: 521
  9014. * SharedInfo length: 128
  9015. * key data length: 1024
  9016. */
  9017. const byte Z4[] = {
  9018. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89,
  9019. 0xfa, 0x58, 0xce, 0xad, 0xc0, 0x47, 0x19, 0x7f,
  9020. 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  9021. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8,
  9022. 0x15, 0x07, 0x39, 0x2f, 0x1a, 0x86, 0xdd, 0xfd,
  9023. 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  9024. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47,
  9025. 0x47, 0xa9, 0xe2, 0xe7, 0x53, 0xf5, 0x5e, 0xf0,
  9026. 0x5a, 0x2d
  9027. };
  9028. const byte info4[] = {
  9029. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d,
  9030. 0x2b, 0x3a, 0x2f, 0x99, 0x37, 0x89, 0x5d, 0x31
  9031. };
  9032. const byte verify4[] = {
  9033. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76,
  9034. 0x9b, 0x52, 0x26, 0x4b, 0x01, 0x12, 0xb5, 0x85,
  9035. 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  9036. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d,
  9037. 0x6e, 0x4d, 0xd2, 0xa5, 0x99, 0xac, 0xce, 0xb3,
  9038. 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  9039. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50,
  9040. 0xa5, 0xa8, 0xee, 0xee, 0x20, 0x80, 0x02, 0x26,
  9041. 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  9042. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d,
  9043. 0xea, 0x9c, 0x4f, 0xf7, 0x45, 0xa2, 0x5a, 0xc2,
  9044. 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  9045. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc,
  9046. 0x0a, 0xb2, 0xaa, 0x8e, 0x2e, 0xfa, 0x7b, 0x17,
  9047. 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  9048. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  9049. };
  9050. #endif
  9051. #ifndef NO_SHA
  9052. ret = wc_X963_KDF(WC_HASH_TYPE_SHA, Z, sizeof(Z), NULL, 0,
  9053. kek, sizeof(verify));
  9054. if (ret != 0)
  9055. return -6500;
  9056. if (XMEMCMP(verify, kek, sizeof(verify)) != 0)
  9057. return -6501;
  9058. #endif
  9059. #ifndef NO_SHA256
  9060. ret = wc_X963_KDF(WC_HASH_TYPE_SHA256, Z2, sizeof(Z2), NULL, 0,
  9061. kek, sizeof(verify2));
  9062. if (ret != 0)
  9063. return -6502;
  9064. if (XMEMCMP(verify2, kek, sizeof(verify2)) != 0)
  9065. return -6503;
  9066. #endif
  9067. #ifdef WOLFSSL_SHA512
  9068. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z3, sizeof(Z3), NULL, 0,
  9069. kek, sizeof(verify3));
  9070. if (ret != 0)
  9071. return -6504;
  9072. if (XMEMCMP(verify3, kek, sizeof(verify3)) != 0)
  9073. return -6505;
  9074. ret = wc_X963_KDF(WC_HASH_TYPE_SHA512, Z4, sizeof(Z4), info4,
  9075. sizeof(info4), kek, sizeof(verify4));
  9076. if (ret != 0)
  9077. return -6506;
  9078. if (XMEMCMP(verify4, kek, sizeof(verify4)) != 0)
  9079. return -6507;
  9080. #endif
  9081. return 0;
  9082. }
  9083. #endif /* HAVE_X963_KDF */
  9084. #ifdef HAVE_ECC
  9085. #ifdef BENCH_EMBEDDED
  9086. #define ECC_SHARED_SIZE 128
  9087. #else
  9088. #define ECC_SHARED_SIZE 1024
  9089. #endif
  9090. #define ECC_DIGEST_SIZE MAX_ECC_BYTES
  9091. #define ECC_SIG_SIZE ECC_MAX_SIG_SIZE
  9092. #ifndef NO_ECC_VECTOR_TEST
  9093. #if (defined(HAVE_ECC192) || defined(HAVE_ECC224) ||\
  9094. !defined(NO_ECC256) || defined(HAVE_ECC384) ||\
  9095. defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES))
  9096. #define HAVE_ECC_VECTOR_TEST
  9097. #endif
  9098. #endif
  9099. #ifdef HAVE_ECC_VECTOR_TEST
  9100. typedef struct eccVector {
  9101. const char* msg; /* SHA-1 Encoded Message */
  9102. const char* Qx;
  9103. const char* Qy;
  9104. const char* d; /* Private Key */
  9105. const char* R;
  9106. const char* S;
  9107. const char* curveName;
  9108. word32 msgLen;
  9109. word32 keySize;
  9110. } eccVector;
  9111. static int ecc_test_vector_item(const eccVector* vector)
  9112. {
  9113. int ret = 0, verify;
  9114. word32 x;
  9115. ecc_key userA;
  9116. DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  9117. wc_ecc_init_ex(&userA, HEAP_HINT, devId);
  9118. XMEMSET(sig, 0, ECC_SIG_SIZE);
  9119. x = ECC_SIG_SIZE;
  9120. ret = wc_ecc_import_raw(&userA, vector->Qx, vector->Qy,
  9121. vector->d, vector->curveName);
  9122. if (ret != 0)
  9123. goto done;
  9124. ret = wc_ecc_rs_to_sig(vector->R, vector->S, sig, &x);
  9125. if (ret != 0)
  9126. goto done;
  9127. do {
  9128. #if defined(WOLFSSL_ASYNC_CRYPT)
  9129. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9130. #endif
  9131. if (ret >= 0) {
  9132. ret = wc_ecc_verify_hash(sig, x, (byte*)vector->msg, vector->msgLen,
  9133. &verify, &userA);
  9134. }
  9135. } while (ret == WC_PENDING_E);
  9136. if (ret != 0)
  9137. goto done;
  9138. if (verify != 1)
  9139. ret = -6508;
  9140. done:
  9141. wc_ecc_free(&userA);
  9142. FREE_VAR(sig, HEAP_HINT);
  9143. return ret;
  9144. }
  9145. static int ecc_test_vector(int keySize)
  9146. {
  9147. int ret;
  9148. eccVector vec;
  9149. XMEMSET(&vec, 0, sizeof(vec));
  9150. vec.keySize = (word32)keySize;
  9151. switch(keySize) {
  9152. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  9153. case 14:
  9154. return 0;
  9155. #endif /* HAVE_ECC112 */
  9156. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  9157. case 16:
  9158. return 0;
  9159. #endif /* HAVE_ECC128 */
  9160. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  9161. case 20:
  9162. return 0;
  9163. #endif /* HAVE_ECC160 */
  9164. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  9165. case 24:
  9166. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  9167. #if 1
  9168. vec.msg = "\x60\x80\x79\x42\x3f\x12\x42\x1d\xe6\x16\xb7\x49\x3e\xbe\x55\x1c\xf4\xd6\x5b\x92";
  9169. vec.msgLen = 20;
  9170. #else
  9171. /* This is the raw message prior to SHA-1 */
  9172. vec.msg =
  9173. "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a\x6e\x6b"
  9174. "\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d\xf3\x46\x4b\xa4"
  9175. "\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b\xb5\x98\x6f\xa1\xa2\xaf"
  9176. "\x04\x8a\x0e\x14\xdc\x51\xe5\x26\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a"
  9177. "\xc6\xf1\xa7\xf1\x56\xce\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd"
  9178. "\x7a\x20\x93\xe2\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5"
  9179. "\x9c\x78\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  9180. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0\x71\xcf";
  9181. vec.msgLen = 128;
  9182. #endif
  9183. vec.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  9184. vec.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  9185. vec.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  9186. vec.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  9187. vec.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  9188. vec.curveName = "SECP192R1";
  9189. break;
  9190. #endif /* HAVE_ECC192 */
  9191. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  9192. case 28:
  9193. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  9194. #if 1
  9195. vec.msg = "\xb9\xa3\xb8\x6d\xb0\xba\x99\xfd\xc6\xd2\x94\x6b\xfe\xbe\x9c\xe8\x3f\x10\x74\xfc";
  9196. vec.msgLen = 20;
  9197. #else
  9198. /* This is the raw message prior to SHA-1 */
  9199. vec.msg =
  9200. "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e\x20\x47"
  9201. "\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69\xe0\x63\x0f\x99"
  9202. "\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28\x27\xb6\x95\xfc\x2d\x64"
  9203. "\xd9\x13\x8b\x1c\xf4\xc1\x21\x55\x89\x4c\x42\x13\x21\xa7\xbb\x97"
  9204. "\x0b\xdc\xe0\xfb\xf0\xd2\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf"
  9205. "\xe3\xff\x8d\xea\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e"
  9206. "\x26\xc1\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  9207. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7\x1c\x20";
  9208. vec.msgLen = 128;
  9209. #endif
  9210. vec.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  9211. vec.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  9212. vec.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  9213. vec.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  9214. vec.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  9215. vec.curveName = "SECP224R1";
  9216. break;
  9217. #endif /* HAVE_ECC224 */
  9218. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  9219. case 30:
  9220. return 0;
  9221. #endif /* HAVE_ECC239 */
  9222. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  9223. case 32:
  9224. /* first [P-256,SHA-1] vector from FIPS 186-3 NIST vectors */
  9225. #if 1
  9226. vec.msg = "\xa3\xf9\x1a\xe2\x1b\xa6\xb3\x03\x98\x64\x47\x2f\x18\x41\x44\xc6\xaf\x62\xcd\x0e";
  9227. vec.msgLen = 20;
  9228. #else
  9229. /* This is the raw message prior to SHA-1 */
  9230. vec.msg =
  9231. "\xa2\x4b\x21\x76\x2e\x6e\xdb\x15\x3c\xc1\x14\x38\xdb\x0e\x92\xcd"
  9232. "\xf5\x2b\x86\xb0\x6c\xa9\x70\x16\x06\x27\x59\xc7\x0d\x36\xd1\x56"
  9233. "\x2c\xc9\x63\x0d\x7f\xc7\xc7\x74\xb2\x8b\x54\xe3\x1e\xf5\x58\x72"
  9234. "\xb2\xa6\x5d\xf1\xd7\xec\x26\xde\xbb\x33\xe7\xd9\x27\xef\xcc\xf4"
  9235. "\x6b\x63\xde\x52\xa4\xf4\x31\xea\xca\x59\xb0\x5d\x2e\xde\xc4\x84"
  9236. "\x5f\xff\xc0\xee\x15\x03\x94\xd6\x1f\x3d\xfe\xcb\xcd\xbf\x6f\x5a"
  9237. "\x73\x38\xd0\xbe\x3f\x2a\x77\x34\x51\x98\x3e\xba\xeb\x48\xf6\x73"
  9238. "\x8f\xc8\x95\xdf\x35\x7e\x1a\x48\xa6\x53\xbb\x35\x5a\x31\xa1\xb4"
  9239. vec.msgLen = 128;
  9240. #endif
  9241. vec.Qx = "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  9242. vec.Qy = "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  9243. vec.d = "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  9244. vec.R = "2b826f5d44e2d0b6de531ad96b51e8f0c56fdfead3c236892e4d84eacfc3b75c";
  9245. vec.S = "a2248b62c03db35a7cd63e8a120a3521a89d3d2f61ff99035a2148ae32e3a248";
  9246. vec.curveName = "SECP256R1";
  9247. break;
  9248. #endif /* !NO_ECC256 */
  9249. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  9250. case 40:
  9251. return 0;
  9252. #endif /* HAVE_ECC320 */
  9253. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  9254. case 48:
  9255. /* first [P-384,SHA-1] vector from FIPS 186-3 NIST vectors */
  9256. #if 1
  9257. vec.msg = "\x9b\x9f\x8c\x95\x35\xa5\xca\x26\x60\x5d\xb7\xf2\xfa\x57\x3b\xdf\xc3\x2e\xab\x8b";
  9258. vec.msgLen = 20;
  9259. #else
  9260. /* This is the raw message prior to SHA-1 */
  9261. vec.msg =
  9262. "\xab\xe1\x0a\xce\x13\xe7\xe1\xd9\x18\x6c\x48\xf7\x88\x9d\x51\x47"
  9263. "\x3d\x3a\x09\x61\x98\x4b\xc8\x72\xdf\x70\x8e\xcc\x3e\xd3\xb8\x16"
  9264. "\x9d\x01\xe3\xd9\x6f\xc4\xf1\xd5\xea\x00\xa0\x36\x92\xbc\xc5\xcf"
  9265. "\xfd\x53\x78\x7c\x88\xb9\x34\xaf\x40\x4c\x03\x9d\x32\x89\xb5\xba"
  9266. "\xc5\xae\x7d\xb1\x49\x68\x75\xb5\xdc\x73\xc3\x09\xf9\x25\xc1\x3d"
  9267. "\x1c\x01\xab\xda\xaf\xeb\xcd\xac\x2c\xee\x43\x39\x39\xce\x8d\x4a"
  9268. "\x0a\x5d\x57\xbb\x70\x5f\x3b\xf6\xec\x08\x47\x95\x11\xd4\xb4\xa3"
  9269. "\x21\x1f\x61\x64\x9a\xd6\x27\x43\x14\xbf\x0d\x43\x8a\x81\xe0\x60"
  9270. vec.msgLen = 128;
  9271. #endif
  9272. vec.Qx = "e55fee6c49d8d523f5ce7bf9c0425ce4ff650708b7de5cfb095901523979a7f042602db30854735369813b5c3f5ef868";
  9273. vec.Qy = "28f59cc5dc509892a988d38a8e2519de3d0c4fd0fbdb0993e38f18506c17606c5e24249246f1ce94983a5361c5be983e";
  9274. vec.d = "a492ce8fa90084c227e1a32f7974d39e9ff67a7e8705ec3419b35fb607582bebd461e0b1520ac76ec2dd4e9b63ebae71";
  9275. vec.R = "6820b8585204648aed63bdff47f6d9acebdea62944774a7d14f0e14aa0b9a5b99545b2daee6b3c74ebf606667a3f39b7";
  9276. vec.S = "491af1d0cccd56ddd520b233775d0bc6b40a6255cc55207d8e9356741f23c96c14714221078dbd5c17f4fdd89b32a907";
  9277. vec.curveName = "SECP384R1";
  9278. break;
  9279. #endif /* HAVE_ECC384 */
  9280. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  9281. case 64:
  9282. return 0;
  9283. #endif /* HAVE_ECC512 */
  9284. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  9285. case 66:
  9286. /* first [P-521,SHA-1] vector from FIPS 186-3 NIST vectors */
  9287. #if 1
  9288. vec.msg = "\x1b\xf7\x03\x9c\xca\x23\x94\x27\x3f\x11\xa1\xd4\x8d\xcc\xb4\x46\x6f\x31\x61\xdf";
  9289. vec.msgLen = 20;
  9290. #else
  9291. /* This is the raw message prior to SHA-1 */
  9292. vec.msg =
  9293. "\x50\x3f\x79\x39\x34\x0a\xc7\x23\xcd\x4a\x2f\x4e\x6c\xcc\x27\x33"
  9294. "\x38\x3a\xca\x2f\xba\x90\x02\x19\x9d\x9e\x1f\x94\x8b\xe0\x41\x21"
  9295. "\x07\xa3\xfd\xd5\x14\xd9\x0c\xd4\xf3\x7c\xc3\xac\x62\xef\x00\x3a"
  9296. "\x2d\xb1\xd9\x65\x7a\xb7\x7f\xe7\x55\xbf\x71\xfa\x59\xe4\xd9\x6e"
  9297. "\xa7\x2a\xe7\xbf\x9d\xe8\x7d\x79\x34\x3b\xc1\xa4\xbb\x14\x4d\x16"
  9298. "\x28\xd1\xe9\xe9\xc8\xed\x80\x8b\x96\x2c\x54\xe5\xf9\x6d\x53\xda"
  9299. "\x14\x7a\x96\x38\xf9\x4a\x91\x75\xd8\xed\x61\x05\x5f\x0b\xa5\x73"
  9300. "\xa8\x2b\xb7\xe0\x18\xee\xda\xc4\xea\x7b\x36\x2e\xc8\x9c\x38\x2b"
  9301. vec.msgLen = 128;
  9302. #endif
  9303. vec.Qx = "12fbcaeffa6a51f3ee4d3d2b51c5dec6d7c726ca353fc014ea2bf7cfbb9b910d32cbfa6a00fe39b6cdb8946f22775398b2e233c0cf144d78c8a7742b5c7a3bb5d23";
  9304. vec.Qy = "09cdef823dd7bf9a79e8cceacd2e4527c231d0ae5967af0958e931d7ddccf2805a3e618dc3039fec9febbd33052fe4c0fee98f033106064982d88f4e03549d4a64d";
  9305. vec.d = "1bd56bd106118eda246155bd43b42b8e13f0a6e25dd3bb376026fab4dc92b6157bc6dfec2d15dd3d0cf2a39aa68494042af48ba9601118da82c6f2108a3a203ad74";
  9306. vec.R = "0bd117b4807710898f9dd7778056485777668f0e78e6ddf5b000356121eb7a220e9493c7f9a57c077947f89ac45d5acb6661bbcd17abb3faea149ba0aa3bb1521be";
  9307. vec.S = "019cd2c5c3f9870ecdeb9b323abdf3a98cd5e231d85c6ddc5b71ab190739f7f226e6b134ba1d5889ddeb2751dabd97911dff90c34684cdbe7bb669b6c3d22f2480c";
  9308. vec.curveName = "SECP521R1";
  9309. break;
  9310. #endif /* HAVE_ECC521 */
  9311. default:
  9312. return NOT_COMPILED_IN; /* Invalid key size / Not supported */
  9313. }; /* Switch */
  9314. ret = ecc_test_vector_item(&vec);
  9315. if (ret < 0) {
  9316. return ret;
  9317. }
  9318. return 0;
  9319. }
  9320. #ifdef HAVE_ECC_CDH
  9321. static int ecc_test_cdh_vectors(void)
  9322. {
  9323. int ret;
  9324. ecc_key pub_key, priv_key;
  9325. byte sharedA[32] = {0}, sharedB[32] = {0};
  9326. word32 x, z;
  9327. const char* QCAVSx = "700c48f77f56584c5cc632ca65640db91b6bacce3a4df6b42ce7cc838833d287";
  9328. const char* QCAVSy = "db71e509e3fd9b060ddb20ba5c51dcc5948d46fbf640dfe0441782cab85fa4ac";
  9329. const char* dIUT = "7d7dc5f71eb29ddaf80d6214632eeae03d9058af1fb6d22ed80badb62bc1a534";
  9330. const char* QIUTx = "ead218590119e8876b29146ff89ca61770c4edbbf97d38ce385ed281d8a6b230";
  9331. const char* QIUTy = "28af61281fd35e2fa7002523acc85a429cb06ee6648325389f59edfce1405141";
  9332. const char* ZIUT = "46fc62106420ff012e54a434fbdd2d25ccc5852060561e68040dd7778997bd7b";
  9333. /* setup private and public keys */
  9334. ret = wc_ecc_init(&pub_key);
  9335. if (ret != 0)
  9336. return ret;
  9337. ret = wc_ecc_init(&priv_key);
  9338. if (ret != 0) {
  9339. wc_ecc_free(&pub_key);
  9340. goto done;
  9341. }
  9342. wc_ecc_set_flags(&pub_key, WC_ECC_FLAG_COFACTOR);
  9343. wc_ecc_set_flags(&priv_key, WC_ECC_FLAG_COFACTOR);
  9344. ret = wc_ecc_import_raw(&pub_key, QCAVSx, QCAVSy, NULL, "SECP256R1");
  9345. if (ret != 0)
  9346. goto done;
  9347. ret = wc_ecc_import_raw(&priv_key, QIUTx, QIUTy, dIUT, "SECP256R1");
  9348. if (ret != 0)
  9349. goto done;
  9350. /* compute ECC Cofactor shared secret */
  9351. x = sizeof(sharedA);
  9352. ret = wc_ecc_shared_secret(&priv_key, &pub_key, sharedA, &x);
  9353. if (ret != 0) {
  9354. goto done;
  9355. }
  9356. /* read in expected Z */
  9357. z = sizeof(sharedB);
  9358. ret = Base16_Decode((const byte*)ZIUT, (word32)XSTRLEN(ZIUT), sharedB, &z);
  9359. if (ret != 0)
  9360. goto done;
  9361. /* compare results */
  9362. if (x != z || XMEMCMP(sharedA, sharedB, x)) {
  9363. ERROR_OUT(-6509, done);
  9364. }
  9365. done:
  9366. wc_ecc_free(&priv_key);
  9367. wc_ecc_free(&pub_key);
  9368. return ret;
  9369. }
  9370. #endif /* HAVE_ECC_CDH */
  9371. #endif /* HAVE_ECC_VECTOR_TEST */
  9372. #ifdef HAVE_ECC_KEY_IMPORT
  9373. /* returns 0 on success */
  9374. static int ecc_test_make_pub(WC_RNG* rng)
  9375. {
  9376. ecc_key key;
  9377. unsigned char* exportBuf;
  9378. unsigned char* tmp;
  9379. unsigned char msg[] = "test wolfSSL ECC public gen";
  9380. word32 x, tmpSz;
  9381. int ret = 0;
  9382. ecc_point* pubPoint = NULL;
  9383. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  9384. ecc_key pub;
  9385. #endif
  9386. #ifdef HAVE_ECC_VERIFY
  9387. int verify = 0;
  9388. #endif
  9389. tmp = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9390. if (tmp == NULL) {
  9391. return -6810;
  9392. }
  9393. exportBuf = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9394. if (exportBuf == NULL) {
  9395. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9396. return -6811;
  9397. }
  9398. #ifdef USE_CERT_BUFFERS_256
  9399. XMEMCPY(tmp, ecc_key_der_256, (size_t)sizeof_ecc_key_der_256);
  9400. tmpSz = (size_t)sizeof_ecc_key_der_256;
  9401. #else
  9402. FILE* file;
  9403. file = fopen(eccKeyDerFile, "rb");
  9404. if (!file) {
  9405. ERROR_OUT(-6812, done);
  9406. }
  9407. tmpSz = (word32)fread(tmp, 1, FOURK_BUF, file);
  9408. fclose(file);
  9409. #endif /* USE_CERT_BUFFERS_256 */
  9410. wc_ecc_init(&key);
  9411. /* import private only then test with */
  9412. ret = wc_ecc_import_private_key(tmp, tmpSz, NULL, 0, NULL);
  9413. if (ret == 0) {
  9414. ERROR_OUT(-6813, done);
  9415. }
  9416. ret = wc_ecc_import_private_key(NULL, tmpSz, NULL, 0, &key);
  9417. if (ret == 0) {
  9418. ERROR_OUT(-6814, done);
  9419. }
  9420. x = 0;
  9421. ret = wc_EccPrivateKeyDecode(tmp, &x, &key, tmpSz);
  9422. if (ret != 0) {
  9423. ERROR_OUT(-6815, done);
  9424. }
  9425. #ifdef HAVE_ECC_KEY_EXPORT
  9426. x = FOURK_BUF;
  9427. ret = wc_ecc_export_private_only(&key, exportBuf, &x);
  9428. if (ret != 0) {
  9429. ERROR_OUT(-6816, done);
  9430. }
  9431. /* make private only key */
  9432. wc_ecc_free(&key);
  9433. wc_ecc_init(&key);
  9434. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, &key);
  9435. if (ret != 0) {
  9436. ERROR_OUT(-6817, done);
  9437. }
  9438. x = FOURK_BUF;
  9439. ret = wc_ecc_export_x963_ex(&key, exportBuf, &x, 0);
  9440. if (ret == 0) {
  9441. ERROR_OUT(-6818, done);
  9442. }
  9443. #endif /* HAVE_ECC_KEY_EXPORT */
  9444. ret = wc_ecc_make_pub(NULL, NULL);
  9445. if (ret == 0) {
  9446. ERROR_OUT(-6819, done);
  9447. }
  9448. pubPoint = wc_ecc_new_point_h(HEAP_HINT);
  9449. if (pubPoint == NULL) {
  9450. ERROR_OUT(-6820, done);
  9451. }
  9452. ret = wc_ecc_make_pub(&key, pubPoint);
  9453. if (ret != 0) {
  9454. ERROR_OUT(-6821, done);
  9455. }
  9456. #ifdef HAVE_ECC_KEY_EXPORT
  9457. /* export should still fail, is private only key */
  9458. x = FOURK_BUF;
  9459. ret = wc_ecc_export_x963_ex(&key, exportBuf, &x, 0);
  9460. if (ret == 0) {
  9461. ERROR_OUT(-6822, done);
  9462. }
  9463. #endif /* HAVE_ECC_KEY_EXPORT */
  9464. #ifdef HAVE_ECC_SIGN
  9465. tmpSz = FOURK_BUF;
  9466. ret = wc_ecc_sign_hash(msg, sizeof(msg), tmp, &tmpSz, rng, &key);
  9467. if (ret != 0) {
  9468. ERROR_OUT(-6823, done);
  9469. }
  9470. #ifdef HAVE_ECC_VERIFY
  9471. /* try verify with private only key */
  9472. ret = wc_ecc_verify_hash(tmp, tmpSz, msg, sizeof(msg), &verify, &key);
  9473. if (ret != 0) {
  9474. ERROR_OUT(-6824, done);
  9475. }
  9476. if (verify != 1) {
  9477. ERROR_OUT(-6825, done);
  9478. }
  9479. #ifdef HAVE_ECC_KEY_EXPORT
  9480. /* exporting the public part should now work */
  9481. x = FOURK_BUF;
  9482. ret = wc_ecc_export_x963_ex(&key, exportBuf, &x, 0);
  9483. if (ret != 0) {
  9484. ERROR_OUT(-6826, done);
  9485. }
  9486. #endif /* HAVE_ECC_KEY_EXPORT */
  9487. #endif /* HAVE_ECC_VERIFY */
  9488. #endif /* HAVE_ECC_SIGN */
  9489. #if defined(HAVE_ECC_DHE) && defined(HAVE_ECC_KEY_EXPORT)
  9490. /* now test private only key with creating a shared secret */
  9491. x = FOURK_BUF;
  9492. ret = wc_ecc_export_private_only(&key, exportBuf, &x);
  9493. if (ret != 0) {
  9494. ERROR_OUT(-6827, done);
  9495. }
  9496. /* make private only key */
  9497. wc_ecc_free(&key);
  9498. wc_ecc_init(&key);
  9499. ret = wc_ecc_import_private_key(exportBuf, x, NULL, 0, &key);
  9500. if (ret != 0) {
  9501. ERROR_OUT(-6828, done);
  9502. }
  9503. /* check that public export fails with private only key */
  9504. x = FOURK_BUF;
  9505. ret = wc_ecc_export_x963_ex(&key, exportBuf, &x, 0);
  9506. if (ret == 0) {
  9507. ERROR_OUT(-6829, done);
  9508. }
  9509. /* make public key for shared secret */
  9510. wc_ecc_init(&pub);
  9511. ret = wc_ecc_make_key(rng, 32, &pub);
  9512. if (ret != 0) {
  9513. ERROR_OUT(-6830, done);
  9514. }
  9515. x = FOURK_BUF;
  9516. ret = wc_ecc_shared_secret(&key, &pub, exportBuf, &x);
  9517. wc_ecc_free(&pub);
  9518. if (ret != 0) {
  9519. ERROR_OUT(-6831, done);
  9520. }
  9521. #endif /* HAVE_ECC_DHE && HAVE_ECC_KEY_EXPORT */
  9522. ret = 0;
  9523. done:
  9524. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9525. XFREE(exportBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9526. wc_ecc_del_point_h(pubPoint, HEAP_HINT);
  9527. wc_ecc_free(&key);
  9528. return ret;
  9529. }
  9530. #endif /* HAVE_ECC_KEY_IMPORT */
  9531. #ifdef WOLFSSL_KEY_GEN
  9532. static int ecc_test_key_gen(WC_RNG* rng, int keySize)
  9533. {
  9534. int ret = 0;
  9535. int derSz;
  9536. byte* der;
  9537. byte* pem;
  9538. ecc_key userA;
  9539. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9540. if (der == NULL) {
  9541. return -6840;
  9542. }
  9543. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9544. if (pem == NULL) {
  9545. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9546. return -6840;
  9547. }
  9548. ret = wc_ecc_init_ex(&userA, HEAP_HINT, devId);
  9549. if (ret != 0)
  9550. goto done;
  9551. ret = wc_ecc_make_key(rng, keySize, &userA);
  9552. #if defined(WOLFSSL_ASYNC_CRYPT)
  9553. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9554. #endif
  9555. if (ret != 0)
  9556. goto done;
  9557. ret = wc_ecc_check_key(&userA);
  9558. if (ret != 0)
  9559. goto done;
  9560. derSz = wc_EccKeyToDer(&userA, der, FOURK_BUF);
  9561. if (derSz < 0) {
  9562. ERROR_OUT(derSz, done);
  9563. }
  9564. ret = SaveDerAndPem(der, derSz, pem, FOURK_BUF, eccCaKeyTempFile,
  9565. eccCaKeyPemFile, ECC_PRIVATEKEY_TYPE, -6510);
  9566. if (ret != 0) {
  9567. goto done;
  9568. }
  9569. /* test export of public key */
  9570. derSz = wc_EccPublicKeyToDer(&userA, der, FOURK_BUF, 1);
  9571. if (derSz < 0) {
  9572. ERROR_OUT(derSz, done);
  9573. }
  9574. if (derSz == 0) {
  9575. ERROR_OUT(-6514, done);
  9576. }
  9577. ret = SaveDerAndPem(der, derSz, NULL, 0, eccPubKeyDerFile,
  9578. NULL, 0, -6515);
  9579. if (ret != 0) {
  9580. goto done;
  9581. }
  9582. done:
  9583. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9584. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9585. wc_ecc_free(&userA);
  9586. return ret;
  9587. }
  9588. #endif /* WOLFSSL_KEY_GEN */
  9589. static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount,
  9590. int curve_id, const ecc_set_type* dp)
  9591. {
  9592. DECLARE_VAR(sharedA, byte, ECC_SHARED_SIZE, HEAP_HINT);
  9593. DECLARE_VAR(sharedB, byte, ECC_SHARED_SIZE, HEAP_HINT);
  9594. #ifdef HAVE_ECC_KEY_EXPORT
  9595. byte exportBuf[1024];
  9596. #endif
  9597. word32 x, y;
  9598. #ifdef HAVE_ECC_SIGN
  9599. DECLARE_VAR(sig, byte, ECC_SIG_SIZE, HEAP_HINT);
  9600. DECLARE_VAR(digest, byte, ECC_DIGEST_SIZE, HEAP_HINT);
  9601. int i;
  9602. #ifdef HAVE_ECC_VERIFY
  9603. int verify;
  9604. #endif /* HAVE_ECC_VERIFY */
  9605. #endif /* HAVE_ECC_SIGN */
  9606. int ret;
  9607. ecc_key userA, userB, pubKey;
  9608. (void)testVerifyCount;
  9609. (void)dp;
  9610. XMEMSET(&userA, 0, sizeof(ecc_key));
  9611. XMEMSET(&userB, 0, sizeof(ecc_key));
  9612. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  9613. ret = wc_ecc_init_ex(&userA, HEAP_HINT, devId);
  9614. if (ret != 0)
  9615. goto done;
  9616. ret = wc_ecc_init_ex(&userB, HEAP_HINT, devId);
  9617. if (ret != 0)
  9618. goto done;
  9619. ret = wc_ecc_init_ex(&pubKey, HEAP_HINT, devId);
  9620. if (ret != 0)
  9621. goto done;
  9622. #ifdef WOLFSSL_CUSTOM_CURVES
  9623. if (dp != NULL) {
  9624. ret = wc_ecc_set_custom_curve(&userA, dp);
  9625. if (ret != 0)
  9626. goto done;
  9627. ret = wc_ecc_set_custom_curve(&userB, dp);
  9628. if (ret != 0)
  9629. goto done;
  9630. }
  9631. #endif
  9632. ret = wc_ecc_make_key_ex(rng, keySize, &userA, curve_id);
  9633. #if defined(WOLFSSL_ASYNC_CRYPT)
  9634. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9635. #endif
  9636. if (ret != 0)
  9637. goto done;
  9638. ret = wc_ecc_check_key(&userA);
  9639. if (ret != 0)
  9640. goto done;
  9641. ret = wc_ecc_make_key_ex(rng, keySize, &userB, curve_id);
  9642. #if defined(WOLFSSL_ASYNC_CRYPT)
  9643. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9644. #endif
  9645. if (ret != 0)
  9646. goto done;
  9647. #ifdef HAVE_ECC_DHE
  9648. x = ECC_SHARED_SIZE;
  9649. do {
  9650. #if defined(WOLFSSL_ASYNC_CRYPT)
  9651. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9652. #endif
  9653. if (ret >= 0)
  9654. ret = wc_ecc_shared_secret(&userA, &userB, sharedA, &x);
  9655. } while (ret == WC_PENDING_E);
  9656. if (ret != 0) {
  9657. goto done;
  9658. }
  9659. y = ECC_SHARED_SIZE;
  9660. do {
  9661. #if defined(WOLFSSL_ASYNC_CRYPT)
  9662. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9663. #endif
  9664. if (ret >= 0)
  9665. ret = wc_ecc_shared_secret(&userB, &userA, sharedB, &y);
  9666. } while (ret == WC_PENDING_E);
  9667. if (ret != 0)
  9668. goto done;
  9669. if (y != x)
  9670. ERROR_OUT(-6517, done);
  9671. if (XMEMCMP(sharedA, sharedB, x))
  9672. ERROR_OUT(-6518, done);
  9673. #endif /* HAVE_ECC_DHE */
  9674. #ifdef HAVE_ECC_CDH
  9675. /* add cofactor flag */
  9676. wc_ecc_set_flags(&userA, WC_ECC_FLAG_COFACTOR);
  9677. wc_ecc_set_flags(&userB, WC_ECC_FLAG_COFACTOR);
  9678. x = sizeof(sharedA);
  9679. ret = wc_ecc_shared_secret(&userA, &userB, sharedA, &x);
  9680. if (ret != 0) {
  9681. goto done;
  9682. }
  9683. y = sizeof(sharedB);
  9684. ret = wc_ecc_shared_secret(&userB, &userA, sharedB, &y);
  9685. if (ret != 0)
  9686. goto done;
  9687. if (y != x)
  9688. ERROR_OUT(-6519, done);
  9689. if (XMEMCMP(sharedA, sharedB, x))
  9690. ERROR_OUT(-6520, done);
  9691. /* remove cofactor flag */
  9692. wc_ecc_set_flags(&userA, 0);
  9693. wc_ecc_set_flags(&userB, 0);
  9694. #endif /* HAVE_ECC_CDH */
  9695. #ifdef HAVE_ECC_KEY_EXPORT
  9696. x = sizeof(exportBuf);
  9697. ret = wc_ecc_export_x963_ex(&userA, exportBuf, &x, 0);
  9698. if (ret != 0)
  9699. goto done;
  9700. #ifdef HAVE_ECC_KEY_IMPORT
  9701. #ifdef WOLFSSL_CUSTOM_CURVES
  9702. if (dp != NULL) {
  9703. ret = wc_ecc_set_custom_curve(&pubKey, dp);
  9704. if (ret != 0) goto done;
  9705. }
  9706. #endif
  9707. ret = wc_ecc_import_x963_ex(exportBuf, x, &pubKey, curve_id);
  9708. if (ret != 0)
  9709. goto done;
  9710. #ifdef HAVE_ECC_DHE
  9711. y = ECC_SHARED_SIZE;
  9712. do {
  9713. #if defined(WOLFSSL_ASYNC_CRYPT)
  9714. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9715. #endif
  9716. if (ret >= 0)
  9717. ret = wc_ecc_shared_secret(&userB, &pubKey, sharedB, &y);
  9718. } while (ret == WC_PENDING_E);
  9719. if (ret != 0)
  9720. goto done;
  9721. if (XMEMCMP(sharedA, sharedB, y))
  9722. ERROR_OUT(-6521, done);
  9723. #endif /* HAVE_ECC_DHE */
  9724. #ifdef HAVE_COMP_KEY
  9725. /* try compressed export / import too */
  9726. x = sizeof(exportBuf);
  9727. ret = wc_ecc_export_x963_ex(&userA, exportBuf, &x, 1);
  9728. if (ret != 0)
  9729. goto done;
  9730. wc_ecc_free(&pubKey);
  9731. ret = wc_ecc_init_ex(&pubKey, HEAP_HINT, devId);
  9732. if (ret != 0)
  9733. goto done;
  9734. #ifdef WOLFSSL_CUSTOM_CURVES
  9735. if (dp != NULL) {
  9736. ret = wc_ecc_set_custom_curve(&pubKey, dp);
  9737. if (ret != 0) goto done;
  9738. }
  9739. #endif
  9740. ret = wc_ecc_import_x963_ex(exportBuf, x, &pubKey, curve_id);
  9741. if (ret != 0)
  9742. goto done;
  9743. #ifdef HAVE_ECC_DHE
  9744. y = ECC_SHARED_SIZE;
  9745. do {
  9746. #if defined(WOLFSSL_ASYNC_CRYPT)
  9747. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9748. #endif
  9749. if (ret >= 0)
  9750. ret = wc_ecc_shared_secret(&userB, &pubKey, sharedB, &y);
  9751. } while (ret == WC_PENDING_E);
  9752. if (ret != 0)
  9753. goto done;
  9754. if (XMEMCMP(sharedA, sharedB, y))
  9755. ERROR_OUT(-6522, done);
  9756. #endif /* HAVE_ECC_DHE */
  9757. #endif /* HAVE_COMP_KEY */
  9758. #endif /* HAVE_ECC_KEY_IMPORT */
  9759. #endif /* HAVE_ECC_KEY_EXPORT */
  9760. #ifdef HAVE_ECC_SIGN
  9761. /* ECC w/out Shamir has issue with all 0 digest */
  9762. /* WC_BIGINT doesn't have 0 len well on hardware */
  9763. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT)
  9764. /* test DSA sign hash with zeros */
  9765. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  9766. digest[i] = 0;
  9767. }
  9768. x = ECC_SIG_SIZE;
  9769. do {
  9770. #if defined(WOLFSSL_ASYNC_CRYPT)
  9771. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9772. #endif
  9773. if (ret >= 0)
  9774. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  9775. &userA);
  9776. } while (ret == WC_PENDING_E);
  9777. if (ret != 0)
  9778. goto done;
  9779. #ifdef HAVE_ECC_VERIFY
  9780. for (i=0; i<testVerifyCount; i++) {
  9781. verify = 0;
  9782. do {
  9783. #if defined(WOLFSSL_ASYNC_CRYPT)
  9784. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9785. #endif
  9786. if (ret >= 0)
  9787. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  9788. &verify, &userA);
  9789. } while (ret == WC_PENDING_E);
  9790. if (ret != 0)
  9791. goto done;
  9792. if (verify != 1)
  9793. ERROR_OUT(-6523, done);
  9794. }
  9795. #endif /* HAVE_ECC_VERIFY */
  9796. #endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT */
  9797. /* test DSA sign hash with sequence (0,1,2,3,4,...) */
  9798. for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) {
  9799. digest[i] = (byte)i;
  9800. }
  9801. x = ECC_SIG_SIZE;
  9802. do {
  9803. #if defined(WOLFSSL_ASYNC_CRYPT)
  9804. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9805. #endif
  9806. if (ret >= 0)
  9807. ret = wc_ecc_sign_hash(digest, ECC_DIGEST_SIZE, sig, &x, rng,
  9808. &userA);
  9809. } while (ret == WC_PENDING_E);
  9810. if (ret != 0)
  9811. ERROR_OUT(-6524, done);
  9812. #ifdef HAVE_ECC_VERIFY
  9813. for (i=0; i<testVerifyCount; i++) {
  9814. verify = 0;
  9815. do {
  9816. #if defined(WOLFSSL_ASYNC_CRYPT)
  9817. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  9818. #endif
  9819. if (ret >= 0)
  9820. ret = wc_ecc_verify_hash(sig, x, digest, ECC_DIGEST_SIZE,
  9821. &verify, &userA);
  9822. } while (ret == WC_PENDING_E);
  9823. if (ret != 0)
  9824. goto done;
  9825. if (verify != 1)
  9826. ERROR_OUT(-6525, done);
  9827. }
  9828. #endif /* HAVE_ECC_VERIFY */
  9829. #endif /* HAVE_ECC_SIGN */
  9830. #ifdef HAVE_ECC_KEY_EXPORT
  9831. x = sizeof(exportBuf);
  9832. ret = wc_ecc_export_private_only(&userA, exportBuf, &x);
  9833. if (ret != 0)
  9834. goto done;
  9835. #endif /* HAVE_ECC_KEY_EXPORT */
  9836. done:
  9837. wc_ecc_free(&pubKey);
  9838. wc_ecc_free(&userB);
  9839. wc_ecc_free(&userA);
  9840. FREE_VAR(sharedA, HEAP_HINT);
  9841. FREE_VAR(sharedB, HEAP_HINT);
  9842. #ifdef HAVE_ECC_SIGN
  9843. FREE_VAR(sig, HEAP_HINT);
  9844. FREE_VAR(digest, HEAP_HINT);
  9845. #endif
  9846. return ret;
  9847. }
  9848. #undef ECC_TEST_VERIFY_COUNT
  9849. #define ECC_TEST_VERIFY_COUNT 2
  9850. static int ecc_test_curve(WC_RNG* rng, int keySize)
  9851. {
  9852. int ret;
  9853. ret = ecc_test_curve_size(rng, keySize, ECC_TEST_VERIFY_COUNT,
  9854. ECC_CURVE_DEF, NULL);
  9855. if (ret < 0) {
  9856. if (ret == ECC_CURVE_OID_E) {
  9857. /* ignore error for curves not found */
  9858. /* some curve sizes are only available with:
  9859. HAVE_ECC_SECPR2, HAVE_ECC_SECPR3, HAVE_ECC_BRAINPOOL
  9860. and HAVE_ECC_KOBLITZ */
  9861. }
  9862. else {
  9863. printf("ecc_test_curve_size %d failed!: %d\n", keySize, ret);
  9864. return ret;
  9865. }
  9866. }
  9867. #ifdef HAVE_ECC_VECTOR_TEST
  9868. ret = ecc_test_vector(keySize);
  9869. if (ret < 0) {
  9870. printf("ecc_test_vector %d failed!: %d\n", keySize, ret);
  9871. return ret;
  9872. }
  9873. #endif
  9874. #ifdef WOLFSSL_KEY_GEN
  9875. ret = ecc_test_key_gen(rng, keySize);
  9876. if (ret < 0) {
  9877. if (ret == ECC_CURVE_OID_E) {
  9878. /* ignore error for curves not found */
  9879. }
  9880. else {
  9881. printf("ecc_test_key_gen %d failed!: %d\n", keySize, ret);
  9882. return ret;
  9883. }
  9884. }
  9885. #endif
  9886. return 0;
  9887. }
  9888. #if !defined(WOLFSSL_ATECC508A) && defined(HAVE_ECC_KEY_IMPORT) && \
  9889. defined(HAVE_ECC_KEY_EXPORT)
  9890. static int ecc_point_test(void)
  9891. {
  9892. int ret;
  9893. ecc_point* point;
  9894. ecc_point* point2;
  9895. #ifdef HAVE_COMP_KEY
  9896. ecc_point* point3;
  9897. ecc_point* point4;
  9898. #endif
  9899. word32 outLen;
  9900. byte out[65];
  9901. byte der[] = { 0x04, /* = Uncompressed */
  9902. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  9903. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  9904. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  9905. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  9906. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  9907. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  9908. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  9909. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  9910. #ifdef HAVE_COMP_KEY
  9911. byte derComp0[] = { 0x02, /* = Compressed, y even */
  9912. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  9913. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  9914. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  9915. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  9916. byte derComp1[] = { 0x03, /* = Compressed, y odd */
  9917. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  9918. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  9919. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  9920. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  9921. #endif
  9922. byte altDer[] = { 0x04, /* = Uncompressed */
  9923. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9924. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9925. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9926. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9927. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9928. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9929. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  9930. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
  9931. int curve_idx = wc_ecc_get_curve_idx(ECC_SECP256R1);
  9932. /* if curve P256 is not enabled then test should not fail */
  9933. if (curve_idx == ECC_CURVE_INVALID)
  9934. return 0;
  9935. outLen = sizeof(out);
  9936. point = wc_ecc_new_point();
  9937. if (point == NULL)
  9938. return -6600;
  9939. point2 = wc_ecc_new_point();
  9940. if (point2 == NULL) {
  9941. wc_ecc_del_point(point);
  9942. return -6601;
  9943. }
  9944. #ifdef HAVE_COMP_KEY
  9945. point3 = wc_ecc_new_point();
  9946. if (point3 == NULL) {
  9947. wc_ecc_del_point(point2);
  9948. wc_ecc_del_point(point);
  9949. return -6602;
  9950. }
  9951. point4 = wc_ecc_new_point();
  9952. if (point4 == NULL) {
  9953. wc_ecc_del_point(point3);
  9954. wc_ecc_del_point(point2);
  9955. wc_ecc_del_point(point);
  9956. return -6603;
  9957. }
  9958. #endif
  9959. /* Parameter Validation testing. */
  9960. wc_ecc_del_point(NULL);
  9961. ret = wc_ecc_import_point_der(NULL, sizeof(der), curve_idx, point);
  9962. if (ret != ECC_BAD_ARG_E) {
  9963. ret = -6604;
  9964. goto done;
  9965. }
  9966. ret = wc_ecc_import_point_der(der, sizeof(der), ECC_CURVE_INVALID, point);
  9967. if (ret != ECC_BAD_ARG_E) {
  9968. ret = -6605;
  9969. goto done;
  9970. }
  9971. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, NULL);
  9972. if (ret != ECC_BAD_ARG_E) {
  9973. ret = -6606;
  9974. goto done;
  9975. }
  9976. ret = wc_ecc_export_point_der(-1, point, out, &outLen);
  9977. if (ret != ECC_BAD_ARG_E) {
  9978. ret = -6607;
  9979. goto done;
  9980. }
  9981. ret = wc_ecc_export_point_der(curve_idx, NULL, out, &outLen);
  9982. if (ret != ECC_BAD_ARG_E) {
  9983. ret = -6608;
  9984. goto done;
  9985. }
  9986. ret = wc_ecc_export_point_der(curve_idx, point, NULL, &outLen);
  9987. if (ret != LENGTH_ONLY_E || outLen != sizeof(out)) {
  9988. ret = -6609;
  9989. goto done;
  9990. }
  9991. ret = wc_ecc_export_point_der(curve_idx, point, out, NULL);
  9992. if (ret != ECC_BAD_ARG_E) {
  9993. ret = -6610;
  9994. goto done;
  9995. }
  9996. outLen = 0;
  9997. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  9998. if (ret != BUFFER_E) {
  9999. ret = -6611;
  10000. goto done;
  10001. }
  10002. ret = wc_ecc_copy_point(NULL, NULL);
  10003. if (ret != ECC_BAD_ARG_E) {
  10004. ret = -6612;
  10005. goto done;
  10006. }
  10007. ret = wc_ecc_copy_point(NULL, point2);
  10008. if (ret != ECC_BAD_ARG_E) {
  10009. ret = -6613;
  10010. goto done;
  10011. }
  10012. ret = wc_ecc_copy_point(point, NULL);
  10013. if (ret != ECC_BAD_ARG_E) {
  10014. ret = -6614;
  10015. goto done;
  10016. }
  10017. ret = wc_ecc_cmp_point(NULL, NULL);
  10018. if (ret != BAD_FUNC_ARG) {
  10019. ret = -6615;
  10020. goto done;
  10021. }
  10022. ret = wc_ecc_cmp_point(NULL, point2);
  10023. if (ret != BAD_FUNC_ARG) {
  10024. ret = -6616;
  10025. goto done;
  10026. }
  10027. ret = wc_ecc_cmp_point(point, NULL);
  10028. if (ret != BAD_FUNC_ARG) {
  10029. ret = -6617;
  10030. goto done;
  10031. }
  10032. /* Use API. */
  10033. ret = wc_ecc_import_point_der(der, sizeof(der), curve_idx, point);
  10034. if (ret != 0) {
  10035. ret = -6618;
  10036. goto done;
  10037. }
  10038. outLen = sizeof(out);
  10039. ret = wc_ecc_export_point_der(curve_idx, point, out, &outLen);
  10040. if (ret != 0) {
  10041. ret = -6619;
  10042. goto done;
  10043. }
  10044. if (outLen != sizeof(der)) {
  10045. ret = -6620;
  10046. goto done;
  10047. }
  10048. if (XMEMCMP(out, der, outLen) != 0) {
  10049. ret = -6621;
  10050. goto done;
  10051. }
  10052. ret = wc_ecc_copy_point(point2, point);
  10053. if (ret != MP_OKAY) {
  10054. ret = -6622;
  10055. goto done;
  10056. }
  10057. ret = wc_ecc_cmp_point(point2, point);
  10058. if (ret != MP_EQ) {
  10059. ret = -6623;
  10060. goto done;
  10061. }
  10062. ret = wc_ecc_import_point_der(altDer, sizeof(altDer), curve_idx, point2);
  10063. if (ret != 0) {
  10064. ret = -6624;
  10065. goto done;
  10066. }
  10067. ret = wc_ecc_cmp_point(point2, point);
  10068. if (ret != MP_GT) {
  10069. ret = -6625;
  10070. goto done;
  10071. }
  10072. #ifdef HAVE_COMP_KEY
  10073. ret = wc_ecc_import_point_der(derComp0, sizeof(der), curve_idx, point3);
  10074. if (ret != 0) {
  10075. ret = -6626;
  10076. goto done;
  10077. }
  10078. ret = wc_ecc_import_point_der(derComp1, sizeof(der), curve_idx, point4);
  10079. if (ret != 0) {
  10080. ret = -6627;
  10081. goto done;
  10082. }
  10083. #endif
  10084. done:
  10085. #ifdef HAVE_COMP_KEY
  10086. wc_ecc_del_point(point4);
  10087. wc_ecc_del_point(point3);
  10088. #endif
  10089. wc_ecc_del_point(point2);
  10090. wc_ecc_del_point(point);
  10091. return ret;
  10092. }
  10093. #endif /* !WOLFSSL_ATECC508A && HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  10094. #ifndef NO_SIG_WRAPPER
  10095. static int ecc_sig_test(WC_RNG* rng, ecc_key* key)
  10096. {
  10097. int ret;
  10098. word32 sigSz;
  10099. int size;
  10100. byte out[ECC_MAX_SIG_SIZE];
  10101. byte in[] = "Everyone gets Friday off.";
  10102. word32 inLen = (word32)XSTRLEN((char*)in);
  10103. size = wc_ecc_sig_size(key);
  10104. ret = wc_SignatureGetSize(WC_SIGNATURE_TYPE_ECC, key, sizeof(*key));
  10105. if (ret != size)
  10106. return -6628;
  10107. sigSz = (word32)ret;
  10108. ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  10109. inLen, out, &sigSz, key, sizeof(*key), rng);
  10110. if (ret != 0)
  10111. return -6629;
  10112. ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, in,
  10113. inLen, out, sigSz, key, sizeof(*key));
  10114. if (ret != 0)
  10115. return -6630;
  10116. return 0;
  10117. }
  10118. #endif
  10119. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  10120. static int ecc_exp_imp_test(ecc_key* key)
  10121. {
  10122. int ret;
  10123. int curve_id;
  10124. ecc_key keyImp;
  10125. byte priv[32];
  10126. word32 privLen;
  10127. byte pub[65];
  10128. word32 pubLen;
  10129. const char qx[] = "7a4e287890a1a47ad3457e52f2f76a83"
  10130. "ce46cbc947616d0cbaa82323818a793d";
  10131. const char qy[] = "eec4084f5b29ebf29c44cce3b3059610"
  10132. "922f8b30ea6e8811742ac7238fe87308";
  10133. const char d[] = "8c14b793cb19137e323a6d2e2a870bca"
  10134. "2e7a493ec1153b3a95feb8a4873f8d08";
  10135. wc_ecc_init(&keyImp);
  10136. privLen = sizeof(priv);
  10137. ret = wc_ecc_export_private_only(key, priv, &privLen);
  10138. if (ret != 0) {
  10139. ret = -6631;
  10140. goto done;
  10141. }
  10142. pubLen = sizeof(pub);
  10143. ret = wc_ecc_export_point_der(key->idx, &key->pubkey, pub, &pubLen);
  10144. if (ret != 0) {
  10145. ret = -6632;
  10146. goto done;
  10147. }
  10148. ret = wc_ecc_import_private_key(priv, privLen, pub, pubLen, &keyImp);
  10149. if (ret != 0) {
  10150. ret = -6633;
  10151. goto done;
  10152. }
  10153. wc_ecc_free(&keyImp);
  10154. wc_ecc_init(&keyImp);
  10155. ret = wc_ecc_import_raw_ex(&keyImp, qx, qy, d, ECC_SECP256R1);
  10156. if (ret != 0) {
  10157. ret = -6634;
  10158. goto done;
  10159. }
  10160. wc_ecc_free(&keyImp);
  10161. wc_ecc_init(&keyImp);
  10162. curve_id = wc_ecc_get_curve_id(key->idx);
  10163. if (curve_id < 0)
  10164. return -6635;
  10165. /* test import private only */
  10166. ret = wc_ecc_import_private_key_ex(priv, privLen, NULL, 0, &keyImp,
  10167. curve_id);
  10168. if (ret != 0)
  10169. return -6636;
  10170. done:
  10171. wc_ecc_free(&keyImp);
  10172. return ret;
  10173. }
  10174. #endif /* HAVE_ECC_KEY_IMPORT && HAVE_ECC_KEY_EXPORT */
  10175. #ifndef WOLFSSL_ATECC508A
  10176. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)
  10177. static int ecc_mulmod_test(ecc_key* key1)
  10178. {
  10179. int ret;
  10180. ecc_key key2;
  10181. ecc_key key3;
  10182. wc_ecc_init(&key2);
  10183. wc_ecc_init(&key3);
  10184. /* TODO: Use test data, test with WOLFSSL_VALIDATE_ECC_IMPORT. */
  10185. /* Need base point (Gx,Gy) and parameter A - load them as the public and
  10186. * private key in key2.
  10187. */
  10188. ret = wc_ecc_import_raw_ex(&key2, key1->dp->Gx, key1->dp->Gy, key1->dp->Af,
  10189. ECC_SECP256R1);
  10190. if (ret != 0)
  10191. goto done;
  10192. /* Need a point (Gx,Gy) and prime - load them as the public and private key
  10193. * in key3.
  10194. */
  10195. ret = wc_ecc_import_raw_ex(&key3, key1->dp->Gx, key1->dp->Gy,
  10196. key1->dp->prime, ECC_SECP256R1);
  10197. if (ret != 0)
  10198. goto done;
  10199. ret = wc_ecc_mulmod(&key1->k, &key2.pubkey, &key3.pubkey, &key2.k, &key3.k,
  10200. 1);
  10201. if (ret != 0) {
  10202. ret = -6637;
  10203. goto done;
  10204. }
  10205. done:
  10206. wc_ecc_free(&key3);
  10207. wc_ecc_free(&key2);
  10208. return ret;
  10209. }
  10210. #endif
  10211. static int ecc_ssh_test(ecc_key* key)
  10212. {
  10213. int ret;
  10214. byte out[128];
  10215. word32 outLen = sizeof(out);
  10216. /* Parameter Validation testing. */
  10217. ret = wc_ecc_shared_secret_ssh(NULL, &key->pubkey, out, &outLen);
  10218. if (ret != BAD_FUNC_ARG)
  10219. return -6638;
  10220. ret = wc_ecc_shared_secret_ssh(key, NULL, out, &outLen);
  10221. if (ret != BAD_FUNC_ARG)
  10222. return -6639;
  10223. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, NULL, &outLen);
  10224. if (ret != BAD_FUNC_ARG)
  10225. return -6640;
  10226. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, NULL);
  10227. if (ret != BAD_FUNC_ARG)
  10228. return -6641;
  10229. /* Use API. */
  10230. ret = wc_ecc_shared_secret_ssh(key, &key->pubkey, out, &outLen);
  10231. if (ret != 0)
  10232. return -6642;
  10233. return 0;
  10234. }
  10235. #endif
  10236. static int ecc_def_curve_test(WC_RNG *rng)
  10237. {
  10238. int ret;
  10239. ecc_key key;
  10240. wc_ecc_init(&key);
  10241. ret = wc_ecc_make_key(rng, 32, &key);
  10242. if (ret != 0) {
  10243. ret = -6643;
  10244. goto done;
  10245. }
  10246. #ifndef NO_SIG_WRAPPER
  10247. ret = ecc_sig_test(rng, &key);
  10248. if (ret < 0)
  10249. goto done;
  10250. #endif
  10251. #if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT)
  10252. ret = ecc_exp_imp_test(&key);
  10253. if (ret < 0)
  10254. goto done;
  10255. #endif
  10256. #ifndef WOLFSSL_ATECC508A
  10257. #if defined(HAVE_ECC_KEY_IMPORT) && !defined(WOLFSSL_VALIDATE_ECC_IMPORT)
  10258. ret = ecc_mulmod_test(&key);
  10259. if (ret < 0)
  10260. goto done;
  10261. #endif
  10262. ret = ecc_ssh_test(&key);
  10263. if (ret < 0)
  10264. goto done;
  10265. #endif /* WOLFSSL_ATECC508A */
  10266. done:
  10267. wc_ecc_free(&key);
  10268. return ret;
  10269. }
  10270. #ifdef WOLFSSL_CERT_EXT
  10271. static int ecc_decode_test(void)
  10272. {
  10273. int ret;
  10274. word32 inSz;
  10275. word32 inOutIdx;
  10276. ecc_key key;
  10277. const byte good[] = { 0x30, 0x0d, 0x30, 0x0b, 0x06, 0x00, 0x06, 0x01, 0x01,
  10278. 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  10279. const byte badNoObjId[] = { 0x30, 0x08, 0x30, 0x06, 0x03, 0x04,
  10280. 0x00, 0x04, 0x01, 0x01 };
  10281. const byte badOneObjId[] = { 0x30, 0x0a, 0x30, 0x08, 0x06, 0x00, 0x03, 0x04,
  10282. 0x00, 0x04, 0x01, 0x01 };
  10283. const byte badObjId1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x09,
  10284. 0x06, 0x00, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  10285. const byte badObj2d1Len[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  10286. 0x06, 0x07, 0x03, 0x04, 0x00, 0x04, 0x01, 0x01 };
  10287. const byte badNotBitStr[] = { 0x30, 0x0d, 0x30, 0x0b, 0x06, 0x00,
  10288. 0x06, 0x01, 0x01, 0x04, 0x04, 0x00, 0x04, 0x01, 0x01 };
  10289. const byte badBitStrLen[] = { 0x30, 0x0d, 0x30, 0x0b, 0x06, 0x00,
  10290. 0x06, 0x01, 0x01, 0x03, 0x05, 0x00, 0x04, 0x01, 0x01 };
  10291. const byte badNoBitStrZero[] = { 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x00,
  10292. 0x06, 0x01, 0x01, 0x03, 0x03, 0x04, 0x01, 0x01 };
  10293. const byte badPoint[] = { 0x30, 0x0b, 0x30, 0x09, 0x06, 0x00, 0x06, 0x01,
  10294. 0x01, 0x03, 0x03, 0x00, 0x04, 0x01 };
  10295. XMEMSET(&key, 0, sizeof(key));
  10296. wc_ecc_init(&key);
  10297. inSz = sizeof(good);
  10298. ret = wc_EccPublicKeyDecode(NULL, &inOutIdx, &key, inSz);
  10299. if (ret != BAD_FUNC_ARG) {
  10300. ret = -6700;
  10301. goto done;
  10302. }
  10303. ret = wc_EccPublicKeyDecode(good, NULL, &key, inSz);
  10304. if (ret != BAD_FUNC_ARG) {
  10305. ret = -6701;
  10306. goto done;
  10307. }
  10308. ret = wc_EccPublicKeyDecode(good, &inOutIdx, NULL, inSz);
  10309. if (ret != BAD_FUNC_ARG) {
  10310. ret = -6702;
  10311. goto done;
  10312. }
  10313. ret = wc_EccPublicKeyDecode(good, &inOutIdx, &key, 0);
  10314. if (ret != BAD_FUNC_ARG) {
  10315. ret = -6703;
  10316. goto done;
  10317. }
  10318. /* Change offset to produce bad input data. */
  10319. inOutIdx = 2;
  10320. inSz = sizeof(good) - inOutIdx;
  10321. ret = wc_EccPublicKeyDecode(good, &inOutIdx, &key, inSz);
  10322. if (ret != ASN_PARSE_E) {
  10323. ret = -6704;
  10324. goto done;
  10325. }
  10326. inOutIdx = 4;
  10327. inSz = sizeof(good) - inOutIdx;
  10328. ret = wc_EccPublicKeyDecode(good, &inOutIdx, &key, inSz);
  10329. if (ret != ASN_PARSE_E) {
  10330. ret = -6705;
  10331. goto done;
  10332. }
  10333. /* Bad data. */
  10334. inSz = sizeof(badNoObjId);
  10335. inOutIdx = 0;
  10336. ret = wc_EccPublicKeyDecode(badNoObjId, &inOutIdx, &key, inSz);
  10337. if (ret != ASN_OBJECT_ID_E) {
  10338. ret = -6706;
  10339. goto done;
  10340. }
  10341. inSz = sizeof(badOneObjId);
  10342. inOutIdx = 0;
  10343. ret = wc_EccPublicKeyDecode(badOneObjId, &inOutIdx, &key, inSz);
  10344. if (ret != ASN_OBJECT_ID_E) {
  10345. ret = -6707;
  10346. goto done;
  10347. }
  10348. inSz = sizeof(badObjId1Len);
  10349. inOutIdx = 0;
  10350. ret = wc_EccPublicKeyDecode(badObjId1Len, &inOutIdx, &key, inSz);
  10351. if (ret != ASN_PARSE_E) {
  10352. ret = -6708;
  10353. goto done;
  10354. }
  10355. inSz = sizeof(badObj2d1Len);
  10356. inOutIdx = 0;
  10357. ret = wc_EccPublicKeyDecode(badObj2d1Len, &inOutIdx, &key, inSz);
  10358. if (ret != ASN_PARSE_E) {
  10359. ret = -6709;
  10360. goto done;
  10361. }
  10362. inSz = sizeof(badNotBitStr);
  10363. inOutIdx = 0;
  10364. ret = wc_EccPublicKeyDecode(badNotBitStr, &inOutIdx, &key, inSz);
  10365. if (ret != ASN_BITSTR_E) {
  10366. ret = -6710;
  10367. goto done;
  10368. }
  10369. inSz = sizeof(badBitStrLen);
  10370. inOutIdx = 0;
  10371. ret = wc_EccPublicKeyDecode(badBitStrLen, &inOutIdx, &key, inSz);
  10372. if (ret != ASN_PARSE_E) {
  10373. ret = -6711;
  10374. goto done;
  10375. }
  10376. inSz = sizeof(badNoBitStrZero);
  10377. inOutIdx = 0;
  10378. ret = wc_EccPublicKeyDecode(badNoBitStrZero, &inOutIdx, &key, inSz);
  10379. if (ret != ASN_EXPECT_0_E) {
  10380. ret = -6712;
  10381. goto done;
  10382. }
  10383. inSz = sizeof(badPoint);
  10384. inOutIdx = 0;
  10385. ret = wc_EccPublicKeyDecode(badPoint, &inOutIdx, &key, inSz);
  10386. if (ret != ASN_ECC_KEY_E) {
  10387. ret = -6713;
  10388. goto done;
  10389. }
  10390. inSz = sizeof(good);
  10391. inOutIdx = 0;
  10392. ret = wc_EccPublicKeyDecode(good, &inOutIdx, &key, inSz);
  10393. if (ret != 0) {
  10394. ret = -6714;
  10395. goto done;
  10396. }
  10397. done:
  10398. wc_ecc_free(&key);
  10399. return ret;
  10400. }
  10401. #endif /* WOLFSSL_CERT_EXT */
  10402. #ifdef WOLFSSL_CUSTOM_CURVES
  10403. static int ecc_test_custom_curves(WC_RNG* rng)
  10404. {
  10405. int ret;
  10406. /* test use of custom curve - using BRAINPOOLP256R1 for test */
  10407. const word32 ecc_oid_brainpoolp256r1_sum = 104;
  10408. const ecc_oid_t ecc_oid_brainpoolp256r1[] = {
  10409. 0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07
  10410. };
  10411. const ecc_set_type ecc_dp_brainpool256r1 = {
  10412. 32, /* size/bytes */
  10413. ECC_CURVE_CUSTOM, /* ID */
  10414. "BRAINPOOLP256R1", /* curve name */
  10415. "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377", /* prime */
  10416. "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9", /* A */
  10417. "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6", /* B */
  10418. "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7", /* order */
  10419. "8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262", /* Gx */
  10420. "547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997", /* Gy */
  10421. ecc_oid_brainpoolp256r1, /* oid/oidSz */
  10422. sizeof(ecc_oid_brainpoolp256r1) / sizeof(ecc_oid_t),
  10423. ecc_oid_brainpoolp256r1_sum, /* oid sum */
  10424. 1, /* cofactor */
  10425. };
  10426. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, ECC_CURVE_DEF,
  10427. &ecc_dp_brainpool256r1);
  10428. if (ret != 0) {
  10429. printf("ECC test for custom curve failed! %d\n", ret);
  10430. return ret;
  10431. }
  10432. #if defined(HAVE_ECC_BRAINPOOL) || defined(HAVE_ECC_KOBLITZ)
  10433. {
  10434. int curve_id;
  10435. #ifdef HAVE_ECC_BRAINPOOL
  10436. curve_id = ECC_BRAINPOOLP256R1;
  10437. #else
  10438. curve_id = ECC_SECP256K1;
  10439. #endif
  10440. /* Test and demonstrate use of non-SECP curve */
  10441. ret = ecc_test_curve_size(rng, 0, ECC_TEST_VERIFY_COUNT, curve_id, NULL);
  10442. if (ret < 0) {
  10443. printf("ECC test for curve_id %d failed! %d\n", curve_id, ret);
  10444. return ret;
  10445. }
  10446. }
  10447. #endif
  10448. return ret;
  10449. }
  10450. #endif /* WOLFSSL_CUSTOM_CURVES */
  10451. #ifdef WOLFSSL_CERT_GEN
  10452. /* Make Cert / Sign example for ECC cert and ECC CA */
  10453. static int ecc_test_cert_gen(WC_RNG* rng)
  10454. {
  10455. int ret;
  10456. Cert myCert;
  10457. int certSz;
  10458. size_t bytes;
  10459. word32 idx = 0;
  10460. #ifndef USE_CERT_BUFFERS_256
  10461. FILE* file;
  10462. #endif
  10463. #ifdef WOLFSSL_TEST_CERT
  10464. DecodedCert decode;
  10465. #endif
  10466. byte* der = NULL;
  10467. byte* pem = NULL;
  10468. ecc_key caEccKey;
  10469. ecc_key certPubKey;
  10470. XMEMSET(&caEccKey, 0, sizeof(caEccKey));
  10471. XMEMSET(&certPubKey, 0, sizeof(certPubKey));
  10472. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10473. if (der == NULL) {
  10474. ERROR_OUT(-6720, exit);
  10475. }
  10476. pem = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10477. if (pem == NULL) {
  10478. ERROR_OUT(-6721, exit);
  10479. }
  10480. /* Get cert private key */
  10481. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  10482. /* Get Cert Key 384 */
  10483. #ifdef USE_CERT_BUFFERS_256
  10484. XMEMCPY(der, ca_ecc_key_der_384, sizeof_ca_ecc_key_der_384);
  10485. bytes = sizeof_ca_ecc_key_der_384;
  10486. #else
  10487. file = fopen(eccCaKey384File, "rb");
  10488. if (!file) {
  10489. ERROR_OUT(-6722, exit);
  10490. }
  10491. bytes = fread(der, 1, FOURK_BUF, file);
  10492. fclose(file);
  10493. (void)eccCaKeyFile;
  10494. #endif /* USE_CERT_BUFFERS_256 */
  10495. #else
  10496. #ifdef USE_CERT_BUFFERS_256
  10497. XMEMCPY(der, ca_ecc_key_der_256, sizeof_ca_ecc_key_der_256);
  10498. bytes = sizeof_ca_ecc_key_der_256;
  10499. #else
  10500. file = fopen(eccCaKeyFile, "rb");
  10501. if (!file) {
  10502. ERROR_OUT(-6722, exit);
  10503. }
  10504. bytes = fread(der, 1, FOURK_BUF, file);
  10505. fclose(file);
  10506. (void)eccCaKey384File;
  10507. #endif /* USE_CERT_BUFFERS_256 */
  10508. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  10509. /* Get CA Key */
  10510. ret = wc_ecc_init_ex(&caEccKey, HEAP_HINT, devId);
  10511. if (ret != 0) {
  10512. ERROR_OUT(-6723, exit);
  10513. }
  10514. ret = wc_EccPrivateKeyDecode(der, &idx, &caEccKey, (word32)bytes);
  10515. if (ret != 0) {
  10516. ERROR_OUT(-6724, exit);
  10517. }
  10518. /* Make a public key */
  10519. ret = wc_ecc_init_ex(&certPubKey, HEAP_HINT, devId);
  10520. if (ret != 0) {
  10521. ERROR_OUT(-6725, exit);
  10522. }
  10523. ret = wc_ecc_make_key(rng, 32, &certPubKey);
  10524. if (ret != 0) {
  10525. ERROR_OUT(-6726, exit);
  10526. }
  10527. /* Setup Certificate */
  10528. if (wc_InitCert(&myCert)) {
  10529. ERROR_OUT(-6727, exit);
  10530. }
  10531. #ifndef NO_SHA256
  10532. myCert.sigType = CTC_SHA256wECDSA;
  10533. #else
  10534. myCert.sigType = CTC_SHAwECDSA;
  10535. #endif
  10536. XMEMCPY(&myCert.subject, &certDefaultName, sizeof(CertName));
  10537. #ifdef WOLFSSL_CERT_EXT
  10538. /* add Policies */
  10539. XSTRNCPY(myCert.certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  10540. CTC_MAX_CERTPOL_SZ);
  10541. XSTRNCPY(myCert.certPolicies[1], "1.2.13025.489.1.113549",
  10542. CTC_MAX_CERTPOL_SZ);
  10543. myCert.certPoliciesNb = 2;
  10544. /* add SKID from the Public Key */
  10545. if (wc_SetSubjectKeyIdFromPublicKey(&myCert, NULL, &certPubKey) != 0) {
  10546. ERROR_OUT(-6728, exit);
  10547. }
  10548. /* add AKID from the Public Key */
  10549. if (wc_SetAuthKeyIdFromPublicKey(&myCert, NULL, &caEccKey) != 0) {
  10550. ERROR_OUT(-6729, exit);
  10551. }
  10552. /* add Key Usage */
  10553. if (wc_SetKeyUsage(&myCert, certKeyUsage) != 0) {
  10554. ERROR_OUT(-6730, exit);
  10555. }
  10556. #endif /* WOLFSSL_CERT_EXT */
  10557. #ifdef ENABLE_ECC384_CERT_GEN_TEST
  10558. #if defined(USE_CERT_BUFFERS_256)
  10559. ret = wc_SetIssuerBuffer(&myCert, ca_ecc_cert_der_384,
  10560. sizeof_ca_ecc_cert_der_384);
  10561. #else
  10562. ret = wc_SetIssuer(&myCert, eccCaCert384File);
  10563. (void)eccCaCertFile;
  10564. #endif
  10565. #else
  10566. #if defined(USE_CERT_BUFFERS_256)
  10567. ret = wc_SetIssuerBuffer(&myCert, ca_ecc_cert_der_256,
  10568. sizeof_ca_ecc_cert_der_256);
  10569. #else
  10570. ret = wc_SetIssuer(&myCert, eccCaCertFile);
  10571. (void)eccCaCert384File;
  10572. #endif
  10573. #endif /* ENABLE_ECC384_CERT_GEN_TEST */
  10574. if (ret < 0) {
  10575. ERROR_OUT(-6731, exit);
  10576. }
  10577. certSz = wc_MakeCert(&myCert, der, FOURK_BUF, NULL, &certPubKey, rng);
  10578. if (certSz < 0) {
  10579. ERROR_OUT(-6732, exit);
  10580. }
  10581. ret = 0;
  10582. do {
  10583. #if defined(WOLFSSL_ASYNC_CRYPT)
  10584. ret = wc_AsyncWait(ret, &caEccKey.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  10585. #endif
  10586. if (ret >= 0) {
  10587. ret = wc_SignCert(myCert.bodySz, myCert.sigType, der,
  10588. FOURK_BUF, NULL, &caEccKey, rng);
  10589. }
  10590. } while (ret == WC_PENDING_E);
  10591. if (ret < 0) {
  10592. ERROR_OUT(-6733, exit);
  10593. }
  10594. certSz = ret;
  10595. #ifdef WOLFSSL_TEST_CERT
  10596. InitDecodedCert(&decode, der, certSz, 0);
  10597. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  10598. if (ret != 0) {
  10599. FreeDecodedCert(&decode);
  10600. ERROR_OUT(-6734, exit);
  10601. }
  10602. FreeDecodedCert(&decode);
  10603. #endif
  10604. ret = SaveDerAndPem(der, certSz, pem, FOURK_BUF, certEccDerFile,
  10605. certEccPemFile, CERT_TYPE, -6735);
  10606. if (ret != 0) {
  10607. goto exit;
  10608. }
  10609. exit:
  10610. wc_ecc_free(&certPubKey);
  10611. wc_ecc_free(&caEccKey);
  10612. XFREE(pem, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10613. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10614. return ret;
  10615. }
  10616. #endif /* WOLFSSL_CERT_GEN */
  10617. int ecc_test(void)
  10618. {
  10619. int ret;
  10620. WC_RNG rng;
  10621. #ifdef WOLFSSL_CERT_EXT
  10622. ret = ecc_decode_test();
  10623. if (ret < 0)
  10624. return ret;
  10625. #endif
  10626. #ifndef HAVE_FIPS
  10627. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  10628. #else
  10629. ret = wc_InitRng(&rng);
  10630. #endif
  10631. if (ret != 0)
  10632. return -6800;
  10633. #if defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)
  10634. ret = ecc_test_curve(&rng, 14);
  10635. if (ret < 0) {
  10636. goto done;
  10637. }
  10638. #endif /* HAVE_ECC112 */
  10639. #if defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)
  10640. ret = ecc_test_curve(&rng, 16);
  10641. if (ret < 0) {
  10642. goto done;
  10643. }
  10644. #endif /* HAVE_ECC128 */
  10645. #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
  10646. ret = ecc_test_curve(&rng, 20);
  10647. if (ret < 0) {
  10648. goto done;
  10649. }
  10650. #endif /* HAVE_ECC160 */
  10651. #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
  10652. ret = ecc_test_curve(&rng, 24);
  10653. if (ret < 0) {
  10654. goto done;
  10655. }
  10656. #endif /* HAVE_ECC192 */
  10657. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  10658. ret = ecc_test_curve(&rng, 28);
  10659. if (ret < 0) {
  10660. goto done;
  10661. }
  10662. #endif /* HAVE_ECC224 */
  10663. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  10664. ret = ecc_test_curve(&rng, 30);
  10665. if (ret < 0) {
  10666. goto done;
  10667. }
  10668. #endif /* HAVE_ECC239 */
  10669. #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
  10670. ret = ecc_test_curve(&rng, 32);
  10671. if (ret < 0) {
  10672. goto done;
  10673. }
  10674. #if !defined(WOLFSSL_ATECC508A) && defined(HAVE_ECC_KEY_IMPORT) && \
  10675. defined(HAVE_ECC_KEY_EXPORT)
  10676. ret = ecc_point_test();
  10677. if (ret < 0) {
  10678. goto done;
  10679. }
  10680. #endif
  10681. ret = ecc_def_curve_test(&rng);
  10682. if (ret < 0) {
  10683. goto done;
  10684. }
  10685. #endif /* !NO_ECC256 */
  10686. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  10687. ret = ecc_test_curve(&rng, 40);
  10688. if (ret < 0) {
  10689. goto done;
  10690. }
  10691. #endif /* HAVE_ECC320 */
  10692. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  10693. ret = ecc_test_curve(&rng, 48);
  10694. if (ret < 0) {
  10695. goto done;
  10696. }
  10697. #endif /* HAVE_ECC384 */
  10698. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  10699. ret = ecc_test_curve(&rng, 64);
  10700. if (ret < 0) {
  10701. goto done;
  10702. }
  10703. #endif /* HAVE_ECC512 */
  10704. #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
  10705. ret = ecc_test_curve(&rng, 66);
  10706. if (ret < 0) {
  10707. goto done;
  10708. }
  10709. #endif /* HAVE_ECC521 */
  10710. #if defined(WOLFSSL_CUSTOM_CURVES)
  10711. ret = ecc_test_custom_curves(&rng);
  10712. if (ret != 0) {
  10713. goto done;
  10714. }
  10715. #endif
  10716. #ifdef HAVE_ECC_CDH
  10717. ret = ecc_test_cdh_vectors();
  10718. if (ret != 0) {
  10719. printf("ecc_test_cdh_vectors failed! %d\n", ret);
  10720. goto done;
  10721. }
  10722. #endif
  10723. ret = ecc_test_make_pub(&rng);
  10724. if (ret != 0) {
  10725. printf("ecc_test_make_pub failed!: %d\n", ret);
  10726. goto done;
  10727. }
  10728. #ifdef WOLFSSL_CERT_GEN
  10729. ret = ecc_test_cert_gen(&rng);
  10730. if (ret != 0) {
  10731. printf("ecc_test_cert_gen failed!: %d\n", ret);
  10732. goto done;
  10733. }
  10734. #endif
  10735. done:
  10736. wc_FreeRng(&rng);
  10737. return ret;
  10738. }
  10739. #ifdef HAVE_ECC_ENCRYPT
  10740. int ecc_encrypt_test(void)
  10741. {
  10742. WC_RNG rng;
  10743. int ret = 0;
  10744. ecc_key userA, userB;
  10745. byte msg[48];
  10746. byte plain[48];
  10747. byte out[80];
  10748. word32 outSz = sizeof(out);
  10749. word32 plainSz = sizeof(plain);
  10750. int i;
  10751. ecEncCtx* cliCtx = NULL;
  10752. ecEncCtx* srvCtx = NULL;
  10753. byte cliSalt[EXCHANGE_SALT_SZ];
  10754. byte srvSalt[EXCHANGE_SALT_SZ];
  10755. const byte* tmpSalt;
  10756. byte msg2[48];
  10757. byte plain2[48];
  10758. byte out2[80];
  10759. word32 outSz2 = sizeof(out2);
  10760. word32 plainSz2 = sizeof(plain2);
  10761. #ifndef HAVE_FIPS
  10762. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  10763. #else
  10764. ret = wc_InitRng(&rng);
  10765. #endif
  10766. if (ret != 0)
  10767. return -6900;
  10768. XMEMSET(&userA, 0, sizeof(userA));
  10769. XMEMSET(&userB, 0, sizeof(userB));
  10770. ret = wc_ecc_init_ex(&userA, HEAP_HINT, devId);
  10771. if (ret != 0)
  10772. goto done;
  10773. ret = wc_ecc_init_ex(&userB, HEAP_HINT, devId);
  10774. if (ret != 0)
  10775. goto done;
  10776. ret = wc_ecc_make_key(&rng, 32, &userA);
  10777. #if defined(WOLFSSL_ASYNC_CRYPT)
  10778. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_NONE);
  10779. #endif
  10780. if (ret != 0){
  10781. ret = -6901; goto done;
  10782. }
  10783. ret = wc_ecc_make_key(&rng, 32, &userB);
  10784. #if defined(WOLFSSL_ASYNC_CRYPT)
  10785. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_NONE);
  10786. #endif
  10787. if (ret != 0){
  10788. ret = -6902; goto done;
  10789. }
  10790. /* set message to incrementing 0,1,2,etc... */
  10791. for (i = 0; i < (int)sizeof(msg); i++)
  10792. msg[i] = i;
  10793. /* encrypt msg to B */
  10794. ret = wc_ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz, NULL);
  10795. if (ret != 0) {
  10796. ret = -6903; goto done;
  10797. }
  10798. /* decrypt msg from A */
  10799. ret = wc_ecc_decrypt(&userB, &userA, out, outSz, plain, &plainSz, NULL);
  10800. if (ret != 0) {
  10801. ret = -6904; goto done;
  10802. }
  10803. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  10804. ret = -6905; goto done;
  10805. }
  10806. /* let's verify message exchange works, A is client, B is server */
  10807. cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  10808. srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  10809. if (cliCtx == NULL || srvCtx == NULL) {
  10810. ret = -6906; goto done;
  10811. }
  10812. /* get salt to send to peer */
  10813. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  10814. if (tmpSalt == NULL) {
  10815. ret = -6907; goto done;
  10816. }
  10817. XMEMCPY(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  10818. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  10819. if (tmpSalt == NULL) {
  10820. ret = -6908; goto done;
  10821. }
  10822. XMEMCPY(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  10823. /* in actual use, we'd get the peer's salt over the transport */
  10824. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  10825. if (ret != 0)
  10826. goto done;
  10827. ret = wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  10828. if (ret != 0)
  10829. goto done;
  10830. ret = wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  10831. if (ret != 0)
  10832. goto done;
  10833. ret = wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  10834. if (ret != 0)
  10835. goto done;
  10836. /* get encrypted msg (request) to send to B */
  10837. outSz = sizeof(out);
  10838. ret = wc_ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz,cliCtx);
  10839. if (ret != 0)
  10840. goto done;
  10841. /* B decrypts msg (request) from A */
  10842. plainSz = sizeof(plain);
  10843. ret = wc_ecc_decrypt(&userB, &userA, out, outSz, plain, &plainSz, srvCtx);
  10844. if (ret != 0)
  10845. goto done;
  10846. if (XMEMCMP(plain, msg, sizeof(msg)) != 0) {
  10847. ret = -6909; goto done;
  10848. }
  10849. /* msg2 (response) from B to A */
  10850. for (i = 0; i < (int)sizeof(msg2); i++)
  10851. msg2[i] = i + sizeof(msg2);
  10852. /* get encrypted msg (response) to send to B */
  10853. ret = wc_ecc_encrypt(&userB, &userA, msg2, sizeof(msg2), out2,
  10854. &outSz2, srvCtx);
  10855. if (ret != 0)
  10856. goto done;
  10857. /* A decrypts msg (response) from B */
  10858. ret = wc_ecc_decrypt(&userA, &userB, out2, outSz2, plain2, &plainSz2,
  10859. cliCtx);
  10860. if (ret != 0)
  10861. goto done;
  10862. if (XMEMCMP(plain2, msg2, sizeof(msg2)) != 0) {
  10863. ret = -6910; goto done;
  10864. }
  10865. done:
  10866. /* cleanup */
  10867. wc_ecc_ctx_free(srvCtx);
  10868. wc_ecc_ctx_free(cliCtx);
  10869. wc_ecc_free(&userB);
  10870. wc_ecc_free(&userA);
  10871. wc_FreeRng(&rng);
  10872. return ret;
  10873. }
  10874. #endif /* HAVE_ECC_ENCRYPT */
  10875. #ifdef USE_CERT_BUFFERS_256
  10876. int ecc_test_buffers(void) {
  10877. size_t bytes;
  10878. ecc_key cliKey;
  10879. ecc_key servKey;
  10880. WC_RNG rng;
  10881. word32 idx = 0;
  10882. int ret;
  10883. /* pad our test message to 32 bytes so evenly divisible by AES_BLOCK_SZ */
  10884. byte in[] = "Everyone gets Friday off. ecc p";
  10885. word32 inLen = (word32)XSTRLEN((char*)in);
  10886. byte out[256];
  10887. byte plain[256];
  10888. int verify = 0;
  10889. word32 x;
  10890. bytes = (size_t)sizeof_ecc_clikey_der_256;
  10891. /* place client key into ecc_key struct cliKey */
  10892. ret = wc_EccPrivateKeyDecode(ecc_clikey_der_256, &idx, &cliKey,
  10893. (word32)bytes);
  10894. if (ret != 0)
  10895. return -6915;
  10896. idx = 0;
  10897. bytes = (size_t)sizeof_ecc_key_der_256;
  10898. /* place server key into ecc_key struct servKey */
  10899. ret = wc_EccPrivateKeyDecode(ecc_key_der_256, &idx, &servKey,
  10900. (word32)bytes);
  10901. if (ret != 0)
  10902. return -6916;
  10903. #ifndef HAVE_FIPS
  10904. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  10905. #else
  10906. ret = wc_InitRng(&rng);
  10907. #endif
  10908. if (ret != 0)
  10909. return -6917;
  10910. #if defined(HAVE_ECC_ENCRYPT) && defined(HAVE_HKDF)
  10911. {
  10912. word32 y;
  10913. /* test encrypt and decrypt if they're available */
  10914. x = sizeof(out);
  10915. ret = wc_ecc_encrypt(&cliKey, &servKey, in, sizeof(in), out, &x, NULL);
  10916. if (ret < 0)
  10917. return -6918;
  10918. y = sizeof(plain);
  10919. ret = wc_ecc_decrypt(&cliKey, &servKey, out, x, plain, &y, NULL);
  10920. if (ret < 0)
  10921. return -6919;
  10922. if (XMEMCMP(plain, in, inLen))
  10923. return -6920;
  10924. }
  10925. #endif
  10926. x = sizeof(out);
  10927. ret = wc_ecc_sign_hash(in, inLen, out, &x, &rng, &cliKey);
  10928. if (ret < 0)
  10929. return -6921;
  10930. XMEMSET(plain, 0, sizeof(plain));
  10931. ret = wc_ecc_verify_hash(out, x, plain, sizeof(plain), &verify, &cliKey);
  10932. if (ret < 0)
  10933. return -6922;
  10934. if (XMEMCMP(plain, in, (word32)ret))
  10935. return -6923;
  10936. #ifdef WOLFSSL_CERT_EXT
  10937. idx = 0;
  10938. bytes = sizeof_ecc_clikeypub_der_256;
  10939. ret = wc_EccPublicKeyDecode(ecc_clikeypub_der_256, &idx, &cliKey,
  10940. (word32) bytes);
  10941. if (ret != 0)
  10942. return -6924;
  10943. #endif
  10944. wc_ecc_free(&cliKey);
  10945. wc_ecc_free(&servKey);
  10946. wc_FreeRng(&rng);
  10947. return 0;
  10948. }
  10949. #endif /* USE_CERT_BUFFERS_256 */
  10950. #endif /* HAVE_ECC */
  10951. #ifdef HAVE_CURVE25519
  10952. int curve25519_test(void)
  10953. {
  10954. WC_RNG rng;
  10955. int ret;
  10956. #ifdef HAVE_CURVE25519_SHARED_SECRET
  10957. byte sharedA[32];
  10958. byte sharedB[32];
  10959. word32 y;
  10960. #endif
  10961. #ifdef HAVE_CURVE25519_KEY_EXPORT
  10962. byte exportBuf[32];
  10963. #endif
  10964. word32 x;
  10965. curve25519_key userA, userB, pubKey;
  10966. #if defined(HAVE_CURVE25519_SHARED_SECRET) && defined(HAVE_CURVE25519_KEY_IMPORT)
  10967. /* test vectors from
  10968. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  10969. */
  10970. /* secret key for party a */
  10971. byte sa[] = {
  10972. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  10973. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  10974. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  10975. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  10976. };
  10977. /* public key for party a */
  10978. byte pa[] = {
  10979. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  10980. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  10981. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  10982. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  10983. };
  10984. /* secret key for party b */
  10985. byte sb[] = {
  10986. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  10987. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  10988. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  10989. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  10990. };
  10991. /* public key for party b */
  10992. byte pb[] = {
  10993. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  10994. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  10995. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  10996. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  10997. };
  10998. /* expected shared key */
  10999. byte ss[] = {
  11000. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  11001. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  11002. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  11003. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  11004. };
  11005. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  11006. #ifndef HAVE_FIPS
  11007. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  11008. #else
  11009. ret = wc_InitRng(&rng);
  11010. #endif
  11011. if (ret != 0)
  11012. return -7000;
  11013. wc_curve25519_init(&userA);
  11014. wc_curve25519_init(&userB);
  11015. wc_curve25519_init(&pubKey);
  11016. /* make curve25519 keys */
  11017. if (wc_curve25519_make_key(&rng, 32, &userA) != 0)
  11018. return -7001;
  11019. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  11020. return -7002;
  11021. #ifdef HAVE_CURVE25519_SHARED_SECRET
  11022. /* find shared secret key */
  11023. x = sizeof(sharedA);
  11024. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  11025. return -7003;
  11026. y = sizeof(sharedB);
  11027. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  11028. return -7004;
  11029. /* compare shared secret keys to test they are the same */
  11030. if (y != x)
  11031. return -7005;
  11032. if (XMEMCMP(sharedA, sharedB, x))
  11033. return -7006;
  11034. #endif
  11035. #ifdef HAVE_CURVE25519_KEY_EXPORT
  11036. /* export a public key and import it for another user */
  11037. x = sizeof(exportBuf);
  11038. if (wc_curve25519_export_public(&userA, exportBuf, &x) != 0)
  11039. return -7007;
  11040. #ifdef HAVE_CURVE25519_KEY_IMPORT
  11041. if (wc_curve25519_import_public(exportBuf, x, &pubKey) != 0)
  11042. return -7008;
  11043. #endif
  11044. #endif
  11045. #if defined(HAVE_CURVE25519_SHARED_SECRET) && defined(HAVE_CURVE25519_KEY_IMPORT)
  11046. /* test shared key after importing a public key */
  11047. XMEMSET(sharedB, 0, sizeof(sharedB));
  11048. y = sizeof(sharedB);
  11049. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  11050. return -7009;
  11051. if (XMEMCMP(sharedA, sharedB, y))
  11052. return -7010;
  11053. /* import RFC test vectors and compare shared key */
  11054. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  11055. != 0)
  11056. return -7011;
  11057. if (wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  11058. != 0)
  11059. return -7012;
  11060. /* test against known test vector */
  11061. XMEMSET(sharedB, 0, sizeof(sharedB));
  11062. y = sizeof(sharedB);
  11063. if (wc_curve25519_shared_secret(&userA, &userB, sharedB, &y) != 0)
  11064. return -7013;
  11065. if (XMEMCMP(ss, sharedB, y))
  11066. return -7014;
  11067. /* test swaping roles of keys and generating same shared key */
  11068. XMEMSET(sharedB, 0, sizeof(sharedB));
  11069. y = sizeof(sharedB);
  11070. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  11071. return -7015;
  11072. if (XMEMCMP(ss, sharedB, y))
  11073. return -7016;
  11074. /* test with 1 generated key and 1 from known test vector */
  11075. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  11076. != 0)
  11077. return -7017;
  11078. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  11079. return -7018;
  11080. x = sizeof(sharedA);
  11081. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  11082. return -7019;
  11083. y = sizeof(sharedB);
  11084. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  11085. return -7020;
  11086. /* compare shared secret keys to test they are the same */
  11087. if (y != x)
  11088. return -7021;
  11089. if (XMEMCMP(sharedA, sharedB, x))
  11090. return -7022;
  11091. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  11092. /* clean up keys when done */
  11093. wc_curve25519_free(&pubKey);
  11094. wc_curve25519_free(&userB);
  11095. wc_curve25519_free(&userA);
  11096. wc_FreeRng(&rng);
  11097. return 0;
  11098. }
  11099. #endif /* HAVE_CURVE25519 */
  11100. #ifdef HAVE_ED25519
  11101. #ifdef WOLFSSL_TEST_CERT
  11102. static int ed25519_test_cert(void)
  11103. {
  11104. DecodedCert cert[2];
  11105. DecodedCert* serverCert = NULL;
  11106. DecodedCert* caCert = NULL;
  11107. #ifdef HAVE_ED25519_VERIFY
  11108. ed25519_key key;
  11109. ed25519_key* pubKey = NULL;
  11110. int verify;
  11111. #endif /* HAVE_ED25519_VERIFY */
  11112. int ret;
  11113. byte* tmp;
  11114. int bytes;
  11115. FILE* file;
  11116. tmp = XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11117. if (tmp == NULL) {
  11118. ERROR_OUT(-7200, done);
  11119. }
  11120. #ifdef USE_CERT_BUFFERS_256
  11121. XMEMCPY(tmp, ca_ed25519_cert, sizeof_ca_ed25519_cert);
  11122. bytes = sizeof_ca_ed25519_cert;
  11123. #elif !defined(NO_FILESYSTEM)
  11124. file = fopen(caEd25519Cert, "rb");
  11125. if (file == NULL) {
  11126. ERROR_OUT(-7201, done);
  11127. }
  11128. bytes = fread(tmp, 1, FOURK_BUF, file);
  11129. fclose(file);
  11130. #else
  11131. /* No certificate to use. */
  11132. ERROR_OUT(-7202, done);
  11133. #endif
  11134. InitDecodedCert(&cert[0], tmp, (word32)bytes, 0);
  11135. caCert = &cert[0];
  11136. ret = ParseCert(caCert, CERT_TYPE, NO_VERIFY, NULL);
  11137. if (ret != 0) {
  11138. ERROR_OUT(-7203, done);
  11139. }
  11140. #ifdef USE_CERT_BUFFERS_256
  11141. XMEMCPY(tmp, server_ed25519_cert, sizeof_server_ed25519_cert);
  11142. bytes = sizeof_server_ed25519_cert;
  11143. #elif !defined(NO_FILESYSTEM)
  11144. file = fopen(serverEd25519Cert, "rb");
  11145. if (file == NULL) {
  11146. ERROR_OUT(-7204, done);
  11147. }
  11148. bytes = fread(tmp, 1, FOURK_BUF, file);
  11149. fclose(file);
  11150. #else
  11151. /* No certificate to use. */
  11152. ERROR_OUT(-7205, done);
  11153. #endif
  11154. InitDecodedCert(&cert[1], tmp, (word32)bytes, 0);
  11155. serverCert = &cert[1];
  11156. ret = ParseCert(serverCert, CERT_TYPE, NO_VERIFY, NULL);
  11157. if (ret != 0) {
  11158. ERROR_OUT(-7206, done);
  11159. }
  11160. #ifdef HAVE_ED25519_VERIFY
  11161. ret = wc_ed25519_init(&key);
  11162. if (ret < 0) {
  11163. ERROR_OUT(-7207, done);
  11164. }
  11165. pubKey = &key;
  11166. ret = wc_ed25519_import_public(caCert->publicKey, caCert->pubKeySize,
  11167. pubKey);
  11168. if (ret < 0) {
  11169. ERROR_OUT(-7208, done);
  11170. }
  11171. if (wc_ed25519_verify_msg(serverCert->signature, serverCert->sigLength,
  11172. serverCert->source + serverCert->certBegin,
  11173. serverCert->sigIndex - serverCert->certBegin,
  11174. &verify, pubKey) < 0 || verify != 1) {
  11175. ERROR_OUT(-7209, done);
  11176. }
  11177. #endif /* HAVE_ED25519_VERIFY */
  11178. done:
  11179. if (tmp != NULL)
  11180. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11181. #ifdef HAVE_ED25519_VERIFY
  11182. wc_ed25519_free(pubKey);
  11183. #endif /* HAVE_ED25519_VERIFY */
  11184. if (caCert != NULL)
  11185. FreeDecodedCert(caCert);
  11186. if (serverCert != NULL)
  11187. FreeDecodedCert(serverCert);
  11188. return ret;
  11189. }
  11190. static int ed25519_test_make_cert(void)
  11191. {
  11192. WC_RNG rng;
  11193. Cert cert;
  11194. DecodedCert decode;
  11195. ed25519_key key;
  11196. ed25519_key* privKey = NULL;
  11197. int ret = 0;
  11198. byte* tmp = NULL;
  11199. wc_InitCert(&cert);
  11200. #ifndef HAVE_FIPS
  11201. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  11202. #else
  11203. ret = wc_InitRng(&rng);
  11204. #endif
  11205. if (ret != 0)
  11206. return -7220;
  11207. wc_ed25519_init(&key);
  11208. privKey = &key;
  11209. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, privKey);
  11210. cert.daysValid = 365 * 2;
  11211. cert.selfSigned = 1;
  11212. XMEMCPY(&cert.issuer, &certDefaultName, sizeof(CertName));
  11213. XMEMCPY(&cert.subject, &certDefaultName, sizeof(CertName));
  11214. cert.isCA = 0;
  11215. #ifdef WOLFSSL_CERT_EXT
  11216. ret = wc_SetKeyUsage(&cert, certKeyUsage);
  11217. if (ret < 0) {
  11218. ERROR_OUT(-7221, done);
  11219. }
  11220. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  11221. if (ret < 0) {
  11222. ERROR_OUT(-7222, done);
  11223. }
  11224. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE, privKey);
  11225. if (ret < 0) {
  11226. ERROR_OUT(-7223, done);
  11227. }
  11228. #endif
  11229. tmp = XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11230. if (tmp == NULL) {
  11231. ERROR_OUT(-7224, done);
  11232. }
  11233. cert.sigType = CTC_ED25519;
  11234. ret = wc_MakeCert_ex(&cert, tmp, FOURK_BUF, ED25519_TYPE, privKey, &rng);
  11235. if (ret < 0) {
  11236. ERROR_OUT(-7225, done);
  11237. }
  11238. ret = wc_SignCert_ex(cert.bodySz, cert.sigType, tmp, FOURK_BUF,
  11239. ED25519_TYPE, privKey, &rng);
  11240. if (ret < 0) {
  11241. ERROR_OUT(-7226, done);
  11242. }
  11243. InitDecodedCert(&decode, tmp, ret, HEAP_HINT);
  11244. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  11245. FreeDecodedCert(&decode);
  11246. if (ret != 0) {
  11247. ERROR_OUT(-7227, done);
  11248. }
  11249. done:
  11250. if (tmp != NULL)
  11251. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11252. wc_ed25519_free(privKey);
  11253. wc_FreeRng(&rng);
  11254. return ret;
  11255. }
  11256. #endif /* WOLFSSL_TEST_CERT */
  11257. int ed25519_test(void)
  11258. {
  11259. int ret;
  11260. WC_RNG rng;
  11261. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  11262. defined(HAVE_ED25519_KEY_IMPORT)
  11263. byte out[ED25519_SIG_SIZE];
  11264. byte exportPKey[ED25519_KEY_SIZE];
  11265. byte exportSKey[ED25519_KEY_SIZE];
  11266. word32 exportPSz;
  11267. word32 exportSSz;
  11268. int i;
  11269. word32 outlen;
  11270. #ifdef HAVE_ED25519_VERIFY
  11271. int verify;
  11272. #endif /* HAVE_ED25519_VERIFY */
  11273. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  11274. word32 keySz, sigSz;
  11275. ed25519_key key;
  11276. ed25519_key key2;
  11277. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) && \
  11278. defined(HAVE_ED25519_KEY_IMPORT)
  11279. /* test vectors from
  11280. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  11281. */
  11282. static const byte sKey1[] = {
  11283. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  11284. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  11285. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  11286. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  11287. };
  11288. static const byte sKey2[] = {
  11289. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  11290. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  11291. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  11292. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  11293. };
  11294. static const byte sKey3[] = {
  11295. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  11296. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  11297. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  11298. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  11299. };
  11300. /* uncompressed test */
  11301. static const byte sKey4[] = {
  11302. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  11303. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  11304. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  11305. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  11306. };
  11307. /* compressed prefix test */
  11308. static const byte sKey5[] = {
  11309. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  11310. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  11311. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  11312. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  11313. };
  11314. static const byte sKey6[] = {
  11315. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  11316. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  11317. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  11318. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  11319. };
  11320. static const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  11321. static const byte pKey1[] = {
  11322. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  11323. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  11324. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  11325. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  11326. };
  11327. static const byte pKey2[] = {
  11328. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  11329. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  11330. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  11331. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  11332. };
  11333. static const byte pKey3[] = {
  11334. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  11335. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  11336. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  11337. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  11338. };
  11339. /* uncompressed test */
  11340. static const byte pKey4[] = {
  11341. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  11342. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  11343. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  11344. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  11345. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  11346. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  11347. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  11348. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  11349. 0xd7
  11350. };
  11351. /* compressed prefix */
  11352. static const byte pKey5[] = {
  11353. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  11354. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  11355. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  11356. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  11357. };
  11358. static const byte pKey6[] = {
  11359. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  11360. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  11361. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  11362. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  11363. };
  11364. static const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  11365. static const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  11366. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  11367. static const byte sig1[] = {
  11368. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  11369. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  11370. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  11371. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  11372. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  11373. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  11374. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  11375. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  11376. };
  11377. static const byte sig2[] = {
  11378. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  11379. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  11380. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  11381. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  11382. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  11383. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  11384. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  11385. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  11386. };
  11387. static const byte sig3[] = {
  11388. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  11389. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  11390. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  11391. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  11392. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  11393. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  11394. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  11395. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  11396. };
  11397. /* uncompressed test */
  11398. static const byte sig4[] = {
  11399. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  11400. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  11401. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  11402. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  11403. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  11404. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  11405. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  11406. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  11407. };
  11408. /* compressed prefix */
  11409. static const byte sig5[] = {
  11410. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  11411. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  11412. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  11413. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  11414. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  11415. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  11416. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  11417. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  11418. };
  11419. static const byte sig6[] = {
  11420. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  11421. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  11422. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  11423. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  11424. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  11425. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  11426. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  11427. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  11428. };
  11429. static const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  11430. static const byte msg1[] = {0x0 };
  11431. static const byte msg2[] = {0x72};
  11432. static const byte msg3[] = {0xAF,0x82};
  11433. /* test of a 1024 byte long message */
  11434. static const byte msg4[] = {
  11435. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  11436. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  11437. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  11438. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  11439. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  11440. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  11441. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  11442. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  11443. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  11444. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  11445. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  11446. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  11447. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  11448. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  11449. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  11450. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  11451. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  11452. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  11453. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  11454. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  11455. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  11456. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  11457. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  11458. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  11459. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  11460. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  11461. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  11462. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  11463. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  11464. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  11465. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  11466. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  11467. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  11468. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  11469. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  11470. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  11471. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  11472. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  11473. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  11474. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  11475. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  11476. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  11477. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  11478. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  11479. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  11480. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  11481. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  11482. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  11483. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  11484. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  11485. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  11486. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  11487. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  11488. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  11489. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  11490. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  11491. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  11492. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  11493. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  11494. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  11495. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  11496. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  11497. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  11498. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  11499. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  11500. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  11501. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  11502. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  11503. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  11504. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  11505. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  11506. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  11507. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  11508. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  11509. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  11510. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  11511. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  11512. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  11513. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  11514. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  11515. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  11516. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  11517. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  11518. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  11519. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  11520. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  11521. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  11522. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  11523. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  11524. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  11525. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  11526. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  11527. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  11528. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  11529. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  11530. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  11531. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  11532. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  11533. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  11534. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  11535. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  11536. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  11537. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  11538. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  11539. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  11540. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  11541. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  11542. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  11543. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  11544. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  11545. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  11546. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  11547. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  11548. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  11549. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  11550. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  11551. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  11552. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  11553. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  11554. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  11555. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  11556. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  11557. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  11558. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  11559. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  11560. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  11561. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  11562. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  11563. };
  11564. static const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  11565. static const word16 msgSz[] = {0 /*sizeof(msg1)*/,
  11566. sizeof(msg2),
  11567. sizeof(msg3),
  11568. 0 /*sizeof(msg1)*/,
  11569. 0 /*sizeof(msg1)*/,
  11570. sizeof(msg4)
  11571. };
  11572. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  11573. /* create ed25519 keys */
  11574. #ifndef HAVE_FIPS
  11575. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  11576. #else
  11577. ret = wc_InitRng(&rng);
  11578. #endif
  11579. if (ret != 0)
  11580. return -7100;
  11581. wc_ed25519_init(&key);
  11582. wc_ed25519_init(&key2);
  11583. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  11584. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  11585. /* helper functions for signature and key size */
  11586. keySz = wc_ed25519_size(&key);
  11587. sigSz = wc_ed25519_sig_size(&key);
  11588. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_EXPORT) &&\
  11589. defined(HAVE_ED25519_KEY_IMPORT)
  11590. for (i = 0; i < 6; i++) {
  11591. outlen = sizeof(out);
  11592. XMEMSET(out, 0, sizeof(out));
  11593. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  11594. pKeySz[i], &key) != 0)
  11595. return -7101 - i;
  11596. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key)
  11597. != 0)
  11598. return -7111 - i;
  11599. if (XMEMCMP(out, sigs[i], 64))
  11600. return -7121 - i;
  11601. #if defined(HAVE_ED25519_VERIFY)
  11602. /* test verify on good msg */
  11603. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  11604. &key) != 0 || verify != 1)
  11605. return -7131 - i;
  11606. /* test verify on bad msg */
  11607. out[outlen-1] = out[outlen-1] + 1;
  11608. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  11609. &key) == 0 || verify == 1)
  11610. return -7141 - i;
  11611. #endif /* HAVE_ED25519_VERIFY */
  11612. /* test api for import/exporting keys */
  11613. exportPSz = sizeof(exportPKey);
  11614. exportSSz = sizeof(exportSKey);
  11615. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  11616. return -7151 - i;
  11617. if (wc_ed25519_import_public(exportPKey, exportPSz, &key2) != 0)
  11618. return -7161 - i;
  11619. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  11620. return -7171 - i;
  11621. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  11622. exportPKey, exportPSz, &key2) != 0)
  11623. return -7181 - i;
  11624. /* clear "out" buffer and test sign with imported keys */
  11625. outlen = sizeof(out);
  11626. XMEMSET(out, 0, sizeof(out));
  11627. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  11628. return -7191 - i;
  11629. #if defined(HAVE_ED25519_VERIFY)
  11630. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  11631. &key2) != 0 || verify != 1)
  11632. return -7201 - i;
  11633. if (XMEMCMP(out, sigs[i], 64))
  11634. return -7211 - i;
  11635. #endif /* HAVE_ED25519_VERIFY */
  11636. }
  11637. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_EXPORT && HAVE_ED25519_KEY_IMPORT */
  11638. /* clean up keys when done */
  11639. wc_ed25519_free(&key);
  11640. wc_ed25519_free(&key2);
  11641. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  11642. wc_FreeRng(&rng);
  11643. #endif
  11644. /* hush warnings of unused keySz and sigSz */
  11645. (void)keySz;
  11646. (void)sigSz;
  11647. #ifdef WOLFSSL_TEST_CERT
  11648. ret = ed25519_test_cert();
  11649. if (ret < 0)
  11650. return ret;
  11651. #ifdef WOLFSSL_CERT_GEN
  11652. ret = ed25519_test_make_cert();
  11653. if (ret < 0)
  11654. return ret;
  11655. #endif /* WOLFSSL_CERT_GEN */
  11656. #endif /* WOLFSSL_TEST_CERT */
  11657. return 0;
  11658. }
  11659. #endif /* HAVE_ED25519 */
  11660. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  11661. typedef struct CMAC_Test_Case {
  11662. int type;
  11663. int partial;
  11664. const byte* m;
  11665. word32 mSz;
  11666. const byte* k;
  11667. word32 kSz;
  11668. const byte* t;
  11669. word32 tSz;
  11670. } CMAC_Test_Case;
  11671. int cmac_test(void)
  11672. {
  11673. const byte k128[] =
  11674. {
  11675. 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
  11676. 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
  11677. };
  11678. const byte k192[] =
  11679. {
  11680. 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
  11681. 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
  11682. 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
  11683. };
  11684. const byte k256[] =
  11685. {
  11686. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  11687. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  11688. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  11689. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  11690. };
  11691. #define KLEN_128 (sizeof(k128))
  11692. #define KLEN_192 (sizeof(k192))
  11693. #define KLEN_256 (sizeof(k256))
  11694. const byte m[] =
  11695. {
  11696. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  11697. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
  11698. 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  11699. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
  11700. 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
  11701. 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  11702. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
  11703. 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
  11704. };
  11705. #define MLEN_0 (0)
  11706. #define MLEN_128 (128/8)
  11707. #define MLEN_320 (320/8)
  11708. #define MLEN_319 (MLEN_320 - 1)
  11709. #define MLEN_512 (512/8)
  11710. const byte t128_0[] =
  11711. {
  11712. 0xbb, 0x1d, 0x69, 0x29, 0xe9, 0x59, 0x37, 0x28,
  11713. 0x7f, 0xa3, 0x7d, 0x12, 0x9b, 0x75, 0x67, 0x46
  11714. };
  11715. const byte t128_128[] =
  11716. {
  11717. 0x07, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
  11718. 0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
  11719. };
  11720. const byte t128_319[] =
  11721. {
  11722. 0x2c, 0x17, 0x84, 0x4c, 0x93, 0x1c, 0x07, 0x95,
  11723. 0x15, 0x92, 0x73, 0x0a, 0x34, 0xd0, 0xd9, 0xd2
  11724. };
  11725. const byte t128_320[] =
  11726. {
  11727. 0xdf, 0xa6, 0x67, 0x47, 0xde, 0x9a, 0xe6, 0x30,
  11728. 0x30, 0xca, 0x32, 0x61, 0x14, 0x97, 0xc8, 0x27
  11729. };
  11730. const byte t128_512[] =
  11731. {
  11732. 0x51, 0xf0, 0xbe, 0xbf, 0x7e, 0x3b, 0x9d, 0x92,
  11733. 0xfc, 0x49, 0x74, 0x17, 0x79, 0x36, 0x3c, 0xfe
  11734. };
  11735. const byte t192_0[] =
  11736. {
  11737. 0xd1, 0x7d, 0xdf, 0x46, 0xad, 0xaa, 0xcd, 0xe5,
  11738. 0x31, 0xca, 0xc4, 0x83, 0xde, 0x7a, 0x93, 0x67
  11739. };
  11740. const byte t192_128[] =
  11741. {
  11742. 0x9e, 0x99, 0xa7, 0xbf, 0x31, 0xe7, 0x10, 0x90,
  11743. 0x06, 0x62, 0xf6, 0x5e, 0x61, 0x7c, 0x51, 0x84
  11744. };
  11745. const byte t192_320[] =
  11746. {
  11747. 0x8a, 0x1d, 0xe5, 0xbe, 0x2e, 0xb3, 0x1a, 0xad,
  11748. 0x08, 0x9a, 0x82, 0xe6, 0xee, 0x90, 0x8b, 0x0e
  11749. };
  11750. const byte t192_512[] =
  11751. {
  11752. 0xa1, 0xd5, 0xdf, 0x0e, 0xed, 0x79, 0x0f, 0x79,
  11753. 0x4d, 0x77, 0x58, 0x96, 0x59, 0xf3, 0x9a, 0x11
  11754. };
  11755. const byte t256_0[] =
  11756. {
  11757. 0x02, 0x89, 0x62, 0xf6, 0x1b, 0x7b, 0xf8, 0x9e,
  11758. 0xfc, 0x6b, 0x55, 0x1f, 0x46, 0x67, 0xd9, 0x83
  11759. };
  11760. const byte t256_128[] =
  11761. {
  11762. 0x28, 0xa7, 0x02, 0x3f, 0x45, 0x2e, 0x8f, 0x82,
  11763. 0xbd, 0x4b, 0xf2, 0x8d, 0x8c, 0x37, 0xc3, 0x5c
  11764. };
  11765. const byte t256_320[] =
  11766. {
  11767. 0xaa, 0xf3, 0xd8, 0xf1, 0xde, 0x56, 0x40, 0xc2,
  11768. 0x32, 0xf5, 0xb1, 0x69, 0xb9, 0xc9, 0x11, 0xe6
  11769. };
  11770. const byte t256_512[] =
  11771. {
  11772. 0xe1, 0x99, 0x21, 0x90, 0x54, 0x9f, 0x6e, 0xd5,
  11773. 0x69, 0x6a, 0x2c, 0x05, 0x6c, 0x31, 0x54, 0x10
  11774. };
  11775. const CMAC_Test_Case testCases[] =
  11776. {
  11777. {WC_CMAC_AES, 0, m, MLEN_0, k128, KLEN_128, t128_0, AES_BLOCK_SIZE},
  11778. {WC_CMAC_AES, 0, m, MLEN_128, k128, KLEN_128, t128_128, AES_BLOCK_SIZE},
  11779. {WC_CMAC_AES, 0, m, MLEN_320, k128, KLEN_128, t128_320, AES_BLOCK_SIZE},
  11780. {WC_CMAC_AES, 0, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  11781. {WC_CMAC_AES, 5, m, MLEN_512, k128, KLEN_128, t128_512, AES_BLOCK_SIZE},
  11782. {WC_CMAC_AES, 0, m, MLEN_0, k192, KLEN_192, t192_0, AES_BLOCK_SIZE},
  11783. {WC_CMAC_AES, 0, m, MLEN_128, k192, KLEN_192, t192_128, AES_BLOCK_SIZE},
  11784. {WC_CMAC_AES, 0, m, MLEN_320, k192, KLEN_192, t192_320, AES_BLOCK_SIZE},
  11785. {WC_CMAC_AES, 0, m, MLEN_512, k192, KLEN_192, t192_512, AES_BLOCK_SIZE},
  11786. {WC_CMAC_AES, 0, m, MLEN_0, k256, KLEN_256, t256_0, AES_BLOCK_SIZE},
  11787. {WC_CMAC_AES, 0, m, MLEN_128, k256, KLEN_256, t256_128, AES_BLOCK_SIZE},
  11788. {WC_CMAC_AES, 0, m, MLEN_320, k256, KLEN_256, t256_320, AES_BLOCK_SIZE},
  11789. {WC_CMAC_AES, 0, m, MLEN_512, k256, KLEN_256, t256_512, AES_BLOCK_SIZE},
  11790. {WC_CMAC_AES, 0, m, MLEN_319, k128, KLEN_128, t128_319, AES_BLOCK_SIZE}
  11791. };
  11792. Cmac cmac;
  11793. byte tag[AES_BLOCK_SIZE];
  11794. const CMAC_Test_Case* tc;
  11795. word32 i, tagSz;
  11796. for (i = 0, tc = testCases;
  11797. i < sizeof(testCases)/sizeof(CMAC_Test_Case);
  11798. i++, tc++) {
  11799. XMEMSET(tag, 0, sizeof(tag));
  11800. tagSz = AES_BLOCK_SIZE;
  11801. if (wc_InitCmac(&cmac, tc->k, tc->kSz, tc->type, NULL) != 0)
  11802. return -7300;
  11803. if (tc->partial) {
  11804. if (wc_CmacUpdate(&cmac, tc->m,
  11805. tc->mSz/2 - tc->partial) != 0)
  11806. return -7301;
  11807. if (wc_CmacUpdate(&cmac, tc->m + tc->mSz/2 - tc->partial,
  11808. tc->mSz/2 + tc->partial) != 0)
  11809. return -7302;
  11810. }
  11811. else {
  11812. if (wc_CmacUpdate(&cmac, tc->m, tc->mSz) != 0)
  11813. return -7303;
  11814. }
  11815. if (wc_CmacFinal(&cmac, tag, &tagSz) != 0)
  11816. return -7304;
  11817. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  11818. return -7305;
  11819. XMEMSET(tag, 0, sizeof(tag));
  11820. tagSz = sizeof(tag);
  11821. if (wc_AesCmacGenerate(tag, &tagSz, tc->m, tc->mSz,
  11822. tc->k, tc->kSz) != 0)
  11823. return -7306;
  11824. if (XMEMCMP(tag, tc->t, AES_BLOCK_SIZE) != 0)
  11825. return -7307;
  11826. if (wc_AesCmacVerify(tc->t, tc->tSz, tc->m, tc->mSz,
  11827. tc->k, tc->kSz) != 0)
  11828. return -7308;
  11829. }
  11830. return 0;
  11831. }
  11832. #endif /* NO_AES && WOLFSSL_CMAC */
  11833. #ifdef HAVE_LIBZ
  11834. const byte sample_text[] =
  11835. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  11836. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  11837. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  11838. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  11839. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  11840. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  11841. "\n"
  11842. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  11843. "four loko you probably haven't heard of them high life. Messenger bag\n"
  11844. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  11845. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  11846. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  11847. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  11848. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  11849. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  11850. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  11851. "four loko whatever street art yr farm-to-table.\n"
  11852. "\n"
  11853. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  11854. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  11855. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  11856. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  11857. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  11858. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  11859. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  11860. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  11861. "locavore.\n"
  11862. "\n"
  11863. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  11864. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  11865. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  11866. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  11867. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  11868. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  11869. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  11870. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  11871. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  11872. "neutra PBR selvage.\n"
  11873. "\n"
  11874. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  11875. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  11876. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  11877. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  11878. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  11879. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  11880. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  11881. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  11882. "mollit magna, sriracha sartorial helvetica.\n"
  11883. "\n"
  11884. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  11885. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  11886. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  11887. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  11888. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  11889. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  11890. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  11891. "\n"
  11892. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  11893. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  11894. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  11895. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  11896. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  11897. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  11898. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  11899. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  11900. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  11901. "\n"
  11902. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  11903. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  11904. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  11905. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  11906. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  11907. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  11908. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  11909. "bag dolor terry richardson sapiente.\n";
  11910. int compress_test(void)
  11911. {
  11912. int ret = 0;
  11913. word32 dSz = sizeof(sample_text);
  11914. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  11915. byte *c = NULL;
  11916. byte *d = NULL;
  11917. c = XMALLOC(cSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11918. d = XMALLOC(dSz * sizeof(byte), HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11919. if (c == NULL || d == NULL) {
  11920. ERROR_OUT(-7400, exit);
  11921. }
  11922. /* follow calloc and initialize to 0 */
  11923. XMEMSET(c, 0, cSz);
  11924. XMEMSET(d, 0, dSz);
  11925. if ((ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0) {
  11926. ERROR_OUT(-7401, exit);
  11927. }
  11928. cSz = (word32)ret;
  11929. if ((ret = wc_DeCompress(d, dSz, c, cSz)) != (int)dSz) {
  11930. ERROR_OUT(-7402, exit);
  11931. }
  11932. if (XMEMCMP(d, sample_text, dSz)) {
  11933. ERROR_OUT(-7403, exit);
  11934. }
  11935. ret = 0;
  11936. exit:
  11937. if (c) XFREE(c, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11938. if (d) XFREE(d, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11939. return ret;
  11940. }
  11941. #endif /* HAVE_LIBZ */
  11942. #ifdef HAVE_PKCS7
  11943. /* External Debugging/Testing Note:
  11944. *
  11945. * PKCS#7 test functions can output generated PKCS#7/CMS bundles for
  11946. * additional testing. To dump bundles to files DER encoded files, please
  11947. * define:
  11948. *
  11949. * #define PKCS7_OUTPUT_TEST_BUNDLES
  11950. */
  11951. /* Loads certs and keys for use with PKCS7 tests, from either files
  11952. * or buffers.
  11953. *
  11954. * rsaCert - output buffer for RSA cert
  11955. * rsaCertSz - IN/OUT size of output buffer, size of RSA cert
  11956. * rsaPrivKey - output buffer for RSA private key
  11957. * rsaPrivKeySz - IN/OUT size of output buffer, size of RSA key
  11958. * eccCert - output buffer for ECC cert
  11959. * eccCertSz - IN/OUT size of output buffer, size of ECC cert
  11960. * eccPrivKey - output buffer for ECC private key
  11961. * eccPrivKeySz - IN/OUT size of output buffer, size of ECC private key
  11962. *
  11963. * Returns 0 on success, negative on error
  11964. */
  11965. static int pkcs7_load_certs_keys(byte* rsaCert, word32* rsaCertSz,
  11966. byte* rsaPrivKey, word32* rsaPrivKeySz,
  11967. byte* eccCert, word32* eccCertSz,
  11968. byte* eccPrivKey, word32* eccPrivKeySz)
  11969. {
  11970. #ifndef NO_FILESYSTEM
  11971. FILE* certFile;
  11972. FILE* keyFile;
  11973. #endif
  11974. #ifndef NO_RSA
  11975. if (rsaCert == NULL || rsaCertSz == NULL ||
  11976. rsaPrivKey == NULL || rsaPrivKeySz == NULL)
  11977. return BAD_FUNC_ARG;
  11978. #endif
  11979. #ifdef HAVE_ECC
  11980. if (eccCert == NULL || eccCertSz == NULL ||
  11981. eccPrivKey == NULL || eccPrivKeySz == NULL)
  11982. return BAD_FUNC_ARG;
  11983. #endif
  11984. /* RSA */
  11985. #ifndef NO_RSA
  11986. #ifdef USE_CERT_BUFFERS_1024
  11987. if (*rsaCertSz < (word32)sizeof_client_cert_der_1024)
  11988. return -7410;
  11989. XMEMCPY(rsaCert, client_cert_der_1024, sizeof_client_cert_der_1024);
  11990. *rsaCertSz = sizeof_client_cert_der_1024;
  11991. #elif defined(USE_CERT_BUFFERS_2048)
  11992. if (*rsaCertSz < (word32)sizeof_client_cert_der_2048)
  11993. return -7411;
  11994. XMEMCPY(rsaCert, client_cert_der_2048, sizeof_client_cert_der_2048);
  11995. *rsaCertSz = sizeof_client_cert_der_2048;
  11996. #else
  11997. certFile = fopen(clientCert, "rb");
  11998. if (!certFile)
  11999. return -7412;
  12000. *rsaCertSz = (word32)fread(rsaCert, 1, *rsaCertSz, certFile);
  12001. fclose(certFile);
  12002. #endif
  12003. #ifdef USE_CERT_BUFFERS_1024
  12004. if (*rsaPrivKeySz < (word32)sizeof_client_key_der_1024)
  12005. return -7413;
  12006. XMEMCPY(rsaPrivKey, client_key_der_1024, sizeof_client_key_der_1024);
  12007. *rsaPrivKeySz = sizeof_client_key_der_1024;
  12008. #elif defined(USE_CERT_BUFFERS_2048)
  12009. if (*rsaPrivKeySz < (word32)sizeof_client_key_der_2048)
  12010. return -7414;
  12011. XMEMCPY(rsaPrivKey, client_key_der_2048, sizeof_client_key_der_2048);
  12012. *rsaPrivKeySz = sizeof_client_key_der_2048;
  12013. #else
  12014. keyFile = fopen(clientKey, "rb");
  12015. if (!keyFile)
  12016. return -7415;
  12017. *rsaPrivKeySz = (word32)fread(rsaPrivKey, 1, *rsaPrivKeySz, keyFile);
  12018. fclose(keyFile);
  12019. #endif /* USE_CERT_BUFFERS */
  12020. #endif /* NO_RSA */
  12021. /* ECC */
  12022. #ifdef HAVE_ECC
  12023. #ifdef USE_CERT_BUFFERS_256
  12024. if (*eccCertSz < (word32)sizeof_cliecc_cert_der_256)
  12025. return -7416;
  12026. XMEMCPY(eccCert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  12027. *eccCertSz = sizeof_cliecc_cert_der_256;
  12028. #else
  12029. certFile = fopen(eccClientCert, "rb");
  12030. if (!certFile)
  12031. return -7417;
  12032. *eccCertSz = (word32)fread(eccCert, 1, *eccCertSz, certFile);
  12033. fclose(certFile);
  12034. #endif /* USE_CERT_BUFFERS_256 */
  12035. #ifdef USE_CERT_BUFFERS_256
  12036. if (*eccPrivKeySz < (word32)sizeof_ecc_clikey_der_256)
  12037. return -7418;
  12038. XMEMCPY(eccPrivKey, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  12039. *eccPrivKeySz = sizeof_ecc_clikey_der_256;
  12040. #else
  12041. keyFile = fopen(eccClientKey, "rb");
  12042. if (!keyFile)
  12043. return -7419;
  12044. *eccPrivKeySz = (word32)fread(eccPrivKey, 1, *eccPrivKeySz, keyFile);
  12045. fclose(keyFile);
  12046. #endif /* USE_CERT_BUFFERS_256 */
  12047. #endif /* HAVE_ECC */
  12048. #ifdef NO_RSA
  12049. (void)rsaCert;
  12050. (void)rsaCertSz;
  12051. (void)rsaPrivKey;
  12052. (void)rsaPrivKeySz;
  12053. #endif
  12054. #ifndef HAVE_ECC
  12055. (void)eccCert;
  12056. (void)eccCertSz;
  12057. (void)eccPrivKey;
  12058. (void)eccPrivKeySz;
  12059. #endif
  12060. #ifndef NO_FILESYSTEM
  12061. (void)certFile;
  12062. (void)keyFile;
  12063. #endif
  12064. return 0;
  12065. }
  12066. typedef struct {
  12067. const byte* content;
  12068. word32 contentSz;
  12069. int contentOID;
  12070. int encryptOID;
  12071. int keyWrapOID;
  12072. int keyAgreeOID;
  12073. byte* cert;
  12074. size_t certSz;
  12075. byte* privateKey;
  12076. word32 privateKeySz;
  12077. byte* optionalUkm;
  12078. word32 optionalUkmSz;
  12079. const char* outFileName;
  12080. } pkcs7EnvelopedVector;
  12081. static int pkcs7enveloped_run_vectors(byte* rsaCert, word32 rsaCertSz,
  12082. byte* rsaPrivKey, word32 rsaPrivKeySz,
  12083. byte* eccCert, word32 eccCertSz,
  12084. byte* eccPrivKey, word32 eccPrivKeySz)
  12085. {
  12086. int ret, testSz, i;
  12087. int envelopedSz, decodedSz;
  12088. byte enveloped[2048];
  12089. byte decoded[2048];
  12090. PKCS7 pkcs7;
  12091. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  12092. FILE* pkcs7File;
  12093. #endif
  12094. const byte data[] = { /* Hello World */
  12095. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  12096. 0x72,0x6c,0x64
  12097. };
  12098. #if !defined(NO_AES) && defined(HAVE_ECC)
  12099. byte optionalUkm[] = {
  12100. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07
  12101. };
  12102. #endif /* NO_AES */
  12103. const pkcs7EnvelopedVector testVectors[] =
  12104. {
  12105. /* key transport key encryption technique */
  12106. #ifndef NO_RSA
  12107. {data, (word32)sizeof(data), DATA, DES3b, 0, 0, rsaCert, rsaCertSz,
  12108. rsaPrivKey, rsaPrivKeySz, NULL, 0, "pkcs7envelopedDataDES3.der"},
  12109. #ifndef NO_AES
  12110. {data, (word32)sizeof(data), DATA, AES128CBCb, 0, 0, rsaCert, rsaCertSz,
  12111. rsaPrivKey, rsaPrivKeySz, NULL, 0, "pkcs7envelopedDataAES128CBC.der"},
  12112. {data, (word32)sizeof(data), DATA, AES192CBCb, 0, 0, rsaCert, rsaCertSz,
  12113. rsaPrivKey, rsaPrivKeySz, NULL, 0, "pkcs7envelopedDataAES192CBC.der"},
  12114. {data, (word32)sizeof(data), DATA, AES256CBCb, 0, 0, rsaCert, rsaCertSz,
  12115. rsaPrivKey, rsaPrivKeySz, NULL, 0, "pkcs7envelopedDataAES256CBC.der"},
  12116. #endif /* NO_AES */
  12117. #endif
  12118. /* key agreement key encryption technique*/
  12119. #ifdef HAVE_ECC
  12120. #ifndef NO_AES
  12121. #ifndef NO_SHA
  12122. {data, (word32)sizeof(data), DATA, AES128CBCb, AES128_WRAP,
  12123. dhSinglePass_stdDH_sha1kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  12124. eccPrivKeySz, NULL, 0,
  12125. "pkcs7envelopedDataAES128CBC_ECDH_SHA1KDF.der"},
  12126. #endif
  12127. #ifndef NO_SHA256
  12128. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  12129. dhSinglePass_stdDH_sha256kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  12130. eccPrivKeySz, NULL, 0,
  12131. "pkcs7envelopedDataAES256CBC_ECDH_SHA256KDF.der"},
  12132. #endif /* NO_SHA256 */
  12133. #ifdef WOLFSSL_SHA512
  12134. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  12135. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  12136. eccPrivKeySz, NULL, 0,
  12137. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF.der"},
  12138. /* with optional user keying material (ukm) */
  12139. {data, (word32)sizeof(data), DATA, AES256CBCb, AES256_WRAP,
  12140. dhSinglePass_stdDH_sha512kdf_scheme, eccCert, eccCertSz, eccPrivKey,
  12141. eccPrivKeySz, optionalUkm, sizeof(optionalUkm),
  12142. "pkcs7envelopedDataAES256CBC_ECDH_SHA512KDF_ukm.der"},
  12143. #endif /* WOLFSSL_SHA512 */
  12144. #endif /* NO_AES */
  12145. #endif
  12146. };
  12147. testSz = sizeof(testVectors) / sizeof(pkcs7EnvelopedVector);
  12148. for (i = 0; i < testSz; i++) {
  12149. ret = wc_PKCS7_Init(&pkcs7, HEAP_HINT, devId);
  12150. if (ret != 0)
  12151. return -7419;
  12152. ret = wc_PKCS7_InitWithCert(&pkcs7, testVectors[i].cert,
  12153. (word32)testVectors[i].certSz);
  12154. if (ret != 0)
  12155. return -7420;
  12156. pkcs7.content = (byte*)testVectors[i].content;
  12157. pkcs7.contentSz = testVectors[i].contentSz;
  12158. pkcs7.contentOID = testVectors[i].contentOID;
  12159. pkcs7.encryptOID = testVectors[i].encryptOID;
  12160. pkcs7.keyWrapOID = testVectors[i].keyWrapOID;
  12161. pkcs7.keyAgreeOID = testVectors[i].keyAgreeOID;
  12162. pkcs7.privateKey = testVectors[i].privateKey;
  12163. pkcs7.privateKeySz = testVectors[i].privateKeySz;
  12164. pkcs7.ukm = testVectors[i].optionalUkm;
  12165. pkcs7.ukmSz = testVectors[i].optionalUkmSz;
  12166. /* encode envelopedData */
  12167. envelopedSz = wc_PKCS7_EncodeEnvelopedData(&pkcs7, enveloped,
  12168. sizeof(enveloped));
  12169. if (envelopedSz <= 0) {
  12170. printf("DEBUG: i = %d, envelopedSz = %d\n", i, envelopedSz);
  12171. return -7421;
  12172. }
  12173. /* decode envelopedData */
  12174. decodedSz = wc_PKCS7_DecodeEnvelopedData(&pkcs7, enveloped, envelopedSz,
  12175. decoded, sizeof(decoded));
  12176. if (decodedSz <= 0)
  12177. return -7422;
  12178. /* test decode result */
  12179. if (XMEMCMP(decoded, data, sizeof(data)) != 0)
  12180. return -7423;
  12181. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  12182. /* output pkcs7 envelopedData for external testing */
  12183. pkcs7File = fopen(testVectors[i].outFileName, "wb");
  12184. if (!pkcs7File)
  12185. return -7424;
  12186. ret = (int)fwrite(enveloped, 1, envelopedSz, pkcs7File);
  12187. fclose(pkcs7File);
  12188. if (ret != envelopedSz) {
  12189. return -7425;
  12190. }
  12191. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  12192. wc_PKCS7_Free(&pkcs7);
  12193. }
  12194. #ifndef HAVE_ECC
  12195. (void)eccCert;
  12196. (void)eccCertSz;
  12197. (void)eccPrivKey;
  12198. (void)eccPrivKeySz;
  12199. #endif
  12200. #ifdef NO_RSA
  12201. (void)rsaCert;
  12202. (void)rsaCertSz;
  12203. (void)rsaPrivKey;
  12204. (void)rsaPrivKeySz;
  12205. #endif
  12206. return 0;
  12207. }
  12208. int pkcs7enveloped_test(void)
  12209. {
  12210. int ret = 0;
  12211. byte* rsaCert = NULL;
  12212. byte* rsaPrivKey = NULL;
  12213. word32 rsaCertSz = 0;
  12214. word32 rsaPrivKeySz = 0;
  12215. byte* eccCert = NULL;
  12216. byte* eccPrivKey = NULL;
  12217. word32 eccCertSz = 0;
  12218. word32 eccPrivKeySz = 0;
  12219. #ifndef NO_RSA
  12220. /* read client RSA cert and key in DER format */
  12221. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12222. if (rsaCert == NULL)
  12223. return -7500;
  12224. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12225. if (rsaPrivKey == NULL) {
  12226. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12227. return -7501;
  12228. }
  12229. rsaCertSz = FOURK_BUF;
  12230. rsaPrivKeySz = FOURK_BUF;
  12231. #endif /* NO_RSA */
  12232. #ifdef HAVE_ECC
  12233. /* read client ECC cert and key in DER format */
  12234. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12235. if (eccCert == NULL) {
  12236. #ifndef NO_RSA
  12237. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12238. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12239. #endif
  12240. return -7504;
  12241. }
  12242. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12243. if (eccPrivKey == NULL) {
  12244. #ifndef NO_RSA
  12245. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12246. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12247. #endif
  12248. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12249. return -7505;
  12250. }
  12251. eccCertSz = FOURK_BUF;
  12252. eccPrivKeySz = FOURK_BUF;
  12253. #endif /* HAVE_ECC */
  12254. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  12255. &rsaPrivKeySz, eccCert, &eccCertSz,
  12256. eccPrivKey, &eccPrivKeySz);
  12257. if (ret < 0) {
  12258. #ifndef NO_RSA
  12259. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12260. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12261. #endif
  12262. #ifdef HAVE_ECC
  12263. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12264. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12265. #endif
  12266. return ret;
  12267. }
  12268. ret = pkcs7enveloped_run_vectors(rsaCert, (word32)rsaCertSz,
  12269. rsaPrivKey, (word32)rsaPrivKeySz,
  12270. eccCert, (word32)eccCertSz,
  12271. eccPrivKey, (word32)eccPrivKeySz);
  12272. #ifndef NO_RSA
  12273. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12274. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12275. #endif
  12276. #ifdef HAVE_ECC
  12277. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12278. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12279. #endif
  12280. return ret;
  12281. }
  12282. typedef struct {
  12283. const byte* content;
  12284. word32 contentSz;
  12285. int contentOID;
  12286. int encryptOID;
  12287. byte* encryptionKey;
  12288. word32 encryptionKeySz;
  12289. PKCS7Attrib* attribs;
  12290. word32 attribsSz;
  12291. const char* outFileName;
  12292. } pkcs7EncryptedVector;
  12293. int pkcs7encrypted_test(void)
  12294. {
  12295. int ret = 0;
  12296. int i, testSz;
  12297. int encryptedSz, decodedSz, attribIdx;
  12298. PKCS7 pkcs7;
  12299. byte encrypted[2048];
  12300. byte decoded[2048];
  12301. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  12302. FILE* pkcs7File;
  12303. #endif
  12304. PKCS7Attrib* expectedAttrib;
  12305. PKCS7DecodedAttrib* decodedAttrib;
  12306. const byte data[] = { /* Hello World */
  12307. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  12308. 0x72,0x6c,0x64
  12309. };
  12310. #ifndef NO_DES3
  12311. byte desKey[] = {
  12312. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  12313. };
  12314. byte des3Key[] = {
  12315. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  12316. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  12317. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  12318. };
  12319. #endif
  12320. #ifndef NO_AES
  12321. byte aes128Key[] = {
  12322. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12323. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  12324. };
  12325. byte aes192Key[] = {
  12326. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12327. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12328. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  12329. };
  12330. byte aes256Key[] = {
  12331. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12332. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12333. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  12334. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  12335. };
  12336. /* Attribute example from RFC 4134, Section 7.2
  12337. * OID = 1.2.5555
  12338. * OCTET STRING = 'This is a test General ASN Attribute, number 1.' */
  12339. static byte genAttrOid[] = { 0x06, 0x03, 0x2a, 0xab, 0x33 };
  12340. static byte genAttr[] = { 0x04, 47,
  12341. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  12342. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  12343. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  12344. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  12345. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  12346. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x31, 0x2e };
  12347. static byte genAttrOid2[] = { 0x06, 0x03, 0x2a, 0xab, 0x34 };
  12348. static byte genAttr2[] = { 0x04, 47,
  12349. 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20,
  12350. 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x47,
  12351. 0x65, 0x6e, 0x65, 0x72, 0x61, 0x6c, 0x20, 0x41,
  12352. 0x53, 0x4e, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69,
  12353. 0x62, 0x75, 0x74, 0x65, 0x2c, 0x20, 0x6e, 0x75,
  12354. 0x6d, 0x62, 0x65, 0x72, 0x20, 0x32, 0x2e };
  12355. PKCS7Attrib attribs[] =
  12356. {
  12357. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) }
  12358. };
  12359. PKCS7Attrib multiAttribs[] =
  12360. {
  12361. { genAttrOid, sizeof(genAttrOid), genAttr, sizeof(genAttr) },
  12362. { genAttrOid2, sizeof(genAttrOid2), genAttr2, sizeof(genAttr2) }
  12363. };
  12364. #endif /* NO_AES */
  12365. const pkcs7EncryptedVector testVectors[] =
  12366. {
  12367. #ifndef NO_DES3
  12368. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key),
  12369. NULL, 0, "pkcs7encryptedDataDES3.der"},
  12370. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey),
  12371. NULL, 0, "pkcs7encryptedDataDES.der"},
  12372. #endif /* NO_DES3 */
  12373. #ifndef NO_AES
  12374. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  12375. sizeof(aes128Key), NULL, 0, "pkcs7encryptedDataAES128CBC.der"},
  12376. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  12377. sizeof(aes192Key), NULL, 0, "pkcs7encryptedDataAES192CBC.der"},
  12378. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  12379. sizeof(aes256Key), NULL, 0, "pkcs7encryptedDataAES256CBC.der"},
  12380. /* test with optional unprotected attributes */
  12381. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  12382. sizeof(aes256Key), attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  12383. "pkcs7encryptedDataAES256CBC_attribs.der"},
  12384. /* test with multiple optional unprotected attributes */
  12385. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  12386. sizeof(aes256Key), multiAttribs,
  12387. (sizeof(multiAttribs)/sizeof(PKCS7Attrib)),
  12388. "pkcs7encryptedDataAES256CBC_multi_attribs.der"},
  12389. #endif /* NO_AES */
  12390. };
  12391. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  12392. for (i = 0; i < testSz; i++) {
  12393. pkcs7.content = (byte*)testVectors[i].content;
  12394. pkcs7.contentSz = testVectors[i].contentSz;
  12395. pkcs7.contentOID = testVectors[i].contentOID;
  12396. pkcs7.encryptOID = testVectors[i].encryptOID;
  12397. pkcs7.encryptionKey = testVectors[i].encryptionKey;
  12398. pkcs7.encryptionKeySz = testVectors[i].encryptionKeySz;
  12399. pkcs7.unprotectedAttribs = testVectors[i].attribs;
  12400. pkcs7.unprotectedAttribsSz = testVectors[i].attribsSz;
  12401. pkcs7.heap = HEAP_HINT;
  12402. /* encode encryptedData */
  12403. encryptedSz = wc_PKCS7_EncodeEncryptedData(&pkcs7, encrypted,
  12404. sizeof(encrypted));
  12405. if (encryptedSz <= 0)
  12406. return -7600;
  12407. /* decode encryptedData */
  12408. decodedSz = wc_PKCS7_DecodeEncryptedData(&pkcs7, encrypted, encryptedSz,
  12409. decoded, sizeof(decoded));
  12410. if (decodedSz <= 0)
  12411. return -7601;
  12412. /* test decode result */
  12413. if (XMEMCMP(decoded, data, sizeof(data)) != 0)
  12414. return -7602;
  12415. /* verify decoded unprotected attributes */
  12416. if (pkcs7.decodedAttrib != NULL) {
  12417. decodedAttrib = pkcs7.decodedAttrib;
  12418. attribIdx = 1;
  12419. while (decodedAttrib != NULL) {
  12420. /* expected attribute, stored list is reversed */
  12421. expectedAttrib = &(pkcs7.unprotectedAttribs
  12422. [pkcs7.unprotectedAttribsSz - attribIdx]);
  12423. /* verify oid */
  12424. if (XMEMCMP(decodedAttrib->oid, expectedAttrib->oid,
  12425. decodedAttrib->oidSz) != 0)
  12426. return -7603;
  12427. /* verify value */
  12428. if (XMEMCMP(decodedAttrib->value, expectedAttrib->value,
  12429. decodedAttrib->valueSz) != 0)
  12430. return -7604;
  12431. decodedAttrib = decodedAttrib->next;
  12432. attribIdx++;
  12433. }
  12434. }
  12435. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  12436. /* output pkcs7 envelopedData for external testing */
  12437. pkcs7File = fopen(testVectors[i].outFileName, "wb");
  12438. if (!pkcs7File)
  12439. return -7605;
  12440. ret = (int)fwrite(encrypted, encryptedSz, 1, pkcs7File);
  12441. fclose(pkcs7File);
  12442. if (ret > 0)
  12443. ret = 0;
  12444. #endif
  12445. wc_PKCS7_Free(&pkcs7);
  12446. }
  12447. return ret;
  12448. }
  12449. typedef struct {
  12450. const byte* content;
  12451. word32 contentSz;
  12452. int hashOID;
  12453. int encryptOID;
  12454. byte* privateKey;
  12455. word32 privateKeySz;
  12456. byte* cert;
  12457. size_t certSz;
  12458. PKCS7Attrib* signedAttribs;
  12459. word32 signedAttribsSz;
  12460. const char* outFileName;
  12461. } pkcs7SignedVector;
  12462. static int pkcs7signed_run_vectors(byte* rsaCert, word32 rsaCertSz,
  12463. byte* rsaPrivKey, word32 rsaPrivKeySz,
  12464. byte* eccCert, word32 eccCertSz,
  12465. byte* eccPrivKey, word32 eccPrivKeySz)
  12466. {
  12467. int ret, testSz, i;
  12468. int encodedSz;
  12469. byte* out;
  12470. word32 outSz;
  12471. WC_RNG rng;
  12472. PKCS7 pkcs7;
  12473. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  12474. FILE* file;
  12475. #endif
  12476. const byte data[] = { /* Hello World */
  12477. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  12478. 0x72,0x6c,0x64
  12479. };
  12480. static byte transIdOid[] =
  12481. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  12482. 0x09, 0x07 };
  12483. static byte messageTypeOid[] =
  12484. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  12485. 0x09, 0x02 };
  12486. static byte senderNonceOid[] =
  12487. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  12488. 0x09, 0x05 };
  12489. static byte transId[(WC_SHA_DIGEST_SIZE + 1) * 2 + 1];
  12490. static byte messageType[] = { 0x13, 2, '1', '9' };
  12491. static byte senderNonce[PKCS7_NONCE_SZ + 2];
  12492. PKCS7Attrib attribs[] =
  12493. {
  12494. { transIdOid, sizeof(transIdOid),
  12495. transId, sizeof(transId) - 1 }, /* take off the null */
  12496. { messageTypeOid, sizeof(messageTypeOid),
  12497. messageType, sizeof(messageType) },
  12498. { senderNonceOid, sizeof(senderNonceOid),
  12499. senderNonce, sizeof(senderNonce) }
  12500. };
  12501. const pkcs7SignedVector testVectors[] =
  12502. {
  12503. #ifndef NO_RSA
  12504. #ifndef NO_SHA
  12505. /* RSA with SHA */
  12506. {data, (word32)sizeof(data), SHAh, RSAk, rsaPrivKey, rsaPrivKeySz,
  12507. rsaCert, rsaCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  12508. "pkcs7signedData_RSA_SHA.der"},
  12509. /* RSA with SHA, no signed attributes */
  12510. {data, (word32)sizeof(data), SHAh, RSAk, rsaPrivKey, rsaPrivKeySz,
  12511. rsaCert, rsaCertSz, NULL, 0,
  12512. "pkcs7signedData_RSA_SHA_noattr.der"},
  12513. #endif
  12514. #ifdef WOLFSSL_SHA224
  12515. /* RSA with SHA224 */
  12516. {data, (word32)sizeof(data), SHA224h, RSAk, rsaPrivKey, rsaPrivKeySz,
  12517. rsaCert, rsaCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  12518. "pkcs7signedData_RSA_SHA224.der"},
  12519. #endif
  12520. #ifndef NO_SHA256
  12521. /* RSA with SHA256 */
  12522. {data, (word32)sizeof(data), SHA256h, RSAk, rsaPrivKey, rsaPrivKeySz,
  12523. rsaCert, rsaCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  12524. "pkcs7signedData_RSA_SHA256.der"},
  12525. #endif
  12526. #if defined(WOLFSSL_SHA384)
  12527. /* RSA with SHA384 */
  12528. {data, (word32)sizeof(data), SHA384h, RSAk, rsaPrivKey, rsaPrivKeySz,
  12529. rsaCert, rsaCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  12530. "pkcs7signedData_RSA_SHA384.der"},
  12531. #endif
  12532. #if defined(WOLFSSL_SHA512)
  12533. /* RSA with SHA512 */
  12534. {data, (word32)sizeof(data), SHA512h, RSAk, rsaPrivKey, rsaPrivKeySz,
  12535. rsaCert, rsaCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  12536. "pkcs7signedData_RSA_SHA512.der"},
  12537. #endif
  12538. #endif /* NO_RSA */
  12539. #ifdef HAVE_ECC
  12540. #ifndef NO_SHA
  12541. /* ECDSA with SHA */
  12542. {data, (word32)sizeof(data), SHAh, ECDSAk, eccPrivKey, eccPrivKeySz,
  12543. eccCert, eccCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  12544. "pkcs7signedData_ECDSA_SHA.der"},
  12545. /* ECDSA with SHA, no signed attributes */
  12546. {data, (word32)sizeof(data), SHAh, ECDSAk, eccPrivKey, eccPrivKeySz,
  12547. eccCert, eccCertSz, NULL, 0,
  12548. "pkcs7signedData_ECDSA_SHA_noattr.der"},
  12549. #endif
  12550. #ifdef WOLFSSL_SHA224
  12551. /* ECDSA with SHA224 */
  12552. {data, (word32)sizeof(data), SHA224h, ECDSAk, eccPrivKey, eccPrivKeySz,
  12553. eccCert, eccCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  12554. "pkcs7signedData_ECDSA_SHA224.der"},
  12555. #endif
  12556. #ifndef NO_SHA256
  12557. /* ECDSA with SHA256 */
  12558. {data, (word32)sizeof(data), SHA256h, ECDSAk, eccPrivKey, eccPrivKeySz,
  12559. eccCert, eccCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  12560. "pkcs7signedData_ECDSA_SHA256.der"},
  12561. #endif
  12562. #ifdef WOLFSSL_SHA384
  12563. /* ECDSA with SHA384 */
  12564. {data, (word32)sizeof(data), SHA384h, ECDSAk, eccPrivKey, eccPrivKeySz,
  12565. eccCert, eccCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  12566. "pkcs7signedData_ECDSA_SHA384.der"},
  12567. #endif
  12568. #ifdef WOLFSSL_SHA512
  12569. /* ECDSA with SHA512 */
  12570. {data, (word32)sizeof(data), SHA512h, ECDSAk, eccPrivKey, eccPrivKeySz,
  12571. eccCert, eccCertSz, attribs, (sizeof(attribs)/sizeof(PKCS7Attrib)),
  12572. "pkcs7signedData_ECDSA_SHA512.der"},
  12573. #endif
  12574. #endif /* HAVE_ECC */
  12575. };
  12576. testSz = sizeof(testVectors) / sizeof(pkcs7SignedVector);
  12577. outSz = FOURK_BUF;
  12578. out = (byte*)XMALLOC(outSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12579. if (out == NULL)
  12580. return -7700;
  12581. #ifndef HAVE_FIPS
  12582. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  12583. #else
  12584. ret = wc_InitRng(&rng);
  12585. #endif
  12586. if (ret != 0) {
  12587. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12588. return -7701;
  12589. }
  12590. for (i = 0; i < testSz; i++) {
  12591. pkcs7.heap = HEAP_HINT;
  12592. ret = wc_PKCS7_InitWithCert(&pkcs7, testVectors[i].cert,
  12593. (word32)testVectors[i].certSz);
  12594. if (ret != 0) {
  12595. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12596. return -7702;
  12597. }
  12598. pkcs7.rng = &rng;
  12599. pkcs7.content = (byte*)testVectors[i].content;
  12600. pkcs7.contentSz = testVectors[i].contentSz;
  12601. pkcs7.hashOID = testVectors[i].hashOID;
  12602. pkcs7.encryptOID = testVectors[i].encryptOID;
  12603. pkcs7.privateKey = testVectors[i].privateKey;
  12604. pkcs7.privateKeySz = testVectors[i].privateKeySz;
  12605. pkcs7.signedAttribs = testVectors[i].signedAttribs;
  12606. pkcs7.signedAttribsSz = testVectors[i].signedAttribsSz;
  12607. /* generate senderNonce */
  12608. {
  12609. senderNonce[0] = 0x04;
  12610. senderNonce[1] = PKCS7_NONCE_SZ;
  12611. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  12612. if (ret != 0) {
  12613. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12614. wc_PKCS7_Free(&pkcs7);
  12615. return -7703;
  12616. }
  12617. }
  12618. /* generate trans ID */
  12619. {
  12620. wc_Sha sha;
  12621. byte digest[WC_SHA_DIGEST_SIZE];
  12622. int j,k;
  12623. transId[0] = 0x13;
  12624. transId[1] = WC_SHA_DIGEST_SIZE * 2;
  12625. ret = wc_InitSha_ex(&sha, HEAP_HINT, devId);
  12626. if (ret != 0) {
  12627. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12628. wc_PKCS7_Free(&pkcs7);
  12629. return -7704;
  12630. }
  12631. wc_ShaUpdate(&sha, pkcs7.publicKey, pkcs7.publicKeySz);
  12632. wc_ShaFinal(&sha, digest);
  12633. wc_ShaFree(&sha);
  12634. for (j = 0, k = 2; j < WC_SHA_DIGEST_SIZE; j++, k += 2) {
  12635. XSNPRINTF((char*)&transId[k], 3, "%02x", digest[j]);
  12636. }
  12637. }
  12638. encodedSz = wc_PKCS7_EncodeSignedData(&pkcs7, out, outSz);
  12639. if (encodedSz < 0) {
  12640. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12641. wc_PKCS7_Free(&pkcs7);
  12642. return -7705;
  12643. }
  12644. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  12645. /* write PKCS#7 to output file for more testing */
  12646. file = fopen(testVectors[i].outFileName, "wb");
  12647. if (!file) {
  12648. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12649. wc_PKCS7_Free(&pkcs7);
  12650. return -7706;
  12651. }
  12652. ret = (int)fwrite(out, 1, encodedSz, file);
  12653. fclose(file);
  12654. if (ret != (int)encodedSz) {
  12655. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12656. wc_PKCS7_Free(&pkcs7);
  12657. return -7707;
  12658. }
  12659. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  12660. wc_PKCS7_Free(&pkcs7);
  12661. wc_PKCS7_InitWithCert(&pkcs7, NULL, 0);
  12662. ret = wc_PKCS7_VerifySignedData(&pkcs7, out, outSz);
  12663. if (ret < 0) {
  12664. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12665. wc_PKCS7_Free(&pkcs7);
  12666. return -7708;
  12667. }
  12668. if (pkcs7.singleCert == NULL || pkcs7.singleCertSz == 0) {
  12669. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12670. wc_PKCS7_Free(&pkcs7);
  12671. return -7709;
  12672. }
  12673. #ifdef PKCS7_OUTPUT_TEST_BUNDLES
  12674. file = fopen("./pkcs7cert.der", "wb");
  12675. if (!file) {
  12676. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12677. wc_PKCS7_Free(&pkcs7);
  12678. return -7710;
  12679. }
  12680. ret = (int)fwrite(pkcs7.singleCert, 1, pkcs7.singleCertSz, file);
  12681. fclose(file);
  12682. #endif /* PKCS7_OUTPUT_TEST_BUNDLES */
  12683. wc_PKCS7_Free(&pkcs7);
  12684. }
  12685. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12686. wc_FreeRng(&rng);
  12687. if (ret > 0)
  12688. return 0;
  12689. #ifndef HAVE_ECC
  12690. (void)eccCert;
  12691. (void)eccCertSz;
  12692. (void)eccPrivKey;
  12693. (void)eccPrivKeySz;
  12694. #endif
  12695. #ifdef NO_RSA
  12696. (void)rsaCert;
  12697. (void)rsaCertSz;
  12698. (void)rsaPrivKey;
  12699. (void)rsaPrivKeySz;
  12700. #endif
  12701. return ret;
  12702. }
  12703. int pkcs7signed_test(void)
  12704. {
  12705. int ret = 0;
  12706. byte* rsaCert = NULL;
  12707. byte* eccCert = NULL;
  12708. byte* rsaPrivKey = NULL;
  12709. byte* eccPrivKey = NULL;
  12710. word32 rsaCertSz = 0;
  12711. word32 eccCertSz = 0;
  12712. word32 rsaPrivKeySz = 0;
  12713. word32 eccPrivKeySz = 0;
  12714. #ifndef NO_RSA
  12715. /* read client RSA cert and key in DER format */
  12716. rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12717. if (rsaCert == NULL)
  12718. return -7720;
  12719. rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12720. if (rsaPrivKey == NULL) {
  12721. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12722. return -7721;
  12723. }
  12724. rsaCertSz = FOURK_BUF;
  12725. rsaPrivKeySz = FOURK_BUF;
  12726. #endif /* NO_RSA */
  12727. #ifdef HAVE_ECC
  12728. /* read client ECC cert and key in DER format */
  12729. eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12730. if (eccCert == NULL) {
  12731. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12732. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12733. return -7722;
  12734. }
  12735. eccPrivKey =(byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12736. if (eccPrivKey == NULL) {
  12737. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12738. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12739. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12740. return -7723;
  12741. }
  12742. eccCertSz = FOURK_BUF;
  12743. eccPrivKeySz = FOURK_BUF;
  12744. #endif /* HAVE_ECC */
  12745. ret = pkcs7_load_certs_keys(rsaCert, &rsaCertSz, rsaPrivKey,
  12746. &rsaPrivKeySz, eccCert, &eccCertSz,
  12747. eccPrivKey, &eccPrivKeySz);
  12748. if (ret < 0) {
  12749. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12750. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12751. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12752. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12753. return ret;
  12754. }
  12755. ret = pkcs7signed_run_vectors(rsaCert, (word32)rsaCertSz,
  12756. rsaPrivKey, (word32)rsaPrivKeySz,
  12757. eccCert, (word32)eccCertSz,
  12758. eccPrivKey, (word32)eccPrivKeySz);
  12759. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12760. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12761. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12762. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  12763. return ret;
  12764. }
  12765. #endif /* HAVE_PKCS7 */
  12766. #ifdef HAVE_VALGRIND
  12767. /* Need a static build to have access to symbols. */
  12768. /* Maximum number of bytes in a number to test. */
  12769. #define MP_MAX_TEST_BYTE_LEN 16
  12770. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  12771. static int randNum(mp_int* n, int len, WC_RNG* rng, void* heap)
  12772. {
  12773. byte d[MP_MAX_TEST_BYTE_LEN];
  12774. int ret;
  12775. (void)heap;
  12776. do {
  12777. ret = wc_RNG_GenerateBlock(rng, d, len);
  12778. if (ret != 0)
  12779. return ret;
  12780. ret = mp_read_unsigned_bin(n, d, len);
  12781. if (ret != 0)
  12782. return ret;
  12783. } while (mp_iszero(n));
  12784. return 0;
  12785. }
  12786. #endif
  12787. int mp_test(void)
  12788. {
  12789. WC_RNG rng;
  12790. int ret;
  12791. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  12792. int i, j, k;
  12793. mp_digit d;
  12794. #endif
  12795. mp_int a, b, r1, r2, p;
  12796. ret = mp_init_multi(&a, &b, &r1, &r2, NULL, NULL);
  12797. if (ret != 0)
  12798. return -7800;
  12799. mp_init_copy(&p, &a);
  12800. #ifndef HAVE_FIPS
  12801. ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  12802. #else
  12803. ret = wc_InitRng(&rng);
  12804. #endif
  12805. if (ret != 0)
  12806. goto done;
  12807. #if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
  12808. mp_set_int(&a, 0);
  12809. if (a.used != 0 || a.dp[0] != 0)
  12810. return -7801;
  12811. for (j = 1; j <= MP_MAX_TEST_BYTE_LEN; j++) {
  12812. for (i = 0; i < 4 * j; i++) {
  12813. /* New values to use. */
  12814. ret = randNum(&p, j, &rng, NULL);
  12815. if (ret != 0)
  12816. return -7802;
  12817. ret = randNum(&a, j, &rng, NULL);
  12818. if (ret != 0)
  12819. return -7803;
  12820. ret = randNum(&b, j, &rng, NULL);
  12821. if (ret != 0)
  12822. return -7804;
  12823. ret = wc_RNG_GenerateBlock(&rng, (byte*)&d, sizeof(d));
  12824. if (ret != 0)
  12825. return -7805;
  12826. d &= MP_MASK;
  12827. /* Ensure sqrmod produce same result as mulmod. */
  12828. ret = mp_sqrmod(&a, &p, &r1);
  12829. if (ret != 0)
  12830. return -7806;
  12831. ret = mp_mulmod(&a, &a, &p, &r2);
  12832. if (ret != 0)
  12833. return -7807;
  12834. if (mp_cmp(&r1, &r2) != 0)
  12835. return -7808;
  12836. /* Ensure add with mod produce same result as sub with mod. */
  12837. ret = mp_addmod(&a, &b, &p, &r1);
  12838. if (ret != 0)
  12839. return -7809;
  12840. b.sign ^= 1;
  12841. ret = mp_submod(&a, &b, &p, &r2);
  12842. if (ret != 0)
  12843. return -7810;
  12844. if (mp_cmp(&r1, &r2) != 0)
  12845. return -7811;
  12846. /* Ensure add digit produce same result as sub digit. */
  12847. ret = mp_add_d(&a, d, &r1);
  12848. if (ret != 0)
  12849. return -7812;
  12850. ret = mp_sub_d(&r1, d, &r2);
  12851. if (ret != 0)
  12852. return -7813;
  12853. if (mp_cmp(&a, &r2) != 0)
  12854. return -7814;
  12855. /* Invert - if p is even it will use the slow impl.
  12856. * - if p and a are even it will fail.
  12857. */
  12858. ret = mp_invmod(&a, &p, &r1);
  12859. if (ret != 0 && ret != MP_VAL)
  12860. return -7815;
  12861. ret = 0;
  12862. /* Shift up and down number all bits in a digit. */
  12863. for (k = 0; k < DIGIT_BIT; k++) {
  12864. mp_mul_2d(&a, k, &r1);
  12865. mp_div_2d(&r1, k, &r2, &p);
  12866. if (mp_cmp(&a, &r2) != 0)
  12867. return -7816;
  12868. if (!mp_iszero(&p))
  12869. return -7817;
  12870. mp_rshb(&r1, k);
  12871. if (mp_cmp(&a, &r1) != 0)
  12872. return -7818;
  12873. }
  12874. }
  12875. }
  12876. /* Check that setting a 32-bit digit works. */
  12877. d &= 0xffffffff;
  12878. mp_set_int(&a, d);
  12879. if (a.used != 1 || a.dp[0] != d)
  12880. return -7819;
  12881. /* Check setting a bit and testing a bit works. */
  12882. for (i = 0; i < MP_MAX_TEST_BYTE_LEN * 8; i++) {
  12883. mp_zero(&a);
  12884. mp_set_bit(&a, i);
  12885. if (!mp_is_bit_set(&a, i))
  12886. return -7820;
  12887. }
  12888. #endif
  12889. done:
  12890. mp_clear(&p);
  12891. mp_clear(&r2);
  12892. mp_clear(&r1);
  12893. mp_clear(&b);
  12894. mp_clear(&a);
  12895. wc_FreeRng(&rng);
  12896. return ret;
  12897. }
  12898. #endif
  12899. #ifdef HAVE_VALGRIND
  12900. /* Need a static build to have access to symbols. */
  12901. #ifndef WOLFSSL_SSL_H
  12902. /* APIs hiding in ssl.h */
  12903. extern int wolfSSL_Debugging_ON(void);
  12904. extern void wolfSSL_Debugging_OFF(void);
  12905. #endif
  12906. #ifdef DEBUG_WOLFSSL
  12907. static int log_cnt = 0;
  12908. static void my_Logging_cb(const int logLevel, const char *const logMessage)
  12909. {
  12910. (void)logLevel;
  12911. (void)logMessage;
  12912. log_cnt++;
  12913. }
  12914. #endif
  12915. int logging_test(void)
  12916. {
  12917. #ifdef DEBUG_WOLFSSL
  12918. const char* msg = "Testing, testing. 1, 2, 3, 4 ...";
  12919. byte a[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
  12920. byte b[256];
  12921. size_t i;
  12922. for (i = 0; i < sizeof(b); i++)
  12923. b[i] = i;
  12924. if (wolfSSL_Debugging_ON() != 0)
  12925. return -7900;
  12926. if (wolfSSL_SetLoggingCb(NULL) != BAD_FUNC_ARG)
  12927. return -7901;
  12928. WOLFSSL_MSG(msg);
  12929. WOLFSSL_BUFFER(a, sizeof(a));
  12930. WOLFSSL_BUFFER(b, sizeof(b));
  12931. WOLFSSL_BUFFER(NULL, 0);
  12932. wolfSSL_Debugging_OFF();
  12933. WOLFSSL_MSG(msg);
  12934. WOLFSSL_BUFFER(b, sizeof(b));
  12935. if (wolfSSL_SetLoggingCb(my_Logging_cb) != 0)
  12936. return -7902;
  12937. wolfSSL_Debugging_OFF();
  12938. WOLFSSL_MSG(msg);
  12939. WOLFSSL_BUFFER(b, sizeof(b));
  12940. if (log_cnt != 0)
  12941. return -7903;
  12942. if (wolfSSL_Debugging_ON() != 0)
  12943. return -7904;
  12944. WOLFSSL_MSG(msg);
  12945. WOLFSSL_BUFFER(b, sizeof(b));
  12946. /* One call for each line of output. */
  12947. if (log_cnt != 17)
  12948. return -7905;
  12949. #else
  12950. if (wolfSSL_Debugging_ON() != NOT_COMPILED_IN)
  12951. return -7906;
  12952. wolfSSL_Debugging_OFF();
  12953. if (wolfSSL_SetLoggingCb(NULL) != NOT_COMPILED_IN)
  12954. return -7907;
  12955. #endif
  12956. return 0;
  12957. }
  12958. #endif
  12959. int mutex_test(void)
  12960. {
  12961. #ifdef WOLFSSL_PTHREADS
  12962. wolfSSL_Mutex m;
  12963. #endif
  12964. wolfSSL_Mutex *mm = wc_InitAndAllocMutex();
  12965. if (mm == NULL)
  12966. return -8000;
  12967. wc_FreeMutex(mm);
  12968. XFREE(mm, NULL, DYNAMIC_TYPE_MUTEX);
  12969. #ifdef WOLFSSL_PTHREADS
  12970. if (wc_InitMutex(&m) != 0)
  12971. return -8001;
  12972. if (wc_LockMutex(&m) != 0)
  12973. return -8002;
  12974. if (wc_FreeMutex(&m) != BAD_MUTEX_E)
  12975. return -8003;
  12976. if (wc_UnLockMutex(&m) != 0)
  12977. return -8004;
  12978. if (wc_FreeMutex(&m) != 0)
  12979. return -8005;
  12980. if (wc_LockMutex(&m) != BAD_MUTEX_E)
  12981. return -8006;
  12982. if (wc_UnLockMutex(&m) != BAD_MUTEX_E)
  12983. return -8007;
  12984. #endif
  12985. return 0;
  12986. }
  12987. #if defined(USE_WOLFSSL_MEMORY) && !defined(FREERTOS)
  12988. static int malloc_cnt = 0;
  12989. static int realloc_cnt = 0;
  12990. static int free_cnt = 0;
  12991. static void *my_Malloc_cb(size_t size)
  12992. {
  12993. malloc_cnt++;
  12994. return malloc(size);
  12995. }
  12996. static void my_Free_cb(void *ptr)
  12997. {
  12998. free_cnt++;
  12999. free(ptr);
  13000. }
  13001. static void *my_Realloc_cb(void *ptr, size_t size)
  13002. {
  13003. realloc_cnt++;
  13004. return realloc(ptr, size);
  13005. }
  13006. int memcb_test(void)
  13007. {
  13008. int ret = 0;
  13009. byte* b = NULL;
  13010. wolfSSL_Malloc_cb mc;
  13011. wolfSSL_Free_cb fc;
  13012. wolfSSL_Realloc_cb rc;
  13013. /* Save existing memory callbacks */
  13014. if (wolfSSL_GetAllocators(&mc, &fc, &rc) != 0)
  13015. return -8100;
  13016. /* test realloc */
  13017. b = (byte*)XREALLOC(b, 1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  13018. if (b == NULL) {
  13019. ERROR_OUT(-8101, exit_memcb);
  13020. }
  13021. XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  13022. b = NULL;
  13023. /* Parameter Validation testing. */
  13024. if (wolfSSL_SetAllocators(NULL, (wolfSSL_Free_cb)&my_Free_cb,
  13025. (wolfSSL_Realloc_cb)&my_Realloc_cb) != BAD_FUNC_ARG) {
  13026. ERROR_OUT(-8102, exit_memcb);
  13027. }
  13028. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)&my_Malloc_cb, NULL,
  13029. (wolfSSL_Realloc_cb)&my_Realloc_cb) != BAD_FUNC_ARG) {
  13030. ERROR_OUT(-8103, exit_memcb);
  13031. }
  13032. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)&my_Malloc_cb,
  13033. (wolfSSL_Free_cb)&my_Free_cb, NULL) != BAD_FUNC_ARG) {
  13034. ERROR_OUT(-8104, exit_memcb);
  13035. }
  13036. /* Use API. */
  13037. if (wolfSSL_SetAllocators((wolfSSL_Malloc_cb)&my_Malloc_cb,
  13038. (wolfSSL_Free_cb)&my_Free_cb, (wolfSSL_Realloc_cb)my_Realloc_cb) != 0) {
  13039. ERROR_OUT(-8105, exit_memcb);
  13040. }
  13041. b = (byte*)XMALLOC(1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  13042. b = (byte*)XREALLOC(b, 1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  13043. XFREE(b, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  13044. #ifndef WOLFSSL_STATIC_MEMORY
  13045. if (malloc_cnt != 1 || free_cnt != 1 || realloc_cnt != 1)
  13046. #else
  13047. if (malloc_cnt != 0 || free_cnt != 0 || realloc_cnt != 0)
  13048. #endif
  13049. ret = -8106;
  13050. exit_memcb:
  13051. /* restore memory callbacks */
  13052. wolfSSL_SetAllocators(mc, fc, rc);
  13053. return ret;
  13054. }
  13055. #endif
  13056. #undef ERROR_OUT
  13057. #else
  13058. #ifndef NO_MAIN_DRIVER
  13059. int main() { return 0; }
  13060. #endif
  13061. #endif /* NO_CRYPT_TEST */