speed.c 151 KB

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