test.c 200 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042
  1. /* test.c
  2. *
  3. * Copyright (C) 2006-2015 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL. (formerly known as CyaSSL)
  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-1301, 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. #ifdef WOLFSSL_TEST_CERT
  30. #include <wolfssl/wolfcrypt/asn.h>
  31. #else
  32. #include <wolfssl/wolfcrypt/asn_public.h>
  33. #endif
  34. #include <wolfssl/wolfcrypt/md2.h>
  35. #include <wolfssl/wolfcrypt/md5.h>
  36. #include <wolfssl/wolfcrypt/md4.h>
  37. #include <wolfssl/wolfcrypt/sha.h>
  38. #include <wolfssl/wolfcrypt/sha256.h>
  39. #include <wolfssl/wolfcrypt/sha512.h>
  40. #include <wolfssl/wolfcrypt/arc4.h>
  41. #include <wolfssl/wolfcrypt/random.h>
  42. #include <wolfssl/wolfcrypt/coding.h>
  43. #include <wolfssl/wolfcrypt/rsa.h>
  44. #include <wolfssl/wolfcrypt/des3.h>
  45. #include <wolfssl/wolfcrypt/aes.h>
  46. #include <wolfssl/wolfcrypt/poly1305.h>
  47. #include <wolfssl/wolfcrypt/camellia.h>
  48. #include <wolfssl/wolfcrypt/hmac.h>
  49. #include <wolfssl/wolfcrypt/dh.h>
  50. #include <wolfssl/wolfcrypt/dsa.h>
  51. #include <wolfssl/wolfcrypt/srp.h>
  52. #include <wolfssl/wolfcrypt/hc128.h>
  53. #include <wolfssl/wolfcrypt/rabbit.h>
  54. #include <wolfssl/wolfcrypt/chacha.h>
  55. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  56. #include <wolfssl/wolfcrypt/pwdbased.h>
  57. #include <wolfssl/wolfcrypt/ripemd.h>
  58. #include <wolfssl/wolfcrypt/error-crypt.h>
  59. #ifdef HAVE_ECC
  60. #include <wolfssl/wolfcrypt/ecc.h>
  61. #endif
  62. #ifdef HAVE_CURVE25519
  63. #include <wolfssl/wolfcrypt/curve25519.h>
  64. #endif
  65. #ifdef HAVE_ED25519
  66. #include <wolfssl/wolfcrypt/ed25519.h>
  67. #endif
  68. #ifdef HAVE_BLAKE2
  69. #include <wolfssl/wolfcrypt/blake2.h>
  70. #endif
  71. #ifdef HAVE_LIBZ
  72. #include <wolfssl/wolfcrypt/compress.h>
  73. #endif
  74. #ifdef HAVE_PKCS7
  75. #include <wolfssl/wolfcrypt/pkcs7.h>
  76. #endif
  77. #ifdef HAVE_FIPS
  78. #include <wolfssl/wolfcrypt/fips_test.h>
  79. #endif
  80. #ifdef _MSC_VER
  81. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  82. #pragma warning(disable: 4996)
  83. #endif
  84. #ifdef OPENSSL_EXTRA
  85. #include <wolfssl/openssl/evp.h>
  86. #include <wolfssl/openssl/rand.h>
  87. #include <wolfssl/openssl/hmac.h>
  88. #include <wolfssl/openssl/des.h>
  89. #endif
  90. #if defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048) \
  91. || !defined(NO_DH)
  92. /* include test cert and key buffers for use with NO_FILESYSTEM */
  93. #include <wolfssl/certs_test.h>
  94. #endif
  95. #if defined(WOLFSSL_MDK_ARM)
  96. #include <stdio.h>
  97. #include <stdlib.h>
  98. extern FILE * wolfSSL_fopen(const char *fname, const char *mode) ;
  99. #define fopen wolfSSL_fopen
  100. #endif
  101. #ifdef HAVE_NTRU
  102. #include "libntruencrypt/ntru_crypto.h"
  103. #endif
  104. #ifdef HAVE_CAVIUM
  105. #include "cavium_sysdep.h"
  106. #include "cavium_common.h"
  107. #include "cavium_ioctl.h"
  108. #endif
  109. #ifdef FREESCALE_MQX
  110. #include <mqx.h>
  111. #include <stdlib.h>
  112. #if MQX_USE_IO_OLD
  113. #include <fio.h>
  114. #else
  115. #include <nio.h>
  116. #endif
  117. #else
  118. #include <stdio.h>
  119. #endif
  120. #ifdef THREADX
  121. /* since just testing, use THREADX log printf instead */
  122. int dc_log_printf(char*, ...);
  123. #undef printf
  124. #define printf dc_log_printf
  125. #endif
  126. #include "wolfcrypt/test/test.h"
  127. typedef struct testVector {
  128. const char* input;
  129. const char* output;
  130. size_t inLen;
  131. size_t outLen;
  132. } testVector;
  133. int md2_test(void);
  134. int md5_test(void);
  135. int md4_test(void);
  136. int sha_test(void);
  137. int sha256_test(void);
  138. int sha512_test(void);
  139. int sha384_test(void);
  140. int hmac_md5_test(void);
  141. int hmac_sha_test(void);
  142. int hmac_sha256_test(void);
  143. int hmac_sha384_test(void);
  144. int hmac_sha512_test(void);
  145. int hmac_blake2b_test(void);
  146. int hkdf_test(void);
  147. int arc4_test(void);
  148. int hc128_test(void);
  149. int rabbit_test(void);
  150. int chacha_test(void);
  151. int chacha20_poly1305_aead_test(void);
  152. int des_test(void);
  153. int des3_test(void);
  154. int aes_test(void);
  155. int poly1305_test(void);
  156. int aesgcm_test(void);
  157. int gmac_test(void);
  158. int aesccm_test(void);
  159. int camellia_test(void);
  160. int rsa_test(void);
  161. int dh_test(void);
  162. int dsa_test(void);
  163. int srp_test(void);
  164. int random_test(void);
  165. int pwdbased_test(void);
  166. int ripemd_test(void);
  167. int openssl_test(void); /* test mini api */
  168. int pbkdf1_test(void);
  169. int pkcs12_test(void);
  170. int pbkdf2_test(void);
  171. #ifdef HAVE_ECC
  172. int ecc_test(void);
  173. #ifdef HAVE_ECC_ENCRYPT
  174. int ecc_encrypt_test(void);
  175. #endif
  176. #endif
  177. #ifdef HAVE_CURVE25519
  178. int curve25519_test(void);
  179. #endif
  180. #ifdef HAVE_ED25519
  181. int ed25519_test(void);
  182. #endif
  183. #ifdef HAVE_BLAKE2
  184. int blake2b_test(void);
  185. #endif
  186. #ifdef HAVE_LIBZ
  187. int compress_test(void);
  188. #endif
  189. #ifdef HAVE_PKCS7
  190. int pkcs7enveloped_test(void);
  191. int pkcs7signed_test(void);
  192. #endif
  193. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT)
  194. int certext_test(void);
  195. #endif
  196. /* General big buffer size for many tests. */
  197. #define FOURK_BUF 4096
  198. static int err_sys(const char* msg, int es)
  199. {
  200. printf("%s error = %d\n", msg, es);
  201. return -1; /* error state */
  202. }
  203. /* func_args from test.h, so don't have to pull in other junk */
  204. typedef struct func_args {
  205. int argc;
  206. char** argv;
  207. int return_code;
  208. } func_args;
  209. #ifdef HAVE_FIPS
  210. static void myFipsCb(int ok, int err, const char* hash)
  211. {
  212. printf("in my Fips callback, ok = %d, err = %d\n", ok, err);
  213. printf("message = %s\n", wc_GetErrorString(err));
  214. printf("hash = %s\n", hash);
  215. if (err == IN_CORE_FIPS_E) {
  216. printf("In core integrity hash check failure, copy above hash\n");
  217. printf("into verifyCore[] in fips_test.c and rebuild\n");
  218. }
  219. }
  220. #endif /* HAVE_FIPS */
  221. int wolfcrypt_test(void* args)
  222. {
  223. int ret = 0;
  224. ((func_args*)args)->return_code = -1; /* error state */
  225. #ifdef HAVE_FIPS
  226. wolfCrypt_SetCb_fips(myFipsCb);
  227. #endif
  228. #if !defined(NO_BIG_INT)
  229. if (CheckCtcSettings() != 1)
  230. return err_sys("Build vs runtime math mismatch\n", -1234);
  231. #ifdef USE_FAST_MATH
  232. if (CheckFastMathSettings() != 1)
  233. return err_sys("Build vs runtime fastmath FP_MAX_BITS mismatch\n",
  234. -1235);
  235. #endif /* USE_FAST_MATH */
  236. #endif /* !NO_BIG_INT */
  237. if ( (ret = rsa_test()) != 0)
  238. return err_sys("RSA EXT test failed!\n", ret);
  239. else
  240. printf( "RSA EXT test passed!\n");
  241. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT)
  242. if ( (ret = certext_test()) != 0)
  243. return err_sys("CERT EXT test failed!\n", ret);
  244. else
  245. printf( "CERT EXT test passed!\n");
  246. #endif
  247. #ifndef NO_MD5
  248. if ( (ret = md5_test()) != 0)
  249. return err_sys("MD5 test failed!\n", ret);
  250. else
  251. printf( "MD5 test passed!\n");
  252. #endif
  253. #ifdef WOLFSSL_MD2
  254. if ( (ret = md2_test()) != 0)
  255. return err_sys("MD2 test failed!\n", ret);
  256. else
  257. printf( "MD2 test passed!\n");
  258. #endif
  259. #ifndef NO_MD4
  260. if ( (ret = md4_test()) != 0)
  261. return err_sys("MD4 test failed!\n", ret);
  262. else
  263. printf( "MD4 test passed!\n");
  264. #endif
  265. #ifndef NO_SHA
  266. if ( (ret = sha_test()) != 0)
  267. return err_sys("SHA test failed!\n", ret);
  268. else
  269. printf( "SHA test passed!\n");
  270. #endif
  271. #ifndef NO_SHA256
  272. if ( (ret = sha256_test()) != 0)
  273. return err_sys("SHA-256 test failed!\n", ret);
  274. else
  275. printf( "SHA-256 test passed!\n");
  276. #endif
  277. #ifdef WOLFSSL_SHA384
  278. if ( (ret = sha384_test()) != 0)
  279. return err_sys("SHA-384 test failed!\n", ret);
  280. else
  281. printf( "SHA-384 test passed!\n");
  282. #endif
  283. #ifdef WOLFSSL_SHA512
  284. if ( (ret = sha512_test()) != 0)
  285. return err_sys("SHA-512 test failed!\n", ret);
  286. else
  287. printf( "SHA-512 test passed!\n");
  288. #endif
  289. #ifdef WOLFSSL_RIPEMD
  290. if ( (ret = ripemd_test()) != 0)
  291. return err_sys("RIPEMD test failed!\n", ret);
  292. else
  293. printf( "RIPEMD test passed!\n");
  294. #endif
  295. #ifdef HAVE_BLAKE2
  296. if ( (ret = blake2b_test()) != 0)
  297. return err_sys("BLAKE2b test failed!\n", ret);
  298. else
  299. printf( "BLAKE2b test passed!\n");
  300. #endif
  301. #ifndef NO_HMAC
  302. #ifndef NO_MD5
  303. if ( (ret = hmac_md5_test()) != 0)
  304. return err_sys("HMAC-MD5 test failed!\n", ret);
  305. else
  306. printf( "HMAC-MD5 test passed!\n");
  307. #endif
  308. #ifndef NO_SHA
  309. if ( (ret = hmac_sha_test()) != 0)
  310. return err_sys("HMAC-SHA test failed!\n", ret);
  311. else
  312. printf( "HMAC-SHA test passed!\n");
  313. #endif
  314. #ifndef NO_SHA256
  315. if ( (ret = hmac_sha256_test()) != 0)
  316. return err_sys("HMAC-SHA256 test failed!\n", ret);
  317. else
  318. printf( "HMAC-SHA256 test passed!\n");
  319. #endif
  320. #ifdef WOLFSSL_SHA384
  321. if ( (ret = hmac_sha384_test()) != 0)
  322. return err_sys("HMAC-SHA384 test failed!\n", ret);
  323. else
  324. printf( "HMAC-SHA384 test passed!\n");
  325. #endif
  326. #ifdef WOLFSSL_SHA512
  327. if ( (ret = hmac_sha512_test()) != 0)
  328. return err_sys("HMAC-SHA512 test failed!\n", ret);
  329. else
  330. printf( "HMAC-SHA512 test passed!\n");
  331. #endif
  332. #ifdef HAVE_BLAKE2
  333. if ( (ret = hmac_blake2b_test()) != 0)
  334. return err_sys("HMAC-BLAKE2 test failed!\n", ret);
  335. else
  336. printf( "HMAC-BLAKE2 test passed!\n");
  337. #endif
  338. #ifdef HAVE_HKDF
  339. if ( (ret = hkdf_test()) != 0)
  340. return err_sys("HMAC-KDF test failed!\n", ret);
  341. else
  342. printf( "HMAC-KDF test passed!\n");
  343. #endif
  344. #endif
  345. #ifdef HAVE_AESGCM
  346. if ( (ret = gmac_test()) != 0)
  347. return err_sys("GMAC test passed!\n", ret);
  348. else
  349. printf( "GMAC test passed!\n");
  350. #endif
  351. #ifndef NO_RC4
  352. if ( (ret = arc4_test()) != 0)
  353. return err_sys("ARC4 test failed!\n", ret);
  354. else
  355. printf( "ARC4 test passed!\n");
  356. #endif
  357. #ifndef NO_HC128
  358. if ( (ret = hc128_test()) != 0)
  359. return err_sys("HC-128 test failed!\n", ret);
  360. else
  361. printf( "HC-128 test passed!\n");
  362. #endif
  363. #ifndef NO_RABBIT
  364. if ( (ret = rabbit_test()) != 0)
  365. return err_sys("Rabbit test failed!\n", ret);
  366. else
  367. printf( "Rabbit test passed!\n");
  368. #endif
  369. #ifdef HAVE_CHACHA
  370. if ( (ret = chacha_test()) != 0)
  371. return err_sys("Chacha test failed!\n", ret);
  372. else
  373. printf( "Chacha test passed!\n");
  374. #endif
  375. #ifdef HAVE_POLY1305
  376. if ( (ret = poly1305_test()) != 0)
  377. return err_sys("POLY1305 test failed!\n", ret);
  378. else
  379. printf( "POLY1305 test passed!\n");
  380. #endif
  381. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  382. if ( (ret = chacha20_poly1305_aead_test()) != 0)
  383. return err_sys("ChaCha20-Poly1305 AEAD test failed!\n", ret);
  384. else
  385. printf( "ChaCha20-Poly1305 AEAD test passed!\n");
  386. #endif
  387. #ifndef NO_DES3
  388. if ( (ret = des_test()) != 0)
  389. return err_sys("DES test failed!\n", ret);
  390. else
  391. printf( "DES test passed!\n");
  392. #endif
  393. #ifndef NO_DES3
  394. if ( (ret = des3_test()) != 0)
  395. return err_sys("DES3 test failed!\n", ret);
  396. else
  397. printf( "DES3 test passed!\n");
  398. #endif
  399. #ifndef NO_AES
  400. if ( (ret = aes_test()) != 0)
  401. return err_sys("AES test failed!\n", ret);
  402. else
  403. printf( "AES test passed!\n");
  404. #ifdef HAVE_AESGCM
  405. if ( (ret = aesgcm_test()) != 0)
  406. return err_sys("AES-GCM test failed!\n", ret);
  407. else
  408. printf( "AES-GCM test passed!\n");
  409. #endif
  410. #ifdef HAVE_AESCCM
  411. if ( (ret = aesccm_test()) != 0)
  412. return err_sys("AES-CCM test failed!\n", ret);
  413. else
  414. printf( "AES-CCM test passed!\n");
  415. #endif
  416. #endif
  417. #ifdef HAVE_CAMELLIA
  418. if ( (ret = camellia_test()) != 0)
  419. return err_sys("CAMELLIA test failed!\n", ret);
  420. else
  421. printf( "CAMELLIA test passed!\n");
  422. #endif
  423. if ( (ret = random_test()) != 0)
  424. return err_sys("RANDOM test failed!\n", ret);
  425. else
  426. printf( "RANDOM test passed!\n");
  427. #ifndef NO_RSA
  428. if ( (ret = rsa_test()) != 0)
  429. return err_sys("RSA test failed!\n", ret);
  430. else
  431. printf( "RSA test passed!\n");
  432. #endif
  433. #ifndef NO_DH
  434. if ( (ret = dh_test()) != 0)
  435. return err_sys("DH test failed!\n", ret);
  436. else
  437. printf( "DH test passed!\n");
  438. #endif
  439. #ifndef NO_DSA
  440. if ( (ret = dsa_test()) != 0)
  441. return err_sys("DSA test failed!\n", ret);
  442. else
  443. printf( "DSA test passed!\n");
  444. #endif
  445. #ifdef WOLFCRYPT_HAVE_SRP
  446. if ( (ret = srp_test()) != 0)
  447. return err_sys("SRP test failed!\n", ret);
  448. else
  449. printf( "SRP test passed!\n");
  450. #endif
  451. #ifndef NO_PWDBASED
  452. if ( (ret = pwdbased_test()) != 0)
  453. return err_sys("PWDBASED test failed!\n", ret);
  454. else
  455. printf( "PWDBASED test passed!\n");
  456. #endif
  457. #ifdef OPENSSL_EXTRA
  458. if ( (ret = openssl_test()) != 0)
  459. return err_sys("OPENSSL test failed!\n", ret);
  460. else
  461. printf( "OPENSSL test passed!\n");
  462. #endif
  463. #ifdef HAVE_ECC
  464. if ( (ret = ecc_test()) != 0)
  465. return err_sys("ECC test failed!\n", ret);
  466. else
  467. printf( "ECC test passed!\n");
  468. #ifdef HAVE_ECC_ENCRYPT
  469. if ( (ret = ecc_encrypt_test()) != 0)
  470. return err_sys("ECC Enc test failed!\n", ret);
  471. else
  472. printf( "ECC Enc test passed!\n");
  473. #endif
  474. #endif
  475. #ifdef HAVE_CURVE25519
  476. if ( (ret = curve25519_test()) != 0)
  477. return err_sys("CURVE25519 test failed!\n", ret);
  478. else
  479. printf( "CURVE25519 test passed!\n");
  480. #endif
  481. #ifdef HAVE_ED25519
  482. if ( (ret = ed25519_test()) != 0)
  483. return err_sys("ED25519 test failed!\n", ret);
  484. else
  485. printf( "ED25519 test passed!\n");
  486. #endif
  487. #ifdef HAVE_LIBZ
  488. if ( (ret = compress_test()) != 0)
  489. return err_sys("COMPRESS test failed!\n", ret);
  490. else
  491. printf( "COMPRESS test passed!\n");
  492. #endif
  493. #ifdef HAVE_PKCS7
  494. if ( (ret = pkcs7enveloped_test()) != 0)
  495. return err_sys("PKCS7enveloped test failed!\n", ret);
  496. else
  497. printf( "PKCS7enveloped test passed!\n");
  498. if ( (ret = pkcs7signed_test()) != 0)
  499. return err_sys("PKCS7signed test failed!\n", ret);
  500. else
  501. printf( "PKCS7signed test passed!\n");
  502. #endif
  503. ((func_args*)args)->return_code = ret;
  504. return ret;
  505. }
  506. #ifndef NO_MAIN_DRIVER
  507. #ifdef HAVE_CAVIUM
  508. static int OpenNitroxDevice(int dma_mode,int dev_id)
  509. {
  510. Csp1CoreAssignment core_assign;
  511. Uint32 device;
  512. if (CspInitialize(CAVIUM_DIRECT,CAVIUM_DEV_ID))
  513. return -1;
  514. if (Csp1GetDevType(&device))
  515. return -1;
  516. if (device != NPX_DEVICE) {
  517. if (ioctl(gpkpdev_hdlr[CAVIUM_DEV_ID], IOCTL_CSP1_GET_CORE_ASSIGNMENT,
  518. (Uint32 *)&core_assign)!= 0)
  519. return -1;
  520. }
  521. CspShutdown(CAVIUM_DEV_ID);
  522. return CspInitialize(dma_mode, dev_id);
  523. }
  524. #endif /* HAVE_CAVIUM */
  525. /* so overall tests can pull in test function */
  526. int main(int argc, char** argv)
  527. {
  528. func_args args;
  529. #ifdef HAVE_CAVIUM
  530. int ret = OpenNitroxDevice(CAVIUM_DIRECT, CAVIUM_DEV_ID);
  531. if (ret != 0) {
  532. err_sys("Cavium OpenNitroxDevice failed", -1236);
  533. return -1236;
  534. }
  535. #endif /* HAVE_CAVIUM */
  536. args.argc = argc;
  537. args.argv = argv;
  538. wolfcrypt_test(&args);
  539. #ifdef HAVE_CAVIUM
  540. CspShutdown(CAVIUM_DEV_ID);
  541. #endif
  542. return args.return_code;
  543. }
  544. #endif /* NO_MAIN_DRIVER */
  545. #ifdef WOLFSSL_MD2
  546. int md2_test()
  547. {
  548. Md2 md2;
  549. byte hash[MD2_DIGEST_SIZE];
  550. testVector a, b, c, d, e, f, g;
  551. testVector test_md2[7];
  552. int times = sizeof(test_md2) / sizeof(testVector), i;
  553. a.input = "";
  554. a.output = "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  555. "\x27\x73";
  556. a.inLen = strlen(a.input);
  557. a.outLen = MD2_DIGEST_SIZE;
  558. b.input = "a";
  559. b.output = "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  560. "\xb5\xd1";
  561. b.inLen = strlen(b.input);
  562. b.outLen = MD2_DIGEST_SIZE;
  563. c.input = "abc";
  564. c.output = "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  565. "\xd6\xbb";
  566. c.inLen = strlen(c.input);
  567. c.outLen = MD2_DIGEST_SIZE;
  568. d.input = "message digest";
  569. d.output = "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  570. "\x06\xb0";
  571. d.inLen = strlen(d.input);
  572. d.outLen = MD2_DIGEST_SIZE;
  573. e.input = "abcdefghijklmnopqrstuvwxyz";
  574. e.output = "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  575. "\x94\x0b";
  576. e.inLen = strlen(e.input);
  577. e.outLen = MD2_DIGEST_SIZE;
  578. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  579. "6789";
  580. f.output = "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  581. "\x38\xcd";
  582. f.inLen = strlen(f.input);
  583. f.outLen = MD2_DIGEST_SIZE;
  584. g.input = "1234567890123456789012345678901234567890123456789012345678"
  585. "9012345678901234567890";
  586. g.output = "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  587. "\xef\xd8";
  588. g.inLen = strlen(g.input);
  589. g.outLen = MD2_DIGEST_SIZE;
  590. test_md2[0] = a;
  591. test_md2[1] = b;
  592. test_md2[2] = c;
  593. test_md2[3] = d;
  594. test_md2[4] = e;
  595. test_md2[5] = f;
  596. test_md2[6] = g;
  597. wc_InitMd2(&md2);
  598. for (i = 0; i < times; ++i) {
  599. wc_Md2Update(&md2, (byte*)test_md2[i].input, (word32)test_md2[i].inLen);
  600. wc_Md2Final(&md2, hash);
  601. if (memcmp(hash, test_md2[i].output, MD2_DIGEST_SIZE) != 0)
  602. return -155 - i;
  603. }
  604. return 0;
  605. }
  606. #endif
  607. #ifndef NO_MD5
  608. int md5_test(void)
  609. {
  610. Md5 md5;
  611. byte hash[MD5_DIGEST_SIZE];
  612. testVector a, b, c, d, e;
  613. testVector test_md5[5];
  614. int times = sizeof(test_md5) / sizeof(testVector), i;
  615. a.input = "abc";
  616. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  617. "\x72";
  618. a.inLen = strlen(a.input);
  619. a.outLen = MD5_DIGEST_SIZE;
  620. b.input = "message digest";
  621. b.output = "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  622. "\xd0";
  623. b.inLen = strlen(b.input);
  624. b.outLen = MD5_DIGEST_SIZE;
  625. c.input = "abcdefghijklmnopqrstuvwxyz";
  626. c.output = "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  627. "\x3b";
  628. c.inLen = strlen(c.input);
  629. c.outLen = MD5_DIGEST_SIZE;
  630. d.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  631. "6789";
  632. d.output = "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  633. "\x9f";
  634. d.inLen = strlen(d.input);
  635. d.outLen = MD5_DIGEST_SIZE;
  636. e.input = "1234567890123456789012345678901234567890123456789012345678"
  637. "9012345678901234567890";
  638. e.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  639. "\x7a";
  640. e.inLen = strlen(e.input);
  641. e.outLen = MD5_DIGEST_SIZE;
  642. test_md5[0] = a;
  643. test_md5[1] = b;
  644. test_md5[2] = c;
  645. test_md5[3] = d;
  646. test_md5[4] = e;
  647. wc_InitMd5(&md5);
  648. for (i = 0; i < times; ++i) {
  649. wc_Md5Update(&md5, (byte*)test_md5[i].input, (word32)test_md5[i].inLen);
  650. wc_Md5Final(&md5, hash);
  651. if (memcmp(hash, test_md5[i].output, MD5_DIGEST_SIZE) != 0)
  652. return -5 - i;
  653. }
  654. return 0;
  655. }
  656. #endif /* NO_MD5 */
  657. #ifndef NO_MD4
  658. int md4_test(void)
  659. {
  660. Md4 md4;
  661. byte hash[MD4_DIGEST_SIZE];
  662. testVector a, b, c, d, e, f, g;
  663. testVector test_md4[7];
  664. int times = sizeof(test_md4) / sizeof(testVector), i;
  665. a.input = "";
  666. a.output = "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  667. "\xc0";
  668. a.inLen = strlen(a.input);
  669. a.outLen = MD4_DIGEST_SIZE;
  670. b.input = "a";
  671. b.output = "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  672. "\x24";
  673. b.inLen = strlen(b.input);
  674. b.outLen = MD4_DIGEST_SIZE;
  675. c.input = "abc";
  676. c.output = "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  677. "\x9d";
  678. c.inLen = strlen(c.input);
  679. c.outLen = MD4_DIGEST_SIZE;
  680. d.input = "message digest";
  681. d.output = "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  682. "\x4b";
  683. d.inLen = strlen(d.input);
  684. d.outLen = MD4_DIGEST_SIZE;
  685. e.input = "abcdefghijklmnopqrstuvwxyz";
  686. e.output = "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  687. "\xa9";
  688. e.inLen = strlen(e.input);
  689. e.outLen = MD4_DIGEST_SIZE;
  690. f.input = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  691. "6789";
  692. f.output = "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  693. "\xe4";
  694. f.inLen = strlen(f.input);
  695. f.outLen = MD4_DIGEST_SIZE;
  696. g.input = "1234567890123456789012345678901234567890123456789012345678"
  697. "9012345678901234567890";
  698. g.output = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  699. "\x36";
  700. g.inLen = strlen(g.input);
  701. g.outLen = MD4_DIGEST_SIZE;
  702. test_md4[0] = a;
  703. test_md4[1] = b;
  704. test_md4[2] = c;
  705. test_md4[3] = d;
  706. test_md4[4] = e;
  707. test_md4[5] = f;
  708. test_md4[6] = g;
  709. wc_InitMd4(&md4);
  710. for (i = 0; i < times; ++i) {
  711. wc_Md4Update(&md4, (byte*)test_md4[i].input, (word32)test_md4[i].inLen);
  712. wc_Md4Final(&md4, hash);
  713. if (memcmp(hash, test_md4[i].output, MD4_DIGEST_SIZE) != 0)
  714. return -205 - i;
  715. }
  716. return 0;
  717. }
  718. #endif /* NO_MD4 */
  719. #ifndef NO_SHA
  720. int sha_test(void)
  721. {
  722. Sha sha;
  723. byte hash[SHA_DIGEST_SIZE];
  724. testVector a, b, c, d;
  725. testVector test_sha[4];
  726. int ret;
  727. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  728. a.input = "abc";
  729. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  730. "\x6C\x9C\xD0\xD8\x9D";
  731. a.inLen = strlen(a.input);
  732. a.outLen = SHA_DIGEST_SIZE;
  733. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  734. b.output = "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  735. "\xE5\xE5\x46\x70\xF1";
  736. b.inLen = strlen(b.input);
  737. b.outLen = SHA_DIGEST_SIZE;
  738. c.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  739. "aaaaaa";
  740. c.output = "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  741. "\x2A\x25\xEC\x64\x4D";
  742. c.inLen = strlen(c.input);
  743. c.outLen = SHA_DIGEST_SIZE;
  744. d.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  745. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  746. "aaaaaaaaaa";
  747. d.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  748. "\x53\x99\x5E\x26\xA0";
  749. d.inLen = strlen(d.input);
  750. d.outLen = SHA_DIGEST_SIZE;
  751. test_sha[0] = a;
  752. test_sha[1] = b;
  753. test_sha[2] = c;
  754. test_sha[3] = d;
  755. ret = wc_InitSha(&sha);
  756. if (ret != 0)
  757. return -4001;
  758. for (i = 0; i < times; ++i) {
  759. wc_ShaUpdate(&sha, (byte*)test_sha[i].input, (word32)test_sha[i].inLen);
  760. wc_ShaFinal(&sha, hash);
  761. if (memcmp(hash, test_sha[i].output, SHA_DIGEST_SIZE) != 0)
  762. return -10 - i;
  763. }
  764. return 0;
  765. }
  766. #endif /* NO_SHA */
  767. #ifdef WOLFSSL_RIPEMD
  768. int ripemd_test(void)
  769. {
  770. RipeMd ripemd;
  771. byte hash[RIPEMD_DIGEST_SIZE];
  772. testVector a, b, c, d;
  773. testVector test_ripemd[4];
  774. int times = sizeof(test_ripemd) / sizeof(struct testVector), i;
  775. a.input = "abc";
  776. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  777. "\xb0\x87\xf1\x5a\x0b\xfc";
  778. a.inLen = strlen(a.input);
  779. a.outLen = RIPEMD_DIGEST_SIZE;
  780. b.input = "message digest";
  781. b.output = "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  782. "\x5f\xfa\x21\x59\x5f\x36";
  783. b.inLen = strlen(b.input);
  784. b.outLen = RIPEMD_DIGEST_SIZE;
  785. c.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  786. c.output = "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  787. "\xf4\x9a\xda\x62\xeb\x2b";
  788. c.inLen = strlen(c.input);
  789. c.outLen = RIPEMD_DIGEST_SIZE;
  790. d.input = "12345678901234567890123456789012345678901234567890123456"
  791. "789012345678901234567890";
  792. d.output = "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  793. "\x82\xbf\x63\x32\x6b\xfb";
  794. d.inLen = strlen(d.input);
  795. d.outLen = RIPEMD_DIGEST_SIZE;
  796. test_ripemd[0] = a;
  797. test_ripemd[1] = b;
  798. test_ripemd[2] = c;
  799. test_ripemd[3] = d;
  800. wc_InitRipeMd(&ripemd);
  801. for (i = 0; i < times; ++i) {
  802. wc_RipeMdUpdate(&ripemd, (byte*)test_ripemd[i].input,
  803. (word32)test_ripemd[i].inLen);
  804. wc_RipeMdFinal(&ripemd, hash);
  805. if (memcmp(hash, test_ripemd[i].output, RIPEMD_DIGEST_SIZE) != 0)
  806. return -10 - i;
  807. }
  808. return 0;
  809. }
  810. #endif /* WOLFSSL_RIPEMD */
  811. #ifdef HAVE_BLAKE2
  812. #define BLAKE2_TESTS 3
  813. static const byte blake2b_vec[BLAKE2_TESTS][BLAKE2B_OUTBYTES] =
  814. {
  815. {
  816. 0x78, 0x6A, 0x02, 0xF7, 0x42, 0x01, 0x59, 0x03,
  817. 0xC6, 0xC6, 0xFD, 0x85, 0x25, 0x52, 0xD2, 0x72,
  818. 0x91, 0x2F, 0x47, 0x40, 0xE1, 0x58, 0x47, 0x61,
  819. 0x8A, 0x86, 0xE2, 0x17, 0xF7, 0x1F, 0x54, 0x19,
  820. 0xD2, 0x5E, 0x10, 0x31, 0xAF, 0xEE, 0x58, 0x53,
  821. 0x13, 0x89, 0x64, 0x44, 0x93, 0x4E, 0xB0, 0x4B,
  822. 0x90, 0x3A, 0x68, 0x5B, 0x14, 0x48, 0xB7, 0x55,
  823. 0xD5, 0x6F, 0x70, 0x1A, 0xFE, 0x9B, 0xE2, 0xCE
  824. },
  825. {
  826. 0x2F, 0xA3, 0xF6, 0x86, 0xDF, 0x87, 0x69, 0x95,
  827. 0x16, 0x7E, 0x7C, 0x2E, 0x5D, 0x74, 0xC4, 0xC7,
  828. 0xB6, 0xE4, 0x8F, 0x80, 0x68, 0xFE, 0x0E, 0x44,
  829. 0x20, 0x83, 0x44, 0xD4, 0x80, 0xF7, 0x90, 0x4C,
  830. 0x36, 0x96, 0x3E, 0x44, 0x11, 0x5F, 0xE3, 0xEB,
  831. 0x2A, 0x3A, 0xC8, 0x69, 0x4C, 0x28, 0xBC, 0xB4,
  832. 0xF5, 0xA0, 0xF3, 0x27, 0x6F, 0x2E, 0x79, 0x48,
  833. 0x7D, 0x82, 0x19, 0x05, 0x7A, 0x50, 0x6E, 0x4B
  834. },
  835. {
  836. 0x1C, 0x08, 0x79, 0x8D, 0xC6, 0x41, 0xAB, 0xA9,
  837. 0xDE, 0xE4, 0x35, 0xE2, 0x25, 0x19, 0xA4, 0x72,
  838. 0x9A, 0x09, 0xB2, 0xBF, 0xE0, 0xFF, 0x00, 0xEF,
  839. 0x2D, 0xCD, 0x8E, 0xD6, 0xF8, 0xA0, 0x7D, 0x15,
  840. 0xEA, 0xF4, 0xAE, 0xE5, 0x2B, 0xBF, 0x18, 0xAB,
  841. 0x56, 0x08, 0xA6, 0x19, 0x0F, 0x70, 0xB9, 0x04,
  842. 0x86, 0xC8, 0xA7, 0xD4, 0x87, 0x37, 0x10, 0xB1,
  843. 0x11, 0x5D, 0x3D, 0xEB, 0xBB, 0x43, 0x27, 0xB5
  844. }
  845. };
  846. int blake2b_test(void)
  847. {
  848. Blake2b b2b;
  849. byte digest[64];
  850. byte input[64];
  851. int i, ret;
  852. for (i = 0; i < (int)sizeof(input); i++)
  853. input[i] = (byte)i;
  854. for (i = 0; i < BLAKE2_TESTS; i++) {
  855. ret = wc_InitBlake2b(&b2b, 64);
  856. if (ret != 0)
  857. return -4002;
  858. ret = wc_Blake2bUpdate(&b2b, input, i);
  859. if (ret != 0)
  860. return -4003;
  861. ret = wc_Blake2bFinal(&b2b, digest, 64);
  862. if (ret != 0)
  863. return -4004;
  864. if (memcmp(digest, blake2b_vec[i], 64) != 0) {
  865. return -300 - i;
  866. }
  867. }
  868. return 0;
  869. }
  870. #endif /* HAVE_BLAKE2 */
  871. #ifndef NO_SHA256
  872. int sha256_test(void)
  873. {
  874. Sha256 sha;
  875. byte hash[SHA256_DIGEST_SIZE];
  876. testVector a, b;
  877. testVector test_sha[2];
  878. int ret;
  879. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  880. a.input = "abc";
  881. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  882. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  883. "\x15\xAD";
  884. a.inLen = strlen(a.input);
  885. a.outLen = SHA256_DIGEST_SIZE;
  886. b.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  887. b.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  888. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  889. "\x06\xC1";
  890. b.inLen = strlen(b.input);
  891. b.outLen = SHA256_DIGEST_SIZE;
  892. test_sha[0] = a;
  893. test_sha[1] = b;
  894. ret = wc_InitSha256(&sha);
  895. if (ret != 0)
  896. return -4005;
  897. for (i = 0; i < times; ++i) {
  898. ret = wc_Sha256Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  899. if (ret != 0)
  900. return -4006;
  901. ret = wc_Sha256Final(&sha, hash);
  902. if (ret != 0)
  903. return -4007;
  904. if (memcmp(hash, test_sha[i].output, SHA256_DIGEST_SIZE) != 0)
  905. return -10 - i;
  906. }
  907. return 0;
  908. }
  909. #endif
  910. #ifdef WOLFSSL_SHA512
  911. int sha512_test(void)
  912. {
  913. Sha512 sha;
  914. byte hash[SHA512_DIGEST_SIZE];
  915. int ret;
  916. testVector a, b;
  917. testVector test_sha[2];
  918. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  919. a.input = "abc";
  920. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  921. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  922. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  923. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  924. "\xa5\x4c\xa4\x9f";
  925. a.inLen = strlen(a.input);
  926. a.outLen = SHA512_DIGEST_SIZE;
  927. b.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  928. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  929. b.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  930. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  931. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  932. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  933. "\x87\x4b\xe9\x09";
  934. b.inLen = strlen(b.input);
  935. b.outLen = SHA512_DIGEST_SIZE;
  936. test_sha[0] = a;
  937. test_sha[1] = b;
  938. ret = wc_InitSha512(&sha);
  939. if (ret != 0)
  940. return -4009;
  941. for (i = 0; i < times; ++i) {
  942. ret = wc_Sha512Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  943. if (ret != 0)
  944. return -4010;
  945. ret = wc_Sha512Final(&sha, hash);
  946. if (ret != 0)
  947. return -4011;
  948. if (memcmp(hash, test_sha[i].output, SHA512_DIGEST_SIZE) != 0)
  949. return -10 - i;
  950. }
  951. return 0;
  952. }
  953. #endif
  954. #ifdef WOLFSSL_SHA384
  955. int sha384_test(void)
  956. {
  957. Sha384 sha;
  958. byte hash[SHA384_DIGEST_SIZE];
  959. int ret;
  960. testVector a, b;
  961. testVector test_sha[2];
  962. int times = sizeof(test_sha) / sizeof(struct testVector), i;
  963. a.input = "abc";
  964. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  965. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  966. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  967. "\xc8\x25\xa7";
  968. a.inLen = strlen(a.input);
  969. a.outLen = SHA384_DIGEST_SIZE;
  970. b.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  971. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  972. b.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  973. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  974. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  975. "\x74\x60\x39";
  976. b.inLen = strlen(b.input);
  977. b.outLen = SHA384_DIGEST_SIZE;
  978. test_sha[0] = a;
  979. test_sha[1] = b;
  980. ret = wc_InitSha384(&sha);
  981. if (ret != 0)
  982. return -4012;
  983. for (i = 0; i < times; ++i) {
  984. ret = wc_Sha384Update(&sha, (byte*)test_sha[i].input,(word32)test_sha[i].inLen);
  985. if (ret != 0)
  986. return -4013;
  987. ret = wc_Sha384Final(&sha, hash);
  988. if (ret != 0)
  989. return -4014;
  990. if (memcmp(hash, test_sha[i].output, SHA384_DIGEST_SIZE) != 0)
  991. return -10 - i;
  992. }
  993. return 0;
  994. }
  995. #endif /* WOLFSSL_SHA384 */
  996. #if !defined(NO_HMAC) && !defined(NO_MD5)
  997. int hmac_md5_test(void)
  998. {
  999. Hmac hmac;
  1000. byte hash[MD5_DIGEST_SIZE];
  1001. const char* keys[]=
  1002. {
  1003. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  1004. "Jefe",
  1005. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  1006. };
  1007. testVector a, b, c;
  1008. testVector test_hmac[3];
  1009. int ret;
  1010. int times = sizeof(test_hmac) / sizeof(testVector), i;
  1011. a.input = "Hi There";
  1012. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  1013. "\x9d";
  1014. a.inLen = strlen(a.input);
  1015. a.outLen = MD5_DIGEST_SIZE;
  1016. b.input = "what do ya want for nothing?";
  1017. b.output = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  1018. "\x38";
  1019. b.inLen = strlen(b.input);
  1020. b.outLen = MD5_DIGEST_SIZE;
  1021. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1022. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1023. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1024. "\xDD\xDD\xDD\xDD\xDD\xDD";
  1025. c.output = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  1026. "\xf6";
  1027. c.inLen = strlen(c.input);
  1028. c.outLen = MD5_DIGEST_SIZE;
  1029. test_hmac[0] = a;
  1030. test_hmac[1] = b;
  1031. test_hmac[2] = c;
  1032. for (i = 0; i < times; ++i) {
  1033. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  1034. if (i == 1)
  1035. continue; /* cavium can't handle short keys, fips not allowed */
  1036. #endif
  1037. #ifdef HAVE_CAVIUM
  1038. if (wc_HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0)
  1039. return -20009;
  1040. #endif
  1041. ret = wc_HmacSetKey(&hmac, MD5, (byte*)keys[i], (word32)strlen(keys[i]));
  1042. if (ret != 0)
  1043. return -4015;
  1044. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  1045. (word32)test_hmac[i].inLen);
  1046. if (ret != 0)
  1047. return -4016;
  1048. ret = wc_HmacFinal(&hmac, hash);
  1049. if (ret != 0)
  1050. return -4017;
  1051. if (memcmp(hash, test_hmac[i].output, MD5_DIGEST_SIZE) != 0)
  1052. return -20 - i;
  1053. #ifdef HAVE_CAVIUM
  1054. wc_HmacFreeCavium(&hmac);
  1055. #endif
  1056. }
  1057. return 0;
  1058. }
  1059. #endif /* NO_HMAC && NO_MD5 */
  1060. #if !defined(NO_HMAC) && !defined(NO_SHA)
  1061. int hmac_sha_test(void)
  1062. {
  1063. Hmac hmac;
  1064. byte hash[SHA_DIGEST_SIZE];
  1065. const char* keys[]=
  1066. {
  1067. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  1068. "\x0b\x0b\x0b",
  1069. "Jefe",
  1070. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  1071. "\xAA\xAA\xAA"
  1072. };
  1073. testVector a, b, c;
  1074. testVector test_hmac[3];
  1075. int ret;
  1076. int times = sizeof(test_hmac) / sizeof(testVector), i;
  1077. a.input = "Hi There";
  1078. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  1079. "\x8e\xf1\x46\xbe\x00";
  1080. a.inLen = strlen(a.input);
  1081. a.outLen = SHA_DIGEST_SIZE;
  1082. b.input = "what do ya want for nothing?";
  1083. b.output = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf"
  1084. "\x9c\x25\x9a\x7c\x79";
  1085. b.inLen = strlen(b.input);
  1086. b.outLen = SHA_DIGEST_SIZE;
  1087. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1088. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1089. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1090. "\xDD\xDD\xDD\xDD\xDD\xDD";
  1091. c.output = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b"
  1092. "\x4f\x63\xf1\x75\xd3";
  1093. c.inLen = strlen(c.input);
  1094. c.outLen = SHA_DIGEST_SIZE;
  1095. test_hmac[0] = a;
  1096. test_hmac[1] = b;
  1097. test_hmac[2] = c;
  1098. for (i = 0; i < times; ++i) {
  1099. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  1100. if (i == 1)
  1101. continue; /* cavium can't handle short keys, fips not allowed */
  1102. #endif
  1103. #ifdef HAVE_CAVIUM
  1104. if (wc_HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0)
  1105. return -20010;
  1106. #endif
  1107. ret = wc_HmacSetKey(&hmac, SHA, (byte*)keys[i], (word32)strlen(keys[i]));
  1108. if (ret != 0)
  1109. return -4018;
  1110. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  1111. (word32)test_hmac[i].inLen);
  1112. if (ret != 0)
  1113. return -4019;
  1114. ret = wc_HmacFinal(&hmac, hash);
  1115. if (ret != 0)
  1116. return -4020;
  1117. if (memcmp(hash, test_hmac[i].output, SHA_DIGEST_SIZE) != 0)
  1118. return -20 - i;
  1119. #ifdef HAVE_CAVIUM
  1120. wc_HmacFreeCavium(&hmac);
  1121. #endif
  1122. }
  1123. return 0;
  1124. }
  1125. #endif
  1126. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  1127. int hmac_sha256_test(void)
  1128. {
  1129. Hmac hmac;
  1130. byte hash[SHA256_DIGEST_SIZE];
  1131. const char* keys[]=
  1132. {
  1133. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  1134. "\x0b\x0b\x0b",
  1135. "Jefe",
  1136. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  1137. "\xAA\xAA\xAA"
  1138. };
  1139. testVector a, b, c;
  1140. testVector test_hmac[3];
  1141. int ret;
  1142. int times = sizeof(test_hmac) / sizeof(testVector), i;
  1143. a.input = "Hi There";
  1144. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  1145. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  1146. "\xcf\xf7";
  1147. a.inLen = strlen(a.input);
  1148. a.outLen = SHA256_DIGEST_SIZE;
  1149. b.input = "what do ya want for nothing?";
  1150. b.output = "\x5b\xdc\xc1\x46\xbf\x60\x75\x4e\x6a\x04\x24\x26\x08\x95\x75"
  1151. "\xc7\x5a\x00\x3f\x08\x9d\x27\x39\x83\x9d\xec\x58\xb9\x64\xec"
  1152. "\x38\x43";
  1153. b.inLen = strlen(b.input);
  1154. b.outLen = SHA256_DIGEST_SIZE;
  1155. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1156. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1157. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1158. "\xDD\xDD\xDD\xDD\xDD\xDD";
  1159. c.output = "\x77\x3e\xa9\x1e\x36\x80\x0e\x46\x85\x4d\xb8\xeb\xd0\x91\x81"
  1160. "\xa7\x29\x59\x09\x8b\x3e\xf8\xc1\x22\xd9\x63\x55\x14\xce\xd5"
  1161. "\x65\xfe";
  1162. c.inLen = strlen(c.input);
  1163. c.outLen = SHA256_DIGEST_SIZE;
  1164. test_hmac[0] = a;
  1165. test_hmac[1] = b;
  1166. test_hmac[2] = c;
  1167. for (i = 0; i < times; ++i) {
  1168. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  1169. if (i == 1)
  1170. continue; /* cavium can't handle short keys, fips not allowed */
  1171. #endif
  1172. #ifdef HAVE_CAVIUM
  1173. if (wc_HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0)
  1174. return -20011;
  1175. #endif
  1176. ret = wc_HmacSetKey(&hmac, SHA256, (byte*)keys[i],(word32)strlen(keys[i]));
  1177. if (ret != 0)
  1178. return -4021;
  1179. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  1180. (word32)test_hmac[i].inLen);
  1181. if (ret != 0)
  1182. return -4022;
  1183. ret = wc_HmacFinal(&hmac, hash);
  1184. if (ret != 0)
  1185. return -4023;
  1186. if (memcmp(hash, test_hmac[i].output, SHA256_DIGEST_SIZE) != 0)
  1187. return -20 - i;
  1188. #ifdef HAVE_CAVIUM
  1189. wc_HmacFreeCavium(&hmac);
  1190. #endif
  1191. }
  1192. return 0;
  1193. }
  1194. #endif
  1195. #if !defined(NO_HMAC) && defined(HAVE_BLAKE2)
  1196. int hmac_blake2b_test(void)
  1197. {
  1198. Hmac hmac;
  1199. byte hash[BLAKE2B_256];
  1200. const char* keys[]=
  1201. {
  1202. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  1203. "\x0b\x0b\x0b",
  1204. "Jefe",
  1205. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  1206. "\xAA\xAA\xAA"
  1207. };
  1208. testVector a, b, c;
  1209. testVector test_hmac[3];
  1210. int ret;
  1211. int times = sizeof(test_hmac) / sizeof(testVector), i;
  1212. a.input = "Hi There";
  1213. a.output = "\x72\x93\x0d\xdd\xf5\xf7\xe1\x78\x38\x07\x44\x18\x0b\x3f\x51"
  1214. "\x37\x25\xb5\x82\xc2\x08\x83\x2f\x1c\x99\xfd\x03\xa0\x16\x75"
  1215. "\xac\xfd";
  1216. a.inLen = strlen(a.input);
  1217. a.outLen = BLAKE2B_256;
  1218. b.input = "what do ya want for nothing?";
  1219. b.output = "\x3d\x20\x50\x71\x05\xc0\x8c\x0c\x38\x44\x1e\xf7\xf9\xd1\x67"
  1220. "\x21\xff\x64\xf5\x94\x00\xcf\xf9\x75\x41\xda\x88\x61\x9d\x7c"
  1221. "\xda\x2b";
  1222. b.inLen = strlen(b.input);
  1223. b.outLen = BLAKE2B_256;
  1224. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1225. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1226. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1227. "\xDD\xDD\xDD\xDD\xDD\xDD";
  1228. c.output = "\xda\xfe\x2a\x24\xfc\xe7\xea\x36\x34\xbe\x41\x92\xc7\x11\xa7"
  1229. "\x00\xae\x53\x9c\x11\x9c\x80\x74\x55\x22\x25\x4a\xb9\x55\xd3"
  1230. "\x0f\x87";
  1231. c.inLen = strlen(c.input);
  1232. c.outLen = BLAKE2B_256;
  1233. test_hmac[0] = a;
  1234. test_hmac[1] = b;
  1235. test_hmac[2] = c;
  1236. for (i = 0; i < times; ++i) {
  1237. #if defined(HAVE_FIPS) || defined(HAVE_CAVIUM)
  1238. if (i == 1)
  1239. continue; /* cavium can't handle short keys, fips not allowed */
  1240. #endif
  1241. #ifdef HAVE_CAVIUM
  1242. if (wc_HmacInitCavium(&hmac, CAVIUM_DEV_ID) != 0)
  1243. return -20011;
  1244. #endif
  1245. ret = wc_HmacSetKey(&hmac, BLAKE2B_ID, (byte*)keys[i],
  1246. (word32)strlen(keys[i]));
  1247. if (ret != 0)
  1248. return -4024;
  1249. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  1250. (word32)test_hmac[i].inLen);
  1251. if (ret != 0)
  1252. return -4025;
  1253. ret = wc_HmacFinal(&hmac, hash);
  1254. if (ret != 0)
  1255. return -4026;
  1256. if (memcmp(hash, test_hmac[i].output, BLAKE2B_256) != 0)
  1257. return -20 - i;
  1258. #ifdef HAVE_CAVIUM
  1259. wc_HmacFreeCavium(&hmac);
  1260. #endif
  1261. }
  1262. return 0;
  1263. }
  1264. #endif
  1265. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  1266. int hmac_sha384_test(void)
  1267. {
  1268. Hmac hmac;
  1269. byte hash[SHA384_DIGEST_SIZE];
  1270. const char* keys[]=
  1271. {
  1272. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  1273. "\x0b\x0b\x0b",
  1274. "Jefe",
  1275. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  1276. "\xAA\xAA\xAA"
  1277. };
  1278. testVector a, b, c;
  1279. testVector test_hmac[3];
  1280. int ret;
  1281. int times = sizeof(test_hmac) / sizeof(testVector), i;
  1282. a.input = "Hi There";
  1283. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  1284. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  1285. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  1286. "\xfa\x9c\xb6";
  1287. a.inLen = strlen(a.input);
  1288. a.outLen = SHA384_DIGEST_SIZE;
  1289. b.input = "what do ya want for nothing?";
  1290. b.output = "\xaf\x45\xd2\xe3\x76\x48\x40\x31\x61\x7f\x78\xd2\xb5\x8a\x6b"
  1291. "\x1b\x9c\x7e\xf4\x64\xf5\xa0\x1b\x47\xe4\x2e\xc3\x73\x63\x22"
  1292. "\x44\x5e\x8e\x22\x40\xca\x5e\x69\xe2\xc7\x8b\x32\x39\xec\xfa"
  1293. "\xb2\x16\x49";
  1294. b.inLen = strlen(b.input);
  1295. b.outLen = SHA384_DIGEST_SIZE;
  1296. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1297. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1298. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1299. "\xDD\xDD\xDD\xDD\xDD\xDD";
  1300. c.output = "\x88\x06\x26\x08\xd3\xe6\xad\x8a\x0a\xa2\xac\xe0\x14\xc8\xa8"
  1301. "\x6f\x0a\xa6\x35\xd9\x47\xac\x9f\xeb\xe8\x3e\xf4\xe5\x59\x66"
  1302. "\x14\x4b\x2a\x5a\xb3\x9d\xc1\x38\x14\xb9\x4e\x3a\xb6\xe1\x01"
  1303. "\xa3\x4f\x27";
  1304. c.inLen = strlen(c.input);
  1305. c.outLen = SHA384_DIGEST_SIZE;
  1306. test_hmac[0] = a;
  1307. test_hmac[1] = b;
  1308. test_hmac[2] = c;
  1309. for (i = 0; i < times; ++i) {
  1310. #if defined(HAVE_FIPS)
  1311. if (i == 1)
  1312. continue; /* fips not allowed */
  1313. #endif
  1314. ret = wc_HmacSetKey(&hmac, SHA384, (byte*)keys[i],(word32)strlen(keys[i]));
  1315. if (ret != 0)
  1316. return -4027;
  1317. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  1318. (word32)test_hmac[i].inLen);
  1319. if (ret != 0)
  1320. return -4028;
  1321. ret = wc_HmacFinal(&hmac, hash);
  1322. if (ret != 0)
  1323. return -4029;
  1324. if (memcmp(hash, test_hmac[i].output, SHA384_DIGEST_SIZE) != 0)
  1325. return -20 - i;
  1326. }
  1327. return 0;
  1328. }
  1329. #endif
  1330. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA512)
  1331. int hmac_sha512_test(void)
  1332. {
  1333. Hmac hmac;
  1334. byte hash[SHA512_DIGEST_SIZE];
  1335. const char* keys[]=
  1336. {
  1337. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  1338. "\x0b\x0b\x0b",
  1339. "Jefe",
  1340. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  1341. "\xAA\xAA\xAA"
  1342. };
  1343. testVector a, b, c;
  1344. testVector test_hmac[3];
  1345. int ret;
  1346. int times = sizeof(test_hmac) / sizeof(testVector), i;
  1347. a.input = "Hi There";
  1348. a.output = "\x87\xaa\x7c\xde\xa5\xef\x61\x9d\x4f\xf0\xb4\x24\x1a\x1d\x6c"
  1349. "\xb0\x23\x79\xf4\xe2\xce\x4e\xc2\x78\x7a\xd0\xb3\x05\x45\xe1"
  1350. "\x7c\xde\xda\xa8\x33\xb7\xd6\xb8\xa7\x02\x03\x8b\x27\x4e\xae"
  1351. "\xa3\xf4\xe4\xbe\x9d\x91\x4e\xeb\x61\xf1\x70\x2e\x69\x6c\x20"
  1352. "\x3a\x12\x68\x54";
  1353. a.inLen = strlen(a.input);
  1354. a.outLen = SHA512_DIGEST_SIZE;
  1355. b.input = "what do ya want for nothing?";
  1356. b.output = "\x16\x4b\x7a\x7b\xfc\xf8\x19\xe2\xe3\x95\xfb\xe7\x3b\x56\xe0"
  1357. "\xa3\x87\xbd\x64\x22\x2e\x83\x1f\xd6\x10\x27\x0c\xd7\xea\x25"
  1358. "\x05\x54\x97\x58\xbf\x75\xc0\x5a\x99\x4a\x6d\x03\x4f\x65\xf8"
  1359. "\xf0\xe6\xfd\xca\xea\xb1\xa3\x4d\x4a\x6b\x4b\x63\x6e\x07\x0a"
  1360. "\x38\xbc\xe7\x37";
  1361. b.inLen = strlen(b.input);
  1362. b.outLen = SHA512_DIGEST_SIZE;
  1363. c.input = "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1364. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1365. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  1366. "\xDD\xDD\xDD\xDD\xDD\xDD";
  1367. c.output = "\xfa\x73\xb0\x08\x9d\x56\xa2\x84\xef\xb0\xf0\x75\x6c\x89\x0b"
  1368. "\xe9\xb1\xb5\xdb\xdd\x8e\xe8\x1a\x36\x55\xf8\x3e\x33\xb2\x27"
  1369. "\x9d\x39\xbf\x3e\x84\x82\x79\xa7\x22\xc8\x06\xb4\x85\xa4\x7e"
  1370. "\x67\xc8\x07\xb9\x46\xa3\x37\xbe\xe8\x94\x26\x74\x27\x88\x59"
  1371. "\xe1\x32\x92\xfb";
  1372. c.inLen = strlen(c.input);
  1373. c.outLen = SHA512_DIGEST_SIZE;
  1374. test_hmac[0] = a;
  1375. test_hmac[1] = b;
  1376. test_hmac[2] = c;
  1377. for (i = 0; i < times; ++i) {
  1378. #if defined(HAVE_FIPS)
  1379. if (i == 1)
  1380. continue; /* fips not allowed */
  1381. #endif
  1382. ret = wc_HmacSetKey(&hmac, SHA512, (byte*)keys[i],(word32)strlen(keys[i]));
  1383. if (ret != 0)
  1384. return -4030;
  1385. ret = wc_HmacUpdate(&hmac, (byte*)test_hmac[i].input,
  1386. (word32)test_hmac[i].inLen);
  1387. if (ret != 0)
  1388. return -4031;
  1389. ret = wc_HmacFinal(&hmac, hash);
  1390. if (ret != 0)
  1391. return -4032;
  1392. if (memcmp(hash, test_hmac[i].output, SHA512_DIGEST_SIZE) != 0)
  1393. return -20 - i;
  1394. }
  1395. return 0;
  1396. }
  1397. #endif
  1398. #ifndef NO_RC4
  1399. int arc4_test(void)
  1400. {
  1401. byte cipher[16];
  1402. byte plain[16];
  1403. const char* keys[] =
  1404. {
  1405. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  1406. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  1407. "\x00\x00\x00\x00\x00\x00\x00\x00",
  1408. "\xef\x01\x23\x45"
  1409. };
  1410. testVector a, b, c, d;
  1411. testVector test_arc4[4];
  1412. int times = sizeof(test_arc4) / sizeof(testVector), i;
  1413. a.input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  1414. a.output = "\x75\xb7\x87\x80\x99\xe0\xc5\x96";
  1415. a.inLen = 8;
  1416. a.outLen = 8;
  1417. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  1418. b.output = "\x74\x94\xc2\xe7\x10\x4b\x08\x79";
  1419. b.inLen = 8;
  1420. b.outLen = 8;
  1421. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  1422. c.output = "\xde\x18\x89\x41\xa3\x37\x5d\x3a";
  1423. c.inLen = 8;
  1424. c.outLen = 8;
  1425. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  1426. d.output = "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61";
  1427. d.inLen = 10;
  1428. d.outLen = 10;
  1429. test_arc4[0] = a;
  1430. test_arc4[1] = b;
  1431. test_arc4[2] = c;
  1432. test_arc4[3] = d;
  1433. for (i = 0; i < times; ++i) {
  1434. Arc4 enc;
  1435. Arc4 dec;
  1436. int keylen = 8; /* strlen with key 0x00 not good */
  1437. if (i == 3)
  1438. keylen = 4;
  1439. #ifdef HAVE_CAVIUM
  1440. if (wc_Arc4InitCavium(&enc, CAVIUM_DEV_ID) != 0)
  1441. return -20001;
  1442. if (wc_Arc4InitCavium(&dec, CAVIUM_DEV_ID) != 0)
  1443. return -20002;
  1444. #endif
  1445. wc_Arc4SetKey(&enc, (byte*)keys[i], keylen);
  1446. wc_Arc4SetKey(&dec, (byte*)keys[i], keylen);
  1447. wc_Arc4Process(&enc, cipher, (byte*)test_arc4[i].input,
  1448. (word32)test_arc4[i].outLen);
  1449. wc_Arc4Process(&dec, plain, cipher, (word32)test_arc4[i].outLen);
  1450. if (memcmp(plain, test_arc4[i].input, test_arc4[i].outLen))
  1451. return -20 - i;
  1452. if (memcmp(cipher, test_arc4[i].output, test_arc4[i].outLen))
  1453. return -20 - 5 - i;
  1454. #ifdef HAVE_CAVIUM
  1455. wc_Arc4FreeCavium(&enc);
  1456. wc_Arc4FreeCavium(&dec);
  1457. #endif
  1458. }
  1459. return 0;
  1460. }
  1461. #endif
  1462. int hc128_test(void)
  1463. {
  1464. #ifdef HAVE_HC128
  1465. byte cipher[16];
  1466. byte plain[16];
  1467. const char* keys[] =
  1468. {
  1469. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  1470. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  1471. "\x00\x53\xA6\xF9\x4C\x9F\xF2\x45\x98\xEB\x3E\x91\xE4\x37\x8A\xDD",
  1472. "\x0F\x62\xB5\x08\x5B\xAE\x01\x54\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC"
  1473. };
  1474. const char* ivs[] =
  1475. {
  1476. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  1477. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  1478. "\x0D\x74\xDB\x42\xA9\x10\x77\xDE\x45\xAC\x13\x7A\xE1\x48\xAF\x16",
  1479. "\x28\x8F\xF6\x5D\xC4\x2B\x92\xF9\x60\xC7\x2E\x95\xFC\x63\xCA\x31"
  1480. };
  1481. testVector a, b, c, d;
  1482. testVector test_hc128[4];
  1483. int times = sizeof(test_hc128) / sizeof(testVector), i;
  1484. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  1485. a.output = "\x37\x86\x02\xB9\x8F\x32\xA7\x48";
  1486. a.inLen = 8;
  1487. a.outLen = 8;
  1488. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  1489. b.output = "\x33\x7F\x86\x11\xC6\xED\x61\x5F";
  1490. b.inLen = 8;
  1491. b.outLen = 8;
  1492. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  1493. c.output = "\x2E\x1E\xD1\x2A\x85\x51\xC0\x5A";
  1494. c.inLen = 8;
  1495. c.outLen = 8;
  1496. d.input = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
  1497. d.output = "\x1C\xD8\xAE\xDD\xFE\x52\xE2\x17\xE8\x35\xD0\xB7\xE8\x4E\x29";
  1498. d.inLen = 15;
  1499. d.outLen = 15;
  1500. test_hc128[0] = a;
  1501. test_hc128[1] = b;
  1502. test_hc128[2] = c;
  1503. test_hc128[3] = d;
  1504. for (i = 0; i < times; ++i) {
  1505. HC128 enc;
  1506. HC128 dec;
  1507. /* align keys/ivs in plain/cipher buffers */
  1508. memcpy(plain, keys[i], 16);
  1509. memcpy(cipher, ivs[i], 16);
  1510. wc_Hc128_SetKey(&enc, plain, cipher);
  1511. wc_Hc128_SetKey(&dec, plain, cipher);
  1512. /* align input */
  1513. memcpy(plain, test_hc128[i].input, test_hc128[i].outLen);
  1514. wc_Hc128_Process(&enc, cipher, plain, (word32)test_hc128[i].outLen);
  1515. wc_Hc128_Process(&dec, plain, cipher, (word32)test_hc128[i].outLen);
  1516. if (memcmp(plain, test_hc128[i].input, test_hc128[i].outLen))
  1517. return -120 - i;
  1518. if (memcmp(cipher, test_hc128[i].output, test_hc128[i].outLen))
  1519. return -120 - 5 - i;
  1520. }
  1521. #endif /* HAVE_HC128 */
  1522. return 0;
  1523. }
  1524. #ifndef NO_RABBIT
  1525. int rabbit_test(void)
  1526. {
  1527. byte cipher[16];
  1528. byte plain[16];
  1529. const char* keys[] =
  1530. {
  1531. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  1532. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  1533. "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B\xFE\x36\x3D\x2E\x29\x13\x28\x91"
  1534. };
  1535. const char* ivs[] =
  1536. {
  1537. "\x00\x00\x00\x00\x00\x00\x00\x00",
  1538. "\x59\x7E\x26\xC1\x75\xF5\x73\xC3",
  1539. 0
  1540. };
  1541. testVector a, b, c;
  1542. testVector test_rabbit[3];
  1543. int times = sizeof(test_rabbit) / sizeof(testVector), i;
  1544. a.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  1545. a.output = "\xED\xB7\x05\x67\x37\x5D\xCD\x7C";
  1546. a.inLen = 8;
  1547. a.outLen = 8;
  1548. b.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  1549. b.output = "\x6D\x7D\x01\x22\x92\xCC\xDC\xE0";
  1550. b.inLen = 8;
  1551. b.outLen = 8;
  1552. c.input = "\x00\x00\x00\x00\x00\x00\x00\x00";
  1553. c.output = "\x04\xCE\xCA\x7A\x1A\x86\x6E\x77";
  1554. c.inLen = 8;
  1555. c.outLen = 8;
  1556. test_rabbit[0] = a;
  1557. test_rabbit[1] = b;
  1558. test_rabbit[2] = c;
  1559. for (i = 0; i < times; ++i) {
  1560. Rabbit enc;
  1561. Rabbit dec;
  1562. byte* iv;
  1563. /* align keys/ivs in plain/cipher buffers */
  1564. memcpy(plain, keys[i], 16);
  1565. if (ivs[i]) {
  1566. memcpy(cipher, ivs[i], 8);
  1567. iv = cipher;
  1568. } else
  1569. iv = NULL;
  1570. wc_RabbitSetKey(&enc, plain, iv);
  1571. wc_RabbitSetKey(&dec, plain, iv);
  1572. /* align input */
  1573. memcpy(plain, test_rabbit[i].input, test_rabbit[i].outLen);
  1574. wc_RabbitProcess(&enc, cipher, plain, (word32)test_rabbit[i].outLen);
  1575. wc_RabbitProcess(&dec, plain, cipher, (word32)test_rabbit[i].outLen);
  1576. if (memcmp(plain, test_rabbit[i].input, test_rabbit[i].outLen))
  1577. return -130 - i;
  1578. if (memcmp(cipher, test_rabbit[i].output, test_rabbit[i].outLen))
  1579. return -130 - 5 - i;
  1580. }
  1581. return 0;
  1582. }
  1583. #endif /* NO_RABBIT */
  1584. #ifdef HAVE_CHACHA
  1585. int chacha_test(void)
  1586. {
  1587. ChaCha enc;
  1588. ChaCha dec;
  1589. byte cipher[128];
  1590. byte plain[128];
  1591. byte sliver[64];
  1592. byte input[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  1593. word32 keySz = 32;
  1594. int ret = 0;
  1595. int i;
  1596. int times = 4;
  1597. static const byte key1[] =
  1598. {
  1599. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1600. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1601. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1602. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  1603. };
  1604. static const byte key2[] =
  1605. {
  1606. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1607. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1608. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1609. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  1610. };
  1611. static const byte key3[] =
  1612. {
  1613. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1614. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1615. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1616. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  1617. };
  1618. /* 128 bit key */
  1619. static const byte key4[] =
  1620. {
  1621. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1622. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  1623. };
  1624. const byte* keys[] = {key1, key2, key3, key4};
  1625. static const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  1626. static const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  1627. static const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
  1628. static const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  1629. const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4};
  1630. byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90};
  1631. byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96};
  1632. byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5};
  1633. byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd};
  1634. byte* test_chacha[4];
  1635. test_chacha[0] = a;
  1636. test_chacha[1] = b;
  1637. test_chacha[2] = c;
  1638. test_chacha[3] = d;
  1639. for (i = 0; i < times; ++i) {
  1640. if (i < 3) {
  1641. keySz = 32;
  1642. }
  1643. else {
  1644. keySz = 16;
  1645. }
  1646. XMEMCPY(plain, keys[i], keySz);
  1647. XMEMSET(cipher, 0, 32);
  1648. XMEMCPY(cipher + 4, ivs[i], 8);
  1649. ret |= wc_Chacha_SetKey(&enc, keys[i], keySz);
  1650. ret |= wc_Chacha_SetKey(&dec, keys[i], keySz);
  1651. if (ret != 0)
  1652. return ret;
  1653. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  1654. ret |= wc_Chacha_SetIV(&dec, cipher, 0);
  1655. if (ret != 0)
  1656. return ret;
  1657. XMEMCPY(plain, input, 8);
  1658. ret |= wc_Chacha_Process(&enc, cipher, plain, (word32)8);
  1659. ret |= wc_Chacha_Process(&dec, plain, cipher, (word32)8);
  1660. if (ret != 0)
  1661. return ret;
  1662. if (XMEMCMP(test_chacha[i], cipher, 8))
  1663. return -130 - 5 - i;
  1664. if (XMEMCMP(plain, input, 8))
  1665. return -130 - i;
  1666. }
  1667. /* test of starting at a diffrent counter
  1668. encrypts all of the information and decrypts starting at 2nd chunck */
  1669. XMEMSET(plain, 0, sizeof(plain));
  1670. XMEMSET(sliver, 1, sizeof(sliver)); /* set as 1's to not match plain */
  1671. XMEMSET(cipher, 0, sizeof(cipher));
  1672. XMEMCPY(cipher + 4, ivs[0], 8);
  1673. ret |= wc_Chacha_SetKey(&enc, keys[0], keySz);
  1674. ret |= wc_Chacha_SetKey(&dec, keys[0], keySz);
  1675. if (ret != 0)
  1676. return ret;
  1677. ret |= wc_Chacha_SetIV(&enc, cipher, 0);
  1678. ret |= wc_Chacha_SetIV(&dec, cipher, 1);
  1679. if (ret != 0)
  1680. return ret;
  1681. ret |= wc_Chacha_Process(&enc, cipher, plain, sizeof(plain));
  1682. ret |= wc_Chacha_Process(&dec, sliver, cipher + 64, sizeof(sliver));
  1683. if (ret != 0)
  1684. return ret;
  1685. if (XMEMCMP(plain + 64, sliver, 64))
  1686. return -140;
  1687. return 0;
  1688. }
  1689. #endif /* HAVE_CHACHA */
  1690. #ifdef HAVE_POLY1305
  1691. int poly1305_test(void)
  1692. {
  1693. int ret = 0;
  1694. int i;
  1695. byte tag[16];
  1696. Poly1305 enc;
  1697. const byte msg[] =
  1698. {
  1699. 0x43,0x72,0x79,0x70,0x74,0x6f,0x67,0x72,
  1700. 0x61,0x70,0x68,0x69,0x63,0x20,0x46,0x6f,
  1701. 0x72,0x75,0x6d,0x20,0x52,0x65,0x73,0x65,
  1702. 0x61,0x72,0x63,0x68,0x20,0x47,0x72,0x6f,
  1703. 0x75,0x70
  1704. };
  1705. const byte msg2[] =
  1706. {
  1707. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x77,0x6f,0x72,
  1708. 0x6c,0x64,0x21
  1709. };
  1710. const byte msg3[] =
  1711. {
  1712. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1713. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1714. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  1715. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  1716. };
  1717. const byte correct[] =
  1718. {
  1719. 0xa8,0x06,0x1d,0xc1,0x30,0x51,0x36,0xc6,
  1720. 0xc2,0x2b,0x8b,0xaf,0x0c,0x01,0x27,0xa9
  1721. };
  1722. const byte correct2[] =
  1723. {
  1724. 0xa6,0xf7,0x45,0x00,0x8f,0x81,0xc9,0x16,
  1725. 0xa2,0x0d,0xcc,0x74,0xee,0xf2,0xb2,0xf0
  1726. };
  1727. const byte correct3[] =
  1728. {
  1729. 0x49,0xec,0x78,0x09,0x0e,0x48,0x1e,0xc6,
  1730. 0xc2,0x6b,0x33,0xb9,0x1c,0xcc,0x03,0x07
  1731. };
  1732. const byte key[] = {
  1733. 0x85,0xd6,0xbe,0x78,0x57,0x55,0x6d,0x33,
  1734. 0x7f,0x44,0x52,0xfe,0x42,0xd5,0x06,0xa8,
  1735. 0x01,0x03,0x80,0x8a,0xfb,0x0d,0xb2,0xfd,
  1736. 0x4a,0xbf,0xf6,0xaf,0x41,0x49,0xf5,0x1b
  1737. };
  1738. const byte key2[] = {
  1739. 0x74,0x68,0x69,0x73,0x20,0x69,0x73,0x20,
  1740. 0x33,0x32,0x2d,0x62,0x79,0x74,0x65,0x20,
  1741. 0x6b,0x65,0x79,0x20,0x66,0x6f,0x72,0x20,
  1742. 0x50,0x6f,0x6c,0x79,0x31,0x33,0x30,0x35
  1743. };
  1744. const byte* msgs[] = {msg, msg2, msg3};
  1745. word32 szm[] = {sizeof(msg),sizeof(msg2),sizeof(msg3)};
  1746. const byte* keys[] = {key, key2, key2};
  1747. const byte* tests[] = {correct, correct2, correct3};
  1748. for (i = 0; i < 3; i++) {
  1749. ret = wc_Poly1305SetKey(&enc, keys[i], 32);
  1750. if (ret != 0)
  1751. return -1001;
  1752. ret = wc_Poly1305Update(&enc, msgs[i], szm[i]);
  1753. if (ret != 0)
  1754. return -1005;
  1755. ret = wc_Poly1305Final(&enc, tag);
  1756. if (ret != 0)
  1757. return -60;
  1758. if (memcmp(tag, tests[i], sizeof(tag)))
  1759. return -61;
  1760. }
  1761. return 0;
  1762. }
  1763. #endif /* HAVE_POLY1305 */
  1764. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1765. int chacha20_poly1305_aead_test(void)
  1766. {
  1767. /* Test #1 from Section 2.8.2 of draft-irtf-cfrg-chacha20-poly1305-10 */
  1768. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  1769. const byte key1[] = {
  1770. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  1771. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  1772. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  1773. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  1774. };
  1775. const byte plaintext1[] = {
  1776. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  1777. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  1778. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  1779. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  1780. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  1781. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  1782. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  1783. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  1784. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  1785. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  1786. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  1787. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  1788. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  1789. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  1790. 0x74, 0x2e
  1791. };
  1792. const byte iv1[] = {
  1793. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  1794. 0x44, 0x45, 0x46, 0x47
  1795. };
  1796. const byte aad1[] = { /* additional data */
  1797. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  1798. 0xc4, 0xc5, 0xc6, 0xc7
  1799. };
  1800. const byte cipher1[] = { /* expected output from operation */
  1801. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  1802. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  1803. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  1804. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  1805. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  1806. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  1807. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  1808. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  1809. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  1810. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  1811. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  1812. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  1813. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  1814. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  1815. 0x61, 0x16
  1816. };
  1817. const byte authTag1[] = { /* expected output from operation */
  1818. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  1819. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  1820. };
  1821. /* Test #2 from Appendix A.2 in draft-irtf-cfrg-chacha20-poly1305-10 */
  1822. /* https://tools.ietf.org/html/draft-irtf-cfrg-chacha20-poly1305-10 */
  1823. const byte key2[] = {
  1824. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  1825. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  1826. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  1827. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  1828. };
  1829. const byte plaintext2[] = {
  1830. 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
  1831. 0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
  1832. 0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
  1833. 0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  1834. 0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
  1835. 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
  1836. 0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
  1837. 0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
  1838. 0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
  1839. 0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
  1840. 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
  1841. 0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
  1842. 0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
  1843. 0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
  1844. 0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
  1845. 0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
  1846. 0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
  1847. 0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
  1848. 0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
  1849. 0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
  1850. 0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
  1851. 0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
  1852. 0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
  1853. 0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
  1854. 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
  1855. 0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
  1856. 0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
  1857. 0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
  1858. 0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
  1859. 0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
  1860. 0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
  1861. 0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
  1862. 0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
  1863. 0x9d
  1864. };
  1865. const byte iv2[] = {
  1866. 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  1867. 0x05, 0x06, 0x07, 0x08
  1868. };
  1869. const byte aad2[] = { /* additional data */
  1870. 0xf3, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
  1871. 0x00, 0x00, 0x4e, 0x91
  1872. };
  1873. const byte cipher2[] = { /* expected output from operation */
  1874. 0x64, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
  1875. 0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
  1876. 0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
  1877. 0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
  1878. 0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
  1879. 0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
  1880. 0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
  1881. 0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
  1882. 0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
  1883. 0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
  1884. 0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
  1885. 0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
  1886. 0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
  1887. 0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
  1888. 0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
  1889. 0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
  1890. 0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
  1891. 0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
  1892. 0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
  1893. 0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
  1894. 0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
  1895. 0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
  1896. 0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
  1897. 0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
  1898. 0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
  1899. 0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
  1900. 0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
  1901. 0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
  1902. 0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
  1903. 0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
  1904. 0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
  1905. 0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
  1906. 0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
  1907. 0x9b
  1908. };
  1909. const byte authTag2[] = { /* expected output from operation */
  1910. 0xee, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
  1911. 0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
  1912. };
  1913. byte generatedCiphertext[272];
  1914. byte generatedPlaintext[272];
  1915. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  1916. int err;
  1917. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  1918. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  1919. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  1920. /* Test #1 */
  1921. err = wc_ChaCha20Poly1305_Encrypt(key1, iv1,
  1922. aad1, sizeof(aad1),
  1923. plaintext1, sizeof(plaintext1),
  1924. generatedCiphertext, generatedAuthTag);
  1925. if (err)
  1926. {
  1927. return err;
  1928. }
  1929. /* -- Check the ciphertext and authtag */
  1930. if (XMEMCMP(generatedCiphertext, cipher1, sizeof(cipher1)))
  1931. {
  1932. return -1064;
  1933. }
  1934. if (XMEMCMP(generatedAuthTag, authTag1, sizeof(authTag1)))
  1935. {
  1936. return -1065;
  1937. }
  1938. /* -- Verify decryption works */
  1939. err = wc_ChaCha20Poly1305_Decrypt(key1, iv1,
  1940. aad1, sizeof(aad1),
  1941. cipher1, sizeof(cipher1),
  1942. authTag1, generatedPlaintext);
  1943. if (err)
  1944. {
  1945. return err;
  1946. }
  1947. if (XMEMCMP(generatedPlaintext, plaintext1, sizeof( plaintext1)))
  1948. {
  1949. return -1066;
  1950. }
  1951. XMEMSET(generatedCiphertext, 0, sizeof(generatedCiphertext));
  1952. XMEMSET(generatedAuthTag, 0, sizeof(generatedAuthTag));
  1953. XMEMSET(generatedPlaintext, 0, sizeof(generatedPlaintext));
  1954. /* Test #2 */
  1955. err = wc_ChaCha20Poly1305_Encrypt(key2, iv2,
  1956. aad2, sizeof(aad2),
  1957. plaintext2, sizeof(plaintext2),
  1958. generatedCiphertext, generatedAuthTag);
  1959. if (err)
  1960. {
  1961. return err;
  1962. }
  1963. /* -- Check the ciphertext and authtag */
  1964. if (XMEMCMP(generatedCiphertext, cipher2, sizeof(cipher2)))
  1965. {
  1966. return -1067;
  1967. }
  1968. if (XMEMCMP(generatedAuthTag, authTag2, sizeof(authTag2)))
  1969. {
  1970. return -1068;
  1971. }
  1972. /* -- Verify decryption works */
  1973. err = wc_ChaCha20Poly1305_Decrypt(key2, iv2,
  1974. aad2, sizeof(aad2),
  1975. cipher2, sizeof(cipher2),
  1976. authTag2, generatedPlaintext);
  1977. if (err)
  1978. {
  1979. return err;
  1980. }
  1981. if (XMEMCMP(generatedPlaintext, plaintext2, sizeof(plaintext2)))
  1982. {
  1983. return -1069;
  1984. }
  1985. return err;
  1986. }
  1987. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  1988. #ifndef NO_DES3
  1989. int des_test(void)
  1990. {
  1991. const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  1992. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  1993. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  1994. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  1995. };
  1996. byte plain[24];
  1997. byte cipher[24];
  1998. Des enc;
  1999. Des dec;
  2000. const byte key[] =
  2001. {
  2002. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  2003. };
  2004. const byte iv[] =
  2005. {
  2006. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  2007. };
  2008. const byte verify[] =
  2009. {
  2010. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  2011. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  2012. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  2013. };
  2014. int ret;
  2015. ret = wc_Des_SetKey(&enc, key, iv, DES_ENCRYPTION);
  2016. if (ret != 0)
  2017. return -31;
  2018. wc_Des_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  2019. ret = wc_Des_SetKey(&dec, key, iv, DES_DECRYPTION);
  2020. if (ret != 0)
  2021. return -32;
  2022. wc_Des_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  2023. if (memcmp(plain, vector, sizeof(plain)))
  2024. return -33;
  2025. if (memcmp(cipher, verify, sizeof(cipher)))
  2026. return -34;
  2027. return 0;
  2028. }
  2029. #endif /* NO_DES3 */
  2030. #ifndef NO_DES3
  2031. int des3_test(void)
  2032. {
  2033. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  2034. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  2035. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  2036. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  2037. };
  2038. byte plain[24];
  2039. byte cipher[24];
  2040. Des3 enc;
  2041. Des3 dec;
  2042. const byte key3[] =
  2043. {
  2044. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  2045. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  2046. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  2047. };
  2048. const byte iv3[] =
  2049. {
  2050. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  2051. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  2052. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  2053. };
  2054. const byte verify3[] =
  2055. {
  2056. 0x43,0xa0,0x29,0x7e,0xd1,0x84,0xf8,0x0e,
  2057. 0x89,0x64,0x84,0x32,0x12,0xd5,0x08,0x98,
  2058. 0x18,0x94,0x15,0x74,0x87,0x12,0x7d,0xb0
  2059. };
  2060. int ret;
  2061. #ifdef HAVE_CAVIUM
  2062. if (wc_Des3_InitCavium(&enc, CAVIUM_DEV_ID) != 0)
  2063. return -20005;
  2064. if (wc_Des3_InitCavium(&dec, CAVIUM_DEV_ID) != 0)
  2065. return -20006;
  2066. #endif
  2067. ret = wc_Des3_SetKey(&enc, key3, iv3, DES_ENCRYPTION);
  2068. if (ret != 0)
  2069. return -31;
  2070. ret = wc_Des3_SetKey(&dec, key3, iv3, DES_DECRYPTION);
  2071. if (ret != 0)
  2072. return -32;
  2073. ret = wc_Des3_CbcEncrypt(&enc, cipher, vector, sizeof(vector));
  2074. if (ret != 0)
  2075. return -33;
  2076. ret = wc_Des3_CbcDecrypt(&dec, plain, cipher, sizeof(cipher));
  2077. if (ret != 0)
  2078. return -34;
  2079. if (memcmp(plain, vector, sizeof(plain)))
  2080. return -35;
  2081. if (memcmp(cipher, verify3, sizeof(cipher)))
  2082. return -36;
  2083. #ifdef HAVE_CAVIUM
  2084. wc_Des3_FreeCavium(&enc);
  2085. wc_Des3_FreeCavium(&dec);
  2086. #endif
  2087. return 0;
  2088. }
  2089. #endif /* NO_DES */
  2090. #ifndef NO_AES
  2091. int aes_test(void)
  2092. {
  2093. Aes enc;
  2094. Aes dec;
  2095. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  2096. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  2097. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  2098. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  2099. };
  2100. const byte verify[] =
  2101. {
  2102. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  2103. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  2104. };
  2105. byte key[] = "0123456789abcdef "; /* align */
  2106. byte iv[] = "1234567890abcdef "; /* align */
  2107. byte cipher[AES_BLOCK_SIZE * 4];
  2108. byte plain [AES_BLOCK_SIZE * 4];
  2109. int ret;
  2110. #ifdef HAVE_CAVIUM
  2111. if (wc_AesInitCavium(&enc, CAVIUM_DEV_ID) != 0)
  2112. return -20003;
  2113. if (wc_AesInitCavium(&dec, CAVIUM_DEV_ID) != 0)
  2114. return -20004;
  2115. #endif
  2116. ret = wc_AesSetKey(&enc, key, AES_BLOCK_SIZE, iv, AES_ENCRYPTION);
  2117. if (ret != 0)
  2118. return -1001;
  2119. ret = wc_AesSetKey(&dec, key, AES_BLOCK_SIZE, iv, AES_DECRYPTION);
  2120. if (ret != 0)
  2121. return -1002;
  2122. ret = wc_AesCbcEncrypt(&enc, cipher, msg, AES_BLOCK_SIZE);
  2123. if (ret != 0)
  2124. return -1005;
  2125. ret = wc_AesCbcDecrypt(&dec, plain, cipher, AES_BLOCK_SIZE);
  2126. if (ret != 0)
  2127. return -1006;
  2128. if (memcmp(plain, msg, AES_BLOCK_SIZE))
  2129. return -60;
  2130. if (memcmp(cipher, verify, AES_BLOCK_SIZE))
  2131. return -61;
  2132. #ifdef HAVE_CAVIUM
  2133. wc_AesFreeCavium(&enc);
  2134. wc_AesFreeCavium(&dec);
  2135. #endif
  2136. #ifdef WOLFSSL_AES_COUNTER
  2137. {
  2138. const byte ctrKey[] =
  2139. {
  2140. 0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
  2141. 0xab,0xf7,0x15,0x88,0x09,0xcf,0x4f,0x3c
  2142. };
  2143. const byte ctrIv[] =
  2144. {
  2145. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  2146. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff
  2147. };
  2148. const byte ctrPlain[] =
  2149. {
  2150. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  2151. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a,
  2152. 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,
  2153. 0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51,
  2154. 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,
  2155. 0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef,
  2156. 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,
  2157. 0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10
  2158. };
  2159. const byte ctrCipher[] =
  2160. {
  2161. 0x87,0x4d,0x61,0x91,0xb6,0x20,0xe3,0x26,
  2162. 0x1b,0xef,0x68,0x64,0x99,0x0d,0xb6,0xce,
  2163. 0x98,0x06,0xf6,0x6b,0x79,0x70,0xfd,0xff,
  2164. 0x86,0x17,0x18,0x7b,0xb9,0xff,0xfd,0xff,
  2165. 0x5a,0xe4,0xdf,0x3e,0xdb,0xd5,0xd3,0x5e,
  2166. 0x5b,0x4f,0x09,0x02,0x0d,0xb0,0x3e,0xab,
  2167. 0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
  2168. 0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
  2169. };
  2170. const byte oddCipher[] =
  2171. {
  2172. 0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
  2173. 0xc2
  2174. };
  2175. wc_AesSetKeyDirect(&enc, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION);
  2176. /* Ctr only uses encrypt, even on key setup */
  2177. wc_AesSetKeyDirect(&dec, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION);
  2178. wc_AesCtrEncrypt(&enc, cipher, ctrPlain, AES_BLOCK_SIZE*4);
  2179. wc_AesCtrEncrypt(&dec, plain, cipher, AES_BLOCK_SIZE*4);
  2180. if (memcmp(plain, ctrPlain, AES_BLOCK_SIZE*4))
  2181. return -66;
  2182. if (memcmp(cipher, ctrCipher, AES_BLOCK_SIZE*4))
  2183. return -67;
  2184. /* let's try with just 9 bytes, non block size test */
  2185. wc_AesSetKeyDirect(&enc, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION);
  2186. /* Ctr only uses encrypt, even on key setup */
  2187. wc_AesSetKeyDirect(&dec, ctrKey, AES_BLOCK_SIZE, ctrIv, AES_ENCRYPTION);
  2188. wc_AesCtrEncrypt(&enc, cipher, ctrPlain, 9);
  2189. wc_AesCtrEncrypt(&dec, plain, cipher, 9);
  2190. if (memcmp(plain, ctrPlain, 9))
  2191. return -68;
  2192. if (memcmp(cipher, ctrCipher, 9))
  2193. return -69;
  2194. /* and an additional 9 bytes to reuse tmp left buffer */
  2195. wc_AesCtrEncrypt(&enc, cipher, ctrPlain, 9);
  2196. wc_AesCtrEncrypt(&dec, plain, cipher, 9);
  2197. if (memcmp(plain, ctrPlain, 9))
  2198. return -70;
  2199. if (memcmp(cipher, oddCipher, 9))
  2200. return -71;
  2201. }
  2202. #endif /* WOLFSSL_AES_COUNTER */
  2203. #if defined(WOLFSSL_AESNI) && defined(WOLFSSL_AES_DIRECT)
  2204. {
  2205. const byte niPlain[] =
  2206. {
  2207. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  2208. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  2209. };
  2210. const byte niCipher[] =
  2211. {
  2212. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  2213. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  2214. };
  2215. const byte niKey[] =
  2216. {
  2217. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  2218. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  2219. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  2220. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  2221. };
  2222. XMEMSET(cipher, 0, AES_BLOCK_SIZE);
  2223. ret = wc_AesSetKey(&enc, niKey, sizeof(niKey), cipher, AES_ENCRYPTION);
  2224. if (ret != 0)
  2225. return -1003;
  2226. wc_AesEncryptDirect(&enc, cipher, niPlain);
  2227. if (XMEMCMP(cipher, niCipher, AES_BLOCK_SIZE) != 0)
  2228. return -20006;
  2229. XMEMSET(plain, 0, AES_BLOCK_SIZE);
  2230. ret = wc_AesSetKey(&dec, niKey, sizeof(niKey), plain, AES_DECRYPTION);
  2231. if (ret != 0)
  2232. return -1004;
  2233. wc_AesDecryptDirect(&dec, plain, niCipher);
  2234. if (XMEMCMP(plain, niPlain, AES_BLOCK_SIZE) != 0)
  2235. return -20007;
  2236. }
  2237. #endif /* WOLFSSL_AESNI && WOLFSSL_AES_DIRECT */
  2238. return 0;
  2239. }
  2240. #ifdef HAVE_AESGCM
  2241. int aesgcm_test(void)
  2242. {
  2243. Aes enc;
  2244. /*
  2245. * This is Test Case 16 from the document Galois/
  2246. * Counter Mode of Operation (GCM) by McGrew and
  2247. * Viega.
  2248. */
  2249. const byte k[] =
  2250. {
  2251. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  2252. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08,
  2253. 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c,
  2254. 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08
  2255. };
  2256. const byte iv[] =
  2257. {
  2258. 0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce, 0xdb, 0xad,
  2259. 0xde, 0xca, 0xf8, 0x88
  2260. };
  2261. const byte p[] =
  2262. {
  2263. 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5,
  2264. 0xa5, 0x59, 0x09, 0xc5, 0xaf, 0xf5, 0x26, 0x9a,
  2265. 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
  2266. 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72,
  2267. 0x1c, 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53,
  2268. 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
  2269. 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57,
  2270. 0xba, 0x63, 0x7b, 0x39
  2271. };
  2272. const byte a[] =
  2273. {
  2274. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  2275. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  2276. 0xab, 0xad, 0xda, 0xd2
  2277. };
  2278. const byte c[] =
  2279. {
  2280. 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07,
  2281. 0xf4, 0x7f, 0x37, 0xa3, 0x2a, 0x84, 0x42, 0x7d,
  2282. 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
  2283. 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa,
  2284. 0x8c, 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d,
  2285. 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
  2286. 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a,
  2287. 0xbc, 0xc9, 0xf6, 0x62
  2288. };
  2289. const byte t[] =
  2290. {
  2291. 0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e, 0x17, 0x68,
  2292. 0xcd, 0xdf, 0x88, 0x53, 0xbb, 0x2d, 0x55, 0x1b
  2293. };
  2294. byte t2[sizeof(t)];
  2295. byte p2[sizeof(c)];
  2296. byte c2[sizeof(p)];
  2297. int result;
  2298. memset(t2, 0, sizeof(t2));
  2299. memset(c2, 0, sizeof(c2));
  2300. memset(p2, 0, sizeof(p2));
  2301. wc_AesGcmSetKey(&enc, k, sizeof(k));
  2302. /* AES-GCM encrypt and decrypt both use AES encrypt internally */
  2303. wc_AesGcmEncrypt(&enc, c2, p, sizeof(c2), iv, sizeof(iv),
  2304. t2, sizeof(t2), a, sizeof(a));
  2305. if (memcmp(c, c2, sizeof(c2)))
  2306. return -68;
  2307. if (memcmp(t, t2, sizeof(t2)))
  2308. return -69;
  2309. result = wc_AesGcmDecrypt(&enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  2310. t2, sizeof(t2), a, sizeof(a));
  2311. if (result != 0)
  2312. return -70;
  2313. if (memcmp(p, p2, sizeof(p2)))
  2314. return -71;
  2315. return 0;
  2316. }
  2317. int gmac_test(void)
  2318. {
  2319. Gmac gmac;
  2320. const byte k1[] =
  2321. {
  2322. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  2323. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  2324. };
  2325. const byte iv1[] =
  2326. {
  2327. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  2328. 0xe2, 0x8c, 0x8f, 0x16
  2329. };
  2330. const byte a1[] =
  2331. {
  2332. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  2333. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  2334. };
  2335. const byte t1[] =
  2336. {
  2337. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  2338. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  2339. };
  2340. const byte k2[] =
  2341. {
  2342. 0x40, 0xf7, 0xec, 0xb2, 0x52, 0x6d, 0xaa, 0xd4,
  2343. 0x74, 0x25, 0x1d, 0xf4, 0x88, 0x9e, 0xf6, 0x5b
  2344. };
  2345. const byte iv2[] =
  2346. {
  2347. 0xee, 0x9c, 0x6e, 0x06, 0x15, 0x45, 0x45, 0x03,
  2348. 0x1a, 0x60, 0x24, 0xa7
  2349. };
  2350. const byte a2[] =
  2351. {
  2352. 0x94, 0x81, 0x2c, 0x87, 0x07, 0x4e, 0x15, 0x18,
  2353. 0x34, 0xb8, 0x35, 0xaf, 0x1c, 0xa5, 0x7e, 0x56
  2354. };
  2355. const byte t2[] =
  2356. {
  2357. 0xc6, 0x81, 0x79, 0x8e, 0x3d, 0xda, 0xb0, 0x9f,
  2358. 0x8d, 0x83, 0xb0, 0xbb, 0x14, 0xb6, 0x91
  2359. };
  2360. const byte k3[] =
  2361. {
  2362. 0xb8, 0xe4, 0x9a, 0x5e, 0x37, 0xf9, 0x98, 0x2b,
  2363. 0xb9, 0x6d, 0xd0, 0xc9, 0xb6, 0xab, 0x26, 0xac
  2364. };
  2365. const byte iv3[] =
  2366. {
  2367. 0xe4, 0x4a, 0x42, 0x18, 0x8c, 0xae, 0x94, 0x92,
  2368. 0x6a, 0x9c, 0x26, 0xb0
  2369. };
  2370. const byte a3[] =
  2371. {
  2372. 0x9d, 0xb9, 0x61, 0x68, 0xa6, 0x76, 0x7a, 0x31,
  2373. 0xf8, 0x29, 0xe4, 0x72, 0x61, 0x68, 0x3f, 0x8a
  2374. };
  2375. const byte t3[] =
  2376. {
  2377. 0x23, 0xe2, 0x9f, 0x66, 0xe4, 0xc6, 0x52, 0x48
  2378. };
  2379. byte tag[16];
  2380. memset(tag, 0, sizeof(tag));
  2381. wc_GmacSetKey(&gmac, k1, sizeof(k1));
  2382. wc_GmacUpdate(&gmac, iv1, sizeof(iv1), a1, sizeof(a1), tag, sizeof(t1));
  2383. if (memcmp(t1, tag, sizeof(t1)) != 0)
  2384. return -126;
  2385. memset(tag, 0, sizeof(tag));
  2386. wc_GmacSetKey(&gmac, k2, sizeof(k2));
  2387. wc_GmacUpdate(&gmac, iv2, sizeof(iv2), a2, sizeof(a2), tag, sizeof(t2));
  2388. if (memcmp(t2, tag, sizeof(t2)) != 0)
  2389. return -127;
  2390. memset(tag, 0, sizeof(tag));
  2391. wc_GmacSetKey(&gmac, k3, sizeof(k3));
  2392. wc_GmacUpdate(&gmac, iv3, sizeof(iv3), a3, sizeof(a3), tag, sizeof(t3));
  2393. if (memcmp(t3, tag, sizeof(t3)) != 0)
  2394. return -128;
  2395. return 0;
  2396. }
  2397. #endif /* HAVE_AESGCM */
  2398. #ifdef HAVE_AESCCM
  2399. int aesccm_test(void)
  2400. {
  2401. Aes enc;
  2402. /* key */
  2403. const byte k[] =
  2404. {
  2405. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  2406. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  2407. };
  2408. /* nonce */
  2409. const byte iv[] =
  2410. {
  2411. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  2412. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  2413. };
  2414. /* plaintext */
  2415. const byte p[] =
  2416. {
  2417. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  2418. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  2419. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  2420. };
  2421. const byte a[] =
  2422. {
  2423. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  2424. };
  2425. const byte c[] =
  2426. {
  2427. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  2428. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  2429. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  2430. };
  2431. const byte t[] =
  2432. {
  2433. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  2434. };
  2435. byte t2[sizeof(t)];
  2436. byte p2[sizeof(p)];
  2437. byte c2[sizeof(c)];
  2438. int result;
  2439. memset(t2, 0, sizeof(t2));
  2440. memset(c2, 0, sizeof(c2));
  2441. memset(p2, 0, sizeof(p2));
  2442. wc_AesCcmSetKey(&enc, k, sizeof(k));
  2443. /* AES-CCM encrypt and decrypt both use AES encrypt internally */
  2444. wc_AesCcmEncrypt(&enc, c2, p, sizeof(c2), iv, sizeof(iv),
  2445. t2, sizeof(t2), a, sizeof(a));
  2446. if (memcmp(c, c2, sizeof(c2)))
  2447. return -107;
  2448. if (memcmp(t, t2, sizeof(t2)))
  2449. return -108;
  2450. result = wc_AesCcmDecrypt(&enc, p2, c2, sizeof(p2), iv, sizeof(iv),
  2451. t2, sizeof(t2), a, sizeof(a));
  2452. if (result != 0)
  2453. return -109;
  2454. if (memcmp(p, p2, sizeof(p2)))
  2455. return -110;
  2456. /* Test the authentication failure */
  2457. t2[0]++; /* Corrupt the authentication tag. */
  2458. result = wc_AesCcmDecrypt(&enc, p2, c, sizeof(p2), iv, sizeof(iv),
  2459. t2, sizeof(t2), a, sizeof(a));
  2460. if (result == 0)
  2461. return -111;
  2462. /* Clear c2 to compare against p2. p2 should be set to zero in case of
  2463. * authentication fail. */
  2464. memset(c2, 0, sizeof(c2));
  2465. if (memcmp(p2, c2, sizeof(p2)))
  2466. return -112;
  2467. return 0;
  2468. }
  2469. #endif /* HAVE_AESCCM */
  2470. #endif /* NO_AES */
  2471. #ifdef HAVE_CAMELLIA
  2472. enum {
  2473. CAM_ECB_ENC, CAM_ECB_DEC, CAM_CBC_ENC, CAM_CBC_DEC
  2474. };
  2475. typedef struct {
  2476. int type;
  2477. const byte* plaintext;
  2478. const byte* iv;
  2479. const byte* ciphertext;
  2480. const byte* key;
  2481. word32 keySz;
  2482. int errorCode;
  2483. } test_vector_t;
  2484. int camellia_test(void)
  2485. {
  2486. /* Camellia ECB Test Plaintext */
  2487. static const byte pte[] =
  2488. {
  2489. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  2490. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  2491. };
  2492. /* Camellia ECB Test Initialization Vector */
  2493. static const byte ive[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  2494. /* Test 1: Camellia ECB 128-bit key */
  2495. static const byte k1[] =
  2496. {
  2497. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  2498. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  2499. };
  2500. static const byte c1[] =
  2501. {
  2502. 0x67, 0x67, 0x31, 0x38, 0x54, 0x96, 0x69, 0x73,
  2503. 0x08, 0x57, 0x06, 0x56, 0x48, 0xea, 0xbe, 0x43
  2504. };
  2505. /* Test 2: Camellia ECB 192-bit key */
  2506. static const byte k2[] =
  2507. {
  2508. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  2509. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  2510. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  2511. };
  2512. static const byte c2[] =
  2513. {
  2514. 0xb4, 0x99, 0x34, 0x01, 0xb3, 0xe9, 0x96, 0xf8,
  2515. 0x4e, 0xe5, 0xce, 0xe7, 0xd7, 0x9b, 0x09, 0xb9
  2516. };
  2517. /* Test 3: Camellia ECB 256-bit key */
  2518. static const byte k3[] =
  2519. {
  2520. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  2521. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  2522. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  2523. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  2524. };
  2525. static const byte c3[] =
  2526. {
  2527. 0x9a, 0xcc, 0x23, 0x7d, 0xff, 0x16, 0xd7, 0x6c,
  2528. 0x20, 0xef, 0x7c, 0x91, 0x9e, 0x3a, 0x75, 0x09
  2529. };
  2530. /* Camellia CBC Test Plaintext */
  2531. static const byte ptc[] =
  2532. {
  2533. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  2534. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  2535. };
  2536. /* Camellia CBC Test Initialization Vector */
  2537. static const byte ivc[] =
  2538. {
  2539. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  2540. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  2541. };
  2542. /* Test 4: Camellia-CBC 128-bit key */
  2543. static const byte k4[] =
  2544. {
  2545. 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
  2546. 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
  2547. };
  2548. static const byte c4[] =
  2549. {
  2550. 0x16, 0x07, 0xCF, 0x49, 0x4B, 0x36, 0xBB, 0xF0,
  2551. 0x0D, 0xAE, 0xB0, 0xB5, 0x03, 0xC8, 0x31, 0xAB
  2552. };
  2553. /* Test 5: Camellia-CBC 192-bit key */
  2554. static const byte k5[] =
  2555. {
  2556. 0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
  2557. 0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
  2558. 0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
  2559. };
  2560. static const byte c5[] =
  2561. {
  2562. 0x2A, 0x48, 0x30, 0xAB, 0x5A, 0xC4, 0xA1, 0xA2,
  2563. 0x40, 0x59, 0x55, 0xFD, 0x21, 0x95, 0xCF, 0x93
  2564. };
  2565. /* Test 6: CBC 256-bit key */
  2566. static const byte k6[] =
  2567. {
  2568. 0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
  2569. 0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
  2570. 0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
  2571. 0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
  2572. };
  2573. static const byte c6[] =
  2574. {
  2575. 0xE6, 0xCF, 0xA3, 0x5F, 0xC0, 0x2B, 0x13, 0x4A,
  2576. 0x4D, 0x2C, 0x0B, 0x67, 0x37, 0xAC, 0x3E, 0xDA
  2577. };
  2578. byte out[CAMELLIA_BLOCK_SIZE];
  2579. Camellia cam;
  2580. int i, testsSz;
  2581. const test_vector_t testVectors[] =
  2582. {
  2583. {CAM_ECB_ENC, pte, ive, c1, k1, sizeof(k1), -114},
  2584. {CAM_ECB_ENC, pte, ive, c2, k2, sizeof(k2), -115},
  2585. {CAM_ECB_ENC, pte, ive, c3, k3, sizeof(k3), -116},
  2586. {CAM_ECB_DEC, pte, ive, c1, k1, sizeof(k1), -117},
  2587. {CAM_ECB_DEC, pte, ive, c2, k2, sizeof(k2), -118},
  2588. {CAM_ECB_DEC, pte, ive, c3, k3, sizeof(k3), -119},
  2589. {CAM_CBC_ENC, ptc, ivc, c4, k4, sizeof(k4), -120},
  2590. {CAM_CBC_ENC, ptc, ivc, c5, k5, sizeof(k5), -121},
  2591. {CAM_CBC_ENC, ptc, ivc, c6, k6, sizeof(k6), -122},
  2592. {CAM_CBC_DEC, ptc, ivc, c4, k4, sizeof(k4), -123},
  2593. {CAM_CBC_DEC, ptc, ivc, c5, k5, sizeof(k5), -124},
  2594. {CAM_CBC_DEC, ptc, ivc, c6, k6, sizeof(k6), -125}
  2595. };
  2596. testsSz = sizeof(testVectors)/sizeof(test_vector_t);
  2597. for (i = 0; i < testsSz; i++) {
  2598. if (wc_CamelliaSetKey(&cam, testVectors[i].key, testVectors[i].keySz,
  2599. testVectors[i].iv) != 0)
  2600. return testVectors[i].errorCode;
  2601. switch (testVectors[i].type) {
  2602. case CAM_ECB_ENC:
  2603. wc_CamelliaEncryptDirect(&cam, out, testVectors[i].plaintext);
  2604. if (memcmp(out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE))
  2605. return testVectors[i].errorCode;
  2606. break;
  2607. case CAM_ECB_DEC:
  2608. wc_CamelliaDecryptDirect(&cam, out, testVectors[i].ciphertext);
  2609. if (memcmp(out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE))
  2610. return testVectors[i].errorCode;
  2611. break;
  2612. case CAM_CBC_ENC:
  2613. wc_CamelliaCbcEncrypt(&cam, out, testVectors[i].plaintext,
  2614. CAMELLIA_BLOCK_SIZE);
  2615. if (memcmp(out, testVectors[i].ciphertext, CAMELLIA_BLOCK_SIZE))
  2616. return testVectors[i].errorCode;
  2617. break;
  2618. case CAM_CBC_DEC:
  2619. wc_CamelliaCbcDecrypt(&cam, out, testVectors[i].ciphertext,
  2620. CAMELLIA_BLOCK_SIZE);
  2621. if (memcmp(out, testVectors[i].plaintext, CAMELLIA_BLOCK_SIZE))
  2622. return testVectors[i].errorCode;
  2623. break;
  2624. default:
  2625. break;
  2626. }
  2627. }
  2628. /* Setting the IV and checking it was actually set. */
  2629. wc_CamelliaSetIV(&cam, ivc);
  2630. if (XMEMCMP(cam.reg, ivc, CAMELLIA_BLOCK_SIZE))
  2631. return -1;
  2632. /* Setting the IV to NULL should be same as all zeros IV */
  2633. if (wc_CamelliaSetIV(&cam, NULL) != 0 ||
  2634. XMEMCMP(cam.reg, ive, CAMELLIA_BLOCK_SIZE))
  2635. return -1;
  2636. /* First parameter should never be null */
  2637. if (wc_CamelliaSetIV(NULL, NULL) == 0)
  2638. return -1;
  2639. /* First parameter should never be null, check it fails */
  2640. if (wc_CamelliaSetKey(NULL, k1, sizeof(k1), NULL) == 0)
  2641. return -1;
  2642. /* Key should have a size of 16, 24, or 32 */
  2643. if (wc_CamelliaSetKey(&cam, k1, 0, NULL) == 0)
  2644. return -1;
  2645. return 0;
  2646. }
  2647. #endif /* HAVE_CAMELLIA */
  2648. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  2649. int random_test(void)
  2650. {
  2651. const byte test1Entropy[] =
  2652. {
  2653. 0xa6, 0x5a, 0xd0, 0xf3, 0x45, 0xdb, 0x4e, 0x0e, 0xff, 0xe8, 0x75, 0xc3,
  2654. 0xa2, 0xe7, 0x1f, 0x42, 0xc7, 0x12, 0x9d, 0x62, 0x0f, 0xf5, 0xc1, 0x19,
  2655. 0xa9, 0xef, 0x55, 0xf0, 0x51, 0x85, 0xe0, 0xfb, 0x85, 0x81, 0xf9, 0x31,
  2656. 0x75, 0x17, 0x27, 0x6e, 0x06, 0xe9, 0x60, 0x7d, 0xdb, 0xcb, 0xcc, 0x2e
  2657. };
  2658. const byte test1Output[] =
  2659. {
  2660. 0xd3, 0xe1, 0x60, 0xc3, 0x5b, 0x99, 0xf3, 0x40, 0xb2, 0x62, 0x82, 0x64,
  2661. 0xd1, 0x75, 0x10, 0x60, 0xe0, 0x04, 0x5d, 0xa3, 0x83, 0xff, 0x57, 0xa5,
  2662. 0x7d, 0x73, 0xa6, 0x73, 0xd2, 0xb8, 0xd8, 0x0d, 0xaa, 0xf6, 0xa6, 0xc3,
  2663. 0x5a, 0x91, 0xbb, 0x45, 0x79, 0xd7, 0x3f, 0xd0, 0xc8, 0xfe, 0xd1, 0x11,
  2664. 0xb0, 0x39, 0x13, 0x06, 0x82, 0x8a, 0xdf, 0xed, 0x52, 0x8f, 0x01, 0x81,
  2665. 0x21, 0xb3, 0xfe, 0xbd, 0xc3, 0x43, 0xe7, 0x97, 0xb8, 0x7d, 0xbb, 0x63,
  2666. 0xdb, 0x13, 0x33, 0xde, 0xd9, 0xd1, 0xec, 0xe1, 0x77, 0xcf, 0xa6, 0xb7,
  2667. 0x1f, 0xe8, 0xab, 0x1d, 0xa4, 0x66, 0x24, 0xed, 0x64, 0x15, 0xe5, 0x1c,
  2668. 0xcd, 0xe2, 0xc7, 0xca, 0x86, 0xe2, 0x83, 0x99, 0x0e, 0xea, 0xeb, 0x91,
  2669. 0x12, 0x04, 0x15, 0x52, 0x8b, 0x22, 0x95, 0x91, 0x02, 0x81, 0xb0, 0x2d,
  2670. 0xd4, 0x31, 0xf4, 0xc9, 0xf7, 0x04, 0x27, 0xdf
  2671. };
  2672. const byte test2EntropyA[] =
  2673. {
  2674. 0x63, 0x36, 0x33, 0x77, 0xe4, 0x1e, 0x86, 0x46, 0x8d, 0xeb, 0x0a, 0xb4,
  2675. 0xa8, 0xed, 0x68, 0x3f, 0x6a, 0x13, 0x4e, 0x47, 0xe0, 0x14, 0xc7, 0x00,
  2676. 0x45, 0x4e, 0x81, 0xe9, 0x53, 0x58, 0xa5, 0x69, 0x80, 0x8a, 0xa3, 0x8f,
  2677. 0x2a, 0x72, 0xa6, 0x23, 0x59, 0x91, 0x5a, 0x9f, 0x8a, 0x04, 0xca, 0x68
  2678. };
  2679. const byte test2EntropyB[] =
  2680. {
  2681. 0xe6, 0x2b, 0x8a, 0x8e, 0xe8, 0xf1, 0x41, 0xb6, 0x98, 0x05, 0x66, 0xe3,
  2682. 0xbf, 0xe3, 0xc0, 0x49, 0x03, 0xda, 0xd4, 0xac, 0x2c, 0xdf, 0x9f, 0x22,
  2683. 0x80, 0x01, 0x0a, 0x67, 0x39, 0xbc, 0x83, 0xd3
  2684. };
  2685. const byte test2Output[] =
  2686. {
  2687. 0x04, 0xee, 0xc6, 0x3b, 0xb2, 0x31, 0xdf, 0x2c, 0x63, 0x0a, 0x1a, 0xfb,
  2688. 0xe7, 0x24, 0x94, 0x9d, 0x00, 0x5a, 0x58, 0x78, 0x51, 0xe1, 0xaa, 0x79,
  2689. 0x5e, 0x47, 0x73, 0x47, 0xc8, 0xb0, 0x56, 0x62, 0x1c, 0x18, 0xbd, 0xdc,
  2690. 0xdd, 0x8d, 0x99, 0xfc, 0x5f, 0xc2, 0xb9, 0x20, 0x53, 0xd8, 0xcf, 0xac,
  2691. 0xfb, 0x0b, 0xb8, 0x83, 0x12, 0x05, 0xfa, 0xd1, 0xdd, 0xd6, 0xc0, 0x71,
  2692. 0x31, 0x8a, 0x60, 0x18, 0xf0, 0x3b, 0x73, 0xf5, 0xed, 0xe4, 0xd4, 0xd0,
  2693. 0x71, 0xf9, 0xde, 0x03, 0xfd, 0x7a, 0xea, 0x10, 0x5d, 0x92, 0x99, 0xb8,
  2694. 0xaf, 0x99, 0xaa, 0x07, 0x5b, 0xdb, 0x4d, 0xb9, 0xaa, 0x28, 0xc1, 0x8d,
  2695. 0x17, 0x4b, 0x56, 0xee, 0x2a, 0x01, 0x4d, 0x09, 0x88, 0x96, 0xff, 0x22,
  2696. 0x82, 0xc9, 0x55, 0xa8, 0x19, 0x69, 0xe0, 0x69, 0xfa, 0x8c, 0xe0, 0x07,
  2697. 0xa1, 0x80, 0x18, 0x3a, 0x07, 0xdf, 0xae, 0x17
  2698. };
  2699. byte output[SHA256_DIGEST_SIZE * 4];
  2700. int ret;
  2701. ret = wc_RNG_HealthTest(0, test1Entropy, sizeof(test1Entropy), NULL, 0,
  2702. output, sizeof(output));
  2703. if (ret != 0)
  2704. return -39;
  2705. if (XMEMCMP(test1Output, output, sizeof(output)) != 0)
  2706. return -40;
  2707. ret = wc_RNG_HealthTest(1, test2EntropyA, sizeof(test2EntropyA),
  2708. test2EntropyB, sizeof(test2EntropyB),
  2709. output, sizeof(output));
  2710. if (ret != 0)
  2711. return -41;
  2712. if (XMEMCMP(test2Output, output, sizeof(output)) != 0)
  2713. return -42;
  2714. return 0;
  2715. }
  2716. #else /* HAVE_HASHDRBG || NO_RC4 */
  2717. int random_test(void)
  2718. {
  2719. WC_RNG rng;
  2720. byte block[32];
  2721. int ret;
  2722. #ifdef HAVE_CAVIUM
  2723. ret = wc_InitRngCavium(&rng, CAVIUM_DEV_ID);
  2724. if (ret != 0) return -2007;
  2725. #endif
  2726. ret = wc_InitRng(&rng);
  2727. if (ret != 0) return -39;
  2728. ret = wc_RNG_GenerateBlock(&rng, block, sizeof(block));
  2729. if (ret != 0) return -40;
  2730. wc_FreeRng(&rng);
  2731. return 0;
  2732. }
  2733. #endif /* HAVE_HASHDRBG || NO_RC4 */
  2734. #ifdef HAVE_NTRU
  2735. byte GetEntropy(ENTROPY_CMD cmd, byte* out);
  2736. byte GetEntropy(ENTROPY_CMD cmd, byte* out)
  2737. {
  2738. static WC_RNG rng;
  2739. if (cmd == INIT)
  2740. return (wc_InitRng(&rng) == 0) ? 1 : 0;
  2741. if (out == NULL)
  2742. return 0;
  2743. if (cmd == GET_BYTE_OF_ENTROPY)
  2744. return (wc_RNG_GenerateBlock(&rng, out, 1) == 0) ? 1 : 0;
  2745. if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
  2746. *out = 1;
  2747. return 1;
  2748. }
  2749. return 0;
  2750. }
  2751. #endif /* HAVE_NTRU */
  2752. #ifndef NO_RSA
  2753. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  2754. #ifdef FREESCALE_MQX
  2755. static const char* clientKey = "a:\\certs\\client-key.der";
  2756. static const char* clientCert = "a:\\certs\\client-cert.der";
  2757. #ifdef WOLFSSL_CERT_EXT
  2758. static const char* clientKeyPub = "a:\\certs\\client-keyPub.der";
  2759. #endif
  2760. #ifdef WOLFSSL_CERT_GEN
  2761. static const char* caKeyFile = "a:\\certs\\ca-key.der";
  2762. #ifdef WOLFSSL_CERT_EXT
  2763. static const char* caKeyPubFile = "a:\\certs\\ca-keyPub.der";
  2764. #endif
  2765. static const char* caCertFile = "a:\\certs\\ca-cert.pem";
  2766. #ifdef HAVE_ECC
  2767. static const char* eccCaKeyFile = "a:\\certs\\ecc-key.der";
  2768. #ifdef WOLFSSL_CERT_EXT
  2769. static const char* eccCaKeyPubFile = "a:\\certs\\ecc-keyPub.der";
  2770. #endif
  2771. static const char* eccCaCertFile = "a:\\certs\\server-ecc.pem";
  2772. #endif
  2773. #endif
  2774. #elif defined(WOLFSSL_MKD_SHELL)
  2775. static char* clientKey = "certs/client-key.der";
  2776. static char* clientCert = "certs/client-cert.der";
  2777. void set_clientKey(char *key) { clientKey = key ; }
  2778. void set_clientCert(char *cert) { clientCert = cert ; }
  2779. #ifdef WOLFSSL_CERT_EXT
  2780. static const char* clientKeyPub = "certs/client-keyPub.der";
  2781. void set_clientKeyPub(char *key) { clientKeyPub = key ; }
  2782. #endif
  2783. #ifdef WOLFSSL_CERT_GEN
  2784. static char* caKeyFile = "certs/ca-key.der";
  2785. #ifdef WOLFSSL_CERT_EXT
  2786. static const char* caKeyPubFile = "certs/ca-keyPub.der";
  2787. void set_caKeyPubFile (char * key) { caKeyPubFile = key ; }
  2788. #endif
  2789. static char* caCertFile = "certs/ca-cert.pem";
  2790. void set_caKeyFile (char * key) { caKeyFile = key ; }
  2791. void set_caCertFile(char * cert) { caCertFile = cert ; }
  2792. #ifdef HAVE_ECC
  2793. static const char* eccCaKeyFile = "certs/ecc-key.der";
  2794. #ifdef WOLFSSL_CERT_EXT
  2795. static const char* eccCaKeyPubFile = "certs/ecc-keyPub.der";
  2796. void set_eccCaKeyPubFile(char * key) { eccCaKeyPubFile = key ; }
  2797. #endif
  2798. static const char* eccCaCertFile = "certs/server-ecc.pem";
  2799. void set_eccCaKeyFile (char * key) { eccCaKeyFile = key ; }
  2800. void set_eccCaCertFile(char * cert) { eccCaCertFile = cert ; }
  2801. #endif
  2802. #endif
  2803. #else
  2804. static const char* clientKey = "./certs/client-key.der";
  2805. static const char* clientCert = "./certs/client-cert.der";
  2806. #ifdef WOLFSSL_CERT_EXT
  2807. static const char* clientKeyPub = "./certs/client-keyPub.der";
  2808. #endif
  2809. #ifdef WOLFSSL_CERT_GEN
  2810. static const char* caKeyFile = "./certs/ca-key.der";
  2811. static const char* caCertFile = "./certs/ca-cert.pem";
  2812. #ifdef HAVE_ECC
  2813. static const char* eccCaKeyFile = "./certs/ecc-key.der";
  2814. #ifdef WOLFSSL_CERT_EXT
  2815. static const char* eccCaKeyPubFile = "./certs/ecc-keyPub.der";
  2816. #endif
  2817. static const char* eccCaCertFile = "./certs/server-ecc.pem";
  2818. #endif
  2819. #endif
  2820. #endif
  2821. #endif
  2822. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT)
  2823. int certext_test(void)
  2824. {
  2825. DecodedCert cert;
  2826. byte* tmp;
  2827. size_t bytes;
  2828. FILE *file;
  2829. int ret;
  2830. /* created from rsa_test : othercert.der */
  2831. byte skid_rsa[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  2832. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  2833. /* created from rsa_test : othercert.der */
  2834. byte akid_rsa[] = "\x27\x8E\x67\x11\x74\xC3\x26\x1D\x3F\xED"
  2835. "\x33\x63\xB3\xA4\xD8\x1D\x30\xE5\xE8\xD5";
  2836. /* created from rsa_test : certecc.der */
  2837. byte akid_ecc[] = "\x5D\x5D\x26\xEF\xAC\x7E\x36\xF9\x9B\x76"
  2838. "\x15\x2B\x4A\x25\x02\x23\xEF\xB2\x89\x30";
  2839. /* created from rsa_test : cert.der */
  2840. byte kid_ca[] = "\x33\xD8\x45\x66\xD7\x68\x87\x18\x7E\x54"
  2841. "\x0D\x70\x27\x91\xC7\x26\xD7\x85\x65\xC0";
  2842. tmp = (byte*)malloc(FOURK_BUF);
  2843. if (tmp == NULL)
  2844. return -200;
  2845. /* load othercert.pem (Cert signed by an authority) */
  2846. #ifdef FREESCALE_MQX
  2847. file = fopen("a:\\certs\\othercert.der", "rb");
  2848. #else
  2849. file = fopen("./othercert.der", "rb");
  2850. #endif
  2851. if (!file) {
  2852. free(tmp);
  2853. return -200;
  2854. }
  2855. bytes = fread(tmp, 1, FOURK_BUF, file);
  2856. fclose(file);
  2857. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  2858. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  2859. printf("ret = %d\n", ret);
  2860. if (ret != 0)
  2861. return -201;
  2862. /* check the SKID from a RSA certificate */
  2863. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, cert.extSubjKeyIdSz))
  2864. return -202;
  2865. /* check the AKID from an RSA certificate */
  2866. if (XMEMCMP(akid_rsa, cert.extAuthKeyId, cert.extAuthKeyIdSz))
  2867. return -203;
  2868. /* check the Key Usage from an RSA certificate */
  2869. if (!cert.extKeyUsageSet)
  2870. return -204;
  2871. if (cert.extKeyUsage != (KEYUSE_KEY_ENCIPHER|KEYUSE_KEY_AGREE))
  2872. return -205;
  2873. /* check the CA Basic Constraints from an RSA certificate */
  2874. if (cert.isCA)
  2875. return -206;
  2876. /* check the Certificate Policies Id */
  2877. if (cert.extCertPoliciesNb != 1)
  2878. return -227;
  2879. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  2880. return -228;
  2881. FreeDecodedCert(&cert);
  2882. /* load certecc.pem (Cert signed by an authority) */
  2883. #ifdef FREESCALE_MQX
  2884. file = fopen("a:\\certs\\certecc.der", "rb");
  2885. #else
  2886. file = fopen("./certecc.der", "rb");
  2887. #endif
  2888. if (!file) {
  2889. free(tmp);
  2890. return -210;
  2891. }
  2892. bytes = fread(tmp, 1, FOURK_BUF, file);
  2893. fclose(file);
  2894. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  2895. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  2896. if (ret != 0)
  2897. return -211;
  2898. /* check the SKID from a ECC certificate */
  2899. if (XMEMCMP(skid_rsa, cert.extSubjKeyId, cert.extSubjKeyIdSz))
  2900. return -212;
  2901. /* check the AKID from an ECC certificate */
  2902. if (XMEMCMP(akid_ecc, cert.extAuthKeyId, cert.extAuthKeyIdSz))
  2903. return -213;
  2904. /* check the Key Usage from an ECC certificate */
  2905. if (!cert.extKeyUsageSet)
  2906. return -214;
  2907. if (cert.extKeyUsage != (KEYUSE_DIGITAL_SIG|KEYUSE_CONTENT_COMMIT))
  2908. return -215;
  2909. /* check the CA Basic Constraints from an ECC certificate */
  2910. if (cert.isCA)
  2911. return -216;
  2912. /* check the Certificate Policies Id */
  2913. if (cert.extCertPoliciesNb != 2)
  2914. return -217;
  2915. if (strncmp(cert.extCertPolicies[0], "2.4.589440.587.101.2.1.9632587.1", 32))
  2916. return -218;
  2917. if (strncmp(cert.extCertPolicies[1], "1.2.13025.489.1.113549", 22))
  2918. return -219;
  2919. FreeDecodedCert(&cert);
  2920. /* load cert.pem (self signed certificate) */
  2921. #ifdef FREESCALE_MQX
  2922. file = fopen("a:\\certs\\cert.der", "rb");
  2923. #else
  2924. file = fopen("./cert.der", "rb");
  2925. #endif
  2926. if (!file) {
  2927. free(tmp);
  2928. return -220;
  2929. }
  2930. bytes = fread(tmp, 1, FOURK_BUF, file);
  2931. fclose(file);
  2932. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  2933. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  2934. if (ret != 0)
  2935. return -221;
  2936. /* check the SKID from a CA certificate */
  2937. if (XMEMCMP(kid_ca, cert.extSubjKeyId, cert.extSubjKeyIdSz))
  2938. return -222;
  2939. /* check the AKID from an CA certificate */
  2940. if (XMEMCMP(kid_ca, cert.extAuthKeyId, cert.extAuthKeyIdSz))
  2941. return -223;
  2942. /* check the Key Usage from CA certificate */
  2943. if (!cert.extKeyUsageSet)
  2944. return -224;
  2945. if (cert.extKeyUsage != (KEYUSE_KEY_CERT_SIGN|KEYUSE_CRL_SIGN))
  2946. return -225;
  2947. /* check the CA Basic Constraints CA certificate */
  2948. if (!cert.isCA)
  2949. return -226;
  2950. /* check the Certificate Policies Id */
  2951. if (cert.extCertPoliciesNb != 2)
  2952. return -227;
  2953. if (strncmp(cert.extCertPolicies[0], "2.16.840.1.101.3.4.1.42", 23))
  2954. return -228;
  2955. if (strncmp(cert.extCertPolicies[1], "1.2.840.113549.1.9.16.6.5", 25))
  2956. return -229;
  2957. FreeDecodedCert(&cert);
  2958. return 0;
  2959. }
  2960. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_TEST_CERT) */
  2961. int rsa_test(void)
  2962. {
  2963. byte* tmp;
  2964. size_t bytes;
  2965. RsaKey key;
  2966. #ifdef WOLFSSL_CERT_EXT
  2967. RsaKey keypub;
  2968. #endif
  2969. WC_RNG rng;
  2970. word32 idx = 0;
  2971. int ret;
  2972. byte in[] = "Everyone gets Friday off.";
  2973. word32 inLen = (word32)strlen((char*)in);
  2974. byte out[256];
  2975. byte plain[256];
  2976. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  2977. FILE *file, *file2;
  2978. #endif
  2979. #ifdef WOLFSSL_TEST_CERT
  2980. DecodedCert cert;
  2981. #endif
  2982. tmp = (byte*)malloc(FOURK_BUF);
  2983. if (tmp == NULL)
  2984. return -40;
  2985. #ifdef USE_CERT_BUFFERS_1024
  2986. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  2987. bytes = sizeof_client_key_der_1024;
  2988. #elif defined(USE_CERT_BUFFERS_2048)
  2989. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  2990. bytes = sizeof_client_key_der_2048;
  2991. #else
  2992. file = fopen(clientKey, "rb");
  2993. if (!file) {
  2994. err_sys("can't open ./certs/client-key.der, "
  2995. "Please run from wolfSSL home dir", -40);
  2996. free(tmp);
  2997. return -40;
  2998. }
  2999. bytes = fread(tmp, 1, FOURK_BUF, file);
  3000. fclose(file);
  3001. #endif /* USE_CERT_BUFFERS */
  3002. #ifdef HAVE_CAVIUM
  3003. wc_RsaInitCavium(&key, CAVIUM_DEV_ID);
  3004. #endif
  3005. ret = wc_InitRsaKey(&key, 0);
  3006. if (ret != 0) {
  3007. free(tmp);
  3008. return -39;
  3009. }
  3010. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  3011. if (ret != 0) {
  3012. free(tmp);
  3013. return -41;
  3014. }
  3015. ret = wc_InitRng(&rng);
  3016. if (ret != 0) {
  3017. free(tmp);
  3018. return -42;
  3019. }
  3020. ret = wc_RsaPublicEncrypt(in, inLen, out, sizeof(out), &key, &rng);
  3021. if (ret < 0) {
  3022. free(tmp);
  3023. return -43;
  3024. }
  3025. ret = wc_RsaPrivateDecrypt(out, ret, plain, sizeof(plain), &key);
  3026. if (ret < 0) {
  3027. free(tmp);
  3028. return -44;
  3029. }
  3030. if (memcmp(plain, in, inLen)) {
  3031. free(tmp);
  3032. return -45;
  3033. }
  3034. ret = wc_RsaSSL_Sign(in, inLen, out, sizeof(out), &key, &rng);
  3035. if (ret < 0) {
  3036. free(tmp);
  3037. return -46;
  3038. }
  3039. memset(plain, 0, sizeof(plain));
  3040. ret = wc_RsaSSL_Verify(out, ret, plain, sizeof(plain), &key);
  3041. if (ret < 0) {
  3042. free(tmp);
  3043. return -47;
  3044. }
  3045. if (memcmp(plain, in, ret)) {
  3046. free(tmp);
  3047. return -48;
  3048. }
  3049. #if defined(WOLFSSL_MDK_ARM)
  3050. #define sizeof(s) strlen((char *)(s))
  3051. #endif
  3052. #ifdef USE_CERT_BUFFERS_1024
  3053. XMEMCPY(tmp, client_cert_der_1024, sizeof_client_cert_der_1024);
  3054. bytes = sizeof_client_cert_der_1024;
  3055. #elif defined(USE_CERT_BUFFERS_2048)
  3056. XMEMCPY(tmp, client_cert_der_2048, sizeof_client_cert_der_2048);
  3057. bytes = sizeof_client_cert_der_2048;
  3058. #else
  3059. file2 = fopen(clientCert, "rb");
  3060. if (!file2) {
  3061. free(tmp);
  3062. return -49;
  3063. }
  3064. bytes = fread(tmp, 1, FOURK_BUF, file2);
  3065. fclose(file2);
  3066. #endif
  3067. #ifdef sizeof
  3068. #undef sizeof
  3069. #endif
  3070. #ifdef WOLFSSL_TEST_CERT
  3071. InitDecodedCert(&cert, tmp, (word32)bytes, 0);
  3072. ret = ParseCert(&cert, CERT_TYPE, NO_VERIFY, 0);
  3073. if (ret != 0) return -491;
  3074. FreeDecodedCert(&cert);
  3075. #else
  3076. (void)bytes;
  3077. #endif
  3078. #ifdef WOLFSSL_CERT_EXT
  3079. #ifdef USE_CERT_BUFFERS_1024
  3080. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  3081. bytes = sizeof_client_keypub_der_1024;
  3082. #elif defined(USE_CERT_BUFFERS_2048)
  3083. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  3084. bytes = sizeof_client_keypub_der_2048;
  3085. #else
  3086. file = fopen(clientKeyPub, "rb");
  3087. if (!file) {
  3088. err_sys("can't open ./certs/client-keyPub.der, "
  3089. "Please run from wolfSSL home dir", -40);
  3090. free(tmp);
  3091. return -50;
  3092. }
  3093. bytes = fread(tmp, 1, FOURK_BUF, file);
  3094. fclose(file);
  3095. #endif /* USE_CERT_BUFFERS */
  3096. ret = wc_InitRsaKey(&keypub, 0);
  3097. if (ret != 0) {
  3098. free(tmp);
  3099. return -51;
  3100. }
  3101. idx = 0;
  3102. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keypub, (word32)bytes);
  3103. if (ret != 0) {
  3104. free(tmp);
  3105. wc_FreeRsaKey(&keypub);
  3106. return -52;
  3107. }
  3108. #endif /* WOLFSSL_CERT_EXT */
  3109. #ifdef WOLFSSL_KEY_GEN
  3110. {
  3111. byte* der;
  3112. byte* pem;
  3113. int derSz = 0;
  3114. int pemSz = 0;
  3115. RsaKey derIn;
  3116. RsaKey genKey;
  3117. FILE* keyFile;
  3118. FILE* pemFile;
  3119. ret = wc_InitRsaKey(&genKey, 0);
  3120. if (ret != 0) {
  3121. free(tmp);
  3122. return -300;
  3123. }
  3124. ret = wc_MakeRsaKey(&genKey, 1024, 65537, &rng);
  3125. if (ret != 0) {
  3126. free(tmp);
  3127. return -301;
  3128. }
  3129. der = (byte*)malloc(FOURK_BUF);
  3130. if (der == NULL) {
  3131. free(tmp);
  3132. wc_FreeRsaKey(&genKey);
  3133. return -307;
  3134. }
  3135. pem = (byte*)malloc(FOURK_BUF);
  3136. if (pem == NULL) {
  3137. free(tmp);
  3138. free(der);
  3139. wc_FreeRsaKey(&genKey);
  3140. return -308;
  3141. }
  3142. derSz = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  3143. if (derSz < 0) {
  3144. free(der);
  3145. free(pem);
  3146. free(tmp);
  3147. return -302;
  3148. }
  3149. #ifdef FREESCALE_MQX
  3150. keyFile = fopen("a:\\certs\\key.der", "wb");
  3151. #else
  3152. keyFile = fopen("./key.der", "wb");
  3153. #endif
  3154. if (!keyFile) {
  3155. free(der);
  3156. free(pem);
  3157. free(tmp);
  3158. wc_FreeRsaKey(&genKey);
  3159. return -303;
  3160. }
  3161. ret = (int)fwrite(der, 1, derSz, keyFile);
  3162. fclose(keyFile);
  3163. if (ret != derSz) {
  3164. free(der);
  3165. free(pem);
  3166. free(tmp);
  3167. wc_FreeRsaKey(&genKey);
  3168. return -313;
  3169. }
  3170. pemSz = wc_DerToPem(der, derSz, pem, FOURK_BUF, PRIVATEKEY_TYPE);
  3171. if (pemSz < 0) {
  3172. free(der);
  3173. free(pem);
  3174. free(tmp);
  3175. wc_FreeRsaKey(&genKey);
  3176. return -304;
  3177. }
  3178. #ifdef FREESCALE_MQX
  3179. pemFile = fopen("a:\\certs\\key.pem", "wb");
  3180. #else
  3181. pemFile = fopen("./key.pem", "wb");
  3182. #endif
  3183. if (!pemFile) {
  3184. free(der);
  3185. free(pem);
  3186. free(tmp);
  3187. wc_FreeRsaKey(&genKey);
  3188. return -305;
  3189. }
  3190. ret = (int)fwrite(pem, 1, pemSz, pemFile);
  3191. fclose(pemFile);
  3192. if (ret != pemSz) {
  3193. free(der);
  3194. free(pem);
  3195. free(tmp);
  3196. wc_FreeRsaKey(&genKey);
  3197. return -314;
  3198. }
  3199. ret = wc_InitRsaKey(&derIn, 0);
  3200. if (ret != 0) {
  3201. free(der);
  3202. free(pem);
  3203. free(tmp);
  3204. wc_FreeRsaKey(&genKey);
  3205. return -3060;
  3206. }
  3207. idx = 0;
  3208. ret = wc_RsaPrivateKeyDecode(der, &idx, &derIn, derSz);
  3209. if (ret != 0) {
  3210. free(der);
  3211. free(pem);
  3212. free(tmp);
  3213. wc_FreeRsaKey(&derIn);
  3214. wc_FreeRsaKey(&genKey);
  3215. return -306;
  3216. }
  3217. wc_FreeRsaKey(&derIn);
  3218. wc_FreeRsaKey(&genKey);
  3219. free(pem);
  3220. free(der);
  3221. }
  3222. #endif /* WOLFSSL_KEY_GEN */
  3223. #ifdef WOLFSSL_CERT_GEN
  3224. /* self signed */
  3225. {
  3226. Cert myCert;
  3227. byte* derCert;
  3228. byte* pem;
  3229. FILE* derFile;
  3230. FILE* pemFile;
  3231. int certSz;
  3232. int pemSz;
  3233. #ifdef WOLFSSL_TEST_CERT
  3234. DecodedCert decode;
  3235. #endif
  3236. derCert = (byte*)malloc(FOURK_BUF);
  3237. if (derCert == NULL) {
  3238. free(tmp);
  3239. return -309;
  3240. }
  3241. pem = (byte*)malloc(FOURK_BUF);
  3242. if (pem == NULL) {
  3243. free(tmp);
  3244. free(derCert);
  3245. return -310;
  3246. }
  3247. wc_InitCert(&myCert);
  3248. strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
  3249. strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
  3250. strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
  3251. strncpy(myCert.subject.org, "yaSSL", CTC_NAME_SIZE);
  3252. strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
  3253. strncpy(myCert.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
  3254. strncpy(myCert.subject.email, "info@yassl.com", CTC_NAME_SIZE);
  3255. myCert.isCA = 1;
  3256. myCert.sigType = CTC_SHA256wRSA;
  3257. #ifdef WOLFSSL_CERT_EXT
  3258. /* add Policies */
  3259. strncpy(myCert.certPolicies[0], "2.16.840.1.101.3.4.1.42",
  3260. CTC_MAX_CERTPOL_SZ);
  3261. strncpy(myCert.certPolicies[1], "1.2.840.113549.1.9.16.6.5",
  3262. CTC_MAX_CERTPOL_SZ);
  3263. myCert.certPoliciesNb = 2;
  3264. /* add SKID from the Public Key */
  3265. if (wc_SetSubjectKeyIdFromPublicKey(&myCert, &keypub, NULL) != 0) {
  3266. free(pem);
  3267. free(derCert);
  3268. free(tmp);
  3269. return -399;
  3270. }
  3271. /* add AKID from the Public Key */
  3272. if (wc_SetAuthKeyIdFromPublicKey(&myCert, &keypub, NULL) != 0) {
  3273. free(pem);
  3274. free(derCert);
  3275. free(tmp);
  3276. return -399;
  3277. }
  3278. /* add Key Usage */
  3279. if (wc_SetKeyUsage(&myCert,"cRLSign,keyCertSign") != 0) {
  3280. free(pem);
  3281. free(derCert);
  3282. free(tmp);
  3283. return -400;
  3284. }
  3285. #endif /* WOLFSSL_CERT_EXT */
  3286. certSz = wc_MakeSelfCert(&myCert, derCert, FOURK_BUF, &key, &rng);
  3287. if (certSz < 0) {
  3288. free(derCert);
  3289. free(pem);
  3290. free(tmp);
  3291. return -401;
  3292. }
  3293. #ifdef WOLFSSL_TEST_CERT
  3294. InitDecodedCert(&decode, derCert, certSz, 0);
  3295. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  3296. if (ret != 0) {
  3297. free(derCert);
  3298. free(pem);
  3299. free(tmp);
  3300. return -402;
  3301. }
  3302. FreeDecodedCert(&decode);
  3303. #endif
  3304. #ifdef FREESCALE_MQX
  3305. derFile = fopen("a:\\certs\\cert.der", "wb");
  3306. #else
  3307. derFile = fopen("./cert.der", "wb");
  3308. #endif
  3309. if (!derFile) {
  3310. free(derCert);
  3311. free(pem);
  3312. free(tmp);
  3313. return -403;
  3314. }
  3315. ret = (int)fwrite(derCert, 1, certSz, derFile);
  3316. fclose(derFile);
  3317. if (ret != certSz) {
  3318. free(derCert);
  3319. free(pem);
  3320. free(tmp);
  3321. return -414;
  3322. }
  3323. pemSz = wc_DerToPem(derCert, certSz, pem, FOURK_BUF, CERT_TYPE);
  3324. if (pemSz < 0) {
  3325. free(derCert);
  3326. free(pem);
  3327. free(tmp);
  3328. return -404;
  3329. }
  3330. #ifdef FREESCALE_MQX
  3331. pemFile = fopen("a:\\certs\\cert.pem", "wb");
  3332. #else
  3333. pemFile = fopen("./cert.pem", "wb");
  3334. #endif
  3335. if (!pemFile) {
  3336. free(derCert);
  3337. free(pem);
  3338. free(tmp);
  3339. return -405;
  3340. }
  3341. ret = (int)fwrite(pem, 1, pemSz, pemFile);
  3342. fclose(pemFile);
  3343. if (ret != pemSz) {
  3344. free(derCert);
  3345. free(pem);
  3346. free(tmp);
  3347. return -406;
  3348. }
  3349. free(pem);
  3350. free(derCert);
  3351. }
  3352. /* CA style */
  3353. {
  3354. RsaKey caKey;
  3355. Cert myCert;
  3356. byte* derCert;
  3357. byte* pem;
  3358. FILE* derFile;
  3359. FILE* pemFile;
  3360. int certSz;
  3361. int pemSz;
  3362. size_t bytes3;
  3363. word32 idx3 = 0;
  3364. FILE* file3 ;
  3365. #ifdef WOLFSSL_TEST_CERT
  3366. DecodedCert decode;
  3367. #endif
  3368. derCert = (byte*)malloc(FOURK_BUF);
  3369. if (derCert == NULL) {
  3370. free(tmp);
  3371. return -311;
  3372. }
  3373. pem = (byte*)malloc(FOURK_BUF);
  3374. if (pem == NULL) {
  3375. free(derCert);
  3376. free(tmp);
  3377. return -312;
  3378. }
  3379. file3 = fopen(caKeyFile, "rb");
  3380. if (!file3) {
  3381. free(derCert);
  3382. free(pem);
  3383. free(tmp);
  3384. return -412;
  3385. }
  3386. bytes3 = fread(tmp, 1, FOURK_BUF, file3);
  3387. fclose(file3);
  3388. ret = wc_InitRsaKey(&caKey, 0);
  3389. if (ret != 0) {
  3390. free(derCert);
  3391. free(pem);
  3392. free(tmp);
  3393. return -411;
  3394. }
  3395. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3);
  3396. if (ret != 0) {
  3397. free(derCert);
  3398. free(pem);
  3399. free(tmp);
  3400. wc_FreeRsaKey(&caKey);
  3401. return -413;
  3402. }
  3403. wc_InitCert(&myCert);
  3404. #ifdef NO_SHA
  3405. myCert.sigType = CTC_SHA256wRSA;
  3406. #endif
  3407. strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
  3408. strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
  3409. strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
  3410. strncpy(myCert.subject.org, "yaSSL", CTC_NAME_SIZE);
  3411. strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
  3412. strncpy(myCert.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
  3413. strncpy(myCert.subject.email, "info@yassl.com", CTC_NAME_SIZE);
  3414. #ifdef WOLFSSL_CERT_EXT
  3415. /* add Policies */
  3416. strncpy(myCert.certPolicies[0], "2.16.840.1.101.3.4.1.42",
  3417. CTC_MAX_CERTPOL_SZ);
  3418. myCert.certPoliciesNb =1;
  3419. /* add SKID from the Public Key */
  3420. if (wc_SetSubjectKeyIdFromPublicKey(&myCert, &key, NULL) != 0) {
  3421. free(pem);
  3422. free(derCert);
  3423. free(tmp);
  3424. return -399;
  3425. }
  3426. /* add AKID from the CA certificate */
  3427. if (wc_SetAuthKeyId(&myCert, caCertFile) != 0) {
  3428. free(pem);
  3429. free(derCert);
  3430. free(tmp);
  3431. return -399;
  3432. }
  3433. /* add Key Usage */
  3434. if (wc_SetKeyUsage(&myCert,"keyEncipherment,keyAgreement") != 0) {
  3435. free(pem);
  3436. free(derCert);
  3437. free(tmp);
  3438. return -400;
  3439. }
  3440. #endif /* WOLFSSL_CERT_EXT */
  3441. ret = wc_SetIssuer(&myCert, caCertFile);
  3442. if (ret < 0) {
  3443. free(derCert);
  3444. free(pem);
  3445. free(tmp);
  3446. wc_FreeRsaKey(&caKey);
  3447. return -405;
  3448. }
  3449. certSz = wc_MakeCert(&myCert, derCert, FOURK_BUF, &key, NULL, &rng);
  3450. if (certSz < 0) {
  3451. free(derCert);
  3452. free(pem);
  3453. free(tmp);
  3454. wc_FreeRsaKey(&caKey);
  3455. return -407;
  3456. }
  3457. certSz = wc_SignCert(myCert.bodySz, myCert.sigType, derCert, FOURK_BUF,
  3458. &caKey, NULL, &rng);
  3459. if (certSz < 0) {
  3460. free(derCert);
  3461. free(pem);
  3462. free(tmp);
  3463. wc_FreeRsaKey(&caKey);
  3464. return -408;
  3465. }
  3466. #ifdef WOLFSSL_TEST_CERT
  3467. InitDecodedCert(&decode, derCert, certSz, 0);
  3468. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  3469. if (ret != 0) {
  3470. free(derCert);
  3471. free(pem);
  3472. free(tmp);
  3473. wc_FreeRsaKey(&caKey);
  3474. return -409;
  3475. }
  3476. FreeDecodedCert(&decode);
  3477. #endif
  3478. #ifdef FREESCALE_MQX
  3479. derFile = fopen("a:\\certs\\othercert.der", "wb");
  3480. #else
  3481. derFile = fopen("./othercert.der", "wb");
  3482. #endif
  3483. if (!derFile) {
  3484. free(derCert);
  3485. free(pem);
  3486. free(tmp);
  3487. wc_FreeRsaKey(&caKey);
  3488. return -410;
  3489. }
  3490. ret = (int)fwrite(derCert, 1, certSz, derFile);
  3491. fclose(derFile);
  3492. if (ret != certSz) {
  3493. free(derCert);
  3494. free(pem);
  3495. free(tmp);
  3496. wc_FreeRsaKey(&caKey);
  3497. return -416;
  3498. }
  3499. pemSz = wc_DerToPem(derCert, certSz, pem, FOURK_BUF, CERT_TYPE);
  3500. if (pemSz < 0) {
  3501. free(derCert);
  3502. free(pem);
  3503. free(tmp);
  3504. wc_FreeRsaKey(&caKey);
  3505. return -411;
  3506. }
  3507. #ifdef FREESCALE_MQX
  3508. pemFile = fopen("a:\\certs\\othercert.pem", "wb");
  3509. #else
  3510. pemFile = fopen("./othercert.pem", "wb");
  3511. #endif
  3512. if (!pemFile) {
  3513. free(derCert);
  3514. free(pem);
  3515. free(tmp);
  3516. wc_FreeRsaKey(&caKey);
  3517. return -412;
  3518. }
  3519. ret = (int)fwrite(pem, 1, pemSz, pemFile);
  3520. if (ret != pemSz) {
  3521. free(derCert);
  3522. free(pem);
  3523. free(tmp);
  3524. wc_FreeRsaKey(&caKey);
  3525. return -415;
  3526. }
  3527. fclose(pemFile);
  3528. free(pem);
  3529. free(derCert);
  3530. wc_FreeRsaKey(&caKey);
  3531. }
  3532. #ifdef HAVE_ECC
  3533. /* ECC CA style */
  3534. {
  3535. ecc_key caKey;
  3536. Cert myCert;
  3537. byte* derCert;
  3538. byte* pem;
  3539. FILE* derFile;
  3540. FILE* pemFile;
  3541. int certSz;
  3542. int pemSz;
  3543. size_t bytes3;
  3544. word32 idx3 = 0;
  3545. FILE* file3;
  3546. #ifdef WOLFSSL_CERT_EXT
  3547. ecc_key caKeyPub;
  3548. #endif
  3549. #ifdef WOLFSSL_TEST_CERT
  3550. DecodedCert decode;
  3551. #endif
  3552. derCert = (byte*)malloc(FOURK_BUF);
  3553. if (derCert == NULL) {
  3554. free(tmp);
  3555. return -5311;
  3556. }
  3557. pem = (byte*)malloc(FOURK_BUF);
  3558. if (pem == NULL) {
  3559. free(derCert);
  3560. free(tmp);
  3561. return -5312;
  3562. }
  3563. file3 = fopen(eccCaKeyFile, "rb");
  3564. if (!file3) {
  3565. free(derCert);
  3566. free(pem);
  3567. free(tmp);
  3568. return -5412;
  3569. }
  3570. bytes3 = fread(tmp, 1, FOURK_BUF, file3);
  3571. fclose(file3);
  3572. wc_ecc_init(&caKey);
  3573. ret = wc_EccPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes3);
  3574. if (ret != 0) {
  3575. free(derCert);
  3576. free(pem);
  3577. free(tmp);
  3578. return -5413;
  3579. }
  3580. wc_InitCert(&myCert);
  3581. myCert.sigType = CTC_SHA256wECDSA;
  3582. strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
  3583. strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
  3584. strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
  3585. strncpy(myCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  3586. strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
  3587. strncpy(myCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  3588. strncpy(myCert.subject.email, "info@wolfssl.com", CTC_NAME_SIZE);
  3589. #ifdef WOLFSSL_CERT_EXT
  3590. /* add Policies */
  3591. strncpy(myCert.certPolicies[0], "2.4.589440.587.101.2.1.9632587.1",
  3592. CTC_MAX_CERTPOL_SZ);
  3593. strncpy(myCert.certPolicies[1], "1.2.13025.489.1.113549",
  3594. CTC_MAX_CERTPOL_SZ);
  3595. myCert.certPoliciesNb = 2;
  3596. file3 = fopen(eccCaKeyPubFile, "rb");
  3597. if (!file3) {
  3598. free(derCert);
  3599. free(pem);
  3600. free(tmp);
  3601. return -5500;
  3602. }
  3603. bytes3 = fread(tmp, 1, FOURK_BUF, file3);
  3604. fclose(file3);
  3605. wc_ecc_init(&caKeyPub);
  3606. if (ret != 0) {
  3607. free(derCert);
  3608. free(pem);
  3609. free(tmp);
  3610. return -5501;
  3611. }
  3612. idx3 = 0;
  3613. ret = wc_EccPublicKeyDecode(tmp, &idx3, &caKeyPub, (word32)bytes3);
  3614. if (ret != 0) {
  3615. free(derCert);
  3616. free(pem);
  3617. free(tmp);
  3618. wc_ecc_free(&caKeyPub);
  3619. return -5502;
  3620. }
  3621. /* add SKID from the Public Key */
  3622. if (wc_SetSubjectKeyIdFromPublicKey(&myCert, &key, NULL) != 0) {
  3623. free(pem);
  3624. free(derCert);
  3625. free(tmp);
  3626. wc_ecc_free(&caKeyPub);
  3627. return -5503;
  3628. }
  3629. /* add AKID from the Public Key */
  3630. if (wc_SetAuthKeyIdFromPublicKey(&myCert, NULL, &caKeyPub) != 0) {
  3631. free(pem);
  3632. free(derCert);
  3633. free(tmp);
  3634. wc_ecc_free(&caKeyPub);
  3635. return -5504;
  3636. }
  3637. wc_ecc_free(&caKeyPub);
  3638. /* add Key Usage */
  3639. if (wc_SetKeyUsage(&myCert,"digitalSignature,nonRepudiation") != 0) {
  3640. free(pem);
  3641. free(derCert);
  3642. free(tmp);
  3643. return -5505;
  3644. }
  3645. #endif /* WOLFSSL_CERT_EXT */
  3646. ret = wc_SetIssuer(&myCert, eccCaCertFile);
  3647. if (ret < 0) {
  3648. free(pem);
  3649. free(derCert);
  3650. wc_ecc_free(&caKey);
  3651. free(tmp);
  3652. return -5405;
  3653. }
  3654. certSz = wc_MakeCert(&myCert, derCert, FOURK_BUF, &key, NULL, &rng);
  3655. if (certSz < 0) {
  3656. free(pem);
  3657. free(derCert);
  3658. wc_ecc_free(&caKey);
  3659. free(tmp);
  3660. return -5407;
  3661. }
  3662. certSz = wc_SignCert(myCert.bodySz, myCert.sigType, derCert, FOURK_BUF,
  3663. NULL, &caKey, &rng);
  3664. if (certSz < 0) {
  3665. free(pem);
  3666. free(derCert);
  3667. wc_ecc_free(&caKey);
  3668. free(tmp);
  3669. return -5408;
  3670. }
  3671. #ifdef WOLFSSL_TEST_CERT
  3672. InitDecodedCert(&decode, derCert, certSz, 0);
  3673. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  3674. if (ret != 0) {
  3675. free(tmp);
  3676. free(pem);
  3677. free(derCert);
  3678. wc_ecc_free(&caKey);
  3679. return -5409;
  3680. }
  3681. FreeDecodedCert(&decode);
  3682. #endif
  3683. #ifdef FREESCALE_MQX
  3684. derFile = fopen("a:\\certs\\certecc.der", "wb");
  3685. #else
  3686. derFile = fopen("./certecc.der", "wb");
  3687. #endif
  3688. if (!derFile) {
  3689. free(pem);
  3690. free(derCert);
  3691. wc_ecc_free(&caKey);
  3692. free(tmp);
  3693. return -5410;
  3694. }
  3695. ret = (int)fwrite(derCert, 1, certSz, derFile);
  3696. fclose(derFile);
  3697. if (ret != certSz) {
  3698. free(pem);
  3699. free(derCert);
  3700. wc_ecc_free(&caKey);
  3701. free(tmp);
  3702. return -5414;
  3703. }
  3704. pemSz = wc_DerToPem(derCert, certSz, pem, FOURK_BUF, CERT_TYPE);
  3705. if (pemSz < 0) {
  3706. free(pem);
  3707. free(derCert);
  3708. wc_ecc_free(&caKey);
  3709. free(tmp);
  3710. return -5411;
  3711. }
  3712. #ifdef FREESCALE_MQX
  3713. pemFile = fopen("a:\\certs\\certecc.pem", "wb");
  3714. #else
  3715. pemFile = fopen("./certecc.pem", "wb");
  3716. #endif
  3717. if (!pemFile) {
  3718. free(pem);
  3719. free(derCert);
  3720. wc_ecc_free(&caKey);
  3721. free(tmp);
  3722. return -5412;
  3723. }
  3724. ret = (int)fwrite(pem, 1, pemSz, pemFile);
  3725. if (ret != pemSz) {
  3726. free(pem);
  3727. free(derCert);
  3728. wc_ecc_free(&caKey);
  3729. free(tmp);
  3730. return -5415;
  3731. }
  3732. fclose(pemFile);
  3733. free(pem);
  3734. free(derCert);
  3735. wc_ecc_free(&caKey);
  3736. }
  3737. #endif /* HAVE_ECC */
  3738. #ifdef HAVE_NTRU
  3739. {
  3740. RsaKey caKey;
  3741. Cert myCert;
  3742. byte* derCert;
  3743. byte* pem;
  3744. FILE* derFile;
  3745. FILE* pemFile;
  3746. FILE* caFile;
  3747. FILE* ntruPrivFile;
  3748. int certSz;
  3749. int pemSz;
  3750. word32 idx3 = 0;
  3751. #ifdef WOLFSSL_TEST_CERT
  3752. DecodedCert decode;
  3753. #endif
  3754. derCert = (byte*)malloc(FOURK_BUF);
  3755. if (derCert == NULL) {
  3756. free(tmp);
  3757. return -311;
  3758. }
  3759. pem = (byte*)malloc(FOURK_BUF);
  3760. if (pem == NULL) {
  3761. free(derCert);
  3762. free(tmp);
  3763. return -312;
  3764. }
  3765. byte public_key[557]; /* sized for EES401EP2 */
  3766. word16 public_key_len; /* no. of octets in public key */
  3767. byte private_key[607]; /* sized for EES401EP2 */
  3768. word16 private_key_len; /* no. of octets in private key */
  3769. DRBG_HANDLE drbg;
  3770. static uint8_t const pers_str[] = {
  3771. 'C', 'y', 'a', 'S', 'S', 'L', ' ', 't', 'e', 's', 't'
  3772. };
  3773. word32 rc = ntru_crypto_drbg_instantiate(112, pers_str,
  3774. sizeof(pers_str), GetEntropy, &drbg);
  3775. if (rc != DRBG_OK) {
  3776. free(derCert);
  3777. free(pem);
  3778. free(tmp);
  3779. return -448;
  3780. }
  3781. rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
  3782. &public_key_len, NULL,
  3783. &private_key_len, NULL);
  3784. if (rc != NTRU_OK) {
  3785. free(derCert);
  3786. free(pem);
  3787. free(tmp);
  3788. return -449;
  3789. }
  3790. rc = ntru_crypto_ntru_encrypt_keygen(drbg, NTRU_EES401EP2,
  3791. &public_key_len, public_key,
  3792. &private_key_len, private_key);
  3793. if (rc != NTRU_OK) {
  3794. free(derCert);
  3795. free(pem);
  3796. free(tmp);
  3797. return -450;
  3798. }
  3799. rc = ntru_crypto_drbg_uninstantiate(drbg);
  3800. if (rc != NTRU_OK) {
  3801. free(derCert);
  3802. free(pem);
  3803. free(tmp);
  3804. return -451;
  3805. }
  3806. caFile = fopen(caKeyFile, "rb");
  3807. if (!caFile) {
  3808. free(derCert);
  3809. free(pem);
  3810. free(tmp);
  3811. return -452;
  3812. }
  3813. bytes = fread(tmp, 1, FOURK_BUF, caFile);
  3814. fclose(caFile);
  3815. ret = wc_InitRsaKey(&caKey, 0);
  3816. if (ret != 0) {
  3817. free(derCert);
  3818. free(pem);
  3819. free(tmp);
  3820. return -453;
  3821. }
  3822. ret = wc_RsaPrivateKeyDecode(tmp, &idx3, &caKey, (word32)bytes);
  3823. if (ret != 0) {
  3824. free(derCert);
  3825. free(pem);
  3826. free(tmp);
  3827. return -454;
  3828. }
  3829. wc_InitCert(&myCert);
  3830. strncpy(myCert.subject.country, "US", CTC_NAME_SIZE);
  3831. strncpy(myCert.subject.state, "OR", CTC_NAME_SIZE);
  3832. strncpy(myCert.subject.locality, "Portland", CTC_NAME_SIZE);
  3833. strncpy(myCert.subject.org, "yaSSL", CTC_NAME_SIZE);
  3834. strncpy(myCert.subject.unit, "Development", CTC_NAME_SIZE);
  3835. strncpy(myCert.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
  3836. strncpy(myCert.subject.email, "info@yassl.com", CTC_NAME_SIZE);
  3837. #ifdef WOLFSSL_CERT_EXT
  3838. /* add SKID from the Public Key */
  3839. if (wc_SetSubjectKeyIdFromNtruPublicKey(&myCert, public_key,
  3840. public_key_len) != 0) {
  3841. free(pem);
  3842. free(derCert);
  3843. free(tmp);
  3844. return -496;
  3845. }
  3846. /* add AKID from the CA certificate */
  3847. if (wc_SetAuthKeyId(&myCert, caCertFile) != 0) {
  3848. free(pem);
  3849. free(derCert);
  3850. free(tmp);
  3851. return -495;
  3852. }
  3853. /* add Key Usage */
  3854. if (wc_SetKeyUsage(&myCert,"digitalSignature,nonRepudiation,"
  3855. "keyEncipherment,keyAgreement") != 0) {
  3856. free(pem);
  3857. free(derCert);
  3858. free(tmp);
  3859. return -494;
  3860. }
  3861. #endif /* WOLFSSL_CERT_EXT */
  3862. ret = wc_SetIssuer(&myCert, caCertFile);
  3863. if (ret < 0) {
  3864. free(derCert);
  3865. free(pem);
  3866. wc_FreeRsaKey(&caKey);
  3867. free(tmp);
  3868. return -455;
  3869. }
  3870. certSz = wc_MakeNtruCert(&myCert, derCert, FOURK_BUF, public_key,
  3871. public_key_len, &rng);
  3872. if (certSz < 0) {
  3873. free(derCert);
  3874. free(pem);
  3875. wc_FreeRsaKey(&caKey);
  3876. free(tmp);
  3877. return -456;
  3878. }
  3879. certSz = wc_SignCert(myCert.bodySz, myCert.sigType, derCert, FOURK_BUF,
  3880. &caKey, NULL, &rng);
  3881. wc_FreeRsaKey(&caKey);
  3882. if (certSz < 0) {
  3883. free(derCert);
  3884. free(pem);
  3885. free(tmp);
  3886. return -457;
  3887. }
  3888. #ifdef WOLFSSL_TEST_CERT
  3889. InitDecodedCert(&decode, derCert, certSz, 0);
  3890. ret = ParseCert(&decode, CERT_TYPE, NO_VERIFY, 0);
  3891. if (ret != 0) {
  3892. free(derCert);
  3893. free(pem);
  3894. free(tmp);
  3895. return -458;
  3896. }
  3897. FreeDecodedCert(&decode);
  3898. #endif
  3899. derFile = fopen("./ntru-cert.der", "wb");
  3900. if (!derFile) {
  3901. free(derCert);
  3902. free(pem);
  3903. free(tmp);
  3904. return -459;
  3905. }
  3906. ret = (int)fwrite(derCert, 1, certSz, derFile);
  3907. fclose(derFile);
  3908. if (ret != certSz) {
  3909. free(derCert);
  3910. free(pem);
  3911. free(tmp);
  3912. return -473;
  3913. }
  3914. pemSz = wc_DerToPem(derCert, certSz, pem, FOURK_BUF, CERT_TYPE);
  3915. if (pemSz < 0) {
  3916. free(derCert);
  3917. free(pem);
  3918. free(tmp);
  3919. return -460;
  3920. }
  3921. pemFile = fopen("./ntru-cert.pem", "wb");
  3922. if (!pemFile) {
  3923. free(derCert);
  3924. free(pem);
  3925. free(tmp);
  3926. return -461;
  3927. }
  3928. ret = (int)fwrite(pem, 1, pemSz, pemFile);
  3929. fclose(pemFile);
  3930. if (ret != pemSz) {
  3931. free(derCert);
  3932. free(pem);
  3933. free(tmp);
  3934. return -474;
  3935. }
  3936. ntruPrivFile = fopen("./ntru-key.raw", "wb");
  3937. if (!ntruPrivFile) {
  3938. free(derCert);
  3939. free(pem);
  3940. free(tmp);
  3941. return -462;
  3942. }
  3943. ret = (int)fwrite(private_key, 1, private_key_len, ntruPrivFile);
  3944. fclose(ntruPrivFile);
  3945. if (ret != private_key_len) {
  3946. free(pem);
  3947. free(derCert);
  3948. free(tmp);
  3949. return -475;
  3950. }
  3951. free(pem);
  3952. free(derCert);
  3953. }
  3954. #endif /* HAVE_NTRU */
  3955. #ifdef WOLFSSL_CERT_REQ
  3956. {
  3957. Cert req;
  3958. byte* der;
  3959. byte* pem;
  3960. int derSz;
  3961. int pemSz;
  3962. FILE* reqFile;
  3963. der = (byte*)malloc(FOURK_BUF);
  3964. if (der == NULL) {
  3965. free(tmp);
  3966. return -463;
  3967. }
  3968. pem = (byte*)malloc(FOURK_BUF);
  3969. if (pem == NULL) {
  3970. free(der);
  3971. free(tmp);
  3972. return -464;
  3973. }
  3974. wc_InitCert(&req);
  3975. req.version = 0;
  3976. req.isCA = 1;
  3977. strncpy(req.challengePw, "yassl123", CTC_NAME_SIZE);
  3978. strncpy(req.subject.country, "US", CTC_NAME_SIZE);
  3979. strncpy(req.subject.state, "OR", CTC_NAME_SIZE);
  3980. strncpy(req.subject.locality, "Portland", CTC_NAME_SIZE);
  3981. strncpy(req.subject.org, "yaSSL", CTC_NAME_SIZE);
  3982. strncpy(req.subject.unit, "Development", CTC_NAME_SIZE);
  3983. strncpy(req.subject.commonName, "www.yassl.com", CTC_NAME_SIZE);
  3984. strncpy(req.subject.email, "info@yassl.com", CTC_NAME_SIZE);
  3985. req.sigType = CTC_SHA256wRSA;
  3986. #ifdef WOLFSSL_CERT_EXT
  3987. /* add SKID from the Public Key */
  3988. if (wc_SetSubjectKeyIdFromPublicKey(&req, &keypub, NULL) != 0) {
  3989. free(pem);
  3990. free(der);
  3991. free(tmp);
  3992. return -496;
  3993. }
  3994. /* add Key Usage */
  3995. if (wc_SetKeyUsage(&req,"digitalSignature,nonRepudiation,"
  3996. "keyEncipherment,keyAgreement") != 0) {
  3997. free(pem);
  3998. free(der);
  3999. free(tmp);
  4000. return -494;
  4001. }
  4002. #endif /* WOLFSSL_CERT_EXT */
  4003. derSz = wc_MakeCertReq(&req, der, FOURK_BUF, &key, NULL);
  4004. if (derSz < 0) {
  4005. free(pem);
  4006. free(der);
  4007. free(tmp);
  4008. return -465;
  4009. }
  4010. derSz = wc_SignCert(req.bodySz, req.sigType, der, FOURK_BUF,
  4011. &key, NULL, &rng);
  4012. if (derSz < 0) {
  4013. free(pem);
  4014. free(der);
  4015. free(tmp);
  4016. return -466;
  4017. }
  4018. pemSz = wc_DerToPem(der, derSz, pem, FOURK_BUF, CERTREQ_TYPE);
  4019. if (pemSz < 0) {
  4020. free(pem);
  4021. free(der);
  4022. free(tmp);
  4023. return -467;
  4024. }
  4025. #ifdef FREESCALE_MQX
  4026. reqFile = fopen("a:\\certs\\certreq.der", "wb");
  4027. #else
  4028. reqFile = fopen("./certreq.der", "wb");
  4029. #endif
  4030. if (!reqFile) {
  4031. free(pem);
  4032. free(der);
  4033. free(tmp);
  4034. return -468;
  4035. }
  4036. ret = (int)fwrite(der, 1, derSz, reqFile);
  4037. fclose(reqFile);
  4038. if (ret != derSz) {
  4039. free(pem);
  4040. free(der);
  4041. free(tmp);
  4042. return -471;
  4043. }
  4044. #ifdef FREESCALE_MQX
  4045. reqFile = fopen("a:\\certs\\certreq.pem", "wb");
  4046. #else
  4047. reqFile = fopen("./certreq.pem", "wb");
  4048. #endif
  4049. if (!reqFile) {
  4050. free(pem);
  4051. free(der);
  4052. free(tmp);
  4053. return -469;
  4054. }
  4055. ret = (int)fwrite(pem, 1, pemSz, reqFile);
  4056. fclose(reqFile);
  4057. if (ret != pemSz) {
  4058. free(pem);
  4059. free(der);
  4060. free(tmp);
  4061. return -470;
  4062. }
  4063. free(pem);
  4064. free(der);
  4065. }
  4066. #endif /* WOLFSSL_CERT_REQ */
  4067. #endif /* WOLFSSL_CERT_GEN */
  4068. wc_FreeRsaKey(&key);
  4069. #ifdef WOLFSSL_CERT_EXT
  4070. wc_FreeRsaKey(&keypub);
  4071. #endif
  4072. #ifdef HAVE_CAVIUM
  4073. wc_RsaFreeCavium(&key);
  4074. #endif
  4075. free(tmp);
  4076. wc_FreeRng(&rng);
  4077. return 0;
  4078. }
  4079. #endif
  4080. #ifndef NO_DH
  4081. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  4082. #ifdef FREESCALE_MQX
  4083. static const char* dhKey = "a:\\certs\\dh2048.der";
  4084. #elif defined(NO_ASN)
  4085. /* don't use file, no DER parsing */
  4086. #else
  4087. static const char* dhKey = "./certs/dh2048.der";
  4088. #endif
  4089. #endif
  4090. int dh_test(void)
  4091. {
  4092. int ret;
  4093. word32 bytes;
  4094. word32 idx = 0, privSz, pubSz, privSz2, pubSz2, agreeSz, agreeSz2;
  4095. byte tmp[1024];
  4096. byte priv[256];
  4097. byte pub[256];
  4098. byte priv2[256];
  4099. byte pub2[256];
  4100. byte agree[256];
  4101. byte agree2[256];
  4102. DhKey key;
  4103. DhKey key2;
  4104. WC_RNG rng;
  4105. #ifdef USE_CERT_BUFFERS_1024
  4106. XMEMCPY(tmp, dh_key_der_1024, sizeof_dh_key_der_1024);
  4107. bytes = sizeof_dh_key_der_1024;
  4108. #elif defined(USE_CERT_BUFFERS_2048)
  4109. XMEMCPY(tmp, dh_key_der_2048, sizeof_dh_key_der_2048);
  4110. bytes = sizeof_dh_key_der_2048;
  4111. #elif defined(NO_ASN)
  4112. /* don't use file, no DER parsing */
  4113. #else
  4114. FILE* file = fopen(dhKey, "rb");
  4115. if (!file)
  4116. return -50;
  4117. bytes = (word32) fread(tmp, 1, sizeof(tmp), file);
  4118. fclose(file);
  4119. #endif /* USE_CERT_BUFFERS */
  4120. (void)idx;
  4121. (void)tmp;
  4122. (void)bytes;
  4123. wc_InitDhKey(&key);
  4124. wc_InitDhKey(&key2);
  4125. #ifdef NO_ASN
  4126. ret = wc_DhSetKey(&key, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  4127. if (ret != 0)
  4128. return -51;
  4129. ret = wc_DhSetKey(&key2, dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  4130. if (ret != 0)
  4131. return -51;
  4132. #else
  4133. ret = wc_DhKeyDecode(tmp, &idx, &key, bytes);
  4134. if (ret != 0)
  4135. return -51;
  4136. idx = 0;
  4137. ret = wc_DhKeyDecode(tmp, &idx, &key2, bytes);
  4138. if (ret != 0)
  4139. return -52;
  4140. #endif
  4141. ret = wc_InitRng(&rng);
  4142. if (ret != 0)
  4143. return -53;
  4144. ret = wc_DhGenerateKeyPair(&key, &rng, priv, &privSz, pub, &pubSz);
  4145. ret += wc_DhGenerateKeyPair(&key2, &rng, priv2, &privSz2, pub2, &pubSz2);
  4146. if (ret != 0)
  4147. return -54;
  4148. ret = wc_DhAgree(&key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  4149. ret += wc_DhAgree(&key2, agree2, &agreeSz2, priv2, privSz2, pub, pubSz);
  4150. if (ret != 0)
  4151. return -55;
  4152. if (memcmp(agree, agree2, agreeSz))
  4153. return -56;
  4154. wc_FreeDhKey(&key);
  4155. wc_FreeDhKey(&key2);
  4156. wc_FreeRng(&rng);
  4157. return 0;
  4158. }
  4159. #endif /* NO_DH */
  4160. #ifndef NO_DSA
  4161. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  4162. #ifdef FREESCALE_MQX
  4163. static const char* dsaKey = "a:\\certs\\dsa2048.der";
  4164. #else
  4165. static const char* dsaKey = "./certs/dsa2048.der";
  4166. #endif
  4167. #endif
  4168. int dsa_test(void)
  4169. {
  4170. int ret, answer;
  4171. word32 bytes;
  4172. word32 idx = 0;
  4173. byte tmp[1024];
  4174. DsaKey key;
  4175. WC_RNG rng;
  4176. Sha sha;
  4177. byte hash[SHA_DIGEST_SIZE];
  4178. byte signature[40];
  4179. #ifdef USE_CERT_BUFFERS_1024
  4180. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  4181. bytes = sizeof_dsa_key_der_1024;
  4182. #elif defined(USE_CERT_BUFFERS_2048)
  4183. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  4184. bytes = sizeof_dsa_key_der_2048;
  4185. #else
  4186. FILE* file = fopen(dsaKey, "rb");
  4187. if (!file)
  4188. return -60;
  4189. bytes = (word32) fread(tmp, 1, sizeof(tmp), file);
  4190. fclose(file);
  4191. #endif /* USE_CERT_BUFFERS */
  4192. ret = wc_InitSha(&sha);
  4193. if (ret != 0)
  4194. return -4002;
  4195. wc_ShaUpdate(&sha, tmp, bytes);
  4196. wc_ShaFinal(&sha, hash);
  4197. wc_InitDsaKey(&key);
  4198. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  4199. if (ret != 0) return -61;
  4200. ret = wc_InitRng(&rng);
  4201. if (ret != 0) return -62;
  4202. ret = wc_DsaSign(hash, signature, &key, &rng);
  4203. if (ret != 0) return -63;
  4204. ret = wc_DsaVerify(hash, signature, &key, &answer);
  4205. if (ret != 0) return -64;
  4206. if (answer != 1) return -65;
  4207. wc_FreeDsaKey(&key);
  4208. #ifdef WOLFSSL_KEY_GEN
  4209. {
  4210. byte* der;
  4211. byte* pem;
  4212. int derSz = 0;
  4213. int pemSz = 0;
  4214. DsaKey derIn;
  4215. DsaKey genKey;
  4216. FILE* keyFile;
  4217. FILE* pemFile;
  4218. wc_InitDsaKey(&genKey);
  4219. ret = wc_MakeDsaParameters(&rng, 1024, &genKey);
  4220. if (ret != 0) return -362;
  4221. ret = wc_MakeDsaKey(&rng, &genKey);
  4222. if (ret != 0) return -363;
  4223. der = (byte*)malloc(FOURK_BUF);
  4224. if (der == NULL) {
  4225. wc_FreeDsaKey(&genKey);
  4226. return -364;
  4227. }
  4228. pem = (byte*)malloc(FOURK_BUF);
  4229. if (pem == NULL) {
  4230. free(der);
  4231. wc_FreeDsaKey(&genKey);
  4232. return -365;
  4233. }
  4234. derSz = wc_DsaKeyToDer(&genKey, der, FOURK_BUF);
  4235. if (derSz < 0) {
  4236. free(der);
  4237. free(pem);
  4238. return -366;
  4239. }
  4240. #ifdef FREESCALE_MQX
  4241. keyFile = fopen("a:\\certs\\key.der", "wb");
  4242. #else
  4243. keyFile = fopen("./key.der", "wb");
  4244. #endif
  4245. if (!keyFile) {
  4246. free(der);
  4247. free(pem);
  4248. wc_FreeDsaKey(&genKey);
  4249. return -367;
  4250. }
  4251. ret = (int)fwrite(der, 1, derSz, keyFile);
  4252. fclose(keyFile);
  4253. if (ret != derSz) {
  4254. free(der);
  4255. free(pem);
  4256. wc_FreeDsaKey(&genKey);
  4257. return -368;
  4258. }
  4259. pemSz = wc_DerToPem(der, derSz, pem, FOURK_BUF, DSA_PRIVATEKEY_TYPE);
  4260. if (pemSz < 0) {
  4261. free(der);
  4262. free(pem);
  4263. wc_FreeDsaKey(&genKey);
  4264. return -369;
  4265. }
  4266. #ifdef FREESCALE_MQX
  4267. pemFile = fopen("a:\\certs\\key.pem", "wb");
  4268. #else
  4269. pemFile = fopen("./key.pem", "wb");
  4270. #endif
  4271. if (!pemFile) {
  4272. free(der);
  4273. free(pem);
  4274. wc_FreeDsaKey(&genKey);
  4275. return -370;
  4276. }
  4277. ret = (int)fwrite(pem, 1, pemSz, pemFile);
  4278. fclose(pemFile);
  4279. if (ret != pemSz) {
  4280. free(der);
  4281. free(pem);
  4282. wc_FreeDsaKey(&genKey);
  4283. return -371;
  4284. }
  4285. wc_InitDsaKey(&derIn);
  4286. idx = 0;
  4287. ret = wc_DsaPrivateKeyDecode(der, &idx, &derIn, derSz);
  4288. if (ret != 0) {
  4289. free(der);
  4290. free(pem);
  4291. wc_FreeDsaKey(&derIn);
  4292. wc_FreeDsaKey(&genKey);
  4293. return -373;
  4294. }
  4295. wc_FreeDsaKey(&derIn);
  4296. wc_FreeDsaKey(&genKey);
  4297. free(pem);
  4298. free(der);
  4299. }
  4300. #endif /* WOLFSSL_KEY_GEN */
  4301. wc_FreeRng(&rng);
  4302. return 0;
  4303. }
  4304. #endif /* NO_DSA */
  4305. #ifdef WOLFCRYPT_HAVE_SRP
  4306. int srp_test(void)
  4307. {
  4308. Srp cli, srv;
  4309. int r;
  4310. byte clientPubKey[80]; /* A */
  4311. byte serverPubKey[80]; /* B */
  4312. word32 clientPubKeySz = 80;
  4313. word32 serverPubKeySz = 80;
  4314. byte clientProof[SRP_MAX_DIGEST_SIZE]; /* M1 */
  4315. byte serverProof[SRP_MAX_DIGEST_SIZE]; /* M2 */
  4316. word32 clientProofSz = SRP_MAX_DIGEST_SIZE;
  4317. word32 serverProofSz = SRP_MAX_DIGEST_SIZE;
  4318. byte username[] = "user";
  4319. word32 usernameSz = 4;
  4320. byte password[] = "password";
  4321. word32 passwordSz = 8;
  4322. byte N[] = {
  4323. 0xC9, 0x4D, 0x67, 0xEB, 0x5B, 0x1A, 0x23, 0x46, 0xE8, 0xAB, 0x42, 0x2F,
  4324. 0xC6, 0xA0, 0xED, 0xAE, 0xDA, 0x8C, 0x7F, 0x89, 0x4C, 0x9E, 0xEE, 0xC4,
  4325. 0x2F, 0x9E, 0xD2, 0x50, 0xFD, 0x7F, 0x00, 0x46, 0xE5, 0xAF, 0x2C, 0xF7,
  4326. 0x3D, 0x6B, 0x2F, 0xA2, 0x6B, 0xB0, 0x80, 0x33, 0xDA, 0x4D, 0xE3, 0x22,
  4327. 0xE1, 0x44, 0xE7, 0xA8, 0xE9, 0xB1, 0x2A, 0x0E, 0x46, 0x37, 0xF6, 0x37,
  4328. 0x1F, 0x34, 0xA2, 0x07, 0x1C, 0x4B, 0x38, 0x36, 0xCB, 0xEE, 0xAB, 0x15,
  4329. 0x03, 0x44, 0x60, 0xFA, 0xA7, 0xAD, 0xF4, 0x83
  4330. };
  4331. byte g[] = {
  4332. 0x02
  4333. };
  4334. byte salt[] = {
  4335. 0xB2, 0xE5, 0x8E, 0xCC, 0xD0, 0xCF, 0x9D, 0x10, 0x3A, 0x56
  4336. };
  4337. byte verifier[] = {
  4338. 0x7C, 0xAB, 0x17, 0xFE, 0x54, 0x3E, 0x8C, 0x13, 0xF2, 0x3D, 0x21, 0xE7,
  4339. 0xD2, 0xAF, 0xAF, 0xDB, 0xA1, 0x52, 0x69, 0x9D, 0x49, 0x01, 0x79, 0x91,
  4340. 0xCF, 0xD1, 0x3F, 0xE5, 0x28, 0x72, 0xCA, 0xBE, 0x13, 0xD1, 0xC2, 0xDA,
  4341. 0x65, 0x34, 0x55, 0x8F, 0x34, 0x0E, 0x05, 0xB8, 0xB4, 0x0F, 0x7F, 0x6B,
  4342. 0xBB, 0xB0, 0x6B, 0x50, 0xD8, 0xB1, 0xCC, 0xB7, 0x81, 0xFE, 0xD4, 0x42,
  4343. 0xF5, 0x11, 0xBC, 0x8A, 0x28, 0xEB, 0x50, 0xB3, 0x46, 0x08, 0xBA, 0x24,
  4344. 0xA2, 0xFB, 0x7F, 0x2E, 0x0A, 0xA5, 0x33, 0xCC
  4345. };
  4346. /* client knows username and password. */
  4347. /* server knows N, g, salt and verifier. */
  4348. r = wc_SrpInit(&cli, SRP_TYPE_SHA, SRP_CLIENT_SIDE);
  4349. if (!r) r = wc_SrpSetUsername(&cli, username, usernameSz);
  4350. /* client sends username to server */
  4351. if (!r) r = wc_SrpInit(&srv, SRP_TYPE_SHA, SRP_SERVER_SIDE);
  4352. if (!r) r = wc_SrpSetUsername(&srv, username, usernameSz);
  4353. if (!r) r = wc_SrpSetParams(&srv, N, sizeof(N),
  4354. g, sizeof(g),
  4355. salt, sizeof(salt));
  4356. if (!r) r = wc_SrpSetVerifier(&srv, verifier, sizeof(verifier));
  4357. if (!r) r = wc_SrpGetPublic(&srv, serverPubKey, &serverPubKeySz);
  4358. /* server sends N, g, salt and B to client */
  4359. if (!r) r = wc_SrpSetParams(&cli, N, sizeof(N),
  4360. g, sizeof(g),
  4361. salt, sizeof(salt));
  4362. if (!r) r = wc_SrpSetPassword(&cli, password, passwordSz);
  4363. if (!r) r = wc_SrpGetPublic(&cli, clientPubKey, &clientPubKeySz);
  4364. if (!r) r = wc_SrpComputeKey(&cli, clientPubKey, clientPubKeySz,
  4365. serverPubKey, serverPubKeySz);
  4366. if (!r) r = wc_SrpGetProof(&cli, clientProof, &clientProofSz);
  4367. /* client sends A and M1 to server */
  4368. if (!r) r = wc_SrpComputeKey(&srv, clientPubKey, clientPubKeySz,
  4369. serverPubKey, serverPubKeySz);
  4370. if (!r) r = wc_SrpVerifyPeersProof(&srv, clientProof, clientProofSz);
  4371. if (!r) r = wc_SrpGetProof(&srv, serverProof, &serverProofSz);
  4372. /* server sends M2 to client */
  4373. if (!r) r = wc_SrpVerifyPeersProof(&cli, serverProof, serverProofSz);
  4374. wc_SrpTerm(&cli);
  4375. wc_SrpTerm(&srv);
  4376. return r;
  4377. }
  4378. #endif /* WOLFCRYPT_HAVE_SRP */
  4379. #ifdef OPENSSL_EXTRA
  4380. int openssl_test(void)
  4381. {
  4382. EVP_MD_CTX md_ctx;
  4383. testVector a, b, c, d, e, f;
  4384. byte hash[SHA256_DIGEST_SIZE*2]; /* max size */
  4385. (void)a;
  4386. (void)b;
  4387. (void)c;
  4388. (void)e;
  4389. (void)f;
  4390. #ifndef NO_MD5
  4391. a.input = "1234567890123456789012345678901234567890123456789012345678"
  4392. "9012345678901234567890";
  4393. a.output = "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  4394. "\x7a";
  4395. a.inLen = strlen(a.input);
  4396. a.outLen = MD5_DIGEST_SIZE;
  4397. EVP_MD_CTX_init(&md_ctx);
  4398. EVP_DigestInit(&md_ctx, EVP_md5());
  4399. EVP_DigestUpdate(&md_ctx, a.input, (unsigned long)a.inLen);
  4400. EVP_DigestFinal(&md_ctx, hash, 0);
  4401. if (memcmp(hash, a.output, MD5_DIGEST_SIZE) != 0)
  4402. return -71;
  4403. #endif /* NO_MD5 */
  4404. #ifndef NO_SHA
  4405. b.input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  4406. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  4407. "aaaaaaaaaa";
  4408. b.output = "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  4409. "\x53\x99\x5E\x26\xA0";
  4410. b.inLen = strlen(b.input);
  4411. b.outLen = SHA_DIGEST_SIZE;
  4412. EVP_MD_CTX_init(&md_ctx);
  4413. EVP_DigestInit(&md_ctx, EVP_sha1());
  4414. EVP_DigestUpdate(&md_ctx, b.input, (unsigned long)b.inLen);
  4415. EVP_DigestFinal(&md_ctx, hash, 0);
  4416. if (memcmp(hash, b.output, SHA_DIGEST_SIZE) != 0)
  4417. return -72;
  4418. #endif /* NO_SHA */
  4419. d.input = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  4420. d.output = "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  4421. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  4422. "\x06\xC1";
  4423. d.inLen = strlen(d.input);
  4424. d.outLen = SHA256_DIGEST_SIZE;
  4425. EVP_MD_CTX_init(&md_ctx);
  4426. EVP_DigestInit(&md_ctx, EVP_sha256());
  4427. EVP_DigestUpdate(&md_ctx, d.input, (unsigned long)d.inLen);
  4428. EVP_DigestFinal(&md_ctx, hash, 0);
  4429. if (memcmp(hash, d.output, SHA256_DIGEST_SIZE) != 0)
  4430. return -78;
  4431. #ifdef WOLFSSL_SHA384
  4432. e.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  4433. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  4434. e.output = "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  4435. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  4436. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  4437. "\x74\x60\x39";
  4438. e.inLen = strlen(e.input);
  4439. e.outLen = SHA384_DIGEST_SIZE;
  4440. EVP_MD_CTX_init(&md_ctx);
  4441. EVP_DigestInit(&md_ctx, EVP_sha384());
  4442. EVP_DigestUpdate(&md_ctx, e.input, (unsigned long)e.inLen);
  4443. EVP_DigestFinal(&md_ctx, hash, 0);
  4444. if (memcmp(hash, e.output, SHA384_DIGEST_SIZE) != 0)
  4445. return -79;
  4446. #endif /* WOLFSSL_SHA384 */
  4447. #ifdef WOLFSSL_SHA512
  4448. f.input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  4449. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
  4450. f.output = "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  4451. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  4452. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  4453. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  4454. "\x87\x4b\xe9\x09";
  4455. f.inLen = strlen(f.input);
  4456. f.outLen = SHA512_DIGEST_SIZE;
  4457. EVP_MD_CTX_init(&md_ctx);
  4458. EVP_DigestInit(&md_ctx, EVP_sha512());
  4459. EVP_DigestUpdate(&md_ctx, f.input, (unsigned long)f.inLen);
  4460. EVP_DigestFinal(&md_ctx, hash, 0);
  4461. if (memcmp(hash, f.output, SHA512_DIGEST_SIZE) != 0)
  4462. return -80;
  4463. #endif /* WOLFSSL_SHA512 */
  4464. #ifndef NO_MD5
  4465. if (RAND_bytes(hash, sizeof(hash)) != 1)
  4466. return -73;
  4467. c.input = "what do ya want for nothing?";
  4468. c.output = "\x55\x78\xe8\x48\x4b\xcc\x93\x80\x93\xec\x53\xaf\x22\xd6\x14"
  4469. "\x76";
  4470. c.inLen = strlen(c.input);
  4471. c.outLen = MD5_DIGEST_SIZE;
  4472. HMAC(EVP_md5(),
  4473. "JefeJefeJefeJefe", 16, (byte*)c.input, (int)c.inLen, hash, 0);
  4474. if (memcmp(hash, c.output, MD5_DIGEST_SIZE) != 0)
  4475. return -74;
  4476. #endif /* NO_MD5 */
  4477. #ifndef NO_DES3
  4478. { /* des test */
  4479. const byte vector[] = { /* "now is the time for all " w/o trailing 0 */
  4480. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  4481. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  4482. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  4483. };
  4484. byte plain[24];
  4485. byte cipher[24];
  4486. const_DES_cblock key =
  4487. {
  4488. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  4489. };
  4490. DES_cblock iv =
  4491. {
  4492. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
  4493. };
  4494. DES_key_schedule sched;
  4495. const byte verify[] =
  4496. {
  4497. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  4498. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  4499. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  4500. };
  4501. DES_key_sched(&key, &sched);
  4502. DES_cbc_encrypt(vector, cipher, sizeof(vector), &sched, &iv, DES_ENCRYPT);
  4503. DES_cbc_encrypt(cipher, plain, sizeof(vector), &sched, &iv, DES_DECRYPT);
  4504. if (memcmp(plain, vector, sizeof(vector)) != 0)
  4505. return -75;
  4506. if (memcmp(cipher, verify, sizeof(verify)) != 0)
  4507. return -76;
  4508. /* test changing iv */
  4509. DES_ncbc_encrypt(vector, cipher, 8, &sched, &iv, DES_ENCRYPT);
  4510. DES_ncbc_encrypt(vector + 8, cipher + 8, 16, &sched, &iv, DES_ENCRYPT);
  4511. if (memcmp(cipher, verify, sizeof(verify)) != 0)
  4512. return -77;
  4513. } /* end des test */
  4514. #endif /* NO_DES3 */
  4515. #ifndef NO_AES
  4516. { /* evp_cipher test */
  4517. EVP_CIPHER_CTX ctx;
  4518. const byte msg[] = { /* "Now is the time for all " w/o trailing 0 */
  4519. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  4520. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  4521. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  4522. };
  4523. const byte verify[] =
  4524. {
  4525. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  4526. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  4527. };
  4528. byte key[] = "0123456789abcdef "; /* align */
  4529. byte iv[] = "1234567890abcdef "; /* align */
  4530. byte cipher[AES_BLOCK_SIZE * 4];
  4531. byte plain [AES_BLOCK_SIZE * 4];
  4532. EVP_CIPHER_CTX_init(&ctx);
  4533. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 1) == 0)
  4534. return -81;
  4535. if (EVP_Cipher(&ctx, cipher, (byte*)msg, 16) == 0)
  4536. return -82;
  4537. if (memcmp(cipher, verify, AES_BLOCK_SIZE))
  4538. return -83;
  4539. EVP_CIPHER_CTX_init(&ctx);
  4540. if (EVP_CipherInit(&ctx, EVP_aes_128_cbc(), key, iv, 0) == 0)
  4541. return -84;
  4542. if (EVP_Cipher(&ctx, plain, cipher, 16) == 0)
  4543. return -85;
  4544. if (memcmp(plain, msg, AES_BLOCK_SIZE))
  4545. return -86;
  4546. } /* end evp_cipher test */
  4547. #endif /* NO_AES */
  4548. return 0;
  4549. }
  4550. #endif /* OPENSSL_EXTRA */
  4551. #ifndef NO_PWDBASED
  4552. int pkcs12_test(void)
  4553. {
  4554. const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
  4555. 0x00, 0x00 };
  4556. const byte salt[] = { 0x0a, 0x58, 0xCF, 0x64, 0x53, 0x0d, 0x82, 0x3f };
  4557. const byte passwd2[] = { 0x00, 0x71, 0x00, 0x75, 0x00, 0x65, 0x00, 0x65,
  4558. 0x00, 0x67, 0x00, 0x00 };
  4559. const byte salt2[] = { 0x16, 0x82, 0xC0, 0xfC, 0x5b, 0x3f, 0x7e, 0xc5 };
  4560. byte derived[64];
  4561. const byte verify[] = {
  4562. 0x27, 0xE9, 0x0D, 0x7E, 0xD5, 0xA1, 0xC4, 0x11,
  4563. 0xBA, 0x87, 0x8B, 0xC0, 0x90, 0xF5, 0xCE, 0xBE,
  4564. 0x5E, 0x9D, 0x5F, 0xE3, 0xD6, 0x2B, 0x73, 0xAA
  4565. };
  4566. const byte verify2[] = {
  4567. 0x90, 0x1B, 0x49, 0x70, 0xF0, 0x94, 0xF0, 0xF8,
  4568. 0x45, 0xC0, 0xF3, 0xF3, 0x13, 0x59, 0x18, 0x6A,
  4569. 0x35, 0xE3, 0x67, 0xFE, 0xD3, 0x21, 0xFD, 0x7C
  4570. };
  4571. int id = 1;
  4572. int kLen = 24;
  4573. int iterations = 1;
  4574. int ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
  4575. iterations, kLen, SHA256, id);
  4576. if (ret < 0)
  4577. return -103;
  4578. if ( (ret = memcmp(derived, verify, kLen)) != 0)
  4579. return -104;
  4580. iterations = 1000;
  4581. ret = wc_PKCS12_PBKDF(derived, passwd2, sizeof(passwd2), salt2, 8,
  4582. iterations, kLen, SHA256, id);
  4583. if (ret < 0)
  4584. return -105;
  4585. if ( (ret = memcmp(derived, verify2, 24)) != 0)
  4586. return -106;
  4587. return 0;
  4588. }
  4589. int pbkdf2_test(void)
  4590. {
  4591. char passwd[] = "passwordpassword";
  4592. const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  4593. int iterations = 2048;
  4594. int kLen = 24;
  4595. byte derived[64];
  4596. const byte verify[] = {
  4597. 0x43, 0x6d, 0xb5, 0xe8, 0xd0, 0xfb, 0x3f, 0x35, 0x42, 0x48, 0x39, 0xbc,
  4598. 0x2d, 0xd4, 0xf9, 0x37, 0xd4, 0x95, 0x16, 0xa7, 0x2a, 0x9a, 0x21, 0xd1
  4599. };
  4600. int ret = wc_PBKDF2(derived, (byte*)passwd, (int)strlen(passwd), salt, 8,
  4601. iterations, kLen, SHA256);
  4602. if (ret != 0)
  4603. return ret;
  4604. if (memcmp(derived, verify, sizeof(verify)) != 0)
  4605. return -102;
  4606. return 0;
  4607. }
  4608. #ifndef NO_SHA
  4609. int pbkdf1_test(void)
  4610. {
  4611. char passwd[] = "password";
  4612. const byte salt[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  4613. int iterations = 1000;
  4614. int kLen = 16;
  4615. byte derived[16];
  4616. const byte verify[] = {
  4617. 0xDC, 0x19, 0x84, 0x7E, 0x05, 0xC6, 0x4D, 0x2F, 0xAF, 0x10, 0xEB, 0xFB,
  4618. 0x4A, 0x3D, 0x2A, 0x20
  4619. };
  4620. wc_PBKDF1(derived, (byte*)passwd, (int)strlen(passwd), salt, 8, iterations,
  4621. kLen, SHA);
  4622. if (memcmp(derived, verify, sizeof(verify)) != 0)
  4623. return -101;
  4624. return 0;
  4625. }
  4626. #endif
  4627. int pwdbased_test(void)
  4628. {
  4629. int ret = 0;
  4630. #ifndef NO_SHA
  4631. ret += pbkdf1_test();
  4632. #endif
  4633. ret += pbkdf2_test();
  4634. return ret + pkcs12_test();
  4635. }
  4636. #endif /* NO_PWDBASED */
  4637. #if defined(HAVE_HKDF) && (!defined(NO_SHA) || !defined(NO_SHA256))
  4638. int hkdf_test(void)
  4639. {
  4640. int ret;
  4641. int L = 42;
  4642. byte okm1[42];
  4643. byte ikm1[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  4644. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  4645. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  4646. byte salt1[13] ={ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  4647. 0x08, 0x09, 0x0a, 0x0b, 0x0c };
  4648. byte info1[10] ={ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
  4649. 0xf8, 0xf9 };
  4650. byte res1[42] = { 0x0a, 0xc1, 0xaf, 0x70, 0x02, 0xb3, 0xd7, 0x61,
  4651. 0xd1, 0xe5, 0x52, 0x98, 0xda, 0x9d, 0x05, 0x06,
  4652. 0xb9, 0xae, 0x52, 0x05, 0x72, 0x20, 0xa3, 0x06,
  4653. 0xe0, 0x7b, 0x6b, 0x87, 0xe8, 0xdf, 0x21, 0xd0,
  4654. 0xea, 0x00, 0x03, 0x3d, 0xe0, 0x39, 0x84, 0xd3,
  4655. 0x49, 0x18 };
  4656. byte res2[42] = { 0x08, 0x5a, 0x01, 0xea, 0x1b, 0x10, 0xf3, 0x69,
  4657. 0x33, 0x06, 0x8b, 0x56, 0xef, 0xa5, 0xad, 0x81,
  4658. 0xa4, 0xf1, 0x4b, 0x82, 0x2f, 0x5b, 0x09, 0x15,
  4659. 0x68, 0xa9, 0xcd, 0xd4, 0xf1, 0x55, 0xfd, 0xa2,
  4660. 0xc2, 0x2e, 0x42, 0x24, 0x78, 0xd3, 0x05, 0xf3,
  4661. 0xf8, 0x96 };
  4662. byte res3[42] = { 0x8d, 0xa4, 0xe7, 0x75, 0xa5, 0x63, 0xc1, 0x8f,
  4663. 0x71, 0x5f, 0x80, 0x2a, 0x06, 0x3c, 0x5a, 0x31,
  4664. 0xb8, 0xa1, 0x1f, 0x5c, 0x5e, 0xe1, 0x87, 0x9e,
  4665. 0xc3, 0x45, 0x4e, 0x5f, 0x3c, 0x73, 0x8d, 0x2d,
  4666. 0x9d, 0x20, 0x13, 0x95, 0xfa, 0xa4, 0xb6, 0x1a,
  4667. 0x96, 0xc8 };
  4668. byte res4[42] = { 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
  4669. 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
  4670. 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
  4671. 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
  4672. 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
  4673. 0x58, 0x65 };
  4674. (void)res1;
  4675. (void)res2;
  4676. (void)res3;
  4677. (void)res4;
  4678. (void)salt1;
  4679. (void)info1;
  4680. #ifndef NO_SHA
  4681. ret = wc_HKDF(SHA, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
  4682. if (ret != 0)
  4683. return -2001;
  4684. if (memcmp(okm1, res1, L) != 0)
  4685. return -2002;
  4686. #ifndef HAVE_FIPS
  4687. /* fips can't have key size under 14 bytes, salt is key too */
  4688. ret = wc_HKDF(SHA, ikm1, 11, salt1, 13, info1, 10, okm1, L);
  4689. if (ret != 0)
  4690. return -2003;
  4691. if (memcmp(okm1, res2, L) != 0)
  4692. return -2004;
  4693. #endif /* HAVE_FIPS */
  4694. #endif /* NO_SHA */
  4695. #ifndef NO_SHA256
  4696. ret = wc_HKDF(SHA256, ikm1, 22, NULL, 0, NULL, 0, okm1, L);
  4697. if (ret != 0)
  4698. return -2005;
  4699. if (memcmp(okm1, res3, L) != 0)
  4700. return -2006;
  4701. #ifndef HAVE_FIPS
  4702. /* fips can't have key size under 14 bytes, salt is key too */
  4703. ret = wc_HKDF(SHA256, ikm1, 22, salt1, 13, info1, 10, okm1, L);
  4704. if (ret != 0)
  4705. return -2007;
  4706. if (memcmp(okm1, res4, L) != 0)
  4707. return -2007;
  4708. #endif /* HAVE_FIPS */
  4709. #endif /* NO_SHA256 */
  4710. return 0;
  4711. }
  4712. #endif /* HAVE_HKDF */
  4713. #ifdef HAVE_ECC
  4714. typedef struct rawEccVector {
  4715. const char* msg;
  4716. const char* Qx;
  4717. const char* Qy;
  4718. const char* d;
  4719. const char* R;
  4720. const char* S;
  4721. const char* curveName;
  4722. size_t msgLen;
  4723. } rawEccVector;
  4724. int ecc_test(void)
  4725. {
  4726. WC_RNG rng;
  4727. byte sharedA[1024];
  4728. byte sharedB[1024];
  4729. byte sig[1024];
  4730. byte digest[20];
  4731. byte exportBuf[1024];
  4732. word32 x, y;
  4733. int i, verify, ret;
  4734. ecc_key userA, userB, pubKey;
  4735. ret = wc_InitRng(&rng);
  4736. if (ret != 0)
  4737. return -1001;
  4738. wc_ecc_init(&userA);
  4739. wc_ecc_init(&userB);
  4740. wc_ecc_init(&pubKey);
  4741. ret = wc_ecc_make_key(&rng, 32, &userA);
  4742. if (ret != 0)
  4743. return -1014;
  4744. ret = wc_ecc_check_key(&userA);
  4745. if (ret != 0)
  4746. return -1024;
  4747. ret = wc_ecc_make_key(&rng, 32, &userB);
  4748. if (ret != 0)
  4749. return -1002;
  4750. x = sizeof(sharedA);
  4751. ret = wc_ecc_shared_secret(&userA, &userB, sharedA, &x);
  4752. if (ret != 0)
  4753. return -1015;
  4754. y = sizeof(sharedB);
  4755. ret = wc_ecc_shared_secret(&userB, &userA, sharedB, &y);
  4756. if (ret != 0)
  4757. return -1003;
  4758. if (y != x)
  4759. return -1004;
  4760. if (memcmp(sharedA, sharedB, x))
  4761. return -1005;
  4762. x = sizeof(exportBuf);
  4763. ret = wc_ecc_export_x963(&userA, exportBuf, &x);
  4764. if (ret != 0)
  4765. return -1006;
  4766. ret = wc_ecc_import_x963(exportBuf, x, &pubKey);
  4767. if (ret != 0)
  4768. return -1007;
  4769. y = sizeof(sharedB);
  4770. ret = wc_ecc_shared_secret(&userB, &pubKey, sharedB, &y);
  4771. if (ret != 0)
  4772. return -1008;
  4773. if (memcmp(sharedA, sharedB, y))
  4774. return -1009;
  4775. #ifdef HAVE_COMP_KEY
  4776. /* try compressed export / import too */
  4777. x = sizeof(exportBuf);
  4778. ret = wc_ecc_export_x963_ex(&userA, exportBuf, &x, 1);
  4779. if (ret != 0)
  4780. return -1010;
  4781. wc_ecc_free(&pubKey);
  4782. wc_ecc_init(&pubKey);
  4783. ret = wc_ecc_import_x963(exportBuf, x, &pubKey);
  4784. if (ret != 0)
  4785. return -1011;
  4786. #endif
  4787. y = sizeof(sharedB);
  4788. ret = wc_ecc_shared_secret(&userB, &pubKey, sharedB, &y);
  4789. if (ret != 0)
  4790. return -1012;
  4791. if (memcmp(sharedA, sharedB, y))
  4792. return -1013;
  4793. /* test DSA sign hash */
  4794. for (i = 0; i < (int)sizeof(digest); i++)
  4795. digest[i] = (byte)i;
  4796. x = sizeof(sig);
  4797. ret = wc_ecc_sign_hash(digest, sizeof(digest), sig, &x, &rng, &userA);
  4798. if (ret != 0)
  4799. return -1014;
  4800. verify = 0;
  4801. ret = wc_ecc_verify_hash(sig, x, digest, sizeof(digest), &verify, &userA);
  4802. if (ret != 0)
  4803. return -1015;
  4804. if (verify != 1)
  4805. return -1016;
  4806. x = sizeof(exportBuf);
  4807. ret = wc_ecc_export_private_only(&userA, exportBuf, &x);
  4808. if (ret != 0)
  4809. return -1017;
  4810. #if !defined(NO_SHA) && \
  4811. ((defined(HAVE_ECC192) && defined(HAVE_ECC224)) || defined(HAVE_ALL_CURVES))
  4812. {
  4813. /* test raw ECC key import */
  4814. Sha sha;
  4815. byte hash[SHA_DIGEST_SIZE];
  4816. rawEccVector a, b;
  4817. rawEccVector test_ecc[2];
  4818. int times = sizeof(test_ecc) / sizeof(rawEccVector);
  4819. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  4820. a.msg = "\xeb\xf7\x48\xd7\x48\xeb\xbc\xa7\xd2\x9f\xb4\x73\x69\x8a"
  4821. "\x6e\x6b\x4f\xb1\x0c\x86\x5d\x4a\xf0\x24\xcc\x39\xae\x3d"
  4822. "\xf3\x46\x4b\xa4\xf1\xd6\xd4\x0f\x32\xbf\x96\x18\xa9\x1b"
  4823. "\xb5\x98\x6f\xa1\xa2\xaf\x04\x8a\x0e\x14\xdc\x51\xe5\x26"
  4824. "\x7e\xb0\x5e\x12\x7d\x68\x9d\x0a\xc6\xf1\xa7\xf1\x56\xce"
  4825. "\x06\x63\x16\xb9\x71\xcc\x7a\x11\xd0\xfd\x7a\x20\x93\xe2"
  4826. "\x7c\xf2\xd0\x87\x27\xa4\xe6\x74\x8c\xc3\x2f\xd5\x9c\x78"
  4827. "\x10\xc5\xb9\x01\x9d\xf2\x1c\xdc\xc0\xbc\xa4\x32\xc0\xa3"
  4828. "\xee\xd0\x78\x53\x87\x50\x88\x77\x11\x43\x59\xce\xe4\xa0"
  4829. "\x71\xcf";
  4830. a.msgLen = 128;
  4831. a.Qx = "07008ea40b08dbe76432096e80a2494c94982d2d5bcf98e6";
  4832. a.Qy = "76fab681d00b414ea636ba215de26d98c41bd7f2e4d65477";
  4833. a.d = "e14f37b3d1374ff8b03f41b9b3fdd2f0ebccf275d660d7f3";
  4834. a.R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  4835. a.S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  4836. a.curveName = "ECC-192";
  4837. /* first [P-224,SHA-1] vector from FIPS 186-3 NIST vectors */
  4838. b.msg = "\x36\xc8\xb2\x29\x86\x48\x7f\x67\x7c\x18\xd0\x97\x2a\x9e"
  4839. "\x20\x47\xb3\xaf\xa5\x9e\xc1\x62\x76\x4e\xc3\x0b\x5b\x69"
  4840. "\xe0\x63\x0f\x99\x0d\x4e\x05\xc2\x73\xb0\xe5\xa9\xd4\x28"
  4841. "\x27\xb6\x95\xfc\x2d\x64\xd9\x13\x8b\x1c\xf4\xc1\x21\x55"
  4842. "\x89\x4c\x42\x13\x21\xa7\xbb\x97\x0b\xdc\xe0\xfb\xf0\xd2"
  4843. "\xae\x85\x61\xaa\xd8\x71\x7f\x2e\x46\xdf\xe3\xff\x8d\xea"
  4844. "\xb4\xd7\x93\x23\x56\x03\x2c\x15\x13\x0d\x59\x9e\x26\xc1"
  4845. "\x0f\x2f\xec\x96\x30\x31\xac\x69\x38\xa1\x8d\x66\x45\x38"
  4846. "\xb9\x4d\xac\x55\x34\xef\x7b\x59\x94\x24\xd6\x9b\xe1\xf7"
  4847. "\x1c\x20";
  4848. b.msgLen = 128;
  4849. b.Qx = "8a4dca35136c4b70e588e23554637ae251077d1365a6ba5db9585de7";
  4850. b.Qy = "ad3dee06de0be8279d4af435d7245f14f3b4f82eb578e519ee0057b1";
  4851. b.d = "97c4b796e1639dd1035b708fc00dc7ba1682cec44a1002a1a820619f";
  4852. b.R = "147b33758321e722a0360a4719738af848449e2c1d08defebc1671a7";
  4853. b.S = "24fc7ed7f1352ca3872aa0916191289e2e04d454935d50fe6af3ad5b";
  4854. b.curveName = "ECC-224";
  4855. test_ecc[0] = a;
  4856. test_ecc[1] = b;
  4857. for (i = 0; i < times; i++) {
  4858. wc_ecc_free(&userA);
  4859. wc_ecc_init(&userA);
  4860. memset(sig, 0, sizeof(sig));
  4861. x = sizeof(sig);
  4862. /* calculate SHA-1 hash of message */
  4863. ret = wc_InitSha(&sha);
  4864. if (ret != 0)
  4865. return -1015 - i;
  4866. wc_ShaUpdate(&sha, (byte*)test_ecc[i].msg, (word32)test_ecc[i].msgLen);
  4867. wc_ShaFinal(&sha, hash);
  4868. ret = wc_ecc_import_raw(&userA, test_ecc[i].Qx, test_ecc[i].Qy,
  4869. test_ecc[i].d, test_ecc[i].curveName);
  4870. if (ret != 0)
  4871. return -1017 - i;
  4872. ret = wc_ecc_rs_to_sig(test_ecc[i].R, test_ecc[i].S, sig, &x);
  4873. if (ret != 0)
  4874. return -1019 - i;
  4875. ret = wc_ecc_verify_hash(sig, x, hash, sizeof(hash), &verify, &userA);
  4876. if (ret != 0)
  4877. return -1021 - i;
  4878. if (verify != 1)
  4879. return -1023 - i;
  4880. }
  4881. }
  4882. #endif /* defined(HAVE_ECC192) && defined(HAVE_ECC256) */
  4883. #ifdef WOLFSSL_KEY_GEN
  4884. {
  4885. int derSz, pemSz;
  4886. byte der[FOURK_BUF];
  4887. byte pem[FOURK_BUF];
  4888. FILE* keyFile;
  4889. FILE* pemFile;
  4890. derSz = wc_EccKeyToDer(&userB, der, FOURK_BUF);
  4891. if (derSz < 0) {
  4892. return -1024;
  4893. }
  4894. keyFile = fopen("./ecc-key.der", "wb");
  4895. if (!keyFile) {
  4896. return -1025;
  4897. }
  4898. ret = (int)fwrite(der, 1, derSz, keyFile);
  4899. fclose(keyFile);
  4900. if (ret != derSz) {
  4901. return -1026;
  4902. }
  4903. pemSz = wc_DerToPem(der, derSz, pem, FOURK_BUF, ECC_PRIVATEKEY_TYPE);
  4904. if (pemSz < 0) {
  4905. return -1027;
  4906. }
  4907. pemFile = fopen("./ecc-key.pem", "wb");
  4908. if (!pemFile) {
  4909. return -1028;
  4910. }
  4911. ret = (int)fwrite(pem, 1, pemSz, pemFile);
  4912. fclose(pemFile);
  4913. if (ret != pemSz) {
  4914. return -1029;
  4915. }
  4916. }
  4917. #endif /* WOLFSSL_KEY_GEN */
  4918. wc_ecc_free(&pubKey);
  4919. wc_ecc_free(&userB);
  4920. wc_ecc_free(&userA);
  4921. wc_FreeRng(&rng);
  4922. return 0;
  4923. }
  4924. #ifdef HAVE_ECC_ENCRYPT
  4925. int ecc_encrypt_test(void)
  4926. {
  4927. WC_RNG rng;
  4928. int ret;
  4929. ecc_key userA, userB;
  4930. byte msg[48];
  4931. byte plain[48];
  4932. byte out[80];
  4933. word32 outSz = sizeof(out);
  4934. word32 plainSz = sizeof(plain);
  4935. int i;
  4936. ret = wc_InitRng(&rng);
  4937. if (ret != 0)
  4938. return -3001;
  4939. wc_ecc_init(&userA);
  4940. wc_ecc_init(&userB);
  4941. ret = wc_ecc_make_key(&rng, 32, &userA);
  4942. ret += wc_ecc_make_key(&rng, 32, &userB);
  4943. if (ret != 0)
  4944. return -3002;
  4945. for (i = 0; i < 48; i++)
  4946. msg[i] = i;
  4947. /* encrypt msg to B */
  4948. ret = wc_ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz, NULL);
  4949. if (ret != 0)
  4950. return -3003;
  4951. /* decrypt msg from A */
  4952. ret = wc_ecc_decrypt(&userB, &userA, out, outSz, plain, &plainSz, NULL);
  4953. if (ret != 0)
  4954. return -3004;
  4955. if (memcmp(plain, msg, sizeof(msg)) != 0)
  4956. return -3005;
  4957. { /* let's verify message exchange works, A is client, B is server */
  4958. ecEncCtx* cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  4959. ecEncCtx* srvCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  4960. byte cliSalt[EXCHANGE_SALT_SZ];
  4961. byte srvSalt[EXCHANGE_SALT_SZ];
  4962. const byte* tmpSalt;
  4963. if (cliCtx == NULL || srvCtx == NULL)
  4964. return -3006;
  4965. /* get salt to send to peer */
  4966. tmpSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  4967. if (tmpSalt == NULL)
  4968. return -3007;
  4969. memcpy(cliSalt, tmpSalt, EXCHANGE_SALT_SZ);
  4970. tmpSalt = wc_ecc_ctx_get_own_salt(srvCtx);
  4971. if (tmpSalt == NULL)
  4972. return -3007;
  4973. memcpy(srvSalt, tmpSalt, EXCHANGE_SALT_SZ);
  4974. /* in actual use, we'd get the peer's salt over the transport */
  4975. ret = wc_ecc_ctx_set_peer_salt(cliCtx, srvSalt);
  4976. ret += wc_ecc_ctx_set_peer_salt(srvCtx, cliSalt);
  4977. ret += wc_ecc_ctx_set_info(cliCtx, (byte*)"wolfSSL MSGE", 11);
  4978. ret += wc_ecc_ctx_set_info(srvCtx, (byte*)"wolfSSL MSGE", 11);
  4979. if (ret != 0)
  4980. return -3008;
  4981. /* get encrypted msg (request) to send to B */
  4982. outSz = sizeof(out);
  4983. ret = wc_ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz,cliCtx);
  4984. if (ret != 0)
  4985. return -3009;
  4986. /* B decrypts msg (request) from A */
  4987. plainSz = sizeof(plain);
  4988. ret = wc_ecc_decrypt(&userB, &userA, out, outSz, plain, &plainSz, srvCtx);
  4989. if (ret != 0)
  4990. return -3010;
  4991. if (memcmp(plain, msg, sizeof(msg)) != 0)
  4992. return -3011;
  4993. {
  4994. /* msg2 (response) from B to A */
  4995. byte msg2[48];
  4996. byte plain2[48];
  4997. byte out2[80];
  4998. word32 outSz2 = sizeof(out2);
  4999. word32 plainSz2 = sizeof(plain2);
  5000. for (i = 0; i < 48; i++)
  5001. msg2[i] = i+48;
  5002. /* get encrypted msg (response) to send to B */
  5003. ret = wc_ecc_encrypt(&userB, &userA, msg2, sizeof(msg2), out2,
  5004. &outSz2, srvCtx);
  5005. if (ret != 0)
  5006. return -3012;
  5007. /* A decrypts msg (response) from B */
  5008. ret = wc_ecc_decrypt(&userA, &userB, out2, outSz2, plain2, &plainSz2,
  5009. cliCtx);
  5010. if (ret != 0)
  5011. return -3013;
  5012. if (memcmp(plain2, msg2, sizeof(msg2)) != 0)
  5013. return -3014;
  5014. }
  5015. /* cleanup */
  5016. wc_ecc_ctx_free(srvCtx);
  5017. wc_ecc_ctx_free(cliCtx);
  5018. }
  5019. /* cleanup */
  5020. wc_ecc_free(&userB);
  5021. wc_ecc_free(&userA);
  5022. wc_FreeRng(&rng);
  5023. return 0;
  5024. }
  5025. #endif /* HAVE_ECC_ENCRYPT */
  5026. #endif /* HAVE_ECC */
  5027. #ifdef HAVE_CURVE25519
  5028. int curve25519_test(void)
  5029. {
  5030. WC_RNG rng;
  5031. byte sharedA[32];
  5032. byte sharedB[32];
  5033. byte exportBuf[32];
  5034. word32 x, y;
  5035. curve25519_key userA, userB, pubKey;
  5036. /* test vectors from
  5037. https://tools.ietf.org/html/draft-josefsson-tls-curve25519-03
  5038. */
  5039. /* secret key for party a */
  5040. byte sa[] = {
  5041. 0x5A,0xC9,0x9F,0x33,0x63,0x2E,0x5A,0x76,
  5042. 0x8D,0xE7,0xE8,0x1B,0xF8,0x54,0xC2,0x7C,
  5043. 0x46,0xE3,0xFB,0xF2,0xAB,0xBA,0xCD,0x29,
  5044. 0xEC,0x4A,0xFF,0x51,0x73,0x69,0xC6,0x60
  5045. };
  5046. /* public key for party a */
  5047. byte pa[] = {
  5048. 0x05,0x7E,0x23,0xEA,0x9F,0x1C,0xBE,0x8A,
  5049. 0x27,0x16,0x8F,0x6E,0x69,0x6A,0x79,0x1D,
  5050. 0xE6,0x1D,0xD3,0xAF,0x7A,0xCD,0x4E,0xEA,
  5051. 0xCC,0x6E,0x7B,0xA5,0x14,0xFD,0xA8,0x63
  5052. };
  5053. /* secret key for party b */
  5054. byte sb[] = {
  5055. 0x47,0xDC,0x3D,0x21,0x41,0x74,0x82,0x0E,
  5056. 0x11,0x54,0xB4,0x9B,0xC6,0xCD,0xB2,0xAB,
  5057. 0xD4,0x5E,0xE9,0x58,0x17,0x05,0x5D,0x25,
  5058. 0x5A,0xA3,0x58,0x31,0xB7,0x0D,0x32,0x60
  5059. };
  5060. /* public key for party b */
  5061. byte pb[] = {
  5062. 0x6E,0xB8,0x9D,0xA9,0x19,0x89,0xAE,0x37,
  5063. 0xC7,0xEA,0xC7,0x61,0x8D,0x9E,0x5C,0x49,
  5064. 0x51,0xDB,0xA1,0xD7,0x3C,0x28,0x5A,0xE1,
  5065. 0xCD,0x26,0xA8,0x55,0x02,0x0E,0xEF,0x04
  5066. };
  5067. /* expected shared key */
  5068. byte ss[] = {
  5069. 0x61,0x45,0x0C,0xD9,0x8E,0x36,0x01,0x6B,
  5070. 0x58,0x77,0x6A,0x89,0x7A,0x9F,0x0A,0xEF,
  5071. 0x73,0x8B,0x99,0xF0,0x94,0x68,0xB8,0xD6,
  5072. 0xB8,0x51,0x11,0x84,0xD5,0x34,0x94,0xAB
  5073. };
  5074. if (wc_InitRng(&rng) != 0)
  5075. return -1001;
  5076. wc_curve25519_init(&userA);
  5077. wc_curve25519_init(&userB);
  5078. wc_curve25519_init(&pubKey);
  5079. /* make curve25519 keys */
  5080. if (wc_curve25519_make_key(&rng, 32, &userA) != 0)
  5081. return -1002;
  5082. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  5083. return -1003;
  5084. /* find shared secret key */
  5085. x = sizeof(sharedA);
  5086. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  5087. return -1004;
  5088. y = sizeof(sharedB);
  5089. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  5090. return -1005;
  5091. /* compare shared secret keys to test they are the same */
  5092. if (y != x)
  5093. return -1006;
  5094. if (XMEMCMP(sharedA, sharedB, x))
  5095. return -1007;
  5096. /* export a public key and import it for another user */
  5097. x = sizeof(exportBuf);
  5098. if (wc_curve25519_export_public(&userA, exportBuf, &x) != 0)
  5099. return -1008;
  5100. if (wc_curve25519_import_public(exportBuf, x, &pubKey) != 0)
  5101. return -1009;
  5102. /* test shared key after importing a public key */
  5103. XMEMSET(sharedB, 0, sizeof(sharedB));
  5104. y = sizeof(sharedB);
  5105. if (wc_curve25519_shared_secret(&userB, &pubKey, sharedB, &y) != 0)
  5106. return -1010;
  5107. if (XMEMCMP(sharedA, sharedB, y))
  5108. return -1011;
  5109. /* import RFC test vectors and compare shared key */
  5110. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  5111. != 0)
  5112. return -1012;
  5113. if (wc_curve25519_import_private_raw(sb, sizeof(sb), pb, sizeof(pb), &userB)
  5114. != 0)
  5115. return -1013;
  5116. /* test against known test vector */
  5117. XMEMSET(sharedB, 0, sizeof(sharedB));
  5118. y = sizeof(sharedB);
  5119. if (wc_curve25519_shared_secret(&userA, &userB, sharedB, &y) != 0)
  5120. return -1014;
  5121. if (XMEMCMP(ss, sharedB, y))
  5122. return -1015;
  5123. /* test swaping roles of keys and generating same shared key */
  5124. XMEMSET(sharedB, 0, sizeof(sharedB));
  5125. y = sizeof(sharedB);
  5126. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  5127. return -1016;
  5128. if (XMEMCMP(ss, sharedB, y))
  5129. return -1017;
  5130. /* test with 1 generated key and 1 from known test vector */
  5131. if (wc_curve25519_import_private_raw(sa, sizeof(sa), pa, sizeof(pa), &userA)
  5132. != 0)
  5133. return -1018;
  5134. if (wc_curve25519_make_key(&rng, 32, &userB) != 0)
  5135. return -1019;
  5136. x = sizeof(sharedA);
  5137. if (wc_curve25519_shared_secret(&userA, &userB, sharedA, &x) != 0)
  5138. return -1020;
  5139. y = sizeof(sharedB);
  5140. if (wc_curve25519_shared_secret(&userB, &userA, sharedB, &y) != 0)
  5141. return -1021;
  5142. /* compare shared secret keys to test they are the same */
  5143. if (y != x)
  5144. return -1022;
  5145. if (XMEMCMP(sharedA, sharedB, x))
  5146. return -1023;
  5147. /* clean up keys when done */
  5148. wc_curve25519_free(&pubKey);
  5149. wc_curve25519_free(&userB);
  5150. wc_curve25519_free(&userA);
  5151. wc_FreeRng(&rng);
  5152. return 0;
  5153. }
  5154. #endif /* HAVE_CURVE25519 */
  5155. #ifdef HAVE_ED25519
  5156. int ed25519_test(void)
  5157. {
  5158. WC_RNG rng;
  5159. byte out[ED25519_SIG_SIZE];
  5160. byte exportPKey[ED25519_KEY_SIZE];
  5161. byte exportSKey[ED25519_KEY_SIZE];
  5162. word32 outlen;
  5163. word32 exportPSz;
  5164. word32 exportSSz;
  5165. word32 keySz, sigSz;
  5166. int i, verify;
  5167. ed25519_key key;
  5168. ed25519_key key2;
  5169. /* test vectors from
  5170. https://tools.ietf.org/html/draft-josefsson-eddsa-ed25519-02
  5171. */
  5172. const byte sKey1[] = {
  5173. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  5174. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  5175. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  5176. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  5177. };
  5178. const byte sKey2[] = {
  5179. 0x4c,0xcd,0x08,0x9b,0x28,0xff,0x96,0xda,
  5180. 0x9d,0xb6,0xc3,0x46,0xec,0x11,0x4e,0x0f,
  5181. 0x5b,0x8a,0x31,0x9f,0x35,0xab,0xa6,0x24,
  5182. 0xda,0x8c,0xf6,0xed,0x4f,0xb8,0xa6,0xfb
  5183. };
  5184. const byte sKey3[] = {
  5185. 0xc5,0xaa,0x8d,0xf4,0x3f,0x9f,0x83,0x7b,
  5186. 0xed,0xb7,0x44,0x2f,0x31,0xdc,0xb7,0xb1,
  5187. 0x66,0xd3,0x85,0x35,0x07,0x6f,0x09,0x4b,
  5188. 0x85,0xce,0x3a,0x2e,0x0b,0x44,0x58,0xf7
  5189. };
  5190. /* uncompressed test */
  5191. const byte sKey4[] = {
  5192. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  5193. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  5194. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  5195. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  5196. };
  5197. /* compressed prefix test */
  5198. const byte sKey5[] = {
  5199. 0x9d,0x61,0xb1,0x9d,0xef,0xfd,0x5a,0x60,
  5200. 0xba,0x84,0x4a,0xf4,0x92,0xec,0x2c,0xc4,
  5201. 0x44,0x49,0xc5,0x69,0x7b,0x32,0x69,0x19,
  5202. 0x70,0x3b,0xac,0x03,0x1c,0xae,0x7f,0x60
  5203. };
  5204. const byte sKey6[] = {
  5205. 0xf5,0xe5,0x76,0x7c,0xf1,0x53,0x31,0x95,
  5206. 0x17,0x63,0x0f,0x22,0x68,0x76,0xb8,0x6c,
  5207. 0x81,0x60,0xcc,0x58,0x3b,0xc0,0x13,0x74,
  5208. 0x4c,0x6b,0xf2,0x55,0xf5,0xcc,0x0e,0xe5
  5209. };
  5210. const byte* sKeys[] = {sKey1, sKey2, sKey3, sKey4, sKey5, sKey6};
  5211. const byte pKey1[] = {
  5212. 0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  5213. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  5214. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  5215. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  5216. };
  5217. const byte pKey2[] = {
  5218. 0x3d,0x40,0x17,0xc3,0xe8,0x43,0x89,0x5a,
  5219. 0x92,0xb7,0x0a,0xa7,0x4d,0x1b,0x7e,0xbc,
  5220. 0x9c,0x98,0x2c,0xcf,0x2e,0xc4,0x96,0x8c,
  5221. 0xc0,0xcd,0x55,0xf1,0x2a,0xf4,0x66,0x0c
  5222. };
  5223. const byte pKey3[] = {
  5224. 0xfc,0x51,0xcd,0x8e,0x62,0x18,0xa1,0xa3,
  5225. 0x8d,0xa4,0x7e,0xd0,0x02,0x30,0xf0,0x58,
  5226. 0x08,0x16,0xed,0x13,0xba,0x33,0x03,0xac,
  5227. 0x5d,0xeb,0x91,0x15,0x48,0x90,0x80,0x25
  5228. };
  5229. /* uncompressed test */
  5230. const byte pKey4[] = {
  5231. 0x04,0x55,0xd0,0xe0,0x9a,0x2b,0x9d,0x34,
  5232. 0x29,0x22,0x97,0xe0,0x8d,0x60,0xd0,0xf6,
  5233. 0x20,0xc5,0x13,0xd4,0x72,0x53,0x18,0x7c,
  5234. 0x24,0xb1,0x27,0x86,0xbd,0x77,0x76,0x45,
  5235. 0xce,0x1a,0x51,0x07,0xf7,0x68,0x1a,0x02,
  5236. 0xaf,0x25,0x23,0xa6,0xda,0xf3,0x72,0xe1,
  5237. 0x0e,0x3a,0x07,0x64,0xc9,0xd3,0xfe,0x4b,
  5238. 0xd5,0xb7,0x0a,0xb1,0x82,0x01,0x98,0x5a,
  5239. 0xd7
  5240. };
  5241. /* compressed prefix */
  5242. const byte pKey5[] = {
  5243. 0x40,0xd7,0x5a,0x98,0x01,0x82,0xb1,0x0a,0xb7,
  5244. 0xd5,0x4b,0xfe,0xd3,0xc9,0x64,0x07,0x3a,
  5245. 0x0e,0xe1,0x72,0xf3,0xda,0xa6,0x23,0x25,
  5246. 0xaf,0x02,0x1a,0x68,0xf7,0x07,0x51,0x1a
  5247. };
  5248. const byte pKey6[] = {
  5249. 0x27,0x81,0x17,0xfc,0x14,0x4c,0x72,0x34,
  5250. 0x0f,0x67,0xd0,0xf2,0x31,0x6e,0x83,0x86,
  5251. 0xce,0xff,0xbf,0x2b,0x24,0x28,0xc9,0xc5,
  5252. 0x1f,0xef,0x7c,0x59,0x7f,0x1d,0x42,0x6e
  5253. };
  5254. const byte* pKeys[] = {pKey1, pKey2, pKey3, pKey4, pKey5, pKey6};
  5255. const byte pKeySz[] = {sizeof(pKey1), sizeof(pKey2), sizeof(pKey3),
  5256. sizeof(pKey4), sizeof(pKey5), sizeof(pKey6)};
  5257. const byte sig1[] = {
  5258. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  5259. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  5260. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  5261. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  5262. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  5263. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  5264. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  5265. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  5266. };
  5267. const byte sig2[] = {
  5268. 0x92,0xa0,0x09,0xa9,0xf0,0xd4,0xca,0xb8,
  5269. 0x72,0x0e,0x82,0x0b,0x5f,0x64,0x25,0x40,
  5270. 0xa2,0xb2,0x7b,0x54,0x16,0x50,0x3f,0x8f,
  5271. 0xb3,0x76,0x22,0x23,0xeb,0xdb,0x69,0xda,
  5272. 0x08,0x5a,0xc1,0xe4,0x3e,0x15,0x99,0x6e,
  5273. 0x45,0x8f,0x36,0x13,0xd0,0xf1,0x1d,0x8c,
  5274. 0x38,0x7b,0x2e,0xae,0xb4,0x30,0x2a,0xee,
  5275. 0xb0,0x0d,0x29,0x16,0x12,0xbb,0x0c,0x00
  5276. };
  5277. const byte sig3[] = {
  5278. 0x62,0x91,0xd6,0x57,0xde,0xec,0x24,0x02,
  5279. 0x48,0x27,0xe6,0x9c,0x3a,0xbe,0x01,0xa3,
  5280. 0x0c,0xe5,0x48,0xa2,0x84,0x74,0x3a,0x44,
  5281. 0x5e,0x36,0x80,0xd7,0xdb,0x5a,0xc3,0xac,
  5282. 0x18,0xff,0x9b,0x53,0x8d,0x16,0xf2,0x90,
  5283. 0xae,0x67,0xf7,0x60,0x98,0x4d,0xc6,0x59,
  5284. 0x4a,0x7c,0x15,0xe9,0x71,0x6e,0xd2,0x8d,
  5285. 0xc0,0x27,0xbe,0xce,0xea,0x1e,0xc4,0x0a
  5286. };
  5287. /* uncompressed test */
  5288. const byte sig4[] = {
  5289. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  5290. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  5291. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  5292. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  5293. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  5294. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  5295. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  5296. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  5297. };
  5298. /* compressed prefix */
  5299. const byte sig5[] = {
  5300. 0xe5,0x56,0x43,0x00,0xc3,0x60,0xac,0x72,
  5301. 0x90,0x86,0xe2,0xcc,0x80,0x6e,0x82,0x8a,
  5302. 0x84,0x87,0x7f,0x1e,0xb8,0xe5,0xd9,0x74,
  5303. 0xd8,0x73,0xe0,0x65,0x22,0x49,0x01,0x55,
  5304. 0x5f,0xb8,0x82,0x15,0x90,0xa3,0x3b,0xac,
  5305. 0xc6,0x1e,0x39,0x70,0x1c,0xf9,0xb4,0x6b,
  5306. 0xd2,0x5b,0xf5,0xf0,0x59,0x5b,0xbe,0x24,
  5307. 0x65,0x51,0x41,0x43,0x8e,0x7a,0x10,0x0b
  5308. };
  5309. const byte sig6[] = {
  5310. 0x0a,0xab,0x4c,0x90,0x05,0x01,0xb3,0xe2,
  5311. 0x4d,0x7c,0xdf,0x46,0x63,0x32,0x6a,0x3a,
  5312. 0x87,0xdf,0x5e,0x48,0x43,0xb2,0xcb,0xdb,
  5313. 0x67,0xcb,0xf6,0xe4,0x60,0xfe,0xc3,0x50,
  5314. 0xaa,0x53,0x71,0xb1,0x50,0x8f,0x9f,0x45,
  5315. 0x28,0xec,0xea,0x23,0xc4,0x36,0xd9,0x4b,
  5316. 0x5e,0x8f,0xcd,0x4f,0x68,0x1e,0x30,0xa6,
  5317. 0xac,0x00,0xa9,0x70,0x4a,0x18,0x8a,0x03
  5318. };
  5319. const byte* sigs[] = {sig1, sig2, sig3, sig4, sig5, sig6};
  5320. const byte msg1[] = {};
  5321. const byte msg2[] = {0x72};
  5322. const byte msg3[] = {0xAF,0x82};
  5323. /* test of a 1024 byte long message */
  5324. const byte msg4[] = {
  5325. 0x08,0xb8,0xb2,0xb7,0x33,0x42,0x42,0x43,
  5326. 0x76,0x0f,0xe4,0x26,0xa4,0xb5,0x49,0x08,
  5327. 0x63,0x21,0x10,0xa6,0x6c,0x2f,0x65,0x91,
  5328. 0xea,0xbd,0x33,0x45,0xe3,0xe4,0xeb,0x98,
  5329. 0xfa,0x6e,0x26,0x4b,0xf0,0x9e,0xfe,0x12,
  5330. 0xee,0x50,0xf8,0xf5,0x4e,0x9f,0x77,0xb1,
  5331. 0xe3,0x55,0xf6,0xc5,0x05,0x44,0xe2,0x3f,
  5332. 0xb1,0x43,0x3d,0xdf,0x73,0xbe,0x84,0xd8,
  5333. 0x79,0xde,0x7c,0x00,0x46,0xdc,0x49,0x96,
  5334. 0xd9,0xe7,0x73,0xf4,0xbc,0x9e,0xfe,0x57,
  5335. 0x38,0x82,0x9a,0xdb,0x26,0xc8,0x1b,0x37,
  5336. 0xc9,0x3a,0x1b,0x27,0x0b,0x20,0x32,0x9d,
  5337. 0x65,0x86,0x75,0xfc,0x6e,0xa5,0x34,0xe0,
  5338. 0x81,0x0a,0x44,0x32,0x82,0x6b,0xf5,0x8c,
  5339. 0x94,0x1e,0xfb,0x65,0xd5,0x7a,0x33,0x8b,
  5340. 0xbd,0x2e,0x26,0x64,0x0f,0x89,0xff,0xbc,
  5341. 0x1a,0x85,0x8e,0xfc,0xb8,0x55,0x0e,0xe3,
  5342. 0xa5,0xe1,0x99,0x8b,0xd1,0x77,0xe9,0x3a,
  5343. 0x73,0x63,0xc3,0x44,0xfe,0x6b,0x19,0x9e,
  5344. 0xe5,0xd0,0x2e,0x82,0xd5,0x22,0xc4,0xfe,
  5345. 0xba,0x15,0x45,0x2f,0x80,0x28,0x8a,0x82,
  5346. 0x1a,0x57,0x91,0x16,0xec,0x6d,0xad,0x2b,
  5347. 0x3b,0x31,0x0d,0xa9,0x03,0x40,0x1a,0xa6,
  5348. 0x21,0x00,0xab,0x5d,0x1a,0x36,0x55,0x3e,
  5349. 0x06,0x20,0x3b,0x33,0x89,0x0c,0xc9,0xb8,
  5350. 0x32,0xf7,0x9e,0xf8,0x05,0x60,0xcc,0xb9,
  5351. 0xa3,0x9c,0xe7,0x67,0x96,0x7e,0xd6,0x28,
  5352. 0xc6,0xad,0x57,0x3c,0xb1,0x16,0xdb,0xef,
  5353. 0xef,0xd7,0x54,0x99,0xda,0x96,0xbd,0x68,
  5354. 0xa8,0xa9,0x7b,0x92,0x8a,0x8b,0xbc,0x10,
  5355. 0x3b,0x66,0x21,0xfc,0xde,0x2b,0xec,0xa1,
  5356. 0x23,0x1d,0x20,0x6b,0xe6,0xcd,0x9e,0xc7,
  5357. 0xaf,0xf6,0xf6,0xc9,0x4f,0xcd,0x72,0x04,
  5358. 0xed,0x34,0x55,0xc6,0x8c,0x83,0xf4,0xa4,
  5359. 0x1d,0xa4,0xaf,0x2b,0x74,0xef,0x5c,0x53,
  5360. 0xf1,0xd8,0xac,0x70,0xbd,0xcb,0x7e,0xd1,
  5361. 0x85,0xce,0x81,0xbd,0x84,0x35,0x9d,0x44,
  5362. 0x25,0x4d,0x95,0x62,0x9e,0x98,0x55,0xa9,
  5363. 0x4a,0x7c,0x19,0x58,0xd1,0xf8,0xad,0xa5,
  5364. 0xd0,0x53,0x2e,0xd8,0xa5,0xaa,0x3f,0xb2,
  5365. 0xd1,0x7b,0xa7,0x0e,0xb6,0x24,0x8e,0x59,
  5366. 0x4e,0x1a,0x22,0x97,0xac,0xbb,0xb3,0x9d,
  5367. 0x50,0x2f,0x1a,0x8c,0x6e,0xb6,0xf1,0xce,
  5368. 0x22,0xb3,0xde,0x1a,0x1f,0x40,0xcc,0x24,
  5369. 0x55,0x41,0x19,0xa8,0x31,0xa9,0xaa,0xd6,
  5370. 0x07,0x9c,0xad,0x88,0x42,0x5d,0xe6,0xbd,
  5371. 0xe1,0xa9,0x18,0x7e,0xbb,0x60,0x92,0xcf,
  5372. 0x67,0xbf,0x2b,0x13,0xfd,0x65,0xf2,0x70,
  5373. 0x88,0xd7,0x8b,0x7e,0x88,0x3c,0x87,0x59,
  5374. 0xd2,0xc4,0xf5,0xc6,0x5a,0xdb,0x75,0x53,
  5375. 0x87,0x8a,0xd5,0x75,0xf9,0xfa,0xd8,0x78,
  5376. 0xe8,0x0a,0x0c,0x9b,0xa6,0x3b,0xcb,0xcc,
  5377. 0x27,0x32,0xe6,0x94,0x85,0xbb,0xc9,0xc9,
  5378. 0x0b,0xfb,0xd6,0x24,0x81,0xd9,0x08,0x9b,
  5379. 0xec,0xcf,0x80,0xcf,0xe2,0xdf,0x16,0xa2,
  5380. 0xcf,0x65,0xbd,0x92,0xdd,0x59,0x7b,0x07,
  5381. 0x07,0xe0,0x91,0x7a,0xf4,0x8b,0xbb,0x75,
  5382. 0xfe,0xd4,0x13,0xd2,0x38,0xf5,0x55,0x5a,
  5383. 0x7a,0x56,0x9d,0x80,0xc3,0x41,0x4a,0x8d,
  5384. 0x08,0x59,0xdc,0x65,0xa4,0x61,0x28,0xba,
  5385. 0xb2,0x7a,0xf8,0x7a,0x71,0x31,0x4f,0x31,
  5386. 0x8c,0x78,0x2b,0x23,0xeb,0xfe,0x80,0x8b,
  5387. 0x82,0xb0,0xce,0x26,0x40,0x1d,0x2e,0x22,
  5388. 0xf0,0x4d,0x83,0xd1,0x25,0x5d,0xc5,0x1a,
  5389. 0xdd,0xd3,0xb7,0x5a,0x2b,0x1a,0xe0,0x78,
  5390. 0x45,0x04,0xdf,0x54,0x3a,0xf8,0x96,0x9b,
  5391. 0xe3,0xea,0x70,0x82,0xff,0x7f,0xc9,0x88,
  5392. 0x8c,0x14,0x4d,0xa2,0xaf,0x58,0x42,0x9e,
  5393. 0xc9,0x60,0x31,0xdb,0xca,0xd3,0xda,0xd9,
  5394. 0xaf,0x0d,0xcb,0xaa,0xaf,0x26,0x8c,0xb8,
  5395. 0xfc,0xff,0xea,0xd9,0x4f,0x3c,0x7c,0xa4,
  5396. 0x95,0xe0,0x56,0xa9,0xb4,0x7a,0xcd,0xb7,
  5397. 0x51,0xfb,0x73,0xe6,0x66,0xc6,0xc6,0x55,
  5398. 0xad,0xe8,0x29,0x72,0x97,0xd0,0x7a,0xd1,
  5399. 0xba,0x5e,0x43,0xf1,0xbc,0xa3,0x23,0x01,
  5400. 0x65,0x13,0x39,0xe2,0x29,0x04,0xcc,0x8c,
  5401. 0x42,0xf5,0x8c,0x30,0xc0,0x4a,0xaf,0xdb,
  5402. 0x03,0x8d,0xda,0x08,0x47,0xdd,0x98,0x8d,
  5403. 0xcd,0xa6,0xf3,0xbf,0xd1,0x5c,0x4b,0x4c,
  5404. 0x45,0x25,0x00,0x4a,0xa0,0x6e,0xef,0xf8,
  5405. 0xca,0x61,0x78,0x3a,0xac,0xec,0x57,0xfb,
  5406. 0x3d,0x1f,0x92,0xb0,0xfe,0x2f,0xd1,0xa8,
  5407. 0x5f,0x67,0x24,0x51,0x7b,0x65,0xe6,0x14,
  5408. 0xad,0x68,0x08,0xd6,0xf6,0xee,0x34,0xdf,
  5409. 0xf7,0x31,0x0f,0xdc,0x82,0xae,0xbf,0xd9,
  5410. 0x04,0xb0,0x1e,0x1d,0xc5,0x4b,0x29,0x27,
  5411. 0x09,0x4b,0x2d,0xb6,0x8d,0x6f,0x90,0x3b,
  5412. 0x68,0x40,0x1a,0xde,0xbf,0x5a,0x7e,0x08,
  5413. 0xd7,0x8f,0xf4,0xef,0x5d,0x63,0x65,0x3a,
  5414. 0x65,0x04,0x0c,0xf9,0xbf,0xd4,0xac,0xa7,
  5415. 0x98,0x4a,0x74,0xd3,0x71,0x45,0x98,0x67,
  5416. 0x80,0xfc,0x0b,0x16,0xac,0x45,0x16,0x49,
  5417. 0xde,0x61,0x88,0xa7,0xdb,0xdf,0x19,0x1f,
  5418. 0x64,0xb5,0xfc,0x5e,0x2a,0xb4,0x7b,0x57,
  5419. 0xf7,0xf7,0x27,0x6c,0xd4,0x19,0xc1,0x7a,
  5420. 0x3c,0xa8,0xe1,0xb9,0x39,0xae,0x49,0xe4,
  5421. 0x88,0xac,0xba,0x6b,0x96,0x56,0x10,0xb5,
  5422. 0x48,0x01,0x09,0xc8,0xb1,0x7b,0x80,0xe1,
  5423. 0xb7,0xb7,0x50,0xdf,0xc7,0x59,0x8d,0x5d,
  5424. 0x50,0x11,0xfd,0x2d,0xcc,0x56,0x00,0xa3,
  5425. 0x2e,0xf5,0xb5,0x2a,0x1e,0xcc,0x82,0x0e,
  5426. 0x30,0x8a,0xa3,0x42,0x72,0x1a,0xac,0x09,
  5427. 0x43,0xbf,0x66,0x86,0xb6,0x4b,0x25,0x79,
  5428. 0x37,0x65,0x04,0xcc,0xc4,0x93,0xd9,0x7e,
  5429. 0x6a,0xed,0x3f,0xb0,0xf9,0xcd,0x71,0xa4,
  5430. 0x3d,0xd4,0x97,0xf0,0x1f,0x17,0xc0,0xe2,
  5431. 0xcb,0x37,0x97,0xaa,0x2a,0x2f,0x25,0x66,
  5432. 0x56,0x16,0x8e,0x6c,0x49,0x6a,0xfc,0x5f,
  5433. 0xb9,0x32,0x46,0xf6,0xb1,0x11,0x63,0x98,
  5434. 0xa3,0x46,0xf1,0xa6,0x41,0xf3,0xb0,0x41,
  5435. 0xe9,0x89,0xf7,0x91,0x4f,0x90,0xcc,0x2c,
  5436. 0x7f,0xff,0x35,0x78,0x76,0xe5,0x06,0xb5,
  5437. 0x0d,0x33,0x4b,0xa7,0x7c,0x22,0x5b,0xc3,
  5438. 0x07,0xba,0x53,0x71,0x52,0xf3,0xf1,0x61,
  5439. 0x0e,0x4e,0xaf,0xe5,0x95,0xf6,0xd9,0xd9,
  5440. 0x0d,0x11,0xfa,0xa9,0x33,0xa1,0x5e,0xf1,
  5441. 0x36,0x95,0x46,0x86,0x8a,0x7f,0x3a,0x45,
  5442. 0xa9,0x67,0x68,0xd4,0x0f,0xd9,0xd0,0x34,
  5443. 0x12,0xc0,0x91,0xc6,0x31,0x5c,0xf4,0xfd,
  5444. 0xe7,0xcb,0x68,0x60,0x69,0x37,0x38,0x0d,
  5445. 0xb2,0xea,0xaa,0x70,0x7b,0x4c,0x41,0x85,
  5446. 0xc3,0x2e,0xdd,0xcd,0xd3,0x06,0x70,0x5e,
  5447. 0x4d,0xc1,0xff,0xc8,0x72,0xee,0xee,0x47,
  5448. 0x5a,0x64,0xdf,0xac,0x86,0xab,0xa4,0x1c,
  5449. 0x06,0x18,0x98,0x3f,0x87,0x41,0xc5,0xef,
  5450. 0x68,0xd3,0xa1,0x01,0xe8,0xa3,0xb8,0xca,
  5451. 0xc6,0x0c,0x90,0x5c,0x15,0xfc,0x91,0x08,
  5452. 0x40,0xb9,0x4c,0x00,0xa0,0xb9,0xd0
  5453. };
  5454. const byte* msgs[] = {msg1, msg2, msg3, msg1, msg1, msg4};
  5455. const word16 msgSz[] = {sizeof(msg1), sizeof(msg2), sizeof(msg3),
  5456. sizeof(msg1), sizeof(msg1), sizeof(msg4)};
  5457. /* create ed25519 keys */
  5458. wc_InitRng(&rng);
  5459. wc_ed25519_init(&key);
  5460. wc_ed25519_init(&key2);
  5461. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  5462. wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key2);
  5463. /* helper functions for signature and key size */
  5464. keySz = wc_ed25519_size(&key);
  5465. sigSz = wc_ed25519_sig_size(&key);
  5466. for (i = 0; i < 6; i++) {
  5467. outlen = sizeof(out);
  5468. XMEMSET(out, 0, sizeof(out));
  5469. if (wc_ed25519_import_private_key(sKeys[i], ED25519_KEY_SIZE, pKeys[i],
  5470. pKeySz[i], &key) != 0)
  5471. return -1021 - i;
  5472. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key)
  5473. != 0)
  5474. return -1027 - i;
  5475. if (XMEMCMP(out, sigs[i], 64))
  5476. return -1033 - i;
  5477. /* test verify on good msg */
  5478. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  5479. &key) != 0 || verify != 1)
  5480. return -1039 - i;
  5481. /* test verify on bad msg */
  5482. out[outlen-1] = out[outlen-1] + 1;
  5483. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  5484. &key) == 0 || verify == 1)
  5485. return -1045 - i;
  5486. /* test api for import/exporting keys */
  5487. exportPSz = sizeof(exportPKey);
  5488. exportSSz = sizeof(exportSKey);
  5489. if (wc_ed25519_export_public(&key, exportPKey, &exportPSz) != 0)
  5490. return -1051 - i;
  5491. if (wc_ed25519_import_public(exportPKey, exportPSz, &key2) != 0)
  5492. return -1057 - i;
  5493. if (wc_ed25519_export_private_only(&key, exportSKey, &exportSSz) != 0)
  5494. return -1063 - i;
  5495. if (wc_ed25519_import_private_key(exportSKey, exportSSz,
  5496. exportPKey, exportPSz, &key2) != 0)
  5497. return -1069 - i;
  5498. /* clear "out" buffer and test sign with imported keys */
  5499. outlen = sizeof(out);
  5500. XMEMSET(out, 0, sizeof(out));
  5501. if (wc_ed25519_sign_msg(msgs[i], msgSz[i], out, &outlen, &key2) != 0)
  5502. return -1075 - i;
  5503. if (wc_ed25519_verify_msg(out, outlen, msgs[i], msgSz[i], &verify,
  5504. &key2) != 0 || verify != 1)
  5505. return -1081 - i;
  5506. if (XMEMCMP(out, sigs[i], 64))
  5507. return -1087 - i;
  5508. }
  5509. /* clean up keys when done */
  5510. wc_ed25519_free(&key);
  5511. wc_ed25519_free(&key2);
  5512. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  5513. wc_FreeRng(&rng);
  5514. #endif
  5515. /* hush warrnings of unused keySz and sigSz */
  5516. (void)keySz;
  5517. (void)sigSz;
  5518. return 0;
  5519. }
  5520. #endif /* HAVE_ED25519 */
  5521. #ifdef HAVE_LIBZ
  5522. const byte sample_text[] =
  5523. "Biodiesel cupidatat marfa, cliche aute put a bird on it incididunt elit\n"
  5524. "polaroid. Sunt tattooed bespoke reprehenderit. Sint twee organic id\n"
  5525. "marfa. Commodo veniam ad esse gastropub. 3 wolf moon sartorial vero,\n"
  5526. "plaid delectus biodiesel squid +1 vice. Post-ironic keffiyeh leggings\n"
  5527. "selfies cray fap hoodie, forage anim. Carles cupidatat shoreditch, VHS\n"
  5528. "small batch meggings kogi dolore food truck bespoke gastropub.\n"
  5529. "\n"
  5530. "Terry richardson adipisicing actually typewriter tumblr, twee whatever\n"
  5531. "four loko you probably haven't heard of them high life. Messenger bag\n"
  5532. "whatever tattooed deep v mlkshk. Brooklyn pinterest assumenda chillwave\n"
  5533. "et, banksy ullamco messenger bag umami pariatur direct trade forage.\n"
  5534. "Typewriter culpa try-hard, pariatur sint brooklyn meggings. Gentrify\n"
  5535. "food truck next level, tousled irony non semiotics PBR ethical anim cred\n"
  5536. "readymade. Mumblecore brunch lomo odd future, portland organic terry\n"
  5537. "richardson elit leggings adipisicing ennui raw denim banjo hella. Godard\n"
  5538. "mixtape polaroid, pork belly readymade organic cray typewriter helvetica\n"
  5539. "four loko whatever street art yr farm-to-table.\n"
  5540. "\n"
  5541. "Vinyl keytar vice tofu. Locavore you probably haven't heard of them pug\n"
  5542. "pickled, hella tonx labore truffaut DIY mlkshk elit cosby sweater sint\n"
  5543. "et mumblecore. Elit swag semiotics, reprehenderit DIY sartorial nisi ugh\n"
  5544. "nesciunt pug pork belly wayfarers selfies delectus. Ethical hoodie\n"
  5545. "seitan fingerstache kale chips. Terry richardson artisan williamsburg,\n"
  5546. "eiusmod fanny pack irony tonx ennui lo-fi incididunt tofu YOLO\n"
  5547. "readymade. 8-bit sed ethnic beard officia. Pour-over iphone DIY butcher,\n"
  5548. "ethnic art party qui letterpress nisi proident jean shorts mlkshk\n"
  5549. "locavore.\n"
  5550. "\n"
  5551. "Narwhal flexitarian letterpress, do gluten-free voluptate next level\n"
  5552. "banh mi tonx incididunt carles DIY. Odd future nulla 8-bit beard ut\n"
  5553. "cillum pickled velit, YOLO officia you probably haven't heard of them\n"
  5554. "trust fund gastropub. Nisi adipisicing tattooed, Austin mlkshk 90's\n"
  5555. "small batch american apparel. Put a bird on it cosby sweater before they\n"
  5556. "sold out pork belly kogi hella. Street art mollit sustainable polaroid,\n"
  5557. "DIY ethnic ea pug beard dreamcatcher cosby sweater magna scenester nisi.\n"
  5558. "Sed pork belly skateboard mollit, labore proident eiusmod. Sriracha\n"
  5559. "excepteur cosby sweater, anim deserunt laborum eu aliquip ethical et\n"
  5560. "neutra PBR selvage.\n"
  5561. "\n"
  5562. "Raw denim pork belly truffaut, irony plaid sustainable put a bird on it\n"
  5563. "next level jean shorts exercitation. Hashtag keytar whatever, nihil\n"
  5564. "authentic aliquip disrupt laborum. Tattooed selfies deserunt trust fund\n"
  5565. "wayfarers. 3 wolf moon synth church-key sartorial, gastropub leggings\n"
  5566. "tattooed. Labore high life commodo, meggings raw denim fingerstache pug\n"
  5567. "trust fund leggings seitan forage. Nostrud ullamco duis, reprehenderit\n"
  5568. "incididunt flannel sustainable helvetica pork belly pug banksy you\n"
  5569. "probably haven't heard of them nesciunt farm-to-table. Disrupt nostrud\n"
  5570. "mollit magna, sriracha sartorial helvetica.\n"
  5571. "\n"
  5572. "Nulla kogi reprehenderit, skateboard sustainable duis adipisicing viral\n"
  5573. "ad fanny pack salvia. Fanny pack trust fund you probably haven't heard\n"
  5574. "of them YOLO vice nihil. Keffiyeh cray lo-fi pinterest cardigan aliqua,\n"
  5575. "reprehenderit aute. Culpa tousled williamsburg, marfa lomo actually anim\n"
  5576. "skateboard. Iphone aliqua ugh, semiotics pariatur vero readymade\n"
  5577. "organic. Marfa squid nulla, in laborum disrupt laboris irure gastropub.\n"
  5578. "Veniam sunt food truck leggings, sint vinyl fap.\n"
  5579. "\n"
  5580. "Hella dolore pork belly, truffaut carles you probably haven't heard of\n"
  5581. "them PBR helvetica in sapiente. Fashion axe ugh bushwick american\n"
  5582. "apparel. Fingerstache sed iphone, jean shorts blue bottle nisi bushwick\n"
  5583. "flexitarian officia veniam plaid bespoke fap YOLO lo-fi. Blog\n"
  5584. "letterpress mumblecore, food truck id cray brooklyn cillum ad sed.\n"
  5585. "Assumenda chambray wayfarers vinyl mixtape sustainable. VHS vinyl\n"
  5586. "delectus, culpa williamsburg polaroid cliche swag church-key synth kogi\n"
  5587. "magna pop-up literally. Swag thundercats ennui shoreditch vegan\n"
  5588. "pitchfork neutra truffaut etsy, sed single-origin coffee craft beer.\n"
  5589. "\n"
  5590. "Odio letterpress brooklyn elit. Nulla single-origin coffee in occaecat\n"
  5591. "meggings. Irony meggings 8-bit, chillwave lo-fi adipisicing cred\n"
  5592. "dreamcatcher veniam. Put a bird on it irony umami, trust fund bushwick\n"
  5593. "locavore kale chips. Sriracha swag thundercats, chillwave disrupt\n"
  5594. "tousled beard mollit mustache leggings portland next level. Nihil esse\n"
  5595. "est, skateboard art party etsy thundercats sed dreamcatcher ut iphone\n"
  5596. "swag consectetur et. Irure skateboard banjo, nulla deserunt messenger\n"
  5597. "bag dolor terry richardson sapiente.\n";
  5598. int compress_test(void)
  5599. {
  5600. int ret = 0;
  5601. word32 dSz = sizeof(sample_text);
  5602. word32 cSz = (dSz + (word32)(dSz * 0.001) + 12);
  5603. byte *c = NULL;
  5604. byte *d = NULL;
  5605. c = calloc(cSz, sizeof(byte));
  5606. d = calloc(dSz, sizeof(byte));
  5607. if (c == NULL || d == NULL)
  5608. ret = -300;
  5609. if (ret == 0 && (ret = wc_Compress(c, cSz, sample_text, dSz, 0)) < 0)
  5610. ret = -301;
  5611. if (ret > 0) {
  5612. cSz = (word32)ret;
  5613. ret = 0;
  5614. }
  5615. if (ret == 0 && wc_DeCompress(d, dSz, c, cSz) != (int)dSz)
  5616. ret = -302;
  5617. if (ret == 0 && memcmp(d, sample_text, dSz))
  5618. ret = -303;
  5619. if (c) free(c);
  5620. if (d) free(d);
  5621. return ret;
  5622. }
  5623. #endif /* HAVE_LIBZ */
  5624. #ifdef HAVE_PKCS7
  5625. int pkcs7enveloped_test(void)
  5626. {
  5627. int ret = 0;
  5628. int cipher = DES3b;
  5629. int envelopedSz, decodedSz;
  5630. PKCS7 pkcs7;
  5631. byte* cert;
  5632. byte* privKey;
  5633. byte enveloped[2048];
  5634. byte decoded[2048];
  5635. size_t certSz;
  5636. size_t privKeySz;
  5637. FILE* certFile;
  5638. FILE* keyFile;
  5639. FILE* pkcs7File;
  5640. const char* pkcs7OutFile = "pkcs7envelopedData.der";
  5641. const byte data[] = { /* Hello World */
  5642. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  5643. 0x72,0x6c,0x64
  5644. };
  5645. /* read client cert and key in DER format */
  5646. cert = (byte*)malloc(FOURK_BUF);
  5647. if (cert == NULL)
  5648. return -201;
  5649. privKey = (byte*)malloc(FOURK_BUF);
  5650. if (privKey == NULL) {
  5651. free(cert);
  5652. return -202;
  5653. }
  5654. certFile = fopen(clientCert, "rb");
  5655. if (!certFile) {
  5656. free(cert);
  5657. free(privKey);
  5658. err_sys("can't open ./certs/client-cert.der, "
  5659. "Please run from wolfSSL home dir", -42);
  5660. return -42;
  5661. }
  5662. certSz = fread(cert, 1, FOURK_BUF, certFile);
  5663. fclose(certFile);
  5664. keyFile = fopen(clientKey, "rb");
  5665. if (!keyFile) {
  5666. free(cert);
  5667. free(privKey);
  5668. err_sys("can't open ./certs/client-key.der, "
  5669. "Please run from wolfSSL home dir", -43);
  5670. return -43;
  5671. }
  5672. privKeySz = fread(privKey, 1, FOURK_BUF, keyFile);
  5673. fclose(keyFile);
  5674. wc_PKCS7_InitWithCert(&pkcs7, cert, (word32)certSz);
  5675. pkcs7.content = (byte*)data;
  5676. pkcs7.contentSz = (word32)sizeof(data);
  5677. pkcs7.contentOID = DATA;
  5678. pkcs7.encryptOID = cipher;
  5679. pkcs7.privateKey = privKey;
  5680. pkcs7.privateKeySz = (word32)privKeySz;
  5681. /* encode envelopedData */
  5682. envelopedSz = wc_PKCS7_EncodeEnvelopedData(&pkcs7, enveloped,
  5683. sizeof(enveloped));
  5684. if (envelopedSz <= 0) {
  5685. free(cert);
  5686. free(privKey);
  5687. return -203;
  5688. }
  5689. /* decode envelopedData */
  5690. decodedSz = wc_PKCS7_DecodeEnvelopedData(&pkcs7, enveloped, envelopedSz,
  5691. decoded, sizeof(decoded));
  5692. if (decodedSz <= 0) {
  5693. free(cert);
  5694. free(privKey);
  5695. return -204;
  5696. }
  5697. /* test decode result */
  5698. if (memcmp(decoded, data, sizeof(data)) != 0) {
  5699. free(cert);
  5700. free(privKey);
  5701. return -205;
  5702. }
  5703. /* output pkcs7 envelopedData for external testing */
  5704. pkcs7File = fopen(pkcs7OutFile, "wb");
  5705. if (!pkcs7File) {
  5706. free(cert);
  5707. free(privKey);
  5708. return -206;
  5709. }
  5710. ret = (int)fwrite(enveloped, envelopedSz, 1, pkcs7File);
  5711. fclose(pkcs7File);
  5712. free(cert);
  5713. free(privKey);
  5714. wc_PKCS7_Free(&pkcs7);
  5715. if (ret > 0)
  5716. return 0;
  5717. return ret;
  5718. }
  5719. int pkcs7signed_test(void)
  5720. {
  5721. int ret = 0;
  5722. FILE* file;
  5723. byte* certDer;
  5724. byte* keyDer;
  5725. byte* out;
  5726. char data[] = "Hello World";
  5727. word32 dataSz, outSz, certDerSz, keyDerSz;
  5728. PKCS7 msg;
  5729. WC_RNG rng;
  5730. byte transIdOid[] =
  5731. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  5732. 0x09, 0x07 };
  5733. byte messageTypeOid[] =
  5734. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  5735. 0x09, 0x02 };
  5736. byte senderNonceOid[] =
  5737. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  5738. 0x09, 0x05 };
  5739. byte transId[(SHA_DIGEST_SIZE + 1) * 2 + 1];
  5740. byte messageType[] = { 0x13, 2, '1', '9' };
  5741. byte senderNonce[PKCS7_NONCE_SZ + 2];
  5742. PKCS7Attrib attribs[] =
  5743. {
  5744. { transIdOid, sizeof(transIdOid),
  5745. transId, sizeof(transId) - 1 }, /* take off the null */
  5746. { messageTypeOid, sizeof(messageTypeOid),
  5747. messageType, sizeof(messageType) },
  5748. { senderNonceOid, sizeof(senderNonceOid),
  5749. senderNonce, sizeof(senderNonce) }
  5750. };
  5751. dataSz = (word32) strlen(data);
  5752. outSz = FOURK_BUF;
  5753. certDer = (byte*)malloc(FOURK_BUF);
  5754. if (certDer == NULL)
  5755. return -207;
  5756. keyDer = (byte*)malloc(FOURK_BUF);
  5757. if (keyDer == NULL) {
  5758. free(certDer);
  5759. return -208;
  5760. }
  5761. out = (byte*)malloc(FOURK_BUF);
  5762. if (out == NULL) {
  5763. free(certDer);
  5764. free(keyDer);
  5765. return -209;
  5766. }
  5767. /* read in DER cert of recipient, into cert of size certSz */
  5768. file = fopen(clientCert, "rb");
  5769. if (!file) {
  5770. free(certDer);
  5771. free(keyDer);
  5772. free(out);
  5773. err_sys("can't open ./certs/client-cert.der, "
  5774. "Please run from wolfSSL home dir", -44);
  5775. return -44;
  5776. }
  5777. certDerSz = (word32)fread(certDer, 1, FOURK_BUF, file);
  5778. fclose(file);
  5779. file = fopen(clientKey, "rb");
  5780. if (!file) {
  5781. free(certDer);
  5782. free(keyDer);
  5783. free(out);
  5784. err_sys("can't open ./certs/client-key.der, "
  5785. "Please run from wolfSSL home dir", -45);
  5786. return -45;
  5787. }
  5788. keyDerSz = (word32)fread(keyDer, 1, FOURK_BUF, file);
  5789. fclose(file);
  5790. ret = wc_InitRng(&rng);
  5791. if (ret != 0) {
  5792. free(certDer);
  5793. free(keyDer);
  5794. free(out);
  5795. return -210;
  5796. }
  5797. senderNonce[0] = 0x04;
  5798. senderNonce[1] = PKCS7_NONCE_SZ;
  5799. ret = wc_RNG_GenerateBlock(&rng, &senderNonce[2], PKCS7_NONCE_SZ);
  5800. if (ret != 0) {
  5801. free(certDer);
  5802. free(keyDer);
  5803. free(out);
  5804. return -211;
  5805. }
  5806. wc_PKCS7_InitWithCert(&msg, certDer, certDerSz);
  5807. msg.privateKey = keyDer;
  5808. msg.privateKeySz = keyDerSz;
  5809. msg.content = (byte*)data;
  5810. msg.contentSz = dataSz;
  5811. msg.hashOID = SHAh;
  5812. msg.encryptOID = RSAk;
  5813. msg.signedAttribs = attribs;
  5814. msg.signedAttribsSz = sizeof(attribs)/sizeof(PKCS7Attrib);
  5815. msg.rng = &rng;
  5816. {
  5817. Sha sha;
  5818. byte digest[SHA_DIGEST_SIZE];
  5819. int i,j;
  5820. transId[0] = 0x13;
  5821. transId[1] = SHA_DIGEST_SIZE * 2;
  5822. ret = wc_InitSha(&sha);
  5823. if (ret != 0) {
  5824. free(certDer);
  5825. free(keyDer);
  5826. free(out);
  5827. return -4003;
  5828. }
  5829. wc_ShaUpdate(&sha, msg.publicKey, msg.publicKeySz);
  5830. wc_ShaFinal(&sha, digest);
  5831. for (i = 0, j = 2; i < SHA_DIGEST_SIZE; i++, j += 2) {
  5832. snprintf((char*)&transId[j], 3, "%02x", digest[i]);
  5833. }
  5834. }
  5835. ret = wc_PKCS7_EncodeSignedData(&msg, out, outSz);
  5836. if (ret < 0) {
  5837. free(certDer);
  5838. free(keyDer);
  5839. free(out);
  5840. wc_PKCS7_Free(&msg);
  5841. return -212;
  5842. }
  5843. else
  5844. outSz = ret;
  5845. /* write PKCS#7 to output file for more testing */
  5846. file = fopen("./pkcs7signedData.der", "wb");
  5847. if (!file) {
  5848. free(certDer);
  5849. free(keyDer);
  5850. free(out);
  5851. wc_PKCS7_Free(&msg);
  5852. return -213;
  5853. }
  5854. ret = (int)fwrite(out, 1, outSz, file);
  5855. fclose(file);
  5856. if (ret != (int)outSz) {
  5857. free(certDer);
  5858. free(keyDer);
  5859. free(out);
  5860. wc_PKCS7_Free(&msg);
  5861. return -218;
  5862. }
  5863. wc_PKCS7_Free(&msg);
  5864. wc_PKCS7_InitWithCert(&msg, NULL, 0);
  5865. ret = wc_PKCS7_VerifySignedData(&msg, out, outSz);
  5866. if (ret < 0) {
  5867. free(certDer);
  5868. free(keyDer);
  5869. free(out);
  5870. wc_PKCS7_Free(&msg);
  5871. return -214;
  5872. }
  5873. if (msg.singleCert == NULL || msg.singleCertSz == 0) {
  5874. free(certDer);
  5875. free(keyDer);
  5876. free(out);
  5877. wc_PKCS7_Free(&msg);
  5878. return -215;
  5879. }
  5880. file = fopen("./pkcs7cert.der", "wb");
  5881. if (!file) {
  5882. free(certDer);
  5883. free(keyDer);
  5884. free(out);
  5885. wc_PKCS7_Free(&msg);
  5886. return -216;
  5887. }
  5888. ret = (int)fwrite(msg.singleCert, 1, msg.singleCertSz, file);
  5889. fclose(file);
  5890. free(certDer);
  5891. free(keyDer);
  5892. free(out);
  5893. wc_PKCS7_Free(&msg);
  5894. wc_FreeRng(&rng);
  5895. if (ret > 0)
  5896. return 0;
  5897. return ret;
  5898. }
  5899. #endif /* HAVE_PKCS7 */
  5900. #endif /* NO_CRYPT_TEST */