speed.c 122 KB

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