2
0

benchmark.c 324 KB

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