1
0

benchmark.c 337 KB

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