benchmark.c 218 KB

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