speed.c 167 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875
  1. /*
  2. * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #undef SECONDS
  11. #define SECONDS 3
  12. #define PKEY_SECONDS 10
  13. #define RSA_SECONDS PKEY_SECONDS
  14. #define DSA_SECONDS PKEY_SECONDS
  15. #define ECDSA_SECONDS PKEY_SECONDS
  16. #define ECDH_SECONDS PKEY_SECONDS
  17. #define EdDSA_SECONDS PKEY_SECONDS
  18. #define SM2_SECONDS PKEY_SECONDS
  19. #define FFDH_SECONDS PKEY_SECONDS
  20. #define KEM_SECONDS PKEY_SECONDS
  21. #define SIG_SECONDS PKEY_SECONDS
  22. #define MAX_ALGNAME_SUFFIX 100
  23. /* We need to use some deprecated APIs */
  24. #define OPENSSL_SUPPRESS_DEPRECATED
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <math.h>
  29. #include "apps.h"
  30. #include "progs.h"
  31. #include "internal/nelem.h"
  32. #include "internal/numbers.h"
  33. #include <openssl/crypto.h>
  34. #include <openssl/rand.h>
  35. #include <openssl/err.h>
  36. #include <openssl/evp.h>
  37. #include <openssl/objects.h>
  38. #include <openssl/core_names.h>
  39. #include <openssl/async.h>
  40. #include <openssl/provider.h>
  41. #if !defined(OPENSSL_SYS_MSDOS)
  42. # include <unistd.h>
  43. #endif
  44. #if defined(__TANDEM)
  45. # if defined(OPENSSL_TANDEM_FLOSS)
  46. # include <floss.h(floss_fork)>
  47. # endif
  48. #endif
  49. #if defined(_WIN32)
  50. # include <windows.h>
  51. /*
  52. * While VirtualLock is available under the app partition (e.g. UWP),
  53. * the headers do not define the API. Define it ourselves instead.
  54. */
  55. WINBASEAPI
  56. BOOL
  57. WINAPI
  58. VirtualLock(
  59. _In_ LPVOID lpAddress,
  60. _In_ SIZE_T dwSize
  61. );
  62. #endif
  63. #if defined(OPENSSL_SYS_LINUX)
  64. # include <sys/mman.h>
  65. #endif
  66. #include <openssl/bn.h>
  67. #include <openssl/rsa.h>
  68. #include "./testrsa.h"
  69. #ifndef OPENSSL_NO_DH
  70. # include <openssl/dh.h>
  71. #endif
  72. #include <openssl/x509.h>
  73. #include <openssl/dsa.h>
  74. #include "./testdsa.h"
  75. #include <openssl/modes.h>
  76. #ifndef HAVE_FORK
  77. # if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_VXWORKS)
  78. # define HAVE_FORK 0
  79. # else
  80. # define HAVE_FORK 1
  81. # include <sys/wait.h>
  82. # endif
  83. #endif
  84. #if HAVE_FORK
  85. # undef NO_FORK
  86. #else
  87. # define NO_FORK
  88. #endif
  89. #define MAX_MISALIGNMENT 63
  90. #define MAX_ECDH_SIZE 256
  91. #define MISALIGN 64
  92. #define MAX_FFDH_SIZE 1024
  93. #ifndef RSA_DEFAULT_PRIME_NUM
  94. # define RSA_DEFAULT_PRIME_NUM 2
  95. #endif
  96. typedef struct openssl_speed_sec_st {
  97. int sym;
  98. int rsa;
  99. int dsa;
  100. int ecdsa;
  101. int ecdh;
  102. int eddsa;
  103. int sm2;
  104. int ffdh;
  105. int kem;
  106. int sig;
  107. } openssl_speed_sec_t;
  108. static volatile int run = 0;
  109. static int mr = 0; /* machine-readeable output format to merge fork results */
  110. static int usertime = 1;
  111. static double Time_F(int s);
  112. static void print_message(const char *s, int length, int tm);
  113. static void pkey_print_message(const char *str, const char *str2,
  114. unsigned int bits, int sec);
  115. static void kskey_print_message(const char *str, const char *str2, int tm);
  116. static void print_result(int alg, int run_no, int count, double time_used);
  117. #ifndef NO_FORK
  118. static int do_multi(int multi, int size_num);
  119. #endif
  120. static int domlock = 0;
  121. static const int lengths_list[] = {
  122. 16, 64, 256, 1024, 8 * 1024, 16 * 1024
  123. };
  124. #define SIZE_NUM OSSL_NELEM(lengths_list)
  125. static const int *lengths = lengths_list;
  126. static const int aead_lengths_list[] = {
  127. 2, 31, 136, 1024, 8 * 1024, 16 * 1024
  128. };
  129. #define START 0
  130. #define STOP 1
  131. #ifdef SIGALRM
  132. static void alarmed(ossl_unused int sig)
  133. {
  134. signal(SIGALRM, alarmed);
  135. run = 0;
  136. }
  137. static double Time_F(int s)
  138. {
  139. double ret = app_tminterval(s, usertime);
  140. if (s == STOP)
  141. alarm(0);
  142. return ret;
  143. }
  144. #elif defined(_WIN32)
  145. # define SIGALRM -1
  146. static unsigned int lapse;
  147. static volatile unsigned int schlock;
  148. static void alarm_win32(unsigned int secs)
  149. {
  150. lapse = secs * 1000;
  151. }
  152. # define alarm alarm_win32
  153. static DWORD WINAPI sleepy(VOID * arg)
  154. {
  155. schlock = 1;
  156. Sleep(lapse);
  157. run = 0;
  158. return 0;
  159. }
  160. static double Time_F(int s)
  161. {
  162. double ret;
  163. static HANDLE thr;
  164. if (s == START) {
  165. schlock = 0;
  166. thr = CreateThread(NULL, 4096, sleepy, NULL, 0, NULL);
  167. if (thr == NULL) {
  168. DWORD err = GetLastError();
  169. BIO_printf(bio_err, "unable to CreateThread (%lu)", err);
  170. ExitProcess(err);
  171. }
  172. while (!schlock)
  173. Sleep(0); /* scheduler spinlock */
  174. ret = app_tminterval(s, usertime);
  175. } else {
  176. ret = app_tminterval(s, usertime);
  177. if (run)
  178. TerminateThread(thr, 0);
  179. CloseHandle(thr);
  180. }
  181. return ret;
  182. }
  183. #else
  184. # error "SIGALRM not defined and the platform is not Windows"
  185. #endif
  186. static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
  187. const openssl_speed_sec_t *seconds);
  188. static int opt_found(const char *name, unsigned int *result,
  189. const OPT_PAIR pairs[], unsigned int nbelem)
  190. {
  191. unsigned int idx;
  192. for (idx = 0; idx < nbelem; ++idx, pairs++)
  193. if (strcmp(name, pairs->name) == 0) {
  194. *result = pairs->retval;
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. #define opt_found(value, pairs, result)\
  200. opt_found(value, result, pairs, OSSL_NELEM(pairs))
  201. typedef enum OPTION_choice {
  202. OPT_COMMON,
  203. OPT_ELAPSED, OPT_EVP, OPT_HMAC, OPT_DECRYPT, OPT_ENGINE, OPT_MULTI,
  204. OPT_MR, OPT_MB, OPT_MISALIGN, OPT_ASYNCJOBS, OPT_R_ENUM, OPT_PROV_ENUM, OPT_CONFIG,
  205. OPT_PRIMES, OPT_SECONDS, OPT_BYTES, OPT_AEAD, OPT_CMAC, OPT_MLOCK, OPT_KEM, OPT_SIG
  206. } OPTION_CHOICE;
  207. const OPTIONS speed_options[] = {
  208. {OPT_HELP_STR, 1, '-',
  209. "Usage: %s [options] [algorithm...]\n"
  210. "All +int options consider prefix '0' as base-8 input, "
  211. "prefix '0x'/'0X' as base-16 input.\n"
  212. },
  213. OPT_SECTION("General"),
  214. {"help", OPT_HELP, '-', "Display this summary"},
  215. {"mb", OPT_MB, '-',
  216. "Enable (tls1>=1) multi-block mode on EVP-named cipher"},
  217. {"mr", OPT_MR, '-', "Produce machine readable output"},
  218. #ifndef NO_FORK
  219. {"multi", OPT_MULTI, 'p', "Run benchmarks in parallel"},
  220. #endif
  221. #ifndef OPENSSL_NO_ASYNC
  222. {"async_jobs", OPT_ASYNCJOBS, 'p',
  223. "Enable async mode and start specified number of jobs"},
  224. #endif
  225. #ifndef OPENSSL_NO_ENGINE
  226. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  227. #endif
  228. {"primes", OPT_PRIMES, 'p', "Specify number of primes (for RSA only)"},
  229. {"mlock", OPT_MLOCK, '-', "Lock memory for better result determinism"},
  230. OPT_CONFIG_OPTION,
  231. OPT_SECTION("Selection"),
  232. {"evp", OPT_EVP, 's', "Use EVP-named cipher or digest"},
  233. {"hmac", OPT_HMAC, 's', "HMAC using EVP-named digest"},
  234. {"cmac", OPT_CMAC, 's', "CMAC using EVP-named cipher"},
  235. {"decrypt", OPT_DECRYPT, '-',
  236. "Time decryption instead of encryption (only EVP)"},
  237. {"aead", OPT_AEAD, '-',
  238. "Benchmark EVP-named AEAD cipher in TLS-like sequence"},
  239. {"kem-algorithms", OPT_KEM, '-',
  240. "Benchmark KEM algorithms"},
  241. {"signature-algorithms", OPT_SIG, '-',
  242. "Benchmark signature algorithms"},
  243. OPT_SECTION("Timing"),
  244. {"elapsed", OPT_ELAPSED, '-',
  245. "Use wall-clock time instead of CPU user time as divisor"},
  246. {"seconds", OPT_SECONDS, 'p',
  247. "Run benchmarks for specified amount of seconds"},
  248. {"bytes", OPT_BYTES, 'p',
  249. "Run [non-PKI] benchmarks on custom-sized buffer"},
  250. {"misalign", OPT_MISALIGN, 'p',
  251. "Use specified offset to mis-align buffers"},
  252. OPT_R_OPTIONS,
  253. OPT_PROV_OPTIONS,
  254. OPT_PARAMETERS(),
  255. {"algorithm", 0, 0, "Algorithm(s) to test (optional; otherwise tests all)"},
  256. {NULL}
  257. };
  258. enum {
  259. D_MD2, D_MDC2, D_MD4, D_MD5, D_SHA1, D_RMD160,
  260. D_SHA256, D_SHA512, D_WHIRLPOOL, D_HMAC,
  261. D_CBC_DES, D_EDE3_DES, D_RC4, D_CBC_IDEA, D_CBC_SEED,
  262. D_CBC_RC2, D_CBC_RC5, D_CBC_BF, D_CBC_CAST,
  263. D_CBC_128_AES, D_CBC_192_AES, D_CBC_256_AES,
  264. D_CBC_128_CML, D_CBC_192_CML, D_CBC_256_CML,
  265. D_EVP, D_GHASH, D_RAND, D_EVP_CMAC, D_KMAC128, D_KMAC256,
  266. ALGOR_NUM
  267. };
  268. /* name of algorithms to test. MUST BE KEEP IN SYNC with above enum ! */
  269. static const char *names[ALGOR_NUM] = {
  270. "md2", "mdc2", "md4", "md5", "sha1", "rmd160",
  271. "sha256", "sha512", "whirlpool", "hmac(sha256)",
  272. "des-cbc", "des-ede3", "rc4", "idea-cbc", "seed-cbc",
  273. "rc2-cbc", "rc5-cbc", "blowfish", "cast-cbc",
  274. "aes-128-cbc", "aes-192-cbc", "aes-256-cbc",
  275. "camellia-128-cbc", "camellia-192-cbc", "camellia-256-cbc",
  276. "evp", "ghash", "rand", "cmac", "kmac128", "kmac256"
  277. };
  278. /* list of configured algorithm (remaining), with some few alias */
  279. static const OPT_PAIR doit_choices[] = {
  280. {"md2", D_MD2},
  281. {"mdc2", D_MDC2},
  282. {"md4", D_MD4},
  283. {"md5", D_MD5},
  284. {"hmac", D_HMAC},
  285. {"sha1", D_SHA1},
  286. {"sha256", D_SHA256},
  287. {"sha512", D_SHA512},
  288. {"whirlpool", D_WHIRLPOOL},
  289. {"ripemd", D_RMD160},
  290. {"rmd160", D_RMD160},
  291. {"ripemd160", D_RMD160},
  292. {"rc4", D_RC4},
  293. {"des-cbc", D_CBC_DES},
  294. {"des-ede3", D_EDE3_DES},
  295. {"aes-128-cbc", D_CBC_128_AES},
  296. {"aes-192-cbc", D_CBC_192_AES},
  297. {"aes-256-cbc", D_CBC_256_AES},
  298. {"camellia-128-cbc", D_CBC_128_CML},
  299. {"camellia-192-cbc", D_CBC_192_CML},
  300. {"camellia-256-cbc", D_CBC_256_CML},
  301. {"rc2-cbc", D_CBC_RC2},
  302. {"rc2", D_CBC_RC2},
  303. {"rc5-cbc", D_CBC_RC5},
  304. {"rc5", D_CBC_RC5},
  305. {"idea-cbc", D_CBC_IDEA},
  306. {"idea", D_CBC_IDEA},
  307. {"seed-cbc", D_CBC_SEED},
  308. {"seed", D_CBC_SEED},
  309. {"bf-cbc", D_CBC_BF},
  310. {"blowfish", D_CBC_BF},
  311. {"bf", D_CBC_BF},
  312. {"cast-cbc", D_CBC_CAST},
  313. {"cast", D_CBC_CAST},
  314. {"cast5", D_CBC_CAST},
  315. {"ghash", D_GHASH},
  316. {"rand", D_RAND},
  317. {"kmac128", D_KMAC128},
  318. {"kmac256", D_KMAC256},
  319. };
  320. static double results[ALGOR_NUM][SIZE_NUM];
  321. enum { R_DSA_1024, R_DSA_2048, DSA_NUM };
  322. static const OPT_PAIR dsa_choices[DSA_NUM] = {
  323. {"dsa1024", R_DSA_1024},
  324. {"dsa2048", R_DSA_2048}
  325. };
  326. static double dsa_results[DSA_NUM][2]; /* 2 ops: sign then verify */
  327. enum {
  328. R_RSA_512, R_RSA_1024, R_RSA_2048, R_RSA_3072, R_RSA_4096, R_RSA_7680,
  329. R_RSA_15360, RSA_NUM
  330. };
  331. static const OPT_PAIR rsa_choices[RSA_NUM] = {
  332. {"rsa512", R_RSA_512},
  333. {"rsa1024", R_RSA_1024},
  334. {"rsa2048", R_RSA_2048},
  335. {"rsa3072", R_RSA_3072},
  336. {"rsa4096", R_RSA_4096},
  337. {"rsa7680", R_RSA_7680},
  338. {"rsa15360", R_RSA_15360}
  339. };
  340. static double rsa_results[RSA_NUM][4]; /* 4 ops: sign, verify, encrypt, decrypt */
  341. #ifndef OPENSSL_NO_DH
  342. enum ff_params_t {
  343. R_FFDH_2048, R_FFDH_3072, R_FFDH_4096, R_FFDH_6144, R_FFDH_8192, FFDH_NUM
  344. };
  345. static const OPT_PAIR ffdh_choices[FFDH_NUM] = {
  346. {"ffdh2048", R_FFDH_2048},
  347. {"ffdh3072", R_FFDH_3072},
  348. {"ffdh4096", R_FFDH_4096},
  349. {"ffdh6144", R_FFDH_6144},
  350. {"ffdh8192", R_FFDH_8192},
  351. };
  352. static double ffdh_results[FFDH_NUM][1]; /* 1 op: derivation */
  353. #endif /* OPENSSL_NO_DH */
  354. enum ec_curves_t {
  355. R_EC_P160, R_EC_P192, R_EC_P224, R_EC_P256, R_EC_P384, R_EC_P521,
  356. #ifndef OPENSSL_NO_EC2M
  357. R_EC_K163, R_EC_K233, R_EC_K283, R_EC_K409, R_EC_K571,
  358. R_EC_B163, R_EC_B233, R_EC_B283, R_EC_B409, R_EC_B571,
  359. #endif
  360. R_EC_BRP256R1, R_EC_BRP256T1, R_EC_BRP384R1, R_EC_BRP384T1,
  361. R_EC_BRP512R1, R_EC_BRP512T1, ECDSA_NUM
  362. };
  363. /* list of ecdsa curves */
  364. static const OPT_PAIR ecdsa_choices[ECDSA_NUM] = {
  365. {"ecdsap160", R_EC_P160},
  366. {"ecdsap192", R_EC_P192},
  367. {"ecdsap224", R_EC_P224},
  368. {"ecdsap256", R_EC_P256},
  369. {"ecdsap384", R_EC_P384},
  370. {"ecdsap521", R_EC_P521},
  371. #ifndef OPENSSL_NO_EC2M
  372. {"ecdsak163", R_EC_K163},
  373. {"ecdsak233", R_EC_K233},
  374. {"ecdsak283", R_EC_K283},
  375. {"ecdsak409", R_EC_K409},
  376. {"ecdsak571", R_EC_K571},
  377. {"ecdsab163", R_EC_B163},
  378. {"ecdsab233", R_EC_B233},
  379. {"ecdsab283", R_EC_B283},
  380. {"ecdsab409", R_EC_B409},
  381. {"ecdsab571", R_EC_B571},
  382. #endif
  383. {"ecdsabrp256r1", R_EC_BRP256R1},
  384. {"ecdsabrp256t1", R_EC_BRP256T1},
  385. {"ecdsabrp384r1", R_EC_BRP384R1},
  386. {"ecdsabrp384t1", R_EC_BRP384T1},
  387. {"ecdsabrp512r1", R_EC_BRP512R1},
  388. {"ecdsabrp512t1", R_EC_BRP512T1}
  389. };
  390. enum {
  391. #ifndef OPENSSL_NO_ECX
  392. R_EC_X25519 = ECDSA_NUM, R_EC_X448, EC_NUM
  393. #else
  394. EC_NUM = ECDSA_NUM
  395. #endif
  396. };
  397. /* list of ecdh curves, extension of |ecdsa_choices| list above */
  398. static const OPT_PAIR ecdh_choices[EC_NUM] = {
  399. {"ecdhp160", R_EC_P160},
  400. {"ecdhp192", R_EC_P192},
  401. {"ecdhp224", R_EC_P224},
  402. {"ecdhp256", R_EC_P256},
  403. {"ecdhp384", R_EC_P384},
  404. {"ecdhp521", R_EC_P521},
  405. #ifndef OPENSSL_NO_EC2M
  406. {"ecdhk163", R_EC_K163},
  407. {"ecdhk233", R_EC_K233},
  408. {"ecdhk283", R_EC_K283},
  409. {"ecdhk409", R_EC_K409},
  410. {"ecdhk571", R_EC_K571},
  411. {"ecdhb163", R_EC_B163},
  412. {"ecdhb233", R_EC_B233},
  413. {"ecdhb283", R_EC_B283},
  414. {"ecdhb409", R_EC_B409},
  415. {"ecdhb571", R_EC_B571},
  416. #endif
  417. {"ecdhbrp256r1", R_EC_BRP256R1},
  418. {"ecdhbrp256t1", R_EC_BRP256T1},
  419. {"ecdhbrp384r1", R_EC_BRP384R1},
  420. {"ecdhbrp384t1", R_EC_BRP384T1},
  421. {"ecdhbrp512r1", R_EC_BRP512R1},
  422. {"ecdhbrp512t1", R_EC_BRP512T1},
  423. #ifndef OPENSSL_NO_ECX
  424. {"ecdhx25519", R_EC_X25519},
  425. {"ecdhx448", R_EC_X448}
  426. #endif
  427. };
  428. static double ecdh_results[EC_NUM][1]; /* 1 op: derivation */
  429. static double ecdsa_results[ECDSA_NUM][2]; /* 2 ops: sign then verify */
  430. #ifndef OPENSSL_NO_ECX
  431. enum { R_EC_Ed25519, R_EC_Ed448, EdDSA_NUM };
  432. static const OPT_PAIR eddsa_choices[EdDSA_NUM] = {
  433. {"ed25519", R_EC_Ed25519},
  434. {"ed448", R_EC_Ed448}
  435. };
  436. static double eddsa_results[EdDSA_NUM][2]; /* 2 ops: sign then verify */
  437. #endif /* OPENSSL_NO_ECX */
  438. #ifndef OPENSSL_NO_SM2
  439. enum { R_EC_CURVESM2, SM2_NUM };
  440. static const OPT_PAIR sm2_choices[SM2_NUM] = {
  441. {"curveSM2", R_EC_CURVESM2}
  442. };
  443. # define SM2_ID "TLSv1.3+GM+Cipher+Suite"
  444. # define SM2_ID_LEN sizeof("TLSv1.3+GM+Cipher+Suite") - 1
  445. static double sm2_results[SM2_NUM][2]; /* 2 ops: sign then verify */
  446. #endif /* OPENSSL_NO_SM2 */
  447. #define MAX_KEM_NUM 111
  448. static size_t kems_algs_len = 0;
  449. static char *kems_algname[MAX_KEM_NUM] = { NULL };
  450. static double kems_results[MAX_KEM_NUM][3]; /* keygen, encaps, decaps */
  451. #define MAX_SIG_NUM 111
  452. static size_t sigs_algs_len = 0;
  453. static char *sigs_algname[MAX_SIG_NUM] = { NULL };
  454. static double sigs_results[MAX_SIG_NUM][3]; /* keygen, sign, verify */
  455. #define COND(unused_cond) (run && count < INT_MAX)
  456. #define COUNT(d) (count)
  457. typedef struct loopargs_st {
  458. ASYNC_JOB *inprogress_job;
  459. ASYNC_WAIT_CTX *wait_ctx;
  460. unsigned char *buf;
  461. unsigned char *buf2;
  462. unsigned char *buf_malloc;
  463. unsigned char *buf2_malloc;
  464. unsigned char *key;
  465. size_t buflen;
  466. size_t sigsize;
  467. size_t encsize;
  468. EVP_PKEY_CTX *rsa_sign_ctx[RSA_NUM];
  469. EVP_PKEY_CTX *rsa_verify_ctx[RSA_NUM];
  470. EVP_PKEY_CTX *rsa_encrypt_ctx[RSA_NUM];
  471. EVP_PKEY_CTX *rsa_decrypt_ctx[RSA_NUM];
  472. EVP_PKEY_CTX *dsa_sign_ctx[DSA_NUM];
  473. EVP_PKEY_CTX *dsa_verify_ctx[DSA_NUM];
  474. EVP_PKEY_CTX *ecdsa_sign_ctx[ECDSA_NUM];
  475. EVP_PKEY_CTX *ecdsa_verify_ctx[ECDSA_NUM];
  476. EVP_PKEY_CTX *ecdh_ctx[EC_NUM];
  477. #ifndef OPENSSL_NO_ECX
  478. EVP_MD_CTX *eddsa_ctx[EdDSA_NUM];
  479. EVP_MD_CTX *eddsa_ctx2[EdDSA_NUM];
  480. #endif /* OPENSSL_NO_ECX */
  481. #ifndef OPENSSL_NO_SM2
  482. EVP_MD_CTX *sm2_ctx[SM2_NUM];
  483. EVP_MD_CTX *sm2_vfy_ctx[SM2_NUM];
  484. EVP_PKEY *sm2_pkey[SM2_NUM];
  485. #endif
  486. unsigned char *secret_a;
  487. unsigned char *secret_b;
  488. size_t outlen[EC_NUM];
  489. #ifndef OPENSSL_NO_DH
  490. EVP_PKEY_CTX *ffdh_ctx[FFDH_NUM];
  491. unsigned char *secret_ff_a;
  492. unsigned char *secret_ff_b;
  493. #endif
  494. EVP_CIPHER_CTX *ctx;
  495. EVP_MAC_CTX *mctx;
  496. EVP_PKEY_CTX *kem_gen_ctx[MAX_KEM_NUM];
  497. EVP_PKEY_CTX *kem_encaps_ctx[MAX_KEM_NUM];
  498. EVP_PKEY_CTX *kem_decaps_ctx[MAX_KEM_NUM];
  499. size_t kem_out_len[MAX_KEM_NUM];
  500. size_t kem_secret_len[MAX_KEM_NUM];
  501. unsigned char *kem_out[MAX_KEM_NUM];
  502. unsigned char *kem_send_secret[MAX_KEM_NUM];
  503. unsigned char *kem_rcv_secret[MAX_KEM_NUM];
  504. EVP_PKEY_CTX *sig_gen_ctx[MAX_KEM_NUM];
  505. EVP_PKEY_CTX *sig_sign_ctx[MAX_KEM_NUM];
  506. EVP_PKEY_CTX *sig_verify_ctx[MAX_KEM_NUM];
  507. size_t sig_max_sig_len[MAX_KEM_NUM];
  508. size_t sig_act_sig_len[MAX_KEM_NUM];
  509. unsigned char *sig_sig[MAX_KEM_NUM];
  510. } loopargs_t;
  511. static int run_benchmark(int async_jobs, int (*loop_function) (void *),
  512. loopargs_t *loopargs);
  513. static unsigned int testnum;
  514. static char *evp_mac_mdname = "sha256";
  515. static char *evp_hmac_name = NULL;
  516. static const char *evp_md_name = NULL;
  517. static char *evp_mac_ciphername = "aes-128-cbc";
  518. static char *evp_cmac_name = NULL;
  519. static int have_md(const char *name)
  520. {
  521. int ret = 0;
  522. EVP_MD *md = NULL;
  523. if (opt_md_silent(name, &md)) {
  524. EVP_MD_CTX *ctx = EVP_MD_CTX_new();
  525. if (ctx != NULL && EVP_DigestInit(ctx, md) > 0)
  526. ret = 1;
  527. EVP_MD_CTX_free(ctx);
  528. EVP_MD_free(md);
  529. }
  530. return ret;
  531. }
  532. static int have_cipher(const char *name)
  533. {
  534. int ret = 0;
  535. EVP_CIPHER *cipher = NULL;
  536. if (opt_cipher_silent(name, &cipher)) {
  537. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  538. if (ctx != NULL
  539. && EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1) > 0)
  540. ret = 1;
  541. EVP_CIPHER_CTX_free(ctx);
  542. EVP_CIPHER_free(cipher);
  543. }
  544. return ret;
  545. }
  546. static int EVP_Digest_loop(const char *mdname, ossl_unused int algindex, void *args)
  547. {
  548. loopargs_t *tempargs = *(loopargs_t **) args;
  549. unsigned char *buf = tempargs->buf;
  550. unsigned char digest[EVP_MAX_MD_SIZE];
  551. int count;
  552. EVP_MD *md = NULL;
  553. if (!opt_md_silent(mdname, &md))
  554. return -1;
  555. for (count = 0; COND(c[algindex][testnum]); count++) {
  556. if (!EVP_Digest(buf, (size_t)lengths[testnum], digest, NULL, md,
  557. NULL)) {
  558. count = -1;
  559. break;
  560. }
  561. }
  562. EVP_MD_free(md);
  563. return count;
  564. }
  565. static int EVP_Digest_md_loop(void *args)
  566. {
  567. return EVP_Digest_loop(evp_md_name, D_EVP, args);
  568. }
  569. static int EVP_Digest_MD2_loop(void *args)
  570. {
  571. return EVP_Digest_loop("md2", D_MD2, args);
  572. }
  573. static int EVP_Digest_MDC2_loop(void *args)
  574. {
  575. return EVP_Digest_loop("mdc2", D_MDC2, args);
  576. }
  577. static int EVP_Digest_MD4_loop(void *args)
  578. {
  579. return EVP_Digest_loop("md4", D_MD4, args);
  580. }
  581. static int MD5_loop(void *args)
  582. {
  583. return EVP_Digest_loop("md5", D_MD5, args);
  584. }
  585. static int mac_setup(const char *name,
  586. EVP_MAC **mac, OSSL_PARAM params[],
  587. loopargs_t *loopargs, unsigned int loopargs_len)
  588. {
  589. unsigned int i;
  590. *mac = EVP_MAC_fetch(app_get0_libctx(), name, app_get0_propq());
  591. if (*mac == NULL)
  592. return 0;
  593. for (i = 0; i < loopargs_len; i++) {
  594. loopargs[i].mctx = EVP_MAC_CTX_new(*mac);
  595. if (loopargs[i].mctx == NULL)
  596. return 0;
  597. if (!EVP_MAC_CTX_set_params(loopargs[i].mctx, params))
  598. return 0;
  599. }
  600. return 1;
  601. }
  602. static void mac_teardown(EVP_MAC **mac,
  603. loopargs_t *loopargs, unsigned int loopargs_len)
  604. {
  605. unsigned int i;
  606. for (i = 0; i < loopargs_len; i++)
  607. EVP_MAC_CTX_free(loopargs[i].mctx);
  608. EVP_MAC_free(*mac);
  609. *mac = NULL;
  610. return;
  611. }
  612. static int EVP_MAC_loop(ossl_unused int algindex, void *args)
  613. {
  614. loopargs_t *tempargs = *(loopargs_t **) args;
  615. unsigned char *buf = tempargs->buf;
  616. EVP_MAC_CTX *mctx = tempargs->mctx;
  617. unsigned char mac[EVP_MAX_MD_SIZE];
  618. int count;
  619. for (count = 0; COND(c[algindex][testnum]); count++) {
  620. size_t outl;
  621. if (!EVP_MAC_init(mctx, NULL, 0, NULL)
  622. || !EVP_MAC_update(mctx, buf, lengths[testnum])
  623. || !EVP_MAC_final(mctx, mac, &outl, sizeof(mac)))
  624. return -1;
  625. }
  626. return count;
  627. }
  628. static int HMAC_loop(void *args)
  629. {
  630. return EVP_MAC_loop(D_HMAC, args);
  631. }
  632. static int CMAC_loop(void *args)
  633. {
  634. return EVP_MAC_loop(D_EVP_CMAC, args);
  635. }
  636. static int KMAC128_loop(void *args)
  637. {
  638. return EVP_MAC_loop(D_KMAC128, args);
  639. }
  640. static int KMAC256_loop(void *args)
  641. {
  642. return EVP_MAC_loop(D_KMAC256, args);
  643. }
  644. static int SHA1_loop(void *args)
  645. {
  646. return EVP_Digest_loop("sha1", D_SHA1, args);
  647. }
  648. static int SHA256_loop(void *args)
  649. {
  650. return EVP_Digest_loop("sha256", D_SHA256, args);
  651. }
  652. static int SHA512_loop(void *args)
  653. {
  654. return EVP_Digest_loop("sha512", D_SHA512, args);
  655. }
  656. static int WHIRLPOOL_loop(void *args)
  657. {
  658. return EVP_Digest_loop("whirlpool", D_WHIRLPOOL, args);
  659. }
  660. static int EVP_Digest_RMD160_loop(void *args)
  661. {
  662. return EVP_Digest_loop("ripemd160", D_RMD160, args);
  663. }
  664. static int algindex;
  665. static int EVP_Cipher_loop(void *args)
  666. {
  667. loopargs_t *tempargs = *(loopargs_t **) args;
  668. unsigned char *buf = tempargs->buf;
  669. int count;
  670. if (tempargs->ctx == NULL)
  671. return -1;
  672. for (count = 0; COND(c[algindex][testnum]); count++)
  673. if (EVP_Cipher(tempargs->ctx, buf, buf, (size_t)lengths[testnum]) <= 0)
  674. return -1;
  675. return count;
  676. }
  677. static int GHASH_loop(void *args)
  678. {
  679. loopargs_t *tempargs = *(loopargs_t **) args;
  680. unsigned char *buf = tempargs->buf;
  681. EVP_MAC_CTX *mctx = tempargs->mctx;
  682. int count;
  683. /* just do the update in the loop to be comparable with 1.1.1 */
  684. for (count = 0; COND(c[D_GHASH][testnum]); count++) {
  685. if (!EVP_MAC_update(mctx, buf, lengths[testnum]))
  686. return -1;
  687. }
  688. return count;
  689. }
  690. #define MAX_BLOCK_SIZE 128
  691. static unsigned char iv[2 * MAX_BLOCK_SIZE / 8];
  692. static EVP_CIPHER_CTX *init_evp_cipher_ctx(const char *ciphername,
  693. const unsigned char *key,
  694. int keylen)
  695. {
  696. EVP_CIPHER_CTX *ctx = NULL;
  697. EVP_CIPHER *cipher = NULL;
  698. if (!opt_cipher_silent(ciphername, &cipher))
  699. return NULL;
  700. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  701. goto end;
  702. if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, 1)) {
  703. EVP_CIPHER_CTX_free(ctx);
  704. ctx = NULL;
  705. goto end;
  706. }
  707. if (EVP_CIPHER_CTX_set_key_length(ctx, keylen) <= 0) {
  708. EVP_CIPHER_CTX_free(ctx);
  709. ctx = NULL;
  710. goto end;
  711. }
  712. if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1)) {
  713. EVP_CIPHER_CTX_free(ctx);
  714. ctx = NULL;
  715. goto end;
  716. }
  717. end:
  718. EVP_CIPHER_free(cipher);
  719. return ctx;
  720. }
  721. static int RAND_bytes_loop(void *args)
  722. {
  723. loopargs_t *tempargs = *(loopargs_t **) args;
  724. unsigned char *buf = tempargs->buf;
  725. int count;
  726. for (count = 0; COND(c[D_RAND][testnum]); count++)
  727. RAND_bytes(buf, lengths[testnum]);
  728. return count;
  729. }
  730. static int decrypt = 0;
  731. static int EVP_Update_loop(void *args)
  732. {
  733. loopargs_t *tempargs = *(loopargs_t **) args;
  734. unsigned char *buf = tempargs->buf;
  735. EVP_CIPHER_CTX *ctx = tempargs->ctx;
  736. int outl, count, rc;
  737. if (decrypt) {
  738. for (count = 0; COND(c[D_EVP][testnum]); count++) {
  739. rc = EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
  740. if (rc != 1) {
  741. /* reset iv in case of counter overflow */
  742. rc = EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
  743. }
  744. }
  745. } else {
  746. for (count = 0; COND(c[D_EVP][testnum]); count++) {
  747. rc = EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
  748. if (rc != 1) {
  749. /* reset iv in case of counter overflow */
  750. rc = EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
  751. }
  752. }
  753. }
  754. if (decrypt)
  755. rc = EVP_DecryptFinal_ex(ctx, buf, &outl);
  756. else
  757. rc = EVP_EncryptFinal_ex(ctx, buf, &outl);
  758. if (rc == 0)
  759. BIO_printf(bio_err, "Error finalizing cipher loop\n");
  760. return count;
  761. }
  762. /*
  763. * CCM does not support streaming. For the purpose of performance measurement,
  764. * each message is encrypted using the same (key,iv)-pair. Do not use this
  765. * code in your application.
  766. */
  767. static int EVP_Update_loop_ccm(void *args)
  768. {
  769. loopargs_t *tempargs = *(loopargs_t **) args;
  770. unsigned char *buf = tempargs->buf;
  771. EVP_CIPHER_CTX *ctx = tempargs->ctx;
  772. int outl, count, realcount = 0, final;
  773. unsigned char tag[12];
  774. if (decrypt) {
  775. for (count = 0; COND(c[D_EVP][testnum]); count++) {
  776. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag),
  777. tag) > 0
  778. /* reset iv */
  779. && EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) > 0
  780. /* counter is reset on every update */
  781. && EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]) > 0)
  782. realcount++;
  783. }
  784. } else {
  785. for (count = 0; COND(c[D_EVP][testnum]); count++) {
  786. /* restore iv length field */
  787. if (EVP_EncryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]) > 0
  788. /* counter is reset on every update */
  789. && EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]) > 0)
  790. realcount++;
  791. }
  792. }
  793. if (decrypt)
  794. final = EVP_DecryptFinal_ex(ctx, buf, &outl);
  795. else
  796. final = EVP_EncryptFinal_ex(ctx, buf, &outl);
  797. if (final == 0)
  798. BIO_printf(bio_err, "Error finalizing ccm loop\n");
  799. return realcount;
  800. }
  801. /*
  802. * To make AEAD benchmarking more relevant perform TLS-like operations,
  803. * 13-byte AAD followed by payload. But don't use TLS-formatted AAD, as
  804. * payload length is not actually limited by 16KB...
  805. */
  806. static int EVP_Update_loop_aead(void *args)
  807. {
  808. loopargs_t *tempargs = *(loopargs_t **) args;
  809. unsigned char *buf = tempargs->buf;
  810. EVP_CIPHER_CTX *ctx = tempargs->ctx;
  811. int outl, count, realcount = 0;
  812. unsigned char aad[13] = { 0xcc };
  813. unsigned char faketag[16] = { 0xcc };
  814. if (decrypt) {
  815. for (count = 0; COND(c[D_EVP][testnum]); count++) {
  816. if (EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) > 0
  817. && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  818. sizeof(faketag), faketag) > 0
  819. && EVP_DecryptUpdate(ctx, NULL, &outl, aad, sizeof(aad)) > 0
  820. && EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]) > 0
  821. && EVP_DecryptFinal_ex(ctx, buf + outl, &outl) >0)
  822. realcount++;
  823. }
  824. } else {
  825. for (count = 0; COND(c[D_EVP][testnum]); count++) {
  826. if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) > 0
  827. && EVP_EncryptUpdate(ctx, NULL, &outl, aad, sizeof(aad)) > 0
  828. && EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]) > 0
  829. && EVP_EncryptFinal_ex(ctx, buf + outl, &outl) > 0)
  830. realcount++;
  831. }
  832. }
  833. return realcount;
  834. }
  835. static int RSA_sign_loop(void *args)
  836. {
  837. loopargs_t *tempargs = *(loopargs_t **) args;
  838. unsigned char *buf = tempargs->buf;
  839. unsigned char *buf2 = tempargs->buf2;
  840. size_t *rsa_num = &tempargs->sigsize;
  841. EVP_PKEY_CTX **rsa_sign_ctx = tempargs->rsa_sign_ctx;
  842. int ret, count;
  843. for (count = 0; COND(rsa_c[testnum][0]); count++) {
  844. *rsa_num = tempargs->buflen;
  845. ret = EVP_PKEY_sign(rsa_sign_ctx[testnum], buf2, rsa_num, buf, 36);
  846. if (ret <= 0) {
  847. BIO_printf(bio_err, "RSA sign failure\n");
  848. ERR_print_errors(bio_err);
  849. count = -1;
  850. break;
  851. }
  852. }
  853. return count;
  854. }
  855. static int RSA_verify_loop(void *args)
  856. {
  857. loopargs_t *tempargs = *(loopargs_t **) args;
  858. unsigned char *buf = tempargs->buf;
  859. unsigned char *buf2 = tempargs->buf2;
  860. size_t rsa_num = tempargs->sigsize;
  861. EVP_PKEY_CTX **rsa_verify_ctx = tempargs->rsa_verify_ctx;
  862. int ret, count;
  863. for (count = 0; COND(rsa_c[testnum][1]); count++) {
  864. ret = EVP_PKEY_verify(rsa_verify_ctx[testnum], buf2, rsa_num, buf, 36);
  865. if (ret <= 0) {
  866. BIO_printf(bio_err, "RSA verify failure\n");
  867. ERR_print_errors(bio_err);
  868. count = -1;
  869. break;
  870. }
  871. }
  872. return count;
  873. }
  874. static int RSA_encrypt_loop(void *args)
  875. {
  876. loopargs_t *tempargs = *(loopargs_t **) args;
  877. unsigned char *buf = tempargs->buf;
  878. unsigned char *buf2 = tempargs->buf2;
  879. size_t *rsa_num = &tempargs->encsize;
  880. EVP_PKEY_CTX **rsa_encrypt_ctx = tempargs->rsa_encrypt_ctx;
  881. int ret, count;
  882. for (count = 0; COND(rsa_c[testnum][2]); count++) {
  883. *rsa_num = tempargs->buflen;
  884. ret = EVP_PKEY_encrypt(rsa_encrypt_ctx[testnum], buf2, rsa_num, buf, 36);
  885. if (ret <= 0) {
  886. BIO_printf(bio_err, "RSA encrypt failure\n");
  887. ERR_print_errors(bio_err);
  888. count = -1;
  889. break;
  890. }
  891. }
  892. return count;
  893. }
  894. static int RSA_decrypt_loop(void *args)
  895. {
  896. loopargs_t *tempargs = *(loopargs_t **) args;
  897. unsigned char *buf = tempargs->buf;
  898. unsigned char *buf2 = tempargs->buf2;
  899. size_t rsa_num;
  900. EVP_PKEY_CTX **rsa_decrypt_ctx = tempargs->rsa_decrypt_ctx;
  901. int ret, count;
  902. for (count = 0; COND(rsa_c[testnum][3]); count++) {
  903. rsa_num = tempargs->buflen;
  904. ret = EVP_PKEY_decrypt(rsa_decrypt_ctx[testnum], buf, &rsa_num, buf2, tempargs->encsize);
  905. if (ret <= 0) {
  906. BIO_printf(bio_err, "RSA decrypt failure\n");
  907. ERR_print_errors(bio_err);
  908. count = -1;
  909. break;
  910. }
  911. }
  912. return count;
  913. }
  914. #ifndef OPENSSL_NO_DH
  915. static int FFDH_derive_key_loop(void *args)
  916. {
  917. loopargs_t *tempargs = *(loopargs_t **) args;
  918. EVP_PKEY_CTX *ffdh_ctx = tempargs->ffdh_ctx[testnum];
  919. unsigned char *derived_secret = tempargs->secret_ff_a;
  920. int count;
  921. for (count = 0; COND(ffdh_c[testnum][0]); count++) {
  922. /* outlen can be overwritten with a too small value (no padding used) */
  923. size_t outlen = MAX_FFDH_SIZE;
  924. EVP_PKEY_derive(ffdh_ctx, derived_secret, &outlen);
  925. }
  926. return count;
  927. }
  928. #endif /* OPENSSL_NO_DH */
  929. static int DSA_sign_loop(void *args)
  930. {
  931. loopargs_t *tempargs = *(loopargs_t **) args;
  932. unsigned char *buf = tempargs->buf;
  933. unsigned char *buf2 = tempargs->buf2;
  934. size_t *dsa_num = &tempargs->sigsize;
  935. EVP_PKEY_CTX **dsa_sign_ctx = tempargs->dsa_sign_ctx;
  936. int ret, count;
  937. for (count = 0; COND(dsa_c[testnum][0]); count++) {
  938. *dsa_num = tempargs->buflen;
  939. ret = EVP_PKEY_sign(dsa_sign_ctx[testnum], buf2, dsa_num, buf, 20);
  940. if (ret <= 0) {
  941. BIO_printf(bio_err, "DSA sign failure\n");
  942. ERR_print_errors(bio_err);
  943. count = -1;
  944. break;
  945. }
  946. }
  947. return count;
  948. }
  949. static int DSA_verify_loop(void *args)
  950. {
  951. loopargs_t *tempargs = *(loopargs_t **) args;
  952. unsigned char *buf = tempargs->buf;
  953. unsigned char *buf2 = tempargs->buf2;
  954. size_t dsa_num = tempargs->sigsize;
  955. EVP_PKEY_CTX **dsa_verify_ctx = tempargs->dsa_verify_ctx;
  956. int ret, count;
  957. for (count = 0; COND(dsa_c[testnum][1]); count++) {
  958. ret = EVP_PKEY_verify(dsa_verify_ctx[testnum], buf2, dsa_num, buf, 20);
  959. if (ret <= 0) {
  960. BIO_printf(bio_err, "DSA verify failure\n");
  961. ERR_print_errors(bio_err);
  962. count = -1;
  963. break;
  964. }
  965. }
  966. return count;
  967. }
  968. static int ECDSA_sign_loop(void *args)
  969. {
  970. loopargs_t *tempargs = *(loopargs_t **) args;
  971. unsigned char *buf = tempargs->buf;
  972. unsigned char *buf2 = tempargs->buf2;
  973. size_t *ecdsa_num = &tempargs->sigsize;
  974. EVP_PKEY_CTX **ecdsa_sign_ctx = tempargs->ecdsa_sign_ctx;
  975. int ret, count;
  976. for (count = 0; COND(ecdsa_c[testnum][0]); count++) {
  977. *ecdsa_num = tempargs->buflen;
  978. ret = EVP_PKEY_sign(ecdsa_sign_ctx[testnum], buf2, ecdsa_num, buf, 20);
  979. if (ret <= 0) {
  980. BIO_printf(bio_err, "ECDSA sign failure\n");
  981. ERR_print_errors(bio_err);
  982. count = -1;
  983. break;
  984. }
  985. }
  986. return count;
  987. }
  988. static int ECDSA_verify_loop(void *args)
  989. {
  990. loopargs_t *tempargs = *(loopargs_t **) args;
  991. unsigned char *buf = tempargs->buf;
  992. unsigned char *buf2 = tempargs->buf2;
  993. size_t ecdsa_num = tempargs->sigsize;
  994. EVP_PKEY_CTX **ecdsa_verify_ctx = tempargs->ecdsa_verify_ctx;
  995. int ret, count;
  996. for (count = 0; COND(ecdsa_c[testnum][1]); count++) {
  997. ret = EVP_PKEY_verify(ecdsa_verify_ctx[testnum], buf2, ecdsa_num,
  998. buf, 20);
  999. if (ret <= 0) {
  1000. BIO_printf(bio_err, "ECDSA verify failure\n");
  1001. ERR_print_errors(bio_err);
  1002. count = -1;
  1003. break;
  1004. }
  1005. }
  1006. return count;
  1007. }
  1008. /* ******************************************************************** */
  1009. static int ECDH_EVP_derive_key_loop(void *args)
  1010. {
  1011. loopargs_t *tempargs = *(loopargs_t **) args;
  1012. EVP_PKEY_CTX *ctx = tempargs->ecdh_ctx[testnum];
  1013. unsigned char *derived_secret = tempargs->secret_a;
  1014. int count;
  1015. size_t *outlen = &(tempargs->outlen[testnum]);
  1016. for (count = 0; COND(ecdh_c[testnum][0]); count++)
  1017. EVP_PKEY_derive(ctx, derived_secret, outlen);
  1018. return count;
  1019. }
  1020. #ifndef OPENSSL_NO_ECX
  1021. static int EdDSA_sign_loop(void *args)
  1022. {
  1023. loopargs_t *tempargs = *(loopargs_t **) args;
  1024. unsigned char *buf = tempargs->buf;
  1025. EVP_MD_CTX **edctx = tempargs->eddsa_ctx;
  1026. unsigned char *eddsasig = tempargs->buf2;
  1027. size_t *eddsasigsize = &tempargs->sigsize;
  1028. int ret, count;
  1029. for (count = 0; COND(eddsa_c[testnum][0]); count++) {
  1030. ret = EVP_DigestSignInit(edctx[testnum], NULL, NULL, NULL, NULL);
  1031. if (ret == 0) {
  1032. BIO_printf(bio_err, "EdDSA sign init failure\n");
  1033. ERR_print_errors(bio_err);
  1034. count = -1;
  1035. break;
  1036. }
  1037. ret = EVP_DigestSign(edctx[testnum], eddsasig, eddsasigsize, buf, 20);
  1038. if (ret == 0) {
  1039. BIO_printf(bio_err, "EdDSA sign failure\n");
  1040. ERR_print_errors(bio_err);
  1041. count = -1;
  1042. break;
  1043. }
  1044. }
  1045. return count;
  1046. }
  1047. static int EdDSA_verify_loop(void *args)
  1048. {
  1049. loopargs_t *tempargs = *(loopargs_t **) args;
  1050. unsigned char *buf = tempargs->buf;
  1051. EVP_MD_CTX **edctx = tempargs->eddsa_ctx2;
  1052. unsigned char *eddsasig = tempargs->buf2;
  1053. size_t eddsasigsize = tempargs->sigsize;
  1054. int ret, count;
  1055. for (count = 0; COND(eddsa_c[testnum][1]); count++) {
  1056. ret = EVP_DigestVerifyInit(edctx[testnum], NULL, NULL, NULL, NULL);
  1057. if (ret == 0) {
  1058. BIO_printf(bio_err, "EdDSA verify init failure\n");
  1059. ERR_print_errors(bio_err);
  1060. count = -1;
  1061. break;
  1062. }
  1063. ret = EVP_DigestVerify(edctx[testnum], eddsasig, eddsasigsize, buf, 20);
  1064. if (ret != 1) {
  1065. BIO_printf(bio_err, "EdDSA verify failure\n");
  1066. ERR_print_errors(bio_err);
  1067. count = -1;
  1068. break;
  1069. }
  1070. }
  1071. return count;
  1072. }
  1073. #endif /* OPENSSL_NO_ECX */
  1074. #ifndef OPENSSL_NO_SM2
  1075. static int SM2_sign_loop(void *args)
  1076. {
  1077. loopargs_t *tempargs = *(loopargs_t **) args;
  1078. unsigned char *buf = tempargs->buf;
  1079. EVP_MD_CTX **sm2ctx = tempargs->sm2_ctx;
  1080. unsigned char *sm2sig = tempargs->buf2;
  1081. size_t sm2sigsize;
  1082. int ret, count;
  1083. EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
  1084. const size_t max_size = EVP_PKEY_get_size(sm2_pkey[testnum]);
  1085. for (count = 0; COND(sm2_c[testnum][0]); count++) {
  1086. sm2sigsize = max_size;
  1087. if (!EVP_DigestSignInit(sm2ctx[testnum], NULL, EVP_sm3(),
  1088. NULL, sm2_pkey[testnum])) {
  1089. BIO_printf(bio_err, "SM2 init sign failure\n");
  1090. ERR_print_errors(bio_err);
  1091. count = -1;
  1092. break;
  1093. }
  1094. ret = EVP_DigestSign(sm2ctx[testnum], sm2sig, &sm2sigsize,
  1095. buf, 20);
  1096. if (ret == 0) {
  1097. BIO_printf(bio_err, "SM2 sign failure\n");
  1098. ERR_print_errors(bio_err);
  1099. count = -1;
  1100. break;
  1101. }
  1102. /* update the latest returned size and always use the fixed buffer size */
  1103. tempargs->sigsize = sm2sigsize;
  1104. }
  1105. return count;
  1106. }
  1107. static int SM2_verify_loop(void *args)
  1108. {
  1109. loopargs_t *tempargs = *(loopargs_t **) args;
  1110. unsigned char *buf = tempargs->buf;
  1111. EVP_MD_CTX **sm2ctx = tempargs->sm2_vfy_ctx;
  1112. unsigned char *sm2sig = tempargs->buf2;
  1113. size_t sm2sigsize = tempargs->sigsize;
  1114. int ret, count;
  1115. EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
  1116. for (count = 0; COND(sm2_c[testnum][1]); count++) {
  1117. if (!EVP_DigestVerifyInit(sm2ctx[testnum], NULL, EVP_sm3(),
  1118. NULL, sm2_pkey[testnum])) {
  1119. BIO_printf(bio_err, "SM2 verify init failure\n");
  1120. ERR_print_errors(bio_err);
  1121. count = -1;
  1122. break;
  1123. }
  1124. ret = EVP_DigestVerify(sm2ctx[testnum], sm2sig, sm2sigsize,
  1125. buf, 20);
  1126. if (ret != 1) {
  1127. BIO_printf(bio_err, "SM2 verify failure\n");
  1128. ERR_print_errors(bio_err);
  1129. count = -1;
  1130. break;
  1131. }
  1132. }
  1133. return count;
  1134. }
  1135. #endif /* OPENSSL_NO_SM2 */
  1136. static int KEM_keygen_loop(void *args)
  1137. {
  1138. loopargs_t *tempargs = *(loopargs_t **) args;
  1139. EVP_PKEY_CTX *ctx = tempargs->kem_gen_ctx[testnum];
  1140. EVP_PKEY *pkey = NULL;
  1141. int count;
  1142. for (count = 0; COND(kems_c[testnum][0]); count++) {
  1143. if (EVP_PKEY_keygen(ctx, &pkey) <= 0)
  1144. return -1;
  1145. /*
  1146. * runtime defined to quite some degree by randomness,
  1147. * so performance overhead of _free doesn't impact
  1148. * results significantly. In any case this test is
  1149. * meant to permit relative algorithm performance
  1150. * comparison.
  1151. */
  1152. EVP_PKEY_free(pkey);
  1153. pkey = NULL;
  1154. }
  1155. return count;
  1156. }
  1157. static int KEM_encaps_loop(void *args)
  1158. {
  1159. loopargs_t *tempargs = *(loopargs_t **) args;
  1160. EVP_PKEY_CTX *ctx = tempargs->kem_encaps_ctx[testnum];
  1161. size_t out_len = tempargs->kem_out_len[testnum];
  1162. size_t secret_len = tempargs->kem_secret_len[testnum];
  1163. unsigned char *out = tempargs->kem_out[testnum];
  1164. unsigned char *secret = tempargs->kem_send_secret[testnum];
  1165. int count;
  1166. for (count = 0; COND(kems_c[testnum][1]); count++) {
  1167. if (EVP_PKEY_encapsulate(ctx, out, &out_len, secret, &secret_len) <= 0)
  1168. return -1;
  1169. }
  1170. return count;
  1171. }
  1172. static int KEM_decaps_loop(void *args)
  1173. {
  1174. loopargs_t *tempargs = *(loopargs_t **) args;
  1175. EVP_PKEY_CTX *ctx = tempargs->kem_decaps_ctx[testnum];
  1176. size_t out_len = tempargs->kem_out_len[testnum];
  1177. size_t secret_len = tempargs->kem_secret_len[testnum];
  1178. unsigned char *out = tempargs->kem_out[testnum];
  1179. unsigned char *secret = tempargs->kem_send_secret[testnum];
  1180. int count;
  1181. for (count = 0; COND(kems_c[testnum][2]); count++) {
  1182. if (EVP_PKEY_decapsulate(ctx, secret, &secret_len, out, out_len) <= 0)
  1183. return -1;
  1184. }
  1185. return count;
  1186. }
  1187. static int SIG_keygen_loop(void *args)
  1188. {
  1189. loopargs_t *tempargs = *(loopargs_t **) args;
  1190. EVP_PKEY_CTX *ctx = tempargs->sig_gen_ctx[testnum];
  1191. EVP_PKEY *pkey = NULL;
  1192. int count;
  1193. for (count = 0; COND(kems_c[testnum][0]); count++) {
  1194. EVP_PKEY_keygen(ctx, &pkey);
  1195. /* TBD: How much does free influence runtime? */
  1196. EVP_PKEY_free(pkey);
  1197. pkey = NULL;
  1198. }
  1199. return count;
  1200. }
  1201. static int SIG_sign_loop(void *args)
  1202. {
  1203. loopargs_t *tempargs = *(loopargs_t **) args;
  1204. EVP_PKEY_CTX *ctx = tempargs->sig_sign_ctx[testnum];
  1205. /* be sure to not change stored sig: */
  1206. unsigned char *sig = app_malloc(tempargs->sig_max_sig_len[testnum],
  1207. "sig sign loop");
  1208. unsigned char md[SHA256_DIGEST_LENGTH] = { 0 };
  1209. size_t md_len = SHA256_DIGEST_LENGTH;
  1210. int count;
  1211. for (count = 0; COND(kems_c[testnum][1]); count++) {
  1212. size_t sig_len = tempargs->sig_max_sig_len[testnum];
  1213. int ret = EVP_PKEY_sign(ctx, sig, &sig_len, md, md_len);
  1214. if (ret <= 0) {
  1215. BIO_printf(bio_err, "SIG sign failure at count %d\n", count);
  1216. ERR_print_errors(bio_err);
  1217. count = -1;
  1218. break;
  1219. }
  1220. }
  1221. OPENSSL_free(sig);
  1222. return count;
  1223. }
  1224. static int SIG_verify_loop(void *args)
  1225. {
  1226. loopargs_t *tempargs = *(loopargs_t **) args;
  1227. EVP_PKEY_CTX *ctx = tempargs->sig_verify_ctx[testnum];
  1228. size_t sig_len = tempargs->sig_act_sig_len[testnum];
  1229. unsigned char *sig = tempargs->sig_sig[testnum];
  1230. unsigned char md[SHA256_DIGEST_LENGTH] = { 0 };
  1231. size_t md_len = SHA256_DIGEST_LENGTH;
  1232. int count;
  1233. for (count = 0; COND(kems_c[testnum][2]); count++) {
  1234. int ret = EVP_PKEY_verify(ctx, sig, sig_len, md, md_len);
  1235. if (ret <= 0) {
  1236. BIO_printf(bio_err, "SIG verify failure at count %d\n", count);
  1237. ERR_print_errors(bio_err);
  1238. count = -1;
  1239. break;
  1240. }
  1241. }
  1242. return count;
  1243. }
  1244. static int run_benchmark(int async_jobs,
  1245. int (*loop_function) (void *), loopargs_t *loopargs)
  1246. {
  1247. int job_op_count = 0;
  1248. int total_op_count = 0;
  1249. int num_inprogress = 0;
  1250. int error = 0, i = 0, ret = 0;
  1251. OSSL_ASYNC_FD job_fd = 0;
  1252. size_t num_job_fds = 0;
  1253. if (async_jobs == 0) {
  1254. return loop_function((void *)&loopargs);
  1255. }
  1256. for (i = 0; i < async_jobs && !error; i++) {
  1257. loopargs_t *looparg_item = loopargs + i;
  1258. /* Copy pointer content (looparg_t item address) into async context */
  1259. ret = ASYNC_start_job(&loopargs[i].inprogress_job, loopargs[i].wait_ctx,
  1260. &job_op_count, loop_function,
  1261. (void *)&looparg_item, sizeof(looparg_item));
  1262. switch (ret) {
  1263. case ASYNC_PAUSE:
  1264. ++num_inprogress;
  1265. break;
  1266. case ASYNC_FINISH:
  1267. if (job_op_count == -1) {
  1268. error = 1;
  1269. } else {
  1270. total_op_count += job_op_count;
  1271. }
  1272. break;
  1273. case ASYNC_NO_JOBS:
  1274. case ASYNC_ERR:
  1275. BIO_printf(bio_err, "Failure in the job\n");
  1276. ERR_print_errors(bio_err);
  1277. error = 1;
  1278. break;
  1279. }
  1280. }
  1281. while (num_inprogress > 0) {
  1282. #if defined(OPENSSL_SYS_WINDOWS)
  1283. DWORD avail = 0;
  1284. #elif defined(OPENSSL_SYS_UNIX)
  1285. int select_result = 0;
  1286. OSSL_ASYNC_FD max_fd = 0;
  1287. fd_set waitfdset;
  1288. FD_ZERO(&waitfdset);
  1289. for (i = 0; i < async_jobs && num_inprogress > 0; i++) {
  1290. if (loopargs[i].inprogress_job == NULL)
  1291. continue;
  1292. if (!ASYNC_WAIT_CTX_get_all_fds
  1293. (loopargs[i].wait_ctx, NULL, &num_job_fds)
  1294. || num_job_fds > 1) {
  1295. BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
  1296. ERR_print_errors(bio_err);
  1297. error = 1;
  1298. break;
  1299. }
  1300. ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
  1301. &num_job_fds);
  1302. FD_SET(job_fd, &waitfdset);
  1303. if (job_fd > max_fd)
  1304. max_fd = job_fd;
  1305. }
  1306. if (max_fd >= (OSSL_ASYNC_FD)FD_SETSIZE) {
  1307. BIO_printf(bio_err,
  1308. "Error: max_fd (%d) must be smaller than FD_SETSIZE (%d). "
  1309. "Decrease the value of async_jobs\n",
  1310. max_fd, FD_SETSIZE);
  1311. ERR_print_errors(bio_err);
  1312. error = 1;
  1313. break;
  1314. }
  1315. select_result = select(max_fd + 1, &waitfdset, NULL, NULL, NULL);
  1316. if (select_result == -1 && errno == EINTR)
  1317. continue;
  1318. if (select_result == -1) {
  1319. BIO_printf(bio_err, "Failure in the select\n");
  1320. ERR_print_errors(bio_err);
  1321. error = 1;
  1322. break;
  1323. }
  1324. if (select_result == 0)
  1325. continue;
  1326. #endif
  1327. for (i = 0; i < async_jobs; i++) {
  1328. if (loopargs[i].inprogress_job == NULL)
  1329. continue;
  1330. if (!ASYNC_WAIT_CTX_get_all_fds
  1331. (loopargs[i].wait_ctx, NULL, &num_job_fds)
  1332. || num_job_fds > 1) {
  1333. BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
  1334. ERR_print_errors(bio_err);
  1335. error = 1;
  1336. break;
  1337. }
  1338. ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
  1339. &num_job_fds);
  1340. #if defined(OPENSSL_SYS_UNIX)
  1341. if (num_job_fds == 1 && !FD_ISSET(job_fd, &waitfdset))
  1342. continue;
  1343. #elif defined(OPENSSL_SYS_WINDOWS)
  1344. if (num_job_fds == 1
  1345. && !PeekNamedPipe(job_fd, NULL, 0, NULL, &avail, NULL)
  1346. && avail > 0)
  1347. continue;
  1348. #endif
  1349. ret = ASYNC_start_job(&loopargs[i].inprogress_job,
  1350. loopargs[i].wait_ctx, &job_op_count,
  1351. loop_function, (void *)(loopargs + i),
  1352. sizeof(loopargs_t));
  1353. switch (ret) {
  1354. case ASYNC_PAUSE:
  1355. break;
  1356. case ASYNC_FINISH:
  1357. if (job_op_count == -1) {
  1358. error = 1;
  1359. } else {
  1360. total_op_count += job_op_count;
  1361. }
  1362. --num_inprogress;
  1363. loopargs[i].inprogress_job = NULL;
  1364. break;
  1365. case ASYNC_NO_JOBS:
  1366. case ASYNC_ERR:
  1367. --num_inprogress;
  1368. loopargs[i].inprogress_job = NULL;
  1369. BIO_printf(bio_err, "Failure in the job\n");
  1370. ERR_print_errors(bio_err);
  1371. error = 1;
  1372. break;
  1373. }
  1374. }
  1375. }
  1376. return error ? -1 : total_op_count;
  1377. }
  1378. typedef struct ec_curve_st {
  1379. const char *name;
  1380. unsigned int nid;
  1381. unsigned int bits;
  1382. size_t sigsize; /* only used for EdDSA curves */
  1383. } EC_CURVE;
  1384. static EVP_PKEY *get_ecdsa(const EC_CURVE *curve)
  1385. {
  1386. EVP_PKEY_CTX *kctx = NULL;
  1387. EVP_PKEY *key = NULL;
  1388. /* Ensure that the error queue is empty */
  1389. if (ERR_peek_error()) {
  1390. BIO_printf(bio_err,
  1391. "WARNING: the error queue contains previous unhandled errors.\n");
  1392. ERR_print_errors(bio_err);
  1393. }
  1394. /*
  1395. * Let's try to create a ctx directly from the NID: this works for
  1396. * curves like Curve25519 that are not implemented through the low
  1397. * level EC interface.
  1398. * If this fails we try creating a EVP_PKEY_EC generic param ctx,
  1399. * then we set the curve by NID before deriving the actual keygen
  1400. * ctx for that specific curve.
  1401. */
  1402. kctx = EVP_PKEY_CTX_new_id(curve->nid, NULL);
  1403. if (kctx == NULL) {
  1404. EVP_PKEY_CTX *pctx = NULL;
  1405. EVP_PKEY *params = NULL;
  1406. /*
  1407. * If we reach this code EVP_PKEY_CTX_new_id() failed and a
  1408. * "int_ctx_new:unsupported algorithm" error was added to the
  1409. * error queue.
  1410. * We remove it from the error queue as we are handling it.
  1411. */
  1412. unsigned long error = ERR_peek_error();
  1413. if (error == ERR_peek_last_error() /* oldest and latest errors match */
  1414. /* check that the error origin matches */
  1415. && ERR_GET_LIB(error) == ERR_LIB_EVP
  1416. && (ERR_GET_REASON(error) == EVP_R_UNSUPPORTED_ALGORITHM
  1417. || ERR_GET_REASON(error) == ERR_R_UNSUPPORTED))
  1418. ERR_get_error(); /* pop error from queue */
  1419. if (ERR_peek_error()) {
  1420. BIO_printf(bio_err,
  1421. "Unhandled error in the error queue during EC key setup.\n");
  1422. ERR_print_errors(bio_err);
  1423. return NULL;
  1424. }
  1425. /* Create the context for parameter generation */
  1426. if ((pctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL)) == NULL
  1427. || EVP_PKEY_paramgen_init(pctx) <= 0
  1428. || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
  1429. curve->nid) <= 0
  1430. || EVP_PKEY_paramgen(pctx, &params) <= 0) {
  1431. BIO_printf(bio_err, "EC params init failure.\n");
  1432. ERR_print_errors(bio_err);
  1433. EVP_PKEY_CTX_free(pctx);
  1434. return NULL;
  1435. }
  1436. EVP_PKEY_CTX_free(pctx);
  1437. /* Create the context for the key generation */
  1438. kctx = EVP_PKEY_CTX_new(params, NULL);
  1439. EVP_PKEY_free(params);
  1440. }
  1441. if (kctx == NULL
  1442. || EVP_PKEY_keygen_init(kctx) <= 0
  1443. || EVP_PKEY_keygen(kctx, &key) <= 0) {
  1444. BIO_printf(bio_err, "EC key generation failure.\n");
  1445. ERR_print_errors(bio_err);
  1446. key = NULL;
  1447. }
  1448. EVP_PKEY_CTX_free(kctx);
  1449. return key;
  1450. }
  1451. #define stop_it(do_it, test_num)\
  1452. memset(do_it + test_num, 0, OSSL_NELEM(do_it) - test_num);
  1453. /* Checks to see if algorithms are fetchable */
  1454. #define IS_FETCHABLE(type, TYPE) \
  1455. static int is_ ## type ## _fetchable(const TYPE *alg) \
  1456. { \
  1457. TYPE *impl; \
  1458. const char *propq = app_get0_propq(); \
  1459. OSSL_LIB_CTX *libctx = app_get0_libctx(); \
  1460. const char *name = TYPE ## _get0_name(alg); \
  1461. \
  1462. ERR_set_mark(); \
  1463. impl = TYPE ## _fetch(libctx, name, propq); \
  1464. ERR_pop_to_mark(); \
  1465. if (impl == NULL) \
  1466. return 0; \
  1467. TYPE ## _free(impl); \
  1468. return 1; \
  1469. }
  1470. IS_FETCHABLE(signature, EVP_SIGNATURE)
  1471. IS_FETCHABLE(kem, EVP_KEM)
  1472. DEFINE_STACK_OF(EVP_KEM)
  1473. static int kems_cmp(const EVP_KEM * const *a,
  1474. const EVP_KEM * const *b)
  1475. {
  1476. return strcmp(OSSL_PROVIDER_get0_name(EVP_KEM_get0_provider(*a)),
  1477. OSSL_PROVIDER_get0_name(EVP_KEM_get0_provider(*b)));
  1478. }
  1479. static void collect_kem(EVP_KEM *kem, void *stack)
  1480. {
  1481. STACK_OF(EVP_KEM) *kem_stack = stack;
  1482. if (is_kem_fetchable(kem)
  1483. && sk_EVP_KEM_push(kem_stack, kem) > 0) {
  1484. EVP_KEM_up_ref(kem);
  1485. }
  1486. }
  1487. static int kem_locate(const char *algo, unsigned int *idx)
  1488. {
  1489. unsigned int i;
  1490. for (i = 0; i < kems_algs_len; i++) {
  1491. if (strcmp(kems_algname[i], algo) == 0) {
  1492. *idx = i;
  1493. return 1;
  1494. }
  1495. }
  1496. return 0;
  1497. }
  1498. DEFINE_STACK_OF(EVP_SIGNATURE)
  1499. static int signatures_cmp(const EVP_SIGNATURE * const *a,
  1500. const EVP_SIGNATURE * const *b)
  1501. {
  1502. return strcmp(OSSL_PROVIDER_get0_name(EVP_SIGNATURE_get0_provider(*a)),
  1503. OSSL_PROVIDER_get0_name(EVP_SIGNATURE_get0_provider(*b)));
  1504. }
  1505. static void collect_signatures(EVP_SIGNATURE *sig, void *stack)
  1506. {
  1507. STACK_OF(EVP_SIGNATURE) *sig_stack = stack;
  1508. if (is_signature_fetchable(sig)
  1509. && sk_EVP_SIGNATURE_push(sig_stack, sig) > 0)
  1510. EVP_SIGNATURE_up_ref(sig);
  1511. }
  1512. static int sig_locate(const char *algo, unsigned int *idx)
  1513. {
  1514. unsigned int i;
  1515. for (i = 0; i < sigs_algs_len; i++) {
  1516. if (strcmp(sigs_algname[i], algo) == 0) {
  1517. *idx = i;
  1518. return 1;
  1519. }
  1520. }
  1521. return 0;
  1522. }
  1523. static int get_max(const uint8_t doit[], size_t algs_len) {
  1524. size_t i = 0;
  1525. int maxcnt = 0;
  1526. for (i = 0; i < algs_len; i++)
  1527. if (maxcnt < doit[i]) maxcnt = doit[i];
  1528. return maxcnt;
  1529. }
  1530. int speed_main(int argc, char **argv)
  1531. {
  1532. CONF *conf = NULL;
  1533. ENGINE *e = NULL;
  1534. loopargs_t *loopargs = NULL;
  1535. const char *prog;
  1536. const char *engine_id = NULL;
  1537. EVP_CIPHER *evp_cipher = NULL;
  1538. EVP_MAC *mac = NULL;
  1539. double d = 0.0;
  1540. OPTION_CHOICE o;
  1541. int async_init = 0, multiblock = 0, pr_header = 0;
  1542. uint8_t doit[ALGOR_NUM] = { 0 };
  1543. int ret = 1, misalign = 0, lengths_single = 0, aead = 0;
  1544. STACK_OF(EVP_KEM) *kem_stack = NULL;
  1545. STACK_OF(EVP_SIGNATURE) *sig_stack = NULL;
  1546. long count = 0;
  1547. unsigned int size_num = SIZE_NUM;
  1548. unsigned int i, k, loopargs_len = 0, async_jobs = 0;
  1549. unsigned int idx;
  1550. int keylen;
  1551. int buflen;
  1552. size_t declen;
  1553. BIGNUM *bn = NULL;
  1554. EVP_PKEY_CTX *genctx = NULL;
  1555. #ifndef NO_FORK
  1556. int multi = 0;
  1557. #endif
  1558. long op_count = 1;
  1559. openssl_speed_sec_t seconds = { SECONDS, RSA_SECONDS, DSA_SECONDS,
  1560. ECDSA_SECONDS, ECDH_SECONDS,
  1561. EdDSA_SECONDS, SM2_SECONDS,
  1562. FFDH_SECONDS, KEM_SECONDS,
  1563. SIG_SECONDS };
  1564. static const unsigned char key32[32] = {
  1565. 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
  1566. 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
  1567. 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
  1568. 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
  1569. };
  1570. static const unsigned char deskey[] = {
  1571. 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, /* key1 */
  1572. 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, /* key2 */
  1573. 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 /* key3 */
  1574. };
  1575. static const struct {
  1576. const unsigned char *data;
  1577. unsigned int length;
  1578. unsigned int bits;
  1579. } rsa_keys[] = {
  1580. { test512, sizeof(test512), 512 },
  1581. { test1024, sizeof(test1024), 1024 },
  1582. { test2048, sizeof(test2048), 2048 },
  1583. { test3072, sizeof(test3072), 3072 },
  1584. { test4096, sizeof(test4096), 4096 },
  1585. { test7680, sizeof(test7680), 7680 },
  1586. { test15360, sizeof(test15360), 15360 }
  1587. };
  1588. uint8_t rsa_doit[RSA_NUM] = { 0 };
  1589. int primes = RSA_DEFAULT_PRIME_NUM;
  1590. #ifndef OPENSSL_NO_DH
  1591. typedef struct ffdh_params_st {
  1592. const char *name;
  1593. unsigned int nid;
  1594. unsigned int bits;
  1595. } FFDH_PARAMS;
  1596. static const FFDH_PARAMS ffdh_params[FFDH_NUM] = {
  1597. {"ffdh2048", NID_ffdhe2048, 2048},
  1598. {"ffdh3072", NID_ffdhe3072, 3072},
  1599. {"ffdh4096", NID_ffdhe4096, 4096},
  1600. {"ffdh6144", NID_ffdhe6144, 6144},
  1601. {"ffdh8192", NID_ffdhe8192, 8192}
  1602. };
  1603. uint8_t ffdh_doit[FFDH_NUM] = { 0 };
  1604. #endif /* OPENSSL_NO_DH */
  1605. static const unsigned int dsa_bits[DSA_NUM] = { 1024, 2048 };
  1606. uint8_t dsa_doit[DSA_NUM] = { 0 };
  1607. /*
  1608. * We only test over the following curves as they are representative, To
  1609. * add tests over more curves, simply add the curve NID and curve name to
  1610. * the following arrays and increase the |ecdh_choices| and |ecdsa_choices|
  1611. * lists accordingly.
  1612. */
  1613. static const EC_CURVE ec_curves[EC_NUM] = {
  1614. /* Prime Curves */
  1615. {"secp160r1", NID_secp160r1, 160},
  1616. {"nistp192", NID_X9_62_prime192v1, 192},
  1617. {"nistp224", NID_secp224r1, 224},
  1618. {"nistp256", NID_X9_62_prime256v1, 256},
  1619. {"nistp384", NID_secp384r1, 384},
  1620. {"nistp521", NID_secp521r1, 521},
  1621. #ifndef OPENSSL_NO_EC2M
  1622. /* Binary Curves */
  1623. {"nistk163", NID_sect163k1, 163},
  1624. {"nistk233", NID_sect233k1, 233},
  1625. {"nistk283", NID_sect283k1, 283},
  1626. {"nistk409", NID_sect409k1, 409},
  1627. {"nistk571", NID_sect571k1, 571},
  1628. {"nistb163", NID_sect163r2, 163},
  1629. {"nistb233", NID_sect233r1, 233},
  1630. {"nistb283", NID_sect283r1, 283},
  1631. {"nistb409", NID_sect409r1, 409},
  1632. {"nistb571", NID_sect571r1, 571},
  1633. #endif
  1634. {"brainpoolP256r1", NID_brainpoolP256r1, 256},
  1635. {"brainpoolP256t1", NID_brainpoolP256t1, 256},
  1636. {"brainpoolP384r1", NID_brainpoolP384r1, 384},
  1637. {"brainpoolP384t1", NID_brainpoolP384t1, 384},
  1638. {"brainpoolP512r1", NID_brainpoolP512r1, 512},
  1639. {"brainpoolP512t1", NID_brainpoolP512t1, 512},
  1640. #ifndef OPENSSL_NO_ECX
  1641. /* Other and ECDH only ones */
  1642. {"X25519", NID_X25519, 253},
  1643. {"X448", NID_X448, 448}
  1644. #endif
  1645. };
  1646. #ifndef OPENSSL_NO_ECX
  1647. static const EC_CURVE ed_curves[EdDSA_NUM] = {
  1648. /* EdDSA */
  1649. {"Ed25519", NID_ED25519, 253, 64},
  1650. {"Ed448", NID_ED448, 456, 114}
  1651. };
  1652. #endif /* OPENSSL_NO_ECX */
  1653. #ifndef OPENSSL_NO_SM2
  1654. static const EC_CURVE sm2_curves[SM2_NUM] = {
  1655. /* SM2 */
  1656. {"CurveSM2", NID_sm2, 256}
  1657. };
  1658. uint8_t sm2_doit[SM2_NUM] = { 0 };
  1659. #endif
  1660. uint8_t ecdsa_doit[ECDSA_NUM] = { 0 };
  1661. uint8_t ecdh_doit[EC_NUM] = { 0 };
  1662. #ifndef OPENSSL_NO_ECX
  1663. uint8_t eddsa_doit[EdDSA_NUM] = { 0 };
  1664. #endif /* OPENSSL_NO_ECX */
  1665. uint8_t kems_doit[MAX_KEM_NUM] = { 0 };
  1666. uint8_t sigs_doit[MAX_SIG_NUM] = { 0 };
  1667. uint8_t do_kems = 0;
  1668. uint8_t do_sigs = 0;
  1669. /* checks declared curves against choices list. */
  1670. #ifndef OPENSSL_NO_ECX
  1671. OPENSSL_assert(ed_curves[EdDSA_NUM - 1].nid == NID_ED448);
  1672. OPENSSL_assert(strcmp(eddsa_choices[EdDSA_NUM - 1].name, "ed448") == 0);
  1673. OPENSSL_assert(ec_curves[EC_NUM - 1].nid == NID_X448);
  1674. OPENSSL_assert(strcmp(ecdh_choices[EC_NUM - 1].name, "ecdhx448") == 0);
  1675. OPENSSL_assert(ec_curves[ECDSA_NUM - 1].nid == NID_brainpoolP512t1);
  1676. OPENSSL_assert(strcmp(ecdsa_choices[ECDSA_NUM - 1].name, "ecdsabrp512t1") == 0);
  1677. #endif /* OPENSSL_NO_ECX */
  1678. #ifndef OPENSSL_NO_SM2
  1679. OPENSSL_assert(sm2_curves[SM2_NUM - 1].nid == NID_sm2);
  1680. OPENSSL_assert(strcmp(sm2_choices[SM2_NUM - 1].name, "curveSM2") == 0);
  1681. #endif
  1682. prog = opt_init(argc, argv, speed_options);
  1683. while ((o = opt_next()) != OPT_EOF) {
  1684. switch (o) {
  1685. case OPT_EOF:
  1686. case OPT_ERR:
  1687. opterr:
  1688. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  1689. goto end;
  1690. case OPT_HELP:
  1691. opt_help(speed_options);
  1692. ret = 0;
  1693. goto end;
  1694. case OPT_ELAPSED:
  1695. usertime = 0;
  1696. break;
  1697. case OPT_EVP:
  1698. if (doit[D_EVP]) {
  1699. BIO_printf(bio_err, "%s: -evp option cannot be used more than once\n", prog);
  1700. goto opterr;
  1701. }
  1702. ERR_set_mark();
  1703. if (!opt_cipher_silent(opt_arg(), &evp_cipher)) {
  1704. if (have_md(opt_arg()))
  1705. evp_md_name = opt_arg();
  1706. }
  1707. if (evp_cipher == NULL && evp_md_name == NULL) {
  1708. ERR_clear_last_mark();
  1709. BIO_printf(bio_err,
  1710. "%s: %s is an unknown cipher or digest\n",
  1711. prog, opt_arg());
  1712. goto end;
  1713. }
  1714. ERR_pop_to_mark();
  1715. doit[D_EVP] = 1;
  1716. break;
  1717. case OPT_HMAC:
  1718. if (!have_md(opt_arg())) {
  1719. BIO_printf(bio_err, "%s: %s is an unknown digest\n",
  1720. prog, opt_arg());
  1721. goto end;
  1722. }
  1723. evp_mac_mdname = opt_arg();
  1724. doit[D_HMAC] = 1;
  1725. break;
  1726. case OPT_CMAC:
  1727. if (!have_cipher(opt_arg())) {
  1728. BIO_printf(bio_err, "%s: %s is an unknown cipher\n",
  1729. prog, opt_arg());
  1730. goto end;
  1731. }
  1732. evp_mac_ciphername = opt_arg();
  1733. doit[D_EVP_CMAC] = 1;
  1734. break;
  1735. case OPT_DECRYPT:
  1736. decrypt = 1;
  1737. break;
  1738. case OPT_ENGINE:
  1739. /*
  1740. * In a forked execution, an engine might need to be
  1741. * initialised by each child process, not by the parent.
  1742. * So store the name here and run setup_engine() later on.
  1743. */
  1744. engine_id = opt_arg();
  1745. break;
  1746. case OPT_MULTI:
  1747. #ifndef NO_FORK
  1748. multi = opt_int_arg();
  1749. if ((size_t)multi >= SIZE_MAX / sizeof(int)) {
  1750. BIO_printf(bio_err, "%s: multi argument too large\n", prog);
  1751. return 0;
  1752. }
  1753. #endif
  1754. break;
  1755. case OPT_ASYNCJOBS:
  1756. #ifndef OPENSSL_NO_ASYNC
  1757. async_jobs = opt_int_arg();
  1758. if (!ASYNC_is_capable()) {
  1759. BIO_printf(bio_err,
  1760. "%s: async_jobs specified but async not supported\n",
  1761. prog);
  1762. goto opterr;
  1763. }
  1764. if (async_jobs > 99999) {
  1765. BIO_printf(bio_err, "%s: too many async_jobs\n", prog);
  1766. goto opterr;
  1767. }
  1768. #endif
  1769. break;
  1770. case OPT_MISALIGN:
  1771. misalign = opt_int_arg();
  1772. if (misalign > MISALIGN) {
  1773. BIO_printf(bio_err,
  1774. "%s: Maximum offset is %d\n", prog, MISALIGN);
  1775. goto opterr;
  1776. }
  1777. break;
  1778. case OPT_MR:
  1779. mr = 1;
  1780. break;
  1781. case OPT_MB:
  1782. multiblock = 1;
  1783. #ifdef OPENSSL_NO_MULTIBLOCK
  1784. BIO_printf(bio_err,
  1785. "%s: -mb specified but multi-block support is disabled\n",
  1786. prog);
  1787. goto end;
  1788. #endif
  1789. break;
  1790. case OPT_R_CASES:
  1791. if (!opt_rand(o))
  1792. goto end;
  1793. break;
  1794. case OPT_PROV_CASES:
  1795. if (!opt_provider(o))
  1796. goto end;
  1797. break;
  1798. case OPT_CONFIG:
  1799. conf = app_load_config_modules(opt_arg());
  1800. if (conf == NULL)
  1801. goto end;
  1802. break;
  1803. case OPT_PRIMES:
  1804. primes = opt_int_arg();
  1805. break;
  1806. case OPT_SECONDS:
  1807. seconds.sym = seconds.rsa = seconds.dsa = seconds.ecdsa
  1808. = seconds.ecdh = seconds.eddsa
  1809. = seconds.sm2 = seconds.ffdh
  1810. = seconds.kem = seconds.sig = opt_int_arg();
  1811. break;
  1812. case OPT_BYTES:
  1813. lengths_single = opt_int_arg();
  1814. lengths = &lengths_single;
  1815. size_num = 1;
  1816. break;
  1817. case OPT_AEAD:
  1818. aead = 1;
  1819. break;
  1820. case OPT_KEM:
  1821. do_kems = 1;
  1822. break;
  1823. case OPT_SIG:
  1824. do_sigs = 1;
  1825. break;
  1826. case OPT_MLOCK:
  1827. domlock = 1;
  1828. #if !defined(_WIN32) && !defined(OPENSSL_SYS_LINUX)
  1829. BIO_printf(bio_err,
  1830. "%s: -mlock not supported on this platform\n",
  1831. prog);
  1832. goto end;
  1833. #endif
  1834. break;
  1835. }
  1836. }
  1837. /* find all KEMs currently available */
  1838. kem_stack = sk_EVP_KEM_new(kems_cmp);
  1839. EVP_KEM_do_all_provided(app_get0_libctx(), collect_kem, kem_stack);
  1840. kems_algs_len = 0;
  1841. for (idx = 0; idx < (unsigned int)sk_EVP_KEM_num(kem_stack); idx++) {
  1842. EVP_KEM *kem = sk_EVP_KEM_value(kem_stack, idx);
  1843. if (strcmp(EVP_KEM_get0_name(kem), "RSA") == 0) {
  1844. if (kems_algs_len + OSSL_NELEM(rsa_choices) >= MAX_KEM_NUM) {
  1845. BIO_printf(bio_err,
  1846. "Too many KEMs registered. Change MAX_KEM_NUM.\n");
  1847. goto end;
  1848. }
  1849. for (i = 0; i < OSSL_NELEM(rsa_choices); i++) {
  1850. kems_doit[kems_algs_len] = 1;
  1851. kems_algname[kems_algs_len++] = OPENSSL_strdup(rsa_choices[i].name);
  1852. }
  1853. } else if (strcmp(EVP_KEM_get0_name(kem), "EC") == 0) {
  1854. if (kems_algs_len + 3 >= MAX_KEM_NUM) {
  1855. BIO_printf(bio_err,
  1856. "Too many KEMs registered. Change MAX_KEM_NUM.\n");
  1857. goto end;
  1858. }
  1859. kems_doit[kems_algs_len] = 1;
  1860. kems_algname[kems_algs_len++] = OPENSSL_strdup("ECP-256");
  1861. kems_doit[kems_algs_len] = 1;
  1862. kems_algname[kems_algs_len++] = OPENSSL_strdup("ECP-384");
  1863. kems_doit[kems_algs_len] = 1;
  1864. kems_algname[kems_algs_len++] = OPENSSL_strdup("ECP-521");
  1865. } else {
  1866. if (kems_algs_len + 1 >= MAX_KEM_NUM) {
  1867. BIO_printf(bio_err,
  1868. "Too many KEMs registered. Change MAX_KEM_NUM.\n");
  1869. goto end;
  1870. }
  1871. kems_doit[kems_algs_len] = 1;
  1872. kems_algname[kems_algs_len++] = OPENSSL_strdup(EVP_KEM_get0_name(kem));
  1873. }
  1874. }
  1875. sk_EVP_KEM_pop_free(kem_stack, EVP_KEM_free);
  1876. kem_stack = NULL;
  1877. /* find all SIGNATUREs currently available */
  1878. sig_stack = sk_EVP_SIGNATURE_new(signatures_cmp);
  1879. EVP_SIGNATURE_do_all_provided(app_get0_libctx(), collect_signatures, sig_stack);
  1880. sigs_algs_len = 0;
  1881. for (idx = 0; idx < (unsigned int)sk_EVP_SIGNATURE_num(sig_stack); idx++) {
  1882. EVP_SIGNATURE *s = sk_EVP_SIGNATURE_value(sig_stack, idx);
  1883. const char *sig_name = EVP_SIGNATURE_get0_name(s);
  1884. if (strcmp(sig_name, "RSA") == 0) {
  1885. if (sigs_algs_len + OSSL_NELEM(rsa_choices) >= MAX_SIG_NUM) {
  1886. BIO_printf(bio_err,
  1887. "Too many signatures registered. Change MAX_SIG_NUM.\n");
  1888. goto end;
  1889. }
  1890. for (i = 0; i < OSSL_NELEM(rsa_choices); i++) {
  1891. sigs_doit[sigs_algs_len] = 1;
  1892. sigs_algname[sigs_algs_len++] = OPENSSL_strdup(rsa_choices[i].name);
  1893. }
  1894. }
  1895. else if (strcmp(sig_name, "DSA") == 0) {
  1896. if (sigs_algs_len + DSA_NUM >= MAX_SIG_NUM) {
  1897. BIO_printf(bio_err,
  1898. "Too many signatures registered. Change MAX_SIG_NUM.\n");
  1899. goto end;
  1900. }
  1901. for (i = 0; i < DSA_NUM; i++) {
  1902. sigs_doit[sigs_algs_len] = 1;
  1903. sigs_algname[sigs_algs_len++] = OPENSSL_strdup(dsa_choices[i].name);
  1904. }
  1905. }
  1906. /* skipping these algs as tested elsewhere - and b/o setup is a pain */
  1907. else if (strcmp(sig_name, "ED25519") &&
  1908. strcmp(sig_name, "ED448") &&
  1909. strcmp(sig_name, "ECDSA") &&
  1910. strcmp(sig_name, "HMAC") &&
  1911. strcmp(sig_name, "SIPHASH") &&
  1912. strcmp(sig_name, "POLY1305") &&
  1913. strcmp(sig_name, "CMAC") &&
  1914. strcmp(sig_name, "SM2")) { /* skip alg */
  1915. if (sigs_algs_len + 1 >= MAX_SIG_NUM) {
  1916. BIO_printf(bio_err,
  1917. "Too many signatures registered. Change MAX_SIG_NUM.\n");
  1918. goto end;
  1919. }
  1920. /* activate this provider algorithm */
  1921. sigs_doit[sigs_algs_len] = 1;
  1922. sigs_algname[sigs_algs_len++] = OPENSSL_strdup(sig_name);
  1923. }
  1924. }
  1925. sk_EVP_SIGNATURE_pop_free(sig_stack, EVP_SIGNATURE_free);
  1926. sig_stack = NULL;
  1927. /* Remaining arguments are algorithms. */
  1928. argc = opt_num_rest();
  1929. argv = opt_rest();
  1930. if (!app_RAND_load())
  1931. goto end;
  1932. for (; *argv; argv++) {
  1933. const char *algo = *argv;
  1934. int algo_found = 0;
  1935. if (opt_found(algo, doit_choices, &i)) {
  1936. doit[i] = 1;
  1937. algo_found = 1;
  1938. }
  1939. if (strcmp(algo, "des") == 0) {
  1940. doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
  1941. algo_found = 1;
  1942. }
  1943. if (strcmp(algo, "sha") == 0) {
  1944. doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
  1945. algo_found = 1;
  1946. }
  1947. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1948. if (strcmp(algo, "openssl") == 0) /* just for compatibility */
  1949. algo_found = 1;
  1950. #endif
  1951. if (HAS_PREFIX(algo, "rsa")) {
  1952. if (algo[sizeof("rsa") - 1] == '\0') {
  1953. memset(rsa_doit, 1, sizeof(rsa_doit));
  1954. algo_found = 1;
  1955. }
  1956. if (opt_found(algo, rsa_choices, &i)) {
  1957. rsa_doit[i] = 1;
  1958. algo_found = 1;
  1959. }
  1960. }
  1961. #ifndef OPENSSL_NO_DH
  1962. if (HAS_PREFIX(algo, "ffdh")) {
  1963. if (algo[sizeof("ffdh") - 1] == '\0') {
  1964. memset(ffdh_doit, 1, sizeof(ffdh_doit));
  1965. algo_found = 1;
  1966. }
  1967. if (opt_found(algo, ffdh_choices, &i)) {
  1968. ffdh_doit[i] = 2;
  1969. algo_found = 1;
  1970. }
  1971. }
  1972. #endif
  1973. if (HAS_PREFIX(algo, "dsa")) {
  1974. if (algo[sizeof("dsa") - 1] == '\0') {
  1975. memset(dsa_doit, 1, sizeof(dsa_doit));
  1976. algo_found = 1;
  1977. }
  1978. if (opt_found(algo, dsa_choices, &i)) {
  1979. dsa_doit[i] = 2;
  1980. algo_found = 1;
  1981. }
  1982. }
  1983. if (strcmp(algo, "aes") == 0) {
  1984. doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;
  1985. algo_found = 1;
  1986. }
  1987. if (strcmp(algo, "camellia") == 0) {
  1988. doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = doit[D_CBC_256_CML] = 1;
  1989. algo_found = 1;
  1990. }
  1991. if (HAS_PREFIX(algo, "ecdsa")) {
  1992. if (algo[sizeof("ecdsa") - 1] == '\0') {
  1993. memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
  1994. algo_found = 1;
  1995. }
  1996. if (opt_found(algo, ecdsa_choices, &i)) {
  1997. ecdsa_doit[i] = 2;
  1998. algo_found = 1;
  1999. }
  2000. }
  2001. if (HAS_PREFIX(algo, "ecdh")) {
  2002. if (algo[sizeof("ecdh") - 1] == '\0') {
  2003. memset(ecdh_doit, 1, sizeof(ecdh_doit));
  2004. algo_found = 1;
  2005. }
  2006. if (opt_found(algo, ecdh_choices, &i)) {
  2007. ecdh_doit[i] = 2;
  2008. algo_found = 1;
  2009. }
  2010. }
  2011. #ifndef OPENSSL_NO_ECX
  2012. if (strcmp(algo, "eddsa") == 0) {
  2013. memset(eddsa_doit, 1, sizeof(eddsa_doit));
  2014. algo_found = 1;
  2015. }
  2016. if (opt_found(algo, eddsa_choices, &i)) {
  2017. eddsa_doit[i] = 2;
  2018. algo_found = 1;
  2019. }
  2020. #endif /* OPENSSL_NO_ECX */
  2021. #ifndef OPENSSL_NO_SM2
  2022. if (strcmp(algo, "sm2") == 0) {
  2023. memset(sm2_doit, 1, sizeof(sm2_doit));
  2024. algo_found = 1;
  2025. }
  2026. if (opt_found(algo, sm2_choices, &i)) {
  2027. sm2_doit[i] = 2;
  2028. algo_found = 1;
  2029. }
  2030. #endif
  2031. if (kem_locate(algo, &idx)) {
  2032. kems_doit[idx]++;
  2033. do_kems = 1;
  2034. algo_found = 1;
  2035. }
  2036. if (sig_locate(algo, &idx)) {
  2037. sigs_doit[idx]++;
  2038. do_sigs = 1;
  2039. algo_found = 1;
  2040. }
  2041. if (strcmp(algo, "kmac") == 0) {
  2042. doit[D_KMAC128] = doit[D_KMAC256] = 1;
  2043. algo_found = 1;
  2044. }
  2045. if (strcmp(algo, "cmac") == 0) {
  2046. doit[D_EVP_CMAC] = 1;
  2047. algo_found = 1;
  2048. }
  2049. if (!algo_found) {
  2050. BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, algo);
  2051. goto end;
  2052. }
  2053. }
  2054. /* Sanity checks */
  2055. if (aead) {
  2056. if (evp_cipher == NULL) {
  2057. BIO_printf(bio_err, "-aead can be used only with an AEAD cipher\n");
  2058. goto end;
  2059. } else if (!(EVP_CIPHER_get_flags(evp_cipher) &
  2060. EVP_CIPH_FLAG_AEAD_CIPHER)) {
  2061. BIO_printf(bio_err, "%s is not an AEAD cipher\n",
  2062. EVP_CIPHER_get0_name(evp_cipher));
  2063. goto end;
  2064. }
  2065. }
  2066. if (kems_algs_len > 0) {
  2067. int maxcnt = get_max(kems_doit, kems_algs_len);
  2068. if (maxcnt > 1) {
  2069. /* some algs explicitly selected */
  2070. for (i = 0; i < kems_algs_len; i++) {
  2071. /* disable the rest */
  2072. kems_doit[i]--;
  2073. }
  2074. }
  2075. }
  2076. if (sigs_algs_len > 0) {
  2077. int maxcnt = get_max(sigs_doit, sigs_algs_len);
  2078. if (maxcnt > 1) {
  2079. /* some algs explicitly selected */
  2080. for (i = 0; i < sigs_algs_len; i++) {
  2081. /* disable the rest */
  2082. sigs_doit[i]--;
  2083. }
  2084. }
  2085. }
  2086. if (multiblock) {
  2087. if (evp_cipher == NULL) {
  2088. BIO_printf(bio_err, "-mb can be used only with a multi-block"
  2089. " capable cipher\n");
  2090. goto end;
  2091. } else if (!(EVP_CIPHER_get_flags(evp_cipher) &
  2092. EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
  2093. BIO_printf(bio_err, "%s is not a multi-block capable\n",
  2094. EVP_CIPHER_get0_name(evp_cipher));
  2095. goto end;
  2096. } else if (async_jobs > 0) {
  2097. BIO_printf(bio_err, "Async mode is not supported with -mb");
  2098. goto end;
  2099. }
  2100. }
  2101. /* Initialize the job pool if async mode is enabled */
  2102. if (async_jobs > 0) {
  2103. async_init = ASYNC_init_thread(async_jobs, async_jobs);
  2104. if (!async_init) {
  2105. BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
  2106. goto end;
  2107. }
  2108. }
  2109. loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
  2110. loopargs =
  2111. app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
  2112. memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));
  2113. buflen = lengths[size_num - 1];
  2114. if (buflen < 36) /* size of random vector in RSA benchmark */
  2115. buflen = 36;
  2116. if (INT_MAX - (MAX_MISALIGNMENT + 1) < buflen) {
  2117. BIO_printf(bio_err, "Error: buffer size too large\n");
  2118. goto end;
  2119. }
  2120. buflen += MAX_MISALIGNMENT + 1;
  2121. for (i = 0; i < loopargs_len; i++) {
  2122. if (async_jobs > 0) {
  2123. loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
  2124. if (loopargs[i].wait_ctx == NULL) {
  2125. BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
  2126. goto end;
  2127. }
  2128. }
  2129. loopargs[i].buf_malloc = app_malloc(buflen, "input buffer");
  2130. loopargs[i].buf2_malloc = app_malloc(buflen, "input buffer");
  2131. /* Align the start of buffers on a 64 byte boundary */
  2132. loopargs[i].buf = loopargs[i].buf_malloc + misalign;
  2133. loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
  2134. loopargs[i].buflen = buflen - misalign;
  2135. loopargs[i].sigsize = buflen - misalign;
  2136. loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
  2137. loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
  2138. #ifndef OPENSSL_NO_DH
  2139. loopargs[i].secret_ff_a = app_malloc(MAX_FFDH_SIZE, "FFDH secret a");
  2140. loopargs[i].secret_ff_b = app_malloc(MAX_FFDH_SIZE, "FFDH secret b");
  2141. #endif
  2142. }
  2143. #ifndef NO_FORK
  2144. if (multi && do_multi(multi, size_num))
  2145. goto show_res;
  2146. #endif
  2147. for (i = 0; i < loopargs_len; ++i) {
  2148. if (domlock) {
  2149. #if defined(_WIN32)
  2150. (void)VirtualLock(loopargs[i].buf_malloc, buflen);
  2151. (void)VirtualLock(loopargs[i].buf2_malloc, buflen);
  2152. #elif defined(OPENSSL_SYS_LINUX)
  2153. (void)mlock(loopargs[i].buf_malloc, buflen);
  2154. (void)mlock(loopargs[i].buf_malloc, buflen);
  2155. #endif
  2156. }
  2157. memset(loopargs[i].buf_malloc, 0, buflen);
  2158. memset(loopargs[i].buf2_malloc, 0, buflen);
  2159. }
  2160. /* Initialize the engine after the fork */
  2161. e = setup_engine(engine_id, 0);
  2162. /* No parameters; turn on everything. */
  2163. if (argc == 0 && !doit[D_EVP] && !doit[D_HMAC]
  2164. && !doit[D_EVP_CMAC] && !do_kems && !do_sigs) {
  2165. memset(doit, 1, sizeof(doit));
  2166. doit[D_EVP] = doit[D_EVP_CMAC] = 0;
  2167. ERR_set_mark();
  2168. for (i = D_MD2; i <= D_WHIRLPOOL; i++) {
  2169. if (!have_md(names[i]))
  2170. doit[i] = 0;
  2171. }
  2172. for (i = D_CBC_DES; i <= D_CBC_256_CML; i++) {
  2173. if (!have_cipher(names[i]))
  2174. doit[i] = 0;
  2175. }
  2176. if ((mac = EVP_MAC_fetch(app_get0_libctx(), "GMAC",
  2177. app_get0_propq())) != NULL) {
  2178. EVP_MAC_free(mac);
  2179. mac = NULL;
  2180. } else {
  2181. doit[D_GHASH] = 0;
  2182. }
  2183. if ((mac = EVP_MAC_fetch(app_get0_libctx(), "HMAC",
  2184. app_get0_propq())) != NULL) {
  2185. EVP_MAC_free(mac);
  2186. mac = NULL;
  2187. } else {
  2188. doit[D_HMAC] = 0;
  2189. }
  2190. ERR_pop_to_mark();
  2191. memset(rsa_doit, 1, sizeof(rsa_doit));
  2192. #ifndef OPENSSL_NO_DH
  2193. memset(ffdh_doit, 1, sizeof(ffdh_doit));
  2194. #endif
  2195. memset(dsa_doit, 1, sizeof(dsa_doit));
  2196. #ifndef OPENSSL_NO_ECX
  2197. memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
  2198. memset(ecdh_doit, 1, sizeof(ecdh_doit));
  2199. memset(eddsa_doit, 1, sizeof(eddsa_doit));
  2200. #endif /* OPENSSL_NO_ECX */
  2201. #ifndef OPENSSL_NO_SM2
  2202. memset(sm2_doit, 1, sizeof(sm2_doit));
  2203. #endif
  2204. memset(kems_doit, 1, sizeof(kems_doit));
  2205. do_kems = 1;
  2206. memset(sigs_doit, 1, sizeof(sigs_doit));
  2207. do_sigs = 1;
  2208. }
  2209. for (i = 0; i < ALGOR_NUM; i++)
  2210. if (doit[i])
  2211. pr_header++;
  2212. if (usertime == 0 && !mr)
  2213. BIO_printf(bio_err,
  2214. "You have chosen to measure elapsed time "
  2215. "instead of user CPU time.\n");
  2216. #if SIGALRM > 0
  2217. signal(SIGALRM, alarmed);
  2218. #endif
  2219. if (doit[D_MD2]) {
  2220. for (testnum = 0; testnum < size_num; testnum++) {
  2221. print_message(names[D_MD2], lengths[testnum], seconds.sym);
  2222. Time_F(START);
  2223. count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
  2224. d = Time_F(STOP);
  2225. print_result(D_MD2, testnum, count, d);
  2226. if (count < 0)
  2227. break;
  2228. }
  2229. }
  2230. if (doit[D_MDC2]) {
  2231. for (testnum = 0; testnum < size_num; testnum++) {
  2232. print_message(names[D_MDC2], lengths[testnum], seconds.sym);
  2233. Time_F(START);
  2234. count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
  2235. d = Time_F(STOP);
  2236. print_result(D_MDC2, testnum, count, d);
  2237. if (count < 0)
  2238. break;
  2239. }
  2240. }
  2241. if (doit[D_MD4]) {
  2242. for (testnum = 0; testnum < size_num; testnum++) {
  2243. print_message(names[D_MD4], lengths[testnum], seconds.sym);
  2244. Time_F(START);
  2245. count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
  2246. d = Time_F(STOP);
  2247. print_result(D_MD4, testnum, count, d);
  2248. if (count < 0)
  2249. break;
  2250. }
  2251. }
  2252. if (doit[D_MD5]) {
  2253. for (testnum = 0; testnum < size_num; testnum++) {
  2254. print_message(names[D_MD5], lengths[testnum], seconds.sym);
  2255. Time_F(START);
  2256. count = run_benchmark(async_jobs, MD5_loop, loopargs);
  2257. d = Time_F(STOP);
  2258. print_result(D_MD5, testnum, count, d);
  2259. if (count < 0)
  2260. break;
  2261. }
  2262. }
  2263. if (doit[D_SHA1]) {
  2264. for (testnum = 0; testnum < size_num; testnum++) {
  2265. print_message(names[D_SHA1], lengths[testnum], seconds.sym);
  2266. Time_F(START);
  2267. count = run_benchmark(async_jobs, SHA1_loop, loopargs);
  2268. d = Time_F(STOP);
  2269. print_result(D_SHA1, testnum, count, d);
  2270. if (count < 0)
  2271. break;
  2272. }
  2273. }
  2274. if (doit[D_SHA256]) {
  2275. for (testnum = 0; testnum < size_num; testnum++) {
  2276. print_message(names[D_SHA256], lengths[testnum], seconds.sym);
  2277. Time_F(START);
  2278. count = run_benchmark(async_jobs, SHA256_loop, loopargs);
  2279. d = Time_F(STOP);
  2280. print_result(D_SHA256, testnum, count, d);
  2281. if (count < 0)
  2282. break;
  2283. }
  2284. }
  2285. if (doit[D_SHA512]) {
  2286. for (testnum = 0; testnum < size_num; testnum++) {
  2287. print_message(names[D_SHA512], lengths[testnum], seconds.sym);
  2288. Time_F(START);
  2289. count = run_benchmark(async_jobs, SHA512_loop, loopargs);
  2290. d = Time_F(STOP);
  2291. print_result(D_SHA512, testnum, count, d);
  2292. if (count < 0)
  2293. break;
  2294. }
  2295. }
  2296. if (doit[D_WHIRLPOOL]) {
  2297. for (testnum = 0; testnum < size_num; testnum++) {
  2298. print_message(names[D_WHIRLPOOL], lengths[testnum], seconds.sym);
  2299. Time_F(START);
  2300. count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
  2301. d = Time_F(STOP);
  2302. print_result(D_WHIRLPOOL, testnum, count, d);
  2303. if (count < 0)
  2304. break;
  2305. }
  2306. }
  2307. if (doit[D_RMD160]) {
  2308. for (testnum = 0; testnum < size_num; testnum++) {
  2309. print_message(names[D_RMD160], lengths[testnum], seconds.sym);
  2310. Time_F(START);
  2311. count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
  2312. d = Time_F(STOP);
  2313. print_result(D_RMD160, testnum, count, d);
  2314. if (count < 0)
  2315. break;
  2316. }
  2317. }
  2318. if (doit[D_HMAC]) {
  2319. static const char hmac_key[] = "This is a key...";
  2320. int len = strlen(hmac_key);
  2321. OSSL_PARAM params[3];
  2322. if (evp_mac_mdname == NULL)
  2323. goto end;
  2324. evp_hmac_name = app_malloc(sizeof("hmac()") + strlen(evp_mac_mdname),
  2325. "HMAC name");
  2326. sprintf(evp_hmac_name, "hmac(%s)", evp_mac_mdname);
  2327. names[D_HMAC] = evp_hmac_name;
  2328. params[0] =
  2329. OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
  2330. evp_mac_mdname, 0);
  2331. params[1] =
  2332. OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
  2333. (char *)hmac_key, len);
  2334. params[2] = OSSL_PARAM_construct_end();
  2335. if (mac_setup("HMAC", &mac, params, loopargs, loopargs_len) < 1)
  2336. goto end;
  2337. for (testnum = 0; testnum < size_num; testnum++) {
  2338. print_message(names[D_HMAC], lengths[testnum], seconds.sym);
  2339. Time_F(START);
  2340. count = run_benchmark(async_jobs, HMAC_loop, loopargs);
  2341. d = Time_F(STOP);
  2342. print_result(D_HMAC, testnum, count, d);
  2343. if (count < 0)
  2344. break;
  2345. }
  2346. mac_teardown(&mac, loopargs, loopargs_len);
  2347. }
  2348. if (doit[D_CBC_DES]) {
  2349. int st = 1;
  2350. for (i = 0; st && i < loopargs_len; i++) {
  2351. loopargs[i].ctx = init_evp_cipher_ctx("des-cbc", deskey,
  2352. sizeof(deskey) / 3);
  2353. st = loopargs[i].ctx != NULL;
  2354. }
  2355. algindex = D_CBC_DES;
  2356. for (testnum = 0; st && testnum < size_num; testnum++) {
  2357. print_message(names[D_CBC_DES], lengths[testnum], seconds.sym);
  2358. Time_F(START);
  2359. count = run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
  2360. d = Time_F(STOP);
  2361. print_result(D_CBC_DES, testnum, count, d);
  2362. }
  2363. for (i = 0; i < loopargs_len; i++)
  2364. EVP_CIPHER_CTX_free(loopargs[i].ctx);
  2365. }
  2366. if (doit[D_EDE3_DES]) {
  2367. int st = 1;
  2368. for (i = 0; st && i < loopargs_len; i++) {
  2369. loopargs[i].ctx = init_evp_cipher_ctx("des-ede3-cbc", deskey,
  2370. sizeof(deskey));
  2371. st = loopargs[i].ctx != NULL;
  2372. }
  2373. algindex = D_EDE3_DES;
  2374. for (testnum = 0; st && testnum < size_num; testnum++) {
  2375. print_message(names[D_EDE3_DES], lengths[testnum], seconds.sym);
  2376. Time_F(START);
  2377. count =
  2378. run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
  2379. d = Time_F(STOP);
  2380. print_result(D_EDE3_DES, testnum, count, d);
  2381. }
  2382. for (i = 0; i < loopargs_len; i++)
  2383. EVP_CIPHER_CTX_free(loopargs[i].ctx);
  2384. }
  2385. for (k = 0; k < 3; k++) {
  2386. algindex = D_CBC_128_AES + k;
  2387. if (doit[algindex]) {
  2388. int st = 1;
  2389. keylen = 16 + k * 8;
  2390. for (i = 0; st && i < loopargs_len; i++) {
  2391. loopargs[i].ctx = init_evp_cipher_ctx(names[algindex],
  2392. key32, keylen);
  2393. st = loopargs[i].ctx != NULL;
  2394. }
  2395. for (testnum = 0; st && testnum < size_num; testnum++) {
  2396. print_message(names[algindex], lengths[testnum], seconds.sym);
  2397. Time_F(START);
  2398. count =
  2399. run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
  2400. d = Time_F(STOP);
  2401. print_result(algindex, testnum, count, d);
  2402. }
  2403. for (i = 0; i < loopargs_len; i++)
  2404. EVP_CIPHER_CTX_free(loopargs[i].ctx);
  2405. }
  2406. }
  2407. for (k = 0; k < 3; k++) {
  2408. algindex = D_CBC_128_CML + k;
  2409. if (doit[algindex]) {
  2410. int st = 1;
  2411. keylen = 16 + k * 8;
  2412. for (i = 0; st && i < loopargs_len; i++) {
  2413. loopargs[i].ctx = init_evp_cipher_ctx(names[algindex],
  2414. key32, keylen);
  2415. st = loopargs[i].ctx != NULL;
  2416. }
  2417. for (testnum = 0; st && testnum < size_num; testnum++) {
  2418. print_message(names[algindex], lengths[testnum], seconds.sym);
  2419. Time_F(START);
  2420. count =
  2421. run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
  2422. d = Time_F(STOP);
  2423. print_result(algindex, testnum, count, d);
  2424. }
  2425. for (i = 0; i < loopargs_len; i++)
  2426. EVP_CIPHER_CTX_free(loopargs[i].ctx);
  2427. }
  2428. }
  2429. for (algindex = D_RC4; algindex <= D_CBC_CAST; algindex++) {
  2430. if (doit[algindex]) {
  2431. int st = 1;
  2432. keylen = 16;
  2433. for (i = 0; st && i < loopargs_len; i++) {
  2434. loopargs[i].ctx = init_evp_cipher_ctx(names[algindex],
  2435. key32, keylen);
  2436. st = loopargs[i].ctx != NULL;
  2437. }
  2438. for (testnum = 0; st && testnum < size_num; testnum++) {
  2439. print_message(names[algindex], lengths[testnum], seconds.sym);
  2440. Time_F(START);
  2441. count =
  2442. run_benchmark(async_jobs, EVP_Cipher_loop, loopargs);
  2443. d = Time_F(STOP);
  2444. print_result(algindex, testnum, count, d);
  2445. }
  2446. for (i = 0; i < loopargs_len; i++)
  2447. EVP_CIPHER_CTX_free(loopargs[i].ctx);
  2448. }
  2449. }
  2450. if (doit[D_GHASH]) {
  2451. static const char gmac_iv[] = "0123456789ab";
  2452. OSSL_PARAM params[4];
  2453. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_CIPHER,
  2454. "aes-128-gcm", 0);
  2455. params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV,
  2456. (char *)gmac_iv,
  2457. sizeof(gmac_iv) - 1);
  2458. params[2] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
  2459. (void *)key32, 16);
  2460. params[3] = OSSL_PARAM_construct_end();
  2461. if (mac_setup("GMAC", &mac, params, loopargs, loopargs_len) < 1)
  2462. goto end;
  2463. /* b/c of the definition of GHASH_loop(), init() calls are needed here */
  2464. for (i = 0; i < loopargs_len; i++) {
  2465. if (!EVP_MAC_init(loopargs[i].mctx, NULL, 0, NULL))
  2466. goto end;
  2467. }
  2468. for (testnum = 0; testnum < size_num; testnum++) {
  2469. print_message(names[D_GHASH], lengths[testnum], seconds.sym);
  2470. Time_F(START);
  2471. count = run_benchmark(async_jobs, GHASH_loop, loopargs);
  2472. d = Time_F(STOP);
  2473. print_result(D_GHASH, testnum, count, d);
  2474. if (count < 0)
  2475. break;
  2476. }
  2477. mac_teardown(&mac, loopargs, loopargs_len);
  2478. }
  2479. if (doit[D_RAND]) {
  2480. for (testnum = 0; testnum < size_num; testnum++) {
  2481. print_message(names[D_RAND], lengths[testnum], seconds.sym);
  2482. Time_F(START);
  2483. count = run_benchmark(async_jobs, RAND_bytes_loop, loopargs);
  2484. d = Time_F(STOP);
  2485. print_result(D_RAND, testnum, count, d);
  2486. }
  2487. }
  2488. if (doit[D_EVP]) {
  2489. if (evp_cipher != NULL) {
  2490. int (*loopfunc) (void *) = EVP_Update_loop;
  2491. if (multiblock && (EVP_CIPHER_get_flags(evp_cipher) &
  2492. EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
  2493. multiblock_speed(evp_cipher, lengths_single, &seconds);
  2494. ret = 0;
  2495. goto end;
  2496. }
  2497. names[D_EVP] = EVP_CIPHER_get0_name(evp_cipher);
  2498. if (EVP_CIPHER_get_mode(evp_cipher) == EVP_CIPH_CCM_MODE) {
  2499. loopfunc = EVP_Update_loop_ccm;
  2500. } else if (aead && (EVP_CIPHER_get_flags(evp_cipher) &
  2501. EVP_CIPH_FLAG_AEAD_CIPHER)) {
  2502. loopfunc = EVP_Update_loop_aead;
  2503. if (lengths == lengths_list) {
  2504. lengths = aead_lengths_list;
  2505. size_num = OSSL_NELEM(aead_lengths_list);
  2506. }
  2507. }
  2508. for (testnum = 0; testnum < size_num; testnum++) {
  2509. print_message(names[D_EVP], lengths[testnum], seconds.sym);
  2510. for (k = 0; k < loopargs_len; k++) {
  2511. loopargs[k].ctx = EVP_CIPHER_CTX_new();
  2512. if (loopargs[k].ctx == NULL) {
  2513. BIO_printf(bio_err, "\nEVP_CIPHER_CTX_new failure\n");
  2514. exit(1);
  2515. }
  2516. if (!EVP_CipherInit_ex(loopargs[k].ctx, evp_cipher, NULL,
  2517. NULL, iv, decrypt ? 0 : 1)) {
  2518. BIO_printf(bio_err, "\nEVP_CipherInit_ex failure\n");
  2519. ERR_print_errors(bio_err);
  2520. exit(1);
  2521. }
  2522. EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
  2523. keylen = EVP_CIPHER_CTX_get_key_length(loopargs[k].ctx);
  2524. loopargs[k].key = app_malloc(keylen, "evp_cipher key");
  2525. EVP_CIPHER_CTX_rand_key(loopargs[k].ctx, loopargs[k].key);
  2526. if (!EVP_CipherInit_ex(loopargs[k].ctx, NULL, NULL,
  2527. loopargs[k].key, NULL, -1)) {
  2528. BIO_printf(bio_err, "\nEVP_CipherInit_ex failure\n");
  2529. ERR_print_errors(bio_err);
  2530. exit(1);
  2531. }
  2532. OPENSSL_clear_free(loopargs[k].key, keylen);
  2533. /* GCM-SIV/SIV mode only allows for a single Update operation */
  2534. if (EVP_CIPHER_get_mode(evp_cipher) == EVP_CIPH_SIV_MODE
  2535. || EVP_CIPHER_get_mode(evp_cipher) == EVP_CIPH_GCM_SIV_MODE)
  2536. (void)EVP_CIPHER_CTX_ctrl(loopargs[k].ctx,
  2537. EVP_CTRL_SET_SPEED, 1, NULL);
  2538. }
  2539. Time_F(START);
  2540. count = run_benchmark(async_jobs, loopfunc, loopargs);
  2541. d = Time_F(STOP);
  2542. for (k = 0; k < loopargs_len; k++)
  2543. EVP_CIPHER_CTX_free(loopargs[k].ctx);
  2544. print_result(D_EVP, testnum, count, d);
  2545. }
  2546. } else if (evp_md_name != NULL) {
  2547. names[D_EVP] = evp_md_name;
  2548. for (testnum = 0; testnum < size_num; testnum++) {
  2549. print_message(names[D_EVP], lengths[testnum], seconds.sym);
  2550. Time_F(START);
  2551. count = run_benchmark(async_jobs, EVP_Digest_md_loop, loopargs);
  2552. d = Time_F(STOP);
  2553. print_result(D_EVP, testnum, count, d);
  2554. if (count < 0)
  2555. break;
  2556. }
  2557. }
  2558. }
  2559. if (doit[D_EVP_CMAC]) {
  2560. OSSL_PARAM params[3];
  2561. EVP_CIPHER *cipher = NULL;
  2562. if (!opt_cipher(evp_mac_ciphername, &cipher))
  2563. goto end;
  2564. keylen = EVP_CIPHER_get_key_length(cipher);
  2565. EVP_CIPHER_free(cipher);
  2566. if (keylen <= 0 || keylen > (int)sizeof(key32)) {
  2567. BIO_printf(bio_err, "\nRequested CMAC cipher with unsupported key length.\n");
  2568. goto end;
  2569. }
  2570. evp_cmac_name = app_malloc(sizeof("cmac()")
  2571. + strlen(evp_mac_ciphername), "CMAC name");
  2572. sprintf(evp_cmac_name, "cmac(%s)", evp_mac_ciphername);
  2573. names[D_EVP_CMAC] = evp_cmac_name;
  2574. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_CIPHER,
  2575. evp_mac_ciphername, 0);
  2576. params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
  2577. (char *)key32, keylen);
  2578. params[2] = OSSL_PARAM_construct_end();
  2579. if (mac_setup("CMAC", &mac, params, loopargs, loopargs_len) < 1)
  2580. goto end;
  2581. for (testnum = 0; testnum < size_num; testnum++) {
  2582. print_message(names[D_EVP_CMAC], lengths[testnum], seconds.sym);
  2583. Time_F(START);
  2584. count = run_benchmark(async_jobs, CMAC_loop, loopargs);
  2585. d = Time_F(STOP);
  2586. print_result(D_EVP_CMAC, testnum, count, d);
  2587. if (count < 0)
  2588. break;
  2589. }
  2590. mac_teardown(&mac, loopargs, loopargs_len);
  2591. }
  2592. if (doit[D_KMAC128]) {
  2593. OSSL_PARAM params[2];
  2594. params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
  2595. (void *)key32, 16);
  2596. params[1] = OSSL_PARAM_construct_end();
  2597. if (mac_setup("KMAC-128", &mac, params, loopargs, loopargs_len) < 1)
  2598. goto end;
  2599. for (testnum = 0; testnum < size_num; testnum++) {
  2600. print_message(names[D_KMAC128], lengths[testnum], seconds.sym);
  2601. Time_F(START);
  2602. count = run_benchmark(async_jobs, KMAC128_loop, loopargs);
  2603. d = Time_F(STOP);
  2604. print_result(D_KMAC128, testnum, count, d);
  2605. if (count < 0)
  2606. break;
  2607. }
  2608. mac_teardown(&mac, loopargs, loopargs_len);
  2609. }
  2610. if (doit[D_KMAC256]) {
  2611. OSSL_PARAM params[2];
  2612. params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
  2613. (void *)key32, 32);
  2614. params[1] = OSSL_PARAM_construct_end();
  2615. if (mac_setup("KMAC-256", &mac, params, loopargs, loopargs_len) < 1)
  2616. goto end;
  2617. for (testnum = 0; testnum < size_num; testnum++) {
  2618. print_message(names[D_KMAC256], lengths[testnum], seconds.sym);
  2619. Time_F(START);
  2620. count = run_benchmark(async_jobs, KMAC256_loop, loopargs);
  2621. d = Time_F(STOP);
  2622. print_result(D_KMAC256, testnum, count, d);
  2623. if (count < 0)
  2624. break;
  2625. }
  2626. mac_teardown(&mac, loopargs, loopargs_len);
  2627. }
  2628. for (i = 0; i < loopargs_len; i++)
  2629. if (RAND_bytes(loopargs[i].buf, 36) <= 0)
  2630. goto end;
  2631. for (testnum = 0; testnum < RSA_NUM; testnum++) {
  2632. EVP_PKEY *rsa_key = NULL;
  2633. int st = 0;
  2634. if (!rsa_doit[testnum])
  2635. continue;
  2636. if (primes > RSA_DEFAULT_PRIME_NUM) {
  2637. /* we haven't set keys yet, generate multi-prime RSA keys */
  2638. bn = BN_new();
  2639. st = bn != NULL
  2640. && BN_set_word(bn, RSA_F4)
  2641. && init_gen_str(&genctx, "RSA", NULL, 0, NULL, NULL)
  2642. && EVP_PKEY_CTX_set_rsa_keygen_bits(genctx, rsa_keys[testnum].bits) > 0
  2643. && EVP_PKEY_CTX_set1_rsa_keygen_pubexp(genctx, bn) > 0
  2644. && EVP_PKEY_CTX_set_rsa_keygen_primes(genctx, primes) > 0
  2645. && EVP_PKEY_keygen(genctx, &rsa_key);
  2646. BN_free(bn);
  2647. bn = NULL;
  2648. EVP_PKEY_CTX_free(genctx);
  2649. genctx = NULL;
  2650. } else {
  2651. const unsigned char *p = rsa_keys[testnum].data;
  2652. st = (rsa_key = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p,
  2653. rsa_keys[testnum].length)) != NULL;
  2654. }
  2655. for (i = 0; st && i < loopargs_len; i++) {
  2656. loopargs[i].rsa_sign_ctx[testnum] = EVP_PKEY_CTX_new(rsa_key, NULL);
  2657. loopargs[i].sigsize = loopargs[i].buflen;
  2658. if (loopargs[i].rsa_sign_ctx[testnum] == NULL
  2659. || EVP_PKEY_sign_init(loopargs[i].rsa_sign_ctx[testnum]) <= 0
  2660. || EVP_PKEY_sign(loopargs[i].rsa_sign_ctx[testnum],
  2661. loopargs[i].buf2,
  2662. &loopargs[i].sigsize,
  2663. loopargs[i].buf, 36) <= 0)
  2664. st = 0;
  2665. }
  2666. if (!st) {
  2667. BIO_printf(bio_err,
  2668. "RSA sign setup failure. No RSA sign will be done.\n");
  2669. ERR_print_errors(bio_err);
  2670. op_count = 1;
  2671. } else {
  2672. pkey_print_message("private", "rsa sign",
  2673. rsa_keys[testnum].bits, seconds.rsa);
  2674. /* RSA_blinding_on(rsa_key[testnum],NULL); */
  2675. Time_F(START);
  2676. count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
  2677. d = Time_F(STOP);
  2678. BIO_printf(bio_err,
  2679. mr ? "+R1:%ld:%d:%.2f\n"
  2680. : "%ld %u bits private RSA sign ops in %.2fs\n",
  2681. count, rsa_keys[testnum].bits, d);
  2682. rsa_results[testnum][0] = (double)count / d;
  2683. op_count = count;
  2684. }
  2685. for (i = 0; st && i < loopargs_len; i++) {
  2686. loopargs[i].rsa_verify_ctx[testnum] = EVP_PKEY_CTX_new(rsa_key,
  2687. NULL);
  2688. if (loopargs[i].rsa_verify_ctx[testnum] == NULL
  2689. || EVP_PKEY_verify_init(loopargs[i].rsa_verify_ctx[testnum]) <= 0
  2690. || EVP_PKEY_verify(loopargs[i].rsa_verify_ctx[testnum],
  2691. loopargs[i].buf2,
  2692. loopargs[i].sigsize,
  2693. loopargs[i].buf, 36) <= 0)
  2694. st = 0;
  2695. }
  2696. if (!st) {
  2697. BIO_printf(bio_err,
  2698. "RSA verify setup failure. No RSA verify will be done.\n");
  2699. ERR_print_errors(bio_err);
  2700. rsa_doit[testnum] = 0;
  2701. } else {
  2702. pkey_print_message("public", "rsa verify",
  2703. rsa_keys[testnum].bits, seconds.rsa);
  2704. Time_F(START);
  2705. count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
  2706. d = Time_F(STOP);
  2707. BIO_printf(bio_err,
  2708. mr ? "+R2:%ld:%d:%.2f\n"
  2709. : "%ld %u bits public RSA verify ops in %.2fs\n",
  2710. count, rsa_keys[testnum].bits, d);
  2711. rsa_results[testnum][1] = (double)count / d;
  2712. }
  2713. for (i = 0; st && i < loopargs_len; i++) {
  2714. loopargs[i].rsa_encrypt_ctx[testnum] = EVP_PKEY_CTX_new(rsa_key, NULL);
  2715. loopargs[i].encsize = loopargs[i].buflen;
  2716. if (loopargs[i].rsa_encrypt_ctx[testnum] == NULL
  2717. || EVP_PKEY_encrypt_init(loopargs[i].rsa_encrypt_ctx[testnum]) <= 0
  2718. || EVP_PKEY_encrypt(loopargs[i].rsa_encrypt_ctx[testnum],
  2719. loopargs[i].buf2,
  2720. &loopargs[i].encsize,
  2721. loopargs[i].buf, 36) <= 0)
  2722. st = 0;
  2723. }
  2724. if (!st) {
  2725. BIO_printf(bio_err,
  2726. "RSA encrypt setup failure. No RSA encrypt will be done.\n");
  2727. ERR_print_errors(bio_err);
  2728. op_count = 1;
  2729. } else {
  2730. pkey_print_message("private", "rsa encrypt",
  2731. rsa_keys[testnum].bits, seconds.rsa);
  2732. /* RSA_blinding_on(rsa_key[testnum],NULL); */
  2733. Time_F(START);
  2734. count = run_benchmark(async_jobs, RSA_encrypt_loop, loopargs);
  2735. d = Time_F(STOP);
  2736. BIO_printf(bio_err,
  2737. mr ? "+R3:%ld:%d:%.2f\n"
  2738. : "%ld %u bits public RSA encrypt ops in %.2fs\n",
  2739. count, rsa_keys[testnum].bits, d);
  2740. rsa_results[testnum][2] = (double)count / d;
  2741. op_count = count;
  2742. }
  2743. for (i = 0; st && i < loopargs_len; i++) {
  2744. loopargs[i].rsa_decrypt_ctx[testnum] = EVP_PKEY_CTX_new(rsa_key, NULL);
  2745. declen = loopargs[i].buflen;
  2746. if (loopargs[i].rsa_decrypt_ctx[testnum] == NULL
  2747. || EVP_PKEY_decrypt_init(loopargs[i].rsa_decrypt_ctx[testnum]) <= 0
  2748. || EVP_PKEY_decrypt(loopargs[i].rsa_decrypt_ctx[testnum],
  2749. loopargs[i].buf,
  2750. &declen,
  2751. loopargs[i].buf2,
  2752. loopargs[i].encsize) <= 0)
  2753. st = 0;
  2754. }
  2755. if (!st) {
  2756. BIO_printf(bio_err,
  2757. "RSA decrypt setup failure. No RSA decrypt will be done.\n");
  2758. ERR_print_errors(bio_err);
  2759. op_count = 1;
  2760. } else {
  2761. pkey_print_message("private", "rsa decrypt",
  2762. rsa_keys[testnum].bits, seconds.rsa);
  2763. /* RSA_blinding_on(rsa_key[testnum],NULL); */
  2764. Time_F(START);
  2765. count = run_benchmark(async_jobs, RSA_decrypt_loop, loopargs);
  2766. d = Time_F(STOP);
  2767. BIO_printf(bio_err,
  2768. mr ? "+R4:%ld:%d:%.2f\n"
  2769. : "%ld %u bits private RSA decrypt ops in %.2fs\n",
  2770. count, rsa_keys[testnum].bits, d);
  2771. rsa_results[testnum][3] = (double)count / d;
  2772. op_count = count;
  2773. }
  2774. if (op_count <= 1) {
  2775. /* if longer than 10s, don't do any more */
  2776. stop_it(rsa_doit, testnum);
  2777. }
  2778. EVP_PKEY_free(rsa_key);
  2779. }
  2780. for (testnum = 0; testnum < DSA_NUM; testnum++) {
  2781. EVP_PKEY *dsa_key = NULL;
  2782. int st;
  2783. if (!dsa_doit[testnum])
  2784. continue;
  2785. st = (dsa_key = get_dsa(dsa_bits[testnum])) != NULL;
  2786. for (i = 0; st && i < loopargs_len; i++) {
  2787. loopargs[i].dsa_sign_ctx[testnum] = EVP_PKEY_CTX_new(dsa_key,
  2788. NULL);
  2789. loopargs[i].sigsize = loopargs[i].buflen;
  2790. if (loopargs[i].dsa_sign_ctx[testnum] == NULL
  2791. || EVP_PKEY_sign_init(loopargs[i].dsa_sign_ctx[testnum]) <= 0
  2792. || EVP_PKEY_sign(loopargs[i].dsa_sign_ctx[testnum],
  2793. loopargs[i].buf2,
  2794. &loopargs[i].sigsize,
  2795. loopargs[i].buf, 20) <= 0)
  2796. st = 0;
  2797. }
  2798. if (!st) {
  2799. BIO_printf(bio_err,
  2800. "DSA sign setup failure. No DSA sign will be done.\n");
  2801. ERR_print_errors(bio_err);
  2802. op_count = 1;
  2803. } else {
  2804. pkey_print_message("sign", "dsa",
  2805. dsa_bits[testnum], seconds.dsa);
  2806. Time_F(START);
  2807. count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
  2808. d = Time_F(STOP);
  2809. BIO_printf(bio_err,
  2810. mr ? "+R5:%ld:%u:%.2f\n"
  2811. : "%ld %u bits DSA sign ops in %.2fs\n",
  2812. count, dsa_bits[testnum], d);
  2813. dsa_results[testnum][0] = (double)count / d;
  2814. op_count = count;
  2815. }
  2816. for (i = 0; st && i < loopargs_len; i++) {
  2817. loopargs[i].dsa_verify_ctx[testnum] = EVP_PKEY_CTX_new(dsa_key,
  2818. NULL);
  2819. if (loopargs[i].dsa_verify_ctx[testnum] == NULL
  2820. || EVP_PKEY_verify_init(loopargs[i].dsa_verify_ctx[testnum]) <= 0
  2821. || EVP_PKEY_verify(loopargs[i].dsa_verify_ctx[testnum],
  2822. loopargs[i].buf2,
  2823. loopargs[i].sigsize,
  2824. loopargs[i].buf, 36) <= 0)
  2825. st = 0;
  2826. }
  2827. if (!st) {
  2828. BIO_printf(bio_err,
  2829. "DSA verify setup failure. No DSA verify will be done.\n");
  2830. ERR_print_errors(bio_err);
  2831. dsa_doit[testnum] = 0;
  2832. } else {
  2833. pkey_print_message("verify", "dsa",
  2834. dsa_bits[testnum], seconds.dsa);
  2835. Time_F(START);
  2836. count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
  2837. d = Time_F(STOP);
  2838. BIO_printf(bio_err,
  2839. mr ? "+R6:%ld:%u:%.2f\n"
  2840. : "%ld %u bits DSA verify ops in %.2fs\n",
  2841. count, dsa_bits[testnum], d);
  2842. dsa_results[testnum][1] = (double)count / d;
  2843. }
  2844. if (op_count <= 1) {
  2845. /* if longer than 10s, don't do any more */
  2846. stop_it(dsa_doit, testnum);
  2847. }
  2848. EVP_PKEY_free(dsa_key);
  2849. }
  2850. for (testnum = 0; testnum < ECDSA_NUM; testnum++) {
  2851. EVP_PKEY *ecdsa_key = NULL;
  2852. int st;
  2853. if (!ecdsa_doit[testnum])
  2854. continue;
  2855. st = (ecdsa_key = get_ecdsa(&ec_curves[testnum])) != NULL;
  2856. for (i = 0; st && i < loopargs_len; i++) {
  2857. loopargs[i].ecdsa_sign_ctx[testnum] = EVP_PKEY_CTX_new(ecdsa_key,
  2858. NULL);
  2859. loopargs[i].sigsize = loopargs[i].buflen;
  2860. if (loopargs[i].ecdsa_sign_ctx[testnum] == NULL
  2861. || EVP_PKEY_sign_init(loopargs[i].ecdsa_sign_ctx[testnum]) <= 0
  2862. || EVP_PKEY_sign(loopargs[i].ecdsa_sign_ctx[testnum],
  2863. loopargs[i].buf2,
  2864. &loopargs[i].sigsize,
  2865. loopargs[i].buf, 20) <= 0)
  2866. st = 0;
  2867. }
  2868. if (!st) {
  2869. BIO_printf(bio_err,
  2870. "ECDSA sign setup failure. No ECDSA sign will be done.\n");
  2871. ERR_print_errors(bio_err);
  2872. op_count = 1;
  2873. } else {
  2874. pkey_print_message("sign", "ecdsa",
  2875. ec_curves[testnum].bits, seconds.ecdsa);
  2876. Time_F(START);
  2877. count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
  2878. d = Time_F(STOP);
  2879. BIO_printf(bio_err,
  2880. mr ? "+R7:%ld:%u:%.2f\n"
  2881. : "%ld %u bits ECDSA sign ops in %.2fs\n",
  2882. count, ec_curves[testnum].bits, d);
  2883. ecdsa_results[testnum][0] = (double)count / d;
  2884. op_count = count;
  2885. }
  2886. for (i = 0; st && i < loopargs_len; i++) {
  2887. loopargs[i].ecdsa_verify_ctx[testnum] = EVP_PKEY_CTX_new(ecdsa_key,
  2888. NULL);
  2889. if (loopargs[i].ecdsa_verify_ctx[testnum] == NULL
  2890. || EVP_PKEY_verify_init(loopargs[i].ecdsa_verify_ctx[testnum]) <= 0
  2891. || EVP_PKEY_verify(loopargs[i].ecdsa_verify_ctx[testnum],
  2892. loopargs[i].buf2,
  2893. loopargs[i].sigsize,
  2894. loopargs[i].buf, 20) <= 0)
  2895. st = 0;
  2896. }
  2897. if (!st) {
  2898. BIO_printf(bio_err,
  2899. "ECDSA verify setup failure. No ECDSA verify will be done.\n");
  2900. ERR_print_errors(bio_err);
  2901. ecdsa_doit[testnum] = 0;
  2902. } else {
  2903. pkey_print_message("verify", "ecdsa",
  2904. ec_curves[testnum].bits, seconds.ecdsa);
  2905. Time_F(START);
  2906. count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
  2907. d = Time_F(STOP);
  2908. BIO_printf(bio_err,
  2909. mr ? "+R8:%ld:%u:%.2f\n"
  2910. : "%ld %u bits ECDSA verify ops in %.2fs\n",
  2911. count, ec_curves[testnum].bits, d);
  2912. ecdsa_results[testnum][1] = (double)count / d;
  2913. }
  2914. if (op_count <= 1) {
  2915. /* if longer than 10s, don't do any more */
  2916. stop_it(ecdsa_doit, testnum);
  2917. }
  2918. }
  2919. for (testnum = 0; testnum < EC_NUM; testnum++) {
  2920. int ecdh_checks = 1;
  2921. if (!ecdh_doit[testnum])
  2922. continue;
  2923. for (i = 0; i < loopargs_len; i++) {
  2924. EVP_PKEY_CTX *test_ctx = NULL;
  2925. EVP_PKEY_CTX *ctx = NULL;
  2926. EVP_PKEY *key_A = NULL;
  2927. EVP_PKEY *key_B = NULL;
  2928. size_t outlen;
  2929. size_t test_outlen;
  2930. if ((key_A = get_ecdsa(&ec_curves[testnum])) == NULL /* generate secret key A */
  2931. || (key_B = get_ecdsa(&ec_curves[testnum])) == NULL /* generate secret key B */
  2932. || (ctx = EVP_PKEY_CTX_new(key_A, NULL)) == NULL /* derivation ctx from skeyA */
  2933. || EVP_PKEY_derive_init(ctx) <= 0 /* init derivation ctx */
  2934. || EVP_PKEY_derive_set_peer(ctx, key_B) <= 0 /* set peer pubkey in ctx */
  2935. || EVP_PKEY_derive(ctx, NULL, &outlen) <= 0 /* determine max length */
  2936. || outlen == 0 /* ensure outlen is a valid size */
  2937. || outlen > MAX_ECDH_SIZE /* avoid buffer overflow */) {
  2938. ecdh_checks = 0;
  2939. BIO_printf(bio_err, "ECDH key generation failure.\n");
  2940. ERR_print_errors(bio_err);
  2941. op_count = 1;
  2942. break;
  2943. }
  2944. /*
  2945. * Here we perform a test run, comparing the output of a*B and b*A;
  2946. * we try this here and assume that further EVP_PKEY_derive calls
  2947. * never fail, so we can skip checks in the actually benchmarked
  2948. * code, for maximum performance.
  2949. */
  2950. if ((test_ctx = EVP_PKEY_CTX_new(key_B, NULL)) == NULL /* test ctx from skeyB */
  2951. || EVP_PKEY_derive_init(test_ctx) <= 0 /* init derivation test_ctx */
  2952. || EVP_PKEY_derive_set_peer(test_ctx, key_A) <= 0 /* set peer pubkey in test_ctx */
  2953. || EVP_PKEY_derive(test_ctx, NULL, &test_outlen) <= 0 /* determine max length */
  2954. || EVP_PKEY_derive(ctx, loopargs[i].secret_a, &outlen) <= 0 /* compute a*B */
  2955. || EVP_PKEY_derive(test_ctx, loopargs[i].secret_b, &test_outlen) <= 0 /* compute b*A */
  2956. || test_outlen != outlen /* compare output length */) {
  2957. ecdh_checks = 0;
  2958. BIO_printf(bio_err, "ECDH computation failure.\n");
  2959. ERR_print_errors(bio_err);
  2960. op_count = 1;
  2961. break;
  2962. }
  2963. /* Compare the computation results: CRYPTO_memcmp() returns 0 if equal */
  2964. if (CRYPTO_memcmp(loopargs[i].secret_a,
  2965. loopargs[i].secret_b, outlen)) {
  2966. ecdh_checks = 0;
  2967. BIO_printf(bio_err, "ECDH computations don't match.\n");
  2968. ERR_print_errors(bio_err);
  2969. op_count = 1;
  2970. break;
  2971. }
  2972. loopargs[i].ecdh_ctx[testnum] = ctx;
  2973. loopargs[i].outlen[testnum] = outlen;
  2974. EVP_PKEY_free(key_A);
  2975. EVP_PKEY_free(key_B);
  2976. EVP_PKEY_CTX_free(test_ctx);
  2977. test_ctx = NULL;
  2978. }
  2979. if (ecdh_checks != 0) {
  2980. pkey_print_message("", "ecdh",
  2981. ec_curves[testnum].bits, seconds.ecdh);
  2982. Time_F(START);
  2983. count =
  2984. run_benchmark(async_jobs, ECDH_EVP_derive_key_loop, loopargs);
  2985. d = Time_F(STOP);
  2986. BIO_printf(bio_err,
  2987. mr ? "+R9:%ld:%d:%.2f\n" :
  2988. "%ld %u-bits ECDH ops in %.2fs\n", count,
  2989. ec_curves[testnum].bits, d);
  2990. ecdh_results[testnum][0] = (double)count / d;
  2991. op_count = count;
  2992. }
  2993. if (op_count <= 1) {
  2994. /* if longer than 10s, don't do any more */
  2995. stop_it(ecdh_doit, testnum);
  2996. }
  2997. }
  2998. #ifndef OPENSSL_NO_ECX
  2999. for (testnum = 0; testnum < EdDSA_NUM; testnum++) {
  3000. int st = 1;
  3001. EVP_PKEY *ed_pkey = NULL;
  3002. EVP_PKEY_CTX *ed_pctx = NULL;
  3003. if (!eddsa_doit[testnum])
  3004. continue; /* Ignore Curve */
  3005. for (i = 0; i < loopargs_len; i++) {
  3006. loopargs[i].eddsa_ctx[testnum] = EVP_MD_CTX_new();
  3007. if (loopargs[i].eddsa_ctx[testnum] == NULL) {
  3008. st = 0;
  3009. break;
  3010. }
  3011. loopargs[i].eddsa_ctx2[testnum] = EVP_MD_CTX_new();
  3012. if (loopargs[i].eddsa_ctx2[testnum] == NULL) {
  3013. st = 0;
  3014. break;
  3015. }
  3016. if ((ed_pctx = EVP_PKEY_CTX_new_id(ed_curves[testnum].nid,
  3017. NULL)) == NULL
  3018. || EVP_PKEY_keygen_init(ed_pctx) <= 0
  3019. || EVP_PKEY_keygen(ed_pctx, &ed_pkey) <= 0) {
  3020. st = 0;
  3021. EVP_PKEY_CTX_free(ed_pctx);
  3022. break;
  3023. }
  3024. EVP_PKEY_CTX_free(ed_pctx);
  3025. if (!EVP_DigestSignInit(loopargs[i].eddsa_ctx[testnum], NULL, NULL,
  3026. NULL, ed_pkey)) {
  3027. st = 0;
  3028. EVP_PKEY_free(ed_pkey);
  3029. break;
  3030. }
  3031. if (!EVP_DigestVerifyInit(loopargs[i].eddsa_ctx2[testnum], NULL,
  3032. NULL, NULL, ed_pkey)) {
  3033. st = 0;
  3034. EVP_PKEY_free(ed_pkey);
  3035. break;
  3036. }
  3037. EVP_PKEY_free(ed_pkey);
  3038. ed_pkey = NULL;
  3039. }
  3040. if (st == 0) {
  3041. BIO_printf(bio_err, "EdDSA failure.\n");
  3042. ERR_print_errors(bio_err);
  3043. op_count = 1;
  3044. } else {
  3045. for (i = 0; i < loopargs_len; i++) {
  3046. /* Perform EdDSA signature test */
  3047. loopargs[i].sigsize = ed_curves[testnum].sigsize;
  3048. st = EVP_DigestSign(loopargs[i].eddsa_ctx[testnum],
  3049. loopargs[i].buf2, &loopargs[i].sigsize,
  3050. loopargs[i].buf, 20);
  3051. if (st == 0)
  3052. break;
  3053. }
  3054. if (st == 0) {
  3055. BIO_printf(bio_err,
  3056. "EdDSA sign failure. No EdDSA sign will be done.\n");
  3057. ERR_print_errors(bio_err);
  3058. op_count = 1;
  3059. } else {
  3060. pkey_print_message("sign", ed_curves[testnum].name,
  3061. ed_curves[testnum].bits, seconds.eddsa);
  3062. Time_F(START);
  3063. count = run_benchmark(async_jobs, EdDSA_sign_loop, loopargs);
  3064. d = Time_F(STOP);
  3065. BIO_printf(bio_err,
  3066. mr ? "+R10:%ld:%u:%s:%.2f\n" :
  3067. "%ld %u bits %s sign ops in %.2fs \n",
  3068. count, ed_curves[testnum].bits,
  3069. ed_curves[testnum].name, d);
  3070. eddsa_results[testnum][0] = (double)count / d;
  3071. op_count = count;
  3072. }
  3073. /* Perform EdDSA verification test */
  3074. for (i = 0; i < loopargs_len; i++) {
  3075. st = EVP_DigestVerify(loopargs[i].eddsa_ctx2[testnum],
  3076. loopargs[i].buf2, loopargs[i].sigsize,
  3077. loopargs[i].buf, 20);
  3078. if (st != 1)
  3079. break;
  3080. }
  3081. if (st != 1) {
  3082. BIO_printf(bio_err,
  3083. "EdDSA verify failure. No EdDSA verify will be done.\n");
  3084. ERR_print_errors(bio_err);
  3085. eddsa_doit[testnum] = 0;
  3086. } else {
  3087. pkey_print_message("verify", ed_curves[testnum].name,
  3088. ed_curves[testnum].bits, seconds.eddsa);
  3089. Time_F(START);
  3090. count = run_benchmark(async_jobs, EdDSA_verify_loop, loopargs);
  3091. d = Time_F(STOP);
  3092. BIO_printf(bio_err,
  3093. mr ? "+R11:%ld:%u:%s:%.2f\n"
  3094. : "%ld %u bits %s verify ops in %.2fs\n",
  3095. count, ed_curves[testnum].bits,
  3096. ed_curves[testnum].name, d);
  3097. eddsa_results[testnum][1] = (double)count / d;
  3098. }
  3099. if (op_count <= 1) {
  3100. /* if longer than 10s, don't do any more */
  3101. stop_it(eddsa_doit, testnum);
  3102. }
  3103. }
  3104. }
  3105. #endif /* OPENSSL_NO_ECX */
  3106. #ifndef OPENSSL_NO_SM2
  3107. for (testnum = 0; testnum < SM2_NUM; testnum++) {
  3108. int st = 1;
  3109. EVP_PKEY *sm2_pkey = NULL;
  3110. if (!sm2_doit[testnum])
  3111. continue; /* Ignore Curve */
  3112. /* Init signing and verification */
  3113. for (i = 0; i < loopargs_len; i++) {
  3114. EVP_PKEY_CTX *sm2_pctx = NULL;
  3115. EVP_PKEY_CTX *sm2_vfy_pctx = NULL;
  3116. EVP_PKEY_CTX *pctx = NULL;
  3117. st = 0;
  3118. loopargs[i].sm2_ctx[testnum] = EVP_MD_CTX_new();
  3119. loopargs[i].sm2_vfy_ctx[testnum] = EVP_MD_CTX_new();
  3120. if (loopargs[i].sm2_ctx[testnum] == NULL
  3121. || loopargs[i].sm2_vfy_ctx[testnum] == NULL)
  3122. break;
  3123. sm2_pkey = NULL;
  3124. st = !((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SM2, NULL)) == NULL
  3125. || EVP_PKEY_keygen_init(pctx) <= 0
  3126. || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
  3127. sm2_curves[testnum].nid) <= 0
  3128. || EVP_PKEY_keygen(pctx, &sm2_pkey) <= 0);
  3129. EVP_PKEY_CTX_free(pctx);
  3130. if (st == 0)
  3131. break;
  3132. st = 0; /* set back to zero */
  3133. /* attach it sooner to rely on main final cleanup */
  3134. loopargs[i].sm2_pkey[testnum] = sm2_pkey;
  3135. loopargs[i].sigsize = EVP_PKEY_get_size(sm2_pkey);
  3136. sm2_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
  3137. sm2_vfy_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
  3138. if (sm2_pctx == NULL || sm2_vfy_pctx == NULL) {
  3139. EVP_PKEY_CTX_free(sm2_vfy_pctx);
  3140. break;
  3141. }
  3142. /* attach them directly to respective ctx */
  3143. EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_ctx[testnum], sm2_pctx);
  3144. EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_vfy_ctx[testnum], sm2_vfy_pctx);
  3145. /*
  3146. * No need to allow user to set an explicit ID here, just use
  3147. * the one defined in the 'draft-yang-tls-tl13-sm-suites' I-D.
  3148. */
  3149. if (EVP_PKEY_CTX_set1_id(sm2_pctx, SM2_ID, SM2_ID_LEN) != 1
  3150. || EVP_PKEY_CTX_set1_id(sm2_vfy_pctx, SM2_ID, SM2_ID_LEN) != 1)
  3151. break;
  3152. if (!EVP_DigestSignInit(loopargs[i].sm2_ctx[testnum], NULL,
  3153. EVP_sm3(), NULL, sm2_pkey))
  3154. break;
  3155. if (!EVP_DigestVerifyInit(loopargs[i].sm2_vfy_ctx[testnum], NULL,
  3156. EVP_sm3(), NULL, sm2_pkey))
  3157. break;
  3158. st = 1; /* mark loop as succeeded */
  3159. }
  3160. if (st == 0) {
  3161. BIO_printf(bio_err, "SM2 init failure.\n");
  3162. ERR_print_errors(bio_err);
  3163. op_count = 1;
  3164. } else {
  3165. for (i = 0; i < loopargs_len; i++) {
  3166. /* Perform SM2 signature test */
  3167. st = EVP_DigestSign(loopargs[i].sm2_ctx[testnum],
  3168. loopargs[i].buf2, &loopargs[i].sigsize,
  3169. loopargs[i].buf, 20);
  3170. if (st == 0)
  3171. break;
  3172. }
  3173. if (st == 0) {
  3174. BIO_printf(bio_err,
  3175. "SM2 sign failure. No SM2 sign will be done.\n");
  3176. ERR_print_errors(bio_err);
  3177. op_count = 1;
  3178. } else {
  3179. pkey_print_message("sign", sm2_curves[testnum].name,
  3180. sm2_curves[testnum].bits, seconds.sm2);
  3181. Time_F(START);
  3182. count = run_benchmark(async_jobs, SM2_sign_loop, loopargs);
  3183. d = Time_F(STOP);
  3184. BIO_printf(bio_err,
  3185. mr ? "+R12:%ld:%u:%s:%.2f\n" :
  3186. "%ld %u bits %s sign ops in %.2fs \n",
  3187. count, sm2_curves[testnum].bits,
  3188. sm2_curves[testnum].name, d);
  3189. sm2_results[testnum][0] = (double)count / d;
  3190. op_count = count;
  3191. }
  3192. /* Perform SM2 verification test */
  3193. for (i = 0; i < loopargs_len; i++) {
  3194. st = EVP_DigestVerify(loopargs[i].sm2_vfy_ctx[testnum],
  3195. loopargs[i].buf2, loopargs[i].sigsize,
  3196. loopargs[i].buf, 20);
  3197. if (st != 1)
  3198. break;
  3199. }
  3200. if (st != 1) {
  3201. BIO_printf(bio_err,
  3202. "SM2 verify failure. No SM2 verify will be done.\n");
  3203. ERR_print_errors(bio_err);
  3204. sm2_doit[testnum] = 0;
  3205. } else {
  3206. pkey_print_message("verify", sm2_curves[testnum].name,
  3207. sm2_curves[testnum].bits, seconds.sm2);
  3208. Time_F(START);
  3209. count = run_benchmark(async_jobs, SM2_verify_loop, loopargs);
  3210. d = Time_F(STOP);
  3211. BIO_printf(bio_err,
  3212. mr ? "+R13:%ld:%u:%s:%.2f\n"
  3213. : "%ld %u bits %s verify ops in %.2fs\n",
  3214. count, sm2_curves[testnum].bits,
  3215. sm2_curves[testnum].name, d);
  3216. sm2_results[testnum][1] = (double)count / d;
  3217. }
  3218. if (op_count <= 1) {
  3219. /* if longer than 10s, don't do any more */
  3220. for (testnum++; testnum < SM2_NUM; testnum++)
  3221. sm2_doit[testnum] = 0;
  3222. }
  3223. }
  3224. }
  3225. #endif /* OPENSSL_NO_SM2 */
  3226. #ifndef OPENSSL_NO_DH
  3227. for (testnum = 0; testnum < FFDH_NUM; testnum++) {
  3228. int ffdh_checks = 1;
  3229. if (!ffdh_doit[testnum])
  3230. continue;
  3231. for (i = 0; i < loopargs_len; i++) {
  3232. EVP_PKEY *pkey_A = NULL;
  3233. EVP_PKEY *pkey_B = NULL;
  3234. EVP_PKEY_CTX *ffdh_ctx = NULL;
  3235. EVP_PKEY_CTX *test_ctx = NULL;
  3236. size_t secret_size;
  3237. size_t test_out;
  3238. /* Ensure that the error queue is empty */
  3239. if (ERR_peek_error()) {
  3240. BIO_printf(bio_err,
  3241. "WARNING: the error queue contains previous unhandled errors.\n");
  3242. ERR_print_errors(bio_err);
  3243. }
  3244. pkey_A = EVP_PKEY_new();
  3245. if (!pkey_A) {
  3246. BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
  3247. ERR_print_errors(bio_err);
  3248. op_count = 1;
  3249. ffdh_checks = 0;
  3250. break;
  3251. }
  3252. pkey_B = EVP_PKEY_new();
  3253. if (!pkey_B) {
  3254. BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
  3255. ERR_print_errors(bio_err);
  3256. op_count = 1;
  3257. ffdh_checks = 0;
  3258. break;
  3259. }
  3260. ffdh_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, NULL);
  3261. if (!ffdh_ctx) {
  3262. BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
  3263. ERR_print_errors(bio_err);
  3264. op_count = 1;
  3265. ffdh_checks = 0;
  3266. break;
  3267. }
  3268. if (EVP_PKEY_keygen_init(ffdh_ctx) <= 0) {
  3269. BIO_printf(bio_err, "Error while initialising EVP_PKEY_CTX.\n");
  3270. ERR_print_errors(bio_err);
  3271. op_count = 1;
  3272. ffdh_checks = 0;
  3273. break;
  3274. }
  3275. if (EVP_PKEY_CTX_set_dh_nid(ffdh_ctx, ffdh_params[testnum].nid) <= 0) {
  3276. BIO_printf(bio_err, "Error setting DH key size for keygen.\n");
  3277. ERR_print_errors(bio_err);
  3278. op_count = 1;
  3279. ffdh_checks = 0;
  3280. break;
  3281. }
  3282. if (EVP_PKEY_keygen(ffdh_ctx, &pkey_A) <= 0 ||
  3283. EVP_PKEY_keygen(ffdh_ctx, &pkey_B) <= 0) {
  3284. BIO_printf(bio_err, "FFDH key generation failure.\n");
  3285. ERR_print_errors(bio_err);
  3286. op_count = 1;
  3287. ffdh_checks = 0;
  3288. break;
  3289. }
  3290. EVP_PKEY_CTX_free(ffdh_ctx);
  3291. /*
  3292. * check if the derivation works correctly both ways so that
  3293. * we know if future derive calls will fail, and we can skip
  3294. * error checking in benchmarked code
  3295. */
  3296. ffdh_ctx = EVP_PKEY_CTX_new(pkey_A, NULL);
  3297. if (ffdh_ctx == NULL) {
  3298. BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
  3299. ERR_print_errors(bio_err);
  3300. op_count = 1;
  3301. ffdh_checks = 0;
  3302. break;
  3303. }
  3304. if (EVP_PKEY_derive_init(ffdh_ctx) <= 0) {
  3305. BIO_printf(bio_err, "FFDH derivation context init failure.\n");
  3306. ERR_print_errors(bio_err);
  3307. op_count = 1;
  3308. ffdh_checks = 0;
  3309. break;
  3310. }
  3311. if (EVP_PKEY_derive_set_peer(ffdh_ctx, pkey_B) <= 0) {
  3312. BIO_printf(bio_err, "Assigning peer key for derivation failed.\n");
  3313. ERR_print_errors(bio_err);
  3314. op_count = 1;
  3315. ffdh_checks = 0;
  3316. break;
  3317. }
  3318. if (EVP_PKEY_derive(ffdh_ctx, NULL, &secret_size) <= 0) {
  3319. BIO_printf(bio_err, "Checking size of shared secret failed.\n");
  3320. ERR_print_errors(bio_err);
  3321. op_count = 1;
  3322. ffdh_checks = 0;
  3323. break;
  3324. }
  3325. if (secret_size > MAX_FFDH_SIZE) {
  3326. BIO_printf(bio_err, "Assertion failure: shared secret too large.\n");
  3327. op_count = 1;
  3328. ffdh_checks = 0;
  3329. break;
  3330. }
  3331. if (EVP_PKEY_derive(ffdh_ctx,
  3332. loopargs[i].secret_ff_a,
  3333. &secret_size) <= 0) {
  3334. BIO_printf(bio_err, "Shared secret derive failure.\n");
  3335. ERR_print_errors(bio_err);
  3336. op_count = 1;
  3337. ffdh_checks = 0;
  3338. break;
  3339. }
  3340. /* Now check from side B */
  3341. test_ctx = EVP_PKEY_CTX_new(pkey_B, NULL);
  3342. if (!test_ctx) {
  3343. BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
  3344. ERR_print_errors(bio_err);
  3345. op_count = 1;
  3346. ffdh_checks = 0;
  3347. break;
  3348. }
  3349. if (EVP_PKEY_derive_init(test_ctx) <= 0 ||
  3350. EVP_PKEY_derive_set_peer(test_ctx, pkey_A) <= 0 ||
  3351. EVP_PKEY_derive(test_ctx, NULL, &test_out) <= 0 ||
  3352. EVP_PKEY_derive(test_ctx, loopargs[i].secret_ff_b, &test_out) <= 0 ||
  3353. test_out != secret_size) {
  3354. BIO_printf(bio_err, "FFDH computation failure.\n");
  3355. op_count = 1;
  3356. ffdh_checks = 0;
  3357. break;
  3358. }
  3359. /* compare the computed secrets */
  3360. if (CRYPTO_memcmp(loopargs[i].secret_ff_a,
  3361. loopargs[i].secret_ff_b, secret_size)) {
  3362. BIO_printf(bio_err, "FFDH computations don't match.\n");
  3363. ERR_print_errors(bio_err);
  3364. op_count = 1;
  3365. ffdh_checks = 0;
  3366. break;
  3367. }
  3368. loopargs[i].ffdh_ctx[testnum] = ffdh_ctx;
  3369. EVP_PKEY_free(pkey_A);
  3370. pkey_A = NULL;
  3371. EVP_PKEY_free(pkey_B);
  3372. pkey_B = NULL;
  3373. EVP_PKEY_CTX_free(test_ctx);
  3374. test_ctx = NULL;
  3375. }
  3376. if (ffdh_checks != 0) {
  3377. pkey_print_message("", "ffdh",
  3378. ffdh_params[testnum].bits, seconds.ffdh);
  3379. Time_F(START);
  3380. count =
  3381. run_benchmark(async_jobs, FFDH_derive_key_loop, loopargs);
  3382. d = Time_F(STOP);
  3383. BIO_printf(bio_err,
  3384. mr ? "+R14:%ld:%d:%.2f\n" :
  3385. "%ld %u-bits FFDH ops in %.2fs\n", count,
  3386. ffdh_params[testnum].bits, d);
  3387. ffdh_results[testnum][0] = (double)count / d;
  3388. op_count = count;
  3389. }
  3390. if (op_count <= 1) {
  3391. /* if longer than 10s, don't do any more */
  3392. stop_it(ffdh_doit, testnum);
  3393. }
  3394. }
  3395. #endif /* OPENSSL_NO_DH */
  3396. for (testnum = 0; testnum < kems_algs_len; testnum++) {
  3397. int kem_checks = 1;
  3398. const char *kem_name = kems_algname[testnum];
  3399. if (!kems_doit[testnum] || !do_kems)
  3400. continue;
  3401. for (i = 0; i < loopargs_len; i++) {
  3402. EVP_PKEY *pkey = NULL;
  3403. EVP_PKEY_CTX *kem_gen_ctx = NULL;
  3404. EVP_PKEY_CTX *kem_encaps_ctx = NULL;
  3405. EVP_PKEY_CTX *kem_decaps_ctx = NULL;
  3406. size_t send_secret_len, out_len;
  3407. size_t rcv_secret_len;
  3408. unsigned char *out = NULL, *send_secret = NULL, *rcv_secret;
  3409. unsigned int bits;
  3410. char *name;
  3411. char sfx[MAX_ALGNAME_SUFFIX];
  3412. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  3413. int use_params = 0;
  3414. enum kem_type_t { KEM_RSA = 1, KEM_EC, KEM_X25519, KEM_X448 } kem_type;
  3415. /* no string after rsa<bitcnt> permitted: */
  3416. if (strlen(kem_name) < MAX_ALGNAME_SUFFIX + 4 /* rsa+digit */
  3417. && sscanf(kem_name, "rsa%u%s", &bits, sfx) == 1)
  3418. kem_type = KEM_RSA;
  3419. else if (strncmp(kem_name, "EC", 2) == 0)
  3420. kem_type = KEM_EC;
  3421. else if (strcmp(kem_name, "X25519") == 0)
  3422. kem_type = KEM_X25519;
  3423. else if (strcmp(kem_name, "X448") == 0)
  3424. kem_type = KEM_X448;
  3425. else kem_type = 0;
  3426. if (ERR_peek_error()) {
  3427. BIO_printf(bio_err,
  3428. "WARNING: the error queue contains previous unhandled errors.\n");
  3429. ERR_print_errors(bio_err);
  3430. }
  3431. if (kem_type == KEM_RSA) {
  3432. params[0] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_RSA_BITS,
  3433. &bits);
  3434. use_params = 1;
  3435. } else if (kem_type == KEM_EC) {
  3436. name = (char *)(kem_name + 2);
  3437. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  3438. name, 0);
  3439. use_params = 1;
  3440. }
  3441. kem_gen_ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(),
  3442. (kem_type == KEM_RSA) ? "RSA":
  3443. (kem_type == KEM_EC) ? "EC":
  3444. kem_name,
  3445. app_get0_propq());
  3446. if ((!kem_gen_ctx || EVP_PKEY_keygen_init(kem_gen_ctx) <= 0)
  3447. || (use_params
  3448. && EVP_PKEY_CTX_set_params(kem_gen_ctx, params) <= 0)) {
  3449. BIO_printf(bio_err, "Error initializing keygen ctx for %s.\n",
  3450. kem_name);
  3451. goto kem_err_break;
  3452. }
  3453. if (EVP_PKEY_keygen(kem_gen_ctx, &pkey) <= 0) {
  3454. BIO_printf(bio_err, "Error while generating KEM EVP_PKEY.\n");
  3455. goto kem_err_break;
  3456. }
  3457. /* Now prepare encaps data structs */
  3458. kem_encaps_ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(),
  3459. pkey,
  3460. app_get0_propq());
  3461. if (kem_encaps_ctx == NULL
  3462. || EVP_PKEY_encapsulate_init(kem_encaps_ctx, NULL) <= 0
  3463. || (kem_type == KEM_RSA
  3464. && EVP_PKEY_CTX_set_kem_op(kem_encaps_ctx, "RSASVE") <= 0)
  3465. || ((kem_type == KEM_EC
  3466. || kem_type == KEM_X25519
  3467. || kem_type == KEM_X448)
  3468. && EVP_PKEY_CTX_set_kem_op(kem_encaps_ctx, "DHKEM") <= 0)
  3469. || EVP_PKEY_encapsulate(kem_encaps_ctx, NULL, &out_len,
  3470. NULL, &send_secret_len) <= 0) {
  3471. BIO_printf(bio_err,
  3472. "Error while initializing encaps data structs for %s.\n",
  3473. kem_name);
  3474. goto kem_err_break;
  3475. }
  3476. out = app_malloc(out_len, "encaps result");
  3477. send_secret = app_malloc(send_secret_len, "encaps secret");
  3478. if (out == NULL || send_secret == NULL) {
  3479. BIO_printf(bio_err, "MemAlloc error in encaps for %s.\n", kem_name);
  3480. goto kem_err_break;
  3481. }
  3482. if (EVP_PKEY_encapsulate(kem_encaps_ctx, out, &out_len,
  3483. send_secret, &send_secret_len) <= 0) {
  3484. BIO_printf(bio_err, "Encaps error for %s.\n", kem_name);
  3485. goto kem_err_break;
  3486. }
  3487. /* Now prepare decaps data structs */
  3488. kem_decaps_ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(),
  3489. pkey,
  3490. app_get0_propq());
  3491. if (kem_decaps_ctx == NULL
  3492. || EVP_PKEY_decapsulate_init(kem_decaps_ctx, NULL) <= 0
  3493. || (kem_type == KEM_RSA
  3494. && EVP_PKEY_CTX_set_kem_op(kem_decaps_ctx, "RSASVE") <= 0)
  3495. || ((kem_type == KEM_EC
  3496. || kem_type == KEM_X25519
  3497. || kem_type == KEM_X448)
  3498. && EVP_PKEY_CTX_set_kem_op(kem_decaps_ctx, "DHKEM") <= 0)
  3499. || EVP_PKEY_decapsulate(kem_decaps_ctx, NULL, &rcv_secret_len,
  3500. out, out_len) <= 0) {
  3501. BIO_printf(bio_err,
  3502. "Error while initializing decaps data structs for %s.\n",
  3503. kem_name);
  3504. goto kem_err_break;
  3505. }
  3506. rcv_secret = app_malloc(rcv_secret_len, "KEM decaps secret");
  3507. if (rcv_secret == NULL) {
  3508. BIO_printf(bio_err, "MemAlloc failure in decaps for %s.\n",
  3509. kem_name);
  3510. goto kem_err_break;
  3511. }
  3512. if (EVP_PKEY_decapsulate(kem_decaps_ctx, rcv_secret,
  3513. &rcv_secret_len, out, out_len) <= 0
  3514. || rcv_secret_len != send_secret_len
  3515. || memcmp(send_secret, rcv_secret, send_secret_len)) {
  3516. BIO_printf(bio_err, "Decaps error for %s.\n", kem_name);
  3517. goto kem_err_break;
  3518. }
  3519. loopargs[i].kem_gen_ctx[testnum] = kem_gen_ctx;
  3520. loopargs[i].kem_encaps_ctx[testnum] = kem_encaps_ctx;
  3521. loopargs[i].kem_decaps_ctx[testnum] = kem_decaps_ctx;
  3522. loopargs[i].kem_out_len[testnum] = out_len;
  3523. loopargs[i].kem_secret_len[testnum] = send_secret_len;
  3524. loopargs[i].kem_out[testnum] = out;
  3525. loopargs[i].kem_send_secret[testnum] = send_secret;
  3526. loopargs[i].kem_rcv_secret[testnum] = rcv_secret;
  3527. EVP_PKEY_free(pkey);
  3528. pkey = NULL;
  3529. continue;
  3530. kem_err_break:
  3531. ERR_print_errors(bio_err);
  3532. EVP_PKEY_free(pkey);
  3533. op_count = 1;
  3534. kem_checks = 0;
  3535. break;
  3536. }
  3537. if (kem_checks != 0) {
  3538. kskey_print_message(kem_name, "keygen", seconds.kem);
  3539. Time_F(START);
  3540. count =
  3541. run_benchmark(async_jobs, KEM_keygen_loop, loopargs);
  3542. d = Time_F(STOP);
  3543. BIO_printf(bio_err,
  3544. mr ? "+R15:%ld:%s:%.2f\n" :
  3545. "%ld %s KEM keygen ops in %.2fs\n", count,
  3546. kem_name, d);
  3547. kems_results[testnum][0] = (double)count / d;
  3548. op_count = count;
  3549. kskey_print_message(kem_name, "encaps", seconds.kem);
  3550. Time_F(START);
  3551. count =
  3552. run_benchmark(async_jobs, KEM_encaps_loop, loopargs);
  3553. d = Time_F(STOP);
  3554. BIO_printf(bio_err,
  3555. mr ? "+R16:%ld:%s:%.2f\n" :
  3556. "%ld %s KEM encaps ops in %.2fs\n", count,
  3557. kem_name, d);
  3558. kems_results[testnum][1] = (double)count / d;
  3559. op_count = count;
  3560. kskey_print_message(kem_name, "decaps", seconds.kem);
  3561. Time_F(START);
  3562. count =
  3563. run_benchmark(async_jobs, KEM_decaps_loop, loopargs);
  3564. d = Time_F(STOP);
  3565. BIO_printf(bio_err,
  3566. mr ? "+R17:%ld:%s:%.2f\n" :
  3567. "%ld %s KEM decaps ops in %.2fs\n", count,
  3568. kem_name, d);
  3569. kems_results[testnum][2] = (double)count / d;
  3570. op_count = count;
  3571. }
  3572. if (op_count <= 1) {
  3573. /* if longer than 10s, don't do any more */
  3574. stop_it(kems_doit, testnum);
  3575. }
  3576. }
  3577. for (testnum = 0; testnum < sigs_algs_len; testnum++) {
  3578. int sig_checks = 1;
  3579. const char *sig_name = sigs_algname[testnum];
  3580. if (!sigs_doit[testnum] || !do_sigs)
  3581. continue;
  3582. for (i = 0; i < loopargs_len; i++) {
  3583. EVP_PKEY *pkey = NULL;
  3584. EVP_PKEY_CTX *ctx_params = NULL;
  3585. EVP_PKEY* pkey_params = NULL;
  3586. EVP_PKEY_CTX *sig_gen_ctx = NULL;
  3587. EVP_PKEY_CTX *sig_sign_ctx = NULL;
  3588. EVP_PKEY_CTX *sig_verify_ctx = NULL;
  3589. unsigned char md[SHA256_DIGEST_LENGTH];
  3590. unsigned char *sig;
  3591. char sfx[MAX_ALGNAME_SUFFIX];
  3592. size_t md_len = SHA256_DIGEST_LENGTH;
  3593. size_t max_sig_len, sig_len;
  3594. unsigned int bits;
  3595. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  3596. int use_params = 0;
  3597. /* only sign little data to avoid measuring digest performance */
  3598. memset(md, 0, SHA256_DIGEST_LENGTH);
  3599. if (ERR_peek_error()) {
  3600. BIO_printf(bio_err,
  3601. "WARNING: the error queue contains previous unhandled errors.\n");
  3602. ERR_print_errors(bio_err);
  3603. }
  3604. /* no string after rsa<bitcnt> permitted: */
  3605. if (strlen(sig_name) < MAX_ALGNAME_SUFFIX + 4 /* rsa+digit */
  3606. && sscanf(sig_name, "rsa%u%s", &bits, sfx) == 1) {
  3607. params[0] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_RSA_BITS,
  3608. &bits);
  3609. use_params = 1;
  3610. }
  3611. if (strncmp(sig_name, "dsa", 3) == 0) {
  3612. ctx_params = EVP_PKEY_CTX_new_id(EVP_PKEY_DSA, NULL);
  3613. if (ctx_params == NULL
  3614. || EVP_PKEY_paramgen_init(ctx_params) <= 0
  3615. || EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx_params,
  3616. atoi(sig_name + 3)) <= 0
  3617. || EVP_PKEY_paramgen(ctx_params, &pkey_params) <= 0
  3618. || (sig_gen_ctx = EVP_PKEY_CTX_new(pkey_params, NULL)) == NULL
  3619. || EVP_PKEY_keygen_init(sig_gen_ctx) <= 0) {
  3620. BIO_printf(bio_err,
  3621. "Error initializing classic keygen ctx for %s.\n",
  3622. sig_name);
  3623. goto sig_err_break;
  3624. }
  3625. }
  3626. if (sig_gen_ctx == NULL)
  3627. sig_gen_ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(),
  3628. use_params == 1 ? "RSA" : sig_name,
  3629. app_get0_propq());
  3630. if (!sig_gen_ctx || EVP_PKEY_keygen_init(sig_gen_ctx) <= 0
  3631. || (use_params &&
  3632. EVP_PKEY_CTX_set_params(sig_gen_ctx, params) <= 0)) {
  3633. BIO_printf(bio_err, "Error initializing keygen ctx for %s.\n",
  3634. sig_name);
  3635. goto sig_err_break;
  3636. }
  3637. if (EVP_PKEY_keygen(sig_gen_ctx, &pkey) <= 0) {
  3638. BIO_printf(bio_err,
  3639. "Error while generating signature EVP_PKEY for %s.\n",
  3640. sig_name);
  3641. goto sig_err_break;
  3642. }
  3643. /* Now prepare signature data structs */
  3644. sig_sign_ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(),
  3645. pkey,
  3646. app_get0_propq());
  3647. if (sig_sign_ctx == NULL
  3648. || EVP_PKEY_sign_init(sig_sign_ctx) <= 0
  3649. || (use_params == 1
  3650. && (EVP_PKEY_CTX_set_rsa_padding(sig_sign_ctx,
  3651. RSA_PKCS1_PADDING) <= 0))
  3652. || EVP_PKEY_sign(sig_sign_ctx, NULL, &max_sig_len,
  3653. md, md_len) <= 0) {
  3654. BIO_printf(bio_err,
  3655. "Error while initializing signing data structs for %s.\n",
  3656. sig_name);
  3657. goto sig_err_break;
  3658. }
  3659. sig = app_malloc(sig_len = max_sig_len, "signature buffer");
  3660. if (sig == NULL) {
  3661. BIO_printf(bio_err, "MemAlloc error in sign for %s.\n", sig_name);
  3662. goto sig_err_break;
  3663. }
  3664. if (EVP_PKEY_sign(sig_sign_ctx, sig, &sig_len, md, md_len) <= 0) {
  3665. BIO_printf(bio_err, "Signing error for %s.\n", sig_name);
  3666. goto sig_err_break;
  3667. }
  3668. /* Now prepare verify data structs */
  3669. memset(md, 0, SHA256_DIGEST_LENGTH);
  3670. sig_verify_ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(),
  3671. pkey,
  3672. app_get0_propq());
  3673. if (sig_verify_ctx == NULL
  3674. || EVP_PKEY_verify_init(sig_verify_ctx) <= 0
  3675. || (use_params == 1
  3676. && (EVP_PKEY_CTX_set_rsa_padding(sig_verify_ctx,
  3677. RSA_PKCS1_PADDING) <= 0))) {
  3678. BIO_printf(bio_err,
  3679. "Error while initializing verify data structs for %s.\n",
  3680. sig_name);
  3681. goto sig_err_break;
  3682. }
  3683. if (EVP_PKEY_verify(sig_verify_ctx, sig, sig_len, md, md_len) <= 0) {
  3684. BIO_printf(bio_err, "Verify error for %s.\n", sig_name);
  3685. goto sig_err_break;
  3686. }
  3687. if (EVP_PKEY_verify(sig_verify_ctx, sig, sig_len, md, md_len) <= 0) {
  3688. BIO_printf(bio_err, "Verify 2 error for %s.\n", sig_name);
  3689. goto sig_err_break;
  3690. }
  3691. loopargs[i].sig_gen_ctx[testnum] = sig_gen_ctx;
  3692. loopargs[i].sig_sign_ctx[testnum] = sig_sign_ctx;
  3693. loopargs[i].sig_verify_ctx[testnum] = sig_verify_ctx;
  3694. loopargs[i].sig_max_sig_len[testnum] = max_sig_len;
  3695. loopargs[i].sig_act_sig_len[testnum] = sig_len;
  3696. loopargs[i].sig_sig[testnum] = sig;
  3697. EVP_PKEY_free(pkey);
  3698. pkey = NULL;
  3699. continue;
  3700. sig_err_break:
  3701. ERR_print_errors(bio_err);
  3702. EVP_PKEY_free(pkey);
  3703. op_count = 1;
  3704. sig_checks = 0;
  3705. break;
  3706. }
  3707. if (sig_checks != 0) {
  3708. kskey_print_message(sig_name, "keygen", seconds.sig);
  3709. Time_F(START);
  3710. count = run_benchmark(async_jobs, SIG_keygen_loop, loopargs);
  3711. d = Time_F(STOP);
  3712. BIO_printf(bio_err,
  3713. mr ? "+R18:%ld:%s:%.2f\n" :
  3714. "%ld %s signature keygen ops in %.2fs\n", count,
  3715. sig_name, d);
  3716. sigs_results[testnum][0] = (double)count / d;
  3717. op_count = count;
  3718. kskey_print_message(sig_name, "signs", seconds.sig);
  3719. Time_F(START);
  3720. count =
  3721. run_benchmark(async_jobs, SIG_sign_loop, loopargs);
  3722. d = Time_F(STOP);
  3723. BIO_printf(bio_err,
  3724. mr ? "+R19:%ld:%s:%.2f\n" :
  3725. "%ld %s signature sign ops in %.2fs\n", count,
  3726. sig_name, d);
  3727. sigs_results[testnum][1] = (double)count / d;
  3728. op_count = count;
  3729. kskey_print_message(sig_name, "verify", seconds.sig);
  3730. Time_F(START);
  3731. count =
  3732. run_benchmark(async_jobs, SIG_verify_loop, loopargs);
  3733. d = Time_F(STOP);
  3734. BIO_printf(bio_err,
  3735. mr ? "+R20:%ld:%s:%.2f\n" :
  3736. "%ld %s signature verify ops in %.2fs\n", count,
  3737. sig_name, d);
  3738. sigs_results[testnum][2] = (double)count / d;
  3739. op_count = count;
  3740. }
  3741. if (op_count <= 1)
  3742. stop_it(sigs_doit, testnum);
  3743. }
  3744. #ifndef NO_FORK
  3745. show_res:
  3746. #endif
  3747. if (!mr) {
  3748. printf("version: %s\n", OpenSSL_version(OPENSSL_FULL_VERSION_STRING));
  3749. printf("%s\n", OpenSSL_version(OPENSSL_BUILT_ON));
  3750. printf("options: %s\n", BN_options());
  3751. printf("%s\n", OpenSSL_version(OPENSSL_CFLAGS));
  3752. printf("%s\n", OpenSSL_version(OPENSSL_CPU_INFO));
  3753. }
  3754. if (pr_header) {
  3755. if (mr) {
  3756. printf("+H");
  3757. } else {
  3758. printf("The 'numbers' are in 1000s of bytes per second processed.\n");
  3759. printf("type ");
  3760. }
  3761. for (testnum = 0; testnum < size_num; testnum++)
  3762. printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
  3763. printf("\n");
  3764. }
  3765. for (k = 0; k < ALGOR_NUM; k++) {
  3766. const char *alg_name = names[k];
  3767. if (!doit[k])
  3768. continue;
  3769. if (k == D_EVP) {
  3770. if (evp_cipher == NULL)
  3771. alg_name = evp_md_name;
  3772. else if ((alg_name = EVP_CIPHER_get0_name(evp_cipher)) == NULL)
  3773. app_bail_out("failed to get name of cipher '%s'\n", evp_cipher);
  3774. }
  3775. if (mr)
  3776. printf("+F:%u:%s", k, alg_name);
  3777. else
  3778. printf("%-13s", alg_name);
  3779. for (testnum = 0; testnum < size_num; testnum++) {
  3780. if (results[k][testnum] > 10000 && !mr)
  3781. printf(" %11.2fk", results[k][testnum] / 1e3);
  3782. else
  3783. printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
  3784. }
  3785. printf("\n");
  3786. }
  3787. testnum = 1;
  3788. for (k = 0; k < RSA_NUM; k++) {
  3789. if (!rsa_doit[k])
  3790. continue;
  3791. if (testnum && !mr) {
  3792. printf("%19ssign verify encrypt decrypt sign/s verify/s encr./s decr./s\n", " ");
  3793. testnum = 0;
  3794. }
  3795. if (mr)
  3796. printf("+F2:%u:%u:%f:%f:%f:%f\n",
  3797. k, rsa_keys[k].bits, rsa_results[k][0], rsa_results[k][1],
  3798. rsa_results[k][2], rsa_results[k][3]);
  3799. else
  3800. printf("rsa %5u bits %8.6fs %8.6fs %8.6fs %8.6fs %8.1f %8.1f %8.1f %8.1f\n",
  3801. rsa_keys[k].bits, 1.0 / rsa_results[k][0],
  3802. 1.0 / rsa_results[k][1], 1.0 / rsa_results[k][2],
  3803. 1.0 / rsa_results[k][3],
  3804. rsa_results[k][0], rsa_results[k][1],
  3805. rsa_results[k][2], rsa_results[k][3]);
  3806. }
  3807. testnum = 1;
  3808. for (k = 0; k < DSA_NUM; k++) {
  3809. if (!dsa_doit[k])
  3810. continue;
  3811. if (testnum && !mr) {
  3812. printf("%18ssign verify sign/s verify/s\n", " ");
  3813. testnum = 0;
  3814. }
  3815. if (mr)
  3816. printf("+F3:%u:%u:%f:%f\n",
  3817. k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
  3818. else
  3819. printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
  3820. dsa_bits[k], 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1],
  3821. dsa_results[k][0], dsa_results[k][1]);
  3822. }
  3823. testnum = 1;
  3824. for (k = 0; k < OSSL_NELEM(ecdsa_doit); k++) {
  3825. if (!ecdsa_doit[k])
  3826. continue;
  3827. if (testnum && !mr) {
  3828. printf("%30ssign verify sign/s verify/s\n", " ");
  3829. testnum = 0;
  3830. }
  3831. if (mr)
  3832. printf("+F4:%u:%u:%f:%f\n",
  3833. k, ec_curves[k].bits,
  3834. ecdsa_results[k][0], ecdsa_results[k][1]);
  3835. else
  3836. printf("%4u bits ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
  3837. ec_curves[k].bits, ec_curves[k].name,
  3838. 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],
  3839. ecdsa_results[k][0], ecdsa_results[k][1]);
  3840. }
  3841. testnum = 1;
  3842. for (k = 0; k < EC_NUM; k++) {
  3843. if (!ecdh_doit[k])
  3844. continue;
  3845. if (testnum && !mr) {
  3846. printf("%30sop op/s\n", " ");
  3847. testnum = 0;
  3848. }
  3849. if (mr)
  3850. printf("+F5:%u:%u:%f:%f\n",
  3851. k, ec_curves[k].bits,
  3852. ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
  3853. else
  3854. printf("%4u bits ecdh (%s) %8.4fs %8.1f\n",
  3855. ec_curves[k].bits, ec_curves[k].name,
  3856. 1.0 / ecdh_results[k][0], ecdh_results[k][0]);
  3857. }
  3858. #ifndef OPENSSL_NO_ECX
  3859. testnum = 1;
  3860. for (k = 0; k < OSSL_NELEM(eddsa_doit); k++) {
  3861. if (!eddsa_doit[k])
  3862. continue;
  3863. if (testnum && !mr) {
  3864. printf("%30ssign verify sign/s verify/s\n", " ");
  3865. testnum = 0;
  3866. }
  3867. if (mr)
  3868. printf("+F6:%u:%u:%s:%f:%f\n",
  3869. k, ed_curves[k].bits, ed_curves[k].name,
  3870. eddsa_results[k][0], eddsa_results[k][1]);
  3871. else
  3872. printf("%4u bits EdDSA (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
  3873. ed_curves[k].bits, ed_curves[k].name,
  3874. 1.0 / eddsa_results[k][0], 1.0 / eddsa_results[k][1],
  3875. eddsa_results[k][0], eddsa_results[k][1]);
  3876. }
  3877. #endif /* OPENSSL_NO_ECX */
  3878. #ifndef OPENSSL_NO_SM2
  3879. testnum = 1;
  3880. for (k = 0; k < OSSL_NELEM(sm2_doit); k++) {
  3881. if (!sm2_doit[k])
  3882. continue;
  3883. if (testnum && !mr) {
  3884. printf("%30ssign verify sign/s verify/s\n", " ");
  3885. testnum = 0;
  3886. }
  3887. if (mr)
  3888. printf("+F7:%u:%u:%s:%f:%f\n",
  3889. k, sm2_curves[k].bits, sm2_curves[k].name,
  3890. sm2_results[k][0], sm2_results[k][1]);
  3891. else
  3892. printf("%4u bits SM2 (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
  3893. sm2_curves[k].bits, sm2_curves[k].name,
  3894. 1.0 / sm2_results[k][0], 1.0 / sm2_results[k][1],
  3895. sm2_results[k][0], sm2_results[k][1]);
  3896. }
  3897. #endif
  3898. #ifndef OPENSSL_NO_DH
  3899. testnum = 1;
  3900. for (k = 0; k < FFDH_NUM; k++) {
  3901. if (!ffdh_doit[k])
  3902. continue;
  3903. if (testnum && !mr) {
  3904. printf("%23sop op/s\n", " ");
  3905. testnum = 0;
  3906. }
  3907. if (mr)
  3908. printf("+F8:%u:%u:%f:%f\n",
  3909. k, ffdh_params[k].bits,
  3910. ffdh_results[k][0], 1.0 / ffdh_results[k][0]);
  3911. else
  3912. printf("%4u bits ffdh %8.4fs %8.1f\n",
  3913. ffdh_params[k].bits,
  3914. 1.0 / ffdh_results[k][0], ffdh_results[k][0]);
  3915. }
  3916. #endif /* OPENSSL_NO_DH */
  3917. testnum = 1;
  3918. for (k = 0; k < kems_algs_len; k++) {
  3919. const char *kem_name = kems_algname[k];
  3920. if (!kems_doit[k] || !do_kems)
  3921. continue;
  3922. if (testnum && !mr) {
  3923. printf("%31skeygen encaps decaps keygens/s encaps/s decaps/s\n", " ");
  3924. testnum = 0;
  3925. }
  3926. if (mr)
  3927. printf("+F9:%u:%f:%f:%f\n",
  3928. k, kems_results[k][0], kems_results[k][1],
  3929. kems_results[k][2]);
  3930. else
  3931. printf("%27s %8.6fs %8.6fs %8.6fs %9.1f %9.1f %9.1f\n", kem_name,
  3932. 1.0 / kems_results[k][0],
  3933. 1.0 / kems_results[k][1], 1.0 / kems_results[k][2],
  3934. kems_results[k][0], kems_results[k][1], kems_results[k][2]);
  3935. }
  3936. ret = 0;
  3937. testnum = 1;
  3938. for (k = 0; k < sigs_algs_len; k++) {
  3939. const char *sig_name = sigs_algname[k];
  3940. if (!sigs_doit[k] || !do_sigs)
  3941. continue;
  3942. if (testnum && !mr) {
  3943. printf("%31skeygen signs verify keygens/s sign/s verify/s\n", " ");
  3944. testnum = 0;
  3945. }
  3946. if (mr)
  3947. printf("+F10:%u:%f:%f:%f\n",
  3948. k, sigs_results[k][0], sigs_results[k][1],
  3949. sigs_results[k][2]);
  3950. else
  3951. printf("%27s %8.6fs %8.6fs %8.6fs %9.1f %9.1f %9.1f\n", sig_name,
  3952. 1.0 / sigs_results[k][0], 1.0 / sigs_results[k][1],
  3953. 1.0 / sigs_results[k][2], sigs_results[k][0],
  3954. sigs_results[k][1], sigs_results[k][2]);
  3955. }
  3956. ret = 0;
  3957. end:
  3958. ERR_print_errors(bio_err);
  3959. for (i = 0; i < loopargs_len; i++) {
  3960. OPENSSL_free(loopargs[i].buf_malloc);
  3961. OPENSSL_free(loopargs[i].buf2_malloc);
  3962. BN_free(bn);
  3963. EVP_PKEY_CTX_free(genctx);
  3964. for (k = 0; k < RSA_NUM; k++) {
  3965. EVP_PKEY_CTX_free(loopargs[i].rsa_sign_ctx[k]);
  3966. EVP_PKEY_CTX_free(loopargs[i].rsa_verify_ctx[k]);
  3967. EVP_PKEY_CTX_free(loopargs[i].rsa_encrypt_ctx[k]);
  3968. EVP_PKEY_CTX_free(loopargs[i].rsa_decrypt_ctx[k]);
  3969. }
  3970. #ifndef OPENSSL_NO_DH
  3971. OPENSSL_free(loopargs[i].secret_ff_a);
  3972. OPENSSL_free(loopargs[i].secret_ff_b);
  3973. for (k = 0; k < FFDH_NUM; k++)
  3974. EVP_PKEY_CTX_free(loopargs[i].ffdh_ctx[k]);
  3975. #endif
  3976. for (k = 0; k < DSA_NUM; k++) {
  3977. EVP_PKEY_CTX_free(loopargs[i].dsa_sign_ctx[k]);
  3978. EVP_PKEY_CTX_free(loopargs[i].dsa_verify_ctx[k]);
  3979. }
  3980. for (k = 0; k < ECDSA_NUM; k++) {
  3981. EVP_PKEY_CTX_free(loopargs[i].ecdsa_sign_ctx[k]);
  3982. EVP_PKEY_CTX_free(loopargs[i].ecdsa_verify_ctx[k]);
  3983. }
  3984. for (k = 0; k < EC_NUM; k++)
  3985. EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
  3986. #ifndef OPENSSL_NO_ECX
  3987. for (k = 0; k < EdDSA_NUM; k++) {
  3988. EVP_MD_CTX_free(loopargs[i].eddsa_ctx[k]);
  3989. EVP_MD_CTX_free(loopargs[i].eddsa_ctx2[k]);
  3990. }
  3991. #endif /* OPENSSL_NO_ECX */
  3992. #ifndef OPENSSL_NO_SM2
  3993. for (k = 0; k < SM2_NUM; k++) {
  3994. EVP_PKEY_CTX *pctx = NULL;
  3995. /* free signing ctx */
  3996. if (loopargs[i].sm2_ctx[k] != NULL
  3997. && (pctx = EVP_MD_CTX_get_pkey_ctx(loopargs[i].sm2_ctx[k])) != NULL)
  3998. EVP_PKEY_CTX_free(pctx);
  3999. EVP_MD_CTX_free(loopargs[i].sm2_ctx[k]);
  4000. /* free verification ctx */
  4001. if (loopargs[i].sm2_vfy_ctx[k] != NULL
  4002. && (pctx = EVP_MD_CTX_get_pkey_ctx(loopargs[i].sm2_vfy_ctx[k])) != NULL)
  4003. EVP_PKEY_CTX_free(pctx);
  4004. EVP_MD_CTX_free(loopargs[i].sm2_vfy_ctx[k]);
  4005. /* free pkey */
  4006. EVP_PKEY_free(loopargs[i].sm2_pkey[k]);
  4007. }
  4008. #endif
  4009. for (k = 0; k < kems_algs_len; k++) {
  4010. EVP_PKEY_CTX_free(loopargs[i].kem_gen_ctx[k]);
  4011. EVP_PKEY_CTX_free(loopargs[i].kem_encaps_ctx[k]);
  4012. EVP_PKEY_CTX_free(loopargs[i].kem_decaps_ctx[k]);
  4013. OPENSSL_free(loopargs[i].kem_out[k]);
  4014. OPENSSL_free(loopargs[i].kem_send_secret[k]);
  4015. OPENSSL_free(loopargs[i].kem_rcv_secret[k]);
  4016. }
  4017. for (k = 0; k < sigs_algs_len; k++) {
  4018. EVP_PKEY_CTX_free(loopargs[i].sig_gen_ctx[k]);
  4019. EVP_PKEY_CTX_free(loopargs[i].sig_sign_ctx[k]);
  4020. EVP_PKEY_CTX_free(loopargs[i].sig_verify_ctx[k]);
  4021. OPENSSL_free(loopargs[i].sig_sig[k]);
  4022. }
  4023. OPENSSL_free(loopargs[i].secret_a);
  4024. OPENSSL_free(loopargs[i].secret_b);
  4025. }
  4026. OPENSSL_free(evp_hmac_name);
  4027. OPENSSL_free(evp_cmac_name);
  4028. for (k = 0; k < kems_algs_len; k++)
  4029. OPENSSL_free(kems_algname[k]);
  4030. if (kem_stack != NULL)
  4031. sk_EVP_KEM_pop_free(kem_stack, EVP_KEM_free);
  4032. for (k = 0; k < sigs_algs_len; k++)
  4033. OPENSSL_free(sigs_algname[k]);
  4034. if (sig_stack != NULL)
  4035. sk_EVP_SIGNATURE_pop_free(sig_stack, EVP_SIGNATURE_free);
  4036. if (async_jobs > 0) {
  4037. for (i = 0; i < loopargs_len; i++)
  4038. ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
  4039. }
  4040. if (async_init) {
  4041. ASYNC_cleanup_thread();
  4042. }
  4043. OPENSSL_free(loopargs);
  4044. release_engine(e);
  4045. EVP_CIPHER_free(evp_cipher);
  4046. EVP_MAC_free(mac);
  4047. NCONF_free(conf);
  4048. return ret;
  4049. }
  4050. static void print_message(const char *s, int length, int tm)
  4051. {
  4052. BIO_printf(bio_err,
  4053. mr ? "+DT:%s:%d:%d\n"
  4054. : "Doing %s ops for %ds on %d size blocks: ", s, tm, length);
  4055. (void)BIO_flush(bio_err);
  4056. run = 1;
  4057. alarm(tm);
  4058. }
  4059. static void pkey_print_message(const char *str, const char *str2, unsigned int bits,
  4060. int tm)
  4061. {
  4062. BIO_printf(bio_err,
  4063. mr ? "+DTP:%d:%s:%s:%d\n"
  4064. : "Doing %u bits %s %s ops for %ds: ", bits, str, str2, tm);
  4065. (void)BIO_flush(bio_err);
  4066. run = 1;
  4067. alarm(tm);
  4068. }
  4069. static void kskey_print_message(const char *str, const char *str2, int tm)
  4070. {
  4071. BIO_printf(bio_err,
  4072. mr ? "+DTP:%s:%s:%d\n"
  4073. : "Doing %s %s ops for %ds: ", str, str2, tm);
  4074. (void)BIO_flush(bio_err);
  4075. run = 1;
  4076. alarm(tm);
  4077. }
  4078. static void print_result(int alg, int run_no, int count, double time_used)
  4079. {
  4080. if (count == -1) {
  4081. BIO_printf(bio_err, "%s error!\n", names[alg]);
  4082. ERR_print_errors(bio_err);
  4083. return;
  4084. }
  4085. BIO_printf(bio_err,
  4086. mr ? "+R:%d:%s:%f\n"
  4087. : "%d %s ops in %.2fs\n", count, names[alg], time_used);
  4088. results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
  4089. }
  4090. #ifndef NO_FORK
  4091. static char *sstrsep(char **string, const char *delim)
  4092. {
  4093. char isdelim[256];
  4094. char *token = *string;
  4095. memset(isdelim, 0, sizeof(isdelim));
  4096. isdelim[0] = 1;
  4097. while (*delim) {
  4098. isdelim[(unsigned char)(*delim)] = 1;
  4099. delim++;
  4100. }
  4101. while (!isdelim[(unsigned char)(**string)])
  4102. (*string)++;
  4103. if (**string) {
  4104. **string = 0;
  4105. (*string)++;
  4106. }
  4107. return token;
  4108. }
  4109. static int strtoint(const char *str, const int min_val, const int upper_val,
  4110. int *res)
  4111. {
  4112. char *end = NULL;
  4113. long int val = 0;
  4114. errno = 0;
  4115. val = strtol(str, &end, 10);
  4116. if (errno == 0 && end != str && *end == 0
  4117. && min_val <= val && val < upper_val) {
  4118. *res = (int)val;
  4119. return 1;
  4120. } else {
  4121. return 0;
  4122. }
  4123. }
  4124. static int do_multi(int multi, int size_num)
  4125. {
  4126. int n;
  4127. int fd[2];
  4128. int *fds;
  4129. int status;
  4130. static char sep[] = ":";
  4131. fds = app_malloc(sizeof(*fds) * multi, "fd buffer for do_multi");
  4132. for (n = 0; n < multi; ++n) {
  4133. if (pipe(fd) == -1) {
  4134. BIO_printf(bio_err, "pipe failure\n");
  4135. exit(1);
  4136. }
  4137. fflush(stdout);
  4138. (void)BIO_flush(bio_err);
  4139. if (fork()) {
  4140. close(fd[1]);
  4141. fds[n] = fd[0];
  4142. } else {
  4143. close(fd[0]);
  4144. close(1);
  4145. if (dup(fd[1]) == -1) {
  4146. BIO_printf(bio_err, "dup failed\n");
  4147. exit(1);
  4148. }
  4149. close(fd[1]);
  4150. mr = 1;
  4151. usertime = 0;
  4152. OPENSSL_free(fds);
  4153. return 0;
  4154. }
  4155. printf("Forked child %d\n", n);
  4156. }
  4157. /* for now, assume the pipe is long enough to take all the output */
  4158. for (n = 0; n < multi; ++n) {
  4159. FILE *f;
  4160. char buf[1024];
  4161. char *p;
  4162. char *tk;
  4163. int k;
  4164. double d;
  4165. if ((f = fdopen(fds[n], "r")) == NULL) {
  4166. BIO_printf(bio_err, "fdopen failure with 0x%x\n",
  4167. errno);
  4168. OPENSSL_free(fds);
  4169. return 1;
  4170. }
  4171. while (fgets(buf, sizeof(buf), f)) {
  4172. p = strchr(buf, '\n');
  4173. if (p)
  4174. *p = '\0';
  4175. if (buf[0] != '+') {
  4176. BIO_printf(bio_err,
  4177. "Don't understand line '%s' from child %d\n", buf,
  4178. n);
  4179. continue;
  4180. }
  4181. printf("Got: %s from %d\n", buf, n);
  4182. p = buf;
  4183. if (CHECK_AND_SKIP_PREFIX(p, "+F:")) {
  4184. int alg;
  4185. int j;
  4186. if (strtoint(sstrsep(&p, sep), 0, ALGOR_NUM, &alg)) {
  4187. sstrsep(&p, sep);
  4188. for (j = 0; j < size_num; ++j)
  4189. results[alg][j] += atof(sstrsep(&p, sep));
  4190. }
  4191. } else if (CHECK_AND_SKIP_PREFIX(p, "+F2:")) {
  4192. tk = sstrsep(&p, sep);
  4193. if (strtoint(tk, 0, OSSL_NELEM(rsa_results), &k)) {
  4194. sstrsep(&p, sep);
  4195. d = atof(sstrsep(&p, sep));
  4196. rsa_results[k][0] += d;
  4197. d = atof(sstrsep(&p, sep));
  4198. rsa_results[k][1] += d;
  4199. d = atof(sstrsep(&p, sep));
  4200. rsa_results[k][2] += d;
  4201. d = atof(sstrsep(&p, sep));
  4202. rsa_results[k][3] += d;
  4203. }
  4204. } else if (CHECK_AND_SKIP_PREFIX(p, "+F3:")) {
  4205. tk = sstrsep(&p, sep);
  4206. if (strtoint(tk, 0, OSSL_NELEM(dsa_results), &k)) {
  4207. sstrsep(&p, sep);
  4208. d = atof(sstrsep(&p, sep));
  4209. dsa_results[k][0] += d;
  4210. d = atof(sstrsep(&p, sep));
  4211. dsa_results[k][1] += d;
  4212. }
  4213. } else if (CHECK_AND_SKIP_PREFIX(p, "+F4:")) {
  4214. tk = sstrsep(&p, sep);
  4215. if (strtoint(tk, 0, OSSL_NELEM(ecdsa_results), &k)) {
  4216. sstrsep(&p, sep);
  4217. d = atof(sstrsep(&p, sep));
  4218. ecdsa_results[k][0] += d;
  4219. d = atof(sstrsep(&p, sep));
  4220. ecdsa_results[k][1] += d;
  4221. }
  4222. } else if (CHECK_AND_SKIP_PREFIX(p, "+F5:")) {
  4223. tk = sstrsep(&p, sep);
  4224. if (strtoint(tk, 0, OSSL_NELEM(ecdh_results), &k)) {
  4225. sstrsep(&p, sep);
  4226. d = atof(sstrsep(&p, sep));
  4227. ecdh_results[k][0] += d;
  4228. }
  4229. # ifndef OPENSSL_NO_ECX
  4230. } else if (CHECK_AND_SKIP_PREFIX(p, "+F6:")) {
  4231. tk = sstrsep(&p, sep);
  4232. if (strtoint(tk, 0, OSSL_NELEM(eddsa_results), &k)) {
  4233. sstrsep(&p, sep);
  4234. sstrsep(&p, sep);
  4235. d = atof(sstrsep(&p, sep));
  4236. eddsa_results[k][0] += d;
  4237. d = atof(sstrsep(&p, sep));
  4238. eddsa_results[k][1] += d;
  4239. }
  4240. # endif /* OPENSSL_NO_ECX */
  4241. # ifndef OPENSSL_NO_SM2
  4242. } else if (CHECK_AND_SKIP_PREFIX(p, "+F7:")) {
  4243. tk = sstrsep(&p, sep);
  4244. if (strtoint(tk, 0, OSSL_NELEM(sm2_results), &k)) {
  4245. sstrsep(&p, sep);
  4246. sstrsep(&p, sep);
  4247. d = atof(sstrsep(&p, sep));
  4248. sm2_results[k][0] += d;
  4249. d = atof(sstrsep(&p, sep));
  4250. sm2_results[k][1] += d;
  4251. }
  4252. # endif /* OPENSSL_NO_SM2 */
  4253. # ifndef OPENSSL_NO_DH
  4254. } else if (CHECK_AND_SKIP_PREFIX(p, "+F8:")) {
  4255. tk = sstrsep(&p, sep);
  4256. if (strtoint(tk, 0, OSSL_NELEM(ffdh_results), &k)) {
  4257. sstrsep(&p, sep);
  4258. d = atof(sstrsep(&p, sep));
  4259. ffdh_results[k][0] += d;
  4260. }
  4261. # endif /* OPENSSL_NO_DH */
  4262. } else if (CHECK_AND_SKIP_PREFIX(p, "+F9:")) {
  4263. tk = sstrsep(&p, sep);
  4264. if (strtoint(tk, 0, OSSL_NELEM(kems_results), &k)) {
  4265. d = atof(sstrsep(&p, sep));
  4266. kems_results[k][0] += d;
  4267. d = atof(sstrsep(&p, sep));
  4268. kems_results[k][1] += d;
  4269. d = atof(sstrsep(&p, sep));
  4270. kems_results[k][2] += d;
  4271. }
  4272. } else if (CHECK_AND_SKIP_PREFIX(p, "+F10:")) {
  4273. tk = sstrsep(&p, sep);
  4274. if (strtoint(tk, 0, OSSL_NELEM(sigs_results), &k)) {
  4275. d = atof(sstrsep(&p, sep));
  4276. sigs_results[k][0] += d;
  4277. d = atof(sstrsep(&p, sep));
  4278. sigs_results[k][1] += d;
  4279. d = atof(sstrsep(&p, sep));
  4280. sigs_results[k][2] += d;
  4281. }
  4282. } else if (!HAS_PREFIX(buf, "+H:")) {
  4283. BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
  4284. n);
  4285. }
  4286. }
  4287. fclose(f);
  4288. }
  4289. OPENSSL_free(fds);
  4290. for (n = 0; n < multi; ++n) {
  4291. while (wait(&status) == -1)
  4292. if (errno != EINTR) {
  4293. BIO_printf(bio_err, "Waitng for child failed with 0x%x\n",
  4294. errno);
  4295. return 1;
  4296. }
  4297. if (WIFEXITED(status) && WEXITSTATUS(status)) {
  4298. BIO_printf(bio_err, "Child exited with %d\n", WEXITSTATUS(status));
  4299. } else if (WIFSIGNALED(status)) {
  4300. BIO_printf(bio_err, "Child terminated by signal %d\n",
  4301. WTERMSIG(status));
  4302. }
  4303. }
  4304. return 1;
  4305. }
  4306. #endif
  4307. static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
  4308. const openssl_speed_sec_t *seconds)
  4309. {
  4310. static const int mblengths_list[] =
  4311. { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
  4312. const int *mblengths = mblengths_list;
  4313. int j, count, keylen, num = OSSL_NELEM(mblengths_list), ciph_success = 1;
  4314. const char *alg_name;
  4315. unsigned char *inp = NULL, *out = NULL, *key, no_key[32], no_iv[16];
  4316. EVP_CIPHER_CTX *ctx = NULL;
  4317. double d = 0.0;
  4318. if (lengths_single) {
  4319. mblengths = &lengths_single;
  4320. num = 1;
  4321. }
  4322. inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
  4323. out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
  4324. if ((ctx = EVP_CIPHER_CTX_new()) == NULL)
  4325. app_bail_out("failed to allocate cipher context\n");
  4326. if (!EVP_EncryptInit_ex(ctx, evp_cipher, NULL, NULL, no_iv))
  4327. app_bail_out("failed to initialise cipher context\n");
  4328. if ((keylen = EVP_CIPHER_CTX_get_key_length(ctx)) < 0) {
  4329. BIO_printf(bio_err, "Impossible negative key length: %d\n", keylen);
  4330. goto err;
  4331. }
  4332. key = app_malloc(keylen, "evp_cipher key");
  4333. if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
  4334. app_bail_out("failed to generate random cipher key\n");
  4335. if (!EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL))
  4336. app_bail_out("failed to set cipher key\n");
  4337. OPENSSL_clear_free(key, keylen);
  4338. if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY,
  4339. sizeof(no_key), no_key) <= 0)
  4340. app_bail_out("failed to set AEAD key\n");
  4341. if ((alg_name = EVP_CIPHER_get0_name(evp_cipher)) == NULL)
  4342. app_bail_out("failed to get cipher name\n");
  4343. for (j = 0; j < num; j++) {
  4344. print_message(alg_name, mblengths[j], seconds->sym);
  4345. Time_F(START);
  4346. for (count = 0; run && count < INT_MAX; count++) {
  4347. unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
  4348. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
  4349. size_t len = mblengths[j];
  4350. int packlen;
  4351. memset(aad, 0, 8); /* avoid uninitialized values */
  4352. aad[8] = 23; /* SSL3_RT_APPLICATION_DATA */
  4353. aad[9] = 3; /* version */
  4354. aad[10] = 2;
  4355. aad[11] = 0; /* length */
  4356. aad[12] = 0;
  4357. mb_param.out = NULL;
  4358. mb_param.inp = aad;
  4359. mb_param.len = len;
  4360. mb_param.interleave = 8;
  4361. packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
  4362. sizeof(mb_param), &mb_param);
  4363. if (packlen > 0) {
  4364. mb_param.out = out;
  4365. mb_param.inp = inp;
  4366. mb_param.len = len;
  4367. (void)EVP_CIPHER_CTX_ctrl(ctx,
  4368. EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
  4369. sizeof(mb_param), &mb_param);
  4370. } else {
  4371. int pad;
  4372. if (RAND_bytes(inp, 16) <= 0)
  4373. app_bail_out("error setting random bytes\n");
  4374. len += 16;
  4375. aad[11] = (unsigned char)(len >> 8);
  4376. aad[12] = (unsigned char)(len);
  4377. pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
  4378. EVP_AEAD_TLS1_AAD_LEN, aad);
  4379. ciph_success = EVP_Cipher(ctx, out, inp, len + pad);
  4380. }
  4381. }
  4382. d = Time_F(STOP);
  4383. BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
  4384. : "%d %s ops in %.2fs\n", count, "evp", d);
  4385. if ((ciph_success <= 0) && (mr == 0))
  4386. BIO_printf(bio_err, "Error performing cipher op\n");
  4387. results[D_EVP][j] = ((double)count) / d * mblengths[j];
  4388. }
  4389. if (mr) {
  4390. fprintf(stdout, "+H");
  4391. for (j = 0; j < num; j++)
  4392. fprintf(stdout, ":%d", mblengths[j]);
  4393. fprintf(stdout, "\n");
  4394. fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
  4395. for (j = 0; j < num; j++)
  4396. fprintf(stdout, ":%.2f", results[D_EVP][j]);
  4397. fprintf(stdout, "\n");
  4398. } else {
  4399. fprintf(stdout,
  4400. "The 'numbers' are in 1000s of bytes per second processed.\n");
  4401. fprintf(stdout, "type ");
  4402. for (j = 0; j < num; j++)
  4403. fprintf(stdout, "%7d bytes", mblengths[j]);
  4404. fprintf(stdout, "\n");
  4405. fprintf(stdout, "%-24s", alg_name);
  4406. for (j = 0; j < num; j++) {
  4407. if (results[D_EVP][j] > 10000)
  4408. fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
  4409. else
  4410. fprintf(stdout, " %11.2f ", results[D_EVP][j]);
  4411. }
  4412. fprintf(stdout, "\n");
  4413. }
  4414. err:
  4415. OPENSSL_free(inp);
  4416. OPENSSL_free(out);
  4417. EVP_CIPHER_CTX_free(ctx);
  4418. }