benchmark.c 252 KB

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