speed.c 111 KB

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