benchmark.c 152 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025
  1. /* benchmark.c
  2. *
  3. * Copyright (C) 2006-2017 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. #include <wolfssl/wolfcrypt/settings.h>
  26. /* Macro to disable benchmark */
  27. #ifndef NO_CRYPT_BENCHMARK
  28. #if defined(XMALLOC_USER) || defined(FREESCALE_MQX)
  29. /* MQX classic needs for EXIT_FAILURE */
  30. #include <stdlib.h> /* we're using malloc / free direct here */
  31. #endif
  32. #ifdef WOLFSSL_STATIC_MEMORY
  33. #include <wolfssl/wolfcrypt/memory.h>
  34. static WOLFSSL_HEAP_HINT* HEAP_HINT;
  35. #else
  36. #define HEAP_HINT NULL
  37. #endif /* WOLFSSL_STATIC_MEMORY */
  38. #include <string.h>
  39. #ifdef FREESCALE_MQX
  40. #include <mqx.h>
  41. #if MQX_USE_IO_OLD
  42. #include <fio.h>
  43. #else
  44. #include <nio.h>
  45. #endif
  46. #elif defined(FREESCALE_KSDK_1_3)
  47. #include "fsl_debug_console.h"
  48. #include "fsl_os_abstraction.h"
  49. #undef printf
  50. #define printf PRINTF
  51. #else
  52. #include <stdio.h>
  53. #endif
  54. #include <wolfssl/wolfcrypt/random.h>
  55. #include <wolfssl/wolfcrypt/des3.h>
  56. #include <wolfssl/wolfcrypt/arc4.h>
  57. #include <wolfssl/wolfcrypt/hc128.h>
  58. #include <wolfssl/wolfcrypt/rabbit.h>
  59. #include <wolfssl/wolfcrypt/chacha.h>
  60. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  61. #include <wolfssl/wolfcrypt/aes.h>
  62. #include <wolfssl/wolfcrypt/poly1305.h>
  63. #include <wolfssl/wolfcrypt/camellia.h>
  64. #include <wolfssl/wolfcrypt/md5.h>
  65. #include <wolfssl/wolfcrypt/sha.h>
  66. #include <wolfssl/wolfcrypt/sha256.h>
  67. #include <wolfssl/wolfcrypt/sha512.h>
  68. #include <wolfssl/wolfcrypt/sha3.h>
  69. #include <wolfssl/wolfcrypt/rsa.h>
  70. #include <wolfssl/wolfcrypt/asn.h>
  71. #include <wolfssl/wolfcrypt/ripemd.h>
  72. #include <wolfssl/wolfcrypt/cmac.h>
  73. #ifndef NO_HMAC
  74. #include <wolfssl/wolfcrypt/hmac.h>
  75. #endif
  76. #ifndef NO_PWDBASED
  77. #include <wolfssl/wolfcrypt/pwdbased.h>
  78. #endif
  79. #ifdef HAVE_ECC
  80. #include <wolfssl/wolfcrypt/ecc.h>
  81. #endif
  82. #ifdef HAVE_IDEA
  83. #include <wolfssl/wolfcrypt/idea.h>
  84. #endif
  85. #ifdef HAVE_CURVE25519
  86. #include <wolfssl/wolfcrypt/curve25519.h>
  87. #endif
  88. #ifdef HAVE_ED25519
  89. #include <wolfssl/wolfcrypt/ed25519.h>
  90. #endif
  91. #include <wolfssl/wolfcrypt/dh.h>
  92. #ifdef HAVE_NTRU
  93. #include "libntruencrypt/ntru_crypto.h"
  94. #endif
  95. #include <wolfssl/wolfcrypt/random.h>
  96. #include <wolfssl/wolfcrypt/error-crypt.h>
  97. #include <wolfssl/wolfcrypt/types.h>
  98. #ifndef EXIT_FAILURE
  99. #define EXIT_FAILURE 1
  100. #endif
  101. /* only for stack size check */
  102. #ifdef HAVE_STACK_SIZE
  103. #include <wolfssl/ssl.h>
  104. #include <wolfssl/test.h>
  105. #endif
  106. #ifdef WOLFSSL_ASYNC_CRYPT
  107. #include <wolfssl/wolfcrypt/async.h>
  108. #endif
  109. /* Bit values for each algorithm that is able to be benchmarked.
  110. * Common grouping of algorithms also.
  111. * Each algorithm has a unique value for its type e.g. cipher.
  112. */
  113. /* Cipher algorithms. */
  114. #define BENCH_AES_CBC 0x00000001
  115. #define BENCH_AES_GCM 0x00000002
  116. #define BENCH_AES_ECB 0x00000004
  117. #define BENCH_AES_XTS 0x00000008
  118. #define BENCH_AES_CTR 0x00000010
  119. #define BENCH_AES_CCM 0x00000020
  120. #define BENCH_CAMELLIA 0x00000100
  121. #define BENCH_ARC4 0x00000200
  122. #define BENCH_HC128 0x00000400
  123. #define BENCH_RABBIT 0x00000800
  124. #define BENCH_CHACHA20 0x00001000
  125. #define BENCH_CHACHA20_POLY1305 0x00002000
  126. #define BENCH_DES 0x00004000
  127. #define BENCH_IDEA 0x00008000
  128. #define BENCH_AES_CFB 0x00010000
  129. /* Digest algorithms. */
  130. #define BENCH_MD5 0x00000001
  131. #define BENCH_POLY1305 0x00000002
  132. #define BENCH_SHA 0x00000004
  133. #define BENCH_SHA224 0x00000010
  134. #define BENCH_SHA256 0x00000020
  135. #define BENCH_SHA384 0x00000040
  136. #define BENCH_SHA512 0x00000080
  137. #define BENCH_SHA2 (BENCH_SHA224 | BENCH_SHA256 | \
  138. BENCH_SHA384 | BENCH_SHA512)
  139. #define BENCH_SHA3_224 0x00000100
  140. #define BENCH_SHA3_256 0x00000200
  141. #define BENCH_SHA3_384 0x00000400
  142. #define BENCH_SHA3_512 0x00000800
  143. #define BENCH_SHA3 (BENCH_SHA3_224 | BENCH_SHA3_256 | \
  144. BENCH_SHA3_384 | BENCH_SHA3_512)
  145. #define BENCH_RIPEMD 0x00001000
  146. #define BENCH_BLAKE2 0x00002000
  147. /* MAC algorithms. */
  148. #define BENCH_CMAC 0x00000001
  149. #define BENCH_HMAC_MD5 0x00000002
  150. #define BENCH_HMAC_SHA 0x00000004
  151. #define BENCH_HMAC_SHA224 0x00000010
  152. #define BENCH_HMAC_SHA256 0x00000020
  153. #define BENCH_HMAC_SHA384 0x00000040
  154. #define BENCH_HMAC_SHA512 0x00000080
  155. #define BENCH_HMAC (BENCH_HMAC_MD5 | BENCH_HMAC_SHA | \
  156. BENCH_HMAC_SHA224 | BENCH_HMAC_SHA256 | \
  157. BENCH_HMAC_SHA384 | BENCH_HMAC_SHA512)
  158. /* Asymmetric algorithms. */
  159. #define BENCH_RSA_KEYGEN 0x00000001
  160. #define BENCH_RSA 0x00000002
  161. #define BENCH_DH 0x00000010
  162. #define BENCH_NTRU 0x00000100
  163. #define BENCH_NTRU_KEYGEN 0x00000200
  164. #define BENCH_ECC_MAKEKEY 0x00001000
  165. #define BENCH_ECC 0x00002000
  166. #define BENCH_ECC_ENCRYPT 0x00004000
  167. #define BENCH_CURVE25519_KEYGEN 0x00010000
  168. #define BENCH_CURVE25519_KA 0x00020000
  169. #define BENCH_ED25519_KEYGEN 0x00040000
  170. #define BENCH_ED25519_SIGN 0x00080000
  171. /* Other */
  172. #define BENCH_RNG 0x00000001
  173. #define BENCH_SCRYPT 0x00000002
  174. /* Benchmark all compiled in algorithms.
  175. * When 1, ignore other benchmark algorithm values.
  176. * 0, only benchmark algorithm values set.
  177. */
  178. static int bench_all = 1;
  179. /* Cipher algorithms to benchmark. */
  180. static int bench_cipher_algs = 0;
  181. /* Digest algorithms to benchmark. */
  182. static int bench_digest_algs = 0;
  183. /* MAC algorithms to benchmark. */
  184. static int bench_mac_algs = 0;
  185. /* Asymmetric algorithms to benchmark. */
  186. static int bench_asym_algs = 0;
  187. /* Other cryptographic algorithms to benchmark. */
  188. static int bench_other_algs = 0;
  189. #if !defined(WOLFSSL_BENCHMARK_ALL) && !defined(NO_MAIN_DRIVER)
  190. /* The mapping of command line option to bit values. */
  191. typedef struct bench_alg {
  192. /* Command line option string. */
  193. const char* str;
  194. /* Bit values to set. */
  195. int val;
  196. } bench_alg;
  197. /* All recognized cipher algorithm choosing command line options. */
  198. static const bench_alg bench_cipher_opt[] = {
  199. { "-cipher", -1 },
  200. #ifdef HAVE_AES_CBC
  201. { "-aes-cbc", BENCH_AES_CBC },
  202. #endif
  203. #ifdef HAVE_AESGCM
  204. { "-aes-gcm", BENCH_AES_GCM },
  205. #endif
  206. #ifdef WOLFSSL_AES_DIRECT
  207. { "-aes-ecb", BENCH_AES_ECB },
  208. #endif
  209. #ifdef WOLFSSL_AES_XTS
  210. { "-aes-xts", BENCH_AES_XTS },
  211. #endif
  212. #ifdef WOLFSSL_AES_CFB
  213. { "-aes-cfb", BENCH_AES_CFB },
  214. #endif
  215. #ifdef WOLFSSL_AES_COUNTER
  216. { "-aes-ctr", BENCH_AES_CTR },
  217. #endif
  218. #ifdef HAVE_AESCCM
  219. { "-aes-ccm", BENCH_AES_CCM },
  220. #endif
  221. #ifdef HAVE_CAMELLIA
  222. { "-camellia", BENCH_CAMELLIA },
  223. #endif
  224. #ifndef NO_RC4
  225. { "-arc4", BENCH_ARC4 },
  226. #endif
  227. #ifdef HAVE_HC128
  228. { "-hc128", BENCH_HC128 },
  229. #endif
  230. #ifndef NO_RABBIT
  231. { "-rabbit", BENCH_RABBIT },
  232. #endif
  233. #ifdef HAVE_CHACHA
  234. { "-chacha20", BENCH_CHACHA20 },
  235. #endif
  236. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  237. { "-chacha20-poly1305", BENCH_CHACHA20_POLY1305 },
  238. #endif
  239. #ifndef NO_DES3
  240. { "-des", BENCH_DES },
  241. #endif
  242. #ifdef HAVE_IDEA
  243. { "-idea", BENCH_IDEA },
  244. #endif
  245. { NULL, 0}
  246. };
  247. /* All recognized digest algorithm choosing command line options. */
  248. static const bench_alg bench_digest_opt[] = {
  249. { "-digest", -1 },
  250. #ifndef NO_MD5
  251. { "-md5", BENCH_MD5 },
  252. #endif
  253. #ifdef HAVE_POLY1305
  254. { "-poly1305", BENCH_POLY1305 },
  255. #endif
  256. #ifndef NO_SHA
  257. { "-sha", BENCH_SHA },
  258. #endif
  259. #if defined(WOLFSSL_SHA224) || !defined(NO_SHA256) || defined(WOLFSSL_SHA384) \
  260. || defined(WOLFSSL_SHA512)
  261. { "-sha2", BENCH_SHA2 },
  262. #endif
  263. #ifdef WOLFSSL_SHA224
  264. { "-sha224", BENCH_SHA224 },
  265. #endif
  266. #ifndef NO_SHA256
  267. { "-sha256", BENCH_SHA256 },
  268. #endif
  269. #ifdef WOLFSSL_SHA384
  270. { "-sha384", BENCH_SHA384 },
  271. #endif
  272. #ifdef WOLFSSL_SHA512
  273. { "-sha512", BENCH_SHA512 },
  274. #endif
  275. #ifdef WOLFSSL_SHA3
  276. { "-sha3", BENCH_SHA3 },
  277. #ifndef WOLFSSL_NOSHA3_224
  278. { "-sha3-224", BENCH_SHA3_224 },
  279. #endif
  280. #ifndef WOLFSSL_NOSHA3_256
  281. { "-sha3-256", BENCH_SHA3_256 },
  282. #endif
  283. #ifndef WOLFSSL_NOSHA3_384
  284. { "-sha3-384", BENCH_SHA3_384 },
  285. #endif
  286. #ifndef WOLFSSL_NOSHA3_512
  287. { "-sha3-512", BENCH_SHA3_512 },
  288. #endif
  289. #endif
  290. #ifdef WOLFSSL_RIPEMD
  291. { "-ripemd", BENCH_RIPEMD },
  292. #endif
  293. #ifdef HAVE_BLAKE2
  294. { "-blake2", BENCH_BLAKE2 },
  295. #endif
  296. { NULL, 0}
  297. };
  298. /* All recognized MAC algorithm choosing command line options. */
  299. static const bench_alg bench_mac_opt[] = {
  300. { "-mac", -1 },
  301. #ifdef WOLFSSL_CMAC
  302. { "-cmac", BENCH_CMAC },
  303. #endif
  304. #ifndef NO_HMAC
  305. { "-hmac", BENCH_HMAC },
  306. #ifndef NO_MD5
  307. { "-hmac-md5", BENCH_HMAC_MD5 },
  308. #endif
  309. #ifndef NO_SHA
  310. { "-hmac-sha", BENCH_HMAC_SHA },
  311. #endif
  312. #ifdef WOLFSSL_SHA224
  313. { "-hmac-sha224", BENCH_HMAC_SHA224 },
  314. #endif
  315. #ifndef NO_SHA256
  316. { "-hmac-sha256", BENCH_HMAC_SHA256 },
  317. #endif
  318. #ifdef WOLFSSL_SHA384
  319. { "-hmac-sha384", BENCH_HMAC_SHA384 },
  320. #endif
  321. #ifdef WOLFSSL_SHA512
  322. { "-hmac-sha512", BENCH_HMAC_SHA512 },
  323. #endif
  324. #endif
  325. { NULL, 0}
  326. };
  327. /* All recognized asymmetric algorithm choosing command line options. */
  328. static const bench_alg bench_asym_opt[] = {
  329. { "-asym", -1 },
  330. #ifndef NO_RSA
  331. #ifdef WOLFSSL_KEY_GEN
  332. { "-rsa-kg", BENCH_RSA_KEYGEN },
  333. #endif
  334. { "-rsa", BENCH_RSA },
  335. #endif
  336. #ifndef NO_DH
  337. { "-dh", BENCH_DH },
  338. #endif
  339. #ifdef HAVE_NTRU
  340. { "-ntru", BENCH_NTRU },
  341. { "-ntru-kg", BENCH_NTRU_KEYGEN },
  342. #endif
  343. #ifdef HAVE_ECC
  344. { "-ecc-kg", BENCH_ECC_MAKEKEY },
  345. { "-ecc", BENCH_ECC },
  346. #ifdef HAVE_ECC_ENCRYPT
  347. { "-ecc-enc", BENCH_ECC_ENCRYPT },
  348. #endif
  349. #endif
  350. #ifdef HAVE_CURVE25519
  351. { "-curve25519_kg", BENCH_CURVE25519_KEYGEN },
  352. #ifdef HAVE_CURVE25519_SHARED_SECRET
  353. { "-x25519", BENCH_CURVE25519_KA },
  354. #endif
  355. #endif
  356. #ifdef HAVE_ED25519
  357. { "-ed25519-kg", BENCH_ED25519_KEYGEN },
  358. { "-ed25519", BENCH_ED25519_SIGN },
  359. #endif
  360. { NULL, 0}
  361. };
  362. /* All recognized other cryptographic algorithm choosing command line options.
  363. */
  364. static const bench_alg bench_other_opt[] = {
  365. { "-other", -1 },
  366. #ifndef WC_NO_RNG
  367. { "-rng", BENCH_RNG },
  368. #endif
  369. #ifdef HAVE_SCRYPT
  370. { "-scrypt", BENCH_SCRYPT },
  371. #endif
  372. { NULL, 0}
  373. };
  374. #endif /* !WOLFSSL_BENCHMARK_ALL && !NO_MAIN_DRIVER */
  375. #ifdef HAVE_WNR
  376. const char* wnrConfigFile = "wnr-example.conf";
  377. #endif
  378. #if defined(WOLFSSL_MDK_ARM)
  379. extern FILE * wolfSSL_fopen(const char *fname, const char *mode);
  380. #define fopen wolfSSL_fopen
  381. #endif
  382. #if defined(__GNUC__) && defined(__x86_64__) && !defined(NO_ASM) && !defined(WOLFSSL_SGX)
  383. #define HAVE_GET_CYCLES
  384. static INLINE word64 get_intel_cycles(void);
  385. static THREAD_LS_T word64 total_cycles;
  386. #define INIT_CYCLE_COUNTER
  387. #define BEGIN_INTEL_CYCLES total_cycles = get_intel_cycles();
  388. #define END_INTEL_CYCLES total_cycles = get_intel_cycles() - total_cycles;
  389. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  390. #define SHOW_INTEL_CYCLES(b, n, s) \
  391. XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " Cycles per byte = %6.2f\n", \
  392. count == 0 ? 0 : (float)total_cycles / ((word64)count*s))
  393. #elif defined(LINUX_CYCLE_COUNT)
  394. #include <linux/perf_event.h>
  395. #include <sys/syscall.h>
  396. #include <unistd.h>
  397. static THREAD_LS_T word64 begin_cycles;
  398. static THREAD_LS_T word64 total_cycles;
  399. static THREAD_LS_T int cycles = -1;
  400. static THREAD_LS_T struct perf_event_attr atr;
  401. #define INIT_CYCLE_COUNTER do { \
  402. atr.type = PERF_TYPE_HARDWARE; \
  403. atr.config = PERF_COUNT_HW_CPU_CYCLES; \
  404. cycles = (int)syscall(__NR_perf_event_open, &atr, 0, -1, -1, 0); \
  405. } while (0);
  406. #define BEGIN_INTEL_CYCLES read(cycles, &begin_cycles, sizeof(begin_cycles));
  407. #define END_INTEL_CYCLES do { \
  408. read(cycles, &total_cycles, sizeof(total_cycles)); \
  409. total_cycles = total_cycles - begin_cycles; \
  410. } while (0);
  411. /* s == size in bytes that 1 count represents, normally BENCH_SIZE */
  412. #define SHOW_INTEL_CYCLES(b, n, s) \
  413. XSNPRINTF(b + XSTRLEN(b), n - XSTRLEN(b), " Cycles per byte = %6.2f\n", \
  414. (float)total_cycles / (count*s))
  415. #else
  416. #define INIT_CYCLE_COUNTER
  417. #define BEGIN_INTEL_CYCLES
  418. #define END_INTEL_CYCLES
  419. #define SHOW_INTEL_CYCLES(b, n, s) b[XSTRLEN(b)] = '\n'
  420. #endif
  421. /* let's use buffers, we have them */
  422. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  423. #define USE_CERT_BUFFERS_2048
  424. #endif
  425. #if defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048) \
  426. || !defined(NO_DH)
  427. /* include test cert and key buffers for use with NO_FILESYSTEM */
  428. #include <wolfssl/certs_test.h>
  429. #endif
  430. #ifdef HAVE_BLAKE2
  431. #include <wolfssl/wolfcrypt/blake2.h>
  432. #endif
  433. #ifdef _MSC_VER
  434. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  435. #pragma warning(disable: 4996)
  436. #endif
  437. #include "wolfcrypt/benchmark/benchmark.h"
  438. #ifdef WOLFSSL_CURRTIME_REMAP
  439. #define current_time WOLFSSL_CURRTIME_REMAP
  440. #elif !defined(HAVE_STACK_SIZE)
  441. double current_time(int);
  442. #endif
  443. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND) && \
  444. !defined(HAVE_STACK_SIZE)
  445. #ifdef __cplusplus
  446. extern "C" {
  447. #endif
  448. WOLFSSL_API int wolfSSL_Debugging_ON(void);
  449. WOLFSSL_API void wolfSSL_Debugging_OFF(void);
  450. #ifdef __cplusplus
  451. } /* extern "C" */
  452. #endif
  453. #endif
  454. #if !defined(NO_RSA) || !defined(NO_DH) \
  455. || defined(WOLFSSL_KEYGEN) || defined(HAVE_ECC) \
  456. || defined(HAVE_CURVE25519) || defined(HAVE_ED25519)
  457. #define HAVE_LOCAL_RNG
  458. static THREAD_LS_T WC_RNG rng;
  459. #endif
  460. #if defined(HAVE_ED25519) || defined(HAVE_CURVE25519) || defined(HAVE_ECC) || \
  461. defined(HAVE_ECC) || defined(HAVE_NTRU) || !defined(NO_DH) || \
  462. !defined(NO_RSA) || defined(HAVE_SCRYPT)
  463. #define BENCH_ASYM
  464. #endif
  465. /* Asynchronous helper macros */
  466. static THREAD_LS_T int devId = INVALID_DEVID;
  467. #ifdef WOLFSSL_ASYNC_CRYPT
  468. static WOLF_EVENT_QUEUE eventQueue;
  469. #define BENCH_ASYNC_GET_DEV(obj) (&(obj)->asyncDev)
  470. #define BENCH_ASYNC_GET_NAME(doAsync) (doAsync) ? "HW" : "SW"
  471. #define BENCH_MAX_PENDING (WOLF_ASYNC_MAX_PENDING)
  472. #ifndef WC_NO_ASYNC_THREADING
  473. typedef struct ThreadData {
  474. pthread_t thread_id;
  475. } ThreadData;
  476. static ThreadData* g_threadData;
  477. static int g_threadCount;
  478. #endif
  479. static int bench_async_check(int* ret, WC_ASYNC_DEV* asyncDev,
  480. int callAgain, int* times, int limit, int* pending)
  481. {
  482. int allowNext = 0;
  483. /* this state can be set from a different thread */
  484. WOLF_EVENT_STATE state = asyncDev->event.state;
  485. /* if algo doesn't require calling again then use this flow */
  486. if (state == WOLF_EVENT_STATE_DONE) {
  487. if (callAgain) {
  488. /* needs called again, so allow it and handle completion in bench_async_handle */
  489. allowNext = 1;
  490. }
  491. else {
  492. *ret = asyncDev->event.ret;
  493. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  494. (*times)++;
  495. if (*pending > 0) /* to support case where async blocks */
  496. (*pending)--;
  497. if ((*times + *pending) < limit)
  498. allowNext = 1;
  499. }
  500. }
  501. /* if slot is available and we haven't reached limit, start another */
  502. else if (state == WOLF_EVENT_STATE_READY && (*times + *pending) < limit) {
  503. allowNext = 1;
  504. }
  505. return allowNext;
  506. }
  507. static int bench_async_handle(int* ret, WC_ASYNC_DEV* asyncDev,
  508. int callAgain, int* times, int* pending)
  509. {
  510. WOLF_EVENT_STATE state = asyncDev->event.state;
  511. if (*ret == WC_PENDING_E) {
  512. if (state == WOLF_EVENT_STATE_DONE) {
  513. *ret = asyncDev->event.ret;
  514. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  515. (*times)++;
  516. (*pending)--;
  517. }
  518. else {
  519. (*pending)++;
  520. *ret = wc_AsyncHandle(asyncDev, &eventQueue,
  521. callAgain ? WC_ASYNC_FLAG_CALL_AGAIN : WC_ASYNC_FLAG_NONE);
  522. }
  523. }
  524. else if (*ret >= 0) {
  525. *ret = asyncDev->event.ret;
  526. asyncDev->event.state = WOLF_EVENT_STATE_READY;
  527. (*times)++;
  528. if (*pending > 0) /* to support case where async blocks */
  529. (*pending)--;
  530. }
  531. return (*ret >= 0) ? 1 : 0;
  532. }
  533. static INLINE int bench_async_poll(int* pending)
  534. {
  535. int ret, asyncDone = 0;
  536. ret = wolfAsync_EventQueuePoll(&eventQueue, NULL, NULL, 0,
  537. WOLF_POLL_FLAG_CHECK_HW, &asyncDone);
  538. if (ret != 0) {
  539. printf("Async poll failed %d\n", ret);
  540. return ret;
  541. }
  542. if (asyncDone == 0) {
  543. #ifndef WC_NO_ASYNC_THREADING
  544. /* give time to other threads */
  545. wc_AsyncThreadYield();
  546. #endif
  547. }
  548. (void)pending;
  549. return asyncDone;
  550. }
  551. #else
  552. #define BENCH_MAX_PENDING (1)
  553. #define BENCH_ASYNC_GET_NAME(doAsync) ""
  554. #define BENCH_ASYNC_GET_DEV(obj) NULL
  555. static INLINE int bench_async_check(int* ret, void* asyncDev,
  556. int callAgain, int* times, int limit, int* pending)
  557. {
  558. (void)ret;
  559. (void)asyncDev;
  560. (void)callAgain;
  561. (void)times;
  562. (void)limit;
  563. (void)pending;
  564. return 1;
  565. }
  566. static INLINE int bench_async_handle(int* ret, void* asyncDev,
  567. int callAgain, int* times, int* pending)
  568. {
  569. (void)asyncDev;
  570. (void)callAgain;
  571. (void)pending;
  572. if (*ret >= 0) {
  573. /* operation completed */
  574. (*times)++;
  575. return 1;
  576. }
  577. return 0;
  578. }
  579. #define bench_async_poll(p)
  580. #endif /* WOLFSSL_ASYNC_CRYPT */
  581. /* maximum runtime for each benchmark */
  582. #define BENCH_MIN_RUNTIME_SEC 1.0f
  583. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  584. #define AES_AUTH_ADD_SZ 13
  585. #define AES_AUTH_TAG_SZ 16
  586. #define BENCH_CIPHER_ADD AES_AUTH_TAG_SZ
  587. static word32 aesAuthAddSz = AES_AUTH_ADD_SZ;
  588. #endif
  589. #ifndef BENCH_CIPHER_ADD
  590. #define BENCH_CIPHER_ADD 0
  591. #endif
  592. /* use kB instead of mB for embedded benchmarking */
  593. #ifdef BENCH_EMBEDDED
  594. enum BenchmarkBounds {
  595. scryptCnt = 1,
  596. ntimes = 2,
  597. genTimes = BENCH_MAX_PENDING,
  598. agreeTimes = 2
  599. };
  600. static int numBlocks = 25; /* how many kB to test (en/de)cryption */
  601. static word32 bench_size = (1024ul);
  602. #else
  603. enum BenchmarkBounds {
  604. scryptCnt = 10,
  605. ntimes = 100,
  606. genTimes = BENCH_MAX_PENDING, /* must be at least BENCH_MAX_PENDING */
  607. agreeTimes = 100
  608. };
  609. static int numBlocks = 5; /* how many megs to test (en/de)cryption */
  610. static word32 bench_size = (1024*1024ul);
  611. #endif
  612. static int base2 = 1;
  613. static int digest_stream = 1;
  614. #ifndef NO_RSA
  615. /* Don't measure RSA sign/verify by default */
  616. static int rsa_sign_verify = 0;
  617. #endif
  618. /* for compatibility */
  619. #define BENCH_SIZE bench_size
  620. /* globals for cipher tests */
  621. static THREAD_LS_T byte* bench_plain = NULL;
  622. static THREAD_LS_T byte* bench_cipher = NULL;
  623. static const XGEN_ALIGN byte bench_key_buf[] =
  624. {
  625. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  626. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  627. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67,
  628. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  629. };
  630. static const XGEN_ALIGN byte bench_iv_buf[] =
  631. {
  632. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  633. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  634. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  635. };
  636. static THREAD_LS_T byte* bench_key = NULL;
  637. static THREAD_LS_T byte* bench_iv = NULL;
  638. #ifdef WOLFSSL_STATIC_MEMORY
  639. #ifdef BENCH_EMBEDDED
  640. static byte gBenchMemory[50000];
  641. #else
  642. static byte gBenchMemory[400000];
  643. #endif
  644. #endif
  645. /******************************************************************************/
  646. /* Begin Stats Functions */
  647. /******************************************************************************/
  648. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  649. typedef enum bench_stat_type {
  650. BENCH_STAT_ASYM,
  651. BENCH_STAT_SYM,
  652. } bench_stat_type_t;
  653. typedef struct bench_stats {
  654. struct bench_stats* next;
  655. struct bench_stats* prev;
  656. const char* algo;
  657. const char* desc;
  658. double perfsec;
  659. int strength;
  660. int doAsync;
  661. int finishCount;
  662. bench_stat_type_t type;
  663. int lastRet;
  664. } bench_stats_t;
  665. static bench_stats_t* bench_stats_head;
  666. static bench_stats_t* bench_stats_tail;
  667. static pthread_mutex_t bench_lock = PTHREAD_MUTEX_INITIALIZER;
  668. static bench_stats_t* bench_stats_add(bench_stat_type_t type,
  669. const char* algo, int strength, const char* desc, int doAsync,
  670. double perfsec, int ret)
  671. {
  672. bench_stats_t* stat;
  673. /* protect bench_stats_head and bench_stats_tail access */
  674. pthread_mutex_lock(&bench_lock);
  675. /* locate existing in list */
  676. for (stat = bench_stats_head; stat != NULL; stat = stat->next) {
  677. /* match based on algo, strength and desc */
  678. if (stat->algo == algo && stat->strength == strength && stat->desc == desc && stat->doAsync == doAsync) {
  679. break;
  680. }
  681. }
  682. if (stat == NULL) {
  683. /* allocate new and put on list */
  684. stat = (bench_stats_t*)XMALLOC(sizeof(bench_stats_t), NULL, DYNAMIC_TYPE_INFO);
  685. if (stat) {
  686. XMEMSET(stat, 0, sizeof(bench_stats_t));
  687. /* add to list */
  688. stat->next = NULL;
  689. if (bench_stats_tail == NULL) {
  690. bench_stats_head = stat;
  691. }
  692. else {
  693. bench_stats_tail->next = stat;
  694. stat->prev = bench_stats_tail;
  695. }
  696. bench_stats_tail = stat; /* add to the end either way */
  697. }
  698. }
  699. if (stat) {
  700. int isLast = 0;
  701. stat->type = type;
  702. stat->algo = algo;
  703. stat->strength = strength;
  704. stat->desc = desc;
  705. stat->doAsync = doAsync;
  706. stat->perfsec += perfsec;
  707. stat->finishCount++;
  708. if (stat->lastRet > ret)
  709. stat->lastRet = ret; /* track last error */
  710. if (stat->finishCount == g_threadCount) {
  711. isLast = 1;
  712. }
  713. pthread_mutex_unlock(&bench_lock);
  714. /* wait until remaining are complete */
  715. while (stat->finishCount < g_threadCount) {
  716. wc_AsyncThreadYield();
  717. }
  718. /* print final stat */
  719. if (isLast) {
  720. if (stat->type == BENCH_STAT_SYM) {
  721. printf("%-12s%s %8.3f MB/s\n", stat->desc,
  722. BENCH_ASYNC_GET_NAME(stat->doAsync), stat->perfsec);
  723. }
  724. else {
  725. printf("%-5s %4d %-9s %s %.3f ops/sec\n",
  726. stat->algo, stat->strength, stat->desc,
  727. BENCH_ASYNC_GET_NAME(stat->doAsync), stat->perfsec);
  728. }
  729. }
  730. }
  731. else {
  732. pthread_mutex_unlock(&bench_lock);
  733. }
  734. return stat;
  735. }
  736. #endif /* WOLFSSL_ASYNC_CRYPT && !WC_NO_ASYNC_THREADING */
  737. static INLINE void bench_stats_init(void)
  738. {
  739. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  740. bench_stats_head = NULL;
  741. bench_stats_tail = NULL;
  742. #endif
  743. INIT_CYCLE_COUNTER
  744. }
  745. static INLINE void bench_stats_start(int* count, double* start)
  746. {
  747. *count = 0;
  748. *start = current_time(1);
  749. BEGIN_INTEL_CYCLES
  750. }
  751. static INLINE int bench_stats_sym_check(double start)
  752. {
  753. return ((current_time(0) - start) < BENCH_MIN_RUNTIME_SEC);
  754. }
  755. /* countSz is number of bytes that 1 count represents. Normally bench_size,
  756. * except for AES direct that operates on AES_BLOCK_SIZE blocks */
  757. static void bench_stats_sym_finish(const char* desc, int doAsync, int count,
  758. int countSz, double start, int ret)
  759. {
  760. double total, persec = 0, blocks = count;
  761. const char* blockType;
  762. char msg[128] = {0};
  763. END_INTEL_CYCLES
  764. total = current_time(0) - start;
  765. /* calculate actual bytes */
  766. blocks *= countSz;
  767. if (base2) {
  768. /* determine if we should show as KB or MB */
  769. if (blocks > (1024 * 1024)) {
  770. blocks /= (1024 * 1024);
  771. blockType = "MB";
  772. }
  773. else if (blocks > 1024) {
  774. blocks /= 1024; /* make KB */
  775. blockType = "KB";
  776. }
  777. else {
  778. blockType = "bytes";
  779. }
  780. }
  781. else {
  782. /* determine if we should show as kB or mB */
  783. if (blocks > (1000 * 1000)) {
  784. blocks /= (1000 * 1000);
  785. blockType = "mB";
  786. }
  787. else if (blocks > 1000) {
  788. blocks /= 1000; /* make kB */
  789. blockType = "kB";
  790. }
  791. else {
  792. blockType = "bytes";
  793. }
  794. }
  795. /* caclulcate blocks per second */
  796. if (total > 0) {
  797. persec = (1 / total) * blocks;
  798. }
  799. XSNPRINTF(msg, sizeof(msg), "%-16s%s %5.0f %s took %5.3f seconds, %8.3f %s/s",
  800. desc, BENCH_ASYNC_GET_NAME(doAsync), blocks, blockType, total,
  801. persec, blockType);
  802. SHOW_INTEL_CYCLES(msg, sizeof(msg), countSz);
  803. printf("%s", msg);
  804. /* show errors */
  805. if (ret < 0) {
  806. printf("Benchmark %s failed: %d\n", desc, ret);
  807. }
  808. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  809. /* Add to thread stats */
  810. bench_stats_add(BENCH_STAT_SYM, NULL, 0, desc, doAsync, persec, ret);
  811. #endif
  812. (void)doAsync;
  813. (void)ret;
  814. }
  815. #ifdef BENCH_ASYM
  816. static void bench_stats_asym_finish(const char* algo, int strength,
  817. const char* desc, int doAsync, int count, double start, int ret)
  818. {
  819. double total, each = 0, opsSec, milliEach;
  820. total = current_time(0) - start;
  821. if (count > 0)
  822. each = total / count; /* per second */
  823. opsSec = count / total; /* ops second */
  824. milliEach = each * 1000; /* milliseconds */
  825. printf("%-6s %5d %-9s %s %6d ops took %5.3f sec, avg %5.3f ms,"
  826. " %.3f ops/sec\n", algo, strength, desc, BENCH_ASYNC_GET_NAME(doAsync),
  827. count, total, milliEach, opsSec);
  828. /* show errors */
  829. if (ret < 0) {
  830. printf("Benchmark %s %s %d failed: %d\n", algo, desc, strength, ret);
  831. }
  832. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  833. /* Add to thread stats */
  834. bench_stats_add(BENCH_STAT_ASYM, algo, strength, desc, doAsync, opsSec, ret);
  835. #endif
  836. (void)doAsync;
  837. (void)ret;
  838. }
  839. #endif /* BENCH_ASYM */
  840. static INLINE void bench_stats_free(void)
  841. {
  842. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  843. bench_stats_t* stat;
  844. for (stat = bench_stats_head; stat != NULL; ) {
  845. bench_stats_t* next = stat->next;
  846. XFREE(stat, NULL, DYNAMIC_TYPE_INFO);
  847. stat = next;
  848. }
  849. bench_stats_head = NULL;
  850. bench_stats_tail = NULL;
  851. #endif
  852. }
  853. /******************************************************************************/
  854. /* End Stats Functions */
  855. /******************************************************************************/
  856. static void* benchmarks_do(void* args)
  857. {
  858. int bench_buf_size;
  859. #ifdef WOLFSSL_ASYNC_CRYPT
  860. #ifndef WC_NO_ASYNC_THREADING
  861. ThreadData* threadData = (ThreadData*)args;
  862. if (wolfAsync_DevOpenThread(&devId, &threadData->thread_id) < 0)
  863. #else
  864. if (wolfAsync_DevOpen(&devId) < 0)
  865. #endif
  866. {
  867. printf("Async device open failed\nRunning without async\n");
  868. }
  869. #endif /* WOLFSSL_ASYNC_CRYPT */
  870. (void)args;
  871. #ifdef WOLFSSL_ASYNC_CRYPT
  872. if (wolfEventQueue_Init(&eventQueue) != 0) {
  873. printf("Async event queue init failure!\n");
  874. }
  875. #endif
  876. #if defined(HAVE_LOCAL_RNG)
  877. {
  878. int rngRet;
  879. #ifndef HAVE_FIPS
  880. rngRet = wc_InitRng_ex(&rng, HEAP_HINT, devId);
  881. #else
  882. rngRet = wc_InitRng(&rng);
  883. #endif
  884. if (rngRet < 0) {
  885. printf("InitRNG failed\n");
  886. return NULL;
  887. }
  888. }
  889. #endif
  890. /* setup bench plain, cipher, key and iv globals */
  891. /* make sure bench buffer is multiple of 16 (AES block size) */
  892. bench_buf_size = (int)bench_size + BENCH_CIPHER_ADD;
  893. if (bench_buf_size % 16)
  894. bench_buf_size += 16 - (bench_buf_size % 16);
  895. bench_plain = (byte*)XMALLOC((size_t)bench_buf_size, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  896. bench_cipher = (byte*)XMALLOC((size_t)bench_buf_size, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  897. if (bench_plain == NULL || bench_cipher == NULL) {
  898. XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  899. XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  900. bench_plain = bench_cipher = NULL;
  901. printf("Benchmark block buffer alloc failed!\n");
  902. goto exit;
  903. }
  904. XMEMSET(bench_plain, 0, (size_t)bench_buf_size);
  905. XMEMSET(bench_cipher, 0, (size_t)bench_buf_size);
  906. #ifdef WOLFSSL_ASYNC_CRYPT
  907. bench_key = (byte*)XMALLOC(sizeof(bench_key_buf), HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  908. bench_iv = (byte*)XMALLOC(sizeof(bench_iv_buf), HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  909. if (bench_key == NULL || bench_iv == NULL) {
  910. XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  911. XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  912. bench_key = bench_iv = NULL;
  913. printf("Benchmark cipher buffer alloc failed!\n");
  914. goto exit;
  915. }
  916. XMEMCPY(bench_key, bench_key_buf, sizeof(bench_key_buf));
  917. XMEMCPY(bench_iv, bench_iv_buf, sizeof(bench_iv_buf));
  918. #else
  919. bench_key = (byte*)bench_key_buf;
  920. bench_iv = (byte*)bench_iv_buf;
  921. #endif
  922. #ifndef WC_NO_RNG
  923. if (bench_all || (bench_other_algs & BENCH_RNG))
  924. bench_rng();
  925. #endif /* WC_NO_RNG */
  926. #ifndef NO_AES
  927. #ifdef HAVE_AES_CBC
  928. if (bench_all || (bench_cipher_algs & BENCH_AES_CBC)) {
  929. #ifndef NO_SW_BENCH
  930. bench_aescbc(0);
  931. #endif
  932. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
  933. bench_aescbc(1);
  934. #endif
  935. }
  936. #endif
  937. #ifdef HAVE_AESGCM
  938. if (bench_all || (bench_cipher_algs & BENCH_AES_GCM)) {
  939. #ifndef NO_SW_BENCH
  940. bench_aesgcm(0);
  941. #endif
  942. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
  943. bench_aesgcm(1);
  944. #endif
  945. }
  946. #endif
  947. #ifdef WOLFSSL_AES_DIRECT
  948. if (bench_all || (bench_cipher_algs & BENCH_AES_ECB)) {
  949. #ifndef NO_SW_BENCH
  950. bench_aesecb(0);
  951. #endif
  952. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
  953. bench_aesecb(1);
  954. #endif
  955. }
  956. #endif
  957. #ifdef WOLFSSL_AES_XTS
  958. if (bench_all || (bench_cipher_algs & BENCH_AES_XTS))
  959. bench_aesxts();
  960. #endif
  961. #ifdef WOLFSSL_AES_CFB
  962. if (bench_all || (bench_cipher_algs & BENCH_AES_CFB))
  963. bench_aescfb();
  964. #endif
  965. #ifdef WOLFSSL_AES_COUNTER
  966. if (bench_all || (bench_cipher_algs & BENCH_AES_CTR))
  967. bench_aesctr();
  968. #endif
  969. #ifdef HAVE_AESCCM
  970. if (bench_all || (bench_cipher_algs & BENCH_AES_CCM))
  971. bench_aesccm();
  972. #endif
  973. #endif /* !NO_AES */
  974. #ifdef HAVE_CAMELLIA
  975. if (bench_all || (bench_cipher_algs & BENCH_CAMELLIA))
  976. bench_camellia();
  977. #endif
  978. #ifndef NO_RC4
  979. if (bench_all || (bench_cipher_algs & BENCH_ARC4)) {
  980. #ifndef NO_SW_BENCH
  981. bench_arc4(0);
  982. #endif
  983. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4)
  984. bench_arc4(1);
  985. #endif
  986. }
  987. #endif
  988. #ifdef HAVE_HC128
  989. if (bench_all || (bench_cipher_algs & BENCH_HC128))
  990. bench_hc128();
  991. #endif
  992. #ifndef NO_RABBIT
  993. if (bench_all || (bench_cipher_algs & BENCH_RABBIT))
  994. bench_rabbit();
  995. #endif
  996. #ifdef HAVE_CHACHA
  997. if (bench_all || (bench_cipher_algs & BENCH_CHACHA20))
  998. bench_chacha();
  999. #endif
  1000. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  1001. if (bench_all || (bench_cipher_algs & BENCH_CHACHA20_POLY1305))
  1002. bench_chacha20_poly1305_aead();
  1003. #endif
  1004. #ifndef NO_DES3
  1005. if (bench_all || (bench_cipher_algs & BENCH_DES)) {
  1006. #ifndef NO_SW_BENCH
  1007. bench_des(0);
  1008. #endif
  1009. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_3DES)
  1010. bench_des(1);
  1011. #endif
  1012. }
  1013. #endif
  1014. #ifdef HAVE_IDEA
  1015. if (bench_all || (bench_cipher_algs & BENCH_IDEA))
  1016. bench_idea();
  1017. #endif
  1018. #ifndef NO_MD5
  1019. if (bench_all || (bench_digest_algs & BENCH_MD5)) {
  1020. #ifndef NO_SW_BENCH
  1021. bench_md5(0);
  1022. #endif
  1023. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_MD5)
  1024. bench_md5(1);
  1025. #endif
  1026. }
  1027. #endif
  1028. #ifdef HAVE_POLY1305
  1029. if (bench_all || (bench_digest_algs & BENCH_POLY1305))
  1030. bench_poly1305();
  1031. #endif
  1032. #ifndef NO_SHA
  1033. if (bench_all || (bench_digest_algs & BENCH_SHA)) {
  1034. #ifndef NO_SW_BENCH
  1035. bench_sha(0);
  1036. #endif
  1037. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
  1038. bench_sha(1);
  1039. #endif
  1040. }
  1041. #endif
  1042. #ifdef WOLFSSL_SHA224
  1043. if (bench_all || (bench_digest_algs & BENCH_SHA224)) {
  1044. #ifndef NO_SW_BENCH
  1045. bench_sha224(0);
  1046. #endif
  1047. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
  1048. bench_sha224(1);
  1049. #endif
  1050. }
  1051. #endif
  1052. #ifndef NO_SHA256
  1053. if (bench_all || (bench_digest_algs & BENCH_SHA256)) {
  1054. #ifndef NO_SW_BENCH
  1055. bench_sha256(0);
  1056. #endif
  1057. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
  1058. bench_sha256(1);
  1059. #endif
  1060. }
  1061. #endif
  1062. #ifdef WOLFSSL_SHA384
  1063. if (bench_all || (bench_digest_algs & BENCH_SHA384)) {
  1064. #ifndef NO_SW_BENCH
  1065. bench_sha384(0);
  1066. #endif
  1067. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
  1068. bench_sha384(1);
  1069. #endif
  1070. }
  1071. #endif
  1072. #ifdef WOLFSSL_SHA512
  1073. if (bench_all || (bench_digest_algs & BENCH_SHA512)) {
  1074. #ifndef NO_SW_BENCH
  1075. bench_sha512(0);
  1076. #endif
  1077. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
  1078. bench_sha512(1);
  1079. #endif
  1080. }
  1081. #endif
  1082. #ifdef WOLFSSL_SHA3
  1083. #ifndef WOLFSSL_NOSHA3_224
  1084. if (bench_all || (bench_digest_algs & BENCH_SHA3_224)) {
  1085. #ifndef NO_SW_BENCH
  1086. bench_sha3_224(0);
  1087. #endif
  1088. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
  1089. bench_sha3_224(1);
  1090. #endif
  1091. }
  1092. #endif /* WOLFSSL_NOSHA3_224 */
  1093. #ifndef WOLFSSL_NOSHA3_256
  1094. if (bench_all || (bench_digest_algs & BENCH_SHA3_256)) {
  1095. #ifndef NO_SW_BENCH
  1096. bench_sha3_256(0);
  1097. #endif
  1098. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
  1099. bench_sha3_256(1);
  1100. #endif
  1101. }
  1102. #endif /* WOLFSSL_NOSHA3_256 */
  1103. #ifndef WOLFSSL_NOSHA3_384
  1104. if (bench_all || (bench_digest_algs & BENCH_SHA3_384)) {
  1105. #ifndef NO_SW_BENCH
  1106. bench_sha3_384(0);
  1107. #endif
  1108. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3)
  1109. bench_sha3_384(1);
  1110. #endif
  1111. }
  1112. #endif /* WOLFSSL_NOSHA3_384 */
  1113. #ifndef WOLFSSL_NOSHA3_512
  1114. if (bench_all || (bench_digest_algs & BENCH_SHA3_512)) {
  1115. #ifndef NO_SW_BENCH
  1116. bench_sha3_512(0);
  1117. #endif
  1118. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
  1119. bench_sha3_512(1);
  1120. #endif
  1121. }
  1122. #endif /* WOLFSSL_NOSHA3_512 */
  1123. #endif
  1124. #ifdef WOLFSSL_RIPEMD
  1125. if (bench_all || (bench_digest_algs & BENCH_RIPEMD))
  1126. bench_ripemd();
  1127. #endif
  1128. #ifdef HAVE_BLAKE2
  1129. if (bench_all || (bench_digest_algs & BENCH_BLAKE2))
  1130. bench_blake2();
  1131. #endif
  1132. #ifdef WOLFSSL_CMAC
  1133. if (bench_all || (bench_mac_algs & BENCH_CMAC))
  1134. bench_cmac();
  1135. #endif
  1136. #ifndef NO_HMAC
  1137. #ifndef NO_MD5
  1138. if (bench_all || (bench_mac_algs & BENCH_HMAC_MD5)) {
  1139. #ifndef NO_SW_BENCH
  1140. bench_hmac_md5(0);
  1141. #endif
  1142. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_HMAC)
  1143. bench_hmac_md5(1);
  1144. #endif
  1145. }
  1146. #endif
  1147. #ifndef NO_SHA
  1148. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA)) {
  1149. #ifndef NO_SW_BENCH
  1150. bench_hmac_sha(0);
  1151. #endif
  1152. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA)
  1153. bench_hmac_sha(1);
  1154. #endif
  1155. }
  1156. #endif
  1157. #ifdef WOLFSSL_SHA224
  1158. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA224)) {
  1159. #ifndef NO_SW_BENCH
  1160. bench_hmac_sha224(0);
  1161. #endif
  1162. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA224)
  1163. bench_hmac_sha224(1);
  1164. #endif
  1165. }
  1166. #endif
  1167. #ifndef NO_SHA256
  1168. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA256)) {
  1169. #ifndef NO_SW_BENCH
  1170. bench_hmac_sha256(0);
  1171. #endif
  1172. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA256)
  1173. bench_hmac_sha256(1);
  1174. #endif
  1175. }
  1176. #endif
  1177. #ifdef WOLFSSL_SHA384
  1178. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA384)) {
  1179. #ifndef NO_SW_BENCH
  1180. bench_hmac_sha384(0);
  1181. #endif
  1182. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA384)
  1183. bench_hmac_sha384(1);
  1184. #endif
  1185. }
  1186. #endif
  1187. #ifdef WOLFSSL_SHA512
  1188. if (bench_all || (bench_mac_algs & BENCH_HMAC_SHA512)) {
  1189. #ifndef NO_SW_BENCH
  1190. bench_hmac_sha512(0);
  1191. #endif
  1192. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA512)
  1193. bench_hmac_sha512(1);
  1194. #endif
  1195. }
  1196. #endif
  1197. #endif /* NO_HMAC */
  1198. #ifdef HAVE_SCRYPT
  1199. if (bench_all || (bench_other_algs & BENCH_SCRYPT))
  1200. bench_scrypt();
  1201. #endif
  1202. #ifndef NO_RSA
  1203. #ifdef WOLFSSL_KEY_GEN
  1204. if (bench_all || (bench_asym_algs & BENCH_RSA_KEYGEN)) {
  1205. #ifndef NO_SW_BENCH
  1206. bench_rsaKeyGen(0);
  1207. #endif
  1208. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA)
  1209. /* async supported in simulator only */
  1210. #ifdef WOLFSSL_ASYNC_CRYPT_TEST
  1211. bench_rsaKeyGen(1);
  1212. #endif
  1213. #endif
  1214. }
  1215. #endif
  1216. if (bench_all || (bench_asym_algs & BENCH_RSA)) {
  1217. #ifndef NO_SW_BENCH
  1218. bench_rsa(0);
  1219. #endif
  1220. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA)
  1221. bench_rsa(1);
  1222. #endif
  1223. }
  1224. #endif
  1225. #ifndef NO_DH
  1226. if (bench_all || (bench_asym_algs & BENCH_DH)) {
  1227. #ifndef NO_SW_BENCH
  1228. bench_dh(0);
  1229. #endif
  1230. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_DH)
  1231. bench_dh(1);
  1232. #endif
  1233. }
  1234. #endif
  1235. #ifdef HAVE_NTRU
  1236. if (bench_all || (bench_asym_algs & BENCH_NTRU))
  1237. bench_ntru();
  1238. if (bench_all || (bench_asym_algs & BENCH_NTRU_KEYGEN))
  1239. bench_ntruKeyGen();
  1240. #endif
  1241. #ifdef HAVE_ECC
  1242. if (bench_all || (bench_asym_algs & BENCH_ECC_MAKEKEY)) {
  1243. #ifndef NO_SW_BENCH
  1244. bench_eccMakeKey(0);
  1245. #endif
  1246. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  1247. /* async supported in simulator only */
  1248. #ifdef WOLFSSL_ASYNC_CRYPT_TEST
  1249. bench_eccMakeKey(1);
  1250. #endif
  1251. #endif
  1252. }
  1253. if (bench_all || (bench_asym_algs & BENCH_ECC)) {
  1254. #ifndef NO_SW_BENCH
  1255. bench_ecc(0);
  1256. #endif
  1257. #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC)
  1258. bench_ecc(1);
  1259. #endif
  1260. }
  1261. #ifdef HAVE_ECC_ENCRYPT
  1262. if (bench_all || (bench_asym_algs & BENCH_ECC_ENCRYPT))
  1263. bench_eccEncrypt();
  1264. #endif
  1265. #endif
  1266. #ifdef HAVE_CURVE25519
  1267. if (bench_all || (bench_asym_algs & BENCH_CURVE25519_KEYGEN))
  1268. bench_curve25519KeyGen();
  1269. #ifdef HAVE_CURVE25519_SHARED_SECRET
  1270. if (bench_all || (bench_asym_algs & BENCH_CURVE25519_KA))
  1271. bench_curve25519KeyAgree();
  1272. #endif
  1273. #endif
  1274. #ifdef HAVE_ED25519
  1275. if (bench_all || (bench_asym_algs & BENCH_ED25519_KEYGEN))
  1276. bench_ed25519KeyGen();
  1277. if (bench_all || (bench_asym_algs & BENCH_ED25519_SIGN))
  1278. bench_ed25519KeySign();
  1279. #endif
  1280. exit:
  1281. /* free benchmark buffers */
  1282. XFREE(bench_plain, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1283. XFREE(bench_cipher, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1284. #ifdef WOLFSSL_ASYNC_CRYPT
  1285. XFREE(bench_key, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1286. XFREE(bench_iv, HEAP_HINT, DYNAMIC_TYPE_WOLF_BIGINT);
  1287. #endif
  1288. #ifdef WOLFSSL_ASYNC_CRYPT
  1289. /* free event queue */
  1290. wolfEventQueue_Free(&eventQueue);
  1291. #endif
  1292. #if defined(HAVE_LOCAL_RNG)
  1293. wc_FreeRng(&rng);
  1294. #endif
  1295. #ifdef WOLFSSL_ASYNC_CRYPT
  1296. wolfAsync_DevClose(&devId);
  1297. #endif
  1298. /* cleanup the thread if fixed point cache is enabled and have thread local */
  1299. #if defined(HAVE_THREAD_LS) && defined(HAVE_ECC) && defined(FP_ECC)
  1300. wc_ecc_fp_free();
  1301. #endif
  1302. (void)bench_cipher_algs;
  1303. (void)bench_digest_algs;
  1304. (void)bench_mac_algs;
  1305. (void)bench_asym_algs;
  1306. (void)bench_other_algs;
  1307. return NULL;
  1308. }
  1309. int benchmark_init(void)
  1310. {
  1311. int ret = 0;
  1312. #ifdef WOLFSSL_STATIC_MEMORY
  1313. ret = wc_LoadStaticMemory(&HEAP_HINT, gBenchMemory, sizeof(gBenchMemory),
  1314. WOLFMEM_GENERAL, 1);
  1315. if (ret != 0) {
  1316. printf("unable to load static memory %d\n", ret);
  1317. }
  1318. #endif /* WOLFSSL_STATIC_MEMORY */
  1319. if ((ret = wolfCrypt_Init()) != 0) {
  1320. printf("wolfCrypt_Init failed %d\n", ret);
  1321. return EXIT_FAILURE;
  1322. }
  1323. bench_stats_init();
  1324. #if defined(DEBUG_WOLFSSL) && !defined(HAVE_VALGRIND)
  1325. wolfSSL_Debugging_ON();
  1326. #endif
  1327. printf("wolfCrypt Benchmark (block bytes %d, min %.1f sec each)\n",
  1328. BENCH_SIZE, BENCH_MIN_RUNTIME_SEC);
  1329. #ifdef HAVE_WNR
  1330. ret = wc_InitNetRandom(wnrConfigFile, NULL, 5000);
  1331. if (ret != 0) {
  1332. printf("Whitewood netRandom config init failed %d\n", ret);
  1333. }
  1334. #endif /* HAVE_WNR */
  1335. return ret;
  1336. }
  1337. int benchmark_free(void)
  1338. {
  1339. int ret;
  1340. #ifdef HAVE_WNR
  1341. ret = wc_FreeNetRandom();
  1342. if (ret < 0) {
  1343. printf("Failed to free netRandom context %d\n", ret);
  1344. }
  1345. #endif
  1346. bench_stats_free();
  1347. if ((ret = wolfCrypt_Cleanup()) != 0) {
  1348. printf("error %d with wolfCrypt_Cleanup\n", ret);
  1349. }
  1350. return ret;
  1351. }
  1352. /* so embedded projects can pull in tests on their own */
  1353. #ifdef HAVE_STACK_SIZE
  1354. THREAD_RETURN WOLFSSL_THREAD benchmark_test(void* args)
  1355. #else
  1356. int benchmark_test(void *args)
  1357. #endif
  1358. {
  1359. int ret;
  1360. (void)args;
  1361. ret = benchmark_init();
  1362. if (ret != 0)
  1363. EXIT_TEST(ret);
  1364. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_NO_ASYNC_THREADING)
  1365. {
  1366. int i;
  1367. int numCpus = wc_AsyncGetNumberOfCpus();
  1368. printf("CPUs: %d\n", numCpus);
  1369. g_threadData = (ThreadData*)XMALLOC(sizeof(ThreadData) * numCpus,
  1370. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1371. if (g_threadData == NULL) {
  1372. printf("Thread data alloc failed!\n");
  1373. EXIT_TEST(EXIT_FAILURE);
  1374. }
  1375. g_threadCount = numCpus;
  1376. /* Create threads */
  1377. for (i = 0; i < numCpus; i++) {
  1378. ret = wc_AsyncThreadCreate(&g_threadData[i].thread_id,
  1379. benchmarks_do, &g_threadData[i]);
  1380. if (ret != 0) {
  1381. printf("Error creating benchmark thread %d\n", ret);
  1382. EXIT_TEST(EXIT_FAILURE);
  1383. }
  1384. }
  1385. /* Start threads */
  1386. for (i = 0; i < numCpus; i++) {
  1387. wc_AsyncThreadJoin(&g_threadData[i].thread_id);
  1388. }
  1389. XFREE(g_threadData, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1390. }
  1391. #else
  1392. benchmarks_do(NULL);
  1393. #endif
  1394. ret = benchmark_free();
  1395. EXIT_TEST(ret);
  1396. }
  1397. #ifndef WC_NO_RNG
  1398. void bench_rng(void)
  1399. {
  1400. int ret, i, count;
  1401. double start;
  1402. int pos, len, remain;
  1403. WC_RNG myrng;
  1404. #ifndef HAVE_FIPS
  1405. ret = wc_InitRng_ex(&myrng, HEAP_HINT, devId);
  1406. #else
  1407. ret = wc_InitRng(&myrng);
  1408. #endif
  1409. if (ret < 0) {
  1410. printf("InitRNG failed %d\n", ret);
  1411. return;
  1412. }
  1413. bench_stats_start(&count, &start);
  1414. do {
  1415. for (i = 0; i < numBlocks; i++) {
  1416. /* Split request to handle large RNG request */
  1417. pos = 0;
  1418. remain = (int)BENCH_SIZE;
  1419. while (remain > 0) {
  1420. len = remain;
  1421. if (len > RNG_MAX_BLOCK_LEN)
  1422. len = RNG_MAX_BLOCK_LEN;
  1423. ret = wc_RNG_GenerateBlock(&myrng, &bench_plain[pos], (word32)len);
  1424. if (ret < 0)
  1425. goto exit_rng;
  1426. remain -= len;
  1427. pos += len;
  1428. }
  1429. }
  1430. count += i;
  1431. } while (bench_stats_sym_check(start));
  1432. exit_rng:
  1433. bench_stats_sym_finish("RNG", 0, count, bench_size, start, ret);
  1434. wc_FreeRng(&myrng);
  1435. }
  1436. #endif /* WC_NO_RNG */
  1437. #ifndef NO_AES
  1438. #ifdef HAVE_AES_CBC
  1439. static void bench_aescbc_internal(int doAsync, const byte* key, word32 keySz,
  1440. const byte* iv, const char* encLabel,
  1441. const char* decLabel)
  1442. {
  1443. int ret = 0, i, count = 0, times, pending = 0;
  1444. Aes enc[BENCH_MAX_PENDING];
  1445. double start;
  1446. /* clear for done cleanup */
  1447. XMEMSET(enc, 0, sizeof(enc));
  1448. /* init keys */
  1449. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1450. if ((ret = wc_AesInit(&enc[i], HEAP_HINT,
  1451. doAsync ? devId : INVALID_DEVID)) != 0) {
  1452. printf("AesInit failed, ret = %d\n", ret);
  1453. goto exit;
  1454. }
  1455. ret = wc_AesSetKey(&enc[i], key, keySz, iv, AES_ENCRYPTION);
  1456. if (ret != 0) {
  1457. printf("AesSetKey failed, ret = %d\n", ret);
  1458. goto exit;
  1459. }
  1460. }
  1461. bench_stats_start(&count, &start);
  1462. do {
  1463. for (times = 0; times < numBlocks || pending > 0; ) {
  1464. bench_async_poll(&pending);
  1465. /* while free pending slots in queue, submit ops */
  1466. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1467. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, numBlocks, &pending)) {
  1468. ret = wc_AesCbcEncrypt(&enc[i], bench_plain, bench_cipher,
  1469. BENCH_SIZE);
  1470. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, &pending)) {
  1471. goto exit_aes_enc;
  1472. }
  1473. }
  1474. } /* for i */
  1475. } /* for times */
  1476. count += times;
  1477. } while (bench_stats_sym_check(start));
  1478. exit_aes_enc:
  1479. bench_stats_sym_finish(encLabel, doAsync, count, bench_size, start, ret);
  1480. if (ret < 0) {
  1481. goto exit;
  1482. }
  1483. #ifdef HAVE_AES_DECRYPT
  1484. /* init keys */
  1485. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1486. ret = wc_AesSetKey(&enc[i], key, keySz, iv, AES_DECRYPTION);
  1487. if (ret != 0) {
  1488. printf("AesSetKey failed, ret = %d\n", ret);
  1489. goto exit;
  1490. }
  1491. }
  1492. bench_stats_start(&count, &start);
  1493. do {
  1494. for (times = 0; times < numBlocks || pending > 0; ) {
  1495. bench_async_poll(&pending);
  1496. /* while free pending slots in queue, submit ops */
  1497. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1498. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, numBlocks, &pending)) {
  1499. ret = wc_AesCbcDecrypt(&enc[i], bench_plain, bench_cipher,
  1500. BENCH_SIZE);
  1501. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, &pending)) {
  1502. goto exit_aes_dec;
  1503. }
  1504. }
  1505. } /* for i */
  1506. } /* for times */
  1507. count += times;
  1508. } while (bench_stats_sym_check(start));
  1509. exit_aes_dec:
  1510. bench_stats_sym_finish(decLabel, doAsync, count, bench_size, start, ret);
  1511. #endif /* HAVE_AES_DECRYPT */
  1512. (void)decLabel;
  1513. exit:
  1514. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1515. wc_AesFree(&enc[i]);
  1516. }
  1517. }
  1518. void bench_aescbc(int doAsync)
  1519. {
  1520. #ifdef WOLFSSL_AES_128
  1521. bench_aescbc_internal(doAsync, bench_key, 16, bench_iv,
  1522. "AES-128-CBC-enc", "AES-128-CBC-dec");
  1523. #endif
  1524. #ifdef WOLFSSL_AES_192
  1525. bench_aescbc_internal(doAsync, bench_key, 24, bench_iv,
  1526. "AES-192-CBC-enc", "AES-192-CBC-dec");
  1527. #endif
  1528. #ifdef WOLFSSL_AES_256
  1529. bench_aescbc_internal(doAsync, bench_key, 32, bench_iv,
  1530. "AES-256-CBC-enc", "AES-256-CBC-dec");
  1531. #endif
  1532. }
  1533. #endif /* HAVE_AES_CBC */
  1534. #ifdef HAVE_AESGCM
  1535. static void bench_aesgcm_internal(int doAsync, const byte* key, word32 keySz,
  1536. const byte* iv, word32 ivSz,
  1537. const char* encLabel, const char* decLabel)
  1538. {
  1539. int ret = 0, i, count = 0, times, pending = 0;
  1540. Aes enc[BENCH_MAX_PENDING];
  1541. double start;
  1542. DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  1543. DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  1544. /* clear for done cleanup */
  1545. XMEMSET(enc, 0, sizeof(enc));
  1546. #ifdef WOLFSSL_ASYNC_CRYPT
  1547. if (bench_additional)
  1548. #endif
  1549. { XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ); }
  1550. #ifdef WOLFSSL_ASYNC_CRYPT
  1551. if (bench_tag)
  1552. #endif
  1553. { XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ); }
  1554. /* init keys */
  1555. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1556. if ((ret = wc_AesInit(&enc[i], HEAP_HINT,
  1557. doAsync ? devId : INVALID_DEVID)) != 0) {
  1558. printf("AesInit failed, ret = %d\n", ret);
  1559. goto exit;
  1560. }
  1561. ret = wc_AesGcmSetKey(&enc[i], key, keySz);
  1562. if (ret != 0) {
  1563. printf("AesGcmSetKey failed, ret = %d\n", ret);
  1564. goto exit;
  1565. }
  1566. }
  1567. /* GCM uses same routine in backend for both encrypt and decrypt */
  1568. bench_stats_start(&count, &start);
  1569. do {
  1570. for (times = 0; times < numBlocks || pending > 0; ) {
  1571. bench_async_poll(&pending);
  1572. /* while free pending slots in queue, submit ops */
  1573. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1574. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, numBlocks, &pending)) {
  1575. ret = wc_AesGcmEncrypt(&enc[i], bench_cipher,
  1576. bench_plain, BENCH_SIZE,
  1577. iv, ivSz, bench_tag, AES_AUTH_TAG_SZ,
  1578. bench_additional, aesAuthAddSz);
  1579. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, &pending)) {
  1580. goto exit_aes_gcm;
  1581. }
  1582. }
  1583. } /* for i */
  1584. } /* for times */
  1585. count += times;
  1586. } while (bench_stats_sym_check(start));
  1587. exit_aes_gcm:
  1588. bench_stats_sym_finish(encLabel, doAsync, count, bench_size, start, ret);
  1589. #ifdef HAVE_AES_DECRYPT
  1590. /* GCM uses same routine in backend for both encrypt and decrypt */
  1591. bench_stats_start(&count, &start);
  1592. do {
  1593. for (times = 0; times < numBlocks || pending > 0; ) {
  1594. bench_async_poll(&pending);
  1595. /* while free pending slots in queue, submit ops */
  1596. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1597. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, numBlocks, &pending)) {
  1598. ret = wc_AesGcmDecrypt(&enc[i], bench_plain,
  1599. bench_cipher, BENCH_SIZE,
  1600. iv, ivSz, bench_tag, AES_AUTH_TAG_SZ,
  1601. bench_additional, aesAuthAddSz);
  1602. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, &pending)) {
  1603. goto exit_aes_gcm_dec;
  1604. }
  1605. }
  1606. } /* for i */
  1607. } /* for times */
  1608. count += times;
  1609. } while (bench_stats_sym_check(start));
  1610. exit_aes_gcm_dec:
  1611. bench_stats_sym_finish(decLabel, doAsync, count, bench_size, start, ret);
  1612. #endif /* HAVE_AES_DECRYPT */
  1613. (void)decLabel;
  1614. exit:
  1615. if (ret < 0) {
  1616. printf("bench_aesgcm failed: %d\n", ret);
  1617. }
  1618. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1619. wc_AesFree(&enc[i]);
  1620. }
  1621. FREE_VAR(bench_additional, HEAP_HINT);
  1622. FREE_VAR(bench_tag, HEAP_HINT);
  1623. }
  1624. void bench_aesgcm(int doAsync)
  1625. {
  1626. #ifdef WOLFSSL_AES_128
  1627. bench_aesgcm_internal(doAsync, bench_key, 16, bench_iv, 12,
  1628. "AES-128-GCM-enc", "AES-128-GCM-dec");
  1629. #endif
  1630. #ifdef WOLFSSL_AES_192
  1631. bench_aesgcm_internal(doAsync, bench_key, 24, bench_iv, 12,
  1632. "AES-192-GCM-enc", "AES-192-GCM-dec");
  1633. #endif
  1634. #ifdef WOLFSSL_AES_256
  1635. bench_aesgcm_internal(doAsync, bench_key, 32, bench_iv, 12,
  1636. "AES-256-GCM-enc", "AES-256-GCM-dec");
  1637. #endif
  1638. }
  1639. #endif /* HAVE_AESGCM */
  1640. #ifdef WOLFSSL_AES_DIRECT
  1641. static void bench_aesecb_internal(int doAsync, const byte* key, word32 keySz,
  1642. const char* encLabel, const char* decLabel)
  1643. {
  1644. int ret, i, count = 0, times, pending = 0;
  1645. Aes enc[BENCH_MAX_PENDING];
  1646. double start;
  1647. /* clear for done cleanup */
  1648. XMEMSET(enc, 0, sizeof(enc));
  1649. /* init keys */
  1650. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1651. if ((ret = wc_AesInit(&enc[i], HEAP_HINT,
  1652. doAsync ? devId : INVALID_DEVID)) != 0) {
  1653. printf("AesInit failed, ret = %d\n", ret);
  1654. goto exit;
  1655. }
  1656. ret = wc_AesSetKey(&enc[i], key, keySz, bench_iv, AES_ENCRYPTION);
  1657. if (ret != 0) {
  1658. printf("AesSetKey failed, ret = %d\n", ret);
  1659. goto exit;
  1660. }
  1661. }
  1662. bench_stats_start(&count, &start);
  1663. do {
  1664. for (times = 0; times < numBlocks || pending > 0; ) {
  1665. bench_async_poll(&pending);
  1666. /* while free pending slots in queue, submit ops */
  1667. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1668. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, numBlocks, &pending)) {
  1669. wc_AesEncryptDirect(&enc[i], bench_cipher, bench_plain);
  1670. ret = 0;
  1671. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, &pending)) {
  1672. goto exit_aes_enc;
  1673. }
  1674. }
  1675. } /* for i */
  1676. } /* for times */
  1677. count += times;
  1678. } while (bench_stats_sym_check(start));
  1679. exit_aes_enc:
  1680. bench_stats_sym_finish(encLabel, doAsync, count, AES_BLOCK_SIZE,
  1681. start, ret);
  1682. #ifdef HAVE_AES_DECRYPT
  1683. /* init keys */
  1684. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1685. ret = wc_AesSetKey(&enc[i], key, keySz, bench_iv, AES_DECRYPTION);
  1686. if (ret != 0) {
  1687. printf("AesSetKey failed, ret = %d\n", ret);
  1688. goto exit;
  1689. }
  1690. }
  1691. bench_stats_start(&count, &start);
  1692. do {
  1693. for (times = 0; times < numBlocks || pending > 0; ) {
  1694. bench_async_poll(&pending);
  1695. /* while free pending slots in queue, submit ops */
  1696. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1697. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, numBlocks, &pending)) {
  1698. wc_AesDecryptDirect(&enc[i], bench_plain,
  1699. bench_cipher);
  1700. ret = 0;
  1701. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, &pending)) {
  1702. goto exit_aes_dec;
  1703. }
  1704. }
  1705. } /* for i */
  1706. } /* for times */
  1707. count += times;
  1708. } while (bench_stats_sym_check(start));
  1709. exit_aes_dec:
  1710. bench_stats_sym_finish(decLabel, doAsync, count, AES_BLOCK_SIZE,
  1711. start, ret);
  1712. #endif /* HAVE_AES_DECRYPT */
  1713. exit:
  1714. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1715. wc_AesFree(&enc[i]);
  1716. }
  1717. }
  1718. void bench_aesecb(int doAsync)
  1719. {
  1720. #ifdef WOLFSSL_AES_128
  1721. bench_aesecb_internal(doAsync, bench_key, 16,
  1722. "AES-128-ECB-enc", "AES-128-ECB-dec");
  1723. #endif
  1724. #ifdef WOLFSSL_AES_192
  1725. bench_aesecb_internal(doAsync, bench_key, 24,
  1726. "AES-192-ECB-enc", "AES-192-ECB-dec");
  1727. #endif
  1728. #ifdef WOLFSSL_AES_256
  1729. bench_aesecb_internal(doAsync, bench_key, 32,
  1730. "AES-256-ECB-enc", "AES-256-ECB-dec");
  1731. #endif
  1732. }
  1733. #endif /* WOLFSSL_AES_DIRECT */
  1734. #ifdef WOLFSSL_AES_CFB
  1735. static void bench_aescfb_internal(const byte* key, word32 keySz, const byte* iv,
  1736. const char* label)
  1737. {
  1738. Aes enc;
  1739. double start;
  1740. int i, ret, count;
  1741. ret = wc_AesSetKey(&enc, key, keySz, iv, AES_ENCRYPTION);
  1742. if (ret != 0) {
  1743. printf("AesSetKey failed, ret = %d\n", ret);
  1744. return;
  1745. }
  1746. bench_stats_start(&count, &start);
  1747. do {
  1748. for (i = 0; i < numBlocks; i++) {
  1749. if((ret = wc_AesCfbEncrypt(&enc, bench_plain, bench_cipher,
  1750. BENCH_SIZE)) != 0) {
  1751. printf("wc_AesCfbEncrypt failed, ret = %d\n", ret);
  1752. return;
  1753. }
  1754. }
  1755. count += i;
  1756. } while (bench_stats_sym_check(start));
  1757. bench_stats_sym_finish(label, 0, count, bench_size, start, ret);
  1758. }
  1759. void bench_aescfb(void)
  1760. {
  1761. #ifdef WOLFSSL_AES_128
  1762. bench_aescfb_internal(bench_key, 16, bench_iv, "AES-128-CFB");
  1763. #endif
  1764. #ifdef WOLFSSL_AES_192
  1765. bench_aescfb_internal(bench_key, 24, bench_iv, "AES-192-CFB");
  1766. #endif
  1767. #ifdef WOLFSSL_AES_256
  1768. bench_aescfb_internal(bench_key, 32, bench_iv, "AES-256-CFB");
  1769. #endif
  1770. }
  1771. #endif /* WOLFSSL_AES_CFB */
  1772. #ifdef WOLFSSL_AES_XTS
  1773. void bench_aesxts(void)
  1774. {
  1775. XtsAes aes;
  1776. double start;
  1777. int i, count, ret;
  1778. static unsigned char k1[] = {
  1779. 0xa1, 0xb9, 0x0c, 0xba, 0x3f, 0x06, 0xac, 0x35,
  1780. 0x3b, 0x2c, 0x34, 0x38, 0x76, 0x08, 0x17, 0x62,
  1781. 0x09, 0x09, 0x23, 0x02, 0x6e, 0x91, 0x77, 0x18,
  1782. 0x15, 0xf2, 0x9d, 0xab, 0x01, 0x93, 0x2f, 0x2f
  1783. };
  1784. static unsigned char i1[] = {
  1785. 0x4f, 0xae, 0xf7, 0x11, 0x7c, 0xda, 0x59, 0xc6,
  1786. 0x6e, 0x4b, 0x92, 0x01, 0x3e, 0x76, 0x8a, 0xd5
  1787. };
  1788. ret = wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_ENCRYPTION,
  1789. HEAP_HINT, devId);
  1790. if (ret != 0) {
  1791. printf("wc_AesXtsSetKey failed, ret = %d\n", ret);
  1792. return;
  1793. }
  1794. bench_stats_start(&count, &start);
  1795. do {
  1796. for (i = 0; i < numBlocks; i++) {
  1797. if ((ret = wc_AesXtsEncrypt(&aes, bench_plain, bench_cipher,
  1798. BENCH_SIZE, i1, sizeof(i1))) != 0) {
  1799. printf("wc_AesXtsEncrypt failed, ret = %d\n", ret);
  1800. return;
  1801. }
  1802. }
  1803. count += i;
  1804. } while (bench_stats_sym_check(start));
  1805. bench_stats_sym_finish("AES-XTS-enc", 0, count, bench_size, start, ret);
  1806. wc_AesXtsFree(&aes);
  1807. /* decryption benchmark */
  1808. ret = wc_AesXtsSetKey(&aes, k1, sizeof(k1), AES_DECRYPTION,
  1809. HEAP_HINT, devId);
  1810. if (ret != 0) {
  1811. printf("wc_AesXtsSetKey failed, ret = %d\n", ret);
  1812. return;
  1813. }
  1814. bench_stats_start(&count, &start);
  1815. do {
  1816. for (i = 0; i < numBlocks; i++) {
  1817. if ((ret = wc_AesXtsDecrypt(&aes, bench_plain, bench_cipher,
  1818. BENCH_SIZE, i1, sizeof(i1))) != 0) {
  1819. printf("wc_AesXtsDecrypt failed, ret = %d\n", ret);
  1820. return;
  1821. }
  1822. }
  1823. count += i;
  1824. } while (bench_stats_sym_check(start));
  1825. bench_stats_sym_finish("AES-XTS-dec", 0, count, bench_size, start, ret);
  1826. wc_AesXtsFree(&aes);
  1827. }
  1828. #endif /* WOLFSSL_AES_XTS */
  1829. #ifdef WOLFSSL_AES_COUNTER
  1830. static void bench_aesctr_internal(const byte* key, word32 keySz, const byte* iv,
  1831. const char* label)
  1832. {
  1833. Aes enc;
  1834. double start;
  1835. int i, count, ret = 0;
  1836. wc_AesSetKeyDirect(&enc, key, keySz, iv, AES_ENCRYPTION);
  1837. bench_stats_start(&count, &start);
  1838. do {
  1839. for (i = 0; i < numBlocks; i++) {
  1840. if((ret = wc_AesCtrEncrypt(&enc, bench_plain, bench_cipher, BENCH_SIZE)) != 0) {
  1841. printf("wc_AesCtrEncrypt failed, ret = %d\n", ret);
  1842. return;
  1843. }
  1844. }
  1845. count += i;
  1846. } while (bench_stats_sym_check(start));
  1847. bench_stats_sym_finish(label, 0, count, bench_size, start, ret);
  1848. }
  1849. void bench_aesctr(void)
  1850. {
  1851. #ifdef WOLFSSL_AES_128
  1852. bench_aesctr_internal(bench_key, 16, bench_iv, "AES-128-CTR");
  1853. #endif
  1854. #ifdef WOLFSSL_AES_192
  1855. bench_aesctr_internal(bench_key, 24, bench_iv, "AES-192-CTR");
  1856. #endif
  1857. #ifdef WOLFSSL_AES_256
  1858. bench_aesctr_internal(bench_key, 32, bench_iv, "AES-256-CTR");
  1859. #endif
  1860. }
  1861. #endif /* WOLFSSL_AES_COUNTER */
  1862. #ifdef HAVE_AESCCM
  1863. void bench_aesccm(void)
  1864. {
  1865. Aes enc;
  1866. double start;
  1867. int ret, i, count;
  1868. DECLARE_VAR(bench_additional, byte, AES_AUTH_ADD_SZ, HEAP_HINT);
  1869. DECLARE_VAR(bench_tag, byte, AES_AUTH_TAG_SZ, HEAP_HINT);
  1870. XMEMSET(bench_tag, 0, AES_AUTH_TAG_SZ);
  1871. XMEMSET(bench_additional, 0, AES_AUTH_ADD_SZ);
  1872. if ((ret = wc_AesCcmSetKey(&enc, bench_key, 16)) != 0) {
  1873. printf("wc_AesCcmSetKey failed, ret = %d\n", ret);
  1874. return;
  1875. }
  1876. bench_stats_start(&count, &start);
  1877. do {
  1878. for (i = 0; i < numBlocks; i++) {
  1879. wc_AesCcmEncrypt(&enc, bench_cipher, bench_plain, BENCH_SIZE,
  1880. bench_iv, 12, bench_tag, AES_AUTH_TAG_SZ,
  1881. bench_additional, aesAuthAddSz);
  1882. }
  1883. count += i;
  1884. } while (bench_stats_sym_check(start));
  1885. bench_stats_sym_finish("AES-CCM", 0, count, bench_size, start, ret);
  1886. FREE_VAR(bench_additional, HEAP_HINT);
  1887. FREE_VAR(bench_tag, HEAP_HINT);
  1888. }
  1889. #endif /* HAVE_AESCCM */
  1890. #endif /* !NO_AES */
  1891. #ifdef HAVE_POLY1305
  1892. void bench_poly1305()
  1893. {
  1894. Poly1305 enc;
  1895. byte mac[16];
  1896. double start;
  1897. int ret = 0, i, count;
  1898. if (digest_stream) {
  1899. ret = wc_Poly1305SetKey(&enc, bench_key, 32);
  1900. if (ret != 0) {
  1901. printf("Poly1305SetKey failed, ret = %d\n", ret);
  1902. return;
  1903. }
  1904. bench_stats_start(&count, &start);
  1905. do {
  1906. for (i = 0; i < numBlocks; i++) {
  1907. ret = wc_Poly1305Update(&enc, bench_plain, BENCH_SIZE);
  1908. if (ret != 0) {
  1909. printf("Poly1305Update failed: %d\n", ret);
  1910. break;
  1911. }
  1912. }
  1913. wc_Poly1305Final(&enc, mac);
  1914. count += i;
  1915. } while (bench_stats_sym_check(start));
  1916. bench_stats_sym_finish("POLY1305", 0, count, bench_size, start, ret);
  1917. }
  1918. else {
  1919. bench_stats_start(&count, &start);
  1920. do {
  1921. for (i = 0; i < numBlocks; i++) {
  1922. ret = wc_Poly1305SetKey(&enc, bench_key, 32);
  1923. if (ret != 0) {
  1924. printf("Poly1305SetKey failed, ret = %d\n", ret);
  1925. return;
  1926. }
  1927. ret = wc_Poly1305Update(&enc, bench_plain, BENCH_SIZE);
  1928. if (ret != 0) {
  1929. printf("Poly1305Update failed: %d\n", ret);
  1930. break;
  1931. }
  1932. wc_Poly1305Final(&enc, mac);
  1933. }
  1934. count += i;
  1935. } while (bench_stats_sym_check(start));
  1936. bench_stats_sym_finish("POLY1305", 0, count, bench_size, start, ret);
  1937. }
  1938. }
  1939. #endif /* HAVE_POLY1305 */
  1940. #ifdef HAVE_CAMELLIA
  1941. void bench_camellia(void)
  1942. {
  1943. Camellia cam;
  1944. double start;
  1945. int ret, i, count;
  1946. ret = wc_CamelliaSetKey(&cam, bench_key, 16, bench_iv);
  1947. if (ret != 0) {
  1948. printf("CamelliaSetKey failed, ret = %d\n", ret);
  1949. return;
  1950. }
  1951. bench_stats_start(&count, &start);
  1952. do {
  1953. for (i = 0; i < numBlocks; i++) {
  1954. ret = wc_CamelliaCbcEncrypt(&cam, bench_plain, bench_cipher,
  1955. BENCH_SIZE);
  1956. if (ret < 0) {
  1957. printf("CamelliaCbcEncrypt failed: %d\n", ret);
  1958. return;
  1959. }
  1960. }
  1961. count += i;
  1962. } while (bench_stats_sym_check(start));
  1963. bench_stats_sym_finish("Camellia", 0, count, bench_size, start, ret);
  1964. }
  1965. #endif
  1966. #ifndef NO_DES3
  1967. void bench_des(int doAsync)
  1968. {
  1969. int ret = 0, i, count = 0, times, pending = 0;
  1970. Des3 enc[BENCH_MAX_PENDING];
  1971. double start;
  1972. /* clear for done cleanup */
  1973. XMEMSET(enc, 0, sizeof(enc));
  1974. /* init keys */
  1975. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1976. if ((ret = wc_Des3Init(&enc[i], HEAP_HINT,
  1977. doAsync ? devId : INVALID_DEVID)) != 0) {
  1978. printf("Des3Init failed, ret = %d\n", ret);
  1979. goto exit;
  1980. }
  1981. ret = wc_Des3_SetKey(&enc[i], bench_key, bench_iv, DES_ENCRYPTION);
  1982. if (ret != 0) {
  1983. printf("Des3_SetKey failed, ret = %d\n", ret);
  1984. goto exit;
  1985. }
  1986. }
  1987. bench_stats_start(&count, &start);
  1988. do {
  1989. for (times = 0; times < numBlocks || pending > 0; ) {
  1990. bench_async_poll(&pending);
  1991. /* while free pending slots in queue, submit ops */
  1992. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  1993. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, numBlocks, &pending)) {
  1994. ret = wc_Des3_CbcEncrypt(&enc[i], bench_plain, bench_cipher,
  1995. BENCH_SIZE);
  1996. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, &pending)) {
  1997. goto exit_3des;
  1998. }
  1999. }
  2000. } /* for i */
  2001. } /* for times */
  2002. count += times;
  2003. } while (bench_stats_sym_check(start));
  2004. exit_3des:
  2005. bench_stats_sym_finish("3DES", doAsync, count, bench_size, start, ret);
  2006. exit:
  2007. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2008. wc_Des3Free(&enc[i]);
  2009. }
  2010. }
  2011. #endif /* !NO_DES3 */
  2012. #ifdef HAVE_IDEA
  2013. void bench_idea(void)
  2014. {
  2015. Idea enc;
  2016. double start;
  2017. int ret = 0, i, count;
  2018. ret = wc_IdeaSetKey(&enc, bench_key, IDEA_KEY_SIZE, bench_iv,
  2019. IDEA_ENCRYPTION);
  2020. if (ret != 0) {
  2021. printf("Des3_SetKey failed, ret = %d\n", ret);
  2022. return;
  2023. }
  2024. bench_stats_start(&count, &start);
  2025. do {
  2026. for (i = 0; i < numBlocks; i++) {
  2027. wc_IdeaCbcEncrypt(&enc, bench_plain, bench_cipher, BENCH_SIZE);
  2028. }
  2029. count += i;
  2030. } while (bench_stats_sym_check(start));
  2031. bench_stats_sym_finish("IDEA", 0, count, bench_size, start, ret);
  2032. }
  2033. #endif /* HAVE_IDEA */
  2034. #ifndef NO_RC4
  2035. void bench_arc4(int doAsync)
  2036. {
  2037. int ret = 0, i, count = 0, times, pending = 0;
  2038. Arc4 enc[BENCH_MAX_PENDING];
  2039. double start;
  2040. /* clear for done cleanup */
  2041. XMEMSET(enc, 0, sizeof(enc));
  2042. /* init keys */
  2043. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2044. if ((ret = wc_Arc4Init(&enc[i], HEAP_HINT,
  2045. doAsync ? devId : INVALID_DEVID)) != 0) {
  2046. printf("Arc4Init failed, ret = %d\n", ret);
  2047. goto exit;
  2048. }
  2049. ret = wc_Arc4SetKey(&enc[i], bench_key, 16);
  2050. if (ret != 0) {
  2051. printf("Arc4SetKey failed, ret = %d\n", ret);
  2052. goto exit;
  2053. }
  2054. }
  2055. bench_stats_start(&count, &start);
  2056. do {
  2057. for (times = 0; times < numBlocks || pending > 0; ) {
  2058. bench_async_poll(&pending);
  2059. /* while free pending slots in queue, submit ops */
  2060. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2061. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, numBlocks, &pending)) {
  2062. ret = wc_Arc4Process(&enc[i], bench_cipher, bench_plain,
  2063. BENCH_SIZE);
  2064. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&enc[i]), 0, &times, &pending)) {
  2065. goto exit_arc4;
  2066. }
  2067. }
  2068. } /* for i */
  2069. } /* for times */
  2070. count += times;
  2071. } while (bench_stats_sym_check(start));
  2072. exit_arc4:
  2073. bench_stats_sym_finish("ARC4", doAsync, count, bench_size, start, ret);
  2074. exit:
  2075. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2076. wc_Arc4Free(&enc[i]);
  2077. }
  2078. }
  2079. #endif /* !NO_RC4 */
  2080. #ifdef HAVE_HC128
  2081. void bench_hc128(void)
  2082. {
  2083. HC128 enc;
  2084. double start;
  2085. int i, count;
  2086. wc_Hc128_SetKey(&enc, bench_key, bench_iv);
  2087. bench_stats_start(&count, &start);
  2088. do {
  2089. for (i = 0; i < numBlocks; i++) {
  2090. wc_Hc128_Process(&enc, bench_cipher, bench_plain, BENCH_SIZE);
  2091. }
  2092. count += i;
  2093. } while (bench_stats_sym_check(start));
  2094. bench_stats_sym_finish("HC128", 0, count, bench_size, start, 0);
  2095. }
  2096. #endif /* HAVE_HC128 */
  2097. #ifndef NO_RABBIT
  2098. void bench_rabbit(void)
  2099. {
  2100. Rabbit enc;
  2101. double start;
  2102. int i, count;
  2103. wc_RabbitSetKey(&enc, bench_key, bench_iv);
  2104. bench_stats_start(&count, &start);
  2105. do {
  2106. for (i = 0; i < numBlocks; i++) {
  2107. wc_RabbitProcess(&enc, bench_cipher, bench_plain, BENCH_SIZE);
  2108. }
  2109. count += i;
  2110. } while (bench_stats_sym_check(start));
  2111. bench_stats_sym_finish("RABBIT", 0, count, bench_size, start, 0);
  2112. }
  2113. #endif /* NO_RABBIT */
  2114. #ifdef HAVE_CHACHA
  2115. void bench_chacha(void)
  2116. {
  2117. ChaCha enc;
  2118. double start;
  2119. int i, count;
  2120. wc_Chacha_SetKey(&enc, bench_key, 16);
  2121. bench_stats_start(&count, &start);
  2122. do {
  2123. for (i = 0; i < numBlocks; i++) {
  2124. wc_Chacha_SetIV(&enc, bench_iv, 0);
  2125. wc_Chacha_Process(&enc, bench_cipher, bench_plain, BENCH_SIZE);
  2126. }
  2127. count += i;
  2128. } while (bench_stats_sym_check(start));
  2129. bench_stats_sym_finish("CHACHA", 0, count, bench_size, start, 0);
  2130. }
  2131. #endif /* HAVE_CHACHA*/
  2132. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  2133. void bench_chacha20_poly1305_aead(void)
  2134. {
  2135. double start;
  2136. int ret = 0, i, count;
  2137. byte authTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  2138. XMEMSET(authTag, 0, sizeof(authTag));
  2139. bench_stats_start(&count, &start);
  2140. do {
  2141. for (i = 0; i < numBlocks; i++) {
  2142. ret = wc_ChaCha20Poly1305_Encrypt(bench_key, bench_iv, NULL, 0,
  2143. bench_plain, BENCH_SIZE, bench_cipher, authTag);
  2144. if (ret < 0) {
  2145. printf("wc_ChaCha20Poly1305_Encrypt error: %d\n", ret);
  2146. break;
  2147. }
  2148. }
  2149. count += i;
  2150. } while (bench_stats_sym_check(start));
  2151. bench_stats_sym_finish("CHA-POLY", 0, count, bench_size, start, ret);
  2152. }
  2153. #endif /* HAVE_CHACHA && HAVE_POLY1305 */
  2154. #ifndef NO_MD5
  2155. void bench_md5(int doAsync)
  2156. {
  2157. wc_Md5 hash[BENCH_MAX_PENDING];
  2158. double start;
  2159. int ret = 0, i, count = 0, times, pending = 0;
  2160. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_MD5_DIGEST_SIZE, HEAP_HINT);
  2161. /* clear for done cleanup */
  2162. XMEMSET(hash, 0, sizeof(hash));
  2163. if (digest_stream) {
  2164. /* init keys */
  2165. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2166. ret = wc_InitMd5_ex(&hash[i], HEAP_HINT,
  2167. doAsync ? devId : INVALID_DEVID);
  2168. if (ret != 0) {
  2169. printf("InitMd5_ex failed, ret = %d\n", ret);
  2170. goto exit;
  2171. }
  2172. #ifdef WOLFSSL_PIC32MZ_HASH
  2173. wc_Md5SizeSet(&hash[i], numBlocks * BENCH_SIZE);
  2174. #endif
  2175. }
  2176. bench_stats_start(&count, &start);
  2177. do {
  2178. for (times = 0; times < numBlocks || pending > 0; ) {
  2179. bench_async_poll(&pending);
  2180. /* while free pending slots in queue, submit ops */
  2181. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2182. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2183. ret = wc_Md5Update(&hash[i], bench_plain,
  2184. BENCH_SIZE);
  2185. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2186. goto exit_md5;
  2187. }
  2188. }
  2189. } /* for i */
  2190. } /* for times */
  2191. count += times;
  2192. times = 0;
  2193. do {
  2194. bench_async_poll(&pending);
  2195. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2196. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2197. ret = wc_Md5Final(&hash[i], digest[i]);
  2198. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2199. goto exit_md5;
  2200. }
  2201. }
  2202. } /* for i */
  2203. } while (pending > 0);
  2204. } while (bench_stats_sym_check(start));
  2205. }
  2206. else {
  2207. bench_stats_start(&count, &start);
  2208. do {
  2209. for (times = 0; times < numBlocks; times++) {
  2210. ret = wc_InitMd5_ex(hash, HEAP_HINT, INVALID_DEVID);
  2211. ret |= wc_Md5Update(hash, bench_plain, BENCH_SIZE);
  2212. ret |= wc_Md5Final(hash, digest[0]);
  2213. if (ret != 0)
  2214. goto exit_md5;
  2215. } /* for times */
  2216. count += times;
  2217. } while (bench_stats_sym_check(start));
  2218. }
  2219. exit_md5:
  2220. bench_stats_sym_finish("MD5", doAsync, count, bench_size, start, ret);
  2221. exit:
  2222. #ifdef WOLFSSL_ASYNC_CRYPT
  2223. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2224. wc_Md5Free(&hash[i]);
  2225. }
  2226. #endif
  2227. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  2228. }
  2229. #endif /* !NO_MD5 */
  2230. #ifndef NO_SHA
  2231. void bench_sha(int doAsync)
  2232. {
  2233. wc_Sha hash[BENCH_MAX_PENDING];
  2234. double start;
  2235. int ret = 0, i, count = 0, times, pending = 0;
  2236. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA_DIGEST_SIZE, HEAP_HINT);
  2237. /* clear for done cleanup */
  2238. XMEMSET(hash, 0, sizeof(hash));
  2239. if (digest_stream) {
  2240. /* init keys */
  2241. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2242. ret = wc_InitSha_ex(&hash[i], HEAP_HINT,
  2243. doAsync ? devId : INVALID_DEVID);
  2244. if (ret != 0) {
  2245. printf("InitSha failed, ret = %d\n", ret);
  2246. goto exit;
  2247. }
  2248. #ifdef WOLFSSL_PIC32MZ_HASH
  2249. wc_ShaSizeSet(&hash[i], numBlocks * BENCH_SIZE);
  2250. #endif
  2251. }
  2252. bench_stats_start(&count, &start);
  2253. do {
  2254. for (times = 0; times < numBlocks || pending > 0; ) {
  2255. bench_async_poll(&pending);
  2256. /* while free pending slots in queue, submit ops */
  2257. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2258. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2259. ret = wc_ShaUpdate(&hash[i], bench_plain,
  2260. BENCH_SIZE);
  2261. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2262. goto exit_sha;
  2263. }
  2264. }
  2265. } /* for i */
  2266. } /* for times */
  2267. count += times;
  2268. times = 0;
  2269. do {
  2270. bench_async_poll(&pending);
  2271. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2272. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2273. ret = wc_ShaFinal(&hash[i], digest[i]);
  2274. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2275. goto exit_sha;
  2276. }
  2277. }
  2278. } /* for i */
  2279. } while (pending > 0);
  2280. } while (bench_stats_sym_check(start));
  2281. }
  2282. else {
  2283. bench_stats_start(&count, &start);
  2284. do {
  2285. for (times = 0; times < numBlocks; times++) {
  2286. ret = wc_InitSha_ex(hash, HEAP_HINT, INVALID_DEVID);
  2287. ret |= wc_ShaUpdate(hash, bench_plain, BENCH_SIZE);
  2288. ret |= wc_ShaFinal(hash, digest[0]);
  2289. if (ret != 0)
  2290. goto exit_sha;
  2291. } /* for times */
  2292. count += times;
  2293. } while (bench_stats_sym_check(start));
  2294. }
  2295. exit_sha:
  2296. bench_stats_sym_finish("SHA", doAsync, count, bench_size, start, ret);
  2297. exit:
  2298. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2299. wc_ShaFree(&hash[i]);
  2300. }
  2301. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  2302. }
  2303. #endif /* NO_SHA */
  2304. #ifdef WOLFSSL_SHA224
  2305. void bench_sha224(int doAsync)
  2306. {
  2307. wc_Sha224 hash[BENCH_MAX_PENDING];
  2308. double start;
  2309. int ret = 0, i, count = 0, times, pending = 0;
  2310. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA224_DIGEST_SIZE, HEAP_HINT);
  2311. /* clear for done cleanup */
  2312. XMEMSET(hash, 0, sizeof(hash));
  2313. if (digest_stream) {
  2314. /* init keys */
  2315. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2316. ret = wc_InitSha224_ex(&hash[i], HEAP_HINT,
  2317. doAsync ? devId : INVALID_DEVID);
  2318. if (ret != 0) {
  2319. printf("InitSha224_ex failed, ret = %d\n", ret);
  2320. goto exit;
  2321. }
  2322. }
  2323. bench_stats_start(&count, &start);
  2324. do {
  2325. for (times = 0; times < numBlocks || pending > 0; ) {
  2326. bench_async_poll(&pending);
  2327. /* while free pending slots in queue, submit ops */
  2328. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2329. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2330. ret = wc_Sha224Update(&hash[i], bench_plain,
  2331. BENCH_SIZE);
  2332. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2333. goto exit_sha224;
  2334. }
  2335. }
  2336. } /* for i */
  2337. } /* for times */
  2338. count += times;
  2339. times = 0;
  2340. do {
  2341. bench_async_poll(&pending);
  2342. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2343. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2344. ret = wc_Sha224Final(&hash[i], digest[i]);
  2345. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2346. goto exit_sha224;
  2347. }
  2348. }
  2349. } /* for i */
  2350. } while (pending > 0);
  2351. } while (bench_stats_sym_check(start));
  2352. }
  2353. else {
  2354. bench_stats_start(&count, &start);
  2355. do {
  2356. for (times = 0; times < numBlocks; times++) {
  2357. ret = wc_InitSha224_ex(hash, HEAP_HINT, INVALID_DEVID);
  2358. ret |= wc_Sha224Update(hash, bench_plain, BENCH_SIZE);
  2359. ret |= wc_Sha224Final(hash, digest[0]);
  2360. if (ret != 0)
  2361. goto exit_sha224;
  2362. } /* for times */
  2363. count += times;
  2364. } while (bench_stats_sym_check(start));
  2365. }
  2366. exit_sha224:
  2367. bench_stats_sym_finish("SHA-224", doAsync, count, bench_size, start, ret);
  2368. exit:
  2369. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2370. wc_Sha224Free(&hash[i]);
  2371. }
  2372. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  2373. }
  2374. #endif
  2375. #ifndef NO_SHA256
  2376. void bench_sha256(int doAsync)
  2377. {
  2378. wc_Sha256 hash[BENCH_MAX_PENDING];
  2379. double start;
  2380. int ret = 0, i, count = 0, times, pending = 0;
  2381. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA256_DIGEST_SIZE, HEAP_HINT);
  2382. /* clear for done cleanup */
  2383. XMEMSET(hash, 0, sizeof(hash));
  2384. if (digest_stream) {
  2385. /* init keys */
  2386. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2387. ret = wc_InitSha256_ex(&hash[i], HEAP_HINT,
  2388. doAsync ? devId : INVALID_DEVID);
  2389. if (ret != 0) {
  2390. printf("InitSha256_ex failed, ret = %d\n", ret);
  2391. goto exit;
  2392. }
  2393. #ifdef WOLFSSL_PIC32MZ_HASH
  2394. wc_Sha256SizeSet(&hash[i], numBlocks * BENCH_SIZE);
  2395. #endif
  2396. }
  2397. bench_stats_start(&count, &start);
  2398. do {
  2399. for (times = 0; times < numBlocks || pending > 0; ) {
  2400. bench_async_poll(&pending);
  2401. /* while free pending slots in queue, submit ops */
  2402. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2403. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2404. ret = wc_Sha256Update(&hash[i], bench_plain,
  2405. BENCH_SIZE);
  2406. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2407. goto exit_sha256;
  2408. }
  2409. }
  2410. } /* for i */
  2411. } /* for times */
  2412. count += times;
  2413. times = 0;
  2414. do {
  2415. bench_async_poll(&pending);
  2416. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2417. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2418. ret = wc_Sha256Final(&hash[i], digest[i]);
  2419. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2420. goto exit_sha256;
  2421. }
  2422. }
  2423. } /* for i */
  2424. } while (pending > 0);
  2425. } while (bench_stats_sym_check(start));
  2426. }
  2427. else {
  2428. bench_stats_start(&count, &start);
  2429. do {
  2430. for (times = 0; times < numBlocks; times++) {
  2431. ret = wc_InitSha256_ex(hash, HEAP_HINT, INVALID_DEVID);
  2432. ret |= wc_Sha256Update(hash, bench_plain, BENCH_SIZE);
  2433. ret |= wc_Sha256Final(hash, digest[0]);
  2434. if (ret != 0)
  2435. goto exit_sha256;
  2436. } /* for times */
  2437. count += times;
  2438. } while (bench_stats_sym_check(start));
  2439. }
  2440. exit_sha256:
  2441. bench_stats_sym_finish("SHA-256", doAsync, count, bench_size, start, ret);
  2442. exit:
  2443. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2444. wc_Sha256Free(&hash[i]);
  2445. }
  2446. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  2447. }
  2448. #endif
  2449. #ifdef WOLFSSL_SHA384
  2450. void bench_sha384(int doAsync)
  2451. {
  2452. wc_Sha384 hash[BENCH_MAX_PENDING];
  2453. double start;
  2454. int ret = 0, i, count = 0, times, pending = 0;
  2455. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA384_DIGEST_SIZE, HEAP_HINT);
  2456. /* clear for done cleanup */
  2457. XMEMSET(hash, 0, sizeof(hash));
  2458. if (digest_stream) {
  2459. /* init keys */
  2460. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2461. ret = wc_InitSha384_ex(&hash[i], HEAP_HINT,
  2462. doAsync ? devId : INVALID_DEVID);
  2463. if (ret != 0) {
  2464. printf("InitSha384_ex failed, ret = %d\n", ret);
  2465. goto exit;
  2466. }
  2467. }
  2468. bench_stats_start(&count, &start);
  2469. do {
  2470. for (times = 0; times < numBlocks || pending > 0; ) {
  2471. bench_async_poll(&pending);
  2472. /* while free pending slots in queue, submit ops */
  2473. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2474. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2475. ret = wc_Sha384Update(&hash[i], bench_plain,
  2476. BENCH_SIZE);
  2477. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2478. goto exit_sha384;
  2479. }
  2480. }
  2481. } /* for i */
  2482. } /* for times */
  2483. count += times;
  2484. times = 0;
  2485. do {
  2486. bench_async_poll(&pending);
  2487. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2488. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2489. ret = wc_Sha384Final(&hash[i], digest[i]);
  2490. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2491. goto exit_sha384;
  2492. }
  2493. }
  2494. } /* for i */
  2495. } while (pending > 0);
  2496. } while (bench_stats_sym_check(start));
  2497. }
  2498. else {
  2499. bench_stats_start(&count, &start);
  2500. do {
  2501. for (times = 0; times < numBlocks; times++) {
  2502. ret = wc_InitSha384_ex(hash, HEAP_HINT, INVALID_DEVID);
  2503. ret |= wc_Sha384Update(hash, bench_plain, BENCH_SIZE);
  2504. ret |= wc_Sha384Final(hash, digest[0]);
  2505. if (ret != 0)
  2506. goto exit_sha384;
  2507. } /* for times */
  2508. count += times;
  2509. } while (bench_stats_sym_check(start));
  2510. }
  2511. exit_sha384:
  2512. bench_stats_sym_finish("SHA-384", doAsync, count, bench_size, start, ret);
  2513. exit:
  2514. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2515. wc_Sha384Free(&hash[i]);
  2516. }
  2517. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  2518. }
  2519. #endif
  2520. #ifdef WOLFSSL_SHA512
  2521. void bench_sha512(int doAsync)
  2522. {
  2523. wc_Sha512 hash[BENCH_MAX_PENDING];
  2524. double start;
  2525. int ret = 0, i, count = 0, times, pending = 0;
  2526. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA512_DIGEST_SIZE, HEAP_HINT);
  2527. /* clear for done cleanup */
  2528. XMEMSET(hash, 0, sizeof(hash));
  2529. if (digest_stream) {
  2530. /* init keys */
  2531. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2532. ret = wc_InitSha512_ex(&hash[i], HEAP_HINT,
  2533. doAsync ? devId : INVALID_DEVID);
  2534. if (ret != 0) {
  2535. printf("InitSha512_ex failed, ret = %d\n", ret);
  2536. goto exit;
  2537. }
  2538. }
  2539. bench_stats_start(&count, &start);
  2540. do {
  2541. for (times = 0; times < numBlocks || pending > 0; ) {
  2542. bench_async_poll(&pending);
  2543. /* while free pending slots in queue, submit ops */
  2544. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2545. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2546. ret = wc_Sha512Update(&hash[i], bench_plain,
  2547. BENCH_SIZE);
  2548. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2549. goto exit_sha512;
  2550. }
  2551. }
  2552. } /* for i */
  2553. } /* for times */
  2554. count += times;
  2555. times = 0;
  2556. do {
  2557. bench_async_poll(&pending);
  2558. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2559. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2560. ret = wc_Sha512Final(&hash[i], digest[i]);
  2561. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2562. goto exit_sha512;
  2563. }
  2564. }
  2565. } /* for i */
  2566. } while (pending > 0);
  2567. } while (bench_stats_sym_check(start));
  2568. }
  2569. else {
  2570. bench_stats_start(&count, &start);
  2571. do {
  2572. for (times = 0; times < numBlocks; times++) {
  2573. ret = wc_InitSha512_ex(hash, HEAP_HINT, INVALID_DEVID);
  2574. ret |= wc_Sha512Update(hash, bench_plain, BENCH_SIZE);
  2575. ret |= wc_Sha512Final(hash, digest[0]);
  2576. if (ret != 0)
  2577. goto exit_sha512;
  2578. } /* for times */
  2579. count += times;
  2580. } while (bench_stats_sym_check(start));
  2581. }
  2582. exit_sha512:
  2583. bench_stats_sym_finish("SHA-512", doAsync, count, bench_size, start, ret);
  2584. exit:
  2585. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2586. wc_Sha512Free(&hash[i]);
  2587. }
  2588. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  2589. }
  2590. #endif
  2591. #ifdef WOLFSSL_SHA3
  2592. #ifndef WOLFSSL_NOSHA3_224
  2593. void bench_sha3_224(int doAsync)
  2594. {
  2595. wc_Sha3 hash[BENCH_MAX_PENDING];
  2596. double start;
  2597. int ret = 0, i, count = 0, times, pending = 0;
  2598. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA3_224_DIGEST_SIZE, HEAP_HINT);
  2599. /* clear for done cleanup */
  2600. XMEMSET(hash, 0, sizeof(hash));
  2601. if (digest_stream) {
  2602. /* init keys */
  2603. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2604. ret = wc_InitSha3_224(&hash[i], HEAP_HINT,
  2605. doAsync ? devId : INVALID_DEVID);
  2606. if (ret != 0) {
  2607. printf("InitSha3_224 failed, ret = %d\n", ret);
  2608. goto exit;
  2609. }
  2610. }
  2611. bench_stats_start(&count, &start);
  2612. do {
  2613. for (times = 0; times < numBlocks || pending > 0; ) {
  2614. bench_async_poll(&pending);
  2615. /* while free pending slots in queue, submit ops */
  2616. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2617. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2618. ret = wc_Sha3_224_Update(&hash[i], bench_plain,
  2619. BENCH_SIZE);
  2620. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2621. goto exit_sha3_224;
  2622. }
  2623. }
  2624. } /* for i */
  2625. } /* for times */
  2626. count += times;
  2627. times = 0;
  2628. do {
  2629. bench_async_poll(&pending);
  2630. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2631. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2632. ret = wc_Sha3_224_Final(&hash[i], digest[i]);
  2633. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2634. goto exit_sha3_224;
  2635. }
  2636. }
  2637. } /* for i */
  2638. } while (pending > 0);
  2639. } while (bench_stats_sym_check(start));
  2640. }
  2641. else {
  2642. bench_stats_start(&count, &start);
  2643. do {
  2644. for (times = 0; times < numBlocks; times++) {
  2645. ret = wc_InitSha3_224(hash, HEAP_HINT, INVALID_DEVID);
  2646. ret |= wc_Sha3_224_Update(hash, bench_plain, BENCH_SIZE);
  2647. ret |= wc_Sha3_224_Final(hash, digest[0]);
  2648. if (ret != 0)
  2649. goto exit_sha3_224;
  2650. } /* for times */
  2651. count += times;
  2652. } while (bench_stats_sym_check(start));
  2653. }
  2654. exit_sha3_224:
  2655. bench_stats_sym_finish("SHA3-224", doAsync, count, bench_size, start, ret);
  2656. exit:
  2657. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2658. wc_Sha3_224_Free(&hash[i]);
  2659. }
  2660. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  2661. }
  2662. #endif /* WOLFSSL_NOSHA3_224 */
  2663. #ifndef WOLFSSL_NOSHA3_256
  2664. void bench_sha3_256(int doAsync)
  2665. {
  2666. wc_Sha3 hash[BENCH_MAX_PENDING];
  2667. double start;
  2668. int ret = 0, i, count = 0, times, pending = 0;
  2669. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA3_256_DIGEST_SIZE, HEAP_HINT);
  2670. /* clear for done cleanup */
  2671. XMEMSET(hash, 0, sizeof(hash));
  2672. if (digest_stream) {
  2673. /* init keys */
  2674. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2675. ret = wc_InitSha3_256(&hash[i], HEAP_HINT,
  2676. doAsync ? devId : INVALID_DEVID);
  2677. if (ret != 0) {
  2678. printf("InitSha3_256 failed, ret = %d\n", ret);
  2679. goto exit;
  2680. }
  2681. }
  2682. bench_stats_start(&count, &start);
  2683. do {
  2684. for (times = 0; times < numBlocks || pending > 0; ) {
  2685. bench_async_poll(&pending);
  2686. /* while free pending slots in queue, submit ops */
  2687. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2688. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2689. ret = wc_Sha3_256_Update(&hash[i], bench_plain,
  2690. BENCH_SIZE);
  2691. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2692. goto exit_sha3_256;
  2693. }
  2694. }
  2695. } /* for i */
  2696. } /* for times */
  2697. count += times;
  2698. times = 0;
  2699. do {
  2700. bench_async_poll(&pending);
  2701. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2702. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2703. ret = wc_Sha3_256_Final(&hash[i], digest[i]);
  2704. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2705. goto exit_sha3_256;
  2706. }
  2707. }
  2708. } /* for i */
  2709. } while (pending > 0);
  2710. } while (bench_stats_sym_check(start));
  2711. }
  2712. else {
  2713. bench_stats_start(&count, &start);
  2714. do {
  2715. for (times = 0; times < numBlocks; times++) {
  2716. ret = wc_InitSha3_256(hash, HEAP_HINT, INVALID_DEVID);
  2717. ret |= wc_Sha3_256_Update(hash, bench_plain, BENCH_SIZE);
  2718. ret |= wc_Sha3_256_Final(hash, digest[0]);
  2719. if (ret != 0)
  2720. goto exit_sha3_256;
  2721. } /* for times */
  2722. count += times;
  2723. } while (bench_stats_sym_check(start));
  2724. }
  2725. exit_sha3_256:
  2726. bench_stats_sym_finish("SHA3-256", doAsync, count, bench_size, start, ret);
  2727. exit:
  2728. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2729. wc_Sha3_256_Free(&hash[i]);
  2730. }
  2731. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  2732. }
  2733. #endif /* WOLFSSL_NOSHA3_256 */
  2734. #ifndef WOLFSSL_NOSHA3_384
  2735. void bench_sha3_384(int doAsync)
  2736. {
  2737. wc_Sha3 hash[BENCH_MAX_PENDING];
  2738. double start;
  2739. int ret = 0, i, count = 0, times, pending = 0;
  2740. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA3_384_DIGEST_SIZE, HEAP_HINT);
  2741. /* clear for done cleanup */
  2742. XMEMSET(hash, 0, sizeof(hash));
  2743. if (digest_stream) {
  2744. /* init keys */
  2745. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2746. ret = wc_InitSha3_384(&hash[i], HEAP_HINT,
  2747. doAsync ? devId : INVALID_DEVID);
  2748. if (ret != 0) {
  2749. printf("InitSha3_384 failed, ret = %d\n", ret);
  2750. goto exit;
  2751. }
  2752. }
  2753. bench_stats_start(&count, &start);
  2754. do {
  2755. for (times = 0; times < numBlocks || pending > 0; ) {
  2756. bench_async_poll(&pending);
  2757. /* while free pending slots in queue, submit ops */
  2758. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2759. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2760. ret = wc_Sha3_384_Update(&hash[i], bench_plain,
  2761. BENCH_SIZE);
  2762. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2763. goto exit_sha3_384;
  2764. }
  2765. }
  2766. } /* for i */
  2767. } /* for times */
  2768. count += times;
  2769. times = 0;
  2770. do {
  2771. bench_async_poll(&pending);
  2772. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2773. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2774. ret = wc_Sha3_384_Final(&hash[i], digest[i]);
  2775. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2776. goto exit_sha3_384;
  2777. }
  2778. }
  2779. } /* for i */
  2780. } while (pending > 0);
  2781. } while (bench_stats_sym_check(start));
  2782. }
  2783. else {
  2784. bench_stats_start(&count, &start);
  2785. do {
  2786. for (times = 0; times < numBlocks; times++) {
  2787. ret = wc_InitSha3_384(hash, HEAP_HINT, INVALID_DEVID);
  2788. ret |= wc_Sha3_384_Update(hash, bench_plain, BENCH_SIZE);
  2789. ret |= wc_Sha3_384_Final(hash, digest[0]);
  2790. if (ret != 0)
  2791. goto exit_sha3_384;
  2792. } /* for times */
  2793. count += times;
  2794. } while (bench_stats_sym_check(start));
  2795. }
  2796. exit_sha3_384:
  2797. bench_stats_sym_finish("SHA3-384", doAsync, count, bench_size, start, ret);
  2798. exit:
  2799. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2800. wc_Sha3_384_Free(&hash[i]);
  2801. }
  2802. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  2803. }
  2804. #endif /* WOLFSSL_NOSHA3_384 */
  2805. #ifndef WOLFSSL_NOSHA3_512
  2806. void bench_sha3_512(int doAsync)
  2807. {
  2808. wc_Sha3 hash[BENCH_MAX_PENDING];
  2809. double start;
  2810. int ret = 0, i, count = 0, times, pending = 0;
  2811. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_SHA3_512_DIGEST_SIZE, HEAP_HINT);
  2812. /* clear for done cleanup */
  2813. XMEMSET(hash, 0, sizeof(hash));
  2814. if (digest_stream) {
  2815. /* init keys */
  2816. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2817. ret = wc_InitSha3_512(&hash[i], HEAP_HINT,
  2818. doAsync ? devId : INVALID_DEVID);
  2819. if (ret != 0) {
  2820. printf("InitSha3_512 failed, ret = %d\n", ret);
  2821. goto exit;
  2822. }
  2823. }
  2824. bench_stats_start(&count, &start);
  2825. do {
  2826. for (times = 0; times < numBlocks || pending > 0; ) {
  2827. bench_async_poll(&pending);
  2828. /* while free pending slots in queue, submit ops */
  2829. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2830. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2831. ret = wc_Sha3_512_Update(&hash[i], bench_plain,
  2832. BENCH_SIZE);
  2833. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2834. goto exit_sha3_512;
  2835. }
  2836. }
  2837. } /* for i */
  2838. } /* for times */
  2839. count += times;
  2840. times = 0;
  2841. do {
  2842. bench_async_poll(&pending);
  2843. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2844. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, numBlocks, &pending)) {
  2845. ret = wc_Sha3_512_Final(&hash[i], digest[i]);
  2846. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hash[i]), 0, &times, &pending)) {
  2847. goto exit_sha3_512;
  2848. }
  2849. }
  2850. } /* for i */
  2851. } while (pending > 0);
  2852. } while (bench_stats_sym_check(start));
  2853. }
  2854. else {
  2855. bench_stats_start(&count, &start);
  2856. do {
  2857. for (times = 0; times < numBlocks; times++) {
  2858. ret = wc_InitSha3_512(hash, HEAP_HINT, INVALID_DEVID);
  2859. ret |= wc_Sha3_512_Update(hash, bench_plain, BENCH_SIZE);
  2860. ret |= wc_Sha3_512_Final(hash, digest[0]);
  2861. if (ret != 0)
  2862. goto exit_sha3_512;
  2863. } /* for times */
  2864. count += times;
  2865. } while (bench_stats_sym_check(start));
  2866. }
  2867. exit_sha3_512:
  2868. bench_stats_sym_finish("SHA3-512", doAsync, count, bench_size, start, ret);
  2869. exit:
  2870. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  2871. wc_Sha3_512_Free(&hash[i]);
  2872. }
  2873. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  2874. }
  2875. #endif /* WOLFSSL_NOSHA3_512 */
  2876. #endif
  2877. #ifdef WOLFSSL_RIPEMD
  2878. int bench_ripemd(void)
  2879. {
  2880. RipeMd hash;
  2881. byte digest[RIPEMD_DIGEST_SIZE];
  2882. double start;
  2883. int i, count, ret = 0;
  2884. if (digest_stream) {
  2885. ret = wc_InitRipeMd(&hash);
  2886. if (ret != 0) {
  2887. return ret;
  2888. }
  2889. bench_stats_start(&count, &start);
  2890. do {
  2891. for (i = 0; i < numBlocks; i++) {
  2892. ret = wc_RipeMdUpdate(&hash, bench_plain, BENCH_SIZE);
  2893. if (ret != 0) {
  2894. return ret;
  2895. }
  2896. }
  2897. ret = wc_RipeMdFinal(&hash, digest);
  2898. if (ret != 0) {
  2899. return ret;
  2900. }
  2901. count += i;
  2902. } while (bench_stats_sym_check(start));
  2903. }
  2904. else {
  2905. bench_stats_start(&count, &start);
  2906. do {
  2907. for (i = 0; i < numBlocks; i++) {
  2908. ret = wc_InitRipeMd(&hash);
  2909. if (ret != 0) {
  2910. return ret;
  2911. }
  2912. ret = wc_RipeMdUpdate(&hash, bench_plain, BENCH_SIZE);
  2913. if (ret != 0) {
  2914. return ret;
  2915. }
  2916. ret = wc_RipeMdFinal(&hash, digest);
  2917. if (ret != 0) {
  2918. return ret;
  2919. }
  2920. }
  2921. count += i;
  2922. } while (bench_stats_sym_check(start));
  2923. }
  2924. bench_stats_sym_finish("RIPEMD", 0, count, bench_size, start, ret);
  2925. return 0;
  2926. }
  2927. #endif
  2928. #ifdef HAVE_BLAKE2
  2929. void bench_blake2(void)
  2930. {
  2931. Blake2b b2b;
  2932. byte digest[64];
  2933. double start;
  2934. int ret, i, count;
  2935. if (digest_stream) {
  2936. ret = wc_InitBlake2b(&b2b, 64);
  2937. if (ret != 0) {
  2938. printf("InitBlake2b failed, ret = %d\n", ret);
  2939. return;
  2940. }
  2941. bench_stats_start(&count, &start);
  2942. do {
  2943. for (i = 0; i < numBlocks; i++) {
  2944. ret = wc_Blake2bUpdate(&b2b, bench_plain, BENCH_SIZE);
  2945. if (ret != 0) {
  2946. printf("Blake2bUpdate failed, ret = %d\n", ret);
  2947. return;
  2948. }
  2949. }
  2950. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2951. if (ret != 0) {
  2952. printf("Blake2bFinal failed, ret = %d\n", ret);
  2953. return;
  2954. }
  2955. count += i;
  2956. } while (bench_stats_sym_check(start));
  2957. }
  2958. else {
  2959. bench_stats_start(&count, &start);
  2960. do {
  2961. for (i = 0; i < numBlocks; i++) {
  2962. ret = wc_InitBlake2b(&b2b, 64);
  2963. if (ret != 0) {
  2964. printf("InitBlake2b failed, ret = %d\n", ret);
  2965. return;
  2966. }
  2967. ret = wc_Blake2bUpdate(&b2b, bench_plain, BENCH_SIZE);
  2968. if (ret != 0) {
  2969. printf("Blake2bUpdate failed, ret = %d\n", ret);
  2970. return;
  2971. }
  2972. ret = wc_Blake2bFinal(&b2b, digest, 64);
  2973. if (ret != 0) {
  2974. printf("Blake2bFinal failed, ret = %d\n", ret);
  2975. return;
  2976. }
  2977. }
  2978. count += i;
  2979. } while (bench_stats_sym_check(start));
  2980. }
  2981. bench_stats_sym_finish("BLAKE2b", 0, count, bench_size, start, ret);
  2982. }
  2983. #endif
  2984. #ifdef WOLFSSL_CMAC
  2985. void bench_cmac(void)
  2986. {
  2987. Cmac cmac;
  2988. byte digest[AES_BLOCK_SIZE];
  2989. word32 digestSz = sizeof(digest);
  2990. double start;
  2991. int ret, i, count;
  2992. bench_stats_start(&count, &start);
  2993. do {
  2994. ret = wc_InitCmac(&cmac, bench_key, 16, WC_CMAC_AES, NULL);
  2995. if (ret != 0) {
  2996. printf("InitCmac failed, ret = %d\n", ret);
  2997. return;
  2998. }
  2999. for (i = 0; i < numBlocks; i++) {
  3000. ret = wc_CmacUpdate(&cmac, bench_plain, BENCH_SIZE);
  3001. if (ret != 0) {
  3002. printf("CmacUpdate failed, ret = %d\n", ret);
  3003. return;
  3004. }
  3005. }
  3006. /* Note: final force zero's the Cmac struct */
  3007. ret = wc_CmacFinal(&cmac, digest, &digestSz);
  3008. if (ret != 0) {
  3009. printf("CmacFinal failed, ret = %d\n", ret);
  3010. return;
  3011. }
  3012. count += i;
  3013. } while (bench_stats_sym_check(start));
  3014. bench_stats_sym_finish("AES-CMAC", 0, count, bench_size, start, ret);
  3015. }
  3016. #endif /* WOLFSSL_CMAC */
  3017. #ifdef HAVE_SCRYPT
  3018. void bench_scrypt(void)
  3019. {
  3020. byte derived[64];
  3021. double start;
  3022. int ret, i, count;
  3023. bench_stats_start(&count, &start);
  3024. do {
  3025. for (i = 0; i < scryptCnt; i++) {
  3026. ret = wc_scrypt(derived, (byte*)"pleaseletmein", 13,
  3027. (byte*)"SodiumChloride", 14, 14, 8, 1, sizeof(derived));
  3028. if (ret != 0) {
  3029. printf("scrypt failed, ret = %d\n", ret);
  3030. goto exit;
  3031. }
  3032. }
  3033. count += i;
  3034. } while (bench_stats_sym_check(start));
  3035. exit:
  3036. bench_stats_asym_finish("scrypt", 17, "", 0, count, start, ret);
  3037. }
  3038. #endif /* HAVE_SCRYPT */
  3039. #ifndef NO_HMAC
  3040. static void bench_hmac(int doAsync, int type, int digestSz,
  3041. byte* key, word32 keySz, const char* label)
  3042. {
  3043. Hmac hmac[BENCH_MAX_PENDING];
  3044. double start;
  3045. int ret = 0, i, count = 0, times, pending = 0;
  3046. #if defined(BENCH_EMBEDDED)
  3047. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, WC_MAX_DIGEST_SIZE, HEAP_HINT);
  3048. (void)digestSz;
  3049. #else
  3050. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, digestSz, HEAP_HINT);
  3051. #endif
  3052. /* clear for done cleanup */
  3053. XMEMSET(hmac, 0, sizeof(hmac));
  3054. /* init keys */
  3055. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3056. ret = wc_HmacInit(&hmac[i], HEAP_HINT,
  3057. doAsync ? devId : INVALID_DEVID);
  3058. if (ret != 0) {
  3059. printf("wc_HmacInit failed for %s, ret = %d\n", label, ret);
  3060. goto exit;
  3061. }
  3062. ret = wc_HmacSetKey(&hmac[i], type, key, keySz);
  3063. if (ret != 0) {
  3064. printf("wc_HmacSetKey failed for %s, ret = %d\n", label, ret);
  3065. goto exit;
  3066. }
  3067. }
  3068. bench_stats_start(&count, &start);
  3069. do {
  3070. for (times = 0; times < numBlocks || pending > 0; ) {
  3071. bench_async_poll(&pending);
  3072. /* while free pending slots in queue, submit ops */
  3073. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3074. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hmac[i]), 0,
  3075. &times, numBlocks, &pending)) {
  3076. ret = wc_HmacUpdate(&hmac[i], bench_plain, BENCH_SIZE);
  3077. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hmac[i]),
  3078. 0, &times, &pending)) {
  3079. goto exit_hmac;
  3080. }
  3081. }
  3082. } /* for i */
  3083. } /* for times */
  3084. count += times;
  3085. times = 0;
  3086. do {
  3087. bench_async_poll(&pending);
  3088. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3089. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&hmac[i]), 0,
  3090. &times, numBlocks, &pending)) {
  3091. ret = wc_HmacFinal(&hmac[i], digest[i]);
  3092. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&hmac[i]),
  3093. 0, &times, &pending)) {
  3094. goto exit_hmac;
  3095. }
  3096. }
  3097. } /* for i */
  3098. } while (pending > 0);
  3099. } while (bench_stats_sym_check(start));
  3100. exit_hmac:
  3101. bench_stats_sym_finish(label, doAsync, count, bench_size, start, ret);
  3102. exit:
  3103. #ifdef WOLFSSL_ASYNC_CRYPT
  3104. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3105. wc_HmacFree(&hmac[i]);
  3106. }
  3107. #endif
  3108. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  3109. }
  3110. #ifndef NO_MD5
  3111. void bench_hmac_md5(int doAsync)
  3112. {
  3113. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3114. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  3115. bench_hmac(doAsync, WC_MD5, WC_MD5_DIGEST_SIZE, key, sizeof(key),
  3116. "HMAC-MD5");
  3117. }
  3118. #endif /* NO_MD5 */
  3119. #ifndef NO_SHA
  3120. void bench_hmac_sha(int doAsync)
  3121. {
  3122. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3123. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3124. 0x0b, 0x0b, 0x0b, 0x0b };
  3125. bench_hmac(doAsync, WC_SHA, WC_SHA_DIGEST_SIZE, key, sizeof(key),
  3126. "HMAC-SHA");
  3127. }
  3128. #endif /* NO_SHA */
  3129. #ifdef WOLFSSL_SHA224
  3130. void bench_hmac_sha224(int doAsync)
  3131. {
  3132. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3133. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3134. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3135. 0x0b, 0x0b, 0x0b, 0x0b };
  3136. bench_hmac(doAsync, WC_SHA224, WC_SHA224_DIGEST_SIZE, key, sizeof(key),
  3137. "HMAC-SHA224");
  3138. }
  3139. #endif /* WOLFSSL_SHA224 */
  3140. #ifndef NO_SHA256
  3141. void bench_hmac_sha256(int doAsync)
  3142. {
  3143. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3144. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3145. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3146. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  3147. bench_hmac(doAsync, WC_SHA256, WC_SHA256_DIGEST_SIZE, key, sizeof(key),
  3148. "HMAC-SHA256");
  3149. }
  3150. #endif /* NO_SHA256 */
  3151. #ifdef WOLFSSL_SHA384
  3152. void bench_hmac_sha384(int doAsync)
  3153. {
  3154. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3155. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3156. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3157. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3158. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3159. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  3160. bench_hmac(doAsync, WC_SHA384, WC_SHA384_DIGEST_SIZE, key, sizeof(key),
  3161. "HMAC-SHA384");
  3162. }
  3163. #endif /* WOLFSSL_SHA384 */
  3164. #ifdef WOLFSSL_SHA512
  3165. void bench_hmac_sha512(int doAsync)
  3166. {
  3167. byte key[] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3168. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3169. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3170. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3171. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3172. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3173. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  3174. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
  3175. bench_hmac(doAsync, WC_SHA512, WC_SHA512_DIGEST_SIZE, key, sizeof(key),
  3176. "HMAC-SHA512");
  3177. }
  3178. #endif /* WOLFSSL_SHA512 */
  3179. #endif /* NO_HMAC */
  3180. #ifndef NO_RSA
  3181. #if defined(WOLFSSL_KEY_GEN)
  3182. void bench_rsaKeyGen(int doAsync)
  3183. {
  3184. RsaKey genKey[BENCH_MAX_PENDING];
  3185. double start;
  3186. int ret = 0, i, count = 0, times, pending = 0;
  3187. int k, keySz;
  3188. const int keySizes[2] = {1024, 2048};
  3189. const long rsa_e_val = WC_RSA_EXPONENT;
  3190. /* clear for done cleanup */
  3191. XMEMSET(genKey, 0, sizeof(genKey));
  3192. for (k = 0; k < (int)(sizeof(keySizes)/sizeof(int)); k++) {
  3193. keySz = keySizes[k];
  3194. bench_stats_start(&count, &start);
  3195. do {
  3196. /* while free pending slots in queue, submit ops */
  3197. for (times = 0; times < genTimes || pending > 0; ) {
  3198. bench_async_poll(&pending);
  3199. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3200. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, &times, genTimes, &pending)) {
  3201. wc_FreeRsaKey(&genKey[i]);
  3202. ret = wc_InitRsaKey_ex(&genKey[i], HEAP_HINT,
  3203. doAsync ? devId : INVALID_DEVID);
  3204. if (ret < 0) {
  3205. goto exit;
  3206. }
  3207. ret = wc_MakeRsaKey(&genKey[i], keySz, rsa_e_val, &rng);
  3208. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, &times, &pending)) {
  3209. goto exit;
  3210. }
  3211. }
  3212. } /* for i */
  3213. } /* for times */
  3214. count += times;
  3215. } while (bench_stats_sym_check(start));
  3216. exit:
  3217. bench_stats_asym_finish("RSA", keySz, "key gen", doAsync, count, start, ret);
  3218. if (ret < 0) {
  3219. break;
  3220. }
  3221. }
  3222. /* cleanup */
  3223. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3224. wc_FreeRsaKey(&genKey[i]);
  3225. }
  3226. }
  3227. #endif /* WOLFSSL_KEY_GEN */
  3228. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  3229. #if defined(WOLFSSL_MDK_SHELL)
  3230. static char *certRSAname = "certs/rsa2048.der";
  3231. /* set by shell command */
  3232. static void set_Bench_RSA_File(char * cert) { certRSAname = cert ; }
  3233. #elif defined(FREESCALE_MQX)
  3234. static char *certRSAname = "a:\\certs\\rsa2048.der";
  3235. #else
  3236. static const char *certRSAname = "certs/rsa2048.der";
  3237. #endif
  3238. #endif
  3239. #define RSA_BUF_SIZE 256 /* for up to 2048 bit */
  3240. void bench_rsa(int doAsync)
  3241. {
  3242. int ret = 0, i, times, count = 0, pending = 0;
  3243. size_t bytes;
  3244. word32 idx = 0;
  3245. const byte* tmp;
  3246. const char* messageStr = "Everyone gets Friday off.";
  3247. const int len = (int)XSTRLEN((char*)messageStr);
  3248. double start = 0.0f;
  3249. RsaKey rsaKey[BENCH_MAX_PENDING];
  3250. int rsaKeySz = RSA_BUF_SIZE * 8; /* used in printf */
  3251. DECLARE_VAR_INIT(message, byte, len, messageStr, HEAP_HINT);
  3252. DECLARE_ARRAY(enc, byte, BENCH_MAX_PENDING, RSA_BUF_SIZE, HEAP_HINT);
  3253. DECLARE_ARRAY(out, byte, BENCH_MAX_PENDING, RSA_BUF_SIZE, HEAP_HINT);
  3254. #ifdef USE_CERT_BUFFERS_1024
  3255. tmp = rsa_key_der_1024;
  3256. bytes = (size_t)sizeof_rsa_key_der_1024;
  3257. rsaKeySz = 1024;
  3258. #elif defined(USE_CERT_BUFFERS_2048)
  3259. tmp = rsa_key_der_2048;
  3260. bytes = (size_t)sizeof_rsa_key_der_2048;
  3261. #else
  3262. #error "need a cert buffer size"
  3263. #endif /* USE_CERT_BUFFERS */
  3264. /* clear for done cleanup */
  3265. XMEMSET(rsaKey, 0, sizeof(rsaKey));
  3266. /* init keys */
  3267. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3268. /* setup an async context for each key */
  3269. if ((ret = wc_InitRsaKey_ex(&rsaKey[i], HEAP_HINT,
  3270. doAsync ? devId : INVALID_DEVID)) < 0) {
  3271. goto exit;
  3272. }
  3273. #ifdef WC_RSA_BLINDING
  3274. ret = wc_RsaSetRNG(&rsaKey[i], &rng);
  3275. if (ret != 0)
  3276. goto exit;
  3277. #endif
  3278. /* decode the private key */
  3279. idx = 0;
  3280. if ((ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsaKey[i],
  3281. (word32)bytes)) != 0) {
  3282. printf("wc_RsaPrivateKeyDecode failed! %d\n", ret);
  3283. goto exit;
  3284. }
  3285. }
  3286. if (!rsa_sign_verify) {
  3287. /* begin public RSA */
  3288. bench_stats_start(&count, &start);
  3289. do {
  3290. for (times = 0; times < ntimes || pending > 0; ) {
  3291. bench_async_poll(&pending);
  3292. /* while free pending slots in queue, submit ops */
  3293. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3294. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  3295. 1, &times, ntimes, &pending)) {
  3296. ret = wc_RsaPublicEncrypt(message, (word32)len, enc[i],
  3297. RSA_BUF_SIZE, &rsaKey[i],
  3298. &rng);
  3299. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(
  3300. &rsaKey[i]), 1, &times, &pending)) {
  3301. goto exit_rsa_pub;
  3302. }
  3303. }
  3304. } /* for i */
  3305. } /* for times */
  3306. count += times;
  3307. } while (bench_stats_sym_check(start));
  3308. exit_rsa_pub:
  3309. bench_stats_asym_finish("RSA", rsaKeySz, "public", doAsync, count,
  3310. start, ret);
  3311. if (ret < 0) {
  3312. goto exit;
  3313. }
  3314. /* capture resulting encrypt length */
  3315. idx = (word32)(rsaKeySz/8);
  3316. /* begin private async RSA */
  3317. bench_stats_start(&count, &start);
  3318. do {
  3319. for (times = 0; times < ntimes || pending > 0; ) {
  3320. bench_async_poll(&pending);
  3321. /* while free pending slots in queue, submit ops */
  3322. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3323. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  3324. 1, &times, ntimes, &pending)) {
  3325. ret = wc_RsaPrivateDecrypt(enc[i], idx, out[i],
  3326. RSA_BUF_SIZE, &rsaKey[i]);
  3327. if (!bench_async_handle(&ret,
  3328. BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  3329. 1, &times, &pending)) {
  3330. goto exit;
  3331. }
  3332. }
  3333. } /* for i */
  3334. } /* for times */
  3335. count += times;
  3336. } while (bench_stats_sym_check(start));
  3337. exit:
  3338. bench_stats_asym_finish("RSA", rsaKeySz, "private", doAsync, count,
  3339. start, ret);
  3340. }
  3341. else {
  3342. /* begin RSA sign */
  3343. bench_stats_start(&count, &start);
  3344. do {
  3345. for (times = 0; times < ntimes || pending > 0; ) {
  3346. bench_async_poll(&pending);
  3347. /* while free pending slots in queue, submit ops */
  3348. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3349. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  3350. 1, &times, ntimes, &pending)) {
  3351. ret = wc_RsaSSL_Sign(message, len, enc[i],
  3352. RSA_BUF_SIZE, &rsaKey[i], &rng);
  3353. if (!bench_async_handle(&ret,
  3354. BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  3355. 1, &times, &pending)) {
  3356. goto exit_rsa_sign;
  3357. }
  3358. }
  3359. } /* for i */
  3360. } /* for times */
  3361. count += times;
  3362. } while (bench_stats_sym_check(start));
  3363. exit_rsa_sign:
  3364. bench_stats_asym_finish("RSA", rsaKeySz, "sign", doAsync, count, start,
  3365. ret);
  3366. if (ret < 0) {
  3367. goto exit;
  3368. }
  3369. /* capture resulting encrypt length */
  3370. idx = rsaKeySz/8;
  3371. /* begin RSA verify */
  3372. bench_stats_start(&count, &start);
  3373. do {
  3374. for (times = 0; times < ntimes || pending > 0; ) {
  3375. bench_async_poll(&pending);
  3376. /* while free pending slots in queue, submit ops */
  3377. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3378. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  3379. 1, &times, ntimes, &pending)) {
  3380. ret = wc_RsaSSL_Verify(enc[i], idx, out[i],
  3381. RSA_BUF_SIZE, &rsaKey[i]);
  3382. if (!bench_async_handle(&ret,
  3383. BENCH_ASYNC_GET_DEV(&rsaKey[i]),
  3384. 1, &times, &pending)) {
  3385. goto exit_rsa_verify;
  3386. }
  3387. }
  3388. } /* for i */
  3389. } /* for times */
  3390. count += times;
  3391. } while (bench_stats_sym_check(start));
  3392. exit_rsa_verify:
  3393. bench_stats_asym_finish("RSA", rsaKeySz, "verify", doAsync, count,
  3394. start, ret);
  3395. }
  3396. /* cleanup */
  3397. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3398. wc_FreeRsaKey(&rsaKey[i]);
  3399. }
  3400. FREE_ARRAY(enc, BENCH_MAX_PENDING, HEAP_HINT);
  3401. FREE_ARRAY(out, BENCH_MAX_PENDING, HEAP_HINT);
  3402. FREE_VAR(message, HEAP_HINT);
  3403. }
  3404. #endif /* !NO_RSA */
  3405. #ifndef NO_DH
  3406. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  3407. #if defined(WOLFSSL_MDK_SHELL)
  3408. static char *certDHname = "certs/dh2048.der";
  3409. /* set by shell command */
  3410. void set_Bench_DH_File(char * cert) { certDHname = cert ; }
  3411. #elif defined(FREESCALE_MQX)
  3412. static char *certDHname = "a:\\certs\\dh2048.der";
  3413. #elif defined(NO_ASN)
  3414. /* do nothing, but don't need a file */
  3415. #else
  3416. static const char *certDHname = "certs/dh2048.der";
  3417. #endif
  3418. #endif
  3419. #define BENCH_DH_KEY_SIZE 256 /* for 2048 bit */
  3420. #define BENCH_DH_PRIV_SIZE (BENCH_DH_KEY_SIZE/8)
  3421. void bench_dh(int doAsync)
  3422. {
  3423. int ret = 0, i;
  3424. int count = 0, times, pending = 0;
  3425. const byte* tmp = NULL;
  3426. double start = 0.0f;
  3427. DhKey dhKey[BENCH_MAX_PENDING];
  3428. int dhKeySz = 2048; /* used in printf */
  3429. #ifndef NO_ASN
  3430. size_t bytes;
  3431. word32 idx;
  3432. #endif
  3433. word32 pubSz[BENCH_MAX_PENDING];
  3434. word32 privSz[BENCH_MAX_PENDING];
  3435. word32 pubSz2;
  3436. word32 privSz2;
  3437. word32 agreeSz[BENCH_MAX_PENDING];
  3438. DECLARE_ARRAY(pub, byte, BENCH_MAX_PENDING, BENCH_DH_KEY_SIZE, HEAP_HINT);
  3439. DECLARE_VAR(pub2, byte, BENCH_DH_KEY_SIZE, HEAP_HINT);
  3440. DECLARE_ARRAY(agree, byte, BENCH_MAX_PENDING, BENCH_DH_KEY_SIZE, HEAP_HINT);
  3441. DECLARE_ARRAY(priv, byte, BENCH_MAX_PENDING, BENCH_DH_PRIV_SIZE, HEAP_HINT);
  3442. DECLARE_VAR(priv2, byte, BENCH_DH_PRIV_SIZE, HEAP_HINT);
  3443. (void)tmp;
  3444. #if defined(NO_ASN)
  3445. dhKeySz = 1024;
  3446. /* do nothing, but don't use default FILE */
  3447. #elif defined(USE_CERT_BUFFERS_1024)
  3448. tmp = dh_key_der_1024;
  3449. bytes = (size_t)sizeof_dh_key_der_1024;
  3450. dhKeySz = 1024;
  3451. #elif defined(USE_CERT_BUFFERS_2048)
  3452. tmp = dh_key_der_2048;
  3453. bytes = (size_t)sizeof_dh_key_der_2048;
  3454. #else
  3455. #error "need to define a cert buffer size"
  3456. #endif /* USE_CERT_BUFFERS */
  3457. /* clear for done cleanup */
  3458. XMEMSET(dhKey, 0, sizeof(dhKey));
  3459. /* init keys */
  3460. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3461. /* setup an async context for each key */
  3462. ret = wc_InitDhKey_ex(&dhKey[i], HEAP_HINT,
  3463. doAsync ? devId : INVALID_DEVID);
  3464. if (ret != 0)
  3465. goto exit;
  3466. /* setup key */
  3467. #ifdef NO_ASN
  3468. ret = wc_DhSetKey(&dhKey[i], dh_p, sizeof(dh_p), dh_g, sizeof(dh_g));
  3469. #else
  3470. idx = 0;
  3471. ret = wc_DhKeyDecode(tmp, &idx, &dhKey[i], (word32)bytes);
  3472. #endif
  3473. if (ret != 0) {
  3474. printf("DhKeyDecode failed %d, can't benchmark\n", ret);
  3475. goto exit;
  3476. }
  3477. }
  3478. /* Key Gen */
  3479. bench_stats_start(&count, &start);
  3480. do {
  3481. /* while free pending slots in queue, submit ops */
  3482. for (times = 0; times < genTimes || pending > 0; ) {
  3483. bench_async_poll(&pending);
  3484. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3485. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, &times, genTimes, &pending)) {
  3486. privSz[i] = 0;
  3487. ret = wc_DhGenerateKeyPair(&dhKey[i], &rng, priv[i], &privSz[i],
  3488. pub[i], &pubSz[i]);
  3489. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, &times, &pending)) {
  3490. goto exit_dh_gen;
  3491. }
  3492. }
  3493. } /* for i */
  3494. } /* for times */
  3495. count += times;
  3496. } while (bench_stats_sym_check(start));
  3497. exit_dh_gen:
  3498. bench_stats_asym_finish("DH", dhKeySz, "key gen", doAsync, count, start, ret);
  3499. if (ret < 0) {
  3500. goto exit;
  3501. }
  3502. /* Generate key to use as other public */
  3503. ret = wc_DhGenerateKeyPair(&dhKey[0], &rng, priv2, &privSz2, pub2, &pubSz2);
  3504. #ifdef WOLFSSL_ASYNC_CRYPT
  3505. ret = wc_AsyncWait(ret, &dhKey[0].asyncDev, WC_ASYNC_FLAG_NONE);
  3506. #endif
  3507. /* Key Agree */
  3508. bench_stats_start(&count, &start);
  3509. do {
  3510. for (times = 0; times < agreeTimes || pending > 0; ) {
  3511. bench_async_poll(&pending);
  3512. /* while free pending slots in queue, submit ops */
  3513. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3514. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, &times, agreeTimes, &pending)) {
  3515. ret = wc_DhAgree(&dhKey[i], agree[i], &agreeSz[i], priv[i], privSz[i],
  3516. pub2, pubSz2);
  3517. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&dhKey[i]), 0, &times, &pending)) {
  3518. goto exit;
  3519. }
  3520. }
  3521. } /* for i */
  3522. } /* for times */
  3523. count += times;
  3524. } while (bench_stats_sym_check(start));
  3525. exit:
  3526. bench_stats_asym_finish("DH", dhKeySz, "agree", doAsync, count, start, ret);
  3527. /* cleanup */
  3528. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3529. wc_FreeDhKey(&dhKey[i]);
  3530. }
  3531. FREE_ARRAY(pub, BENCH_MAX_PENDING, HEAP_HINT);
  3532. FREE_VAR(pub2, HEAP_HINT);
  3533. FREE_ARRAY(priv, BENCH_MAX_PENDING, HEAP_HINT);
  3534. FREE_VAR(priv2, HEAP_HINT);
  3535. FREE_ARRAY(agree, BENCH_MAX_PENDING, HEAP_HINT);
  3536. }
  3537. #endif /* !NO_DH */
  3538. #ifdef HAVE_NTRU
  3539. byte GetEntropy(ENTROPY_CMD cmd, byte* out);
  3540. byte GetEntropy(ENTROPY_CMD cmd, byte* out)
  3541. {
  3542. if (cmd == INIT)
  3543. return 1; /* using local rng */
  3544. if (out == NULL)
  3545. return 0;
  3546. if (cmd == GET_BYTE_OF_ENTROPY)
  3547. return (wc_RNG_GenerateBlock(&rng, out, 1) == 0) ? 1 : 0;
  3548. if (cmd == GET_NUM_BYTES_PER_BYTE_OF_ENTROPY) {
  3549. *out = 1;
  3550. return 1;
  3551. }
  3552. return 0;
  3553. }
  3554. void bench_ntru(void)
  3555. {
  3556. int i;
  3557. double start;
  3558. byte public_key[1027];
  3559. word16 public_key_len = sizeof(public_key);
  3560. byte private_key[1120];
  3561. word16 private_key_len = sizeof(private_key);
  3562. word16 ntruBits = 128;
  3563. word16 type = 0;
  3564. word32 ret;
  3565. byte ciphertext[1022];
  3566. word16 ciphertext_len;
  3567. byte plaintext[16];
  3568. word16 plaintext_len;
  3569. DRBG_HANDLE drbg;
  3570. static byte const aes_key[] = {
  3571. 0xf3, 0xe9, 0x87, 0xbb, 0x18, 0x08, 0x3c, 0xaa,
  3572. 0x7b, 0x12, 0x49, 0x88, 0xaf, 0xb3, 0x22, 0xd8
  3573. };
  3574. static byte const wolfsslStr[] = {
  3575. 'w', 'o', 'l', 'f', 'S', 'S', 'L', ' ', 'N', 'T', 'R', 'U'
  3576. };
  3577. for (ntruBits = 128; ntruBits < 257; ntruBits += 64) {
  3578. switch (ntruBits) {
  3579. case 128:
  3580. type = NTRU_EES439EP1;
  3581. break;
  3582. case 192:
  3583. type = NTRU_EES593EP1;
  3584. break;
  3585. case 256:
  3586. type = NTRU_EES743EP1;
  3587. break;
  3588. }
  3589. ret = ntru_crypto_drbg_instantiate(ntruBits, wolfsslStr,
  3590. sizeof(wolfsslStr), (ENTROPY_FN) GetEntropy, &drbg);
  3591. if(ret != DRBG_OK) {
  3592. printf("NTRU drbg instantiate failed\n");
  3593. return;
  3594. }
  3595. /* set key sizes */
  3596. ret = ntru_crypto_ntru_encrypt_keygen(drbg, type, &public_key_len,
  3597. NULL, &private_key_len, NULL);
  3598. if (ret != NTRU_OK) {
  3599. ntru_crypto_drbg_uninstantiate(drbg);
  3600. printf("NTRU failed to get key lengths\n");
  3601. return;
  3602. }
  3603. ret = ntru_crypto_ntru_encrypt_keygen(drbg, type, &public_key_len,
  3604. public_key, &private_key_len,
  3605. private_key);
  3606. ntru_crypto_drbg_uninstantiate(drbg);
  3607. if (ret != NTRU_OK) {
  3608. printf("NTRU keygen failed\n");
  3609. return;
  3610. }
  3611. ret = ntru_crypto_drbg_instantiate(ntruBits, NULL, 0,
  3612. (ENTROPY_FN)GetEntropy, &drbg);
  3613. if (ret != DRBG_OK) {
  3614. printf("NTRU error occurred during DRBG instantiation\n");
  3615. return;
  3616. }
  3617. ret = ntru_crypto_ntru_encrypt(drbg, public_key_len, public_key,
  3618. sizeof(aes_key), aes_key, &ciphertext_len, NULL);
  3619. if (ret != NTRU_OK) {
  3620. printf("NTRU error occurred requesting the buffer size needed\n");
  3621. return;
  3622. }
  3623. bench_stats_start(&i, &start);
  3624. for (i = 0; i < ntimes; i++) {
  3625. ret = ntru_crypto_ntru_encrypt(drbg, public_key_len, public_key,
  3626. sizeof(aes_key), aes_key, &ciphertext_len, ciphertext);
  3627. if (ret != NTRU_OK) {
  3628. printf("NTRU encrypt error\n");
  3629. return;
  3630. }
  3631. }
  3632. bench_stats_asym_finish("NTRU", ntruBits, "encryption", 0, i, start, ret);
  3633. ret = ntru_crypto_drbg_uninstantiate(drbg);
  3634. if (ret != DRBG_OK) {
  3635. printf("NTRU error occurred uninstantiating the DRBG\n");
  3636. return;
  3637. }
  3638. ret = ntru_crypto_ntru_decrypt(private_key_len, private_key,
  3639. ciphertext_len, ciphertext, &plaintext_len, NULL);
  3640. if (ret != NTRU_OK) {
  3641. printf("NTRU decrypt error occurred getting the buffer size needed\n");
  3642. return;
  3643. }
  3644. plaintext_len = sizeof(plaintext);
  3645. bench_stats_start(&i, &start);
  3646. for (i = 0; i < ntimes; i++) {
  3647. ret = ntru_crypto_ntru_decrypt(private_key_len, private_key,
  3648. ciphertext_len, ciphertext,
  3649. &plaintext_len, plaintext);
  3650. if (ret != NTRU_OK) {
  3651. printf("NTRU error occurred decrypting the key\n");
  3652. return;
  3653. }
  3654. }
  3655. bench_stats_asym_finish("NTRU", ntruBits, "decryption", 0, i, start, ret);
  3656. }
  3657. }
  3658. void bench_ntruKeyGen(void)
  3659. {
  3660. double start;
  3661. int i;
  3662. byte public_key[1027];
  3663. word16 public_key_len = sizeof(public_key);
  3664. byte private_key[1120];
  3665. word16 private_key_len = sizeof(private_key);
  3666. word16 ntruBits = 128;
  3667. word16 type = 0;
  3668. word32 ret;
  3669. DRBG_HANDLE drbg;
  3670. static uint8_t const pers_str[] = {
  3671. 'w', 'o', 'l', 'f', 'S', 'S', 'L', ' ', 't', 'e', 's', 't'
  3672. };
  3673. for (ntruBits = 128; ntruBits < 257; ntruBits += 64) {
  3674. ret = ntru_crypto_drbg_instantiate(ntruBits, pers_str,
  3675. sizeof(pers_str), GetEntropy, &drbg);
  3676. if (ret != DRBG_OK) {
  3677. printf("NTRU drbg instantiate failed\n");
  3678. return;
  3679. }
  3680. switch (ntruBits) {
  3681. case 128:
  3682. type = NTRU_EES439EP1;
  3683. break;
  3684. case 192:
  3685. type = NTRU_EES593EP1;
  3686. break;
  3687. case 256:
  3688. type = NTRU_EES743EP1;
  3689. break;
  3690. }
  3691. /* set key sizes */
  3692. ret = ntru_crypto_ntru_encrypt_keygen(drbg, type, &public_key_len,
  3693. NULL, &private_key_len, NULL);
  3694. bench_stats_start(&i, &start);
  3695. for (i = 0; i < genTimes; i++) {
  3696. ret = ntru_crypto_ntru_encrypt_keygen(drbg, type, &public_key_len,
  3697. public_key, &private_key_len,
  3698. private_key);
  3699. }
  3700. bench_stats_asym_finish("NTRU", ntruBits, "key gen", 0, i, start, ret);
  3701. if (ret != NTRU_OK) {
  3702. return;
  3703. }
  3704. ret = ntru_crypto_drbg_uninstantiate(drbg);
  3705. if (ret != NTRU_OK) {
  3706. printf("NTRU drbg uninstantiate failed\n");
  3707. return;
  3708. }
  3709. }
  3710. }
  3711. #endif
  3712. #ifdef HAVE_ECC
  3713. #ifndef BENCH_ECC_SIZE
  3714. #define BENCH_ECC_SIZE 32
  3715. #endif
  3716. void bench_eccMakeKey(int doAsync)
  3717. {
  3718. int ret = 0, i, times, count, pending = 0;
  3719. const int keySize = BENCH_ECC_SIZE;
  3720. ecc_key genKey[BENCH_MAX_PENDING];
  3721. double start;
  3722. /* clear for done cleanup */
  3723. XMEMSET(&genKey, 0, sizeof(genKey));
  3724. /* ECC Make Key */
  3725. bench_stats_start(&count, &start);
  3726. do {
  3727. /* while free pending slots in queue, submit ops */
  3728. for (times = 0; times < genTimes || pending > 0; ) {
  3729. bench_async_poll(&pending);
  3730. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3731. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, &times, genTimes, &pending)) {
  3732. wc_ecc_free(&genKey[i]);
  3733. ret = wc_ecc_init_ex(&genKey[i], HEAP_HINT, doAsync ? devId : INVALID_DEVID);
  3734. if (ret < 0) {
  3735. goto exit;
  3736. }
  3737. ret = wc_ecc_make_key(&rng, keySize, &genKey[i]);
  3738. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 0, &times, &pending)) {
  3739. goto exit;
  3740. }
  3741. }
  3742. } /* for i */
  3743. } /* for times */
  3744. count += times;
  3745. } while (bench_stats_sym_check(start));
  3746. exit:
  3747. bench_stats_asym_finish("ECC", keySize * 8, "key gen", doAsync, count, start, ret);
  3748. /* cleanup */
  3749. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3750. wc_ecc_free(&genKey[i]);
  3751. }
  3752. }
  3753. void bench_ecc(int doAsync)
  3754. {
  3755. int ret = 0, i, times, count, pending = 0;
  3756. const int keySize = BENCH_ECC_SIZE;
  3757. ecc_key genKey[BENCH_MAX_PENDING];
  3758. #ifdef HAVE_ECC_DHE
  3759. ecc_key genKey2[BENCH_MAX_PENDING];
  3760. #endif
  3761. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  3762. #ifdef HAVE_ECC_VERIFY
  3763. int verify[BENCH_MAX_PENDING];
  3764. #endif
  3765. #endif
  3766. word32 x[BENCH_MAX_PENDING];
  3767. double start;
  3768. #ifdef HAVE_ECC_DHE
  3769. DECLARE_ARRAY(shared, byte, BENCH_MAX_PENDING, BENCH_ECC_SIZE, HEAP_HINT);
  3770. #endif
  3771. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  3772. DECLARE_ARRAY(sig, byte, BENCH_MAX_PENDING, ECC_MAX_SIG_SIZE, HEAP_HINT);
  3773. #endif
  3774. DECLARE_ARRAY(digest, byte, BENCH_MAX_PENDING, BENCH_ECC_SIZE, HEAP_HINT);
  3775. /* clear for done cleanup */
  3776. XMEMSET(&genKey, 0, sizeof(genKey));
  3777. #ifdef HAVE_ECC_DHE
  3778. XMEMSET(&genKey2, 0, sizeof(genKey2));
  3779. #endif
  3780. /* init keys */
  3781. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3782. /* setup an context for each key */
  3783. if ((ret = wc_ecc_init_ex(&genKey[i], HEAP_HINT,
  3784. doAsync ? devId : INVALID_DEVID)) < 0) {
  3785. goto exit;
  3786. }
  3787. ret = wc_ecc_make_key(&rng, keySize, &genKey[i]);
  3788. #ifdef WOLFSSL_ASYNC_CRYPT
  3789. ret = wc_AsyncWait(ret, &genKey[i].asyncDev, WC_ASYNC_FLAG_NONE);
  3790. #endif
  3791. if (ret < 0) {
  3792. goto exit;
  3793. }
  3794. #ifdef HAVE_ECC_DHE
  3795. if ((ret = wc_ecc_init_ex(&genKey2[i], HEAP_HINT, INVALID_DEVID)) < 0) {
  3796. goto exit;
  3797. }
  3798. if ((ret = wc_ecc_make_key(&rng, keySize, &genKey2[i])) > 0) {
  3799. goto exit;
  3800. }
  3801. #endif
  3802. }
  3803. #ifdef HAVE_ECC_DHE
  3804. /* ECC Shared Secret */
  3805. bench_stats_start(&count, &start);
  3806. do {
  3807. for (times = 0; times < agreeTimes || pending > 0; ) {
  3808. bench_async_poll(&pending);
  3809. /* while free pending slots in queue, submit ops */
  3810. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3811. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, &times, agreeTimes, &pending)) {
  3812. x[i] = (word32)keySize;
  3813. ret = wc_ecc_shared_secret(&genKey[i], &genKey2[i], shared[i], &x[i]);
  3814. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, &times, &pending)) {
  3815. goto exit_ecdhe;
  3816. }
  3817. }
  3818. } /* for i */
  3819. } /* for times */
  3820. count += times;
  3821. } while (bench_stats_sym_check(start));
  3822. exit_ecdhe:
  3823. bench_stats_asym_finish("ECDHE", keySize * 8, "agree", doAsync, count, start, ret);
  3824. if (ret < 0) {
  3825. goto exit;
  3826. }
  3827. #endif /* HAVE_ECC_DHE */
  3828. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  3829. /* Init digest to sign */
  3830. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3831. for (count = 0; count < keySize; count++) {
  3832. digest[i][count] = (byte)count;
  3833. }
  3834. }
  3835. /* ECC Sign */
  3836. bench_stats_start(&count, &start);
  3837. do {
  3838. for (times = 0; times < agreeTimes || pending > 0; ) {
  3839. bench_async_poll(&pending);
  3840. /* while free pending slots in queue, submit ops */
  3841. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3842. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, &times, agreeTimes, &pending)) {
  3843. if (genKey[i].state == 0)
  3844. x[i] = ECC_MAX_SIG_SIZE;
  3845. ret = wc_ecc_sign_hash(digest[i], (word32)keySize, sig[i], &x[i],
  3846. &rng, &genKey[i]);
  3847. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, &times, &pending)) {
  3848. goto exit_ecdsa_sign;
  3849. }
  3850. }
  3851. } /* for i */
  3852. } /* for times */
  3853. count += times;
  3854. } while (bench_stats_sym_check(start));
  3855. exit_ecdsa_sign:
  3856. bench_stats_asym_finish("ECDSA", keySize * 8, "sign", doAsync, count, start, ret);
  3857. if (ret < 0) {
  3858. goto exit;
  3859. }
  3860. #ifdef HAVE_ECC_VERIFY
  3861. /* ECC Verify */
  3862. bench_stats_start(&count, &start);
  3863. do {
  3864. for (times = 0; times < agreeTimes || pending > 0; ) {
  3865. bench_async_poll(&pending);
  3866. /* while free pending slots in queue, submit ops */
  3867. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3868. if (bench_async_check(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, &times, agreeTimes, &pending)) {
  3869. if (genKey[i].state == 0)
  3870. verify[i] = 0;
  3871. ret = wc_ecc_verify_hash(sig[i], x[i], digest[i],
  3872. (word32)keySize, &verify[i], &genKey[i]);
  3873. if (!bench_async_handle(&ret, BENCH_ASYNC_GET_DEV(&genKey[i]), 1, &times, &pending)) {
  3874. goto exit_ecdsa_verify;
  3875. }
  3876. }
  3877. } /* for i */
  3878. } /* for times */
  3879. count += times;
  3880. } while (bench_stats_sym_check(start));
  3881. exit_ecdsa_verify:
  3882. bench_stats_asym_finish("ECDSA", keySize * 8, "verify", doAsync, count, start, ret);
  3883. #endif /* HAVE_ECC_VERIFY */
  3884. #endif /* !NO_ASN && HAVE_ECC_SIGN */
  3885. exit:
  3886. /* cleanup */
  3887. for (i = 0; i < BENCH_MAX_PENDING; i++) {
  3888. wc_ecc_free(&genKey[i]);
  3889. #ifdef HAVE_ECC_DHE
  3890. wc_ecc_free(&genKey2[i]);
  3891. #endif
  3892. }
  3893. #ifdef HAVE_ECC_DHE
  3894. FREE_ARRAY(shared, BENCH_MAX_PENDING, HEAP_HINT);
  3895. #endif
  3896. #if !defined(NO_ASN) && defined(HAVE_ECC_SIGN)
  3897. FREE_ARRAY(sig, BENCH_MAX_PENDING, HEAP_HINT);
  3898. #endif
  3899. FREE_ARRAY(digest, BENCH_MAX_PENDING, HEAP_HINT);
  3900. }
  3901. #ifdef HAVE_ECC_ENCRYPT
  3902. void bench_eccEncrypt(void)
  3903. {
  3904. ecc_key userA, userB;
  3905. const int keySize = BENCH_ECC_SIZE;
  3906. byte msg[48];
  3907. byte out[80];
  3908. word32 outSz = sizeof(out);
  3909. word32 bench_plainSz = BENCH_SIZE;
  3910. int ret, i, count;
  3911. double start;
  3912. ret = wc_ecc_init_ex(&userA, HEAP_HINT, devId);
  3913. if (ret != 0) {
  3914. printf("wc_ecc_encrypt make key A failed: %d\n", ret);
  3915. return;
  3916. }
  3917. ret = wc_ecc_init_ex(&userB, HEAP_HINT, devId);
  3918. if (ret != 0) {
  3919. printf("wc_ecc_encrypt make key B failed: %d\n", ret);
  3920. wc_ecc_free(&userA);
  3921. return;
  3922. }
  3923. ret = wc_ecc_make_key(&rng, keySize, &userA);
  3924. #ifdef WOLFSSL_ASYNC_CRYPT
  3925. ret = wc_AsyncWait(ret, &userA.asyncDev, WC_ASYNC_FLAG_NONE);
  3926. #endif
  3927. if (ret != 0)
  3928. goto exit;
  3929. ret = wc_ecc_make_key(&rng, keySize, &userB);
  3930. #ifdef WOLFSSL_ASYNC_CRYPT
  3931. ret = wc_AsyncWait(ret, &userB.asyncDev, WC_ASYNC_FLAG_NONE);
  3932. #endif
  3933. if (ret != 0)
  3934. goto exit;
  3935. for (i = 0; i < (int)sizeof(msg); i++)
  3936. msg[i] = i;
  3937. bench_stats_start(&count, &start);
  3938. do {
  3939. for (i = 0; i < ntimes; i++) {
  3940. /* encrypt msg to B */
  3941. ret = wc_ecc_encrypt(&userA, &userB, msg, sizeof(msg), out, &outSz, NULL);
  3942. if (ret != 0) {
  3943. printf("wc_ecc_encrypt failed! %d\n", ret);
  3944. goto exit_enc;
  3945. }
  3946. }
  3947. count += i;
  3948. } while (bench_stats_sym_check(start));
  3949. exit_enc:
  3950. bench_stats_asym_finish("ECC", keySize * 8, "encrypt", 0, count, start, ret);
  3951. bench_stats_start(&count, &start);
  3952. do {
  3953. for (i = 0; i < ntimes; i++) {
  3954. /* decrypt msg from A */
  3955. ret = wc_ecc_decrypt(&userB, &userA, out, outSz, bench_plain, &bench_plainSz, NULL);
  3956. if (ret != 0) {
  3957. printf("wc_ecc_decrypt failed! %d\n", ret);
  3958. goto exit_dec;
  3959. }
  3960. }
  3961. count += i;
  3962. } while (bench_stats_sym_check(start));
  3963. exit_dec:
  3964. bench_stats_asym_finish("ECC", keySize * 8, "decrypt", 0, count, start, ret);
  3965. exit:
  3966. /* cleanup */
  3967. wc_ecc_free(&userB);
  3968. wc_ecc_free(&userA);
  3969. }
  3970. #endif
  3971. #endif /* HAVE_ECC */
  3972. #ifdef HAVE_CURVE25519
  3973. void bench_curve25519KeyGen(void)
  3974. {
  3975. curve25519_key genKey;
  3976. double start;
  3977. int ret = 0, i, count;
  3978. /* Key Gen */
  3979. bench_stats_start(&count, &start);
  3980. do {
  3981. for (i = 0; i < genTimes; i++) {
  3982. ret = wc_curve25519_make_key(&rng, 32, &genKey);
  3983. wc_curve25519_free(&genKey);
  3984. if (ret != 0) {
  3985. printf("wc_curve25519_make_key failed: %d\n", ret);
  3986. break;
  3987. }
  3988. }
  3989. count += i;
  3990. } while (bench_stats_sym_check(start));
  3991. bench_stats_asym_finish("CURVE", 25519, "key gen", 0, count, start, ret);
  3992. }
  3993. #ifdef HAVE_CURVE25519_SHARED_SECRET
  3994. void bench_curve25519KeyAgree(void)
  3995. {
  3996. curve25519_key genKey, genKey2;
  3997. double start;
  3998. int ret, i, count;
  3999. byte shared[32];
  4000. word32 x = 0;
  4001. wc_curve25519_init(&genKey);
  4002. wc_curve25519_init(&genKey2);
  4003. ret = wc_curve25519_make_key(&rng, 32, &genKey);
  4004. if (ret != 0) {
  4005. printf("curve25519_make_key failed\n");
  4006. return;
  4007. }
  4008. ret = wc_curve25519_make_key(&rng, 32, &genKey2);
  4009. if (ret != 0) {
  4010. printf("curve25519_make_key failed: %d\n", ret);
  4011. wc_curve25519_free(&genKey);
  4012. return;
  4013. }
  4014. /* Shared secret */
  4015. bench_stats_start(&count, &start);
  4016. do {
  4017. for (i = 0; i < agreeTimes; i++) {
  4018. x = sizeof(shared);
  4019. ret = wc_curve25519_shared_secret(&genKey, &genKey2, shared, &x);
  4020. if (ret != 0) {
  4021. printf("curve25519_shared_secret failed: %d\n", ret);
  4022. goto exit;
  4023. }
  4024. }
  4025. count += i;
  4026. } while (bench_stats_sym_check(start));
  4027. exit:
  4028. bench_stats_asym_finish("CURVE", 25519, "agree", 0, count, start, ret);
  4029. wc_curve25519_free(&genKey2);
  4030. wc_curve25519_free(&genKey);
  4031. }
  4032. #endif /* HAVE_CURVE25519_SHARED_SECRET */
  4033. #endif /* HAVE_CURVE25519 */
  4034. #ifdef HAVE_ED25519
  4035. void bench_ed25519KeyGen(void)
  4036. {
  4037. ed25519_key genKey;
  4038. double start;
  4039. int i, count;
  4040. /* Key Gen */
  4041. bench_stats_start(&count, &start);
  4042. do {
  4043. for (i = 0; i < genTimes; i++) {
  4044. wc_ed25519_init(&genKey);
  4045. wc_ed25519_make_key(&rng, 32, &genKey);
  4046. wc_ed25519_free(&genKey);
  4047. }
  4048. count += i;
  4049. } while (bench_stats_sym_check(start));
  4050. bench_stats_asym_finish("ED", 25519, "key gen", 0, count, start, 0);
  4051. }
  4052. void bench_ed25519KeySign(void)
  4053. {
  4054. int ret;
  4055. ed25519_key genKey;
  4056. #ifdef HAVE_ED25519_SIGN
  4057. double start;
  4058. int i, count;
  4059. byte sig[ED25519_SIG_SIZE];
  4060. byte msg[512];
  4061. word32 x = 0;
  4062. #endif
  4063. wc_ed25519_init(&genKey);
  4064. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &genKey);
  4065. if (ret != 0) {
  4066. printf("ed25519_make_key failed\n");
  4067. return;
  4068. }
  4069. #ifdef HAVE_ED25519_SIGN
  4070. /* make dummy msg */
  4071. for (i = 0; i < (int)sizeof(msg); i++)
  4072. msg[i] = (byte)i;
  4073. bench_stats_start(&count, &start);
  4074. do {
  4075. for (i = 0; i < agreeTimes; i++) {
  4076. x = sizeof(sig);
  4077. ret = wc_ed25519_sign_msg(msg, sizeof(msg), sig, &x, &genKey);
  4078. if (ret != 0) {
  4079. printf("ed25519_sign_msg failed\n");
  4080. goto exit_ed_sign;
  4081. }
  4082. }
  4083. count += i;
  4084. } while (bench_stats_sym_check(start));
  4085. exit_ed_sign:
  4086. bench_stats_asym_finish("ED", 25519, "sign", 0, count, start, ret);
  4087. #ifdef HAVE_ED25519_VERIFY
  4088. bench_stats_start(&count, &start);
  4089. do {
  4090. for (i = 0; i < agreeTimes; i++) {
  4091. int verify = 0;
  4092. ret = wc_ed25519_verify_msg(sig, x, msg, sizeof(msg), &verify,
  4093. &genKey);
  4094. if (ret != 0 || verify != 1) {
  4095. printf("ed25519_verify_msg failed\n");
  4096. goto exit_ed_verify;
  4097. }
  4098. }
  4099. count += i;
  4100. } while (bench_stats_sym_check(start));
  4101. exit_ed_verify:
  4102. bench_stats_asym_finish("ED", 25519, "verify", 0, count, start, ret);
  4103. #endif /* HAVE_ED25519_VERIFY */
  4104. #endif /* HAVE_ED25519_SIGN */
  4105. wc_ed25519_free(&genKey);
  4106. }
  4107. #endif /* HAVE_ED25519 */
  4108. #ifndef HAVE_STACK_SIZE
  4109. #if defined(_WIN32) && !defined(INTIME_RTOS)
  4110. #define WIN32_LEAN_AND_MEAN
  4111. #include <windows.h>
  4112. double current_time(int reset)
  4113. {
  4114. static int init = 0;
  4115. static LARGE_INTEGER freq;
  4116. LARGE_INTEGER count;
  4117. (void)reset;
  4118. if (!init) {
  4119. QueryPerformanceFrequency(&freq);
  4120. init = 1;
  4121. }
  4122. QueryPerformanceCounter(&count);
  4123. return (double)count.QuadPart / freq.QuadPart;
  4124. }
  4125. #elif defined MICROCHIP_PIC32
  4126. #if defined(WOLFSSL_MICROCHIP_PIC32MZ)
  4127. #define CLOCK 80000000.0
  4128. #else
  4129. #define CLOCK 40000000.0
  4130. #endif
  4131. extern void WriteCoreTimer(word32 t);
  4132. extern word32 ReadCoreTimer(void);
  4133. double current_time(int reset)
  4134. {
  4135. unsigned int ns;
  4136. if (reset) {
  4137. WriteCoreTimer(0);
  4138. }
  4139. /* get timer in ns */
  4140. ns = ReadCoreTimer();
  4141. /* return seconds as a double */
  4142. return ( ns / CLOCK * 2.0);
  4143. }
  4144. #elif defined(WOLFSSL_IAR_ARM_TIME) || defined (WOLFSSL_MDK_ARM) || \
  4145. defined(WOLFSSL_USER_CURRTIME) || defined(WOLFSSL_CURRTIME_REMAP)
  4146. /* declared above at line 239 */
  4147. /* extern double current_time(int reset); */
  4148. #elif defined FREERTOS
  4149. double current_time(int reset)
  4150. {
  4151. portTickType tickCount;
  4152. (void) reset;
  4153. /* tick count == ms, if configTICK_RATE_HZ is set to 1000 */
  4154. tickCount = xTaskGetTickCount();
  4155. return (double)tickCount / 1000;
  4156. }
  4157. #elif defined (WOLFSSL_TIRTOS)
  4158. extern double current_time(int reset);
  4159. #elif defined(FREESCALE_MQX)
  4160. double current_time(int reset)
  4161. {
  4162. TIME_STRUCT tv;
  4163. _time_get(&tv);
  4164. return (double)tv.SECONDS + (double)tv.MILLISECONDS / 1000;
  4165. }
  4166. #elif defined(FREESCALE_KSDK_BM)
  4167. double current_time(int reset)
  4168. {
  4169. return (double)OSA_TimeGetMsec() / 1000;
  4170. }
  4171. #elif defined(WOLFSSL_EMBOS)
  4172. #include "RTOS.h"
  4173. double current_time(int reset)
  4174. {
  4175. double time_now;
  4176. double current_s = OS_GetTime() / 1000.0;
  4177. double current_us = OS_GetTime_us() / 1000000.0;
  4178. time_now = (double)( current_s + current_us);
  4179. (void) reset;
  4180. return time_now;
  4181. }
  4182. #elif defined(WOLFSSL_SGX)
  4183. double current_time(int reset);
  4184. #else
  4185. #include <sys/time.h>
  4186. double current_time(int reset)
  4187. {
  4188. struct timeval tv;
  4189. (void)reset;
  4190. gettimeofday(&tv, 0);
  4191. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  4192. }
  4193. #endif /* _WIN32 */
  4194. #endif /* !HAVE_STACK_SIZE */
  4195. #if defined(HAVE_GET_CYCLES)
  4196. static INLINE word64 get_intel_cycles(void)
  4197. {
  4198. unsigned int lo_c, hi_c;
  4199. __asm__ __volatile__ (
  4200. "cpuid\n\t"
  4201. "rdtsc"
  4202. : "=a"(lo_c), "=d"(hi_c) /* out */
  4203. : "a"(0) /* in */
  4204. : "%ebx", "%ecx"); /* clobber */
  4205. return ((word64)lo_c) | (((word64)hi_c) << 32);
  4206. }
  4207. #endif /* HAVE_GET_CYCLES */
  4208. void benchmark_configure(int block_size)
  4209. {
  4210. /* must be greater than 0 */
  4211. if (block_size > 0) {
  4212. numBlocks = numBlocks * bench_size / block_size;
  4213. bench_size = (word32)block_size;
  4214. }
  4215. }
  4216. #ifndef NO_MAIN_DRIVER
  4217. #ifndef WOLFSSL_BENCHMARK_ALL
  4218. /* Display the algorithm string and keep to 80 characters per line.
  4219. *
  4220. * str Algorithm string to print.
  4221. * line Length of line used so far.
  4222. */
  4223. static void print_alg(const char* str, int* line)
  4224. {
  4225. int optLen;
  4226. optLen = (int)XSTRLEN(str) + 1;
  4227. if (optLen + *line > 80) {
  4228. printf("\n ");
  4229. *line = 13;
  4230. }
  4231. *line += optLen;
  4232. printf(" %s", str);
  4233. }
  4234. #endif
  4235. /* Display the usage options of the benchmark program. */
  4236. static void Usage(void)
  4237. {
  4238. #ifndef WOLFSSL_BENCHMARK_ALL
  4239. int i;
  4240. int line;
  4241. #endif
  4242. printf("benchmark\n");
  4243. printf("-? Help, print this usage\n");
  4244. printf("-base10 Display bytes as power of 10 (eg 1 kB = 1000 Bytes)\n");
  4245. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  4246. printf("-no_aad No additional authentication data passed.\n");
  4247. #endif
  4248. printf("-dgst_full Full digest operation performed.\n");
  4249. #ifndef NO_RSA
  4250. printf("-rsa_sign Measure RSA sign/verify instead of encrypt/decrypt.\n");
  4251. #endif
  4252. #ifndef WOLFSSL_BENCHMARK_ALL
  4253. printf("-<alg> Algorithm to benchmark. Available algorithms "
  4254. "include:\n");
  4255. printf(" ");
  4256. line = 13;
  4257. for (i=0; bench_cipher_opt[i].str != NULL; i++)
  4258. print_alg(bench_cipher_opt[i].str + 1, &line);
  4259. printf("\n ");
  4260. line = 13;
  4261. for (i=0; bench_digest_opt[i].str != NULL; i++)
  4262. print_alg(bench_digest_opt[i].str + 1, &line);
  4263. printf("\n ");
  4264. line = 13;
  4265. for (i=0; bench_mac_opt[i].str != NULL; i++)
  4266. print_alg(bench_mac_opt[i].str + 1, &line);
  4267. printf("\n ");
  4268. line = 13;
  4269. for (i=0; bench_asym_opt[i].str != NULL; i++)
  4270. print_alg(bench_asym_opt[i].str + 1, &line);
  4271. printf("\n ");
  4272. line = 13;
  4273. for (i=0; bench_other_opt[i].str != NULL; i++)
  4274. print_alg(bench_other_opt[i].str + 1, &line);
  4275. printf("\n");
  4276. #endif
  4277. printf("<num> Size of block in bytes\n");
  4278. }
  4279. /* Match the command line argument with the string.
  4280. *
  4281. * arg Command line argument.
  4282. * str String to check for.
  4283. * return 1 if the command line argument matches the string, 0 otherwise.
  4284. */
  4285. static int string_matches(const char* arg, const char* str)
  4286. {
  4287. int len = (int)XSTRLEN(str) + 1;
  4288. return XSTRNCMP(arg, str, len) == 0;
  4289. }
  4290. int main(int argc, char** argv)
  4291. {
  4292. int ret = 0;
  4293. int optMatched;
  4294. #ifndef WOLFSSL_BENCHMARK_ALL
  4295. int i;
  4296. #endif
  4297. while (argc > 1) {
  4298. if (string_matches(argv[1], "-?")) {
  4299. Usage();
  4300. return 0;
  4301. }
  4302. else if (string_matches(argv[1], "-base10"))
  4303. base2 = 0;
  4304. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  4305. else if (string_matches(argv[1], "-no_aad"))
  4306. aesAuthAddSz = 0;
  4307. #endif
  4308. else if (string_matches(argv[1], "-dgst_full"))
  4309. digest_stream = 0;
  4310. #ifndef NO_RSA
  4311. else if (string_matches(argv[1], "-rsa_sign"))
  4312. rsa_sign_verify = 1;
  4313. #endif
  4314. else if (argv[1][0] == '-') {
  4315. optMatched = 0;
  4316. #ifndef WOLFSSL_BENCHMARK_ALL
  4317. /* Check known algorithm choosing command line options. */
  4318. /* Known cipher algorithms */
  4319. for (i=0; !optMatched && bench_cipher_opt[i].str != NULL; i++) {
  4320. if (string_matches(argv[1], bench_cipher_opt[i].str)) {
  4321. bench_cipher_algs |= bench_cipher_opt[i].val;
  4322. bench_all = 0;
  4323. optMatched = 1;
  4324. }
  4325. }
  4326. /* Known digest algorithms */
  4327. for (i=0; !optMatched && bench_digest_opt[i].str != NULL; i++) {
  4328. if (string_matches(argv[1], bench_digest_opt[i].str)) {
  4329. bench_digest_algs |= bench_digest_opt[i].val;
  4330. bench_all = 0;
  4331. optMatched = 1;
  4332. }
  4333. }
  4334. /* Known MAC algorithms */
  4335. for (i=0; !optMatched && bench_mac_opt[i].str != NULL; i++) {
  4336. if (string_matches(argv[1], bench_mac_opt[i].str)) {
  4337. bench_mac_algs |= bench_mac_opt[i].val;
  4338. bench_all = 0;
  4339. optMatched = 1;
  4340. }
  4341. }
  4342. /* Known asymmetric algorithms */
  4343. for (i=0; !optMatched && bench_asym_opt[i].str != NULL; i++) {
  4344. if (string_matches(argv[1], bench_asym_opt[i].str)) {
  4345. bench_asym_algs |= bench_asym_opt[i].val;
  4346. bench_all = 0;
  4347. optMatched = 1;
  4348. }
  4349. }
  4350. /* Other known cryptographic algorithms */
  4351. for (i=0; !optMatched && bench_other_opt[i].str != NULL; i++) {
  4352. if (string_matches(argv[1], bench_other_opt[i].str)) {
  4353. bench_other_algs |= bench_other_opt[i].val;
  4354. bench_all = 0;
  4355. optMatched = 1;
  4356. }
  4357. }
  4358. #endif
  4359. if (!optMatched) {
  4360. printf("Option not recognized: %s\n", argv[1]);
  4361. Usage();
  4362. return 1;
  4363. }
  4364. }
  4365. else {
  4366. /* parse for block size */
  4367. benchmark_configure(atoi(argv[1]));
  4368. }
  4369. argc--;
  4370. argv++;
  4371. }
  4372. #ifdef HAVE_STACK_SIZE
  4373. ret = StackSizeCheck(NULL, benchmark_test);
  4374. #else
  4375. ret = benchmark_test(NULL);
  4376. #endif
  4377. return ret;
  4378. }
  4379. #endif /* !NO_MAIN_DRIVER */
  4380. #else
  4381. #ifndef NO_MAIN_DRIVER
  4382. int main() { return 0; }
  4383. #endif
  4384. #endif /* !NO_CRYPT_BENCHMARK */