speed.c 107 KB

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