speed.c 125 KB

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