benchmark.c 195 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340
  1. /* benchmark.c
  2. *
  3. * Copyright (C) 2006-2020 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* wolfCrypt benchmark */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #ifndef WOLFSSL_USER_SETTINGS
  26. #include <wolfssl/options.h>
  27. #endif
  28. #include <wolfssl/wolfcrypt/settings.h>
  29. #include <wolfssl/version.h>
  30. #include <wolfssl/wolfcrypt/wc_port.h>
  31. /* Macro to disable benchmark */
  32. #ifndef NO_CRYPT_BENCHMARK
  33. /* only for stack size check */
  34. #ifdef HAVE_STACK_SIZE
  35. #include <wolfssl/ssl.h>
  36. #include <wolfssl/test.h>
  37. #endif
  38. #ifdef USE_FLAT_BENCHMARK_H
  39. #include "benchmark.h"
  40. #else
  41. #include "wolfcrypt/benchmark/benchmark.h"
  42. #endif
  43. /* printf mappings */
  44. #ifdef FREESCALE_MQX
  45. #include <mqx.h>
  46. /* see wc_port.h for fio.h and nio.h includes */
  47. #elif defined(FREESCALE_KSDK_1_3)
  48. #include "fsl_debug_console.h"
  49. #include "fsl_os_abstraction.h"
  50. #undef printf
  51. #define printf PRINTF
  52. #elif defined(WOLFSSL_DEOS)
  53. #include <deos.h>
  54. #undef printf
  55. #define printf printx
  56. #elif defined(MICRIUM)
  57. #include <bsp_ser.h>
  58. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  59. #undef printf
  60. #define printf BSP_Ser_Printf
  61. #elif defined(WOLFSSL_ZEPHYR)
  62. #include <stdio.h>
  63. #define BENCH_EMBEDDED
  64. #define printf printfk
  65. static int printfk(const char *fmt, ...)
  66. {
  67. int ret;
  68. char line[150];
  69. va_list ap;
  70. va_start(ap, fmt);
  71. ret = vsnprintf(line, sizeof(line), fmt, ap);
  72. line[sizeof(line)-1] = '\0';
  73. printk("%s", line);
  74. va_end(ap);
  75. return ret;
  76. }
  77. #elif defined(WOLFSSL_TELIT_M2MB)
  78. #include <stdarg.h>
  79. #include <stdio.h>
  80. #include <string.h>
  81. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  82. /* remap printf */
  83. #undef printf
  84. #define printf M2M_LOG_INFO
  85. /* OS requires occasional sleep() */
  86. #ifndef TEST_SLEEP_MS
  87. #define TEST_SLEEP_MS 50
  88. #endif
  89. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  90. /* don't use file system for these tests, since ./certs dir isn't loaded */
  91. #undef NO_FILESYSTEM
  92. #define NO_FILESYSTEM
  93. #else
  94. #if defined(XMALLOC_USER) || defined(FREESCALE_MQX)
  95. /* MQX classic needs for EXIT_FAILURE */
  96. #include <stdlib.h> /* we're using malloc / free direct here */
  97. #endif
  98. #ifndef STRING_USER
  99. #include <string.h>
  100. #include <stdio.h>
  101. #endif
  102. /* enable way for customer to override test/bench printf */
  103. #ifdef XPRINTF
  104. #undef printf
  105. #define printf XPRINTF
  106. #endif
  107. #endif
  108. #include <wolfssl/wolfcrypt/memory.h>
  109. #include <wolfssl/wolfcrypt/random.h>
  110. #include <wolfssl/wolfcrypt/des3.h>
  111. #include <wolfssl/wolfcrypt/arc4.h>
  112. #include <wolfssl/wolfcrypt/hc128.h>
  113. #include <wolfssl/wolfcrypt/rabbit.h>
  114. #include <wolfssl/wolfcrypt/chacha.h>
  115. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  116. #include <wolfssl/wolfcrypt/aes.h>
  117. #include <wolfssl/wolfcrypt/poly1305.h>
  118. #include <wolfssl/wolfcrypt/camellia.h>
  119. #include <wolfssl/wolfcrypt/md5.h>
  120. #include <wolfssl/wolfcrypt/sha.h>
  121. #include <wolfssl/wolfcrypt/sha256.h>
  122. #include <wolfssl/wolfcrypt/sha512.h>
  123. #include <wolfssl/wolfcrypt/sha3.h>
  124. #include <wolfssl/wolfcrypt/rsa.h>
  125. #include <wolfssl/wolfcrypt/asn.h>
  126. #include <wolfssl/wolfcrypt/ripemd.h>
  127. #include <wolfssl/wolfcrypt/cmac.h>
  128. #ifndef NO_HMAC
  129. #include <wolfssl/wolfcrypt/hmac.h>
  130. #endif
  131. #ifndef NO_PWDBASED
  132. #include <wolfssl/wolfcrypt/pwdbased.h>
  133. #endif
  134. #ifdef HAVE_ECC
  135. #include <wolfssl/wolfcrypt/ecc.h>
  136. #endif
  137. #ifdef HAVE_IDEA
  138. #include <wolfssl/wolfcrypt/idea.h>
  139. #endif
  140. #ifdef HAVE_CURVE25519
  141. #include <wolfssl/wolfcrypt/curve25519.h>
  142. #endif
  143. #ifdef HAVE_ED25519
  144. #include <wolfssl/wolfcrypt/ed25519.h>
  145. #endif
  146. #ifdef HAVE_CURVE448
  147. #include <wolfssl/wolfcrypt/curve448.h>
  148. #endif
  149. #ifdef HAVE_ED448
  150. #include <wolfssl/wolfcrypt/ed448.h>
  151. #endif
  152. #include <wolfssl/wolfcrypt/dh.h>
  153. #ifdef HAVE_NTRU
  154. #include "libntruencrypt/ntru_crypto.h"
  155. #endif
  156. #include <wolfssl/wolfcrypt/random.h>
  157. #include <wolfssl/wolfcrypt/error-crypt.h>
  158. #include <wolfssl/wolfcrypt/types.h>
  159. #ifdef WOLF_CRYPTO_CB
  160. #include <wolfssl/wolfcrypt/cryptocb.h>
  161. #ifdef HAVE_INTEL_QA_SYNC
  162. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  163. #endif
  164. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  165. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  166. #endif
  167. #endif
  168. #ifdef WOLFSSL_ASYNC_CRYPT
  169. #include <wolfssl/wolfcrypt/async.h>
  170. #endif
  171. #ifdef WOLFSSL_STATIC_MEMORY
  172. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  173. #else
  174. #define HEAP_HINT NULL
  175. #endif /* WOLFSSL_STATIC_MEMORY */
  176. #ifndef EXIT_FAILURE
  177. #define EXIT_FAILURE 1
  178. #endif
  179. /* optional macro to add sleep between tests */
  180. #ifndef TEST_SLEEP
  181. /* stub the sleep macro */
  182. #define TEST_SLEEP()
  183. #endif
  184. /* Bit values for each algorithm that is able to be benchmarked.
  185. * Common grouping of algorithms also.
  186. * Each algorithm has a unique value for its type e.g. cipher.
  187. */
  188. /* Cipher algorithms. */
  189. #define BENCH_AES_CBC 0x00000001
  190. #define BENCH_AES_GCM 0x00000002
  191. #define BENCH_AES_ECB 0x00000004
  192. #define BENCH_AES_XTS 0x00000008
  193. #define BENCH_AES_CTR 0x00000010
  194. #define BENCH_AES_CCM 0x00000020
  195. #define BENCH_CAMELLIA 0x00000100
  196. #define BENCH_ARC4 0x00000200
  197. #define BENCH_HC128 0x00000400
  198. #define BENCH_RABBIT 0x00000800
  199. #define BENCH_CHACHA20 0x00001000
  200. #define BENCH_CHACHA20_POLY1305 0x00002000
  201. #define BENCH_DES 0x00004000
  202. #define BENCH_IDEA 0x00008000
  203. #define BENCH_AES_CFB 0x00010000
  204. #define BENCH_AES_OFB 0x00020000
  205. /* Digest algorithms. */
  206. #define BENCH_MD5 0x00000001
  207. #define BENCH_POLY1305 0x00000002
  208. #define BENCH_SHA 0x00000004
  209. #define BENCH_SHA224 0x00000010
  210. #define BENCH_SHA256 0x00000020
  211. #define BENCH_SHA384 0x00000040
  212. #define BENCH_SHA512 0x00000080
  213. #define BENCH_SHA2 (BENCH_SHA224 | BENCH_SHA256 | \
  214. BENCH_SHA384 | BENCH_SHA512)
  215. #define BENCH_SHA3_224 0x00000100
  216. #define BENCH_SHA3_256 0x00000200
  217. #define BENCH_SHA3_384 0x00000400
  218. #define BENCH_SHA3_512 0x00000800
  219. #define BENCH_SHA3 (BENCH_SHA3_224 | BENCH_SHA3_256 | \
  220. BENCH_SHA3_384 | BENCH_SHA3_512)
  221. #define BENCH_RIPEMD 0x00001000
  222. #define BENCH_BLAKE2B 0x00002000
  223. #define BENCH_BLAKE2S 0x00004000
  224. /* MAC algorithms. */
  225. #define BENCH_CMAC 0x00000001
  226. #define BENCH_HMAC_MD5 0x00000002
  227. #define BENCH_HMAC_SHA 0x00000004
  228. #define BENCH_HMAC_SHA224 0x00000010
  229. #define BENCH_HMAC_SHA256 0x00000020
  230. #define BENCH_HMAC_SHA384 0x00000040
  231. #define BENCH_HMAC_SHA512 0x00000080
  232. #define BENCH_HMAC (BENCH_HMAC_MD5 | BENCH_HMAC_SHA | \
  233. BENCH_HMAC_SHA224 | BENCH_HMAC_SHA256 | \
  234. BENCH_HMAC_SHA384 | BENCH_HMAC_SHA512)
  235. #define BENCH_PBKDF2 0x00000100
  236. /* Asymmetric algorithms. */
  237. #define BENCH_RSA_KEYGEN 0x00000001
  238. #define BENCH_RSA 0x00000002
  239. #define BENCH_RSA_SZ 0x00000004
  240. #define BENCH_DH 0x00000010
  241. #define BENCH_NTRU 0x00000100
  242. #define BENCH_NTRU_KEYGEN 0x00000200
  243. #define BENCH_ECC_MAKEKEY 0x00001000
  244. #define BENCH_ECC 0x00002000
  245. #define BENCH_ECC_ENCRYPT 0x00004000
  246. #define BENCH_CURVE25519_KEYGEN 0x00010000
  247. #define BENCH_CURVE25519_KA 0x00020000
  248. #define BENCH_ED25519_KEYGEN 0x00040000
  249. #define BENCH_ED25519_SIGN 0x00080000
  250. #define BENCH_CURVE448_KEYGEN 0x00100000
  251. #define BENCH_CURVE448_KA 0x00200000
  252. #define BENCH_ED448_KEYGEN 0x00400000
  253. #define BENCH_ED448_SIGN 0x00800000
  254. /* Other */
  255. #define BENCH_RNG 0x00000001
  256. #define BENCH_SCRYPT 0x00000002
  257. /* Benchmark all compiled in algorithms.
  258. * When 1, ignore other benchmark algorithm values.
  259. * 0, only benchmark algorithm values set.
  260. */
  261. static int bench_all = 1;
  262. /* Cipher algorithms to benchmark. */
  263. static int bench_cipher_algs = 0;
  264. /* Digest algorithms to benchmark. */
  265. static int bench_digest_algs = 0;
  266. /* MAC algorithms to benchmark. */
  267. static int bench_mac_algs = 0;
  268. /* Asymmetric algorithms to benchmark. */
  269. static int bench_asym_algs = 0;
  270. /* Other cryptographic algorithms to benchmark. */
  271. static int bench_other_algs = 0;
  272. #if !defined(WOLFSSL_BENCHMARK_ALL) && !defined(NO_MAIN_DRIVER)
  273. /* The mapping of command line option to bit values. */
  274. typedef struct bench_alg {
  275. /* Command line option string. */
  276. const char* str;
  277. /* Bit values to set. */
  278. int val;
  279. } bench_alg;
  280. #ifndef MAIN_NO_ARGS
  281. /* All recognized cipher algorithm choosing command line options. */
  282. static const bench_alg bench_cipher_opt[] = {
  283. { "-cipher", -1 },
  284. #ifdef HAVE_AES_CBC
  285. { "-aes-cbc", BENCH_AES_CBC },
  286. #endif
  287. #ifdef HAVE_AESGCM
  288. { "-aes-gcm", BENCH_AES_GCM },
  289. #endif
  290. #ifdef WOLFSSL_AES_DIRECT
  291. { "-aes-ecb", BENCH_AES_ECB },
  292. #endif
  293. #ifdef WOLFSSL_AES_XTS
  294. { "-aes-xts", BENCH_AES_XTS },
  295. #endif
  296. #ifdef WOLFSSL_AES_CFB
  297. { "-aes-cfb", BENCH_AES_CFB },
  298. #endif
  299. #ifdef WOLFSSL_AES_OFB
  300. { "-aes-ofb", BENCH_AES_OFB },
  301. #endif
  302. #ifdef WOLFSSL_AES_COUNTER
  303. { "-aes-ctr", BENCH_AES_CTR },
  304. #endif
  305. #ifdef HAVE_AESCCM
  306. { "-aes-ccm", BENCH_AES_CCM },
  307. #endif
  308. #ifdef HAVE_CAMELLIA
  309. { "-camellia", BENCH_CAMELLIA },
  310. #endif
  311. #ifndef NO_RC4
  312. { "-arc4", BENCH_ARC4 },
  313. #endif
  314. #ifdef HAVE_HC128
  315. { "-hc128", BENCH_HC128 },
  316. #endif
  317. #ifndef NO_RABBIT
  318. { "-rabbit", BENCH_RABBIT },
  319. #endif
  320. #ifdef HAVE_CHACHA
  321. { "-chacha20", BENCH_CHACHA20 },
  322. #endif
  323. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  324. { "-chacha20-poly1305", BENCH_CHACHA20_POLY1305 },
  325. #endif
  326. #ifndef NO_DES3
  327. { "-des", BENCH_DES },
  328. #endif
  329. #ifdef HAVE_IDEA
  330. { "-idea", BENCH_IDEA },
  331. #endif
  332. { NULL, 0}
  333. };
  334. /* All recognized digest algorithm choosing command line options. */
  335. static const bench_alg bench_digest_opt[] = {
  336. { "-digest", -1 },
  337. #ifndef NO_MD5
  338. { "-md5", BENCH_MD5 },
  339. #endif
  340. #ifdef HAVE_POLY1305
  341. { "-poly1305", BENCH_POLY1305 },
  342. #endif
  343. #ifndef NO_SHA
  344. { "-sha", BENCH_SHA },
  345. #endif
  346. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256) || defined(WOLFSSL_SHA384) \
  347. || defined(WOLFSSL_SHA512)
  348. { "-sha2", BENCH_SHA2 },
  349. #endif
  350. #ifdef WOLFSSL_SHA224
  351. { "-sha224", BENCH_SHA224 },
  352. #endif
  353. #ifndef NO_SHA256
  354. { "-sha256", BENCH_SHA256 },
  355. #endif
  356. #ifdef WOLFSSL_SHA384
  357. { "-sha384", BENCH_SHA384 },
  358. #endif
  359. #ifdef WOLFSSL_SHA512
  360. { "-sha512", BENCH_SHA512 },
  361. #endif
  362. #ifdef WOLFSSL_SHA3
  363. { "-sha3", BENCH_SHA3 },
  364. #ifndef WOLFSSL_NOSHA3_224
  365. { "-sha3-224", BENCH_SHA3_224 },
  366. #endif
  367. #ifndef WOLFSSL_NOSHA3_256
  368. { "-sha3-256", BENCH_SHA3_256 },
  369. #endif
  370. #ifndef WOLFSSL_NOSHA3_384
  371. { "-sha3-384", BENCH_SHA3_384 },
  372. #endif
  373. #ifndef WOLFSSL_NOSHA3_512
  374. { "-sha3-512", BENCH_SHA3_512 },
  375. #endif
  376. #endif
  377. #ifdef WOLFSSL_RIPEMD
  378. { "-ripemd", BENCH_RIPEMD },
  379. #endif
  380. #ifdef HAVE_BLAKE2
  381. { "-blake2b", BENCH_BLAKE2B },
  382. #endif
  383. #ifdef HAVE_BLAKE2S
  384. { "-blake2s", BENCH_BLAKE2S },
  385. #endif
  386. { NULL, 0}
  387. };
  388. /* All recognized MAC algorithm choosing command line options. */
  389. static const bench_alg bench_mac_opt[] = {
  390. { "-mac", -1 },
  391. #ifdef WOLFSSL_CMAC
  392. { "-cmac", BENCH_CMAC },
  393. #endif
  394. #ifndef NO_HMAC
  395. { "-hmac", BENCH_HMAC },
  396. #ifndef NO_MD5
  397. { "-hmac-md5", BENCH_HMAC_MD5 },
  398. #endif
  399. #ifndef NO_SHA
  400. { "-hmac-sha", BENCH_HMAC_SHA },
  401. #endif
  402. #ifdef WOLFSSL_SHA224
  403. { "-hmac-sha224", BENCH_HMAC_SHA224 },
  404. #endif
  405. #ifndef NO_SHA256
  406. { "-hmac-sha256", BENCH_HMAC_SHA256 },
  407. #endif
  408. #ifdef WOLFSSL_SHA384
  409. { "-hmac-sha384", BENCH_HMAC_SHA384 },
  410. #endif
  411. #ifdef WOLFSSL_SHA512
  412. { "-hmac-sha512", BENCH_HMAC_SHA512 },
  413. #endif
  414. #ifndef NO_PWDBASED
  415. { "-pbkdf2", BENCH_PBKDF2 },
  416. #endif
  417. #endif
  418. { NULL, 0}
  419. };
  420. /* All recognized asymmetric algorithm choosing command line options. */
  421. static const bench_alg bench_asym_opt[] = {
  422. { "-asym", -1 },
  423. #ifndef NO_RSA
  424. #ifdef WOLFSSL_KEY_GEN
  425. { "-rsa-kg", BENCH_RSA_KEYGEN },
  426. #endif
  427. { "-rsa", BENCH_RSA },
  428. { "-rsa-sz", BENCH_RSA_SZ },
  429. #endif
  430. #ifndef NO_DH
  431. { "-dh", BENCH_DH },
  432. #endif
  433. #ifdef HAVE_NTRU
  434. { "-ntru", BENCH_NTRU },
  435. { "-ntru-kg", BENCH_NTRU_KEYGEN },
  436. #endif
  437. #ifdef HAVE_ECC
  438. { "-ecc-kg", BENCH_ECC_MAKEKEY },
  439. { "-ecc", BENCH_ECC },
  440. #ifdef HAVE_ECC_ENCRYPT
  441. { "-ecc-enc", BENCH_ECC_ENCRYPT },
  442. #endif
  443. #endif
  444. #ifdef HAVE_CURVE25519
  445. { "-curve25519-kg", BENCH_CURVE25519_KEYGEN },
  446. #ifdef HAVE_CURVE25519_SHARED_SECRET
  447. { "-x25519", BENCH_CURVE25519_KA },
  448. #endif
  449. #endif
  450. #ifdef HAVE_ED25519
  451. { "-ed25519-kg", BENCH_ED25519_KEYGEN },
  452. { "-ed25519", BENCH_ED25519_SIGN },
  453. #endif
  454. #ifdef HAVE_CURVE448
  455. { "-curve448-kg", BENCH_CURVE448_KEYGEN },
  456. #ifdef HAVE_CURVE448_SHARED_SECRET
  457. { "-x448", BENCH_CURVE448_KA },
  458. #endif
  459. #endif
  460. #ifdef HAVE_ED448
  461. { "-ed448-kg", BENCH_ED448_KEYGEN },
  462. { "-ed448", BENCH_ED448_SIGN },
  463. #endif
  464. { NULL, 0}
  465. };
  466. /* All recognized other cryptographic algorithm choosing command line options.
  467. */
  468. static const bench_alg bench_other_opt[] = {
  469. { "-other", -1 },
  470. #ifndef WC_NO_RNG
  471. { "-rng", BENCH_RNG },
  472. #endif
  473. #ifdef HAVE_SCRYPT
  474. { "-scrypt", BENCH_SCRYPT },
  475. #endif
  476. { NULL, 0}
  477. };
  478. #endif /* MAIN_NO_ARGS */
  479. #endif /* !WOLFSSL_BENCHMARK_ALL && !NO_MAIN_DRIVER */
  480. #ifdef HAVE_WNR
  481. const char* wnrConfigFile = "wnr-example.conf";
  482. #endif
  483. #if defined(WOLFSSL_MDK_ARM)
  484. extern XFILE wolfSSL_fopen(const char *fname, const char *mode);
  485. #define fopen wolfSSL_fopen
  486. #endif
  487. static int lng_index = 0;
  488. #ifndef NO_MAIN_DRIVER
  489. #ifndef MAIN_NO_ARGS
  490. static const char* bench_Usage_msg1[][16] = {
  491. /* 0 English */
  492. { "-? <num> Help, print this usage\n 0: English, 1: Japanese\n",
  493. "-csv Print terminal output in csv format\n",
  494. "-base10 Display bytes as power of 10 (eg 1 kB = 1000 Bytes)\n",
  495. "-no_aad No additional authentication data passed.\n",
  496. "-dgst_full Full digest operation performed.\n",
  497. "-rsa_sign Measure RSA sign/verify instead of encrypt/decrypt.\n",
  498. "<keySz> -rsa-sz\n Measure RSA <key size> performance.\n",
  499. "-ffhdhe2048 Measure DH using FFDHE 2048-bit parameters.\n",
  500. "-ffhdhe3072 Measure DH using FFDHE 3072-bit parameters.\n",
  501. "-p256 Measure ECC using P-256 curve.\n",
  502. "-p384 Measure ECC using P-384 curve.\n",
  503. "-<alg> Algorithm to benchmark. Available algorithms include:\n",
  504. "-lng <num> Display benchmark result by specified language.\n 0: English, 1: Japanese\n",
  505. "<num> Size of block in bytes\n",
  506. "-threads <num> Number of threads to run\n",
  507. "-print Show benchmark stats summary\n"
  508. },
  509. #ifndef NO_MULTIBYTE_PRINT
  510. /* 1 Japanese */
  511. { "-? <num> ヘルプ, 使い方を表示します。\n 0: 英語、 1: 日本語\n",
  512. "-csv csv 形式で端末に出力します。\n",
  513. "-base10 バイトを10のべき乗で表示します。(例 1 kB = 1000 Bytes)\n",
  514. "-no_aad 追加の認証データを使用しません.\n",
  515. "-dgst_full フルの digest 暗号操作を実施します。\n",
  516. "-rsa_sign 暗号/復号化の代わりに RSA の署名/検証を測定します。\n",
  517. "<keySz> -rsa-sz\n RSA <key size> の性能を測定します。\n",
  518. "-ffhdhe2048 Measure DH using FFDHE 2048-bit parameters.\n",
  519. "-ffhdhe3072 Measure DH using FFDHE 3072-bit parameters.\n",
  520. "-p256 Measure ECC using P-256 curve.\n",
  521. "-p384 Measure ECC using P-384 curve.\n",
  522. "-<alg> アルゴリズムのベンチマークを実施します。\n 利用可能なアルゴリズムは下記を含みます:\n",
  523. "-lng <num> 指定された言語でベンチマーク結果を表示します。\n 0: 英語、 1: 日本語\n",
  524. "<num> ブロックサイズをバイト単位で指定します。\n",
  525. "-threads <num> 実行するスレッド数\n",
  526. "-print ベンチマーク統計の要約を表示する\n"
  527. },
  528. #endif
  529. };
  530. #endif /* MAIN_NO_ARGS */
  531. #endif
  532. static const char* bench_result_words1[][4] = {
  533. { "took", "seconds" , "Cycles per byte", NULL }, /* 0 English */
  534. #ifndef NO_MULTIBYTE_PRINT
  535. { "を" , "秒で処理", "1バイトあたりのサイクル数", NULL }, /* 1 Japanese */
  536. #endif
  537. };
  538. #if !defined(NO_RSA) || defined(WOLFSSL_KEY_GEN) || defined(HAVE_NTRU) || \
  539. defined(HAVE_ECC) || !defined(NO_DH) || defined(HAVE_ECC_ENCRYPT) || \
  540. defined(HAVE_CURVE25519) || defined(HAVE_CURVE25519_SHARED_SECRET) || \
  541. defined(HAVE_ED25519) || defined(HAVE_CURVE448) || \
  542. defined(HAVE_CURVE448_SHARED_SECRET) || defined(HAVE_ED448)
  543. #if defined(HAVE_ECC) || !defined(WOLFSSL_RSA_PUBLIC_ONLY) || \
  544. defined(WOLFSSL_PUBLIC_MP) || !defined(NO_DH)
  545. static const char* bench_desc_words[][9] = {
  546. /* 0 1 2 3 4 5 6 7 8 */
  547. {"public", "private", "key gen", "agree" , "sign", "verify", "encryption", "decryption", NULL}, /* 0 English */
  548. #ifndef NO_MULTIBYTE_PRINT
  549. {"公開鍵", "秘密鍵" ,"鍵生成" , "鍵共有" , "署名", "検証" , "暗号化" , "復号化" , NULL}, /* 1 Japanese */
  550. #endif
  551. };
  552. #endif
  553. #endif
  554. #if defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM) && !defined(WOLFSSL_SGX)
  555. #define HAVE_GET_CYCLES
  556. static WC_INLINE word64 get_intel_cycles(void);
  557. static THREAD_LS_T word64 total_cycles;
  558. #define INIT_CYCLE_COUNTER
  559. #define BEGIN_INTEL_CYCLES total_cycles = get_intel_cycles();
  560. #define END_INTEL_CYCLES total_cycles = get_intel_cycles() - total_cycles;
  561. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  562. #define SHOW_INTEL_CYCLES(b, n, s) \
  563. XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " %s = %6.2f\n", \
  564. bench_result_words1[lng_index][2], \
  565. count == 0 ? 0 : (float)total_cycles / ((word64)count*s))
  566. #define SHOW_INTEL_CYCLES_CSV(b, n, s) \
  567. XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), "%.2f,\n", \
  568. count == 0 ? 0 : (float)total_cycles / ((word64)count*s))
  569. #elif defined(LINUX_CYCLE_COUNT)
  570. #include <linux/perf_event.h>
  571. #include <sys/syscall.h>
  572. #include <unistd.h>
  573. static THREAD_LS_T word64 begin_cycles;
  574. static THREAD_LS_T word64 total_cycles;
  575. static THREAD_LS_T int cycles = -1;
  576. static THREAD_LS_T struct perf_event_attr atr;
  577. #define INIT_CYCLE_COUNTER do { \
  578. atr.type = PERF_TYPE_HARDWARE; \
  579. atr.config = PERF_COUNT_HW_CPU_CYCLES; \
  580. cycles = (int)syscall(__NR_perf_event_open, &atr, 0, -1, -1, 0); \
  581. } while (0);
  582. #define BEGIN_INTEL_CYCLES read(cycles, &begin_cycles, sizeof(begin_cycles));
  583. #define END_INTEL_CYCLES do { \
  584. read(cycles, &total_cycles, sizeof(total_cycles)); \
  585. total_cycles = total_cycles - begin_cycles; \
  586. } while (0);
  587. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  588. #define SHOW_INTEL_CYCLES(b, n, s) \
  589. XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " %s = %6.2f\n", \
  590. bench_result_words1[lng_index][2], \
  591. (float)total_cycles / (count*s))
  592. #define SHOW_INTEL_CYCLES_CSV(b, n, s) \
  593. XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), "%.2f,\n", \
  594. (float)total_cycles / (count*s))
  595. #elif defined(SYNERGY_CYCLE_COUNT)
  596. #include "hal_data.h"
  597. static THREAD_LS_T word64 begin_cycles;
  598. static THREAD_LS_T word64 total_cycles;
  599. #define INIT_CYCLE_COUNTER
  600. #define BEGIN_INTEL_CYCLES begin_cycles = DWT->CYCCNT = 0;
  601. #define END_INTEL_CYCLES total_cycles = DWT->CYCCNT - begin_cycles;
  602. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  603. #define SHOW_INTEL_CYCLES(b, n, s) \
  604. XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " %s = %6.2f\n", \
  605. bench_result_words1[lng_index][2], \
  606. (float)total_cycles / (count*s))
  607. #define SHOW_INTEL_CYCLES_CSV(b, n, s) \
  608. XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), "%.2f,\n", \
  609. (float)total_cycles / (count*s))
  610. #else
  611. #define INIT_CYCLE_COUNTER
  612. #define BEGIN_INTEL_CYCLES
  613. #define END_INTEL_CYCLES
  614. #define SHOW_INTEL_CYCLES(b, n, s) b[XSTRLEN(b)] = '\n'
  615. #define SHOW_INTEL_CYCLES_CSV(b, n, s) b[XSTRLEN(b)] = '\n'
  616. #endif
  617. /* determine benchmark buffer to use (if NO_FILESYSTEM) */
  618. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  619. !defined(USE_CERT_BUFFERS_3072)
  620. #define USE_CERT_BUFFERS_2048 /* default to 2048 */
  621. #endif
  622. #if defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048) || \
  623. defined(USE_CERT_BUFFERS_3072) || !defined(NO_DH)
  624. /* include test cert and key buffers for use with NO_FILESYSTEM */
  625. #include <wolfssl/certs_test.h>
  626. #endif
  627. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  628. #include <wolfssl/wolfcrypt/blake2.h>
  629. #endif
  630. #ifdef _MSC_VER
  631. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  632. #pragma warning(disable: 4996)
  633. #endif
  634. #ifdef WOLFSSL_CURRTIME_REMAP
  635. #define current_time WOLFSSL_CURRTIME_REMAP
  636. #elif !defined(HAVE_STACK_SIZE)
  637. double current_time(int);
  638. #endif
  639. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND) && \
  640. !defined(HAVE_STACK_SIZE)
  641. #ifdef __cplusplus
  642. extern "C" {
  643. #endif
  644. WOLFSSL_API int wolfSSL_Debugging_ON(void);
  645. WOLFSSL_API void wolfSSL_Debugging_OFF(void);
  646. #ifdef __cplusplus
  647. } /* extern "C" */
  648. #endif
  649. #endif
  650. #if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || !defined(NO_DH) \
  651. || defined(WOLFSSL_KEY_GEN) || defined(HAVE_ECC) \
  652. || defined(HAVE_CURVE25519) || defined(HAVE_ED25519) \
  653. || defined(HAVE_CURVE448) || defined(HAVE_ED448)
  654. #define HAVE_LOCAL_RNG
  655. static THREAD_LS_T WC_RNG gRng;
  656. #endif
  657. #if defined(HAVE_ED25519) || defined(HAVE_CURVE25519) || \
  658. defined(HAVE_CURVE448) || defined(HAVE_ED448) || \
  659. defined(HAVE_ECC) || defined(HAVE_NTRU) || !defined(NO_DH) || \
  660. !defined(NO_RSA) || defined(HAVE_SCRYPT)
  661. #define BENCH_ASYM
  662. #endif
  663. #if defined(BENCH_ASYM)
  664. #if defined(HAVE_ECC) || !defined(WOLFSSL_RSA_PUBLIC_ONLY) || \
  665. defined(WOLFSSL_PUBLIC_MP) || !defined(NO_DH)
  666. static const char* bench_result_words2[][5] = {
  667. { "ops took", "sec" , "avg" , "ops/sec", NULL }, /* 0 English */
  668. #ifndef NO_MULTIBYTE_PRINT
  669. { "回処理を", "秒で実施", "平均", "処理/秒", NULL }, /* 1 Japanese */
  670. #endif
  671. };
  672. #endif
  673. #endif
  674. /* Asynchronous helper macros */
  675. static THREAD_LS_T int devId = INVALID_DEVID;
  676. #ifdef WOLFSSL_ASYNC_CRYPT
  677. static WOLF_EVENT_QUEUE eventQueue;
  678. #define BENCH_ASYNC_GET_DEV(obj) (&(obj)->asyncDev)
  679. #define BENCH_ASYNC_GET_NAME(doAsync) (doAsync) ? "HW" : "SW"
  680. #define BENCH_MAX_PENDING (WOLF_ASYNC_MAX_PENDING)
  681. #ifndef WC_NO_ASYNC_THREADING
  682. typedef struct ThreadData {
  683. pthread_t thread_id;
  684. } ThreadData;
  685. static ThreadData* g_threadData;
  686. static int g_threadCount;
  687. #endif
  688. static int bench_async_check(int* ret, WC_ASYNC_DEV* asyncDev,
  689. int callAgain, int* times, int limit, int* pending)
  690. {
  691. int allowNext = 0;
  692. /* this state can be set from a different thread */
  693. WOLF_EVENT_STATE state = asyncDev->event.state;
  694. /* if algo doesn't require calling again then use this flow */
  695. if (state == WOLF_EVENT_STATE_DONE) {
  696. if (callAgain) {
  697. /* needs called again, so allow it and handle completion in bench_async_handle */
  698. allowNext = 1;
  699. }
  700. else {
  701. *ret = asyncDev->event.ret;
  702. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  703. (*times)++;
  704. if (*pending > 0) /* to support case where async blocks */
  705. (*pending)--;
  706. if ((*times + *pending) < limit)
  707. allowNext = 1;
  708. }
  709. }
  710. /* if slot is available and we haven't reached limit, start another */
  711. else if (state == WOLF_EVENT_STATE_READY && (*times + *pending) < limit) {
  712. allowNext = 1;
  713. }
  714. return allowNext;
  715. }
  716. static int bench_async_handle(int* ret, WC_ASYNC_DEV* asyncDev,
  717. int callAgain, int* times, int* pending)
  718. {
  719. WOLF_EVENT_STATE state = asyncDev->event.state;
  720. if (*ret == WC_PENDING_E) {
  721. if (state == WOLF_EVENT_STATE_DONE) {
  722. *ret = asyncDev->event.ret;
  723. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  724. (*times)++;
  725. (*pending)--;
  726. }
  727. else {
  728. (*pending)++;
  729. *ret = wc_AsyncHandle(asyncDev, &eventQueue,
  730. callAgain ? WC_ASYNC_FLAG_CALL_AGAIN : WC_ASYNC_FLAG_NONE);
  731. }
  732. }
  733. else if (*ret >= 0) {
  734. *ret = asyncDev->event.ret;
  735. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  736. (*times)++;
  737. if (*pending > 0) /* to support case where async blocks */
  738. (*pending)--;
  739. }
  740. return (*ret >= 0) ? 1 : 0;
  741. }
  742. static WC_INLINE int bench_async_poll(int* pending)
  743. {
  744. int ret, asyncDone = 0;
  745. ret = wolfAsync_EventQueuePoll(&eventQueue, NULL, NULL, 0,
  746. WOLF_POLL_FLAG_CHECK_HW, &asyncDone);
  747. if (ret != 0) {
  748. printf("Async poll failed %d\n", ret);
  749. return ret;
  750. }
  751. if (asyncDone == 0) {
  752. #ifndef WC_NO_ASYNC_THREADING
  753. /* give time to other threads */
  754. wc_AsyncThreadYield();
  755. #endif
  756. }
  757. (void)pending;
  758. return asyncDone;
  759. }
  760. #else
  761. #define BENCH_MAX_PENDING (1)
  762. #define BENCH_ASYNC_GET_NAME(doAsync) ""
  763. #define BENCH_ASYNC_GET_DEV(obj) NULL
  764. static WC_INLINE int bench_async_check(int* ret, void* asyncDev,
  765. int callAgain, int* times, int limit, int* pending)
  766. {
  767. (void)ret;
  768. (void)asyncDev;
  769. (void)callAgain;
  770. (void)times;
  771. (void)limit;
  772. (void)pending;
  773. return 1;
  774. }
  775. static WC_INLINE int bench_async_handle(int* ret, void* asyncDev,
  776. int callAgain, int* times, int* pending)
  777. {
  778. (void)asyncDev;
  779. (void)callAgain;
  780. (void)pending;
  781. if (*ret >= 0) {
  782. /* operation completed */
  783. (*times)++;
  784. return 1;
  785. }
  786. return 0;
  787. }
  788. #define bench_async_poll(p)
  789. #endif /* WOLFSSL_ASYNC_CRYPT */
  790. /* maximum runtime for each benchmark */
  791. #define BENCH_MIN_RUNTIME_SEC 1.0f
  792. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  793. #define AES_AUTH_ADD_SZ 13
  794. #define AES_AUTH_TAG_SZ 16
  795. #define BENCH_CIPHER_ADD AES_AUTH_TAG_SZ
  796. static word32 aesAuthAddSz = AES_AUTH_ADD_SZ;
  797. #endif
  798. #ifndef BENCH_CIPHER_ADD
  799. #define BENCH_CIPHER_ADD 0
  800. #endif
  801. /* use kB instead of mB for embedded benchmarking */
  802. #ifdef BENCH_EMBEDDED
  803. enum BenchmarkBounds {
  804. scryptCnt = 1,
  805. ntimes = 2,
  806. genTimes = BENCH_MAX_PENDING,
  807. agreeTimes = 2
  808. };
  809. static int numBlocks = 25; /* how many kB to test (en/de)cryption */
  810. static word32 bench_size = (1024ul);
  811. #else
  812. enum BenchmarkBounds {
  813. scryptCnt = 10,
  814. ntimes = 100,
  815. genTimes = BENCH_MAX_PENDING, /* must be at least BENCH_MAX_PENDING */
  816. agreeTimes = 100
  817. };
  818. static int numBlocks = 5; /* how many megs to test (en/de)cryption */
  819. static word32 bench_size = (1024*1024ul);
  820. #endif
  821. static int base2 = 1;
  822. static int digest_stream = 1;
  823. #ifndef NO_RSA
  824. /* Don't measure RSA sign/verify by default */
  825. static int rsa_sign_verify = 0;
  826. #endif
  827. #ifndef NO_DH
  828. /* Use the FFDHE parameters */
  829. static int use_ffdhe = 0;
  830. #endif
  831. /* Don't print out in CSV format by default */
  832. static int csv_format = 0;
  833. #ifdef BENCH_ASYM
  834. static int csv_header_count = 0;
  835. #endif
  836. /* for compatibility */
  837. #define BENCH_SIZE bench_size
  838. /* globals for cipher tests */
  839. static THREAD_LS_T byte* bench_plain = NULL;
  840. static THREAD_LS_T byte* bench_cipher = NULL;
  841. static const XGEN_ALIGN byte bench_key_buf[] =
  842. {
  843. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  844. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  845. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67,
  846. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  847. };
  848. static const XGEN_ALIGN byte bench_iv_buf[] =
  849. {
  850. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  851. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  852. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  853. };
  854. static THREAD_LS_T byte* bench_key = NULL;
  855. static THREAD_LS_T byte* bench_iv = NULL;
  856. #ifdef WOLFSSL_STATIC_MEMORY
  857. #ifdef BENCH_EMBEDDED
  858. static byte gBenchMemory[50000];
  859. #else
  860. static byte gBenchMemory[400000];
  861. #endif
  862. #endif
  863. /* This code handles cases with systems where static (non cost) ram variables
  864. aren't properly initialized with data */
  865. static int gBenchStaticInit = 0;
  866. static void benchmark_static_init(void)
  867. {
  868. if (gBenchStaticInit == 0) {
  869. gBenchStaticInit = 1;
  870. /* Init static variables */
  871. bench_all = 1;
  872. #ifdef BENCH_EMBEDDED
  873. numBlocks = 25; /* how many kB to test (en/de)cryption */
  874. bench_size = (1024ul);
  875. #else
  876. numBlocks = 5; /* how many megs to test (en/de)cryption */
  877. bench_size = (1024*1024ul);
  878. #endif
  879. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  880. aesAuthAddSz = AES_AUTH_ADD_SZ;
  881. #endif
  882. base2 = 1;
  883. digest_stream = 1;
  884. }
  885. }
  886. /******************************************************************************/
  887. /* Begin Stats Functions */
  888. /******************************************************************************/
  889. static int gPrintStats = 0;
  890. typedef enum bench_stat_type {
  891. BENCH_STAT_ASYM,
  892. BENCH_STAT_SYM,
  893. } bench_stat_type_t;
  894. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  895. typedef struct bench_stats {
  896. struct bench_stats* next;
  897. struct bench_stats* prev;
  898. const char* algo;
  899. const char* desc;
  900. double perfsec;
  901. int strength;
  902. int doAsync;
  903. int finishCount;
  904. bench_stat_type_t type;
  905. int lastRet;
  906. const char* perftype;
  907. } bench_stats_t;
  908. static bench_stats_t* bench_stats_head;
  909. static bench_stats_t* bench_stats_tail;
  910. static pthread_mutex_t bench_lock = PTHREAD_MUTEX_INITIALIZER;
  911. static bench_stats_t* bench_stats_add(bench_stat_type_t type,
  912. const char* algo, int strength, const char* desc, int doAsync,
  913. double perfsec, const char* perftype, int ret)
  914. {
  915. bench_stats_t* bstat;
  916. /* protect bench_stats_head and bench_stats_tail access */
  917. pthread_mutex_lock(&bench_lock);
  918. /* locate existing in list */
  919. for (bstat = bench_stats_head; bstat != NULL; bstat = bstat->next) {
  920. /* match based on algo, strength and desc */
  921. if (bstat->algo == algo && bstat->strength == strength && bstat->desc == desc && bstat->doAsync == doAsync) {
  922. break;
  923. }
  924. }
  925. if (bstat == NULL) {
  926. /* allocate new and put on list */
  927. bstat = (bench_stats_t*)XMALLOC(sizeof(bench_stats_t), NULL, DYNAMIC_TYPE_INFO);
  928. if (bstat) {
  929. XMEMSET(bstat, 0, sizeof(bench_stats_t));
  930. /* add to list */
  931. bstat->next = NULL;
  932. if (bench_stats_tail == NULL) {
  933. bench_stats_head = bstat;
  934. }
  935. else {
  936. bench_stats_tail->next = bstat;
  937. bstat->prev = bench_stats_tail;
  938. }
  939. bench_stats_tail = bstat; /* add to the end either way */
  940. }
  941. }
  942. if (bstat) {
  943. bstat->type = type;
  944. bstat->algo = algo;
  945. bstat->strength = strength;
  946. bstat->desc = desc;
  947. bstat->doAsync = doAsync;
  948. bstat->perfsec += perfsec;
  949. bstat->finishCount++;
  950. bstat->perftype = perftype;
  951. if (bstat->lastRet > ret)
  952. bstat->lastRet = ret; /* track last error */
  953. pthread_mutex_unlock(&bench_lock);
  954. /* wait until remaining are complete */
  955. while (bstat->finishCount < g_threadCount) {
  956. wc_AsyncThreadYield();
  957. }
  958. }
  959. else {
  960. pthread_mutex_unlock(&bench_lock);
  961. }
  962. return bstat;
  963. }
  964. void bench_stats_print(void)
  965. {
  966. bench_stats_t* bstat;
  967. /* protect bench_stats_head and bench_stats_tail access */
  968. pthread_mutex_lock(&bench_lock);
  969. for (bstat = bench_stats_head; bstat != NULL; ) {
  970. if (bstat->type == BENCH_STAT_SYM) {
  971. printf("%-16s%s %8.3f %s/s\n", bstat->desc,
  972. BENCH_ASYNC_GET_NAME(bstat->doAsync), bstat->perfsec,
  973. base2 ? "MB" : "mB");
  974. }
  975. else {
  976. printf("%-5s %4d %-9s %s %.3f ops/sec\n",
  977. bstat->algo, bstat->strength, bstat->desc,
  978. BENCH_ASYNC_GET_NAME(bstat->doAsync), bstat->perfsec);
  979. }
  980. bstat = bstat->next;
  981. }
  982. pthread_mutex_unlock(&bench_lock);
  983. }
  984. #else
  985. typedef struct bench_stats {
  986. const char* algo;
  987. const char* desc;
  988. double perfsec;
  989. const char* perftype;
  990. int strength;
  991. bench_stat_type_t type;
  992. int ret;
  993. } bench_stats_t;
  994. #define MAX_BENCH_STATS 50
  995. static bench_stats_t gStats[MAX_BENCH_STATS];
  996. static int gStatsCount;
  997. static bench_stats_t* bench_stats_add(bench_stat_type_t type,
  998. const char* algo, int strength, const char* desc, int doAsync,
  999. double perfsec, const char* perftype, int ret)
  1000. {
  1001. bench_stats_t* bstat = NULL;
  1002. if (gStatsCount >= MAX_BENCH_STATS)
  1003. return bstat;
  1004. bstat = &gStats[gStatsCount++];
  1005. bstat->algo = algo;
  1006. bstat->desc = desc;
  1007. bstat->perfsec = perfsec;
  1008. bstat->perftype = perftype;
  1009. bstat->strength = strength;
  1010. bstat->type = type;
  1011. bstat->ret = ret;
  1012. (void)doAsync;
  1013. return bstat;
  1014. }
  1015. void bench_stats_print(void)
  1016. {
  1017. int i;
  1018. bench_stats_t* bstat;
  1019. for (i=0; i<gStatsCount; i++) {
  1020. bstat = &gStats[i];
  1021. if (bstat->type == BENCH_STAT_SYM) {
  1022. printf("%-16s %8.3f %s/s\n", bstat->desc, bstat->perfsec,
  1023. base2 ? "MB" : "mB");
  1024. }
  1025. else {
  1026. printf("%-5s %4d %-9s %.3f ops/sec\n",
  1027. bstat->algo, bstat->strength, bstat->desc, bstat->perfsec);
  1028. }
  1029. }
  1030. }
  1031. #endif /* WOLFSSL_ASYNC_CRYPT && !WC_NO_ASYNC_THREADING */
  1032. static WC_INLINE void bench_stats_init(void)
  1033. {
  1034. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  1035. bench_stats_head = NULL;
  1036. bench_stats_tail = NULL;
  1037. #endif
  1038. INIT_CYCLE_COUNTER
  1039. }
  1040. static WC_INLINE void bench_stats_start(int* count, double* start)
  1041. {
  1042. *count = 0;
  1043. *start = current_time(1);
  1044. BEGIN_INTEL_CYCLES
  1045. }
  1046. static WC_INLINE int bench_stats_sym_check(double start)
  1047. {
  1048. return ((current_time(0) - start) < BENCH_MIN_RUNTIME_SEC);
  1049. }
  1050. /* countSz is number of bytes that 1 count represents. Normally bench_size,
  1051. * except for AES direct that operates on AES_BLOCK_SIZE blocks */
  1052. static void bench_stats_sym_finish(const char* desc, int doAsync, int count,
  1053. int countSz, double start, int ret)
  1054. {
  1055. double total, persec = 0, blocks = count;
  1056. const char* blockType;
  1057. char msg[128] = {0};
  1058. const char** word = bench_result_words1[lng_index];
  1059. END_INTEL_CYCLES
  1060. total = current_time(0) - start;
  1061. /* calculate actual bytes */
  1062. blocks *= countSz;
  1063. if (base2) {
  1064. /* determine if we should show as KB or MB */
  1065. if (blocks > (1024ul * 1024ul)) {
  1066. blocks /= (1024ul * 1024ul);
  1067. blockType = "MB";
  1068. }
  1069. else if (blocks > 1024) {
  1070. blocks /= 1024; /* make KB */
  1071. blockType = "KB";
  1072. }
  1073. else {
  1074. blockType = "bytes";
  1075. }
  1076. }
  1077. else {
  1078. /* determine if we should show as kB or mB */
  1079. if (blocks > (1000ul * 1000ul)) {
  1080. blocks /= (1000ul * 1000ul);
  1081. blockType = "mB";
  1082. }
  1083. else if (blocks > 1000) {
  1084. blocks /= 1000; /* make kB */
  1085. blockType = "kB";
  1086. }
  1087. else {
  1088. blockType = "bytes";
  1089. }
  1090. }
  1091. /* caclulcate blocks per second */
  1092. if (total > 0) {
  1093. persec = (1 / total) * blocks;
  1094. }
  1095. /* format and print to terminal */
  1096. if (csv_format == 1) {
  1097. XSNPRINTF(msg, sizeof(msg), "%s,%.3f,", desc, persec);
  1098. SHOW_INTEL_CYCLES_CSV(msg, sizeof(msg), countSz);
  1099. } else {
  1100. XSNPRINTF(msg, sizeof(msg), "%-16s%s %5.0f %s %s %5.3f %s, %8.3f %s/s",
  1101. desc, BENCH_ASYNC_GET_NAME(doAsync), blocks, blockType, word[0], total, word[1],
  1102. persec, blockType);
  1103. SHOW_INTEL_CYCLES(msg, sizeof(msg), countSz);
  1104. }
  1105. printf("%s", msg);
  1106. /* show errors */
  1107. if (ret < 0) {
  1108. printf("Benchmark %s failed: %d\n", desc, ret);
  1109. }
  1110. /* Add to thread stats */
  1111. bench_stats_add(BENCH_STAT_SYM, NULL, 0, desc, doAsync, persec, blockType, ret);
  1112. (void)doAsync;
  1113. (void)ret;
  1114. TEST_SLEEP();
  1115. }
  1116. #ifdef BENCH_ASYM
  1117. #if defined(HAVE_ECC) || !defined(WOLFSSL_RSA_PUBLIC_ONLY) || \
  1118. defined(WOLFSSL_PUBLIC_MP) || !defined(NO_DH)
  1119. static void bench_stats_asym_finish(const char* algo, int strength,
  1120. const char* desc, int doAsync, int count, double start, int ret)
  1121. {
  1122. double total, each = 0, opsSec, milliEach;
  1123. const char **word = bench_result_words2[lng_index];
  1124. const char* kOpsSec = "Ops/Sec";
  1125. char msg[128] = {0};
  1126. total = current_time(0) - start;
  1127. if (count > 0)
  1128. each = total / count; /* per second */
  1129. opsSec = count / total; /* ops second */
  1130. milliEach = each * 1000; /* milliseconds */
  1131. /* format and print to terminal */
  1132. if (csv_format == 1) {
  1133. /* only print out header once */
  1134. if (csv_header_count == 1) {
  1135. printf("\nAsymmetric Ciphers:\n\n");
  1136. printf("Algorithm,avg ms,ops/sec,\n");
  1137. csv_header_count++;
  1138. }
  1139. XSNPRINTF(msg, sizeof(msg), "%s %d %s,%.3f,%.3f,\n", algo, strength, desc, milliEach, opsSec);
  1140. } else {
  1141. XSNPRINTF(msg, sizeof(msg), "%-6s %5d %-9s %s %6d %s %5.3f %s, %s %5.3f ms,"
  1142. " %.3f %s\n", algo, strength, desc, BENCH_ASYNC_GET_NAME(doAsync),
  1143. count, word[0], total, word[1], word[2], milliEach, opsSec, word[3]);
  1144. }
  1145. printf("%s", msg);
  1146. /* show errors */
  1147. if (ret < 0) {
  1148. printf("Benchmark %s %s %d failed: %d\n", algo, desc, strength, ret);
  1149. }
  1150. /* Add to thread stats */
  1151. bench_stats_add(BENCH_STAT_ASYM, algo, strength, desc, doAsync, opsSec, kOpsSec, ret);
  1152. (void)doAsync;
  1153. (void)ret;
  1154. TEST_SLEEP();
  1155. }
  1156. #endif
  1157. #endif /* BENCH_ASYM */
  1158. static WC_INLINE void bench_stats_free(void)
  1159. {
  1160. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  1161. bench_stats_t* bstat;
  1162. for (bstat = bench_stats_head; bstat != NULL; ) {
  1163. bench_stats_t* next = bstat->next;
  1164. XFREE(bstat, NULL, DYNAMIC_TYPE_INFO);
  1165. bstat = next;
  1166. }
  1167. bench_stats_head = NULL;
  1168. bench_stats_tail = NULL;
  1169. #endif
  1170. }
  1171. /******************************************************************************/
  1172. /* End Stats Functions */
  1173. /******************************************************************************/
  1174. static void* benchmarks_do(void* args)
  1175. {
  1176. int bench_buf_size;
  1177. #ifdef WOLFSSL_ASYNC_CRYPT
  1178. #ifndef WC_NO_ASYNC_THREADING
  1179. ThreadData* threadData = (ThreadData*)args;
  1180. if (wolfAsync_DevOpenThread(&devId, &threadData->thread_id) < 0)
  1181. #else
  1182. if (wolfAsync_DevOpen(&devId) < 0)
  1183. #endif
  1184. {
  1185. printf("Async device open failed\nRunning without async\n");
  1186. }
  1187. #endif /* WOLFSSL_ASYNC_CRYPT */
  1188. (void)args;
  1189. #ifdef WOLFSSL_ASYNC_CRYPT
  1190. if (wolfEventQueue_Init(&eventQueue) != 0) {
  1191. printf("Async event queue init failure!\n");
  1192. }
  1193. #endif
  1194. #ifdef WOLF_CRYPTO_CB
  1195. #ifdef HAVE_INTEL_QA_SYNC
  1196. devId = wc_CryptoCb_InitIntelQa();
  1197. if (devId == INVALID_DEVID) {
  1198. printf("Couldn't init the Intel QA\n");
  1199. }
  1200. #endif
  1201. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1202. devId = wc_CryptoCb_InitOcteon();
  1203. if (devId == INVALID_DEVID) {
  1204. printf("Couldn't get the Octeon device ID\n");
  1205. }
  1206. #endif
  1207. #endif
  1208. #if defined(HAVE_LOCAL_RNG)
  1209. {
  1210. int rngRet;
  1211. #ifndef HAVE_FIPS
  1212. rngRet = wc_InitRng_ex(&gRng, HEAP_HINT, devId);
  1213. #else
  1214. rngRet = wc_InitRng(&gRng);
  1215. #endif
  1216. if (rngRet < 0) {
  1217. printf("InitRNG failed\n");
  1218. return NULL;
  1219. }
  1220. }
  1221. #endif
  1222. /* setup bench plain, cipher, key and iv globals */
  1223. /* make sure bench buffer is multiple of 16 (AES block size) */
  1224. bench_buf_size = (int)bench_size + BENCH_CIPHER_ADD;
  1225. if (bench_buf_size % 16)
  1226. bench_buf_size += 16 - (bench_buf_size % 16);
  1227. #ifdef WOLFSSL_AFALG_XILINX_AES
  1228. bench_plain = (byte*)aligned_alloc(64, (size_t)bench_buf_size + 16);
  1229. bench_cipher = (byte*)aligned_alloc(64, (size_t)bench_buf_size + 16);
  1230. #else
  1231. bench_plain = (byte*)XMALLOC((size_t)bench_buf_size + 16, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1232. bench_cipher = (byte*)XMALLOC((size_t)bench_buf_size + 16, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1233. #endif
  1234. if (bench_plain == NULL || bench_cipher == NULL) {
  1235. XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1236. XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1237. bench_plain = bench_cipher = NULL;
  1238. printf("Benchmark block buffer alloc failed!\n");
  1239. goto exit;
  1240. }
  1241. XMEMSET(bench_plain, 0, (size_t)bench_buf_size);
  1242. XMEMSET(bench_cipher, 0, (size_t)bench_buf_size);
  1243. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(HAVE_INTEL_QA_SYNC)
  1244. bench_key = (byte*)XMALLOC(sizeof(bench_key_buf), HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1245. bench_iv = (byte*)XMALLOC(sizeof(bench_iv_buf), HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1246. if (bench_key == NULL || bench_iv == NULL) {
  1247. XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1248. XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1249. bench_key = bench_iv = NULL;
  1250. printf("Benchmark cipher buffer alloc failed!\n");
  1251. goto exit;
  1252. }
  1253. XMEMCPY(bench_key, bench_key_buf, sizeof(bench_key_buf));
  1254. XMEMCPY(bench_iv, bench_iv_buf, sizeof(bench_iv_buf));
  1255. #else
  1256. bench_key = (byte*)bench_key_buf;
  1257. bench_iv = (byte*)bench_iv_buf;
  1258. #endif
  1259. #ifndef WC_NO_RNG
  1260. if (bench_all || (bench_other_algs & BENCH_RNG))
  1261. bench_rng();
  1262. #endif /* WC_NO_RNG */
  1263. #ifndef NO_AES
  1264. #ifdef HAVE_AES_CBC
  1265. if (bench_all || (bench_cipher_algs & BENCH_AES_CBC)) {
  1266. #ifndef NO_SW_BENCH
  1267. bench_aescbc(0);
  1268. #endif
  1269. #if ((defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)) || \
  1270. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)) && \
  1271. !defined(NO_HW_BENCH)
  1272. bench_aescbc(1);
  1273. #endif
  1274. }
  1275. #endif
  1276. #ifdef HAVE_AESGCM
  1277. if (bench_all || (bench_cipher_algs & BENCH_AES_GCM)) {
  1278. #ifndef NO_SW_BENCH
  1279. bench_aesgcm(0);
  1280. #endif
  1281. #if ((defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)) || \
  1282. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)) && \
  1283. !defined(NO_HW_BENCH)
  1284. bench_aesgcm(1);
  1285. #endif
  1286. }
  1287. #endif
  1288. #ifdef WOLFSSL_AES_DIRECT
  1289. if (bench_all || (bench_cipher_algs & BENCH_AES_ECB)) {
  1290. #ifndef NO_SW_BENCH
  1291. bench_aesecb(0);
  1292. #endif
  1293. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES) && \
  1294. !defined(NO_HW_BENCH)
  1295. bench_aesecb(1);
  1296. #endif
  1297. }
  1298. #endif
  1299. #ifdef WOLFSSL_AES_XTS
  1300. if (bench_all || (bench_cipher_algs & BENCH_AES_XTS))
  1301. bench_aesxts();
  1302. #endif
  1303. #ifdef WOLFSSL_AES_CFB
  1304. if (bench_all || (bench_cipher_algs & BENCH_AES_CFB))
  1305. bench_aescfb();
  1306. #endif
  1307. #ifdef WOLFSSL_AES_OFB
  1308. if (bench_all || (bench_cipher_algs & BENCH_AES_OFB))
  1309. bench_aesofb();
  1310. #endif
  1311. #ifdef WOLFSSL_AES_COUNTER
  1312. if (bench_all || (bench_cipher_algs & BENCH_AES_CTR))
  1313. bench_aesctr();
  1314. #endif
  1315. #ifdef HAVE_AESCCM
  1316. if (bench_all || (bench_cipher_algs & BENCH_AES_CCM))
  1317. bench_aesccm();
  1318. #endif
  1319. #endif /* !NO_AES */
  1320. #ifdef HAVE_CAMELLIA
  1321. if (bench_all || (bench_cipher_algs & BENCH_CAMELLIA))
  1322. bench_camellia();
  1323. #endif
  1324. #ifndef NO_RC4
  1325. if (bench_all || (bench_cipher_algs & BENCH_ARC4)) {
  1326. #ifndef NO_SW_BENCH
  1327. bench_arc4(0);
  1328. #endif
  1329. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \
  1330. !defined(NO_HW_BENCH)
  1331. bench_arc4(1);
  1332. #endif
  1333. }
  1334. #endif
  1335. #ifdef HAVE_HC128
  1336. if (bench_all || (bench_cipher_algs & BENCH_HC128))
  1337. bench_hc128();
  1338. #endif
  1339. #ifndef NO_RABBIT
  1340. if (bench_all || (bench_cipher_algs & BENCH_RABBIT))
  1341. bench_rabbit();
  1342. #endif
  1343. #ifdef HAVE_CHACHA
  1344. if (bench_all || (bench_cipher_algs & BENCH_CHACHA20))
  1345. bench_chacha();
  1346. #endif
  1347. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1348. if (bench_all || (bench_cipher_algs & BENCH_CHACHA20_POLY1305))
  1349. bench_chacha20_poly1305_aead();
  1350. #endif
  1351. #ifndef NO_DES3
  1352. if (bench_all || (bench_cipher_algs & BENCH_DES)) {
  1353. #ifndef NO_SW_BENCH
  1354. bench_des(0);
  1355. #endif
  1356. #if ((defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)) || \
  1357. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC)) && \
  1358. !defined(NO_HW_BENCH)
  1359. bench_des(1);
  1360. #endif
  1361. }
  1362. #endif
  1363. #ifdef HAVE_IDEA
  1364. if (bench_all || (bench_cipher_algs & BENCH_IDEA))
  1365. bench_idea();
  1366. #endif
  1367. #ifndef NO_MD5
  1368. if (bench_all || (bench_digest_algs & BENCH_MD5)) {
  1369. #ifndef NO_SW_BENCH
  1370. bench_md5(0);
  1371. #endif
  1372. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_MD5) && \
  1373. !defined(NO_HW_BENCH)
  1374. bench_md5(1);
  1375. #endif
  1376. }
  1377. #endif
  1378. #ifdef HAVE_POLY1305
  1379. if (bench_all || (bench_digest_algs & BENCH_POLY1305))
  1380. bench_poly1305();
  1381. #endif
  1382. #ifndef NO_SHA
  1383. if (bench_all || (bench_digest_algs & BENCH_SHA)) {
  1384. #ifndef NO_SW_BENCH
  1385. bench_sha(0);
  1386. #endif
  1387. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA) && \
  1388. !defined(NO_HW_BENCH)
  1389. bench_sha(1);
  1390. #endif
  1391. }
  1392. #endif
  1393. #ifdef WOLFSSL_SHA224
  1394. if (bench_all || (bench_digest_algs & BENCH_SHA224)) {
  1395. #ifndef NO_SW_BENCH
  1396. bench_sha224(0);
  1397. #endif
  1398. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224) && \
  1399. !defined(NO_HW_BENCH)
  1400. bench_sha224(1);
  1401. #endif
  1402. }
  1403. #endif
  1404. #ifndef NO_SHA256
  1405. if (bench_all || (bench_digest_algs & BENCH_SHA256)) {
  1406. #ifndef NO_SW_BENCH
  1407. bench_sha256(0);
  1408. #endif
  1409. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256) && \
  1410. !defined(NO_HW_BENCH)
  1411. bench_sha256(1);
  1412. #endif
  1413. }
  1414. #endif
  1415. #ifdef WOLFSSL_SHA384
  1416. if (bench_all || (bench_digest_algs & BENCH_SHA384)) {
  1417. #ifndef NO_SW_BENCH
  1418. bench_sha384(0);
  1419. #endif
  1420. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384) && \
  1421. !defined(NO_HW_BENCH)
  1422. bench_sha384(1);
  1423. #endif
  1424. }
  1425. #endif
  1426. #ifdef WOLFSSL_SHA512
  1427. if (bench_all || (bench_digest_algs & BENCH_SHA512)) {
  1428. #ifndef NO_SW_BENCH
  1429. bench_sha512(0);
  1430. #endif
  1431. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512) && \
  1432. !defined(NO_HW_BENCH)
  1433. bench_sha512(1);
  1434. #endif
  1435. }
  1436. #endif
  1437. #ifdef WOLFSSL_SHA3
  1438. #ifndef WOLFSSL_NOSHA3_224
  1439. if (bench_all || (bench_digest_algs & BENCH_SHA3_224)) {
  1440. #ifndef NO_SW_BENCH
  1441. bench_sha3_224(0);
  1442. #endif
  1443. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3) && \
  1444. !defined(NO_HW_BENCH)
  1445. bench_sha3_224(1);
  1446. #endif
  1447. }
  1448. #endif /* WOLFSSL_NOSHA3_224 */
  1449. #ifndef WOLFSSL_NOSHA3_256
  1450. if (bench_all || (bench_digest_algs & BENCH_SHA3_256)) {
  1451. #ifndef NO_SW_BENCH
  1452. bench_sha3_256(0);
  1453. #endif
  1454. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3) && \
  1455. !defined(NO_HW_BENCH)
  1456. bench_sha3_256(1);
  1457. #endif
  1458. }
  1459. #endif /* WOLFSSL_NOSHA3_256 */
  1460. #ifndef WOLFSSL_NOSHA3_384
  1461. if (bench_all || (bench_digest_algs & BENCH_SHA3_384)) {
  1462. #ifndef NO_SW_BENCH
  1463. bench_sha3_384(0);
  1464. #endif
  1465. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3) && \
  1466. !defined(NO_HW_BENCH)
  1467. bench_sha3_384(1);
  1468. #endif
  1469. }
  1470. #endif /* WOLFSSL_NOSHA3_384 */
  1471. #ifndef WOLFSSL_NOSHA3_512
  1472. if (bench_all || (bench_digest_algs & BENCH_SHA3_512)) {
  1473. #ifndef NO_SW_BENCH
  1474. bench_sha3_512(0);
  1475. #endif
  1476. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3) && \
  1477. !defined(NO_HW_BENCH)
  1478. bench_sha3_512(1);
  1479. #endif
  1480. }
  1481. #endif /* WOLFSSL_NOSHA3_512 */
  1482. #endif
  1483. #ifdef WOLFSSL_RIPEMD
  1484. if (bench_all || (bench_digest_algs & BENCH_RIPEMD))
  1485. bench_ripemd();
  1486. #endif
  1487. #ifdef HAVE_BLAKE2
  1488. if (bench_all || (bench_digest_algs & BENCH_BLAKE2B))
  1489. bench_blake2b();
  1490. #endif
  1491. #ifdef HAVE_BLAKE2S
  1492. if (bench_all || (bench_digest_algs & BENCH_BLAKE2S))
  1493. bench_blake2s();
  1494. #endif
  1495. #ifdef WOLFSSL_CMAC
  1496. if (bench_all || (bench_mac_algs & BENCH_CMAC))
  1497. bench_cmac();
  1498. #endif
  1499. #ifndef NO_HMAC
  1500. #ifndef NO_MD5
  1501. if (bench_all || (bench_mac_algs & BENCH_HMAC_MD5)) {
  1502. #ifndef NO_SW_BENCH
  1503. bench_hmac_md5(0);
  1504. #endif
  1505. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) && \
  1506. defined(WC_ASYNC_ENABLE_MD5) && !defined(NO_HW_BENCH)
  1507. bench_hmac_md5(1);
  1508. #endif
  1509. }
  1510. #endif
  1511. #ifndef NO_SHA
  1512. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA)) {
  1513. #ifndef NO_SW_BENCH
  1514. bench_hmac_sha(0);
  1515. #endif
  1516. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) && \
  1517. defined(WC_ASYNC_ENABLE_SHA) && !defined(NO_HW_BENCH)
  1518. bench_hmac_sha(1);
  1519. #endif
  1520. }
  1521. #endif
  1522. #ifdef WOLFSSL_SHA224
  1523. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA224)) {
  1524. #ifndef NO_SW_BENCH
  1525. bench_hmac_sha224(0);
  1526. #endif
  1527. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) && \
  1528. defined(WC_ASYNC_ENABLE_SHA224) && !defined(NO_HW_BENCH)
  1529. bench_hmac_sha224(1);
  1530. #endif
  1531. }
  1532. #endif
  1533. #ifndef NO_SHA256
  1534. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA256)) {
  1535. #ifndef NO_SW_BENCH
  1536. bench_hmac_sha256(0);
  1537. #endif
  1538. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) && \
  1539. defined(WC_ASYNC_ENABLE_SHA256) && !defined(NO_HW_BENCH)
  1540. bench_hmac_sha256(1);
  1541. #endif
  1542. }
  1543. #endif
  1544. #ifdef WOLFSSL_SHA384
  1545. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA384)) {
  1546. #ifndef NO_SW_BENCH
  1547. bench_hmac_sha384(0);
  1548. #endif
  1549. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) && \
  1550. defined(WC_ASYNC_ENABLE_SHA384) && !defined(NO_HW_BENCH)
  1551. bench_hmac_sha384(1);
  1552. #endif
  1553. }
  1554. #endif
  1555. #ifdef WOLFSSL_SHA512
  1556. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA512)) {
  1557. #ifndef NO_SW_BENCH
  1558. bench_hmac_sha512(0);
  1559. #endif
  1560. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC) && \
  1561. defined(WC_ASYNC_ENABLE_SHA512) && !defined(NO_HW_BENCH)
  1562. bench_hmac_sha512(1);
  1563. #endif
  1564. }
  1565. #endif
  1566. #ifndef NO_PWDBASED
  1567. if (bench_all || (bench_mac_algs & BENCH_PBKDF2)) {
  1568. bench_pbkdf2();
  1569. }
  1570. #endif
  1571. #endif /* NO_HMAC */
  1572. #ifdef HAVE_SCRYPT
  1573. if (bench_all || (bench_other_algs & BENCH_SCRYPT))
  1574. bench_scrypt();
  1575. #endif
  1576. #ifndef NO_RSA
  1577. #ifdef WOLFSSL_KEY_GEN
  1578. if (bench_all || (bench_asym_algs & BENCH_RSA_KEYGEN)) {
  1579. #ifndef NO_SW_BENCH
  1580. if (bench_asym_algs & BENCH_RSA_SZ) {
  1581. bench_rsaKeyGen_size(0, bench_size);
  1582. }
  1583. else {
  1584. bench_rsaKeyGen(0);
  1585. }
  1586. #endif
  1587. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA_KEYGEN) \
  1588. && !defined(NO_HW_BENCH)
  1589. if (bench_asym_algs & BENCH_RSA_SZ) {
  1590. bench_rsaKeyGen_size(1, bench_size);
  1591. }
  1592. else {
  1593. bench_rsaKeyGen(1);
  1594. }
  1595. #endif
  1596. }
  1597. #endif
  1598. if (bench_all || (bench_asym_algs & BENCH_RSA)) {
  1599. #ifndef NO_SW_BENCH
  1600. bench_rsa(0);
  1601. #endif
  1602. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA) && \
  1603. !defined(NO_HW_BENCH)
  1604. bench_rsa(1);
  1605. #endif
  1606. }
  1607. #ifdef WOLFSSL_KEY_GEN
  1608. if (bench_asym_algs & BENCH_RSA_SZ) {
  1609. #ifndef NO_SW_BENCH
  1610. bench_rsa_key(0, bench_size);
  1611. #endif
  1612. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA) && \
  1613. !defined(NO_HW_BENCH)
  1614. bench_rsa_key(1, bench_size);
  1615. #endif
  1616. }
  1617. #endif
  1618. #endif
  1619. #ifndef NO_DH
  1620. if (bench_all || (bench_asym_algs & BENCH_DH)) {
  1621. #ifndef NO_SW_BENCH
  1622. bench_dh(0);
  1623. #endif
  1624. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_DH) && \
  1625. !defined(NO_HW_BENCH)
  1626. bench_dh(1);
  1627. #endif
  1628. }
  1629. #endif
  1630. #ifdef HAVE_NTRU
  1631. if (bench_all || (bench_asym_algs & BENCH_NTRU))
  1632. bench_ntru();
  1633. if (bench_all || (bench_asym_algs & BENCH_NTRU_KEYGEN))
  1634. bench_ntruKeyGen();
  1635. #endif
  1636. #ifdef HAVE_ECC
  1637. if (bench_all || (bench_asym_algs & BENCH_ECC_MAKEKEY)) {
  1638. #ifndef NO_SW_BENCH
  1639. bench_eccMakeKey(0);
  1640. #endif
  1641. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \
  1642. !defined(NO_HW_BENCH)
  1643. bench_eccMakeKey(1);
  1644. #endif
  1645. }
  1646. if (bench_all || (bench_asym_algs & BENCH_ECC)) {
  1647. #ifndef NO_SW_BENCH
  1648. bench_ecc(0);
  1649. #endif
  1650. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \
  1651. !defined(NO_HW_BENCH)
  1652. bench_ecc(1);
  1653. #endif
  1654. }
  1655. #ifdef HAVE_ECC_ENCRYPT
  1656. if (bench_all || (bench_asym_algs & BENCH_ECC_ENCRYPT))
  1657. bench_eccEncrypt();
  1658. #endif
  1659. #endif
  1660. #ifdef HAVE_CURVE25519
  1661. if (bench_all || (bench_asym_algs & BENCH_CURVE25519_KEYGEN))
  1662. bench_curve25519KeyGen();
  1663. #ifdef HAVE_CURVE25519_SHARED_SECRET
  1664. if (bench_all || (bench_asym_algs & BENCH_CURVE25519_KA))
  1665. bench_curve25519KeyAgree();
  1666. #endif
  1667. #endif
  1668. #ifdef HAVE_ED25519
  1669. if (bench_all || (bench_asym_algs & BENCH_ED25519_KEYGEN))
  1670. bench_ed25519KeyGen();
  1671. if (bench_all || (bench_asym_algs & BENCH_ED25519_SIGN))
  1672. bench_ed25519KeySign();
  1673. #endif
  1674. #ifdef HAVE_CURVE448
  1675. if (bench_all || (bench_asym_algs & BENCH_CURVE448_KEYGEN))
  1676. bench_curve448KeyGen();
  1677. #ifdef HAVE_CURVE448_SHARED_SECRET
  1678. if (bench_all || (bench_asym_algs & BENCH_CURVE448_KA))
  1679. bench_curve448KeyAgree();
  1680. #endif
  1681. #endif
  1682. #ifdef HAVE_ED448
  1683. if (bench_all || (bench_asym_algs & BENCH_ED448_KEYGEN))
  1684. bench_ed448KeyGen();
  1685. if (bench_all || (bench_asym_algs & BENCH_ED448_SIGN))
  1686. bench_ed448KeySign();
  1687. #endif
  1688. exit:
  1689. /* free benchmark buffers */
  1690. XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1691. XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1692. #ifdef WOLFSSL_ASYNC_CRYPT
  1693. XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1694. XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1695. #endif
  1696. #ifdef WOLF_CRYPTO_CB
  1697. #ifdef HAVE_INTEL_QA_SYNC
  1698. wc_CryptoCb_CleanupIntelQa(&devId);
  1699. #endif
  1700. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1701. wc_CryptoCb_CleanupOcteon(&devId);
  1702. #endif
  1703. #endif
  1704. #ifdef WOLFSSL_ASYNC_CRYPT
  1705. /* free event queue */
  1706. wolfEventQueue_Free(&eventQueue);
  1707. #endif
  1708. #if defined(HAVE_LOCAL_RNG)
  1709. wc_FreeRng(&gRng);
  1710. #endif
  1711. #ifdef WOLFSSL_ASYNC_CRYPT
  1712. wolfAsync_DevClose(&devId);
  1713. #endif
  1714. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1715. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1716. wc_ecc_fp_free();
  1717. #endif
  1718. (void)bench_cipher_algs;
  1719. (void)bench_digest_algs;
  1720. (void)bench_mac_algs;
  1721. (void)bench_asym_algs;
  1722. (void)bench_other_algs;
  1723. return NULL;
  1724. }
  1725. int benchmark_init(void)
  1726. {
  1727. int ret = 0;
  1728. benchmark_static_init();
  1729. #ifdef WOLFSSL_STATIC_MEMORY
  1730. ret = wc_LoadStaticMemory(&HEAP_HINT, gBenchMemory, sizeof(gBenchMemory),
  1731. WOLFMEM_GENERAL, 1);
  1732. if (ret != 0) {
  1733. printf("unable to load static memory %d\n", ret);
  1734. }
  1735. #endif /* WOLFSSL_STATIC_MEMORY */
  1736. if ((ret = wolfCrypt_Init()) != 0) {
  1737. printf("wolfCrypt_Init failed %d\n", ret);
  1738. return EXIT_FAILURE;
  1739. }
  1740. bench_stats_init();
  1741. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  1742. wolfSSL_Debugging_ON();
  1743. #endif
  1744. if (csv_format == 1) {
  1745. printf("wolfCrypt Benchmark (block bytes %d, min %.1f sec each)\n",
  1746. (int)BENCH_SIZE, BENCH_MIN_RUNTIME_SEC);
  1747. printf("This format allows you to easily copy the output to a csv file.");
  1748. printf("\n\nSymmetric Ciphers:\n\n");
  1749. printf("Algorithm,MB/s,Cycles per byte,\n");
  1750. } else {
  1751. printf("wolfCrypt Benchmark (block bytes %d, min %.1f sec each)\n",
  1752. (int)BENCH_SIZE, BENCH_MIN_RUNTIME_SEC);
  1753. }
  1754. #ifdef HAVE_WNR
  1755. ret = wc_InitNetRandom(wnrConfigFile, NULL, 5000);
  1756. if (ret != 0) {
  1757. printf("Whitewood netRandom config init failed %d\n", ret);
  1758. }
  1759. #endif /* HAVE_WNR */
  1760. return ret;
  1761. }
  1762. int benchmark_free(void)
  1763. {
  1764. int ret;
  1765. #ifdef HAVE_WNR
  1766. ret = wc_FreeNetRandom();
  1767. if (ret < 0) {
  1768. printf("Failed to free netRandom context %d\n", ret);
  1769. }
  1770. #endif
  1771. if (gPrintStats || devId != INVALID_DEVID) {
  1772. bench_stats_print();
  1773. }
  1774. bench_stats_free();
  1775. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1776. printf("error %d with wolfCrypt_Cleanup\n", ret);
  1777. }
  1778. return ret;
  1779. }
  1780. /* so embedded projects can pull in tests on their own */
  1781. #ifdef HAVE_STACK_SIZE
  1782. THREAD_RETURN WOLFSSL_THREAD benchmark_test(void* args)
  1783. #else
  1784. int benchmark_test(void *args)
  1785. #endif
  1786. {
  1787. int ret;
  1788. (void)args;
  1789. printf("------------------------------------------------------------------------------\n");
  1790. printf(" wolfSSL version %s\n", LIBWOLFSSL_VERSION_STRING);
  1791. printf("------------------------------------------------------------------------------\n");
  1792. ret = benchmark_init();
  1793. if (ret != 0)
  1794. EXIT_TEST(ret);
  1795. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  1796. {
  1797. int i;
  1798. if (g_threadCount == 0) {
  1799. #ifdef WC_ASYNC_BENCH_THREAD_COUNT
  1800. g_threadCount = WC_ASYNC_BENCH_THREAD_COUNT;
  1801. #else
  1802. g_threadCount = wc_AsyncGetNumberOfCpus();
  1803. #endif
  1804. }
  1805. printf("CPUs: %d\n", g_threadCount);
  1806. g_threadData = (ThreadData*)XMALLOC(sizeof(ThreadData) * g_threadCount,
  1807. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1808. if (g_threadData == NULL) {
  1809. printf("Thread data alloc failed!\n");
  1810. EXIT_TEST(EXIT_FAILURE);
  1811. }
  1812. /* Create threads */
  1813. for (i = 0; i < g_threadCount; i++) {
  1814. ret = wc_AsyncThreadCreate(&g_threadData[i].thread_id,
  1815. benchmarks_do, &g_threadData[i]);
  1816. if (ret != 0) {
  1817. printf("Error creating benchmark thread %d\n", ret);
  1818. EXIT_TEST(EXIT_FAILURE);
  1819. }
  1820. }
  1821. /* Start threads */
  1822. for (i = 0; i < g_threadCount; i++) {
  1823. wc_AsyncThreadJoin(&g_threadData[i].thread_id);
  1824. }
  1825. XFREE(g_threadData, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1826. }
  1827. #else
  1828. benchmarks_do(NULL);
  1829. #endif
  1830. printf("Benchmark complete\n");
  1831. ret = benchmark_free();
  1832. EXIT_TEST(ret);
  1833. }
  1834. #ifndef WC_NO_RNG
  1835. void bench_rng(void)
  1836. {
  1837. int ret, i, count;
  1838. double start;
  1839. long pos, len, remain;
  1840. WC_RNG myrng;
  1841. #ifndef HAVE_FIPS
  1842. ret = wc_InitRng_ex(&myrng, HEAP_HINT, devId);
  1843. #else
  1844. ret = wc_InitRng(&myrng);
  1845. #endif
  1846. if (ret < 0) {
  1847. printf("InitRNG failed %d\n", ret);
  1848. return;
  1849. }
  1850. bench_stats_start(&count, &start);
  1851. do {
  1852. for (i = 0; i < numBlocks; i++) {
  1853. /* Split request to handle large RNG request */
  1854. pos = 0;
  1855. remain = (int)BENCH_SIZE;
  1856. while (remain > 0) {
  1857. len = remain;
  1858. if (len > RNG_MAX_BLOCK_LEN)
  1859. len = RNG_MAX_BLOCK_LEN;
  1860. ret = wc_RNG_GenerateBlock(&myrng, &bench_plain[pos], (word32)len);
  1861. if (ret < 0)
  1862. goto exit_rng;
  1863. remain -= len;
  1864. pos += len;
  1865. }
  1866. }
  1867. count += i;
  1868. } while (bench_stats_sym_check(start));
  1869. exit_rng:
  1870. bench_stats_sym_finish("RNG", 0, count, bench_size, start, ret);
  1871. wc_FreeRng(&myrng);
  1872. }
  1873. #endif /* WC_NO_RNG */
  1874. #ifndef NO_AES
  1875. #ifdef HAVE_AES_CBC
  1876. static void bench_aescbc_internal(int doAsync, const byte* key, word32 keySz,
  1877. const byte* iv, const char* encLabel,
  1878. const char* decLabel)
  1879. {
  1880. int ret = 0, i, count = 0, times, pending = 0;
  1881. Aes enc[BENCH_MAX_PENDING];
  1882. double start;
  1883. /* clear for done cleanup */
  1884. XMEMSET(enc, 0, sizeof(enc));
  1885. /* init keys */
  1886. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1887. if ((ret = wc_AesInit(&enc[i], HEAP_HINT,
  1888. doAsync ? devId : INVALID_DEVID)) != 0) {
  1889. printf("AesInit failed, ret = %d\n", ret);
  1890. goto exit;
  1891. }
  1892. ret = wc_AesSetKey(&enc[i], key, keySz, iv, AES_ENCRYPTION);
  1893. if (ret != 0) {
  1894. printf("AesSetKey failed, ret = %d\n", ret);
  1895. goto exit;
  1896. }
  1897. }
  1898. bench_stats_start(&count, &start);
  1899. do {
  1900. for (times = 0; times < numBlocks || pending > 0; ) {
  1901. bench_async_poll(&pending);
  1902. /* while free pending slots in queue, submit ops */
  1903. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1904. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, numBlocks, &pending)) {
  1905. ret = wc_AesCbcEncrypt(&enc[i], bench_plain, bench_cipher,
  1906. BENCH_SIZE);
  1907. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, &pending)) {
  1908. goto exit_aes_enc;
  1909. }
  1910. }
  1911. } /* for i */
  1912. } /* for times */
  1913. count += times;
  1914. } while (bench_stats_sym_check(start));
  1915. exit_aes_enc:
  1916. bench_stats_sym_finish(encLabel, doAsync, count, bench_size, start, ret);
  1917. if (ret < 0) {
  1918. goto exit;
  1919. }
  1920. #ifdef HAVE_AES_DECRYPT
  1921. /* init keys */
  1922. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1923. ret = wc_AesSetKey(&enc[i], key, keySz, iv, AES_DECRYPTION);
  1924. if (ret != 0) {
  1925. printf("AesSetKey failed, ret = %d\n", ret);
  1926. goto exit;
  1927. }
  1928. }
  1929. bench_stats_start(&count, &start);
  1930. do {
  1931. for (times = 0; times < numBlocks || pending > 0; ) {
  1932. bench_async_poll(&pending);
  1933. /* while free pending slots in queue, submit ops */
  1934. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1935. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, numBlocks, &pending)) {
  1936. ret = wc_AesCbcDecrypt(&enc[i], bench_plain, bench_cipher,
  1937. BENCH_SIZE);
  1938. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, &pending)) {
  1939. goto exit_aes_dec;
  1940. }
  1941. }
  1942. } /* for i */
  1943. } /* for times */
  1944. count += times;
  1945. } while (bench_stats_sym_check(start));
  1946. exit_aes_dec:
  1947. bench_stats_sym_finish(decLabel, doAsync, count, bench_size, start, ret);
  1948. #endif /* HAVE_AES_DECRYPT */
  1949. (void)decLabel;
  1950. exit:
  1951. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1952. wc_AesFree(&enc[i]);
  1953. }
  1954. }
  1955. void bench_aescbc(int doAsync)
  1956. {
  1957. #ifdef WOLFSSL_AES_128
  1958. bench_aescbc_internal(doAsync, bench_key, 16, bench_iv,
  1959. "AES-128-CBC-enc", "AES-128-CBC-dec");
  1960. #endif
  1961. #ifdef WOLFSSL_AES_192
  1962. bench_aescbc_internal(doAsync, bench_key, 24, bench_iv,
  1963. "AES-192-CBC-enc", "AES-192-CBC-dec");
  1964. #endif
  1965. #ifdef WOLFSSL_AES_256
  1966. bench_aescbc_internal(doAsync, bench_key, 32, bench_iv,
  1967. "AES-256-CBC-enc", "AES-256-CBC-dec");
  1968. #endif
  1969. }
  1970. #endif /* HAVE_AES_CBC */
  1971. #ifdef HAVE_AESGCM
  1972. static void bench_aesgcm_internal(int doAsync, const byte* key, word32 keySz,
  1973. const byte* iv, word32 ivSz,
  1974. const char* encLabel, const char* decLabel)
  1975. {
  1976. int ret = 0, i, count = 0, times, pending = 0;
  1977. Aes enc[BENCH_MAX_PENDING];
  1978. #ifdef HAVE_AES_DECRYPT
  1979. Aes dec[BENCH_MAX_PENDING];
  1980. #endif
  1981. double start;
  1982. DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  1983. DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  1984. /* clear for done cleanup */
  1985. XMEMSET(enc, 0, sizeof(enc));
  1986. #ifdef HAVE_AES_DECRYPT
  1987. XMEMSET(dec, 0, sizeof(dec));
  1988. #endif
  1989. #ifdef WOLFSSL_ASYNC_CRYPT
  1990. if (bench_additional)
  1991. #endif
  1992. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  1993. #ifdef WOLFSSL_ASYNC_CRYPT
  1994. if (bench_tag)
  1995. #endif
  1996. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  1997. /* init keys */
  1998. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1999. if ((ret = wc_AesInit(&enc[i], HEAP_HINT,
  2000. doAsync ? devId : INVALID_DEVID)) != 0) {
  2001. printf("AesInit failed, ret = %d\n", ret);
  2002. goto exit;
  2003. }
  2004. ret = wc_AesGcmSetKey(&enc[i], key, keySz);
  2005. if (ret != 0) {
  2006. printf("AesGcmSetKey failed, ret = %d\n", ret);
  2007. goto exit;
  2008. }
  2009. }
  2010. /* GCM uses same routine in backend for both encrypt and decrypt */
  2011. bench_stats_start(&count, &start);
  2012. do {
  2013. for (times = 0; times < numBlocks || pending > 0; ) {
  2014. bench_async_poll(&pending);
  2015. /* while free pending slots in queue, submit ops */
  2016. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2017. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, numBlocks, &pending)) {
  2018. ret = wc_AesGcmEncrypt(&enc[i], bench_cipher,
  2019. bench_plain, BENCH_SIZE,
  2020. iv, ivSz, bench_tag, AES_AUTH_TAG_SZ,
  2021. bench_additional, aesAuthAddSz);
  2022. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, &pending)) {
  2023. goto exit_aes_gcm;
  2024. }
  2025. }
  2026. } /* for i */
  2027. } /* for times */
  2028. count += times;
  2029. } while (bench_stats_sym_check(start));
  2030. exit_aes_gcm:
  2031. bench_stats_sym_finish(encLabel, doAsync, count, bench_size, start, ret);
  2032. #ifdef HAVE_AES_DECRYPT
  2033. /* init keys */
  2034. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2035. if ((ret = wc_AesInit(&dec[i], HEAP_HINT,
  2036. doAsync ? devId : INVALID_DEVID)) != 0) {
  2037. printf("AesInit failed, ret = %d\n", ret);
  2038. goto exit;
  2039. }
  2040. ret = wc_AesGcmSetKey(&dec[i], key, keySz);
  2041. if (ret != 0) {
  2042. printf("AesGcmSetKey failed, ret = %d\n", ret);
  2043. goto exit;
  2044. }
  2045. }
  2046. bench_stats_start(&count, &start);
  2047. do {
  2048. for (times = 0; times < numBlocks || pending > 0; ) {
  2049. bench_async_poll(&pending);
  2050. /* while free pending slots in queue, submit ops */
  2051. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2052. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&dec[i]), 0, &times, numBlocks, &pending)) {
  2053. ret = wc_AesGcmDecrypt(&dec[i], bench_plain,
  2054. bench_cipher, BENCH_SIZE,
  2055. iv, ivSz, bench_tag, AES_AUTH_TAG_SZ,
  2056. bench_additional, aesAuthAddSz);
  2057. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&dec[i]), 0, &times, &pending)) {
  2058. goto exit_aes_gcm_dec;
  2059. }
  2060. }
  2061. } /* for i */
  2062. } /* for times */
  2063. count += times;
  2064. } while (bench_stats_sym_check(start));
  2065. exit_aes_gcm_dec:
  2066. bench_stats_sym_finish(decLabel, doAsync, count, bench_size, start, ret);
  2067. #endif /* HAVE_AES_DECRYPT */
  2068. (void)decLabel;
  2069. exit:
  2070. if (ret < 0) {
  2071. printf("bench_aesgcm failed: %d\n", ret);
  2072. }
  2073. #ifdef HAVE_AES_DECRYPT
  2074. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2075. wc_AesFree(&dec[i]);
  2076. }
  2077. #endif
  2078. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2079. wc_AesFree(&enc[i]);
  2080. }
  2081. FREE_VAR(bench_additional, HEAP_HINT);
  2082. FREE_VAR(bench_tag, HEAP_HINT);
  2083. }
  2084. void bench_aesgcm(int doAsync)
  2085. {
  2086. #if defined(WOLFSSL_AES_128) && !defined(WOLFSSL_AFALG_XILINX_AES) \
  2087. && !defined(WOLFSSL_XILINX_CRYPT)
  2088. bench_aesgcm_internal(doAsync, bench_key, 16, bench_iv, 12,
  2089. "AES-128-GCM-enc", "AES-128-GCM-dec");
  2090. #endif
  2091. #if defined(WOLFSSL_AES_192) && !defined(WOLFSSL_AFALG_XILINX_AES) \
  2092. && !defined(WOLFSSL_XILINX_CRYPT)
  2093. bench_aesgcm_internal(doAsync, bench_key, 24, bench_iv, 12,
  2094. "AES-192-GCM-enc", "AES-192-GCM-dec");
  2095. #endif
  2096. #ifdef WOLFSSL_AES_256
  2097. bench_aesgcm_internal(doAsync, bench_key, 32, bench_iv, 12,
  2098. "AES-256-GCM-enc", "AES-256-GCM-dec");
  2099. #endif
  2100. }
  2101. #endif /* HAVE_AESGCM */
  2102. #ifdef WOLFSSL_AES_DIRECT
  2103. static void bench_aesecb_internal(int doAsync, const byte* key, word32 keySz,
  2104. const char* encLabel, const char* decLabel)
  2105. {
  2106. int ret, i, count = 0, times, pending = 0;
  2107. Aes enc[BENCH_MAX_PENDING];
  2108. double start;
  2109. /* clear for done cleanup */
  2110. XMEMSET(enc, 0, sizeof(enc));
  2111. /* init keys */
  2112. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2113. if ((ret = wc_AesInit(&enc[i], HEAP_HINT,
  2114. doAsync ? devId : INVALID_DEVID)) != 0) {
  2115. printf("AesInit failed, ret = %d\n", ret);
  2116. goto exit;
  2117. }
  2118. ret = wc_AesSetKey(&enc[i], key, keySz, bench_iv, AES_ENCRYPTION);
  2119. if (ret != 0) {
  2120. printf("AesSetKey failed, ret = %d\n", ret);
  2121. goto exit;
  2122. }
  2123. }
  2124. bench_stats_start(&count, &start);
  2125. do {
  2126. for (times = 0; times < numBlocks || pending > 0; ) {
  2127. bench_async_poll(&pending);
  2128. /* while free pending slots in queue, submit ops */
  2129. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2130. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, numBlocks, &pending)) {
  2131. wc_AesEncryptDirect(&enc[i], bench_cipher, bench_plain);
  2132. ret = 0;
  2133. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, &pending)) {
  2134. goto exit_aes_enc;
  2135. }
  2136. }
  2137. } /* for i */
  2138. } /* for times */
  2139. count += times;
  2140. } while (bench_stats_sym_check(start));
  2141. exit_aes_enc:
  2142. bench_stats_sym_finish(encLabel, doAsync, count, AES_BLOCK_SIZE,
  2143. start, ret);
  2144. #ifdef HAVE_AES_DECRYPT
  2145. /* init keys */
  2146. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2147. ret = wc_AesSetKey(&enc[i], key, keySz, bench_iv, AES_DECRYPTION);
  2148. if (ret != 0) {
  2149. printf("AesSetKey failed, ret = %d\n", ret);
  2150. goto exit;
  2151. }
  2152. }
  2153. bench_stats_start(&count, &start);
  2154. do {
  2155. for (times = 0; times < numBlocks || pending > 0; ) {
  2156. bench_async_poll(&pending);
  2157. /* while free pending slots in queue, submit ops */
  2158. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2159. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, numBlocks, &pending)) {
  2160. wc_AesDecryptDirect(&enc[i], bench_plain,
  2161. bench_cipher);
  2162. ret = 0;
  2163. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, &pending)) {
  2164. goto exit_aes_dec;
  2165. }
  2166. }
  2167. } /* for i */
  2168. } /* for times */
  2169. count += times;
  2170. } while (bench_stats_sym_check(start));
  2171. exit_aes_dec:
  2172. bench_stats_sym_finish(decLabel, doAsync, count, AES_BLOCK_SIZE,
  2173. start, ret);
  2174. #endif /* HAVE_AES_DECRYPT */
  2175. exit:
  2176. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2177. wc_AesFree(&enc[i]);
  2178. }
  2179. }
  2180. void bench_aesecb(int doAsync)
  2181. {
  2182. #ifdef WOLFSSL_AES_128
  2183. bench_aesecb_internal(doAsync, bench_key, 16,
  2184. "AES-128-ECB-enc", "AES-128-ECB-dec");
  2185. #endif
  2186. #ifdef WOLFSSL_AES_192
  2187. bench_aesecb_internal(doAsync, bench_key, 24,
  2188. "AES-192-ECB-enc", "AES-192-ECB-dec");
  2189. #endif
  2190. #ifdef WOLFSSL_AES_256
  2191. bench_aesecb_internal(doAsync, bench_key, 32,
  2192. "AES-256-ECB-enc", "AES-256-ECB-dec");
  2193. #endif
  2194. }
  2195. #endif /* WOLFSSL_AES_DIRECT */
  2196. #ifdef WOLFSSL_AES_CFB
  2197. static void bench_aescfb_internal(const byte* key, word32 keySz, const byte* iv,
  2198. const char* label)
  2199. {
  2200. Aes enc;
  2201. double start;
  2202. int i, ret, count;
  2203. ret = wc_AesSetKey(&enc, key, keySz, iv, AES_ENCRYPTION);
  2204. if (ret != 0) {
  2205. printf("AesSetKey failed, ret = %d\n", ret);
  2206. return;
  2207. }
  2208. bench_stats_start(&count, &start);
  2209. do {
  2210. for (i = 0; i < numBlocks; i++) {
  2211. if((ret = wc_AesCfbEncrypt(&enc, bench_plain, bench_cipher,
  2212. BENCH_SIZE)) != 0) {
  2213. printf("wc_AesCfbEncrypt failed, ret = %d\n", ret);
  2214. return;
  2215. }
  2216. }
  2217. count += i;
  2218. } while (bench_stats_sym_check(start));
  2219. bench_stats_sym_finish(label, 0, count, bench_size, start, ret);
  2220. }
  2221. void bench_aescfb(void)
  2222. {
  2223. #ifdef WOLFSSL_AES_128
  2224. bench_aescfb_internal(bench_key, 16, bench_iv, "AES-128-CFB");
  2225. #endif
  2226. #ifdef WOLFSSL_AES_192
  2227. bench_aescfb_internal(bench_key, 24, bench_iv, "AES-192-CFB");
  2228. #endif
  2229. #ifdef WOLFSSL_AES_256
  2230. bench_aescfb_internal(bench_key, 32, bench_iv, "AES-256-CFB");
  2231. #endif
  2232. }
  2233. #endif /* WOLFSSL_AES_CFB */
  2234. #ifdef WOLFSSL_AES_OFB
  2235. static void bench_aesofb_internal(const byte* key, word32 keySz, const byte* iv,
  2236. const char* label)
  2237. {
  2238. Aes enc;
  2239. double start;
  2240. int i, ret, count;
  2241. ret = wc_AesSetKey(&enc, key, keySz, iv, AES_ENCRYPTION);
  2242. if (ret != 0) {
  2243. printf("AesSetKey failed, ret = %d\n", ret);
  2244. return;
  2245. }
  2246. bench_stats_start(&count, &start);
  2247. do {
  2248. for (i = 0; i < numBlocks; i++) {
  2249. if((ret = wc_AesOfbEncrypt(&enc, bench_plain, bench_cipher,
  2250. BENCH_SIZE)) != 0) {
  2251. printf("wc_AesCfbEncrypt failed, ret = %d\n", ret);
  2252. return;
  2253. }
  2254. }
  2255. count += i;
  2256. } while (bench_stats_sym_check(start));
  2257. bench_stats_sym_finish(label, 0, count, bench_size, start, ret);
  2258. }
  2259. void bench_aesofb(void)
  2260. {
  2261. #ifdef WOLFSSL_AES_128
  2262. bench_aesofb_internal(bench_key, 16, bench_iv, "AES-128-OFB");
  2263. #endif
  2264. #ifdef WOLFSSL_AES_192
  2265. bench_aesofb_internal(bench_key, 24, bench_iv, "AES-192-OFB");
  2266. #endif
  2267. #ifdef WOLFSSL_AES_256
  2268. bench_aesofb_internal(bench_key, 32, bench_iv, "AES-256-OFB");
  2269. #endif
  2270. }
  2271. #endif /* WOLFSSL_AES_CFB */
  2272. #ifdef WOLFSSL_AES_XTS
  2273. void bench_aesxts(void)
  2274. {
  2275. XtsAes aes;
  2276. double start;
  2277. int i, count, ret;
  2278. static unsigned char k1[] = {
  2279. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  2280. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  2281. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  2282. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  2283. };
  2284. static unsigned char i1[] = {
  2285. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  2286. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  2287. };
  2288. ret = wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_ENCRYPTION,
  2289. HEAP_HINT, devId);
  2290. if (ret != 0) {
  2291. printf("wc_AesXtsSetKey failed, ret = %d\n", ret);
  2292. return;
  2293. }
  2294. bench_stats_start(&count, &start);
  2295. do {
  2296. for (i = 0; i < numBlocks; i++) {
  2297. if ((ret = wc_AesXtsEncrypt(&aes, bench_plain, bench_cipher,
  2298. BENCH_SIZE, i1, sizeof(i1))) != 0) {
  2299. printf("wc_AesXtsEncrypt failed, ret = %d\n", ret);
  2300. return;
  2301. }
  2302. }
  2303. count += i;
  2304. } while (bench_stats_sym_check(start));
  2305. bench_stats_sym_finish("AES-XTS-enc", 0, count, bench_size, start, ret);
  2306. wc_AesXtsFree(&aes);
  2307. /* decryption benchmark */
  2308. ret = wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_DECRYPTION,
  2309. HEAP_HINT, devId);
  2310. if (ret != 0) {
  2311. printf("wc_AesXtsSetKey failed, ret = %d\n", ret);
  2312. return;
  2313. }
  2314. bench_stats_start(&count, &start);
  2315. do {
  2316. for (i = 0; i < numBlocks; i++) {
  2317. if ((ret = wc_AesXtsDecrypt(&aes, bench_plain, bench_cipher,
  2318. BENCH_SIZE, i1, sizeof(i1))) != 0) {
  2319. printf("wc_AesXtsDecrypt failed, ret = %d\n", ret);
  2320. return;
  2321. }
  2322. }
  2323. count += i;
  2324. } while (bench_stats_sym_check(start));
  2325. bench_stats_sym_finish("AES-XTS-dec", 0, count, bench_size, start, ret);
  2326. wc_AesXtsFree(&aes);
  2327. }
  2328. #endif /* WOLFSSL_AES_XTS */
  2329. #ifdef WOLFSSL_AES_COUNTER
  2330. static void bench_aesctr_internal(const byte* key, word32 keySz, const byte* iv,
  2331. const char* label)
  2332. {
  2333. Aes enc;
  2334. double start;
  2335. int i, count, ret = 0;
  2336. wc_AesSetKeyDirect(&enc, key, keySz, iv, AES_ENCRYPTION);
  2337. bench_stats_start(&count, &start);
  2338. do {
  2339. for (i = 0; i < numBlocks; i++) {
  2340. if((ret = wc_AesCtrEncrypt(&enc, bench_plain, bench_cipher, BENCH_SIZE)) != 0) {
  2341. printf("wc_AesCtrEncrypt failed, ret = %d\n", ret);
  2342. return;
  2343. }
  2344. }
  2345. count += i;
  2346. } while (bench_stats_sym_check(start));
  2347. bench_stats_sym_finish(label, 0, count, bench_size, start, ret);
  2348. }
  2349. void bench_aesctr(void)
  2350. {
  2351. #ifdef WOLFSSL_AES_128
  2352. bench_aesctr_internal(bench_key, 16, bench_iv, "AES-128-CTR");
  2353. #endif
  2354. #ifdef WOLFSSL_AES_192
  2355. bench_aesctr_internal(bench_key, 24, bench_iv, "AES-192-CTR");
  2356. #endif
  2357. #ifdef WOLFSSL_AES_256
  2358. bench_aesctr_internal(bench_key, 32, bench_iv, "AES-256-CTR");
  2359. #endif
  2360. }
  2361. #endif /* WOLFSSL_AES_COUNTER */
  2362. #ifdef HAVE_AESCCM
  2363. void bench_aesccm(void)
  2364. {
  2365. Aes enc;
  2366. double start;
  2367. int ret, i, count;
  2368. DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  2369. DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  2370. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  2371. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  2372. if ((ret = wc_AesCcmSetKey(&enc, bench_key, 16)) != 0) {
  2373. printf("wc_AesCcmSetKey failed, ret = %d\n", ret);
  2374. return;
  2375. }
  2376. bench_stats_start(&count, &start);
  2377. do {
  2378. for (i = 0; i < numBlocks; i++) {
  2379. wc_AesCcmEncrypt(&enc, bench_cipher, bench_plain, BENCH_SIZE,
  2380. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  2381. bench_additional, aesAuthAddSz);
  2382. }
  2383. count += i;
  2384. } while (bench_stats_sym_check(start));
  2385. bench_stats_sym_finish("AES-CCM-Enc", 0, count, bench_size, start, ret);
  2386. bench_stats_start(&count, &start);
  2387. do {
  2388. for (i = 0; i < numBlocks; i++) {
  2389. wc_AesCcmDecrypt(&enc, bench_plain, bench_cipher, BENCH_SIZE,
  2390. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  2391. bench_additional, aesAuthAddSz);
  2392. }
  2393. count += i;
  2394. } while (bench_stats_sym_check(start));
  2395. bench_stats_sym_finish("AES-CCM-Dec", 0, count, bench_size, start, ret);
  2396. FREE_VAR(bench_additional, HEAP_HINT);
  2397. FREE_VAR(bench_tag, HEAP_HINT);
  2398. }
  2399. #endif /* HAVE_AESCCM */
  2400. #endif /* !NO_AES */
  2401. #ifdef HAVE_POLY1305
  2402. void bench_poly1305(void)
  2403. {
  2404. Poly1305 enc;
  2405. byte mac[16];
  2406. double start;
  2407. int ret = 0, i, count;
  2408. if (digest_stream) {
  2409. ret = wc_Poly1305SetKey(&enc, bench_key, 32);
  2410. if (ret != 0) {
  2411. printf("Poly1305SetKey failed, ret = %d\n", ret);
  2412. return;
  2413. }
  2414. bench_stats_start(&count, &start);
  2415. do {
  2416. for (i = 0; i < numBlocks; i++) {
  2417. ret = wc_Poly1305Update(&enc, bench_plain, BENCH_SIZE);
  2418. if (ret != 0) {
  2419. printf("Poly1305Update failed: %d\n", ret);
  2420. break;
  2421. }
  2422. }
  2423. wc_Poly1305Final(&enc, mac);
  2424. count += i;
  2425. } while (bench_stats_sym_check(start));
  2426. bench_stats_sym_finish("POLY1305", 0, count, bench_size, start, ret);
  2427. }
  2428. else {
  2429. bench_stats_start(&count, &start);
  2430. do {
  2431. for (i = 0; i < numBlocks; i++) {
  2432. ret = wc_Poly1305SetKey(&enc, bench_key, 32);
  2433. if (ret != 0) {
  2434. printf("Poly1305SetKey failed, ret = %d\n", ret);
  2435. return;
  2436. }
  2437. ret = wc_Poly1305Update(&enc, bench_plain, BENCH_SIZE);
  2438. if (ret != 0) {
  2439. printf("Poly1305Update failed: %d\n", ret);
  2440. break;
  2441. }
  2442. wc_Poly1305Final(&enc, mac);
  2443. }
  2444. count += i;
  2445. } while (bench_stats_sym_check(start));
  2446. bench_stats_sym_finish("POLY1305", 0, count, bench_size, start, ret);
  2447. }
  2448. }
  2449. #endif /* HAVE_POLY1305 */
  2450. #ifdef HAVE_CAMELLIA
  2451. void bench_camellia(void)
  2452. {
  2453. Camellia cam;
  2454. double start;
  2455. int ret, i, count;
  2456. ret = wc_CamelliaSetKey(&cam, bench_key, 16, bench_iv);
  2457. if (ret != 0) {
  2458. printf("CamelliaSetKey failed, ret = %d\n", ret);
  2459. return;
  2460. }
  2461. bench_stats_start(&count, &start);
  2462. do {
  2463. for (i = 0; i < numBlocks; i++) {
  2464. ret = wc_CamelliaCbcEncrypt(&cam, bench_plain, bench_cipher,
  2465. BENCH_SIZE);
  2466. if (ret < 0) {
  2467. printf("CamelliaCbcEncrypt failed: %d\n", ret);
  2468. return;
  2469. }
  2470. }
  2471. count += i;
  2472. } while (bench_stats_sym_check(start));
  2473. bench_stats_sym_finish("Camellia", 0, count, bench_size, start, ret);
  2474. }
  2475. #endif
  2476. #ifndef NO_DES3
  2477. void bench_des(int doAsync)
  2478. {
  2479. int ret = 0, i, count = 0, times, pending = 0;
  2480. Des3 enc[BENCH_MAX_PENDING];
  2481. double start;
  2482. /* clear for done cleanup */
  2483. XMEMSET(enc, 0, sizeof(enc));
  2484. /* init keys */
  2485. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2486. if ((ret = wc_Des3Init(&enc[i], HEAP_HINT,
  2487. doAsync ? devId : INVALID_DEVID)) != 0) {
  2488. printf("Des3Init failed, ret = %d\n", ret);
  2489. goto exit;
  2490. }
  2491. ret = wc_Des3_SetKey(&enc[i], bench_key, bench_iv, DES_ENCRYPTION);
  2492. if (ret != 0) {
  2493. printf("Des3_SetKey failed, ret = %d\n", ret);
  2494. goto exit;
  2495. }
  2496. }
  2497. bench_stats_start(&count, &start);
  2498. do {
  2499. for (times = 0; times < numBlocks || pending > 0; ) {
  2500. bench_async_poll(&pending);
  2501. /* while free pending slots in queue, submit ops */
  2502. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2503. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, numBlocks, &pending)) {
  2504. ret = wc_Des3_CbcEncrypt(&enc[i], bench_plain, bench_cipher,
  2505. BENCH_SIZE);
  2506. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, &pending)) {
  2507. goto exit_3des;
  2508. }
  2509. }
  2510. } /* for i */
  2511. } /* for times */
  2512. count += times;
  2513. } while (bench_stats_sym_check(start));
  2514. exit_3des:
  2515. bench_stats_sym_finish("3DES", doAsync, count, bench_size, start, ret);
  2516. exit:
  2517. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2518. wc_Des3Free(&enc[i]);
  2519. }
  2520. }
  2521. #endif /* !NO_DES3 */
  2522. #ifdef HAVE_IDEA
  2523. void bench_idea(void)
  2524. {
  2525. Idea enc;
  2526. double start;
  2527. int ret = 0, i, count;
  2528. ret = wc_IdeaSetKey(&enc, bench_key, IDEA_KEY_SIZE, bench_iv,
  2529. IDEA_ENCRYPTION);
  2530. if (ret != 0) {
  2531. printf("Des3_SetKey failed, ret = %d\n", ret);
  2532. return;
  2533. }
  2534. bench_stats_start(&count, &start);
  2535. do {
  2536. for (i = 0; i < numBlocks; i++) {
  2537. wc_IdeaCbcEncrypt(&enc, bench_plain, bench_cipher, BENCH_SIZE);
  2538. }
  2539. count += i;
  2540. } while (bench_stats_sym_check(start));
  2541. bench_stats_sym_finish("IDEA", 0, count, bench_size, start, ret);
  2542. }
  2543. #endif /* HAVE_IDEA */
  2544. #ifndef NO_RC4
  2545. void bench_arc4(int doAsync)
  2546. {
  2547. int ret = 0, i, count = 0, times, pending = 0;
  2548. Arc4 enc[BENCH_MAX_PENDING];
  2549. double start;
  2550. /* clear for done cleanup */
  2551. XMEMSET(enc, 0, sizeof(enc));
  2552. /* init keys */
  2553. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2554. if ((ret = wc_Arc4Init(&enc[i], HEAP_HINT,
  2555. doAsync ? devId : INVALID_DEVID)) != 0) {
  2556. printf("Arc4Init failed, ret = %d\n", ret);
  2557. goto exit;
  2558. }
  2559. ret = wc_Arc4SetKey(&enc[i], bench_key, 16);
  2560. if (ret != 0) {
  2561. printf("Arc4SetKey failed, ret = %d\n", ret);
  2562. goto exit;
  2563. }
  2564. }
  2565. bench_stats_start(&count, &start);
  2566. do {
  2567. for (times = 0; times < numBlocks || pending > 0; ) {
  2568. bench_async_poll(&pending);
  2569. /* while free pending slots in queue, submit ops */
  2570. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2571. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, numBlocks, &pending)) {
  2572. ret = wc_Arc4Process(&enc[i], bench_cipher, bench_plain,
  2573. BENCH_SIZE);
  2574. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, &pending)) {
  2575. goto exit_arc4;
  2576. }
  2577. }
  2578. } /* for i */
  2579. } /* for times */
  2580. count += times;
  2581. } while (bench_stats_sym_check(start));
  2582. exit_arc4:
  2583. bench_stats_sym_finish("ARC4", doAsync, count, bench_size, start, ret);
  2584. exit:
  2585. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2586. wc_Arc4Free(&enc[i]);
  2587. }
  2588. }
  2589. #endif /* !NO_RC4 */
  2590. #ifdef HAVE_HC128
  2591. void bench_hc128(void)
  2592. {
  2593. HC128 enc;
  2594. double start;
  2595. int i, count;
  2596. wc_Hc128_SetKey(&enc, bench_key, bench_iv);
  2597. bench_stats_start(&count, &start);
  2598. do {
  2599. for (i = 0; i < numBlocks; i++) {
  2600. wc_Hc128_Process(&enc, bench_cipher, bench_plain, BENCH_SIZE);
  2601. }
  2602. count += i;
  2603. } while (bench_stats_sym_check(start));
  2604. bench_stats_sym_finish("HC128", 0, count, bench_size, start, 0);
  2605. }
  2606. #endif /* HAVE_HC128 */
  2607. #ifndef NO_RABBIT
  2608. void bench_rabbit(void)
  2609. {
  2610. Rabbit enc;
  2611. double start;
  2612. int i, count;
  2613. wc_RabbitSetKey(&enc, bench_key, bench_iv);
  2614. bench_stats_start(&count, &start);
  2615. do {
  2616. for (i = 0; i < numBlocks; i++) {
  2617. wc_RabbitProcess(&enc, bench_cipher, bench_plain, BENCH_SIZE);
  2618. }
  2619. count += i;
  2620. } while (bench_stats_sym_check(start));
  2621. bench_stats_sym_finish("RABBIT", 0, count, bench_size, start, 0);
  2622. }
  2623. #endif /* NO_RABBIT */
  2624. #ifdef HAVE_CHACHA
  2625. void bench_chacha(void)
  2626. {
  2627. ChaCha enc;
  2628. double start;
  2629. int i, count;
  2630. wc_Chacha_SetKey(&enc, bench_key, 16);
  2631. bench_stats_start(&count, &start);
  2632. do {
  2633. for (i = 0; i < numBlocks; i++) {
  2634. wc_Chacha_SetIV(&enc, bench_iv, 0);
  2635. wc_Chacha_Process(&enc, bench_cipher, bench_plain, BENCH_SIZE);
  2636. }
  2637. count += i;
  2638. } while (bench_stats_sym_check(start));
  2639. bench_stats_sym_finish("CHACHA", 0, count, bench_size, start, 0);
  2640. }
  2641. #endif /* HAVE_CHACHA*/
  2642. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  2643. void bench_chacha20_poly1305_aead(void)
  2644. {
  2645. double start;
  2646. int ret = 0, i, count;
  2647. byte authTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  2648. XMEMSET(authTag, 0, sizeof(authTag));
  2649. bench_stats_start(&count, &start);
  2650. do {
  2651. for (i = 0; i < numBlocks; i++) {
  2652. ret = wc_ChaCha20Poly1305_Encrypt(bench_key, bench_iv, NULL, 0,
  2653. bench_plain, BENCH_SIZE, bench_cipher, authTag);
  2654. if (ret < 0) {
  2655. printf("wc_ChaCha20Poly1305_Encrypt error: %d\n", ret);
  2656. break;
  2657. }
  2658. }
  2659. count += i;
  2660. } while (bench_stats_sym_check(start));
  2661. bench_stats_sym_finish("CHA-POLY", 0, count, bench_size, start, ret);
  2662. }
  2663. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  2664. #ifndef NO_MD5
  2665. void bench_md5(int doAsync)
  2666. {
  2667. wc_Md5 hash[BENCH_MAX_PENDING];
  2668. double start;
  2669. int ret = 0, i, count = 0, times, pending = 0;
  2670. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_MD5_DIGEST_SIZE, HEAP_HINT);
  2671. /* clear for done cleanup */
  2672. XMEMSET(hash, 0, sizeof(hash));
  2673. if (digest_stream) {
  2674. /* init keys */
  2675. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2676. ret = wc_InitMd5_ex(&hash[i], HEAP_HINT,
  2677. doAsync ? devId : INVALID_DEVID);
  2678. if (ret != 0) {
  2679. printf("InitMd5_ex failed, ret = %d\n", ret);
  2680. goto exit;
  2681. }
  2682. #ifdef WOLFSSL_PIC32MZ_HASH
  2683. wc_Md5SizeSet(&hash[i], numBlocks * BENCH_SIZE);
  2684. #endif
  2685. }
  2686. bench_stats_start(&count, &start);
  2687. do {
  2688. for (times = 0; times < numBlocks || pending > 0; ) {
  2689. bench_async_poll(&pending);
  2690. /* while free pending slots in queue, submit ops */
  2691. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2692. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2693. ret = wc_Md5Update(&hash[i], bench_plain,
  2694. BENCH_SIZE);
  2695. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2696. goto exit_md5;
  2697. }
  2698. }
  2699. } /* for i */
  2700. } /* for times */
  2701. count += times;
  2702. times = 0;
  2703. do {
  2704. bench_async_poll(&pending);
  2705. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2706. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2707. ret = wc_Md5Final(&hash[i], digest[i]);
  2708. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2709. goto exit_md5;
  2710. }
  2711. }
  2712. } /* for i */
  2713. } while (pending > 0);
  2714. } while (bench_stats_sym_check(start));
  2715. }
  2716. else {
  2717. bench_stats_start(&count, &start);
  2718. do {
  2719. for (times = 0; times < numBlocks; times++) {
  2720. ret = wc_InitMd5_ex(hash, HEAP_HINT, INVALID_DEVID);
  2721. ret |= wc_Md5Update(hash, bench_plain, BENCH_SIZE);
  2722. ret |= wc_Md5Final(hash, digest[0]);
  2723. if (ret != 0)
  2724. goto exit_md5;
  2725. } /* for times */
  2726. count += times;
  2727. } while (bench_stats_sym_check(start));
  2728. }
  2729. exit_md5:
  2730. bench_stats_sym_finish("MD5", doAsync, count, bench_size, start, ret);
  2731. exit:
  2732. #ifdef WOLFSSL_ASYNC_CRYPT
  2733. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2734. wc_Md5Free(&hash[i]);
  2735. }
  2736. #endif
  2737. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  2738. }
  2739. #endif /* !NO_MD5 */
  2740. #ifndef NO_SHA
  2741. void bench_sha(int doAsync)
  2742. {
  2743. wc_Sha hash[BENCH_MAX_PENDING];
  2744. double start;
  2745. int ret = 0, i, count = 0, times, pending = 0;
  2746. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA_DIGEST_SIZE, HEAP_HINT);
  2747. /* clear for done cleanup */
  2748. XMEMSET(hash, 0, sizeof(hash));
  2749. if (digest_stream) {
  2750. /* init keys */
  2751. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2752. ret = wc_InitSha_ex(&hash[i], HEAP_HINT,
  2753. doAsync ? devId : INVALID_DEVID);
  2754. if (ret != 0) {
  2755. printf("InitSha failed, ret = %d\n", ret);
  2756. goto exit;
  2757. }
  2758. #ifdef WOLFSSL_PIC32MZ_HASH
  2759. wc_ShaSizeSet(&hash[i], numBlocks * BENCH_SIZE);
  2760. #endif
  2761. }
  2762. bench_stats_start(&count, &start);
  2763. do {
  2764. for (times = 0; times < numBlocks || pending > 0; ) {
  2765. bench_async_poll(&pending);
  2766. /* while free pending slots in queue, submit ops */
  2767. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2768. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2769. ret = wc_ShaUpdate(&hash[i], bench_plain,
  2770. BENCH_SIZE);
  2771. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2772. goto exit_sha;
  2773. }
  2774. }
  2775. } /* for i */
  2776. } /* for times */
  2777. count += times;
  2778. times = 0;
  2779. do {
  2780. bench_async_poll(&pending);
  2781. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2782. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2783. ret = wc_ShaFinal(&hash[i], digest[i]);
  2784. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2785. goto exit_sha;
  2786. }
  2787. }
  2788. } /* for i */
  2789. } while (pending > 0);
  2790. } while (bench_stats_sym_check(start));
  2791. }
  2792. else {
  2793. bench_stats_start(&count, &start);
  2794. do {
  2795. for (times = 0; times < numBlocks; times++) {
  2796. ret = wc_InitSha_ex(hash, HEAP_HINT, INVALID_DEVID);
  2797. ret |= wc_ShaUpdate(hash, bench_plain, BENCH_SIZE);
  2798. ret |= wc_ShaFinal(hash, digest[0]);
  2799. if (ret != 0)
  2800. goto exit_sha;
  2801. } /* for times */
  2802. count += times;
  2803. } while (bench_stats_sym_check(start));
  2804. }
  2805. exit_sha:
  2806. bench_stats_sym_finish("SHA", doAsync, count, bench_size, start, ret);
  2807. exit:
  2808. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2809. wc_ShaFree(&hash[i]);
  2810. }
  2811. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  2812. }
  2813. #endif /* NO_SHA */
  2814. #ifdef WOLFSSL_SHA224
  2815. void bench_sha224(int doAsync)
  2816. {
  2817. wc_Sha224 hash[BENCH_MAX_PENDING];
  2818. double start;
  2819. int ret = 0, i, count = 0, times, pending = 0;
  2820. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA224_DIGEST_SIZE, HEAP_HINT);
  2821. /* clear for done cleanup */
  2822. XMEMSET(hash, 0, sizeof(hash));
  2823. if (digest_stream) {
  2824. /* init keys */
  2825. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2826. ret = wc_InitSha224_ex(&hash[i], HEAP_HINT,
  2827. doAsync ? devId : INVALID_DEVID);
  2828. if (ret != 0) {
  2829. printf("InitSha224_ex failed, ret = %d\n", ret);
  2830. goto exit;
  2831. }
  2832. }
  2833. bench_stats_start(&count, &start);
  2834. do {
  2835. for (times = 0; times < numBlocks || pending > 0; ) {
  2836. bench_async_poll(&pending);
  2837. /* while free pending slots in queue, submit ops */
  2838. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2839. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2840. ret = wc_Sha224Update(&hash[i], bench_plain,
  2841. BENCH_SIZE);
  2842. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2843. goto exit_sha224;
  2844. }
  2845. }
  2846. } /* for i */
  2847. } /* for times */
  2848. count += times;
  2849. times = 0;
  2850. do {
  2851. bench_async_poll(&pending);
  2852. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2853. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2854. ret = wc_Sha224Final(&hash[i], digest[i]);
  2855. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2856. goto exit_sha224;
  2857. }
  2858. }
  2859. } /* for i */
  2860. } while (pending > 0);
  2861. } while (bench_stats_sym_check(start));
  2862. }
  2863. else {
  2864. bench_stats_start(&count, &start);
  2865. do {
  2866. for (times = 0; times < numBlocks; times++) {
  2867. ret = wc_InitSha224_ex(hash, HEAP_HINT, INVALID_DEVID);
  2868. ret |= wc_Sha224Update(hash, bench_plain, BENCH_SIZE);
  2869. ret |= wc_Sha224Final(hash, digest[0]);
  2870. if (ret != 0)
  2871. goto exit_sha224;
  2872. } /* for times */
  2873. count += times;
  2874. } while (bench_stats_sym_check(start));
  2875. }
  2876. exit_sha224:
  2877. bench_stats_sym_finish("SHA-224", doAsync, count, bench_size, start, ret);
  2878. exit:
  2879. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2880. wc_Sha224Free(&hash[i]);
  2881. }
  2882. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  2883. }
  2884. #endif
  2885. #ifndef NO_SHA256
  2886. void bench_sha256(int doAsync)
  2887. {
  2888. wc_Sha256 hash[BENCH_MAX_PENDING];
  2889. double start;
  2890. int ret = 0, i, count = 0, times, pending = 0;
  2891. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA256_DIGEST_SIZE, HEAP_HINT);
  2892. /* clear for done cleanup */
  2893. XMEMSET(hash, 0, sizeof(hash));
  2894. if (digest_stream) {
  2895. /* init keys */
  2896. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2897. ret = wc_InitSha256_ex(&hash[i], HEAP_HINT,
  2898. doAsync ? devId : INVALID_DEVID);
  2899. if (ret != 0) {
  2900. printf("InitSha256_ex failed, ret = %d\n", ret);
  2901. goto exit;
  2902. }
  2903. #ifdef WOLFSSL_PIC32MZ_HASH
  2904. wc_Sha256SizeSet(&hash[i], numBlocks * BENCH_SIZE);
  2905. #endif
  2906. }
  2907. bench_stats_start(&count, &start);
  2908. do {
  2909. for (times = 0; times < numBlocks || pending > 0; ) {
  2910. bench_async_poll(&pending);
  2911. /* while free pending slots in queue, submit ops */
  2912. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2913. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2914. ret = wc_Sha256Update(&hash[i], bench_plain,
  2915. BENCH_SIZE);
  2916. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2917. goto exit_sha256;
  2918. }
  2919. }
  2920. } /* for i */
  2921. } /* for times */
  2922. count += times;
  2923. times = 0;
  2924. do {
  2925. bench_async_poll(&pending);
  2926. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2927. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2928. ret = wc_Sha256Final(&hash[i], digest[i]);
  2929. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2930. goto exit_sha256;
  2931. }
  2932. }
  2933. } /* for i */
  2934. } while (pending > 0);
  2935. } while (bench_stats_sym_check(start));
  2936. }
  2937. else {
  2938. bench_stats_start(&count, &start);
  2939. do {
  2940. for (times = 0; times < numBlocks; times++) {
  2941. ret = wc_InitSha256_ex(hash, HEAP_HINT, INVALID_DEVID);
  2942. ret |= wc_Sha256Update(hash, bench_plain, BENCH_SIZE);
  2943. ret |= wc_Sha256Final(hash, digest[0]);
  2944. if (ret != 0)
  2945. goto exit_sha256;
  2946. } /* for times */
  2947. count += times;
  2948. } while (bench_stats_sym_check(start));
  2949. }
  2950. exit_sha256:
  2951. bench_stats_sym_finish("SHA-256", doAsync, count, bench_size, start, ret);
  2952. exit:
  2953. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2954. wc_Sha256Free(&hash[i]);
  2955. }
  2956. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  2957. }
  2958. #endif
  2959. #ifdef WOLFSSL_SHA384
  2960. void bench_sha384(int doAsync)
  2961. {
  2962. wc_Sha384 hash[BENCH_MAX_PENDING];
  2963. double start;
  2964. int ret = 0, i, count = 0, times, pending = 0;
  2965. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA384_DIGEST_SIZE, HEAP_HINT);
  2966. /* clear for done cleanup */
  2967. XMEMSET(hash, 0, sizeof(hash));
  2968. if (digest_stream) {
  2969. /* init keys */
  2970. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2971. ret = wc_InitSha384_ex(&hash[i], HEAP_HINT,
  2972. doAsync ? devId : INVALID_DEVID);
  2973. if (ret != 0) {
  2974. printf("InitSha384_ex failed, ret = %d\n", ret);
  2975. goto exit;
  2976. }
  2977. }
  2978. bench_stats_start(&count, &start);
  2979. do {
  2980. for (times = 0; times < numBlocks || pending > 0; ) {
  2981. bench_async_poll(&pending);
  2982. /* while free pending slots in queue, submit ops */
  2983. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2984. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2985. ret = wc_Sha384Update(&hash[i], bench_plain,
  2986. BENCH_SIZE);
  2987. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2988. goto exit_sha384;
  2989. }
  2990. }
  2991. } /* for i */
  2992. } /* for times */
  2993. count += times;
  2994. times = 0;
  2995. do {
  2996. bench_async_poll(&pending);
  2997. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2998. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2999. ret = wc_Sha384Final(&hash[i], digest[i]);
  3000. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  3001. goto exit_sha384;
  3002. }
  3003. }
  3004. } /* for i */
  3005. } while (pending > 0);
  3006. } while (bench_stats_sym_check(start));
  3007. }
  3008. else {
  3009. bench_stats_start(&count, &start);
  3010. do {
  3011. for (times = 0; times < numBlocks; times++) {
  3012. ret = wc_InitSha384_ex(hash, HEAP_HINT, INVALID_DEVID);
  3013. ret |= wc_Sha384Update(hash, bench_plain, BENCH_SIZE);
  3014. ret |= wc_Sha384Final(hash, digest[0]);
  3015. if (ret != 0)
  3016. goto exit_sha384;
  3017. } /* for times */
  3018. count += times;
  3019. } while (bench_stats_sym_check(start));
  3020. }
  3021. exit_sha384:
  3022. bench_stats_sym_finish("SHA-384", doAsync, count, bench_size, start, ret);
  3023. exit:
  3024. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3025. wc_Sha384Free(&hash[i]);
  3026. }
  3027. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  3028. }
  3029. #endif
  3030. #ifdef WOLFSSL_SHA512
  3031. void bench_sha512(int doAsync)
  3032. {
  3033. wc_Sha512 hash[BENCH_MAX_PENDING];
  3034. double start;
  3035. int ret = 0, i, count = 0, times, pending = 0;
  3036. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA512_DIGEST_SIZE, HEAP_HINT);
  3037. /* clear for done cleanup */
  3038. XMEMSET(hash, 0, sizeof(hash));
  3039. if (digest_stream) {
  3040. /* init keys */
  3041. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3042. ret = wc_InitSha512_ex(&hash[i], HEAP_HINT,
  3043. doAsync ? devId : INVALID_DEVID);
  3044. if (ret != 0) {
  3045. printf("InitSha512_ex failed, ret = %d\n", ret);
  3046. goto exit;
  3047. }
  3048. }
  3049. bench_stats_start(&count, &start);
  3050. do {
  3051. for (times = 0; times < numBlocks || pending > 0; ) {
  3052. bench_async_poll(&pending);
  3053. /* while free pending slots in queue, submit ops */
  3054. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3055. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  3056. ret = wc_Sha512Update(&hash[i], bench_plain,
  3057. BENCH_SIZE);
  3058. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  3059. goto exit_sha512;
  3060. }
  3061. }
  3062. } /* for i */
  3063. } /* for times */
  3064. count += times;
  3065. times = 0;
  3066. do {
  3067. bench_async_poll(&pending);
  3068. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3069. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  3070. ret = wc_Sha512Final(&hash[i], digest[i]);
  3071. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  3072. goto exit_sha512;
  3073. }
  3074. }
  3075. } /* for i */
  3076. } while (pending > 0);
  3077. } while (bench_stats_sym_check(start));
  3078. }
  3079. else {
  3080. bench_stats_start(&count, &start);
  3081. do {
  3082. for (times = 0; times < numBlocks; times++) {
  3083. ret = wc_InitSha512_ex(hash, HEAP_HINT, INVALID_DEVID);
  3084. ret |= wc_Sha512Update(hash, bench_plain, BENCH_SIZE);
  3085. ret |= wc_Sha512Final(hash, digest[0]);
  3086. if (ret != 0)
  3087. goto exit_sha512;
  3088. } /* for times */
  3089. count += times;
  3090. } while (bench_stats_sym_check(start));
  3091. }
  3092. exit_sha512:
  3093. bench_stats_sym_finish("SHA-512", doAsync, count, bench_size, start, ret);
  3094. exit:
  3095. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3096. wc_Sha512Free(&hash[i]);
  3097. }
  3098. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  3099. }
  3100. #endif
  3101. #ifdef WOLFSSL_SHA3
  3102. #ifndef WOLFSSL_NOSHA3_224
  3103. void bench_sha3_224(int doAsync)
  3104. {
  3105. wc_Sha3 hash[BENCH_MAX_PENDING];
  3106. double start;
  3107. int ret = 0, i, count = 0, times, pending = 0;
  3108. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA3_224_DIGEST_SIZE, HEAP_HINT);
  3109. /* clear for done cleanup */
  3110. XMEMSET(hash, 0, sizeof(hash));
  3111. if (digest_stream) {
  3112. /* init keys */
  3113. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3114. ret = wc_InitSha3_224(&hash[i], HEAP_HINT,
  3115. doAsync ? devId : INVALID_DEVID);
  3116. if (ret != 0) {
  3117. printf("InitSha3_224 failed, ret = %d\n", ret);
  3118. goto exit;
  3119. }
  3120. }
  3121. bench_stats_start(&count, &start);
  3122. do {
  3123. for (times = 0; times < numBlocks || pending > 0; ) {
  3124. bench_async_poll(&pending);
  3125. /* while free pending slots in queue, submit ops */
  3126. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3127. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  3128. ret = wc_Sha3_224_Update(&hash[i], bench_plain,
  3129. BENCH_SIZE);
  3130. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  3131. goto exit_sha3_224;
  3132. }
  3133. }
  3134. } /* for i */
  3135. } /* for times */
  3136. count += times;
  3137. times = 0;
  3138. do {
  3139. bench_async_poll(&pending);
  3140. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3141. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  3142. ret = wc_Sha3_224_Final(&hash[i], digest[i]);
  3143. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  3144. goto exit_sha3_224;
  3145. }
  3146. }
  3147. } /* for i */
  3148. } while (pending > 0);
  3149. } while (bench_stats_sym_check(start));
  3150. }
  3151. else {
  3152. bench_stats_start(&count, &start);
  3153. do {
  3154. for (times = 0; times < numBlocks; times++) {
  3155. ret = wc_InitSha3_224(hash, HEAP_HINT, INVALID_DEVID);
  3156. ret |= wc_Sha3_224_Update(hash, bench_plain, BENCH_SIZE);
  3157. ret |= wc_Sha3_224_Final(hash, digest[0]);
  3158. if (ret != 0)
  3159. goto exit_sha3_224;
  3160. } /* for times */
  3161. count += times;
  3162. } while (bench_stats_sym_check(start));
  3163. }
  3164. exit_sha3_224:
  3165. bench_stats_sym_finish("SHA3-224", doAsync, count, bench_size, start, ret);
  3166. exit:
  3167. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3168. wc_Sha3_224_Free(&hash[i]);
  3169. }
  3170. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  3171. }
  3172. #endif /* WOLFSSL_NOSHA3_224 */
  3173. #ifndef WOLFSSL_NOSHA3_256
  3174. void bench_sha3_256(int doAsync)
  3175. {
  3176. wc_Sha3 hash[BENCH_MAX_PENDING];
  3177. double start;
  3178. int ret = 0, i, count = 0, times, pending = 0;
  3179. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA3_256_DIGEST_SIZE, HEAP_HINT);
  3180. /* clear for done cleanup */
  3181. XMEMSET(hash, 0, sizeof(hash));
  3182. if (digest_stream) {
  3183. /* init keys */
  3184. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3185. ret = wc_InitSha3_256(&hash[i], HEAP_HINT,
  3186. doAsync ? devId : INVALID_DEVID);
  3187. if (ret != 0) {
  3188. printf("InitSha3_256 failed, ret = %d\n", ret);
  3189. goto exit;
  3190. }
  3191. }
  3192. bench_stats_start(&count, &start);
  3193. do {
  3194. for (times = 0; times < numBlocks || pending > 0; ) {
  3195. bench_async_poll(&pending);
  3196. /* while free pending slots in queue, submit ops */
  3197. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3198. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  3199. ret = wc_Sha3_256_Update(&hash[i], bench_plain,
  3200. BENCH_SIZE);
  3201. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  3202. goto exit_sha3_256;
  3203. }
  3204. }
  3205. } /* for i */
  3206. } /* for times */
  3207. count += times;
  3208. times = 0;
  3209. do {
  3210. bench_async_poll(&pending);
  3211. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3212. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  3213. ret = wc_Sha3_256_Final(&hash[i], digest[i]);
  3214. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  3215. goto exit_sha3_256;
  3216. }
  3217. }
  3218. } /* for i */
  3219. } while (pending > 0);
  3220. } while (bench_stats_sym_check(start));
  3221. }
  3222. else {
  3223. bench_stats_start(&count, &start);
  3224. do {
  3225. for (times = 0; times < numBlocks; times++) {
  3226. ret = wc_InitSha3_256(hash, HEAP_HINT, INVALID_DEVID);
  3227. ret |= wc_Sha3_256_Update(hash, bench_plain, BENCH_SIZE);
  3228. ret |= wc_Sha3_256_Final(hash, digest[0]);
  3229. if (ret != 0)
  3230. goto exit_sha3_256;
  3231. } /* for times */
  3232. count += times;
  3233. } while (bench_stats_sym_check(start));
  3234. }
  3235. exit_sha3_256:
  3236. bench_stats_sym_finish("SHA3-256", doAsync, count, bench_size, start, ret);
  3237. exit:
  3238. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3239. wc_Sha3_256_Free(&hash[i]);
  3240. }
  3241. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  3242. }
  3243. #endif /* WOLFSSL_NOSHA3_256 */
  3244. #ifndef WOLFSSL_NOSHA3_384
  3245. void bench_sha3_384(int doAsync)
  3246. {
  3247. wc_Sha3 hash[BENCH_MAX_PENDING];
  3248. double start;
  3249. int ret = 0, i, count = 0, times, pending = 0;
  3250. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA3_384_DIGEST_SIZE, HEAP_HINT);
  3251. /* clear for done cleanup */
  3252. XMEMSET(hash, 0, sizeof(hash));
  3253. if (digest_stream) {
  3254. /* init keys */
  3255. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3256. ret = wc_InitSha3_384(&hash[i], HEAP_HINT,
  3257. doAsync ? devId : INVALID_DEVID);
  3258. if (ret != 0) {
  3259. printf("InitSha3_384 failed, ret = %d\n", ret);
  3260. goto exit;
  3261. }
  3262. }
  3263. bench_stats_start(&count, &start);
  3264. do {
  3265. for (times = 0; times < numBlocks || pending > 0; ) {
  3266. bench_async_poll(&pending);
  3267. /* while free pending slots in queue, submit ops */
  3268. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3269. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  3270. ret = wc_Sha3_384_Update(&hash[i], bench_plain,
  3271. BENCH_SIZE);
  3272. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  3273. goto exit_sha3_384;
  3274. }
  3275. }
  3276. } /* for i */
  3277. } /* for times */
  3278. count += times;
  3279. times = 0;
  3280. do {
  3281. bench_async_poll(&pending);
  3282. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3283. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  3284. ret = wc_Sha3_384_Final(&hash[i], digest[i]);
  3285. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  3286. goto exit_sha3_384;
  3287. }
  3288. }
  3289. } /* for i */
  3290. } while (pending > 0);
  3291. } while (bench_stats_sym_check(start));
  3292. }
  3293. else {
  3294. bench_stats_start(&count, &start);
  3295. do {
  3296. for (times = 0; times < numBlocks; times++) {
  3297. ret = wc_InitSha3_384(hash, HEAP_HINT, INVALID_DEVID);
  3298. ret |= wc_Sha3_384_Update(hash, bench_plain, BENCH_SIZE);
  3299. ret |= wc_Sha3_384_Final(hash, digest[0]);
  3300. if (ret != 0)
  3301. goto exit_sha3_384;
  3302. } /* for times */
  3303. count += times;
  3304. } while (bench_stats_sym_check(start));
  3305. }
  3306. exit_sha3_384:
  3307. bench_stats_sym_finish("SHA3-384", doAsync, count, bench_size, start, ret);
  3308. exit:
  3309. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3310. wc_Sha3_384_Free(&hash[i]);
  3311. }
  3312. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  3313. }
  3314. #endif /* WOLFSSL_NOSHA3_384 */
  3315. #ifndef WOLFSSL_NOSHA3_512
  3316. void bench_sha3_512(int doAsync)
  3317. {
  3318. wc_Sha3 hash[BENCH_MAX_PENDING];
  3319. double start;
  3320. int ret = 0, i, count = 0, times, pending = 0;
  3321. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA3_512_DIGEST_SIZE, HEAP_HINT);
  3322. /* clear for done cleanup */
  3323. XMEMSET(hash, 0, sizeof(hash));
  3324. if (digest_stream) {
  3325. /* init keys */
  3326. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3327. ret = wc_InitSha3_512(&hash[i], HEAP_HINT,
  3328. doAsync ? devId : INVALID_DEVID);
  3329. if (ret != 0) {
  3330. printf("InitSha3_512 failed, ret = %d\n", ret);
  3331. goto exit;
  3332. }
  3333. }
  3334. bench_stats_start(&count, &start);
  3335. do {
  3336. for (times = 0; times < numBlocks || pending > 0; ) {
  3337. bench_async_poll(&pending);
  3338. /* while free pending slots in queue, submit ops */
  3339. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3340. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  3341. ret = wc_Sha3_512_Update(&hash[i], bench_plain,
  3342. BENCH_SIZE);
  3343. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  3344. goto exit_sha3_512;
  3345. }
  3346. }
  3347. } /* for i */
  3348. } /* for times */
  3349. count += times;
  3350. times = 0;
  3351. do {
  3352. bench_async_poll(&pending);
  3353. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3354. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  3355. ret = wc_Sha3_512_Final(&hash[i], digest[i]);
  3356. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  3357. goto exit_sha3_512;
  3358. }
  3359. }
  3360. } /* for i */
  3361. } while (pending > 0);
  3362. } while (bench_stats_sym_check(start));
  3363. }
  3364. else {
  3365. bench_stats_start(&count, &start);
  3366. do {
  3367. for (times = 0; times < numBlocks; times++) {
  3368. ret = wc_InitSha3_512(hash, HEAP_HINT, INVALID_DEVID);
  3369. ret |= wc_Sha3_512_Update(hash, bench_plain, BENCH_SIZE);
  3370. ret |= wc_Sha3_512_Final(hash, digest[0]);
  3371. if (ret != 0)
  3372. goto exit_sha3_512;
  3373. } /* for times */
  3374. count += times;
  3375. } while (bench_stats_sym_check(start));
  3376. }
  3377. exit_sha3_512:
  3378. bench_stats_sym_finish("SHA3-512", doAsync, count, bench_size, start, ret);
  3379. exit:
  3380. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3381. wc_Sha3_512_Free(&hash[i]);
  3382. }
  3383. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  3384. }
  3385. #endif /* WOLFSSL_NOSHA3_512 */
  3386. #endif
  3387. #ifdef WOLFSSL_RIPEMD
  3388. int bench_ripemd(void)
  3389. {
  3390. RipeMd hash;
  3391. byte digest[RIPEMD_DIGEST_SIZE];
  3392. double start;
  3393. int i, count, ret = 0;
  3394. if (digest_stream) {
  3395. ret = wc_InitRipeMd(&hash);
  3396. if (ret != 0) {
  3397. return ret;
  3398. }
  3399. bench_stats_start(&count, &start);
  3400. do {
  3401. for (i = 0; i < numBlocks; i++) {
  3402. ret = wc_RipeMdUpdate(&hash, bench_plain, BENCH_SIZE);
  3403. if (ret != 0) {
  3404. return ret;
  3405. }
  3406. }
  3407. ret = wc_RipeMdFinal(&hash, digest);
  3408. if (ret != 0) {
  3409. return ret;
  3410. }
  3411. count += i;
  3412. } while (bench_stats_sym_check(start));
  3413. }
  3414. else {
  3415. bench_stats_start(&count, &start);
  3416. do {
  3417. for (i = 0; i < numBlocks; i++) {
  3418. ret = wc_InitRipeMd(&hash);
  3419. if (ret != 0) {
  3420. return ret;
  3421. }
  3422. ret = wc_RipeMdUpdate(&hash, bench_plain, BENCH_SIZE);
  3423. if (ret != 0) {
  3424. return ret;
  3425. }
  3426. ret = wc_RipeMdFinal(&hash, digest);
  3427. if (ret != 0) {
  3428. return ret;
  3429. }
  3430. }
  3431. count += i;
  3432. } while (bench_stats_sym_check(start));
  3433. }
  3434. bench_stats_sym_finish("RIPEMD", 0, count, bench_size, start, ret);
  3435. return 0;
  3436. }
  3437. #endif
  3438. #ifdef HAVE_BLAKE2
  3439. void bench_blake2b(void)
  3440. {
  3441. Blake2b b2b;
  3442. byte digest[64];
  3443. double start;
  3444. int ret = 0, i, count;
  3445. if (digest_stream) {
  3446. ret = wc_InitBlake2b(&b2b, 64);
  3447. if (ret != 0) {
  3448. printf("InitBlake2b failed, ret = %d\n", ret);
  3449. return;
  3450. }
  3451. bench_stats_start(&count, &start);
  3452. do {
  3453. for (i = 0; i < numBlocks; i++) {
  3454. ret = wc_Blake2bUpdate(&b2b, bench_plain, BENCH_SIZE);
  3455. if (ret != 0) {
  3456. printf("Blake2bUpdate failed, ret = %d\n", ret);
  3457. return;
  3458. }
  3459. }
  3460. ret = wc_Blake2bFinal(&b2b, digest, 64);
  3461. if (ret != 0) {
  3462. printf("Blake2bFinal failed, ret = %d\n", ret);
  3463. return;
  3464. }
  3465. count += i;
  3466. } while (bench_stats_sym_check(start));
  3467. }
  3468. else {
  3469. bench_stats_start(&count, &start);
  3470. do {
  3471. for (i = 0; i < numBlocks; i++) {
  3472. ret = wc_InitBlake2b(&b2b, 64);
  3473. if (ret != 0) {
  3474. printf("InitBlake2b failed, ret = %d\n", ret);
  3475. return;
  3476. }
  3477. ret = wc_Blake2bUpdate(&b2b, bench_plain, BENCH_SIZE);
  3478. if (ret != 0) {
  3479. printf("Blake2bUpdate failed, ret = %d\n", ret);
  3480. return;
  3481. }
  3482. ret = wc_Blake2bFinal(&b2b, digest, 64);
  3483. if (ret != 0) {
  3484. printf("Blake2bFinal failed, ret = %d\n", ret);
  3485. return;
  3486. }
  3487. }
  3488. count += i;
  3489. } while (bench_stats_sym_check(start));
  3490. }
  3491. bench_stats_sym_finish("BLAKE2b", 0, count, bench_size, start, ret);
  3492. }
  3493. #endif
  3494. #if defined(HAVE_BLAKE2S)
  3495. void bench_blake2s(void)
  3496. {
  3497. Blake2s b2s;
  3498. byte digest[32];
  3499. double start;
  3500. int ret = 0, i, count;
  3501. if (digest_stream) {
  3502. ret = wc_InitBlake2s(&b2s, 32);
  3503. if (ret != 0) {
  3504. printf("InitBlake2s failed, ret = %d\n", ret);
  3505. return;
  3506. }
  3507. bench_stats_start(&count, &start);
  3508. do {
  3509. for (i = 0; i < numBlocks; i++) {
  3510. ret = wc_Blake2sUpdate(&b2s, bench_plain, BENCH_SIZE);
  3511. if (ret != 0) {
  3512. printf("Blake2sUpdate failed, ret = %d\n", ret);
  3513. return;
  3514. }
  3515. }
  3516. ret = wc_Blake2sFinal(&b2s, digest, 32);
  3517. if (ret != 0) {
  3518. printf("Blake2sFinal failed, ret = %d\n", ret);
  3519. return;
  3520. }
  3521. count += i;
  3522. } while (bench_stats_sym_check(start));
  3523. }
  3524. else {
  3525. bench_stats_start(&count, &start);
  3526. do {
  3527. for (i = 0; i < numBlocks; i++) {
  3528. ret = wc_InitBlake2s(&b2s, 32);
  3529. if (ret != 0) {
  3530. printf("InitBlake2b failed, ret = %d\n", ret);
  3531. return;
  3532. }
  3533. ret = wc_Blake2sUpdate(&b2s, bench_plain, BENCH_SIZE);
  3534. if (ret != 0) {
  3535. printf("Blake2bUpdate failed, ret = %d\n", ret);
  3536. return;
  3537. }
  3538. ret = wc_Blake2sFinal(&b2s, digest, 32);
  3539. if (ret != 0) {
  3540. printf("Blake2sFinal failed, ret = %d\n", ret);
  3541. return;
  3542. }
  3543. }
  3544. count += i;
  3545. } while (bench_stats_sym_check(start));
  3546. }
  3547. bench_stats_sym_finish("BLAKE2s", 0, count, bench_size, start, ret);
  3548. }
  3549. #endif
  3550. #ifdef WOLFSSL_CMAC
  3551. static void bench_cmac_helper(int keySz, const char* outMsg)
  3552. {
  3553. Cmac cmac;
  3554. byte digest[AES_BLOCK_SIZE];
  3555. word32 digestSz = sizeof(digest);
  3556. double start;
  3557. int ret, i, count;
  3558. bench_stats_start(&count, &start);
  3559. do {
  3560. ret = wc_InitCmac(&cmac, bench_key, keySz, WC_CMAC_AES, NULL);
  3561. if (ret != 0) {
  3562. printf("InitCmac failed, ret = %d\n", ret);
  3563. return;
  3564. }
  3565. for (i = 0; i < numBlocks; i++) {
  3566. ret = wc_CmacUpdate(&cmac, bench_plain, BENCH_SIZE);
  3567. if (ret != 0) {
  3568. printf("CmacUpdate failed, ret = %d\n", ret);
  3569. return;
  3570. }
  3571. }
  3572. /* Note: final force zero's the Cmac struct */
  3573. ret = wc_CmacFinal(&cmac, digest, &digestSz);
  3574. if (ret != 0) {
  3575. printf("CmacFinal failed, ret = %d\n", ret);
  3576. return;
  3577. }
  3578. count += i;
  3579. } while (bench_stats_sym_check(start));
  3580. bench_stats_sym_finish(outMsg, 0, count, bench_size, start, ret);
  3581. }
  3582. void bench_cmac(void)
  3583. {
  3584. #ifdef WOLFSSL_AES_128
  3585. bench_cmac_helper(16, "AES-128-CMAC");
  3586. #endif
  3587. #ifdef WOLFSSL_AES_256
  3588. bench_cmac_helper(32, "AES-256-CMAC");
  3589. #endif
  3590. }
  3591. #endif /* WOLFSSL_CMAC */
  3592. #ifdef HAVE_SCRYPT
  3593. void bench_scrypt(void)
  3594. {
  3595. byte derived[64];
  3596. double start;
  3597. int ret, i, count;
  3598. bench_stats_start(&count, &start);
  3599. do {
  3600. for (i = 0; i < scryptCnt; i++) {
  3601. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  3602. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(derived));
  3603. if (ret != 0) {
  3604. printf("scrypt failed, ret = %d\n", ret);
  3605. goto exit;
  3606. }
  3607. }
  3608. count += i;
  3609. } while (bench_stats_sym_check(start));
  3610. exit:
  3611. bench_stats_asym_finish("scrypt", 17, "", 0, count, start, ret);
  3612. }
  3613. #endif /* HAVE_SCRYPT */
  3614. #ifndef NO_HMAC
  3615. static void bench_hmac(int doAsync, int type, int digestSz,
  3616. byte* key, word32 keySz, const char* label)
  3617. {
  3618. Hmac hmac[BENCH_MAX_PENDING];
  3619. double start;
  3620. int ret = 0, i, count = 0, times, pending = 0;
  3621. #ifdef WOLFSSL_ASYNC_CRYPT
  3622. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_MAX_DIGEST_SIZE, HEAP_HINT);
  3623. #else
  3624. byte digest[BENCH_MAX_PENDING][WC_MAX_DIGEST_SIZE];
  3625. #endif
  3626. (void)digestSz;
  3627. /* clear for done cleanup */
  3628. XMEMSET(hmac, 0, sizeof(hmac));
  3629. /* init keys */
  3630. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3631. ret = wc_HmacInit(&hmac[i], HEAP_HINT,
  3632. doAsync ? devId : INVALID_DEVID);
  3633. if (ret != 0) {
  3634. printf("wc_HmacInit failed for %s, ret = %d\n", label, ret);
  3635. goto exit;
  3636. }
  3637. ret = wc_HmacSetKey(&hmac[i], type, key, keySz);
  3638. if (ret != 0) {
  3639. printf("wc_HmacSetKey failed for %s, ret = %d\n", label, ret);
  3640. goto exit;
  3641. }
  3642. }
  3643. bench_stats_start(&count, &start);
  3644. do {
  3645. for (times = 0; times < numBlocks || pending > 0; ) {
  3646. bench_async_poll(&pending);
  3647. /* while free pending slots in queue, submit ops */
  3648. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3649. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hmac[i]), 0,
  3650. &times, numBlocks, &pending)) {
  3651. ret = wc_HmacUpdate(&hmac[i], bench_plain, BENCH_SIZE);
  3652. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hmac[i]),
  3653. 0, &times, &pending)) {
  3654. goto exit_hmac;
  3655. }
  3656. }
  3657. } /* for i */
  3658. } /* for times */
  3659. count += times;
  3660. times = 0;
  3661. do {
  3662. bench_async_poll(&pending);
  3663. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3664. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hmac[i]), 0,
  3665. &times, numBlocks, &pending)) {
  3666. ret = wc_HmacFinal(&hmac[i], digest[i]);
  3667. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hmac[i]),
  3668. 0, &times, &pending)) {
  3669. goto exit_hmac;
  3670. }
  3671. }
  3672. } /* for i */
  3673. } while (pending > 0);
  3674. } while (bench_stats_sym_check(start));
  3675. exit_hmac:
  3676. bench_stats_sym_finish(label, doAsync, count, bench_size, start, ret);
  3677. exit:
  3678. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3679. wc_HmacFree(&hmac[i]);
  3680. }
  3681. #ifdef WOLFSSL_ASYNC_CRYPT
  3682. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  3683. #endif
  3684. }
  3685. #ifndef NO_MD5
  3686. void bench_hmac_md5(int doAsync)
  3687. {
  3688. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3689. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  3690. bench_hmac(doAsync, WC_MD5, WC_MD5_DIGEST_SIZE, key, sizeof(key),
  3691. "HMAC-MD5");
  3692. }
  3693. #endif /* NO_MD5 */
  3694. #ifndef NO_SHA
  3695. void bench_hmac_sha(int doAsync)
  3696. {
  3697. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3698. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3699. 0x0b, 0x0b, 0x0b, 0x0b };
  3700. bench_hmac(doAsync, WC_SHA, WC_SHA_DIGEST_SIZE, key, sizeof(key),
  3701. "HMAC-SHA");
  3702. }
  3703. #endif /* NO_SHA */
  3704. #ifdef WOLFSSL_SHA224
  3705. void bench_hmac_sha224(int doAsync)
  3706. {
  3707. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3708. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3709. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3710. 0x0b, 0x0b, 0x0b, 0x0b };
  3711. bench_hmac(doAsync, WC_SHA224, WC_SHA224_DIGEST_SIZE, key, sizeof(key),
  3712. "HMAC-SHA224");
  3713. }
  3714. #endif /* WOLFSSL_SHA224 */
  3715. #ifndef NO_SHA256
  3716. void bench_hmac_sha256(int doAsync)
  3717. {
  3718. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3719. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3720. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3721. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  3722. bench_hmac(doAsync, WC_SHA256, WC_SHA256_DIGEST_SIZE, key, sizeof(key),
  3723. "HMAC-SHA256");
  3724. }
  3725. #endif /* NO_SHA256 */
  3726. #ifdef WOLFSSL_SHA384
  3727. void bench_hmac_sha384(int doAsync)
  3728. {
  3729. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3730. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3731. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3732. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3733. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3734. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  3735. bench_hmac(doAsync, WC_SHA384, WC_SHA384_DIGEST_SIZE, key, sizeof(key),
  3736. "HMAC-SHA384");
  3737. }
  3738. #endif /* WOLFSSL_SHA384 */
  3739. #ifdef WOLFSSL_SHA512
  3740. void bench_hmac_sha512(int doAsync)
  3741. {
  3742. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3743. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3744. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3745. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3746. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3747. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3748. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3749. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  3750. bench_hmac(doAsync, WC_SHA512, WC_SHA512_DIGEST_SIZE, key, sizeof(key),
  3751. "HMAC-SHA512");
  3752. }
  3753. #endif /* WOLFSSL_SHA512 */
  3754. #ifndef NO_PWDBASED
  3755. void bench_pbkdf2(void)
  3756. {
  3757. double start;
  3758. int ret = 0, count = 0;
  3759. const char* passwd32 = "passwordpasswordpasswordpassword";
  3760. const byte salt32[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06,
  3761. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06,
  3762. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06,
  3763. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  3764. byte derived[32];
  3765. bench_stats_start(&count, &start);
  3766. do {
  3767. ret = wc_PBKDF2(derived, (const byte*)passwd32, (int)XSTRLEN(passwd32),
  3768. salt32, (int)sizeof(salt32), 1000, 32, WC_SHA256);
  3769. count++;
  3770. } while (bench_stats_sym_check(start));
  3771. bench_stats_sym_finish("PBKDF2", 32, count, 32, start, ret);
  3772. }
  3773. #endif /* !NO_PWDBASED */
  3774. #endif /* NO_HMAC */
  3775. #ifndef NO_RSA
  3776. #if defined(WOLFSSL_KEY_GEN)
  3777. static void bench_rsaKeyGen_helper(int doAsync, int keySz)
  3778. {
  3779. RsaKey genKey[BENCH_MAX_PENDING];
  3780. double start;
  3781. int ret = 0, i, count = 0, times, pending = 0;
  3782. const long rsa_e_val = WC_RSA_EXPONENT;
  3783. const char**desc = bench_desc_words[lng_index];
  3784. /* clear for done cleanup */
  3785. XMEMSET(genKey, 0, sizeof(genKey));
  3786. bench_stats_start(&count, &start);
  3787. do {
  3788. /* while free pending slots in queue, submit ops */
  3789. for (times = 0; times < genTimes || pending > 0; ) {
  3790. bench_async_poll(&pending);
  3791. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3792. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, &times, genTimes, &pending)) {
  3793. wc_FreeRsaKey(&genKey[i]);
  3794. ret = wc_InitRsaKey_ex(&genKey[i], HEAP_HINT,
  3795. doAsync ? devId : INVALID_DEVID);
  3796. if (ret < 0) {
  3797. goto exit;
  3798. }
  3799. ret = wc_MakeRsaKey(&genKey[i], keySz, rsa_e_val, &gRng);
  3800. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, &times, &pending)) {
  3801. goto exit;
  3802. }
  3803. }
  3804. } /* for i */
  3805. } /* for times */
  3806. count += times;
  3807. } while (bench_stats_sym_check(start));
  3808. exit:
  3809. bench_stats_asym_finish("RSA", keySz, desc[2], doAsync, count, start, ret);
  3810. /* cleanup */
  3811. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3812. wc_FreeRsaKey(&genKey[i]);
  3813. }
  3814. }
  3815. void bench_rsaKeyGen(int doAsync)
  3816. {
  3817. int k, keySz;
  3818. #ifndef WOLFSSL_SP_MATH
  3819. const int keySizes[2] = {1024, 2048};
  3820. #else
  3821. const int keySizes[1] = {2048};
  3822. #endif
  3823. for (k = 0; k < (int)(sizeof(keySizes)/sizeof(int)); k++) {
  3824. keySz = keySizes[k];
  3825. bench_rsaKeyGen_helper(doAsync, keySz);
  3826. }
  3827. }
  3828. void bench_rsaKeyGen_size(int doAsync, int keySz)
  3829. {
  3830. bench_rsaKeyGen_helper(doAsync, keySz);
  3831. }
  3832. #endif /* WOLFSSL_KEY_GEN */
  3833. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  3834. !defined(USE_CERT_BUFFERS_3072)
  3835. #if defined(WOLFSSL_MDK_SHELL)
  3836. static char *certRSAname = "certs/rsa2048.der";
  3837. /* set by shell command */
  3838. static void set_Bench_RSA_File(char * cert) { certRSAname = cert ; }
  3839. #elif defined(FREESCALE_MQX)
  3840. static char *certRSAname = "a:\\certs\\rsa2048.der";
  3841. #else
  3842. static const char *certRSAname = "certs/rsa2048.der";
  3843. #endif
  3844. #endif
  3845. #define RSA_BUF_SIZE 384 /* for up to 3072 bit */
  3846. #if !defined(WOLFSSL_RSA_VERIFY_INLINE) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  3847. #elif defined(WOLFSSL_PUBLIC_MP) || !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  3848. #if defined(USE_CERT_BUFFERS_2048)
  3849. static unsigned char rsa_2048_sig[] = {
  3850. 0x8c, 0x9e, 0x37, 0xbf, 0xc3, 0xa6, 0xba, 0x1c,
  3851. 0x53, 0x22, 0x40, 0x4b, 0x8b, 0x0d, 0x3c, 0x0e,
  3852. 0x2e, 0x8c, 0x31, 0x2c, 0x47, 0xbf, 0x03, 0x48,
  3853. 0x18, 0x46, 0x73, 0x8d, 0xd7, 0xdd, 0x17, 0x64,
  3854. 0x0d, 0x7f, 0xdc, 0x74, 0xed, 0x80, 0xc3, 0xe8,
  3855. 0x9a, 0x18, 0x33, 0xd4, 0xe6, 0xc5, 0xe1, 0x54,
  3856. 0x75, 0xd1, 0xbb, 0x40, 0xde, 0xa8, 0xb9, 0x1b,
  3857. 0x14, 0xe8, 0xc1, 0x39, 0xeb, 0xa0, 0x69, 0x8a,
  3858. 0xc6, 0x9b, 0xef, 0x53, 0xb5, 0x23, 0x2b, 0x78,
  3859. 0x06, 0x43, 0x37, 0x11, 0x81, 0x84, 0x73, 0x33,
  3860. 0x33, 0xfe, 0xf7, 0x5d, 0x2b, 0x84, 0xd6, 0x83,
  3861. 0xd6, 0xdd, 0x55, 0x33, 0xef, 0xd1, 0xf7, 0x12,
  3862. 0xb0, 0xc2, 0x0e, 0xb1, 0x78, 0xd4, 0xa8, 0xa3,
  3863. 0x25, 0xeb, 0xed, 0x9a, 0xb3, 0xee, 0xc3, 0x7e,
  3864. 0xce, 0x13, 0x18, 0x86, 0x31, 0xe1, 0xef, 0x01,
  3865. 0x0f, 0x6e, 0x67, 0x24, 0x74, 0xbd, 0x0b, 0x7f,
  3866. 0xa9, 0xca, 0x6f, 0xaa, 0x83, 0x28, 0x90, 0x40,
  3867. 0xf1, 0xb5, 0x10, 0x0e, 0x26, 0x03, 0x05, 0x5d,
  3868. 0x87, 0xb4, 0xe0, 0x4c, 0x98, 0xd8, 0xc6, 0x42,
  3869. 0x89, 0x77, 0xeb, 0xb6, 0xd4, 0xe6, 0x26, 0xf3,
  3870. 0x31, 0x25, 0xde, 0x28, 0x38, 0x58, 0xe8, 0x2c,
  3871. 0xf4, 0x56, 0x7c, 0xb6, 0xfd, 0x99, 0xb0, 0xb0,
  3872. 0xf4, 0x83, 0xb6, 0x74, 0xa9, 0x5b, 0x9f, 0xe8,
  3873. 0xe9, 0xf1, 0xa1, 0x2a, 0xbd, 0xf6, 0x83, 0x28,
  3874. 0x09, 0xda, 0xa6, 0xd6, 0xcd, 0x61, 0x60, 0xf7,
  3875. 0x13, 0x4e, 0x46, 0x57, 0x38, 0x1e, 0x11, 0x92,
  3876. 0x6b, 0x6b, 0xcf, 0xd3, 0xf4, 0x8b, 0x66, 0x03,
  3877. 0x25, 0xa3, 0x7a, 0x2f, 0xce, 0xc1, 0x85, 0xa5,
  3878. 0x48, 0x91, 0x8a, 0xb3, 0x4f, 0x5d, 0x98, 0xb1,
  3879. 0x69, 0x58, 0x47, 0x69, 0x0c, 0x52, 0xdc, 0x42,
  3880. 0x4c, 0xef, 0xe8, 0xd4, 0x4d, 0x6a, 0x33, 0x7d,
  3881. 0x9e, 0xd2, 0x51, 0xe6, 0x41, 0xbf, 0x4f, 0xa2
  3882. };
  3883. #elif defined(USE_CERT_BUFFERS_3072)
  3884. static unsigned char rsa_3072_sig[] = {
  3885. 0x1a, 0xd6, 0x0d, 0xfd, 0xe3, 0x41, 0x95, 0x76,
  3886. 0x27, 0x16, 0x7d, 0xc7, 0x94, 0x16, 0xca, 0xa8,
  3887. 0x26, 0x08, 0xbe, 0x78, 0x87, 0x72, 0x4c, 0xd9,
  3888. 0xa7, 0xfc, 0x33, 0x77, 0x2d, 0x53, 0x07, 0xb5,
  3889. 0x8c, 0xce, 0x48, 0x17, 0x9b, 0xff, 0x9f, 0x9b,
  3890. 0x17, 0xc4, 0xbb, 0x72, 0xed, 0xdb, 0xa0, 0x34,
  3891. 0x69, 0x5b, 0xc7, 0x4e, 0xbf, 0xec, 0x13, 0xc5,
  3892. 0x98, 0x71, 0x9a, 0x4e, 0x18, 0x0e, 0xcb, 0xe7,
  3893. 0xc6, 0xd5, 0x21, 0x31, 0x7c, 0x0d, 0xae, 0x14,
  3894. 0x2b, 0x87, 0x4f, 0x77, 0x95, 0x2e, 0x26, 0xe2,
  3895. 0x83, 0xfe, 0x49, 0x1e, 0x87, 0x19, 0x4a, 0x63,
  3896. 0x73, 0x75, 0xf1, 0xf5, 0x71, 0xd2, 0xce, 0xd4,
  3897. 0x39, 0x2b, 0xd9, 0xe0, 0x76, 0x70, 0xc8, 0xf8,
  3898. 0xed, 0xdf, 0x90, 0x57, 0x17, 0xb9, 0x16, 0xf6,
  3899. 0xe9, 0x49, 0x48, 0xce, 0x5a, 0x8b, 0xe4, 0x84,
  3900. 0x7c, 0xf3, 0x31, 0x68, 0x97, 0x45, 0x68, 0x38,
  3901. 0x50, 0x3a, 0x70, 0xbd, 0xb3, 0xd3, 0xd2, 0xe0,
  3902. 0x56, 0x5b, 0xc2, 0x0c, 0x2c, 0x10, 0x70, 0x7b,
  3903. 0xd4, 0x99, 0xf9, 0x38, 0x31, 0xb1, 0x86, 0xa0,
  3904. 0x07, 0xf1, 0xf6, 0x53, 0xb0, 0x44, 0x82, 0x40,
  3905. 0xd2, 0xab, 0x0e, 0x71, 0x5d, 0xe1, 0xea, 0x3a,
  3906. 0x77, 0xc9, 0xef, 0xfe, 0x54, 0x65, 0xa3, 0x49,
  3907. 0xfd, 0xa5, 0x33, 0xaa, 0x16, 0x1a, 0x38, 0xe7,
  3908. 0xaa, 0xb7, 0x13, 0xb2, 0x3b, 0xc7, 0x00, 0x87,
  3909. 0x12, 0xfe, 0xfd, 0xf4, 0x55, 0x6d, 0x1d, 0x4a,
  3910. 0x0e, 0xad, 0xd0, 0x4c, 0x55, 0x91, 0x60, 0xd9,
  3911. 0xef, 0x74, 0x69, 0x22, 0x8c, 0x51, 0x65, 0xc2,
  3912. 0x04, 0xac, 0xd3, 0x8d, 0xf7, 0x35, 0x29, 0x13,
  3913. 0x6d, 0x61, 0x7c, 0x39, 0x2f, 0x41, 0x4c, 0xdf,
  3914. 0x38, 0xfd, 0x1a, 0x7d, 0x42, 0xa7, 0x6f, 0x3f,
  3915. 0x3d, 0x9b, 0xd1, 0x97, 0xab, 0xc0, 0xa7, 0x28,
  3916. 0x1c, 0xc0, 0x02, 0x26, 0xeb, 0xce, 0xf9, 0xe1,
  3917. 0x34, 0x45, 0xaf, 0xbf, 0x8d, 0xb8, 0xe0, 0xff,
  3918. 0xd9, 0x6f, 0x77, 0xf3, 0xf7, 0xed, 0x6a, 0xbb,
  3919. 0x03, 0x52, 0xfb, 0x38, 0xfc, 0xea, 0x9f, 0xc9,
  3920. 0x98, 0xed, 0x21, 0x45, 0xaf, 0x43, 0x2b, 0x64,
  3921. 0x96, 0x82, 0x30, 0xe9, 0xb4, 0x36, 0x89, 0x77,
  3922. 0x07, 0x4a, 0xc6, 0x1f, 0x38, 0x7a, 0xee, 0xb6,
  3923. 0x86, 0xf6, 0x2f, 0x03, 0xec, 0xa2, 0xe5, 0x48,
  3924. 0xe5, 0x5a, 0xf5, 0x1c, 0xd2, 0xd9, 0xd8, 0x2d,
  3925. 0x9d, 0x06, 0x07, 0xc9, 0x8b, 0x5d, 0xe0, 0x0f,
  3926. 0x5e, 0x0c, 0x53, 0x27, 0xff, 0x23, 0xee, 0xca,
  3927. 0x5e, 0x4d, 0xf1, 0x95, 0x77, 0x78, 0x1f, 0xf2,
  3928. 0x44, 0x5b, 0x7d, 0x01, 0x49, 0x61, 0x6f, 0x6d,
  3929. 0xbf, 0xf5, 0x19, 0x06, 0x39, 0xe9, 0xe9, 0x29,
  3930. 0xde, 0x47, 0x5e, 0x2e, 0x1f, 0x68, 0xf4, 0x32,
  3931. 0x5e, 0xe9, 0xd0, 0xa7, 0xb4, 0x2a, 0x45, 0xdf,
  3932. 0x15, 0x7d, 0x0d, 0x5b, 0xef, 0xc6, 0x23, 0xac
  3933. };
  3934. #else
  3935. #error Not Supported Yet!
  3936. #endif
  3937. #endif
  3938. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_PUBLIC_MP)
  3939. static void bench_rsa_helper(int doAsync, RsaKey rsaKey[BENCH_MAX_PENDING],
  3940. int rsaKeySz)
  3941. {
  3942. int ret = 0, i, times, count = 0, pending = 0;
  3943. word32 idx = 0;
  3944. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  3945. const char* messageStr = "Everyone gets Friday off.";
  3946. const int len = (int)XSTRLEN((char*)messageStr);
  3947. #endif
  3948. double start = 0.0f;
  3949. const char**desc = bench_desc_words[lng_index];
  3950. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  3951. DECLARE_VAR_INIT(message, byte, len, messageStr, HEAP_HINT);
  3952. #endif
  3953. #if !defined(WOLFSSL_MDK5_COMPLv5)
  3954. /* MDK5 compiler regard this as a executable statement, and does not allow declarations after the line. */
  3955. DECLARE_ARRAY_DYNAMIC_DEC(enc, byte, BENCH_MAX_PENDING, rsaKeySz, HEAP_HINT);
  3956. #else
  3957. byte* enc[BENCH_MAX_PENDING];
  3958. #endif
  3959. #if !defined(WOLFSSL_RSA_VERIFY_INLINE) && \
  3960. !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  3961. #if !defined(WOLFSSL_MDK5_COMPLv5)
  3962. /* MDK5 compiler regard this as a executable statement, and does not allow declarations after the line. */
  3963. DECLARE_ARRAY_DYNAMIC_DEC(out, byte, BENCH_MAX_PENDING, rsaKeySz, HEAP_HINT);
  3964. #else
  3965. int idxout;
  3966. byte* out[BENCH_MAX_PENDING];
  3967. #endif
  3968. #else
  3969. byte* out[BENCH_MAX_PENDING];
  3970. #endif
  3971. DECLARE_ARRAY_DYNAMIC_EXE(enc, byte, BENCH_MAX_PENDING, rsaKeySz, HEAP_HINT);
  3972. #if !defined(WOLFSSL_RSA_VERIFY_INLINE) && \
  3973. !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  3974. DECLARE_ARRAY_DYNAMIC_EXE(out, byte, BENCH_MAX_PENDING, rsaKeySz, HEAP_HINT);
  3975. #endif
  3976. if (!rsa_sign_verify) {
  3977. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  3978. /* begin public RSA */
  3979. bench_stats_start(&count, &start);
  3980. do {
  3981. for (times = 0; times < ntimes || pending > 0; ) {
  3982. bench_async_poll(&pending);
  3983. /* while free pending slots in queue, submit ops */
  3984. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3985. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  3986. 1, &times, ntimes, &pending)) {
  3987. ret = wc_RsaPublicEncrypt(message, (word32)len, enc[i],
  3988. rsaKeySz/8, &rsaKey[i],
  3989. &gRng);
  3990. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(
  3991. &rsaKey[i]), 1, &times, &pending)) {
  3992. goto exit_rsa_pub;
  3993. }
  3994. }
  3995. } /* for i */
  3996. } /* for times */
  3997. count += times;
  3998. } while (bench_stats_sym_check(start));
  3999. exit_rsa_pub:
  4000. bench_stats_asym_finish("RSA", rsaKeySz, desc[0], doAsync, count,
  4001. start, ret);
  4002. #endif
  4003. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  4004. if (ret < 0) {
  4005. goto exit;
  4006. }
  4007. /* capture resulting encrypt length */
  4008. idx = (word32)(rsaKeySz/8);
  4009. /* begin private async RSA */
  4010. bench_stats_start(&count, &start);
  4011. do {
  4012. for (times = 0; times < ntimes || pending > 0; ) {
  4013. bench_async_poll(&pending);
  4014. /* while free pending slots in queue, submit ops */
  4015. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4016. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  4017. 1, &times, ntimes, &pending)) {
  4018. ret = wc_RsaPrivateDecrypt(enc[i], idx, out[i],
  4019. rsaKeySz/8, &rsaKey[i]);
  4020. if (!bench_async_handle(&ret,
  4021. BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  4022. 1, &times, &pending)) {
  4023. goto exit;
  4024. }
  4025. }
  4026. } /* for i */
  4027. } /* for times */
  4028. count += times;
  4029. } while (bench_stats_sym_check(start));
  4030. exit:
  4031. bench_stats_asym_finish("RSA", rsaKeySz, desc[1], doAsync, count,
  4032. start, ret);
  4033. #endif
  4034. }
  4035. else {
  4036. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  4037. /* begin RSA sign */
  4038. bench_stats_start(&count, &start);
  4039. do {
  4040. for (times = 0; times < ntimes || pending > 0; ) {
  4041. bench_async_poll(&pending);
  4042. /* while free pending slots in queue, submit ops */
  4043. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4044. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  4045. 1, &times, ntimes, &pending)) {
  4046. ret = wc_RsaSSL_Sign(message, len, enc[i],
  4047. rsaKeySz/8, &rsaKey[i], &gRng);
  4048. if (!bench_async_handle(&ret,
  4049. BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  4050. 1, &times, &pending)) {
  4051. goto exit_rsa_sign;
  4052. }
  4053. }
  4054. } /* for i */
  4055. } /* for times */
  4056. count += times;
  4057. } while (bench_stats_sym_check(start));
  4058. exit_rsa_sign:
  4059. bench_stats_asym_finish("RSA", rsaKeySz, desc[4], doAsync, count, start,
  4060. ret);
  4061. if (ret < 0) {
  4062. goto exit;
  4063. }
  4064. #endif
  4065. /* capture resulting encrypt length */
  4066. idx = rsaKeySz/8;
  4067. /* begin RSA verify */
  4068. bench_stats_start(&count, &start);
  4069. do {
  4070. for (times = 0; times < ntimes || pending > 0; ) {
  4071. bench_async_poll(&pending);
  4072. /* while free pending slots in queue, submit ops */
  4073. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4074. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  4075. 1, &times, ntimes, &pending)) {
  4076. #if !defined(WOLFSSL_RSA_VERIFY_INLINE) && \
  4077. !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  4078. ret = wc_RsaSSL_Verify(enc[i], idx, out[i],
  4079. rsaKeySz/8, &rsaKey[i]);
  4080. #elif defined(USE_CERT_BUFFERS_2048)
  4081. XMEMCPY(enc[i], rsa_2048_sig, sizeof(rsa_2048_sig));
  4082. idx = sizeof(rsa_2048_sig);
  4083. out[i] = NULL;
  4084. ret = wc_RsaSSL_VerifyInline(enc[i], idx, &out[i],
  4085. &rsaKey[i]);
  4086. if (ret > 0)
  4087. ret = 0;
  4088. #elif defined(USE_CERT_BUFFERS_3072)
  4089. XMEMCPY(enc[i], rsa_3072_sig, sizeof(rsa_3072_sig));
  4090. idx = sizeof(rsa_3072_sig);
  4091. out[i] = NULL;
  4092. ret = wc_RsaSSL_VerifyInline(enc[i], idx, &out[i],
  4093. &rsaKey[i]);
  4094. if (ret > 0)
  4095. ret = 0;
  4096. #endif
  4097. if (!bench_async_handle(&ret,
  4098. BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  4099. 1, &times, &pending)) {
  4100. goto exit_rsa_verify;
  4101. }
  4102. }
  4103. } /* for i */
  4104. } /* for times */
  4105. count += times;
  4106. } while (bench_stats_sym_check(start));
  4107. exit_rsa_verify:
  4108. bench_stats_asym_finish("RSA", rsaKeySz, desc[5], doAsync, count,
  4109. start, ret);
  4110. }
  4111. FREE_ARRAY_DYNAMIC(enc, BENCH_MAX_PENDING, HEAP_HINT);
  4112. #if !defined(WOLFSSL_RSA_VERIFY_INLINE) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  4113. FREE_ARRAY_DYNAMIC(out, BENCH_MAX_PENDING, HEAP_HINT);
  4114. #endif
  4115. FREE_VAR(message, HEAP_HINT);
  4116. }
  4117. #endif
  4118. void bench_rsa(int doAsync)
  4119. {
  4120. int ret = 0, i;
  4121. RsaKey rsaKey[BENCH_MAX_PENDING];
  4122. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_PUBLIC_MP)
  4123. int rsaKeySz; /* used in printf */
  4124. size_t bytes;
  4125. const byte* tmp;
  4126. word32 idx;
  4127. #ifdef USE_CERT_BUFFERS_1024
  4128. tmp = rsa_key_der_1024;
  4129. bytes = (size_t)sizeof_rsa_key_der_1024;
  4130. rsaKeySz = 1024;
  4131. #elif defined(USE_CERT_BUFFERS_2048)
  4132. tmp = rsa_key_der_2048;
  4133. bytes = (size_t)sizeof_rsa_key_der_2048;
  4134. rsaKeySz = 2048;
  4135. #elif defined(USE_CERT_BUFFERS_3072)
  4136. tmp = rsa_key_der_3072;
  4137. bytes = (size_t)sizeof_rsa_key_der_3072;
  4138. rsaKeySz = 3072;
  4139. #else
  4140. #error "need a cert buffer size"
  4141. #endif /* USE_CERT_BUFFERS */
  4142. #endif
  4143. /* clear for done cleanup */
  4144. XMEMSET(rsaKey, 0, sizeof(rsaKey));
  4145. /* init keys */
  4146. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4147. /* setup an async context for each key */
  4148. if ((ret = wc_InitRsaKey_ex(&rsaKey[i], HEAP_HINT,
  4149. doAsync ? devId : INVALID_DEVID)) < 0) {
  4150. goto exit_bench_rsa;
  4151. }
  4152. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  4153. #ifdef WC_RSA_BLINDING
  4154. ret = wc_RsaSetRNG(&rsaKey[i], &gRng);
  4155. if (ret != 0)
  4156. goto exit_bench_rsa;
  4157. #endif
  4158. #endif
  4159. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  4160. /* decode the private key */
  4161. idx = 0;
  4162. if ((ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsaKey[i],
  4163. (word32)bytes)) != 0) {
  4164. printf("wc_RsaPrivateKeyDecode failed! %d\n", ret);
  4165. goto exit_bench_rsa;
  4166. }
  4167. #elif defined(WOLFSSL_PUBLIC_MP)
  4168. #ifdef USE_CERT_BUFFERS_2048
  4169. ret = mp_read_unsigned_bin(&rsaKey[i].n, &tmp[12], 256);
  4170. if (ret != 0) {
  4171. printf("wc_RsaPrivateKeyDecode failed! %d\n", ret);
  4172. goto exit_bench_rsa;
  4173. }
  4174. ret = mp_set_int(&rsaKey[i].e, WC_RSA_EXPONENT);
  4175. if (ret != 0) {
  4176. printf("wc_RsaPrivateKeyDecode failed! %d\n", ret);
  4177. goto exit_bench_rsa;
  4178. }
  4179. #else
  4180. #error Not supported yet!
  4181. #endif
  4182. (void)idx;
  4183. (void)bytes;
  4184. #endif
  4185. }
  4186. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) || defined(WOLFSSL_PUBLIC_MP)
  4187. bench_rsa_helper(doAsync, rsaKey, rsaKeySz);
  4188. #endif
  4189. exit_bench_rsa:
  4190. /* cleanup */
  4191. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4192. wc_FreeRsaKey(&rsaKey[i]);
  4193. }
  4194. }
  4195. #ifdef WOLFSSL_KEY_GEN
  4196. /* bench any size of RSA key */
  4197. void bench_rsa_key(int doAsync, int rsaKeySz)
  4198. {
  4199. int ret = 0, i, pending = 0;
  4200. RsaKey rsaKey[BENCH_MAX_PENDING];
  4201. int isPending[BENCH_MAX_PENDING];
  4202. long exp = 65537l;
  4203. /* clear for done cleanup */
  4204. XMEMSET(rsaKey, 0, sizeof(rsaKey));
  4205. XMEMSET(isPending, 0, sizeof(isPending));
  4206. /* init keys */
  4207. do {
  4208. pending = 0;
  4209. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4210. if (!isPending[i]) { /* if making the key is pending then just call
  4211. * wc_MakeRsaKey again */
  4212. /* setup an async context for each key */
  4213. if ((ret = wc_InitRsaKey_ex(&rsaKey[i], HEAP_HINT,
  4214. doAsync ? devId : INVALID_DEVID)) < 0) {
  4215. goto exit_bench_rsa_key;
  4216. }
  4217. #ifdef WC_RSA_BLINDING
  4218. ret = wc_RsaSetRNG(&rsaKey[i], &gRng);
  4219. if (ret != 0)
  4220. goto exit_bench_rsa_key;
  4221. #endif
  4222. }
  4223. /* create the RSA key */
  4224. ret = wc_MakeRsaKey(&rsaKey[i], rsaKeySz, exp, &gRng);
  4225. if (ret == WC_PENDING_E) {
  4226. isPending[i] = 1;
  4227. pending = 1;
  4228. }
  4229. else if (ret != 0) {
  4230. printf("wc_MakeRsaKey failed! %d\n", ret);
  4231. goto exit_bench_rsa_key;
  4232. }
  4233. } /* for i */
  4234. } while (pending > 0);
  4235. bench_rsa_helper(doAsync, rsaKey, rsaKeySz);
  4236. exit_bench_rsa_key:
  4237. /* cleanup */
  4238. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4239. wc_FreeRsaKey(&rsaKey[i]);
  4240. }
  4241. }
  4242. #endif /* WOLFSSL_KEY_GEN */
  4243. #endif /* !NO_RSA */
  4244. #ifndef NO_DH
  4245. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  4246. !defined(USE_CERT_BUFFERS_3072)
  4247. #if defined(WOLFSSL_MDK_SHELL)
  4248. static char *certDHname = "certs/dh2048.der";
  4249. /* set by shell command */
  4250. void set_Bench_DH_File(char * cert) { certDHname = cert ; }
  4251. #elif defined(FREESCALE_MQX)
  4252. static char *certDHname = "a:\\certs\\dh2048.der";
  4253. #elif defined(NO_ASN)
  4254. /* do nothing, but don't need a file */
  4255. #else
  4256. static const char *certDHname = "certs/dh2048.der";
  4257. #endif
  4258. #endif
  4259. #define BENCH_DH_KEY_SIZE 384 /* for 3072 bit */
  4260. #define BENCH_DH_PRIV_SIZE (BENCH_DH_KEY_SIZE/8)
  4261. void bench_dh(int doAsync)
  4262. {
  4263. int ret = 0, i;
  4264. int count = 0, times, pending = 0;
  4265. const byte* tmp = NULL;
  4266. double start = 0.0f;
  4267. DhKey dhKey[BENCH_MAX_PENDING];
  4268. int dhKeySz = BENCH_DH_KEY_SIZE * 8; /* used in printf */
  4269. const char**desc = bench_desc_words[lng_index];
  4270. #ifndef NO_ASN
  4271. size_t bytes = 0;
  4272. word32 idx;
  4273. #endif
  4274. word32 pubSz[BENCH_MAX_PENDING];
  4275. word32 privSz[BENCH_MAX_PENDING];
  4276. word32 pubSz2;
  4277. word32 privSz2;
  4278. word32 agreeSz[BENCH_MAX_PENDING];
  4279. #ifdef HAVE_FFDHE_2048
  4280. const DhParams *params = NULL;
  4281. #endif
  4282. DECLARE_ARRAY(pub, byte, BENCH_MAX_PENDING, BENCH_DH_KEY_SIZE, HEAP_HINT);
  4283. DECLARE_VAR(pub2, byte, BENCH_DH_KEY_SIZE, HEAP_HINT);
  4284. DECLARE_ARRAY(agree, byte, BENCH_MAX_PENDING, BENCH_DH_KEY_SIZE, HEAP_HINT);
  4285. DECLARE_ARRAY(priv, byte, BENCH_MAX_PENDING, BENCH_DH_PRIV_SIZE, HEAP_HINT);
  4286. DECLARE_VAR(priv2, byte, BENCH_DH_PRIV_SIZE, HEAP_HINT);
  4287. (void)tmp;
  4288. if (!use_ffdhe) {
  4289. #if defined(NO_ASN)
  4290. dhKeySz = 1024;
  4291. /* do nothing, but don't use default FILE */
  4292. #elif defined(USE_CERT_BUFFERS_1024)
  4293. tmp = dh_key_der_1024;
  4294. bytes = (size_t)sizeof_dh_key_der_1024;
  4295. dhKeySz = 1024;
  4296. #elif defined(USE_CERT_BUFFERS_2048)
  4297. tmp = dh_key_der_2048;
  4298. bytes = (size_t)sizeof_dh_key_der_2048;
  4299. dhKeySz = 2048;
  4300. #elif defined(USE_CERT_BUFFERS_3072)
  4301. tmp = dh_key_der_3072;
  4302. bytes = (size_t)sizeof_dh_key_der_3072;
  4303. dhKeySz = 3072;
  4304. #else
  4305. #error "need to define a cert buffer size"
  4306. #endif /* USE_CERT_BUFFERS */
  4307. }
  4308. #ifdef HAVE_FFDHE_2048
  4309. else if (use_ffdhe == 2048) {
  4310. params = wc_Dh_ffdhe2048_Get();
  4311. dhKeySz = 2048;
  4312. }
  4313. #endif
  4314. #ifdef HAVE_FFDHE_3072
  4315. else if (use_ffdhe == 3072) {
  4316. params = wc_Dh_ffdhe3072_Get();
  4317. dhKeySz = 3072;
  4318. }
  4319. #endif
  4320. /* clear for done cleanup */
  4321. XMEMSET(dhKey, 0, sizeof(dhKey));
  4322. /* init keys */
  4323. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4324. /* setup an async context for each key */
  4325. ret = wc_InitDhKey_ex(&dhKey[i], HEAP_HINT,
  4326. doAsync ? devId : INVALID_DEVID);
  4327. if (ret != 0)
  4328. goto exit;
  4329. /* setup key */
  4330. if (!use_ffdhe) {
  4331. #ifdef NO_ASN
  4332. ret = wc_DhSetKey(&dhKey[i], dh_p, sizeof(dh_p), dh_g,
  4333. sizeof(dh_g));
  4334. #else
  4335. idx = 0;
  4336. ret = wc_DhKeyDecode(tmp, &idx, &dhKey[i], (word32)bytes);
  4337. #endif
  4338. }
  4339. #if defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072)
  4340. else if (params != NULL) {
  4341. ret = wc_DhSetKey(&dhKey[i], params->p, params->p_len, params->g,
  4342. params->g_len);
  4343. }
  4344. #endif
  4345. if (ret != 0) {
  4346. printf("DhKeyDecode failed %d, can't benchmark\n", ret);
  4347. goto exit;
  4348. }
  4349. }
  4350. /* Key Gen */
  4351. bench_stats_start(&count, &start);
  4352. do {
  4353. /* while free pending slots in queue, submit ops */
  4354. for (times = 0; times < genTimes || pending > 0; ) {
  4355. bench_async_poll(&pending);
  4356. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4357. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, &times, genTimes, &pending)) {
  4358. privSz[i] = 0;
  4359. ret = wc_DhGenerateKeyPair(&dhKey[i], &gRng, priv[i], &privSz[i],
  4360. pub[i], &pubSz[i]);
  4361. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, &times, &pending)) {
  4362. goto exit_dh_gen;
  4363. }
  4364. }
  4365. } /* for i */
  4366. } /* for times */
  4367. count += times;
  4368. } while (bench_stats_sym_check(start));
  4369. exit_dh_gen:
  4370. bench_stats_asym_finish("DH", dhKeySz, desc[2], doAsync, count, start, ret);
  4371. if (ret < 0) {
  4372. goto exit;
  4373. }
  4374. /* Generate key to use as other public */
  4375. ret = wc_DhGenerateKeyPair(&dhKey[0], &gRng, priv2, &privSz2, pub2, &pubSz2);
  4376. #ifdef WOLFSSL_ASYNC_CRYPT
  4377. ret = wc_AsyncWait(ret, &dhKey[0].asyncDev, WC_ASYNC_FLAG_NONE);
  4378. #endif
  4379. /* Key Agree */
  4380. bench_stats_start(&count, &start);
  4381. do {
  4382. for (times = 0; times < agreeTimes || pending > 0; ) {
  4383. bench_async_poll(&pending);
  4384. /* while free pending slots in queue, submit ops */
  4385. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4386. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, &times, agreeTimes, &pending)) {
  4387. ret = wc_DhAgree(&dhKey[i], agree[i], &agreeSz[i], priv[i], privSz[i],
  4388. pub2, pubSz2);
  4389. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, &times, &pending)) {
  4390. goto exit;
  4391. }
  4392. }
  4393. } /* for i */
  4394. } /* for times */
  4395. count += times;
  4396. } while (bench_stats_sym_check(start));
  4397. exit:
  4398. bench_stats_asym_finish("DH", dhKeySz, desc[3], doAsync, count, start, ret);
  4399. /* cleanup */
  4400. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4401. wc_FreeDhKey(&dhKey[i]);
  4402. }
  4403. FREE_ARRAY(pub, BENCH_MAX_PENDING, HEAP_HINT);
  4404. FREE_VAR(pub2, HEAP_HINT);
  4405. FREE_ARRAY(priv, BENCH_MAX_PENDING, HEAP_HINT);
  4406. FREE_VAR(priv2, HEAP_HINT);
  4407. FREE_ARRAY(agree, BENCH_MAX_PENDING, HEAP_HINT);
  4408. }
  4409. #endif /* !NO_DH */
  4410. #ifdef HAVE_NTRU
  4411. byte GetEntropy(ENTROPY_CMD cmd, byte* out);
  4412. byte GetEntropy(ENTROPY_CMD cmd, byte* out)
  4413. {
  4414. if (cmd == INIT)
  4415. return 1; /* using local rng */
  4416. if (out == NULL)
  4417. return 0;
  4418. if (cmd == GET_BYTE_OF_ENTROPY)
  4419. return (wc_RNG_GenerateBlock(&gRng, out, 1) == 0) ? 1 : 0;
  4420. if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
  4421. *out = 1;
  4422. return 1;
  4423. }
  4424. return 0;
  4425. }
  4426. void bench_ntru(void)
  4427. {
  4428. int i;
  4429. double start;
  4430. byte public_key[1027];
  4431. word16 public_key_len = sizeof(public_key);
  4432. byte private_key[1120];
  4433. word16 private_key_len = sizeof(private_key);
  4434. word16 ntruBits = 128;
  4435. word16 type = 0;
  4436. word32 ret;
  4437. byte ciphertext[1022];
  4438. word16 ciphertext_len;
  4439. byte plaintext[16];
  4440. word16 plaintext_len;
  4441. const char**desc = bench_desc_words[lng_index];
  4442. DRBG_HANDLE drbg;
  4443. static byte const aes_key[] = {
  4444. 0xf3, 0xe9, 0x87, 0xbb, 0x18, 0x08, 0x3c, 0xaa,
  4445. 0x7b, 0x12, 0x49, 0x88, 0xaf, 0xb3, 0x22, 0xd8
  4446. };
  4447. static byte const wolfsslStr[] = {
  4448. 'w', 'o', 'l', 'f', 'S', 'S', 'L', ' ', 'N', 'T', 'R', 'U'
  4449. };
  4450. for (ntruBits = 128; ntruBits < 257; ntruBits += 64) {
  4451. switch (ntruBits) {
  4452. case 128:
  4453. type = NTRU_EES439EP1;
  4454. break;
  4455. case 192:
  4456. type = NTRU_EES593EP1;
  4457. break;
  4458. case 256:
  4459. type = NTRU_EES743EP1;
  4460. break;
  4461. }
  4462. ret = ntru_crypto_drbg_instantiate(ntruBits, wolfsslStr,
  4463. sizeof(wolfsslStr), (ENTROPY_FN) GetEntropy, &drbg);
  4464. if(ret != DRBG_OK) {
  4465. printf("NTRU drbg instantiate failed\n");
  4466. return;
  4467. }
  4468. /* set key sizes */
  4469. ret = ntru_crypto_ntru_encrypt_keygen(drbg, type, &public_key_len,
  4470. NULL, &private_key_len, NULL);
  4471. if (ret != NTRU_OK) {
  4472. ntru_crypto_drbg_uninstantiate(drbg);
  4473. printf("NTRU failed to get key lengths\n");
  4474. return;
  4475. }
  4476. ret = ntru_crypto_ntru_encrypt_keygen(drbg, type, &public_key_len,
  4477. public_key, &private_key_len,
  4478. private_key);
  4479. ntru_crypto_drbg_uninstantiate(drbg);
  4480. if (ret != NTRU_OK) {
  4481. printf("NTRU keygen failed\n");
  4482. return;
  4483. }
  4484. ret = ntru_crypto_drbg_instantiate(ntruBits, NULL, 0,
  4485. (ENTROPY_FN)GetEntropy, &drbg);
  4486. if (ret != DRBG_OK) {
  4487. printf("NTRU error occurred during DRBG instantiation\n");
  4488. return;
  4489. }
  4490. ret = ntru_crypto_ntru_encrypt(drbg, public_key_len, public_key,
  4491. sizeof(aes_key), aes_key, &ciphertext_len, NULL);
  4492. if (ret != NTRU_OK) {
  4493. printf("NTRU error occurred requesting the buffer size needed\n");
  4494. return;
  4495. }
  4496. bench_stats_start(&i, &start);
  4497. for (i = 0; i < ntimes; i++) {
  4498. ret = ntru_crypto_ntru_encrypt(drbg, public_key_len, public_key,
  4499. sizeof(aes_key), aes_key, &ciphertext_len, ciphertext);
  4500. if (ret != NTRU_OK) {
  4501. printf("NTRU encrypt error\n");
  4502. return;
  4503. }
  4504. }
  4505. bench_stats_asym_finish("NTRU", ntruBits, desc[6], 0, i, start, ret);
  4506. ret = ntru_crypto_drbg_uninstantiate(drbg);
  4507. if (ret != DRBG_OK) {
  4508. printf("NTRU error occurred uninstantiating the DRBG\n");
  4509. return;
  4510. }
  4511. ret = ntru_crypto_ntru_decrypt(private_key_len, private_key,
  4512. ciphertext_len, ciphertext, &plaintext_len, NULL);
  4513. if (ret != NTRU_OK) {
  4514. printf("NTRU decrypt error occurred getting the buffer size needed\n");
  4515. return;
  4516. }
  4517. plaintext_len = sizeof(plaintext);
  4518. bench_stats_start(&i, &start);
  4519. for (i = 0; i < ntimes; i++) {
  4520. ret = ntru_crypto_ntru_decrypt(private_key_len, private_key,
  4521. ciphertext_len, ciphertext,
  4522. &plaintext_len, plaintext);
  4523. if (ret != NTRU_OK) {
  4524. printf("NTRU error occurred decrypting the key\n");
  4525. return;
  4526. }
  4527. }
  4528. bench_stats_asym_finish("NTRU", ntruBits, desc[7], 0, i, start, ret);
  4529. }
  4530. }
  4531. void bench_ntruKeyGen(void)
  4532. {
  4533. double start;
  4534. int i;
  4535. byte public_key[1027];
  4536. word16 public_key_len = sizeof(public_key);
  4537. byte private_key[1120];
  4538. word16 private_key_len = sizeof(private_key);
  4539. word16 ntruBits = 128;
  4540. word16 type = 0;
  4541. word32 ret;
  4542. const char**desc = bench_desc_words[lng_index];
  4543. DRBG_HANDLE drbg;
  4544. static uint8_t const pers_str[] = {
  4545. 'w', 'o', 'l', 'f', 'S', 'S', 'L', ' ', 't', 'e', 's', 't'
  4546. };
  4547. for (ntruBits = 128; ntruBits < 257; ntruBits += 64) {
  4548. ret = ntru_crypto_drbg_instantiate(ntruBits, pers_str,
  4549. sizeof(pers_str), GetEntropy, &drbg);
  4550. if (ret != DRBG_OK) {
  4551. printf("NTRU drbg instantiate failed\n");
  4552. return;
  4553. }
  4554. switch (ntruBits) {
  4555. case 128:
  4556. type = NTRU_EES439EP1;
  4557. break;
  4558. case 192:
  4559. type = NTRU_EES593EP1;
  4560. break;
  4561. case 256:
  4562. type = NTRU_EES743EP1;
  4563. break;
  4564. }
  4565. /* set key sizes */
  4566. ret = ntru_crypto_ntru_encrypt_keygen(drbg, type, &public_key_len,
  4567. NULL, &private_key_len, NULL);
  4568. bench_stats_start(&i, &start);
  4569. for (i = 0; i < genTimes; i++) {
  4570. ret = ntru_crypto_ntru_encrypt_keygen(drbg, type, &public_key_len,
  4571. public_key, &private_key_len,
  4572. private_key);
  4573. }
  4574. bench_stats_asym_finish("NTRU", ntruBits, desc[2], 0, i, start, ret);
  4575. if (ret != NTRU_OK) {
  4576. return;
  4577. }
  4578. ret = ntru_crypto_drbg_uninstantiate(drbg);
  4579. if (ret != NTRU_OK) {
  4580. printf("NTRU drbg uninstantiate failed\n");
  4581. return;
  4582. }
  4583. }
  4584. }
  4585. #endif
  4586. #ifdef HAVE_ECC
  4587. /* Detect ECC key size to use */
  4588. #ifndef BENCH_ECC_SIZE
  4589. #ifndef NO_ECC256
  4590. #define BENCH_ECC_SIZE 32
  4591. #elif defined(HAVE_ECC384)
  4592. #define BENCH_ECC_SIZE 48
  4593. #elif defined(HAVE_ECC224)
  4594. #define BENCH_ECC_SIZE 28
  4595. #else
  4596. #error No ECC keygen size defined for benchmark
  4597. #endif
  4598. #endif
  4599. static int bench_ecc_size = BENCH_ECC_SIZE;
  4600. void bench_eccMakeKey(int doAsync)
  4601. {
  4602. int ret = 0, i, times, count, pending = 0;
  4603. const int keySize = bench_ecc_size;
  4604. ecc_key genKey[BENCH_MAX_PENDING];
  4605. double start;
  4606. const char**desc = bench_desc_words[lng_index];
  4607. /* clear for done cleanup */
  4608. XMEMSET(&genKey, 0, sizeof(genKey));
  4609. /* ECC Make Key */
  4610. bench_stats_start(&count, &start);
  4611. do {
  4612. /* while free pending slots in queue, submit ops */
  4613. for (times = 0; times < genTimes || pending > 0; ) {
  4614. bench_async_poll(&pending);
  4615. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4616. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, &times, genTimes, &pending)) {
  4617. wc_ecc_free(&genKey[i]);
  4618. ret = wc_ecc_init_ex(&genKey[i], HEAP_HINT, doAsync ? devId : INVALID_DEVID);
  4619. if (ret < 0) {
  4620. goto exit;
  4621. }
  4622. ret = wc_ecc_make_key(&gRng, keySize, &genKey[i]);
  4623. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, &times, &pending)) {
  4624. goto exit;
  4625. }
  4626. }
  4627. } /* for i */
  4628. } /* for times */
  4629. count += times;
  4630. } while (bench_stats_sym_check(start));
  4631. exit:
  4632. bench_stats_asym_finish("ECC", keySize * 8, desc[2], doAsync, count, start, ret);
  4633. /* cleanup */
  4634. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4635. wc_ecc_free(&genKey[i]);
  4636. }
  4637. }
  4638. void bench_ecc(int doAsync)
  4639. {
  4640. int ret = 0, i, times, count, pending = 0;
  4641. const int keySize = bench_ecc_size;
  4642. ecc_key genKey[BENCH_MAX_PENDING];
  4643. #ifdef HAVE_ECC_DHE
  4644. ecc_key genKey2[BENCH_MAX_PENDING];
  4645. #endif
  4646. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  4647. #ifdef HAVE_ECC_VERIFY
  4648. int verify[BENCH_MAX_PENDING];
  4649. #endif
  4650. #endif
  4651. word32 x[BENCH_MAX_PENDING];
  4652. double start;
  4653. const char**desc = bench_desc_words[lng_index];
  4654. #ifdef HAVE_ECC_DHE
  4655. DECLARE_ARRAY(shared, byte, BENCH_MAX_PENDING, BENCH_ECC_SIZE, HEAP_HINT);
  4656. #endif
  4657. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  4658. DECLARE_ARRAY(sig, byte, BENCH_MAX_PENDING, ECC_MAX_SIG_SIZE, HEAP_HINT);
  4659. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, BENCH_ECC_SIZE, HEAP_HINT);
  4660. #endif
  4661. /* clear for done cleanup */
  4662. XMEMSET(&genKey, 0, sizeof(genKey));
  4663. #ifdef HAVE_ECC_DHE
  4664. XMEMSET(&genKey2, 0, sizeof(genKey2));
  4665. #endif
  4666. /* init keys */
  4667. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4668. /* setup an context for each key */
  4669. if ((ret = wc_ecc_init_ex(&genKey[i], HEAP_HINT,
  4670. doAsync ? devId : INVALID_DEVID)) < 0) {
  4671. goto exit;
  4672. }
  4673. ret = wc_ecc_make_key(&gRng, keySize, &genKey[i]);
  4674. #ifdef WOLFSSL_ASYNC_CRYPT
  4675. ret = wc_AsyncWait(ret, &genKey[i].asyncDev, WC_ASYNC_FLAG_NONE);
  4676. #endif
  4677. if (ret < 0) {
  4678. goto exit;
  4679. }
  4680. #ifdef HAVE_ECC_DHE
  4681. if ((ret = wc_ecc_init_ex(&genKey2[i], HEAP_HINT, INVALID_DEVID)) < 0) {
  4682. goto exit;
  4683. }
  4684. if ((ret = wc_ecc_make_key(&gRng, keySize, &genKey2[i])) > 0) {
  4685. goto exit;
  4686. }
  4687. #endif
  4688. }
  4689. #ifdef HAVE_ECC_DHE
  4690. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  4691. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) && \
  4692. !defined(HAVE_SELFTEST)
  4693. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4694. (void)wc_ecc_set_rng(&genKey[i], &gRng);
  4695. }
  4696. #endif
  4697. /* ECC Shared Secret */
  4698. bench_stats_start(&count, &start);
  4699. do {
  4700. for (times = 0; times < agreeTimes || pending > 0; ) {
  4701. bench_async_poll(&pending);
  4702. /* while free pending slots in queue, submit ops */
  4703. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4704. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, &times, agreeTimes, &pending)) {
  4705. x[i] = (word32)keySize;
  4706. ret = wc_ecc_shared_secret(&genKey[i], &genKey2[i], shared[i], &x[i]);
  4707. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, &times, &pending)) {
  4708. goto exit_ecdhe;
  4709. }
  4710. }
  4711. } /* for i */
  4712. } /* for times */
  4713. count += times;
  4714. } while (bench_stats_sym_check(start));
  4715. exit_ecdhe:
  4716. bench_stats_asym_finish("ECDHE", keySize * 8, desc[3], doAsync, count, start, ret);
  4717. if (ret < 0) {
  4718. goto exit;
  4719. }
  4720. #endif /* HAVE_ECC_DHE */
  4721. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  4722. /* Init digest to sign */
  4723. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4724. for (count = 0; count < keySize; count++) {
  4725. digest[i][count] = (byte)count;
  4726. }
  4727. }
  4728. /* ECC Sign */
  4729. bench_stats_start(&count, &start);
  4730. do {
  4731. for (times = 0; times < agreeTimes || pending > 0; ) {
  4732. bench_async_poll(&pending);
  4733. /* while free pending slots in queue, submit ops */
  4734. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4735. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, &times, agreeTimes, &pending)) {
  4736. if (genKey[i].state == 0)
  4737. x[i] = ECC_MAX_SIG_SIZE;
  4738. ret = wc_ecc_sign_hash(digest[i], (word32)keySize, sig[i], &x[i],
  4739. &gRng, &genKey[i]);
  4740. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, &times, &pending)) {
  4741. goto exit_ecdsa_sign;
  4742. }
  4743. }
  4744. } /* for i */
  4745. } /* for times */
  4746. count += times;
  4747. } while (bench_stats_sym_check(start));
  4748. exit_ecdsa_sign:
  4749. bench_stats_asym_finish("ECDSA", keySize * 8, desc[4], doAsync, count, start, ret);
  4750. if (ret < 0) {
  4751. goto exit;
  4752. }
  4753. #ifdef HAVE_ECC_VERIFY
  4754. /* ECC Verify */
  4755. bench_stats_start(&count, &start);
  4756. do {
  4757. for (times = 0; times < agreeTimes || pending > 0; ) {
  4758. bench_async_poll(&pending);
  4759. /* while free pending slots in queue, submit ops */
  4760. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4761. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, &times, agreeTimes, &pending)) {
  4762. if (genKey[i].state == 0)
  4763. verify[i] = 0;
  4764. ret = wc_ecc_verify_hash(sig[i], x[i], digest[i],
  4765. (word32)keySize, &verify[i], &genKey[i]);
  4766. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, &times, &pending)) {
  4767. goto exit_ecdsa_verify;
  4768. }
  4769. }
  4770. } /* for i */
  4771. } /* for times */
  4772. count += times;
  4773. } while (bench_stats_sym_check(start));
  4774. exit_ecdsa_verify:
  4775. bench_stats_asym_finish("ECDSA", keySize * 8, desc[5], doAsync, count, start, ret);
  4776. #endif /* HAVE_ECC_VERIFY */
  4777. #endif /* !NO_ASN && HAVE_ECC_SIGN */
  4778. exit:
  4779. /* cleanup */
  4780. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4781. wc_ecc_free(&genKey[i]);
  4782. #ifdef HAVE_ECC_DHE
  4783. wc_ecc_free(&genKey2[i]);
  4784. #endif
  4785. }
  4786. #ifdef HAVE_ECC_DHE
  4787. FREE_ARRAY(shared, BENCH_MAX_PENDING, HEAP_HINT);
  4788. #endif
  4789. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  4790. FREE_ARRAY(sig, BENCH_MAX_PENDING, HEAP_HINT);
  4791. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  4792. #endif
  4793. }
  4794. #ifdef HAVE_ECC_ENCRYPT
  4795. void bench_eccEncrypt(void)
  4796. {
  4797. ecc_key userA, userB;
  4798. const int keySize = bench_ecc_size;
  4799. byte msg[48];
  4800. byte out[80];
  4801. word32 outSz = sizeof(out);
  4802. word32 bench_plainSz = BENCH_SIZE;
  4803. int ret, i, count;
  4804. double start;
  4805. const char**desc = bench_desc_words[lng_index];
  4806. ret = wc_ecc_init_ex(&userA, HEAP_HINT, devId);
  4807. if (ret != 0) {
  4808. printf("wc_ecc_encrypt make key A failed: %d\n", ret);
  4809. return;
  4810. }
  4811. ret = wc_ecc_init_ex(&userB, HEAP_HINT, devId);
  4812. if (ret != 0) {
  4813. printf("wc_ecc_encrypt make key B failed: %d\n", ret);
  4814. wc_ecc_free(&userA);
  4815. return;
  4816. }
  4817. ret = wc_ecc_make_key(&gRng, keySize, &userA);
  4818. #ifdef WOLFSSL_ASYNC_CRYPT
  4819. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_NONE);
  4820. #endif
  4821. if (ret != 0)
  4822. goto exit;
  4823. ret = wc_ecc_make_key(&gRng, keySize, &userB);
  4824. #ifdef WOLFSSL_ASYNC_CRYPT
  4825. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_NONE);
  4826. #endif
  4827. if (ret != 0)
  4828. goto exit;
  4829. for (i = 0; i < (int)sizeof(msg); i++)
  4830. msg[i] = i;
  4831. bench_stats_start(&count, &start);
  4832. do {
  4833. for (i = 0; i < ntimes; i++) {
  4834. /* encrypt msg to B */
  4835. ret = wc_ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz, NULL);
  4836. if (ret != 0) {
  4837. printf("wc_ecc_encrypt failed! %d\n", ret);
  4838. goto exit_enc;
  4839. }
  4840. }
  4841. count += i;
  4842. } while (bench_stats_sym_check(start));
  4843. exit_enc:
  4844. bench_stats_asym_finish("ECC", keySize * 8, desc[6], 0, count, start, ret);
  4845. bench_stats_start(&count, &start);
  4846. do {
  4847. for (i = 0; i < ntimes; i++) {
  4848. /* decrypt msg from A */
  4849. ret = wc_ecc_decrypt(&userB, &userA, out, outSz, bench_plain, &bench_plainSz, NULL);
  4850. if (ret != 0) {
  4851. printf("wc_ecc_decrypt failed! %d\n", ret);
  4852. goto exit_dec;
  4853. }
  4854. }
  4855. count += i;
  4856. } while (bench_stats_sym_check(start));
  4857. exit_dec:
  4858. bench_stats_asym_finish("ECC", keySize * 8, desc[7], 0, count, start, ret);
  4859. exit:
  4860. /* cleanup */
  4861. wc_ecc_free(&userB);
  4862. wc_ecc_free(&userA);
  4863. }
  4864. #endif
  4865. #endif /* HAVE_ECC */
  4866. #ifdef HAVE_CURVE25519
  4867. void bench_curve25519KeyGen(void)
  4868. {
  4869. curve25519_key genKey;
  4870. double start;
  4871. int ret = 0, i, count;
  4872. const char**desc = bench_desc_words[lng_index];
  4873. /* Key Gen */
  4874. bench_stats_start(&count, &start);
  4875. do {
  4876. for (i = 0; i < genTimes; i++) {
  4877. ret = wc_curve25519_make_key(&gRng, 32, &genKey);
  4878. wc_curve25519_free(&genKey);
  4879. if (ret != 0) {
  4880. printf("wc_curve25519_make_key failed: %d\n", ret);
  4881. break;
  4882. }
  4883. }
  4884. count += i;
  4885. } while (bench_stats_sym_check(start));
  4886. bench_stats_asym_finish("CURVE", 25519, desc[2], 0, count, start, ret);
  4887. }
  4888. #ifdef HAVE_CURVE25519_SHARED_SECRET
  4889. void bench_curve25519KeyAgree(void)
  4890. {
  4891. curve25519_key genKey, genKey2;
  4892. double start;
  4893. int ret, i, count;
  4894. byte shared[32];
  4895. const char**desc = bench_desc_words[lng_index];
  4896. word32 x = 0;
  4897. wc_curve25519_init(&genKey);
  4898. wc_curve25519_init(&genKey2);
  4899. ret = wc_curve25519_make_key(&gRng, 32, &genKey);
  4900. if (ret != 0) {
  4901. printf("curve25519_make_key failed\n");
  4902. return;
  4903. }
  4904. ret = wc_curve25519_make_key(&gRng, 32, &genKey2);
  4905. if (ret != 0) {
  4906. printf("curve25519_make_key failed: %d\n", ret);
  4907. wc_curve25519_free(&genKey);
  4908. return;
  4909. }
  4910. /* Shared secret */
  4911. bench_stats_start(&count, &start);
  4912. do {
  4913. for (i = 0; i < agreeTimes; i++) {
  4914. x = sizeof(shared);
  4915. ret = wc_curve25519_shared_secret(&genKey, &genKey2, shared, &x);
  4916. if (ret != 0) {
  4917. printf("curve25519_shared_secret failed: %d\n", ret);
  4918. goto exit;
  4919. }
  4920. }
  4921. count += i;
  4922. } while (bench_stats_sym_check(start));
  4923. exit:
  4924. bench_stats_asym_finish("CURVE", 25519, desc[3], 0, count, start, ret);
  4925. wc_curve25519_free(&genKey2);
  4926. wc_curve25519_free(&genKey);
  4927. }
  4928. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  4929. #endif /* HAVE_CURVE25519 */
  4930. #ifdef HAVE_ED25519
  4931. void bench_ed25519KeyGen(void)
  4932. {
  4933. ed25519_key genKey;
  4934. double start;
  4935. int i, count;
  4936. const char**desc = bench_desc_words[lng_index];
  4937. /* Key Gen */
  4938. bench_stats_start(&count, &start);
  4939. do {
  4940. for (i = 0; i < genTimes; i++) {
  4941. wc_ed25519_init(&genKey);
  4942. (void)wc_ed25519_make_key(&gRng, 32, &genKey);
  4943. wc_ed25519_free(&genKey);
  4944. }
  4945. count += i;
  4946. } while (bench_stats_sym_check(start));
  4947. bench_stats_asym_finish("ED", 25519, desc[2], 0, count, start, 0);
  4948. }
  4949. void bench_ed25519KeySign(void)
  4950. {
  4951. int ret;
  4952. ed25519_key genKey;
  4953. #ifdef HAVE_ED25519_SIGN
  4954. double start;
  4955. int i, count;
  4956. byte sig[ED25519_SIG_SIZE];
  4957. byte msg[512];
  4958. word32 x = 0;
  4959. #endif
  4960. const char**desc = bench_desc_words[lng_index];
  4961. wc_ed25519_init(&genKey);
  4962. ret = wc_ed25519_make_key(&gRng, ED25519_KEY_SIZE, &genKey);
  4963. if (ret != 0) {
  4964. printf("ed25519_make_key failed\n");
  4965. return;
  4966. }
  4967. #ifdef HAVE_ED25519_SIGN
  4968. /* make dummy msg */
  4969. for (i = 0; i < (int)sizeof(msg); i++)
  4970. msg[i] = (byte)i;
  4971. bench_stats_start(&count, &start);
  4972. do {
  4973. for (i = 0; i < agreeTimes; i++) {
  4974. x = sizeof(sig);
  4975. ret = wc_ed25519_sign_msg(msg, sizeof(msg), sig, &x, &genKey);
  4976. if (ret != 0) {
  4977. printf("ed25519_sign_msg failed\n");
  4978. goto exit_ed_sign;
  4979. }
  4980. }
  4981. count += i;
  4982. } while (bench_stats_sym_check(start));
  4983. exit_ed_sign:
  4984. bench_stats_asym_finish("ED", 25519, desc[4], 0, count, start, ret);
  4985. #ifdef HAVE_ED25519_VERIFY
  4986. bench_stats_start(&count, &start);
  4987. do {
  4988. for (i = 0; i < agreeTimes; i++) {
  4989. int verify = 0;
  4990. ret = wc_ed25519_verify_msg(sig, x, msg, sizeof(msg), &verify,
  4991. &genKey);
  4992. if (ret != 0 || verify != 1) {
  4993. printf("ed25519_verify_msg failed\n");
  4994. goto exit_ed_verify;
  4995. }
  4996. }
  4997. count += i;
  4998. } while (bench_stats_sym_check(start));
  4999. exit_ed_verify:
  5000. bench_stats_asym_finish("ED", 25519, desc[5], 0, count, start, ret);
  5001. #endif /* HAVE_ED25519_VERIFY */
  5002. #endif /* HAVE_ED25519_SIGN */
  5003. wc_ed25519_free(&genKey);
  5004. }
  5005. #endif /* HAVE_ED25519 */
  5006. #ifdef HAVE_CURVE448
  5007. void bench_curve448KeyGen(void)
  5008. {
  5009. curve448_key genKey;
  5010. double start;
  5011. int ret = 0, i, count;
  5012. const char**desc = bench_desc_words[lng_index];
  5013. /* Key Gen */
  5014. bench_stats_start(&count, &start);
  5015. do {
  5016. for (i = 0; i < genTimes; i++) {
  5017. ret = wc_curve448_make_key(&gRng, 56, &genKey);
  5018. wc_curve448_free(&genKey);
  5019. if (ret != 0) {
  5020. printf("wc_curve448_make_key failed: %d\n", ret);
  5021. break;
  5022. }
  5023. }
  5024. count += i;
  5025. } while (bench_stats_sym_check(start));
  5026. bench_stats_asym_finish("CURVE", 448, desc[2], 0, count, start, ret);
  5027. }
  5028. #ifdef HAVE_CURVE448_SHARED_SECRET
  5029. void bench_curve448KeyAgree(void)
  5030. {
  5031. curve448_key genKey, genKey2;
  5032. double start;
  5033. int ret, i, count;
  5034. byte shared[56];
  5035. const char**desc = bench_desc_words[lng_index];
  5036. word32 x = 0;
  5037. wc_curve448_init(&genKey);
  5038. wc_curve448_init(&genKey2);
  5039. ret = wc_curve448_make_key(&gRng, 56, &genKey);
  5040. if (ret != 0) {
  5041. printf("curve448_make_key failed\n");
  5042. return;
  5043. }
  5044. ret = wc_curve448_make_key(&gRng, 56, &genKey2);
  5045. if (ret != 0) {
  5046. printf("curve448_make_key failed: %d\n", ret);
  5047. wc_curve448_free(&genKey);
  5048. return;
  5049. }
  5050. /* Shared secret */
  5051. bench_stats_start(&count, &start);
  5052. do {
  5053. for (i = 0; i < agreeTimes; i++) {
  5054. x = sizeof(shared);
  5055. ret = wc_curve448_shared_secret(&genKey, &genKey2, shared, &x);
  5056. if (ret != 0) {
  5057. printf("curve448_shared_secret failed: %d\n", ret);
  5058. goto exit;
  5059. }
  5060. }
  5061. count += i;
  5062. } while (bench_stats_sym_check(start));
  5063. exit:
  5064. bench_stats_asym_finish("CURVE", 448, desc[3], 0, count, start, ret);
  5065. wc_curve448_free(&genKey2);
  5066. wc_curve448_free(&genKey);
  5067. }
  5068. #endif /* HAVE_CURVE448_SHARED_SECRET */
  5069. #endif /* HAVE_CURVE448 */
  5070. #ifdef HAVE_ED448
  5071. void bench_ed448KeyGen(void)
  5072. {
  5073. ed448_key genKey;
  5074. double start;
  5075. int i, count;
  5076. const char**desc = bench_desc_words[lng_index];
  5077. /* Key Gen */
  5078. bench_stats_start(&count, &start);
  5079. do {
  5080. for (i = 0; i < genTimes; i++) {
  5081. wc_ed448_init(&genKey);
  5082. (void)wc_ed448_make_key(&gRng, ED448_KEY_SIZE, &genKey);
  5083. wc_ed448_free(&genKey);
  5084. }
  5085. count += i;
  5086. } while (bench_stats_sym_check(start));
  5087. bench_stats_asym_finish("ED", 448, desc[2], 0, count, start, 0);
  5088. }
  5089. void bench_ed448KeySign(void)
  5090. {
  5091. int ret;
  5092. ed448_key genKey;
  5093. #ifdef HAVE_ED448_SIGN
  5094. double start;
  5095. int i, count;
  5096. byte sig[ED448_SIG_SIZE];
  5097. byte msg[512];
  5098. word32 x = 0;
  5099. #endif
  5100. const char**desc = bench_desc_words[lng_index];
  5101. wc_ed448_init(&genKey);
  5102. ret = wc_ed448_make_key(&gRng, ED448_KEY_SIZE, &genKey);
  5103. if (ret != 0) {
  5104. printf("ed448_make_key failed\n");
  5105. return;
  5106. }
  5107. #ifdef HAVE_ED448_SIGN
  5108. /* make dummy msg */
  5109. for (i = 0; i < (int)sizeof(msg); i++)
  5110. msg[i] = (byte)i;
  5111. bench_stats_start(&count, &start);
  5112. do {
  5113. for (i = 0; i < agreeTimes; i++) {
  5114. x = sizeof(sig);
  5115. ret = wc_ed448_sign_msg(msg, sizeof(msg), sig, &x, &genKey,
  5116. NULL, 0);
  5117. if (ret != 0) {
  5118. printf("ed448_sign_msg failed\n");
  5119. goto exit_ed_sign;
  5120. }
  5121. }
  5122. count += i;
  5123. } while (bench_stats_sym_check(start));
  5124. exit_ed_sign:
  5125. bench_stats_asym_finish("ED", 448, desc[4], 0, count, start, ret);
  5126. #ifdef HAVE_ED448_VERIFY
  5127. bench_stats_start(&count, &start);
  5128. do {
  5129. for (i = 0; i < agreeTimes; i++) {
  5130. int verify = 0;
  5131. ret = wc_ed448_verify_msg(sig, x, msg, sizeof(msg), &verify,
  5132. &genKey, NULL, 0);
  5133. if (ret != 0 || verify != 1) {
  5134. printf("ed448_verify_msg failed\n");
  5135. goto exit_ed_verify;
  5136. }
  5137. }
  5138. count += i;
  5139. } while (bench_stats_sym_check(start));
  5140. exit_ed_verify:
  5141. bench_stats_asym_finish("ED", 448, desc[5], 0, count, start, ret);
  5142. #endif /* HAVE_ED448_VERIFY */
  5143. #endif /* HAVE_ED448_SIGN */
  5144. wc_ed448_free(&genKey);
  5145. }
  5146. #endif /* HAVE_ED448 */
  5147. #ifndef HAVE_STACK_SIZE
  5148. #if defined(_WIN32) && !defined(INTIME_RTOS)
  5149. #define WIN32_LEAN_AND_MEAN
  5150. #include <windows.h>
  5151. double current_time(int reset)
  5152. {
  5153. static int init = 0;
  5154. static LARGE_INTEGER freq;
  5155. LARGE_INTEGER count;
  5156. (void)reset;
  5157. if (!init) {
  5158. QueryPerformanceFrequency(&freq);
  5159. init = 1;
  5160. }
  5161. QueryPerformanceCounter(&count);
  5162. return (double)count.QuadPart / freq.QuadPart;
  5163. }
  5164. #elif defined MICROCHIP_PIC32
  5165. #if defined(WOLFSSL_MICROCHIP_PIC32MZ)
  5166. #define CLOCK 80000000.0
  5167. #else
  5168. #define CLOCK 40000000.0
  5169. #endif
  5170. extern void WriteCoreTimer(word32 t);
  5171. extern word32 ReadCoreTimer(void);
  5172. double current_time(int reset)
  5173. {
  5174. unsigned int ns;
  5175. if (reset) {
  5176. WriteCoreTimer(0);
  5177. }
  5178. /* get timer in ns */
  5179. ns = ReadCoreTimer();
  5180. /* return seconds as a double */
  5181. return ( ns / CLOCK * 2.0);
  5182. }
  5183. #elif defined(WOLFSSL_IAR_ARM_TIME) || defined (WOLFSSL_MDK_ARM) || \
  5184. defined(WOLFSSL_USER_CURRTIME) || defined(WOLFSSL_CURRTIME_REMAP)
  5185. /* declared above at line 239 */
  5186. /* extern double current_time(int reset); */
  5187. #elif defined(FREERTOS)
  5188. #include "task.h"
  5189. #if defined(WOLFSSL_ESPIDF)
  5190. /* proto type definition */
  5191. int construct_argv();
  5192. extern char* __argv[22];
  5193. #endif
  5194. double current_time(int reset)
  5195. {
  5196. portTickType tickCount;
  5197. (void) reset;
  5198. /* tick count == ms, if configTICK_RATE_HZ is set to 1000 */
  5199. tickCount = xTaskGetTickCount();
  5200. return (double)tickCount / 1000;
  5201. }
  5202. #elif defined (WOLFSSL_TIRTOS)
  5203. extern double current_time(int reset);
  5204. #elif defined(FREESCALE_MQX)
  5205. double current_time(int reset)
  5206. {
  5207. TIME_STRUCT tv;
  5208. _time_get(&tv);
  5209. return (double)tv.SECONDS + (double)tv.MILLISECONDS / 1000;
  5210. }
  5211. #elif defined(FREESCALE_KSDK_BM)
  5212. double current_time(int reset)
  5213. {
  5214. return (double)OSA_TimeGetMsec() / 1000;
  5215. }
  5216. #elif defined(WOLFSSL_EMBOS)
  5217. #include "RTOS.h"
  5218. double current_time(int reset)
  5219. {
  5220. double time_now;
  5221. double current_s = OS_GetTime() / 1000.0;
  5222. double current_us = OS_GetTime_us() / 1000000.0;
  5223. time_now = (double)( current_s + current_us);
  5224. (void) reset;
  5225. return time_now;
  5226. }
  5227. #elif defined(WOLFSSL_SGX)
  5228. double current_time(int reset);
  5229. #elif defined(WOLFSSL_DEOS)
  5230. double current_time(int reset)
  5231. {
  5232. const uint32_t systemTickTimeInHz = 1000000 / systemTickInMicroseconds();
  5233. uint32_t *systemTickPtr = systemTickPointer();
  5234. (void)reset;
  5235. return (double) *systemTickPtr/systemTickTimeInHz;
  5236. }
  5237. #elif defined(MICRIUM)
  5238. double current_time(int reset)
  5239. {
  5240. CPU_ERR err;
  5241. (void)reset;
  5242. return (double) CPU_TS_Get32()/CPU_TS_TmrFreqGet(&err);
  5243. }
  5244. #elif defined(WOLFSSL_ZEPHYR)
  5245. #include <time.h>
  5246. double current_time(int reset)
  5247. {
  5248. (void)reset;
  5249. #if defined(CONFIG_ARCH_POSIX)
  5250. k_cpu_idle();
  5251. #endif
  5252. return (double)k_uptime_get() / 1000;
  5253. }
  5254. #elif defined(WOLFSSL_NETBURNER)
  5255. #include <predef.h>
  5256. #include <utils.h>
  5257. #include <constants.h>
  5258. double current_time(int reset)
  5259. {
  5260. DWORD ticks = TimeTick; /* ticks since system start */
  5261. (void)reset;
  5262. return (double) ticks/TICKS_PER_SECOND;
  5263. }
  5264. #elif defined(THREADX)
  5265. #include "tx_api.h"
  5266. double current_time(int reset)
  5267. {
  5268. (void)reset;
  5269. return (double) tx_time_get() / TX_TIMER_TICKS_PER_SECOND;
  5270. }
  5271. #elif defined(WOLFSSL_XILINX)
  5272. #ifndef XPAR_CPU_CORTEXA53_0_TIMESTAMP_CLK_FREQ
  5273. #define XPAR_CPU_CORTEXA53_0_TIMESTAMP_CLK_FREQ 50000000
  5274. #endif
  5275. #ifndef COUNTS_PER_SECOND
  5276. #define COUNTS_PER_SECOND XPAR_CPU_CORTEXA53_0_TIMESTAMP_CLK_FREQ
  5277. #endif
  5278. double current_time(int reset)
  5279. {
  5280. double timer;
  5281. uint64_t cntPct = 0;
  5282. asm volatile("mrs %0, CNTPCT_EL0" : "=r" (cntPct));
  5283. /* Convert to milliseconds */
  5284. timer = (double)(cntPct / (COUNTS_PER_SECOND / 1000));
  5285. /* Convert to seconds.millisecond */
  5286. timer /= 1000;
  5287. return timer;
  5288. }
  5289. #else
  5290. #include <sys/time.h>
  5291. double current_time(int reset)
  5292. {
  5293. struct timeval tv;
  5294. (void)reset;
  5295. gettimeofday(&tv, 0);
  5296. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  5297. }
  5298. #endif /* _WIN32 */
  5299. #endif /* !HAVE_STACK_SIZE */
  5300. #if defined(HAVE_GET_CYCLES)
  5301. static WC_INLINE word64 get_intel_cycles(void)
  5302. {
  5303. unsigned int lo_c, hi_c;
  5304. __asm__ __volatile__ (
  5305. "cpuid\n\t"
  5306. "rdtsc"
  5307. : "=a"(lo_c), "=d"(hi_c) /* out */
  5308. : "a"(0) /* in */
  5309. : "%ebx", "%ecx"); /* clobber */
  5310. return ((word64)lo_c) | (((word64)hi_c) << 32);
  5311. }
  5312. #endif /* HAVE_GET_CYCLES */
  5313. void benchmark_configure(int block_size)
  5314. {
  5315. /* must be greater than 0 */
  5316. if (block_size > 0) {
  5317. numBlocks = numBlocks * bench_size / block_size;
  5318. bench_size = (word32)block_size;
  5319. }
  5320. }
  5321. #ifndef NO_MAIN_DRIVER
  5322. #ifndef MAIN_NO_ARGS
  5323. #ifndef WOLFSSL_BENCHMARK_ALL
  5324. /* Display the algorithm string and keep to 80 characters per line.
  5325. *
  5326. * str Algorithm string to print.
  5327. * line Length of line used so far.
  5328. */
  5329. static void print_alg(const char* str, int* line)
  5330. {
  5331. int optLen;
  5332. optLen = (int)XSTRLEN(str) + 1;
  5333. if (optLen + *line > 80) {
  5334. printf("\n ");
  5335. *line = 13;
  5336. }
  5337. *line += optLen;
  5338. printf(" %s", str);
  5339. }
  5340. #endif
  5341. /* Display the usage options of the benchmark program. */
  5342. static void Usage(void)
  5343. {
  5344. #ifndef WOLFSSL_BENCHMARK_ALL
  5345. int i;
  5346. int line;
  5347. #endif
  5348. printf("benchmark\n");
  5349. printf("%s", bench_Usage_msg1[lng_index][0]); /* option -? */
  5350. printf("%s", bench_Usage_msg1[lng_index][1]); /* option -csv */
  5351. printf("%s", bench_Usage_msg1[lng_index][2]); /* option -base10 */
  5352. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  5353. printf("%s", bench_Usage_msg1[lng_index][3]); /* option -no_add */
  5354. #endif
  5355. printf("%s", bench_Usage_msg1[lng_index][4]); /* option -dgst_full */
  5356. #ifndef NO_RSA
  5357. printf("%s", bench_Usage_msg1[lng_index][5]); /* option -ras_sign */
  5358. #ifdef WOLFSSL_KEY_GEN
  5359. printf("%s", bench_Usage_msg1[lng_index][6]); /* option -rsa-sz */
  5360. #endif
  5361. #endif
  5362. #if !defined(NO_DH) && defined(HAVE_FFDHE_2048)
  5363. printf("%s", bench_Usage_msg1[lng_index][7]); /* option -ffdhe2048 */
  5364. #endif
  5365. #if !defined(NO_DH) && defined(HAVE_FFDHE_3072)
  5366. printf("%s", bench_Usage_msg1[lng_index][8]); /* option -ffdhe3072 */
  5367. #endif
  5368. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  5369. printf("%s", bench_Usage_msg1[lng_index][9]); /* option -p256 */
  5370. #endif
  5371. #if defined(HAVE_ECC) && defined(HAVE_ECC384)
  5372. printf("%s", bench_Usage_msg1[lng_index][10]); /* option -p384 */
  5373. #endif
  5374. #ifndef WOLFSSL_BENCHMARK_ALL
  5375. printf("%s", bench_Usage_msg1[lng_index][11]); /* option -<alg> */
  5376. printf(" ");
  5377. line = 13;
  5378. for (i=0; bench_cipher_opt[i].str != NULL; i++)
  5379. print_alg(bench_cipher_opt[i].str + 1, &line);
  5380. printf("\n ");
  5381. line = 13;
  5382. for (i=0; bench_digest_opt[i].str != NULL; i++)
  5383. print_alg(bench_digest_opt[i].str + 1, &line);
  5384. printf("\n ");
  5385. line = 13;
  5386. for (i=0; bench_mac_opt[i].str != NULL; i++)
  5387. print_alg(bench_mac_opt[i].str + 1, &line);
  5388. printf("\n ");
  5389. line = 13;
  5390. for (i=0; bench_asym_opt[i].str != NULL; i++)
  5391. print_alg(bench_asym_opt[i].str + 1, &line);
  5392. printf("\n ");
  5393. line = 13;
  5394. for (i=0; bench_other_opt[i].str != NULL; i++)
  5395. print_alg(bench_other_opt[i].str + 1, &line);
  5396. printf("\n");
  5397. #endif
  5398. printf("%s", bench_Usage_msg1[lng_index][12]); /* option -lng */
  5399. printf("%s", bench_Usage_msg1[lng_index][13]); /* option <num> */
  5400. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  5401. printf("%s", bench_Usage_msg1[lng_index][14]); /* option -threads <num> */
  5402. #endif
  5403. printf("%s", bench_Usage_msg1[lng_index][15]); /* option -print */
  5404. }
  5405. /* Match the command line argument with the string.
  5406. *
  5407. * arg Command line argument.
  5408. * str String to check for.
  5409. * return 1 if the command line argument matches the string, 0 otherwise.
  5410. */
  5411. static int string_matches(const char* arg, const char* str)
  5412. {
  5413. int len = (int)XSTRLEN(str) + 1;
  5414. return XSTRNCMP(arg, str, len) == 0;
  5415. }
  5416. #endif /* MAIN_NO_ARGS */
  5417. #ifdef WOLFSSL_ESPIDF
  5418. int wolf_benchmark_task( )
  5419. #elif defined(MAIN_NO_ARGS)
  5420. int main()
  5421. #else
  5422. int main(int argc, char** argv)
  5423. #endif
  5424. {
  5425. int ret = 0;
  5426. #ifndef MAIN_NO_ARGS
  5427. int optMatched;
  5428. #ifdef WOLFSSL_ESPIDF
  5429. int argc = construct_argv();
  5430. char** argv = (char**)__argv;
  5431. #endif
  5432. #ifndef WOLFSSL_BENCHMARK_ALL
  5433. int i;
  5434. #endif
  5435. #endif
  5436. benchmark_static_init();
  5437. #ifndef MAIN_NO_ARGS
  5438. while (argc > 1) {
  5439. if (string_matches(argv[1], "-?")) {
  5440. if(--argc>1){
  5441. lng_index = XATOI((++argv)[1]);
  5442. if(lng_index<0||lng_index>1) {
  5443. lng_index = 0;
  5444. }
  5445. }
  5446. Usage();
  5447. return 0;
  5448. }
  5449. else if (string_matches(argv[1], "-v")) {
  5450. printf("-----------------------------------------------------------"
  5451. "-------------------\n wolfSSL version %s\n-----------------"
  5452. "-----------------------------------------------------------"
  5453. "--\n", LIBWOLFSSL_VERSION_STRING);
  5454. return 0;
  5455. }
  5456. else if (string_matches(argv[1], "-lng")) {
  5457. argc--;
  5458. argv++;
  5459. if(argc>1) {
  5460. lng_index = XATOI(argv[1]);
  5461. if(lng_index<0||lng_index>1){
  5462. printf("invalid number(%d) is specified. [<num> :0-1]\n",lng_index);
  5463. lng_index = 0;
  5464. }
  5465. }
  5466. }
  5467. else if (string_matches(argv[1], "-base10"))
  5468. base2 = 0;
  5469. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  5470. else if (string_matches(argv[1], "-no_aad"))
  5471. aesAuthAddSz = 0;
  5472. #endif
  5473. else if (string_matches(argv[1], "-dgst_full"))
  5474. digest_stream = 0;
  5475. #ifndef NO_RSA
  5476. else if (string_matches(argv[1], "-rsa_sign"))
  5477. rsa_sign_verify = 1;
  5478. #endif
  5479. #if !defined(NO_DH) && defined(HAVE_FFDHE_2048)
  5480. else if (string_matches(argv[1], "-ffdhe2048"))
  5481. use_ffdhe = 2048;
  5482. #endif
  5483. #if !defined(NO_DH) && defined(HAVE_FFDHE_3072)
  5484. else if (string_matches(argv[1], "-ffdhe3072"))
  5485. use_ffdhe = 3072;
  5486. #endif
  5487. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  5488. else if (string_matches(argv[1], "-p256"))
  5489. bench_ecc_size = 32;
  5490. #endif
  5491. #if defined(HAVE_ECC) && defined(HAVE_ECC384)
  5492. else if (string_matches(argv[1], "-p384"))
  5493. bench_ecc_size = 48;
  5494. #endif
  5495. #ifdef BENCH_ASYM
  5496. else if (string_matches(argv[1], "-csv")) {
  5497. csv_format = 1;
  5498. csv_header_count = 1;
  5499. }
  5500. #endif
  5501. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  5502. else if (string_matches(argv[1], "-threads")) {
  5503. argc--;
  5504. argv++;
  5505. if (argc > 1) {
  5506. g_threadCount = XATOI(argv[1]);
  5507. if (g_threadCount < 1 || lng_index > 128){
  5508. printf("invalid number(%d) is specified. [<num> :1-128]\n",
  5509. g_threadCount);
  5510. g_threadCount = 0;
  5511. }
  5512. }
  5513. }
  5514. #endif
  5515. else if (string_matches(argv[1], "-print")) {
  5516. gPrintStats = 1;
  5517. }
  5518. else if (argv[1][0] == '-') {
  5519. optMatched = 0;
  5520. #ifndef WOLFSSL_BENCHMARK_ALL
  5521. /* Check known algorithm choosing command line options. */
  5522. /* Known cipher algorithms */
  5523. for (i=0; !optMatched && bench_cipher_opt[i].str != NULL; i++) {
  5524. if (string_matches(argv[1], bench_cipher_opt[i].str)) {
  5525. bench_cipher_algs |= bench_cipher_opt[i].val;
  5526. bench_all = 0;
  5527. optMatched = 1;
  5528. }
  5529. }
  5530. /* Known digest algorithms */
  5531. for (i=0; !optMatched && bench_digest_opt[i].str != NULL; i++) {
  5532. if (string_matches(argv[1], bench_digest_opt[i].str)) {
  5533. bench_digest_algs |= bench_digest_opt[i].val;
  5534. bench_all = 0;
  5535. optMatched = 1;
  5536. }
  5537. }
  5538. /* Known MAC algorithms */
  5539. for (i=0; !optMatched && bench_mac_opt[i].str != NULL; i++) {
  5540. if (string_matches(argv[1], bench_mac_opt[i].str)) {
  5541. bench_mac_algs |= bench_mac_opt[i].val;
  5542. bench_all = 0;
  5543. optMatched = 1;
  5544. }
  5545. }
  5546. /* Known asymmetric algorithms */
  5547. for (i=0; !optMatched && bench_asym_opt[i].str != NULL; i++) {
  5548. if (string_matches(argv[1], bench_asym_opt[i].str)) {
  5549. bench_asym_algs |= bench_asym_opt[i].val;
  5550. bench_all = 0;
  5551. optMatched = 1;
  5552. }
  5553. }
  5554. /* Other known cryptographic algorithms */
  5555. for (i=0; !optMatched && bench_other_opt[i].str != NULL; i++) {
  5556. if (string_matches(argv[1], bench_other_opt[i].str)) {
  5557. bench_other_algs |= bench_other_opt[i].val;
  5558. bench_all = 0;
  5559. optMatched = 1;
  5560. }
  5561. }
  5562. #endif
  5563. if (!optMatched) {
  5564. printf("Option not recognized: %s\n", argv[1]);
  5565. Usage();
  5566. return 1;
  5567. }
  5568. }
  5569. else {
  5570. /* parse for block size */
  5571. benchmark_configure(XATOI(argv[1]));
  5572. }
  5573. argc--;
  5574. argv++;
  5575. }
  5576. #endif /* MAIN_NO_ARGS */
  5577. #ifdef HAVE_STACK_SIZE
  5578. ret = StackSizeCheck(NULL, benchmark_test);
  5579. #else
  5580. ret = benchmark_test(NULL);
  5581. #endif
  5582. return ret;
  5583. }
  5584. #endif /* !NO_MAIN_DRIVER */
  5585. #else
  5586. #ifndef NO_MAIN_DRIVER
  5587. int main() { return 0; }
  5588. #endif
  5589. #endif /* !NO_CRYPT_BENCHMARK */