speed.c 125 KB

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