1
0

benchmark.c 437 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761
  1. /* benchmark.c
  2. *
  3. * Copyright (C) 2006-2024 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. * Turn on timer debugging (used when CPU cycles not available)
  54. * WOLFSSL_BENCHMARK_TIMER_DEBUG
  55. */
  56. #ifdef HAVE_CONFIG_H
  57. #include <config.h>
  58. #endif
  59. #ifndef WOLFSSL_USER_SETTINGS
  60. #include <wolfssl/options.h>
  61. #endif
  62. #include <wolfssl/wolfcrypt/settings.h> /* also picks up user_settings.h */
  63. /* Macro to disable benchmark */
  64. #ifndef NO_CRYPT_BENCHMARK
  65. #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)
  66. #include <wolfssl/wolfcrypt/types.h>
  67. #include <wolfssl/wolfcrypt/wc_port.h>
  68. #include <wolfssl/wolfcrypt/wolfmath.h>
  69. #include <wolfssl/wolfcrypt/memory.h>
  70. #include <wolfssl/wolfcrypt/random.h>
  71. #include <wolfssl/wolfcrypt/error-crypt.h>
  72. #include <wolfssl/wolfcrypt/asn.h>
  73. #include <wolfssl/version.h>
  74. #ifdef WOLFSSL_LINUXKM
  75. /* remap current_time() -- collides with a function in kernel linux/fs.h */
  76. #define current_time benchmark_current_time
  77. #endif /* WOLFSSL_LINUXKM */
  78. #ifdef HAVE_CHACHA
  79. #include <wolfssl/wolfcrypt/chacha.h>
  80. #endif
  81. #ifdef HAVE_POLY1305
  82. #include <wolfssl/wolfcrypt/poly1305.h>
  83. #endif
  84. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  85. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  86. #endif
  87. #ifndef NO_AES
  88. #include <wolfssl/wolfcrypt/aes.h>
  89. #endif
  90. #ifdef HAVE_CAMELLIA
  91. #include <wolfssl/wolfcrypt/camellia.h>
  92. #endif
  93. #ifdef WOLFSSL_SM4
  94. #include <wolfssl/wolfcrypt/sm4.h>
  95. #endif
  96. #ifndef NO_MD5
  97. #include <wolfssl/wolfcrypt/md5.h>
  98. #endif
  99. #ifndef NO_SHA
  100. #include <wolfssl/wolfcrypt/sha.h>
  101. #endif
  102. #ifndef NO_SHA256
  103. #include <wolfssl/wolfcrypt/sha256.h>
  104. #endif
  105. #if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)
  106. #include <wolfssl/wolfcrypt/sha512.h>
  107. #endif
  108. #ifdef WOLFSSL_SHA3
  109. #include <wolfssl/wolfcrypt/sha3.h>
  110. #endif
  111. #ifdef WOLFSSL_SM3
  112. #include <wolfssl/wolfcrypt/sm3.h>
  113. #endif
  114. #ifndef NO_RSA
  115. #include <wolfssl/wolfcrypt/rsa.h>
  116. #endif
  117. #ifdef WOLFSSL_RIPEMD
  118. #include <wolfssl/wolfcrypt/ripemd.h>
  119. #endif
  120. #ifdef WOLFSSL_CMAC
  121. #include <wolfssl/wolfcrypt/cmac.h>
  122. #endif
  123. #ifndef NO_DH
  124. #include <wolfssl/wolfcrypt/dh.h>
  125. #endif
  126. #ifndef NO_DES3
  127. #include <wolfssl/wolfcrypt/des3.h>
  128. #endif
  129. #ifndef NO_RC4
  130. #include <wolfssl/wolfcrypt/arc4.h>
  131. #endif
  132. #ifndef NO_HMAC
  133. #include <wolfssl/wolfcrypt/hmac.h>
  134. #endif
  135. #ifdef WOLFSSL_SIPHASH
  136. #include <wolfssl/wolfcrypt/siphash.h>
  137. #endif
  138. #include <wolfssl/wolfcrypt/kdf.h>
  139. #ifndef NO_PWDBASED
  140. #include <wolfssl/wolfcrypt/pwdbased.h>
  141. #endif
  142. #ifdef HAVE_ECC
  143. #include <wolfssl/wolfcrypt/ecc.h>
  144. #endif
  145. #ifdef WOLFSSL_SM2
  146. #include <wolfssl/wolfcrypt/sm2.h>
  147. #endif
  148. #ifdef HAVE_CURVE25519
  149. #include <wolfssl/wolfcrypt/curve25519.h>
  150. #endif
  151. #ifdef HAVE_ED25519
  152. #include <wolfssl/wolfcrypt/ed25519.h>
  153. #endif
  154. #ifdef HAVE_CURVE448
  155. #include <wolfssl/wolfcrypt/curve448.h>
  156. #endif
  157. #ifdef HAVE_ED448
  158. #include <wolfssl/wolfcrypt/ed448.h>
  159. #endif
  160. #ifdef WOLFSSL_HAVE_KYBER
  161. #include <wolfssl/wolfcrypt/kyber.h>
  162. #ifdef WOLFSSL_WC_KYBER
  163. #include <wolfssl/wolfcrypt/wc_kyber.h>
  164. #endif
  165. #if defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  166. #include <wolfssl/wolfcrypt/ext_kyber.h>
  167. #endif
  168. #endif
  169. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  170. #include <wolfssl/wolfcrypt/lms.h>
  171. #ifdef HAVE_LIBLMS
  172. #include <wolfssl/wolfcrypt/ext_lms.h>
  173. #else
  174. #include <wolfssl/wolfcrypt/wc_lms.h>
  175. #endif
  176. #endif
  177. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  178. #include <wolfssl/wolfcrypt/xmss.h>
  179. #ifdef HAVE_LIBXMSS
  180. #include <wolfssl/wolfcrypt/ext_xmss.h>
  181. #else
  182. #include <wolfssl/wolfcrypt/wc_xmss.h>
  183. #endif
  184. #endif
  185. #ifdef WOLFCRYPT_HAVE_ECCSI
  186. #include <wolfssl/wolfcrypt/eccsi.h>
  187. #endif
  188. #ifdef WOLFCRYPT_HAVE_SAKKE
  189. #include <wolfssl/wolfcrypt/sakke.h>
  190. #endif
  191. #if defined(HAVE_PQC)
  192. #if defined(HAVE_FALCON)
  193. #include <wolfssl/wolfcrypt/falcon.h>
  194. #endif
  195. #if defined(HAVE_DILITHIUM)
  196. #include <wolfssl/wolfcrypt/dilithium.h>
  197. #endif
  198. #if defined(HAVE_SPHINCS)
  199. #include <wolfssl/wolfcrypt/sphincs.h>
  200. #endif
  201. #endif
  202. #ifdef WOLF_CRYPTO_CB
  203. #include <wolfssl/wolfcrypt/cryptocb.h>
  204. #ifdef HAVE_INTEL_QA_SYNC
  205. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  206. #endif
  207. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  208. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  209. #endif
  210. #ifdef HAVE_RENESAS_SYNC
  211. #include <wolfssl/wolfcrypt/port/renesas/renesas_sync.h>
  212. #endif
  213. #endif
  214. #ifdef WOLFSSL_ASYNC_CRYPT
  215. #include <wolfssl/wolfcrypt/async.h>
  216. #endif
  217. #ifdef USE_FLAT_BENCHMARK_H
  218. #include "benchmark.h"
  219. #else
  220. #include "wolfcrypt/benchmark/benchmark.h"
  221. #endif
  222. /* define the max length for each string of metric reported */
  223. #ifndef WC_BENCH_MAX_LINE_LEN
  224. #define WC_BENCH_MAX_LINE_LEN 150
  225. #endif
  226. /* default units per second. See WOLFSSL_BENCHMARK_FIXED_UNITS_* to change */
  227. #define WOLFSSL_FIXED_UNIT "MB" /* may be re-set by fixed units */
  228. #define MILLION_VALUE 1000000.0
  229. #ifdef BENCH_MICROSECOND
  230. #define WOLFSSL_FIXED_TIME_UNIT "μs"
  231. #define WOLFSSL_BENCHMARK_FIXED_UNITS_KB
  232. #else
  233. #define WOLFSSL_FIXED_TIME_UNIT "s"
  234. #endif
  235. #ifdef MULTI_VALUE_STATISTICS
  236. #define STATS_CLAUSE_SEPARATOR ""
  237. #define DECLARE_MULTI_VALUE_STATS_VARS() double max = 0, min = 0, sum = 0,\
  238. squareSum = 0, prev = 0, delta;\
  239. int runs = 0;
  240. #define RECORD_MULTI_VALUE_STATS() if (runs == 0) {\
  241. delta = current_time(0) - start;\
  242. min = delta;\
  243. max = delta;\
  244. }\
  245. else {\
  246. delta = current_time(0) - prev;\
  247. }\
  248. if (max < delta)\
  249. max = delta;\
  250. else if (min > delta)\
  251. min = delta;\
  252. sum += delta;\
  253. squareSum += delta * delta;\
  254. runs++;\
  255. prev = current_time(0)
  256. #define RESET_MULTI_VALUE_STATS_VARS() prev = 0;\
  257. runs = 0;\
  258. sum = 0;\
  259. squareSum = 0
  260. #else
  261. #define STATS_CLAUSE_SEPARATOR "\n"
  262. #define DECLARE_MULTI_VALUE_STATS_VARS()
  263. #define RECORD_MULTI_VALUE_STATS() WC_DO_NOTHING
  264. #define RESET_MULTI_VALUE_STATS_VARS() WC_DO_NOTHING
  265. #endif
  266. #ifdef WOLFSSL_NO_FLOAT_FMT
  267. #define FLT_FMT "%0ld,%09lu"
  268. #define FLT_FMT_PREC "%0ld.%0*lu"
  269. #define FLT_FMT_PREC2 FLT_FMT_PREC
  270. #define FLT_FMT_ARGS(x) (long)(x), ((x) < 0) ? \
  271. (unsigned long)(-(((x) - (double)(long)(x)) * 1000000000.0)) : \
  272. (unsigned long)(((x) - (double)(long)(x)) * 1000000000.0)
  273. static const double pow_10_array[] = { 0.0, 1.0, 10.0, 100.0, 1000.0, \
  274. 10000.0, 100000.0, 1000000.0, \
  275. 10000000.0, 100000000.0, \
  276. 1000000000.0 };
  277. #define FLT_FMT_PREC_ARGS(p, x) \
  278. (long)(x), \
  279. p, \
  280. (x) >= 0.0 ? \
  281. (unsigned long int)((((x) - (double)(long)(x)) * \
  282. pow_10_array[(p)+1]) + 0.5) : \
  283. (unsigned long int)((((-(x)) - (double)((long)-(x))) * \
  284. pow_10_array[(p)+1]) + 0.5)
  285. #define FLT_FMT_PREC2_ARGS(w, p, x) FLT_FMT_PREC_ARGS(p, x)
  286. #else
  287. #define FLT_FMT "%f"
  288. #define FLT_FMT_PREC "%.*f"
  289. #define FLT_FMT_PREC2 "%*.*f"
  290. #define FLT_FMT_ARGS(x) x
  291. #define FLT_FMT_PREC_ARGS(p, x) p, x
  292. #define FLT_FMT_PREC2_ARGS(w, p, x) w, p, x
  293. #endif /* WOLFSSL_NO_FLOAT_FMT */
  294. #ifdef WOLFSSL_ESPIDF
  295. #include <wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h>
  296. /* Benchmark uses 64 bit integer formatting support. When new nanolib is
  297. * enabled, all if the values in report are blank. */
  298. #ifdef CONFIG_NEWLIB_NANO_FORMAT
  299. #if CONFIG_NEWLIB_NANO_FORMAT == 1
  300. #error "Nano newlib formatting must not be enabled for benchmark"
  301. #endif
  302. #endif
  303. #ifdef configTICK_RATE_HZ
  304. /* Define CPU clock cycles per tick of FreeRTOS clock
  305. * CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ is typically a value like 240
  306. * configTICK_RATE_HZ is typically 100 or 1000.
  307. **/
  308. #if defined(CONFIG_IDF_TARGET_ESP8266)
  309. #ifndef CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ
  310. #define CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ \
  311. CONFIG_ESP8266_DEFAULT_CPU_FREQ_MHZ
  312. #endif
  313. #ifndef CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ
  314. #define CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ configCPU_CLOCK_HZ
  315. #endif
  316. #endif
  317. #define CPU_TICK_CYCLES ( \
  318. (CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ * MILLION_VALUE) \
  319. / configTICK_RATE_HZ \
  320. )
  321. #endif /* WOLFSSL_ESPIDF configTICK_RATE_HZ */
  322. #if defined(CONFIG_IDF_TARGET_ESP32C2)
  323. #include "driver/gptimer.h"
  324. static gptimer_handle_t esp_gptimer = NULL;
  325. static gptimer_config_t esp_timer_config = {
  326. .clk_src = GPTIMER_CLK_SRC_DEFAULT,
  327. .direction = GPTIMER_COUNT_UP,
  328. .resolution_hz = CONFIG_XTAL_FREQ * 100000,
  329. };
  330. #elif defined(CONFIG_IDF_TARGET_ESP32C3) || \
  331. defined(CONFIG_IDF_TARGET_ESP32C6)
  332. #include <esp_cpu.h>
  333. #include "driver/gptimer.h"
  334. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  335. #define RESOLUTION_SCALE 100
  336. static gptimer_handle_t esp_gptimer = NULL;
  337. static gptimer_config_t esp_timer_config = {
  338. .clk_src = GPTIMER_CLK_SRC_DEFAULT,
  339. .direction = GPTIMER_COUNT_UP,
  340. /* CONFIG_XTAL_FREQ = 40,
  341. * CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ = 160 */
  342. .resolution_hz = CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ *
  343. (MILLION_VALUE / RESOLUTION_SCALE),
  344. };
  345. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  346. #elif defined(CONFIG_IDF_TARGET_ESP32) || \
  347. defined(CONFIG_IDF_TARGET_ESP32S2) || \
  348. defined(CONFIG_IDF_TARGET_ESP32S3)
  349. #include <xtensa/hal.h>
  350. #elif defined(CONFIG_IDF_TARGET_ESP8266)
  351. /* no CPU HAL for ESP8266, we'll use RTOS tick calc estimates */
  352. #include <FreeRTOS.h>
  353. #elif defined(CONFIG_IDF_TARGET_ESP32H2)
  354. /* TODO add ESP32-H2 benchmark support */
  355. #else
  356. /* Other platform */
  357. #endif
  358. #include <esp_log.h>
  359. #endif /* WOLFSSL_ESPIDF */
  360. #if defined(HAVE_PTHREAD) || \
  361. (!defined(NO_CRYPT_BENCHMARK) && !defined(NO_STDIO_FILESYSTEM) && \
  362. !defined(NO_ERROR_STRINGS) && !defined(NO_MAIN_DRIVER) && \
  363. !defined(BENCH_EMBEDDED))
  364. #include <errno.h>
  365. #if !defined(WOLFSSL_ZEPHYR) && !defined(_WIN32)
  366. #include <unistd.h>
  367. #endif
  368. #endif
  369. #if defined(WOLFSSL_ZEPHYR) || defined(NO_STDIO_FILESYSTEM) || !defined(XFFLUSH)
  370. /* fflush in Zephyr doesn't work on stdout and stderr. Use
  371. * CONFIG_LOG_MODE_IMMEDIATE compilation option instead. */
  372. #undef XFFLUSH
  373. #define XFFLUSH(...) WC_DO_NOTHING
  374. #endif
  375. /* only for stack size check */
  376. #include <wolfssl/wolfcrypt/mem_track.h>
  377. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  378. #define WC_ENABLE_BENCH_THREADING
  379. #endif
  380. /* enable tracking of stats for threaded benchmark */
  381. #if defined(WC_ENABLE_BENCH_THREADING) && !defined(WC_BENCH_TRACK_STATS)
  382. #define WC_BENCH_TRACK_STATS
  383. #endif
  384. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  385. static const char info_prefix[] = "###, ";
  386. static const char err_prefix[] = "!!!, ";
  387. #else
  388. static const char info_prefix[] = "";
  389. static const char err_prefix[] = "";
  390. #endif
  391. /* printf mappings */
  392. #ifdef FREESCALE_MQX
  393. #include <mqx.h>
  394. /* see wc_port.h for fio.h and nio.h includes */
  395. #elif defined(FREESCALE_KSDK_1_3)
  396. #include "fsl_debug_console.h"
  397. #include "fsl_os_abstraction.h"
  398. #undef printf
  399. #define printf PRINTF
  400. #elif defined(WOLFSSL_DEOS)
  401. #include <deos.h>
  402. #include <printx.h>
  403. #undef printf
  404. #define printf printx
  405. #elif defined(MICRIUM)
  406. #if (OS_VERSION < 50000)
  407. #include <bsp_ser.h>
  408. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  409. #undef printf
  410. #define printf BSP_Ser_Printf
  411. #endif
  412. #elif defined(WOLFSSL_ZEPHYR)
  413. #include <stdio.h>
  414. #define BENCH_EMBEDDED
  415. #define printf printfk
  416. static int printfk(const char *fmt, ...)
  417. {
  418. int ret;
  419. char line[WC_BENCH_MAX_LINE_LEN];
  420. va_list ap;
  421. va_start(ap, fmt);
  422. ret = vsnprintf(line, sizeof(line), fmt, ap);
  423. line[sizeof(line)-1] = '\0';
  424. printk("%s", line);
  425. va_end(ap);
  426. return ret;
  427. }
  428. #elif defined(WOLFSSL_TELIT_M2MB)
  429. #include <stdarg.h>
  430. #include <stdio.h>
  431. #include <string.h>
  432. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  433. /* remap printf */
  434. #undef printf
  435. #define printf M2M_LOG_INFO
  436. /* OS requires occasional sleep() */
  437. #ifndef TEST_SLEEP_MS
  438. #define TEST_SLEEP_MS 50
  439. #endif
  440. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  441. /* don't use file system for these tests, since ./certs dir isn't loaded */
  442. #undef NO_FILESYSTEM
  443. #define NO_FILESYSTEM
  444. /* ANDROID_V454 (for android studio) displays information in a textview
  445. * and redirects printf to the textview output instead of using
  446. * __android_log_print() */
  447. #elif defined(ANDROID) && !defined(ANDROID_V454)
  448. #ifdef XMALLOC_USER
  449. #include <stdlib.h> /* we're using malloc / free direct here */
  450. #endif
  451. #ifndef STRING_USER
  452. #include <stdio.h>
  453. #endif
  454. #include <android/log.h>
  455. #define printf(...) \
  456. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  457. #define fprintf(fp, ...) \
  458. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  459. #else
  460. #if defined(XMALLOC_USER) || defined(FREESCALE_MQX)
  461. /* MQX classic needs for EXIT_FAILURE */
  462. #include <stdlib.h> /* we're using malloc / free direct here */
  463. #endif
  464. #if !defined(STRING_USER) && !defined(NO_STDIO_FILESYSTEM)
  465. #include <string.h>
  466. #include <stdio.h>
  467. #endif
  468. /* enable way for customer to override test/bench printf */
  469. #ifdef XPRINTF
  470. #undef printf
  471. #define printf XPRINTF
  472. #elif defined(NETOS)
  473. #undef printf
  474. #define printf dc_log_printf
  475. #endif
  476. #endif
  477. #ifdef HAVE_FIPS
  478. #include <wolfssl/wolfcrypt/fips_test.h>
  479. static void myFipsCb(int ok, int err, const char* hash)
  480. {
  481. printf("%sin my Fips callback, ok = %d, err = %d\n",
  482. ok ? info_prefix : err_prefix, ok, err);
  483. printf("%smessage = %s\n", ok ? info_prefix : err_prefix,
  484. wc_GetErrorString(err));
  485. printf("%shash = %s\n", ok ? info_prefix : err_prefix, hash);
  486. if (err == IN_CORE_FIPS_E) {
  487. printf("%sIn core integrity hash check failure, copy above hash\n",
  488. err_prefix);
  489. printf("%sinto verifyCore[] in fips_test.c and rebuild\n",
  490. err_prefix);
  491. }
  492. }
  493. #endif
  494. #ifdef WOLFSSL_STATIC_MEMORY
  495. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  496. #else
  497. #define HEAP_HINT NULL
  498. #endif /* WOLFSSL_STATIC_MEMORY */
  499. #ifndef EXIT_FAILURE
  500. #define EXIT_FAILURE 1
  501. #endif
  502. #undef LIBCALL_CHECK_RET
  503. #if defined(NO_STDIO_FILESYSTEM) || defined(NO_ERROR_STRINGS) || \
  504. defined(NO_MAIN_DRIVER) || defined(BENCH_EMBEDDED)
  505. #define LIBCALL_CHECK_RET(...) __VA_ARGS__
  506. #else
  507. #define LIBCALL_CHECK_RET(...) do { \
  508. int _libcall_ret = (__VA_ARGS__); \
  509. if (_libcall_ret < 0) { \
  510. printf("%s%s L%d error %d for \"%s\"\n", \
  511. err_prefix, __FILE__, __LINE__, \
  512. errno, #__VA_ARGS__); \
  513. XFFLUSH(stdout); \
  514. _exit(1); \
  515. } \
  516. } while(0)
  517. #endif
  518. #undef THREAD_CHECK_RET
  519. #define THREAD_CHECK_RET(...) do { \
  520. int _thread_ret = (__VA_ARGS__); \
  521. if (_thread_ret != 0) { \
  522. errno = _thread_ret; \
  523. printf("%s%s L%d error %d for \"%s\"\n", \
  524. err_prefix, __FILE__, __LINE__, \
  525. _thread_ret, #__VA_ARGS__); \
  526. XFFLUSH(stdout); \
  527. _exit(1); \
  528. } \
  529. } while(0)
  530. /* optional macro to add sleep between tests */
  531. #ifndef TEST_SLEEP
  532. /* stub the sleep macro */
  533. #define TEST_SLEEP() WC_DO_NOTHING
  534. #endif
  535. #define TEST_STRING "Everyone gets Friday off."
  536. #define TEST_STRING_SZ 25
  537. /* Bit values for each algorithm that is able to be benchmarked.
  538. * Common grouping of algorithms also.
  539. * Each algorithm has a unique value for its type e.g. cipher.
  540. */
  541. /* Cipher algorithms. */
  542. #define BENCH_AES_CBC 0x00000001
  543. #define BENCH_AES_GCM 0x00000002
  544. #define BENCH_AES_ECB 0x00000004
  545. #define BENCH_AES_XTS 0x00000008
  546. #define BENCH_AES_CTR 0x00000010
  547. #define BENCH_AES_CCM 0x00000020
  548. #define BENCH_CAMELLIA 0x00000100
  549. #define BENCH_ARC4 0x00000200
  550. #define BENCH_CHACHA20 0x00001000
  551. #define BENCH_CHACHA20_POLY1305 0x00002000
  552. #define BENCH_DES 0x00004000
  553. #define BENCH_AES_CFB 0x00010000
  554. #define BENCH_AES_OFB 0x00020000
  555. #define BENCH_AES_SIV 0x00040000
  556. #define BENCH_SM4_CBC 0x00080000
  557. #define BENCH_SM4_GCM 0x00100000
  558. #define BENCH_SM4_CCM 0x00200000
  559. #define BENCH_SM4 (BENCH_SM4_CBC | BENCH_SM4_GCM | BENCH_SM4_CCM)
  560. /* Digest algorithms. */
  561. #define BENCH_MD5 0x00000001
  562. #define BENCH_POLY1305 0x00000002
  563. #define BENCH_SHA 0x00000004
  564. #define BENCH_SHA224 0x00000010
  565. #define BENCH_SHA256 0x00000020
  566. #define BENCH_SHA384 0x00000040
  567. #define BENCH_SHA512 0x00000080
  568. #define BENCH_SHA2 (BENCH_SHA224 | BENCH_SHA256 | \
  569. BENCH_SHA384 | BENCH_SHA512)
  570. #define BENCH_SHA3_224 0x00000100
  571. #define BENCH_SHA3_256 0x00000200
  572. #define BENCH_SHA3_384 0x00000400
  573. #define BENCH_SHA3_512 0x00000800
  574. #define BENCH_SHA3 (BENCH_SHA3_224 | BENCH_SHA3_256 | \
  575. BENCH_SHA3_384 | BENCH_SHA3_512)
  576. #define BENCH_SHAKE128 0x00001000
  577. #define BENCH_SHAKE256 0x00002000
  578. #define BENCH_SHAKE (BENCH_SHAKE128 | BENCH_SHAKE256)
  579. #define BENCH_RIPEMD 0x00004000
  580. #define BENCH_BLAKE2B 0x00008000
  581. #define BENCH_BLAKE2S 0x00010000
  582. #define BENCH_SM3 0x00020000
  583. /* MAC algorithms. */
  584. #define BENCH_CMAC 0x00000001
  585. #define BENCH_HMAC_MD5 0x00000002
  586. #define BENCH_HMAC_SHA 0x00000004
  587. #define BENCH_HMAC_SHA224 0x00000010
  588. #define BENCH_HMAC_SHA256 0x00000020
  589. #define BENCH_HMAC_SHA384 0x00000040
  590. #define BENCH_HMAC_SHA512 0x00000080
  591. #define BENCH_HMAC (BENCH_HMAC_MD5 | BENCH_HMAC_SHA | \
  592. BENCH_HMAC_SHA224 | BENCH_HMAC_SHA256 | \
  593. BENCH_HMAC_SHA384 | BENCH_HMAC_SHA512)
  594. #define BENCH_PBKDF2 0x00000100
  595. #define BENCH_SIPHASH 0x00000200
  596. /* KDF algorithms */
  597. #define BENCH_SRTP_KDF 0x00000001
  598. /* Asymmetric algorithms. */
  599. #define BENCH_RSA_KEYGEN 0x00000001
  600. #define BENCH_RSA 0x00000002
  601. #define BENCH_RSA_SZ 0x00000004
  602. #define BENCH_DH 0x00000010
  603. #define BENCH_KYBER 0x00000020
  604. #define BENCH_ECC_MAKEKEY 0x00001000
  605. #define BENCH_ECC 0x00002000
  606. #define BENCH_ECC_ENCRYPT 0x00004000
  607. #define BENCH_ECC_ALL 0x00008000
  608. #define BENCH_CURVE25519_KEYGEN 0x00010000
  609. #define BENCH_CURVE25519_KA 0x00020000
  610. #define BENCH_ED25519_KEYGEN 0x00040000
  611. #define BENCH_ED25519_SIGN 0x00080000
  612. #define BENCH_CURVE448_KEYGEN 0x00100000
  613. #define BENCH_CURVE448_KA 0x00200000
  614. #define BENCH_ED448_KEYGEN 0x00400000
  615. #define BENCH_ED448_SIGN 0x00800000
  616. #define BENCH_ECC_P256 0x01000000
  617. #define BENCH_ECC_P384 0x02000000
  618. #define BENCH_ECC_P521 0x04000000
  619. #define BENCH_SM2 0x08000000
  620. #define BENCH_ECCSI_KEYGEN 0x00000020
  621. #define BENCH_ECCSI_PAIRGEN 0x00000040
  622. #define BENCH_ECCSI_VALIDATE 0x00000080
  623. #define BENCH_ECCSI 0x00000400
  624. #define BENCH_SAKKE_KEYGEN 0x10000000
  625. #define BENCH_SAKKE_RSKGEN 0x20000000
  626. #define BENCH_SAKKE_VALIDATE 0x40000000
  627. #define BENCH_SAKKE 0x80000000
  628. /* Post-Quantum Asymmetric algorithms. */
  629. #define BENCH_FALCON_LEVEL1_SIGN 0x00000001
  630. #define BENCH_FALCON_LEVEL5_SIGN 0x00000002
  631. #define BENCH_DILITHIUM_LEVEL2_SIGN 0x04000000
  632. #define BENCH_DILITHIUM_LEVEL3_SIGN 0x08000000
  633. #define BENCH_DILITHIUM_LEVEL5_SIGN 0x10000000
  634. /* Post-Quantum Asymmetric algorithms. (Part 2) */
  635. #define BENCH_SPHINCS_FAST_LEVEL1_SIGN 0x00000001
  636. #define BENCH_SPHINCS_FAST_LEVEL3_SIGN 0x00000002
  637. #define BENCH_SPHINCS_FAST_LEVEL5_SIGN 0x00000004
  638. #define BENCH_SPHINCS_SMALL_LEVEL1_SIGN 0x00000008
  639. #define BENCH_SPHINCS_SMALL_LEVEL3_SIGN 0x00000010
  640. #define BENCH_SPHINCS_SMALL_LEVEL5_SIGN 0x00000020
  641. /* Post-Quantum Stateful Hash-Based sig algorithms. */
  642. #define BENCH_LMS_HSS 0x00000001
  643. #define BENCH_XMSS_XMSSMT_SHA256 0x00000002
  644. #define BENCH_XMSS_XMSSMT_SHA512 0x00000004
  645. #define BENCH_XMSS_XMSSMT_SHAKE128 0x00000008
  646. #define BENCH_XMSS_XMSSMT_SHAKE256 0x00000010
  647. #ifndef NO_SHA256
  648. #define BENCH_XMSS_XMSSMT BENCH_XMSS_XMSSMT_SHA256
  649. #elif defined(WOLFSSL_SHA512)
  650. #define BENCH_XMSS_XMSSMT BENCH_XMSS_XMSSMT_SHA512
  651. #elif defined(WOLFSSL_SHAKE128)
  652. #define BENCH_XMSS_XMSSMT BENCH_XMSS_XMSSMT_SHAKE128
  653. #elif defined(WOLFSSL_SHAKE256)
  654. #define BENCH_XMSS_XMSSMT BENCH_XMSS_XMSSMT_SHAKE256
  655. #else
  656. #define BENCH_XMSS_XMSSMT 0x00000000
  657. #endif
  658. /* Other */
  659. #define BENCH_RNG 0x00000001
  660. #define BENCH_SCRYPT 0x00000002
  661. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  662. /* Define AES_AUTH_ADD_SZ already here, since it's used in the
  663. * static declaration of `bench_Usage_msg1`. */
  664. #if !defined(AES_AUTH_ADD_SZ) && \
  665. defined(STM32_CRYPTO) && !defined(STM32_AESGCM_PARTIAL) || \
  666. defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  667. /* For STM32 use multiple of 4 to leverage crypto hardware
  668. * Xilinx Versal requires to use multiples of 16 bytes */
  669. #define AES_AUTH_ADD_SZ 16
  670. #endif
  671. #ifndef AES_AUTH_ADD_SZ
  672. #define AES_AUTH_ADD_SZ 13
  673. #endif
  674. #endif
  675. #if (defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)) || \
  676. (defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY))
  677. #define BENCH_PQ_STATEFUL_HBS
  678. #endif
  679. /* Benchmark all compiled in algorithms.
  680. * When 1, ignore other benchmark algorithm values.
  681. * 0, only benchmark algorithm values set.
  682. */
  683. static int bench_all = 1;
  684. /* Cipher algorithms to benchmark. */
  685. static word32 bench_cipher_algs = 0;
  686. /* Digest algorithms to benchmark. */
  687. static word32 bench_digest_algs = 0;
  688. /* MAC algorithms to benchmark. */
  689. static word32 bench_mac_algs = 0;
  690. /* KDF algorithms to benchmark. */
  691. static word32 bench_kdf_algs = 0;
  692. /* Asymmetric algorithms to benchmark. */
  693. static word32 bench_asym_algs = 0;
  694. /* Post-Quantum Asymmetric algorithms to benchmark. */
  695. static word32 bench_pq_asym_algs = 0;
  696. /* Post-Quantum Asymmetric algorithms to benchmark. (Part 2)*/
  697. static word32 bench_pq_asym_algs2 = 0;
  698. /* Other cryptographic algorithms to benchmark. */
  699. static word32 bench_other_algs = 0;
  700. /* Post-Quantum Stateful Hash-Based sig algorithms to benchmark. */
  701. static word32 bench_pq_hash_sig_algs = 0;
  702. #if !defined(WOLFSSL_BENCHMARK_ALL) && !defined(NO_MAIN_DRIVER)
  703. /* The mapping of command line option to bit values. */
  704. typedef struct bench_alg {
  705. /* Command line option string. */
  706. const char* str;
  707. /* Bit values to set. */
  708. word32 val;
  709. } bench_alg;
  710. #ifndef MAIN_NO_ARGS
  711. /* All recognized cipher algorithm choosing command line options. */
  712. static const bench_alg bench_cipher_opt[] = {
  713. { "-cipher", 0xffffffff },
  714. #ifdef HAVE_AES_CBC
  715. { "-aes-cbc", BENCH_AES_CBC },
  716. #endif
  717. #ifdef HAVE_AESGCM
  718. { "-aes-gcm", BENCH_AES_GCM },
  719. #endif
  720. #ifdef WOLFSSL_AES_DIRECT
  721. { "-aes-ecb", BENCH_AES_ECB },
  722. #endif
  723. #ifdef WOLFSSL_AES_XTS
  724. { "-aes-xts", BENCH_AES_XTS },
  725. #endif
  726. #ifdef WOLFSSL_AES_CFB
  727. { "-aes-cfb", BENCH_AES_CFB },
  728. #endif
  729. #ifdef WOLFSSL_AES_OFB
  730. { "-aes-ofb", BENCH_AES_OFB },
  731. #endif
  732. #ifdef WOLFSSL_AES_COUNTER
  733. { "-aes-ctr", BENCH_AES_CTR },
  734. #endif
  735. #ifdef HAVE_AESCCM
  736. { "-aes-ccm", BENCH_AES_CCM },
  737. #endif
  738. #ifdef WOLFSSL_AES_SIV
  739. { "-aes-siv", BENCH_AES_SIV },
  740. #endif
  741. #ifdef HAVE_CAMELLIA
  742. { "-camellia", BENCH_CAMELLIA },
  743. #endif
  744. #ifndef NO_RC4
  745. { "-arc4", BENCH_ARC4 },
  746. #endif
  747. #ifdef HAVE_CHACHA
  748. { "-chacha20", BENCH_CHACHA20 },
  749. #endif
  750. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  751. { "-chacha20-poly1305", BENCH_CHACHA20_POLY1305 },
  752. #endif
  753. #ifdef WOLFSSL_SM4_CBC
  754. { "-sm4-cbc", BENCH_SM4_CBC },
  755. #endif
  756. #ifdef WOLFSSL_SM4_GCM
  757. { "-sm4-gcm", BENCH_SM4_GCM },
  758. #endif
  759. #ifdef WOLFSSL_SM4_CCM
  760. { "-sm4-ccm", BENCH_SM4_CCM },
  761. #endif
  762. #ifdef WOLFSSL_SM4
  763. { "-sm4", BENCH_SM4 },
  764. #endif
  765. #ifndef NO_DES3
  766. { "-des", BENCH_DES },
  767. #endif
  768. { NULL, 0 }
  769. };
  770. /* All recognized digest algorithm choosing command line options. */
  771. static const bench_alg bench_digest_opt[] = {
  772. { "-digest", 0xffffffff },
  773. #ifndef NO_MD5
  774. { "-md5", BENCH_MD5 },
  775. #endif
  776. #ifdef HAVE_POLY1305
  777. { "-poly1305", BENCH_POLY1305 },
  778. #endif
  779. #ifndef NO_SHA
  780. { "-sha", BENCH_SHA },
  781. #endif
  782. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256) || defined(WOLFSSL_SHA384) \
  783. || defined(WOLFSSL_SHA512)
  784. { "-sha2", BENCH_SHA2 },
  785. #endif
  786. #ifdef WOLFSSL_SHA224
  787. { "-sha224", BENCH_SHA224 },
  788. #endif
  789. #ifndef NO_SHA256
  790. { "-sha256", BENCH_SHA256 },
  791. #endif
  792. #ifdef WOLFSSL_SHA384
  793. { "-sha384", BENCH_SHA384 },
  794. #endif
  795. #ifdef WOLFSSL_SHA512
  796. { "-sha512", BENCH_SHA512 },
  797. #endif
  798. #ifdef WOLFSSL_SHA3
  799. { "-sha3", BENCH_SHA3 },
  800. #ifndef WOLFSSL_NOSHA3_224
  801. { "-sha3-224", BENCH_SHA3_224 },
  802. #endif
  803. #ifndef WOLFSSL_NOSHA3_256
  804. { "-sha3-256", BENCH_SHA3_256 },
  805. #endif
  806. #ifndef WOLFSSL_NOSHA3_384
  807. { "-sha3-384", BENCH_SHA3_384 },
  808. #endif
  809. #ifndef WOLFSSL_NOSHA3_512
  810. { "-sha3-512", BENCH_SHA3_512 },
  811. #endif
  812. #if defined(WOLFSSL_SHAKE128) || defined(WOLFSSL_SHAKE256)
  813. { "-shake", BENCH_SHAKE },
  814. #endif
  815. #ifdef WOLFSSL_SHAKE128
  816. { "-shake128", BENCH_SHAKE128 },
  817. #endif
  818. #ifdef WOLFSSL_SHAKE256
  819. { "-shake256", BENCH_SHAKE256 },
  820. #endif
  821. #endif
  822. #ifdef WOLFSSL_SM3
  823. { "-sm3", BENCH_SM3 },
  824. #endif
  825. #ifdef WOLFSSL_RIPEMD
  826. { "-ripemd", BENCH_RIPEMD },
  827. #endif
  828. #ifdef HAVE_BLAKE2
  829. { "-blake2b", BENCH_BLAKE2B },
  830. #endif
  831. #ifdef HAVE_BLAKE2S
  832. { "-blake2s", BENCH_BLAKE2S },
  833. #endif
  834. { NULL, 0 }
  835. };
  836. /* All recognized MAC algorithm choosing command line options. */
  837. static const bench_alg bench_mac_opt[] = {
  838. { "-mac", 0xffffffff },
  839. #ifdef WOLFSSL_CMAC
  840. { "-cmac", BENCH_CMAC },
  841. #endif
  842. #ifndef NO_HMAC
  843. { "-hmac", BENCH_HMAC },
  844. #ifndef NO_MD5
  845. { "-hmac-md5", BENCH_HMAC_MD5 },
  846. #endif
  847. #ifndef NO_SHA
  848. { "-hmac-sha", BENCH_HMAC_SHA },
  849. #endif
  850. #ifdef WOLFSSL_SHA224
  851. { "-hmac-sha224", BENCH_HMAC_SHA224 },
  852. #endif
  853. #ifndef NO_SHA256
  854. { "-hmac-sha256", BENCH_HMAC_SHA256 },
  855. #endif
  856. #ifdef WOLFSSL_SHA384
  857. { "-hmac-sha384", BENCH_HMAC_SHA384 },
  858. #endif
  859. #ifdef WOLFSSL_SHA512
  860. { "-hmac-sha512", BENCH_HMAC_SHA512 },
  861. #endif
  862. #ifndef NO_PWDBASED
  863. { "-pbkdf2", BENCH_PBKDF2 },
  864. #endif
  865. #endif
  866. #ifdef WOLFSSL_SIPHASH
  867. { "-siphash", BENCH_SIPHASH },
  868. #endif
  869. { NULL, 0 }
  870. };
  871. /* All recognized KDF algorithm choosing command line options. */
  872. static const bench_alg bench_kdf_opt[] = {
  873. { "-kdf", 0xffffffff },
  874. #ifdef WC_SRTP_KDF
  875. { "-srtp-kdf", BENCH_SRTP_KDF },
  876. #endif
  877. { NULL, 0 }
  878. };
  879. /* All recognized asymmetric algorithm choosing command line options. */
  880. static const bench_alg bench_asym_opt[] = {
  881. { "-asym", 0xffffffff },
  882. #ifndef NO_RSA
  883. #ifdef WOLFSSL_KEY_GEN
  884. { "-rsa-kg", BENCH_RSA_KEYGEN },
  885. #endif
  886. { "-rsa", BENCH_RSA },
  887. #ifdef WOLFSSL_KEY_GEN
  888. { "-rsa-sz", BENCH_RSA_SZ },
  889. #endif
  890. #endif
  891. #ifndef NO_DH
  892. { "-dh", BENCH_DH },
  893. #endif
  894. #ifdef WOLFSSL_HAVE_KYBER
  895. { "-kyber", BENCH_KYBER },
  896. #endif
  897. #ifdef HAVE_ECC
  898. { "-ecc-kg", BENCH_ECC_MAKEKEY },
  899. { "-ecc", BENCH_ECC },
  900. #ifdef HAVE_ECC_ENCRYPT
  901. { "-ecc-enc", BENCH_ECC_ENCRYPT },
  902. #endif
  903. { "-ecc-all", BENCH_ECC_ALL },
  904. #endif
  905. #ifdef WOLFSSL_SM2
  906. { "-sm2", BENCH_SM2 },
  907. #endif
  908. #ifdef HAVE_CURVE25519
  909. { "-curve25519-kg", BENCH_CURVE25519_KEYGEN },
  910. #ifdef HAVE_CURVE25519_SHARED_SECRET
  911. { "-x25519", BENCH_CURVE25519_KA },
  912. #endif
  913. #endif
  914. #ifdef HAVE_ED25519
  915. { "-ed25519-kg", BENCH_ED25519_KEYGEN },
  916. { "-ed25519", BENCH_ED25519_SIGN },
  917. #endif
  918. #ifdef HAVE_CURVE448
  919. { "-curve448-kg", BENCH_CURVE448_KEYGEN },
  920. #ifdef HAVE_CURVE448_SHARED_SECRET
  921. { "-x448", BENCH_CURVE448_KA },
  922. #endif
  923. #endif
  924. #ifdef HAVE_ED448
  925. { "-ed448-kg", BENCH_ED448_KEYGEN },
  926. { "-ed448", BENCH_ED448_SIGN },
  927. #endif
  928. #ifdef WOLFCRYPT_HAVE_ECCSI
  929. { "-eccsi-kg", BENCH_ECCSI_KEYGEN },
  930. { "-eccsi-pair", BENCH_ECCSI_PAIRGEN },
  931. { "-eccsi-val", BENCH_ECCSI_VALIDATE },
  932. { "-eccsi", BENCH_ECCSI },
  933. #endif
  934. #ifdef WOLFCRYPT_HAVE_SAKKE
  935. { "-sakke-kg", BENCH_SAKKE_KEYGEN },
  936. { "-sakke-rsk", BENCH_SAKKE_RSKGEN },
  937. { "-sakke-val", BENCH_SAKKE_VALIDATE },
  938. { "-sakke", BENCH_SAKKE },
  939. #endif
  940. { NULL, 0 }
  941. };
  942. /* All recognized other cryptographic algorithm choosing command line options.
  943. */
  944. static const bench_alg bench_other_opt[] = {
  945. { "-other", 0xffffffff },
  946. #ifndef WC_NO_RNG
  947. { "-rng", BENCH_RNG },
  948. #endif
  949. #ifdef HAVE_SCRYPT
  950. { "-scrypt", BENCH_SCRYPT },
  951. #endif
  952. { NULL, 0}
  953. };
  954. #endif /* MAIN_NO_ARGS */
  955. #endif /* !WOLFSSL_BENCHMARK_ALL && !NO_MAIN_DRIVER */
  956. #if defined(BENCH_PQ_STATEFUL_HBS)
  957. typedef struct bench_pq_hash_sig_alg {
  958. /* Command line option string. */
  959. const char* str;
  960. /* Bit values to set. */
  961. word32 val;
  962. } bench_pq_hash_sig_alg;
  963. static const bench_pq_hash_sig_alg bench_pq_hash_sig_opt[] = {
  964. { "-pq_hash_sig", 0xffffffff},
  965. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  966. { "-lms_hss", BENCH_LMS_HSS},
  967. #endif
  968. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  969. { "-xmss_xmssmt", BENCH_XMSS_XMSSMT},
  970. #ifdef WC_XMSS_SHA256
  971. { "-xmss_xmssmt_sha256", BENCH_XMSS_XMSSMT_SHA256},
  972. #endif
  973. #ifdef WC_XMSS_SHA512
  974. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 512 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 512
  975. { "-xmss_xmssmt_sha512", BENCH_XMSS_XMSSMT_SHA512},
  976. #endif
  977. #endif
  978. #ifdef WC_XMSS_SHAKE128
  979. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  980. { "-xmss_xmssmt_shake128", BENCH_XMSS_XMSSMT_SHAKE128},
  981. #endif
  982. #endif
  983. #ifdef WC_XMSS_SHAKE256
  984. { "-xmss_xmssmt_shake256", BENCH_XMSS_XMSSMT_SHAKE256},
  985. #endif
  986. #endif
  987. { NULL, 0}
  988. };
  989. #endif /* BENCH_PQ_STATEFUL_HBS */
  990. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  991. /* The post-quantum-specific mapping of command line option to bit values and
  992. * OQS name. */
  993. typedef struct bench_pq_alg {
  994. /* Command line option string. */
  995. const char* str;
  996. /* Bit values to set. */
  997. word32 val;
  998. const char* pqc_name;
  999. } bench_pq_alg;
  1000. /* All recognized post-quantum asymmetric algorithm choosing command line
  1001. * options. */
  1002. static const bench_pq_alg bench_pq_asym_opt[] = {
  1003. { "-pq", 0xffffffff, NULL},
  1004. #ifdef HAVE_LIBOQS
  1005. { "-falcon_level1", BENCH_FALCON_LEVEL1_SIGN,
  1006. OQS_SIG_alg_falcon_512 },
  1007. { "-falcon_level5", BENCH_FALCON_LEVEL5_SIGN,
  1008. OQS_SIG_alg_falcon_1024 },
  1009. { "-dilithium_level2", BENCH_DILITHIUM_LEVEL2_SIGN,
  1010. OQS_SIG_alg_dilithium_2 },
  1011. { "-dilithium_level3", BENCH_DILITHIUM_LEVEL3_SIGN,
  1012. OQS_SIG_alg_dilithium_3 },
  1013. { "-dilithium_level5", BENCH_DILITHIUM_LEVEL5_SIGN,
  1014. OQS_SIG_alg_dilithium_5 },
  1015. #endif /* HAVE_LIBOQS */
  1016. { NULL, 0, NULL }
  1017. };
  1018. #if defined(HAVE_LIBOQS) && defined(HAVE_SPHINCS)
  1019. /* All recognized post-quantum asymmetric algorithm choosing command line
  1020. * options. (Part 2) */
  1021. static const bench_pq_alg bench_pq_asym_opt2[] = {
  1022. { "-pq", 0xffffffff, NULL},
  1023. { "-sphincs_fast_level1", BENCH_SPHINCS_FAST_LEVEL1_SIGN,
  1024. OQS_SIG_alg_sphincs_shake_128f_simple },
  1025. { "-sphincs_fast_level3", BENCH_SPHINCS_FAST_LEVEL3_SIGN,
  1026. OQS_SIG_alg_sphincs_shake_192f_simple },
  1027. { "-sphincs_fast_level5", BENCH_SPHINCS_FAST_LEVEL5_SIGN,
  1028. OQS_SIG_alg_sphincs_shake_256f_simple },
  1029. { "-sphincs_small_level1", BENCH_SPHINCS_SMALL_LEVEL1_SIGN,
  1030. OQS_SIG_alg_sphincs_shake_128s_simple },
  1031. { "-sphincs_small_level3", BENCH_SPHINCS_SMALL_LEVEL3_SIGN,
  1032. OQS_SIG_alg_sphincs_shake_192s_simple },
  1033. { "-sphincs_small_level5", BENCH_SPHINCS_SMALL_LEVEL5_SIGN,
  1034. OQS_SIG_alg_sphincs_shake_256s_simple },
  1035. { NULL, 0, NULL }
  1036. };
  1037. #endif /* HAVE_LIBOQS && HAVE_SPHINCS */
  1038. #endif /* HAVE_PQC */
  1039. #ifdef HAVE_WNR
  1040. const char* wnrConfigFile = "wnr-example.conf";
  1041. #endif
  1042. #if defined(WOLFSSL_MDK_ARM)
  1043. extern XFILE wolfSSL_fopen(const char *fname, const char *mode);
  1044. #define fopen wolfSSL_fopen
  1045. #endif
  1046. static int lng_index = 0;
  1047. #ifndef NO_MAIN_DRIVER
  1048. #ifndef MAIN_NO_ARGS
  1049. static const char* bench_Usage_msg1[][25] = {
  1050. /* 0 English */
  1051. { "-? <num> Help, print this usage\n",
  1052. " 0: English, 1: Japanese\n",
  1053. "-csv Print terminal output in csv format\n",
  1054. "-base10 Display bytes as power of 10 (eg 1 kB = 1000 Bytes)\n",
  1055. "-no_aad No additional authentication data passed.\n",
  1056. "-aad_size <num> With <num> bytes of AAD.\n",
  1057. ("-all_aad With AAD length of 0, "
  1058. WC_STRINGIFY(AES_AUTH_ADD_SZ)
  1059. " and\n"
  1060. " (if set via -aad_size) <aad_size> bytes.\n"
  1061. ),
  1062. "-dgst_full Full digest operation performed.\n",
  1063. "-rsa_sign Measure RSA sign/verify instead of encrypt/decrypt.\n",
  1064. "<keySz> -rsa-sz\n Measure RSA <key size> performance.\n",
  1065. "-ffhdhe2048 Measure DH using FFDHE 2048-bit parameters.\n",
  1066. "-ffhdhe3072 Measure DH using FFDHE 3072-bit parameters.\n",
  1067. "-p256 Measure ECC using P-256 curve.\n",
  1068. "-p384 Measure ECC using P-384 curve.\n",
  1069. "-p521 Measure ECC using P-521 curve.\n",
  1070. "-ecc-all Bench all enabled ECC curves.\n",
  1071. "-<alg> Algorithm to benchmark. Available algorithms include:\n",
  1072. ("-lng <num> Display benchmark result by specified language.\n"
  1073. " 0: English, 1: Japanese\n"
  1074. ),
  1075. "<num> Size of block in bytes\n",
  1076. ("-blocks <num> Number of blocks. Can be used together with the "
  1077. "'Size of block'\n"
  1078. " option, but must be used after that one.\n"
  1079. ),
  1080. "-threads <num> Number of threads to run\n",
  1081. "-print Show benchmark stats summary\n",
  1082. "-hash_input <file> Input data to use for hash benchmarking\n",
  1083. "-cipher_input <file> Input data to use for cipher benchmarking\n",
  1084. "-min_runs <num> Specify minimum number of operation runs\n"
  1085. },
  1086. #ifndef NO_MULTIBYTE_PRINT
  1087. /* 1 Japanese */
  1088. { "-? <num> ヘルプ, 使い方を表示します。\n",
  1089. " 0: 英語、 1: 日本語\n",
  1090. "-csv csv 形式で端末に出力します。\n",
  1091. "-base10 バイトを10のべき乗で表示します。(例 1 kB = 1000 Bytes)\n",
  1092. "-no_aad 追加の認証データを使用しません.\n",
  1093. "-aad_size <num> TBD.\n",
  1094. "-all_aad TBD.\n",
  1095. "-dgst_full フルの digest 暗号操作を実施します。\n",
  1096. "-rsa_sign 暗号/復号化の代わりに RSA の署名/検証を測定します。\n",
  1097. "<keySz> -rsa-sz\n RSA <key size> の性能を測定します。\n",
  1098. "-ffhdhe2048 Measure DH using FFDHE 2048-bit parameters.\n",
  1099. "-ffhdhe3072 Measure DH using FFDHE 3072-bit parameters.\n",
  1100. "-p256 Measure ECC using P-256 curve.\n",
  1101. "-p384 Measure ECC using P-384 curve.\n",
  1102. "-p521 Measure ECC using P-521 curve.\n",
  1103. "-ecc-all Bench all enabled ECC curves.\n",
  1104. ("-<alg> アルゴリズムのベンチマークを実施します。\n"
  1105. " 利用可能なアルゴリズムは下記を含みます:\n"
  1106. ),
  1107. ("-lng <num> 指定された言語でベンチマーク結果を表示します。\n"
  1108. " 0: 英語、 1: 日本語\n"
  1109. ),
  1110. "<num> ブロックサイズをバイト単位で指定します。\n",
  1111. "-blocks <num> TBD.\n",
  1112. "-threads <num> 実行するスレッド数\n",
  1113. "-print ベンチマーク統計の要約を表示する\n",
  1114. /* TODO: translate below */
  1115. "-hash_input <file> Input data to use for hash benchmarking\n",
  1116. "-cipher_input <file> Input data to use for cipher benchmarking\n",
  1117. "-min_runs <num> Specify minimum number of operation runs\n"
  1118. },
  1119. #endif
  1120. };
  1121. #endif /* MAIN_NO_ARGS */
  1122. #endif
  1123. static const char* bench_result_words1[][4] = {
  1124. { "took",
  1125. #ifdef BENCH_MICROSECOND
  1126. "microseconds"
  1127. #else
  1128. "seconds"
  1129. #endif
  1130. , "Cycles per byte", NULL }, /* 0 English */
  1131. #ifndef NO_MULTIBYTE_PRINT
  1132. { "を" , "秒で処理", "1バイトあたりのサイクル数", NULL }, /* 1 Japanese */
  1133. #endif
  1134. };
  1135. #if !defined(NO_RSA) || \
  1136. defined(HAVE_ECC) || !defined(NO_DH) || defined(HAVE_ECC_ENCRYPT) || \
  1137. defined(HAVE_CURVE25519) || defined(HAVE_CURVE25519_SHARED_SECRET) || \
  1138. defined(HAVE_ED25519) || defined(HAVE_CURVE448) || \
  1139. defined(HAVE_CURVE448_SHARED_SECRET) || defined(HAVE_ED448) || \
  1140. defined(WOLFSSL_HAVE_KYBER)
  1141. static const char* bench_desc_words[][15] = {
  1142. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 */
  1143. {"public", "private", "key gen", "agree" , "sign", "verify", "encrypt", "decrypt", "rsk gen", "encap", "derive", "valid", "pair gen", "decap", NULL}, /* 0 English */
  1144. #ifndef NO_MULTIBYTE_PRINT
  1145. {"公開鍵", "秘密鍵" ,"鍵生成" , "鍵共有" , "署名", "検証" , "暗号化" , "復号化" , "rsk gen", "encap", "derive", "valid", "pair gen", "decap", NULL}, /* 1 Japanese */
  1146. #endif
  1147. };
  1148. #endif
  1149. #ifdef MULTI_VALUE_STATISTICS
  1150. static const char* bench_result_words3[][5] = {
  1151. /* 0 English */
  1152. { "max duration", "min duration" , "mean duration", "sd", NULL },
  1153. /* TODO: Add japenese version */
  1154. { "max duration", "min duration" , "mean duration", "sd", NULL }
  1155. };
  1156. #endif
  1157. #if defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM) && !defined(WOLFSSL_SGX)
  1158. #define HAVE_GET_CYCLES
  1159. static WC_INLINE word64 get_intel_cycles(void);
  1160. static THREAD_LS_T word64 total_cycles;
  1161. #define INIT_CYCLE_COUNTER
  1162. #define BEGIN_INTEL_CYCLES total_cycles = get_intel_cycles();
  1163. #define END_INTEL_CYCLES total_cycles = get_intel_cycles() - total_cycles;
  1164. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  1165. #define SHOW_INTEL_CYCLES(b, n, s) \
  1166. (void)XSNPRINTF((b) + XSTRLEN(b), (n) - XSTRLEN(b), \
  1167. " %s = " FLT_FMT_PREC2 STATS_CLAUSE_SEPARATOR, \
  1168. bench_result_words1[lng_index][2], \
  1169. FLT_FMT_PREC2_ARGS(6, 2, count == 0 ? 0 : \
  1170. (double)total_cycles / ((word64)count*(s))))
  1171. #define SHOW_INTEL_CYCLES_CSV(b, n, s) \
  1172. (void)XSNPRINTF((b) + XSTRLEN(b), (n) - XSTRLEN(b), FLT_FMT_PREC "," \
  1173. STATS_CLAUSE_SEPARATOR, FLT_FMT_PREC_ARGS(6, count == 0 ? 0 : \
  1174. (double)total_cycles / ((word64)count*(s))))
  1175. #elif defined(LINUX_CYCLE_COUNT)
  1176. #include <linux/perf_event.h>
  1177. #include <sys/syscall.h>
  1178. #include <unistd.h>
  1179. static THREAD_LS_T word64 begin_cycles;
  1180. static THREAD_LS_T word64 total_cycles;
  1181. static THREAD_LS_T int cycles = -1;
  1182. static THREAD_LS_T struct perf_event_attr atr;
  1183. #define INIT_CYCLE_COUNTER do { \
  1184. atr.type = PERF_TYPE_HARDWARE; \
  1185. atr.config = PERF_COUNT_HW_CPU_CYCLES; \
  1186. cycles = (int)syscall(__NR_perf_event_open, &atr, 0, -1, -1, 0); \
  1187. } while (0);
  1188. #define BEGIN_INTEL_CYCLES read(cycles, &begin_cycles, sizeof(begin_cycles));
  1189. #define END_INTEL_CYCLES do { \
  1190. read(cycles, &total_cycles, sizeof(total_cycles)); \
  1191. total_cycles = total_cycles - begin_cycles; \
  1192. } while (0);
  1193. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  1194. #define SHOW_INTEL_CYCLES(b, n, s) \
  1195. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), \
  1196. " %s = " FLT_FMT_PREC2 STATS_CLAUSE_SEPARATOR, \
  1197. bench_result_words1[lng_index][2], \
  1198. FLT_FMT_PREC2_ARGS(6, 2, (double)total_cycles / \
  1199. (count*s)))
  1200. #define SHOW_INTEL_CYCLES_CSV(b, n, s) \
  1201. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), FLT_FMT_PREC "," \
  1202. STATS_CLAUSE_SEPARATOR, FLT_FMT_PREC_ARGS(6, (double)total_cycles \
  1203. / (count*s)))
  1204. #elif defined(SYNERGY_CYCLE_COUNT)
  1205. #include "hal_data.h"
  1206. static THREAD_LS_T word64 begin_cycles;
  1207. static THREAD_LS_T word64 total_cycles;
  1208. #define INIT_CYCLE_COUNTER
  1209. #define BEGIN_INTEL_CYCLES begin_cycles = DWT->CYCCNT = 0;
  1210. #define END_INTEL_CYCLES total_cycles = DWT->CYCCNT - begin_cycles;
  1211. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  1212. #define SHOW_INTEL_CYCLES(b, n, s) \
  1213. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), \
  1214. " %s = " FLT_FMT_PREC2 STATS_CLAUSE_SEPARATOR, \
  1215. bench_result_words1[lng_index][2], \
  1216. FLT_FMT_PREC2_ARGS(6, 2, (double)total_cycles / (count*s)))
  1217. #define SHOW_INTEL_CYCLES_CSV(b, n, s) \
  1218. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), FLT_FMT_PREC ",\n", \
  1219. FLT_FMT_PREC_ARGS(6, (double)total_cycles / (count*s)))
  1220. #elif defined(WOLFSSL_ESPIDF)
  1221. /* TAG for ESP_LOGx() */
  1222. static const char* TAG = "wolfssl_benchmark";
  1223. static THREAD_LS_T word64 begin_cycles = 0;
  1224. static THREAD_LS_T word64 begin_cycles_ticks = 0;
  1225. static THREAD_LS_T word64 end_cycles = 0;
  1226. static THREAD_LS_T word64 total_cycles = 0;
  1227. /* the return value, as a global var */
  1228. static THREAD_LS_T word64 _esp_get_cycle_count_ex = 0;
  1229. /* the last value seen, adjusted for an overflow, as a global var */
  1230. static THREAD_LS_T word64 _esp_cpu_count_last = 0;
  1231. static THREAD_LS_T TickType_t last_tickCount = 0; /* last FreeRTOS value */
  1232. /* esp_get_cpu_benchmark_cycles(void):
  1233. *
  1234. * Architecture-independant CPU clock counter.
  1235. * WARNING: the hal UINT xthal_get_ccount() quietly rolls over. */
  1236. static WC_INLINE word64 esp_get_cpu_benchmark_cycles(void);
  1237. /* Some vars for debugging, compare ticks to cycles */
  1238. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1239. static THREAD_LS_T word64 _esp_cpu_timer_last = 0;
  1240. static THREAD_LS_T word64 _esp_cpu_timer_diff = 0;
  1241. static THREAD_LS_T word64 _xthal_get_ccount_exAlt = 0;
  1242. static THREAD_LS_T word64 _xthal_get_ccount_exDiff = 0;
  1243. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  1244. /* The ESP32 (both Xtensa and RISC-V have raw CPU counters). */
  1245. #if ESP_IDF_VERSION_MAJOR >= 5
  1246. /* esp_cpu_set_cycle_count() introduced in ESP-IDF v5 */
  1247. #define HAVE_GET_CYCLES
  1248. #define INIT_CYCLE_COUNTER do { \
  1249. ESP_LOGV(TAG, "INIT_CYCLE_COUNTER"); \
  1250. esp_cpu_set_cycle_count(0); \
  1251. } while (0);
  1252. #else
  1253. #define HAVE_GET_CYCLES
  1254. #define INIT_CYCLE_COUNTER do { \
  1255. ESP_LOGV(TAG, "INIT_CYCLE_COUNTER"); \
  1256. } while (0);
  1257. #endif
  1258. #define BEGIN_ESP_CYCLES do { \
  1259. ESP_LOGV(TAG, "BEGIN_ESP_CYCLES"); \
  1260. begin_cycles = esp_get_cpu_benchmark_cycles(); \
  1261. begin_cycles_ticks = xTaskGetTickCount(); \
  1262. } while (0);
  1263. /* since it rolls over, we have something that will tolerate one */
  1264. #define END_ESP_CYCLES \
  1265. end_cycles = esp_get_cpu_benchmark_cycles(); \
  1266. ESP_LOGV(TAG,"END_ESP_CYCLES %llu - %llu", \
  1267. end_cycles, \
  1268. begin_cycles \
  1269. ); \
  1270. total_cycles = (end_cycles - begin_cycles);
  1271. #define SHOW_ESP_CYCLES(b, n, s) \
  1272. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), \
  1273. " %s = " FLT_FMT_PREC2 "\n", \
  1274. bench_result_words1[lng_index][2], \
  1275. FLT_FMT_PREC2_ARGS(6, 2, (double)total_cycles / (count*s)) \
  1276. )
  1277. #define SHOW_ESP_CYCLES_CSV(b, n, s) \
  1278. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), FLT_FMT_PREC ",\n", \
  1279. FLT_FMT_PREC_ARGS(6, (double)total_cycles / (count*s)))
  1280. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1281. /* 64 bit, unisgned, absolute difference
  1282. * used in CPU cycle counter debug calcs. */
  1283. static uint64_t esp_cycle_abs_diff(uint64_t x, uint64_t y)
  1284. {
  1285. uint64_t ret;
  1286. ret = (x > y) ? (x - y) : (y - x);
  1287. return ret;
  1288. }
  1289. #endif
  1290. /* esp_get_cycle_count_ex() is a single-overflow-tolerant extension to
  1291. ** the Espressif `unsigned xthal_get_ccount()` (Xtensa) or
  1292. ** `esp_cpu_get_cycle_count` (RISC-V) which are known to overflow
  1293. ** at least once during full benchmark tests.
  1294. **
  1295. ** To test timing overflow, add a delay longer than max cycles:
  1296. ** vTaskDelay( (const TickType_t)(configTICK_RATE_HZ * 17 * 5) );
  1297. */
  1298. uint64_t esp_get_cycle_count_ex()
  1299. {
  1300. /* reminder: unsigned long long max = 18,446,744,073,709,551,615 */
  1301. /* unsigned int max = 4,294,967,295 */
  1302. uint64_t thisVal = 0; /* CPU counter, "this current value" as read. */
  1303. uint64_t thisIncrement = 0; /* The adjusted increment amount. */
  1304. uint64_t expected_diff = 0; /* FreeRTOS estimated expected CPU diff.*/
  1305. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1306. uint64_t tickCount = 0; /* Current rtos tick counter. */
  1307. uint64_t tickDiff = 0; /* Tick difference from last check. */
  1308. uint64_t tickBeginDiff = 0; /* Tick difference from beginning. */
  1309. #endif
  1310. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1311. uint64_t thisTimerVal = 0; /* Timer Value as alternate to compare */
  1312. uint64_t diffDiff = 0; /* Difference between CPU & Timer differences:
  1313. * (current - last) */
  1314. #endif
  1315. #if defined(CONFIG_IDF_TARGET_ESP32C2) || \
  1316. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  1317. defined(CONFIG_IDF_TARGET_ESP32C6)
  1318. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1319. ESP_ERROR_CHECK(gptimer_get_raw_count(esp_gptimer, &thisTimerVal));
  1320. thisTimerVal = thisTimerVal * RESOLUTION_SCALE;
  1321. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  1322. thisVal = esp_cpu_get_cycle_count();
  1323. #elif defined(CONFIG_IDF_TARGET_ESP32H2)
  1324. thisVal = esp_cpu_get_cycle_count();
  1325. #else
  1326. /* TODO: Why doesn't esp_cpu_get_cycle_count work for Xtensa?
  1327. * Calling current_time(1) to reset time causes thisVal overflow,
  1328. * on Xtensa, but not on RISC-V architecture. See also, below */
  1329. #if defined(CONFIG_IDF_TARGET_ESP8266) || (ESP_IDF_VERSION_MAJOR < 5)
  1330. #ifndef configCPU_CLOCK_HZ
  1331. /* esp_cpu_get_cycle_count not available in ESP-IDF v4 */
  1332. #define configCPU_CLOCK_HZ \
  1333. (CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ * MILLION_VALUE)
  1334. #endif
  1335. /* There's no CPU counter on the ESP8266 (Tensilica). Using RTOS */
  1336. thisVal = (uint64_t)xTaskGetTickCount() *
  1337. (uint64_t)(configCPU_CLOCK_HZ / CONFIG_FREERTOS_HZ);
  1338. #elif defined(__XTENSA__)
  1339. thisVal = esp_cpu_get_cycle_count();
  1340. #else
  1341. /* Not Tensilica(ESP8266), not Xtensa(ESP32/-S2/-S3, then RISC-V */
  1342. thisVal = xthal_get_ccount(); /* or esp_cpu_get_cycle_count(); */
  1343. #endif
  1344. #endif
  1345. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1346. {
  1347. tickCount = xTaskGetTickCount(); /* Our local FreeRTOS tick count */
  1348. tickDiff = tickCount - last_tickCount; /* ticks since bench start */
  1349. expected_diff = CPU_TICK_CYCLES * tickDiff; /* CPU expected count */
  1350. ESP_LOGV(TAG, "CPU_TICK_CYCLES = %d", (int)CPU_TICK_CYCLES);
  1351. ESP_LOGV(TAG, "tickCount = %llu", tickCount);
  1352. ESP_LOGV(TAG, "last_tickCount = %u", last_tickCount);
  1353. ESP_LOGV(TAG, "tickDiff = %llu", tickDiff);
  1354. ESP_LOGV(TAG, "expected_diff1 = %llu", expected_diff);
  1355. }
  1356. #endif
  1357. /* If either thisVal is smaller than last (overflow), and/or the
  1358. * expected value calculated from FreeRTOS tick difference that would
  1359. * have never fit into an unsigned 32 bit integer anyhow... then we
  1360. * need to adjust thisVal to save. */
  1361. if ( (thisVal < _esp_cpu_count_last) || (expected_diff > UINT_MAX) )
  1362. {
  1363. /* Warning: we assume the return type of esp_cpu_get_cycle_count()
  1364. ** will always be unsigned int (or uint32_t) to add UINT_MAX.
  1365. **
  1366. ** NOTE for long duration between calls with multiple overflows:
  1367. **
  1368. ** WILL NOT BE DETECTED - the return value will be INCORRECT.
  1369. **
  1370. ** At this time no single test overflows. This is currently only a
  1371. ** concern for cumulative counts over multiple tests. As long
  1372. ** as well call xthal_get_ccount_ex() with no more than one
  1373. ** overflow CPU tick count, all will be well.
  1374. */
  1375. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1376. ESP_LOGW(TAG, "Alert: Detected xthal_get_ccount overflow at "
  1377. "(%llu < %llu) adding UINT_MAX = %llu.",
  1378. thisVal, _esp_cpu_count_last, (uint64_t) UINT_MAX);
  1379. #endif
  1380. #if !defined(CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ) && \
  1381. !defined(CONFIG_ESP32_DEFAULT_CPU_FREQ_MHZ)
  1382. #error "CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ not found"
  1383. #endif
  1384. /* double check expected diff calc */
  1385. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1386. expected_diff = (CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ * MILLION_VALUE)
  1387. * tickDiff / configTICK_RATE_HZ;
  1388. ESP_LOGI(TAG, "expected_diff2 = %llu", expected_diff);
  1389. #endif
  1390. if (expected_diff > UINT_MAX) {
  1391. /* The number of cycles expected from FreeRTOS ticks is
  1392. * greater than the maximum size of an unsigned 32-bit
  1393. * integer, meaning multiple overflows occurred. */
  1394. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1395. ESP_LOGW(TAG, "expected_diff > UINT_MAX (%u)", UINT_MAX);
  1396. #endif
  1397. thisVal += expected_diff; /* FreeRTOS calc to our 64 bit val */
  1398. }
  1399. else {
  1400. thisVal += (word64)UINT_MAX; /* add 32 bit max to our 64 bit */
  1401. }
  1402. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1403. {
  1404. tickBeginDiff = tickCount - begin_cycles_ticks;
  1405. ESP_LOGI(TAG, "begin_cycles_ticks = %llu", begin_cycles_ticks);
  1406. ESP_LOGI(TAG, "tickDiff = %llu", tickDiff);
  1407. ESP_LOGI(TAG, "expected_diff = %llu", expected_diff);
  1408. ESP_LOGI(TAG, "tickBeginDiff = %llu", tickBeginDiff);
  1409. ESP_LOGW(TAG, WOLFSSL_ESPIDF_BLANKLINE_MESSAGE);
  1410. }
  1411. #endif
  1412. }
  1413. else {
  1414. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1415. ESP_LOGI(TAG, "thisVal, read CPU = %llu", thisVal);
  1416. #endif
  1417. } /* if thisVal adjustment check */
  1418. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1419. if (thisTimerVal < _esp_cpu_timer_last)
  1420. {
  1421. ESP_LOGW(TAG, "Alert: Detected xthal_get_ccountAlt overflow, "
  1422. "adding %ull", UINT_MAX);
  1423. thisTimerVal += (word64)UINT_MAX;
  1424. }
  1425. /* Check an alternate counter using a timer */
  1426. _esp_cpu_timer_diff = esp_cycle_abs_diff(_esp_cpu_count_last, _esp_cpu_timer_last);
  1427. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  1428. /* Adjust our actual returned value that takes into account overflow,
  1429. * increment 64 bit extended total by this 32 bit differential: */
  1430. thisIncrement = (thisVal - _esp_cpu_count_last);
  1431. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1432. ESP_LOGI(TAG, "thisIncrement = %llu", thisIncrement);
  1433. #endif
  1434. /* Add our adjustment, taking into account overflows (see above) */
  1435. _esp_get_cycle_count_ex += thisIncrement;
  1436. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1437. _xthal_get_ccount_exDiff = esp_cycle_abs_diff(_esp_get_cycle_count_ex, _xthal_get_ccount_exAlt);
  1438. _xthal_get_ccount_exAlt += (thisTimerVal - _esp_cpu_timer_last);
  1439. diffDiff = esp_cycle_abs_diff(_xthal_get_ccount_exDiff, _esp_cpu_timer_diff);
  1440. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  1441. /* all of this took some time, so reset the "last seen" value
  1442. * for the next measurement. */
  1443. #if defined(CONFIG_IDF_TARGET_ESP32C2) || \
  1444. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  1445. defined(CONFIG_IDF_TARGET_ESP32C6)
  1446. {
  1447. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1448. ESP_ERROR_CHECK(gptimer_get_raw_count(esp_gptimer,
  1449. &_esp_cpu_timer_last));
  1450. ESP_LOGI(TAG, "thisVal = %llu", thisVal);
  1451. ESP_LOGI(TAG, "thisTimerVal = %llu", thisTimerVal);
  1452. ESP_LOGI(TAG, "diffDiff = %llu", diffDiff);
  1453. ESP_LOGI(TAG, "_xthal_get_ccount_exDiff = %llu", _xthal_get_ccount_exDiff);
  1454. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  1455. _esp_cpu_count_last = esp_cpu_get_cycle_count();
  1456. ESP_LOGV(TAG, "_xthal_get_ccount_last = %llu", _esp_cpu_count_last);
  1457. }
  1458. #elif defined(CONFIG_IDF_TARGET_ESP32H2)
  1459. _esp_cpu_count_last = esp_cpu_get_cycle_count();
  1460. #else
  1461. /* TODO: Why doesn't esp_cpu_get_cycle_count work for Xtensa
  1462. * when resetting CPU cycle counter? FreeRTOS tick collision?
  1463. * thisVal = esp_cpu_get_cycle_count(); See also, above
  1464. * or thisVal = xthal_get_ccount(); */
  1465. #if defined(CONFIG_IDF_TARGET_ESP8266)
  1466. /* There's no CPU counter on the ESP8266, so we'll estimate
  1467. * cycles based on defined CPU frequency from sdkconfig and
  1468. * the RTOS tick frequency */
  1469. _esp_cpu_count_last = (uint64_t)xTaskGetTickCount() *
  1470. (uint64_t)(configCPU_CLOCK_HZ / CONFIG_FREERTOS_HZ);
  1471. #elif ESP_IDF_VERSION_MAJOR < 5
  1472. _esp_cpu_count_last = xthal_get_ccount();
  1473. #else
  1474. _esp_cpu_count_last = esp_cpu_get_cycle_count();
  1475. #endif
  1476. #endif
  1477. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1478. ESP_LOGI(TAG, "_esp_cpu_count_last = %llu", _esp_cpu_count_last);
  1479. #endif
  1480. /* Return the 64 bit extended total from 32 bit counter. */
  1481. return _esp_get_cycle_count_ex;
  1482. } /* esp_get_cycle_count_ex for esp_get_cpu_benchmark_cycles() */
  1483. /* implement other architecture cycle counters here */
  1484. #else
  1485. /* if we don't know the platform, it is unlikely we can count CPU cycles */
  1486. #undef HAVE_GET_CYCLES
  1487. #define INIT_CYCLE_COUNTER
  1488. #define BEGIN_INTEL_CYCLES
  1489. #define END_INTEL_CYCLES
  1490. #ifdef MULTI_VALUE_STATISTICS
  1491. #define SHOW_INTEL_CYCLES(b, n, s) WC_DO_NOTHING
  1492. #define SHOW_INTEL_CYCLES_CSV(b, n, s) WC_DO_NOTHING
  1493. #else
  1494. #define SHOW_INTEL_CYCLES(b, n, s) b[XSTRLEN(b)] = '\n'
  1495. #define SHOW_INTEL_CYCLES_CSV(b, n, s) b[XSTRLEN(b)] = '\n'
  1496. #endif
  1497. #endif
  1498. /* determine benchmark buffer to use (if NO_FILESYSTEM) */
  1499. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  1500. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  1501. #define USE_CERT_BUFFERS_2048 /* default to 2048 */
  1502. #endif
  1503. #if defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048) || \
  1504. defined(USE_CERT_BUFFERS_3072) || defined(USE_CERT_BUFFERS_4096) || \
  1505. !defined(NO_DH)
  1506. /* include test cert and key buffers for use with NO_FILESYSTEM */
  1507. #include <wolfssl/certs_test.h>
  1508. #endif
  1509. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  1510. #include <wolfssl/wolfcrypt/blake2.h>
  1511. #endif
  1512. #ifdef _MSC_VER
  1513. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  1514. #pragma warning(disable: 4996)
  1515. #endif
  1516. #ifdef WOLFSSL_CURRTIME_REMAP
  1517. #define current_time WOLFSSL_CURRTIME_REMAP
  1518. #else
  1519. double current_time(int reset);
  1520. #endif
  1521. #ifdef LINUX_RUSAGE_UTIME
  1522. static void check_for_excessive_stime(const char *desc,
  1523. const char *desc_extra);
  1524. #endif
  1525. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND) && \
  1526. !defined(HAVE_STACK_SIZE)
  1527. #ifdef __cplusplus
  1528. extern "C" {
  1529. #endif
  1530. WOLFSSL_API int wolfSSL_Debugging_ON(void);
  1531. WOLFSSL_API void wolfSSL_Debugging_OFF(void);
  1532. #ifdef __cplusplus
  1533. } /* extern "C" */
  1534. #endif
  1535. #endif
  1536. #if !defined(WC_NO_RNG) && \
  1537. ((!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) \
  1538. || !defined(NO_DH) || defined(WOLFSSL_KEY_GEN) || defined(HAVE_ECC) \
  1539. || defined(HAVE_CURVE25519) || defined(HAVE_ED25519) \
  1540. || defined(HAVE_CURVE448) || defined(HAVE_ED448) \
  1541. || defined(WOLFSSL_HAVE_KYBER))
  1542. #define HAVE_LOCAL_RNG
  1543. static THREAD_LS_T WC_RNG gRng;
  1544. #define GLOBAL_RNG &gRng
  1545. #else
  1546. #define GLOBAL_RNG NULL
  1547. #endif
  1548. #if defined(HAVE_ED25519) || defined(HAVE_CURVE25519) || \
  1549. defined(HAVE_CURVE448) || defined(HAVE_ED448) || \
  1550. defined(HAVE_ECC) || !defined(NO_DH) || \
  1551. !defined(NO_RSA) || defined(HAVE_SCRYPT) || \
  1552. defined(WOLFSSL_HAVE_KYBER)
  1553. #define BENCH_ASYM
  1554. #endif
  1555. #if defined(BENCH_ASYM)
  1556. #if defined(HAVE_ECC) || !defined(NO_RSA) || !defined(NO_DH) || \
  1557. defined(HAVE_CURVE25519) || defined(HAVE_ED25519) || \
  1558. defined(HAVE_CURVE448) || defined(HAVE_ED448) || \
  1559. defined(WOLFSSL_HAVE_KYBER)
  1560. static const char* bench_result_words2[][5] = {
  1561. #ifdef BENCH_MICROSECOND
  1562. { "ops took", "μsec" , "avg" , "ops/μsec", NULL }, /* 0 English
  1563. for μsec */
  1564. #else
  1565. { "ops took", "sec" , "avg" , "ops/sec", NULL }, /* 0 English */
  1566. #endif
  1567. #ifndef NO_MULTIBYTE_PRINT
  1568. { "回処理を", "秒で実施", "平均", "処理/秒", NULL }, /* 1 Japanese */
  1569. #endif
  1570. };
  1571. #endif
  1572. #endif
  1573. #ifdef WOLFSSL_CAAM
  1574. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  1575. #ifdef WOLFSSL_SECO_CAAM
  1576. #define SECO_MAX_UPDATES 10000
  1577. #define SECO_BENCHMARK_NONCE 0x7777
  1578. #define SECO_KEY_STORE_ID 1
  1579. #endif
  1580. static THREAD_LS_T int devId = WOLFSSL_CAAM_DEVID;
  1581. #else
  1582. #ifdef WC_USE_DEVID
  1583. static THREAD_LS_T int devId = WC_USE_DEVID;
  1584. #else
  1585. static THREAD_LS_T int devId = INVALID_DEVID;
  1586. #endif
  1587. #endif
  1588. /* Asynchronous helper macros */
  1589. #ifdef WC_ENABLE_BENCH_THREADING
  1590. typedef struct ThreadData {
  1591. pthread_t thread_id;
  1592. } ThreadData;
  1593. static ThreadData* g_threadData;
  1594. static volatile int g_threadCount;
  1595. #endif
  1596. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_CAAM) || defined(WC_USE_DEVID)
  1597. #ifndef NO_HW_BENCH
  1598. #define BENCH_DEVID
  1599. #endif
  1600. #ifndef HAVE_RENESAS_SYNC
  1601. #define BENCH_DEVID_GET_NAME(useDeviceID) (useDeviceID) ? "HW" : "SW"
  1602. #else
  1603. #define BENCH_DEVID_GET_NAME(useDeviceID) ""
  1604. #endif
  1605. #else
  1606. #define BENCH_DEVID_GET_NAME(useDeviceID) ""
  1607. #endif
  1608. #ifdef WOLFSSL_ASYNC_CRYPT
  1609. static WOLF_EVENT_QUEUE eventQueue;
  1610. #define BENCH_ASYNC_GET_DEV(obj) (&(obj)->asyncDev)
  1611. #define BENCH_MAX_PENDING (WOLF_ASYNC_MAX_PENDING)
  1612. static int bench_async_check(int* ret, WC_ASYNC_DEV* asyncDev,
  1613. int callAgain, int* times, int limit, int* pending)
  1614. {
  1615. int allowNext = 0;
  1616. /* this state can be set from a different thread */
  1617. WOLF_EVENT_STATE state = asyncDev->event.state;
  1618. /* if algo doesn't require calling again then use this flow */
  1619. if (state == WOLF_EVENT_STATE_DONE) {
  1620. if (callAgain) {
  1621. /* needs called again, so allow it and handle completion in
  1622. * bench_async_handle */
  1623. allowNext = 1;
  1624. }
  1625. else {
  1626. *ret = asyncDev->event.ret;
  1627. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  1628. (*times)++;
  1629. if (*pending > 0) /* to support case where async blocks */
  1630. (*pending)--;
  1631. if ((*times + *pending) < limit)
  1632. allowNext = 1;
  1633. }
  1634. }
  1635. /* if slot is available and we haven't reached limit, start another */
  1636. else if (state == WOLF_EVENT_STATE_READY && (*times + *pending) < limit) {
  1637. allowNext = 1;
  1638. }
  1639. return allowNext;
  1640. }
  1641. static int bench_async_handle(int* ret, WC_ASYNC_DEV* asyncDev,
  1642. int callAgain, int* times, int* pending)
  1643. {
  1644. WOLF_EVENT_STATE state = asyncDev->event.state;
  1645. if (*ret == WC_PENDING_E) {
  1646. if (state == WOLF_EVENT_STATE_DONE) {
  1647. *ret = asyncDev->event.ret;
  1648. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  1649. (*times)++;
  1650. (*pending)--;
  1651. }
  1652. else {
  1653. (*pending)++;
  1654. *ret = wc_AsyncHandle(asyncDev, &eventQueue,
  1655. callAgain ? WC_ASYNC_FLAG_CALL_AGAIN : WC_ASYNC_FLAG_NONE);
  1656. }
  1657. }
  1658. else if (*ret >= 0) {
  1659. *ret = asyncDev->event.ret;
  1660. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  1661. (*times)++;
  1662. if (*pending > 0) /* to support case where async blocks */
  1663. (*pending)--;
  1664. }
  1665. return (*ret >= 0) ? 1 : 0;
  1666. }
  1667. static WC_INLINE int bench_async_poll(int* pending)
  1668. {
  1669. int ret, asyncDone = 0;
  1670. ret = wolfAsync_EventQueuePoll(&eventQueue, NULL, NULL, 0,
  1671. WOLF_POLL_FLAG_CHECK_HW, &asyncDone);
  1672. if (ret != 0) {
  1673. printf("%sAsync poll failed %d\n", err_prefix, ret);
  1674. return ret;
  1675. }
  1676. if (asyncDone == 0) {
  1677. #ifndef WC_NO_ASYNC_THREADING
  1678. /* give time to other threads */
  1679. wc_AsyncThreadYield();
  1680. #endif
  1681. }
  1682. (void)pending;
  1683. return asyncDone;
  1684. }
  1685. #else
  1686. #define BENCH_MAX_PENDING 1
  1687. #define BENCH_ASYNC_GET_DEV(obj) NULL
  1688. static WC_INLINE int bench_async_check(int* ret, void* asyncDev,
  1689. int callAgain, int* times, int limit, int* pending)
  1690. {
  1691. (void)ret;
  1692. (void)asyncDev;
  1693. (void)callAgain;
  1694. (void)times;
  1695. (void)limit;
  1696. (void)pending;
  1697. return 1;
  1698. }
  1699. static WC_INLINE int bench_async_handle(int* ret, void* asyncDev,
  1700. int callAgain, int* times, int* pending)
  1701. {
  1702. (void)asyncDev;
  1703. (void)callAgain;
  1704. (void)pending;
  1705. if (*ret >= 0) {
  1706. /* operation completed */
  1707. (*times)++;
  1708. return 1;
  1709. }
  1710. return 0;
  1711. }
  1712. #define bench_async_poll(p) WC_DO_NOTHING
  1713. #endif /* WOLFSSL_ASYNC_CRYPT */
  1714. /* maximum runtime for each benchmark */
  1715. #ifndef BENCH_MIN_RUNTIME_SEC
  1716. #define BENCH_MIN_RUNTIME_SEC 1.0F
  1717. #endif
  1718. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1719. #define AES_AUTH_TAG_SZ 16
  1720. #define BENCH_CIPHER_ADD AES_AUTH_TAG_SZ
  1721. static word32 aesAuthAddSz = AES_AUTH_ADD_SZ;
  1722. #if !defined(AES_AAD_OPTIONS_DEFAULT)
  1723. #if !defined(NO_MAIN_DRIVER)
  1724. #define AES_AAD_OPTIONS_DEFAULT 0x1U
  1725. #else
  1726. #define AES_AAD_OPTIONS_DEFAULT 0x3U
  1727. #endif
  1728. #endif
  1729. #define AES_AAD_STRING(s) \
  1730. (aesAuthAddSz == 0 ? (s "-no_AAD") : \
  1731. (aesAuthAddSz == AES_AUTH_ADD_SZ ? (s) : (s "-custom")))
  1732. enum en_aad_options {
  1733. AAD_SIZE_DEFAULT = 0x1U,
  1734. AAD_SIZE_ZERO = 0x2U,
  1735. AAD_SIZE_CUSTOM = 0x4U,
  1736. };
  1737. static word32 aes_aad_options = AES_AAD_OPTIONS_DEFAULT;
  1738. static word32 aes_aad_size = 0;
  1739. static void bench_aes_aad_options_wrap(void (*fn)(int), int i)
  1740. {
  1741. word32 aesAuthAddSz_orig = aesAuthAddSz;
  1742. word32 options = aes_aad_options;
  1743. while(options) {
  1744. if (options & AAD_SIZE_DEFAULT) {
  1745. aesAuthAddSz = AES_AUTH_ADD_SZ;
  1746. options &= ~(word32)AAD_SIZE_DEFAULT;
  1747. }
  1748. else if (options & AAD_SIZE_ZERO) {
  1749. aesAuthAddSz = 0;
  1750. options &= ~(word32)AAD_SIZE_ZERO;
  1751. }
  1752. else if (options & AAD_SIZE_CUSTOM) {
  1753. aesAuthAddSz = aes_aad_size;
  1754. options &= ~(word32)AAD_SIZE_CUSTOM;
  1755. }
  1756. fn(i);
  1757. aesAuthAddSz = aesAuthAddSz_orig;
  1758. }
  1759. }
  1760. #endif
  1761. #ifndef BENCH_CIPHER_ADD
  1762. #define BENCH_CIPHER_ADD 0
  1763. #endif
  1764. /* use kB instead of mB for embedded benchmarking */
  1765. #ifdef BENCH_EMBEDDED
  1766. #ifndef BENCH_NTIMES
  1767. #define BENCH_NTIMES 2
  1768. #endif
  1769. #ifndef BENCH_AGREETIMES
  1770. #define BENCH_AGREETIMES 2
  1771. #endif
  1772. enum BenchmarkBounds {
  1773. scryptCnt = 1,
  1774. ntimes = BENCH_NTIMES,
  1775. genTimes = BENCH_MAX_PENDING,
  1776. agreeTimes = BENCH_AGREETIMES
  1777. };
  1778. /* how many kB to test (en/de)cryption */
  1779. #define NUM_BLOCKS 25
  1780. #define BENCH_SIZE (1024uL)
  1781. #else
  1782. #ifndef BENCH_NTIMES
  1783. #define BENCH_NTIMES 100
  1784. #endif
  1785. #ifndef BENCH_AGREETIMES
  1786. #define BENCH_AGREETIMES 100
  1787. #endif
  1788. enum BenchmarkBounds {
  1789. scryptCnt = 10,
  1790. ntimes = BENCH_NTIMES,
  1791. genTimes = BENCH_MAX_PENDING, /* must be at least BENCH_MAX_PENDING */
  1792. agreeTimes = BENCH_AGREETIMES
  1793. };
  1794. /* how many megs to test (en/de)cryption */
  1795. #define NUM_BLOCKS 5
  1796. #define BENCH_SIZE (1024*1024uL)
  1797. #endif
  1798. static int numBlocks = NUM_BLOCKS;
  1799. static word32 bench_size = BENCH_SIZE;
  1800. static int base2 = 1;
  1801. static int digest_stream = 1;
  1802. static int encrypt_only = 0;
  1803. #ifdef MULTI_VALUE_STATISTICS
  1804. static int minimum_runs = 0;
  1805. #endif
  1806. #ifndef NO_RSA
  1807. /* Don't measure RSA sign/verify by default */
  1808. static int rsa_sign_verify = 0;
  1809. #endif
  1810. #ifndef NO_DH
  1811. /* Use the FFDHE parameters */
  1812. static int use_ffdhe = 0;
  1813. #endif
  1814. /* Don't print out in CSV format by default */
  1815. static int csv_format = 0;
  1816. #ifdef WOLFSSL_XILINX_CRYPT_VERSAL
  1817. /* Versal PLM maybe prints an error message to the same console.
  1818. * In order to not mix those outputs up, sleep a little while
  1819. * before erroring out.
  1820. */
  1821. #define SLEEP_ON_ERROR(ret) do{ if (ret != 0) { sleep(1); } }while(0)
  1822. #else
  1823. #define SLEEP_ON_ERROR(ret) do{ /* noop */ }while(0)
  1824. #endif
  1825. /* globals for cipher tests */
  1826. static THREAD_LS_T byte* bench_plain = NULL;
  1827. static THREAD_LS_T byte* bench_cipher = NULL;
  1828. #ifndef NO_FILESYSTEM
  1829. static THREAD_LS_T char* hash_input = NULL;
  1830. static THREAD_LS_T char* cipher_input = NULL;
  1831. #endif
  1832. static const XGEN_ALIGN byte bench_key_buf[] =
  1833. {
  1834. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  1835. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  1836. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67,
  1837. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  1838. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  1839. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff,
  1840. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  1841. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
  1842. };
  1843. static const XGEN_ALIGN byte bench_iv_buf[] =
  1844. {
  1845. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  1846. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1847. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  1848. };
  1849. static THREAD_LS_T byte* bench_key = NULL;
  1850. static THREAD_LS_T byte* bench_iv = NULL;
  1851. #ifdef HAVE_RENESAS_SYNC
  1852. static THREAD_LS_T byte* bench_key1 = NULL;
  1853. static THREAD_LS_T byte* bench_key2 = NULL;
  1854. #endif
  1855. #ifdef WOLFSSL_STATIC_MEMORY
  1856. #ifdef WOLFSSL_STATIC_MEMORY_TEST_SZ
  1857. static byte gBenchMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  1858. #elif defined(BENCH_EMBEDDED)
  1859. static byte gBenchMemory[50000];
  1860. #else
  1861. static byte gBenchMemory[400000];
  1862. #endif
  1863. #endif
  1864. /* This code handles cases with systems where static (non cost) ram variables
  1865. aren't properly initialized with data */
  1866. static void benchmark_static_init(int force)
  1867. {
  1868. static int gBenchStaticInit = 0;
  1869. if (gBenchStaticInit == 0 || force) {
  1870. gBenchStaticInit = 1;
  1871. /* Init static variables */
  1872. numBlocks = NUM_BLOCKS;
  1873. bench_size = BENCH_SIZE;
  1874. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1875. aesAuthAddSz = AES_AUTH_ADD_SZ;
  1876. aes_aad_options = AES_AAD_OPTIONS_DEFAULT;
  1877. aes_aad_size = 0;
  1878. #endif
  1879. base2 = 1;
  1880. digest_stream = 1;
  1881. #ifdef MULTI_VALUE_STATISTICS
  1882. minimum_runs = 0;
  1883. #endif
  1884. bench_all = 1;
  1885. bench_cipher_algs = 0;
  1886. bench_digest_algs = 0;
  1887. bench_mac_algs = 0;
  1888. bench_kdf_algs = 0;
  1889. bench_asym_algs = 0;
  1890. bench_pq_asym_algs = 0;
  1891. bench_other_algs = 0;
  1892. bench_pq_hash_sig_algs = 0;
  1893. csv_format = 0;
  1894. }
  1895. }
  1896. /*****************************************************************************/
  1897. /* Begin Stats Functions */
  1898. /*****************************************************************************/
  1899. typedef enum bench_stat_type {
  1900. BENCH_STAT_ASYM,
  1901. BENCH_STAT_SYM,
  1902. BENCH_STAT_IGNORE,
  1903. } bench_stat_type_t;
  1904. #ifdef WC_BENCH_TRACK_STATS
  1905. static int gPrintStats = 0;
  1906. #ifdef WC_ENABLE_BENCH_THREADING
  1907. static pthread_mutex_t bench_lock = PTHREAD_MUTEX_INITIALIZER;
  1908. #endif
  1909. #ifndef BENCH_MAX_NAME_SZ
  1910. #define BENCH_MAX_NAME_SZ 24
  1911. #endif
  1912. typedef struct bench_stats {
  1913. struct bench_stats* next;
  1914. struct bench_stats* prev;
  1915. char algo[BENCH_MAX_NAME_SZ+1]; /* may not be static, so make copy */
  1916. const char* desc;
  1917. double perfsec;
  1918. int strength;
  1919. int useDeviceID;
  1920. int finishCount;
  1921. bench_stat_type_t type;
  1922. int lastRet;
  1923. const char* perftype;
  1924. } bench_stats_t;
  1925. static bench_stats_t* bench_stats_head;
  1926. static bench_stats_t* bench_stats_tail;
  1927. static bench_stats_t* bench_stats_add(bench_stat_type_t type,
  1928. const char* algo, int strength, const char* desc, int useDeviceID,
  1929. double perfsec, const char* perftype, int ret)
  1930. {
  1931. bench_stats_t* bstat = NULL;
  1932. #ifdef WC_ENABLE_BENCH_THREADING
  1933. /* protect bench_stats_head and bench_stats_tail access */
  1934. THREAD_CHECK_RET(pthread_mutex_lock(&bench_lock));
  1935. #endif
  1936. if (algo != NULL) {
  1937. /* locate existing in list */
  1938. for (bstat = bench_stats_head; bstat != NULL; bstat = bstat->next) {
  1939. /* match based on algo, strength and desc */
  1940. if (XSTRNCMP(bstat->algo, algo, BENCH_MAX_NAME_SZ) == 0 &&
  1941. bstat->strength == strength &&
  1942. bstat->desc == desc &&
  1943. bstat->useDeviceID == useDeviceID) {
  1944. break;
  1945. }
  1946. }
  1947. }
  1948. if (bstat == NULL) {
  1949. /* allocate new and put on list */
  1950. bstat = (bench_stats_t*)XMALLOC(sizeof(bench_stats_t), NULL,
  1951. DYNAMIC_TYPE_INFO);
  1952. if (bstat) {
  1953. XMEMSET(bstat, 0, sizeof(bench_stats_t));
  1954. /* add to list */
  1955. bstat->next = NULL;
  1956. if (bench_stats_tail == NULL) {
  1957. bench_stats_head = bstat;
  1958. }
  1959. else {
  1960. bench_stats_tail->next = bstat;
  1961. bstat->prev = bench_stats_tail;
  1962. }
  1963. bench_stats_tail = bstat; /* add to the end either way */
  1964. }
  1965. }
  1966. if (bstat) {
  1967. bstat->type = type;
  1968. if (algo != NULL)
  1969. XSTRNCPY(bstat->algo, algo, BENCH_MAX_NAME_SZ);
  1970. bstat->strength = strength;
  1971. bstat->desc = desc;
  1972. bstat->useDeviceID = useDeviceID;
  1973. bstat->perfsec += perfsec;
  1974. bstat->finishCount++;
  1975. bstat->perftype = perftype;
  1976. if (bstat->lastRet > ret)
  1977. bstat->lastRet = ret; /* track last error */
  1978. }
  1979. #ifdef WC_ENABLE_BENCH_THREADING
  1980. THREAD_CHECK_RET(pthread_mutex_unlock(&bench_lock));
  1981. #endif
  1982. return bstat;
  1983. }
  1984. void bench_stats_print(void)
  1985. {
  1986. bench_stats_t* bstat;
  1987. int digits;
  1988. #ifdef WC_ENABLE_BENCH_THREADING
  1989. /* protect bench_stats_head and bench_stats_tail access */
  1990. THREAD_CHECK_RET(pthread_mutex_lock(&bench_lock));
  1991. #endif
  1992. #ifdef BENCH_MICROSECOND
  1993. digits = 5;
  1994. #else
  1995. digits = 3;
  1996. #endif
  1997. for (bstat = bench_stats_head; bstat != NULL; ) {
  1998. if (bstat->type == BENCH_STAT_SYM) {
  1999. printf("%-16s%s " FLT_FMT_PREC2 " %s/" WOLFSSL_FIXED_TIME_UNIT
  2000. "\n", bstat->desc,
  2001. BENCH_DEVID_GET_NAME(bstat->useDeviceID),
  2002. FLT_FMT_PREC2_ARGS(8, digits, bstat->perfsec),
  2003. base2 ? "MB" : "mB");
  2004. }
  2005. else {
  2006. printf("%-5s %4d %-9s %s " FLT_FMT_PREC " ops/"
  2007. WOLFSSL_FIXED_TIME_UNIT "ec\n",
  2008. bstat->algo, bstat->strength, bstat->desc,
  2009. BENCH_DEVID_GET_NAME(bstat->useDeviceID),
  2010. FLT_FMT_PREC_ARGS(digits, bstat->perfsec));
  2011. }
  2012. bstat = bstat->next;
  2013. }
  2014. #ifdef WC_ENABLE_BENCH_THREADING
  2015. THREAD_CHECK_RET(pthread_mutex_unlock(&bench_lock));
  2016. #endif
  2017. }
  2018. #endif /* WC_BENCH_TRACK_STATS */
  2019. static WC_INLINE void bench_stats_init(void)
  2020. {
  2021. #ifdef WC_BENCH_TRACK_STATS
  2022. bench_stats_head = NULL;
  2023. bench_stats_tail = NULL;
  2024. #endif
  2025. INIT_CYCLE_COUNTER
  2026. }
  2027. static WC_INLINE void bench_stats_start(int* count, double* start)
  2028. {
  2029. *count = 0;
  2030. *start = current_time(1);
  2031. #ifdef WOLFSSL_ESPIDF
  2032. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  2033. ESP_LOGI(TAG, "bench_stats_start total_cycles = %llu, start=" FLT_FMT,
  2034. total_cycles, FLT_FMT_ARGS(*start) );
  2035. #endif
  2036. BEGIN_ESP_CYCLES
  2037. #else
  2038. BEGIN_INTEL_CYCLES
  2039. #endif
  2040. }
  2041. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  2042. #define bench_stats_start(count, start) do { \
  2043. SAVE_VECTOR_REGISTERS(pr_err( \
  2044. "SAVE_VECTOR_REGISTERS failed for benchmark run."); \
  2045. return; ); \
  2046. bench_stats_start(count, start); \
  2047. } while (0)
  2048. #endif
  2049. static WC_INLINE int bench_stats_check(double start)
  2050. {
  2051. int ret = 0;
  2052. double this_current_time;
  2053. this_current_time = current_time(0); /* get the timestamp, no reset */
  2054. #if defined(DEBUG_WOLFSSL_BENCHMARK_TIMING) && defined(WOLFSSL_ESPIDF)
  2055. ESP_LOGV(TAG, "bench_stats_check: Current time %f, start %f",
  2056. this_current_time, start );
  2057. #endif
  2058. ret = ((this_current_time - start) < BENCH_MIN_RUNTIME_SEC
  2059. #ifdef BENCH_MICROSECOND
  2060. * 1000000
  2061. #endif
  2062. );
  2063. return ret;
  2064. }
  2065. /* return text for units and scale the value of blocks as needed */
  2066. static const char* get_blocktype(double* blocks)
  2067. {
  2068. const char* rt;
  2069. #if ( defined(WOLFSSL_BENCHMARK_FIXED_UNITS_G) || \
  2070. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_GB))
  2071. #undef WOLFSSL_FIXED_UNIT
  2072. #define WOLFSSL_FIXED_UNIT "GB"
  2073. *blocks /= (1024UL * 1024UL * 1024UL);
  2074. rt = "GiB";
  2075. #elif (defined(WOLFSSL_BENCHMARK_FIXED_UNITS_M) || \
  2076. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_MB))
  2077. #undef WOLFSSL_FIXED_UNIT
  2078. #define WOLFSSL_FIXED_UNIT "MB"
  2079. *blocks /= (1024UL * 1024UL);
  2080. rt = "MiB";
  2081. #elif (defined(WOLFSSL_BENCHMARK_FIXED_UNITS_K) || \
  2082. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_KB))
  2083. #undef WOLFSSL_FIXED_UNIT
  2084. #define WOLFSSL_FIXED_UNIT "KB"
  2085. *blocks /= 1024;
  2086. rt = "KiB";
  2087. #elif defined (WOLFSSL_BENCHMARK_FIXED_UNITS_B)
  2088. #undef WOLFSSL_FIXED_UNIT
  2089. #define WOLFSSL_FIXED_UNIT "bytes"
  2090. (void)(*blocks); /* no adjustment, just appease compiler for not used */
  2091. rt = "bytes";
  2092. #else
  2093. /* If no user-specified, auto-scale each metric (results vary).
  2094. * Determine if we should show as KB or MB or bytes. No GiB here. */
  2095. if (*blocks > (1024UL * 1024UL)) {
  2096. *blocks /= (1024UL * 1024UL);
  2097. rt = "MiB";
  2098. }
  2099. else if (*blocks > 1024) {
  2100. *blocks /= 1024;
  2101. rt = "KiB";
  2102. }
  2103. else {
  2104. rt = "bytes";
  2105. }
  2106. #endif
  2107. return rt;
  2108. }
  2109. /* return text for units and scale the value of blocks as needed for base2 */
  2110. static const char* get_blocktype_base10(double* blocks)
  2111. {
  2112. const char* rt;
  2113. #if ( defined(WOLFSSL_BENCHMARK_FIXED_UNITS_G) || \
  2114. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_GB))
  2115. *blocks /= (1000UL * 1000UL * 1000UL);
  2116. rt = "GB";
  2117. #elif (defined(WOLFSSL_BENCHMARK_FIXED_UNITS_M) || \
  2118. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_MB))
  2119. *blocks /= (1000UL * 1000UL);
  2120. rt = "MB";
  2121. #elif (defined(WOLFSSL_BENCHMARK_FIXED_UNITS_K) || \
  2122. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_KB))
  2123. *blocks /= (1000UL);
  2124. rt = "KB";
  2125. #elif defined (WOLFSSL_BENCHMARK_FIXED_UNITS_B)
  2126. (void)(*blocks); /* no adjustment, just appease compiler */
  2127. rt = "bytes";
  2128. #else
  2129. /* If not user-specified, auto-scale each metric (results vary).
  2130. * Determine if we should show as KB or MB or bytes */
  2131. if (*blocks > (1000UL * 1000UL)) {
  2132. *blocks /= (1000UL * 1000UL);
  2133. rt = "MB";
  2134. }
  2135. else if (*blocks > 1000) {
  2136. *blocks /= 1000; /* make KB */
  2137. rt = "KB";
  2138. }
  2139. else {
  2140. rt = "bytes";
  2141. }
  2142. #endif
  2143. return rt;
  2144. }
  2145. #ifdef MULTI_VALUE_STATISTICS
  2146. static double wc_sqroot(double in)
  2147. {
  2148. /* do 32 iterations for the sqroot */
  2149. int iter = 32;
  2150. double root = in/3.0;
  2151. if (in < 0.0)
  2152. return -1;
  2153. for (int i=0; i < iter; i++)
  2154. root = (root + in / root) / 2.0;
  2155. return root;
  2156. }
  2157. static void bench_multi_value_stats(double max, double min, double sum,
  2158. double squareSum, int runs)
  2159. {
  2160. double mean = 0;
  2161. double sd = 0;
  2162. char msg[WC_BENCH_MAX_LINE_LEN];
  2163. const char** word = bench_result_words3[lng_index];
  2164. XMEMSET(msg, 0, sizeof(msg));
  2165. mean = sum / runs;
  2166. /* Calculating standard deviation */
  2167. sd = (squareSum / runs) - (mean * mean);
  2168. sd = wc_sqroot(sd);
  2169. if (csv_format == 1) {
  2170. (void)XSNPRINTF(msg, sizeof(msg), FLT_FMT_PREC2 ","
  2171. FLT_FMT_PREC2 "," FLT_FMT_PREC2 "," FLT_FMT_PREC2 ",\n",
  2172. FLT_FMT_PREC2_ARGS(3, 3, max),
  2173. FLT_FMT_PREC2_ARGS(3, 3, min),
  2174. FLT_FMT_PREC2_ARGS(3, 3, mean),
  2175. FLT_FMT_PREC2_ARGS(3, 3, sd));
  2176. }
  2177. else{
  2178. (void)XSNPRINTF(msg, sizeof(msg), ", %s " FLT_FMT_PREC2 " "
  2179. WOLFSSL_FIXED_TIME_UNIT ", %s " FLT_FMT_PREC2 " "
  2180. WOLFSSL_FIXED_TIME_UNIT ", %s " FLT_FMT_PREC2 " "
  2181. WOLFSSL_FIXED_TIME_UNIT ", %s " FLT_FMT_PREC2 " "
  2182. WOLFSSL_FIXED_TIME_UNIT "\n",
  2183. word[0], FLT_FMT_PREC2_ARGS(3, 3, max),
  2184. word[1], FLT_FMT_PREC2_ARGS(3, 3, min),
  2185. word[2], FLT_FMT_PREC2_ARGS(3, 3, mean),
  2186. word[3], FLT_FMT_PREC2_ARGS(3, 3, sd));
  2187. }
  2188. printf("%s", msg);
  2189. #ifndef WOLFSSL_SGX
  2190. XFFLUSH(stdout);
  2191. #endif
  2192. }
  2193. #endif
  2194. /* countSz is number of bytes that 1 count represents. Normally bench_size,
  2195. * except for AES direct that operates on AES_BLOCK_SIZE blocks */
  2196. static void bench_stats_sym_finish(const char* desc, int useDeviceID,
  2197. int count, word32 countSz,
  2198. double start, int ret)
  2199. {
  2200. double total, persec = 0, blocks = (double)count;
  2201. const char* blockType;
  2202. char msg[WC_BENCH_MAX_LINE_LEN];
  2203. const char** word = bench_result_words1[lng_index];
  2204. static int sym_header_printed = 0;
  2205. XMEMSET(msg, 0, sizeof(msg));
  2206. #ifdef WOLFSSL_ESPIDF
  2207. END_ESP_CYCLES
  2208. #else
  2209. END_INTEL_CYCLES
  2210. #endif
  2211. total = current_time(0) - start;
  2212. #if defined(WOLFSSL_ESPIDF) && defined(DEBUG_WOLFSSL_BENCHMARK_TIMING)
  2213. ESP_LOGI(TAG, "%s total_cycles = %llu", desc, total_cycles);
  2214. #endif
  2215. #ifdef LINUX_RUSAGE_UTIME
  2216. check_for_excessive_stime(desc, "");
  2217. #endif
  2218. /* calculate actual bytes */
  2219. blocks *= countSz;
  2220. if (csv_format == 1) {
  2221. /* only print out header once */
  2222. if (sym_header_printed == 0) {
  2223. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2224. /* machine parseable CSV */
  2225. #ifdef HAVE_GET_CYCLES
  2226. printf("%s", "\"sym\",Algorithm,HW/SW,bytes_total,"
  2227. WOLFSSL_FIXED_TIME_UNIT "econds_total,"
  2228. WOLFSSL_FIXED_UNIT "/" WOLFSSL_FIXED_TIME_UNIT
  2229. ",cycles_total,Cycles per byte,");
  2230. #else
  2231. printf("%s", "\"sym\",Algorithm,HW/SW,bytes_total,"
  2232. WOLFSSL_FIXED_TIME_UNIT "econds_total,"
  2233. WOLFSSL_FIXED_UNIT "/" WOLFSSL_FIXED_TIME_UNIT
  2234. ",cycles_total,");
  2235. #endif
  2236. #else
  2237. /* normal CSV */
  2238. #ifdef BENCH_DEVID
  2239. #define BENCH_DEVID_COLUMN_HEADER "HW/SW,"
  2240. #else
  2241. #define BENCH_DEVID_COLUMN_HEADER
  2242. #endif
  2243. #ifdef HAVE_GET_CYCLES
  2244. printf("\n\nSymmetric Ciphers:\n\n");
  2245. printf("Algorithm,"
  2246. BENCH_DEVID_COLUMN_HEADER
  2247. WOLFSSL_FIXED_UNIT "/" WOLFSSL_FIXED_TIME_UNIT
  2248. ",Cycles per byte,");
  2249. #else
  2250. printf("\n\nSymmetric Ciphers:\n\n");
  2251. printf("Algorithm,"
  2252. BENCH_DEVID_COLUMN_HEADER
  2253. WOLFSSL_FIXED_UNIT "/" WOLFSSL_FIXED_TIME_UNIT ",");
  2254. #endif
  2255. #endif
  2256. #ifdef MULTI_VALUE_STATISTICS
  2257. printf("max duration,min duration,mean duration,sd,\n");
  2258. #else
  2259. printf("\n");
  2260. #endif
  2261. sym_header_printed = 1;
  2262. }
  2263. }
  2264. /* determine if we have fixed units, or auto-scale bits or bytes for units.
  2265. * note that the blockType text is assigned AND the blocks param is scaled.
  2266. */
  2267. if (base2) {
  2268. blockType = get_blocktype(&blocks);
  2269. }
  2270. else {
  2271. blockType = get_blocktype_base10(&blocks);
  2272. }
  2273. /* calculate blocks per second */
  2274. if (total > 0) {
  2275. persec = (1 / total) * blocks;
  2276. }
  2277. SLEEP_ON_ERROR(ret);
  2278. /* format and print to terminal */
  2279. if (csv_format == 1) {
  2280. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2281. #ifdef WOLFSSL_ESPIDF
  2282. unsigned long bytes_processed =
  2283. (unsigned long)count * (unsigned long)countSz;
  2284. #else
  2285. word64 bytes_processed = (word64)count * (word64)countSz;
  2286. #endif
  2287. /* note this codepath brings in all the fields from the non-CSV case. */
  2288. #ifdef WOLFSSL_ESPIDF
  2289. #ifdef HAVE_GET_CYCLES
  2290. (void)XSNPRINTF(msg, sizeof(msg),
  2291. "sym,%s,%s,%lu," FLT_FMT "," FLT_FMT ",%lu,", desc,
  2292. BENCH_DEVID_GET_NAME(useDeviceID),
  2293. bytes_processed, FLT_FMT_ARGS(total),
  2294. FLT_FMT_ARGS(persec),
  2295. (long unsigned int) total_cycles);
  2296. #else
  2297. #warning "HAVE_GET_CYCLES should be defined for WOLFSSL_ESPIDF"
  2298. #endif
  2299. /* implement other architectures here */
  2300. #else
  2301. #ifdef HAVE_GET_CYCLES
  2302. (void)XSNPRINTF(msg, sizeof(msg),
  2303. "sym,%s,%s,%lu," FLT_FMT "," FLT_FMT ",%lu,", desc,
  2304. BENCH_DEVID_GET_NAME(useDeviceID),
  2305. bytes_processed, FLT_FMT_ARGS(total),
  2306. FLT_FMT_ARGS(persec), total_cycles);
  2307. #else
  2308. (void)XSNPRINTF(msg, sizeof(msg),
  2309. "sym,%s,%s,%lu," FLT_FMT "," FLT_FMT ",", desc,
  2310. BENCH_DEVID_GET_NAME(useDeviceID),
  2311. bytes_processed, FLT_FMT_ARGS(total),
  2312. FLT_FMT_ARGS(persec));
  2313. #endif
  2314. #endif
  2315. #elif defined(BENCH_DEVID)
  2316. (void)XSNPRINTF(msg, sizeof(msg), "%s,%s," FLT_FMT ",", desc,
  2317. BENCH_DEVID_GET_NAME(useDeviceID), FLT_FMT_ARGS(persec));
  2318. #else
  2319. (void)XSNPRINTF(msg, sizeof(msg), "%s," FLT_FMT ",", desc,
  2320. FLT_FMT_ARGS(persec));
  2321. #endif
  2322. #ifdef WOLFSSL_ESPIDF
  2323. SHOW_ESP_CYCLES_CSV(msg, sizeof(msg), countSz);
  2324. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  2325. ESP_LOGI(TAG, "bench_stats_sym_finish total_cycles = %llu",
  2326. total_cycles);
  2327. #endif
  2328. /* implement other cycle counters here */
  2329. #else
  2330. /* the default cycle counter is Intel */
  2331. SHOW_INTEL_CYCLES_CSV(msg, sizeof(msg), (unsigned)countSz);
  2332. #endif
  2333. } /* if (csv_format == 1) */
  2334. else {
  2335. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2336. #ifdef HAVE_GET_CYCLES
  2337. (void)XSNPRINTF(msg, sizeof(msg),
  2338. "%-24s%s " FLT_FMT_PREC2 " %s %s " FLT_FMT_PREC2 " %s, "
  2339. FLT_FMT_PREC2 " %s/" WOLFSSL_FIXED_TIME_UNIT ", %lu cycles,",
  2340. desc, BENCH_DEVID_GET_NAME(useDeviceID),
  2341. FLT_FMT_PREC2_ARGS(5, 0, blocks), blockType,
  2342. word[0], FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2343. FLT_FMT_PREC2_ARGS(8, 3, persec), blockType,
  2344. (unsigned long) total_cycles);
  2345. #else
  2346. (void)XSNPRINTF(msg, sizeof(msg),
  2347. "%-24s%s " FLT_FMT_PREC2 " %s %s " FLT_FMT_PREC2 " %s, "
  2348. FLT_FMT_PREC2 " %s/" WOLFSSL_FIXED_TIME_UNIT ",",
  2349. desc, BENCH_DEVID_GET_NAME(useDeviceID),
  2350. FLT_FMT_PREC2_ARGS(5, 0, blocks), blockType,
  2351. word[0], FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2352. FLT_FMT_PREC2_ARGS(8, 3, persec), blockType);
  2353. #endif /* HAVE_GET_CYCLES */
  2354. #else
  2355. (void)XSNPRINTF(msg, sizeof(msg),
  2356. "%-24s%s " FLT_FMT_PREC2 " %s %s " FLT_FMT_PREC2 " %s, "
  2357. FLT_FMT_PREC2 " %s/" WOLFSSL_FIXED_TIME_UNIT,
  2358. desc, BENCH_DEVID_GET_NAME(useDeviceID),
  2359. FLT_FMT_PREC2_ARGS(5, 0, blocks), blockType,
  2360. word[0], FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2361. FLT_FMT_PREC2_ARGS(8, 3, persec), blockType);
  2362. #endif
  2363. #ifdef WOLFSSL_ESPIDF
  2364. SHOW_ESP_CYCLES(msg, sizeof(msg), countSz);
  2365. /* implement other architecture cycle counters here */
  2366. #else
  2367. SHOW_INTEL_CYCLES(msg, sizeof(msg), (unsigned)countSz);
  2368. #endif
  2369. } /* not CSV format */
  2370. printf("%s", msg);
  2371. /* show errors */
  2372. if (ret < 0) {
  2373. printf("%sBenchmark %s failed: %d\n", err_prefix, desc, ret);
  2374. }
  2375. #ifndef WOLFSSL_SGX
  2376. XFFLUSH(stdout);
  2377. #endif
  2378. #ifdef WC_BENCH_TRACK_STATS
  2379. /* Add to thread stats */
  2380. bench_stats_add(BENCH_STAT_SYM, desc, 0, desc, useDeviceID, persec,
  2381. blockType, ret);
  2382. #endif
  2383. (void)useDeviceID;
  2384. (void)ret;
  2385. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  2386. RESTORE_VECTOR_REGISTERS();
  2387. #endif
  2388. TEST_SLEEP();
  2389. } /* bench_stats_sym_finish */
  2390. #ifdef BENCH_ASYM
  2391. #if defined(HAVE_ECC) || !defined(NO_RSA) || !defined(NO_DH) || \
  2392. defined(HAVE_CURVE25519) || defined(HAVE_ED25519) || \
  2393. defined(HAVE_CURVE448) || defined(HAVE_ED448) || \
  2394. defined(WOLFSSL_HAVE_KYBER)
  2395. static void bench_stats_asym_finish_ex(const char* algo, int strength,
  2396. const char* desc, const char* desc_extra, int useDeviceID, int count,
  2397. double start, int ret)
  2398. {
  2399. double total, each = 0, opsSec, milliEach;
  2400. const char **word = bench_result_words2[lng_index];
  2401. #ifdef WC_BENCH_TRACK_STATS
  2402. const char* kOpsSec = "Ops/Sec";
  2403. #endif
  2404. char msg[256];
  2405. static int asym_header_printed = 0;
  2406. #ifdef BENCH_MICROSECOND
  2407. const int digits = 5;
  2408. #else
  2409. const int digits = 3;
  2410. #endif
  2411. XMEMSET(msg, 0, sizeof(msg));
  2412. total = current_time(0) - start;
  2413. #ifdef LINUX_RUSAGE_UTIME
  2414. check_for_excessive_stime(desc, desc_extra);
  2415. #endif
  2416. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2417. #ifdef WOLFSSL_ESPIDF
  2418. END_ESP_CYCLES
  2419. #else
  2420. END_INTEL_CYCLES
  2421. #endif
  2422. #endif
  2423. /* some sanity checks on the final numbers */
  2424. if (count > 0) {
  2425. each = total / count; /* per second */
  2426. }
  2427. else {
  2428. count = 0;
  2429. each = 0;
  2430. }
  2431. if (total > 0) {
  2432. opsSec = count / total; /* ops second */
  2433. }
  2434. else {
  2435. opsSec = 0;
  2436. }
  2437. #ifdef BENCH_MICROSECOND
  2438. milliEach = each / 1000; /* milliseconds */
  2439. #else
  2440. milliEach = each * 1000; /* milliseconds */
  2441. #endif
  2442. SLEEP_ON_ERROR(ret);
  2443. #ifdef MULTI_VALUE_STATISTICS /* Print without avg ms */
  2444. (void)milliEach;
  2445. /* format and print to terminal */
  2446. if (csv_format == 1) {
  2447. /* only print out header once */
  2448. if (asym_header_printed == 0) {
  2449. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2450. #ifdef HAVE_GET_CYCLES
  2451. printf("%s", "\"asym\",Algorithm,key size,operation,ops/"
  2452. WOLFSSL_FIXED_TIME_UNIT "ec,ops," WOLFSSL_FIXED_TIME_UNIT
  2453. "ecs,cycles,cycles/op,");
  2454. #else
  2455. printf("%s", "\"asym\",Algorithm,key size,operation,ops/"
  2456. WOLFSSL_FIXED_TIME_UNIT "ec,ops," WOLFSSL_FIXED_TIME_UNIT
  2457. "ecs,");
  2458. #endif
  2459. #else
  2460. printf("\n%sAsymmetric Ciphers:\n\n", info_prefix);
  2461. printf("%sAlgorithm,key size,operation,ops/"
  2462. WOLFSSL_FIXED_TIME_UNIT "ec,", info_prefix);
  2463. #endif
  2464. printf("max duration,min duration,mean duration,sd,\n");
  2465. asym_header_printed = 1;
  2466. }
  2467. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2468. #ifdef HAVE_GET_CYCLES
  2469. (void)XSNPRINTF(msg, sizeof(msg),
  2470. "asym,%s,%d,%s%s," FLT_FMT_PREC ",%d,"
  2471. FLT_FMT ",%lu," FLT_FMT_PREC STATS_CLAUSE_SEPARATOR,
  2472. algo, strength, desc, desc_extra,
  2473. FLT_FMT_PREC_ARGS(digits, opsSec),
  2474. count, FLT_FMT_ARGS(total), (unsigned long)total_cycles,
  2475. FLT_FMT_PREC_ARGS(6,
  2476. (double)total_cycles / (double)count));
  2477. #else
  2478. (void)XSNPRINTF(msg, sizeof(msg),
  2479. "asym,%s,%d,%s%s," FLT_FMT_PREC ",%d,"
  2480. FLT_FMT STATS_CLAUSE_SEPARATOR,
  2481. algo, strength, desc, desc_extra,
  2482. FLT_FMT_PREC_ARGS(digits, opsSec),
  2483. count, FLT_FMT_ARGS(total));
  2484. #endif
  2485. #else
  2486. (void)XSNPRINTF(msg, sizeof(msg), "%s,%d,%s%s,"
  2487. FLT_FMT_PREC "," STATS_CLAUSE_SEPARATOR,
  2488. algo, strength, desc, desc_extra,
  2489. FLT_FMT_PREC_ARGS(digits, opsSec));
  2490. #endif
  2491. } /* if (csv_format == 1) */
  2492. else {
  2493. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2494. #ifdef HAVE_GET_CYCLES
  2495. (void)XSNPRINTF(msg, sizeof(msg),
  2496. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, "
  2497. FLT_FMT_PREC " %s, %lu cycles" STATS_CLAUSE_SEPARATOR,
  2498. algo, strength, desc, desc_extra,
  2499. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2500. FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2501. FLT_FMT_PREC_ARGS(digits, opsSec), word[3],
  2502. (unsigned long)total_cycles);
  2503. #else
  2504. (void)XSNPRINTF(msg, sizeof(msg),
  2505. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, "
  2506. FLT_FMT_PREC " %s" STATS_CLAUSE_SEPARATOR,
  2507. algo, strength, desc, desc_extra,
  2508. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2509. FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2510. FLT_FMT_PREC_ARGS(digits, opsSec), word[3]);
  2511. #endif /* HAVE_GET_CYCLES */
  2512. #else
  2513. (void)XSNPRINTF(msg, sizeof(msg),
  2514. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, "
  2515. FLT_FMT_PREC " %s" STATS_CLAUSE_SEPARATOR,
  2516. algo, strength, desc, desc_extra,
  2517. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2518. FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2519. FLT_FMT_PREC_ARGS(digits, opsSec), word[3]);
  2520. #endif
  2521. }
  2522. #else /* MULTI_VALUE_STATISTICS. Print with avg ms */
  2523. /* format and print to terminal */
  2524. if (csv_format == 1) {
  2525. /* only print out header once */
  2526. if (asym_header_printed == 0) {
  2527. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2528. #ifdef HAVE_GET_CYCLES
  2529. printf("%s", "\"asym\",Algorithm,key size,operation,avg ms,ops/"
  2530. WOLFSSL_FIXED_TIME_UNIT "ec,ops," WOLFSSL_FIXED_TIME_UNIT
  2531. "ecs,cycles,cycles/op,");
  2532. #else
  2533. printf("%s", "\"asym\",Algorithm,key size,operation,avg ms,ops/"
  2534. WOLFSSL_FIXED_TIME_UNIT "ec,ops," WOLFSSL_FIXED_TIME_UNIT
  2535. "ecs,");
  2536. #endif
  2537. #else
  2538. printf("\n%sAsymmetric Ciphers:\n\n", info_prefix);
  2539. printf("%sAlgorithm,key size,operation,avg ms,ops/"
  2540. WOLFSSL_FIXED_TIME_UNIT "ec,", info_prefix);
  2541. #endif
  2542. printf("\n");
  2543. asym_header_printed = 1;
  2544. }
  2545. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2546. #ifdef HAVE_GET_CYCLES
  2547. (void)XSNPRINTF(msg, sizeof(msg),
  2548. "asym,%s,%d,%s%s," FLT_FMT_PREC "," FLT_FMT_PREC ",%d,"
  2549. FLT_FMT ",%lu," FLT_FMT_PREC STATS_CLAUSE_SEPARATOR,
  2550. algo, strength, desc, desc_extra,
  2551. FLT_FMT_PREC_ARGS(3, milliEach),
  2552. FLT_FMT_PREC_ARGS(digits, opsSec),
  2553. count, FLT_FMT_ARGS(total), (unsigned long)total_cycles,
  2554. FLT_FMT_PREC_ARGS(6,
  2555. (double)total_cycles / (double)count));
  2556. #else
  2557. (void)XSNPRINTF(msg, sizeof(msg),
  2558. "asym,%s,%d,%s%s," FLT_FMT_PREC "," FLT_FMT_PREC ",%d,"
  2559. FLT_FMT STATS_CLAUSE_SEPARATOR,
  2560. algo, strength, desc, desc_extra,
  2561. FLT_FMT_PREC_ARGS(3, milliEach),
  2562. FLT_FMT_PREC_ARGS(digits, opsSec),
  2563. count, FLT_FMT_ARGS(total));
  2564. #endif
  2565. #else
  2566. (void)XSNPRINTF(msg, sizeof(msg), "%s,%d,%s%s," FLT_FMT_PREC ","
  2567. FLT_FMT_PREC "," STATS_CLAUSE_SEPARATOR,
  2568. algo, strength, desc, desc_extra,
  2569. FLT_FMT_PREC_ARGS(3, milliEach),
  2570. FLT_FMT_PREC_ARGS(digits, opsSec));
  2571. #endif
  2572. } /* if (csv_format == 1) */
  2573. else {
  2574. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2575. #ifdef HAVE_GET_CYCLES
  2576. (void)XSNPRINTF(msg, sizeof(msg),
  2577. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, %s "
  2578. FLT_FMT_PREC2 " ms, " FLT_FMT_PREC " %s, %lu cycles"
  2579. STATS_CLAUSE_SEPARATOR,
  2580. algo, strength, desc, desc_extra,
  2581. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2582. FLT_FMT_PREC2_ARGS(5, 3, total), word[1], word[2],
  2583. FLT_FMT_PREC2_ARGS(5, 3, milliEach),
  2584. FLT_FMT_PREC_ARGS(digits, opsSec), word[3],
  2585. (unsigned long)total_cycles);
  2586. #else
  2587. (void)XSNPRINTF(msg, sizeof(msg),
  2588. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, %s "
  2589. FLT_FMT_PREC2 " ms, " FLT_FMT_PREC " %s"
  2590. STATS_CLAUSE_SEPARATOR,
  2591. algo, strength, desc, desc_extra,
  2592. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2593. FLT_FMT_PREC2_ARGS(5, 3, total), word[1], word[2],
  2594. FLT_FMT_PREC2_ARGS(5, 3, milliEach),
  2595. FLT_FMT_PREC_ARGS(digits, opsSec), word[3]);
  2596. #endif /* HAVE_GET_CYCLES */
  2597. #else
  2598. (void)XSNPRINTF(msg, sizeof(msg),
  2599. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, %s "
  2600. FLT_FMT_PREC2 " ms, " FLT_FMT_PREC " %s"
  2601. STATS_CLAUSE_SEPARATOR,
  2602. algo, strength, desc, desc_extra,
  2603. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2604. FLT_FMT_PREC2_ARGS(5, 3, total), word[1], word[2],
  2605. FLT_FMT_PREC2_ARGS(5, 3, milliEach),
  2606. FLT_FMT_PREC_ARGS(digits, opsSec), word[3]);
  2607. #endif
  2608. }
  2609. #endif /* MULTI_VALUE_STATISTICS */
  2610. printf("%s", msg);
  2611. /* show errors */
  2612. if (ret < 0) {
  2613. printf("%sBenchmark %s %s %d failed: %d\n",
  2614. err_prefix, algo, desc, strength, ret);
  2615. }
  2616. #ifndef WOLFSSL_SGX
  2617. XFFLUSH(stdout);
  2618. #endif
  2619. #ifdef WC_BENCH_TRACK_STATS
  2620. /* Add to thread stats */
  2621. bench_stats_add(BENCH_STAT_ASYM, algo, strength, desc, useDeviceID, opsSec,
  2622. kOpsSec, ret);
  2623. #endif
  2624. (void)useDeviceID;
  2625. (void)ret;
  2626. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  2627. RESTORE_VECTOR_REGISTERS();
  2628. #endif
  2629. TEST_SLEEP();
  2630. } /* bench_stats_asym_finish_ex */
  2631. static void bench_stats_asym_finish(const char* algo, int strength,
  2632. const char* desc, int useDeviceID, int count, double start, int ret)
  2633. {
  2634. bench_stats_asym_finish_ex(algo, strength, desc, "", useDeviceID, count,
  2635. start, ret);
  2636. }
  2637. #endif
  2638. #endif /* BENCH_ASYM */
  2639. static WC_INLINE void bench_stats_free(void)
  2640. {
  2641. #ifdef WC_BENCH_TRACK_STATS
  2642. bench_stats_t* bstat;
  2643. for (bstat = bench_stats_head; bstat != NULL; ) {
  2644. bench_stats_t* next = bstat->next;
  2645. XFREE(bstat, NULL, DYNAMIC_TYPE_INFO);
  2646. bstat = next;
  2647. }
  2648. bench_stats_head = NULL;
  2649. bench_stats_tail = NULL;
  2650. #endif
  2651. }
  2652. /*****************************************************************************/
  2653. /* End Stats Functions */
  2654. /*****************************************************************************/
  2655. static void* benchmarks_do(void* args)
  2656. {
  2657. long bench_buf_size;
  2658. #ifdef WOLFSSL_ASYNC_CRYPT
  2659. #ifndef WC_NO_ASYNC_THREADING
  2660. ThreadData* threadData = (ThreadData*)args;
  2661. if (wolfAsync_DevOpenThread(&devId, &threadData->thread_id) < 0)
  2662. #else
  2663. if (wolfAsync_DevOpen(&devId) < 0)
  2664. #endif
  2665. {
  2666. printf("%sAsync device open failed\n%sRunning without async\n",
  2667. err_prefix, err_prefix);
  2668. }
  2669. #endif /* WOLFSSL_ASYNC_CRYPT */
  2670. (void)args;
  2671. #ifdef WOLFSSL_ASYNC_CRYPT
  2672. if (wolfEventQueue_Init(&eventQueue) != 0) {
  2673. printf("%sAsync event queue init failure!\n", err_prefix);
  2674. }
  2675. #endif
  2676. #ifdef WOLF_CRYPTO_CB
  2677. #ifdef HAVE_INTEL_QA_SYNC
  2678. devId = wc_CryptoCb_InitIntelQa();
  2679. if (devId == INVALID_DEVID) {
  2680. printf("%sCouldn't init the Intel QA\n", err_prefix);
  2681. }
  2682. #endif
  2683. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  2684. devId = wc_CryptoCb_InitOcteon();
  2685. if (devId == INVALID_DEVID) {
  2686. printf("%sCouldn't get the Octeon device ID\n", err_prefix);
  2687. }
  2688. #endif
  2689. #ifdef HAVE_RENESAS_SYNC
  2690. devId = wc_CryptoCb_CryptInitRenesasCmn(NULL, &guser_PKCbInfo);
  2691. if (devId == INVALID_DEVID) {
  2692. printf("%sCouldn't get the Renesas device ID\n", err_prefix);
  2693. }
  2694. #endif
  2695. #endif
  2696. #if defined(HAVE_LOCAL_RNG)
  2697. {
  2698. int rngRet;
  2699. #ifndef HAVE_FIPS
  2700. rngRet = wc_InitRng_ex(&gRng, HEAP_HINT, devId);
  2701. #else
  2702. rngRet = wc_InitRng(&gRng);
  2703. #endif
  2704. if (rngRet < 0) {
  2705. printf("%sInitRNG failed\n", err_prefix);
  2706. return NULL;
  2707. }
  2708. }
  2709. #endif
  2710. /* setup bench plain, cipher, key and iv globals */
  2711. /* make sure bench buffer is multiple of 16 (AES block size) */
  2712. bench_buf_size = (int)bench_size + BENCH_CIPHER_ADD;
  2713. if (bench_buf_size % 16)
  2714. bench_buf_size += 16 - (bench_buf_size % 16);
  2715. #ifdef WOLFSSL_AFALG_XILINX_AES
  2716. bench_plain = (byte*)aligned_alloc(64, (size_t)bench_buf_size + 16);
  2717. bench_cipher = (byte*)aligned_alloc(64, (size_t)bench_buf_size + 16);
  2718. #else
  2719. bench_plain = (byte*)XMALLOC((size_t)bench_buf_size + 16,
  2720. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2721. bench_cipher = (byte*)XMALLOC((size_t)bench_buf_size + 16,
  2722. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2723. #endif
  2724. if (bench_plain == NULL || bench_cipher == NULL) {
  2725. XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2726. XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2727. bench_plain = bench_cipher = NULL;
  2728. printf("%sBenchmark block buffer alloc failed!\n", err_prefix);
  2729. goto exit;
  2730. }
  2731. #ifndef NO_FILESYSTEM
  2732. if (hash_input) {
  2733. size_t rawSz;
  2734. XFILE file;
  2735. file = XFOPEN(hash_input, "rb");
  2736. if (file == XBADFILE)
  2737. goto exit;
  2738. if (XFSEEK(file, 0, XSEEK_END) != 0) {
  2739. XFCLOSE(file);
  2740. goto exit;
  2741. }
  2742. bench_buf_size = XFTELL(file);
  2743. if(XFSEEK(file, 0, XSEEK_SET) != 0) {
  2744. XFCLOSE(file);
  2745. goto exit;
  2746. }
  2747. XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2748. rawSz = (size_t)bench_buf_size;
  2749. if (bench_buf_size % 16)
  2750. bench_buf_size += 16 - (bench_buf_size % 16);
  2751. bench_size = (word32)bench_buf_size;
  2752. bench_plain = (byte*)XMALLOC((size_t)bench_buf_size + 16*2,
  2753. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2754. if (bench_plain == NULL) {
  2755. XFCLOSE(file);
  2756. goto exit;
  2757. }
  2758. if ((size_t)XFREAD(bench_plain, 1, rawSz, file)
  2759. != rawSz) {
  2760. XFCLOSE(file);
  2761. goto exit;
  2762. }
  2763. XFCLOSE(file);
  2764. }
  2765. else {
  2766. XMEMSET(bench_plain, 0, (size_t)bench_buf_size);
  2767. }
  2768. if (cipher_input) {
  2769. size_t rawSz;
  2770. XFILE file;
  2771. file = XFOPEN(cipher_input, "rb");
  2772. if (file == XBADFILE)
  2773. goto exit;
  2774. if (XFSEEK(file, 0, XSEEK_END) != 0) {
  2775. XFCLOSE(file);
  2776. goto exit;
  2777. }
  2778. bench_buf_size = XFTELL(file);
  2779. if(XFSEEK(file, 0, XSEEK_SET) != 0) {
  2780. XFCLOSE(file);
  2781. goto exit;
  2782. }
  2783. XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2784. rawSz = (size_t)bench_buf_size;
  2785. if (bench_buf_size % 16)
  2786. bench_buf_size += 16 - (bench_buf_size % 16);
  2787. if (bench_size > (word32)bench_buf_size)
  2788. bench_size = (word32)bench_buf_size;
  2789. bench_cipher = (byte*)XMALLOC((size_t)bench_buf_size + 16*2,
  2790. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2791. if (bench_cipher == NULL) {
  2792. XFCLOSE(file);
  2793. goto exit;
  2794. }
  2795. if ((size_t)XFREAD(bench_cipher, 1, rawSz, file)
  2796. != rawSz) {
  2797. XFCLOSE(file);
  2798. goto exit;
  2799. }
  2800. XFCLOSE(file);
  2801. }
  2802. else {
  2803. XMEMSET(bench_cipher, 0, (size_t)bench_buf_size);
  2804. }
  2805. #endif
  2806. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(HAVE_INTEL_QA_SYNC)
  2807. bench_key = (byte*)XMALLOC(sizeof(bench_key_buf),
  2808. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2809. bench_iv = (byte*)XMALLOC(sizeof(bench_iv_buf),
  2810. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2811. if (bench_key == NULL || bench_iv == NULL) {
  2812. XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2813. XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2814. bench_key = bench_iv = NULL;
  2815. printf("%sBenchmark cipher buffer alloc failed!\n", err_prefix);
  2816. goto exit;
  2817. }
  2818. XMEMCPY(bench_key, bench_key_buf, sizeof(bench_key_buf));
  2819. XMEMCPY(bench_iv, bench_iv_buf, sizeof(bench_iv_buf));
  2820. #elif defined(HAVE_RENESAS_SYNC)
  2821. bench_key1 = (byte*)guser_PKCbInfo.wrapped_key_aes128;
  2822. bench_key2 = (byte*)guser_PKCbInfo.wrapped_key_aes256;
  2823. bench_key = (byte*)bench_key_buf;
  2824. bench_iv = (byte*)bench_iv_buf;
  2825. #else
  2826. bench_key = (byte*)bench_key_buf;
  2827. bench_iv = (byte*)bench_iv_buf;
  2828. #endif
  2829. #ifndef WC_NO_RNG
  2830. if (bench_all || (bench_other_algs & BENCH_RNG))
  2831. bench_rng();
  2832. #endif /* WC_NO_RNG */
  2833. #ifndef NO_AES
  2834. #ifdef HAVE_AES_CBC
  2835. if (bench_all || (bench_cipher_algs & BENCH_AES_CBC)) {
  2836. #ifndef NO_SW_BENCH
  2837. bench_aescbc(0);
  2838. #endif
  2839. #if defined(BENCH_DEVID)
  2840. bench_aescbc(1);
  2841. #endif
  2842. }
  2843. #endif
  2844. #ifdef HAVE_AESGCM
  2845. if (bench_all || (bench_cipher_algs & BENCH_AES_GCM)) {
  2846. #ifndef NO_SW_BENCH
  2847. bench_aes_aad_options_wrap(bench_aesgcm, 0);
  2848. #endif
  2849. #if ((defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)) || \
  2850. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  2851. defined(HAVE_RENESAS_SYNC) || defined(WOLFSSL_CAAM)) && \
  2852. !defined(NO_HW_BENCH)
  2853. bench_aes_aad_options_wrap(bench_aesgcm, 1);
  2854. #endif
  2855. #ifndef NO_SW_BENCH
  2856. bench_gmac(0);
  2857. #endif
  2858. #if defined(BENCH_DEVID)
  2859. bench_gmac(1);
  2860. #endif
  2861. }
  2862. #endif
  2863. #ifdef HAVE_AES_ECB
  2864. if (bench_all || (bench_cipher_algs & BENCH_AES_ECB)) {
  2865. #ifndef NO_SW_BENCH
  2866. bench_aesecb(0);
  2867. #endif
  2868. #ifdef BENCH_DEVID
  2869. bench_aesecb(1);
  2870. #endif
  2871. }
  2872. #endif
  2873. #ifdef WOLFSSL_AES_XTS
  2874. if (bench_all || (bench_cipher_algs & BENCH_AES_XTS))
  2875. bench_aesxts();
  2876. #endif
  2877. #ifdef WOLFSSL_AES_CFB
  2878. if (bench_all || (bench_cipher_algs & BENCH_AES_CFB))
  2879. bench_aescfb();
  2880. #endif
  2881. #ifdef WOLFSSL_AES_OFB
  2882. if (bench_all || (bench_cipher_algs & BENCH_AES_OFB))
  2883. bench_aesofb();
  2884. #endif
  2885. #ifdef WOLFSSL_AES_COUNTER
  2886. if (bench_all || (bench_cipher_algs & BENCH_AES_CTR)) {
  2887. bench_aesctr(0);
  2888. #ifdef BENCH_DEVID
  2889. bench_aesctr(1);
  2890. #endif
  2891. }
  2892. #endif
  2893. #ifdef HAVE_AESCCM
  2894. if (bench_all || (bench_cipher_algs & BENCH_AES_CCM)) {
  2895. bench_aes_aad_options_wrap(bench_aesccm, 0);
  2896. #ifdef BENCH_DEVID
  2897. bench_aes_aad_options_wrap(bench_aesccm, 1);
  2898. #endif
  2899. }
  2900. #endif
  2901. #ifdef WOLFSSL_AES_SIV
  2902. if (bench_all || (bench_cipher_algs & BENCH_AES_SIV))
  2903. bench_aessiv();
  2904. #endif
  2905. #endif /* !NO_AES */
  2906. #ifdef HAVE_CAMELLIA
  2907. if (bench_all || (bench_cipher_algs & BENCH_CAMELLIA))
  2908. bench_camellia();
  2909. #endif
  2910. #ifdef WOLFSSL_SM4_CBC
  2911. if (bench_all || (bench_cipher_algs & BENCH_SM4_CBC))
  2912. bench_sm4_cbc();
  2913. #endif
  2914. #ifdef WOLFSSL_SM4_GCM
  2915. if (bench_all || (bench_cipher_algs & BENCH_SM4_GCM))
  2916. bench_sm4_gcm();
  2917. #endif
  2918. #ifdef WOLFSSL_SM4_CCM
  2919. if (bench_all || (bench_cipher_algs & BENCH_SM4_CCM))
  2920. bench_sm4_ccm();
  2921. #endif
  2922. #ifndef NO_RC4
  2923. if (bench_all || (bench_cipher_algs & BENCH_ARC4)) {
  2924. #ifndef NO_SW_BENCH
  2925. bench_arc4(0);
  2926. #endif
  2927. #ifdef BENCH_DEVID
  2928. bench_arc4(1);
  2929. #endif
  2930. }
  2931. #endif
  2932. #ifdef HAVE_CHACHA
  2933. if (bench_all || (bench_cipher_algs & BENCH_CHACHA20))
  2934. bench_chacha();
  2935. #endif
  2936. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  2937. if (bench_all || (bench_cipher_algs & BENCH_CHACHA20_POLY1305))
  2938. bench_chacha20_poly1305_aead();
  2939. #endif
  2940. #ifndef NO_DES3
  2941. if (bench_all || (bench_cipher_algs & BENCH_DES)) {
  2942. #ifndef NO_SW_BENCH
  2943. bench_des(0);
  2944. #endif
  2945. #ifdef BENCH_DEVID
  2946. bench_des(1);
  2947. #endif
  2948. }
  2949. #endif
  2950. #ifndef NO_MD5
  2951. if (bench_all || (bench_digest_algs & BENCH_MD5)) {
  2952. #ifndef NO_SW_BENCH
  2953. bench_md5(0);
  2954. #endif
  2955. #ifdef BENCH_DEVID
  2956. bench_md5(1);
  2957. #endif
  2958. }
  2959. #endif
  2960. #ifdef HAVE_POLY1305
  2961. if (bench_all || (bench_digest_algs & BENCH_POLY1305))
  2962. bench_poly1305();
  2963. #endif
  2964. #ifndef NO_SHA
  2965. if (bench_all || (bench_digest_algs & BENCH_SHA)) {
  2966. #ifndef NO_SW_BENCH
  2967. bench_sha(0);
  2968. #endif
  2969. #ifdef BENCH_DEVID
  2970. bench_sha(1);
  2971. #endif
  2972. }
  2973. #endif
  2974. #ifdef WOLFSSL_SHA224
  2975. if (bench_all || (bench_digest_algs & BENCH_SHA224)) {
  2976. #ifndef NO_SW_BENCH
  2977. bench_sha224(0);
  2978. #endif
  2979. #ifdef BENCH_DEVID
  2980. bench_sha224(1);
  2981. #endif
  2982. }
  2983. #endif
  2984. #ifndef NO_SHA256
  2985. if (bench_all || (bench_digest_algs & BENCH_SHA256)) {
  2986. #ifndef NO_SW_BENCH
  2987. bench_sha256(0);
  2988. #endif
  2989. #ifdef BENCH_DEVID
  2990. bench_sha256(1);
  2991. #endif
  2992. }
  2993. #endif
  2994. #ifdef WOLFSSL_SHA384
  2995. if (bench_all || (bench_digest_algs & BENCH_SHA384)) {
  2996. #ifndef NO_SW_BENCH
  2997. bench_sha384(0);
  2998. #endif
  2999. #ifdef BENCH_DEVID
  3000. bench_sha384(1);
  3001. #endif
  3002. }
  3003. #endif
  3004. #ifdef WOLFSSL_SHA512
  3005. if (bench_all || (bench_digest_algs & BENCH_SHA512)) {
  3006. #ifndef NO_SW_BENCH
  3007. bench_sha512(0);
  3008. #endif
  3009. #ifdef BENCH_DEVID
  3010. bench_sha512(1);
  3011. #endif
  3012. }
  3013. #if !defined(WOLFSSL_NOSHA512_224) && \
  3014. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  3015. if (bench_all || (bench_digest_algs & BENCH_SHA512)) {
  3016. #ifndef NO_SW_BENCH
  3017. bench_sha512_224(0);
  3018. #endif
  3019. #ifdef BENCH_DEVID
  3020. bench_sha512_224(1);
  3021. #endif
  3022. }
  3023. #endif /* WOLFSSL_NOSHA512_224 */
  3024. #if !defined(WOLFSSL_NOSHA512_256) && \
  3025. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  3026. if (bench_all || (bench_digest_algs & BENCH_SHA512)) {
  3027. #ifndef NO_SW_BENCH
  3028. bench_sha512_256(0);
  3029. #endif
  3030. #ifdef BENCH_DEVID
  3031. bench_sha512_256(1);
  3032. #endif
  3033. }
  3034. #endif /* WOLFSSL_NOSHA512_256 */
  3035. #endif /* WOLFSSL_SHA512 */
  3036. #ifdef WOLFSSL_SHA3
  3037. #ifndef WOLFSSL_NOSHA3_224
  3038. if (bench_all || (bench_digest_algs & BENCH_SHA3_224)) {
  3039. #ifndef NO_SW_BENCH
  3040. bench_sha3_224(0);
  3041. #endif
  3042. #ifdef BENCH_DEVID
  3043. bench_sha3_224(1);
  3044. #endif
  3045. }
  3046. #endif /* WOLFSSL_NOSHA3_224 */
  3047. #ifndef WOLFSSL_NOSHA3_256
  3048. if (bench_all || (bench_digest_algs & BENCH_SHA3_256)) {
  3049. #ifndef NO_SW_BENCH
  3050. bench_sha3_256(0);
  3051. #endif
  3052. #ifdef BENCH_DEVID
  3053. bench_sha3_256(1);
  3054. #endif
  3055. }
  3056. #endif /* WOLFSSL_NOSHA3_256 */
  3057. #ifndef WOLFSSL_NOSHA3_384
  3058. if (bench_all || (bench_digest_algs & BENCH_SHA3_384)) {
  3059. #ifndef NO_SW_BENCH
  3060. bench_sha3_384(0);
  3061. #endif
  3062. #ifdef BENCH_DEVID
  3063. bench_sha3_384(1);
  3064. #endif
  3065. }
  3066. #endif /* WOLFSSL_NOSHA3_384 */
  3067. #ifndef WOLFSSL_NOSHA3_512
  3068. if (bench_all || (bench_digest_algs & BENCH_SHA3_512)) {
  3069. #ifndef NO_SW_BENCH
  3070. bench_sha3_512(0);
  3071. #endif
  3072. #ifdef BENCH_DEVID
  3073. bench_sha3_512(1);
  3074. #endif
  3075. }
  3076. #endif /* WOLFSSL_NOSHA3_512 */
  3077. #ifdef WOLFSSL_SHAKE128
  3078. if (bench_all || (bench_digest_algs & BENCH_SHAKE128)) {
  3079. #ifndef NO_SW_BENCH
  3080. bench_shake128(0);
  3081. #endif
  3082. #ifdef BENCH_DEVID
  3083. bench_shake128(1);
  3084. #endif
  3085. }
  3086. #endif /* WOLFSSL_SHAKE128 */
  3087. #ifdef WOLFSSL_SHAKE256
  3088. if (bench_all || (bench_digest_algs & BENCH_SHAKE256)) {
  3089. #ifndef NO_SW_BENCH
  3090. bench_shake256(0);
  3091. #endif
  3092. #ifdef BENCH_DEVID
  3093. bench_shake256(1);
  3094. #endif
  3095. }
  3096. #endif /* WOLFSSL_SHAKE256 */
  3097. #endif
  3098. #ifdef WOLFSSL_SM3
  3099. if (bench_all || (bench_digest_algs & BENCH_SM3)) {
  3100. #ifndef NO_SW_BENCH
  3101. bench_sm3(0);
  3102. #endif
  3103. #ifdef BENCH_DEVID
  3104. bench_sm3(1);
  3105. #endif
  3106. }
  3107. #endif
  3108. #ifdef WOLFSSL_RIPEMD
  3109. if (bench_all || (bench_digest_algs & BENCH_RIPEMD))
  3110. bench_ripemd();
  3111. #endif
  3112. #ifdef HAVE_BLAKE2
  3113. if (bench_all || (bench_digest_algs & BENCH_BLAKE2B))
  3114. bench_blake2b();
  3115. #endif
  3116. #ifdef HAVE_BLAKE2S
  3117. if (bench_all || (bench_digest_algs & BENCH_BLAKE2S))
  3118. bench_blake2s();
  3119. #endif
  3120. #ifdef WOLFSSL_CMAC
  3121. if (bench_all || (bench_mac_algs & BENCH_CMAC)) {
  3122. bench_cmac(0);
  3123. #ifdef BENCH_DEVID
  3124. bench_cmac(1);
  3125. #endif
  3126. }
  3127. #endif
  3128. #ifndef NO_HMAC
  3129. #ifndef NO_MD5
  3130. if (bench_all || (bench_mac_algs & BENCH_HMAC_MD5)) {
  3131. #ifndef NO_SW_BENCH
  3132. bench_hmac_md5(0);
  3133. #endif
  3134. #ifdef BENCH_DEVID
  3135. bench_hmac_md5(1);
  3136. #endif
  3137. }
  3138. #endif
  3139. #ifndef NO_SHA
  3140. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA)) {
  3141. #ifndef NO_SW_BENCH
  3142. bench_hmac_sha(0);
  3143. #endif
  3144. #ifdef BENCH_DEVID
  3145. bench_hmac_sha(1);
  3146. #endif
  3147. }
  3148. #endif
  3149. #ifdef WOLFSSL_SHA224
  3150. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA224)) {
  3151. #ifndef NO_SW_BENCH
  3152. bench_hmac_sha224(0);
  3153. #endif
  3154. #ifdef BENCH_DEVID
  3155. bench_hmac_sha224(1);
  3156. #endif
  3157. }
  3158. #endif
  3159. #ifndef NO_SHA256
  3160. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA256)) {
  3161. #ifndef NO_SW_BENCH
  3162. bench_hmac_sha256(0);
  3163. #endif
  3164. #ifdef BENCH_DEVID
  3165. bench_hmac_sha256(1);
  3166. #endif
  3167. }
  3168. #endif
  3169. #ifdef WOLFSSL_SHA384
  3170. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA384)) {
  3171. #ifndef NO_SW_BENCH
  3172. bench_hmac_sha384(0);
  3173. #endif
  3174. #ifdef BENCH_DEVID
  3175. bench_hmac_sha384(1);
  3176. #endif
  3177. }
  3178. #endif
  3179. #ifdef WOLFSSL_SHA512
  3180. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA512)) {
  3181. #ifndef NO_SW_BENCH
  3182. bench_hmac_sha512(0);
  3183. #endif
  3184. #ifdef BENCH_DEVID
  3185. bench_hmac_sha512(1);
  3186. #endif
  3187. }
  3188. #endif
  3189. #ifndef NO_PWDBASED
  3190. if (bench_all || (bench_mac_algs & BENCH_PBKDF2)) {
  3191. bench_pbkdf2();
  3192. }
  3193. #endif
  3194. #endif /* NO_HMAC */
  3195. #ifdef WOLFSSL_SIPHASH
  3196. if (bench_all || (bench_mac_algs & BENCH_SIPHASH)) {
  3197. bench_siphash();
  3198. }
  3199. #endif
  3200. #ifdef WC_SRTP_KDF
  3201. if (bench_all || (bench_kdf_algs & BENCH_SRTP_KDF)) {
  3202. bench_srtpkdf();
  3203. }
  3204. #endif
  3205. #ifdef HAVE_SCRYPT
  3206. if (bench_all || (bench_other_algs & BENCH_SCRYPT))
  3207. bench_scrypt();
  3208. #endif
  3209. #ifndef NO_RSA
  3210. #ifndef HAVE_RENESAS_SYNC
  3211. #ifdef WOLFSSL_KEY_GEN
  3212. if (bench_all || (bench_asym_algs & BENCH_RSA_KEYGEN)) {
  3213. #ifndef NO_SW_BENCH
  3214. if (((word32)bench_asym_algs == 0xFFFFFFFFU) ||
  3215. (bench_asym_algs & BENCH_RSA_SZ) == 0) {
  3216. bench_rsaKeyGen(0);
  3217. }
  3218. else {
  3219. bench_rsaKeyGen_size(0, bench_size);
  3220. }
  3221. #endif
  3222. #ifdef BENCH_DEVID
  3223. if (bench_asym_algs & BENCH_RSA_SZ) {
  3224. bench_rsaKeyGen_size(1, bench_size);
  3225. }
  3226. else {
  3227. bench_rsaKeyGen(1);
  3228. }
  3229. #endif
  3230. }
  3231. #endif
  3232. if (bench_all || (bench_asym_algs & BENCH_RSA)) {
  3233. #ifndef NO_SW_BENCH
  3234. bench_rsa(0);
  3235. #endif
  3236. #ifdef BENCH_DEVID
  3237. bench_rsa(1);
  3238. #endif
  3239. }
  3240. #ifdef WOLFSSL_KEY_GEN
  3241. if (bench_asym_algs & BENCH_RSA_SZ) {
  3242. #ifndef NO_SW_BENCH
  3243. bench_rsa_key(0, bench_size);
  3244. #endif
  3245. #ifdef BENCH_DEVID
  3246. bench_rsa_key(1, bench_size);
  3247. #endif
  3248. }
  3249. #endif
  3250. #endif
  3251. #endif
  3252. #ifndef NO_DH
  3253. if (bench_all || (bench_asym_algs & BENCH_DH)) {
  3254. #ifndef NO_SW_BENCH
  3255. bench_dh(0);
  3256. #endif
  3257. #ifdef BENCH_DEVID
  3258. bench_dh(1);
  3259. #endif
  3260. }
  3261. #endif
  3262. #ifdef WOLFSSL_HAVE_KYBER
  3263. if (bench_all || (bench_asym_algs & BENCH_KYBER)) {
  3264. #ifdef WOLFSSL_KYBER512
  3265. bench_kyber(KYBER512);
  3266. #endif
  3267. #ifdef WOLFSSL_KYBER768
  3268. bench_kyber(KYBER768);
  3269. #endif
  3270. #ifdef WOLFSSL_KYBER1024
  3271. bench_kyber(KYBER1024);
  3272. #endif
  3273. }
  3274. #endif
  3275. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  3276. if (bench_all || (bench_pq_hash_sig_algs & BENCH_LMS_HSS)) {
  3277. bench_lms();
  3278. }
  3279. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY) */
  3280. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  3281. if (bench_all) {
  3282. bench_pq_hash_sig_algs |= BENCH_XMSS_XMSSMT;
  3283. }
  3284. #ifndef NO_SHA256
  3285. if (bench_pq_hash_sig_algs & BENCH_XMSS_XMSSMT_SHA256) {
  3286. bench_xmss(WC_HASH_TYPE_SHA256);
  3287. }
  3288. #endif
  3289. #ifdef WOLFSSL_SHA512
  3290. if (bench_pq_hash_sig_algs & BENCH_XMSS_XMSSMT_SHA512) {
  3291. bench_xmss(WC_HASH_TYPE_SHA512);
  3292. }
  3293. #endif
  3294. #ifdef WOLFSSL_SHAKE128
  3295. if (bench_pq_hash_sig_algs & BENCH_XMSS_XMSSMT_SHAKE128) {
  3296. bench_xmss(WC_HASH_TYPE_SHAKE128);
  3297. }
  3298. #endif
  3299. #ifdef WOLFSSL_SHAKE256
  3300. if (bench_pq_hash_sig_algs & BENCH_XMSS_XMSSMT_SHAKE256) {
  3301. bench_xmss(WC_HASH_TYPE_SHAKE256);
  3302. }
  3303. #endif
  3304. #endif /* if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY) */
  3305. #ifdef HAVE_ECC
  3306. if (bench_all || (bench_asym_algs & BENCH_ECC_MAKEKEY) ||
  3307. (bench_asym_algs & BENCH_ECC) ||
  3308. (bench_asym_algs & BENCH_ECC_ALL) ||
  3309. (bench_asym_algs & BENCH_ECC_ENCRYPT)) {
  3310. if (bench_asym_algs & BENCH_ECC_ALL) {
  3311. #if defined(HAVE_FIPS) || defined(HAVE_SELFTEST)
  3312. printf("%snot supported in FIPS mode (no ending enum value)\n",
  3313. err_prefix);
  3314. #else
  3315. int curveId = (int)ECC_SECP192R1;
  3316. /* set make key and encrypt */
  3317. bench_asym_algs |= BENCH_ECC_MAKEKEY | BENCH_ECC |
  3318. BENCH_ECC_ENCRYPT;
  3319. if (csv_format != 1) {
  3320. printf("\n%sECC Benchmarks:\n", info_prefix);
  3321. }
  3322. do {
  3323. #ifdef WOLFCRYPT_HAVE_SAKKE
  3324. /* SAKKE is not usable with ECDH/ECDSA. Run separate test. */
  3325. if (curveId == ECC_SAKKE_1) {
  3326. curveId++;
  3327. continue;
  3328. }
  3329. #endif
  3330. if (wc_ecc_get_curve_size_from_id(curveId) !=
  3331. ECC_BAD_ARG_E) {
  3332. bench_ecc_curve(curveId);
  3333. if (csv_format != 1) {
  3334. printf("\n");
  3335. }
  3336. }
  3337. curveId++;
  3338. } while (curveId != (int)ECC_CURVE_MAX);
  3339. #endif
  3340. }
  3341. else if (bench_asym_algs & BENCH_ECC_P256) {
  3342. bench_ecc_curve((int)ECC_SECP256R1);
  3343. }
  3344. else if (bench_asym_algs & BENCH_ECC_P384) {
  3345. bench_ecc_curve((int)ECC_SECP384R1);
  3346. }
  3347. else if (bench_asym_algs & BENCH_ECC_P521) {
  3348. bench_ecc_curve((int)ECC_SECP521R1);
  3349. }
  3350. else {
  3351. #ifndef NO_ECC256
  3352. bench_ecc_curve((int)ECC_SECP256R1);
  3353. #elif defined(HAVE_ECC384)
  3354. bench_ecc_curve((int)ECC_SECP384R1);
  3355. #elif defined(HAVE_ECC521)
  3356. bench_ecc_curve((int)ECC_SECP521R1);
  3357. #endif
  3358. #ifdef HAVE_ECC_BRAINPOOL
  3359. bench_ecc_curve((int)ECC_BRAINPOOLP256R1);
  3360. #endif
  3361. }
  3362. }
  3363. #endif
  3364. #ifdef WOLFSSL_SM2
  3365. if (bench_all || (bench_asym_algs & BENCH_SM2)) {
  3366. bench_sm2(0);
  3367. }
  3368. #endif
  3369. #ifdef HAVE_CURVE25519
  3370. if (bench_all || (bench_asym_algs & BENCH_CURVE25519_KEYGEN)) {
  3371. bench_curve25519KeyGen(0);
  3372. #ifdef BENCH_DEVID
  3373. bench_curve25519KeyGen(1);
  3374. #endif
  3375. }
  3376. #ifdef HAVE_CURVE25519_SHARED_SECRET
  3377. if (bench_all || (bench_asym_algs & BENCH_CURVE25519_KA)) {
  3378. bench_curve25519KeyAgree(0);
  3379. #ifdef BENCH_DEVID
  3380. bench_curve25519KeyAgree(1);
  3381. #endif
  3382. }
  3383. #endif
  3384. #endif
  3385. #ifdef HAVE_ED25519
  3386. if (bench_all || (bench_asym_algs & BENCH_ED25519_KEYGEN))
  3387. bench_ed25519KeyGen();
  3388. if (bench_all || (bench_asym_algs & BENCH_ED25519_SIGN))
  3389. bench_ed25519KeySign();
  3390. #endif
  3391. #ifdef HAVE_CURVE448
  3392. if (bench_all || (bench_asym_algs & BENCH_CURVE448_KEYGEN))
  3393. bench_curve448KeyGen();
  3394. #ifdef HAVE_CURVE448_SHARED_SECRET
  3395. if (bench_all || (bench_asym_algs & BENCH_CURVE448_KA))
  3396. bench_curve448KeyAgree();
  3397. #endif
  3398. #endif
  3399. #ifdef HAVE_ED448
  3400. if (bench_all || (bench_asym_algs & BENCH_ED448_KEYGEN))
  3401. bench_ed448KeyGen();
  3402. if (bench_all || (bench_asym_algs & BENCH_ED448_SIGN))
  3403. bench_ed448KeySign();
  3404. #endif
  3405. #ifdef WOLFCRYPT_HAVE_ECCSI
  3406. #ifdef WOLFCRYPT_ECCSI_KMS
  3407. if (bench_all || (bench_asym_algs & BENCH_ECCSI_KEYGEN)) {
  3408. bench_eccsiKeyGen();
  3409. }
  3410. if (bench_all || (bench_asym_algs & BENCH_ECCSI_PAIRGEN)) {
  3411. bench_eccsiPairGen();
  3412. }
  3413. #endif
  3414. #ifdef WOLFCRYPT_ECCSI_CLIENT
  3415. if (bench_all || (bench_asym_algs & BENCH_ECCSI_VALIDATE)) {
  3416. bench_eccsiValidate();
  3417. }
  3418. if (bench_all || (bench_asym_algs & BENCH_ECCSI)) {
  3419. bench_eccsi();
  3420. }
  3421. #endif
  3422. #endif
  3423. #ifdef WOLFCRYPT_HAVE_SAKKE
  3424. #ifdef WOLFCRYPT_SAKKE_KMS
  3425. if (bench_all || (bench_asym_algs & BENCH_SAKKE_KEYGEN)) {
  3426. bench_sakkeKeyGen();
  3427. }
  3428. if (bench_all || (bench_asym_algs & BENCH_SAKKE_RSKGEN)) {
  3429. bench_sakkeRskGen();
  3430. }
  3431. #endif
  3432. #ifdef WOLFCRYPT_SAKKE_CLIENT
  3433. if (bench_all || (bench_asym_algs & BENCH_SAKKE_VALIDATE)) {
  3434. bench_sakkeValidate();
  3435. }
  3436. if (bench_all || (bench_asym_algs & BENCH_SAKKE)) {
  3437. bench_sakke();
  3438. }
  3439. #endif
  3440. #endif
  3441. #if defined(HAVE_LIBOQS)
  3442. #ifdef HAVE_FALCON
  3443. if (bench_all || (bench_pq_asym_algs & BENCH_FALCON_LEVEL1_SIGN))
  3444. bench_falconKeySign(1);
  3445. if (bench_all || (bench_pq_asym_algs & BENCH_FALCON_LEVEL5_SIGN))
  3446. bench_falconKeySign(5);
  3447. #endif
  3448. #ifdef HAVE_DILITHIUM
  3449. if (bench_all || (bench_pq_asym_algs & BENCH_DILITHIUM_LEVEL2_SIGN))
  3450. bench_dilithiumKeySign(2);
  3451. if (bench_all || (bench_pq_asym_algs & BENCH_DILITHIUM_LEVEL3_SIGN))
  3452. bench_dilithiumKeySign(3);
  3453. if (bench_all || (bench_pq_asym_algs & BENCH_DILITHIUM_LEVEL5_SIGN))
  3454. bench_dilithiumKeySign(5);
  3455. #endif
  3456. #ifdef HAVE_SPHINCS
  3457. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_FAST_LEVEL1_SIGN))
  3458. bench_sphincsKeySign(1, FAST_VARIANT);
  3459. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_FAST_LEVEL3_SIGN))
  3460. bench_sphincsKeySign(3, FAST_VARIANT);
  3461. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_FAST_LEVEL5_SIGN))
  3462. bench_sphincsKeySign(5, FAST_VARIANT);
  3463. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_SMALL_LEVEL1_SIGN))
  3464. bench_sphincsKeySign(1, SMALL_VARIANT);
  3465. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_SMALL_LEVEL3_SIGN))
  3466. bench_sphincsKeySign(3, SMALL_VARIANT);
  3467. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_SMALL_LEVEL5_SIGN))
  3468. bench_sphincsKeySign(5, SMALL_VARIANT);
  3469. #endif
  3470. #endif /* HAVE_LIBOQS */
  3471. exit:
  3472. /* free benchmark buffers */
  3473. XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  3474. XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  3475. #ifdef WOLFSSL_ASYNC_CRYPT
  3476. XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  3477. XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  3478. #endif
  3479. #if defined(HAVE_LOCAL_RNG)
  3480. wc_FreeRng(&gRng);
  3481. #endif
  3482. /* cleanup the thread if fixed point cache is enabled and have thread local */
  3483. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  3484. wc_ecc_fp_free();
  3485. #endif
  3486. (void)bench_cipher_algs;
  3487. (void)bench_digest_algs;
  3488. (void)bench_mac_algs;
  3489. (void)bench_asym_algs;
  3490. (void)bench_other_algs;
  3491. (void)bench_pq_asym_algs;
  3492. (void)bench_pq_asym_algs2;
  3493. return NULL;
  3494. }
  3495. int benchmark_init(void)
  3496. {
  3497. int ret = 0;
  3498. benchmark_static_init(0);
  3499. #ifdef WOLFSSL_STATIC_MEMORY
  3500. ret = wc_LoadStaticMemory(&HEAP_HINT, gBenchMemory,
  3501. sizeof(gBenchMemory), WOLFMEM_GENERAL, 1);
  3502. if (ret != 0) {
  3503. printf("%sunable to load static memory %d\n", err_prefix, ret);
  3504. }
  3505. #endif /* WOLFSSL_STATIC_MEMORY */
  3506. if ((ret = wolfCrypt_Init()) != 0) {
  3507. printf("%swolfCrypt_Init failed %d\n", err_prefix, ret);
  3508. return EXIT_FAILURE;
  3509. }
  3510. #ifdef HAVE_WC_INTROSPECTION
  3511. printf("Math: %s\n", wc_GetMathInfo());
  3512. #endif
  3513. #ifdef WOLFSSL_SECO_CAAM
  3514. if (wc_SECO_OpenHSM(SECO_KEY_STORE_ID,
  3515. SECO_BENCHMARK_NONCE, SECO_MAX_UPDATES, CAAM_KEYSTORE_CREATE)
  3516. != 0) {
  3517. printf("%sunable to open HSM\n", err_prefix);
  3518. wolfCrypt_Cleanup();
  3519. return EXIT_FAILURE;
  3520. }
  3521. #endif
  3522. #ifdef WC_RNG_SEED_CB
  3523. wc_SetSeed_Cb(wc_GenerateSeed);
  3524. #endif
  3525. bench_stats_init();
  3526. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  3527. wolfSSL_Debugging_ON();
  3528. #endif
  3529. printf("%swolfCrypt Benchmark (block bytes %d, min " FLT_FMT_PREC " sec each)\n",
  3530. info_prefix, (int)bench_size, FLT_FMT_PREC_ARGS(1, BENCH_MIN_RUNTIME_SEC));
  3531. #ifndef GENERATE_MACHINE_PARSEABLE_REPORT
  3532. if (csv_format == 1) {
  3533. printf("This format allows you to easily copy "
  3534. "the output to a csv file.");
  3535. }
  3536. #endif
  3537. #ifdef HAVE_WNR
  3538. ret = wc_InitNetRandom(wnrConfigFile, NULL, 5000);
  3539. if (ret != 0) {
  3540. printf("%sWhitewood netRandom config init failed %d\n",
  3541. err_prefix, ret);
  3542. }
  3543. #endif /* HAVE_WNR */
  3544. return ret;
  3545. }
  3546. int benchmark_free(void)
  3547. {
  3548. int ret;
  3549. #ifdef WC_BENCH_TRACK_STATS
  3550. if (gPrintStats || devId != INVALID_DEVID) {
  3551. bench_stats_print();
  3552. }
  3553. #endif
  3554. bench_stats_free();
  3555. #ifdef WOLF_CRYPTO_CB
  3556. #ifdef HAVE_INTEL_QA_SYNC
  3557. wc_CryptoCb_CleanupIntelQa(&devId);
  3558. #endif
  3559. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  3560. wc_CryptoCb_CleanupOcteon(&devId);
  3561. #endif
  3562. #ifdef HAVE_RENESAS_SYNC
  3563. wc_CryptoCb_CleanupRenesasCmn(&devId);
  3564. #endif
  3565. #endif
  3566. #ifdef WOLFSSL_ASYNC_CRYPT
  3567. /* free event queue */
  3568. wolfEventQueue_Free(&eventQueue);
  3569. /* close device */
  3570. wolfAsync_DevClose(&devId);
  3571. #endif
  3572. #ifdef HAVE_WNR
  3573. ret = wc_FreeNetRandom();
  3574. if (ret < 0) {
  3575. printf("%sFailed to free netRandom context %d\n", err_prefix, ret);
  3576. }
  3577. #endif
  3578. #ifdef WOLFSSL_SECO_CAAM
  3579. if (wc_SECO_CloseHSM() != 0) {
  3580. printf("%sError closing down the key store\n", err_prefix);
  3581. }
  3582. #endif
  3583. if ((ret = wolfCrypt_Cleanup()) != 0) {
  3584. printf("%serror %d with wolfCrypt_Cleanup\n", err_prefix, ret);
  3585. }
  3586. return ret;
  3587. }
  3588. #if defined(WC_ENABLE_BENCH_THREADING) && !defined(WOLFSSL_ASYNC_CRYPT)
  3589. static THREAD_RETURN WOLFSSL_THREAD run_bench(void* args)
  3590. {
  3591. benchmark_test(args);
  3592. EXIT_TEST(0);
  3593. }
  3594. static int benchmark_test_threaded(void* args)
  3595. {
  3596. int i;
  3597. printf("%sThreads: %d\n", info_prefix, g_threadCount);
  3598. g_threadData = (ThreadData*)XMALLOC(sizeof(ThreadData) * g_threadCount,
  3599. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3600. if (g_threadData == NULL) {
  3601. printf("%sThread data alloc failed!\n", err_prefix);
  3602. return EXIT_FAILURE;
  3603. }
  3604. for (i = 0; i < g_threadCount; i++) {
  3605. THREAD_CHECK_RET(pthread_create(&g_threadData[i].thread_id,
  3606. NULL, run_bench, args));
  3607. }
  3608. for (i = 0; i < g_threadCount; i++) {
  3609. THREAD_CHECK_RET(pthread_join(g_threadData[i].thread_id, 0));
  3610. }
  3611. printf("\n");
  3612. bench_stats_print();
  3613. return 0;
  3614. }
  3615. #endif
  3616. /* so embedded projects can pull in tests on their own */
  3617. #ifdef HAVE_STACK_SIZE
  3618. THREAD_RETURN WOLFSSL_THREAD benchmark_test(void* args)
  3619. #else
  3620. int benchmark_test(void *args)
  3621. #endif
  3622. {
  3623. int ret;
  3624. (void)args;
  3625. #ifdef HAVE_FIPS
  3626. wolfCrypt_SetCb_fips(myFipsCb);
  3627. #endif
  3628. ret = benchmark_init();
  3629. if (ret != 0)
  3630. EXIT_TEST(ret);
  3631. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  3632. {
  3633. /* See the documentation when turning on WOLFSSL_ASYNC_CRYPT
  3634. **
  3635. ** Chapter Two, Build Options:
  3636. **
  3637. ** https://www.wolfssl.com/documentation/manuals/wolfssl/wolfSSL-Manual.pdf
  3638. **
  3639. ** asynchronous cryptography using hardware based adapters such as
  3640. ** the Intel QuickAssist or Marvell (Cavium) Nitrox V.
  3641. */
  3642. int i;
  3643. if (g_threadCount == 0) {
  3644. #ifdef WC_ASYNC_BENCH_THREAD_COUNT
  3645. g_threadCount = WC_ASYNC_BENCH_THREAD_COUNT;
  3646. #else
  3647. g_threadCount = wc_AsyncGetNumberOfCpus();
  3648. if (g_threadCount > 0) {
  3649. g_threadCount /= 2; /* use physical core count */
  3650. }
  3651. #endif
  3652. }
  3653. if (g_threadCount <= 0) {
  3654. g_threadCount = 1;
  3655. }
  3656. printf("%sCPUs: %d\n", info_prefix, g_threadCount);
  3657. g_threadData = (ThreadData*)XMALLOC(sizeof(ThreadData) * g_threadCount,
  3658. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3659. if (g_threadData == NULL) {
  3660. printf("%sThread data alloc failed!\n", err_prefix);
  3661. EXIT_TEST(EXIT_FAILURE);
  3662. }
  3663. /* Create threads */
  3664. for (i = 0; i < g_threadCount; i++) {
  3665. ret = wc_AsyncThreadCreate(&g_threadData[i].thread_id,
  3666. benchmarks_do, &g_threadData[i]);
  3667. if (ret != 0) {
  3668. printf("%sError creating benchmark thread %d\n", err_prefix, ret);
  3669. EXIT_TEST(EXIT_FAILURE);
  3670. }
  3671. }
  3672. /* Start threads */
  3673. for (i = 0; i < g_threadCount; i++) {
  3674. wc_AsyncThreadJoin(&g_threadData[i].thread_id);
  3675. }
  3676. XFREE(g_threadData, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3677. }
  3678. #else
  3679. benchmarks_do(NULL);
  3680. #endif
  3681. SLEEP_ON_ERROR(1);
  3682. printf("%sBenchmark complete\n", info_prefix);
  3683. ret = benchmark_free();
  3684. EXIT_TEST(ret);
  3685. }
  3686. #ifndef WC_NO_RNG
  3687. void bench_rng(void)
  3688. {
  3689. int ret, i, count;
  3690. double start;
  3691. long pos, len, remain;
  3692. WC_RNG myrng;
  3693. DECLARE_MULTI_VALUE_STATS_VARS()
  3694. #ifndef HAVE_FIPS
  3695. ret = wc_InitRng_ex(&myrng, HEAP_HINT, devId);
  3696. #else
  3697. ret = wc_InitRng(&myrng);
  3698. #endif
  3699. if (ret < 0) {
  3700. printf("InitRNG failed %d\n", ret);
  3701. return;
  3702. }
  3703. bench_stats_start(&count, &start);
  3704. do {
  3705. for (i = 0; i < numBlocks; i++) {
  3706. /* Split request to handle large RNG request */
  3707. pos = 0;
  3708. remain = (int)bench_size;
  3709. while (remain > 0) {
  3710. len = remain;
  3711. if (len > RNG_MAX_BLOCK_LEN)
  3712. len = RNG_MAX_BLOCK_LEN;
  3713. ret = wc_RNG_GenerateBlock(&myrng, &bench_plain[pos],
  3714. (word32)len);
  3715. if (ret < 0)
  3716. goto exit_rng;
  3717. remain -= len;
  3718. pos += len;
  3719. }
  3720. RECORD_MULTI_VALUE_STATS();
  3721. }
  3722. count += i;
  3723. } while (bench_stats_check(start)
  3724. #ifdef MULTI_VALUE_STATISTICS
  3725. || runs < minimum_runs
  3726. #endif
  3727. );
  3728. exit_rng:
  3729. bench_stats_sym_finish("RNG", 0, count, bench_size, start, ret);
  3730. #ifdef MULTI_VALUE_STATISTICS
  3731. bench_multi_value_stats(max, min, sum, squareSum, runs);
  3732. #endif
  3733. wc_FreeRng(&myrng);
  3734. }
  3735. #endif /* WC_NO_RNG */
  3736. #ifndef NO_AES
  3737. #ifdef HAVE_AES_CBC
  3738. static void bench_aescbc_internal(int useDeviceID,
  3739. const byte* key, word32 keySz,
  3740. const byte* iv, const char* encLabel,
  3741. const char* decLabel)
  3742. {
  3743. int ret = 0, i, count = 0, times, pending = 0;
  3744. WC_DECLARE_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  3745. sizeof(Aes), HEAP_HINT);
  3746. double start;
  3747. DECLARE_MULTI_VALUE_STATS_VARS()
  3748. WC_CALLOC_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  3749. sizeof(Aes), HEAP_HINT);
  3750. /* init keys */
  3751. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3752. if ((ret = wc_AesInit(enc[i], HEAP_HINT,
  3753. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  3754. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  3755. goto exit;
  3756. }
  3757. ret = wc_AesSetKey(enc[i], key, keySz, iv, AES_ENCRYPTION);
  3758. if (ret != 0) {
  3759. printf("AesSetKey failed, ret = %d\n", ret);
  3760. goto exit;
  3761. }
  3762. }
  3763. bench_stats_start(&count, &start);
  3764. do {
  3765. for (times = 0; times < numBlocks || pending > 0; ) {
  3766. bench_async_poll(&pending);
  3767. /* while free pending slots in queue, submit ops */
  3768. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3769. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  3770. &times, numBlocks, &pending)) {
  3771. ret = wc_AesCbcEncrypt(enc[i], bench_plain, bench_cipher,
  3772. bench_size);
  3773. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  3774. 0, &times, &pending)) {
  3775. goto exit_aes_enc;
  3776. }
  3777. }
  3778. } /* for i */
  3779. RECORD_MULTI_VALUE_STATS();
  3780. } /* for times */
  3781. count += times;
  3782. } while (bench_stats_check(start)
  3783. #ifdef MULTI_VALUE_STATISTICS
  3784. || runs < minimum_runs
  3785. #endif
  3786. );
  3787. exit_aes_enc:
  3788. bench_stats_sym_finish(encLabel, useDeviceID, count,
  3789. bench_size, start, ret);
  3790. #ifdef MULTI_VALUE_STATISTICS
  3791. bench_multi_value_stats(max, min, sum, squareSum, runs);
  3792. #endif
  3793. if (ret < 0) {
  3794. goto exit;
  3795. }
  3796. #ifdef HAVE_AES_DECRYPT
  3797. /* init keys */
  3798. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3799. ret = wc_AesSetKey(enc[i], key, keySz, iv, AES_DECRYPTION);
  3800. if (ret != 0) {
  3801. printf("AesSetKey failed, ret = %d\n", ret);
  3802. goto exit;
  3803. }
  3804. }
  3805. RESET_MULTI_VALUE_STATS_VARS();
  3806. bench_stats_start(&count, &start);
  3807. do {
  3808. for (times = 0; times < numBlocks || pending > 0; ) {
  3809. bench_async_poll(&pending);
  3810. /* while free pending slots in queue, submit ops */
  3811. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3812. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  3813. &times, numBlocks, &pending)) {
  3814. ret = wc_AesCbcDecrypt(enc[i], bench_cipher, bench_plain,
  3815. bench_size);
  3816. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  3817. 0, &times, &pending)) {
  3818. goto exit_aes_dec;
  3819. }
  3820. }
  3821. } /* for i */
  3822. RECORD_MULTI_VALUE_STATS();
  3823. } /* for times */
  3824. count += times;
  3825. } while (bench_stats_check(start)
  3826. #ifdef MULTI_VALUE_STATISTICS
  3827. || runs < minimum_runs
  3828. #endif
  3829. );
  3830. exit_aes_dec:
  3831. bench_stats_sym_finish(decLabel, useDeviceID, count, bench_size,
  3832. start, ret);
  3833. #ifdef MULTI_VALUE_STATISTICS
  3834. bench_multi_value_stats(max, min, sum, squareSum, runs);
  3835. #endif
  3836. #endif /* HAVE_AES_DECRYPT */
  3837. (void)decLabel;
  3838. exit:
  3839. if (WC_ARRAY_OK(enc)) {
  3840. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3841. wc_AesFree(enc[i]);
  3842. }
  3843. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  3844. }
  3845. }
  3846. void bench_aescbc(int useDeviceID)
  3847. {
  3848. #ifdef WOLFSSL_AES_128
  3849. #ifdef HAVE_RENESAS_SYNC
  3850. bench_aescbc_internal(useDeviceID, bench_key1, 16, bench_iv,
  3851. "AES-128-CBC-enc", "AES-128-CBC-dec");
  3852. #else
  3853. bench_aescbc_internal(useDeviceID, bench_key, 16, bench_iv,
  3854. "AES-128-CBC-enc", "AES-128-CBC-dec");
  3855. #endif
  3856. #endif
  3857. #ifdef WOLFSSL_AES_192
  3858. bench_aescbc_internal(useDeviceID, bench_key, 24, bench_iv,
  3859. "AES-192-CBC-enc", "AES-192-CBC-dec");
  3860. #endif
  3861. #ifdef WOLFSSL_AES_256
  3862. #ifdef HAVE_RENESAS_SYNC
  3863. bench_aescbc_internal(useDeviceID, bench_key2, 32, bench_iv,
  3864. "AES-256-CBC-enc", "AES-256-CBC-dec");
  3865. #else
  3866. bench_aescbc_internal(useDeviceID, bench_key, 32, bench_iv,
  3867. "AES-256-CBC-enc", "AES-256-CBC-dec");
  3868. #endif
  3869. #endif
  3870. }
  3871. #endif /* HAVE_AES_CBC */
  3872. #ifdef HAVE_AESGCM
  3873. static void bench_aesgcm_internal(int useDeviceID,
  3874. const byte* key, word32 keySz,
  3875. const byte* iv, word32 ivSz,
  3876. const char* encLabel, const char* decLabel)
  3877. {
  3878. int ret = 0, i, count = 0, times, pending = 0;
  3879. WC_DECLARE_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  3880. sizeof(Aes), HEAP_HINT);
  3881. #ifdef HAVE_AES_DECRYPT
  3882. WC_DECLARE_ARRAY(dec, Aes, BENCH_MAX_PENDING,
  3883. sizeof(Aes), HEAP_HINT);
  3884. #endif
  3885. double start;
  3886. DECLARE_MULTI_VALUE_STATS_VARS()
  3887. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  3888. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  3889. WC_ALLOC_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  3890. WC_ALLOC_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  3891. WC_CALLOC_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  3892. sizeof(Aes), HEAP_HINT);
  3893. #ifdef HAVE_AES_DECRYPT
  3894. WC_CALLOC_ARRAY(dec, Aes, BENCH_MAX_PENDING,
  3895. sizeof(Aes), HEAP_HINT);
  3896. #endif
  3897. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  3898. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  3899. /* init keys */
  3900. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3901. if ((ret = wc_AesInit(enc[i], HEAP_HINT,
  3902. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  3903. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  3904. goto exit;
  3905. }
  3906. ret = wc_AesGcmSetKey(enc[i], key, keySz);
  3907. if (ret != 0) {
  3908. printf("AesGcmSetKey failed, ret = %d\n", ret);
  3909. goto exit;
  3910. }
  3911. }
  3912. /* GCM uses same routine in backend for both encrypt and decrypt */
  3913. bench_stats_start(&count, &start);
  3914. do {
  3915. for (times = 0; times < numBlocks || pending > 0; ) {
  3916. bench_async_poll(&pending);
  3917. /* while free pending slots in queue, submit ops */
  3918. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3919. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  3920. &times, numBlocks, &pending)) {
  3921. ret = wc_AesGcmEncrypt(enc[i], bench_cipher,
  3922. bench_plain, bench_size,
  3923. iv, ivSz, bench_tag, AES_AUTH_TAG_SZ,
  3924. bench_additional, aesAuthAddSz);
  3925. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  3926. 0, &times, &pending)) {
  3927. goto exit_aes_gcm;
  3928. }
  3929. }
  3930. } /* for i */
  3931. RECORD_MULTI_VALUE_STATS();
  3932. } /* for times */
  3933. count += times;
  3934. } while (bench_stats_check(start)
  3935. #ifdef MULTI_VALUE_STATISTICS
  3936. || runs < minimum_runs
  3937. #endif
  3938. );
  3939. exit_aes_gcm:
  3940. bench_stats_sym_finish(encLabel, useDeviceID, count, bench_size,
  3941. start, ret);
  3942. #ifdef MULTI_VALUE_STATISTICS
  3943. bench_multi_value_stats(max, min, sum, squareSum, runs);
  3944. #endif
  3945. #ifdef HAVE_AES_DECRYPT
  3946. RESET_MULTI_VALUE_STATS_VARS();
  3947. /* init keys */
  3948. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3949. if ((ret = wc_AesInit(dec[i], HEAP_HINT,
  3950. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  3951. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  3952. goto exit;
  3953. }
  3954. ret = wc_AesGcmSetKey(dec[i], key, keySz);
  3955. if (ret != 0) {
  3956. printf("AesGcmSetKey failed, ret = %d\n", ret);
  3957. goto exit;
  3958. }
  3959. }
  3960. bench_stats_start(&count, &start);
  3961. do {
  3962. for (times = 0; times < numBlocks || pending > 0; ) {
  3963. bench_async_poll(&pending);
  3964. /* while free pending slots in queue, submit ops */
  3965. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3966. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(dec[i]), 0,
  3967. &times, numBlocks, &pending)) {
  3968. ret = wc_AesGcmDecrypt(dec[i], bench_plain,
  3969. bench_cipher, bench_size,
  3970. iv, ivSz, bench_tag, AES_AUTH_TAG_SZ,
  3971. bench_additional, aesAuthAddSz);
  3972. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(dec[i]),
  3973. 0, &times, &pending)) {
  3974. goto exit_aes_gcm_dec;
  3975. }
  3976. }
  3977. } /* for i */
  3978. RECORD_MULTI_VALUE_STATS();
  3979. } /* for times */
  3980. count += times;
  3981. } while (bench_stats_check(start)
  3982. #ifdef MULTI_VALUE_STATISTICS
  3983. || runs < minimum_runs
  3984. #endif
  3985. );
  3986. exit_aes_gcm_dec:
  3987. bench_stats_sym_finish(decLabel, useDeviceID, count, bench_size,
  3988. start, ret);
  3989. #ifdef MULTI_VALUE_STATISTICS
  3990. bench_multi_value_stats(max, min, sum, squareSum, runs);
  3991. #endif
  3992. #endif /* HAVE_AES_DECRYPT */
  3993. (void)decLabel;
  3994. exit:
  3995. if (ret < 0) {
  3996. printf("bench_aesgcm failed: %d\n", ret);
  3997. }
  3998. #ifdef HAVE_AES_DECRYPT
  3999. if (WC_ARRAY_OK(dec)) {
  4000. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4001. wc_AesFree(dec[i]);
  4002. }
  4003. WC_FREE_ARRAY(dec, BENCH_MAX_PENDING, HEAP_HINT);
  4004. }
  4005. #endif
  4006. if (WC_ARRAY_OK(enc)) {
  4007. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4008. wc_AesFree(enc[i]);
  4009. }
  4010. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  4011. }
  4012. WC_FREE_VAR(bench_additional, HEAP_HINT);
  4013. WC_FREE_VAR(bench_tag, HEAP_HINT);
  4014. }
  4015. #ifdef WOLFSSL_AESGCM_STREAM
  4016. static void bench_aesgcm_stream_internal(int useDeviceID,
  4017. const byte* key, word32 keySz, const byte* iv, word32 ivSz,
  4018. const char* encLabel, const char* decLabel)
  4019. {
  4020. int ret = 0, i, count = 0, times, pending = 0;
  4021. WC_DECLARE_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  4022. sizeof(Aes), HEAP_HINT);
  4023. #ifdef HAVE_AES_DECRYPT
  4024. WC_DECLARE_ARRAY(dec, Aes, BENCH_MAX_PENDING,
  4025. sizeof(Aes), HEAP_HINT);
  4026. #endif
  4027. double start;
  4028. DECLARE_MULTI_VALUE_STATS_VARS()
  4029. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4030. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4031. WC_ALLOC_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4032. WC_ALLOC_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4033. WC_CALLOC_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  4034. sizeof(Aes), HEAP_HINT);
  4035. #ifdef HAVE_AES_DECRYPT
  4036. WC_CALLOC_ARRAY(dec, Aes, BENCH_MAX_PENDING,
  4037. sizeof(Aes), HEAP_HINT);
  4038. #endif
  4039. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  4040. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  4041. /* init keys */
  4042. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4043. if ((ret = wc_AesInit(enc[i], HEAP_HINT,
  4044. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  4045. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  4046. goto exit;
  4047. }
  4048. ret = wc_AesGcmSetKey(enc[i], key, keySz);
  4049. if (ret != 0) {
  4050. printf("AesGcmSetKey failed, ret = %d\n", ret);
  4051. goto exit;
  4052. }
  4053. }
  4054. /* GCM uses same routine in backend for both encrypt and decrypt */
  4055. bench_stats_start(&count, &start);
  4056. do {
  4057. for (times = 0; times < numBlocks || pending > 0; ) {
  4058. bench_async_poll(&pending);
  4059. /* while free pending slots in queue, submit ops */
  4060. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4061. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  4062. &times, numBlocks, &pending)) {
  4063. ret = wc_AesGcmEncryptInit(enc[i], NULL, 0, iv, ivSz);
  4064. if (ret == 0) {
  4065. ret = wc_AesGcmEncryptUpdate(enc[i], bench_cipher,
  4066. bench_plain, bench_size, bench_additional,
  4067. aesAuthAddSz);
  4068. }
  4069. if (ret == 0) {
  4070. ret = wc_AesGcmEncryptFinal(enc[i], bench_tag,
  4071. AES_AUTH_TAG_SZ);
  4072. }
  4073. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  4074. 0, &times, &pending)) {
  4075. goto exit_aes_gcm;
  4076. }
  4077. }
  4078. } /* for i */
  4079. RECORD_MULTI_VALUE_STATS();
  4080. } /* for times */
  4081. count += times;
  4082. } while (bench_stats_check(start)
  4083. #ifdef MULTI_VALUE_STATISTICS
  4084. || runs < minimum_runs
  4085. #endif
  4086. );
  4087. exit_aes_gcm:
  4088. bench_stats_sym_finish(encLabel, useDeviceID, count, bench_size,
  4089. start, ret);
  4090. #ifdef MULTI_VALUE_STATISTICS
  4091. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4092. #endif
  4093. #ifdef HAVE_AES_DECRYPT
  4094. /* init keys */
  4095. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4096. if ((ret = wc_AesInit(dec[i], HEAP_HINT,
  4097. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  4098. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  4099. goto exit;
  4100. }
  4101. ret = wc_AesGcmSetKey(dec[i], key, keySz);
  4102. if (ret != 0) {
  4103. printf("AesGcmSetKey failed, ret = %d\n", ret);
  4104. goto exit;
  4105. }
  4106. }
  4107. RESET_MULTI_VALUE_STATS_VARS();
  4108. bench_stats_start(&count, &start);
  4109. do {
  4110. for (times = 0; times < numBlocks || pending > 0; ) {
  4111. bench_async_poll(&pending);
  4112. /* while free pending slots in queue, submit ops */
  4113. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4114. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(dec[i]), 0,
  4115. &times, numBlocks, &pending)) {
  4116. ret = wc_AesGcmDecryptInit(enc[i], NULL, 0, iv, ivSz);
  4117. if (ret == 0) {
  4118. ret = wc_AesGcmDecryptUpdate(enc[i], bench_plain,
  4119. bench_cipher, bench_size, bench_additional,
  4120. aesAuthAddSz);
  4121. }
  4122. if (ret == 0) {
  4123. ret = wc_AesGcmDecryptFinal(enc[i], bench_tag,
  4124. AES_AUTH_TAG_SZ);
  4125. }
  4126. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(dec[i]),
  4127. 0, &times, &pending)) {
  4128. goto exit_aes_gcm_dec;
  4129. }
  4130. }
  4131. } /* for i */
  4132. RECORD_MULTI_VALUE_STATS();
  4133. } /* for times */
  4134. count += times;
  4135. } while (bench_stats_check(start)
  4136. #ifdef MULTI_VALUE_STATISTICS
  4137. || runs < minimum_runs
  4138. #endif
  4139. );
  4140. exit_aes_gcm_dec:
  4141. bench_stats_sym_finish(decLabel, useDeviceID, count, bench_size,
  4142. start, ret);
  4143. #ifdef MULTI_VALUE_STATISTICS
  4144. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4145. #endif
  4146. #endif /* HAVE_AES_DECRYPT */
  4147. (void)decLabel;
  4148. exit:
  4149. if (ret < 0) {
  4150. printf("bench_aesgcm failed: %d\n", ret);
  4151. }
  4152. #ifdef HAVE_AES_DECRYPT
  4153. if (WC_ARRAY_OK(dec)) {
  4154. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4155. wc_AesFree(dec[i]);
  4156. }
  4157. WC_FREE_ARRAY(dec, BENCH_MAX_PENDING, HEAP_HINT);
  4158. }
  4159. #endif
  4160. if (WC_ARRAY_OK(enc)) {
  4161. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4162. wc_AesFree(enc[i]);
  4163. }
  4164. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  4165. }
  4166. WC_FREE_VAR(bench_additional, HEAP_HINT);
  4167. WC_FREE_VAR(bench_tag, HEAP_HINT);
  4168. }
  4169. #endif
  4170. void bench_aesgcm(int useDeviceID)
  4171. {
  4172. #define AES_GCM_STRING(n, dir) AES_AAD_STRING("AES-" #n "-GCM-" #dir)
  4173. #if defined(WOLFSSL_AES_128) && !defined(WOLFSSL_AFALG_XILINX_AES) \
  4174. && !defined(WOLFSSL_XILINX_CRYPT) \
  4175. || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  4176. #ifdef HAVE_RENESAS_SYNC
  4177. bench_aesgcm_internal(useDeviceID, bench_key1, 16, bench_iv, 12,
  4178. AES_GCM_STRING(128, enc), AES_GCM_STRING(128, dec));
  4179. #else
  4180. bench_aesgcm_internal(useDeviceID, bench_key, 16, bench_iv, 12,
  4181. AES_GCM_STRING(128, enc), AES_GCM_STRING(128, dec));
  4182. #endif
  4183. #endif
  4184. #if defined(WOLFSSL_AES_192) && !defined(WOLFSSL_AFALG_XILINX_AES) \
  4185. && !defined(WOLFSSL_XILINX_CRYPT)
  4186. bench_aesgcm_internal(useDeviceID, bench_key, 24, bench_iv, 12,
  4187. AES_GCM_STRING(192, enc), AES_GCM_STRING(192, dec));
  4188. #endif
  4189. #ifdef WOLFSSL_AES_256
  4190. #ifdef HAVE_RENESAS_SYNC
  4191. bench_aesgcm_internal(useDeviceID, bench_key2, 32, bench_iv, 12,
  4192. AES_GCM_STRING(256, enc), AES_GCM_STRING(256, dec));
  4193. #else
  4194. bench_aesgcm_internal(useDeviceID, bench_key, 32, bench_iv, 12,
  4195. AES_GCM_STRING(256, enc), AES_GCM_STRING(256, dec));
  4196. #endif
  4197. #endif
  4198. #ifdef WOLFSSL_AESGCM_STREAM
  4199. #undef AES_GCM_STRING
  4200. #define AES_GCM_STRING(n, dir) AES_AAD_STRING("AES-" #n "-GCM-STREAM-" #dir)
  4201. #if defined(WOLFSSL_AES_128) && !defined(WOLFSSL_AFALG_XILINX_AES) \
  4202. && !defined(WOLFSSL_XILINX_CRYPT) \
  4203. || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  4204. bench_aesgcm_stream_internal(useDeviceID, bench_key, 16, bench_iv, 12,
  4205. AES_GCM_STRING(128, enc), AES_GCM_STRING(128, dec));
  4206. #endif
  4207. #if defined(WOLFSSL_AES_192) && !defined(WOLFSSL_AFALG_XILINX_AES) \
  4208. && !defined(WOLFSSL_XILINX_CRYPT)
  4209. bench_aesgcm_stream_internal(useDeviceID, bench_key, 24, bench_iv, 12,
  4210. AES_GCM_STRING(192, enc), AES_GCM_STRING(192, dec));
  4211. #endif
  4212. #ifdef WOLFSSL_AES_256
  4213. bench_aesgcm_stream_internal(useDeviceID, bench_key, 32, bench_iv, 12,
  4214. AES_GCM_STRING(256, enc), AES_GCM_STRING(256, dec));
  4215. #endif
  4216. #endif /* WOLFSSL_AESGCM_STREAM */
  4217. #undef AES_GCM_STRING
  4218. }
  4219. /* GMAC */
  4220. void bench_gmac(int useDeviceID)
  4221. {
  4222. int ret, count = 0;
  4223. Gmac gmac;
  4224. double start;
  4225. byte tag[AES_AUTH_TAG_SZ];
  4226. DECLARE_MULTI_VALUE_STATS_VARS()
  4227. /* determine GCM GHASH method */
  4228. #ifdef GCM_SMALL
  4229. const char* gmacStr = "GMAC Small";
  4230. #elif defined(GCM_TABLE)
  4231. const char* gmacStr = "GMAC Table";
  4232. #elif defined(GCM_TABLE_4BIT)
  4233. const char* gmacStr = "GMAC Table 4-bit";
  4234. #elif defined(GCM_WORD32)
  4235. const char* gmacStr = "GMAC Word32";
  4236. #else
  4237. const char* gmacStr = "GMAC Default";
  4238. #endif
  4239. /* init keys */
  4240. XMEMSET(bench_plain, 0, bench_size);
  4241. XMEMSET(tag, 0, sizeof(tag));
  4242. XMEMSET(&gmac, 0, sizeof(Gmac)); /* clear context */
  4243. (void)wc_AesInit((Aes*)&gmac, HEAP_HINT,
  4244. useDeviceID ? devId: INVALID_DEVID);
  4245. #ifdef HAVE_RENESAS_SYNC
  4246. wc_GmacSetKey(&gmac, bench_key1, 16);
  4247. #else
  4248. wc_GmacSetKey(&gmac, bench_key, 16);
  4249. #endif
  4250. bench_stats_start(&count, &start);
  4251. do {
  4252. ret = wc_GmacUpdate(&gmac, bench_iv, 12, bench_plain, bench_size,
  4253. tag, sizeof(tag));
  4254. count++;
  4255. RECORD_MULTI_VALUE_STATS();
  4256. } while (bench_stats_check(start)
  4257. #ifdef MULTI_VALUE_STATISTICS
  4258. || runs < minimum_runs
  4259. #endif
  4260. );
  4261. wc_AesFree((Aes*)&gmac);
  4262. bench_stats_sym_finish(gmacStr, 0, count, bench_size, start, ret);
  4263. #ifdef MULTI_VALUE_STATISTICS
  4264. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4265. #endif
  4266. }
  4267. #endif /* HAVE_AESGCM */
  4268. #ifdef HAVE_AES_ECB
  4269. static void bench_aesecb_internal(int useDeviceID,
  4270. const byte* key, word32 keySz,
  4271. const char* encLabel, const char* decLabel)
  4272. {
  4273. int ret = 0, i, count = 0, times, pending = 0;
  4274. WC_DECLARE_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  4275. sizeof(Aes), HEAP_HINT);
  4276. double start;
  4277. DECLARE_MULTI_VALUE_STATS_VARS()
  4278. #ifdef HAVE_FIPS
  4279. const word32 benchSz = AES_BLOCK_SIZE;
  4280. #else
  4281. const word32 benchSz = bench_size;
  4282. #endif
  4283. WC_CALLOC_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  4284. sizeof(Aes), HEAP_HINT);
  4285. /* init keys */
  4286. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4287. if ((ret = wc_AesInit(enc[i], HEAP_HINT,
  4288. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  4289. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  4290. goto exit;
  4291. }
  4292. ret = wc_AesSetKey(enc[i], key, keySz, bench_iv, AES_ENCRYPTION);
  4293. if (ret != 0) {
  4294. printf("AesSetKey failed, ret = %d\n", ret);
  4295. goto exit;
  4296. }
  4297. }
  4298. bench_stats_start(&count, &start);
  4299. do {
  4300. int outer_loop_limit = (int)((bench_size / benchSz) * 10) + 1;
  4301. for (times = 0;
  4302. times < outer_loop_limit /* numBlocks */ || pending > 0;
  4303. ) {
  4304. bench_async_poll(&pending);
  4305. /* while free pending slots in queue, submit ops */
  4306. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4307. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  4308. &times, outer_loop_limit, &pending)) {
  4309. #ifdef HAVE_FIPS
  4310. wc_AesEncryptDirect(enc[i], bench_cipher, bench_plain);
  4311. #else
  4312. wc_AesEcbEncrypt(enc[i], bench_cipher, bench_plain,
  4313. benchSz);
  4314. #endif
  4315. ret = 0;
  4316. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  4317. 0, &times, &pending)) {
  4318. goto exit_aes_enc;
  4319. }
  4320. }
  4321. } /* for i */
  4322. RECORD_MULTI_VALUE_STATS();
  4323. } /* for times */
  4324. count += times;
  4325. } while (bench_stats_check(start)
  4326. #ifdef MULTI_VALUE_STATISTICS
  4327. || runs < minimum_runs
  4328. #endif
  4329. );
  4330. exit_aes_enc:
  4331. bench_stats_sym_finish(encLabel, useDeviceID, count, benchSz,
  4332. start, ret);
  4333. #ifdef MULTI_VALUE_STATISTICS
  4334. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4335. #endif
  4336. #ifdef HAVE_AES_DECRYPT
  4337. /* init keys */
  4338. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4339. ret = wc_AesSetKey(enc[i], key, keySz, bench_iv, AES_DECRYPTION);
  4340. if (ret != 0) {
  4341. printf("AesSetKey failed, ret = %d\n", ret);
  4342. goto exit;
  4343. }
  4344. }
  4345. RESET_MULTI_VALUE_STATS_VARS();
  4346. bench_stats_start(&count, &start);
  4347. do {
  4348. int outer_loop_limit = (int)(10 * (bench_size / benchSz)) + 1;
  4349. for (times = 0; times < outer_loop_limit || pending > 0; ) {
  4350. bench_async_poll(&pending);
  4351. /* while free pending slots in queue, submit ops */
  4352. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4353. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  4354. &times, outer_loop_limit, &pending)) {
  4355. #ifdef HAVE_FIPS
  4356. wc_AesDecryptDirect(enc[i], bench_plain, bench_cipher);
  4357. #else
  4358. wc_AesEcbDecrypt(enc[i], bench_plain, bench_cipher,
  4359. benchSz);
  4360. #endif
  4361. ret = 0;
  4362. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  4363. 0, &times, &pending)) {
  4364. goto exit_aes_dec;
  4365. }
  4366. }
  4367. } /* for i */
  4368. RECORD_MULTI_VALUE_STATS();
  4369. } /* for times */
  4370. count += times;
  4371. } while (bench_stats_check(start)
  4372. #ifdef MULTI_VALUE_STATISTICS
  4373. || runs < minimum_runs
  4374. #endif
  4375. );
  4376. exit_aes_dec:
  4377. bench_stats_sym_finish(decLabel, useDeviceID, count, benchSz,
  4378. start, ret);
  4379. #ifdef MULTI_VALUE_STATISTICS
  4380. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4381. #endif
  4382. #endif /* HAVE_AES_DECRYPT */
  4383. exit:
  4384. if (WC_ARRAY_OK(enc)) {
  4385. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4386. wc_AesFree(enc[i]);
  4387. }
  4388. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  4389. }
  4390. }
  4391. void bench_aesecb(int useDeviceID)
  4392. {
  4393. #ifdef WOLFSSL_AES_128
  4394. bench_aesecb_internal(useDeviceID, bench_key, 16,
  4395. "AES-128-ECB-enc", "AES-128-ECB-dec");
  4396. #endif
  4397. #ifdef WOLFSSL_AES_192
  4398. bench_aesecb_internal(useDeviceID, bench_key, 24,
  4399. "AES-192-ECB-enc", "AES-192-ECB-dec");
  4400. #endif
  4401. #ifdef WOLFSSL_AES_256
  4402. bench_aesecb_internal(useDeviceID, bench_key, 32,
  4403. "AES-256-ECB-enc", "AES-256-ECB-dec");
  4404. #endif
  4405. }
  4406. #endif /* HAVE_AES_ECB */
  4407. #ifdef WOLFSSL_AES_CFB
  4408. static void bench_aescfb_internal(const byte* key,
  4409. word32 keySz, const byte* iv,
  4410. const char* label)
  4411. {
  4412. Aes enc;
  4413. double start;
  4414. int i, ret, count;
  4415. DECLARE_MULTI_VALUE_STATS_VARS()
  4416. ret = wc_AesInit(&enc, HEAP_HINT, INVALID_DEVID);
  4417. if (ret != 0) {
  4418. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  4419. return;
  4420. }
  4421. ret = wc_AesSetKey(&enc, key, keySz, iv, AES_ENCRYPTION);
  4422. if (ret != 0) {
  4423. printf("AesSetKey failed, ret = %d\n", ret);
  4424. goto out;
  4425. }
  4426. bench_stats_start(&count, &start);
  4427. do {
  4428. for (i = 0; i < numBlocks; i++) {
  4429. if((ret = wc_AesCfbEncrypt(&enc, bench_plain, bench_cipher,
  4430. bench_size)) != 0) {
  4431. printf("wc_AesCfbEncrypt failed, ret = %d\n", ret);
  4432. goto out;
  4433. }
  4434. RECORD_MULTI_VALUE_STATS();
  4435. }
  4436. count += i;
  4437. } while (bench_stats_check(start)
  4438. #ifdef MULTI_VALUE_STATISTICS
  4439. || runs < minimum_runs
  4440. #endif
  4441. );
  4442. bench_stats_sym_finish(label, 0, count, bench_size, start, ret);
  4443. #ifdef MULTI_VALUE_STATISTICS
  4444. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4445. #endif
  4446. out:
  4447. wc_AesFree(&enc);
  4448. return;
  4449. }
  4450. void bench_aescfb(void)
  4451. {
  4452. #ifdef WOLFSSL_AES_128
  4453. bench_aescfb_internal(bench_key, 16, bench_iv, "AES-128-CFB");
  4454. #endif
  4455. #ifdef WOLFSSL_AES_192
  4456. bench_aescfb_internal(bench_key, 24, bench_iv, "AES-192-CFB");
  4457. #endif
  4458. #ifdef WOLFSSL_AES_256
  4459. bench_aescfb_internal(bench_key, 32, bench_iv, "AES-256-CFB");
  4460. #endif
  4461. }
  4462. #endif /* WOLFSSL_AES_CFB */
  4463. #ifdef WOLFSSL_AES_OFB
  4464. static void bench_aesofb_internal(const byte* key,
  4465. word32 keySz, const byte* iv,
  4466. const char* label)
  4467. {
  4468. Aes enc;
  4469. double start;
  4470. int i, ret, count;
  4471. DECLARE_MULTI_VALUE_STATS_VARS()
  4472. ret = wc_AesInit(&enc, NULL, INVALID_DEVID);
  4473. if (ret != 0) {
  4474. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  4475. return;
  4476. }
  4477. ret = wc_AesSetKey(&enc, key, keySz, iv, AES_ENCRYPTION);
  4478. if (ret != 0) {
  4479. printf("AesSetKey failed, ret = %d\n", ret);
  4480. return;
  4481. }
  4482. bench_stats_start(&count, &start);
  4483. do {
  4484. for (i = 0; i < numBlocks; i++) {
  4485. if((ret = wc_AesOfbEncrypt(&enc, bench_plain, bench_cipher,
  4486. bench_size)) != 0) {
  4487. printf("wc_AesCfbEncrypt failed, ret = %d\n", ret);
  4488. return;
  4489. }
  4490. RECORD_MULTI_VALUE_STATS();
  4491. }
  4492. count += i;
  4493. } while (bench_stats_check(start)
  4494. #ifdef MULTI_VALUE_STATISTICS
  4495. || runs < minimum_runs
  4496. #endif
  4497. );
  4498. bench_stats_sym_finish(label, 0, count, bench_size, start, ret);
  4499. #ifdef MULTI_VALUE_STATISTICS
  4500. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4501. #endif
  4502. wc_AesFree(&enc);
  4503. }
  4504. void bench_aesofb(void)
  4505. {
  4506. #ifdef WOLFSSL_AES_128
  4507. bench_aesofb_internal(bench_key, 16, bench_iv, "AES-128-OFB");
  4508. #endif
  4509. #ifdef WOLFSSL_AES_192
  4510. bench_aesofb_internal(bench_key, 24, bench_iv, "AES-192-OFB");
  4511. #endif
  4512. #ifdef WOLFSSL_AES_256
  4513. bench_aesofb_internal(bench_key, 32, bench_iv, "AES-256-OFB");
  4514. #endif
  4515. }
  4516. #endif /* WOLFSSL_AES_CFB */
  4517. #ifdef WOLFSSL_AES_XTS
  4518. void bench_aesxts(void)
  4519. {
  4520. WC_DECLARE_VAR(aes, XtsAes, 1, HEAP_HINT);
  4521. double start;
  4522. int i, count, ret;
  4523. DECLARE_MULTI_VALUE_STATS_VARS()
  4524. static const unsigned char k1[] = {
  4525. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  4526. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  4527. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  4528. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  4529. };
  4530. static const unsigned char i1[] = {
  4531. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  4532. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  4533. };
  4534. WC_ALLOC_VAR(aes, XtsAes, 1, HEAP_HINT);
  4535. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  4536. HEAP_HINT, devId);
  4537. if (ret != 0) {
  4538. printf("wc_AesXtsSetKey failed, ret = %d\n", ret);
  4539. goto exit;
  4540. }
  4541. bench_stats_start(&count, &start);
  4542. do {
  4543. for (i = 0; i < numBlocks; i++) {
  4544. if ((ret = wc_AesXtsEncrypt(aes, bench_cipher, bench_plain,
  4545. bench_size, i1, sizeof(i1))) != 0) {
  4546. printf("wc_AesXtsEncrypt failed, ret = %d\n", ret);
  4547. goto exit;
  4548. }
  4549. RECORD_MULTI_VALUE_STATS();
  4550. }
  4551. count += i;
  4552. } while (bench_stats_check(start)
  4553. #ifdef MULTI_VALUE_STATISTICS
  4554. || runs < minimum_runs
  4555. #endif
  4556. );
  4557. bench_stats_sym_finish("AES-XTS-enc", 0, count, bench_size, start, ret);
  4558. #ifdef MULTI_VALUE_STATISTICS
  4559. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4560. #endif
  4561. wc_AesXtsFree(aes);
  4562. /* decryption benchmark */
  4563. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  4564. HEAP_HINT, devId);
  4565. if (ret != 0) {
  4566. printf("wc_AesXtsSetKey failed, ret = %d\n", ret);
  4567. goto exit;
  4568. }
  4569. RESET_MULTI_VALUE_STATS_VARS();
  4570. bench_stats_start(&count, &start);
  4571. do {
  4572. for (i = 0; i < numBlocks; i++) {
  4573. if ((ret = wc_AesXtsDecrypt(aes, bench_plain, bench_cipher,
  4574. bench_size, i1, sizeof(i1))) != 0) {
  4575. printf("wc_AesXtsDecrypt failed, ret = %d\n", ret);
  4576. goto exit;
  4577. }
  4578. RECORD_MULTI_VALUE_STATS();
  4579. }
  4580. count += i;
  4581. } while (bench_stats_check(start)
  4582. #ifdef MULTI_VALUE_STATISTICS
  4583. || runs < minimum_runs
  4584. #endif
  4585. );
  4586. bench_stats_sym_finish("AES-XTS-dec", 0, count, bench_size, start, ret);
  4587. #ifdef MULTI_VALUE_STATISTICS
  4588. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4589. #endif
  4590. exit:
  4591. wc_AesXtsFree(aes);
  4592. WC_FREE_VAR(aes, HEAP_HINT);
  4593. }
  4594. #endif /* WOLFSSL_AES_XTS */
  4595. #ifdef WOLFSSL_AES_COUNTER
  4596. static void bench_aesctr_internal(const byte* key, word32 keySz,
  4597. const byte* iv, const char* label,
  4598. int useDeviceID)
  4599. {
  4600. Aes enc;
  4601. double start;
  4602. int i, count, ret = 0;
  4603. DECLARE_MULTI_VALUE_STATS_VARS()
  4604. if ((ret = wc_AesInit(&enc, HEAP_HINT,
  4605. useDeviceID ? devId : INVALID_DEVID)) != 0) {
  4606. printf("wc_AesInit failed, ret = %d\n", ret);
  4607. }
  4608. if (wc_AesSetKeyDirect(&enc, key, keySz, iv, AES_ENCRYPTION) < 0) {
  4609. printf("wc_AesSetKeyDirect failed, ret = %d\n", ret);
  4610. return;
  4611. }
  4612. bench_stats_start(&count, &start);
  4613. do {
  4614. for (i = 0; i < numBlocks; i++) {
  4615. if((ret = wc_AesCtrEncrypt(&enc, bench_plain, bench_cipher,
  4616. bench_size)) != 0) {
  4617. printf("wc_AesCtrEncrypt failed, ret = %d\n", ret);
  4618. return;
  4619. }
  4620. RECORD_MULTI_VALUE_STATS();
  4621. }
  4622. count += i;
  4623. } while (bench_stats_check(start)
  4624. #ifdef MULTI_VALUE_STATISTICS
  4625. || runs < minimum_runs
  4626. #endif
  4627. );
  4628. bench_stats_sym_finish(label, useDeviceID, count, bench_size, start, ret);
  4629. #ifdef MULTI_VALUE_STATISTICS
  4630. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4631. #endif
  4632. wc_AesFree(&enc);
  4633. }
  4634. void bench_aesctr(int useDeviceID)
  4635. {
  4636. #ifdef WOLFSSL_AES_128
  4637. bench_aesctr_internal(bench_key, 16, bench_iv, "AES-128-CTR", useDeviceID);
  4638. #endif
  4639. #ifdef WOLFSSL_AES_192
  4640. bench_aesctr_internal(bench_key, 24, bench_iv, "AES-192-CTR", useDeviceID);
  4641. #endif
  4642. #ifdef WOLFSSL_AES_256
  4643. bench_aesctr_internal(bench_key, 32, bench_iv, "AES-256-CTR", useDeviceID);
  4644. #endif
  4645. }
  4646. #endif /* WOLFSSL_AES_COUNTER */
  4647. #ifdef HAVE_AESCCM
  4648. void bench_aesccm(int useDeviceID)
  4649. {
  4650. Aes enc;
  4651. int enc_inited = 0;
  4652. double start;
  4653. int ret, i, count;
  4654. DECLARE_MULTI_VALUE_STATS_VARS()
  4655. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4656. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4657. WC_ALLOC_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4658. WC_ALLOC_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4659. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  4660. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  4661. if ((ret = wc_AesInit(&enc, HEAP_HINT,
  4662. useDeviceID ? devId : INVALID_DEVID)) != 0)
  4663. {
  4664. printf("wc_AesInit failed, ret = %d\n", ret);
  4665. goto exit;
  4666. }
  4667. if ((ret = wc_AesCcmSetKey(&enc, bench_key, 16)) != 0) {
  4668. printf("wc_AesCcmSetKey failed, ret = %d\n", ret);
  4669. goto exit;
  4670. }
  4671. enc_inited = 1;
  4672. bench_stats_start(&count, &start);
  4673. do {
  4674. for (i = 0; i < numBlocks; i++) {
  4675. ret |= wc_AesCcmEncrypt(&enc, bench_cipher, bench_plain, bench_size,
  4676. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  4677. bench_additional, 0);
  4678. RECORD_MULTI_VALUE_STATS();
  4679. }
  4680. count += i;
  4681. } while (bench_stats_check(start)
  4682. #ifdef MULTI_VALUE_STATISTICS
  4683. || runs < minimum_runs
  4684. #endif
  4685. );
  4686. bench_stats_sym_finish(AES_AAD_STRING("AES-CCM-enc"), useDeviceID, count,
  4687. bench_size, start, ret);
  4688. #ifdef MULTI_VALUE_STATISTICS
  4689. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4690. #endif
  4691. if (ret != 0) {
  4692. printf("wc_AesCcmEncrypt failed, ret = %d\n", ret);
  4693. goto exit;
  4694. }
  4695. #ifdef HAVE_AES_DECRYPT
  4696. RESET_MULTI_VALUE_STATS_VARS();
  4697. bench_stats_start(&count, &start);
  4698. do {
  4699. for (i = 0; i < numBlocks; i++) {
  4700. ret |= wc_AesCcmDecrypt(&enc, bench_plain, bench_cipher, bench_size,
  4701. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  4702. bench_additional, 0);
  4703. RECORD_MULTI_VALUE_STATS();
  4704. }
  4705. count += i;
  4706. } while (bench_stats_check(start)
  4707. #ifdef MULTI_VALUE_STATISTICS
  4708. || runs < minimum_runs
  4709. #endif
  4710. );
  4711. bench_stats_sym_finish(AES_AAD_STRING("AES-CCM-dec"), useDeviceID, count,
  4712. bench_size, start, ret);
  4713. #ifdef MULTI_VALUE_STATISTICS
  4714. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4715. #endif
  4716. if (ret != 0) {
  4717. printf("wc_AesCcmEncrypt failed, ret = %d\n", ret);
  4718. goto exit;
  4719. }
  4720. #endif
  4721. exit:
  4722. if (enc_inited)
  4723. wc_AesFree(&enc);
  4724. WC_FREE_VAR(bench_additional, HEAP_HINT);
  4725. WC_FREE_VAR(bench_tag, HEAP_HINT);
  4726. }
  4727. #endif /* HAVE_AESCCM */
  4728. #ifdef WOLFSSL_AES_SIV
  4729. static void bench_aessiv_internal(const byte* key, word32 keySz, const char*
  4730. encLabel, const char* decLabel)
  4731. {
  4732. int i;
  4733. int ret = 0;
  4734. byte assoc[AES_BLOCK_SIZE];
  4735. byte nonce[AES_BLOCK_SIZE];
  4736. byte siv[AES_BLOCK_SIZE];
  4737. int count = 0;
  4738. double start;
  4739. DECLARE_MULTI_VALUE_STATS_VARS()
  4740. bench_stats_start(&count, &start);
  4741. do {
  4742. for (i = 0; i < numBlocks; i++) {
  4743. ret = wc_AesSivEncrypt(key, keySz, assoc, AES_BLOCK_SIZE, nonce,
  4744. AES_BLOCK_SIZE, bench_plain, bench_size,
  4745. siv, bench_cipher);
  4746. if (ret != 0) {
  4747. printf("wc_AesSivEncrypt failed (%d)\n", ret);
  4748. return;
  4749. }
  4750. RECORD_MULTI_VALUE_STATS();
  4751. }
  4752. count += i;
  4753. } while (bench_stats_check(start)
  4754. #ifdef MULTI_VALUE_STATISTICS
  4755. || runs < minimum_runs
  4756. #endif
  4757. );
  4758. bench_stats_sym_finish(encLabel, 0, count, bench_size, start, ret);
  4759. #ifdef MULTI_VALUE_STATISTICS
  4760. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4761. #endif
  4762. RESET_MULTI_VALUE_STATS_VARS();
  4763. bench_stats_start(&count, &start);
  4764. do {
  4765. for (i = 0; i < numBlocks; i++) {
  4766. ret = wc_AesSivDecrypt(key, keySz, assoc, AES_BLOCK_SIZE, nonce,
  4767. AES_BLOCK_SIZE, bench_cipher, bench_size,
  4768. siv, bench_plain);
  4769. if (ret != 0) {
  4770. printf("wc_AesSivDecrypt failed (%d)\n", ret);
  4771. return;
  4772. }
  4773. RECORD_MULTI_VALUE_STATS();
  4774. }
  4775. count += i;
  4776. } while (bench_stats_check(start)
  4777. #ifdef MULTI_VALUE_STATISTICS
  4778. || runs < minimum_runs
  4779. #endif
  4780. );
  4781. bench_stats_sym_finish(decLabel, 0, count, bench_size, start, ret);
  4782. #ifdef MULTI_VALUE_STATISTICS
  4783. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4784. #endif
  4785. }
  4786. void bench_aessiv(void)
  4787. {
  4788. bench_aessiv_internal(bench_key, 32, "AES-256-SIV-enc", "AES-256-SIV-dec");
  4789. bench_aessiv_internal(bench_key, 48, "AES-384-SIV-enc", "AES-384-SIV-dec");
  4790. bench_aessiv_internal(bench_key, 64, "AES-512-SIV-enc", "AES-512-SIV-dec");
  4791. }
  4792. #endif /* WOLFSSL_AES_SIV */
  4793. #endif /* !NO_AES */
  4794. #ifdef HAVE_POLY1305
  4795. void bench_poly1305(void)
  4796. {
  4797. Poly1305 enc;
  4798. byte mac[16];
  4799. double start;
  4800. int ret = 0, i, count;
  4801. DECLARE_MULTI_VALUE_STATS_VARS()
  4802. if (digest_stream) {
  4803. ret = wc_Poly1305SetKey(&enc, bench_key, 32);
  4804. if (ret != 0) {
  4805. printf("Poly1305SetKey failed, ret = %d\n", ret);
  4806. return;
  4807. }
  4808. bench_stats_start(&count, &start);
  4809. do {
  4810. for (i = 0; i < numBlocks; i++) {
  4811. ret = wc_Poly1305Update(&enc, bench_plain, bench_size);
  4812. if (ret != 0) {
  4813. printf("Poly1305Update failed: %d\n", ret);
  4814. break;
  4815. }
  4816. RECORD_MULTI_VALUE_STATS();
  4817. }
  4818. wc_Poly1305Final(&enc, mac);
  4819. count += i;
  4820. } while (bench_stats_check(start)
  4821. #ifdef MULTI_VALUE_STATISTICS
  4822. || runs < minimum_runs
  4823. #endif
  4824. );
  4825. }
  4826. else {
  4827. bench_stats_start(&count, &start);
  4828. do {
  4829. for (i = 0; i < numBlocks; i++) {
  4830. ret = wc_Poly1305SetKey(&enc, bench_key, 32);
  4831. if (ret != 0) {
  4832. printf("Poly1305SetKey failed, ret = %d\n", ret);
  4833. return;
  4834. }
  4835. ret = wc_Poly1305Update(&enc, bench_plain, bench_size);
  4836. if (ret != 0) {
  4837. printf("Poly1305Update failed: %d\n", ret);
  4838. break;
  4839. }
  4840. wc_Poly1305Final(&enc, mac);
  4841. RECORD_MULTI_VALUE_STATS();
  4842. }
  4843. count += i;
  4844. } while (bench_stats_check(start)
  4845. #ifdef MULTI_VALUE_STATISTICS
  4846. || runs < minimum_runs
  4847. #endif
  4848. );
  4849. }
  4850. bench_stats_sym_finish("POLY1305", 0, count, bench_size, start, ret);
  4851. #ifdef MULTI_VALUE_STATISTICS
  4852. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4853. #endif
  4854. }
  4855. #endif /* HAVE_POLY1305 */
  4856. #ifdef HAVE_CAMELLIA
  4857. void bench_camellia(void)
  4858. {
  4859. Camellia cam;
  4860. double start;
  4861. int ret, i, count;
  4862. DECLARE_MULTI_VALUE_STATS_VARS()
  4863. ret = wc_CamelliaSetKey(&cam, bench_key, 16, bench_iv);
  4864. if (ret != 0) {
  4865. printf("CamelliaSetKey failed, ret = %d\n", ret);
  4866. return;
  4867. }
  4868. bench_stats_start(&count, &start);
  4869. do {
  4870. for (i = 0; i < numBlocks; i++) {
  4871. ret = wc_CamelliaCbcEncrypt(&cam, bench_cipher, bench_plain,
  4872. bench_size);
  4873. if (ret < 0) {
  4874. printf("CamelliaCbcEncrypt failed: %d\n", ret);
  4875. return;
  4876. }
  4877. RECORD_MULTI_VALUE_STATS();
  4878. }
  4879. count += i;
  4880. } while (bench_stats_check(start)
  4881. #ifdef MULTI_VALUE_STATISTICS
  4882. || runs < minimum_runs
  4883. #endif
  4884. );
  4885. bench_stats_sym_finish("Camellia", 0, count, bench_size, start, ret);
  4886. #ifdef MULTI_VALUE_STATISTICS
  4887. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4888. #endif
  4889. }
  4890. #endif
  4891. #ifdef WOLFSSL_SM4_CBC
  4892. void bench_sm4_cbc(void)
  4893. {
  4894. wc_Sm4 sm4;
  4895. double start;
  4896. int ret;
  4897. int i;
  4898. int count;
  4899. DECLARE_MULTI_VALUE_STATS_VARS()
  4900. ret = wc_Sm4SetKey(&sm4, bench_key, SM4_KEY_SIZE);
  4901. if (ret != 0) {
  4902. printf("Sm4SetKey failed, ret = %d\n", ret);
  4903. return;
  4904. }
  4905. ret = wc_Sm4SetIV(&sm4, bench_iv);
  4906. if (ret != 0) {
  4907. printf("Sm4SetIV failed, ret = %d\n", ret);
  4908. return;
  4909. }
  4910. bench_stats_start(&count, &start);
  4911. do {
  4912. for (i = 0; i < numBlocks; i++) {
  4913. ret = wc_Sm4CbcEncrypt(&sm4, bench_cipher, bench_plain, bench_size);
  4914. if (ret < 0) {
  4915. printf("Sm4CbcEncrypt failed: %d\n", ret);
  4916. return;
  4917. }
  4918. RECORD_MULTI_VALUE_STATS();
  4919. }
  4920. count += i;
  4921. } while (bench_stats_check(start)
  4922. #ifdef MULTI_VALUE_STATISTICS
  4923. || runs < minimum_runs
  4924. #endif
  4925. );
  4926. bench_stats_sym_finish("SM4-CBC-enc", 0, count, bench_size, start, ret);
  4927. #ifdef MULTI_VALUE_STATISTICS
  4928. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4929. #endif
  4930. RESET_MULTI_VALUE_STATS_VARS();
  4931. bench_stats_start(&count, &start);
  4932. do {
  4933. for (i = 0; i < numBlocks; i++) {
  4934. ret = wc_Sm4CbcDecrypt(&sm4, bench_plain, bench_cipher, bench_size);
  4935. if (ret < 0) {
  4936. printf("Sm4CbcDecrypt failed: %d\n", ret);
  4937. return;
  4938. }
  4939. RECORD_MULTI_VALUE_STATS();
  4940. }
  4941. count += i;
  4942. } while (bench_stats_check(start)
  4943. #ifdef MULTI_VALUE_STATISTICS
  4944. || runs < minimum_runs
  4945. #endif
  4946. );
  4947. bench_stats_sym_finish("SM4-CBC-dec", 0, count, bench_size, start, ret);
  4948. #ifdef MULTI_VALUE_STATISTICS
  4949. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4950. #endif
  4951. }
  4952. #endif
  4953. #ifdef WOLFSSL_SM4_GCM
  4954. void bench_sm4_gcm(void)
  4955. {
  4956. wc_Sm4 sm4;
  4957. double start;
  4958. int ret;
  4959. int i;
  4960. int count;
  4961. DECLARE_MULTI_VALUE_STATS_VARS()
  4962. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4963. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4964. WC_ALLOC_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4965. WC_ALLOC_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4966. ret = wc_Sm4GcmSetKey(&sm4, bench_key, SM4_KEY_SIZE);
  4967. if (ret != 0) {
  4968. printf("Sm4GcmSetKey failed, ret = %d\n", ret);
  4969. goto exit;
  4970. }
  4971. bench_stats_start(&count, &start);
  4972. do {
  4973. for (i = 0; i < numBlocks; i++) {
  4974. ret = wc_Sm4GcmEncrypt(&sm4, bench_cipher, bench_plain, bench_size,
  4975. bench_iv, GCM_NONCE_MID_SZ, bench_tag, SM4_BLOCK_SIZE,
  4976. bench_additional, aesAuthAddSz);
  4977. if (ret < 0) {
  4978. printf("Sm4GcmEncrypt failed: %d\n", ret);
  4979. goto exit;
  4980. }
  4981. RECORD_MULTI_VALUE_STATS();
  4982. }
  4983. count += i;
  4984. } while (bench_stats_check(start)
  4985. #ifdef MULTI_VALUE_STATISTICS
  4986. || runs < minimum_runs
  4987. #endif
  4988. );
  4989. bench_stats_sym_finish("SM4-GCM-enc", 0, count, bench_size, start, ret);
  4990. #ifdef MULTI_VALUE_STATISTICS
  4991. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4992. #endif
  4993. RESET_MULTI_VALUE_STATS_VARS();
  4994. bench_stats_start(&count, &start);
  4995. do {
  4996. for (i = 0; i < numBlocks; i++) {
  4997. ret = wc_Sm4GcmDecrypt(&sm4, bench_plain, bench_cipher, bench_size,
  4998. bench_iv, GCM_NONCE_MID_SZ, bench_tag, SM4_BLOCK_SIZE,
  4999. bench_additional, aesAuthAddSz);
  5000. if (ret < 0) {
  5001. printf("Sm4GcmDecrypt failed: %d\n", ret);
  5002. goto exit;
  5003. }
  5004. RECORD_MULTI_VALUE_STATS();
  5005. }
  5006. count += i;
  5007. } while (bench_stats_check(start)
  5008. #ifdef MULTI_VALUE_STATISTICS
  5009. || runs < minimum_runs
  5010. #endif
  5011. );
  5012. bench_stats_sym_finish("SM4-GCM-dec", 0, count, bench_size, start, ret);
  5013. #ifdef MULTI_VALUE_STATISTICS
  5014. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5015. #endif
  5016. exit:
  5017. WC_FREE_VAR(bench_additional, HEAP_HINT);
  5018. WC_FREE_VAR(bench_tag, HEAP_HINT);
  5019. }
  5020. #endif
  5021. #ifdef WOLFSSL_SM4_CCM
  5022. void bench_sm4_ccm(void)
  5023. {
  5024. wc_Sm4 enc;
  5025. double start;
  5026. int ret, i, count;
  5027. DECLARE_MULTI_VALUE_STATS_VARS()
  5028. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  5029. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  5030. WC_ALLOC_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  5031. WC_ALLOC_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  5032. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  5033. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  5034. if ((ret = wc_Sm4SetKey(&enc, bench_key, 16)) != 0) {
  5035. printf("wc_Sm4SetKey failed, ret = %d\n", ret);
  5036. goto exit;
  5037. }
  5038. bench_stats_start(&count, &start);
  5039. do {
  5040. for (i = 0; i < numBlocks; i++) {
  5041. ret |= wc_Sm4CcmEncrypt(&enc, bench_cipher, bench_plain, bench_size,
  5042. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  5043. bench_additional, 0);
  5044. RECORD_MULTI_VALUE_STATS();
  5045. }
  5046. count += i;
  5047. } while (bench_stats_check(start)
  5048. #ifdef MULTI_VALUE_STATISTICS
  5049. || runs < minimum_runs
  5050. #endif
  5051. );
  5052. bench_stats_sym_finish("SM4-CCM-enc", 0, count, bench_size, start, ret);
  5053. #ifdef MULTI_VALUE_STATISTICS
  5054. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5055. #endif
  5056. if (ret != 0) {
  5057. printf("wc_Sm4Encrypt failed, ret = %d\n", ret);
  5058. goto exit;
  5059. }
  5060. RESET_MULTI_VALUE_STATS_VARS();
  5061. bench_stats_start(&count, &start);
  5062. do {
  5063. for (i = 0; i < numBlocks; i++) {
  5064. ret |= wc_Sm4CcmDecrypt(&enc, bench_plain, bench_cipher, bench_size,
  5065. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  5066. bench_additional, 0);
  5067. RECORD_MULTI_VALUE_STATS();
  5068. }
  5069. count += i;
  5070. } while (bench_stats_check(start)
  5071. #ifdef MULTI_VALUE_STATISTICS
  5072. || runs < minimum_runs
  5073. #endif
  5074. );
  5075. bench_stats_sym_finish("SM4-CCM-dec", 0, count, bench_size, start, ret);
  5076. #ifdef MULTI_VALUE_STATISTICS
  5077. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5078. #endif
  5079. if (ret != 0) {
  5080. printf("wc_Sm4Decrypt failed, ret = %d\n", ret);
  5081. goto exit;
  5082. }
  5083. exit:
  5084. WC_FREE_VAR(bench_additional, HEAP_HINT);
  5085. WC_FREE_VAR(bench_tag, HEAP_HINT);
  5086. }
  5087. #endif /* HAVE_AESCCM */
  5088. #ifndef NO_DES3
  5089. void bench_des(int useDeviceID)
  5090. {
  5091. int ret = 0, i, count = 0, times, pending = 0;
  5092. WC_DECLARE_ARRAY(enc, Des3, BENCH_MAX_PENDING,
  5093. sizeof(Des3), HEAP_HINT);
  5094. double start;
  5095. DECLARE_MULTI_VALUE_STATS_VARS()
  5096. WC_CALLOC_ARRAY(enc, Des3, BENCH_MAX_PENDING,
  5097. sizeof(Des3), HEAP_HINT);
  5098. /* init keys */
  5099. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5100. if ((ret = wc_Des3Init(enc[i], HEAP_HINT,
  5101. useDeviceID ? devId : INVALID_DEVID)) != 0) {
  5102. printf("Des3Init failed, ret = %d\n", ret);
  5103. goto exit;
  5104. }
  5105. ret = wc_Des3_SetKey(enc[i], bench_key, bench_iv, DES_ENCRYPTION);
  5106. if (ret != 0) {
  5107. printf("Des3_SetKey failed, ret = %d\n", ret);
  5108. goto exit;
  5109. }
  5110. }
  5111. bench_stats_start(&count, &start);
  5112. do {
  5113. for (times = 0; times < numBlocks || pending > 0; ) {
  5114. bench_async_poll(&pending);
  5115. /* while free pending slots in queue, submit ops */
  5116. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5117. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  5118. &times, numBlocks, &pending)) {
  5119. ret = wc_Des3_CbcEncrypt(enc[i],
  5120. bench_cipher,
  5121. bench_plain, bench_size);
  5122. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  5123. 0, &times, &pending)) {
  5124. goto exit_3des;
  5125. }
  5126. }
  5127. } /* for i */
  5128. RECORD_MULTI_VALUE_STATS();
  5129. } /* for times */
  5130. count += times;
  5131. } while (bench_stats_check(start)
  5132. #ifdef MULTI_VALUE_STATISTICS
  5133. || runs < minimum_runs
  5134. #endif
  5135. );
  5136. exit_3des:
  5137. bench_stats_sym_finish("3DES", useDeviceID, count, bench_size, start, ret);
  5138. #ifdef MULTI_VALUE_STATISTICS
  5139. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5140. #endif
  5141. exit:
  5142. if (WC_ARRAY_OK(enc)) {
  5143. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5144. wc_Des3Free(enc[i]);
  5145. }
  5146. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  5147. }
  5148. }
  5149. #endif /* !NO_DES3 */
  5150. #ifndef NO_RC4
  5151. void bench_arc4(int useDeviceID)
  5152. {
  5153. int ret = 0, i, count = 0, times, pending = 0;
  5154. WC_DECLARE_ARRAY(enc, Arc4, BENCH_MAX_PENDING,
  5155. sizeof(Arc4), HEAP_HINT);
  5156. double start;
  5157. DECLARE_MULTI_VALUE_STATS_VARS()
  5158. WC_CALLOC_ARRAY(enc, Arc4, BENCH_MAX_PENDING,
  5159. sizeof(Arc4), HEAP_HINT);
  5160. /* init keys */
  5161. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5162. if ((ret = wc_Arc4Init(enc[i], HEAP_HINT,
  5163. useDeviceID ? devId : INVALID_DEVID)) != 0) {
  5164. printf("Arc4Init failed, ret = %d\n", ret);
  5165. goto exit;
  5166. }
  5167. ret = wc_Arc4SetKey(enc[i], bench_key, 16);
  5168. if (ret != 0) {
  5169. printf("Arc4SetKey failed, ret = %d\n", ret);
  5170. goto exit;
  5171. }
  5172. }
  5173. bench_stats_start(&count, &start);
  5174. do {
  5175. for (times = 0; times < numBlocks || pending > 0; ) {
  5176. bench_async_poll(&pending);
  5177. /* while free pending slots in queue, submit ops */
  5178. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5179. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  5180. &times, numBlocks, &pending)) {
  5181. ret = wc_Arc4Process(enc[i], bench_cipher, bench_plain,
  5182. bench_size);
  5183. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  5184. 0, &times, &pending)) {
  5185. goto exit_arc4;
  5186. }
  5187. }
  5188. } /* for i */
  5189. RECORD_MULTI_VALUE_STATS();
  5190. } /* for times */
  5191. count += times;
  5192. } while (bench_stats_check(start)
  5193. #ifdef MULTI_VALUE_STATISTICS
  5194. || runs < minimum_runs
  5195. #endif
  5196. );
  5197. exit_arc4:
  5198. bench_stats_sym_finish("ARC4", useDeviceID, count, bench_size, start, ret);
  5199. #ifdef MULTI_VALUE_STATISTICS
  5200. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5201. #endif
  5202. exit:
  5203. if (WC_ARRAY_OK(enc)) {
  5204. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5205. wc_Arc4Free(enc[i]);
  5206. }
  5207. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  5208. }
  5209. }
  5210. #endif /* !NO_RC4 */
  5211. #ifdef HAVE_CHACHA
  5212. void bench_chacha(void)
  5213. {
  5214. WC_DECLARE_VAR(enc, ChaCha, 1, HEAP_HINT);
  5215. double start;
  5216. int ret, i, count;
  5217. DECLARE_MULTI_VALUE_STATS_VARS()
  5218. WC_ALLOC_VAR(enc, ChaCha, 1, HEAP_HINT);
  5219. XMEMSET(enc, 0, sizeof(ChaCha));
  5220. wc_Chacha_SetKey(enc, bench_key, 16);
  5221. if (encrypt_only) {
  5222. ret = wc_Chacha_SetIV(enc, bench_iv, 0);
  5223. if (ret < 0) {
  5224. printf("wc_Chacha_SetIV error: %d\n", ret);
  5225. goto exit;
  5226. }
  5227. bench_stats_start(&count, &start);
  5228. do {
  5229. for (i = 0; i < numBlocks; i++) {
  5230. ret = wc_Chacha_Process(enc, bench_cipher, bench_plain,
  5231. bench_size);
  5232. if (ret < 0) {
  5233. printf("wc_Chacha_Process error: %d\n", ret);
  5234. goto exit;
  5235. }
  5236. RECORD_MULTI_VALUE_STATS();
  5237. }
  5238. count += i;
  5239. } while (bench_stats_check(start)
  5240. #ifdef MULTI_VALUE_STATISTICS
  5241. || runs < minimum_runs
  5242. #endif
  5243. );
  5244. }
  5245. else {
  5246. bench_stats_start(&count, &start);
  5247. do {
  5248. for (i = 0; i < numBlocks; i++) {
  5249. ret = wc_Chacha_SetIV(enc, bench_iv, 0);
  5250. if (ret < 0) {
  5251. printf("wc_Chacha_SetIV error: %d\n", ret);
  5252. goto exit;
  5253. }
  5254. ret = wc_Chacha_Process(enc, bench_cipher, bench_plain,
  5255. bench_size);
  5256. if (ret < 0) {
  5257. printf("wc_Chacha_Process error: %d\n", ret);
  5258. goto exit;
  5259. }
  5260. RECORD_MULTI_VALUE_STATS();
  5261. }
  5262. count += i;
  5263. } while (bench_stats_check(start)
  5264. #ifdef MULTI_VALUE_STATISTICS
  5265. || runs < minimum_runs
  5266. #endif
  5267. );
  5268. }
  5269. bench_stats_sym_finish("CHACHA", 0, count, bench_size, start, 0);
  5270. #ifdef MULTI_VALUE_STATISTICS
  5271. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5272. #endif
  5273. exit:
  5274. WC_FREE_VAR(enc, HEAP_HINT);
  5275. }
  5276. #endif /* HAVE_CHACHA*/
  5277. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5278. void bench_chacha20_poly1305_aead(void)
  5279. {
  5280. double start;
  5281. int ret = 0, i, count;
  5282. DECLARE_MULTI_VALUE_STATS_VARS()
  5283. WC_DECLARE_VAR(authTag, byte, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, HEAP_HINT);
  5284. WC_ALLOC_VAR(authTag, byte, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, HEAP_HINT);
  5285. XMEMSET(authTag, 0, CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE);
  5286. bench_stats_start(&count, &start);
  5287. do {
  5288. for (i = 0; i < numBlocks; i++) {
  5289. ret = wc_ChaCha20Poly1305_Encrypt(bench_key, bench_iv, NULL, 0,
  5290. bench_plain, bench_size, bench_cipher, authTag);
  5291. if (ret < 0) {
  5292. printf("wc_ChaCha20Poly1305_Encrypt error: %d\n", ret);
  5293. goto exit;
  5294. }
  5295. RECORD_MULTI_VALUE_STATS();
  5296. }
  5297. count += i;
  5298. } while (bench_stats_check(start)
  5299. #ifdef MULTI_VALUE_STATISTICS
  5300. || runs < minimum_runs
  5301. #endif
  5302. );
  5303. bench_stats_sym_finish("CHA-POLY", 0, count, bench_size, start, ret);
  5304. #ifdef MULTI_VALUE_STATISTICS
  5305. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5306. #endif
  5307. exit:
  5308. WC_FREE_VAR(authTag, HEAP_HINT);
  5309. }
  5310. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  5311. #ifndef NO_MD5
  5312. void bench_md5(int useDeviceID)
  5313. {
  5314. WC_DECLARE_ARRAY(hash, wc_Md5, BENCH_MAX_PENDING,
  5315. sizeof(wc_Md5), HEAP_HINT);
  5316. double start = 0;
  5317. int ret = 0, i, count = 0, times, pending = 0;
  5318. DECLARE_MULTI_VALUE_STATS_VARS()
  5319. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5320. WC_MD5_DIGEST_SIZE, HEAP_HINT);
  5321. WC_CALLOC_ARRAY(hash, wc_Md5, BENCH_MAX_PENDING,
  5322. sizeof(wc_Md5), HEAP_HINT);
  5323. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5324. WC_MD5_DIGEST_SIZE, HEAP_HINT);
  5325. if (digest_stream) {
  5326. /* init keys */
  5327. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5328. ret = wc_InitMd5_ex(hash[i], HEAP_HINT,
  5329. useDeviceID ? devId : INVALID_DEVID);
  5330. if (ret != 0) {
  5331. printf("InitMd5_ex failed, ret = %d\n", ret);
  5332. goto exit;
  5333. }
  5334. #ifdef WOLFSSL_PIC32MZ_HASH
  5335. wc_Md5SizeSet(hash[i], numBlocks * bench_size);
  5336. #endif
  5337. }
  5338. bench_stats_start(&count, &start);
  5339. do {
  5340. for (times = 0; times < numBlocks || pending > 0; ) {
  5341. bench_async_poll(&pending);
  5342. /* while free pending slots in queue, submit ops */
  5343. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5344. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5345. 0, &times, numBlocks, &pending)) {
  5346. ret = wc_Md5Update(hash[i], bench_plain,
  5347. bench_size);
  5348. if (!bench_async_handle(&ret,
  5349. BENCH_ASYNC_GET_DEV(hash[i]),
  5350. 0, &times, &pending)) {
  5351. goto exit_md5;
  5352. }
  5353. }
  5354. } /* for i */
  5355. RECORD_MULTI_VALUE_STATS();
  5356. } /* for times */
  5357. count += times;
  5358. times = 0;
  5359. do {
  5360. bench_async_poll(&pending);
  5361. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5362. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5363. 0, &times, numBlocks, &pending)) {
  5364. ret = wc_Md5Final(hash[i], digest[i]);
  5365. if (!bench_async_handle(&ret,
  5366. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5367. &times, &pending)) {
  5368. goto exit_md5;
  5369. }
  5370. }
  5371. } /* for i */
  5372. } while (pending > 0);
  5373. } while (bench_stats_check(start)
  5374. #ifdef MULTI_VALUE_STATISTICS
  5375. || runs < minimum_runs
  5376. #endif
  5377. );
  5378. }
  5379. else {
  5380. bench_stats_start(&count, &start);
  5381. do {
  5382. for (times = 0; times < numBlocks; times++) {
  5383. ret = wc_InitMd5_ex(hash[0], HEAP_HINT, INVALID_DEVID);
  5384. if (ret == 0)
  5385. ret = wc_Md5Update(hash[0], bench_plain, bench_size);
  5386. if (ret == 0)
  5387. ret = wc_Md5Final(hash[0], digest[0]);
  5388. if (ret != 0)
  5389. goto exit_md5;
  5390. RECORD_MULTI_VALUE_STATS();
  5391. } /* for times */
  5392. count += times;
  5393. } while (bench_stats_check(start)
  5394. #ifdef MULTI_VALUE_STATISTICS
  5395. || runs < minimum_runs
  5396. #endif
  5397. );
  5398. }
  5399. exit_md5:
  5400. bench_stats_sym_finish("MD5", useDeviceID, count, bench_size, start, ret);
  5401. #ifdef MULTI_VALUE_STATISTICS
  5402. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5403. #endif
  5404. exit:
  5405. #ifdef WOLFSSL_ASYNC_CRYPT
  5406. if (WC_ARRAY_OK(hash)) {
  5407. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5408. wc_Md5Free(hash[i]);
  5409. }
  5410. }
  5411. #endif
  5412. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5413. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5414. }
  5415. #endif /* !NO_MD5 */
  5416. #ifndef NO_SHA
  5417. void bench_sha(int useDeviceID)
  5418. {
  5419. WC_DECLARE_ARRAY(hash, wc_Sha, BENCH_MAX_PENDING,
  5420. sizeof(wc_Sha), HEAP_HINT);
  5421. double start;
  5422. int ret = 0, i, count = 0, times, pending = 0;
  5423. DECLARE_MULTI_VALUE_STATS_VARS()
  5424. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5425. WC_SHA_DIGEST_SIZE, HEAP_HINT);
  5426. WC_CALLOC_ARRAY(hash, wc_Sha, BENCH_MAX_PENDING,
  5427. sizeof(wc_Sha), HEAP_HINT);
  5428. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5429. WC_SHA_DIGEST_SIZE, HEAP_HINT);
  5430. if (digest_stream) {
  5431. /* init keys */
  5432. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5433. ret = wc_InitSha_ex(hash[i], HEAP_HINT,
  5434. useDeviceID ? devId : INVALID_DEVID);
  5435. if (ret != 0) {
  5436. printf("InitSha failed, ret = %d\n", ret);
  5437. goto exit;
  5438. }
  5439. #ifdef WOLFSSL_PIC32MZ_HASH
  5440. wc_ShaSizeSet(hash[i], numBlocks * bench_size);
  5441. #endif
  5442. }
  5443. bench_stats_start(&count, &start);
  5444. do {
  5445. for (times = 0; times < numBlocks || pending > 0; ) {
  5446. bench_async_poll(&pending);
  5447. /* while free pending slots in queue, submit ops */
  5448. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5449. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5450. 0, &times, numBlocks, &pending)) {
  5451. ret = wc_ShaUpdate(hash[i], bench_plain,
  5452. bench_size);
  5453. if (!bench_async_handle(&ret,
  5454. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5455. &times, &pending)) {
  5456. goto exit_sha;
  5457. }
  5458. }
  5459. } /* for i */
  5460. RECORD_MULTI_VALUE_STATS();
  5461. } /* for times */
  5462. count += times;
  5463. times = 0;
  5464. do {
  5465. bench_async_poll(&pending);
  5466. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5467. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5468. 0, &times, numBlocks, &pending)) {
  5469. ret = wc_ShaFinal(hash[i], digest[i]);
  5470. if (!bench_async_handle(&ret,
  5471. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5472. &times, &pending)) {
  5473. goto exit_sha;
  5474. }
  5475. }
  5476. } /* for i */
  5477. } while (pending > 0);
  5478. } while (bench_stats_check(start)
  5479. #ifdef MULTI_VALUE_STATISTICS
  5480. || runs < minimum_runs
  5481. #endif
  5482. );
  5483. }
  5484. else {
  5485. bench_stats_start(&count, &start);
  5486. do {
  5487. for (times = 0; times < numBlocks; times++) {
  5488. ret = wc_InitSha_ex(hash[0], HEAP_HINT,
  5489. useDeviceID ? devId : INVALID_DEVID);
  5490. if (ret == 0)
  5491. ret = wc_ShaUpdate(hash[0], bench_plain, bench_size);
  5492. if (ret == 0)
  5493. ret = wc_ShaFinal(hash[0], digest[0]);
  5494. if (ret != 0)
  5495. goto exit_sha;
  5496. RECORD_MULTI_VALUE_STATS();
  5497. } /* for times */
  5498. count += times;
  5499. } while (bench_stats_check(start)
  5500. #ifdef MULTI_VALUE_STATISTICS
  5501. || runs < minimum_runs
  5502. #endif
  5503. );
  5504. }
  5505. exit_sha:
  5506. bench_stats_sym_finish("SHA", useDeviceID, count, bench_size, start, ret);
  5507. #ifdef MULTI_VALUE_STATISTICS
  5508. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5509. #endif
  5510. exit:
  5511. if (WC_ARRAY_OK(hash)) {
  5512. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5513. wc_ShaFree(hash[i]);
  5514. }
  5515. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5516. }
  5517. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5518. }
  5519. #endif /* NO_SHA */
  5520. #ifdef WOLFSSL_SHA224
  5521. void bench_sha224(int useDeviceID)
  5522. {
  5523. WC_DECLARE_ARRAY(hash, wc_Sha224, BENCH_MAX_PENDING,
  5524. sizeof(wc_Sha224), HEAP_HINT);
  5525. double start;
  5526. int ret = 0, i, count = 0, times, pending = 0;
  5527. DECLARE_MULTI_VALUE_STATS_VARS()
  5528. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5529. WC_SHA224_DIGEST_SIZE, HEAP_HINT);
  5530. WC_CALLOC_ARRAY(hash, wc_Sha224, BENCH_MAX_PENDING,
  5531. sizeof(wc_Sha224), HEAP_HINT);
  5532. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5533. WC_SHA224_DIGEST_SIZE, HEAP_HINT);
  5534. if (digest_stream) {
  5535. /* init keys */
  5536. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5537. ret = wc_InitSha224_ex(hash[i], HEAP_HINT,
  5538. useDeviceID ? devId : INVALID_DEVID);
  5539. if (ret != 0) {
  5540. printf("InitSha224_ex failed, ret = %d\n", ret);
  5541. goto exit;
  5542. }
  5543. }
  5544. bench_stats_start(&count, &start);
  5545. do {
  5546. for (times = 0; times < numBlocks || pending > 0; ) {
  5547. bench_async_poll(&pending);
  5548. /* while free pending slots in queue, submit ops */
  5549. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5550. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5551. 0, &times, numBlocks, &pending)) {
  5552. ret = wc_Sha224Update(hash[i], bench_plain,
  5553. bench_size);
  5554. if (!bench_async_handle(&ret,
  5555. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5556. &times, &pending)) {
  5557. goto exit_sha224;
  5558. }
  5559. }
  5560. } /* for i */
  5561. RECORD_MULTI_VALUE_STATS();
  5562. } /* for times */
  5563. count += times;
  5564. times = 0;
  5565. do {
  5566. bench_async_poll(&pending);
  5567. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5568. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5569. 0, &times, numBlocks, &pending)) {
  5570. ret = wc_Sha224Final(hash[i], digest[i]);
  5571. if (!bench_async_handle(&ret,
  5572. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5573. &times, &pending)) {
  5574. goto exit_sha224;
  5575. }
  5576. }
  5577. } /* for i */
  5578. } while (pending > 0);
  5579. } while (bench_stats_check(start)
  5580. #ifdef MULTI_VALUE_STATISTICS
  5581. || runs < minimum_runs
  5582. #endif
  5583. );
  5584. }
  5585. else {
  5586. bench_stats_start(&count, &start);
  5587. do {
  5588. for (times = 0; times < numBlocks; times++) {
  5589. ret = wc_InitSha224_ex(hash[0], HEAP_HINT,
  5590. useDeviceID ? devId : INVALID_DEVID);
  5591. if (ret == 0)
  5592. ret = wc_Sha224Update(hash[0], bench_plain, bench_size);
  5593. if (ret == 0)
  5594. ret = wc_Sha224Final(hash[0], digest[0]);
  5595. if (ret != 0)
  5596. goto exit_sha224;
  5597. } /* for times */
  5598. count += times;
  5599. } while (bench_stats_check(start)
  5600. #ifdef MULTI_VALUE_STATISTICS
  5601. || runs < minimum_runs
  5602. #endif
  5603. );
  5604. }
  5605. exit_sha224:
  5606. bench_stats_sym_finish("SHA-224", useDeviceID, count,
  5607. bench_size, start, ret);
  5608. #ifdef MULTI_VALUE_STATISTICS
  5609. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5610. #endif
  5611. exit:
  5612. if (WC_ARRAY_OK(hash)) {
  5613. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5614. wc_Sha224Free(hash[i]);
  5615. }
  5616. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5617. }
  5618. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5619. }
  5620. #endif
  5621. #ifndef NO_SHA256
  5622. void bench_sha256(int useDeviceID)
  5623. {
  5624. WC_DECLARE_ARRAY(hash, wc_Sha256, BENCH_MAX_PENDING,
  5625. sizeof(wc_Sha256), HEAP_HINT);
  5626. double start;
  5627. int ret = 0, i, count = 0, times, pending = 0;
  5628. DECLARE_MULTI_VALUE_STATS_VARS()
  5629. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5630. WC_SHA256_DIGEST_SIZE, HEAP_HINT);
  5631. WC_CALLOC_ARRAY(hash, wc_Sha256, BENCH_MAX_PENDING,
  5632. sizeof(wc_Sha256), HEAP_HINT);
  5633. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5634. WC_SHA256_DIGEST_SIZE, HEAP_HINT);
  5635. if (digest_stream) {
  5636. /* init keys */
  5637. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5638. ret = wc_InitSha256_ex(hash[i], HEAP_HINT,
  5639. useDeviceID ? devId: INVALID_DEVID);
  5640. if (ret != 0) {
  5641. printf("InitSha256_ex failed, ret = %d\n", ret);
  5642. goto exit;
  5643. }
  5644. #ifdef WOLFSSL_PIC32MZ_HASH
  5645. wc_Sha256SizeSet(hash[i], numBlocks * bench_size);
  5646. #endif
  5647. }
  5648. bench_stats_start(&count, &start);
  5649. do {
  5650. for (times = 0; times < numBlocks || pending > 0; ) {
  5651. bench_async_poll(&pending);
  5652. /* while free pending slots in queue, submit ops */
  5653. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5654. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5655. 0, &times, numBlocks, &pending)) {
  5656. ret = wc_Sha256Update(hash[i], bench_plain,
  5657. bench_size);
  5658. if (!bench_async_handle(&ret,
  5659. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5660. &times, &pending)) {
  5661. goto exit_sha256;
  5662. }
  5663. }
  5664. } /* for i */
  5665. RECORD_MULTI_VALUE_STATS();
  5666. } /* for times */
  5667. count += times;
  5668. times = 0;
  5669. do {
  5670. bench_async_poll(&pending);
  5671. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5672. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5673. 0, &times, numBlocks, &pending)) {
  5674. ret = wc_Sha256Final(hash[i], digest[i]);
  5675. if (!bench_async_handle(&ret,
  5676. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5677. &times, &pending)) {
  5678. goto exit_sha256;
  5679. }
  5680. }
  5681. } /* for i */
  5682. } while (pending > 0);
  5683. } while (bench_stats_check(start)
  5684. #ifdef MULTI_VALUE_STATISTICS
  5685. || runs < minimum_runs
  5686. #endif
  5687. );
  5688. }
  5689. else {
  5690. bench_stats_start(&count, &start);
  5691. do {
  5692. for (times = 0; times < numBlocks; times++) {
  5693. ret = wc_InitSha256_ex(hash[0], HEAP_HINT,
  5694. useDeviceID ? devId: INVALID_DEVID);
  5695. if (ret == 0)
  5696. ret = wc_Sha256Update(hash[0], bench_plain, bench_size);
  5697. if (ret == 0)
  5698. ret = wc_Sha256Final(hash[0], digest[0]);
  5699. if (ret != 0)
  5700. goto exit_sha256;
  5701. RECORD_MULTI_VALUE_STATS();
  5702. } /* for times */
  5703. count += times;
  5704. } while (bench_stats_check(start)
  5705. #ifdef MULTI_VALUE_STATISTICS
  5706. || runs < minimum_runs
  5707. #endif
  5708. );
  5709. }
  5710. exit_sha256:
  5711. bench_stats_sym_finish("SHA-256", useDeviceID, count, bench_size,
  5712. start, ret);
  5713. #ifdef MULTI_VALUE_STATISTICS
  5714. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5715. #endif
  5716. exit:
  5717. if (WC_ARRAY_OK(hash)) {
  5718. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5719. wc_Sha256Free(hash[i]);
  5720. }
  5721. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5722. }
  5723. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5724. }
  5725. #endif
  5726. #ifdef WOLFSSL_SHA384
  5727. void bench_sha384(int useDeviceID)
  5728. {
  5729. WC_DECLARE_ARRAY(hash, wc_Sha384, BENCH_MAX_PENDING,
  5730. sizeof(wc_Sha384), HEAP_HINT);
  5731. double start;
  5732. int ret = 0, i, count = 0, times, pending = 0;
  5733. DECLARE_MULTI_VALUE_STATS_VARS()
  5734. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5735. WC_SHA384_DIGEST_SIZE, HEAP_HINT);
  5736. WC_CALLOC_ARRAY(hash, wc_Sha384, BENCH_MAX_PENDING,
  5737. sizeof(wc_Sha384), HEAP_HINT);
  5738. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5739. WC_SHA384_DIGEST_SIZE, HEAP_HINT);
  5740. if (digest_stream) {
  5741. /* init keys */
  5742. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5743. ret = wc_InitSha384_ex(hash[i], HEAP_HINT,
  5744. useDeviceID ? devId : INVALID_DEVID);
  5745. if (ret != 0) {
  5746. printf("InitSha384_ex failed, ret = %d\n", ret);
  5747. goto exit;
  5748. }
  5749. }
  5750. bench_stats_start(&count, &start);
  5751. do {
  5752. for (times = 0; times < numBlocks || pending > 0; ) {
  5753. bench_async_poll(&pending);
  5754. /* while free pending slots in queue, submit ops */
  5755. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5756. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5757. 0, &times, numBlocks, &pending)) {
  5758. ret = wc_Sha384Update(hash[i], bench_plain,
  5759. bench_size);
  5760. if (!bench_async_handle(&ret,
  5761. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5762. &times, &pending)) {
  5763. goto exit_sha384;
  5764. }
  5765. }
  5766. } /* for i */
  5767. RECORD_MULTI_VALUE_STATS();
  5768. } /* for times */
  5769. count += times;
  5770. times = 0;
  5771. do {
  5772. bench_async_poll(&pending);
  5773. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5774. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5775. 0, &times, numBlocks, &pending)) {
  5776. ret = wc_Sha384Final(hash[i], digest[i]);
  5777. if (!bench_async_handle(&ret,
  5778. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5779. &times, &pending)) {
  5780. goto exit_sha384;
  5781. }
  5782. }
  5783. } /* for i */
  5784. } while (pending > 0);
  5785. } while (bench_stats_check(start)
  5786. #ifdef MULTI_VALUE_STATISTICS
  5787. || runs < minimum_runs
  5788. #endif
  5789. );
  5790. }
  5791. else {
  5792. bench_stats_start(&count, &start);
  5793. do {
  5794. for (times = 0; times < numBlocks; times++) {
  5795. ret = wc_InitSha384_ex(hash[0], HEAP_HINT,
  5796. useDeviceID ? devId : INVALID_DEVID);
  5797. if (ret == 0)
  5798. ret = wc_Sha384Update(hash[0], bench_plain, bench_size);
  5799. if (ret == 0)
  5800. ret = wc_Sha384Final(hash[0], digest[0]);
  5801. if (ret != 0)
  5802. goto exit_sha384;
  5803. RECORD_MULTI_VALUE_STATS();
  5804. } /* for times */
  5805. count += times;
  5806. } while (bench_stats_check(start)
  5807. #ifdef MULTI_VALUE_STATISTICS
  5808. || runs < minimum_runs
  5809. #endif
  5810. );
  5811. }
  5812. exit_sha384:
  5813. bench_stats_sym_finish("SHA-384", useDeviceID, count, bench_size,
  5814. start, ret);
  5815. #ifdef MULTI_VALUE_STATISTICS
  5816. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5817. #endif
  5818. exit:
  5819. if (WC_ARRAY_OK(hash)) {
  5820. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5821. wc_Sha384Free(hash[i]);
  5822. }
  5823. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5824. }
  5825. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5826. }
  5827. #endif
  5828. #ifdef WOLFSSL_SHA512
  5829. void bench_sha512(int useDeviceID)
  5830. {
  5831. WC_DECLARE_ARRAY(hash, wc_Sha512, BENCH_MAX_PENDING,
  5832. sizeof(wc_Sha512), HEAP_HINT);
  5833. double start;
  5834. int ret = 0, i, count = 0, times, pending = 0;
  5835. DECLARE_MULTI_VALUE_STATS_VARS()
  5836. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5837. WC_SHA512_DIGEST_SIZE, HEAP_HINT);
  5838. WC_CALLOC_ARRAY(hash, wc_Sha512, BENCH_MAX_PENDING,
  5839. sizeof(wc_Sha512), HEAP_HINT);
  5840. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5841. WC_SHA512_DIGEST_SIZE, HEAP_HINT);
  5842. if (digest_stream) {
  5843. /* init keys */
  5844. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5845. ret = wc_InitSha512_ex(hash[i], HEAP_HINT,
  5846. useDeviceID ? devId : INVALID_DEVID);
  5847. if (ret != 0) {
  5848. printf("InitSha512_ex failed, ret = %d\n", ret);
  5849. goto exit;
  5850. }
  5851. }
  5852. bench_stats_start(&count, &start);
  5853. do {
  5854. for (times = 0; times < numBlocks || pending > 0; ) {
  5855. bench_async_poll(&pending);
  5856. /* while free pending slots in queue, submit ops */
  5857. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5858. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5859. 0, &times, numBlocks, &pending)) {
  5860. ret = wc_Sha512Update(hash[i], bench_plain,
  5861. bench_size);
  5862. if (!bench_async_handle(&ret,
  5863. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5864. &times, &pending)) {
  5865. goto exit_sha512;
  5866. }
  5867. }
  5868. } /* for i */
  5869. RECORD_MULTI_VALUE_STATS();
  5870. } /* for times */
  5871. count += times;
  5872. times = 0;
  5873. do {
  5874. bench_async_poll(&pending);
  5875. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5876. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5877. 0, &times, numBlocks, &pending)) {
  5878. ret = wc_Sha512Final(hash[i], digest[i]);
  5879. if (!bench_async_handle(&ret,
  5880. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5881. &times, &pending)) {
  5882. goto exit_sha512;
  5883. }
  5884. }
  5885. } /* for i */
  5886. } while (pending > 0);
  5887. } while (bench_stats_check(start)
  5888. #ifdef MULTI_VALUE_STATISTICS
  5889. || runs < minimum_runs
  5890. #endif
  5891. );
  5892. }
  5893. else {
  5894. bench_stats_start(&count, &start);
  5895. do {
  5896. for (times = 0; times < numBlocks; times++) {
  5897. ret = wc_InitSha512_ex(hash[0], HEAP_HINT,
  5898. useDeviceID ? devId : INVALID_DEVID);
  5899. if (ret == 0)
  5900. ret = wc_Sha512Update(hash[0], bench_plain, bench_size);
  5901. if (ret == 0)
  5902. ret = wc_Sha512Final(hash[0], digest[0]);
  5903. if (ret != 0)
  5904. goto exit_sha512;
  5905. RECORD_MULTI_VALUE_STATS();
  5906. } /* for times */
  5907. count += times;
  5908. } while (bench_stats_check(start)
  5909. #ifdef MULTI_VALUE_STATISTICS
  5910. || runs < minimum_runs
  5911. #endif
  5912. );
  5913. }
  5914. exit_sha512:
  5915. bench_stats_sym_finish("SHA-512", useDeviceID, count, bench_size,
  5916. start, ret);
  5917. #ifdef MULTI_VALUE_STATISTICS
  5918. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5919. #endif
  5920. exit:
  5921. if (WC_ARRAY_OK(hash)) {
  5922. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5923. wc_Sha512Free(hash[i]);
  5924. }
  5925. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5926. }
  5927. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5928. }
  5929. #if !defined(WOLFSSL_NOSHA512_224) && \
  5930. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  5931. void bench_sha512_224(int useDeviceID)
  5932. {
  5933. WC_DECLARE_ARRAY(hash, wc_Sha512_224, BENCH_MAX_PENDING,
  5934. sizeof(wc_Sha512_224), HEAP_HINT);
  5935. double start;
  5936. int ret = 0, i, count = 0, times, pending = 0;
  5937. DECLARE_MULTI_VALUE_STATS_VARS()
  5938. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5939. WC_SHA512_224_DIGEST_SIZE, HEAP_HINT);
  5940. WC_CALLOC_ARRAY(hash, wc_Sha512_224, BENCH_MAX_PENDING,
  5941. sizeof(wc_Sha512_224), HEAP_HINT);
  5942. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5943. WC_SHA512_224_DIGEST_SIZE, HEAP_HINT);
  5944. if (digest_stream) {
  5945. /* init keys */
  5946. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5947. ret = wc_InitSha512_224_ex(hash[i], HEAP_HINT,
  5948. useDeviceID ? devId : INVALID_DEVID);
  5949. if (ret != 0) {
  5950. printf("InitSha512_224_ex failed, ret = %d\n", ret);
  5951. goto exit;
  5952. }
  5953. }
  5954. bench_stats_start(&count, &start);
  5955. do {
  5956. for (times = 0; times < numBlocks || pending > 0; ) {
  5957. bench_async_poll(&pending);
  5958. /* while free pending slots in queue, submit ops */
  5959. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5960. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5961. 0, &times, numBlocks, &pending)) {
  5962. ret = wc_Sha512_224Update(hash[i], bench_plain,
  5963. bench_size);
  5964. if (!bench_async_handle(&ret,
  5965. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5966. &times, &pending)) {
  5967. goto exit_sha512_224;
  5968. }
  5969. }
  5970. } /* for i */
  5971. RECORD_MULTI_VALUE_STATS();
  5972. } /* for times */
  5973. count += times;
  5974. times = 0;
  5975. do {
  5976. bench_async_poll(&pending);
  5977. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5978. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5979. 0, &times, numBlocks, &pending)) {
  5980. ret = wc_Sha512_224Final(hash[i], digest[i]);
  5981. if (!bench_async_handle(&ret,
  5982. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5983. &times, &pending)) {
  5984. goto exit_sha512_224;
  5985. }
  5986. }
  5987. } /* for i */
  5988. } while (pending > 0);
  5989. } while (bench_stats_check(start)
  5990. #ifdef MULTI_VALUE_STATISTICS
  5991. || runs < minimum_runs
  5992. #endif
  5993. );
  5994. }
  5995. else {
  5996. bench_stats_start(&count, &start);
  5997. do {
  5998. for (times = 0; times < numBlocks; times++) {
  5999. ret = wc_InitSha512_224_ex(hash[0], HEAP_HINT,
  6000. useDeviceID ? devId : INVALID_DEVID);
  6001. if (ret == 0)
  6002. ret = wc_Sha512_224Update(hash[0], bench_plain, bench_size);
  6003. if (ret == 0)
  6004. ret = wc_Sha512_224Final(hash[0], digest[0]);
  6005. if (ret != 0)
  6006. goto exit_sha512_224;
  6007. RECORD_MULTI_VALUE_STATS();
  6008. } /* for times */
  6009. count += times;
  6010. } while (bench_stats_check(start)
  6011. #ifdef MULTI_VALUE_STATISTICS
  6012. || runs < minimum_runs
  6013. #endif
  6014. );
  6015. }
  6016. exit_sha512_224:
  6017. bench_stats_sym_finish("SHA-512/224", useDeviceID, count, bench_size,
  6018. start, ret);
  6019. #ifdef MULTI_VALUE_STATISTICS
  6020. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6021. #endif
  6022. exit:
  6023. if (WC_ARRAY_OK(hash)) {
  6024. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6025. wc_Sha512_224Free(hash[i]);
  6026. }
  6027. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6028. }
  6029. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6030. }
  6031. #endif /* WOLFSSL_NOSHA512_224 && !FIPS ... */
  6032. #if !defined(WOLFSSL_NOSHA512_256) && \
  6033. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  6034. void bench_sha512_256(int useDeviceID)
  6035. {
  6036. WC_DECLARE_ARRAY(hash, wc_Sha512_256, BENCH_MAX_PENDING,
  6037. sizeof(wc_Sha512_256), HEAP_HINT);
  6038. double start;
  6039. int ret = 0, i, count = 0, times, pending = 0;
  6040. DECLARE_MULTI_VALUE_STATS_VARS()
  6041. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6042. WC_SHA512_256_DIGEST_SIZE, HEAP_HINT);
  6043. WC_CALLOC_ARRAY(hash, wc_Sha512_256, BENCH_MAX_PENDING,
  6044. sizeof(wc_Sha512_256), HEAP_HINT);
  6045. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6046. WC_SHA512_256_DIGEST_SIZE, HEAP_HINT);
  6047. if (digest_stream) {
  6048. /* init keys */
  6049. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6050. ret = wc_InitSha512_256_ex(hash[i], HEAP_HINT,
  6051. useDeviceID ? devId : INVALID_DEVID);
  6052. if (ret != 0) {
  6053. printf("InitSha512_256_ex failed, ret = %d\n", ret);
  6054. goto exit;
  6055. }
  6056. }
  6057. bench_stats_start(&count, &start);
  6058. do {
  6059. for (times = 0; times < numBlocks || pending > 0; ) {
  6060. bench_async_poll(&pending);
  6061. /* while free pending slots in queue, submit ops */
  6062. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6063. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6064. 0, &times, numBlocks, &pending)) {
  6065. ret = wc_Sha512_256Update(hash[i], bench_plain,
  6066. bench_size);
  6067. if (!bench_async_handle(&ret,
  6068. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6069. &times, &pending)) {
  6070. goto exit_sha512_256;
  6071. }
  6072. }
  6073. } /* for i */
  6074. RECORD_MULTI_VALUE_STATS();
  6075. } /* for times */
  6076. count += times;
  6077. times = 0;
  6078. do {
  6079. bench_async_poll(&pending);
  6080. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6081. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6082. 0, &times, numBlocks, &pending)) {
  6083. ret = wc_Sha512_256Final(hash[i], digest[i]);
  6084. if (!bench_async_handle(&ret,
  6085. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6086. &times, &pending)) {
  6087. goto exit_sha512_256;
  6088. }
  6089. }
  6090. } /* for i */
  6091. } while (pending > 0);
  6092. } while (bench_stats_check(start)
  6093. #ifdef MULTI_VALUE_STATISTICS
  6094. || runs < minimum_runs
  6095. #endif
  6096. );
  6097. }
  6098. else {
  6099. bench_stats_start(&count, &start);
  6100. do {
  6101. for (times = 0; times < numBlocks; times++) {
  6102. ret = wc_InitSha512_256_ex(hash[0], HEAP_HINT,
  6103. useDeviceID ? devId : INVALID_DEVID);
  6104. if (ret == 0)
  6105. ret = wc_Sha512_256Update(hash[0], bench_plain, bench_size);
  6106. if (ret == 0)
  6107. ret = wc_Sha512_256Final(hash[0], digest[0]);
  6108. if (ret != 0)
  6109. goto exit_sha512_256;
  6110. RECORD_MULTI_VALUE_STATS();
  6111. } /* for times */
  6112. count += times;
  6113. } while (bench_stats_check(start)
  6114. #ifdef MULTI_VALUE_STATISTICS
  6115. || runs < minimum_runs
  6116. #endif
  6117. );
  6118. }
  6119. exit_sha512_256:
  6120. bench_stats_sym_finish("SHA-512/256", useDeviceID, count, bench_size,
  6121. start, ret);
  6122. #ifdef MULTI_VALUE_STATISTICS
  6123. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6124. #endif
  6125. exit:
  6126. if (WC_ARRAY_OK(hash)) {
  6127. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6128. wc_Sha512_256Free(hash[i]);
  6129. }
  6130. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6131. }
  6132. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6133. }
  6134. #endif /* WOLFSSL_NOSHA512_256 && !FIPS ... */
  6135. #endif /* WOLFSSL_SHA512 */
  6136. #ifdef WOLFSSL_SHA3
  6137. #ifndef WOLFSSL_NOSHA3_224
  6138. void bench_sha3_224(int useDeviceID)
  6139. {
  6140. WC_DECLARE_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6141. sizeof(wc_Sha3), HEAP_HINT);
  6142. double start;
  6143. int ret = 0, i, count = 0, times, pending = 0;
  6144. DECLARE_MULTI_VALUE_STATS_VARS()
  6145. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6146. WC_SHA3_224_DIGEST_SIZE, HEAP_HINT);
  6147. WC_CALLOC_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6148. sizeof(wc_Sha3), HEAP_HINT);
  6149. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6150. WC_SHA3_224_DIGEST_SIZE, HEAP_HINT);
  6151. if (digest_stream) {
  6152. /* init keys */
  6153. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6154. ret = wc_InitSha3_224(hash[i], HEAP_HINT,
  6155. useDeviceID ? devId : INVALID_DEVID);
  6156. if (ret != 0) {
  6157. printf("InitSha3_224 failed, ret = %d\n", ret);
  6158. goto exit;
  6159. }
  6160. }
  6161. bench_stats_start(&count, &start);
  6162. do {
  6163. for (times = 0; times < numBlocks || pending > 0; ) {
  6164. bench_async_poll(&pending);
  6165. /* while free pending slots in queue, submit ops */
  6166. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6167. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6168. 0, &times, numBlocks, &pending)) {
  6169. ret = wc_Sha3_224_Update(hash[i], bench_plain,
  6170. bench_size);
  6171. if (!bench_async_handle(&ret,
  6172. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6173. &times, &pending)) {
  6174. goto exit_sha3_224;
  6175. }
  6176. }
  6177. } /* for i */
  6178. RECORD_MULTI_VALUE_STATS();
  6179. } /* for times */
  6180. count += times;
  6181. times = 0;
  6182. do {
  6183. bench_async_poll(&pending);
  6184. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6185. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6186. 0, &times, numBlocks, &pending)) {
  6187. ret = wc_Sha3_224_Final(hash[i], digest[i]);
  6188. if (!bench_async_handle(&ret,
  6189. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6190. &times, &pending)) {
  6191. goto exit_sha3_224;
  6192. }
  6193. }
  6194. } /* for i */
  6195. } while (pending > 0);
  6196. } while (bench_stats_check(start)
  6197. #ifdef MULTI_VALUE_STATISTICS
  6198. || runs < minimum_runs
  6199. #endif
  6200. );
  6201. }
  6202. else {
  6203. bench_stats_start(&count, &start);
  6204. do {
  6205. for (times = 0; times < numBlocks; times++) {
  6206. ret = wc_InitSha3_224(hash[0], HEAP_HINT,
  6207. useDeviceID ? devId : INVALID_DEVID);
  6208. if (ret == 0)
  6209. ret = wc_Sha3_224_Update(hash[0], bench_plain, bench_size);
  6210. if (ret == 0)
  6211. ret = wc_Sha3_224_Final(hash[0], digest[0]);
  6212. if (ret != 0)
  6213. goto exit_sha3_224;
  6214. RECORD_MULTI_VALUE_STATS();
  6215. } /* for times */
  6216. count += times;
  6217. } while (bench_stats_check(start)
  6218. #ifdef MULTI_VALUE_STATISTICS
  6219. || runs < minimum_runs
  6220. #endif
  6221. );
  6222. }
  6223. exit_sha3_224:
  6224. bench_stats_sym_finish("SHA3-224", useDeviceID, count, bench_size,
  6225. start, ret);
  6226. #ifdef MULTI_VALUE_STATISTICS
  6227. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6228. #endif
  6229. exit:
  6230. if (WC_ARRAY_OK(hash)) {
  6231. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6232. wc_Sha3_224_Free(hash[i]);
  6233. }
  6234. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6235. }
  6236. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6237. }
  6238. #endif /* WOLFSSL_NOSHA3_224 */
  6239. #ifndef WOLFSSL_NOSHA3_256
  6240. void bench_sha3_256(int useDeviceID)
  6241. {
  6242. WC_DECLARE_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6243. sizeof(wc_Sha3), HEAP_HINT);
  6244. double start;
  6245. DECLARE_MULTI_VALUE_STATS_VARS()
  6246. int ret = 0, i, count = 0, times, pending = 0;
  6247. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6248. WC_SHA3_256_DIGEST_SIZE, HEAP_HINT);
  6249. WC_CALLOC_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6250. sizeof(wc_Sha3), HEAP_HINT);
  6251. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6252. WC_SHA3_256_DIGEST_SIZE, HEAP_HINT);
  6253. if (digest_stream) {
  6254. /* init keys */
  6255. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6256. ret = wc_InitSha3_256(hash[i], HEAP_HINT,
  6257. useDeviceID ? devId : INVALID_DEVID);
  6258. if (ret != 0) {
  6259. printf("InitSha3_256 failed, ret = %d\n", ret);
  6260. goto exit;
  6261. }
  6262. }
  6263. bench_stats_start(&count, &start);
  6264. do {
  6265. for (times = 0; times < numBlocks || pending > 0; ) {
  6266. bench_async_poll(&pending);
  6267. /* while free pending slots in queue, submit ops */
  6268. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6269. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6270. 0, &times, numBlocks, &pending)) {
  6271. ret = wc_Sha3_256_Update(hash[i], bench_plain,
  6272. bench_size);
  6273. if (!bench_async_handle(&ret,
  6274. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6275. &times, &pending)) {
  6276. goto exit_sha3_256;
  6277. }
  6278. }
  6279. } /* for i */
  6280. RECORD_MULTI_VALUE_STATS();
  6281. } /* for times */
  6282. count += times;
  6283. times = 0;
  6284. do {
  6285. bench_async_poll(&pending);
  6286. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6287. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6288. 0, &times, numBlocks, &pending)) {
  6289. ret = wc_Sha3_256_Final(hash[i], digest[i]);
  6290. if (!bench_async_handle(&ret,
  6291. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6292. &times, &pending)) {
  6293. goto exit_sha3_256;
  6294. }
  6295. }
  6296. } /* for i */
  6297. } while (pending > 0);
  6298. } while (bench_stats_check(start)
  6299. #ifdef MULTI_VALUE_STATISTICS
  6300. || runs < minimum_runs
  6301. #endif
  6302. );
  6303. }
  6304. else {
  6305. bench_stats_start(&count, &start);
  6306. do {
  6307. for (times = 0; times < numBlocks; times++) {
  6308. ret = wc_InitSha3_256(hash[0], HEAP_HINT,
  6309. useDeviceID ? devId : INVALID_DEVID);
  6310. if (ret == 0)
  6311. ret = wc_Sha3_256_Update(hash[0], bench_plain, bench_size);
  6312. if (ret == 0)
  6313. ret = wc_Sha3_256_Final(hash[0], digest[0]);
  6314. if (ret != 0)
  6315. goto exit_sha3_256;
  6316. RECORD_MULTI_VALUE_STATS();
  6317. } /* for times */
  6318. count += times;
  6319. } while (bench_stats_check(start)
  6320. #ifdef MULTI_VALUE_STATISTICS
  6321. || runs < minimum_runs
  6322. #endif
  6323. );
  6324. }
  6325. exit_sha3_256:
  6326. bench_stats_sym_finish("SHA3-256", useDeviceID, count, bench_size,
  6327. start, ret);
  6328. #ifdef MULTI_VALUE_STATISTICS
  6329. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6330. #endif
  6331. exit:
  6332. if (WC_ARRAY_OK(hash)) {
  6333. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6334. wc_Sha3_256_Free(hash[i]);
  6335. }
  6336. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6337. }
  6338. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6339. }
  6340. #endif /* WOLFSSL_NOSHA3_256 */
  6341. #ifndef WOLFSSL_NOSHA3_384
  6342. void bench_sha3_384(int useDeviceID)
  6343. {
  6344. WC_DECLARE_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6345. sizeof(wc_Sha3), HEAP_HINT);
  6346. double start;
  6347. int ret = 0, i, count = 0, times, pending = 0;
  6348. DECLARE_MULTI_VALUE_STATS_VARS()
  6349. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6350. WC_SHA3_384_DIGEST_SIZE, HEAP_HINT);
  6351. WC_CALLOC_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6352. sizeof(wc_Sha3), HEAP_HINT);
  6353. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6354. WC_SHA3_384_DIGEST_SIZE, HEAP_HINT);
  6355. if (digest_stream) {
  6356. /* init keys */
  6357. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6358. ret = wc_InitSha3_384(hash[i], HEAP_HINT,
  6359. useDeviceID ? devId : INVALID_DEVID);
  6360. if (ret != 0) {
  6361. printf("InitSha3_384 failed, ret = %d\n", ret);
  6362. goto exit;
  6363. }
  6364. }
  6365. bench_stats_start(&count, &start);
  6366. do {
  6367. for (times = 0; times < numBlocks || pending > 0; ) {
  6368. bench_async_poll(&pending);
  6369. /* while free pending slots in queue, submit ops */
  6370. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6371. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6372. 0, &times, numBlocks, &pending)) {
  6373. ret = wc_Sha3_384_Update(hash[i], bench_plain,
  6374. bench_size);
  6375. if (!bench_async_handle(&ret,
  6376. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6377. &times, &pending)) {
  6378. goto exit_sha3_384;
  6379. }
  6380. }
  6381. } /* for i */
  6382. RECORD_MULTI_VALUE_STATS();
  6383. } /* for times */
  6384. count += times;
  6385. times = 0;
  6386. do {
  6387. bench_async_poll(&pending);
  6388. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6389. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6390. 0, &times, numBlocks, &pending)) {
  6391. ret = wc_Sha3_384_Final(hash[i], digest[i]);
  6392. if (!bench_async_handle(&ret,
  6393. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6394. &times, &pending)) {
  6395. goto exit_sha3_384;
  6396. }
  6397. }
  6398. } /* for i */
  6399. } while (pending > 0);
  6400. } while (bench_stats_check(start)
  6401. #ifdef MULTI_VALUE_STATISTICS
  6402. || runs < minimum_runs
  6403. #endif
  6404. );
  6405. }
  6406. else {
  6407. bench_stats_start(&count, &start);
  6408. do {
  6409. for (times = 0; times < numBlocks; times++) {
  6410. ret = wc_InitSha3_384(hash[0], HEAP_HINT,
  6411. useDeviceID ? devId : INVALID_DEVID);
  6412. if (ret == 0)
  6413. ret = wc_Sha3_384_Update(hash[0], bench_plain, bench_size);
  6414. if (ret == 0)
  6415. ret = wc_Sha3_384_Final(hash[0], digest[0]);
  6416. if (ret != 0)
  6417. goto exit_sha3_384;
  6418. RECORD_MULTI_VALUE_STATS();
  6419. } /* for times */
  6420. count += times;
  6421. } while (bench_stats_check(start)
  6422. #ifdef MULTI_VALUE_STATISTICS
  6423. || runs < minimum_runs
  6424. #endif
  6425. );
  6426. }
  6427. exit_sha3_384:
  6428. bench_stats_sym_finish("SHA3-384", useDeviceID, count, bench_size,
  6429. start, ret);
  6430. #ifdef MULTI_VALUE_STATISTICS
  6431. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6432. #endif
  6433. exit:
  6434. if (WC_ARRAY_OK(hash)) {
  6435. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6436. wc_Sha3_384_Free(hash[i]);
  6437. }
  6438. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6439. }
  6440. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6441. }
  6442. #endif /* WOLFSSL_NOSHA3_384 */
  6443. #ifndef WOLFSSL_NOSHA3_512
  6444. void bench_sha3_512(int useDeviceID)
  6445. {
  6446. WC_DECLARE_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6447. sizeof(wc_Sha3), HEAP_HINT);
  6448. double start;
  6449. int ret = 0, i, count = 0, times, pending = 0;
  6450. DECLARE_MULTI_VALUE_STATS_VARS()
  6451. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6452. WC_SHA3_512_DIGEST_SIZE, HEAP_HINT);
  6453. WC_CALLOC_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6454. sizeof(wc_Sha3), HEAP_HINT);
  6455. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6456. WC_SHA3_512_DIGEST_SIZE, HEAP_HINT);
  6457. if (digest_stream) {
  6458. /* init keys */
  6459. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6460. ret = wc_InitSha3_512(hash[i], HEAP_HINT,
  6461. useDeviceID ? devId : INVALID_DEVID);
  6462. if (ret != 0) {
  6463. printf("InitSha3_512 failed, ret = %d\n", ret);
  6464. goto exit;
  6465. }
  6466. }
  6467. bench_stats_start(&count, &start);
  6468. do {
  6469. for (times = 0; times < numBlocks || pending > 0; ) {
  6470. bench_async_poll(&pending);
  6471. /* while free pending slots in queue, submit ops */
  6472. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6473. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6474. 0, &times, numBlocks, &pending)) {
  6475. ret = wc_Sha3_512_Update(hash[i], bench_plain,
  6476. bench_size);
  6477. if (!bench_async_handle(&ret,
  6478. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6479. &times, &pending)) {
  6480. goto exit_sha3_512;
  6481. }
  6482. }
  6483. } /* for i */
  6484. RECORD_MULTI_VALUE_STATS();
  6485. } /* for times */
  6486. count += times;
  6487. times = 0;
  6488. do {
  6489. bench_async_poll(&pending);
  6490. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6491. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6492. 0, &times, numBlocks, &pending)) {
  6493. ret = wc_Sha3_512_Final(hash[i], digest[i]);
  6494. if (!bench_async_handle(&ret,
  6495. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6496. &times, &pending)) {
  6497. goto exit_sha3_512;
  6498. }
  6499. }
  6500. } /* for i */
  6501. } while (pending > 0);
  6502. } while (bench_stats_check(start)
  6503. #ifdef MULTI_VALUE_STATISTICS
  6504. || runs < minimum_runs
  6505. #endif
  6506. );
  6507. }
  6508. else {
  6509. bench_stats_start(&count, &start);
  6510. do {
  6511. for (times = 0; times < numBlocks; times++) {
  6512. ret = wc_InitSha3_512(hash[0], HEAP_HINT,
  6513. useDeviceID ? devId : INVALID_DEVID);
  6514. if (ret == 0)
  6515. ret = wc_Sha3_512_Update(hash[0], bench_plain, bench_size);
  6516. if (ret == 0)
  6517. ret = wc_Sha3_512_Final(hash[0], digest[0]);
  6518. if (ret != 0)
  6519. goto exit_sha3_512;
  6520. RECORD_MULTI_VALUE_STATS();
  6521. } /* for times */
  6522. count += times;
  6523. } while (bench_stats_check(start)
  6524. #ifdef MULTI_VALUE_STATISTICS
  6525. || runs < minimum_runs
  6526. #endif
  6527. );
  6528. }
  6529. exit_sha3_512:
  6530. bench_stats_sym_finish("SHA3-512", useDeviceID, count, bench_size,
  6531. start, ret);
  6532. #ifdef MULTI_VALUE_STATISTICS
  6533. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6534. #endif
  6535. exit:
  6536. if (WC_ARRAY_OK(hash)) {
  6537. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6538. wc_Sha3_512_Free(hash[i]);
  6539. }
  6540. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6541. }
  6542. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6543. }
  6544. #endif /* WOLFSSL_NOSHA3_512 */
  6545. #ifdef WOLFSSL_SHAKE128
  6546. void bench_shake128(int useDeviceID)
  6547. {
  6548. WC_DECLARE_ARRAY(hash, wc_Shake, BENCH_MAX_PENDING,
  6549. sizeof(wc_Shake), HEAP_HINT);
  6550. double start;
  6551. int ret = 0, i, count = 0, times, pending = 0;
  6552. DECLARE_MULTI_VALUE_STATS_VARS()
  6553. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6554. WC_SHA3_128_BLOCK_SIZE, HEAP_HINT);
  6555. WC_CALLOC_ARRAY(hash, wc_Shake, BENCH_MAX_PENDING,
  6556. sizeof(wc_Shake), HEAP_HINT);
  6557. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6558. WC_SHA3_128_BLOCK_SIZE, HEAP_HINT);
  6559. if (digest_stream) {
  6560. /* init keys */
  6561. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6562. ret = wc_InitShake128(hash[i], HEAP_HINT,
  6563. useDeviceID ? devId : INVALID_DEVID);
  6564. if (ret != 0) {
  6565. printf("InitShake128 failed, ret = %d\n", ret);
  6566. goto exit;
  6567. }
  6568. }
  6569. bench_stats_start(&count, &start);
  6570. do {
  6571. for (times = 0; times < numBlocks || pending > 0; ) {
  6572. bench_async_poll(&pending);
  6573. /* while free pending slots in queue, submit ops */
  6574. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6575. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6576. 0, &times, numBlocks, &pending)) {
  6577. ret = wc_Shake128_Update(hash[i], bench_plain,
  6578. bench_size);
  6579. if (!bench_async_handle(&ret,
  6580. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6581. &times, &pending)) {
  6582. goto exit_shake128;
  6583. }
  6584. }
  6585. } /* for i */
  6586. RECORD_MULTI_VALUE_STATS();
  6587. } /* for times */
  6588. count += times;
  6589. times = 0;
  6590. do {
  6591. bench_async_poll(&pending);
  6592. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6593. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6594. 0, &times, numBlocks, &pending)) {
  6595. ret = wc_Shake128_Final(hash[i], digest[i],
  6596. WC_SHA3_128_BLOCK_SIZE);
  6597. if (!bench_async_handle(&ret,
  6598. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6599. &times, &pending)) {
  6600. goto exit_shake128;
  6601. }
  6602. }
  6603. } /* for i */
  6604. } while (pending > 0);
  6605. } while (bench_stats_check(start)
  6606. #ifdef MULTI_VALUE_STATISTICS
  6607. || runs < minimum_runs
  6608. #endif
  6609. );
  6610. }
  6611. else {
  6612. bench_stats_start(&count, &start);
  6613. do {
  6614. for (times = 0; times < numBlocks; times++) {
  6615. ret = wc_InitShake128(hash[0], HEAP_HINT,
  6616. useDeviceID ? devId : INVALID_DEVID);
  6617. if (ret == 0)
  6618. ret = wc_Shake128_Update(hash[0], bench_plain, bench_size);
  6619. if (ret == 0)
  6620. ret = wc_Shake128_Final(hash[0], digest[0],
  6621. WC_SHA3_128_BLOCK_SIZE);
  6622. if (ret != 0)
  6623. goto exit_shake128;
  6624. RECORD_MULTI_VALUE_STATS();
  6625. } /* for times */
  6626. count += times;
  6627. } while (bench_stats_check(start)
  6628. #ifdef MULTI_VALUE_STATISTICS
  6629. || runs < minimum_runs
  6630. #endif
  6631. );
  6632. }
  6633. exit_shake128:
  6634. bench_stats_sym_finish("SHAKE128", useDeviceID, count, bench_size,
  6635. start, ret);
  6636. #ifdef MULTI_VALUE_STATISTICS
  6637. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6638. #endif
  6639. exit:
  6640. if (WC_ARRAY_OK(hash)) {
  6641. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6642. wc_Shake128_Free(hash[i]);
  6643. }
  6644. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6645. }
  6646. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6647. }
  6648. #endif /* WOLFSSL_SHAKE128 */
  6649. #ifdef WOLFSSL_SHAKE256
  6650. void bench_shake256(int useDeviceID)
  6651. {
  6652. WC_DECLARE_ARRAY(hash, wc_Shake, BENCH_MAX_PENDING,
  6653. sizeof(wc_Shake), HEAP_HINT);
  6654. double start;
  6655. int ret = 0, i, count = 0, times, pending = 0;
  6656. DECLARE_MULTI_VALUE_STATS_VARS()
  6657. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6658. WC_SHA3_256_BLOCK_SIZE, HEAP_HINT);
  6659. WC_CALLOC_ARRAY(hash, wc_Shake, BENCH_MAX_PENDING,
  6660. sizeof(wc_Shake), HEAP_HINT);
  6661. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6662. WC_SHA3_256_BLOCK_SIZE, HEAP_HINT);
  6663. if (digest_stream) {
  6664. /* init keys */
  6665. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6666. ret = wc_InitShake256(hash[i], HEAP_HINT,
  6667. useDeviceID ? devId : INVALID_DEVID);
  6668. if (ret != 0) {
  6669. printf("InitShake256 failed, ret = %d\n", ret);
  6670. goto exit;
  6671. }
  6672. }
  6673. bench_stats_start(&count, &start);
  6674. do {
  6675. for (times = 0; times < numBlocks || pending > 0; ) {
  6676. bench_async_poll(&pending);
  6677. /* while free pending slots in queue, submit ops */
  6678. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6679. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6680. 0, &times, numBlocks, &pending)) {
  6681. ret = wc_Shake256_Update(hash[i], bench_plain,
  6682. bench_size);
  6683. if (!bench_async_handle(&ret,
  6684. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6685. &times, &pending)) {
  6686. goto exit_shake256;
  6687. }
  6688. }
  6689. } /* for i */
  6690. RECORD_MULTI_VALUE_STATS();
  6691. } /* for times */
  6692. count += times;
  6693. times = 0;
  6694. do {
  6695. bench_async_poll(&pending);
  6696. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6697. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6698. 0, &times, numBlocks, &pending)) {
  6699. ret = wc_Shake256_Final(hash[i], digest[i],
  6700. WC_SHA3_256_BLOCK_SIZE);
  6701. if (!bench_async_handle(&ret,
  6702. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6703. &times, &pending)) {
  6704. goto exit_shake256;
  6705. }
  6706. }
  6707. } /* for i */
  6708. } while (pending > 0);
  6709. } while (bench_stats_check(start)
  6710. #ifdef MULTI_VALUE_STATISTICS
  6711. || runs < minimum_runs
  6712. #endif
  6713. );
  6714. }
  6715. else {
  6716. bench_stats_start(&count, &start);
  6717. do {
  6718. for (times = 0; times < numBlocks; times++) {
  6719. ret = wc_InitShake256(hash[0], HEAP_HINT,
  6720. useDeviceID ? devId : INVALID_DEVID);
  6721. if (ret == 0)
  6722. ret = wc_Shake256_Update(hash[0], bench_plain, bench_size);
  6723. if (ret == 0)
  6724. ret = wc_Shake256_Final(hash[0], digest[0],
  6725. WC_SHA3_256_BLOCK_SIZE);
  6726. if (ret != 0)
  6727. goto exit_shake256;
  6728. RECORD_MULTI_VALUE_STATS();
  6729. } /* for times */
  6730. count += times;
  6731. } while (bench_stats_check(start)
  6732. #ifdef MULTI_VALUE_STATISTICS
  6733. || runs < minimum_runs
  6734. #endif
  6735. );
  6736. }
  6737. exit_shake256:
  6738. bench_stats_sym_finish("SHAKE256", useDeviceID, count, bench_size,
  6739. start, ret);
  6740. #ifdef MULTI_VALUE_STATISTICS
  6741. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6742. #endif
  6743. exit:
  6744. if (WC_ARRAY_OK(hash)) {
  6745. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6746. wc_Shake256_Free(hash[i]);
  6747. }
  6748. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6749. }
  6750. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6751. }
  6752. #endif /* WOLFSSL_SHAKE256 */
  6753. #endif
  6754. #ifdef WOLFSSL_SM3
  6755. void bench_sm3(int useDeviceID)
  6756. {
  6757. WC_DECLARE_ARRAY(hash, wc_Sm3, BENCH_MAX_PENDING,
  6758. sizeof(wc_Sm3), HEAP_HINT);
  6759. double start;
  6760. int ret = 0, i, count = 0, times, pending = 0;
  6761. DECLARE_MULTI_VALUE_STATS_VARS()
  6762. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SM3_DIGEST_SIZE,
  6763. HEAP_HINT);
  6764. WC_CALLOC_ARRAY(hash, wc_Sm3, BENCH_MAX_PENDING,
  6765. sizeof(wc_Sm3), HEAP_HINT);
  6766. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SM3_DIGEST_SIZE,
  6767. HEAP_HINT);
  6768. if (digest_stream) {
  6769. /* init keys */
  6770. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6771. ret = wc_InitSm3(hash[i], HEAP_HINT,
  6772. useDeviceID ? devId: INVALID_DEVID);
  6773. if (ret != 0) {
  6774. printf("InitSm3 failed, ret = %d\n", ret);
  6775. goto exit;
  6776. }
  6777. }
  6778. bench_stats_start(&count, &start);
  6779. do {
  6780. for (times = 0; times < numBlocks || pending > 0; ) {
  6781. bench_async_poll(&pending);
  6782. /* while free pending slots in queue, submit ops */
  6783. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6784. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6785. 0, &times, numBlocks, &pending)) {
  6786. ret = wc_Sm3Update(hash[i], bench_plain,
  6787. bench_size);
  6788. if (!bench_async_handle(&ret,
  6789. BENCH_ASYNC_GET_DEV(hash[i]), 0, &times, &pending)) {
  6790. goto exit_sm3;
  6791. }
  6792. }
  6793. } /* for i */
  6794. RECORD_MULTI_VALUE_STATS();
  6795. } /* for times */
  6796. count += times;
  6797. times = 0;
  6798. do {
  6799. bench_async_poll(&pending);
  6800. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6801. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6802. 0, &times, numBlocks, &pending)) {
  6803. ret = wc_Sm3Final(hash[i], digest[i]);
  6804. if (!bench_async_handle(&ret,
  6805. BENCH_ASYNC_GET_DEV(hash[i]), 0, &times, &pending)) {
  6806. goto exit_sm3;
  6807. }
  6808. }
  6809. } /* for i */
  6810. } while (pending > 0);
  6811. } while (bench_stats_check(start)
  6812. #ifdef MULTI_VALUE_STATISTICS
  6813. || runs < minimum_runs
  6814. #endif
  6815. );
  6816. }
  6817. else {
  6818. bench_stats_start(&count, &start);
  6819. do {
  6820. for (times = 0; times < numBlocks; times++) {
  6821. ret = wc_InitSm3(hash[0], HEAP_HINT,
  6822. useDeviceID ? devId: INVALID_DEVID);
  6823. if (ret == 0)
  6824. ret = wc_Sm3Update(hash[0], bench_plain, bench_size);
  6825. if (ret == 0)
  6826. ret = wc_Sm3Final(hash[0], digest[0]);
  6827. if (ret != 0)
  6828. goto exit_sm3;
  6829. RECORD_MULTI_VALUE_STATS();
  6830. } /* for times */
  6831. count += times;
  6832. } while (bench_stats_check(start)
  6833. #ifdef MULTI_VALUE_STATISTICS
  6834. || runs < minimum_runs
  6835. #endif
  6836. );
  6837. }
  6838. exit_sm3:
  6839. bench_stats_sym_finish("SM3", useDeviceID, count, bench_size, start, ret);
  6840. #ifdef MULTI_VALUE_STATISTICS
  6841. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6842. #endif
  6843. exit:
  6844. if (WC_ARRAY_OK(hash)) {
  6845. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6846. wc_Sm3Free(hash[i]);
  6847. }
  6848. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6849. }
  6850. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6851. }
  6852. #endif
  6853. #ifdef WOLFSSL_RIPEMD
  6854. void bench_ripemd(void)
  6855. {
  6856. RipeMd hash;
  6857. byte digest[RIPEMD_DIGEST_SIZE];
  6858. double start;
  6859. int i, count, ret = 0;
  6860. DECLARE_MULTI_VALUE_STATS_VARS()
  6861. if (digest_stream) {
  6862. ret = wc_InitRipeMd(&hash);
  6863. if (ret != 0) {
  6864. printf("wc_InitRipeMd failed, retval %d\n", ret);
  6865. return;
  6866. }
  6867. bench_stats_start(&count, &start);
  6868. do {
  6869. for (i = 0; i < numBlocks; i++) {
  6870. ret = wc_RipeMdUpdate(&hash, bench_plain, bench_size);
  6871. if (ret != 0) {
  6872. printf("wc_RipeMdUpdate failed, retval %d\n", ret);
  6873. return;
  6874. }
  6875. RECORD_MULTI_VALUE_STATS();
  6876. }
  6877. ret = wc_RipeMdFinal(&hash, digest);
  6878. if (ret != 0) {
  6879. printf("wc_RipeMdFinal failed, retval %d\n", ret);
  6880. return;
  6881. }
  6882. count += i;
  6883. } while (bench_stats_check(start)
  6884. #ifdef MULTI_VALUE_STATISTICS
  6885. || runs < minimum_runs
  6886. #endif
  6887. );
  6888. }
  6889. else {
  6890. bench_stats_start(&count, &start);
  6891. do {
  6892. for (i = 0; i < numBlocks; i++) {
  6893. ret = wc_InitRipeMd(&hash);
  6894. if (ret != 0) {
  6895. printf("wc_InitRipeMd failed, retval %d\n", ret);
  6896. return;
  6897. }
  6898. ret = wc_RipeMdUpdate(&hash, bench_plain, bench_size);
  6899. if (ret != 0) {
  6900. printf("wc_RipeMdUpdate failed, retval %d\n", ret);
  6901. return;
  6902. }
  6903. ret = wc_RipeMdFinal(&hash, digest);
  6904. if (ret != 0) {
  6905. printf("wc_RipeMdFinal failed, retval %d\n", ret);
  6906. return;
  6907. }
  6908. RECORD_MULTI_VALUE_STATS();
  6909. }
  6910. count += i;
  6911. } while (bench_stats_check(start)
  6912. #ifdef MULTI_VALUE_STATISTICS
  6913. || runs < minimum_runs
  6914. #endif
  6915. );
  6916. }
  6917. bench_stats_sym_finish("RIPEMD", 0, count, bench_size, start, ret);
  6918. #ifdef MULTI_VALUE_STATISTICS
  6919. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6920. #endif
  6921. return;
  6922. }
  6923. #endif
  6924. #ifdef HAVE_BLAKE2
  6925. void bench_blake2b(void)
  6926. {
  6927. Blake2b b2b;
  6928. byte digest[64];
  6929. double start;
  6930. int ret = 0, i, count;
  6931. DECLARE_MULTI_VALUE_STATS_VARS()
  6932. if (digest_stream) {
  6933. ret = wc_InitBlake2b(&b2b, 64);
  6934. if (ret != 0) {
  6935. printf("InitBlake2b failed, ret = %d\n", ret);
  6936. return;
  6937. }
  6938. bench_stats_start(&count, &start);
  6939. do {
  6940. for (i = 0; i < numBlocks; i++) {
  6941. ret = wc_Blake2bUpdate(&b2b, bench_plain, bench_size);
  6942. if (ret != 0) {
  6943. printf("Blake2bUpdate failed, ret = %d\n", ret);
  6944. return;
  6945. }
  6946. RECORD_MULTI_VALUE_STATS();
  6947. }
  6948. ret = wc_Blake2bFinal(&b2b, digest, 64);
  6949. if (ret != 0) {
  6950. printf("Blake2bFinal failed, ret = %d\n", ret);
  6951. return;
  6952. }
  6953. count += i;
  6954. } while (bench_stats_check(start)
  6955. #ifdef MULTI_VALUE_STATISTICS
  6956. || runs < minimum_runs
  6957. #endif
  6958. );
  6959. }
  6960. else {
  6961. bench_stats_start(&count, &start);
  6962. do {
  6963. for (i = 0; i < numBlocks; i++) {
  6964. ret = wc_InitBlake2b(&b2b, 64);
  6965. if (ret != 0) {
  6966. printf("InitBlake2b failed, ret = %d\n", ret);
  6967. return;
  6968. }
  6969. ret = wc_Blake2bUpdate(&b2b, bench_plain, bench_size);
  6970. if (ret != 0) {
  6971. printf("Blake2bUpdate failed, ret = %d\n", ret);
  6972. return;
  6973. }
  6974. ret = wc_Blake2bFinal(&b2b, digest, 64);
  6975. if (ret != 0) {
  6976. printf("Blake2bFinal failed, ret = %d\n", ret);
  6977. return;
  6978. }
  6979. RECORD_MULTI_VALUE_STATS();
  6980. }
  6981. count += i;
  6982. } while (bench_stats_check(start)
  6983. #ifdef MULTI_VALUE_STATISTICS
  6984. || runs < minimum_runs
  6985. #endif
  6986. );
  6987. }
  6988. bench_stats_sym_finish("BLAKE2b", 0, count, bench_size, start, ret);
  6989. #ifdef MULTI_VALUE_STATISTICS
  6990. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6991. #endif
  6992. }
  6993. #endif
  6994. #if defined(HAVE_BLAKE2S)
  6995. void bench_blake2s(void)
  6996. {
  6997. Blake2s b2s;
  6998. byte digest[32];
  6999. double start;
  7000. int ret = 0, i, count;
  7001. DECLARE_MULTI_VALUE_STATS_VARS()
  7002. if (digest_stream) {
  7003. ret = wc_InitBlake2s(&b2s, 32);
  7004. if (ret != 0) {
  7005. printf("InitBlake2s failed, ret = %d\n", ret);
  7006. return;
  7007. }
  7008. bench_stats_start(&count, &start);
  7009. do {
  7010. for (i = 0; i < numBlocks; i++) {
  7011. ret = wc_Blake2sUpdate(&b2s, bench_plain, bench_size);
  7012. if (ret != 0) {
  7013. printf("Blake2sUpdate failed, ret = %d\n", ret);
  7014. return;
  7015. }
  7016. RECORD_MULTI_VALUE_STATS();
  7017. }
  7018. ret = wc_Blake2sFinal(&b2s, digest, 32);
  7019. if (ret != 0) {
  7020. printf("Blake2sFinal failed, ret = %d\n", ret);
  7021. return;
  7022. }
  7023. count += i;
  7024. } while (bench_stats_check(start)
  7025. #ifdef MULTI_VALUE_STATISTICS
  7026. || runs < minimum_runs
  7027. #endif
  7028. );
  7029. }
  7030. else {
  7031. bench_stats_start(&count, &start);
  7032. do {
  7033. for (i = 0; i < numBlocks; i++) {
  7034. ret = wc_InitBlake2s(&b2s, 32);
  7035. if (ret != 0) {
  7036. printf("InitBlake2b failed, ret = %d\n", ret);
  7037. return;
  7038. }
  7039. ret = wc_Blake2sUpdate(&b2s, bench_plain, bench_size);
  7040. if (ret != 0) {
  7041. printf("Blake2bUpdate failed, ret = %d\n", ret);
  7042. return;
  7043. }
  7044. ret = wc_Blake2sFinal(&b2s, digest, 32);
  7045. if (ret != 0) {
  7046. printf("Blake2sFinal failed, ret = %d\n", ret);
  7047. return;
  7048. }
  7049. RECORD_MULTI_VALUE_STATS();
  7050. }
  7051. count += i;
  7052. } while (bench_stats_check(start)
  7053. #ifdef MULTI_VALUE_STATISTICS
  7054. || runs < minimum_runs
  7055. #endif
  7056. );
  7057. }
  7058. bench_stats_sym_finish("BLAKE2s", 0, count, bench_size, start, ret);
  7059. #ifdef MULTI_VALUE_STATISTICS
  7060. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7061. #endif
  7062. }
  7063. #endif
  7064. #ifdef WOLFSSL_CMAC
  7065. static void bench_cmac_helper(word32 keySz, const char* outMsg, int useDeviceID)
  7066. {
  7067. Cmac cmac;
  7068. byte digest[AES_BLOCK_SIZE];
  7069. word32 digestSz = sizeof(digest);
  7070. double start;
  7071. int ret, i, count;
  7072. DECLARE_MULTI_VALUE_STATS_VARS()
  7073. #ifdef WOLFSSL_SECO_CAAM
  7074. unsigned int keyID;
  7075. int keyGroup = 1; /* group one was chosen arbitrarily */
  7076. int keyInfo = CAAM_KEY_TRANSIENT;
  7077. int keyType = CAAM_KEYTYPE_AES128;
  7078. byte pubKey[AES_256_KEY_SIZE];
  7079. if (keySz == AES_256_KEY_SIZE) {
  7080. keyType = CAAM_KEYTYPE_AES256;
  7081. }
  7082. if (useDeviceID &&
  7083. wc_SECO_GenerateKey(CAAM_GENERATE_KEY, keyGroup, pubKey, 0, keyType,
  7084. keyInfo, &keyID) != 0) {
  7085. printf("Error generating key in hsm\n");
  7086. return;
  7087. }
  7088. #endif
  7089. (void)useDeviceID;
  7090. bench_stats_start(&count, &start);
  7091. do {
  7092. #ifdef HAVE_FIPS
  7093. ret = wc_InitCmac(&cmac, bench_key, keySz, WC_CMAC_AES, NULL);
  7094. #else
  7095. ret = wc_InitCmac_ex(&cmac, bench_key, keySz, WC_CMAC_AES, NULL,
  7096. HEAP_HINT, useDeviceID ? devId : INVALID_DEVID);
  7097. #endif
  7098. if (ret != 0) {
  7099. printf("InitCmac failed, ret = %d\n", ret);
  7100. return;
  7101. }
  7102. #ifdef WOLFSSL_SECO_CAAM
  7103. if (useDeviceID) {
  7104. wc_SECO_CMACSetKeyID(&cmac, keyID);
  7105. }
  7106. #endif
  7107. for (i = 0; i < numBlocks; i++) {
  7108. ret = wc_CmacUpdate(&cmac, bench_plain, bench_size);
  7109. if (ret != 0) {
  7110. printf("CmacUpdate failed, ret = %d\n", ret);
  7111. return;
  7112. }
  7113. RECORD_MULTI_VALUE_STATS();
  7114. }
  7115. /* Note: final force zero's the Cmac struct */
  7116. ret = wc_CmacFinal(&cmac, digest, &digestSz);
  7117. if (ret != 0) {
  7118. printf("CmacFinal failed, ret = %d\n", ret);
  7119. return;
  7120. }
  7121. count += i;
  7122. } while (bench_stats_check(start)
  7123. #ifdef MULTI_VALUE_STATISTICS
  7124. || runs < minimum_runs
  7125. #endif
  7126. );
  7127. bench_stats_sym_finish(outMsg, useDeviceID, count, bench_size, start, ret);
  7128. #ifdef MULTI_VALUE_STATISTICS
  7129. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7130. #endif
  7131. }
  7132. void bench_cmac(int useDeviceID)
  7133. {
  7134. #ifdef WOLFSSL_AES_128
  7135. bench_cmac_helper(16, "AES-128-CMAC", useDeviceID);
  7136. #endif
  7137. #ifdef WOLFSSL_AES_256
  7138. bench_cmac_helper(32, "AES-256-CMAC", useDeviceID);
  7139. #endif
  7140. }
  7141. #endif /* WOLFSSL_CMAC */
  7142. #ifdef HAVE_SCRYPT
  7143. void bench_scrypt(void)
  7144. {
  7145. byte derived[64];
  7146. double start;
  7147. int ret, i, count;
  7148. DECLARE_MULTI_VALUE_STATS_VARS()
  7149. bench_stats_start(&count, &start);
  7150. do {
  7151. for (i = 0; i < scryptCnt; i++) {
  7152. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  7153. (byte*)"SodiumChloride", 14, 14, 8, 1,
  7154. sizeof(derived));
  7155. if (ret != 0) {
  7156. printf("scrypt failed, ret = %d\n", ret);
  7157. goto exit;
  7158. }
  7159. RECORD_MULTI_VALUE_STATS();
  7160. }
  7161. count += i;
  7162. } while (bench_stats_check(start)
  7163. #ifdef MULTI_VALUE_STATISTICS
  7164. || runs < minimum_runs
  7165. #endif
  7166. );
  7167. exit:
  7168. bench_stats_asym_finish("scrypt", 17, "", 0, count, start, ret);
  7169. #ifdef MULTI_VALUE_STATISTICS
  7170. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7171. #endif
  7172. }
  7173. #endif /* HAVE_SCRYPT */
  7174. #ifndef NO_HMAC
  7175. static void bench_hmac(int useDeviceID, int type, int digestSz,
  7176. const byte* key, word32 keySz, const char* label)
  7177. {
  7178. WC_DECLARE_ARRAY(hmac, Hmac, BENCH_MAX_PENDING,
  7179. sizeof(Hmac), HEAP_HINT);
  7180. double start;
  7181. int ret = 0, i, count = 0, times, pending = 0;
  7182. DECLARE_MULTI_VALUE_STATS_VARS()
  7183. #ifdef WOLFSSL_ASYNC_CRYPT
  7184. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  7185. WC_MAX_DIGEST_SIZE, HEAP_HINT);
  7186. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  7187. WC_MAX_DIGEST_SIZE, HEAP_HINT);
  7188. #else
  7189. byte digest[BENCH_MAX_PENDING][WC_MAX_DIGEST_SIZE];
  7190. #endif
  7191. (void)digestSz;
  7192. WC_CALLOC_ARRAY(hmac, Hmac, BENCH_MAX_PENDING,
  7193. sizeof(Hmac), HEAP_HINT);
  7194. /* init keys */
  7195. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7196. ret = wc_HmacInit(hmac[i], HEAP_HINT,
  7197. useDeviceID ? devId : INVALID_DEVID);
  7198. if (ret != 0) {
  7199. printf("wc_HmacInit failed for %s, ret = %d\n", label, ret);
  7200. goto exit;
  7201. }
  7202. ret = wc_HmacSetKey(hmac[i], type, key, keySz);
  7203. if (ret != 0) {
  7204. printf("wc_HmacSetKey failed for %s, ret = %d\n", label, ret);
  7205. goto exit;
  7206. }
  7207. }
  7208. bench_stats_start(&count, &start);
  7209. do {
  7210. for (times = 0; times < numBlocks || pending > 0; ) {
  7211. bench_async_poll(&pending);
  7212. /* while free pending slots in queue, submit ops */
  7213. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7214. if (bench_async_check(&ret,
  7215. BENCH_ASYNC_GET_DEV(hmac[i]), 0,
  7216. &times, numBlocks, &pending)) {
  7217. ret = wc_HmacUpdate(hmac[i], bench_plain, bench_size);
  7218. if (!bench_async_handle(&ret,
  7219. BENCH_ASYNC_GET_DEV(hmac[i]),
  7220. 0, &times, &pending)) {
  7221. goto exit_hmac;
  7222. }
  7223. }
  7224. } /* for i */
  7225. } /* for times */
  7226. count += times;
  7227. times = 0;
  7228. do {
  7229. bench_async_poll(&pending);
  7230. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7231. if (bench_async_check(&ret,
  7232. BENCH_ASYNC_GET_DEV(hmac[i]), 0,
  7233. &times, numBlocks, &pending)) {
  7234. ret = wc_HmacFinal(hmac[i], digest[i]);
  7235. if (!bench_async_handle(&ret,
  7236. BENCH_ASYNC_GET_DEV(hmac[i]),
  7237. 0, &times, &pending)) {
  7238. goto exit_hmac;
  7239. }
  7240. }
  7241. RECORD_MULTI_VALUE_STATS();
  7242. } /* for i */
  7243. } while (pending > 0);
  7244. } while (bench_stats_check(start)
  7245. #ifdef MULTI_VALUE_STATISTICS
  7246. || runs < minimum_runs
  7247. #endif
  7248. );
  7249. exit_hmac:
  7250. bench_stats_sym_finish(label, useDeviceID, count, bench_size, start, ret);
  7251. #ifdef MULTI_VALUE_STATISTICS
  7252. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7253. #endif
  7254. exit:
  7255. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7256. wc_HmacFree(hmac[i]);
  7257. }
  7258. WC_FREE_ARRAY(hmac, BENCH_MAX_PENDING, HEAP_HINT);
  7259. #ifdef WOLFSSL_ASYNC_CRYPT
  7260. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  7261. #endif
  7262. }
  7263. #ifndef NO_MD5
  7264. void bench_hmac_md5(int useDeviceID)
  7265. {
  7266. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7267. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7268. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  7269. bench_hmac(useDeviceID, WC_MD5, WC_MD5_DIGEST_SIZE, key, sizeof(key),
  7270. "HMAC-MD5");
  7271. }
  7272. #endif /* NO_MD5 */
  7273. #ifndef NO_SHA
  7274. void bench_hmac_sha(int useDeviceID)
  7275. {
  7276. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7277. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7278. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7279. 0x0b, 0x0b, 0x0b, 0x0b };
  7280. bench_hmac(useDeviceID, WC_SHA, WC_SHA_DIGEST_SIZE, key, sizeof(key),
  7281. "HMAC-SHA");
  7282. }
  7283. #endif /* NO_SHA */
  7284. #ifdef WOLFSSL_SHA224
  7285. void bench_hmac_sha224(int useDeviceID)
  7286. {
  7287. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7288. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7289. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7290. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7291. 0x0b, 0x0b, 0x0b, 0x0b };
  7292. bench_hmac(useDeviceID, WC_SHA224,
  7293. WC_SHA224_DIGEST_SIZE, key, sizeof(key),
  7294. "HMAC-SHA224");
  7295. }
  7296. #endif /* WOLFSSL_SHA224 */
  7297. #ifndef NO_SHA256
  7298. void bench_hmac_sha256(int useDeviceID)
  7299. {
  7300. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7301. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7302. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7303. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7304. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  7305. bench_hmac(useDeviceID, WC_SHA256, WC_SHA256_DIGEST_SIZE, key, sizeof(key),
  7306. "HMAC-SHA256");
  7307. }
  7308. #endif /* NO_SHA256 */
  7309. #ifdef WOLFSSL_SHA384
  7310. void bench_hmac_sha384(int useDeviceID)
  7311. {
  7312. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7313. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7314. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7315. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7316. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7317. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7318. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  7319. bench_hmac(useDeviceID, WC_SHA384, WC_SHA384_DIGEST_SIZE, key, sizeof(key),
  7320. "HMAC-SHA384");
  7321. }
  7322. #endif /* WOLFSSL_SHA384 */
  7323. #ifdef WOLFSSL_SHA512
  7324. void bench_hmac_sha512(int useDeviceID)
  7325. {
  7326. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7327. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7328. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7329. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7330. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7331. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7332. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7333. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7334. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  7335. bench_hmac(useDeviceID, WC_SHA512, WC_SHA512_DIGEST_SIZE, key, sizeof(key),
  7336. "HMAC-SHA512");
  7337. }
  7338. #endif /* WOLFSSL_SHA512 */
  7339. #ifndef NO_PWDBASED
  7340. void bench_pbkdf2(void)
  7341. {
  7342. double start;
  7343. int ret = 0, count = 0;
  7344. const char* passwd32 = "passwordpasswordpasswordpassword";
  7345. WOLFSSL_SMALL_STACK_STATIC const byte salt32[] = {
  7346. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06,
  7347. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06,
  7348. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06,
  7349. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  7350. byte derived[32];
  7351. DECLARE_MULTI_VALUE_STATS_VARS()
  7352. bench_stats_start(&count, &start);
  7353. PRIVATE_KEY_UNLOCK();
  7354. do {
  7355. ret = wc_PBKDF2(derived, (const byte*)passwd32, (int)XSTRLEN(passwd32),
  7356. salt32, (int)sizeof(salt32), 1000, 32, WC_SHA256);
  7357. count++;
  7358. RECORD_MULTI_VALUE_STATS();
  7359. } while (bench_stats_check(start)
  7360. #ifdef MULTI_VALUE_STATISTICS
  7361. || runs < minimum_runs
  7362. #endif
  7363. );
  7364. PRIVATE_KEY_LOCK();
  7365. bench_stats_sym_finish("PBKDF2", 32, count, 32, start, ret);
  7366. #ifdef MULTI_VALUE_STATISTICS
  7367. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7368. #endif
  7369. }
  7370. #endif /* !NO_PWDBASED */
  7371. #endif /* NO_HMAC */
  7372. #ifdef WOLFSSL_SIPHASH
  7373. void bench_siphash(void)
  7374. {
  7375. double start;
  7376. int ret = 0, count;
  7377. const char* passwd16 = "passwordpassword";
  7378. byte out[16];
  7379. int i;
  7380. DECLARE_MULTI_VALUE_STATS_VARS()
  7381. bench_stats_start(&count, &start);
  7382. do {
  7383. for (i = 0; i < numBlocks; i++) {
  7384. ret = wc_SipHash((const byte*)passwd16, bench_plain, bench_size,
  7385. out, 8);
  7386. RECORD_MULTI_VALUE_STATS();
  7387. }
  7388. count += i;
  7389. } while (bench_stats_check(start)
  7390. #ifdef MULTI_VALUE_STATISTICS
  7391. || runs < minimum_runs
  7392. #endif
  7393. );
  7394. bench_stats_sym_finish("SipHash-8", 1, count, bench_size, start, ret);
  7395. #ifdef MULTI_VALUE_STATISTICS
  7396. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7397. #endif
  7398. RESET_MULTI_VALUE_STATS_VARS();
  7399. bench_stats_start(&count, &start);
  7400. do {
  7401. for (i = 0; i < numBlocks; i++) {
  7402. ret = wc_SipHash((const byte*)passwd16, bench_plain, bench_size,
  7403. out, 16);
  7404. RECORD_MULTI_VALUE_STATS();
  7405. }
  7406. count += i;
  7407. } while (bench_stats_check(start)
  7408. #ifdef MULTI_VALUE_STATISTICS
  7409. || runs < minimum_runs
  7410. #endif
  7411. );
  7412. bench_stats_sym_finish("SipHash-16", 1, count, bench_size, start, ret);
  7413. #ifdef MULTI_VALUE_STATISTICS
  7414. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7415. #endif
  7416. }
  7417. #endif
  7418. #ifdef WC_SRTP_KDF
  7419. void bench_srtpkdf(void)
  7420. {
  7421. double start;
  7422. int count;
  7423. int ret = 0;
  7424. byte keyE[32];
  7425. byte keyA[20];
  7426. byte keyS[14];
  7427. const byte *key = bench_key_buf;
  7428. const byte salt[14] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  7429. 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e };
  7430. const byte index[6] = { 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA };
  7431. int kdrIdx = 0;
  7432. int i;
  7433. DECLARE_MULTI_VALUE_STATS_VARS()
  7434. bench_stats_start(&count, &start);
  7435. PRIVATE_KEY_UNLOCK();
  7436. do {
  7437. for (i = 0; i < numBlocks; i++) {
  7438. ret = wc_SRTP_KDF(key, AES_128_KEY_SIZE, salt, sizeof(salt),
  7439. kdrIdx, index, keyE, AES_128_KEY_SIZE, keyA, sizeof(keyA),
  7440. keyS, sizeof(keyS));
  7441. RECORD_MULTI_VALUE_STATS();
  7442. }
  7443. count += i;
  7444. } while (bench_stats_check(start)
  7445. #ifdef MULTI_VALUE_STATISTICS
  7446. || runs < minimum_runs
  7447. #endif
  7448. );
  7449. PRIVATE_KEY_LOCK();
  7450. bench_stats_asym_finish("KDF", 128, "SRTP", 0, count, start, ret);
  7451. #ifdef MULTI_VALUE_STATISTICS
  7452. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7453. #endif
  7454. RESET_MULTI_VALUE_STATS_VARS();
  7455. bench_stats_start(&count, &start);
  7456. PRIVATE_KEY_UNLOCK();
  7457. do {
  7458. for (i = 0; i < numBlocks; i++) {
  7459. ret = wc_SRTP_KDF(key, AES_256_KEY_SIZE, salt, sizeof(salt),
  7460. kdrIdx, index, keyE, AES_256_KEY_SIZE, keyA, sizeof(keyA),
  7461. keyS, sizeof(keyS));
  7462. RECORD_MULTI_VALUE_STATS();
  7463. }
  7464. count += i;
  7465. } while (bench_stats_check(start)
  7466. #ifdef MULTI_VALUE_STATISTICS
  7467. || runs < minimum_runs
  7468. #endif
  7469. );
  7470. PRIVATE_KEY_LOCK();
  7471. bench_stats_asym_finish("KDF", 256, "SRTP", 0, count, start, ret);
  7472. #ifdef MULTI_VALUE_STATISTICS
  7473. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7474. #endif
  7475. RESET_MULTI_VALUE_STATS_VARS();
  7476. bench_stats_start(&count, &start);
  7477. PRIVATE_KEY_UNLOCK();
  7478. do {
  7479. for (i = 0; i < numBlocks; i++) {
  7480. ret = wc_SRTCP_KDF(key, AES_128_KEY_SIZE, salt, sizeof(salt),
  7481. kdrIdx, index, keyE, AES_128_KEY_SIZE, keyA, sizeof(keyA),
  7482. keyS, sizeof(keyS));
  7483. RECORD_MULTI_VALUE_STATS();
  7484. }
  7485. count += i;
  7486. } while (bench_stats_check(start)
  7487. #ifdef MULTI_VALUE_STATISTICS
  7488. || runs < minimum_runs
  7489. #endif
  7490. );
  7491. PRIVATE_KEY_LOCK();
  7492. bench_stats_asym_finish("KDF", 128, "SRTCP", 0, count, start, ret);
  7493. #ifdef MULTI_VALUE_STATISTICS
  7494. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7495. #endif
  7496. RESET_MULTI_VALUE_STATS_VARS();
  7497. bench_stats_start(&count, &start);
  7498. PRIVATE_KEY_UNLOCK();
  7499. do {
  7500. for (i = 0; i < numBlocks; i++) {
  7501. ret = wc_SRTCP_KDF(key, AES_256_KEY_SIZE, salt, sizeof(salt),
  7502. kdrIdx, index, keyE, AES_256_KEY_SIZE, keyA, sizeof(keyA),
  7503. keyS, sizeof(keyS));
  7504. RECORD_MULTI_VALUE_STATS();
  7505. }
  7506. count += i;
  7507. } while (bench_stats_check(start)
  7508. #ifdef MULTI_VALUE_STATISTICS
  7509. || runs < minimum_runs
  7510. #endif
  7511. );
  7512. PRIVATE_KEY_LOCK();
  7513. bench_stats_asym_finish("KDF", 256, "SRTCP", 0, count, start, ret);
  7514. #ifdef MULTI_VALUE_STATISTICS
  7515. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7516. #endif
  7517. }
  7518. #endif
  7519. #ifndef NO_RSA
  7520. #if defined(WOLFSSL_KEY_GEN)
  7521. static void bench_rsaKeyGen_helper(int useDeviceID, word32 keySz)
  7522. {
  7523. WC_DECLARE_ARRAY(genKey, RsaKey, BENCH_MAX_PENDING,
  7524. sizeof(RsaKey), HEAP_HINT);
  7525. double start = 0;
  7526. int ret = 0, i, count = 0, times, pending = 0;
  7527. const long rsa_e_val = WC_RSA_EXPONENT;
  7528. const char**desc = bench_desc_words[lng_index];
  7529. DECLARE_MULTI_VALUE_STATS_VARS()
  7530. WC_CALLOC_ARRAY(genKey, RsaKey, BENCH_MAX_PENDING,
  7531. sizeof(RsaKey), HEAP_HINT);
  7532. bench_stats_start(&count, &start);
  7533. do {
  7534. /* while free pending slots in queue, submit ops */
  7535. for (times = 0; times < genTimes || pending > 0; ) {
  7536. bench_async_poll(&pending);
  7537. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7538. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]),
  7539. 0, &times, genTimes, &pending)) {
  7540. wc_FreeRsaKey(genKey[i]);
  7541. ret = wc_InitRsaKey_ex(genKey[i], HEAP_HINT, devId);
  7542. if (ret < 0) {
  7543. goto exit;
  7544. }
  7545. ret = wc_MakeRsaKey(genKey[i], (int)keySz, rsa_e_val,
  7546. &gRng);
  7547. if (!bench_async_handle(&ret,
  7548. BENCH_ASYNC_GET_DEV(genKey[i]), 0,
  7549. &times, &pending)) {
  7550. goto exit;
  7551. }
  7552. }
  7553. } /* for i */
  7554. RECORD_MULTI_VALUE_STATS();
  7555. } /* for times */
  7556. count += times;
  7557. } while (bench_stats_check(start)
  7558. #ifdef MULTI_VALUE_STATISTICS
  7559. || runs < minimum_runs
  7560. #endif
  7561. );
  7562. exit:
  7563. bench_stats_asym_finish("RSA", (int)keySz, desc[2], useDeviceID, count,
  7564. start, ret);
  7565. #ifdef MULTI_VALUE_STATISTICS
  7566. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7567. #endif
  7568. /* cleanup */
  7569. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7570. wc_FreeRsaKey(genKey[i]);
  7571. }
  7572. WC_FREE_ARRAY(genKey, BENCH_MAX_PENDING, HEAP_HINT);
  7573. }
  7574. void bench_rsaKeyGen(int useDeviceID)
  7575. {
  7576. int k;
  7577. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  7578. static const word32 keySizes[2] = {1024, 2048};
  7579. #else
  7580. static const word32 keySizes[1] = {2048};
  7581. #endif
  7582. for (k = 0; k < (int)(sizeof(keySizes)/sizeof(int)); k++) {
  7583. bench_rsaKeyGen_helper(useDeviceID, keySizes[k]);
  7584. }
  7585. }
  7586. void bench_rsaKeyGen_size(int useDeviceID, word32 keySz)
  7587. {
  7588. bench_rsaKeyGen_helper(useDeviceID, keySz);
  7589. }
  7590. #endif /* WOLFSSL_KEY_GEN */
  7591. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  7592. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  7593. #if defined(WOLFSSL_MDK_SHELL)
  7594. static char *certRSAname = "certs/rsa2048.der";
  7595. /* set by shell command */
  7596. static void set_Bench_RSA_File(char * cert) { certRSAname = cert ; }
  7597. #elif defined(FREESCALE_MQX)
  7598. static char *certRSAname = "a:\\certs\\rsa2048.der";
  7599. #else
  7600. static const char *certRSAname = "certs/rsa2048.der";
  7601. #endif
  7602. #endif
  7603. #define RSA_BUF_SIZE 384 /* for up to 3072 bit */
  7604. #if defined(WOLFSSL_RSA_VERIFY_INLINE) || defined(WOLFSSL_RSA_PUBLIC_ONLY)
  7605. #if defined(USE_CERT_BUFFERS_2048)
  7606. static const unsigned char rsa_2048_sig[] = {
  7607. 0x8c, 0x9e, 0x37, 0xbf, 0xc3, 0xa6, 0xba, 0x1c,
  7608. 0x53, 0x22, 0x40, 0x4b, 0x8b, 0x0d, 0x3c, 0x0e,
  7609. 0x2e, 0x8c, 0x31, 0x2c, 0x47, 0xbf, 0x03, 0x48,
  7610. 0x18, 0x46, 0x73, 0x8d, 0xd7, 0xdd, 0x17, 0x64,
  7611. 0x0d, 0x7f, 0xdc, 0x74, 0xed, 0x80, 0xc3, 0xe8,
  7612. 0x9a, 0x18, 0x33, 0xd4, 0xe6, 0xc5, 0xe1, 0x54,
  7613. 0x75, 0xd1, 0xbb, 0x40, 0xde, 0xa8, 0xb9, 0x1b,
  7614. 0x14, 0xe8, 0xc1, 0x39, 0xeb, 0xa0, 0x69, 0x8a,
  7615. 0xc6, 0x9b, 0xef, 0x53, 0xb5, 0x23, 0x2b, 0x78,
  7616. 0x06, 0x43, 0x37, 0x11, 0x81, 0x84, 0x73, 0x33,
  7617. 0x33, 0xfe, 0xf7, 0x5d, 0x2b, 0x84, 0xd6, 0x83,
  7618. 0xd6, 0xdd, 0x55, 0x33, 0xef, 0xd1, 0xf7, 0x12,
  7619. 0xb0, 0xc2, 0x0e, 0xb1, 0x78, 0xd4, 0xa8, 0xa3,
  7620. 0x25, 0xeb, 0xed, 0x9a, 0xb3, 0xee, 0xc3, 0x7e,
  7621. 0xce, 0x13, 0x18, 0x86, 0x31, 0xe1, 0xef, 0x01,
  7622. 0x0f, 0x6e, 0x67, 0x24, 0x74, 0xbd, 0x0b, 0x7f,
  7623. 0xa9, 0xca, 0x6f, 0xaa, 0x83, 0x28, 0x90, 0x40,
  7624. 0xf1, 0xb5, 0x10, 0x0e, 0x26, 0x03, 0x05, 0x5d,
  7625. 0x87, 0xb4, 0xe0, 0x4c, 0x98, 0xd8, 0xc6, 0x42,
  7626. 0x89, 0x77, 0xeb, 0xb6, 0xd4, 0xe6, 0x26, 0xf3,
  7627. 0x31, 0x25, 0xde, 0x28, 0x38, 0x58, 0xe8, 0x2c,
  7628. 0xf4, 0x56, 0x7c, 0xb6, 0xfd, 0x99, 0xb0, 0xb0,
  7629. 0xf4, 0x83, 0xb6, 0x74, 0xa9, 0x5b, 0x9f, 0xe8,
  7630. 0xe9, 0xf1, 0xa1, 0x2a, 0xbd, 0xf6, 0x83, 0x28,
  7631. 0x09, 0xda, 0xa6, 0xd6, 0xcd, 0x61, 0x60, 0xf7,
  7632. 0x13, 0x4e, 0x46, 0x57, 0x38, 0x1e, 0x11, 0x92,
  7633. 0x6b, 0x6b, 0xcf, 0xd3, 0xf4, 0x8b, 0x66, 0x03,
  7634. 0x25, 0xa3, 0x7a, 0x2f, 0xce, 0xc1, 0x85, 0xa5,
  7635. 0x48, 0x91, 0x8a, 0xb3, 0x4f, 0x5d, 0x98, 0xb1,
  7636. 0x69, 0x58, 0x47, 0x69, 0x0c, 0x52, 0xdc, 0x42,
  7637. 0x4c, 0xef, 0xe8, 0xd4, 0x4d, 0x6a, 0x33, 0x7d,
  7638. 0x9e, 0xd2, 0x51, 0xe6, 0x41, 0xbf, 0x4f, 0xa2
  7639. };
  7640. #elif defined(USE_CERT_BUFFERS_3072)
  7641. static const unsigned char rsa_3072_sig[] = {
  7642. 0x1a, 0xd6, 0x0d, 0xfd, 0xe3, 0x41, 0x95, 0x76,
  7643. 0x27, 0x16, 0x7d, 0xc7, 0x94, 0x16, 0xca, 0xa8,
  7644. 0x26, 0x08, 0xbe, 0x78, 0x87, 0x72, 0x4c, 0xd9,
  7645. 0xa7, 0xfc, 0x33, 0x77, 0x2d, 0x53, 0x07, 0xb5,
  7646. 0x8c, 0xce, 0x48, 0x17, 0x9b, 0xff, 0x9f, 0x9b,
  7647. 0x17, 0xc4, 0xbb, 0x72, 0xed, 0xdb, 0xa0, 0x34,
  7648. 0x69, 0x5b, 0xc7, 0x4e, 0xbf, 0xec, 0x13, 0xc5,
  7649. 0x98, 0x71, 0x9a, 0x4e, 0x18, 0x0e, 0xcb, 0xe7,
  7650. 0xc6, 0xd5, 0x21, 0x31, 0x7c, 0x0d, 0xae, 0x14,
  7651. 0x2b, 0x87, 0x4f, 0x77, 0x95, 0x2e, 0x26, 0xe2,
  7652. 0x83, 0xfe, 0x49, 0x1e, 0x87, 0x19, 0x4a, 0x63,
  7653. 0x73, 0x75, 0xf1, 0xf5, 0x71, 0xd2, 0xce, 0xd4,
  7654. 0x39, 0x2b, 0xd9, 0xe0, 0x76, 0x70, 0xc8, 0xf8,
  7655. 0xed, 0xdf, 0x90, 0x57, 0x17, 0xb9, 0x16, 0xf6,
  7656. 0xe9, 0x49, 0x48, 0xce, 0x5a, 0x8b, 0xe4, 0x84,
  7657. 0x7c, 0xf3, 0x31, 0x68, 0x97, 0x45, 0x68, 0x38,
  7658. 0x50, 0x3a, 0x70, 0xbd, 0xb3, 0xd3, 0xd2, 0xe0,
  7659. 0x56, 0x5b, 0xc2, 0x0c, 0x2c, 0x10, 0x70, 0x7b,
  7660. 0xd4, 0x99, 0xf9, 0x38, 0x31, 0xb1, 0x86, 0xa0,
  7661. 0x07, 0xf1, 0xf6, 0x53, 0xb0, 0x44, 0x82, 0x40,
  7662. 0xd2, 0xab, 0x0e, 0x71, 0x5d, 0xe1, 0xea, 0x3a,
  7663. 0x77, 0xc9, 0xef, 0xfe, 0x54, 0x65, 0xa3, 0x49,
  7664. 0xfd, 0xa5, 0x33, 0xaa, 0x16, 0x1a, 0x38, 0xe7,
  7665. 0xaa, 0xb7, 0x13, 0xb2, 0x3b, 0xc7, 0x00, 0x87,
  7666. 0x12, 0xfe, 0xfd, 0xf4, 0x55, 0x6d, 0x1d, 0x4a,
  7667. 0x0e, 0xad, 0xd0, 0x4c, 0x55, 0x91, 0x60, 0xd9,
  7668. 0xef, 0x74, 0x69, 0x22, 0x8c, 0x51, 0x65, 0xc2,
  7669. 0x04, 0xac, 0xd3, 0x8d, 0xf7, 0x35, 0x29, 0x13,
  7670. 0x6d, 0x61, 0x7c, 0x39, 0x2f, 0x41, 0x4c, 0xdf,
  7671. 0x38, 0xfd, 0x1a, 0x7d, 0x42, 0xa7, 0x6f, 0x3f,
  7672. 0x3d, 0x9b, 0xd1, 0x97, 0xab, 0xc0, 0xa7, 0x28,
  7673. 0x1c, 0xc0, 0x02, 0x26, 0xeb, 0xce, 0xf9, 0xe1,
  7674. 0x34, 0x45, 0xaf, 0xbf, 0x8d, 0xb8, 0xe0, 0xff,
  7675. 0xd9, 0x6f, 0x77, 0xf3, 0xf7, 0xed, 0x6a, 0xbb,
  7676. 0x03, 0x52, 0xfb, 0x38, 0xfc, 0xea, 0x9f, 0xc9,
  7677. 0x98, 0xed, 0x21, 0x45, 0xaf, 0x43, 0x2b, 0x64,
  7678. 0x96, 0x82, 0x30, 0xe9, 0xb4, 0x36, 0x89, 0x77,
  7679. 0x07, 0x4a, 0xc6, 0x1f, 0x38, 0x7a, 0xee, 0xb6,
  7680. 0x86, 0xf6, 0x2f, 0x03, 0xec, 0xa2, 0xe5, 0x48,
  7681. 0xe5, 0x5a, 0xf5, 0x1c, 0xd2, 0xd9, 0xd8, 0x2d,
  7682. 0x9d, 0x06, 0x07, 0xc9, 0x8b, 0x5d, 0xe0, 0x0f,
  7683. 0x5e, 0x0c, 0x53, 0x27, 0xff, 0x23, 0xee, 0xca,
  7684. 0x5e, 0x4d, 0xf1, 0x95, 0x77, 0x78, 0x1f, 0xf2,
  7685. 0x44, 0x5b, 0x7d, 0x01, 0x49, 0x61, 0x6f, 0x6d,
  7686. 0xbf, 0xf5, 0x19, 0x06, 0x39, 0xe9, 0xe9, 0x29,
  7687. 0xde, 0x47, 0x5e, 0x2e, 0x1f, 0x68, 0xf4, 0x32,
  7688. 0x5e, 0xe9, 0xd0, 0xa7, 0xb4, 0x2a, 0x45, 0xdf,
  7689. 0x15, 0x7d, 0x0d, 0x5b, 0xef, 0xc6, 0x23, 0xac
  7690. };
  7691. #else
  7692. #error Not Supported Yet!
  7693. #endif
  7694. #endif /* WOLFSSL_RSA_VERIFY_INLINE || WOLFSSL_RSA_PUBLIC_ONLY */
  7695. static void bench_rsa_helper(int useDeviceID,
  7696. WC_ARRAY_ARG(rsaKey,
  7697. RsaKey,
  7698. BENCH_MAX_PENDING,
  7699. sizeof(RsaKey)),
  7700. word32 rsaKeySz)
  7701. {
  7702. int ret = 0, i, times, count = 0, pending = 0;
  7703. word32 idx = 0;
  7704. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  7705. const char* messageStr = TEST_STRING;
  7706. const int len = (int)TEST_STRING_SZ;
  7707. #endif
  7708. double start = 0.0F;
  7709. const char**desc = bench_desc_words[lng_index];
  7710. DECLARE_MULTI_VALUE_STATS_VARS()
  7711. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  7712. WC_DECLARE_VAR(message, byte, TEST_STRING_SZ, HEAP_HINT);
  7713. #endif
  7714. WC_DECLARE_HEAP_ARRAY(enc, byte, BENCH_MAX_PENDING,
  7715. rsaKeySz, HEAP_HINT);
  7716. #if (!defined(WOLFSSL_RSA_VERIFY_INLINE) && \
  7717. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  7718. WC_DECLARE_HEAP_ARRAY(out, byte, BENCH_MAX_PENDING,
  7719. rsaKeySz, HEAP_HINT);
  7720. #else
  7721. byte* out[BENCH_MAX_PENDING];
  7722. #endif
  7723. XMEMSET(out, 0, sizeof(out));
  7724. WC_ALLOC_HEAP_ARRAY(enc, byte, BENCH_MAX_PENDING,
  7725. rsaKeySz, HEAP_HINT);
  7726. #if (!defined(WOLFSSL_RSA_VERIFY_INLINE) && \
  7727. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  7728. WC_ALLOC_HEAP_ARRAY(out, byte, BENCH_MAX_PENDING,
  7729. rsaKeySz, HEAP_HINT);
  7730. if (out[0] == NULL) {
  7731. ret = MEMORY_E;
  7732. goto exit;
  7733. }
  7734. #endif
  7735. if (enc[0] == NULL) {
  7736. ret = MEMORY_E;
  7737. goto exit;
  7738. }
  7739. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  7740. WC_ALLOC_VAR(message, byte, TEST_STRING_SZ, HEAP_HINT);
  7741. XMEMCPY(message, messageStr, len);
  7742. #endif
  7743. if (!rsa_sign_verify) {
  7744. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  7745. /* begin public RSA */
  7746. bench_stats_start(&count, &start);
  7747. do {
  7748. for (times = 0; times < ntimes || pending > 0; ) {
  7749. bench_async_poll(&pending);
  7750. /* while free pending slots in queue, submit ops */
  7751. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7752. if (bench_async_check(&ret,
  7753. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7754. 1, &times, ntimes, &pending)) {
  7755. ret = wc_RsaPublicEncrypt(message, (word32)len, enc[i],
  7756. rsaKeySz/8, rsaKey[i],
  7757. GLOBAL_RNG);
  7758. if (!bench_async_handle(&ret,
  7759. BENCH_ASYNC_GET_DEV(
  7760. rsaKey[i]), 1, &times,
  7761. &pending)) {
  7762. goto exit_rsa_verify;
  7763. }
  7764. }
  7765. } /* for i */
  7766. RECORD_MULTI_VALUE_STATS();
  7767. } /* for times */
  7768. count += times;
  7769. } while (bench_stats_check(start)
  7770. #ifdef MULTI_VALUE_STATISTICS
  7771. || runs < minimum_runs
  7772. #endif
  7773. );
  7774. exit_rsa_verify:
  7775. bench_stats_asym_finish("RSA", (int)rsaKeySz, desc[0],
  7776. useDeviceID, count, start, ret);
  7777. #ifdef MULTI_VALUE_STATISTICS
  7778. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7779. #endif
  7780. #endif /* !WOLFSSL_RSA_VERIFY_ONLY */
  7781. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  7782. if (ret < 0) {
  7783. goto exit;
  7784. }
  7785. RESET_MULTI_VALUE_STATS_VARS();
  7786. /* capture resulting encrypt length */
  7787. idx = (word32)(rsaKeySz/8);
  7788. /* begin private async RSA */
  7789. bench_stats_start(&count, &start);
  7790. do {
  7791. for (times = 0; times < ntimes || pending > 0; ) {
  7792. bench_async_poll(&pending);
  7793. /* while free pending slots in queue, submit ops */
  7794. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7795. if (bench_async_check(&ret,
  7796. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7797. 1, &times, ntimes, &pending)) {
  7798. ret = wc_RsaPrivateDecrypt(enc[i], idx, out[i],
  7799. rsaKeySz/8, rsaKey[i]);
  7800. if (!bench_async_handle(&ret,
  7801. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7802. 1, &times, &pending)) {
  7803. goto exit_rsa_pub;
  7804. }
  7805. }
  7806. } /* for i */
  7807. RECORD_MULTI_VALUE_STATS();
  7808. } /* for times */
  7809. count += times;
  7810. } while (bench_stats_check(start)
  7811. #ifdef MULTI_VALUE_STATISTICS
  7812. || runs < minimum_runs
  7813. #endif
  7814. );
  7815. exit_rsa_pub:
  7816. bench_stats_asym_finish("RSA", (int)rsaKeySz, desc[1],
  7817. useDeviceID, count, start, ret);
  7818. #ifdef MULTI_VALUE_STATISTICS
  7819. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7820. #endif
  7821. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY */
  7822. }
  7823. else {
  7824. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  7825. /* begin RSA sign */
  7826. bench_stats_start(&count, &start);
  7827. do {
  7828. for (times = 0; times < ntimes || pending > 0; ) {
  7829. bench_async_poll(&pending);
  7830. /* while free pending slots in queue, submit ops */
  7831. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7832. if (bench_async_check(&ret,
  7833. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7834. 1, &times, ntimes, &pending)) {
  7835. ret = wc_RsaSSL_Sign(message, len, enc[i],
  7836. rsaKeySz/8, rsaKey[i], GLOBAL_RNG);
  7837. if (!bench_async_handle(&ret,
  7838. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7839. 1, &times, &pending)) {
  7840. goto exit_rsa_sign;
  7841. }
  7842. }
  7843. } /* for i */
  7844. RECORD_MULTI_VALUE_STATS();
  7845. } /* for times */
  7846. count += times;
  7847. } while (bench_stats_check(start)
  7848. #ifdef MULTI_VALUE_STATISTICS
  7849. || runs < minimum_runs
  7850. #endif
  7851. );
  7852. exit_rsa_sign:
  7853. bench_stats_asym_finish("RSA", (int)rsaKeySz, desc[4], useDeviceID,
  7854. count, start, ret);
  7855. #ifdef MULTI_VALUE_STATISTICS
  7856. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7857. #endif
  7858. if (ret < 0) {
  7859. goto exit;
  7860. }
  7861. RESET_MULTI_VALUE_STATS_VARS();
  7862. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  7863. /* capture resulting encrypt length */
  7864. idx = rsaKeySz/8;
  7865. /* begin RSA verify */
  7866. bench_stats_start(&count, &start);
  7867. do {
  7868. for (times = 0; times < ntimes || pending > 0; ) {
  7869. bench_async_poll(&pending);
  7870. /* while free pending slots in queue, submit ops */
  7871. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7872. if (bench_async_check(&ret,
  7873. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7874. 1, &times, ntimes, &pending)) {
  7875. #if !defined(WOLFSSL_RSA_VERIFY_INLINE) && \
  7876. !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  7877. ret = wc_RsaSSL_Verify(enc[i], idx, out[i],
  7878. rsaKeySz/8, rsaKey[i]);
  7879. #elif defined(USE_CERT_BUFFERS_2048)
  7880. XMEMCPY(enc[i], rsa_2048_sig, sizeof(rsa_2048_sig));
  7881. idx = sizeof(rsa_2048_sig);
  7882. out[i] = NULL;
  7883. ret = wc_RsaSSL_VerifyInline(enc[i], idx,
  7884. &out[i], rsaKey[i]);
  7885. if (ret > 0) {
  7886. ret = 0;
  7887. }
  7888. #elif defined(USE_CERT_BUFFERS_3072)
  7889. XMEMCPY(enc[i], rsa_3072_sig, sizeof(rsa_3072_sig));
  7890. idx = sizeof(rsa_3072_sig);
  7891. out[i] = NULL;
  7892. ret = wc_RsaSSL_VerifyInline(enc[i], idx,
  7893. &out[i], rsaKey[i]);
  7894. if (ret > 0)
  7895. ret = 0;
  7896. #endif
  7897. if (!bench_async_handle(&ret,
  7898. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7899. 1, &times, &pending)) {
  7900. goto exit_rsa_verifyinline;
  7901. }
  7902. }
  7903. } /* for i */
  7904. RECORD_MULTI_VALUE_STATS();
  7905. } /* for times */
  7906. count += times;
  7907. } while (bench_stats_check(start)
  7908. #ifdef MULTI_VALUE_STATISTICS
  7909. || runs < minimum_runs
  7910. #endif
  7911. );
  7912. exit_rsa_verifyinline:
  7913. bench_stats_asym_finish("RSA", (int)rsaKeySz, desc[5],
  7914. useDeviceID, count, start, ret);
  7915. #ifdef MULTI_VALUE_STATISTICS
  7916. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7917. #endif
  7918. }
  7919. exit:
  7920. WC_FREE_HEAP_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  7921. #if !defined(WOLFSSL_RSA_VERIFY_INLINE) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  7922. WC_FREE_HEAP_ARRAY(out, BENCH_MAX_PENDING, HEAP_HINT);
  7923. #endif
  7924. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  7925. WC_FREE_VAR(message, HEAP_HINT);
  7926. #endif
  7927. }
  7928. void bench_rsa(int useDeviceID)
  7929. {
  7930. int i;
  7931. WC_DECLARE_ARRAY(rsaKey, RsaKey, BENCH_MAX_PENDING,
  7932. sizeof(RsaKey), HEAP_HINT);
  7933. int ret = 0;
  7934. word32 rsaKeySz = 0;
  7935. const byte* tmp;
  7936. size_t bytes;
  7937. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  7938. word32 idx;
  7939. #endif
  7940. WC_CALLOC_ARRAY(rsaKey, RsaKey, BENCH_MAX_PENDING,
  7941. sizeof(RsaKey), HEAP_HINT);
  7942. #ifdef USE_CERT_BUFFERS_1024
  7943. tmp = rsa_key_der_1024;
  7944. bytes = (size_t)sizeof_rsa_key_der_1024;
  7945. rsaKeySz = 1024;
  7946. #elif defined(USE_CERT_BUFFERS_2048)
  7947. tmp = rsa_key_der_2048;
  7948. bytes = (size_t)sizeof_rsa_key_der_2048;
  7949. rsaKeySz = 2048;
  7950. #elif defined(USE_CERT_BUFFERS_3072)
  7951. tmp = rsa_key_der_3072;
  7952. bytes = (size_t)sizeof_rsa_key_der_3072;
  7953. rsaKeySz = 3072;
  7954. #elif defined(USE_CERT_BUFFERS_4096)
  7955. tmp = client_key_der_4096;
  7956. bytes = (size_t)sizeof_client_key_der_4096;
  7957. rsaKeySz = 4096;
  7958. #else
  7959. #error "need a cert buffer size"
  7960. #endif /* USE_CERT_BUFFERS */
  7961. /* init keys */
  7962. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7963. /* setup an async context for each key */
  7964. ret = wc_InitRsaKey_ex(rsaKey[i], HEAP_HINT,
  7965. useDeviceID ? devId : INVALID_DEVID);
  7966. if (ret < 0) {
  7967. goto exit;
  7968. }
  7969. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  7970. #ifdef WC_RSA_BLINDING
  7971. ret = wc_RsaSetRNG(rsaKey[i], &gRng);
  7972. if (ret != 0)
  7973. goto exit;
  7974. #endif
  7975. #endif
  7976. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  7977. /* decode the private key */
  7978. idx = 0;
  7979. if ((ret = wc_RsaPrivateKeyDecode(tmp, &idx,
  7980. rsaKey[i], (word32)bytes)) != 0) {
  7981. printf("wc_RsaPrivateKeyDecode failed! %d\n", ret);
  7982. goto exit;
  7983. }
  7984. #elif defined(WOLFSSL_PUBLIC_MP)
  7985. /* get offset to public portion of the RSA key */
  7986. #ifdef USE_CERT_BUFFERS_1024
  7987. bytes = 11;
  7988. #elif defined(USE_CERT_BUFFERS_2048) || defined(USE_CERT_BUFFERS_3072)
  7989. bytes = 12;
  7990. #endif
  7991. ret = mp_read_unsigned_bin(&rsaKey[i]->n, &tmp[bytes], rsaKeySz/8);
  7992. if (ret != 0) {
  7993. printf("wc_RsaPrivateKeyDecode failed! %d\n", ret);
  7994. goto exit;
  7995. }
  7996. ret = mp_set_int(&rsaKey[i]->e, WC_RSA_EXPONENT);
  7997. if (ret != 0) {
  7998. printf("wc_RsaPrivateKeyDecode failed! %d\n", ret);
  7999. goto exit;
  8000. }
  8001. #else
  8002. /* Note: To benchmark public only define WOLFSSL_PUBLIC_MP */
  8003. rsaKeySz = 0;
  8004. #endif
  8005. }
  8006. if (rsaKeySz > 0) {
  8007. bench_rsa_helper(useDeviceID, rsaKey, rsaKeySz);
  8008. }
  8009. (void)bytes;
  8010. (void)tmp;
  8011. exit:
  8012. /* cleanup */
  8013. if (WC_ARRAY_OK(rsaKey)) {
  8014. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  8015. wc_FreeRsaKey(rsaKey[i]);
  8016. }
  8017. WC_FREE_ARRAY(rsaKey, BENCH_MAX_PENDING, HEAP_HINT);
  8018. }
  8019. }
  8020. #ifdef WOLFSSL_KEY_GEN
  8021. /* bench any size of RSA key */
  8022. void bench_rsa_key(int useDeviceID, word32 rsaKeySz)
  8023. {
  8024. int ret = 0, i, pending = 0;
  8025. WC_DECLARE_ARRAY(rsaKey, RsaKey, BENCH_MAX_PENDING,
  8026. sizeof(RsaKey), HEAP_HINT);
  8027. int isPending[BENCH_MAX_PENDING];
  8028. long exp = 65537L;
  8029. /* clear for done cleanup */
  8030. XMEMSET(isPending, 0, sizeof(isPending));
  8031. WC_CALLOC_ARRAY(rsaKey, RsaKey, BENCH_MAX_PENDING,
  8032. sizeof(RsaKey), HEAP_HINT);
  8033. /* init keys */
  8034. do {
  8035. pending = 0;
  8036. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  8037. if (!isPending[i]) { /* if making the key is pending then just call
  8038. * wc_MakeRsaKey again */
  8039. /* setup an async context for each key */
  8040. if (wc_InitRsaKey_ex(rsaKey[i], HEAP_HINT,
  8041. useDeviceID ? devId : INVALID_DEVID) < 0) {
  8042. goto exit;
  8043. }
  8044. #ifdef WC_RSA_BLINDING
  8045. ret = wc_RsaSetRNG(rsaKey[i], &gRng);
  8046. if (ret != 0)
  8047. goto exit;
  8048. #endif
  8049. }
  8050. /* create the RSA key */
  8051. ret = wc_MakeRsaKey(rsaKey[i], (int)rsaKeySz, exp, &gRng);
  8052. if (ret == WC_PENDING_E) {
  8053. isPending[i] = 1;
  8054. pending = 1;
  8055. }
  8056. else if (ret != 0) {
  8057. printf("wc_MakeRsaKey failed! %d\n", ret);
  8058. goto exit;
  8059. }
  8060. } /* for i */
  8061. } while (pending > 0);
  8062. bench_rsa_helper(useDeviceID, rsaKey, rsaKeySz);
  8063. exit:
  8064. /* cleanup */
  8065. if (WC_ARRAY_OK(rsaKey)) {
  8066. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  8067. wc_FreeRsaKey(rsaKey[i]);
  8068. }
  8069. WC_FREE_ARRAY(rsaKey, BENCH_MAX_PENDING, HEAP_HINT);
  8070. }
  8071. }
  8072. #endif /* WOLFSSL_KEY_GEN */
  8073. #endif /* !NO_RSA */
  8074. #ifndef NO_DH
  8075. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  8076. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  8077. #if defined(WOLFSSL_MDK_SHELL)
  8078. static char *certDHname = "certs/dh2048.der";
  8079. /* set by shell command */
  8080. void set_Bench_DH_File(char * cert) { certDHname = cert ; }
  8081. #elif defined(FREESCALE_MQX)
  8082. static char *certDHname = "a:\\certs\\dh2048.der";
  8083. #elif defined(NO_ASN)
  8084. /* do nothing, but don't need a file */
  8085. #else
  8086. static const char *certDHname = "certs/dh2048.der";
  8087. #endif
  8088. #endif
  8089. #ifdef HAVE_FFDHE_4096
  8090. #define BENCH_DH_KEY_SIZE 512 /* for 4096 bit */
  8091. #else
  8092. #define BENCH_DH_KEY_SIZE 384 /* for 3072 bit */
  8093. #endif
  8094. #define BENCH_DH_PRIV_SIZE (BENCH_DH_KEY_SIZE/8)
  8095. void bench_dh(int useDeviceID)
  8096. {
  8097. int ret = 0, i;
  8098. int count = 0, times, pending = 0;
  8099. const byte* tmp = NULL;
  8100. double start = 0.0F;
  8101. WC_DECLARE_ARRAY(dhKey, DhKey, BENCH_MAX_PENDING,
  8102. sizeof(DhKey), HEAP_HINT);
  8103. int dhKeySz = BENCH_DH_KEY_SIZE * 8; /* used in printf */
  8104. const char**desc = bench_desc_words[lng_index];
  8105. #ifndef NO_ASN
  8106. size_t bytes = 0;
  8107. word32 idx;
  8108. #endif
  8109. word32 pubSz[BENCH_MAX_PENDING];
  8110. word32 privSz[BENCH_MAX_PENDING];
  8111. word32 pubSz2 = BENCH_DH_KEY_SIZE;
  8112. word32 privSz2 = BENCH_DH_PRIV_SIZE;
  8113. word32 agreeSz[BENCH_MAX_PENDING];
  8114. #if defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072) || defined(HAVE_FFDHE_4096)
  8115. #ifdef HAVE_PUBLIC_FFDHE
  8116. const DhParams *params = NULL;
  8117. #else
  8118. int paramName = 0;
  8119. #endif
  8120. #endif
  8121. DECLARE_MULTI_VALUE_STATS_VARS()
  8122. WC_DECLARE_ARRAY(pub, byte, BENCH_MAX_PENDING,
  8123. BENCH_DH_KEY_SIZE, HEAP_HINT);
  8124. WC_DECLARE_VAR(pub2, byte,
  8125. BENCH_DH_KEY_SIZE, HEAP_HINT);
  8126. WC_DECLARE_ARRAY(agree, byte, BENCH_MAX_PENDING,
  8127. BENCH_DH_KEY_SIZE, HEAP_HINT);
  8128. WC_DECLARE_ARRAY(priv, byte, BENCH_MAX_PENDING,
  8129. BENCH_DH_PRIV_SIZE, HEAP_HINT);
  8130. WC_DECLARE_VAR(priv2, byte,
  8131. BENCH_DH_PRIV_SIZE, HEAP_HINT);
  8132. /* old scan-build misfires -Wmaybe-uninitialized on these. */
  8133. XMEMSET(pub, 0, sizeof(pub));
  8134. XMEMSET(agree, 0, sizeof(agree));
  8135. XMEMSET(priv, 0, sizeof(priv));
  8136. WC_CALLOC_ARRAY(dhKey, DhKey, BENCH_MAX_PENDING,
  8137. sizeof(DhKey), HEAP_HINT);
  8138. WC_ALLOC_ARRAY(pub, byte,
  8139. BENCH_MAX_PENDING, BENCH_DH_KEY_SIZE, HEAP_HINT);
  8140. WC_ALLOC_ARRAY(agree, byte,
  8141. BENCH_MAX_PENDING, BENCH_DH_KEY_SIZE, HEAP_HINT);
  8142. WC_ALLOC_ARRAY(priv, byte,
  8143. BENCH_MAX_PENDING, BENCH_DH_PRIV_SIZE, HEAP_HINT);
  8144. WC_ALLOC_VAR(pub2, byte, BENCH_DH_KEY_SIZE, HEAP_HINT);
  8145. WC_ALLOC_VAR(priv2, byte, BENCH_DH_PRIV_SIZE, HEAP_HINT);
  8146. (void)tmp;
  8147. if (!use_ffdhe) {
  8148. #if defined(NO_ASN)
  8149. dhKeySz = 1024;
  8150. /* do nothing, but don't use default FILE */
  8151. #elif defined(USE_CERT_BUFFERS_1024)
  8152. tmp = dh_key_der_1024;
  8153. bytes = (size_t)sizeof_dh_key_der_1024;
  8154. dhKeySz = 1024;
  8155. #elif defined(USE_CERT_BUFFERS_2048)
  8156. tmp = dh_key_der_2048;
  8157. bytes = (size_t)sizeof_dh_key_der_2048;
  8158. dhKeySz = 2048;
  8159. #elif defined(USE_CERT_BUFFERS_3072)
  8160. tmp = dh_key_der_3072;
  8161. bytes = (size_t)sizeof_dh_key_der_3072;
  8162. dhKeySz = 3072;
  8163. #elif defined(USE_CERT_BUFFERS_4096)
  8164. tmp = dh_key_der_4096;
  8165. bytes = (size_t)sizeof_dh_key_der_4096;
  8166. dhKeySz = 4096;
  8167. #else
  8168. #error "need to define a cert buffer size"
  8169. #endif /* USE_CERT_BUFFERS */
  8170. }
  8171. #ifdef HAVE_FFDHE_2048
  8172. else if (use_ffdhe == 2048) {
  8173. #ifdef HAVE_PUBLIC_FFDHE
  8174. params = wc_Dh_ffdhe2048_Get();
  8175. #else
  8176. paramName = WC_FFDHE_2048;
  8177. #endif
  8178. dhKeySz = 2048;
  8179. }
  8180. #endif
  8181. #ifdef HAVE_FFDHE_3072
  8182. else if (use_ffdhe == 3072) {
  8183. #ifdef HAVE_PUBLIC_FFDHE
  8184. params = wc_Dh_ffdhe3072_Get();
  8185. #else
  8186. paramName = WC_FFDHE_3072;
  8187. #endif
  8188. dhKeySz = 3072;
  8189. }
  8190. #endif
  8191. #ifdef HAVE_FFDHE_4096
  8192. else if (use_ffdhe == 4096) {
  8193. #ifdef HAVE_PUBLIC_FFDHE
  8194. params = wc_Dh_ffdhe4096_Get();
  8195. #else
  8196. paramName = WC_FFDHE_4096;
  8197. #endif
  8198. dhKeySz = 4096;
  8199. }
  8200. #endif
  8201. /* init keys */
  8202. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  8203. /* setup an async context for each key */
  8204. ret = wc_InitDhKey_ex(dhKey[i], HEAP_HINT,
  8205. useDeviceID ? devId : INVALID_DEVID);
  8206. if (ret != 0)
  8207. goto exit;
  8208. /* setup key */
  8209. if (!use_ffdhe) {
  8210. #ifdef NO_ASN
  8211. ret = wc_DhSetKey(dhKey[i], dh_p,
  8212. sizeof(dh_p), dh_g, sizeof(dh_g));
  8213. #else
  8214. idx = 0;
  8215. ret = wc_DhKeyDecode(tmp, &idx, dhKey[i], (word32)bytes);
  8216. #endif
  8217. }
  8218. #if defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072)
  8219. #ifdef HAVE_PUBLIC_FFDHE
  8220. else if (params != NULL) {
  8221. ret = wc_DhSetKey(dhKey[i], params->p, params->p_len,
  8222. params->g, params->g_len);
  8223. }
  8224. #else
  8225. else if (paramName != 0) {
  8226. ret = wc_DhSetNamedKey(dhKey[i], paramName);
  8227. }
  8228. #endif
  8229. #endif
  8230. if (ret != 0) {
  8231. printf("DhKeyDecode failed %d, can't benchmark\n", ret);
  8232. goto exit;
  8233. }
  8234. }
  8235. /* Key Gen */
  8236. bench_stats_start(&count, &start);
  8237. PRIVATE_KEY_UNLOCK();
  8238. do {
  8239. /* while free pending slots in queue, submit ops */
  8240. for (times = 0; times < genTimes || pending > 0; ) {
  8241. bench_async_poll(&pending);
  8242. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  8243. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(dhKey[i]),
  8244. 0, &times, genTimes, &pending)) {
  8245. privSz[i] = BENCH_DH_PRIV_SIZE;
  8246. pubSz[i] = BENCH_DH_KEY_SIZE;
  8247. ret = wc_DhGenerateKeyPair(dhKey[i], &gRng,
  8248. priv[i], &privSz[i],
  8249. pub[i], &pubSz[i]);
  8250. if (!bench_async_handle(&ret,
  8251. BENCH_ASYNC_GET_DEV(dhKey[i]),
  8252. 0, &times, &pending)) {
  8253. goto exit_dh_gen;
  8254. }
  8255. }
  8256. } /* for i */
  8257. RECORD_MULTI_VALUE_STATS();
  8258. } /* for times */
  8259. count += times;
  8260. } while (bench_stats_check(start)
  8261. #ifdef MULTI_VALUE_STATISTICS
  8262. || runs < minimum_runs
  8263. #endif
  8264. );
  8265. PRIVATE_KEY_LOCK();
  8266. exit_dh_gen:
  8267. bench_stats_asym_finish("DH", dhKeySz, desc[2],
  8268. useDeviceID, count, start, ret);
  8269. #ifdef MULTI_VALUE_STATISTICS
  8270. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8271. #endif
  8272. if (ret < 0) {
  8273. goto exit;
  8274. }
  8275. RESET_MULTI_VALUE_STATS_VARS();
  8276. /* Generate key to use as other public */
  8277. PRIVATE_KEY_UNLOCK();
  8278. ret = wc_DhGenerateKeyPair(dhKey[0], &gRng,
  8279. priv2, &privSz2, pub2, &pubSz2);
  8280. PRIVATE_KEY_LOCK();
  8281. #ifdef WOLFSSL_ASYNC_CRYPT
  8282. ret = wc_AsyncWait(ret, &dhKey[0]->asyncDev, WC_ASYNC_FLAG_NONE);
  8283. #endif
  8284. /* Key Agree */
  8285. bench_stats_start(&count, &start);
  8286. PRIVATE_KEY_UNLOCK();
  8287. do {
  8288. for (times = 0; times < agreeTimes || pending > 0; ) {
  8289. bench_async_poll(&pending);
  8290. /* while free pending slots in queue, submit ops */
  8291. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  8292. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(dhKey[i]),
  8293. 0, &times, agreeTimes, &pending)) {
  8294. ret = wc_DhAgree(dhKey[i], agree[i], &agreeSz[i], priv[i],
  8295. privSz[i], pub2, pubSz2);
  8296. if (!bench_async_handle(&ret,
  8297. BENCH_ASYNC_GET_DEV(dhKey[i]), 0, &times, &pending)) {
  8298. goto exit;
  8299. }
  8300. }
  8301. } /* for i */
  8302. RECORD_MULTI_VALUE_STATS();
  8303. } /* for times */
  8304. count += times;
  8305. } while (bench_stats_check(start)
  8306. #ifdef MULTI_VALUE_STATISTICS
  8307. || runs < minimum_runs
  8308. #endif
  8309. );
  8310. PRIVATE_KEY_LOCK();
  8311. exit:
  8312. bench_stats_asym_finish("DH", dhKeySz, desc[3],
  8313. useDeviceID, count, start, ret);
  8314. #ifdef MULTI_VALUE_STATISTICS
  8315. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8316. #endif
  8317. /* cleanup */
  8318. if (WC_ARRAY_OK(dhKey)) {
  8319. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  8320. wc_FreeDhKey(dhKey[i]);
  8321. }
  8322. WC_FREE_ARRAY(dhKey, BENCH_MAX_PENDING, HEAP_HINT);
  8323. }
  8324. WC_FREE_ARRAY(pub, BENCH_MAX_PENDING, HEAP_HINT);
  8325. WC_FREE_VAR(pub2, HEAP_HINT);
  8326. WC_FREE_ARRAY(priv, BENCH_MAX_PENDING, HEAP_HINT);
  8327. WC_FREE_VAR(priv2, HEAP_HINT);
  8328. WC_FREE_ARRAY(agree, BENCH_MAX_PENDING, HEAP_HINT);
  8329. }
  8330. #endif /* !NO_DH */
  8331. #ifdef WOLFSSL_HAVE_KYBER
  8332. static void bench_kyber_keygen(int type, const char* name, int keySize,
  8333. KyberKey* key)
  8334. {
  8335. int ret = 0, times, count, pending = 0;
  8336. double start;
  8337. const char**desc = bench_desc_words[lng_index];
  8338. DECLARE_MULTI_VALUE_STATS_VARS()
  8339. /* KYBER Make Key */
  8340. bench_stats_start(&count, &start);
  8341. do {
  8342. /* while free pending slots in queue, submit ops */
  8343. for (times = 0; times < agreeTimes || pending > 0; times++) {
  8344. wc_KyberKey_Free(key);
  8345. ret = wc_KyberKey_Init(type, key, HEAP_HINT, INVALID_DEVID);
  8346. if (ret != 0)
  8347. goto exit;
  8348. #ifdef KYBER_NONDETERMINISTIC
  8349. ret = wc_KyberKey_MakeKey(key, &gRng);
  8350. #else
  8351. unsigned char rand[KYBER_MAKEKEY_RAND_SZ] = {0,};
  8352. ret = wc_KyberKey_MakeKeyWithRandom(key, rand, sizeof(rand));
  8353. #endif
  8354. if (ret != 0)
  8355. goto exit;
  8356. RECORD_MULTI_VALUE_STATS();
  8357. } /* for times */
  8358. count += times;
  8359. } while (bench_stats_check(start)
  8360. #ifdef MULTI_VALUE_STATISTICS
  8361. || runs < minimum_runs
  8362. #endif
  8363. );
  8364. exit:
  8365. bench_stats_asym_finish(name, keySize, desc[2], 0, count, start, ret);
  8366. #ifdef MULTI_VALUE_STATISTICS
  8367. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8368. #endif
  8369. }
  8370. static void bench_kyber_encap(const char* name, int keySize, KyberKey* key)
  8371. {
  8372. int ret = 0, times, count, pending = 0;
  8373. double start;
  8374. const char**desc = bench_desc_words[lng_index];
  8375. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  8376. byte ss[KYBER_SS_SZ];
  8377. word32 ctSz;
  8378. DECLARE_MULTI_VALUE_STATS_VARS()
  8379. ret = wc_KyberKey_CipherTextSize(key, &ctSz);
  8380. if (ret != 0) {
  8381. return;
  8382. }
  8383. /* KYBER Encapsulate */
  8384. bench_stats_start(&count, &start);
  8385. do {
  8386. /* while free pending slots in queue, submit ops */
  8387. for (times = 0; times < agreeTimes || pending > 0; times++) {
  8388. #ifdef KYBER_NONDETERMINISTIC
  8389. ret = wc_KyberKey_Encapsulate(key, ct, ss, &gRng);
  8390. #else
  8391. unsigned char rand[KYBER_ENC_RAND_SZ] = {0,};
  8392. ret = wc_KyberKey_EncapsulateWithRandom(key, ct, ss, rand,
  8393. sizeof(rand));
  8394. #endif
  8395. if (ret != 0)
  8396. goto exit_encap;
  8397. RECORD_MULTI_VALUE_STATS();
  8398. } /* for times */
  8399. count += times;
  8400. } while (bench_stats_check(start)
  8401. #ifdef MULTI_VALUE_STATISTICS
  8402. || runs < minimum_runs
  8403. #endif
  8404. );
  8405. exit_encap:
  8406. bench_stats_asym_finish(name, keySize, desc[9], 0, count, start, ret);
  8407. #ifdef MULTI_VALUE_STATISTICS
  8408. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8409. #endif
  8410. RESET_MULTI_VALUE_STATS_VARS();
  8411. /* KYBER Decapsulate */
  8412. bench_stats_start(&count, &start);
  8413. do {
  8414. /* while free pending slots in queue, submit ops */
  8415. for (times = 0; times < agreeTimes || pending > 0; times++) {
  8416. ret = wc_KyberKey_Decapsulate(key, ss, ct, ctSz);
  8417. if (ret != 0)
  8418. goto exit_decap;
  8419. RECORD_MULTI_VALUE_STATS();
  8420. } /* for times */
  8421. count += times;
  8422. } while (bench_stats_check(start)
  8423. #ifdef MULTI_VALUE_STATISTICS
  8424. || runs < minimum_runs
  8425. #endif
  8426. );
  8427. exit_decap:
  8428. bench_stats_asym_finish(name, keySize, desc[13], 0, count, start, ret);
  8429. #ifdef MULTI_VALUE_STATISTICS
  8430. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8431. #endif
  8432. }
  8433. void bench_kyber(int type)
  8434. {
  8435. KyberKey key;
  8436. const char* name = NULL;
  8437. int keySize = 0;
  8438. switch (type) {
  8439. #ifdef WOLFSSL_KYBER512
  8440. case KYBER512:
  8441. name = "KYBER512 ";
  8442. keySize = 128;
  8443. break;
  8444. #endif
  8445. #ifdef WOLFSSL_KYBER768
  8446. case KYBER768:
  8447. name = "KYBER768 ";
  8448. keySize = 192;
  8449. break;
  8450. #endif
  8451. #ifdef WOLFSSL_KYBER1024
  8452. case KYBER1024:
  8453. name = "KYBER1024";
  8454. keySize = 256;
  8455. break;
  8456. #endif
  8457. }
  8458. bench_kyber_keygen(type, name, keySize, &key);
  8459. bench_kyber_encap(name, keySize, &key);
  8460. wc_KyberKey_Free(&key);
  8461. }
  8462. #endif
  8463. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  8464. /* WC_LMS_PARM_L2_H10_W2
  8465. * signature length: 9300 */
  8466. static const byte lms_priv_L2_H10_W2[64] =
  8467. {
  8468. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8469. 0x62,0x62,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  8470. 0xC7,0x74,0x25,0x5B,0x2C,0xE8,0xDA,0x53,
  8471. 0xF0,0x7C,0x04,0x3F,0x64,0x2D,0x26,0x2C,
  8472. 0x46,0x1D,0xC8,0x90,0x77,0x59,0xD6,0xC0,
  8473. 0x56,0x46,0x7D,0x97,0x64,0xF2,0xA3,0xA1,
  8474. 0xF8,0xD0,0x3B,0x5F,0xAC,0x40,0xB9,0x9E,
  8475. 0x83,0x67,0xBF,0x92,0x8D,0xFE,0x45,0x79
  8476. };
  8477. static const byte lms_pub_L2_H10_W2[60] =
  8478. {
  8479. 0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x06,
  8480. 0x00,0x00,0x00,0x02,0xF8,0xD0,0x3B,0x5F,
  8481. 0xAC,0x40,0xB9,0x9E,0x83,0x67,0xBF,0x92,
  8482. 0x8D,0xFE,0x45,0x79,0x41,0xBC,0x2A,0x3B,
  8483. 0x9F,0xC0,0x11,0x12,0x93,0xF0,0x5A,0xA5,
  8484. 0xC1,0x88,0x29,0x79,0x6C,0x3E,0x0A,0x0F,
  8485. 0xEC,0x3B,0x3E,0xE4,0x38,0xD3,0xD2,0x34,
  8486. 0x7F,0xC8,0x91,0xB0
  8487. };
  8488. /* WC_LMS_PARM_L2_H10_W4
  8489. * signature length: 5076 */
  8490. static const byte lms_priv_L2_H10_W4[64] =
  8491. {
  8492. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8493. 0x63,0x63,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  8494. 0xAE,0x28,0x87,0x19,0x4F,0x4B,0x68,0x61,
  8495. 0x93,0x9A,0xC7,0x0E,0x33,0xB8,0xCE,0x96,
  8496. 0x66,0x0D,0xC7,0xB1,0xFA,0x94,0x80,0xA2,
  8497. 0x28,0x9B,0xCF,0xE2,0x08,0xB5,0x25,0xAC,
  8498. 0xFB,0xB8,0x65,0x5E,0xD1,0xCC,0x31,0xDA,
  8499. 0x2E,0x49,0x3A,0xEE,0xAF,0x63,0x70,0x5E
  8500. };
  8501. static const byte lms_pub_L2_H10_W4[60] =
  8502. {
  8503. 0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x06,
  8504. 0x00,0x00,0x00,0x03,0xFB,0xB8,0x65,0x5E,
  8505. 0xD1,0xCC,0x31,0xDA,0x2E,0x49,0x3A,0xEE,
  8506. 0xAF,0x63,0x70,0x5E,0xA2,0xD5,0xB6,0x15,
  8507. 0x33,0x8C,0x9B,0xE9,0xE1,0x91,0x40,0x1A,
  8508. 0x12,0xE0,0xD7,0xBD,0xE4,0xE0,0x76,0xF5,
  8509. 0x04,0x90,0x76,0xA5,0x9A,0xA7,0x4E,0xFE,
  8510. 0x6B,0x9A,0xD3,0x14
  8511. };
  8512. /* WC_LMS_PARM_L3_H5_W4
  8513. * signature length: 7160 */
  8514. static const byte lms_priv_L3_H5_W4[64] =
  8515. {
  8516. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8517. 0x53,0x53,0x53,0xFF,0xFF,0xFF,0xFF,0xFF,
  8518. 0x38,0xD1,0xBE,0x68,0xD1,0x93,0xE1,0x14,
  8519. 0x6C,0x8B,0xED,0xE2,0x25,0x88,0xED,0xAC,
  8520. 0x57,0xBD,0x87,0x9F,0x54,0xF3,0x58,0xD9,
  8521. 0x4D,0xF5,0x6A,0xBD,0x71,0x99,0x6A,0x28,
  8522. 0x2F,0xE1,0xFC,0xD1,0xD1,0x0C,0x7C,0xF8,
  8523. 0xB4,0xDC,0xDF,0x7F,0x14,0x1A,0x7B,0x50
  8524. };
  8525. static const byte lms_pub_L3_H5_W4[60] =
  8526. {
  8527. 0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x05,
  8528. 0x00,0x00,0x00,0x03,0x2F,0xE1,0xFC,0xD1,
  8529. 0xD1,0x0C,0x7C,0xF8,0xB4,0xDC,0xDF,0x7F,
  8530. 0x14,0x1A,0x7B,0x50,0x8E,0x3A,0xD4,0x05,
  8531. 0x0C,0x95,0x59,0xA0,0xCA,0x7A,0xD8,0xD6,
  8532. 0x5D,0xBD,0x42,0xBB,0xD5,0x82,0xB8,0x9C,
  8533. 0x52,0x37,0xB7,0x45,0x03,0xC2,0x06,0xCE,
  8534. 0xAB,0x4B,0x51,0x39
  8535. };
  8536. /* WC_LMS_PARM_L3_H5_W8
  8537. * signature length: 3992 */
  8538. static const byte lms_priv_L3_H5_W8[64] =
  8539. {
  8540. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8541. 0x54,0x54,0x54,0xFF,0xFF,0xFF,0xFF,0xFF,
  8542. 0xA5,0x46,0x97,0x0C,0xA1,0x3C,0xEA,0x17,
  8543. 0x5C,0x9D,0x59,0xF4,0x0E,0x27,0x37,0xF3,
  8544. 0x6A,0x1C,0xF7,0x29,0x4A,0xCC,0xCD,0x7B,
  8545. 0x4F,0xE7,0x37,0x6E,0xEF,0xC1,0xBD,0xBD,
  8546. 0x04,0x5D,0x8E,0xDD,0xAA,0x47,0xCC,0xE6,
  8547. 0xCE,0x78,0x46,0x20,0x41,0x87,0xE0,0x85
  8548. };
  8549. static const byte lms_pub_L3_H5_W8[60] =
  8550. {
  8551. 0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x05,
  8552. 0x00,0x00,0x00,0x04,0x04,0x5D,0x8E,0xDD,
  8553. 0xAA,0x47,0xCC,0xE6,0xCE,0x78,0x46,0x20,
  8554. 0x41,0x87,0xE0,0x85,0x0D,0x2C,0x46,0xB9,
  8555. 0x39,0x8C,0xA3,0x92,0x4F,0xCE,0x50,0x96,
  8556. 0x90,0x9C,0xF3,0x36,0x2E,0x09,0x15,0x3B,
  8557. 0x4B,0x34,0x17,0xE7,0xE2,0x55,0xFC,0x5B,
  8558. 0x83,0xAB,0x43,0xAF
  8559. };
  8560. /* WC_LMS_PARM_L3_H10_W4
  8561. * signature length: 7640 */
  8562. static const byte lms_priv_L3_H10_W4[64] =
  8563. {
  8564. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8565. 0x63,0x63,0x63,0xFF,0xFF,0xFF,0xFF,0xFF,
  8566. 0xDF,0x98,0xAB,0xEC,0xFE,0x13,0x9F,0xF8,
  8567. 0xD7,0x2B,0x4F,0x4C,0x79,0x34,0xB8,0x89,
  8568. 0x24,0x6B,0x26,0x7D,0x7A,0x2E,0xA2,0xCB,
  8569. 0x82,0x75,0x4E,0x96,0x54,0x49,0xED,0xA0,
  8570. 0xAF,0xC7,0xA5,0xEE,0x8A,0xA2,0x83,0x99,
  8571. 0x4B,0x18,0x59,0x2B,0x66,0xC0,0x32,0xDB
  8572. };
  8573. static const byte lms_pub_L3_H10_W4[60] =
  8574. {
  8575. 0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x06,
  8576. 0x00,0x00,0x00,0x03,0xAF,0xC7,0xA5,0xEE,
  8577. 0x8A,0xA2,0x83,0x99,0x4B,0x18,0x59,0x2B,
  8578. 0x66,0xC0,0x32,0xDB,0xC4,0x18,0xEB,0x11,
  8579. 0x17,0x7D,0xAA,0x93,0xFD,0xA0,0x70,0x4D,
  8580. 0x68,0x4B,0x63,0x8F,0xC2,0xE7,0xCA,0x34,
  8581. 0x14,0x31,0x0D,0xAA,0x18,0xBF,0x9B,0x32,
  8582. 0x8D,0x78,0xD5,0xA8
  8583. };
  8584. /* WC_LMS_PARM_L4_H5_W8
  8585. * signature length: 5340 */
  8586. static const byte lms_priv_L4_H5_W8[64] =
  8587. {
  8588. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8589. 0x54,0x54,0x54,0x54,0xFF,0xFF,0xFF,0xFF,
  8590. 0x46,0x8F,0x2A,0x4A,0x14,0x26,0xF0,0x89,
  8591. 0xFE,0xED,0x66,0x0F,0x73,0x69,0xB1,0x4C,
  8592. 0x47,0xA1,0x35,0x9F,0x7B,0xBA,0x08,0x03,
  8593. 0xEE,0xA2,0xEB,0xAD,0xB4,0x82,0x52,0x1F,
  8594. 0xFD,0x9B,0x22,0x82,0x42,0x1A,0x96,0x1E,
  8595. 0xE4,0xA1,0x9C,0x33,0xED,0xE6,0x9F,0xAB
  8596. };
  8597. static const byte lms_pub_L4_H5_W8[60] =
  8598. {
  8599. 0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x05,
  8600. 0x00,0x00,0x00,0x04,0xFD,0x9B,0x22,0x82,
  8601. 0x42,0x1A,0x96,0x1E,0xE4,0xA1,0x9C,0x33,
  8602. 0xED,0xE6,0x9F,0xAB,0x6B,0x47,0x05,0x5B,
  8603. 0xA7,0xAD,0xF6,0x88,0xA5,0x4F,0xCD,0xF1,
  8604. 0xDA,0x29,0x67,0xC3,0x7F,0x2C,0x11,0xFE,
  8605. 0x85,0x1A,0x7A,0xD8,0xD5,0x46,0x74,0x3B,
  8606. 0x74,0x24,0x12,0xC8
  8607. };
  8608. static int lms_write_key_mem(const byte* priv, word32 privSz, void* context)
  8609. {
  8610. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  8611. * BE USED FOR TESTING PURPOSES! Production applications should
  8612. * write only to non-volatile storage. */
  8613. XMEMCPY(context, priv, privSz);
  8614. return WC_LMS_RC_SAVED_TO_NV_MEMORY;
  8615. }
  8616. static int lms_read_key_mem(byte* priv, word32 privSz, void* context)
  8617. {
  8618. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  8619. * BE USED FOR TESTING PURPOSES! */
  8620. XMEMCPY(priv, context, privSz);
  8621. return WC_LMS_RC_READ_TO_MEMORY;
  8622. }
  8623. static byte lms_priv[HSS_MAX_PRIVATE_KEY_LEN];
  8624. static void bench_lms_keygen(int parm, byte* pub)
  8625. {
  8626. WC_RNG rng;
  8627. LmsKey key;
  8628. int ret;
  8629. word32 pubLen = HSS_MAX_PUBLIC_KEY_LEN;
  8630. int times = 0;
  8631. int count = 0;
  8632. double start = 0.0F;
  8633. int levels;
  8634. int height;
  8635. int winternitz;
  8636. const char* str = wc_LmsKey_ParmToStr(parm);
  8637. DECLARE_MULTI_VALUE_STATS_VARS()
  8638. #ifndef HAVE_FIPS
  8639. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  8640. #else
  8641. ret = wc_InitRng(&rng);
  8642. #endif
  8643. if (ret != 0) {
  8644. fprintf(stderr, "error: wc_InitRng failed: %d\n", ret);
  8645. return;
  8646. }
  8647. ret = wc_LmsKey_Init(&key, NULL, INVALID_DEVID);
  8648. if (ret) {
  8649. printf("wc_LmsKey_Init failed: %d\n", ret);
  8650. wc_FreeRng(&rng);
  8651. return;
  8652. }
  8653. count = 0;
  8654. bench_stats_start(&count, &start);
  8655. do {
  8656. /* LMS is stateful. Async queuing not practical. */
  8657. for (times = 0; times < 1; ++times) {
  8658. wc_LmsKey_Free(&key);
  8659. ret = wc_LmsKey_Init(&key, NULL, INVALID_DEVID);
  8660. if (ret) {
  8661. printf("wc_LmsKey_Init failed: %d\n", ret);
  8662. goto exit_lms_keygen;
  8663. }
  8664. ret = wc_LmsKey_SetLmsParm(&key, parm);
  8665. if (ret) {
  8666. printf("wc_LmsKey_SetLmsParm failed: %d\n", ret);
  8667. goto exit_lms_keygen;
  8668. }
  8669. ret = wc_LmsKey_GetParameters(&key, &levels, &height, &winternitz);
  8670. if (ret) {
  8671. fprintf(stderr, "error: wc_LmsKey_GetParameters failed: %d\n",
  8672. ret);
  8673. goto exit_lms_keygen;
  8674. }
  8675. ret = wc_LmsKey_SetWriteCb(&key, lms_write_key_mem);
  8676. if (ret) {
  8677. fprintf(stderr, "error: wc_LmsKey_SetWriteCb failed: %d\n",
  8678. ret);
  8679. goto exit_lms_keygen;
  8680. }
  8681. ret = wc_LmsKey_SetReadCb(&key, lms_read_key_mem);
  8682. if (ret) {
  8683. fprintf(stderr, "error: wc_LmsKey_SetReadCb failed: %d\n", ret);
  8684. goto exit_lms_keygen;
  8685. }
  8686. ret = wc_LmsKey_SetContext(&key, (void*)lms_priv);
  8687. if (ret) {
  8688. fprintf(stderr, "error: wc_LmsKey_SetContext failed: %d\n",
  8689. ret);
  8690. goto exit_lms_keygen;
  8691. }
  8692. ret = wc_LmsKey_MakeKey(&key, &rng);
  8693. if (ret) {
  8694. printf("wc_LmsKey_MakeKey failed: %d\n", ret);
  8695. goto exit_lms_keygen;
  8696. }
  8697. RECORD_MULTI_VALUE_STATS();
  8698. }
  8699. count += times;
  8700. } while (bench_stats_check(start)
  8701. #ifdef MULTI_VALUE_STATISTICS
  8702. || runs < minimum_runs
  8703. #endif
  8704. );
  8705. bench_stats_asym_finish(str, levels * height, "keygen", 0,
  8706. count, start, ret);
  8707. #ifdef MULTI_VALUE_STATISTICS
  8708. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8709. #endif
  8710. ret = wc_LmsKey_ExportPubRaw(&key, pub, &pubLen);
  8711. if (ret) {
  8712. fprintf(stderr, "error: wc_LmsKey_ExportPubRaw failed: %d\n", ret);
  8713. }
  8714. exit_lms_keygen:
  8715. wc_LmsKey_Free(&key);
  8716. wc_FreeRng(&rng);
  8717. }
  8718. static void bench_lms_sign_verify(int parm, byte* pub)
  8719. {
  8720. LmsKey key;
  8721. int ret = 0;
  8722. const char * msg = TEST_STRING;
  8723. word32 msgSz = TEST_STRING_SZ;
  8724. byte * sig = NULL;
  8725. word32 sigSz = 0;
  8726. word32 privLen = 0;
  8727. int loaded = 0;
  8728. int times = 0;
  8729. int count = 0;
  8730. double start = 0.0F;
  8731. const char * str = wc_LmsKey_ParmToStr(parm);
  8732. DECLARE_MULTI_VALUE_STATS_VARS()
  8733. ret = wc_LmsKey_Init(&key, NULL, INVALID_DEVID);
  8734. if (ret) {
  8735. printf("wc_LmsKey_Init failed: %d\n", ret);
  8736. goto exit_lms_sign_verify;
  8737. }
  8738. ret = wc_LmsKey_SetLmsParm(&key, parm);
  8739. if (ret) {
  8740. printf("wc_LmsKey_SetLmsParm failed: %d\n", ret);
  8741. goto exit_lms_sign_verify;
  8742. }
  8743. switch (parm) {
  8744. case WC_LMS_PARM_L2_H10_W2:
  8745. XMEMCPY(lms_priv, lms_priv_L2_H10_W2, sizeof(lms_priv_L2_H10_W2));
  8746. XMEMCPY(key.pub, lms_pub_L2_H10_W2, HSS_MAX_PUBLIC_KEY_LEN);
  8747. break;
  8748. case WC_LMS_PARM_L2_H10_W4:
  8749. XMEMCPY(lms_priv, lms_priv_L2_H10_W4, sizeof(lms_priv_L2_H10_W4));
  8750. XMEMCPY(key.pub, lms_pub_L2_H10_W4, HSS_MAX_PUBLIC_KEY_LEN);
  8751. break;
  8752. case WC_LMS_PARM_L3_H5_W4:
  8753. XMEMCPY(lms_priv, lms_priv_L3_H5_W4, sizeof(lms_priv_L3_H5_W4));
  8754. XMEMCPY(key.pub, lms_pub_L3_H5_W4, HSS_MAX_PUBLIC_KEY_LEN);
  8755. break;
  8756. case WC_LMS_PARM_L3_H5_W8:
  8757. XMEMCPY(lms_priv, lms_priv_L3_H5_W8, sizeof(lms_priv_L3_H5_W8));
  8758. XMEMCPY(key.pub, lms_pub_L3_H5_W8, HSS_MAX_PUBLIC_KEY_LEN);
  8759. break;
  8760. case WC_LMS_PARM_L3_H10_W4:
  8761. XMEMCPY(lms_priv, lms_priv_L3_H10_W4, sizeof(lms_priv_L3_H10_W4));
  8762. XMEMCPY(key.pub, lms_pub_L3_H10_W4, HSS_MAX_PUBLIC_KEY_LEN);
  8763. break;
  8764. case WC_LMS_PARM_L4_H5_W8:
  8765. XMEMCPY(lms_priv, lms_priv_L4_H5_W8, sizeof(lms_priv_L4_H5_W8));
  8766. XMEMCPY(key.pub, lms_pub_L4_H5_W8, HSS_MAX_PUBLIC_KEY_LEN);
  8767. break;
  8768. case WC_LMS_PARM_NONE:
  8769. case WC_LMS_PARM_L1_H15_W2:
  8770. case WC_LMS_PARM_L1_H15_W4:
  8771. case WC_LMS_PARM_L2_H10_W8:
  8772. case WC_LMS_PARM_L3_H5_W2:
  8773. default:
  8774. XMEMCPY(key.pub, pub, HSS_MAX_PUBLIC_KEY_LEN);
  8775. break;
  8776. }
  8777. ret = wc_LmsKey_SetWriteCb(&key, lms_write_key_mem);
  8778. if (ret) {
  8779. fprintf(stderr, "error: wc_LmsKey_SetWriteCb failed: %d\n", ret);
  8780. goto exit_lms_sign_verify;
  8781. }
  8782. ret = wc_LmsKey_SetReadCb(&key, lms_read_key_mem);
  8783. if (ret) {
  8784. fprintf(stderr, "error: wc_LmsKey_SetReadCb failed: %d\n", ret);
  8785. goto exit_lms_sign_verify;
  8786. }
  8787. ret = wc_LmsKey_SetContext(&key, (void*)lms_priv);
  8788. if (ret) {
  8789. fprintf(stderr, "error: wc_LmsKey_SetContext failed: %d\n", ret);
  8790. goto exit_lms_sign_verify;
  8791. }
  8792. /* Even with saved priv/pub keys, we must still reload the private
  8793. * key before using it. Reloading the private key is the bottleneck
  8794. * for larger heights. Only print load time in debug builds. */
  8795. count = 0;
  8796. bench_stats_start(&count, &start);
  8797. #ifndef WOLFSSL_WC_LMS_SMALL
  8798. do {
  8799. #ifdef WOLFSSL_WC_LMS
  8800. key.priv.inited = 0;
  8801. key.state = WC_LMS_STATE_PARMSET;
  8802. #endif
  8803. ret = wc_LmsKey_Reload(&key);
  8804. if (ret) {
  8805. printf("wc_LmsKey_Reload failed: %d\n", ret);
  8806. goto exit_lms_sign_verify;
  8807. }
  8808. RECORD_MULTI_VALUE_STATS();
  8809. count++;
  8810. ret = wc_LmsKey_GetSigLen(&key, &sigSz);
  8811. if (ret) {
  8812. printf("wc_LmsKey_GetSigLen failed: %d\n", ret);
  8813. goto exit_lms_sign_verify;
  8814. }
  8815. ret = wc_LmsKey_GetPrivLen(&key, &privLen);
  8816. if (ret) {
  8817. printf("wc_LmsKey_GetPrivLen failed: %d\n", ret);
  8818. goto exit_lms_sign_verify;
  8819. }
  8820. #ifdef HAVE_LIBLMS
  8821. break;
  8822. #endif
  8823. } while (bench_stats_check(start)
  8824. #ifdef MULTI_VALUE_STATISTICS
  8825. || runs < minimum_runs
  8826. #endif
  8827. );
  8828. bench_stats_asym_finish(str, (int)privLen, "load", 0,
  8829. count, start, ret);
  8830. #ifdef MULTI_VALUE_STATISTICS
  8831. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8832. #endif
  8833. RESET_MULTI_VALUE_STATS_VARS();
  8834. #else
  8835. ret = wc_LmsKey_Reload(&key);
  8836. if (ret) {
  8837. printf("wc_LmsKey_Reload failed: %d\n", ret);
  8838. goto exit_lms_sign_verify;
  8839. }
  8840. ret = wc_LmsKey_GetSigLen(&key, &sigSz);
  8841. if (ret) {
  8842. printf("wc_LmsKey_GetSigLen failed: %d\n", ret);
  8843. goto exit_lms_sign_verify;
  8844. }
  8845. ret = wc_LmsKey_GetPrivLen(&key, &privLen);
  8846. if (ret) {
  8847. printf("wc_LmsKey_GetPrivLen failed: %d\n", ret);
  8848. goto exit_lms_sign_verify;
  8849. }
  8850. #endif
  8851. loaded = 1;
  8852. sig = XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8853. if (sig == NULL) {
  8854. printf("bench_lms_sign_verify malloc failed\n");
  8855. goto exit_lms_sign_verify;
  8856. }
  8857. count = 0;
  8858. bench_stats_start(&count, &start);
  8859. do {
  8860. /* LMS is stateful. Async queuing not practical. */
  8861. #ifndef WOLFSSL_WC_LMS_SMALL
  8862. for (times = 0; times < ntimes; ++times)
  8863. #else
  8864. for (times = 0; times < 1; ++times)
  8865. #endif
  8866. {
  8867. ret = wc_LmsKey_Sign(&key, sig, &sigSz, (byte *) msg, msgSz);
  8868. if (ret) {
  8869. printf("wc_LmsKey_Sign failed: %d\n", ret);
  8870. goto exit_lms_sign_verify;
  8871. }
  8872. RECORD_MULTI_VALUE_STATS();
  8873. if (!wc_LmsKey_SigsLeft(&key)) {
  8874. break;
  8875. }
  8876. }
  8877. count += times;
  8878. } while (wc_LmsKey_SigsLeft(&key) && (bench_stats_check(start)
  8879. #ifdef MULTI_VALUE_STATISTICS
  8880. || runs < minimum_runs
  8881. #endif
  8882. ));
  8883. bench_stats_asym_finish(str, (int)sigSz, "sign", 0,
  8884. count, start, ret);
  8885. #ifdef MULTI_VALUE_STATISTICS
  8886. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8887. #endif
  8888. RESET_MULTI_VALUE_STATS_VARS();
  8889. count = 0;
  8890. bench_stats_start(&count, &start);
  8891. do {
  8892. /* LMS is stateful. Async queuing not practical. */
  8893. for (times = 0; times < ntimes; ++times) {
  8894. ret = wc_LmsKey_Verify(&key, sig, sigSz, (byte *) msg, msgSz);
  8895. if (ret) {
  8896. printf("wc_LmsKey_Verify failed: %d\n", ret);
  8897. goto exit_lms_sign_verify;
  8898. }
  8899. RECORD_MULTI_VALUE_STATS();
  8900. }
  8901. count += times;
  8902. } while (bench_stats_check(start)
  8903. #ifdef MULTI_VALUE_STATISTICS
  8904. || runs < minimum_runs
  8905. #endif
  8906. );
  8907. exit_lms_sign_verify:
  8908. bench_stats_asym_finish(str, (int)sigSz, "verify", 0,
  8909. count, start, ret);
  8910. #ifdef MULTI_VALUE_STATISTICS
  8911. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8912. #endif
  8913. if (loaded) {
  8914. wc_LmsKey_Free(&key);
  8915. }
  8916. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8917. return;
  8918. }
  8919. void bench_lms(void)
  8920. {
  8921. byte pub[HSS_MAX_PUBLIC_KEY_LEN];
  8922. #ifdef BENCH_LMS_SLOW_KEYGEN
  8923. #if !defined(WOLFSSL_WC_LMS) || (LMS_MAX_HEIGHT >= 15)
  8924. bench_lms_keygen(WC_LMS_PARM_L1_H15_W2, pub);
  8925. bench_lms_sign_verify(WC_LMS_PARM_L1_H15_W2, pub);
  8926. bench_lms_keygen(WC_LMS_PARM_L1_H15_W4, pub);
  8927. bench_lms_sign_verify(WC_LMS_PARM_L1_H15_W4, pub);
  8928. #undef LMS_PARAMS_BENCHED
  8929. #define LMS_PARAMS_BENCHED
  8930. #endif
  8931. #endif
  8932. #if !defined(WOLFSSL_WC_LMS) || ((LMS_MAX_LEVELS >= 2) && \
  8933. (LMS_MAX_HEIGHT >= 10))
  8934. bench_lms_keygen(WC_LMS_PARM_L2_H10_W2, pub);
  8935. bench_lms_sign_verify(WC_LMS_PARM_L2_H10_W2, pub);
  8936. bench_lms_keygen(WC_LMS_PARM_L2_H10_W4, pub);
  8937. bench_lms_sign_verify(WC_LMS_PARM_L2_H10_W4, pub);
  8938. #undef LMS_PARAMS_BENCHED
  8939. #define LMS_PARAMS_BENCHED
  8940. #ifdef BENCH_LMS_SLOW_KEYGEN
  8941. bench_lms_keygen(WC_LMS_PARM_L2_H10_W8, pub);
  8942. bench_lms_sign_verify(WC_LMS_PARM_L2_H10_W8, pub);
  8943. #endif
  8944. #endif
  8945. #if !defined(WOLFSSL_WC_LMS) || (LMS_MAX_LEVELS >= 3)
  8946. bench_lms_keygen(WC_LMS_PARM_L3_H5_W4, pub);
  8947. bench_lms_sign_verify(WC_LMS_PARM_L3_H5_W4, pub);
  8948. bench_lms_keygen(WC_LMS_PARM_L3_H5_W8, pub);
  8949. bench_lms_sign_verify(WC_LMS_PARM_L3_H5_W8, pub);
  8950. #undef LMS_PARAMS_BENCHED
  8951. #define LMS_PARAMS_BENCHED
  8952. #endif
  8953. #if !defined(WOLFSSL_WC_LMS) || ((LMS_MAX_LEVELS >= 3) && \
  8954. (LMS_MAX_HEIGHT >= 10))
  8955. bench_lms_keygen(WC_LMS_PARM_L3_H10_W4, pub);
  8956. bench_lms_sign_verify(WC_LMS_PARM_L3_H10_W4, pub);
  8957. #endif
  8958. #if !defined(WOLFSSL_WC_LMS) || (LMS_MAX_LEVELS >= 4)
  8959. bench_lms_keygen(WC_LMS_PARM_L4_H5_W8, pub);
  8960. bench_lms_sign_verify(WC_LMS_PARM_L4_H5_W8, pub);
  8961. #endif
  8962. #if defined(WOLFSSL_WC_LMS) && !defined(LMS_PARAMS_BENCHED)
  8963. bench_lms_keygen(WC_LMS_PARM_L1_H5_W1, pub);
  8964. bench_lms_sign_verify(WC_LMS_PARM_L1_H5_W1, pub);
  8965. #endif
  8966. return;
  8967. }
  8968. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY) */
  8969. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  8970. static enum wc_XmssRc xmss_write_key_mem(const byte * priv, word32 privSz,
  8971. void *context)
  8972. {
  8973. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  8974. * BE USED FOR TESTING PURPOSES! Production applications should
  8975. * write only to non-volatile storage. */
  8976. XMEMCPY(context, priv, privSz);
  8977. return WC_XMSS_RC_SAVED_TO_NV_MEMORY;
  8978. }
  8979. static enum wc_XmssRc xmss_read_key_mem(byte * priv, word32 privSz,
  8980. void *context)
  8981. {
  8982. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  8983. * BE USED FOR TESTING PURPOSES! */
  8984. XMEMCPY(priv, context, privSz);
  8985. return WC_XMSS_RC_READ_TO_MEMORY;
  8986. }
  8987. static void bench_xmss_sign_verify(const char * params)
  8988. {
  8989. WC_RNG rng;
  8990. XmssKey key;
  8991. word32 pkSz = 0;
  8992. word32 skSz = 0;
  8993. int freeRng = 0;
  8994. int freeKey = 0;
  8995. unsigned char * sk = NULL;
  8996. const char * msg = "XMSS post quantum signature test";
  8997. word32 msgSz = (word32) XSTRLEN(msg);
  8998. int ret = 0;
  8999. byte * sig = NULL;
  9000. word32 sigSz = 0;
  9001. int times = 0;
  9002. int count = 0;
  9003. double start = 0.0F;
  9004. #ifndef HAVE_FIPS
  9005. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  9006. #else
  9007. ret = wc_InitRng(&rng);
  9008. #endif
  9009. if (ret != 0) {
  9010. fprintf(stderr, "error: wc_InitRng failed: %d\n", ret);
  9011. goto exit_xmss_sign_verify;
  9012. }
  9013. freeRng = 1;
  9014. ret = wc_XmssKey_Init(&key, NULL, INVALID_DEVID);
  9015. if (ret != 0) {
  9016. fprintf(stderr, "wc_XmssKey_Init failed: %d\n", ret);
  9017. goto exit_xmss_sign_verify;
  9018. }
  9019. ret = wc_XmssKey_SetParamStr(&key, params);
  9020. if (ret != 0) {
  9021. fprintf(stderr, "wc_XmssKey_SetParamStr failed: %d\n", ret);
  9022. goto exit_xmss_sign_verify;
  9023. }
  9024. ret = wc_XmssKey_GetPubLen(&key, &pkSz);
  9025. if (ret != 0) {
  9026. fprintf(stderr, "wc_XmssKey_GetPubLen failed: %d\n", ret);
  9027. goto exit_xmss_sign_verify;
  9028. }
  9029. #ifndef WOLFSSL_WC_XMSS
  9030. if (pkSz != XMSS_SHA256_PUBLEN) {
  9031. fprintf(stderr, "error: xmss pub len: got %u, expected %d\n", pkSz,
  9032. XMSS_SHA256_PUBLEN);
  9033. goto exit_xmss_sign_verify;
  9034. }
  9035. #endif
  9036. ret = wc_XmssKey_GetPrivLen(&key, &skSz);
  9037. if (ret != 0 || skSz <= 0) {
  9038. fprintf(stderr, "error: wc_XmssKey_GetPrivLen failed\n");
  9039. goto exit_xmss_sign_verify;
  9040. }
  9041. ret = wc_XmssKey_GetSigLen(&key, &sigSz);
  9042. if (ret != 0 || sigSz <= 0) {
  9043. fprintf(stderr, "error: wc_XmssKey_GetSigLen failed\n");
  9044. goto exit_xmss_sign_verify;
  9045. }
  9046. /* Allocate secret keys.*/
  9047. sk = (unsigned char *)XMALLOC(skSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9048. if (sk == NULL) {
  9049. fprintf(stderr, "error: allocate xmss sk failed\n");
  9050. goto exit_xmss_sign_verify;
  9051. }
  9052. /* Allocate signature array. */
  9053. sig = (byte *)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9054. if (sig == NULL) {
  9055. fprintf(stderr, "error: allocate xmss sig failed\n");
  9056. goto exit_xmss_sign_verify;
  9057. }
  9058. ret = wc_XmssKey_SetWriteCb(&key, xmss_write_key_mem);
  9059. if (ret != 0) {
  9060. fprintf(stderr, "error: wc_XmssKey_SetWriteCb failed: %d\n", ret);
  9061. goto exit_xmss_sign_verify;
  9062. }
  9063. ret = wc_XmssKey_SetReadCb(&key, xmss_read_key_mem);
  9064. if (ret != 0) {
  9065. fprintf(stderr, "error: wc_XmssKey_SetReadCb failed: %d\n", ret);
  9066. goto exit_xmss_sign_verify;
  9067. }
  9068. ret = wc_XmssKey_SetContext(&key, (void *)sk);
  9069. if (ret != 0) {
  9070. fprintf(stderr, "error: wc_XmssKey_SetContext failed: %d\n", ret);
  9071. goto exit_xmss_sign_verify;
  9072. }
  9073. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  9074. fprintf(stderr, "params: %s\n", params);
  9075. fprintf(stderr, "pkSz: %d\n", pkSz);
  9076. fprintf(stderr, "skSz: %d\n", skSz);
  9077. fprintf(stderr, "sigSz: %d\n", sigSz);
  9078. #endif
  9079. /* Making the private key is the bottleneck for larger heights. */
  9080. count = 0;
  9081. bench_stats_start(&count, &start);
  9082. ret = wc_XmssKey_MakeKey(&key, &rng);
  9083. if (ret != 0) {
  9084. printf("wc_XmssKey_MakeKey failed: %d\n", ret);
  9085. goto exit_xmss_sign_verify;
  9086. }
  9087. /* Can only do one at a time - state changes after make key. */
  9088. count +=1;
  9089. bench_stats_check(start);
  9090. bench_stats_asym_finish(params, (int)skSz, "gen", 0, count, start, ret);
  9091. freeKey = 1;
  9092. count = 0;
  9093. bench_stats_start(&count, &start);
  9094. do {
  9095. /* XMSS is stateful. Async queuing not practical. */
  9096. #ifndef WOLFSSL_WC_XMSS_SMALL
  9097. for (times = 0; times < ntimes; ++times)
  9098. #else
  9099. for (times = 0; times < 1; ++times)
  9100. #endif
  9101. {
  9102. if (!wc_XmssKey_SigsLeft(&key))
  9103. break;
  9104. ret = wc_XmssKey_Sign(&key, sig, &sigSz, (byte *) msg, msgSz);
  9105. if (ret) {
  9106. printf("wc_XmssKey_Sign failed: %d\n", ret);
  9107. goto exit_xmss_sign_verify;
  9108. }
  9109. }
  9110. count += times;
  9111. } while (wc_XmssKey_SigsLeft(&key) && bench_stats_check(start));
  9112. bench_stats_asym_finish(params, (int)sigSz, "sign", 0, count, start, ret);
  9113. count = 0;
  9114. bench_stats_start(&count, &start);
  9115. do {
  9116. /* XMSS is stateful. Async queuing not practical. */
  9117. for (times = 0; times < ntimes; ++times) {
  9118. ret = wc_XmssKey_Verify(&key, sig, sigSz, (byte *) msg, msgSz);
  9119. if (ret) {
  9120. printf("wc_XmssKey_Verify failed: %d\n", ret);
  9121. goto exit_xmss_sign_verify;
  9122. }
  9123. }
  9124. count += times;
  9125. } while (bench_stats_check(start));
  9126. exit_xmss_sign_verify:
  9127. bench_stats_asym_finish(params, (int)sigSz, "verify", 0, count, start, ret);
  9128. /* Cleanup everything. */
  9129. if (sig != NULL) {
  9130. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9131. sig = NULL;
  9132. }
  9133. if (sk != NULL) {
  9134. XFREE(sk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9135. sk = NULL;
  9136. }
  9137. if (freeRng) {
  9138. wc_FreeRng(&rng);
  9139. }
  9140. if (freeKey) {
  9141. wc_XmssKey_Free(&key);
  9142. }
  9143. return;
  9144. }
  9145. void bench_xmss(int hash)
  9146. {
  9147. /* All NIST SP 800-208 approved SHA256 XMSS/XMSS^MT parameter
  9148. * sets.
  9149. *
  9150. * Note: not testing "XMSS-SHA2_16_256", "XMSS-SHA2_20_256",
  9151. * and "XMSSMT-SHA2_60/3_256", because their keygen can be
  9152. * very slow, their signatures and private keys quite large,
  9153. * and xmss private keys are not portable across different
  9154. * XMSS/XMSS^MT implementations.
  9155. *
  9156. * The bottleneck in key generation is the height of the first
  9157. * level tree (or h/d).
  9158. *
  9159. * h is the total height of the hyper tree, and d the number of
  9160. * trees.
  9161. */
  9162. /* h/d h d */
  9163. #ifdef WC_XMSS_SHA256
  9164. if (hash == WC_HASH_TYPE_SHA256) {
  9165. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  9166. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9167. bench_xmss_sign_verify("XMSS-SHA2_10_256"); /* 10 10 1 */
  9168. #endif
  9169. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9170. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9171. bench_xmss_sign_verify("XMSS-SHA2_16_256"); /* 16 16 1 */
  9172. #endif
  9173. #endif
  9174. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9175. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9176. bench_xmss_sign_verify("XMSS-SHA2_20_256"); /* 20 20 1 */
  9177. #endif
  9178. #endif
  9179. #endif /* HASH_SIZE 256 */
  9180. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 192 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 192
  9181. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9182. bench_xmss_sign_verify("XMSS-SHA2_10_192"); /* 10 10 1 */
  9183. #endif
  9184. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9185. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9186. bench_xmss_sign_verify("XMSS-SHA2_16_192"); /* 16 16 1 */
  9187. #endif
  9188. #endif
  9189. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9190. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9191. bench_xmss_sign_verify("XMSS-SHA2_20_192"); /* 20 20 1 */
  9192. #endif
  9193. #endif
  9194. #endif /* HASH_SIZE 192 */
  9195. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  9196. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9197. bench_xmss_sign_verify("XMSSMT-SHA2_20/2_256"); /* 10 20 2 */
  9198. bench_xmss_sign_verify("XMSSMT-SHA2_20/4_256"); /* 5 20 4 */
  9199. #endif
  9200. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9201. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9202. bench_xmss_sign_verify("XMSSMT-SHA2_40/2_256"); /* 20 40 4 */
  9203. #endif
  9204. bench_xmss_sign_verify("XMSSMT-SHA2_40/4_256"); /* 10 40 4 */
  9205. bench_xmss_sign_verify("XMSSMT-SHA2_40/8_256"); /* 5 40 8 */
  9206. #endif
  9207. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9208. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9209. bench_xmss_sign_verify("XMSSMT-SHA2_60/3_256"); /* 20 60 3 */
  9210. #endif
  9211. bench_xmss_sign_verify("XMSSMT-SHA2_60/6_256"); /* 10 60 6 */
  9212. bench_xmss_sign_verify("XMSSMT-SHA2_60/12_256"); /* 5 60 12 */
  9213. #endif
  9214. #endif /* HASH_SIZE 256 */
  9215. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 192 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 192
  9216. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9217. bench_xmss_sign_verify("XMSSMT-SHA2_20/2_192"); /* 10 20 2 */
  9218. bench_xmss_sign_verify("XMSSMT-SHA2_20/4_192"); /* 5 20 4 */
  9219. #endif
  9220. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9221. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9222. bench_xmss_sign_verify("XMSSMT-SHA2_40/2_192"); /* 20 40 4 */
  9223. #endif
  9224. bench_xmss_sign_verify("XMSSMT-SHA2_40/4_192"); /* 10 40 4 */
  9225. bench_xmss_sign_verify("XMSSMT-SHA2_40/8_192"); /* 5 40 8 */
  9226. #endif
  9227. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9228. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9229. bench_xmss_sign_verify("XMSSMT-SHA2_60/3_192"); /* 20 60 3 */
  9230. #endif
  9231. bench_xmss_sign_verify("XMSSMT-SHA2_60/6_192"); /* 10 60 6 */
  9232. bench_xmss_sign_verify("XMSSMT-SHA2_60/12_192"); /* 5 60 12 */
  9233. #endif
  9234. #endif /* HASH_SIZE 192 */
  9235. }
  9236. #endif
  9237. #ifdef WC_XMSS_SHA512
  9238. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 512 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 512
  9239. if (hash == WC_HASH_TYPE_SHA512) {
  9240. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9241. bench_xmss_sign_verify("XMSS-SHA2_10_512"); /* 10 10 1 */
  9242. #endif
  9243. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9244. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9245. bench_xmss_sign_verify("XMSS-SHA2_16_512"); /* 16 16 1 */
  9246. #endif
  9247. #endif
  9248. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9249. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9250. bench_xmss_sign_verify("XMSS-SHA2_20_512"); /* 20 20 1 */
  9251. #endif
  9252. #endif
  9253. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9254. bench_xmss_sign_verify("XMSSMT-SHA2_20/2_512"); /* 10 20 2 */
  9255. bench_xmss_sign_verify("XMSSMT-SHA2_20/4_512"); /* 5 20 4 */
  9256. #endif
  9257. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9258. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9259. bench_xmss_sign_verify("XMSSMT-SHA2_40/2_512"); /* 20 40 4 */
  9260. #endif
  9261. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9262. bench_xmss_sign_verify("XMSSMT-SHA2_40/4_512"); /* 10 40 4 */
  9263. #endif
  9264. bench_xmss_sign_verify("XMSSMT-SHA2_40/8_512"); /* 5 40 8 */
  9265. #endif
  9266. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9267. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9268. bench_xmss_sign_verify("XMSSMT-SHA2_60/3_512"); /* 20 60 3 */
  9269. #endif
  9270. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9271. bench_xmss_sign_verify("XMSSMT-SHA2_60/6_512"); /* 10 60 6 */
  9272. #endif
  9273. bench_xmss_sign_verify("XMSSMT-SHA2_60/12_512"); /* 5 60 12 */
  9274. #endif
  9275. }
  9276. #endif /* HASH_SIZE 512 */
  9277. #endif
  9278. #ifdef WC_XMSS_SHAKE128
  9279. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  9280. if (hash == WC_HASH_TYPE_SHAKE128) {
  9281. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9282. bench_xmss_sign_verify("XMSS-SHAKE_10_256"); /* 10 10 1 */
  9283. #endif
  9284. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9285. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9286. bench_xmss_sign_verify("XMSS-SHAKE_16_256"); /* 16 16 1 */
  9287. #endif
  9288. #endif
  9289. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9290. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9291. bench_xmss_sign_verify("XMSS-SHAKE_20_256"); /* 20 20 1 */
  9292. #endif
  9293. #endif
  9294. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9295. bench_xmss_sign_verify("XMSSMT-SHAKE_20/2_256"); /* 10 20 2 */
  9296. bench_xmss_sign_verify("XMSSMT-SHAKE_20/4_256"); /* 5 20 4 */
  9297. #endif
  9298. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9299. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9300. bench_xmss_sign_verify("XMSSMT-SHAKE_40/2_256"); /* 20 40 4 */
  9301. #endif
  9302. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9303. bench_xmss_sign_verify("XMSSMT-SHAKE_40/4_256"); /* 10 40 4 */
  9304. #endif
  9305. bench_xmss_sign_verify("XMSSMT-SHAKE_40/8_256"); /* 5 40 8 */
  9306. #endif
  9307. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9308. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9309. bench_xmss_sign_verify("XMSSMT-SHAKE_60/3_256"); /* 20 60 3 */
  9310. #endif
  9311. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9312. bench_xmss_sign_verify("XMSSMT-SHAKE_60/6_256"); /* 10 60 6 */
  9313. #endif
  9314. bench_xmss_sign_verify("XMSSMT-SHAKE_60/12_256"); /* 5 60 12 */
  9315. #endif
  9316. }
  9317. #endif /* HASH_SIZE 256 */
  9318. #endif
  9319. #ifdef WC_XMSS_SHAKE256
  9320. if (hash == WC_HASH_TYPE_SHAKE256) {
  9321. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 512 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 512
  9322. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9323. bench_xmss_sign_verify("XMSS-SHAKE_10_512"); /* 10 10 1 */
  9324. #endif
  9325. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9326. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9327. bench_xmss_sign_verify("XMSS-SHAKE_16_512"); /* 16 16 1 */
  9328. #endif
  9329. #endif
  9330. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9331. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9332. bench_xmss_sign_verify("XMSS-SHAKE_20_512"); /* 20 20 1 */
  9333. #endif
  9334. #endif
  9335. #endif /* HASH_SIZE 512 */
  9336. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  9337. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9338. bench_xmss_sign_verify("XMSS-SHAKE256_10_256"); /* 10 10 1 */
  9339. #endif
  9340. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9341. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9342. bench_xmss_sign_verify("XMSS-SHAKE256_16_256"); /* 16 16 1 */
  9343. #endif
  9344. #endif
  9345. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9346. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9347. bench_xmss_sign_verify("XMSS-SHAKE256_20_256"); /* 20 20 1 */
  9348. #endif
  9349. #endif
  9350. #endif /* HASH_SIZE 256 */
  9351. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 192 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 192
  9352. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9353. bench_xmss_sign_verify("XMSS-SHAKE256_10_192"); /* 10 10 1 */
  9354. #endif
  9355. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9356. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9357. bench_xmss_sign_verify("XMSS-SHAKE256_16_192"); /* 16 16 1 */
  9358. #endif
  9359. #endif
  9360. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9361. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9362. bench_xmss_sign_verify("XMSS-SHAKE256_20_192"); /* 20 20 1 */
  9363. #endif
  9364. #endif
  9365. #endif /* HASH_SIZE 192 */
  9366. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 512 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 512
  9367. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9368. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9369. bench_xmss_sign_verify("XMSSMT-SHAKE_20/2_512"); /* 10 20 2 */
  9370. #endif
  9371. bench_xmss_sign_verify("XMSSMT-SHAKE_20/4_512"); /* 5 20 4 */
  9372. #endif
  9373. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9374. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9375. bench_xmss_sign_verify("XMSSMT-SHAKE_40/2_512"); /* 20 40 4 */
  9376. #endif
  9377. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9378. bench_xmss_sign_verify("XMSSMT-SHAKE_40/4_512"); /* 10 40 4 */
  9379. #endif
  9380. bench_xmss_sign_verify("XMSSMT-SHAKE_40/8_512"); /* 5 40 8 */
  9381. #endif
  9382. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9383. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9384. bench_xmss_sign_verify("XMSSMT-SHAKE_60/3_512"); /* 20 60 3 */
  9385. #endif
  9386. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9387. bench_xmss_sign_verify("XMSSMT-SHAKE_60/6_512"); /* 10 60 6 */
  9388. #endif
  9389. bench_xmss_sign_verify("XMSSMT-SHAKE_60/12_512"); /* 5 60 12 */
  9390. #endif
  9391. #endif /* HASH_SIZE 512 */
  9392. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  9393. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9394. bench_xmss_sign_verify("XMSSMT-SHAKE256_20/2_256"); /* 10 20 2 */
  9395. bench_xmss_sign_verify("XMSSMT-SHAKE256_20/4_256"); /* 5 20 4 */
  9396. #endif
  9397. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9398. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9399. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/2_256"); /* 20 40 4 */
  9400. #endif
  9401. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9402. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/4_256"); /* 10 40 4 */
  9403. #endif
  9404. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/8_256"); /* 5 40 8 */
  9405. #endif
  9406. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9407. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9408. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/3_256"); /* 20 60 3 */
  9409. #endif
  9410. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9411. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/6_256"); /* 10 60 6 */
  9412. #endif
  9413. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/12_256");/* 5 60 12 */
  9414. #endif
  9415. #endif /* HASH_SIZE 256 */
  9416. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 192 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 192
  9417. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9418. bench_xmss_sign_verify("XMSSMT-SHAKE256_20/2_192"); /* 10 20 2 */
  9419. bench_xmss_sign_verify("XMSSMT-SHAKE256_20/4_192"); /* 5 20 4 */
  9420. #endif
  9421. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9422. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9423. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/2_192"); /* 20 40 4 */
  9424. #endif
  9425. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9426. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/4_192"); /* 10 40 4 */
  9427. #endif
  9428. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/8_192"); /* 5 40 8 */
  9429. #endif
  9430. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9431. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9432. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/3_192"); /* 20 60 3 */
  9433. #endif
  9434. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9435. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/6_192"); /* 10 60 6 */
  9436. #endif
  9437. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/12_192");/* 5 60 12 */
  9438. #endif
  9439. #endif /* HASH_SIZE 192 */
  9440. }
  9441. #endif
  9442. return;
  9443. }
  9444. #endif /* if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY) */
  9445. #ifdef HAVE_ECC
  9446. /* Maximum ECC name plus null terminator:
  9447. * "ECC [%15s]" and "ECDHE [%15s]" and "ECDSA [%15s]" */
  9448. #define BENCH_ECC_NAME_SZ (ECC_MAXNAME + 8)
  9449. /* run all benchmarks on a curve */
  9450. void bench_ecc_curve(int curveId)
  9451. {
  9452. if (bench_all || (bench_asym_algs & BENCH_ECC_MAKEKEY)) {
  9453. #ifndef NO_SW_BENCH
  9454. bench_eccMakeKey(0, curveId);
  9455. #endif
  9456. #if defined(BENCH_DEVID)
  9457. bench_eccMakeKey(1, curveId);
  9458. #endif
  9459. }
  9460. if (bench_all || (bench_asym_algs & BENCH_ECC)) {
  9461. #ifndef NO_SW_BENCH
  9462. bench_ecc(0, curveId);
  9463. #endif
  9464. #if defined(BENCH_DEVID)
  9465. bench_ecc(1, curveId);
  9466. #endif
  9467. }
  9468. #ifdef HAVE_ECC_ENCRYPT
  9469. if (bench_all || (bench_asym_algs & BENCH_ECC_ENCRYPT))
  9470. bench_eccEncrypt(curveId);
  9471. #endif
  9472. }
  9473. void bench_eccMakeKey(int useDeviceID, int curveId)
  9474. {
  9475. int ret = 0, i, times, count = 0, pending = 0;
  9476. int deviceID;
  9477. int keySize = 0;
  9478. WC_DECLARE_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9479. sizeof(ecc_key), HEAP_HINT);
  9480. char name[BENCH_ECC_NAME_SZ];
  9481. double start = 0;
  9482. const char**desc = bench_desc_words[lng_index];
  9483. DECLARE_MULTI_VALUE_STATS_VARS()
  9484. WC_CALLOC_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9485. sizeof(ecc_key), HEAP_HINT);
  9486. deviceID = useDeviceID ? devId : INVALID_DEVID;
  9487. keySize = wc_ecc_get_curve_size_from_id(curveId);
  9488. /* ECC Make Key */
  9489. bench_stats_start(&count, &start);
  9490. do {
  9491. /* while free pending slots in queue, submit ops */
  9492. for (times = 0; times < agreeTimes || pending > 0; ) {
  9493. bench_async_poll(&pending);
  9494. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9495. if (bench_async_check(&ret,
  9496. BENCH_ASYNC_GET_DEV(genKey[i]), 0,
  9497. &times, agreeTimes, &pending)) {
  9498. wc_ecc_free(genKey[i]);
  9499. ret = wc_ecc_init_ex(genKey[i], HEAP_HINT, deviceID);
  9500. if (ret < 0) {
  9501. goto exit;
  9502. }
  9503. ret = wc_ecc_make_key_ex(&gRng, keySize, genKey[i],
  9504. curveId);
  9505. if (!bench_async_handle(&ret,
  9506. BENCH_ASYNC_GET_DEV(genKey[i]), 0, &times,
  9507. &pending)) {
  9508. goto exit;
  9509. }
  9510. }
  9511. } /* for i */
  9512. RECORD_MULTI_VALUE_STATS();
  9513. } /* for times */
  9514. count += times;
  9515. } while (bench_stats_check(start)
  9516. #ifdef MULTI_VALUE_STATISTICS
  9517. || runs < minimum_runs
  9518. #endif
  9519. );
  9520. exit:
  9521. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECC [%15s]",
  9522. wc_ecc_get_name(curveId));
  9523. bench_stats_asym_finish(name, keySize * 8, desc[2],
  9524. useDeviceID, count, start, ret);
  9525. #ifdef MULTI_VALUE_STATISTICS
  9526. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9527. #endif
  9528. /* cleanup */
  9529. if (WC_ARRAY_OK(genKey)) {
  9530. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9531. wc_ecc_free(genKey[i]);
  9532. }
  9533. WC_FREE_ARRAY(genKey, BENCH_MAX_PENDING, HEAP_HINT);
  9534. }
  9535. }
  9536. void bench_ecc(int useDeviceID, int curveId)
  9537. {
  9538. int ret = 0, i, times, count, pending = 0;
  9539. int deviceID;
  9540. int keySize;
  9541. char name[BENCH_ECC_NAME_SZ];
  9542. WC_DECLARE_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9543. sizeof(ecc_key), HEAP_HINT);
  9544. #ifdef HAVE_ECC_DHE
  9545. WC_DECLARE_ARRAY(genKey2, ecc_key, BENCH_MAX_PENDING,
  9546. sizeof(ecc_key), HEAP_HINT);
  9547. #endif
  9548. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9549. #ifdef HAVE_ECC_VERIFY
  9550. int verify[BENCH_MAX_PENDING];
  9551. #endif
  9552. #endif
  9553. word32 x[BENCH_MAX_PENDING];
  9554. double start = 0;
  9555. const char**desc = bench_desc_words[lng_index];
  9556. DECLARE_MULTI_VALUE_STATS_VARS()
  9557. #ifdef HAVE_ECC_DHE
  9558. WC_DECLARE_ARRAY(shared, byte,
  9559. BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9560. #endif
  9561. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9562. WC_DECLARE_ARRAY(sig, byte,
  9563. BENCH_MAX_PENDING, ECC_MAX_SIG_SIZE, HEAP_HINT);
  9564. WC_DECLARE_ARRAY(digest, byte,
  9565. BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9566. #endif
  9567. /* old scan-build misfires -Wmaybe-uninitialized on these. */
  9568. XMEMSET(sig, 0, sizeof(sig));
  9569. XMEMSET(digest, 0, sizeof(digest));
  9570. XMEMSET(shared, 0, sizeof(shared));
  9571. WC_CALLOC_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9572. sizeof(ecc_key), HEAP_HINT);
  9573. #ifdef HAVE_ECC_DHE
  9574. WC_CALLOC_ARRAY(genKey2, ecc_key, BENCH_MAX_PENDING,
  9575. sizeof(ecc_key), HEAP_HINT);
  9576. WC_ALLOC_ARRAY(shared, byte,
  9577. BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9578. #endif
  9579. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9580. WC_ALLOC_ARRAY(sig, byte, BENCH_MAX_PENDING, ECC_MAX_SIG_SIZE, HEAP_HINT);
  9581. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9582. #endif
  9583. deviceID = useDeviceID ? devId : INVALID_DEVID;
  9584. keySize = wc_ecc_get_curve_size_from_id(curveId);
  9585. /* init keys */
  9586. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9587. /* setup an context for each key */
  9588. if ((ret = wc_ecc_init_ex(genKey[i], HEAP_HINT, deviceID)) < 0) {
  9589. goto exit;
  9590. }
  9591. ret = wc_ecc_make_key_ex(&gRng, keySize, genKey[i], curveId);
  9592. #ifdef WOLFSSL_ASYNC_CRYPT
  9593. ret = wc_AsyncWait(ret, &genKey[i]->asyncDev, WC_ASYNC_FLAG_NONE);
  9594. #endif
  9595. if (ret < 0) {
  9596. goto exit;
  9597. }
  9598. #ifdef HAVE_ECC_DHE
  9599. if ((ret = wc_ecc_init_ex(genKey2[i], HEAP_HINT, deviceID)) < 0) {
  9600. goto exit;
  9601. }
  9602. if ((ret = wc_ecc_make_key_ex(&gRng, keySize, genKey2[i],
  9603. curveId)) > 0) {
  9604. goto exit;
  9605. }
  9606. #endif
  9607. }
  9608. #ifdef HAVE_ECC_DHE
  9609. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  9610. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  9611. !defined(HAVE_SELFTEST)
  9612. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9613. (void)wc_ecc_set_rng(genKey[i], &gRng);
  9614. }
  9615. #endif
  9616. /* ECC Shared Secret */
  9617. bench_stats_start(&count, &start);
  9618. PRIVATE_KEY_UNLOCK();
  9619. do {
  9620. for (times = 0; times < agreeTimes || pending > 0; ) {
  9621. bench_async_poll(&pending);
  9622. /* while free pending slots in queue, submit ops */
  9623. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9624. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  9625. &times, agreeTimes, &pending)) {
  9626. x[i] = (word32)keySize;
  9627. ret = wc_ecc_shared_secret(genKey[i], genKey2[i],
  9628. shared[i], &x[i]);
  9629. if (!bench_async_handle(&ret,
  9630. BENCH_ASYNC_GET_DEV(genKey[i]), 1, &times,
  9631. &pending)) {
  9632. goto exit_ecdhe;
  9633. }
  9634. }
  9635. } /* for i */
  9636. RECORD_MULTI_VALUE_STATS();
  9637. } /* for times */
  9638. count += times;
  9639. } while (bench_stats_check(start)
  9640. #ifdef MULTI_VALUE_STATISTICS
  9641. || runs < minimum_runs
  9642. #endif
  9643. );
  9644. PRIVATE_KEY_UNLOCK();
  9645. exit_ecdhe:
  9646. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDHE [%15s]",
  9647. wc_ecc_get_name(curveId));
  9648. bench_stats_asym_finish(name, keySize * 8, desc[3],
  9649. useDeviceID, count, start, ret);
  9650. #ifdef MULTI_VALUE_STATISTICS
  9651. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9652. #endif
  9653. RESET_MULTI_VALUE_STATS_VARS();
  9654. if (ret < 0) {
  9655. goto exit;
  9656. }
  9657. #endif /* HAVE_ECC_DHE */
  9658. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9659. /* Init digest to sign */
  9660. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9661. for (count = 0; count < keySize; count++) {
  9662. digest[i][count] = (byte)count;
  9663. }
  9664. }
  9665. /* ECC Sign */
  9666. bench_stats_start(&count, &start);
  9667. do {
  9668. for (times = 0; times < agreeTimes || pending > 0; ) {
  9669. bench_async_poll(&pending);
  9670. /* while free pending slots in queue, submit ops */
  9671. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9672. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  9673. &times, agreeTimes, &pending)) {
  9674. if (genKey[i]->state == 0) {
  9675. x[i] = ECC_MAX_SIG_SIZE;
  9676. }
  9677. ret = wc_ecc_sign_hash(digest[i], (word32)keySize, sig[i],
  9678. &x[i], GLOBAL_RNG, genKey[i]);
  9679. if (!bench_async_handle(&ret,
  9680. BENCH_ASYNC_GET_DEV(genKey[i]), 1, &times,
  9681. &pending)) {
  9682. goto exit_ecdsa_sign;
  9683. }
  9684. } /* bench_async_check */
  9685. } /* for i */
  9686. RECORD_MULTI_VALUE_STATS();
  9687. } /* for times */
  9688. count += times;
  9689. } while (bench_stats_check(start)
  9690. #ifdef MULTI_VALUE_STATISTICS
  9691. || runs < minimum_runs
  9692. #endif
  9693. );
  9694. exit_ecdsa_sign:
  9695. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDSA [%15s]",
  9696. wc_ecc_get_name(curveId));
  9697. bench_stats_asym_finish(name, keySize * 8, desc[4],
  9698. useDeviceID, count, start, ret);
  9699. #ifdef MULTI_VALUE_STATISTICS
  9700. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9701. #endif
  9702. RESET_MULTI_VALUE_STATS_VARS();
  9703. if (ret < 0) {
  9704. goto exit;
  9705. }
  9706. #ifdef HAVE_ECC_VERIFY
  9707. /* ECC Verify */
  9708. bench_stats_start(&count, &start);
  9709. do {
  9710. for (times = 0; times < agreeTimes || pending > 0; ) {
  9711. bench_async_poll(&pending);
  9712. /* while free pending slots in queue, submit ops */
  9713. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9714. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  9715. &times, agreeTimes, &pending)) {
  9716. if (genKey[i]->state == 0) {
  9717. verify[i] = 0;
  9718. }
  9719. ret = wc_ecc_verify_hash(sig[i], x[i], digest[i],
  9720. (word32)keySize, &verify[i],
  9721. genKey[i]);
  9722. if (!bench_async_handle(&ret,
  9723. BENCH_ASYNC_GET_DEV(genKey[i]),
  9724. 1, &times,
  9725. &pending)) {
  9726. goto exit_ecdsa_verify;
  9727. }
  9728. } /* if bench_async_check */
  9729. } /* for i */
  9730. RECORD_MULTI_VALUE_STATS();
  9731. } /* for times */
  9732. count += times;
  9733. } while (bench_stats_check(start)
  9734. #ifdef MULTI_VALUE_STATISTICS
  9735. || runs < minimum_runs
  9736. #endif
  9737. );
  9738. exit_ecdsa_verify:
  9739. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDSA [%15s]",
  9740. wc_ecc_get_name(curveId));
  9741. bench_stats_asym_finish(name, keySize * 8, desc[5],
  9742. useDeviceID, count, start, ret);
  9743. #ifdef MULTI_VALUE_STATISTICS
  9744. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9745. #endif
  9746. #endif /* HAVE_ECC_VERIFY */
  9747. #endif /* !NO_ASN && HAVE_ECC_SIGN */
  9748. exit:
  9749. /* cleanup */
  9750. if (WC_ARRAY_OK(genKey)) {
  9751. for (i = 0; i < BENCH_MAX_PENDING; i++)
  9752. wc_ecc_free(genKey[i]);
  9753. WC_FREE_ARRAY(genKey, BENCH_MAX_PENDING, HEAP_HINT);
  9754. }
  9755. #ifdef HAVE_ECC_DHE
  9756. if (WC_ARRAY_OK(genKey2)) {
  9757. for (i = 0; i < BENCH_MAX_PENDING; i++)
  9758. wc_ecc_free(genKey2[i]);
  9759. WC_FREE_ARRAY(genKey2, BENCH_MAX_PENDING, HEAP_HINT);
  9760. }
  9761. #endif
  9762. #ifdef HAVE_ECC_DHE
  9763. WC_FREE_ARRAY(shared, BENCH_MAX_PENDING, HEAP_HINT);
  9764. #endif
  9765. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9766. WC_FREE_ARRAY(sig, BENCH_MAX_PENDING, HEAP_HINT);
  9767. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  9768. #endif
  9769. (void)useDeviceID;
  9770. (void)pending;
  9771. (void)x;
  9772. (void)count;
  9773. (void)times;
  9774. (void)desc;
  9775. (void)start;
  9776. (void)name;
  9777. }
  9778. #ifdef HAVE_ECC_ENCRYPT
  9779. void bench_eccEncrypt(int curveId)
  9780. {
  9781. #define BENCH_ECCENCRYPT_MSG_SIZE 48
  9782. #define BENCH_ECCENCRYPT_OUT_SIZE (BENCH_ECCENCRYPT_MSG_SIZE + \
  9783. WC_SHA256_DIGEST_SIZE + \
  9784. (MAX_ECC_BITS+3)/4 + 2)
  9785. word32 outSz = BENCH_ECCENCRYPT_OUT_SIZE;
  9786. #ifdef WOLFSSL_SMALL_STACK
  9787. ecc_key *userA = NULL, *userB = NULL;
  9788. byte *msg = NULL;
  9789. byte *out = NULL;
  9790. #else
  9791. ecc_key userA[1], userB[1];
  9792. byte msg[BENCH_ECCENCRYPT_MSG_SIZE];
  9793. byte out[BENCH_ECCENCRYPT_OUT_SIZE];
  9794. #endif
  9795. char name[BENCH_ECC_NAME_SZ];
  9796. int keySize;
  9797. word32 bench_plainSz = bench_size;
  9798. int ret, i, count;
  9799. double start;
  9800. const char**desc = bench_desc_words[lng_index];
  9801. DECLARE_MULTI_VALUE_STATS_VARS()
  9802. #ifdef WOLFSSL_SMALL_STACK
  9803. userA = (ecc_key *)XMALLOC(sizeof(*userA),
  9804. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9805. userB = (ecc_key *)XMALLOC(sizeof(*userB),
  9806. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9807. msg = (byte *)XMALLOC(BENCH_ECCENCRYPT_MSG_SIZE,
  9808. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9809. out = (byte *)XMALLOC(outSz,
  9810. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9811. if ((! userA) || (! userB) || (! msg) || (! out)) {
  9812. printf("bench_eccEncrypt malloc failed\n");
  9813. goto exit;
  9814. }
  9815. #endif
  9816. keySize = wc_ecc_get_curve_size_from_id(curveId);
  9817. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  9818. if (ret != 0) {
  9819. printf("wc_ecc_encrypt make key A failed: %d\n", ret);
  9820. goto exit;
  9821. }
  9822. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  9823. if (ret != 0) {
  9824. printf("wc_ecc_encrypt make key B failed: %d\n", ret);
  9825. goto exit;
  9826. }
  9827. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  9828. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  9829. !defined(HAVE_SELFTEST)
  9830. ret = wc_ecc_set_rng(userA, &gRng);
  9831. if (ret != 0) {
  9832. goto exit;
  9833. }
  9834. ret = wc_ecc_set_rng(userB, &gRng);
  9835. if (ret != 0) {
  9836. goto exit;
  9837. }
  9838. #endif
  9839. ret = wc_ecc_make_key_ex(&gRng, keySize, userA, curveId);
  9840. #ifdef WOLFSSL_ASYNC_CRYPT
  9841. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  9842. #endif
  9843. if (ret != 0)
  9844. goto exit;
  9845. ret = wc_ecc_make_key_ex(&gRng, keySize, userB, curveId);
  9846. #ifdef WOLFSSL_ASYNC_CRYPT
  9847. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  9848. #endif
  9849. if (ret != 0)
  9850. goto exit;
  9851. for (i = 0; i < BENCH_ECCENCRYPT_MSG_SIZE; i++) {
  9852. msg[i] = (byte)i;
  9853. }
  9854. bench_stats_start(&count, &start);
  9855. do {
  9856. for (i = 0; i < ntimes; i++) {
  9857. /* encrypt msg to B */
  9858. ret = wc_ecc_encrypt(userA, userB, msg, BENCH_ECCENCRYPT_MSG_SIZE,
  9859. out, &outSz, NULL);
  9860. if (ret != 0) {
  9861. printf("wc_ecc_encrypt failed! %d\n", ret);
  9862. goto exit_enc;
  9863. }
  9864. RECORD_MULTI_VALUE_STATS();
  9865. }
  9866. count += i;
  9867. } while (bench_stats_check(start)
  9868. #ifdef MULTI_VALUE_STATISTICS
  9869. || runs < minimum_runs
  9870. #endif
  9871. );
  9872. exit_enc:
  9873. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECC [%15s]",
  9874. wc_ecc_get_name(curveId));
  9875. bench_stats_asym_finish(name, keySize * 8, desc[6], 0, count, start, ret);
  9876. #ifdef MULTI_VALUE_STATISTICS
  9877. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9878. #endif
  9879. RESET_MULTI_VALUE_STATS_VARS();
  9880. if (ret != 0)
  9881. goto exit;
  9882. bench_stats_start(&count, &start);
  9883. do {
  9884. for (i = 0; i < ntimes; i++) {
  9885. /* decrypt msg from A */
  9886. ret = wc_ecc_decrypt(userB, userA, out, outSz, bench_plain,
  9887. &bench_plainSz, NULL);
  9888. if (ret != 0) {
  9889. printf("wc_ecc_decrypt failed! %d\n", ret);
  9890. goto exit_dec;
  9891. }
  9892. RECORD_MULTI_VALUE_STATS();
  9893. }
  9894. count += i;
  9895. } while (bench_stats_check(start)
  9896. #ifdef MULTI_VALUE_STATISTICS
  9897. || runs < minimum_runs
  9898. #endif
  9899. );
  9900. exit_dec:
  9901. bench_stats_asym_finish(name, keySize * 8, desc[7], 0, count, start, ret);
  9902. #ifdef MULTI_VALUE_STATISTICS
  9903. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9904. #endif
  9905. exit:
  9906. /* cleanup */
  9907. #ifdef WOLFSSL_SMALL_STACK
  9908. if (userA) {
  9909. wc_ecc_free(userA);
  9910. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9911. }
  9912. if (userB) {
  9913. wc_ecc_free(userB);
  9914. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9915. }
  9916. if (msg)
  9917. XFREE(msg, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9918. if (out)
  9919. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9920. #else
  9921. wc_ecc_free(userB);
  9922. wc_ecc_free(userA);
  9923. #endif
  9924. }
  9925. #endif
  9926. #ifdef WOLFSSL_SM2
  9927. static void bench_sm2_MakeKey(int useDeviceID)
  9928. {
  9929. int ret = 0, i, times, count = 0, pending = 0;
  9930. int deviceID;
  9931. int keySize;
  9932. WC_DECLARE_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9933. sizeof(ecc_key), HEAP_HINT);
  9934. char name[BENCH_ECC_NAME_SZ];
  9935. double start = 0;
  9936. const char**desc = bench_desc_words[lng_index];
  9937. DECLARE_MULTI_VALUE_STATS_VARS()
  9938. deviceID = useDeviceID ? devId : INVALID_DEVID;
  9939. keySize = wc_ecc_get_curve_size_from_id(ECC_SM2P256V1);
  9940. WC_CALLOC_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9941. sizeof(ecc_key), HEAP_HINT);
  9942. /* ECC Make Key */
  9943. bench_stats_start(&count, &start);
  9944. do {
  9945. /* while free pending slots in queue, submit ops */
  9946. for (times = 0; times < agreeTimes || pending > 0; ) {
  9947. bench_async_poll(&pending);
  9948. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9949. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 0,
  9950. &times, agreeTimes, &pending)) {
  9951. wc_ecc_free(genKey[i]);
  9952. ret = wc_ecc_init_ex(genKey[i], HEAP_HINT, deviceID);
  9953. if (ret < 0) {
  9954. goto exit;
  9955. }
  9956. ret = wc_ecc_sm2_make_key(&gRng, genKey[i],
  9957. WC_ECC_FLAG_NONE);
  9958. if (!bench_async_handle(&ret,
  9959. BENCH_ASYNC_GET_DEV(genKey[i]), 0, &times,
  9960. &pending)) {
  9961. goto exit;
  9962. }
  9963. }
  9964. } /* for i */
  9965. RECORD_MULTI_VALUE_STATS();
  9966. } /* for times */
  9967. count += times;
  9968. } while (bench_stats_check(start)
  9969. #ifdef MULTI_VALUE_STATISTICS
  9970. || runs < minimum_runs
  9971. #endif
  9972. );
  9973. exit:
  9974. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECC [%15s]",
  9975. wc_ecc_get_name(ECC_SM2P256V1));
  9976. bench_stats_asym_finish(name, keySize * 8, desc[2], useDeviceID, count,
  9977. start, ret);
  9978. #ifdef MULTI_VALUE_STATISTICS
  9979. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9980. #endif
  9981. /* cleanup */
  9982. if (WC_ARRAY_OK(genKey)) {
  9983. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9984. wc_ecc_free(genKey[i]);
  9985. }
  9986. WC_FREE_ARRAY(genKey, BENCH_MAX_PENDING, HEAP_HINT);
  9987. }
  9988. }
  9989. void bench_sm2(int useDeviceID)
  9990. {
  9991. int ret = 0, i, times, count, pending = 0;
  9992. int deviceID;
  9993. int keySize;
  9994. char name[BENCH_ECC_NAME_SZ];
  9995. WC_DECLARE_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9996. sizeof(ecc_key), HEAP_HINT);
  9997. #ifdef HAVE_ECC_DHE
  9998. WC_DECLARE_ARRAY(genKey2, ecc_key, BENCH_MAX_PENDING,
  9999. sizeof(ecc_key), HEAP_HINT);
  10000. #endif
  10001. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  10002. #ifdef HAVE_ECC_VERIFY
  10003. int verify[BENCH_MAX_PENDING];
  10004. #endif
  10005. #endif
  10006. word32 x[BENCH_MAX_PENDING];
  10007. double start = 0;
  10008. const char**desc = bench_desc_words[lng_index];
  10009. DECLARE_MULTI_VALUE_STATS_VARS()
  10010. #ifdef HAVE_ECC_DHE
  10011. WC_DECLARE_ARRAY(shared, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  10012. #endif
  10013. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  10014. WC_DECLARE_ARRAY(sig, byte, BENCH_MAX_PENDING, ECC_MAX_SIG_SIZE, HEAP_HINT);
  10015. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  10016. #endif
  10017. #ifdef HAVE_ECC_DHE
  10018. WC_ALLOC_ARRAY(shared, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  10019. #endif
  10020. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  10021. WC_ALLOC_ARRAY(sig, byte, BENCH_MAX_PENDING, ECC_MAX_SIG_SIZE, HEAP_HINT);
  10022. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  10023. #endif
  10024. deviceID = useDeviceID ? devId : INVALID_DEVID;
  10025. bench_sm2_MakeKey(useDeviceID);
  10026. WC_CALLOC_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  10027. sizeof(ecc_key), HEAP_HINT);
  10028. #ifdef HAVE_ECC_DHE
  10029. WC_CALLOC_ARRAY(genKey2, ecc_key, BENCH_MAX_PENDING,
  10030. sizeof(ecc_key), HEAP_HINT);
  10031. #endif
  10032. keySize = wc_ecc_get_curve_size_from_id(ECC_SM2P256V1);
  10033. /* init keys */
  10034. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  10035. /* setup an context for each key */
  10036. if ((ret = wc_ecc_init_ex(genKey[i], HEAP_HINT, deviceID)) < 0) {
  10037. goto exit;
  10038. }
  10039. ret = wc_ecc_sm2_make_key(&gRng, genKey[i], WC_ECC_FLAG_NONE);
  10040. #ifdef WOLFSSL_ASYNC_CRYPT
  10041. ret = wc_AsyncWait(ret, genKey[i].asyncDev, WC_ASYNC_FLAG_NONE);
  10042. #endif
  10043. if (ret < 0) {
  10044. goto exit;
  10045. }
  10046. #ifdef HAVE_ECC_DHE
  10047. if ((ret = wc_ecc_init_ex(genKey2[i], HEAP_HINT, deviceID)) < 0) {
  10048. goto exit;
  10049. }
  10050. if ((ret = wc_ecc_sm2_make_key(&gRng, genKey2[i],
  10051. WC_ECC_FLAG_NONE)) > 0) {
  10052. goto exit;
  10053. }
  10054. #endif
  10055. }
  10056. #ifdef HAVE_ECC_DHE
  10057. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  10058. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  10059. !defined(HAVE_SELFTEST)
  10060. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  10061. (void)wc_ecc_set_rng(genKey[i], &gRng);
  10062. }
  10063. #endif
  10064. /* ECC Shared Secret */
  10065. bench_stats_start(&count, &start);
  10066. PRIVATE_KEY_UNLOCK();
  10067. do {
  10068. for (times = 0; times < agreeTimes || pending > 0; ) {
  10069. bench_async_poll(&pending);
  10070. /* while free pending slots in queue, submit ops */
  10071. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  10072. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  10073. &times, agreeTimes, &pending)) {
  10074. x[i] = (word32)keySize;
  10075. ret = wc_ecc_sm2_shared_secret(genKey[i], genKey2[i],
  10076. shared[i], &x[i]);
  10077. if (!bench_async_handle(&ret,
  10078. BENCH_ASYNC_GET_DEV(genKey[i]), 1, &times,
  10079. &pending)) {
  10080. goto exit_ecdhe;
  10081. }
  10082. }
  10083. } /* for i */
  10084. RECORD_MULTI_VALUE_STATS();
  10085. } /* for times */
  10086. count += times;
  10087. } while (bench_stats_check(start)
  10088. #ifdef MULTI_VALUE_STATISTICS
  10089. || runs < minimum_runs
  10090. #endif
  10091. );
  10092. PRIVATE_KEY_UNLOCK();
  10093. exit_ecdhe:
  10094. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDHE [%15s]",
  10095. wc_ecc_get_name(ECC_SM2P256V1));
  10096. bench_stats_asym_finish(name, keySize * 8, desc[3], useDeviceID, count,
  10097. start, ret);
  10098. #ifdef MULTI_VALUE_STATISTICS
  10099. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10100. #endif
  10101. if (ret < 0) {
  10102. goto exit;
  10103. }
  10104. #endif /* HAVE_ECC_DHE */
  10105. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  10106. /* Init digest to sign */
  10107. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  10108. for (count = 0; count < keySize; count++) {
  10109. digest[i][count] = (byte)count;
  10110. }
  10111. }
  10112. RESET_MULTI_VALUE_STATS_VARS();
  10113. /* ECC Sign */
  10114. bench_stats_start(&count, &start);
  10115. do {
  10116. for (times = 0; times < agreeTimes || pending > 0; ) {
  10117. bench_async_poll(&pending);
  10118. /* while free pending slots in queue, submit ops */
  10119. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  10120. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  10121. &times, agreeTimes, &pending)) {
  10122. if (genKey[i]->state == 0)
  10123. x[i] = ECC_MAX_SIG_SIZE;
  10124. ret = wc_ecc_sm2_sign_hash(digest[i], (word32)keySize,
  10125. sig[i], &x[i], &gRng, genKey[i]);
  10126. if (!bench_async_handle(&ret,
  10127. BENCH_ASYNC_GET_DEV(genKey[i]), 1, &times,
  10128. &pending)) {
  10129. goto exit_ecdsa_sign;
  10130. }
  10131. }
  10132. } /* for i */
  10133. RECORD_MULTI_VALUE_STATS();
  10134. } /* for times */
  10135. count += times;
  10136. } while (bench_stats_check(start)
  10137. #ifdef MULTI_VALUE_STATISTICS
  10138. || runs < minimum_runs
  10139. #endif
  10140. );
  10141. exit_ecdsa_sign:
  10142. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDSA [%15s]",
  10143. wc_ecc_get_name(ECC_SM2P256V1));
  10144. bench_stats_asym_finish(name, keySize * 8, desc[4], useDeviceID, count,
  10145. start, ret);
  10146. #ifdef MULTI_VALUE_STATISTICS
  10147. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10148. #endif
  10149. if (ret < 0) {
  10150. goto exit;
  10151. }
  10152. #ifdef HAVE_ECC_VERIFY
  10153. /* ECC Verify */
  10154. bench_stats_start(&count, &start);
  10155. do {
  10156. for (times = 0; times < agreeTimes || pending > 0; ) {
  10157. bench_async_poll(&pending);
  10158. /* while free pending slots in queue, submit ops */
  10159. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  10160. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  10161. &times, agreeTimes, &pending)) {
  10162. if (genKey[i]->state == 0)
  10163. verify[i] = 0;
  10164. ret = wc_ecc_sm2_verify_hash(sig[i], x[i], digest[i],
  10165. (word32)keySize, &verify[i], genKey[i]);
  10166. if (!bench_async_handle(&ret,
  10167. BENCH_ASYNC_GET_DEV(genKey[i]), 1, &times,
  10168. &pending)) {
  10169. goto exit_ecdsa_verify;
  10170. }
  10171. }
  10172. } /* for i */
  10173. RECORD_MULTI_VALUE_STATS();
  10174. } /* for times */
  10175. count += times;
  10176. } while (bench_stats_check(start)
  10177. #ifdef MULTI_VALUE_STATISTICS
  10178. || runs < minimum_runs
  10179. #endif
  10180. );
  10181. exit_ecdsa_verify:
  10182. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDSA [%15s]",
  10183. wc_ecc_get_name(ECC_SM2P256V1));
  10184. bench_stats_asym_finish(name, keySize * 8, desc[5], useDeviceID, count,
  10185. start, ret);
  10186. #ifdef MULTI_VALUE_STATISTICS
  10187. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10188. #endif
  10189. #endif /* HAVE_ECC_VERIFY */
  10190. #endif /* !NO_ASN && HAVE_ECC_SIGN */
  10191. exit:
  10192. /* cleanup */
  10193. if (WC_ARRAY_OK(genKey)) {
  10194. for (i = 0; i < BENCH_MAX_PENDING; i++)
  10195. wc_ecc_free(genKey[i]);
  10196. WC_FREE_ARRAY(genKey, BENCH_MAX_PENDING, HEAP_HINT);
  10197. }
  10198. #ifdef HAVE_ECC_DHE
  10199. if (WC_ARRAY_OK(genKey2)) {
  10200. for (i = 0; i < BENCH_MAX_PENDING; i++)
  10201. wc_ecc_free(genKey2[i]);
  10202. WC_FREE_ARRAY(genKey2, BENCH_MAX_PENDING, HEAP_HINT);
  10203. }
  10204. #endif
  10205. #ifdef HAVE_ECC_DHE
  10206. WC_FREE_ARRAY(shared, BENCH_MAX_PENDING, HEAP_HINT);
  10207. #endif
  10208. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  10209. WC_FREE_ARRAY(sig, BENCH_MAX_PENDING, HEAP_HINT);
  10210. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  10211. #endif
  10212. (void)useDeviceID;
  10213. (void)pending;
  10214. (void)x;
  10215. (void)count;
  10216. (void)times;
  10217. (void)desc;
  10218. (void)start;
  10219. (void)name;
  10220. }
  10221. #endif /* WOLFSSL_SM2 */
  10222. #endif /* HAVE_ECC */
  10223. #ifdef HAVE_CURVE25519
  10224. void bench_curve25519KeyGen(int useDeviceID)
  10225. {
  10226. curve25519_key genKey;
  10227. double start;
  10228. int ret = 0, i, count;
  10229. const char**desc = bench_desc_words[lng_index];
  10230. DECLARE_MULTI_VALUE_STATS_VARS()
  10231. /* Key Gen */
  10232. bench_stats_start(&count, &start);
  10233. do {
  10234. for (i = 0; i < genTimes; i++) {
  10235. ret = wc_curve25519_init_ex(&genKey, HEAP_HINT,
  10236. useDeviceID ? devId : INVALID_DEVID);
  10237. if (ret != 0) {
  10238. printf("wc_curve25519_init_ex failed: %d\n", ret);
  10239. break;
  10240. }
  10241. ret = wc_curve25519_make_key(&gRng, 32, &genKey);
  10242. wc_curve25519_free(&genKey);
  10243. if (ret != 0) {
  10244. printf("wc_curve25519_make_key failed: %d\n", ret);
  10245. break;
  10246. }
  10247. RECORD_MULTI_VALUE_STATS();
  10248. }
  10249. count += i;
  10250. } while (bench_stats_check(start)
  10251. #ifdef MULTI_VALUE_STATISTICS
  10252. || runs < minimum_runs
  10253. #endif
  10254. );
  10255. bench_stats_asym_finish("CURVE", 25519, desc[2], useDeviceID, count, start,
  10256. ret);
  10257. #ifdef MULTI_VALUE_STATISTICS
  10258. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10259. #endif
  10260. }
  10261. #ifdef HAVE_CURVE25519_SHARED_SECRET
  10262. void bench_curve25519KeyAgree(int useDeviceID)
  10263. {
  10264. curve25519_key genKey, genKey2;
  10265. double start;
  10266. int ret, i, count;
  10267. byte shared[32];
  10268. const char**desc = bench_desc_words[lng_index];
  10269. word32 x = 0;
  10270. DECLARE_MULTI_VALUE_STATS_VARS()
  10271. wc_curve25519_init_ex(&genKey, HEAP_HINT,
  10272. useDeviceID ? devId : INVALID_DEVID);
  10273. wc_curve25519_init_ex(&genKey2, HEAP_HINT,
  10274. useDeviceID ? devId : INVALID_DEVID);
  10275. ret = wc_curve25519_make_key(&gRng, 32, &genKey);
  10276. if (ret != 0) {
  10277. printf("curve25519_make_key failed\n");
  10278. return;
  10279. }
  10280. ret = wc_curve25519_make_key(&gRng, 32, &genKey2);
  10281. if (ret != 0) {
  10282. printf("curve25519_make_key failed: %d\n", ret);
  10283. wc_curve25519_free(&genKey);
  10284. return;
  10285. }
  10286. /* Shared secret */
  10287. bench_stats_start(&count, &start);
  10288. do {
  10289. for (i = 0; i < agreeTimes; i++) {
  10290. x = sizeof(shared);
  10291. ret = wc_curve25519_shared_secret(&genKey, &genKey2, shared, &x);
  10292. if (ret != 0) {
  10293. printf("curve25519_shared_secret failed: %d\n", ret);
  10294. goto exit;
  10295. }
  10296. RECORD_MULTI_VALUE_STATS();
  10297. }
  10298. count += i;
  10299. } while (bench_stats_check(start)
  10300. #ifdef MULTI_VALUE_STATISTICS
  10301. || runs < minimum_runs
  10302. #endif
  10303. );
  10304. exit:
  10305. bench_stats_asym_finish("CURVE", 25519, desc[3], useDeviceID, count, start,
  10306. ret);
  10307. #ifdef MULTI_VALUE_STATISTICS
  10308. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10309. #endif
  10310. wc_curve25519_free(&genKey2);
  10311. wc_curve25519_free(&genKey);
  10312. }
  10313. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  10314. #endif /* HAVE_CURVE25519 */
  10315. #ifdef HAVE_ED25519
  10316. void bench_ed25519KeyGen(void)
  10317. {
  10318. #ifdef HAVE_ED25519_MAKE_KEY
  10319. ed25519_key genKey;
  10320. double start;
  10321. int i, count;
  10322. const char**desc = bench_desc_words[lng_index];
  10323. DECLARE_MULTI_VALUE_STATS_VARS()
  10324. /* Key Gen */
  10325. bench_stats_start(&count, &start);
  10326. do {
  10327. for (i = 0; i < genTimes; i++) {
  10328. wc_ed25519_init(&genKey);
  10329. (void)wc_ed25519_make_key(&gRng, 32, &genKey);
  10330. wc_ed25519_free(&genKey);
  10331. RECORD_MULTI_VALUE_STATS();
  10332. }
  10333. count += i;
  10334. } while (bench_stats_check(start)
  10335. #ifdef MULTI_VALUE_STATISTICS
  10336. || runs < minimum_runs
  10337. #endif
  10338. );
  10339. bench_stats_asym_finish("ED", 25519, desc[2], 0, count, start, 0);
  10340. #ifdef MULTI_VALUE_STATISTICS
  10341. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10342. #endif
  10343. #endif /* HAVE_ED25519_MAKE_KEY */
  10344. }
  10345. void bench_ed25519KeySign(void)
  10346. {
  10347. #ifdef HAVE_ED25519_MAKE_KEY
  10348. int ret;
  10349. #endif
  10350. ed25519_key genKey;
  10351. #ifdef HAVE_ED25519_SIGN
  10352. double start;
  10353. int i, count;
  10354. byte sig[ED25519_SIG_SIZE];
  10355. byte msg[512];
  10356. word32 x = 0;
  10357. const char**desc = bench_desc_words[lng_index];
  10358. DECLARE_MULTI_VALUE_STATS_VARS()
  10359. #endif
  10360. wc_ed25519_init(&genKey);
  10361. #ifdef HAVE_ED25519_MAKE_KEY
  10362. ret = wc_ed25519_make_key(&gRng, ED25519_KEY_SIZE, &genKey);
  10363. if (ret != 0) {
  10364. printf("ed25519_make_key failed\n");
  10365. return;
  10366. }
  10367. #endif
  10368. #ifdef HAVE_ED25519_SIGN
  10369. /* make dummy msg */
  10370. for (i = 0; i < (int)sizeof(msg); i++)
  10371. msg[i] = (byte)i;
  10372. bench_stats_start(&count, &start);
  10373. do {
  10374. for (i = 0; i < agreeTimes; i++) {
  10375. x = sizeof(sig);
  10376. ret = wc_ed25519_sign_msg(msg, sizeof(msg), sig, &x, &genKey);
  10377. if (ret != 0) {
  10378. printf("ed25519_sign_msg failed\n");
  10379. goto exit_ed_sign;
  10380. }
  10381. RECORD_MULTI_VALUE_STATS();
  10382. }
  10383. count += i;
  10384. } while (bench_stats_check(start)
  10385. #ifdef MULTI_VALUE_STATISTICS
  10386. || runs < minimum_runs
  10387. #endif
  10388. );
  10389. exit_ed_sign:
  10390. bench_stats_asym_finish("ED", 25519, desc[4], 0, count, start, ret);
  10391. #ifdef MULTI_VALUE_STATISTICS
  10392. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10393. #endif
  10394. RESET_MULTI_VALUE_STATS_VARS();
  10395. #ifdef HAVE_ED25519_VERIFY
  10396. bench_stats_start(&count, &start);
  10397. do {
  10398. for (i = 0; i < agreeTimes; i++) {
  10399. int verify = 0;
  10400. ret = wc_ed25519_verify_msg(sig, x, msg, sizeof(msg), &verify,
  10401. &genKey);
  10402. if (ret != 0 || verify != 1) {
  10403. printf("ed25519_verify_msg failed\n");
  10404. goto exit_ed_verify;
  10405. }
  10406. RECORD_MULTI_VALUE_STATS();
  10407. }
  10408. count += i;
  10409. } while (bench_stats_check(start)
  10410. #ifdef MULTI_VALUE_STATISTICS
  10411. || runs < minimum_runs
  10412. #endif
  10413. );
  10414. exit_ed_verify:
  10415. bench_stats_asym_finish("ED", 25519, desc[5], 0, count, start, ret);
  10416. #ifdef MULTI_VALUE_STATISTICS
  10417. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10418. #endif
  10419. #endif /* HAVE_ED25519_VERIFY */
  10420. #endif /* HAVE_ED25519_SIGN */
  10421. wc_ed25519_free(&genKey);
  10422. }
  10423. #endif /* HAVE_ED25519 */
  10424. #ifdef HAVE_CURVE448
  10425. void bench_curve448KeyGen(void)
  10426. {
  10427. curve448_key genKey;
  10428. double start;
  10429. int ret = 0, i, count;
  10430. const char**desc = bench_desc_words[lng_index];
  10431. DECLARE_MULTI_VALUE_STATS_VARS()
  10432. /* Key Gen */
  10433. bench_stats_start(&count, &start);
  10434. do {
  10435. for (i = 0; i < genTimes; i++) {
  10436. ret = wc_curve448_make_key(&gRng, 56, &genKey);
  10437. wc_curve448_free(&genKey);
  10438. if (ret != 0) {
  10439. printf("wc_curve448_make_key failed: %d\n", ret);
  10440. break;
  10441. }
  10442. RECORD_MULTI_VALUE_STATS();
  10443. }
  10444. count += i;
  10445. } while (bench_stats_check(start)
  10446. #ifdef MULTI_VALUE_STATISTICS
  10447. || runs < minimum_runs
  10448. #endif
  10449. );
  10450. bench_stats_asym_finish("CURVE", 448, desc[2], 0, count, start, ret);
  10451. #ifdef MULTI_VALUE_STATISTICS
  10452. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10453. #endif
  10454. }
  10455. #ifdef HAVE_CURVE448_SHARED_SECRET
  10456. void bench_curve448KeyAgree(void)
  10457. {
  10458. curve448_key genKey, genKey2;
  10459. double start;
  10460. int ret, i, count;
  10461. byte shared[56];
  10462. const char**desc = bench_desc_words[lng_index];
  10463. word32 x = 0;
  10464. DECLARE_MULTI_VALUE_STATS_VARS()
  10465. wc_curve448_init(&genKey);
  10466. wc_curve448_init(&genKey2);
  10467. ret = wc_curve448_make_key(&gRng, 56, &genKey);
  10468. if (ret != 0) {
  10469. printf("curve448_make_key failed\n");
  10470. return;
  10471. }
  10472. ret = wc_curve448_make_key(&gRng, 56, &genKey2);
  10473. if (ret != 0) {
  10474. printf("curve448_make_key failed: %d\n", ret);
  10475. wc_curve448_free(&genKey);
  10476. return;
  10477. }
  10478. /* Shared secret */
  10479. bench_stats_start(&count, &start);
  10480. do {
  10481. for (i = 0; i < agreeTimes; i++) {
  10482. x = sizeof(shared);
  10483. ret = wc_curve448_shared_secret(&genKey, &genKey2, shared, &x);
  10484. if (ret != 0) {
  10485. printf("curve448_shared_secret failed: %d\n", ret);
  10486. goto exit;
  10487. }
  10488. RECORD_MULTI_VALUE_STATS();
  10489. }
  10490. count += i;
  10491. } while (bench_stats_check(start)
  10492. #ifdef MULTI_VALUE_STATISTICS
  10493. || runs < minimum_runs
  10494. #endif
  10495. );
  10496. exit:
  10497. bench_stats_asym_finish("CURVE", 448, desc[3], 0, count, start, ret);
  10498. #ifdef MULTI_VALUE_STATISTICS
  10499. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10500. #endif
  10501. wc_curve448_free(&genKey2);
  10502. wc_curve448_free(&genKey);
  10503. }
  10504. #endif /* HAVE_CURVE448_SHARED_SECRET */
  10505. #endif /* HAVE_CURVE448 */
  10506. #ifdef HAVE_ED448
  10507. void bench_ed448KeyGen(void)
  10508. {
  10509. ed448_key genKey;
  10510. double start;
  10511. int i, count;
  10512. const char**desc = bench_desc_words[lng_index];
  10513. DECLARE_MULTI_VALUE_STATS_VARS()
  10514. /* Key Gen */
  10515. bench_stats_start(&count, &start);
  10516. do {
  10517. for (i = 0; i < genTimes; i++) {
  10518. wc_ed448_init(&genKey);
  10519. (void)wc_ed448_make_key(&gRng, ED448_KEY_SIZE, &genKey);
  10520. wc_ed448_free(&genKey);
  10521. RECORD_MULTI_VALUE_STATS();
  10522. }
  10523. count += i;
  10524. } while (bench_stats_check(start)
  10525. #ifdef MULTI_VALUE_STATISTICS
  10526. || runs < minimum_runs
  10527. #endif
  10528. );
  10529. bench_stats_asym_finish("ED", 448, desc[2], 0, count, start, 0);
  10530. #ifdef MULTI_VALUE_STATISTICS
  10531. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10532. #endif
  10533. }
  10534. void bench_ed448KeySign(void)
  10535. {
  10536. int ret;
  10537. WC_DECLARE_VAR(genKey, ed448_key, 1, HEAP_HINT);
  10538. #ifdef HAVE_ED448_SIGN
  10539. double start;
  10540. int i, count;
  10541. byte sig[ED448_SIG_SIZE];
  10542. byte msg[512];
  10543. word32 x = 0;
  10544. const char**desc = bench_desc_words[lng_index];
  10545. DECLARE_MULTI_VALUE_STATS_VARS()
  10546. #endif
  10547. WC_ALLOC_VAR(genKey, ed448_key, 1, HEAP_HINT);
  10548. wc_ed448_init(genKey);
  10549. ret = wc_ed448_make_key(&gRng, ED448_KEY_SIZE, genKey);
  10550. if (ret != 0) {
  10551. printf("ed448_make_key failed\n");
  10552. goto exit;
  10553. }
  10554. #ifdef HAVE_ED448_SIGN
  10555. /* make dummy msg */
  10556. for (i = 0; i < (int)sizeof(msg); i++)
  10557. msg[i] = (byte)i;
  10558. bench_stats_start(&count, &start);
  10559. do {
  10560. for (i = 0; i < agreeTimes; i++) {
  10561. x = sizeof(sig);
  10562. ret = wc_ed448_sign_msg(msg, sizeof(msg), sig, &x, genKey,
  10563. NULL, 0);
  10564. if (ret != 0) {
  10565. printf("ed448_sign_msg failed\n");
  10566. goto exit;
  10567. }
  10568. RECORD_MULTI_VALUE_STATS();
  10569. }
  10570. count += i;
  10571. } while (bench_stats_check(start)
  10572. #ifdef MULTI_VALUE_STATISTICS
  10573. || runs < minimum_runs
  10574. #endif
  10575. );
  10576. bench_stats_asym_finish("ED", 448, desc[4], 0, count, start, ret);
  10577. #ifdef MULTI_VALUE_STATISTICS
  10578. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10579. #endif
  10580. RESET_MULTI_VALUE_STATS_VARS();
  10581. #ifdef HAVE_ED448_VERIFY
  10582. bench_stats_start(&count, &start);
  10583. do {
  10584. for (i = 0; i < agreeTimes; i++) {
  10585. int verify = 0;
  10586. ret = wc_ed448_verify_msg(sig, x, msg, sizeof(msg), &verify,
  10587. genKey, NULL, 0);
  10588. if (ret != 0 || verify != 1) {
  10589. printf("ed448_verify_msg failed\n");
  10590. goto exit;
  10591. }
  10592. RECORD_MULTI_VALUE_STATS();
  10593. }
  10594. count += i;
  10595. } while (bench_stats_check(start)
  10596. #ifdef MULTI_VALUE_STATISTICS
  10597. || runs < minimum_runs
  10598. #endif
  10599. );
  10600. bench_stats_asym_finish("ED", 448, desc[5], 0, count, start, ret);
  10601. #ifdef MULTI_VALUE_STATISTICS
  10602. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10603. #endif
  10604. #endif /* HAVE_ED448_VERIFY */
  10605. #endif /* HAVE_ED448_SIGN */
  10606. exit:
  10607. wc_ed448_free(genKey);
  10608. WC_FREE_VAR(genKey, HEAP_HINT);
  10609. }
  10610. #endif /* HAVE_ED448 */
  10611. #ifdef WOLFCRYPT_HAVE_ECCSI
  10612. #ifdef WOLFCRYPT_ECCSI_KMS
  10613. void bench_eccsiKeyGen(void)
  10614. {
  10615. WC_DECLARE_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10616. double start;
  10617. int i, count;
  10618. const char**desc = bench_desc_words[lng_index];
  10619. int ret;
  10620. DECLARE_MULTI_VALUE_STATS_VARS()
  10621. WC_ALLOC_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10622. /* Key Gen */
  10623. bench_stats_start(&count, &start);
  10624. do {
  10625. for (i = 0; i < genTimes; i++) {
  10626. wc_InitEccsiKey(genKey, NULL, INVALID_DEVID);
  10627. ret = wc_MakeEccsiKey(genKey, &gRng);
  10628. wc_FreeEccsiKey(genKey);
  10629. if (ret != 0) {
  10630. printf("wc_MakeEccsiKey failed: %d\n", ret);
  10631. goto exit;
  10632. }
  10633. RECORD_MULTI_VALUE_STATS();
  10634. }
  10635. count += i;
  10636. } while (bench_stats_check(start)
  10637. #ifdef MULTI_VALUE_STATISTICS
  10638. || runs < minimum_runs
  10639. #endif
  10640. );
  10641. bench_stats_asym_finish("ECCSI", 256, desc[2], 0, count, start, 0);
  10642. #ifdef MULTI_VALUE_STATISTICS
  10643. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10644. #endif
  10645. exit:
  10646. WC_FREE_VAR(genKey, HEAP_HINT);
  10647. }
  10648. void bench_eccsiPairGen(void)
  10649. {
  10650. WC_DECLARE_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10651. double start;
  10652. int i, count;
  10653. const char**desc = bench_desc_words[lng_index];
  10654. WC_DECLARE_VAR(ssk, mp_int, 1, HEAP_HINT);
  10655. ecc_point* pvt;
  10656. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10657. int ret;
  10658. DECLARE_MULTI_VALUE_STATS_VARS()
  10659. WC_ALLOC_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10660. WC_ALLOC_VAR(ssk, mp_int, 1, HEAP_HINT);
  10661. (void)mp_init(ssk);
  10662. pvt = wc_ecc_new_point();
  10663. wc_InitEccsiKey(genKey, NULL, INVALID_DEVID);
  10664. (void)wc_MakeEccsiKey(genKey, &gRng);
  10665. /* RSK Gen */
  10666. bench_stats_start(&count, &start);
  10667. do {
  10668. for (i = 0; i < genTimes; i++) {
  10669. ret = wc_MakeEccsiPair(genKey, &gRng, WC_HASH_TYPE_SHA256, id,
  10670. sizeof(id), ssk, pvt);
  10671. if (ret != 0) {
  10672. printf("wc_MakeEccsiPair failed: %d\n", ret);
  10673. goto exit;
  10674. }
  10675. RECORD_MULTI_VALUE_STATS();
  10676. }
  10677. count += i;
  10678. } while (bench_stats_check(start)
  10679. #ifdef MULTI_VALUE_STATISTICS
  10680. || runs < minimum_runs
  10681. #endif
  10682. );
  10683. bench_stats_asym_finish("ECCSI", 256, desc[12], 0, count, start, 0);
  10684. #ifdef MULTI_VALUE_STATISTICS
  10685. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10686. #endif
  10687. wc_FreeEccsiKey(genKey);
  10688. wc_ecc_del_point(pvt);
  10689. mp_free(ssk);
  10690. exit:
  10691. WC_FREE_VAR(genKey, HEAP_HINT);
  10692. WC_FREE_VAR(ssk, HEAP_HINT);
  10693. }
  10694. #endif
  10695. #ifdef WOLFCRYPT_ECCSI_CLIENT
  10696. void bench_eccsiValidate(void)
  10697. {
  10698. WC_DECLARE_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10699. double start;
  10700. int i, count;
  10701. const char**desc = bench_desc_words[lng_index];
  10702. WC_DECLARE_VAR(ssk, mp_int, 1, HEAP_HINT);
  10703. ecc_point* pvt;
  10704. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10705. int valid;
  10706. int ret;
  10707. DECLARE_MULTI_VALUE_STATS_VARS()
  10708. WC_ALLOC_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10709. WC_ALLOC_VAR(ssk, mp_int, 1, HEAP_HINT);
  10710. (void)mp_init(ssk);
  10711. pvt = wc_ecc_new_point();
  10712. wc_InitEccsiKey(genKey, NULL, INVALID_DEVID);
  10713. (void)wc_MakeEccsiKey(genKey, &gRng);
  10714. (void)wc_MakeEccsiPair(genKey, &gRng, WC_HASH_TYPE_SHA256, id, sizeof(id),
  10715. ssk, pvt);
  10716. /* Validation of RSK */
  10717. bench_stats_start(&count, &start);
  10718. do {
  10719. for (i = 0; i < genTimes; i++) {
  10720. ret = wc_ValidateEccsiPair(genKey, WC_HASH_TYPE_SHA256, id,
  10721. sizeof(id), ssk, pvt, &valid);
  10722. if (ret != 0 || !valid) {
  10723. printf("wc_ValidateEccsiPair failed: %d (valid=%d))\n", ret,
  10724. valid);
  10725. goto exit;
  10726. }
  10727. RECORD_MULTI_VALUE_STATS();
  10728. }
  10729. count += i;
  10730. } while (bench_stats_check(start)
  10731. #ifdef MULTI_VALUE_STATISTICS
  10732. || runs < minimum_runs
  10733. #endif
  10734. );
  10735. bench_stats_asym_finish("ECCSI", 256, desc[11], 0, count, start, 0);
  10736. #ifdef MULTI_VALUE_STATISTICS
  10737. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10738. #endif
  10739. wc_FreeEccsiKey(genKey);
  10740. wc_ecc_del_point(pvt);
  10741. mp_free(ssk);
  10742. exit:
  10743. WC_FREE_VAR(genKey, HEAP_HINT);
  10744. WC_FREE_VAR(ssk, HEAP_HINT);
  10745. }
  10746. void bench_eccsi(void)
  10747. {
  10748. WC_DECLARE_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10749. double start;
  10750. int i, count;
  10751. const char**desc = bench_desc_words[lng_index];
  10752. WC_DECLARE_VAR(ssk, mp_int, 1, HEAP_HINT);
  10753. ecc_point* pvt;
  10754. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10755. static const byte msg[] = { 0x01, 0x23, 0x34, 0x45 };
  10756. byte hash[WC_SHA256_DIGEST_SIZE];
  10757. byte hashSz = (byte)sizeof(hash);
  10758. byte sig[257];
  10759. word32 sigSz = sizeof(sig);
  10760. int ret;
  10761. int verified;
  10762. DECLARE_MULTI_VALUE_STATS_VARS()
  10763. WC_ALLOC_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10764. WC_ALLOC_VAR(ssk, mp_int, 1, HEAP_HINT);
  10765. (void)mp_init(ssk);
  10766. pvt = wc_ecc_new_point();
  10767. (void)wc_InitEccsiKey(genKey, NULL, INVALID_DEVID);
  10768. (void)wc_MakeEccsiKey(genKey, &gRng);
  10769. (void)wc_MakeEccsiPair(genKey, &gRng, WC_HASH_TYPE_SHA256, id, sizeof(id),
  10770. ssk, pvt);
  10771. (void)wc_HashEccsiId(genKey, WC_HASH_TYPE_SHA256, id, sizeof(id), pvt,
  10772. hash, &hashSz);
  10773. (void)wc_SetEccsiHash(genKey, hash, hashSz);
  10774. (void)wc_SetEccsiPair(genKey, ssk, pvt);
  10775. /* Encapsulate */
  10776. bench_stats_start(&count, &start);
  10777. do {
  10778. for (i = 0; i < genTimes; i++) {
  10779. ret = wc_SignEccsiHash(genKey, &gRng, WC_HASH_TYPE_SHA256, msg,
  10780. sizeof(msg), sig, &sigSz);
  10781. if (ret != 0) {
  10782. printf("wc_SignEccsiHash failed: %d\n", ret);
  10783. break;
  10784. }
  10785. RECORD_MULTI_VALUE_STATS();
  10786. }
  10787. count += i;
  10788. } while (bench_stats_check(start)
  10789. #ifdef MULTI_VALUE_STATISTICS
  10790. || runs < minimum_runs
  10791. #endif
  10792. );
  10793. bench_stats_asym_finish("ECCSI", 256, desc[4], 0, count, start, 0);
  10794. #ifdef MULTI_VALUE_STATISTICS
  10795. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10796. #endif
  10797. RESET_MULTI_VALUE_STATS_VARS();
  10798. /* Derive */
  10799. bench_stats_start(&count, &start);
  10800. do {
  10801. for (i = 0; i < genTimes; i++) {
  10802. ret = wc_VerifyEccsiHash(genKey, WC_HASH_TYPE_SHA256, msg,
  10803. sizeof(msg), sig, sigSz, &verified);
  10804. if (ret != 0 || !verified) {
  10805. printf("wc_VerifyEccsiHash failed: %d (verified: %d)\n", ret,
  10806. verified);
  10807. goto exit;
  10808. }
  10809. RECORD_MULTI_VALUE_STATS();
  10810. }
  10811. count += i;
  10812. } while (bench_stats_check(start)
  10813. #ifdef MULTI_VALUE_STATISTICS
  10814. || runs < minimum_runs
  10815. #endif
  10816. );
  10817. bench_stats_asym_finish("ECCSI", 256, desc[5], 0, count, start, 0);
  10818. #ifdef MULTI_VALUE_STATISTICS
  10819. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10820. #endif
  10821. wc_FreeEccsiKey(genKey);
  10822. wc_ecc_del_point(pvt);
  10823. exit:
  10824. WC_FREE_VAR(genKey, HEAP_HINT);
  10825. WC_FREE_VAR(ssk, HEAP_HINT);
  10826. }
  10827. #endif /* WOLFCRYPT_ECCSI_CLIENT */
  10828. #endif /* WOLFCRYPT_HAVE_ECCSI */
  10829. #ifdef WOLFCRYPT_HAVE_SAKKE
  10830. #ifdef WOLFCRYPT_SAKKE_KMS
  10831. void bench_sakkeKeyGen(void)
  10832. {
  10833. WC_DECLARE_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10834. double start;
  10835. int i, count;
  10836. const char**desc = bench_desc_words[lng_index];
  10837. int ret;
  10838. DECLARE_MULTI_VALUE_STATS_VARS()
  10839. WC_ALLOC_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10840. /* Key Gen */
  10841. bench_stats_start(&count, &start);
  10842. do {
  10843. for (i = 0; i < genTimes; i++) {
  10844. wc_InitSakkeKey_ex(genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  10845. ret = wc_MakeSakkeKey(genKey, &gRng);
  10846. if (ret != 0) {
  10847. printf("wc_MakeSakkeKey failed: %d\n", ret);
  10848. goto exit;
  10849. }
  10850. wc_FreeSakkeKey(genKey);
  10851. RECORD_MULTI_VALUE_STATS();
  10852. }
  10853. count += i;
  10854. } while (bench_stats_check(start)
  10855. #ifdef MULTI_VALUE_STATISTICS
  10856. || runs < minimum_runs
  10857. #endif
  10858. );
  10859. bench_stats_asym_finish("SAKKE", 1024, desc[2], 0, count, start, 0);
  10860. #ifdef MULTI_VALUE_STATISTICS
  10861. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10862. #endif
  10863. exit:
  10864. WC_FREE_VAR(genKey, HEAP_HINT);
  10865. }
  10866. void bench_sakkeRskGen(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. int ret;
  10875. DECLARE_MULTI_VALUE_STATS_VARS()
  10876. WC_ALLOC_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10877. rsk = wc_ecc_new_point();
  10878. wc_InitSakkeKey_ex(genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  10879. (void)wc_MakeSakkeKey(genKey, &gRng);
  10880. /* RSK Gen */
  10881. bench_stats_start(&count, &start);
  10882. do {
  10883. for (i = 0; i < genTimes; i++) {
  10884. ret = wc_MakeSakkeRsk(genKey, id, sizeof(id), rsk);
  10885. if (ret != 0) {
  10886. printf("wc_MakeSakkeRsk failed: %d\n", ret);
  10887. goto exit;
  10888. }
  10889. RECORD_MULTI_VALUE_STATS();
  10890. }
  10891. count += i;
  10892. } while (bench_stats_check(start)
  10893. #ifdef MULTI_VALUE_STATISTICS
  10894. || runs < minimum_runs
  10895. #endif
  10896. );
  10897. bench_stats_asym_finish("SAKKE", 1024, desc[8], 0, count, start, 0);
  10898. #ifdef MULTI_VALUE_STATISTICS
  10899. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10900. #endif
  10901. wc_FreeSakkeKey(genKey);
  10902. wc_ecc_del_point(rsk);
  10903. exit:
  10904. WC_FREE_VAR(genKey, HEAP_HINT);
  10905. }
  10906. #endif
  10907. #ifdef WOLFCRYPT_SAKKE_CLIENT
  10908. void bench_sakkeValidate(void)
  10909. {
  10910. WC_DECLARE_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10911. double start;
  10912. int i, count;
  10913. const char**desc = bench_desc_words[lng_index];
  10914. ecc_point* rsk;
  10915. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10916. int valid;
  10917. int ret;
  10918. DECLARE_MULTI_VALUE_STATS_VARS()
  10919. WC_ALLOC_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10920. rsk = wc_ecc_new_point();
  10921. (void)wc_InitSakkeKey_ex(genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  10922. (void)wc_MakeSakkeKey(genKey, &gRng);
  10923. (void)wc_MakeSakkeRsk(genKey, id, sizeof(id), rsk);
  10924. (void)wc_ValidateSakkeRsk(genKey, id, sizeof(id), rsk, &valid);
  10925. /* Validation of RSK */
  10926. bench_stats_start(&count, &start);
  10927. do {
  10928. for (i = 0; i < genTimes; i++) {
  10929. ret = wc_ValidateSakkeRsk(genKey, id, sizeof(id), rsk, &valid);
  10930. if (ret != 0 || !valid) {
  10931. printf("wc_ValidateSakkeRsk failed: %d (valid=%d))\n", ret,
  10932. valid);
  10933. goto exit;
  10934. }
  10935. RECORD_MULTI_VALUE_STATS();
  10936. }
  10937. count += i;
  10938. } while (bench_stats_check(start)
  10939. #ifdef MULTI_VALUE_STATISTICS
  10940. || runs < minimum_runs
  10941. #endif
  10942. );
  10943. bench_stats_asym_finish("SAKKE", 1024, desc[11], 0, count, start, 0);
  10944. #ifdef MULTI_VALUE_STATISTICS
  10945. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10946. #endif
  10947. wc_FreeSakkeKey(genKey);
  10948. wc_ecc_del_point(rsk);
  10949. exit:
  10950. WC_FREE_VAR(genKey, HEAP_HINT);
  10951. }
  10952. void bench_sakke(void)
  10953. {
  10954. WC_DECLARE_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10955. double start;
  10956. int i, count;
  10957. const char**desc = bench_desc_words[lng_index];
  10958. ecc_point* rsk;
  10959. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10960. static const byte ssv_init[] = { 0x01, 0x23, 0x34, 0x45 };
  10961. byte ssv[sizeof(ssv_init)];
  10962. byte derSSV[sizeof(ssv)];
  10963. byte auth[257];
  10964. word16 authSz = sizeof(auth);
  10965. int ret = 0;
  10966. byte* table = NULL;
  10967. word32 len = 0;
  10968. byte* iTable = NULL;
  10969. word32 iTableLen = 0;
  10970. DECLARE_MULTI_VALUE_STATS_VARS()
  10971. WC_ALLOC_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10972. XMEMCPY(ssv, ssv_init, sizeof ssv);
  10973. rsk = wc_ecc_new_point();
  10974. (void)wc_InitSakkeKey_ex(genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  10975. (void)wc_MakeSakkeKey(genKey, &gRng);
  10976. (void)wc_MakeSakkeRsk(genKey, id, sizeof(id), rsk);
  10977. (void)wc_SetSakkeRsk(genKey, rsk, NULL, 0);
  10978. (void)wc_SetSakkeIdentity(genKey, id, sizeof(id));
  10979. /* Encapsulate */
  10980. bench_stats_start(&count, &start);
  10981. do {
  10982. for (i = 0; i < genTimes; i++) {
  10983. ret = wc_MakeSakkeEncapsulatedSSV(genKey,
  10984. WC_HASH_TYPE_SHA256,
  10985. ssv, sizeof(ssv), auth, &authSz);
  10986. if (ret != 0) {
  10987. printf("wc_MakeSakkeEncapsulatedSSV failed: %d\n", ret);
  10988. break;
  10989. }
  10990. RECORD_MULTI_VALUE_STATS();
  10991. } /* for */
  10992. count += i;
  10993. } while (bench_stats_check(start)
  10994. #ifdef MULTI_VALUE_STATISTICS
  10995. || runs < minimum_runs
  10996. #endif
  10997. );
  10998. bench_stats_asym_finish_ex("SAKKE", 1024, desc[9], "-1",
  10999. 0, count, start, 0);
  11000. #ifdef MULTI_VALUE_STATISTICS
  11001. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11002. #endif
  11003. RESET_MULTI_VALUE_STATS_VARS();
  11004. /* Derive */
  11005. bench_stats_start(&count, &start);
  11006. do {
  11007. for (i = 0; i < genTimes; i++) {
  11008. XMEMCPY(derSSV, ssv, sizeof(ssv));
  11009. ret = wc_DeriveSakkeSSV(genKey, WC_HASH_TYPE_SHA256, derSSV,
  11010. sizeof(derSSV), auth, authSz);
  11011. if (ret != 0) {
  11012. printf("wc_DeriveSakkeSSV failed: %d\n", ret);
  11013. break;
  11014. }
  11015. RECORD_MULTI_VALUE_STATS();
  11016. }
  11017. if (ret != 0) break;
  11018. count += i;
  11019. } while (bench_stats_check(start)
  11020. #ifdef MULTI_VALUE_STATISTICS
  11021. || runs < minimum_runs
  11022. #endif
  11023. );
  11024. bench_stats_asym_finish_ex("SAKKE", 1024, desc[10], "-1",
  11025. 0, count, start, 0);
  11026. #ifdef MULTI_VALUE_STATISTICS
  11027. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11028. #endif
  11029. /* Calculate Point I and generate table. */
  11030. (void)wc_MakeSakkePointI(genKey, id, sizeof(id));
  11031. iTableLen = 0;
  11032. (void)wc_GenerateSakkePointITable(genKey, NULL, &iTableLen);
  11033. if (iTableLen != 0) {
  11034. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11035. if (iTable == NULL)
  11036. WC_ALLOC_DO_ON_FAILURE();
  11037. (void)wc_GenerateSakkePointITable(genKey, iTable, &iTableLen);
  11038. }
  11039. /* Encapsulate with Point I table */
  11040. bench_stats_start(&count, &start);
  11041. do {
  11042. for (i = 0; i < genTimes; i++) {
  11043. ret = wc_MakeSakkeEncapsulatedSSV(genKey,
  11044. WC_HASH_TYPE_SHA256, ssv,
  11045. sizeof(ssv), auth, &authSz);
  11046. if (ret != 0) {
  11047. printf("wc_MakeSakkeEncapsulatedSSV failed: %d\n", ret);
  11048. break;
  11049. }
  11050. RECORD_MULTI_VALUE_STATS();
  11051. }
  11052. count += i;
  11053. } while (bench_stats_check(start)
  11054. #ifdef MULTI_VALUE_STATISTICS
  11055. || runs < minimum_runs
  11056. #endif
  11057. );
  11058. bench_stats_asym_finish_ex("SAKKE", 1024, desc[9], "-2", 0,
  11059. count, start, 0);
  11060. #ifdef MULTI_VALUE_STATISTICS
  11061. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11062. #endif
  11063. RESET_MULTI_VALUE_STATS_VARS();
  11064. (void)wc_SetSakkeRsk(genKey, rsk, table, len);
  11065. /* Derive with Point I table */
  11066. bench_stats_start(&count, &start);
  11067. do {
  11068. for (i = 0; i < genTimes; i++) {
  11069. XMEMCPY(derSSV, ssv, sizeof(ssv));
  11070. ret = wc_DeriveSakkeSSV(genKey, WC_HASH_TYPE_SHA256, derSSV,
  11071. sizeof(derSSV), auth, authSz);
  11072. if (ret != 0) {
  11073. printf("wc_DeriveSakkeSSV failed: %d\n", ret);
  11074. break;
  11075. }
  11076. RECORD_MULTI_VALUE_STATS();
  11077. }
  11078. if (ret != 0) break;
  11079. count += i;
  11080. } while (bench_stats_check(start)
  11081. #ifdef MULTI_VALUE_STATISTICS
  11082. || runs < minimum_runs
  11083. #endif
  11084. );
  11085. bench_stats_asym_finish_ex("SAKKE", 1024, desc[10], "-2", 0,
  11086. count, start, 0);
  11087. #ifdef MULTI_VALUE_STATISTICS
  11088. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11089. #endif
  11090. RESET_MULTI_VALUE_STATS_VARS();
  11091. len = 0;
  11092. (void)wc_GenerateSakkeRskTable(genKey, rsk, NULL, &len);
  11093. if (len > 0) {
  11094. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11095. if (table == NULL)
  11096. WC_ALLOC_DO_ON_FAILURE();
  11097. (void)wc_GenerateSakkeRskTable(genKey, rsk, table, &len);
  11098. }
  11099. (void)wc_SetSakkeRsk(genKey, rsk, table, len);
  11100. /* Derive with Point I table and RSK table */
  11101. bench_stats_start(&count, &start);
  11102. do {
  11103. for (i = 0; i < genTimes; i++) {
  11104. XMEMCPY(derSSV, ssv, sizeof(ssv));
  11105. ret = wc_DeriveSakkeSSV(genKey, WC_HASH_TYPE_SHA256, derSSV,
  11106. sizeof(derSSV), auth, authSz);
  11107. if (ret != 0) {
  11108. printf("wc_DeriveSakkeSSV failed: %d\n", ret);
  11109. break;
  11110. }
  11111. RECORD_MULTI_VALUE_STATS();
  11112. }
  11113. if (ret != 0) break;
  11114. count += i;
  11115. } while (bench_stats_check(start)
  11116. #ifdef MULTI_VALUE_STATISTICS
  11117. || runs < minimum_runs
  11118. #endif
  11119. );
  11120. bench_stats_asym_finish_ex("SAKKE", 1024, desc[10], "-3",
  11121. 0, count, start, 0);
  11122. #ifdef MULTI_VALUE_STATISTICS
  11123. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11124. #endif
  11125. RESET_MULTI_VALUE_STATS_VARS();
  11126. wc_ClearSakkePointITable(genKey);
  11127. /* Derive with RSK table */
  11128. bench_stats_start(&count, &start);
  11129. do {
  11130. for (i = 0; i < genTimes; i++) {
  11131. XMEMCPY(derSSV, ssv, sizeof(ssv));
  11132. ret = wc_DeriveSakkeSSV(genKey, WC_HASH_TYPE_SHA256, derSSV,
  11133. sizeof(derSSV), auth, authSz);
  11134. if (ret != 0) {
  11135. printf("wc_DeriveSakkeSSV failed: %d\n", ret);
  11136. break;
  11137. }
  11138. RECORD_MULTI_VALUE_STATS();
  11139. }
  11140. if (ret != 0) break;
  11141. count += i;
  11142. } while (bench_stats_check(start)
  11143. #ifdef MULTI_VALUE_STATISTICS
  11144. || runs < minimum_runs
  11145. #endif
  11146. );
  11147. bench_stats_asym_finish_ex("SAKKE", 1024, desc[10], "-4", 0,
  11148. count, start, 0);
  11149. #ifdef MULTI_VALUE_STATISTICS
  11150. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11151. #endif
  11152. wc_FreeSakkeKey(genKey);
  11153. wc_ecc_del_point(rsk);
  11154. exit:
  11155. if (iTable)
  11156. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11157. if (table)
  11158. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11159. WC_FREE_VAR(genKey, HEAP_HINT);
  11160. }
  11161. #endif /* WOLFCRYPT_SAKKE_CLIENT */
  11162. #endif /* WOLFCRYPT_HAVE_SAKKE */
  11163. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  11164. #ifdef HAVE_FALCON
  11165. void bench_falconKeySign(byte level)
  11166. {
  11167. int ret = 0;
  11168. falcon_key key;
  11169. double start;
  11170. int i, count;
  11171. byte sig[FALCON_MAX_SIG_SIZE];
  11172. byte msg[512];
  11173. word32 x = 0;
  11174. const char**desc = bench_desc_words[lng_index];
  11175. DECLARE_MULTI_VALUE_STATS_VARS()
  11176. ret = wc_falcon_init(&key);
  11177. if (ret != 0) {
  11178. printf("wc_falcon_init failed %d\n", ret);
  11179. return;
  11180. }
  11181. ret = wc_falcon_set_level(&key, level);
  11182. if (ret != 0) {
  11183. printf("wc_falcon_set_level failed %d\n", ret);
  11184. }
  11185. if (ret == 0) {
  11186. if (level == 1) {
  11187. ret = wc_falcon_import_private_key(bench_falcon_level1_key,
  11188. sizeof_bench_falcon_level1_key,
  11189. NULL, 0, &key);
  11190. }
  11191. else {
  11192. ret = wc_falcon_import_private_key(bench_falcon_level5_key,
  11193. sizeof_bench_falcon_level5_key,
  11194. NULL, 0, &key);
  11195. }
  11196. if (ret != 0) {
  11197. printf("wc_falcon_import_private_key failed %d\n", ret);
  11198. }
  11199. }
  11200. /* make dummy msg */
  11201. for (i = 0; i < (int)sizeof(msg); i++) {
  11202. msg[i] = (byte)i;
  11203. }
  11204. bench_stats_start(&count, &start);
  11205. do {
  11206. for (i = 0; i < agreeTimes; i++) {
  11207. if (ret == 0) {
  11208. if (level == 1) {
  11209. x = FALCON_LEVEL1_SIG_SIZE;
  11210. }
  11211. else {
  11212. x = FALCON_LEVEL5_SIG_SIZE;
  11213. }
  11214. ret = wc_falcon_sign_msg(msg, sizeof(msg), sig, &x, &key, GLOBAL_RNG);
  11215. if (ret != 0) {
  11216. printf("wc_falcon_sign_msg failed\n");
  11217. }
  11218. }
  11219. RECORD_MULTI_VALUE_STATS();
  11220. }
  11221. count += i;
  11222. } while (bench_stats_check(start)
  11223. #ifdef MULTI_VALUE_STATISTICS
  11224. || runs < minimum_runs
  11225. #endif
  11226. );
  11227. if (ret == 0) {
  11228. bench_stats_asym_finish("FALCON", level, desc[4], 0,
  11229. count, start, ret);
  11230. #ifdef MULTI_VALUE_STATISTICS
  11231. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11232. #endif
  11233. }
  11234. RESET_MULTI_VALUE_STATS_VARS();
  11235. bench_stats_start(&count, &start);
  11236. do {
  11237. for (i = 0; i < agreeTimes; i++) {
  11238. if (ret == 0) {
  11239. int verify = 0;
  11240. ret = wc_falcon_verify_msg(sig, x, msg, sizeof(msg), &verify,
  11241. &key);
  11242. if (ret != 0 || verify != 1) {
  11243. printf("wc_falcon_verify_msg failed %d, verify %d\n",
  11244. ret, verify);
  11245. ret = -1;
  11246. }
  11247. }
  11248. RECORD_MULTI_VALUE_STATS();
  11249. }
  11250. count += i;
  11251. } while (bench_stats_check(start)
  11252. #ifdef MULTI_VALUE_STATISTICS
  11253. || runs < minimum_runs
  11254. #endif
  11255. );
  11256. if (ret == 0) {
  11257. bench_stats_asym_finish("FALCON", level, desc[5],
  11258. 0, count, start, ret);
  11259. #ifdef MULTI_VALUE_STATISTICS
  11260. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11261. #endif
  11262. }
  11263. wc_falcon_free(&key);
  11264. }
  11265. #endif /* HAVE_FALCON */
  11266. #ifdef HAVE_DILITHIUM
  11267. void bench_dilithiumKeySign(byte level)
  11268. {
  11269. int ret = 0;
  11270. dilithium_key key;
  11271. double start;
  11272. int i, count;
  11273. byte sig[DILITHIUM_MAX_SIG_SIZE];
  11274. byte msg[512];
  11275. word32 x = 0;
  11276. const char**desc = bench_desc_words[lng_index];
  11277. DECLARE_MULTI_VALUE_STATS_VARS()
  11278. ret = wc_dilithium_init(&key);
  11279. if (ret != 0) {
  11280. printf("wc_dilithium_init failed %d\n", ret);
  11281. return;
  11282. }
  11283. ret = wc_dilithium_set_level(&key, level);
  11284. if (ret != 0) {
  11285. printf("wc_dilithium_set_level() failed %d\n", ret);
  11286. }
  11287. if (ret == 0) {
  11288. ret = -1;
  11289. if (level == 2) {
  11290. ret = wc_dilithium_import_private_key(bench_dilithium_level2_key,
  11291. sizeof_bench_dilithium_level2_key, NULL, 0, &key);
  11292. }
  11293. else if (level == 3) {
  11294. ret = wc_dilithium_import_private_key(bench_dilithium_level3_key,
  11295. sizeof_bench_dilithium_level3_key, NULL, 0, &key);
  11296. }
  11297. else if (level == 5) {
  11298. ret = wc_dilithium_import_private_key(bench_dilithium_level5_key,
  11299. sizeof_bench_dilithium_level5_key, NULL, 0, &key);
  11300. }
  11301. if (ret != 0) {
  11302. printf("wc_dilithium_import_private_key failed %d\n", ret);
  11303. }
  11304. }
  11305. /* make dummy msg */
  11306. for (i = 0; i < (int)sizeof(msg); i++) {
  11307. msg[i] = (byte)i;
  11308. }
  11309. bench_stats_start(&count, &start);
  11310. do {
  11311. for (i = 0; i < agreeTimes; i++) {
  11312. if (ret == 0) {
  11313. if (level == 2) {
  11314. x = DILITHIUM_LEVEL2_SIG_SIZE;
  11315. }
  11316. else if (level == 3) {
  11317. x = DILITHIUM_LEVEL3_SIG_SIZE;
  11318. }
  11319. else {
  11320. x = DILITHIUM_LEVEL5_SIG_SIZE;
  11321. }
  11322. ret = wc_dilithium_sign_msg(msg, sizeof(msg), sig, &x, &key, GLOBAL_RNG);
  11323. if (ret != 0) {
  11324. printf("wc_dilithium_sign_msg failed\n");
  11325. }
  11326. }
  11327. RECORD_MULTI_VALUE_STATS();
  11328. }
  11329. count += i;
  11330. } while (bench_stats_check(start)
  11331. #ifdef MULTI_VALUE_STATISTICS
  11332. || runs < minimum_runs
  11333. #endif
  11334. );
  11335. if (ret == 0) {
  11336. bench_stats_asym_finish("DILITHIUM", level, desc[4], 0, count, start,
  11337. ret);
  11338. #ifdef MULTI_VALUE_STATISTICS
  11339. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11340. #endif
  11341. }
  11342. RESET_MULTI_VALUE_STATS_VARS();
  11343. bench_stats_start(&count, &start);
  11344. do {
  11345. for (i = 0; i < agreeTimes; i++) {
  11346. if (ret == 0) {
  11347. int verify = 0;
  11348. ret = wc_dilithium_verify_msg(sig, x, msg, sizeof(msg),
  11349. &verify, &key);
  11350. if (ret != 0 || verify != 1) {
  11351. printf("wc_dilithium_verify_msg failed %d, verify %d\n",
  11352. ret, verify);
  11353. ret = -1;
  11354. }
  11355. }
  11356. RECORD_MULTI_VALUE_STATS();
  11357. }
  11358. count += i;
  11359. } while (bench_stats_check(start)
  11360. #ifdef MULTI_VALUE_STATISTICS
  11361. || runs < minimum_runs
  11362. #endif
  11363. );
  11364. if (ret == 0) {
  11365. bench_stats_asym_finish("DILITHIUM", level, desc[5], 0, count, start,
  11366. ret);
  11367. #ifdef MULTI_VALUE_STATISTICS
  11368. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11369. #endif
  11370. }
  11371. wc_dilithium_free(&key);
  11372. }
  11373. #endif /* HAVE_DILITHIUM */
  11374. #ifdef HAVE_SPHINCS
  11375. void bench_sphincsKeySign(byte level, byte optim)
  11376. {
  11377. int ret = 0;
  11378. sphincs_key key;
  11379. double start;
  11380. int i, count;
  11381. byte sig[SPHINCS_MAX_SIG_SIZE];
  11382. byte msg[512];
  11383. word32 x = 0;
  11384. const char**desc = bench_desc_words[lng_index];
  11385. DECLARE_MULTI_VALUE_STATS_VARS()
  11386. ret = wc_sphincs_init(&key);
  11387. if (ret != 0) {
  11388. printf("wc_sphincs_init failed %d\n", ret);
  11389. return;
  11390. }
  11391. ret = wc_sphincs_set_level_and_optim(&key, level, optim);
  11392. if (ret != 0) {
  11393. printf("wc_sphincs_set_level_and_optim() failed %d\n", ret);
  11394. }
  11395. if (ret == 0) {
  11396. ret = -1;
  11397. if ((level == 1) && (optim == FAST_VARIANT)) {
  11398. ret = wc_sphincs_import_private_key(bench_sphincs_fast_level1_key,
  11399. sizeof_bench_sphincs_fast_level1_key, NULL, 0, &key);
  11400. }
  11401. else if ((level == 3) && (optim == FAST_VARIANT)) {
  11402. ret = wc_sphincs_import_private_key(bench_sphincs_fast_level3_key,
  11403. sizeof_bench_sphincs_fast_level3_key, NULL, 0, &key);
  11404. }
  11405. else if ((level == 5) && (optim == FAST_VARIANT)) {
  11406. ret = wc_sphincs_import_private_key(bench_sphincs_fast_level5_key,
  11407. sizeof_bench_sphincs_fast_level5_key, NULL, 0, &key);
  11408. }
  11409. else if ((level == 1) && (optim == SMALL_VARIANT)) {
  11410. ret = wc_sphincs_import_private_key(
  11411. bench_sphincs_small_level1_key,
  11412. sizeof_bench_sphincs_small_level1_key, NULL, 0, &key);
  11413. }
  11414. else if ((level == 3) && (optim == SMALL_VARIANT)) {
  11415. ret = wc_sphincs_import_private_key(
  11416. bench_sphincs_small_level3_key,
  11417. sizeof_bench_sphincs_small_level3_key, NULL, 0, &key);
  11418. }
  11419. else if ((level == 5) && (optim == SMALL_VARIANT)) {
  11420. ret = wc_sphincs_import_private_key(
  11421. bench_sphincs_small_level5_key,
  11422. sizeof_bench_sphincs_small_level5_key, NULL, 0, &key);
  11423. }
  11424. if (ret != 0) {
  11425. printf("wc_sphincs_import_private_key failed %d\n", ret);
  11426. }
  11427. }
  11428. /* make dummy msg */
  11429. for (i = 0; i < (int)sizeof(msg); i++) {
  11430. msg[i] = (byte)i;
  11431. }
  11432. bench_stats_start(&count, &start);
  11433. do {
  11434. for (i = 0; i < agreeTimes; i++) {
  11435. if (ret == 0) {
  11436. if ((level == 1) && (optim == FAST_VARIANT)) {
  11437. x = SPHINCS_FAST_LEVEL1_SIG_SIZE;
  11438. }
  11439. else if ((level == 3) && (optim == FAST_VARIANT)) {
  11440. x = SPHINCS_FAST_LEVEL3_SIG_SIZE;
  11441. }
  11442. else if ((level == 5) && (optim == FAST_VARIANT)) {
  11443. x = SPHINCS_FAST_LEVEL5_SIG_SIZE;
  11444. }
  11445. else if ((level == 1) && (optim == SMALL_VARIANT)) {
  11446. x = SPHINCS_SMALL_LEVEL1_SIG_SIZE;
  11447. }
  11448. else if ((level == 3) && (optim == SMALL_VARIANT)) {
  11449. x = SPHINCS_SMALL_LEVEL3_SIG_SIZE;
  11450. }
  11451. else if ((level == 5) && (optim == SMALL_VARIANT)) {
  11452. x = SPHINCS_SMALL_LEVEL5_SIG_SIZE;
  11453. }
  11454. ret = wc_sphincs_sign_msg(msg, sizeof(msg), sig, &x, &key, GLOBAL_RNG);
  11455. if (ret != 0) {
  11456. printf("wc_sphincs_sign_msg failed\n");
  11457. }
  11458. }
  11459. RECORD_MULTI_VALUE_STATS();
  11460. }
  11461. count += i;
  11462. } while (bench_stats_check(start)
  11463. #ifdef MULTI_VALUE_STATISTICS
  11464. || runs < minimum_runs
  11465. #endif
  11466. );
  11467. if (ret == 0) {
  11468. if (optim == FAST_VARIANT) {
  11469. bench_stats_asym_finish("SPHINCS-FAST", level, desc[4], 0, count,
  11470. start, ret);
  11471. }
  11472. else {
  11473. bench_stats_asym_finish("SPHINCS-SMALL", level, desc[4], 0, count,
  11474. start, ret);
  11475. }
  11476. #ifdef MULTI_VALUE_STATISTICS
  11477. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11478. #endif
  11479. }
  11480. RESET_MULTI_VALUE_STATS_VARS();
  11481. bench_stats_start(&count, &start);
  11482. do {
  11483. for (i = 0; i < agreeTimes; i++) {
  11484. if (ret == 0) {
  11485. int verify = 0;
  11486. ret = wc_sphincs_verify_msg(sig, x, msg, sizeof(msg), &verify,
  11487. &key);
  11488. if (ret != 0 || verify != 1) {
  11489. printf("wc_sphincs_verify_msg failed %d, verify %d\n",
  11490. ret, verify);
  11491. ret = -1;
  11492. }
  11493. }
  11494. RECORD_MULTI_VALUE_STATS();
  11495. }
  11496. count += i;
  11497. } while (bench_stats_check(start)
  11498. #ifdef MULTI_VALUE_STATISTICS
  11499. || runs < minimum_runs
  11500. #endif
  11501. );
  11502. if (ret == 0) {
  11503. if (optim == FAST_VARIANT) {
  11504. bench_stats_asym_finish("SPHINCS-FAST", level, desc[5], 0, count,
  11505. start, ret);
  11506. }
  11507. else {
  11508. bench_stats_asym_finish("SPHINCS-SMALL", level, desc[5], 0, count,
  11509. start, ret);
  11510. }
  11511. #ifdef MULTI_VALUE_STATISTICS
  11512. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11513. #endif
  11514. }
  11515. wc_sphincs_free(&key);
  11516. }
  11517. #endif /* HAVE_SPHINCS */
  11518. #endif /* HAVE_PQC */
  11519. #if defined(_WIN32) && !defined(INTIME_RTOS)
  11520. #define WIN32_LEAN_AND_MEAN
  11521. #include <windows.h>
  11522. double current_time(int reset)
  11523. {
  11524. static int init = 0;
  11525. static LARGE_INTEGER freq;
  11526. LARGE_INTEGER count;
  11527. (void)reset;
  11528. if (!init) {
  11529. QueryPerformanceFrequency(&freq);
  11530. init = 1;
  11531. }
  11532. QueryPerformanceCounter(&count);
  11533. #ifdef BENCH_MICROSECOND
  11534. return ((double)count.QuadPart * 1000000) / freq.QuadPart;
  11535. #else
  11536. return (double)count.QuadPart / freq.QuadPart;
  11537. #endif
  11538. }
  11539. #elif defined MICROCHIP_PIC32
  11540. #if defined(WOLFSSL_MICROCHIP_PIC32MZ)
  11541. #define CLOCK 80000000.0
  11542. #else
  11543. #define CLOCK 40000000.0
  11544. #endif
  11545. extern void WriteCoreTimer(word32 t);
  11546. extern word32 ReadCoreTimer(void);
  11547. double current_time(int reset)
  11548. {
  11549. unsigned int ns;
  11550. if (reset) {
  11551. WriteCoreTimer(0);
  11552. }
  11553. /* get timer in ns */
  11554. ns = ReadCoreTimer();
  11555. /* return seconds as a double */
  11556. return ( ns / CLOCK * 2.0);
  11557. }
  11558. #elif defined(WOLFSSL_IAR_ARM_TIME) || defined (WOLFSSL_MDK_ARM) || \
  11559. defined(WOLFSSL_USER_CURRTIME) || defined(WOLFSSL_CURRTIME_REMAP)
  11560. /* declared above at line 239 */
  11561. /* extern double current_time(int reset); */
  11562. #elif defined(FREERTOS)
  11563. #ifdef PLATFORMIO
  11564. #include <freertos/FreeRTOS.h>
  11565. #include <freertos/task.h>
  11566. #else
  11567. #include "task.h"
  11568. #endif
  11569. #if defined(WOLFSSL_ESPIDF)
  11570. /* prototype definition */
  11571. int construct_argv();
  11572. extern char* __argv[22];
  11573. /* current_time(reset)
  11574. *
  11575. * Benchmark passage of time, in fractional seconds.
  11576. * [reset] is non zero to adjust timer or counter to zero
  11577. *
  11578. * Use care when repeatedly calling calling. See implementation. */
  11579. double current_time(int reset)
  11580. {
  11581. double ret;
  11582. #if ESP_IDF_VERSION_MAJOR >= 4
  11583. TickType_t tickCount; /* typically 32 bit, local FreeRTOS ticks */
  11584. #else
  11585. portTickType tickCount;
  11586. #endif
  11587. #if defined(__XTENSA__)
  11588. (void)reset;
  11589. if (reset) {
  11590. /* TODO: Determine a mechanism for reset that does not interfere
  11591. * with freeRTOS tick. Using this code for Xtensa appears to cause
  11592. * RTOS tick timer to stick. See "last_tickCount unchanged".
  11593. ESP_LOGW(TAG, "Current_time() reset!");
  11594. portTICK_TYPE_ENTER_CRITICAL();
  11595. {
  11596. esp_cpu_set_cycle_count((esp_cpu_cycle_count_t)0);
  11597. _esp_cpu_count_last = xthal_get_ccount();
  11598. _esp_cpu_count_last = esp_cpu_get_cycle_count();
  11599. }
  11600. portTICK_TYPE_EXIT_CRITICAL();
  11601. */
  11602. }
  11603. #else
  11604. /* Only reset the CPU counter for RISC-V */
  11605. if (reset) {
  11606. ESP_LOGV(TAG, "current_time() reset!");
  11607. /* TODO: why does Espressif esp_cpu_get_cycle_count() cause
  11608. * unexpected rollovers in return values for Xtensa but not RISC-V?
  11609. * See also esp_get_cycle_count_ex() */
  11610. #ifdef __XTENSA__
  11611. _esp_cpu_count_last = xthal_get_ccount();
  11612. #else
  11613. esp_cpu_set_cycle_count((esp_cpu_cycle_count_t)0);
  11614. _esp_cpu_count_last = esp_cpu_get_cycle_count();
  11615. #endif
  11616. }
  11617. #endif
  11618. /* tick count == ms, if configTICK_RATE_HZ is set to 1000 */
  11619. tickCount = xTaskGetTickCount(); /* RTOS ticks, not CPU cycles!
  11620. The count of ticks since vTaskStartScheduler was called,
  11621. typiclly in app_startup.c */
  11622. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  11623. ESP_LOGV(TAG, "tickCount = %d", tickCount);
  11624. if (tickCount == last_tickCount) {
  11625. ESP_LOGW(TAG, "last_tickCount unchanged? %d", tickCount);
  11626. }
  11627. if (tickCount < last_tickCount) {
  11628. ESP_LOGW(TAG, "last_tickCount overflow?");
  11629. }
  11630. #endif
  11631. if (reset) {
  11632. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  11633. ESP_LOGW(TAG, "Assign last_tickCount = %d", tickCount);
  11634. #endif
  11635. last_tickCount = tickCount;
  11636. }
  11637. else {
  11638. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  11639. ESP_LOGV(TAG, "No Reset last_tickCount = %d", tickCount);
  11640. #endif
  11641. }
  11642. #if defined(configTICK_RATE_HZ) && defined(CONFIG_FREERTOS_HZ)
  11643. ret = (double)tickCount / configTICK_RATE_HZ;
  11644. #else
  11645. ESP_LOGW(TAG, "Warning: configTICK_RATE_HZ not defined,"
  11646. "assuming 1000 Hz.");
  11647. ret = (double)(tickCount / 1000.0);
  11648. #endif /* configTICK_RATE_HZ */
  11649. return ret;
  11650. } /* current_time */
  11651. #else
  11652. /* current_time(reset)
  11653. *
  11654. * Benchmark passage of time, in fractional seconds.
  11655. * [reset] is non zero to adjust timer or counter to zero
  11656. *
  11657. * Use care when repeatedly calling calling. See implementation. */
  11658. double current_time(int reset)
  11659. {
  11660. portTickType tickCount;
  11661. /* tick count == ms, if configTICK_RATE_HZ is set to 1000 */
  11662. tickCount = xTaskGetTickCount();
  11663. return (double)tickCount / 1000;
  11664. }
  11665. #endif
  11666. #elif defined (WOLFSSL_TIRTOS)
  11667. extern double current_time(int reset);
  11668. #elif defined(FREESCALE_MQX)
  11669. double current_time(int reset)
  11670. {
  11671. TIME_STRUCT tv;
  11672. _time_get(&tv);
  11673. return (double)tv.SECONDS + (double)tv.MILLISECONDS / 1000;
  11674. }
  11675. #elif defined(FREESCALE_KSDK_BM)
  11676. double current_time(int reset)
  11677. {
  11678. return (double)OSA_TimeGetMsec() / 1000;
  11679. }
  11680. #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2)
  11681. double current_time(int reset)
  11682. {
  11683. (void)reset;
  11684. return (double)osKernelGetTickCount() / 1000.0;
  11685. }
  11686. #elif defined(WOLFSSL_EMBOS)
  11687. #include "RTOS.h"
  11688. double current_time(int reset)
  11689. {
  11690. double time_now;
  11691. double current_s = OS_GetTime() / 1000.0;
  11692. double current_us = OS_GetTime_us() / MILLION_VALUE;
  11693. time_now = (double)( current_s + current_us);
  11694. (void) reset;
  11695. return time_now;
  11696. }
  11697. #elif defined(WOLFSSL_SGX)
  11698. double current_time(int reset);
  11699. #elif defined(WOLFSSL_DEOS)
  11700. double current_time(int reset)
  11701. {
  11702. const uint32_t systemTickTimeInHz
  11703. = 1000000 / systemTickInMicroseconds();
  11704. const volatile uint32_t *systemTickPtr = systemTickPointer();
  11705. (void)reset;
  11706. return (double) *systemTickPtr/systemTickTimeInHz;
  11707. }
  11708. #elif defined(MICRIUM)
  11709. double current_time(int reset)
  11710. {
  11711. #if (OS_VERSION < 50000)
  11712. CPU_ERR err;
  11713. (void)reset;
  11714. return (double) CPU_TS_Get32()/CPU_TS_TmrFreqGet(&err);
  11715. #else
  11716. RTOS_ERR err;
  11717. double ret = 0;
  11718. OS_TICK tick = OSTimeGet(&err);
  11719. OS_RATE_HZ rate = OSTimeTickRateHzGet(&err);
  11720. (void)reset;
  11721. if (RTOS_ERR_CODE_GET(err) == RTOS_ERR_NONE) {
  11722. ret = ((double)tick)/rate;
  11723. }
  11724. return ret;
  11725. #endif
  11726. }
  11727. #elif defined(WOLFSSL_ZEPHYR)
  11728. #include <time.h>
  11729. double current_time(int reset)
  11730. {
  11731. int64_t t;
  11732. (void)reset;
  11733. #if defined(CONFIG_ARCH_POSIX)
  11734. k_cpu_idle();
  11735. #endif
  11736. t = k_uptime_get(); /* returns current uptime in milliseconds */
  11737. return (double)(t / 1000);
  11738. }
  11739. #elif defined(WOLFSSL_NETBURNER)
  11740. #include <predef.h>
  11741. #include <utils.h>
  11742. #include <constants.h>
  11743. double current_time(int reset)
  11744. {
  11745. DWORD ticks = TimeTick; /* ticks since system start */
  11746. (void)reset;
  11747. return (double) ticks/TICKS_PER_SECOND;
  11748. }
  11749. #elif defined(THREADX)
  11750. #include "tx_api.h"
  11751. double current_time(int reset)
  11752. {
  11753. (void)reset;
  11754. return (double) tx_time_get() / TX_TIMER_TICKS_PER_SECOND;
  11755. }
  11756. #elif defined(WOLFSSL_XILINX)
  11757. #ifdef XPAR_VERSAL_CIPS_0_PSPMC_0_PSV_CORTEXA72_0_TIMESTAMP_CLK_FREQ
  11758. #define COUNTS_PER_SECOND \
  11759. XPAR_VERSAL_CIPS_0_PSPMC_0_PSV_CORTEXA72_0_TIMESTAMP_CLK_FREQ
  11760. #else
  11761. #define COUNTS_PER_SECOND \
  11762. XPAR_CPU_CORTEXA53_0_TIMESTAMP_CLK_FREQ
  11763. #endif
  11764. double current_time(int reset)
  11765. {
  11766. double timer;
  11767. uint64_t cntPct = 0;
  11768. asm volatile("mrs %0, CNTPCT_EL0" : "=r" (cntPct));
  11769. /* Convert to milliseconds */
  11770. timer = (double)(cntPct / (COUNTS_PER_SECOND / 1000));
  11771. /* Convert to seconds.millisecond */
  11772. timer /= 1000;
  11773. return timer;
  11774. }
  11775. #elif defined(LINUX_RUSAGE_UTIME)
  11776. #include <sys/time.h>
  11777. #include <sys/resource.h>
  11778. static struct rusage base_rusage;
  11779. static struct rusage cur_rusage;
  11780. double current_time(int reset)
  11781. {
  11782. struct rusage rusage;
  11783. (void)reset;
  11784. LIBCALL_CHECK_RET(getrusage(RUSAGE_SELF, &rusage));
  11785. if (reset)
  11786. base_rusage = rusage;
  11787. else
  11788. cur_rusage = rusage;
  11789. /* only consider user time, as system time is host-related overhead
  11790. * outside wolfcrypt.
  11791. */
  11792. return (double)rusage.ru_utime.tv_sec +
  11793. (double)rusage.ru_utime.tv_usec / MILLION_VALUE;
  11794. }
  11795. static void check_for_excessive_stime(const char *desc,
  11796. const char *desc_extra)
  11797. {
  11798. double start_utime = (double)base_rusage.ru_utime.tv_sec +
  11799. (double)base_rusage.ru_utime.tv_usec / MILLION_VALUE;
  11800. double start_stime = (double)base_rusage.ru_stime.tv_sec +
  11801. (double)base_rusage.ru_stime.tv_usec / MILLION_VALUE;
  11802. double cur_utime = (double)cur_rusage.ru_utime.tv_sec +
  11803. (double)cur_rusage.ru_utime.tv_usec / MILLION_VALUE;
  11804. double cur_stime = (double)cur_rusage.ru_stime.tv_sec +
  11805. (double)cur_rusage.ru_stime.tv_usec / MILLION_VALUE;
  11806. double stime_utime_ratio =
  11807. (cur_stime - start_stime) / (cur_utime - start_utime);
  11808. if (stime_utime_ratio > .1)
  11809. printf("%swarning, "
  11810. "excessive system time ratio for %s%s (" FLT_FMT_PREC "%%).\n",
  11811. err_prefix, desc, desc_extra,
  11812. FLT_FMT_PREC_ARGS(3, stime_utime_ratio * 100.0));
  11813. }
  11814. #elif defined(WOLFSSL_LINUXKM)
  11815. double current_time(int reset)
  11816. {
  11817. (void)reset;
  11818. u64 ns = ktime_get_ns();
  11819. return (double)ns / 1000000000.0;
  11820. }
  11821. #else
  11822. #include <sys/time.h>
  11823. double current_time(int reset)
  11824. {
  11825. struct timespec tv;
  11826. (void)reset;
  11827. LIBCALL_CHECK_RET(clock_gettime(CLOCK_REALTIME, &tv));
  11828. #ifdef BENCH_MICROSECOND
  11829. return (double)tv.tv_sec * 1000000 + (double)tv.tv_nsec / 1000;
  11830. #else
  11831. return (double)tv.tv_sec + (double)tv.tv_nsec / 1000000000;
  11832. #endif
  11833. }
  11834. #endif /* _WIN32 */
  11835. #if defined(HAVE_GET_CYCLES)
  11836. #if defined(WOLFSSL_ESPIDF)
  11837. /* Generic CPU cycle counter for either Xtensa or RISC-V */
  11838. static WC_INLINE word64 esp_get_cpu_benchmark_cycles(void)
  11839. {
  11840. /* Reminder for long duration between calls with
  11841. * multiple overflows will not be detected. */
  11842. return esp_get_cycle_count_ex();
  11843. }
  11844. /* implement other architectures here */
  11845. #else
  11846. static WC_INLINE word64 get_intel_cycles(void)
  11847. {
  11848. unsigned int lo_c, hi_c;
  11849. __asm__ __volatile__ (
  11850. "cpuid\n\t"
  11851. "rdtsc"
  11852. : "=a"(lo_c), "=d"(hi_c) /* out */
  11853. : "a"(0) /* in */
  11854. : "%ebx", "%ecx"); /* clobber */
  11855. return ((word64)lo_c) | (((word64)hi_c) << 32);
  11856. }
  11857. #endif
  11858. #endif /* HAVE_GET_CYCLES */
  11859. void benchmark_configure(word32 block_size)
  11860. {
  11861. /* must be greater than 0 */
  11862. if (block_size > 0) {
  11863. numBlocks = (int)((word32)numBlocks * bench_size / block_size);
  11864. bench_size = block_size;
  11865. }
  11866. }
  11867. #ifndef NO_MAIN_DRIVER
  11868. #ifndef MAIN_NO_ARGS
  11869. #ifndef WOLFSSL_BENCHMARK_ALL
  11870. /* Display the algorithm string and keep to 80 characters per line.
  11871. *
  11872. * str Algorithm string to print.
  11873. * line Length of line used so far.
  11874. */
  11875. #ifndef BENCH_MAX_LINE
  11876. #define BENCH_MAX_LINE 80
  11877. #endif
  11878. static void print_alg(const char* str, int* line)
  11879. {
  11880. const char* const ident = " ";
  11881. if (*line == 0) {
  11882. printf("%s", ident);
  11883. *line = (int)XSTRLEN(ident);
  11884. }
  11885. printf(" %s", str);
  11886. *line += (int)XSTRLEN(str) + 1;
  11887. if (*line > BENCH_MAX_LINE) {
  11888. printf("\n");
  11889. *line = 0;
  11890. }
  11891. }
  11892. #endif /* WOLFSSL_BENCHMARK_ALL */
  11893. /* Display the usage options of the benchmark program. */
  11894. static void Usage(void)
  11895. {
  11896. int e = 0;
  11897. #ifndef WOLFSSL_BENCHMARK_ALL
  11898. int i;
  11899. int line;
  11900. #endif
  11901. printf("benchmark\n");
  11902. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -? */
  11903. printf("%s", bench_Usage_msg1[lng_index][e++]); /* English / Japanese */
  11904. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -csv */
  11905. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -base10 */
  11906. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  11907. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -no_aad */
  11908. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -aad_size */
  11909. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -all_aad */
  11910. #else
  11911. e += 3;
  11912. #endif
  11913. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -dgst_full */
  11914. #ifndef NO_RSA
  11915. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -ras_sign */
  11916. #ifdef WOLFSSL_KEY_GEN
  11917. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -rsa-sz */
  11918. #endif
  11919. e++;
  11920. #else
  11921. e += 2;
  11922. #endif
  11923. #if !defined(NO_DH) && defined(HAVE_FFDHE_2048)
  11924. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -ffdhe2048 */
  11925. #endif
  11926. e++;
  11927. #if !defined(NO_DH) && defined(HAVE_FFDHE_3072)
  11928. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -ffdhe3072 */
  11929. #endif
  11930. e++;
  11931. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  11932. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -p256 */
  11933. #endif
  11934. e++;
  11935. #if defined(HAVE_ECC) && defined(HAVE_ECC384)
  11936. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -p384 */
  11937. #endif
  11938. e++;
  11939. #if defined(HAVE_ECC) && defined(HAVE_ECC521)
  11940. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -p521 */
  11941. #endif
  11942. e++;
  11943. #if defined(HAVE_ECC)
  11944. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -ecc-all */
  11945. #endif
  11946. e++;
  11947. #ifndef WOLFSSL_BENCHMARK_ALL
  11948. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -<alg> */
  11949. line = 0;
  11950. for (i=0; bench_cipher_opt[i].str != NULL; i++)
  11951. print_alg(bench_cipher_opt[i].str, &line);
  11952. for (i=0; bench_digest_opt[i].str != NULL; i++)
  11953. print_alg(bench_digest_opt[i].str, &line);
  11954. for (i=0; bench_mac_opt[i].str != NULL; i++)
  11955. print_alg(bench_mac_opt[i].str, &line);
  11956. for (i=0; bench_kdf_opt[i].str != NULL; i++)
  11957. print_alg(bench_kdf_opt[i].str, &line);
  11958. for (i=0; bench_asym_opt[i].str != NULL; i++)
  11959. print_alg(bench_asym_opt[i].str, &line);
  11960. for (i=0; bench_other_opt[i].str != NULL; i++)
  11961. print_alg(bench_other_opt[i].str, &line);
  11962. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  11963. for (i=0; bench_pq_asym_opt[i].str != NULL; i++)
  11964. print_alg(bench_pq_asym_opt[i].str, &line);
  11965. #if defined(HAVE_LIBOQS) && defined(HAVE_SPHINCS)
  11966. for (i=0; bench_pq_asym_opt2[i].str != NULL; i++)
  11967. print_alg(bench_pq_asym_opt2[i].str, &line);
  11968. #endif /* HAVE_LIBOQS && HAVE_SPHINCS */
  11969. #endif /* HAVE_PQC */
  11970. #if defined(BENCH_PQ_STATEFUL_HBS)
  11971. for (i=0; bench_pq_hash_sig_opt[i].str != NULL; i++)
  11972. print_alg(bench_pq_hash_sig_opt[i].str, &line);
  11973. #endif /* BENCH_PQ_STATEFUL_HBS */
  11974. printf("\n");
  11975. #endif /* !WOLFSSL_BENCHMARK_ALL */
  11976. e++;
  11977. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -lng */
  11978. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option <num> */
  11979. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -blocks <num> */
  11980. #ifdef WC_ENABLE_BENCH_THREADING
  11981. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -threads <num> */
  11982. #endif
  11983. e++;
  11984. #ifdef WC_BENCH_TRACK_STATS
  11985. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -print */
  11986. #endif
  11987. e++;
  11988. #ifndef NO_FILESYSTEM
  11989. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -hash_input */
  11990. #endif
  11991. e++;
  11992. #ifndef NO_FILESYSTEM
  11993. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -cipher_input */
  11994. #endif
  11995. #ifdef MULTI_VALUE_STATISTICS
  11996. e++;
  11997. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -min_runs */
  11998. #endif
  11999. }
  12000. /* Match the command line argument with the string.
  12001. *
  12002. * arg Command line argument.
  12003. * str String to check for.
  12004. * return 1 if the command line argument matches the string, 0 otherwise.
  12005. */
  12006. static int string_matches(const char* arg, const char* str)
  12007. {
  12008. return XSTRCMP(arg, str) == 0;
  12009. }
  12010. #endif /* MAIN_NO_ARGS */
  12011. /*
  12012. ** ----------------------------------------------------------------------------
  12013. ** determine how the benchmarks are called, the function name varies:
  12014. ** ----------------------------------------------------------------------------
  12015. */
  12016. #if !defined(NO_MAIN_DRIVER) && !defined(NO_MAIN_FUNCTION)
  12017. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  12018. /* for some environments, we'll call a function wolf_benchmark_task: */
  12019. int wolf_benchmark_task(void)
  12020. #elif defined(MAIN_NO_ARGS)
  12021. /* otherwise we'll use main() with no arguments as desired: */
  12022. int main()
  12023. #else
  12024. /* else we'll be calling main with default arg parameters */
  12025. int main(int argc, char** argv)
  12026. #endif
  12027. {
  12028. /* Code for main() or wolf_benchmark_task() */
  12029. #ifdef WOLFSSL_ESPIDF
  12030. int argc = construct_argv();
  12031. char** argv = (char**)__argv;
  12032. #elif defined(MAIN_NO_ARGS)
  12033. int argc = 0;
  12034. char** argv = NULL;
  12035. #endif
  12036. return wolfcrypt_benchmark_main(argc, argv);
  12037. }
  12038. #endif /* !NO_MAIN_DRIVER && !NO_MAIN_FUNCTION */
  12039. int wolfcrypt_benchmark_main(int argc, char** argv)
  12040. {
  12041. int ret = 0;
  12042. #ifndef MAIN_NO_ARGS
  12043. int optMatched;
  12044. #ifndef WOLFSSL_BENCHMARK_ALL
  12045. int i;
  12046. #endif
  12047. #endif
  12048. benchmark_static_init(1);
  12049. printf("%s------------------------------------------------------------------------------\n",
  12050. info_prefix);
  12051. printf("%s wolfSSL version %s\n", info_prefix, LIBWOLFSSL_VERSION_STRING);
  12052. printf("%s------------------------------------------------------------------------------\n",
  12053. info_prefix);
  12054. #ifndef MAIN_NO_ARGS
  12055. while (argc > 1) {
  12056. if (string_matches(argv[1], "-?")) {
  12057. if (--argc > 1) {
  12058. lng_index = XATOI((++argv)[1]);
  12059. if (lng_index<0 || lng_index>1) {
  12060. lng_index = 0;
  12061. }
  12062. }
  12063. Usage();
  12064. return 0;
  12065. }
  12066. else if (string_matches(argv[1], "-lng")) {
  12067. argc--;
  12068. argv++;
  12069. if (argc > 1) {
  12070. lng_index = XATOI(argv[1]);
  12071. if (lng_index<0 || lng_index>1) {
  12072. printf("invalid number(%d) is specified. [<num> :0-1]\n",
  12073. lng_index);
  12074. lng_index = 0;
  12075. }
  12076. }
  12077. }
  12078. else if (string_matches(argv[1], "-base10"))
  12079. base2 = 0;
  12080. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  12081. else if (string_matches(argv[1], "-no_aad"))
  12082. aes_aad_options = AAD_SIZE_ZERO;
  12083. else if (string_matches(argv[1], "-all_aad"))
  12084. aes_aad_options |= AAD_SIZE_ZERO | AAD_SIZE_DEFAULT;
  12085. else if (string_matches(argv[1], "-aad_size")) {
  12086. argc--;
  12087. argv++;
  12088. if (argc > 1) {
  12089. aes_aad_size = (word32)XATOI(argv[1]);
  12090. aes_aad_options |= AAD_SIZE_CUSTOM;
  12091. }
  12092. }
  12093. #endif
  12094. else if (string_matches(argv[1], "-dgst_full"))
  12095. digest_stream = 0;
  12096. else if (string_matches(argv[1], "-enc_only"))
  12097. encrypt_only = 1;
  12098. #ifndef NO_RSA
  12099. else if (string_matches(argv[1], "-rsa_sign"))
  12100. rsa_sign_verify = 1;
  12101. #endif
  12102. #if !defined(NO_DH) && defined(HAVE_FFDHE_2048)
  12103. else if (string_matches(argv[1], "-ffdhe2048"))
  12104. use_ffdhe = 2048;
  12105. #endif
  12106. #if !defined(NO_DH) && defined(HAVE_FFDHE_3072)
  12107. else if (string_matches(argv[1], "-ffdhe3072"))
  12108. use_ffdhe = 3072;
  12109. #endif
  12110. #if !defined(NO_DH) && defined(HAVE_FFDHE_4096)
  12111. else if (string_matches(argv[1], "-ffdhe4096"))
  12112. use_ffdhe = 4096;
  12113. #endif
  12114. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  12115. else if (string_matches(argv[1], "-p256"))
  12116. bench_asym_algs |= BENCH_ECC_P256;
  12117. #endif
  12118. #if defined(HAVE_ECC) && defined(HAVE_ECC384)
  12119. else if (string_matches(argv[1], "-p384"))
  12120. bench_asym_algs |= BENCH_ECC_P384;
  12121. #endif
  12122. #if defined(HAVE_ECC) && defined(HAVE_ECC521)
  12123. else if (string_matches(argv[1], "-p521"))
  12124. bench_asym_algs |= BENCH_ECC_P521;
  12125. #endif
  12126. #ifdef BENCH_ASYM
  12127. else if (string_matches(argv[1], "-csv")) {
  12128. csv_format = 1;
  12129. }
  12130. #endif
  12131. #ifdef WC_ENABLE_BENCH_THREADING
  12132. else if (string_matches(argv[1], "-threads")) {
  12133. argc--;
  12134. argv++;
  12135. if (argc > 1) {
  12136. g_threadCount = XATOI(argv[1]);
  12137. if (g_threadCount < 1 || lng_index > 128){
  12138. printf("invalid number(%d) is specified. [<num> :1-128]\n",
  12139. g_threadCount);
  12140. g_threadCount = 0;
  12141. }
  12142. }
  12143. }
  12144. #endif
  12145. #ifdef WC_BENCH_TRACK_STATS
  12146. else if (string_matches(argv[1], "-print")) {
  12147. gPrintStats = 1;
  12148. }
  12149. #endif
  12150. else if (string_matches(argv[1], "-blocks")) {
  12151. argc--;
  12152. argv++;
  12153. if (argc > 1)
  12154. numBlocks = XATOI(argv[1]);
  12155. }
  12156. #ifndef NO_FILESYSTEM
  12157. else if (string_matches(argv[1], "-hash_input")) {
  12158. argc--;
  12159. argv++;
  12160. if (argc > 1)
  12161. hash_input = argv[1];
  12162. }
  12163. else if (string_matches(argv[1], "-cipher_input")) {
  12164. argc--;
  12165. argv++;
  12166. if (argc > 1)
  12167. cipher_input = argv[1];
  12168. }
  12169. #endif
  12170. #ifdef MULTI_VALUE_STATISTICS
  12171. else if (string_matches(argv[1], "-min_runs")) {
  12172. argc--;
  12173. argv++;
  12174. if (argc > 1) {
  12175. minimum_runs = XATOI(argv[1]);
  12176. }
  12177. }
  12178. #endif
  12179. else if (argv[1][0] == '-') {
  12180. optMatched = 0;
  12181. #ifndef WOLFSSL_BENCHMARK_ALL
  12182. /* Check known algorithm choosing command line options. */
  12183. /* Known cipher algorithms */
  12184. for (i=0; !optMatched && bench_cipher_opt[i].str != NULL; i++) {
  12185. if (string_matches(argv[1], bench_cipher_opt[i].str)) {
  12186. bench_cipher_algs |= bench_cipher_opt[i].val;
  12187. bench_all = 0;
  12188. optMatched = 1;
  12189. }
  12190. }
  12191. /* Known digest algorithms */
  12192. for (i=0; !optMatched && bench_digest_opt[i].str != NULL; i++) {
  12193. if (string_matches(argv[1], bench_digest_opt[i].str)) {
  12194. bench_digest_algs |= bench_digest_opt[i].val;
  12195. bench_all = 0;
  12196. optMatched = 1;
  12197. }
  12198. }
  12199. /* Known MAC algorithms */
  12200. for (i=0; !optMatched && bench_mac_opt[i].str != NULL; i++) {
  12201. if (string_matches(argv[1], bench_mac_opt[i].str)) {
  12202. bench_mac_algs |= bench_mac_opt[i].val;
  12203. bench_all = 0;
  12204. optMatched = 1;
  12205. }
  12206. }
  12207. /* Known KDF algorithms */
  12208. for (i=0; !optMatched && bench_kdf_opt[i].str != NULL; i++) {
  12209. if (string_matches(argv[1], bench_kdf_opt[i].str)) {
  12210. bench_kdf_algs |= bench_kdf_opt[i].val;
  12211. bench_all = 0;
  12212. optMatched = 1;
  12213. }
  12214. }
  12215. /* Known asymmetric algorithms */
  12216. for (i=0; !optMatched && bench_asym_opt[i].str != NULL; i++) {
  12217. if (string_matches(argv[1], bench_asym_opt[i].str)) {
  12218. bench_asym_algs |= bench_asym_opt[i].val;
  12219. bench_all = 0;
  12220. optMatched = 1;
  12221. }
  12222. }
  12223. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  12224. /* Known asymmetric post-quantum algorithms */
  12225. for (i=0; !optMatched && bench_pq_asym_opt[i].str != NULL; i++) {
  12226. if (string_matches(argv[1], bench_pq_asym_opt[i].str)) {
  12227. bench_pq_asym_algs |= bench_pq_asym_opt[i].val;
  12228. bench_all = 0;
  12229. optMatched = 1;
  12230. }
  12231. }
  12232. #ifdef HAVE_SPHINCS
  12233. /* Both bench_pq_asym_opt and bench_pq_asym_opt2 are looking for
  12234. * -pq, so we need to do a special case for -pq since optMatched
  12235. * was set to 1 just above. */
  12236. if ((bench_pq_asym_opt[0].str != NULL) &&
  12237. string_matches(argv[1], bench_pq_asym_opt[0].str))
  12238. {
  12239. bench_pq_asym_algs2 |= bench_pq_asym_opt2[0].val;
  12240. bench_all = 0;
  12241. optMatched = 1;
  12242. }
  12243. for (i=1; !optMatched && bench_pq_asym_opt2[i].str != NULL; i++) {
  12244. if (string_matches(argv[1], bench_pq_asym_opt2[i].str)) {
  12245. bench_pq_asym_algs2 |= bench_pq_asym_opt2[i].val;
  12246. bench_all = 0;
  12247. optMatched = 1;
  12248. }
  12249. }
  12250. #endif
  12251. #endif /* HAVE_PQC */
  12252. /* Other known cryptographic algorithms */
  12253. for (i=0; !optMatched && bench_other_opt[i].str != NULL; i++) {
  12254. if (string_matches(argv[1], bench_other_opt[i].str)) {
  12255. bench_other_algs |= bench_other_opt[i].val;
  12256. bench_all = 0;
  12257. optMatched = 1;
  12258. }
  12259. }
  12260. #if defined(BENCH_PQ_STATEFUL_HBS)
  12261. /* post-quantum stateful hash-based signatures */
  12262. for (i=0; !optMatched && bench_pq_hash_sig_opt[i].str != NULL; i++) {
  12263. if (string_matches(argv[1], bench_pq_hash_sig_opt[i].str)) {
  12264. bench_pq_hash_sig_algs |= bench_pq_hash_sig_opt[i].val;
  12265. bench_all = 0;
  12266. optMatched = 1;
  12267. }
  12268. }
  12269. #endif /* BENCH_PQ_STATEFUL_HBS */
  12270. #endif
  12271. if (!optMatched) {
  12272. printf("Option not recognized: %s\n", argv[1]);
  12273. Usage();
  12274. return 1;
  12275. }
  12276. }
  12277. else {
  12278. /* parse for block size */
  12279. benchmark_configure((word32)XATOI(argv[1]));
  12280. }
  12281. argc--;
  12282. argv++;
  12283. }
  12284. #endif /* MAIN_NO_ARGS */
  12285. #if defined(WOLFSSL_BENCHMARK_FIXED_CSV)
  12286. /* when defined, we'll always output CSV regardless of params.
  12287. ** this is typically convenient in embedded environments.
  12288. */
  12289. csv_format = 1;
  12290. #endif
  12291. #if defined(WC_ENABLE_BENCH_THREADING) && !defined(WOLFSSL_ASYNC_CRYPT)
  12292. if (g_threadCount > 1) {
  12293. ret = benchmark_test_threaded(NULL);
  12294. }
  12295. else
  12296. #endif
  12297. {
  12298. #if defined(CONFIG_IDF_TARGET_ESP32C2) || \
  12299. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  12300. defined(CONFIG_IDF_TARGET_ESP32C6)
  12301. {
  12302. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  12303. if (esp_gptimer == NULL) {
  12304. ESP_ERROR_CHECK(gptimer_new_timer(&esp_timer_config,
  12305. &esp_gptimer) );
  12306. }
  12307. ESP_ERROR_CHECK(gptimer_enable(esp_gptimer));
  12308. ESP_ERROR_CHECK(gptimer_start(esp_gptimer));
  12309. ESP_LOGI(TAG, "Enable %s timer", CONFIG_IDF_TARGET);
  12310. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  12311. }
  12312. #endif
  12313. #ifdef HAVE_STACK_SIZE
  12314. ret = StackSizeCheck(NULL, benchmark_test);
  12315. #else
  12316. ret = benchmark_test(NULL);
  12317. #endif
  12318. }
  12319. #if defined(CONFIG_IDF_TARGET_ESP32C2) || \
  12320. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  12321. defined(CONFIG_IDF_TARGET_ESP32C6)
  12322. {
  12323. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  12324. ESP_ERROR_CHECK(gptimer_stop(esp_gptimer));
  12325. ESP_ERROR_CHECK(gptimer_disable(esp_gptimer));
  12326. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  12327. }
  12328. #endif
  12329. return ret;
  12330. }
  12331. #endif /* !NO_MAIN_DRIVER */
  12332. #else
  12333. #if !defined(NO_MAIN_DRIVER) && !defined(NO_MAIN_FUNCTION)
  12334. int main(void) { return 0; }
  12335. #endif
  12336. #endif /* !NO_CRYPT_BENCHMARK */