benchmark.c 202 KB

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