speed.c 120 KB

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