speed.c 120 KB

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