1
0

benchmark.c 280 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968
  1. /* benchmark.c
  2. *
  3. * Copyright (C) 2006-2023 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. /* Some common, optional user settings */
  26. /* these can also be set in wolfssl/options.h or user_settings.h */
  27. /* ------------------------------------------------------------- */
  28. /* make the binary always use CSV format: */
  29. /* #define WOLFSSL_BENCHMARK_FIXED_CSV */
  30. /* */
  31. /* choose to use the same units, regardless of scale. pick 1: */
  32. /* #define WOLFSSL_BENCHMARK_FIXED_UNITS_GB */
  33. /* #define WOLFSSL_BENCHMARK_FIXED_UNITS_MB */
  34. /* #define WOLFSSL_BENCHMARK_FIXED_UNITS_KB */
  35. /* #define WOLFSSL_BENCHMARK_FIXED_UNITS_B */
  36. /* */
  37. /* when the output should be in machine-parseable format: */
  38. /* #define GENERATE_MACHINE_PARSEABLE_REPORT */
  39. /* */
  40. /* define the max length for each string of metric reported */
  41. #define __BENCHMARK_MAXIMUM_LINE_LENGTH 150
  42. /* some internal helpers to get values of settings */
  43. /* this first one gets the text name of the #define parameter */
  44. #define __BENCHMARK_VALUE_TO_STRING(x) #x
  45. /* this next one gets the text value of the assigned value of #define param */
  46. #define __BENCHMARK_VALUE(x) __BENCHMARK_VALUE_TO_STRING(x)
  47. #define WOLFSSL_FIXED_UNITS_PER_SEC "MB/s" /* may be re-set by fixed units */
  48. #ifndef WOLFSSL_USER_SETTINGS
  49. #include <wolfssl/options.h>
  50. #endif
  51. #include <wolfssl/wolfcrypt/settings.h> /* also picks up user_settings.h */
  52. #include <wolfssl/wolfcrypt/types.h>
  53. #include <wolfssl/version.h>
  54. #include <wolfssl/wolfcrypt/wc_port.h>
  55. #include <wolfssl/wolfcrypt/ecc.h>
  56. #ifdef WOLFSSL_ESPIDF
  57. #include <xtensa/hal.h> /* reminder Espressif RISC-V not yet implemented */
  58. #include <esp_log.h>
  59. #endif
  60. #ifdef HAVE_PTHREAD
  61. #include <pthread.h>
  62. #endif
  63. #if defined(HAVE_PTHREAD) || \
  64. (!defined(NO_CRYPT_BENCHMARK) && !defined(NO_STDIO_FILESYSTEM) && \
  65. !defined(NO_ERROR_STRINGS) && !defined(NO_MAIN_DRIVER) && \
  66. !defined(BENCH_EMBEDDED))
  67. #include <errno.h>
  68. #ifndef WOLFSSL_ZEPHYR
  69. #include <unistd.h>
  70. #endif
  71. #endif
  72. #ifdef NO_STDIO_FILESYSTEM
  73. #define fflush(...) do {} while (0)
  74. #endif
  75. /* Macro to disable benchmark */
  76. #ifndef NO_CRYPT_BENCHMARK
  77. #include <wolfssl/wolfcrypt/mem_track.h>
  78. /* only for stack size check */
  79. #if defined(WOLFSSL_ASYNC_CRYPT)
  80. #ifndef WC_NO_ASYNC_THREADING
  81. #define WC_ENABLE_BENCH_THREADING
  82. #endif
  83. #endif
  84. #ifdef USE_FLAT_BENCHMARK_H
  85. #include "benchmark.h"
  86. #else
  87. #include "wolfcrypt/benchmark/benchmark.h"
  88. #endif
  89. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  90. static const char info_prefix[] = "###, ";
  91. static const char err_prefix[] = "!!!, ";
  92. #else
  93. static const char info_prefix[] = "";
  94. static const char err_prefix[] = "";
  95. #endif
  96. /* printf mappings */
  97. #ifdef FREESCALE_MQX
  98. #include <mqx.h>
  99. /* see wc_port.h for fio.h and nio.h includes */
  100. #elif defined(FREESCALE_KSDK_1_3)
  101. #include "fsl_debug_console.h"
  102. #include "fsl_os_abstraction.h"
  103. #undef printf
  104. #define printf PRINTF
  105. #elif defined(WOLFSSL_DEOS)
  106. #include <deos.h>
  107. #include <printx.h>
  108. #undef printf
  109. #define printf printx
  110. #elif defined(MICRIUM)
  111. #if (OS_VERSION < 50000)
  112. #include <bsp_ser.h>
  113. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  114. #undef printf
  115. #define printf BSP_Ser_Printf
  116. #endif
  117. #elif defined(WOLFSSL_ZEPHYR)
  118. #include <stdio.h>
  119. #define BENCH_EMBEDDED
  120. #define printf printfk
  121. static int printfk(const char *fmt, ...)
  122. {
  123. int ret;
  124. char line[__BENCHMARK_MAXIMUM_LINE_LENGTH];
  125. va_list ap;
  126. va_start(ap, fmt);
  127. ret = vsnprintf(line, sizeof(line), fmt, ap);
  128. line[sizeof(line)-1] = '\0';
  129. printk("%s", line);
  130. va_end(ap);
  131. return ret;
  132. }
  133. #elif defined(WOLFSSL_TELIT_M2MB)
  134. #include <stdarg.h>
  135. #include <stdio.h>
  136. #include <string.h>
  137. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  138. /* remap printf */
  139. #undef printf
  140. #define printf M2M_LOG_INFO
  141. /* OS requires occasional sleep() */
  142. #ifndef TEST_SLEEP_MS
  143. #define TEST_SLEEP_MS 50
  144. #endif
  145. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  146. /* don't use file system for these tests, since ./certs dir isn't loaded */
  147. #undef NO_FILESYSTEM
  148. #define NO_FILESYSTEM
  149. /* ANDROID_V454 (for android studio) displays information in a textview
  150. * and redirects printf to the textview output instead of using
  151. * __android_log_print() */
  152. #elif defined(ANDROID) && !defined(ANDROID_V454)
  153. #ifdef XMALLOC_USER
  154. #include <stdlib.h> /* we're using malloc / free direct here */
  155. #endif
  156. #ifndef STRING_USER
  157. #include <stdio.h>
  158. #endif
  159. #include <android/log.h>
  160. #define printf(...) \
  161. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  162. #define fprintf(fp, ...) \
  163. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  164. #else
  165. #if defined(XMALLOC_USER) || defined(FREESCALE_MQX)
  166. /* MQX classic needs for EXIT_FAILURE */
  167. #include <stdlib.h> /* we're using malloc / free direct here */
  168. #endif
  169. #ifndef STRING_USER
  170. #include <string.h>
  171. #include <stdio.h>
  172. #endif
  173. /* enable way for customer to override test/bench printf */
  174. #ifdef XPRINTF
  175. #undef printf
  176. #define printf XPRINTF
  177. #elif defined(NETOS)
  178. #undef printf
  179. #define printf dc_log_printf
  180. #endif
  181. #endif
  182. #include <wolfssl/wolfcrypt/memory.h>
  183. #include <wolfssl/wolfcrypt/random.h>
  184. #include <wolfssl/wolfcrypt/des3.h>
  185. #include <wolfssl/wolfcrypt/arc4.h>
  186. #include <wolfssl/wolfcrypt/chacha.h>
  187. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  188. #include <wolfssl/wolfcrypt/aes.h>
  189. #include <wolfssl/wolfcrypt/poly1305.h>
  190. #include <wolfssl/wolfcrypt/camellia.h>
  191. #include <wolfssl/wolfcrypt/md5.h>
  192. #include <wolfssl/wolfcrypt/sha.h>
  193. #include <wolfssl/wolfcrypt/sha256.h>
  194. #include <wolfssl/wolfcrypt/sha512.h>
  195. #include <wolfssl/wolfcrypt/sha3.h>
  196. #include <wolfssl/wolfcrypt/rsa.h>
  197. #include <wolfssl/wolfcrypt/asn.h>
  198. #include <wolfssl/wolfcrypt/ripemd.h>
  199. #include <wolfssl/wolfcrypt/cmac.h>
  200. #ifndef NO_HMAC
  201. #include <wolfssl/wolfcrypt/hmac.h>
  202. #endif
  203. #ifdef WOLFSSL_SIPHASH
  204. #include <wolfssl/wolfcrypt/siphash.h>
  205. #endif
  206. #ifndef NO_PWDBASED
  207. #include <wolfssl/wolfcrypt/pwdbased.h>
  208. #endif
  209. #ifdef HAVE_ECC
  210. #include <wolfssl/wolfcrypt/ecc.h>
  211. #endif
  212. #ifdef HAVE_CURVE25519
  213. #include <wolfssl/wolfcrypt/curve25519.h>
  214. #endif
  215. #ifdef HAVE_ED25519
  216. #include <wolfssl/wolfcrypt/ed25519.h>
  217. #endif
  218. #ifdef HAVE_CURVE448
  219. #include <wolfssl/wolfcrypt/curve448.h>
  220. #endif
  221. #ifdef HAVE_ED448
  222. #include <wolfssl/wolfcrypt/ed448.h>
  223. #endif
  224. #ifdef WOLFSSL_HAVE_KYBER
  225. #include <wolfssl/wolfcrypt/kyber.h>
  226. #ifdef WOLFSSL_WC_KYBER
  227. #include <wolfssl/wolfcrypt/wc_kyber.h>
  228. #endif
  229. #if defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  230. #include <wolfssl/wolfcrypt/ext_kyber.h>
  231. #endif
  232. #endif
  233. #ifdef WOLFCRYPT_HAVE_ECCSI
  234. #include <wolfssl/wolfcrypt/eccsi.h>
  235. #endif
  236. #ifdef WOLFCRYPT_HAVE_SAKKE
  237. #include <wolfssl/wolfcrypt/sakke.h>
  238. #endif
  239. #if defined(HAVE_PQC)
  240. #if defined(HAVE_FALCON)
  241. #include <wolfssl/wolfcrypt/falcon.h>
  242. #endif
  243. #if defined(HAVE_DILITHIUM)
  244. #include <wolfssl/wolfcrypt/dilithium.h>
  245. #endif
  246. #if defined(HAVE_SPHINCS)
  247. #include <wolfssl/wolfcrypt/sphincs.h>
  248. #endif
  249. #endif
  250. #include <wolfssl/wolfcrypt/dh.h>
  251. #include <wolfssl/wolfcrypt/random.h>
  252. #include <wolfssl/wolfcrypt/error-crypt.h>
  253. #include <wolfssl/wolfcrypt/types.h>
  254. #ifdef WOLF_CRYPTO_CB
  255. #include <wolfssl/wolfcrypt/cryptocb.h>
  256. #ifdef HAVE_INTEL_QA_SYNC
  257. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  258. #endif
  259. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  260. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  261. #endif
  262. #ifdef HAVE_RENESAS_SYNC
  263. #include <wolfssl/wolfcrypt/port/renesas/renesas_sync.h>
  264. #endif
  265. #endif
  266. #ifdef WOLFSSL_ASYNC_CRYPT
  267. #include <wolfssl/wolfcrypt/async.h>
  268. #endif
  269. #ifdef HAVE_FIPS
  270. #include <wolfssl/wolfcrypt/fips_test.h>
  271. static void myFipsCb(int ok, int err, const char* hash)
  272. {
  273. printf("%sin my Fips callback, ok = %d, err = %d\n",
  274. ok ? info_prefix : err_prefix, ok, err);
  275. printf("%smessage = %s\n", ok ? info_prefix : err_prefix,
  276. wc_GetErrorString(err));
  277. printf("%shash = %s\n", ok ? info_prefix : err_prefix, hash);
  278. if (err == IN_CORE_FIPS_E) {
  279. printf("%sIn core integrity hash check failure, copy above hash\n",
  280. err_prefix);
  281. printf("%sinto verifyCore[] in fips_test.c and rebuild\n",
  282. err_prefix);
  283. }
  284. }
  285. #endif
  286. #ifdef WOLFSSL_STATIC_MEMORY
  287. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  288. #else
  289. #define HEAP_HINT NULL
  290. #endif /* WOLFSSL_STATIC_MEMORY */
  291. #ifndef EXIT_FAILURE
  292. #define EXIT_FAILURE 1
  293. #endif
  294. #undef LIBCALL_CHECK_RET
  295. #if defined(NO_STDIO_FILESYSTEM) || defined(NO_ERROR_STRINGS) || \
  296. defined(NO_MAIN_DRIVER) || defined(BENCH_EMBEDDED)
  297. #define LIBCALL_CHECK_RET(...) __VA_ARGS__
  298. #else
  299. #define LIBCALL_CHECK_RET(...) do { \
  300. int _libcall_ret = (__VA_ARGS__); \
  301. if (_libcall_ret < 0) { \
  302. printf("%s%s L%d error %d for \"%s\"\n", \
  303. err_prefix, __FILE__, __LINE__, \
  304. errno, #__VA_ARGS__); \
  305. fflush(stdout); \
  306. _exit(1); \
  307. } \
  308. } while(0)
  309. #endif
  310. #undef PTHREAD_CHECK_RET
  311. #define PTHREAD_CHECK_RET(...) do { \
  312. int _pthread_ret = (__VA_ARGS__); \
  313. if (_pthread_ret != 0) { \
  314. errno = _pthread_ret; \
  315. printf("%s%s L%d error %d for \"%s\"\n", \
  316. err_prefix, __FILE__, __LINE__, \
  317. _pthread_ret, #__VA_ARGS__); \
  318. fflush(stdout); \
  319. _exit(1); \
  320. } \
  321. } while(0)
  322. /* optional macro to add sleep between tests */
  323. #ifndef TEST_SLEEP
  324. /* stub the sleep macro */
  325. #define TEST_SLEEP()
  326. #endif
  327. #define TEST_STRING "Everyone gets Friday off."
  328. #define TEST_STRING_SZ 25
  329. /* Bit values for each algorithm that is able to be benchmarked.
  330. * Common grouping of algorithms also.
  331. * Each algorithm has a unique value for its type e.g. cipher.
  332. */
  333. /* Cipher algorithms. */
  334. #define BENCH_AES_CBC 0x00000001
  335. #define BENCH_AES_GCM 0x00000002
  336. #define BENCH_AES_ECB 0x00000004
  337. #define BENCH_AES_XTS 0x00000008
  338. #define BENCH_AES_CTR 0x00000010
  339. #define BENCH_AES_CCM 0x00000020
  340. #define BENCH_CAMELLIA 0x00000100
  341. #define BENCH_ARC4 0x00000200
  342. #define BENCH_CHACHA20 0x00001000
  343. #define BENCH_CHACHA20_POLY1305 0x00002000
  344. #define BENCH_DES 0x00004000
  345. #define BENCH_AES_CFB 0x00010000
  346. #define BENCH_AES_OFB 0x00020000
  347. #define BENCH_AES_SIV 0x00040000
  348. /* Digest algorithms. */
  349. #define BENCH_MD5 0x00000001
  350. #define BENCH_POLY1305 0x00000002
  351. #define BENCH_SHA 0x00000004
  352. #define BENCH_SHA224 0x00000010
  353. #define BENCH_SHA256 0x00000020
  354. #define BENCH_SHA384 0x00000040
  355. #define BENCH_SHA512 0x00000080
  356. #define BENCH_SHA2 (BENCH_SHA224 | BENCH_SHA256 | \
  357. BENCH_SHA384 | BENCH_SHA512)
  358. #define BENCH_SHA3_224 0x00000100
  359. #define BENCH_SHA3_256 0x00000200
  360. #define BENCH_SHA3_384 0x00000400
  361. #define BENCH_SHA3_512 0x00000800
  362. #define BENCH_SHA3 (BENCH_SHA3_224 | BENCH_SHA3_256 | \
  363. BENCH_SHA3_384 | BENCH_SHA3_512)
  364. #define BENCH_SHAKE128 0x00001000
  365. #define BENCH_SHAKE256 0x00002000
  366. #define BENCH_SHAKE (BENCH_SHAKE128 | BENCH_SHAKE256)
  367. #define BENCH_RIPEMD 0x00004000
  368. #define BENCH_BLAKE2B 0x00008000
  369. #define BENCH_BLAKE2S 0x00010000
  370. /* MAC algorithms. */
  371. #define BENCH_CMAC 0x00000001
  372. #define BENCH_HMAC_MD5 0x00000002
  373. #define BENCH_HMAC_SHA 0x00000004
  374. #define BENCH_HMAC_SHA224 0x00000010
  375. #define BENCH_HMAC_SHA256 0x00000020
  376. #define BENCH_HMAC_SHA384 0x00000040
  377. #define BENCH_HMAC_SHA512 0x00000080
  378. #define BENCH_HMAC (BENCH_HMAC_MD5 | BENCH_HMAC_SHA | \
  379. BENCH_HMAC_SHA224 | BENCH_HMAC_SHA256 | \
  380. BENCH_HMAC_SHA384 | BENCH_HMAC_SHA512)
  381. #define BENCH_PBKDF2 0x00000100
  382. #define BENCH_SIPHASH 0x00000200
  383. /* Asymmetric algorithms. */
  384. #define BENCH_RSA_KEYGEN 0x00000001
  385. #define BENCH_RSA 0x00000002
  386. #define BENCH_RSA_SZ 0x00000004
  387. #define BENCH_DH 0x00000010
  388. #define BENCH_KYBER 0x00000020
  389. #define BENCH_ECC_MAKEKEY 0x00001000
  390. #define BENCH_ECC 0x00002000
  391. #define BENCH_ECC_ENCRYPT 0x00004000
  392. #define BENCH_ECC_ALL 0x00008000
  393. #define BENCH_CURVE25519_KEYGEN 0x00010000
  394. #define BENCH_CURVE25519_KA 0x00020000
  395. #define BENCH_ED25519_KEYGEN 0x00040000
  396. #define BENCH_ED25519_SIGN 0x00080000
  397. #define BENCH_CURVE448_KEYGEN 0x00100000
  398. #define BENCH_CURVE448_KA 0x00200000
  399. #define BENCH_ED448_KEYGEN 0x00400000
  400. #define BENCH_ED448_SIGN 0x00800000
  401. #define BENCH_ECC_P256 0x01000000
  402. #define BENCH_ECC_P384 0x02000000
  403. #define BENCH_ECC_P521 0x04000000
  404. #define BENCH_ECCSI_KEYGEN 0x00000020
  405. #define BENCH_ECCSI_PAIRGEN 0x00000040
  406. #define BENCH_ECCSI_VALIDATE 0x00000080
  407. #define BENCH_ECCSI 0x00000400
  408. #define BENCH_SAKKE_KEYGEN 0x10000000
  409. #define BENCH_SAKKE_RSKGEN 0x20000000
  410. #define BENCH_SAKKE_VALIDATE 0x40000000
  411. #define BENCH_SAKKE 0x80000000
  412. /* Post-Quantum Asymmetric algorithms. */
  413. #define BENCH_FALCON_LEVEL1_SIGN 0x00000001
  414. #define BENCH_FALCON_LEVEL5_SIGN 0x00000002
  415. #define BENCH_DILITHIUM_LEVEL2_SIGN 0x04000000
  416. #define BENCH_DILITHIUM_LEVEL3_SIGN 0x08000000
  417. #define BENCH_DILITHIUM_LEVEL5_SIGN 0x10000000
  418. /* Post-Quantum Asymmetric algorithms. (Part 2) */
  419. #define BENCH_SPHINCS_FAST_LEVEL1_SIGN 0x00000001
  420. #define BENCH_SPHINCS_FAST_LEVEL3_SIGN 0x00000002
  421. #define BENCH_SPHINCS_FAST_LEVEL5_SIGN 0x00000004
  422. #define BENCH_SPHINCS_SMALL_LEVEL1_SIGN 0x00000008
  423. #define BENCH_SPHINCS_SMALL_LEVEL3_SIGN 0x00000010
  424. #define BENCH_SPHINCS_SMALL_LEVEL5_SIGN 0x00000020
  425. /* Other */
  426. #define BENCH_RNG 0x00000001
  427. #define BENCH_SCRYPT 0x00000002
  428. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  429. /* Define AES_AUTH_ADD_SZ already here, since it's used in the
  430. * static declaration of `bench_Usage_msg1`. */
  431. #if !defined(AES_AUTH_ADD_SZ) && \
  432. defined(STM32_CRYPTO) && !defined(STM32_AESGCM_PARTIAL) || \
  433. defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  434. /* For STM32 use multiple of 4 to leverage crypto hardware
  435. * Xilinx Versal requires to use multiples of 16 bytes */
  436. #define AES_AUTH_ADD_SZ 16
  437. #endif
  438. #ifndef AES_AUTH_ADD_SZ
  439. #define AES_AUTH_ADD_SZ 13
  440. #endif
  441. #endif
  442. /* Benchmark all compiled in algorithms.
  443. * When 1, ignore other benchmark algorithm values.
  444. * 0, only benchmark algorithm values set.
  445. */
  446. static int bench_all = 1;
  447. /* Cipher algorithms to benchmark. */
  448. static int bench_cipher_algs = 0;
  449. /* Digest algorithms to benchmark. */
  450. static int bench_digest_algs = 0;
  451. /* MAC algorithms to benchmark. */
  452. static int bench_mac_algs = 0;
  453. /* Asymmetric algorithms to benchmark. */
  454. static int bench_asym_algs = 0;
  455. /* Post-Quantum Asymmetric algorithms to benchmark. */
  456. static int bench_pq_asym_algs = 0;
  457. /* Post-Quantum Asymmetric algorithms to benchmark. (Part 2)*/
  458. static int bench_pq_asym_algs2 = 0;
  459. /* Other cryptographic algorithms to benchmark. */
  460. static int bench_other_algs = 0;
  461. #if !defined(WOLFSSL_BENCHMARK_ALL) && !defined(NO_MAIN_DRIVER)
  462. /* The mapping of command line option to bit values. */
  463. typedef struct bench_alg {
  464. /* Command line option string. */
  465. const char* str;
  466. /* Bit values to set. */
  467. word32 val;
  468. } bench_alg;
  469. #ifndef MAIN_NO_ARGS
  470. /* All recognized cipher algorithm choosing command line options. */
  471. static const bench_alg bench_cipher_opt[] = {
  472. { "-cipher", 0xffffffff },
  473. #ifdef HAVE_AES_CBC
  474. { "-aes-cbc", BENCH_AES_CBC },
  475. #endif
  476. #ifdef HAVE_AESGCM
  477. { "-aes-gcm", BENCH_AES_GCM },
  478. #endif
  479. #ifdef WOLFSSL_AES_DIRECT
  480. { "-aes-ecb", BENCH_AES_ECB },
  481. #endif
  482. #ifdef WOLFSSL_AES_XTS
  483. { "-aes-xts", BENCH_AES_XTS },
  484. #endif
  485. #ifdef WOLFSSL_AES_CFB
  486. { "-aes-cfb", BENCH_AES_CFB },
  487. #endif
  488. #ifdef WOLFSSL_AES_OFB
  489. { "-aes-ofb", BENCH_AES_OFB },
  490. #endif
  491. #ifdef WOLFSSL_AES_COUNTER
  492. { "-aes-ctr", BENCH_AES_CTR },
  493. #endif
  494. #ifdef HAVE_AESCCM
  495. { "-aes-ccm", BENCH_AES_CCM },
  496. #endif
  497. #ifdef WOLFSSL_AES_SIV
  498. { "-aes-siv", BENCH_AES_SIV },
  499. #endif
  500. #ifdef HAVE_CAMELLIA
  501. { "-camellia", BENCH_CAMELLIA },
  502. #endif
  503. #ifndef NO_RC4
  504. { "-arc4", BENCH_ARC4 },
  505. #endif
  506. #ifdef HAVE_CHACHA
  507. { "-chacha20", BENCH_CHACHA20 },
  508. #endif
  509. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  510. { "-chacha20-poly1305", BENCH_CHACHA20_POLY1305 },
  511. #endif
  512. #ifndef NO_DES3
  513. { "-des", BENCH_DES },
  514. #endif
  515. { NULL, 0 }
  516. };
  517. /* All recognized digest algorithm choosing command line options. */
  518. static const bench_alg bench_digest_opt[] = {
  519. { "-digest", 0xffffffff },
  520. #ifndef NO_MD5
  521. { "-md5", BENCH_MD5 },
  522. #endif
  523. #ifdef HAVE_POLY1305
  524. { "-poly1305", BENCH_POLY1305 },
  525. #endif
  526. #ifndef NO_SHA
  527. { "-sha", BENCH_SHA },
  528. #endif
  529. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256) || defined(WOLFSSL_SHA384) \
  530. || defined(WOLFSSL_SHA512)
  531. { "-sha2", BENCH_SHA2 },
  532. #endif
  533. #ifdef WOLFSSL_SHA224
  534. { "-sha224", BENCH_SHA224 },
  535. #endif
  536. #ifndef NO_SHA256
  537. { "-sha256", BENCH_SHA256 },
  538. #endif
  539. #ifdef WOLFSSL_SHA384
  540. { "-sha384", BENCH_SHA384 },
  541. #endif
  542. #ifdef WOLFSSL_SHA512
  543. { "-sha512", BENCH_SHA512 },
  544. #endif
  545. #ifdef WOLFSSL_SHA3
  546. { "-sha3", BENCH_SHA3 },
  547. #ifndef WOLFSSL_NOSHA3_224
  548. { "-sha3-224", BENCH_SHA3_224 },
  549. #endif
  550. #ifndef WOLFSSL_NOSHA3_256
  551. { "-sha3-256", BENCH_SHA3_256 },
  552. #endif
  553. #ifndef WOLFSSL_NOSHA3_384
  554. { "-sha3-384", BENCH_SHA3_384 },
  555. #endif
  556. #ifndef WOLFSSL_NOSHA3_512
  557. { "-sha3-512", BENCH_SHA3_512 },
  558. #endif
  559. #if defined(WOLFSSL_SHAKE128) || defined(WOLFSSL_SHAKE256)
  560. { "-shake", BENCH_SHAKE },
  561. #endif
  562. #ifdef WOLFSSL_SHAKE128
  563. { "-shake128", BENCH_SHAKE128 },
  564. #endif
  565. #ifdef WOLFSSL_SHAKE256
  566. { "-shake256", BENCH_SHAKE256 },
  567. #endif
  568. #endif
  569. #ifdef WOLFSSL_RIPEMD
  570. { "-ripemd", BENCH_RIPEMD },
  571. #endif
  572. #ifdef HAVE_BLAKE2
  573. { "-blake2b", BENCH_BLAKE2B },
  574. #endif
  575. #ifdef HAVE_BLAKE2S
  576. { "-blake2s", BENCH_BLAKE2S },
  577. #endif
  578. { NULL, 0 }
  579. };
  580. /* All recognized MAC algorithm choosing command line options. */
  581. static const bench_alg bench_mac_opt[] = {
  582. { "-mac", 0xffffffff },
  583. #ifdef WOLFSSL_CMAC
  584. { "-cmac", BENCH_CMAC },
  585. #endif
  586. #ifndef NO_HMAC
  587. { "-hmac", BENCH_HMAC },
  588. #ifndef NO_MD5
  589. { "-hmac-md5", BENCH_HMAC_MD5 },
  590. #endif
  591. #ifndef NO_SHA
  592. { "-hmac-sha", BENCH_HMAC_SHA },
  593. #endif
  594. #ifdef WOLFSSL_SHA224
  595. { "-hmac-sha224", BENCH_HMAC_SHA224 },
  596. #endif
  597. #ifndef NO_SHA256
  598. { "-hmac-sha256", BENCH_HMAC_SHA256 },
  599. #endif
  600. #ifdef WOLFSSL_SHA384
  601. { "-hmac-sha384", BENCH_HMAC_SHA384 },
  602. #endif
  603. #ifdef WOLFSSL_SHA512
  604. { "-hmac-sha512", BENCH_HMAC_SHA512 },
  605. #endif
  606. #ifndef NO_PWDBASED
  607. { "-pbkdf2", BENCH_PBKDF2 },
  608. #endif
  609. #ifdef WOLFSSL_SIPHASH
  610. { "-siphash", BENCH_SIPHASH },
  611. #endif
  612. #endif
  613. { NULL, 0 }
  614. };
  615. /* All recognized asymmetric algorithm choosing command line options. */
  616. static const bench_alg bench_asym_opt[] = {
  617. { "-asym", 0xffffffff },
  618. #ifndef NO_RSA
  619. #ifdef WOLFSSL_KEY_GEN
  620. { "-rsa-kg", BENCH_RSA_KEYGEN },
  621. #endif
  622. { "-rsa", BENCH_RSA },
  623. { "-rsa-sz", BENCH_RSA_SZ },
  624. #endif
  625. #ifndef NO_DH
  626. { "-dh", BENCH_DH },
  627. #endif
  628. #ifdef WOLFSSL_HAVE_KYBER
  629. { "-kyber", BENCH_KYBER },
  630. #endif
  631. #ifdef HAVE_ECC
  632. { "-ecc-kg", BENCH_ECC_MAKEKEY },
  633. { "-ecc", BENCH_ECC },
  634. #ifdef HAVE_ECC_ENCRYPT
  635. { "-ecc-enc", BENCH_ECC_ENCRYPT },
  636. #endif
  637. { "-ecc-all", BENCH_ECC_ALL },
  638. #endif
  639. #ifdef HAVE_CURVE25519
  640. { "-curve25519-kg", BENCH_CURVE25519_KEYGEN },
  641. #ifdef HAVE_CURVE25519_SHARED_SECRET
  642. { "-x25519", BENCH_CURVE25519_KA },
  643. #endif
  644. #endif
  645. #ifdef HAVE_ED25519
  646. { "-ed25519-kg", BENCH_ED25519_KEYGEN },
  647. { "-ed25519", BENCH_ED25519_SIGN },
  648. #endif
  649. #ifdef HAVE_CURVE448
  650. { "-curve448-kg", BENCH_CURVE448_KEYGEN },
  651. #ifdef HAVE_CURVE448_SHARED_SECRET
  652. { "-x448", BENCH_CURVE448_KA },
  653. #endif
  654. #endif
  655. #ifdef HAVE_ED448
  656. { "-ed448-kg", BENCH_ED448_KEYGEN },
  657. { "-ed448", BENCH_ED448_SIGN },
  658. #endif
  659. #ifdef WOLFCRYPT_HAVE_ECCSI
  660. { "-eccsi-kg", BENCH_ECCSI_KEYGEN },
  661. { "-eccsi-pair", BENCH_ECCSI_PAIRGEN },
  662. { "-eccsi-val", BENCH_ECCSI_VALIDATE },
  663. { "-eccsi", BENCH_ECCSI },
  664. #endif
  665. #ifdef WOLFCRYPT_HAVE_SAKKE
  666. { "-sakke-kg", BENCH_SAKKE_KEYGEN },
  667. { "-sakke-rsk", BENCH_SAKKE_RSKGEN },
  668. { "-sakke-val", BENCH_SAKKE_VALIDATE },
  669. { "-sakke", BENCH_SAKKE },
  670. #endif
  671. { NULL, 0 }
  672. };
  673. /* All recognized other cryptographic algorithm choosing command line options.
  674. */
  675. static const bench_alg bench_other_opt[] = {
  676. { "-other", 0xffffffff },
  677. #ifndef WC_NO_RNG
  678. { "-rng", BENCH_RNG },
  679. #endif
  680. #ifdef HAVE_SCRYPT
  681. { "-scrypt", BENCH_SCRYPT },
  682. #endif
  683. { NULL, 0}
  684. };
  685. #endif /* MAIN_NO_ARGS */
  686. #endif /* !WOLFSSL_BENCHMARK_ALL && !NO_MAIN_DRIVER */
  687. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  688. /* The post-quantum-specific mapping of command line option to bit values and
  689. * OQS name. */
  690. typedef struct bench_pq_alg {
  691. /* Command line option string. */
  692. const char* str;
  693. /* Bit values to set. */
  694. word32 val;
  695. const char* pqc_name;
  696. } bench_pq_alg;
  697. /* All recognized post-quantum asymmetric algorithm choosing command line
  698. * options. */
  699. static const bench_pq_alg bench_pq_asym_opt[] = {
  700. { "-pq", 0xffffffff, NULL},
  701. #ifdef HAVE_LIBOQS
  702. { "-falcon_level1", BENCH_FALCON_LEVEL1_SIGN,
  703. OQS_SIG_alg_falcon_512 },
  704. { "-falcon_level5", BENCH_FALCON_LEVEL5_SIGN,
  705. OQS_SIG_alg_falcon_1024 },
  706. { "-dilithium_level2", BENCH_DILITHIUM_LEVEL2_SIGN,
  707. OQS_SIG_alg_dilithium_2 },
  708. { "-dilithium_level3", BENCH_DILITHIUM_LEVEL3_SIGN,
  709. OQS_SIG_alg_dilithium_3 },
  710. { "-dilithium_level5", BENCH_DILITHIUM_LEVEL5_SIGN,
  711. OQS_SIG_alg_dilithium_5 },
  712. #endif /* HAVE_LIBOQS */
  713. { NULL, 0, NULL }
  714. };
  715. #ifdef HAVE_LIBOQS
  716. /* All recognized post-quantum asymmetric algorithm choosing command line
  717. * options. (Part 2) */
  718. static const bench_pq_alg bench_pq_asym_opt2[] = {
  719. { "-pq", 0xffffffff, NULL},
  720. { "-sphincs_fast_level1", BENCH_SPHINCS_FAST_LEVEL1_SIGN,
  721. OQS_SIG_alg_sphincs_shake256_128f_simple },
  722. { "-sphincs_fast_level3", BENCH_SPHINCS_FAST_LEVEL3_SIGN,
  723. OQS_SIG_alg_sphincs_shake256_192f_simple },
  724. { "-sphincs_fast_level5", BENCH_SPHINCS_FAST_LEVEL5_SIGN,
  725. OQS_SIG_alg_sphincs_shake256_256f_simple },
  726. { "-sphincs_small_level1", BENCH_SPHINCS_SMALL_LEVEL1_SIGN,
  727. OQS_SIG_alg_sphincs_shake256_128s_simple },
  728. { "-sphincs_small_level3", BENCH_SPHINCS_SMALL_LEVEL3_SIGN,
  729. OQS_SIG_alg_sphincs_shake256_192s_simple },
  730. { "-sphincs_small_level5", BENCH_SPHINCS_SMALL_LEVEL5_SIGN,
  731. OQS_SIG_alg_sphincs_shake256_256s_simple },
  732. { NULL, 0, NULL }
  733. };
  734. #endif /* HAVE_LIBOQS */
  735. #endif /* HAVE_PQC */
  736. #ifdef HAVE_WNR
  737. const char* wnrConfigFile = "wnr-example.conf";
  738. #endif
  739. #if defined(WOLFSSL_MDK_ARM)
  740. extern XFILE wolfSSL_fopen(const char *fname, const char *mode);
  741. #define fopen wolfSSL_fopen
  742. #endif
  743. static int lng_index = 0;
  744. #ifndef NO_MAIN_DRIVER
  745. #ifndef MAIN_NO_ARGS
  746. static const char* bench_Usage_msg1[][21] = {
  747. /* 0 English */
  748. { "-? <num> Help, print this usage\n 0: English, 1: Japanese\n",
  749. "-csv Print terminal output in csv format\n",
  750. "-base10 Display bytes as power of 10 (eg 1 kB = 1000 Bytes)\n",
  751. "-no_aad No additional authentication data passed.\n",
  752. "-aad_size <num> With <num> bytes of AAD.\n",
  753. ("-all_aad With AAD length of 0, "
  754. WC_STRINGIFY(AES_AUTH_ADD_SZ)
  755. " and\n"
  756. " (if set via -aad_size) <aad_size> bytes.\n"
  757. ),
  758. "-dgst_full Full digest operation performed.\n",
  759. "-rsa_sign Measure RSA sign/verify instead of encrypt/decrypt.\n",
  760. "<keySz> -rsa-sz\n Measure RSA <key size> performance.\n",
  761. "-ffhdhe2048 Measure DH using FFDHE 2048-bit parameters.\n",
  762. "-ffhdhe3072 Measure DH using FFDHE 3072-bit parameters.\n",
  763. "-p256 Measure ECC using P-256 curve.\n",
  764. "-p384 Measure ECC using P-384 curve.\n",
  765. "-p521 Measure ECC using P-521 curve.\n",
  766. "-ecc-all Bench all enabled ECC curves.\n",
  767. "-<alg> Algorithm to benchmark. Available algorithms include:\n",
  768. "-lng <num> Display benchmark result by specified language.\n 0: English, 1: Japanese\n",
  769. "<num> Size of block in bytes\n",
  770. ("-blocks <num> Number of blocks. Can be used together with the 'Size of block'\n"
  771. " option, but must be used after that one.\n"
  772. ),
  773. "-threads <num> Number of threads to run\n",
  774. "-print Show benchmark stats summary\n"
  775. },
  776. #ifndef NO_MULTIBYTE_PRINT
  777. /* 1 Japanese */
  778. { "-? <num> ヘルプ, 使い方を表示します。\n 0: 英語、 1: 日本語\n",
  779. "-csv csv 形式で端末に出力します。\n",
  780. "-base10 バイトを10のべき乗で表示します。(例 1 kB = 1000 Bytes)\n",
  781. "-no_aad 追加の認証データを使用しません.\n",
  782. "-aad_size <num> TBD.\n",
  783. "-all_aad TBD.\n",
  784. "-dgst_full フルの digest 暗号操作を実施します。\n",
  785. "-rsa_sign 暗号/復号化の代わりに RSA の署名/検証を測定します。\n",
  786. "<keySz> -rsa-sz\n RSA <key size> の性能を測定します。\n",
  787. "-ffhdhe2048 Measure DH using FFDHE 2048-bit parameters.\n",
  788. "-ffhdhe3072 Measure DH using FFDHE 3072-bit parameters.\n",
  789. "-p256 Measure ECC using P-256 curve.\n",
  790. "-p384 Measure ECC using P-384 curve.\n",
  791. "-p521 Measure ECC using P-521 curve.\n",
  792. "-ecc-all Bench all enabled ECC curves.\n",
  793. "-<alg> アルゴリズムのベンチマークを実施します。\n 利用可能なアルゴリズムは下記を含みます:\n",
  794. "-lng <num> 指定された言語でベンチマーク結果を表示します。\n 0: 英語、 1: 日本語\n",
  795. "<num> ブロックサイズをバイト単位で指定します。\n",
  796. "-blocks <num> TBD.\n",
  797. "-threads <num> 実行するスレッド数\n",
  798. "-print ベンチマーク統計の要約を表示する\n"
  799. },
  800. #endif
  801. };
  802. #endif /* MAIN_NO_ARGS */
  803. #endif
  804. static const char* bench_result_words1[][4] = {
  805. { "took", "seconds" , "Cycles per byte", NULL }, /* 0 English */
  806. #ifndef NO_MULTIBYTE_PRINT
  807. { "を" , "秒で処理", "1バイトあたりのサイクル数", NULL }, /* 1 Japanese */
  808. #endif
  809. };
  810. #if !defined(NO_RSA) || \
  811. defined(HAVE_ECC) || !defined(NO_DH) || defined(HAVE_ECC_ENCRYPT) || \
  812. defined(HAVE_CURVE25519) || defined(HAVE_CURVE25519_SHARED_SECRET) || \
  813. defined(HAVE_ED25519) || defined(HAVE_CURVE448) || \
  814. defined(HAVE_CURVE448_SHARED_SECRET) || defined(HAVE_ED448) || \
  815. defined(WOLFSSL_HAVE_KYBER)
  816. static const char* bench_desc_words[][15] = {
  817. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 */
  818. {"public", "private", "key gen", "agree" , "sign", "verify", "encrypt", "decrypt", "rsk gen", "encap", "derive", "valid", "pair gen", "decap", NULL}, /* 0 English */
  819. #ifndef NO_MULTIBYTE_PRINT
  820. {"公開鍵", "秘密鍵" ,"鍵生成" , "鍵共有" , "署名", "検証" , "暗号化" , "復号化" , "rsk gen", "encap", "derive", "valid", "pair gen", "decap", NULL}, /* 1 Japanese */
  821. #endif
  822. };
  823. #endif
  824. #if defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM) && !defined(WOLFSSL_SGX)
  825. #define HAVE_GET_CYCLES
  826. static WC_INLINE word64 get_intel_cycles(void);
  827. static THREAD_LS_T word64 total_cycles;
  828. #define INIT_CYCLE_COUNTER
  829. #define BEGIN_INTEL_CYCLES total_cycles = get_intel_cycles();
  830. #define END_INTEL_CYCLES total_cycles = get_intel_cycles() - total_cycles;
  831. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  832. #define SHOW_INTEL_CYCLES(b, n, s) \
  833. (void)XSNPRINTF((b) + XSTRLEN(b), (n) - XSTRLEN(b), " %s = %6.2f\n", \
  834. bench_result_words1[lng_index][2], \
  835. count == 0 ? 0 : (float)total_cycles / ((word64)count*(s)))
  836. #define SHOW_INTEL_CYCLES_CSV(b, n, s) \
  837. (void)XSNPRINTF((b) + XSTRLEN(b), (n) - XSTRLEN(b), "%.6f,\n", \
  838. count == 0 ? 0 : (float)total_cycles / ((word64)count*(s)))
  839. #elif defined(LINUX_CYCLE_COUNT)
  840. #include <linux/perf_event.h>
  841. #include <sys/syscall.h>
  842. #include <unistd.h>
  843. static THREAD_LS_T word64 begin_cycles;
  844. static THREAD_LS_T word64 total_cycles;
  845. static THREAD_LS_T int cycles = -1;
  846. static THREAD_LS_T struct perf_event_attr atr;
  847. #define INIT_CYCLE_COUNTER do { \
  848. atr.type = PERF_TYPE_HARDWARE; \
  849. atr.config = PERF_COUNT_HW_CPU_CYCLES; \
  850. cycles = (int)syscall(__NR_perf_event_open, &atr, 0, -1, -1, 0); \
  851. } while (0);
  852. #define BEGIN_INTEL_CYCLES read(cycles, &begin_cycles, sizeof(begin_cycles));
  853. #define END_INTEL_CYCLES do { \
  854. read(cycles, &total_cycles, sizeof(total_cycles)); \
  855. total_cycles = total_cycles - begin_cycles; \
  856. } while (0);
  857. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  858. #define SHOW_INTEL_CYCLES(b, n, s) \
  859. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " %s = %6.2f\n", \
  860. bench_result_words1[lng_index][2], \
  861. (float)total_cycles / (count*s))
  862. #define SHOW_INTEL_CYCLES_CSV(b, n, s) \
  863. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), "%.6f,\n", \
  864. (float)total_cycles / (count*s))
  865. #elif defined(SYNERGY_CYCLE_COUNT)
  866. #include "hal_data.h"
  867. static THREAD_LS_T word64 begin_cycles;
  868. static THREAD_LS_T word64 total_cycles;
  869. #define INIT_CYCLE_COUNTER
  870. #define BEGIN_INTEL_CYCLES begin_cycles = DWT->CYCCNT = 0;
  871. #define END_INTEL_CYCLES total_cycles = DWT->CYCCNT - begin_cycles;
  872. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  873. #define SHOW_INTEL_CYCLES(b, n, s) \
  874. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " %s = %6.2f\n", \
  875. bench_result_words1[lng_index][2], \
  876. (float)total_cycles / (count*s))
  877. #define SHOW_INTEL_CYCLES_CSV(b, n, s) \
  878. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), "%.6f,\n", \
  879. (float)total_cycles / (count*s))
  880. #elif defined(WOLFSSL_ESPIDF)
  881. static THREAD_LS_T word64 begin_cycles;
  882. static THREAD_LS_T word64 total_cycles;
  883. /* the return value */
  884. static THREAD_LS_T word64 _xthal_get_ccount_ex = 0;
  885. /* the last value seen, adjusted for an overflow */
  886. static THREAD_LS_T word64 _xthal_get_ccount_last = 0;
  887. /* TAG for ESP_LOGx() */
  888. static char * TAG = "wolfssl_benchmark";
  889. #define HAVE_GET_CYCLES
  890. #define INIT_CYCLE_COUNTER
  891. static WC_INLINE word64 get_xtensa_cycles(void);
  892. /* WARNING the hal UINT xthal_get_ccount() quietly rolls over. */
  893. #define BEGIN_ESP_CYCLES begin_cycles = (get_xtensa_cycles());
  894. /* since it rolls over, we have something that will tolerate one */
  895. #define END_ESP_CYCLES \
  896. ESP_LOGV(TAG,"%llu - %llu", \
  897. get_xtensa_cycles(), \
  898. begin_cycles \
  899. ); \
  900. total_cycles = (get_xtensa_cycles() - begin_cycles);
  901. #define SHOW_ESP_CYCLES(b, n, s) \
  902. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " %s = %6.2f\n", \
  903. bench_result_words1[lng_index][2], \
  904. (float)total_cycles / (count*s) \
  905. )
  906. #define SHOW_ESP_CYCLES_CSV(b, n, s) \
  907. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), "%.6f,\n", \
  908. (float)total_cycles / (count*s))
  909. /* xthal_get_ccount_ex() is a single-overflow-tolerant extension to
  910. ** the Espressif `unsigned xthal_get_ccount()` which is known to overflow
  911. ** at least once during full benchmark tests.
  912. */
  913. word64 xthal_get_ccount_ex()
  914. {
  915. /* reminder: unsigned long long max = 18,446,744,073,709,551,615 */
  916. /* the currently observed clock counter value */
  917. word64 thisVal = xthal_get_ccount();
  918. /* if the current value is less than the previous value,
  919. ** we likely overflowed at least once.
  920. */
  921. if (thisVal < _xthal_get_ccount_last)
  922. {
  923. /* Warning: we assume the return type of xthal_get_ccount()
  924. ** will always be unsigned int to add UINT_MAX.
  925. **
  926. ** NOTE for long duration between calls with multiple overflows:
  927. **
  928. ** WILL NOT BE DETECTED - the return value will be INCORRECT.
  929. **
  930. ** At this time no single test overflows. This is currently only a
  931. ** concern for cumulative counts over multiple tests. As long
  932. ** as well call xthal_get_ccount_ex() with no more than one
  933. ** overflow CPU tick count, all will be well.
  934. */
  935. ESP_LOGV(TAG, "Alert: Detected xthal_get_ccount overflow, "
  936. "adding %ull", UINT_MAX);
  937. thisVal += (word64)UINT_MAX;
  938. }
  939. /* adjust our actual returned value that takes into account overflow */
  940. _xthal_get_ccount_ex += (thisVal - _xthal_get_ccount_last);
  941. /* all of this took some time, so reset the "last seen" value */
  942. _xthal_get_ccount_last = xthal_get_ccount();
  943. return _xthal_get_ccount_ex;
  944. }
  945. /* implement other architecture cycle counters here */
  946. #else
  947. /* if we don't know the platform, it is unlikely we can count CPU cycles */
  948. #undef HAVE_GET_CYCLES
  949. #define INIT_CYCLE_COUNTER
  950. #define BEGIN_INTEL_CYCLES
  951. #define END_INTEL_CYCLES
  952. #define SHOW_INTEL_CYCLES(b, n, s) b[XSTRLEN(b)] = '\n'
  953. #define SHOW_INTEL_CYCLES_CSV(b, n, s) b[XSTRLEN(b)] = '\n'
  954. #endif
  955. /* determine benchmark buffer to use (if NO_FILESYSTEM) */
  956. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  957. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  958. #define USE_CERT_BUFFERS_2048 /* default to 2048 */
  959. #endif
  960. #if defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048) || \
  961. defined(USE_CERT_BUFFERS_3072) || defined(USE_CERT_BUFFERS_4096) || \
  962. !defined(NO_DH)
  963. /* include test cert and key buffers for use with NO_FILESYSTEM */
  964. #include <wolfssl/certs_test.h>
  965. #endif
  966. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  967. #include <wolfssl/wolfcrypt/blake2.h>
  968. #endif
  969. #ifdef _MSC_VER
  970. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  971. #pragma warning(disable: 4996)
  972. #endif
  973. #ifdef WOLFSSL_CURRTIME_REMAP
  974. #define current_time WOLFSSL_CURRTIME_REMAP
  975. #else
  976. double current_time(int reset);
  977. #endif
  978. #ifdef LINUX_RUSAGE_UTIME
  979. static void check_for_excessive_stime(const char *desc,
  980. const char *desc_extra);
  981. #endif
  982. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND) && \
  983. !defined(HAVE_STACK_SIZE)
  984. #ifdef __cplusplus
  985. extern "C" {
  986. #endif
  987. WOLFSSL_API int wolfSSL_Debugging_ON(void);
  988. WOLFSSL_API void wolfSSL_Debugging_OFF(void);
  989. #ifdef __cplusplus
  990. } /* extern "C" */
  991. #endif
  992. #endif
  993. #if !defined(WC_NO_RNG) && \
  994. ((!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) \
  995. || !defined(NO_DH) || defined(WOLFSSL_KEY_GEN) || defined(HAVE_ECC) \
  996. || defined(HAVE_CURVE25519) || defined(HAVE_ED25519) \
  997. || defined(HAVE_CURVE448) || defined(HAVE_ED448) \
  998. || defined(WOLFSSL_HAVE_KYBER))
  999. #define HAVE_LOCAL_RNG
  1000. static THREAD_LS_T WC_RNG gRng;
  1001. #define GLOBAL_RNG &gRng
  1002. #else
  1003. #define GLOBAL_RNG NULL
  1004. #endif
  1005. #if defined(HAVE_ED25519) || defined(HAVE_CURVE25519) || \
  1006. defined(HAVE_CURVE448) || defined(HAVE_ED448) || \
  1007. defined(HAVE_ECC) || !defined(NO_DH) || \
  1008. !defined(NO_RSA) || defined(HAVE_SCRYPT) || \
  1009. defined(WOLFSSL_HAVE_KYBER)
  1010. #define BENCH_ASYM
  1011. #endif
  1012. #if defined(BENCH_ASYM)
  1013. #if defined(HAVE_ECC) || !defined(NO_RSA) || !defined(NO_DH) || \
  1014. defined(HAVE_CURVE25519) || defined(HAVE_ED25519) || \
  1015. defined(HAVE_CURVE448) || defined(HAVE_ED448) || \
  1016. defined(WOLFSSL_HAVE_KYBER)
  1017. static const char* bench_result_words2[][5] = {
  1018. { "ops took", "sec" , "avg" , "ops/sec", NULL }, /* 0 English */
  1019. #ifndef NO_MULTIBYTE_PRINT
  1020. { "回処理を", "秒で実施", "平均", "処理/秒", NULL }, /* 1 Japanese */
  1021. #endif
  1022. };
  1023. #endif
  1024. #endif
  1025. #ifdef WOLFSSL_CAAM
  1026. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  1027. #ifdef WOLFSSL_SECO_CAAM
  1028. #define SECO_MAX_UPDATES 10000
  1029. #define SECO_BENCHMARK_NONCE 0x7777
  1030. #define SECO_KEY_STORE_ID 1
  1031. #endif
  1032. static THREAD_LS_T int devId = WOLFSSL_CAAM_DEVID;
  1033. #else
  1034. static THREAD_LS_T int devId = INVALID_DEVID;
  1035. #endif
  1036. /* Asynchronous helper macros */
  1037. #ifdef WC_ENABLE_BENCH_THREADING
  1038. typedef struct ThreadData {
  1039. pthread_t thread_id;
  1040. } ThreadData;
  1041. static ThreadData* g_threadData;
  1042. static volatile int g_threadCount;
  1043. #endif
  1044. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_CAAM)
  1045. #ifndef NO_HW_BENCH
  1046. #define BENCH_DEVID
  1047. #endif
  1048. #define BENCH_DEVID_GET_NAME(useDeviceID) (useDeviceID) ? "HW" : "SW"
  1049. #else
  1050. #define BENCH_DEVID_GET_NAME(useDeviceID) ""
  1051. #endif
  1052. #ifdef WOLFSSL_ASYNC_CRYPT
  1053. static WOLF_EVENT_QUEUE eventQueue;
  1054. #define BENCH_ASYNC_GET_DEV(obj) (&(obj)->asyncDev)
  1055. #define BENCH_MAX_PENDING (WOLF_ASYNC_MAX_PENDING)
  1056. static int bench_async_check(int* ret, WC_ASYNC_DEV* asyncDev,
  1057. int callAgain, int* times, int limit, int* pending)
  1058. {
  1059. int allowNext = 0;
  1060. /* this state can be set from a different thread */
  1061. WOLF_EVENT_STATE state = asyncDev->event.state;
  1062. /* if algo doesn't require calling again then use this flow */
  1063. if (state == WOLF_EVENT_STATE_DONE) {
  1064. if (callAgain) {
  1065. /* needs called again, so allow it and handle completion in bench_async_handle */
  1066. allowNext = 1;
  1067. }
  1068. else {
  1069. *ret = asyncDev->event.ret;
  1070. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  1071. (*times)++;
  1072. if (*pending > 0) /* to support case where async blocks */
  1073. (*pending)--;
  1074. if ((*times + *pending) < limit)
  1075. allowNext = 1;
  1076. }
  1077. }
  1078. /* if slot is available and we haven't reached limit, start another */
  1079. else if (state == WOLF_EVENT_STATE_READY && (*times + *pending) < limit) {
  1080. allowNext = 1;
  1081. }
  1082. return allowNext;
  1083. }
  1084. static int bench_async_handle(int* ret, WC_ASYNC_DEV* asyncDev,
  1085. int callAgain, int* times, int* pending)
  1086. {
  1087. WOLF_EVENT_STATE state = asyncDev->event.state;
  1088. if (*ret == WC_PENDING_E) {
  1089. if (state == WOLF_EVENT_STATE_DONE) {
  1090. *ret = asyncDev->event.ret;
  1091. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  1092. (*times)++;
  1093. (*pending)--;
  1094. }
  1095. else {
  1096. (*pending)++;
  1097. *ret = wc_AsyncHandle(asyncDev, &eventQueue,
  1098. callAgain ? WC_ASYNC_FLAG_CALL_AGAIN : WC_ASYNC_FLAG_NONE);
  1099. }
  1100. }
  1101. else if (*ret >= 0) {
  1102. *ret = asyncDev->event.ret;
  1103. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  1104. (*times)++;
  1105. if (*pending > 0) /* to support case where async blocks */
  1106. (*pending)--;
  1107. }
  1108. return (*ret >= 0) ? 1 : 0;
  1109. }
  1110. static WC_INLINE int bench_async_poll(int* pending)
  1111. {
  1112. int ret, asyncDone = 0;
  1113. ret = wolfAsync_EventQueuePoll(&eventQueue, NULL, NULL, 0,
  1114. WOLF_POLL_FLAG_CHECK_HW, &asyncDone);
  1115. if (ret != 0) {
  1116. printf("%sAsync poll failed %d\n", err_prefix, ret);
  1117. return ret;
  1118. }
  1119. if (asyncDone == 0) {
  1120. #ifndef WC_NO_ASYNC_THREADING
  1121. /* give time to other threads */
  1122. wc_AsyncThreadYield();
  1123. #endif
  1124. }
  1125. (void)pending;
  1126. return asyncDone;
  1127. }
  1128. #else
  1129. #define BENCH_MAX_PENDING (1)
  1130. #define BENCH_ASYNC_GET_DEV(obj) NULL
  1131. static WC_INLINE int bench_async_check(int* ret, void* asyncDev,
  1132. int callAgain, int* times, int limit, int* pending)
  1133. {
  1134. (void)ret;
  1135. (void)asyncDev;
  1136. (void)callAgain;
  1137. (void)times;
  1138. (void)limit;
  1139. (void)pending;
  1140. return 1;
  1141. }
  1142. static WC_INLINE int bench_async_handle(int* ret, void* asyncDev,
  1143. int callAgain, int* times, int* pending)
  1144. {
  1145. (void)asyncDev;
  1146. (void)callAgain;
  1147. (void)pending;
  1148. if (*ret >= 0) {
  1149. /* operation completed */
  1150. (*times)++;
  1151. return 1;
  1152. }
  1153. return 0;
  1154. }
  1155. #define bench_async_poll(p)
  1156. #endif /* WOLFSSL_ASYNC_CRYPT */
  1157. /* maximum runtime for each benchmark */
  1158. #ifndef BENCH_MIN_RUNTIME_SEC
  1159. #define BENCH_MIN_RUNTIME_SEC 1.0f
  1160. #endif
  1161. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1162. #define AES_AUTH_TAG_SZ 16
  1163. #define BENCH_CIPHER_ADD AES_AUTH_TAG_SZ
  1164. static word32 aesAuthAddSz = AES_AUTH_ADD_SZ;
  1165. #if !defined(AES_AAD_OPTIONS_DEFAULT)
  1166. #if !defined(NO_MAIN_DRIVER)
  1167. #define AES_AAD_OPTIONS_DEFAULT 0x1U
  1168. #else
  1169. #define AES_AAD_OPTIONS_DEFAULT 0x3U
  1170. #endif
  1171. #endif
  1172. #define AES_AAD_STRING(s) (aesAuthAddSz == 0 ? (s "-no_AAD") : (aesAuthAddSz == AES_AUTH_ADD_SZ ? (s) : (s "-custom")))
  1173. enum en_aad_options {
  1174. AAD_SIZE_DEFAULT = 0x1U,
  1175. AAD_SIZE_ZERO = 0x2U,
  1176. AAD_SIZE_CUSTOM = 0x4U,
  1177. };
  1178. static word32 aes_aad_options = AES_AAD_OPTIONS_DEFAULT;
  1179. static word32 aes_aad_size = 0;
  1180. static void bench_aes_aad_options_wrap(void (*fn)(int), int i)
  1181. {
  1182. word32 aesAuthAddSz_orig = aesAuthAddSz;
  1183. word32 options = aes_aad_options;
  1184. while(options) {
  1185. if (options & AAD_SIZE_DEFAULT) {
  1186. aesAuthAddSz = AES_AUTH_ADD_SZ;
  1187. options &= ~AAD_SIZE_DEFAULT;
  1188. }
  1189. else if (options & AAD_SIZE_ZERO) {
  1190. aesAuthAddSz = 0;
  1191. options &= ~AAD_SIZE_ZERO;
  1192. }
  1193. else if (options & AAD_SIZE_CUSTOM) {
  1194. aesAuthAddSz = aes_aad_size;
  1195. options &= ~AAD_SIZE_CUSTOM;
  1196. }
  1197. fn(i);
  1198. aesAuthAddSz = aesAuthAddSz_orig;
  1199. }
  1200. }
  1201. #endif
  1202. #ifndef BENCH_CIPHER_ADD
  1203. #define BENCH_CIPHER_ADD 0
  1204. #endif
  1205. /* use kB instead of mB for embedded benchmarking */
  1206. #ifdef BENCH_EMBEDDED
  1207. enum BenchmarkBounds {
  1208. scryptCnt = 1,
  1209. ntimes = 2,
  1210. genTimes = BENCH_MAX_PENDING,
  1211. agreeTimes = 2
  1212. };
  1213. /* how many kB to test (en/de)cryption */
  1214. #define NUM_BLOCKS 25
  1215. #define BENCH_SIZE (1024uL)
  1216. #else
  1217. enum BenchmarkBounds {
  1218. scryptCnt = 10,
  1219. ntimes = 100,
  1220. genTimes = BENCH_MAX_PENDING, /* must be at least BENCH_MAX_PENDING */
  1221. agreeTimes = 100
  1222. };
  1223. /* how many megs to test (en/de)cryption */
  1224. #define NUM_BLOCKS 5
  1225. #define BENCH_SIZE (1024*1024uL)
  1226. #endif
  1227. static int numBlocks = NUM_BLOCKS;
  1228. static word32 bench_size = BENCH_SIZE;
  1229. static int base2 = 1;
  1230. static int digest_stream = 1;
  1231. #ifndef NO_RSA
  1232. /* Don't measure RSA sign/verify by default */
  1233. static int rsa_sign_verify = 0;
  1234. #endif
  1235. #ifndef NO_DH
  1236. /* Use the FFDHE parameters */
  1237. static int use_ffdhe = 0;
  1238. #endif
  1239. /* Don't print out in CSV format by default */
  1240. static int csv_format = 0;
  1241. #ifdef WOLFSSL_XILINX_CRYPT_VERSAL
  1242. /* Versal PLM maybe prints an error message to the same console.
  1243. * In order to not mix those outputs up, sleep a little while
  1244. * before erroring out.
  1245. */
  1246. #define SLEEP_ON_ERROR(ret) do{ if (ret != 0) { sleep(1); } }while(0)
  1247. #else
  1248. #define SLEEP_ON_ERROR(ret) do{ /* noop */ }while(0)
  1249. #endif
  1250. /* globals for cipher tests */
  1251. static THREAD_LS_T byte* bench_plain = NULL;
  1252. static THREAD_LS_T byte* bench_cipher = NULL;
  1253. static const XGEN_ALIGN byte bench_key_buf[] =
  1254. {
  1255. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  1256. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  1257. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67,
  1258. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  1259. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  1260. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff,
  1261. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  1262. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
  1263. };
  1264. static const XGEN_ALIGN byte bench_iv_buf[] =
  1265. {
  1266. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  1267. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1268. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  1269. };
  1270. static THREAD_LS_T byte* bench_key = NULL;
  1271. static THREAD_LS_T byte* bench_iv = NULL;
  1272. #ifdef WOLFSSL_STATIC_MEMORY
  1273. #ifdef WOLFSSL_STATIC_MEMORY_TEST_SZ
  1274. static byte gBenchMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  1275. #elif defined(BENCH_EMBEDDED)
  1276. static byte gBenchMemory[50000];
  1277. #else
  1278. static byte gBenchMemory[400000];
  1279. #endif
  1280. #endif
  1281. /* This code handles cases with systems where static (non cost) ram variables
  1282. aren't properly initialized with data */
  1283. static void benchmark_static_init(int force)
  1284. {
  1285. static int gBenchStaticInit = 0;
  1286. if (gBenchStaticInit == 0 || force) {
  1287. gBenchStaticInit = 1;
  1288. /* Init static variables */
  1289. numBlocks = NUM_BLOCKS;
  1290. bench_size = BENCH_SIZE;
  1291. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1292. aesAuthAddSz = AES_AUTH_ADD_SZ;
  1293. aes_aad_options = AES_AAD_OPTIONS_DEFAULT;
  1294. aes_aad_size = 0;
  1295. #endif
  1296. base2 = 1;
  1297. digest_stream = 1;
  1298. bench_all = 1;
  1299. bench_cipher_algs = 0;
  1300. bench_digest_algs = 0;
  1301. bench_mac_algs = 0;
  1302. bench_asym_algs = 0;
  1303. bench_pq_asym_algs = 0;
  1304. bench_other_algs = 0;
  1305. csv_format = 0;
  1306. }
  1307. }
  1308. /*****************************************************************************/
  1309. /* Begin Stats Functions */
  1310. /*****************************************************************************/
  1311. typedef enum bench_stat_type {
  1312. BENCH_STAT_ASYM,
  1313. BENCH_STAT_SYM,
  1314. BENCH_STAT_IGNORE,
  1315. } bench_stat_type_t;
  1316. #ifdef WC_ENABLE_BENCH_THREADING
  1317. static int gPrintStats = 0;
  1318. static pthread_mutex_t bench_lock = PTHREAD_MUTEX_INITIALIZER;
  1319. #ifndef BENCH_MAX_NAME_SZ
  1320. #define BENCH_MAX_NAME_SZ 24
  1321. #endif
  1322. typedef struct bench_stats {
  1323. struct bench_stats* next;
  1324. struct bench_stats* prev;
  1325. char algo[BENCH_MAX_NAME_SZ+1]; /* may not be static, so make copy */
  1326. const char* desc;
  1327. double perfsec;
  1328. int strength;
  1329. int useDeviceID;
  1330. int finishCount;
  1331. bench_stat_type_t type;
  1332. int lastRet;
  1333. const char* perftype;
  1334. } bench_stats_t;
  1335. static bench_stats_t* bench_stats_head;
  1336. static bench_stats_t* bench_stats_tail;
  1337. static bench_stats_t* bench_stats_add(bench_stat_type_t type,
  1338. const char* algo, int strength, const char* desc, int useDeviceID,
  1339. double perfsec, const char* perftype, int ret)
  1340. {
  1341. bench_stats_t* bstat = NULL;
  1342. /* protect bench_stats_head and bench_stats_tail access */
  1343. PTHREAD_CHECK_RET(pthread_mutex_lock(&bench_lock));
  1344. if (algo != NULL) {
  1345. /* locate existing in list */
  1346. for (bstat = bench_stats_head; bstat != NULL; bstat = bstat->next) {
  1347. /* match based on algo, strength and desc */
  1348. if (XSTRNCMP(bstat->algo, algo, BENCH_MAX_NAME_SZ) == 0 &&
  1349. bstat->strength == strength &&
  1350. bstat->desc == desc &&
  1351. bstat->useDeviceID == useDeviceID) {
  1352. break;
  1353. }
  1354. }
  1355. }
  1356. if (bstat == NULL) {
  1357. /* allocate new and put on list */
  1358. bstat = (bench_stats_t*)XMALLOC(sizeof(bench_stats_t), NULL,
  1359. DYNAMIC_TYPE_INFO);
  1360. if (bstat) {
  1361. XMEMSET(bstat, 0, sizeof(bench_stats_t));
  1362. /* add to list */
  1363. bstat->next = NULL;
  1364. if (bench_stats_tail == NULL) {
  1365. bench_stats_head = bstat;
  1366. }
  1367. else {
  1368. bench_stats_tail->next = bstat;
  1369. bstat->prev = bench_stats_tail;
  1370. }
  1371. bench_stats_tail = bstat; /* add to the end either way */
  1372. }
  1373. }
  1374. if (bstat) {
  1375. bstat->type = type;
  1376. if (algo != NULL)
  1377. XSTRNCPY(bstat->algo, algo, BENCH_MAX_NAME_SZ);
  1378. bstat->strength = strength;
  1379. bstat->desc = desc;
  1380. bstat->useDeviceID = useDeviceID;
  1381. bstat->perfsec += perfsec;
  1382. bstat->finishCount++;
  1383. bstat->perftype = perftype;
  1384. if (bstat->lastRet > ret)
  1385. bstat->lastRet = ret; /* track last error */
  1386. }
  1387. PTHREAD_CHECK_RET(pthread_mutex_unlock(&bench_lock));
  1388. return bstat;
  1389. }
  1390. void bench_stats_print(void)
  1391. {
  1392. bench_stats_t* bstat;
  1393. /* protect bench_stats_head and bench_stats_tail access */
  1394. PTHREAD_CHECK_RET(pthread_mutex_lock(&bench_lock));
  1395. for (bstat = bench_stats_head; bstat != NULL; ) {
  1396. if (bstat->type == BENCH_STAT_SYM) {
  1397. printf("%-16s%s %8.3f %s/s\n", bstat->desc,
  1398. BENCH_DEVID_GET_NAME(bstat->useDeviceID), bstat->perfsec,
  1399. base2 ? "MB" : "mB");
  1400. }
  1401. else {
  1402. printf("%-5s %4d %-9s %s %.3f ops/sec\n",
  1403. bstat->algo, bstat->strength, bstat->desc,
  1404. BENCH_DEVID_GET_NAME(bstat->useDeviceID), bstat->perfsec);
  1405. }
  1406. bstat = bstat->next;
  1407. }
  1408. PTHREAD_CHECK_RET(pthread_mutex_unlock(&bench_lock));
  1409. }
  1410. #else /* !WC_ENABLE_BENCH_THREADING */
  1411. typedef struct bench_stats {
  1412. const char* algo;
  1413. const char* desc;
  1414. double perfsec;
  1415. const char* perftype;
  1416. int strength;
  1417. bench_stat_type_t type;
  1418. int ret;
  1419. } bench_stats_t;
  1420. /* 16 threads and 8 different operations. */
  1421. #define MAX_BENCH_STATS (16 * 8)
  1422. static bench_stats_t gStats[MAX_BENCH_STATS];
  1423. static int gStatsCount;
  1424. static bench_stats_t* bench_stats_add(bench_stat_type_t type,
  1425. const char* algo, int strength, const char* desc, int useDeviceID,
  1426. double perfsec, const char* perftype, int ret)
  1427. {
  1428. bench_stats_t* bstat = NULL;
  1429. if (gStatsCount >= MAX_BENCH_STATS)
  1430. return bstat;
  1431. bstat = &gStats[gStatsCount++];
  1432. bstat->algo = algo;
  1433. bstat->desc = desc;
  1434. bstat->perfsec = perfsec;
  1435. bstat->perftype = perftype;
  1436. bstat->strength = strength;
  1437. bstat->type = type;
  1438. bstat->ret = ret;
  1439. (void)useDeviceID;
  1440. return bstat;
  1441. }
  1442. void bench_stats_print(void)
  1443. {
  1444. int i;
  1445. bench_stats_t* bstat;
  1446. for (i=0; i<gStatsCount; i++) {
  1447. bstat = &gStats[i];
  1448. if (bstat->type == BENCH_STAT_SYM) {
  1449. printf("%-16s %8.3f %s/s\n", bstat->desc, bstat->perfsec,
  1450. base2 ? "MB" : "mB");
  1451. }
  1452. else if (bstat->type == BENCH_STAT_ASYM) {
  1453. printf("%-5s %4d %-9s %.3f ops/sec\n",
  1454. bstat->algo, bstat->strength, bstat->desc, bstat->perfsec);
  1455. }
  1456. }
  1457. }
  1458. #endif /* WC_ENABLE_BENCH_THREADING */
  1459. static WC_INLINE void bench_stats_init(void)
  1460. {
  1461. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  1462. bench_stats_head = NULL;
  1463. bench_stats_tail = NULL;
  1464. #endif
  1465. INIT_CYCLE_COUNTER
  1466. }
  1467. static WC_INLINE void bench_stats_start(int* count, double* start)
  1468. {
  1469. *count = 0;
  1470. *start = current_time(1);
  1471. #ifdef WOLFSSL_ESPIDF
  1472. ESP_LOGV(TAG, "finish total_cycles = %llu, start=%f",
  1473. total_cycles, *start );
  1474. BEGIN_ESP_CYCLES
  1475. #else
  1476. BEGIN_INTEL_CYCLES
  1477. #endif
  1478. }
  1479. static WC_INLINE int bench_stats_check(double start)
  1480. {
  1481. return ((current_time(0) - start) < BENCH_MIN_RUNTIME_SEC);
  1482. }
  1483. /* return text for units and scale the value of blocks as needed for base2 */
  1484. static WC_INLINE const char* specified_base2_blockType(double * blocks)
  1485. {
  1486. const char* rt;
  1487. #if ( defined(WOLFSSL_BENCHMARK_FIXED_UNITS_G) \
  1488. || defined(WOLFSSL_BENCHMARK_FIXED_UNITS_GB) )
  1489. #undef WOLFSSL_FIXED_UNITS_PER_SEC
  1490. #define WOLFSSL_FIXED_UNITS_PER_SEC "GB/s"
  1491. *blocks /= (1000UL * 1000UL * 1000UL);
  1492. rt = "GiB";
  1493. #elif ( defined(WOLFSSL_BENCHMARK_FIXED_UNITS_M) \
  1494. || defined(WOLFSSL_BENCHMARK_FIXED_UNITS_MB) )
  1495. #undef WOLFSSL_FIXED_UNITS_PER_SEC
  1496. #define WOLFSSL_FIXED_UNITS_PER_SEC "MB/s"
  1497. *blocks /= (1024UL * 1024UL);
  1498. rt = "MiB";
  1499. #elif ( defined (WOLFSSL_BENCHMARK_FIXED_UNITS_K) \
  1500. || defined (WOLFSSL_BENCHMARK_FIXED_UNITS_KB))
  1501. #undef WOLFSSL_FIXED_UNITS_PER_SEC
  1502. #define WOLFSSL_FIXED_UNITS_PER_SEC "KB/s"
  1503. *blocks /= 1024;
  1504. rt = "KiB";
  1505. #elif ( defined (WOLFSSL_BENCHMARK_FIXED_UNITS_B) )
  1506. #undef WOLFSSL_FIXED_UNITS_PER_SEC
  1507. #define WOLFSSL_FIXED_UNITS_PER_SEC "bytes/s"
  1508. (void)(*blocks); /* no adjustment, just appease compiler for not used */
  1509. rt = "bytes";
  1510. #else
  1511. /* if no user-specified, auto-scale each metric (results vary)
  1512. **
  1513. ** determine if we should show as KB or MB or bytes. No GiB here.
  1514. */
  1515. if (*blocks > (1024UL * 1024UL)) {
  1516. *blocks /= (1024UL * 1024UL);
  1517. rt = "MiB";
  1518. }
  1519. else if (*blocks > 1024) {
  1520. *blocks /= 1024;
  1521. rt = "KiB";
  1522. }
  1523. else {
  1524. rt = "bytes";
  1525. }
  1526. #endif
  1527. return rt;
  1528. } /* specified_base2_blockType() */
  1529. /* return text for units and scale the value of blocks as needed */
  1530. static WC_INLINE const char* specified_blockType(double * blocks)
  1531. {
  1532. const char* rt;
  1533. #if ( defined(WOLFSSL_BENCHMARK_FIXED_UNITS_G) \
  1534. || defined(WOLFSSL_BENCHMARK_FIXED_UNITS_GB) )
  1535. *blocks /= (1000UL * 1000UL * 1000UL);
  1536. rt = "GB";
  1537. #elif ( defined(WOLFSSL_BENCHMARK_FIXED_UNITS_M) \
  1538. || defined(WOLFSSL_BENCHMARK_FIXED_UNITS_MB) )
  1539. *blocks /= (1000UL * 1000UL);
  1540. rt = "MB";
  1541. #elif ( defined (WOLFSSL_BENCHMARK_FIXED_UNITS_K) \
  1542. || defined (WOLFSSL_BENCHMARK_FIXED_UNITS_KB) )
  1543. *blocks /= (1000UL);
  1544. rt = "KB";
  1545. #elif ( defined (WOLFSSL_BENCHMARK_FIXED_UNITS_B) )
  1546. (void)(*blocks); /* no adjustment, just appease compiler */
  1547. rt = "bytes";
  1548. #else
  1549. /* if not user-specified, auto-scale each metric (results vary)
  1550. **
  1551. ** determine if we should show as KB or MB or bytes
  1552. */
  1553. if (*blocks > (1000UL * 1000UL)) {
  1554. *blocks /= (1000UL * 1000UL);
  1555. rt = "MB";
  1556. }
  1557. else if (*blocks > 1000) {
  1558. *blocks /= 1000; /* make KB */
  1559. rt = "KB";
  1560. }
  1561. else {
  1562. rt = "bytes";
  1563. } /* rt auto-assigned */
  1564. #endif /* WOLFSSL_BENCHMARK UNITS */
  1565. return rt;
  1566. } /* specified_blockType */
  1567. /* countSz is number of bytes that 1 count represents. Normally bench_size,
  1568. * except for AES direct that operates on AES_BLOCK_SIZE blocks */
  1569. static void bench_stats_sym_finish(const char* desc, int useDeviceID,
  1570. int count, int countSz,
  1571. double start, int ret)
  1572. {
  1573. double total, persec = 0, blocks = (double)count;
  1574. const char* blockType;
  1575. char msg[__BENCHMARK_MAXIMUM_LINE_LENGTH] = {0};
  1576. const char** word = bench_result_words1[lng_index];
  1577. static int sym_header_printed = 0;
  1578. #ifdef WOLFSSL_ESPIDF
  1579. END_ESP_CYCLES
  1580. #else
  1581. END_INTEL_CYCLES
  1582. #endif
  1583. total = current_time(0) - start;
  1584. #ifdef WOLFSSL_ESPIDF
  1585. ESP_LOGV(TAG, "%s total_cycles = %llu", desc, total_cycles);
  1586. #endif
  1587. #ifdef LINUX_RUSAGE_UTIME
  1588. check_for_excessive_stime(desc, "");
  1589. #endif
  1590. /* calculate actual bytes */
  1591. blocks *= countSz;
  1592. if (csv_format == 1) {
  1593. /* only print out header once */
  1594. if (sym_header_printed == 0) {
  1595. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  1596. /* machine parseable CSV */
  1597. #ifdef HAVE_GET_CYCLES
  1598. printf("%s", "\"sym\",Algorithm,HW/SW,bytes_total,seconds_total,"
  1599. WOLFSSL_FIXED_UNITS_PER_SEC ",cycles_total,Cycles per byte,\n");
  1600. #else
  1601. printf("%s", "\"sym\",Algorithm,HW/SW,bytes_total,seconds_total,"
  1602. WOLFSSL_FIXED_UNITS_PER_SEC ",cycles_total,\n");
  1603. #endif
  1604. #else
  1605. /* normal CSV */
  1606. #ifdef HAVE_GET_CYCLES
  1607. printf("\n\nSymmetric Ciphers:\n\n");
  1608. printf("Algorithm,"
  1609. WOLFSSL_FIXED_UNITS_PER_SEC ",Cycles per byte,\n");
  1610. #else
  1611. printf("\n\nSymmetric Ciphers:\n\n");
  1612. printf("Algorithm,"
  1613. WOLFSSL_FIXED_UNITS_PER_SEC ", \n");
  1614. #endif
  1615. #endif
  1616. sym_header_printed = 1;
  1617. }
  1618. }
  1619. /* determine if we have fixed units, or auto-scale bits or bytes for units.
  1620. ** note that the blockType text is assigned AND the blocks param is scaled.
  1621. */
  1622. if (base2) {
  1623. blockType = specified_base2_blockType(&blocks);
  1624. } /* is base2 bit counter */
  1625. else {
  1626. blockType = specified_blockType(&blocks);
  1627. } /* not base2, is byte counter */
  1628. /* calculate blocks per second */
  1629. if (total > 0) {
  1630. persec = (1 / total) * blocks;
  1631. }
  1632. SLEEP_ON_ERROR(ret);
  1633. /* format and print to terminal */
  1634. if (csv_format == 1) {
  1635. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  1636. #ifdef WOLFSSL_ESPIDF
  1637. unsigned long bytes_processed =
  1638. (unsigned long)count * (unsigned long)countSz;
  1639. #else
  1640. word64 bytes_processed = (word64)count * (word64)countSz;
  1641. #endif
  1642. /* note this codepath brings in all the fields from the non-CSV case. */
  1643. #ifdef WOLFSSL_ESPIDF
  1644. #ifdef HAVE_GET_CYCLES
  1645. (void)XSNPRINTF(msg, sizeof(msg), "sym,%s,%s,%lu,%f,%f,%lu,", desc,
  1646. BENCH_DEVID_GET_NAME(useDeviceID),
  1647. bytes_processed, total, persec,
  1648. (long unsigned int) total_cycles);
  1649. #else
  1650. #warning "HAVE_GET_CYCLES should be defined for WOLFSSL_ESPIDF"
  1651. #endif
  1652. /* implement other architectures here */
  1653. #else
  1654. #ifdef HAVE_GET_CYCLES
  1655. (void)XSNPRINTF(msg, sizeof(msg), "sym,%s,%s,%lu,%f,%f,%lu,", desc,
  1656. BENCH_DEVID_GET_NAME(useDeviceID),
  1657. bytes_processed, total, persec, total_cycles);
  1658. #else
  1659. (void)XSNPRINTF(msg, sizeof(msg), "sym,%s,%s,%lu,%f,%f,", desc,
  1660. BENCH_ASYNC_GET_NAME(useDeviceID),
  1661. bytes_processed, total, persec);
  1662. #endif
  1663. #endif
  1664. #else
  1665. (void)XSNPRINTF(msg, sizeof(msg), "%s,%f,", desc, persec);
  1666. #endif
  1667. #ifdef WOLFSSL_ESPIDF
  1668. SHOW_ESP_CYCLES_CSV(msg, sizeof(msg), countSz);
  1669. ESP_LOGV(TAG, "finish total_cycles = %llu", total_cycles);
  1670. /* implement other cycle counters here */
  1671. #else
  1672. SHOW_INTEL_CYCLES_CSV(msg, sizeof(msg), countSz);
  1673. #endif
  1674. } /* if (csv_format == 1) */
  1675. else {
  1676. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  1677. #ifdef HAVE_GET_CYCLES
  1678. (void)XSNPRINTF(msg, sizeof(msg),
  1679. "%-24s%s %5.0f %s %s %5.3f %s, %8.3f %s/s"
  1680. ", %lu cycles,",
  1681. desc, BENCH_DEVID_GET_NAME(useDeviceID), blocks, blockType,
  1682. word[0], total, word[1], persec, blockType,
  1683. (unsigned long) total_cycles);
  1684. #else
  1685. (void)XSNPRINTF(msg, sizeof(msg),
  1686. "%-24s%s %5.0f %s %s %5.3f %s, %8.3f %s/s"
  1687. ",",
  1688. desc, BENCH_ASYNC_GET_NAME(useDeviceID), blocks, blockType,
  1689. word[0], total, word[1], persec, blockType);
  1690. #endif /* HAVE_GET_CYCLES */
  1691. #else
  1692. (void)XSNPRINTF(msg, sizeof(msg),
  1693. "%-24s%s %5.0f %s %s %5.3f %s, %8.3f %s/s",
  1694. desc, BENCH_DEVID_GET_NAME(useDeviceID), blocks, blockType,
  1695. word[0], total, word[1], persec, blockType);
  1696. #endif
  1697. #ifdef WOLFSSL_ESPIDF
  1698. SHOW_ESP_CYCLES(msg, sizeof(msg), countSz);
  1699. /* implement other architecture cycle counters here */
  1700. #else
  1701. SHOW_INTEL_CYCLES(msg, sizeof(msg), countSz);
  1702. #endif
  1703. } /* not CSV format */
  1704. printf("%s", msg);
  1705. /* show errors */
  1706. if (ret < 0) {
  1707. printf("%sBenchmark %s failed: %d\n", err_prefix, desc, ret);
  1708. }
  1709. #ifndef WOLFSSL_SGX
  1710. fflush(stdout);
  1711. #endif
  1712. /* Add to thread stats */
  1713. bench_stats_add(BENCH_STAT_SYM, desc, 0, desc, useDeviceID, persec,
  1714. blockType, ret);
  1715. (void)useDeviceID;
  1716. (void)ret;
  1717. TEST_SLEEP();
  1718. } /* bench_stats_sym_finish */
  1719. #ifdef BENCH_ASYM
  1720. #if defined(HAVE_ECC) || !defined(NO_RSA) || !defined(NO_DH) || \
  1721. defined(HAVE_CURVE25519) || defined(HAVE_ED25519) || \
  1722. defined(HAVE_CURVE448) || defined(HAVE_ED448) || \
  1723. defined(WOLFSSL_HAVE_KYBER)
  1724. static void bench_stats_asym_finish_ex(const char* algo, int strength,
  1725. const char* desc, const char* desc_extra, int useDeviceID, int count,
  1726. double start, int ret)
  1727. {
  1728. double total, each = 0, opsSec, milliEach;
  1729. const char **word = bench_result_words2[lng_index];
  1730. const char* kOpsSec = "Ops/Sec";
  1731. char msg[256] = {0};
  1732. static int asym_header_printed = 0;
  1733. total = current_time(0) - start;
  1734. #ifdef LINUX_RUSAGE_UTIME
  1735. check_for_excessive_stime(desc, desc_extra);
  1736. #endif
  1737. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  1738. #ifdef WOLFSSL_ESPIDF
  1739. END_ESP_CYCLES
  1740. #else
  1741. END_INTEL_CYCLES
  1742. #endif
  1743. #endif
  1744. /* some sanity checks on the final numbers */
  1745. if (count > 0) {
  1746. each = total / count; /* per second */
  1747. }
  1748. else {
  1749. count = 0;
  1750. each = 0;
  1751. }
  1752. if (total > 0) {
  1753. opsSec = count / total; /* ops second */
  1754. }
  1755. else {
  1756. opsSec = 0;
  1757. }
  1758. milliEach = each * 1000; /* milliseconds */
  1759. SLEEP_ON_ERROR(ret);
  1760. /* format and print to terminal */
  1761. if (csv_format == 1) {
  1762. /* only print out header once */
  1763. if (asym_header_printed == 0) {
  1764. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  1765. #ifdef HAVE_GET_CYCLES
  1766. printf("%s", "\"asym\",Algorithm,key size,operation,avg ms,ops/sec,"
  1767. "ops,secs,cycles,cycles/op\n");
  1768. #else
  1769. printf("%s", "\"asym\",Algorithm,key size,operation,avg ms,ops/sec,"
  1770. "ops,secs\n");
  1771. #endif
  1772. #else
  1773. printf("\n%sAsymmetric Ciphers:\n\n", info_prefix);
  1774. printf("%sAlgorithm,key size,operation,avg ms,ops/sec,\n",
  1775. info_prefix);
  1776. #endif
  1777. asym_header_printed = 1;
  1778. }
  1779. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  1780. #ifdef HAVE_GET_CYCLES
  1781. (void)XSNPRINTF(msg, sizeof(msg),
  1782. "asym,%s,%d,%s%s,%.3f,%.3f,%d,%f,%lu,%.6f\n",
  1783. algo, strength, desc, desc_extra, milliEach, opsSec,
  1784. count, total, (unsigned long) total_cycles,
  1785. (double)total_cycles / (double)count);
  1786. #else
  1787. (void)XSNPRINTF(msg, sizeof(msg),
  1788. "asym,%s,%d,%s%s,%.3f,%.3f,%d,%f\n",
  1789. algo, strength, desc, desc_extra, milliEach, opsSec,
  1790. count, total);
  1791. #endif
  1792. #else
  1793. (void)XSNPRINTF(msg, sizeof(msg), "%s,%d,%s%s,%.3f,%.3f,\n", algo,
  1794. strength, desc, desc_extra, milliEach, opsSec);
  1795. #endif
  1796. } /* if (csv_format == 1) */
  1797. else {
  1798. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  1799. #ifdef HAVE_GET_CYCLES
  1800. (void)XSNPRINTF(msg, sizeof(msg),
  1801. "%-6s %5d %8s%-2s %s %6d %s %5.3f %s, %s %5.3f ms,"
  1802. " %.3f %s, %lu cycles\n", algo, strength, desc,
  1803. desc_extra, BENCH_DEVID_GET_NAME(useDeviceID),
  1804. count, word[0], total, word[1], word[2], milliEach,
  1805. opsSec, word[3], (unsigned long) total_cycles);
  1806. #else
  1807. (void)XSNPRINTF(msg, sizeof(msg),
  1808. "%-6s %5d %8s%-2s %s %6d %s %5.3f %s, %s %5.3f ms,"
  1809. " %.3f %s\n", algo, strength, desc,
  1810. desc_extra, BENCH_ASYNC_GET_NAME(useDeviceID),
  1811. count, word[0], total, word[1], word[2], milliEach,
  1812. opsSec, word[3]);
  1813. #endif /* HAVE_GET_CYCLES */
  1814. #else
  1815. (void)XSNPRINTF(msg, sizeof(msg),
  1816. "%-6s %5d %8s%-2s %s %6d %s %5.3f %s, %s %5.3f ms,"
  1817. " %.3f %s\n", algo, strength, desc, desc_extra,
  1818. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  1819. total, word[1], word[2], milliEach, opsSec, word[3]);
  1820. #endif
  1821. }
  1822. printf("%s", msg);
  1823. /* show errors */
  1824. if (ret < 0) {
  1825. printf("%sBenchmark %s %s %d failed: %d\n",
  1826. err_prefix, algo, desc, strength, ret);
  1827. }
  1828. #ifndef WOLFSSL_SGX
  1829. fflush(stdout);
  1830. #endif
  1831. /* Add to thread stats */
  1832. bench_stats_add(BENCH_STAT_ASYM, algo, strength, desc, useDeviceID, opsSec,
  1833. kOpsSec, ret);
  1834. (void)useDeviceID;
  1835. (void)ret;
  1836. TEST_SLEEP();
  1837. } /* bench_stats_asym_finish_ex */
  1838. static void bench_stats_asym_finish(const char* algo, int strength,
  1839. const char* desc, int useDeviceID, int count, double start, int ret)
  1840. {
  1841. bench_stats_asym_finish_ex(algo, strength, desc, "", useDeviceID, count,
  1842. start, ret);
  1843. }
  1844. #endif
  1845. #endif /* BENCH_ASYM */
  1846. static WC_INLINE void bench_stats_free(void)
  1847. {
  1848. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  1849. bench_stats_t* bstat;
  1850. for (bstat = bench_stats_head; bstat != NULL; ) {
  1851. bench_stats_t* next = bstat->next;
  1852. XFREE(bstat, NULL, DYNAMIC_TYPE_INFO);
  1853. bstat = next;
  1854. }
  1855. bench_stats_head = NULL;
  1856. bench_stats_tail = NULL;
  1857. #endif
  1858. }
  1859. /*****************************************************************************/
  1860. /* End Stats Functions */
  1861. /*****************************************************************************/
  1862. static void* benchmarks_do(void* args)
  1863. {
  1864. int bench_buf_size;
  1865. #ifdef WOLFSSL_ASYNC_CRYPT
  1866. #ifndef WC_NO_ASYNC_THREADING
  1867. ThreadData* threadData = (ThreadData*)args;
  1868. if (wolfAsync_DevOpenThread(&devId, &threadData->thread_id) < 0)
  1869. #else
  1870. if (wolfAsync_DevOpen(&devId) < 0)
  1871. #endif
  1872. {
  1873. printf("%sAsync device open failed\n%sRunning without async\n",
  1874. err_prefix, err_prefix);
  1875. }
  1876. #endif /* WOLFSSL_ASYNC_CRYPT */
  1877. (void)args;
  1878. #ifdef WOLFSSL_ASYNC_CRYPT
  1879. if (wolfEventQueue_Init(&eventQueue) != 0) {
  1880. printf("%sAsync event queue init failure!\n", err_prefix);
  1881. }
  1882. #endif
  1883. #ifdef WOLF_CRYPTO_CB
  1884. #ifdef HAVE_INTEL_QA_SYNC
  1885. devId = wc_CryptoCb_InitIntelQa();
  1886. if (devId == INVALID_DEVID) {
  1887. printf("%sCouldn't init the Intel QA\n", err_prefix);
  1888. }
  1889. #endif
  1890. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  1891. devId = wc_CryptoCb_InitOcteon();
  1892. if (devId == INVALID_DEVID) {
  1893. printf("%sCouldn't get the Octeon device ID\n", err_prefix);
  1894. }
  1895. #endif
  1896. #ifdef HAVE_RENESAS_SYNC
  1897. devId = wc_CryptoCb_CryptInitRenesasCmn(NULL, &guser_PKCbInfo);
  1898. if (devId == INVALID_DEVID) {
  1899. printf("%sCouldn't get the Renesas device ID\n", err_prefix);
  1900. }
  1901. #endif
  1902. #endif
  1903. #if defined(HAVE_LOCAL_RNG)
  1904. {
  1905. int rngRet;
  1906. #ifndef HAVE_FIPS
  1907. rngRet = wc_InitRng_ex(&gRng, HEAP_HINT, devId);
  1908. #else
  1909. rngRet = wc_InitRng(&gRng);
  1910. #endif
  1911. if (rngRet < 0) {
  1912. printf("%sInitRNG failed\n", err_prefix);
  1913. return NULL;
  1914. }
  1915. }
  1916. #endif
  1917. /* setup bench plain, cipher, key and iv globals */
  1918. /* make sure bench buffer is multiple of 16 (AES block size) */
  1919. bench_buf_size = (int)bench_size + BENCH_CIPHER_ADD;
  1920. if (bench_buf_size % 16)
  1921. bench_buf_size += 16 - (bench_buf_size % 16);
  1922. #ifdef WOLFSSL_AFALG_XILINX_AES
  1923. bench_plain = (byte*)aligned_alloc(64, (size_t)bench_buf_size + 16);
  1924. bench_cipher = (byte*)aligned_alloc(64, (size_t)bench_buf_size + 16);
  1925. #else
  1926. bench_plain = (byte*)XMALLOC((size_t)bench_buf_size + 16,
  1927. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1928. bench_cipher = (byte*)XMALLOC((size_t)bench_buf_size + 16,
  1929. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1930. #endif
  1931. if (bench_plain == NULL || bench_cipher == NULL) {
  1932. XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1933. XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1934. bench_plain = bench_cipher = NULL;
  1935. printf("%sBenchmark block buffer alloc failed!\n", err_prefix);
  1936. goto exit;
  1937. }
  1938. XMEMSET(bench_plain, 0, (size_t)bench_buf_size);
  1939. XMEMSET(bench_cipher, 0, (size_t)bench_buf_size);
  1940. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(HAVE_INTEL_QA_SYNC)
  1941. bench_key = (byte*)XMALLOC(sizeof(bench_key_buf),
  1942. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1943. bench_iv = (byte*)XMALLOC(sizeof(bench_iv_buf),
  1944. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1945. if (bench_key == NULL || bench_iv == NULL) {
  1946. XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1947. XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1948. bench_key = bench_iv = NULL;
  1949. printf("%sBenchmark cipher buffer alloc failed!\n", err_prefix);
  1950. goto exit;
  1951. }
  1952. XMEMCPY(bench_key, bench_key_buf, sizeof(bench_key_buf));
  1953. XMEMCPY(bench_iv, bench_iv_buf, sizeof(bench_iv_buf));
  1954. #else
  1955. bench_key = (byte*)bench_key_buf;
  1956. bench_iv = (byte*)bench_iv_buf;
  1957. #endif
  1958. #ifndef WC_NO_RNG
  1959. if (bench_all || (bench_other_algs & BENCH_RNG))
  1960. bench_rng();
  1961. #endif /* WC_NO_RNG */
  1962. #ifndef NO_AES
  1963. #ifdef HAVE_AES_CBC
  1964. if (bench_all || (bench_cipher_algs & BENCH_AES_CBC)) {
  1965. #ifndef NO_SW_BENCH
  1966. bench_aescbc(0);
  1967. #endif
  1968. #if defined(BENCH_DEVID) || defined(HAVE_RENESAS_SYNC)
  1969. bench_aescbc(1);
  1970. #endif
  1971. }
  1972. #endif
  1973. #ifdef HAVE_AESGCM
  1974. if (bench_all || (bench_cipher_algs & BENCH_AES_GCM)) {
  1975. #ifndef NO_SW_BENCH
  1976. bench_aes_aad_options_wrap(bench_aesgcm, 0);
  1977. #endif
  1978. #if ((defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)) || \
  1979. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  1980. defined(HAVE_RENESAS_SYNC) || defined(WOLFSSL_CAAM)) && \
  1981. !defined(NO_HW_BENCH)
  1982. bench_aes_aad_options_wrap(bench_aesgcm, 1);
  1983. #endif
  1984. bench_gmac();
  1985. }
  1986. #endif
  1987. #ifdef HAVE_AES_ECB
  1988. if (bench_all || (bench_cipher_algs & BENCH_AES_ECB)) {
  1989. #ifndef NO_SW_BENCH
  1990. bench_aesecb(0);
  1991. #endif
  1992. #ifdef BENCH_DEVID
  1993. bench_aesecb(1);
  1994. #endif
  1995. }
  1996. #endif
  1997. #ifdef WOLFSSL_AES_XTS
  1998. if (bench_all || (bench_cipher_algs & BENCH_AES_XTS))
  1999. bench_aesxts();
  2000. #endif
  2001. #ifdef WOLFSSL_AES_CFB
  2002. if (bench_all || (bench_cipher_algs & BENCH_AES_CFB))
  2003. bench_aescfb();
  2004. #endif
  2005. #ifdef WOLFSSL_AES_OFB
  2006. if (bench_all || (bench_cipher_algs & BENCH_AES_OFB))
  2007. bench_aesofb();
  2008. #endif
  2009. #ifdef WOLFSSL_AES_COUNTER
  2010. if (bench_all || (bench_cipher_algs & BENCH_AES_CTR))
  2011. bench_aesctr();
  2012. #endif
  2013. #ifdef HAVE_AESCCM
  2014. if (bench_all || (bench_cipher_algs & BENCH_AES_CCM)) {
  2015. bench_aes_aad_options_wrap(bench_aesccm, 0);
  2016. #ifdef BENCH_DEVID
  2017. bench_aes_aad_options_wrap(bench_aesccm, 1);
  2018. #endif
  2019. }
  2020. #endif
  2021. #ifdef WOLFSSL_AES_SIV
  2022. if (bench_all || (bench_cipher_algs & BENCH_AES_SIV))
  2023. bench_aessiv();
  2024. #endif
  2025. #endif /* !NO_AES */
  2026. #ifdef HAVE_CAMELLIA
  2027. if (bench_all || (bench_cipher_algs & BENCH_CAMELLIA))
  2028. bench_camellia();
  2029. #endif
  2030. #ifndef NO_RC4
  2031. if (bench_all || (bench_cipher_algs & BENCH_ARC4)) {
  2032. #ifndef NO_SW_BENCH
  2033. bench_arc4(0);
  2034. #endif
  2035. #ifdef BENCH_DEVID
  2036. bench_arc4(1);
  2037. #endif
  2038. }
  2039. #endif
  2040. #ifdef HAVE_CHACHA
  2041. if (bench_all || (bench_cipher_algs & BENCH_CHACHA20))
  2042. bench_chacha();
  2043. #endif
  2044. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  2045. if (bench_all || (bench_cipher_algs & BENCH_CHACHA20_POLY1305))
  2046. bench_chacha20_poly1305_aead();
  2047. #endif
  2048. #ifndef NO_DES3
  2049. if (bench_all || (bench_cipher_algs & BENCH_DES)) {
  2050. #ifndef NO_SW_BENCH
  2051. bench_des(0);
  2052. #endif
  2053. #ifdef BENCH_DEVID
  2054. bench_des(1);
  2055. #endif
  2056. }
  2057. #endif
  2058. #ifndef NO_MD5
  2059. if (bench_all || (bench_digest_algs & BENCH_MD5)) {
  2060. #ifndef NO_SW_BENCH
  2061. bench_md5(0);
  2062. #endif
  2063. #ifdef BENCH_DEVID
  2064. bench_md5(1);
  2065. #endif
  2066. }
  2067. #endif
  2068. #ifdef HAVE_POLY1305
  2069. if (bench_all || (bench_digest_algs & BENCH_POLY1305))
  2070. bench_poly1305();
  2071. #endif
  2072. #ifndef NO_SHA
  2073. if (bench_all || (bench_digest_algs & BENCH_SHA)) {
  2074. #ifndef NO_SW_BENCH
  2075. bench_sha(0);
  2076. #endif
  2077. #ifdef BENCH_DEVID
  2078. bench_sha(1);
  2079. #endif
  2080. }
  2081. #endif
  2082. #ifdef WOLFSSL_SHA224
  2083. if (bench_all || (bench_digest_algs & BENCH_SHA224)) {
  2084. #ifndef NO_SW_BENCH
  2085. bench_sha224(0);
  2086. #endif
  2087. #ifdef BENCH_DEVID
  2088. bench_sha224(1);
  2089. #endif
  2090. }
  2091. #endif
  2092. #ifndef NO_SHA256
  2093. if (bench_all || (bench_digest_algs & BENCH_SHA256)) {
  2094. #ifndef NO_SW_BENCH
  2095. bench_sha256(0);
  2096. #endif
  2097. #ifdef BENCH_DEVID
  2098. bench_sha256(1);
  2099. #endif
  2100. }
  2101. #endif
  2102. #ifdef WOLFSSL_SHA384
  2103. if (bench_all || (bench_digest_algs & BENCH_SHA384)) {
  2104. #ifndef NO_SW_BENCH
  2105. bench_sha384(0);
  2106. #endif
  2107. #ifdef BENCH_DEVID
  2108. bench_sha384(1);
  2109. #endif
  2110. }
  2111. #endif
  2112. #ifdef WOLFSSL_SHA512
  2113. if (bench_all || (bench_digest_algs & BENCH_SHA512)) {
  2114. #ifndef NO_SW_BENCH
  2115. bench_sha512(0);
  2116. #endif
  2117. #ifdef BENCH_DEVID
  2118. bench_sha512(1);
  2119. #endif
  2120. }
  2121. #endif
  2122. #ifdef WOLFSSL_SHA3
  2123. #ifndef WOLFSSL_NOSHA3_224
  2124. if (bench_all || (bench_digest_algs & BENCH_SHA3_224)) {
  2125. #ifndef NO_SW_BENCH
  2126. bench_sha3_224(0);
  2127. #endif
  2128. #ifdef BENCH_DEVID
  2129. bench_sha3_224(1);
  2130. #endif
  2131. }
  2132. #endif /* WOLFSSL_NOSHA3_224 */
  2133. #ifndef WOLFSSL_NOSHA3_256
  2134. if (bench_all || (bench_digest_algs & BENCH_SHA3_256)) {
  2135. #ifndef NO_SW_BENCH
  2136. bench_sha3_256(0);
  2137. #endif
  2138. #ifdef BENCH_DEVID
  2139. bench_sha3_256(1);
  2140. #endif
  2141. }
  2142. #endif /* WOLFSSL_NOSHA3_256 */
  2143. #ifndef WOLFSSL_NOSHA3_384
  2144. if (bench_all || (bench_digest_algs & BENCH_SHA3_384)) {
  2145. #ifndef NO_SW_BENCH
  2146. bench_sha3_384(0);
  2147. #endif
  2148. #ifdef BENCH_DEVID
  2149. bench_sha3_384(1);
  2150. #endif
  2151. }
  2152. #endif /* WOLFSSL_NOSHA3_384 */
  2153. #ifndef WOLFSSL_NOSHA3_512
  2154. if (bench_all || (bench_digest_algs & BENCH_SHA3_512)) {
  2155. #ifndef NO_SW_BENCH
  2156. bench_sha3_512(0);
  2157. #endif
  2158. #ifdef BENCH_DEVID
  2159. bench_sha3_512(1);
  2160. #endif
  2161. }
  2162. #endif /* WOLFSSL_NOSHA3_512 */
  2163. #ifdef WOLFSSL_SHAKE128
  2164. if (bench_all || (bench_digest_algs & BENCH_SHAKE128)) {
  2165. #ifndef NO_SW_BENCH
  2166. bench_shake128(0);
  2167. #endif
  2168. #ifdef BENCH_DEVID
  2169. bench_shake128(1);
  2170. #endif
  2171. }
  2172. #endif /* WOLFSSL_SHAKE128 */
  2173. #ifdef WOLFSSL_SHAKE256
  2174. if (bench_all || (bench_digest_algs & BENCH_SHAKE256)) {
  2175. #ifndef NO_SW_BENCH
  2176. bench_shake256(0);
  2177. #endif
  2178. #ifdef BENCH_DEVID
  2179. bench_shake256(1);
  2180. #endif
  2181. }
  2182. #endif /* WOLFSSL_SHAKE256 */
  2183. #endif
  2184. #ifdef WOLFSSL_RIPEMD
  2185. if (bench_all || (bench_digest_algs & BENCH_RIPEMD))
  2186. bench_ripemd();
  2187. #endif
  2188. #ifdef HAVE_BLAKE2
  2189. if (bench_all || (bench_digest_algs & BENCH_BLAKE2B))
  2190. bench_blake2b();
  2191. #endif
  2192. #ifdef HAVE_BLAKE2S
  2193. if (bench_all || (bench_digest_algs & BENCH_BLAKE2S))
  2194. bench_blake2s();
  2195. #endif
  2196. #ifdef WOLFSSL_CMAC
  2197. if (bench_all || (bench_mac_algs & BENCH_CMAC)) {
  2198. bench_cmac(0);
  2199. #ifdef BENCH_DEVID
  2200. bench_cmac(1);
  2201. #endif
  2202. }
  2203. #endif
  2204. #ifndef NO_HMAC
  2205. #ifndef NO_MD5
  2206. if (bench_all || (bench_mac_algs & BENCH_HMAC_MD5)) {
  2207. #ifndef NO_SW_BENCH
  2208. bench_hmac_md5(0);
  2209. #endif
  2210. #ifdef BENCH_DEVID
  2211. bench_hmac_md5(1);
  2212. #endif
  2213. }
  2214. #endif
  2215. #ifndef NO_SHA
  2216. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA)) {
  2217. #ifndef NO_SW_BENCH
  2218. bench_hmac_sha(0);
  2219. #endif
  2220. #ifdef BENCH_DEVID
  2221. bench_hmac_sha(1);
  2222. #endif
  2223. }
  2224. #endif
  2225. #ifdef WOLFSSL_SHA224
  2226. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA224)) {
  2227. #ifndef NO_SW_BENCH
  2228. bench_hmac_sha224(0);
  2229. #endif
  2230. #ifdef BENCH_DEVID
  2231. bench_hmac_sha224(1);
  2232. #endif
  2233. }
  2234. #endif
  2235. #ifndef NO_SHA256
  2236. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA256)) {
  2237. #ifndef NO_SW_BENCH
  2238. bench_hmac_sha256(0);
  2239. #endif
  2240. #ifdef BENCH_DEVID
  2241. bench_hmac_sha256(1);
  2242. #endif
  2243. }
  2244. #endif
  2245. #ifdef WOLFSSL_SHA384
  2246. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA384)) {
  2247. #ifndef NO_SW_BENCH
  2248. bench_hmac_sha384(0);
  2249. #endif
  2250. #ifdef BENCH_DEVID
  2251. bench_hmac_sha384(1);
  2252. #endif
  2253. }
  2254. #endif
  2255. #ifdef WOLFSSL_SHA512
  2256. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA512)) {
  2257. #ifndef NO_SW_BENCH
  2258. bench_hmac_sha512(0);
  2259. #endif
  2260. #ifdef BENCH_DEVID
  2261. bench_hmac_sha512(1);
  2262. #endif
  2263. }
  2264. #endif
  2265. #ifndef NO_PWDBASED
  2266. if (bench_all || (bench_mac_algs & BENCH_PBKDF2)) {
  2267. bench_pbkdf2();
  2268. }
  2269. #endif
  2270. #ifdef WOLFSSL_SIPHASH
  2271. if (bench_all || (bench_mac_algs & BENCH_SIPHASH)) {
  2272. bench_siphash();
  2273. }
  2274. #endif
  2275. #endif /* NO_HMAC */
  2276. #ifdef HAVE_SCRYPT
  2277. if (bench_all || (bench_other_algs & BENCH_SCRYPT))
  2278. bench_scrypt();
  2279. #endif
  2280. #ifndef NO_RSA
  2281. #ifdef WOLFSSL_KEY_GEN
  2282. if (bench_all || (bench_asym_algs & BENCH_RSA_KEYGEN)) {
  2283. #ifndef NO_SW_BENCH
  2284. if (((word32)bench_asym_algs == 0xFFFFFFFFU) ||
  2285. (bench_asym_algs & BENCH_RSA_SZ) == 0) {
  2286. bench_rsaKeyGen(0);
  2287. }
  2288. else {
  2289. bench_rsaKeyGen_size(0, bench_size);
  2290. }
  2291. #endif
  2292. #ifdef BENCH_DEVID
  2293. if (bench_asym_algs & BENCH_RSA_SZ) {
  2294. bench_rsaKeyGen_size(1, bench_size);
  2295. }
  2296. else {
  2297. bench_rsaKeyGen(1);
  2298. }
  2299. #endif
  2300. }
  2301. #endif
  2302. if (bench_all || (bench_asym_algs & BENCH_RSA)) {
  2303. #ifndef NO_SW_BENCH
  2304. bench_rsa(0);
  2305. #endif
  2306. #ifdef BENCH_DEVID
  2307. bench_rsa(1);
  2308. #endif
  2309. }
  2310. #ifdef WOLFSSL_KEY_GEN
  2311. if (bench_asym_algs & BENCH_RSA_SZ) {
  2312. #ifndef NO_SW_BENCH
  2313. bench_rsa_key(0, bench_size);
  2314. #endif
  2315. #ifdef BENCH_DEVID
  2316. bench_rsa_key(1, bench_size);
  2317. #endif
  2318. }
  2319. #endif
  2320. #endif
  2321. #ifndef NO_DH
  2322. if (bench_all || (bench_asym_algs & BENCH_DH)) {
  2323. #ifndef NO_SW_BENCH
  2324. bench_dh(0);
  2325. #endif
  2326. #ifdef BENCH_DEVID
  2327. bench_dh(1);
  2328. #endif
  2329. }
  2330. #endif
  2331. #ifdef WOLFSSL_HAVE_KYBER
  2332. if (bench_all || (bench_asym_algs & BENCH_KYBER)) {
  2333. #ifdef WOLFSSL_KYBER512
  2334. bench_kyber(KYBER512);
  2335. #endif
  2336. #ifdef WOLFSSL_KYBER768
  2337. bench_kyber(KYBER768);
  2338. #endif
  2339. #ifdef WOLFSSL_KYBER1024
  2340. bench_kyber(KYBER1024);
  2341. #endif
  2342. }
  2343. #endif
  2344. #ifdef HAVE_ECC
  2345. if (bench_all || (bench_asym_algs & BENCH_ECC_MAKEKEY) ||
  2346. (bench_asym_algs & BENCH_ECC) ||
  2347. (bench_asym_algs & BENCH_ECC_ALL) ||
  2348. (bench_asym_algs & BENCH_ECC_ENCRYPT)) {
  2349. if (bench_asym_algs & BENCH_ECC_ALL) {
  2350. #if defined(HAVE_FIPS) || defined(HAVE_SELFTEST)
  2351. printf("%snot supported in FIPS mode (no ending enum value)\n",
  2352. err_prefix);
  2353. #else
  2354. int curveId = (int)ECC_SECP192R1;
  2355. /* set make key and encrypt */
  2356. bench_asym_algs |= BENCH_ECC_MAKEKEY | BENCH_ECC |
  2357. BENCH_ECC_ENCRYPT;
  2358. if (csv_format != 1) {
  2359. printf("\n%sECC Benchmarks:\n", info_prefix);
  2360. }
  2361. do {
  2362. #ifdef WOLFCRYPT_HAVE_SAKKE
  2363. /* SAKKE is not useable with ECDH/ECDSA. Run separate test. */
  2364. if (curveId == ECC_SAKKE_1) {
  2365. curveId++;
  2366. continue;
  2367. }
  2368. #endif
  2369. if (wc_ecc_get_curve_size_from_id(curveId) !=
  2370. ECC_BAD_ARG_E) {
  2371. bench_ecc_curve(curveId);
  2372. if (csv_format != 1) {
  2373. printf("\n");
  2374. }
  2375. }
  2376. curveId++;
  2377. } while (curveId != (int)ECC_CURVE_MAX);
  2378. #endif
  2379. }
  2380. else if (bench_asym_algs & BENCH_ECC_P256) {
  2381. bench_ecc_curve((int)ECC_SECP256R1);
  2382. }
  2383. else if (bench_asym_algs & BENCH_ECC_P384) {
  2384. bench_ecc_curve((int)ECC_SECP384R1);
  2385. }
  2386. else if (bench_asym_algs & BENCH_ECC_P521) {
  2387. bench_ecc_curve((int)ECC_SECP521R1);
  2388. }
  2389. else {
  2390. #ifndef NO_ECC256
  2391. bench_ecc_curve((int)ECC_SECP256R1);
  2392. #elif defined(HAVE_ECC384)
  2393. bench_ecc_curve((int)ECC_SECP384R1);
  2394. #elif defined(HAVE_ECC521)
  2395. bench_ecc_curve((int)ECC_SECP521R1);
  2396. #endif
  2397. #ifdef HAVE_ECC_BRAINPOOL
  2398. bench_ecc_curve((int)ECC_BRAINPOOLP256R1);
  2399. #endif
  2400. }
  2401. }
  2402. #endif
  2403. #ifdef HAVE_CURVE25519
  2404. if (bench_all || (bench_asym_algs & BENCH_CURVE25519_KEYGEN)) {
  2405. bench_curve25519KeyGen(0);
  2406. #ifdef BENCH_DEVID
  2407. bench_curve25519KeyGen(1);
  2408. #endif
  2409. }
  2410. #ifdef HAVE_CURVE25519_SHARED_SECRET
  2411. if (bench_all || (bench_asym_algs & BENCH_CURVE25519_KA)) {
  2412. bench_curve25519KeyAgree(0);
  2413. #ifdef BENCH_DEVID
  2414. bench_curve25519KeyAgree(1);
  2415. #endif
  2416. }
  2417. #endif
  2418. #endif
  2419. #ifdef HAVE_ED25519
  2420. if (bench_all || (bench_asym_algs & BENCH_ED25519_KEYGEN))
  2421. bench_ed25519KeyGen();
  2422. if (bench_all || (bench_asym_algs & BENCH_ED25519_SIGN))
  2423. bench_ed25519KeySign();
  2424. #endif
  2425. #ifdef HAVE_CURVE448
  2426. if (bench_all || (bench_asym_algs & BENCH_CURVE448_KEYGEN))
  2427. bench_curve448KeyGen();
  2428. #ifdef HAVE_CURVE448_SHARED_SECRET
  2429. if (bench_all || (bench_asym_algs & BENCH_CURVE448_KA))
  2430. bench_curve448KeyAgree();
  2431. #endif
  2432. #endif
  2433. #ifdef HAVE_ED448
  2434. if (bench_all || (bench_asym_algs & BENCH_ED448_KEYGEN))
  2435. bench_ed448KeyGen();
  2436. if (bench_all || (bench_asym_algs & BENCH_ED448_SIGN))
  2437. bench_ed448KeySign();
  2438. #endif
  2439. #ifdef WOLFCRYPT_HAVE_ECCSI
  2440. #ifdef WOLFCRYPT_ECCSI_KMS
  2441. if (bench_all || (bench_asym_algs & BENCH_ECCSI_KEYGEN)) {
  2442. bench_eccsiKeyGen();
  2443. }
  2444. if (bench_all || (bench_asym_algs & BENCH_ECCSI_PAIRGEN)) {
  2445. bench_eccsiPairGen();
  2446. }
  2447. #endif
  2448. #ifdef WOLFCRYPT_ECCSI_CLIENT
  2449. if (bench_all || (bench_asym_algs & BENCH_ECCSI_VALIDATE)) {
  2450. bench_eccsiValidate();
  2451. }
  2452. if (bench_all || (bench_asym_algs & BENCH_ECCSI)) {
  2453. bench_eccsi();
  2454. }
  2455. #endif
  2456. #endif
  2457. #ifdef WOLFCRYPT_HAVE_SAKKE
  2458. #ifdef WOLFCRYPT_SAKKE_KMS
  2459. if (bench_all || (bench_asym_algs & BENCH_SAKKE_KEYGEN)) {
  2460. bench_sakkeKeyGen();
  2461. }
  2462. if (bench_all || (bench_asym_algs & BENCH_SAKKE_RSKGEN)) {
  2463. bench_sakkeRskGen();
  2464. }
  2465. #endif
  2466. #ifdef WOLFCRYPT_SAKKE_CLIENT
  2467. if (bench_all || (bench_asym_algs & BENCH_SAKKE_VALIDATE)) {
  2468. bench_sakkeValidate();
  2469. }
  2470. if (bench_all || (bench_asym_algs & BENCH_SAKKE)) {
  2471. bench_sakke();
  2472. }
  2473. #endif
  2474. #endif
  2475. #if defined(HAVE_LIBOQS)
  2476. #ifdef HAVE_FALCON
  2477. if (bench_all || (bench_pq_asym_algs & BENCH_FALCON_LEVEL1_SIGN))
  2478. bench_falconKeySign(1);
  2479. if (bench_all || (bench_pq_asym_algs & BENCH_FALCON_LEVEL5_SIGN))
  2480. bench_falconKeySign(5);
  2481. #endif
  2482. #ifdef HAVE_DILITHIUM
  2483. if (bench_all || (bench_pq_asym_algs & BENCH_DILITHIUM_LEVEL2_SIGN))
  2484. bench_dilithiumKeySign(2);
  2485. if (bench_all || (bench_pq_asym_algs & BENCH_DILITHIUM_LEVEL3_SIGN))
  2486. bench_dilithiumKeySign(3);
  2487. if (bench_all || (bench_pq_asym_algs & BENCH_DILITHIUM_LEVEL5_SIGN))
  2488. bench_dilithiumKeySign(5);
  2489. #endif
  2490. #ifdef HAVE_SPHINCS
  2491. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_FAST_LEVEL1_SIGN))
  2492. bench_sphincsKeySign(1, FAST_VARIANT);
  2493. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_FAST_LEVEL3_SIGN))
  2494. bench_sphincsKeySign(3, FAST_VARIANT);
  2495. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_FAST_LEVEL5_SIGN))
  2496. bench_sphincsKeySign(5, FAST_VARIANT);
  2497. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_SMALL_LEVEL1_SIGN))
  2498. bench_sphincsKeySign(1, SMALL_VARIANT);
  2499. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_SMALL_LEVEL3_SIGN))
  2500. bench_sphincsKeySign(3, SMALL_VARIANT);
  2501. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_SMALL_LEVEL5_SIGN))
  2502. bench_sphincsKeySign(5, SMALL_VARIANT);
  2503. #endif
  2504. #endif /* HAVE_LIBOQS */
  2505. exit:
  2506. /* free benchmark buffers */
  2507. XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2508. XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2509. #ifdef WOLFSSL_ASYNC_CRYPT
  2510. XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2511. XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2512. #endif
  2513. #if defined(HAVE_LOCAL_RNG)
  2514. wc_FreeRng(&gRng);
  2515. #endif
  2516. /* cleanup the thread if fixed point cache is enabled and have thread local */
  2517. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  2518. wc_ecc_fp_free();
  2519. #endif
  2520. (void)bench_cipher_algs;
  2521. (void)bench_digest_algs;
  2522. (void)bench_mac_algs;
  2523. (void)bench_asym_algs;
  2524. (void)bench_other_algs;
  2525. (void)bench_pq_asym_algs;
  2526. (void)bench_pq_asym_algs2;
  2527. return NULL;
  2528. }
  2529. int benchmark_init(void)
  2530. {
  2531. int ret = 0;
  2532. benchmark_static_init(0);
  2533. #ifdef WOLFSSL_STATIC_MEMORY
  2534. ret = wc_LoadStaticMemory(&HEAP_HINT, gBenchMemory,
  2535. sizeof(gBenchMemory), WOLFMEM_GENERAL, 1);
  2536. if (ret != 0) {
  2537. printf("%sunable to load static memory %d\n", err_prefix, ret);
  2538. }
  2539. #endif /* WOLFSSL_STATIC_MEMORY */
  2540. if ((ret = wolfCrypt_Init()) != 0) {
  2541. printf("%swolfCrypt_Init failed %d\n", err_prefix, ret);
  2542. return EXIT_FAILURE;
  2543. }
  2544. #ifdef WOLFSSL_SECO_CAAM
  2545. if (wc_SECO_OpenHSM(SECO_KEY_STORE_ID,
  2546. SECO_BENCHMARK_NONCE, SECO_MAX_UPDATES, CAAM_KEYSTORE_CREATE)
  2547. != 0) {
  2548. printf("%sunable to open HSM\n", err_prefix);
  2549. wolfCrypt_Cleanup();
  2550. return EXIT_FAILURE;
  2551. }
  2552. #endif
  2553. #ifdef WC_RNG_SEED_CB
  2554. wc_SetSeed_Cb(wc_GenerateSeed);
  2555. #endif
  2556. bench_stats_init();
  2557. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  2558. wolfSSL_Debugging_ON();
  2559. #endif
  2560. printf("%swolfCrypt Benchmark (block bytes %d, min %.1f sec each)\n",
  2561. info_prefix, (int)bench_size, BENCH_MIN_RUNTIME_SEC);
  2562. #ifndef GENERATE_MACHINE_PARSEABLE_REPORT
  2563. if (csv_format == 1) {
  2564. printf("This format allows you to easily copy "
  2565. "the output to a csv file.");
  2566. }
  2567. #endif
  2568. #ifdef HAVE_WNR
  2569. ret = wc_InitNetRandom(wnrConfigFile, NULL, 5000);
  2570. if (ret != 0) {
  2571. printf("%sWhitewood netRandom config init failed %d\n",
  2572. err_prefix, ret);
  2573. }
  2574. #endif /* HAVE_WNR */
  2575. return ret;
  2576. }
  2577. int benchmark_free(void)
  2578. {
  2579. int ret;
  2580. #ifdef WC_ENABLE_BENCH_THREADING
  2581. if (gPrintStats || devId != INVALID_DEVID) {
  2582. bench_stats_print();
  2583. }
  2584. #endif
  2585. bench_stats_free();
  2586. #ifdef WOLF_CRYPTO_CB
  2587. #ifdef HAVE_INTEL_QA_SYNC
  2588. wc_CryptoCb_CleanupIntelQa(&devId);
  2589. #endif
  2590. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  2591. wc_CryptoCb_CleanupOcteon(&devId);
  2592. #endif
  2593. #ifdef HAVE_RENESAS_SYNC
  2594. wc_CryptoCb_CleanupRenesasCmn(&devId);
  2595. #endif
  2596. #endif
  2597. #ifdef WOLFSSL_ASYNC_CRYPT
  2598. /* free event queue */
  2599. wolfEventQueue_Free(&eventQueue);
  2600. /* close device */
  2601. wolfAsync_DevClose(&devId);
  2602. #endif
  2603. #ifdef HAVE_WNR
  2604. ret = wc_FreeNetRandom();
  2605. if (ret < 0) {
  2606. printf("%sFailed to free netRandom context %d\n", err_prefix, ret);
  2607. }
  2608. #endif
  2609. #ifdef WOLFSSL_SECO_CAAM
  2610. if (wc_SECO_CloseHSM() != 0) {
  2611. printf("%sError closing down the key store\n", err_prefix);
  2612. }
  2613. #endif
  2614. if ((ret = wolfCrypt_Cleanup()) != 0) {
  2615. printf("%serror %d with wolfCrypt_Cleanup\n", err_prefix, ret);
  2616. }
  2617. return ret;
  2618. }
  2619. #if defined(WC_ENABLE_BENCH_THREADING) && !defined(WOLFSSL_ASYNC_CRYPT)
  2620. static THREAD_RETURN WOLFSSL_THREAD run_bench(void* args)
  2621. {
  2622. benchmark_test(args);
  2623. EXIT_TEST(0);
  2624. }
  2625. static int benchmark_test_threaded(void* args)
  2626. {
  2627. int i;
  2628. printf("%sThreads: %d\n", info_prefix, g_threadCount);
  2629. g_threadData = (ThreadData*)XMALLOC(sizeof(ThreadData) * g_threadCount,
  2630. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2631. if (g_threadData == NULL) {
  2632. printf("%sThread data alloc failed!\n", err_prefix);
  2633. return EXIT_FAILURE;
  2634. }
  2635. for (i = 0; i < g_threadCount; i++) {
  2636. PTHREAD_CHECK_RET(pthread_create(&g_threadData[i].thread_id,
  2637. NULL, run_bench, args));
  2638. }
  2639. for (i = 0; i < g_threadCount; i++) {
  2640. PTHREAD_CHECK_RET(pthread_join(g_threadData[i].thread_id, 0));
  2641. }
  2642. printf("\n");
  2643. bench_stats_print();
  2644. return 0;
  2645. }
  2646. #endif
  2647. /* so embedded projects can pull in tests on their own */
  2648. #ifdef HAVE_STACK_SIZE
  2649. THREAD_RETURN WOLFSSL_THREAD benchmark_test(void* args)
  2650. #else
  2651. int benchmark_test(void *args)
  2652. #endif
  2653. {
  2654. int ret;
  2655. (void)args;
  2656. #ifdef HAVE_FIPS
  2657. wolfCrypt_SetCb_fips(myFipsCb);
  2658. #endif
  2659. ret = benchmark_init();
  2660. if (ret != 0)
  2661. EXIT_TEST(ret);
  2662. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  2663. {
  2664. /* See the documentation when turning on WOLFSSL_ASYNC_CRYPT
  2665. **
  2666. ** Chapter Two, Build Options:
  2667. **
  2668. ** https://www.wolfssl.com/documentation/manuals/wolfssl/wolfSSL-Manual.pdf
  2669. **
  2670. ** asynchronous cryptography using hardware based adapters such as
  2671. ** the Intel QuickAssist or Marvell (Cavium) Nitrox V.
  2672. */
  2673. int i;
  2674. if (g_threadCount == 0) {
  2675. #ifdef WC_ASYNC_BENCH_THREAD_COUNT
  2676. g_threadCount = WC_ASYNC_BENCH_THREAD_COUNT;
  2677. #else
  2678. g_threadCount = wc_AsyncGetNumberOfCpus();
  2679. if (g_threadCount > 0) {
  2680. g_threadCount /= 2; /* use physical core count */
  2681. }
  2682. #endif
  2683. }
  2684. if (g_threadCount <= 0) {
  2685. g_threadCount = 1;
  2686. }
  2687. printf("%sCPUs: %d\n", info_prefix, g_threadCount);
  2688. g_threadData = (ThreadData*)XMALLOC(sizeof(ThreadData) * g_threadCount,
  2689. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2690. if (g_threadData == NULL) {
  2691. printf("%sThread data alloc failed!\n", err_prefix);
  2692. EXIT_TEST(EXIT_FAILURE);
  2693. }
  2694. /* Create threads */
  2695. for (i = 0; i < g_threadCount; i++) {
  2696. ret = wc_AsyncThreadCreate(&g_threadData[i].thread_id,
  2697. benchmarks_do, &g_threadData[i]);
  2698. if (ret != 0) {
  2699. printf("%sError creating benchmark thread %d\n", err_prefix, ret);
  2700. EXIT_TEST(EXIT_FAILURE);
  2701. }
  2702. }
  2703. /* Start threads */
  2704. for (i = 0; i < g_threadCount; i++) {
  2705. wc_AsyncThreadJoin(&g_threadData[i].thread_id);
  2706. }
  2707. XFREE(g_threadData, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2708. }
  2709. #else
  2710. benchmarks_do(NULL);
  2711. #endif
  2712. SLEEP_ON_ERROR(1);
  2713. printf("%sBenchmark complete\n", info_prefix);
  2714. ret = benchmark_free();
  2715. EXIT_TEST(ret);
  2716. }
  2717. #ifndef WC_NO_RNG
  2718. void bench_rng(void)
  2719. {
  2720. int ret, i, count;
  2721. double start;
  2722. long pos, len, remain;
  2723. WC_RNG myrng;
  2724. #ifndef HAVE_FIPS
  2725. ret = wc_InitRng_ex(&myrng, HEAP_HINT, devId);
  2726. #else
  2727. ret = wc_InitRng(&myrng);
  2728. #endif
  2729. if (ret < 0) {
  2730. printf("InitRNG failed %d\n", ret);
  2731. return;
  2732. }
  2733. bench_stats_start(&count, &start);
  2734. do {
  2735. for (i = 0; i < numBlocks; i++) {
  2736. /* Split request to handle large RNG request */
  2737. pos = 0;
  2738. remain = (int)bench_size;
  2739. while (remain > 0) {
  2740. len = remain;
  2741. if (len > RNG_MAX_BLOCK_LEN)
  2742. len = RNG_MAX_BLOCK_LEN;
  2743. ret = wc_RNG_GenerateBlock(&myrng, &bench_plain[pos],
  2744. (word32)len);
  2745. if (ret < 0)
  2746. goto exit_rng;
  2747. remain -= len;
  2748. pos += len;
  2749. }
  2750. }
  2751. count += i;
  2752. } while (bench_stats_check(start));
  2753. exit_rng:
  2754. bench_stats_sym_finish("RNG", 0, count, bench_size, start, ret);
  2755. wc_FreeRng(&myrng);
  2756. }
  2757. #endif /* WC_NO_RNG */
  2758. #ifndef NO_AES
  2759. #ifdef HAVE_AES_CBC
  2760. static void bench_aescbc_internal(int useDeviceID,
  2761. const byte* key, word32 keySz,
  2762. const byte* iv, const char* encLabel,
  2763. const char* decLabel)
  2764. {
  2765. int ret = 0, i, count = 0, times, pending = 0;
  2766. Aes enc[BENCH_MAX_PENDING];
  2767. double start;
  2768. /* clear for done cleanup */
  2769. XMEMSET(enc, 0, sizeof(enc));
  2770. /* init keys */
  2771. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2772. if ((ret = wc_AesInit(&enc[i], HEAP_HINT,
  2773. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  2774. printf("AesInit failed, ret = %d\n", ret);
  2775. goto exit;
  2776. }
  2777. ret = wc_AesSetKey(&enc[i], key, keySz, iv, AES_ENCRYPTION);
  2778. if (ret != 0) {
  2779. printf("AesSetKey failed, ret = %d\n", ret);
  2780. goto exit;
  2781. }
  2782. }
  2783. bench_stats_start(&count, &start);
  2784. do {
  2785. for (times = 0; times < numBlocks || pending > 0; ) {
  2786. bench_async_poll(&pending);
  2787. /* while free pending slots in queue, submit ops */
  2788. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2789. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0,
  2790. &times, numBlocks, &pending)) {
  2791. ret = wc_AesCbcEncrypt(&enc[i], bench_plain, bench_cipher,
  2792. bench_size);
  2793. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]),
  2794. 0, &times, &pending)) {
  2795. goto exit_aes_enc;
  2796. }
  2797. }
  2798. } /* for i */
  2799. } /* for times */
  2800. count += times;
  2801. } while (bench_stats_check(start));
  2802. exit_aes_enc:
  2803. bench_stats_sym_finish(encLabel, useDeviceID, count,
  2804. bench_size, start, ret);
  2805. if (ret < 0) {
  2806. goto exit;
  2807. }
  2808. #ifdef HAVE_AES_DECRYPT
  2809. /* init keys */
  2810. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2811. ret = wc_AesSetKey(&enc[i], key, keySz, iv, AES_DECRYPTION);
  2812. if (ret != 0) {
  2813. printf("AesSetKey failed, ret = %d\n", ret);
  2814. goto exit;
  2815. }
  2816. }
  2817. bench_stats_start(&count, &start);
  2818. do {
  2819. for (times = 0; times < numBlocks || pending > 0; ) {
  2820. bench_async_poll(&pending);
  2821. /* while free pending slots in queue, submit ops */
  2822. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2823. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0,
  2824. &times, numBlocks, &pending)) {
  2825. ret = wc_AesCbcDecrypt(&enc[i], bench_cipher, bench_plain,
  2826. bench_size);
  2827. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]),
  2828. 0, &times, &pending)) {
  2829. goto exit_aes_dec;
  2830. }
  2831. }
  2832. } /* for i */
  2833. } /* for times */
  2834. count += times;
  2835. } while (bench_stats_check(start));
  2836. exit_aes_dec:
  2837. bench_stats_sym_finish(decLabel, useDeviceID, count, bench_size,
  2838. start, ret);
  2839. #endif /* HAVE_AES_DECRYPT */
  2840. (void)decLabel;
  2841. exit:
  2842. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2843. wc_AesFree(&enc[i]);
  2844. }
  2845. }
  2846. void bench_aescbc(int useDeviceID)
  2847. {
  2848. #ifdef WOLFSSL_AES_128
  2849. bench_aescbc_internal(useDeviceID, bench_key, 16, bench_iv,
  2850. "AES-128-CBC-enc", "AES-128-CBC-dec");
  2851. #endif
  2852. #ifdef WOLFSSL_AES_192
  2853. bench_aescbc_internal(useDeviceID, bench_key, 24, bench_iv,
  2854. "AES-192-CBC-enc", "AES-192-CBC-dec");
  2855. #endif
  2856. #ifdef WOLFSSL_AES_256
  2857. bench_aescbc_internal(useDeviceID, bench_key, 32, bench_iv,
  2858. "AES-256-CBC-enc", "AES-256-CBC-dec");
  2859. #endif
  2860. }
  2861. #endif /* HAVE_AES_CBC */
  2862. #ifdef HAVE_AESGCM
  2863. static void bench_aesgcm_internal(int useDeviceID,
  2864. const byte* key, word32 keySz,
  2865. const byte* iv, word32 ivSz,
  2866. const char* encLabel, const char* decLabel)
  2867. {
  2868. int ret = 0, i, count = 0, times, pending = 0;
  2869. Aes enc[BENCH_MAX_PENDING];
  2870. #ifdef HAVE_AES_DECRYPT
  2871. Aes dec[BENCH_MAX_PENDING];
  2872. #endif
  2873. double start;
  2874. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  2875. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  2876. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  2877. if (bench_additional == NULL || bench_tag == NULL) {
  2878. printf("bench_aesgcm_internal malloc failed\n");
  2879. goto exit;
  2880. }
  2881. #endif
  2882. /* clear for done cleanup */
  2883. XMEMSET(enc, 0, sizeof(enc));
  2884. #ifdef HAVE_AES_DECRYPT
  2885. XMEMSET(dec, 0, sizeof(dec));
  2886. #endif
  2887. #ifdef WOLFSSL_ASYNC_CRYPT
  2888. if (bench_additional)
  2889. #endif
  2890. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  2891. #ifdef WOLFSSL_ASYNC_CRYPT
  2892. if (bench_tag)
  2893. #endif
  2894. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  2895. /* init keys */
  2896. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2897. if ((ret = wc_AesInit(&enc[i], HEAP_HINT,
  2898. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  2899. printf("AesInit failed, ret = %d\n", ret);
  2900. goto exit;
  2901. }
  2902. ret = wc_AesGcmSetKey(&enc[i], key, keySz);
  2903. if (ret != 0) {
  2904. printf("AesGcmSetKey failed, ret = %d\n", ret);
  2905. goto exit;
  2906. }
  2907. }
  2908. /* GCM uses same routine in backend for both encrypt and decrypt */
  2909. bench_stats_start(&count, &start);
  2910. do {
  2911. for (times = 0; times < numBlocks || pending > 0; ) {
  2912. bench_async_poll(&pending);
  2913. /* while free pending slots in queue, submit ops */
  2914. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2915. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0,
  2916. &times, numBlocks, &pending)) {
  2917. #ifndef BENCHMARK_AESGCM_STREAM
  2918. ret = wc_AesGcmEncrypt(&enc[i], bench_cipher,
  2919. bench_plain, bench_size,
  2920. iv, ivSz, bench_tag, AES_AUTH_TAG_SZ,
  2921. bench_additional, aesAuthAddSz);
  2922. #else
  2923. ret = wc_AesGcmEncryptInit(&enc[i], NULL, 0, iv, ivSz);
  2924. if (ret == 0) {
  2925. ret = wc_AesGcmEncryptUpdate(&enc[i], bench_cipher,
  2926. bench_plain, bench_size, bench_additional,
  2927. aesAuthAddSz);
  2928. }
  2929. if (ret == 0) {
  2930. ret = wc_AesGcmEncryptFinal(&enc[i], bench_tag,
  2931. AES_AUTH_TAG_SZ);
  2932. }
  2933. #endif
  2934. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]),
  2935. 0, &times, &pending)) {
  2936. goto exit_aes_gcm;
  2937. }
  2938. }
  2939. } /* for i */
  2940. } /* for times */
  2941. count += times;
  2942. } while (bench_stats_check(start));
  2943. exit_aes_gcm:
  2944. bench_stats_sym_finish(encLabel, useDeviceID, count, bench_size,
  2945. start, ret);
  2946. #ifdef HAVE_AES_DECRYPT
  2947. /* init keys */
  2948. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2949. if ((ret = wc_AesInit(&dec[i], HEAP_HINT,
  2950. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  2951. printf("AesInit failed, ret = %d\n", ret);
  2952. goto exit;
  2953. }
  2954. ret = wc_AesGcmSetKey(&dec[i], key, keySz);
  2955. if (ret != 0) {
  2956. printf("AesGcmSetKey failed, ret = %d\n", ret);
  2957. goto exit;
  2958. }
  2959. }
  2960. bench_stats_start(&count, &start);
  2961. do {
  2962. for (times = 0; times < numBlocks || pending > 0; ) {
  2963. bench_async_poll(&pending);
  2964. /* while free pending slots in queue, submit ops */
  2965. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2966. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&dec[i]), 0,
  2967. &times, numBlocks, &pending)) {
  2968. #ifndef BENCHMARK_AESGCM_STREAM
  2969. ret = wc_AesGcmDecrypt(&dec[i], bench_plain,
  2970. bench_cipher, bench_size,
  2971. iv, ivSz, bench_tag, AES_AUTH_TAG_SZ,
  2972. bench_additional, aesAuthAddSz);
  2973. #else
  2974. ret = wc_AesGcmDecryptInit(&enc[i], NULL, 0, iv, ivSz);
  2975. if (ret == 0) {
  2976. ret = wc_AesGcmDecryptUpdate(&enc[i], bench_plain,
  2977. bench_cipher, bench_size, bench_additional,
  2978. aesAuthAddSz);
  2979. }
  2980. if (ret == 0) {
  2981. ret = wc_AesGcmDecryptFinal(&enc[i], bench_tag,
  2982. AES_AUTH_TAG_SZ);
  2983. }
  2984. #endif
  2985. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&dec[i]),
  2986. 0, &times, &pending)) {
  2987. goto exit_aes_gcm_dec;
  2988. }
  2989. }
  2990. } /* for i */
  2991. } /* for times */
  2992. count += times;
  2993. } while (bench_stats_check(start));
  2994. exit_aes_gcm_dec:
  2995. bench_stats_sym_finish(decLabel, useDeviceID, count, bench_size,
  2996. start, ret);
  2997. #endif /* HAVE_AES_DECRYPT */
  2998. (void)decLabel;
  2999. exit:
  3000. if (ret < 0) {
  3001. printf("bench_aesgcm failed: %d\n", ret);
  3002. }
  3003. #ifdef HAVE_AES_DECRYPT
  3004. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3005. wc_AesFree(&dec[i]);
  3006. }
  3007. #endif
  3008. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3009. wc_AesFree(&enc[i]);
  3010. }
  3011. WC_FREE_VAR(bench_additional, HEAP_HINT);
  3012. WC_FREE_VAR(bench_tag, HEAP_HINT);
  3013. }
  3014. void bench_aesgcm(int useDeviceID)
  3015. {
  3016. #define AES_GCM_STRING(n, dir) AES_AAD_STRING("AES-" #n "-GCM-" #dir)
  3017. #if defined(WOLFSSL_AES_128) && !defined(WOLFSSL_AFALG_XILINX_AES) \
  3018. && !defined(WOLFSSL_XILINX_CRYPT) \
  3019. || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  3020. bench_aesgcm_internal(useDeviceID, bench_key, 16, bench_iv, 12,
  3021. AES_GCM_STRING(128, enc), AES_GCM_STRING(128, dec));
  3022. #endif
  3023. #if defined(WOLFSSL_AES_192) && !defined(WOLFSSL_AFALG_XILINX_AES) \
  3024. && !defined(WOLFSSL_XILINX_CRYPT)
  3025. bench_aesgcm_internal(useDeviceID, bench_key, 24, bench_iv, 12,
  3026. AES_GCM_STRING(192, enc), AES_GCM_STRING(192, dec));
  3027. #endif
  3028. #ifdef WOLFSSL_AES_256
  3029. bench_aesgcm_internal(useDeviceID, bench_key, 32, bench_iv, 12,
  3030. AES_GCM_STRING(256, enc), AES_GCM_STRING(256, dec));
  3031. #endif
  3032. #undef AES_GCM_STRING
  3033. }
  3034. /* GMAC */
  3035. void bench_gmac(void)
  3036. {
  3037. int ret, count = 0;
  3038. Gmac gmac;
  3039. double start;
  3040. byte tag[AES_AUTH_TAG_SZ];
  3041. /* determine GCM GHASH method */
  3042. #ifdef GCM_SMALL
  3043. const char* gmacStr = "GMAC Small";
  3044. #elif defined(GCM_TABLE)
  3045. const char* gmacStr = "GMAC Table";
  3046. #elif defined(GCM_TABLE_4BIT)
  3047. const char* gmacStr = "GMAC Table 4-bit";
  3048. #elif defined(GCM_WORD32)
  3049. const char* gmacStr = "GMAC Word32";
  3050. #else
  3051. const char* gmacStr = "GMAC Default";
  3052. #endif
  3053. /* init keys */
  3054. XMEMSET(bench_plain, 0, bench_size);
  3055. XMEMSET(tag, 0, sizeof(tag));
  3056. XMEMSET(&gmac, 0, sizeof(Gmac)); /* clear context */
  3057. (void)wc_AesInit((Aes*)&gmac, HEAP_HINT, INVALID_DEVID);
  3058. wc_GmacSetKey(&gmac, bench_key, 16);
  3059. bench_stats_start(&count, &start);
  3060. do {
  3061. ret = wc_GmacUpdate(&gmac, bench_iv, 12, bench_plain, bench_size,
  3062. tag, sizeof(tag));
  3063. count++;
  3064. } while (bench_stats_check(start));
  3065. wc_AesFree((Aes*)&gmac);
  3066. bench_stats_sym_finish(gmacStr, 0, count, bench_size, start, ret);
  3067. }
  3068. #endif /* HAVE_AESGCM */
  3069. #ifdef HAVE_AES_ECB
  3070. static void bench_aesecb_internal(int useDeviceID,
  3071. const byte* key, word32 keySz,
  3072. const char* encLabel, const char* decLabel)
  3073. {
  3074. int ret = 0, i, count = 0, times, pending = 0;
  3075. Aes enc[BENCH_MAX_PENDING];
  3076. double start;
  3077. #ifdef HAVE_FIPS
  3078. static const int benchSz = AES_BLOCK_SIZE;
  3079. #else
  3080. static const int benchSz = BENCH_SIZE;
  3081. #endif
  3082. /* clear for done cleanup */
  3083. XMEMSET(enc, 0, sizeof(enc));
  3084. /* init keys */
  3085. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3086. if ((ret = wc_AesInit(&enc[i], HEAP_HINT,
  3087. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  3088. printf("AesInit failed, ret = %d\n", ret);
  3089. goto exit;
  3090. }
  3091. ret = wc_AesSetKey(&enc[i], key, keySz, bench_iv, AES_ENCRYPTION);
  3092. if (ret != 0) {
  3093. printf("AesSetKey failed, ret = %d\n", ret);
  3094. goto exit;
  3095. }
  3096. }
  3097. bench_stats_start(&count, &start);
  3098. do {
  3099. int outer_loop_limit = ((bench_size / benchSz) * 10) + 1;
  3100. for (times = 0;
  3101. times < outer_loop_limit /* numBlocks */ || pending > 0;
  3102. ) {
  3103. bench_async_poll(&pending);
  3104. /* while free pending slots in queue, submit ops */
  3105. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3106. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0,
  3107. &times, numBlocks, &pending)) {
  3108. #ifdef HAVE_FIPS
  3109. wc_AesEncryptDirect(&enc[i], bench_cipher, bench_plain);
  3110. #else
  3111. wc_AesEcbEncrypt(&enc[i], bench_cipher, bench_plain,
  3112. benchSz);
  3113. #endif
  3114. ret = 0;
  3115. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]),
  3116. 0, &times, &pending)) {
  3117. goto exit_aes_enc;
  3118. }
  3119. }
  3120. } /* for i */
  3121. } /* for times */
  3122. count += times;
  3123. } while (bench_stats_check(start));
  3124. exit_aes_enc:
  3125. bench_stats_sym_finish(encLabel, useDeviceID, count, benchSz,
  3126. start, ret);
  3127. #ifdef HAVE_AES_DECRYPT
  3128. /* init keys */
  3129. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3130. ret = wc_AesSetKey(&enc[i], key, keySz, bench_iv, AES_DECRYPTION);
  3131. if (ret != 0) {
  3132. printf("AesSetKey failed, ret = %d\n", ret);
  3133. goto exit;
  3134. }
  3135. }
  3136. bench_stats_start(&count, &start);
  3137. do {
  3138. int outer_loop_limit = (10 * (bench_size / benchSz)) + 1;
  3139. for (times = 0; times < outer_loop_limit || pending > 0; ) {
  3140. bench_async_poll(&pending);
  3141. /* while free pending slots in queue, submit ops */
  3142. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3143. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0,
  3144. &times, numBlocks, &pending)) {
  3145. #ifdef HAVE_FIPS
  3146. wc_AesDecryptDirect(&enc[i], bench_plain, bench_cipher);
  3147. #else
  3148. wc_AesEcbDecrypt(&enc[i], bench_plain, bench_cipher,
  3149. benchSz);
  3150. #endif
  3151. ret = 0;
  3152. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]),
  3153. 0, &times, &pending)) {
  3154. goto exit_aes_dec;
  3155. }
  3156. }
  3157. } /* for i */
  3158. } /* for times */
  3159. count += times;
  3160. } while (bench_stats_check(start));
  3161. exit_aes_dec:
  3162. bench_stats_sym_finish(decLabel, useDeviceID, count, benchSz,
  3163. start, ret);
  3164. #endif /* HAVE_AES_DECRYPT */
  3165. exit:
  3166. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3167. wc_AesFree(&enc[i]);
  3168. }
  3169. }
  3170. void bench_aesecb(int useDeviceID)
  3171. {
  3172. #ifdef WOLFSSL_AES_128
  3173. bench_aesecb_internal(useDeviceID, bench_key, 16,
  3174. "AES-128-ECB-enc", "AES-128-ECB-dec");
  3175. #endif
  3176. #ifdef WOLFSSL_AES_192
  3177. bench_aesecb_internal(useDeviceID, bench_key, 24,
  3178. "AES-192-ECB-enc", "AES-192-ECB-dec");
  3179. #endif
  3180. #ifdef WOLFSSL_AES_256
  3181. bench_aesecb_internal(useDeviceID, bench_key, 32,
  3182. "AES-256-ECB-enc", "AES-256-ECB-dec");
  3183. #endif
  3184. }
  3185. #endif /* HAVE_AES_ECB */
  3186. #ifdef WOLFSSL_AES_CFB
  3187. static void bench_aescfb_internal(const byte* key,
  3188. word32 keySz, const byte* iv,
  3189. const char* label)
  3190. {
  3191. Aes enc;
  3192. double start;
  3193. int i, ret, count;
  3194. ret = wc_AesSetKey(&enc, key, keySz, iv, AES_ENCRYPTION);
  3195. if (ret != 0) {
  3196. printf("AesSetKey failed, ret = %d\n", ret);
  3197. return;
  3198. }
  3199. bench_stats_start(&count, &start);
  3200. do {
  3201. for (i = 0; i < numBlocks; i++) {
  3202. if((ret = wc_AesCfbEncrypt(&enc, bench_plain, bench_cipher,
  3203. bench_size)) != 0) {
  3204. printf("wc_AesCfbEncrypt failed, ret = %d\n", ret);
  3205. return;
  3206. }
  3207. }
  3208. count += i;
  3209. } while (bench_stats_check(start));
  3210. bench_stats_sym_finish(label, 0, count, bench_size, start, ret);
  3211. }
  3212. void bench_aescfb(void)
  3213. {
  3214. #ifdef WOLFSSL_AES_128
  3215. bench_aescfb_internal(bench_key, 16, bench_iv, "AES-128-CFB");
  3216. #endif
  3217. #ifdef WOLFSSL_AES_192
  3218. bench_aescfb_internal(bench_key, 24, bench_iv, "AES-192-CFB");
  3219. #endif
  3220. #ifdef WOLFSSL_AES_256
  3221. bench_aescfb_internal(bench_key, 32, bench_iv, "AES-256-CFB");
  3222. #endif
  3223. }
  3224. #endif /* WOLFSSL_AES_CFB */
  3225. #ifdef WOLFSSL_AES_OFB
  3226. static void bench_aesofb_internal(const byte* key,
  3227. word32 keySz, const byte* iv,
  3228. const char* label)
  3229. {
  3230. Aes enc;
  3231. double start;
  3232. int i, ret, count;
  3233. ret = wc_AesInit(&enc, NULL, INVALID_DEVID);
  3234. if (ret != 0) {
  3235. printf("AesInit failed, ret = %d\n", ret);
  3236. return;
  3237. }
  3238. ret = wc_AesSetKey(&enc, key, keySz, iv, AES_ENCRYPTION);
  3239. if (ret != 0) {
  3240. printf("AesSetKey failed, ret = %d\n", ret);
  3241. return;
  3242. }
  3243. bench_stats_start(&count, &start);
  3244. do {
  3245. for (i = 0; i < numBlocks; i++) {
  3246. if((ret = wc_AesOfbEncrypt(&enc, bench_plain, bench_cipher,
  3247. bench_size)) != 0) {
  3248. printf("wc_AesCfbEncrypt failed, ret = %d\n", ret);
  3249. return;
  3250. }
  3251. }
  3252. count += i;
  3253. } while (bench_stats_check(start));
  3254. bench_stats_sym_finish(label, 0, count, bench_size, start, ret);
  3255. wc_AesFree(&enc);
  3256. }
  3257. void bench_aesofb(void)
  3258. {
  3259. #ifdef WOLFSSL_AES_128
  3260. bench_aesofb_internal(bench_key, 16, bench_iv, "AES-128-OFB");
  3261. #endif
  3262. #ifdef WOLFSSL_AES_192
  3263. bench_aesofb_internal(bench_key, 24, bench_iv, "AES-192-OFB");
  3264. #endif
  3265. #ifdef WOLFSSL_AES_256
  3266. bench_aesofb_internal(bench_key, 32, bench_iv, "AES-256-OFB");
  3267. #endif
  3268. }
  3269. #endif /* WOLFSSL_AES_CFB */
  3270. #ifdef WOLFSSL_AES_XTS
  3271. void bench_aesxts(void)
  3272. {
  3273. XtsAes aes;
  3274. double start;
  3275. int i, count, ret;
  3276. static unsigned char k1[] = {
  3277. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  3278. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  3279. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  3280. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  3281. };
  3282. static unsigned char i1[] = {
  3283. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  3284. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  3285. };
  3286. ret = wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_ENCRYPTION,
  3287. HEAP_HINT, devId);
  3288. if (ret != 0) {
  3289. printf("wc_AesXtsSetKey failed, ret = %d\n", ret);
  3290. return;
  3291. }
  3292. bench_stats_start(&count, &start);
  3293. do {
  3294. for (i = 0; i < numBlocks; i++) {
  3295. if ((ret = wc_AesXtsEncrypt(&aes, bench_cipher, bench_plain,
  3296. bench_size, i1, sizeof(i1))) != 0) {
  3297. printf("wc_AesXtsEncrypt failed, ret = %d\n", ret);
  3298. return;
  3299. }
  3300. }
  3301. count += i;
  3302. } while (bench_stats_check(start));
  3303. bench_stats_sym_finish("AES-XTS-enc", 0, count, bench_size, start, ret);
  3304. wc_AesXtsFree(&aes);
  3305. /* decryption benchmark */
  3306. ret = wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_DECRYPTION,
  3307. HEAP_HINT, devId);
  3308. if (ret != 0) {
  3309. printf("wc_AesXtsSetKey failed, ret = %d\n", ret);
  3310. return;
  3311. }
  3312. bench_stats_start(&count, &start);
  3313. do {
  3314. for (i = 0; i < numBlocks; i++) {
  3315. if ((ret = wc_AesXtsDecrypt(&aes, bench_plain, bench_cipher,
  3316. bench_size, i1, sizeof(i1))) != 0) {
  3317. printf("wc_AesXtsDecrypt failed, ret = %d\n", ret);
  3318. return;
  3319. }
  3320. }
  3321. count += i;
  3322. } while (bench_stats_check(start));
  3323. bench_stats_sym_finish("AES-XTS-dec", 0, count, bench_size, start, ret);
  3324. wc_AesXtsFree(&aes);
  3325. }
  3326. #endif /* WOLFSSL_AES_XTS */
  3327. #ifdef WOLFSSL_AES_COUNTER
  3328. static void bench_aesctr_internal(const byte* key, word32 keySz,
  3329. const byte* iv, const char* label)
  3330. {
  3331. Aes enc;
  3332. double start;
  3333. int i, count, ret = 0;
  3334. wc_AesSetKeyDirect(&enc, key, keySz, iv, AES_ENCRYPTION);
  3335. bench_stats_start(&count, &start);
  3336. do {
  3337. for (i = 0; i < numBlocks; i++) {
  3338. if((ret = wc_AesCtrEncrypt(&enc, bench_plain, bench_cipher,
  3339. bench_size)) != 0) {
  3340. printf("wc_AesCtrEncrypt failed, ret = %d\n", ret);
  3341. return;
  3342. }
  3343. }
  3344. count += i;
  3345. } while (bench_stats_check(start));
  3346. bench_stats_sym_finish(label, 0, count, bench_size, start, ret);
  3347. }
  3348. void bench_aesctr(void)
  3349. {
  3350. #ifdef WOLFSSL_AES_128
  3351. bench_aesctr_internal(bench_key, 16, bench_iv, "AES-128-CTR");
  3352. #endif
  3353. #ifdef WOLFSSL_AES_192
  3354. bench_aesctr_internal(bench_key, 24, bench_iv, "AES-192-CTR");
  3355. #endif
  3356. #ifdef WOLFSSL_AES_256
  3357. bench_aesctr_internal(bench_key, 32, bench_iv, "AES-256-CTR");
  3358. #endif
  3359. }
  3360. #endif /* WOLFSSL_AES_COUNTER */
  3361. #ifdef HAVE_AESCCM
  3362. void bench_aesccm(int useDeviceID)
  3363. {
  3364. Aes enc;
  3365. double start;
  3366. int ret, i, count;
  3367. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  3368. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  3369. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  3370. if (bench_additional == NULL || bench_tag == NULL) {
  3371. printf("bench_aesccm malloc failed\n");
  3372. goto exit;
  3373. }
  3374. #endif
  3375. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  3376. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  3377. if ((ret = wc_AesInit(&enc, HEAP_HINT,
  3378. useDeviceID ? devId : INVALID_DEVID)) != 0) {
  3379. printf("wc_AesInit failed, ret = %d\n", ret);
  3380. goto exit;
  3381. }
  3382. if ((ret = wc_AesCcmSetKey(&enc, bench_key, 16)) != 0) {
  3383. printf("wc_AesCcmSetKey failed, ret = %d\n", ret);
  3384. goto exit;
  3385. }
  3386. bench_stats_start(&count, &start);
  3387. do {
  3388. for (i = 0; i < numBlocks; i++) {
  3389. ret |= wc_AesCcmEncrypt(&enc, bench_cipher, bench_plain, bench_size,
  3390. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  3391. bench_additional, 0);
  3392. }
  3393. count += i;
  3394. } while (bench_stats_check(start));
  3395. bench_stats_sym_finish(AES_AAD_STRING("AES-CCM-enc"), useDeviceID, count,
  3396. bench_size, start, ret);
  3397. if (ret != 0) {
  3398. printf("wc_AesCcmEncrypt failed, ret = %d\n", ret);
  3399. goto exit;
  3400. }
  3401. bench_stats_start(&count, &start);
  3402. do {
  3403. for (i = 0; i < numBlocks; i++) {
  3404. ret |= wc_AesCcmDecrypt(&enc, bench_plain, bench_cipher, bench_size,
  3405. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  3406. bench_additional, 0);
  3407. }
  3408. count += i;
  3409. } while (bench_stats_check(start));
  3410. bench_stats_sym_finish(AES_AAD_STRING("AES-CCM-dec"), useDeviceID, count,
  3411. bench_size, start, ret);
  3412. if (ret != 0) {
  3413. printf("wc_AesCcmEncrypt failed, ret = %d\n", ret);
  3414. goto exit;
  3415. }
  3416. exit:
  3417. WC_FREE_VAR(bench_additional, HEAP_HINT);
  3418. WC_FREE_VAR(bench_tag, HEAP_HINT);
  3419. }
  3420. #endif /* HAVE_AESCCM */
  3421. #ifdef WOLFSSL_AES_SIV
  3422. static void bench_aessiv_internal(const byte* key, word32 keySz, const char*
  3423. encLabel, const char* decLabel)
  3424. {
  3425. int i;
  3426. int ret = 0;
  3427. byte assoc[AES_BLOCK_SIZE];
  3428. byte nonce[AES_BLOCK_SIZE];
  3429. byte siv[AES_BLOCK_SIZE];
  3430. int count = 0;
  3431. double start;
  3432. bench_stats_start(&count, &start);
  3433. do {
  3434. for (i = 0; i < numBlocks; i++) {
  3435. ret = wc_AesSivEncrypt(key, keySz, assoc, AES_BLOCK_SIZE, nonce,
  3436. AES_BLOCK_SIZE, bench_plain, bench_size,
  3437. siv, bench_cipher);
  3438. if (ret != 0) {
  3439. printf("wc_AesSivEncrypt failed (%d)\n", ret);
  3440. return;
  3441. }
  3442. }
  3443. count += i;
  3444. } while (bench_stats_check(start));
  3445. bench_stats_sym_finish(encLabel, 0, count, bench_size, start, ret);
  3446. bench_stats_start(&count, &start);
  3447. do {
  3448. for (i = 0; i < numBlocks; i++) {
  3449. ret = wc_AesSivDecrypt(key, keySz, assoc, AES_BLOCK_SIZE, nonce,
  3450. AES_BLOCK_SIZE, bench_cipher, bench_size,
  3451. siv, bench_plain);
  3452. if (ret != 0) {
  3453. printf("wc_AesSivDecrypt failed (%d)\n", ret);
  3454. return;
  3455. }
  3456. }
  3457. count += i;
  3458. } while (bench_stats_check(start));
  3459. bench_stats_sym_finish(decLabel, 0, count, bench_size, start, ret);
  3460. }
  3461. void bench_aessiv(void)
  3462. {
  3463. bench_aessiv_internal(bench_key, 32, "AES-256-SIV-enc", "AES-256-SIV-dec");
  3464. bench_aessiv_internal(bench_key, 48, "AES-384-SIV-enc", "AES-384-SIV-dec");
  3465. bench_aessiv_internal(bench_key, 64, "AES-512-SIV-enc", "AES-512-SIV-dec");
  3466. }
  3467. #endif /* WOLFSSL_AES_SIV */
  3468. #endif /* !NO_AES */
  3469. #ifdef HAVE_POLY1305
  3470. void bench_poly1305(void)
  3471. {
  3472. Poly1305 enc;
  3473. byte mac[16];
  3474. double start;
  3475. int ret = 0, i, count;
  3476. if (digest_stream) {
  3477. ret = wc_Poly1305SetKey(&enc, bench_key, 32);
  3478. if (ret != 0) {
  3479. printf("Poly1305SetKey failed, ret = %d\n", ret);
  3480. return;
  3481. }
  3482. bench_stats_start(&count, &start);
  3483. do {
  3484. for (i = 0; i < numBlocks; i++) {
  3485. ret = wc_Poly1305Update(&enc, bench_plain, bench_size);
  3486. if (ret != 0) {
  3487. printf("Poly1305Update failed: %d\n", ret);
  3488. break;
  3489. }
  3490. }
  3491. wc_Poly1305Final(&enc, mac);
  3492. count += i;
  3493. } while (bench_stats_check(start));
  3494. bench_stats_sym_finish("POLY1305", 0, count, bench_size, start, ret);
  3495. }
  3496. else {
  3497. bench_stats_start(&count, &start);
  3498. do {
  3499. for (i = 0; i < numBlocks; i++) {
  3500. ret = wc_Poly1305SetKey(&enc, bench_key, 32);
  3501. if (ret != 0) {
  3502. printf("Poly1305SetKey failed, ret = %d\n", ret);
  3503. return;
  3504. }
  3505. ret = wc_Poly1305Update(&enc, bench_plain, bench_size);
  3506. if (ret != 0) {
  3507. printf("Poly1305Update failed: %d\n", ret);
  3508. break;
  3509. }
  3510. wc_Poly1305Final(&enc, mac);
  3511. }
  3512. count += i;
  3513. } while (bench_stats_check(start));
  3514. bench_stats_sym_finish("POLY1305", 0, count, bench_size, start, ret);
  3515. }
  3516. }
  3517. #endif /* HAVE_POLY1305 */
  3518. #ifdef HAVE_CAMELLIA
  3519. void bench_camellia(void)
  3520. {
  3521. Camellia cam;
  3522. double start;
  3523. int ret, i, count;
  3524. ret = wc_CamelliaSetKey(&cam, bench_key, 16, bench_iv);
  3525. if (ret != 0) {
  3526. printf("CamelliaSetKey failed, ret = %d\n", ret);
  3527. return;
  3528. }
  3529. bench_stats_start(&count, &start);
  3530. do {
  3531. for (i = 0; i < numBlocks; i++) {
  3532. ret = wc_CamelliaCbcEncrypt(&cam, bench_cipher, bench_plain,
  3533. bench_size);
  3534. if (ret < 0) {
  3535. printf("CamelliaCbcEncrypt failed: %d\n", ret);
  3536. return;
  3537. }
  3538. }
  3539. count += i;
  3540. } while (bench_stats_check(start));
  3541. bench_stats_sym_finish("Camellia", 0, count, bench_size, start, ret);
  3542. }
  3543. #endif
  3544. #ifndef NO_DES3
  3545. void bench_des(int useDeviceID)
  3546. {
  3547. int ret = 0, i, count = 0, times, pending = 0;
  3548. Des3 enc[BENCH_MAX_PENDING];
  3549. double start;
  3550. /* clear for done cleanup */
  3551. XMEMSET(enc, 0, sizeof(enc));
  3552. /* init keys */
  3553. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3554. if ((ret = wc_Des3Init(&enc[i], HEAP_HINT,
  3555. useDeviceID ? devId : INVALID_DEVID)) != 0) {
  3556. printf("Des3Init failed, ret = %d\n", ret);
  3557. goto exit;
  3558. }
  3559. ret = wc_Des3_SetKey(&enc[i], bench_key, bench_iv, DES_ENCRYPTION);
  3560. if (ret != 0) {
  3561. printf("Des3_SetKey failed, ret = %d\n", ret);
  3562. goto exit;
  3563. }
  3564. }
  3565. bench_stats_start(&count, &start);
  3566. do {
  3567. for (times = 0; times < numBlocks || pending > 0; ) {
  3568. bench_async_poll(&pending);
  3569. /* while free pending slots in queue, submit ops */
  3570. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3571. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0,
  3572. &times, numBlocks, &pending)) {
  3573. ret = wc_Des3_CbcEncrypt(&enc[i],
  3574. bench_cipher,
  3575. bench_plain, bench_size);
  3576. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]),
  3577. 0, &times, &pending)) {
  3578. goto exit_3des;
  3579. }
  3580. }
  3581. } /* for i */
  3582. } /* for times */
  3583. count += times;
  3584. } while (bench_stats_check(start));
  3585. exit_3des:
  3586. bench_stats_sym_finish("3DES", useDeviceID, count, bench_size, start, ret);
  3587. exit:
  3588. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3589. wc_Des3Free(&enc[i]);
  3590. }
  3591. }
  3592. #endif /* !NO_DES3 */
  3593. #ifndef NO_RC4
  3594. void bench_arc4(int useDeviceID)
  3595. {
  3596. int ret = 0, i, count = 0, times, pending = 0;
  3597. Arc4 enc[BENCH_MAX_PENDING];
  3598. double start;
  3599. /* clear for done cleanup */
  3600. XMEMSET(enc, 0, sizeof(enc));
  3601. /* init keys */
  3602. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3603. if ((ret = wc_Arc4Init(&enc[i], HEAP_HINT,
  3604. useDeviceID ? devId : INVALID_DEVID)) != 0) {
  3605. printf("Arc4Init failed, ret = %d\n", ret);
  3606. goto exit;
  3607. }
  3608. ret = wc_Arc4SetKey(&enc[i], bench_key, 16);
  3609. if (ret != 0) {
  3610. printf("Arc4SetKey failed, ret = %d\n", ret);
  3611. goto exit;
  3612. }
  3613. }
  3614. bench_stats_start(&count, &start);
  3615. do {
  3616. for (times = 0; times < numBlocks || pending > 0; ) {
  3617. bench_async_poll(&pending);
  3618. /* while free pending slots in queue, submit ops */
  3619. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3620. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0,
  3621. &times, numBlocks, &pending)) {
  3622. ret = wc_Arc4Process(&enc[i], bench_cipher, bench_plain,
  3623. bench_size);
  3624. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]),
  3625. 0, &times, &pending)) {
  3626. goto exit_arc4;
  3627. }
  3628. }
  3629. } /* for i */
  3630. } /* for times */
  3631. count += times;
  3632. } while (bench_stats_check(start));
  3633. exit_arc4:
  3634. bench_stats_sym_finish("ARC4", useDeviceID, count, bench_size, start, ret);
  3635. exit:
  3636. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3637. wc_Arc4Free(&enc[i]);
  3638. }
  3639. }
  3640. #endif /* !NO_RC4 */
  3641. #ifdef HAVE_CHACHA
  3642. void bench_chacha(void)
  3643. {
  3644. ChaCha enc;
  3645. double start;
  3646. int i, count;
  3647. wc_Chacha_SetKey(&enc, bench_key, 16);
  3648. bench_stats_start(&count, &start);
  3649. do {
  3650. for (i = 0; i < numBlocks; i++) {
  3651. wc_Chacha_SetIV(&enc, bench_iv, 0);
  3652. wc_Chacha_Process(&enc, bench_cipher, bench_plain, bench_size);
  3653. }
  3654. count += i;
  3655. } while (bench_stats_check(start));
  3656. bench_stats_sym_finish("CHACHA", 0, count, bench_size, start, 0);
  3657. }
  3658. #endif /* HAVE_CHACHA*/
  3659. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  3660. void bench_chacha20_poly1305_aead(void)
  3661. {
  3662. double start;
  3663. int ret = 0, i, count;
  3664. byte authTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  3665. XMEMSET(authTag, 0, sizeof(authTag));
  3666. bench_stats_start(&count, &start);
  3667. do {
  3668. for (i = 0; i < numBlocks; i++) {
  3669. ret = wc_ChaCha20Poly1305_Encrypt(bench_key, bench_iv, NULL, 0,
  3670. bench_plain, bench_size, bench_cipher, authTag);
  3671. if (ret < 0) {
  3672. printf("wc_ChaCha20Poly1305_Encrypt error: %d\n", ret);
  3673. break;
  3674. }
  3675. }
  3676. count += i;
  3677. } while (bench_stats_check(start));
  3678. bench_stats_sym_finish("CHA-POLY", 0, count, bench_size, start, ret);
  3679. }
  3680. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  3681. #ifndef NO_MD5
  3682. void bench_md5(int useDeviceID)
  3683. {
  3684. wc_Md5 hash[BENCH_MAX_PENDING];
  3685. double start;
  3686. int ret = 0, i, count = 0, times, pending = 0;
  3687. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  3688. WC_MD5_DIGEST_SIZE, HEAP_HINT);
  3689. WC_INIT_ARRAY(digest, byte, BENCH_MAX_PENDING,
  3690. WC_MD5_DIGEST_SIZE, HEAP_HINT);
  3691. /* clear for done cleanup */
  3692. XMEMSET(hash, 0, sizeof(hash));
  3693. if (digest_stream) {
  3694. /* init keys */
  3695. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3696. ret = wc_InitMd5_ex(&hash[i], HEAP_HINT,
  3697. useDeviceID ? devId : INVALID_DEVID);
  3698. if (ret != 0) {
  3699. printf("InitMd5_ex failed, ret = %d\n", ret);
  3700. goto exit;
  3701. }
  3702. #ifdef WOLFSSL_PIC32MZ_HASH
  3703. wc_Md5SizeSet(&hash[i], numBlocks * bench_size);
  3704. #endif
  3705. }
  3706. bench_stats_start(&count, &start);
  3707. do {
  3708. for (times = 0; times < numBlocks || pending > 0; ) {
  3709. bench_async_poll(&pending);
  3710. /* while free pending slots in queue, submit ops */
  3711. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3712. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  3713. 0, &times, numBlocks, &pending)) {
  3714. ret = wc_Md5Update(&hash[i], bench_plain,
  3715. bench_size);
  3716. if (!bench_async_handle(&ret,
  3717. BENCH_ASYNC_GET_DEV(&hash[i]),
  3718. 0, &times, &pending)) {
  3719. goto exit_md5;
  3720. }
  3721. }
  3722. } /* for i */
  3723. } /* for times */
  3724. count += times;
  3725. times = 0;
  3726. do {
  3727. bench_async_poll(&pending);
  3728. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3729. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  3730. 0, &times, numBlocks, &pending)) {
  3731. ret = wc_Md5Final(&hash[i], digest[i]);
  3732. if (!bench_async_handle(&ret,
  3733. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  3734. &times, &pending)) {
  3735. goto exit_md5;
  3736. }
  3737. }
  3738. } /* for i */
  3739. } while (pending > 0);
  3740. } while (bench_stats_check(start));
  3741. }
  3742. else {
  3743. bench_stats_start(&count, &start);
  3744. do {
  3745. for (times = 0; times < numBlocks; times++) {
  3746. ret = wc_InitMd5_ex(hash, HEAP_HINT, INVALID_DEVID);
  3747. if (ret == 0)
  3748. ret = wc_Md5Update(hash, bench_plain, bench_size);
  3749. if (ret == 0)
  3750. ret = wc_Md5Final(hash, digest[0]);
  3751. if (ret != 0)
  3752. goto exit_md5;
  3753. } /* for times */
  3754. count += times;
  3755. } while (bench_stats_check(start));
  3756. }
  3757. exit_md5:
  3758. bench_stats_sym_finish("MD5", useDeviceID, count, bench_size, start, ret);
  3759. exit:
  3760. #ifdef WOLFSSL_ASYNC_CRYPT
  3761. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3762. wc_Md5Free(&hash[i]);
  3763. }
  3764. #endif
  3765. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  3766. }
  3767. #endif /* !NO_MD5 */
  3768. #ifndef NO_SHA
  3769. void bench_sha(int useDeviceID)
  3770. {
  3771. wc_Sha hash[BENCH_MAX_PENDING];
  3772. double start;
  3773. int ret = 0, i, count = 0, times, pending = 0;
  3774. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  3775. WC_SHA_DIGEST_SIZE, HEAP_HINT);
  3776. WC_INIT_ARRAY(digest, byte, BENCH_MAX_PENDING,
  3777. WC_SHA_DIGEST_SIZE, HEAP_HINT);
  3778. /* clear for done cleanup */
  3779. XMEMSET(hash, 0, sizeof(hash));
  3780. if (digest_stream) {
  3781. /* init keys */
  3782. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3783. ret = wc_InitSha_ex(&hash[i], HEAP_HINT,
  3784. useDeviceID ? devId : INVALID_DEVID);
  3785. if (ret != 0) {
  3786. printf("InitSha failed, ret = %d\n", ret);
  3787. goto exit;
  3788. }
  3789. #ifdef WOLFSSL_PIC32MZ_HASH
  3790. wc_ShaSizeSet(&hash[i], numBlocks * bench_size);
  3791. #endif
  3792. }
  3793. bench_stats_start(&count, &start);
  3794. do {
  3795. for (times = 0; times < numBlocks || pending > 0; ) {
  3796. bench_async_poll(&pending);
  3797. /* while free pending slots in queue, submit ops */
  3798. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3799. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  3800. 0, &times, numBlocks, &pending)) {
  3801. ret = wc_ShaUpdate(&hash[i], bench_plain,
  3802. bench_size);
  3803. if (!bench_async_handle(&ret,
  3804. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  3805. &times, &pending)) {
  3806. goto exit_sha;
  3807. }
  3808. }
  3809. } /* for i */
  3810. } /* for times */
  3811. count += times;
  3812. times = 0;
  3813. do {
  3814. bench_async_poll(&pending);
  3815. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3816. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  3817. 0, &times, numBlocks, &pending)) {
  3818. ret = wc_ShaFinal(&hash[i], digest[i]);
  3819. if (!bench_async_handle(&ret,
  3820. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  3821. &times, &pending)) {
  3822. goto exit_sha;
  3823. }
  3824. }
  3825. } /* for i */
  3826. } while (pending > 0);
  3827. } while (bench_stats_check(start));
  3828. }
  3829. else {
  3830. bench_stats_start(&count, &start);
  3831. do {
  3832. for (times = 0; times < numBlocks; times++) {
  3833. ret = wc_InitSha_ex(hash, HEAP_HINT,
  3834. useDeviceID ? devId : INVALID_DEVID);
  3835. if (ret == 0)
  3836. ret = wc_ShaUpdate(hash, bench_plain, bench_size);
  3837. if (ret == 0)
  3838. ret = wc_ShaFinal(hash, digest[0]);
  3839. if (ret != 0)
  3840. goto exit_sha;
  3841. } /* for times */
  3842. count += times;
  3843. } while (bench_stats_check(start));
  3844. }
  3845. exit_sha:
  3846. bench_stats_sym_finish("SHA", useDeviceID, count, bench_size, start, ret);
  3847. exit:
  3848. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3849. wc_ShaFree(&hash[i]);
  3850. }
  3851. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  3852. }
  3853. #endif /* NO_SHA */
  3854. #ifdef WOLFSSL_SHA224
  3855. void bench_sha224(int useDeviceID)
  3856. {
  3857. wc_Sha224 hash[BENCH_MAX_PENDING];
  3858. double start;
  3859. int ret = 0, i, count = 0, times, pending = 0;
  3860. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  3861. WC_SHA224_DIGEST_SIZE, HEAP_HINT);
  3862. WC_INIT_ARRAY(digest, byte, BENCH_MAX_PENDING,
  3863. WC_SHA224_DIGEST_SIZE, HEAP_HINT);
  3864. /* clear for done cleanup */
  3865. XMEMSET(hash, 0, sizeof(hash));
  3866. if (digest_stream) {
  3867. /* init keys */
  3868. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3869. ret = wc_InitSha224_ex(&hash[i], HEAP_HINT,
  3870. useDeviceID ? devId : INVALID_DEVID);
  3871. if (ret != 0) {
  3872. printf("InitSha224_ex failed, ret = %d\n", ret);
  3873. goto exit;
  3874. }
  3875. }
  3876. bench_stats_start(&count, &start);
  3877. do {
  3878. for (times = 0; times < numBlocks || pending > 0; ) {
  3879. bench_async_poll(&pending);
  3880. /* while free pending slots in queue, submit ops */
  3881. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3882. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  3883. 0, &times, numBlocks, &pending)) {
  3884. ret = wc_Sha224Update(&hash[i], bench_plain,
  3885. bench_size);
  3886. if (!bench_async_handle(&ret,
  3887. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  3888. &times, &pending)) {
  3889. goto exit_sha224;
  3890. }
  3891. }
  3892. } /* for i */
  3893. } /* for times */
  3894. count += times;
  3895. times = 0;
  3896. do {
  3897. bench_async_poll(&pending);
  3898. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3899. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  3900. 0, &times, numBlocks, &pending)) {
  3901. ret = wc_Sha224Final(&hash[i], digest[i]);
  3902. if (!bench_async_handle(&ret,
  3903. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  3904. &times, &pending)) {
  3905. goto exit_sha224;
  3906. }
  3907. }
  3908. } /* for i */
  3909. } while (pending > 0);
  3910. } while (bench_stats_check(start));
  3911. }
  3912. else {
  3913. bench_stats_start(&count, &start);
  3914. do {
  3915. for (times = 0; times < numBlocks; times++) {
  3916. ret = wc_InitSha224_ex(hash, HEAP_HINT,
  3917. useDeviceID ? devId : INVALID_DEVID);
  3918. if (ret == 0)
  3919. ret = wc_Sha224Update(hash, bench_plain, bench_size);
  3920. if (ret == 0)
  3921. ret = wc_Sha224Final(hash, digest[0]);
  3922. if (ret != 0)
  3923. goto exit_sha224;
  3924. } /* for times */
  3925. count += times;
  3926. } while (bench_stats_check(start));
  3927. }
  3928. exit_sha224:
  3929. bench_stats_sym_finish("SHA-224", useDeviceID, count,
  3930. bench_size, start, ret);
  3931. exit:
  3932. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3933. wc_Sha224Free(&hash[i]);
  3934. }
  3935. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  3936. }
  3937. #endif
  3938. #ifndef NO_SHA256
  3939. void bench_sha256(int useDeviceID)
  3940. {
  3941. wc_Sha256 hash[BENCH_MAX_PENDING];
  3942. double start;
  3943. int ret = 0, i, count = 0, times, pending = 0;
  3944. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  3945. WC_SHA256_DIGEST_SIZE, HEAP_HINT);
  3946. WC_INIT_ARRAY(digest, byte, BENCH_MAX_PENDING,
  3947. WC_SHA256_DIGEST_SIZE, HEAP_HINT);
  3948. /* clear for done cleanup */
  3949. XMEMSET(hash, 0, sizeof(hash));
  3950. if (digest_stream) {
  3951. /* init keys */
  3952. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3953. ret = wc_InitSha256_ex(&hash[i], HEAP_HINT,
  3954. useDeviceID ? devId: INVALID_DEVID);
  3955. if (ret != 0) {
  3956. printf("InitSha256_ex failed, ret = %d\n", ret);
  3957. goto exit;
  3958. }
  3959. #ifdef WOLFSSL_PIC32MZ_HASH
  3960. wc_Sha256SizeSet(&hash[i], numBlocks * bench_size);
  3961. #endif
  3962. }
  3963. bench_stats_start(&count, &start);
  3964. do {
  3965. for (times = 0; times < numBlocks || pending > 0; ) {
  3966. bench_async_poll(&pending);
  3967. /* while free pending slots in queue, submit ops */
  3968. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3969. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  3970. 0, &times, numBlocks, &pending)) {
  3971. ret = wc_Sha256Update(&hash[i], bench_plain,
  3972. bench_size);
  3973. if (!bench_async_handle(&ret,
  3974. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  3975. &times, &pending)) {
  3976. goto exit_sha256;
  3977. }
  3978. }
  3979. } /* for i */
  3980. } /* for times */
  3981. count += times;
  3982. times = 0;
  3983. do {
  3984. bench_async_poll(&pending);
  3985. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3986. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  3987. 0, &times, numBlocks, &pending)) {
  3988. ret = wc_Sha256Final(&hash[i], digest[i]);
  3989. if (!bench_async_handle(&ret,
  3990. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  3991. &times, &pending)) {
  3992. goto exit_sha256;
  3993. }
  3994. }
  3995. } /* for i */
  3996. } while (pending > 0);
  3997. } while (bench_stats_check(start));
  3998. }
  3999. else {
  4000. bench_stats_start(&count, &start);
  4001. do {
  4002. for (times = 0; times < numBlocks; times++) {
  4003. ret = wc_InitSha256_ex(hash, HEAP_HINT,
  4004. useDeviceID ? devId: INVALID_DEVID);
  4005. if (ret == 0)
  4006. ret = wc_Sha256Update(hash, bench_plain, bench_size);
  4007. if (ret == 0)
  4008. ret = wc_Sha256Final(hash, digest[0]);
  4009. if (ret != 0)
  4010. goto exit_sha256;
  4011. } /* for times */
  4012. count += times;
  4013. } while (bench_stats_check(start));
  4014. }
  4015. exit_sha256:
  4016. bench_stats_sym_finish("SHA-256", useDeviceID, count, bench_size,
  4017. start, ret);
  4018. exit:
  4019. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4020. wc_Sha256Free(&hash[i]);
  4021. }
  4022. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  4023. }
  4024. #endif
  4025. #ifdef WOLFSSL_SHA384
  4026. void bench_sha384(int useDeviceID)
  4027. {
  4028. wc_Sha384 hash[BENCH_MAX_PENDING];
  4029. double start;
  4030. int ret = 0, i, count = 0, times, pending = 0;
  4031. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4032. WC_SHA384_DIGEST_SIZE, HEAP_HINT);
  4033. WC_INIT_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4034. WC_SHA384_DIGEST_SIZE, HEAP_HINT);
  4035. /* clear for done cleanup */
  4036. XMEMSET(hash, 0, sizeof(hash));
  4037. if (digest_stream) {
  4038. /* init keys */
  4039. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4040. ret = wc_InitSha384_ex(&hash[i], HEAP_HINT,
  4041. useDeviceID ? devId : INVALID_DEVID);
  4042. if (ret != 0) {
  4043. printf("InitSha384_ex failed, ret = %d\n", ret);
  4044. goto exit;
  4045. }
  4046. }
  4047. bench_stats_start(&count, &start);
  4048. do {
  4049. for (times = 0; times < numBlocks || pending > 0; ) {
  4050. bench_async_poll(&pending);
  4051. /* while free pending slots in queue, submit ops */
  4052. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4053. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  4054. 0, &times, numBlocks, &pending)) {
  4055. ret = wc_Sha384Update(&hash[i], bench_plain,
  4056. bench_size);
  4057. if (!bench_async_handle(&ret,
  4058. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  4059. &times, &pending)) {
  4060. goto exit_sha384;
  4061. }
  4062. }
  4063. } /* for i */
  4064. } /* for times */
  4065. count += times;
  4066. times = 0;
  4067. do {
  4068. bench_async_poll(&pending);
  4069. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4070. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  4071. 0, &times, numBlocks, &pending)) {
  4072. ret = wc_Sha384Final(&hash[i], digest[i]);
  4073. if (!bench_async_handle(&ret,
  4074. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  4075. &times, &pending)) {
  4076. goto exit_sha384;
  4077. }
  4078. }
  4079. } /* for i */
  4080. } while (pending > 0);
  4081. } while (bench_stats_check(start));
  4082. }
  4083. else {
  4084. bench_stats_start(&count, &start);
  4085. do {
  4086. for (times = 0; times < numBlocks; times++) {
  4087. ret = wc_InitSha384_ex(hash, HEAP_HINT,
  4088. useDeviceID ? devId : INVALID_DEVID);
  4089. if (ret == 0)
  4090. ret = wc_Sha384Update(hash, bench_plain, bench_size);
  4091. if (ret == 0)
  4092. ret = wc_Sha384Final(hash, digest[0]);
  4093. if (ret != 0)
  4094. goto exit_sha384;
  4095. } /* for times */
  4096. count += times;
  4097. } while (bench_stats_check(start));
  4098. }
  4099. exit_sha384:
  4100. bench_stats_sym_finish("SHA-384", useDeviceID, count, bench_size,
  4101. start, ret);
  4102. exit:
  4103. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4104. wc_Sha384Free(&hash[i]);
  4105. }
  4106. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  4107. }
  4108. #endif
  4109. #ifdef WOLFSSL_SHA512
  4110. void bench_sha512(int useDeviceID)
  4111. {
  4112. wc_Sha512 hash[BENCH_MAX_PENDING];
  4113. double start;
  4114. int ret = 0, i, count = 0, times, pending = 0;
  4115. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4116. WC_SHA512_DIGEST_SIZE, HEAP_HINT);
  4117. WC_INIT_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4118. WC_SHA512_DIGEST_SIZE, HEAP_HINT);
  4119. /* clear for done cleanup */
  4120. XMEMSET(hash, 0, sizeof(hash));
  4121. if (digest_stream) {
  4122. /* init keys */
  4123. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4124. ret = wc_InitSha512_ex(&hash[i], HEAP_HINT,
  4125. useDeviceID ? devId : INVALID_DEVID);
  4126. if (ret != 0) {
  4127. printf("InitSha512_ex failed, ret = %d\n", ret);
  4128. goto exit;
  4129. }
  4130. }
  4131. bench_stats_start(&count, &start);
  4132. do {
  4133. for (times = 0; times < numBlocks || pending > 0; ) {
  4134. bench_async_poll(&pending);
  4135. /* while free pending slots in queue, submit ops */
  4136. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4137. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  4138. 0, &times, numBlocks, &pending)) {
  4139. ret = wc_Sha512Update(&hash[i], bench_plain,
  4140. bench_size);
  4141. if (!bench_async_handle(&ret,
  4142. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  4143. &times, &pending)) {
  4144. goto exit_sha512;
  4145. }
  4146. }
  4147. } /* for i */
  4148. } /* for times */
  4149. count += times;
  4150. times = 0;
  4151. do {
  4152. bench_async_poll(&pending);
  4153. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4154. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  4155. 0, &times, numBlocks, &pending)) {
  4156. ret = wc_Sha512Final(&hash[i], digest[i]);
  4157. if (!bench_async_handle(&ret,
  4158. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  4159. &times, &pending)) {
  4160. goto exit_sha512;
  4161. }
  4162. }
  4163. } /* for i */
  4164. } while (pending > 0);
  4165. } while (bench_stats_check(start));
  4166. }
  4167. else {
  4168. bench_stats_start(&count, &start);
  4169. do {
  4170. for (times = 0; times < numBlocks; times++) {
  4171. ret = wc_InitSha512_ex(hash, HEAP_HINT,
  4172. useDeviceID ? devId : INVALID_DEVID);
  4173. if (ret == 0)
  4174. ret = wc_Sha512Update(hash, bench_plain, bench_size);
  4175. if (ret == 0)
  4176. ret = wc_Sha512Final(hash, digest[0]);
  4177. if (ret != 0)
  4178. goto exit_sha512;
  4179. } /* for times */
  4180. count += times;
  4181. } while (bench_stats_check(start));
  4182. }
  4183. exit_sha512:
  4184. bench_stats_sym_finish("SHA-512", useDeviceID, count, bench_size,
  4185. start, ret);
  4186. exit:
  4187. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4188. wc_Sha512Free(&hash[i]);
  4189. }
  4190. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  4191. }
  4192. #endif
  4193. #ifdef WOLFSSL_SHA3
  4194. #ifndef WOLFSSL_NOSHA3_224
  4195. void bench_sha3_224(int useDeviceID)
  4196. {
  4197. wc_Sha3 hash[BENCH_MAX_PENDING];
  4198. double start;
  4199. int ret = 0, i, count = 0, times, pending = 0;
  4200. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4201. WC_SHA3_224_DIGEST_SIZE, HEAP_HINT);
  4202. WC_INIT_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4203. WC_SHA3_224_DIGEST_SIZE, HEAP_HINT);
  4204. /* clear for done cleanup */
  4205. XMEMSET(hash, 0, sizeof(hash));
  4206. if (digest_stream) {
  4207. /* init keys */
  4208. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4209. ret = wc_InitSha3_224(&hash[i], HEAP_HINT,
  4210. useDeviceID ? devId : INVALID_DEVID);
  4211. if (ret != 0) {
  4212. printf("InitSha3_224 failed, ret = %d\n", ret);
  4213. goto exit;
  4214. }
  4215. }
  4216. bench_stats_start(&count, &start);
  4217. do {
  4218. for (times = 0; times < numBlocks || pending > 0; ) {
  4219. bench_async_poll(&pending);
  4220. /* while free pending slots in queue, submit ops */
  4221. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4222. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  4223. 0, &times, numBlocks, &pending)) {
  4224. ret = wc_Sha3_224_Update(&hash[i], bench_plain,
  4225. bench_size);
  4226. if (!bench_async_handle(&ret,
  4227. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  4228. &times, &pending)) {
  4229. goto exit_sha3_224;
  4230. }
  4231. }
  4232. } /* for i */
  4233. } /* for times */
  4234. count += times;
  4235. times = 0;
  4236. do {
  4237. bench_async_poll(&pending);
  4238. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4239. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  4240. 0, &times, numBlocks, &pending)) {
  4241. ret = wc_Sha3_224_Final(&hash[i], digest[i]);
  4242. if (!bench_async_handle(&ret,
  4243. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  4244. &times, &pending)) {
  4245. goto exit_sha3_224;
  4246. }
  4247. }
  4248. } /* for i */
  4249. } while (pending > 0);
  4250. } while (bench_stats_check(start));
  4251. }
  4252. else {
  4253. bench_stats_start(&count, &start);
  4254. do {
  4255. for (times = 0; times < numBlocks; times++) {
  4256. ret = wc_InitSha3_224(hash, HEAP_HINT,
  4257. useDeviceID ? devId : INVALID_DEVID);
  4258. if (ret == 0)
  4259. ret = wc_Sha3_224_Update(hash, bench_plain, bench_size);
  4260. if (ret == 0)
  4261. ret = wc_Sha3_224_Final(hash, digest[0]);
  4262. if (ret != 0)
  4263. goto exit_sha3_224;
  4264. } /* for times */
  4265. count += times;
  4266. } while (bench_stats_check(start));
  4267. }
  4268. exit_sha3_224:
  4269. bench_stats_sym_finish("SHA3-224", useDeviceID, count, bench_size,
  4270. start, ret);
  4271. exit:
  4272. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4273. wc_Sha3_224_Free(&hash[i]);
  4274. }
  4275. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  4276. }
  4277. #endif /* WOLFSSL_NOSHA3_224 */
  4278. #ifndef WOLFSSL_NOSHA3_256
  4279. void bench_sha3_256(int useDeviceID)
  4280. {
  4281. wc_Sha3 hash[BENCH_MAX_PENDING];
  4282. double start;
  4283. int ret = 0, i, count = 0, times, pending = 0;
  4284. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4285. WC_SHA3_256_DIGEST_SIZE, HEAP_HINT);
  4286. WC_INIT_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4287. WC_SHA3_256_DIGEST_SIZE, HEAP_HINT);
  4288. /* clear for done cleanup */
  4289. XMEMSET(hash, 0, sizeof(hash));
  4290. if (digest_stream) {
  4291. /* init keys */
  4292. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4293. ret = wc_InitSha3_256(&hash[i], HEAP_HINT,
  4294. useDeviceID ? devId : INVALID_DEVID);
  4295. if (ret != 0) {
  4296. printf("InitSha3_256 failed, ret = %d\n", ret);
  4297. goto exit;
  4298. }
  4299. }
  4300. bench_stats_start(&count, &start);
  4301. do {
  4302. for (times = 0; times < numBlocks || pending > 0; ) {
  4303. bench_async_poll(&pending);
  4304. /* while free pending slots in queue, submit ops */
  4305. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4306. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  4307. 0, &times, numBlocks, &pending)) {
  4308. ret = wc_Sha3_256_Update(&hash[i], bench_plain,
  4309. bench_size);
  4310. if (!bench_async_handle(&ret,
  4311. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  4312. &times, &pending)) {
  4313. goto exit_sha3_256;
  4314. }
  4315. }
  4316. } /* for i */
  4317. } /* for times */
  4318. count += times;
  4319. times = 0;
  4320. do {
  4321. bench_async_poll(&pending);
  4322. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4323. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  4324. 0, &times, numBlocks, &pending)) {
  4325. ret = wc_Sha3_256_Final(&hash[i], digest[i]);
  4326. if (!bench_async_handle(&ret,
  4327. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  4328. &times, &pending)) {
  4329. goto exit_sha3_256;
  4330. }
  4331. }
  4332. } /* for i */
  4333. } while (pending > 0);
  4334. } while (bench_stats_check(start));
  4335. }
  4336. else {
  4337. bench_stats_start(&count, &start);
  4338. do {
  4339. for (times = 0; times < numBlocks; times++) {
  4340. ret = wc_InitSha3_256(hash, HEAP_HINT,
  4341. useDeviceID ? devId : INVALID_DEVID);
  4342. if (ret == 0)
  4343. ret = wc_Sha3_256_Update(hash, bench_plain, bench_size);
  4344. if (ret == 0)
  4345. ret = wc_Sha3_256_Final(hash, digest[0]);
  4346. if (ret != 0)
  4347. goto exit_sha3_256;
  4348. } /* for times */
  4349. count += times;
  4350. } while (bench_stats_check(start));
  4351. }
  4352. exit_sha3_256:
  4353. bench_stats_sym_finish("SHA3-256", useDeviceID, count, bench_size,
  4354. start, ret);
  4355. exit:
  4356. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4357. wc_Sha3_256_Free(&hash[i]);
  4358. }
  4359. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  4360. }
  4361. #endif /* WOLFSSL_NOSHA3_256 */
  4362. #ifndef WOLFSSL_NOSHA3_384
  4363. void bench_sha3_384(int useDeviceID)
  4364. {
  4365. wc_Sha3 hash[BENCH_MAX_PENDING];
  4366. double start;
  4367. int ret = 0, i, count = 0, times, pending = 0;
  4368. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4369. WC_SHA3_384_DIGEST_SIZE, HEAP_HINT);
  4370. WC_INIT_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4371. WC_SHA3_384_DIGEST_SIZE, HEAP_HINT);
  4372. /* clear for done cleanup */
  4373. XMEMSET(hash, 0, sizeof(hash));
  4374. if (digest_stream) {
  4375. /* init keys */
  4376. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4377. ret = wc_InitSha3_384(&hash[i], HEAP_HINT,
  4378. useDeviceID ? devId : INVALID_DEVID);
  4379. if (ret != 0) {
  4380. printf("InitSha3_384 failed, ret = %d\n", ret);
  4381. goto exit;
  4382. }
  4383. }
  4384. bench_stats_start(&count, &start);
  4385. do {
  4386. for (times = 0; times < numBlocks || pending > 0; ) {
  4387. bench_async_poll(&pending);
  4388. /* while free pending slots in queue, submit ops */
  4389. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4390. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  4391. 0, &times, numBlocks, &pending)) {
  4392. ret = wc_Sha3_384_Update(&hash[i], bench_plain,
  4393. bench_size);
  4394. if (!bench_async_handle(&ret,
  4395. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  4396. &times, &pending)) {
  4397. goto exit_sha3_384;
  4398. }
  4399. }
  4400. } /* for i */
  4401. } /* for times */
  4402. count += times;
  4403. times = 0;
  4404. do {
  4405. bench_async_poll(&pending);
  4406. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4407. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  4408. 0, &times, numBlocks, &pending)) {
  4409. ret = wc_Sha3_384_Final(&hash[i], digest[i]);
  4410. if (!bench_async_handle(&ret,
  4411. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  4412. &times, &pending)) {
  4413. goto exit_sha3_384;
  4414. }
  4415. }
  4416. } /* for i */
  4417. } while (pending > 0);
  4418. } while (bench_stats_check(start));
  4419. }
  4420. else {
  4421. bench_stats_start(&count, &start);
  4422. do {
  4423. for (times = 0; times < numBlocks; times++) {
  4424. ret = wc_InitSha3_384(hash, HEAP_HINT,
  4425. useDeviceID ? devId : INVALID_DEVID);
  4426. if (ret == 0)
  4427. ret = wc_Sha3_384_Update(hash, bench_plain, bench_size);
  4428. if (ret == 0)
  4429. ret = wc_Sha3_384_Final(hash, digest[0]);
  4430. if (ret != 0)
  4431. goto exit_sha3_384;
  4432. } /* for times */
  4433. count += times;
  4434. } while (bench_stats_check(start));
  4435. }
  4436. exit_sha3_384:
  4437. bench_stats_sym_finish("SHA3-384", useDeviceID, count, bench_size,
  4438. start, ret);
  4439. exit:
  4440. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4441. wc_Sha3_384_Free(&hash[i]);
  4442. }
  4443. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  4444. }
  4445. #endif /* WOLFSSL_NOSHA3_384 */
  4446. #ifndef WOLFSSL_NOSHA3_512
  4447. void bench_sha3_512(int useDeviceID)
  4448. {
  4449. wc_Sha3 hash[BENCH_MAX_PENDING];
  4450. double start;
  4451. int ret = 0, i, count = 0, times, pending = 0;
  4452. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4453. WC_SHA3_512_DIGEST_SIZE, HEAP_HINT);
  4454. WC_INIT_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4455. WC_SHA3_512_DIGEST_SIZE, HEAP_HINT);
  4456. /* clear for done cleanup */
  4457. XMEMSET(hash, 0, sizeof(hash));
  4458. if (digest_stream) {
  4459. /* init keys */
  4460. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4461. ret = wc_InitSha3_512(&hash[i], HEAP_HINT,
  4462. useDeviceID ? devId : INVALID_DEVID);
  4463. if (ret != 0) {
  4464. printf("InitSha3_512 failed, ret = %d\n", ret);
  4465. goto exit;
  4466. }
  4467. }
  4468. bench_stats_start(&count, &start);
  4469. do {
  4470. for (times = 0; times < numBlocks || pending > 0; ) {
  4471. bench_async_poll(&pending);
  4472. /* while free pending slots in queue, submit ops */
  4473. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4474. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  4475. 0, &times, numBlocks, &pending)) {
  4476. ret = wc_Sha3_512_Update(&hash[i], bench_plain,
  4477. bench_size);
  4478. if (!bench_async_handle(&ret,
  4479. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  4480. &times, &pending)) {
  4481. goto exit_sha3_512;
  4482. }
  4483. }
  4484. } /* for i */
  4485. } /* for times */
  4486. count += times;
  4487. times = 0;
  4488. do {
  4489. bench_async_poll(&pending);
  4490. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4491. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  4492. 0, &times, numBlocks, &pending)) {
  4493. ret = wc_Sha3_512_Final(&hash[i], digest[i]);
  4494. if (!bench_async_handle(&ret,
  4495. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  4496. &times, &pending)) {
  4497. goto exit_sha3_512;
  4498. }
  4499. }
  4500. } /* for i */
  4501. } while (pending > 0);
  4502. } while (bench_stats_check(start));
  4503. }
  4504. else {
  4505. bench_stats_start(&count, &start);
  4506. do {
  4507. for (times = 0; times < numBlocks; times++) {
  4508. ret = wc_InitSha3_512(hash, HEAP_HINT,
  4509. useDeviceID ? devId : INVALID_DEVID);
  4510. if (ret == 0)
  4511. ret = wc_Sha3_512_Update(hash, bench_plain, bench_size);
  4512. if (ret == 0)
  4513. ret = wc_Sha3_512_Final(hash, digest[0]);
  4514. if (ret != 0)
  4515. goto exit_sha3_512;
  4516. } /* for times */
  4517. count += times;
  4518. } while (bench_stats_check(start));
  4519. }
  4520. exit_sha3_512:
  4521. bench_stats_sym_finish("SHA3-512", useDeviceID, count, bench_size,
  4522. start, ret);
  4523. exit:
  4524. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4525. wc_Sha3_512_Free(&hash[i]);
  4526. }
  4527. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  4528. }
  4529. #endif /* WOLFSSL_NOSHA3_512 */
  4530. #ifndef WOLFSSL_NO_SHAKE128
  4531. void bench_shake128(int useDeviceID)
  4532. {
  4533. wc_Shake hash[BENCH_MAX_PENDING];
  4534. double start;
  4535. int ret = 0, i, count = 0, times, pending = 0;
  4536. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4537. WC_SHA3_128_BLOCK_SIZE, HEAP_HINT);
  4538. WC_INIT_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4539. WC_SHA3_128_BLOCK_SIZE, HEAP_HINT);
  4540. /* clear for done cleanup */
  4541. XMEMSET(hash, 0, sizeof(hash));
  4542. if (digest_stream) {
  4543. /* init keys */
  4544. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4545. ret = wc_InitShake128(&hash[i], HEAP_HINT,
  4546. useDeviceID ? devId : INVALID_DEVID);
  4547. if (ret != 0) {
  4548. printf("InitShake128 failed, ret = %d\n", ret);
  4549. goto exit;
  4550. }
  4551. }
  4552. bench_stats_start(&count, &start);
  4553. do {
  4554. for (times = 0; times < numBlocks || pending > 0; ) {
  4555. bench_async_poll(&pending);
  4556. /* while free pending slots in queue, submit ops */
  4557. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4558. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  4559. 0, &times, numBlocks, &pending)) {
  4560. ret = wc_Shake128_Update(&hash[i], bench_plain,
  4561. BENCH_SIZE);
  4562. if (!bench_async_handle(&ret,
  4563. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  4564. &times, &pending)) {
  4565. goto exit_shake128;
  4566. }
  4567. }
  4568. } /* for i */
  4569. } /* for times */
  4570. count += times;
  4571. times = 0;
  4572. do {
  4573. bench_async_poll(&pending);
  4574. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4575. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  4576. 0, &times, numBlocks, &pending)) {
  4577. ret = wc_Shake128_Final(&hash[i], digest[i],
  4578. WC_SHA3_128_BLOCK_SIZE);
  4579. if (!bench_async_handle(&ret,
  4580. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  4581. &times, &pending)) {
  4582. goto exit_shake128;
  4583. }
  4584. }
  4585. } /* for i */
  4586. } while (pending > 0);
  4587. } while (bench_stats_check(start));
  4588. }
  4589. else {
  4590. bench_stats_start(&count, &start);
  4591. do {
  4592. for (times = 0; times < numBlocks; times++) {
  4593. ret = wc_InitShake128(hash, HEAP_HINT,
  4594. useDeviceID ? devId : INVALID_DEVID);
  4595. if (ret == 0)
  4596. ret = wc_Shake128_Update(hash, bench_plain, BENCH_SIZE);
  4597. if (ret == 0)
  4598. ret = wc_Shake128_Final(hash, digest[0],
  4599. WC_SHA3_128_BLOCK_SIZE);
  4600. if (ret != 0)
  4601. goto exit_shake128;
  4602. } /* for times */
  4603. count += times;
  4604. } while (bench_stats_check(start));
  4605. }
  4606. exit_shake128:
  4607. bench_stats_sym_finish("SHAKE128", useDeviceID, count, bench_size,
  4608. start, ret);
  4609. exit:
  4610. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4611. wc_Shake128_Free(&hash[i]);
  4612. }
  4613. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  4614. }
  4615. #endif /* WOLFSSL_NO_SHAKE128 */
  4616. #ifdef WOLFSSL_SHAKE256
  4617. void bench_shake256(int useDeviceID)
  4618. {
  4619. wc_Shake hash[BENCH_MAX_PENDING];
  4620. double start;
  4621. int ret = 0, i, count = 0, times, pending = 0;
  4622. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4623. WC_SHA3_256_BLOCK_SIZE, HEAP_HINT);
  4624. WC_INIT_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4625. WC_SHA3_256_BLOCK_SIZE, HEAP_HINT);
  4626. /* clear for done cleanup */
  4627. XMEMSET(hash, 0, sizeof(hash));
  4628. if (digest_stream) {
  4629. /* init keys */
  4630. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4631. ret = wc_InitShake256(&hash[i], HEAP_HINT,
  4632. useDeviceID ? devId : INVALID_DEVID);
  4633. if (ret != 0) {
  4634. printf("InitShake256 failed, ret = %d\n", ret);
  4635. goto exit;
  4636. }
  4637. }
  4638. bench_stats_start(&count, &start);
  4639. do {
  4640. for (times = 0; times < numBlocks || pending > 0; ) {
  4641. bench_async_poll(&pending);
  4642. /* while free pending slots in queue, submit ops */
  4643. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4644. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  4645. 0, &times, numBlocks, &pending)) {
  4646. ret = wc_Shake256_Update(&hash[i], bench_plain,
  4647. BENCH_SIZE);
  4648. if (!bench_async_handle(&ret,
  4649. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  4650. &times, &pending)) {
  4651. goto exit_shake256;
  4652. }
  4653. }
  4654. } /* for i */
  4655. } /* for times */
  4656. count += times;
  4657. times = 0;
  4658. do {
  4659. bench_async_poll(&pending);
  4660. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4661. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]),
  4662. 0, &times, numBlocks, &pending)) {
  4663. ret = wc_Shake256_Final(&hash[i], digest[i],
  4664. WC_SHA3_256_BLOCK_SIZE);
  4665. if (!bench_async_handle(&ret,
  4666. BENCH_ASYNC_GET_DEV(&hash[i]), 0,
  4667. &times, &pending)) {
  4668. goto exit_shake256;
  4669. }
  4670. }
  4671. } /* for i */
  4672. } while (pending > 0);
  4673. } while (bench_stats_check(start));
  4674. }
  4675. else {
  4676. bench_stats_start(&count, &start);
  4677. do {
  4678. for (times = 0; times < numBlocks; times++) {
  4679. ret = wc_InitShake256(hash, HEAP_HINT,
  4680. useDeviceID ? devId : INVALID_DEVID);
  4681. if (ret == 0)
  4682. ret = wc_Shake256_Update(hash, bench_plain, BENCH_SIZE);
  4683. if (ret == 0)
  4684. ret = wc_Shake256_Final(hash, digest[0],
  4685. WC_SHA3_256_BLOCK_SIZE);
  4686. if (ret != 0)
  4687. goto exit_shake256;
  4688. } /* for times */
  4689. count += times;
  4690. } while (bench_stats_check(start));
  4691. }
  4692. exit_shake256:
  4693. bench_stats_sym_finish("SHAKE256", useDeviceID, count, bench_size,
  4694. start, ret);
  4695. exit:
  4696. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4697. wc_Shake256_Free(&hash[i]);
  4698. }
  4699. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  4700. }
  4701. #endif /* WOLFSSL_SHAKE256 */
  4702. #endif
  4703. #ifdef WOLFSSL_RIPEMD
  4704. int bench_ripemd(void)
  4705. {
  4706. RipeMd hash;
  4707. byte digest[RIPEMD_DIGEST_SIZE];
  4708. double start;
  4709. int i, count, ret = 0;
  4710. if (digest_stream) {
  4711. ret = wc_InitRipeMd(&hash);
  4712. if (ret != 0) {
  4713. return ret;
  4714. }
  4715. bench_stats_start(&count, &start);
  4716. do {
  4717. for (i = 0; i < numBlocks; i++) {
  4718. ret = wc_RipeMdUpdate(&hash, bench_plain, bench_size);
  4719. if (ret != 0) {
  4720. return ret;
  4721. }
  4722. }
  4723. ret = wc_RipeMdFinal(&hash, digest);
  4724. if (ret != 0) {
  4725. return ret;
  4726. }
  4727. count += i;
  4728. } while (bench_stats_check(start));
  4729. }
  4730. else {
  4731. bench_stats_start(&count, &start);
  4732. do {
  4733. for (i = 0; i < numBlocks; i++) {
  4734. ret = wc_InitRipeMd(&hash);
  4735. if (ret != 0) {
  4736. return ret;
  4737. }
  4738. ret = wc_RipeMdUpdate(&hash, bench_plain, bench_size);
  4739. if (ret != 0) {
  4740. return ret;
  4741. }
  4742. ret = wc_RipeMdFinal(&hash, digest);
  4743. if (ret != 0) {
  4744. return ret;
  4745. }
  4746. }
  4747. count += i;
  4748. } while (bench_stats_check(start));
  4749. }
  4750. bench_stats_sym_finish("RIPEMD", 0, count, bench_size, start, ret);
  4751. return 0;
  4752. }
  4753. #endif
  4754. #ifdef HAVE_BLAKE2
  4755. void bench_blake2b(void)
  4756. {
  4757. Blake2b b2b;
  4758. byte digest[64];
  4759. double start;
  4760. int ret = 0, i, count;
  4761. if (digest_stream) {
  4762. ret = wc_InitBlake2b(&b2b, 64);
  4763. if (ret != 0) {
  4764. printf("InitBlake2b failed, ret = %d\n", ret);
  4765. return;
  4766. }
  4767. bench_stats_start(&count, &start);
  4768. do {
  4769. for (i = 0; i < numBlocks; i++) {
  4770. ret = wc_Blake2bUpdate(&b2b, bench_plain, bench_size);
  4771. if (ret != 0) {
  4772. printf("Blake2bUpdate failed, ret = %d\n", ret);
  4773. return;
  4774. }
  4775. }
  4776. ret = wc_Blake2bFinal(&b2b, digest, 64);
  4777. if (ret != 0) {
  4778. printf("Blake2bFinal failed, ret = %d\n", ret);
  4779. return;
  4780. }
  4781. count += i;
  4782. } while (bench_stats_check(start));
  4783. }
  4784. else {
  4785. bench_stats_start(&count, &start);
  4786. do {
  4787. for (i = 0; i < numBlocks; i++) {
  4788. ret = wc_InitBlake2b(&b2b, 64);
  4789. if (ret != 0) {
  4790. printf("InitBlake2b failed, ret = %d\n", ret);
  4791. return;
  4792. }
  4793. ret = wc_Blake2bUpdate(&b2b, bench_plain, bench_size);
  4794. if (ret != 0) {
  4795. printf("Blake2bUpdate failed, ret = %d\n", ret);
  4796. return;
  4797. }
  4798. ret = wc_Blake2bFinal(&b2b, digest, 64);
  4799. if (ret != 0) {
  4800. printf("Blake2bFinal failed, ret = %d\n", ret);
  4801. return;
  4802. }
  4803. }
  4804. count += i;
  4805. } while (bench_stats_check(start));
  4806. }
  4807. bench_stats_sym_finish("BLAKE2b", 0, count, bench_size, start, ret);
  4808. }
  4809. #endif
  4810. #if defined(HAVE_BLAKE2S)
  4811. void bench_blake2s(void)
  4812. {
  4813. Blake2s b2s;
  4814. byte digest[32];
  4815. double start;
  4816. int ret = 0, i, count;
  4817. if (digest_stream) {
  4818. ret = wc_InitBlake2s(&b2s, 32);
  4819. if (ret != 0) {
  4820. printf("InitBlake2s failed, ret = %d\n", ret);
  4821. return;
  4822. }
  4823. bench_stats_start(&count, &start);
  4824. do {
  4825. for (i = 0; i < numBlocks; i++) {
  4826. ret = wc_Blake2sUpdate(&b2s, bench_plain, bench_size);
  4827. if (ret != 0) {
  4828. printf("Blake2sUpdate failed, ret = %d\n", ret);
  4829. return;
  4830. }
  4831. }
  4832. ret = wc_Blake2sFinal(&b2s, digest, 32);
  4833. if (ret != 0) {
  4834. printf("Blake2sFinal failed, ret = %d\n", ret);
  4835. return;
  4836. }
  4837. count += i;
  4838. } while (bench_stats_check(start));
  4839. }
  4840. else {
  4841. bench_stats_start(&count, &start);
  4842. do {
  4843. for (i = 0; i < numBlocks; i++) {
  4844. ret = wc_InitBlake2s(&b2s, 32);
  4845. if (ret != 0) {
  4846. printf("InitBlake2b failed, ret = %d\n", ret);
  4847. return;
  4848. }
  4849. ret = wc_Blake2sUpdate(&b2s, bench_plain, bench_size);
  4850. if (ret != 0) {
  4851. printf("Blake2bUpdate failed, ret = %d\n", ret);
  4852. return;
  4853. }
  4854. ret = wc_Blake2sFinal(&b2s, digest, 32);
  4855. if (ret != 0) {
  4856. printf("Blake2sFinal failed, ret = %d\n", ret);
  4857. return;
  4858. }
  4859. }
  4860. count += i;
  4861. } while (bench_stats_check(start));
  4862. }
  4863. bench_stats_sym_finish("BLAKE2s", 0, count, bench_size, start, ret);
  4864. }
  4865. #endif
  4866. #ifdef WOLFSSL_CMAC
  4867. static void bench_cmac_helper(int keySz, const char* outMsg, int useDeviceID)
  4868. {
  4869. Cmac cmac;
  4870. byte digest[AES_BLOCK_SIZE];
  4871. word32 digestSz = sizeof(digest);
  4872. double start;
  4873. int ret, i, count;
  4874. #ifdef WOLFSSL_SECO_CAAM
  4875. unsigned int keyID;
  4876. int keyGroup = 1; /* group one was chosen arbitrarily */
  4877. int keyInfo = CAAM_KEY_TRANSIENT;
  4878. int keyType = CAAM_KEYTYPE_AES128;
  4879. byte pubKey[AES_256_KEY_SIZE];
  4880. if (keySz == AES_256_KEY_SIZE) {
  4881. keyType = CAAM_KEYTYPE_AES256;
  4882. }
  4883. if (useDeviceID &&
  4884. wc_SECO_GenerateKey(CAAM_GENERATE_KEY, keyGroup, pubKey, 0, keyType,
  4885. keyInfo, &keyID) != 0) {
  4886. printf("Error generating key in hsm\n");
  4887. return;
  4888. }
  4889. #endif
  4890. (void)useDeviceID;
  4891. bench_stats_start(&count, &start);
  4892. do {
  4893. #ifdef HAVE_FIPS
  4894. ret = wc_InitCmac(&cmac, bench_key, keySz, WC_CMAC_AES, NULL);
  4895. #else
  4896. ret = wc_InitCmac_ex(&cmac, bench_key, keySz, WC_CMAC_AES, NULL,
  4897. HEAP_HINT, useDeviceID ? devId : INVALID_DEVID);
  4898. #endif
  4899. if (ret != 0) {
  4900. printf("InitCmac failed, ret = %d\n", ret);
  4901. return;
  4902. }
  4903. #ifdef WOLFSSL_SECO_CAAM
  4904. if (useDeviceID) {
  4905. wc_SECO_CMACSetKeyID(&cmac, keyID);
  4906. }
  4907. #endif
  4908. for (i = 0; i < numBlocks; i++) {
  4909. ret = wc_CmacUpdate(&cmac, bench_plain, bench_size);
  4910. if (ret != 0) {
  4911. printf("CmacUpdate failed, ret = %d\n", ret);
  4912. return;
  4913. }
  4914. }
  4915. /* Note: final force zero's the Cmac struct */
  4916. ret = wc_CmacFinal(&cmac, digest, &digestSz);
  4917. if (ret != 0) {
  4918. printf("CmacFinal failed, ret = %d\n", ret);
  4919. return;
  4920. }
  4921. count += i;
  4922. } while (bench_stats_check(start));
  4923. bench_stats_sym_finish(outMsg, 0, count, bench_size, start, ret);
  4924. }
  4925. void bench_cmac(int useDeviceID)
  4926. {
  4927. #ifdef WOLFSSL_AES_128
  4928. bench_cmac_helper(16, "AES-128-CMAC", useDeviceID);
  4929. #endif
  4930. #ifdef WOLFSSL_AES_256
  4931. bench_cmac_helper(32, "AES-256-CMAC", useDeviceID);
  4932. #endif
  4933. }
  4934. #endif /* WOLFSSL_CMAC */
  4935. #ifdef HAVE_SCRYPT
  4936. void bench_scrypt(void)
  4937. {
  4938. byte derived[64];
  4939. double start;
  4940. int ret, i, count;
  4941. bench_stats_start(&count, &start);
  4942. do {
  4943. for (i = 0; i < scryptCnt; i++) {
  4944. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  4945. (byte*)"SodiumChloride", 14, 14, 8, 1,
  4946. sizeof(derived));
  4947. if (ret != 0) {
  4948. printf("scrypt failed, ret = %d\n", ret);
  4949. goto exit;
  4950. }
  4951. }
  4952. count += i;
  4953. } while (bench_stats_check(start));
  4954. exit:
  4955. bench_stats_asym_finish("scrypt", 17, "", 0, count, start, ret);
  4956. }
  4957. #endif /* HAVE_SCRYPT */
  4958. #ifndef NO_HMAC
  4959. static void bench_hmac(int useDeviceID, int type, int digestSz,
  4960. byte* key, word32 keySz, const char* label)
  4961. {
  4962. Hmac hmac[BENCH_MAX_PENDING];
  4963. double start;
  4964. int ret = 0, i, count = 0, times, pending = 0;
  4965. #ifdef WOLFSSL_ASYNC_CRYPT
  4966. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4967. WC_MAX_DIGEST_SIZE, HEAP_HINT);
  4968. WC_INIT_ARRAY(digest, byte, BENCH_MAX_PENDING,
  4969. WC_MAX_DIGEST_SIZE, HEAP_HINT);
  4970. #else
  4971. byte digest[BENCH_MAX_PENDING][WC_MAX_DIGEST_SIZE];
  4972. #endif
  4973. (void)digestSz;
  4974. /* clear for done cleanup */
  4975. XMEMSET(hmac, 0, sizeof(hmac));
  4976. /* init keys */
  4977. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4978. ret = wc_HmacInit(&hmac[i], HEAP_HINT,
  4979. useDeviceID ? devId : INVALID_DEVID);
  4980. if (ret != 0) {
  4981. printf("wc_HmacInit failed for %s, ret = %d\n", label, ret);
  4982. goto exit;
  4983. }
  4984. ret = wc_HmacSetKey(&hmac[i], type, key, keySz);
  4985. if (ret != 0) {
  4986. printf("wc_HmacSetKey failed for %s, ret = %d\n", label, ret);
  4987. goto exit;
  4988. }
  4989. }
  4990. bench_stats_start(&count, &start);
  4991. do {
  4992. for (times = 0; times < numBlocks || pending > 0; ) {
  4993. bench_async_poll(&pending);
  4994. /* while free pending slots in queue, submit ops */
  4995. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4996. if (bench_async_check(&ret,
  4997. BENCH_ASYNC_GET_DEV(&hmac[i]), 0,
  4998. &times, numBlocks, &pending)) {
  4999. ret = wc_HmacUpdate(&hmac[i], bench_plain, bench_size);
  5000. if (!bench_async_handle(&ret,
  5001. BENCH_ASYNC_GET_DEV(&hmac[i]),
  5002. 0, &times, &pending)) {
  5003. goto exit_hmac;
  5004. }
  5005. }
  5006. } /* for i */
  5007. } /* for times */
  5008. count += times;
  5009. times = 0;
  5010. do {
  5011. bench_async_poll(&pending);
  5012. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5013. if (bench_async_check(&ret,
  5014. BENCH_ASYNC_GET_DEV(&hmac[i]), 0,
  5015. &times, numBlocks, &pending)) {
  5016. ret = wc_HmacFinal(&hmac[i], digest[i]);
  5017. if (!bench_async_handle(&ret,
  5018. BENCH_ASYNC_GET_DEV(&hmac[i]),
  5019. 0, &times, &pending)) {
  5020. goto exit_hmac;
  5021. }
  5022. }
  5023. } /* for i */
  5024. } while (pending > 0);
  5025. } while (bench_stats_check(start));
  5026. exit_hmac:
  5027. bench_stats_sym_finish(label, useDeviceID, count, bench_size, start, ret);
  5028. exit:
  5029. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5030. wc_HmacFree(&hmac[i]);
  5031. }
  5032. #ifdef WOLFSSL_ASYNC_CRYPT
  5033. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5034. #endif
  5035. }
  5036. #ifndef NO_MD5
  5037. void bench_hmac_md5(int useDeviceID)
  5038. {
  5039. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5040. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  5041. bench_hmac(useDeviceID, WC_MD5, WC_MD5_DIGEST_SIZE, key, sizeof(key),
  5042. "HMAC-MD5");
  5043. }
  5044. #endif /* NO_MD5 */
  5045. #ifndef NO_SHA
  5046. void bench_hmac_sha(int useDeviceID)
  5047. {
  5048. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5049. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5050. 0x0b, 0x0b, 0x0b, 0x0b };
  5051. bench_hmac(useDeviceID, WC_SHA, WC_SHA_DIGEST_SIZE, key, sizeof(key),
  5052. "HMAC-SHA");
  5053. }
  5054. #endif /* NO_SHA */
  5055. #ifdef WOLFSSL_SHA224
  5056. void bench_hmac_sha224(int useDeviceID)
  5057. {
  5058. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5059. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5060. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5061. 0x0b, 0x0b, 0x0b, 0x0b };
  5062. bench_hmac(useDeviceID, WC_SHA224,
  5063. WC_SHA224_DIGEST_SIZE, key, sizeof(key),
  5064. "HMAC-SHA224");
  5065. }
  5066. #endif /* WOLFSSL_SHA224 */
  5067. #ifndef NO_SHA256
  5068. void bench_hmac_sha256(int useDeviceID)
  5069. {
  5070. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5071. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5072. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5073. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  5074. bench_hmac(useDeviceID, WC_SHA256, WC_SHA256_DIGEST_SIZE, key, sizeof(key),
  5075. "HMAC-SHA256");
  5076. }
  5077. #endif /* NO_SHA256 */
  5078. #ifdef WOLFSSL_SHA384
  5079. void bench_hmac_sha384(int useDeviceID)
  5080. {
  5081. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5082. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5083. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5084. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5085. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5086. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  5087. bench_hmac(useDeviceID, WC_SHA384, WC_SHA384_DIGEST_SIZE, key, sizeof(key),
  5088. "HMAC-SHA384");
  5089. }
  5090. #endif /* WOLFSSL_SHA384 */
  5091. #ifdef WOLFSSL_SHA512
  5092. void bench_hmac_sha512(int useDeviceID)
  5093. {
  5094. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5095. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5096. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5097. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5098. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5099. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5100. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  5101. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  5102. bench_hmac(useDeviceID, WC_SHA512, WC_SHA512_DIGEST_SIZE, key, sizeof(key),
  5103. "HMAC-SHA512");
  5104. }
  5105. #endif /* WOLFSSL_SHA512 */
  5106. #ifndef NO_PWDBASED
  5107. void bench_pbkdf2(void)
  5108. {
  5109. double start;
  5110. int ret = 0, count = 0;
  5111. const char* passwd32 = "passwordpasswordpasswordpassword";
  5112. const byte salt32[] = { 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06,
  5113. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06,
  5114. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06,
  5115. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  5116. byte derived[32];
  5117. bench_stats_start(&count, &start);
  5118. do {
  5119. ret = wc_PBKDF2(derived, (const byte*)passwd32, (int)XSTRLEN(passwd32),
  5120. salt32, (int)sizeof(salt32), 1000, 32, WC_SHA256);
  5121. count++;
  5122. } while (bench_stats_check(start));
  5123. bench_stats_sym_finish("PBKDF2", 32, count, 32, start, ret);
  5124. }
  5125. #endif /* !NO_PWDBASED */
  5126. #endif /* NO_HMAC */
  5127. #ifdef WOLFSSL_SIPHASH
  5128. void bench_siphash(void)
  5129. {
  5130. double start;
  5131. int ret = 0, count;
  5132. const char* passwd16 = "passwordpassword";
  5133. byte out[16];
  5134. int i;
  5135. bench_stats_start(&count, &start);
  5136. do {
  5137. for (i = 0; i < numBlocks; i++) {
  5138. ret = wc_SipHash((const byte*)passwd16, bench_plain, bench_size,
  5139. out, 8);
  5140. }
  5141. count += i;
  5142. } while (bench_stats_check(start));
  5143. bench_stats_sym_finish("SipHash-8", 1, count, bench_size, start, ret);
  5144. bench_stats_start(&count, &start);
  5145. do {
  5146. for (i = 0; i < numBlocks; i++) {
  5147. ret = wc_SipHash((const byte*)passwd16, bench_plain, bench_size,
  5148. out, 16);
  5149. }
  5150. count += i;
  5151. } while (bench_stats_check(start));
  5152. bench_stats_sym_finish("SipHash-16", 1, count, bench_size, start, ret);
  5153. }
  5154. #endif
  5155. #ifndef NO_RSA
  5156. #if defined(WOLFSSL_KEY_GEN)
  5157. static void bench_rsaKeyGen_helper(int useDeviceID, int keySz)
  5158. {
  5159. RsaKey genKey[BENCH_MAX_PENDING];
  5160. double start;
  5161. int ret = 0, i, count = 0, times, pending = 0;
  5162. const long rsa_e_val = WC_RSA_EXPONENT;
  5163. const char**desc = bench_desc_words[lng_index];
  5164. /* clear for done cleanup */
  5165. XMEMSET(genKey, 0, sizeof(genKey));
  5166. bench_stats_start(&count, &start);
  5167. do {
  5168. /* while free pending slots in queue, submit ops */
  5169. for (times = 0; times < genTimes || pending > 0; ) {
  5170. bench_async_poll(&pending);
  5171. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5172. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]),
  5173. 0, &times, genTimes, &pending)) {
  5174. wc_FreeRsaKey(&genKey[i]);
  5175. ret = wc_InitRsaKey_ex(&genKey[i], HEAP_HINT, devId);
  5176. if (ret < 0) {
  5177. goto exit;
  5178. }
  5179. ret = wc_MakeRsaKey(&genKey[i], keySz, rsa_e_val, &gRng);
  5180. if (!bench_async_handle(&ret,
  5181. BENCH_ASYNC_GET_DEV(&genKey[i]), 0,
  5182. &times, &pending)) {
  5183. goto exit;
  5184. }
  5185. }
  5186. } /* for i */
  5187. } /* for times */
  5188. count += times;
  5189. } while (bench_stats_check(start));
  5190. exit:
  5191. bench_stats_asym_finish("RSA", keySz, desc[2], useDeviceID, count,
  5192. start, ret);
  5193. /* cleanup */
  5194. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5195. wc_FreeRsaKey(&genKey[i]);
  5196. }
  5197. }
  5198. void bench_rsaKeyGen(int useDeviceID)
  5199. {
  5200. int k, keySz;
  5201. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  5202. const int keySizes[2] = {1024, 2048};
  5203. #else
  5204. const int keySizes[1] = {2048};
  5205. #endif
  5206. for (k = 0; k < (int)(sizeof(keySizes)/sizeof(int)); k++) {
  5207. keySz = keySizes[k];
  5208. bench_rsaKeyGen_helper(useDeviceID, keySz);
  5209. }
  5210. }
  5211. void bench_rsaKeyGen_size(int useDeviceID, int keySz)
  5212. {
  5213. bench_rsaKeyGen_helper(useDeviceID, keySz);
  5214. }
  5215. #endif /* WOLFSSL_KEY_GEN */
  5216. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  5217. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  5218. #if defined(WOLFSSL_MDK_SHELL)
  5219. static char *certRSAname = "certs/rsa2048.der";
  5220. /* set by shell command */
  5221. static void set_Bench_RSA_File(char * cert) { certRSAname = cert ; }
  5222. #elif defined(FREESCALE_MQX)
  5223. static char *certRSAname = "a:\\certs\\rsa2048.der";
  5224. #else
  5225. static const char *certRSAname = "certs/rsa2048.der";
  5226. #endif
  5227. #endif
  5228. #define RSA_BUF_SIZE 384 /* for up to 3072 bit */
  5229. #if defined(WOLFSSL_RSA_VERIFY_INLINE) || defined(WOLFSSL_RSA_PUBLIC_ONLY)
  5230. #if defined(USE_CERT_BUFFERS_2048)
  5231. static unsigned char rsa_2048_sig[] = {
  5232. 0x8c, 0x9e, 0x37, 0xbf, 0xc3, 0xa6, 0xba, 0x1c,
  5233. 0x53, 0x22, 0x40, 0x4b, 0x8b, 0x0d, 0x3c, 0x0e,
  5234. 0x2e, 0x8c, 0x31, 0x2c, 0x47, 0xbf, 0x03, 0x48,
  5235. 0x18, 0x46, 0x73, 0x8d, 0xd7, 0xdd, 0x17, 0x64,
  5236. 0x0d, 0x7f, 0xdc, 0x74, 0xed, 0x80, 0xc3, 0xe8,
  5237. 0x9a, 0x18, 0x33, 0xd4, 0xe6, 0xc5, 0xe1, 0x54,
  5238. 0x75, 0xd1, 0xbb, 0x40, 0xde, 0xa8, 0xb9, 0x1b,
  5239. 0x14, 0xe8, 0xc1, 0x39, 0xeb, 0xa0, 0x69, 0x8a,
  5240. 0xc6, 0x9b, 0xef, 0x53, 0xb5, 0x23, 0x2b, 0x78,
  5241. 0x06, 0x43, 0x37, 0x11, 0x81, 0x84, 0x73, 0x33,
  5242. 0x33, 0xfe, 0xf7, 0x5d, 0x2b, 0x84, 0xd6, 0x83,
  5243. 0xd6, 0xdd, 0x55, 0x33, 0xef, 0xd1, 0xf7, 0x12,
  5244. 0xb0, 0xc2, 0x0e, 0xb1, 0x78, 0xd4, 0xa8, 0xa3,
  5245. 0x25, 0xeb, 0xed, 0x9a, 0xb3, 0xee, 0xc3, 0x7e,
  5246. 0xce, 0x13, 0x18, 0x86, 0x31, 0xe1, 0xef, 0x01,
  5247. 0x0f, 0x6e, 0x67, 0x24, 0x74, 0xbd, 0x0b, 0x7f,
  5248. 0xa9, 0xca, 0x6f, 0xaa, 0x83, 0x28, 0x90, 0x40,
  5249. 0xf1, 0xb5, 0x10, 0x0e, 0x26, 0x03, 0x05, 0x5d,
  5250. 0x87, 0xb4, 0xe0, 0x4c, 0x98, 0xd8, 0xc6, 0x42,
  5251. 0x89, 0x77, 0xeb, 0xb6, 0xd4, 0xe6, 0x26, 0xf3,
  5252. 0x31, 0x25, 0xde, 0x28, 0x38, 0x58, 0xe8, 0x2c,
  5253. 0xf4, 0x56, 0x7c, 0xb6, 0xfd, 0x99, 0xb0, 0xb0,
  5254. 0xf4, 0x83, 0xb6, 0x74, 0xa9, 0x5b, 0x9f, 0xe8,
  5255. 0xe9, 0xf1, 0xa1, 0x2a, 0xbd, 0xf6, 0x83, 0x28,
  5256. 0x09, 0xda, 0xa6, 0xd6, 0xcd, 0x61, 0x60, 0xf7,
  5257. 0x13, 0x4e, 0x46, 0x57, 0x38, 0x1e, 0x11, 0x92,
  5258. 0x6b, 0x6b, 0xcf, 0xd3, 0xf4, 0x8b, 0x66, 0x03,
  5259. 0x25, 0xa3, 0x7a, 0x2f, 0xce, 0xc1, 0x85, 0xa5,
  5260. 0x48, 0x91, 0x8a, 0xb3, 0x4f, 0x5d, 0x98, 0xb1,
  5261. 0x69, 0x58, 0x47, 0x69, 0x0c, 0x52, 0xdc, 0x42,
  5262. 0x4c, 0xef, 0xe8, 0xd4, 0x4d, 0x6a, 0x33, 0x7d,
  5263. 0x9e, 0xd2, 0x51, 0xe6, 0x41, 0xbf, 0x4f, 0xa2
  5264. };
  5265. #elif defined(USE_CERT_BUFFERS_3072)
  5266. static unsigned char rsa_3072_sig[] = {
  5267. 0x1a, 0xd6, 0x0d, 0xfd, 0xe3, 0x41, 0x95, 0x76,
  5268. 0x27, 0x16, 0x7d, 0xc7, 0x94, 0x16, 0xca, 0xa8,
  5269. 0x26, 0x08, 0xbe, 0x78, 0x87, 0x72, 0x4c, 0xd9,
  5270. 0xa7, 0xfc, 0x33, 0x77, 0x2d, 0x53, 0x07, 0xb5,
  5271. 0x8c, 0xce, 0x48, 0x17, 0x9b, 0xff, 0x9f, 0x9b,
  5272. 0x17, 0xc4, 0xbb, 0x72, 0xed, 0xdb, 0xa0, 0x34,
  5273. 0x69, 0x5b, 0xc7, 0x4e, 0xbf, 0xec, 0x13, 0xc5,
  5274. 0x98, 0x71, 0x9a, 0x4e, 0x18, 0x0e, 0xcb, 0xe7,
  5275. 0xc6, 0xd5, 0x21, 0x31, 0x7c, 0x0d, 0xae, 0x14,
  5276. 0x2b, 0x87, 0x4f, 0x77, 0x95, 0x2e, 0x26, 0xe2,
  5277. 0x83, 0xfe, 0x49, 0x1e, 0x87, 0x19, 0x4a, 0x63,
  5278. 0x73, 0x75, 0xf1, 0xf5, 0x71, 0xd2, 0xce, 0xd4,
  5279. 0x39, 0x2b, 0xd9, 0xe0, 0x76, 0x70, 0xc8, 0xf8,
  5280. 0xed, 0xdf, 0x90, 0x57, 0x17, 0xb9, 0x16, 0xf6,
  5281. 0xe9, 0x49, 0x48, 0xce, 0x5a, 0x8b, 0xe4, 0x84,
  5282. 0x7c, 0xf3, 0x31, 0x68, 0x97, 0x45, 0x68, 0x38,
  5283. 0x50, 0x3a, 0x70, 0xbd, 0xb3, 0xd3, 0xd2, 0xe0,
  5284. 0x56, 0x5b, 0xc2, 0x0c, 0x2c, 0x10, 0x70, 0x7b,
  5285. 0xd4, 0x99, 0xf9, 0x38, 0x31, 0xb1, 0x86, 0xa0,
  5286. 0x07, 0xf1, 0xf6, 0x53, 0xb0, 0x44, 0x82, 0x40,
  5287. 0xd2, 0xab, 0x0e, 0x71, 0x5d, 0xe1, 0xea, 0x3a,
  5288. 0x77, 0xc9, 0xef, 0xfe, 0x54, 0x65, 0xa3, 0x49,
  5289. 0xfd, 0xa5, 0x33, 0xaa, 0x16, 0x1a, 0x38, 0xe7,
  5290. 0xaa, 0xb7, 0x13, 0xb2, 0x3b, 0xc7, 0x00, 0x87,
  5291. 0x12, 0xfe, 0xfd, 0xf4, 0x55, 0x6d, 0x1d, 0x4a,
  5292. 0x0e, 0xad, 0xd0, 0x4c, 0x55, 0x91, 0x60, 0xd9,
  5293. 0xef, 0x74, 0x69, 0x22, 0x8c, 0x51, 0x65, 0xc2,
  5294. 0x04, 0xac, 0xd3, 0x8d, 0xf7, 0x35, 0x29, 0x13,
  5295. 0x6d, 0x61, 0x7c, 0x39, 0x2f, 0x41, 0x4c, 0xdf,
  5296. 0x38, 0xfd, 0x1a, 0x7d, 0x42, 0xa7, 0x6f, 0x3f,
  5297. 0x3d, 0x9b, 0xd1, 0x97, 0xab, 0xc0, 0xa7, 0x28,
  5298. 0x1c, 0xc0, 0x02, 0x26, 0xeb, 0xce, 0xf9, 0xe1,
  5299. 0x34, 0x45, 0xaf, 0xbf, 0x8d, 0xb8, 0xe0, 0xff,
  5300. 0xd9, 0x6f, 0x77, 0xf3, 0xf7, 0xed, 0x6a, 0xbb,
  5301. 0x03, 0x52, 0xfb, 0x38, 0xfc, 0xea, 0x9f, 0xc9,
  5302. 0x98, 0xed, 0x21, 0x45, 0xaf, 0x43, 0x2b, 0x64,
  5303. 0x96, 0x82, 0x30, 0xe9, 0xb4, 0x36, 0x89, 0x77,
  5304. 0x07, 0x4a, 0xc6, 0x1f, 0x38, 0x7a, 0xee, 0xb6,
  5305. 0x86, 0xf6, 0x2f, 0x03, 0xec, 0xa2, 0xe5, 0x48,
  5306. 0xe5, 0x5a, 0xf5, 0x1c, 0xd2, 0xd9, 0xd8, 0x2d,
  5307. 0x9d, 0x06, 0x07, 0xc9, 0x8b, 0x5d, 0xe0, 0x0f,
  5308. 0x5e, 0x0c, 0x53, 0x27, 0xff, 0x23, 0xee, 0xca,
  5309. 0x5e, 0x4d, 0xf1, 0x95, 0x77, 0x78, 0x1f, 0xf2,
  5310. 0x44, 0x5b, 0x7d, 0x01, 0x49, 0x61, 0x6f, 0x6d,
  5311. 0xbf, 0xf5, 0x19, 0x06, 0x39, 0xe9, 0xe9, 0x29,
  5312. 0xde, 0x47, 0x5e, 0x2e, 0x1f, 0x68, 0xf4, 0x32,
  5313. 0x5e, 0xe9, 0xd0, 0xa7, 0xb4, 0x2a, 0x45, 0xdf,
  5314. 0x15, 0x7d, 0x0d, 0x5b, 0xef, 0xc6, 0x23, 0xac
  5315. };
  5316. #else
  5317. #error Not Supported Yet!
  5318. #endif
  5319. #endif /* WOLFSSL_RSA_VERIFY_INLINE || WOLFSSL_RSA_PUBLIC_ONLY */
  5320. static void bench_rsa_helper(int useDeviceID, RsaKey rsaKey[BENCH_MAX_PENDING],
  5321. int rsaKeySz)
  5322. {
  5323. int ret = 0, i, times, count = 0, pending = 0;
  5324. word32 idx = 0;
  5325. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  5326. const char* messageStr = TEST_STRING;
  5327. const int len = (int)TEST_STRING_SZ;
  5328. #endif
  5329. double start = 0.0F;
  5330. const char**desc = bench_desc_words[lng_index];
  5331. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  5332. WC_DECLARE_VAR(message, byte, TEST_STRING_SZ, HEAP_HINT);
  5333. #endif
  5334. WC_DECLARE_ARRAY_DYNAMIC_DEC(enc, byte, BENCH_MAX_PENDING,
  5335. rsaKeySz, HEAP_HINT);
  5336. #if ( !defined(WOLFSSL_RSA_VERIFY_INLINE) \
  5337. && !defined(WOLFSSL_RSA_PUBLIC_ONLY) )
  5338. WC_DECLARE_ARRAY_DYNAMIC_DEC(out, byte, BENCH_MAX_PENDING,
  5339. rsaKeySz, HEAP_HINT);
  5340. #else
  5341. byte* out[BENCH_MAX_PENDING];
  5342. #endif
  5343. WC_DECLARE_ARRAY_DYNAMIC_EXE(enc, byte, BENCH_MAX_PENDING,
  5344. rsaKeySz, HEAP_HINT);
  5345. #if ( !defined(WOLFSSL_RSA_VERIFY_INLINE) \
  5346. && !defined(WOLFSSL_RSA_PUBLIC_ONLY) )
  5347. WC_DECLARE_ARRAY_DYNAMIC_EXE(out, byte, BENCH_MAX_PENDING,
  5348. rsaKeySz, HEAP_HINT);
  5349. if (out[0] == NULL) {
  5350. ret = MEMORY_E;
  5351. goto exit;
  5352. }
  5353. #endif
  5354. if (enc[0] == NULL) {
  5355. ret = MEMORY_E;
  5356. goto exit;
  5357. }
  5358. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  5359. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  5360. if (message == NULL) {
  5361. ret = MEMORY_E;
  5362. goto exit;
  5363. }
  5364. #endif
  5365. XMEMCPY(message, messageStr, len);
  5366. #endif
  5367. if (!rsa_sign_verify) {
  5368. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  5369. /* begin public RSA */
  5370. bench_stats_start(&count, &start);
  5371. do {
  5372. for (times = 0; times < ntimes || pending > 0; ) {
  5373. bench_async_poll(&pending);
  5374. /* while free pending slots in queue, submit ops */
  5375. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5376. if (bench_async_check(&ret,
  5377. BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  5378. 1, &times, ntimes, &pending)) {
  5379. ret = wc_RsaPublicEncrypt(message, (word32)len, enc[i],
  5380. rsaKeySz/8, &rsaKey[i],
  5381. GLOBAL_RNG);
  5382. if (!bench_async_handle(&ret,
  5383. BENCH_ASYNC_GET_DEV(
  5384. &rsaKey[i]), 1, &times,
  5385. &pending)) {
  5386. goto exit_rsa_verify;
  5387. }
  5388. }
  5389. } /* for i */
  5390. } /* for times */
  5391. count += times;
  5392. } while (bench_stats_check(start));
  5393. exit_rsa_verify:
  5394. bench_stats_asym_finish("RSA", rsaKeySz, desc[0],
  5395. useDeviceID, count, start, ret);
  5396. #endif /* !WOLFSSL_RSA_VERIFY_ONLY */
  5397. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  5398. if (ret < 0) {
  5399. goto exit;
  5400. }
  5401. /* capture resulting encrypt length */
  5402. idx = (word32)(rsaKeySz/8);
  5403. /* begin private async RSA */
  5404. bench_stats_start(&count, &start);
  5405. do {
  5406. for (times = 0; times < ntimes || pending > 0; ) {
  5407. bench_async_poll(&pending);
  5408. /* while free pending slots in queue, submit ops */
  5409. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5410. if (bench_async_check(&ret,
  5411. BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  5412. 1, &times, ntimes, &pending)) {
  5413. ret = wc_RsaPrivateDecrypt(enc[i], idx, out[i],
  5414. rsaKeySz/8, &rsaKey[i]);
  5415. if (!bench_async_handle(&ret,
  5416. BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  5417. 1, &times, &pending)) {
  5418. goto exit_rsa_pub;
  5419. }
  5420. }
  5421. } /* for i */
  5422. } /* for times */
  5423. count += times;
  5424. } while (bench_stats_check(start));
  5425. exit_rsa_pub:
  5426. bench_stats_asym_finish("RSA", rsaKeySz, desc[1],
  5427. useDeviceID, count, start, ret);
  5428. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY */
  5429. }
  5430. else {
  5431. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  5432. /* begin RSA sign */
  5433. bench_stats_start(&count, &start);
  5434. do {
  5435. for (times = 0; times < ntimes || pending > 0; ) {
  5436. bench_async_poll(&pending);
  5437. /* while free pending slots in queue, submit ops */
  5438. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5439. if (bench_async_check(&ret,
  5440. BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  5441. 1, &times, ntimes, &pending)) {
  5442. ret = wc_RsaSSL_Sign(message, len, enc[i],
  5443. rsaKeySz/8, &rsaKey[i], &gRng);
  5444. if (!bench_async_handle(&ret,
  5445. BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  5446. 1, &times, &pending)) {
  5447. goto exit_rsa_sign;
  5448. }
  5449. }
  5450. } /* for i */
  5451. } /* for times */
  5452. count += times;
  5453. } while (bench_stats_check(start));
  5454. exit_rsa_sign:
  5455. bench_stats_asym_finish("RSA", rsaKeySz, desc[4], useDeviceID,
  5456. count, start, ret);
  5457. if (ret < 0) {
  5458. goto exit;
  5459. }
  5460. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  5461. /* capture resulting encrypt length */
  5462. idx = rsaKeySz/8;
  5463. /* begin RSA verify */
  5464. bench_stats_start(&count, &start);
  5465. do {
  5466. for (times = 0; times < ntimes || pending > 0; ) {
  5467. bench_async_poll(&pending);
  5468. /* while free pending slots in queue, submit ops */
  5469. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5470. if (bench_async_check(&ret,
  5471. BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  5472. 1, &times, ntimes, &pending)) {
  5473. #if !defined(WOLFSSL_RSA_VERIFY_INLINE) && \
  5474. !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  5475. ret = wc_RsaSSL_Verify(enc[i], idx, out[i],
  5476. rsaKeySz/8, &rsaKey[i]);
  5477. #elif defined(USE_CERT_BUFFERS_2048)
  5478. XMEMCPY(enc[i], rsa_2048_sig, sizeof(rsa_2048_sig));
  5479. idx = sizeof(rsa_2048_sig);
  5480. out[i] = NULL;
  5481. ret = wc_RsaSSL_VerifyInline(enc[i], idx,
  5482. &out[i], &rsaKey[i]);
  5483. if (ret > 0) {
  5484. ret = 0;
  5485. }
  5486. #elif defined(USE_CERT_BUFFERS_3072)
  5487. XMEMCPY(enc[i], rsa_3072_sig, sizeof(rsa_3072_sig));
  5488. idx = sizeof(rsa_3072_sig);
  5489. out[i] = NULL;
  5490. ret = wc_RsaSSL_VerifyInline(enc[i], idx,
  5491. &out[i], &rsaKey[i]);
  5492. if (ret > 0)
  5493. ret = 0;
  5494. #endif
  5495. if (!bench_async_handle(&ret,
  5496. BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  5497. 1, &times, &pending)) {
  5498. goto exit_rsa_verifyinline;
  5499. }
  5500. }
  5501. } /* for i */
  5502. } /* for times */
  5503. count += times;
  5504. } while (bench_stats_check(start));
  5505. exit_rsa_verifyinline:
  5506. bench_stats_asym_finish("RSA", rsaKeySz, desc[5],
  5507. useDeviceID, count, start, ret);
  5508. }
  5509. exit:
  5510. WC_FREE_ARRAY_DYNAMIC(enc, BENCH_MAX_PENDING, HEAP_HINT);
  5511. #if !defined(WOLFSSL_RSA_VERIFY_INLINE) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  5512. WC_FREE_ARRAY_DYNAMIC(out, BENCH_MAX_PENDING, HEAP_HINT);
  5513. #endif
  5514. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  5515. WC_FREE_VAR(message, HEAP_HINT);
  5516. #endif
  5517. }
  5518. void bench_rsa(int useDeviceID)
  5519. {
  5520. int i;
  5521. RsaKey rsaKey[BENCH_MAX_PENDING];
  5522. int ret = 0;
  5523. int rsaKeySz = 0;
  5524. const byte* tmp;
  5525. size_t bytes;
  5526. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  5527. word32 idx;
  5528. #endif
  5529. #ifdef USE_CERT_BUFFERS_1024
  5530. tmp = rsa_key_der_1024;
  5531. bytes = (size_t)sizeof_rsa_key_der_1024;
  5532. rsaKeySz = 1024;
  5533. #elif defined(USE_CERT_BUFFERS_2048)
  5534. tmp = rsa_key_der_2048;
  5535. bytes = (size_t)sizeof_rsa_key_der_2048;
  5536. rsaKeySz = 2048;
  5537. #elif defined(USE_CERT_BUFFERS_3072)
  5538. tmp = rsa_key_der_3072;
  5539. bytes = (size_t)sizeof_rsa_key_der_3072;
  5540. rsaKeySz = 3072;
  5541. #elif defined(USE_CERT_BUFFERS_4096)
  5542. tmp = client_key_der_4096;
  5543. bytes = (size_t)sizeof_client_key_der_4096;
  5544. rsaKeySz = 4096;
  5545. #else
  5546. #error "need a cert buffer size"
  5547. #endif /* USE_CERT_BUFFERS */
  5548. /* clear for done cleanup */
  5549. XMEMSET(rsaKey, 0, sizeof(rsaKey));
  5550. /* init keys */
  5551. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5552. /* setup an async context for each key */
  5553. ret = wc_InitRsaKey_ex(&rsaKey[i], HEAP_HINT,
  5554. useDeviceID ? devId : INVALID_DEVID);
  5555. if (ret < 0) {
  5556. goto exit_bench_rsa;
  5557. }
  5558. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  5559. #ifdef WC_RSA_BLINDING
  5560. ret = wc_RsaSetRNG(&rsaKey[i], &gRng);
  5561. if (ret != 0)
  5562. goto exit_bench_rsa;
  5563. #endif
  5564. #endif
  5565. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  5566. /* decode the private key */
  5567. idx = 0;
  5568. if ((ret = wc_RsaPrivateKeyDecode(tmp, &idx,
  5569. &rsaKey[i], (word32)bytes)) != 0) {
  5570. printf("wc_RsaPrivateKeyDecode failed! %d\n", ret);
  5571. goto exit_bench_rsa;
  5572. }
  5573. #elif defined(WOLFSSL_PUBLIC_MP)
  5574. /* get offset to public portion of the RSA key */
  5575. #ifdef USE_CERT_BUFFERS_1024
  5576. bytes = 11;
  5577. #elif defined(USE_CERT_BUFFERS_2048) || defined(USE_CERT_BUFFERS_3072)
  5578. bytes = 12;
  5579. #endif
  5580. ret = mp_read_unsigned_bin(&rsaKey[i].n, &tmp[bytes], rsaKeySz/8);
  5581. if (ret != 0) {
  5582. printf("wc_RsaPrivateKeyDecode failed! %d\n", ret);
  5583. goto exit_bench_rsa;
  5584. }
  5585. ret = mp_set_int(&rsaKey[i].e, WC_RSA_EXPONENT);
  5586. if (ret != 0) {
  5587. printf("wc_RsaPrivateKeyDecode failed! %d\n", ret);
  5588. goto exit_bench_rsa;
  5589. }
  5590. #else
  5591. /* Note: To benchmark public only define WOLFSSL_PUBLIC_MP */
  5592. rsaKeySz = 0;
  5593. #endif
  5594. }
  5595. if (rsaKeySz > 0) {
  5596. bench_rsa_helper(useDeviceID, rsaKey, rsaKeySz);
  5597. }
  5598. (void)bytes;
  5599. (void)tmp;
  5600. exit_bench_rsa:
  5601. /* cleanup */
  5602. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5603. wc_FreeRsaKey(&rsaKey[i]);
  5604. }
  5605. }
  5606. #ifdef WOLFSSL_KEY_GEN
  5607. /* bench any size of RSA key */
  5608. void bench_rsa_key(int useDeviceID, int rsaKeySz)
  5609. {
  5610. int ret = 0, i, pending = 0;
  5611. RsaKey rsaKey[BENCH_MAX_PENDING];
  5612. int isPending[BENCH_MAX_PENDING];
  5613. long exp = 65537L;
  5614. /* clear for done cleanup */
  5615. XMEMSET(rsaKey, 0, sizeof(rsaKey));
  5616. XMEMSET(isPending, 0, sizeof(isPending));
  5617. /* init keys */
  5618. do {
  5619. pending = 0;
  5620. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5621. if (!isPending[i]) { /* if making the key is pending then just call
  5622. * wc_MakeRsaKey again */
  5623. /* setup an async context for each key */
  5624. if (wc_InitRsaKey_ex(&rsaKey[i], HEAP_HINT,
  5625. useDeviceID ? devId : INVALID_DEVID) < 0) {
  5626. goto exit_bench_rsa_key;
  5627. }
  5628. #ifdef WC_RSA_BLINDING
  5629. ret = wc_RsaSetRNG(&rsaKey[i], &gRng);
  5630. if (ret != 0)
  5631. goto exit_bench_rsa_key;
  5632. #endif
  5633. }
  5634. /* create the RSA key */
  5635. ret = wc_MakeRsaKey(&rsaKey[i], rsaKeySz, exp, &gRng);
  5636. if (ret == WC_PENDING_E) {
  5637. isPending[i] = 1;
  5638. pending = 1;
  5639. }
  5640. else if (ret != 0) {
  5641. printf("wc_MakeRsaKey failed! %d\n", ret);
  5642. goto exit_bench_rsa_key;
  5643. }
  5644. } /* for i */
  5645. } while (pending > 0);
  5646. bench_rsa_helper(useDeviceID, rsaKey, rsaKeySz);
  5647. exit_bench_rsa_key:
  5648. /* cleanup */
  5649. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5650. wc_FreeRsaKey(&rsaKey[i]);
  5651. }
  5652. }
  5653. #endif /* WOLFSSL_KEY_GEN */
  5654. #endif /* !NO_RSA */
  5655. #ifndef NO_DH
  5656. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  5657. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  5658. #if defined(WOLFSSL_MDK_SHELL)
  5659. static char *certDHname = "certs/dh2048.der";
  5660. /* set by shell command */
  5661. void set_Bench_DH_File(char * cert) { certDHname = cert ; }
  5662. #elif defined(FREESCALE_MQX)
  5663. static char *certDHname = "a:\\certs\\dh2048.der";
  5664. #elif defined(NO_ASN)
  5665. /* do nothing, but don't need a file */
  5666. #else
  5667. static const char *certDHname = "certs/dh2048.der";
  5668. #endif
  5669. #endif
  5670. #ifdef HAVE_FFDHE_4096
  5671. #define BENCH_DH_KEY_SIZE 512 /* for 4096 bit */
  5672. #else
  5673. #define BENCH_DH_KEY_SIZE 384 /* for 3072 bit */
  5674. #endif
  5675. #define BENCH_DH_PRIV_SIZE (BENCH_DH_KEY_SIZE/8)
  5676. void bench_dh(int useDeviceID)
  5677. {
  5678. int ret = 0, i;
  5679. int count = 0, times, pending = 0;
  5680. const byte* tmp = NULL;
  5681. double start = 0.0F;
  5682. DhKey dhKey[BENCH_MAX_PENDING];
  5683. int dhKeySz = BENCH_DH_KEY_SIZE * 8; /* used in printf */
  5684. const char**desc = bench_desc_words[lng_index];
  5685. #ifndef NO_ASN
  5686. size_t bytes = 0;
  5687. word32 idx;
  5688. #endif
  5689. word32 pubSz[BENCH_MAX_PENDING];
  5690. word32 privSz[BENCH_MAX_PENDING];
  5691. word32 pubSz2 = BENCH_DH_KEY_SIZE;
  5692. word32 privSz2 = BENCH_DH_PRIV_SIZE;
  5693. word32 agreeSz[BENCH_MAX_PENDING];
  5694. #if defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072)
  5695. #ifdef HAVE_PUBLIC_FFDHE
  5696. const DhParams *params = NULL;
  5697. #else
  5698. int paramName = 0;
  5699. #endif
  5700. #endif
  5701. WC_DECLARE_ARRAY(pub, byte, BENCH_MAX_PENDING,
  5702. BENCH_DH_KEY_SIZE, HEAP_HINT);
  5703. WC_DECLARE_VAR(pub2, byte,
  5704. BENCH_DH_KEY_SIZE, HEAP_HINT);
  5705. WC_DECLARE_ARRAY(agree, byte, BENCH_MAX_PENDING,
  5706. BENCH_DH_KEY_SIZE, HEAP_HINT);
  5707. WC_DECLARE_ARRAY(priv, byte, BENCH_MAX_PENDING,
  5708. BENCH_DH_PRIV_SIZE, HEAP_HINT);
  5709. WC_DECLARE_VAR(priv2, byte,
  5710. BENCH_DH_PRIV_SIZE, HEAP_HINT);
  5711. WC_INIT_ARRAY(pub, byte,
  5712. BENCH_MAX_PENDING, BENCH_DH_KEY_SIZE, HEAP_HINT);
  5713. WC_INIT_ARRAY(agree, byte,
  5714. BENCH_MAX_PENDING, BENCH_DH_KEY_SIZE, HEAP_HINT);
  5715. WC_INIT_ARRAY(priv, byte,
  5716. BENCH_MAX_PENDING, BENCH_DH_PRIV_SIZE, HEAP_HINT);
  5717. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  5718. if (pub[0] == NULL || pub2 == NULL || agree[0] == NULL || priv[0] == NULL || priv2 == NULL) {
  5719. ret = MEMORY_E;
  5720. goto exit;
  5721. }
  5722. #endif
  5723. (void)tmp;
  5724. if (!use_ffdhe) {
  5725. #if defined(NO_ASN)
  5726. dhKeySz = 1024;
  5727. /* do nothing, but don't use default FILE */
  5728. #elif defined(USE_CERT_BUFFERS_1024)
  5729. tmp = dh_key_der_1024;
  5730. bytes = (size_t)sizeof_dh_key_der_1024;
  5731. dhKeySz = 1024;
  5732. #elif defined(USE_CERT_BUFFERS_2048)
  5733. tmp = dh_key_der_2048;
  5734. bytes = (size_t)sizeof_dh_key_der_2048;
  5735. dhKeySz = 2048;
  5736. #elif defined(USE_CERT_BUFFERS_3072)
  5737. tmp = dh_key_der_3072;
  5738. bytes = (size_t)sizeof_dh_key_der_3072;
  5739. dhKeySz = 3072;
  5740. #elif defined(USE_CERT_BUFFERS_4096)
  5741. tmp = dh_key_der_4096;
  5742. bytes = (size_t)sizeof_dh_key_der_4096;
  5743. dhKeySz = 4096;
  5744. #else
  5745. #error "need to define a cert buffer size"
  5746. #endif /* USE_CERT_BUFFERS */
  5747. }
  5748. #ifdef HAVE_FFDHE_2048
  5749. else if (use_ffdhe == 2048) {
  5750. #ifdef HAVE_PUBLIC_FFDHE
  5751. params = wc_Dh_ffdhe2048_Get();
  5752. #else
  5753. paramName = WC_FFDHE_2048;
  5754. #endif
  5755. dhKeySz = 2048;
  5756. }
  5757. #endif
  5758. #ifdef HAVE_FFDHE_3072
  5759. else if (use_ffdhe == 3072) {
  5760. #ifdef HAVE_PUBLIC_FFDHE
  5761. params = wc_Dh_ffdhe3072_Get();
  5762. #else
  5763. paramName = WC_FFDHE_3072;
  5764. #endif
  5765. dhKeySz = 3072;
  5766. }
  5767. #endif
  5768. #ifdef HAVE_FFDHE_4096
  5769. else if (use_ffdhe == 4096) {
  5770. #ifdef HAVE_PUBLIC_FFDHE
  5771. params = wc_Dh_ffdhe4096_Get();
  5772. #else
  5773. paramName = WC_FFDHE_4096;
  5774. #endif
  5775. dhKeySz = 4096;
  5776. }
  5777. #endif
  5778. /* clear for done cleanup */
  5779. XMEMSET(dhKey, 0, sizeof(dhKey));
  5780. /* init keys */
  5781. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5782. /* setup an async context for each key */
  5783. ret = wc_InitDhKey_ex(&dhKey[i], HEAP_HINT,
  5784. useDeviceID ? devId : INVALID_DEVID);
  5785. if (ret != 0)
  5786. goto exit;
  5787. /* setup key */
  5788. if (!use_ffdhe) {
  5789. #ifdef NO_ASN
  5790. ret = wc_DhSetKey(&dhKey[i], dh_p,
  5791. sizeof(dh_p), dh_g, sizeof(dh_g));
  5792. #else
  5793. idx = 0;
  5794. ret = wc_DhKeyDecode(tmp, &idx, &dhKey[i], (word32)bytes);
  5795. #endif
  5796. }
  5797. #if defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072)
  5798. #ifdef HAVE_PUBLIC_FFDHE
  5799. else if (params != NULL) {
  5800. ret = wc_DhSetKey(&dhKey[i], params->p, params->p_len,
  5801. params->g, params->g_len);
  5802. }
  5803. #else
  5804. else if (paramName != 0) {
  5805. ret = wc_DhSetNamedKey(&dhKey[i], paramName);
  5806. }
  5807. #endif
  5808. #endif
  5809. if (ret != 0) {
  5810. printf("DhKeyDecode failed %d, can't benchmark\n", ret);
  5811. goto exit;
  5812. }
  5813. }
  5814. /* Key Gen */
  5815. bench_stats_start(&count, &start);
  5816. PRIVATE_KEY_UNLOCK();
  5817. do {
  5818. /* while free pending slots in queue, submit ops */
  5819. for (times = 0; times < genTimes || pending > 0; ) {
  5820. bench_async_poll(&pending);
  5821. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5822. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]),
  5823. 0, &times, genTimes, &pending)) {
  5824. privSz[i] = BENCH_DH_PRIV_SIZE;
  5825. pubSz[i] = BENCH_DH_KEY_SIZE;
  5826. ret = wc_DhGenerateKeyPair(&dhKey[i], &gRng,
  5827. priv[i], &privSz[i],
  5828. pub[i], &pubSz[i]);
  5829. if (!bench_async_handle(&ret,
  5830. BENCH_ASYNC_GET_DEV(&dhKey[i]),
  5831. 0, &times, &pending)) {
  5832. goto exit_dh_gen;
  5833. }
  5834. }
  5835. } /* for i */
  5836. } /* for times */
  5837. count += times;
  5838. } while (bench_stats_check(start));
  5839. PRIVATE_KEY_LOCK();
  5840. exit_dh_gen:
  5841. bench_stats_asym_finish("DH", dhKeySz, desc[2],
  5842. useDeviceID, count, start, ret);
  5843. if (ret < 0) {
  5844. goto exit;
  5845. }
  5846. /* Generate key to use as other public */
  5847. PRIVATE_KEY_UNLOCK();
  5848. ret = wc_DhGenerateKeyPair(&dhKey[0], &gRng,
  5849. priv2, &privSz2, pub2, &pubSz2);
  5850. PRIVATE_KEY_LOCK();
  5851. #ifdef WOLFSSL_ASYNC_CRYPT
  5852. ret = wc_AsyncWait(ret, &dhKey[0].asyncDev, WC_ASYNC_FLAG_NONE);
  5853. #endif
  5854. /* Key Agree */
  5855. bench_stats_start(&count, &start);
  5856. PRIVATE_KEY_UNLOCK();
  5857. do {
  5858. for (times = 0; times < agreeTimes || pending > 0; ) {
  5859. bench_async_poll(&pending);
  5860. /* while free pending slots in queue, submit ops */
  5861. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5862. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]),
  5863. 0, &times, agreeTimes, &pending)) {
  5864. ret = wc_DhAgree(&dhKey[i], agree[i], &agreeSz[i], priv[i],
  5865. privSz[i], pub2, pubSz2);
  5866. if (!bench_async_handle(&ret,
  5867. BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, &times, &pending)) {
  5868. goto exit;
  5869. }
  5870. }
  5871. } /* for i */
  5872. } /* for times */
  5873. count += times;
  5874. } while (bench_stats_check(start));
  5875. PRIVATE_KEY_LOCK();
  5876. exit:
  5877. bench_stats_asym_finish("DH", dhKeySz, desc[3],
  5878. useDeviceID, count, start, ret);
  5879. /* cleanup */
  5880. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5881. wc_FreeDhKey(&dhKey[i]);
  5882. }
  5883. WC_FREE_ARRAY(pub, BENCH_MAX_PENDING, HEAP_HINT);
  5884. WC_FREE_VAR(pub2, HEAP_HINT);
  5885. WC_FREE_ARRAY(priv, BENCH_MAX_PENDING, HEAP_HINT);
  5886. WC_FREE_VAR(priv2, HEAP_HINT);
  5887. WC_FREE_ARRAY(agree, BENCH_MAX_PENDING, HEAP_HINT);
  5888. }
  5889. #endif /* !NO_DH */
  5890. #ifdef WOLFSSL_HAVE_KYBER
  5891. static void bench_kyber_keygen(int type, const char* name, int keySize,
  5892. KyberKey* key)
  5893. {
  5894. int ret = 0, times, count, pending = 0;
  5895. double start;
  5896. const char**desc = bench_desc_words[lng_index];
  5897. /* KYBER Make Key */
  5898. bench_stats_start(&count, &start);
  5899. do {
  5900. /* while free pending slots in queue, submit ops */
  5901. for (times = 0; times < agreeTimes || pending > 0; times++) {
  5902. wc_KyberKey_Free(key);
  5903. ret = wc_KyberKey_Init(type, key, HEAP_HINT, INVALID_DEVID);
  5904. if (ret != 0)
  5905. goto exit;
  5906. #ifdef KYBER_NONDETERMINISTIC
  5907. ret = wc_KyberKey_MakeKey(key, &gRng);
  5908. #else
  5909. unsigned char rand[KYBER_MAKEKEY_RAND_SZ] = {0,};
  5910. ret = wc_KyberKey_MakeKeyWithRandom(key, rand, sizeof(rand));
  5911. #endif
  5912. if (ret != 0)
  5913. goto exit;
  5914. } /* for times */
  5915. count += times;
  5916. }
  5917. while (bench_stats_check(start));
  5918. exit:
  5919. bench_stats_asym_finish(name, keySize, desc[2], 0, count, start, ret);
  5920. }
  5921. static void bench_kyber_encap(const char* name, int keySize, KyberKey* key)
  5922. {
  5923. int ret = 0, times, count, pending = 0;
  5924. double start;
  5925. const char**desc = bench_desc_words[lng_index];
  5926. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  5927. byte ss[KYBER_SS_SZ];
  5928. word32 ctSz;
  5929. ret = wc_KyberKey_CipherTextSize(key, &ctSz);
  5930. if (ret != 0) {
  5931. return;
  5932. }
  5933. /* KYBER Encapsulate */
  5934. bench_stats_start(&count, &start);
  5935. do {
  5936. /* while free pending slots in queue, submit ops */
  5937. for (times = 0; times < agreeTimes || pending > 0; times++) {
  5938. #ifdef KYBER_NONDETERMINISTIC
  5939. ret = wc_KyberKey_Encapsulate(key, ct, ss, &gRng);
  5940. #else
  5941. unsigned char rand[KYBER_ENC_RAND_SZ] = {0,};
  5942. ret = wc_KyberKey_EncapsulateWithRandom(key, ct, ss, rand,
  5943. sizeof(rand));
  5944. #endif
  5945. if (ret != 0)
  5946. goto exit_encap;
  5947. } /* for times */
  5948. count += times;
  5949. }
  5950. while (bench_stats_check(start));
  5951. exit_encap:
  5952. bench_stats_asym_finish(name, keySize, desc[9], 0, count, start, ret);
  5953. /* KYBER Decapsulate */
  5954. bench_stats_start(&count, &start);
  5955. do {
  5956. /* while free pending slots in queue, submit ops */
  5957. for (times = 0; times < agreeTimes || pending > 0; times++) {
  5958. ret = wc_KyberKey_Decapsulate(key, ss, ct, ctSz);
  5959. if (ret != 0)
  5960. goto exit_decap;
  5961. } /* for times */
  5962. count += times;
  5963. }
  5964. while (bench_stats_check(start));
  5965. exit_decap:
  5966. bench_stats_asym_finish(name, keySize, desc[13], 0, count, start, ret);
  5967. }
  5968. void bench_kyber(int type)
  5969. {
  5970. KyberKey key;
  5971. const char* name = NULL;
  5972. int keySize = 0;
  5973. switch (type) {
  5974. #ifdef WOLFSSL_KYBER512
  5975. case KYBER512:
  5976. name = "KYBER512 ";
  5977. keySize = 128;
  5978. break;
  5979. #endif
  5980. #ifdef WOLFSSL_KYBER768
  5981. case KYBER768:
  5982. name = "KYBER768 ";
  5983. keySize = 192;
  5984. break;
  5985. #endif
  5986. #ifdef WOLFSSL_KYBER1024
  5987. case KYBER1024:
  5988. name = "KYBER1024";
  5989. keySize = 256;
  5990. break;
  5991. #endif
  5992. }
  5993. bench_kyber_keygen(type, name, keySize, &key);
  5994. bench_kyber_encap(name, keySize, &key);
  5995. wc_KyberKey_Free(&key);
  5996. }
  5997. #endif
  5998. #ifdef HAVE_ECC
  5999. /* +8 for 'ECDSA [%s]' and null terminator */
  6000. #define BENCH_ECC_NAME_SZ (ECC_MAXNAME + 8)
  6001. /* run all benchmarks on a curve */
  6002. void bench_ecc_curve(int curveId)
  6003. {
  6004. if (bench_all || (bench_asym_algs & BENCH_ECC_MAKEKEY)) {
  6005. #ifndef NO_SW_BENCH
  6006. bench_eccMakeKey(0, curveId);
  6007. #endif
  6008. #if defined(BENCH_DEVID)
  6009. bench_eccMakeKey(1, curveId);
  6010. #endif
  6011. }
  6012. if (bench_all || (bench_asym_algs & BENCH_ECC)) {
  6013. #ifndef NO_SW_BENCH
  6014. bench_ecc(0, curveId);
  6015. #endif
  6016. #if defined(BENCH_DEVID)
  6017. bench_ecc(1, curveId);
  6018. #endif
  6019. }
  6020. #ifdef HAVE_ECC_ENCRYPT
  6021. if (bench_all || (bench_asym_algs & BENCH_ECC_ENCRYPT))
  6022. bench_eccEncrypt(curveId);
  6023. #endif
  6024. }
  6025. void bench_eccMakeKey(int useDeviceID, int curveId)
  6026. {
  6027. int ret = 0, i, times, count, pending = 0;
  6028. int deviceID;
  6029. int keySize;
  6030. ecc_key genKey[BENCH_MAX_PENDING];
  6031. char name[BENCH_ECC_NAME_SZ];
  6032. double start;
  6033. const char**desc = bench_desc_words[lng_index];
  6034. deviceID = useDeviceID ? devId : INVALID_DEVID;
  6035. keySize = wc_ecc_get_curve_size_from_id(curveId);
  6036. /* clear for done cleanup */
  6037. XMEMSET(&genKey, 0, sizeof(genKey));
  6038. /* ECC Make Key */
  6039. bench_stats_start(&count, &start);
  6040. do {
  6041. /* while free pending slots in queue, submit ops */
  6042. for (times = 0; times < agreeTimes || pending > 0; ) {
  6043. bench_async_poll(&pending);
  6044. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6045. if (bench_async_check(&ret,
  6046. BENCH_ASYNC_GET_DEV(&genKey[i]), 0,
  6047. &times, agreeTimes, &pending)) {
  6048. wc_ecc_free(&genKey[i]);
  6049. ret = wc_ecc_init_ex(&genKey[i], HEAP_HINT, deviceID);
  6050. if (ret < 0) {
  6051. goto exit;
  6052. }
  6053. ret = wc_ecc_make_key_ex(&gRng, keySize, &genKey[i],
  6054. curveId);
  6055. if (!bench_async_handle(&ret,
  6056. BENCH_ASYNC_GET_DEV(&genKey[i]), 0, &times,
  6057. &pending)) {
  6058. goto exit;
  6059. }
  6060. }
  6061. } /* for i */
  6062. } /* for times */
  6063. count += times;
  6064. } while (bench_stats_check(start));
  6065. exit:
  6066. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECC [%15s]",
  6067. wc_ecc_get_name(curveId));
  6068. bench_stats_asym_finish(name, keySize * 8, desc[2],
  6069. useDeviceID, count, start, ret);
  6070. /* cleanup */
  6071. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6072. wc_ecc_free(&genKey[i]);
  6073. }
  6074. }
  6075. void bench_ecc(int useDeviceID, int curveId)
  6076. {
  6077. int ret = 0, i, times, count, pending = 0;
  6078. int deviceID;
  6079. int keySize;
  6080. char name[BENCH_ECC_NAME_SZ];
  6081. ecc_key genKey[BENCH_MAX_PENDING];
  6082. #ifdef HAVE_ECC_DHE
  6083. ecc_key genKey2[BENCH_MAX_PENDING];
  6084. #endif
  6085. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  6086. #ifdef HAVE_ECC_VERIFY
  6087. int verify[BENCH_MAX_PENDING];
  6088. #endif
  6089. #endif
  6090. word32 x[BENCH_MAX_PENDING];
  6091. double start = 0;
  6092. const char**desc = bench_desc_words[lng_index];
  6093. #ifdef HAVE_ECC_DHE
  6094. WC_DECLARE_ARRAY(shared, byte,
  6095. BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  6096. #endif
  6097. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  6098. WC_DECLARE_ARRAY(sig, byte,
  6099. BENCH_MAX_PENDING, ECC_MAX_SIG_SIZE, HEAP_HINT);
  6100. WC_DECLARE_ARRAY(digest, byte,
  6101. BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  6102. #endif
  6103. #ifdef HAVE_ECC_DHE
  6104. WC_INIT_ARRAY(shared, byte,
  6105. BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  6106. #endif
  6107. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  6108. WC_INIT_ARRAY(sig, byte, BENCH_MAX_PENDING, ECC_MAX_SIG_SIZE, HEAP_HINT);
  6109. WC_INIT_ARRAY(digest, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  6110. #endif
  6111. deviceID = useDeviceID ? devId : INVALID_DEVID;
  6112. /* clear for done cleanup */
  6113. XMEMSET(&genKey, 0, sizeof(genKey));
  6114. #ifdef HAVE_ECC_DHE
  6115. XMEMSET(&genKey2, 0, sizeof(genKey2));
  6116. #endif
  6117. keySize = wc_ecc_get_curve_size_from_id(curveId);
  6118. /* init keys */
  6119. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6120. /* setup an context for each key */
  6121. if ((ret = wc_ecc_init_ex(&genKey[i], HEAP_HINT, deviceID)) < 0) {
  6122. goto exit;
  6123. }
  6124. ret = wc_ecc_make_key_ex(&gRng, keySize, &genKey[i], curveId);
  6125. #ifdef WOLFSSL_ASYNC_CRYPT
  6126. ret = wc_AsyncWait(ret, &genKey[i].asyncDev, WC_ASYNC_FLAG_NONE);
  6127. #endif
  6128. if (ret < 0) {
  6129. goto exit;
  6130. }
  6131. #ifdef HAVE_ECC_DHE
  6132. if ((ret = wc_ecc_init_ex(&genKey2[i], HEAP_HINT, deviceID)) < 0) {
  6133. goto exit;
  6134. }
  6135. if ((ret = wc_ecc_make_key_ex(&gRng, keySize, &genKey2[i],
  6136. curveId)) > 0) {
  6137. goto exit;
  6138. }
  6139. #endif
  6140. }
  6141. #ifdef HAVE_ECC_DHE
  6142. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  6143. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  6144. !defined(HAVE_SELFTEST)
  6145. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6146. (void)wc_ecc_set_rng(&genKey[i], &gRng);
  6147. }
  6148. #endif
  6149. /* ECC Shared Secret */
  6150. bench_stats_start(&count, &start);
  6151. PRIVATE_KEY_UNLOCK();
  6152. do {
  6153. for (times = 0; times < agreeTimes || pending > 0; ) {
  6154. bench_async_poll(&pending);
  6155. /* while free pending slots in queue, submit ops */
  6156. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6157. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1,
  6158. &times, agreeTimes, &pending)) {
  6159. x[i] = (word32)keySize;
  6160. ret = wc_ecc_shared_secret(&genKey[i], &genKey2[i],
  6161. shared[i], &x[i]);
  6162. if (!bench_async_handle(&ret,
  6163. BENCH_ASYNC_GET_DEV(&genKey[i]), 1, &times,
  6164. &pending)) {
  6165. goto exit_ecdhe;
  6166. }
  6167. }
  6168. } /* for i */
  6169. } /* for times */
  6170. count += times;
  6171. } while (bench_stats_check(start));
  6172. PRIVATE_KEY_UNLOCK();
  6173. exit_ecdhe:
  6174. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDHE [%15s]",
  6175. wc_ecc_get_name(curveId));
  6176. bench_stats_asym_finish(name, keySize * 8, desc[3],
  6177. useDeviceID, count, start, ret);
  6178. if (ret < 0) {
  6179. goto exit;
  6180. }
  6181. #endif /* HAVE_ECC_DHE */
  6182. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  6183. /* Init digest to sign */
  6184. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6185. for (count = 0; count < keySize; count++) {
  6186. digest[i][count] = (byte)count;
  6187. }
  6188. }
  6189. /* ECC Sign */
  6190. bench_stats_start(&count, &start);
  6191. do {
  6192. for (times = 0; times < agreeTimes || pending > 0; ) {
  6193. bench_async_poll(&pending);
  6194. /* while free pending slots in queue, submit ops */
  6195. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6196. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1,
  6197. &times, agreeTimes, &pending)) {
  6198. if (genKey[i].state == 0) {
  6199. x[i] = ECC_MAX_SIG_SIZE;
  6200. }
  6201. ret = wc_ecc_sign_hash(digest[i], (word32)keySize, sig[i],
  6202. &x[i], &gRng, &genKey[i]);
  6203. if (!bench_async_handle(&ret,
  6204. BENCH_ASYNC_GET_DEV(&genKey[i]), 1, &times,
  6205. &pending)) {
  6206. goto exit_ecdsa_sign;
  6207. }
  6208. } /* bench_async_check */
  6209. } /* for i */
  6210. } /* for times */
  6211. count += times;
  6212. } while (bench_stats_check(start));
  6213. exit_ecdsa_sign:
  6214. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDSA [%15s]",
  6215. wc_ecc_get_name(curveId));
  6216. bench_stats_asym_finish(name, keySize * 8, desc[4],
  6217. useDeviceID, count, start, ret);
  6218. if (ret < 0) {
  6219. goto exit;
  6220. }
  6221. #ifdef HAVE_ECC_VERIFY
  6222. /* ECC Verify */
  6223. bench_stats_start(&count, &start);
  6224. do {
  6225. for (times = 0; times < agreeTimes || pending > 0; ) {
  6226. bench_async_poll(&pending);
  6227. /* while free pending slots in queue, submit ops */
  6228. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6229. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1,
  6230. &times, agreeTimes, &pending)) {
  6231. if (genKey[i].state == 0) {
  6232. verify[i] = 0;
  6233. }
  6234. ret = wc_ecc_verify_hash(sig[i], x[i], digest[i],
  6235. (word32)keySize, &verify[i],
  6236. &genKey[i]);
  6237. if (!bench_async_handle(&ret,
  6238. BENCH_ASYNC_GET_DEV(&genKey[i]),
  6239. 1, &times,
  6240. &pending)) {
  6241. goto exit_ecdsa_verify;
  6242. }
  6243. } /* if bench_async_check */
  6244. } /* for i */
  6245. } /* for times */
  6246. count += times;
  6247. } while (bench_stats_check(start));
  6248. exit_ecdsa_verify:
  6249. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDSA [%15s]",
  6250. wc_ecc_get_name(curveId));
  6251. bench_stats_asym_finish(name, keySize * 8, desc[5],
  6252. useDeviceID, count, start, ret);
  6253. #endif /* HAVE_ECC_VERIFY */
  6254. #endif /* !NO_ASN && HAVE_ECC_SIGN */
  6255. exit:
  6256. /* cleanup */
  6257. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6258. wc_ecc_free(&genKey[i]);
  6259. #ifdef HAVE_ECC_DHE
  6260. wc_ecc_free(&genKey2[i]);
  6261. #endif
  6262. }
  6263. #ifdef HAVE_ECC_DHE
  6264. WC_FREE_ARRAY(shared, BENCH_MAX_PENDING, HEAP_HINT);
  6265. #endif
  6266. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  6267. WC_FREE_ARRAY(sig, BENCH_MAX_PENDING, HEAP_HINT);
  6268. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6269. #endif
  6270. (void)useDeviceID;
  6271. (void)pending;
  6272. (void)x;
  6273. (void)count;
  6274. (void)times;
  6275. (void)desc;
  6276. (void)start;
  6277. (void)name;
  6278. }
  6279. #ifdef HAVE_ECC_ENCRYPT
  6280. void bench_eccEncrypt(int curveId)
  6281. {
  6282. ecc_key userA, userB;
  6283. int keySize;
  6284. byte msg[48];
  6285. byte out[sizeof(msg) + WC_SHA256_DIGEST_SIZE + (MAX_ECC_BITS+3)/4 + 2];
  6286. word32 outSz = sizeof(out);
  6287. word32 bench_plainSz = bench_size;
  6288. int ret, i, count;
  6289. double start;
  6290. const char**desc = bench_desc_words[lng_index];
  6291. char name[BENCH_ECC_NAME_SZ];
  6292. keySize = wc_ecc_get_curve_size_from_id(curveId);
  6293. ret = wc_ecc_init_ex(&userA, HEAP_HINT, devId);
  6294. if (ret != 0) {
  6295. printf("wc_ecc_encrypt make key A failed: %d\n", ret);
  6296. return;
  6297. }
  6298. ret = wc_ecc_init_ex(&userB, HEAP_HINT, devId);
  6299. if (ret != 0) {
  6300. printf("wc_ecc_encrypt make key B failed: %d\n", ret);
  6301. wc_ecc_free(&userA);
  6302. return;
  6303. }
  6304. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  6305. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  6306. !defined(HAVE_SELFTEST)
  6307. ret = wc_ecc_set_rng(&userA, &gRng);
  6308. if (ret != 0) {
  6309. goto exit;
  6310. }
  6311. ret = wc_ecc_set_rng(&userB, &gRng);
  6312. if (ret != 0) {
  6313. goto exit;
  6314. }
  6315. #endif
  6316. ret = wc_ecc_make_key_ex(&gRng, keySize, &userA, curveId);
  6317. #ifdef WOLFSSL_ASYNC_CRYPT
  6318. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_NONE);
  6319. #endif
  6320. if (ret != 0)
  6321. goto exit;
  6322. ret = wc_ecc_make_key_ex(&gRng, keySize, &userB, curveId);
  6323. #ifdef WOLFSSL_ASYNC_CRYPT
  6324. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_NONE);
  6325. #endif
  6326. if (ret != 0)
  6327. goto exit;
  6328. for (i = 0; i < (int)sizeof(msg); i++)
  6329. msg[i] = i;
  6330. bench_stats_start(&count, &start);
  6331. do {
  6332. for (i = 0; i < ntimes; i++) {
  6333. /* encrypt msg to B */
  6334. ret = wc_ecc_encrypt(&userA, &userB, msg, sizeof(msg),
  6335. out, &outSz, NULL);
  6336. if (ret != 0) {
  6337. printf("wc_ecc_encrypt failed! %d\n", ret);
  6338. goto exit_enc;
  6339. }
  6340. }
  6341. count += i;
  6342. } while (bench_stats_check(start));
  6343. exit_enc:
  6344. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECC [%15s]",
  6345. wc_ecc_get_name(curveId));
  6346. bench_stats_asym_finish(name, keySize * 8, desc[6], 0, count, start, ret);
  6347. bench_stats_start(&count, &start);
  6348. do {
  6349. for (i = 0; i < ntimes; i++) {
  6350. /* decrypt msg from A */
  6351. ret = wc_ecc_decrypt(&userB, &userA, out, outSz, bench_plain,
  6352. &bench_plainSz, NULL);
  6353. if (ret != 0) {
  6354. printf("wc_ecc_decrypt failed! %d\n", ret);
  6355. goto exit_dec;
  6356. }
  6357. }
  6358. count += i;
  6359. } while (bench_stats_check(start));
  6360. exit_dec:
  6361. bench_stats_asym_finish(name, keySize * 8, desc[7], 0, count, start, ret);
  6362. exit:
  6363. /* cleanup */
  6364. wc_ecc_free(&userB);
  6365. wc_ecc_free(&userA);
  6366. }
  6367. #endif
  6368. #endif /* HAVE_ECC */
  6369. #ifdef HAVE_CURVE25519
  6370. void bench_curve25519KeyGen(int useDeviceID)
  6371. {
  6372. curve25519_key genKey;
  6373. double start;
  6374. int ret = 0, i, count;
  6375. const char**desc = bench_desc_words[lng_index];
  6376. /* Key Gen */
  6377. bench_stats_start(&count, &start);
  6378. do {
  6379. for (i = 0; i < genTimes; i++) {
  6380. ret = wc_curve25519_init_ex(&genKey, HEAP_HINT,
  6381. useDeviceID ? devId : INVALID_DEVID);
  6382. if (ret != 0) {
  6383. printf("wc_curve25519_init_ex failed: %d\n", ret);
  6384. break;
  6385. }
  6386. ret = wc_curve25519_make_key(&gRng, 32, &genKey);
  6387. wc_curve25519_free(&genKey);
  6388. if (ret != 0) {
  6389. printf("wc_curve25519_make_key failed: %d\n", ret);
  6390. break;
  6391. }
  6392. }
  6393. count += i;
  6394. } while (bench_stats_check(start));
  6395. bench_stats_asym_finish("CURVE", 25519, desc[2], useDeviceID, count, start,
  6396. ret);
  6397. }
  6398. #ifdef HAVE_CURVE25519_SHARED_SECRET
  6399. void bench_curve25519KeyAgree(int useDeviceID)
  6400. {
  6401. curve25519_key genKey, genKey2;
  6402. double start;
  6403. int ret, i, count;
  6404. byte shared[32];
  6405. const char**desc = bench_desc_words[lng_index];
  6406. word32 x = 0;
  6407. wc_curve25519_init_ex(&genKey, HEAP_HINT,
  6408. useDeviceID ? devId : INVALID_DEVID);
  6409. wc_curve25519_init_ex(&genKey2, HEAP_HINT,
  6410. useDeviceID ? devId : INVALID_DEVID);
  6411. ret = wc_curve25519_make_key(&gRng, 32, &genKey);
  6412. if (ret != 0) {
  6413. printf("curve25519_make_key failed\n");
  6414. return;
  6415. }
  6416. ret = wc_curve25519_make_key(&gRng, 32, &genKey2);
  6417. if (ret != 0) {
  6418. printf("curve25519_make_key failed: %d\n", ret);
  6419. wc_curve25519_free(&genKey);
  6420. return;
  6421. }
  6422. /* Shared secret */
  6423. bench_stats_start(&count, &start);
  6424. do {
  6425. for (i = 0; i < agreeTimes; i++) {
  6426. x = sizeof(shared);
  6427. ret = wc_curve25519_shared_secret(&genKey, &genKey2, shared, &x);
  6428. if (ret != 0) {
  6429. printf("curve25519_shared_secret failed: %d\n", ret);
  6430. goto exit;
  6431. }
  6432. }
  6433. count += i;
  6434. } while (bench_stats_check(start));
  6435. exit:
  6436. bench_stats_asym_finish("CURVE", 25519, desc[3], useDeviceID, count, start,
  6437. ret);
  6438. wc_curve25519_free(&genKey2);
  6439. wc_curve25519_free(&genKey);
  6440. }
  6441. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  6442. #endif /* HAVE_CURVE25519 */
  6443. #ifdef HAVE_ED25519
  6444. void bench_ed25519KeyGen(void)
  6445. {
  6446. ed25519_key genKey;
  6447. double start;
  6448. int i, count;
  6449. const char**desc = bench_desc_words[lng_index];
  6450. /* Key Gen */
  6451. bench_stats_start(&count, &start);
  6452. do {
  6453. for (i = 0; i < genTimes; i++) {
  6454. wc_ed25519_init(&genKey);
  6455. (void)wc_ed25519_make_key(&gRng, 32, &genKey);
  6456. wc_ed25519_free(&genKey);
  6457. }
  6458. count += i;
  6459. } while (bench_stats_check(start));
  6460. bench_stats_asym_finish("ED", 25519, desc[2], 0, count, start, 0);
  6461. }
  6462. void bench_ed25519KeySign(void)
  6463. {
  6464. int ret;
  6465. ed25519_key genKey;
  6466. #ifdef HAVE_ED25519_SIGN
  6467. double start;
  6468. int i, count;
  6469. byte sig[ED25519_SIG_SIZE];
  6470. byte msg[512];
  6471. word32 x = 0;
  6472. const char**desc = bench_desc_words[lng_index];
  6473. #endif
  6474. wc_ed25519_init(&genKey);
  6475. ret = wc_ed25519_make_key(&gRng, ED25519_KEY_SIZE, &genKey);
  6476. if (ret != 0) {
  6477. printf("ed25519_make_key failed\n");
  6478. return;
  6479. }
  6480. #ifdef HAVE_ED25519_SIGN
  6481. /* make dummy msg */
  6482. for (i = 0; i < (int)sizeof(msg); i++)
  6483. msg[i] = (byte)i;
  6484. bench_stats_start(&count, &start);
  6485. do {
  6486. for (i = 0; i < agreeTimes; i++) {
  6487. x = sizeof(sig);
  6488. ret = wc_ed25519_sign_msg(msg, sizeof(msg), sig, &x, &genKey);
  6489. if (ret != 0) {
  6490. printf("ed25519_sign_msg failed\n");
  6491. goto exit_ed_sign;
  6492. }
  6493. }
  6494. count += i;
  6495. } while (bench_stats_check(start));
  6496. exit_ed_sign:
  6497. bench_stats_asym_finish("ED", 25519, desc[4], 0, count, start, ret);
  6498. #ifdef HAVE_ED25519_VERIFY
  6499. bench_stats_start(&count, &start);
  6500. do {
  6501. for (i = 0; i < agreeTimes; i++) {
  6502. int verify = 0;
  6503. ret = wc_ed25519_verify_msg(sig, x, msg, sizeof(msg), &verify,
  6504. &genKey);
  6505. if (ret != 0 || verify != 1) {
  6506. printf("ed25519_verify_msg failed\n");
  6507. goto exit_ed_verify;
  6508. }
  6509. }
  6510. count += i;
  6511. } while (bench_stats_check(start));
  6512. exit_ed_verify:
  6513. bench_stats_asym_finish("ED", 25519, desc[5], 0, count, start, ret);
  6514. #endif /* HAVE_ED25519_VERIFY */
  6515. #endif /* HAVE_ED25519_SIGN */
  6516. wc_ed25519_free(&genKey);
  6517. }
  6518. #endif /* HAVE_ED25519 */
  6519. #ifdef HAVE_CURVE448
  6520. void bench_curve448KeyGen(void)
  6521. {
  6522. curve448_key genKey;
  6523. double start;
  6524. int ret = 0, i, count;
  6525. const char**desc = bench_desc_words[lng_index];
  6526. /* Key Gen */
  6527. bench_stats_start(&count, &start);
  6528. do {
  6529. for (i = 0; i < genTimes; i++) {
  6530. ret = wc_curve448_make_key(&gRng, 56, &genKey);
  6531. wc_curve448_free(&genKey);
  6532. if (ret != 0) {
  6533. printf("wc_curve448_make_key failed: %d\n", ret);
  6534. break;
  6535. }
  6536. }
  6537. count += i;
  6538. } while (bench_stats_check(start));
  6539. bench_stats_asym_finish("CURVE", 448, desc[2], 0, count, start, ret);
  6540. }
  6541. #ifdef HAVE_CURVE448_SHARED_SECRET
  6542. void bench_curve448KeyAgree(void)
  6543. {
  6544. curve448_key genKey, genKey2;
  6545. double start;
  6546. int ret, i, count;
  6547. byte shared[56];
  6548. const char**desc = bench_desc_words[lng_index];
  6549. word32 x = 0;
  6550. wc_curve448_init(&genKey);
  6551. wc_curve448_init(&genKey2);
  6552. ret = wc_curve448_make_key(&gRng, 56, &genKey);
  6553. if (ret != 0) {
  6554. printf("curve448_make_key failed\n");
  6555. return;
  6556. }
  6557. ret = wc_curve448_make_key(&gRng, 56, &genKey2);
  6558. if (ret != 0) {
  6559. printf("curve448_make_key failed: %d\n", ret);
  6560. wc_curve448_free(&genKey);
  6561. return;
  6562. }
  6563. /* Shared secret */
  6564. bench_stats_start(&count, &start);
  6565. do {
  6566. for (i = 0; i < agreeTimes; i++) {
  6567. x = sizeof(shared);
  6568. ret = wc_curve448_shared_secret(&genKey, &genKey2, shared, &x);
  6569. if (ret != 0) {
  6570. printf("curve448_shared_secret failed: %d\n", ret);
  6571. goto exit;
  6572. }
  6573. }
  6574. count += i;
  6575. } while (bench_stats_check(start));
  6576. exit:
  6577. bench_stats_asym_finish("CURVE", 448, desc[3], 0, count, start, ret);
  6578. wc_curve448_free(&genKey2);
  6579. wc_curve448_free(&genKey);
  6580. }
  6581. #endif /* HAVE_CURVE448_SHARED_SECRET */
  6582. #endif /* HAVE_CURVE448 */
  6583. #ifdef HAVE_ED448
  6584. void bench_ed448KeyGen(void)
  6585. {
  6586. ed448_key genKey;
  6587. double start;
  6588. int i, count;
  6589. const char**desc = bench_desc_words[lng_index];
  6590. /* Key Gen */
  6591. bench_stats_start(&count, &start);
  6592. do {
  6593. for (i = 0; i < genTimes; i++) {
  6594. wc_ed448_init(&genKey);
  6595. (void)wc_ed448_make_key(&gRng, ED448_KEY_SIZE, &genKey);
  6596. wc_ed448_free(&genKey);
  6597. }
  6598. count += i;
  6599. } while (bench_stats_check(start));
  6600. bench_stats_asym_finish("ED", 448, desc[2], 0, count, start, 0);
  6601. }
  6602. void bench_ed448KeySign(void)
  6603. {
  6604. int ret;
  6605. ed448_key genKey;
  6606. #ifdef HAVE_ED448_SIGN
  6607. double start;
  6608. int i, count;
  6609. byte sig[ED448_SIG_SIZE];
  6610. byte msg[512];
  6611. word32 x = 0;
  6612. const char**desc = bench_desc_words[lng_index];
  6613. #endif
  6614. wc_ed448_init(&genKey);
  6615. ret = wc_ed448_make_key(&gRng, ED448_KEY_SIZE, &genKey);
  6616. if (ret != 0) {
  6617. printf("ed448_make_key failed\n");
  6618. return;
  6619. }
  6620. #ifdef HAVE_ED448_SIGN
  6621. /* make dummy msg */
  6622. for (i = 0; i < (int)sizeof(msg); i++)
  6623. msg[i] = (byte)i;
  6624. bench_stats_start(&count, &start);
  6625. do {
  6626. for (i = 0; i < agreeTimes; i++) {
  6627. x = sizeof(sig);
  6628. ret = wc_ed448_sign_msg(msg, sizeof(msg), sig, &x, &genKey,
  6629. NULL, 0);
  6630. if (ret != 0) {
  6631. printf("ed448_sign_msg failed\n");
  6632. goto exit_ed_sign;
  6633. }
  6634. }
  6635. count += i;
  6636. } while (bench_stats_check(start));
  6637. exit_ed_sign:
  6638. bench_stats_asym_finish("ED", 448, desc[4], 0, count, start, ret);
  6639. #ifdef HAVE_ED448_VERIFY
  6640. bench_stats_start(&count, &start);
  6641. do {
  6642. for (i = 0; i < agreeTimes; i++) {
  6643. int verify = 0;
  6644. ret = wc_ed448_verify_msg(sig, x, msg, sizeof(msg), &verify,
  6645. &genKey, NULL, 0);
  6646. if (ret != 0 || verify != 1) {
  6647. printf("ed448_verify_msg failed\n");
  6648. goto exit_ed_verify;
  6649. }
  6650. }
  6651. count += i;
  6652. } while (bench_stats_check(start));
  6653. exit_ed_verify:
  6654. bench_stats_asym_finish("ED", 448, desc[5], 0, count, start, ret);
  6655. #endif /* HAVE_ED448_VERIFY */
  6656. #endif /* HAVE_ED448_SIGN */
  6657. wc_ed448_free(&genKey);
  6658. }
  6659. #endif /* HAVE_ED448 */
  6660. #ifdef WOLFCRYPT_HAVE_ECCSI
  6661. #ifdef WOLFCRYPT_ECCSI_KMS
  6662. void bench_eccsiKeyGen(void)
  6663. {
  6664. EccsiKey genKey;
  6665. double start;
  6666. int i, count;
  6667. const char**desc = bench_desc_words[lng_index];
  6668. int ret;
  6669. /* Key Gen */
  6670. bench_stats_start(&count, &start);
  6671. do {
  6672. for (i = 0; i < genTimes; i++) {
  6673. wc_InitEccsiKey(&genKey, NULL, INVALID_DEVID);
  6674. ret = wc_MakeEccsiKey(&genKey, &gRng);
  6675. if (ret != 0) {
  6676. printf("wc_MakeEccsiKey failed: %d\n", ret);
  6677. break;
  6678. }
  6679. wc_FreeEccsiKey(&genKey);
  6680. }
  6681. count += i;
  6682. } while (bench_stats_check(start));
  6683. bench_stats_asym_finish("ECCSI", 256, desc[2], 0, count, start, 0);
  6684. }
  6685. void bench_eccsiPairGen(void)
  6686. {
  6687. EccsiKey genKey;
  6688. double start;
  6689. int i, count;
  6690. const char**desc = bench_desc_words[lng_index];
  6691. mp_int ssk;
  6692. ecc_point* pvt;
  6693. byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  6694. int ret;
  6695. (void)mp_init(&ssk);
  6696. pvt = wc_ecc_new_point();
  6697. wc_InitEccsiKey(&genKey, NULL, INVALID_DEVID);
  6698. (void)wc_MakeEccsiKey(&genKey, &gRng);
  6699. /* RSK Gen */
  6700. bench_stats_start(&count, &start);
  6701. do {
  6702. for (i = 0; i < genTimes; i++) {
  6703. ret = wc_MakeEccsiPair(&genKey, &gRng, WC_HASH_TYPE_SHA256, id,
  6704. sizeof(id), &ssk, pvt);
  6705. if (ret != 0) {
  6706. printf("wc_MakeEccsiPair failed: %d\n", ret);
  6707. break;
  6708. }
  6709. }
  6710. count += i;
  6711. } while (bench_stats_check(start));
  6712. bench_stats_asym_finish("ECCSI", 256, desc[12], 0, count, start, 0);
  6713. wc_FreeEccsiKey(&genKey);
  6714. wc_ecc_del_point(pvt);
  6715. mp_free(&ssk);
  6716. }
  6717. #endif
  6718. #ifdef WOLFCRYPT_ECCSI_CLIENT
  6719. void bench_eccsiValidate(void)
  6720. {
  6721. EccsiKey genKey;
  6722. double start;
  6723. int i, count;
  6724. const char**desc = bench_desc_words[lng_index];
  6725. mp_int ssk;
  6726. ecc_point* pvt;
  6727. byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  6728. int valid;
  6729. int ret;
  6730. (void)mp_init(&ssk);
  6731. pvt = wc_ecc_new_point();
  6732. wc_InitEccsiKey(&genKey, NULL, INVALID_DEVID);
  6733. (void)wc_MakeEccsiKey(&genKey, &gRng);
  6734. (void)wc_MakeEccsiPair(&genKey, &gRng, WC_HASH_TYPE_SHA256, id, sizeof(id),
  6735. &ssk, pvt);
  6736. /* Validation of RSK */
  6737. bench_stats_start(&count, &start);
  6738. do {
  6739. for (i = 0; i < genTimes; i++) {
  6740. ret = wc_ValidateEccsiPair(&genKey, WC_HASH_TYPE_SHA256, id,
  6741. sizeof(id), &ssk, pvt, &valid);
  6742. if (ret != 0 || !valid) {
  6743. printf("wc_ValidateEccsiPair failed: %d (valid=%d))\n", ret,
  6744. valid);
  6745. break;
  6746. }
  6747. }
  6748. count += i;
  6749. } while (bench_stats_check(start));
  6750. bench_stats_asym_finish("ECCSI", 256, desc[11], 0, count, start, 0);
  6751. wc_FreeEccsiKey(&genKey);
  6752. wc_ecc_del_point(pvt);
  6753. mp_free(&ssk);
  6754. }
  6755. void bench_eccsi(void)
  6756. {
  6757. EccsiKey genKey;
  6758. double start;
  6759. int i, count;
  6760. const char**desc = bench_desc_words[lng_index];
  6761. mp_int ssk;
  6762. ecc_point* pvt;
  6763. byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  6764. byte msg[] = { 0x01, 0x23, 0x34, 0x45 };
  6765. byte hash[WC_SHA256_DIGEST_SIZE];
  6766. byte hashSz = (byte)sizeof(hash);
  6767. byte sig[257];
  6768. word32 sigSz = sizeof(sig);
  6769. int ret;
  6770. int verified;
  6771. (void)mp_init(&ssk);
  6772. pvt = wc_ecc_new_point();
  6773. (void)wc_InitEccsiKey(&genKey, NULL, INVALID_DEVID);
  6774. (void)wc_MakeEccsiKey(&genKey, &gRng);
  6775. (void)wc_MakeEccsiPair(&genKey, &gRng, WC_HASH_TYPE_SHA256, id, sizeof(id),
  6776. &ssk, pvt);
  6777. (void)wc_HashEccsiId(&genKey, WC_HASH_TYPE_SHA256, id, sizeof(id), pvt,
  6778. hash, &hashSz);
  6779. (void)wc_SetEccsiHash(&genKey, hash, hashSz);
  6780. (void)wc_SetEccsiPair(&genKey, &ssk, pvt);
  6781. /* Encapsulate */
  6782. bench_stats_start(&count, &start);
  6783. do {
  6784. for (i = 0; i < genTimes; i++) {
  6785. ret = wc_SignEccsiHash(&genKey, &gRng, WC_HASH_TYPE_SHA256, msg,
  6786. sizeof(msg), sig, &sigSz);
  6787. if (ret != 0) {
  6788. printf("wc_SignEccsiHash failed: %d\n", ret);
  6789. break;
  6790. }
  6791. }
  6792. count += i;
  6793. } while (bench_stats_check(start));
  6794. bench_stats_asym_finish("ECCSI", 256, desc[4], 0, count, start, 0);
  6795. /* Derive */
  6796. bench_stats_start(&count, &start);
  6797. do {
  6798. for (i = 0; i < genTimes; i++) {
  6799. ret = wc_VerifyEccsiHash(&genKey, WC_HASH_TYPE_SHA256, msg,
  6800. sizeof(msg), sig, sigSz, &verified);
  6801. if (ret != 0 || !verified) {
  6802. printf("wc_VerifyEccsiHash failed: %d (verified: %d)\n", ret,
  6803. verified);
  6804. break;
  6805. }
  6806. }
  6807. count += i;
  6808. } while (bench_stats_check(start));
  6809. bench_stats_asym_finish("ECCSI", 256, desc[5], 0, count, start, 0);
  6810. wc_FreeEccsiKey(&genKey);
  6811. wc_ecc_del_point(pvt);
  6812. }
  6813. #endif /* WOLFCRYPT_ECCSI_CLIENT */
  6814. #endif /* WOLFCRYPT_HAVE_ECCSI */
  6815. #ifdef WOLFCRYPT_HAVE_SAKKE
  6816. #ifdef WOLFCRYPT_SAKKE_KMS
  6817. void bench_sakkeKeyGen(void)
  6818. {
  6819. SakkeKey genKey;
  6820. double start;
  6821. int i, count;
  6822. const char**desc = bench_desc_words[lng_index];
  6823. int ret;
  6824. /* Key Gen */
  6825. bench_stats_start(&count, &start);
  6826. do {
  6827. for (i = 0; i < genTimes; i++) {
  6828. wc_InitSakkeKey_ex(&genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  6829. ret = wc_MakeSakkeKey(&genKey, &gRng);
  6830. if (ret != 0) {
  6831. printf("wc_MakeSakkeKey failed: %d\n", ret);
  6832. break;
  6833. }
  6834. wc_FreeSakkeKey(&genKey);
  6835. }
  6836. count += i;
  6837. } while (bench_stats_check(start));
  6838. bench_stats_asym_finish("SAKKE", 1024, desc[2], 0, count, start, 0);
  6839. }
  6840. void bench_sakkeRskGen(void)
  6841. {
  6842. SakkeKey genKey;
  6843. double start;
  6844. int i, count;
  6845. const char**desc = bench_desc_words[lng_index];
  6846. ecc_point* rsk;
  6847. byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  6848. int ret;
  6849. rsk = wc_ecc_new_point();
  6850. wc_InitSakkeKey_ex(&genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  6851. (void)wc_MakeSakkeKey(&genKey, &gRng);
  6852. /* RSK Gen */
  6853. bench_stats_start(&count, &start);
  6854. do {
  6855. for (i = 0; i < genTimes; i++) {
  6856. ret = wc_MakeSakkeRsk(&genKey, id, sizeof(id), rsk);
  6857. if (ret != 0) {
  6858. printf("wc_MakeSakkeRsk failed: %d\n", ret);
  6859. break;
  6860. }
  6861. }
  6862. count += i;
  6863. } while (bench_stats_check(start));
  6864. bench_stats_asym_finish("SAKKE", 1024, desc[8], 0, count, start, 0);
  6865. wc_FreeSakkeKey(&genKey);
  6866. wc_ecc_del_point(rsk);
  6867. }
  6868. #endif
  6869. #ifdef WOLFCRYPT_SAKKE_CLIENT
  6870. void bench_sakkeValidate(void)
  6871. {
  6872. SakkeKey genKey;
  6873. double start;
  6874. int i, count;
  6875. const char**desc = bench_desc_words[lng_index];
  6876. ecc_point* rsk;
  6877. byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  6878. int valid;
  6879. int ret;
  6880. rsk = wc_ecc_new_point();
  6881. (void)wc_InitSakkeKey_ex(&genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  6882. (void)wc_MakeSakkeKey(&genKey, &gRng);
  6883. (void)wc_MakeSakkeRsk(&genKey, id, sizeof(id), rsk);
  6884. (void)wc_ValidateSakkeRsk(&genKey, id, sizeof(id), rsk, &valid);
  6885. /* Validation of RSK */
  6886. bench_stats_start(&count, &start);
  6887. do {
  6888. for (i = 0; i < genTimes; i++) {
  6889. ret = wc_ValidateSakkeRsk(&genKey, id, sizeof(id), rsk, &valid);
  6890. if (ret != 0 || !valid) {
  6891. printf("wc_ValidateSakkeRsk failed: %d (valid=%d))\n", ret,
  6892. valid);
  6893. break;
  6894. }
  6895. }
  6896. count += i;
  6897. } while (bench_stats_check(start));
  6898. bench_stats_asym_finish("SAKKE", 1024, desc[11], 0, count, start, 0);
  6899. wc_FreeSakkeKey(&genKey);
  6900. wc_ecc_del_point(rsk);
  6901. }
  6902. void bench_sakke(void)
  6903. {
  6904. SakkeKey genKey;
  6905. double start;
  6906. int i, count;
  6907. const char**desc = bench_desc_words[lng_index];
  6908. ecc_point* rsk;
  6909. byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  6910. byte ssv[] = { 0x01, 0x23, 0x34, 0x45 };
  6911. byte derSSV[sizeof(ssv)];
  6912. byte auth[257];
  6913. word16 authSz = sizeof(auth);
  6914. int ret = 0;
  6915. byte* table = NULL;
  6916. word32 len = 0;
  6917. byte* iTable = NULL;
  6918. word32 iTableLen = 0;
  6919. rsk = wc_ecc_new_point();
  6920. (void)wc_InitSakkeKey_ex(&genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  6921. (void)wc_MakeSakkeKey(&genKey, &gRng);
  6922. (void)wc_MakeSakkeRsk(&genKey, id, sizeof(id), rsk);
  6923. (void)wc_SetSakkeRsk(&genKey, rsk, NULL, 0);
  6924. (void)wc_SetSakkeIdentity(&genKey, id, sizeof(id));
  6925. /* Encapsulate */
  6926. bench_stats_start(&count, &start);
  6927. do {
  6928. for (i = 0; i < genTimes; i++) {
  6929. ret = wc_MakeSakkeEncapsulatedSSV(&genKey,
  6930. WC_HASH_TYPE_SHA256,
  6931. ssv, sizeof(ssv), auth, &authSz);
  6932. if (ret != 0) {
  6933. printf("wc_MakeSakkeEncapsulatedSSV failed: %d\n", ret);
  6934. break;
  6935. }
  6936. } /* for */
  6937. count += i;
  6938. } while (bench_stats_check(start));
  6939. bench_stats_asym_finish_ex("SAKKE", 1024, desc[9], "-1",
  6940. 0, count, start, 0);
  6941. /* Derive */
  6942. bench_stats_start(&count, &start);
  6943. do {
  6944. for (i = 0; i < genTimes; i++) {
  6945. XMEMCPY(derSSV, ssv, sizeof(ssv));
  6946. ret = wc_DeriveSakkeSSV(&genKey, WC_HASH_TYPE_SHA256, derSSV,
  6947. sizeof(derSSV), auth, authSz);
  6948. if (ret != 0) {
  6949. printf("wc_DeriveSakkeSSV failed: %d\n", ret);
  6950. break;
  6951. }
  6952. }
  6953. if (ret != 0) break;
  6954. count += i;
  6955. } while (bench_stats_check(start));
  6956. bench_stats_asym_finish_ex("SAKKE", 1024, desc[10], "-1",
  6957. 0, count, start, 0);
  6958. /* Calculate Point I and generate table. */
  6959. (void)wc_MakeSakkePointI(&genKey, id, sizeof(id));
  6960. iTableLen = 0;
  6961. (void)wc_GenerateSakkePointITable(&genKey, NULL, &iTableLen);
  6962. if (iTableLen != 0) {
  6963. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6964. (void)wc_GenerateSakkePointITable(&genKey, iTable, &iTableLen);
  6965. }
  6966. /* Encapsulate with Point I table */
  6967. bench_stats_start(&count, &start);
  6968. do {
  6969. for (i = 0; i < genTimes; i++) {
  6970. ret = wc_MakeSakkeEncapsulatedSSV(&genKey,
  6971. WC_HASH_TYPE_SHA256, ssv,
  6972. sizeof(ssv), auth, &authSz);
  6973. if (ret != 0) {
  6974. printf("wc_MakeSakkeEncapsulatedSSV failed: %d\n", ret);
  6975. break;
  6976. }
  6977. }
  6978. count += i;
  6979. } while (bench_stats_check(start));
  6980. bench_stats_asym_finish_ex("SAKKE", 1024, desc[9], "-2", 0,
  6981. count, start, 0);
  6982. (void)wc_SetSakkeRsk(&genKey, rsk, table, len);
  6983. /* Derive with Point I table */
  6984. bench_stats_start(&count, &start);
  6985. do {
  6986. for (i = 0; i < genTimes; i++) {
  6987. XMEMCPY(derSSV, ssv, sizeof(ssv));
  6988. ret = wc_DeriveSakkeSSV(&genKey, WC_HASH_TYPE_SHA256, derSSV,
  6989. sizeof(derSSV), auth, authSz);
  6990. if (ret != 0) {
  6991. printf("wc_DeriveSakkeSSV failed: %d\n", ret);
  6992. break;
  6993. }
  6994. }
  6995. if (ret != 0) break;
  6996. count += i;
  6997. } while (bench_stats_check(start));
  6998. bench_stats_asym_finish_ex("SAKKE", 1024, desc[10], "-2", 0,
  6999. count, start, 0);
  7000. len = 0;
  7001. (void)wc_GenerateSakkeRskTable(&genKey, rsk, NULL, &len);
  7002. if (len > 0) {
  7003. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  7004. (void)wc_GenerateSakkeRskTable(&genKey, rsk, table, &len);
  7005. }
  7006. (void)wc_SetSakkeRsk(&genKey, rsk, table, len);
  7007. /* Derive with Point I table and RSK table */
  7008. bench_stats_start(&count, &start);
  7009. do {
  7010. for (i = 0; i < genTimes; i++) {
  7011. XMEMCPY(derSSV, ssv, sizeof(ssv));
  7012. ret = wc_DeriveSakkeSSV(&genKey, WC_HASH_TYPE_SHA256, derSSV,
  7013. sizeof(derSSV), auth, authSz);
  7014. if (ret != 0) {
  7015. printf("wc_DeriveSakkeSSV failed: %d\n", ret);
  7016. break;
  7017. }
  7018. }
  7019. if (ret != 0) break;
  7020. count += i;
  7021. } while (bench_stats_check(start));
  7022. bench_stats_asym_finish_ex("SAKKE", 1024, desc[10], "-3",
  7023. 0, count, start, 0);
  7024. wc_ClearSakkePointITable(&genKey);
  7025. /* Derive with RSK table */
  7026. bench_stats_start(&count, &start);
  7027. do {
  7028. for (i = 0; i < genTimes; i++) {
  7029. XMEMCPY(derSSV, ssv, sizeof(ssv));
  7030. ret = wc_DeriveSakkeSSV(&genKey, WC_HASH_TYPE_SHA256, derSSV,
  7031. sizeof(derSSV), auth, authSz);
  7032. if (ret != 0) {
  7033. printf("wc_DeriveSakkeSSV failed: %d\n", ret);
  7034. break;
  7035. }
  7036. }
  7037. if (ret != 0) break;
  7038. count += i;
  7039. } while (bench_stats_check(start));
  7040. bench_stats_asym_finish_ex("SAKKE", 1024, desc[10], "-4", 0,
  7041. count, start, 0);
  7042. wc_FreeSakkeKey(&genKey);
  7043. wc_ecc_del_point(rsk);
  7044. }
  7045. #endif /* WOLFCRYPT_SAKKE_CLIENT */
  7046. #endif /* WOLFCRYPT_HAVE_SAKKE */
  7047. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  7048. #ifdef HAVE_FALCON
  7049. void bench_falconKeySign(byte level)
  7050. {
  7051. int ret = 0;
  7052. falcon_key key;
  7053. double start;
  7054. int i, count;
  7055. byte sig[FALCON_MAX_SIG_SIZE];
  7056. byte msg[512];
  7057. word32 x = 0;
  7058. const char**desc = bench_desc_words[lng_index];
  7059. ret = wc_falcon_init(&key);
  7060. if (ret != 0) {
  7061. printf("wc_falcon_init failed %d\n", ret);
  7062. return;
  7063. }
  7064. ret = wc_falcon_set_level(&key, level);
  7065. if (ret != 0) {
  7066. printf("wc_falcon_set_level failed %d\n", ret);
  7067. }
  7068. if (ret == 0) {
  7069. if (level == 1) {
  7070. ret = wc_falcon_import_private_key(bench_falcon_level1_key,
  7071. sizeof_bench_falcon_level1_key,
  7072. NULL, 0, &key);
  7073. }
  7074. else {
  7075. ret = wc_falcon_import_private_key(bench_falcon_level5_key,
  7076. sizeof_bench_falcon_level5_key,
  7077. NULL, 0, &key);
  7078. }
  7079. if (ret != 0) {
  7080. printf("wc_falcon_import_private_key failed %d\n", ret);
  7081. }
  7082. }
  7083. /* make dummy msg */
  7084. for (i = 0; i < (int)sizeof(msg); i++) {
  7085. msg[i] = (byte)i;
  7086. }
  7087. bench_stats_start(&count, &start);
  7088. do {
  7089. for (i = 0; i < agreeTimes; i++) {
  7090. if (ret == 0) {
  7091. if (level == 1) {
  7092. x = FALCON_LEVEL1_SIG_SIZE;
  7093. }
  7094. else {
  7095. x = FALCON_LEVEL5_SIG_SIZE;
  7096. }
  7097. ret = wc_falcon_sign_msg(msg, sizeof(msg), sig, &x, &key);
  7098. if (ret != 0) {
  7099. printf("wc_falcon_sign_msg failed\n");
  7100. }
  7101. }
  7102. }
  7103. count += i;
  7104. } while (bench_stats_check(start));
  7105. if (ret == 0) {
  7106. bench_stats_asym_finish("FALCON", level, desc[4], 0,
  7107. count, start, ret);
  7108. }
  7109. bench_stats_start(&count, &start);
  7110. do {
  7111. for (i = 0; i < agreeTimes; i++) {
  7112. if (ret == 0) {
  7113. int verify = 0;
  7114. ret = wc_falcon_verify_msg(sig, x, msg, sizeof(msg), &verify,
  7115. &key);
  7116. if (ret != 0 || verify != 1) {
  7117. printf("wc_falcon_verify_msg failed %d, verify %d\n",
  7118. ret, verify);
  7119. ret = -1;
  7120. }
  7121. }
  7122. }
  7123. count += i;
  7124. } while (bench_stats_check(start));
  7125. if (ret == 0) {
  7126. bench_stats_asym_finish("FALCON", level, desc[5],
  7127. 0, count, start, ret);
  7128. }
  7129. wc_falcon_free(&key);
  7130. }
  7131. #endif /* HAVE_FALCON */
  7132. #ifdef HAVE_DILITHIUM
  7133. void bench_dilithiumKeySign(byte level)
  7134. {
  7135. int ret = 0;
  7136. dilithium_key key;
  7137. double start;
  7138. int i, count;
  7139. byte sig[DILITHIUM_MAX_SIG_SIZE];
  7140. byte msg[512];
  7141. word32 x = 0;
  7142. const char**desc = bench_desc_words[lng_index];
  7143. ret = wc_dilithium_init(&key);
  7144. if (ret != 0) {
  7145. printf("wc_dilithium_init failed %d\n", ret);
  7146. return;
  7147. }
  7148. ret = wc_dilithium_set_level(&key, level);
  7149. if (ret != 0) {
  7150. printf("wc_dilithium_set_level() failed %d\n", ret);
  7151. }
  7152. if (ret == 0) {
  7153. ret = -1;
  7154. if (level == 2) {
  7155. ret = wc_dilithium_import_private_key(bench_dilithium_level2_key,
  7156. sizeof_bench_dilithium_level2_key, NULL, 0, &key);
  7157. }
  7158. else if (level == 3) {
  7159. ret = wc_dilithium_import_private_key(bench_dilithium_level3_key,
  7160. sizeof_bench_dilithium_level3_key, NULL, 0, &key);
  7161. }
  7162. else if (level == 5) {
  7163. ret = wc_dilithium_import_private_key(bench_dilithium_level5_key,
  7164. sizeof_bench_dilithium_level5_key, NULL, 0, &key);
  7165. }
  7166. if (ret != 0) {
  7167. printf("wc_dilithium_import_private_key failed %d\n", ret);
  7168. }
  7169. }
  7170. /* make dummy msg */
  7171. for (i = 0; i < (int)sizeof(msg); i++) {
  7172. msg[i] = (byte)i;
  7173. }
  7174. bench_stats_start(&count, &start);
  7175. do {
  7176. for (i = 0; i < agreeTimes; i++) {
  7177. if (ret == 0) {
  7178. if (level == 2) {
  7179. x = DILITHIUM_LEVEL2_SIG_SIZE;
  7180. }
  7181. else if (level == 3) {
  7182. x = DILITHIUM_LEVEL3_SIG_SIZE;
  7183. }
  7184. else {
  7185. x = DILITHIUM_LEVEL5_SIG_SIZE;
  7186. }
  7187. ret = wc_dilithium_sign_msg(msg, sizeof(msg), sig, &x, &key);
  7188. if (ret != 0) {
  7189. printf("wc_dilithium_sign_msg failed\n");
  7190. }
  7191. }
  7192. }
  7193. count += i;
  7194. } while (bench_stats_check(start));
  7195. if (ret == 0) {
  7196. bench_stats_asym_finish("DILITHIUM", level, desc[4], 0, count, start,
  7197. ret);
  7198. }
  7199. bench_stats_start(&count, &start);
  7200. do {
  7201. for (i = 0; i < agreeTimes; i++) {
  7202. if (ret == 0) {
  7203. int verify = 0;
  7204. ret = wc_dilithium_verify_msg(sig, x, msg, sizeof(msg),
  7205. &verify, &key);
  7206. if (ret != 0 || verify != 1) {
  7207. printf("wc_dilithium_verify_msg failed %d, verify %d\n",
  7208. ret, verify);
  7209. ret = -1;
  7210. }
  7211. }
  7212. }
  7213. count += i;
  7214. } while (bench_stats_check(start));
  7215. if (ret == 0) {
  7216. bench_stats_asym_finish("DILITHIUM", level, desc[5], 0, count, start,
  7217. ret);
  7218. }
  7219. wc_dilithium_free(&key);
  7220. }
  7221. #endif /* HAVE_DILITHIUM */
  7222. #ifdef HAVE_SPHINCS
  7223. void bench_sphincsKeySign(byte level, byte optim)
  7224. {
  7225. int ret = 0;
  7226. sphincs_key key;
  7227. double start;
  7228. int i, count;
  7229. byte sig[SPHINCS_MAX_SIG_SIZE];
  7230. byte msg[512];
  7231. word32 x = 0;
  7232. const char**desc = bench_desc_words[lng_index];
  7233. ret = wc_sphincs_init(&key);
  7234. if (ret != 0) {
  7235. printf("wc_sphincs_init failed %d\n", ret);
  7236. return;
  7237. }
  7238. ret = wc_sphincs_set_level_and_optim(&key, level, optim);
  7239. if (ret != 0) {
  7240. printf("wc_sphincs_set_level_and_optim() failed %d\n", ret);
  7241. }
  7242. if (ret == 0) {
  7243. ret = -1;
  7244. if ((level == 1) && (optim == FAST_VARIANT)) {
  7245. ret = wc_sphincs_import_private_key(bench_sphincs_fast_level1_key,
  7246. sizeof_bench_sphincs_fast_level1_key, NULL, 0, &key);
  7247. }
  7248. else if ((level == 3) && (optim == FAST_VARIANT)) {
  7249. ret = wc_sphincs_import_private_key(bench_sphincs_fast_level3_key,
  7250. sizeof_bench_sphincs_fast_level3_key, NULL, 0, &key);
  7251. }
  7252. else if ((level == 5) && (optim == FAST_VARIANT)) {
  7253. ret = wc_sphincs_import_private_key(bench_sphincs_fast_level5_key,
  7254. sizeof_bench_sphincs_fast_level5_key, NULL, 0, &key);
  7255. }
  7256. else if ((level == 1) && (optim == SMALL_VARIANT)) {
  7257. ret = wc_sphincs_import_private_key(
  7258. bench_sphincs_small_level1_key,
  7259. sizeof_bench_sphincs_small_level1_key, NULL, 0, &key);
  7260. }
  7261. else if ((level == 3) && (optim == SMALL_VARIANT)) {
  7262. ret = wc_sphincs_import_private_key(
  7263. bench_sphincs_small_level3_key,
  7264. sizeof_bench_sphincs_small_level3_key, NULL, 0, &key);
  7265. }
  7266. else if ((level == 5) && (optim == SMALL_VARIANT)) {
  7267. ret = wc_sphincs_import_private_key(
  7268. bench_sphincs_small_level5_key,
  7269. sizeof_bench_sphincs_small_level5_key, NULL, 0, &key);
  7270. }
  7271. if (ret != 0) {
  7272. printf("wc_sphincs_import_private_key failed %d\n", ret);
  7273. }
  7274. }
  7275. /* make dummy msg */
  7276. for (i = 0; i < (int)sizeof(msg); i++) {
  7277. msg[i] = (byte)i;
  7278. }
  7279. bench_stats_start(&count, &start);
  7280. do {
  7281. for (i = 0; i < agreeTimes; i++) {
  7282. if (ret == 0) {
  7283. if ((level == 1) && (optim == FAST_VARIANT)) {
  7284. x = SPHINCS_FAST_LEVEL1_SIG_SIZE;
  7285. }
  7286. else if ((level == 3) && (optim == FAST_VARIANT)) {
  7287. x = SPHINCS_FAST_LEVEL3_SIG_SIZE;
  7288. }
  7289. else if ((level == 5) && (optim == FAST_VARIANT)) {
  7290. x = SPHINCS_FAST_LEVEL5_SIG_SIZE;
  7291. }
  7292. else if ((level == 1) && (optim == SMALL_VARIANT)) {
  7293. x = SPHINCS_SMALL_LEVEL1_SIG_SIZE;
  7294. }
  7295. else if ((level == 3) && (optim == SMALL_VARIANT)) {
  7296. x = SPHINCS_SMALL_LEVEL3_SIG_SIZE;
  7297. }
  7298. else if ((level == 5) && (optim == SMALL_VARIANT)) {
  7299. x = SPHINCS_SMALL_LEVEL5_SIG_SIZE;
  7300. }
  7301. ret = wc_sphincs_sign_msg(msg, sizeof(msg), sig, &x, &key);
  7302. if (ret != 0) {
  7303. printf("wc_sphincs_sign_msg failed\n");
  7304. }
  7305. }
  7306. }
  7307. count += i;
  7308. } while (bench_stats_check(start));
  7309. if (ret == 0) {
  7310. if (optim == FAST_VARIANT) {
  7311. bench_stats_asym_finish("SPHINCS-FAST", level, desc[4], 0, count,
  7312. start, ret);
  7313. }
  7314. else {
  7315. bench_stats_asym_finish("SPHINCS-SMALL", level, desc[4], 0, count,
  7316. start, ret);
  7317. }
  7318. }
  7319. bench_stats_start(&count, &start);
  7320. do {
  7321. for (i = 0; i < agreeTimes; i++) {
  7322. if (ret == 0) {
  7323. int verify = 0;
  7324. ret = wc_sphincs_verify_msg(sig, x, msg, sizeof(msg), &verify,
  7325. &key);
  7326. if (ret != 0 || verify != 1) {
  7327. printf("wc_sphincs_verify_msg failed %d, verify %d\n",
  7328. ret, verify);
  7329. ret = -1;
  7330. }
  7331. }
  7332. }
  7333. count += i;
  7334. } while (bench_stats_check(start));
  7335. if (ret == 0) {
  7336. if (optim == FAST_VARIANT) {
  7337. bench_stats_asym_finish("SPHINCS-FAST", level, desc[5], 0, count,
  7338. start, ret);
  7339. }
  7340. else {
  7341. bench_stats_asym_finish("SPHINCS-SMALL", level, desc[5], 0, count,
  7342. start, ret);
  7343. }
  7344. }
  7345. wc_sphincs_free(&key);
  7346. }
  7347. #endif /* HAVE_SPHINCS */
  7348. #endif /* HAVE_PQC */
  7349. #if defined(_WIN32) && !defined(INTIME_RTOS)
  7350. #define WIN32_LEAN_AND_MEAN
  7351. #include <windows.h>
  7352. double current_time(int reset)
  7353. {
  7354. static int init = 0;
  7355. static LARGE_INTEGER freq;
  7356. LARGE_INTEGER count;
  7357. (void)reset;
  7358. if (!init) {
  7359. QueryPerformanceFrequency(&freq);
  7360. init = 1;
  7361. }
  7362. QueryPerformanceCounter(&count);
  7363. return (double)count.QuadPart / freq.QuadPart;
  7364. }
  7365. #elif defined MICROCHIP_PIC32
  7366. #if defined(WOLFSSL_MICROCHIP_PIC32MZ)
  7367. #define CLOCK 80000000.0
  7368. #else
  7369. #define CLOCK 40000000.0
  7370. #endif
  7371. extern void WriteCoreTimer(word32 t);
  7372. extern word32 ReadCoreTimer(void);
  7373. double current_time(int reset)
  7374. {
  7375. unsigned int ns;
  7376. if (reset) {
  7377. WriteCoreTimer(0);
  7378. }
  7379. /* get timer in ns */
  7380. ns = ReadCoreTimer();
  7381. /* return seconds as a double */
  7382. return ( ns / CLOCK * 2.0);
  7383. }
  7384. #elif defined(WOLFSSL_IAR_ARM_TIME) || defined (WOLFSSL_MDK_ARM) || \
  7385. defined(WOLFSSL_USER_CURRTIME) || defined(WOLFSSL_CURRTIME_REMAP)
  7386. /* declared above at line 239 */
  7387. /* extern double current_time(int reset); */
  7388. #elif defined(FREERTOS)
  7389. #include "task.h"
  7390. #if defined(WOLFSSL_ESPIDF)
  7391. /* prototype definition */
  7392. int construct_argv();
  7393. extern char* __argv[22];
  7394. #endif
  7395. double current_time(int reset)
  7396. {
  7397. #if ESP_IDF_VERSION_MAJOR >= 4
  7398. TickType_t tickCount;
  7399. #else
  7400. portTickType tickCount;
  7401. #endif
  7402. (void) reset;
  7403. /* tick count == ms, if configTICK_RATE_HZ is set to 1000 */
  7404. tickCount = xTaskGetTickCount();
  7405. return (double)tickCount / 1000;
  7406. }
  7407. #elif defined (WOLFSSL_TIRTOS)
  7408. extern double current_time(int reset);
  7409. #elif defined(FREESCALE_MQX)
  7410. double current_time(int reset)
  7411. {
  7412. TIME_STRUCT tv;
  7413. _time_get(&tv);
  7414. return (double)tv.SECONDS + (double)tv.MILLISECONDS / 1000;
  7415. }
  7416. #elif defined(FREESCALE_KSDK_BM)
  7417. double current_time(int reset)
  7418. {
  7419. return (double)OSA_TimeGetMsec() / 1000;
  7420. }
  7421. #elif defined(WOLFSSL_EMBOS)
  7422. #include "RTOS.h"
  7423. double current_time(int reset)
  7424. {
  7425. double time_now;
  7426. double current_s = OS_GetTime() / 1000.0;
  7427. double current_us = OS_GetTime_us() / 1000000.0;
  7428. time_now = (double)( current_s + current_us);
  7429. (void) reset;
  7430. return time_now;
  7431. }
  7432. #elif defined(WOLFSSL_SGX)
  7433. double current_time(int reset);
  7434. #elif defined(WOLFSSL_DEOS)
  7435. double current_time(int reset)
  7436. {
  7437. const uint32_t systemTickTimeInHz
  7438. = 1000000 / systemTickInMicroseconds();
  7439. const volatile uint32_t *systemTickPtr = systemTickPointer();
  7440. (void)reset;
  7441. return (double) *systemTickPtr/systemTickTimeInHz;
  7442. }
  7443. #elif defined(MICRIUM)
  7444. double current_time(int reset)
  7445. {
  7446. #if (OS_VERSION < 50000)
  7447. CPU_ERR err;
  7448. (void)reset;
  7449. return (double) CPU_TS_Get32()/CPU_TS_TmrFreqGet(&err);
  7450. #else
  7451. RTOS_ERR err;
  7452. double ret = 0;
  7453. OS_TICK tick = OSTimeGet(&err);
  7454. OS_RATE_HZ rate = OSTimeTickRateHzGet(&err);
  7455. (void)reset;
  7456. if (RTOS_ERR_CODE_GET(err) == RTOS_ERR_NONE) {
  7457. ret = ((double)tick)/rate;
  7458. }
  7459. return ret;
  7460. #endif
  7461. }
  7462. #elif defined(WOLFSSL_ZEPHYR)
  7463. #include <time.h>
  7464. double current_time(int reset)
  7465. {
  7466. (void)reset;
  7467. #if defined(CONFIG_ARCH_POSIX)
  7468. k_cpu_idle();
  7469. #endif
  7470. return (double)k_uptime_get() / 1000;
  7471. }
  7472. #elif defined(WOLFSSL_NETBURNER)
  7473. #include <predef.h>
  7474. #include <utils.h>
  7475. #include <constants.h>
  7476. double current_time(int reset)
  7477. {
  7478. DWORD ticks = TimeTick; /* ticks since system start */
  7479. (void)reset;
  7480. return (double) ticks/TICKS_PER_SECOND;
  7481. }
  7482. #elif defined(THREADX)
  7483. #include "tx_api.h"
  7484. double current_time(int reset)
  7485. {
  7486. (void)reset;
  7487. return (double) tx_time_get() / TX_TIMER_TICKS_PER_SECOND;
  7488. }
  7489. #elif defined(WOLFSSL_XILINX)
  7490. #ifdef XPAR_VERSAL_CIPS_0_PSPMC_0_PSV_CORTEXA72_0_TIMESTAMP_CLK_FREQ
  7491. #define COUNTS_PER_SECOND \
  7492. XPAR_VERSAL_CIPS_0_PSPMC_0_PSV_CORTEXA72_0_TIMESTAMP_CLK_FREQ
  7493. #else
  7494. #define COUNTS_PER_SECOND \
  7495. XPAR_CPU_CORTEXA53_0_TIMESTAMP_CLK_FREQ
  7496. #endif
  7497. double current_time(int reset)
  7498. {
  7499. double timer;
  7500. uint64_t cntPct = 0;
  7501. asm volatile("mrs %0, CNTPCT_EL0" : "=r" (cntPct));
  7502. /* Convert to milliseconds */
  7503. timer = (double)(cntPct / (COUNTS_PER_SECOND / 1000));
  7504. /* Convert to seconds.millisecond */
  7505. timer /= 1000;
  7506. return timer;
  7507. }
  7508. #elif defined(LINUX_RUSAGE_UTIME)
  7509. #include <sys/time.h>
  7510. #include <sys/resource.h>
  7511. static struct rusage base_rusage;
  7512. static struct rusage cur_rusage;
  7513. double current_time(int reset)
  7514. {
  7515. struct rusage rusage;
  7516. (void)reset;
  7517. LIBCALL_CHECK_RET(getrusage(RUSAGE_SELF, &rusage));
  7518. if (reset)
  7519. base_rusage = rusage;
  7520. else
  7521. cur_rusage = rusage;
  7522. /* only consider user time, as system time is host-related overhead
  7523. * outside wolfcrypt.
  7524. */
  7525. return (double)rusage.ru_utime.tv_sec +
  7526. (double)rusage.ru_utime.tv_usec / 1000000.0;
  7527. }
  7528. static void check_for_excessive_stime(const char *desc,
  7529. const char *desc_extra)
  7530. {
  7531. double start_utime = (double)base_rusage.ru_utime.tv_sec +
  7532. (double)base_rusage.ru_utime.tv_usec / 1000000.0;
  7533. double start_stime = (double)base_rusage.ru_stime.tv_sec +
  7534. (double)base_rusage.ru_stime.tv_usec / 1000000.0;
  7535. double cur_utime = (double)cur_rusage.ru_utime.tv_sec +
  7536. (double)cur_rusage.ru_utime.tv_usec / 1000000.0;
  7537. double cur_stime = (double)cur_rusage.ru_stime.tv_sec +
  7538. (double)cur_rusage.ru_stime.tv_usec / 1000000.0;
  7539. double stime_utime_ratio =
  7540. (cur_stime - start_stime) / (cur_utime - start_utime);
  7541. if (stime_utime_ratio > .1)
  7542. printf("%swarning, "
  7543. "excessive system time ratio for %s%s (%.3f%%).\n",
  7544. err_prefix, desc, desc_extra, stime_utime_ratio * 100.0);
  7545. }
  7546. #else
  7547. #include <sys/time.h>
  7548. double current_time(int reset)
  7549. {
  7550. struct timeval tv;
  7551. (void)reset;
  7552. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  7553. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  7554. }
  7555. #endif /* _WIN32 */
  7556. #if defined(HAVE_GET_CYCLES)
  7557. #if defined(WOLFSSL_ESPIDF)
  7558. static WC_INLINE word64 get_xtensa_cycles(void)
  7559. {
  7560. return xthal_get_ccount_ex();
  7561. }
  7562. /* implement other architectures here */
  7563. #else
  7564. static WC_INLINE word64 get_intel_cycles(void)
  7565. {
  7566. unsigned int lo_c, hi_c;
  7567. __asm__ __volatile__ (
  7568. "cpuid\n\t"
  7569. "rdtsc"
  7570. : "=a"(lo_c), "=d"(hi_c) /* out */
  7571. : "a"(0) /* in */
  7572. : "%ebx", "%ecx"); /* clobber */
  7573. return ((word64)lo_c) | (((word64)hi_c) << 32);
  7574. }
  7575. #endif
  7576. #endif /* HAVE_GET_CYCLES */
  7577. void benchmark_configure(int block_size)
  7578. {
  7579. /* must be greater than 0 */
  7580. if (block_size > 0) {
  7581. numBlocks = numBlocks * bench_size / block_size;
  7582. bench_size = (word32)block_size;
  7583. }
  7584. }
  7585. #ifndef NO_MAIN_DRIVER
  7586. #ifndef MAIN_NO_ARGS
  7587. #ifndef WOLFSSL_BENCHMARK_ALL
  7588. /* Display the algorithm string and keep to 80 characters per line.
  7589. *
  7590. * str Algorithm string to print.
  7591. * line Length of line used so far.
  7592. */
  7593. static void print_alg(const char* str, int* line)
  7594. {
  7595. int optLen;
  7596. optLen = (int)XSTRLEN(str) + 1;
  7597. if (optLen + *line > 80) {
  7598. printf("\n ");
  7599. *line = 13;
  7600. }
  7601. *line += optLen;
  7602. }
  7603. #endif /* WOLFSSL_BENCHMARK_ALL */
  7604. /* Display the usage options of the benchmark program. */
  7605. static void Usage(void)
  7606. {
  7607. int e = 0;
  7608. #ifndef WOLFSSL_BENCHMARK_ALL
  7609. int i;
  7610. int line;
  7611. #endif
  7612. printf("benchmark\n");
  7613. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -? */
  7614. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -csv */
  7615. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -base10 */
  7616. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  7617. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -no_aad */
  7618. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -aad_size */
  7619. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -all_aad */
  7620. #else
  7621. e += 3;
  7622. #endif
  7623. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -dgst_full */
  7624. #ifndef NO_RSA
  7625. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -ras_sign */
  7626. #ifdef WOLFSSL_KEY_GEN
  7627. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -rsa-sz */
  7628. #endif
  7629. e++;
  7630. #else
  7631. e += 2;
  7632. #endif
  7633. #if !defined(NO_DH) && defined(HAVE_FFDHE_2048)
  7634. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -ffdhe2048 */
  7635. #endif
  7636. e++;
  7637. #if !defined(NO_DH) && defined(HAVE_FFDHE_3072)
  7638. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -ffdhe3072 */
  7639. #endif
  7640. e++;
  7641. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  7642. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -p256 */
  7643. #endif
  7644. e++;
  7645. #if defined(HAVE_ECC) && defined(HAVE_ECC384)
  7646. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -p384 */
  7647. #endif
  7648. e++;
  7649. #if defined(HAVE_ECC) && defined(HAVE_ECC521)
  7650. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -p521 */
  7651. #endif
  7652. e++;
  7653. #if defined(HAVE_ECC)
  7654. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -ecc-all */
  7655. #endif
  7656. e++;
  7657. #ifndef WOLFSSL_BENCHMARK_ALL
  7658. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -<alg> */
  7659. printf(" ");
  7660. line = 13;
  7661. for (i=0; bench_cipher_opt[i].str != NULL; i++)
  7662. print_alg(bench_cipher_opt[i].str + 1, &line);
  7663. printf("\n ");
  7664. line = 13;
  7665. for (i=0; bench_digest_opt[i].str != NULL; i++)
  7666. print_alg(bench_digest_opt[i].str + 1, &line);
  7667. printf("\n ");
  7668. line = 13;
  7669. for (i=0; bench_mac_opt[i].str != NULL; i++)
  7670. print_alg(bench_mac_opt[i].str + 1, &line);
  7671. printf("\n ");
  7672. line = 13;
  7673. for (i=0; bench_asym_opt[i].str != NULL; i++)
  7674. print_alg(bench_asym_opt[i].str + 1, &line);
  7675. printf("\n ");
  7676. line = 13;
  7677. for (i=0; bench_other_opt[i].str != NULL; i++)
  7678. print_alg(bench_other_opt[i].str + 1, &line);
  7679. printf("\n ");
  7680. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  7681. line = 13;
  7682. for (i=0; bench_pq_asym_opt[i].str != NULL; i++)
  7683. print_alg(bench_pq_asym_opt[i].str + 1, &line);
  7684. #if defined(HAVE_LIBOQS)
  7685. for (i=0; bench_pq_asym_opt2[i].str != NULL; i++)
  7686. print_alg(bench_pq_asym_opt2[i].str + 1, &line);
  7687. printf("\n");
  7688. #endif /* HAVE_LIBOQS */
  7689. #endif /* HAVE_PQC */
  7690. #endif /* !WOLFSSL_BENCHMARK_ALL */
  7691. e++;
  7692. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -lng */
  7693. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option <num> */
  7694. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -blocks <num> */
  7695. #ifdef WC_ENABLE_BENCH_THREADING
  7696. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -threads <num> */
  7697. #endif
  7698. e++;
  7699. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -print */
  7700. }
  7701. /* Match the command line argument with the string.
  7702. *
  7703. * arg Command line argument.
  7704. * str String to check for.
  7705. * return 1 if the command line argument matches the string, 0 otherwise.
  7706. */
  7707. static int string_matches(const char* arg, const char* str)
  7708. {
  7709. int len = (int)XSTRLEN(str) + 1;
  7710. return XSTRNCMP(arg, str, len) == 0;
  7711. }
  7712. #endif /* MAIN_NO_ARGS */
  7713. /*
  7714. ** ----------------------------------------------------------------------------
  7715. ** determine how the benchmarks are called, the function name varies:
  7716. ** ----------------------------------------------------------------------------
  7717. */
  7718. #if !defined(NO_MAIN_DRIVER) && !defined(NO_MAIN_FUNCTION)
  7719. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  7720. /* for some environments, we'll call a function wolf_benchmark_task: */
  7721. int wolf_benchmark_task(void)
  7722. #elif defined(MAIN_NO_ARGS)
  7723. /* otherwise we'll use main() with no arguments as desired: */
  7724. int main()
  7725. #else
  7726. /* else we'll be calling main with default arg parameters */
  7727. int main(int argc, char** argv)
  7728. #endif
  7729. {
  7730. #ifdef WOLFSSL_ESPIDF
  7731. int argc = construct_argv();
  7732. char** argv = (char**)__argv;
  7733. #endif
  7734. return wolfcrypt_benchmark_main(argc, argv);
  7735. }
  7736. #endif /* NO_MAIN_DRIVER && NO_MAIN_FUNCTION */
  7737. int wolfcrypt_benchmark_main(int argc, char** argv)
  7738. {
  7739. int ret = 0;
  7740. #ifndef MAIN_NO_ARGS
  7741. int optMatched;
  7742. #ifndef WOLFSSL_BENCHMARK_ALL
  7743. int i;
  7744. #endif
  7745. #endif
  7746. benchmark_static_init(1);
  7747. printf("%s------------------------------------------------------------------------------\n",
  7748. info_prefix);
  7749. printf("%s wolfSSL version %s\n", info_prefix, LIBWOLFSSL_VERSION_STRING);
  7750. printf("%s------------------------------------------------------------------------------\n",
  7751. info_prefix);
  7752. #ifndef MAIN_NO_ARGS
  7753. while (argc > 1) {
  7754. if (string_matches(argv[1], "-?")) {
  7755. if (--argc > 1) {
  7756. lng_index = XATOI((++argv)[1]);
  7757. if (lng_index<0 || lng_index>1) {
  7758. lng_index = 0;
  7759. }
  7760. }
  7761. Usage();
  7762. return 0;
  7763. }
  7764. else if (string_matches(argv[1], "-lng")) {
  7765. argc--;
  7766. argv++;
  7767. if (argc > 1) {
  7768. lng_index = XATOI(argv[1]);
  7769. if (lng_index<0 || lng_index>1) {
  7770. printf("invalid number(%d) is specified. [<num> :0-1]\n",
  7771. lng_index);
  7772. lng_index = 0;
  7773. }
  7774. }
  7775. }
  7776. else if (string_matches(argv[1], "-base10"))
  7777. base2 = 0;
  7778. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  7779. else if (string_matches(argv[1], "-no_aad"))
  7780. aes_aad_options = AAD_SIZE_ZERO;
  7781. else if (string_matches(argv[1], "-all_aad"))
  7782. aes_aad_options |= AAD_SIZE_ZERO | AAD_SIZE_DEFAULT;
  7783. else if (string_matches(argv[1], "-aad_size")) {
  7784. argc--;
  7785. argv++;
  7786. if (argc > 1) {
  7787. aes_aad_size = XATOI(argv[1]);
  7788. aes_aad_options |= AAD_SIZE_CUSTOM;
  7789. }
  7790. }
  7791. #endif
  7792. else if (string_matches(argv[1], "-dgst_full"))
  7793. digest_stream = 0;
  7794. #ifndef NO_RSA
  7795. else if (string_matches(argv[1], "-rsa_sign"))
  7796. rsa_sign_verify = 1;
  7797. #endif
  7798. #if !defined(NO_DH) && defined(HAVE_FFDHE_2048)
  7799. else if (string_matches(argv[1], "-ffdhe2048"))
  7800. use_ffdhe = 2048;
  7801. #endif
  7802. #if !defined(NO_DH) && defined(HAVE_FFDHE_3072)
  7803. else if (string_matches(argv[1], "-ffdhe3072"))
  7804. use_ffdhe = 3072;
  7805. #endif
  7806. #if !defined(NO_DH) && defined(HAVE_FFDHE_4096)
  7807. else if (string_matches(argv[1], "-ffdhe4096"))
  7808. use_ffdhe = 4096;
  7809. #endif
  7810. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  7811. else if (string_matches(argv[1], "-p256"))
  7812. bench_asym_algs |= BENCH_ECC_P256;
  7813. #endif
  7814. #if defined(HAVE_ECC) && defined(HAVE_ECC384)
  7815. else if (string_matches(argv[1], "-p384"))
  7816. bench_asym_algs |= BENCH_ECC_P384;
  7817. #endif
  7818. #if defined(HAVE_ECC) && defined(HAVE_ECC521)
  7819. else if (string_matches(argv[1], "-p521"))
  7820. bench_asym_algs |= BENCH_ECC_P521;
  7821. #endif
  7822. #ifdef BENCH_ASYM
  7823. else if (string_matches(argv[1], "-csv")) {
  7824. csv_format = 1;
  7825. }
  7826. #endif
  7827. #ifdef WC_ENABLE_BENCH_THREADING
  7828. else if (string_matches(argv[1], "-threads")) {
  7829. argc--;
  7830. argv++;
  7831. if (argc > 1) {
  7832. g_threadCount = XATOI(argv[1]);
  7833. if (g_threadCount < 1 || lng_index > 128){
  7834. printf("invalid number(%d) is specified. [<num> :1-128]\n",
  7835. g_threadCount);
  7836. g_threadCount = 0;
  7837. }
  7838. }
  7839. }
  7840. else if (string_matches(argv[1], "-print")) {
  7841. gPrintStats = 1;
  7842. }
  7843. #endif
  7844. else if (string_matches(argv[1], "-blocks")) {
  7845. argc--;
  7846. argv++;
  7847. if (argc > 1)
  7848. numBlocks = XATOI(argv[1]);
  7849. }
  7850. else if (argv[1][0] == '-') {
  7851. optMatched = 0;
  7852. #ifndef WOLFSSL_BENCHMARK_ALL
  7853. /* Check known algorithm choosing command line options. */
  7854. /* Known cipher algorithms */
  7855. for (i=0; !optMatched && bench_cipher_opt[i].str != NULL; i++) {
  7856. if (string_matches(argv[1], bench_cipher_opt[i].str)) {
  7857. bench_cipher_algs |= bench_cipher_opt[i].val;
  7858. bench_all = 0;
  7859. optMatched = 1;
  7860. }
  7861. }
  7862. /* Known digest algorithms */
  7863. for (i=0; !optMatched && bench_digest_opt[i].str != NULL; i++) {
  7864. if (string_matches(argv[1], bench_digest_opt[i].str)) {
  7865. bench_digest_algs |= bench_digest_opt[i].val;
  7866. bench_all = 0;
  7867. optMatched = 1;
  7868. }
  7869. }
  7870. /* Known MAC algorithms */
  7871. for (i=0; !optMatched && bench_mac_opt[i].str != NULL; i++) {
  7872. if (string_matches(argv[1], bench_mac_opt[i].str)) {
  7873. bench_mac_algs |= bench_mac_opt[i].val;
  7874. bench_all = 0;
  7875. optMatched = 1;
  7876. }
  7877. }
  7878. /* Known asymmetric algorithms */
  7879. for (i=0; !optMatched && bench_asym_opt[i].str != NULL; i++) {
  7880. if (string_matches(argv[1], bench_asym_opt[i].str)) {
  7881. bench_asym_algs |= bench_asym_opt[i].val;
  7882. bench_all = 0;
  7883. optMatched = 1;
  7884. }
  7885. }
  7886. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  7887. /* Known asymmetric post-quantum algorithms */
  7888. for (i=0; !optMatched && bench_pq_asym_opt[i].str != NULL; i++) {
  7889. if (string_matches(argv[1], bench_pq_asym_opt[i].str)) {
  7890. bench_pq_asym_algs |= bench_pq_asym_opt[i].val;
  7891. bench_all = 0;
  7892. optMatched = 1;
  7893. }
  7894. }
  7895. /* Both bench_pq_asym_opt and bench_pq_asym_opt2 are looking for
  7896. * -pq, so we need to do a special case for -pq since optMatched
  7897. * was set to 1 just above. */
  7898. if (string_matches(argv[1], bench_pq_asym_opt[0].str)) {
  7899. bench_pq_asym_algs2 |= bench_pq_asym_opt2[0].val;
  7900. bench_all = 0;
  7901. optMatched = 1;
  7902. }
  7903. for (i=1; !optMatched && bench_pq_asym_opt2[i].str != NULL; i++) {
  7904. if (string_matches(argv[1], bench_pq_asym_opt2[i].str)) {
  7905. bench_pq_asym_algs2 |= bench_pq_asym_opt2[i].val;
  7906. bench_all = 0;
  7907. optMatched = 1;
  7908. }
  7909. }
  7910. #endif /* HAVE_PQC */
  7911. /* Other known cryptographic algorithms */
  7912. for (i=0; !optMatched && bench_other_opt[i].str != NULL; i++) {
  7913. if (string_matches(argv[1], bench_other_opt[i].str)) {
  7914. bench_other_algs |= bench_other_opt[i].val;
  7915. bench_all = 0;
  7916. optMatched = 1;
  7917. }
  7918. }
  7919. #endif
  7920. if (!optMatched) {
  7921. printf("Option not recognized: %s\n", argv[1]);
  7922. Usage();
  7923. return 1;
  7924. }
  7925. }
  7926. else {
  7927. /* parse for block size */
  7928. benchmark_configure(XATOI(argv[1]));
  7929. }
  7930. argc--;
  7931. argv++;
  7932. }
  7933. #endif /* MAIN_NO_ARGS */
  7934. #if defined(WOLFSSL_BENCHMARK_FIXED_CSV)
  7935. /* when defined, we'll always output CSV regardless of params.
  7936. ** this is typically convenient in embedded environments.
  7937. */
  7938. csv_format = 1;
  7939. #endif
  7940. #if defined(WC_ENABLE_BENCH_THREADING) && !defined(WOLFSSL_ASYNC_CRYPT)
  7941. if (g_threadCount > 1) {
  7942. ret = benchmark_test_threaded(NULL);
  7943. }
  7944. else
  7945. #endif
  7946. {
  7947. #ifdef HAVE_STACK_SIZE
  7948. ret = StackSizeCheck(NULL, benchmark_test);
  7949. #else
  7950. ret = benchmark_test(NULL);
  7951. #endif
  7952. }
  7953. return ret;
  7954. }
  7955. #endif /* !NO_MAIN_DRIVER */
  7956. #else
  7957. #if !defined(NO_MAIN_DRIVER) && !defined(NO_MAIN_FUNCTION)
  7958. int main(void) { return 0; }
  7959. #endif
  7960. #endif /* !NO_CRYPT_BENCHMARK */