speed.c 122 KB

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