2
0

benchmark.c 434 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670
  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. /* Some common, optional build settings:
  23. * these can also be set in wolfssl/options.h or user_settings.h
  24. * -------------------------------------------------------------
  25. * make the binary always use CSV format:
  26. * WOLFSSL_BENCHMARK_FIXED_CSV
  27. *
  28. * choose to use the same units, regardless of scale. pick 1:
  29. * WOLFSSL_BENCHMARK_FIXED_UNITS_GB
  30. * WOLFSSL_BENCHMARK_FIXED_UNITS_MB
  31. * WOLFSSL_BENCHMARK_FIXED_UNITS_KB
  32. * WOLFSSL_BENCHMARK_FIXED_UNITS_B
  33. *
  34. * when the output should be in machine-parseable format:
  35. * GENERATE_MACHINE_PARSEABLE_REPORT
  36. *
  37. * use microseconds as the unit of time:
  38. * BENCH_MICROSECOND
  39. *
  40. * display mean, max, min and sd of operation durations:
  41. * MULTI_VALUE_STATISTICS
  42. *
  43. * Enable tracking of the stats into an allocated linked list:
  44. * (use -print to display results):
  45. * WC_BENCH_TRACK_STATS
  46. *
  47. * set the default devId for cryptocb to the value instead of INVALID_DEVID
  48. * WC_USE_DEVID=0x1234
  49. *
  50. * Turn on benchmark timing debugging (CPU Cycles, RTOS ticks, etc)
  51. * DEBUG_WOLFSSL_BENCHMARK_TIMING
  52. *
  53. */
  54. #ifdef HAVE_CONFIG_H
  55. #include <config.h>
  56. #endif
  57. #ifndef WOLFSSL_USER_SETTINGS
  58. #include <wolfssl/options.h>
  59. #endif
  60. #include <wolfssl/wolfcrypt/settings.h> /* also picks up user_settings.h */
  61. /* Macro to disable benchmark */
  62. #ifndef NO_CRYPT_BENCHMARK
  63. #define WC_ALLOC_DO_ON_FAILURE() do { printf("out of memory at benchmark.c L %d\n", __LINE__); ret = MEMORY_E; goto exit; } while (0)
  64. #include <wolfssl/wolfcrypt/types.h>
  65. #include <wolfssl/wolfcrypt/wc_port.h>
  66. #include <wolfssl/wolfcrypt/wolfmath.h>
  67. #include <wolfssl/wolfcrypt/memory.h>
  68. #include <wolfssl/wolfcrypt/random.h>
  69. #include <wolfssl/wolfcrypt/error-crypt.h>
  70. #include <wolfssl/wolfcrypt/asn.h>
  71. #include <wolfssl/version.h>
  72. #ifdef WOLFSSL_LINUXKM
  73. /* remap current_time() -- collides with a function in kernel linux/fs.h */
  74. #define current_time benchmark_current_time
  75. #endif /* WOLFSSL_LINUXKM */
  76. #ifdef HAVE_CHACHA
  77. #include <wolfssl/wolfcrypt/chacha.h>
  78. #endif
  79. #ifdef HAVE_POLY1305
  80. #include <wolfssl/wolfcrypt/poly1305.h>
  81. #endif
  82. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  83. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  84. #endif
  85. #ifndef NO_AES
  86. #include <wolfssl/wolfcrypt/aes.h>
  87. #endif
  88. #ifdef HAVE_CAMELLIA
  89. #include <wolfssl/wolfcrypt/camellia.h>
  90. #endif
  91. #ifdef WOLFSSL_SM4
  92. #include <wolfssl/wolfcrypt/sm4.h>
  93. #endif
  94. #ifndef NO_MD5
  95. #include <wolfssl/wolfcrypt/md5.h>
  96. #endif
  97. #ifndef NO_SHA
  98. #include <wolfssl/wolfcrypt/sha.h>
  99. #endif
  100. #ifndef NO_SHA256
  101. #include <wolfssl/wolfcrypt/sha256.h>
  102. #endif
  103. #if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)
  104. #include <wolfssl/wolfcrypt/sha512.h>
  105. #endif
  106. #ifdef WOLFSSL_SHA3
  107. #include <wolfssl/wolfcrypt/sha3.h>
  108. #endif
  109. #ifdef WOLFSSL_SM3
  110. #include <wolfssl/wolfcrypt/sm3.h>
  111. #endif
  112. #ifndef NO_RSA
  113. #include <wolfssl/wolfcrypt/rsa.h>
  114. #endif
  115. #ifdef WOLFSSL_RIPEMD
  116. #include <wolfssl/wolfcrypt/ripemd.h>
  117. #endif
  118. #ifdef WOLFSSL_CMAC
  119. #include <wolfssl/wolfcrypt/cmac.h>
  120. #endif
  121. #ifndef NO_DH
  122. #include <wolfssl/wolfcrypt/dh.h>
  123. #endif
  124. #ifndef NO_DES3
  125. #include <wolfssl/wolfcrypt/des3.h>
  126. #endif
  127. #ifndef NO_RC4
  128. #include <wolfssl/wolfcrypt/arc4.h>
  129. #endif
  130. #ifndef NO_HMAC
  131. #include <wolfssl/wolfcrypt/hmac.h>
  132. #endif
  133. #ifdef WOLFSSL_SIPHASH
  134. #include <wolfssl/wolfcrypt/siphash.h>
  135. #endif
  136. #include <wolfssl/wolfcrypt/kdf.h>
  137. #ifndef NO_PWDBASED
  138. #include <wolfssl/wolfcrypt/pwdbased.h>
  139. #endif
  140. #ifdef HAVE_ECC
  141. #include <wolfssl/wolfcrypt/ecc.h>
  142. #endif
  143. #ifdef WOLFSSL_SM2
  144. #include <wolfssl/wolfcrypt/sm2.h>
  145. #endif
  146. #ifdef HAVE_CURVE25519
  147. #include <wolfssl/wolfcrypt/curve25519.h>
  148. #endif
  149. #ifdef HAVE_ED25519
  150. #include <wolfssl/wolfcrypt/ed25519.h>
  151. #endif
  152. #ifdef HAVE_CURVE448
  153. #include <wolfssl/wolfcrypt/curve448.h>
  154. #endif
  155. #ifdef HAVE_ED448
  156. #include <wolfssl/wolfcrypt/ed448.h>
  157. #endif
  158. #ifdef WOLFSSL_HAVE_KYBER
  159. #include <wolfssl/wolfcrypt/kyber.h>
  160. #ifdef WOLFSSL_WC_KYBER
  161. #include <wolfssl/wolfcrypt/wc_kyber.h>
  162. #endif
  163. #if defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  164. #include <wolfssl/wolfcrypt/ext_kyber.h>
  165. #endif
  166. #endif
  167. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  168. #include <wolfssl/wolfcrypt/lms.h>
  169. #ifdef HAVE_LIBLMS
  170. #include <wolfssl/wolfcrypt/ext_lms.h>
  171. #else
  172. #include <wolfssl/wolfcrypt/wc_lms.h>
  173. #endif
  174. #endif
  175. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  176. #include <wolfssl/wolfcrypt/xmss.h>
  177. #ifdef HAVE_LIBXMSS
  178. #include <wolfssl/wolfcrypt/ext_xmss.h>
  179. #else
  180. #include <wolfssl/wolfcrypt/wc_xmss.h>
  181. #endif
  182. #endif
  183. #ifdef WOLFCRYPT_HAVE_ECCSI
  184. #include <wolfssl/wolfcrypt/eccsi.h>
  185. #endif
  186. #ifdef WOLFCRYPT_HAVE_SAKKE
  187. #include <wolfssl/wolfcrypt/sakke.h>
  188. #endif
  189. #if defined(HAVE_PQC)
  190. #if defined(HAVE_FALCON)
  191. #include <wolfssl/wolfcrypt/falcon.h>
  192. #endif
  193. #if defined(HAVE_DILITHIUM)
  194. #include <wolfssl/wolfcrypt/dilithium.h>
  195. #endif
  196. #if defined(HAVE_SPHINCS)
  197. #include <wolfssl/wolfcrypt/sphincs.h>
  198. #endif
  199. #endif
  200. #ifdef WOLF_CRYPTO_CB
  201. #include <wolfssl/wolfcrypt/cryptocb.h>
  202. #ifdef HAVE_INTEL_QA_SYNC
  203. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  204. #endif
  205. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  206. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  207. #endif
  208. #ifdef HAVE_RENESAS_SYNC
  209. #include <wolfssl/wolfcrypt/port/renesas/renesas_sync.h>
  210. #endif
  211. #endif
  212. #ifdef WOLFSSL_ASYNC_CRYPT
  213. #include <wolfssl/wolfcrypt/async.h>
  214. #endif
  215. #ifdef USE_FLAT_BENCHMARK_H
  216. #include "benchmark.h"
  217. #else
  218. #include "wolfcrypt/benchmark/benchmark.h"
  219. #endif
  220. /* define the max length for each string of metric reported */
  221. #ifndef WC_BENCH_MAX_LINE_LEN
  222. #define WC_BENCH_MAX_LINE_LEN 150
  223. #endif
  224. /* default units per second. See WOLFSSL_BENCHMARK_FIXED_UNITS_* to change */
  225. #define WOLFSSL_FIXED_UNIT "MB" /* may be re-set by fixed units */
  226. #define MILLION_VALUE 1000000.0
  227. #ifdef BENCH_MICROSECOND
  228. #define WOLFSSL_FIXED_TIME_UNIT "μs"
  229. #define WOLFSSL_BENCHMARK_FIXED_UNITS_KB
  230. #else
  231. #define WOLFSSL_FIXED_TIME_UNIT "s"
  232. #endif
  233. #ifdef MULTI_VALUE_STATISTICS
  234. #define STATS_CLAUSE_SEPARATOR ""
  235. #define DECLARE_MULTI_VALUE_STATS_VARS() double max = 0, min = 0, sum = 0,\
  236. squareSum = 0, prev = 0, delta;\
  237. int runs = 0;
  238. #define RECORD_MULTI_VALUE_STATS() if (runs == 0) {\
  239. delta = current_time(0) - start;\
  240. min = delta;\
  241. max = delta;\
  242. }\
  243. else {\
  244. delta = current_time(0) - prev;\
  245. }\
  246. if (max < delta)\
  247. max = delta;\
  248. else if (min > delta)\
  249. min = delta;\
  250. sum += delta;\
  251. squareSum += delta * delta;\
  252. runs++;\
  253. prev = current_time(0)
  254. #define RESET_MULTI_VALUE_STATS_VARS() prev = 0;\
  255. runs = 0;\
  256. sum = 0;\
  257. squareSum = 0
  258. #else
  259. #define STATS_CLAUSE_SEPARATOR "\n"
  260. #define DECLARE_MULTI_VALUE_STATS_VARS()
  261. #define RECORD_MULTI_VALUE_STATS() WC_DO_NOTHING
  262. #define RESET_MULTI_VALUE_STATS_VARS() WC_DO_NOTHING
  263. #endif
  264. #ifdef WOLFSSL_NO_FLOAT_FMT
  265. #define FLT_FMT "%0ld,%09lu"
  266. #define FLT_FMT_PREC "%0ld.%0*lu"
  267. #define FLT_FMT_PREC2 FLT_FMT_PREC
  268. #define FLT_FMT_ARGS(x) (long)(x), ((x) < 0) ? \
  269. (unsigned long)(-(((x) - (double)(long)(x)) * 1000000000.0)) : \
  270. (unsigned long)(((x) - (double)(long)(x)) * 1000000000.0)
  271. static const double pow_10_array[] = { 0.0, 1.0, 10.0, 100.0, 1000.0, \
  272. 10000.0, 100000.0, 1000000.0, \
  273. 10000000.0, 100000000.0, \
  274. 1000000000.0 };
  275. #define FLT_FMT_PREC_ARGS(p, x) \
  276. (long)(x), \
  277. p, \
  278. (x) >= 0.0 ? \
  279. (unsigned long int)((((x) - (double)(long)(x)) * \
  280. pow_10_array[(p)+1]) + 0.5) : \
  281. (unsigned long int)((((-(x)) - (double)((long)-(x))) * \
  282. pow_10_array[(p)+1]) + 0.5)
  283. #define FLT_FMT_PREC2_ARGS(w, p, x) FLT_FMT_PREC_ARGS(p, x)
  284. #else
  285. #define FLT_FMT "%f"
  286. #define FLT_FMT_PREC "%.*f"
  287. #define FLT_FMT_PREC2 "%*.*f"
  288. #define FLT_FMT_ARGS(x) x
  289. #define FLT_FMT_PREC_ARGS(p, x) p, x
  290. #define FLT_FMT_PREC2_ARGS(w, p, x) w, p, x
  291. #endif /* WOLFSSL_NO_FLOAT_FMT */
  292. #ifdef WOLFSSL_ESPIDF
  293. #ifdef configTICK_RATE_HZ
  294. /* Define CPU clock cycles per tick of FreeRTOS clock
  295. * CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ is typically a value like 240
  296. * configTICK_RATE_HZ is typically 100 or 1000.
  297. **/
  298. #define CPU_TICK_CYCLES ( \
  299. (CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ * MILLION_VALUE) \
  300. / configTICK_RATE_HZ \
  301. )
  302. #endif
  303. #if defined(CONFIG_IDF_TARGET_ESP32C2)
  304. #include "driver/gptimer.h"
  305. static gptimer_handle_t esp_gptimer = NULL;
  306. static gptimer_config_t esp_timer_config = {
  307. .clk_src = GPTIMER_CLK_SRC_DEFAULT,
  308. .direction = GPTIMER_COUNT_UP,
  309. .resolution_hz = CONFIG_XTAL_FREQ * 100000,
  310. };
  311. #elif defined(CONFIG_IDF_TARGET_ESP32C3) || \
  312. defined(CONFIG_IDF_TARGET_ESP32C6)
  313. #include <esp_cpu.h>
  314. #include "driver/gptimer.h"
  315. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  316. #define RESOLUTION_SCALE 100
  317. static gptimer_handle_t esp_gptimer = NULL;
  318. static gptimer_config_t esp_timer_config = {
  319. .clk_src = GPTIMER_CLK_SRC_DEFAULT,
  320. .direction = GPTIMER_COUNT_UP,
  321. .resolution_hz = CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ * (MILLION_VALUE / RESOLUTION_SCALE), /* CONFIG_XTAL_FREQ = 40, CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ = 160 */
  322. };
  323. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  324. #elif defined(CONFIG_IDF_TARGET_ESP32) || \
  325. defined(CONFIG_IDF_TARGET_ESP32S2) || \
  326. defined(CONFIG_IDF_TARGET_ESP32S3)
  327. #include <xtensa/hal.h>
  328. #elif defined(CONFIG_IDF_TARGET_ESP32H2)
  329. #else
  330. /* Other platform */
  331. #endif
  332. #include <esp_log.h>
  333. #endif /* WOLFSSL_ESPIDF */
  334. #if defined(HAVE_PTHREAD) || \
  335. (!defined(NO_CRYPT_BENCHMARK) && !defined(NO_STDIO_FILESYSTEM) && \
  336. !defined(NO_ERROR_STRINGS) && !defined(NO_MAIN_DRIVER) && \
  337. !defined(BENCH_EMBEDDED))
  338. #include <errno.h>
  339. #if !defined(WOLFSSL_ZEPHYR) && !defined(_WIN32)
  340. #include <unistd.h>
  341. #endif
  342. #endif
  343. #if defined(WOLFSSL_ZEPHYR) || defined(NO_STDIO_FILESYSTEM) || !defined(XFFLUSH)
  344. /* fflush in Zephyr doesn't work on stdout and stderr. Use
  345. * CONFIG_LOG_MODE_IMMEDIATE compilation option instead. */
  346. #undef XFFLUSH
  347. #define XFFLUSH(...) WC_DO_NOTHING
  348. #endif
  349. /* only for stack size check */
  350. #include <wolfssl/wolfcrypt/mem_track.h>
  351. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  352. #define WC_ENABLE_BENCH_THREADING
  353. #endif
  354. /* enable tracking of stats for threaded benchmark */
  355. #if defined(WC_ENABLE_BENCH_THREADING) && !defined(WC_BENCH_TRACK_STATS)
  356. #define WC_BENCH_TRACK_STATS
  357. #endif
  358. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  359. static const char info_prefix[] = "###, ";
  360. static const char err_prefix[] = "!!!, ";
  361. #else
  362. static const char info_prefix[] = "";
  363. static const char err_prefix[] = "";
  364. #endif
  365. /* printf mappings */
  366. #ifdef FREESCALE_MQX
  367. #include <mqx.h>
  368. /* see wc_port.h for fio.h and nio.h includes */
  369. #elif defined(FREESCALE_KSDK_1_3)
  370. #include "fsl_debug_console.h"
  371. #include "fsl_os_abstraction.h"
  372. #undef printf
  373. #define printf PRINTF
  374. #elif defined(WOLFSSL_DEOS)
  375. #include <deos.h>
  376. #include <printx.h>
  377. #undef printf
  378. #define printf printx
  379. #elif defined(MICRIUM)
  380. #if (OS_VERSION < 50000)
  381. #include <bsp_ser.h>
  382. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  383. #undef printf
  384. #define printf BSP_Ser_Printf
  385. #endif
  386. #elif defined(WOLFSSL_ZEPHYR)
  387. #include <stdio.h>
  388. #define BENCH_EMBEDDED
  389. #define printf printfk
  390. static int printfk(const char *fmt, ...)
  391. {
  392. int ret;
  393. char line[WC_BENCH_MAX_LINE_LEN];
  394. va_list ap;
  395. va_start(ap, fmt);
  396. ret = vsnprintf(line, sizeof(line), fmt, ap);
  397. line[sizeof(line)-1] = '\0';
  398. printk("%s", line);
  399. va_end(ap);
  400. return ret;
  401. }
  402. #elif defined(WOLFSSL_TELIT_M2MB)
  403. #include <stdarg.h>
  404. #include <stdio.h>
  405. #include <string.h>
  406. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  407. /* remap printf */
  408. #undef printf
  409. #define printf M2M_LOG_INFO
  410. /* OS requires occasional sleep() */
  411. #ifndef TEST_SLEEP_MS
  412. #define TEST_SLEEP_MS 50
  413. #endif
  414. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  415. /* don't use file system for these tests, since ./certs dir isn't loaded */
  416. #undef NO_FILESYSTEM
  417. #define NO_FILESYSTEM
  418. /* ANDROID_V454 (for android studio) displays information in a textview
  419. * and redirects printf to the textview output instead of using
  420. * __android_log_print() */
  421. #elif defined(ANDROID) && !defined(ANDROID_V454)
  422. #ifdef XMALLOC_USER
  423. #include <stdlib.h> /* we're using malloc / free direct here */
  424. #endif
  425. #ifndef STRING_USER
  426. #include <stdio.h>
  427. #endif
  428. #include <android/log.h>
  429. #define printf(...) \
  430. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  431. #define fprintf(fp, ...) \
  432. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  433. #else
  434. #if defined(XMALLOC_USER) || defined(FREESCALE_MQX)
  435. /* MQX classic needs for EXIT_FAILURE */
  436. #include <stdlib.h> /* we're using malloc / free direct here */
  437. #endif
  438. #if !defined(STRING_USER) && !defined(NO_STDIO_FILESYSTEM)
  439. #include <string.h>
  440. #include <stdio.h>
  441. #endif
  442. /* enable way for customer to override test/bench printf */
  443. #ifdef XPRINTF
  444. #undef printf
  445. #define printf XPRINTF
  446. #elif defined(NETOS)
  447. #undef printf
  448. #define printf dc_log_printf
  449. #endif
  450. #endif
  451. #ifdef HAVE_FIPS
  452. #include <wolfssl/wolfcrypt/fips_test.h>
  453. static void myFipsCb(int ok, int err, const char* hash)
  454. {
  455. printf("%sin my Fips callback, ok = %d, err = %d\n",
  456. ok ? info_prefix : err_prefix, ok, err);
  457. printf("%smessage = %s\n", ok ? info_prefix : err_prefix,
  458. wc_GetErrorString(err));
  459. printf("%shash = %s\n", ok ? info_prefix : err_prefix, hash);
  460. if (err == IN_CORE_FIPS_E) {
  461. printf("%sIn core integrity hash check failure, copy above hash\n",
  462. err_prefix);
  463. printf("%sinto verifyCore[] in fips_test.c and rebuild\n",
  464. err_prefix);
  465. }
  466. }
  467. #endif
  468. #ifdef WOLFSSL_STATIC_MEMORY
  469. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  470. #else
  471. #define HEAP_HINT NULL
  472. #endif /* WOLFSSL_STATIC_MEMORY */
  473. #ifndef EXIT_FAILURE
  474. #define EXIT_FAILURE 1
  475. #endif
  476. #undef LIBCALL_CHECK_RET
  477. #if defined(NO_STDIO_FILESYSTEM) || defined(NO_ERROR_STRINGS) || \
  478. defined(NO_MAIN_DRIVER) || defined(BENCH_EMBEDDED)
  479. #define LIBCALL_CHECK_RET(...) __VA_ARGS__
  480. #else
  481. #define LIBCALL_CHECK_RET(...) do { \
  482. int _libcall_ret = (__VA_ARGS__); \
  483. if (_libcall_ret < 0) { \
  484. printf("%s%s L%d error %d for \"%s\"\n", \
  485. err_prefix, __FILE__, __LINE__, \
  486. errno, #__VA_ARGS__); \
  487. XFFLUSH(stdout); \
  488. _exit(1); \
  489. } \
  490. } while(0)
  491. #endif
  492. #undef THREAD_CHECK_RET
  493. #define THREAD_CHECK_RET(...) do { \
  494. int _thread_ret = (__VA_ARGS__); \
  495. if (_thread_ret != 0) { \
  496. errno = _thread_ret; \
  497. printf("%s%s L%d error %d for \"%s\"\n", \
  498. err_prefix, __FILE__, __LINE__, \
  499. _thread_ret, #__VA_ARGS__); \
  500. XFFLUSH(stdout); \
  501. _exit(1); \
  502. } \
  503. } while(0)
  504. /* optional macro to add sleep between tests */
  505. #ifndef TEST_SLEEP
  506. /* stub the sleep macro */
  507. #define TEST_SLEEP() WC_DO_NOTHING
  508. #endif
  509. #define TEST_STRING "Everyone gets Friday off."
  510. #define TEST_STRING_SZ 25
  511. /* Bit values for each algorithm that is able to be benchmarked.
  512. * Common grouping of algorithms also.
  513. * Each algorithm has a unique value for its type e.g. cipher.
  514. */
  515. /* Cipher algorithms. */
  516. #define BENCH_AES_CBC 0x00000001
  517. #define BENCH_AES_GCM 0x00000002
  518. #define BENCH_AES_ECB 0x00000004
  519. #define BENCH_AES_XTS 0x00000008
  520. #define BENCH_AES_CTR 0x00000010
  521. #define BENCH_AES_CCM 0x00000020
  522. #define BENCH_CAMELLIA 0x00000100
  523. #define BENCH_ARC4 0x00000200
  524. #define BENCH_CHACHA20 0x00001000
  525. #define BENCH_CHACHA20_POLY1305 0x00002000
  526. #define BENCH_DES 0x00004000
  527. #define BENCH_AES_CFB 0x00010000
  528. #define BENCH_AES_OFB 0x00020000
  529. #define BENCH_AES_SIV 0x00040000
  530. #define BENCH_SM4_CBC 0x00080000
  531. #define BENCH_SM4_GCM 0x00100000
  532. #define BENCH_SM4_CCM 0x00200000
  533. #define BENCH_SM4 (BENCH_SM4_CBC | BENCH_SM4_GCM | BENCH_SM4_CCM)
  534. /* Digest algorithms. */
  535. #define BENCH_MD5 0x00000001
  536. #define BENCH_POLY1305 0x00000002
  537. #define BENCH_SHA 0x00000004
  538. #define BENCH_SHA224 0x00000010
  539. #define BENCH_SHA256 0x00000020
  540. #define BENCH_SHA384 0x00000040
  541. #define BENCH_SHA512 0x00000080
  542. #define BENCH_SHA2 (BENCH_SHA224 | BENCH_SHA256 | \
  543. BENCH_SHA384 | BENCH_SHA512)
  544. #define BENCH_SHA3_224 0x00000100
  545. #define BENCH_SHA3_256 0x00000200
  546. #define BENCH_SHA3_384 0x00000400
  547. #define BENCH_SHA3_512 0x00000800
  548. #define BENCH_SHA3 (BENCH_SHA3_224 | BENCH_SHA3_256 | \
  549. BENCH_SHA3_384 | BENCH_SHA3_512)
  550. #define BENCH_SHAKE128 0x00001000
  551. #define BENCH_SHAKE256 0x00002000
  552. #define BENCH_SHAKE (BENCH_SHAKE128 | BENCH_SHAKE256)
  553. #define BENCH_RIPEMD 0x00004000
  554. #define BENCH_BLAKE2B 0x00008000
  555. #define BENCH_BLAKE2S 0x00010000
  556. #define BENCH_SM3 0x00020000
  557. /* MAC algorithms. */
  558. #define BENCH_CMAC 0x00000001
  559. #define BENCH_HMAC_MD5 0x00000002
  560. #define BENCH_HMAC_SHA 0x00000004
  561. #define BENCH_HMAC_SHA224 0x00000010
  562. #define BENCH_HMAC_SHA256 0x00000020
  563. #define BENCH_HMAC_SHA384 0x00000040
  564. #define BENCH_HMAC_SHA512 0x00000080
  565. #define BENCH_HMAC (BENCH_HMAC_MD5 | BENCH_HMAC_SHA | \
  566. BENCH_HMAC_SHA224 | BENCH_HMAC_SHA256 | \
  567. BENCH_HMAC_SHA384 | BENCH_HMAC_SHA512)
  568. #define BENCH_PBKDF2 0x00000100
  569. #define BENCH_SIPHASH 0x00000200
  570. /* KDF algorithms */
  571. #define BENCH_SRTP_KDF 0x00000001
  572. /* Asymmetric algorithms. */
  573. #define BENCH_RSA_KEYGEN 0x00000001
  574. #define BENCH_RSA 0x00000002
  575. #define BENCH_RSA_SZ 0x00000004
  576. #define BENCH_DH 0x00000010
  577. #define BENCH_KYBER 0x00000020
  578. #define BENCH_ECC_MAKEKEY 0x00001000
  579. #define BENCH_ECC 0x00002000
  580. #define BENCH_ECC_ENCRYPT 0x00004000
  581. #define BENCH_ECC_ALL 0x00008000
  582. #define BENCH_CURVE25519_KEYGEN 0x00010000
  583. #define BENCH_CURVE25519_KA 0x00020000
  584. #define BENCH_ED25519_KEYGEN 0x00040000
  585. #define BENCH_ED25519_SIGN 0x00080000
  586. #define BENCH_CURVE448_KEYGEN 0x00100000
  587. #define BENCH_CURVE448_KA 0x00200000
  588. #define BENCH_ED448_KEYGEN 0x00400000
  589. #define BENCH_ED448_SIGN 0x00800000
  590. #define BENCH_ECC_P256 0x01000000
  591. #define BENCH_ECC_P384 0x02000000
  592. #define BENCH_ECC_P521 0x04000000
  593. #define BENCH_SM2 0x08000000
  594. #define BENCH_ECCSI_KEYGEN 0x00000020
  595. #define BENCH_ECCSI_PAIRGEN 0x00000040
  596. #define BENCH_ECCSI_VALIDATE 0x00000080
  597. #define BENCH_ECCSI 0x00000400
  598. #define BENCH_SAKKE_KEYGEN 0x10000000
  599. #define BENCH_SAKKE_RSKGEN 0x20000000
  600. #define BENCH_SAKKE_VALIDATE 0x40000000
  601. #define BENCH_SAKKE 0x80000000
  602. /* Post-Quantum Asymmetric algorithms. */
  603. #define BENCH_FALCON_LEVEL1_SIGN 0x00000001
  604. #define BENCH_FALCON_LEVEL5_SIGN 0x00000002
  605. #define BENCH_DILITHIUM_LEVEL2_SIGN 0x04000000
  606. #define BENCH_DILITHIUM_LEVEL3_SIGN 0x08000000
  607. #define BENCH_DILITHIUM_LEVEL5_SIGN 0x10000000
  608. /* Post-Quantum Asymmetric algorithms. (Part 2) */
  609. #define BENCH_SPHINCS_FAST_LEVEL1_SIGN 0x00000001
  610. #define BENCH_SPHINCS_FAST_LEVEL3_SIGN 0x00000002
  611. #define BENCH_SPHINCS_FAST_LEVEL5_SIGN 0x00000004
  612. #define BENCH_SPHINCS_SMALL_LEVEL1_SIGN 0x00000008
  613. #define BENCH_SPHINCS_SMALL_LEVEL3_SIGN 0x00000010
  614. #define BENCH_SPHINCS_SMALL_LEVEL5_SIGN 0x00000020
  615. /* Post-Quantum Stateful Hash-Based sig algorithms. */
  616. #define BENCH_LMS_HSS 0x00000001
  617. #define BENCH_XMSS_XMSSMT_SHA256 0x00000002
  618. #define BENCH_XMSS_XMSSMT_SHA512 0x00000004
  619. #define BENCH_XMSS_XMSSMT_SHAKE128 0x00000008
  620. #define BENCH_XMSS_XMSSMT_SHAKE256 0x00000010
  621. #ifndef NO_SHA256
  622. #define BENCH_XMSS_XMSSMT BENCH_XMSS_XMSSMT_SHA256
  623. #elif defined(WOLFSSL_SHA512)
  624. #define BENCH_XMSS_XMSSMT BENCH_XMSS_XMSSMT_SHA512
  625. #elif defined(WOLFSSL_SHAKE128)
  626. #define BENCH_XMSS_XMSSMT BENCH_XMSS_XMSSMT_SHAKE128
  627. #elif defined(WOLFSSL_SHAKE256)
  628. #define BENCH_XMSS_XMSSMT BENCH_XMSS_XMSSMT_SHAKE256
  629. #else
  630. #define BENCH_XMSS_XMSSMT 0x00000000
  631. #endif
  632. /* Other */
  633. #define BENCH_RNG 0x00000001
  634. #define BENCH_SCRYPT 0x00000002
  635. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  636. /* Define AES_AUTH_ADD_SZ already here, since it's used in the
  637. * static declaration of `bench_Usage_msg1`. */
  638. #if !defined(AES_AUTH_ADD_SZ) && \
  639. defined(STM32_CRYPTO) && !defined(STM32_AESGCM_PARTIAL) || \
  640. defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  641. /* For STM32 use multiple of 4 to leverage crypto hardware
  642. * Xilinx Versal requires to use multiples of 16 bytes */
  643. #define AES_AUTH_ADD_SZ 16
  644. #endif
  645. #ifndef AES_AUTH_ADD_SZ
  646. #define AES_AUTH_ADD_SZ 13
  647. #endif
  648. #endif
  649. #if (defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)) || \
  650. (defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY))
  651. #define BENCH_PQ_STATEFUL_HBS
  652. #endif
  653. /* Benchmark all compiled in algorithms.
  654. * When 1, ignore other benchmark algorithm values.
  655. * 0, only benchmark algorithm values set.
  656. */
  657. static int bench_all = 1;
  658. /* Cipher algorithms to benchmark. */
  659. static word32 bench_cipher_algs = 0;
  660. /* Digest algorithms to benchmark. */
  661. static word32 bench_digest_algs = 0;
  662. /* MAC algorithms to benchmark. */
  663. static word32 bench_mac_algs = 0;
  664. /* KDF algorithms to benchmark. */
  665. static word32 bench_kdf_algs = 0;
  666. /* Asymmetric algorithms to benchmark. */
  667. static word32 bench_asym_algs = 0;
  668. /* Post-Quantum Asymmetric algorithms to benchmark. */
  669. static word32 bench_pq_asym_algs = 0;
  670. /* Post-Quantum Asymmetric algorithms to benchmark. (Part 2)*/
  671. static word32 bench_pq_asym_algs2 = 0;
  672. /* Other cryptographic algorithms to benchmark. */
  673. static word32 bench_other_algs = 0;
  674. /* Post-Quantum Stateful Hash-Based sig algorithms to benchmark. */
  675. static word32 bench_pq_hash_sig_algs = 0;
  676. #if !defined(WOLFSSL_BENCHMARK_ALL) && !defined(NO_MAIN_DRIVER)
  677. /* The mapping of command line option to bit values. */
  678. typedef struct bench_alg {
  679. /* Command line option string. */
  680. const char* str;
  681. /* Bit values to set. */
  682. word32 val;
  683. } bench_alg;
  684. #ifndef MAIN_NO_ARGS
  685. /* All recognized cipher algorithm choosing command line options. */
  686. static const bench_alg bench_cipher_opt[] = {
  687. { "-cipher", 0xffffffff },
  688. #ifdef HAVE_AES_CBC
  689. { "-aes-cbc", BENCH_AES_CBC },
  690. #endif
  691. #ifdef HAVE_AESGCM
  692. { "-aes-gcm", BENCH_AES_GCM },
  693. #endif
  694. #ifdef WOLFSSL_AES_DIRECT
  695. { "-aes-ecb", BENCH_AES_ECB },
  696. #endif
  697. #ifdef WOLFSSL_AES_XTS
  698. { "-aes-xts", BENCH_AES_XTS },
  699. #endif
  700. #ifdef WOLFSSL_AES_CFB
  701. { "-aes-cfb", BENCH_AES_CFB },
  702. #endif
  703. #ifdef WOLFSSL_AES_OFB
  704. { "-aes-ofb", BENCH_AES_OFB },
  705. #endif
  706. #ifdef WOLFSSL_AES_COUNTER
  707. { "-aes-ctr", BENCH_AES_CTR },
  708. #endif
  709. #ifdef HAVE_AESCCM
  710. { "-aes-ccm", BENCH_AES_CCM },
  711. #endif
  712. #ifdef WOLFSSL_AES_SIV
  713. { "-aes-siv", BENCH_AES_SIV },
  714. #endif
  715. #ifdef HAVE_CAMELLIA
  716. { "-camellia", BENCH_CAMELLIA },
  717. #endif
  718. #ifndef NO_RC4
  719. { "-arc4", BENCH_ARC4 },
  720. #endif
  721. #ifdef HAVE_CHACHA
  722. { "-chacha20", BENCH_CHACHA20 },
  723. #endif
  724. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  725. { "-chacha20-poly1305", BENCH_CHACHA20_POLY1305 },
  726. #endif
  727. #ifdef WOLFSSL_SM4_CBC
  728. { "-sm4-cbc", BENCH_SM4_CBC },
  729. #endif
  730. #ifdef WOLFSSL_SM4_GCM
  731. { "-sm4-gcm", BENCH_SM4_GCM },
  732. #endif
  733. #ifdef WOLFSSL_SM4_CCM
  734. { "-sm4-ccm", BENCH_SM4_CCM },
  735. #endif
  736. #ifdef WOLFSSL_SM4
  737. { "-sm4", BENCH_SM4 },
  738. #endif
  739. #ifndef NO_DES3
  740. { "-des", BENCH_DES },
  741. #endif
  742. { NULL, 0 }
  743. };
  744. /* All recognized digest algorithm choosing command line options. */
  745. static const bench_alg bench_digest_opt[] = {
  746. { "-digest", 0xffffffff },
  747. #ifndef NO_MD5
  748. { "-md5", BENCH_MD5 },
  749. #endif
  750. #ifdef HAVE_POLY1305
  751. { "-poly1305", BENCH_POLY1305 },
  752. #endif
  753. #ifndef NO_SHA
  754. { "-sha", BENCH_SHA },
  755. #endif
  756. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256) || defined(WOLFSSL_SHA384) \
  757. || defined(WOLFSSL_SHA512)
  758. { "-sha2", BENCH_SHA2 },
  759. #endif
  760. #ifdef WOLFSSL_SHA224
  761. { "-sha224", BENCH_SHA224 },
  762. #endif
  763. #ifndef NO_SHA256
  764. { "-sha256", BENCH_SHA256 },
  765. #endif
  766. #ifdef WOLFSSL_SHA384
  767. { "-sha384", BENCH_SHA384 },
  768. #endif
  769. #ifdef WOLFSSL_SHA512
  770. { "-sha512", BENCH_SHA512 },
  771. #endif
  772. #ifdef WOLFSSL_SHA3
  773. { "-sha3", BENCH_SHA3 },
  774. #ifndef WOLFSSL_NOSHA3_224
  775. { "-sha3-224", BENCH_SHA3_224 },
  776. #endif
  777. #ifndef WOLFSSL_NOSHA3_256
  778. { "-sha3-256", BENCH_SHA3_256 },
  779. #endif
  780. #ifndef WOLFSSL_NOSHA3_384
  781. { "-sha3-384", BENCH_SHA3_384 },
  782. #endif
  783. #ifndef WOLFSSL_NOSHA3_512
  784. { "-sha3-512", BENCH_SHA3_512 },
  785. #endif
  786. #if defined(WOLFSSL_SHAKE128) || defined(WOLFSSL_SHAKE256)
  787. { "-shake", BENCH_SHAKE },
  788. #endif
  789. #ifdef WOLFSSL_SHAKE128
  790. { "-shake128", BENCH_SHAKE128 },
  791. #endif
  792. #ifdef WOLFSSL_SHAKE256
  793. { "-shake256", BENCH_SHAKE256 },
  794. #endif
  795. #endif
  796. #ifdef WOLFSSL_SM3
  797. { "-sm3", BENCH_SM3 },
  798. #endif
  799. #ifdef WOLFSSL_RIPEMD
  800. { "-ripemd", BENCH_RIPEMD },
  801. #endif
  802. #ifdef HAVE_BLAKE2
  803. { "-blake2b", BENCH_BLAKE2B },
  804. #endif
  805. #ifdef HAVE_BLAKE2S
  806. { "-blake2s", BENCH_BLAKE2S },
  807. #endif
  808. { NULL, 0 }
  809. };
  810. /* All recognized MAC algorithm choosing command line options. */
  811. static const bench_alg bench_mac_opt[] = {
  812. { "-mac", 0xffffffff },
  813. #ifdef WOLFSSL_CMAC
  814. { "-cmac", BENCH_CMAC },
  815. #endif
  816. #ifndef NO_HMAC
  817. { "-hmac", BENCH_HMAC },
  818. #ifndef NO_MD5
  819. { "-hmac-md5", BENCH_HMAC_MD5 },
  820. #endif
  821. #ifndef NO_SHA
  822. { "-hmac-sha", BENCH_HMAC_SHA },
  823. #endif
  824. #ifdef WOLFSSL_SHA224
  825. { "-hmac-sha224", BENCH_HMAC_SHA224 },
  826. #endif
  827. #ifndef NO_SHA256
  828. { "-hmac-sha256", BENCH_HMAC_SHA256 },
  829. #endif
  830. #ifdef WOLFSSL_SHA384
  831. { "-hmac-sha384", BENCH_HMAC_SHA384 },
  832. #endif
  833. #ifdef WOLFSSL_SHA512
  834. { "-hmac-sha512", BENCH_HMAC_SHA512 },
  835. #endif
  836. #ifndef NO_PWDBASED
  837. { "-pbkdf2", BENCH_PBKDF2 },
  838. #endif
  839. #endif
  840. #ifdef WOLFSSL_SIPHASH
  841. { "-siphash", BENCH_SIPHASH },
  842. #endif
  843. { NULL, 0 }
  844. };
  845. /* All recognized KDF algorithm choosing command line options. */
  846. static const bench_alg bench_kdf_opt[] = {
  847. { "-kdf", 0xffffffff },
  848. #ifdef WC_SRTP_KDF
  849. { "-srtp-kdf", BENCH_SRTP_KDF },
  850. #endif
  851. { NULL, 0 }
  852. };
  853. /* All recognized asymmetric algorithm choosing command line options. */
  854. static const bench_alg bench_asym_opt[] = {
  855. { "-asym", 0xffffffff },
  856. #ifndef NO_RSA
  857. #ifdef WOLFSSL_KEY_GEN
  858. { "-rsa-kg", BENCH_RSA_KEYGEN },
  859. #endif
  860. { "-rsa", BENCH_RSA },
  861. #ifdef WOLFSSL_KEY_GEN
  862. { "-rsa-sz", BENCH_RSA_SZ },
  863. #endif
  864. #endif
  865. #ifndef NO_DH
  866. { "-dh", BENCH_DH },
  867. #endif
  868. #ifdef WOLFSSL_HAVE_KYBER
  869. { "-kyber", BENCH_KYBER },
  870. #endif
  871. #ifdef HAVE_ECC
  872. { "-ecc-kg", BENCH_ECC_MAKEKEY },
  873. { "-ecc", BENCH_ECC },
  874. #ifdef HAVE_ECC_ENCRYPT
  875. { "-ecc-enc", BENCH_ECC_ENCRYPT },
  876. #endif
  877. { "-ecc-all", BENCH_ECC_ALL },
  878. #endif
  879. #ifdef WOLFSSL_SM2
  880. { "-sm2", BENCH_SM2 },
  881. #endif
  882. #ifdef HAVE_CURVE25519
  883. { "-curve25519-kg", BENCH_CURVE25519_KEYGEN },
  884. #ifdef HAVE_CURVE25519_SHARED_SECRET
  885. { "-x25519", BENCH_CURVE25519_KA },
  886. #endif
  887. #endif
  888. #ifdef HAVE_ED25519
  889. { "-ed25519-kg", BENCH_ED25519_KEYGEN },
  890. { "-ed25519", BENCH_ED25519_SIGN },
  891. #endif
  892. #ifdef HAVE_CURVE448
  893. { "-curve448-kg", BENCH_CURVE448_KEYGEN },
  894. #ifdef HAVE_CURVE448_SHARED_SECRET
  895. { "-x448", BENCH_CURVE448_KA },
  896. #endif
  897. #endif
  898. #ifdef HAVE_ED448
  899. { "-ed448-kg", BENCH_ED448_KEYGEN },
  900. { "-ed448", BENCH_ED448_SIGN },
  901. #endif
  902. #ifdef WOLFCRYPT_HAVE_ECCSI
  903. { "-eccsi-kg", BENCH_ECCSI_KEYGEN },
  904. { "-eccsi-pair", BENCH_ECCSI_PAIRGEN },
  905. { "-eccsi-val", BENCH_ECCSI_VALIDATE },
  906. { "-eccsi", BENCH_ECCSI },
  907. #endif
  908. #ifdef WOLFCRYPT_HAVE_SAKKE
  909. { "-sakke-kg", BENCH_SAKKE_KEYGEN },
  910. { "-sakke-rsk", BENCH_SAKKE_RSKGEN },
  911. { "-sakke-val", BENCH_SAKKE_VALIDATE },
  912. { "-sakke", BENCH_SAKKE },
  913. #endif
  914. { NULL, 0 }
  915. };
  916. /* All recognized other cryptographic algorithm choosing command line options.
  917. */
  918. static const bench_alg bench_other_opt[] = {
  919. { "-other", 0xffffffff },
  920. #ifndef WC_NO_RNG
  921. { "-rng", BENCH_RNG },
  922. #endif
  923. #ifdef HAVE_SCRYPT
  924. { "-scrypt", BENCH_SCRYPT },
  925. #endif
  926. { NULL, 0}
  927. };
  928. #endif /* MAIN_NO_ARGS */
  929. #endif /* !WOLFSSL_BENCHMARK_ALL && !NO_MAIN_DRIVER */
  930. #if defined(BENCH_PQ_STATEFUL_HBS)
  931. typedef struct bench_pq_hash_sig_alg {
  932. /* Command line option string. */
  933. const char* str;
  934. /* Bit values to set. */
  935. word32 val;
  936. } bench_pq_hash_sig_alg;
  937. static const bench_pq_hash_sig_alg bench_pq_hash_sig_opt[] = {
  938. { "-pq_hash_sig", 0xffffffff},
  939. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  940. { "-lms_hss", BENCH_LMS_HSS},
  941. #endif
  942. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  943. { "-xmss_xmssmt", BENCH_XMSS_XMSSMT},
  944. #ifdef WC_XMSS_SHA256
  945. { "-xmss_xmssmt_sha256", BENCH_XMSS_XMSSMT_SHA256},
  946. #endif
  947. #ifdef WC_XMSS_SHA512
  948. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 512 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 512
  949. { "-xmss_xmssmt_sha512", BENCH_XMSS_XMSSMT_SHA512},
  950. #endif
  951. #endif
  952. #ifdef WC_XMSS_SHAKE128
  953. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  954. { "-xmss_xmssmt_shake128", BENCH_XMSS_XMSSMT_SHAKE128},
  955. #endif
  956. #endif
  957. #ifdef WC_XMSS_SHAKE256
  958. { "-xmss_xmssmt_shake256", BENCH_XMSS_XMSSMT_SHAKE256},
  959. #endif
  960. #endif
  961. { NULL, 0}
  962. };
  963. #endif /* BENCH_PQ_STATEFUL_HBS */
  964. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  965. /* The post-quantum-specific mapping of command line option to bit values and
  966. * OQS name. */
  967. typedef struct bench_pq_alg {
  968. /* Command line option string. */
  969. const char* str;
  970. /* Bit values to set. */
  971. word32 val;
  972. const char* pqc_name;
  973. } bench_pq_alg;
  974. /* All recognized post-quantum asymmetric algorithm choosing command line
  975. * options. */
  976. static const bench_pq_alg bench_pq_asym_opt[] = {
  977. { "-pq", 0xffffffff, NULL},
  978. #ifdef HAVE_LIBOQS
  979. { "-falcon_level1", BENCH_FALCON_LEVEL1_SIGN,
  980. OQS_SIG_alg_falcon_512 },
  981. { "-falcon_level5", BENCH_FALCON_LEVEL5_SIGN,
  982. OQS_SIG_alg_falcon_1024 },
  983. { "-dilithium_level2", BENCH_DILITHIUM_LEVEL2_SIGN,
  984. OQS_SIG_alg_dilithium_2 },
  985. { "-dilithium_level3", BENCH_DILITHIUM_LEVEL3_SIGN,
  986. OQS_SIG_alg_dilithium_3 },
  987. { "-dilithium_level5", BENCH_DILITHIUM_LEVEL5_SIGN,
  988. OQS_SIG_alg_dilithium_5 },
  989. #endif /* HAVE_LIBOQS */
  990. { NULL, 0, NULL }
  991. };
  992. #if defined(HAVE_LIBOQS) && defined(HAVE_SPHINCS)
  993. /* All recognized post-quantum asymmetric algorithm choosing command line
  994. * options. (Part 2) */
  995. static const bench_pq_alg bench_pq_asym_opt2[] = {
  996. { "-pq", 0xffffffff, NULL},
  997. { "-sphincs_fast_level1", BENCH_SPHINCS_FAST_LEVEL1_SIGN,
  998. OQS_SIG_alg_sphincs_shake_128f_simple },
  999. { "-sphincs_fast_level3", BENCH_SPHINCS_FAST_LEVEL3_SIGN,
  1000. OQS_SIG_alg_sphincs_shake_192f_simple },
  1001. { "-sphincs_fast_level5", BENCH_SPHINCS_FAST_LEVEL5_SIGN,
  1002. OQS_SIG_alg_sphincs_shake_256f_simple },
  1003. { "-sphincs_small_level1", BENCH_SPHINCS_SMALL_LEVEL1_SIGN,
  1004. OQS_SIG_alg_sphincs_shake_128s_simple },
  1005. { "-sphincs_small_level3", BENCH_SPHINCS_SMALL_LEVEL3_SIGN,
  1006. OQS_SIG_alg_sphincs_shake_192s_simple },
  1007. { "-sphincs_small_level5", BENCH_SPHINCS_SMALL_LEVEL5_SIGN,
  1008. OQS_SIG_alg_sphincs_shake_256s_simple },
  1009. { NULL, 0, NULL }
  1010. };
  1011. #endif /* HAVE_LIBOQS && HAVE_SPHINCS */
  1012. #endif /* HAVE_PQC */
  1013. #ifdef HAVE_WNR
  1014. const char* wnrConfigFile = "wnr-example.conf";
  1015. #endif
  1016. #if defined(WOLFSSL_MDK_ARM)
  1017. extern XFILE wolfSSL_fopen(const char *fname, const char *mode);
  1018. #define fopen wolfSSL_fopen
  1019. #endif
  1020. static int lng_index = 0;
  1021. #ifndef NO_MAIN_DRIVER
  1022. #ifndef MAIN_NO_ARGS
  1023. static const char* bench_Usage_msg1[][25] = {
  1024. /* 0 English */
  1025. { "-? <num> Help, print this usage\n",
  1026. " 0: English, 1: Japanese\n",
  1027. "-csv Print terminal output in csv format\n",
  1028. "-base10 Display bytes as power of 10 (eg 1 kB = 1000 Bytes)\n",
  1029. "-no_aad No additional authentication data passed.\n",
  1030. "-aad_size <num> With <num> bytes of AAD.\n",
  1031. ("-all_aad With AAD length of 0, "
  1032. WC_STRINGIFY(AES_AUTH_ADD_SZ)
  1033. " and\n"
  1034. " (if set via -aad_size) <aad_size> bytes.\n"
  1035. ),
  1036. "-dgst_full Full digest operation performed.\n",
  1037. "-rsa_sign Measure RSA sign/verify instead of encrypt/decrypt.\n",
  1038. "<keySz> -rsa-sz\n Measure RSA <key size> performance.\n",
  1039. "-ffhdhe2048 Measure DH using FFDHE 2048-bit parameters.\n",
  1040. "-ffhdhe3072 Measure DH using FFDHE 3072-bit parameters.\n",
  1041. "-p256 Measure ECC using P-256 curve.\n",
  1042. "-p384 Measure ECC using P-384 curve.\n",
  1043. "-p521 Measure ECC using P-521 curve.\n",
  1044. "-ecc-all Bench all enabled ECC curves.\n",
  1045. "-<alg> Algorithm to benchmark. Available algorithms include:\n",
  1046. ("-lng <num> Display benchmark result by specified language.\n"
  1047. " 0: English, 1: Japanese\n"
  1048. ),
  1049. "<num> Size of block in bytes\n",
  1050. ("-blocks <num> Number of blocks. Can be used together with the "
  1051. "'Size of block'\n"
  1052. " option, but must be used after that one.\n"
  1053. ),
  1054. "-threads <num> Number of threads to run\n",
  1055. "-print Show benchmark stats summary\n",
  1056. "-hash_input <file> Input data to use for hash benchmarking\n",
  1057. "-cipher_input <file> Input data to use for cipher benchmarking\n",
  1058. "-min_runs <num> Specify minimum number of operation runs\n"
  1059. },
  1060. #ifndef NO_MULTIBYTE_PRINT
  1061. /* 1 Japanese */
  1062. { "-? <num> ヘルプ, 使い方を表示します。\n",
  1063. " 0: 英語、 1: 日本語\n",
  1064. "-csv csv 形式で端末に出力します。\n",
  1065. "-base10 バイトを10のべき乗で表示します。(例 1 kB = 1000 Bytes)\n",
  1066. "-no_aad 追加の認証データを使用しません.\n",
  1067. "-aad_size <num> TBD.\n",
  1068. "-all_aad TBD.\n",
  1069. "-dgst_full フルの digest 暗号操作を実施します。\n",
  1070. "-rsa_sign 暗号/復号化の代わりに RSA の署名/検証を測定します。\n",
  1071. "<keySz> -rsa-sz\n RSA <key size> の性能を測定します。\n",
  1072. "-ffhdhe2048 Measure DH using FFDHE 2048-bit parameters.\n",
  1073. "-ffhdhe3072 Measure DH using FFDHE 3072-bit parameters.\n",
  1074. "-p256 Measure ECC using P-256 curve.\n",
  1075. "-p384 Measure ECC using P-384 curve.\n",
  1076. "-p521 Measure ECC using P-521 curve.\n",
  1077. "-ecc-all Bench all enabled ECC curves.\n",
  1078. ("-<alg> アルゴリズムのベンチマークを実施します。\n"
  1079. " 利用可能なアルゴリズムは下記を含みます:\n"
  1080. ),
  1081. ("-lng <num> 指定された言語でベンチマーク結果を表示します。\n"
  1082. " 0: 英語、 1: 日本語\n"
  1083. ),
  1084. "<num> ブロックサイズをバイト単位で指定します。\n",
  1085. "-blocks <num> TBD.\n",
  1086. "-threads <num> 実行するスレッド数\n",
  1087. "-print ベンチマーク統計の要約を表示する\n",
  1088. /* TODO: translate below */
  1089. "-hash_input <file> Input data to use for hash benchmarking\n",
  1090. "-cipher_input <file> Input data to use for cipher benchmarking\n",
  1091. "-min_runs <num> Specify minimum number of operation runs\n"
  1092. },
  1093. #endif
  1094. };
  1095. #endif /* MAIN_NO_ARGS */
  1096. #endif
  1097. static const char* bench_result_words1[][4] = {
  1098. { "took",
  1099. #ifdef BENCH_MICROSECOND
  1100. "microseconds"
  1101. #else
  1102. "seconds"
  1103. #endif
  1104. , "Cycles per byte", NULL }, /* 0 English */
  1105. #ifndef NO_MULTIBYTE_PRINT
  1106. { "を" , "秒で処理", "1バイトあたりのサイクル数", NULL }, /* 1 Japanese */
  1107. #endif
  1108. };
  1109. #if !defined(NO_RSA) || \
  1110. defined(HAVE_ECC) || !defined(NO_DH) || defined(HAVE_ECC_ENCRYPT) || \
  1111. defined(HAVE_CURVE25519) || defined(HAVE_CURVE25519_SHARED_SECRET) || \
  1112. defined(HAVE_ED25519) || defined(HAVE_CURVE448) || \
  1113. defined(HAVE_CURVE448_SHARED_SECRET) || defined(HAVE_ED448) || \
  1114. defined(WOLFSSL_HAVE_KYBER)
  1115. static const char* bench_desc_words[][15] = {
  1116. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 */
  1117. {"public", "private", "key gen", "agree" , "sign", "verify", "encrypt", "decrypt", "rsk gen", "encap", "derive", "valid", "pair gen", "decap", NULL}, /* 0 English */
  1118. #ifndef NO_MULTIBYTE_PRINT
  1119. {"公開鍵", "秘密鍵" ,"鍵生成" , "鍵共有" , "署名", "検証" , "暗号化" , "復号化" , "rsk gen", "encap", "derive", "valid", "pair gen", "decap", NULL}, /* 1 Japanese */
  1120. #endif
  1121. };
  1122. #endif
  1123. #ifdef MULTI_VALUE_STATISTICS
  1124. static const char* bench_result_words3[][5] = {
  1125. /* 0 English */
  1126. { "max duration", "min duration" , "mean duration", "sd", NULL },
  1127. /* TODO: Add japenese version */
  1128. { "max duration", "min duration" , "mean duration", "sd", NULL }
  1129. };
  1130. #endif
  1131. #if defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM) && !defined(WOLFSSL_SGX)
  1132. #define HAVE_GET_CYCLES
  1133. static WC_INLINE word64 get_intel_cycles(void);
  1134. static THREAD_LS_T word64 total_cycles;
  1135. #define INIT_CYCLE_COUNTER
  1136. #define BEGIN_INTEL_CYCLES total_cycles = get_intel_cycles();
  1137. #define END_INTEL_CYCLES total_cycles = get_intel_cycles() - total_cycles;
  1138. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  1139. #define SHOW_INTEL_CYCLES(b, n, s) \
  1140. (void)XSNPRINTF((b) + XSTRLEN(b), (n) - XSTRLEN(b), \
  1141. " %s = " FLT_FMT_PREC2 STATS_CLAUSE_SEPARATOR, \
  1142. bench_result_words1[lng_index][2], \
  1143. FLT_FMT_PREC2_ARGS(6, 2, count == 0 ? 0 : \
  1144. (double)total_cycles / ((word64)count*(s))))
  1145. #define SHOW_INTEL_CYCLES_CSV(b, n, s) \
  1146. (void)XSNPRINTF((b) + XSTRLEN(b), (n) - XSTRLEN(b), FLT_FMT_PREC "," \
  1147. STATS_CLAUSE_SEPARATOR, FLT_FMT_PREC_ARGS(6, count == 0 ? 0 : \
  1148. (double)total_cycles / ((word64)count*(s))))
  1149. #elif defined(LINUX_CYCLE_COUNT)
  1150. #include <linux/perf_event.h>
  1151. #include <sys/syscall.h>
  1152. #include <unistd.h>
  1153. static THREAD_LS_T word64 begin_cycles;
  1154. static THREAD_LS_T word64 total_cycles;
  1155. static THREAD_LS_T int cycles = -1;
  1156. static THREAD_LS_T struct perf_event_attr atr;
  1157. #define INIT_CYCLE_COUNTER do { \
  1158. atr.type = PERF_TYPE_HARDWARE; \
  1159. atr.config = PERF_COUNT_HW_CPU_CYCLES; \
  1160. cycles = (int)syscall(__NR_perf_event_open, &atr, 0, -1, -1, 0); \
  1161. } while (0);
  1162. #define BEGIN_INTEL_CYCLES read(cycles, &begin_cycles, sizeof(begin_cycles));
  1163. #define END_INTEL_CYCLES do { \
  1164. read(cycles, &total_cycles, sizeof(total_cycles)); \
  1165. total_cycles = total_cycles - begin_cycles; \
  1166. } while (0);
  1167. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  1168. #define SHOW_INTEL_CYCLES(b, n, s) \
  1169. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), \
  1170. " %s = " FLT_FMT_PREC2 STATS_CLAUSE_SEPARATOR, \
  1171. bench_result_words1[lng_index][2], \
  1172. FLT_FMT_PREC2_ARGS(6, 2, (double)total_cycles / \
  1173. (count*s)))
  1174. #define SHOW_INTEL_CYCLES_CSV(b, n, s) \
  1175. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), FLT_FMT_PREC "," \
  1176. STATS_CLAUSE_SEPARATOR, FLT_FMT_PREC_ARGS(6, (double)total_cycles \
  1177. / (count*s)))
  1178. #elif defined(SYNERGY_CYCLE_COUNT)
  1179. #include "hal_data.h"
  1180. static THREAD_LS_T word64 begin_cycles;
  1181. static THREAD_LS_T word64 total_cycles;
  1182. #define INIT_CYCLE_COUNTER
  1183. #define BEGIN_INTEL_CYCLES begin_cycles = DWT->CYCCNT = 0;
  1184. #define END_INTEL_CYCLES total_cycles = DWT->CYCCNT - begin_cycles;
  1185. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  1186. #define SHOW_INTEL_CYCLES(b, n, s) \
  1187. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), \
  1188. " %s = " FLT_FMT_PREC2 STATS_CLAUSE_SEPARATOR, \
  1189. bench_result_words1[lng_index][2], \
  1190. FLT_FMT_PREC2_ARGS(6, 2, (double)total_cycles / (count*s)))
  1191. #define SHOW_INTEL_CYCLES_CSV(b, n, s) \
  1192. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), FLT_FMT_PREC ",\n", \
  1193. FLT_FMT_PREC_ARGS(6, (double)total_cycles / (count*s)))
  1194. #elif defined(WOLFSSL_ESPIDF)
  1195. /* TAG for ESP_LOGx() */
  1196. static const char* TAG = "wolfssl_benchmark";
  1197. static THREAD_LS_T word64 begin_cycles;
  1198. static THREAD_LS_T word64 begin_cycles_ticks;
  1199. static THREAD_LS_T word64 end_cycles;
  1200. static THREAD_LS_T word64 total_cycles;
  1201. /* the return value, as a global var */
  1202. static THREAD_LS_T word64 _esp_get_cycle_count_ex = 0;
  1203. /* the last value seen, adjusted for an overflow, as a global var */
  1204. static THREAD_LS_T word64 _esp_cpu_count_last = 0;
  1205. static THREAD_LS_T TickType_t last_tickCount = 0; /* last FreeRTOS value */
  1206. /* esp_get_cpu_benchmark_cycles(void):
  1207. *
  1208. * Architecture-independant CPU clock counter.
  1209. * WARNING: the hal UINT xthal_get_ccount() quietly rolls over. */
  1210. static WC_INLINE word64 esp_get_cpu_benchmark_cycles(void);
  1211. /* Some vars for debugging, compare ticks to cycles */
  1212. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1213. static THREAD_LS_T word64 _esp_cpu_timer_last = 0;
  1214. static THREAD_LS_T word64 _esp_cpu_timer_diff = 0;
  1215. static THREAD_LS_T word64 _xthal_get_ccount_exAlt = 0;
  1216. static THREAD_LS_T word64 _xthal_get_ccount_exDiff = 0;
  1217. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  1218. /* The ESP32 (both Xtensa and RISC-V have raw CPU counters). */
  1219. #if ESP_IDF_VERSION_MAJOR >= 5
  1220. /* esp_cpu_set_cycle_count() introduced in ESP-IDF v5 */
  1221. #define HAVE_GET_CYCLES
  1222. #define INIT_CYCLE_COUNTER do { \
  1223. ESP_LOGV(TAG, "INIT_CYCLE_COUNTER"); \
  1224. esp_cpu_set_cycle_count(0); \
  1225. } while (0);
  1226. #else
  1227. #define HAVE_GET_CYCLES
  1228. #define INIT_CYCLE_COUNTER do { \
  1229. ESP_LOGV(TAG, "INIT_CYCLE_COUNTER"); \
  1230. } while (0);
  1231. #endif
  1232. #define BEGIN_ESP_CYCLES do { \
  1233. ESP_LOGV(TAG, "BEGIN_ESP_CYCLES"); \
  1234. begin_cycles = esp_get_cpu_benchmark_cycles(); \
  1235. begin_cycles_ticks = xTaskGetTickCount(); \
  1236. } while (0);
  1237. /* since it rolls over, we have something that will tolerate one */
  1238. #define END_ESP_CYCLES \
  1239. end_cycles = esp_get_cpu_benchmark_cycles(); \
  1240. ESP_LOGV(TAG,"END_ESP_CYCLES %llu - %llu", \
  1241. end_cycles, \
  1242. begin_cycles \
  1243. ); \
  1244. total_cycles = (end_cycles - begin_cycles);
  1245. #define SHOW_ESP_CYCLES(b, n, s) \
  1246. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), \
  1247. " %s = " FLT_FMT_PREC2 "\n", \
  1248. bench_result_words1[lng_index][2], \
  1249. FLT_FMT_PREC2_ARGS(6, 2, (double)total_cycles / (count*s)) \
  1250. )
  1251. #define SHOW_ESP_CYCLES_CSV(b, n, s) \
  1252. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), FLT_FMT_PREC ",\n", \
  1253. FLT_FMT_PREC_ARGS(6, (double)total_cycles / (count*s)))
  1254. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1255. /* 64 bit, unisgned, absolute difference
  1256. * used in CPU cycle counter debug calcs. */
  1257. static uint64_t esp_cycle_abs_diff(uint64_t x, uint64_t y)
  1258. {
  1259. uint64_t ret;
  1260. ret = (x > y) ? (x - y) : (y - x);
  1261. return ret;
  1262. }
  1263. #endif
  1264. /* esp_get_cycle_count_ex() is a single-overflow-tolerant extension to
  1265. ** the Espressif `unsigned xthal_get_ccount()` (Xtensa) or
  1266. ** `esp_cpu_get_cycle_count` (RISC-V) which are known to overflow
  1267. ** at least once during full benchmark tests.
  1268. **
  1269. ** To test timing overflow, add a delay longer than max cycles:
  1270. ** vTaskDelay( (const TickType_t)(configTICK_RATE_HZ * 17 * 5) );
  1271. */
  1272. uint64_t esp_get_cycle_count_ex()
  1273. {
  1274. /* reminder: unsigned long long max = 18,446,744,073,709,551,615 */
  1275. /* unsigned int max = 4,294,967,295 */
  1276. uint64_t thisVal = 0; /* CPU counter, "this current value" as read. */
  1277. uint64_t thisIncrement = 0; /* The adjusted increment amount. */
  1278. uint64_t expected_diff = 0; /* FreeRTOS estimated expected CPU diff.*/
  1279. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1280. uint32_t tickCount = 0; /* Current rtos tick counter. */
  1281. uint32_t tickDiff = 0; /* Tick difference from last check. */
  1282. uint32_t tickBeginDiff = 0; /* Tick difference from beginning. */
  1283. #endif
  1284. #if defined(CONFIG_IDF_TARGET_ESP32C2) || \
  1285. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  1286. defined(CONFIG_IDF_TARGET_ESP32C6)
  1287. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1288. uint64_t thisTimerVal = 0; /* Timer Value as alternate to compare */
  1289. uint64_t diffDiff = 0; /* Difference between CPU & Timer differences:
  1290. * (current - last) */
  1291. ESP_ERROR_CHECK(gptimer_get_raw_count(esp_gptimer, &thisTimerVal));
  1292. thisTimerVal = thisTimerVal * RESOLUTION_SCALE;
  1293. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  1294. thisVal = esp_cpu_get_cycle_count();
  1295. #elif defined(CONFIG_IDF_TARGET_ESP32H2)
  1296. thisVal = esp_cpu_get_cycle_count();
  1297. #else
  1298. /* TODO: Why doesn't esp_cpu_get_cycle_count work for Xtensa?
  1299. * Calling current_time(1) to reset time causes thisVal overflow,
  1300. * on Xtensa, but not on RISC-V architecture. See also, below */
  1301. #ifndef __XTENSA__
  1302. thisVal = esp_cpu_get_cycle_count();
  1303. #else
  1304. thisVal = xthal_get_ccount(); /* or esp_cpu_get_cycle_count(); */
  1305. #endif
  1306. #endif
  1307. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1308. {
  1309. tickCount = xTaskGetTickCount(); /* Our local FreeRTOS tick count */
  1310. tickDiff = tickCount - last_tickCount; /* ticks since bench start */
  1311. expected_diff = CPU_TICK_CYCLES * tickDiff; /* CPU expected count */
  1312. ESP_LOGV(TAG, "CPU_TICK_CYCLES = %d", (int)CPU_TICK_CYCLES);
  1313. ESP_LOGV(TAG, "tickCount = %lu", tickCount);
  1314. ESP_LOGV(TAG, "last_tickCount = %lu", last_tickCount);
  1315. ESP_LOGV(TAG, "tickDiff = %lu", tickDiff);
  1316. ESP_LOGV(TAG, "expected_diff1 = %llu", expected_diff);
  1317. }
  1318. #endif
  1319. /* If either thisVal is smaller than last (overflow), and/or the
  1320. * expected value calculated from FreeRTOS tick difference that would
  1321. * have never fit into an unsigned 32 bit integer anyhow... then we
  1322. * need to adjust thisVal to save. */
  1323. if ( (thisVal < _esp_cpu_count_last) || (expected_diff > UINT_MAX) )
  1324. {
  1325. /* Warning: we assume the return type of esp_cpu_get_cycle_count()
  1326. ** will always be unsigned int (or uint32_t) to add UINT_MAX.
  1327. **
  1328. ** NOTE for long duration between calls with multiple overflows:
  1329. **
  1330. ** WILL NOT BE DETECTED - the return value will be INCORRECT.
  1331. **
  1332. ** At this time no single test overflows. This is currently only a
  1333. ** concern for cumulative counts over multiple tests. As long
  1334. ** as well call xthal_get_ccount_ex() with no more than one
  1335. ** overflow CPU tick count, all will be well.
  1336. */
  1337. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1338. ESP_LOGW(TAG,
  1339. "Alert: Detected xthal_get_ccount overflow at %llu, "
  1340. "adding UINT_MAX.",
  1341. thisVal);
  1342. #endif
  1343. /* double check expected diff calc */
  1344. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1345. expected_diff = (CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ * MILLION_VALUE)
  1346. * tickDiff / configTICK_RATE_HZ;
  1347. ESP_LOGI(TAG, "expected_diff2 = %llu", expected_diff);
  1348. #endif
  1349. if (expected_diff > UINT_MAX) {
  1350. /* The number of cycles expected from FreeRTOS ticks is
  1351. * greater than the maximum size of an unsigned 32-bit
  1352. * integer, meaning multiple overflows occurred. */
  1353. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1354. ESP_LOGW(TAG, "expected_diff > UINT_MAX (%u)", UINT_MAX);
  1355. #endif
  1356. thisVal += expected_diff; /* FreeRTOS calc to our 64 bit val */
  1357. }
  1358. else {
  1359. thisVal += (word64)UINT_MAX; /* add 32 bit max to our 64 bit */
  1360. }
  1361. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1362. {
  1363. tickBeginDiff = tickCount - begin_cycles_ticks;
  1364. ESP_LOGI(TAG, "begin_cycles_ticks = %llu", begin_cycles_ticks);
  1365. ESP_LOGI(TAG, "tickDiff = %lu", tickDiff);
  1366. ESP_LOGI(TAG, "expected_diff = %llu", expected_diff);
  1367. ESP_LOGI(TAG, "tickBeginDiff = %lu", tickBeginDiff);
  1368. ESP_LOGW(TAG, WOLFSSL_ESPIDF_BLANKLINE_MESSAGE);
  1369. }
  1370. #endif
  1371. }
  1372. else {
  1373. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1374. ESP_LOGI(TAG, "thisVal, read CPU = %llu", thisVal);
  1375. #endif
  1376. } /* if thisVal adjustment check */
  1377. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1378. if (thisTimerVal < _esp_cpu_timer_last)
  1379. {
  1380. ESP_LOGW(TAG, "Alert: Detected xthal_get_ccountAlt overflow, "
  1381. "adding %ull", UINT_MAX);
  1382. thisTimerVal += (word64)UINT_MAX;
  1383. }
  1384. /* Check an alternate counter using a timer */
  1385. _esp_cpu_timer_diff = esp_cycle_abs_diff(_esp_cpu_count_last, _esp_cpu_timer_last);
  1386. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  1387. /* Adjust our actual returned value that takes into account overflow,
  1388. * increment 64 bit extended total by this 32 bit differential: */
  1389. thisIncrement = (thisVal - _esp_cpu_count_last);
  1390. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1391. ESP_LOGI(TAG, "thisIncrement = %llu", thisIncrement);
  1392. #endif
  1393. /* Add our adjustment, taking into account overflows (see above) */
  1394. _esp_get_cycle_count_ex += thisIncrement;
  1395. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1396. _xthal_get_ccount_exDiff = esp_cycle_abs_diff(_esp_get_cycle_count_ex, _xthal_get_ccount_exAlt);
  1397. _xthal_get_ccount_exAlt += (thisTimerVal - _esp_cpu_timer_last);
  1398. diffDiff = esp_cycle_abs_diff(_xthal_get_ccount_exDiff, _esp_cpu_timer_diff);
  1399. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  1400. /* all of this took some time, so reset the "last seen" value
  1401. * for the next measurement. */
  1402. #if defined(CONFIG_IDF_TARGET_ESP32C2) || \
  1403. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  1404. defined(CONFIG_IDF_TARGET_ESP32C6)
  1405. {
  1406. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1407. ESP_ERROR_CHECK(gptimer_get_raw_count(esp_gptimer,
  1408. &_esp_cpu_timer_last));
  1409. ESP_LOGI(TAG, "thisVal = %llu", thisVal);
  1410. ESP_LOGI(TAG, "thisTimerVal = %llu", thisTimerVal);
  1411. ESP_LOGI(TAG, "diffDiff = %llu", diffDiff);
  1412. ESP_LOGI(TAG, "_xthal_get_ccount_exDiff = %llu", _xthal_get_ccount_exDiff);
  1413. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  1414. _esp_cpu_count_last = esp_cpu_get_cycle_count();
  1415. ESP_LOGV(TAG, "_xthal_get_ccount_last = %llu", _esp_cpu_count_last);
  1416. }
  1417. #elif defined(CONFIG_IDF_TARGET_ESP32H2)
  1418. _esp_cpu_count_last = esp_cpu_get_cycle_count();
  1419. #else
  1420. /* TODO: Why doesn't esp_cpu_get_cycle_count work for Xtensa
  1421. * when resetting CPU cycle counter? FreeRTOS tick collision?
  1422. * thisVal = esp_cpu_get_cycle_count(); See also, above
  1423. * or thisVal = xthal_get_ccount(); */
  1424. #if ESP_IDF_VERSION_MAJOR < 5
  1425. _esp_cpu_count_last = xthal_get_ccount();
  1426. #else
  1427. _esp_cpu_count_last = esp_cpu_get_cycle_count();
  1428. #endif
  1429. #endif
  1430. /* Return the 64 bit extended total from 32 bit counter. */
  1431. return _esp_get_cycle_count_ex;
  1432. }
  1433. /* implement other architecture cycle counters here */
  1434. #else
  1435. /* if we don't know the platform, it is unlikely we can count CPU cycles */
  1436. #undef HAVE_GET_CYCLES
  1437. #define INIT_CYCLE_COUNTER
  1438. #define BEGIN_INTEL_CYCLES
  1439. #define END_INTEL_CYCLES
  1440. #ifdef MULTI_VALUE_STATISTICS
  1441. #define SHOW_INTEL_CYCLES(b, n, s) WC_DO_NOTHING
  1442. #define SHOW_INTEL_CYCLES_CSV(b, n, s) WC_DO_NOTHING
  1443. #else
  1444. #define SHOW_INTEL_CYCLES(b, n, s) b[XSTRLEN(b)] = '\n'
  1445. #define SHOW_INTEL_CYCLES_CSV(b, n, s) b[XSTRLEN(b)] = '\n'
  1446. #endif
  1447. #endif
  1448. /* determine benchmark buffer to use (if NO_FILESYSTEM) */
  1449. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  1450. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  1451. #define USE_CERT_BUFFERS_2048 /* default to 2048 */
  1452. #endif
  1453. #if defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048) || \
  1454. defined(USE_CERT_BUFFERS_3072) || defined(USE_CERT_BUFFERS_4096) || \
  1455. !defined(NO_DH)
  1456. /* include test cert and key buffers for use with NO_FILESYSTEM */
  1457. #include <wolfssl/certs_test.h>
  1458. #endif
  1459. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  1460. #include <wolfssl/wolfcrypt/blake2.h>
  1461. #endif
  1462. #ifdef _MSC_VER
  1463. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  1464. #pragma warning(disable: 4996)
  1465. #endif
  1466. #ifdef WOLFSSL_CURRTIME_REMAP
  1467. #define current_time WOLFSSL_CURRTIME_REMAP
  1468. #else
  1469. double current_time(int reset);
  1470. #endif
  1471. #ifdef LINUX_RUSAGE_UTIME
  1472. static void check_for_excessive_stime(const char *desc,
  1473. const char *desc_extra);
  1474. #endif
  1475. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND) && \
  1476. !defined(HAVE_STACK_SIZE)
  1477. #ifdef __cplusplus
  1478. extern "C" {
  1479. #endif
  1480. WOLFSSL_API int wolfSSL_Debugging_ON(void);
  1481. WOLFSSL_API void wolfSSL_Debugging_OFF(void);
  1482. #ifdef __cplusplus
  1483. } /* extern "C" */
  1484. #endif
  1485. #endif
  1486. #if !defined(WC_NO_RNG) && \
  1487. ((!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) \
  1488. || !defined(NO_DH) || defined(WOLFSSL_KEY_GEN) || defined(HAVE_ECC) \
  1489. || defined(HAVE_CURVE25519) || defined(HAVE_ED25519) \
  1490. || defined(HAVE_CURVE448) || defined(HAVE_ED448) \
  1491. || defined(WOLFSSL_HAVE_KYBER))
  1492. #define HAVE_LOCAL_RNG
  1493. static THREAD_LS_T WC_RNG gRng;
  1494. #define GLOBAL_RNG &gRng
  1495. #else
  1496. #define GLOBAL_RNG NULL
  1497. #endif
  1498. #if defined(HAVE_ED25519) || defined(HAVE_CURVE25519) || \
  1499. defined(HAVE_CURVE448) || defined(HAVE_ED448) || \
  1500. defined(HAVE_ECC) || !defined(NO_DH) || \
  1501. !defined(NO_RSA) || defined(HAVE_SCRYPT) || \
  1502. defined(WOLFSSL_HAVE_KYBER)
  1503. #define BENCH_ASYM
  1504. #endif
  1505. #if defined(BENCH_ASYM)
  1506. #if defined(HAVE_ECC) || !defined(NO_RSA) || !defined(NO_DH) || \
  1507. defined(HAVE_CURVE25519) || defined(HAVE_ED25519) || \
  1508. defined(HAVE_CURVE448) || defined(HAVE_ED448) || \
  1509. defined(WOLFSSL_HAVE_KYBER)
  1510. static const char* bench_result_words2[][5] = {
  1511. #ifdef BENCH_MICROSECOND
  1512. { "ops took", "μsec" , "avg" , "ops/μsec", NULL }, /* 0 English
  1513. for μsec */
  1514. #else
  1515. { "ops took", "sec" , "avg" , "ops/sec", NULL }, /* 0 English */
  1516. #endif
  1517. #ifndef NO_MULTIBYTE_PRINT
  1518. { "回処理を", "秒で実施", "平均", "処理/秒", NULL }, /* 1 Japanese */
  1519. #endif
  1520. };
  1521. #endif
  1522. #endif
  1523. #ifdef WOLFSSL_CAAM
  1524. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  1525. #ifdef WOLFSSL_SECO_CAAM
  1526. #define SECO_MAX_UPDATES 10000
  1527. #define SECO_BENCHMARK_NONCE 0x7777
  1528. #define SECO_KEY_STORE_ID 1
  1529. #endif
  1530. static THREAD_LS_T int devId = WOLFSSL_CAAM_DEVID;
  1531. #else
  1532. #ifdef WC_USE_DEVID
  1533. static THREAD_LS_T int devId = WC_USE_DEVID;
  1534. #else
  1535. static THREAD_LS_T int devId = INVALID_DEVID;
  1536. #endif
  1537. #endif
  1538. /* Asynchronous helper macros */
  1539. #ifdef WC_ENABLE_BENCH_THREADING
  1540. typedef struct ThreadData {
  1541. pthread_t thread_id;
  1542. } ThreadData;
  1543. static ThreadData* g_threadData;
  1544. static volatile int g_threadCount;
  1545. #endif
  1546. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_CAAM) || defined(WC_USE_DEVID)
  1547. #ifndef NO_HW_BENCH
  1548. #define BENCH_DEVID
  1549. #endif
  1550. #ifndef HAVE_RENESAS_SYNC
  1551. #define BENCH_DEVID_GET_NAME(useDeviceID) (useDeviceID) ? "HW" : "SW"
  1552. #else
  1553. #define BENCH_DEVID_GET_NAME(useDeviceID) ""
  1554. #endif
  1555. #else
  1556. #define BENCH_DEVID_GET_NAME(useDeviceID) ""
  1557. #endif
  1558. #ifdef WOLFSSL_ASYNC_CRYPT
  1559. static WOLF_EVENT_QUEUE eventQueue;
  1560. #define BENCH_ASYNC_GET_DEV(obj) (&(obj)->asyncDev)
  1561. #define BENCH_MAX_PENDING (WOLF_ASYNC_MAX_PENDING)
  1562. static int bench_async_check(int* ret, WC_ASYNC_DEV* asyncDev,
  1563. int callAgain, int* times, int limit, int* pending)
  1564. {
  1565. int allowNext = 0;
  1566. /* this state can be set from a different thread */
  1567. WOLF_EVENT_STATE state = asyncDev->event.state;
  1568. /* if algo doesn't require calling again then use this flow */
  1569. if (state == WOLF_EVENT_STATE_DONE) {
  1570. if (callAgain) {
  1571. /* needs called again, so allow it and handle completion in
  1572. * bench_async_handle */
  1573. allowNext = 1;
  1574. }
  1575. else {
  1576. *ret = asyncDev->event.ret;
  1577. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  1578. (*times)++;
  1579. if (*pending > 0) /* to support case where async blocks */
  1580. (*pending)--;
  1581. if ((*times + *pending) < limit)
  1582. allowNext = 1;
  1583. }
  1584. }
  1585. /* if slot is available and we haven't reached limit, start another */
  1586. else if (state == WOLF_EVENT_STATE_READY && (*times + *pending) < limit) {
  1587. allowNext = 1;
  1588. }
  1589. return allowNext;
  1590. }
  1591. static int bench_async_handle(int* ret, WC_ASYNC_DEV* asyncDev,
  1592. int callAgain, int* times, int* pending)
  1593. {
  1594. WOLF_EVENT_STATE state = asyncDev->event.state;
  1595. if (*ret == WC_PENDING_E) {
  1596. if (state == WOLF_EVENT_STATE_DONE) {
  1597. *ret = asyncDev->event.ret;
  1598. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  1599. (*times)++;
  1600. (*pending)--;
  1601. }
  1602. else {
  1603. (*pending)++;
  1604. *ret = wc_AsyncHandle(asyncDev, &eventQueue,
  1605. callAgain ? WC_ASYNC_FLAG_CALL_AGAIN : WC_ASYNC_FLAG_NONE);
  1606. }
  1607. }
  1608. else if (*ret >= 0) {
  1609. *ret = asyncDev->event.ret;
  1610. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  1611. (*times)++;
  1612. if (*pending > 0) /* to support case where async blocks */
  1613. (*pending)--;
  1614. }
  1615. return (*ret >= 0) ? 1 : 0;
  1616. }
  1617. static WC_INLINE int bench_async_poll(int* pending)
  1618. {
  1619. int ret, asyncDone = 0;
  1620. ret = wolfAsync_EventQueuePoll(&eventQueue, NULL, NULL, 0,
  1621. WOLF_POLL_FLAG_CHECK_HW, &asyncDone);
  1622. if (ret != 0) {
  1623. printf("%sAsync poll failed %d\n", err_prefix, ret);
  1624. return ret;
  1625. }
  1626. if (asyncDone == 0) {
  1627. #ifndef WC_NO_ASYNC_THREADING
  1628. /* give time to other threads */
  1629. wc_AsyncThreadYield();
  1630. #endif
  1631. }
  1632. (void)pending;
  1633. return asyncDone;
  1634. }
  1635. #else
  1636. #define BENCH_MAX_PENDING 1
  1637. #define BENCH_ASYNC_GET_DEV(obj) NULL
  1638. static WC_INLINE int bench_async_check(int* ret, void* asyncDev,
  1639. int callAgain, int* times, int limit, int* pending)
  1640. {
  1641. (void)ret;
  1642. (void)asyncDev;
  1643. (void)callAgain;
  1644. (void)times;
  1645. (void)limit;
  1646. (void)pending;
  1647. return 1;
  1648. }
  1649. static WC_INLINE int bench_async_handle(int* ret, void* asyncDev,
  1650. int callAgain, int* times, int* pending)
  1651. {
  1652. (void)asyncDev;
  1653. (void)callAgain;
  1654. (void)pending;
  1655. if (*ret >= 0) {
  1656. /* operation completed */
  1657. (*times)++;
  1658. return 1;
  1659. }
  1660. return 0;
  1661. }
  1662. #define bench_async_poll(p) WC_DO_NOTHING
  1663. #endif /* WOLFSSL_ASYNC_CRYPT */
  1664. /* maximum runtime for each benchmark */
  1665. #ifndef BENCH_MIN_RUNTIME_SEC
  1666. #define BENCH_MIN_RUNTIME_SEC 1.0F
  1667. #endif
  1668. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1669. #define AES_AUTH_TAG_SZ 16
  1670. #define BENCH_CIPHER_ADD AES_AUTH_TAG_SZ
  1671. static word32 aesAuthAddSz = AES_AUTH_ADD_SZ;
  1672. #if !defined(AES_AAD_OPTIONS_DEFAULT)
  1673. #if !defined(NO_MAIN_DRIVER)
  1674. #define AES_AAD_OPTIONS_DEFAULT 0x1U
  1675. #else
  1676. #define AES_AAD_OPTIONS_DEFAULT 0x3U
  1677. #endif
  1678. #endif
  1679. #define AES_AAD_STRING(s) \
  1680. (aesAuthAddSz == 0 ? (s "-no_AAD") : \
  1681. (aesAuthAddSz == AES_AUTH_ADD_SZ ? (s) : (s "-custom")))
  1682. enum en_aad_options {
  1683. AAD_SIZE_DEFAULT = 0x1U,
  1684. AAD_SIZE_ZERO = 0x2U,
  1685. AAD_SIZE_CUSTOM = 0x4U,
  1686. };
  1687. static word32 aes_aad_options = AES_AAD_OPTIONS_DEFAULT;
  1688. static word32 aes_aad_size = 0;
  1689. static void bench_aes_aad_options_wrap(void (*fn)(int), int i)
  1690. {
  1691. word32 aesAuthAddSz_orig = aesAuthAddSz;
  1692. word32 options = aes_aad_options;
  1693. while(options) {
  1694. if (options & AAD_SIZE_DEFAULT) {
  1695. aesAuthAddSz = AES_AUTH_ADD_SZ;
  1696. options &= ~(word32)AAD_SIZE_DEFAULT;
  1697. }
  1698. else if (options & AAD_SIZE_ZERO) {
  1699. aesAuthAddSz = 0;
  1700. options &= ~(word32)AAD_SIZE_ZERO;
  1701. }
  1702. else if (options & AAD_SIZE_CUSTOM) {
  1703. aesAuthAddSz = aes_aad_size;
  1704. options &= ~(word32)AAD_SIZE_CUSTOM;
  1705. }
  1706. fn(i);
  1707. aesAuthAddSz = aesAuthAddSz_orig;
  1708. }
  1709. }
  1710. #endif
  1711. #ifndef BENCH_CIPHER_ADD
  1712. #define BENCH_CIPHER_ADD 0
  1713. #endif
  1714. /* use kB instead of mB for embedded benchmarking */
  1715. #ifdef BENCH_EMBEDDED
  1716. #ifndef BENCH_NTIMES
  1717. #define BENCH_NTIMES 2
  1718. #endif
  1719. #ifndef BENCH_AGREETIMES
  1720. #define BENCH_AGREETIMES 2
  1721. #endif
  1722. enum BenchmarkBounds {
  1723. scryptCnt = 1,
  1724. ntimes = BENCH_NTIMES,
  1725. genTimes = BENCH_MAX_PENDING,
  1726. agreeTimes = BENCH_AGREETIMES
  1727. };
  1728. /* how many kB to test (en/de)cryption */
  1729. #define NUM_BLOCKS 25
  1730. #define BENCH_SIZE (1024uL)
  1731. #else
  1732. #ifndef BENCH_NTIMES
  1733. #define BENCH_NTIMES 100
  1734. #endif
  1735. #ifndef BENCH_AGREETIMES
  1736. #define BENCH_AGREETIMES 100
  1737. #endif
  1738. enum BenchmarkBounds {
  1739. scryptCnt = 10,
  1740. ntimes = BENCH_NTIMES,
  1741. genTimes = BENCH_MAX_PENDING, /* must be at least BENCH_MAX_PENDING */
  1742. agreeTimes = BENCH_AGREETIMES
  1743. };
  1744. /* how many megs to test (en/de)cryption */
  1745. #define NUM_BLOCKS 5
  1746. #define BENCH_SIZE (1024*1024uL)
  1747. #endif
  1748. static int numBlocks = NUM_BLOCKS;
  1749. static word32 bench_size = BENCH_SIZE;
  1750. static int base2 = 1;
  1751. static int digest_stream = 1;
  1752. #ifdef MULTI_VALUE_STATISTICS
  1753. static int minimum_runs = 0;
  1754. #endif
  1755. #ifndef NO_RSA
  1756. /* Don't measure RSA sign/verify by default */
  1757. static int rsa_sign_verify = 0;
  1758. #endif
  1759. #ifndef NO_DH
  1760. /* Use the FFDHE parameters */
  1761. static int use_ffdhe = 0;
  1762. #endif
  1763. /* Don't print out in CSV format by default */
  1764. static int csv_format = 0;
  1765. #ifdef WOLFSSL_XILINX_CRYPT_VERSAL
  1766. /* Versal PLM maybe prints an error message to the same console.
  1767. * In order to not mix those outputs up, sleep a little while
  1768. * before erroring out.
  1769. */
  1770. #define SLEEP_ON_ERROR(ret) do{ if (ret != 0) { sleep(1); } }while(0)
  1771. #else
  1772. #define SLEEP_ON_ERROR(ret) do{ /* noop */ }while(0)
  1773. #endif
  1774. /* globals for cipher tests */
  1775. static THREAD_LS_T byte* bench_plain = NULL;
  1776. static THREAD_LS_T byte* bench_cipher = NULL;
  1777. #ifndef NO_FILESYSTEM
  1778. static THREAD_LS_T char* hash_input = NULL;
  1779. static THREAD_LS_T char* cipher_input = NULL;
  1780. #endif
  1781. static const XGEN_ALIGN byte bench_key_buf[] =
  1782. {
  1783. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  1784. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  1785. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67,
  1786. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  1787. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  1788. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff,
  1789. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  1790. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
  1791. };
  1792. static const XGEN_ALIGN byte bench_iv_buf[] =
  1793. {
  1794. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  1795. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1796. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  1797. };
  1798. static THREAD_LS_T byte* bench_key = NULL;
  1799. static THREAD_LS_T byte* bench_iv = NULL;
  1800. #ifdef HAVE_RENESAS_SYNC
  1801. static THREAD_LS_T byte* bench_key1 = NULL;
  1802. static THREAD_LS_T byte* bench_key2 = NULL;
  1803. #endif
  1804. #ifdef WOLFSSL_STATIC_MEMORY
  1805. #ifdef WOLFSSL_STATIC_MEMORY_TEST_SZ
  1806. static byte gBenchMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  1807. #elif defined(BENCH_EMBEDDED)
  1808. static byte gBenchMemory[50000];
  1809. #else
  1810. static byte gBenchMemory[400000];
  1811. #endif
  1812. #endif
  1813. /* This code handles cases with systems where static (non cost) ram variables
  1814. aren't properly initialized with data */
  1815. static void benchmark_static_init(int force)
  1816. {
  1817. static int gBenchStaticInit = 0;
  1818. if (gBenchStaticInit == 0 || force) {
  1819. gBenchStaticInit = 1;
  1820. /* Init static variables */
  1821. numBlocks = NUM_BLOCKS;
  1822. bench_size = BENCH_SIZE;
  1823. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1824. aesAuthAddSz = AES_AUTH_ADD_SZ;
  1825. aes_aad_options = AES_AAD_OPTIONS_DEFAULT;
  1826. aes_aad_size = 0;
  1827. #endif
  1828. base2 = 1;
  1829. digest_stream = 1;
  1830. #ifdef MULTI_VALUE_STATISTICS
  1831. minimum_runs = 0;
  1832. #endif
  1833. bench_all = 1;
  1834. bench_cipher_algs = 0;
  1835. bench_digest_algs = 0;
  1836. bench_mac_algs = 0;
  1837. bench_kdf_algs = 0;
  1838. bench_asym_algs = 0;
  1839. bench_pq_asym_algs = 0;
  1840. bench_other_algs = 0;
  1841. bench_pq_hash_sig_algs = 0;
  1842. csv_format = 0;
  1843. }
  1844. }
  1845. /*****************************************************************************/
  1846. /* Begin Stats Functions */
  1847. /*****************************************************************************/
  1848. typedef enum bench_stat_type {
  1849. BENCH_STAT_ASYM,
  1850. BENCH_STAT_SYM,
  1851. BENCH_STAT_IGNORE,
  1852. } bench_stat_type_t;
  1853. #ifdef WC_BENCH_TRACK_STATS
  1854. static int gPrintStats = 0;
  1855. #ifdef WC_ENABLE_BENCH_THREADING
  1856. static pthread_mutex_t bench_lock = PTHREAD_MUTEX_INITIALIZER;
  1857. #endif
  1858. #ifndef BENCH_MAX_NAME_SZ
  1859. #define BENCH_MAX_NAME_SZ 24
  1860. #endif
  1861. typedef struct bench_stats {
  1862. struct bench_stats* next;
  1863. struct bench_stats* prev;
  1864. char algo[BENCH_MAX_NAME_SZ+1]; /* may not be static, so make copy */
  1865. const char* desc;
  1866. double perfsec;
  1867. int strength;
  1868. int useDeviceID;
  1869. int finishCount;
  1870. bench_stat_type_t type;
  1871. int lastRet;
  1872. const char* perftype;
  1873. } bench_stats_t;
  1874. static bench_stats_t* bench_stats_head;
  1875. static bench_stats_t* bench_stats_tail;
  1876. static bench_stats_t* bench_stats_add(bench_stat_type_t type,
  1877. const char* algo, int strength, const char* desc, int useDeviceID,
  1878. double perfsec, const char* perftype, int ret)
  1879. {
  1880. bench_stats_t* bstat = NULL;
  1881. #ifdef WC_ENABLE_BENCH_THREADING
  1882. /* protect bench_stats_head and bench_stats_tail access */
  1883. THREAD_CHECK_RET(pthread_mutex_lock(&bench_lock));
  1884. #endif
  1885. if (algo != NULL) {
  1886. /* locate existing in list */
  1887. for (bstat = bench_stats_head; bstat != NULL; bstat = bstat->next) {
  1888. /* match based on algo, strength and desc */
  1889. if (XSTRNCMP(bstat->algo, algo, BENCH_MAX_NAME_SZ) == 0 &&
  1890. bstat->strength == strength &&
  1891. bstat->desc == desc &&
  1892. bstat->useDeviceID == useDeviceID) {
  1893. break;
  1894. }
  1895. }
  1896. }
  1897. if (bstat == NULL) {
  1898. /* allocate new and put on list */
  1899. bstat = (bench_stats_t*)XMALLOC(sizeof(bench_stats_t), NULL,
  1900. DYNAMIC_TYPE_INFO);
  1901. if (bstat) {
  1902. XMEMSET(bstat, 0, sizeof(bench_stats_t));
  1903. /* add to list */
  1904. bstat->next = NULL;
  1905. if (bench_stats_tail == NULL) {
  1906. bench_stats_head = bstat;
  1907. }
  1908. else {
  1909. bench_stats_tail->next = bstat;
  1910. bstat->prev = bench_stats_tail;
  1911. }
  1912. bench_stats_tail = bstat; /* add to the end either way */
  1913. }
  1914. }
  1915. if (bstat) {
  1916. bstat->type = type;
  1917. if (algo != NULL)
  1918. XSTRNCPY(bstat->algo, algo, BENCH_MAX_NAME_SZ);
  1919. bstat->strength = strength;
  1920. bstat->desc = desc;
  1921. bstat->useDeviceID = useDeviceID;
  1922. bstat->perfsec += perfsec;
  1923. bstat->finishCount++;
  1924. bstat->perftype = perftype;
  1925. if (bstat->lastRet > ret)
  1926. bstat->lastRet = ret; /* track last error */
  1927. }
  1928. #ifdef WC_ENABLE_BENCH_THREADING
  1929. THREAD_CHECK_RET(pthread_mutex_unlock(&bench_lock));
  1930. #endif
  1931. return bstat;
  1932. }
  1933. void bench_stats_print(void)
  1934. {
  1935. bench_stats_t* bstat;
  1936. int digits;
  1937. #ifdef WC_ENABLE_BENCH_THREADING
  1938. /* protect bench_stats_head and bench_stats_tail access */
  1939. THREAD_CHECK_RET(pthread_mutex_lock(&bench_lock));
  1940. #endif
  1941. #ifdef BENCH_MICROSECOND
  1942. digits = 5;
  1943. #else
  1944. digits = 3;
  1945. #endif
  1946. for (bstat = bench_stats_head; bstat != NULL; ) {
  1947. if (bstat->type == BENCH_STAT_SYM) {
  1948. printf("%-16s%s " FLT_FMT_PREC2 " %s/" WOLFSSL_FIXED_TIME_UNIT
  1949. "\n", bstat->desc,
  1950. BENCH_DEVID_GET_NAME(bstat->useDeviceID),
  1951. FLT_FMT_PREC2_ARGS(8, digits, bstat->perfsec),
  1952. base2 ? "MB" : "mB");
  1953. }
  1954. else {
  1955. printf("%-5s %4d %-9s %s " FLT_FMT_PREC " ops/"
  1956. WOLFSSL_FIXED_TIME_UNIT "ec\n",
  1957. bstat->algo, bstat->strength, bstat->desc,
  1958. BENCH_DEVID_GET_NAME(bstat->useDeviceID),
  1959. FLT_FMT_PREC_ARGS(digits, bstat->perfsec));
  1960. }
  1961. bstat = bstat->next;
  1962. }
  1963. #ifdef WC_ENABLE_BENCH_THREADING
  1964. THREAD_CHECK_RET(pthread_mutex_unlock(&bench_lock));
  1965. #endif
  1966. }
  1967. #endif /* WC_BENCH_TRACK_STATS */
  1968. static WC_INLINE void bench_stats_init(void)
  1969. {
  1970. #ifdef WC_BENCH_TRACK_STATS
  1971. bench_stats_head = NULL;
  1972. bench_stats_tail = NULL;
  1973. #endif
  1974. INIT_CYCLE_COUNTER
  1975. }
  1976. static WC_INLINE void bench_stats_start(int* count, double* start)
  1977. {
  1978. *count = 0;
  1979. *start = current_time(1);
  1980. #ifdef WOLFSSL_ESPIDF
  1981. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1982. ESP_LOGI(TAG, "bench_stats_start total_cycles = %llu, start=" FLT_FMT,
  1983. total_cycles, FLT_FMT_ARGS(*start) );
  1984. #endif
  1985. BEGIN_ESP_CYCLES
  1986. #else
  1987. BEGIN_INTEL_CYCLES
  1988. #endif
  1989. }
  1990. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  1991. #define bench_stats_start(count, start) do { \
  1992. SAVE_VECTOR_REGISTERS(pr_err( \
  1993. "SAVE_VECTOR_REGISTERS failed for benchmark run."); \
  1994. return; ); \
  1995. bench_stats_start(count, start); \
  1996. } while (0)
  1997. #endif
  1998. static WC_INLINE int bench_stats_check(double start)
  1999. {
  2000. int ret = 0;
  2001. double this_current_time;
  2002. this_current_time = current_time(0); /* get the timestamp, no reset */
  2003. #if defined(DEBUG_WOLFSSL_BENCHMARK_TIMING)
  2004. #if (WOLFSSL_ESPIDF)
  2005. ESP_LOGI(TAG, "bench_stats_check Current time %f, start %f",
  2006. this_current_time, start );
  2007. #endif
  2008. #endif
  2009. ret = ((this_current_time - start) < BENCH_MIN_RUNTIME_SEC
  2010. #ifdef BENCH_MICROSECOND
  2011. * 1000000
  2012. #endif
  2013. );
  2014. return ret;
  2015. }
  2016. /* return text for units and scale the value of blocks as needed */
  2017. static const char* get_blocktype(double* blocks)
  2018. {
  2019. const char* rt;
  2020. #if ( defined(WOLFSSL_BENCHMARK_FIXED_UNITS_G) || \
  2021. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_GB))
  2022. #undef WOLFSSL_FIXED_UNIT
  2023. #define WOLFSSL_FIXED_UNIT "GB"
  2024. *blocks /= (1024UL * 1024UL * 1024UL);
  2025. rt = "GiB";
  2026. #elif (defined(WOLFSSL_BENCHMARK_FIXED_UNITS_M) || \
  2027. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_MB))
  2028. #undef WOLFSSL_FIXED_UNIT
  2029. #define WOLFSSL_FIXED_UNIT "MB"
  2030. *blocks /= (1024UL * 1024UL);
  2031. rt = "MiB";
  2032. #elif (defined(WOLFSSL_BENCHMARK_FIXED_UNITS_K) || \
  2033. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_KB))
  2034. #undef WOLFSSL_FIXED_UNIT
  2035. #define WOLFSSL_FIXED_UNIT "KB"
  2036. *blocks /= 1024;
  2037. rt = "KiB";
  2038. #elif defined (WOLFSSL_BENCHMARK_FIXED_UNITS_B)
  2039. #undef WOLFSSL_FIXED_UNIT
  2040. #define WOLFSSL_FIXED_UNIT "bytes"
  2041. (void)(*blocks); /* no adjustment, just appease compiler for not used */
  2042. rt = "bytes";
  2043. #else
  2044. /* If no user-specified, auto-scale each metric (results vary).
  2045. * Determine if we should show as KB or MB or bytes. No GiB here. */
  2046. if (*blocks > (1024UL * 1024UL)) {
  2047. *blocks /= (1024UL * 1024UL);
  2048. rt = "MiB";
  2049. }
  2050. else if (*blocks > 1024) {
  2051. *blocks /= 1024;
  2052. rt = "KiB";
  2053. }
  2054. else {
  2055. rt = "bytes";
  2056. }
  2057. #endif
  2058. return rt;
  2059. }
  2060. /* return text for units and scale the value of blocks as needed for base2 */
  2061. static const char* get_blocktype_base10(double* blocks)
  2062. {
  2063. const char* rt;
  2064. #if ( defined(WOLFSSL_BENCHMARK_FIXED_UNITS_G) || \
  2065. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_GB))
  2066. *blocks /= (1000UL * 1000UL * 1000UL);
  2067. rt = "GB";
  2068. #elif (defined(WOLFSSL_BENCHMARK_FIXED_UNITS_M) || \
  2069. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_MB))
  2070. *blocks /= (1000UL * 1000UL);
  2071. rt = "MB";
  2072. #elif (defined(WOLFSSL_BENCHMARK_FIXED_UNITS_K) || \
  2073. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_KB))
  2074. *blocks /= (1000UL);
  2075. rt = "KB";
  2076. #elif defined (WOLFSSL_BENCHMARK_FIXED_UNITS_B)
  2077. (void)(*blocks); /* no adjustment, just appease compiler */
  2078. rt = "bytes";
  2079. #else
  2080. /* If not user-specified, auto-scale each metric (results vary).
  2081. * Determine if we should show as KB or MB or bytes */
  2082. if (*blocks > (1000UL * 1000UL)) {
  2083. *blocks /= (1000UL * 1000UL);
  2084. rt = "MB";
  2085. }
  2086. else if (*blocks > 1000) {
  2087. *blocks /= 1000; /* make KB */
  2088. rt = "KB";
  2089. }
  2090. else {
  2091. rt = "bytes";
  2092. }
  2093. #endif
  2094. return rt;
  2095. }
  2096. #ifdef MULTI_VALUE_STATISTICS
  2097. static double wc_sqroot(double in)
  2098. {
  2099. /* do 32 iterations for the sqroot */
  2100. int iter = 32;
  2101. double root = in/3.0;
  2102. if (in < 0.0)
  2103. return -1;
  2104. for (int i=0; i < iter; i++)
  2105. root = (root + in / root) / 2.0;
  2106. return root;
  2107. }
  2108. static void bench_multi_value_stats(double max, double min, double sum,
  2109. double squareSum, int runs)
  2110. {
  2111. double mean = 0;
  2112. double sd = 0;
  2113. char msg[WC_BENCH_MAX_LINE_LEN];
  2114. const char** word = bench_result_words3[lng_index];
  2115. XMEMSET(msg, 0, sizeof(msg));
  2116. mean = sum / runs;
  2117. /* Calculating standard deviation */
  2118. sd = (squareSum / runs) - (mean * mean);
  2119. sd = wc_sqroot(sd);
  2120. if (csv_format == 1) {
  2121. (void)XSNPRINTF(msg, sizeof(msg), FLT_FMT_PREC2 ","
  2122. FLT_FMT_PREC2 "," FLT_FMT_PREC2 "," FLT_FMT_PREC2 ",\n",
  2123. FLT_FMT_PREC2_ARGS(3, 3, max),
  2124. FLT_FMT_PREC2_ARGS(3, 3, min),
  2125. FLT_FMT_PREC2_ARGS(3, 3, mean),
  2126. FLT_FMT_PREC2_ARGS(3, 3, sd));
  2127. }
  2128. else{
  2129. (void)XSNPRINTF(msg, sizeof(msg), ", %s " FLT_FMT_PREC2 " "
  2130. WOLFSSL_FIXED_TIME_UNIT ", %s " FLT_FMT_PREC2 " "
  2131. WOLFSSL_FIXED_TIME_UNIT ", %s " FLT_FMT_PREC2 " "
  2132. WOLFSSL_FIXED_TIME_UNIT ", %s " FLT_FMT_PREC2 " "
  2133. WOLFSSL_FIXED_TIME_UNIT "\n",
  2134. word[0], FLT_FMT_PREC2_ARGS(3, 3, max),
  2135. word[1], FLT_FMT_PREC2_ARGS(3, 3, min),
  2136. word[2], FLT_FMT_PREC2_ARGS(3, 3, mean),
  2137. word[3], FLT_FMT_PREC2_ARGS(3, 3, sd));
  2138. }
  2139. printf("%s", msg);
  2140. #ifndef WOLFSSL_SGX
  2141. XFFLUSH(stdout);
  2142. #endif
  2143. }
  2144. #endif
  2145. /* countSz is number of bytes that 1 count represents. Normally bench_size,
  2146. * except for AES direct that operates on AES_BLOCK_SIZE blocks */
  2147. static void bench_stats_sym_finish(const char* desc, int useDeviceID,
  2148. int count, word32 countSz,
  2149. double start, int ret)
  2150. {
  2151. double total, persec = 0, blocks = (double)count;
  2152. const char* blockType;
  2153. char msg[WC_BENCH_MAX_LINE_LEN];
  2154. const char** word = bench_result_words1[lng_index];
  2155. static int sym_header_printed = 0;
  2156. XMEMSET(msg, 0, sizeof(msg));
  2157. #ifdef WOLFSSL_ESPIDF
  2158. END_ESP_CYCLES
  2159. #else
  2160. END_INTEL_CYCLES
  2161. #endif
  2162. total = current_time(0) - start;
  2163. #if defined(WOLFSSL_ESPIDF) && defined(DEBUG_WOLFSSL_BENCHMARK_TIMING)
  2164. ESP_LOGI(TAG, "%s total_cycles = %llu", desc, total_cycles);
  2165. #endif
  2166. #ifdef LINUX_RUSAGE_UTIME
  2167. check_for_excessive_stime(desc, "");
  2168. #endif
  2169. /* calculate actual bytes */
  2170. blocks *= countSz;
  2171. if (csv_format == 1) {
  2172. /* only print out header once */
  2173. if (sym_header_printed == 0) {
  2174. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2175. /* machine parseable CSV */
  2176. #ifdef HAVE_GET_CYCLES
  2177. printf("%s", "\"sym\",Algorithm,HW/SW,bytes_total,"
  2178. WOLFSSL_FIXED_TIME_UNIT "econds_total,"
  2179. WOLFSSL_FIXED_UNIT "/" WOLFSSL_FIXED_TIME_UNIT
  2180. ",cycles_total,Cycles per byte,");
  2181. #else
  2182. printf("%s", "\"sym\",Algorithm,HW/SW,bytes_total,"
  2183. WOLFSSL_FIXED_TIME_UNIT "econds_total,"
  2184. WOLFSSL_FIXED_UNIT "/" WOLFSSL_FIXED_TIME_UNIT
  2185. ",cycles_total,");
  2186. #endif
  2187. #else
  2188. /* normal CSV */
  2189. #ifdef BENCH_DEVID
  2190. #define BENCH_DEVID_COLUMN_HEADER "HW/SW,"
  2191. #else
  2192. #define BENCH_DEVID_COLUMN_HEADER
  2193. #endif
  2194. #ifdef HAVE_GET_CYCLES
  2195. printf("\n\nSymmetric Ciphers:\n\n");
  2196. printf("Algorithm,"
  2197. BENCH_DEVID_COLUMN_HEADER
  2198. WOLFSSL_FIXED_UNIT "/" WOLFSSL_FIXED_TIME_UNIT
  2199. ",Cycles per byte,");
  2200. #else
  2201. printf("\n\nSymmetric Ciphers:\n\n");
  2202. printf("Algorithm,"
  2203. BENCH_DEVID_COLUMN_HEADER
  2204. WOLFSSL_FIXED_UNIT "/" WOLFSSL_FIXED_TIME_UNIT ",");
  2205. #endif
  2206. #endif
  2207. #ifdef MULTI_VALUE_STATISTICS
  2208. printf("max duration,min duration,mean duration,sd,\n");
  2209. #else
  2210. printf("\n");
  2211. #endif
  2212. sym_header_printed = 1;
  2213. }
  2214. }
  2215. /* determine if we have fixed units, or auto-scale bits or bytes for units.
  2216. * note that the blockType text is assigned AND the blocks param is scaled.
  2217. */
  2218. if (base2) {
  2219. blockType = get_blocktype(&blocks);
  2220. }
  2221. else {
  2222. blockType = get_blocktype_base10(&blocks);
  2223. }
  2224. /* calculate blocks per second */
  2225. if (total > 0) {
  2226. persec = (1 / total) * blocks;
  2227. }
  2228. SLEEP_ON_ERROR(ret);
  2229. /* format and print to terminal */
  2230. if (csv_format == 1) {
  2231. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2232. #ifdef WOLFSSL_ESPIDF
  2233. unsigned long bytes_processed =
  2234. (unsigned long)count * (unsigned long)countSz;
  2235. #else
  2236. word64 bytes_processed = (word64)count * (word64)countSz;
  2237. #endif
  2238. /* note this codepath brings in all the fields from the non-CSV case. */
  2239. #ifdef WOLFSSL_ESPIDF
  2240. #ifdef HAVE_GET_CYCLES
  2241. (void)XSNPRINTF(msg, sizeof(msg),
  2242. "sym,%s,%s,%lu," FLT_FMT "," FLT_FMT ",%lu,", desc,
  2243. BENCH_DEVID_GET_NAME(useDeviceID),
  2244. bytes_processed, FLT_FMT_ARGS(total),
  2245. FLT_FMT_ARGS(persec),
  2246. (long unsigned int) total_cycles);
  2247. #else
  2248. #warning "HAVE_GET_CYCLES should be defined for WOLFSSL_ESPIDF"
  2249. #endif
  2250. /* implement other architectures here */
  2251. #else
  2252. #ifdef HAVE_GET_CYCLES
  2253. (void)XSNPRINTF(msg, sizeof(msg),
  2254. "sym,%s,%s,%lu," FLT_FMT "," FLT_FMT ",%lu,", desc,
  2255. BENCH_DEVID_GET_NAME(useDeviceID),
  2256. bytes_processed, FLT_FMT_ARGS(total),
  2257. FLT_FMT_ARGS(persec), total_cycles);
  2258. #else
  2259. (void)XSNPRINTF(msg, sizeof(msg),
  2260. "sym,%s,%s,%lu," FLT_FMT "," FLT_FMT ",", desc,
  2261. BENCH_DEVID_GET_NAME(useDeviceID),
  2262. bytes_processed, FLT_FMT_ARGS(total),
  2263. FLT_FMT_ARGS(persec));
  2264. #endif
  2265. #endif
  2266. #elif defined(BENCH_DEVID)
  2267. (void)XSNPRINTF(msg, sizeof(msg), "%s,%s," FLT_FMT ",", desc,
  2268. BENCH_DEVID_GET_NAME(useDeviceID), FLT_FMT_ARGS(persec));
  2269. #else
  2270. (void)XSNPRINTF(msg, sizeof(msg), "%s," FLT_FMT ",", desc,
  2271. FLT_FMT_ARGS(persec));
  2272. #endif
  2273. #ifdef WOLFSSL_ESPIDF
  2274. SHOW_ESP_CYCLES_CSV(msg, sizeof(msg), countSz);
  2275. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  2276. ESP_LOGI(TAG, "bench_stats_sym_finish total_cycles = %llu",
  2277. total_cycles);
  2278. #endif
  2279. /* implement other cycle counters here */
  2280. #else
  2281. /* the default cycle counter is Intel */
  2282. SHOW_INTEL_CYCLES_CSV(msg, sizeof(msg), (unsigned)countSz);
  2283. #endif
  2284. } /* if (csv_format == 1) */
  2285. else {
  2286. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2287. #ifdef HAVE_GET_CYCLES
  2288. (void)XSNPRINTF(msg, sizeof(msg),
  2289. "%-24s%s " FLT_FMT_PREC2 " %s %s " FLT_FMT_PREC2 " %s, "
  2290. FLT_FMT_PREC2 " %s/" WOLFSSL_FIXED_TIME_UNIT ", %lu cycles,",
  2291. desc, BENCH_DEVID_GET_NAME(useDeviceID),
  2292. FLT_FMT_PREC2_ARGS(5, 0, blocks), blockType,
  2293. word[0], FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2294. FLT_FMT_PREC2_ARGS(8, 3, persec), blockType,
  2295. (unsigned long) total_cycles);
  2296. #else
  2297. (void)XSNPRINTF(msg, sizeof(msg),
  2298. "%-24s%s " FLT_FMT_PREC2 " %s %s " FLT_FMT_PREC2 " %s, "
  2299. FLT_FMT_PREC2 " %s/" WOLFSSL_FIXED_TIME_UNIT ",",
  2300. desc, BENCH_DEVID_GET_NAME(useDeviceID),
  2301. FLT_FMT_PREC2_ARGS(5, 0, blocks), blockType,
  2302. word[0], FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2303. FLT_FMT_PREC2_ARGS(8, 3, persec), blockType);
  2304. #endif /* HAVE_GET_CYCLES */
  2305. #else
  2306. (void)XSNPRINTF(msg, sizeof(msg),
  2307. "%-24s%s " FLT_FMT_PREC2 " %s %s " FLT_FMT_PREC2 " %s, "
  2308. FLT_FMT_PREC2 " %s/" WOLFSSL_FIXED_TIME_UNIT,
  2309. desc, BENCH_DEVID_GET_NAME(useDeviceID),
  2310. FLT_FMT_PREC2_ARGS(5, 0, blocks), blockType,
  2311. word[0], FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2312. FLT_FMT_PREC2_ARGS(8, 3, persec), blockType);
  2313. #endif
  2314. #ifdef WOLFSSL_ESPIDF
  2315. SHOW_ESP_CYCLES(msg, sizeof(msg), countSz);
  2316. /* implement other architecture cycle counters here */
  2317. #else
  2318. SHOW_INTEL_CYCLES(msg, sizeof(msg), (unsigned)countSz);
  2319. #endif
  2320. } /* not CSV format */
  2321. printf("%s", msg);
  2322. /* show errors */
  2323. if (ret < 0) {
  2324. printf("%sBenchmark %s failed: %d\n", err_prefix, desc, ret);
  2325. }
  2326. #ifndef WOLFSSL_SGX
  2327. XFFLUSH(stdout);
  2328. #endif
  2329. #ifdef WC_BENCH_TRACK_STATS
  2330. /* Add to thread stats */
  2331. bench_stats_add(BENCH_STAT_SYM, desc, 0, desc, useDeviceID, persec,
  2332. blockType, ret);
  2333. #endif
  2334. (void)useDeviceID;
  2335. (void)ret;
  2336. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  2337. RESTORE_VECTOR_REGISTERS();
  2338. #endif
  2339. TEST_SLEEP();
  2340. } /* bench_stats_sym_finish */
  2341. #ifdef BENCH_ASYM
  2342. #if defined(HAVE_ECC) || !defined(NO_RSA) || !defined(NO_DH) || \
  2343. defined(HAVE_CURVE25519) || defined(HAVE_ED25519) || \
  2344. defined(HAVE_CURVE448) || defined(HAVE_ED448) || \
  2345. defined(WOLFSSL_HAVE_KYBER)
  2346. static void bench_stats_asym_finish_ex(const char* algo, int strength,
  2347. const char* desc, const char* desc_extra, int useDeviceID, int count,
  2348. double start, int ret)
  2349. {
  2350. double total, each = 0, opsSec, milliEach;
  2351. const char **word = bench_result_words2[lng_index];
  2352. #ifdef WC_BENCH_TRACK_STATS
  2353. const char* kOpsSec = "Ops/Sec";
  2354. #endif
  2355. char msg[256];
  2356. static int asym_header_printed = 0;
  2357. #ifdef BENCH_MICROSECOND
  2358. const int digits = 5;
  2359. #else
  2360. const int digits = 3;
  2361. #endif
  2362. XMEMSET(msg, 0, sizeof(msg));
  2363. total = current_time(0) - start;
  2364. #ifdef LINUX_RUSAGE_UTIME
  2365. check_for_excessive_stime(desc, desc_extra);
  2366. #endif
  2367. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2368. #ifdef WOLFSSL_ESPIDF
  2369. END_ESP_CYCLES
  2370. #else
  2371. END_INTEL_CYCLES
  2372. #endif
  2373. #endif
  2374. /* some sanity checks on the final numbers */
  2375. if (count > 0) {
  2376. each = total / count; /* per second */
  2377. }
  2378. else {
  2379. count = 0;
  2380. each = 0;
  2381. }
  2382. if (total > 0) {
  2383. opsSec = count / total; /* ops second */
  2384. }
  2385. else {
  2386. opsSec = 0;
  2387. }
  2388. #ifdef BENCH_MICROSECOND
  2389. milliEach = each / 1000; /* milliseconds */
  2390. #else
  2391. milliEach = each * 1000; /* milliseconds */
  2392. #endif
  2393. SLEEP_ON_ERROR(ret);
  2394. #ifdef MULTI_VALUE_STATISTICS /* Print without avg ms */
  2395. (void)milliEach;
  2396. /* format and print to terminal */
  2397. if (csv_format == 1) {
  2398. /* only print out header once */
  2399. if (asym_header_printed == 0) {
  2400. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2401. #ifdef HAVE_GET_CYCLES
  2402. printf("%s", "\"asym\",Algorithm,key size,operation,ops/"
  2403. WOLFSSL_FIXED_TIME_UNIT "ec,ops," WOLFSSL_FIXED_TIME_UNIT
  2404. "ecs,cycles,cycles/op,");
  2405. #else
  2406. printf("%s", "\"asym\",Algorithm,key size,operation,ops/"
  2407. WOLFSSL_FIXED_TIME_UNIT "ec,ops," WOLFSSL_FIXED_TIME_UNIT
  2408. "ecs,");
  2409. #endif
  2410. #else
  2411. printf("\n%sAsymmetric Ciphers:\n\n", info_prefix);
  2412. printf("%sAlgorithm,key size,operation,ops/"
  2413. WOLFSSL_FIXED_TIME_UNIT "ec,", info_prefix);
  2414. #endif
  2415. printf("max duration,min duration,mean duration,sd,\n");
  2416. asym_header_printed = 1;
  2417. }
  2418. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2419. #ifdef HAVE_GET_CYCLES
  2420. (void)XSNPRINTF(msg, sizeof(msg),
  2421. "asym,%s,%d,%s%s," FLT_FMT_PREC ",%d,"
  2422. FLT_FMT ",%lu," FLT_FMT_PREC STATS_CLAUSE_SEPARATOR,
  2423. algo, strength, desc, desc_extra,
  2424. FLT_FMT_PREC_ARGS(digits, opsSec),
  2425. count, FLT_FMT_ARGS(total), (unsigned long)total_cycles,
  2426. FLT_FMT_PREC_ARGS(6,
  2427. (double)total_cycles / (double)count));
  2428. #else
  2429. (void)XSNPRINTF(msg, sizeof(msg),
  2430. "asym,%s,%d,%s%s," FLT_FMT_PREC ",%d,"
  2431. FLT_FMT STATS_CLAUSE_SEPARATOR,
  2432. algo, strength, desc, desc_extra,
  2433. FLT_FMT_PREC_ARGS(digits, opsSec),
  2434. count, FLT_FMT_ARGS(total));
  2435. #endif
  2436. #else
  2437. (void)XSNPRINTF(msg, sizeof(msg), "%s,%d,%s%s,"
  2438. FLT_FMT_PREC "," STATS_CLAUSE_SEPARATOR,
  2439. algo, strength, desc, desc_extra,
  2440. FLT_FMT_PREC_ARGS(digits, opsSec));
  2441. #endif
  2442. } /* if (csv_format == 1) */
  2443. else {
  2444. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2445. #ifdef HAVE_GET_CYCLES
  2446. (void)XSNPRINTF(msg, sizeof(msg),
  2447. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, "
  2448. FLT_FMT_PREC " %s, %lu cycles" STATS_CLAUSE_SEPARATOR,
  2449. algo, strength, desc, desc_extra,
  2450. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2451. FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2452. FLT_FMT_PREC_ARGS(digits, opsSec), word[3],
  2453. (unsigned long)total_cycles);
  2454. #else
  2455. (void)XSNPRINTF(msg, sizeof(msg),
  2456. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, "
  2457. FLT_FMT_PREC " %s" STATS_CLAUSE_SEPARATOR,
  2458. algo, strength, desc, desc_extra,
  2459. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2460. FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2461. FLT_FMT_PREC_ARGS(digits, opsSec), word[3]);
  2462. #endif /* HAVE_GET_CYCLES */
  2463. #else
  2464. (void)XSNPRINTF(msg, sizeof(msg),
  2465. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, "
  2466. FLT_FMT_PREC " %s" STATS_CLAUSE_SEPARATOR,
  2467. algo, strength, desc, desc_extra,
  2468. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2469. FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2470. FLT_FMT_PREC_ARGS(digits, opsSec), word[3]);
  2471. #endif
  2472. }
  2473. #else /* MULTI_VALUE_STATISTICS. Print with avg ms */
  2474. /* format and print to terminal */
  2475. if (csv_format == 1) {
  2476. /* only print out header once */
  2477. if (asym_header_printed == 0) {
  2478. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2479. #ifdef HAVE_GET_CYCLES
  2480. printf("%s", "\"asym\",Algorithm,key size,operation,avg ms,ops/"
  2481. WOLFSSL_FIXED_TIME_UNIT "ec,ops," WOLFSSL_FIXED_TIME_UNIT
  2482. "ecs,cycles,cycles/op,");
  2483. #else
  2484. printf("%s", "\"asym\",Algorithm,key size,operation,avg ms,ops/"
  2485. WOLFSSL_FIXED_TIME_UNIT "ec,ops," WOLFSSL_FIXED_TIME_UNIT
  2486. "ecs,");
  2487. #endif
  2488. #else
  2489. printf("\n%sAsymmetric Ciphers:\n\n", info_prefix);
  2490. printf("%sAlgorithm,key size,operation,avg ms,ops/"
  2491. WOLFSSL_FIXED_TIME_UNIT "ec,", info_prefix);
  2492. #endif
  2493. printf("\n");
  2494. asym_header_printed = 1;
  2495. }
  2496. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2497. #ifdef HAVE_GET_CYCLES
  2498. (void)XSNPRINTF(msg, sizeof(msg),
  2499. "asym,%s,%d,%s%s," FLT_FMT_PREC "," FLT_FMT_PREC ",%d,"
  2500. FLT_FMT ",%lu," FLT_FMT_PREC STATS_CLAUSE_SEPARATOR,
  2501. algo, strength, desc, desc_extra,
  2502. FLT_FMT_PREC_ARGS(3, milliEach),
  2503. FLT_FMT_PREC_ARGS(digits, opsSec),
  2504. count, FLT_FMT_ARGS(total), (unsigned long)total_cycles,
  2505. FLT_FMT_PREC_ARGS(6,
  2506. (double)total_cycles / (double)count));
  2507. #else
  2508. (void)XSNPRINTF(msg, sizeof(msg),
  2509. "asym,%s,%d,%s%s," FLT_FMT_PREC "," FLT_FMT_PREC ",%d,"
  2510. FLT_FMT STATS_CLAUSE_SEPARATOR,
  2511. algo, strength, desc, desc_extra,
  2512. FLT_FMT_PREC_ARGS(3, milliEach),
  2513. FLT_FMT_PREC_ARGS(digits, opsSec),
  2514. count, FLT_FMT_ARGS(total));
  2515. #endif
  2516. #else
  2517. (void)XSNPRINTF(msg, sizeof(msg), "%s,%d,%s%s," FLT_FMT_PREC ","
  2518. FLT_FMT_PREC "," STATS_CLAUSE_SEPARATOR,
  2519. algo, strength, desc, desc_extra,
  2520. FLT_FMT_PREC_ARGS(3, milliEach),
  2521. FLT_FMT_PREC_ARGS(digits, opsSec));
  2522. #endif
  2523. } /* if (csv_format == 1) */
  2524. else {
  2525. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2526. #ifdef HAVE_GET_CYCLES
  2527. (void)XSNPRINTF(msg, sizeof(msg),
  2528. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, %s "
  2529. FLT_FMT_PREC2 " ms, " FLT_FMT_PREC " %s, %lu cycles"
  2530. STATS_CLAUSE_SEPARATOR,
  2531. algo, strength, desc, desc_extra,
  2532. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2533. FLT_FMT_PREC2_ARGS(5, 3, total), word[1], word[2],
  2534. FLT_FMT_PREC2_ARGS(5, 3, milliEach),
  2535. FLT_FMT_PREC_ARGS(digits, opsSec), word[3],
  2536. (unsigned long)total_cycles);
  2537. #else
  2538. (void)XSNPRINTF(msg, sizeof(msg),
  2539. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, %s "
  2540. FLT_FMT_PREC2 " ms, " FLT_FMT_PREC " %s"
  2541. STATS_CLAUSE_SEPARATOR,
  2542. algo, strength, desc, desc_extra,
  2543. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2544. FLT_FMT_PREC2_ARGS(5, 3, total), word[1], word[2],
  2545. FLT_FMT_PREC2_ARGS(5, 3, milliEach),
  2546. FLT_FMT_PREC_ARGS(digits, opsSec), word[3]);
  2547. #endif /* HAVE_GET_CYCLES */
  2548. #else
  2549. (void)XSNPRINTF(msg, sizeof(msg),
  2550. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, %s "
  2551. FLT_FMT_PREC2 " ms, " FLT_FMT_PREC " %s"
  2552. STATS_CLAUSE_SEPARATOR,
  2553. algo, strength, desc, desc_extra,
  2554. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2555. FLT_FMT_PREC2_ARGS(5, 3, total), word[1], word[2],
  2556. FLT_FMT_PREC2_ARGS(5, 3, milliEach),
  2557. FLT_FMT_PREC_ARGS(digits, opsSec), word[3]);
  2558. #endif
  2559. }
  2560. #endif /* MULTI_VALUE_STATISTICS */
  2561. printf("%s", msg);
  2562. /* show errors */
  2563. if (ret < 0) {
  2564. printf("%sBenchmark %s %s %d failed: %d\n",
  2565. err_prefix, algo, desc, strength, ret);
  2566. }
  2567. #ifndef WOLFSSL_SGX
  2568. XFFLUSH(stdout);
  2569. #endif
  2570. #ifdef WC_BENCH_TRACK_STATS
  2571. /* Add to thread stats */
  2572. bench_stats_add(BENCH_STAT_ASYM, algo, strength, desc, useDeviceID, opsSec,
  2573. kOpsSec, ret);
  2574. #endif
  2575. (void)useDeviceID;
  2576. (void)ret;
  2577. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  2578. RESTORE_VECTOR_REGISTERS();
  2579. #endif
  2580. TEST_SLEEP();
  2581. } /* bench_stats_asym_finish_ex */
  2582. static void bench_stats_asym_finish(const char* algo, int strength,
  2583. const char* desc, int useDeviceID, int count, double start, int ret)
  2584. {
  2585. bench_stats_asym_finish_ex(algo, strength, desc, "", useDeviceID, count,
  2586. start, ret);
  2587. }
  2588. #endif
  2589. #endif /* BENCH_ASYM */
  2590. static WC_INLINE void bench_stats_free(void)
  2591. {
  2592. #ifdef WC_BENCH_TRACK_STATS
  2593. bench_stats_t* bstat;
  2594. for (bstat = bench_stats_head; bstat != NULL; ) {
  2595. bench_stats_t* next = bstat->next;
  2596. XFREE(bstat, NULL, DYNAMIC_TYPE_INFO);
  2597. bstat = next;
  2598. }
  2599. bench_stats_head = NULL;
  2600. bench_stats_tail = NULL;
  2601. #endif
  2602. }
  2603. /*****************************************************************************/
  2604. /* End Stats Functions */
  2605. /*****************************************************************************/
  2606. static void* benchmarks_do(void* args)
  2607. {
  2608. long bench_buf_size;
  2609. #ifdef WOLFSSL_ASYNC_CRYPT
  2610. #ifndef WC_NO_ASYNC_THREADING
  2611. ThreadData* threadData = (ThreadData*)args;
  2612. if (wolfAsync_DevOpenThread(&devId, &threadData->thread_id) < 0)
  2613. #else
  2614. if (wolfAsync_DevOpen(&devId) < 0)
  2615. #endif
  2616. {
  2617. printf("%sAsync device open failed\n%sRunning without async\n",
  2618. err_prefix, err_prefix);
  2619. }
  2620. #endif /* WOLFSSL_ASYNC_CRYPT */
  2621. (void)args;
  2622. #ifdef WOLFSSL_ASYNC_CRYPT
  2623. if (wolfEventQueue_Init(&eventQueue) != 0) {
  2624. printf("%sAsync event queue init failure!\n", err_prefix);
  2625. }
  2626. #endif
  2627. #ifdef WOLF_CRYPTO_CB
  2628. #ifdef HAVE_INTEL_QA_SYNC
  2629. devId = wc_CryptoCb_InitIntelQa();
  2630. if (devId == INVALID_DEVID) {
  2631. printf("%sCouldn't init the Intel QA\n", err_prefix);
  2632. }
  2633. #endif
  2634. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  2635. devId = wc_CryptoCb_InitOcteon();
  2636. if (devId == INVALID_DEVID) {
  2637. printf("%sCouldn't get the Octeon device ID\n", err_prefix);
  2638. }
  2639. #endif
  2640. #ifdef HAVE_RENESAS_SYNC
  2641. devId = wc_CryptoCb_CryptInitRenesasCmn(NULL, &guser_PKCbInfo);
  2642. if (devId == INVALID_DEVID) {
  2643. printf("%sCouldn't get the Renesas device ID\n", err_prefix);
  2644. }
  2645. #endif
  2646. #endif
  2647. #if defined(HAVE_LOCAL_RNG)
  2648. {
  2649. int rngRet;
  2650. #ifndef HAVE_FIPS
  2651. rngRet = wc_InitRng_ex(&gRng, HEAP_HINT, devId);
  2652. #else
  2653. rngRet = wc_InitRng(&gRng);
  2654. #endif
  2655. if (rngRet < 0) {
  2656. printf("%sInitRNG failed\n", err_prefix);
  2657. return NULL;
  2658. }
  2659. }
  2660. #endif
  2661. /* setup bench plain, cipher, key and iv globals */
  2662. /* make sure bench buffer is multiple of 16 (AES block size) */
  2663. bench_buf_size = (int)bench_size + BENCH_CIPHER_ADD;
  2664. if (bench_buf_size % 16)
  2665. bench_buf_size += 16 - (bench_buf_size % 16);
  2666. #ifdef WOLFSSL_AFALG_XILINX_AES
  2667. bench_plain = (byte*)aligned_alloc(64, (size_t)bench_buf_size + 16);
  2668. bench_cipher = (byte*)aligned_alloc(64, (size_t)bench_buf_size + 16);
  2669. #else
  2670. bench_plain = (byte*)XMALLOC((size_t)bench_buf_size + 16,
  2671. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2672. bench_cipher = (byte*)XMALLOC((size_t)bench_buf_size + 16,
  2673. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2674. #endif
  2675. if (bench_plain == NULL || bench_cipher == NULL) {
  2676. XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2677. XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2678. bench_plain = bench_cipher = NULL;
  2679. printf("%sBenchmark block buffer alloc failed!\n", err_prefix);
  2680. goto exit;
  2681. }
  2682. #ifndef NO_FILESYSTEM
  2683. if (hash_input) {
  2684. int rawSz;
  2685. XFILE file;
  2686. file = XFOPEN(hash_input, "rb");
  2687. if (file == XBADFILE)
  2688. goto exit;
  2689. if (XFSEEK(file, 0, XSEEK_END) != 0) {
  2690. XFCLOSE(file);
  2691. goto exit;
  2692. }
  2693. bench_buf_size = XFTELL(file);
  2694. if(XFSEEK(file, 0, XSEEK_SET) != 0) {
  2695. XFCLOSE(file);
  2696. goto exit;
  2697. }
  2698. XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2699. rawSz = (int)bench_buf_size;
  2700. if (bench_buf_size % 16)
  2701. bench_buf_size += 16 - (bench_buf_size % 16);
  2702. bench_size = (word32)bench_buf_size;
  2703. bench_plain = (byte*)XMALLOC((size_t)bench_buf_size + 16*2,
  2704. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2705. if (bench_plain == NULL) {
  2706. XFCLOSE(file);
  2707. goto exit;
  2708. }
  2709. if ((size_t)XFREAD(bench_plain, 1, rawSz, file)
  2710. != (size_t)rawSz) {
  2711. XFCLOSE(file);
  2712. goto exit;
  2713. }
  2714. XFCLOSE(file);
  2715. }
  2716. else {
  2717. XMEMSET(bench_plain, 0, (size_t)bench_buf_size);
  2718. }
  2719. if (cipher_input) {
  2720. int rawSz;
  2721. XFILE file;
  2722. file = XFOPEN(cipher_input, "rb");
  2723. if (file == XBADFILE)
  2724. goto exit;
  2725. if (XFSEEK(file, 0, XSEEK_END) != 0) {
  2726. XFCLOSE(file);
  2727. goto exit;
  2728. }
  2729. bench_buf_size = XFTELL(file);
  2730. if(XFSEEK(file, 0, XSEEK_SET) != 0) {
  2731. XFCLOSE(file);
  2732. goto exit;
  2733. }
  2734. XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2735. rawSz = (int)bench_buf_size;
  2736. if (bench_buf_size % 16)
  2737. bench_buf_size += 16 - (bench_buf_size % 16);
  2738. if (bench_size > (word32)bench_buf_size)
  2739. bench_size = (word32)bench_buf_size;
  2740. bench_cipher = (byte*)XMALLOC((size_t)bench_buf_size + 16*2,
  2741. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2742. if (bench_cipher == NULL) {
  2743. XFCLOSE(file);
  2744. goto exit;
  2745. }
  2746. if ((size_t)XFREAD(bench_cipher, 1, rawSz, file)
  2747. != (size_t)rawSz) {
  2748. XFCLOSE(file);
  2749. goto exit;
  2750. }
  2751. XFCLOSE(file);
  2752. }
  2753. else {
  2754. XMEMSET(bench_cipher, 0, (size_t)bench_buf_size);
  2755. }
  2756. #endif
  2757. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(HAVE_INTEL_QA_SYNC)
  2758. bench_key = (byte*)XMALLOC(sizeof(bench_key_buf),
  2759. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2760. bench_iv = (byte*)XMALLOC(sizeof(bench_iv_buf),
  2761. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2762. if (bench_key == NULL || bench_iv == NULL) {
  2763. XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2764. XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2765. bench_key = bench_iv = NULL;
  2766. printf("%sBenchmark cipher buffer alloc failed!\n", err_prefix);
  2767. goto exit;
  2768. }
  2769. XMEMCPY(bench_key, bench_key_buf, sizeof(bench_key_buf));
  2770. XMEMCPY(bench_iv, bench_iv_buf, sizeof(bench_iv_buf));
  2771. #elif defined(HAVE_RENESAS_SYNC)
  2772. bench_key1 = (byte*)guser_PKCbInfo.wrapped_key_aes128;
  2773. bench_key2 = (byte*)guser_PKCbInfo.wrapped_key_aes256;
  2774. bench_key = (byte*)bench_key_buf;
  2775. bench_iv = (byte*)bench_iv_buf;
  2776. #else
  2777. bench_key = (byte*)bench_key_buf;
  2778. bench_iv = (byte*)bench_iv_buf;
  2779. #endif
  2780. #ifndef WC_NO_RNG
  2781. if (bench_all || (bench_other_algs & BENCH_RNG))
  2782. bench_rng();
  2783. #endif /* WC_NO_RNG */
  2784. #ifndef NO_AES
  2785. #ifdef HAVE_AES_CBC
  2786. if (bench_all || (bench_cipher_algs & BENCH_AES_CBC)) {
  2787. #ifndef NO_SW_BENCH
  2788. bench_aescbc(0);
  2789. #endif
  2790. #if defined(BENCH_DEVID)
  2791. bench_aescbc(1);
  2792. #endif
  2793. }
  2794. #endif
  2795. #ifdef HAVE_AESGCM
  2796. if (bench_all || (bench_cipher_algs & BENCH_AES_GCM)) {
  2797. #ifndef NO_SW_BENCH
  2798. bench_aes_aad_options_wrap(bench_aesgcm, 0);
  2799. #endif
  2800. #if ((defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)) || \
  2801. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  2802. defined(HAVE_RENESAS_SYNC) || defined(WOLFSSL_CAAM)) && \
  2803. !defined(NO_HW_BENCH)
  2804. bench_aes_aad_options_wrap(bench_aesgcm, 1);
  2805. #endif
  2806. #ifndef NO_SW_BENCH
  2807. bench_gmac(0);
  2808. #endif
  2809. #if defined(BENCH_DEVID)
  2810. bench_gmac(1);
  2811. #endif
  2812. }
  2813. #endif
  2814. #ifdef HAVE_AES_ECB
  2815. if (bench_all || (bench_cipher_algs & BENCH_AES_ECB)) {
  2816. #ifndef NO_SW_BENCH
  2817. bench_aesecb(0);
  2818. #endif
  2819. #ifdef BENCH_DEVID
  2820. bench_aesecb(1);
  2821. #endif
  2822. }
  2823. #endif
  2824. #ifdef WOLFSSL_AES_XTS
  2825. if (bench_all || (bench_cipher_algs & BENCH_AES_XTS))
  2826. bench_aesxts();
  2827. #endif
  2828. #ifdef WOLFSSL_AES_CFB
  2829. if (bench_all || (bench_cipher_algs & BENCH_AES_CFB))
  2830. bench_aescfb();
  2831. #endif
  2832. #ifdef WOLFSSL_AES_OFB
  2833. if (bench_all || (bench_cipher_algs & BENCH_AES_OFB))
  2834. bench_aesofb();
  2835. #endif
  2836. #ifdef WOLFSSL_AES_COUNTER
  2837. if (bench_all || (bench_cipher_algs & BENCH_AES_CTR)) {
  2838. bench_aesctr(0);
  2839. #ifdef BENCH_DEVID
  2840. bench_aesctr(1);
  2841. #endif
  2842. }
  2843. #endif
  2844. #ifdef HAVE_AESCCM
  2845. if (bench_all || (bench_cipher_algs & BENCH_AES_CCM)) {
  2846. bench_aes_aad_options_wrap(bench_aesccm, 0);
  2847. #ifdef BENCH_DEVID
  2848. bench_aes_aad_options_wrap(bench_aesccm, 1);
  2849. #endif
  2850. }
  2851. #endif
  2852. #ifdef WOLFSSL_AES_SIV
  2853. if (bench_all || (bench_cipher_algs & BENCH_AES_SIV))
  2854. bench_aessiv();
  2855. #endif
  2856. #endif /* !NO_AES */
  2857. #ifdef HAVE_CAMELLIA
  2858. if (bench_all || (bench_cipher_algs & BENCH_CAMELLIA))
  2859. bench_camellia();
  2860. #endif
  2861. #ifdef WOLFSSL_SM4_CBC
  2862. if (bench_all || (bench_cipher_algs & BENCH_SM4_CBC))
  2863. bench_sm4_cbc();
  2864. #endif
  2865. #ifdef WOLFSSL_SM4_GCM
  2866. if (bench_all || (bench_cipher_algs & BENCH_SM4_GCM))
  2867. bench_sm4_gcm();
  2868. #endif
  2869. #ifdef WOLFSSL_SM4_CCM
  2870. if (bench_all || (bench_cipher_algs & BENCH_SM4_CCM))
  2871. bench_sm4_ccm();
  2872. #endif
  2873. #ifndef NO_RC4
  2874. if (bench_all || (bench_cipher_algs & BENCH_ARC4)) {
  2875. #ifndef NO_SW_BENCH
  2876. bench_arc4(0);
  2877. #endif
  2878. #ifdef BENCH_DEVID
  2879. bench_arc4(1);
  2880. #endif
  2881. }
  2882. #endif
  2883. #ifdef HAVE_CHACHA
  2884. if (bench_all || (bench_cipher_algs & BENCH_CHACHA20))
  2885. bench_chacha();
  2886. #endif
  2887. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  2888. if (bench_all || (bench_cipher_algs & BENCH_CHACHA20_POLY1305))
  2889. bench_chacha20_poly1305_aead();
  2890. #endif
  2891. #ifndef NO_DES3
  2892. if (bench_all || (bench_cipher_algs & BENCH_DES)) {
  2893. #ifndef NO_SW_BENCH
  2894. bench_des(0);
  2895. #endif
  2896. #ifdef BENCH_DEVID
  2897. bench_des(1);
  2898. #endif
  2899. }
  2900. #endif
  2901. #ifndef NO_MD5
  2902. if (bench_all || (bench_digest_algs & BENCH_MD5)) {
  2903. #ifndef NO_SW_BENCH
  2904. bench_md5(0);
  2905. #endif
  2906. #ifdef BENCH_DEVID
  2907. bench_md5(1);
  2908. #endif
  2909. }
  2910. #endif
  2911. #ifdef HAVE_POLY1305
  2912. if (bench_all || (bench_digest_algs & BENCH_POLY1305))
  2913. bench_poly1305();
  2914. #endif
  2915. #ifndef NO_SHA
  2916. if (bench_all || (bench_digest_algs & BENCH_SHA)) {
  2917. #ifndef NO_SW_BENCH
  2918. bench_sha(0);
  2919. #endif
  2920. #ifdef BENCH_DEVID
  2921. bench_sha(1);
  2922. #endif
  2923. }
  2924. #endif
  2925. #ifdef WOLFSSL_SHA224
  2926. if (bench_all || (bench_digest_algs & BENCH_SHA224)) {
  2927. #ifndef NO_SW_BENCH
  2928. bench_sha224(0);
  2929. #endif
  2930. #ifdef BENCH_DEVID
  2931. bench_sha224(1);
  2932. #endif
  2933. }
  2934. #endif
  2935. #ifndef NO_SHA256
  2936. if (bench_all || (bench_digest_algs & BENCH_SHA256)) {
  2937. #ifndef NO_SW_BENCH
  2938. bench_sha256(0);
  2939. #endif
  2940. #ifdef BENCH_DEVID
  2941. bench_sha256(1);
  2942. #endif
  2943. }
  2944. #endif
  2945. #ifdef WOLFSSL_SHA384
  2946. if (bench_all || (bench_digest_algs & BENCH_SHA384)) {
  2947. #ifndef NO_SW_BENCH
  2948. bench_sha384(0);
  2949. #endif
  2950. #ifdef BENCH_DEVID
  2951. bench_sha384(1);
  2952. #endif
  2953. }
  2954. #endif
  2955. #ifdef WOLFSSL_SHA512
  2956. if (bench_all || (bench_digest_algs & BENCH_SHA512)) {
  2957. #ifndef NO_SW_BENCH
  2958. bench_sha512(0);
  2959. #endif
  2960. #ifdef BENCH_DEVID
  2961. bench_sha512(1);
  2962. #endif
  2963. }
  2964. #if !defined(WOLFSSL_NOSHA512_224) && \
  2965. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  2966. if (bench_all || (bench_digest_algs & BENCH_SHA512)) {
  2967. #ifndef NO_SW_BENCH
  2968. bench_sha512_224(0);
  2969. #endif
  2970. #ifdef BENCH_DEVID
  2971. bench_sha512_224(1);
  2972. #endif
  2973. }
  2974. #endif /* WOLFSSL_NOSHA512_224 */
  2975. #if !defined(WOLFSSL_NOSHA512_256) && \
  2976. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  2977. if (bench_all || (bench_digest_algs & BENCH_SHA512)) {
  2978. #ifndef NO_SW_BENCH
  2979. bench_sha512_256(0);
  2980. #endif
  2981. #ifdef BENCH_DEVID
  2982. bench_sha512_256(1);
  2983. #endif
  2984. }
  2985. #endif /* WOLFSSL_NOSHA512_256 */
  2986. #endif /* WOLFSSL_SHA512 */
  2987. #ifdef WOLFSSL_SHA3
  2988. #ifndef WOLFSSL_NOSHA3_224
  2989. if (bench_all || (bench_digest_algs & BENCH_SHA3_224)) {
  2990. #ifndef NO_SW_BENCH
  2991. bench_sha3_224(0);
  2992. #endif
  2993. #ifdef BENCH_DEVID
  2994. bench_sha3_224(1);
  2995. #endif
  2996. }
  2997. #endif /* WOLFSSL_NOSHA3_224 */
  2998. #ifndef WOLFSSL_NOSHA3_256
  2999. if (bench_all || (bench_digest_algs & BENCH_SHA3_256)) {
  3000. #ifndef NO_SW_BENCH
  3001. bench_sha3_256(0);
  3002. #endif
  3003. #ifdef BENCH_DEVID
  3004. bench_sha3_256(1);
  3005. #endif
  3006. }
  3007. #endif /* WOLFSSL_NOSHA3_256 */
  3008. #ifndef WOLFSSL_NOSHA3_384
  3009. if (bench_all || (bench_digest_algs & BENCH_SHA3_384)) {
  3010. #ifndef NO_SW_BENCH
  3011. bench_sha3_384(0);
  3012. #endif
  3013. #ifdef BENCH_DEVID
  3014. bench_sha3_384(1);
  3015. #endif
  3016. }
  3017. #endif /* WOLFSSL_NOSHA3_384 */
  3018. #ifndef WOLFSSL_NOSHA3_512
  3019. if (bench_all || (bench_digest_algs & BENCH_SHA3_512)) {
  3020. #ifndef NO_SW_BENCH
  3021. bench_sha3_512(0);
  3022. #endif
  3023. #ifdef BENCH_DEVID
  3024. bench_sha3_512(1);
  3025. #endif
  3026. }
  3027. #endif /* WOLFSSL_NOSHA3_512 */
  3028. #ifdef WOLFSSL_SHAKE128
  3029. if (bench_all || (bench_digest_algs & BENCH_SHAKE128)) {
  3030. #ifndef NO_SW_BENCH
  3031. bench_shake128(0);
  3032. #endif
  3033. #ifdef BENCH_DEVID
  3034. bench_shake128(1);
  3035. #endif
  3036. }
  3037. #endif /* WOLFSSL_SHAKE128 */
  3038. #ifdef WOLFSSL_SHAKE256
  3039. if (bench_all || (bench_digest_algs & BENCH_SHAKE256)) {
  3040. #ifndef NO_SW_BENCH
  3041. bench_shake256(0);
  3042. #endif
  3043. #ifdef BENCH_DEVID
  3044. bench_shake256(1);
  3045. #endif
  3046. }
  3047. #endif /* WOLFSSL_SHAKE256 */
  3048. #endif
  3049. #ifdef WOLFSSL_SM3
  3050. if (bench_all || (bench_digest_algs & BENCH_SM3)) {
  3051. #ifndef NO_SW_BENCH
  3052. bench_sm3(0);
  3053. #endif
  3054. #ifdef BENCH_DEVID
  3055. bench_sm3(1);
  3056. #endif
  3057. }
  3058. #endif
  3059. #ifdef WOLFSSL_RIPEMD
  3060. if (bench_all || (bench_digest_algs & BENCH_RIPEMD))
  3061. bench_ripemd();
  3062. #endif
  3063. #ifdef HAVE_BLAKE2
  3064. if (bench_all || (bench_digest_algs & BENCH_BLAKE2B))
  3065. bench_blake2b();
  3066. #endif
  3067. #ifdef HAVE_BLAKE2S
  3068. if (bench_all || (bench_digest_algs & BENCH_BLAKE2S))
  3069. bench_blake2s();
  3070. #endif
  3071. #ifdef WOLFSSL_CMAC
  3072. if (bench_all || (bench_mac_algs & BENCH_CMAC)) {
  3073. bench_cmac(0);
  3074. #ifdef BENCH_DEVID
  3075. bench_cmac(1);
  3076. #endif
  3077. }
  3078. #endif
  3079. #ifndef NO_HMAC
  3080. #ifndef NO_MD5
  3081. if (bench_all || (bench_mac_algs & BENCH_HMAC_MD5)) {
  3082. #ifndef NO_SW_BENCH
  3083. bench_hmac_md5(0);
  3084. #endif
  3085. #ifdef BENCH_DEVID
  3086. bench_hmac_md5(1);
  3087. #endif
  3088. }
  3089. #endif
  3090. #ifndef NO_SHA
  3091. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA)) {
  3092. #ifndef NO_SW_BENCH
  3093. bench_hmac_sha(0);
  3094. #endif
  3095. #ifdef BENCH_DEVID
  3096. bench_hmac_sha(1);
  3097. #endif
  3098. }
  3099. #endif
  3100. #ifdef WOLFSSL_SHA224
  3101. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA224)) {
  3102. #ifndef NO_SW_BENCH
  3103. bench_hmac_sha224(0);
  3104. #endif
  3105. #ifdef BENCH_DEVID
  3106. bench_hmac_sha224(1);
  3107. #endif
  3108. }
  3109. #endif
  3110. #ifndef NO_SHA256
  3111. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA256)) {
  3112. #ifndef NO_SW_BENCH
  3113. bench_hmac_sha256(0);
  3114. #endif
  3115. #ifdef BENCH_DEVID
  3116. bench_hmac_sha256(1);
  3117. #endif
  3118. }
  3119. #endif
  3120. #ifdef WOLFSSL_SHA384
  3121. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA384)) {
  3122. #ifndef NO_SW_BENCH
  3123. bench_hmac_sha384(0);
  3124. #endif
  3125. #ifdef BENCH_DEVID
  3126. bench_hmac_sha384(1);
  3127. #endif
  3128. }
  3129. #endif
  3130. #ifdef WOLFSSL_SHA512
  3131. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA512)) {
  3132. #ifndef NO_SW_BENCH
  3133. bench_hmac_sha512(0);
  3134. #endif
  3135. #ifdef BENCH_DEVID
  3136. bench_hmac_sha512(1);
  3137. #endif
  3138. }
  3139. #endif
  3140. #ifndef NO_PWDBASED
  3141. if (bench_all || (bench_mac_algs & BENCH_PBKDF2)) {
  3142. bench_pbkdf2();
  3143. }
  3144. #endif
  3145. #endif /* NO_HMAC */
  3146. #ifdef WOLFSSL_SIPHASH
  3147. if (bench_all || (bench_mac_algs & BENCH_SIPHASH)) {
  3148. bench_siphash();
  3149. }
  3150. #endif
  3151. #ifdef WC_SRTP_KDF
  3152. if (bench_all || (bench_kdf_algs & BENCH_SRTP_KDF)) {
  3153. bench_srtpkdf();
  3154. }
  3155. #endif
  3156. #ifdef HAVE_SCRYPT
  3157. if (bench_all || (bench_other_algs & BENCH_SCRYPT))
  3158. bench_scrypt();
  3159. #endif
  3160. #ifndef NO_RSA
  3161. #ifndef HAVE_RENESAS_SYNC
  3162. #ifdef WOLFSSL_KEY_GEN
  3163. if (bench_all || (bench_asym_algs & BENCH_RSA_KEYGEN)) {
  3164. #ifndef NO_SW_BENCH
  3165. if (((word32)bench_asym_algs == 0xFFFFFFFFU) ||
  3166. (bench_asym_algs & BENCH_RSA_SZ) == 0) {
  3167. bench_rsaKeyGen(0);
  3168. }
  3169. else {
  3170. bench_rsaKeyGen_size(0, bench_size);
  3171. }
  3172. #endif
  3173. #ifdef BENCH_DEVID
  3174. if (bench_asym_algs & BENCH_RSA_SZ) {
  3175. bench_rsaKeyGen_size(1, bench_size);
  3176. }
  3177. else {
  3178. bench_rsaKeyGen(1);
  3179. }
  3180. #endif
  3181. }
  3182. #endif
  3183. if (bench_all || (bench_asym_algs & BENCH_RSA)) {
  3184. #ifndef NO_SW_BENCH
  3185. bench_rsa(0);
  3186. #endif
  3187. #ifdef BENCH_DEVID
  3188. bench_rsa(1);
  3189. #endif
  3190. }
  3191. #ifdef WOLFSSL_KEY_GEN
  3192. if (bench_asym_algs & BENCH_RSA_SZ) {
  3193. #ifndef NO_SW_BENCH
  3194. bench_rsa_key(0, bench_size);
  3195. #endif
  3196. #ifdef BENCH_DEVID
  3197. bench_rsa_key(1, bench_size);
  3198. #endif
  3199. }
  3200. #endif
  3201. #endif
  3202. #endif
  3203. #ifndef NO_DH
  3204. if (bench_all || (bench_asym_algs & BENCH_DH)) {
  3205. #ifndef NO_SW_BENCH
  3206. bench_dh(0);
  3207. #endif
  3208. #ifdef BENCH_DEVID
  3209. bench_dh(1);
  3210. #endif
  3211. }
  3212. #endif
  3213. #ifdef WOLFSSL_HAVE_KYBER
  3214. if (bench_all || (bench_asym_algs & BENCH_KYBER)) {
  3215. #ifdef WOLFSSL_KYBER512
  3216. bench_kyber(KYBER512);
  3217. #endif
  3218. #ifdef WOLFSSL_KYBER768
  3219. bench_kyber(KYBER768);
  3220. #endif
  3221. #ifdef WOLFSSL_KYBER1024
  3222. bench_kyber(KYBER1024);
  3223. #endif
  3224. }
  3225. #endif
  3226. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  3227. if (bench_all || (bench_pq_hash_sig_algs & BENCH_LMS_HSS)) {
  3228. bench_lms();
  3229. }
  3230. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY) */
  3231. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  3232. if (bench_all) {
  3233. bench_pq_hash_sig_algs |= BENCH_XMSS_XMSSMT;
  3234. }
  3235. #ifndef NO_SHA256
  3236. if (bench_pq_hash_sig_algs & BENCH_XMSS_XMSSMT_SHA256) {
  3237. bench_xmss(WC_HASH_TYPE_SHA256);
  3238. }
  3239. #endif
  3240. #ifdef WOLFSSL_SHA512
  3241. if (bench_pq_hash_sig_algs & BENCH_XMSS_XMSSMT_SHA512) {
  3242. bench_xmss(WC_HASH_TYPE_SHA512);
  3243. }
  3244. #endif
  3245. #ifdef WOLFSSL_SHAKE128
  3246. if (bench_pq_hash_sig_algs & BENCH_XMSS_XMSSMT_SHAKE128) {
  3247. bench_xmss(WC_HASH_TYPE_SHAKE128);
  3248. }
  3249. #endif
  3250. #ifdef WOLFSSL_SHAKE256
  3251. if (bench_pq_hash_sig_algs & BENCH_XMSS_XMSSMT_SHAKE256) {
  3252. bench_xmss(WC_HASH_TYPE_SHAKE256);
  3253. }
  3254. #endif
  3255. #endif /* if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY) */
  3256. #ifdef HAVE_ECC
  3257. if (bench_all || (bench_asym_algs & BENCH_ECC_MAKEKEY) ||
  3258. (bench_asym_algs & BENCH_ECC) ||
  3259. (bench_asym_algs & BENCH_ECC_ALL) ||
  3260. (bench_asym_algs & BENCH_ECC_ENCRYPT)) {
  3261. if (bench_asym_algs & BENCH_ECC_ALL) {
  3262. #if defined(HAVE_FIPS) || defined(HAVE_SELFTEST)
  3263. printf("%snot supported in FIPS mode (no ending enum value)\n",
  3264. err_prefix);
  3265. #else
  3266. int curveId = (int)ECC_SECP192R1;
  3267. /* set make key and encrypt */
  3268. bench_asym_algs |= BENCH_ECC_MAKEKEY | BENCH_ECC |
  3269. BENCH_ECC_ENCRYPT;
  3270. if (csv_format != 1) {
  3271. printf("\n%sECC Benchmarks:\n", info_prefix);
  3272. }
  3273. do {
  3274. #ifdef WOLFCRYPT_HAVE_SAKKE
  3275. /* SAKKE is not usable with ECDH/ECDSA. Run separate test. */
  3276. if (curveId == ECC_SAKKE_1) {
  3277. curveId++;
  3278. continue;
  3279. }
  3280. #endif
  3281. if (wc_ecc_get_curve_size_from_id(curveId) !=
  3282. ECC_BAD_ARG_E) {
  3283. bench_ecc_curve(curveId);
  3284. if (csv_format != 1) {
  3285. printf("\n");
  3286. }
  3287. }
  3288. curveId++;
  3289. } while (curveId != (int)ECC_CURVE_MAX);
  3290. #endif
  3291. }
  3292. else if (bench_asym_algs & BENCH_ECC_P256) {
  3293. bench_ecc_curve((int)ECC_SECP256R1);
  3294. }
  3295. else if (bench_asym_algs & BENCH_ECC_P384) {
  3296. bench_ecc_curve((int)ECC_SECP384R1);
  3297. }
  3298. else if (bench_asym_algs & BENCH_ECC_P521) {
  3299. bench_ecc_curve((int)ECC_SECP521R1);
  3300. }
  3301. else {
  3302. #ifndef NO_ECC256
  3303. bench_ecc_curve((int)ECC_SECP256R1);
  3304. #elif defined(HAVE_ECC384)
  3305. bench_ecc_curve((int)ECC_SECP384R1);
  3306. #elif defined(HAVE_ECC521)
  3307. bench_ecc_curve((int)ECC_SECP521R1);
  3308. #endif
  3309. #ifdef HAVE_ECC_BRAINPOOL
  3310. bench_ecc_curve((int)ECC_BRAINPOOLP256R1);
  3311. #endif
  3312. }
  3313. }
  3314. #endif
  3315. #ifdef WOLFSSL_SM2
  3316. if (bench_all || (bench_asym_algs & BENCH_SM2)) {
  3317. bench_sm2(0);
  3318. }
  3319. #endif
  3320. #ifdef HAVE_CURVE25519
  3321. if (bench_all || (bench_asym_algs & BENCH_CURVE25519_KEYGEN)) {
  3322. bench_curve25519KeyGen(0);
  3323. #ifdef BENCH_DEVID
  3324. bench_curve25519KeyGen(1);
  3325. #endif
  3326. }
  3327. #ifdef HAVE_CURVE25519_SHARED_SECRET
  3328. if (bench_all || (bench_asym_algs & BENCH_CURVE25519_KA)) {
  3329. bench_curve25519KeyAgree(0);
  3330. #ifdef BENCH_DEVID
  3331. bench_curve25519KeyAgree(1);
  3332. #endif
  3333. }
  3334. #endif
  3335. #endif
  3336. #ifdef HAVE_ED25519
  3337. if (bench_all || (bench_asym_algs & BENCH_ED25519_KEYGEN))
  3338. bench_ed25519KeyGen();
  3339. if (bench_all || (bench_asym_algs & BENCH_ED25519_SIGN))
  3340. bench_ed25519KeySign();
  3341. #endif
  3342. #ifdef HAVE_CURVE448
  3343. if (bench_all || (bench_asym_algs & BENCH_CURVE448_KEYGEN))
  3344. bench_curve448KeyGen();
  3345. #ifdef HAVE_CURVE448_SHARED_SECRET
  3346. if (bench_all || (bench_asym_algs & BENCH_CURVE448_KA))
  3347. bench_curve448KeyAgree();
  3348. #endif
  3349. #endif
  3350. #ifdef HAVE_ED448
  3351. if (bench_all || (bench_asym_algs & BENCH_ED448_KEYGEN))
  3352. bench_ed448KeyGen();
  3353. if (bench_all || (bench_asym_algs & BENCH_ED448_SIGN))
  3354. bench_ed448KeySign();
  3355. #endif
  3356. #ifdef WOLFCRYPT_HAVE_ECCSI
  3357. #ifdef WOLFCRYPT_ECCSI_KMS
  3358. if (bench_all || (bench_asym_algs & BENCH_ECCSI_KEYGEN)) {
  3359. bench_eccsiKeyGen();
  3360. }
  3361. if (bench_all || (bench_asym_algs & BENCH_ECCSI_PAIRGEN)) {
  3362. bench_eccsiPairGen();
  3363. }
  3364. #endif
  3365. #ifdef WOLFCRYPT_ECCSI_CLIENT
  3366. if (bench_all || (bench_asym_algs & BENCH_ECCSI_VALIDATE)) {
  3367. bench_eccsiValidate();
  3368. }
  3369. if (bench_all || (bench_asym_algs & BENCH_ECCSI)) {
  3370. bench_eccsi();
  3371. }
  3372. #endif
  3373. #endif
  3374. #ifdef WOLFCRYPT_HAVE_SAKKE
  3375. #ifdef WOLFCRYPT_SAKKE_KMS
  3376. if (bench_all || (bench_asym_algs & BENCH_SAKKE_KEYGEN)) {
  3377. bench_sakkeKeyGen();
  3378. }
  3379. if (bench_all || (bench_asym_algs & BENCH_SAKKE_RSKGEN)) {
  3380. bench_sakkeRskGen();
  3381. }
  3382. #endif
  3383. #ifdef WOLFCRYPT_SAKKE_CLIENT
  3384. if (bench_all || (bench_asym_algs & BENCH_SAKKE_VALIDATE)) {
  3385. bench_sakkeValidate();
  3386. }
  3387. if (bench_all || (bench_asym_algs & BENCH_SAKKE)) {
  3388. bench_sakke();
  3389. }
  3390. #endif
  3391. #endif
  3392. #if defined(HAVE_LIBOQS)
  3393. #ifdef HAVE_FALCON
  3394. if (bench_all || (bench_pq_asym_algs & BENCH_FALCON_LEVEL1_SIGN))
  3395. bench_falconKeySign(1);
  3396. if (bench_all || (bench_pq_asym_algs & BENCH_FALCON_LEVEL5_SIGN))
  3397. bench_falconKeySign(5);
  3398. #endif
  3399. #ifdef HAVE_DILITHIUM
  3400. if (bench_all || (bench_pq_asym_algs & BENCH_DILITHIUM_LEVEL2_SIGN))
  3401. bench_dilithiumKeySign(2);
  3402. if (bench_all || (bench_pq_asym_algs & BENCH_DILITHIUM_LEVEL3_SIGN))
  3403. bench_dilithiumKeySign(3);
  3404. if (bench_all || (bench_pq_asym_algs & BENCH_DILITHIUM_LEVEL5_SIGN))
  3405. bench_dilithiumKeySign(5);
  3406. #endif
  3407. #ifdef HAVE_SPHINCS
  3408. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_FAST_LEVEL1_SIGN))
  3409. bench_sphincsKeySign(1, FAST_VARIANT);
  3410. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_FAST_LEVEL3_SIGN))
  3411. bench_sphincsKeySign(3, FAST_VARIANT);
  3412. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_FAST_LEVEL5_SIGN))
  3413. bench_sphincsKeySign(5, FAST_VARIANT);
  3414. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_SMALL_LEVEL1_SIGN))
  3415. bench_sphincsKeySign(1, SMALL_VARIANT);
  3416. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_SMALL_LEVEL3_SIGN))
  3417. bench_sphincsKeySign(3, SMALL_VARIANT);
  3418. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_SMALL_LEVEL5_SIGN))
  3419. bench_sphincsKeySign(5, SMALL_VARIANT);
  3420. #endif
  3421. #endif /* HAVE_LIBOQS */
  3422. exit:
  3423. /* free benchmark buffers */
  3424. XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  3425. XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  3426. #ifdef WOLFSSL_ASYNC_CRYPT
  3427. XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  3428. XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  3429. #endif
  3430. #if defined(HAVE_LOCAL_RNG)
  3431. wc_FreeRng(&gRng);
  3432. #endif
  3433. /* cleanup the thread if fixed point cache is enabled and have thread local */
  3434. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  3435. wc_ecc_fp_free();
  3436. #endif
  3437. (void)bench_cipher_algs;
  3438. (void)bench_digest_algs;
  3439. (void)bench_mac_algs;
  3440. (void)bench_asym_algs;
  3441. (void)bench_other_algs;
  3442. (void)bench_pq_asym_algs;
  3443. (void)bench_pq_asym_algs2;
  3444. return NULL;
  3445. }
  3446. int benchmark_init(void)
  3447. {
  3448. int ret = 0;
  3449. benchmark_static_init(0);
  3450. #ifdef WOLFSSL_STATIC_MEMORY
  3451. ret = wc_LoadStaticMemory(&HEAP_HINT, gBenchMemory,
  3452. sizeof(gBenchMemory), WOLFMEM_GENERAL, 1);
  3453. if (ret != 0) {
  3454. printf("%sunable to load static memory %d\n", err_prefix, ret);
  3455. }
  3456. #endif /* WOLFSSL_STATIC_MEMORY */
  3457. if ((ret = wolfCrypt_Init()) != 0) {
  3458. printf("%swolfCrypt_Init failed %d\n", err_prefix, ret);
  3459. return EXIT_FAILURE;
  3460. }
  3461. #ifdef HAVE_WC_INTROSPECTION
  3462. printf("Math: %s\n", wc_GetMathInfo());
  3463. #endif
  3464. #ifdef WOLFSSL_SECO_CAAM
  3465. if (wc_SECO_OpenHSM(SECO_KEY_STORE_ID,
  3466. SECO_BENCHMARK_NONCE, SECO_MAX_UPDATES, CAAM_KEYSTORE_CREATE)
  3467. != 0) {
  3468. printf("%sunable to open HSM\n", err_prefix);
  3469. wolfCrypt_Cleanup();
  3470. return EXIT_FAILURE;
  3471. }
  3472. #endif
  3473. #ifdef WC_RNG_SEED_CB
  3474. wc_SetSeed_Cb(wc_GenerateSeed);
  3475. #endif
  3476. bench_stats_init();
  3477. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  3478. wolfSSL_Debugging_ON();
  3479. #endif
  3480. printf("%swolfCrypt Benchmark (block bytes %d, min " FLT_FMT_PREC " sec each)\n",
  3481. info_prefix, (int)bench_size, FLT_FMT_PREC_ARGS(1, BENCH_MIN_RUNTIME_SEC));
  3482. #ifndef GENERATE_MACHINE_PARSEABLE_REPORT
  3483. if (csv_format == 1) {
  3484. printf("This format allows you to easily copy "
  3485. "the output to a csv file.");
  3486. }
  3487. #endif
  3488. #ifdef HAVE_WNR
  3489. ret = wc_InitNetRandom(wnrConfigFile, NULL, 5000);
  3490. if (ret != 0) {
  3491. printf("%sWhitewood netRandom config init failed %d\n",
  3492. err_prefix, ret);
  3493. }
  3494. #endif /* HAVE_WNR */
  3495. return ret;
  3496. }
  3497. int benchmark_free(void)
  3498. {
  3499. int ret;
  3500. #ifdef WC_BENCH_TRACK_STATS
  3501. if (gPrintStats || devId != INVALID_DEVID) {
  3502. bench_stats_print();
  3503. }
  3504. #endif
  3505. bench_stats_free();
  3506. #ifdef WOLF_CRYPTO_CB
  3507. #ifdef HAVE_INTEL_QA_SYNC
  3508. wc_CryptoCb_CleanupIntelQa(&devId);
  3509. #endif
  3510. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  3511. wc_CryptoCb_CleanupOcteon(&devId);
  3512. #endif
  3513. #ifdef HAVE_RENESAS_SYNC
  3514. wc_CryptoCb_CleanupRenesasCmn(&devId);
  3515. #endif
  3516. #endif
  3517. #ifdef WOLFSSL_ASYNC_CRYPT
  3518. /* free event queue */
  3519. wolfEventQueue_Free(&eventQueue);
  3520. /* close device */
  3521. wolfAsync_DevClose(&devId);
  3522. #endif
  3523. #ifdef HAVE_WNR
  3524. ret = wc_FreeNetRandom();
  3525. if (ret < 0) {
  3526. printf("%sFailed to free netRandom context %d\n", err_prefix, ret);
  3527. }
  3528. #endif
  3529. #ifdef WOLFSSL_SECO_CAAM
  3530. if (wc_SECO_CloseHSM() != 0) {
  3531. printf("%sError closing down the key store\n", err_prefix);
  3532. }
  3533. #endif
  3534. if ((ret = wolfCrypt_Cleanup()) != 0) {
  3535. printf("%serror %d with wolfCrypt_Cleanup\n", err_prefix, ret);
  3536. }
  3537. return ret;
  3538. }
  3539. #if defined(WC_ENABLE_BENCH_THREADING) && !defined(WOLFSSL_ASYNC_CRYPT)
  3540. static THREAD_RETURN WOLFSSL_THREAD run_bench(void* args)
  3541. {
  3542. benchmark_test(args);
  3543. EXIT_TEST(0);
  3544. }
  3545. static int benchmark_test_threaded(void* args)
  3546. {
  3547. int i;
  3548. printf("%sThreads: %d\n", info_prefix, g_threadCount);
  3549. g_threadData = (ThreadData*)XMALLOC(sizeof(ThreadData) * g_threadCount,
  3550. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3551. if (g_threadData == NULL) {
  3552. printf("%sThread data alloc failed!\n", err_prefix);
  3553. return EXIT_FAILURE;
  3554. }
  3555. for (i = 0; i < g_threadCount; i++) {
  3556. THREAD_CHECK_RET(pthread_create(&g_threadData[i].thread_id,
  3557. NULL, run_bench, args));
  3558. }
  3559. for (i = 0; i < g_threadCount; i++) {
  3560. THREAD_CHECK_RET(pthread_join(g_threadData[i].thread_id, 0));
  3561. }
  3562. printf("\n");
  3563. bench_stats_print();
  3564. return 0;
  3565. }
  3566. #endif
  3567. /* so embedded projects can pull in tests on their own */
  3568. #ifdef HAVE_STACK_SIZE
  3569. THREAD_RETURN WOLFSSL_THREAD benchmark_test(void* args)
  3570. #else
  3571. int benchmark_test(void *args)
  3572. #endif
  3573. {
  3574. int ret;
  3575. (void)args;
  3576. #ifdef HAVE_FIPS
  3577. wolfCrypt_SetCb_fips(myFipsCb);
  3578. #endif
  3579. ret = benchmark_init();
  3580. if (ret != 0)
  3581. EXIT_TEST(ret);
  3582. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  3583. {
  3584. /* See the documentation when turning on WOLFSSL_ASYNC_CRYPT
  3585. **
  3586. ** Chapter Two, Build Options:
  3587. **
  3588. ** https://www.wolfssl.com/documentation/manuals/wolfssl/wolfSSL-Manual.pdf
  3589. **
  3590. ** asynchronous cryptography using hardware based adapters such as
  3591. ** the Intel QuickAssist or Marvell (Cavium) Nitrox V.
  3592. */
  3593. int i;
  3594. if (g_threadCount == 0) {
  3595. #ifdef WC_ASYNC_BENCH_THREAD_COUNT
  3596. g_threadCount = WC_ASYNC_BENCH_THREAD_COUNT;
  3597. #else
  3598. g_threadCount = wc_AsyncGetNumberOfCpus();
  3599. if (g_threadCount > 0) {
  3600. g_threadCount /= 2; /* use physical core count */
  3601. }
  3602. #endif
  3603. }
  3604. if (g_threadCount <= 0) {
  3605. g_threadCount = 1;
  3606. }
  3607. printf("%sCPUs: %d\n", info_prefix, g_threadCount);
  3608. g_threadData = (ThreadData*)XMALLOC(sizeof(ThreadData) * g_threadCount,
  3609. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3610. if (g_threadData == NULL) {
  3611. printf("%sThread data alloc failed!\n", err_prefix);
  3612. EXIT_TEST(EXIT_FAILURE);
  3613. }
  3614. /* Create threads */
  3615. for (i = 0; i < g_threadCount; i++) {
  3616. ret = wc_AsyncThreadCreate(&g_threadData[i].thread_id,
  3617. benchmarks_do, &g_threadData[i]);
  3618. if (ret != 0) {
  3619. printf("%sError creating benchmark thread %d\n", err_prefix, ret);
  3620. EXIT_TEST(EXIT_FAILURE);
  3621. }
  3622. }
  3623. /* Start threads */
  3624. for (i = 0; i < g_threadCount; i++) {
  3625. wc_AsyncThreadJoin(&g_threadData[i].thread_id);
  3626. }
  3627. XFREE(g_threadData, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3628. }
  3629. #else
  3630. benchmarks_do(NULL);
  3631. #endif
  3632. SLEEP_ON_ERROR(1);
  3633. printf("%sBenchmark complete\n", info_prefix);
  3634. ret = benchmark_free();
  3635. EXIT_TEST(ret);
  3636. }
  3637. #ifndef WC_NO_RNG
  3638. void bench_rng(void)
  3639. {
  3640. int ret, i, count;
  3641. double start;
  3642. long pos, len, remain;
  3643. WC_RNG myrng;
  3644. DECLARE_MULTI_VALUE_STATS_VARS()
  3645. #ifndef HAVE_FIPS
  3646. ret = wc_InitRng_ex(&myrng, HEAP_HINT, devId);
  3647. #else
  3648. ret = wc_InitRng(&myrng);
  3649. #endif
  3650. if (ret < 0) {
  3651. printf("InitRNG failed %d\n", ret);
  3652. return;
  3653. }
  3654. bench_stats_start(&count, &start);
  3655. do {
  3656. for (i = 0; i < numBlocks; i++) {
  3657. /* Split request to handle large RNG request */
  3658. pos = 0;
  3659. remain = (int)bench_size;
  3660. while (remain > 0) {
  3661. len = remain;
  3662. if (len > RNG_MAX_BLOCK_LEN)
  3663. len = RNG_MAX_BLOCK_LEN;
  3664. ret = wc_RNG_GenerateBlock(&myrng, &bench_plain[pos],
  3665. (word32)len);
  3666. if (ret < 0)
  3667. goto exit_rng;
  3668. remain -= len;
  3669. pos += len;
  3670. }
  3671. RECORD_MULTI_VALUE_STATS();
  3672. }
  3673. count += i;
  3674. } while (bench_stats_check(start)
  3675. #ifdef MULTI_VALUE_STATISTICS
  3676. || runs < minimum_runs
  3677. #endif
  3678. );
  3679. exit_rng:
  3680. bench_stats_sym_finish("RNG", 0, count, bench_size, start, ret);
  3681. #ifdef MULTI_VALUE_STATISTICS
  3682. bench_multi_value_stats(max, min, sum, squareSum, runs);
  3683. #endif
  3684. wc_FreeRng(&myrng);
  3685. }
  3686. #endif /* WC_NO_RNG */
  3687. #ifndef NO_AES
  3688. #ifdef HAVE_AES_CBC
  3689. static void bench_aescbc_internal(int useDeviceID,
  3690. const byte* key, word32 keySz,
  3691. const byte* iv, const char* encLabel,
  3692. const char* decLabel)
  3693. {
  3694. int ret = 0, i, count = 0, times, pending = 0;
  3695. WC_DECLARE_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  3696. sizeof(Aes), HEAP_HINT);
  3697. double start;
  3698. DECLARE_MULTI_VALUE_STATS_VARS()
  3699. WC_CALLOC_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  3700. sizeof(Aes), HEAP_HINT);
  3701. /* init keys */
  3702. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3703. if ((ret = wc_AesInit(enc[i], HEAP_HINT,
  3704. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  3705. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  3706. goto exit;
  3707. }
  3708. ret = wc_AesSetKey(enc[i], key, keySz, iv, AES_ENCRYPTION);
  3709. if (ret != 0) {
  3710. printf("AesSetKey failed, ret = %d\n", ret);
  3711. goto exit;
  3712. }
  3713. }
  3714. bench_stats_start(&count, &start);
  3715. do {
  3716. for (times = 0; times < numBlocks || pending > 0; ) {
  3717. bench_async_poll(&pending);
  3718. /* while free pending slots in queue, submit ops */
  3719. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3720. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  3721. &times, numBlocks, &pending)) {
  3722. ret = wc_AesCbcEncrypt(enc[i], bench_plain, bench_cipher,
  3723. bench_size);
  3724. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  3725. 0, &times, &pending)) {
  3726. goto exit_aes_enc;
  3727. }
  3728. }
  3729. } /* for i */
  3730. RECORD_MULTI_VALUE_STATS();
  3731. } /* for times */
  3732. count += times;
  3733. } while (bench_stats_check(start)
  3734. #ifdef MULTI_VALUE_STATISTICS
  3735. || runs < minimum_runs
  3736. #endif
  3737. );
  3738. exit_aes_enc:
  3739. bench_stats_sym_finish(encLabel, useDeviceID, count,
  3740. bench_size, start, ret);
  3741. #ifdef MULTI_VALUE_STATISTICS
  3742. bench_multi_value_stats(max, min, sum, squareSum, runs);
  3743. #endif
  3744. if (ret < 0) {
  3745. goto exit;
  3746. }
  3747. #ifdef HAVE_AES_DECRYPT
  3748. /* init keys */
  3749. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3750. ret = wc_AesSetKey(enc[i], key, keySz, iv, AES_DECRYPTION);
  3751. if (ret != 0) {
  3752. printf("AesSetKey failed, ret = %d\n", ret);
  3753. goto exit;
  3754. }
  3755. }
  3756. RESET_MULTI_VALUE_STATS_VARS();
  3757. bench_stats_start(&count, &start);
  3758. do {
  3759. for (times = 0; times < numBlocks || pending > 0; ) {
  3760. bench_async_poll(&pending);
  3761. /* while free pending slots in queue, submit ops */
  3762. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3763. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  3764. &times, numBlocks, &pending)) {
  3765. ret = wc_AesCbcDecrypt(enc[i], bench_cipher, bench_plain,
  3766. bench_size);
  3767. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  3768. 0, &times, &pending)) {
  3769. goto exit_aes_dec;
  3770. }
  3771. }
  3772. } /* for i */
  3773. RECORD_MULTI_VALUE_STATS();
  3774. } /* for times */
  3775. count += times;
  3776. } while (bench_stats_check(start)
  3777. #ifdef MULTI_VALUE_STATISTICS
  3778. || runs < minimum_runs
  3779. #endif
  3780. );
  3781. exit_aes_dec:
  3782. bench_stats_sym_finish(decLabel, useDeviceID, count, bench_size,
  3783. start, ret);
  3784. #ifdef MULTI_VALUE_STATISTICS
  3785. bench_multi_value_stats(max, min, sum, squareSum, runs);
  3786. #endif
  3787. #endif /* HAVE_AES_DECRYPT */
  3788. (void)decLabel;
  3789. exit:
  3790. if (WC_ARRAY_OK(enc)) {
  3791. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3792. wc_AesFree(enc[i]);
  3793. }
  3794. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  3795. }
  3796. }
  3797. void bench_aescbc(int useDeviceID)
  3798. {
  3799. #ifdef WOLFSSL_AES_128
  3800. #ifdef HAVE_RENESAS_SYNC
  3801. bench_aescbc_internal(useDeviceID, bench_key1, 16, bench_iv,
  3802. "AES-128-CBC-enc", "AES-128-CBC-dec");
  3803. #else
  3804. bench_aescbc_internal(useDeviceID, bench_key, 16, bench_iv,
  3805. "AES-128-CBC-enc", "AES-128-CBC-dec");
  3806. #endif
  3807. #endif
  3808. #ifdef WOLFSSL_AES_192
  3809. bench_aescbc_internal(useDeviceID, bench_key, 24, bench_iv,
  3810. "AES-192-CBC-enc", "AES-192-CBC-dec");
  3811. #endif
  3812. #ifdef WOLFSSL_AES_256
  3813. #ifdef HAVE_RENESAS_SYNC
  3814. bench_aescbc_internal(useDeviceID, bench_key2, 32, bench_iv,
  3815. "AES-256-CBC-enc", "AES-256-CBC-dec");
  3816. #else
  3817. bench_aescbc_internal(useDeviceID, bench_key, 32, bench_iv,
  3818. "AES-256-CBC-enc", "AES-256-CBC-dec");
  3819. #endif
  3820. #endif
  3821. }
  3822. #endif /* HAVE_AES_CBC */
  3823. #ifdef HAVE_AESGCM
  3824. static void bench_aesgcm_internal(int useDeviceID,
  3825. const byte* key, word32 keySz,
  3826. const byte* iv, word32 ivSz,
  3827. const char* encLabel, const char* decLabel)
  3828. {
  3829. int ret = 0, i, count = 0, times, pending = 0;
  3830. WC_DECLARE_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  3831. sizeof(Aes), HEAP_HINT);
  3832. #ifdef HAVE_AES_DECRYPT
  3833. WC_DECLARE_ARRAY(dec, Aes, BENCH_MAX_PENDING,
  3834. sizeof(Aes), HEAP_HINT);
  3835. #endif
  3836. double start;
  3837. DECLARE_MULTI_VALUE_STATS_VARS()
  3838. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  3839. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  3840. WC_ALLOC_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  3841. WC_ALLOC_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  3842. WC_CALLOC_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  3843. sizeof(Aes), HEAP_HINT);
  3844. #ifdef HAVE_AES_DECRYPT
  3845. WC_CALLOC_ARRAY(dec, Aes, BENCH_MAX_PENDING,
  3846. sizeof(Aes), HEAP_HINT);
  3847. #endif
  3848. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  3849. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  3850. /* init keys */
  3851. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3852. if ((ret = wc_AesInit(enc[i], HEAP_HINT,
  3853. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  3854. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  3855. goto exit;
  3856. }
  3857. ret = wc_AesGcmSetKey(enc[i], key, keySz);
  3858. if (ret != 0) {
  3859. printf("AesGcmSetKey failed, ret = %d\n", ret);
  3860. goto exit;
  3861. }
  3862. }
  3863. /* GCM uses same routine in backend for both encrypt and decrypt */
  3864. bench_stats_start(&count, &start);
  3865. do {
  3866. for (times = 0; times < numBlocks || pending > 0; ) {
  3867. bench_async_poll(&pending);
  3868. /* while free pending slots in queue, submit ops */
  3869. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3870. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  3871. &times, numBlocks, &pending)) {
  3872. ret = wc_AesGcmEncrypt(enc[i], bench_cipher,
  3873. bench_plain, bench_size,
  3874. iv, ivSz, bench_tag, AES_AUTH_TAG_SZ,
  3875. bench_additional, aesAuthAddSz);
  3876. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  3877. 0, &times, &pending)) {
  3878. goto exit_aes_gcm;
  3879. }
  3880. }
  3881. } /* for i */
  3882. RECORD_MULTI_VALUE_STATS();
  3883. } /* for times */
  3884. count += times;
  3885. } while (bench_stats_check(start)
  3886. #ifdef MULTI_VALUE_STATISTICS
  3887. || runs < minimum_runs
  3888. #endif
  3889. );
  3890. exit_aes_gcm:
  3891. bench_stats_sym_finish(encLabel, useDeviceID, count, bench_size,
  3892. start, ret);
  3893. #ifdef MULTI_VALUE_STATISTICS
  3894. bench_multi_value_stats(max, min, sum, squareSum, runs);
  3895. #endif
  3896. #ifdef HAVE_AES_DECRYPT
  3897. RESET_MULTI_VALUE_STATS_VARS();
  3898. /* init keys */
  3899. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3900. if ((ret = wc_AesInit(dec[i], HEAP_HINT,
  3901. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  3902. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  3903. goto exit;
  3904. }
  3905. ret = wc_AesGcmSetKey(dec[i], key, keySz);
  3906. if (ret != 0) {
  3907. printf("AesGcmSetKey failed, ret = %d\n", ret);
  3908. goto exit;
  3909. }
  3910. }
  3911. bench_stats_start(&count, &start);
  3912. do {
  3913. for (times = 0; times < numBlocks || pending > 0; ) {
  3914. bench_async_poll(&pending);
  3915. /* while free pending slots in queue, submit ops */
  3916. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3917. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(dec[i]), 0,
  3918. &times, numBlocks, &pending)) {
  3919. ret = wc_AesGcmDecrypt(dec[i], bench_plain,
  3920. bench_cipher, bench_size,
  3921. iv, ivSz, bench_tag, AES_AUTH_TAG_SZ,
  3922. bench_additional, aesAuthAddSz);
  3923. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(dec[i]),
  3924. 0, &times, &pending)) {
  3925. goto exit_aes_gcm_dec;
  3926. }
  3927. }
  3928. } /* for i */
  3929. RECORD_MULTI_VALUE_STATS();
  3930. } /* for times */
  3931. count += times;
  3932. } while (bench_stats_check(start)
  3933. #ifdef MULTI_VALUE_STATISTICS
  3934. || runs < minimum_runs
  3935. #endif
  3936. );
  3937. exit_aes_gcm_dec:
  3938. bench_stats_sym_finish(decLabel, useDeviceID, count, bench_size,
  3939. start, ret);
  3940. #ifdef MULTI_VALUE_STATISTICS
  3941. bench_multi_value_stats(max, min, sum, squareSum, runs);
  3942. #endif
  3943. #endif /* HAVE_AES_DECRYPT */
  3944. (void)decLabel;
  3945. exit:
  3946. if (ret < 0) {
  3947. printf("bench_aesgcm failed: %d\n", ret);
  3948. }
  3949. #ifdef HAVE_AES_DECRYPT
  3950. if (WC_ARRAY_OK(dec)) {
  3951. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3952. wc_AesFree(dec[i]);
  3953. }
  3954. WC_FREE_ARRAY(dec, BENCH_MAX_PENDING, HEAP_HINT);
  3955. }
  3956. #endif
  3957. if (WC_ARRAY_OK(enc)) {
  3958. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3959. wc_AesFree(enc[i]);
  3960. }
  3961. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  3962. }
  3963. WC_FREE_VAR(bench_additional, HEAP_HINT);
  3964. WC_FREE_VAR(bench_tag, HEAP_HINT);
  3965. }
  3966. #ifdef WOLFSSL_AESGCM_STREAM
  3967. static void bench_aesgcm_stream_internal(int useDeviceID,
  3968. const byte* key, word32 keySz, const byte* iv, word32 ivSz,
  3969. const char* encLabel, const char* decLabel)
  3970. {
  3971. int ret = 0, i, count = 0, times, pending = 0;
  3972. WC_DECLARE_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  3973. sizeof(Aes), HEAP_HINT);
  3974. #ifdef HAVE_AES_DECRYPT
  3975. WC_DECLARE_ARRAY(dec, Aes, BENCH_MAX_PENDING,
  3976. sizeof(Aes), HEAP_HINT);
  3977. #endif
  3978. double start;
  3979. DECLARE_MULTI_VALUE_STATS_VARS()
  3980. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  3981. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  3982. WC_ALLOC_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  3983. WC_ALLOC_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  3984. WC_CALLOC_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  3985. sizeof(Aes), HEAP_HINT);
  3986. #ifdef HAVE_AES_DECRYPT
  3987. WC_CALLOC_ARRAY(dec, Aes, BENCH_MAX_PENDING,
  3988. sizeof(Aes), HEAP_HINT);
  3989. #endif
  3990. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  3991. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  3992. /* init keys */
  3993. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3994. if ((ret = wc_AesInit(enc[i], HEAP_HINT,
  3995. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  3996. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  3997. goto exit;
  3998. }
  3999. ret = wc_AesGcmSetKey(enc[i], key, keySz);
  4000. if (ret != 0) {
  4001. printf("AesGcmSetKey failed, ret = %d\n", ret);
  4002. goto exit;
  4003. }
  4004. }
  4005. /* GCM uses same routine in backend for both encrypt and decrypt */
  4006. bench_stats_start(&count, &start);
  4007. do {
  4008. for (times = 0; times < numBlocks || pending > 0; ) {
  4009. bench_async_poll(&pending);
  4010. /* while free pending slots in queue, submit ops */
  4011. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4012. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  4013. &times, numBlocks, &pending)) {
  4014. ret = wc_AesGcmEncryptInit(enc[i], NULL, 0, iv, ivSz);
  4015. if (ret == 0) {
  4016. ret = wc_AesGcmEncryptUpdate(enc[i], bench_cipher,
  4017. bench_plain, bench_size, bench_additional,
  4018. aesAuthAddSz);
  4019. }
  4020. if (ret == 0) {
  4021. ret = wc_AesGcmEncryptFinal(enc[i], bench_tag,
  4022. AES_AUTH_TAG_SZ);
  4023. }
  4024. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  4025. 0, &times, &pending)) {
  4026. goto exit_aes_gcm;
  4027. }
  4028. }
  4029. } /* for i */
  4030. RECORD_MULTI_VALUE_STATS();
  4031. } /* for times */
  4032. count += times;
  4033. } while (bench_stats_check(start)
  4034. #ifdef MULTI_VALUE_STATISTICS
  4035. || runs < minimum_runs
  4036. #endif
  4037. );
  4038. exit_aes_gcm:
  4039. bench_stats_sym_finish(encLabel, useDeviceID, count, bench_size,
  4040. start, ret);
  4041. #ifdef MULTI_VALUE_STATISTICS
  4042. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4043. #endif
  4044. #ifdef HAVE_AES_DECRYPT
  4045. /* init keys */
  4046. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4047. if ((ret = wc_AesInit(dec[i], HEAP_HINT,
  4048. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  4049. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  4050. goto exit;
  4051. }
  4052. ret = wc_AesGcmSetKey(dec[i], key, keySz);
  4053. if (ret != 0) {
  4054. printf("AesGcmSetKey failed, ret = %d\n", ret);
  4055. goto exit;
  4056. }
  4057. }
  4058. RESET_MULTI_VALUE_STATS_VARS();
  4059. bench_stats_start(&count, &start);
  4060. do {
  4061. for (times = 0; times < numBlocks || pending > 0; ) {
  4062. bench_async_poll(&pending);
  4063. /* while free pending slots in queue, submit ops */
  4064. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4065. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(dec[i]), 0,
  4066. &times, numBlocks, &pending)) {
  4067. ret = wc_AesGcmDecryptInit(enc[i], NULL, 0, iv, ivSz);
  4068. if (ret == 0) {
  4069. ret = wc_AesGcmDecryptUpdate(enc[i], bench_plain,
  4070. bench_cipher, bench_size, bench_additional,
  4071. aesAuthAddSz);
  4072. }
  4073. if (ret == 0) {
  4074. ret = wc_AesGcmDecryptFinal(enc[i], bench_tag,
  4075. AES_AUTH_TAG_SZ);
  4076. }
  4077. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(dec[i]),
  4078. 0, &times, &pending)) {
  4079. goto exit_aes_gcm_dec;
  4080. }
  4081. }
  4082. } /* for i */
  4083. RECORD_MULTI_VALUE_STATS();
  4084. } /* for times */
  4085. count += times;
  4086. } while (bench_stats_check(start)
  4087. #ifdef MULTI_VALUE_STATISTICS
  4088. || runs < minimum_runs
  4089. #endif
  4090. );
  4091. exit_aes_gcm_dec:
  4092. bench_stats_sym_finish(decLabel, useDeviceID, count, bench_size,
  4093. start, ret);
  4094. #ifdef MULTI_VALUE_STATISTICS
  4095. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4096. #endif
  4097. #endif /* HAVE_AES_DECRYPT */
  4098. (void)decLabel;
  4099. exit:
  4100. if (ret < 0) {
  4101. printf("bench_aesgcm failed: %d\n", ret);
  4102. }
  4103. #ifdef HAVE_AES_DECRYPT
  4104. if (WC_ARRAY_OK(dec)) {
  4105. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4106. wc_AesFree(dec[i]);
  4107. }
  4108. WC_FREE_ARRAY(dec, BENCH_MAX_PENDING, HEAP_HINT);
  4109. }
  4110. #endif
  4111. if (WC_ARRAY_OK(enc)) {
  4112. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4113. wc_AesFree(enc[i]);
  4114. }
  4115. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  4116. }
  4117. WC_FREE_VAR(bench_additional, HEAP_HINT);
  4118. WC_FREE_VAR(bench_tag, HEAP_HINT);
  4119. }
  4120. #endif
  4121. void bench_aesgcm(int useDeviceID)
  4122. {
  4123. #define AES_GCM_STRING(n, dir) AES_AAD_STRING("AES-" #n "-GCM-" #dir)
  4124. #if defined(WOLFSSL_AES_128) && !defined(WOLFSSL_AFALG_XILINX_AES) \
  4125. && !defined(WOLFSSL_XILINX_CRYPT) \
  4126. || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  4127. #ifdef HAVE_RENESAS_SYNC
  4128. bench_aesgcm_internal(useDeviceID, bench_key1, 16, bench_iv, 12,
  4129. AES_GCM_STRING(128, enc), AES_GCM_STRING(128, dec));
  4130. #else
  4131. bench_aesgcm_internal(useDeviceID, bench_key, 16, bench_iv, 12,
  4132. AES_GCM_STRING(128, enc), AES_GCM_STRING(128, dec));
  4133. #endif
  4134. #endif
  4135. #if defined(WOLFSSL_AES_192) && !defined(WOLFSSL_AFALG_XILINX_AES) \
  4136. && !defined(WOLFSSL_XILINX_CRYPT)
  4137. bench_aesgcm_internal(useDeviceID, bench_key, 24, bench_iv, 12,
  4138. AES_GCM_STRING(192, enc), AES_GCM_STRING(192, dec));
  4139. #endif
  4140. #ifdef WOLFSSL_AES_256
  4141. #ifdef HAVE_RENESAS_SYNC
  4142. bench_aesgcm_internal(useDeviceID, bench_key2, 32, bench_iv, 12,
  4143. AES_GCM_STRING(256, enc), AES_GCM_STRING(256, dec));
  4144. #else
  4145. bench_aesgcm_internal(useDeviceID, bench_key, 32, bench_iv, 12,
  4146. AES_GCM_STRING(256, enc), AES_GCM_STRING(256, dec));
  4147. #endif
  4148. #endif
  4149. #ifdef WOLFSSL_AESGCM_STREAM
  4150. #undef AES_GCM_STRING
  4151. #define AES_GCM_STRING(n, dir) AES_AAD_STRING("AES-" #n "-GCM-STREAM-" #dir)
  4152. #if defined(WOLFSSL_AES_128) && !defined(WOLFSSL_AFALG_XILINX_AES) \
  4153. && !defined(WOLFSSL_XILINX_CRYPT) \
  4154. || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  4155. bench_aesgcm_stream_internal(useDeviceID, bench_key, 16, bench_iv, 12,
  4156. AES_GCM_STRING(128, enc), AES_GCM_STRING(128, dec));
  4157. #endif
  4158. #if defined(WOLFSSL_AES_192) && !defined(WOLFSSL_AFALG_XILINX_AES) \
  4159. && !defined(WOLFSSL_XILINX_CRYPT)
  4160. bench_aesgcm_stream_internal(useDeviceID, bench_key, 24, bench_iv, 12,
  4161. AES_GCM_STRING(192, enc), AES_GCM_STRING(192, dec));
  4162. #endif
  4163. #ifdef WOLFSSL_AES_256
  4164. bench_aesgcm_stream_internal(useDeviceID, bench_key, 32, bench_iv, 12,
  4165. AES_GCM_STRING(256, enc), AES_GCM_STRING(256, dec));
  4166. #endif
  4167. #endif /* WOLFSSL_AESGCM_STREAM */
  4168. #undef AES_GCM_STRING
  4169. }
  4170. /* GMAC */
  4171. void bench_gmac(int useDeviceID)
  4172. {
  4173. int ret, count = 0;
  4174. Gmac gmac;
  4175. double start;
  4176. byte tag[AES_AUTH_TAG_SZ];
  4177. DECLARE_MULTI_VALUE_STATS_VARS()
  4178. /* determine GCM GHASH method */
  4179. #ifdef GCM_SMALL
  4180. const char* gmacStr = "GMAC Small";
  4181. #elif defined(GCM_TABLE)
  4182. const char* gmacStr = "GMAC Table";
  4183. #elif defined(GCM_TABLE_4BIT)
  4184. const char* gmacStr = "GMAC Table 4-bit";
  4185. #elif defined(GCM_WORD32)
  4186. const char* gmacStr = "GMAC Word32";
  4187. #else
  4188. const char* gmacStr = "GMAC Default";
  4189. #endif
  4190. /* init keys */
  4191. XMEMSET(bench_plain, 0, bench_size);
  4192. XMEMSET(tag, 0, sizeof(tag));
  4193. XMEMSET(&gmac, 0, sizeof(Gmac)); /* clear context */
  4194. (void)wc_AesInit((Aes*)&gmac, HEAP_HINT,
  4195. useDeviceID ? devId: INVALID_DEVID);
  4196. #ifdef HAVE_RENESAS_SYNC
  4197. wc_GmacSetKey(&gmac, bench_key1, 16);
  4198. #else
  4199. wc_GmacSetKey(&gmac, bench_key, 16);
  4200. #endif
  4201. bench_stats_start(&count, &start);
  4202. do {
  4203. ret = wc_GmacUpdate(&gmac, bench_iv, 12, bench_plain, bench_size,
  4204. tag, sizeof(tag));
  4205. count++;
  4206. RECORD_MULTI_VALUE_STATS();
  4207. } while (bench_stats_check(start)
  4208. #ifdef MULTI_VALUE_STATISTICS
  4209. || runs < minimum_runs
  4210. #endif
  4211. );
  4212. wc_AesFree((Aes*)&gmac);
  4213. bench_stats_sym_finish(gmacStr, 0, count, bench_size, start, ret);
  4214. #ifdef MULTI_VALUE_STATISTICS
  4215. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4216. #endif
  4217. }
  4218. #endif /* HAVE_AESGCM */
  4219. #ifdef HAVE_AES_ECB
  4220. static void bench_aesecb_internal(int useDeviceID,
  4221. const byte* key, word32 keySz,
  4222. const char* encLabel, const char* decLabel)
  4223. {
  4224. int ret = 0, i, count = 0, times, pending = 0;
  4225. WC_DECLARE_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  4226. sizeof(Aes), HEAP_HINT);
  4227. double start;
  4228. DECLARE_MULTI_VALUE_STATS_VARS()
  4229. #ifdef HAVE_FIPS
  4230. const int benchSz = AES_BLOCK_SIZE;
  4231. #else
  4232. const int benchSz = (int)bench_size;
  4233. #endif
  4234. WC_CALLOC_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  4235. sizeof(Aes), HEAP_HINT);
  4236. /* init keys */
  4237. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4238. if ((ret = wc_AesInit(enc[i], HEAP_HINT,
  4239. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  4240. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  4241. goto exit;
  4242. }
  4243. ret = wc_AesSetKey(enc[i], key, keySz, bench_iv, AES_ENCRYPTION);
  4244. if (ret != 0) {
  4245. printf("AesSetKey failed, ret = %d\n", ret);
  4246. goto exit;
  4247. }
  4248. }
  4249. bench_stats_start(&count, &start);
  4250. do {
  4251. int outer_loop_limit = (((int)bench_size / benchSz) * 10) + 1;
  4252. for (times = 0;
  4253. times < outer_loop_limit /* numBlocks */ || pending > 0;
  4254. ) {
  4255. bench_async_poll(&pending);
  4256. /* while free pending slots in queue, submit ops */
  4257. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4258. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  4259. &times, outer_loop_limit, &pending)) {
  4260. #ifdef HAVE_FIPS
  4261. wc_AesEncryptDirect(enc[i], bench_cipher, bench_plain);
  4262. #else
  4263. wc_AesEcbEncrypt(enc[i], bench_cipher, bench_plain,
  4264. benchSz);
  4265. #endif
  4266. ret = 0;
  4267. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  4268. 0, &times, &pending)) {
  4269. goto exit_aes_enc;
  4270. }
  4271. }
  4272. } /* for i */
  4273. RECORD_MULTI_VALUE_STATS();
  4274. } /* for times */
  4275. count += times;
  4276. } while (bench_stats_check(start)
  4277. #ifdef MULTI_VALUE_STATISTICS
  4278. || runs < minimum_runs
  4279. #endif
  4280. );
  4281. exit_aes_enc:
  4282. bench_stats_sym_finish(encLabel, useDeviceID, count, benchSz,
  4283. start, ret);
  4284. #ifdef MULTI_VALUE_STATISTICS
  4285. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4286. #endif
  4287. #ifdef HAVE_AES_DECRYPT
  4288. /* init keys */
  4289. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4290. ret = wc_AesSetKey(enc[i], key, keySz, bench_iv, AES_DECRYPTION);
  4291. if (ret != 0) {
  4292. printf("AesSetKey failed, ret = %d\n", ret);
  4293. goto exit;
  4294. }
  4295. }
  4296. RESET_MULTI_VALUE_STATS_VARS();
  4297. bench_stats_start(&count, &start);
  4298. do {
  4299. int outer_loop_limit = (10 * ((int)bench_size / benchSz)) + 1;
  4300. for (times = 0; times < outer_loop_limit || pending > 0; ) {
  4301. bench_async_poll(&pending);
  4302. /* while free pending slots in queue, submit ops */
  4303. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4304. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  4305. &times, outer_loop_limit, &pending)) {
  4306. #ifdef HAVE_FIPS
  4307. wc_AesDecryptDirect(enc[i], bench_plain, bench_cipher);
  4308. #else
  4309. wc_AesEcbDecrypt(enc[i], bench_plain, bench_cipher,
  4310. benchSz);
  4311. #endif
  4312. ret = 0;
  4313. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  4314. 0, &times, &pending)) {
  4315. goto exit_aes_dec;
  4316. }
  4317. }
  4318. } /* for i */
  4319. RECORD_MULTI_VALUE_STATS();
  4320. } /* for times */
  4321. count += times;
  4322. } while (bench_stats_check(start)
  4323. #ifdef MULTI_VALUE_STATISTICS
  4324. || runs < minimum_runs
  4325. #endif
  4326. );
  4327. exit_aes_dec:
  4328. bench_stats_sym_finish(decLabel, useDeviceID, count, benchSz,
  4329. start, ret);
  4330. #ifdef MULTI_VALUE_STATISTICS
  4331. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4332. #endif
  4333. #endif /* HAVE_AES_DECRYPT */
  4334. exit:
  4335. if (WC_ARRAY_OK(enc)) {
  4336. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4337. wc_AesFree(enc[i]);
  4338. }
  4339. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  4340. }
  4341. }
  4342. void bench_aesecb(int useDeviceID)
  4343. {
  4344. #ifdef WOLFSSL_AES_128
  4345. bench_aesecb_internal(useDeviceID, bench_key, 16,
  4346. "AES-128-ECB-enc", "AES-128-ECB-dec");
  4347. #endif
  4348. #ifdef WOLFSSL_AES_192
  4349. bench_aesecb_internal(useDeviceID, bench_key, 24,
  4350. "AES-192-ECB-enc", "AES-192-ECB-dec");
  4351. #endif
  4352. #ifdef WOLFSSL_AES_256
  4353. bench_aesecb_internal(useDeviceID, bench_key, 32,
  4354. "AES-256-ECB-enc", "AES-256-ECB-dec");
  4355. #endif
  4356. }
  4357. #endif /* HAVE_AES_ECB */
  4358. #ifdef WOLFSSL_AES_CFB
  4359. static void bench_aescfb_internal(const byte* key,
  4360. word32 keySz, const byte* iv,
  4361. const char* label)
  4362. {
  4363. Aes enc;
  4364. double start;
  4365. int i, ret, count;
  4366. DECLARE_MULTI_VALUE_STATS_VARS()
  4367. ret = wc_AesInit(&enc, HEAP_HINT, INVALID_DEVID);
  4368. if (ret != 0) {
  4369. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  4370. return;
  4371. }
  4372. ret = wc_AesSetKey(&enc, key, keySz, iv, AES_ENCRYPTION);
  4373. if (ret != 0) {
  4374. printf("AesSetKey failed, ret = %d\n", ret);
  4375. goto out;
  4376. }
  4377. bench_stats_start(&count, &start);
  4378. do {
  4379. for (i = 0; i < numBlocks; i++) {
  4380. if((ret = wc_AesCfbEncrypt(&enc, bench_plain, bench_cipher,
  4381. bench_size)) != 0) {
  4382. printf("wc_AesCfbEncrypt failed, ret = %d\n", ret);
  4383. goto out;
  4384. }
  4385. RECORD_MULTI_VALUE_STATS();
  4386. }
  4387. count += i;
  4388. } while (bench_stats_check(start)
  4389. #ifdef MULTI_VALUE_STATISTICS
  4390. || runs < minimum_runs
  4391. #endif
  4392. );
  4393. bench_stats_sym_finish(label, 0, count, bench_size, start, ret);
  4394. #ifdef MULTI_VALUE_STATISTICS
  4395. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4396. #endif
  4397. out:
  4398. wc_AesFree(&enc);
  4399. return;
  4400. }
  4401. void bench_aescfb(void)
  4402. {
  4403. #ifdef WOLFSSL_AES_128
  4404. bench_aescfb_internal(bench_key, 16, bench_iv, "AES-128-CFB");
  4405. #endif
  4406. #ifdef WOLFSSL_AES_192
  4407. bench_aescfb_internal(bench_key, 24, bench_iv, "AES-192-CFB");
  4408. #endif
  4409. #ifdef WOLFSSL_AES_256
  4410. bench_aescfb_internal(bench_key, 32, bench_iv, "AES-256-CFB");
  4411. #endif
  4412. }
  4413. #endif /* WOLFSSL_AES_CFB */
  4414. #ifdef WOLFSSL_AES_OFB
  4415. static void bench_aesofb_internal(const byte* key,
  4416. word32 keySz, const byte* iv,
  4417. const char* label)
  4418. {
  4419. Aes enc;
  4420. double start;
  4421. int i, ret, count;
  4422. DECLARE_MULTI_VALUE_STATS_VARS()
  4423. ret = wc_AesInit(&enc, NULL, INVALID_DEVID);
  4424. if (ret != 0) {
  4425. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  4426. return;
  4427. }
  4428. ret = wc_AesSetKey(&enc, key, keySz, iv, AES_ENCRYPTION);
  4429. if (ret != 0) {
  4430. printf("AesSetKey failed, ret = %d\n", ret);
  4431. return;
  4432. }
  4433. bench_stats_start(&count, &start);
  4434. do {
  4435. for (i = 0; i < numBlocks; i++) {
  4436. if((ret = wc_AesOfbEncrypt(&enc, bench_plain, bench_cipher,
  4437. bench_size)) != 0) {
  4438. printf("wc_AesCfbEncrypt failed, ret = %d\n", ret);
  4439. return;
  4440. }
  4441. RECORD_MULTI_VALUE_STATS();
  4442. }
  4443. count += i;
  4444. } while (bench_stats_check(start)
  4445. #ifdef MULTI_VALUE_STATISTICS
  4446. || runs < minimum_runs
  4447. #endif
  4448. );
  4449. bench_stats_sym_finish(label, 0, count, bench_size, start, ret);
  4450. #ifdef MULTI_VALUE_STATISTICS
  4451. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4452. #endif
  4453. wc_AesFree(&enc);
  4454. }
  4455. void bench_aesofb(void)
  4456. {
  4457. #ifdef WOLFSSL_AES_128
  4458. bench_aesofb_internal(bench_key, 16, bench_iv, "AES-128-OFB");
  4459. #endif
  4460. #ifdef WOLFSSL_AES_192
  4461. bench_aesofb_internal(bench_key, 24, bench_iv, "AES-192-OFB");
  4462. #endif
  4463. #ifdef WOLFSSL_AES_256
  4464. bench_aesofb_internal(bench_key, 32, bench_iv, "AES-256-OFB");
  4465. #endif
  4466. }
  4467. #endif /* WOLFSSL_AES_CFB */
  4468. #ifdef WOLFSSL_AES_XTS
  4469. void bench_aesxts(void)
  4470. {
  4471. WC_DECLARE_VAR(aes, XtsAes, 1, HEAP_HINT);
  4472. double start;
  4473. int i, count, ret;
  4474. DECLARE_MULTI_VALUE_STATS_VARS()
  4475. static const unsigned char k1[] = {
  4476. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  4477. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  4478. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  4479. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  4480. };
  4481. static const unsigned char i1[] = {
  4482. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  4483. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  4484. };
  4485. WC_ALLOC_VAR(aes, XtsAes, 1, HEAP_HINT);
  4486. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  4487. HEAP_HINT, devId);
  4488. if (ret != 0) {
  4489. printf("wc_AesXtsSetKey failed, ret = %d\n", ret);
  4490. goto exit;
  4491. }
  4492. bench_stats_start(&count, &start);
  4493. do {
  4494. for (i = 0; i < numBlocks; i++) {
  4495. if ((ret = wc_AesXtsEncrypt(aes, bench_cipher, bench_plain,
  4496. bench_size, i1, sizeof(i1))) != 0) {
  4497. printf("wc_AesXtsEncrypt failed, ret = %d\n", ret);
  4498. goto exit;
  4499. }
  4500. RECORD_MULTI_VALUE_STATS();
  4501. }
  4502. count += i;
  4503. } while (bench_stats_check(start)
  4504. #ifdef MULTI_VALUE_STATISTICS
  4505. || runs < minimum_runs
  4506. #endif
  4507. );
  4508. bench_stats_sym_finish("AES-XTS-enc", 0, count, bench_size, start, ret);
  4509. #ifdef MULTI_VALUE_STATISTICS
  4510. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4511. #endif
  4512. wc_AesXtsFree(aes);
  4513. /* decryption benchmark */
  4514. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  4515. HEAP_HINT, devId);
  4516. if (ret != 0) {
  4517. printf("wc_AesXtsSetKey failed, ret = %d\n", ret);
  4518. goto exit;
  4519. }
  4520. RESET_MULTI_VALUE_STATS_VARS();
  4521. bench_stats_start(&count, &start);
  4522. do {
  4523. for (i = 0; i < numBlocks; i++) {
  4524. if ((ret = wc_AesXtsDecrypt(aes, bench_plain, bench_cipher,
  4525. bench_size, i1, sizeof(i1))) != 0) {
  4526. printf("wc_AesXtsDecrypt failed, ret = %d\n", ret);
  4527. goto exit;
  4528. }
  4529. RECORD_MULTI_VALUE_STATS();
  4530. }
  4531. count += i;
  4532. } while (bench_stats_check(start)
  4533. #ifdef MULTI_VALUE_STATISTICS
  4534. || runs < minimum_runs
  4535. #endif
  4536. );
  4537. bench_stats_sym_finish("AES-XTS-dec", 0, count, bench_size, start, ret);
  4538. #ifdef MULTI_VALUE_STATISTICS
  4539. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4540. #endif
  4541. exit:
  4542. wc_AesXtsFree(aes);
  4543. WC_FREE_VAR(aes, HEAP_HINT);
  4544. }
  4545. #endif /* WOLFSSL_AES_XTS */
  4546. #ifdef WOLFSSL_AES_COUNTER
  4547. static void bench_aesctr_internal(const byte* key, word32 keySz,
  4548. const byte* iv, const char* label,
  4549. int useDeviceID)
  4550. {
  4551. Aes enc;
  4552. double start;
  4553. int i, count, ret = 0;
  4554. DECLARE_MULTI_VALUE_STATS_VARS()
  4555. if ((ret = wc_AesInit(&enc, HEAP_HINT,
  4556. useDeviceID ? devId : INVALID_DEVID)) != 0) {
  4557. printf("wc_AesInit failed, ret = %d\n", ret);
  4558. }
  4559. if (wc_AesSetKeyDirect(&enc, key, keySz, iv, AES_ENCRYPTION) < 0) {
  4560. printf("wc_AesSetKeyDirect failed, ret = %d\n", ret);
  4561. return;
  4562. }
  4563. bench_stats_start(&count, &start);
  4564. do {
  4565. for (i = 0; i < numBlocks; i++) {
  4566. if((ret = wc_AesCtrEncrypt(&enc, bench_plain, bench_cipher,
  4567. bench_size)) != 0) {
  4568. printf("wc_AesCtrEncrypt failed, ret = %d\n", ret);
  4569. return;
  4570. }
  4571. RECORD_MULTI_VALUE_STATS();
  4572. }
  4573. count += i;
  4574. } while (bench_stats_check(start)
  4575. #ifdef MULTI_VALUE_STATISTICS
  4576. || runs < minimum_runs
  4577. #endif
  4578. );
  4579. bench_stats_sym_finish(label, useDeviceID, count, bench_size, start, ret);
  4580. #ifdef MULTI_VALUE_STATISTICS
  4581. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4582. #endif
  4583. wc_AesFree(&enc);
  4584. }
  4585. void bench_aesctr(int useDeviceID)
  4586. {
  4587. #ifdef WOLFSSL_AES_128
  4588. bench_aesctr_internal(bench_key, 16, bench_iv, "AES-128-CTR", useDeviceID);
  4589. #endif
  4590. #ifdef WOLFSSL_AES_192
  4591. bench_aesctr_internal(bench_key, 24, bench_iv, "AES-192-CTR", useDeviceID);
  4592. #endif
  4593. #ifdef WOLFSSL_AES_256
  4594. bench_aesctr_internal(bench_key, 32, bench_iv, "AES-256-CTR", useDeviceID);
  4595. #endif
  4596. }
  4597. #endif /* WOLFSSL_AES_COUNTER */
  4598. #ifdef HAVE_AESCCM
  4599. void bench_aesccm(int useDeviceID)
  4600. {
  4601. Aes enc;
  4602. int enc_inited = 0;
  4603. double start;
  4604. int ret, i, count;
  4605. DECLARE_MULTI_VALUE_STATS_VARS()
  4606. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4607. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4608. WC_ALLOC_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4609. WC_ALLOC_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4610. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  4611. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  4612. if ((ret = wc_AesInit(&enc, HEAP_HINT,
  4613. useDeviceID ? devId : INVALID_DEVID)) != 0)
  4614. {
  4615. printf("wc_AesInit failed, ret = %d\n", ret);
  4616. goto exit;
  4617. }
  4618. if ((ret = wc_AesCcmSetKey(&enc, bench_key, 16)) != 0) {
  4619. printf("wc_AesCcmSetKey failed, ret = %d\n", ret);
  4620. goto exit;
  4621. }
  4622. enc_inited = 1;
  4623. bench_stats_start(&count, &start);
  4624. do {
  4625. for (i = 0; i < numBlocks; i++) {
  4626. ret |= wc_AesCcmEncrypt(&enc, bench_cipher, bench_plain, bench_size,
  4627. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  4628. bench_additional, 0);
  4629. RECORD_MULTI_VALUE_STATS();
  4630. }
  4631. count += i;
  4632. } while (bench_stats_check(start)
  4633. #ifdef MULTI_VALUE_STATISTICS
  4634. || runs < minimum_runs
  4635. #endif
  4636. );
  4637. bench_stats_sym_finish(AES_AAD_STRING("AES-CCM-enc"), useDeviceID, count,
  4638. bench_size, start, ret);
  4639. #ifdef MULTI_VALUE_STATISTICS
  4640. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4641. #endif
  4642. if (ret != 0) {
  4643. printf("wc_AesCcmEncrypt failed, ret = %d\n", ret);
  4644. goto exit;
  4645. }
  4646. RESET_MULTI_VALUE_STATS_VARS();
  4647. bench_stats_start(&count, &start);
  4648. do {
  4649. for (i = 0; i < numBlocks; i++) {
  4650. ret |= wc_AesCcmDecrypt(&enc, bench_plain, bench_cipher, bench_size,
  4651. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  4652. bench_additional, 0);
  4653. RECORD_MULTI_VALUE_STATS();
  4654. }
  4655. count += i;
  4656. } while (bench_stats_check(start)
  4657. #ifdef MULTI_VALUE_STATISTICS
  4658. || runs < minimum_runs
  4659. #endif
  4660. );
  4661. bench_stats_sym_finish(AES_AAD_STRING("AES-CCM-dec"), useDeviceID, count,
  4662. bench_size, start, ret);
  4663. #ifdef MULTI_VALUE_STATISTICS
  4664. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4665. #endif
  4666. if (ret != 0) {
  4667. printf("wc_AesCcmEncrypt failed, ret = %d\n", ret);
  4668. goto exit;
  4669. }
  4670. exit:
  4671. if (enc_inited)
  4672. wc_AesFree(&enc);
  4673. WC_FREE_VAR(bench_additional, HEAP_HINT);
  4674. WC_FREE_VAR(bench_tag, HEAP_HINT);
  4675. }
  4676. #endif /* HAVE_AESCCM */
  4677. #ifdef WOLFSSL_AES_SIV
  4678. static void bench_aessiv_internal(const byte* key, word32 keySz, const char*
  4679. encLabel, const char* decLabel)
  4680. {
  4681. int i;
  4682. int ret = 0;
  4683. byte assoc[AES_BLOCK_SIZE];
  4684. byte nonce[AES_BLOCK_SIZE];
  4685. byte siv[AES_BLOCK_SIZE];
  4686. int count = 0;
  4687. double start;
  4688. DECLARE_MULTI_VALUE_STATS_VARS()
  4689. bench_stats_start(&count, &start);
  4690. do {
  4691. for (i = 0; i < numBlocks; i++) {
  4692. ret = wc_AesSivEncrypt(key, keySz, assoc, AES_BLOCK_SIZE, nonce,
  4693. AES_BLOCK_SIZE, bench_plain, bench_size,
  4694. siv, bench_cipher);
  4695. if (ret != 0) {
  4696. printf("wc_AesSivEncrypt failed (%d)\n", ret);
  4697. return;
  4698. }
  4699. RECORD_MULTI_VALUE_STATS();
  4700. }
  4701. count += i;
  4702. } while (bench_stats_check(start)
  4703. #ifdef MULTI_VALUE_STATISTICS
  4704. || runs < minimum_runs
  4705. #endif
  4706. );
  4707. bench_stats_sym_finish(encLabel, 0, count, bench_size, start, ret);
  4708. #ifdef MULTI_VALUE_STATISTICS
  4709. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4710. #endif
  4711. RESET_MULTI_VALUE_STATS_VARS();
  4712. bench_stats_start(&count, &start);
  4713. do {
  4714. for (i = 0; i < numBlocks; i++) {
  4715. ret = wc_AesSivDecrypt(key, keySz, assoc, AES_BLOCK_SIZE, nonce,
  4716. AES_BLOCK_SIZE, bench_cipher, bench_size,
  4717. siv, bench_plain);
  4718. if (ret != 0) {
  4719. printf("wc_AesSivDecrypt failed (%d)\n", ret);
  4720. return;
  4721. }
  4722. RECORD_MULTI_VALUE_STATS();
  4723. }
  4724. count += i;
  4725. } while (bench_stats_check(start)
  4726. #ifdef MULTI_VALUE_STATISTICS
  4727. || runs < minimum_runs
  4728. #endif
  4729. );
  4730. bench_stats_sym_finish(decLabel, 0, count, bench_size, start, ret);
  4731. #ifdef MULTI_VALUE_STATISTICS
  4732. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4733. #endif
  4734. }
  4735. void bench_aessiv(void)
  4736. {
  4737. bench_aessiv_internal(bench_key, 32, "AES-256-SIV-enc", "AES-256-SIV-dec");
  4738. bench_aessiv_internal(bench_key, 48, "AES-384-SIV-enc", "AES-384-SIV-dec");
  4739. bench_aessiv_internal(bench_key, 64, "AES-512-SIV-enc", "AES-512-SIV-dec");
  4740. }
  4741. #endif /* WOLFSSL_AES_SIV */
  4742. #endif /* !NO_AES */
  4743. #ifdef HAVE_POLY1305
  4744. void bench_poly1305(void)
  4745. {
  4746. Poly1305 enc;
  4747. byte mac[16];
  4748. double start;
  4749. int ret = 0, i, count;
  4750. DECLARE_MULTI_VALUE_STATS_VARS()
  4751. if (digest_stream) {
  4752. ret = wc_Poly1305SetKey(&enc, bench_key, 32);
  4753. if (ret != 0) {
  4754. printf("Poly1305SetKey failed, ret = %d\n", ret);
  4755. return;
  4756. }
  4757. bench_stats_start(&count, &start);
  4758. do {
  4759. for (i = 0; i < numBlocks; i++) {
  4760. ret = wc_Poly1305Update(&enc, bench_plain, bench_size);
  4761. if (ret != 0) {
  4762. printf("Poly1305Update failed: %d\n", ret);
  4763. break;
  4764. }
  4765. RECORD_MULTI_VALUE_STATS();
  4766. }
  4767. wc_Poly1305Final(&enc, mac);
  4768. count += i;
  4769. } while (bench_stats_check(start)
  4770. #ifdef MULTI_VALUE_STATISTICS
  4771. || runs < minimum_runs
  4772. #endif
  4773. );
  4774. }
  4775. else {
  4776. bench_stats_start(&count, &start);
  4777. do {
  4778. for (i = 0; i < numBlocks; i++) {
  4779. ret = wc_Poly1305SetKey(&enc, bench_key, 32);
  4780. if (ret != 0) {
  4781. printf("Poly1305SetKey failed, ret = %d\n", ret);
  4782. return;
  4783. }
  4784. ret = wc_Poly1305Update(&enc, bench_plain, bench_size);
  4785. if (ret != 0) {
  4786. printf("Poly1305Update failed: %d\n", ret);
  4787. break;
  4788. }
  4789. wc_Poly1305Final(&enc, mac);
  4790. RECORD_MULTI_VALUE_STATS();
  4791. }
  4792. count += i;
  4793. } while (bench_stats_check(start)
  4794. #ifdef MULTI_VALUE_STATISTICS
  4795. || runs < minimum_runs
  4796. #endif
  4797. );
  4798. }
  4799. bench_stats_sym_finish("POLY1305", 0, count, bench_size, start, ret);
  4800. #ifdef MULTI_VALUE_STATISTICS
  4801. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4802. #endif
  4803. }
  4804. #endif /* HAVE_POLY1305 */
  4805. #ifdef HAVE_CAMELLIA
  4806. void bench_camellia(void)
  4807. {
  4808. Camellia cam;
  4809. double start;
  4810. int ret, i, count;
  4811. DECLARE_MULTI_VALUE_STATS_VARS()
  4812. ret = wc_CamelliaSetKey(&cam, bench_key, 16, bench_iv);
  4813. if (ret != 0) {
  4814. printf("CamelliaSetKey failed, ret = %d\n", ret);
  4815. return;
  4816. }
  4817. bench_stats_start(&count, &start);
  4818. do {
  4819. for (i = 0; i < numBlocks; i++) {
  4820. ret = wc_CamelliaCbcEncrypt(&cam, bench_cipher, bench_plain,
  4821. bench_size);
  4822. if (ret < 0) {
  4823. printf("CamelliaCbcEncrypt failed: %d\n", ret);
  4824. return;
  4825. }
  4826. RECORD_MULTI_VALUE_STATS();
  4827. }
  4828. count += i;
  4829. } while (bench_stats_check(start)
  4830. #ifdef MULTI_VALUE_STATISTICS
  4831. || runs < minimum_runs
  4832. #endif
  4833. );
  4834. bench_stats_sym_finish("Camellia", 0, count, bench_size, start, ret);
  4835. #ifdef MULTI_VALUE_STATISTICS
  4836. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4837. #endif
  4838. }
  4839. #endif
  4840. #ifdef WOLFSSL_SM4_CBC
  4841. void bench_sm4_cbc(void)
  4842. {
  4843. wc_Sm4 sm4;
  4844. double start;
  4845. int ret;
  4846. int i;
  4847. int count;
  4848. DECLARE_MULTI_VALUE_STATS_VARS()
  4849. ret = wc_Sm4SetKey(&sm4, bench_key, SM4_KEY_SIZE);
  4850. if (ret != 0) {
  4851. printf("Sm4SetKey failed, ret = %d\n", ret);
  4852. return;
  4853. }
  4854. ret = wc_Sm4SetIV(&sm4, bench_iv);
  4855. if (ret != 0) {
  4856. printf("Sm4SetIV failed, ret = %d\n", ret);
  4857. return;
  4858. }
  4859. bench_stats_start(&count, &start);
  4860. do {
  4861. for (i = 0; i < numBlocks; i++) {
  4862. ret = wc_Sm4CbcEncrypt(&sm4, bench_cipher, bench_plain, bench_size);
  4863. if (ret < 0) {
  4864. printf("Sm4CbcEncrypt failed: %d\n", ret);
  4865. return;
  4866. }
  4867. RECORD_MULTI_VALUE_STATS();
  4868. }
  4869. count += i;
  4870. } while (bench_stats_check(start)
  4871. #ifdef MULTI_VALUE_STATISTICS
  4872. || runs < minimum_runs
  4873. #endif
  4874. );
  4875. bench_stats_sym_finish("SM4-CBC-enc", 0, count, bench_size, start, ret);
  4876. #ifdef MULTI_VALUE_STATISTICS
  4877. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4878. #endif
  4879. RESET_MULTI_VALUE_STATS_VARS();
  4880. bench_stats_start(&count, &start);
  4881. do {
  4882. for (i = 0; i < numBlocks; i++) {
  4883. ret = wc_Sm4CbcDecrypt(&sm4, bench_plain, bench_cipher, bench_size);
  4884. if (ret < 0) {
  4885. printf("Sm4CbcDecrypt failed: %d\n", ret);
  4886. return;
  4887. }
  4888. RECORD_MULTI_VALUE_STATS();
  4889. }
  4890. count += i;
  4891. } while (bench_stats_check(start)
  4892. #ifdef MULTI_VALUE_STATISTICS
  4893. || runs < minimum_runs
  4894. #endif
  4895. );
  4896. bench_stats_sym_finish("SM4-CBC-dec", 0, count, bench_size, start, ret);
  4897. #ifdef MULTI_VALUE_STATISTICS
  4898. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4899. #endif
  4900. }
  4901. #endif
  4902. #ifdef WOLFSSL_SM4_GCM
  4903. void bench_sm4_gcm(void)
  4904. {
  4905. wc_Sm4 sm4;
  4906. double start;
  4907. int ret;
  4908. int i;
  4909. int count;
  4910. DECLARE_MULTI_VALUE_STATS_VARS()
  4911. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4912. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4913. WC_ALLOC_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4914. WC_ALLOC_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4915. ret = wc_Sm4GcmSetKey(&sm4, bench_key, SM4_KEY_SIZE);
  4916. if (ret != 0) {
  4917. printf("Sm4GcmSetKey failed, ret = %d\n", ret);
  4918. goto exit;
  4919. }
  4920. bench_stats_start(&count, &start);
  4921. do {
  4922. for (i = 0; i < numBlocks; i++) {
  4923. ret = wc_Sm4GcmEncrypt(&sm4, bench_cipher, bench_plain, bench_size,
  4924. bench_iv, GCM_NONCE_MID_SZ, bench_tag, SM4_BLOCK_SIZE,
  4925. bench_additional, aesAuthAddSz);
  4926. if (ret < 0) {
  4927. printf("Sm4GcmEncrypt failed: %d\n", ret);
  4928. goto exit;
  4929. }
  4930. RECORD_MULTI_VALUE_STATS();
  4931. }
  4932. count += i;
  4933. } while (bench_stats_check(start)
  4934. #ifdef MULTI_VALUE_STATISTICS
  4935. || runs < minimum_runs
  4936. #endif
  4937. );
  4938. bench_stats_sym_finish("SM4-GCM-enc", 0, count, bench_size, start, ret);
  4939. #ifdef MULTI_VALUE_STATISTICS
  4940. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4941. #endif
  4942. RESET_MULTI_VALUE_STATS_VARS();
  4943. bench_stats_start(&count, &start);
  4944. do {
  4945. for (i = 0; i < numBlocks; i++) {
  4946. ret = wc_Sm4GcmDecrypt(&sm4, bench_plain, bench_cipher, bench_size,
  4947. bench_iv, GCM_NONCE_MID_SZ, bench_tag, SM4_BLOCK_SIZE,
  4948. bench_additional, aesAuthAddSz);
  4949. if (ret < 0) {
  4950. printf("Sm4GcmDecrypt failed: %d\n", ret);
  4951. goto exit;
  4952. }
  4953. RECORD_MULTI_VALUE_STATS();
  4954. }
  4955. count += i;
  4956. } while (bench_stats_check(start)
  4957. #ifdef MULTI_VALUE_STATISTICS
  4958. || runs < minimum_runs
  4959. #endif
  4960. );
  4961. bench_stats_sym_finish("SM4-GCM-dec", 0, count, bench_size, start, ret);
  4962. #ifdef MULTI_VALUE_STATISTICS
  4963. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4964. #endif
  4965. exit:
  4966. WC_FREE_VAR(bench_additional, HEAP_HINT);
  4967. WC_FREE_VAR(bench_tag, HEAP_HINT);
  4968. }
  4969. #endif
  4970. #ifdef WOLFSSL_SM4_CCM
  4971. void bench_sm4_ccm()
  4972. {
  4973. wc_Sm4 enc;
  4974. double start;
  4975. int ret, i, count;
  4976. DECLARE_MULTI_VALUE_STATS_VARS()
  4977. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4978. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4979. WC_ALLOC_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4980. WC_ALLOC_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4981. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  4982. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  4983. if ((ret = wc_Sm4SetKey(&enc, bench_key, 16)) != 0) {
  4984. printf("wc_Sm4SetKey failed, ret = %d\n", ret);
  4985. goto exit;
  4986. }
  4987. bench_stats_start(&count, &start);
  4988. do {
  4989. for (i = 0; i < numBlocks; i++) {
  4990. ret |= wc_Sm4CcmEncrypt(&enc, bench_cipher, bench_plain, bench_size,
  4991. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  4992. bench_additional, 0);
  4993. RECORD_MULTI_VALUE_STATS();
  4994. }
  4995. count += i;
  4996. } while (bench_stats_check(start)
  4997. #ifdef MULTI_VALUE_STATISTICS
  4998. || runs < minimum_runs
  4999. #endif
  5000. );
  5001. bench_stats_sym_finish("SM4-CCM-enc", 0, count, bench_size, start, ret);
  5002. #ifdef MULTI_VALUE_STATISTICS
  5003. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5004. #endif
  5005. if (ret != 0) {
  5006. printf("wc_Sm4Encrypt failed, ret = %d\n", ret);
  5007. goto exit;
  5008. }
  5009. RESET_MULTI_VALUE_STATS_VARS();
  5010. bench_stats_start(&count, &start);
  5011. do {
  5012. for (i = 0; i < numBlocks; i++) {
  5013. ret |= wc_Sm4CcmDecrypt(&enc, bench_plain, bench_cipher, bench_size,
  5014. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  5015. bench_additional, 0);
  5016. RECORD_MULTI_VALUE_STATS();
  5017. }
  5018. count += i;
  5019. } while (bench_stats_check(start)
  5020. #ifdef MULTI_VALUE_STATISTICS
  5021. || runs < minimum_runs
  5022. #endif
  5023. );
  5024. bench_stats_sym_finish("SM4-CCM-dec", 0, count, bench_size, start, ret);
  5025. #ifdef MULTI_VALUE_STATISTICS
  5026. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5027. #endif
  5028. if (ret != 0) {
  5029. printf("wc_Sm4Decrypt failed, ret = %d\n", ret);
  5030. goto exit;
  5031. }
  5032. exit:
  5033. WC_FREE_VAR(bench_additional, HEAP_HINT);
  5034. WC_FREE_VAR(bench_tag, HEAP_HINT);
  5035. }
  5036. #endif /* HAVE_AESCCM */
  5037. #ifndef NO_DES3
  5038. void bench_des(int useDeviceID)
  5039. {
  5040. int ret = 0, i, count = 0, times, pending = 0;
  5041. WC_DECLARE_ARRAY(enc, Des3, BENCH_MAX_PENDING,
  5042. sizeof(Des3), HEAP_HINT);
  5043. double start;
  5044. DECLARE_MULTI_VALUE_STATS_VARS()
  5045. WC_CALLOC_ARRAY(enc, Des3, BENCH_MAX_PENDING,
  5046. sizeof(Des3), HEAP_HINT);
  5047. /* init keys */
  5048. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5049. if ((ret = wc_Des3Init(enc[i], HEAP_HINT,
  5050. useDeviceID ? devId : INVALID_DEVID)) != 0) {
  5051. printf("Des3Init failed, ret = %d\n", ret);
  5052. goto exit;
  5053. }
  5054. ret = wc_Des3_SetKey(enc[i], bench_key, bench_iv, DES_ENCRYPTION);
  5055. if (ret != 0) {
  5056. printf("Des3_SetKey failed, ret = %d\n", ret);
  5057. goto exit;
  5058. }
  5059. }
  5060. bench_stats_start(&count, &start);
  5061. do {
  5062. for (times = 0; times < numBlocks || pending > 0; ) {
  5063. bench_async_poll(&pending);
  5064. /* while free pending slots in queue, submit ops */
  5065. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5066. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  5067. &times, numBlocks, &pending)) {
  5068. ret = wc_Des3_CbcEncrypt(enc[i],
  5069. bench_cipher,
  5070. bench_plain, bench_size);
  5071. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  5072. 0, &times, &pending)) {
  5073. goto exit_3des;
  5074. }
  5075. }
  5076. } /* for i */
  5077. RECORD_MULTI_VALUE_STATS();
  5078. } /* for times */
  5079. count += times;
  5080. } while (bench_stats_check(start)
  5081. #ifdef MULTI_VALUE_STATISTICS
  5082. || runs < minimum_runs
  5083. #endif
  5084. );
  5085. exit_3des:
  5086. bench_stats_sym_finish("3DES", useDeviceID, count, bench_size, start, ret);
  5087. #ifdef MULTI_VALUE_STATISTICS
  5088. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5089. #endif
  5090. exit:
  5091. if (WC_ARRAY_OK(enc)) {
  5092. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5093. wc_Des3Free(enc[i]);
  5094. }
  5095. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  5096. }
  5097. }
  5098. #endif /* !NO_DES3 */
  5099. #ifndef NO_RC4
  5100. void bench_arc4(int useDeviceID)
  5101. {
  5102. int ret = 0, i, count = 0, times, pending = 0;
  5103. WC_DECLARE_ARRAY(enc, Arc4, BENCH_MAX_PENDING,
  5104. sizeof(Arc4), HEAP_HINT);
  5105. double start;
  5106. DECLARE_MULTI_VALUE_STATS_VARS()
  5107. WC_CALLOC_ARRAY(enc, Arc4, BENCH_MAX_PENDING,
  5108. sizeof(Arc4), HEAP_HINT);
  5109. /* init keys */
  5110. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5111. if ((ret = wc_Arc4Init(enc[i], HEAP_HINT,
  5112. useDeviceID ? devId : INVALID_DEVID)) != 0) {
  5113. printf("Arc4Init failed, ret = %d\n", ret);
  5114. goto exit;
  5115. }
  5116. ret = wc_Arc4SetKey(enc[i], bench_key, 16);
  5117. if (ret != 0) {
  5118. printf("Arc4SetKey failed, ret = %d\n", ret);
  5119. goto exit;
  5120. }
  5121. }
  5122. bench_stats_start(&count, &start);
  5123. do {
  5124. for (times = 0; times < numBlocks || pending > 0; ) {
  5125. bench_async_poll(&pending);
  5126. /* while free pending slots in queue, submit ops */
  5127. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5128. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  5129. &times, numBlocks, &pending)) {
  5130. ret = wc_Arc4Process(enc[i], bench_cipher, bench_plain,
  5131. bench_size);
  5132. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  5133. 0, &times, &pending)) {
  5134. goto exit_arc4;
  5135. }
  5136. }
  5137. } /* for i */
  5138. RECORD_MULTI_VALUE_STATS();
  5139. } /* for times */
  5140. count += times;
  5141. } while (bench_stats_check(start)
  5142. #ifdef MULTI_VALUE_STATISTICS
  5143. || runs < minimum_runs
  5144. #endif
  5145. );
  5146. exit_arc4:
  5147. bench_stats_sym_finish("ARC4", useDeviceID, count, bench_size, start, ret);
  5148. #ifdef MULTI_VALUE_STATISTICS
  5149. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5150. #endif
  5151. exit:
  5152. if (WC_ARRAY_OK(enc)) {
  5153. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5154. wc_Arc4Free(enc[i]);
  5155. }
  5156. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  5157. }
  5158. }
  5159. #endif /* !NO_RC4 */
  5160. #ifdef HAVE_CHACHA
  5161. void bench_chacha(void)
  5162. {
  5163. WC_DECLARE_VAR(enc, ChaCha, 1, HEAP_HINT);
  5164. double start;
  5165. int ret, i, count;
  5166. DECLARE_MULTI_VALUE_STATS_VARS()
  5167. WC_ALLOC_VAR(enc, ChaCha, 1, HEAP_HINT);
  5168. XMEMSET(enc, 0, sizeof(ChaCha));
  5169. wc_Chacha_SetKey(enc, bench_key, 16);
  5170. bench_stats_start(&count, &start);
  5171. do {
  5172. for (i = 0; i < numBlocks; i++) {
  5173. ret = wc_Chacha_SetIV(enc, bench_iv, 0);
  5174. if (ret < 0) {
  5175. printf("wc_Chacha_SetIV error: %d\n", ret);
  5176. goto exit;
  5177. }
  5178. ret = wc_Chacha_Process(enc, bench_cipher, bench_plain, bench_size);
  5179. if (ret < 0) {
  5180. printf("wc_Chacha_Process error: %d\n", ret);
  5181. goto exit;
  5182. }
  5183. RECORD_MULTI_VALUE_STATS();
  5184. }
  5185. count += i;
  5186. } while (bench_stats_check(start)
  5187. #ifdef MULTI_VALUE_STATISTICS
  5188. || runs < minimum_runs
  5189. #endif
  5190. );
  5191. bench_stats_sym_finish("CHACHA", 0, count, bench_size, start, 0);
  5192. #ifdef MULTI_VALUE_STATISTICS
  5193. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5194. #endif
  5195. exit:
  5196. WC_FREE_VAR(enc, HEAP_HINT);
  5197. }
  5198. #endif /* HAVE_CHACHA*/
  5199. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5200. void bench_chacha20_poly1305_aead(void)
  5201. {
  5202. double start;
  5203. int ret = 0, i, count;
  5204. DECLARE_MULTI_VALUE_STATS_VARS()
  5205. WC_DECLARE_VAR(authTag, byte, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, HEAP_HINT);
  5206. WC_ALLOC_VAR(authTag, byte, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, HEAP_HINT);
  5207. XMEMSET(authTag, 0, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE);
  5208. bench_stats_start(&count, &start);
  5209. do {
  5210. for (i = 0; i < numBlocks; i++) {
  5211. ret = wc_ChaCha20Poly1305_Encrypt(bench_key, bench_iv, NULL, 0,
  5212. bench_plain, bench_size, bench_cipher, authTag);
  5213. if (ret < 0) {
  5214. printf("wc_ChaCha20Poly1305_Encrypt error: %d\n", ret);
  5215. goto exit;
  5216. }
  5217. RECORD_MULTI_VALUE_STATS();
  5218. }
  5219. count += i;
  5220. } while (bench_stats_check(start)
  5221. #ifdef MULTI_VALUE_STATISTICS
  5222. || runs < minimum_runs
  5223. #endif
  5224. );
  5225. bench_stats_sym_finish("CHA-POLY", 0, count, bench_size, start, ret);
  5226. #ifdef MULTI_VALUE_STATISTICS
  5227. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5228. #endif
  5229. exit:
  5230. WC_FREE_VAR(authTag, HEAP_HINT);
  5231. }
  5232. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  5233. #ifndef NO_MD5
  5234. void bench_md5(int useDeviceID)
  5235. {
  5236. WC_DECLARE_ARRAY(hash, wc_Md5, BENCH_MAX_PENDING,
  5237. sizeof(wc_Md5), HEAP_HINT);
  5238. double start = 0;
  5239. int ret = 0, i, count = 0, times, pending = 0;
  5240. DECLARE_MULTI_VALUE_STATS_VARS()
  5241. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5242. WC_MD5_DIGEST_SIZE, HEAP_HINT);
  5243. WC_CALLOC_ARRAY(hash, wc_Md5, BENCH_MAX_PENDING,
  5244. sizeof(wc_Md5), HEAP_HINT);
  5245. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5246. WC_MD5_DIGEST_SIZE, HEAP_HINT);
  5247. if (digest_stream) {
  5248. /* init keys */
  5249. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5250. ret = wc_InitMd5_ex(hash[i], HEAP_HINT,
  5251. useDeviceID ? devId : INVALID_DEVID);
  5252. if (ret != 0) {
  5253. printf("InitMd5_ex failed, ret = %d\n", ret);
  5254. goto exit;
  5255. }
  5256. #ifdef WOLFSSL_PIC32MZ_HASH
  5257. wc_Md5SizeSet(hash[i], numBlocks * bench_size);
  5258. #endif
  5259. }
  5260. bench_stats_start(&count, &start);
  5261. do {
  5262. for (times = 0; times < numBlocks || pending > 0; ) {
  5263. bench_async_poll(&pending);
  5264. /* while free pending slots in queue, submit ops */
  5265. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5266. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5267. 0, &times, numBlocks, &pending)) {
  5268. ret = wc_Md5Update(hash[i], bench_plain,
  5269. bench_size);
  5270. if (!bench_async_handle(&ret,
  5271. BENCH_ASYNC_GET_DEV(hash[i]),
  5272. 0, &times, &pending)) {
  5273. goto exit_md5;
  5274. }
  5275. }
  5276. } /* for i */
  5277. RECORD_MULTI_VALUE_STATS();
  5278. } /* for times */
  5279. count += times;
  5280. times = 0;
  5281. do {
  5282. bench_async_poll(&pending);
  5283. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5284. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5285. 0, &times, numBlocks, &pending)) {
  5286. ret = wc_Md5Final(hash[i], digest[i]);
  5287. if (!bench_async_handle(&ret,
  5288. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5289. &times, &pending)) {
  5290. goto exit_md5;
  5291. }
  5292. }
  5293. } /* for i */
  5294. } while (pending > 0);
  5295. } while (bench_stats_check(start)
  5296. #ifdef MULTI_VALUE_STATISTICS
  5297. || runs < minimum_runs
  5298. #endif
  5299. );
  5300. }
  5301. else {
  5302. bench_stats_start(&count, &start);
  5303. do {
  5304. for (times = 0; times < numBlocks; times++) {
  5305. ret = wc_InitMd5_ex(hash[0], HEAP_HINT, INVALID_DEVID);
  5306. if (ret == 0)
  5307. ret = wc_Md5Update(hash[0], bench_plain, bench_size);
  5308. if (ret == 0)
  5309. ret = wc_Md5Final(hash[0], digest[0]);
  5310. if (ret != 0)
  5311. goto exit_md5;
  5312. RECORD_MULTI_VALUE_STATS();
  5313. } /* for times */
  5314. count += times;
  5315. } while (bench_stats_check(start)
  5316. #ifdef MULTI_VALUE_STATISTICS
  5317. || runs < minimum_runs
  5318. #endif
  5319. );
  5320. }
  5321. exit_md5:
  5322. bench_stats_sym_finish("MD5", useDeviceID, count, bench_size, start, ret);
  5323. #ifdef MULTI_VALUE_STATISTICS
  5324. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5325. #endif
  5326. exit:
  5327. #ifdef WOLFSSL_ASYNC_CRYPT
  5328. if (WC_ARRAY_OK(hash)) {
  5329. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5330. wc_Md5Free(hash[i]);
  5331. }
  5332. }
  5333. #endif
  5334. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5335. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5336. }
  5337. #endif /* !NO_MD5 */
  5338. #ifndef NO_SHA
  5339. void bench_sha(int useDeviceID)
  5340. {
  5341. WC_DECLARE_ARRAY(hash, wc_Sha, BENCH_MAX_PENDING,
  5342. sizeof(wc_Sha), HEAP_HINT);
  5343. double start;
  5344. int ret = 0, i, count = 0, times, pending = 0;
  5345. DECLARE_MULTI_VALUE_STATS_VARS()
  5346. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5347. WC_SHA_DIGEST_SIZE, HEAP_HINT);
  5348. WC_CALLOC_ARRAY(hash, wc_Sha, BENCH_MAX_PENDING,
  5349. sizeof(wc_Sha), HEAP_HINT);
  5350. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5351. WC_SHA_DIGEST_SIZE, HEAP_HINT);
  5352. if (digest_stream) {
  5353. /* init keys */
  5354. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5355. ret = wc_InitSha_ex(hash[i], HEAP_HINT,
  5356. useDeviceID ? devId : INVALID_DEVID);
  5357. if (ret != 0) {
  5358. printf("InitSha failed, ret = %d\n", ret);
  5359. goto exit;
  5360. }
  5361. #ifdef WOLFSSL_PIC32MZ_HASH
  5362. wc_ShaSizeSet(hash[i], numBlocks * bench_size);
  5363. #endif
  5364. }
  5365. bench_stats_start(&count, &start);
  5366. do {
  5367. for (times = 0; times < numBlocks || pending > 0; ) {
  5368. bench_async_poll(&pending);
  5369. /* while free pending slots in queue, submit ops */
  5370. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5371. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5372. 0, &times, numBlocks, &pending)) {
  5373. ret = wc_ShaUpdate(hash[i], bench_plain,
  5374. bench_size);
  5375. if (!bench_async_handle(&ret,
  5376. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5377. &times, &pending)) {
  5378. goto exit_sha;
  5379. }
  5380. }
  5381. } /* for i */
  5382. RECORD_MULTI_VALUE_STATS();
  5383. } /* for times */
  5384. count += times;
  5385. times = 0;
  5386. do {
  5387. bench_async_poll(&pending);
  5388. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5389. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5390. 0, &times, numBlocks, &pending)) {
  5391. ret = wc_ShaFinal(hash[i], digest[i]);
  5392. if (!bench_async_handle(&ret,
  5393. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5394. &times, &pending)) {
  5395. goto exit_sha;
  5396. }
  5397. }
  5398. } /* for i */
  5399. } while (pending > 0);
  5400. } while (bench_stats_check(start)
  5401. #ifdef MULTI_VALUE_STATISTICS
  5402. || runs < minimum_runs
  5403. #endif
  5404. );
  5405. }
  5406. else {
  5407. bench_stats_start(&count, &start);
  5408. do {
  5409. for (times = 0; times < numBlocks; times++) {
  5410. ret = wc_InitSha_ex(hash[0], HEAP_HINT,
  5411. useDeviceID ? devId : INVALID_DEVID);
  5412. if (ret == 0)
  5413. ret = wc_ShaUpdate(hash[0], bench_plain, bench_size);
  5414. if (ret == 0)
  5415. ret = wc_ShaFinal(hash[0], digest[0]);
  5416. if (ret != 0)
  5417. goto exit_sha;
  5418. RECORD_MULTI_VALUE_STATS();
  5419. } /* for times */
  5420. count += times;
  5421. } while (bench_stats_check(start)
  5422. #ifdef MULTI_VALUE_STATISTICS
  5423. || runs < minimum_runs
  5424. #endif
  5425. );
  5426. }
  5427. exit_sha:
  5428. bench_stats_sym_finish("SHA", useDeviceID, count, bench_size, start, ret);
  5429. #ifdef MULTI_VALUE_STATISTICS
  5430. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5431. #endif
  5432. exit:
  5433. if (WC_ARRAY_OK(hash)) {
  5434. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5435. wc_ShaFree(hash[i]);
  5436. }
  5437. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5438. }
  5439. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5440. }
  5441. #endif /* NO_SHA */
  5442. #ifdef WOLFSSL_SHA224
  5443. void bench_sha224(int useDeviceID)
  5444. {
  5445. WC_DECLARE_ARRAY(hash, wc_Sha224, BENCH_MAX_PENDING,
  5446. sizeof(wc_Sha224), HEAP_HINT);
  5447. double start;
  5448. int ret = 0, i, count = 0, times, pending = 0;
  5449. DECLARE_MULTI_VALUE_STATS_VARS()
  5450. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5451. WC_SHA224_DIGEST_SIZE, HEAP_HINT);
  5452. WC_CALLOC_ARRAY(hash, wc_Sha224, BENCH_MAX_PENDING,
  5453. sizeof(wc_Sha224), HEAP_HINT);
  5454. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5455. WC_SHA224_DIGEST_SIZE, HEAP_HINT);
  5456. if (digest_stream) {
  5457. /* init keys */
  5458. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5459. ret = wc_InitSha224_ex(hash[i], HEAP_HINT,
  5460. useDeviceID ? devId : INVALID_DEVID);
  5461. if (ret != 0) {
  5462. printf("InitSha224_ex failed, ret = %d\n", ret);
  5463. goto exit;
  5464. }
  5465. }
  5466. bench_stats_start(&count, &start);
  5467. do {
  5468. for (times = 0; times < numBlocks || pending > 0; ) {
  5469. bench_async_poll(&pending);
  5470. /* while free pending slots in queue, submit ops */
  5471. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5472. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5473. 0, &times, numBlocks, &pending)) {
  5474. ret = wc_Sha224Update(hash[i], bench_plain,
  5475. bench_size);
  5476. if (!bench_async_handle(&ret,
  5477. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5478. &times, &pending)) {
  5479. goto exit_sha224;
  5480. }
  5481. }
  5482. } /* for i */
  5483. RECORD_MULTI_VALUE_STATS();
  5484. } /* for times */
  5485. count += times;
  5486. times = 0;
  5487. do {
  5488. bench_async_poll(&pending);
  5489. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5490. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5491. 0, &times, numBlocks, &pending)) {
  5492. ret = wc_Sha224Final(hash[i], digest[i]);
  5493. if (!bench_async_handle(&ret,
  5494. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5495. &times, &pending)) {
  5496. goto exit_sha224;
  5497. }
  5498. }
  5499. } /* for i */
  5500. } while (pending > 0);
  5501. } while (bench_stats_check(start)
  5502. #ifdef MULTI_VALUE_STATISTICS
  5503. || runs < minimum_runs
  5504. #endif
  5505. );
  5506. }
  5507. else {
  5508. bench_stats_start(&count, &start);
  5509. do {
  5510. for (times = 0; times < numBlocks; times++) {
  5511. ret = wc_InitSha224_ex(hash[0], HEAP_HINT,
  5512. useDeviceID ? devId : INVALID_DEVID);
  5513. if (ret == 0)
  5514. ret = wc_Sha224Update(hash[0], bench_plain, bench_size);
  5515. if (ret == 0)
  5516. ret = wc_Sha224Final(hash[0], digest[0]);
  5517. if (ret != 0)
  5518. goto exit_sha224;
  5519. } /* for times */
  5520. count += times;
  5521. } while (bench_stats_check(start)
  5522. #ifdef MULTI_VALUE_STATISTICS
  5523. || runs < minimum_runs
  5524. #endif
  5525. );
  5526. }
  5527. exit_sha224:
  5528. bench_stats_sym_finish("SHA-224", useDeviceID, count,
  5529. bench_size, start, ret);
  5530. #ifdef MULTI_VALUE_STATISTICS
  5531. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5532. #endif
  5533. exit:
  5534. if (WC_ARRAY_OK(hash)) {
  5535. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5536. wc_Sha224Free(hash[i]);
  5537. }
  5538. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5539. }
  5540. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5541. }
  5542. #endif
  5543. #ifndef NO_SHA256
  5544. void bench_sha256(int useDeviceID)
  5545. {
  5546. WC_DECLARE_ARRAY(hash, wc_Sha256, BENCH_MAX_PENDING,
  5547. sizeof(wc_Sha256), HEAP_HINT);
  5548. double start;
  5549. int ret = 0, i, count = 0, times, pending = 0;
  5550. DECLARE_MULTI_VALUE_STATS_VARS()
  5551. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5552. WC_SHA256_DIGEST_SIZE, HEAP_HINT);
  5553. WC_CALLOC_ARRAY(hash, wc_Sha256, BENCH_MAX_PENDING,
  5554. sizeof(wc_Sha256), HEAP_HINT);
  5555. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5556. WC_SHA256_DIGEST_SIZE, HEAP_HINT);
  5557. if (digest_stream) {
  5558. /* init keys */
  5559. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5560. ret = wc_InitSha256_ex(hash[i], HEAP_HINT,
  5561. useDeviceID ? devId: INVALID_DEVID);
  5562. if (ret != 0) {
  5563. printf("InitSha256_ex failed, ret = %d\n", ret);
  5564. goto exit;
  5565. }
  5566. #ifdef WOLFSSL_PIC32MZ_HASH
  5567. wc_Sha256SizeSet(hash[i], numBlocks * bench_size);
  5568. #endif
  5569. }
  5570. bench_stats_start(&count, &start);
  5571. do {
  5572. for (times = 0; times < numBlocks || pending > 0; ) {
  5573. bench_async_poll(&pending);
  5574. /* while free pending slots in queue, submit ops */
  5575. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5576. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5577. 0, &times, numBlocks, &pending)) {
  5578. ret = wc_Sha256Update(hash[i], bench_plain,
  5579. bench_size);
  5580. if (!bench_async_handle(&ret,
  5581. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5582. &times, &pending)) {
  5583. goto exit_sha256;
  5584. }
  5585. }
  5586. } /* for i */
  5587. RECORD_MULTI_VALUE_STATS();
  5588. } /* for times */
  5589. count += times;
  5590. times = 0;
  5591. do {
  5592. bench_async_poll(&pending);
  5593. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5594. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5595. 0, &times, numBlocks, &pending)) {
  5596. ret = wc_Sha256Final(hash[i], digest[i]);
  5597. if (!bench_async_handle(&ret,
  5598. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5599. &times, &pending)) {
  5600. goto exit_sha256;
  5601. }
  5602. }
  5603. } /* for i */
  5604. } while (pending > 0);
  5605. } while (bench_stats_check(start)
  5606. #ifdef MULTI_VALUE_STATISTICS
  5607. || runs < minimum_runs
  5608. #endif
  5609. );
  5610. }
  5611. else {
  5612. bench_stats_start(&count, &start);
  5613. do {
  5614. for (times = 0; times < numBlocks; times++) {
  5615. ret = wc_InitSha256_ex(hash[0], HEAP_HINT,
  5616. useDeviceID ? devId: INVALID_DEVID);
  5617. if (ret == 0)
  5618. ret = wc_Sha256Update(hash[0], bench_plain, bench_size);
  5619. if (ret == 0)
  5620. ret = wc_Sha256Final(hash[0], digest[0]);
  5621. if (ret != 0)
  5622. goto exit_sha256;
  5623. RECORD_MULTI_VALUE_STATS();
  5624. } /* for times */
  5625. count += times;
  5626. } while (bench_stats_check(start)
  5627. #ifdef MULTI_VALUE_STATISTICS
  5628. || runs < minimum_runs
  5629. #endif
  5630. );
  5631. }
  5632. exit_sha256:
  5633. bench_stats_sym_finish("SHA-256", useDeviceID, count, bench_size,
  5634. start, ret);
  5635. #ifdef MULTI_VALUE_STATISTICS
  5636. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5637. #endif
  5638. exit:
  5639. if (WC_ARRAY_OK(hash)) {
  5640. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5641. wc_Sha256Free(hash[i]);
  5642. }
  5643. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5644. }
  5645. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5646. }
  5647. #endif
  5648. #ifdef WOLFSSL_SHA384
  5649. void bench_sha384(int useDeviceID)
  5650. {
  5651. WC_DECLARE_ARRAY(hash, wc_Sha384, BENCH_MAX_PENDING,
  5652. sizeof(wc_Sha384), HEAP_HINT);
  5653. double start;
  5654. int ret = 0, i, count = 0, times, pending = 0;
  5655. DECLARE_MULTI_VALUE_STATS_VARS()
  5656. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5657. WC_SHA384_DIGEST_SIZE, HEAP_HINT);
  5658. WC_CALLOC_ARRAY(hash, wc_Sha384, BENCH_MAX_PENDING,
  5659. sizeof(wc_Sha384), HEAP_HINT);
  5660. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5661. WC_SHA384_DIGEST_SIZE, HEAP_HINT);
  5662. if (digest_stream) {
  5663. /* init keys */
  5664. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5665. ret = wc_InitSha384_ex(hash[i], HEAP_HINT,
  5666. useDeviceID ? devId : INVALID_DEVID);
  5667. if (ret != 0) {
  5668. printf("InitSha384_ex failed, ret = %d\n", ret);
  5669. goto exit;
  5670. }
  5671. }
  5672. bench_stats_start(&count, &start);
  5673. do {
  5674. for (times = 0; times < numBlocks || pending > 0; ) {
  5675. bench_async_poll(&pending);
  5676. /* while free pending slots in queue, submit ops */
  5677. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5678. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5679. 0, &times, numBlocks, &pending)) {
  5680. ret = wc_Sha384Update(hash[i], bench_plain,
  5681. bench_size);
  5682. if (!bench_async_handle(&ret,
  5683. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5684. &times, &pending)) {
  5685. goto exit_sha384;
  5686. }
  5687. }
  5688. } /* for i */
  5689. RECORD_MULTI_VALUE_STATS();
  5690. } /* for times */
  5691. count += times;
  5692. times = 0;
  5693. do {
  5694. bench_async_poll(&pending);
  5695. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5696. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5697. 0, &times, numBlocks, &pending)) {
  5698. ret = wc_Sha384Final(hash[i], digest[i]);
  5699. if (!bench_async_handle(&ret,
  5700. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5701. &times, &pending)) {
  5702. goto exit_sha384;
  5703. }
  5704. }
  5705. } /* for i */
  5706. } while (pending > 0);
  5707. } while (bench_stats_check(start)
  5708. #ifdef MULTI_VALUE_STATISTICS
  5709. || runs < minimum_runs
  5710. #endif
  5711. );
  5712. }
  5713. else {
  5714. bench_stats_start(&count, &start);
  5715. do {
  5716. for (times = 0; times < numBlocks; times++) {
  5717. ret = wc_InitSha384_ex(hash[0], HEAP_HINT,
  5718. useDeviceID ? devId : INVALID_DEVID);
  5719. if (ret == 0)
  5720. ret = wc_Sha384Update(hash[0], bench_plain, bench_size);
  5721. if (ret == 0)
  5722. ret = wc_Sha384Final(hash[0], digest[0]);
  5723. if (ret != 0)
  5724. goto exit_sha384;
  5725. RECORD_MULTI_VALUE_STATS();
  5726. } /* for times */
  5727. count += times;
  5728. } while (bench_stats_check(start)
  5729. #ifdef MULTI_VALUE_STATISTICS
  5730. || runs < minimum_runs
  5731. #endif
  5732. );
  5733. }
  5734. exit_sha384:
  5735. bench_stats_sym_finish("SHA-384", useDeviceID, count, bench_size,
  5736. start, ret);
  5737. #ifdef MULTI_VALUE_STATISTICS
  5738. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5739. #endif
  5740. exit:
  5741. if (WC_ARRAY_OK(hash)) {
  5742. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5743. wc_Sha384Free(hash[i]);
  5744. }
  5745. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5746. }
  5747. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5748. }
  5749. #endif
  5750. #ifdef WOLFSSL_SHA512
  5751. void bench_sha512(int useDeviceID)
  5752. {
  5753. WC_DECLARE_ARRAY(hash, wc_Sha512, BENCH_MAX_PENDING,
  5754. sizeof(wc_Sha512), HEAP_HINT);
  5755. double start;
  5756. int ret = 0, i, count = 0, times, pending = 0;
  5757. DECLARE_MULTI_VALUE_STATS_VARS()
  5758. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5759. WC_SHA512_DIGEST_SIZE, HEAP_HINT);
  5760. WC_CALLOC_ARRAY(hash, wc_Sha512, BENCH_MAX_PENDING,
  5761. sizeof(wc_Sha512), HEAP_HINT);
  5762. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5763. WC_SHA512_DIGEST_SIZE, HEAP_HINT);
  5764. if (digest_stream) {
  5765. /* init keys */
  5766. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5767. ret = wc_InitSha512_ex(hash[i], HEAP_HINT,
  5768. useDeviceID ? devId : INVALID_DEVID);
  5769. if (ret != 0) {
  5770. printf("InitSha512_ex failed, ret = %d\n", ret);
  5771. goto exit;
  5772. }
  5773. }
  5774. bench_stats_start(&count, &start);
  5775. do {
  5776. for (times = 0; times < numBlocks || pending > 0; ) {
  5777. bench_async_poll(&pending);
  5778. /* while free pending slots in queue, submit ops */
  5779. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5780. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5781. 0, &times, numBlocks, &pending)) {
  5782. ret = wc_Sha512Update(hash[i], bench_plain,
  5783. bench_size);
  5784. if (!bench_async_handle(&ret,
  5785. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5786. &times, &pending)) {
  5787. goto exit_sha512;
  5788. }
  5789. }
  5790. } /* for i */
  5791. RECORD_MULTI_VALUE_STATS();
  5792. } /* for times */
  5793. count += times;
  5794. times = 0;
  5795. do {
  5796. bench_async_poll(&pending);
  5797. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5798. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5799. 0, &times, numBlocks, &pending)) {
  5800. ret = wc_Sha512Final(hash[i], digest[i]);
  5801. if (!bench_async_handle(&ret,
  5802. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5803. &times, &pending)) {
  5804. goto exit_sha512;
  5805. }
  5806. }
  5807. } /* for i */
  5808. } while (pending > 0);
  5809. } while (bench_stats_check(start)
  5810. #ifdef MULTI_VALUE_STATISTICS
  5811. || runs < minimum_runs
  5812. #endif
  5813. );
  5814. }
  5815. else {
  5816. bench_stats_start(&count, &start);
  5817. do {
  5818. for (times = 0; times < numBlocks; times++) {
  5819. ret = wc_InitSha512_ex(hash[0], HEAP_HINT,
  5820. useDeviceID ? devId : INVALID_DEVID);
  5821. if (ret == 0)
  5822. ret = wc_Sha512Update(hash[0], bench_plain, bench_size);
  5823. if (ret == 0)
  5824. ret = wc_Sha512Final(hash[0], digest[0]);
  5825. if (ret != 0)
  5826. goto exit_sha512;
  5827. RECORD_MULTI_VALUE_STATS();
  5828. } /* for times */
  5829. count += times;
  5830. } while (bench_stats_check(start)
  5831. #ifdef MULTI_VALUE_STATISTICS
  5832. || runs < minimum_runs
  5833. #endif
  5834. );
  5835. }
  5836. exit_sha512:
  5837. bench_stats_sym_finish("SHA-512", useDeviceID, count, bench_size,
  5838. start, ret);
  5839. #ifdef MULTI_VALUE_STATISTICS
  5840. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5841. #endif
  5842. exit:
  5843. if (WC_ARRAY_OK(hash)) {
  5844. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5845. wc_Sha512Free(hash[i]);
  5846. }
  5847. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5848. }
  5849. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5850. }
  5851. #if !defined(WOLFSSL_NOSHA512_224) && \
  5852. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  5853. void bench_sha512_224(int useDeviceID)
  5854. {
  5855. WC_DECLARE_ARRAY(hash, wc_Sha512_224, BENCH_MAX_PENDING,
  5856. sizeof(wc_Sha512_224), HEAP_HINT);
  5857. double start;
  5858. int ret = 0, i, count = 0, times, pending = 0;
  5859. DECLARE_MULTI_VALUE_STATS_VARS()
  5860. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5861. WC_SHA512_224_DIGEST_SIZE, HEAP_HINT);
  5862. WC_CALLOC_ARRAY(hash, wc_Sha512_224, BENCH_MAX_PENDING,
  5863. sizeof(wc_Sha512_224), HEAP_HINT);
  5864. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5865. WC_SHA512_224_DIGEST_SIZE, HEAP_HINT);
  5866. if (digest_stream) {
  5867. /* init keys */
  5868. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5869. ret = wc_InitSha512_224_ex(hash[i], HEAP_HINT,
  5870. useDeviceID ? devId : INVALID_DEVID);
  5871. if (ret != 0) {
  5872. printf("InitSha512_224_ex failed, ret = %d\n", ret);
  5873. goto exit;
  5874. }
  5875. }
  5876. bench_stats_start(&count, &start);
  5877. do {
  5878. for (times = 0; times < numBlocks || pending > 0; ) {
  5879. bench_async_poll(&pending);
  5880. /* while free pending slots in queue, submit ops */
  5881. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5882. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5883. 0, &times, numBlocks, &pending)) {
  5884. ret = wc_Sha512_224Update(hash[i], bench_plain,
  5885. bench_size);
  5886. if (!bench_async_handle(&ret,
  5887. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5888. &times, &pending)) {
  5889. goto exit_sha512_224;
  5890. }
  5891. }
  5892. } /* for i */
  5893. RECORD_MULTI_VALUE_STATS();
  5894. } /* for times */
  5895. count += times;
  5896. times = 0;
  5897. do {
  5898. bench_async_poll(&pending);
  5899. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5900. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5901. 0, &times, numBlocks, &pending)) {
  5902. ret = wc_Sha512_224Final(hash[i], digest[i]);
  5903. if (!bench_async_handle(&ret,
  5904. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5905. &times, &pending)) {
  5906. goto exit_sha512_224;
  5907. }
  5908. }
  5909. } /* for i */
  5910. } while (pending > 0);
  5911. } while (bench_stats_check(start)
  5912. #ifdef MULTI_VALUE_STATISTICS
  5913. || runs < minimum_runs
  5914. #endif
  5915. );
  5916. }
  5917. else {
  5918. bench_stats_start(&count, &start);
  5919. do {
  5920. for (times = 0; times < numBlocks; times++) {
  5921. ret = wc_InitSha512_224_ex(hash[0], HEAP_HINT,
  5922. useDeviceID ? devId : INVALID_DEVID);
  5923. if (ret == 0)
  5924. ret = wc_Sha512_224Update(hash[0], bench_plain, bench_size);
  5925. if (ret == 0)
  5926. ret = wc_Sha512_224Final(hash[0], digest[0]);
  5927. if (ret != 0)
  5928. goto exit_sha512_224;
  5929. RECORD_MULTI_VALUE_STATS();
  5930. } /* for times */
  5931. count += times;
  5932. } while (bench_stats_check(start)
  5933. #ifdef MULTI_VALUE_STATISTICS
  5934. || runs < minimum_runs
  5935. #endif
  5936. );
  5937. }
  5938. exit_sha512_224:
  5939. bench_stats_sym_finish("SHA-512/224", useDeviceID, count, bench_size,
  5940. start, ret);
  5941. #ifdef MULTI_VALUE_STATISTICS
  5942. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5943. #endif
  5944. exit:
  5945. if (WC_ARRAY_OK(hash)) {
  5946. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5947. wc_Sha512_224Free(hash[i]);
  5948. }
  5949. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5950. }
  5951. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5952. }
  5953. #endif /* WOLFSSL_NOSHA512_224 && !FIPS ... */
  5954. #if !defined(WOLFSSL_NOSHA512_256) && \
  5955. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  5956. void bench_sha512_256(int useDeviceID)
  5957. {
  5958. WC_DECLARE_ARRAY(hash, wc_Sha512_256, BENCH_MAX_PENDING,
  5959. sizeof(wc_Sha512_256), HEAP_HINT);
  5960. double start;
  5961. int ret = 0, i, count = 0, times, pending = 0;
  5962. DECLARE_MULTI_VALUE_STATS_VARS()
  5963. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5964. WC_SHA512_256_DIGEST_SIZE, HEAP_HINT);
  5965. WC_CALLOC_ARRAY(hash, wc_Sha512_256, BENCH_MAX_PENDING,
  5966. sizeof(wc_Sha512_256), HEAP_HINT);
  5967. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5968. WC_SHA512_256_DIGEST_SIZE, HEAP_HINT);
  5969. if (digest_stream) {
  5970. /* init keys */
  5971. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5972. ret = wc_InitSha512_256_ex(hash[i], HEAP_HINT,
  5973. useDeviceID ? devId : INVALID_DEVID);
  5974. if (ret != 0) {
  5975. printf("InitSha512_256_ex failed, ret = %d\n", ret);
  5976. goto exit;
  5977. }
  5978. }
  5979. bench_stats_start(&count, &start);
  5980. do {
  5981. for (times = 0; times < numBlocks || pending > 0; ) {
  5982. bench_async_poll(&pending);
  5983. /* while free pending slots in queue, submit ops */
  5984. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5985. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5986. 0, &times, numBlocks, &pending)) {
  5987. ret = wc_Sha512_256Update(hash[i], bench_plain,
  5988. bench_size);
  5989. if (!bench_async_handle(&ret,
  5990. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5991. &times, &pending)) {
  5992. goto exit_sha512_256;
  5993. }
  5994. }
  5995. } /* for i */
  5996. RECORD_MULTI_VALUE_STATS();
  5997. } /* for times */
  5998. count += times;
  5999. times = 0;
  6000. do {
  6001. bench_async_poll(&pending);
  6002. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6003. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6004. 0, &times, numBlocks, &pending)) {
  6005. ret = wc_Sha512_256Final(hash[i], digest[i]);
  6006. if (!bench_async_handle(&ret,
  6007. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6008. &times, &pending)) {
  6009. goto exit_sha512_256;
  6010. }
  6011. }
  6012. } /* for i */
  6013. } while (pending > 0);
  6014. } while (bench_stats_check(start)
  6015. #ifdef MULTI_VALUE_STATISTICS
  6016. || runs < minimum_runs
  6017. #endif
  6018. );
  6019. }
  6020. else {
  6021. bench_stats_start(&count, &start);
  6022. do {
  6023. for (times = 0; times < numBlocks; times++) {
  6024. ret = wc_InitSha512_256_ex(hash[0], HEAP_HINT,
  6025. useDeviceID ? devId : INVALID_DEVID);
  6026. if (ret == 0)
  6027. ret = wc_Sha512_256Update(hash[0], bench_plain, bench_size);
  6028. if (ret == 0)
  6029. ret = wc_Sha512_256Final(hash[0], digest[0]);
  6030. if (ret != 0)
  6031. goto exit_sha512_256;
  6032. RECORD_MULTI_VALUE_STATS();
  6033. } /* for times */
  6034. count += times;
  6035. } while (bench_stats_check(start)
  6036. #ifdef MULTI_VALUE_STATISTICS
  6037. || runs < minimum_runs
  6038. #endif
  6039. );
  6040. }
  6041. exit_sha512_256:
  6042. bench_stats_sym_finish("SHA-512/256", useDeviceID, count, bench_size,
  6043. start, ret);
  6044. #ifdef MULTI_VALUE_STATISTICS
  6045. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6046. #endif
  6047. exit:
  6048. if (WC_ARRAY_OK(hash)) {
  6049. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6050. wc_Sha512_256Free(hash[i]);
  6051. }
  6052. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6053. }
  6054. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6055. }
  6056. #endif /* WOLFSSL_NOSHA512_256 && !FIPS ... */
  6057. #endif /* WOLFSSL_SHA512 */
  6058. #ifdef WOLFSSL_SHA3
  6059. #ifndef WOLFSSL_NOSHA3_224
  6060. void bench_sha3_224(int useDeviceID)
  6061. {
  6062. WC_DECLARE_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6063. sizeof(wc_Sha3), HEAP_HINT);
  6064. double start;
  6065. int ret = 0, i, count = 0, times, pending = 0;
  6066. DECLARE_MULTI_VALUE_STATS_VARS()
  6067. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6068. WC_SHA3_224_DIGEST_SIZE, HEAP_HINT);
  6069. WC_CALLOC_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6070. sizeof(wc_Sha3), HEAP_HINT);
  6071. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6072. WC_SHA3_224_DIGEST_SIZE, HEAP_HINT);
  6073. if (digest_stream) {
  6074. /* init keys */
  6075. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6076. ret = wc_InitSha3_224(hash[i], HEAP_HINT,
  6077. useDeviceID ? devId : INVALID_DEVID);
  6078. if (ret != 0) {
  6079. printf("InitSha3_224 failed, ret = %d\n", ret);
  6080. goto exit;
  6081. }
  6082. }
  6083. bench_stats_start(&count, &start);
  6084. do {
  6085. for (times = 0; times < numBlocks || pending > 0; ) {
  6086. bench_async_poll(&pending);
  6087. /* while free pending slots in queue, submit ops */
  6088. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6089. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6090. 0, &times, numBlocks, &pending)) {
  6091. ret = wc_Sha3_224_Update(hash[i], bench_plain,
  6092. bench_size);
  6093. if (!bench_async_handle(&ret,
  6094. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6095. &times, &pending)) {
  6096. goto exit_sha3_224;
  6097. }
  6098. }
  6099. } /* for i */
  6100. RECORD_MULTI_VALUE_STATS();
  6101. } /* for times */
  6102. count += times;
  6103. times = 0;
  6104. do {
  6105. bench_async_poll(&pending);
  6106. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6107. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6108. 0, &times, numBlocks, &pending)) {
  6109. ret = wc_Sha3_224_Final(hash[i], digest[i]);
  6110. if (!bench_async_handle(&ret,
  6111. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6112. &times, &pending)) {
  6113. goto exit_sha3_224;
  6114. }
  6115. }
  6116. } /* for i */
  6117. } while (pending > 0);
  6118. } while (bench_stats_check(start)
  6119. #ifdef MULTI_VALUE_STATISTICS
  6120. || runs < minimum_runs
  6121. #endif
  6122. );
  6123. }
  6124. else {
  6125. bench_stats_start(&count, &start);
  6126. do {
  6127. for (times = 0; times < numBlocks; times++) {
  6128. ret = wc_InitSha3_224(hash[0], HEAP_HINT,
  6129. useDeviceID ? devId : INVALID_DEVID);
  6130. if (ret == 0)
  6131. ret = wc_Sha3_224_Update(hash[0], bench_plain, bench_size);
  6132. if (ret == 0)
  6133. ret = wc_Sha3_224_Final(hash[0], digest[0]);
  6134. if (ret != 0)
  6135. goto exit_sha3_224;
  6136. RECORD_MULTI_VALUE_STATS();
  6137. } /* for times */
  6138. count += times;
  6139. } while (bench_stats_check(start)
  6140. #ifdef MULTI_VALUE_STATISTICS
  6141. || runs < minimum_runs
  6142. #endif
  6143. );
  6144. }
  6145. exit_sha3_224:
  6146. bench_stats_sym_finish("SHA3-224", useDeviceID, count, bench_size,
  6147. start, ret);
  6148. #ifdef MULTI_VALUE_STATISTICS
  6149. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6150. #endif
  6151. exit:
  6152. if (WC_ARRAY_OK(hash)) {
  6153. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6154. wc_Sha3_224_Free(hash[i]);
  6155. }
  6156. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6157. }
  6158. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6159. }
  6160. #endif /* WOLFSSL_NOSHA3_224 */
  6161. #ifndef WOLFSSL_NOSHA3_256
  6162. void bench_sha3_256(int useDeviceID)
  6163. {
  6164. WC_DECLARE_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6165. sizeof(wc_Sha3), HEAP_HINT);
  6166. double start;
  6167. DECLARE_MULTI_VALUE_STATS_VARS()
  6168. int ret = 0, i, count = 0, times, pending = 0;
  6169. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6170. WC_SHA3_256_DIGEST_SIZE, HEAP_HINT);
  6171. WC_CALLOC_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6172. sizeof(wc_Sha3), HEAP_HINT);
  6173. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6174. WC_SHA3_256_DIGEST_SIZE, HEAP_HINT);
  6175. if (digest_stream) {
  6176. /* init keys */
  6177. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6178. ret = wc_InitSha3_256(hash[i], HEAP_HINT,
  6179. useDeviceID ? devId : INVALID_DEVID);
  6180. if (ret != 0) {
  6181. printf("InitSha3_256 failed, ret = %d\n", ret);
  6182. goto exit;
  6183. }
  6184. }
  6185. bench_stats_start(&count, &start);
  6186. do {
  6187. for (times = 0; times < numBlocks || pending > 0; ) {
  6188. bench_async_poll(&pending);
  6189. /* while free pending slots in queue, submit ops */
  6190. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6191. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6192. 0, &times, numBlocks, &pending)) {
  6193. ret = wc_Sha3_256_Update(hash[i], bench_plain,
  6194. bench_size);
  6195. if (!bench_async_handle(&ret,
  6196. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6197. &times, &pending)) {
  6198. goto exit_sha3_256;
  6199. }
  6200. }
  6201. } /* for i */
  6202. RECORD_MULTI_VALUE_STATS();
  6203. } /* for times */
  6204. count += times;
  6205. times = 0;
  6206. do {
  6207. bench_async_poll(&pending);
  6208. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6209. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6210. 0, &times, numBlocks, &pending)) {
  6211. ret = wc_Sha3_256_Final(hash[i], digest[i]);
  6212. if (!bench_async_handle(&ret,
  6213. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6214. &times, &pending)) {
  6215. goto exit_sha3_256;
  6216. }
  6217. }
  6218. } /* for i */
  6219. } while (pending > 0);
  6220. } while (bench_stats_check(start)
  6221. #ifdef MULTI_VALUE_STATISTICS
  6222. || runs < minimum_runs
  6223. #endif
  6224. );
  6225. }
  6226. else {
  6227. bench_stats_start(&count, &start);
  6228. do {
  6229. for (times = 0; times < numBlocks; times++) {
  6230. ret = wc_InitSha3_256(hash[0], HEAP_HINT,
  6231. useDeviceID ? devId : INVALID_DEVID);
  6232. if (ret == 0)
  6233. ret = wc_Sha3_256_Update(hash[0], bench_plain, bench_size);
  6234. if (ret == 0)
  6235. ret = wc_Sha3_256_Final(hash[0], digest[0]);
  6236. if (ret != 0)
  6237. goto exit_sha3_256;
  6238. RECORD_MULTI_VALUE_STATS();
  6239. } /* for times */
  6240. count += times;
  6241. } while (bench_stats_check(start)
  6242. #ifdef MULTI_VALUE_STATISTICS
  6243. || runs < minimum_runs
  6244. #endif
  6245. );
  6246. }
  6247. exit_sha3_256:
  6248. bench_stats_sym_finish("SHA3-256", useDeviceID, count, bench_size,
  6249. start, ret);
  6250. #ifdef MULTI_VALUE_STATISTICS
  6251. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6252. #endif
  6253. exit:
  6254. if (WC_ARRAY_OK(hash)) {
  6255. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6256. wc_Sha3_256_Free(hash[i]);
  6257. }
  6258. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6259. }
  6260. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6261. }
  6262. #endif /* WOLFSSL_NOSHA3_256 */
  6263. #ifndef WOLFSSL_NOSHA3_384
  6264. void bench_sha3_384(int useDeviceID)
  6265. {
  6266. WC_DECLARE_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6267. sizeof(wc_Sha3), HEAP_HINT);
  6268. double start;
  6269. int ret = 0, i, count = 0, times, pending = 0;
  6270. DECLARE_MULTI_VALUE_STATS_VARS()
  6271. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6272. WC_SHA3_384_DIGEST_SIZE, HEAP_HINT);
  6273. WC_CALLOC_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6274. sizeof(wc_Sha3), HEAP_HINT);
  6275. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6276. WC_SHA3_384_DIGEST_SIZE, HEAP_HINT);
  6277. if (digest_stream) {
  6278. /* init keys */
  6279. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6280. ret = wc_InitSha3_384(hash[i], HEAP_HINT,
  6281. useDeviceID ? devId : INVALID_DEVID);
  6282. if (ret != 0) {
  6283. printf("InitSha3_384 failed, ret = %d\n", ret);
  6284. goto exit;
  6285. }
  6286. }
  6287. bench_stats_start(&count, &start);
  6288. do {
  6289. for (times = 0; times < numBlocks || pending > 0; ) {
  6290. bench_async_poll(&pending);
  6291. /* while free pending slots in queue, submit ops */
  6292. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6293. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6294. 0, &times, numBlocks, &pending)) {
  6295. ret = wc_Sha3_384_Update(hash[i], bench_plain,
  6296. bench_size);
  6297. if (!bench_async_handle(&ret,
  6298. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6299. &times, &pending)) {
  6300. goto exit_sha3_384;
  6301. }
  6302. }
  6303. } /* for i */
  6304. RECORD_MULTI_VALUE_STATS();
  6305. } /* for times */
  6306. count += times;
  6307. times = 0;
  6308. do {
  6309. bench_async_poll(&pending);
  6310. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6311. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6312. 0, &times, numBlocks, &pending)) {
  6313. ret = wc_Sha3_384_Final(hash[i], digest[i]);
  6314. if (!bench_async_handle(&ret,
  6315. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6316. &times, &pending)) {
  6317. goto exit_sha3_384;
  6318. }
  6319. }
  6320. } /* for i */
  6321. } while (pending > 0);
  6322. } while (bench_stats_check(start)
  6323. #ifdef MULTI_VALUE_STATISTICS
  6324. || runs < minimum_runs
  6325. #endif
  6326. );
  6327. }
  6328. else {
  6329. bench_stats_start(&count, &start);
  6330. do {
  6331. for (times = 0; times < numBlocks; times++) {
  6332. ret = wc_InitSha3_384(hash[0], HEAP_HINT,
  6333. useDeviceID ? devId : INVALID_DEVID);
  6334. if (ret == 0)
  6335. ret = wc_Sha3_384_Update(hash[0], bench_plain, bench_size);
  6336. if (ret == 0)
  6337. ret = wc_Sha3_384_Final(hash[0], digest[0]);
  6338. if (ret != 0)
  6339. goto exit_sha3_384;
  6340. RECORD_MULTI_VALUE_STATS();
  6341. } /* for times */
  6342. count += times;
  6343. } while (bench_stats_check(start)
  6344. #ifdef MULTI_VALUE_STATISTICS
  6345. || runs < minimum_runs
  6346. #endif
  6347. );
  6348. }
  6349. exit_sha3_384:
  6350. bench_stats_sym_finish("SHA3-384", useDeviceID, count, bench_size,
  6351. start, ret);
  6352. #ifdef MULTI_VALUE_STATISTICS
  6353. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6354. #endif
  6355. exit:
  6356. if (WC_ARRAY_OK(hash)) {
  6357. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6358. wc_Sha3_384_Free(hash[i]);
  6359. }
  6360. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6361. }
  6362. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6363. }
  6364. #endif /* WOLFSSL_NOSHA3_384 */
  6365. #ifndef WOLFSSL_NOSHA3_512
  6366. void bench_sha3_512(int useDeviceID)
  6367. {
  6368. WC_DECLARE_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6369. sizeof(wc_Sha3), HEAP_HINT);
  6370. double start;
  6371. int ret = 0, i, count = 0, times, pending = 0;
  6372. DECLARE_MULTI_VALUE_STATS_VARS()
  6373. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6374. WC_SHA3_512_DIGEST_SIZE, HEAP_HINT);
  6375. WC_CALLOC_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6376. sizeof(wc_Sha3), HEAP_HINT);
  6377. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6378. WC_SHA3_512_DIGEST_SIZE, HEAP_HINT);
  6379. if (digest_stream) {
  6380. /* init keys */
  6381. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6382. ret = wc_InitSha3_512(hash[i], HEAP_HINT,
  6383. useDeviceID ? devId : INVALID_DEVID);
  6384. if (ret != 0) {
  6385. printf("InitSha3_512 failed, ret = %d\n", ret);
  6386. goto exit;
  6387. }
  6388. }
  6389. bench_stats_start(&count, &start);
  6390. do {
  6391. for (times = 0; times < numBlocks || pending > 0; ) {
  6392. bench_async_poll(&pending);
  6393. /* while free pending slots in queue, submit ops */
  6394. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6395. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6396. 0, &times, numBlocks, &pending)) {
  6397. ret = wc_Sha3_512_Update(hash[i], bench_plain,
  6398. bench_size);
  6399. if (!bench_async_handle(&ret,
  6400. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6401. &times, &pending)) {
  6402. goto exit_sha3_512;
  6403. }
  6404. }
  6405. } /* for i */
  6406. RECORD_MULTI_VALUE_STATS();
  6407. } /* for times */
  6408. count += times;
  6409. times = 0;
  6410. do {
  6411. bench_async_poll(&pending);
  6412. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6413. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6414. 0, &times, numBlocks, &pending)) {
  6415. ret = wc_Sha3_512_Final(hash[i], digest[i]);
  6416. if (!bench_async_handle(&ret,
  6417. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6418. &times, &pending)) {
  6419. goto exit_sha3_512;
  6420. }
  6421. }
  6422. } /* for i */
  6423. } while (pending > 0);
  6424. } while (bench_stats_check(start)
  6425. #ifdef MULTI_VALUE_STATISTICS
  6426. || runs < minimum_runs
  6427. #endif
  6428. );
  6429. }
  6430. else {
  6431. bench_stats_start(&count, &start);
  6432. do {
  6433. for (times = 0; times < numBlocks; times++) {
  6434. ret = wc_InitSha3_512(hash[0], HEAP_HINT,
  6435. useDeviceID ? devId : INVALID_DEVID);
  6436. if (ret == 0)
  6437. ret = wc_Sha3_512_Update(hash[0], bench_plain, bench_size);
  6438. if (ret == 0)
  6439. ret = wc_Sha3_512_Final(hash[0], digest[0]);
  6440. if (ret != 0)
  6441. goto exit_sha3_512;
  6442. RECORD_MULTI_VALUE_STATS();
  6443. } /* for times */
  6444. count += times;
  6445. } while (bench_stats_check(start)
  6446. #ifdef MULTI_VALUE_STATISTICS
  6447. || runs < minimum_runs
  6448. #endif
  6449. );
  6450. }
  6451. exit_sha3_512:
  6452. bench_stats_sym_finish("SHA3-512", useDeviceID, count, bench_size,
  6453. start, ret);
  6454. #ifdef MULTI_VALUE_STATISTICS
  6455. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6456. #endif
  6457. exit:
  6458. if (WC_ARRAY_OK(hash)) {
  6459. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6460. wc_Sha3_512_Free(hash[i]);
  6461. }
  6462. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6463. }
  6464. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6465. }
  6466. #endif /* WOLFSSL_NOSHA3_512 */
  6467. #ifdef WOLFSSL_SHAKE128
  6468. void bench_shake128(int useDeviceID)
  6469. {
  6470. WC_DECLARE_ARRAY(hash, wc_Shake, BENCH_MAX_PENDING,
  6471. sizeof(wc_Shake), HEAP_HINT);
  6472. double start;
  6473. int ret = 0, i, count = 0, times, pending = 0;
  6474. DECLARE_MULTI_VALUE_STATS_VARS()
  6475. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6476. WC_SHA3_128_BLOCK_SIZE, HEAP_HINT);
  6477. WC_CALLOC_ARRAY(hash, wc_Shake, BENCH_MAX_PENDING,
  6478. sizeof(wc_Shake), HEAP_HINT);
  6479. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6480. WC_SHA3_128_BLOCK_SIZE, HEAP_HINT);
  6481. if (digest_stream) {
  6482. /* init keys */
  6483. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6484. ret = wc_InitShake128(hash[i], HEAP_HINT,
  6485. useDeviceID ? devId : INVALID_DEVID);
  6486. if (ret != 0) {
  6487. printf("InitShake128 failed, ret = %d\n", ret);
  6488. goto exit;
  6489. }
  6490. }
  6491. bench_stats_start(&count, &start);
  6492. do {
  6493. for (times = 0; times < numBlocks || pending > 0; ) {
  6494. bench_async_poll(&pending);
  6495. /* while free pending slots in queue, submit ops */
  6496. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6497. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6498. 0, &times, numBlocks, &pending)) {
  6499. ret = wc_Shake128_Update(hash[i], bench_plain,
  6500. bench_size);
  6501. if (!bench_async_handle(&ret,
  6502. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6503. &times, &pending)) {
  6504. goto exit_shake128;
  6505. }
  6506. }
  6507. } /* for i */
  6508. RECORD_MULTI_VALUE_STATS();
  6509. } /* for times */
  6510. count += times;
  6511. times = 0;
  6512. do {
  6513. bench_async_poll(&pending);
  6514. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6515. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6516. 0, &times, numBlocks, &pending)) {
  6517. ret = wc_Shake128_Final(hash[i], digest[i],
  6518. WC_SHA3_128_BLOCK_SIZE);
  6519. if (!bench_async_handle(&ret,
  6520. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6521. &times, &pending)) {
  6522. goto exit_shake128;
  6523. }
  6524. }
  6525. } /* for i */
  6526. } while (pending > 0);
  6527. } while (bench_stats_check(start)
  6528. #ifdef MULTI_VALUE_STATISTICS
  6529. || runs < minimum_runs
  6530. #endif
  6531. );
  6532. }
  6533. else {
  6534. bench_stats_start(&count, &start);
  6535. do {
  6536. for (times = 0; times < numBlocks; times++) {
  6537. ret = wc_InitShake128(hash[0], HEAP_HINT,
  6538. useDeviceID ? devId : INVALID_DEVID);
  6539. if (ret == 0)
  6540. ret = wc_Shake128_Update(hash[0], bench_plain, bench_size);
  6541. if (ret == 0)
  6542. ret = wc_Shake128_Final(hash[0], digest[0],
  6543. WC_SHA3_128_BLOCK_SIZE);
  6544. if (ret != 0)
  6545. goto exit_shake128;
  6546. RECORD_MULTI_VALUE_STATS();
  6547. } /* for times */
  6548. count += times;
  6549. } while (bench_stats_check(start)
  6550. #ifdef MULTI_VALUE_STATISTICS
  6551. || runs < minimum_runs
  6552. #endif
  6553. );
  6554. }
  6555. exit_shake128:
  6556. bench_stats_sym_finish("SHAKE128", useDeviceID, count, bench_size,
  6557. start, ret);
  6558. #ifdef MULTI_VALUE_STATISTICS
  6559. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6560. #endif
  6561. exit:
  6562. if (WC_ARRAY_OK(hash)) {
  6563. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6564. wc_Shake128_Free(hash[i]);
  6565. }
  6566. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6567. }
  6568. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6569. }
  6570. #endif /* WOLFSSL_SHAKE128 */
  6571. #ifdef WOLFSSL_SHAKE256
  6572. void bench_shake256(int useDeviceID)
  6573. {
  6574. WC_DECLARE_ARRAY(hash, wc_Shake, BENCH_MAX_PENDING,
  6575. sizeof(wc_Shake), HEAP_HINT);
  6576. double start;
  6577. int ret = 0, i, count = 0, times, pending = 0;
  6578. DECLARE_MULTI_VALUE_STATS_VARS()
  6579. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6580. WC_SHA3_256_BLOCK_SIZE, HEAP_HINT);
  6581. WC_CALLOC_ARRAY(hash, wc_Shake, BENCH_MAX_PENDING,
  6582. sizeof(wc_Shake), HEAP_HINT);
  6583. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6584. WC_SHA3_256_BLOCK_SIZE, HEAP_HINT);
  6585. if (digest_stream) {
  6586. /* init keys */
  6587. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6588. ret = wc_InitShake256(hash[i], HEAP_HINT,
  6589. useDeviceID ? devId : INVALID_DEVID);
  6590. if (ret != 0) {
  6591. printf("InitShake256 failed, ret = %d\n", ret);
  6592. goto exit;
  6593. }
  6594. }
  6595. bench_stats_start(&count, &start);
  6596. do {
  6597. for (times = 0; times < numBlocks || pending > 0; ) {
  6598. bench_async_poll(&pending);
  6599. /* while free pending slots in queue, submit ops */
  6600. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6601. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6602. 0, &times, numBlocks, &pending)) {
  6603. ret = wc_Shake256_Update(hash[i], bench_plain,
  6604. bench_size);
  6605. if (!bench_async_handle(&ret,
  6606. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6607. &times, &pending)) {
  6608. goto exit_shake256;
  6609. }
  6610. }
  6611. } /* for i */
  6612. RECORD_MULTI_VALUE_STATS();
  6613. } /* for times */
  6614. count += times;
  6615. times = 0;
  6616. do {
  6617. bench_async_poll(&pending);
  6618. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6619. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6620. 0, &times, numBlocks, &pending)) {
  6621. ret = wc_Shake256_Final(hash[i], digest[i],
  6622. WC_SHA3_256_BLOCK_SIZE);
  6623. if (!bench_async_handle(&ret,
  6624. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6625. &times, &pending)) {
  6626. goto exit_shake256;
  6627. }
  6628. }
  6629. } /* for i */
  6630. } while (pending > 0);
  6631. } while (bench_stats_check(start)
  6632. #ifdef MULTI_VALUE_STATISTICS
  6633. || runs < minimum_runs
  6634. #endif
  6635. );
  6636. }
  6637. else {
  6638. bench_stats_start(&count, &start);
  6639. do {
  6640. for (times = 0; times < numBlocks; times++) {
  6641. ret = wc_InitShake256(hash[0], HEAP_HINT,
  6642. useDeviceID ? devId : INVALID_DEVID);
  6643. if (ret == 0)
  6644. ret = wc_Shake256_Update(hash[0], bench_plain, bench_size);
  6645. if (ret == 0)
  6646. ret = wc_Shake256_Final(hash[0], digest[0],
  6647. WC_SHA3_256_BLOCK_SIZE);
  6648. if (ret != 0)
  6649. goto exit_shake256;
  6650. RECORD_MULTI_VALUE_STATS();
  6651. } /* for times */
  6652. count += times;
  6653. } while (bench_stats_check(start)
  6654. #ifdef MULTI_VALUE_STATISTICS
  6655. || runs < minimum_runs
  6656. #endif
  6657. );
  6658. }
  6659. exit_shake256:
  6660. bench_stats_sym_finish("SHAKE256", useDeviceID, count, bench_size,
  6661. start, ret);
  6662. #ifdef MULTI_VALUE_STATISTICS
  6663. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6664. #endif
  6665. exit:
  6666. if (WC_ARRAY_OK(hash)) {
  6667. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6668. wc_Shake256_Free(hash[i]);
  6669. }
  6670. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6671. }
  6672. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6673. }
  6674. #endif /* WOLFSSL_SHAKE256 */
  6675. #endif
  6676. #ifdef WOLFSSL_SM3
  6677. void bench_sm3(int useDeviceID)
  6678. {
  6679. WC_DECLARE_ARRAY(hash, wc_Sm3, BENCH_MAX_PENDING,
  6680. sizeof(wc_Sm3), HEAP_HINT);
  6681. double start;
  6682. int ret = 0, i, count = 0, times, pending = 0;
  6683. DECLARE_MULTI_VALUE_STATS_VARS()
  6684. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SM3_DIGEST_SIZE,
  6685. HEAP_HINT);
  6686. WC_CALLOC_ARRAY(hash, wc_Sm3, BENCH_MAX_PENDING,
  6687. sizeof(wc_Sm3), HEAP_HINT);
  6688. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SM3_DIGEST_SIZE,
  6689. HEAP_HINT);
  6690. if (digest_stream) {
  6691. /* init keys */
  6692. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6693. ret = wc_InitSm3(hash[i], HEAP_HINT,
  6694. useDeviceID ? devId: INVALID_DEVID);
  6695. if (ret != 0) {
  6696. printf("InitSm3 failed, ret = %d\n", ret);
  6697. goto exit;
  6698. }
  6699. }
  6700. bench_stats_start(&count, &start);
  6701. do {
  6702. for (times = 0; times < numBlocks || pending > 0; ) {
  6703. bench_async_poll(&pending);
  6704. /* while free pending slots in queue, submit ops */
  6705. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6706. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6707. 0, &times, numBlocks, &pending)) {
  6708. ret = wc_Sm3Update(hash[i], bench_plain,
  6709. bench_size);
  6710. if (!bench_async_handle(&ret,
  6711. BENCH_ASYNC_GET_DEV(hash[i]), 0, &times, &pending)) {
  6712. goto exit_sm3;
  6713. }
  6714. }
  6715. } /* for i */
  6716. RECORD_MULTI_VALUE_STATS();
  6717. } /* for times */
  6718. count += times;
  6719. times = 0;
  6720. do {
  6721. bench_async_poll(&pending);
  6722. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6723. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6724. 0, &times, numBlocks, &pending)) {
  6725. ret = wc_Sm3Final(hash[i], digest[i]);
  6726. if (!bench_async_handle(&ret,
  6727. BENCH_ASYNC_GET_DEV(hash[i]), 0, &times, &pending)) {
  6728. goto exit_sm3;
  6729. }
  6730. }
  6731. } /* for i */
  6732. } while (pending > 0);
  6733. } while (bench_stats_check(start)
  6734. #ifdef MULTI_VALUE_STATISTICS
  6735. || runs < minimum_runs
  6736. #endif
  6737. );
  6738. }
  6739. else {
  6740. bench_stats_start(&count, &start);
  6741. do {
  6742. for (times = 0; times < numBlocks; times++) {
  6743. ret = wc_InitSm3(hash, HEAP_HINT,
  6744. useDeviceID ? devId: INVALID_DEVID);
  6745. if (ret == 0)
  6746. ret = wc_Sm3Update(hash, bench_plain, bench_size);
  6747. if (ret == 0)
  6748. ret = wc_Sm3Final(hash, digest[0]);
  6749. if (ret != 0)
  6750. goto exit_sm3;
  6751. RECORD_MULTI_VALUE_STATS();
  6752. } /* for times */
  6753. count += times;
  6754. } while (bench_stats_check(start)
  6755. #ifdef MULTI_VALUE_STATISTICS
  6756. || runs < minimum_runs
  6757. #endif
  6758. );
  6759. }
  6760. exit_sm3:
  6761. bench_stats_sym_finish("SM3", useDeviceID, count, bench_size, start, ret);
  6762. #ifdef MULTI_VALUE_STATISTICS
  6763. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6764. #endif
  6765. exit:
  6766. if (WC_ARRAY_OK(hash)) {
  6767. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6768. wc_Sm3Free(hash[i]);
  6769. }
  6770. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6771. }
  6772. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6773. }
  6774. #endif
  6775. #ifdef WOLFSSL_RIPEMD
  6776. void bench_ripemd(void)
  6777. {
  6778. RipeMd hash;
  6779. byte digest[RIPEMD_DIGEST_SIZE];
  6780. double start;
  6781. int i, count, ret = 0;
  6782. DECLARE_MULTI_VALUE_STATS_VARS()
  6783. if (digest_stream) {
  6784. ret = wc_InitRipeMd(&hash);
  6785. if (ret != 0) {
  6786. printf("wc_InitRipeMd failed, retval %d\n", ret);
  6787. return;
  6788. }
  6789. bench_stats_start(&count, &start);
  6790. do {
  6791. for (i = 0; i < numBlocks; i++) {
  6792. ret = wc_RipeMdUpdate(&hash, bench_plain, bench_size);
  6793. if (ret != 0) {
  6794. printf("wc_RipeMdUpdate failed, retval %d\n", ret);
  6795. return;
  6796. }
  6797. RECORD_MULTI_VALUE_STATS();
  6798. }
  6799. ret = wc_RipeMdFinal(&hash, digest);
  6800. if (ret != 0) {
  6801. printf("wc_RipeMdFinal failed, retval %d\n", ret);
  6802. return;
  6803. }
  6804. count += i;
  6805. } while (bench_stats_check(start)
  6806. #ifdef MULTI_VALUE_STATISTICS
  6807. || runs < minimum_runs
  6808. #endif
  6809. );
  6810. }
  6811. else {
  6812. bench_stats_start(&count, &start);
  6813. do {
  6814. for (i = 0; i < numBlocks; i++) {
  6815. ret = wc_InitRipeMd(&hash);
  6816. if (ret != 0) {
  6817. printf("wc_InitRipeMd failed, retval %d\n", ret);
  6818. return;
  6819. }
  6820. ret = wc_RipeMdUpdate(&hash, bench_plain, bench_size);
  6821. if (ret != 0) {
  6822. printf("wc_RipeMdUpdate failed, retval %d\n", ret);
  6823. return;
  6824. }
  6825. ret = wc_RipeMdFinal(&hash, digest);
  6826. if (ret != 0) {
  6827. printf("wc_RipeMdFinal failed, retval %d\n", ret);
  6828. return;
  6829. }
  6830. RECORD_MULTI_VALUE_STATS();
  6831. }
  6832. count += i;
  6833. } while (bench_stats_check(start)
  6834. #ifdef MULTI_VALUE_STATISTICS
  6835. || runs < minimum_runs
  6836. #endif
  6837. );
  6838. }
  6839. bench_stats_sym_finish("RIPEMD", 0, count, bench_size, start, ret);
  6840. #ifdef MULTI_VALUE_STATISTICS
  6841. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6842. #endif
  6843. return;
  6844. }
  6845. #endif
  6846. #ifdef HAVE_BLAKE2
  6847. void bench_blake2b(void)
  6848. {
  6849. Blake2b b2b;
  6850. byte digest[64];
  6851. double start;
  6852. int ret = 0, i, count;
  6853. DECLARE_MULTI_VALUE_STATS_VARS()
  6854. if (digest_stream) {
  6855. ret = wc_InitBlake2b(&b2b, 64);
  6856. if (ret != 0) {
  6857. printf("InitBlake2b failed, ret = %d\n", ret);
  6858. return;
  6859. }
  6860. bench_stats_start(&count, &start);
  6861. do {
  6862. for (i = 0; i < numBlocks; i++) {
  6863. ret = wc_Blake2bUpdate(&b2b, bench_plain, bench_size);
  6864. if (ret != 0) {
  6865. printf("Blake2bUpdate failed, ret = %d\n", ret);
  6866. return;
  6867. }
  6868. RECORD_MULTI_VALUE_STATS();
  6869. }
  6870. ret = wc_Blake2bFinal(&b2b, digest, 64);
  6871. if (ret != 0) {
  6872. printf("Blake2bFinal failed, ret = %d\n", ret);
  6873. return;
  6874. }
  6875. count += i;
  6876. } while (bench_stats_check(start)
  6877. #ifdef MULTI_VALUE_STATISTICS
  6878. || runs < minimum_runs
  6879. #endif
  6880. );
  6881. }
  6882. else {
  6883. bench_stats_start(&count, &start);
  6884. do {
  6885. for (i = 0; i < numBlocks; i++) {
  6886. ret = wc_InitBlake2b(&b2b, 64);
  6887. if (ret != 0) {
  6888. printf("InitBlake2b failed, ret = %d\n", ret);
  6889. return;
  6890. }
  6891. ret = wc_Blake2bUpdate(&b2b, bench_plain, bench_size);
  6892. if (ret != 0) {
  6893. printf("Blake2bUpdate failed, ret = %d\n", ret);
  6894. return;
  6895. }
  6896. ret = wc_Blake2bFinal(&b2b, digest, 64);
  6897. if (ret != 0) {
  6898. printf("Blake2bFinal failed, ret = %d\n", ret);
  6899. return;
  6900. }
  6901. RECORD_MULTI_VALUE_STATS();
  6902. }
  6903. count += i;
  6904. } while (bench_stats_check(start)
  6905. #ifdef MULTI_VALUE_STATISTICS
  6906. || runs < minimum_runs
  6907. #endif
  6908. );
  6909. }
  6910. bench_stats_sym_finish("BLAKE2b", 0, count, bench_size, start, ret);
  6911. #ifdef MULTI_VALUE_STATISTICS
  6912. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6913. #endif
  6914. }
  6915. #endif
  6916. #if defined(HAVE_BLAKE2S)
  6917. void bench_blake2s(void)
  6918. {
  6919. Blake2s b2s;
  6920. byte digest[32];
  6921. double start;
  6922. int ret = 0, i, count;
  6923. DECLARE_MULTI_VALUE_STATS_VARS()
  6924. if (digest_stream) {
  6925. ret = wc_InitBlake2s(&b2s, 32);
  6926. if (ret != 0) {
  6927. printf("InitBlake2s failed, ret = %d\n", ret);
  6928. return;
  6929. }
  6930. bench_stats_start(&count, &start);
  6931. do {
  6932. for (i = 0; i < numBlocks; i++) {
  6933. ret = wc_Blake2sUpdate(&b2s, bench_plain, bench_size);
  6934. if (ret != 0) {
  6935. printf("Blake2sUpdate failed, ret = %d\n", ret);
  6936. return;
  6937. }
  6938. RECORD_MULTI_VALUE_STATS();
  6939. }
  6940. ret = wc_Blake2sFinal(&b2s, digest, 32);
  6941. if (ret != 0) {
  6942. printf("Blake2sFinal failed, ret = %d\n", ret);
  6943. return;
  6944. }
  6945. count += i;
  6946. } while (bench_stats_check(start)
  6947. #ifdef MULTI_VALUE_STATISTICS
  6948. || runs < minimum_runs
  6949. #endif
  6950. );
  6951. }
  6952. else {
  6953. bench_stats_start(&count, &start);
  6954. do {
  6955. for (i = 0; i < numBlocks; i++) {
  6956. ret = wc_InitBlake2s(&b2s, 32);
  6957. if (ret != 0) {
  6958. printf("InitBlake2b failed, ret = %d\n", ret);
  6959. return;
  6960. }
  6961. ret = wc_Blake2sUpdate(&b2s, bench_plain, bench_size);
  6962. if (ret != 0) {
  6963. printf("Blake2bUpdate failed, ret = %d\n", ret);
  6964. return;
  6965. }
  6966. ret = wc_Blake2sFinal(&b2s, digest, 32);
  6967. if (ret != 0) {
  6968. printf("Blake2sFinal failed, ret = %d\n", ret);
  6969. return;
  6970. }
  6971. RECORD_MULTI_VALUE_STATS();
  6972. }
  6973. count += i;
  6974. } while (bench_stats_check(start)
  6975. #ifdef MULTI_VALUE_STATISTICS
  6976. || runs < minimum_runs
  6977. #endif
  6978. );
  6979. }
  6980. bench_stats_sym_finish("BLAKE2s", 0, count, bench_size, start, ret);
  6981. #ifdef MULTI_VALUE_STATISTICS
  6982. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6983. #endif
  6984. }
  6985. #endif
  6986. #ifdef WOLFSSL_CMAC
  6987. static void bench_cmac_helper(word32 keySz, const char* outMsg, int useDeviceID)
  6988. {
  6989. Cmac cmac;
  6990. byte digest[AES_BLOCK_SIZE];
  6991. word32 digestSz = sizeof(digest);
  6992. double start;
  6993. int ret, i, count;
  6994. DECLARE_MULTI_VALUE_STATS_VARS()
  6995. #ifdef WOLFSSL_SECO_CAAM
  6996. unsigned int keyID;
  6997. int keyGroup = 1; /* group one was chosen arbitrarily */
  6998. int keyInfo = CAAM_KEY_TRANSIENT;
  6999. int keyType = CAAM_KEYTYPE_AES128;
  7000. byte pubKey[AES_256_KEY_SIZE];
  7001. if (keySz == AES_256_KEY_SIZE) {
  7002. keyType = CAAM_KEYTYPE_AES256;
  7003. }
  7004. if (useDeviceID &&
  7005. wc_SECO_GenerateKey(CAAM_GENERATE_KEY, keyGroup, pubKey, 0, keyType,
  7006. keyInfo, &keyID) != 0) {
  7007. printf("Error generating key in hsm\n");
  7008. return;
  7009. }
  7010. #endif
  7011. (void)useDeviceID;
  7012. bench_stats_start(&count, &start);
  7013. do {
  7014. #ifdef HAVE_FIPS
  7015. ret = wc_InitCmac(&cmac, bench_key, keySz, WC_CMAC_AES, NULL);
  7016. #else
  7017. ret = wc_InitCmac_ex(&cmac, bench_key, keySz, WC_CMAC_AES, NULL,
  7018. HEAP_HINT, useDeviceID ? devId : INVALID_DEVID);
  7019. #endif
  7020. if (ret != 0) {
  7021. printf("InitCmac failed, ret = %d\n", ret);
  7022. return;
  7023. }
  7024. #ifdef WOLFSSL_SECO_CAAM
  7025. if (useDeviceID) {
  7026. wc_SECO_CMACSetKeyID(&cmac, keyID);
  7027. }
  7028. #endif
  7029. for (i = 0; i < numBlocks; i++) {
  7030. ret = wc_CmacUpdate(&cmac, bench_plain, bench_size);
  7031. if (ret != 0) {
  7032. printf("CmacUpdate failed, ret = %d\n", ret);
  7033. return;
  7034. }
  7035. RECORD_MULTI_VALUE_STATS();
  7036. }
  7037. /* Note: final force zero's the Cmac struct */
  7038. ret = wc_CmacFinal(&cmac, digest, &digestSz);
  7039. if (ret != 0) {
  7040. printf("CmacFinal failed, ret = %d\n", ret);
  7041. return;
  7042. }
  7043. count += i;
  7044. } while (bench_stats_check(start)
  7045. #ifdef MULTI_VALUE_STATISTICS
  7046. || runs < minimum_runs
  7047. #endif
  7048. );
  7049. bench_stats_sym_finish(outMsg, useDeviceID, count, bench_size, start, ret);
  7050. #ifdef MULTI_VALUE_STATISTICS
  7051. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7052. #endif
  7053. }
  7054. void bench_cmac(int useDeviceID)
  7055. {
  7056. #ifdef WOLFSSL_AES_128
  7057. bench_cmac_helper(16, "AES-128-CMAC", useDeviceID);
  7058. #endif
  7059. #ifdef WOLFSSL_AES_256
  7060. bench_cmac_helper(32, "AES-256-CMAC", useDeviceID);
  7061. #endif
  7062. }
  7063. #endif /* WOLFSSL_CMAC */
  7064. #ifdef HAVE_SCRYPT
  7065. void bench_scrypt(void)
  7066. {
  7067. byte derived[64];
  7068. double start;
  7069. int ret, i, count;
  7070. DECLARE_MULTI_VALUE_STATS_VARS()
  7071. bench_stats_start(&count, &start);
  7072. do {
  7073. for (i = 0; i < scryptCnt; i++) {
  7074. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  7075. (byte*)"SodiumChloride", 14, 14, 8, 1,
  7076. sizeof(derived));
  7077. if (ret != 0) {
  7078. printf("scrypt failed, ret = %d\n", ret);
  7079. goto exit;
  7080. }
  7081. RECORD_MULTI_VALUE_STATS();
  7082. }
  7083. count += i;
  7084. } while (bench_stats_check(start)
  7085. #ifdef MULTI_VALUE_STATISTICS
  7086. || runs < minimum_runs
  7087. #endif
  7088. );
  7089. exit:
  7090. bench_stats_asym_finish("scrypt", 17, "", 0, count, start, ret);
  7091. #ifdef MULTI_VALUE_STATISTICS
  7092. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7093. #endif
  7094. }
  7095. #endif /* HAVE_SCRYPT */
  7096. #ifndef NO_HMAC
  7097. static void bench_hmac(int useDeviceID, int type, int digestSz,
  7098. const byte* key, word32 keySz, const char* label)
  7099. {
  7100. WC_DECLARE_ARRAY(hmac, Hmac, BENCH_MAX_PENDING,
  7101. sizeof(Hmac), HEAP_HINT);
  7102. double start;
  7103. int ret = 0, i, count = 0, times, pending = 0;
  7104. DECLARE_MULTI_VALUE_STATS_VARS()
  7105. #ifdef WOLFSSL_ASYNC_CRYPT
  7106. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  7107. WC_MAX_DIGEST_SIZE, HEAP_HINT);
  7108. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  7109. WC_MAX_DIGEST_SIZE, HEAP_HINT);
  7110. #else
  7111. byte digest[BENCH_MAX_PENDING][WC_MAX_DIGEST_SIZE];
  7112. #endif
  7113. (void)digestSz;
  7114. WC_CALLOC_ARRAY(hmac, Hmac, BENCH_MAX_PENDING,
  7115. sizeof(Hmac), HEAP_HINT);
  7116. /* init keys */
  7117. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7118. ret = wc_HmacInit(hmac[i], HEAP_HINT,
  7119. useDeviceID ? devId : INVALID_DEVID);
  7120. if (ret != 0) {
  7121. printf("wc_HmacInit failed for %s, ret = %d\n", label, ret);
  7122. goto exit;
  7123. }
  7124. ret = wc_HmacSetKey(hmac[i], type, key, keySz);
  7125. if (ret != 0) {
  7126. printf("wc_HmacSetKey failed for %s, ret = %d\n", label, ret);
  7127. goto exit;
  7128. }
  7129. }
  7130. bench_stats_start(&count, &start);
  7131. do {
  7132. for (times = 0; times < numBlocks || pending > 0; ) {
  7133. bench_async_poll(&pending);
  7134. /* while free pending slots in queue, submit ops */
  7135. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7136. if (bench_async_check(&ret,
  7137. BENCH_ASYNC_GET_DEV(hmac[i]), 0,
  7138. &times, numBlocks, &pending)) {
  7139. ret = wc_HmacUpdate(hmac[i], bench_plain, bench_size);
  7140. if (!bench_async_handle(&ret,
  7141. BENCH_ASYNC_GET_DEV(hmac[i]),
  7142. 0, &times, &pending)) {
  7143. goto exit_hmac;
  7144. }
  7145. }
  7146. } /* for i */
  7147. } /* for times */
  7148. count += times;
  7149. times = 0;
  7150. do {
  7151. bench_async_poll(&pending);
  7152. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7153. if (bench_async_check(&ret,
  7154. BENCH_ASYNC_GET_DEV(hmac[i]), 0,
  7155. &times, numBlocks, &pending)) {
  7156. ret = wc_HmacFinal(hmac[i], digest[i]);
  7157. if (!bench_async_handle(&ret,
  7158. BENCH_ASYNC_GET_DEV(hmac[i]),
  7159. 0, &times, &pending)) {
  7160. goto exit_hmac;
  7161. }
  7162. }
  7163. RECORD_MULTI_VALUE_STATS();
  7164. } /* for i */
  7165. } while (pending > 0);
  7166. } while (bench_stats_check(start)
  7167. #ifdef MULTI_VALUE_STATISTICS
  7168. || runs < minimum_runs
  7169. #endif
  7170. );
  7171. exit_hmac:
  7172. bench_stats_sym_finish(label, useDeviceID, count, bench_size, start, ret);
  7173. #ifdef MULTI_VALUE_STATISTICS
  7174. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7175. #endif
  7176. exit:
  7177. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7178. wc_HmacFree(hmac[i]);
  7179. }
  7180. WC_FREE_ARRAY(hmac, BENCH_MAX_PENDING, HEAP_HINT);
  7181. #ifdef WOLFSSL_ASYNC_CRYPT
  7182. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  7183. #endif
  7184. }
  7185. #ifndef NO_MD5
  7186. void bench_hmac_md5(int useDeviceID)
  7187. {
  7188. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7189. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7190. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  7191. bench_hmac(useDeviceID, WC_MD5, WC_MD5_DIGEST_SIZE, key, sizeof(key),
  7192. "HMAC-MD5");
  7193. }
  7194. #endif /* NO_MD5 */
  7195. #ifndef NO_SHA
  7196. void bench_hmac_sha(int useDeviceID)
  7197. {
  7198. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7199. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7200. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7201. 0x0b, 0x0b, 0x0b, 0x0b };
  7202. bench_hmac(useDeviceID, WC_SHA, WC_SHA_DIGEST_SIZE, key, sizeof(key),
  7203. "HMAC-SHA");
  7204. }
  7205. #endif /* NO_SHA */
  7206. #ifdef WOLFSSL_SHA224
  7207. void bench_hmac_sha224(int useDeviceID)
  7208. {
  7209. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7210. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7211. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7212. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7213. 0x0b, 0x0b, 0x0b, 0x0b };
  7214. bench_hmac(useDeviceID, WC_SHA224,
  7215. WC_SHA224_DIGEST_SIZE, key, sizeof(key),
  7216. "HMAC-SHA224");
  7217. }
  7218. #endif /* WOLFSSL_SHA224 */
  7219. #ifndef NO_SHA256
  7220. void bench_hmac_sha256(int useDeviceID)
  7221. {
  7222. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7223. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7224. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7225. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7226. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  7227. bench_hmac(useDeviceID, WC_SHA256, WC_SHA256_DIGEST_SIZE, key, sizeof(key),
  7228. "HMAC-SHA256");
  7229. }
  7230. #endif /* NO_SHA256 */
  7231. #ifdef WOLFSSL_SHA384
  7232. void bench_hmac_sha384(int useDeviceID)
  7233. {
  7234. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7235. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7236. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7237. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7238. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7239. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7240. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  7241. bench_hmac(useDeviceID, WC_SHA384, WC_SHA384_DIGEST_SIZE, key, sizeof(key),
  7242. "HMAC-SHA384");
  7243. }
  7244. #endif /* WOLFSSL_SHA384 */
  7245. #ifdef WOLFSSL_SHA512
  7246. void bench_hmac_sha512(int useDeviceID)
  7247. {
  7248. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7249. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7250. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7251. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7252. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7253. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7254. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7255. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7256. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  7257. bench_hmac(useDeviceID, WC_SHA512, WC_SHA512_DIGEST_SIZE, key, sizeof(key),
  7258. "HMAC-SHA512");
  7259. }
  7260. #endif /* WOLFSSL_SHA512 */
  7261. #ifndef NO_PWDBASED
  7262. void bench_pbkdf2(void)
  7263. {
  7264. double start;
  7265. int ret = 0, count = 0;
  7266. const char* passwd32 = "passwordpasswordpasswordpassword";
  7267. WOLFSSL_SMALL_STACK_STATIC const byte salt32[] = {
  7268. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06,
  7269. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06,
  7270. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06,
  7271. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  7272. byte derived[32];
  7273. DECLARE_MULTI_VALUE_STATS_VARS()
  7274. bench_stats_start(&count, &start);
  7275. do {
  7276. ret = wc_PBKDF2(derived, (const byte*)passwd32, (int)XSTRLEN(passwd32),
  7277. salt32, (int)sizeof(salt32), 1000, 32, WC_SHA256);
  7278. count++;
  7279. RECORD_MULTI_VALUE_STATS();
  7280. } while (bench_stats_check(start)
  7281. #ifdef MULTI_VALUE_STATISTICS
  7282. || runs < minimum_runs
  7283. #endif
  7284. );
  7285. bench_stats_sym_finish("PBKDF2", 32, count, 32, start, ret);
  7286. #ifdef MULTI_VALUE_STATISTICS
  7287. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7288. #endif
  7289. }
  7290. #endif /* !NO_PWDBASED */
  7291. #endif /* NO_HMAC */
  7292. #ifdef WOLFSSL_SIPHASH
  7293. void bench_siphash(void)
  7294. {
  7295. double start;
  7296. int ret = 0, count;
  7297. const char* passwd16 = "passwordpassword";
  7298. byte out[16];
  7299. int i;
  7300. DECLARE_MULTI_VALUE_STATS_VARS()
  7301. bench_stats_start(&count, &start);
  7302. do {
  7303. for (i = 0; i < numBlocks; i++) {
  7304. ret = wc_SipHash((const byte*)passwd16, bench_plain, bench_size,
  7305. out, 8);
  7306. RECORD_MULTI_VALUE_STATS();
  7307. }
  7308. count += i;
  7309. } while (bench_stats_check(start)
  7310. #ifdef MULTI_VALUE_STATISTICS
  7311. || runs < minimum_runs
  7312. #endif
  7313. );
  7314. bench_stats_sym_finish("SipHash-8", 1, count, bench_size, start, ret);
  7315. #ifdef MULTI_VALUE_STATISTICS
  7316. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7317. #endif
  7318. RESET_MULTI_VALUE_STATS_VARS();
  7319. bench_stats_start(&count, &start);
  7320. do {
  7321. for (i = 0; i < numBlocks; i++) {
  7322. ret = wc_SipHash((const byte*)passwd16, bench_plain, bench_size,
  7323. out, 16);
  7324. RECORD_MULTI_VALUE_STATS();
  7325. }
  7326. count += i;
  7327. } while (bench_stats_check(start)
  7328. #ifdef MULTI_VALUE_STATISTICS
  7329. || runs < minimum_runs
  7330. #endif
  7331. );
  7332. bench_stats_sym_finish("SipHash-16", 1, count, bench_size, start, ret);
  7333. #ifdef MULTI_VALUE_STATISTICS
  7334. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7335. #endif
  7336. }
  7337. #endif
  7338. #ifdef WC_SRTP_KDF
  7339. void bench_srtpkdf(void)
  7340. {
  7341. double start;
  7342. int count;
  7343. int ret = 0;
  7344. byte keyE[32];
  7345. byte keyA[20];
  7346. byte keyS[14];
  7347. const byte *key = bench_key_buf;
  7348. const byte salt[14] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  7349. 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e };
  7350. const byte index[6] = { 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA };
  7351. int kdrIdx = 0;
  7352. int i;
  7353. DECLARE_MULTI_VALUE_STATS_VARS()
  7354. bench_stats_start(&count, &start);
  7355. do {
  7356. for (i = 0; i < numBlocks; i++) {
  7357. ret = wc_SRTP_KDF(key, AES_128_KEY_SIZE, salt, sizeof(salt),
  7358. kdrIdx, index, keyE, AES_128_KEY_SIZE, keyA, sizeof(keyA),
  7359. keyS, sizeof(keyS));
  7360. RECORD_MULTI_VALUE_STATS();
  7361. }
  7362. count += i;
  7363. } while (bench_stats_check(start)
  7364. #ifdef MULTI_VALUE_STATISTICS
  7365. || runs < minimum_runs
  7366. #endif
  7367. );
  7368. bench_stats_asym_finish("KDF", 128, "SRTP", 0, count, start, ret);
  7369. #ifdef MULTI_VALUE_STATISTICS
  7370. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7371. #endif
  7372. RESET_MULTI_VALUE_STATS_VARS();
  7373. bench_stats_start(&count, &start);
  7374. do {
  7375. for (i = 0; i < numBlocks; i++) {
  7376. ret = wc_SRTP_KDF(key, AES_256_KEY_SIZE, salt, sizeof(salt),
  7377. kdrIdx, index, keyE, AES_256_KEY_SIZE, keyA, sizeof(keyA),
  7378. keyS, sizeof(keyS));
  7379. RECORD_MULTI_VALUE_STATS();
  7380. }
  7381. count += i;
  7382. } while (bench_stats_check(start)
  7383. #ifdef MULTI_VALUE_STATISTICS
  7384. || runs < minimum_runs
  7385. #endif
  7386. );
  7387. bench_stats_asym_finish("KDF", 256, "SRTP", 0, count, start, ret);
  7388. #ifdef MULTI_VALUE_STATISTICS
  7389. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7390. #endif
  7391. RESET_MULTI_VALUE_STATS_VARS();
  7392. bench_stats_start(&count, &start);
  7393. do {
  7394. for (i = 0; i < numBlocks; i++) {
  7395. ret = wc_SRTCP_KDF(key, AES_128_KEY_SIZE, salt, sizeof(salt),
  7396. kdrIdx, index, keyE, AES_128_KEY_SIZE, keyA, sizeof(keyA),
  7397. keyS, sizeof(keyS));
  7398. RECORD_MULTI_VALUE_STATS();
  7399. }
  7400. count += i;
  7401. } while (bench_stats_check(start)
  7402. #ifdef MULTI_VALUE_STATISTICS
  7403. || runs < minimum_runs
  7404. #endif
  7405. );
  7406. bench_stats_asym_finish("KDF", 128, "SRTCP", 0, count, start, ret);
  7407. #ifdef MULTI_VALUE_STATISTICS
  7408. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7409. #endif
  7410. RESET_MULTI_VALUE_STATS_VARS();
  7411. bench_stats_start(&count, &start);
  7412. do {
  7413. for (i = 0; i < numBlocks; i++) {
  7414. ret = wc_SRTCP_KDF(key, AES_256_KEY_SIZE, salt, sizeof(salt),
  7415. kdrIdx, index, keyE, AES_256_KEY_SIZE, keyA, sizeof(keyA),
  7416. keyS, sizeof(keyS));
  7417. RECORD_MULTI_VALUE_STATS();
  7418. }
  7419. count += i;
  7420. } while (bench_stats_check(start)
  7421. #ifdef MULTI_VALUE_STATISTICS
  7422. || runs < minimum_runs
  7423. #endif
  7424. );
  7425. bench_stats_asym_finish("KDF", 256, "SRTCP", 0, count, start, ret);
  7426. #ifdef MULTI_VALUE_STATISTICS
  7427. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7428. #endif
  7429. }
  7430. #endif
  7431. #ifndef NO_RSA
  7432. #if defined(WOLFSSL_KEY_GEN)
  7433. static void bench_rsaKeyGen_helper(int useDeviceID, word32 keySz)
  7434. {
  7435. WC_DECLARE_ARRAY(genKey, RsaKey, BENCH_MAX_PENDING,
  7436. sizeof(RsaKey), HEAP_HINT);
  7437. double start = 0;
  7438. int ret = 0, i, count = 0, times, pending = 0;
  7439. const long rsa_e_val = WC_RSA_EXPONENT;
  7440. const char**desc = bench_desc_words[lng_index];
  7441. DECLARE_MULTI_VALUE_STATS_VARS()
  7442. WC_CALLOC_ARRAY(genKey, RsaKey, BENCH_MAX_PENDING,
  7443. sizeof(RsaKey), HEAP_HINT);
  7444. bench_stats_start(&count, &start);
  7445. do {
  7446. /* while free pending slots in queue, submit ops */
  7447. for (times = 0; times < genTimes || pending > 0; ) {
  7448. bench_async_poll(&pending);
  7449. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7450. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]),
  7451. 0, &times, genTimes, &pending)) {
  7452. wc_FreeRsaKey(genKey[i]);
  7453. ret = wc_InitRsaKey_ex(genKey[i], HEAP_HINT, devId);
  7454. if (ret < 0) {
  7455. goto exit;
  7456. }
  7457. ret = wc_MakeRsaKey(genKey[i], (int)keySz, rsa_e_val,
  7458. &gRng);
  7459. if (!bench_async_handle(&ret,
  7460. BENCH_ASYNC_GET_DEV(genKey[i]), 0,
  7461. &times, &pending)) {
  7462. goto exit;
  7463. }
  7464. }
  7465. } /* for i */
  7466. RECORD_MULTI_VALUE_STATS();
  7467. } /* for times */
  7468. count += times;
  7469. } while (bench_stats_check(start)
  7470. #ifdef MULTI_VALUE_STATISTICS
  7471. || runs < minimum_runs
  7472. #endif
  7473. );
  7474. exit:
  7475. bench_stats_asym_finish("RSA", (int)keySz, desc[2], useDeviceID, count,
  7476. start, ret);
  7477. #ifdef MULTI_VALUE_STATISTICS
  7478. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7479. #endif
  7480. /* cleanup */
  7481. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7482. wc_FreeRsaKey(genKey[i]);
  7483. }
  7484. WC_FREE_ARRAY(genKey, BENCH_MAX_PENDING, HEAP_HINT);
  7485. }
  7486. void bench_rsaKeyGen(int useDeviceID)
  7487. {
  7488. int k;
  7489. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  7490. static const word32 keySizes[2] = {1024, 2048};
  7491. #else
  7492. static const word32 keySizes[1] = {2048};
  7493. #endif
  7494. for (k = 0; k < (int)(sizeof(keySizes)/sizeof(int)); k++) {
  7495. bench_rsaKeyGen_helper(useDeviceID, keySizes[k]);
  7496. }
  7497. }
  7498. void bench_rsaKeyGen_size(int useDeviceID, word32 keySz)
  7499. {
  7500. bench_rsaKeyGen_helper(useDeviceID, keySz);
  7501. }
  7502. #endif /* WOLFSSL_KEY_GEN */
  7503. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  7504. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  7505. #if defined(WOLFSSL_MDK_SHELL)
  7506. static char *certRSAname = "certs/rsa2048.der";
  7507. /* set by shell command */
  7508. static void set_Bench_RSA_File(char * cert) { certRSAname = cert ; }
  7509. #elif defined(FREESCALE_MQX)
  7510. static char *certRSAname = "a:\\certs\\rsa2048.der";
  7511. #else
  7512. static const char *certRSAname = "certs/rsa2048.der";
  7513. #endif
  7514. #endif
  7515. #define RSA_BUF_SIZE 384 /* for up to 3072 bit */
  7516. #if defined(WOLFSSL_RSA_VERIFY_INLINE) || defined(WOLFSSL_RSA_PUBLIC_ONLY)
  7517. #if defined(USE_CERT_BUFFERS_2048)
  7518. static const unsigned char rsa_2048_sig[] = {
  7519. 0x8c, 0x9e, 0x37, 0xbf, 0xc3, 0xa6, 0xba, 0x1c,
  7520. 0x53, 0x22, 0x40, 0x4b, 0x8b, 0x0d, 0x3c, 0x0e,
  7521. 0x2e, 0x8c, 0x31, 0x2c, 0x47, 0xbf, 0x03, 0x48,
  7522. 0x18, 0x46, 0x73, 0x8d, 0xd7, 0xdd, 0x17, 0x64,
  7523. 0x0d, 0x7f, 0xdc, 0x74, 0xed, 0x80, 0xc3, 0xe8,
  7524. 0x9a, 0x18, 0x33, 0xd4, 0xe6, 0xc5, 0xe1, 0x54,
  7525. 0x75, 0xd1, 0xbb, 0x40, 0xde, 0xa8, 0xb9, 0x1b,
  7526. 0x14, 0xe8, 0xc1, 0x39, 0xeb, 0xa0, 0x69, 0x8a,
  7527. 0xc6, 0x9b, 0xef, 0x53, 0xb5, 0x23, 0x2b, 0x78,
  7528. 0x06, 0x43, 0x37, 0x11, 0x81, 0x84, 0x73, 0x33,
  7529. 0x33, 0xfe, 0xf7, 0x5d, 0x2b, 0x84, 0xd6, 0x83,
  7530. 0xd6, 0xdd, 0x55, 0x33, 0xef, 0xd1, 0xf7, 0x12,
  7531. 0xb0, 0xc2, 0x0e, 0xb1, 0x78, 0xd4, 0xa8, 0xa3,
  7532. 0x25, 0xeb, 0xed, 0x9a, 0xb3, 0xee, 0xc3, 0x7e,
  7533. 0xce, 0x13, 0x18, 0x86, 0x31, 0xe1, 0xef, 0x01,
  7534. 0x0f, 0x6e, 0x67, 0x24, 0x74, 0xbd, 0x0b, 0x7f,
  7535. 0xa9, 0xca, 0x6f, 0xaa, 0x83, 0x28, 0x90, 0x40,
  7536. 0xf1, 0xb5, 0x10, 0x0e, 0x26, 0x03, 0x05, 0x5d,
  7537. 0x87, 0xb4, 0xe0, 0x4c, 0x98, 0xd8, 0xc6, 0x42,
  7538. 0x89, 0x77, 0xeb, 0xb6, 0xd4, 0xe6, 0x26, 0xf3,
  7539. 0x31, 0x25, 0xde, 0x28, 0x38, 0x58, 0xe8, 0x2c,
  7540. 0xf4, 0x56, 0x7c, 0xb6, 0xfd, 0x99, 0xb0, 0xb0,
  7541. 0xf4, 0x83, 0xb6, 0x74, 0xa9, 0x5b, 0x9f, 0xe8,
  7542. 0xe9, 0xf1, 0xa1, 0x2a, 0xbd, 0xf6, 0x83, 0x28,
  7543. 0x09, 0xda, 0xa6, 0xd6, 0xcd, 0x61, 0x60, 0xf7,
  7544. 0x13, 0x4e, 0x46, 0x57, 0x38, 0x1e, 0x11, 0x92,
  7545. 0x6b, 0x6b, 0xcf, 0xd3, 0xf4, 0x8b, 0x66, 0x03,
  7546. 0x25, 0xa3, 0x7a, 0x2f, 0xce, 0xc1, 0x85, 0xa5,
  7547. 0x48, 0x91, 0x8a, 0xb3, 0x4f, 0x5d, 0x98, 0xb1,
  7548. 0x69, 0x58, 0x47, 0x69, 0x0c, 0x52, 0xdc, 0x42,
  7549. 0x4c, 0xef, 0xe8, 0xd4, 0x4d, 0x6a, 0x33, 0x7d,
  7550. 0x9e, 0xd2, 0x51, 0xe6, 0x41, 0xbf, 0x4f, 0xa2
  7551. };
  7552. #elif defined(USE_CERT_BUFFERS_3072)
  7553. static const unsigned char rsa_3072_sig[] = {
  7554. 0x1a, 0xd6, 0x0d, 0xfd, 0xe3, 0x41, 0x95, 0x76,
  7555. 0x27, 0x16, 0x7d, 0xc7, 0x94, 0x16, 0xca, 0xa8,
  7556. 0x26, 0x08, 0xbe, 0x78, 0x87, 0x72, 0x4c, 0xd9,
  7557. 0xa7, 0xfc, 0x33, 0x77, 0x2d, 0x53, 0x07, 0xb5,
  7558. 0x8c, 0xce, 0x48, 0x17, 0x9b, 0xff, 0x9f, 0x9b,
  7559. 0x17, 0xc4, 0xbb, 0x72, 0xed, 0xdb, 0xa0, 0x34,
  7560. 0x69, 0x5b, 0xc7, 0x4e, 0xbf, 0xec, 0x13, 0xc5,
  7561. 0x98, 0x71, 0x9a, 0x4e, 0x18, 0x0e, 0xcb, 0xe7,
  7562. 0xc6, 0xd5, 0x21, 0x31, 0x7c, 0x0d, 0xae, 0x14,
  7563. 0x2b, 0x87, 0x4f, 0x77, 0x95, 0x2e, 0x26, 0xe2,
  7564. 0x83, 0xfe, 0x49, 0x1e, 0x87, 0x19, 0x4a, 0x63,
  7565. 0x73, 0x75, 0xf1, 0xf5, 0x71, 0xd2, 0xce, 0xd4,
  7566. 0x39, 0x2b, 0xd9, 0xe0, 0x76, 0x70, 0xc8, 0xf8,
  7567. 0xed, 0xdf, 0x90, 0x57, 0x17, 0xb9, 0x16, 0xf6,
  7568. 0xe9, 0x49, 0x48, 0xce, 0x5a, 0x8b, 0xe4, 0x84,
  7569. 0x7c, 0xf3, 0x31, 0x68, 0x97, 0x45, 0x68, 0x38,
  7570. 0x50, 0x3a, 0x70, 0xbd, 0xb3, 0xd3, 0xd2, 0xe0,
  7571. 0x56, 0x5b, 0xc2, 0x0c, 0x2c, 0x10, 0x70, 0x7b,
  7572. 0xd4, 0x99, 0xf9, 0x38, 0x31, 0xb1, 0x86, 0xa0,
  7573. 0x07, 0xf1, 0xf6, 0x53, 0xb0, 0x44, 0x82, 0x40,
  7574. 0xd2, 0xab, 0x0e, 0x71, 0x5d, 0xe1, 0xea, 0x3a,
  7575. 0x77, 0xc9, 0xef, 0xfe, 0x54, 0x65, 0xa3, 0x49,
  7576. 0xfd, 0xa5, 0x33, 0xaa, 0x16, 0x1a, 0x38, 0xe7,
  7577. 0xaa, 0xb7, 0x13, 0xb2, 0x3b, 0xc7, 0x00, 0x87,
  7578. 0x12, 0xfe, 0xfd, 0xf4, 0x55, 0x6d, 0x1d, 0x4a,
  7579. 0x0e, 0xad, 0xd0, 0x4c, 0x55, 0x91, 0x60, 0xd9,
  7580. 0xef, 0x74, 0x69, 0x22, 0x8c, 0x51, 0x65, 0xc2,
  7581. 0x04, 0xac, 0xd3, 0x8d, 0xf7, 0x35, 0x29, 0x13,
  7582. 0x6d, 0x61, 0x7c, 0x39, 0x2f, 0x41, 0x4c, 0xdf,
  7583. 0x38, 0xfd, 0x1a, 0x7d, 0x42, 0xa7, 0x6f, 0x3f,
  7584. 0x3d, 0x9b, 0xd1, 0x97, 0xab, 0xc0, 0xa7, 0x28,
  7585. 0x1c, 0xc0, 0x02, 0x26, 0xeb, 0xce, 0xf9, 0xe1,
  7586. 0x34, 0x45, 0xaf, 0xbf, 0x8d, 0xb8, 0xe0, 0xff,
  7587. 0xd9, 0x6f, 0x77, 0xf3, 0xf7, 0xed, 0x6a, 0xbb,
  7588. 0x03, 0x52, 0xfb, 0x38, 0xfc, 0xea, 0x9f, 0xc9,
  7589. 0x98, 0xed, 0x21, 0x45, 0xaf, 0x43, 0x2b, 0x64,
  7590. 0x96, 0x82, 0x30, 0xe9, 0xb4, 0x36, 0x89, 0x77,
  7591. 0x07, 0x4a, 0xc6, 0x1f, 0x38, 0x7a, 0xee, 0xb6,
  7592. 0x86, 0xf6, 0x2f, 0x03, 0xec, 0xa2, 0xe5, 0x48,
  7593. 0xe5, 0x5a, 0xf5, 0x1c, 0xd2, 0xd9, 0xd8, 0x2d,
  7594. 0x9d, 0x06, 0x07, 0xc9, 0x8b, 0x5d, 0xe0, 0x0f,
  7595. 0x5e, 0x0c, 0x53, 0x27, 0xff, 0x23, 0xee, 0xca,
  7596. 0x5e, 0x4d, 0xf1, 0x95, 0x77, 0x78, 0x1f, 0xf2,
  7597. 0x44, 0x5b, 0x7d, 0x01, 0x49, 0x61, 0x6f, 0x6d,
  7598. 0xbf, 0xf5, 0x19, 0x06, 0x39, 0xe9, 0xe9, 0x29,
  7599. 0xde, 0x47, 0x5e, 0x2e, 0x1f, 0x68, 0xf4, 0x32,
  7600. 0x5e, 0xe9, 0xd0, 0xa7, 0xb4, 0x2a, 0x45, 0xdf,
  7601. 0x15, 0x7d, 0x0d, 0x5b, 0xef, 0xc6, 0x23, 0xac
  7602. };
  7603. #else
  7604. #error Not Supported Yet!
  7605. #endif
  7606. #endif /* WOLFSSL_RSA_VERIFY_INLINE || WOLFSSL_RSA_PUBLIC_ONLY */
  7607. static void bench_rsa_helper(int useDeviceID,
  7608. WC_ARRAY_ARG(rsaKey,
  7609. RsaKey,
  7610. BENCH_MAX_PENDING,
  7611. sizeof(RsaKey)),
  7612. word32 rsaKeySz)
  7613. {
  7614. int ret = 0, i, times, count = 0, pending = 0;
  7615. word32 idx = 0;
  7616. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  7617. const char* messageStr = TEST_STRING;
  7618. const int len = (int)TEST_STRING_SZ;
  7619. #endif
  7620. double start = 0.0F;
  7621. const char**desc = bench_desc_words[lng_index];
  7622. DECLARE_MULTI_VALUE_STATS_VARS()
  7623. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  7624. WC_DECLARE_VAR(message, byte, TEST_STRING_SZ, HEAP_HINT);
  7625. #endif
  7626. WC_DECLARE_HEAP_ARRAY(enc, byte, BENCH_MAX_PENDING,
  7627. rsaKeySz, HEAP_HINT);
  7628. #if (!defined(WOLFSSL_RSA_VERIFY_INLINE) && \
  7629. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  7630. WC_DECLARE_HEAP_ARRAY(out, byte, BENCH_MAX_PENDING,
  7631. rsaKeySz, HEAP_HINT);
  7632. #else
  7633. byte* out[BENCH_MAX_PENDING];
  7634. #endif
  7635. XMEMSET(out, 0, sizeof(out));
  7636. WC_ALLOC_HEAP_ARRAY(enc, byte, BENCH_MAX_PENDING,
  7637. rsaKeySz, HEAP_HINT);
  7638. #if (!defined(WOLFSSL_RSA_VERIFY_INLINE) && \
  7639. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  7640. WC_ALLOC_HEAP_ARRAY(out, byte, BENCH_MAX_PENDING,
  7641. rsaKeySz, HEAP_HINT);
  7642. if (out[0] == NULL) {
  7643. ret = MEMORY_E;
  7644. goto exit;
  7645. }
  7646. #endif
  7647. if (enc[0] == NULL) {
  7648. ret = MEMORY_E;
  7649. goto exit;
  7650. }
  7651. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  7652. WC_ALLOC_VAR(message, byte, TEST_STRING_SZ, HEAP_HINT);
  7653. XMEMCPY(message, messageStr, len);
  7654. #endif
  7655. if (!rsa_sign_verify) {
  7656. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  7657. /* begin public RSA */
  7658. bench_stats_start(&count, &start);
  7659. do {
  7660. for (times = 0; times < ntimes || pending > 0; ) {
  7661. bench_async_poll(&pending);
  7662. /* while free pending slots in queue, submit ops */
  7663. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7664. if (bench_async_check(&ret,
  7665. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7666. 1, &times, ntimes, &pending)) {
  7667. ret = wc_RsaPublicEncrypt(message, (word32)len, enc[i],
  7668. rsaKeySz/8, rsaKey[i],
  7669. GLOBAL_RNG);
  7670. if (!bench_async_handle(&ret,
  7671. BENCH_ASYNC_GET_DEV(
  7672. rsaKey[i]), 1, &times,
  7673. &pending)) {
  7674. goto exit_rsa_verify;
  7675. }
  7676. }
  7677. } /* for i */
  7678. RECORD_MULTI_VALUE_STATS();
  7679. } /* for times */
  7680. count += times;
  7681. } while (bench_stats_check(start)
  7682. #ifdef MULTI_VALUE_STATISTICS
  7683. || runs < minimum_runs
  7684. #endif
  7685. );
  7686. exit_rsa_verify:
  7687. bench_stats_asym_finish("RSA", (int)rsaKeySz, desc[0],
  7688. useDeviceID, count, start, ret);
  7689. #ifdef MULTI_VALUE_STATISTICS
  7690. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7691. #endif
  7692. #endif /* !WOLFSSL_RSA_VERIFY_ONLY */
  7693. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  7694. if (ret < 0) {
  7695. goto exit;
  7696. }
  7697. RESET_MULTI_VALUE_STATS_VARS();
  7698. /* capture resulting encrypt length */
  7699. idx = (word32)(rsaKeySz/8);
  7700. /* begin private async RSA */
  7701. bench_stats_start(&count, &start);
  7702. do {
  7703. for (times = 0; times < ntimes || pending > 0; ) {
  7704. bench_async_poll(&pending);
  7705. /* while free pending slots in queue, submit ops */
  7706. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7707. if (bench_async_check(&ret,
  7708. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7709. 1, &times, ntimes, &pending)) {
  7710. ret = wc_RsaPrivateDecrypt(enc[i], idx, out[i],
  7711. rsaKeySz/8, rsaKey[i]);
  7712. if (!bench_async_handle(&ret,
  7713. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7714. 1, &times, &pending)) {
  7715. goto exit_rsa_pub;
  7716. }
  7717. }
  7718. } /* for i */
  7719. RECORD_MULTI_VALUE_STATS();
  7720. } /* for times */
  7721. count += times;
  7722. } while (bench_stats_check(start)
  7723. #ifdef MULTI_VALUE_STATISTICS
  7724. || runs < minimum_runs
  7725. #endif
  7726. );
  7727. exit_rsa_pub:
  7728. bench_stats_asym_finish("RSA", (int)rsaKeySz, desc[1],
  7729. useDeviceID, count, start, ret);
  7730. #ifdef MULTI_VALUE_STATISTICS
  7731. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7732. #endif
  7733. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY */
  7734. }
  7735. else {
  7736. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  7737. /* begin RSA sign */
  7738. bench_stats_start(&count, &start);
  7739. do {
  7740. for (times = 0; times < ntimes || pending > 0; ) {
  7741. bench_async_poll(&pending);
  7742. /* while free pending slots in queue, submit ops */
  7743. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7744. if (bench_async_check(&ret,
  7745. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7746. 1, &times, ntimes, &pending)) {
  7747. ret = wc_RsaSSL_Sign(message, len, enc[i],
  7748. rsaKeySz/8, rsaKey[i], GLOBAL_RNG);
  7749. if (!bench_async_handle(&ret,
  7750. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7751. 1, &times, &pending)) {
  7752. goto exit_rsa_sign;
  7753. }
  7754. }
  7755. } /* for i */
  7756. RECORD_MULTI_VALUE_STATS();
  7757. } /* for times */
  7758. count += times;
  7759. } while (bench_stats_check(start)
  7760. #ifdef MULTI_VALUE_STATISTICS
  7761. || runs < minimum_runs
  7762. #endif
  7763. );
  7764. exit_rsa_sign:
  7765. bench_stats_asym_finish("RSA", (int)rsaKeySz, desc[4], useDeviceID,
  7766. count, start, ret);
  7767. #ifdef MULTI_VALUE_STATISTICS
  7768. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7769. #endif
  7770. if (ret < 0) {
  7771. goto exit;
  7772. }
  7773. RESET_MULTI_VALUE_STATS_VARS();
  7774. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  7775. /* capture resulting encrypt length */
  7776. idx = rsaKeySz/8;
  7777. /* begin RSA verify */
  7778. bench_stats_start(&count, &start);
  7779. do {
  7780. for (times = 0; times < ntimes || pending > 0; ) {
  7781. bench_async_poll(&pending);
  7782. /* while free pending slots in queue, submit ops */
  7783. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7784. if (bench_async_check(&ret,
  7785. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7786. 1, &times, ntimes, &pending)) {
  7787. #if !defined(WOLFSSL_RSA_VERIFY_INLINE) && \
  7788. !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  7789. ret = wc_RsaSSL_Verify(enc[i], idx, out[i],
  7790. rsaKeySz/8, rsaKey[i]);
  7791. #elif defined(USE_CERT_BUFFERS_2048)
  7792. XMEMCPY(enc[i], rsa_2048_sig, sizeof(rsa_2048_sig));
  7793. idx = sizeof(rsa_2048_sig);
  7794. out[i] = NULL;
  7795. ret = wc_RsaSSL_VerifyInline(enc[i], idx,
  7796. &out[i], rsaKey[i]);
  7797. if (ret > 0) {
  7798. ret = 0;
  7799. }
  7800. #elif defined(USE_CERT_BUFFERS_3072)
  7801. XMEMCPY(enc[i], rsa_3072_sig, sizeof(rsa_3072_sig));
  7802. idx = sizeof(rsa_3072_sig);
  7803. out[i] = NULL;
  7804. ret = wc_RsaSSL_VerifyInline(enc[i], idx,
  7805. &out[i], rsaKey[i]);
  7806. if (ret > 0)
  7807. ret = 0;
  7808. #endif
  7809. if (!bench_async_handle(&ret,
  7810. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7811. 1, &times, &pending)) {
  7812. goto exit_rsa_verifyinline;
  7813. }
  7814. }
  7815. } /* for i */
  7816. RECORD_MULTI_VALUE_STATS();
  7817. } /* for times */
  7818. count += times;
  7819. } while (bench_stats_check(start)
  7820. #ifdef MULTI_VALUE_STATISTICS
  7821. || runs < minimum_runs
  7822. #endif
  7823. );
  7824. exit_rsa_verifyinline:
  7825. bench_stats_asym_finish("RSA", (int)rsaKeySz, desc[5],
  7826. useDeviceID, count, start, ret);
  7827. #ifdef MULTI_VALUE_STATISTICS
  7828. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7829. #endif
  7830. }
  7831. exit:
  7832. WC_FREE_HEAP_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  7833. #if !defined(WOLFSSL_RSA_VERIFY_INLINE) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  7834. WC_FREE_HEAP_ARRAY(out, BENCH_MAX_PENDING, HEAP_HINT);
  7835. #endif
  7836. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  7837. WC_FREE_VAR(message, HEAP_HINT);
  7838. #endif
  7839. }
  7840. void bench_rsa(int useDeviceID)
  7841. {
  7842. int i;
  7843. WC_DECLARE_ARRAY(rsaKey, RsaKey, BENCH_MAX_PENDING,
  7844. sizeof(RsaKey), HEAP_HINT);
  7845. int ret = 0;
  7846. word32 rsaKeySz = 0;
  7847. const byte* tmp;
  7848. size_t bytes;
  7849. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  7850. word32 idx;
  7851. #endif
  7852. WC_CALLOC_ARRAY(rsaKey, RsaKey, BENCH_MAX_PENDING,
  7853. sizeof(RsaKey), HEAP_HINT);
  7854. #ifdef USE_CERT_BUFFERS_1024
  7855. tmp = rsa_key_der_1024;
  7856. bytes = (size_t)sizeof_rsa_key_der_1024;
  7857. rsaKeySz = 1024;
  7858. #elif defined(USE_CERT_BUFFERS_2048)
  7859. tmp = rsa_key_der_2048;
  7860. bytes = (size_t)sizeof_rsa_key_der_2048;
  7861. rsaKeySz = 2048;
  7862. #elif defined(USE_CERT_BUFFERS_3072)
  7863. tmp = rsa_key_der_3072;
  7864. bytes = (size_t)sizeof_rsa_key_der_3072;
  7865. rsaKeySz = 3072;
  7866. #elif defined(USE_CERT_BUFFERS_4096)
  7867. tmp = client_key_der_4096;
  7868. bytes = (size_t)sizeof_client_key_der_4096;
  7869. rsaKeySz = 4096;
  7870. #else
  7871. #error "need a cert buffer size"
  7872. #endif /* USE_CERT_BUFFERS */
  7873. /* init keys */
  7874. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7875. /* setup an async context for each key */
  7876. ret = wc_InitRsaKey_ex(rsaKey[i], HEAP_HINT,
  7877. useDeviceID ? devId : INVALID_DEVID);
  7878. if (ret < 0) {
  7879. goto exit;
  7880. }
  7881. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  7882. #ifdef WC_RSA_BLINDING
  7883. ret = wc_RsaSetRNG(rsaKey[i], &gRng);
  7884. if (ret != 0)
  7885. goto exit;
  7886. #endif
  7887. #endif
  7888. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  7889. /* decode the private key */
  7890. idx = 0;
  7891. if ((ret = wc_RsaPrivateKeyDecode(tmp, &idx,
  7892. rsaKey[i], (word32)bytes)) != 0) {
  7893. printf("wc_RsaPrivateKeyDecode failed! %d\n", ret);
  7894. goto exit;
  7895. }
  7896. #elif defined(WOLFSSL_PUBLIC_MP)
  7897. /* get offset to public portion of the RSA key */
  7898. #ifdef USE_CERT_BUFFERS_1024
  7899. bytes = 11;
  7900. #elif defined(USE_CERT_BUFFERS_2048) || defined(USE_CERT_BUFFERS_3072)
  7901. bytes = 12;
  7902. #endif
  7903. ret = mp_read_unsigned_bin(&rsaKey[i]->n, &tmp[bytes], rsaKeySz/8);
  7904. if (ret != 0) {
  7905. printf("wc_RsaPrivateKeyDecode failed! %d\n", ret);
  7906. goto exit;
  7907. }
  7908. ret = mp_set_int(&rsaKey[i]->e, WC_RSA_EXPONENT);
  7909. if (ret != 0) {
  7910. printf("wc_RsaPrivateKeyDecode failed! %d\n", ret);
  7911. goto exit;
  7912. }
  7913. #else
  7914. /* Note: To benchmark public only define WOLFSSL_PUBLIC_MP */
  7915. rsaKeySz = 0;
  7916. #endif
  7917. }
  7918. if (rsaKeySz > 0) {
  7919. bench_rsa_helper(useDeviceID, rsaKey, rsaKeySz);
  7920. }
  7921. (void)bytes;
  7922. (void)tmp;
  7923. exit:
  7924. /* cleanup */
  7925. if (WC_ARRAY_OK(rsaKey)) {
  7926. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7927. wc_FreeRsaKey(rsaKey[i]);
  7928. }
  7929. WC_FREE_ARRAY(rsaKey, BENCH_MAX_PENDING, HEAP_HINT);
  7930. }
  7931. }
  7932. #ifdef WOLFSSL_KEY_GEN
  7933. /* bench any size of RSA key */
  7934. void bench_rsa_key(int useDeviceID, word32 rsaKeySz)
  7935. {
  7936. int ret = 0, i, pending = 0;
  7937. WC_DECLARE_ARRAY(rsaKey, RsaKey, BENCH_MAX_PENDING,
  7938. sizeof(RsaKey), HEAP_HINT);
  7939. int isPending[BENCH_MAX_PENDING];
  7940. long exp = 65537L;
  7941. /* clear for done cleanup */
  7942. XMEMSET(isPending, 0, sizeof(isPending));
  7943. WC_CALLOC_ARRAY(rsaKey, RsaKey, BENCH_MAX_PENDING,
  7944. sizeof(RsaKey), HEAP_HINT);
  7945. /* init keys */
  7946. do {
  7947. pending = 0;
  7948. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7949. if (!isPending[i]) { /* if making the key is pending then just call
  7950. * wc_MakeRsaKey again */
  7951. /* setup an async context for each key */
  7952. if (wc_InitRsaKey_ex(rsaKey[i], HEAP_HINT,
  7953. useDeviceID ? devId : INVALID_DEVID) < 0) {
  7954. goto exit;
  7955. }
  7956. #ifdef WC_RSA_BLINDING
  7957. ret = wc_RsaSetRNG(rsaKey[i], &gRng);
  7958. if (ret != 0)
  7959. goto exit;
  7960. #endif
  7961. }
  7962. /* create the RSA key */
  7963. ret = wc_MakeRsaKey(rsaKey[i], (int)rsaKeySz, exp, &gRng);
  7964. if (ret == WC_PENDING_E) {
  7965. isPending[i] = 1;
  7966. pending = 1;
  7967. }
  7968. else if (ret != 0) {
  7969. printf("wc_MakeRsaKey failed! %d\n", ret);
  7970. goto exit;
  7971. }
  7972. } /* for i */
  7973. } while (pending > 0);
  7974. bench_rsa_helper(useDeviceID, rsaKey, rsaKeySz);
  7975. exit:
  7976. /* cleanup */
  7977. if (WC_ARRAY_OK(rsaKey)) {
  7978. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7979. wc_FreeRsaKey(rsaKey[i]);
  7980. }
  7981. WC_FREE_ARRAY(rsaKey, BENCH_MAX_PENDING, HEAP_HINT);
  7982. }
  7983. }
  7984. #endif /* WOLFSSL_KEY_GEN */
  7985. #endif /* !NO_RSA */
  7986. #ifndef NO_DH
  7987. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  7988. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  7989. #if defined(WOLFSSL_MDK_SHELL)
  7990. static char *certDHname = "certs/dh2048.der";
  7991. /* set by shell command */
  7992. void set_Bench_DH_File(char * cert) { certDHname = cert ; }
  7993. #elif defined(FREESCALE_MQX)
  7994. static char *certDHname = "a:\\certs\\dh2048.der";
  7995. #elif defined(NO_ASN)
  7996. /* do nothing, but don't need a file */
  7997. #else
  7998. static const char *certDHname = "certs/dh2048.der";
  7999. #endif
  8000. #endif
  8001. #ifdef HAVE_FFDHE_4096
  8002. #define BENCH_DH_KEY_SIZE 512 /* for 4096 bit */
  8003. #else
  8004. #define BENCH_DH_KEY_SIZE 384 /* for 3072 bit */
  8005. #endif
  8006. #define BENCH_DH_PRIV_SIZE (BENCH_DH_KEY_SIZE/8)
  8007. void bench_dh(int useDeviceID)
  8008. {
  8009. int ret = 0, i;
  8010. int count = 0, times, pending = 0;
  8011. const byte* tmp = NULL;
  8012. double start = 0.0F;
  8013. WC_DECLARE_ARRAY(dhKey, DhKey, BENCH_MAX_PENDING,
  8014. sizeof(DhKey), HEAP_HINT);
  8015. int dhKeySz = BENCH_DH_KEY_SIZE * 8; /* used in printf */
  8016. const char**desc = bench_desc_words[lng_index];
  8017. #ifndef NO_ASN
  8018. size_t bytes = 0;
  8019. word32 idx;
  8020. #endif
  8021. word32 pubSz[BENCH_MAX_PENDING];
  8022. word32 privSz[BENCH_MAX_PENDING];
  8023. word32 pubSz2 = BENCH_DH_KEY_SIZE;
  8024. word32 privSz2 = BENCH_DH_PRIV_SIZE;
  8025. word32 agreeSz[BENCH_MAX_PENDING];
  8026. #if defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072) || defined(HAVE_FFDHE_4096)
  8027. #ifdef HAVE_PUBLIC_FFDHE
  8028. const DhParams *params = NULL;
  8029. #else
  8030. int paramName = 0;
  8031. #endif
  8032. #endif
  8033. DECLARE_MULTI_VALUE_STATS_VARS()
  8034. WC_DECLARE_ARRAY(pub, byte, BENCH_MAX_PENDING,
  8035. BENCH_DH_KEY_SIZE, HEAP_HINT);
  8036. WC_DECLARE_VAR(pub2, byte,
  8037. BENCH_DH_KEY_SIZE, HEAP_HINT);
  8038. WC_DECLARE_ARRAY(agree, byte, BENCH_MAX_PENDING,
  8039. BENCH_DH_KEY_SIZE, HEAP_HINT);
  8040. WC_DECLARE_ARRAY(priv, byte, BENCH_MAX_PENDING,
  8041. BENCH_DH_PRIV_SIZE, HEAP_HINT);
  8042. WC_DECLARE_VAR(priv2, byte,
  8043. BENCH_DH_PRIV_SIZE, HEAP_HINT);
  8044. /* old scan-build misfires -Wmaybe-uninitialized on these. */
  8045. XMEMSET(pub, 0, sizeof(pub));
  8046. XMEMSET(agree, 0, sizeof(agree));
  8047. XMEMSET(priv, 0, sizeof(priv));
  8048. WC_CALLOC_ARRAY(dhKey, DhKey, BENCH_MAX_PENDING,
  8049. sizeof(DhKey), HEAP_HINT);
  8050. WC_ALLOC_ARRAY(pub, byte,
  8051. BENCH_MAX_PENDING, BENCH_DH_KEY_SIZE, HEAP_HINT);
  8052. WC_ALLOC_ARRAY(agree, byte,
  8053. BENCH_MAX_PENDING, BENCH_DH_KEY_SIZE, HEAP_HINT);
  8054. WC_ALLOC_ARRAY(priv, byte,
  8055. BENCH_MAX_PENDING, BENCH_DH_PRIV_SIZE, HEAP_HINT);
  8056. WC_ALLOC_VAR(pub2, byte, BENCH_DH_KEY_SIZE, HEAP_HINT);
  8057. WC_ALLOC_VAR(priv2, byte, BENCH_DH_PRIV_SIZE, HEAP_HINT);
  8058. (void)tmp;
  8059. if (!use_ffdhe) {
  8060. #if defined(NO_ASN)
  8061. dhKeySz = 1024;
  8062. /* do nothing, but don't use default FILE */
  8063. #elif defined(USE_CERT_BUFFERS_1024)
  8064. tmp = dh_key_der_1024;
  8065. bytes = (size_t)sizeof_dh_key_der_1024;
  8066. dhKeySz = 1024;
  8067. #elif defined(USE_CERT_BUFFERS_2048)
  8068. tmp = dh_key_der_2048;
  8069. bytes = (size_t)sizeof_dh_key_der_2048;
  8070. dhKeySz = 2048;
  8071. #elif defined(USE_CERT_BUFFERS_3072)
  8072. tmp = dh_key_der_3072;
  8073. bytes = (size_t)sizeof_dh_key_der_3072;
  8074. dhKeySz = 3072;
  8075. #elif defined(USE_CERT_BUFFERS_4096)
  8076. tmp = dh_key_der_4096;
  8077. bytes = (size_t)sizeof_dh_key_der_4096;
  8078. dhKeySz = 4096;
  8079. #else
  8080. #error "need to define a cert buffer size"
  8081. #endif /* USE_CERT_BUFFERS */
  8082. }
  8083. #ifdef HAVE_FFDHE_2048
  8084. else if (use_ffdhe == 2048) {
  8085. #ifdef HAVE_PUBLIC_FFDHE
  8086. params = wc_Dh_ffdhe2048_Get();
  8087. #else
  8088. paramName = WC_FFDHE_2048;
  8089. #endif
  8090. dhKeySz = 2048;
  8091. }
  8092. #endif
  8093. #ifdef HAVE_FFDHE_3072
  8094. else if (use_ffdhe == 3072) {
  8095. #ifdef HAVE_PUBLIC_FFDHE
  8096. params = wc_Dh_ffdhe3072_Get();
  8097. #else
  8098. paramName = WC_FFDHE_3072;
  8099. #endif
  8100. dhKeySz = 3072;
  8101. }
  8102. #endif
  8103. #ifdef HAVE_FFDHE_4096
  8104. else if (use_ffdhe == 4096) {
  8105. #ifdef HAVE_PUBLIC_FFDHE
  8106. params = wc_Dh_ffdhe4096_Get();
  8107. #else
  8108. paramName = WC_FFDHE_4096;
  8109. #endif
  8110. dhKeySz = 4096;
  8111. }
  8112. #endif
  8113. /* init keys */
  8114. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  8115. /* setup an async context for each key */
  8116. ret = wc_InitDhKey_ex(dhKey[i], HEAP_HINT,
  8117. useDeviceID ? devId : INVALID_DEVID);
  8118. if (ret != 0)
  8119. goto exit;
  8120. /* setup key */
  8121. if (!use_ffdhe) {
  8122. #ifdef NO_ASN
  8123. ret = wc_DhSetKey(dhKey[i], dh_p,
  8124. sizeof(dh_p), dh_g, sizeof(dh_g));
  8125. #else
  8126. idx = 0;
  8127. ret = wc_DhKeyDecode(tmp, &idx, dhKey[i], (word32)bytes);
  8128. #endif
  8129. }
  8130. #if defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072)
  8131. #ifdef HAVE_PUBLIC_FFDHE
  8132. else if (params != NULL) {
  8133. ret = wc_DhSetKey(dhKey[i], params->p, params->p_len,
  8134. params->g, params->g_len);
  8135. }
  8136. #else
  8137. else if (paramName != 0) {
  8138. ret = wc_DhSetNamedKey(dhKey[i], paramName);
  8139. }
  8140. #endif
  8141. #endif
  8142. if (ret != 0) {
  8143. printf("DhKeyDecode failed %d, can't benchmark\n", ret);
  8144. goto exit;
  8145. }
  8146. }
  8147. /* Key Gen */
  8148. bench_stats_start(&count, &start);
  8149. PRIVATE_KEY_UNLOCK();
  8150. do {
  8151. /* while free pending slots in queue, submit ops */
  8152. for (times = 0; times < genTimes || pending > 0; ) {
  8153. bench_async_poll(&pending);
  8154. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  8155. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(dhKey[i]),
  8156. 0, &times, genTimes, &pending)) {
  8157. privSz[i] = BENCH_DH_PRIV_SIZE;
  8158. pubSz[i] = BENCH_DH_KEY_SIZE;
  8159. ret = wc_DhGenerateKeyPair(dhKey[i], &gRng,
  8160. priv[i], &privSz[i],
  8161. pub[i], &pubSz[i]);
  8162. if (!bench_async_handle(&ret,
  8163. BENCH_ASYNC_GET_DEV(dhKey[i]),
  8164. 0, &times, &pending)) {
  8165. goto exit_dh_gen;
  8166. }
  8167. }
  8168. } /* for i */
  8169. RECORD_MULTI_VALUE_STATS();
  8170. } /* for times */
  8171. count += times;
  8172. } while (bench_stats_check(start)
  8173. #ifdef MULTI_VALUE_STATISTICS
  8174. || runs < minimum_runs
  8175. #endif
  8176. );
  8177. PRIVATE_KEY_LOCK();
  8178. exit_dh_gen:
  8179. bench_stats_asym_finish("DH", dhKeySz, desc[2],
  8180. useDeviceID, count, start, ret);
  8181. #ifdef MULTI_VALUE_STATISTICS
  8182. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8183. #endif
  8184. if (ret < 0) {
  8185. goto exit;
  8186. }
  8187. RESET_MULTI_VALUE_STATS_VARS();
  8188. /* Generate key to use as other public */
  8189. PRIVATE_KEY_UNLOCK();
  8190. ret = wc_DhGenerateKeyPair(dhKey[0], &gRng,
  8191. priv2, &privSz2, pub2, &pubSz2);
  8192. PRIVATE_KEY_LOCK();
  8193. #ifdef WOLFSSL_ASYNC_CRYPT
  8194. ret = wc_AsyncWait(ret, &dhKey[0]->asyncDev, WC_ASYNC_FLAG_NONE);
  8195. #endif
  8196. /* Key Agree */
  8197. bench_stats_start(&count, &start);
  8198. PRIVATE_KEY_UNLOCK();
  8199. do {
  8200. for (times = 0; times < agreeTimes || pending > 0; ) {
  8201. bench_async_poll(&pending);
  8202. /* while free pending slots in queue, submit ops */
  8203. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  8204. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(dhKey[i]),
  8205. 0, &times, agreeTimes, &pending)) {
  8206. ret = wc_DhAgree(dhKey[i], agree[i], &agreeSz[i], priv[i],
  8207. privSz[i], pub2, pubSz2);
  8208. if (!bench_async_handle(&ret,
  8209. BENCH_ASYNC_GET_DEV(dhKey[i]), 0, &times, &pending)) {
  8210. goto exit;
  8211. }
  8212. }
  8213. } /* for i */
  8214. RECORD_MULTI_VALUE_STATS();
  8215. } /* for times */
  8216. count += times;
  8217. } while (bench_stats_check(start)
  8218. #ifdef MULTI_VALUE_STATISTICS
  8219. || runs < minimum_runs
  8220. #endif
  8221. );
  8222. PRIVATE_KEY_LOCK();
  8223. exit:
  8224. bench_stats_asym_finish("DH", dhKeySz, desc[3],
  8225. useDeviceID, count, start, ret);
  8226. #ifdef MULTI_VALUE_STATISTICS
  8227. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8228. #endif
  8229. /* cleanup */
  8230. if (WC_ARRAY_OK(dhKey)) {
  8231. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  8232. wc_FreeDhKey(dhKey[i]);
  8233. }
  8234. WC_FREE_ARRAY(dhKey, BENCH_MAX_PENDING, HEAP_HINT);
  8235. }
  8236. WC_FREE_ARRAY(pub, BENCH_MAX_PENDING, HEAP_HINT);
  8237. WC_FREE_VAR(pub2, HEAP_HINT);
  8238. WC_FREE_ARRAY(priv, BENCH_MAX_PENDING, HEAP_HINT);
  8239. WC_FREE_VAR(priv2, HEAP_HINT);
  8240. WC_FREE_ARRAY(agree, BENCH_MAX_PENDING, HEAP_HINT);
  8241. }
  8242. #endif /* !NO_DH */
  8243. #ifdef WOLFSSL_HAVE_KYBER
  8244. static void bench_kyber_keygen(int type, const char* name, int keySize,
  8245. KyberKey* key)
  8246. {
  8247. int ret = 0, times, count, pending = 0;
  8248. double start;
  8249. const char**desc = bench_desc_words[lng_index];
  8250. DECLARE_MULTI_VALUE_STATS_VARS()
  8251. /* KYBER Make Key */
  8252. bench_stats_start(&count, &start);
  8253. do {
  8254. /* while free pending slots in queue, submit ops */
  8255. for (times = 0; times < agreeTimes || pending > 0; times++) {
  8256. wc_KyberKey_Free(key);
  8257. ret = wc_KyberKey_Init(type, key, HEAP_HINT, INVALID_DEVID);
  8258. if (ret != 0)
  8259. goto exit;
  8260. #ifdef KYBER_NONDETERMINISTIC
  8261. ret = wc_KyberKey_MakeKey(key, &gRng);
  8262. #else
  8263. unsigned char rand[KYBER_MAKEKEY_RAND_SZ] = {0,};
  8264. ret = wc_KyberKey_MakeKeyWithRandom(key, rand, sizeof(rand));
  8265. #endif
  8266. if (ret != 0)
  8267. goto exit;
  8268. RECORD_MULTI_VALUE_STATS();
  8269. } /* for times */
  8270. count += times;
  8271. } while (bench_stats_check(start)
  8272. #ifdef MULTI_VALUE_STATISTICS
  8273. || runs < minimum_runs
  8274. #endif
  8275. );
  8276. exit:
  8277. bench_stats_asym_finish(name, keySize, desc[2], 0, count, start, ret);
  8278. #ifdef MULTI_VALUE_STATISTICS
  8279. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8280. #endif
  8281. }
  8282. static void bench_kyber_encap(const char* name, int keySize, KyberKey* key)
  8283. {
  8284. int ret = 0, times, count, pending = 0;
  8285. double start;
  8286. const char**desc = bench_desc_words[lng_index];
  8287. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  8288. byte ss[KYBER_SS_SZ];
  8289. word32 ctSz;
  8290. DECLARE_MULTI_VALUE_STATS_VARS()
  8291. ret = wc_KyberKey_CipherTextSize(key, &ctSz);
  8292. if (ret != 0) {
  8293. return;
  8294. }
  8295. /* KYBER Encapsulate */
  8296. bench_stats_start(&count, &start);
  8297. do {
  8298. /* while free pending slots in queue, submit ops */
  8299. for (times = 0; times < agreeTimes || pending > 0; times++) {
  8300. #ifdef KYBER_NONDETERMINISTIC
  8301. ret = wc_KyberKey_Encapsulate(key, ct, ss, &gRng);
  8302. #else
  8303. unsigned char rand[KYBER_ENC_RAND_SZ] = {0,};
  8304. ret = wc_KyberKey_EncapsulateWithRandom(key, ct, ss, rand,
  8305. sizeof(rand));
  8306. #endif
  8307. if (ret != 0)
  8308. goto exit_encap;
  8309. RECORD_MULTI_VALUE_STATS();
  8310. } /* for times */
  8311. count += times;
  8312. } while (bench_stats_check(start)
  8313. #ifdef MULTI_VALUE_STATISTICS
  8314. || runs < minimum_runs
  8315. #endif
  8316. );
  8317. exit_encap:
  8318. bench_stats_asym_finish(name, keySize, desc[9], 0, count, start, ret);
  8319. #ifdef MULTI_VALUE_STATISTICS
  8320. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8321. #endif
  8322. RESET_MULTI_VALUE_STATS_VARS();
  8323. /* KYBER Decapsulate */
  8324. bench_stats_start(&count, &start);
  8325. do {
  8326. /* while free pending slots in queue, submit ops */
  8327. for (times = 0; times < agreeTimes || pending > 0; times++) {
  8328. ret = wc_KyberKey_Decapsulate(key, ss, ct, ctSz);
  8329. if (ret != 0)
  8330. goto exit_decap;
  8331. RECORD_MULTI_VALUE_STATS();
  8332. } /* for times */
  8333. count += times;
  8334. } while (bench_stats_check(start)
  8335. #ifdef MULTI_VALUE_STATISTICS
  8336. || runs < minimum_runs
  8337. #endif
  8338. );
  8339. exit_decap:
  8340. bench_stats_asym_finish(name, keySize, desc[13], 0, count, start, ret);
  8341. #ifdef MULTI_VALUE_STATISTICS
  8342. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8343. #endif
  8344. }
  8345. void bench_kyber(int type)
  8346. {
  8347. KyberKey key;
  8348. const char* name = NULL;
  8349. int keySize = 0;
  8350. switch (type) {
  8351. #ifdef WOLFSSL_KYBER512
  8352. case KYBER512:
  8353. name = "KYBER512 ";
  8354. keySize = 128;
  8355. break;
  8356. #endif
  8357. #ifdef WOLFSSL_KYBER768
  8358. case KYBER768:
  8359. name = "KYBER768 ";
  8360. keySize = 192;
  8361. break;
  8362. #endif
  8363. #ifdef WOLFSSL_KYBER1024
  8364. case KYBER1024:
  8365. name = "KYBER1024";
  8366. keySize = 256;
  8367. break;
  8368. #endif
  8369. }
  8370. bench_kyber_keygen(type, name, keySize, &key);
  8371. bench_kyber_encap(name, keySize, &key);
  8372. wc_KyberKey_Free(&key);
  8373. }
  8374. #endif
  8375. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  8376. /* WC_LMS_PARM_L2_H10_W2
  8377. * signature length: 9300 */
  8378. static const byte lms_priv_L2_H10_W2[64] =
  8379. {
  8380. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8381. 0x62,0x62,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  8382. 0xC7,0x74,0x25,0x5B,0x2C,0xE8,0xDA,0x53,
  8383. 0xF0,0x7C,0x04,0x3F,0x64,0x2D,0x26,0x2C,
  8384. 0x46,0x1D,0xC8,0x90,0x77,0x59,0xD6,0xC0,
  8385. 0x56,0x46,0x7D,0x97,0x64,0xF2,0xA3,0xA1,
  8386. 0xF8,0xD0,0x3B,0x5F,0xAC,0x40,0xB9,0x9E,
  8387. 0x83,0x67,0xBF,0x92,0x8D,0xFE,0x45,0x79
  8388. };
  8389. static const byte lms_pub_L2_H10_W2[60] =
  8390. {
  8391. 0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x06,
  8392. 0x00,0x00,0x00,0x02,0xF8,0xD0,0x3B,0x5F,
  8393. 0xAC,0x40,0xB9,0x9E,0x83,0x67,0xBF,0x92,
  8394. 0x8D,0xFE,0x45,0x79,0x41,0xBC,0x2A,0x3B,
  8395. 0x9F,0xC0,0x11,0x12,0x93,0xF0,0x5A,0xA5,
  8396. 0xC1,0x88,0x29,0x79,0x6C,0x3E,0x0A,0x0F,
  8397. 0xEC,0x3B,0x3E,0xE4,0x38,0xD3,0xD2,0x34,
  8398. 0x7F,0xC8,0x91,0xB0
  8399. };
  8400. /* WC_LMS_PARM_L2_H10_W4
  8401. * signature length: 5076 */
  8402. static const byte lms_priv_L2_H10_W4[64] =
  8403. {
  8404. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8405. 0x63,0x63,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  8406. 0xAE,0x28,0x87,0x19,0x4F,0x4B,0x68,0x61,
  8407. 0x93,0x9A,0xC7,0x0E,0x33,0xB8,0xCE,0x96,
  8408. 0x66,0x0D,0xC7,0xB1,0xFA,0x94,0x80,0xA2,
  8409. 0x28,0x9B,0xCF,0xE2,0x08,0xB5,0x25,0xAC,
  8410. 0xFB,0xB8,0x65,0x5E,0xD1,0xCC,0x31,0xDA,
  8411. 0x2E,0x49,0x3A,0xEE,0xAF,0x63,0x70,0x5E
  8412. };
  8413. static const byte lms_pub_L2_H10_W4[60] =
  8414. {
  8415. 0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x06,
  8416. 0x00,0x00,0x00,0x03,0xFB,0xB8,0x65,0x5E,
  8417. 0xD1,0xCC,0x31,0xDA,0x2E,0x49,0x3A,0xEE,
  8418. 0xAF,0x63,0x70,0x5E,0xA2,0xD5,0xB6,0x15,
  8419. 0x33,0x8C,0x9B,0xE9,0xE1,0x91,0x40,0x1A,
  8420. 0x12,0xE0,0xD7,0xBD,0xE4,0xE0,0x76,0xF5,
  8421. 0x04,0x90,0x76,0xA5,0x9A,0xA7,0x4E,0xFE,
  8422. 0x6B,0x9A,0xD3,0x14
  8423. };
  8424. /* WC_LMS_PARM_L3_H5_W4
  8425. * signature length: 7160 */
  8426. static const byte lms_priv_L3_H5_W4[64] =
  8427. {
  8428. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8429. 0x53,0x53,0x53,0xFF,0xFF,0xFF,0xFF,0xFF,
  8430. 0x38,0xD1,0xBE,0x68,0xD1,0x93,0xE1,0x14,
  8431. 0x6C,0x8B,0xED,0xE2,0x25,0x88,0xED,0xAC,
  8432. 0x57,0xBD,0x87,0x9F,0x54,0xF3,0x58,0xD9,
  8433. 0x4D,0xF5,0x6A,0xBD,0x71,0x99,0x6A,0x28,
  8434. 0x2F,0xE1,0xFC,0xD1,0xD1,0x0C,0x7C,0xF8,
  8435. 0xB4,0xDC,0xDF,0x7F,0x14,0x1A,0x7B,0x50
  8436. };
  8437. static const byte lms_pub_L3_H5_W4[60] =
  8438. {
  8439. 0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x05,
  8440. 0x00,0x00,0x00,0x03,0x2F,0xE1,0xFC,0xD1,
  8441. 0xD1,0x0C,0x7C,0xF8,0xB4,0xDC,0xDF,0x7F,
  8442. 0x14,0x1A,0x7B,0x50,0x8E,0x3A,0xD4,0x05,
  8443. 0x0C,0x95,0x59,0xA0,0xCA,0x7A,0xD8,0xD6,
  8444. 0x5D,0xBD,0x42,0xBB,0xD5,0x82,0xB8,0x9C,
  8445. 0x52,0x37,0xB7,0x45,0x03,0xC2,0x06,0xCE,
  8446. 0xAB,0x4B,0x51,0x39
  8447. };
  8448. /* WC_LMS_PARM_L3_H5_W8
  8449. * signature length: 3992 */
  8450. static const byte lms_priv_L3_H5_W8[64] =
  8451. {
  8452. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8453. 0x54,0x54,0x54,0xFF,0xFF,0xFF,0xFF,0xFF,
  8454. 0xA5,0x46,0x97,0x0C,0xA1,0x3C,0xEA,0x17,
  8455. 0x5C,0x9D,0x59,0xF4,0x0E,0x27,0x37,0xF3,
  8456. 0x6A,0x1C,0xF7,0x29,0x4A,0xCC,0xCD,0x7B,
  8457. 0x4F,0xE7,0x37,0x6E,0xEF,0xC1,0xBD,0xBD,
  8458. 0x04,0x5D,0x8E,0xDD,0xAA,0x47,0xCC,0xE6,
  8459. 0xCE,0x78,0x46,0x20,0x41,0x87,0xE0,0x85
  8460. };
  8461. static const byte lms_pub_L3_H5_W8[60] =
  8462. {
  8463. 0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x05,
  8464. 0x00,0x00,0x00,0x04,0x04,0x5D,0x8E,0xDD,
  8465. 0xAA,0x47,0xCC,0xE6,0xCE,0x78,0x46,0x20,
  8466. 0x41,0x87,0xE0,0x85,0x0D,0x2C,0x46,0xB9,
  8467. 0x39,0x8C,0xA3,0x92,0x4F,0xCE,0x50,0x96,
  8468. 0x90,0x9C,0xF3,0x36,0x2E,0x09,0x15,0x3B,
  8469. 0x4B,0x34,0x17,0xE7,0xE2,0x55,0xFC,0x5B,
  8470. 0x83,0xAB,0x43,0xAF
  8471. };
  8472. /* WC_LMS_PARM_L3_H10_W4
  8473. * signature length: 7640 */
  8474. static const byte lms_priv_L3_H10_W4[64] =
  8475. {
  8476. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8477. 0x63,0x63,0x63,0xFF,0xFF,0xFF,0xFF,0xFF,
  8478. 0xDF,0x98,0xAB,0xEC,0xFE,0x13,0x9F,0xF8,
  8479. 0xD7,0x2B,0x4F,0x4C,0x79,0x34,0xB8,0x89,
  8480. 0x24,0x6B,0x26,0x7D,0x7A,0x2E,0xA2,0xCB,
  8481. 0x82,0x75,0x4E,0x96,0x54,0x49,0xED,0xA0,
  8482. 0xAF,0xC7,0xA5,0xEE,0x8A,0xA2,0x83,0x99,
  8483. 0x4B,0x18,0x59,0x2B,0x66,0xC0,0x32,0xDB
  8484. };
  8485. static const byte lms_pub_L3_H10_W4[60] =
  8486. {
  8487. 0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x06,
  8488. 0x00,0x00,0x00,0x03,0xAF,0xC7,0xA5,0xEE,
  8489. 0x8A,0xA2,0x83,0x99,0x4B,0x18,0x59,0x2B,
  8490. 0x66,0xC0,0x32,0xDB,0xC4,0x18,0xEB,0x11,
  8491. 0x17,0x7D,0xAA,0x93,0xFD,0xA0,0x70,0x4D,
  8492. 0x68,0x4B,0x63,0x8F,0xC2,0xE7,0xCA,0x34,
  8493. 0x14,0x31,0x0D,0xAA,0x18,0xBF,0x9B,0x32,
  8494. 0x8D,0x78,0xD5,0xA8
  8495. };
  8496. /* WC_LMS_PARM_L4_H5_W8
  8497. * signature length: 5340 */
  8498. static const byte lms_priv_L4_H5_W8[64] =
  8499. {
  8500. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8501. 0x54,0x54,0x54,0x54,0xFF,0xFF,0xFF,0xFF,
  8502. 0x46,0x8F,0x2A,0x4A,0x14,0x26,0xF0,0x89,
  8503. 0xFE,0xED,0x66,0x0F,0x73,0x69,0xB1,0x4C,
  8504. 0x47,0xA1,0x35,0x9F,0x7B,0xBA,0x08,0x03,
  8505. 0xEE,0xA2,0xEB,0xAD,0xB4,0x82,0x52,0x1F,
  8506. 0xFD,0x9B,0x22,0x82,0x42,0x1A,0x96,0x1E,
  8507. 0xE4,0xA1,0x9C,0x33,0xED,0xE6,0x9F,0xAB
  8508. };
  8509. static const byte lms_pub_L4_H5_W8[60] =
  8510. {
  8511. 0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x05,
  8512. 0x00,0x00,0x00,0x04,0xFD,0x9B,0x22,0x82,
  8513. 0x42,0x1A,0x96,0x1E,0xE4,0xA1,0x9C,0x33,
  8514. 0xED,0xE6,0x9F,0xAB,0x6B,0x47,0x05,0x5B,
  8515. 0xA7,0xAD,0xF6,0x88,0xA5,0x4F,0xCD,0xF1,
  8516. 0xDA,0x29,0x67,0xC3,0x7F,0x2C,0x11,0xFE,
  8517. 0x85,0x1A,0x7A,0xD8,0xD5,0x46,0x74,0x3B,
  8518. 0x74,0x24,0x12,0xC8
  8519. };
  8520. static int lms_write_key_mem(const byte* priv, word32 privSz, void* context)
  8521. {
  8522. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  8523. * BE USED FOR TESTING PURPOSES! Production applications should
  8524. * write only to non-volatile storage. */
  8525. XMEMCPY(context, priv, privSz);
  8526. return WC_LMS_RC_SAVED_TO_NV_MEMORY;
  8527. }
  8528. static int lms_read_key_mem(byte* priv, word32 privSz, void* context)
  8529. {
  8530. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  8531. * BE USED FOR TESTING PURPOSES! */
  8532. XMEMCPY(priv, context, privSz);
  8533. return WC_LMS_RC_READ_TO_MEMORY;
  8534. }
  8535. static byte lms_priv[HSS_MAX_PRIVATE_KEY_LEN];
  8536. static void bench_lms_keygen(int parm, byte* pub)
  8537. {
  8538. WC_RNG rng;
  8539. LmsKey key;
  8540. int ret;
  8541. word32 pubLen = HSS_MAX_PUBLIC_KEY_LEN;
  8542. int times = 0;
  8543. int count = 0;
  8544. double start = 0.0F;
  8545. int levels;
  8546. int height;
  8547. int winternitz;
  8548. const char* str = wc_LmsKey_ParmToStr(parm);
  8549. DECLARE_MULTI_VALUE_STATS_VARS()
  8550. #ifndef HAVE_FIPS
  8551. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  8552. #else
  8553. ret = wc_InitRng(&rng);
  8554. #endif
  8555. if (ret != 0) {
  8556. fprintf(stderr, "error: wc_InitRng failed: %d\n", ret);
  8557. return;
  8558. }
  8559. ret = wc_LmsKey_Init(&key, NULL, INVALID_DEVID);
  8560. if (ret) {
  8561. printf("wc_LmsKey_Init failed: %d\n", ret);
  8562. wc_FreeRng(&rng);
  8563. return;
  8564. }
  8565. count = 0;
  8566. bench_stats_start(&count, &start);
  8567. do {
  8568. /* LMS is stateful. Async queuing not practical. */
  8569. for (times = 0; times < 1; ++times) {
  8570. wc_LmsKey_Free(&key);
  8571. ret = wc_LmsKey_Init(&key, NULL, INVALID_DEVID);
  8572. if (ret) {
  8573. printf("wc_LmsKey_Init failed: %d\n", ret);
  8574. goto exit_lms_keygen;
  8575. }
  8576. ret = wc_LmsKey_SetLmsParm(&key, parm);
  8577. if (ret) {
  8578. printf("wc_LmsKey_SetLmsParm failed: %d\n", ret);
  8579. goto exit_lms_keygen;
  8580. }
  8581. ret = wc_LmsKey_GetParameters(&key, &levels, &height, &winternitz);
  8582. if (ret) {
  8583. fprintf(stderr, "error: wc_LmsKey_GetParameters failed: %d\n",
  8584. ret);
  8585. goto exit_lms_keygen;
  8586. }
  8587. ret = wc_LmsKey_SetWriteCb(&key, lms_write_key_mem);
  8588. if (ret) {
  8589. fprintf(stderr, "error: wc_LmsKey_SetWriteCb failed: %d\n",
  8590. ret);
  8591. goto exit_lms_keygen;
  8592. }
  8593. ret = wc_LmsKey_SetReadCb(&key, lms_read_key_mem);
  8594. if (ret) {
  8595. fprintf(stderr, "error: wc_LmsKey_SetReadCb failed: %d\n", ret);
  8596. goto exit_lms_keygen;
  8597. }
  8598. ret = wc_LmsKey_SetContext(&key, (void*)lms_priv);
  8599. if (ret) {
  8600. fprintf(stderr, "error: wc_LmsKey_SetContext failed: %d\n",
  8601. ret);
  8602. goto exit_lms_keygen;
  8603. }
  8604. ret = wc_LmsKey_MakeKey(&key, &rng);
  8605. if (ret) {
  8606. printf("wc_LmsKey_MakeKey failed: %d\n", ret);
  8607. goto exit_lms_keygen;
  8608. }
  8609. RECORD_MULTI_VALUE_STATS();
  8610. }
  8611. count += times;
  8612. } while (bench_stats_check(start)
  8613. #ifdef MULTI_VALUE_STATISTICS
  8614. || runs < minimum_runs
  8615. #endif
  8616. );
  8617. bench_stats_asym_finish(str, levels * height, "keygen", 0,
  8618. count, start, ret);
  8619. #ifdef MULTI_VALUE_STATISTICS
  8620. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8621. #endif
  8622. ret = wc_LmsKey_ExportPubRaw(&key, pub, &pubLen);
  8623. if (ret) {
  8624. fprintf(stderr, "error: wc_LmsKey_ExportPubRaw failed: %d\n", ret);
  8625. }
  8626. exit_lms_keygen:
  8627. wc_LmsKey_Free(&key);
  8628. wc_FreeRng(&rng);
  8629. }
  8630. static void bench_lms_sign_verify(int parm, byte* pub)
  8631. {
  8632. LmsKey key;
  8633. int ret = 0;
  8634. const char * msg = TEST_STRING;
  8635. word32 msgSz = TEST_STRING_SZ;
  8636. byte * sig = NULL;
  8637. word32 sigSz = 0;
  8638. word32 privLen = 0;
  8639. int loaded = 0;
  8640. int times = 0;
  8641. int count = 0;
  8642. double start = 0.0F;
  8643. const char * str = wc_LmsKey_ParmToStr(parm);
  8644. DECLARE_MULTI_VALUE_STATS_VARS()
  8645. ret = wc_LmsKey_Init(&key, NULL, INVALID_DEVID);
  8646. if (ret) {
  8647. printf("wc_LmsKey_Init failed: %d\n", ret);
  8648. goto exit_lms_sign_verify;
  8649. }
  8650. ret = wc_LmsKey_SetLmsParm(&key, parm);
  8651. if (ret) {
  8652. printf("wc_LmsKey_SetLmsParm failed: %d\n", ret);
  8653. goto exit_lms_sign_verify;
  8654. }
  8655. switch (parm) {
  8656. case WC_LMS_PARM_L2_H10_W2:
  8657. XMEMCPY(lms_priv, lms_priv_L2_H10_W2, sizeof(lms_priv_L2_H10_W2));
  8658. XMEMCPY(key.pub, lms_pub_L2_H10_W2, HSS_MAX_PUBLIC_KEY_LEN);
  8659. break;
  8660. case WC_LMS_PARM_L2_H10_W4:
  8661. XMEMCPY(lms_priv, lms_priv_L2_H10_W4, sizeof(lms_priv_L2_H10_W4));
  8662. XMEMCPY(key.pub, lms_pub_L2_H10_W4, HSS_MAX_PUBLIC_KEY_LEN);
  8663. break;
  8664. case WC_LMS_PARM_L3_H5_W4:
  8665. XMEMCPY(lms_priv, lms_priv_L3_H5_W4, sizeof(lms_priv_L3_H5_W4));
  8666. XMEMCPY(key.pub, lms_pub_L3_H5_W4, HSS_MAX_PUBLIC_KEY_LEN);
  8667. break;
  8668. case WC_LMS_PARM_L3_H5_W8:
  8669. XMEMCPY(lms_priv, lms_priv_L3_H5_W8, sizeof(lms_priv_L3_H5_W8));
  8670. XMEMCPY(key.pub, lms_pub_L3_H5_W8, HSS_MAX_PUBLIC_KEY_LEN);
  8671. break;
  8672. case WC_LMS_PARM_L3_H10_W4:
  8673. XMEMCPY(lms_priv, lms_priv_L3_H10_W4, sizeof(lms_priv_L3_H10_W4));
  8674. XMEMCPY(key.pub, lms_pub_L3_H10_W4, HSS_MAX_PUBLIC_KEY_LEN);
  8675. break;
  8676. case WC_LMS_PARM_L4_H5_W8:
  8677. XMEMCPY(lms_priv, lms_priv_L4_H5_W8, sizeof(lms_priv_L4_H5_W8));
  8678. XMEMCPY(key.pub, lms_pub_L4_H5_W8, HSS_MAX_PUBLIC_KEY_LEN);
  8679. break;
  8680. case WC_LMS_PARM_NONE:
  8681. case WC_LMS_PARM_L1_H15_W2:
  8682. case WC_LMS_PARM_L1_H15_W4:
  8683. case WC_LMS_PARM_L2_H10_W8:
  8684. case WC_LMS_PARM_L3_H5_W2:
  8685. default:
  8686. XMEMCPY(key.pub, pub, HSS_MAX_PUBLIC_KEY_LEN);
  8687. break;
  8688. }
  8689. ret = wc_LmsKey_SetWriteCb(&key, lms_write_key_mem);
  8690. if (ret) {
  8691. fprintf(stderr, "error: wc_LmsKey_SetWriteCb failed: %d\n", ret);
  8692. goto exit_lms_sign_verify;
  8693. }
  8694. ret = wc_LmsKey_SetReadCb(&key, lms_read_key_mem);
  8695. if (ret) {
  8696. fprintf(stderr, "error: wc_LmsKey_SetReadCb failed: %d\n", ret);
  8697. goto exit_lms_sign_verify;
  8698. }
  8699. ret = wc_LmsKey_SetContext(&key, (void*)lms_priv);
  8700. if (ret) {
  8701. fprintf(stderr, "error: wc_LmsKey_SetContext failed: %d\n", ret);
  8702. goto exit_lms_sign_verify;
  8703. }
  8704. /* Even with saved priv/pub keys, we must still reload the private
  8705. * key before using it. Reloading the private key is the bottleneck
  8706. * for larger heights. Only print load time in debug builds. */
  8707. count = 0;
  8708. bench_stats_start(&count, &start);
  8709. #ifndef WOLFSSL_WC_LMS_SMALL
  8710. do {
  8711. #ifdef WOLFSSL_WC_LMS
  8712. key.priv.inited = 0;
  8713. key.state = WC_LMS_STATE_PARMSET;
  8714. #endif
  8715. ret = wc_LmsKey_Reload(&key);
  8716. if (ret) {
  8717. printf("wc_LmsKey_Reload failed: %d\n", ret);
  8718. goto exit_lms_sign_verify;
  8719. }
  8720. RECORD_MULTI_VALUE_STATS();
  8721. count++;
  8722. ret = wc_LmsKey_GetSigLen(&key, &sigSz);
  8723. if (ret) {
  8724. printf("wc_LmsKey_GetSigLen failed: %d\n", ret);
  8725. goto exit_lms_sign_verify;
  8726. }
  8727. ret = wc_LmsKey_GetPrivLen(&key, &privLen);
  8728. if (ret) {
  8729. printf("wc_LmsKey_GetPrivLen failed: %d\n", ret);
  8730. goto exit_lms_sign_verify;
  8731. }
  8732. #ifdef HAVE_LIBLMS
  8733. break;
  8734. #endif
  8735. } while (bench_stats_check(start)
  8736. #ifdef MULTI_VALUE_STATISTICS
  8737. || runs < minimum_runs
  8738. #endif
  8739. );
  8740. bench_stats_asym_finish(str, (int)privLen, "load", 0,
  8741. count, start, ret);
  8742. #ifdef MULTI_VALUE_STATISTICS
  8743. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8744. #endif
  8745. RESET_MULTI_VALUE_STATS_VARS();
  8746. #else
  8747. ret = wc_LmsKey_Reload(&key);
  8748. if (ret) {
  8749. printf("wc_LmsKey_Reload failed: %d\n", ret);
  8750. goto exit_lms_sign_verify;
  8751. }
  8752. ret = wc_LmsKey_GetSigLen(&key, &sigSz);
  8753. if (ret) {
  8754. printf("wc_LmsKey_GetSigLen failed: %d\n", ret);
  8755. goto exit_lms_sign_verify;
  8756. }
  8757. ret = wc_LmsKey_GetPrivLen(&key, &privLen);
  8758. if (ret) {
  8759. printf("wc_LmsKey_GetPrivLen failed: %d\n", ret);
  8760. goto exit_lms_sign_verify;
  8761. }
  8762. #endif
  8763. loaded = 1;
  8764. sig = XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8765. if (sig == NULL) {
  8766. printf("bench_lms_sign_verify malloc failed\n");
  8767. goto exit_lms_sign_verify;
  8768. }
  8769. count = 0;
  8770. bench_stats_start(&count, &start);
  8771. do {
  8772. /* LMS is stateful. Async queuing not practical. */
  8773. #ifndef WOLFSSL_WC_LMS_SMALL
  8774. for (times = 0; times < ntimes; ++times)
  8775. #else
  8776. for (times = 0; times < 1; ++times)
  8777. #endif
  8778. {
  8779. ret = wc_LmsKey_Sign(&key, sig, &sigSz, (byte *) msg, msgSz);
  8780. if (ret) {
  8781. printf("wc_LmsKey_Sign failed: %d\n", ret);
  8782. goto exit_lms_sign_verify;
  8783. }
  8784. RECORD_MULTI_VALUE_STATS();
  8785. if (!wc_LmsKey_SigsLeft(&key)) {
  8786. break;
  8787. }
  8788. }
  8789. count += times;
  8790. } while (wc_LmsKey_SigsLeft(&key) && (bench_stats_check(start)
  8791. #ifdef MULTI_VALUE_STATISTICS
  8792. || runs < minimum_runs
  8793. #endif
  8794. ));
  8795. bench_stats_asym_finish(str, (int)sigSz, "sign", 0,
  8796. count, start, ret);
  8797. #ifdef MULTI_VALUE_STATISTICS
  8798. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8799. #endif
  8800. RESET_MULTI_VALUE_STATS_VARS();
  8801. count = 0;
  8802. bench_stats_start(&count, &start);
  8803. do {
  8804. /* LMS is stateful. Async queuing not practical. */
  8805. for (times = 0; times < ntimes; ++times) {
  8806. ret = wc_LmsKey_Verify(&key, sig, sigSz, (byte *) msg, msgSz);
  8807. if (ret) {
  8808. printf("wc_LmsKey_Verify failed: %d\n", ret);
  8809. goto exit_lms_sign_verify;
  8810. }
  8811. RECORD_MULTI_VALUE_STATS();
  8812. }
  8813. count += times;
  8814. } while (bench_stats_check(start)
  8815. #ifdef MULTI_VALUE_STATISTICS
  8816. || runs < minimum_runs
  8817. #endif
  8818. );
  8819. exit_lms_sign_verify:
  8820. bench_stats_asym_finish(str, (int)sigSz, "verify", 0,
  8821. count, start, ret);
  8822. #ifdef MULTI_VALUE_STATISTICS
  8823. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8824. #endif
  8825. if (loaded) {
  8826. wc_LmsKey_Free(&key);
  8827. }
  8828. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8829. return;
  8830. }
  8831. void bench_lms(void)
  8832. {
  8833. byte pub[HSS_MAX_PUBLIC_KEY_LEN];
  8834. #ifdef BENCH_LMS_SLOW_KEYGEN
  8835. #if !defined(WOLFSSL_WC_LMS) || (LMS_MAX_HEIGHT >= 15)
  8836. bench_lms_keygen(WC_LMS_PARM_L1_H15_W2, pub);
  8837. bench_lms_sign_verify(WC_LMS_PARM_L1_H15_W2, pub);
  8838. bench_lms_keygen(WC_LMS_PARM_L1_H15_W4, pub);
  8839. bench_lms_sign_verify(WC_LMS_PARM_L1_H15_W4, pub);
  8840. #undef LMS_PARAMS_BENCHED
  8841. #define LMS_PARAMS_BENCHED
  8842. #endif
  8843. #endif
  8844. #if !defined(WOLFSSL_WC_LMS) || ((LMS_MAX_LEVELS >= 2) && \
  8845. (LMS_MAX_HEIGHT >= 10))
  8846. bench_lms_keygen(WC_LMS_PARM_L2_H10_W2, pub);
  8847. bench_lms_sign_verify(WC_LMS_PARM_L2_H10_W2, pub);
  8848. bench_lms_keygen(WC_LMS_PARM_L2_H10_W4, pub);
  8849. bench_lms_sign_verify(WC_LMS_PARM_L2_H10_W4, pub);
  8850. #undef LMS_PARAMS_BENCHED
  8851. #define LMS_PARAMS_BENCHED
  8852. #ifdef BENCH_LMS_SLOW_KEYGEN
  8853. bench_lms_keygen(WC_LMS_PARM_L2_H10_W8, pub);
  8854. bench_lms_sign_verify(WC_LMS_PARM_L2_H10_W8, pub);
  8855. #endif
  8856. #endif
  8857. #if !defined(WOLFSSL_WC_LMS) || (LMS_MAX_LEVELS >= 3)
  8858. bench_lms_keygen(WC_LMS_PARM_L3_H5_W4, pub);
  8859. bench_lms_sign_verify(WC_LMS_PARM_L3_H5_W4, pub);
  8860. bench_lms_keygen(WC_LMS_PARM_L3_H5_W8, pub);
  8861. bench_lms_sign_verify(WC_LMS_PARM_L3_H5_W8, pub);
  8862. #undef LMS_PARAMS_BENCHED
  8863. #define LMS_PARAMS_BENCHED
  8864. #endif
  8865. #if !defined(WOLFSSL_WC_LMS) || ((LMS_MAX_LEVELS >= 3) && \
  8866. (LMS_MAX_HEIGHT >= 10))
  8867. bench_lms_keygen(WC_LMS_PARM_L3_H10_W4, pub);
  8868. bench_lms_sign_verify(WC_LMS_PARM_L3_H10_W4, pub);
  8869. #endif
  8870. #if !defined(WOLFSSL_WC_LMS) || (LMS_MAX_LEVELS >= 4)
  8871. bench_lms_keygen(WC_LMS_PARM_L4_H5_W8, pub);
  8872. bench_lms_sign_verify(WC_LMS_PARM_L4_H5_W8, pub);
  8873. #endif
  8874. #if defined(WOLFSSL_WC_LMS) && !defined(LMS_PARAMS_BENCHED)
  8875. bench_lms_keygen(WC_LMS_PARM_L1_H5_W1, pub);
  8876. bench_lms_sign_verify(WC_LMS_PARM_L1_H5_W1, pub);
  8877. #endif
  8878. return;
  8879. }
  8880. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY) */
  8881. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  8882. static enum wc_XmssRc xmss_write_key_mem(const byte * priv, word32 privSz,
  8883. void *context)
  8884. {
  8885. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  8886. * BE USED FOR TESTING PURPOSES! Production applications should
  8887. * write only to non-volatile storage. */
  8888. XMEMCPY(context, priv, privSz);
  8889. return WC_XMSS_RC_SAVED_TO_NV_MEMORY;
  8890. }
  8891. static enum wc_XmssRc xmss_read_key_mem(byte * priv, word32 privSz,
  8892. void *context)
  8893. {
  8894. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  8895. * BE USED FOR TESTING PURPOSES! */
  8896. XMEMCPY(priv, context, privSz);
  8897. return WC_XMSS_RC_READ_TO_MEMORY;
  8898. }
  8899. static void bench_xmss_sign_verify(const char * params)
  8900. {
  8901. WC_RNG rng;
  8902. XmssKey key;
  8903. word32 pkSz = 0;
  8904. word32 skSz = 0;
  8905. int freeRng = 0;
  8906. int freeKey = 0;
  8907. unsigned char * sk = NULL;
  8908. const char * msg = "XMSS post quantum signature test";
  8909. word32 msgSz = (word32) XSTRLEN(msg);
  8910. int ret = 0;
  8911. byte * sig = NULL;
  8912. word32 sigSz = 0;
  8913. int times = 0;
  8914. int count = 0;
  8915. double start = 0.0F;
  8916. #ifndef HAVE_FIPS
  8917. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  8918. #else
  8919. ret = wc_InitRng(&rng);
  8920. #endif
  8921. if (ret != 0) {
  8922. fprintf(stderr, "error: wc_InitRng failed: %d\n", ret);
  8923. goto exit_xmss_sign_verify;
  8924. }
  8925. freeRng = 1;
  8926. ret = wc_XmssKey_Init(&key, NULL, INVALID_DEVID);
  8927. if (ret != 0) {
  8928. fprintf(stderr, "wc_XmssKey_Init failed: %d\n", ret);
  8929. goto exit_xmss_sign_verify;
  8930. }
  8931. ret = wc_XmssKey_SetParamStr(&key, params);
  8932. if (ret != 0) {
  8933. fprintf(stderr, "wc_XmssKey_SetParamStr failed: %d\n", ret);
  8934. goto exit_xmss_sign_verify;
  8935. }
  8936. ret = wc_XmssKey_GetPubLen(&key, &pkSz);
  8937. if (ret != 0) {
  8938. fprintf(stderr, "wc_XmssKey_GetPubLen failed: %d\n", ret);
  8939. goto exit_xmss_sign_verify;
  8940. }
  8941. #ifndef WOLFSSL_WC_XMSS
  8942. if (pkSz != XMSS_SHA256_PUBLEN) {
  8943. fprintf(stderr, "error: xmss pub len: got %u, expected %d\n", pkSz,
  8944. XMSS_SHA256_PUBLEN);
  8945. goto exit_xmss_sign_verify;
  8946. }
  8947. #endif
  8948. ret = wc_XmssKey_GetPrivLen(&key, &skSz);
  8949. if (ret != 0 || skSz <= 0) {
  8950. fprintf(stderr, "error: wc_XmssKey_GetPrivLen failed\n");
  8951. goto exit_xmss_sign_verify;
  8952. }
  8953. ret = wc_XmssKey_GetSigLen(&key, &sigSz);
  8954. if (ret != 0 || sigSz <= 0) {
  8955. fprintf(stderr, "error: wc_XmssKey_GetSigLen failed\n");
  8956. goto exit_xmss_sign_verify;
  8957. }
  8958. /* Allocate secret keys.*/
  8959. sk = (unsigned char *)XMALLOC(skSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8960. if (sk == NULL) {
  8961. fprintf(stderr, "error: allocate xmss sk failed\n");
  8962. goto exit_xmss_sign_verify;
  8963. }
  8964. /* Allocate signature array. */
  8965. sig = (byte *)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8966. if (sig == NULL) {
  8967. fprintf(stderr, "error: allocate xmss sig failed\n");
  8968. goto exit_xmss_sign_verify;
  8969. }
  8970. ret = wc_XmssKey_SetWriteCb(&key, xmss_write_key_mem);
  8971. if (ret != 0) {
  8972. fprintf(stderr, "error: wc_XmssKey_SetWriteCb failed: %d\n", ret);
  8973. goto exit_xmss_sign_verify;
  8974. }
  8975. ret = wc_XmssKey_SetReadCb(&key, xmss_read_key_mem);
  8976. if (ret != 0) {
  8977. fprintf(stderr, "error: wc_XmssKey_SetReadCb failed: %d\n", ret);
  8978. goto exit_xmss_sign_verify;
  8979. }
  8980. ret = wc_XmssKey_SetContext(&key, (void *)sk);
  8981. if (ret != 0) {
  8982. fprintf(stderr, "error: wc_XmssKey_SetContext failed: %d\n", ret);
  8983. goto exit_xmss_sign_verify;
  8984. }
  8985. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  8986. fprintf(stderr, "params: %s\n", params);
  8987. fprintf(stderr, "pkSz: %d\n", pkSz);
  8988. fprintf(stderr, "skSz: %d\n", skSz);
  8989. fprintf(stderr, "sigSz: %d\n", sigSz);
  8990. #endif
  8991. /* Making the private key is the bottleneck for larger heights. */
  8992. count = 0;
  8993. bench_stats_start(&count, &start);
  8994. ret = wc_XmssKey_MakeKey(&key, &rng);
  8995. if (ret != 0) {
  8996. printf("wc_XmssKey_MakeKey failed: %d\n", ret);
  8997. goto exit_xmss_sign_verify;
  8998. }
  8999. /* Can only do one at a time - state changes after make key. */
  9000. count +=1;
  9001. bench_stats_check(start);
  9002. bench_stats_asym_finish(params, (int)skSz, "gen", 0, count, start, ret);
  9003. freeKey = 1;
  9004. count = 0;
  9005. bench_stats_start(&count, &start);
  9006. do {
  9007. /* XMSS is stateful. Async queuing not practical. */
  9008. #ifndef WOLFSSL_WC_XMSS_SMALL
  9009. for (times = 0; times < ntimes; ++times)
  9010. #else
  9011. for (times = 0; times < 1; ++times)
  9012. #endif
  9013. {
  9014. if (!wc_XmssKey_SigsLeft(&key))
  9015. break;
  9016. ret = wc_XmssKey_Sign(&key, sig, &sigSz, (byte *) msg, msgSz);
  9017. if (ret) {
  9018. printf("wc_XmssKey_Sign failed: %d\n", ret);
  9019. goto exit_xmss_sign_verify;
  9020. }
  9021. }
  9022. count += times;
  9023. } while (wc_XmssKey_SigsLeft(&key) && bench_stats_check(start));
  9024. bench_stats_asym_finish(params, (int)sigSz, "sign", 0, count, start, ret);
  9025. count = 0;
  9026. bench_stats_start(&count, &start);
  9027. do {
  9028. /* XMSS is stateful. Async queuing not practical. */
  9029. for (times = 0; times < ntimes; ++times) {
  9030. ret = wc_XmssKey_Verify(&key, sig, sigSz, (byte *) msg, msgSz);
  9031. if (ret) {
  9032. printf("wc_XmssKey_Verify failed: %d\n", ret);
  9033. goto exit_xmss_sign_verify;
  9034. }
  9035. }
  9036. count += times;
  9037. } while (bench_stats_check(start));
  9038. exit_xmss_sign_verify:
  9039. bench_stats_asym_finish(params, (int)sigSz, "verify", 0, count, start, ret);
  9040. /* Cleanup everything. */
  9041. if (sig != NULL) {
  9042. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9043. sig = NULL;
  9044. }
  9045. if (sk != NULL) {
  9046. XFREE(sk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9047. sk = NULL;
  9048. }
  9049. if (freeRng) {
  9050. wc_FreeRng(&rng);
  9051. freeRng = 0;
  9052. }
  9053. if (freeKey) {
  9054. wc_XmssKey_Free(&key);
  9055. freeKey = 0;
  9056. }
  9057. return;
  9058. }
  9059. void bench_xmss(int hash)
  9060. {
  9061. /* All NIST SP 800-208 approved SHA256 XMSS/XMSS^MT parameter
  9062. * sets.
  9063. *
  9064. * Note: not testing "XMSS-SHA2_16_256", "XMSS-SHA2_20_256",
  9065. * and "XMSSMT-SHA2_60/3_256", because their keygen can be
  9066. * very slow, their signatures and private keys quite large,
  9067. * and xmss private keys are not portable across different
  9068. * XMSS/XMSS^MT implementations.
  9069. *
  9070. * The bottleneck in key generation is the height of the first
  9071. * level tree (or h/d).
  9072. *
  9073. * h is the total height of the hyper tree, and d the number of
  9074. * trees.
  9075. */
  9076. /* h/d h d */
  9077. #ifdef WC_XMSS_SHA256
  9078. if (hash == WC_HASH_TYPE_SHA256) {
  9079. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  9080. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9081. bench_xmss_sign_verify("XMSS-SHA2_10_256"); /* 10 10 1 */
  9082. #endif
  9083. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9084. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9085. bench_xmss_sign_verify("XMSS-SHA2_16_256"); /* 16 16 1 */
  9086. #endif
  9087. #endif
  9088. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9089. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9090. bench_xmss_sign_verify("XMSS-SHA2_20_256"); /* 20 20 1 */
  9091. #endif
  9092. #endif
  9093. #endif /* HASH_SIZE 256 */
  9094. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 192 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 192
  9095. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9096. bench_xmss_sign_verify("XMSS-SHA2_10_192"); /* 10 10 1 */
  9097. #endif
  9098. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9099. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9100. bench_xmss_sign_verify("XMSS-SHA2_16_192"); /* 16 16 1 */
  9101. #endif
  9102. #endif
  9103. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9104. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9105. bench_xmss_sign_verify("XMSS-SHA2_20_192"); /* 20 20 1 */
  9106. #endif
  9107. #endif
  9108. #endif /* HASH_SIZE 192 */
  9109. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  9110. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9111. bench_xmss_sign_verify("XMSSMT-SHA2_20/2_256"); /* 10 20 2 */
  9112. bench_xmss_sign_verify("XMSSMT-SHA2_20/4_256"); /* 5 20 4 */
  9113. #endif
  9114. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9115. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9116. bench_xmss_sign_verify("XMSSMT-SHA2_40/2_256"); /* 20 40 4 */
  9117. #endif
  9118. bench_xmss_sign_verify("XMSSMT-SHA2_40/4_256"); /* 10 40 4 */
  9119. bench_xmss_sign_verify("XMSSMT-SHA2_40/8_256"); /* 5 40 8 */
  9120. #endif
  9121. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9122. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9123. bench_xmss_sign_verify("XMSSMT-SHA2_60/3_256"); /* 20 60 3 */
  9124. #endif
  9125. bench_xmss_sign_verify("XMSSMT-SHA2_60/6_256"); /* 10 60 6 */
  9126. bench_xmss_sign_verify("XMSSMT-SHA2_60/12_256"); /* 5 60 12 */
  9127. #endif
  9128. #endif /* HASH_SIZE 256 */
  9129. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 192 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 192
  9130. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9131. bench_xmss_sign_verify("XMSSMT-SHA2_20/2_192"); /* 10 20 2 */
  9132. bench_xmss_sign_verify("XMSSMT-SHA2_20/4_192"); /* 5 20 4 */
  9133. #endif
  9134. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9135. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9136. bench_xmss_sign_verify("XMSSMT-SHA2_40/2_192"); /* 20 40 4 */
  9137. #endif
  9138. bench_xmss_sign_verify("XMSSMT-SHA2_40/4_192"); /* 10 40 4 */
  9139. bench_xmss_sign_verify("XMSSMT-SHA2_40/8_192"); /* 5 40 8 */
  9140. #endif
  9141. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9142. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9143. bench_xmss_sign_verify("XMSSMT-SHA2_60/3_192"); /* 20 60 3 */
  9144. #endif
  9145. bench_xmss_sign_verify("XMSSMT-SHA2_60/6_192"); /* 10 60 6 */
  9146. bench_xmss_sign_verify("XMSSMT-SHA2_60/12_192"); /* 5 60 12 */
  9147. #endif
  9148. #endif /* HASH_SIZE 192 */
  9149. }
  9150. #endif
  9151. #ifdef WC_XMSS_SHA512
  9152. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 512 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 512
  9153. if (hash == WC_HASH_TYPE_SHA512) {
  9154. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9155. bench_xmss_sign_verify("XMSS-SHA2_10_512"); /* 10 10 1 */
  9156. #endif
  9157. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9158. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9159. bench_xmss_sign_verify("XMSS-SHA2_16_512"); /* 16 16 1 */
  9160. #endif
  9161. #endif
  9162. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9163. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9164. bench_xmss_sign_verify("XMSS-SHA2_20_512"); /* 20 20 1 */
  9165. #endif
  9166. #endif
  9167. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9168. bench_xmss_sign_verify("XMSSMT-SHA2_20/2_512"); /* 10 20 2 */
  9169. bench_xmss_sign_verify("XMSSMT-SHA2_20/4_512"); /* 5 20 4 */
  9170. #endif
  9171. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9172. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9173. bench_xmss_sign_verify("XMSSMT-SHA2_40/2_512"); /* 20 40 4 */
  9174. #endif
  9175. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9176. bench_xmss_sign_verify("XMSSMT-SHA2_40/4_512"); /* 10 40 4 */
  9177. #endif
  9178. bench_xmss_sign_verify("XMSSMT-SHA2_40/8_512"); /* 5 40 8 */
  9179. #endif
  9180. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9181. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9182. bench_xmss_sign_verify("XMSSMT-SHA2_60/3_512"); /* 20 60 3 */
  9183. #endif
  9184. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9185. bench_xmss_sign_verify("XMSSMT-SHA2_60/6_512"); /* 10 60 6 */
  9186. #endif
  9187. bench_xmss_sign_verify("XMSSMT-SHA2_60/12_512"); /* 5 60 12 */
  9188. #endif
  9189. }
  9190. #endif /* HASH_SIZE 512 */
  9191. #endif
  9192. #ifdef WC_XMSS_SHAKE128
  9193. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  9194. if (hash == WC_HASH_TYPE_SHAKE128) {
  9195. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9196. bench_xmss_sign_verify("XMSS-SHAKE_10_256"); /* 10 10 1 */
  9197. #endif
  9198. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9199. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9200. bench_xmss_sign_verify("XMSS-SHAKE_16_256"); /* 16 16 1 */
  9201. #endif
  9202. #endif
  9203. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9204. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9205. bench_xmss_sign_verify("XMSS-SHAKE_20_256"); /* 20 20 1 */
  9206. #endif
  9207. #endif
  9208. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9209. bench_xmss_sign_verify("XMSSMT-SHAKE_20/2_256"); /* 10 20 2 */
  9210. bench_xmss_sign_verify("XMSSMT-SHAKE_20/4_256"); /* 5 20 4 */
  9211. #endif
  9212. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9213. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9214. bench_xmss_sign_verify("XMSSMT-SHAKE_40/2_256"); /* 20 40 4 */
  9215. #endif
  9216. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9217. bench_xmss_sign_verify("XMSSMT-SHAKE_40/4_256"); /* 10 40 4 */
  9218. #endif
  9219. bench_xmss_sign_verify("XMSSMT-SHAKE_40/8_256"); /* 5 40 8 */
  9220. #endif
  9221. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9222. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9223. bench_xmss_sign_verify("XMSSMT-SHAKE_60/3_256"); /* 20 60 3 */
  9224. #endif
  9225. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9226. bench_xmss_sign_verify("XMSSMT-SHAKE_60/6_256"); /* 10 60 6 */
  9227. #endif
  9228. bench_xmss_sign_verify("XMSSMT-SHAKE_60/12_256"); /* 5 60 12 */
  9229. #endif
  9230. }
  9231. #endif /* HASH_SIZE 256 */
  9232. #endif
  9233. #ifdef WC_XMSS_SHAKE256
  9234. if (hash == WC_HASH_TYPE_SHAKE256) {
  9235. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 512 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 512
  9236. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9237. bench_xmss_sign_verify("XMSS-SHAKE_10_512"); /* 10 10 1 */
  9238. #endif
  9239. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9240. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9241. bench_xmss_sign_verify("XMSS-SHAKE_16_512"); /* 16 16 1 */
  9242. #endif
  9243. #endif
  9244. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9245. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9246. bench_xmss_sign_verify("XMSS-SHAKE_20_512"); /* 20 20 1 */
  9247. #endif
  9248. #endif
  9249. #endif /* HASH_SIZE 512 */
  9250. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  9251. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9252. bench_xmss_sign_verify("XMSS-SHAKE256_10_256"); /* 10 10 1 */
  9253. #endif
  9254. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9255. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9256. bench_xmss_sign_verify("XMSS-SHAKE256_16_256"); /* 16 16 1 */
  9257. #endif
  9258. #endif
  9259. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9260. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9261. bench_xmss_sign_verify("XMSS-SHAKE256_20_256"); /* 20 20 1 */
  9262. #endif
  9263. #endif
  9264. #endif /* HASH_SIZE 256 */
  9265. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 192 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 192
  9266. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9267. bench_xmss_sign_verify("XMSS-SHAKE256_10_192"); /* 10 10 1 */
  9268. #endif
  9269. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9270. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9271. bench_xmss_sign_verify("XMSS-SHAKE256_16_192"); /* 16 16 1 */
  9272. #endif
  9273. #endif
  9274. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9275. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9276. bench_xmss_sign_verify("XMSS-SHAKE256_20_192"); /* 20 20 1 */
  9277. #endif
  9278. #endif
  9279. #endif /* HASH_SIZE 192 */
  9280. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 512 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 512
  9281. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9282. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9283. bench_xmss_sign_verify("XMSSMT-SHAKE_20/2_512"); /* 10 20 2 */
  9284. #endif
  9285. bench_xmss_sign_verify("XMSSMT-SHAKE_20/4_512"); /* 5 20 4 */
  9286. #endif
  9287. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9288. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9289. bench_xmss_sign_verify("XMSSMT-SHAKE_40/2_512"); /* 20 40 4 */
  9290. #endif
  9291. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9292. bench_xmss_sign_verify("XMSSMT-SHAKE_40/4_512"); /* 10 40 4 */
  9293. #endif
  9294. bench_xmss_sign_verify("XMSSMT-SHAKE_40/8_512"); /* 5 40 8 */
  9295. #endif
  9296. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9297. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9298. bench_xmss_sign_verify("XMSSMT-SHAKE_60/3_512"); /* 20 60 3 */
  9299. #endif
  9300. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9301. bench_xmss_sign_verify("XMSSMT-SHAKE_60/6_512"); /* 10 60 6 */
  9302. #endif
  9303. bench_xmss_sign_verify("XMSSMT-SHAKE_60/12_512"); /* 5 60 12 */
  9304. #endif
  9305. #endif /* HASH_SIZE 512 */
  9306. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  9307. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9308. bench_xmss_sign_verify("XMSSMT-SHAKE256_20/2_256"); /* 10 20 2 */
  9309. bench_xmss_sign_verify("XMSSMT-SHAKE256_20/4_256"); /* 5 20 4 */
  9310. #endif
  9311. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9312. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9313. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/2_256"); /* 20 40 4 */
  9314. #endif
  9315. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9316. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/4_256"); /* 10 40 4 */
  9317. #endif
  9318. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/8_256"); /* 5 40 8 */
  9319. #endif
  9320. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9321. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9322. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/3_256"); /* 20 60 3 */
  9323. #endif
  9324. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9325. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/6_256"); /* 10 60 6 */
  9326. #endif
  9327. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/12_256");/* 5 60 12 */
  9328. #endif
  9329. #endif /* HASH_SIZE 256 */
  9330. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 192 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 192
  9331. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9332. bench_xmss_sign_verify("XMSSMT-SHAKE256_20/2_192"); /* 10 20 2 */
  9333. bench_xmss_sign_verify("XMSSMT-SHAKE256_20/4_192"); /* 5 20 4 */
  9334. #endif
  9335. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9336. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9337. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/2_192"); /* 20 40 4 */
  9338. #endif
  9339. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9340. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/4_192"); /* 10 40 4 */
  9341. #endif
  9342. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/8_192"); /* 5 40 8 */
  9343. #endif
  9344. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9345. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9346. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/3_192"); /* 20 60 3 */
  9347. #endif
  9348. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9349. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/6_192"); /* 10 60 6 */
  9350. #endif
  9351. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/12_192");/* 5 60 12 */
  9352. #endif
  9353. #endif /* HASH_SIZE 192 */
  9354. }
  9355. #endif
  9356. return;
  9357. }
  9358. #endif /* if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY) */
  9359. #ifdef HAVE_ECC
  9360. /* Maximum ECC name plus null terminator:
  9361. * "ECC [%15s]" and "ECDHE [%15s]" and "ECDSA [%15s]" */
  9362. #define BENCH_ECC_NAME_SZ (ECC_MAXNAME + 8)
  9363. /* run all benchmarks on a curve */
  9364. void bench_ecc_curve(int curveId)
  9365. {
  9366. if (bench_all || (bench_asym_algs & BENCH_ECC_MAKEKEY)) {
  9367. #ifndef NO_SW_BENCH
  9368. bench_eccMakeKey(0, curveId);
  9369. #endif
  9370. #if defined(BENCH_DEVID)
  9371. bench_eccMakeKey(1, curveId);
  9372. #endif
  9373. }
  9374. if (bench_all || (bench_asym_algs & BENCH_ECC)) {
  9375. #ifndef NO_SW_BENCH
  9376. bench_ecc(0, curveId);
  9377. #endif
  9378. #if defined(BENCH_DEVID)
  9379. bench_ecc(1, curveId);
  9380. #endif
  9381. }
  9382. #ifdef HAVE_ECC_ENCRYPT
  9383. if (bench_all || (bench_asym_algs & BENCH_ECC_ENCRYPT))
  9384. bench_eccEncrypt(curveId);
  9385. #endif
  9386. }
  9387. void bench_eccMakeKey(int useDeviceID, int curveId)
  9388. {
  9389. int ret = 0, i, times, count = 0, pending = 0;
  9390. int deviceID;
  9391. int keySize = 0;
  9392. WC_DECLARE_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9393. sizeof(ecc_key), HEAP_HINT);
  9394. char name[BENCH_ECC_NAME_SZ];
  9395. double start = 0;
  9396. const char**desc = bench_desc_words[lng_index];
  9397. DECLARE_MULTI_VALUE_STATS_VARS()
  9398. WC_CALLOC_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9399. sizeof(ecc_key), HEAP_HINT);
  9400. deviceID = useDeviceID ? devId : INVALID_DEVID;
  9401. keySize = wc_ecc_get_curve_size_from_id(curveId);
  9402. /* ECC Make Key */
  9403. bench_stats_start(&count, &start);
  9404. do {
  9405. /* while free pending slots in queue, submit ops */
  9406. for (times = 0; times < agreeTimes || pending > 0; ) {
  9407. bench_async_poll(&pending);
  9408. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9409. if (bench_async_check(&ret,
  9410. BENCH_ASYNC_GET_DEV(genKey[i]), 0,
  9411. &times, agreeTimes, &pending)) {
  9412. wc_ecc_free(genKey[i]);
  9413. ret = wc_ecc_init_ex(genKey[i], HEAP_HINT, deviceID);
  9414. if (ret < 0) {
  9415. goto exit;
  9416. }
  9417. ret = wc_ecc_make_key_ex(&gRng, keySize, genKey[i],
  9418. curveId);
  9419. if (!bench_async_handle(&ret,
  9420. BENCH_ASYNC_GET_DEV(genKey[i]), 0, &times,
  9421. &pending)) {
  9422. goto exit;
  9423. }
  9424. }
  9425. } /* for i */
  9426. RECORD_MULTI_VALUE_STATS();
  9427. } /* for times */
  9428. count += times;
  9429. } while (bench_stats_check(start)
  9430. #ifdef MULTI_VALUE_STATISTICS
  9431. || runs < minimum_runs
  9432. #endif
  9433. );
  9434. exit:
  9435. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECC [%15s]",
  9436. wc_ecc_get_name(curveId));
  9437. bench_stats_asym_finish(name, keySize * 8, desc[2],
  9438. useDeviceID, count, start, ret);
  9439. #ifdef MULTI_VALUE_STATISTICS
  9440. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9441. #endif
  9442. /* cleanup */
  9443. if (WC_ARRAY_OK(genKey)) {
  9444. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9445. wc_ecc_free(genKey[i]);
  9446. }
  9447. WC_FREE_ARRAY(genKey, BENCH_MAX_PENDING, HEAP_HINT);
  9448. }
  9449. }
  9450. void bench_ecc(int useDeviceID, int curveId)
  9451. {
  9452. int ret = 0, i, times, count, pending = 0;
  9453. int deviceID;
  9454. int keySize;
  9455. char name[BENCH_ECC_NAME_SZ];
  9456. WC_DECLARE_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9457. sizeof(ecc_key), HEAP_HINT);
  9458. #ifdef HAVE_ECC_DHE
  9459. WC_DECLARE_ARRAY(genKey2, ecc_key, BENCH_MAX_PENDING,
  9460. sizeof(ecc_key), HEAP_HINT);
  9461. #endif
  9462. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9463. #ifdef HAVE_ECC_VERIFY
  9464. int verify[BENCH_MAX_PENDING];
  9465. #endif
  9466. #endif
  9467. word32 x[BENCH_MAX_PENDING];
  9468. double start = 0;
  9469. const char**desc = bench_desc_words[lng_index];
  9470. DECLARE_MULTI_VALUE_STATS_VARS()
  9471. #ifdef HAVE_ECC_DHE
  9472. WC_DECLARE_ARRAY(shared, byte,
  9473. BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9474. #endif
  9475. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9476. WC_DECLARE_ARRAY(sig, byte,
  9477. BENCH_MAX_PENDING, ECC_MAX_SIG_SIZE, HEAP_HINT);
  9478. WC_DECLARE_ARRAY(digest, byte,
  9479. BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9480. #endif
  9481. /* old scan-build misfires -Wmaybe-uninitialized on these. */
  9482. XMEMSET(sig, 0, sizeof(sig));
  9483. XMEMSET(digest, 0, sizeof(digest));
  9484. XMEMSET(shared, 0, sizeof(shared));
  9485. WC_CALLOC_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9486. sizeof(ecc_key), HEAP_HINT);
  9487. #ifdef HAVE_ECC_DHE
  9488. WC_CALLOC_ARRAY(genKey2, ecc_key, BENCH_MAX_PENDING,
  9489. sizeof(ecc_key), HEAP_HINT);
  9490. WC_ALLOC_ARRAY(shared, byte,
  9491. BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9492. #endif
  9493. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9494. WC_ALLOC_ARRAY(sig, byte, BENCH_MAX_PENDING, ECC_MAX_SIG_SIZE, HEAP_HINT);
  9495. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9496. #endif
  9497. deviceID = useDeviceID ? devId : INVALID_DEVID;
  9498. keySize = wc_ecc_get_curve_size_from_id(curveId);
  9499. /* init keys */
  9500. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9501. /* setup an context for each key */
  9502. if ((ret = wc_ecc_init_ex(genKey[i], HEAP_HINT, deviceID)) < 0) {
  9503. goto exit;
  9504. }
  9505. ret = wc_ecc_make_key_ex(&gRng, keySize, genKey[i], curveId);
  9506. #ifdef WOLFSSL_ASYNC_CRYPT
  9507. ret = wc_AsyncWait(ret, &genKey[i]->asyncDev, WC_ASYNC_FLAG_NONE);
  9508. #endif
  9509. if (ret < 0) {
  9510. goto exit;
  9511. }
  9512. #ifdef HAVE_ECC_DHE
  9513. if ((ret = wc_ecc_init_ex(genKey2[i], HEAP_HINT, deviceID)) < 0) {
  9514. goto exit;
  9515. }
  9516. if ((ret = wc_ecc_make_key_ex(&gRng, keySize, genKey2[i],
  9517. curveId)) > 0) {
  9518. goto exit;
  9519. }
  9520. #endif
  9521. }
  9522. #ifdef HAVE_ECC_DHE
  9523. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  9524. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  9525. !defined(HAVE_SELFTEST)
  9526. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9527. (void)wc_ecc_set_rng(genKey[i], &gRng);
  9528. }
  9529. #endif
  9530. /* ECC Shared Secret */
  9531. bench_stats_start(&count, &start);
  9532. PRIVATE_KEY_UNLOCK();
  9533. do {
  9534. for (times = 0; times < agreeTimes || pending > 0; ) {
  9535. bench_async_poll(&pending);
  9536. /* while free pending slots in queue, submit ops */
  9537. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9538. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  9539. &times, agreeTimes, &pending)) {
  9540. x[i] = (word32)keySize;
  9541. ret = wc_ecc_shared_secret(genKey[i], genKey2[i],
  9542. shared[i], &x[i]);
  9543. if (!bench_async_handle(&ret,
  9544. BENCH_ASYNC_GET_DEV(genKey[i]), 1, &times,
  9545. &pending)) {
  9546. goto exit_ecdhe;
  9547. }
  9548. }
  9549. } /* for i */
  9550. RECORD_MULTI_VALUE_STATS();
  9551. } /* for times */
  9552. count += times;
  9553. } while (bench_stats_check(start)
  9554. #ifdef MULTI_VALUE_STATISTICS
  9555. || runs < minimum_runs
  9556. #endif
  9557. );
  9558. PRIVATE_KEY_UNLOCK();
  9559. exit_ecdhe:
  9560. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDHE [%15s]",
  9561. wc_ecc_get_name(curveId));
  9562. bench_stats_asym_finish(name, keySize * 8, desc[3],
  9563. useDeviceID, count, start, ret);
  9564. #ifdef MULTI_VALUE_STATISTICS
  9565. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9566. #endif
  9567. RESET_MULTI_VALUE_STATS_VARS();
  9568. if (ret < 0) {
  9569. goto exit;
  9570. }
  9571. #endif /* HAVE_ECC_DHE */
  9572. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9573. /* Init digest to sign */
  9574. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9575. for (count = 0; count < keySize; count++) {
  9576. digest[i][count] = (byte)count;
  9577. }
  9578. }
  9579. /* ECC Sign */
  9580. bench_stats_start(&count, &start);
  9581. do {
  9582. for (times = 0; times < agreeTimes || pending > 0; ) {
  9583. bench_async_poll(&pending);
  9584. /* while free pending slots in queue, submit ops */
  9585. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9586. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  9587. &times, agreeTimes, &pending)) {
  9588. if (genKey[i]->state == 0) {
  9589. x[i] = ECC_MAX_SIG_SIZE;
  9590. }
  9591. ret = wc_ecc_sign_hash(digest[i], (word32)keySize, sig[i],
  9592. &x[i], GLOBAL_RNG, genKey[i]);
  9593. if (!bench_async_handle(&ret,
  9594. BENCH_ASYNC_GET_DEV(genKey[i]), 1, &times,
  9595. &pending)) {
  9596. goto exit_ecdsa_sign;
  9597. }
  9598. } /* bench_async_check */
  9599. } /* for i */
  9600. RECORD_MULTI_VALUE_STATS();
  9601. } /* for times */
  9602. count += times;
  9603. } while (bench_stats_check(start)
  9604. #ifdef MULTI_VALUE_STATISTICS
  9605. || runs < minimum_runs
  9606. #endif
  9607. );
  9608. exit_ecdsa_sign:
  9609. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDSA [%15s]",
  9610. wc_ecc_get_name(curveId));
  9611. bench_stats_asym_finish(name, keySize * 8, desc[4],
  9612. useDeviceID, count, start, ret);
  9613. #ifdef MULTI_VALUE_STATISTICS
  9614. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9615. #endif
  9616. RESET_MULTI_VALUE_STATS_VARS();
  9617. if (ret < 0) {
  9618. goto exit;
  9619. }
  9620. #ifdef HAVE_ECC_VERIFY
  9621. /* ECC Verify */
  9622. bench_stats_start(&count, &start);
  9623. do {
  9624. for (times = 0; times < agreeTimes || pending > 0; ) {
  9625. bench_async_poll(&pending);
  9626. /* while free pending slots in queue, submit ops */
  9627. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9628. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  9629. &times, agreeTimes, &pending)) {
  9630. if (genKey[i]->state == 0) {
  9631. verify[i] = 0;
  9632. }
  9633. ret = wc_ecc_verify_hash(sig[i], x[i], digest[i],
  9634. (word32)keySize, &verify[i],
  9635. genKey[i]);
  9636. if (!bench_async_handle(&ret,
  9637. BENCH_ASYNC_GET_DEV(genKey[i]),
  9638. 1, &times,
  9639. &pending)) {
  9640. goto exit_ecdsa_verify;
  9641. }
  9642. } /* if bench_async_check */
  9643. } /* for i */
  9644. RECORD_MULTI_VALUE_STATS();
  9645. } /* for times */
  9646. count += times;
  9647. } while (bench_stats_check(start)
  9648. #ifdef MULTI_VALUE_STATISTICS
  9649. || runs < minimum_runs
  9650. #endif
  9651. );
  9652. exit_ecdsa_verify:
  9653. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDSA [%15s]",
  9654. wc_ecc_get_name(curveId));
  9655. bench_stats_asym_finish(name, keySize * 8, desc[5],
  9656. useDeviceID, count, start, ret);
  9657. #ifdef MULTI_VALUE_STATISTICS
  9658. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9659. #endif
  9660. #endif /* HAVE_ECC_VERIFY */
  9661. #endif /* !NO_ASN && HAVE_ECC_SIGN */
  9662. exit:
  9663. /* cleanup */
  9664. if (WC_ARRAY_OK(genKey)) {
  9665. for (i = 0; i < BENCH_MAX_PENDING; i++)
  9666. wc_ecc_free(genKey[i]);
  9667. WC_FREE_ARRAY(genKey, BENCH_MAX_PENDING, HEAP_HINT);
  9668. }
  9669. #ifdef HAVE_ECC_DHE
  9670. if (WC_ARRAY_OK(genKey2)) {
  9671. for (i = 0; i < BENCH_MAX_PENDING; i++)
  9672. wc_ecc_free(genKey2[i]);
  9673. WC_FREE_ARRAY(genKey2, BENCH_MAX_PENDING, HEAP_HINT);
  9674. }
  9675. #endif
  9676. #ifdef HAVE_ECC_DHE
  9677. WC_FREE_ARRAY(shared, BENCH_MAX_PENDING, HEAP_HINT);
  9678. #endif
  9679. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9680. WC_FREE_ARRAY(sig, BENCH_MAX_PENDING, HEAP_HINT);
  9681. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  9682. #endif
  9683. (void)useDeviceID;
  9684. (void)pending;
  9685. (void)x;
  9686. (void)count;
  9687. (void)times;
  9688. (void)desc;
  9689. (void)start;
  9690. (void)name;
  9691. }
  9692. #ifdef HAVE_ECC_ENCRYPT
  9693. void bench_eccEncrypt(int curveId)
  9694. {
  9695. #define BENCH_ECCENCRYPT_MSG_SIZE 48
  9696. #define BENCH_ECCENCRYPT_OUT_SIZE (BENCH_ECCENCRYPT_MSG_SIZE + \
  9697. WC_SHA256_DIGEST_SIZE + \
  9698. (MAX_ECC_BITS+3)/4 + 2)
  9699. word32 outSz = BENCH_ECCENCRYPT_OUT_SIZE;
  9700. #ifdef WOLFSSL_SMALL_STACK
  9701. ecc_key *userA = NULL, *userB = NULL;
  9702. byte *msg = NULL;
  9703. byte *out = NULL;
  9704. #else
  9705. ecc_key userA[1], userB[1];
  9706. byte msg[BENCH_ECCENCRYPT_MSG_SIZE];
  9707. byte out[BENCH_ECCENCRYPT_OUT_SIZE];
  9708. #endif
  9709. char name[BENCH_ECC_NAME_SZ];
  9710. int keySize;
  9711. word32 bench_plainSz = bench_size;
  9712. int ret, i, count;
  9713. double start;
  9714. const char**desc = bench_desc_words[lng_index];
  9715. DECLARE_MULTI_VALUE_STATS_VARS()
  9716. #ifdef WOLFSSL_SMALL_STACK
  9717. userA = (ecc_key *)XMALLOC(sizeof(*userA),
  9718. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9719. userB = (ecc_key *)XMALLOC(sizeof(*userB),
  9720. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9721. msg = (byte *)XMALLOC(BENCH_ECCENCRYPT_MSG_SIZE,
  9722. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9723. out = (byte *)XMALLOC(outSz,
  9724. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9725. if ((! userA) || (! userB) || (! msg) || (! out)) {
  9726. printf("bench_eccEncrypt malloc failed\n");
  9727. goto exit;
  9728. }
  9729. #endif
  9730. keySize = wc_ecc_get_curve_size_from_id(curveId);
  9731. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  9732. if (ret != 0) {
  9733. printf("wc_ecc_encrypt make key A failed: %d\n", ret);
  9734. goto exit;
  9735. }
  9736. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  9737. if (ret != 0) {
  9738. printf("wc_ecc_encrypt make key B failed: %d\n", ret);
  9739. goto exit;
  9740. }
  9741. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  9742. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  9743. !defined(HAVE_SELFTEST)
  9744. ret = wc_ecc_set_rng(userA, &gRng);
  9745. if (ret != 0) {
  9746. goto exit;
  9747. }
  9748. ret = wc_ecc_set_rng(userB, &gRng);
  9749. if (ret != 0) {
  9750. goto exit;
  9751. }
  9752. #endif
  9753. ret = wc_ecc_make_key_ex(&gRng, keySize, userA, curveId);
  9754. #ifdef WOLFSSL_ASYNC_CRYPT
  9755. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  9756. #endif
  9757. if (ret != 0)
  9758. goto exit;
  9759. ret = wc_ecc_make_key_ex(&gRng, keySize, userB, curveId);
  9760. #ifdef WOLFSSL_ASYNC_CRYPT
  9761. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  9762. #endif
  9763. if (ret != 0)
  9764. goto exit;
  9765. for (i = 0; i < BENCH_ECCENCRYPT_MSG_SIZE; i++) {
  9766. msg[i] = (byte)i;
  9767. }
  9768. bench_stats_start(&count, &start);
  9769. do {
  9770. for (i = 0; i < ntimes; i++) {
  9771. /* encrypt msg to B */
  9772. ret = wc_ecc_encrypt(userA, userB, msg, BENCH_ECCENCRYPT_MSG_SIZE,
  9773. out, &outSz, NULL);
  9774. if (ret != 0) {
  9775. printf("wc_ecc_encrypt failed! %d\n", ret);
  9776. goto exit_enc;
  9777. }
  9778. RECORD_MULTI_VALUE_STATS();
  9779. }
  9780. count += i;
  9781. } while (bench_stats_check(start)
  9782. #ifdef MULTI_VALUE_STATISTICS
  9783. || runs < minimum_runs
  9784. #endif
  9785. );
  9786. exit_enc:
  9787. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECC [%15s]",
  9788. wc_ecc_get_name(curveId));
  9789. bench_stats_asym_finish(name, keySize * 8, desc[6], 0, count, start, ret);
  9790. #ifdef MULTI_VALUE_STATISTICS
  9791. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9792. #endif
  9793. RESET_MULTI_VALUE_STATS_VARS();
  9794. if (ret != 0)
  9795. goto exit;
  9796. bench_stats_start(&count, &start);
  9797. do {
  9798. for (i = 0; i < ntimes; i++) {
  9799. /* decrypt msg from A */
  9800. ret = wc_ecc_decrypt(userB, userA, out, outSz, bench_plain,
  9801. &bench_plainSz, NULL);
  9802. if (ret != 0) {
  9803. printf("wc_ecc_decrypt failed! %d\n", ret);
  9804. goto exit_dec;
  9805. }
  9806. RECORD_MULTI_VALUE_STATS();
  9807. }
  9808. count += i;
  9809. } while (bench_stats_check(start)
  9810. #ifdef MULTI_VALUE_STATISTICS
  9811. || runs < minimum_runs
  9812. #endif
  9813. );
  9814. exit_dec:
  9815. bench_stats_asym_finish(name, keySize * 8, desc[7], 0, count, start, ret);
  9816. #ifdef MULTI_VALUE_STATISTICS
  9817. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9818. #endif
  9819. exit:
  9820. /* cleanup */
  9821. #ifdef WOLFSSL_SMALL_STACK
  9822. if (userA) {
  9823. wc_ecc_free(userA);
  9824. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9825. }
  9826. if (userB) {
  9827. wc_ecc_free(userB);
  9828. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9829. }
  9830. if (msg)
  9831. XFREE(msg, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9832. if (out)
  9833. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9834. #else
  9835. wc_ecc_free(userB);
  9836. wc_ecc_free(userA);
  9837. #endif
  9838. }
  9839. #endif
  9840. #ifdef WOLFSSL_SM2
  9841. static void bench_sm2_MakeKey(int useDeviceID)
  9842. {
  9843. int ret = 0, i, times, count, pending = 0;
  9844. int deviceID;
  9845. int keySize;
  9846. WC_DECLARE_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9847. sizeof(ecc_key), HEAP_HINT);
  9848. char name[BENCH_ECC_NAME_SZ];
  9849. double start;
  9850. const char**desc = bench_desc_words[lng_index];
  9851. DECLARE_MULTI_VALUE_STATS_VARS()
  9852. deviceID = useDeviceID ? devId : INVALID_DEVID;
  9853. keySize = wc_ecc_get_curve_size_from_id(ECC_SM2P256V1);
  9854. WC_CALLOC_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9855. sizeof(ecc_key), HEAP_HINT);
  9856. /* ECC Make Key */
  9857. bench_stats_start(&count, &start);
  9858. do {
  9859. /* while free pending slots in queue, submit ops */
  9860. for (times = 0; times < agreeTimes || pending > 0; ) {
  9861. bench_async_poll(&pending);
  9862. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9863. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 0,
  9864. &times, agreeTimes, &pending)) {
  9865. wc_ecc_free(genKey[i]);
  9866. ret = wc_ecc_init_ex(genKey[i], HEAP_HINT, deviceID);
  9867. if (ret < 0) {
  9868. goto exit;
  9869. }
  9870. ret = wc_ecc_sm2_make_key(&gRng, genKey[i],
  9871. WC_ECC_FLAG_NONE);
  9872. if (!bench_async_handle(&ret,
  9873. BENCH_ASYNC_GET_DEV(genKey[i]), 0, &times,
  9874. &pending)) {
  9875. goto exit;
  9876. }
  9877. }
  9878. } /* for i */
  9879. RECORD_MULTI_VALUE_STATS();
  9880. } /* for times */
  9881. count += times;
  9882. } while (bench_stats_check(start)
  9883. #ifdef MULTI_VALUE_STATISTICS
  9884. || runs < minimum_runs
  9885. #endif
  9886. );
  9887. exit:
  9888. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECC [%15s]",
  9889. wc_ecc_get_name(ECC_SM2P256V1));
  9890. bench_stats_asym_finish(name, keySize * 8, desc[2], useDeviceID, count,
  9891. start, ret);
  9892. #ifdef MULTI_VALUE_STATISTICS
  9893. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9894. #endif
  9895. /* cleanup */
  9896. if (WC_ARRAY_OK(genKey)) {
  9897. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9898. wc_ecc_free(genKey[i]);
  9899. }
  9900. WC_FREE_ARRAY(genKey, BENCH_MAX_PENDING, HEAP_HINT);
  9901. }
  9902. }
  9903. void bench_sm2(int useDeviceID)
  9904. {
  9905. int ret = 0, i, times, count, pending = 0;
  9906. int deviceID;
  9907. int keySize;
  9908. char name[BENCH_ECC_NAME_SZ];
  9909. WC_DECLARE_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9910. sizeof(ecc_key), HEAP_HINT);
  9911. #ifdef HAVE_ECC_DHE
  9912. WC_DECLARE_ARRAY(genKey2, ecc_key, BENCH_MAX_PENDING,
  9913. sizeof(ecc_key), HEAP_HINT);
  9914. #endif
  9915. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9916. #ifdef HAVE_ECC_VERIFY
  9917. int verify[BENCH_MAX_PENDING];
  9918. #endif
  9919. #endif
  9920. word32 x[BENCH_MAX_PENDING];
  9921. double start = 0;
  9922. const char**desc = bench_desc_words[lng_index];
  9923. DECLARE_MULTI_VALUE_STATS_VARS()
  9924. #ifdef HAVE_ECC_DHE
  9925. WC_DECLARE_ARRAY(shared, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9926. #endif
  9927. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9928. WC_DECLARE_ARRAY(sig, byte, BENCH_MAX_PENDING, ECC_MAX_SIG_SIZE, HEAP_HINT);
  9929. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9930. #endif
  9931. #ifdef HAVE_ECC_DHE
  9932. WC_ALLOC_ARRAY(shared, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9933. #endif
  9934. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9935. WC_ALLOC_ARRAY(sig, byte, BENCH_MAX_PENDING, ECC_MAX_SIG_SIZE, HEAP_HINT);
  9936. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9937. #endif
  9938. deviceID = useDeviceID ? devId : INVALID_DEVID;
  9939. bench_sm2_MakeKey(useDeviceID);
  9940. WC_CALLOC_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9941. sizeof(ecc_key), HEAP_HINT);
  9942. #ifdef HAVE_ECC_DHE
  9943. WC_CALLOC_ARRAY(genKey2, ecc_key, BENCH_MAX_PENDING,
  9944. sizeof(ecc_key), HEAP_HINT);
  9945. #endif
  9946. keySize = wc_ecc_get_curve_size_from_id(ECC_SM2P256V1);
  9947. /* init keys */
  9948. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9949. /* setup an context for each key */
  9950. if ((ret = wc_ecc_init_ex(genKey[i], HEAP_HINT, deviceID)) < 0) {
  9951. goto exit;
  9952. }
  9953. ret = wc_ecc_sm2_make_key(&gRng, genKey[i], WC_ECC_FLAG_NONE);
  9954. #ifdef WOLFSSL_ASYNC_CRYPT
  9955. ret = wc_AsyncWait(ret, genKey[i].asyncDev, WC_ASYNC_FLAG_NONE);
  9956. #endif
  9957. if (ret < 0) {
  9958. goto exit;
  9959. }
  9960. #ifdef HAVE_ECC_DHE
  9961. if ((ret = wc_ecc_init_ex(genKey2[i], HEAP_HINT, deviceID)) < 0) {
  9962. goto exit;
  9963. }
  9964. if ((ret = wc_ecc_sm2_make_key(&gRng, genKey2[i],
  9965. WC_ECC_FLAG_NONE)) > 0) {
  9966. goto exit;
  9967. }
  9968. #endif
  9969. }
  9970. #ifdef HAVE_ECC_DHE
  9971. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  9972. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  9973. !defined(HAVE_SELFTEST)
  9974. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9975. (void)wc_ecc_set_rng(genKey[i], &gRng);
  9976. }
  9977. #endif
  9978. /* ECC Shared Secret */
  9979. bench_stats_start(&count, &start);
  9980. PRIVATE_KEY_UNLOCK();
  9981. do {
  9982. for (times = 0; times < agreeTimes || pending > 0; ) {
  9983. bench_async_poll(&pending);
  9984. /* while free pending slots in queue, submit ops */
  9985. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9986. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  9987. &times, agreeTimes, &pending)) {
  9988. x[i] = (word32)keySize;
  9989. ret = wc_ecc_sm2_shared_secret(genKey[i], genKey2[i],
  9990. shared[i], &x[i]);
  9991. if (!bench_async_handle(&ret,
  9992. BENCH_ASYNC_GET_DEV(genKey[i]), 1, &times,
  9993. &pending)) {
  9994. goto exit_ecdhe;
  9995. }
  9996. }
  9997. } /* for i */
  9998. RECORD_MULTI_VALUE_STATS();
  9999. } /* for times */
  10000. count += times;
  10001. } while (bench_stats_check(start)
  10002. #ifdef MULTI_VALUE_STATISTICS
  10003. || runs < minimum_runs
  10004. #endif
  10005. );
  10006. PRIVATE_KEY_UNLOCK();
  10007. exit_ecdhe:
  10008. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDHE [%15s]",
  10009. wc_ecc_get_name(ECC_SM2P256V1));
  10010. bench_stats_asym_finish(name, keySize * 8, desc[3], useDeviceID, count,
  10011. start, ret);
  10012. #ifdef MULTI_VALUE_STATISTICS
  10013. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10014. #endif
  10015. if (ret < 0) {
  10016. goto exit;
  10017. }
  10018. #endif /* HAVE_ECC_DHE */
  10019. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  10020. /* Init digest to sign */
  10021. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  10022. for (count = 0; count < keySize; count++) {
  10023. digest[i][count] = (byte)count;
  10024. }
  10025. }
  10026. RESET_MULTI_VALUE_STATS_VARS();
  10027. /* ECC Sign */
  10028. bench_stats_start(&count, &start);
  10029. do {
  10030. for (times = 0; times < agreeTimes || pending > 0; ) {
  10031. bench_async_poll(&pending);
  10032. /* while free pending slots in queue, submit ops */
  10033. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  10034. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  10035. &times, agreeTimes, &pending)) {
  10036. if (genKey[i]->state == 0)
  10037. x[i] = ECC_MAX_SIG_SIZE;
  10038. ret = wc_ecc_sm2_sign_hash(digest[i], (word32)keySize,
  10039. sig[i], &x[i], &gRng, genKey[i]);
  10040. if (!bench_async_handle(&ret,
  10041. BENCH_ASYNC_GET_DEV(genKey[i]), 1, &times,
  10042. &pending)) {
  10043. goto exit_ecdsa_sign;
  10044. }
  10045. }
  10046. } /* for i */
  10047. RECORD_MULTI_VALUE_STATS();
  10048. } /* for times */
  10049. count += times;
  10050. } while (bench_stats_check(start)
  10051. #ifdef MULTI_VALUE_STATISTICS
  10052. || runs < minimum_runs
  10053. #endif
  10054. );
  10055. exit_ecdsa_sign:
  10056. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDSA [%15s]",
  10057. wc_ecc_get_name(ECC_SM2P256V1));
  10058. bench_stats_asym_finish(name, keySize * 8, desc[4], useDeviceID, count,
  10059. start, ret);
  10060. #ifdef MULTI_VALUE_STATISTICS
  10061. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10062. #endif
  10063. if (ret < 0) {
  10064. goto exit;
  10065. }
  10066. #ifdef HAVE_ECC_VERIFY
  10067. /* ECC Verify */
  10068. bench_stats_start(&count, &start);
  10069. do {
  10070. for (times = 0; times < agreeTimes || pending > 0; ) {
  10071. bench_async_poll(&pending);
  10072. /* while free pending slots in queue, submit ops */
  10073. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  10074. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  10075. &times, agreeTimes, &pending)) {
  10076. if (genKey[i]->state == 0)
  10077. verify[i] = 0;
  10078. ret = wc_ecc_sm2_verify_hash(sig[i], x[i], digest[i],
  10079. (word32)keySize, &verify[i], genKey[i]);
  10080. if (!bench_async_handle(&ret,
  10081. BENCH_ASYNC_GET_DEV(genKey[i]), 1, &times,
  10082. &pending)) {
  10083. goto exit_ecdsa_verify;
  10084. }
  10085. }
  10086. } /* for i */
  10087. RECORD_MULTI_VALUE_STATS();
  10088. } /* for times */
  10089. count += times;
  10090. } while (bench_stats_check(start)
  10091. #ifdef MULTI_VALUE_STATISTICS
  10092. || runs < minimum_runs
  10093. #endif
  10094. );
  10095. exit_ecdsa_verify:
  10096. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDSA [%15s]",
  10097. wc_ecc_get_name(ECC_SM2P256V1));
  10098. bench_stats_asym_finish(name, keySize * 8, desc[5], useDeviceID, count,
  10099. start, ret);
  10100. #ifdef MULTI_VALUE_STATISTICS
  10101. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10102. #endif
  10103. #endif /* HAVE_ECC_VERIFY */
  10104. #endif /* !NO_ASN && HAVE_ECC_SIGN */
  10105. exit:
  10106. /* cleanup */
  10107. if (WC_ARRAY_OK(genKey)) {
  10108. for (i = 0; i < BENCH_MAX_PENDING; i++)
  10109. wc_ecc_free(genKey[i]);
  10110. WC_FREE_ARRAY(genKey, BENCH_MAX_PENDING, HEAP_HINT);
  10111. }
  10112. #ifdef HAVE_ECC_DHE
  10113. if (WC_ARRAY_OK(genKey2)) {
  10114. for (i = 0; i < BENCH_MAX_PENDING; i++)
  10115. wc_ecc_free(genKey2[i]);
  10116. WC_FREE_ARRAY(genKey2, BENCH_MAX_PENDING, HEAP_HINT);
  10117. }
  10118. #endif
  10119. #ifdef HAVE_ECC_DHE
  10120. WC_FREE_ARRAY(shared, BENCH_MAX_PENDING, HEAP_HINT);
  10121. #endif
  10122. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  10123. WC_FREE_ARRAY(sig, BENCH_MAX_PENDING, HEAP_HINT);
  10124. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  10125. #endif
  10126. (void)useDeviceID;
  10127. (void)pending;
  10128. (void)x;
  10129. (void)count;
  10130. (void)times;
  10131. (void)desc;
  10132. (void)start;
  10133. (void)name;
  10134. }
  10135. #endif /* WOLFSSL_SM2 */
  10136. #endif /* HAVE_ECC */
  10137. #ifdef HAVE_CURVE25519
  10138. void bench_curve25519KeyGen(int useDeviceID)
  10139. {
  10140. curve25519_key genKey;
  10141. double start;
  10142. int ret = 0, i, count;
  10143. const char**desc = bench_desc_words[lng_index];
  10144. DECLARE_MULTI_VALUE_STATS_VARS()
  10145. /* Key Gen */
  10146. bench_stats_start(&count, &start);
  10147. do {
  10148. for (i = 0; i < genTimes; i++) {
  10149. ret = wc_curve25519_init_ex(&genKey, HEAP_HINT,
  10150. useDeviceID ? devId : INVALID_DEVID);
  10151. if (ret != 0) {
  10152. printf("wc_curve25519_init_ex failed: %d\n", ret);
  10153. break;
  10154. }
  10155. ret = wc_curve25519_make_key(&gRng, 32, &genKey);
  10156. wc_curve25519_free(&genKey);
  10157. if (ret != 0) {
  10158. printf("wc_curve25519_make_key failed: %d\n", ret);
  10159. break;
  10160. }
  10161. RECORD_MULTI_VALUE_STATS();
  10162. }
  10163. count += i;
  10164. } while (bench_stats_check(start)
  10165. #ifdef MULTI_VALUE_STATISTICS
  10166. || runs < minimum_runs
  10167. #endif
  10168. );
  10169. bench_stats_asym_finish("CURVE", 25519, desc[2], useDeviceID, count, start,
  10170. ret);
  10171. #ifdef MULTI_VALUE_STATISTICS
  10172. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10173. #endif
  10174. }
  10175. #ifdef HAVE_CURVE25519_SHARED_SECRET
  10176. void bench_curve25519KeyAgree(int useDeviceID)
  10177. {
  10178. curve25519_key genKey, genKey2;
  10179. double start;
  10180. int ret, i, count;
  10181. byte shared[32];
  10182. const char**desc = bench_desc_words[lng_index];
  10183. word32 x = 0;
  10184. DECLARE_MULTI_VALUE_STATS_VARS()
  10185. wc_curve25519_init_ex(&genKey, HEAP_HINT,
  10186. useDeviceID ? devId : INVALID_DEVID);
  10187. wc_curve25519_init_ex(&genKey2, HEAP_HINT,
  10188. useDeviceID ? devId : INVALID_DEVID);
  10189. ret = wc_curve25519_make_key(&gRng, 32, &genKey);
  10190. if (ret != 0) {
  10191. printf("curve25519_make_key failed\n");
  10192. return;
  10193. }
  10194. ret = wc_curve25519_make_key(&gRng, 32, &genKey2);
  10195. if (ret != 0) {
  10196. printf("curve25519_make_key failed: %d\n", ret);
  10197. wc_curve25519_free(&genKey);
  10198. return;
  10199. }
  10200. /* Shared secret */
  10201. bench_stats_start(&count, &start);
  10202. do {
  10203. for (i = 0; i < agreeTimes; i++) {
  10204. x = sizeof(shared);
  10205. ret = wc_curve25519_shared_secret(&genKey, &genKey2, shared, &x);
  10206. if (ret != 0) {
  10207. printf("curve25519_shared_secret failed: %d\n", ret);
  10208. goto exit;
  10209. }
  10210. RECORD_MULTI_VALUE_STATS();
  10211. }
  10212. count += i;
  10213. } while (bench_stats_check(start)
  10214. #ifdef MULTI_VALUE_STATISTICS
  10215. || runs < minimum_runs
  10216. #endif
  10217. );
  10218. exit:
  10219. bench_stats_asym_finish("CURVE", 25519, desc[3], useDeviceID, count, start,
  10220. ret);
  10221. #ifdef MULTI_VALUE_STATISTICS
  10222. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10223. #endif
  10224. wc_curve25519_free(&genKey2);
  10225. wc_curve25519_free(&genKey);
  10226. }
  10227. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  10228. #endif /* HAVE_CURVE25519 */
  10229. #ifdef HAVE_ED25519
  10230. void bench_ed25519KeyGen(void)
  10231. {
  10232. #ifdef HAVE_ED25519_MAKE_KEY
  10233. ed25519_key genKey;
  10234. double start;
  10235. int i, count;
  10236. const char**desc = bench_desc_words[lng_index];
  10237. DECLARE_MULTI_VALUE_STATS_VARS()
  10238. /* Key Gen */
  10239. bench_stats_start(&count, &start);
  10240. do {
  10241. for (i = 0; i < genTimes; i++) {
  10242. wc_ed25519_init(&genKey);
  10243. (void)wc_ed25519_make_key(&gRng, 32, &genKey);
  10244. wc_ed25519_free(&genKey);
  10245. RECORD_MULTI_VALUE_STATS();
  10246. }
  10247. count += i;
  10248. } while (bench_stats_check(start)
  10249. #ifdef MULTI_VALUE_STATISTICS
  10250. || runs < minimum_runs
  10251. #endif
  10252. );
  10253. bench_stats_asym_finish("ED", 25519, desc[2], 0, count, start, 0);
  10254. #ifdef MULTI_VALUE_STATISTICS
  10255. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10256. #endif
  10257. #endif /* HAVE_ED25519_MAKE_KEY */
  10258. }
  10259. void bench_ed25519KeySign(void)
  10260. {
  10261. #ifdef HAVE_ED25519_MAKE_KEY
  10262. int ret;
  10263. #endif
  10264. ed25519_key genKey;
  10265. #ifdef HAVE_ED25519_SIGN
  10266. double start;
  10267. int i, count;
  10268. byte sig[ED25519_SIG_SIZE];
  10269. byte msg[512];
  10270. word32 x = 0;
  10271. const char**desc = bench_desc_words[lng_index];
  10272. DECLARE_MULTI_VALUE_STATS_VARS()
  10273. #endif
  10274. wc_ed25519_init(&genKey);
  10275. #ifdef HAVE_ED25519_MAKE_KEY
  10276. ret = wc_ed25519_make_key(&gRng, ED25519_KEY_SIZE, &genKey);
  10277. if (ret != 0) {
  10278. printf("ed25519_make_key failed\n");
  10279. return;
  10280. }
  10281. #endif
  10282. #ifdef HAVE_ED25519_SIGN
  10283. /* make dummy msg */
  10284. for (i = 0; i < (int)sizeof(msg); i++)
  10285. msg[i] = (byte)i;
  10286. bench_stats_start(&count, &start);
  10287. do {
  10288. for (i = 0; i < agreeTimes; i++) {
  10289. x = sizeof(sig);
  10290. ret = wc_ed25519_sign_msg(msg, sizeof(msg), sig, &x, &genKey);
  10291. if (ret != 0) {
  10292. printf("ed25519_sign_msg failed\n");
  10293. goto exit_ed_sign;
  10294. }
  10295. RECORD_MULTI_VALUE_STATS();
  10296. }
  10297. count += i;
  10298. } while (bench_stats_check(start)
  10299. #ifdef MULTI_VALUE_STATISTICS
  10300. || runs < minimum_runs
  10301. #endif
  10302. );
  10303. exit_ed_sign:
  10304. bench_stats_asym_finish("ED", 25519, desc[4], 0, count, start, ret);
  10305. #ifdef MULTI_VALUE_STATISTICS
  10306. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10307. #endif
  10308. RESET_MULTI_VALUE_STATS_VARS();
  10309. #ifdef HAVE_ED25519_VERIFY
  10310. bench_stats_start(&count, &start);
  10311. do {
  10312. for (i = 0; i < agreeTimes; i++) {
  10313. int verify = 0;
  10314. ret = wc_ed25519_verify_msg(sig, x, msg, sizeof(msg), &verify,
  10315. &genKey);
  10316. if (ret != 0 || verify != 1) {
  10317. printf("ed25519_verify_msg failed\n");
  10318. goto exit_ed_verify;
  10319. }
  10320. RECORD_MULTI_VALUE_STATS();
  10321. }
  10322. count += i;
  10323. } while (bench_stats_check(start)
  10324. #ifdef MULTI_VALUE_STATISTICS
  10325. || runs < minimum_runs
  10326. #endif
  10327. );
  10328. exit_ed_verify:
  10329. bench_stats_asym_finish("ED", 25519, desc[5], 0, count, start, ret);
  10330. #ifdef MULTI_VALUE_STATISTICS
  10331. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10332. #endif
  10333. #endif /* HAVE_ED25519_VERIFY */
  10334. #endif /* HAVE_ED25519_SIGN */
  10335. wc_ed25519_free(&genKey);
  10336. }
  10337. #endif /* HAVE_ED25519 */
  10338. #ifdef HAVE_CURVE448
  10339. void bench_curve448KeyGen(void)
  10340. {
  10341. curve448_key genKey;
  10342. double start;
  10343. int ret = 0, i, count;
  10344. const char**desc = bench_desc_words[lng_index];
  10345. DECLARE_MULTI_VALUE_STATS_VARS()
  10346. /* Key Gen */
  10347. bench_stats_start(&count, &start);
  10348. do {
  10349. for (i = 0; i < genTimes; i++) {
  10350. ret = wc_curve448_make_key(&gRng, 56, &genKey);
  10351. wc_curve448_free(&genKey);
  10352. if (ret != 0) {
  10353. printf("wc_curve448_make_key failed: %d\n", ret);
  10354. break;
  10355. }
  10356. RECORD_MULTI_VALUE_STATS();
  10357. }
  10358. count += i;
  10359. } while (bench_stats_check(start)
  10360. #ifdef MULTI_VALUE_STATISTICS
  10361. || runs < minimum_runs
  10362. #endif
  10363. );
  10364. bench_stats_asym_finish("CURVE", 448, desc[2], 0, count, start, ret);
  10365. #ifdef MULTI_VALUE_STATISTICS
  10366. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10367. #endif
  10368. }
  10369. #ifdef HAVE_CURVE448_SHARED_SECRET
  10370. void bench_curve448KeyAgree(void)
  10371. {
  10372. curve448_key genKey, genKey2;
  10373. double start;
  10374. int ret, i, count;
  10375. byte shared[56];
  10376. const char**desc = bench_desc_words[lng_index];
  10377. word32 x = 0;
  10378. DECLARE_MULTI_VALUE_STATS_VARS()
  10379. wc_curve448_init(&genKey);
  10380. wc_curve448_init(&genKey2);
  10381. ret = wc_curve448_make_key(&gRng, 56, &genKey);
  10382. if (ret != 0) {
  10383. printf("curve448_make_key failed\n");
  10384. return;
  10385. }
  10386. ret = wc_curve448_make_key(&gRng, 56, &genKey2);
  10387. if (ret != 0) {
  10388. printf("curve448_make_key failed: %d\n", ret);
  10389. wc_curve448_free(&genKey);
  10390. return;
  10391. }
  10392. /* Shared secret */
  10393. bench_stats_start(&count, &start);
  10394. do {
  10395. for (i = 0; i < agreeTimes; i++) {
  10396. x = sizeof(shared);
  10397. ret = wc_curve448_shared_secret(&genKey, &genKey2, shared, &x);
  10398. if (ret != 0) {
  10399. printf("curve448_shared_secret failed: %d\n", ret);
  10400. goto exit;
  10401. }
  10402. RECORD_MULTI_VALUE_STATS();
  10403. }
  10404. count += i;
  10405. } while (bench_stats_check(start)
  10406. #ifdef MULTI_VALUE_STATISTICS
  10407. || runs < minimum_runs
  10408. #endif
  10409. );
  10410. exit:
  10411. bench_stats_asym_finish("CURVE", 448, desc[3], 0, count, start, ret);
  10412. #ifdef MULTI_VALUE_STATISTICS
  10413. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10414. #endif
  10415. wc_curve448_free(&genKey2);
  10416. wc_curve448_free(&genKey);
  10417. }
  10418. #endif /* HAVE_CURVE448_SHARED_SECRET */
  10419. #endif /* HAVE_CURVE448 */
  10420. #ifdef HAVE_ED448
  10421. void bench_ed448KeyGen(void)
  10422. {
  10423. ed448_key genKey;
  10424. double start;
  10425. int i, count;
  10426. const char**desc = bench_desc_words[lng_index];
  10427. DECLARE_MULTI_VALUE_STATS_VARS()
  10428. /* Key Gen */
  10429. bench_stats_start(&count, &start);
  10430. do {
  10431. for (i = 0; i < genTimes; i++) {
  10432. wc_ed448_init(&genKey);
  10433. (void)wc_ed448_make_key(&gRng, ED448_KEY_SIZE, &genKey);
  10434. wc_ed448_free(&genKey);
  10435. RECORD_MULTI_VALUE_STATS();
  10436. }
  10437. count += i;
  10438. } while (bench_stats_check(start)
  10439. #ifdef MULTI_VALUE_STATISTICS
  10440. || runs < minimum_runs
  10441. #endif
  10442. );
  10443. bench_stats_asym_finish("ED", 448, desc[2], 0, count, start, 0);
  10444. #ifdef MULTI_VALUE_STATISTICS
  10445. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10446. #endif
  10447. }
  10448. void bench_ed448KeySign(void)
  10449. {
  10450. int ret;
  10451. WC_DECLARE_VAR(genKey, ed448_key, 1, HEAP_HINT);
  10452. #ifdef HAVE_ED448_SIGN
  10453. double start;
  10454. int i, count;
  10455. byte sig[ED448_SIG_SIZE];
  10456. byte msg[512];
  10457. word32 x = 0;
  10458. const char**desc = bench_desc_words[lng_index];
  10459. DECLARE_MULTI_VALUE_STATS_VARS()
  10460. #endif
  10461. WC_ALLOC_VAR(genKey, ed448_key, 1, HEAP_HINT);
  10462. wc_ed448_init(genKey);
  10463. ret = wc_ed448_make_key(&gRng, ED448_KEY_SIZE, genKey);
  10464. if (ret != 0) {
  10465. printf("ed448_make_key failed\n");
  10466. goto exit;
  10467. }
  10468. #ifdef HAVE_ED448_SIGN
  10469. /* make dummy msg */
  10470. for (i = 0; i < (int)sizeof(msg); i++)
  10471. msg[i] = (byte)i;
  10472. bench_stats_start(&count, &start);
  10473. do {
  10474. for (i = 0; i < agreeTimes; i++) {
  10475. x = sizeof(sig);
  10476. ret = wc_ed448_sign_msg(msg, sizeof(msg), sig, &x, genKey,
  10477. NULL, 0);
  10478. if (ret != 0) {
  10479. printf("ed448_sign_msg failed\n");
  10480. goto exit;
  10481. }
  10482. RECORD_MULTI_VALUE_STATS();
  10483. }
  10484. count += i;
  10485. } while (bench_stats_check(start)
  10486. #ifdef MULTI_VALUE_STATISTICS
  10487. || runs < minimum_runs
  10488. #endif
  10489. );
  10490. bench_stats_asym_finish("ED", 448, desc[4], 0, count, start, ret);
  10491. #ifdef MULTI_VALUE_STATISTICS
  10492. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10493. #endif
  10494. RESET_MULTI_VALUE_STATS_VARS();
  10495. #ifdef HAVE_ED448_VERIFY
  10496. bench_stats_start(&count, &start);
  10497. do {
  10498. for (i = 0; i < agreeTimes; i++) {
  10499. int verify = 0;
  10500. ret = wc_ed448_verify_msg(sig, x, msg, sizeof(msg), &verify,
  10501. genKey, NULL, 0);
  10502. if (ret != 0 || verify != 1) {
  10503. printf("ed448_verify_msg failed\n");
  10504. goto exit;
  10505. }
  10506. RECORD_MULTI_VALUE_STATS();
  10507. }
  10508. count += i;
  10509. } while (bench_stats_check(start)
  10510. #ifdef MULTI_VALUE_STATISTICS
  10511. || runs < minimum_runs
  10512. #endif
  10513. );
  10514. bench_stats_asym_finish("ED", 448, desc[5], 0, count, start, ret);
  10515. #ifdef MULTI_VALUE_STATISTICS
  10516. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10517. #endif
  10518. #endif /* HAVE_ED448_VERIFY */
  10519. #endif /* HAVE_ED448_SIGN */
  10520. exit:
  10521. wc_ed448_free(genKey);
  10522. WC_FREE_VAR(genKey, HEAP_HINT);
  10523. }
  10524. #endif /* HAVE_ED448 */
  10525. #ifdef WOLFCRYPT_HAVE_ECCSI
  10526. #ifdef WOLFCRYPT_ECCSI_KMS
  10527. void bench_eccsiKeyGen(void)
  10528. {
  10529. WC_DECLARE_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10530. double start;
  10531. int i, count;
  10532. const char**desc = bench_desc_words[lng_index];
  10533. int ret;
  10534. DECLARE_MULTI_VALUE_STATS_VARS()
  10535. WC_ALLOC_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10536. /* Key Gen */
  10537. bench_stats_start(&count, &start);
  10538. do {
  10539. for (i = 0; i < genTimes; i++) {
  10540. wc_InitEccsiKey(genKey, NULL, INVALID_DEVID);
  10541. ret = wc_MakeEccsiKey(genKey, &gRng);
  10542. wc_FreeEccsiKey(genKey);
  10543. if (ret != 0) {
  10544. printf("wc_MakeEccsiKey failed: %d\n", ret);
  10545. goto exit;
  10546. }
  10547. RECORD_MULTI_VALUE_STATS();
  10548. }
  10549. count += i;
  10550. } while (bench_stats_check(start)
  10551. #ifdef MULTI_VALUE_STATISTICS
  10552. || runs < minimum_runs
  10553. #endif
  10554. );
  10555. bench_stats_asym_finish("ECCSI", 256, desc[2], 0, count, start, 0);
  10556. #ifdef MULTI_VALUE_STATISTICS
  10557. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10558. #endif
  10559. exit:
  10560. WC_FREE_VAR(genKey, HEAP_HINT);
  10561. }
  10562. void bench_eccsiPairGen(void)
  10563. {
  10564. WC_DECLARE_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10565. double start;
  10566. int i, count;
  10567. const char**desc = bench_desc_words[lng_index];
  10568. WC_DECLARE_VAR(ssk, mp_int, 1, HEAP_HINT);
  10569. ecc_point* pvt;
  10570. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10571. int ret;
  10572. DECLARE_MULTI_VALUE_STATS_VARS()
  10573. WC_ALLOC_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10574. WC_ALLOC_VAR(ssk, mp_int, 1, HEAP_HINT);
  10575. (void)mp_init(ssk);
  10576. pvt = wc_ecc_new_point();
  10577. wc_InitEccsiKey(genKey, NULL, INVALID_DEVID);
  10578. (void)wc_MakeEccsiKey(genKey, &gRng);
  10579. /* RSK Gen */
  10580. bench_stats_start(&count, &start);
  10581. do {
  10582. for (i = 0; i < genTimes; i++) {
  10583. ret = wc_MakeEccsiPair(genKey, &gRng, WC_HASH_TYPE_SHA256, id,
  10584. sizeof(id), ssk, pvt);
  10585. if (ret != 0) {
  10586. printf("wc_MakeEccsiPair failed: %d\n", ret);
  10587. goto exit;
  10588. }
  10589. RECORD_MULTI_VALUE_STATS();
  10590. }
  10591. count += i;
  10592. } while (bench_stats_check(start)
  10593. #ifdef MULTI_VALUE_STATISTICS
  10594. || runs < minimum_runs
  10595. #endif
  10596. );
  10597. bench_stats_asym_finish("ECCSI", 256, desc[12], 0, count, start, 0);
  10598. #ifdef MULTI_VALUE_STATISTICS
  10599. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10600. #endif
  10601. wc_FreeEccsiKey(genKey);
  10602. wc_ecc_del_point(pvt);
  10603. mp_free(ssk);
  10604. exit:
  10605. WC_FREE_VAR(genKey, HEAP_HINT);
  10606. WC_FREE_VAR(ssk, HEAP_HINT);
  10607. }
  10608. #endif
  10609. #ifdef WOLFCRYPT_ECCSI_CLIENT
  10610. void bench_eccsiValidate(void)
  10611. {
  10612. WC_DECLARE_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10613. double start;
  10614. int i, count;
  10615. const char**desc = bench_desc_words[lng_index];
  10616. WC_DECLARE_VAR(ssk, mp_int, 1, HEAP_HINT);
  10617. ecc_point* pvt;
  10618. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10619. int valid;
  10620. int ret;
  10621. DECLARE_MULTI_VALUE_STATS_VARS()
  10622. WC_ALLOC_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10623. WC_ALLOC_VAR(ssk, mp_int, 1, HEAP_HINT);
  10624. (void)mp_init(ssk);
  10625. pvt = wc_ecc_new_point();
  10626. wc_InitEccsiKey(genKey, NULL, INVALID_DEVID);
  10627. (void)wc_MakeEccsiKey(genKey, &gRng);
  10628. (void)wc_MakeEccsiPair(genKey, &gRng, WC_HASH_TYPE_SHA256, id, sizeof(id),
  10629. ssk, pvt);
  10630. /* Validation of RSK */
  10631. bench_stats_start(&count, &start);
  10632. do {
  10633. for (i = 0; i < genTimes; i++) {
  10634. ret = wc_ValidateEccsiPair(genKey, WC_HASH_TYPE_SHA256, id,
  10635. sizeof(id), ssk, pvt, &valid);
  10636. if (ret != 0 || !valid) {
  10637. printf("wc_ValidateEccsiPair failed: %d (valid=%d))\n", ret,
  10638. valid);
  10639. goto exit;
  10640. }
  10641. RECORD_MULTI_VALUE_STATS();
  10642. }
  10643. count += i;
  10644. } while (bench_stats_check(start)
  10645. #ifdef MULTI_VALUE_STATISTICS
  10646. || runs < minimum_runs
  10647. #endif
  10648. );
  10649. bench_stats_asym_finish("ECCSI", 256, desc[11], 0, count, start, 0);
  10650. #ifdef MULTI_VALUE_STATISTICS
  10651. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10652. #endif
  10653. wc_FreeEccsiKey(genKey);
  10654. wc_ecc_del_point(pvt);
  10655. mp_free(ssk);
  10656. exit:
  10657. WC_FREE_VAR(genKey, HEAP_HINT);
  10658. WC_FREE_VAR(ssk, HEAP_HINT);
  10659. }
  10660. void bench_eccsi(void)
  10661. {
  10662. WC_DECLARE_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10663. double start;
  10664. int i, count;
  10665. const char**desc = bench_desc_words[lng_index];
  10666. WC_DECLARE_VAR(ssk, mp_int, 1, HEAP_HINT);
  10667. ecc_point* pvt;
  10668. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10669. static const byte msg[] = { 0x01, 0x23, 0x34, 0x45 };
  10670. byte hash[WC_SHA256_DIGEST_SIZE];
  10671. byte hashSz = (byte)sizeof(hash);
  10672. byte sig[257];
  10673. word32 sigSz = sizeof(sig);
  10674. int ret;
  10675. int verified;
  10676. DECLARE_MULTI_VALUE_STATS_VARS()
  10677. WC_ALLOC_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10678. WC_ALLOC_VAR(ssk, mp_int, 1, HEAP_HINT);
  10679. (void)mp_init(ssk);
  10680. pvt = wc_ecc_new_point();
  10681. (void)wc_InitEccsiKey(genKey, NULL, INVALID_DEVID);
  10682. (void)wc_MakeEccsiKey(genKey, &gRng);
  10683. (void)wc_MakeEccsiPair(genKey, &gRng, WC_HASH_TYPE_SHA256, id, sizeof(id),
  10684. ssk, pvt);
  10685. (void)wc_HashEccsiId(genKey, WC_HASH_TYPE_SHA256, id, sizeof(id), pvt,
  10686. hash, &hashSz);
  10687. (void)wc_SetEccsiHash(genKey, hash, hashSz);
  10688. (void)wc_SetEccsiPair(genKey, ssk, pvt);
  10689. /* Encapsulate */
  10690. bench_stats_start(&count, &start);
  10691. do {
  10692. for (i = 0; i < genTimes; i++) {
  10693. ret = wc_SignEccsiHash(genKey, &gRng, WC_HASH_TYPE_SHA256, msg,
  10694. sizeof(msg), sig, &sigSz);
  10695. if (ret != 0) {
  10696. printf("wc_SignEccsiHash failed: %d\n", ret);
  10697. break;
  10698. }
  10699. RECORD_MULTI_VALUE_STATS();
  10700. }
  10701. count += i;
  10702. } while (bench_stats_check(start)
  10703. #ifdef MULTI_VALUE_STATISTICS
  10704. || runs < minimum_runs
  10705. #endif
  10706. );
  10707. bench_stats_asym_finish("ECCSI", 256, desc[4], 0, count, start, 0);
  10708. #ifdef MULTI_VALUE_STATISTICS
  10709. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10710. #endif
  10711. RESET_MULTI_VALUE_STATS_VARS();
  10712. /* Derive */
  10713. bench_stats_start(&count, &start);
  10714. do {
  10715. for (i = 0; i < genTimes; i++) {
  10716. ret = wc_VerifyEccsiHash(genKey, WC_HASH_TYPE_SHA256, msg,
  10717. sizeof(msg), sig, sigSz, &verified);
  10718. if (ret != 0 || !verified) {
  10719. printf("wc_VerifyEccsiHash failed: %d (verified: %d)\n", ret,
  10720. verified);
  10721. goto exit;
  10722. }
  10723. RECORD_MULTI_VALUE_STATS();
  10724. }
  10725. count += i;
  10726. } while (bench_stats_check(start)
  10727. #ifdef MULTI_VALUE_STATISTICS
  10728. || runs < minimum_runs
  10729. #endif
  10730. );
  10731. bench_stats_asym_finish("ECCSI", 256, desc[5], 0, count, start, 0);
  10732. #ifdef MULTI_VALUE_STATISTICS
  10733. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10734. #endif
  10735. wc_FreeEccsiKey(genKey);
  10736. wc_ecc_del_point(pvt);
  10737. exit:
  10738. WC_FREE_VAR(genKey, HEAP_HINT);
  10739. WC_FREE_VAR(ssk, HEAP_HINT);
  10740. }
  10741. #endif /* WOLFCRYPT_ECCSI_CLIENT */
  10742. #endif /* WOLFCRYPT_HAVE_ECCSI */
  10743. #ifdef WOLFCRYPT_HAVE_SAKKE
  10744. #ifdef WOLFCRYPT_SAKKE_KMS
  10745. void bench_sakkeKeyGen(void)
  10746. {
  10747. WC_DECLARE_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10748. double start;
  10749. int i, count;
  10750. const char**desc = bench_desc_words[lng_index];
  10751. int ret;
  10752. DECLARE_MULTI_VALUE_STATS_VARS()
  10753. WC_ALLOC_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10754. /* Key Gen */
  10755. bench_stats_start(&count, &start);
  10756. do {
  10757. for (i = 0; i < genTimes; i++) {
  10758. wc_InitSakkeKey_ex(genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  10759. ret = wc_MakeSakkeKey(genKey, &gRng);
  10760. if (ret != 0) {
  10761. printf("wc_MakeSakkeKey failed: %d\n", ret);
  10762. goto exit;
  10763. }
  10764. wc_FreeSakkeKey(genKey);
  10765. RECORD_MULTI_VALUE_STATS();
  10766. }
  10767. count += i;
  10768. } while (bench_stats_check(start)
  10769. #ifdef MULTI_VALUE_STATISTICS
  10770. || runs < minimum_runs
  10771. #endif
  10772. );
  10773. bench_stats_asym_finish("SAKKE", 1024, desc[2], 0, count, start, 0);
  10774. #ifdef MULTI_VALUE_STATISTICS
  10775. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10776. #endif
  10777. exit:
  10778. WC_FREE_VAR(genKey, HEAP_HINT);
  10779. }
  10780. void bench_sakkeRskGen(void)
  10781. {
  10782. WC_DECLARE_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10783. double start;
  10784. int i, count;
  10785. const char**desc = bench_desc_words[lng_index];
  10786. ecc_point* rsk;
  10787. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10788. int ret;
  10789. DECLARE_MULTI_VALUE_STATS_VARS()
  10790. WC_ALLOC_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10791. rsk = wc_ecc_new_point();
  10792. wc_InitSakkeKey_ex(genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  10793. (void)wc_MakeSakkeKey(genKey, &gRng);
  10794. /* RSK Gen */
  10795. bench_stats_start(&count, &start);
  10796. do {
  10797. for (i = 0; i < genTimes; i++) {
  10798. ret = wc_MakeSakkeRsk(genKey, id, sizeof(id), rsk);
  10799. if (ret != 0) {
  10800. printf("wc_MakeSakkeRsk failed: %d\n", ret);
  10801. goto exit;
  10802. }
  10803. RECORD_MULTI_VALUE_STATS();
  10804. }
  10805. count += i;
  10806. } while (bench_stats_check(start)
  10807. #ifdef MULTI_VALUE_STATISTICS
  10808. || runs < minimum_runs
  10809. #endif
  10810. );
  10811. bench_stats_asym_finish("SAKKE", 1024, desc[8], 0, count, start, 0);
  10812. #ifdef MULTI_VALUE_STATISTICS
  10813. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10814. #endif
  10815. wc_FreeSakkeKey(genKey);
  10816. wc_ecc_del_point(rsk);
  10817. exit:
  10818. WC_FREE_VAR(genKey, HEAP_HINT);
  10819. }
  10820. #endif
  10821. #ifdef WOLFCRYPT_SAKKE_CLIENT
  10822. void bench_sakkeValidate(void)
  10823. {
  10824. WC_DECLARE_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10825. double start;
  10826. int i, count;
  10827. const char**desc = bench_desc_words[lng_index];
  10828. ecc_point* rsk;
  10829. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10830. int valid;
  10831. int ret;
  10832. DECLARE_MULTI_VALUE_STATS_VARS()
  10833. WC_ALLOC_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10834. rsk = wc_ecc_new_point();
  10835. (void)wc_InitSakkeKey_ex(genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  10836. (void)wc_MakeSakkeKey(genKey, &gRng);
  10837. (void)wc_MakeSakkeRsk(genKey, id, sizeof(id), rsk);
  10838. (void)wc_ValidateSakkeRsk(genKey, id, sizeof(id), rsk, &valid);
  10839. /* Validation of RSK */
  10840. bench_stats_start(&count, &start);
  10841. do {
  10842. for (i = 0; i < genTimes; i++) {
  10843. ret = wc_ValidateSakkeRsk(genKey, id, sizeof(id), rsk, &valid);
  10844. if (ret != 0 || !valid) {
  10845. printf("wc_ValidateSakkeRsk failed: %d (valid=%d))\n", ret,
  10846. valid);
  10847. goto exit;
  10848. }
  10849. RECORD_MULTI_VALUE_STATS();
  10850. }
  10851. count += i;
  10852. } while (bench_stats_check(start)
  10853. #ifdef MULTI_VALUE_STATISTICS
  10854. || runs < minimum_runs
  10855. #endif
  10856. );
  10857. bench_stats_asym_finish("SAKKE", 1024, desc[11], 0, count, start, 0);
  10858. #ifdef MULTI_VALUE_STATISTICS
  10859. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10860. #endif
  10861. wc_FreeSakkeKey(genKey);
  10862. wc_ecc_del_point(rsk);
  10863. exit:
  10864. WC_FREE_VAR(genKey, HEAP_HINT);
  10865. }
  10866. void bench_sakke(void)
  10867. {
  10868. WC_DECLARE_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10869. double start;
  10870. int i, count;
  10871. const char**desc = bench_desc_words[lng_index];
  10872. ecc_point* rsk;
  10873. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10874. static const byte ssv_init[] = { 0x01, 0x23, 0x34, 0x45 };
  10875. byte ssv[sizeof(ssv_init)];
  10876. byte derSSV[sizeof(ssv)];
  10877. byte auth[257];
  10878. word16 authSz = sizeof(auth);
  10879. int ret = 0;
  10880. byte* table = NULL;
  10881. word32 len = 0;
  10882. byte* iTable = NULL;
  10883. word32 iTableLen = 0;
  10884. DECLARE_MULTI_VALUE_STATS_VARS()
  10885. WC_ALLOC_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10886. XMEMCPY(ssv, ssv_init, sizeof ssv);
  10887. rsk = wc_ecc_new_point();
  10888. (void)wc_InitSakkeKey_ex(genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  10889. (void)wc_MakeSakkeKey(genKey, &gRng);
  10890. (void)wc_MakeSakkeRsk(genKey, id, sizeof(id), rsk);
  10891. (void)wc_SetSakkeRsk(genKey, rsk, NULL, 0);
  10892. (void)wc_SetSakkeIdentity(genKey, id, sizeof(id));
  10893. /* Encapsulate */
  10894. bench_stats_start(&count, &start);
  10895. do {
  10896. for (i = 0; i < genTimes; i++) {
  10897. ret = wc_MakeSakkeEncapsulatedSSV(genKey,
  10898. WC_HASH_TYPE_SHA256,
  10899. ssv, sizeof(ssv), auth, &authSz);
  10900. if (ret != 0) {
  10901. printf("wc_MakeSakkeEncapsulatedSSV failed: %d\n", ret);
  10902. break;
  10903. }
  10904. RECORD_MULTI_VALUE_STATS();
  10905. } /* for */
  10906. count += i;
  10907. } while (bench_stats_check(start)
  10908. #ifdef MULTI_VALUE_STATISTICS
  10909. || runs < minimum_runs
  10910. #endif
  10911. );
  10912. bench_stats_asym_finish_ex("SAKKE", 1024, desc[9], "-1",
  10913. 0, count, start, 0);
  10914. #ifdef MULTI_VALUE_STATISTICS
  10915. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10916. #endif
  10917. RESET_MULTI_VALUE_STATS_VARS();
  10918. /* Derive */
  10919. bench_stats_start(&count, &start);
  10920. do {
  10921. for (i = 0; i < genTimes; i++) {
  10922. XMEMCPY(derSSV, ssv, sizeof(ssv));
  10923. ret = wc_DeriveSakkeSSV(genKey, WC_HASH_TYPE_SHA256, derSSV,
  10924. sizeof(derSSV), auth, authSz);
  10925. if (ret != 0) {
  10926. printf("wc_DeriveSakkeSSV failed: %d\n", ret);
  10927. break;
  10928. }
  10929. RECORD_MULTI_VALUE_STATS();
  10930. }
  10931. if (ret != 0) break;
  10932. count += i;
  10933. } while (bench_stats_check(start)
  10934. #ifdef MULTI_VALUE_STATISTICS
  10935. || runs < minimum_runs
  10936. #endif
  10937. );
  10938. bench_stats_asym_finish_ex("SAKKE", 1024, desc[10], "-1",
  10939. 0, count, start, 0);
  10940. #ifdef MULTI_VALUE_STATISTICS
  10941. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10942. #endif
  10943. /* Calculate Point I and generate table. */
  10944. (void)wc_MakeSakkePointI(genKey, id, sizeof(id));
  10945. iTableLen = 0;
  10946. (void)wc_GenerateSakkePointITable(genKey, NULL, &iTableLen);
  10947. if (iTableLen != 0) {
  10948. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10949. if (iTable == NULL)
  10950. WC_ALLOC_DO_ON_FAILURE();
  10951. (void)wc_GenerateSakkePointITable(genKey, iTable, &iTableLen);
  10952. }
  10953. /* Encapsulate with Point I table */
  10954. bench_stats_start(&count, &start);
  10955. do {
  10956. for (i = 0; i < genTimes; i++) {
  10957. ret = wc_MakeSakkeEncapsulatedSSV(genKey,
  10958. WC_HASH_TYPE_SHA256, ssv,
  10959. sizeof(ssv), auth, &authSz);
  10960. if (ret != 0) {
  10961. printf("wc_MakeSakkeEncapsulatedSSV failed: %d\n", ret);
  10962. break;
  10963. }
  10964. RECORD_MULTI_VALUE_STATS();
  10965. }
  10966. count += i;
  10967. } while (bench_stats_check(start)
  10968. #ifdef MULTI_VALUE_STATISTICS
  10969. || runs < minimum_runs
  10970. #endif
  10971. );
  10972. bench_stats_asym_finish_ex("SAKKE", 1024, desc[9], "-2", 0,
  10973. count, start, 0);
  10974. #ifdef MULTI_VALUE_STATISTICS
  10975. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10976. #endif
  10977. RESET_MULTI_VALUE_STATS_VARS();
  10978. (void)wc_SetSakkeRsk(genKey, rsk, table, len);
  10979. /* Derive with Point I table */
  10980. bench_stats_start(&count, &start);
  10981. do {
  10982. for (i = 0; i < genTimes; i++) {
  10983. XMEMCPY(derSSV, ssv, sizeof(ssv));
  10984. ret = wc_DeriveSakkeSSV(genKey, WC_HASH_TYPE_SHA256, derSSV,
  10985. sizeof(derSSV), auth, authSz);
  10986. if (ret != 0) {
  10987. printf("wc_DeriveSakkeSSV failed: %d\n", ret);
  10988. break;
  10989. }
  10990. RECORD_MULTI_VALUE_STATS();
  10991. }
  10992. if (ret != 0) break;
  10993. count += i;
  10994. } while (bench_stats_check(start)
  10995. #ifdef MULTI_VALUE_STATISTICS
  10996. || runs < minimum_runs
  10997. #endif
  10998. );
  10999. bench_stats_asym_finish_ex("SAKKE", 1024, desc[10], "-2", 0,
  11000. count, start, 0);
  11001. #ifdef MULTI_VALUE_STATISTICS
  11002. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11003. #endif
  11004. RESET_MULTI_VALUE_STATS_VARS();
  11005. len = 0;
  11006. (void)wc_GenerateSakkeRskTable(genKey, rsk, NULL, &len);
  11007. if (len > 0) {
  11008. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11009. if (table == NULL)
  11010. WC_ALLOC_DO_ON_FAILURE();
  11011. (void)wc_GenerateSakkeRskTable(genKey, rsk, table, &len);
  11012. }
  11013. (void)wc_SetSakkeRsk(genKey, rsk, table, len);
  11014. /* Derive with Point I table and RSK table */
  11015. bench_stats_start(&count, &start);
  11016. do {
  11017. for (i = 0; i < genTimes; i++) {
  11018. XMEMCPY(derSSV, ssv, sizeof(ssv));
  11019. ret = wc_DeriveSakkeSSV(genKey, WC_HASH_TYPE_SHA256, derSSV,
  11020. sizeof(derSSV), auth, authSz);
  11021. if (ret != 0) {
  11022. printf("wc_DeriveSakkeSSV failed: %d\n", ret);
  11023. break;
  11024. }
  11025. RECORD_MULTI_VALUE_STATS();
  11026. }
  11027. if (ret != 0) break;
  11028. count += i;
  11029. } while (bench_stats_check(start)
  11030. #ifdef MULTI_VALUE_STATISTICS
  11031. || runs < minimum_runs
  11032. #endif
  11033. );
  11034. bench_stats_asym_finish_ex("SAKKE", 1024, desc[10], "-3",
  11035. 0, count, start, 0);
  11036. #ifdef MULTI_VALUE_STATISTICS
  11037. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11038. #endif
  11039. RESET_MULTI_VALUE_STATS_VARS();
  11040. wc_ClearSakkePointITable(genKey);
  11041. /* Derive with RSK table */
  11042. bench_stats_start(&count, &start);
  11043. do {
  11044. for (i = 0; i < genTimes; i++) {
  11045. XMEMCPY(derSSV, ssv, sizeof(ssv));
  11046. ret = wc_DeriveSakkeSSV(genKey, WC_HASH_TYPE_SHA256, derSSV,
  11047. sizeof(derSSV), auth, authSz);
  11048. if (ret != 0) {
  11049. printf("wc_DeriveSakkeSSV failed: %d\n", ret);
  11050. break;
  11051. }
  11052. RECORD_MULTI_VALUE_STATS();
  11053. }
  11054. if (ret != 0) break;
  11055. count += i;
  11056. } while (bench_stats_check(start)
  11057. #ifdef MULTI_VALUE_STATISTICS
  11058. || runs < minimum_runs
  11059. #endif
  11060. );
  11061. bench_stats_asym_finish_ex("SAKKE", 1024, desc[10], "-4", 0,
  11062. count, start, 0);
  11063. #ifdef MULTI_VALUE_STATISTICS
  11064. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11065. #endif
  11066. wc_FreeSakkeKey(genKey);
  11067. wc_ecc_del_point(rsk);
  11068. exit:
  11069. if (iTable)
  11070. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11071. if (table)
  11072. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11073. WC_FREE_VAR(genKey, HEAP_HINT);
  11074. }
  11075. #endif /* WOLFCRYPT_SAKKE_CLIENT */
  11076. #endif /* WOLFCRYPT_HAVE_SAKKE */
  11077. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  11078. #ifdef HAVE_FALCON
  11079. void bench_falconKeySign(byte level)
  11080. {
  11081. int ret = 0;
  11082. falcon_key key;
  11083. double start;
  11084. int i, count;
  11085. byte sig[FALCON_MAX_SIG_SIZE];
  11086. byte msg[512];
  11087. word32 x = 0;
  11088. const char**desc = bench_desc_words[lng_index];
  11089. DECLARE_MULTI_VALUE_STATS_VARS()
  11090. ret = wc_falcon_init(&key);
  11091. if (ret != 0) {
  11092. printf("wc_falcon_init failed %d\n", ret);
  11093. return;
  11094. }
  11095. ret = wc_falcon_set_level(&key, level);
  11096. if (ret != 0) {
  11097. printf("wc_falcon_set_level failed %d\n", ret);
  11098. }
  11099. if (ret == 0) {
  11100. if (level == 1) {
  11101. ret = wc_falcon_import_private_key(bench_falcon_level1_key,
  11102. sizeof_bench_falcon_level1_key,
  11103. NULL, 0, &key);
  11104. }
  11105. else {
  11106. ret = wc_falcon_import_private_key(bench_falcon_level5_key,
  11107. sizeof_bench_falcon_level5_key,
  11108. NULL, 0, &key);
  11109. }
  11110. if (ret != 0) {
  11111. printf("wc_falcon_import_private_key failed %d\n", ret);
  11112. }
  11113. }
  11114. /* make dummy msg */
  11115. for (i = 0; i < (int)sizeof(msg); i++) {
  11116. msg[i] = (byte)i;
  11117. }
  11118. bench_stats_start(&count, &start);
  11119. do {
  11120. for (i = 0; i < agreeTimes; i++) {
  11121. if (ret == 0) {
  11122. if (level == 1) {
  11123. x = FALCON_LEVEL1_SIG_SIZE;
  11124. }
  11125. else {
  11126. x = FALCON_LEVEL5_SIG_SIZE;
  11127. }
  11128. ret = wc_falcon_sign_msg(msg, sizeof(msg), sig, &x, &key, GLOBAL_RNG);
  11129. if (ret != 0) {
  11130. printf("wc_falcon_sign_msg failed\n");
  11131. }
  11132. }
  11133. RECORD_MULTI_VALUE_STATS();
  11134. }
  11135. count += i;
  11136. } while (bench_stats_check(start)
  11137. #ifdef MULTI_VALUE_STATISTICS
  11138. || runs < minimum_runs
  11139. #endif
  11140. );
  11141. if (ret == 0) {
  11142. bench_stats_asym_finish("FALCON", level, desc[4], 0,
  11143. count, start, ret);
  11144. #ifdef MULTI_VALUE_STATISTICS
  11145. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11146. #endif
  11147. }
  11148. RESET_MULTI_VALUE_STATS_VARS();
  11149. bench_stats_start(&count, &start);
  11150. do {
  11151. for (i = 0; i < agreeTimes; i++) {
  11152. if (ret == 0) {
  11153. int verify = 0;
  11154. ret = wc_falcon_verify_msg(sig, x, msg, sizeof(msg), &verify,
  11155. &key);
  11156. if (ret != 0 || verify != 1) {
  11157. printf("wc_falcon_verify_msg failed %d, verify %d\n",
  11158. ret, verify);
  11159. ret = -1;
  11160. }
  11161. }
  11162. RECORD_MULTI_VALUE_STATS();
  11163. }
  11164. count += i;
  11165. } while (bench_stats_check(start)
  11166. #ifdef MULTI_VALUE_STATISTICS
  11167. || runs < minimum_runs
  11168. #endif
  11169. );
  11170. if (ret == 0) {
  11171. bench_stats_asym_finish("FALCON", level, desc[5],
  11172. 0, count, start, ret);
  11173. #ifdef MULTI_VALUE_STATISTICS
  11174. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11175. #endif
  11176. }
  11177. wc_falcon_free(&key);
  11178. }
  11179. #endif /* HAVE_FALCON */
  11180. #ifdef HAVE_DILITHIUM
  11181. void bench_dilithiumKeySign(byte level)
  11182. {
  11183. int ret = 0;
  11184. dilithium_key key;
  11185. double start;
  11186. int i, count;
  11187. byte sig[DILITHIUM_MAX_SIG_SIZE];
  11188. byte msg[512];
  11189. word32 x = 0;
  11190. const char**desc = bench_desc_words[lng_index];
  11191. DECLARE_MULTI_VALUE_STATS_VARS()
  11192. ret = wc_dilithium_init(&key);
  11193. if (ret != 0) {
  11194. printf("wc_dilithium_init failed %d\n", ret);
  11195. return;
  11196. }
  11197. ret = wc_dilithium_set_level(&key, level);
  11198. if (ret != 0) {
  11199. printf("wc_dilithium_set_level() failed %d\n", ret);
  11200. }
  11201. if (ret == 0) {
  11202. ret = -1;
  11203. if (level == 2) {
  11204. ret = wc_dilithium_import_private_key(bench_dilithium_level2_key,
  11205. sizeof_bench_dilithium_level2_key, NULL, 0, &key);
  11206. }
  11207. else if (level == 3) {
  11208. ret = wc_dilithium_import_private_key(bench_dilithium_level3_key,
  11209. sizeof_bench_dilithium_level3_key, NULL, 0, &key);
  11210. }
  11211. else if (level == 5) {
  11212. ret = wc_dilithium_import_private_key(bench_dilithium_level5_key,
  11213. sizeof_bench_dilithium_level5_key, NULL, 0, &key);
  11214. }
  11215. if (ret != 0) {
  11216. printf("wc_dilithium_import_private_key failed %d\n", ret);
  11217. }
  11218. }
  11219. /* make dummy msg */
  11220. for (i = 0; i < (int)sizeof(msg); i++) {
  11221. msg[i] = (byte)i;
  11222. }
  11223. bench_stats_start(&count, &start);
  11224. do {
  11225. for (i = 0; i < agreeTimes; i++) {
  11226. if (ret == 0) {
  11227. if (level == 2) {
  11228. x = DILITHIUM_LEVEL2_SIG_SIZE;
  11229. }
  11230. else if (level == 3) {
  11231. x = DILITHIUM_LEVEL3_SIG_SIZE;
  11232. }
  11233. else {
  11234. x = DILITHIUM_LEVEL5_SIG_SIZE;
  11235. }
  11236. ret = wc_dilithium_sign_msg(msg, sizeof(msg), sig, &x, &key, GLOBAL_RNG);
  11237. if (ret != 0) {
  11238. printf("wc_dilithium_sign_msg failed\n");
  11239. }
  11240. }
  11241. RECORD_MULTI_VALUE_STATS();
  11242. }
  11243. count += i;
  11244. } while (bench_stats_check(start)
  11245. #ifdef MULTI_VALUE_STATISTICS
  11246. || runs < minimum_runs
  11247. #endif
  11248. );
  11249. if (ret == 0) {
  11250. bench_stats_asym_finish("DILITHIUM", level, desc[4], 0, count, start,
  11251. ret);
  11252. #ifdef MULTI_VALUE_STATISTICS
  11253. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11254. #endif
  11255. }
  11256. RESET_MULTI_VALUE_STATS_VARS();
  11257. bench_stats_start(&count, &start);
  11258. do {
  11259. for (i = 0; i < agreeTimes; i++) {
  11260. if (ret == 0) {
  11261. int verify = 0;
  11262. ret = wc_dilithium_verify_msg(sig, x, msg, sizeof(msg),
  11263. &verify, &key);
  11264. if (ret != 0 || verify != 1) {
  11265. printf("wc_dilithium_verify_msg failed %d, verify %d\n",
  11266. ret, verify);
  11267. ret = -1;
  11268. }
  11269. }
  11270. RECORD_MULTI_VALUE_STATS();
  11271. }
  11272. count += i;
  11273. } while (bench_stats_check(start)
  11274. #ifdef MULTI_VALUE_STATISTICS
  11275. || runs < minimum_runs
  11276. #endif
  11277. );
  11278. if (ret == 0) {
  11279. bench_stats_asym_finish("DILITHIUM", level, desc[5], 0, count, start,
  11280. ret);
  11281. #ifdef MULTI_VALUE_STATISTICS
  11282. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11283. #endif
  11284. }
  11285. wc_dilithium_free(&key);
  11286. }
  11287. #endif /* HAVE_DILITHIUM */
  11288. #ifdef HAVE_SPHINCS
  11289. void bench_sphincsKeySign(byte level, byte optim)
  11290. {
  11291. int ret = 0;
  11292. sphincs_key key;
  11293. double start;
  11294. int i, count;
  11295. byte sig[SPHINCS_MAX_SIG_SIZE];
  11296. byte msg[512];
  11297. word32 x = 0;
  11298. const char**desc = bench_desc_words[lng_index];
  11299. DECLARE_MULTI_VALUE_STATS_VARS()
  11300. ret = wc_sphincs_init(&key);
  11301. if (ret != 0) {
  11302. printf("wc_sphincs_init failed %d\n", ret);
  11303. return;
  11304. }
  11305. ret = wc_sphincs_set_level_and_optim(&key, level, optim);
  11306. if (ret != 0) {
  11307. printf("wc_sphincs_set_level_and_optim() failed %d\n", ret);
  11308. }
  11309. if (ret == 0) {
  11310. ret = -1;
  11311. if ((level == 1) && (optim == FAST_VARIANT)) {
  11312. ret = wc_sphincs_import_private_key(bench_sphincs_fast_level1_key,
  11313. sizeof_bench_sphincs_fast_level1_key, NULL, 0, &key);
  11314. }
  11315. else if ((level == 3) && (optim == FAST_VARIANT)) {
  11316. ret = wc_sphincs_import_private_key(bench_sphincs_fast_level3_key,
  11317. sizeof_bench_sphincs_fast_level3_key, NULL, 0, &key);
  11318. }
  11319. else if ((level == 5) && (optim == FAST_VARIANT)) {
  11320. ret = wc_sphincs_import_private_key(bench_sphincs_fast_level5_key,
  11321. sizeof_bench_sphincs_fast_level5_key, NULL, 0, &key);
  11322. }
  11323. else if ((level == 1) && (optim == SMALL_VARIANT)) {
  11324. ret = wc_sphincs_import_private_key(
  11325. bench_sphincs_small_level1_key,
  11326. sizeof_bench_sphincs_small_level1_key, NULL, 0, &key);
  11327. }
  11328. else if ((level == 3) && (optim == SMALL_VARIANT)) {
  11329. ret = wc_sphincs_import_private_key(
  11330. bench_sphincs_small_level3_key,
  11331. sizeof_bench_sphincs_small_level3_key, NULL, 0, &key);
  11332. }
  11333. else if ((level == 5) && (optim == SMALL_VARIANT)) {
  11334. ret = wc_sphincs_import_private_key(
  11335. bench_sphincs_small_level5_key,
  11336. sizeof_bench_sphincs_small_level5_key, NULL, 0, &key);
  11337. }
  11338. if (ret != 0) {
  11339. printf("wc_sphincs_import_private_key failed %d\n", ret);
  11340. }
  11341. }
  11342. /* make dummy msg */
  11343. for (i = 0; i < (int)sizeof(msg); i++) {
  11344. msg[i] = (byte)i;
  11345. }
  11346. bench_stats_start(&count, &start);
  11347. do {
  11348. for (i = 0; i < agreeTimes; i++) {
  11349. if (ret == 0) {
  11350. if ((level == 1) && (optim == FAST_VARIANT)) {
  11351. x = SPHINCS_FAST_LEVEL1_SIG_SIZE;
  11352. }
  11353. else if ((level == 3) && (optim == FAST_VARIANT)) {
  11354. x = SPHINCS_FAST_LEVEL3_SIG_SIZE;
  11355. }
  11356. else if ((level == 5) && (optim == FAST_VARIANT)) {
  11357. x = SPHINCS_FAST_LEVEL5_SIG_SIZE;
  11358. }
  11359. else if ((level == 1) && (optim == SMALL_VARIANT)) {
  11360. x = SPHINCS_SMALL_LEVEL1_SIG_SIZE;
  11361. }
  11362. else if ((level == 3) && (optim == SMALL_VARIANT)) {
  11363. x = SPHINCS_SMALL_LEVEL3_SIG_SIZE;
  11364. }
  11365. else if ((level == 5) && (optim == SMALL_VARIANT)) {
  11366. x = SPHINCS_SMALL_LEVEL5_SIG_SIZE;
  11367. }
  11368. ret = wc_sphincs_sign_msg(msg, sizeof(msg), sig, &x, &key, GLOBAL_RNG);
  11369. if (ret != 0) {
  11370. printf("wc_sphincs_sign_msg failed\n");
  11371. }
  11372. }
  11373. RECORD_MULTI_VALUE_STATS();
  11374. }
  11375. count += i;
  11376. } while (bench_stats_check(start)
  11377. #ifdef MULTI_VALUE_STATISTICS
  11378. || runs < minimum_runs
  11379. #endif
  11380. );
  11381. if (ret == 0) {
  11382. if (optim == FAST_VARIANT) {
  11383. bench_stats_asym_finish("SPHINCS-FAST", level, desc[4], 0, count,
  11384. start, ret);
  11385. }
  11386. else {
  11387. bench_stats_asym_finish("SPHINCS-SMALL", level, desc[4], 0, count,
  11388. start, ret);
  11389. }
  11390. #ifdef MULTI_VALUE_STATISTICS
  11391. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11392. #endif
  11393. }
  11394. RESET_MULTI_VALUE_STATS_VARS();
  11395. bench_stats_start(&count, &start);
  11396. do {
  11397. for (i = 0; i < agreeTimes; i++) {
  11398. if (ret == 0) {
  11399. int verify = 0;
  11400. ret = wc_sphincs_verify_msg(sig, x, msg, sizeof(msg), &verify,
  11401. &key);
  11402. if (ret != 0 || verify != 1) {
  11403. printf("wc_sphincs_verify_msg failed %d, verify %d\n",
  11404. ret, verify);
  11405. ret = -1;
  11406. }
  11407. }
  11408. RECORD_MULTI_VALUE_STATS();
  11409. }
  11410. count += i;
  11411. } while (bench_stats_check(start)
  11412. #ifdef MULTI_VALUE_STATISTICS
  11413. || runs < minimum_runs
  11414. #endif
  11415. );
  11416. if (ret == 0) {
  11417. if (optim == FAST_VARIANT) {
  11418. bench_stats_asym_finish("SPHINCS-FAST", level, desc[5], 0, count,
  11419. start, ret);
  11420. }
  11421. else {
  11422. bench_stats_asym_finish("SPHINCS-SMALL", level, desc[5], 0, count,
  11423. start, ret);
  11424. }
  11425. #ifdef MULTI_VALUE_STATISTICS
  11426. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11427. #endif
  11428. }
  11429. wc_sphincs_free(&key);
  11430. }
  11431. #endif /* HAVE_SPHINCS */
  11432. #endif /* HAVE_PQC */
  11433. #if defined(_WIN32) && !defined(INTIME_RTOS)
  11434. #define WIN32_LEAN_AND_MEAN
  11435. #include <windows.h>
  11436. double current_time(int reset)
  11437. {
  11438. static int init = 0;
  11439. static LARGE_INTEGER freq;
  11440. LARGE_INTEGER count;
  11441. (void)reset;
  11442. if (!init) {
  11443. QueryPerformanceFrequency(&freq);
  11444. init = 1;
  11445. }
  11446. QueryPerformanceCounter(&count);
  11447. #ifdef BENCH_MICROSECOND
  11448. return ((double)count.QuadPart * 1000000) / freq.QuadPart;
  11449. #else
  11450. return (double)count.QuadPart / freq.QuadPart;
  11451. #endif
  11452. }
  11453. #elif defined MICROCHIP_PIC32
  11454. #if defined(WOLFSSL_MICROCHIP_PIC32MZ)
  11455. #define CLOCK 80000000.0
  11456. #else
  11457. #define CLOCK 40000000.0
  11458. #endif
  11459. extern void WriteCoreTimer(word32 t);
  11460. extern word32 ReadCoreTimer(void);
  11461. double current_time(int reset)
  11462. {
  11463. unsigned int ns;
  11464. if (reset) {
  11465. WriteCoreTimer(0);
  11466. }
  11467. /* get timer in ns */
  11468. ns = ReadCoreTimer();
  11469. /* return seconds as a double */
  11470. return ( ns / CLOCK * 2.0);
  11471. }
  11472. #elif defined(WOLFSSL_IAR_ARM_TIME) || defined (WOLFSSL_MDK_ARM) || \
  11473. defined(WOLFSSL_USER_CURRTIME) || defined(WOLFSSL_CURRTIME_REMAP)
  11474. /* declared above at line 239 */
  11475. /* extern double current_time(int reset); */
  11476. #elif defined(FREERTOS)
  11477. #ifdef PLATFORMIO
  11478. #include <freertos/FreeRTOS.h>
  11479. #include <freertos/task.h>
  11480. #else
  11481. #include "task.h"
  11482. #endif
  11483. #if defined(WOLFSSL_ESPIDF)
  11484. /* prototype definition */
  11485. int construct_argv();
  11486. extern char* __argv[22];
  11487. /* current_time(reset)
  11488. *
  11489. * Benchmark passage of time, in fractional seconds.
  11490. * [reset] is non zero to adjust timer or counter to zero
  11491. *
  11492. * Use care when repeatedly calling calling. See implementation. */
  11493. double current_time(int reset)
  11494. {
  11495. double ret;
  11496. #if ESP_IDF_VERSION_MAJOR >= 4
  11497. TickType_t tickCount; /* typically 32 bit, local FreeRTOS ticks */
  11498. #else
  11499. portTickType tickCount;
  11500. #endif
  11501. #if defined(__XTENSA__)
  11502. (void)reset;
  11503. if (reset) {
  11504. /* TODO: Determine a mechanism for reset that does not interfere
  11505. * with freeRTOS tick. Using this code for Xtensa appears to cause
  11506. * RTOS tick timer to stick. See "last_tickCount unchanged".
  11507. ESP_LOGW(TAG, "Current_time() reset!");
  11508. portTICK_TYPE_ENTER_CRITICAL();
  11509. {
  11510. esp_cpu_set_cycle_count((esp_cpu_cycle_count_t)0);
  11511. _esp_cpu_count_last = xthal_get_ccount();
  11512. _esp_cpu_count_last = esp_cpu_get_cycle_count();
  11513. }
  11514. portTICK_TYPE_EXIT_CRITICAL();
  11515. */
  11516. }
  11517. #else
  11518. /* Only reset the CPU counter for RISC-V */
  11519. if (reset) {
  11520. ESP_LOGV(TAG, "current_time() reset!");
  11521. /* TODO: why does Espressif esp_cpu_get_cycle_count() cause
  11522. * unexpected rollovers in return values for Xtensa but not RISC-V?
  11523. * See also esp_get_cycle_count_ex() */
  11524. #ifdef __XTENSA__
  11525. _esp_cpu_count_last = xthal_get_ccount();
  11526. #else
  11527. esp_cpu_set_cycle_count((esp_cpu_cycle_count_t)0);
  11528. _esp_cpu_count_last = esp_cpu_get_cycle_count();
  11529. #endif
  11530. }
  11531. #endif
  11532. /* tick count == ms, if configTICK_RATE_HZ is set to 1000 */
  11533. tickCount = xTaskGetTickCount(); /* RTOS ticks, not CPU cycles!
  11534. The count of ticks since vTaskStartScheduler was called,
  11535. typiclly in app_startup.c */
  11536. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  11537. ESP_LOGV(TAG, "tickCount = %lu", tickCount);
  11538. if (tickCount == last_tickCount) {
  11539. ESP_LOGW(TAG, "last_tickCount unchanged? %lu", tickCount);
  11540. }
  11541. if (tickCount < last_tickCount) {
  11542. ESP_LOGW(TAG, "last_tickCount overflow?");
  11543. }
  11544. #endif
  11545. if (reset) {
  11546. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  11547. ESP_LOGW(TAG, "Assign last_tickCount = %lu", tickCount);
  11548. #endif
  11549. last_tickCount = tickCount;
  11550. }
  11551. else {
  11552. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  11553. ESP_LOGW(TAG, "No Reset last_tickCount = %lu", tickCount);
  11554. #endif
  11555. }
  11556. #if defined(configTICK_RATE_HZ) && defined(CONFIG_FREERTOS_HZ)
  11557. ret = (double)tickCount / configTICK_RATE_HZ;
  11558. #else
  11559. ESP_LOGW(TAG, "Warning: configTICK_RATE_HZ not defined,"
  11560. "assuming 1000 Hz.");
  11561. ret = (double)(tickCount / 1000.0);
  11562. #endif /* configTICK_RATE_HZ */
  11563. return ret;
  11564. } /* current_time */
  11565. #else
  11566. /* current_time(reset)
  11567. *
  11568. * Benchmark passage of time, in fractional seconds.
  11569. * [reset] is non zero to adjust timer or counter to zero
  11570. *
  11571. * Use care when repeatedly calling calling. See implementation. */
  11572. double current_time(int reset)
  11573. {
  11574. portTickType tickCount;
  11575. /* tick count == ms, if configTICK_RATE_HZ is set to 1000 */
  11576. tickCount = xTaskGetTickCount();
  11577. return (double)tickCount / 1000;
  11578. }
  11579. #endif
  11580. #elif defined (WOLFSSL_TIRTOS)
  11581. extern double current_time(int reset);
  11582. #elif defined(FREESCALE_MQX)
  11583. double current_time(int reset)
  11584. {
  11585. TIME_STRUCT tv;
  11586. _time_get(&tv);
  11587. return (double)tv.SECONDS + (double)tv.MILLISECONDS / 1000;
  11588. }
  11589. #elif defined(FREESCALE_KSDK_BM)
  11590. double current_time(int reset)
  11591. {
  11592. return (double)OSA_TimeGetMsec() / 1000;
  11593. }
  11594. #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2)
  11595. double current_time(int reset)
  11596. {
  11597. (void)reset;
  11598. return (double)osKernelGetTickCount() / 1000.0;
  11599. }
  11600. #elif defined(WOLFSSL_EMBOS)
  11601. #include "RTOS.h"
  11602. double current_time(int reset)
  11603. {
  11604. double time_now;
  11605. double current_s = OS_GetTime() / 1000.0;
  11606. double current_us = OS_GetTime_us() / MILLION_VALUE;
  11607. time_now = (double)( current_s + current_us);
  11608. (void) reset;
  11609. return time_now;
  11610. }
  11611. #elif defined(WOLFSSL_SGX)
  11612. double current_time(int reset);
  11613. #elif defined(WOLFSSL_DEOS)
  11614. double current_time(int reset)
  11615. {
  11616. const uint32_t systemTickTimeInHz
  11617. = 1000000 / systemTickInMicroseconds();
  11618. const volatile uint32_t *systemTickPtr = systemTickPointer();
  11619. (void)reset;
  11620. return (double) *systemTickPtr/systemTickTimeInHz;
  11621. }
  11622. #elif defined(MICRIUM)
  11623. double current_time(int reset)
  11624. {
  11625. #if (OS_VERSION < 50000)
  11626. CPU_ERR err;
  11627. (void)reset;
  11628. return (double) CPU_TS_Get32()/CPU_TS_TmrFreqGet(&err);
  11629. #else
  11630. RTOS_ERR err;
  11631. double ret = 0;
  11632. OS_TICK tick = OSTimeGet(&err);
  11633. OS_RATE_HZ rate = OSTimeTickRateHzGet(&err);
  11634. (void)reset;
  11635. if (RTOS_ERR_CODE_GET(err) == RTOS_ERR_NONE) {
  11636. ret = ((double)tick)/rate;
  11637. }
  11638. return ret;
  11639. #endif
  11640. }
  11641. #elif defined(WOLFSSL_ZEPHYR)
  11642. #include <time.h>
  11643. double current_time(int reset)
  11644. {
  11645. int64_t t;
  11646. (void)reset;
  11647. #if defined(CONFIG_ARCH_POSIX)
  11648. k_cpu_idle();
  11649. #endif
  11650. t = k_uptime_get(); /* returns current uptime in milliseconds */
  11651. return (double)(t / 1000);
  11652. }
  11653. #elif defined(WOLFSSL_NETBURNER)
  11654. #include <predef.h>
  11655. #include <utils.h>
  11656. #include <constants.h>
  11657. double current_time(int reset)
  11658. {
  11659. DWORD ticks = TimeTick; /* ticks since system start */
  11660. (void)reset;
  11661. return (double) ticks/TICKS_PER_SECOND;
  11662. }
  11663. #elif defined(THREADX)
  11664. #include "tx_api.h"
  11665. double current_time(int reset)
  11666. {
  11667. (void)reset;
  11668. return (double) tx_time_get() / TX_TIMER_TICKS_PER_SECOND;
  11669. }
  11670. #elif defined(WOLFSSL_XILINX)
  11671. #ifdef XPAR_VERSAL_CIPS_0_PSPMC_0_PSV_CORTEXA72_0_TIMESTAMP_CLK_FREQ
  11672. #define COUNTS_PER_SECOND \
  11673. XPAR_VERSAL_CIPS_0_PSPMC_0_PSV_CORTEXA72_0_TIMESTAMP_CLK_FREQ
  11674. #else
  11675. #define COUNTS_PER_SECOND \
  11676. XPAR_CPU_CORTEXA53_0_TIMESTAMP_CLK_FREQ
  11677. #endif
  11678. double current_time(int reset)
  11679. {
  11680. double timer;
  11681. uint64_t cntPct = 0;
  11682. asm volatile("mrs %0, CNTPCT_EL0" : "=r" (cntPct));
  11683. /* Convert to milliseconds */
  11684. timer = (double)(cntPct / (COUNTS_PER_SECOND / 1000));
  11685. /* Convert to seconds.millisecond */
  11686. timer /= 1000;
  11687. return timer;
  11688. }
  11689. #elif defined(LINUX_RUSAGE_UTIME)
  11690. #include <sys/time.h>
  11691. #include <sys/resource.h>
  11692. static struct rusage base_rusage;
  11693. static struct rusage cur_rusage;
  11694. double current_time(int reset)
  11695. {
  11696. struct rusage rusage;
  11697. (void)reset;
  11698. LIBCALL_CHECK_RET(getrusage(RUSAGE_SELF, &rusage));
  11699. if (reset)
  11700. base_rusage = rusage;
  11701. else
  11702. cur_rusage = rusage;
  11703. /* only consider user time, as system time is host-related overhead
  11704. * outside wolfcrypt.
  11705. */
  11706. return (double)rusage.ru_utime.tv_sec +
  11707. (double)rusage.ru_utime.tv_usec / MILLION_VALUE;
  11708. }
  11709. static void check_for_excessive_stime(const char *desc,
  11710. const char *desc_extra)
  11711. {
  11712. double start_utime = (double)base_rusage.ru_utime.tv_sec +
  11713. (double)base_rusage.ru_utime.tv_usec / MILLION_VALUE;
  11714. double start_stime = (double)base_rusage.ru_stime.tv_sec +
  11715. (double)base_rusage.ru_stime.tv_usec / MILLION_VALUE;
  11716. double cur_utime = (double)cur_rusage.ru_utime.tv_sec +
  11717. (double)cur_rusage.ru_utime.tv_usec / MILLION_VALUE;
  11718. double cur_stime = (double)cur_rusage.ru_stime.tv_sec +
  11719. (double)cur_rusage.ru_stime.tv_usec / MILLION_VALUE;
  11720. double stime_utime_ratio =
  11721. (cur_stime - start_stime) / (cur_utime - start_utime);
  11722. if (stime_utime_ratio > .1)
  11723. printf("%swarning, "
  11724. "excessive system time ratio for %s%s (" FLT_FMT_PREC "%%).\n",
  11725. err_prefix, desc, desc_extra,
  11726. FLT_FMT_PREC_ARGS(3, stime_utime_ratio * 100.0));
  11727. }
  11728. #elif defined(WOLFSSL_LINUXKM)
  11729. double current_time(int reset)
  11730. {
  11731. (void)reset;
  11732. u64 ns = ktime_get_ns();
  11733. return (double)ns / 1000000000.0;
  11734. }
  11735. #else
  11736. #include <sys/time.h>
  11737. double current_time(int reset)
  11738. {
  11739. struct timespec tv;
  11740. (void)reset;
  11741. LIBCALL_CHECK_RET(clock_gettime(CLOCK_REALTIME, &tv));
  11742. #ifdef BENCH_MICROSECOND
  11743. return (double)tv.tv_sec * 1000000 + (double)tv.tv_nsec / 1000;
  11744. #else
  11745. return (double)tv.tv_sec + (double)tv.tv_nsec / 1000000000;
  11746. #endif
  11747. }
  11748. #endif /* _WIN32 */
  11749. #if defined(HAVE_GET_CYCLES)
  11750. #if defined(WOLFSSL_ESPIDF)
  11751. /* Generic CPU cycle counter for either Xtensa or RISC-V */
  11752. static WC_INLINE word64 esp_get_cpu_benchmark_cycles(void)
  11753. {
  11754. /* Reminder for long duration between calls with
  11755. * multiple overflows will not be detected. */
  11756. return esp_get_cycle_count_ex();
  11757. }
  11758. /* implement other architectures here */
  11759. #else
  11760. static WC_INLINE word64 get_intel_cycles(void)
  11761. {
  11762. unsigned int lo_c, hi_c;
  11763. __asm__ __volatile__ (
  11764. "cpuid\n\t"
  11765. "rdtsc"
  11766. : "=a"(lo_c), "=d"(hi_c) /* out */
  11767. : "a"(0) /* in */
  11768. : "%ebx", "%ecx"); /* clobber */
  11769. return ((word64)lo_c) | (((word64)hi_c) << 32);
  11770. }
  11771. #endif
  11772. #endif /* HAVE_GET_CYCLES */
  11773. void benchmark_configure(word32 block_size)
  11774. {
  11775. /* must be greater than 0 */
  11776. if (block_size > 0) {
  11777. numBlocks = (int)((word32)numBlocks * bench_size / block_size);
  11778. bench_size = block_size;
  11779. }
  11780. }
  11781. #ifndef NO_MAIN_DRIVER
  11782. #ifndef MAIN_NO_ARGS
  11783. #ifndef WOLFSSL_BENCHMARK_ALL
  11784. /* Display the algorithm string and keep to 80 characters per line.
  11785. *
  11786. * str Algorithm string to print.
  11787. * line Length of line used so far.
  11788. */
  11789. #ifndef BENCH_MAX_LINE
  11790. #define BENCH_MAX_LINE 80
  11791. #endif
  11792. static void print_alg(const char* str, int* line)
  11793. {
  11794. const char* const ident = " ";
  11795. if (*line == 0) {
  11796. printf("%s", ident);
  11797. *line = (int)XSTRLEN(ident);
  11798. }
  11799. printf(" %s", str);
  11800. *line += (int)XSTRLEN(str) + 1;
  11801. if (*line > BENCH_MAX_LINE) {
  11802. printf("\n");
  11803. *line = 0;
  11804. }
  11805. }
  11806. #endif /* WOLFSSL_BENCHMARK_ALL */
  11807. /* Display the usage options of the benchmark program. */
  11808. static void Usage(void)
  11809. {
  11810. int e = 0;
  11811. #ifndef WOLFSSL_BENCHMARK_ALL
  11812. int i;
  11813. int line;
  11814. #endif
  11815. printf("benchmark\n");
  11816. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -? */
  11817. printf("%s", bench_Usage_msg1[lng_index][e++]); /* English / Japanese */
  11818. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -csv */
  11819. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -base10 */
  11820. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  11821. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -no_aad */
  11822. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -aad_size */
  11823. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -all_aad */
  11824. #else
  11825. e += 3;
  11826. #endif
  11827. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -dgst_full */
  11828. #ifndef NO_RSA
  11829. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -ras_sign */
  11830. #ifdef WOLFSSL_KEY_GEN
  11831. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -rsa-sz */
  11832. #endif
  11833. e++;
  11834. #else
  11835. e += 2;
  11836. #endif
  11837. #if !defined(NO_DH) && defined(HAVE_FFDHE_2048)
  11838. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -ffdhe2048 */
  11839. #endif
  11840. e++;
  11841. #if !defined(NO_DH) && defined(HAVE_FFDHE_3072)
  11842. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -ffdhe3072 */
  11843. #endif
  11844. e++;
  11845. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  11846. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -p256 */
  11847. #endif
  11848. e++;
  11849. #if defined(HAVE_ECC) && defined(HAVE_ECC384)
  11850. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -p384 */
  11851. #endif
  11852. e++;
  11853. #if defined(HAVE_ECC) && defined(HAVE_ECC521)
  11854. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -p521 */
  11855. #endif
  11856. e++;
  11857. #if defined(HAVE_ECC)
  11858. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -ecc-all */
  11859. #endif
  11860. e++;
  11861. #ifndef WOLFSSL_BENCHMARK_ALL
  11862. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -<alg> */
  11863. line = 0;
  11864. for (i=0; bench_cipher_opt[i].str != NULL; i++)
  11865. print_alg(bench_cipher_opt[i].str, &line);
  11866. for (i=0; bench_digest_opt[i].str != NULL; i++)
  11867. print_alg(bench_digest_opt[i].str, &line);
  11868. for (i=0; bench_mac_opt[i].str != NULL; i++)
  11869. print_alg(bench_mac_opt[i].str, &line);
  11870. for (i=0; bench_kdf_opt[i].str != NULL; i++)
  11871. print_alg(bench_kdf_opt[i].str, &line);
  11872. for (i=0; bench_asym_opt[i].str != NULL; i++)
  11873. print_alg(bench_asym_opt[i].str, &line);
  11874. for (i=0; bench_other_opt[i].str != NULL; i++)
  11875. print_alg(bench_other_opt[i].str, &line);
  11876. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  11877. for (i=0; bench_pq_asym_opt[i].str != NULL; i++)
  11878. print_alg(bench_pq_asym_opt[i].str, &line);
  11879. #if defined(HAVE_LIBOQS) && defined(HAVE_SPHINCS)
  11880. for (i=0; bench_pq_asym_opt2[i].str != NULL; i++)
  11881. print_alg(bench_pq_asym_opt2[i].str, &line);
  11882. #endif /* HAVE_LIBOQS && HAVE_SPHINCS */
  11883. #endif /* HAVE_PQC */
  11884. #if defined(BENCH_PQ_STATEFUL_HBS)
  11885. for (i=0; bench_pq_hash_sig_opt[i].str != NULL; i++)
  11886. print_alg(bench_pq_hash_sig_opt[i].str, &line);
  11887. #endif /* BENCH_PQ_STATEFUL_HBS */
  11888. printf("\n");
  11889. #endif /* !WOLFSSL_BENCHMARK_ALL */
  11890. e++;
  11891. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -lng */
  11892. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option <num> */
  11893. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -blocks <num> */
  11894. #ifdef WC_ENABLE_BENCH_THREADING
  11895. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -threads <num> */
  11896. #endif
  11897. e++;
  11898. #ifdef WC_BENCH_TRACK_STATS
  11899. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -print */
  11900. #endif
  11901. e++;
  11902. #ifndef NO_FILESYSTEM
  11903. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -hash_input */
  11904. #endif
  11905. e++;
  11906. #ifndef NO_FILESYSTEM
  11907. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -cipher_input */
  11908. #endif
  11909. #ifdef MULTI_VALUE_STATISTICS
  11910. e++;
  11911. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -min_runs */
  11912. #endif
  11913. }
  11914. /* Match the command line argument with the string.
  11915. *
  11916. * arg Command line argument.
  11917. * str String to check for.
  11918. * return 1 if the command line argument matches the string, 0 otherwise.
  11919. */
  11920. static int string_matches(const char* arg, const char* str)
  11921. {
  11922. return XSTRCMP(arg, str) == 0;
  11923. }
  11924. #endif /* MAIN_NO_ARGS */
  11925. /*
  11926. ** ----------------------------------------------------------------------------
  11927. ** determine how the benchmarks are called, the function name varies:
  11928. ** ----------------------------------------------------------------------------
  11929. */
  11930. #if !defined(NO_MAIN_DRIVER) && !defined(NO_MAIN_FUNCTION)
  11931. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  11932. /* for some environments, we'll call a function wolf_benchmark_task: */
  11933. int wolf_benchmark_task(void)
  11934. #elif defined(MAIN_NO_ARGS)
  11935. /* otherwise we'll use main() with no arguments as desired: */
  11936. int main()
  11937. #else
  11938. /* else we'll be calling main with default arg parameters */
  11939. int main(int argc, char** argv)
  11940. #endif
  11941. {
  11942. /* Code for main() or wolf_benchmark_task() */
  11943. #ifdef WOLFSSL_ESPIDF
  11944. int argc = construct_argv();
  11945. char** argv = (char**)__argv;
  11946. #elif defined(MAIN_NO_ARGS)
  11947. int argc = 0;
  11948. char** argv = NULL;
  11949. #endif
  11950. return wolfcrypt_benchmark_main(argc, argv);
  11951. }
  11952. #endif /* !NO_MAIN_DRIVER && !NO_MAIN_FUNCTION */
  11953. int wolfcrypt_benchmark_main(int argc, char** argv)
  11954. {
  11955. int ret = 0;
  11956. #ifndef MAIN_NO_ARGS
  11957. int optMatched;
  11958. #ifndef WOLFSSL_BENCHMARK_ALL
  11959. int i;
  11960. #endif
  11961. #endif
  11962. benchmark_static_init(1);
  11963. printf("%s------------------------------------------------------------------------------\n",
  11964. info_prefix);
  11965. printf("%s wolfSSL version %s\n", info_prefix, LIBWOLFSSL_VERSION_STRING);
  11966. printf("%s------------------------------------------------------------------------------\n",
  11967. info_prefix);
  11968. #ifndef MAIN_NO_ARGS
  11969. while (argc > 1) {
  11970. if (string_matches(argv[1], "-?")) {
  11971. if (--argc > 1) {
  11972. lng_index = XATOI((++argv)[1]);
  11973. if (lng_index<0 || lng_index>1) {
  11974. lng_index = 0;
  11975. }
  11976. }
  11977. Usage();
  11978. return 0;
  11979. }
  11980. else if (string_matches(argv[1], "-lng")) {
  11981. argc--;
  11982. argv++;
  11983. if (argc > 1) {
  11984. lng_index = XATOI(argv[1]);
  11985. if (lng_index<0 || lng_index>1) {
  11986. printf("invalid number(%d) is specified. [<num> :0-1]\n",
  11987. lng_index);
  11988. lng_index = 0;
  11989. }
  11990. }
  11991. }
  11992. else if (string_matches(argv[1], "-base10"))
  11993. base2 = 0;
  11994. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  11995. else if (string_matches(argv[1], "-no_aad"))
  11996. aes_aad_options = AAD_SIZE_ZERO;
  11997. else if (string_matches(argv[1], "-all_aad"))
  11998. aes_aad_options |= AAD_SIZE_ZERO | AAD_SIZE_DEFAULT;
  11999. else if (string_matches(argv[1], "-aad_size")) {
  12000. argc--;
  12001. argv++;
  12002. if (argc > 1) {
  12003. aes_aad_size = (word32)XATOI(argv[1]);
  12004. aes_aad_options |= AAD_SIZE_CUSTOM;
  12005. }
  12006. }
  12007. #endif
  12008. else if (string_matches(argv[1], "-dgst_full"))
  12009. digest_stream = 0;
  12010. #ifndef NO_RSA
  12011. else if (string_matches(argv[1], "-rsa_sign"))
  12012. rsa_sign_verify = 1;
  12013. #endif
  12014. #if !defined(NO_DH) && defined(HAVE_FFDHE_2048)
  12015. else if (string_matches(argv[1], "-ffdhe2048"))
  12016. use_ffdhe = 2048;
  12017. #endif
  12018. #if !defined(NO_DH) && defined(HAVE_FFDHE_3072)
  12019. else if (string_matches(argv[1], "-ffdhe3072"))
  12020. use_ffdhe = 3072;
  12021. #endif
  12022. #if !defined(NO_DH) && defined(HAVE_FFDHE_4096)
  12023. else if (string_matches(argv[1], "-ffdhe4096"))
  12024. use_ffdhe = 4096;
  12025. #endif
  12026. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  12027. else if (string_matches(argv[1], "-p256"))
  12028. bench_asym_algs |= BENCH_ECC_P256;
  12029. #endif
  12030. #if defined(HAVE_ECC) && defined(HAVE_ECC384)
  12031. else if (string_matches(argv[1], "-p384"))
  12032. bench_asym_algs |= BENCH_ECC_P384;
  12033. #endif
  12034. #if defined(HAVE_ECC) && defined(HAVE_ECC521)
  12035. else if (string_matches(argv[1], "-p521"))
  12036. bench_asym_algs |= BENCH_ECC_P521;
  12037. #endif
  12038. #ifdef BENCH_ASYM
  12039. else if (string_matches(argv[1], "-csv")) {
  12040. csv_format = 1;
  12041. }
  12042. #endif
  12043. #ifdef WC_ENABLE_BENCH_THREADING
  12044. else if (string_matches(argv[1], "-threads")) {
  12045. argc--;
  12046. argv++;
  12047. if (argc > 1) {
  12048. g_threadCount = XATOI(argv[1]);
  12049. if (g_threadCount < 1 || lng_index > 128){
  12050. printf("invalid number(%d) is specified. [<num> :1-128]\n",
  12051. g_threadCount);
  12052. g_threadCount = 0;
  12053. }
  12054. }
  12055. }
  12056. #endif
  12057. #ifdef WC_BENCH_TRACK_STATS
  12058. else if (string_matches(argv[1], "-print")) {
  12059. gPrintStats = 1;
  12060. }
  12061. #endif
  12062. else if (string_matches(argv[1], "-blocks")) {
  12063. argc--;
  12064. argv++;
  12065. if (argc > 1)
  12066. numBlocks = XATOI(argv[1]);
  12067. }
  12068. #ifndef NO_FILESYSTEM
  12069. else if (string_matches(argv[1], "-hash_input")) {
  12070. argc--;
  12071. argv++;
  12072. if (argc > 1)
  12073. hash_input = argv[1];
  12074. }
  12075. else if (string_matches(argv[1], "-cipher_input")) {
  12076. argc--;
  12077. argv++;
  12078. if (argc > 1)
  12079. cipher_input = argv[1];
  12080. }
  12081. #endif
  12082. #ifdef MULTI_VALUE_STATISTICS
  12083. else if (string_matches(argv[1], "-min_runs")) {
  12084. argc--;
  12085. argv++;
  12086. if (argc > 1) {
  12087. minimum_runs = XATOI(argv[1]);
  12088. }
  12089. }
  12090. #endif
  12091. else if (argv[1][0] == '-') {
  12092. optMatched = 0;
  12093. #ifndef WOLFSSL_BENCHMARK_ALL
  12094. /* Check known algorithm choosing command line options. */
  12095. /* Known cipher algorithms */
  12096. for (i=0; !optMatched && bench_cipher_opt[i].str != NULL; i++) {
  12097. if (string_matches(argv[1], bench_cipher_opt[i].str)) {
  12098. bench_cipher_algs |= bench_cipher_opt[i].val;
  12099. bench_all = 0;
  12100. optMatched = 1;
  12101. }
  12102. }
  12103. /* Known digest algorithms */
  12104. for (i=0; !optMatched && bench_digest_opt[i].str != NULL; i++) {
  12105. if (string_matches(argv[1], bench_digest_opt[i].str)) {
  12106. bench_digest_algs |= bench_digest_opt[i].val;
  12107. bench_all = 0;
  12108. optMatched = 1;
  12109. }
  12110. }
  12111. /* Known MAC algorithms */
  12112. for (i=0; !optMatched && bench_mac_opt[i].str != NULL; i++) {
  12113. if (string_matches(argv[1], bench_mac_opt[i].str)) {
  12114. bench_mac_algs |= bench_mac_opt[i].val;
  12115. bench_all = 0;
  12116. optMatched = 1;
  12117. }
  12118. }
  12119. /* Known KDF algorithms */
  12120. for (i=0; !optMatched && bench_kdf_opt[i].str != NULL; i++) {
  12121. if (string_matches(argv[1], bench_kdf_opt[i].str)) {
  12122. bench_kdf_algs |= bench_kdf_opt[i].val;
  12123. bench_all = 0;
  12124. optMatched = 1;
  12125. }
  12126. }
  12127. /* Known asymmetric algorithms */
  12128. for (i=0; !optMatched && bench_asym_opt[i].str != NULL; i++) {
  12129. if (string_matches(argv[1], bench_asym_opt[i].str)) {
  12130. bench_asym_algs |= bench_asym_opt[i].val;
  12131. bench_all = 0;
  12132. optMatched = 1;
  12133. }
  12134. }
  12135. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  12136. /* Known asymmetric post-quantum algorithms */
  12137. for (i=0; !optMatched && bench_pq_asym_opt[i].str != NULL; i++) {
  12138. if (string_matches(argv[1], bench_pq_asym_opt[i].str)) {
  12139. bench_pq_asym_algs |= bench_pq_asym_opt[i].val;
  12140. bench_all = 0;
  12141. optMatched = 1;
  12142. }
  12143. }
  12144. #ifdef HAVE_SPHINCS
  12145. /* Both bench_pq_asym_opt and bench_pq_asym_opt2 are looking for
  12146. * -pq, so we need to do a special case for -pq since optMatched
  12147. * was set to 1 just above. */
  12148. if ((bench_pq_asym_opt[0].str != NULL) &&
  12149. string_matches(argv[1], bench_pq_asym_opt[0].str))
  12150. {
  12151. bench_pq_asym_algs2 |= bench_pq_asym_opt2[0].val;
  12152. bench_all = 0;
  12153. optMatched = 1;
  12154. }
  12155. for (i=1; !optMatched && bench_pq_asym_opt2[i].str != NULL; i++) {
  12156. if (string_matches(argv[1], bench_pq_asym_opt2[i].str)) {
  12157. bench_pq_asym_algs2 |= bench_pq_asym_opt2[i].val;
  12158. bench_all = 0;
  12159. optMatched = 1;
  12160. }
  12161. }
  12162. #endif
  12163. #endif /* HAVE_PQC */
  12164. /* Other known cryptographic algorithms */
  12165. for (i=0; !optMatched && bench_other_opt[i].str != NULL; i++) {
  12166. if (string_matches(argv[1], bench_other_opt[i].str)) {
  12167. bench_other_algs |= bench_other_opt[i].val;
  12168. bench_all = 0;
  12169. optMatched = 1;
  12170. }
  12171. }
  12172. #if defined(BENCH_PQ_STATEFUL_HBS)
  12173. /* post-quantum stateful hash-based signatures */
  12174. for (i=0; !optMatched && bench_pq_hash_sig_opt[i].str != NULL; i++) {
  12175. if (string_matches(argv[1], bench_pq_hash_sig_opt[i].str)) {
  12176. bench_pq_hash_sig_algs |= bench_pq_hash_sig_opt[i].val;
  12177. bench_all = 0;
  12178. optMatched = 1;
  12179. }
  12180. }
  12181. #endif /* BENCH_PQ_STATEFUL_HBS */
  12182. #endif
  12183. if (!optMatched) {
  12184. printf("Option not recognized: %s\n", argv[1]);
  12185. Usage();
  12186. return 1;
  12187. }
  12188. }
  12189. else {
  12190. /* parse for block size */
  12191. benchmark_configure((word32)XATOI(argv[1]));
  12192. }
  12193. argc--;
  12194. argv++;
  12195. }
  12196. #endif /* MAIN_NO_ARGS */
  12197. #if defined(WOLFSSL_BENCHMARK_FIXED_CSV)
  12198. /* when defined, we'll always output CSV regardless of params.
  12199. ** this is typically convenient in embedded environments.
  12200. */
  12201. csv_format = 1;
  12202. #endif
  12203. #if defined(WC_ENABLE_BENCH_THREADING) && !defined(WOLFSSL_ASYNC_CRYPT)
  12204. if (g_threadCount > 1) {
  12205. ret = benchmark_test_threaded(NULL);
  12206. }
  12207. else
  12208. #endif
  12209. {
  12210. #if defined(CONFIG_IDF_TARGET_ESP32C2) || \
  12211. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  12212. defined(CONFIG_IDF_TARGET_ESP32C6)
  12213. {
  12214. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  12215. if (esp_gptimer == NULL) {
  12216. ESP_ERROR_CHECK(gptimer_new_timer(&esp_timer_config,
  12217. &esp_gptimer) );
  12218. }
  12219. ESP_ERROR_CHECK(gptimer_enable(esp_gptimer));
  12220. ESP_ERROR_CHECK(gptimer_start(esp_gptimer));
  12221. ESP_LOGI(TAG, "Enable %s timer", CONFIG_IDF_TARGET);
  12222. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  12223. }
  12224. #endif
  12225. #ifdef HAVE_STACK_SIZE
  12226. ret = StackSizeCheck(NULL, benchmark_test);
  12227. #else
  12228. ret = benchmark_test(NULL);
  12229. #endif
  12230. }
  12231. #if defined(CONFIG_IDF_TARGET_ESP32C2) || \
  12232. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  12233. defined(CONFIG_IDF_TARGET_ESP32C6)
  12234. {
  12235. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  12236. ESP_ERROR_CHECK(gptimer_stop(esp_gptimer));
  12237. ESP_ERROR_CHECK(gptimer_disable(esp_gptimer));
  12238. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  12239. }
  12240. #endif
  12241. return ret;
  12242. }
  12243. #endif /* !NO_MAIN_DRIVER */
  12244. #else
  12245. #if !defined(NO_MAIN_DRIVER) && !defined(NO_MAIN_FUNCTION)
  12246. int main(void) { return 0; }
  12247. #endif
  12248. #endif /* !NO_CRYPT_BENCHMARK */