2
0

benchmark.c 427 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453
  1. /* benchmark.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* wolfCrypt benchmark */
  22. /* Some common, optional build settings:
  23. * these can also be set in wolfssl/options.h or user_settings.h
  24. * -------------------------------------------------------------
  25. * make the binary always use CSV format:
  26. * WOLFSSL_BENCHMARK_FIXED_CSV
  27. *
  28. * choose to use the same units, regardless of scale. pick 1:
  29. * WOLFSSL_BENCHMARK_FIXED_UNITS_GB
  30. * WOLFSSL_BENCHMARK_FIXED_UNITS_MB
  31. * WOLFSSL_BENCHMARK_FIXED_UNITS_KB
  32. * WOLFSSL_BENCHMARK_FIXED_UNITS_B
  33. *
  34. * when the output should be in machine-parseable format:
  35. * GENERATE_MACHINE_PARSEABLE_REPORT
  36. *
  37. * use microseconds as the unit of time:
  38. * BENCH_MICROSECOND
  39. *
  40. * display mean, max, min and sd of operation durations:
  41. * MULTI_VALUE_STATISTICS
  42. *
  43. * Enable tracking of the stats into an allocated linked list:
  44. * (use -print to display results):
  45. * WC_BENCH_TRACK_STATS
  46. *
  47. * set the default devId for cryptocb to the value instead of INVALID_DEVID
  48. * WC_USE_DEVID=0x1234
  49. *
  50. * Turn on benchmark timing debugging (CPU Cycles, RTOS ticks, etc)
  51. * DEBUG_WOLFSSL_BENCHMARK_TIMING
  52. *
  53. */
  54. #ifdef HAVE_CONFIG_H
  55. #include <config.h>
  56. #endif
  57. #ifndef WOLFSSL_USER_SETTINGS
  58. #include <wolfssl/options.h>
  59. #endif
  60. #include <wolfssl/wolfcrypt/settings.h> /* also picks up user_settings.h */
  61. /* Macro to disable benchmark */
  62. #ifndef NO_CRYPT_BENCHMARK
  63. #define WC_ALLOC_DO_ON_FAILURE() do { printf("out of memory at benchmark.c L %d\n", __LINE__); ret = MEMORY_E; goto exit; } while (0)
  64. #include <wolfssl/wolfcrypt/types.h>
  65. #include <wolfssl/wolfcrypt/wc_port.h>
  66. #include <wolfssl/wolfcrypt/wolfmath.h>
  67. #include <wolfssl/wolfcrypt/memory.h>
  68. #include <wolfssl/wolfcrypt/random.h>
  69. #include <wolfssl/wolfcrypt/error-crypt.h>
  70. #include <wolfssl/wolfcrypt/asn.h>
  71. #include <wolfssl/version.h>
  72. #ifdef WOLFSSL_LINUXKM
  73. /* remap current_time() -- collides with a function in kernel linux/fs.h */
  74. #define current_time benchmark_current_time
  75. #endif /* WOLFSSL_LINUXKM */
  76. #ifdef HAVE_CHACHA
  77. #include <wolfssl/wolfcrypt/chacha.h>
  78. #endif
  79. #ifdef HAVE_POLY1305
  80. #include <wolfssl/wolfcrypt/poly1305.h>
  81. #endif
  82. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  83. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  84. #endif
  85. #ifndef NO_AES
  86. #include <wolfssl/wolfcrypt/aes.h>
  87. #endif
  88. #ifdef HAVE_CAMELLIA
  89. #include <wolfssl/wolfcrypt/camellia.h>
  90. #endif
  91. #ifdef WOLFSSL_SM4
  92. #include <wolfssl/wolfcrypt/sm4.h>
  93. #endif
  94. #ifndef NO_MD5
  95. #include <wolfssl/wolfcrypt/md5.h>
  96. #endif
  97. #ifndef NO_SHA
  98. #include <wolfssl/wolfcrypt/sha.h>
  99. #endif
  100. #ifndef NO_SHA256
  101. #include <wolfssl/wolfcrypt/sha256.h>
  102. #endif
  103. #if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)
  104. #include <wolfssl/wolfcrypt/sha512.h>
  105. #endif
  106. #ifdef WOLFSSL_SHA3
  107. #include <wolfssl/wolfcrypt/sha3.h>
  108. #endif
  109. #ifdef WOLFSSL_SM3
  110. #include <wolfssl/wolfcrypt/sm3.h>
  111. #endif
  112. #ifndef NO_RSA
  113. #include <wolfssl/wolfcrypt/rsa.h>
  114. #endif
  115. #ifdef WOLFSSL_RIPEMD
  116. #include <wolfssl/wolfcrypt/ripemd.h>
  117. #endif
  118. #ifdef WOLFSSL_CMAC
  119. #include <wolfssl/wolfcrypt/cmac.h>
  120. #endif
  121. #ifndef NO_DH
  122. #include <wolfssl/wolfcrypt/dh.h>
  123. #endif
  124. #ifndef NO_DES3
  125. #include <wolfssl/wolfcrypt/des3.h>
  126. #endif
  127. #ifndef NO_RC4
  128. #include <wolfssl/wolfcrypt/arc4.h>
  129. #endif
  130. #ifndef NO_HMAC
  131. #include <wolfssl/wolfcrypt/hmac.h>
  132. #endif
  133. #ifdef WOLFSSL_SIPHASH
  134. #include <wolfssl/wolfcrypt/siphash.h>
  135. #endif
  136. #include <wolfssl/wolfcrypt/kdf.h>
  137. #ifndef NO_PWDBASED
  138. #include <wolfssl/wolfcrypt/pwdbased.h>
  139. #endif
  140. #ifdef HAVE_ECC
  141. #include <wolfssl/wolfcrypt/ecc.h>
  142. #endif
  143. #ifdef WOLFSSL_SM2
  144. #include <wolfssl/wolfcrypt/sm2.h>
  145. #endif
  146. #ifdef HAVE_CURVE25519
  147. #include <wolfssl/wolfcrypt/curve25519.h>
  148. #endif
  149. #ifdef HAVE_ED25519
  150. #include <wolfssl/wolfcrypt/ed25519.h>
  151. #endif
  152. #ifdef HAVE_CURVE448
  153. #include <wolfssl/wolfcrypt/curve448.h>
  154. #endif
  155. #ifdef HAVE_ED448
  156. #include <wolfssl/wolfcrypt/ed448.h>
  157. #endif
  158. #ifdef WOLFSSL_HAVE_KYBER
  159. #include <wolfssl/wolfcrypt/kyber.h>
  160. #ifdef WOLFSSL_WC_KYBER
  161. #include <wolfssl/wolfcrypt/wc_kyber.h>
  162. #endif
  163. #if defined(HAVE_LIBOQS) || defined(HAVE_PQM4)
  164. #include <wolfssl/wolfcrypt/ext_kyber.h>
  165. #endif
  166. #endif
  167. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  168. #include <wolfssl/wolfcrypt/lms.h>
  169. #ifdef HAVE_LIBLMS
  170. #include <wolfssl/wolfcrypt/ext_lms.h>
  171. #endif
  172. #endif
  173. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  174. #include <wolfssl/wolfcrypt/xmss.h>
  175. #ifdef HAVE_LIBXMSS
  176. #include <wolfssl/wolfcrypt/ext_xmss.h>
  177. #else
  178. #include <wolfssl/wolfcrypt/wc_xmss.h>
  179. #endif
  180. #endif
  181. #ifdef WOLFCRYPT_HAVE_ECCSI
  182. #include <wolfssl/wolfcrypt/eccsi.h>
  183. #endif
  184. #ifdef WOLFCRYPT_HAVE_SAKKE
  185. #include <wolfssl/wolfcrypt/sakke.h>
  186. #endif
  187. #if defined(HAVE_PQC)
  188. #if defined(HAVE_FALCON)
  189. #include <wolfssl/wolfcrypt/falcon.h>
  190. #endif
  191. #if defined(HAVE_DILITHIUM)
  192. #include <wolfssl/wolfcrypt/dilithium.h>
  193. #endif
  194. #if defined(HAVE_SPHINCS)
  195. #include <wolfssl/wolfcrypt/sphincs.h>
  196. #endif
  197. #endif
  198. #ifdef WOLF_CRYPTO_CB
  199. #include <wolfssl/wolfcrypt/cryptocb.h>
  200. #ifdef HAVE_INTEL_QA_SYNC
  201. #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
  202. #endif
  203. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  204. #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
  205. #endif
  206. #ifdef HAVE_RENESAS_SYNC
  207. #include <wolfssl/wolfcrypt/port/renesas/renesas_sync.h>
  208. #endif
  209. #endif
  210. #ifdef WOLFSSL_ASYNC_CRYPT
  211. #include <wolfssl/wolfcrypt/async.h>
  212. #endif
  213. #ifdef USE_FLAT_BENCHMARK_H
  214. #include "benchmark.h"
  215. #else
  216. #include "wolfcrypt/benchmark/benchmark.h"
  217. #endif
  218. /* define the max length for each string of metric reported */
  219. #ifndef WC_BENCH_MAX_LINE_LEN
  220. #define WC_BENCH_MAX_LINE_LEN 150
  221. #endif
  222. /* default units per second. See WOLFSSL_BENCHMARK_FIXED_UNITS_* to change */
  223. #define WOLFSSL_FIXED_UNIT "MB" /* may be re-set by fixed units */
  224. #define MILLION_VALUE 1000000.0
  225. #ifdef BENCH_MICROSECOND
  226. #define WOLFSSL_FIXED_TIME_UNIT "μs"
  227. #define WOLFSSL_BENCHMARK_FIXED_UNITS_KB
  228. #else
  229. #define WOLFSSL_FIXED_TIME_UNIT "s"
  230. #endif
  231. #ifdef MULTI_VALUE_STATISTICS
  232. #define STATS_CLAUSE_SEPARATOR ""
  233. #define DECLARE_MULTI_VALUE_STATS_VARS() double max = 0, min = 0, sum = 0,\
  234. squareSum = 0, prev = 0, delta;\
  235. int runs = 0;
  236. #define RECORD_MULTI_VALUE_STATS() if (runs == 0) {\
  237. delta = current_time(0) - start;\
  238. min = delta;\
  239. max = delta;\
  240. }\
  241. else {\
  242. delta = current_time(0) - prev;\
  243. }\
  244. if (max < delta)\
  245. max = delta;\
  246. else if (min > delta)\
  247. min = delta;\
  248. sum += delta;\
  249. squareSum += delta * delta;\
  250. runs++;\
  251. prev = current_time(0)
  252. #define RESET_MULTI_VALUE_STATS_VARS() prev = 0;\
  253. runs = 0;\
  254. sum = 0;\
  255. squareSum = 0
  256. #else
  257. #define STATS_CLAUSE_SEPARATOR "\n"
  258. #define DECLARE_MULTI_VALUE_STATS_VARS()
  259. #define RECORD_MULTI_VALUE_STATS() WC_DO_NOTHING
  260. #define RESET_MULTI_VALUE_STATS_VARS() WC_DO_NOTHING
  261. #endif
  262. #ifdef WOLFSSL_NO_FLOAT_FMT
  263. #define FLT_FMT "%0ld,%09lu"
  264. #define FLT_FMT_PREC "%0ld.%0*lu"
  265. #define FLT_FMT_PREC2 FLT_FMT_PREC
  266. #define FLT_FMT_ARGS(x) (long)(x), ((x) < 0) ? \
  267. (unsigned long)(-(((x) - (double)(long)(x)) * 1000000000.0)) : \
  268. (unsigned long)(((x) - (double)(long)(x)) * 1000000000.0)
  269. static const double pow_10_array[] = { 0.0, 1.0, 10.0, 100.0, 1000.0, \
  270. 10000.0, 100000.0, 1000000.0, \
  271. 10000000.0, 100000000.0, \
  272. 1000000000.0 };
  273. #define FLT_FMT_PREC_ARGS(p, x) \
  274. (long)(x), \
  275. p, \
  276. (x) >= 0.0 ? \
  277. (unsigned long int)((((x) - (double)(long)(x)) * \
  278. pow_10_array[(p)+1]) + 0.5) : \
  279. (unsigned long int)((((-(x)) - (double)((long)-(x))) * \
  280. pow_10_array[(p)+1]) + 0.5)
  281. #define FLT_FMT_PREC2_ARGS(w, p, x) FLT_FMT_PREC_ARGS(p, x)
  282. #else
  283. #define FLT_FMT "%f"
  284. #define FLT_FMT_PREC "%.*f"
  285. #define FLT_FMT_PREC2 "%*.*f"
  286. #define FLT_FMT_ARGS(x) x
  287. #define FLT_FMT_PREC_ARGS(p, x) p, x
  288. #define FLT_FMT_PREC2_ARGS(w, p, x) w, p, x
  289. #endif /* WOLFSSL_NO_FLOAT_FMT */
  290. #ifdef WOLFSSL_ESPIDF
  291. #ifdef configTICK_RATE_HZ
  292. /* Define CPU clock cycles per tick of FreeRTOS clock
  293. * CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ is typically a value like 240
  294. * configTICK_RATE_HZ is typically 100 or 1000.
  295. **/
  296. #define CPU_TICK_CYCLES ( \
  297. (CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ * MILLION_VALUE) \
  298. / configTICK_RATE_HZ \
  299. )
  300. #endif
  301. #if defined(CONFIG_IDF_TARGET_ESP32C2)
  302. #include "driver/gptimer.h"
  303. static gptimer_handle_t esp_gptimer = NULL;
  304. static gptimer_config_t esp_timer_config = {
  305. .clk_src = GPTIMER_CLK_SRC_DEFAULT,
  306. .direction = GPTIMER_COUNT_UP,
  307. .resolution_hz = CONFIG_XTAL_FREQ * 100000,
  308. };
  309. #elif defined(CONFIG_IDF_TARGET_ESP32C3) || \
  310. defined(CONFIG_IDF_TARGET_ESP32C6)
  311. #include <esp_cpu.h>
  312. #include "driver/gptimer.h"
  313. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  314. #define RESOLUTION_SCALE 100
  315. static gptimer_handle_t esp_gptimer = NULL;
  316. static gptimer_config_t esp_timer_config = {
  317. .clk_src = GPTIMER_CLK_SRC_DEFAULT,
  318. .direction = GPTIMER_COUNT_UP,
  319. .resolution_hz = CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ * (MILLION_VALUE / RESOLUTION_SCALE), /* CONFIG_XTAL_FREQ = 40, CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ = 160 */
  320. };
  321. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  322. #elif defined(CONFIG_IDF_TARGET_ESP32) || \
  323. defined(CONFIG_IDF_TARGET_ESP32S2) || \
  324. defined(CONFIG_IDF_TARGET_ESP32S3)
  325. #include <xtensa/hal.h>
  326. #elif defined(CONFIG_IDF_TARGET_ESP32H2)
  327. #else
  328. /* Other platform */
  329. #endif
  330. #include <esp_log.h>
  331. #endif /* WOLFSSL_ESPIDF */
  332. #if defined(HAVE_PTHREAD) || \
  333. (!defined(NO_CRYPT_BENCHMARK) && !defined(NO_STDIO_FILESYSTEM) && \
  334. !defined(NO_ERROR_STRINGS) && !defined(NO_MAIN_DRIVER) && \
  335. !defined(BENCH_EMBEDDED))
  336. #include <errno.h>
  337. #if !defined(WOLFSSL_ZEPHYR) && !defined(_WIN32)
  338. #include <unistd.h>
  339. #endif
  340. #endif
  341. #if defined(WOLFSSL_ZEPHYR) || defined(NO_STDIO_FILESYSTEM) || !defined(XFFLUSH)
  342. /* fflush in Zephyr doesn't work on stdout and stderr. Use
  343. * CONFIG_LOG_MODE_IMMEDIATE compilation option instead. */
  344. #undef XFFLUSH
  345. #define XFFLUSH(...) WC_DO_NOTHING
  346. #endif
  347. /* only for stack size check */
  348. #include <wolfssl/wolfcrypt/mem_track.h>
  349. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  350. #define WC_ENABLE_BENCH_THREADING
  351. #endif
  352. /* enable tracking of stats for threaded benchmark */
  353. #if defined(WC_ENABLE_BENCH_THREADING) && !defined(WC_BENCH_TRACK_STATS)
  354. #define WC_BENCH_TRACK_STATS
  355. #endif
  356. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  357. static const char info_prefix[] = "###, ";
  358. static const char err_prefix[] = "!!!, ";
  359. #else
  360. static const char info_prefix[] = "";
  361. static const char err_prefix[] = "";
  362. #endif
  363. /* printf mappings */
  364. #ifdef FREESCALE_MQX
  365. #include <mqx.h>
  366. /* see wc_port.h for fio.h and nio.h includes */
  367. #elif defined(FREESCALE_KSDK_1_3)
  368. #include "fsl_debug_console.h"
  369. #include "fsl_os_abstraction.h"
  370. #undef printf
  371. #define printf PRINTF
  372. #elif defined(WOLFSSL_DEOS)
  373. #include <deos.h>
  374. #include <printx.h>
  375. #undef printf
  376. #define printf printx
  377. #elif defined(MICRIUM)
  378. #if (OS_VERSION < 50000)
  379. #include <bsp_ser.h>
  380. void BSP_Ser_Printf (CPU_CHAR* format, ...);
  381. #undef printf
  382. #define printf BSP_Ser_Printf
  383. #endif
  384. #elif defined(WOLFSSL_ZEPHYR)
  385. #include <stdio.h>
  386. #define BENCH_EMBEDDED
  387. #define printf printfk
  388. static int printfk(const char *fmt, ...)
  389. {
  390. int ret;
  391. char line[WC_BENCH_MAX_LINE_LEN];
  392. va_list ap;
  393. va_start(ap, fmt);
  394. ret = vsnprintf(line, sizeof(line), fmt, ap);
  395. line[sizeof(line)-1] = '\0';
  396. printk("%s", line);
  397. va_end(ap);
  398. return ret;
  399. }
  400. #elif defined(WOLFSSL_TELIT_M2MB)
  401. #include <stdarg.h>
  402. #include <stdio.h>
  403. #include <string.h>
  404. #include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
  405. /* remap printf */
  406. #undef printf
  407. #define printf M2M_LOG_INFO
  408. /* OS requires occasional sleep() */
  409. #ifndef TEST_SLEEP_MS
  410. #define TEST_SLEEP_MS 50
  411. #endif
  412. #define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
  413. /* don't use file system for these tests, since ./certs dir isn't loaded */
  414. #undef NO_FILESYSTEM
  415. #define NO_FILESYSTEM
  416. /* ANDROID_V454 (for android studio) displays information in a textview
  417. * and redirects printf to the textview output instead of using
  418. * __android_log_print() */
  419. #elif defined(ANDROID) && !defined(ANDROID_V454)
  420. #ifdef XMALLOC_USER
  421. #include <stdlib.h> /* we're using malloc / free direct here */
  422. #endif
  423. #ifndef STRING_USER
  424. #include <stdio.h>
  425. #endif
  426. #include <android/log.h>
  427. #define printf(...) \
  428. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  429. #define fprintf(fp, ...) \
  430. __android_log_print(ANDROID_LOG_DEBUG, "[WOLFCRYPT]", __VA_ARGS__)
  431. #else
  432. #if defined(XMALLOC_USER) || defined(FREESCALE_MQX)
  433. /* MQX classic needs for EXIT_FAILURE */
  434. #include <stdlib.h> /* we're using malloc / free direct here */
  435. #endif
  436. #if !defined(STRING_USER) && !defined(NO_STDIO_FILESYSTEM)
  437. #include <string.h>
  438. #include <stdio.h>
  439. #endif
  440. /* enable way for customer to override test/bench printf */
  441. #ifdef XPRINTF
  442. #undef printf
  443. #define printf XPRINTF
  444. #elif defined(NETOS)
  445. #undef printf
  446. #define printf dc_log_printf
  447. #endif
  448. #endif
  449. #ifdef HAVE_FIPS
  450. #include <wolfssl/wolfcrypt/fips_test.h>
  451. static void myFipsCb(int ok, int err, const char* hash)
  452. {
  453. printf("%sin my Fips callback, ok = %d, err = %d\n",
  454. ok ? info_prefix : err_prefix, ok, err);
  455. printf("%smessage = %s\n", ok ? info_prefix : err_prefix,
  456. wc_GetErrorString(err));
  457. printf("%shash = %s\n", ok ? info_prefix : err_prefix, hash);
  458. if (err == IN_CORE_FIPS_E) {
  459. printf("%sIn core integrity hash check failure, copy above hash\n",
  460. err_prefix);
  461. printf("%sinto verifyCore[] in fips_test.c and rebuild\n",
  462. err_prefix);
  463. }
  464. }
  465. #endif
  466. #ifdef WOLFSSL_STATIC_MEMORY
  467. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  468. #else
  469. #define HEAP_HINT NULL
  470. #endif /* WOLFSSL_STATIC_MEMORY */
  471. #ifndef EXIT_FAILURE
  472. #define EXIT_FAILURE 1
  473. #endif
  474. #undef LIBCALL_CHECK_RET
  475. #if defined(NO_STDIO_FILESYSTEM) || defined(NO_ERROR_STRINGS) || \
  476. defined(NO_MAIN_DRIVER) || defined(BENCH_EMBEDDED)
  477. #define LIBCALL_CHECK_RET(...) __VA_ARGS__
  478. #else
  479. #define LIBCALL_CHECK_RET(...) do { \
  480. int _libcall_ret = (__VA_ARGS__); \
  481. if (_libcall_ret < 0) { \
  482. printf("%s%s L%d error %d for \"%s\"\n", \
  483. err_prefix, __FILE__, __LINE__, \
  484. errno, #__VA_ARGS__); \
  485. XFFLUSH(stdout); \
  486. _exit(1); \
  487. } \
  488. } while(0)
  489. #endif
  490. #undef THREAD_CHECK_RET
  491. #define THREAD_CHECK_RET(...) do { \
  492. int _thread_ret = (__VA_ARGS__); \
  493. if (_thread_ret != 0) { \
  494. errno = _thread_ret; \
  495. printf("%s%s L%d error %d for \"%s\"\n", \
  496. err_prefix, __FILE__, __LINE__, \
  497. _thread_ret, #__VA_ARGS__); \
  498. XFFLUSH(stdout); \
  499. _exit(1); \
  500. } \
  501. } while(0)
  502. /* optional macro to add sleep between tests */
  503. #ifndef TEST_SLEEP
  504. /* stub the sleep macro */
  505. #define TEST_SLEEP() WC_DO_NOTHING
  506. #endif
  507. #define TEST_STRING "Everyone gets Friday off."
  508. #define TEST_STRING_SZ 25
  509. /* Bit values for each algorithm that is able to be benchmarked.
  510. * Common grouping of algorithms also.
  511. * Each algorithm has a unique value for its type e.g. cipher.
  512. */
  513. /* Cipher algorithms. */
  514. #define BENCH_AES_CBC 0x00000001
  515. #define BENCH_AES_GCM 0x00000002
  516. #define BENCH_AES_ECB 0x00000004
  517. #define BENCH_AES_XTS 0x00000008
  518. #define BENCH_AES_CTR 0x00000010
  519. #define BENCH_AES_CCM 0x00000020
  520. #define BENCH_CAMELLIA 0x00000100
  521. #define BENCH_ARC4 0x00000200
  522. #define BENCH_CHACHA20 0x00001000
  523. #define BENCH_CHACHA20_POLY1305 0x00002000
  524. #define BENCH_DES 0x00004000
  525. #define BENCH_AES_CFB 0x00010000
  526. #define BENCH_AES_OFB 0x00020000
  527. #define BENCH_AES_SIV 0x00040000
  528. #define BENCH_SM4_CBC 0x00080000
  529. #define BENCH_SM4_GCM 0x00100000
  530. #define BENCH_SM4_CCM 0x00200000
  531. #define BENCH_SM4 (BENCH_SM4_CBC | BENCH_SM4_GCM | BENCH_SM4_CCM)
  532. /* Digest algorithms. */
  533. #define BENCH_MD5 0x00000001
  534. #define BENCH_POLY1305 0x00000002
  535. #define BENCH_SHA 0x00000004
  536. #define BENCH_SHA224 0x00000010
  537. #define BENCH_SHA256 0x00000020
  538. #define BENCH_SHA384 0x00000040
  539. #define BENCH_SHA512 0x00000080
  540. #define BENCH_SHA2 (BENCH_SHA224 | BENCH_SHA256 | \
  541. BENCH_SHA384 | BENCH_SHA512)
  542. #define BENCH_SHA3_224 0x00000100
  543. #define BENCH_SHA3_256 0x00000200
  544. #define BENCH_SHA3_384 0x00000400
  545. #define BENCH_SHA3_512 0x00000800
  546. #define BENCH_SHA3 (BENCH_SHA3_224 | BENCH_SHA3_256 | \
  547. BENCH_SHA3_384 | BENCH_SHA3_512)
  548. #define BENCH_SHAKE128 0x00001000
  549. #define BENCH_SHAKE256 0x00002000
  550. #define BENCH_SHAKE (BENCH_SHAKE128 | BENCH_SHAKE256)
  551. #define BENCH_RIPEMD 0x00004000
  552. #define BENCH_BLAKE2B 0x00008000
  553. #define BENCH_BLAKE2S 0x00010000
  554. #define BENCH_SM3 0x00020000
  555. /* MAC algorithms. */
  556. #define BENCH_CMAC 0x00000001
  557. #define BENCH_HMAC_MD5 0x00000002
  558. #define BENCH_HMAC_SHA 0x00000004
  559. #define BENCH_HMAC_SHA224 0x00000010
  560. #define BENCH_HMAC_SHA256 0x00000020
  561. #define BENCH_HMAC_SHA384 0x00000040
  562. #define BENCH_HMAC_SHA512 0x00000080
  563. #define BENCH_HMAC (BENCH_HMAC_MD5 | BENCH_HMAC_SHA | \
  564. BENCH_HMAC_SHA224 | BENCH_HMAC_SHA256 | \
  565. BENCH_HMAC_SHA384 | BENCH_HMAC_SHA512)
  566. #define BENCH_PBKDF2 0x00000100
  567. #define BENCH_SIPHASH 0x00000200
  568. /* KDF algorithms */
  569. #define BENCH_SRTP_KDF 0x00000001
  570. /* Asymmetric algorithms. */
  571. #define BENCH_RSA_KEYGEN 0x00000001
  572. #define BENCH_RSA 0x00000002
  573. #define BENCH_RSA_SZ 0x00000004
  574. #define BENCH_DH 0x00000010
  575. #define BENCH_KYBER 0x00000020
  576. #define BENCH_ECC_MAKEKEY 0x00001000
  577. #define BENCH_ECC 0x00002000
  578. #define BENCH_ECC_ENCRYPT 0x00004000
  579. #define BENCH_ECC_ALL 0x00008000
  580. #define BENCH_CURVE25519_KEYGEN 0x00010000
  581. #define BENCH_CURVE25519_KA 0x00020000
  582. #define BENCH_ED25519_KEYGEN 0x00040000
  583. #define BENCH_ED25519_SIGN 0x00080000
  584. #define BENCH_CURVE448_KEYGEN 0x00100000
  585. #define BENCH_CURVE448_KA 0x00200000
  586. #define BENCH_ED448_KEYGEN 0x00400000
  587. #define BENCH_ED448_SIGN 0x00800000
  588. #define BENCH_ECC_P256 0x01000000
  589. #define BENCH_ECC_P384 0x02000000
  590. #define BENCH_ECC_P521 0x04000000
  591. #define BENCH_SM2 0x08000000
  592. #define BENCH_ECCSI_KEYGEN 0x00000020
  593. #define BENCH_ECCSI_PAIRGEN 0x00000040
  594. #define BENCH_ECCSI_VALIDATE 0x00000080
  595. #define BENCH_ECCSI 0x00000400
  596. #define BENCH_SAKKE_KEYGEN 0x10000000
  597. #define BENCH_SAKKE_RSKGEN 0x20000000
  598. #define BENCH_SAKKE_VALIDATE 0x40000000
  599. #define BENCH_SAKKE 0x80000000
  600. /* Post-Quantum Asymmetric algorithms. */
  601. #define BENCH_FALCON_LEVEL1_SIGN 0x00000001
  602. #define BENCH_FALCON_LEVEL5_SIGN 0x00000002
  603. #define BENCH_DILITHIUM_LEVEL2_SIGN 0x04000000
  604. #define BENCH_DILITHIUM_LEVEL3_SIGN 0x08000000
  605. #define BENCH_DILITHIUM_LEVEL5_SIGN 0x10000000
  606. /* Post-Quantum Asymmetric algorithms. (Part 2) */
  607. #define BENCH_SPHINCS_FAST_LEVEL1_SIGN 0x00000001
  608. #define BENCH_SPHINCS_FAST_LEVEL3_SIGN 0x00000002
  609. #define BENCH_SPHINCS_FAST_LEVEL5_SIGN 0x00000004
  610. #define BENCH_SPHINCS_SMALL_LEVEL1_SIGN 0x00000008
  611. #define BENCH_SPHINCS_SMALL_LEVEL3_SIGN 0x00000010
  612. #define BENCH_SPHINCS_SMALL_LEVEL5_SIGN 0x00000020
  613. /* Post-Quantum Stateful Hash-Based sig algorithms. */
  614. #define BENCH_LMS_HSS 0x00000001
  615. #define BENCH_XMSS_XMSSMT_SHA256 0x00000002
  616. #define BENCH_XMSS_XMSSMT_SHA512 0x00000004
  617. #define BENCH_XMSS_XMSSMT_SHAKE128 0x00000008
  618. #define BENCH_XMSS_XMSSMT_SHAKE256 0x00000010
  619. #ifndef NO_SHA256
  620. #define BENCH_XMSS_XMSSMT BENCH_XMSS_XMSSMT_SHA256
  621. #elif defined(WOLFSSL_SHA512)
  622. #define BENCH_XMSS_XMSSMT BENCH_XMSS_XMSSMT_SHA512
  623. #elif defined(WOLFSSL_SHAKE128)
  624. #define BENCH_XMSS_XMSSMT BENCH_XMSS_XMSSMT_SHAKE128
  625. #elif defined(WOLFSSL_SHAKE256)
  626. #define BENCH_XMSS_XMSSMT BENCH_XMSS_XMSSMT_SHAKE256
  627. #else
  628. #define BENCH_XMSS_XMSSMT 0x00000000
  629. #endif
  630. /* Other */
  631. #define BENCH_RNG 0x00000001
  632. #define BENCH_SCRYPT 0x00000002
  633. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  634. /* Define AES_AUTH_ADD_SZ already here, since it's used in the
  635. * static declaration of `bench_Usage_msg1`. */
  636. #if !defined(AES_AUTH_ADD_SZ) && \
  637. defined(STM32_CRYPTO) && !defined(STM32_AESGCM_PARTIAL) || \
  638. defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  639. /* For STM32 use multiple of 4 to leverage crypto hardware
  640. * Xilinx Versal requires to use multiples of 16 bytes */
  641. #define AES_AUTH_ADD_SZ 16
  642. #endif
  643. #ifndef AES_AUTH_ADD_SZ
  644. #define AES_AUTH_ADD_SZ 13
  645. #endif
  646. #endif
  647. #if (defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)) || \
  648. (defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY))
  649. #define BENCH_PQ_STATEFUL_HBS
  650. #endif
  651. /* Benchmark all compiled in algorithms.
  652. * When 1, ignore other benchmark algorithm values.
  653. * 0, only benchmark algorithm values set.
  654. */
  655. static int bench_all = 1;
  656. /* Cipher algorithms to benchmark. */
  657. static word32 bench_cipher_algs = 0;
  658. /* Digest algorithms to benchmark. */
  659. static word32 bench_digest_algs = 0;
  660. /* MAC algorithms to benchmark. */
  661. static word32 bench_mac_algs = 0;
  662. /* KDF algorithms to benchmark. */
  663. static word32 bench_kdf_algs = 0;
  664. /* Asymmetric algorithms to benchmark. */
  665. static word32 bench_asym_algs = 0;
  666. /* Post-Quantum Asymmetric algorithms to benchmark. */
  667. static word32 bench_pq_asym_algs = 0;
  668. /* Post-Quantum Asymmetric algorithms to benchmark. (Part 2)*/
  669. static word32 bench_pq_asym_algs2 = 0;
  670. /* Other cryptographic algorithms to benchmark. */
  671. static word32 bench_other_algs = 0;
  672. /* Post-Quantum Stateful Hash-Based sig algorithms to benchmark. */
  673. static word32 bench_pq_hash_sig_algs = 0;
  674. #if !defined(WOLFSSL_BENCHMARK_ALL) && !defined(NO_MAIN_DRIVER)
  675. /* The mapping of command line option to bit values. */
  676. typedef struct bench_alg {
  677. /* Command line option string. */
  678. const char* str;
  679. /* Bit values to set. */
  680. word32 val;
  681. } bench_alg;
  682. #ifndef MAIN_NO_ARGS
  683. /* All recognized cipher algorithm choosing command line options. */
  684. static const bench_alg bench_cipher_opt[] = {
  685. { "-cipher", 0xffffffff },
  686. #ifdef HAVE_AES_CBC
  687. { "-aes-cbc", BENCH_AES_CBC },
  688. #endif
  689. #ifdef HAVE_AESGCM
  690. { "-aes-gcm", BENCH_AES_GCM },
  691. #endif
  692. #ifdef WOLFSSL_AES_DIRECT
  693. { "-aes-ecb", BENCH_AES_ECB },
  694. #endif
  695. #ifdef WOLFSSL_AES_XTS
  696. { "-aes-xts", BENCH_AES_XTS },
  697. #endif
  698. #ifdef WOLFSSL_AES_CFB
  699. { "-aes-cfb", BENCH_AES_CFB },
  700. #endif
  701. #ifdef WOLFSSL_AES_OFB
  702. { "-aes-ofb", BENCH_AES_OFB },
  703. #endif
  704. #ifdef WOLFSSL_AES_COUNTER
  705. { "-aes-ctr", BENCH_AES_CTR },
  706. #endif
  707. #ifdef HAVE_AESCCM
  708. { "-aes-ccm", BENCH_AES_CCM },
  709. #endif
  710. #ifdef WOLFSSL_AES_SIV
  711. { "-aes-siv", BENCH_AES_SIV },
  712. #endif
  713. #ifdef HAVE_CAMELLIA
  714. { "-camellia", BENCH_CAMELLIA },
  715. #endif
  716. #ifndef NO_RC4
  717. { "-arc4", BENCH_ARC4 },
  718. #endif
  719. #ifdef HAVE_CHACHA
  720. { "-chacha20", BENCH_CHACHA20 },
  721. #endif
  722. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  723. { "-chacha20-poly1305", BENCH_CHACHA20_POLY1305 },
  724. #endif
  725. #ifdef WOLFSSL_SM4_CBC
  726. { "-sm4-cbc", BENCH_SM4_CBC },
  727. #endif
  728. #ifdef WOLFSSL_SM4_GCM
  729. { "-sm4-gcm", BENCH_SM4_GCM },
  730. #endif
  731. #ifdef WOLFSSL_SM4_CCM
  732. { "-sm4-ccm", BENCH_SM4_CCM },
  733. #endif
  734. #ifdef WOLFSSL_SM4
  735. { "-sm4", BENCH_SM4 },
  736. #endif
  737. #ifndef NO_DES3
  738. { "-des", BENCH_DES },
  739. #endif
  740. { NULL, 0 }
  741. };
  742. /* All recognized digest algorithm choosing command line options. */
  743. static const bench_alg bench_digest_opt[] = {
  744. { "-digest", 0xffffffff },
  745. #ifndef NO_MD5
  746. { "-md5", BENCH_MD5 },
  747. #endif
  748. #ifdef HAVE_POLY1305
  749. { "-poly1305", BENCH_POLY1305 },
  750. #endif
  751. #ifndef NO_SHA
  752. { "-sha", BENCH_SHA },
  753. #endif
  754. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256) || defined(WOLFSSL_SHA384) \
  755. || defined(WOLFSSL_SHA512)
  756. { "-sha2", BENCH_SHA2 },
  757. #endif
  758. #ifdef WOLFSSL_SHA224
  759. { "-sha224", BENCH_SHA224 },
  760. #endif
  761. #ifndef NO_SHA256
  762. { "-sha256", BENCH_SHA256 },
  763. #endif
  764. #ifdef WOLFSSL_SHA384
  765. { "-sha384", BENCH_SHA384 },
  766. #endif
  767. #ifdef WOLFSSL_SHA512
  768. { "-sha512", BENCH_SHA512 },
  769. #endif
  770. #ifdef WOLFSSL_SHA3
  771. { "-sha3", BENCH_SHA3 },
  772. #ifndef WOLFSSL_NOSHA3_224
  773. { "-sha3-224", BENCH_SHA3_224 },
  774. #endif
  775. #ifndef WOLFSSL_NOSHA3_256
  776. { "-sha3-256", BENCH_SHA3_256 },
  777. #endif
  778. #ifndef WOLFSSL_NOSHA3_384
  779. { "-sha3-384", BENCH_SHA3_384 },
  780. #endif
  781. #ifndef WOLFSSL_NOSHA3_512
  782. { "-sha3-512", BENCH_SHA3_512 },
  783. #endif
  784. #if defined(WOLFSSL_SHAKE128) || defined(WOLFSSL_SHAKE256)
  785. { "-shake", BENCH_SHAKE },
  786. #endif
  787. #ifdef WOLFSSL_SHAKE128
  788. { "-shake128", BENCH_SHAKE128 },
  789. #endif
  790. #ifdef WOLFSSL_SHAKE256
  791. { "-shake256", BENCH_SHAKE256 },
  792. #endif
  793. #endif
  794. #ifdef WOLFSSL_SM3
  795. { "-sm3", BENCH_SM3 },
  796. #endif
  797. #ifdef WOLFSSL_RIPEMD
  798. { "-ripemd", BENCH_RIPEMD },
  799. #endif
  800. #ifdef HAVE_BLAKE2
  801. { "-blake2b", BENCH_BLAKE2B },
  802. #endif
  803. #ifdef HAVE_BLAKE2S
  804. { "-blake2s", BENCH_BLAKE2S },
  805. #endif
  806. { NULL, 0 }
  807. };
  808. /* All recognized MAC algorithm choosing command line options. */
  809. static const bench_alg bench_mac_opt[] = {
  810. { "-mac", 0xffffffff },
  811. #ifdef WOLFSSL_CMAC
  812. { "-cmac", BENCH_CMAC },
  813. #endif
  814. #ifndef NO_HMAC
  815. { "-hmac", BENCH_HMAC },
  816. #ifndef NO_MD5
  817. { "-hmac-md5", BENCH_HMAC_MD5 },
  818. #endif
  819. #ifndef NO_SHA
  820. { "-hmac-sha", BENCH_HMAC_SHA },
  821. #endif
  822. #ifdef WOLFSSL_SHA224
  823. { "-hmac-sha224", BENCH_HMAC_SHA224 },
  824. #endif
  825. #ifndef NO_SHA256
  826. { "-hmac-sha256", BENCH_HMAC_SHA256 },
  827. #endif
  828. #ifdef WOLFSSL_SHA384
  829. { "-hmac-sha384", BENCH_HMAC_SHA384 },
  830. #endif
  831. #ifdef WOLFSSL_SHA512
  832. { "-hmac-sha512", BENCH_HMAC_SHA512 },
  833. #endif
  834. #ifndef NO_PWDBASED
  835. { "-pbkdf2", BENCH_PBKDF2 },
  836. #endif
  837. #endif
  838. #ifdef WOLFSSL_SIPHASH
  839. { "-siphash", BENCH_SIPHASH },
  840. #endif
  841. { NULL, 0 }
  842. };
  843. /* All recognized KDF algorithm choosing command line options. */
  844. static const bench_alg bench_kdf_opt[] = {
  845. { "-kdf", 0xffffffff },
  846. #ifdef WC_SRTP_KDF
  847. { "-srtp-kdf", BENCH_SRTP_KDF },
  848. #endif
  849. { NULL, 0 }
  850. };
  851. /* All recognized asymmetric algorithm choosing command line options. */
  852. static const bench_alg bench_asym_opt[] = {
  853. { "-asym", 0xffffffff },
  854. #ifndef NO_RSA
  855. #ifdef WOLFSSL_KEY_GEN
  856. { "-rsa-kg", BENCH_RSA_KEYGEN },
  857. #endif
  858. { "-rsa", BENCH_RSA },
  859. { "-rsa-sz", BENCH_RSA_SZ },
  860. #endif
  861. #ifndef NO_DH
  862. { "-dh", BENCH_DH },
  863. #endif
  864. #ifdef WOLFSSL_HAVE_KYBER
  865. { "-kyber", BENCH_KYBER },
  866. #endif
  867. #ifdef HAVE_ECC
  868. { "-ecc-kg", BENCH_ECC_MAKEKEY },
  869. { "-ecc", BENCH_ECC },
  870. #ifdef HAVE_ECC_ENCRYPT
  871. { "-ecc-enc", BENCH_ECC_ENCRYPT },
  872. #endif
  873. { "-ecc-all", BENCH_ECC_ALL },
  874. #endif
  875. #ifdef WOLFSSL_SM2
  876. { "-sm2", BENCH_SM2 },
  877. #endif
  878. #ifdef HAVE_CURVE25519
  879. { "-curve25519-kg", BENCH_CURVE25519_KEYGEN },
  880. #ifdef HAVE_CURVE25519_SHARED_SECRET
  881. { "-x25519", BENCH_CURVE25519_KA },
  882. #endif
  883. #endif
  884. #ifdef HAVE_ED25519
  885. { "-ed25519-kg", BENCH_ED25519_KEYGEN },
  886. { "-ed25519", BENCH_ED25519_SIGN },
  887. #endif
  888. #ifdef HAVE_CURVE448
  889. { "-curve448-kg", BENCH_CURVE448_KEYGEN },
  890. #ifdef HAVE_CURVE448_SHARED_SECRET
  891. { "-x448", BENCH_CURVE448_KA },
  892. #endif
  893. #endif
  894. #ifdef HAVE_ED448
  895. { "-ed448-kg", BENCH_ED448_KEYGEN },
  896. { "-ed448", BENCH_ED448_SIGN },
  897. #endif
  898. #ifdef WOLFCRYPT_HAVE_ECCSI
  899. { "-eccsi-kg", BENCH_ECCSI_KEYGEN },
  900. { "-eccsi-pair", BENCH_ECCSI_PAIRGEN },
  901. { "-eccsi-val", BENCH_ECCSI_VALIDATE },
  902. { "-eccsi", BENCH_ECCSI },
  903. #endif
  904. #ifdef WOLFCRYPT_HAVE_SAKKE
  905. { "-sakke-kg", BENCH_SAKKE_KEYGEN },
  906. { "-sakke-rsk", BENCH_SAKKE_RSKGEN },
  907. { "-sakke-val", BENCH_SAKKE_VALIDATE },
  908. { "-sakke", BENCH_SAKKE },
  909. #endif
  910. { NULL, 0 }
  911. };
  912. /* All recognized other cryptographic algorithm choosing command line options.
  913. */
  914. static const bench_alg bench_other_opt[] = {
  915. { "-other", 0xffffffff },
  916. #ifndef WC_NO_RNG
  917. { "-rng", BENCH_RNG },
  918. #endif
  919. #ifdef HAVE_SCRYPT
  920. { "-scrypt", BENCH_SCRYPT },
  921. #endif
  922. { NULL, 0}
  923. };
  924. #endif /* MAIN_NO_ARGS */
  925. #endif /* !WOLFSSL_BENCHMARK_ALL && !NO_MAIN_DRIVER */
  926. #if defined(BENCH_PQ_STATEFUL_HBS)
  927. typedef struct bench_pq_hash_sig_alg {
  928. /* Command line option string. */
  929. const char* str;
  930. /* Bit values to set. */
  931. word32 val;
  932. } bench_pq_hash_sig_alg;
  933. static const bench_pq_hash_sig_alg bench_pq_hash_sig_opt[] = {
  934. { "-pq_hash_sig", 0xffffffff},
  935. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  936. { "-lms_hss", BENCH_LMS_HSS},
  937. #endif
  938. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  939. { "-xmss_xmssmt", BENCH_XMSS_XMSSMT},
  940. #ifdef WC_XMSS_SHA256
  941. { "-xmss_xmssmt_sha256", BENCH_XMSS_XMSSMT_SHA256},
  942. #endif
  943. #ifdef WC_XMSS_SHA512
  944. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 512 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 512
  945. { "-xmss_xmssmt_sha512", BENCH_XMSS_XMSSMT_SHA512},
  946. #endif
  947. #endif
  948. #ifdef WC_XMSS_SHAKE128
  949. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  950. { "-xmss_xmssmt_shake128", BENCH_XMSS_XMSSMT_SHAKE128},
  951. #endif
  952. #endif
  953. #ifdef WC_XMSS_SHAKE256
  954. { "-xmss_xmssmt_shake256", BENCH_XMSS_XMSSMT_SHAKE256},
  955. #endif
  956. #endif
  957. { NULL, 0}
  958. };
  959. #endif /* BENCH_PQ_STATEFUL_HBS */
  960. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  961. /* The post-quantum-specific mapping of command line option to bit values and
  962. * OQS name. */
  963. typedef struct bench_pq_alg {
  964. /* Command line option string. */
  965. const char* str;
  966. /* Bit values to set. */
  967. word32 val;
  968. const char* pqc_name;
  969. } bench_pq_alg;
  970. /* All recognized post-quantum asymmetric algorithm choosing command line
  971. * options. */
  972. static const bench_pq_alg bench_pq_asym_opt[] = {
  973. { "-pq", 0xffffffff, NULL},
  974. #ifdef HAVE_LIBOQS
  975. { "-falcon_level1", BENCH_FALCON_LEVEL1_SIGN,
  976. OQS_SIG_alg_falcon_512 },
  977. { "-falcon_level5", BENCH_FALCON_LEVEL5_SIGN,
  978. OQS_SIG_alg_falcon_1024 },
  979. { "-dilithium_level2", BENCH_DILITHIUM_LEVEL2_SIGN,
  980. OQS_SIG_alg_dilithium_2 },
  981. { "-dilithium_level3", BENCH_DILITHIUM_LEVEL3_SIGN,
  982. OQS_SIG_alg_dilithium_3 },
  983. { "-dilithium_level5", BENCH_DILITHIUM_LEVEL5_SIGN,
  984. OQS_SIG_alg_dilithium_5 },
  985. #endif /* HAVE_LIBOQS */
  986. { NULL, 0, NULL }
  987. };
  988. #if defined(HAVE_LIBOQS) && defined(HAVE_SPHINCS)
  989. /* All recognized post-quantum asymmetric algorithm choosing command line
  990. * options. (Part 2) */
  991. static const bench_pq_alg bench_pq_asym_opt2[] = {
  992. { "-pq", 0xffffffff, NULL},
  993. { "-sphincs_fast_level1", BENCH_SPHINCS_FAST_LEVEL1_SIGN,
  994. OQS_SIG_alg_sphincs_shake_128f_simple },
  995. { "-sphincs_fast_level3", BENCH_SPHINCS_FAST_LEVEL3_SIGN,
  996. OQS_SIG_alg_sphincs_shake_192f_simple },
  997. { "-sphincs_fast_level5", BENCH_SPHINCS_FAST_LEVEL5_SIGN,
  998. OQS_SIG_alg_sphincs_shake_256f_simple },
  999. { "-sphincs_small_level1", BENCH_SPHINCS_SMALL_LEVEL1_SIGN,
  1000. OQS_SIG_alg_sphincs_shake_128s_simple },
  1001. { "-sphincs_small_level3", BENCH_SPHINCS_SMALL_LEVEL3_SIGN,
  1002. OQS_SIG_alg_sphincs_shake_192s_simple },
  1003. { "-sphincs_small_level5", BENCH_SPHINCS_SMALL_LEVEL5_SIGN,
  1004. OQS_SIG_alg_sphincs_shake_256s_simple },
  1005. { NULL, 0, NULL }
  1006. };
  1007. #endif /* HAVE_LIBOQS && HAVE_SPHINCS */
  1008. #endif /* HAVE_PQC */
  1009. #ifdef HAVE_WNR
  1010. const char* wnrConfigFile = "wnr-example.conf";
  1011. #endif
  1012. #if defined(WOLFSSL_MDK_ARM)
  1013. extern XFILE wolfSSL_fopen(const char *fname, const char *mode);
  1014. #define fopen wolfSSL_fopen
  1015. #endif
  1016. static int lng_index = 0;
  1017. #ifndef NO_MAIN_DRIVER
  1018. #ifndef MAIN_NO_ARGS
  1019. static const char* bench_Usage_msg1[][25] = {
  1020. /* 0 English */
  1021. { "-? <num> Help, print this usage\n",
  1022. " 0: English, 1: Japanese\n",
  1023. "-csv Print terminal output in csv format\n",
  1024. "-base10 Display bytes as power of 10 (eg 1 kB = 1000 Bytes)\n",
  1025. "-no_aad No additional authentication data passed.\n",
  1026. "-aad_size <num> With <num> bytes of AAD.\n",
  1027. ("-all_aad With AAD length of 0, "
  1028. WC_STRINGIFY(AES_AUTH_ADD_SZ)
  1029. " and\n"
  1030. " (if set via -aad_size) <aad_size> bytes.\n"
  1031. ),
  1032. "-dgst_full Full digest operation performed.\n",
  1033. "-rsa_sign Measure RSA sign/verify instead of encrypt/decrypt.\n",
  1034. "<keySz> -rsa-sz\n Measure RSA <key size> performance.\n",
  1035. "-ffhdhe2048 Measure DH using FFDHE 2048-bit parameters.\n",
  1036. "-ffhdhe3072 Measure DH using FFDHE 3072-bit parameters.\n",
  1037. "-p256 Measure ECC using P-256 curve.\n",
  1038. "-p384 Measure ECC using P-384 curve.\n",
  1039. "-p521 Measure ECC using P-521 curve.\n",
  1040. "-ecc-all Bench all enabled ECC curves.\n",
  1041. "-<alg> Algorithm to benchmark. Available algorithms include:\n",
  1042. ("-lng <num> Display benchmark result by specified language.\n"
  1043. " 0: English, 1: Japanese\n"
  1044. ),
  1045. "<num> Size of block in bytes\n",
  1046. ("-blocks <num> Number of blocks. Can be used together with the "
  1047. "'Size of block'\n"
  1048. " option, but must be used after that one.\n"
  1049. ),
  1050. "-threads <num> Number of threads to run\n",
  1051. "-print Show benchmark stats summary\n",
  1052. "-hash_input <file> Input data to use for hash benchmarking\n",
  1053. "-cipher_input <file> Input data to use for cipher benchmarking\n",
  1054. "-min_runs <num> Specify minimum number of operation runs\n"
  1055. },
  1056. #ifndef NO_MULTIBYTE_PRINT
  1057. /* 1 Japanese */
  1058. { "-? <num> ヘルプ, 使い方を表示します。\n",
  1059. " 0: 英語、 1: 日本語\n",
  1060. "-csv csv 形式で端末に出力します。\n",
  1061. "-base10 バイトを10のべき乗で表示します。(例 1 kB = 1000 Bytes)\n",
  1062. "-no_aad 追加の認証データを使用しません.\n",
  1063. "-aad_size <num> TBD.\n",
  1064. "-all_aad TBD.\n",
  1065. "-dgst_full フルの digest 暗号操作を実施します。\n",
  1066. "-rsa_sign 暗号/復号化の代わりに RSA の署名/検証を測定します。\n",
  1067. "<keySz> -rsa-sz\n RSA <key size> の性能を測定します。\n",
  1068. "-ffhdhe2048 Measure DH using FFDHE 2048-bit parameters.\n",
  1069. "-ffhdhe3072 Measure DH using FFDHE 3072-bit parameters.\n",
  1070. "-p256 Measure ECC using P-256 curve.\n",
  1071. "-p384 Measure ECC using P-384 curve.\n",
  1072. "-p521 Measure ECC using P-521 curve.\n",
  1073. "-ecc-all Bench all enabled ECC curves.\n",
  1074. ("-<alg> アルゴリズムのベンチマークを実施します。\n"
  1075. " 利用可能なアルゴリズムは下記を含みます:\n"
  1076. ),
  1077. ("-lng <num> 指定された言語でベンチマーク結果を表示します。\n"
  1078. " 0: 英語、 1: 日本語\n"
  1079. ),
  1080. "<num> ブロックサイズをバイト単位で指定します。\n",
  1081. "-blocks <num> TBD.\n",
  1082. "-threads <num> 実行するスレッド数\n",
  1083. "-print ベンチマーク統計の要約を表示する\n",
  1084. /* TODO: translate below */
  1085. "-hash_input <file> Input data to use for hash benchmarking\n",
  1086. "-cipher_input <file> Input data to use for cipher benchmarking\n",
  1087. "-min_runs <num> Specify minimum number of operation runs\n"
  1088. },
  1089. #endif
  1090. };
  1091. #endif /* MAIN_NO_ARGS */
  1092. #endif
  1093. static const char* bench_result_words1[][4] = {
  1094. { "took",
  1095. #ifdef BENCH_MICROSECOND
  1096. "microseconds"
  1097. #else
  1098. "seconds"
  1099. #endif
  1100. , "Cycles per byte", NULL }, /* 0 English */
  1101. #ifndef NO_MULTIBYTE_PRINT
  1102. { "を" , "秒で処理", "1バイトあたりのサイクル数", NULL }, /* 1 Japanese */
  1103. #endif
  1104. };
  1105. #if !defined(NO_RSA) || \
  1106. defined(HAVE_ECC) || !defined(NO_DH) || defined(HAVE_ECC_ENCRYPT) || \
  1107. defined(HAVE_CURVE25519) || defined(HAVE_CURVE25519_SHARED_SECRET) || \
  1108. defined(HAVE_ED25519) || defined(HAVE_CURVE448) || \
  1109. defined(HAVE_CURVE448_SHARED_SECRET) || defined(HAVE_ED448) || \
  1110. defined(WOLFSSL_HAVE_KYBER)
  1111. static const char* bench_desc_words[][15] = {
  1112. /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 */
  1113. {"public", "private", "key gen", "agree" , "sign", "verify", "encrypt", "decrypt", "rsk gen", "encap", "derive", "valid", "pair gen", "decap", NULL}, /* 0 English */
  1114. #ifndef NO_MULTIBYTE_PRINT
  1115. {"公開鍵", "秘密鍵" ,"鍵生成" , "鍵共有" , "署名", "検証" , "暗号化" , "復号化" , "rsk gen", "encap", "derive", "valid", "pair gen", "decap", NULL}, /* 1 Japanese */
  1116. #endif
  1117. };
  1118. #endif
  1119. #ifdef MULTI_VALUE_STATISTICS
  1120. static const char* bench_result_words3[][5] = {
  1121. /* 0 English */
  1122. { "max duration", "min duration" , "mean duration", "sd", NULL },
  1123. /* TODO: Add japenese version */
  1124. { "max duration", "min duration" , "mean duration", "sd", NULL }
  1125. };
  1126. #endif
  1127. #if defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM) && !defined(WOLFSSL_SGX)
  1128. #define HAVE_GET_CYCLES
  1129. static WC_INLINE word64 get_intel_cycles(void);
  1130. static THREAD_LS_T word64 total_cycles;
  1131. #define INIT_CYCLE_COUNTER
  1132. #define BEGIN_INTEL_CYCLES total_cycles = get_intel_cycles();
  1133. #define END_INTEL_CYCLES total_cycles = get_intel_cycles() - total_cycles;
  1134. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  1135. #define SHOW_INTEL_CYCLES(b, n, s) \
  1136. (void)XSNPRINTF((b) + XSTRLEN(b), (n) - XSTRLEN(b), \
  1137. " %s = " FLT_FMT_PREC2 STATS_CLAUSE_SEPARATOR, \
  1138. bench_result_words1[lng_index][2], \
  1139. FLT_FMT_PREC2_ARGS(6, 2, count == 0 ? 0 : \
  1140. (double)total_cycles / ((word64)count*(s))))
  1141. #define SHOW_INTEL_CYCLES_CSV(b, n, s) \
  1142. (void)XSNPRINTF((b) + XSTRLEN(b), (n) - XSTRLEN(b), FLT_FMT_PREC "," \
  1143. STATS_CLAUSE_SEPARATOR, FLT_FMT_PREC_ARGS(6, count == 0 ? 0 : \
  1144. (double)total_cycles / ((word64)count*(s))))
  1145. #elif defined(LINUX_CYCLE_COUNT)
  1146. #include <linux/perf_event.h>
  1147. #include <sys/syscall.h>
  1148. #include <unistd.h>
  1149. static THREAD_LS_T word64 begin_cycles;
  1150. static THREAD_LS_T word64 total_cycles;
  1151. static THREAD_LS_T int cycles = -1;
  1152. static THREAD_LS_T struct perf_event_attr atr;
  1153. #define INIT_CYCLE_COUNTER do { \
  1154. atr.type = PERF_TYPE_HARDWARE; \
  1155. atr.config = PERF_COUNT_HW_CPU_CYCLES; \
  1156. cycles = (int)syscall(__NR_perf_event_open, &atr, 0, -1, -1, 0); \
  1157. } while (0);
  1158. #define BEGIN_INTEL_CYCLES read(cycles, &begin_cycles, sizeof(begin_cycles));
  1159. #define END_INTEL_CYCLES do { \
  1160. read(cycles, &total_cycles, sizeof(total_cycles)); \
  1161. total_cycles = total_cycles - begin_cycles; \
  1162. } while (0);
  1163. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  1164. #define SHOW_INTEL_CYCLES(b, n, s) \
  1165. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), \
  1166. " %s = " FLT_FMT_PREC2 STATS_CLAUSE_SEPARATOR, \
  1167. bench_result_words1[lng_index][2], \
  1168. FLT_FMT_PREC2_ARGS(6, 2, (double)total_cycles / \
  1169. (count*s)))
  1170. #define SHOW_INTEL_CYCLES_CSV(b, n, s) \
  1171. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), FLT_FMT_PREC "," \
  1172. STATS_CLAUSE_SEPARATOR, FLT_FMT_PREC_ARGS(6, (double)total_cycles \
  1173. / (count*s)))
  1174. #elif defined(SYNERGY_CYCLE_COUNT)
  1175. #include "hal_data.h"
  1176. static THREAD_LS_T word64 begin_cycles;
  1177. static THREAD_LS_T word64 total_cycles;
  1178. #define INIT_CYCLE_COUNTER
  1179. #define BEGIN_INTEL_CYCLES begin_cycles = DWT->CYCCNT = 0;
  1180. #define END_INTEL_CYCLES total_cycles = DWT->CYCCNT - begin_cycles;
  1181. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  1182. #define SHOW_INTEL_CYCLES(b, n, s) \
  1183. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), \
  1184. " %s = " FLT_FMT_PREC2 STATS_CLAUSE_SEPARATOR, \
  1185. bench_result_words1[lng_index][2], \
  1186. FLT_FMT_PREC2_ARGS(6, 2, (double)total_cycles / (count*s)))
  1187. #define SHOW_INTEL_CYCLES_CSV(b, n, s) \
  1188. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), FLT_FMT_PREC ",\n", \
  1189. FLT_FMT_PREC_ARGS(6, (double)total_cycles / (count*s)))
  1190. #elif defined(WOLFSSL_ESPIDF)
  1191. /* TAG for ESP_LOGx() */
  1192. static const char* TAG = "wolfssl_benchmark";
  1193. static THREAD_LS_T word64 begin_cycles;
  1194. static THREAD_LS_T word64 begin_cycles_ticks;
  1195. static THREAD_LS_T word64 end_cycles;
  1196. static THREAD_LS_T word64 total_cycles;
  1197. /* the return value, as a global var */
  1198. static THREAD_LS_T word64 _esp_get_cycle_count_ex = 0;
  1199. /* the last value seen, adjusted for an overflow, as a global var */
  1200. static THREAD_LS_T word64 _esp_cpu_count_last = 0;
  1201. static THREAD_LS_T TickType_t last_tickCount = 0; /* last FreeRTOS value */
  1202. /* esp_get_cpu_benchmark_cycles(void):
  1203. *
  1204. * Architecture-independant CPU clock counter.
  1205. * WARNING: the hal UINT xthal_get_ccount() quietly rolls over. */
  1206. static WC_INLINE word64 esp_get_cpu_benchmark_cycles(void);
  1207. /* Some vars for debugging, compare ticks to cycles */
  1208. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1209. static THREAD_LS_T word64 _esp_cpu_timer_last = 0;
  1210. static THREAD_LS_T word64 _esp_cpu_timer_diff = 0;
  1211. static THREAD_LS_T word64 _xthal_get_ccount_exAlt = 0;
  1212. static THREAD_LS_T word64 _xthal_get_ccount_exDiff = 0;
  1213. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  1214. /* The ESP32 (both Xtensa and RISC-V have raw CPU counters). */
  1215. #if ESP_IDF_VERSION_MAJOR >= 5
  1216. /* esp_cpu_set_cycle_count() introduced in ESP-IDF v5 */
  1217. #define HAVE_GET_CYCLES
  1218. #define INIT_CYCLE_COUNTER do { \
  1219. ESP_LOGV(TAG, "INIT_CYCLE_COUNTER"); \
  1220. esp_cpu_set_cycle_count(0); \
  1221. } while (0);
  1222. #else
  1223. #define HAVE_GET_CYCLES
  1224. #define INIT_CYCLE_COUNTER do { \
  1225. ESP_LOGV(TAG, "INIT_CYCLE_COUNTER"); \
  1226. } while (0);
  1227. #endif
  1228. #define BEGIN_ESP_CYCLES do { \
  1229. ESP_LOGV(TAG, "BEGIN_ESP_CYCLES"); \
  1230. begin_cycles = esp_get_cpu_benchmark_cycles(); \
  1231. begin_cycles_ticks = xTaskGetTickCount(); \
  1232. } while (0);
  1233. /* since it rolls over, we have something that will tolerate one */
  1234. #define END_ESP_CYCLES \
  1235. end_cycles = esp_get_cpu_benchmark_cycles(); \
  1236. ESP_LOGV(TAG,"END_ESP_CYCLES %llu - %llu", \
  1237. end_cycles, \
  1238. begin_cycles \
  1239. ); \
  1240. total_cycles = (end_cycles - begin_cycles);
  1241. #define SHOW_ESP_CYCLES(b, n, s) \
  1242. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), \
  1243. " %s = " FLT_FMT_PREC2 "\n", \
  1244. bench_result_words1[lng_index][2], \
  1245. FLT_FMT_PREC2_ARGS(6, 2, (double)total_cycles / (count*s)) \
  1246. )
  1247. #define SHOW_ESP_CYCLES_CSV(b, n, s) \
  1248. (void)XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), FLT_FMT_PREC ",\n", \
  1249. FLT_FMT_PREC_ARGS(6, (double)total_cycles / (count*s)))
  1250. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1251. /* 64 bit, unisgned, absolute difference
  1252. * used in CPU cycle counter debug calcs. */
  1253. static uint64_t esp_cycle_abs_diff(uint64_t x, uint64_t y)
  1254. {
  1255. uint64_t ret;
  1256. ret = (x > y) ? (x - y) : (y - x);
  1257. return ret;
  1258. }
  1259. #endif
  1260. /* esp_get_cycle_count_ex() is a single-overflow-tolerant extension to
  1261. ** the Espressif `unsigned xthal_get_ccount()` (Xtensa) or
  1262. ** `esp_cpu_get_cycle_count` (RISC-V) which are known to overflow
  1263. ** at least once during full benchmark tests.
  1264. **
  1265. ** To test timing overflow, add a delay longer than max cycles:
  1266. ** vTaskDelay( (const TickType_t)(configTICK_RATE_HZ * 17 * 5) );
  1267. */
  1268. uint64_t esp_get_cycle_count_ex()
  1269. {
  1270. /* reminder: unsigned long long max = 18,446,744,073,709,551,615 */
  1271. /* unsigned int max = 4,294,967,295 */
  1272. uint64_t thisVal = 0; /* CPU counter, "this current value" as read. */
  1273. uint64_t thisIncrement = 0; /* The adjusted increment amount. */
  1274. uint64_t expected_diff = 0; /* FreeRTOS esimated expected CPU diff. */
  1275. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1276. uint32_t tickCount = 0; /* Currrent rtos tick counter. */
  1277. uint32_t tickDiff = 0; /* Tick difference from last check. */
  1278. uint32_t tickBeginDiff = 0; /* Tick difference from beginning. */
  1279. #endif
  1280. #if defined(CONFIG_IDF_TARGET_ESP32C2) || \
  1281. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  1282. defined(CONFIG_IDF_TARGET_ESP32C6)
  1283. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1284. uint64_t thisTimerVal = 0; /* Timer Value as alternate to compare */
  1285. uint64_t diffDiff = 0; /* Difference between CPU & Timer differences:
  1286. * (current - last) */
  1287. ESP_ERROR_CHECK(gptimer_get_raw_count(esp_gptimer, &thisTimerVal));
  1288. thisTimerVal = thisTimerVal * RESOLUTION_SCALE;
  1289. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  1290. thisVal = esp_cpu_get_cycle_count();
  1291. #elif defined(CONFIG_IDF_TARGET_ESP32H2)
  1292. thisVal = esp_cpu_get_cycle_count();
  1293. #else
  1294. /* TODO: Why doesn't esp_cpu_get_cycle_count work for Xtensa?
  1295. * Calling current_time(1) to reset time causes thisVal overflow,
  1296. * on Xtensa, but not on RISC-V architecture. See also, below */
  1297. #ifndef __XTENSA__
  1298. thisVal = esp_cpu_get_cycle_count();
  1299. #else
  1300. thisVal = xthal_get_ccount(); /* or esp_cpu_get_cycle_count(); */
  1301. #endif
  1302. #endif
  1303. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1304. {
  1305. tickCount = xTaskGetTickCount(); /* Our local FreeRTOS tick count */
  1306. tickDiff = tickCount - last_tickCount; /* ticks since bench start */
  1307. expected_diff = CPU_TICK_CYCLES * tickDiff; /* CPU expected count */
  1308. ESP_LOGV(TAG, "CPU_TICK_CYCLES = %d", (int)CPU_TICK_CYCLES);
  1309. ESP_LOGV(TAG, "tickCount = %lu", tickCount);
  1310. ESP_LOGV(TAG, "last_tickCount = %lu", last_tickCount);
  1311. ESP_LOGV(TAG, "tickDiff = %lu", tickDiff);
  1312. ESP_LOGV(TAG, "expected_diff1 = %llu", expected_diff);
  1313. }
  1314. #endif
  1315. /* If either thisVal is smaller than last (overflow), and/or the
  1316. * expected value calculated from FreeRTOS tick difference that would
  1317. * have never fit into an unsigned 32 bit integer anyhow... then we
  1318. * need to adjust thisVal to save. */
  1319. if ( (thisVal < _esp_cpu_count_last) || (expected_diff > UINT_MAX) )
  1320. {
  1321. /* Warning: we assume the return type of esp_cpu_get_cycle_count()
  1322. ** will always be unsigned int (or uint32_t) to add UINT_MAX.
  1323. **
  1324. ** NOTE for long duration between calls with multiple overflows:
  1325. **
  1326. ** WILL NOT BE DETECTED - the return value will be INCORRECT.
  1327. **
  1328. ** At this time no single test overflows. This is currently only a
  1329. ** concern for cumulative counts over multiple tests. As long
  1330. ** as well call xthal_get_ccount_ex() with no more than one
  1331. ** overflow CPU tick count, all will be well.
  1332. */
  1333. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1334. ESP_LOGW(TAG,
  1335. "Alert: Detected xthal_get_ccount overflow at %llu, "
  1336. "adding UINT_MAX.",
  1337. thisVal);
  1338. #endif
  1339. /* double check expected diff calc */
  1340. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1341. expected_diff = (CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ * MILLION_VALUE)
  1342. * tickDiff / configTICK_RATE_HZ;
  1343. ESP_LOGI(TAG, "expected_diff2 = %llu", expected_diff);
  1344. #endif
  1345. if (expected_diff > UINT_MAX) {
  1346. /* The number of cycles expected from FreeRTOS ticks is
  1347. * greater than the maximum size of an unsigned 32-bit
  1348. * integer, meaning multiple overflows occured. */
  1349. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1350. ESP_LOGW(TAG, "expected_diff > UINT_MAX (%u)", UINT_MAX);
  1351. #endif
  1352. thisVal += expected_diff; /* FreeRTOS calc to our 64 bit val */
  1353. }
  1354. else {
  1355. thisVal += (word64)UINT_MAX; /* add 32 bit max to our 64 bit */
  1356. }
  1357. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1358. {
  1359. tickBeginDiff = tickCount - begin_cycles_ticks;
  1360. ESP_LOGI(TAG, "begin_cycles_ticks = %llu", begin_cycles_ticks);
  1361. ESP_LOGI(TAG, "tickDiff = %lu", tickDiff);
  1362. ESP_LOGI(TAG, "expected_diff = %llu", expected_diff);
  1363. ESP_LOGI(TAG, "tickBeginDiff = %lu", tickBeginDiff);
  1364. ESP_LOGW(TAG, "");
  1365. }
  1366. #endif
  1367. }
  1368. else {
  1369. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1370. ESP_LOGI(TAG, "thisVal, read CPU = %llu", thisVal);
  1371. #endif
  1372. } /* if thisVal adjustment check */
  1373. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1374. if (thisTimerVal < _esp_cpu_timer_last)
  1375. {
  1376. ESP_LOGW(TAG, "Alert: Detected xthal_get_ccountAlt overflow, "
  1377. "adding %ull", UINT_MAX);
  1378. thisTimerVal += (word64)UINT_MAX;
  1379. }
  1380. /* Check an alternate counter using a timer */
  1381. _esp_cpu_timer_diff = esp_cycle_abs_diff(_esp_cpu_count_last, _esp_cpu_timer_last);
  1382. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  1383. /* Adjust our actual returned value that takes into account overflow,
  1384. * increment 64 bit extended total by this 32 bit differential: */
  1385. thisIncrement = (thisVal - _esp_cpu_count_last);
  1386. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1387. ESP_LOGI(TAG, "thisIncrement = %llu", thisIncrement);
  1388. #endif
  1389. /* Add our adjustment, taking into account overflows (see above) */
  1390. _esp_get_cycle_count_ex += thisIncrement;
  1391. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1392. _xthal_get_ccount_exDiff = esp_cycle_abs_diff(_esp_get_cycle_count_ex, _xthal_get_ccount_exAlt);
  1393. _xthal_get_ccount_exAlt += (thisTimerVal - _esp_cpu_timer_last);
  1394. diffDiff = esp_cycle_abs_diff(_xthal_get_ccount_exDiff, _esp_cpu_timer_diff);
  1395. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  1396. /* all of this took some time, so reset the "last seen" value
  1397. * for the next measurement. */
  1398. #if defined(CONFIG_IDF_TARGET_ESP32C2) || \
  1399. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  1400. defined(CONFIG_IDF_TARGET_ESP32C6)
  1401. {
  1402. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  1403. ESP_ERROR_CHECK(gptimer_get_raw_count(esp_gptimer,
  1404. &_esp_cpu_timer_last));
  1405. ESP_LOGI(TAG, "thisVal = %llu", thisVal);
  1406. ESP_LOGI(TAG, "thisTimerVal = %llu", thisTimerVal);
  1407. ESP_LOGI(TAG, "diffDiff = %llu", diffDiff);
  1408. ESP_LOGI(TAG, "_xthal_get_ccount_exDiff = %llu", _xthal_get_ccount_exDiff);
  1409. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  1410. _esp_cpu_count_last = esp_cpu_get_cycle_count();
  1411. ESP_LOGV(TAG, "_xthal_get_ccount_last = %llu", _esp_cpu_count_last);
  1412. }
  1413. #elif defined(CONFIG_IDF_TARGET_ESP32H2)
  1414. _esp_cpu_count_last = esp_cpu_get_cycle_count();
  1415. #else
  1416. /* TODO: Why doesn't esp_cpu_get_cycle_count work for Xtensa
  1417. * when resetting CPU cycle counter? FreeRTOS tick collison?
  1418. * thisVal = esp_cpu_get_cycle_count(); See also, above
  1419. * or thisVal = xthal_get_ccount(); */
  1420. #if ESP_IDF_VERSION_MAJOR < 5
  1421. _esp_cpu_count_last = xthal_get_ccount();
  1422. #else
  1423. _esp_cpu_count_last = esp_cpu_get_cycle_count();
  1424. #endif
  1425. #endif
  1426. /* Return the 64 bit extended total from 32 bit counter. */
  1427. return _esp_get_cycle_count_ex;
  1428. }
  1429. /* implement other architecture cycle counters here */
  1430. #else
  1431. /* if we don't know the platform, it is unlikely we can count CPU cycles */
  1432. #undef HAVE_GET_CYCLES
  1433. #define INIT_CYCLE_COUNTER
  1434. #define BEGIN_INTEL_CYCLES
  1435. #define END_INTEL_CYCLES
  1436. #ifdef MULTI_VALUE_STATISTICS
  1437. #define SHOW_INTEL_CYCLES(b, n, s) WC_DO_NOTHING
  1438. #define SHOW_INTEL_CYCLES_CSV(b, n, s) WC_DO_NOTHING
  1439. #else
  1440. #define SHOW_INTEL_CYCLES(b, n, s) b[XSTRLEN(b)] = '\n'
  1441. #define SHOW_INTEL_CYCLES_CSV(b, n, s) b[XSTRLEN(b)] = '\n'
  1442. #endif
  1443. #endif
  1444. /* determine benchmark buffer to use (if NO_FILESYSTEM) */
  1445. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  1446. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  1447. #define USE_CERT_BUFFERS_2048 /* default to 2048 */
  1448. #endif
  1449. #if defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048) || \
  1450. defined(USE_CERT_BUFFERS_3072) || defined(USE_CERT_BUFFERS_4096) || \
  1451. !defined(NO_DH)
  1452. /* include test cert and key buffers for use with NO_FILESYSTEM */
  1453. #include <wolfssl/certs_test.h>
  1454. #endif
  1455. #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
  1456. #include <wolfssl/wolfcrypt/blake2.h>
  1457. #endif
  1458. #ifdef _MSC_VER
  1459. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  1460. #pragma warning(disable: 4996)
  1461. #endif
  1462. #ifdef WOLFSSL_CURRTIME_REMAP
  1463. #define current_time WOLFSSL_CURRTIME_REMAP
  1464. #else
  1465. double current_time(int reset);
  1466. #endif
  1467. #ifdef LINUX_RUSAGE_UTIME
  1468. static void check_for_excessive_stime(const char *desc,
  1469. const char *desc_extra);
  1470. #endif
  1471. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND) && \
  1472. !defined(HAVE_STACK_SIZE)
  1473. #ifdef __cplusplus
  1474. extern "C" {
  1475. #endif
  1476. WOLFSSL_API int wolfSSL_Debugging_ON(void);
  1477. WOLFSSL_API void wolfSSL_Debugging_OFF(void);
  1478. #ifdef __cplusplus
  1479. } /* extern "C" */
  1480. #endif
  1481. #endif
  1482. #if !defined(WC_NO_RNG) && \
  1483. ((!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) \
  1484. || !defined(NO_DH) || defined(WOLFSSL_KEY_GEN) || defined(HAVE_ECC) \
  1485. || defined(HAVE_CURVE25519) || defined(HAVE_ED25519) \
  1486. || defined(HAVE_CURVE448) || defined(HAVE_ED448) \
  1487. || defined(WOLFSSL_HAVE_KYBER))
  1488. #define HAVE_LOCAL_RNG
  1489. static THREAD_LS_T WC_RNG gRng;
  1490. #define GLOBAL_RNG &gRng
  1491. #else
  1492. #define GLOBAL_RNG NULL
  1493. #endif
  1494. #if defined(HAVE_ED25519) || defined(HAVE_CURVE25519) || \
  1495. defined(HAVE_CURVE448) || defined(HAVE_ED448) || \
  1496. defined(HAVE_ECC) || !defined(NO_DH) || \
  1497. !defined(NO_RSA) || defined(HAVE_SCRYPT) || \
  1498. defined(WOLFSSL_HAVE_KYBER)
  1499. #define BENCH_ASYM
  1500. #endif
  1501. #if defined(BENCH_ASYM)
  1502. #if defined(HAVE_ECC) || !defined(NO_RSA) || !defined(NO_DH) || \
  1503. defined(HAVE_CURVE25519) || defined(HAVE_ED25519) || \
  1504. defined(HAVE_CURVE448) || defined(HAVE_ED448) || \
  1505. defined(WOLFSSL_HAVE_KYBER)
  1506. static const char* bench_result_words2[][5] = {
  1507. #ifdef BENCH_MICROSECOND
  1508. { "ops took", "μsec" , "avg" , "ops/μsec", NULL }, /* 0 English
  1509. for μsec */
  1510. #else
  1511. { "ops took", "sec" , "avg" , "ops/sec", NULL }, /* 0 English */
  1512. #endif
  1513. #ifndef NO_MULTIBYTE_PRINT
  1514. { "回処理を", "秒で実施", "平均", "処理/秒", NULL }, /* 1 Japanese */
  1515. #endif
  1516. };
  1517. #endif
  1518. #endif
  1519. #ifdef WOLFSSL_CAAM
  1520. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  1521. #ifdef WOLFSSL_SECO_CAAM
  1522. #define SECO_MAX_UPDATES 10000
  1523. #define SECO_BENCHMARK_NONCE 0x7777
  1524. #define SECO_KEY_STORE_ID 1
  1525. #endif
  1526. static THREAD_LS_T int devId = WOLFSSL_CAAM_DEVID;
  1527. #else
  1528. #ifdef WC_USE_DEVID
  1529. static THREAD_LS_T int devId = WC_USE_DEVID;
  1530. #else
  1531. static THREAD_LS_T int devId = INVALID_DEVID;
  1532. #endif
  1533. #endif
  1534. /* Asynchronous helper macros */
  1535. #ifdef WC_ENABLE_BENCH_THREADING
  1536. typedef struct ThreadData {
  1537. pthread_t thread_id;
  1538. } ThreadData;
  1539. static ThreadData* g_threadData;
  1540. static volatile int g_threadCount;
  1541. #endif
  1542. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_CAAM) || defined(WC_USE_DEVID)
  1543. #ifndef NO_HW_BENCH
  1544. #define BENCH_DEVID
  1545. #endif
  1546. #ifndef HAVE_RENESAS_SYNC
  1547. #define BENCH_DEVID_GET_NAME(useDeviceID) (useDeviceID) ? "HW" : "SW"
  1548. #else
  1549. #define BENCH_DEVID_GET_NAME(useDeviceID) ""
  1550. #endif
  1551. #else
  1552. #define BENCH_DEVID_GET_NAME(useDeviceID) ""
  1553. #endif
  1554. #ifdef WOLFSSL_ASYNC_CRYPT
  1555. static WOLF_EVENT_QUEUE eventQueue;
  1556. #define BENCH_ASYNC_GET_DEV(obj) (&(obj)->asyncDev)
  1557. #define BENCH_MAX_PENDING (WOLF_ASYNC_MAX_PENDING)
  1558. static int bench_async_check(int* ret, WC_ASYNC_DEV* asyncDev,
  1559. int callAgain, int* times, int limit, int* pending)
  1560. {
  1561. int allowNext = 0;
  1562. /* this state can be set from a different thread */
  1563. WOLF_EVENT_STATE state = asyncDev->event.state;
  1564. /* if algo doesn't require calling again then use this flow */
  1565. if (state == WOLF_EVENT_STATE_DONE) {
  1566. if (callAgain) {
  1567. /* needs called again, so allow it and handle completion in
  1568. * bench_async_handle */
  1569. allowNext = 1;
  1570. }
  1571. else {
  1572. *ret = asyncDev->event.ret;
  1573. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  1574. (*times)++;
  1575. if (*pending > 0) /* to support case where async blocks */
  1576. (*pending)--;
  1577. if ((*times + *pending) < limit)
  1578. allowNext = 1;
  1579. }
  1580. }
  1581. /* if slot is available and we haven't reached limit, start another */
  1582. else if (state == WOLF_EVENT_STATE_READY && (*times + *pending) < limit) {
  1583. allowNext = 1;
  1584. }
  1585. return allowNext;
  1586. }
  1587. static int bench_async_handle(int* ret, WC_ASYNC_DEV* asyncDev,
  1588. int callAgain, int* times, int* pending)
  1589. {
  1590. WOLF_EVENT_STATE state = asyncDev->event.state;
  1591. if (*ret == WC_PENDING_E) {
  1592. if (state == WOLF_EVENT_STATE_DONE) {
  1593. *ret = asyncDev->event.ret;
  1594. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  1595. (*times)++;
  1596. (*pending)--;
  1597. }
  1598. else {
  1599. (*pending)++;
  1600. *ret = wc_AsyncHandle(asyncDev, &eventQueue,
  1601. callAgain ? WC_ASYNC_FLAG_CALL_AGAIN : WC_ASYNC_FLAG_NONE);
  1602. }
  1603. }
  1604. else if (*ret >= 0) {
  1605. *ret = asyncDev->event.ret;
  1606. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  1607. (*times)++;
  1608. if (*pending > 0) /* to support case where async blocks */
  1609. (*pending)--;
  1610. }
  1611. return (*ret >= 0) ? 1 : 0;
  1612. }
  1613. static WC_INLINE int bench_async_poll(int* pending)
  1614. {
  1615. int ret, asyncDone = 0;
  1616. ret = wolfAsync_EventQueuePoll(&eventQueue, NULL, NULL, 0,
  1617. WOLF_POLL_FLAG_CHECK_HW, &asyncDone);
  1618. if (ret != 0) {
  1619. printf("%sAsync poll failed %d\n", err_prefix, ret);
  1620. return ret;
  1621. }
  1622. if (asyncDone == 0) {
  1623. #ifndef WC_NO_ASYNC_THREADING
  1624. /* give time to other threads */
  1625. wc_AsyncThreadYield();
  1626. #endif
  1627. }
  1628. (void)pending;
  1629. return asyncDone;
  1630. }
  1631. #else
  1632. #define BENCH_MAX_PENDING 1
  1633. #define BENCH_ASYNC_GET_DEV(obj) NULL
  1634. static WC_INLINE int bench_async_check(int* ret, void* asyncDev,
  1635. int callAgain, int* times, int limit, int* pending)
  1636. {
  1637. (void)ret;
  1638. (void)asyncDev;
  1639. (void)callAgain;
  1640. (void)times;
  1641. (void)limit;
  1642. (void)pending;
  1643. return 1;
  1644. }
  1645. static WC_INLINE int bench_async_handle(int* ret, void* asyncDev,
  1646. int callAgain, int* times, int* pending)
  1647. {
  1648. (void)asyncDev;
  1649. (void)callAgain;
  1650. (void)pending;
  1651. if (*ret >= 0) {
  1652. /* operation completed */
  1653. (*times)++;
  1654. return 1;
  1655. }
  1656. return 0;
  1657. }
  1658. #define bench_async_poll(p) WC_DO_NOTHING
  1659. #endif /* WOLFSSL_ASYNC_CRYPT */
  1660. /* maximum runtime for each benchmark */
  1661. #ifndef BENCH_MIN_RUNTIME_SEC
  1662. #define BENCH_MIN_RUNTIME_SEC 1.0F
  1663. #endif
  1664. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1665. #define AES_AUTH_TAG_SZ 16
  1666. #define BENCH_CIPHER_ADD AES_AUTH_TAG_SZ
  1667. static word32 aesAuthAddSz = AES_AUTH_ADD_SZ;
  1668. #if !defined(AES_AAD_OPTIONS_DEFAULT)
  1669. #if !defined(NO_MAIN_DRIVER)
  1670. #define AES_AAD_OPTIONS_DEFAULT 0x1U
  1671. #else
  1672. #define AES_AAD_OPTIONS_DEFAULT 0x3U
  1673. #endif
  1674. #endif
  1675. #define AES_AAD_STRING(s) \
  1676. (aesAuthAddSz == 0 ? (s "-no_AAD") : \
  1677. (aesAuthAddSz == AES_AUTH_ADD_SZ ? (s) : (s "-custom")))
  1678. enum en_aad_options {
  1679. AAD_SIZE_DEFAULT = 0x1U,
  1680. AAD_SIZE_ZERO = 0x2U,
  1681. AAD_SIZE_CUSTOM = 0x4U,
  1682. };
  1683. static word32 aes_aad_options = AES_AAD_OPTIONS_DEFAULT;
  1684. static word32 aes_aad_size = 0;
  1685. static void bench_aes_aad_options_wrap(void (*fn)(int), int i)
  1686. {
  1687. word32 aesAuthAddSz_orig = aesAuthAddSz;
  1688. word32 options = aes_aad_options;
  1689. while(options) {
  1690. if (options & AAD_SIZE_DEFAULT) {
  1691. aesAuthAddSz = AES_AUTH_ADD_SZ;
  1692. options &= ~(word32)AAD_SIZE_DEFAULT;
  1693. }
  1694. else if (options & AAD_SIZE_ZERO) {
  1695. aesAuthAddSz = 0;
  1696. options &= ~(word32)AAD_SIZE_ZERO;
  1697. }
  1698. else if (options & AAD_SIZE_CUSTOM) {
  1699. aesAuthAddSz = aes_aad_size;
  1700. options &= ~(word32)AAD_SIZE_CUSTOM;
  1701. }
  1702. fn(i);
  1703. aesAuthAddSz = aesAuthAddSz_orig;
  1704. }
  1705. }
  1706. #endif
  1707. #ifndef BENCH_CIPHER_ADD
  1708. #define BENCH_CIPHER_ADD 0
  1709. #endif
  1710. /* use kB instead of mB for embedded benchmarking */
  1711. #ifdef BENCH_EMBEDDED
  1712. #ifndef BENCH_NTIMES
  1713. #define BENCH_NTIMES 2
  1714. #endif
  1715. #ifndef BENCH_AGREETIMES
  1716. #define BENCH_AGREETIMES 2
  1717. #endif
  1718. enum BenchmarkBounds {
  1719. scryptCnt = 1,
  1720. ntimes = BENCH_NTIMES,
  1721. genTimes = BENCH_MAX_PENDING,
  1722. agreeTimes = BENCH_AGREETIMES
  1723. };
  1724. /* how many kB to test (en/de)cryption */
  1725. #define NUM_BLOCKS 25
  1726. #define BENCH_SIZE (1024uL)
  1727. #else
  1728. #ifndef BENCH_NTIMES
  1729. #define BENCH_NTIMES 100
  1730. #endif
  1731. #ifndef BENCH_AGREETIMES
  1732. #define BENCH_AGREETIMES 100
  1733. #endif
  1734. enum BenchmarkBounds {
  1735. scryptCnt = 10,
  1736. ntimes = BENCH_NTIMES,
  1737. genTimes = BENCH_MAX_PENDING, /* must be at least BENCH_MAX_PENDING */
  1738. agreeTimes = BENCH_AGREETIMES
  1739. };
  1740. /* how many megs to test (en/de)cryption */
  1741. #define NUM_BLOCKS 5
  1742. #define BENCH_SIZE (1024*1024uL)
  1743. #endif
  1744. static int numBlocks = NUM_BLOCKS;
  1745. static word32 bench_size = BENCH_SIZE;
  1746. static int base2 = 1;
  1747. static int digest_stream = 1;
  1748. #ifdef MULTI_VALUE_STATISTICS
  1749. static int minimum_runs = 0;
  1750. #endif
  1751. #ifndef NO_RSA
  1752. /* Don't measure RSA sign/verify by default */
  1753. static int rsa_sign_verify = 0;
  1754. #endif
  1755. #ifndef NO_DH
  1756. /* Use the FFDHE parameters */
  1757. static int use_ffdhe = 0;
  1758. #endif
  1759. /* Don't print out in CSV format by default */
  1760. static int csv_format = 0;
  1761. #ifdef WOLFSSL_XILINX_CRYPT_VERSAL
  1762. /* Versal PLM maybe prints an error message to the same console.
  1763. * In order to not mix those outputs up, sleep a little while
  1764. * before erroring out.
  1765. */
  1766. #define SLEEP_ON_ERROR(ret) do{ if (ret != 0) { sleep(1); } }while(0)
  1767. #else
  1768. #define SLEEP_ON_ERROR(ret) do{ /* noop */ }while(0)
  1769. #endif
  1770. /* globals for cipher tests */
  1771. static THREAD_LS_T byte* bench_plain = NULL;
  1772. static THREAD_LS_T byte* bench_cipher = NULL;
  1773. #ifndef NO_FILESYSTEM
  1774. static THREAD_LS_T char* hash_input = NULL;
  1775. static THREAD_LS_T char* cipher_input = NULL;
  1776. #endif
  1777. static const XGEN_ALIGN byte bench_key_buf[] =
  1778. {
  1779. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  1780. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  1781. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67,
  1782. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  1783. 0xf0,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,
  1784. 0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff,
  1785. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  1786. 0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,
  1787. };
  1788. static const XGEN_ALIGN byte bench_iv_buf[] =
  1789. {
  1790. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  1791. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1792. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  1793. };
  1794. static THREAD_LS_T byte* bench_key = NULL;
  1795. static THREAD_LS_T byte* bench_iv = NULL;
  1796. #ifdef HAVE_RENESAS_SYNC
  1797. static THREAD_LS_T byte* bench_key1 = NULL;
  1798. static THREAD_LS_T byte* bench_key2 = NULL;
  1799. #endif
  1800. #ifdef WOLFSSL_STATIC_MEMORY
  1801. #ifdef WOLFSSL_STATIC_MEMORY_TEST_SZ
  1802. static byte gBenchMemory[WOLFSSL_STATIC_MEMORY_TEST_SZ];
  1803. #elif defined(BENCH_EMBEDDED)
  1804. static byte gBenchMemory[50000];
  1805. #else
  1806. static byte gBenchMemory[400000];
  1807. #endif
  1808. #endif
  1809. /* This code handles cases with systems where static (non cost) ram variables
  1810. aren't properly initialized with data */
  1811. static void benchmark_static_init(int force)
  1812. {
  1813. static int gBenchStaticInit = 0;
  1814. if (gBenchStaticInit == 0 || force) {
  1815. gBenchStaticInit = 1;
  1816. /* Init static variables */
  1817. numBlocks = NUM_BLOCKS;
  1818. bench_size = BENCH_SIZE;
  1819. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  1820. aesAuthAddSz = AES_AUTH_ADD_SZ;
  1821. aes_aad_options = AES_AAD_OPTIONS_DEFAULT;
  1822. aes_aad_size = 0;
  1823. #endif
  1824. base2 = 1;
  1825. digest_stream = 1;
  1826. #ifdef MULTI_VALUE_STATISTICS
  1827. minimum_runs = 0;
  1828. #endif
  1829. bench_all = 1;
  1830. bench_cipher_algs = 0;
  1831. bench_digest_algs = 0;
  1832. bench_mac_algs = 0;
  1833. bench_kdf_algs = 0;
  1834. bench_asym_algs = 0;
  1835. bench_pq_asym_algs = 0;
  1836. bench_other_algs = 0;
  1837. bench_pq_hash_sig_algs = 0;
  1838. csv_format = 0;
  1839. }
  1840. }
  1841. /*****************************************************************************/
  1842. /* Begin Stats Functions */
  1843. /*****************************************************************************/
  1844. typedef enum bench_stat_type {
  1845. BENCH_STAT_ASYM,
  1846. BENCH_STAT_SYM,
  1847. BENCH_STAT_IGNORE,
  1848. } bench_stat_type_t;
  1849. #ifdef WC_BENCH_TRACK_STATS
  1850. static int gPrintStats = 0;
  1851. #ifdef WC_ENABLE_BENCH_THREADING
  1852. static pthread_mutex_t bench_lock = PTHREAD_MUTEX_INITIALIZER;
  1853. #endif
  1854. #ifndef BENCH_MAX_NAME_SZ
  1855. #define BENCH_MAX_NAME_SZ 24
  1856. #endif
  1857. typedef struct bench_stats {
  1858. struct bench_stats* next;
  1859. struct bench_stats* prev;
  1860. char algo[BENCH_MAX_NAME_SZ+1]; /* may not be static, so make copy */
  1861. const char* desc;
  1862. double perfsec;
  1863. int strength;
  1864. int useDeviceID;
  1865. int finishCount;
  1866. bench_stat_type_t type;
  1867. int lastRet;
  1868. const char* perftype;
  1869. } bench_stats_t;
  1870. static bench_stats_t* bench_stats_head;
  1871. static bench_stats_t* bench_stats_tail;
  1872. static bench_stats_t* bench_stats_add(bench_stat_type_t type,
  1873. const char* algo, int strength, const char* desc, int useDeviceID,
  1874. double perfsec, const char* perftype, int ret)
  1875. {
  1876. bench_stats_t* bstat = NULL;
  1877. #ifdef WC_ENABLE_BENCH_THREADING
  1878. /* protect bench_stats_head and bench_stats_tail access */
  1879. THREAD_CHECK_RET(pthread_mutex_lock(&bench_lock));
  1880. #endif
  1881. if (algo != NULL) {
  1882. /* locate existing in list */
  1883. for (bstat = bench_stats_head; bstat != NULL; bstat = bstat->next) {
  1884. /* match based on algo, strength and desc */
  1885. if (XSTRNCMP(bstat->algo, algo, BENCH_MAX_NAME_SZ) == 0 &&
  1886. bstat->strength == strength &&
  1887. bstat->desc == desc &&
  1888. bstat->useDeviceID == useDeviceID) {
  1889. break;
  1890. }
  1891. }
  1892. }
  1893. if (bstat == NULL) {
  1894. /* allocate new and put on list */
  1895. bstat = (bench_stats_t*)XMALLOC(sizeof(bench_stats_t), NULL,
  1896. DYNAMIC_TYPE_INFO);
  1897. if (bstat) {
  1898. XMEMSET(bstat, 0, sizeof(bench_stats_t));
  1899. /* add to list */
  1900. bstat->next = NULL;
  1901. if (bench_stats_tail == NULL) {
  1902. bench_stats_head = bstat;
  1903. }
  1904. else {
  1905. bench_stats_tail->next = bstat;
  1906. bstat->prev = bench_stats_tail;
  1907. }
  1908. bench_stats_tail = bstat; /* add to the end either way */
  1909. }
  1910. }
  1911. if (bstat) {
  1912. bstat->type = type;
  1913. if (algo != NULL)
  1914. XSTRNCPY(bstat->algo, algo, BENCH_MAX_NAME_SZ);
  1915. bstat->strength = strength;
  1916. bstat->desc = desc;
  1917. bstat->useDeviceID = useDeviceID;
  1918. bstat->perfsec += perfsec;
  1919. bstat->finishCount++;
  1920. bstat->perftype = perftype;
  1921. if (bstat->lastRet > ret)
  1922. bstat->lastRet = ret; /* track last error */
  1923. }
  1924. #ifdef WC_ENABLE_BENCH_THREADING
  1925. THREAD_CHECK_RET(pthread_mutex_unlock(&bench_lock));
  1926. #endif
  1927. return bstat;
  1928. }
  1929. void bench_stats_print(void)
  1930. {
  1931. bench_stats_t* bstat;
  1932. int digits;
  1933. #ifdef WC_ENABLE_BENCH_THREADING
  1934. /* protect bench_stats_head and bench_stats_tail access */
  1935. THREAD_CHECK_RET(pthread_mutex_lock(&bench_lock));
  1936. #endif
  1937. #ifdef BENCH_MICROSECOND
  1938. digits = 5;
  1939. #else
  1940. digits = 3;
  1941. #endif
  1942. for (bstat = bench_stats_head; bstat != NULL; ) {
  1943. if (bstat->type == BENCH_STAT_SYM) {
  1944. printf("%-16s%s " FLT_FMT_PREC2 " %s/" WOLFSSL_FIXED_TIME_UNIT
  1945. "\n", bstat->desc,
  1946. BENCH_DEVID_GET_NAME(bstat->useDeviceID),
  1947. FLT_FMT_PREC2_ARGS(8, digits, bstat->perfsec),
  1948. base2 ? "MB" : "mB");
  1949. }
  1950. else {
  1951. printf("%-5s %4d %-9s %s " FLT_FMT_PREC " ops/"
  1952. WOLFSSL_FIXED_TIME_UNIT "ec\n",
  1953. bstat->algo, bstat->strength, bstat->desc,
  1954. BENCH_DEVID_GET_NAME(bstat->useDeviceID),
  1955. FLT_FMT_PREC_ARGS(digits, bstat->perfsec));
  1956. }
  1957. bstat = bstat->next;
  1958. }
  1959. #ifdef WC_ENABLE_BENCH_THREADING
  1960. THREAD_CHECK_RET(pthread_mutex_unlock(&bench_lock));
  1961. #endif
  1962. }
  1963. #endif /* WC_BENCH_TRACK_STATS */
  1964. static WC_INLINE void bench_stats_init(void)
  1965. {
  1966. #ifdef WC_BENCH_TRACK_STATS
  1967. bench_stats_head = NULL;
  1968. bench_stats_tail = NULL;
  1969. #endif
  1970. INIT_CYCLE_COUNTER
  1971. }
  1972. static WC_INLINE void bench_stats_start(int* count, double* start)
  1973. {
  1974. *count = 0;
  1975. *start = current_time(1);
  1976. #ifdef WOLFSSL_ESPIDF
  1977. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  1978. ESP_LOGI(TAG, "bench_stats_start total_cycles = %llu, start=" FLT_FMT,
  1979. total_cycles, FLT_FMT_ARGS(*start) );
  1980. #endif
  1981. BEGIN_ESP_CYCLES
  1982. #else
  1983. BEGIN_INTEL_CYCLES
  1984. #endif
  1985. }
  1986. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  1987. #define bench_stats_start(count, start) do { \
  1988. SAVE_VECTOR_REGISTERS(pr_err( \
  1989. "SAVE_VECTOR_REGISTERS failed for benchmark run."); \
  1990. return; ); \
  1991. bench_stats_start(count, start); \
  1992. } while (0)
  1993. #endif
  1994. static WC_INLINE int bench_stats_check(double start)
  1995. {
  1996. int ret = 0;
  1997. double this_current_time;
  1998. this_current_time = current_time(0); /* get the timestamp, no reset */
  1999. #if defined(DEBUG_WOLFSSL_BENCHMARK_TIMING)
  2000. #if (WOLFSSL_ESPIDF)
  2001. ESP_LOGI(TAG, "bench_stats_check Current time %f, start %f",
  2002. this_current_time, start );
  2003. #endif
  2004. #endif
  2005. ret = ((this_current_time - start) < BENCH_MIN_RUNTIME_SEC
  2006. #ifdef BENCH_MICROSECOND
  2007. * 1000000
  2008. #endif
  2009. );
  2010. return ret;
  2011. }
  2012. /* return text for units and scale the value of blocks as needed */
  2013. static const char* get_blocktype(double* blocks)
  2014. {
  2015. const char* rt;
  2016. #if ( defined(WOLFSSL_BENCHMARK_FIXED_UNITS_G) || \
  2017. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_GB))
  2018. #undef WOLFSSL_FIXED_UNIT
  2019. #define WOLFSSL_FIXED_UNIT "GB"
  2020. *blocks /= (1024UL * 1024UL * 1024UL);
  2021. rt = "GiB";
  2022. #elif (defined(WOLFSSL_BENCHMARK_FIXED_UNITS_M) || \
  2023. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_MB))
  2024. #undef WOLFSSL_FIXED_UNIT
  2025. #define WOLFSSL_FIXED_UNIT "MB"
  2026. *blocks /= (1024UL * 1024UL);
  2027. rt = "MiB";
  2028. #elif (defined(WOLFSSL_BENCHMARK_FIXED_UNITS_K) || \
  2029. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_KB))
  2030. #undef WOLFSSL_FIXED_UNIT
  2031. #define WOLFSSL_FIXED_UNIT "KB"
  2032. *blocks /= 1024;
  2033. rt = "KiB";
  2034. #elif defined (WOLFSSL_BENCHMARK_FIXED_UNITS_B)
  2035. #undef WOLFSSL_FIXED_UNIT
  2036. #define WOLFSSL_FIXED_UNIT "bytes"
  2037. (void)(*blocks); /* no adjustment, just appease compiler for not used */
  2038. rt = "bytes";
  2039. #else
  2040. /* If no user-specified, auto-scale each metric (results vary).
  2041. * Determine if we should show as KB or MB or bytes. No GiB here. */
  2042. if (*blocks > (1024UL * 1024UL)) {
  2043. *blocks /= (1024UL * 1024UL);
  2044. rt = "MiB";
  2045. }
  2046. else if (*blocks > 1024) {
  2047. *blocks /= 1024;
  2048. rt = "KiB";
  2049. }
  2050. else {
  2051. rt = "bytes";
  2052. }
  2053. #endif
  2054. return rt;
  2055. }
  2056. /* return text for units and scale the value of blocks as needed for base2 */
  2057. static const char* get_blocktype_base10(double* blocks)
  2058. {
  2059. const char* rt;
  2060. #if ( defined(WOLFSSL_BENCHMARK_FIXED_UNITS_G) || \
  2061. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_GB))
  2062. *blocks /= (1000UL * 1000UL * 1000UL);
  2063. rt = "GB";
  2064. #elif (defined(WOLFSSL_BENCHMARK_FIXED_UNITS_M) || \
  2065. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_MB))
  2066. *blocks /= (1000UL * 1000UL);
  2067. rt = "MB";
  2068. #elif (defined(WOLFSSL_BENCHMARK_FIXED_UNITS_K) || \
  2069. defined(WOLFSSL_BENCHMARK_FIXED_UNITS_KB))
  2070. *blocks /= (1000UL);
  2071. rt = "KB";
  2072. #elif defined (WOLFSSL_BENCHMARK_FIXED_UNITS_B)
  2073. (void)(*blocks); /* no adjustment, just appease compiler */
  2074. rt = "bytes";
  2075. #else
  2076. /* If not user-specified, auto-scale each metric (results vary).
  2077. * Determine if we should show as KB or MB or bytes */
  2078. if (*blocks > (1000UL * 1000UL)) {
  2079. *blocks /= (1000UL * 1000UL);
  2080. rt = "MB";
  2081. }
  2082. else if (*blocks > 1000) {
  2083. *blocks /= 1000; /* make KB */
  2084. rt = "KB";
  2085. }
  2086. else {
  2087. rt = "bytes";
  2088. }
  2089. #endif
  2090. return rt;
  2091. }
  2092. #ifdef MULTI_VALUE_STATISTICS
  2093. static double wc_sqroot(double in)
  2094. {
  2095. /* do 32 iterations for the sqroot */
  2096. int iter = 32;
  2097. double root = in/3.0;
  2098. if (in < 0.0)
  2099. return -1;
  2100. for (int i=0; i < iter; i++)
  2101. root = (root + in / root) / 2.0;
  2102. return root;
  2103. }
  2104. static void bench_multi_value_stats(double max, double min, double sum,
  2105. double squareSum, int runs)
  2106. {
  2107. double mean = 0;
  2108. double sd = 0;
  2109. char msg[WC_BENCH_MAX_LINE_LEN];
  2110. const char** word = bench_result_words3[lng_index];
  2111. XMEMSET(msg, 0, sizeof(msg));
  2112. mean = sum / runs;
  2113. /* Calculating standard deviation */
  2114. sd = (squareSum / runs) - (mean * mean);
  2115. sd = wc_sqroot(sd);
  2116. if (csv_format == 1) {
  2117. (void)XSNPRINTF(msg, sizeof(msg), FLT_FMT_PREC2 ","
  2118. FLT_FMT_PREC2 "," FLT_FMT_PREC2 "," FLT_FMT_PREC2 ",\n",
  2119. FLT_FMT_PREC2_ARGS(3, 3, max),
  2120. FLT_FMT_PREC2_ARGS(3, 3, min),
  2121. FLT_FMT_PREC2_ARGS(3, 3, mean),
  2122. FLT_FMT_PREC2_ARGS(3, 3, sd));
  2123. }
  2124. else{
  2125. (void)XSNPRINTF(msg, sizeof(msg), ", %s " FLT_FMT_PREC2 " "
  2126. WOLFSSL_FIXED_TIME_UNIT ", %s " FLT_FMT_PREC2 " "
  2127. WOLFSSL_FIXED_TIME_UNIT ", %s " FLT_FMT_PREC2 " "
  2128. WOLFSSL_FIXED_TIME_UNIT ", %s " FLT_FMT_PREC2 " "
  2129. WOLFSSL_FIXED_TIME_UNIT "\n",
  2130. word[0], FLT_FMT_PREC2_ARGS(3, 3, max),
  2131. word[1], FLT_FMT_PREC2_ARGS(3, 3, min),
  2132. word[2], FLT_FMT_PREC2_ARGS(3, 3, mean),
  2133. word[3], FLT_FMT_PREC2_ARGS(3, 3, sd));
  2134. }
  2135. printf("%s", msg);
  2136. #ifndef WOLFSSL_SGX
  2137. XFFLUSH(stdout);
  2138. #endif
  2139. }
  2140. #endif
  2141. /* countSz is number of bytes that 1 count represents. Normally bench_size,
  2142. * except for AES direct that operates on AES_BLOCK_SIZE blocks */
  2143. static void bench_stats_sym_finish(const char* desc, int useDeviceID,
  2144. int count, word32 countSz,
  2145. double start, int ret)
  2146. {
  2147. double total, persec = 0, blocks = (double)count;
  2148. const char* blockType;
  2149. char msg[WC_BENCH_MAX_LINE_LEN];
  2150. const char** word = bench_result_words1[lng_index];
  2151. static int sym_header_printed = 0;
  2152. XMEMSET(msg, 0, sizeof(msg));
  2153. #ifdef WOLFSSL_ESPIDF
  2154. END_ESP_CYCLES
  2155. #else
  2156. END_INTEL_CYCLES
  2157. #endif
  2158. total = current_time(0) - start;
  2159. #if defined(WOLFSSL_ESPIDF) && defined(DEBUG_WOLFSSL_BENCHMARK_TIMING)
  2160. ESP_LOGI(TAG, "%s total_cycles = %llu", desc, total_cycles);
  2161. #endif
  2162. #ifdef LINUX_RUSAGE_UTIME
  2163. check_for_excessive_stime(desc, "");
  2164. #endif
  2165. /* calculate actual bytes */
  2166. blocks *= countSz;
  2167. if (csv_format == 1) {
  2168. /* only print out header once */
  2169. if (sym_header_printed == 0) {
  2170. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2171. /* machine parseable CSV */
  2172. #ifdef HAVE_GET_CYCLES
  2173. printf("%s", "\"sym\",Algorithm,HW/SW,bytes_total,"
  2174. WOLFSSL_FIXED_TIME_UNIT "econds_total,"
  2175. WOLFSSL_FIXED_UNIT "/" WOLFSSL_FIXED_TIME_UNIT
  2176. ",cycles_total,Cycles per byte,");
  2177. #else
  2178. printf("%s", "\"sym\",Algorithm,HW/SW,bytes_total,"
  2179. WOLFSSL_FIXED_TIME_UNIT "econds_total,"
  2180. WOLFSSL_FIXED_UNIT "/" WOLFSSL_FIXED_TIME_UNIT
  2181. ",cycles_total,");
  2182. #endif
  2183. #else
  2184. /* normal CSV */
  2185. #ifdef BENCH_DEVID
  2186. #define BENCH_DEVID_COLUMN_HEADER "HW/SW,"
  2187. #else
  2188. #define BENCH_DEVID_COLUMN_HEADER
  2189. #endif
  2190. #ifdef HAVE_GET_CYCLES
  2191. printf("\n\nSymmetric Ciphers:\n\n");
  2192. printf("Algorithm,"
  2193. BENCH_DEVID_COLUMN_HEADER
  2194. WOLFSSL_FIXED_UNIT "/" WOLFSSL_FIXED_TIME_UNIT
  2195. ",Cycles per byte,");
  2196. #else
  2197. printf("\n\nSymmetric Ciphers:\n\n");
  2198. printf("Algorithm,"
  2199. BENCH_DEVID_COLUMN_HEADER
  2200. WOLFSSL_FIXED_UNIT "/" WOLFSSL_FIXED_TIME_UNIT ",");
  2201. #endif
  2202. #endif
  2203. #ifdef MULTI_VALUE_STATISTICS
  2204. printf("max duration,min duration,mean duration,sd,\n");
  2205. #else
  2206. printf("\n");
  2207. #endif
  2208. sym_header_printed = 1;
  2209. }
  2210. }
  2211. /* determine if we have fixed units, or auto-scale bits or bytes for units.
  2212. * note that the blockType text is assigned AND the blocks param is scaled.
  2213. */
  2214. if (base2) {
  2215. blockType = get_blocktype(&blocks);
  2216. }
  2217. else {
  2218. blockType = get_blocktype_base10(&blocks);
  2219. }
  2220. /* calculate blocks per second */
  2221. if (total > 0) {
  2222. persec = (1 / total) * blocks;
  2223. }
  2224. SLEEP_ON_ERROR(ret);
  2225. /* format and print to terminal */
  2226. if (csv_format == 1) {
  2227. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2228. #ifdef WOLFSSL_ESPIDF
  2229. unsigned long bytes_processed =
  2230. (unsigned long)count * (unsigned long)countSz;
  2231. #else
  2232. word64 bytes_processed = (word64)count * (word64)countSz;
  2233. #endif
  2234. /* note this codepath brings in all the fields from the non-CSV case. */
  2235. #ifdef WOLFSSL_ESPIDF
  2236. #ifdef HAVE_GET_CYCLES
  2237. (void)XSNPRINTF(msg, sizeof(msg),
  2238. "sym,%s,%s,%lu," FLT_FMT "," FLT_FMT ",%lu,", desc,
  2239. BENCH_DEVID_GET_NAME(useDeviceID),
  2240. bytes_processed, FLT_FMT_ARGS(total),
  2241. FLT_FMT_ARGS(persec),
  2242. (long unsigned int) total_cycles);
  2243. #else
  2244. #warning "HAVE_GET_CYCLES should be defined for WOLFSSL_ESPIDF"
  2245. #endif
  2246. /* implement other architectures here */
  2247. #else
  2248. #ifdef HAVE_GET_CYCLES
  2249. (void)XSNPRINTF(msg, sizeof(msg),
  2250. "sym,%s,%s,%lu," FLT_FMT "," FLT_FMT ",%lu,", desc,
  2251. BENCH_DEVID_GET_NAME(useDeviceID),
  2252. bytes_processed, FLT_FMT_ARGS(total),
  2253. FLT_FMT_ARGS(persec), total_cycles);
  2254. #else
  2255. (void)XSNPRINTF(msg, sizeof(msg),
  2256. "sym,%s,%s,%lu," FLT_FMT "," FLT_FMT ",", desc,
  2257. BENCH_DEVID_GET_NAME(useDeviceID),
  2258. bytes_processed, FLT_FMT_ARGS(total),
  2259. FLT_FMT_ARGS(persec));
  2260. #endif
  2261. #endif
  2262. #elif defined(BENCH_DEVID)
  2263. (void)XSNPRINTF(msg, sizeof(msg), "%s,%s," FLT_FMT ",", desc,
  2264. BENCH_DEVID_GET_NAME(useDeviceID), FLT_FMT_ARGS(persec));
  2265. #else
  2266. (void)XSNPRINTF(msg, sizeof(msg), "%s," FLT_FMT ",", desc,
  2267. FLT_FMT_ARGS(persec));
  2268. #endif
  2269. #ifdef WOLFSSL_ESPIDF
  2270. SHOW_ESP_CYCLES_CSV(msg, sizeof(msg), countSz);
  2271. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  2272. ESP_LOGI(TAG, "bench_stats_sym_finish total_cycles = %llu",
  2273. total_cycles);
  2274. #endif
  2275. /* implement other cycle counters here */
  2276. #else
  2277. /* the default cycle counter is Intel */
  2278. SHOW_INTEL_CYCLES_CSV(msg, sizeof(msg), (unsigned)countSz);
  2279. #endif
  2280. } /* if (csv_format == 1) */
  2281. else {
  2282. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2283. #ifdef HAVE_GET_CYCLES
  2284. (void)XSNPRINTF(msg, sizeof(msg),
  2285. "%-24s%s " FLT_FMT_PREC2 " %s %s " FLT_FMT_PREC2 " %s, "
  2286. FLT_FMT_PREC2 " %s/" WOLFSSL_FIXED_TIME_UNIT ", %lu cycles,",
  2287. desc, BENCH_DEVID_GET_NAME(useDeviceID),
  2288. FLT_FMT_PREC2_ARGS(5, 0, blocks), blockType,
  2289. word[0], FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2290. FLT_FMT_PREC2_ARGS(8, 3, persec), blockType,
  2291. (unsigned long) total_cycles);
  2292. #else
  2293. (void)XSNPRINTF(msg, sizeof(msg),
  2294. "%-24s%s " FLT_FMT_PREC2 " %s %s " FLT_FMT_PREC2 " %s, "
  2295. FLT_FMT_PREC2 " %s/" WOLFSSL_FIXED_TIME_UNIT ",",
  2296. desc, BENCH_DEVID_GET_NAME(useDeviceID),
  2297. FLT_FMT_PREC2_ARGS(5, 0, blocks), blockType,
  2298. word[0], FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2299. FLT_FMT_PREC2_ARGS(8, 3, persec), blockType);
  2300. #endif /* HAVE_GET_CYCLES */
  2301. #else
  2302. (void)XSNPRINTF(msg, sizeof(msg),
  2303. "%-24s%s " FLT_FMT_PREC2 " %s %s " FLT_FMT_PREC2 " %s, "
  2304. FLT_FMT_PREC2 " %s/" WOLFSSL_FIXED_TIME_UNIT,
  2305. desc, BENCH_DEVID_GET_NAME(useDeviceID),
  2306. FLT_FMT_PREC2_ARGS(5, 0, blocks), blockType,
  2307. word[0], FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2308. FLT_FMT_PREC2_ARGS(8, 3, persec), blockType);
  2309. #endif
  2310. #ifdef WOLFSSL_ESPIDF
  2311. SHOW_ESP_CYCLES(msg, sizeof(msg), countSz);
  2312. /* implement other architecture cycle counters here */
  2313. #else
  2314. SHOW_INTEL_CYCLES(msg, sizeof(msg), (unsigned)countSz);
  2315. #endif
  2316. } /* not CSV format */
  2317. printf("%s", msg);
  2318. /* show errors */
  2319. if (ret < 0) {
  2320. printf("%sBenchmark %s failed: %d\n", err_prefix, desc, ret);
  2321. }
  2322. #ifndef WOLFSSL_SGX
  2323. XFFLUSH(stdout);
  2324. #endif
  2325. #ifdef WC_BENCH_TRACK_STATS
  2326. /* Add to thread stats */
  2327. bench_stats_add(BENCH_STAT_SYM, desc, 0, desc, useDeviceID, persec,
  2328. blockType, ret);
  2329. #endif
  2330. (void)useDeviceID;
  2331. (void)ret;
  2332. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  2333. RESTORE_VECTOR_REGISTERS();
  2334. #endif
  2335. TEST_SLEEP();
  2336. } /* bench_stats_sym_finish */
  2337. #ifdef BENCH_ASYM
  2338. #if defined(HAVE_ECC) || !defined(NO_RSA) || !defined(NO_DH) || \
  2339. defined(HAVE_CURVE25519) || defined(HAVE_ED25519) || \
  2340. defined(HAVE_CURVE448) || defined(HAVE_ED448) || \
  2341. defined(WOLFSSL_HAVE_KYBER)
  2342. static void bench_stats_asym_finish_ex(const char* algo, int strength,
  2343. const char* desc, const char* desc_extra, int useDeviceID, int count,
  2344. double start, int ret)
  2345. {
  2346. double total, each = 0, opsSec, milliEach;
  2347. const char **word = bench_result_words2[lng_index];
  2348. #ifdef WC_BENCH_TRACK_STATS
  2349. const char* kOpsSec = "Ops/Sec";
  2350. #endif
  2351. char msg[256];
  2352. static int asym_header_printed = 0;
  2353. #ifdef BENCH_MICROSECOND
  2354. const int digits = 5;
  2355. #else
  2356. const int digits = 3;
  2357. #endif
  2358. XMEMSET(msg, 0, sizeof(msg));
  2359. total = current_time(0) - start;
  2360. #ifdef LINUX_RUSAGE_UTIME
  2361. check_for_excessive_stime(desc, desc_extra);
  2362. #endif
  2363. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2364. #ifdef WOLFSSL_ESPIDF
  2365. END_ESP_CYCLES
  2366. #else
  2367. END_INTEL_CYCLES
  2368. #endif
  2369. #endif
  2370. /* some sanity checks on the final numbers */
  2371. if (count > 0) {
  2372. each = total / count; /* per second */
  2373. }
  2374. else {
  2375. count = 0;
  2376. each = 0;
  2377. }
  2378. if (total > 0) {
  2379. opsSec = count / total; /* ops second */
  2380. }
  2381. else {
  2382. opsSec = 0;
  2383. }
  2384. #ifdef BENCH_MICROSECOND
  2385. milliEach = each / 1000; /* milliseconds */
  2386. #else
  2387. milliEach = each * 1000; /* milliseconds */
  2388. #endif
  2389. SLEEP_ON_ERROR(ret);
  2390. #ifdef MULTI_VALUE_STATISTICS /* Print without avg ms */
  2391. (void)milliEach;
  2392. /* format and print to terminal */
  2393. if (csv_format == 1) {
  2394. /* only print out header once */
  2395. if (asym_header_printed == 0) {
  2396. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2397. #ifdef HAVE_GET_CYCLES
  2398. printf("%s", "\"asym\",Algorithm,key size,operation,ops/"
  2399. WOLFSSL_FIXED_TIME_UNIT "ec,ops," WOLFSSL_FIXED_TIME_UNIT
  2400. "ecs,cycles,cycles/op,");
  2401. #else
  2402. printf("%s", "\"asym\",Algorithm,key size,operation,ops/"
  2403. WOLFSSL_FIXED_TIME_UNIT "ec,ops," WOLFSSL_FIXED_TIME_UNIT
  2404. "ecs,");
  2405. #endif
  2406. #else
  2407. printf("\n%sAsymmetric Ciphers:\n\n", info_prefix);
  2408. printf("%sAlgorithm,key size,operation,ops/"
  2409. WOLFSSL_FIXED_TIME_UNIT "ec,", info_prefix);
  2410. #endif
  2411. printf("max duration,min duration,mean duration,sd,\n");
  2412. asym_header_printed = 1;
  2413. }
  2414. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2415. #ifdef HAVE_GET_CYCLES
  2416. (void)XSNPRINTF(msg, sizeof(msg),
  2417. "asym,%s,%d,%s%s," FLT_FMT_PREC ",%d,"
  2418. FLT_FMT ",%lu," FLT_FMT_PREC STATS_CLAUSE_SEPARATOR,
  2419. algo, strength, desc, desc_extra,
  2420. FLT_FMT_PREC_ARGS(digits, opsSec),
  2421. count, FLT_FMT_ARGS(total), (unsigned long)total_cycles,
  2422. FLT_FMT_PREC_ARGS(6,
  2423. (double)total_cycles / (double)count));
  2424. #else
  2425. (void)XSNPRINTF(msg, sizeof(msg),
  2426. "asym,%s,%d,%s%s," FLT_FMT_PREC ",%d,"
  2427. FLT_FMT STATS_CLAUSE_SEPARATOR,
  2428. algo, strength, desc, desc_extra,
  2429. FLT_FMT_PREC_ARGS(digits, opsSec),
  2430. count, FLT_FMT_ARGS(total));
  2431. #endif
  2432. #else
  2433. (void)XSNPRINTF(msg, sizeof(msg), "%s,%d,%s%s,"
  2434. FLT_FMT_PREC "," STATS_CLAUSE_SEPARATOR,
  2435. algo, strength, desc, desc_extra,
  2436. FLT_FMT_PREC_ARGS(digits, opsSec));
  2437. #endif
  2438. } /* if (csv_format == 1) */
  2439. else {
  2440. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2441. #ifdef HAVE_GET_CYCLES
  2442. (void)XSNPRINTF(msg, sizeof(msg),
  2443. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, "
  2444. FLT_FMT_PREC " %s, %lu cycles" STATS_CLAUSE_SEPARATOR,
  2445. algo, strength, desc, desc_extra,
  2446. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2447. FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2448. FLT_FMT_PREC_ARGS(digits, opsSec), word[3],
  2449. (unsigned long)total_cycles);
  2450. #else
  2451. (void)XSNPRINTF(msg, sizeof(msg),
  2452. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, "
  2453. FLT_FMT_PREC " %s" STATS_CLAUSE_SEPARATOR,
  2454. algo, strength, desc, desc_extra,
  2455. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2456. FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2457. FLT_FMT_PREC_ARGS(digits, opsSec), word[3]);
  2458. #endif /* HAVE_GET_CYCLES */
  2459. #else
  2460. (void)XSNPRINTF(msg, sizeof(msg),
  2461. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, "
  2462. FLT_FMT_PREC " %s" STATS_CLAUSE_SEPARATOR,
  2463. algo, strength, desc, desc_extra,
  2464. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2465. FLT_FMT_PREC2_ARGS(5, 3, total), word[1],
  2466. FLT_FMT_PREC_ARGS(digits, opsSec), word[3]);
  2467. #endif
  2468. }
  2469. #else /* MULTI_VALUE_STATISTICS. Print with avg ms */
  2470. /* format and print to terminal */
  2471. if (csv_format == 1) {
  2472. /* only print out header once */
  2473. if (asym_header_printed == 0) {
  2474. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2475. #ifdef HAVE_GET_CYCLES
  2476. printf("%s", "\"asym\",Algorithm,key size,operation,avg ms,ops/"
  2477. WOLFSSL_FIXED_TIME_UNIT "ec,ops," WOLFSSL_FIXED_TIME_UNIT
  2478. "ecs,cycles,cycles/op,");
  2479. #else
  2480. printf("%s", "\"asym\",Algorithm,key size,operation,avg ms,ops/"
  2481. WOLFSSL_FIXED_TIME_UNIT "ec,ops," WOLFSSL_FIXED_TIME_UNIT
  2482. "ecs,");
  2483. #endif
  2484. #else
  2485. printf("\n%sAsymmetric Ciphers:\n\n", info_prefix);
  2486. printf("%sAlgorithm,key size,operation,avg ms,ops/"
  2487. WOLFSSL_FIXED_TIME_UNIT "ec,", info_prefix);
  2488. #endif
  2489. printf("\n");
  2490. asym_header_printed = 1;
  2491. }
  2492. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2493. #ifdef HAVE_GET_CYCLES
  2494. (void)XSNPRINTF(msg, sizeof(msg),
  2495. "asym,%s,%d,%s%s," FLT_FMT_PREC "," FLT_FMT_PREC ",%d,"
  2496. FLT_FMT ",%lu," FLT_FMT_PREC STATS_CLAUSE_SEPARATOR,
  2497. algo, strength, desc, desc_extra,
  2498. FLT_FMT_PREC_ARGS(3, milliEach),
  2499. FLT_FMT_PREC_ARGS(digits, opsSec),
  2500. count, FLT_FMT_ARGS(total), (unsigned long)total_cycles,
  2501. FLT_FMT_PREC_ARGS(6,
  2502. (double)total_cycles / (double)count));
  2503. #else
  2504. (void)XSNPRINTF(msg, sizeof(msg),
  2505. "asym,%s,%d,%s%s," FLT_FMT_PREC "," FLT_FMT_PREC ",%d,"
  2506. FLT_FMT STATS_CLAUSE_SEPARATOR,
  2507. algo, strength, desc, desc_extra,
  2508. FLT_FMT_PREC_ARGS(3, milliEach),
  2509. FLT_FMT_PREC_ARGS(digits, opsSec),
  2510. count, FLT_FMT_ARGS(total));
  2511. #endif
  2512. #else
  2513. (void)XSNPRINTF(msg, sizeof(msg), "%s,%d,%s%s," FLT_FMT_PREC ","
  2514. FLT_FMT_PREC "," STATS_CLAUSE_SEPARATOR,
  2515. algo, strength, desc, desc_extra,
  2516. FLT_FMT_PREC_ARGS(3, milliEach),
  2517. FLT_FMT_PREC_ARGS(digits, opsSec));
  2518. #endif
  2519. } /* if (csv_format == 1) */
  2520. else {
  2521. #ifdef GENERATE_MACHINE_PARSEABLE_REPORT
  2522. #ifdef HAVE_GET_CYCLES
  2523. (void)XSNPRINTF(msg, sizeof(msg),
  2524. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, %s "
  2525. FLT_FMT_PREC2 " ms, " FLT_FMT_PREC " %s, %lu cycles"
  2526. STATS_CLAUSE_SEPARATOR,
  2527. algo, strength, desc, desc_extra,
  2528. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2529. FLT_FMT_PREC2_ARGS(5, 3, total), word[1], word[2],
  2530. FLT_FMT_PREC2_ARGS(5, 3, milliEach),
  2531. FLT_FMT_PREC_ARGS(digits, opsSec), word[3],
  2532. (unsigned long)total_cycles);
  2533. #else
  2534. (void)XSNPRINTF(msg, sizeof(msg),
  2535. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, %s "
  2536. FLT_FMT_PREC2 " ms, " FLT_FMT_PREC " %s"
  2537. STATS_CLAUSE_SEPARATOR,
  2538. algo, strength, desc, desc_extra,
  2539. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2540. FLT_FMT_PREC2_ARGS(5, 3, total), word[1], word[2],
  2541. FLT_FMT_PREC2_ARGS(5, 3, milliEach),
  2542. FLT_FMT_PREC_ARGS(digits, opsSec), word[3]);
  2543. #endif /* HAVE_GET_CYCLES */
  2544. #else
  2545. (void)XSNPRINTF(msg, sizeof(msg),
  2546. "%-6s %5d %8s%-2s %s %6d %s " FLT_FMT_PREC2 " %s, %s "
  2547. FLT_FMT_PREC2 " ms, " FLT_FMT_PREC " %s"
  2548. STATS_CLAUSE_SEPARATOR,
  2549. algo, strength, desc, desc_extra,
  2550. BENCH_DEVID_GET_NAME(useDeviceID), count, word[0],
  2551. FLT_FMT_PREC2_ARGS(5, 3, total), word[1], word[2],
  2552. FLT_FMT_PREC2_ARGS(5, 3, milliEach),
  2553. FLT_FMT_PREC_ARGS(digits, opsSec), word[3]);
  2554. #endif
  2555. }
  2556. #endif /* MULTI_VALUE_STATISTICS */
  2557. printf("%s", msg);
  2558. /* show errors */
  2559. if (ret < 0) {
  2560. printf("%sBenchmark %s %s %d failed: %d\n",
  2561. err_prefix, algo, desc, strength, ret);
  2562. }
  2563. #ifndef WOLFSSL_SGX
  2564. XFFLUSH(stdout);
  2565. #endif
  2566. #ifdef WC_BENCH_TRACK_STATS
  2567. /* Add to thread stats */
  2568. bench_stats_add(BENCH_STAT_ASYM, algo, strength, desc, useDeviceID, opsSec,
  2569. kOpsSec, ret);
  2570. #endif
  2571. (void)useDeviceID;
  2572. (void)ret;
  2573. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  2574. RESTORE_VECTOR_REGISTERS();
  2575. #endif
  2576. TEST_SLEEP();
  2577. } /* bench_stats_asym_finish_ex */
  2578. static void bench_stats_asym_finish(const char* algo, int strength,
  2579. const char* desc, int useDeviceID, int count, double start, int ret)
  2580. {
  2581. bench_stats_asym_finish_ex(algo, strength, desc, "", useDeviceID, count,
  2582. start, ret);
  2583. }
  2584. #endif
  2585. #endif /* BENCH_ASYM */
  2586. static WC_INLINE void bench_stats_free(void)
  2587. {
  2588. #ifdef WC_BENCH_TRACK_STATS
  2589. bench_stats_t* bstat;
  2590. for (bstat = bench_stats_head; bstat != NULL; ) {
  2591. bench_stats_t* next = bstat->next;
  2592. XFREE(bstat, NULL, DYNAMIC_TYPE_INFO);
  2593. bstat = next;
  2594. }
  2595. bench_stats_head = NULL;
  2596. bench_stats_tail = NULL;
  2597. #endif
  2598. }
  2599. /*****************************************************************************/
  2600. /* End Stats Functions */
  2601. /*****************************************************************************/
  2602. static void* benchmarks_do(void* args)
  2603. {
  2604. long bench_buf_size;
  2605. #ifdef WOLFSSL_ASYNC_CRYPT
  2606. #ifndef WC_NO_ASYNC_THREADING
  2607. ThreadData* threadData = (ThreadData*)args;
  2608. if (wolfAsync_DevOpenThread(&devId, &threadData->thread_id) < 0)
  2609. #else
  2610. if (wolfAsync_DevOpen(&devId) < 0)
  2611. #endif
  2612. {
  2613. printf("%sAsync device open failed\n%sRunning without async\n",
  2614. err_prefix, err_prefix);
  2615. }
  2616. #endif /* WOLFSSL_ASYNC_CRYPT */
  2617. (void)args;
  2618. #ifdef WOLFSSL_ASYNC_CRYPT
  2619. if (wolfEventQueue_Init(&eventQueue) != 0) {
  2620. printf("%sAsync event queue init failure!\n", err_prefix);
  2621. }
  2622. #endif
  2623. #ifdef WOLF_CRYPTO_CB
  2624. #ifdef HAVE_INTEL_QA_SYNC
  2625. devId = wc_CryptoCb_InitIntelQa();
  2626. if (devId == INVALID_DEVID) {
  2627. printf("%sCouldn't init the Intel QA\n", err_prefix);
  2628. }
  2629. #endif
  2630. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  2631. devId = wc_CryptoCb_InitOcteon();
  2632. if (devId == INVALID_DEVID) {
  2633. printf("%sCouldn't get the Octeon device ID\n", err_prefix);
  2634. }
  2635. #endif
  2636. #ifdef HAVE_RENESAS_SYNC
  2637. devId = wc_CryptoCb_CryptInitRenesasCmn(NULL, &guser_PKCbInfo);
  2638. if (devId == INVALID_DEVID) {
  2639. printf("%sCouldn't get the Renesas device ID\n", err_prefix);
  2640. }
  2641. #endif
  2642. #endif
  2643. #if defined(HAVE_LOCAL_RNG)
  2644. {
  2645. int rngRet;
  2646. #ifndef HAVE_FIPS
  2647. rngRet = wc_InitRng_ex(&gRng, HEAP_HINT, devId);
  2648. #else
  2649. rngRet = wc_InitRng(&gRng);
  2650. #endif
  2651. if (rngRet < 0) {
  2652. printf("%sInitRNG failed\n", err_prefix);
  2653. return NULL;
  2654. }
  2655. }
  2656. #endif
  2657. /* setup bench plain, cipher, key and iv globals */
  2658. /* make sure bench buffer is multiple of 16 (AES block size) */
  2659. bench_buf_size = (int)bench_size + BENCH_CIPHER_ADD;
  2660. if (bench_buf_size % 16)
  2661. bench_buf_size += 16 - (bench_buf_size % 16);
  2662. #ifdef WOLFSSL_AFALG_XILINX_AES
  2663. bench_plain = (byte*)aligned_alloc(64, (size_t)bench_buf_size + 16);
  2664. bench_cipher = (byte*)aligned_alloc(64, (size_t)bench_buf_size + 16);
  2665. #else
  2666. bench_plain = (byte*)XMALLOC((size_t)bench_buf_size + 16,
  2667. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2668. bench_cipher = (byte*)XMALLOC((size_t)bench_buf_size + 16,
  2669. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2670. #endif
  2671. if (bench_plain == NULL || bench_cipher == NULL) {
  2672. XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2673. XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2674. bench_plain = bench_cipher = NULL;
  2675. printf("%sBenchmark block buffer alloc failed!\n", err_prefix);
  2676. goto exit;
  2677. }
  2678. #ifndef NO_FILESYSTEM
  2679. if (hash_input) {
  2680. int rawSz;
  2681. XFILE file;
  2682. file = XFOPEN(hash_input, "rb");
  2683. if (file == XBADFILE)
  2684. goto exit;
  2685. if (XFSEEK(file, 0, XSEEK_END) != 0) {
  2686. XFCLOSE(file);
  2687. goto exit;
  2688. }
  2689. bench_buf_size = XFTELL(file);
  2690. if(XFSEEK(file, 0, XSEEK_SET) != 0) {
  2691. XFCLOSE(file);
  2692. goto exit;
  2693. }
  2694. XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2695. rawSz = (int)bench_buf_size;
  2696. if (bench_buf_size % 16)
  2697. bench_buf_size += 16 - (bench_buf_size % 16);
  2698. bench_size = (word32)bench_buf_size;
  2699. bench_plain = (byte*)XMALLOC((size_t)bench_buf_size + 16*2,
  2700. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2701. if (bench_plain == NULL) {
  2702. XFCLOSE(file);
  2703. goto exit;
  2704. }
  2705. if ((size_t)XFREAD(bench_plain, 1, rawSz, file)
  2706. != (size_t)rawSz) {
  2707. XFCLOSE(file);
  2708. goto exit;
  2709. }
  2710. XFCLOSE(file);
  2711. }
  2712. else {
  2713. XMEMSET(bench_plain, 0, (size_t)bench_buf_size);
  2714. }
  2715. if (cipher_input) {
  2716. int rawSz;
  2717. XFILE file;
  2718. file = XFOPEN(cipher_input, "rb");
  2719. if (file == XBADFILE)
  2720. goto exit;
  2721. if (XFSEEK(file, 0, XSEEK_END) != 0) {
  2722. XFCLOSE(file);
  2723. goto exit;
  2724. }
  2725. bench_buf_size = XFTELL(file);
  2726. if(XFSEEK(file, 0, XSEEK_SET) != 0) {
  2727. XFCLOSE(file);
  2728. goto exit;
  2729. }
  2730. XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2731. rawSz = (int)bench_buf_size;
  2732. if (bench_buf_size % 16)
  2733. bench_buf_size += 16 - (bench_buf_size % 16);
  2734. if (bench_size > (word32)bench_buf_size)
  2735. bench_size = (word32)bench_buf_size;
  2736. bench_cipher = (byte*)XMALLOC((size_t)bench_buf_size + 16*2,
  2737. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2738. if (bench_cipher == NULL) {
  2739. XFCLOSE(file);
  2740. goto exit;
  2741. }
  2742. if ((size_t)XFREAD(bench_cipher, 1, rawSz, file)
  2743. != (size_t)rawSz) {
  2744. XFCLOSE(file);
  2745. goto exit;
  2746. }
  2747. XFCLOSE(file);
  2748. }
  2749. else {
  2750. XMEMSET(bench_cipher, 0, (size_t)bench_buf_size);
  2751. }
  2752. #endif
  2753. #if defined(WOLFSSL_ASYNC_CRYPT) || defined(HAVE_INTEL_QA_SYNC)
  2754. bench_key = (byte*)XMALLOC(sizeof(bench_key_buf),
  2755. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2756. bench_iv = (byte*)XMALLOC(sizeof(bench_iv_buf),
  2757. HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2758. if (bench_key == NULL || bench_iv == NULL) {
  2759. XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2760. XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  2761. bench_key = bench_iv = NULL;
  2762. printf("%sBenchmark cipher buffer alloc failed!\n", err_prefix);
  2763. goto exit;
  2764. }
  2765. XMEMCPY(bench_key, bench_key_buf, sizeof(bench_key_buf));
  2766. XMEMCPY(bench_iv, bench_iv_buf, sizeof(bench_iv_buf));
  2767. #elif defined(HAVE_RENESAS_SYNC)
  2768. bench_key1 = (byte*)guser_PKCbInfo.wrapped_key_aes128;
  2769. bench_key2 = (byte*)guser_PKCbInfo.wrapped_key_aes256;
  2770. bench_key = (byte*)bench_key_buf;
  2771. bench_iv = (byte*)bench_iv_buf;
  2772. #else
  2773. bench_key = (byte*)bench_key_buf;
  2774. bench_iv = (byte*)bench_iv_buf;
  2775. #endif
  2776. #ifndef WC_NO_RNG
  2777. if (bench_all || (bench_other_algs & BENCH_RNG))
  2778. bench_rng();
  2779. #endif /* WC_NO_RNG */
  2780. #ifndef NO_AES
  2781. #ifdef HAVE_AES_CBC
  2782. if (bench_all || (bench_cipher_algs & BENCH_AES_CBC)) {
  2783. #ifndef NO_SW_BENCH
  2784. bench_aescbc(0);
  2785. #endif
  2786. #if defined(BENCH_DEVID)
  2787. bench_aescbc(1);
  2788. #endif
  2789. }
  2790. #endif
  2791. #ifdef HAVE_AESGCM
  2792. if (bench_all || (bench_cipher_algs & BENCH_AES_GCM)) {
  2793. #ifndef NO_SW_BENCH
  2794. bench_aes_aad_options_wrap(bench_aesgcm, 0);
  2795. #endif
  2796. #if ((defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)) || \
  2797. defined(HAVE_INTEL_QA_SYNC) || defined(HAVE_CAVIUM_OCTEON_SYNC) || \
  2798. defined(HAVE_RENESAS_SYNC) || defined(WOLFSSL_CAAM)) && \
  2799. !defined(NO_HW_BENCH)
  2800. bench_aes_aad_options_wrap(bench_aesgcm, 1);
  2801. #endif
  2802. #ifndef NO_SW_BENCH
  2803. bench_gmac(0);
  2804. #endif
  2805. #if defined(BENCH_DEVID)
  2806. bench_gmac(1);
  2807. #endif
  2808. }
  2809. #endif
  2810. #ifdef HAVE_AES_ECB
  2811. if (bench_all || (bench_cipher_algs & BENCH_AES_ECB)) {
  2812. #ifndef NO_SW_BENCH
  2813. bench_aesecb(0);
  2814. #endif
  2815. #ifdef BENCH_DEVID
  2816. bench_aesecb(1);
  2817. #endif
  2818. }
  2819. #endif
  2820. #ifdef WOLFSSL_AES_XTS
  2821. if (bench_all || (bench_cipher_algs & BENCH_AES_XTS))
  2822. bench_aesxts();
  2823. #endif
  2824. #ifdef WOLFSSL_AES_CFB
  2825. if (bench_all || (bench_cipher_algs & BENCH_AES_CFB))
  2826. bench_aescfb();
  2827. #endif
  2828. #ifdef WOLFSSL_AES_OFB
  2829. if (bench_all || (bench_cipher_algs & BENCH_AES_OFB))
  2830. bench_aesofb();
  2831. #endif
  2832. #ifdef WOLFSSL_AES_COUNTER
  2833. if (bench_all || (bench_cipher_algs & BENCH_AES_CTR)) {
  2834. bench_aesctr(0);
  2835. #ifdef BENCH_DEVID
  2836. bench_aesctr(1);
  2837. #endif
  2838. }
  2839. #endif
  2840. #ifdef HAVE_AESCCM
  2841. if (bench_all || (bench_cipher_algs & BENCH_AES_CCM)) {
  2842. bench_aes_aad_options_wrap(bench_aesccm, 0);
  2843. #ifdef BENCH_DEVID
  2844. bench_aes_aad_options_wrap(bench_aesccm, 1);
  2845. #endif
  2846. }
  2847. #endif
  2848. #ifdef WOLFSSL_AES_SIV
  2849. if (bench_all || (bench_cipher_algs & BENCH_AES_SIV))
  2850. bench_aessiv();
  2851. #endif
  2852. #endif /* !NO_AES */
  2853. #ifdef HAVE_CAMELLIA
  2854. if (bench_all || (bench_cipher_algs & BENCH_CAMELLIA))
  2855. bench_camellia();
  2856. #endif
  2857. #ifdef WOLFSSL_SM4_CBC
  2858. if (bench_all || (bench_cipher_algs & BENCH_SM4_CBC))
  2859. bench_sm4_cbc();
  2860. #endif
  2861. #ifdef WOLFSSL_SM4_GCM
  2862. if (bench_all || (bench_cipher_algs & BENCH_SM4_GCM))
  2863. bench_sm4_gcm();
  2864. #endif
  2865. #ifdef WOLFSSL_SM4_CCM
  2866. if (bench_all || (bench_cipher_algs & BENCH_SM4_CCM))
  2867. bench_sm4_ccm();
  2868. #endif
  2869. #ifndef NO_RC4
  2870. if (bench_all || (bench_cipher_algs & BENCH_ARC4)) {
  2871. #ifndef NO_SW_BENCH
  2872. bench_arc4(0);
  2873. #endif
  2874. #ifdef BENCH_DEVID
  2875. bench_arc4(1);
  2876. #endif
  2877. }
  2878. #endif
  2879. #ifdef HAVE_CHACHA
  2880. if (bench_all || (bench_cipher_algs & BENCH_CHACHA20))
  2881. bench_chacha();
  2882. #endif
  2883. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  2884. if (bench_all || (bench_cipher_algs & BENCH_CHACHA20_POLY1305))
  2885. bench_chacha20_poly1305_aead();
  2886. #endif
  2887. #ifndef NO_DES3
  2888. if (bench_all || (bench_cipher_algs & BENCH_DES)) {
  2889. #ifndef NO_SW_BENCH
  2890. bench_des(0);
  2891. #endif
  2892. #ifdef BENCH_DEVID
  2893. bench_des(1);
  2894. #endif
  2895. }
  2896. #endif
  2897. #ifndef NO_MD5
  2898. if (bench_all || (bench_digest_algs & BENCH_MD5)) {
  2899. #ifndef NO_SW_BENCH
  2900. bench_md5(0);
  2901. #endif
  2902. #ifdef BENCH_DEVID
  2903. bench_md5(1);
  2904. #endif
  2905. }
  2906. #endif
  2907. #ifdef HAVE_POLY1305
  2908. if (bench_all || (bench_digest_algs & BENCH_POLY1305))
  2909. bench_poly1305();
  2910. #endif
  2911. #ifndef NO_SHA
  2912. if (bench_all || (bench_digest_algs & BENCH_SHA)) {
  2913. #ifndef NO_SW_BENCH
  2914. bench_sha(0);
  2915. #endif
  2916. #ifdef BENCH_DEVID
  2917. bench_sha(1);
  2918. #endif
  2919. }
  2920. #endif
  2921. #ifdef WOLFSSL_SHA224
  2922. if (bench_all || (bench_digest_algs & BENCH_SHA224)) {
  2923. #ifndef NO_SW_BENCH
  2924. bench_sha224(0);
  2925. #endif
  2926. #ifdef BENCH_DEVID
  2927. bench_sha224(1);
  2928. #endif
  2929. }
  2930. #endif
  2931. #ifndef NO_SHA256
  2932. if (bench_all || (bench_digest_algs & BENCH_SHA256)) {
  2933. #ifndef NO_SW_BENCH
  2934. bench_sha256(0);
  2935. #endif
  2936. #ifdef BENCH_DEVID
  2937. bench_sha256(1);
  2938. #endif
  2939. }
  2940. #endif
  2941. #ifdef WOLFSSL_SHA384
  2942. if (bench_all || (bench_digest_algs & BENCH_SHA384)) {
  2943. #ifndef NO_SW_BENCH
  2944. bench_sha384(0);
  2945. #endif
  2946. #ifdef BENCH_DEVID
  2947. bench_sha384(1);
  2948. #endif
  2949. }
  2950. #endif
  2951. #ifdef WOLFSSL_SHA512
  2952. if (bench_all || (bench_digest_algs & BENCH_SHA512)) {
  2953. #ifndef NO_SW_BENCH
  2954. bench_sha512(0);
  2955. #endif
  2956. #ifdef BENCH_DEVID
  2957. bench_sha512(1);
  2958. #endif
  2959. }
  2960. #if !defined(WOLFSSL_NOSHA512_224) && \
  2961. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  2962. if (bench_all || (bench_digest_algs & BENCH_SHA512)) {
  2963. #ifndef NO_SW_BENCH
  2964. bench_sha512_224(0);
  2965. #endif
  2966. #ifdef BENCH_DEVID
  2967. bench_sha512_224(1);
  2968. #endif
  2969. }
  2970. #endif /* WOLFSSL_NOSHA512_224 */
  2971. #if !defined(WOLFSSL_NOSHA512_256) && \
  2972. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  2973. if (bench_all || (bench_digest_algs & BENCH_SHA512)) {
  2974. #ifndef NO_SW_BENCH
  2975. bench_sha512_256(0);
  2976. #endif
  2977. #ifdef BENCH_DEVID
  2978. bench_sha512_256(1);
  2979. #endif
  2980. }
  2981. #endif /* WOLFSSL_NOSHA512_256 */
  2982. #endif /* WOLFSSL_SHA512 */
  2983. #ifdef WOLFSSL_SHA3
  2984. #ifndef WOLFSSL_NOSHA3_224
  2985. if (bench_all || (bench_digest_algs & BENCH_SHA3_224)) {
  2986. #ifndef NO_SW_BENCH
  2987. bench_sha3_224(0);
  2988. #endif
  2989. #ifdef BENCH_DEVID
  2990. bench_sha3_224(1);
  2991. #endif
  2992. }
  2993. #endif /* WOLFSSL_NOSHA3_224 */
  2994. #ifndef WOLFSSL_NOSHA3_256
  2995. if (bench_all || (bench_digest_algs & BENCH_SHA3_256)) {
  2996. #ifndef NO_SW_BENCH
  2997. bench_sha3_256(0);
  2998. #endif
  2999. #ifdef BENCH_DEVID
  3000. bench_sha3_256(1);
  3001. #endif
  3002. }
  3003. #endif /* WOLFSSL_NOSHA3_256 */
  3004. #ifndef WOLFSSL_NOSHA3_384
  3005. if (bench_all || (bench_digest_algs & BENCH_SHA3_384)) {
  3006. #ifndef NO_SW_BENCH
  3007. bench_sha3_384(0);
  3008. #endif
  3009. #ifdef BENCH_DEVID
  3010. bench_sha3_384(1);
  3011. #endif
  3012. }
  3013. #endif /* WOLFSSL_NOSHA3_384 */
  3014. #ifndef WOLFSSL_NOSHA3_512
  3015. if (bench_all || (bench_digest_algs & BENCH_SHA3_512)) {
  3016. #ifndef NO_SW_BENCH
  3017. bench_sha3_512(0);
  3018. #endif
  3019. #ifdef BENCH_DEVID
  3020. bench_sha3_512(1);
  3021. #endif
  3022. }
  3023. #endif /* WOLFSSL_NOSHA3_512 */
  3024. #ifdef WOLFSSL_SHAKE128
  3025. if (bench_all || (bench_digest_algs & BENCH_SHAKE128)) {
  3026. #ifndef NO_SW_BENCH
  3027. bench_shake128(0);
  3028. #endif
  3029. #ifdef BENCH_DEVID
  3030. bench_shake128(1);
  3031. #endif
  3032. }
  3033. #endif /* WOLFSSL_SHAKE128 */
  3034. #ifdef WOLFSSL_SHAKE256
  3035. if (bench_all || (bench_digest_algs & BENCH_SHAKE256)) {
  3036. #ifndef NO_SW_BENCH
  3037. bench_shake256(0);
  3038. #endif
  3039. #ifdef BENCH_DEVID
  3040. bench_shake256(1);
  3041. #endif
  3042. }
  3043. #endif /* WOLFSSL_SHAKE256 */
  3044. #endif
  3045. #ifdef WOLFSSL_SM3
  3046. if (bench_all || (bench_digest_algs & BENCH_SM3)) {
  3047. #ifndef NO_SW_BENCH
  3048. bench_sm3(0);
  3049. #endif
  3050. #ifdef BENCH_DEVID
  3051. bench_sm3(1);
  3052. #endif
  3053. }
  3054. #endif
  3055. #ifdef WOLFSSL_RIPEMD
  3056. if (bench_all || (bench_digest_algs & BENCH_RIPEMD))
  3057. bench_ripemd();
  3058. #endif
  3059. #ifdef HAVE_BLAKE2
  3060. if (bench_all || (bench_digest_algs & BENCH_BLAKE2B))
  3061. bench_blake2b();
  3062. #endif
  3063. #ifdef HAVE_BLAKE2S
  3064. if (bench_all || (bench_digest_algs & BENCH_BLAKE2S))
  3065. bench_blake2s();
  3066. #endif
  3067. #ifdef WOLFSSL_CMAC
  3068. if (bench_all || (bench_mac_algs & BENCH_CMAC)) {
  3069. bench_cmac(0);
  3070. #ifdef BENCH_DEVID
  3071. bench_cmac(1);
  3072. #endif
  3073. }
  3074. #endif
  3075. #ifndef NO_HMAC
  3076. #ifndef NO_MD5
  3077. if (bench_all || (bench_mac_algs & BENCH_HMAC_MD5)) {
  3078. #ifndef NO_SW_BENCH
  3079. bench_hmac_md5(0);
  3080. #endif
  3081. #ifdef BENCH_DEVID
  3082. bench_hmac_md5(1);
  3083. #endif
  3084. }
  3085. #endif
  3086. #ifndef NO_SHA
  3087. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA)) {
  3088. #ifndef NO_SW_BENCH
  3089. bench_hmac_sha(0);
  3090. #endif
  3091. #ifdef BENCH_DEVID
  3092. bench_hmac_sha(1);
  3093. #endif
  3094. }
  3095. #endif
  3096. #ifdef WOLFSSL_SHA224
  3097. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA224)) {
  3098. #ifndef NO_SW_BENCH
  3099. bench_hmac_sha224(0);
  3100. #endif
  3101. #ifdef BENCH_DEVID
  3102. bench_hmac_sha224(1);
  3103. #endif
  3104. }
  3105. #endif
  3106. #ifndef NO_SHA256
  3107. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA256)) {
  3108. #ifndef NO_SW_BENCH
  3109. bench_hmac_sha256(0);
  3110. #endif
  3111. #ifdef BENCH_DEVID
  3112. bench_hmac_sha256(1);
  3113. #endif
  3114. }
  3115. #endif
  3116. #ifdef WOLFSSL_SHA384
  3117. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA384)) {
  3118. #ifndef NO_SW_BENCH
  3119. bench_hmac_sha384(0);
  3120. #endif
  3121. #ifdef BENCH_DEVID
  3122. bench_hmac_sha384(1);
  3123. #endif
  3124. }
  3125. #endif
  3126. #ifdef WOLFSSL_SHA512
  3127. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA512)) {
  3128. #ifndef NO_SW_BENCH
  3129. bench_hmac_sha512(0);
  3130. #endif
  3131. #ifdef BENCH_DEVID
  3132. bench_hmac_sha512(1);
  3133. #endif
  3134. }
  3135. #endif
  3136. #ifndef NO_PWDBASED
  3137. if (bench_all || (bench_mac_algs & BENCH_PBKDF2)) {
  3138. bench_pbkdf2();
  3139. }
  3140. #endif
  3141. #endif /* NO_HMAC */
  3142. #ifdef WOLFSSL_SIPHASH
  3143. if (bench_all || (bench_mac_algs & BENCH_SIPHASH)) {
  3144. bench_siphash();
  3145. }
  3146. #endif
  3147. #ifdef WC_SRTP_KDF
  3148. if (bench_all || (bench_kdf_algs & BENCH_SRTP_KDF)) {
  3149. bench_srtpkdf();
  3150. }
  3151. #endif
  3152. #ifdef HAVE_SCRYPT
  3153. if (bench_all || (bench_other_algs & BENCH_SCRYPT))
  3154. bench_scrypt();
  3155. #endif
  3156. #ifndef NO_RSA
  3157. #ifndef HAVE_RENESAS_SYNC
  3158. #ifdef WOLFSSL_KEY_GEN
  3159. if (bench_all || (bench_asym_algs & BENCH_RSA_KEYGEN)) {
  3160. #ifndef NO_SW_BENCH
  3161. if (((word32)bench_asym_algs == 0xFFFFFFFFU) ||
  3162. (bench_asym_algs & BENCH_RSA_SZ) == 0) {
  3163. bench_rsaKeyGen(0);
  3164. }
  3165. else {
  3166. bench_rsaKeyGen_size(0, bench_size);
  3167. }
  3168. #endif
  3169. #ifdef BENCH_DEVID
  3170. if (bench_asym_algs & BENCH_RSA_SZ) {
  3171. bench_rsaKeyGen_size(1, bench_size);
  3172. }
  3173. else {
  3174. bench_rsaKeyGen(1);
  3175. }
  3176. #endif
  3177. }
  3178. #endif
  3179. if (bench_all || (bench_asym_algs & BENCH_RSA)) {
  3180. #ifndef NO_SW_BENCH
  3181. bench_rsa(0);
  3182. #endif
  3183. #ifdef BENCH_DEVID
  3184. bench_rsa(1);
  3185. #endif
  3186. }
  3187. #ifdef WOLFSSL_KEY_GEN
  3188. if (bench_asym_algs & BENCH_RSA_SZ) {
  3189. #ifndef NO_SW_BENCH
  3190. bench_rsa_key(0, bench_size);
  3191. #endif
  3192. #ifdef BENCH_DEVID
  3193. bench_rsa_key(1, bench_size);
  3194. #endif
  3195. }
  3196. #endif
  3197. #endif
  3198. #endif
  3199. #ifndef NO_DH
  3200. if (bench_all || (bench_asym_algs & BENCH_DH)) {
  3201. #ifndef NO_SW_BENCH
  3202. bench_dh(0);
  3203. #endif
  3204. #ifdef BENCH_DEVID
  3205. bench_dh(1);
  3206. #endif
  3207. }
  3208. #endif
  3209. #ifdef WOLFSSL_HAVE_KYBER
  3210. if (bench_all || (bench_asym_algs & BENCH_KYBER)) {
  3211. #ifdef WOLFSSL_KYBER512
  3212. bench_kyber(KYBER512);
  3213. #endif
  3214. #ifdef WOLFSSL_KYBER768
  3215. bench_kyber(KYBER768);
  3216. #endif
  3217. #ifdef WOLFSSL_KYBER1024
  3218. bench_kyber(KYBER1024);
  3219. #endif
  3220. }
  3221. #endif
  3222. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  3223. if (bench_all || (bench_pq_hash_sig_algs & BENCH_LMS_HSS)) {
  3224. bench_lms();
  3225. }
  3226. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY) */
  3227. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  3228. if (bench_all) {
  3229. bench_pq_hash_sig_algs |= BENCH_XMSS_XMSSMT;
  3230. }
  3231. #ifndef NO_SHA256
  3232. if (bench_pq_hash_sig_algs & BENCH_XMSS_XMSSMT_SHA256) {
  3233. bench_xmss(WC_HASH_TYPE_SHA256);
  3234. }
  3235. #endif
  3236. #ifdef WOLFSSL_SHA512
  3237. if (bench_pq_hash_sig_algs & BENCH_XMSS_XMSSMT_SHA512) {
  3238. bench_xmss(WC_HASH_TYPE_SHA512);
  3239. }
  3240. #endif
  3241. #ifdef WOLFSSL_SHAKE128
  3242. if (bench_pq_hash_sig_algs & BENCH_XMSS_XMSSMT_SHAKE128) {
  3243. bench_xmss(WC_HASH_TYPE_SHAKE128);
  3244. }
  3245. #endif
  3246. #ifdef WOLFSSL_SHAKE256
  3247. if (bench_pq_hash_sig_algs & BENCH_XMSS_XMSSMT_SHAKE256) {
  3248. bench_xmss(WC_HASH_TYPE_SHAKE256);
  3249. }
  3250. #endif
  3251. #endif /* if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY) */
  3252. #ifdef HAVE_ECC
  3253. if (bench_all || (bench_asym_algs & BENCH_ECC_MAKEKEY) ||
  3254. (bench_asym_algs & BENCH_ECC) ||
  3255. (bench_asym_algs & BENCH_ECC_ALL) ||
  3256. (bench_asym_algs & BENCH_ECC_ENCRYPT)) {
  3257. if (bench_asym_algs & BENCH_ECC_ALL) {
  3258. #if defined(HAVE_FIPS) || defined(HAVE_SELFTEST)
  3259. printf("%snot supported in FIPS mode (no ending enum value)\n",
  3260. err_prefix);
  3261. #else
  3262. int curveId = (int)ECC_SECP192R1;
  3263. /* set make key and encrypt */
  3264. bench_asym_algs |= BENCH_ECC_MAKEKEY | BENCH_ECC |
  3265. BENCH_ECC_ENCRYPT;
  3266. if (csv_format != 1) {
  3267. printf("\n%sECC Benchmarks:\n", info_prefix);
  3268. }
  3269. do {
  3270. #ifdef WOLFCRYPT_HAVE_SAKKE
  3271. /* SAKKE is not usable with ECDH/ECDSA. Run separate test. */
  3272. if (curveId == ECC_SAKKE_1) {
  3273. curveId++;
  3274. continue;
  3275. }
  3276. #endif
  3277. if (wc_ecc_get_curve_size_from_id(curveId) !=
  3278. ECC_BAD_ARG_E) {
  3279. bench_ecc_curve(curveId);
  3280. if (csv_format != 1) {
  3281. printf("\n");
  3282. }
  3283. }
  3284. curveId++;
  3285. } while (curveId != (int)ECC_CURVE_MAX);
  3286. #endif
  3287. }
  3288. else if (bench_asym_algs & BENCH_ECC_P256) {
  3289. bench_ecc_curve((int)ECC_SECP256R1);
  3290. }
  3291. else if (bench_asym_algs & BENCH_ECC_P384) {
  3292. bench_ecc_curve((int)ECC_SECP384R1);
  3293. }
  3294. else if (bench_asym_algs & BENCH_ECC_P521) {
  3295. bench_ecc_curve((int)ECC_SECP521R1);
  3296. }
  3297. else {
  3298. #ifndef NO_ECC256
  3299. bench_ecc_curve((int)ECC_SECP256R1);
  3300. #elif defined(HAVE_ECC384)
  3301. bench_ecc_curve((int)ECC_SECP384R1);
  3302. #elif defined(HAVE_ECC521)
  3303. bench_ecc_curve((int)ECC_SECP521R1);
  3304. #endif
  3305. #ifdef HAVE_ECC_BRAINPOOL
  3306. bench_ecc_curve((int)ECC_BRAINPOOLP256R1);
  3307. #endif
  3308. }
  3309. }
  3310. #endif
  3311. #ifdef WOLFSSL_SM2
  3312. if (bench_all || (bench_asym_algs & BENCH_SM2)) {
  3313. bench_sm2(0);
  3314. }
  3315. #endif
  3316. #ifdef HAVE_CURVE25519
  3317. if (bench_all || (bench_asym_algs & BENCH_CURVE25519_KEYGEN)) {
  3318. bench_curve25519KeyGen(0);
  3319. #ifdef BENCH_DEVID
  3320. bench_curve25519KeyGen(1);
  3321. #endif
  3322. }
  3323. #ifdef HAVE_CURVE25519_SHARED_SECRET
  3324. if (bench_all || (bench_asym_algs & BENCH_CURVE25519_KA)) {
  3325. bench_curve25519KeyAgree(0);
  3326. #ifdef BENCH_DEVID
  3327. bench_curve25519KeyAgree(1);
  3328. #endif
  3329. }
  3330. #endif
  3331. #endif
  3332. #ifdef HAVE_ED25519
  3333. if (bench_all || (bench_asym_algs & BENCH_ED25519_KEYGEN))
  3334. bench_ed25519KeyGen();
  3335. if (bench_all || (bench_asym_algs & BENCH_ED25519_SIGN))
  3336. bench_ed25519KeySign();
  3337. #endif
  3338. #ifdef HAVE_CURVE448
  3339. if (bench_all || (bench_asym_algs & BENCH_CURVE448_KEYGEN))
  3340. bench_curve448KeyGen();
  3341. #ifdef HAVE_CURVE448_SHARED_SECRET
  3342. if (bench_all || (bench_asym_algs & BENCH_CURVE448_KA))
  3343. bench_curve448KeyAgree();
  3344. #endif
  3345. #endif
  3346. #ifdef HAVE_ED448
  3347. if (bench_all || (bench_asym_algs & BENCH_ED448_KEYGEN))
  3348. bench_ed448KeyGen();
  3349. if (bench_all || (bench_asym_algs & BENCH_ED448_SIGN))
  3350. bench_ed448KeySign();
  3351. #endif
  3352. #ifdef WOLFCRYPT_HAVE_ECCSI
  3353. #ifdef WOLFCRYPT_ECCSI_KMS
  3354. if (bench_all || (bench_asym_algs & BENCH_ECCSI_KEYGEN)) {
  3355. bench_eccsiKeyGen();
  3356. }
  3357. if (bench_all || (bench_asym_algs & BENCH_ECCSI_PAIRGEN)) {
  3358. bench_eccsiPairGen();
  3359. }
  3360. #endif
  3361. #ifdef WOLFCRYPT_ECCSI_CLIENT
  3362. if (bench_all || (bench_asym_algs & BENCH_ECCSI_VALIDATE)) {
  3363. bench_eccsiValidate();
  3364. }
  3365. if (bench_all || (bench_asym_algs & BENCH_ECCSI)) {
  3366. bench_eccsi();
  3367. }
  3368. #endif
  3369. #endif
  3370. #ifdef WOLFCRYPT_HAVE_SAKKE
  3371. #ifdef WOLFCRYPT_SAKKE_KMS
  3372. if (bench_all || (bench_asym_algs & BENCH_SAKKE_KEYGEN)) {
  3373. bench_sakkeKeyGen();
  3374. }
  3375. if (bench_all || (bench_asym_algs & BENCH_SAKKE_RSKGEN)) {
  3376. bench_sakkeRskGen();
  3377. }
  3378. #endif
  3379. #ifdef WOLFCRYPT_SAKKE_CLIENT
  3380. if (bench_all || (bench_asym_algs & BENCH_SAKKE_VALIDATE)) {
  3381. bench_sakkeValidate();
  3382. }
  3383. if (bench_all || (bench_asym_algs & BENCH_SAKKE)) {
  3384. bench_sakke();
  3385. }
  3386. #endif
  3387. #endif
  3388. #if defined(HAVE_LIBOQS)
  3389. #ifdef HAVE_FALCON
  3390. if (bench_all || (bench_pq_asym_algs & BENCH_FALCON_LEVEL1_SIGN))
  3391. bench_falconKeySign(1);
  3392. if (bench_all || (bench_pq_asym_algs & BENCH_FALCON_LEVEL5_SIGN))
  3393. bench_falconKeySign(5);
  3394. #endif
  3395. #ifdef HAVE_DILITHIUM
  3396. if (bench_all || (bench_pq_asym_algs & BENCH_DILITHIUM_LEVEL2_SIGN))
  3397. bench_dilithiumKeySign(2);
  3398. if (bench_all || (bench_pq_asym_algs & BENCH_DILITHIUM_LEVEL3_SIGN))
  3399. bench_dilithiumKeySign(3);
  3400. if (bench_all || (bench_pq_asym_algs & BENCH_DILITHIUM_LEVEL5_SIGN))
  3401. bench_dilithiumKeySign(5);
  3402. #endif
  3403. #ifdef HAVE_SPHINCS
  3404. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_FAST_LEVEL1_SIGN))
  3405. bench_sphincsKeySign(1, FAST_VARIANT);
  3406. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_FAST_LEVEL3_SIGN))
  3407. bench_sphincsKeySign(3, FAST_VARIANT);
  3408. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_FAST_LEVEL5_SIGN))
  3409. bench_sphincsKeySign(5, FAST_VARIANT);
  3410. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_SMALL_LEVEL1_SIGN))
  3411. bench_sphincsKeySign(1, SMALL_VARIANT);
  3412. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_SMALL_LEVEL3_SIGN))
  3413. bench_sphincsKeySign(3, SMALL_VARIANT);
  3414. if (bench_all || (bench_pq_asym_algs2 & BENCH_SPHINCS_SMALL_LEVEL5_SIGN))
  3415. bench_sphincsKeySign(5, SMALL_VARIANT);
  3416. #endif
  3417. #endif /* HAVE_LIBOQS */
  3418. exit:
  3419. /* free benchmark buffers */
  3420. XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  3421. XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  3422. #ifdef WOLFSSL_ASYNC_CRYPT
  3423. XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  3424. XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  3425. #endif
  3426. #if defined(HAVE_LOCAL_RNG)
  3427. wc_FreeRng(&gRng);
  3428. #endif
  3429. /* cleanup the thread if fixed point cache is enabled and have thread local */
  3430. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  3431. wc_ecc_fp_free();
  3432. #endif
  3433. (void)bench_cipher_algs;
  3434. (void)bench_digest_algs;
  3435. (void)bench_mac_algs;
  3436. (void)bench_asym_algs;
  3437. (void)bench_other_algs;
  3438. (void)bench_pq_asym_algs;
  3439. (void)bench_pq_asym_algs2;
  3440. return NULL;
  3441. }
  3442. int benchmark_init(void)
  3443. {
  3444. int ret = 0;
  3445. benchmark_static_init(0);
  3446. #ifdef WOLFSSL_STATIC_MEMORY
  3447. ret = wc_LoadStaticMemory(&HEAP_HINT, gBenchMemory,
  3448. sizeof(gBenchMemory), WOLFMEM_GENERAL, 1);
  3449. if (ret != 0) {
  3450. printf("%sunable to load static memory %d\n", err_prefix, ret);
  3451. }
  3452. #endif /* WOLFSSL_STATIC_MEMORY */
  3453. if ((ret = wolfCrypt_Init()) != 0) {
  3454. printf("%swolfCrypt_Init failed %d\n", err_prefix, ret);
  3455. return EXIT_FAILURE;
  3456. }
  3457. #ifdef HAVE_WC_INTROSPECTION
  3458. printf("Math: %s\n", wc_GetMathInfo());
  3459. #endif
  3460. #ifdef WOLFSSL_SECO_CAAM
  3461. if (wc_SECO_OpenHSM(SECO_KEY_STORE_ID,
  3462. SECO_BENCHMARK_NONCE, SECO_MAX_UPDATES, CAAM_KEYSTORE_CREATE)
  3463. != 0) {
  3464. printf("%sunable to open HSM\n", err_prefix);
  3465. wolfCrypt_Cleanup();
  3466. return EXIT_FAILURE;
  3467. }
  3468. #endif
  3469. #ifdef WC_RNG_SEED_CB
  3470. wc_SetSeed_Cb(wc_GenerateSeed);
  3471. #endif
  3472. bench_stats_init();
  3473. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  3474. wolfSSL_Debugging_ON();
  3475. #endif
  3476. printf("%swolfCrypt Benchmark (block bytes %d, min " FLT_FMT_PREC " sec each)\n",
  3477. info_prefix, (int)bench_size, FLT_FMT_PREC_ARGS(1, BENCH_MIN_RUNTIME_SEC));
  3478. #ifndef GENERATE_MACHINE_PARSEABLE_REPORT
  3479. if (csv_format == 1) {
  3480. printf("This format allows you to easily copy "
  3481. "the output to a csv file.");
  3482. }
  3483. #endif
  3484. #ifdef HAVE_WNR
  3485. ret = wc_InitNetRandom(wnrConfigFile, NULL, 5000);
  3486. if (ret != 0) {
  3487. printf("%sWhitewood netRandom config init failed %d\n",
  3488. err_prefix, ret);
  3489. }
  3490. #endif /* HAVE_WNR */
  3491. return ret;
  3492. }
  3493. int benchmark_free(void)
  3494. {
  3495. int ret;
  3496. #ifdef WC_BENCH_TRACK_STATS
  3497. if (gPrintStats || devId != INVALID_DEVID) {
  3498. bench_stats_print();
  3499. }
  3500. #endif
  3501. bench_stats_free();
  3502. #ifdef WOLF_CRYPTO_CB
  3503. #ifdef HAVE_INTEL_QA_SYNC
  3504. wc_CryptoCb_CleanupIntelQa(&devId);
  3505. #endif
  3506. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  3507. wc_CryptoCb_CleanupOcteon(&devId);
  3508. #endif
  3509. #ifdef HAVE_RENESAS_SYNC
  3510. wc_CryptoCb_CleanupRenesasCmn(&devId);
  3511. #endif
  3512. #endif
  3513. #ifdef WOLFSSL_ASYNC_CRYPT
  3514. /* free event queue */
  3515. wolfEventQueue_Free(&eventQueue);
  3516. /* close device */
  3517. wolfAsync_DevClose(&devId);
  3518. #endif
  3519. #ifdef HAVE_WNR
  3520. ret = wc_FreeNetRandom();
  3521. if (ret < 0) {
  3522. printf("%sFailed to free netRandom context %d\n", err_prefix, ret);
  3523. }
  3524. #endif
  3525. #ifdef WOLFSSL_SECO_CAAM
  3526. if (wc_SECO_CloseHSM() != 0) {
  3527. printf("%sError closing down the key store\n", err_prefix);
  3528. }
  3529. #endif
  3530. if ((ret = wolfCrypt_Cleanup()) != 0) {
  3531. printf("%serror %d with wolfCrypt_Cleanup\n", err_prefix, ret);
  3532. }
  3533. return ret;
  3534. }
  3535. #if defined(WC_ENABLE_BENCH_THREADING) && !defined(WOLFSSL_ASYNC_CRYPT)
  3536. static THREAD_RETURN WOLFSSL_THREAD run_bench(void* args)
  3537. {
  3538. benchmark_test(args);
  3539. EXIT_TEST(0);
  3540. }
  3541. static int benchmark_test_threaded(void* args)
  3542. {
  3543. int i;
  3544. printf("%sThreads: %d\n", info_prefix, g_threadCount);
  3545. g_threadData = (ThreadData*)XMALLOC(sizeof(ThreadData) * g_threadCount,
  3546. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3547. if (g_threadData == NULL) {
  3548. printf("%sThread data alloc failed!\n", err_prefix);
  3549. return EXIT_FAILURE;
  3550. }
  3551. for (i = 0; i < g_threadCount; i++) {
  3552. THREAD_CHECK_RET(pthread_create(&g_threadData[i].thread_id,
  3553. NULL, run_bench, args));
  3554. }
  3555. for (i = 0; i < g_threadCount; i++) {
  3556. THREAD_CHECK_RET(pthread_join(g_threadData[i].thread_id, 0));
  3557. }
  3558. printf("\n");
  3559. bench_stats_print();
  3560. return 0;
  3561. }
  3562. #endif
  3563. /* so embedded projects can pull in tests on their own */
  3564. #ifdef HAVE_STACK_SIZE
  3565. THREAD_RETURN WOLFSSL_THREAD benchmark_test(void* args)
  3566. #else
  3567. int benchmark_test(void *args)
  3568. #endif
  3569. {
  3570. int ret;
  3571. (void)args;
  3572. #ifdef HAVE_FIPS
  3573. wolfCrypt_SetCb_fips(myFipsCb);
  3574. #endif
  3575. ret = benchmark_init();
  3576. if (ret != 0)
  3577. EXIT_TEST(ret);
  3578. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  3579. {
  3580. /* See the documentation when turning on WOLFSSL_ASYNC_CRYPT
  3581. **
  3582. ** Chapter Two, Build Options:
  3583. **
  3584. ** https://www.wolfssl.com/documentation/manuals/wolfssl/wolfSSL-Manual.pdf
  3585. **
  3586. ** asynchronous cryptography using hardware based adapters such as
  3587. ** the Intel QuickAssist or Marvell (Cavium) Nitrox V.
  3588. */
  3589. int i;
  3590. if (g_threadCount == 0) {
  3591. #ifdef WC_ASYNC_BENCH_THREAD_COUNT
  3592. g_threadCount = WC_ASYNC_BENCH_THREAD_COUNT;
  3593. #else
  3594. g_threadCount = wc_AsyncGetNumberOfCpus();
  3595. if (g_threadCount > 0) {
  3596. g_threadCount /= 2; /* use physical core count */
  3597. }
  3598. #endif
  3599. }
  3600. if (g_threadCount <= 0) {
  3601. g_threadCount = 1;
  3602. }
  3603. printf("%sCPUs: %d\n", info_prefix, g_threadCount);
  3604. g_threadData = (ThreadData*)XMALLOC(sizeof(ThreadData) * g_threadCount,
  3605. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3606. if (g_threadData == NULL) {
  3607. printf("%sThread data alloc failed!\n", err_prefix);
  3608. EXIT_TEST(EXIT_FAILURE);
  3609. }
  3610. /* Create threads */
  3611. for (i = 0; i < g_threadCount; i++) {
  3612. ret = wc_AsyncThreadCreate(&g_threadData[i].thread_id,
  3613. benchmarks_do, &g_threadData[i]);
  3614. if (ret != 0) {
  3615. printf("%sError creating benchmark thread %d\n", err_prefix, ret);
  3616. EXIT_TEST(EXIT_FAILURE);
  3617. }
  3618. }
  3619. /* Start threads */
  3620. for (i = 0; i < g_threadCount; i++) {
  3621. wc_AsyncThreadJoin(&g_threadData[i].thread_id);
  3622. }
  3623. XFREE(g_threadData, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  3624. }
  3625. #else
  3626. benchmarks_do(NULL);
  3627. #endif
  3628. SLEEP_ON_ERROR(1);
  3629. printf("%sBenchmark complete\n", info_prefix);
  3630. ret = benchmark_free();
  3631. EXIT_TEST(ret);
  3632. }
  3633. #ifndef WC_NO_RNG
  3634. void bench_rng(void)
  3635. {
  3636. int ret, i, count;
  3637. double start;
  3638. long pos, len, remain;
  3639. WC_RNG myrng;
  3640. DECLARE_MULTI_VALUE_STATS_VARS()
  3641. #ifndef HAVE_FIPS
  3642. ret = wc_InitRng_ex(&myrng, HEAP_HINT, devId);
  3643. #else
  3644. ret = wc_InitRng(&myrng);
  3645. #endif
  3646. if (ret < 0) {
  3647. printf("InitRNG failed %d\n", ret);
  3648. return;
  3649. }
  3650. bench_stats_start(&count, &start);
  3651. do {
  3652. for (i = 0; i < numBlocks; i++) {
  3653. /* Split request to handle large RNG request */
  3654. pos = 0;
  3655. remain = (int)bench_size;
  3656. while (remain > 0) {
  3657. len = remain;
  3658. if (len > RNG_MAX_BLOCK_LEN)
  3659. len = RNG_MAX_BLOCK_LEN;
  3660. ret = wc_RNG_GenerateBlock(&myrng, &bench_plain[pos],
  3661. (word32)len);
  3662. if (ret < 0)
  3663. goto exit_rng;
  3664. remain -= len;
  3665. pos += len;
  3666. }
  3667. RECORD_MULTI_VALUE_STATS();
  3668. }
  3669. count += i;
  3670. } while (bench_stats_check(start)
  3671. #ifdef MULTI_VALUE_STATISTICS
  3672. || runs < minimum_runs
  3673. #endif
  3674. );
  3675. exit_rng:
  3676. bench_stats_sym_finish("RNG", 0, count, bench_size, start, ret);
  3677. #ifdef MULTI_VALUE_STATISTICS
  3678. bench_multi_value_stats(max, min, sum, squareSum, runs);
  3679. #endif
  3680. wc_FreeRng(&myrng);
  3681. }
  3682. #endif /* WC_NO_RNG */
  3683. #ifndef NO_AES
  3684. #ifdef HAVE_AES_CBC
  3685. static void bench_aescbc_internal(int useDeviceID,
  3686. const byte* key, word32 keySz,
  3687. const byte* iv, const char* encLabel,
  3688. const char* decLabel)
  3689. {
  3690. int ret = 0, i, count = 0, times, pending = 0;
  3691. WC_DECLARE_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  3692. sizeof(Aes), HEAP_HINT);
  3693. double start;
  3694. DECLARE_MULTI_VALUE_STATS_VARS()
  3695. WC_CALLOC_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  3696. sizeof(Aes), HEAP_HINT);
  3697. /* init keys */
  3698. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3699. if ((ret = wc_AesInit(enc[i], HEAP_HINT,
  3700. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  3701. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  3702. goto exit;
  3703. }
  3704. ret = wc_AesSetKey(enc[i], key, keySz, iv, AES_ENCRYPTION);
  3705. if (ret != 0) {
  3706. printf("AesSetKey failed, ret = %d\n", ret);
  3707. goto exit;
  3708. }
  3709. }
  3710. bench_stats_start(&count, &start);
  3711. do {
  3712. for (times = 0; times < numBlocks || pending > 0; ) {
  3713. bench_async_poll(&pending);
  3714. /* while free pending slots in queue, submit ops */
  3715. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3716. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  3717. &times, numBlocks, &pending)) {
  3718. ret = wc_AesCbcEncrypt(enc[i], bench_plain, bench_cipher,
  3719. bench_size);
  3720. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  3721. 0, &times, &pending)) {
  3722. goto exit_aes_enc;
  3723. }
  3724. }
  3725. } /* for i */
  3726. RECORD_MULTI_VALUE_STATS();
  3727. } /* for times */
  3728. count += times;
  3729. } while (bench_stats_check(start)
  3730. #ifdef MULTI_VALUE_STATISTICS
  3731. || runs < minimum_runs
  3732. #endif
  3733. );
  3734. exit_aes_enc:
  3735. bench_stats_sym_finish(encLabel, useDeviceID, count,
  3736. bench_size, start, ret);
  3737. #ifdef MULTI_VALUE_STATISTICS
  3738. bench_multi_value_stats(max, min, sum, squareSum, runs);
  3739. #endif
  3740. if (ret < 0) {
  3741. goto exit;
  3742. }
  3743. #ifdef HAVE_AES_DECRYPT
  3744. /* init keys */
  3745. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3746. ret = wc_AesSetKey(enc[i], key, keySz, iv, AES_DECRYPTION);
  3747. if (ret != 0) {
  3748. printf("AesSetKey failed, ret = %d\n", ret);
  3749. goto exit;
  3750. }
  3751. }
  3752. RESET_MULTI_VALUE_STATS_VARS();
  3753. bench_stats_start(&count, &start);
  3754. do {
  3755. for (times = 0; times < numBlocks || pending > 0; ) {
  3756. bench_async_poll(&pending);
  3757. /* while free pending slots in queue, submit ops */
  3758. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3759. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  3760. &times, numBlocks, &pending)) {
  3761. ret = wc_AesCbcDecrypt(enc[i], bench_cipher, bench_plain,
  3762. bench_size);
  3763. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  3764. 0, &times, &pending)) {
  3765. goto exit_aes_dec;
  3766. }
  3767. }
  3768. } /* for i */
  3769. RECORD_MULTI_VALUE_STATS();
  3770. } /* for times */
  3771. count += times;
  3772. } while (bench_stats_check(start)
  3773. #ifdef MULTI_VALUE_STATISTICS
  3774. || runs < minimum_runs
  3775. #endif
  3776. );
  3777. exit_aes_dec:
  3778. bench_stats_sym_finish(decLabel, useDeviceID, count, bench_size,
  3779. start, ret);
  3780. #ifdef MULTI_VALUE_STATISTICS
  3781. bench_multi_value_stats(max, min, sum, squareSum, runs);
  3782. #endif
  3783. #endif /* HAVE_AES_DECRYPT */
  3784. (void)decLabel;
  3785. exit:
  3786. if (WC_ARRAY_OK(enc)) {
  3787. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3788. wc_AesFree(enc[i]);
  3789. }
  3790. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  3791. }
  3792. }
  3793. void bench_aescbc(int useDeviceID)
  3794. {
  3795. #ifdef WOLFSSL_AES_128
  3796. #ifdef HAVE_RENESAS_SYNC
  3797. bench_aescbc_internal(useDeviceID, bench_key1, 16, bench_iv,
  3798. "AES-128-CBC-enc", "AES-128-CBC-dec");
  3799. #else
  3800. bench_aescbc_internal(useDeviceID, bench_key, 16, bench_iv,
  3801. "AES-128-CBC-enc", "AES-128-CBC-dec");
  3802. #endif
  3803. #endif
  3804. #ifdef WOLFSSL_AES_192
  3805. bench_aescbc_internal(useDeviceID, bench_key, 24, bench_iv,
  3806. "AES-192-CBC-enc", "AES-192-CBC-dec");
  3807. #endif
  3808. #ifdef WOLFSSL_AES_256
  3809. #ifdef HAVE_RENESAS_SYNC
  3810. bench_aescbc_internal(useDeviceID, bench_key2, 32, bench_iv,
  3811. "AES-256-CBC-enc", "AES-256-CBC-dec");
  3812. #else
  3813. bench_aescbc_internal(useDeviceID, bench_key, 32, bench_iv,
  3814. "AES-256-CBC-enc", "AES-256-CBC-dec");
  3815. #endif
  3816. #endif
  3817. }
  3818. #endif /* HAVE_AES_CBC */
  3819. #ifdef HAVE_AESGCM
  3820. static void bench_aesgcm_internal(int useDeviceID,
  3821. const byte* key, word32 keySz,
  3822. const byte* iv, word32 ivSz,
  3823. const char* encLabel, const char* decLabel)
  3824. {
  3825. int ret = 0, i, count = 0, times, pending = 0;
  3826. WC_DECLARE_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  3827. sizeof(Aes), HEAP_HINT);
  3828. #ifdef HAVE_AES_DECRYPT
  3829. WC_DECLARE_ARRAY(dec, Aes, BENCH_MAX_PENDING,
  3830. sizeof(Aes), HEAP_HINT);
  3831. #endif
  3832. double start;
  3833. DECLARE_MULTI_VALUE_STATS_VARS()
  3834. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  3835. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  3836. WC_ALLOC_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  3837. WC_ALLOC_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  3838. WC_CALLOC_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  3839. sizeof(Aes), HEAP_HINT);
  3840. #ifdef HAVE_AES_DECRYPT
  3841. WC_CALLOC_ARRAY(dec, Aes, BENCH_MAX_PENDING,
  3842. sizeof(Aes), HEAP_HINT);
  3843. #endif
  3844. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  3845. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  3846. /* init keys */
  3847. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3848. if ((ret = wc_AesInit(enc[i], HEAP_HINT,
  3849. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  3850. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  3851. goto exit;
  3852. }
  3853. ret = wc_AesGcmSetKey(enc[i], key, keySz);
  3854. if (ret != 0) {
  3855. printf("AesGcmSetKey failed, ret = %d\n", ret);
  3856. goto exit;
  3857. }
  3858. }
  3859. /* GCM uses same routine in backend for both encrypt and decrypt */
  3860. bench_stats_start(&count, &start);
  3861. do {
  3862. for (times = 0; times < numBlocks || pending > 0; ) {
  3863. bench_async_poll(&pending);
  3864. /* while free pending slots in queue, submit ops */
  3865. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3866. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  3867. &times, numBlocks, &pending)) {
  3868. ret = wc_AesGcmEncrypt(enc[i], bench_cipher,
  3869. bench_plain, bench_size,
  3870. iv, ivSz, bench_tag, AES_AUTH_TAG_SZ,
  3871. bench_additional, aesAuthAddSz);
  3872. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  3873. 0, &times, &pending)) {
  3874. goto exit_aes_gcm;
  3875. }
  3876. }
  3877. } /* for i */
  3878. RECORD_MULTI_VALUE_STATS();
  3879. } /* for times */
  3880. count += times;
  3881. } while (bench_stats_check(start)
  3882. #ifdef MULTI_VALUE_STATISTICS
  3883. || runs < minimum_runs
  3884. #endif
  3885. );
  3886. exit_aes_gcm:
  3887. bench_stats_sym_finish(encLabel, useDeviceID, count, bench_size,
  3888. start, ret);
  3889. #ifdef MULTI_VALUE_STATISTICS
  3890. bench_multi_value_stats(max, min, sum, squareSum, runs);
  3891. #endif
  3892. #ifdef HAVE_AES_DECRYPT
  3893. RESET_MULTI_VALUE_STATS_VARS();
  3894. /* init keys */
  3895. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3896. if ((ret = wc_AesInit(dec[i], HEAP_HINT,
  3897. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  3898. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  3899. goto exit;
  3900. }
  3901. ret = wc_AesGcmSetKey(dec[i], key, keySz);
  3902. if (ret != 0) {
  3903. printf("AesGcmSetKey failed, ret = %d\n", ret);
  3904. goto exit;
  3905. }
  3906. }
  3907. bench_stats_start(&count, &start);
  3908. do {
  3909. for (times = 0; times < numBlocks || pending > 0; ) {
  3910. bench_async_poll(&pending);
  3911. /* while free pending slots in queue, submit ops */
  3912. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3913. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(dec[i]), 0,
  3914. &times, numBlocks, &pending)) {
  3915. ret = wc_AesGcmDecrypt(dec[i], bench_plain,
  3916. bench_cipher, bench_size,
  3917. iv, ivSz, bench_tag, AES_AUTH_TAG_SZ,
  3918. bench_additional, aesAuthAddSz);
  3919. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(dec[i]),
  3920. 0, &times, &pending)) {
  3921. goto exit_aes_gcm_dec;
  3922. }
  3923. }
  3924. } /* for i */
  3925. RECORD_MULTI_VALUE_STATS();
  3926. } /* for times */
  3927. count += times;
  3928. } while (bench_stats_check(start)
  3929. #ifdef MULTI_VALUE_STATISTICS
  3930. || runs < minimum_runs
  3931. #endif
  3932. );
  3933. exit_aes_gcm_dec:
  3934. bench_stats_sym_finish(decLabel, useDeviceID, count, bench_size,
  3935. start, ret);
  3936. #ifdef MULTI_VALUE_STATISTICS
  3937. bench_multi_value_stats(max, min, sum, squareSum, runs);
  3938. #endif
  3939. #endif /* HAVE_AES_DECRYPT */
  3940. (void)decLabel;
  3941. exit:
  3942. if (ret < 0) {
  3943. printf("bench_aesgcm failed: %d\n", ret);
  3944. }
  3945. #ifdef HAVE_AES_DECRYPT
  3946. if (WC_ARRAY_OK(dec)) {
  3947. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3948. wc_AesFree(dec[i]);
  3949. }
  3950. WC_FREE_ARRAY(dec, BENCH_MAX_PENDING, HEAP_HINT);
  3951. }
  3952. #endif
  3953. if (WC_ARRAY_OK(enc)) {
  3954. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3955. wc_AesFree(enc[i]);
  3956. }
  3957. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  3958. }
  3959. WC_FREE_VAR(bench_additional, HEAP_HINT);
  3960. WC_FREE_VAR(bench_tag, HEAP_HINT);
  3961. }
  3962. #ifdef WOLFSSL_AESGCM_STREAM
  3963. static void bench_aesgcm_stream_internal(int useDeviceID,
  3964. const byte* key, word32 keySz, const byte* iv, word32 ivSz,
  3965. const char* encLabel, const char* decLabel)
  3966. {
  3967. int ret = 0, i, count = 0, times, pending = 0;
  3968. WC_DECLARE_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  3969. sizeof(Aes), HEAP_HINT);
  3970. #ifdef HAVE_AES_DECRYPT
  3971. WC_DECLARE_ARRAY(dec, Aes, BENCH_MAX_PENDING,
  3972. sizeof(Aes), HEAP_HINT);
  3973. #endif
  3974. double start;
  3975. DECLARE_MULTI_VALUE_STATS_VARS()
  3976. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  3977. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  3978. WC_ALLOC_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  3979. WC_ALLOC_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  3980. WC_CALLOC_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  3981. sizeof(Aes), HEAP_HINT);
  3982. #ifdef HAVE_AES_DECRYPT
  3983. WC_CALLOC_ARRAY(dec, Aes, BENCH_MAX_PENDING,
  3984. sizeof(Aes), HEAP_HINT);
  3985. #endif
  3986. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  3987. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  3988. /* init keys */
  3989. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3990. if ((ret = wc_AesInit(enc[i], HEAP_HINT,
  3991. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  3992. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  3993. goto exit;
  3994. }
  3995. ret = wc_AesGcmSetKey(enc[i], key, keySz);
  3996. if (ret != 0) {
  3997. printf("AesGcmSetKey failed, ret = %d\n", ret);
  3998. goto exit;
  3999. }
  4000. }
  4001. /* GCM uses same routine in backend for both encrypt and decrypt */
  4002. bench_stats_start(&count, &start);
  4003. do {
  4004. for (times = 0; times < numBlocks || pending > 0; ) {
  4005. bench_async_poll(&pending);
  4006. /* while free pending slots in queue, submit ops */
  4007. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4008. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  4009. &times, numBlocks, &pending)) {
  4010. ret = wc_AesGcmEncryptInit(enc[i], NULL, 0, iv, ivSz);
  4011. if (ret == 0) {
  4012. ret = wc_AesGcmEncryptUpdate(enc[i], bench_cipher,
  4013. bench_plain, bench_size, bench_additional,
  4014. aesAuthAddSz);
  4015. }
  4016. if (ret == 0) {
  4017. ret = wc_AesGcmEncryptFinal(enc[i], bench_tag,
  4018. AES_AUTH_TAG_SZ);
  4019. }
  4020. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  4021. 0, &times, &pending)) {
  4022. goto exit_aes_gcm;
  4023. }
  4024. }
  4025. } /* for i */
  4026. RECORD_MULTI_VALUE_STATS();
  4027. } /* for times */
  4028. count += times;
  4029. } while (bench_stats_check(start)
  4030. #ifdef MULTI_VALUE_STATISTICS
  4031. || runs < minimum_runs
  4032. #endif
  4033. );
  4034. exit_aes_gcm:
  4035. bench_stats_sym_finish(encLabel, useDeviceID, count, bench_size,
  4036. start, ret);
  4037. #ifdef MULTI_VALUE_STATISTICS
  4038. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4039. #endif
  4040. #ifdef HAVE_AES_DECRYPT
  4041. /* init keys */
  4042. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4043. if ((ret = wc_AesInit(dec[i], HEAP_HINT,
  4044. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  4045. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  4046. goto exit;
  4047. }
  4048. ret = wc_AesGcmSetKey(dec[i], key, keySz);
  4049. if (ret != 0) {
  4050. printf("AesGcmSetKey failed, ret = %d\n", ret);
  4051. goto exit;
  4052. }
  4053. }
  4054. RESET_MULTI_VALUE_STATS_VARS();
  4055. bench_stats_start(&count, &start);
  4056. do {
  4057. for (times = 0; times < numBlocks || pending > 0; ) {
  4058. bench_async_poll(&pending);
  4059. /* while free pending slots in queue, submit ops */
  4060. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4061. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(dec[i]), 0,
  4062. &times, numBlocks, &pending)) {
  4063. ret = wc_AesGcmDecryptInit(enc[i], NULL, 0, iv, ivSz);
  4064. if (ret == 0) {
  4065. ret = wc_AesGcmDecryptUpdate(enc[i], bench_plain,
  4066. bench_cipher, bench_size, bench_additional,
  4067. aesAuthAddSz);
  4068. }
  4069. if (ret == 0) {
  4070. ret = wc_AesGcmDecryptFinal(enc[i], bench_tag,
  4071. AES_AUTH_TAG_SZ);
  4072. }
  4073. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(dec[i]),
  4074. 0, &times, &pending)) {
  4075. goto exit_aes_gcm_dec;
  4076. }
  4077. }
  4078. } /* for i */
  4079. RECORD_MULTI_VALUE_STATS();
  4080. } /* for times */
  4081. count += times;
  4082. } while (bench_stats_check(start)
  4083. #ifdef MULTI_VALUE_STATISTICS
  4084. || runs < minimum_runs
  4085. #endif
  4086. );
  4087. exit_aes_gcm_dec:
  4088. bench_stats_sym_finish(decLabel, useDeviceID, count, bench_size,
  4089. start, ret);
  4090. #ifdef MULTI_VALUE_STATISTICS
  4091. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4092. #endif
  4093. #endif /* HAVE_AES_DECRYPT */
  4094. (void)decLabel;
  4095. exit:
  4096. if (ret < 0) {
  4097. printf("bench_aesgcm failed: %d\n", ret);
  4098. }
  4099. #ifdef HAVE_AES_DECRYPT
  4100. if (WC_ARRAY_OK(dec)) {
  4101. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4102. wc_AesFree(dec[i]);
  4103. }
  4104. WC_FREE_ARRAY(dec, BENCH_MAX_PENDING, HEAP_HINT);
  4105. }
  4106. #endif
  4107. if (WC_ARRAY_OK(enc)) {
  4108. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4109. wc_AesFree(enc[i]);
  4110. }
  4111. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  4112. }
  4113. WC_FREE_VAR(bench_additional, HEAP_HINT);
  4114. WC_FREE_VAR(bench_tag, HEAP_HINT);
  4115. }
  4116. #endif
  4117. void bench_aesgcm(int useDeviceID)
  4118. {
  4119. #define AES_GCM_STRING(n, dir) AES_AAD_STRING("AES-" #n "-GCM-" #dir)
  4120. #if defined(WOLFSSL_AES_128) && !defined(WOLFSSL_AFALG_XILINX_AES) \
  4121. && !defined(WOLFSSL_XILINX_CRYPT) \
  4122. || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  4123. #ifdef HAVE_RENESAS_SYNC
  4124. bench_aesgcm_internal(useDeviceID, bench_key1, 16, bench_iv, 12,
  4125. AES_GCM_STRING(128, enc), AES_GCM_STRING(128, dec));
  4126. #else
  4127. bench_aesgcm_internal(useDeviceID, bench_key, 16, bench_iv, 12,
  4128. AES_GCM_STRING(128, enc), AES_GCM_STRING(128, dec));
  4129. #endif
  4130. #endif
  4131. #if defined(WOLFSSL_AES_192) && !defined(WOLFSSL_AFALG_XILINX_AES) \
  4132. && !defined(WOLFSSL_XILINX_CRYPT)
  4133. bench_aesgcm_internal(useDeviceID, bench_key, 24, bench_iv, 12,
  4134. AES_GCM_STRING(192, enc), AES_GCM_STRING(192, dec));
  4135. #endif
  4136. #ifdef WOLFSSL_AES_256
  4137. #ifdef HAVE_RENESAS_SYNC
  4138. bench_aesgcm_internal(useDeviceID, bench_key2, 32, bench_iv, 12,
  4139. AES_GCM_STRING(256, enc), AES_GCM_STRING(256, dec));
  4140. #else
  4141. bench_aesgcm_internal(useDeviceID, bench_key, 32, bench_iv, 12,
  4142. AES_GCM_STRING(256, enc), AES_GCM_STRING(256, dec));
  4143. #endif
  4144. #endif
  4145. #ifdef WOLFSSL_AESGCM_STREAM
  4146. #undef AES_GCM_STRING
  4147. #define AES_GCM_STRING(n, dir) AES_AAD_STRING("AES-" #n "-GCM-STREAM-" #dir)
  4148. #if defined(WOLFSSL_AES_128) && !defined(WOLFSSL_AFALG_XILINX_AES) \
  4149. && !defined(WOLFSSL_XILINX_CRYPT) \
  4150. || defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  4151. bench_aesgcm_stream_internal(useDeviceID, bench_key, 16, bench_iv, 12,
  4152. AES_GCM_STRING(128, enc), AES_GCM_STRING(128, dec));
  4153. #endif
  4154. #if defined(WOLFSSL_AES_192) && !defined(WOLFSSL_AFALG_XILINX_AES) \
  4155. && !defined(WOLFSSL_XILINX_CRYPT)
  4156. bench_aesgcm_stream_internal(useDeviceID, bench_key, 24, bench_iv, 12,
  4157. AES_GCM_STRING(192, enc), AES_GCM_STRING(192, dec));
  4158. #endif
  4159. #ifdef WOLFSSL_AES_256
  4160. bench_aesgcm_stream_internal(useDeviceID, bench_key, 32, bench_iv, 12,
  4161. AES_GCM_STRING(256, enc), AES_GCM_STRING(256, dec));
  4162. #endif
  4163. #endif /* WOLFSSL_AESGCM_STREAM */
  4164. #undef AES_GCM_STRING
  4165. }
  4166. /* GMAC */
  4167. void bench_gmac(int useDeviceID)
  4168. {
  4169. int ret, count = 0;
  4170. Gmac gmac;
  4171. double start;
  4172. byte tag[AES_AUTH_TAG_SZ];
  4173. DECLARE_MULTI_VALUE_STATS_VARS()
  4174. /* determine GCM GHASH method */
  4175. #ifdef GCM_SMALL
  4176. const char* gmacStr = "GMAC Small";
  4177. #elif defined(GCM_TABLE)
  4178. const char* gmacStr = "GMAC Table";
  4179. #elif defined(GCM_TABLE_4BIT)
  4180. const char* gmacStr = "GMAC Table 4-bit";
  4181. #elif defined(GCM_WORD32)
  4182. const char* gmacStr = "GMAC Word32";
  4183. #else
  4184. const char* gmacStr = "GMAC Default";
  4185. #endif
  4186. /* init keys */
  4187. XMEMSET(bench_plain, 0, bench_size);
  4188. XMEMSET(tag, 0, sizeof(tag));
  4189. XMEMSET(&gmac, 0, sizeof(Gmac)); /* clear context */
  4190. (void)wc_AesInit((Aes*)&gmac, HEAP_HINT,
  4191. useDeviceID ? devId: INVALID_DEVID);
  4192. #ifdef HAVE_RENESAS_SYNC
  4193. wc_GmacSetKey(&gmac, bench_key1, 16);
  4194. #else
  4195. wc_GmacSetKey(&gmac, bench_key, 16);
  4196. #endif
  4197. bench_stats_start(&count, &start);
  4198. do {
  4199. ret = wc_GmacUpdate(&gmac, bench_iv, 12, bench_plain, bench_size,
  4200. tag, sizeof(tag));
  4201. count++;
  4202. RECORD_MULTI_VALUE_STATS();
  4203. } while (bench_stats_check(start)
  4204. #ifdef MULTI_VALUE_STATISTICS
  4205. || runs < minimum_runs
  4206. #endif
  4207. );
  4208. wc_AesFree((Aes*)&gmac);
  4209. bench_stats_sym_finish(gmacStr, 0, count, bench_size, start, ret);
  4210. #ifdef MULTI_VALUE_STATISTICS
  4211. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4212. #endif
  4213. }
  4214. #endif /* HAVE_AESGCM */
  4215. #ifdef HAVE_AES_ECB
  4216. static void bench_aesecb_internal(int useDeviceID,
  4217. const byte* key, word32 keySz,
  4218. const char* encLabel, const char* decLabel)
  4219. {
  4220. int ret = 0, i, count = 0, times, pending = 0;
  4221. WC_DECLARE_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  4222. sizeof(Aes), HEAP_HINT);
  4223. double start;
  4224. DECLARE_MULTI_VALUE_STATS_VARS()
  4225. #ifdef HAVE_FIPS
  4226. const int benchSz = AES_BLOCK_SIZE;
  4227. #else
  4228. const int benchSz = (int)bench_size;
  4229. #endif
  4230. WC_CALLOC_ARRAY(enc, Aes, BENCH_MAX_PENDING,
  4231. sizeof(Aes), HEAP_HINT);
  4232. /* init keys */
  4233. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4234. if ((ret = wc_AesInit(enc[i], HEAP_HINT,
  4235. useDeviceID ? devId: INVALID_DEVID)) != 0) {
  4236. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  4237. goto exit;
  4238. }
  4239. ret = wc_AesSetKey(enc[i], key, keySz, bench_iv, AES_ENCRYPTION);
  4240. if (ret != 0) {
  4241. printf("AesSetKey failed, ret = %d\n", ret);
  4242. goto exit;
  4243. }
  4244. }
  4245. bench_stats_start(&count, &start);
  4246. do {
  4247. int outer_loop_limit = (((int)bench_size / benchSz) * 10) + 1;
  4248. for (times = 0;
  4249. times < outer_loop_limit /* numBlocks */ || pending > 0;
  4250. ) {
  4251. bench_async_poll(&pending);
  4252. /* while free pending slots in queue, submit ops */
  4253. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4254. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  4255. &times, outer_loop_limit, &pending)) {
  4256. #ifdef HAVE_FIPS
  4257. wc_AesEncryptDirect(enc[i], bench_cipher, bench_plain);
  4258. #else
  4259. wc_AesEcbEncrypt(enc[i], bench_cipher, bench_plain,
  4260. benchSz);
  4261. #endif
  4262. ret = 0;
  4263. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  4264. 0, &times, &pending)) {
  4265. goto exit_aes_enc;
  4266. }
  4267. }
  4268. } /* for i */
  4269. RECORD_MULTI_VALUE_STATS();
  4270. } /* for times */
  4271. count += times;
  4272. } while (bench_stats_check(start)
  4273. #ifdef MULTI_VALUE_STATISTICS
  4274. || runs < minimum_runs
  4275. #endif
  4276. );
  4277. exit_aes_enc:
  4278. bench_stats_sym_finish(encLabel, useDeviceID, count, benchSz,
  4279. start, ret);
  4280. #ifdef MULTI_VALUE_STATISTICS
  4281. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4282. #endif
  4283. #ifdef HAVE_AES_DECRYPT
  4284. /* init keys */
  4285. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4286. ret = wc_AesSetKey(enc[i], key, keySz, bench_iv, AES_DECRYPTION);
  4287. if (ret != 0) {
  4288. printf("AesSetKey failed, ret = %d\n", ret);
  4289. goto exit;
  4290. }
  4291. }
  4292. RESET_MULTI_VALUE_STATS_VARS();
  4293. bench_stats_start(&count, &start);
  4294. do {
  4295. int outer_loop_limit = (10 * ((int)bench_size / benchSz)) + 1;
  4296. for (times = 0; times < outer_loop_limit || pending > 0; ) {
  4297. bench_async_poll(&pending);
  4298. /* while free pending slots in queue, submit ops */
  4299. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4300. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  4301. &times, outer_loop_limit, &pending)) {
  4302. #ifdef HAVE_FIPS
  4303. wc_AesDecryptDirect(enc[i], bench_plain, bench_cipher);
  4304. #else
  4305. wc_AesEcbDecrypt(enc[i], bench_plain, bench_cipher,
  4306. benchSz);
  4307. #endif
  4308. ret = 0;
  4309. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  4310. 0, &times, &pending)) {
  4311. goto exit_aes_dec;
  4312. }
  4313. }
  4314. } /* for i */
  4315. RECORD_MULTI_VALUE_STATS();
  4316. } /* for times */
  4317. count += times;
  4318. } while (bench_stats_check(start)
  4319. #ifdef MULTI_VALUE_STATISTICS
  4320. || runs < minimum_runs
  4321. #endif
  4322. );
  4323. exit_aes_dec:
  4324. bench_stats_sym_finish(decLabel, useDeviceID, count, benchSz,
  4325. start, ret);
  4326. #ifdef MULTI_VALUE_STATISTICS
  4327. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4328. #endif
  4329. #endif /* HAVE_AES_DECRYPT */
  4330. exit:
  4331. if (WC_ARRAY_OK(enc)) {
  4332. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  4333. wc_AesFree(enc[i]);
  4334. }
  4335. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  4336. }
  4337. }
  4338. void bench_aesecb(int useDeviceID)
  4339. {
  4340. #ifdef WOLFSSL_AES_128
  4341. bench_aesecb_internal(useDeviceID, bench_key, 16,
  4342. "AES-128-ECB-enc", "AES-128-ECB-dec");
  4343. #endif
  4344. #ifdef WOLFSSL_AES_192
  4345. bench_aesecb_internal(useDeviceID, bench_key, 24,
  4346. "AES-192-ECB-enc", "AES-192-ECB-dec");
  4347. #endif
  4348. #ifdef WOLFSSL_AES_256
  4349. bench_aesecb_internal(useDeviceID, bench_key, 32,
  4350. "AES-256-ECB-enc", "AES-256-ECB-dec");
  4351. #endif
  4352. }
  4353. #endif /* HAVE_AES_ECB */
  4354. #ifdef WOLFSSL_AES_CFB
  4355. static void bench_aescfb_internal(const byte* key,
  4356. word32 keySz, const byte* iv,
  4357. const char* label)
  4358. {
  4359. Aes enc;
  4360. double start;
  4361. int i, ret, count;
  4362. DECLARE_MULTI_VALUE_STATS_VARS()
  4363. ret = wc_AesInit(&enc, HEAP_HINT, INVALID_DEVID);
  4364. if (ret != 0) {
  4365. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  4366. return;
  4367. }
  4368. ret = wc_AesSetKey(&enc, key, keySz, iv, AES_ENCRYPTION);
  4369. if (ret != 0) {
  4370. printf("AesSetKey failed, ret = %d\n", ret);
  4371. goto out;
  4372. }
  4373. bench_stats_start(&count, &start);
  4374. do {
  4375. for (i = 0; i < numBlocks; i++) {
  4376. if((ret = wc_AesCfbEncrypt(&enc, bench_plain, bench_cipher,
  4377. bench_size)) != 0) {
  4378. printf("wc_AesCfbEncrypt failed, ret = %d\n", ret);
  4379. goto out;
  4380. }
  4381. RECORD_MULTI_VALUE_STATS();
  4382. }
  4383. count += i;
  4384. } while (bench_stats_check(start)
  4385. #ifdef MULTI_VALUE_STATISTICS
  4386. || runs < minimum_runs
  4387. #endif
  4388. );
  4389. bench_stats_sym_finish(label, 0, count, bench_size, start, ret);
  4390. #ifdef MULTI_VALUE_STATISTICS
  4391. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4392. #endif
  4393. out:
  4394. wc_AesFree(&enc);
  4395. return;
  4396. }
  4397. void bench_aescfb(void)
  4398. {
  4399. #ifdef WOLFSSL_AES_128
  4400. bench_aescfb_internal(bench_key, 16, bench_iv, "AES-128-CFB");
  4401. #endif
  4402. #ifdef WOLFSSL_AES_192
  4403. bench_aescfb_internal(bench_key, 24, bench_iv, "AES-192-CFB");
  4404. #endif
  4405. #ifdef WOLFSSL_AES_256
  4406. bench_aescfb_internal(bench_key, 32, bench_iv, "AES-256-CFB");
  4407. #endif
  4408. }
  4409. #endif /* WOLFSSL_AES_CFB */
  4410. #ifdef WOLFSSL_AES_OFB
  4411. static void bench_aesofb_internal(const byte* key,
  4412. word32 keySz, const byte* iv,
  4413. const char* label)
  4414. {
  4415. Aes enc;
  4416. double start;
  4417. int i, ret, count;
  4418. DECLARE_MULTI_VALUE_STATS_VARS()
  4419. ret = wc_AesInit(&enc, NULL, INVALID_DEVID);
  4420. if (ret != 0) {
  4421. printf("AesInit failed at L%d, ret = %d\n", __LINE__, ret);
  4422. return;
  4423. }
  4424. ret = wc_AesSetKey(&enc, key, keySz, iv, AES_ENCRYPTION);
  4425. if (ret != 0) {
  4426. printf("AesSetKey failed, ret = %d\n", ret);
  4427. return;
  4428. }
  4429. bench_stats_start(&count, &start);
  4430. do {
  4431. for (i = 0; i < numBlocks; i++) {
  4432. if((ret = wc_AesOfbEncrypt(&enc, bench_plain, bench_cipher,
  4433. bench_size)) != 0) {
  4434. printf("wc_AesCfbEncrypt failed, ret = %d\n", ret);
  4435. return;
  4436. }
  4437. RECORD_MULTI_VALUE_STATS();
  4438. }
  4439. count += i;
  4440. } while (bench_stats_check(start)
  4441. #ifdef MULTI_VALUE_STATISTICS
  4442. || runs < minimum_runs
  4443. #endif
  4444. );
  4445. bench_stats_sym_finish(label, 0, count, bench_size, start, ret);
  4446. #ifdef MULTI_VALUE_STATISTICS
  4447. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4448. #endif
  4449. wc_AesFree(&enc);
  4450. }
  4451. void bench_aesofb(void)
  4452. {
  4453. #ifdef WOLFSSL_AES_128
  4454. bench_aesofb_internal(bench_key, 16, bench_iv, "AES-128-OFB");
  4455. #endif
  4456. #ifdef WOLFSSL_AES_192
  4457. bench_aesofb_internal(bench_key, 24, bench_iv, "AES-192-OFB");
  4458. #endif
  4459. #ifdef WOLFSSL_AES_256
  4460. bench_aesofb_internal(bench_key, 32, bench_iv, "AES-256-OFB");
  4461. #endif
  4462. }
  4463. #endif /* WOLFSSL_AES_CFB */
  4464. #ifdef WOLFSSL_AES_XTS
  4465. void bench_aesxts(void)
  4466. {
  4467. WC_DECLARE_VAR(aes, XtsAes, 1, HEAP_HINT);
  4468. double start;
  4469. int i, count, ret;
  4470. DECLARE_MULTI_VALUE_STATS_VARS()
  4471. static const unsigned char k1[] = {
  4472. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  4473. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  4474. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  4475. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  4476. };
  4477. static const unsigned char i1[] = {
  4478. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  4479. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  4480. };
  4481. WC_ALLOC_VAR(aes, XtsAes, 1, HEAP_HINT);
  4482. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_ENCRYPTION,
  4483. HEAP_HINT, devId);
  4484. if (ret != 0) {
  4485. printf("wc_AesXtsSetKey failed, ret = %d\n", ret);
  4486. goto exit;
  4487. }
  4488. bench_stats_start(&count, &start);
  4489. do {
  4490. for (i = 0; i < numBlocks; i++) {
  4491. if ((ret = wc_AesXtsEncrypt(aes, bench_cipher, bench_plain,
  4492. bench_size, i1, sizeof(i1))) != 0) {
  4493. printf("wc_AesXtsEncrypt failed, ret = %d\n", ret);
  4494. goto exit;
  4495. }
  4496. RECORD_MULTI_VALUE_STATS();
  4497. }
  4498. count += i;
  4499. } while (bench_stats_check(start)
  4500. #ifdef MULTI_VALUE_STATISTICS
  4501. || runs < minimum_runs
  4502. #endif
  4503. );
  4504. bench_stats_sym_finish("AES-XTS-enc", 0, count, bench_size, start, ret);
  4505. #ifdef MULTI_VALUE_STATISTICS
  4506. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4507. #endif
  4508. wc_AesXtsFree(aes);
  4509. /* decryption benchmark */
  4510. ret = wc_AesXtsSetKey(aes, k1, sizeof(k1), AES_DECRYPTION,
  4511. HEAP_HINT, devId);
  4512. if (ret != 0) {
  4513. printf("wc_AesXtsSetKey failed, ret = %d\n", ret);
  4514. goto exit;
  4515. }
  4516. RESET_MULTI_VALUE_STATS_VARS();
  4517. bench_stats_start(&count, &start);
  4518. do {
  4519. for (i = 0; i < numBlocks; i++) {
  4520. if ((ret = wc_AesXtsDecrypt(aes, bench_plain, bench_cipher,
  4521. bench_size, i1, sizeof(i1))) != 0) {
  4522. printf("wc_AesXtsDecrypt failed, ret = %d\n", ret);
  4523. goto exit;
  4524. }
  4525. RECORD_MULTI_VALUE_STATS();
  4526. }
  4527. count += i;
  4528. } while (bench_stats_check(start)
  4529. #ifdef MULTI_VALUE_STATISTICS
  4530. || runs < minimum_runs
  4531. #endif
  4532. );
  4533. bench_stats_sym_finish("AES-XTS-dec", 0, count, bench_size, start, ret);
  4534. #ifdef MULTI_VALUE_STATISTICS
  4535. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4536. #endif
  4537. exit:
  4538. wc_AesXtsFree(aes);
  4539. WC_FREE_VAR(aes, HEAP_HINT);
  4540. }
  4541. #endif /* WOLFSSL_AES_XTS */
  4542. #ifdef WOLFSSL_AES_COUNTER
  4543. static void bench_aesctr_internal(const byte* key, word32 keySz,
  4544. const byte* iv, const char* label,
  4545. int useDeviceID)
  4546. {
  4547. Aes enc;
  4548. double start;
  4549. int i, count, ret = 0;
  4550. DECLARE_MULTI_VALUE_STATS_VARS()
  4551. if ((ret = wc_AesInit(&enc, HEAP_HINT,
  4552. useDeviceID ? devId : INVALID_DEVID)) != 0) {
  4553. printf("wc_AesInit failed, ret = %d\n", ret);
  4554. }
  4555. if (wc_AesSetKeyDirect(&enc, key, keySz, iv, AES_ENCRYPTION) < 0) {
  4556. printf("wc_AesSetKeyDirect failed, ret = %d\n", ret);
  4557. return;
  4558. }
  4559. bench_stats_start(&count, &start);
  4560. do {
  4561. for (i = 0; i < numBlocks; i++) {
  4562. if((ret = wc_AesCtrEncrypt(&enc, bench_plain, bench_cipher,
  4563. bench_size)) != 0) {
  4564. printf("wc_AesCtrEncrypt failed, ret = %d\n", ret);
  4565. return;
  4566. }
  4567. RECORD_MULTI_VALUE_STATS();
  4568. }
  4569. count += i;
  4570. } while (bench_stats_check(start)
  4571. #ifdef MULTI_VALUE_STATISTICS
  4572. || runs < minimum_runs
  4573. #endif
  4574. );
  4575. bench_stats_sym_finish(label, useDeviceID, count, bench_size, start, ret);
  4576. #ifdef MULTI_VALUE_STATISTICS
  4577. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4578. #endif
  4579. wc_AesFree(&enc);
  4580. }
  4581. void bench_aesctr(int useDeviceID)
  4582. {
  4583. #ifdef WOLFSSL_AES_128
  4584. bench_aesctr_internal(bench_key, 16, bench_iv, "AES-128-CTR", useDeviceID);
  4585. #endif
  4586. #ifdef WOLFSSL_AES_192
  4587. bench_aesctr_internal(bench_key, 24, bench_iv, "AES-192-CTR", useDeviceID);
  4588. #endif
  4589. #ifdef WOLFSSL_AES_256
  4590. bench_aesctr_internal(bench_key, 32, bench_iv, "AES-256-CTR", useDeviceID);
  4591. #endif
  4592. }
  4593. #endif /* WOLFSSL_AES_COUNTER */
  4594. #ifdef HAVE_AESCCM
  4595. void bench_aesccm(int useDeviceID)
  4596. {
  4597. Aes enc;
  4598. int enc_inited = 0;
  4599. double start;
  4600. int ret, i, count;
  4601. DECLARE_MULTI_VALUE_STATS_VARS()
  4602. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4603. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4604. WC_ALLOC_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4605. WC_ALLOC_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4606. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  4607. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  4608. if ((ret = wc_AesInit(&enc, HEAP_HINT,
  4609. useDeviceID ? devId : INVALID_DEVID)) != 0)
  4610. {
  4611. printf("wc_AesInit failed, ret = %d\n", ret);
  4612. goto exit;
  4613. }
  4614. if ((ret = wc_AesCcmSetKey(&enc, bench_key, 16)) != 0) {
  4615. printf("wc_AesCcmSetKey failed, ret = %d\n", ret);
  4616. goto exit;
  4617. }
  4618. enc_inited = 1;
  4619. bench_stats_start(&count, &start);
  4620. do {
  4621. for (i = 0; i < numBlocks; i++) {
  4622. ret |= wc_AesCcmEncrypt(&enc, bench_cipher, bench_plain, bench_size,
  4623. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  4624. bench_additional, 0);
  4625. RECORD_MULTI_VALUE_STATS();
  4626. }
  4627. count += i;
  4628. } while (bench_stats_check(start)
  4629. #ifdef MULTI_VALUE_STATISTICS
  4630. || runs < minimum_runs
  4631. #endif
  4632. );
  4633. bench_stats_sym_finish(AES_AAD_STRING("AES-CCM-enc"), useDeviceID, count,
  4634. bench_size, start, ret);
  4635. #ifdef MULTI_VALUE_STATISTICS
  4636. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4637. #endif
  4638. if (ret != 0) {
  4639. printf("wc_AesCcmEncrypt failed, ret = %d\n", ret);
  4640. goto exit;
  4641. }
  4642. RESET_MULTI_VALUE_STATS_VARS();
  4643. bench_stats_start(&count, &start);
  4644. do {
  4645. for (i = 0; i < numBlocks; i++) {
  4646. ret |= wc_AesCcmDecrypt(&enc, bench_plain, bench_cipher, bench_size,
  4647. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  4648. bench_additional, 0);
  4649. RECORD_MULTI_VALUE_STATS();
  4650. }
  4651. count += i;
  4652. } while (bench_stats_check(start)
  4653. #ifdef MULTI_VALUE_STATISTICS
  4654. || runs < minimum_runs
  4655. #endif
  4656. );
  4657. bench_stats_sym_finish(AES_AAD_STRING("AES-CCM-dec"), useDeviceID, count,
  4658. bench_size, start, ret);
  4659. #ifdef MULTI_VALUE_STATISTICS
  4660. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4661. #endif
  4662. if (ret != 0) {
  4663. printf("wc_AesCcmEncrypt failed, ret = %d\n", ret);
  4664. goto exit;
  4665. }
  4666. exit:
  4667. if (enc_inited)
  4668. wc_AesFree(&enc);
  4669. WC_FREE_VAR(bench_additional, HEAP_HINT);
  4670. WC_FREE_VAR(bench_tag, HEAP_HINT);
  4671. }
  4672. #endif /* HAVE_AESCCM */
  4673. #ifdef WOLFSSL_AES_SIV
  4674. static void bench_aessiv_internal(const byte* key, word32 keySz, const char*
  4675. encLabel, const char* decLabel)
  4676. {
  4677. int i;
  4678. int ret = 0;
  4679. byte assoc[AES_BLOCK_SIZE];
  4680. byte nonce[AES_BLOCK_SIZE];
  4681. byte siv[AES_BLOCK_SIZE];
  4682. int count = 0;
  4683. double start;
  4684. DECLARE_MULTI_VALUE_STATS_VARS()
  4685. bench_stats_start(&count, &start);
  4686. do {
  4687. for (i = 0; i < numBlocks; i++) {
  4688. ret = wc_AesSivEncrypt(key, keySz, assoc, AES_BLOCK_SIZE, nonce,
  4689. AES_BLOCK_SIZE, bench_plain, bench_size,
  4690. siv, bench_cipher);
  4691. if (ret != 0) {
  4692. printf("wc_AesSivEncrypt failed (%d)\n", ret);
  4693. return;
  4694. }
  4695. RECORD_MULTI_VALUE_STATS();
  4696. }
  4697. count += i;
  4698. } while (bench_stats_check(start)
  4699. #ifdef MULTI_VALUE_STATISTICS
  4700. || runs < minimum_runs
  4701. #endif
  4702. );
  4703. bench_stats_sym_finish(encLabel, 0, count, bench_size, start, ret);
  4704. #ifdef MULTI_VALUE_STATISTICS
  4705. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4706. #endif
  4707. RESET_MULTI_VALUE_STATS_VARS();
  4708. bench_stats_start(&count, &start);
  4709. do {
  4710. for (i = 0; i < numBlocks; i++) {
  4711. ret = wc_AesSivDecrypt(key, keySz, assoc, AES_BLOCK_SIZE, nonce,
  4712. AES_BLOCK_SIZE, bench_cipher, bench_size,
  4713. siv, bench_plain);
  4714. if (ret != 0) {
  4715. printf("wc_AesSivDecrypt failed (%d)\n", ret);
  4716. return;
  4717. }
  4718. RECORD_MULTI_VALUE_STATS();
  4719. }
  4720. count += i;
  4721. } while (bench_stats_check(start)
  4722. #ifdef MULTI_VALUE_STATISTICS
  4723. || runs < minimum_runs
  4724. #endif
  4725. );
  4726. bench_stats_sym_finish(decLabel, 0, count, bench_size, start, ret);
  4727. #ifdef MULTI_VALUE_STATISTICS
  4728. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4729. #endif
  4730. }
  4731. void bench_aessiv(void)
  4732. {
  4733. bench_aessiv_internal(bench_key, 32, "AES-256-SIV-enc", "AES-256-SIV-dec");
  4734. bench_aessiv_internal(bench_key, 48, "AES-384-SIV-enc", "AES-384-SIV-dec");
  4735. bench_aessiv_internal(bench_key, 64, "AES-512-SIV-enc", "AES-512-SIV-dec");
  4736. }
  4737. #endif /* WOLFSSL_AES_SIV */
  4738. #endif /* !NO_AES */
  4739. #ifdef HAVE_POLY1305
  4740. void bench_poly1305(void)
  4741. {
  4742. Poly1305 enc;
  4743. byte mac[16];
  4744. double start;
  4745. int ret = 0, i, count;
  4746. DECLARE_MULTI_VALUE_STATS_VARS()
  4747. if (digest_stream) {
  4748. ret = wc_Poly1305SetKey(&enc, bench_key, 32);
  4749. if (ret != 0) {
  4750. printf("Poly1305SetKey failed, ret = %d\n", ret);
  4751. return;
  4752. }
  4753. bench_stats_start(&count, &start);
  4754. do {
  4755. for (i = 0; i < numBlocks; i++) {
  4756. ret = wc_Poly1305Update(&enc, bench_plain, bench_size);
  4757. if (ret != 0) {
  4758. printf("Poly1305Update failed: %d\n", ret);
  4759. break;
  4760. }
  4761. RECORD_MULTI_VALUE_STATS();
  4762. }
  4763. wc_Poly1305Final(&enc, mac);
  4764. count += i;
  4765. } while (bench_stats_check(start)
  4766. #ifdef MULTI_VALUE_STATISTICS
  4767. || runs < minimum_runs
  4768. #endif
  4769. );
  4770. }
  4771. else {
  4772. bench_stats_start(&count, &start);
  4773. do {
  4774. for (i = 0; i < numBlocks; i++) {
  4775. ret = wc_Poly1305SetKey(&enc, bench_key, 32);
  4776. if (ret != 0) {
  4777. printf("Poly1305SetKey failed, ret = %d\n", ret);
  4778. return;
  4779. }
  4780. ret = wc_Poly1305Update(&enc, bench_plain, bench_size);
  4781. if (ret != 0) {
  4782. printf("Poly1305Update failed: %d\n", ret);
  4783. break;
  4784. }
  4785. wc_Poly1305Final(&enc, mac);
  4786. RECORD_MULTI_VALUE_STATS();
  4787. }
  4788. count += i;
  4789. } while (bench_stats_check(start)
  4790. #ifdef MULTI_VALUE_STATISTICS
  4791. || runs < minimum_runs
  4792. #endif
  4793. );
  4794. }
  4795. bench_stats_sym_finish("POLY1305", 0, count, bench_size, start, ret);
  4796. #ifdef MULTI_VALUE_STATISTICS
  4797. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4798. #endif
  4799. }
  4800. #endif /* HAVE_POLY1305 */
  4801. #ifdef HAVE_CAMELLIA
  4802. void bench_camellia(void)
  4803. {
  4804. Camellia cam;
  4805. double start;
  4806. int ret, i, count;
  4807. DECLARE_MULTI_VALUE_STATS_VARS()
  4808. ret = wc_CamelliaSetKey(&cam, bench_key, 16, bench_iv);
  4809. if (ret != 0) {
  4810. printf("CamelliaSetKey failed, ret = %d\n", ret);
  4811. return;
  4812. }
  4813. bench_stats_start(&count, &start);
  4814. do {
  4815. for (i = 0; i < numBlocks; i++) {
  4816. ret = wc_CamelliaCbcEncrypt(&cam, bench_cipher, bench_plain,
  4817. bench_size);
  4818. if (ret < 0) {
  4819. printf("CamelliaCbcEncrypt failed: %d\n", ret);
  4820. return;
  4821. }
  4822. RECORD_MULTI_VALUE_STATS();
  4823. }
  4824. count += i;
  4825. } while (bench_stats_check(start)
  4826. #ifdef MULTI_VALUE_STATISTICS
  4827. || runs < minimum_runs
  4828. #endif
  4829. );
  4830. bench_stats_sym_finish("Camellia", 0, count, bench_size, start, ret);
  4831. #ifdef MULTI_VALUE_STATISTICS
  4832. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4833. #endif
  4834. }
  4835. #endif
  4836. #ifdef WOLFSSL_SM4_CBC
  4837. void bench_sm4_cbc(void)
  4838. {
  4839. wc_Sm4 sm4;
  4840. double start;
  4841. int ret;
  4842. int i;
  4843. int count;
  4844. DECLARE_MULTI_VALUE_STATS_VARS()
  4845. ret = wc_Sm4SetKey(&sm4, bench_key, SM4_KEY_SIZE);
  4846. if (ret != 0) {
  4847. printf("Sm4SetKey failed, ret = %d\n", ret);
  4848. return;
  4849. }
  4850. ret = wc_Sm4SetIV(&sm4, bench_iv);
  4851. if (ret != 0) {
  4852. printf("Sm4SetIV failed, ret = %d\n", ret);
  4853. return;
  4854. }
  4855. bench_stats_start(&count, &start);
  4856. do {
  4857. for (i = 0; i < numBlocks; i++) {
  4858. ret = wc_Sm4CbcEncrypt(&sm4, bench_cipher, bench_plain, bench_size);
  4859. if (ret < 0) {
  4860. printf("Sm4CbcEncrypt failed: %d\n", ret);
  4861. return;
  4862. }
  4863. RECORD_MULTI_VALUE_STATS();
  4864. }
  4865. count += i;
  4866. } while (bench_stats_check(start)
  4867. #ifdef MULTI_VALUE_STATISTICS
  4868. || runs < minimum_runs
  4869. #endif
  4870. );
  4871. bench_stats_sym_finish("SM4-CBC-enc", 0, count, bench_size, start, ret);
  4872. #ifdef MULTI_VALUE_STATISTICS
  4873. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4874. #endif
  4875. RESET_MULTI_VALUE_STATS_VARS();
  4876. bench_stats_start(&count, &start);
  4877. do {
  4878. for (i = 0; i < numBlocks; i++) {
  4879. ret = wc_Sm4CbcDecrypt(&sm4, bench_plain, bench_cipher, bench_size);
  4880. if (ret < 0) {
  4881. printf("Sm4CbcDecrypt failed: %d\n", ret);
  4882. return;
  4883. }
  4884. RECORD_MULTI_VALUE_STATS();
  4885. }
  4886. count += i;
  4887. } while (bench_stats_check(start)
  4888. #ifdef MULTI_VALUE_STATISTICS
  4889. || runs < minimum_runs
  4890. #endif
  4891. );
  4892. bench_stats_sym_finish("SM4-CBC-dec", 0, count, bench_size, start, ret);
  4893. #ifdef MULTI_VALUE_STATISTICS
  4894. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4895. #endif
  4896. }
  4897. #endif
  4898. #ifdef WOLFSSL_SM4_GCM
  4899. void bench_sm4_gcm(void)
  4900. {
  4901. wc_Sm4 sm4;
  4902. double start;
  4903. int ret;
  4904. int i;
  4905. int count;
  4906. DECLARE_MULTI_VALUE_STATS_VARS()
  4907. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4908. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4909. WC_ALLOC_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4910. WC_ALLOC_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4911. ret = wc_Sm4GcmSetKey(&sm4, bench_key, SM4_KEY_SIZE);
  4912. if (ret != 0) {
  4913. printf("Sm4GcmSetKey failed, ret = %d\n", ret);
  4914. goto exit;
  4915. }
  4916. bench_stats_start(&count, &start);
  4917. do {
  4918. for (i = 0; i < numBlocks; i++) {
  4919. ret = wc_Sm4GcmEncrypt(&sm4, bench_cipher, bench_plain, bench_size,
  4920. bench_iv, GCM_NONCE_MID_SZ, bench_tag, SM4_BLOCK_SIZE,
  4921. bench_additional, aesAuthAddSz);
  4922. if (ret < 0) {
  4923. printf("Sm4GcmEncrypt failed: %d\n", ret);
  4924. goto exit;
  4925. }
  4926. RECORD_MULTI_VALUE_STATS();
  4927. }
  4928. count += i;
  4929. } while (bench_stats_check(start)
  4930. #ifdef MULTI_VALUE_STATISTICS
  4931. || runs < minimum_runs
  4932. #endif
  4933. );
  4934. bench_stats_sym_finish("SM4-GCM-enc", 0, count, bench_size, start, ret);
  4935. #ifdef MULTI_VALUE_STATISTICS
  4936. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4937. #endif
  4938. RESET_MULTI_VALUE_STATS_VARS();
  4939. bench_stats_start(&count, &start);
  4940. do {
  4941. for (i = 0; i < numBlocks; i++) {
  4942. ret = wc_Sm4GcmDecrypt(&sm4, bench_plain, bench_cipher, bench_size,
  4943. bench_iv, GCM_NONCE_MID_SZ, bench_tag, SM4_BLOCK_SIZE,
  4944. bench_additional, aesAuthAddSz);
  4945. if (ret < 0) {
  4946. printf("Sm4GcmDecrypt failed: %d\n", ret);
  4947. goto exit;
  4948. }
  4949. RECORD_MULTI_VALUE_STATS();
  4950. }
  4951. count += i;
  4952. } while (bench_stats_check(start)
  4953. #ifdef MULTI_VALUE_STATISTICS
  4954. || runs < minimum_runs
  4955. #endif
  4956. );
  4957. bench_stats_sym_finish("SM4-GCM-dec", 0, count, bench_size, start, ret);
  4958. #ifdef MULTI_VALUE_STATISTICS
  4959. bench_multi_value_stats(max, min, sum, squareSum, runs);
  4960. #endif
  4961. exit:
  4962. WC_FREE_VAR(bench_additional);
  4963. WC_FREE_VAR(bench_tag);
  4964. }
  4965. #endif
  4966. #ifdef WOLFSSL_SM4_CCM
  4967. void bench_sm4_ccm()
  4968. {
  4969. wc_Sm4 enc;
  4970. double start;
  4971. int ret, i, count;
  4972. DECLARE_MULTI_VALUE_STATS_VARS()
  4973. WC_DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4974. WC_DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4975. WC_ALLOC_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  4976. WC_ALLOC_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  4977. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  4978. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  4979. if ((ret = wc_Sm4SetKey(&enc, bench_key, 16)) != 0) {
  4980. printf("wc_Sm4SetKey failed, ret = %d\n", ret);
  4981. goto exit;
  4982. }
  4983. bench_stats_start(&count, &start);
  4984. do {
  4985. for (i = 0; i < numBlocks; i++) {
  4986. ret |= wc_Sm4CcmEncrypt(&enc, bench_cipher, bench_plain, bench_size,
  4987. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  4988. bench_additional, 0);
  4989. RECORD_MULTI_VALUE_STATS();
  4990. }
  4991. count += i;
  4992. } while (bench_stats_check(start)
  4993. #ifdef MULTI_VALUE_STATISTICS
  4994. || runs < minimum_runs
  4995. #endif
  4996. );
  4997. bench_stats_sym_finish("SM4-CCM-enc", 0, count, bench_size, start, ret);
  4998. #ifdef MULTI_VALUE_STATISTICS
  4999. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5000. #endif
  5001. if (ret != 0) {
  5002. printf("wc_Sm4Encrypt failed, ret = %d\n", ret);
  5003. goto exit;
  5004. }
  5005. RESET_MULTI_VALUE_STATS_VARS();
  5006. bench_stats_start(&count, &start);
  5007. do {
  5008. for (i = 0; i < numBlocks; i++) {
  5009. ret |= wc_Sm4CcmDecrypt(&enc, bench_plain, bench_cipher, bench_size,
  5010. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  5011. bench_additional, 0);
  5012. RECORD_MULTI_VALUE_STATS();
  5013. }
  5014. count += i;
  5015. } while (bench_stats_check(start)
  5016. #ifdef MULTI_VALUE_STATISTICS
  5017. || runs < minimum_runs
  5018. #endif
  5019. );
  5020. bench_stats_sym_finish("SM4-CCM-dec", 0, count, bench_size, start, ret);
  5021. #ifdef MULTI_VALUE_STATISTICS
  5022. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5023. #endif
  5024. if (ret != 0) {
  5025. printf("wc_Sm4Decrypt failed, ret = %d\n", ret);
  5026. goto exit;
  5027. }
  5028. exit:
  5029. WC_FREE_VAR(bench_additional, HEAP_HINT);
  5030. WC_FREE_VAR(bench_tag, HEAP_HINT);
  5031. }
  5032. #endif /* HAVE_AESCCM */
  5033. #ifndef NO_DES3
  5034. void bench_des(int useDeviceID)
  5035. {
  5036. int ret = 0, i, count = 0, times, pending = 0;
  5037. WC_DECLARE_ARRAY(enc, Des3, BENCH_MAX_PENDING,
  5038. sizeof(Des3), HEAP_HINT);
  5039. double start;
  5040. DECLARE_MULTI_VALUE_STATS_VARS()
  5041. WC_CALLOC_ARRAY(enc, Des3, BENCH_MAX_PENDING,
  5042. sizeof(Des3), HEAP_HINT);
  5043. /* init keys */
  5044. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5045. if ((ret = wc_Des3Init(enc[i], HEAP_HINT,
  5046. useDeviceID ? devId : INVALID_DEVID)) != 0) {
  5047. printf("Des3Init failed, ret = %d\n", ret);
  5048. goto exit;
  5049. }
  5050. ret = wc_Des3_SetKey(enc[i], bench_key, bench_iv, DES_ENCRYPTION);
  5051. if (ret != 0) {
  5052. printf("Des3_SetKey failed, ret = %d\n", ret);
  5053. goto exit;
  5054. }
  5055. }
  5056. bench_stats_start(&count, &start);
  5057. do {
  5058. for (times = 0; times < numBlocks || pending > 0; ) {
  5059. bench_async_poll(&pending);
  5060. /* while free pending slots in queue, submit ops */
  5061. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5062. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  5063. &times, numBlocks, &pending)) {
  5064. ret = wc_Des3_CbcEncrypt(enc[i],
  5065. bench_cipher,
  5066. bench_plain, bench_size);
  5067. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  5068. 0, &times, &pending)) {
  5069. goto exit_3des;
  5070. }
  5071. }
  5072. } /* for i */
  5073. RECORD_MULTI_VALUE_STATS();
  5074. } /* for times */
  5075. count += times;
  5076. } while (bench_stats_check(start)
  5077. #ifdef MULTI_VALUE_STATISTICS
  5078. || runs < minimum_runs
  5079. #endif
  5080. );
  5081. exit_3des:
  5082. bench_stats_sym_finish("3DES", useDeviceID, count, bench_size, start, ret);
  5083. #ifdef MULTI_VALUE_STATISTICS
  5084. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5085. #endif
  5086. exit:
  5087. if (WC_ARRAY_OK(enc)) {
  5088. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5089. wc_Des3Free(enc[i]);
  5090. }
  5091. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  5092. }
  5093. }
  5094. #endif /* !NO_DES3 */
  5095. #ifndef NO_RC4
  5096. void bench_arc4(int useDeviceID)
  5097. {
  5098. int ret = 0, i, count = 0, times, pending = 0;
  5099. WC_DECLARE_ARRAY(enc, Arc4, BENCH_MAX_PENDING,
  5100. sizeof(Arc4), HEAP_HINT);
  5101. double start;
  5102. DECLARE_MULTI_VALUE_STATS_VARS()
  5103. WC_CALLOC_ARRAY(enc, Arc4, BENCH_MAX_PENDING,
  5104. sizeof(Arc4), HEAP_HINT);
  5105. /* init keys */
  5106. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5107. if ((ret = wc_Arc4Init(enc[i], HEAP_HINT,
  5108. useDeviceID ? devId : INVALID_DEVID)) != 0) {
  5109. printf("Arc4Init failed, ret = %d\n", ret);
  5110. goto exit;
  5111. }
  5112. ret = wc_Arc4SetKey(enc[i], bench_key, 16);
  5113. if (ret != 0) {
  5114. printf("Arc4SetKey failed, ret = %d\n", ret);
  5115. goto exit;
  5116. }
  5117. }
  5118. bench_stats_start(&count, &start);
  5119. do {
  5120. for (times = 0; times < numBlocks || pending > 0; ) {
  5121. bench_async_poll(&pending);
  5122. /* while free pending slots in queue, submit ops */
  5123. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5124. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(enc[i]), 0,
  5125. &times, numBlocks, &pending)) {
  5126. ret = wc_Arc4Process(enc[i], bench_cipher, bench_plain,
  5127. bench_size);
  5128. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(enc[i]),
  5129. 0, &times, &pending)) {
  5130. goto exit_arc4;
  5131. }
  5132. }
  5133. } /* for i */
  5134. RECORD_MULTI_VALUE_STATS();
  5135. } /* for times */
  5136. count += times;
  5137. } while (bench_stats_check(start)
  5138. #ifdef MULTI_VALUE_STATISTICS
  5139. || runs < minimum_runs
  5140. #endif
  5141. );
  5142. exit_arc4:
  5143. bench_stats_sym_finish("ARC4", useDeviceID, count, bench_size, start, ret);
  5144. #ifdef MULTI_VALUE_STATISTICS
  5145. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5146. #endif
  5147. exit:
  5148. if (WC_ARRAY_OK(enc)) {
  5149. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5150. wc_Arc4Free(enc[i]);
  5151. }
  5152. WC_FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  5153. }
  5154. }
  5155. #endif /* !NO_RC4 */
  5156. #ifdef HAVE_CHACHA
  5157. void bench_chacha(void)
  5158. {
  5159. ChaCha enc;
  5160. double start;
  5161. int i, count;
  5162. DECLARE_MULTI_VALUE_STATS_VARS()
  5163. XMEMSET(&enc, 0, sizeof(enc));
  5164. wc_Chacha_SetKey(&enc, bench_key, 16);
  5165. bench_stats_start(&count, &start);
  5166. do {
  5167. for (i = 0; i < numBlocks; i++) {
  5168. wc_Chacha_SetIV(&enc, bench_iv, 0);
  5169. wc_Chacha_Process(&enc, bench_cipher, bench_plain, bench_size);
  5170. RECORD_MULTI_VALUE_STATS();
  5171. }
  5172. count += i;
  5173. } while (bench_stats_check(start)
  5174. #ifdef MULTI_VALUE_STATISTICS
  5175. || runs < minimum_runs
  5176. #endif
  5177. );
  5178. bench_stats_sym_finish("CHACHA", 0, count, bench_size, start, 0);
  5179. #ifdef MULTI_VALUE_STATISTICS
  5180. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5181. #endif
  5182. }
  5183. #endif /* HAVE_CHACHA*/
  5184. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5185. void bench_chacha20_poly1305_aead(void)
  5186. {
  5187. double start;
  5188. int ret = 0, i, count;
  5189. DECLARE_MULTI_VALUE_STATS_VARS()
  5190. byte authTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  5191. XMEMSET(authTag, 0, sizeof(authTag));
  5192. bench_stats_start(&count, &start);
  5193. do {
  5194. for (i = 0; i < numBlocks; i++) {
  5195. ret = wc_ChaCha20Poly1305_Encrypt(bench_key, bench_iv, NULL, 0,
  5196. bench_plain, bench_size, bench_cipher, authTag);
  5197. if (ret < 0) {
  5198. printf("wc_ChaCha20Poly1305_Encrypt error: %d\n", ret);
  5199. break;
  5200. }
  5201. RECORD_MULTI_VALUE_STATS();
  5202. }
  5203. count += i;
  5204. } while (bench_stats_check(start)
  5205. #ifdef MULTI_VALUE_STATISTICS
  5206. || runs < minimum_runs
  5207. #endif
  5208. );
  5209. bench_stats_sym_finish("CHA-POLY", 0, count, bench_size, start, ret);
  5210. #ifdef MULTI_VALUE_STATISTICS
  5211. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5212. #endif
  5213. }
  5214. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  5215. #ifndef NO_MD5
  5216. void bench_md5(int useDeviceID)
  5217. {
  5218. WC_DECLARE_ARRAY(hash, wc_Md5, BENCH_MAX_PENDING,
  5219. sizeof(wc_Md5), HEAP_HINT);
  5220. double start = 0;
  5221. int ret = 0, i, count = 0, times, pending = 0;
  5222. DECLARE_MULTI_VALUE_STATS_VARS()
  5223. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5224. WC_MD5_DIGEST_SIZE, HEAP_HINT);
  5225. WC_CALLOC_ARRAY(hash, wc_Md5, BENCH_MAX_PENDING,
  5226. sizeof(wc_Md5), HEAP_HINT);
  5227. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5228. WC_MD5_DIGEST_SIZE, HEAP_HINT);
  5229. if (digest_stream) {
  5230. /* init keys */
  5231. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5232. ret = wc_InitMd5_ex(hash[i], HEAP_HINT,
  5233. useDeviceID ? devId : INVALID_DEVID);
  5234. if (ret != 0) {
  5235. printf("InitMd5_ex failed, ret = %d\n", ret);
  5236. goto exit;
  5237. }
  5238. #ifdef WOLFSSL_PIC32MZ_HASH
  5239. wc_Md5SizeSet(hash[i], numBlocks * bench_size);
  5240. #endif
  5241. }
  5242. bench_stats_start(&count, &start);
  5243. do {
  5244. for (times = 0; times < numBlocks || pending > 0; ) {
  5245. bench_async_poll(&pending);
  5246. /* while free pending slots in queue, submit ops */
  5247. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5248. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5249. 0, &times, numBlocks, &pending)) {
  5250. ret = wc_Md5Update(hash[i], bench_plain,
  5251. bench_size);
  5252. if (!bench_async_handle(&ret,
  5253. BENCH_ASYNC_GET_DEV(hash[i]),
  5254. 0, &times, &pending)) {
  5255. goto exit_md5;
  5256. }
  5257. }
  5258. } /* for i */
  5259. RECORD_MULTI_VALUE_STATS();
  5260. } /* for times */
  5261. count += times;
  5262. times = 0;
  5263. do {
  5264. bench_async_poll(&pending);
  5265. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5266. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5267. 0, &times, numBlocks, &pending)) {
  5268. ret = wc_Md5Final(hash[i], digest[i]);
  5269. if (!bench_async_handle(&ret,
  5270. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5271. &times, &pending)) {
  5272. goto exit_md5;
  5273. }
  5274. }
  5275. } /* for i */
  5276. } while (pending > 0);
  5277. } while (bench_stats_check(start)
  5278. #ifdef MULTI_VALUE_STATISTICS
  5279. || runs < minimum_runs
  5280. #endif
  5281. );
  5282. }
  5283. else {
  5284. bench_stats_start(&count, &start);
  5285. do {
  5286. for (times = 0; times < numBlocks; times++) {
  5287. ret = wc_InitMd5_ex(hash[0], HEAP_HINT, INVALID_DEVID);
  5288. if (ret == 0)
  5289. ret = wc_Md5Update(hash[0], bench_plain, bench_size);
  5290. if (ret == 0)
  5291. ret = wc_Md5Final(hash[0], digest[0]);
  5292. if (ret != 0)
  5293. goto exit_md5;
  5294. RECORD_MULTI_VALUE_STATS();
  5295. } /* for times */
  5296. count += times;
  5297. } while (bench_stats_check(start)
  5298. #ifdef MULTI_VALUE_STATISTICS
  5299. || runs < minimum_runs
  5300. #endif
  5301. );
  5302. }
  5303. exit_md5:
  5304. bench_stats_sym_finish("MD5", useDeviceID, count, bench_size, start, ret);
  5305. #ifdef MULTI_VALUE_STATISTICS
  5306. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5307. #endif
  5308. exit:
  5309. #ifdef WOLFSSL_ASYNC_CRYPT
  5310. if (WC_ARRAY_OK(hash)) {
  5311. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5312. wc_Md5Free(hash[i]);
  5313. }
  5314. }
  5315. #endif
  5316. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5317. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5318. }
  5319. #endif /* !NO_MD5 */
  5320. #ifndef NO_SHA
  5321. void bench_sha(int useDeviceID)
  5322. {
  5323. WC_DECLARE_ARRAY(hash, wc_Sha, BENCH_MAX_PENDING,
  5324. sizeof(wc_Sha), HEAP_HINT);
  5325. double start;
  5326. int ret = 0, i, count = 0, times, pending = 0;
  5327. DECLARE_MULTI_VALUE_STATS_VARS()
  5328. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5329. WC_SHA_DIGEST_SIZE, HEAP_HINT);
  5330. WC_CALLOC_ARRAY(hash, wc_Sha, BENCH_MAX_PENDING,
  5331. sizeof(wc_Sha), HEAP_HINT);
  5332. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5333. WC_SHA_DIGEST_SIZE, HEAP_HINT);
  5334. if (digest_stream) {
  5335. /* init keys */
  5336. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5337. ret = wc_InitSha_ex(hash[i], HEAP_HINT,
  5338. useDeviceID ? devId : INVALID_DEVID);
  5339. if (ret != 0) {
  5340. printf("InitSha failed, ret = %d\n", ret);
  5341. goto exit;
  5342. }
  5343. #ifdef WOLFSSL_PIC32MZ_HASH
  5344. wc_ShaSizeSet(hash[i], numBlocks * bench_size);
  5345. #endif
  5346. }
  5347. bench_stats_start(&count, &start);
  5348. do {
  5349. for (times = 0; times < numBlocks || pending > 0; ) {
  5350. bench_async_poll(&pending);
  5351. /* while free pending slots in queue, submit ops */
  5352. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5353. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5354. 0, &times, numBlocks, &pending)) {
  5355. ret = wc_ShaUpdate(hash[i], bench_plain,
  5356. bench_size);
  5357. if (!bench_async_handle(&ret,
  5358. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5359. &times, &pending)) {
  5360. goto exit_sha;
  5361. }
  5362. }
  5363. } /* for i */
  5364. RECORD_MULTI_VALUE_STATS();
  5365. } /* for times */
  5366. count += times;
  5367. times = 0;
  5368. do {
  5369. bench_async_poll(&pending);
  5370. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5371. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5372. 0, &times, numBlocks, &pending)) {
  5373. ret = wc_ShaFinal(hash[i], digest[i]);
  5374. if (!bench_async_handle(&ret,
  5375. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5376. &times, &pending)) {
  5377. goto exit_sha;
  5378. }
  5379. }
  5380. } /* for i */
  5381. } while (pending > 0);
  5382. } while (bench_stats_check(start)
  5383. #ifdef MULTI_VALUE_STATISTICS
  5384. || runs < minimum_runs
  5385. #endif
  5386. );
  5387. }
  5388. else {
  5389. bench_stats_start(&count, &start);
  5390. do {
  5391. for (times = 0; times < numBlocks; times++) {
  5392. ret = wc_InitSha_ex(hash[0], HEAP_HINT,
  5393. useDeviceID ? devId : INVALID_DEVID);
  5394. if (ret == 0)
  5395. ret = wc_ShaUpdate(hash[0], bench_plain, bench_size);
  5396. if (ret == 0)
  5397. ret = wc_ShaFinal(hash[0], digest[0]);
  5398. if (ret != 0)
  5399. goto exit_sha;
  5400. RECORD_MULTI_VALUE_STATS();
  5401. } /* for times */
  5402. count += times;
  5403. } while (bench_stats_check(start)
  5404. #ifdef MULTI_VALUE_STATISTICS
  5405. || runs < minimum_runs
  5406. #endif
  5407. );
  5408. }
  5409. exit_sha:
  5410. bench_stats_sym_finish("SHA", useDeviceID, count, bench_size, start, ret);
  5411. #ifdef MULTI_VALUE_STATISTICS
  5412. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5413. #endif
  5414. exit:
  5415. if (WC_ARRAY_OK(hash)) {
  5416. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5417. wc_ShaFree(hash[i]);
  5418. }
  5419. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5420. }
  5421. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5422. }
  5423. #endif /* NO_SHA */
  5424. #ifdef WOLFSSL_SHA224
  5425. void bench_sha224(int useDeviceID)
  5426. {
  5427. WC_DECLARE_ARRAY(hash, wc_Sha224, BENCH_MAX_PENDING,
  5428. sizeof(wc_Sha224), HEAP_HINT);
  5429. double start;
  5430. int ret = 0, i, count = 0, times, pending = 0;
  5431. DECLARE_MULTI_VALUE_STATS_VARS()
  5432. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5433. WC_SHA224_DIGEST_SIZE, HEAP_HINT);
  5434. WC_CALLOC_ARRAY(hash, wc_Sha224, BENCH_MAX_PENDING,
  5435. sizeof(wc_Sha224), HEAP_HINT);
  5436. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5437. WC_SHA224_DIGEST_SIZE, HEAP_HINT);
  5438. if (digest_stream) {
  5439. /* init keys */
  5440. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5441. ret = wc_InitSha224_ex(hash[i], HEAP_HINT,
  5442. useDeviceID ? devId : INVALID_DEVID);
  5443. if (ret != 0) {
  5444. printf("InitSha224_ex failed, ret = %d\n", ret);
  5445. goto exit;
  5446. }
  5447. }
  5448. bench_stats_start(&count, &start);
  5449. do {
  5450. for (times = 0; times < numBlocks || pending > 0; ) {
  5451. bench_async_poll(&pending);
  5452. /* while free pending slots in queue, submit ops */
  5453. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5454. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5455. 0, &times, numBlocks, &pending)) {
  5456. ret = wc_Sha224Update(hash[i], bench_plain,
  5457. bench_size);
  5458. if (!bench_async_handle(&ret,
  5459. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5460. &times, &pending)) {
  5461. goto exit_sha224;
  5462. }
  5463. }
  5464. } /* for i */
  5465. RECORD_MULTI_VALUE_STATS();
  5466. } /* for times */
  5467. count += times;
  5468. times = 0;
  5469. do {
  5470. bench_async_poll(&pending);
  5471. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5472. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5473. 0, &times, numBlocks, &pending)) {
  5474. ret = wc_Sha224Final(hash[i], digest[i]);
  5475. if (!bench_async_handle(&ret,
  5476. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5477. &times, &pending)) {
  5478. goto exit_sha224;
  5479. }
  5480. }
  5481. } /* for i */
  5482. } while (pending > 0);
  5483. } while (bench_stats_check(start)
  5484. #ifdef MULTI_VALUE_STATISTICS
  5485. || runs < minimum_runs
  5486. #endif
  5487. );
  5488. }
  5489. else {
  5490. bench_stats_start(&count, &start);
  5491. do {
  5492. for (times = 0; times < numBlocks; times++) {
  5493. ret = wc_InitSha224_ex(hash[0], HEAP_HINT,
  5494. useDeviceID ? devId : INVALID_DEVID);
  5495. if (ret == 0)
  5496. ret = wc_Sha224Update(hash[0], bench_plain, bench_size);
  5497. if (ret == 0)
  5498. ret = wc_Sha224Final(hash[0], digest[0]);
  5499. if (ret != 0)
  5500. goto exit_sha224;
  5501. } /* for times */
  5502. count += times;
  5503. } while (bench_stats_check(start)
  5504. #ifdef MULTI_VALUE_STATISTICS
  5505. || runs < minimum_runs
  5506. #endif
  5507. );
  5508. }
  5509. exit_sha224:
  5510. bench_stats_sym_finish("SHA-224", useDeviceID, count,
  5511. bench_size, start, ret);
  5512. #ifdef MULTI_VALUE_STATISTICS
  5513. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5514. #endif
  5515. exit:
  5516. if (WC_ARRAY_OK(hash)) {
  5517. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5518. wc_Sha224Free(hash[i]);
  5519. }
  5520. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5521. }
  5522. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5523. }
  5524. #endif
  5525. #ifndef NO_SHA256
  5526. void bench_sha256(int useDeviceID)
  5527. {
  5528. WC_DECLARE_ARRAY(hash, wc_Sha256, BENCH_MAX_PENDING,
  5529. sizeof(wc_Sha256), HEAP_HINT);
  5530. double start;
  5531. int ret = 0, i, count = 0, times, pending = 0;
  5532. DECLARE_MULTI_VALUE_STATS_VARS()
  5533. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5534. WC_SHA256_DIGEST_SIZE, HEAP_HINT);
  5535. WC_CALLOC_ARRAY(hash, wc_Sha256, BENCH_MAX_PENDING,
  5536. sizeof(wc_Sha256), HEAP_HINT);
  5537. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5538. WC_SHA256_DIGEST_SIZE, HEAP_HINT);
  5539. if (digest_stream) {
  5540. /* init keys */
  5541. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5542. ret = wc_InitSha256_ex(hash[i], HEAP_HINT,
  5543. useDeviceID ? devId: INVALID_DEVID);
  5544. if (ret != 0) {
  5545. printf("InitSha256_ex failed, ret = %d\n", ret);
  5546. goto exit;
  5547. }
  5548. #ifdef WOLFSSL_PIC32MZ_HASH
  5549. wc_Sha256SizeSet(hash[i], numBlocks * bench_size);
  5550. #endif
  5551. }
  5552. bench_stats_start(&count, &start);
  5553. do {
  5554. for (times = 0; times < numBlocks || pending > 0; ) {
  5555. bench_async_poll(&pending);
  5556. /* while free pending slots in queue, submit ops */
  5557. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5558. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5559. 0, &times, numBlocks, &pending)) {
  5560. ret = wc_Sha256Update(hash[i], bench_plain,
  5561. bench_size);
  5562. if (!bench_async_handle(&ret,
  5563. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5564. &times, &pending)) {
  5565. goto exit_sha256;
  5566. }
  5567. }
  5568. } /* for i */
  5569. RECORD_MULTI_VALUE_STATS();
  5570. } /* for times */
  5571. count += times;
  5572. times = 0;
  5573. do {
  5574. bench_async_poll(&pending);
  5575. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5576. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5577. 0, &times, numBlocks, &pending)) {
  5578. ret = wc_Sha256Final(hash[i], digest[i]);
  5579. if (!bench_async_handle(&ret,
  5580. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5581. &times, &pending)) {
  5582. goto exit_sha256;
  5583. }
  5584. }
  5585. } /* for i */
  5586. } while (pending > 0);
  5587. } while (bench_stats_check(start)
  5588. #ifdef MULTI_VALUE_STATISTICS
  5589. || runs < minimum_runs
  5590. #endif
  5591. );
  5592. }
  5593. else {
  5594. bench_stats_start(&count, &start);
  5595. do {
  5596. for (times = 0; times < numBlocks; times++) {
  5597. ret = wc_InitSha256_ex(hash[0], HEAP_HINT,
  5598. useDeviceID ? devId: INVALID_DEVID);
  5599. if (ret == 0)
  5600. ret = wc_Sha256Update(hash[0], bench_plain, bench_size);
  5601. if (ret == 0)
  5602. ret = wc_Sha256Final(hash[0], digest[0]);
  5603. if (ret != 0)
  5604. goto exit_sha256;
  5605. RECORD_MULTI_VALUE_STATS();
  5606. } /* for times */
  5607. count += times;
  5608. } while (bench_stats_check(start)
  5609. #ifdef MULTI_VALUE_STATISTICS
  5610. || runs < minimum_runs
  5611. #endif
  5612. );
  5613. }
  5614. exit_sha256:
  5615. bench_stats_sym_finish("SHA-256", useDeviceID, count, bench_size,
  5616. start, ret);
  5617. #ifdef MULTI_VALUE_STATISTICS
  5618. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5619. #endif
  5620. exit:
  5621. if (WC_ARRAY_OK(hash)) {
  5622. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5623. wc_Sha256Free(hash[i]);
  5624. }
  5625. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5626. }
  5627. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5628. }
  5629. #endif
  5630. #ifdef WOLFSSL_SHA384
  5631. void bench_sha384(int useDeviceID)
  5632. {
  5633. WC_DECLARE_ARRAY(hash, wc_Sha384, BENCH_MAX_PENDING,
  5634. sizeof(wc_Sha384), HEAP_HINT);
  5635. double start;
  5636. int ret = 0, i, count = 0, times, pending = 0;
  5637. DECLARE_MULTI_VALUE_STATS_VARS()
  5638. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5639. WC_SHA384_DIGEST_SIZE, HEAP_HINT);
  5640. WC_CALLOC_ARRAY(hash, wc_Sha384, BENCH_MAX_PENDING,
  5641. sizeof(wc_Sha384), HEAP_HINT);
  5642. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5643. WC_SHA384_DIGEST_SIZE, HEAP_HINT);
  5644. if (digest_stream) {
  5645. /* init keys */
  5646. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5647. ret = wc_InitSha384_ex(hash[i], HEAP_HINT,
  5648. useDeviceID ? devId : INVALID_DEVID);
  5649. if (ret != 0) {
  5650. printf("InitSha384_ex failed, ret = %d\n", ret);
  5651. goto exit;
  5652. }
  5653. }
  5654. bench_stats_start(&count, &start);
  5655. do {
  5656. for (times = 0; times < numBlocks || pending > 0; ) {
  5657. bench_async_poll(&pending);
  5658. /* while free pending slots in queue, submit ops */
  5659. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5660. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5661. 0, &times, numBlocks, &pending)) {
  5662. ret = wc_Sha384Update(hash[i], bench_plain,
  5663. bench_size);
  5664. if (!bench_async_handle(&ret,
  5665. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5666. &times, &pending)) {
  5667. goto exit_sha384;
  5668. }
  5669. }
  5670. } /* for i */
  5671. RECORD_MULTI_VALUE_STATS();
  5672. } /* for times */
  5673. count += times;
  5674. times = 0;
  5675. do {
  5676. bench_async_poll(&pending);
  5677. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5678. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5679. 0, &times, numBlocks, &pending)) {
  5680. ret = wc_Sha384Final(hash[i], digest[i]);
  5681. if (!bench_async_handle(&ret,
  5682. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5683. &times, &pending)) {
  5684. goto exit_sha384;
  5685. }
  5686. }
  5687. } /* for i */
  5688. } while (pending > 0);
  5689. } while (bench_stats_check(start)
  5690. #ifdef MULTI_VALUE_STATISTICS
  5691. || runs < minimum_runs
  5692. #endif
  5693. );
  5694. }
  5695. else {
  5696. bench_stats_start(&count, &start);
  5697. do {
  5698. for (times = 0; times < numBlocks; times++) {
  5699. ret = wc_InitSha384_ex(hash[0], HEAP_HINT,
  5700. useDeviceID ? devId : INVALID_DEVID);
  5701. if (ret == 0)
  5702. ret = wc_Sha384Update(hash[0], bench_plain, bench_size);
  5703. if (ret == 0)
  5704. ret = wc_Sha384Final(hash[0], digest[0]);
  5705. if (ret != 0)
  5706. goto exit_sha384;
  5707. RECORD_MULTI_VALUE_STATS();
  5708. } /* for times */
  5709. count += times;
  5710. } while (bench_stats_check(start)
  5711. #ifdef MULTI_VALUE_STATISTICS
  5712. || runs < minimum_runs
  5713. #endif
  5714. );
  5715. }
  5716. exit_sha384:
  5717. bench_stats_sym_finish("SHA-384", useDeviceID, count, bench_size,
  5718. start, ret);
  5719. #ifdef MULTI_VALUE_STATISTICS
  5720. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5721. #endif
  5722. exit:
  5723. if (WC_ARRAY_OK(hash)) {
  5724. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5725. wc_Sha384Free(hash[i]);
  5726. }
  5727. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5728. }
  5729. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5730. }
  5731. #endif
  5732. #ifdef WOLFSSL_SHA512
  5733. void bench_sha512(int useDeviceID)
  5734. {
  5735. WC_DECLARE_ARRAY(hash, wc_Sha512, BENCH_MAX_PENDING,
  5736. sizeof(wc_Sha512), HEAP_HINT);
  5737. double start;
  5738. int ret = 0, i, count = 0, times, pending = 0;
  5739. DECLARE_MULTI_VALUE_STATS_VARS()
  5740. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5741. WC_SHA512_DIGEST_SIZE, HEAP_HINT);
  5742. WC_CALLOC_ARRAY(hash, wc_Sha512, BENCH_MAX_PENDING,
  5743. sizeof(wc_Sha512), HEAP_HINT);
  5744. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5745. WC_SHA512_DIGEST_SIZE, HEAP_HINT);
  5746. if (digest_stream) {
  5747. /* init keys */
  5748. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5749. ret = wc_InitSha512_ex(hash[i], HEAP_HINT,
  5750. useDeviceID ? devId : INVALID_DEVID);
  5751. if (ret != 0) {
  5752. printf("InitSha512_ex failed, ret = %d\n", ret);
  5753. goto exit;
  5754. }
  5755. }
  5756. bench_stats_start(&count, &start);
  5757. do {
  5758. for (times = 0; times < numBlocks || pending > 0; ) {
  5759. bench_async_poll(&pending);
  5760. /* while free pending slots in queue, submit ops */
  5761. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5762. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5763. 0, &times, numBlocks, &pending)) {
  5764. ret = wc_Sha512Update(hash[i], bench_plain,
  5765. bench_size);
  5766. if (!bench_async_handle(&ret,
  5767. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5768. &times, &pending)) {
  5769. goto exit_sha512;
  5770. }
  5771. }
  5772. } /* for i */
  5773. RECORD_MULTI_VALUE_STATS();
  5774. } /* for times */
  5775. count += times;
  5776. times = 0;
  5777. do {
  5778. bench_async_poll(&pending);
  5779. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5780. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5781. 0, &times, numBlocks, &pending)) {
  5782. ret = wc_Sha512Final(hash[i], digest[i]);
  5783. if (!bench_async_handle(&ret,
  5784. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5785. &times, &pending)) {
  5786. goto exit_sha512;
  5787. }
  5788. }
  5789. } /* for i */
  5790. } while (pending > 0);
  5791. } while (bench_stats_check(start)
  5792. #ifdef MULTI_VALUE_STATISTICS
  5793. || runs < minimum_runs
  5794. #endif
  5795. );
  5796. }
  5797. else {
  5798. bench_stats_start(&count, &start);
  5799. do {
  5800. for (times = 0; times < numBlocks; times++) {
  5801. ret = wc_InitSha512_ex(hash[0], HEAP_HINT,
  5802. useDeviceID ? devId : INVALID_DEVID);
  5803. if (ret == 0)
  5804. ret = wc_Sha512Update(hash[0], bench_plain, bench_size);
  5805. if (ret == 0)
  5806. ret = wc_Sha512Final(hash[0], digest[0]);
  5807. if (ret != 0)
  5808. goto exit_sha512;
  5809. RECORD_MULTI_VALUE_STATS();
  5810. } /* for times */
  5811. count += times;
  5812. } while (bench_stats_check(start)
  5813. #ifdef MULTI_VALUE_STATISTICS
  5814. || runs < minimum_runs
  5815. #endif
  5816. );
  5817. }
  5818. exit_sha512:
  5819. bench_stats_sym_finish("SHA-512", useDeviceID, count, bench_size,
  5820. start, ret);
  5821. #ifdef MULTI_VALUE_STATISTICS
  5822. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5823. #endif
  5824. exit:
  5825. if (WC_ARRAY_OK(hash)) {
  5826. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5827. wc_Sha512Free(hash[i]);
  5828. }
  5829. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5830. }
  5831. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5832. }
  5833. #if !defined(WOLFSSL_NOSHA512_224) && \
  5834. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  5835. void bench_sha512_224(int useDeviceID)
  5836. {
  5837. WC_DECLARE_ARRAY(hash, wc_Sha512_224, BENCH_MAX_PENDING,
  5838. sizeof(wc_Sha512_224), HEAP_HINT);
  5839. double start;
  5840. int ret = 0, i, count = 0, times, pending = 0;
  5841. DECLARE_MULTI_VALUE_STATS_VARS()
  5842. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5843. WC_SHA512_224_DIGEST_SIZE, HEAP_HINT);
  5844. WC_CALLOC_ARRAY(hash, wc_Sha512_224, BENCH_MAX_PENDING,
  5845. sizeof(wc_Sha512_224), HEAP_HINT);
  5846. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5847. WC_SHA512_224_DIGEST_SIZE, HEAP_HINT);
  5848. if (digest_stream) {
  5849. /* init keys */
  5850. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5851. ret = wc_InitSha512_224_ex(hash[i], HEAP_HINT,
  5852. useDeviceID ? devId : INVALID_DEVID);
  5853. if (ret != 0) {
  5854. printf("InitSha512_224_ex failed, ret = %d\n", ret);
  5855. goto exit;
  5856. }
  5857. }
  5858. bench_stats_start(&count, &start);
  5859. do {
  5860. for (times = 0; times < numBlocks || pending > 0; ) {
  5861. bench_async_poll(&pending);
  5862. /* while free pending slots in queue, submit ops */
  5863. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5864. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5865. 0, &times, numBlocks, &pending)) {
  5866. ret = wc_Sha512_224Update(hash[i], bench_plain,
  5867. bench_size);
  5868. if (!bench_async_handle(&ret,
  5869. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5870. &times, &pending)) {
  5871. goto exit_sha512_224;
  5872. }
  5873. }
  5874. } /* for i */
  5875. RECORD_MULTI_VALUE_STATS();
  5876. } /* for times */
  5877. count += times;
  5878. times = 0;
  5879. do {
  5880. bench_async_poll(&pending);
  5881. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5882. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5883. 0, &times, numBlocks, &pending)) {
  5884. ret = wc_Sha512_224Final(hash[i], digest[i]);
  5885. if (!bench_async_handle(&ret,
  5886. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5887. &times, &pending)) {
  5888. goto exit_sha512_224;
  5889. }
  5890. }
  5891. } /* for i */
  5892. } while (pending > 0);
  5893. } while (bench_stats_check(start)
  5894. #ifdef MULTI_VALUE_STATISTICS
  5895. || runs < minimum_runs
  5896. #endif
  5897. );
  5898. }
  5899. else {
  5900. bench_stats_start(&count, &start);
  5901. do {
  5902. for (times = 0; times < numBlocks; times++) {
  5903. ret = wc_InitSha512_224_ex(hash[0], HEAP_HINT,
  5904. useDeviceID ? devId : INVALID_DEVID);
  5905. if (ret == 0)
  5906. ret = wc_Sha512_224Update(hash[0], bench_plain, bench_size);
  5907. if (ret == 0)
  5908. ret = wc_Sha512_224Final(hash[0], digest[0]);
  5909. if (ret != 0)
  5910. goto exit_sha512_224;
  5911. RECORD_MULTI_VALUE_STATS();
  5912. } /* for times */
  5913. count += times;
  5914. } while (bench_stats_check(start)
  5915. #ifdef MULTI_VALUE_STATISTICS
  5916. || runs < minimum_runs
  5917. #endif
  5918. );
  5919. }
  5920. exit_sha512_224:
  5921. bench_stats_sym_finish("SHA-512/224", useDeviceID, count, bench_size,
  5922. start, ret);
  5923. #ifdef MULTI_VALUE_STATISTICS
  5924. bench_multi_value_stats(max, min, sum, squareSum, runs);
  5925. #endif
  5926. exit:
  5927. if (WC_ARRAY_OK(hash)) {
  5928. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5929. wc_Sha512_224Free(hash[i]);
  5930. }
  5931. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  5932. }
  5933. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  5934. }
  5935. #endif /* WOLFSSL_NOSHA512_224 && !FIPS ... */
  5936. #if !defined(WOLFSSL_NOSHA512_256) && \
  5937. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  5938. void bench_sha512_256(int useDeviceID)
  5939. {
  5940. WC_DECLARE_ARRAY(hash, wc_Sha512_256, BENCH_MAX_PENDING,
  5941. sizeof(wc_Sha512_256), HEAP_HINT);
  5942. double start;
  5943. int ret = 0, i, count = 0, times, pending = 0;
  5944. DECLARE_MULTI_VALUE_STATS_VARS()
  5945. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5946. WC_SHA512_256_DIGEST_SIZE, HEAP_HINT);
  5947. WC_CALLOC_ARRAY(hash, wc_Sha512_256, BENCH_MAX_PENDING,
  5948. sizeof(wc_Sha512_256), HEAP_HINT);
  5949. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  5950. WC_SHA512_256_DIGEST_SIZE, HEAP_HINT);
  5951. if (digest_stream) {
  5952. /* init keys */
  5953. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5954. ret = wc_InitSha512_256_ex(hash[i], HEAP_HINT,
  5955. useDeviceID ? devId : INVALID_DEVID);
  5956. if (ret != 0) {
  5957. printf("InitSha512_256_ex failed, ret = %d\n", ret);
  5958. goto exit;
  5959. }
  5960. }
  5961. bench_stats_start(&count, &start);
  5962. do {
  5963. for (times = 0; times < numBlocks || pending > 0; ) {
  5964. bench_async_poll(&pending);
  5965. /* while free pending slots in queue, submit ops */
  5966. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5967. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5968. 0, &times, numBlocks, &pending)) {
  5969. ret = wc_Sha512_256Update(hash[i], bench_plain,
  5970. bench_size);
  5971. if (!bench_async_handle(&ret,
  5972. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5973. &times, &pending)) {
  5974. goto exit_sha512_256;
  5975. }
  5976. }
  5977. } /* for i */
  5978. RECORD_MULTI_VALUE_STATS();
  5979. } /* for times */
  5980. count += times;
  5981. times = 0;
  5982. do {
  5983. bench_async_poll(&pending);
  5984. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  5985. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  5986. 0, &times, numBlocks, &pending)) {
  5987. ret = wc_Sha512_256Final(hash[i], digest[i]);
  5988. if (!bench_async_handle(&ret,
  5989. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  5990. &times, &pending)) {
  5991. goto exit_sha512_256;
  5992. }
  5993. }
  5994. } /* for i */
  5995. } while (pending > 0);
  5996. } while (bench_stats_check(start)
  5997. #ifdef MULTI_VALUE_STATISTICS
  5998. || runs < minimum_runs
  5999. #endif
  6000. );
  6001. }
  6002. else {
  6003. bench_stats_start(&count, &start);
  6004. do {
  6005. for (times = 0; times < numBlocks; times++) {
  6006. ret = wc_InitSha512_256_ex(hash[0], HEAP_HINT,
  6007. useDeviceID ? devId : INVALID_DEVID);
  6008. if (ret == 0)
  6009. ret = wc_Sha512_256Update(hash[0], bench_plain, bench_size);
  6010. if (ret == 0)
  6011. ret = wc_Sha512_256Final(hash[0], digest[0]);
  6012. if (ret != 0)
  6013. goto exit_sha512_256;
  6014. RECORD_MULTI_VALUE_STATS();
  6015. } /* for times */
  6016. count += times;
  6017. } while (bench_stats_check(start)
  6018. #ifdef MULTI_VALUE_STATISTICS
  6019. || runs < minimum_runs
  6020. #endif
  6021. );
  6022. }
  6023. exit_sha512_256:
  6024. bench_stats_sym_finish("SHA-512/256", useDeviceID, count, bench_size,
  6025. start, ret);
  6026. #ifdef MULTI_VALUE_STATISTICS
  6027. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6028. #endif
  6029. exit:
  6030. if (WC_ARRAY_OK(hash)) {
  6031. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6032. wc_Sha512_256Free(hash[i]);
  6033. }
  6034. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6035. }
  6036. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6037. }
  6038. #endif /* WOLFSSL_NOSHA512_256 && !FIPS ... */
  6039. #endif /* WOLFSSL_SHA512 */
  6040. #ifdef WOLFSSL_SHA3
  6041. #ifndef WOLFSSL_NOSHA3_224
  6042. void bench_sha3_224(int useDeviceID)
  6043. {
  6044. WC_DECLARE_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6045. sizeof(wc_Sha3), HEAP_HINT);
  6046. double start;
  6047. int ret = 0, i, count = 0, times, pending = 0;
  6048. DECLARE_MULTI_VALUE_STATS_VARS()
  6049. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6050. WC_SHA3_224_DIGEST_SIZE, HEAP_HINT);
  6051. WC_CALLOC_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6052. sizeof(wc_Sha3), HEAP_HINT);
  6053. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6054. WC_SHA3_224_DIGEST_SIZE, HEAP_HINT);
  6055. if (digest_stream) {
  6056. /* init keys */
  6057. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6058. ret = wc_InitSha3_224(hash[i], HEAP_HINT,
  6059. useDeviceID ? devId : INVALID_DEVID);
  6060. if (ret != 0) {
  6061. printf("InitSha3_224 failed, ret = %d\n", ret);
  6062. goto exit;
  6063. }
  6064. }
  6065. bench_stats_start(&count, &start);
  6066. do {
  6067. for (times = 0; times < numBlocks || pending > 0; ) {
  6068. bench_async_poll(&pending);
  6069. /* while free pending slots in queue, submit ops */
  6070. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6071. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6072. 0, &times, numBlocks, &pending)) {
  6073. ret = wc_Sha3_224_Update(hash[i], bench_plain,
  6074. bench_size);
  6075. if (!bench_async_handle(&ret,
  6076. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6077. &times, &pending)) {
  6078. goto exit_sha3_224;
  6079. }
  6080. }
  6081. } /* for i */
  6082. RECORD_MULTI_VALUE_STATS();
  6083. } /* for times */
  6084. count += times;
  6085. times = 0;
  6086. do {
  6087. bench_async_poll(&pending);
  6088. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6089. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6090. 0, &times, numBlocks, &pending)) {
  6091. ret = wc_Sha3_224_Final(hash[i], digest[i]);
  6092. if (!bench_async_handle(&ret,
  6093. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6094. &times, &pending)) {
  6095. goto exit_sha3_224;
  6096. }
  6097. }
  6098. } /* for i */
  6099. } while (pending > 0);
  6100. } while (bench_stats_check(start)
  6101. #ifdef MULTI_VALUE_STATISTICS
  6102. || runs < minimum_runs
  6103. #endif
  6104. );
  6105. }
  6106. else {
  6107. bench_stats_start(&count, &start);
  6108. do {
  6109. for (times = 0; times < numBlocks; times++) {
  6110. ret = wc_InitSha3_224(hash[0], HEAP_HINT,
  6111. useDeviceID ? devId : INVALID_DEVID);
  6112. if (ret == 0)
  6113. ret = wc_Sha3_224_Update(hash[0], bench_plain, bench_size);
  6114. if (ret == 0)
  6115. ret = wc_Sha3_224_Final(hash[0], digest[0]);
  6116. if (ret != 0)
  6117. goto exit_sha3_224;
  6118. RECORD_MULTI_VALUE_STATS();
  6119. } /* for times */
  6120. count += times;
  6121. } while (bench_stats_check(start)
  6122. #ifdef MULTI_VALUE_STATISTICS
  6123. || runs < minimum_runs
  6124. #endif
  6125. );
  6126. }
  6127. exit_sha3_224:
  6128. bench_stats_sym_finish("SHA3-224", useDeviceID, count, bench_size,
  6129. start, ret);
  6130. #ifdef MULTI_VALUE_STATISTICS
  6131. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6132. #endif
  6133. exit:
  6134. if (WC_ARRAY_OK(hash)) {
  6135. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6136. wc_Sha3_224_Free(hash[i]);
  6137. }
  6138. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6139. }
  6140. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6141. }
  6142. #endif /* WOLFSSL_NOSHA3_224 */
  6143. #ifndef WOLFSSL_NOSHA3_256
  6144. void bench_sha3_256(int useDeviceID)
  6145. {
  6146. WC_DECLARE_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6147. sizeof(wc_Sha3), HEAP_HINT);
  6148. double start;
  6149. DECLARE_MULTI_VALUE_STATS_VARS()
  6150. int ret = 0, i, count = 0, times, pending = 0;
  6151. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6152. WC_SHA3_256_DIGEST_SIZE, HEAP_HINT);
  6153. WC_CALLOC_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6154. sizeof(wc_Sha3), HEAP_HINT);
  6155. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6156. WC_SHA3_256_DIGEST_SIZE, HEAP_HINT);
  6157. if (digest_stream) {
  6158. /* init keys */
  6159. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6160. ret = wc_InitSha3_256(hash[i], HEAP_HINT,
  6161. useDeviceID ? devId : INVALID_DEVID);
  6162. if (ret != 0) {
  6163. printf("InitSha3_256 failed, ret = %d\n", ret);
  6164. goto exit;
  6165. }
  6166. }
  6167. bench_stats_start(&count, &start);
  6168. do {
  6169. for (times = 0; times < numBlocks || pending > 0; ) {
  6170. bench_async_poll(&pending);
  6171. /* while free pending slots in queue, submit ops */
  6172. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6173. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6174. 0, &times, numBlocks, &pending)) {
  6175. ret = wc_Sha3_256_Update(hash[i], bench_plain,
  6176. bench_size);
  6177. if (!bench_async_handle(&ret,
  6178. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6179. &times, &pending)) {
  6180. goto exit_sha3_256;
  6181. }
  6182. }
  6183. } /* for i */
  6184. RECORD_MULTI_VALUE_STATS();
  6185. } /* for times */
  6186. count += times;
  6187. times = 0;
  6188. do {
  6189. bench_async_poll(&pending);
  6190. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6191. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6192. 0, &times, numBlocks, &pending)) {
  6193. ret = wc_Sha3_256_Final(hash[i], digest[i]);
  6194. if (!bench_async_handle(&ret,
  6195. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6196. &times, &pending)) {
  6197. goto exit_sha3_256;
  6198. }
  6199. }
  6200. } /* for i */
  6201. } while (pending > 0);
  6202. } while (bench_stats_check(start)
  6203. #ifdef MULTI_VALUE_STATISTICS
  6204. || runs < minimum_runs
  6205. #endif
  6206. );
  6207. }
  6208. else {
  6209. bench_stats_start(&count, &start);
  6210. do {
  6211. for (times = 0; times < numBlocks; times++) {
  6212. ret = wc_InitSha3_256(hash[0], HEAP_HINT,
  6213. useDeviceID ? devId : INVALID_DEVID);
  6214. if (ret == 0)
  6215. ret = wc_Sha3_256_Update(hash[0], bench_plain, bench_size);
  6216. if (ret == 0)
  6217. ret = wc_Sha3_256_Final(hash[0], digest[0]);
  6218. if (ret != 0)
  6219. goto exit_sha3_256;
  6220. RECORD_MULTI_VALUE_STATS();
  6221. } /* for times */
  6222. count += times;
  6223. } while (bench_stats_check(start)
  6224. #ifdef MULTI_VALUE_STATISTICS
  6225. || runs < minimum_runs
  6226. #endif
  6227. );
  6228. }
  6229. exit_sha3_256:
  6230. bench_stats_sym_finish("SHA3-256", useDeviceID, count, bench_size,
  6231. start, ret);
  6232. #ifdef MULTI_VALUE_STATISTICS
  6233. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6234. #endif
  6235. exit:
  6236. if (WC_ARRAY_OK(hash)) {
  6237. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6238. wc_Sha3_256_Free(hash[i]);
  6239. }
  6240. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6241. }
  6242. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6243. }
  6244. #endif /* WOLFSSL_NOSHA3_256 */
  6245. #ifndef WOLFSSL_NOSHA3_384
  6246. void bench_sha3_384(int useDeviceID)
  6247. {
  6248. WC_DECLARE_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6249. sizeof(wc_Sha3), HEAP_HINT);
  6250. double start;
  6251. int ret = 0, i, count = 0, times, pending = 0;
  6252. DECLARE_MULTI_VALUE_STATS_VARS()
  6253. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6254. WC_SHA3_384_DIGEST_SIZE, HEAP_HINT);
  6255. WC_CALLOC_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6256. sizeof(wc_Sha3), HEAP_HINT);
  6257. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6258. WC_SHA3_384_DIGEST_SIZE, HEAP_HINT);
  6259. if (digest_stream) {
  6260. /* init keys */
  6261. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6262. ret = wc_InitSha3_384(hash[i], HEAP_HINT,
  6263. useDeviceID ? devId : INVALID_DEVID);
  6264. if (ret != 0) {
  6265. printf("InitSha3_384 failed, ret = %d\n", ret);
  6266. goto exit;
  6267. }
  6268. }
  6269. bench_stats_start(&count, &start);
  6270. do {
  6271. for (times = 0; times < numBlocks || pending > 0; ) {
  6272. bench_async_poll(&pending);
  6273. /* while free pending slots in queue, submit ops */
  6274. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6275. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6276. 0, &times, numBlocks, &pending)) {
  6277. ret = wc_Sha3_384_Update(hash[i], bench_plain,
  6278. bench_size);
  6279. if (!bench_async_handle(&ret,
  6280. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6281. &times, &pending)) {
  6282. goto exit_sha3_384;
  6283. }
  6284. }
  6285. } /* for i */
  6286. RECORD_MULTI_VALUE_STATS();
  6287. } /* for times */
  6288. count += times;
  6289. times = 0;
  6290. do {
  6291. bench_async_poll(&pending);
  6292. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6293. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6294. 0, &times, numBlocks, &pending)) {
  6295. ret = wc_Sha3_384_Final(hash[i], digest[i]);
  6296. if (!bench_async_handle(&ret,
  6297. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6298. &times, &pending)) {
  6299. goto exit_sha3_384;
  6300. }
  6301. }
  6302. } /* for i */
  6303. } while (pending > 0);
  6304. } while (bench_stats_check(start)
  6305. #ifdef MULTI_VALUE_STATISTICS
  6306. || runs < minimum_runs
  6307. #endif
  6308. );
  6309. }
  6310. else {
  6311. bench_stats_start(&count, &start);
  6312. do {
  6313. for (times = 0; times < numBlocks; times++) {
  6314. ret = wc_InitSha3_384(hash[0], HEAP_HINT,
  6315. useDeviceID ? devId : INVALID_DEVID);
  6316. if (ret == 0)
  6317. ret = wc_Sha3_384_Update(hash[0], bench_plain, bench_size);
  6318. if (ret == 0)
  6319. ret = wc_Sha3_384_Final(hash[0], digest[0]);
  6320. if (ret != 0)
  6321. goto exit_sha3_384;
  6322. RECORD_MULTI_VALUE_STATS();
  6323. } /* for times */
  6324. count += times;
  6325. } while (bench_stats_check(start)
  6326. #ifdef MULTI_VALUE_STATISTICS
  6327. || runs < minimum_runs
  6328. #endif
  6329. );
  6330. }
  6331. exit_sha3_384:
  6332. bench_stats_sym_finish("SHA3-384", useDeviceID, count, bench_size,
  6333. start, ret);
  6334. #ifdef MULTI_VALUE_STATISTICS
  6335. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6336. #endif
  6337. exit:
  6338. if (WC_ARRAY_OK(hash)) {
  6339. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6340. wc_Sha3_384_Free(hash[i]);
  6341. }
  6342. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6343. }
  6344. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6345. }
  6346. #endif /* WOLFSSL_NOSHA3_384 */
  6347. #ifndef WOLFSSL_NOSHA3_512
  6348. void bench_sha3_512(int useDeviceID)
  6349. {
  6350. WC_DECLARE_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6351. sizeof(wc_Sha3), HEAP_HINT);
  6352. double start;
  6353. int ret = 0, i, count = 0, times, pending = 0;
  6354. DECLARE_MULTI_VALUE_STATS_VARS()
  6355. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6356. WC_SHA3_512_DIGEST_SIZE, HEAP_HINT);
  6357. WC_CALLOC_ARRAY(hash, wc_Sha3, BENCH_MAX_PENDING,
  6358. sizeof(wc_Sha3), HEAP_HINT);
  6359. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6360. WC_SHA3_512_DIGEST_SIZE, HEAP_HINT);
  6361. if (digest_stream) {
  6362. /* init keys */
  6363. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6364. ret = wc_InitSha3_512(hash[i], HEAP_HINT,
  6365. useDeviceID ? devId : INVALID_DEVID);
  6366. if (ret != 0) {
  6367. printf("InitSha3_512 failed, ret = %d\n", ret);
  6368. goto exit;
  6369. }
  6370. }
  6371. bench_stats_start(&count, &start);
  6372. do {
  6373. for (times = 0; times < numBlocks || pending > 0; ) {
  6374. bench_async_poll(&pending);
  6375. /* while free pending slots in queue, submit ops */
  6376. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6377. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6378. 0, &times, numBlocks, &pending)) {
  6379. ret = wc_Sha3_512_Update(hash[i], bench_plain,
  6380. bench_size);
  6381. if (!bench_async_handle(&ret,
  6382. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6383. &times, &pending)) {
  6384. goto exit_sha3_512;
  6385. }
  6386. }
  6387. } /* for i */
  6388. RECORD_MULTI_VALUE_STATS();
  6389. } /* for times */
  6390. count += times;
  6391. times = 0;
  6392. do {
  6393. bench_async_poll(&pending);
  6394. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6395. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6396. 0, &times, numBlocks, &pending)) {
  6397. ret = wc_Sha3_512_Final(hash[i], digest[i]);
  6398. if (!bench_async_handle(&ret,
  6399. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6400. &times, &pending)) {
  6401. goto exit_sha3_512;
  6402. }
  6403. }
  6404. } /* for i */
  6405. } while (pending > 0);
  6406. } while (bench_stats_check(start)
  6407. #ifdef MULTI_VALUE_STATISTICS
  6408. || runs < minimum_runs
  6409. #endif
  6410. );
  6411. }
  6412. else {
  6413. bench_stats_start(&count, &start);
  6414. do {
  6415. for (times = 0; times < numBlocks; times++) {
  6416. ret = wc_InitSha3_512(hash[0], HEAP_HINT,
  6417. useDeviceID ? devId : INVALID_DEVID);
  6418. if (ret == 0)
  6419. ret = wc_Sha3_512_Update(hash[0], bench_plain, bench_size);
  6420. if (ret == 0)
  6421. ret = wc_Sha3_512_Final(hash[0], digest[0]);
  6422. if (ret != 0)
  6423. goto exit_sha3_512;
  6424. RECORD_MULTI_VALUE_STATS();
  6425. } /* for times */
  6426. count += times;
  6427. } while (bench_stats_check(start)
  6428. #ifdef MULTI_VALUE_STATISTICS
  6429. || runs < minimum_runs
  6430. #endif
  6431. );
  6432. }
  6433. exit_sha3_512:
  6434. bench_stats_sym_finish("SHA3-512", useDeviceID, count, bench_size,
  6435. start, ret);
  6436. #ifdef MULTI_VALUE_STATISTICS
  6437. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6438. #endif
  6439. exit:
  6440. if (WC_ARRAY_OK(hash)) {
  6441. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6442. wc_Sha3_512_Free(hash[i]);
  6443. }
  6444. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6445. }
  6446. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6447. }
  6448. #endif /* WOLFSSL_NOSHA3_512 */
  6449. #ifdef WOLFSSL_SHAKE128
  6450. void bench_shake128(int useDeviceID)
  6451. {
  6452. WC_DECLARE_ARRAY(hash, wc_Shake, BENCH_MAX_PENDING,
  6453. sizeof(wc_Shake), HEAP_HINT);
  6454. double start;
  6455. int ret = 0, i, count = 0, times, pending = 0;
  6456. DECLARE_MULTI_VALUE_STATS_VARS()
  6457. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6458. WC_SHA3_128_BLOCK_SIZE, HEAP_HINT);
  6459. WC_CALLOC_ARRAY(hash, wc_Shake, BENCH_MAX_PENDING,
  6460. sizeof(wc_Shake), HEAP_HINT);
  6461. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6462. WC_SHA3_128_BLOCK_SIZE, HEAP_HINT);
  6463. if (digest_stream) {
  6464. /* init keys */
  6465. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6466. ret = wc_InitShake128(hash[i], HEAP_HINT,
  6467. useDeviceID ? devId : INVALID_DEVID);
  6468. if (ret != 0) {
  6469. printf("InitShake128 failed, ret = %d\n", ret);
  6470. goto exit;
  6471. }
  6472. }
  6473. bench_stats_start(&count, &start);
  6474. do {
  6475. for (times = 0; times < numBlocks || pending > 0; ) {
  6476. bench_async_poll(&pending);
  6477. /* while free pending slots in queue, submit ops */
  6478. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6479. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6480. 0, &times, numBlocks, &pending)) {
  6481. ret = wc_Shake128_Update(hash[i], bench_plain,
  6482. bench_size);
  6483. if (!bench_async_handle(&ret,
  6484. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6485. &times, &pending)) {
  6486. goto exit_shake128;
  6487. }
  6488. }
  6489. } /* for i */
  6490. RECORD_MULTI_VALUE_STATS();
  6491. } /* for times */
  6492. count += times;
  6493. times = 0;
  6494. do {
  6495. bench_async_poll(&pending);
  6496. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6497. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6498. 0, &times, numBlocks, &pending)) {
  6499. ret = wc_Shake128_Final(hash[i], digest[i],
  6500. WC_SHA3_128_BLOCK_SIZE);
  6501. if (!bench_async_handle(&ret,
  6502. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6503. &times, &pending)) {
  6504. goto exit_shake128;
  6505. }
  6506. }
  6507. } /* for i */
  6508. } while (pending > 0);
  6509. } while (bench_stats_check(start)
  6510. #ifdef MULTI_VALUE_STATISTICS
  6511. || runs < minimum_runs
  6512. #endif
  6513. );
  6514. }
  6515. else {
  6516. bench_stats_start(&count, &start);
  6517. do {
  6518. for (times = 0; times < numBlocks; times++) {
  6519. ret = wc_InitShake128(hash[0], HEAP_HINT,
  6520. useDeviceID ? devId : INVALID_DEVID);
  6521. if (ret == 0)
  6522. ret = wc_Shake128_Update(hash[0], bench_plain, bench_size);
  6523. if (ret == 0)
  6524. ret = wc_Shake128_Final(hash[0], digest[0],
  6525. WC_SHA3_128_BLOCK_SIZE);
  6526. if (ret != 0)
  6527. goto exit_shake128;
  6528. RECORD_MULTI_VALUE_STATS();
  6529. } /* for times */
  6530. count += times;
  6531. } while (bench_stats_check(start)
  6532. #ifdef MULTI_VALUE_STATISTICS
  6533. || runs < minimum_runs
  6534. #endif
  6535. );
  6536. }
  6537. exit_shake128:
  6538. bench_stats_sym_finish("SHAKE128", useDeviceID, count, bench_size,
  6539. start, ret);
  6540. #ifdef MULTI_VALUE_STATISTICS
  6541. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6542. #endif
  6543. exit:
  6544. if (WC_ARRAY_OK(hash)) {
  6545. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6546. wc_Shake128_Free(hash[i]);
  6547. }
  6548. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6549. }
  6550. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6551. }
  6552. #endif /* WOLFSSL_SHAKE128 */
  6553. #ifdef WOLFSSL_SHAKE256
  6554. void bench_shake256(int useDeviceID)
  6555. {
  6556. WC_DECLARE_ARRAY(hash, wc_Shake, BENCH_MAX_PENDING,
  6557. sizeof(wc_Shake), HEAP_HINT);
  6558. double start;
  6559. int ret = 0, i, count = 0, times, pending = 0;
  6560. DECLARE_MULTI_VALUE_STATS_VARS()
  6561. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6562. WC_SHA3_256_BLOCK_SIZE, HEAP_HINT);
  6563. WC_CALLOC_ARRAY(hash, wc_Shake, BENCH_MAX_PENDING,
  6564. sizeof(wc_Shake), HEAP_HINT);
  6565. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  6566. WC_SHA3_256_BLOCK_SIZE, HEAP_HINT);
  6567. if (digest_stream) {
  6568. /* init keys */
  6569. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6570. ret = wc_InitShake256(hash[i], HEAP_HINT,
  6571. useDeviceID ? devId : INVALID_DEVID);
  6572. if (ret != 0) {
  6573. printf("InitShake256 failed, ret = %d\n", ret);
  6574. goto exit;
  6575. }
  6576. }
  6577. bench_stats_start(&count, &start);
  6578. do {
  6579. for (times = 0; times < numBlocks || pending > 0; ) {
  6580. bench_async_poll(&pending);
  6581. /* while free pending slots in queue, submit ops */
  6582. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6583. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6584. 0, &times, numBlocks, &pending)) {
  6585. ret = wc_Shake256_Update(hash[i], bench_plain,
  6586. bench_size);
  6587. if (!bench_async_handle(&ret,
  6588. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6589. &times, &pending)) {
  6590. goto exit_shake256;
  6591. }
  6592. }
  6593. } /* for i */
  6594. RECORD_MULTI_VALUE_STATS();
  6595. } /* for times */
  6596. count += times;
  6597. times = 0;
  6598. do {
  6599. bench_async_poll(&pending);
  6600. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6601. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6602. 0, &times, numBlocks, &pending)) {
  6603. ret = wc_Shake256_Final(hash[i], digest[i],
  6604. WC_SHA3_256_BLOCK_SIZE);
  6605. if (!bench_async_handle(&ret,
  6606. BENCH_ASYNC_GET_DEV(hash[i]), 0,
  6607. &times, &pending)) {
  6608. goto exit_shake256;
  6609. }
  6610. }
  6611. } /* for i */
  6612. } while (pending > 0);
  6613. } while (bench_stats_check(start)
  6614. #ifdef MULTI_VALUE_STATISTICS
  6615. || runs < minimum_runs
  6616. #endif
  6617. );
  6618. }
  6619. else {
  6620. bench_stats_start(&count, &start);
  6621. do {
  6622. for (times = 0; times < numBlocks; times++) {
  6623. ret = wc_InitShake256(hash[0], HEAP_HINT,
  6624. useDeviceID ? devId : INVALID_DEVID);
  6625. if (ret == 0)
  6626. ret = wc_Shake256_Update(hash[0], bench_plain, bench_size);
  6627. if (ret == 0)
  6628. ret = wc_Shake256_Final(hash[0], digest[0],
  6629. WC_SHA3_256_BLOCK_SIZE);
  6630. if (ret != 0)
  6631. goto exit_shake256;
  6632. RECORD_MULTI_VALUE_STATS();
  6633. } /* for times */
  6634. count += times;
  6635. } while (bench_stats_check(start)
  6636. #ifdef MULTI_VALUE_STATISTICS
  6637. || runs < minimum_runs
  6638. #endif
  6639. );
  6640. }
  6641. exit_shake256:
  6642. bench_stats_sym_finish("SHAKE256", useDeviceID, count, bench_size,
  6643. start, ret);
  6644. #ifdef MULTI_VALUE_STATISTICS
  6645. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6646. #endif
  6647. exit:
  6648. if (WC_ARRAY_OK(hash)) {
  6649. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6650. wc_Shake256_Free(hash[i]);
  6651. }
  6652. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6653. }
  6654. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6655. }
  6656. #endif /* WOLFSSL_SHAKE256 */
  6657. #endif
  6658. #ifdef WOLFSSL_SM3
  6659. void bench_sm3(int useDeviceID)
  6660. {
  6661. WC_DECLARE_ARRAY(hash, wc_Sm3, BENCH_MAX_PENDING,
  6662. sizeof(wc_Sm3), HEAP_HINT);
  6663. double start;
  6664. int ret = 0, i, count = 0, times, pending = 0;
  6665. DECLARE_MULTI_VALUE_STATS_VARS()
  6666. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SM3_DIGEST_SIZE,
  6667. HEAP_HINT);
  6668. WC_CALLOC_ARRAY(hash, wc_Sm3, BENCH_MAX_PENDING,
  6669. sizeof(wc_Sm3), HEAP_HINT);
  6670. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SM3_DIGEST_SIZE,
  6671. HEAP_HINT);
  6672. if (digest_stream) {
  6673. /* init keys */
  6674. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6675. ret = wc_InitSm3(hash[i], HEAP_HINT,
  6676. useDeviceID ? devId: INVALID_DEVID);
  6677. if (ret != 0) {
  6678. printf("InitSm3 failed, ret = %d\n", ret);
  6679. goto exit;
  6680. }
  6681. }
  6682. bench_stats_start(&count, &start);
  6683. do {
  6684. for (times = 0; times < numBlocks || pending > 0; ) {
  6685. bench_async_poll(&pending);
  6686. /* while free pending slots in queue, submit ops */
  6687. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6688. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6689. 0, &times, numBlocks, &pending)) {
  6690. ret = wc_Sm3Update(hash[i], bench_plain,
  6691. bench_size);
  6692. if (!bench_async_handle(&ret,
  6693. BENCH_ASYNC_GET_DEV(hash[i]), 0, &times, &pending)) {
  6694. goto exit_sm3;
  6695. }
  6696. }
  6697. } /* for i */
  6698. RECORD_MULTI_VALUE_STATS();
  6699. } /* for times */
  6700. count += times;
  6701. times = 0;
  6702. do {
  6703. bench_async_poll(&pending);
  6704. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6705. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(hash[i]),
  6706. 0, &times, numBlocks, &pending)) {
  6707. ret = wc_Sm3Final(hash[i], digest[i]);
  6708. if (!bench_async_handle(&ret,
  6709. BENCH_ASYNC_GET_DEV(hash[i]), 0, &times, &pending)) {
  6710. goto exit_sm3;
  6711. }
  6712. }
  6713. } /* for i */
  6714. } while (pending > 0);
  6715. } while (bench_stats_check(start)
  6716. #ifdef MULTI_VALUE_STATISTICS
  6717. || runs < minimum_runs
  6718. #endif
  6719. );
  6720. }
  6721. else {
  6722. bench_stats_start(&count, &start);
  6723. do {
  6724. for (times = 0; times < numBlocks; times++) {
  6725. ret = wc_InitSm3(hash, HEAP_HINT,
  6726. useDeviceID ? devId: INVALID_DEVID);
  6727. if (ret == 0)
  6728. ret = wc_Sm3Update(hash, bench_plain, bench_size);
  6729. if (ret == 0)
  6730. ret = wc_Sm3Final(hash, digest[0]);
  6731. if (ret != 0)
  6732. goto exit_sm3;
  6733. RECORD_MULTI_VALUE_STATS();
  6734. } /* for times */
  6735. count += times;
  6736. } while (bench_stats_check(start)
  6737. #ifdef MULTI_VALUE_STATISTICS
  6738. || runs < minimum_runs
  6739. #endif
  6740. );
  6741. }
  6742. exit_sm3:
  6743. bench_stats_sym_finish("SM3", useDeviceID, count, bench_size, start, ret);
  6744. #ifdef MULTI_VALUE_STATISTICS
  6745. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6746. #endif
  6747. exit:
  6748. if (WC_ARRAY_OK(hash)) {
  6749. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  6750. wc_Sm3Free(hash[i]);
  6751. }
  6752. WC_FREE_ARRAY(hash, BENCH_MAX_PENDING, HEAP_HINT);
  6753. }
  6754. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  6755. }
  6756. #endif
  6757. #ifdef WOLFSSL_RIPEMD
  6758. void bench_ripemd(void)
  6759. {
  6760. RipeMd hash;
  6761. byte digest[RIPEMD_DIGEST_SIZE];
  6762. double start;
  6763. int i, count, ret = 0;
  6764. DECLARE_MULTI_VALUE_STATS_VARS()
  6765. if (digest_stream) {
  6766. ret = wc_InitRipeMd(&hash);
  6767. if (ret != 0) {
  6768. printf("wc_InitRipeMd failed, retval %d\n", ret);
  6769. return;
  6770. }
  6771. bench_stats_start(&count, &start);
  6772. do {
  6773. for (i = 0; i < numBlocks; i++) {
  6774. ret = wc_RipeMdUpdate(&hash, bench_plain, bench_size);
  6775. if (ret != 0) {
  6776. printf("wc_RipeMdUpdate failed, retval %d\n", ret);
  6777. return;
  6778. }
  6779. RECORD_MULTI_VALUE_STATS();
  6780. }
  6781. ret = wc_RipeMdFinal(&hash, digest);
  6782. if (ret != 0) {
  6783. printf("wc_RipeMdFinal failed, retval %d\n", ret);
  6784. return;
  6785. }
  6786. count += i;
  6787. } while (bench_stats_check(start)
  6788. #ifdef MULTI_VALUE_STATISTICS
  6789. || runs < minimum_runs
  6790. #endif
  6791. );
  6792. }
  6793. else {
  6794. bench_stats_start(&count, &start);
  6795. do {
  6796. for (i = 0; i < numBlocks; i++) {
  6797. ret = wc_InitRipeMd(&hash);
  6798. if (ret != 0) {
  6799. printf("wc_InitRipeMd failed, retval %d\n", ret);
  6800. return;
  6801. }
  6802. ret = wc_RipeMdUpdate(&hash, bench_plain, bench_size);
  6803. if (ret != 0) {
  6804. printf("wc_RipeMdUpdate failed, retval %d\n", ret);
  6805. return;
  6806. }
  6807. ret = wc_RipeMdFinal(&hash, digest);
  6808. if (ret != 0) {
  6809. printf("wc_RipeMdFinal failed, retval %d\n", ret);
  6810. return;
  6811. }
  6812. RECORD_MULTI_VALUE_STATS();
  6813. }
  6814. count += i;
  6815. } while (bench_stats_check(start)
  6816. #ifdef MULTI_VALUE_STATISTICS
  6817. || runs < minimum_runs
  6818. #endif
  6819. );
  6820. }
  6821. bench_stats_sym_finish("RIPEMD", 0, count, bench_size, start, ret);
  6822. #ifdef MULTI_VALUE_STATISTICS
  6823. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6824. #endif
  6825. return;
  6826. }
  6827. #endif
  6828. #ifdef HAVE_BLAKE2
  6829. void bench_blake2b(void)
  6830. {
  6831. Blake2b b2b;
  6832. byte digest[64];
  6833. double start;
  6834. int ret = 0, i, count;
  6835. DECLARE_MULTI_VALUE_STATS_VARS()
  6836. if (digest_stream) {
  6837. ret = wc_InitBlake2b(&b2b, 64);
  6838. if (ret != 0) {
  6839. printf("InitBlake2b failed, ret = %d\n", ret);
  6840. return;
  6841. }
  6842. bench_stats_start(&count, &start);
  6843. do {
  6844. for (i = 0; i < numBlocks; i++) {
  6845. ret = wc_Blake2bUpdate(&b2b, bench_plain, bench_size);
  6846. if (ret != 0) {
  6847. printf("Blake2bUpdate failed, ret = %d\n", ret);
  6848. return;
  6849. }
  6850. RECORD_MULTI_VALUE_STATS();
  6851. }
  6852. ret = wc_Blake2bFinal(&b2b, digest, 64);
  6853. if (ret != 0) {
  6854. printf("Blake2bFinal failed, ret = %d\n", ret);
  6855. return;
  6856. }
  6857. count += i;
  6858. } while (bench_stats_check(start)
  6859. #ifdef MULTI_VALUE_STATISTICS
  6860. || runs < minimum_runs
  6861. #endif
  6862. );
  6863. }
  6864. else {
  6865. bench_stats_start(&count, &start);
  6866. do {
  6867. for (i = 0; i < numBlocks; i++) {
  6868. ret = wc_InitBlake2b(&b2b, 64);
  6869. if (ret != 0) {
  6870. printf("InitBlake2b failed, ret = %d\n", ret);
  6871. return;
  6872. }
  6873. ret = wc_Blake2bUpdate(&b2b, bench_plain, bench_size);
  6874. if (ret != 0) {
  6875. printf("Blake2bUpdate failed, ret = %d\n", ret);
  6876. return;
  6877. }
  6878. ret = wc_Blake2bFinal(&b2b, digest, 64);
  6879. if (ret != 0) {
  6880. printf("Blake2bFinal failed, ret = %d\n", ret);
  6881. return;
  6882. }
  6883. RECORD_MULTI_VALUE_STATS();
  6884. }
  6885. count += i;
  6886. } while (bench_stats_check(start)
  6887. #ifdef MULTI_VALUE_STATISTICS
  6888. || runs < minimum_runs
  6889. #endif
  6890. );
  6891. }
  6892. bench_stats_sym_finish("BLAKE2b", 0, count, bench_size, start, ret);
  6893. #ifdef MULTI_VALUE_STATISTICS
  6894. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6895. #endif
  6896. }
  6897. #endif
  6898. #if defined(HAVE_BLAKE2S)
  6899. void bench_blake2s(void)
  6900. {
  6901. Blake2s b2s;
  6902. byte digest[32];
  6903. double start;
  6904. int ret = 0, i, count;
  6905. DECLARE_MULTI_VALUE_STATS_VARS()
  6906. if (digest_stream) {
  6907. ret = wc_InitBlake2s(&b2s, 32);
  6908. if (ret != 0) {
  6909. printf("InitBlake2s failed, ret = %d\n", ret);
  6910. return;
  6911. }
  6912. bench_stats_start(&count, &start);
  6913. do {
  6914. for (i = 0; i < numBlocks; i++) {
  6915. ret = wc_Blake2sUpdate(&b2s, bench_plain, bench_size);
  6916. if (ret != 0) {
  6917. printf("Blake2sUpdate failed, ret = %d\n", ret);
  6918. return;
  6919. }
  6920. RECORD_MULTI_VALUE_STATS();
  6921. }
  6922. ret = wc_Blake2sFinal(&b2s, digest, 32);
  6923. if (ret != 0) {
  6924. printf("Blake2sFinal failed, ret = %d\n", ret);
  6925. return;
  6926. }
  6927. count += i;
  6928. } while (bench_stats_check(start)
  6929. #ifdef MULTI_VALUE_STATISTICS
  6930. || runs < minimum_runs
  6931. #endif
  6932. );
  6933. }
  6934. else {
  6935. bench_stats_start(&count, &start);
  6936. do {
  6937. for (i = 0; i < numBlocks; i++) {
  6938. ret = wc_InitBlake2s(&b2s, 32);
  6939. if (ret != 0) {
  6940. printf("InitBlake2b failed, ret = %d\n", ret);
  6941. return;
  6942. }
  6943. ret = wc_Blake2sUpdate(&b2s, bench_plain, bench_size);
  6944. if (ret != 0) {
  6945. printf("Blake2bUpdate failed, ret = %d\n", ret);
  6946. return;
  6947. }
  6948. ret = wc_Blake2sFinal(&b2s, digest, 32);
  6949. if (ret != 0) {
  6950. printf("Blake2sFinal failed, ret = %d\n", ret);
  6951. return;
  6952. }
  6953. RECORD_MULTI_VALUE_STATS();
  6954. }
  6955. count += i;
  6956. } while (bench_stats_check(start)
  6957. #ifdef MULTI_VALUE_STATISTICS
  6958. || runs < minimum_runs
  6959. #endif
  6960. );
  6961. }
  6962. bench_stats_sym_finish("BLAKE2s", 0, count, bench_size, start, ret);
  6963. #ifdef MULTI_VALUE_STATISTICS
  6964. bench_multi_value_stats(max, min, sum, squareSum, runs);
  6965. #endif
  6966. }
  6967. #endif
  6968. #ifdef WOLFSSL_CMAC
  6969. static void bench_cmac_helper(word32 keySz, const char* outMsg, int useDeviceID)
  6970. {
  6971. Cmac cmac;
  6972. byte digest[AES_BLOCK_SIZE];
  6973. word32 digestSz = sizeof(digest);
  6974. double start;
  6975. int ret, i, count;
  6976. DECLARE_MULTI_VALUE_STATS_VARS()
  6977. #ifdef WOLFSSL_SECO_CAAM
  6978. unsigned int keyID;
  6979. int keyGroup = 1; /* group one was chosen arbitrarily */
  6980. int keyInfo = CAAM_KEY_TRANSIENT;
  6981. int keyType = CAAM_KEYTYPE_AES128;
  6982. byte pubKey[AES_256_KEY_SIZE];
  6983. if (keySz == AES_256_KEY_SIZE) {
  6984. keyType = CAAM_KEYTYPE_AES256;
  6985. }
  6986. if (useDeviceID &&
  6987. wc_SECO_GenerateKey(CAAM_GENERATE_KEY, keyGroup, pubKey, 0, keyType,
  6988. keyInfo, &keyID) != 0) {
  6989. printf("Error generating key in hsm\n");
  6990. return;
  6991. }
  6992. #endif
  6993. (void)useDeviceID;
  6994. bench_stats_start(&count, &start);
  6995. do {
  6996. #ifdef HAVE_FIPS
  6997. ret = wc_InitCmac(&cmac, bench_key, keySz, WC_CMAC_AES, NULL);
  6998. #else
  6999. ret = wc_InitCmac_ex(&cmac, bench_key, keySz, WC_CMAC_AES, NULL,
  7000. HEAP_HINT, useDeviceID ? devId : INVALID_DEVID);
  7001. #endif
  7002. if (ret != 0) {
  7003. printf("InitCmac failed, ret = %d\n", ret);
  7004. return;
  7005. }
  7006. #ifdef WOLFSSL_SECO_CAAM
  7007. if (useDeviceID) {
  7008. wc_SECO_CMACSetKeyID(&cmac, keyID);
  7009. }
  7010. #endif
  7011. for (i = 0; i < numBlocks; i++) {
  7012. ret = wc_CmacUpdate(&cmac, bench_plain, bench_size);
  7013. if (ret != 0) {
  7014. printf("CmacUpdate failed, ret = %d\n", ret);
  7015. return;
  7016. }
  7017. RECORD_MULTI_VALUE_STATS();
  7018. }
  7019. /* Note: final force zero's the Cmac struct */
  7020. ret = wc_CmacFinal(&cmac, digest, &digestSz);
  7021. if (ret != 0) {
  7022. printf("CmacFinal failed, ret = %d\n", ret);
  7023. return;
  7024. }
  7025. count += i;
  7026. } while (bench_stats_check(start)
  7027. #ifdef MULTI_VALUE_STATISTICS
  7028. || runs < minimum_runs
  7029. #endif
  7030. );
  7031. bench_stats_sym_finish(outMsg, useDeviceID, count, bench_size, start, ret);
  7032. #ifdef MULTI_VALUE_STATISTICS
  7033. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7034. #endif
  7035. }
  7036. void bench_cmac(int useDeviceID)
  7037. {
  7038. #ifdef WOLFSSL_AES_128
  7039. bench_cmac_helper(16, "AES-128-CMAC", useDeviceID);
  7040. #endif
  7041. #ifdef WOLFSSL_AES_256
  7042. bench_cmac_helper(32, "AES-256-CMAC", useDeviceID);
  7043. #endif
  7044. }
  7045. #endif /* WOLFSSL_CMAC */
  7046. #ifdef HAVE_SCRYPT
  7047. void bench_scrypt(void)
  7048. {
  7049. byte derived[64];
  7050. double start;
  7051. int ret, i, count;
  7052. DECLARE_MULTI_VALUE_STATS_VARS()
  7053. bench_stats_start(&count, &start);
  7054. do {
  7055. for (i = 0; i < scryptCnt; i++) {
  7056. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  7057. (byte*)"SodiumChloride", 14, 14, 8, 1,
  7058. sizeof(derived));
  7059. if (ret != 0) {
  7060. printf("scrypt failed, ret = %d\n", ret);
  7061. goto exit;
  7062. }
  7063. RECORD_MULTI_VALUE_STATS();
  7064. }
  7065. count += i;
  7066. } while (bench_stats_check(start)
  7067. #ifdef MULTI_VALUE_STATISTICS
  7068. || runs < minimum_runs
  7069. #endif
  7070. );
  7071. exit:
  7072. bench_stats_asym_finish("scrypt", 17, "", 0, count, start, ret);
  7073. #ifdef MULTI_VALUE_STATISTICS
  7074. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7075. #endif
  7076. }
  7077. #endif /* HAVE_SCRYPT */
  7078. #ifndef NO_HMAC
  7079. static void bench_hmac(int useDeviceID, int type, int digestSz,
  7080. const byte* key, word32 keySz, const char* label)
  7081. {
  7082. WC_DECLARE_ARRAY(hmac, Hmac, BENCH_MAX_PENDING,
  7083. sizeof(Hmac), HEAP_HINT);
  7084. double start;
  7085. int ret = 0, i, count = 0, times, pending = 0;
  7086. DECLARE_MULTI_VALUE_STATS_VARS()
  7087. #ifdef WOLFSSL_ASYNC_CRYPT
  7088. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING,
  7089. WC_MAX_DIGEST_SIZE, HEAP_HINT);
  7090. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING,
  7091. WC_MAX_DIGEST_SIZE, HEAP_HINT);
  7092. #else
  7093. byte digest[BENCH_MAX_PENDING][WC_MAX_DIGEST_SIZE];
  7094. #endif
  7095. (void)digestSz;
  7096. WC_CALLOC_ARRAY(hmac, Hmac, BENCH_MAX_PENDING,
  7097. sizeof(Hmac), HEAP_HINT);
  7098. /* init keys */
  7099. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7100. ret = wc_HmacInit(hmac[i], HEAP_HINT,
  7101. useDeviceID ? devId : INVALID_DEVID);
  7102. if (ret != 0) {
  7103. printf("wc_HmacInit failed for %s, ret = %d\n", label, ret);
  7104. goto exit;
  7105. }
  7106. ret = wc_HmacSetKey(hmac[i], type, key, keySz);
  7107. if (ret != 0) {
  7108. printf("wc_HmacSetKey failed for %s, ret = %d\n", label, ret);
  7109. goto exit;
  7110. }
  7111. }
  7112. bench_stats_start(&count, &start);
  7113. do {
  7114. for (times = 0; times < numBlocks || pending > 0; ) {
  7115. bench_async_poll(&pending);
  7116. /* while free pending slots in queue, submit ops */
  7117. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7118. if (bench_async_check(&ret,
  7119. BENCH_ASYNC_GET_DEV(hmac[i]), 0,
  7120. &times, numBlocks, &pending)) {
  7121. ret = wc_HmacUpdate(hmac[i], bench_plain, bench_size);
  7122. if (!bench_async_handle(&ret,
  7123. BENCH_ASYNC_GET_DEV(hmac[i]),
  7124. 0, &times, &pending)) {
  7125. goto exit_hmac;
  7126. }
  7127. }
  7128. } /* for i */
  7129. } /* for times */
  7130. count += times;
  7131. times = 0;
  7132. do {
  7133. bench_async_poll(&pending);
  7134. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7135. if (bench_async_check(&ret,
  7136. BENCH_ASYNC_GET_DEV(hmac[i]), 0,
  7137. &times, numBlocks, &pending)) {
  7138. ret = wc_HmacFinal(hmac[i], digest[i]);
  7139. if (!bench_async_handle(&ret,
  7140. BENCH_ASYNC_GET_DEV(hmac[i]),
  7141. 0, &times, &pending)) {
  7142. goto exit_hmac;
  7143. }
  7144. }
  7145. RECORD_MULTI_VALUE_STATS();
  7146. } /* for i */
  7147. } while (pending > 0);
  7148. } while (bench_stats_check(start)
  7149. #ifdef MULTI_VALUE_STATISTICS
  7150. || runs < minimum_runs
  7151. #endif
  7152. );
  7153. exit_hmac:
  7154. bench_stats_sym_finish(label, useDeviceID, count, bench_size, start, ret);
  7155. #ifdef MULTI_VALUE_STATISTICS
  7156. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7157. #endif
  7158. exit:
  7159. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7160. wc_HmacFree(hmac[i]);
  7161. }
  7162. WC_FREE_ARRAY(hmac, BENCH_MAX_PENDING, HEAP_HINT);
  7163. #ifdef WOLFSSL_ASYNC_CRYPT
  7164. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  7165. #endif
  7166. }
  7167. #ifndef NO_MD5
  7168. void bench_hmac_md5(int useDeviceID)
  7169. {
  7170. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7171. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7172. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  7173. bench_hmac(useDeviceID, WC_MD5, WC_MD5_DIGEST_SIZE, key, sizeof(key),
  7174. "HMAC-MD5");
  7175. }
  7176. #endif /* NO_MD5 */
  7177. #ifndef NO_SHA
  7178. void bench_hmac_sha(int useDeviceID)
  7179. {
  7180. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7181. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7182. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7183. 0x0b, 0x0b, 0x0b, 0x0b };
  7184. bench_hmac(useDeviceID, WC_SHA, WC_SHA_DIGEST_SIZE, key, sizeof(key),
  7185. "HMAC-SHA");
  7186. }
  7187. #endif /* NO_SHA */
  7188. #ifdef WOLFSSL_SHA224
  7189. void bench_hmac_sha224(int useDeviceID)
  7190. {
  7191. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7192. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7193. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7194. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7195. 0x0b, 0x0b, 0x0b, 0x0b };
  7196. bench_hmac(useDeviceID, WC_SHA224,
  7197. WC_SHA224_DIGEST_SIZE, key, sizeof(key),
  7198. "HMAC-SHA224");
  7199. }
  7200. #endif /* WOLFSSL_SHA224 */
  7201. #ifndef NO_SHA256
  7202. void bench_hmac_sha256(int useDeviceID)
  7203. {
  7204. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7205. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7206. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7207. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7208. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  7209. bench_hmac(useDeviceID, WC_SHA256, WC_SHA256_DIGEST_SIZE, key, sizeof(key),
  7210. "HMAC-SHA256");
  7211. }
  7212. #endif /* NO_SHA256 */
  7213. #ifdef WOLFSSL_SHA384
  7214. void bench_hmac_sha384(int useDeviceID)
  7215. {
  7216. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7217. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7218. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7219. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7220. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7221. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7222. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  7223. bench_hmac(useDeviceID, WC_SHA384, WC_SHA384_DIGEST_SIZE, key, sizeof(key),
  7224. "HMAC-SHA384");
  7225. }
  7226. #endif /* WOLFSSL_SHA384 */
  7227. #ifdef WOLFSSL_SHA512
  7228. void bench_hmac_sha512(int useDeviceID)
  7229. {
  7230. WOLFSSL_SMALL_STACK_STATIC const byte key[] = {
  7231. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7232. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7233. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7234. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7235. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7236. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7237. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  7238. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  7239. bench_hmac(useDeviceID, WC_SHA512, WC_SHA512_DIGEST_SIZE, key, sizeof(key),
  7240. "HMAC-SHA512");
  7241. }
  7242. #endif /* WOLFSSL_SHA512 */
  7243. #ifndef NO_PWDBASED
  7244. void bench_pbkdf2(void)
  7245. {
  7246. double start;
  7247. int ret = 0, count = 0;
  7248. const char* passwd32 = "passwordpasswordpasswordpassword";
  7249. WOLFSSL_SMALL_STACK_STATIC const byte salt32[] = {
  7250. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06,
  7251. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06,
  7252. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06,
  7253. 0x78, 0x57, 0x8E, 0x5a, 0x5d, 0x63, 0xcb, 0x06 };
  7254. byte derived[32];
  7255. DECLARE_MULTI_VALUE_STATS_VARS()
  7256. bench_stats_start(&count, &start);
  7257. do {
  7258. ret = wc_PBKDF2(derived, (const byte*)passwd32, (int)XSTRLEN(passwd32),
  7259. salt32, (int)sizeof(salt32), 1000, 32, WC_SHA256);
  7260. count++;
  7261. RECORD_MULTI_VALUE_STATS();
  7262. } while (bench_stats_check(start)
  7263. #ifdef MULTI_VALUE_STATISTICS
  7264. || runs < minimum_runs
  7265. #endif
  7266. );
  7267. bench_stats_sym_finish("PBKDF2", 32, count, 32, start, ret);
  7268. #ifdef MULTI_VALUE_STATISTICS
  7269. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7270. #endif
  7271. }
  7272. #endif /* !NO_PWDBASED */
  7273. #endif /* NO_HMAC */
  7274. #ifdef WOLFSSL_SIPHASH
  7275. void bench_siphash(void)
  7276. {
  7277. double start;
  7278. int ret = 0, count;
  7279. const char* passwd16 = "passwordpassword";
  7280. byte out[16];
  7281. int i;
  7282. DECLARE_MULTI_VALUE_STATS_VARS()
  7283. bench_stats_start(&count, &start);
  7284. do {
  7285. for (i = 0; i < numBlocks; i++) {
  7286. ret = wc_SipHash((const byte*)passwd16, bench_plain, bench_size,
  7287. out, 8);
  7288. RECORD_MULTI_VALUE_STATS();
  7289. }
  7290. count += i;
  7291. } while (bench_stats_check(start)
  7292. #ifdef MULTI_VALUE_STATISTICS
  7293. || runs < minimum_runs
  7294. #endif
  7295. );
  7296. bench_stats_sym_finish("SipHash-8", 1, count, bench_size, start, ret);
  7297. #ifdef MULTI_VALUE_STATISTICS
  7298. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7299. #endif
  7300. RESET_MULTI_VALUE_STATS_VARS();
  7301. bench_stats_start(&count, &start);
  7302. do {
  7303. for (i = 0; i < numBlocks; i++) {
  7304. ret = wc_SipHash((const byte*)passwd16, bench_plain, bench_size,
  7305. out, 16);
  7306. RECORD_MULTI_VALUE_STATS();
  7307. }
  7308. count += i;
  7309. } while (bench_stats_check(start)
  7310. #ifdef MULTI_VALUE_STATISTICS
  7311. || runs < minimum_runs
  7312. #endif
  7313. );
  7314. bench_stats_sym_finish("SipHash-16", 1, count, bench_size, start, ret);
  7315. #ifdef MULTI_VALUE_STATISTICS
  7316. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7317. #endif
  7318. }
  7319. #endif
  7320. #ifdef WC_SRTP_KDF
  7321. void bench_srtpkdf(void)
  7322. {
  7323. double start;
  7324. int count;
  7325. int ret = 0;
  7326. byte keyE[32];
  7327. byte keyA[20];
  7328. byte keyS[14];
  7329. const byte *key = bench_key_buf;
  7330. const byte salt[14] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  7331. 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e };
  7332. const byte index[6] = { 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA };
  7333. int kdrIdx = 0;
  7334. int i;
  7335. DECLARE_MULTI_VALUE_STATS_VARS()
  7336. bench_stats_start(&count, &start);
  7337. do {
  7338. for (i = 0; i < numBlocks; i++) {
  7339. ret = wc_SRTP_KDF(key, AES_128_KEY_SIZE, salt, sizeof(salt),
  7340. kdrIdx, index, keyE, AES_128_KEY_SIZE, keyA, sizeof(keyA),
  7341. keyS, sizeof(keyS));
  7342. RECORD_MULTI_VALUE_STATS();
  7343. }
  7344. count += i;
  7345. } while (bench_stats_check(start)
  7346. #ifdef MULTI_VALUE_STATISTICS
  7347. || runs < minimum_runs
  7348. #endif
  7349. );
  7350. bench_stats_asym_finish("KDF", 128, "SRTP", 0, count, start, ret);
  7351. #ifdef MULTI_VALUE_STATISTICS
  7352. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7353. #endif
  7354. RESET_MULTI_VALUE_STATS_VARS();
  7355. bench_stats_start(&count, &start);
  7356. do {
  7357. for (i = 0; i < numBlocks; i++) {
  7358. ret = wc_SRTP_KDF(key, AES_256_KEY_SIZE, salt, sizeof(salt),
  7359. kdrIdx, index, keyE, AES_256_KEY_SIZE, keyA, sizeof(keyA),
  7360. keyS, sizeof(keyS));
  7361. RECORD_MULTI_VALUE_STATS();
  7362. }
  7363. count += i;
  7364. } while (bench_stats_check(start)
  7365. #ifdef MULTI_VALUE_STATISTICS
  7366. || runs < minimum_runs
  7367. #endif
  7368. );
  7369. bench_stats_asym_finish("KDF", 256, "SRTP", 0, count, start, ret);
  7370. #ifdef MULTI_VALUE_STATISTICS
  7371. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7372. #endif
  7373. RESET_MULTI_VALUE_STATS_VARS();
  7374. bench_stats_start(&count, &start);
  7375. do {
  7376. for (i = 0; i < numBlocks; i++) {
  7377. ret = wc_SRTCP_KDF(key, AES_128_KEY_SIZE, salt, sizeof(salt),
  7378. kdrIdx, index, keyE, AES_128_KEY_SIZE, keyA, sizeof(keyA),
  7379. keyS, sizeof(keyS));
  7380. RECORD_MULTI_VALUE_STATS();
  7381. }
  7382. count += i;
  7383. } while (bench_stats_check(start)
  7384. #ifdef MULTI_VALUE_STATISTICS
  7385. || runs < minimum_runs
  7386. #endif
  7387. );
  7388. bench_stats_asym_finish("KDF", 128, "SRTCP", 0, count, start, ret);
  7389. #ifdef MULTI_VALUE_STATISTICS
  7390. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7391. #endif
  7392. RESET_MULTI_VALUE_STATS_VARS();
  7393. bench_stats_start(&count, &start);
  7394. do {
  7395. for (i = 0; i < numBlocks; i++) {
  7396. ret = wc_SRTCP_KDF(key, AES_256_KEY_SIZE, salt, sizeof(salt),
  7397. kdrIdx, index, keyE, AES_256_KEY_SIZE, keyA, sizeof(keyA),
  7398. keyS, sizeof(keyS));
  7399. RECORD_MULTI_VALUE_STATS();
  7400. }
  7401. count += i;
  7402. } while (bench_stats_check(start)
  7403. #ifdef MULTI_VALUE_STATISTICS
  7404. || runs < minimum_runs
  7405. #endif
  7406. );
  7407. bench_stats_asym_finish("KDF", 256, "SRTCP", 0, count, start, ret);
  7408. #ifdef MULTI_VALUE_STATISTICS
  7409. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7410. #endif
  7411. }
  7412. #endif
  7413. #ifndef NO_RSA
  7414. #if defined(WOLFSSL_KEY_GEN)
  7415. static void bench_rsaKeyGen_helper(int useDeviceID, word32 keySz)
  7416. {
  7417. WC_DECLARE_ARRAY(genKey, RsaKey, BENCH_MAX_PENDING,
  7418. sizeof(RsaKey), HEAP_HINT);
  7419. double start = 0;
  7420. int ret = 0, i, count = 0, times, pending = 0;
  7421. const long rsa_e_val = WC_RSA_EXPONENT;
  7422. const char**desc = bench_desc_words[lng_index];
  7423. DECLARE_MULTI_VALUE_STATS_VARS()
  7424. WC_CALLOC_ARRAY(genKey, RsaKey, BENCH_MAX_PENDING,
  7425. sizeof(RsaKey), HEAP_HINT);
  7426. bench_stats_start(&count, &start);
  7427. do {
  7428. /* while free pending slots in queue, submit ops */
  7429. for (times = 0; times < genTimes || pending > 0; ) {
  7430. bench_async_poll(&pending);
  7431. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7432. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]),
  7433. 0, &times, genTimes, &pending)) {
  7434. wc_FreeRsaKey(genKey[i]);
  7435. ret = wc_InitRsaKey_ex(genKey[i], HEAP_HINT, devId);
  7436. if (ret < 0) {
  7437. goto exit;
  7438. }
  7439. ret = wc_MakeRsaKey(genKey[i], (int)keySz, rsa_e_val,
  7440. &gRng);
  7441. if (!bench_async_handle(&ret,
  7442. BENCH_ASYNC_GET_DEV(genKey[i]), 0,
  7443. &times, &pending)) {
  7444. goto exit;
  7445. }
  7446. }
  7447. } /* for i */
  7448. RECORD_MULTI_VALUE_STATS();
  7449. } /* for times */
  7450. count += times;
  7451. } while (bench_stats_check(start)
  7452. #ifdef MULTI_VALUE_STATISTICS
  7453. || runs < minimum_runs
  7454. #endif
  7455. );
  7456. exit:
  7457. bench_stats_asym_finish("RSA", (int)keySz, desc[2], useDeviceID, count,
  7458. start, ret);
  7459. #ifdef MULTI_VALUE_STATISTICS
  7460. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7461. #endif
  7462. /* cleanup */
  7463. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7464. wc_FreeRsaKey(genKey[i]);
  7465. }
  7466. WC_FREE_ARRAY(genKey, BENCH_MAX_PENDING, HEAP_HINT);
  7467. }
  7468. void bench_rsaKeyGen(int useDeviceID)
  7469. {
  7470. int k;
  7471. #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
  7472. static const word32 keySizes[2] = {1024, 2048};
  7473. #else
  7474. static const word32 keySizes[1] = {2048};
  7475. #endif
  7476. for (k = 0; k < (int)(sizeof(keySizes)/sizeof(int)); k++) {
  7477. bench_rsaKeyGen_helper(useDeviceID, keySizes[k]);
  7478. }
  7479. }
  7480. void bench_rsaKeyGen_size(int useDeviceID, word32 keySz)
  7481. {
  7482. bench_rsaKeyGen_helper(useDeviceID, keySz);
  7483. }
  7484. #endif /* WOLFSSL_KEY_GEN */
  7485. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  7486. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  7487. #if defined(WOLFSSL_MDK_SHELL)
  7488. static char *certRSAname = "certs/rsa2048.der";
  7489. /* set by shell command */
  7490. static void set_Bench_RSA_File(char * cert) { certRSAname = cert ; }
  7491. #elif defined(FREESCALE_MQX)
  7492. static char *certRSAname = "a:\\certs\\rsa2048.der";
  7493. #else
  7494. static const char *certRSAname = "certs/rsa2048.der";
  7495. #endif
  7496. #endif
  7497. #define RSA_BUF_SIZE 384 /* for up to 3072 bit */
  7498. #if defined(WOLFSSL_RSA_VERIFY_INLINE) || defined(WOLFSSL_RSA_PUBLIC_ONLY)
  7499. #if defined(USE_CERT_BUFFERS_2048)
  7500. static const unsigned char rsa_2048_sig[] = {
  7501. 0x8c, 0x9e, 0x37, 0xbf, 0xc3, 0xa6, 0xba, 0x1c,
  7502. 0x53, 0x22, 0x40, 0x4b, 0x8b, 0x0d, 0x3c, 0x0e,
  7503. 0x2e, 0x8c, 0x31, 0x2c, 0x47, 0xbf, 0x03, 0x48,
  7504. 0x18, 0x46, 0x73, 0x8d, 0xd7, 0xdd, 0x17, 0x64,
  7505. 0x0d, 0x7f, 0xdc, 0x74, 0xed, 0x80, 0xc3, 0xe8,
  7506. 0x9a, 0x18, 0x33, 0xd4, 0xe6, 0xc5, 0xe1, 0x54,
  7507. 0x75, 0xd1, 0xbb, 0x40, 0xde, 0xa8, 0xb9, 0x1b,
  7508. 0x14, 0xe8, 0xc1, 0x39, 0xeb, 0xa0, 0x69, 0x8a,
  7509. 0xc6, 0x9b, 0xef, 0x53, 0xb5, 0x23, 0x2b, 0x78,
  7510. 0x06, 0x43, 0x37, 0x11, 0x81, 0x84, 0x73, 0x33,
  7511. 0x33, 0xfe, 0xf7, 0x5d, 0x2b, 0x84, 0xd6, 0x83,
  7512. 0xd6, 0xdd, 0x55, 0x33, 0xef, 0xd1, 0xf7, 0x12,
  7513. 0xb0, 0xc2, 0x0e, 0xb1, 0x78, 0xd4, 0xa8, 0xa3,
  7514. 0x25, 0xeb, 0xed, 0x9a, 0xb3, 0xee, 0xc3, 0x7e,
  7515. 0xce, 0x13, 0x18, 0x86, 0x31, 0xe1, 0xef, 0x01,
  7516. 0x0f, 0x6e, 0x67, 0x24, 0x74, 0xbd, 0x0b, 0x7f,
  7517. 0xa9, 0xca, 0x6f, 0xaa, 0x83, 0x28, 0x90, 0x40,
  7518. 0xf1, 0xb5, 0x10, 0x0e, 0x26, 0x03, 0x05, 0x5d,
  7519. 0x87, 0xb4, 0xe0, 0x4c, 0x98, 0xd8, 0xc6, 0x42,
  7520. 0x89, 0x77, 0xeb, 0xb6, 0xd4, 0xe6, 0x26, 0xf3,
  7521. 0x31, 0x25, 0xde, 0x28, 0x38, 0x58, 0xe8, 0x2c,
  7522. 0xf4, 0x56, 0x7c, 0xb6, 0xfd, 0x99, 0xb0, 0xb0,
  7523. 0xf4, 0x83, 0xb6, 0x74, 0xa9, 0x5b, 0x9f, 0xe8,
  7524. 0xe9, 0xf1, 0xa1, 0x2a, 0xbd, 0xf6, 0x83, 0x28,
  7525. 0x09, 0xda, 0xa6, 0xd6, 0xcd, 0x61, 0x60, 0xf7,
  7526. 0x13, 0x4e, 0x46, 0x57, 0x38, 0x1e, 0x11, 0x92,
  7527. 0x6b, 0x6b, 0xcf, 0xd3, 0xf4, 0x8b, 0x66, 0x03,
  7528. 0x25, 0xa3, 0x7a, 0x2f, 0xce, 0xc1, 0x85, 0xa5,
  7529. 0x48, 0x91, 0x8a, 0xb3, 0x4f, 0x5d, 0x98, 0xb1,
  7530. 0x69, 0x58, 0x47, 0x69, 0x0c, 0x52, 0xdc, 0x42,
  7531. 0x4c, 0xef, 0xe8, 0xd4, 0x4d, 0x6a, 0x33, 0x7d,
  7532. 0x9e, 0xd2, 0x51, 0xe6, 0x41, 0xbf, 0x4f, 0xa2
  7533. };
  7534. #elif defined(USE_CERT_BUFFERS_3072)
  7535. static const unsigned char rsa_3072_sig[] = {
  7536. 0x1a, 0xd6, 0x0d, 0xfd, 0xe3, 0x41, 0x95, 0x76,
  7537. 0x27, 0x16, 0x7d, 0xc7, 0x94, 0x16, 0xca, 0xa8,
  7538. 0x26, 0x08, 0xbe, 0x78, 0x87, 0x72, 0x4c, 0xd9,
  7539. 0xa7, 0xfc, 0x33, 0x77, 0x2d, 0x53, 0x07, 0xb5,
  7540. 0x8c, 0xce, 0x48, 0x17, 0x9b, 0xff, 0x9f, 0x9b,
  7541. 0x17, 0xc4, 0xbb, 0x72, 0xed, 0xdb, 0xa0, 0x34,
  7542. 0x69, 0x5b, 0xc7, 0x4e, 0xbf, 0xec, 0x13, 0xc5,
  7543. 0x98, 0x71, 0x9a, 0x4e, 0x18, 0x0e, 0xcb, 0xe7,
  7544. 0xc6, 0xd5, 0x21, 0x31, 0x7c, 0x0d, 0xae, 0x14,
  7545. 0x2b, 0x87, 0x4f, 0x77, 0x95, 0x2e, 0x26, 0xe2,
  7546. 0x83, 0xfe, 0x49, 0x1e, 0x87, 0x19, 0x4a, 0x63,
  7547. 0x73, 0x75, 0xf1, 0xf5, 0x71, 0xd2, 0xce, 0xd4,
  7548. 0x39, 0x2b, 0xd9, 0xe0, 0x76, 0x70, 0xc8, 0xf8,
  7549. 0xed, 0xdf, 0x90, 0x57, 0x17, 0xb9, 0x16, 0xf6,
  7550. 0xe9, 0x49, 0x48, 0xce, 0x5a, 0x8b, 0xe4, 0x84,
  7551. 0x7c, 0xf3, 0x31, 0x68, 0x97, 0x45, 0x68, 0x38,
  7552. 0x50, 0x3a, 0x70, 0xbd, 0xb3, 0xd3, 0xd2, 0xe0,
  7553. 0x56, 0x5b, 0xc2, 0x0c, 0x2c, 0x10, 0x70, 0x7b,
  7554. 0xd4, 0x99, 0xf9, 0x38, 0x31, 0xb1, 0x86, 0xa0,
  7555. 0x07, 0xf1, 0xf6, 0x53, 0xb0, 0x44, 0x82, 0x40,
  7556. 0xd2, 0xab, 0x0e, 0x71, 0x5d, 0xe1, 0xea, 0x3a,
  7557. 0x77, 0xc9, 0xef, 0xfe, 0x54, 0x65, 0xa3, 0x49,
  7558. 0xfd, 0xa5, 0x33, 0xaa, 0x16, 0x1a, 0x38, 0xe7,
  7559. 0xaa, 0xb7, 0x13, 0xb2, 0x3b, 0xc7, 0x00, 0x87,
  7560. 0x12, 0xfe, 0xfd, 0xf4, 0x55, 0x6d, 0x1d, 0x4a,
  7561. 0x0e, 0xad, 0xd0, 0x4c, 0x55, 0x91, 0x60, 0xd9,
  7562. 0xef, 0x74, 0x69, 0x22, 0x8c, 0x51, 0x65, 0xc2,
  7563. 0x04, 0xac, 0xd3, 0x8d, 0xf7, 0x35, 0x29, 0x13,
  7564. 0x6d, 0x61, 0x7c, 0x39, 0x2f, 0x41, 0x4c, 0xdf,
  7565. 0x38, 0xfd, 0x1a, 0x7d, 0x42, 0xa7, 0x6f, 0x3f,
  7566. 0x3d, 0x9b, 0xd1, 0x97, 0xab, 0xc0, 0xa7, 0x28,
  7567. 0x1c, 0xc0, 0x02, 0x26, 0xeb, 0xce, 0xf9, 0xe1,
  7568. 0x34, 0x45, 0xaf, 0xbf, 0x8d, 0xb8, 0xe0, 0xff,
  7569. 0xd9, 0x6f, 0x77, 0xf3, 0xf7, 0xed, 0x6a, 0xbb,
  7570. 0x03, 0x52, 0xfb, 0x38, 0xfc, 0xea, 0x9f, 0xc9,
  7571. 0x98, 0xed, 0x21, 0x45, 0xaf, 0x43, 0x2b, 0x64,
  7572. 0x96, 0x82, 0x30, 0xe9, 0xb4, 0x36, 0x89, 0x77,
  7573. 0x07, 0x4a, 0xc6, 0x1f, 0x38, 0x7a, 0xee, 0xb6,
  7574. 0x86, 0xf6, 0x2f, 0x03, 0xec, 0xa2, 0xe5, 0x48,
  7575. 0xe5, 0x5a, 0xf5, 0x1c, 0xd2, 0xd9, 0xd8, 0x2d,
  7576. 0x9d, 0x06, 0x07, 0xc9, 0x8b, 0x5d, 0xe0, 0x0f,
  7577. 0x5e, 0x0c, 0x53, 0x27, 0xff, 0x23, 0xee, 0xca,
  7578. 0x5e, 0x4d, 0xf1, 0x95, 0x77, 0x78, 0x1f, 0xf2,
  7579. 0x44, 0x5b, 0x7d, 0x01, 0x49, 0x61, 0x6f, 0x6d,
  7580. 0xbf, 0xf5, 0x19, 0x06, 0x39, 0xe9, 0xe9, 0x29,
  7581. 0xde, 0x47, 0x5e, 0x2e, 0x1f, 0x68, 0xf4, 0x32,
  7582. 0x5e, 0xe9, 0xd0, 0xa7, 0xb4, 0x2a, 0x45, 0xdf,
  7583. 0x15, 0x7d, 0x0d, 0x5b, 0xef, 0xc6, 0x23, 0xac
  7584. };
  7585. #else
  7586. #error Not Supported Yet!
  7587. #endif
  7588. #endif /* WOLFSSL_RSA_VERIFY_INLINE || WOLFSSL_RSA_PUBLIC_ONLY */
  7589. static void bench_rsa_helper(int useDeviceID,
  7590. WC_ARRAY_ARG(rsaKey,
  7591. RsaKey,
  7592. BENCH_MAX_PENDING,
  7593. sizeof(RsaKey)),
  7594. word32 rsaKeySz)
  7595. {
  7596. int ret = 0, i, times, count = 0, pending = 0;
  7597. word32 idx = 0;
  7598. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  7599. const char* messageStr = TEST_STRING;
  7600. const int len = (int)TEST_STRING_SZ;
  7601. #endif
  7602. double start = 0.0F;
  7603. const char**desc = bench_desc_words[lng_index];
  7604. DECLARE_MULTI_VALUE_STATS_VARS()
  7605. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  7606. WC_DECLARE_VAR(message, byte, TEST_STRING_SZ, HEAP_HINT);
  7607. #endif
  7608. WC_DECLARE_HEAP_ARRAY(enc, byte, BENCH_MAX_PENDING,
  7609. rsaKeySz, HEAP_HINT);
  7610. #if (!defined(WOLFSSL_RSA_VERIFY_INLINE) && \
  7611. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  7612. WC_DECLARE_HEAP_ARRAY(out, byte, BENCH_MAX_PENDING,
  7613. rsaKeySz, HEAP_HINT);
  7614. #else
  7615. byte* out[BENCH_MAX_PENDING];
  7616. #endif
  7617. XMEMSET(out, 0, sizeof(out));
  7618. WC_ALLOC_HEAP_ARRAY(enc, byte, BENCH_MAX_PENDING,
  7619. rsaKeySz, HEAP_HINT);
  7620. #if (!defined(WOLFSSL_RSA_VERIFY_INLINE) && \
  7621. !defined(WOLFSSL_RSA_PUBLIC_ONLY))
  7622. WC_ALLOC_HEAP_ARRAY(out, byte, BENCH_MAX_PENDING,
  7623. rsaKeySz, HEAP_HINT);
  7624. if (out[0] == NULL) {
  7625. ret = MEMORY_E;
  7626. goto exit;
  7627. }
  7628. #endif
  7629. if (enc[0] == NULL) {
  7630. ret = MEMORY_E;
  7631. goto exit;
  7632. }
  7633. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  7634. WC_ALLOC_VAR(message, byte, TEST_STRING_SZ, HEAP_HINT);
  7635. XMEMCPY(message, messageStr, len);
  7636. #endif
  7637. if (!rsa_sign_verify) {
  7638. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  7639. /* begin public RSA */
  7640. bench_stats_start(&count, &start);
  7641. do {
  7642. for (times = 0; times < ntimes || pending > 0; ) {
  7643. bench_async_poll(&pending);
  7644. /* while free pending slots in queue, submit ops */
  7645. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7646. if (bench_async_check(&ret,
  7647. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7648. 1, &times, ntimes, &pending)) {
  7649. ret = wc_RsaPublicEncrypt(message, (word32)len, enc[i],
  7650. rsaKeySz/8, rsaKey[i],
  7651. GLOBAL_RNG);
  7652. if (!bench_async_handle(&ret,
  7653. BENCH_ASYNC_GET_DEV(
  7654. rsaKey[i]), 1, &times,
  7655. &pending)) {
  7656. goto exit_rsa_verify;
  7657. }
  7658. }
  7659. } /* for i */
  7660. RECORD_MULTI_VALUE_STATS();
  7661. } /* for times */
  7662. count += times;
  7663. } while (bench_stats_check(start)
  7664. #ifdef MULTI_VALUE_STATISTICS
  7665. || runs < minimum_runs
  7666. #endif
  7667. );
  7668. exit_rsa_verify:
  7669. bench_stats_asym_finish("RSA", (int)rsaKeySz, desc[0],
  7670. useDeviceID, count, start, ret);
  7671. #ifdef MULTI_VALUE_STATISTICS
  7672. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7673. #endif
  7674. #endif /* !WOLFSSL_RSA_VERIFY_ONLY */
  7675. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  7676. if (ret < 0) {
  7677. goto exit;
  7678. }
  7679. RESET_MULTI_VALUE_STATS_VARS();
  7680. /* capture resulting encrypt length */
  7681. idx = (word32)(rsaKeySz/8);
  7682. /* begin private async RSA */
  7683. bench_stats_start(&count, &start);
  7684. do {
  7685. for (times = 0; times < ntimes || pending > 0; ) {
  7686. bench_async_poll(&pending);
  7687. /* while free pending slots in queue, submit ops */
  7688. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7689. if (bench_async_check(&ret,
  7690. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7691. 1, &times, ntimes, &pending)) {
  7692. ret = wc_RsaPrivateDecrypt(enc[i], idx, out[i],
  7693. rsaKeySz/8, rsaKey[i]);
  7694. if (!bench_async_handle(&ret,
  7695. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7696. 1, &times, &pending)) {
  7697. goto exit_rsa_pub;
  7698. }
  7699. }
  7700. } /* for i */
  7701. RECORD_MULTI_VALUE_STATS();
  7702. } /* for times */
  7703. count += times;
  7704. } while (bench_stats_check(start)
  7705. #ifdef MULTI_VALUE_STATISTICS
  7706. || runs < minimum_runs
  7707. #endif
  7708. );
  7709. exit_rsa_pub:
  7710. bench_stats_asym_finish("RSA", (int)rsaKeySz, desc[1],
  7711. useDeviceID, count, start, ret);
  7712. #ifdef MULTI_VALUE_STATISTICS
  7713. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7714. #endif
  7715. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY */
  7716. }
  7717. else {
  7718. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  7719. /* begin RSA sign */
  7720. bench_stats_start(&count, &start);
  7721. do {
  7722. for (times = 0; times < ntimes || pending > 0; ) {
  7723. bench_async_poll(&pending);
  7724. /* while free pending slots in queue, submit ops */
  7725. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7726. if (bench_async_check(&ret,
  7727. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7728. 1, &times, ntimes, &pending)) {
  7729. ret = wc_RsaSSL_Sign(message, len, enc[i],
  7730. rsaKeySz/8, rsaKey[i], GLOBAL_RNG);
  7731. if (!bench_async_handle(&ret,
  7732. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7733. 1, &times, &pending)) {
  7734. goto exit_rsa_sign;
  7735. }
  7736. }
  7737. } /* for i */
  7738. RECORD_MULTI_VALUE_STATS();
  7739. } /* for times */
  7740. count += times;
  7741. } while (bench_stats_check(start)
  7742. #ifdef MULTI_VALUE_STATISTICS
  7743. || runs < minimum_runs
  7744. #endif
  7745. );
  7746. exit_rsa_sign:
  7747. bench_stats_asym_finish("RSA", (int)rsaKeySz, desc[4], useDeviceID,
  7748. count, start, ret);
  7749. #ifdef MULTI_VALUE_STATISTICS
  7750. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7751. #endif
  7752. if (ret < 0) {
  7753. goto exit;
  7754. }
  7755. RESET_MULTI_VALUE_STATS_VARS();
  7756. #endif /* !WOLFSSL_RSA_PUBLIC_ONLY && !WOLFSSL_RSA_VERIFY_ONLY */
  7757. /* capture resulting encrypt length */
  7758. idx = rsaKeySz/8;
  7759. /* begin RSA verify */
  7760. bench_stats_start(&count, &start);
  7761. do {
  7762. for (times = 0; times < ntimes || pending > 0; ) {
  7763. bench_async_poll(&pending);
  7764. /* while free pending slots in queue, submit ops */
  7765. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7766. if (bench_async_check(&ret,
  7767. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7768. 1, &times, ntimes, &pending)) {
  7769. #if !defined(WOLFSSL_RSA_VERIFY_INLINE) && \
  7770. !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  7771. ret = wc_RsaSSL_Verify(enc[i], idx, out[i],
  7772. rsaKeySz/8, rsaKey[i]);
  7773. #elif defined(USE_CERT_BUFFERS_2048)
  7774. XMEMCPY(enc[i], rsa_2048_sig, sizeof(rsa_2048_sig));
  7775. idx = sizeof(rsa_2048_sig);
  7776. out[i] = NULL;
  7777. ret = wc_RsaSSL_VerifyInline(enc[i], idx,
  7778. &out[i], rsaKey[i]);
  7779. if (ret > 0) {
  7780. ret = 0;
  7781. }
  7782. #elif defined(USE_CERT_BUFFERS_3072)
  7783. XMEMCPY(enc[i], rsa_3072_sig, sizeof(rsa_3072_sig));
  7784. idx = sizeof(rsa_3072_sig);
  7785. out[i] = NULL;
  7786. ret = wc_RsaSSL_VerifyInline(enc[i], idx,
  7787. &out[i], rsaKey[i]);
  7788. if (ret > 0)
  7789. ret = 0;
  7790. #endif
  7791. if (!bench_async_handle(&ret,
  7792. BENCH_ASYNC_GET_DEV(rsaKey[i]),
  7793. 1, &times, &pending)) {
  7794. goto exit_rsa_verifyinline;
  7795. }
  7796. }
  7797. } /* for i */
  7798. RECORD_MULTI_VALUE_STATS();
  7799. } /* for times */
  7800. count += times;
  7801. } while (bench_stats_check(start)
  7802. #ifdef MULTI_VALUE_STATISTICS
  7803. || runs < minimum_runs
  7804. #endif
  7805. );
  7806. exit_rsa_verifyinline:
  7807. bench_stats_asym_finish("RSA", (int)rsaKeySz, desc[5],
  7808. useDeviceID, count, start, ret);
  7809. #ifdef MULTI_VALUE_STATISTICS
  7810. bench_multi_value_stats(max, min, sum, squareSum, runs);
  7811. #endif
  7812. }
  7813. exit:
  7814. WC_FREE_HEAP_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  7815. #if !defined(WOLFSSL_RSA_VERIFY_INLINE) && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
  7816. WC_FREE_HEAP_ARRAY(out, BENCH_MAX_PENDING, HEAP_HINT);
  7817. #endif
  7818. #ifndef WOLFSSL_RSA_VERIFY_ONLY
  7819. WC_FREE_VAR(message, HEAP_HINT);
  7820. #endif
  7821. }
  7822. void bench_rsa(int useDeviceID)
  7823. {
  7824. int i;
  7825. WC_DECLARE_ARRAY(rsaKey, RsaKey, BENCH_MAX_PENDING,
  7826. sizeof(RsaKey), HEAP_HINT);
  7827. int ret = 0;
  7828. word32 rsaKeySz = 0;
  7829. const byte* tmp;
  7830. size_t bytes;
  7831. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  7832. word32 idx;
  7833. #endif
  7834. WC_CALLOC_ARRAY(rsaKey, RsaKey, BENCH_MAX_PENDING,
  7835. sizeof(RsaKey), HEAP_HINT);
  7836. #ifdef USE_CERT_BUFFERS_1024
  7837. tmp = rsa_key_der_1024;
  7838. bytes = (size_t)sizeof_rsa_key_der_1024;
  7839. rsaKeySz = 1024;
  7840. #elif defined(USE_CERT_BUFFERS_2048)
  7841. tmp = rsa_key_der_2048;
  7842. bytes = (size_t)sizeof_rsa_key_der_2048;
  7843. rsaKeySz = 2048;
  7844. #elif defined(USE_CERT_BUFFERS_3072)
  7845. tmp = rsa_key_der_3072;
  7846. bytes = (size_t)sizeof_rsa_key_der_3072;
  7847. rsaKeySz = 3072;
  7848. #elif defined(USE_CERT_BUFFERS_4096)
  7849. tmp = client_key_der_4096;
  7850. bytes = (size_t)sizeof_client_key_der_4096;
  7851. rsaKeySz = 4096;
  7852. #else
  7853. #error "need a cert buffer size"
  7854. #endif /* USE_CERT_BUFFERS */
  7855. /* init keys */
  7856. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7857. /* setup an async context for each key */
  7858. ret = wc_InitRsaKey_ex(rsaKey[i], HEAP_HINT,
  7859. useDeviceID ? devId : INVALID_DEVID);
  7860. if (ret < 0) {
  7861. goto exit;
  7862. }
  7863. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  7864. #ifdef WC_RSA_BLINDING
  7865. ret = wc_RsaSetRNG(rsaKey[i], &gRng);
  7866. if (ret != 0)
  7867. goto exit;
  7868. #endif
  7869. #endif
  7870. #if !defined(WOLFSSL_RSA_PUBLIC_ONLY) && !defined(WOLFSSL_RSA_VERIFY_ONLY)
  7871. /* decode the private key */
  7872. idx = 0;
  7873. if ((ret = wc_RsaPrivateKeyDecode(tmp, &idx,
  7874. rsaKey[i], (word32)bytes)) != 0) {
  7875. printf("wc_RsaPrivateKeyDecode failed! %d\n", ret);
  7876. goto exit;
  7877. }
  7878. #elif defined(WOLFSSL_PUBLIC_MP)
  7879. /* get offset to public portion of the RSA key */
  7880. #ifdef USE_CERT_BUFFERS_1024
  7881. bytes = 11;
  7882. #elif defined(USE_CERT_BUFFERS_2048) || defined(USE_CERT_BUFFERS_3072)
  7883. bytes = 12;
  7884. #endif
  7885. ret = mp_read_unsigned_bin(rsaKey[i].n, &tmp[bytes], rsaKeySz/8);
  7886. if (ret != 0) {
  7887. printf("wc_RsaPrivateKeyDecode failed! %d\n", ret);
  7888. goto exit;
  7889. }
  7890. ret = mp_set_int(rsaKey[i].e, WC_RSA_EXPONENT);
  7891. if (ret != 0) {
  7892. printf("wc_RsaPrivateKeyDecode failed! %d\n", ret);
  7893. goto exit;
  7894. }
  7895. #else
  7896. /* Note: To benchmark public only define WOLFSSL_PUBLIC_MP */
  7897. rsaKeySz = 0;
  7898. #endif
  7899. }
  7900. if (rsaKeySz > 0) {
  7901. bench_rsa_helper(useDeviceID, rsaKey, rsaKeySz);
  7902. }
  7903. (void)bytes;
  7904. (void)tmp;
  7905. exit:
  7906. /* cleanup */
  7907. if (WC_ARRAY_OK(rsaKey)) {
  7908. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7909. wc_FreeRsaKey(rsaKey[i]);
  7910. }
  7911. WC_FREE_ARRAY(rsaKey, BENCH_MAX_PENDING, HEAP_HINT);
  7912. }
  7913. }
  7914. #ifdef WOLFSSL_KEY_GEN
  7915. /* bench any size of RSA key */
  7916. void bench_rsa_key(int useDeviceID, word32 rsaKeySz)
  7917. {
  7918. int ret = 0, i, pending = 0;
  7919. WC_DECLARE_ARRAY(rsaKey, RsaKey, BENCH_MAX_PENDING,
  7920. sizeof(RsaKey), HEAP_HINT);
  7921. int isPending[BENCH_MAX_PENDING];
  7922. long exp = 65537L;
  7923. /* clear for done cleanup */
  7924. XMEMSET(isPending, 0, sizeof(isPending));
  7925. WC_CALLOC_ARRAY(rsaKey, RsaKey, BENCH_MAX_PENDING,
  7926. sizeof(RsaKey), HEAP_HINT);
  7927. /* init keys */
  7928. do {
  7929. pending = 0;
  7930. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7931. if (!isPending[i]) { /* if making the key is pending then just call
  7932. * wc_MakeRsaKey again */
  7933. /* setup an async context for each key */
  7934. if (wc_InitRsaKey_ex(rsaKey[i], HEAP_HINT,
  7935. useDeviceID ? devId : INVALID_DEVID) < 0) {
  7936. goto exit;
  7937. }
  7938. #ifdef WC_RSA_BLINDING
  7939. ret = wc_RsaSetRNG(rsaKey[i], &gRng);
  7940. if (ret != 0)
  7941. goto exit;
  7942. #endif
  7943. }
  7944. /* create the RSA key */
  7945. ret = wc_MakeRsaKey(rsaKey[i], (int)rsaKeySz, exp, &gRng);
  7946. if (ret == WC_PENDING_E) {
  7947. isPending[i] = 1;
  7948. pending = 1;
  7949. }
  7950. else if (ret != 0) {
  7951. printf("wc_MakeRsaKey failed! %d\n", ret);
  7952. goto exit;
  7953. }
  7954. } /* for i */
  7955. } while (pending > 0);
  7956. bench_rsa_helper(useDeviceID, rsaKey, rsaKeySz);
  7957. exit:
  7958. /* cleanup */
  7959. if (WC_ARRAY_OK(rsaKey)) {
  7960. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  7961. wc_FreeRsaKey(rsaKey[i]);
  7962. }
  7963. WC_FREE_ARRAY(rsaKey, BENCH_MAX_PENDING, HEAP_HINT);
  7964. }
  7965. }
  7966. #endif /* WOLFSSL_KEY_GEN */
  7967. #endif /* !NO_RSA */
  7968. #ifndef NO_DH
  7969. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048) && \
  7970. !defined(USE_CERT_BUFFERS_3072) && !defined(USE_CERT_BUFFERS_4096)
  7971. #if defined(WOLFSSL_MDK_SHELL)
  7972. static char *certDHname = "certs/dh2048.der";
  7973. /* set by shell command */
  7974. void set_Bench_DH_File(char * cert) { certDHname = cert ; }
  7975. #elif defined(FREESCALE_MQX)
  7976. static char *certDHname = "a:\\certs\\dh2048.der";
  7977. #elif defined(NO_ASN)
  7978. /* do nothing, but don't need a file */
  7979. #else
  7980. static const char *certDHname = "certs/dh2048.der";
  7981. #endif
  7982. #endif
  7983. #ifdef HAVE_FFDHE_4096
  7984. #define BENCH_DH_KEY_SIZE 512 /* for 4096 bit */
  7985. #else
  7986. #define BENCH_DH_KEY_SIZE 384 /* for 3072 bit */
  7987. #endif
  7988. #define BENCH_DH_PRIV_SIZE (BENCH_DH_KEY_SIZE/8)
  7989. void bench_dh(int useDeviceID)
  7990. {
  7991. int ret = 0, i;
  7992. int count = 0, times, pending = 0;
  7993. const byte* tmp = NULL;
  7994. double start = 0.0F;
  7995. WC_DECLARE_ARRAY(dhKey, DhKey, BENCH_MAX_PENDING,
  7996. sizeof(DhKey), HEAP_HINT);
  7997. int dhKeySz = BENCH_DH_KEY_SIZE * 8; /* used in printf */
  7998. const char**desc = bench_desc_words[lng_index];
  7999. #ifndef NO_ASN
  8000. size_t bytes = 0;
  8001. word32 idx;
  8002. #endif
  8003. word32 pubSz[BENCH_MAX_PENDING];
  8004. word32 privSz[BENCH_MAX_PENDING];
  8005. word32 pubSz2 = BENCH_DH_KEY_SIZE;
  8006. word32 privSz2 = BENCH_DH_PRIV_SIZE;
  8007. word32 agreeSz[BENCH_MAX_PENDING];
  8008. #if defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072) || defined(HAVE_FFDHE_4096)
  8009. #ifdef HAVE_PUBLIC_FFDHE
  8010. const DhParams *params = NULL;
  8011. #else
  8012. int paramName = 0;
  8013. #endif
  8014. #endif
  8015. DECLARE_MULTI_VALUE_STATS_VARS()
  8016. WC_DECLARE_ARRAY(pub, byte, BENCH_MAX_PENDING,
  8017. BENCH_DH_KEY_SIZE, HEAP_HINT);
  8018. WC_DECLARE_VAR(pub2, byte,
  8019. BENCH_DH_KEY_SIZE, HEAP_HINT);
  8020. WC_DECLARE_ARRAY(agree, byte, BENCH_MAX_PENDING,
  8021. BENCH_DH_KEY_SIZE, HEAP_HINT);
  8022. WC_DECLARE_ARRAY(priv, byte, BENCH_MAX_PENDING,
  8023. BENCH_DH_PRIV_SIZE, HEAP_HINT);
  8024. WC_DECLARE_VAR(priv2, byte,
  8025. BENCH_DH_PRIV_SIZE, HEAP_HINT);
  8026. /* old scan-build misfires -Wmaybe-uninitialized on these. */
  8027. XMEMSET(pub, 0, sizeof(pub));
  8028. XMEMSET(agree, 0, sizeof(agree));
  8029. XMEMSET(priv, 0, sizeof(priv));
  8030. WC_CALLOC_ARRAY(dhKey, DhKey, BENCH_MAX_PENDING,
  8031. sizeof(DhKey), HEAP_HINT);
  8032. WC_ALLOC_ARRAY(pub, byte,
  8033. BENCH_MAX_PENDING, BENCH_DH_KEY_SIZE, HEAP_HINT);
  8034. WC_ALLOC_ARRAY(agree, byte,
  8035. BENCH_MAX_PENDING, BENCH_DH_KEY_SIZE, HEAP_HINT);
  8036. WC_ALLOC_ARRAY(priv, byte,
  8037. BENCH_MAX_PENDING, BENCH_DH_PRIV_SIZE, HEAP_HINT);
  8038. WC_ALLOC_VAR(pub2, byte, BENCH_DH_KEY_SIZE, HEAP_HINT);
  8039. WC_ALLOC_VAR(priv2, byte, BENCH_DH_PRIV_SIZE, HEAP_HINT);
  8040. (void)tmp;
  8041. if (!use_ffdhe) {
  8042. #if defined(NO_ASN)
  8043. dhKeySz = 1024;
  8044. /* do nothing, but don't use default FILE */
  8045. #elif defined(USE_CERT_BUFFERS_1024)
  8046. tmp = dh_key_der_1024;
  8047. bytes = (size_t)sizeof_dh_key_der_1024;
  8048. dhKeySz = 1024;
  8049. #elif defined(USE_CERT_BUFFERS_2048)
  8050. tmp = dh_key_der_2048;
  8051. bytes = (size_t)sizeof_dh_key_der_2048;
  8052. dhKeySz = 2048;
  8053. #elif defined(USE_CERT_BUFFERS_3072)
  8054. tmp = dh_key_der_3072;
  8055. bytes = (size_t)sizeof_dh_key_der_3072;
  8056. dhKeySz = 3072;
  8057. #elif defined(USE_CERT_BUFFERS_4096)
  8058. tmp = dh_key_der_4096;
  8059. bytes = (size_t)sizeof_dh_key_der_4096;
  8060. dhKeySz = 4096;
  8061. #else
  8062. #error "need to define a cert buffer size"
  8063. #endif /* USE_CERT_BUFFERS */
  8064. }
  8065. #ifdef HAVE_FFDHE_2048
  8066. else if (use_ffdhe == 2048) {
  8067. #ifdef HAVE_PUBLIC_FFDHE
  8068. params = wc_Dh_ffdhe2048_Get();
  8069. #else
  8070. paramName = WC_FFDHE_2048;
  8071. #endif
  8072. dhKeySz = 2048;
  8073. }
  8074. #endif
  8075. #ifdef HAVE_FFDHE_3072
  8076. else if (use_ffdhe == 3072) {
  8077. #ifdef HAVE_PUBLIC_FFDHE
  8078. params = wc_Dh_ffdhe3072_Get();
  8079. #else
  8080. paramName = WC_FFDHE_3072;
  8081. #endif
  8082. dhKeySz = 3072;
  8083. }
  8084. #endif
  8085. #ifdef HAVE_FFDHE_4096
  8086. else if (use_ffdhe == 4096) {
  8087. #ifdef HAVE_PUBLIC_FFDHE
  8088. params = wc_Dh_ffdhe4096_Get();
  8089. #else
  8090. paramName = WC_FFDHE_4096;
  8091. #endif
  8092. dhKeySz = 4096;
  8093. }
  8094. #endif
  8095. /* init keys */
  8096. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  8097. /* setup an async context for each key */
  8098. ret = wc_InitDhKey_ex(dhKey[i], HEAP_HINT,
  8099. useDeviceID ? devId : INVALID_DEVID);
  8100. if (ret != 0)
  8101. goto exit;
  8102. /* setup key */
  8103. if (!use_ffdhe) {
  8104. #ifdef NO_ASN
  8105. ret = wc_DhSetKey(dhKey[i], dh_p,
  8106. sizeof(dh_p), dh_g, sizeof(dh_g));
  8107. #else
  8108. idx = 0;
  8109. ret = wc_DhKeyDecode(tmp, &idx, dhKey[i], (word32)bytes);
  8110. #endif
  8111. }
  8112. #if defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072)
  8113. #ifdef HAVE_PUBLIC_FFDHE
  8114. else if (params != NULL) {
  8115. ret = wc_DhSetKey(dhKey[i], params->p, params->p_len,
  8116. params->g, params->g_len);
  8117. }
  8118. #else
  8119. else if (paramName != 0) {
  8120. ret = wc_DhSetNamedKey(dhKey[i], paramName);
  8121. }
  8122. #endif
  8123. #endif
  8124. if (ret != 0) {
  8125. printf("DhKeyDecode failed %d, can't benchmark\n", ret);
  8126. goto exit;
  8127. }
  8128. }
  8129. /* Key Gen */
  8130. bench_stats_start(&count, &start);
  8131. PRIVATE_KEY_UNLOCK();
  8132. do {
  8133. /* while free pending slots in queue, submit ops */
  8134. for (times = 0; times < genTimes || pending > 0; ) {
  8135. bench_async_poll(&pending);
  8136. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  8137. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(dhKey[i]),
  8138. 0, &times, genTimes, &pending)) {
  8139. privSz[i] = BENCH_DH_PRIV_SIZE;
  8140. pubSz[i] = BENCH_DH_KEY_SIZE;
  8141. ret = wc_DhGenerateKeyPair(dhKey[i], &gRng,
  8142. priv[i], &privSz[i],
  8143. pub[i], &pubSz[i]);
  8144. if (!bench_async_handle(&ret,
  8145. BENCH_ASYNC_GET_DEV(dhKey[i]),
  8146. 0, &times, &pending)) {
  8147. goto exit_dh_gen;
  8148. }
  8149. }
  8150. } /* for i */
  8151. RECORD_MULTI_VALUE_STATS();
  8152. } /* for times */
  8153. count += times;
  8154. } while (bench_stats_check(start)
  8155. #ifdef MULTI_VALUE_STATISTICS
  8156. || runs < minimum_runs
  8157. #endif
  8158. );
  8159. PRIVATE_KEY_LOCK();
  8160. exit_dh_gen:
  8161. bench_stats_asym_finish("DH", dhKeySz, desc[2],
  8162. useDeviceID, count, start, ret);
  8163. #ifdef MULTI_VALUE_STATISTICS
  8164. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8165. #endif
  8166. if (ret < 0) {
  8167. goto exit;
  8168. }
  8169. RESET_MULTI_VALUE_STATS_VARS();
  8170. /* Generate key to use as other public */
  8171. PRIVATE_KEY_UNLOCK();
  8172. ret = wc_DhGenerateKeyPair(dhKey[0], &gRng,
  8173. priv2, &privSz2, pub2, &pubSz2);
  8174. PRIVATE_KEY_LOCK();
  8175. #ifdef WOLFSSL_ASYNC_CRYPT
  8176. ret = wc_AsyncWait(ret, &dhKey[0]->asyncDev, WC_ASYNC_FLAG_NONE);
  8177. #endif
  8178. /* Key Agree */
  8179. bench_stats_start(&count, &start);
  8180. PRIVATE_KEY_UNLOCK();
  8181. do {
  8182. for (times = 0; times < agreeTimes || pending > 0; ) {
  8183. bench_async_poll(&pending);
  8184. /* while free pending slots in queue, submit ops */
  8185. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  8186. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(dhKey[i]),
  8187. 0, &times, agreeTimes, &pending)) {
  8188. ret = wc_DhAgree(dhKey[i], agree[i], &agreeSz[i], priv[i],
  8189. privSz[i], pub2, pubSz2);
  8190. if (!bench_async_handle(&ret,
  8191. BENCH_ASYNC_GET_DEV(dhKey[i]), 0, &times, &pending)) {
  8192. goto exit;
  8193. }
  8194. }
  8195. } /* for i */
  8196. RECORD_MULTI_VALUE_STATS();
  8197. } /* for times */
  8198. count += times;
  8199. } while (bench_stats_check(start)
  8200. #ifdef MULTI_VALUE_STATISTICS
  8201. || runs < minimum_runs
  8202. #endif
  8203. );
  8204. PRIVATE_KEY_LOCK();
  8205. exit:
  8206. bench_stats_asym_finish("DH", dhKeySz, desc[3],
  8207. useDeviceID, count, start, ret);
  8208. #ifdef MULTI_VALUE_STATISTICS
  8209. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8210. #endif
  8211. /* cleanup */
  8212. if (WC_ARRAY_OK(dhKey)) {
  8213. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  8214. wc_FreeDhKey(dhKey[i]);
  8215. }
  8216. WC_FREE_ARRAY(dhKey, BENCH_MAX_PENDING, HEAP_HINT);
  8217. }
  8218. WC_FREE_ARRAY(pub, BENCH_MAX_PENDING, HEAP_HINT);
  8219. WC_FREE_VAR(pub2, HEAP_HINT);
  8220. WC_FREE_ARRAY(priv, BENCH_MAX_PENDING, HEAP_HINT);
  8221. WC_FREE_VAR(priv2, HEAP_HINT);
  8222. WC_FREE_ARRAY(agree, BENCH_MAX_PENDING, HEAP_HINT);
  8223. }
  8224. #endif /* !NO_DH */
  8225. #ifdef WOLFSSL_HAVE_KYBER
  8226. static void bench_kyber_keygen(int type, const char* name, int keySize,
  8227. KyberKey* key)
  8228. {
  8229. int ret = 0, times, count, pending = 0;
  8230. double start;
  8231. const char**desc = bench_desc_words[lng_index];
  8232. DECLARE_MULTI_VALUE_STATS_VARS()
  8233. /* KYBER Make Key */
  8234. bench_stats_start(&count, &start);
  8235. do {
  8236. /* while free pending slots in queue, submit ops */
  8237. for (times = 0; times < agreeTimes || pending > 0; times++) {
  8238. wc_KyberKey_Free(key);
  8239. ret = wc_KyberKey_Init(type, key, HEAP_HINT, INVALID_DEVID);
  8240. if (ret != 0)
  8241. goto exit;
  8242. #ifdef KYBER_NONDETERMINISTIC
  8243. ret = wc_KyberKey_MakeKey(key, &gRng);
  8244. #else
  8245. unsigned char rand[KYBER_MAKEKEY_RAND_SZ] = {0,};
  8246. ret = wc_KyberKey_MakeKeyWithRandom(key, rand, sizeof(rand));
  8247. #endif
  8248. if (ret != 0)
  8249. goto exit;
  8250. RECORD_MULTI_VALUE_STATS();
  8251. } /* for times */
  8252. count += times;
  8253. } while (bench_stats_check(start)
  8254. #ifdef MULTI_VALUE_STATISTICS
  8255. || runs < minimum_runs
  8256. #endif
  8257. );
  8258. exit:
  8259. bench_stats_asym_finish(name, keySize, desc[2], 0, count, start, ret);
  8260. #ifdef MULTI_VALUE_STATISTICS
  8261. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8262. #endif
  8263. }
  8264. static void bench_kyber_encap(const char* name, int keySize, KyberKey* key)
  8265. {
  8266. int ret = 0, times, count, pending = 0;
  8267. double start;
  8268. const char**desc = bench_desc_words[lng_index];
  8269. byte ct[KYBER_MAX_CIPHER_TEXT_SIZE];
  8270. byte ss[KYBER_SS_SZ];
  8271. word32 ctSz;
  8272. DECLARE_MULTI_VALUE_STATS_VARS()
  8273. ret = wc_KyberKey_CipherTextSize(key, &ctSz);
  8274. if (ret != 0) {
  8275. return;
  8276. }
  8277. /* KYBER Encapsulate */
  8278. bench_stats_start(&count, &start);
  8279. do {
  8280. /* while free pending slots in queue, submit ops */
  8281. for (times = 0; times < agreeTimes || pending > 0; times++) {
  8282. #ifdef KYBER_NONDETERMINISTIC
  8283. ret = wc_KyberKey_Encapsulate(key, ct, ss, &gRng);
  8284. #else
  8285. unsigned char rand[KYBER_ENC_RAND_SZ] = {0,};
  8286. ret = wc_KyberKey_EncapsulateWithRandom(key, ct, ss, rand,
  8287. sizeof(rand));
  8288. #endif
  8289. if (ret != 0)
  8290. goto exit_encap;
  8291. RECORD_MULTI_VALUE_STATS();
  8292. } /* for times */
  8293. count += times;
  8294. } while (bench_stats_check(start)
  8295. #ifdef MULTI_VALUE_STATISTICS
  8296. || runs < minimum_runs
  8297. #endif
  8298. );
  8299. exit_encap:
  8300. bench_stats_asym_finish(name, keySize, desc[9], 0, count, start, ret);
  8301. #ifdef MULTI_VALUE_STATISTICS
  8302. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8303. #endif
  8304. RESET_MULTI_VALUE_STATS_VARS();
  8305. /* KYBER Decapsulate */
  8306. bench_stats_start(&count, &start);
  8307. do {
  8308. /* while free pending slots in queue, submit ops */
  8309. for (times = 0; times < agreeTimes || pending > 0; times++) {
  8310. ret = wc_KyberKey_Decapsulate(key, ss, ct, ctSz);
  8311. if (ret != 0)
  8312. goto exit_decap;
  8313. RECORD_MULTI_VALUE_STATS();
  8314. } /* for times */
  8315. count += times;
  8316. } while (bench_stats_check(start)
  8317. #ifdef MULTI_VALUE_STATISTICS
  8318. || runs < minimum_runs
  8319. #endif
  8320. );
  8321. exit_decap:
  8322. bench_stats_asym_finish(name, keySize, desc[13], 0, count, start, ret);
  8323. #ifdef MULTI_VALUE_STATISTICS
  8324. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8325. #endif
  8326. }
  8327. void bench_kyber(int type)
  8328. {
  8329. KyberKey key;
  8330. const char* name = NULL;
  8331. int keySize = 0;
  8332. switch (type) {
  8333. #ifdef WOLFSSL_KYBER512
  8334. case KYBER512:
  8335. name = "KYBER512 ";
  8336. keySize = 128;
  8337. break;
  8338. #endif
  8339. #ifdef WOLFSSL_KYBER768
  8340. case KYBER768:
  8341. name = "KYBER768 ";
  8342. keySize = 192;
  8343. break;
  8344. #endif
  8345. #ifdef WOLFSSL_KYBER1024
  8346. case KYBER1024:
  8347. name = "KYBER1024";
  8348. keySize = 256;
  8349. break;
  8350. #endif
  8351. }
  8352. bench_kyber_keygen(type, name, keySize, &key);
  8353. bench_kyber_encap(name, keySize, &key);
  8354. wc_KyberKey_Free(&key);
  8355. }
  8356. #endif
  8357. #if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY)
  8358. /* WC_LMS_PARM_L2_H10_W2
  8359. * signature length: 9300 */
  8360. static const byte lms_priv_L2_H10_W2[64] =
  8361. {
  8362. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8363. 0x62,0x62,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  8364. 0xC7,0x74,0x25,0x5B,0x2C,0xE8,0xDA,0x53,
  8365. 0xF0,0x7C,0x04,0x3F,0x64,0x2D,0x26,0x2C,
  8366. 0x46,0x1D,0xC8,0x90,0x77,0x59,0xD6,0xC0,
  8367. 0x56,0x46,0x7D,0x97,0x64,0xF2,0xA3,0xA1,
  8368. 0xF8,0xD0,0x3B,0x5F,0xAC,0x40,0xB9,0x9E,
  8369. 0x83,0x67,0xBF,0x92,0x8D,0xFE,0x45,0x79
  8370. };
  8371. static const byte lms_pub_L2_H10_W2[60] =
  8372. {
  8373. 0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x06,
  8374. 0x00,0x00,0x00,0x02,0xF8,0xD0,0x3B,0x5F,
  8375. 0xAC,0x40,0xB9,0x9E,0x83,0x67,0xBF,0x92,
  8376. 0x8D,0xFE,0x45,0x79,0x41,0xBC,0x2A,0x3B,
  8377. 0x9F,0xC0,0x11,0x12,0x93,0xF0,0x5A,0xA5,
  8378. 0xC1,0x88,0x29,0x79,0x6C,0x3E,0x0A,0x0F,
  8379. 0xEC,0x3B,0x3E,0xE4,0x38,0xD3,0xD2,0x34,
  8380. 0x7F,0xC8,0x91,0xB0
  8381. };
  8382. /* WC_LMS_PARM_L2_H10_W4
  8383. * signature length: 5076 */
  8384. static const byte lms_priv_L2_H10_W4[64] =
  8385. {
  8386. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8387. 0x63,0x63,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  8388. 0xAE,0x28,0x87,0x19,0x4F,0x4B,0x68,0x61,
  8389. 0x93,0x9A,0xC7,0x0E,0x33,0xB8,0xCE,0x96,
  8390. 0x66,0x0D,0xC7,0xB1,0xFA,0x94,0x80,0xA2,
  8391. 0x28,0x9B,0xCF,0xE2,0x08,0xB5,0x25,0xAC,
  8392. 0xFB,0xB8,0x65,0x5E,0xD1,0xCC,0x31,0xDA,
  8393. 0x2E,0x49,0x3A,0xEE,0xAF,0x63,0x70,0x5E
  8394. };
  8395. static const byte lms_pub_L2_H10_W4[60] =
  8396. {
  8397. 0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x06,
  8398. 0x00,0x00,0x00,0x03,0xFB,0xB8,0x65,0x5E,
  8399. 0xD1,0xCC,0x31,0xDA,0x2E,0x49,0x3A,0xEE,
  8400. 0xAF,0x63,0x70,0x5E,0xA2,0xD5,0xB6,0x15,
  8401. 0x33,0x8C,0x9B,0xE9,0xE1,0x91,0x40,0x1A,
  8402. 0x12,0xE0,0xD7,0xBD,0xE4,0xE0,0x76,0xF5,
  8403. 0x04,0x90,0x76,0xA5,0x9A,0xA7,0x4E,0xFE,
  8404. 0x6B,0x9A,0xD3,0x14
  8405. };
  8406. /* WC_LMS_PARM_L3_H5_W4
  8407. * signature length: 7160 */
  8408. static const byte lms_priv_L3_H5_W4[64] =
  8409. {
  8410. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8411. 0x53,0x53,0x53,0xFF,0xFF,0xFF,0xFF,0xFF,
  8412. 0x38,0xD1,0xBE,0x68,0xD1,0x93,0xE1,0x14,
  8413. 0x6C,0x8B,0xED,0xE2,0x25,0x88,0xED,0xAC,
  8414. 0x57,0xBD,0x87,0x9F,0x54,0xF3,0x58,0xD9,
  8415. 0x4D,0xF5,0x6A,0xBD,0x71,0x99,0x6A,0x28,
  8416. 0x2F,0xE1,0xFC,0xD1,0xD1,0x0C,0x7C,0xF8,
  8417. 0xB4,0xDC,0xDF,0x7F,0x14,0x1A,0x7B,0x50
  8418. };
  8419. static const byte lms_pub_L3_H5_W4[60] =
  8420. {
  8421. 0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x05,
  8422. 0x00,0x00,0x00,0x03,0x2F,0xE1,0xFC,0xD1,
  8423. 0xD1,0x0C,0x7C,0xF8,0xB4,0xDC,0xDF,0x7F,
  8424. 0x14,0x1A,0x7B,0x50,0x8E,0x3A,0xD4,0x05,
  8425. 0x0C,0x95,0x59,0xA0,0xCA,0x7A,0xD8,0xD6,
  8426. 0x5D,0xBD,0x42,0xBB,0xD5,0x82,0xB8,0x9C,
  8427. 0x52,0x37,0xB7,0x45,0x03,0xC2,0x06,0xCE,
  8428. 0xAB,0x4B,0x51,0x39
  8429. };
  8430. /* WC_LMS_PARM_L3_H5_W8
  8431. * signature length: 3992 */
  8432. static const byte lms_priv_L3_H5_W8[64] =
  8433. {
  8434. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8435. 0x54,0x54,0x54,0xFF,0xFF,0xFF,0xFF,0xFF,
  8436. 0xA5,0x46,0x97,0x0C,0xA1,0x3C,0xEA,0x17,
  8437. 0x5C,0x9D,0x59,0xF4,0x0E,0x27,0x37,0xF3,
  8438. 0x6A,0x1C,0xF7,0x29,0x4A,0xCC,0xCD,0x7B,
  8439. 0x4F,0xE7,0x37,0x6E,0xEF,0xC1,0xBD,0xBD,
  8440. 0x04,0x5D,0x8E,0xDD,0xAA,0x47,0xCC,0xE6,
  8441. 0xCE,0x78,0x46,0x20,0x41,0x87,0xE0,0x85
  8442. };
  8443. static const byte lms_pub_L3_H5_W8[60] =
  8444. {
  8445. 0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x05,
  8446. 0x00,0x00,0x00,0x04,0x04,0x5D,0x8E,0xDD,
  8447. 0xAA,0x47,0xCC,0xE6,0xCE,0x78,0x46,0x20,
  8448. 0x41,0x87,0xE0,0x85,0x0D,0x2C,0x46,0xB9,
  8449. 0x39,0x8C,0xA3,0x92,0x4F,0xCE,0x50,0x96,
  8450. 0x90,0x9C,0xF3,0x36,0x2E,0x09,0x15,0x3B,
  8451. 0x4B,0x34,0x17,0xE7,0xE2,0x55,0xFC,0x5B,
  8452. 0x83,0xAB,0x43,0xAF
  8453. };
  8454. /* WC_LMS_PARM_L3_H10_W4
  8455. * signature length: 7640 */
  8456. static const byte lms_priv_L3_H10_W4[64] =
  8457. {
  8458. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8459. 0x63,0x63,0x63,0xFF,0xFF,0xFF,0xFF,0xFF,
  8460. 0xDF,0x98,0xAB,0xEC,0xFE,0x13,0x9F,0xF8,
  8461. 0xD7,0x2B,0x4F,0x4C,0x79,0x34,0xB8,0x89,
  8462. 0x24,0x6B,0x26,0x7D,0x7A,0x2E,0xA2,0xCB,
  8463. 0x82,0x75,0x4E,0x96,0x54,0x49,0xED,0xA0,
  8464. 0xAF,0xC7,0xA5,0xEE,0x8A,0xA2,0x83,0x99,
  8465. 0x4B,0x18,0x59,0x2B,0x66,0xC0,0x32,0xDB
  8466. };
  8467. static const byte lms_pub_L3_H10_W4[60] =
  8468. {
  8469. 0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x06,
  8470. 0x00,0x00,0x00,0x03,0xAF,0xC7,0xA5,0xEE,
  8471. 0x8A,0xA2,0x83,0x99,0x4B,0x18,0x59,0x2B,
  8472. 0x66,0xC0,0x32,0xDB,0xC4,0x18,0xEB,0x11,
  8473. 0x17,0x7D,0xAA,0x93,0xFD,0xA0,0x70,0x4D,
  8474. 0x68,0x4B,0x63,0x8F,0xC2,0xE7,0xCA,0x34,
  8475. 0x14,0x31,0x0D,0xAA,0x18,0xBF,0x9B,0x32,
  8476. 0x8D,0x78,0xD5,0xA8
  8477. };
  8478. /* WC_LMS_PARM_L4_H5_W8
  8479. * signature length: 5340 */
  8480. static const byte lms_priv_L4_H5_W8[64] =
  8481. {
  8482. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  8483. 0x54,0x54,0x54,0x54,0xFF,0xFF,0xFF,0xFF,
  8484. 0x46,0x8F,0x2A,0x4A,0x14,0x26,0xF0,0x89,
  8485. 0xFE,0xED,0x66,0x0F,0x73,0x69,0xB1,0x4C,
  8486. 0x47,0xA1,0x35,0x9F,0x7B,0xBA,0x08,0x03,
  8487. 0xEE,0xA2,0xEB,0xAD,0xB4,0x82,0x52,0x1F,
  8488. 0xFD,0x9B,0x22,0x82,0x42,0x1A,0x96,0x1E,
  8489. 0xE4,0xA1,0x9C,0x33,0xED,0xE6,0x9F,0xAB
  8490. };
  8491. static const byte lms_pub_L4_H5_W8[60] =
  8492. {
  8493. 0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x05,
  8494. 0x00,0x00,0x00,0x04,0xFD,0x9B,0x22,0x82,
  8495. 0x42,0x1A,0x96,0x1E,0xE4,0xA1,0x9C,0x33,
  8496. 0xED,0xE6,0x9F,0xAB,0x6B,0x47,0x05,0x5B,
  8497. 0xA7,0xAD,0xF6,0x88,0xA5,0x4F,0xCD,0xF1,
  8498. 0xDA,0x29,0x67,0xC3,0x7F,0x2C,0x11,0xFE,
  8499. 0x85,0x1A,0x7A,0xD8,0xD5,0x46,0x74,0x3B,
  8500. 0x74,0x24,0x12,0xC8
  8501. };
  8502. static int lms_write_key_mem(const byte * priv, word32 privSz, void *context)
  8503. {
  8504. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  8505. * BE USED FOR TESTING PURPOSES! Production applications should
  8506. * write only to non-volatile storage. */
  8507. XMEMCPY(context, priv, privSz);
  8508. return WC_LMS_RC_SAVED_TO_NV_MEMORY;
  8509. }
  8510. static int lms_read_key_mem(byte * priv, word32 privSz, void *context)
  8511. {
  8512. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  8513. * BE USED FOR TESTING PURPOSES! */
  8514. XMEMCPY(priv, context, privSz);
  8515. return WC_LMS_RC_READ_TO_MEMORY;
  8516. }
  8517. static void bench_lms_sign_verify(enum wc_LmsParm parm)
  8518. {
  8519. LmsKey key;
  8520. int ret = 0;
  8521. const char * msg = TEST_STRING;
  8522. word32 msgSz = TEST_STRING_SZ;
  8523. byte * sig = NULL;
  8524. word32 sigSz = 0;
  8525. word32 privLen = 0;
  8526. int loaded = 0;
  8527. int times = 0;
  8528. int count = 0;
  8529. double start = 0.0F;
  8530. byte priv[HSS_MAX_PRIVATE_KEY_LEN];
  8531. const char * str = wc_LmsKey_ParmToStr(parm);
  8532. ret = wc_LmsKey_Init(&key, NULL, INVALID_DEVID);
  8533. if (ret) {
  8534. printf("wc_LmsKey_Init failed: %d\n", ret);
  8535. goto exit_lms_sign_verify;
  8536. }
  8537. ret = wc_LmsKey_SetLmsParm(&key, parm);
  8538. if (ret) {
  8539. printf("wc_LmsKey_SetLmsParm failed: %d\n", ret);
  8540. goto exit_lms_sign_verify;
  8541. }
  8542. switch (parm) {
  8543. case WC_LMS_PARM_L2_H10_W2:
  8544. XMEMCPY(priv, lms_priv_L2_H10_W2, sizeof(lms_priv_L2_H10_W2));
  8545. XMEMCPY(key.pub, lms_pub_L2_H10_W2, sizeof(lms_pub_L2_H10_W2));
  8546. break;
  8547. case WC_LMS_PARM_L2_H10_W4:
  8548. XMEMCPY(priv, lms_priv_L2_H10_W4, sizeof(lms_priv_L2_H10_W4));
  8549. XMEMCPY(key.pub, lms_pub_L2_H10_W4, sizeof(lms_pub_L2_H10_W4));
  8550. break;
  8551. case WC_LMS_PARM_L3_H5_W4:
  8552. XMEMCPY(priv, lms_priv_L3_H5_W4, sizeof(lms_priv_L3_H5_W4));
  8553. XMEMCPY(key.pub, lms_pub_L3_H5_W4, sizeof(lms_pub_L3_H5_W4));
  8554. break;
  8555. case WC_LMS_PARM_L3_H5_W8:
  8556. XMEMCPY(priv, lms_priv_L3_H5_W8, sizeof(lms_priv_L3_H5_W8));
  8557. XMEMCPY(key.pub, lms_pub_L3_H5_W8, sizeof(lms_pub_L3_H5_W8));
  8558. break;
  8559. case WC_LMS_PARM_L3_H10_W4:
  8560. XMEMCPY(priv, lms_priv_L3_H10_W4, sizeof(lms_priv_L3_H10_W4));
  8561. XMEMCPY(key.pub, lms_pub_L3_H10_W4, sizeof(lms_pub_L3_H10_W4));
  8562. break;
  8563. case WC_LMS_PARM_L4_H5_W8:
  8564. XMEMCPY(priv, lms_priv_L4_H5_W8, sizeof(lms_priv_L4_H5_W8));
  8565. XMEMCPY(key.pub, lms_pub_L4_H5_W8, sizeof(lms_pub_L4_H5_W8));
  8566. break;
  8567. case WC_LMS_PARM_NONE:
  8568. case WC_LMS_PARM_L1_H15_W2:
  8569. case WC_LMS_PARM_L1_H15_W4:
  8570. case WC_LMS_PARM_L2_H10_W8:
  8571. case WC_LMS_PARM_L3_H5_W2:
  8572. printf("bench_lms_sign_verify: unsupported benchmark option: %d\n",
  8573. parm);
  8574. goto exit_lms_sign_verify;
  8575. }
  8576. ret = wc_LmsKey_SetWriteCb(&key, lms_write_key_mem);
  8577. if (ret) {
  8578. fprintf(stderr, "error: wc_LmsKey_SetWriteCb failed: %d\n", ret);
  8579. goto exit_lms_sign_verify;
  8580. }
  8581. ret = wc_LmsKey_SetReadCb(&key, lms_read_key_mem);
  8582. if (ret) {
  8583. fprintf(stderr, "error: wc_LmsKey_SetReadCb failed: %d\n", ret);
  8584. goto exit_lms_sign_verify;
  8585. }
  8586. ret = wc_LmsKey_SetContext(&key, (void *) priv);
  8587. if (ret) {
  8588. fprintf(stderr, "error: wc_LmsKey_SetContext failed: %d\n", ret);
  8589. goto exit_lms_sign_verify;
  8590. }
  8591. /* Even with saved priv/pub keys, we must still reload the private
  8592. * key before using it. Reloading the private key is the bottleneck
  8593. * for larger heights. Only print load time in debug builds. */
  8594. #if defined(DEBUG_WOLFSSL)
  8595. bench_stats_start(&count, &start);
  8596. #endif /* if defined DEBUG_WOLFSSL*/
  8597. ret = wc_LmsKey_Reload(&key);
  8598. if (ret) {
  8599. printf("wc_LmsKey_Reload failed: %d\n", ret);
  8600. goto exit_lms_sign_verify;
  8601. }
  8602. count +=1;
  8603. ret = wc_LmsKey_GetSigLen(&key, &sigSz);
  8604. if (ret) {
  8605. printf("wc_LmsKey_GetSigLen failed: %d\n", ret);
  8606. goto exit_lms_sign_verify;
  8607. }
  8608. ret = wc_LmsKey_GetPrivLen(&key, &privLen);
  8609. if (ret) {
  8610. printf("wc_LmsKey_GetPrivLen failed: %d\n", ret);
  8611. goto exit_lms_sign_verify;
  8612. }
  8613. #if defined(DEBUG_WOLFSSL)
  8614. bench_stats_check(start);
  8615. bench_stats_asym_finish(str, (int)privLen, "load", 0,
  8616. count, start, ret);
  8617. #endif /* if defined DEBUG_WOLFSSL*/
  8618. loaded = 1;
  8619. sig = XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8620. if (sig == NULL) {
  8621. printf("bench_lms_sign_verify malloc failed\n");
  8622. goto exit_lms_sign_verify;
  8623. }
  8624. count = 0;
  8625. bench_stats_start(&count, &start);
  8626. do {
  8627. /* LMS is stateful. Async queuing not practical. */
  8628. for (times = 0; times < ntimes; ++times) {
  8629. ret = wc_LmsKey_Sign(&key, sig, &sigSz, (byte *) msg, msgSz);
  8630. if (ret) {
  8631. printf("wc_LmsKey_Sign failed: %d\n", ret);
  8632. goto exit_lms_sign_verify;
  8633. }
  8634. RECORD_MULTI_VALUE_STATS();
  8635. }
  8636. count += times;
  8637. } while (bench_stats_check(start)
  8638. #ifdef MULTI_VALUE_STATISTICS
  8639. || runs < minimum_runs
  8640. #endif
  8641. );
  8642. bench_stats_asym_finish(str, (int)sigSz, "sign", 0,
  8643. count, start, ret);
  8644. #ifdef MULTI_VALUE_STATISTICS
  8645. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8646. #endif
  8647. RESET_MULTI_VALUE_STATS_VARS();
  8648. count = 0;
  8649. bench_stats_start(&count, &start);
  8650. do {
  8651. /* LMS is stateful. Async queuing not practical. */
  8652. for (times = 0; times < ntimes; ++times) {
  8653. ret = wc_LmsKey_Verify(&key, sig, sigSz, (byte *) msg, msgSz);
  8654. if (ret) {
  8655. printf("wc_LmsKey_Verify failed: %d\n", ret);
  8656. goto exit_lms_sign_verify;
  8657. }
  8658. RECORD_MULTI_VALUE_STATS();
  8659. }
  8660. count += times;
  8661. } while (bench_stats_check(start)
  8662. #ifdef MULTI_VALUE_STATISTICS
  8663. || runs < minimum_runs
  8664. #endif
  8665. );
  8666. exit_lms_sign_verify:
  8667. bench_stats_asym_finish(str, (int)sigSz, "verify", 0,
  8668. count, start, ret);
  8669. #ifdef MULTI_VALUE_STATISTICS
  8670. bench_multi_value_stats(max, min, sum, squareSum, runs);
  8671. #endif
  8672. if (loaded) {
  8673. wc_LmsKey_Free(&key);
  8674. loaded = 0;
  8675. }
  8676. if (sig != NULL) {
  8677. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8678. sig = NULL;
  8679. }
  8680. return;
  8681. }
  8682. void bench_lms(void)
  8683. {
  8684. bench_lms_sign_verify(WC_LMS_PARM_L2_H10_W2);
  8685. bench_lms_sign_verify(WC_LMS_PARM_L2_H10_W4);
  8686. bench_lms_sign_verify(WC_LMS_PARM_L3_H5_W4);
  8687. bench_lms_sign_verify(WC_LMS_PARM_L3_H5_W8);
  8688. bench_lms_sign_verify(WC_LMS_PARM_L3_H10_W4);
  8689. bench_lms_sign_verify(WC_LMS_PARM_L4_H5_W8);
  8690. return;
  8691. }
  8692. #endif /* if defined(WOLFSSL_HAVE_LMS) && !defined(WOLFSSL_LMS_VERIFY_ONLY) */
  8693. #if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY)
  8694. static enum wc_XmssRc xmss_write_key_mem(const byte * priv, word32 privSz,
  8695. void *context)
  8696. {
  8697. /* WARNING: THIS IS AN INSECURE WRITE CALLBACK THAT SHOULD ONLY
  8698. * BE USED FOR TESTING PURPOSES! Production applications should
  8699. * write only to non-volatile storage. */
  8700. XMEMCPY(context, priv, privSz);
  8701. return WC_XMSS_RC_SAVED_TO_NV_MEMORY;
  8702. }
  8703. static enum wc_XmssRc xmss_read_key_mem(byte * priv, word32 privSz,
  8704. void *context)
  8705. {
  8706. /* WARNING: THIS IS AN INSECURE READ CALLBACK THAT SHOULD ONLY
  8707. * BE USED FOR TESTING PURPOSES! */
  8708. XMEMCPY(priv, context, privSz);
  8709. return WC_XMSS_RC_READ_TO_MEMORY;
  8710. }
  8711. static void bench_xmss_sign_verify(const char * params)
  8712. {
  8713. WC_RNG rng;
  8714. XmssKey key;
  8715. word32 pkSz = 0;
  8716. word32 skSz = 0;
  8717. int freeRng = 0;
  8718. int freeKey = 0;
  8719. unsigned char * sk = NULL;
  8720. const char * msg = "XMSS post quantum signature test";
  8721. word32 msgSz = (word32) XSTRLEN(msg);
  8722. int ret = 0;
  8723. byte * sig = NULL;
  8724. word32 sigSz = 0;
  8725. int times = 0;
  8726. int count = 0;
  8727. double start = 0.0F;
  8728. #ifndef HAVE_FIPS
  8729. ret = wc_InitRng_ex(&rng, HEAP_HINT, INVALID_DEVID);
  8730. #else
  8731. ret = wc_InitRng(&rng);
  8732. #endif
  8733. if (ret != 0) {
  8734. fprintf(stderr, "error: wc_InitRng failed: %d\n", ret);
  8735. goto exit_xmss_sign_verify;
  8736. }
  8737. freeRng = 1;
  8738. ret = wc_XmssKey_Init(&key, NULL, INVALID_DEVID);
  8739. if (ret != 0) {
  8740. fprintf(stderr, "wc_XmssKey_Init failed: %d\n", ret);
  8741. goto exit_xmss_sign_verify;
  8742. }
  8743. ret = wc_XmssKey_SetParamStr(&key, params);
  8744. if (ret != 0) {
  8745. fprintf(stderr, "wc_XmssKey_SetParamStr failed: %d\n", ret);
  8746. goto exit_xmss_sign_verify;
  8747. }
  8748. ret = wc_XmssKey_GetPubLen(&key, &pkSz);
  8749. if (ret != 0) {
  8750. fprintf(stderr, "wc_XmssKey_GetPubLen failed: %d\n", ret);
  8751. goto exit_xmss_sign_verify;
  8752. }
  8753. #ifndef WOLFSSL_WC_XMSS
  8754. if (pkSz != XMSS_SHA256_PUBLEN) {
  8755. fprintf(stderr, "error: xmss pub len: got %u, expected %d\n", pkSz,
  8756. XMSS_SHA256_PUBLEN);
  8757. goto exit_xmss_sign_verify;
  8758. }
  8759. #endif
  8760. ret = wc_XmssKey_GetPrivLen(&key, &skSz);
  8761. if (ret != 0 || skSz <= 0) {
  8762. fprintf(stderr, "error: wc_XmssKey_GetPrivLen failed\n");
  8763. goto exit_xmss_sign_verify;
  8764. }
  8765. ret = wc_XmssKey_GetSigLen(&key, &sigSz);
  8766. if (ret != 0 || sigSz <= 0) {
  8767. fprintf(stderr, "error: wc_XmssKey_GetSigLen failed\n");
  8768. goto exit_xmss_sign_verify;
  8769. }
  8770. /* Allocate secret keys.*/
  8771. sk = (unsigned char *)XMALLOC(skSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8772. if (sk == NULL) {
  8773. fprintf(stderr, "error: allocate xmss sk failed\n");
  8774. goto exit_xmss_sign_verify;
  8775. }
  8776. /* Allocate signature array. */
  8777. sig = (byte *)XMALLOC(sigSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8778. if (sig == NULL) {
  8779. fprintf(stderr, "error: allocate xmss sig failed\n");
  8780. goto exit_xmss_sign_verify;
  8781. }
  8782. ret = wc_XmssKey_SetWriteCb(&key, xmss_write_key_mem);
  8783. if (ret != 0) {
  8784. fprintf(stderr, "error: wc_XmssKey_SetWriteCb failed: %d\n", ret);
  8785. goto exit_xmss_sign_verify;
  8786. }
  8787. ret = wc_XmssKey_SetReadCb(&key, xmss_read_key_mem);
  8788. if (ret != 0) {
  8789. fprintf(stderr, "error: wc_XmssKey_SetReadCb failed: %d\n", ret);
  8790. goto exit_xmss_sign_verify;
  8791. }
  8792. ret = wc_XmssKey_SetContext(&key, (void *)sk);
  8793. if (ret != 0) {
  8794. fprintf(stderr, "error: wc_XmssKey_SetContext failed: %d\n", ret);
  8795. goto exit_xmss_sign_verify;
  8796. }
  8797. #if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_DEBUG_NONBLOCK)
  8798. fprintf(stderr, "params: %s\n", params);
  8799. fprintf(stderr, "pkSz: %d\n", pkSz);
  8800. fprintf(stderr, "skSz: %d\n", skSz);
  8801. fprintf(stderr, "sigSz: %d\n", sigSz);
  8802. #endif
  8803. /* Making the private key is the bottleneck for larger heights. */
  8804. count = 0;
  8805. bench_stats_start(&count, &start);
  8806. ret = wc_XmssKey_MakeKey(&key, &rng);
  8807. if (ret != 0) {
  8808. printf("wc_XmssKey_MakeKey failed: %d\n", ret);
  8809. goto exit_xmss_sign_verify;
  8810. }
  8811. /* Can only do one at a time - state changes after make key. */
  8812. count +=1;
  8813. bench_stats_check(start);
  8814. bench_stats_asym_finish(params, (int)skSz, "gen", 0, count, start, ret);
  8815. freeKey = 1;
  8816. count = 0;
  8817. bench_stats_start(&count, &start);
  8818. do {
  8819. /* XMSS is stateful. Async queuing not practical. */
  8820. #ifndef WOLFSSL_WC_XMSS_SMALL
  8821. for (times = 0; times < ntimes; ++times)
  8822. #else
  8823. for (times = 0; times < 1; ++times)
  8824. #endif
  8825. {
  8826. if (!wc_XmssKey_SigsLeft(&key))
  8827. break;
  8828. ret = wc_XmssKey_Sign(&key, sig, &sigSz, (byte *) msg, msgSz);
  8829. if (ret) {
  8830. printf("wc_XmssKey_Sign failed: %d\n", ret);
  8831. goto exit_xmss_sign_verify;
  8832. }
  8833. }
  8834. count += times;
  8835. } while (wc_XmssKey_SigsLeft(&key) && bench_stats_check(start));
  8836. bench_stats_asym_finish(params, (int)sigSz, "sign", 0, count, start, ret);
  8837. count = 0;
  8838. bench_stats_start(&count, &start);
  8839. do {
  8840. /* XMSS is stateful. Async queuing not practical. */
  8841. for (times = 0; times < ntimes; ++times) {
  8842. ret = wc_XmssKey_Verify(&key, sig, sigSz, (byte *) msg, msgSz);
  8843. if (ret) {
  8844. printf("wc_XmssKey_Verify failed: %d\n", ret);
  8845. goto exit_xmss_sign_verify;
  8846. }
  8847. }
  8848. count += times;
  8849. } while (bench_stats_check(start));
  8850. exit_xmss_sign_verify:
  8851. bench_stats_asym_finish(params, (int)sigSz, "verify", 0, count, start, ret);
  8852. /* Cleanup everything. */
  8853. if (sig != NULL) {
  8854. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8855. sig = NULL;
  8856. }
  8857. if (sk != NULL) {
  8858. XFREE(sk, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8859. sk = NULL;
  8860. }
  8861. if (freeRng) {
  8862. wc_FreeRng(&rng);
  8863. freeRng = 0;
  8864. }
  8865. if (freeKey) {
  8866. wc_XmssKey_Free(&key);
  8867. freeKey = 0;
  8868. }
  8869. return;
  8870. }
  8871. void bench_xmss(int hash)
  8872. {
  8873. /* All NIST SP 800-208 approved SHA256 XMSS/XMSS^MT parameter
  8874. * sets.
  8875. *
  8876. * Note: not testing "XMSS-SHA2_16_256", "XMSS-SHA2_20_256",
  8877. * and "XMSSMT-SHA2_60/3_256", because their keygen can be
  8878. * very slow, their signatures and private keys quite large,
  8879. * and xmss private keys are not portable across different
  8880. * XMSS/XMSS^MT implementations.
  8881. *
  8882. * The bottleneck in key generation is the height of the first
  8883. * level tree (or h/d).
  8884. *
  8885. * h is the total height of the hyper tree, and d the number of
  8886. * trees.
  8887. */
  8888. /* h/d h d */
  8889. #ifdef WC_XMSS_SHA256
  8890. if (hash == WC_HASH_TYPE_SHA256) {
  8891. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  8892. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  8893. bench_xmss_sign_verify("XMSS-SHA2_10_256"); /* 10 10 1 */
  8894. #endif
  8895. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  8896. #ifdef BENCH_XMSS_SLOW_KEYGEN
  8897. bench_xmss_sign_verify("XMSS-SHA2_16_256"); /* 16 16 1 */
  8898. #endif
  8899. #endif
  8900. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  8901. #ifdef BENCH_XMSS_SLOW_KEYGEN
  8902. bench_xmss_sign_verify("XMSS-SHA2_20_256"); /* 20 20 1 */
  8903. #endif
  8904. #endif
  8905. #endif /* HASH_SIZE 256 */
  8906. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 192 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 192
  8907. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  8908. bench_xmss_sign_verify("XMSS-SHA2_10_192"); /* 10 10 1 */
  8909. #endif
  8910. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  8911. #ifdef BENCH_XMSS_SLOW_KEYGEN
  8912. bench_xmss_sign_verify("XMSS-SHA2_16_192"); /* 16 16 1 */
  8913. #endif
  8914. #endif
  8915. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  8916. #ifdef BENCH_XMSS_SLOW_KEYGEN
  8917. bench_xmss_sign_verify("XMSS-SHA2_20_192"); /* 20 20 1 */
  8918. #endif
  8919. #endif
  8920. #endif /* HASH_SIZE 192 */
  8921. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  8922. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  8923. bench_xmss_sign_verify("XMSSMT-SHA2_20/2_256"); /* 10 20 2 */
  8924. bench_xmss_sign_verify("XMSSMT-SHA2_20/4_256"); /* 5 20 4 */
  8925. #endif
  8926. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  8927. #ifdef BENCH_XMSS_SLOW_KEYGEN
  8928. bench_xmss_sign_verify("XMSSMT-SHA2_40/2_256"); /* 20 40 4 */
  8929. #endif
  8930. bench_xmss_sign_verify("XMSSMT-SHA2_40/4_256"); /* 10 40 4 */
  8931. bench_xmss_sign_verify("XMSSMT-SHA2_40/8_256"); /* 5 40 8 */
  8932. #endif
  8933. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  8934. #ifdef BENCH_XMSS_SLOW_KEYGEN
  8935. bench_xmss_sign_verify("XMSSMT-SHA2_60/3_256"); /* 20 60 3 */
  8936. #endif
  8937. bench_xmss_sign_verify("XMSSMT-SHA2_60/6_256"); /* 10 60 6 */
  8938. bench_xmss_sign_verify("XMSSMT-SHA2_60/12_256"); /* 5 60 12 */
  8939. #endif
  8940. #endif /* HASH_SIZE 256 */
  8941. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 192 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 192
  8942. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  8943. bench_xmss_sign_verify("XMSSMT-SHA2_20/2_192"); /* 10 20 2 */
  8944. bench_xmss_sign_verify("XMSSMT-SHA2_20/4_192"); /* 5 20 4 */
  8945. #endif
  8946. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  8947. #ifdef BENCH_XMSS_SLOW_KEYGEN
  8948. bench_xmss_sign_verify("XMSSMT-SHA2_40/2_192"); /* 20 40 4 */
  8949. #endif
  8950. bench_xmss_sign_verify("XMSSMT-SHA2_40/4_192"); /* 10 40 4 */
  8951. bench_xmss_sign_verify("XMSSMT-SHA2_40/8_192"); /* 5 40 8 */
  8952. #endif
  8953. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  8954. #ifdef BENCH_XMSS_SLOW_KEYGEN
  8955. bench_xmss_sign_verify("XMSSMT-SHA2_60/3_192"); /* 20 60 3 */
  8956. #endif
  8957. bench_xmss_sign_verify("XMSSMT-SHA2_60/6_192"); /* 10 60 6 */
  8958. bench_xmss_sign_verify("XMSSMT-SHA2_60/12_192"); /* 5 60 12 */
  8959. #endif
  8960. #endif /* HASH_SIZE 192 */
  8961. }
  8962. #endif
  8963. #ifdef WC_XMSS_SHA512
  8964. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 512 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 512
  8965. if (hash == WC_HASH_TYPE_SHA512) {
  8966. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  8967. bench_xmss_sign_verify("XMSS-SHA2_10_512"); /* 10 10 1 */
  8968. #endif
  8969. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  8970. #ifdef BENCH_XMSS_SLOW_KEYGEN
  8971. bench_xmss_sign_verify("XMSS-SHA2_16_512"); /* 16 16 1 */
  8972. #endif
  8973. #endif
  8974. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  8975. #ifdef BENCH_XMSS_SLOW_KEYGEN
  8976. bench_xmss_sign_verify("XMSS-SHA2_20_512"); /* 20 20 1 */
  8977. #endif
  8978. #endif
  8979. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  8980. bench_xmss_sign_verify("XMSSMT-SHA2_20/2_512"); /* 10 20 2 */
  8981. bench_xmss_sign_verify("XMSSMT-SHA2_20/4_512"); /* 5 20 4 */
  8982. #endif
  8983. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  8984. #ifdef BENCH_XMSS_SLOW_KEYGEN
  8985. bench_xmss_sign_verify("XMSSMT-SHA2_40/2_512"); /* 20 40 4 */
  8986. #endif
  8987. #ifdef BENCH_XMSS_SLOW_KEYGEN
  8988. bench_xmss_sign_verify("XMSSMT-SHA2_40/4_512"); /* 10 40 4 */
  8989. #endif
  8990. bench_xmss_sign_verify("XMSSMT-SHA2_40/8_512"); /* 5 40 8 */
  8991. #endif
  8992. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  8993. #ifdef BENCH_XMSS_SLOW_KEYGEN
  8994. bench_xmss_sign_verify("XMSSMT-SHA2_60/3_512"); /* 20 60 3 */
  8995. #endif
  8996. #ifdef BENCH_XMSS_SLOW_KEYGEN
  8997. bench_xmss_sign_verify("XMSSMT-SHA2_60/6_512"); /* 10 60 6 */
  8998. #endif
  8999. bench_xmss_sign_verify("XMSSMT-SHA2_60/12_512"); /* 5 60 12 */
  9000. #endif
  9001. }
  9002. #endif /* HASH_SIZE 512 */
  9003. #endif
  9004. #ifdef WC_XMSS_SHAKE128
  9005. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  9006. if (hash == WC_HASH_TYPE_SHAKE128) {
  9007. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9008. bench_xmss_sign_verify("XMSS-SHAKE_10_256"); /* 10 10 1 */
  9009. #endif
  9010. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9011. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9012. bench_xmss_sign_verify("XMSS-SHAKE_16_256"); /* 16 16 1 */
  9013. #endif
  9014. #endif
  9015. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9016. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9017. bench_xmss_sign_verify("XMSS-SHAKE_20_256"); /* 20 20 1 */
  9018. #endif
  9019. #endif
  9020. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9021. bench_xmss_sign_verify("XMSSMT-SHAKE_20/2_256"); /* 10 20 2 */
  9022. bench_xmss_sign_verify("XMSSMT-SHAKE_20/4_256"); /* 5 20 4 */
  9023. #endif
  9024. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9025. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9026. bench_xmss_sign_verify("XMSSMT-SHAKE_40/2_256"); /* 20 40 4 */
  9027. #endif
  9028. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9029. bench_xmss_sign_verify("XMSSMT-SHAKE_40/4_256"); /* 10 40 4 */
  9030. #endif
  9031. bench_xmss_sign_verify("XMSSMT-SHAKE_40/8_256"); /* 5 40 8 */
  9032. #endif
  9033. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9034. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9035. bench_xmss_sign_verify("XMSSMT-SHAKE_60/3_256"); /* 20 60 3 */
  9036. #endif
  9037. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9038. bench_xmss_sign_verify("XMSSMT-SHAKE_60/6_256"); /* 10 60 6 */
  9039. #endif
  9040. bench_xmss_sign_verify("XMSSMT-SHAKE_60/12_256"); /* 5 60 12 */
  9041. #endif
  9042. }
  9043. #endif /* HASH_SIZE 256 */
  9044. #endif
  9045. #ifdef WC_XMSS_SHAKE256
  9046. if (hash == WC_HASH_TYPE_SHAKE256) {
  9047. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 512 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 512
  9048. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9049. bench_xmss_sign_verify("XMSS-SHAKE_10_512"); /* 10 10 1 */
  9050. #endif
  9051. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9052. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9053. bench_xmss_sign_verify("XMSS-SHAKE_16_512"); /* 16 16 1 */
  9054. #endif
  9055. #endif
  9056. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9057. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9058. bench_xmss_sign_verify("XMSS-SHAKE_20_512"); /* 20 20 1 */
  9059. #endif
  9060. #endif
  9061. #endif /* HASH_SIZE 512 */
  9062. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  9063. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9064. bench_xmss_sign_verify("XMSS-SHAKE256_10_256"); /* 10 10 1 */
  9065. #endif
  9066. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9067. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9068. bench_xmss_sign_verify("XMSS-SHAKE256_16_256"); /* 16 16 1 */
  9069. #endif
  9070. #endif
  9071. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9072. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9073. bench_xmss_sign_verify("XMSS-SHAKE256_20_256"); /* 20 20 1 */
  9074. #endif
  9075. #endif
  9076. #endif /* HASH_SIZE 256 */
  9077. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 192 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 192
  9078. #if WOLFSSL_XMSS_MIN_HEIGHT <= 10 && WOLFSSL_XMSS_MAX_HEIGHT >= 10
  9079. bench_xmss_sign_verify("XMSS-SHAKE256_10_192"); /* 10 10 1 */
  9080. #endif
  9081. #if WOLFSSL_XMSS_MIN_HEIGHT <= 16 && WOLFSSL_XMSS_MAX_HEIGHT >= 16
  9082. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9083. bench_xmss_sign_verify("XMSS-SHAKE256_16_192"); /* 16 16 1 */
  9084. #endif
  9085. #endif
  9086. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9087. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9088. bench_xmss_sign_verify("XMSS-SHAKE256_20_192"); /* 20 20 1 */
  9089. #endif
  9090. #endif
  9091. #endif /* HASH_SIZE 192 */
  9092. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 512 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 512
  9093. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9094. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9095. bench_xmss_sign_verify("XMSSMT-SHAKE_20/2_512"); /* 10 20 2 */
  9096. #endif
  9097. bench_xmss_sign_verify("XMSSMT-SHAKE_20/4_512"); /* 5 20 4 */
  9098. #endif
  9099. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9100. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9101. bench_xmss_sign_verify("XMSSMT-SHAKE_40/2_512"); /* 20 40 4 */
  9102. #endif
  9103. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9104. bench_xmss_sign_verify("XMSSMT-SHAKE_40/4_512"); /* 10 40 4 */
  9105. #endif
  9106. bench_xmss_sign_verify("XMSSMT-SHAKE_40/8_512"); /* 5 40 8 */
  9107. #endif
  9108. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9109. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9110. bench_xmss_sign_verify("XMSSMT-SHAKE_60/3_512"); /* 20 60 3 */
  9111. #endif
  9112. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9113. bench_xmss_sign_verify("XMSSMT-SHAKE_60/6_512"); /* 10 60 6 */
  9114. #endif
  9115. bench_xmss_sign_verify("XMSSMT-SHAKE_60/12_512"); /* 5 60 12 */
  9116. #endif
  9117. #endif /* HASH_SIZE 512 */
  9118. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 256 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 256
  9119. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9120. bench_xmss_sign_verify("XMSSMT-SHAKE256_20/2_256"); /* 10 20 2 */
  9121. bench_xmss_sign_verify("XMSSMT-SHAKE256_20/4_256"); /* 5 20 4 */
  9122. #endif
  9123. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9124. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9125. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/2_256"); /* 20 40 4 */
  9126. #endif
  9127. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9128. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/4_256"); /* 10 40 4 */
  9129. #endif
  9130. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/8_256"); /* 5 40 8 */
  9131. #endif
  9132. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9133. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9134. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/3_256"); /* 20 60 3 */
  9135. #endif
  9136. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9137. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/6_256"); /* 10 60 6 */
  9138. #endif
  9139. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/12_256");/* 5 60 12 */
  9140. #endif
  9141. #endif /* HASH_SIZE 256 */
  9142. #if WOLFSSL_WC_XMSS_MIN_HASH_SIZE <= 192 && WOLFSSL_WC_XMSS_MAX_HASH_SIZE >= 192
  9143. #if WOLFSSL_XMSS_MIN_HEIGHT <= 20 && WOLFSSL_XMSS_MAX_HEIGHT >= 20
  9144. bench_xmss_sign_verify("XMSSMT-SHAKE256_20/2_192"); /* 10 20 2 */
  9145. bench_xmss_sign_verify("XMSSMT-SHAKE256_20/4_192"); /* 5 20 4 */
  9146. #endif
  9147. #if WOLFSSL_XMSS_MIN_HEIGHT <= 40 && WOLFSSL_XMSS_MAX_HEIGHT >= 40
  9148. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9149. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/2_192"); /* 20 40 4 */
  9150. #endif
  9151. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9152. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/4_192"); /* 10 40 4 */
  9153. #endif
  9154. bench_xmss_sign_verify("XMSSMT-SHAKE256_40/8_192"); /* 5 40 8 */
  9155. #endif
  9156. #if WOLFSSL_XMSS_MIN_HEIGHT <= 60 && WOLFSSL_XMSS_MAX_HEIGHT >= 60
  9157. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9158. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/3_192"); /* 20 60 3 */
  9159. #endif
  9160. #ifdef BENCH_XMSS_SLOW_KEYGEN
  9161. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/6_192"); /* 10 60 6 */
  9162. #endif
  9163. bench_xmss_sign_verify("XMSSMT-SHAKE256_60/12_192");/* 5 60 12 */
  9164. #endif
  9165. #endif /* HASH_SIZE 192 */
  9166. }
  9167. #endif
  9168. return;
  9169. }
  9170. #endif /* if defined(WOLFSSL_HAVE_XMSS) && !defined(WOLFSSL_XMSS_VERIFY_ONLY) */
  9171. #ifdef HAVE_ECC
  9172. /* Maximum ECC name plus null terminator:
  9173. * "ECC [%15s]" and "ECDHE [%15s]" and "ECDSA [%15s]" */
  9174. #define BENCH_ECC_NAME_SZ (ECC_MAXNAME + 8)
  9175. /* run all benchmarks on a curve */
  9176. void bench_ecc_curve(int curveId)
  9177. {
  9178. if (bench_all || (bench_asym_algs & BENCH_ECC_MAKEKEY)) {
  9179. #ifndef NO_SW_BENCH
  9180. bench_eccMakeKey(0, curveId);
  9181. #endif
  9182. #if defined(BENCH_DEVID)
  9183. bench_eccMakeKey(1, curveId);
  9184. #endif
  9185. }
  9186. if (bench_all || (bench_asym_algs & BENCH_ECC)) {
  9187. #ifndef NO_SW_BENCH
  9188. bench_ecc(0, curveId);
  9189. #endif
  9190. #if defined(BENCH_DEVID)
  9191. bench_ecc(1, curveId);
  9192. #endif
  9193. }
  9194. #ifdef HAVE_ECC_ENCRYPT
  9195. if (bench_all || (bench_asym_algs & BENCH_ECC_ENCRYPT))
  9196. bench_eccEncrypt(curveId);
  9197. #endif
  9198. }
  9199. void bench_eccMakeKey(int useDeviceID, int curveId)
  9200. {
  9201. int ret = 0, i, times, count = 0, pending = 0;
  9202. int deviceID;
  9203. int keySize = 0;
  9204. WC_DECLARE_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9205. sizeof(ecc_key), HEAP_HINT);
  9206. char name[BENCH_ECC_NAME_SZ];
  9207. double start = 0;
  9208. const char**desc = bench_desc_words[lng_index];
  9209. DECLARE_MULTI_VALUE_STATS_VARS()
  9210. WC_CALLOC_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9211. sizeof(ecc_key), HEAP_HINT);
  9212. deviceID = useDeviceID ? devId : INVALID_DEVID;
  9213. keySize = wc_ecc_get_curve_size_from_id(curveId);
  9214. /* ECC Make Key */
  9215. bench_stats_start(&count, &start);
  9216. do {
  9217. /* while free pending slots in queue, submit ops */
  9218. for (times = 0; times < agreeTimes || pending > 0; ) {
  9219. bench_async_poll(&pending);
  9220. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9221. if (bench_async_check(&ret,
  9222. BENCH_ASYNC_GET_DEV(genKey[i]), 0,
  9223. &times, agreeTimes, &pending)) {
  9224. wc_ecc_free(genKey[i]);
  9225. ret = wc_ecc_init_ex(genKey[i], HEAP_HINT, deviceID);
  9226. if (ret < 0) {
  9227. goto exit;
  9228. }
  9229. ret = wc_ecc_make_key_ex(&gRng, keySize, genKey[i],
  9230. curveId);
  9231. if (!bench_async_handle(&ret,
  9232. BENCH_ASYNC_GET_DEV(genKey[i]), 0, &times,
  9233. &pending)) {
  9234. goto exit;
  9235. }
  9236. }
  9237. } /* for i */
  9238. RECORD_MULTI_VALUE_STATS();
  9239. } /* for times */
  9240. count += times;
  9241. } while (bench_stats_check(start)
  9242. #ifdef MULTI_VALUE_STATISTICS
  9243. || runs < minimum_runs
  9244. #endif
  9245. );
  9246. exit:
  9247. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECC [%15s]",
  9248. wc_ecc_get_name(curveId));
  9249. bench_stats_asym_finish(name, keySize * 8, desc[2],
  9250. useDeviceID, count, start, ret);
  9251. #ifdef MULTI_VALUE_STATISTICS
  9252. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9253. #endif
  9254. /* cleanup */
  9255. if (WC_ARRAY_OK(genKey)) {
  9256. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9257. wc_ecc_free(genKey[i]);
  9258. }
  9259. WC_FREE_ARRAY(genKey, BENCH_MAX_PENDING, HEAP_HINT);
  9260. }
  9261. }
  9262. void bench_ecc(int useDeviceID, int curveId)
  9263. {
  9264. int ret = 0, i, times, count, pending = 0;
  9265. int deviceID;
  9266. int keySize;
  9267. char name[BENCH_ECC_NAME_SZ];
  9268. WC_DECLARE_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9269. sizeof(ecc_key), HEAP_HINT);
  9270. #ifdef HAVE_ECC_DHE
  9271. WC_DECLARE_ARRAY(genKey2, ecc_key, BENCH_MAX_PENDING,
  9272. sizeof(ecc_key), HEAP_HINT);
  9273. #endif
  9274. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9275. #ifdef HAVE_ECC_VERIFY
  9276. int verify[BENCH_MAX_PENDING];
  9277. #endif
  9278. #endif
  9279. word32 x[BENCH_MAX_PENDING];
  9280. double start = 0;
  9281. const char**desc = bench_desc_words[lng_index];
  9282. DECLARE_MULTI_VALUE_STATS_VARS()
  9283. #ifdef HAVE_ECC_DHE
  9284. WC_DECLARE_ARRAY(shared, byte,
  9285. BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9286. #endif
  9287. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9288. WC_DECLARE_ARRAY(sig, byte,
  9289. BENCH_MAX_PENDING, ECC_MAX_SIG_SIZE, HEAP_HINT);
  9290. WC_DECLARE_ARRAY(digest, byte,
  9291. BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9292. #endif
  9293. /* old scan-build misfires -Wmaybe-uninitialized on these. */
  9294. XMEMSET(sig, 0, sizeof(sig));
  9295. XMEMSET(digest, 0, sizeof(digest));
  9296. XMEMSET(shared, 0, sizeof(shared));
  9297. WC_CALLOC_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9298. sizeof(ecc_key), HEAP_HINT);
  9299. #ifdef HAVE_ECC_DHE
  9300. WC_CALLOC_ARRAY(genKey2, ecc_key, BENCH_MAX_PENDING,
  9301. sizeof(ecc_key), HEAP_HINT);
  9302. WC_ALLOC_ARRAY(shared, byte,
  9303. BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9304. #endif
  9305. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9306. WC_ALLOC_ARRAY(sig, byte, BENCH_MAX_PENDING, ECC_MAX_SIG_SIZE, HEAP_HINT);
  9307. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9308. #endif
  9309. deviceID = useDeviceID ? devId : INVALID_DEVID;
  9310. keySize = wc_ecc_get_curve_size_from_id(curveId);
  9311. /* init keys */
  9312. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9313. /* setup an context for each key */
  9314. if ((ret = wc_ecc_init_ex(genKey[i], HEAP_HINT, deviceID)) < 0) {
  9315. goto exit;
  9316. }
  9317. ret = wc_ecc_make_key_ex(&gRng, keySize, genKey[i], curveId);
  9318. #ifdef WOLFSSL_ASYNC_CRYPT
  9319. ret = wc_AsyncWait(ret, &genKey[i]->asyncDev, WC_ASYNC_FLAG_NONE);
  9320. #endif
  9321. if (ret < 0) {
  9322. goto exit;
  9323. }
  9324. #ifdef HAVE_ECC_DHE
  9325. if ((ret = wc_ecc_init_ex(genKey2[i], HEAP_HINT, deviceID)) < 0) {
  9326. goto exit;
  9327. }
  9328. if ((ret = wc_ecc_make_key_ex(&gRng, keySize, genKey2[i],
  9329. curveId)) > 0) {
  9330. goto exit;
  9331. }
  9332. #endif
  9333. }
  9334. #ifdef HAVE_ECC_DHE
  9335. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  9336. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  9337. !defined(HAVE_SELFTEST)
  9338. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9339. (void)wc_ecc_set_rng(genKey[i], &gRng);
  9340. }
  9341. #endif
  9342. /* ECC Shared Secret */
  9343. bench_stats_start(&count, &start);
  9344. PRIVATE_KEY_UNLOCK();
  9345. do {
  9346. for (times = 0; times < agreeTimes || pending > 0; ) {
  9347. bench_async_poll(&pending);
  9348. /* while free pending slots in queue, submit ops */
  9349. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9350. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  9351. &times, agreeTimes, &pending)) {
  9352. x[i] = (word32)keySize;
  9353. ret = wc_ecc_shared_secret(genKey[i], genKey2[i],
  9354. shared[i], &x[i]);
  9355. if (!bench_async_handle(&ret,
  9356. BENCH_ASYNC_GET_DEV(genKey[i]), 1, &times,
  9357. &pending)) {
  9358. goto exit_ecdhe;
  9359. }
  9360. }
  9361. } /* for i */
  9362. RECORD_MULTI_VALUE_STATS();
  9363. } /* for times */
  9364. count += times;
  9365. } while (bench_stats_check(start)
  9366. #ifdef MULTI_VALUE_STATISTICS
  9367. || runs < minimum_runs
  9368. #endif
  9369. );
  9370. PRIVATE_KEY_UNLOCK();
  9371. exit_ecdhe:
  9372. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDHE [%15s]",
  9373. wc_ecc_get_name(curveId));
  9374. bench_stats_asym_finish(name, keySize * 8, desc[3],
  9375. useDeviceID, count, start, ret);
  9376. #ifdef MULTI_VALUE_STATISTICS
  9377. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9378. #endif
  9379. RESET_MULTI_VALUE_STATS_VARS();
  9380. if (ret < 0) {
  9381. goto exit;
  9382. }
  9383. #endif /* HAVE_ECC_DHE */
  9384. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9385. /* Init digest to sign */
  9386. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9387. for (count = 0; count < keySize; count++) {
  9388. digest[i][count] = (byte)count;
  9389. }
  9390. }
  9391. /* ECC Sign */
  9392. bench_stats_start(&count, &start);
  9393. do {
  9394. for (times = 0; times < agreeTimes || pending > 0; ) {
  9395. bench_async_poll(&pending);
  9396. /* while free pending slots in queue, submit ops */
  9397. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9398. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  9399. &times, agreeTimes, &pending)) {
  9400. if (genKey[i]->state == 0) {
  9401. x[i] = ECC_MAX_SIG_SIZE;
  9402. }
  9403. ret = wc_ecc_sign_hash(digest[i], (word32)keySize, sig[i],
  9404. &x[i], GLOBAL_RNG, genKey[i]);
  9405. if (!bench_async_handle(&ret,
  9406. BENCH_ASYNC_GET_DEV(genKey[i]), 1, &times,
  9407. &pending)) {
  9408. goto exit_ecdsa_sign;
  9409. }
  9410. } /* bench_async_check */
  9411. } /* for i */
  9412. RECORD_MULTI_VALUE_STATS();
  9413. } /* for times */
  9414. count += times;
  9415. } while (bench_stats_check(start)
  9416. #ifdef MULTI_VALUE_STATISTICS
  9417. || runs < minimum_runs
  9418. #endif
  9419. );
  9420. exit_ecdsa_sign:
  9421. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDSA [%15s]",
  9422. wc_ecc_get_name(curveId));
  9423. bench_stats_asym_finish(name, keySize * 8, desc[4],
  9424. useDeviceID, count, start, ret);
  9425. #ifdef MULTI_VALUE_STATISTICS
  9426. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9427. #endif
  9428. RESET_MULTI_VALUE_STATS_VARS();
  9429. if (ret < 0) {
  9430. goto exit;
  9431. }
  9432. #ifdef HAVE_ECC_VERIFY
  9433. /* ECC Verify */
  9434. bench_stats_start(&count, &start);
  9435. do {
  9436. for (times = 0; times < agreeTimes || pending > 0; ) {
  9437. bench_async_poll(&pending);
  9438. /* while free pending slots in queue, submit ops */
  9439. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9440. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  9441. &times, agreeTimes, &pending)) {
  9442. if (genKey[i]->state == 0) {
  9443. verify[i] = 0;
  9444. }
  9445. ret = wc_ecc_verify_hash(sig[i], x[i], digest[i],
  9446. (word32)keySize, &verify[i],
  9447. genKey[i]);
  9448. if (!bench_async_handle(&ret,
  9449. BENCH_ASYNC_GET_DEV(genKey[i]),
  9450. 1, &times,
  9451. &pending)) {
  9452. goto exit_ecdsa_verify;
  9453. }
  9454. } /* if bench_async_check */
  9455. } /* for i */
  9456. RECORD_MULTI_VALUE_STATS();
  9457. } /* for times */
  9458. count += times;
  9459. } while (bench_stats_check(start)
  9460. #ifdef MULTI_VALUE_STATISTICS
  9461. || runs < minimum_runs
  9462. #endif
  9463. );
  9464. exit_ecdsa_verify:
  9465. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDSA [%15s]",
  9466. wc_ecc_get_name(curveId));
  9467. bench_stats_asym_finish(name, keySize * 8, desc[5],
  9468. useDeviceID, count, start, ret);
  9469. #ifdef MULTI_VALUE_STATISTICS
  9470. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9471. #endif
  9472. #endif /* HAVE_ECC_VERIFY */
  9473. #endif /* !NO_ASN && HAVE_ECC_SIGN */
  9474. exit:
  9475. /* cleanup */
  9476. if (WC_ARRAY_OK(genKey)) {
  9477. for (i = 0; i < BENCH_MAX_PENDING; i++)
  9478. wc_ecc_free(genKey[i]);
  9479. WC_FREE_ARRAY(genKey, BENCH_MAX_PENDING, HEAP_HINT);
  9480. }
  9481. #ifdef HAVE_ECC_DHE
  9482. if (WC_ARRAY_OK(genKey2)) {
  9483. for (i = 0; i < BENCH_MAX_PENDING; i++)
  9484. wc_ecc_free(genKey2[i]);
  9485. WC_FREE_ARRAY(genKey2, BENCH_MAX_PENDING, HEAP_HINT);
  9486. }
  9487. #endif
  9488. #ifdef HAVE_ECC_DHE
  9489. WC_FREE_ARRAY(shared, BENCH_MAX_PENDING, HEAP_HINT);
  9490. #endif
  9491. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9492. WC_FREE_ARRAY(sig, BENCH_MAX_PENDING, HEAP_HINT);
  9493. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  9494. #endif
  9495. (void)useDeviceID;
  9496. (void)pending;
  9497. (void)x;
  9498. (void)count;
  9499. (void)times;
  9500. (void)desc;
  9501. (void)start;
  9502. (void)name;
  9503. }
  9504. #ifdef HAVE_ECC_ENCRYPT
  9505. void bench_eccEncrypt(int curveId)
  9506. {
  9507. #define BENCH_ECCENCRYPT_MSG_SIZE 48
  9508. #define BENCH_ECCENCRYPT_OUT_SIZE (BENCH_ECCENCRYPT_MSG_SIZE + \
  9509. WC_SHA256_DIGEST_SIZE + \
  9510. (MAX_ECC_BITS+3)/4 + 2)
  9511. word32 outSz = BENCH_ECCENCRYPT_OUT_SIZE;
  9512. #ifdef WOLFSSL_SMALL_STACK
  9513. ecc_key *userA = NULL, *userB = NULL;
  9514. byte *msg = NULL;
  9515. byte *out = NULL;
  9516. #else
  9517. ecc_key userA[1], userB[1];
  9518. byte msg[BENCH_ECCENCRYPT_MSG_SIZE];
  9519. byte out[BENCH_ECCENCRYPT_OUT_SIZE];
  9520. #endif
  9521. char name[BENCH_ECC_NAME_SZ];
  9522. int keySize;
  9523. word32 bench_plainSz = bench_size;
  9524. int ret, i, count;
  9525. double start;
  9526. const char**desc = bench_desc_words[lng_index];
  9527. DECLARE_MULTI_VALUE_STATS_VARS()
  9528. #ifdef WOLFSSL_SMALL_STACK
  9529. userA = (ecc_key *)XMALLOC(sizeof(*userA),
  9530. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9531. userB = (ecc_key *)XMALLOC(sizeof(*userB),
  9532. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9533. msg = (byte *)XMALLOC(BENCH_ECCENCRYPT_MSG_SIZE,
  9534. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9535. out = (byte *)XMALLOC(outSz,
  9536. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9537. if ((! userA) || (! userB) || (! msg) || (! out)) {
  9538. printf("bench_eccEncrypt malloc failed\n");
  9539. goto exit;
  9540. }
  9541. #endif
  9542. keySize = wc_ecc_get_curve_size_from_id(curveId);
  9543. ret = wc_ecc_init_ex(userA, HEAP_HINT, devId);
  9544. if (ret != 0) {
  9545. printf("wc_ecc_encrypt make key A failed: %d\n", ret);
  9546. goto exit;
  9547. }
  9548. ret = wc_ecc_init_ex(userB, HEAP_HINT, devId);
  9549. if (ret != 0) {
  9550. printf("wc_ecc_encrypt make key B failed: %d\n", ret);
  9551. goto exit;
  9552. }
  9553. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  9554. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  9555. !defined(HAVE_SELFTEST)
  9556. ret = wc_ecc_set_rng(userA, &gRng);
  9557. if (ret != 0) {
  9558. goto exit;
  9559. }
  9560. ret = wc_ecc_set_rng(userB, &gRng);
  9561. if (ret != 0) {
  9562. goto exit;
  9563. }
  9564. #endif
  9565. ret = wc_ecc_make_key_ex(&gRng, keySize, userA, curveId);
  9566. #ifdef WOLFSSL_ASYNC_CRYPT
  9567. ret = wc_AsyncWait(ret, &userA->asyncDev, WC_ASYNC_FLAG_NONE);
  9568. #endif
  9569. if (ret != 0)
  9570. goto exit;
  9571. ret = wc_ecc_make_key_ex(&gRng, keySize, userB, curveId);
  9572. #ifdef WOLFSSL_ASYNC_CRYPT
  9573. ret = wc_AsyncWait(ret, &userB->asyncDev, WC_ASYNC_FLAG_NONE);
  9574. #endif
  9575. if (ret != 0)
  9576. goto exit;
  9577. for (i = 0; i < BENCH_ECCENCRYPT_MSG_SIZE; i++) {
  9578. msg[i] = (byte)i;
  9579. }
  9580. bench_stats_start(&count, &start);
  9581. do {
  9582. for (i = 0; i < ntimes; i++) {
  9583. /* encrypt msg to B */
  9584. ret = wc_ecc_encrypt(userA, userB, msg, BENCH_ECCENCRYPT_MSG_SIZE,
  9585. out, &outSz, NULL);
  9586. if (ret != 0) {
  9587. printf("wc_ecc_encrypt failed! %d\n", ret);
  9588. goto exit_enc;
  9589. }
  9590. RECORD_MULTI_VALUE_STATS();
  9591. }
  9592. count += i;
  9593. } while (bench_stats_check(start)
  9594. #ifdef MULTI_VALUE_STATISTICS
  9595. || runs < minimum_runs
  9596. #endif
  9597. );
  9598. exit_enc:
  9599. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECC [%15s]",
  9600. wc_ecc_get_name(curveId));
  9601. bench_stats_asym_finish(name, keySize * 8, desc[6], 0, count, start, ret);
  9602. #ifdef MULTI_VALUE_STATISTICS
  9603. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9604. #endif
  9605. RESET_MULTI_VALUE_STATS_VARS();
  9606. if (ret != 0)
  9607. goto exit;
  9608. bench_stats_start(&count, &start);
  9609. do {
  9610. for (i = 0; i < ntimes; i++) {
  9611. /* decrypt msg from A */
  9612. ret = wc_ecc_decrypt(userB, userA, out, outSz, bench_plain,
  9613. &bench_plainSz, NULL);
  9614. if (ret != 0) {
  9615. printf("wc_ecc_decrypt failed! %d\n", ret);
  9616. goto exit_dec;
  9617. }
  9618. RECORD_MULTI_VALUE_STATS();
  9619. }
  9620. count += i;
  9621. } while (bench_stats_check(start)
  9622. #ifdef MULTI_VALUE_STATISTICS
  9623. || runs < minimum_runs
  9624. #endif
  9625. );
  9626. exit_dec:
  9627. bench_stats_asym_finish(name, keySize * 8, desc[7], 0, count, start, ret);
  9628. #ifdef MULTI_VALUE_STATISTICS
  9629. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9630. #endif
  9631. exit:
  9632. /* cleanup */
  9633. #ifdef WOLFSSL_SMALL_STACK
  9634. if (userA) {
  9635. wc_ecc_free(userA);
  9636. XFREE(userA, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9637. }
  9638. if (userB) {
  9639. wc_ecc_free(userB);
  9640. XFREE(userB, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9641. }
  9642. if (msg)
  9643. XFREE(msg, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9644. if (out)
  9645. XFREE(out, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9646. #else
  9647. wc_ecc_free(userB);
  9648. wc_ecc_free(userA);
  9649. #endif
  9650. }
  9651. #endif
  9652. #ifdef WOLFSSL_SM2
  9653. static void bench_sm2_MakeKey(int useDeviceID)
  9654. {
  9655. int ret = 0, i, times, count, pending = 0;
  9656. int deviceID;
  9657. int keySize;
  9658. WC_DECLARE_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9659. sizeof(ecc_key), HEAP_HINT);
  9660. char name[BENCH_ECC_NAME_SZ];
  9661. double start;
  9662. const char**desc = bench_desc_words[lng_index];
  9663. DECLARE_MULTI_VALUE_STATS_VARS()
  9664. deviceID = useDeviceID ? devId : INVALID_DEVID;
  9665. keySize = wc_ecc_get_curve_size_from_id(ECC_SM2P256V1);
  9666. WC_CALLOC_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9667. sizeof(ecc_key), HEAP_HINT);
  9668. /* ECC Make Key */
  9669. bench_stats_start(&count, &start);
  9670. do {
  9671. /* while free pending slots in queue, submit ops */
  9672. for (times = 0; times < agreeTimes || pending > 0; ) {
  9673. bench_async_poll(&pending);
  9674. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9675. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 0,
  9676. &times, agreeTimes, &pending)) {
  9677. wc_ecc_free(genKey[i]);
  9678. ret = wc_ecc_init_ex(genKey[i], HEAP_HINT, deviceID);
  9679. if (ret < 0) {
  9680. goto exit;
  9681. }
  9682. ret = wc_ecc_sm2_make_key(&gRng, genKey[i],
  9683. WC_ECC_FLAG_NONE);
  9684. if (!bench_async_handle(&ret,
  9685. BENCH_ASYNC_GET_DEV(genKey[i]), 0, &times,
  9686. &pending)) {
  9687. goto exit;
  9688. }
  9689. }
  9690. } /* for i */
  9691. RECORD_MULTI_VALUE_STATS();
  9692. } /* for times */
  9693. count += times;
  9694. } while (bench_stats_check(start)
  9695. #ifdef MULTI_VALUE_STATISTICS
  9696. || runs < minimum_runs
  9697. #endif
  9698. );
  9699. exit:
  9700. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECC [%15s]",
  9701. wc_ecc_get_name(ECC_SM2P256V1));
  9702. bench_stats_asym_finish(name, keySize * 8, desc[2], useDeviceID, count,
  9703. start, ret);
  9704. #ifdef MULTI_VALUE_STATISTICS
  9705. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9706. #endif
  9707. /* cleanup */
  9708. if (WC_ARRAY_OK(genKey)) {
  9709. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9710. wc_ecc_free(genKey[i]);
  9711. }
  9712. WC_FREE_ARRAY(genKey, BENCH_MAX_PENDING, HEAP_HINT);
  9713. }
  9714. }
  9715. void bench_sm2(int useDeviceID)
  9716. {
  9717. int ret = 0, i, times, count, pending = 0;
  9718. int deviceID;
  9719. int keySize;
  9720. char name[BENCH_ECC_NAME_SZ];
  9721. WC_DECLARE_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9722. sizeof(ecc_key), HEAP_HINT);
  9723. #ifdef HAVE_ECC_DHE
  9724. WC_DECLARE_ARRAY(genKey2, ecc_key, BENCH_MAX_PENDING,
  9725. sizeof(ecc_key), HEAP_HINT);
  9726. #endif
  9727. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9728. #ifdef HAVE_ECC_VERIFY
  9729. WC_DECLARE_ARRAY(verify, int, BENCH_MAX_PENDING,
  9730. sizeof(int), HEAP_HINT);
  9731. #endif
  9732. #endif
  9733. word32 x[BENCH_MAX_PENDING];
  9734. double start = 0;
  9735. const char**desc = bench_desc_words[lng_index];
  9736. DECLARE_MULTI_VALUE_STATS_VARS()
  9737. #ifdef HAVE_ECC_DHE
  9738. WC_DECLARE_ARRAY(shared, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9739. #endif
  9740. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9741. WC_DECLARE_ARRAY(sig, byte, BENCH_MAX_PENDING, ECC_MAX_SIG_SIZE, HEAP_HINT);
  9742. WC_DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9743. #endif
  9744. #ifdef HAVE_ECC_DHE
  9745. WC_ALLOC_ARRAY(shared, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9746. #endif
  9747. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9748. WC_ALLOC_ARRAY(sig, byte, BENCH_MAX_PENDING, ECC_MAX_SIG_SIZE, HEAP_HINT);
  9749. WC_ALLOC_ARRAY(digest, byte, BENCH_MAX_PENDING, MAX_ECC_BYTES, HEAP_HINT);
  9750. #endif
  9751. deviceID = useDeviceID ? devId : INVALID_DEVID;
  9752. bench_sm2_MakeKey(useDeviceID);
  9753. WC_CALLOC_ARRAY(genKey, ecc_key, BENCH_MAX_PENDING,
  9754. sizeof(ecc_key), HEAP_HINT);
  9755. #ifdef HAVE_ECC_DHE
  9756. WC_CALLOC_ARRAY(genKey2, ecc_key, BENCH_MAX_PENDING,
  9757. sizeof(ecc_key), HEAP_HINT);
  9758. #endif
  9759. keySize = wc_ecc_get_curve_size_from_id(ECC_SM2P256V1);
  9760. /* init keys */
  9761. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9762. /* setup an context for each key */
  9763. if ((ret = wc_ecc_init_ex(genKey[i], HEAP_HINT, deviceID)) < 0) {
  9764. goto exit;
  9765. }
  9766. ret = wc_ecc_sm2_make_key(&gRng, genKey[i], WC_ECC_FLAG_NONE);
  9767. #ifdef WOLFSSL_ASYNC_CRYPT
  9768. ret = wc_AsyncWait(ret, genKey[i].asyncDev, WC_ASYNC_FLAG_NONE);
  9769. #endif
  9770. if (ret < 0) {
  9771. goto exit;
  9772. }
  9773. #ifdef HAVE_ECC_DHE
  9774. if ((ret = wc_ecc_init_ex(genKey2[i], HEAP_HINT, deviceID)) < 0) {
  9775. goto exit;
  9776. }
  9777. if ((ret = wc_ecc_sm2_make_key(&gRng, genKey2[i],
  9778. WC_ECC_FLAG_NONE)) > 0) {
  9779. goto exit;
  9780. }
  9781. #endif
  9782. }
  9783. #ifdef HAVE_ECC_DHE
  9784. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  9785. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  9786. !defined(HAVE_SELFTEST)
  9787. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9788. (void)wc_ecc_set_rng(genKey[i], &gRng);
  9789. }
  9790. #endif
  9791. /* ECC Shared Secret */
  9792. bench_stats_start(&count, &start);
  9793. PRIVATE_KEY_UNLOCK();
  9794. do {
  9795. for (times = 0; times < agreeTimes || pending > 0; ) {
  9796. bench_async_poll(&pending);
  9797. /* while free pending slots in queue, submit ops */
  9798. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9799. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  9800. &times, agreeTimes, &pending)) {
  9801. x[i] = (word32)keySize;
  9802. ret = wc_ecc_sm2_shared_secret(genKey[i], genKey2[i],
  9803. shared[i], &x[i]);
  9804. if (!bench_async_handle(&ret,
  9805. BENCH_ASYNC_GET_DEV(genKey[i]), 1, &times,
  9806. &pending)) {
  9807. goto exit_ecdhe;
  9808. }
  9809. }
  9810. } /* for i */
  9811. RECORD_MULTI_VALUE_STATS();
  9812. } /* for times */
  9813. count += times;
  9814. } while (bench_stats_check(start)
  9815. #ifdef MULTI_VALUE_STATISTICS
  9816. || runs < minimum_runs
  9817. #endif
  9818. );
  9819. PRIVATE_KEY_UNLOCK();
  9820. exit_ecdhe:
  9821. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDHE [%15s]",
  9822. wc_ecc_get_name(ECC_SM2P256V1));
  9823. bench_stats_asym_finish(name, keySize * 8, desc[3], useDeviceID, count,
  9824. start, ret);
  9825. #ifdef MULTI_VALUE_STATISTICS
  9826. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9827. #endif
  9828. if (ret < 0) {
  9829. goto exit;
  9830. }
  9831. #endif /* HAVE_ECC_DHE */
  9832. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9833. /* Init digest to sign */
  9834. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9835. for (count = 0; count < keySize; count++) {
  9836. digest[i][count] = (byte)count;
  9837. }
  9838. }
  9839. RESET_MULTI_VALUE_STATS_VARS();
  9840. /* ECC Sign */
  9841. bench_stats_start(&count, &start);
  9842. do {
  9843. for (times = 0; times < agreeTimes || pending > 0; ) {
  9844. bench_async_poll(&pending);
  9845. /* while free pending slots in queue, submit ops */
  9846. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9847. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  9848. &times, agreeTimes, &pending)) {
  9849. if (genKey[i].state == 0)
  9850. x[i] = ECC_MAX_SIG_SIZE;
  9851. ret = wc_ecc_sm2_sign_hash(digest[i], (word32)keySize,
  9852. sig[i], x[i], &gRng, genKey[i]);
  9853. if (!bench_async_handle(&ret,
  9854. BENCH_ASYNC_GET_DEV(genKey[i]), 1, &times,
  9855. &pending)) {
  9856. goto exit_ecdsa_sign;
  9857. }
  9858. }
  9859. } /* for i */
  9860. RECORD_MULTI_VALUE_STATS();
  9861. } /* for times */
  9862. count += times;
  9863. } while (bench_stats_check(start)
  9864. #ifdef MULTI_VALUE_STATISTICS
  9865. || runs < minimum_runs
  9866. #endif
  9867. );
  9868. exit_ecdsa_sign:
  9869. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDSA [%15s]",
  9870. wc_ecc_get_name(ECC_SM2P256V1));
  9871. bench_stats_asym_finish(name, keySize * 8, desc[4], useDeviceID, count,
  9872. start, ret);
  9873. #ifdef MULTI_VALUE_STATISTICS
  9874. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9875. #endif
  9876. if (ret < 0) {
  9877. goto exit;
  9878. }
  9879. #ifdef HAVE_ECC_VERIFY
  9880. /* ECC Verify */
  9881. bench_stats_start(&count, &start);
  9882. do {
  9883. for (times = 0; times < agreeTimes || pending > 0; ) {
  9884. bench_async_poll(&pending);
  9885. /* while free pending slots in queue, submit ops */
  9886. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  9887. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(genKey[i]), 1,
  9888. &times, agreeTimes, &pending)) {
  9889. if (genKey[i].state == 0)
  9890. verify[i] = 0;
  9891. ret = wc_ecc_sm2_verify_hash(sig[i], x[i], digest[i],
  9892. (word32)keySize, verify[i], genKey[i]);
  9893. if (!bench_async_handle(&ret,
  9894. BENCH_ASYNC_GET_DEV(genKey[i]), 1, &times,
  9895. &pending)) {
  9896. goto exit_ecdsa_verify;
  9897. }
  9898. }
  9899. } /* for i */
  9900. RECORD_MULTI_VALUE_STATS();
  9901. } /* for times */
  9902. count += times;
  9903. } while (bench_stats_check(start)
  9904. #ifdef MULTI_VALUE_STATISTICS
  9905. || runs < minimum_runs
  9906. #endif
  9907. );
  9908. exit_ecdsa_verify:
  9909. (void)XSNPRINTF(name, BENCH_ECC_NAME_SZ, "ECDSA [%15s]",
  9910. wc_ecc_get_name(ECC_SM2P256V1));
  9911. bench_stats_asym_finish(name, keySize * 8, desc[5], useDeviceID, count,
  9912. start, ret);
  9913. #ifdef MULTI_VALUE_STATISTICS
  9914. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9915. #endif
  9916. #endif /* HAVE_ECC_VERIFY */
  9917. #endif /* !NO_ASN && HAVE_ECC_SIGN */
  9918. exit:
  9919. /* cleanup */
  9920. if (WC_ARRAY_OK(genKey)) {
  9921. for (i = 0; i < BENCH_MAX_PENDING; i++)
  9922. wc_ecc_free(genKey[i]);
  9923. WC_FREE_ARRAY(genKey, BENCH_MAX_PENDING, HEAP_HINT);
  9924. }
  9925. #ifdef HAVE_ECC_DHE
  9926. if (WC_ARRAY_OK(genKey2)) {
  9927. for (i = 0; i < BENCH_MAX_PENDING; i++)
  9928. wc_ecc_free(genKey2[i]);
  9929. WC_FREE_ARRAY(genKey2, BENCH_MAX_PENDING, HEAP_HINT);
  9930. }
  9931. #endif
  9932. #ifdef HAVE_ECC_DHE
  9933. WC_FREE_ARRAY(shared, BENCH_MAX_PENDING, HEAP_HINT);
  9934. #endif
  9935. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  9936. WC_FREE_ARRAY(sig, BENCH_MAX_PENDING, HEAP_HINT);
  9937. WC_FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  9938. #endif
  9939. (void)useDeviceID;
  9940. (void)pending;
  9941. (void)x;
  9942. (void)count;
  9943. (void)times;
  9944. (void)desc;
  9945. (void)start;
  9946. (void)name;
  9947. }
  9948. #endif /* WOLFSSL_SM2 */
  9949. #endif /* HAVE_ECC */
  9950. #ifdef HAVE_CURVE25519
  9951. void bench_curve25519KeyGen(int useDeviceID)
  9952. {
  9953. curve25519_key genKey;
  9954. double start;
  9955. int ret = 0, i, count;
  9956. const char**desc = bench_desc_words[lng_index];
  9957. DECLARE_MULTI_VALUE_STATS_VARS()
  9958. /* Key Gen */
  9959. bench_stats_start(&count, &start);
  9960. do {
  9961. for (i = 0; i < genTimes; i++) {
  9962. ret = wc_curve25519_init_ex(&genKey, HEAP_HINT,
  9963. useDeviceID ? devId : INVALID_DEVID);
  9964. if (ret != 0) {
  9965. printf("wc_curve25519_init_ex failed: %d\n", ret);
  9966. break;
  9967. }
  9968. ret = wc_curve25519_make_key(&gRng, 32, &genKey);
  9969. wc_curve25519_free(&genKey);
  9970. if (ret != 0) {
  9971. printf("wc_curve25519_make_key failed: %d\n", ret);
  9972. break;
  9973. }
  9974. RECORD_MULTI_VALUE_STATS();
  9975. }
  9976. count += i;
  9977. } while (bench_stats_check(start)
  9978. #ifdef MULTI_VALUE_STATISTICS
  9979. || runs < minimum_runs
  9980. #endif
  9981. );
  9982. bench_stats_asym_finish("CURVE", 25519, desc[2], useDeviceID, count, start,
  9983. ret);
  9984. #ifdef MULTI_VALUE_STATISTICS
  9985. bench_multi_value_stats(max, min, sum, squareSum, runs);
  9986. #endif
  9987. }
  9988. #ifdef HAVE_CURVE25519_SHARED_SECRET
  9989. void bench_curve25519KeyAgree(int useDeviceID)
  9990. {
  9991. curve25519_key genKey, genKey2;
  9992. double start;
  9993. int ret, i, count;
  9994. byte shared[32];
  9995. const char**desc = bench_desc_words[lng_index];
  9996. word32 x = 0;
  9997. DECLARE_MULTI_VALUE_STATS_VARS()
  9998. wc_curve25519_init_ex(&genKey, HEAP_HINT,
  9999. useDeviceID ? devId : INVALID_DEVID);
  10000. wc_curve25519_init_ex(&genKey2, HEAP_HINT,
  10001. useDeviceID ? devId : INVALID_DEVID);
  10002. ret = wc_curve25519_make_key(&gRng, 32, &genKey);
  10003. if (ret != 0) {
  10004. printf("curve25519_make_key failed\n");
  10005. return;
  10006. }
  10007. ret = wc_curve25519_make_key(&gRng, 32, &genKey2);
  10008. if (ret != 0) {
  10009. printf("curve25519_make_key failed: %d\n", ret);
  10010. wc_curve25519_free(&genKey);
  10011. return;
  10012. }
  10013. /* Shared secret */
  10014. bench_stats_start(&count, &start);
  10015. do {
  10016. for (i = 0; i < agreeTimes; i++) {
  10017. x = sizeof(shared);
  10018. ret = wc_curve25519_shared_secret(&genKey, &genKey2, shared, &x);
  10019. if (ret != 0) {
  10020. printf("curve25519_shared_secret failed: %d\n", ret);
  10021. goto exit;
  10022. }
  10023. RECORD_MULTI_VALUE_STATS();
  10024. }
  10025. count += i;
  10026. } while (bench_stats_check(start)
  10027. #ifdef MULTI_VALUE_STATISTICS
  10028. || runs < minimum_runs
  10029. #endif
  10030. );
  10031. exit:
  10032. bench_stats_asym_finish("CURVE", 25519, desc[3], useDeviceID, count, start,
  10033. ret);
  10034. #ifdef MULTI_VALUE_STATISTICS
  10035. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10036. #endif
  10037. wc_curve25519_free(&genKey2);
  10038. wc_curve25519_free(&genKey);
  10039. }
  10040. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  10041. #endif /* HAVE_CURVE25519 */
  10042. #ifdef HAVE_ED25519
  10043. void bench_ed25519KeyGen(void)
  10044. {
  10045. #ifdef HAVE_ED25519_MAKE_KEY
  10046. ed25519_key genKey;
  10047. double start;
  10048. int i, count;
  10049. const char**desc = bench_desc_words[lng_index];
  10050. DECLARE_MULTI_VALUE_STATS_VARS()
  10051. /* Key Gen */
  10052. bench_stats_start(&count, &start);
  10053. do {
  10054. for (i = 0; i < genTimes; i++) {
  10055. wc_ed25519_init(&genKey);
  10056. (void)wc_ed25519_make_key(&gRng, 32, &genKey);
  10057. wc_ed25519_free(&genKey);
  10058. RECORD_MULTI_VALUE_STATS();
  10059. }
  10060. count += i;
  10061. } while (bench_stats_check(start)
  10062. #ifdef MULTI_VALUE_STATISTICS
  10063. || runs < minimum_runs
  10064. #endif
  10065. );
  10066. bench_stats_asym_finish("ED", 25519, desc[2], 0, count, start, 0);
  10067. #ifdef MULTI_VALUE_STATISTICS
  10068. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10069. #endif
  10070. #endif /* HAVE_ED25519_MAKE_KEY */
  10071. }
  10072. void bench_ed25519KeySign(void)
  10073. {
  10074. #ifdef HAVE_ED25519_MAKE_KEY
  10075. int ret;
  10076. #endif
  10077. ed25519_key genKey;
  10078. #ifdef HAVE_ED25519_SIGN
  10079. double start;
  10080. int i, count;
  10081. byte sig[ED25519_SIG_SIZE];
  10082. byte msg[512];
  10083. word32 x = 0;
  10084. const char**desc = bench_desc_words[lng_index];
  10085. DECLARE_MULTI_VALUE_STATS_VARS()
  10086. #endif
  10087. wc_ed25519_init(&genKey);
  10088. #ifdef HAVE_ED25519_MAKE_KEY
  10089. ret = wc_ed25519_make_key(&gRng, ED25519_KEY_SIZE, &genKey);
  10090. if (ret != 0) {
  10091. printf("ed25519_make_key failed\n");
  10092. return;
  10093. }
  10094. #endif
  10095. #ifdef HAVE_ED25519_SIGN
  10096. /* make dummy msg */
  10097. for (i = 0; i < (int)sizeof(msg); i++)
  10098. msg[i] = (byte)i;
  10099. bench_stats_start(&count, &start);
  10100. do {
  10101. for (i = 0; i < agreeTimes; i++) {
  10102. x = sizeof(sig);
  10103. ret = wc_ed25519_sign_msg(msg, sizeof(msg), sig, &x, &genKey);
  10104. if (ret != 0) {
  10105. printf("ed25519_sign_msg failed\n");
  10106. goto exit_ed_sign;
  10107. }
  10108. RECORD_MULTI_VALUE_STATS();
  10109. }
  10110. count += i;
  10111. } while (bench_stats_check(start)
  10112. #ifdef MULTI_VALUE_STATISTICS
  10113. || runs < minimum_runs
  10114. #endif
  10115. );
  10116. exit_ed_sign:
  10117. bench_stats_asym_finish("ED", 25519, desc[4], 0, count, start, ret);
  10118. #ifdef MULTI_VALUE_STATISTICS
  10119. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10120. #endif
  10121. RESET_MULTI_VALUE_STATS_VARS();
  10122. #ifdef HAVE_ED25519_VERIFY
  10123. bench_stats_start(&count, &start);
  10124. do {
  10125. for (i = 0; i < agreeTimes; i++) {
  10126. int verify = 0;
  10127. ret = wc_ed25519_verify_msg(sig, x, msg, sizeof(msg), &verify,
  10128. &genKey);
  10129. if (ret != 0 || verify != 1) {
  10130. printf("ed25519_verify_msg failed\n");
  10131. goto exit_ed_verify;
  10132. }
  10133. RECORD_MULTI_VALUE_STATS();
  10134. }
  10135. count += i;
  10136. } while (bench_stats_check(start)
  10137. #ifdef MULTI_VALUE_STATISTICS
  10138. || runs < minimum_runs
  10139. #endif
  10140. );
  10141. exit_ed_verify:
  10142. bench_stats_asym_finish("ED", 25519, desc[5], 0, count, start, ret);
  10143. #ifdef MULTI_VALUE_STATISTICS
  10144. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10145. #endif
  10146. #endif /* HAVE_ED25519_VERIFY */
  10147. #endif /* HAVE_ED25519_SIGN */
  10148. wc_ed25519_free(&genKey);
  10149. }
  10150. #endif /* HAVE_ED25519 */
  10151. #ifdef HAVE_CURVE448
  10152. void bench_curve448KeyGen(void)
  10153. {
  10154. curve448_key genKey;
  10155. double start;
  10156. int ret = 0, i, count;
  10157. const char**desc = bench_desc_words[lng_index];
  10158. DECLARE_MULTI_VALUE_STATS_VARS()
  10159. /* Key Gen */
  10160. bench_stats_start(&count, &start);
  10161. do {
  10162. for (i = 0; i < genTimes; i++) {
  10163. ret = wc_curve448_make_key(&gRng, 56, &genKey);
  10164. wc_curve448_free(&genKey);
  10165. if (ret != 0) {
  10166. printf("wc_curve448_make_key failed: %d\n", ret);
  10167. break;
  10168. }
  10169. RECORD_MULTI_VALUE_STATS();
  10170. }
  10171. count += i;
  10172. } while (bench_stats_check(start)
  10173. #ifdef MULTI_VALUE_STATISTICS
  10174. || runs < minimum_runs
  10175. #endif
  10176. );
  10177. bench_stats_asym_finish("CURVE", 448, desc[2], 0, count, start, ret);
  10178. #ifdef MULTI_VALUE_STATISTICS
  10179. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10180. #endif
  10181. }
  10182. #ifdef HAVE_CURVE448_SHARED_SECRET
  10183. void bench_curve448KeyAgree(void)
  10184. {
  10185. curve448_key genKey, genKey2;
  10186. double start;
  10187. int ret, i, count;
  10188. byte shared[56];
  10189. const char**desc = bench_desc_words[lng_index];
  10190. word32 x = 0;
  10191. DECLARE_MULTI_VALUE_STATS_VARS()
  10192. wc_curve448_init(&genKey);
  10193. wc_curve448_init(&genKey2);
  10194. ret = wc_curve448_make_key(&gRng, 56, &genKey);
  10195. if (ret != 0) {
  10196. printf("curve448_make_key failed\n");
  10197. return;
  10198. }
  10199. ret = wc_curve448_make_key(&gRng, 56, &genKey2);
  10200. if (ret != 0) {
  10201. printf("curve448_make_key failed: %d\n", ret);
  10202. wc_curve448_free(&genKey);
  10203. return;
  10204. }
  10205. /* Shared secret */
  10206. bench_stats_start(&count, &start);
  10207. do {
  10208. for (i = 0; i < agreeTimes; i++) {
  10209. x = sizeof(shared);
  10210. ret = wc_curve448_shared_secret(&genKey, &genKey2, shared, &x);
  10211. if (ret != 0) {
  10212. printf("curve448_shared_secret failed: %d\n", ret);
  10213. goto exit;
  10214. }
  10215. RECORD_MULTI_VALUE_STATS();
  10216. }
  10217. count += i;
  10218. } while (bench_stats_check(start)
  10219. #ifdef MULTI_VALUE_STATISTICS
  10220. || runs < minimum_runs
  10221. #endif
  10222. );
  10223. exit:
  10224. bench_stats_asym_finish("CURVE", 448, desc[3], 0, count, start, ret);
  10225. #ifdef MULTI_VALUE_STATISTICS
  10226. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10227. #endif
  10228. wc_curve448_free(&genKey2);
  10229. wc_curve448_free(&genKey);
  10230. }
  10231. #endif /* HAVE_CURVE448_SHARED_SECRET */
  10232. #endif /* HAVE_CURVE448 */
  10233. #ifdef HAVE_ED448
  10234. void bench_ed448KeyGen(void)
  10235. {
  10236. ed448_key genKey;
  10237. double start;
  10238. int i, count;
  10239. const char**desc = bench_desc_words[lng_index];
  10240. DECLARE_MULTI_VALUE_STATS_VARS()
  10241. /* Key Gen */
  10242. bench_stats_start(&count, &start);
  10243. do {
  10244. for (i = 0; i < genTimes; i++) {
  10245. wc_ed448_init(&genKey);
  10246. (void)wc_ed448_make_key(&gRng, ED448_KEY_SIZE, &genKey);
  10247. wc_ed448_free(&genKey);
  10248. RECORD_MULTI_VALUE_STATS();
  10249. }
  10250. count += i;
  10251. } while (bench_stats_check(start)
  10252. #ifdef MULTI_VALUE_STATISTICS
  10253. || runs < minimum_runs
  10254. #endif
  10255. );
  10256. bench_stats_asym_finish("ED", 448, desc[2], 0, count, start, 0);
  10257. #ifdef MULTI_VALUE_STATISTICS
  10258. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10259. #endif
  10260. }
  10261. void bench_ed448KeySign(void)
  10262. {
  10263. int ret;
  10264. WC_DECLARE_VAR(genKey, ed448_key, 1, HEAP_HINT);
  10265. #ifdef HAVE_ED448_SIGN
  10266. double start;
  10267. int i, count;
  10268. byte sig[ED448_SIG_SIZE];
  10269. byte msg[512];
  10270. word32 x = 0;
  10271. const char**desc = bench_desc_words[lng_index];
  10272. DECLARE_MULTI_VALUE_STATS_VARS()
  10273. #endif
  10274. WC_ALLOC_VAR(genKey, ed448_key, 1, HEAP_HINT);
  10275. wc_ed448_init(genKey);
  10276. ret = wc_ed448_make_key(&gRng, ED448_KEY_SIZE, genKey);
  10277. if (ret != 0) {
  10278. printf("ed448_make_key failed\n");
  10279. goto exit;
  10280. }
  10281. #ifdef HAVE_ED448_SIGN
  10282. /* make dummy msg */
  10283. for (i = 0; i < (int)sizeof(msg); i++)
  10284. msg[i] = (byte)i;
  10285. bench_stats_start(&count, &start);
  10286. do {
  10287. for (i = 0; i < agreeTimes; i++) {
  10288. x = sizeof(sig);
  10289. ret = wc_ed448_sign_msg(msg, sizeof(msg), sig, &x, genKey,
  10290. NULL, 0);
  10291. if (ret != 0) {
  10292. printf("ed448_sign_msg failed\n");
  10293. goto exit;
  10294. }
  10295. RECORD_MULTI_VALUE_STATS();
  10296. }
  10297. count += i;
  10298. } while (bench_stats_check(start)
  10299. #ifdef MULTI_VALUE_STATISTICS
  10300. || runs < minimum_runs
  10301. #endif
  10302. );
  10303. bench_stats_asym_finish("ED", 448, desc[4], 0, count, start, ret);
  10304. #ifdef MULTI_VALUE_STATISTICS
  10305. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10306. #endif
  10307. RESET_MULTI_VALUE_STATS_VARS();
  10308. #ifdef HAVE_ED448_VERIFY
  10309. bench_stats_start(&count, &start);
  10310. do {
  10311. for (i = 0; i < agreeTimes; i++) {
  10312. int verify = 0;
  10313. ret = wc_ed448_verify_msg(sig, x, msg, sizeof(msg), &verify,
  10314. genKey, NULL, 0);
  10315. if (ret != 0 || verify != 1) {
  10316. printf("ed448_verify_msg failed\n");
  10317. goto exit;
  10318. }
  10319. RECORD_MULTI_VALUE_STATS();
  10320. }
  10321. count += i;
  10322. } while (bench_stats_check(start)
  10323. #ifdef MULTI_VALUE_STATISTICS
  10324. || runs < minimum_runs
  10325. #endif
  10326. );
  10327. bench_stats_asym_finish("ED", 448, desc[5], 0, count, start, ret);
  10328. #ifdef MULTI_VALUE_STATISTICS
  10329. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10330. #endif
  10331. #endif /* HAVE_ED448_VERIFY */
  10332. #endif /* HAVE_ED448_SIGN */
  10333. exit:
  10334. wc_ed448_free(genKey);
  10335. WC_FREE_VAR(genKey, HEAP_HINT);
  10336. }
  10337. #endif /* HAVE_ED448 */
  10338. #ifdef WOLFCRYPT_HAVE_ECCSI
  10339. #ifdef WOLFCRYPT_ECCSI_KMS
  10340. void bench_eccsiKeyGen(void)
  10341. {
  10342. WC_DECLARE_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10343. double start;
  10344. int i, count;
  10345. const char**desc = bench_desc_words[lng_index];
  10346. int ret;
  10347. DECLARE_MULTI_VALUE_STATS_VARS()
  10348. WC_ALLOC_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10349. /* Key Gen */
  10350. bench_stats_start(&count, &start);
  10351. do {
  10352. for (i = 0; i < genTimes; i++) {
  10353. wc_InitEccsiKey(genKey, NULL, INVALID_DEVID);
  10354. ret = wc_MakeEccsiKey(genKey, &gRng);
  10355. wc_FreeEccsiKey(genKey);
  10356. if (ret != 0) {
  10357. printf("wc_MakeEccsiKey failed: %d\n", ret);
  10358. goto exit;
  10359. }
  10360. RECORD_MULTI_VALUE_STATS();
  10361. }
  10362. count += i;
  10363. } while (bench_stats_check(start)
  10364. #ifdef MULTI_VALUE_STATISTICS
  10365. || runs < minimum_runs
  10366. #endif
  10367. );
  10368. bench_stats_asym_finish("ECCSI", 256, desc[2], 0, count, start, 0);
  10369. #ifdef MULTI_VALUE_STATISTICS
  10370. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10371. #endif
  10372. exit:
  10373. WC_FREE_VAR(genKey, HEAP_HINT);
  10374. }
  10375. void bench_eccsiPairGen(void)
  10376. {
  10377. WC_DECLARE_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10378. double start;
  10379. int i, count;
  10380. const char**desc = bench_desc_words[lng_index];
  10381. WC_DECLARE_VAR(ssk, mp_int, 1, HEAP_HINT);
  10382. ecc_point* pvt;
  10383. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10384. int ret;
  10385. DECLARE_MULTI_VALUE_STATS_VARS()
  10386. WC_ALLOC_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10387. WC_ALLOC_VAR(ssk, mp_int, 1, HEAP_HINT);
  10388. (void)mp_init(ssk);
  10389. pvt = wc_ecc_new_point();
  10390. wc_InitEccsiKey(genKey, NULL, INVALID_DEVID);
  10391. (void)wc_MakeEccsiKey(genKey, &gRng);
  10392. /* RSK Gen */
  10393. bench_stats_start(&count, &start);
  10394. do {
  10395. for (i = 0; i < genTimes; i++) {
  10396. ret = wc_MakeEccsiPair(genKey, &gRng, WC_HASH_TYPE_SHA256, id,
  10397. sizeof(id), ssk, pvt);
  10398. if (ret != 0) {
  10399. printf("wc_MakeEccsiPair failed: %d\n", ret);
  10400. goto exit;
  10401. }
  10402. RECORD_MULTI_VALUE_STATS();
  10403. }
  10404. count += i;
  10405. } while (bench_stats_check(start)
  10406. #ifdef MULTI_VALUE_STATISTICS
  10407. || runs < minimum_runs
  10408. #endif
  10409. );
  10410. bench_stats_asym_finish("ECCSI", 256, desc[12], 0, count, start, 0);
  10411. #ifdef MULTI_VALUE_STATISTICS
  10412. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10413. #endif
  10414. wc_FreeEccsiKey(genKey);
  10415. wc_ecc_del_point(pvt);
  10416. mp_free(ssk);
  10417. exit:
  10418. WC_FREE_VAR(genKey, HEAP_HINT);
  10419. WC_FREE_VAR(ssk, HEAP_HINT);
  10420. }
  10421. #endif
  10422. #ifdef WOLFCRYPT_ECCSI_CLIENT
  10423. void bench_eccsiValidate(void)
  10424. {
  10425. WC_DECLARE_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10426. double start;
  10427. int i, count;
  10428. const char**desc = bench_desc_words[lng_index];
  10429. WC_DECLARE_VAR(ssk, mp_int, 1, HEAP_HINT);
  10430. ecc_point* pvt;
  10431. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10432. int valid;
  10433. int ret;
  10434. DECLARE_MULTI_VALUE_STATS_VARS()
  10435. WC_ALLOC_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10436. WC_ALLOC_VAR(ssk, mp_int, 1, HEAP_HINT);
  10437. (void)mp_init(ssk);
  10438. pvt = wc_ecc_new_point();
  10439. wc_InitEccsiKey(genKey, NULL, INVALID_DEVID);
  10440. (void)wc_MakeEccsiKey(genKey, &gRng);
  10441. (void)wc_MakeEccsiPair(genKey, &gRng, WC_HASH_TYPE_SHA256, id, sizeof(id),
  10442. ssk, pvt);
  10443. /* Validation of RSK */
  10444. bench_stats_start(&count, &start);
  10445. do {
  10446. for (i = 0; i < genTimes; i++) {
  10447. ret = wc_ValidateEccsiPair(genKey, WC_HASH_TYPE_SHA256, id,
  10448. sizeof(id), ssk, pvt, &valid);
  10449. if (ret != 0 || !valid) {
  10450. printf("wc_ValidateEccsiPair failed: %d (valid=%d))\n", ret,
  10451. valid);
  10452. goto exit;
  10453. }
  10454. RECORD_MULTI_VALUE_STATS();
  10455. }
  10456. count += i;
  10457. } while (bench_stats_check(start)
  10458. #ifdef MULTI_VALUE_STATISTICS
  10459. || runs < minimum_runs
  10460. #endif
  10461. );
  10462. bench_stats_asym_finish("ECCSI", 256, desc[11], 0, count, start, 0);
  10463. #ifdef MULTI_VALUE_STATISTICS
  10464. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10465. #endif
  10466. wc_FreeEccsiKey(genKey);
  10467. wc_ecc_del_point(pvt);
  10468. mp_free(ssk);
  10469. exit:
  10470. WC_FREE_VAR(genKey, HEAP_HINT);
  10471. WC_FREE_VAR(ssk, HEAP_HINT);
  10472. }
  10473. void bench_eccsi(void)
  10474. {
  10475. WC_DECLARE_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10476. double start;
  10477. int i, count;
  10478. const char**desc = bench_desc_words[lng_index];
  10479. WC_DECLARE_VAR(ssk, mp_int, 1, HEAP_HINT);
  10480. ecc_point* pvt;
  10481. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10482. static const byte msg[] = { 0x01, 0x23, 0x34, 0x45 };
  10483. byte hash[WC_SHA256_DIGEST_SIZE];
  10484. byte hashSz = (byte)sizeof(hash);
  10485. byte sig[257];
  10486. word32 sigSz = sizeof(sig);
  10487. int ret;
  10488. int verified;
  10489. DECLARE_MULTI_VALUE_STATS_VARS()
  10490. WC_ALLOC_VAR(genKey, EccsiKey, 1, HEAP_HINT);
  10491. WC_ALLOC_VAR(ssk, mp_int, 1, HEAP_HINT);
  10492. (void)mp_init(ssk);
  10493. pvt = wc_ecc_new_point();
  10494. (void)wc_InitEccsiKey(genKey, NULL, INVALID_DEVID);
  10495. (void)wc_MakeEccsiKey(genKey, &gRng);
  10496. (void)wc_MakeEccsiPair(genKey, &gRng, WC_HASH_TYPE_SHA256, id, sizeof(id),
  10497. ssk, pvt);
  10498. (void)wc_HashEccsiId(genKey, WC_HASH_TYPE_SHA256, id, sizeof(id), pvt,
  10499. hash, &hashSz);
  10500. (void)wc_SetEccsiHash(genKey, hash, hashSz);
  10501. (void)wc_SetEccsiPair(genKey, ssk, pvt);
  10502. /* Encapsulate */
  10503. bench_stats_start(&count, &start);
  10504. do {
  10505. for (i = 0; i < genTimes; i++) {
  10506. ret = wc_SignEccsiHash(genKey, &gRng, WC_HASH_TYPE_SHA256, msg,
  10507. sizeof(msg), sig, &sigSz);
  10508. if (ret != 0) {
  10509. printf("wc_SignEccsiHash failed: %d\n", ret);
  10510. break;
  10511. }
  10512. RECORD_MULTI_VALUE_STATS();
  10513. }
  10514. count += i;
  10515. } while (bench_stats_check(start)
  10516. #ifdef MULTI_VALUE_STATISTICS
  10517. || runs < minimum_runs
  10518. #endif
  10519. );
  10520. bench_stats_asym_finish("ECCSI", 256, desc[4], 0, count, start, 0);
  10521. #ifdef MULTI_VALUE_STATISTICS
  10522. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10523. #endif
  10524. RESET_MULTI_VALUE_STATS_VARS();
  10525. /* Derive */
  10526. bench_stats_start(&count, &start);
  10527. do {
  10528. for (i = 0; i < genTimes; i++) {
  10529. ret = wc_VerifyEccsiHash(genKey, WC_HASH_TYPE_SHA256, msg,
  10530. sizeof(msg), sig, sigSz, &verified);
  10531. if (ret != 0 || !verified) {
  10532. printf("wc_VerifyEccsiHash failed: %d (verified: %d)\n", ret,
  10533. verified);
  10534. goto exit;
  10535. }
  10536. RECORD_MULTI_VALUE_STATS();
  10537. }
  10538. count += i;
  10539. } while (bench_stats_check(start)
  10540. #ifdef MULTI_VALUE_STATISTICS
  10541. || runs < minimum_runs
  10542. #endif
  10543. );
  10544. bench_stats_asym_finish("ECCSI", 256, desc[5], 0, count, start, 0);
  10545. #ifdef MULTI_VALUE_STATISTICS
  10546. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10547. #endif
  10548. wc_FreeEccsiKey(genKey);
  10549. wc_ecc_del_point(pvt);
  10550. exit:
  10551. WC_FREE_VAR(genKey, HEAP_HINT);
  10552. WC_FREE_VAR(ssk, HEAP_HINT);
  10553. }
  10554. #endif /* WOLFCRYPT_ECCSI_CLIENT */
  10555. #endif /* WOLFCRYPT_HAVE_ECCSI */
  10556. #ifdef WOLFCRYPT_HAVE_SAKKE
  10557. #ifdef WOLFCRYPT_SAKKE_KMS
  10558. void bench_sakkeKeyGen(void)
  10559. {
  10560. WC_DECLARE_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10561. double start;
  10562. int i, count;
  10563. const char**desc = bench_desc_words[lng_index];
  10564. int ret;
  10565. DECLARE_MULTI_VALUE_STATS_VARS()
  10566. WC_ALLOC_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10567. /* Key Gen */
  10568. bench_stats_start(&count, &start);
  10569. do {
  10570. for (i = 0; i < genTimes; i++) {
  10571. wc_InitSakkeKey_ex(genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  10572. ret = wc_MakeSakkeKey(genKey, &gRng);
  10573. if (ret != 0) {
  10574. printf("wc_MakeSakkeKey failed: %d\n", ret);
  10575. goto exit;
  10576. }
  10577. wc_FreeSakkeKey(genKey);
  10578. RECORD_MULTI_VALUE_STATS();
  10579. }
  10580. count += i;
  10581. } while (bench_stats_check(start)
  10582. #ifdef MULTI_VALUE_STATISTICS
  10583. || runs < minimum_runs
  10584. #endif
  10585. );
  10586. bench_stats_asym_finish("SAKKE", 1024, desc[2], 0, count, start, 0);
  10587. #ifdef MULTI_VALUE_STATISTICS
  10588. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10589. #endif
  10590. exit:
  10591. WC_FREE_VAR(genKey, HEAP_HINT);
  10592. }
  10593. void bench_sakkeRskGen(void)
  10594. {
  10595. WC_DECLARE_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10596. double start;
  10597. int i, count;
  10598. const char**desc = bench_desc_words[lng_index];
  10599. ecc_point* rsk;
  10600. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10601. int ret;
  10602. DECLARE_MULTI_VALUE_STATS_VARS()
  10603. WC_ALLOC_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10604. rsk = wc_ecc_new_point();
  10605. wc_InitSakkeKey_ex(genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  10606. (void)wc_MakeSakkeKey(genKey, &gRng);
  10607. /* RSK Gen */
  10608. bench_stats_start(&count, &start);
  10609. do {
  10610. for (i = 0; i < genTimes; i++) {
  10611. ret = wc_MakeSakkeRsk(genKey, id, sizeof(id), rsk);
  10612. if (ret != 0) {
  10613. printf("wc_MakeSakkeRsk failed: %d\n", ret);
  10614. goto exit;
  10615. }
  10616. RECORD_MULTI_VALUE_STATS();
  10617. }
  10618. count += i;
  10619. } while (bench_stats_check(start)
  10620. #ifdef MULTI_VALUE_STATISTICS
  10621. || runs < minimum_runs
  10622. #endif
  10623. );
  10624. bench_stats_asym_finish("SAKKE", 1024, desc[8], 0, count, start, 0);
  10625. #ifdef MULTI_VALUE_STATISTICS
  10626. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10627. #endif
  10628. wc_FreeSakkeKey(genKey);
  10629. wc_ecc_del_point(rsk);
  10630. exit:
  10631. WC_FREE_VAR(genKey, HEAP_HINT);
  10632. }
  10633. #endif
  10634. #ifdef WOLFCRYPT_SAKKE_CLIENT
  10635. void bench_sakkeValidate(void)
  10636. {
  10637. WC_DECLARE_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10638. double start;
  10639. int i, count;
  10640. const char**desc = bench_desc_words[lng_index];
  10641. ecc_point* rsk;
  10642. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10643. int valid;
  10644. int ret;
  10645. DECLARE_MULTI_VALUE_STATS_VARS()
  10646. WC_ALLOC_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10647. rsk = wc_ecc_new_point();
  10648. (void)wc_InitSakkeKey_ex(genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  10649. (void)wc_MakeSakkeKey(genKey, &gRng);
  10650. (void)wc_MakeSakkeRsk(genKey, id, sizeof(id), rsk);
  10651. (void)wc_ValidateSakkeRsk(genKey, id, sizeof(id), rsk, &valid);
  10652. /* Validation of RSK */
  10653. bench_stats_start(&count, &start);
  10654. do {
  10655. for (i = 0; i < genTimes; i++) {
  10656. ret = wc_ValidateSakkeRsk(genKey, id, sizeof(id), rsk, &valid);
  10657. if (ret != 0 || !valid) {
  10658. printf("wc_ValidateSakkeRsk failed: %d (valid=%d))\n", ret,
  10659. valid);
  10660. goto exit;
  10661. }
  10662. RECORD_MULTI_VALUE_STATS();
  10663. }
  10664. count += i;
  10665. } while (bench_stats_check(start)
  10666. #ifdef MULTI_VALUE_STATISTICS
  10667. || runs < minimum_runs
  10668. #endif
  10669. );
  10670. bench_stats_asym_finish("SAKKE", 1024, desc[11], 0, count, start, 0);
  10671. #ifdef MULTI_VALUE_STATISTICS
  10672. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10673. #endif
  10674. wc_FreeSakkeKey(genKey);
  10675. wc_ecc_del_point(rsk);
  10676. exit:
  10677. WC_FREE_VAR(genKey, HEAP_HINT);
  10678. }
  10679. void bench_sakke(void)
  10680. {
  10681. WC_DECLARE_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10682. double start;
  10683. int i, count;
  10684. const char**desc = bench_desc_words[lng_index];
  10685. ecc_point* rsk;
  10686. static const byte id[] = { 0x01, 0x23, 0x34, 0x45 };
  10687. static const byte ssv_init[] = { 0x01, 0x23, 0x34, 0x45 };
  10688. byte ssv[sizeof(ssv_init)];
  10689. byte derSSV[sizeof(ssv)];
  10690. byte auth[257];
  10691. word16 authSz = sizeof(auth);
  10692. int ret = 0;
  10693. byte* table = NULL;
  10694. word32 len = 0;
  10695. byte* iTable = NULL;
  10696. word32 iTableLen = 0;
  10697. DECLARE_MULTI_VALUE_STATS_VARS()
  10698. WC_ALLOC_VAR(genKey, SakkeKey, 1, HEAP_HINT);
  10699. XMEMCPY(ssv, ssv_init, sizeof ssv);
  10700. rsk = wc_ecc_new_point();
  10701. (void)wc_InitSakkeKey_ex(genKey, 128, ECC_SAKKE_1, NULL, INVALID_DEVID);
  10702. (void)wc_MakeSakkeKey(genKey, &gRng);
  10703. (void)wc_MakeSakkeRsk(genKey, id, sizeof(id), rsk);
  10704. (void)wc_SetSakkeRsk(genKey, rsk, NULL, 0);
  10705. (void)wc_SetSakkeIdentity(genKey, id, sizeof(id));
  10706. /* Encapsulate */
  10707. bench_stats_start(&count, &start);
  10708. do {
  10709. for (i = 0; i < genTimes; i++) {
  10710. ret = wc_MakeSakkeEncapsulatedSSV(genKey,
  10711. WC_HASH_TYPE_SHA256,
  10712. ssv, sizeof(ssv), auth, &authSz);
  10713. if (ret != 0) {
  10714. printf("wc_MakeSakkeEncapsulatedSSV failed: %d\n", ret);
  10715. break;
  10716. }
  10717. RECORD_MULTI_VALUE_STATS();
  10718. } /* for */
  10719. count += i;
  10720. } while (bench_stats_check(start)
  10721. #ifdef MULTI_VALUE_STATISTICS
  10722. || runs < minimum_runs
  10723. #endif
  10724. );
  10725. bench_stats_asym_finish_ex("SAKKE", 1024, desc[9], "-1",
  10726. 0, count, start, 0);
  10727. #ifdef MULTI_VALUE_STATISTICS
  10728. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10729. #endif
  10730. RESET_MULTI_VALUE_STATS_VARS();
  10731. /* Derive */
  10732. bench_stats_start(&count, &start);
  10733. do {
  10734. for (i = 0; i < genTimes; i++) {
  10735. XMEMCPY(derSSV, ssv, sizeof(ssv));
  10736. ret = wc_DeriveSakkeSSV(genKey, WC_HASH_TYPE_SHA256, derSSV,
  10737. sizeof(derSSV), auth, authSz);
  10738. if (ret != 0) {
  10739. printf("wc_DeriveSakkeSSV failed: %d\n", ret);
  10740. break;
  10741. }
  10742. RECORD_MULTI_VALUE_STATS();
  10743. }
  10744. if (ret != 0) break;
  10745. count += i;
  10746. } while (bench_stats_check(start)
  10747. #ifdef MULTI_VALUE_STATISTICS
  10748. || runs < minimum_runs
  10749. #endif
  10750. );
  10751. bench_stats_asym_finish_ex("SAKKE", 1024, desc[10], "-1",
  10752. 0, count, start, 0);
  10753. #ifdef MULTI_VALUE_STATISTICS
  10754. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10755. #endif
  10756. /* Calculate Point I and generate table. */
  10757. (void)wc_MakeSakkePointI(genKey, id, sizeof(id));
  10758. iTableLen = 0;
  10759. (void)wc_GenerateSakkePointITable(genKey, NULL, &iTableLen);
  10760. if (iTableLen != 0) {
  10761. iTable = (byte*)XMALLOC(iTableLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10762. if (iTable == NULL)
  10763. WC_ALLOC_DO_ON_FAILURE();
  10764. (void)wc_GenerateSakkePointITable(genKey, iTable, &iTableLen);
  10765. }
  10766. /* Encapsulate with Point I table */
  10767. bench_stats_start(&count, &start);
  10768. do {
  10769. for (i = 0; i < genTimes; i++) {
  10770. ret = wc_MakeSakkeEncapsulatedSSV(genKey,
  10771. WC_HASH_TYPE_SHA256, ssv,
  10772. sizeof(ssv), auth, &authSz);
  10773. if (ret != 0) {
  10774. printf("wc_MakeSakkeEncapsulatedSSV failed: %d\n", ret);
  10775. break;
  10776. }
  10777. RECORD_MULTI_VALUE_STATS();
  10778. }
  10779. count += i;
  10780. } while (bench_stats_check(start)
  10781. #ifdef MULTI_VALUE_STATISTICS
  10782. || runs < minimum_runs
  10783. #endif
  10784. );
  10785. bench_stats_asym_finish_ex("SAKKE", 1024, desc[9], "-2", 0,
  10786. count, start, 0);
  10787. #ifdef MULTI_VALUE_STATISTICS
  10788. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10789. #endif
  10790. RESET_MULTI_VALUE_STATS_VARS();
  10791. (void)wc_SetSakkeRsk(genKey, rsk, table, len);
  10792. /* Derive with Point I table */
  10793. bench_stats_start(&count, &start);
  10794. do {
  10795. for (i = 0; i < genTimes; i++) {
  10796. XMEMCPY(derSSV, ssv, sizeof(ssv));
  10797. ret = wc_DeriveSakkeSSV(genKey, WC_HASH_TYPE_SHA256, derSSV,
  10798. sizeof(derSSV), auth, authSz);
  10799. if (ret != 0) {
  10800. printf("wc_DeriveSakkeSSV failed: %d\n", ret);
  10801. break;
  10802. }
  10803. RECORD_MULTI_VALUE_STATS();
  10804. }
  10805. if (ret != 0) break;
  10806. count += i;
  10807. } while (bench_stats_check(start)
  10808. #ifdef MULTI_VALUE_STATISTICS
  10809. || runs < minimum_runs
  10810. #endif
  10811. );
  10812. bench_stats_asym_finish_ex("SAKKE", 1024, desc[10], "-2", 0,
  10813. count, start, 0);
  10814. #ifdef MULTI_VALUE_STATISTICS
  10815. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10816. #endif
  10817. RESET_MULTI_VALUE_STATS_VARS();
  10818. len = 0;
  10819. (void)wc_GenerateSakkeRskTable(genKey, rsk, NULL, &len);
  10820. if (len > 0) {
  10821. table = (byte*)XMALLOC(len, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10822. if (table == NULL)
  10823. WC_ALLOC_DO_ON_FAILURE();
  10824. (void)wc_GenerateSakkeRskTable(genKey, rsk, table, &len);
  10825. }
  10826. (void)wc_SetSakkeRsk(genKey, rsk, table, len);
  10827. /* Derive with Point I table and RSK table */
  10828. bench_stats_start(&count, &start);
  10829. do {
  10830. for (i = 0; i < genTimes; i++) {
  10831. XMEMCPY(derSSV, ssv, sizeof(ssv));
  10832. ret = wc_DeriveSakkeSSV(genKey, WC_HASH_TYPE_SHA256, derSSV,
  10833. sizeof(derSSV), auth, authSz);
  10834. if (ret != 0) {
  10835. printf("wc_DeriveSakkeSSV failed: %d\n", ret);
  10836. break;
  10837. }
  10838. RECORD_MULTI_VALUE_STATS();
  10839. }
  10840. if (ret != 0) break;
  10841. count += i;
  10842. } while (bench_stats_check(start)
  10843. #ifdef MULTI_VALUE_STATISTICS
  10844. || runs < minimum_runs
  10845. #endif
  10846. );
  10847. bench_stats_asym_finish_ex("SAKKE", 1024, desc[10], "-3",
  10848. 0, count, start, 0);
  10849. #ifdef MULTI_VALUE_STATISTICS
  10850. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10851. #endif
  10852. RESET_MULTI_VALUE_STATS_VARS();
  10853. wc_ClearSakkePointITable(genKey);
  10854. /* Derive with RSK table */
  10855. bench_stats_start(&count, &start);
  10856. do {
  10857. for (i = 0; i < genTimes; i++) {
  10858. XMEMCPY(derSSV, ssv, sizeof(ssv));
  10859. ret = wc_DeriveSakkeSSV(genKey, WC_HASH_TYPE_SHA256, derSSV,
  10860. sizeof(derSSV), auth, authSz);
  10861. if (ret != 0) {
  10862. printf("wc_DeriveSakkeSSV failed: %d\n", ret);
  10863. break;
  10864. }
  10865. RECORD_MULTI_VALUE_STATS();
  10866. }
  10867. if (ret != 0) break;
  10868. count += i;
  10869. } while (bench_stats_check(start)
  10870. #ifdef MULTI_VALUE_STATISTICS
  10871. || runs < minimum_runs
  10872. #endif
  10873. );
  10874. bench_stats_asym_finish_ex("SAKKE", 1024, desc[10], "-4", 0,
  10875. count, start, 0);
  10876. #ifdef MULTI_VALUE_STATISTICS
  10877. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10878. #endif
  10879. wc_FreeSakkeKey(genKey);
  10880. wc_ecc_del_point(rsk);
  10881. exit:
  10882. if (iTable)
  10883. XFREE(iTable, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10884. if (table)
  10885. XFREE(table, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  10886. WC_FREE_VAR(genKey, HEAP_HINT);
  10887. }
  10888. #endif /* WOLFCRYPT_SAKKE_CLIENT */
  10889. #endif /* WOLFCRYPT_HAVE_SAKKE */
  10890. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  10891. #ifdef HAVE_FALCON
  10892. void bench_falconKeySign(byte level)
  10893. {
  10894. int ret = 0;
  10895. falcon_key key;
  10896. double start;
  10897. int i, count;
  10898. byte sig[FALCON_MAX_SIG_SIZE];
  10899. byte msg[512];
  10900. word32 x = 0;
  10901. const char**desc = bench_desc_words[lng_index];
  10902. DECLARE_MULTI_VALUE_STATS_VARS()
  10903. ret = wc_falcon_init(&key);
  10904. if (ret != 0) {
  10905. printf("wc_falcon_init failed %d\n", ret);
  10906. return;
  10907. }
  10908. ret = wc_falcon_set_level(&key, level);
  10909. if (ret != 0) {
  10910. printf("wc_falcon_set_level failed %d\n", ret);
  10911. }
  10912. if (ret == 0) {
  10913. if (level == 1) {
  10914. ret = wc_falcon_import_private_key(bench_falcon_level1_key,
  10915. sizeof_bench_falcon_level1_key,
  10916. NULL, 0, &key);
  10917. }
  10918. else {
  10919. ret = wc_falcon_import_private_key(bench_falcon_level5_key,
  10920. sizeof_bench_falcon_level5_key,
  10921. NULL, 0, &key);
  10922. }
  10923. if (ret != 0) {
  10924. printf("wc_falcon_import_private_key failed %d\n", ret);
  10925. }
  10926. }
  10927. /* make dummy msg */
  10928. for (i = 0; i < (int)sizeof(msg); i++) {
  10929. msg[i] = (byte)i;
  10930. }
  10931. bench_stats_start(&count, &start);
  10932. do {
  10933. for (i = 0; i < agreeTimes; i++) {
  10934. if (ret == 0) {
  10935. if (level == 1) {
  10936. x = FALCON_LEVEL1_SIG_SIZE;
  10937. }
  10938. else {
  10939. x = FALCON_LEVEL5_SIG_SIZE;
  10940. }
  10941. ret = wc_falcon_sign_msg(msg, sizeof(msg), sig, &x, &key, GLOBAL_RNG);
  10942. if (ret != 0) {
  10943. printf("wc_falcon_sign_msg failed\n");
  10944. }
  10945. }
  10946. RECORD_MULTI_VALUE_STATS();
  10947. }
  10948. count += i;
  10949. } while (bench_stats_check(start)
  10950. #ifdef MULTI_VALUE_STATISTICS
  10951. || runs < minimum_runs
  10952. #endif
  10953. );
  10954. if (ret == 0) {
  10955. bench_stats_asym_finish("FALCON", level, desc[4], 0,
  10956. count, start, ret);
  10957. #ifdef MULTI_VALUE_STATISTICS
  10958. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10959. #endif
  10960. }
  10961. RESET_MULTI_VALUE_STATS_VARS();
  10962. bench_stats_start(&count, &start);
  10963. do {
  10964. for (i = 0; i < agreeTimes; i++) {
  10965. if (ret == 0) {
  10966. int verify = 0;
  10967. ret = wc_falcon_verify_msg(sig, x, msg, sizeof(msg), &verify,
  10968. &key);
  10969. if (ret != 0 || verify != 1) {
  10970. printf("wc_falcon_verify_msg failed %d, verify %d\n",
  10971. ret, verify);
  10972. ret = -1;
  10973. }
  10974. }
  10975. RECORD_MULTI_VALUE_STATS();
  10976. }
  10977. count += i;
  10978. } while (bench_stats_check(start)
  10979. #ifdef MULTI_VALUE_STATISTICS
  10980. || runs < minimum_runs
  10981. #endif
  10982. );
  10983. if (ret == 0) {
  10984. bench_stats_asym_finish("FALCON", level, desc[5],
  10985. 0, count, start, ret);
  10986. #ifdef MULTI_VALUE_STATISTICS
  10987. bench_multi_value_stats(max, min, sum, squareSum, runs);
  10988. #endif
  10989. }
  10990. wc_falcon_free(&key);
  10991. }
  10992. #endif /* HAVE_FALCON */
  10993. #ifdef HAVE_DILITHIUM
  10994. void bench_dilithiumKeySign(byte level)
  10995. {
  10996. int ret = 0;
  10997. dilithium_key key;
  10998. double start;
  10999. int i, count;
  11000. byte sig[DILITHIUM_MAX_SIG_SIZE];
  11001. byte msg[512];
  11002. word32 x = 0;
  11003. const char**desc = bench_desc_words[lng_index];
  11004. DECLARE_MULTI_VALUE_STATS_VARS()
  11005. ret = wc_dilithium_init(&key);
  11006. if (ret != 0) {
  11007. printf("wc_dilithium_init failed %d\n", ret);
  11008. return;
  11009. }
  11010. ret = wc_dilithium_set_level(&key, level);
  11011. if (ret != 0) {
  11012. printf("wc_dilithium_set_level() failed %d\n", ret);
  11013. }
  11014. if (ret == 0) {
  11015. ret = -1;
  11016. if (level == 2) {
  11017. ret = wc_dilithium_import_private_key(bench_dilithium_level2_key,
  11018. sizeof_bench_dilithium_level2_key, NULL, 0, &key);
  11019. }
  11020. else if (level == 3) {
  11021. ret = wc_dilithium_import_private_key(bench_dilithium_level3_key,
  11022. sizeof_bench_dilithium_level3_key, NULL, 0, &key);
  11023. }
  11024. else if (level == 5) {
  11025. ret = wc_dilithium_import_private_key(bench_dilithium_level5_key,
  11026. sizeof_bench_dilithium_level5_key, NULL, 0, &key);
  11027. }
  11028. if (ret != 0) {
  11029. printf("wc_dilithium_import_private_key failed %d\n", ret);
  11030. }
  11031. }
  11032. /* make dummy msg */
  11033. for (i = 0; i < (int)sizeof(msg); i++) {
  11034. msg[i] = (byte)i;
  11035. }
  11036. bench_stats_start(&count, &start);
  11037. do {
  11038. for (i = 0; i < agreeTimes; i++) {
  11039. if (ret == 0) {
  11040. if (level == 2) {
  11041. x = DILITHIUM_LEVEL2_SIG_SIZE;
  11042. }
  11043. else if (level == 3) {
  11044. x = DILITHIUM_LEVEL3_SIG_SIZE;
  11045. }
  11046. else {
  11047. x = DILITHIUM_LEVEL5_SIG_SIZE;
  11048. }
  11049. ret = wc_dilithium_sign_msg(msg, sizeof(msg), sig, &x, &key, GLOBAL_RNG);
  11050. if (ret != 0) {
  11051. printf("wc_dilithium_sign_msg failed\n");
  11052. }
  11053. }
  11054. RECORD_MULTI_VALUE_STATS();
  11055. }
  11056. count += i;
  11057. } while (bench_stats_check(start)
  11058. #ifdef MULTI_VALUE_STATISTICS
  11059. || runs < minimum_runs
  11060. #endif
  11061. );
  11062. if (ret == 0) {
  11063. bench_stats_asym_finish("DILITHIUM", level, desc[4], 0, count, start,
  11064. ret);
  11065. #ifdef MULTI_VALUE_STATISTICS
  11066. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11067. #endif
  11068. }
  11069. RESET_MULTI_VALUE_STATS_VARS();
  11070. bench_stats_start(&count, &start);
  11071. do {
  11072. for (i = 0; i < agreeTimes; i++) {
  11073. if (ret == 0) {
  11074. int verify = 0;
  11075. ret = wc_dilithium_verify_msg(sig, x, msg, sizeof(msg),
  11076. &verify, &key);
  11077. if (ret != 0 || verify != 1) {
  11078. printf("wc_dilithium_verify_msg failed %d, verify %d\n",
  11079. ret, verify);
  11080. ret = -1;
  11081. }
  11082. }
  11083. RECORD_MULTI_VALUE_STATS();
  11084. }
  11085. count += i;
  11086. } while (bench_stats_check(start)
  11087. #ifdef MULTI_VALUE_STATISTICS
  11088. || runs < minimum_runs
  11089. #endif
  11090. );
  11091. if (ret == 0) {
  11092. bench_stats_asym_finish("DILITHIUM", level, desc[5], 0, count, start,
  11093. ret);
  11094. #ifdef MULTI_VALUE_STATISTICS
  11095. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11096. #endif
  11097. }
  11098. wc_dilithium_free(&key);
  11099. }
  11100. #endif /* HAVE_DILITHIUM */
  11101. #ifdef HAVE_SPHINCS
  11102. void bench_sphincsKeySign(byte level, byte optim)
  11103. {
  11104. int ret = 0;
  11105. sphincs_key key;
  11106. double start;
  11107. int i, count;
  11108. byte sig[SPHINCS_MAX_SIG_SIZE];
  11109. byte msg[512];
  11110. word32 x = 0;
  11111. const char**desc = bench_desc_words[lng_index];
  11112. DECLARE_MULTI_VALUE_STATS_VARS()
  11113. ret = wc_sphincs_init(&key);
  11114. if (ret != 0) {
  11115. printf("wc_sphincs_init failed %d\n", ret);
  11116. return;
  11117. }
  11118. ret = wc_sphincs_set_level_and_optim(&key, level, optim);
  11119. if (ret != 0) {
  11120. printf("wc_sphincs_set_level_and_optim() failed %d\n", ret);
  11121. }
  11122. if (ret == 0) {
  11123. ret = -1;
  11124. if ((level == 1) && (optim == FAST_VARIANT)) {
  11125. ret = wc_sphincs_import_private_key(bench_sphincs_fast_level1_key,
  11126. sizeof_bench_sphincs_fast_level1_key, NULL, 0, &key);
  11127. }
  11128. else if ((level == 3) && (optim == FAST_VARIANT)) {
  11129. ret = wc_sphincs_import_private_key(bench_sphincs_fast_level3_key,
  11130. sizeof_bench_sphincs_fast_level3_key, NULL, 0, &key);
  11131. }
  11132. else if ((level == 5) && (optim == FAST_VARIANT)) {
  11133. ret = wc_sphincs_import_private_key(bench_sphincs_fast_level5_key,
  11134. sizeof_bench_sphincs_fast_level5_key, NULL, 0, &key);
  11135. }
  11136. else if ((level == 1) && (optim == SMALL_VARIANT)) {
  11137. ret = wc_sphincs_import_private_key(
  11138. bench_sphincs_small_level1_key,
  11139. sizeof_bench_sphincs_small_level1_key, NULL, 0, &key);
  11140. }
  11141. else if ((level == 3) && (optim == SMALL_VARIANT)) {
  11142. ret = wc_sphincs_import_private_key(
  11143. bench_sphincs_small_level3_key,
  11144. sizeof_bench_sphincs_small_level3_key, NULL, 0, &key);
  11145. }
  11146. else if ((level == 5) && (optim == SMALL_VARIANT)) {
  11147. ret = wc_sphincs_import_private_key(
  11148. bench_sphincs_small_level5_key,
  11149. sizeof_bench_sphincs_small_level5_key, NULL, 0, &key);
  11150. }
  11151. if (ret != 0) {
  11152. printf("wc_sphincs_import_private_key failed %d\n", ret);
  11153. }
  11154. }
  11155. /* make dummy msg */
  11156. for (i = 0; i < (int)sizeof(msg); i++) {
  11157. msg[i] = (byte)i;
  11158. }
  11159. bench_stats_start(&count, &start);
  11160. do {
  11161. for (i = 0; i < agreeTimes; i++) {
  11162. if (ret == 0) {
  11163. if ((level == 1) && (optim == FAST_VARIANT)) {
  11164. x = SPHINCS_FAST_LEVEL1_SIG_SIZE;
  11165. }
  11166. else if ((level == 3) && (optim == FAST_VARIANT)) {
  11167. x = SPHINCS_FAST_LEVEL3_SIG_SIZE;
  11168. }
  11169. else if ((level == 5) && (optim == FAST_VARIANT)) {
  11170. x = SPHINCS_FAST_LEVEL5_SIG_SIZE;
  11171. }
  11172. else if ((level == 1) && (optim == SMALL_VARIANT)) {
  11173. x = SPHINCS_SMALL_LEVEL1_SIG_SIZE;
  11174. }
  11175. else if ((level == 3) && (optim == SMALL_VARIANT)) {
  11176. x = SPHINCS_SMALL_LEVEL3_SIG_SIZE;
  11177. }
  11178. else if ((level == 5) && (optim == SMALL_VARIANT)) {
  11179. x = SPHINCS_SMALL_LEVEL5_SIG_SIZE;
  11180. }
  11181. ret = wc_sphincs_sign_msg(msg, sizeof(msg), sig, &x, &key, GLOBAL_RNG);
  11182. if (ret != 0) {
  11183. printf("wc_sphincs_sign_msg failed\n");
  11184. }
  11185. }
  11186. RECORD_MULTI_VALUE_STATS();
  11187. }
  11188. count += i;
  11189. } while (bench_stats_check(start)
  11190. #ifdef MULTI_VALUE_STATISTICS
  11191. || runs < minimum_runs
  11192. #endif
  11193. );
  11194. if (ret == 0) {
  11195. if (optim == FAST_VARIANT) {
  11196. bench_stats_asym_finish("SPHINCS-FAST", level, desc[4], 0, count,
  11197. start, ret);
  11198. }
  11199. else {
  11200. bench_stats_asym_finish("SPHINCS-SMALL", level, desc[4], 0, count,
  11201. start, ret);
  11202. }
  11203. #ifdef MULTI_VALUE_STATISTICS
  11204. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11205. #endif
  11206. }
  11207. RESET_MULTI_VALUE_STATS_VARS();
  11208. bench_stats_start(&count, &start);
  11209. do {
  11210. for (i = 0; i < agreeTimes; i++) {
  11211. if (ret == 0) {
  11212. int verify = 0;
  11213. ret = wc_sphincs_verify_msg(sig, x, msg, sizeof(msg), &verify,
  11214. &key);
  11215. if (ret != 0 || verify != 1) {
  11216. printf("wc_sphincs_verify_msg failed %d, verify %d\n",
  11217. ret, verify);
  11218. ret = -1;
  11219. }
  11220. }
  11221. RECORD_MULTI_VALUE_STATS();
  11222. }
  11223. count += i;
  11224. } while (bench_stats_check(start)
  11225. #ifdef MULTI_VALUE_STATISTICS
  11226. || runs < minimum_runs
  11227. #endif
  11228. );
  11229. if (ret == 0) {
  11230. if (optim == FAST_VARIANT) {
  11231. bench_stats_asym_finish("SPHINCS-FAST", level, desc[5], 0, count,
  11232. start, ret);
  11233. }
  11234. else {
  11235. bench_stats_asym_finish("SPHINCS-SMALL", level, desc[5], 0, count,
  11236. start, ret);
  11237. }
  11238. #ifdef MULTI_VALUE_STATISTICS
  11239. bench_multi_value_stats(max, min, sum, squareSum, runs);
  11240. #endif
  11241. }
  11242. wc_sphincs_free(&key);
  11243. }
  11244. #endif /* HAVE_SPHINCS */
  11245. #endif /* HAVE_PQC */
  11246. #if defined(_WIN32) && !defined(INTIME_RTOS)
  11247. #define WIN32_LEAN_AND_MEAN
  11248. #include <windows.h>
  11249. double current_time(int reset)
  11250. {
  11251. static int init = 0;
  11252. static LARGE_INTEGER freq;
  11253. LARGE_INTEGER count;
  11254. (void)reset;
  11255. if (!init) {
  11256. QueryPerformanceFrequency(&freq);
  11257. init = 1;
  11258. }
  11259. QueryPerformanceCounter(&count);
  11260. #ifdef BENCH_MICROSECOND
  11261. return ((double)count.QuadPart * 1000000) / freq.QuadPart;
  11262. #else
  11263. return (double)count.QuadPart / freq.QuadPart;
  11264. #endif
  11265. }
  11266. #elif defined MICROCHIP_PIC32
  11267. #if defined(WOLFSSL_MICROCHIP_PIC32MZ)
  11268. #define CLOCK 80000000.0
  11269. #else
  11270. #define CLOCK 40000000.0
  11271. #endif
  11272. extern void WriteCoreTimer(word32 t);
  11273. extern word32 ReadCoreTimer(void);
  11274. double current_time(int reset)
  11275. {
  11276. unsigned int ns;
  11277. if (reset) {
  11278. WriteCoreTimer(0);
  11279. }
  11280. /* get timer in ns */
  11281. ns = ReadCoreTimer();
  11282. /* return seconds as a double */
  11283. return ( ns / CLOCK * 2.0);
  11284. }
  11285. #elif defined(WOLFSSL_IAR_ARM_TIME) || defined (WOLFSSL_MDK_ARM) || \
  11286. defined(WOLFSSL_USER_CURRTIME) || defined(WOLFSSL_CURRTIME_REMAP)
  11287. /* declared above at line 239 */
  11288. /* extern double current_time(int reset); */
  11289. #elif defined(FREERTOS)
  11290. #include "task.h"
  11291. #if defined(WOLFSSL_ESPIDF)
  11292. /* prototype definition */
  11293. int construct_argv();
  11294. extern char* __argv[22];
  11295. /* current_time(reset)
  11296. *
  11297. * Benchmark passage of time, in fractional seconds.
  11298. * [reset] is non zero to adjust timer or counter to zero
  11299. *
  11300. * Use care when repeatedly calling calling. See implementation. */
  11301. double current_time(int reset)
  11302. {
  11303. double ret;
  11304. #if ESP_IDF_VERSION_MAJOR >= 4
  11305. TickType_t tickCount; /* typically 32 bit, local FreeRTOS ticks */
  11306. #else
  11307. portTickType tickCount;
  11308. #endif
  11309. #if defined(__XTENSA__)
  11310. (void)reset;
  11311. if (reset) {
  11312. /* TODO: Determine a mechanism for reset that does not interfere
  11313. * with freeRTOS tick. Using this code for Xtensa appears to cause
  11314. * RTOS tick timer to stick. See "last_tickCount unchanged".
  11315. ESP_LOGW(TAG, "Current_time() reset!");
  11316. portTICK_TYPE_ENTER_CRITICAL();
  11317. {
  11318. esp_cpu_set_cycle_count((esp_cpu_cycle_count_t)0);
  11319. _esp_cpu_count_last = xthal_get_ccount();
  11320. _esp_cpu_count_last = esp_cpu_get_cycle_count();
  11321. }
  11322. portTICK_TYPE_EXIT_CRITICAL();
  11323. */
  11324. }
  11325. #else
  11326. /* Only reset the CPU counter for RISC-V */
  11327. if (reset) {
  11328. ESP_LOGV(TAG, "current_time() reset!");
  11329. /* TODO: why does Espressif esp_cpu_get_cycle_count() cause
  11330. * unexpected rollovers in return values for Xtensa but not RISC-V?
  11331. * See also esp_get_cycle_count_ex() */
  11332. #ifdef __XTENSA__
  11333. _esp_cpu_count_last = xthal_get_ccount();
  11334. #else
  11335. esp_cpu_set_cycle_count((esp_cpu_cycle_count_t)0);
  11336. _esp_cpu_count_last = esp_cpu_get_cycle_count();
  11337. #endif
  11338. }
  11339. #endif
  11340. /* tick count == ms, if configTICK_RATE_HZ is set to 1000 */
  11341. tickCount = xTaskGetTickCount(); /* RTOS ticks, not CPU cycles!
  11342. The count of ticks since vTaskStartScheduler was called,
  11343. typiclly in app_startup.c */
  11344. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  11345. ESP_LOGV(TAG, "tickCount = %lu", tickCount);
  11346. if (tickCount == last_tickCount) {
  11347. ESP_LOGW(TAG, "last_tickCount unchanged? %lu", tickCount);
  11348. }
  11349. if (tickCount < last_tickCount) {
  11350. ESP_LOGW(TAG, "last_tickCount overflow?");
  11351. }
  11352. #endif
  11353. if (reset) {
  11354. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  11355. ESP_LOGW(TAG, "Assign last_tickCount = %lu", tickCount);
  11356. #endif
  11357. last_tickCount = tickCount;
  11358. }
  11359. else {
  11360. #ifdef DEBUG_WOLFSSL_BENCHMARK_TIMING
  11361. ESP_LOGW(TAG, "No Reset last_tickCount = %lu", tickCount);
  11362. #endif
  11363. }
  11364. #if defined(configTICK_RATE_HZ) && defined(CONFIG_FREERTOS_HZ)
  11365. ret = (double)tickCount / configTICK_RATE_HZ;
  11366. #else
  11367. ESP_LOGW(TAG, "Warning: configTICK_RATE_HZ not defined,"
  11368. "assuming 1000 Hz.");
  11369. ret = (double)(tickCount / 1000.0);
  11370. #endif /* configTICK_RATE_HZ */
  11371. return ret;
  11372. } /* current_time */
  11373. #else
  11374. /* current_time(reset)
  11375. *
  11376. * Benchmark passage of time, in fractional seconds.
  11377. * [reset] is non zero to adjust timer or counter to zero
  11378. *
  11379. * Use care when repeatedly calling calling. See implementation. */
  11380. double current_time(int reset)
  11381. {
  11382. portTickType tickCount;
  11383. /* tick count == ms, if configTICK_RATE_HZ is set to 1000 */
  11384. tickCount = xTaskGetTickCount();
  11385. return (double)tickCount / 1000;
  11386. }
  11387. #endif
  11388. #elif defined (WOLFSSL_TIRTOS)
  11389. extern double current_time(int reset);
  11390. #elif defined(FREESCALE_MQX)
  11391. double current_time(int reset)
  11392. {
  11393. TIME_STRUCT tv;
  11394. _time_get(&tv);
  11395. return (double)tv.SECONDS + (double)tv.MILLISECONDS / 1000;
  11396. }
  11397. #elif defined(FREESCALE_KSDK_BM)
  11398. double current_time(int reset)
  11399. {
  11400. return (double)OSA_TimeGetMsec() / 1000;
  11401. }
  11402. #elif defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2)
  11403. double current_time(int reset)
  11404. {
  11405. (void)reset;
  11406. return (double)osKernelGetTickCount() / 1000.0;
  11407. }
  11408. #elif defined(WOLFSSL_EMBOS)
  11409. #include "RTOS.h"
  11410. double current_time(int reset)
  11411. {
  11412. double time_now;
  11413. double current_s = OS_GetTime() / 1000.0;
  11414. double current_us = OS_GetTime_us() / MILLION_VALUE;
  11415. time_now = (double)( current_s + current_us);
  11416. (void) reset;
  11417. return time_now;
  11418. }
  11419. #elif defined(WOLFSSL_SGX)
  11420. double current_time(int reset);
  11421. #elif defined(WOLFSSL_DEOS)
  11422. double current_time(int reset)
  11423. {
  11424. const uint32_t systemTickTimeInHz
  11425. = 1000000 / systemTickInMicroseconds();
  11426. const volatile uint32_t *systemTickPtr = systemTickPointer();
  11427. (void)reset;
  11428. return (double) *systemTickPtr/systemTickTimeInHz;
  11429. }
  11430. #elif defined(MICRIUM)
  11431. double current_time(int reset)
  11432. {
  11433. #if (OS_VERSION < 50000)
  11434. CPU_ERR err;
  11435. (void)reset;
  11436. return (double) CPU_TS_Get32()/CPU_TS_TmrFreqGet(&err);
  11437. #else
  11438. RTOS_ERR err;
  11439. double ret = 0;
  11440. OS_TICK tick = OSTimeGet(&err);
  11441. OS_RATE_HZ rate = OSTimeTickRateHzGet(&err);
  11442. (void)reset;
  11443. if (RTOS_ERR_CODE_GET(err) == RTOS_ERR_NONE) {
  11444. ret = ((double)tick)/rate;
  11445. }
  11446. return ret;
  11447. #endif
  11448. }
  11449. #elif defined(WOLFSSL_ZEPHYR)
  11450. #include <time.h>
  11451. double current_time(int reset)
  11452. {
  11453. (void)reset;
  11454. #if defined(CONFIG_ARCH_POSIX)
  11455. k_cpu_idle();
  11456. #endif
  11457. return (double)k_uptime_get() / 1000;
  11458. }
  11459. #elif defined(WOLFSSL_NETBURNER)
  11460. #include <predef.h>
  11461. #include <utils.h>
  11462. #include <constants.h>
  11463. double current_time(int reset)
  11464. {
  11465. DWORD ticks = TimeTick; /* ticks since system start */
  11466. (void)reset;
  11467. return (double) ticks/TICKS_PER_SECOND;
  11468. }
  11469. #elif defined(THREADX)
  11470. #include "tx_api.h"
  11471. double current_time(int reset)
  11472. {
  11473. (void)reset;
  11474. return (double) tx_time_get() / TX_TIMER_TICKS_PER_SECOND;
  11475. }
  11476. #elif defined(WOLFSSL_XILINX)
  11477. #ifdef XPAR_VERSAL_CIPS_0_PSPMC_0_PSV_CORTEXA72_0_TIMESTAMP_CLK_FREQ
  11478. #define COUNTS_PER_SECOND \
  11479. XPAR_VERSAL_CIPS_0_PSPMC_0_PSV_CORTEXA72_0_TIMESTAMP_CLK_FREQ
  11480. #else
  11481. #define COUNTS_PER_SECOND \
  11482. XPAR_CPU_CORTEXA53_0_TIMESTAMP_CLK_FREQ
  11483. #endif
  11484. double current_time(int reset)
  11485. {
  11486. double timer;
  11487. uint64_t cntPct = 0;
  11488. asm volatile("mrs %0, CNTPCT_EL0" : "=r" (cntPct));
  11489. /* Convert to milliseconds */
  11490. timer = (double)(cntPct / (COUNTS_PER_SECOND / 1000));
  11491. /* Convert to seconds.millisecond */
  11492. timer /= 1000;
  11493. return timer;
  11494. }
  11495. #elif defined(LINUX_RUSAGE_UTIME)
  11496. #include <sys/time.h>
  11497. #include <sys/resource.h>
  11498. static struct rusage base_rusage;
  11499. static struct rusage cur_rusage;
  11500. double current_time(int reset)
  11501. {
  11502. struct rusage rusage;
  11503. (void)reset;
  11504. LIBCALL_CHECK_RET(getrusage(RUSAGE_SELF, &rusage));
  11505. if (reset)
  11506. base_rusage = rusage;
  11507. else
  11508. cur_rusage = rusage;
  11509. /* only consider user time, as system time is host-related overhead
  11510. * outside wolfcrypt.
  11511. */
  11512. return (double)rusage.ru_utime.tv_sec +
  11513. (double)rusage.ru_utime.tv_usec / MILLION_VALUE;
  11514. }
  11515. static void check_for_excessive_stime(const char *desc,
  11516. const char *desc_extra)
  11517. {
  11518. double start_utime = (double)base_rusage.ru_utime.tv_sec +
  11519. (double)base_rusage.ru_utime.tv_usec / MILLION_VALUE;
  11520. double start_stime = (double)base_rusage.ru_stime.tv_sec +
  11521. (double)base_rusage.ru_stime.tv_usec / MILLION_VALUE;
  11522. double cur_utime = (double)cur_rusage.ru_utime.tv_sec +
  11523. (double)cur_rusage.ru_utime.tv_usec / MILLION_VALUE;
  11524. double cur_stime = (double)cur_rusage.ru_stime.tv_sec +
  11525. (double)cur_rusage.ru_stime.tv_usec / MILLION_VALUE;
  11526. double stime_utime_ratio =
  11527. (cur_stime - start_stime) / (cur_utime - start_utime);
  11528. if (stime_utime_ratio > .1)
  11529. printf("%swarning, "
  11530. "excessive system time ratio for %s%s (" FLT_FMT_PREC "%%).\n",
  11531. err_prefix, desc, desc_extra,
  11532. FLT_FMT_PREC_ARGS(3, stime_utime_ratio * 100.0));
  11533. }
  11534. #elif defined(WOLFSSL_LINUXKM)
  11535. double current_time(int reset)
  11536. {
  11537. (void)reset;
  11538. u64 ns = ktime_get_ns();
  11539. return (double)ns / 1000000000.0;
  11540. }
  11541. #else
  11542. #include <sys/time.h>
  11543. double current_time(int reset)
  11544. {
  11545. struct timespec tv;
  11546. (void)reset;
  11547. LIBCALL_CHECK_RET(clock_gettime(CLOCK_REALTIME, &tv));
  11548. #ifdef BENCH_MICROSECOND
  11549. return (double)tv.tv_sec * 1000000 + (double)tv.tv_nsec / 1000;
  11550. #else
  11551. return (double)tv.tv_sec + (double)tv.tv_nsec / 1000000000;
  11552. #endif
  11553. }
  11554. #endif /* _WIN32 */
  11555. #if defined(HAVE_GET_CYCLES)
  11556. #if defined(WOLFSSL_ESPIDF)
  11557. /* Generic CPU cycle counter for either Xtensa or RISC-V */
  11558. static WC_INLINE word64 esp_get_cpu_benchmark_cycles(void)
  11559. {
  11560. /* Reminder for long duration between calls with
  11561. * multiple overflows will not be detected. */
  11562. return esp_get_cycle_count_ex();
  11563. }
  11564. /* implement other architectures here */
  11565. #else
  11566. static WC_INLINE word64 get_intel_cycles(void)
  11567. {
  11568. unsigned int lo_c, hi_c;
  11569. __asm__ __volatile__ (
  11570. "cpuid\n\t"
  11571. "rdtsc"
  11572. : "=a"(lo_c), "=d"(hi_c) /* out */
  11573. : "a"(0) /* in */
  11574. : "%ebx", "%ecx"); /* clobber */
  11575. return ((word64)lo_c) | (((word64)hi_c) << 32);
  11576. }
  11577. #endif
  11578. #endif /* HAVE_GET_CYCLES */
  11579. void benchmark_configure(word32 block_size)
  11580. {
  11581. /* must be greater than 0 */
  11582. if (block_size > 0) {
  11583. numBlocks = (int)((word32)numBlocks * bench_size / block_size);
  11584. bench_size = block_size;
  11585. }
  11586. }
  11587. #ifndef NO_MAIN_DRIVER
  11588. #ifndef MAIN_NO_ARGS
  11589. #ifndef WOLFSSL_BENCHMARK_ALL
  11590. /* Display the algorithm string and keep to 80 characters per line.
  11591. *
  11592. * str Algorithm string to print.
  11593. * line Length of line used so far.
  11594. */
  11595. #ifndef BENCH_MAX_LINE
  11596. #define BENCH_MAX_LINE 80
  11597. #endif
  11598. static void print_alg(const char* str, int* line)
  11599. {
  11600. const char* const ident = " ";
  11601. if (*line == 0) {
  11602. printf("%s", ident);
  11603. *line = (int)XSTRLEN(ident);
  11604. }
  11605. printf(" %s", str);
  11606. *line += (int)XSTRLEN(str) + 1;
  11607. if (*line > BENCH_MAX_LINE) {
  11608. printf("\n");
  11609. *line = 0;
  11610. }
  11611. }
  11612. #endif /* WOLFSSL_BENCHMARK_ALL */
  11613. /* Display the usage options of the benchmark program. */
  11614. static void Usage(void)
  11615. {
  11616. int e = 0;
  11617. #ifndef WOLFSSL_BENCHMARK_ALL
  11618. int i;
  11619. int line;
  11620. #endif
  11621. printf("benchmark\n");
  11622. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -? */
  11623. printf("%s", bench_Usage_msg1[lng_index][e++]); /* English / Japanese */
  11624. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -csv */
  11625. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -base10 */
  11626. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  11627. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -no_aad */
  11628. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -aad_size */
  11629. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -all_aad */
  11630. #else
  11631. e += 3;
  11632. #endif
  11633. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -dgst_full */
  11634. #ifndef NO_RSA
  11635. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -ras_sign */
  11636. #ifdef WOLFSSL_KEY_GEN
  11637. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -rsa-sz */
  11638. #endif
  11639. e++;
  11640. #else
  11641. e += 2;
  11642. #endif
  11643. #if !defined(NO_DH) && defined(HAVE_FFDHE_2048)
  11644. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -ffdhe2048 */
  11645. #endif
  11646. e++;
  11647. #if !defined(NO_DH) && defined(HAVE_FFDHE_3072)
  11648. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -ffdhe3072 */
  11649. #endif
  11650. e++;
  11651. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  11652. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -p256 */
  11653. #endif
  11654. e++;
  11655. #if defined(HAVE_ECC) && defined(HAVE_ECC384)
  11656. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -p384 */
  11657. #endif
  11658. e++;
  11659. #if defined(HAVE_ECC) && defined(HAVE_ECC521)
  11660. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -p521 */
  11661. #endif
  11662. e++;
  11663. #if defined(HAVE_ECC)
  11664. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -ecc-all */
  11665. #endif
  11666. e++;
  11667. #ifndef WOLFSSL_BENCHMARK_ALL
  11668. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -<alg> */
  11669. line = 0;
  11670. for (i=0; bench_cipher_opt[i].str != NULL; i++)
  11671. print_alg(bench_cipher_opt[i].str, &line);
  11672. for (i=0; bench_digest_opt[i].str != NULL; i++)
  11673. print_alg(bench_digest_opt[i].str, &line);
  11674. for (i=0; bench_mac_opt[i].str != NULL; i++)
  11675. print_alg(bench_mac_opt[i].str, &line);
  11676. for (i=0; bench_kdf_opt[i].str != NULL; i++)
  11677. print_alg(bench_kdf_opt[i].str, &line);
  11678. for (i=0; bench_asym_opt[i].str != NULL; i++)
  11679. print_alg(bench_asym_opt[i].str, &line);
  11680. for (i=0; bench_other_opt[i].str != NULL; i++)
  11681. print_alg(bench_other_opt[i].str, &line);
  11682. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  11683. for (i=0; bench_pq_asym_opt[i].str != NULL; i++)
  11684. print_alg(bench_pq_asym_opt[i].str, &line);
  11685. #if defined(HAVE_LIBOQS) && defined(HAVE_SPHINCS)
  11686. for (i=0; bench_pq_asym_opt2[i].str != NULL; i++)
  11687. print_alg(bench_pq_asym_opt2[i].str, &line);
  11688. #endif /* HAVE_LIBOQS && HAVE_SPHINCS */
  11689. #endif /* HAVE_PQC */
  11690. #if defined(BENCH_PQ_STATEFUL_HBS)
  11691. for (i=0; bench_pq_hash_sig_opt[i].str != NULL; i++)
  11692. print_alg(bench_pq_hash_sig_opt[i].str, &line);
  11693. #endif /* BENCH_PQ_STATEFUL_HBS */
  11694. printf("\n");
  11695. #endif /* !WOLFSSL_BENCHMARK_ALL */
  11696. e++;
  11697. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -lng */
  11698. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option <num> */
  11699. printf("%s", bench_Usage_msg1[lng_index][e++]); /* option -blocks <num> */
  11700. #ifdef WC_ENABLE_BENCH_THREADING
  11701. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -threads <num> */
  11702. #endif
  11703. e++;
  11704. #ifdef WC_BENCH_TRACK_STATS
  11705. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -print */
  11706. #endif
  11707. e++;
  11708. #ifndef NO_FILESYSTEM
  11709. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -hash_input */
  11710. #endif
  11711. e++;
  11712. #ifndef NO_FILESYSTEM
  11713. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -cipher_input */
  11714. #endif
  11715. #ifdef MULTI_VALUE_STATISTICS
  11716. e++;
  11717. printf("%s", bench_Usage_msg1[lng_index][e]); /* option -min_runs */
  11718. #endif
  11719. }
  11720. /* Match the command line argument with the string.
  11721. *
  11722. * arg Command line argument.
  11723. * str String to check for.
  11724. * return 1 if the command line argument matches the string, 0 otherwise.
  11725. */
  11726. static int string_matches(const char* arg, const char* str)
  11727. {
  11728. return XSTRCMP(arg, str) == 0;
  11729. }
  11730. #endif /* MAIN_NO_ARGS */
  11731. /*
  11732. ** ----------------------------------------------------------------------------
  11733. ** determine how the benchmarks are called, the function name varies:
  11734. ** ----------------------------------------------------------------------------
  11735. */
  11736. #if !defined(NO_MAIN_DRIVER) && !defined(NO_MAIN_FUNCTION)
  11737. #if defined(WOLFSSL_ESPIDF) || defined(_WIN32_WCE)
  11738. /* for some environments, we'll call a function wolf_benchmark_task: */
  11739. int wolf_benchmark_task(void)
  11740. #elif defined(MAIN_NO_ARGS)
  11741. /* otherwise we'll use main() with no arguments as desired: */
  11742. int main()
  11743. #else
  11744. /* else we'll be calling main with default arg parameters */
  11745. int main(int argc, char** argv)
  11746. #endif
  11747. {
  11748. /* Code for main() or wolf_benchmark_task() */
  11749. #ifdef WOLFSSL_ESPIDF
  11750. int argc = construct_argv();
  11751. char** argv = (char**)__argv;
  11752. #elif defined(MAIN_NO_ARGS)
  11753. int argc = 0;
  11754. char** argv = NULL;
  11755. #endif
  11756. return wolfcrypt_benchmark_main(argc, argv);
  11757. }
  11758. #endif /* !NO_MAIN_DRIVER && !NO_MAIN_FUNCTION */
  11759. int wolfcrypt_benchmark_main(int argc, char** argv)
  11760. {
  11761. int ret = 0;
  11762. #ifndef MAIN_NO_ARGS
  11763. int optMatched;
  11764. #ifndef WOLFSSL_BENCHMARK_ALL
  11765. int i;
  11766. #endif
  11767. #endif
  11768. benchmark_static_init(1);
  11769. printf("%s------------------------------------------------------------------------------\n",
  11770. info_prefix);
  11771. printf("%s wolfSSL version %s\n", info_prefix, LIBWOLFSSL_VERSION_STRING);
  11772. printf("%s------------------------------------------------------------------------------\n",
  11773. info_prefix);
  11774. #ifndef MAIN_NO_ARGS
  11775. while (argc > 1) {
  11776. if (string_matches(argv[1], "-?")) {
  11777. if (--argc > 1) {
  11778. lng_index = XATOI((++argv)[1]);
  11779. if (lng_index<0 || lng_index>1) {
  11780. lng_index = 0;
  11781. }
  11782. }
  11783. Usage();
  11784. return 0;
  11785. }
  11786. else if (string_matches(argv[1], "-lng")) {
  11787. argc--;
  11788. argv++;
  11789. if (argc > 1) {
  11790. lng_index = XATOI(argv[1]);
  11791. if (lng_index<0 || lng_index>1) {
  11792. printf("invalid number(%d) is specified. [<num> :0-1]\n",
  11793. lng_index);
  11794. lng_index = 0;
  11795. }
  11796. }
  11797. }
  11798. else if (string_matches(argv[1], "-base10"))
  11799. base2 = 0;
  11800. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  11801. else if (string_matches(argv[1], "-no_aad"))
  11802. aes_aad_options = AAD_SIZE_ZERO;
  11803. else if (string_matches(argv[1], "-all_aad"))
  11804. aes_aad_options |= AAD_SIZE_ZERO | AAD_SIZE_DEFAULT;
  11805. else if (string_matches(argv[1], "-aad_size")) {
  11806. argc--;
  11807. argv++;
  11808. if (argc > 1) {
  11809. aes_aad_size = (word32)XATOI(argv[1]);
  11810. aes_aad_options |= AAD_SIZE_CUSTOM;
  11811. }
  11812. }
  11813. #endif
  11814. else if (string_matches(argv[1], "-dgst_full"))
  11815. digest_stream = 0;
  11816. #ifndef NO_RSA
  11817. else if (string_matches(argv[1], "-rsa_sign"))
  11818. rsa_sign_verify = 1;
  11819. #endif
  11820. #if !defined(NO_DH) && defined(HAVE_FFDHE_2048)
  11821. else if (string_matches(argv[1], "-ffdhe2048"))
  11822. use_ffdhe = 2048;
  11823. #endif
  11824. #if !defined(NO_DH) && defined(HAVE_FFDHE_3072)
  11825. else if (string_matches(argv[1], "-ffdhe3072"))
  11826. use_ffdhe = 3072;
  11827. #endif
  11828. #if !defined(NO_DH) && defined(HAVE_FFDHE_4096)
  11829. else if (string_matches(argv[1], "-ffdhe4096"))
  11830. use_ffdhe = 4096;
  11831. #endif
  11832. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  11833. else if (string_matches(argv[1], "-p256"))
  11834. bench_asym_algs |= BENCH_ECC_P256;
  11835. #endif
  11836. #if defined(HAVE_ECC) && defined(HAVE_ECC384)
  11837. else if (string_matches(argv[1], "-p384"))
  11838. bench_asym_algs |= BENCH_ECC_P384;
  11839. #endif
  11840. #if defined(HAVE_ECC) && defined(HAVE_ECC521)
  11841. else if (string_matches(argv[1], "-p521"))
  11842. bench_asym_algs |= BENCH_ECC_P521;
  11843. #endif
  11844. #ifdef BENCH_ASYM
  11845. else if (string_matches(argv[1], "-csv")) {
  11846. csv_format = 1;
  11847. }
  11848. #endif
  11849. #ifdef WC_ENABLE_BENCH_THREADING
  11850. else if (string_matches(argv[1], "-threads")) {
  11851. argc--;
  11852. argv++;
  11853. if (argc > 1) {
  11854. g_threadCount = XATOI(argv[1]);
  11855. if (g_threadCount < 1 || lng_index > 128){
  11856. printf("invalid number(%d) is specified. [<num> :1-128]\n",
  11857. g_threadCount);
  11858. g_threadCount = 0;
  11859. }
  11860. }
  11861. }
  11862. #endif
  11863. #ifdef WC_BENCH_TRACK_STATS
  11864. else if (string_matches(argv[1], "-print")) {
  11865. gPrintStats = 1;
  11866. }
  11867. #endif
  11868. else if (string_matches(argv[1], "-blocks")) {
  11869. argc--;
  11870. argv++;
  11871. if (argc > 1)
  11872. numBlocks = XATOI(argv[1]);
  11873. }
  11874. #ifndef NO_FILESYSTEM
  11875. else if (string_matches(argv[1], "-hash_input")) {
  11876. argc--;
  11877. argv++;
  11878. if (argc > 1)
  11879. hash_input = argv[1];
  11880. }
  11881. else if (string_matches(argv[1], "-cipher_input")) {
  11882. argc--;
  11883. argv++;
  11884. if (argc > 1)
  11885. cipher_input = argv[1];
  11886. }
  11887. #endif
  11888. #ifdef MULTI_VALUE_STATISTICS
  11889. else if (string_matches(argv[1], "-min_runs")) {
  11890. argc--;
  11891. argv++;
  11892. if (argc > 1) {
  11893. minimum_runs = XATOI(argv[1]);
  11894. }
  11895. }
  11896. #endif
  11897. else if (argv[1][0] == '-') {
  11898. optMatched = 0;
  11899. #ifndef WOLFSSL_BENCHMARK_ALL
  11900. /* Check known algorithm choosing command line options. */
  11901. /* Known cipher algorithms */
  11902. for (i=0; !optMatched && bench_cipher_opt[i].str != NULL; i++) {
  11903. if (string_matches(argv[1], bench_cipher_opt[i].str)) {
  11904. bench_cipher_algs |= bench_cipher_opt[i].val;
  11905. bench_all = 0;
  11906. optMatched = 1;
  11907. }
  11908. }
  11909. /* Known digest algorithms */
  11910. for (i=0; !optMatched && bench_digest_opt[i].str != NULL; i++) {
  11911. if (string_matches(argv[1], bench_digest_opt[i].str)) {
  11912. bench_digest_algs |= bench_digest_opt[i].val;
  11913. bench_all = 0;
  11914. optMatched = 1;
  11915. }
  11916. }
  11917. /* Known MAC algorithms */
  11918. for (i=0; !optMatched && bench_mac_opt[i].str != NULL; i++) {
  11919. if (string_matches(argv[1], bench_mac_opt[i].str)) {
  11920. bench_mac_algs |= bench_mac_opt[i].val;
  11921. bench_all = 0;
  11922. optMatched = 1;
  11923. }
  11924. }
  11925. /* Known KDF algorithms */
  11926. for (i=0; !optMatched && bench_kdf_opt[i].str != NULL; i++) {
  11927. if (string_matches(argv[1], bench_kdf_opt[i].str)) {
  11928. bench_kdf_algs |= bench_kdf_opt[i].val;
  11929. bench_all = 0;
  11930. optMatched = 1;
  11931. }
  11932. }
  11933. /* Known asymmetric algorithms */
  11934. for (i=0; !optMatched && bench_asym_opt[i].str != NULL; i++) {
  11935. if (string_matches(argv[1], bench_asym_opt[i].str)) {
  11936. bench_asym_algs |= bench_asym_opt[i].val;
  11937. bench_all = 0;
  11938. optMatched = 1;
  11939. }
  11940. }
  11941. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  11942. /* Known asymmetric post-quantum algorithms */
  11943. for (i=0; !optMatched && bench_pq_asym_opt[i].str != NULL; i++) {
  11944. if (string_matches(argv[1], bench_pq_asym_opt[i].str)) {
  11945. bench_pq_asym_algs |= bench_pq_asym_opt[i].val;
  11946. bench_all = 0;
  11947. optMatched = 1;
  11948. }
  11949. }
  11950. #ifdef HAVE_SPHINCS
  11951. /* Both bench_pq_asym_opt and bench_pq_asym_opt2 are looking for
  11952. * -pq, so we need to do a special case for -pq since optMatched
  11953. * was set to 1 just above. */
  11954. if ((bench_pq_asym_opt[0].str != NULL) &&
  11955. string_matches(argv[1], bench_pq_asym_opt[0].str))
  11956. {
  11957. bench_pq_asym_algs2 |= bench_pq_asym_opt2[0].val;
  11958. bench_all = 0;
  11959. optMatched = 1;
  11960. }
  11961. for (i=1; !optMatched && bench_pq_asym_opt2[i].str != NULL; i++) {
  11962. if (string_matches(argv[1], bench_pq_asym_opt2[i].str)) {
  11963. bench_pq_asym_algs2 |= bench_pq_asym_opt2[i].val;
  11964. bench_all = 0;
  11965. optMatched = 1;
  11966. }
  11967. }
  11968. #endif
  11969. #endif /* HAVE_PQC */
  11970. /* Other known cryptographic algorithms */
  11971. for (i=0; !optMatched && bench_other_opt[i].str != NULL; i++) {
  11972. if (string_matches(argv[1], bench_other_opt[i].str)) {
  11973. bench_other_algs |= bench_other_opt[i].val;
  11974. bench_all = 0;
  11975. optMatched = 1;
  11976. }
  11977. }
  11978. #if defined(BENCH_PQ_STATEFUL_HBS)
  11979. /* post-quantum stateful hash-based signatures */
  11980. for (i=0; !optMatched && bench_pq_hash_sig_opt[i].str != NULL; i++) {
  11981. if (string_matches(argv[1], bench_pq_hash_sig_opt[i].str)) {
  11982. bench_pq_hash_sig_algs |= bench_pq_hash_sig_opt[i].val;
  11983. bench_all = 0;
  11984. optMatched = 1;
  11985. }
  11986. }
  11987. #endif /* BENCH_PQ_STATEFUL_HBS */
  11988. #endif
  11989. if (!optMatched) {
  11990. printf("Option not recognized: %s\n", argv[1]);
  11991. Usage();
  11992. return 1;
  11993. }
  11994. }
  11995. else {
  11996. /* parse for block size */
  11997. benchmark_configure((word32)XATOI(argv[1]));
  11998. }
  11999. argc--;
  12000. argv++;
  12001. }
  12002. #endif /* MAIN_NO_ARGS */
  12003. #if defined(WOLFSSL_BENCHMARK_FIXED_CSV)
  12004. /* when defined, we'll always output CSV regardless of params.
  12005. ** this is typically convenient in embedded environments.
  12006. */
  12007. csv_format = 1;
  12008. #endif
  12009. #if defined(WC_ENABLE_BENCH_THREADING) && !defined(WOLFSSL_ASYNC_CRYPT)
  12010. if (g_threadCount > 1) {
  12011. ret = benchmark_test_threaded(NULL);
  12012. }
  12013. else
  12014. #endif
  12015. {
  12016. #if defined(CONFIG_IDF_TARGET_ESP32C2) || \
  12017. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  12018. defined(CONFIG_IDF_TARGET_ESP32C6)
  12019. {
  12020. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  12021. if (esp_gptimer == NULL) {
  12022. ESP_ERROR_CHECK(gptimer_new_timer(&esp_timer_config,
  12023. &esp_gptimer) );
  12024. }
  12025. ESP_ERROR_CHECK(gptimer_enable(esp_gptimer));
  12026. ESP_ERROR_CHECK(gptimer_start(esp_gptimer));
  12027. ESP_LOGI(TAG, "Enable %s timer", CONFIG_IDF_TARGET);
  12028. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  12029. }
  12030. #endif
  12031. #ifdef HAVE_STACK_SIZE
  12032. ret = StackSizeCheck(NULL, benchmark_test);
  12033. #else
  12034. ret = benchmark_test(NULL);
  12035. #endif
  12036. }
  12037. #if defined(CONFIG_IDF_TARGET_ESP32C2) || \
  12038. defined(CONFIG_IDF_TARGET_ESP32C3) || \
  12039. defined(CONFIG_IDF_TARGET_ESP32C6)
  12040. {
  12041. #ifdef WOLFSSL_BENCHMARK_TIMER_DEBUG
  12042. ESP_ERROR_CHECK(gptimer_stop(esp_gptimer));
  12043. ESP_ERROR_CHECK(gptimer_disable(esp_gptimer));
  12044. #endif /* WOLFSSL_BENCHMARK_TIMER_DEBUG */
  12045. }
  12046. #endif
  12047. return ret;
  12048. }
  12049. #endif /* !NO_MAIN_DRIVER */
  12050. #else
  12051. #if !defined(NO_MAIN_DRIVER) && !defined(NO_MAIN_FUNCTION)
  12052. int main(void) { return 0; }
  12053. #endif
  12054. #endif /* !NO_CRYPT_BENCHMARK */