1
0

benchmark.c 292 KB

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