benchmark.c 268 KB

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