speed.c 120 KB

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