speed.c 124 KB

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