speed.c 126 KB

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