benchmark.c 260 KB

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