speed.c 152 KB

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