speed.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  59. *
  60. * Portions of the attached software ("Contribution") are developed by
  61. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  62. *
  63. * The Contribution is licensed pursuant to the OpenSSL open source
  64. * license provided above.
  65. *
  66. * The ECDH and ECDSA speed test software is originally written by
  67. * Sumit Gupta of Sun Microsystems Laboratories.
  68. *
  69. */
  70. #undef SECONDS
  71. #define SECONDS 3
  72. #define PRIME_SECONDS 10
  73. #define RSA_SECONDS 10
  74. #define DSA_SECONDS 10
  75. #define ECDSA_SECONDS 10
  76. #define ECDH_SECONDS 10
  77. #include <stdio.h>
  78. #include <stdlib.h>
  79. #include <string.h>
  80. #include <math.h>
  81. #include "apps.h"
  82. #include <openssl/crypto.h>
  83. #include <openssl/rand.h>
  84. #include <openssl/err.h>
  85. #include <openssl/evp.h>
  86. #include <openssl/objects.h>
  87. #if !defined(OPENSSL_SYS_MSDOS)
  88. # include OPENSSL_UNISTD
  89. #endif
  90. #ifndef OPENSSL_SYS_NETWARE
  91. # include <signal.h>
  92. #endif
  93. #if defined(_WIN32) || defined(__CYGWIN__)
  94. # include <windows.h>
  95. # if defined(__CYGWIN__) && !defined(_WIN32)
  96. /*
  97. * <windows.h> should define _WIN32, which normally is mutually exclusive
  98. * with __CYGWIN__, but if it didn't...
  99. */
  100. # define _WIN32
  101. /* this is done because Cygwin alarm() fails sometimes. */
  102. # endif
  103. #endif
  104. #include <openssl/bn.h>
  105. #ifndef OPENSSL_NO_DES
  106. # include <openssl/des.h>
  107. #endif
  108. #ifndef OPENSSL_NO_AES
  109. # include <openssl/aes.h>
  110. #endif
  111. #ifndef OPENSSL_NO_CAMELLIA
  112. # include <openssl/camellia.h>
  113. #endif
  114. #ifndef OPENSSL_NO_MD2
  115. # include <openssl/md2.h>
  116. #endif
  117. #ifndef OPENSSL_NO_MDC2
  118. # include <openssl/mdc2.h>
  119. #endif
  120. #ifndef OPENSSL_NO_MD4
  121. # include <openssl/md4.h>
  122. #endif
  123. #ifndef OPENSSL_NO_MD5
  124. # include <openssl/md5.h>
  125. #endif
  126. #include <openssl/hmac.h>
  127. #include <openssl/evp.h>
  128. #include <openssl/sha.h>
  129. #ifndef OPENSSL_NO_RMD160
  130. # include <openssl/ripemd.h>
  131. #endif
  132. #ifndef OPENSSL_NO_WHIRLPOOL
  133. # include <openssl/whrlpool.h>
  134. #endif
  135. #ifndef OPENSSL_NO_RC4
  136. # include <openssl/rc4.h>
  137. #endif
  138. #ifndef OPENSSL_NO_RC5
  139. # include <openssl/rc5.h>
  140. #endif
  141. #ifndef OPENSSL_NO_RC2
  142. # include <openssl/rc2.h>
  143. #endif
  144. #ifndef OPENSSL_NO_IDEA
  145. # include <openssl/idea.h>
  146. #endif
  147. #ifndef OPENSSL_NO_SEED
  148. # include <openssl/seed.h>
  149. #endif
  150. #ifndef OPENSSL_NO_BF
  151. # include <openssl/blowfish.h>
  152. #endif
  153. #ifndef OPENSSL_NO_CAST
  154. # include <openssl/cast.h>
  155. #endif
  156. #ifndef OPENSSL_NO_RSA
  157. # include <openssl/rsa.h>
  158. # include "./testrsa.h"
  159. #endif
  160. #include <openssl/x509.h>
  161. #ifndef OPENSSL_NO_DSA
  162. # include <openssl/dsa.h>
  163. # include "./testdsa.h"
  164. #endif
  165. #ifndef OPENSSL_NO_EC
  166. # include <openssl/ecdsa.h>
  167. # include <openssl/ecdh.h>
  168. #endif
  169. #include <openssl/modes.h>
  170. #include <openssl/bn.h>
  171. #ifndef HAVE_FORK
  172. # if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_NETWARE)
  173. # define HAVE_FORK 0
  174. # else
  175. # define HAVE_FORK 1
  176. # endif
  177. #endif
  178. #if HAVE_FORK
  179. # undef NO_FORK
  180. #else
  181. # define NO_FORK
  182. #endif
  183. #undef BUFSIZE
  184. #define BUFSIZE (1024*8+1)
  185. #define MAX_MISALIGNMENT 63
  186. static volatile int run = 0;
  187. static int mr = 0;
  188. static int usertime = 1;
  189. static double Time_F(int s);
  190. static void print_message(const char *s, long num, int length);
  191. static void pkey_print_message(const char *str, const char *str2,
  192. long num, int bits, int sec);
  193. static void print_result(int alg, int run_no, int count, double time_used);
  194. #ifndef NO_FORK
  195. static int do_multi(int multi);
  196. #endif
  197. #define ALGOR_NUM 30
  198. #define SIZE_NUM 5
  199. #define PRIME_NUM 3
  200. #define RSA_NUM 7
  201. #define DSA_NUM 3
  202. #define EC_NUM 16
  203. #define MAX_ECDH_SIZE 256
  204. #define MISALIGN 64
  205. static const char *names[ALGOR_NUM] = {
  206. "md2", "mdc2", "md4", "md5", "hmac(md5)", "sha1", "rmd160", "rc4",
  207. "des cbc", "des ede3", "idea cbc", "seed cbc",
  208. "rc2 cbc", "rc5-32/12 cbc", "blowfish cbc", "cast cbc",
  209. "aes-128 cbc", "aes-192 cbc", "aes-256 cbc",
  210. "camellia-128 cbc", "camellia-192 cbc", "camellia-256 cbc",
  211. "evp", "sha256", "sha512", "whirlpool",
  212. "aes-128 ige", "aes-192 ige", "aes-256 ige", "ghash"
  213. };
  214. static double results[ALGOR_NUM][SIZE_NUM];
  215. static int lengths[SIZE_NUM] = {
  216. 16, 64, 256, 1024, 8 * 1024
  217. };
  218. #ifndef OPENSSL_NO_RSA
  219. static double rsa_results[RSA_NUM][2];
  220. #endif
  221. #ifndef OPENSSL_NO_DSA
  222. static double dsa_results[DSA_NUM][2];
  223. #endif
  224. #ifndef OPENSSL_NO_EC
  225. static double ecdsa_results[EC_NUM][2];
  226. static double ecdh_results[EC_NUM][1];
  227. #endif
  228. #if defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_EC)
  229. static const char rnd_seed[] =
  230. "string to make the random number generator think it has entropy";
  231. static int rnd_fake = 0;
  232. #endif
  233. #ifdef SIGALRM
  234. # if defined(__STDC__) || defined(sgi) || defined(_AIX)
  235. # define SIGRETTYPE void
  236. # else
  237. # define SIGRETTYPE int
  238. # endif
  239. static SIGRETTYPE sig_done(int sig);
  240. static SIGRETTYPE sig_done(int sig)
  241. {
  242. signal(SIGALRM, sig_done);
  243. run = 0;
  244. }
  245. #endif
  246. #define START 0
  247. #define STOP 1
  248. #if defined(_WIN32)
  249. # if !defined(SIGALRM)
  250. # define SIGALRM
  251. # endif
  252. static unsigned int lapse, schlock;
  253. static void alarm_win32(unsigned int secs)
  254. {
  255. lapse = secs * 1000;
  256. }
  257. # define alarm alarm_win32
  258. static DWORD WINAPI sleepy(VOID * arg)
  259. {
  260. schlock = 1;
  261. Sleep(lapse);
  262. run = 0;
  263. return 0;
  264. }
  265. static double Time_F(int s)
  266. {
  267. double ret;
  268. static HANDLE thr;
  269. if (s == START) {
  270. schlock = 0;
  271. thr = CreateThread(NULL, 4096, sleepy, NULL, 0, NULL);
  272. if (thr == NULL) {
  273. DWORD ret = GetLastError();
  274. BIO_printf(bio_err, "unable to CreateThread (%d)", ret);
  275. ExitProcess(ret);
  276. }
  277. while (!schlock)
  278. Sleep(0); /* scheduler spinlock */
  279. ret = app_tminterval(s, usertime);
  280. } else {
  281. ret = app_tminterval(s, usertime);
  282. if (run)
  283. TerminateThread(thr, 0);
  284. CloseHandle(thr);
  285. }
  286. return ret;
  287. }
  288. #else
  289. static double Time_F(int s)
  290. {
  291. double ret = app_tminterval(s, usertime);
  292. if (s == STOP)
  293. alarm(0);
  294. return ret;
  295. }
  296. #endif
  297. #ifndef OPENSSL_NO_EC
  298. static const int KDF1_SHA1_len = 20;
  299. static void *KDF1_SHA1(const void *in, size_t inlen, void *out,
  300. size_t *outlen)
  301. {
  302. if (*outlen < SHA_DIGEST_LENGTH)
  303. return NULL;
  304. *outlen = SHA_DIGEST_LENGTH;
  305. return SHA1(in, inlen, out);
  306. }
  307. #endif /* OPENSSL_NO_EC */
  308. static void multiblock_speed(const EVP_CIPHER *evp_cipher);
  309. static int found(const char *name, const OPT_PAIR * pairs, int *result)
  310. {
  311. for (; pairs->name; pairs++)
  312. if (strcmp(name, pairs->name) == 0) {
  313. *result = pairs->retval;
  314. return 1;
  315. }
  316. return 0;
  317. }
  318. typedef enum OPTION_choice {
  319. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  320. OPT_ELAPSED, OPT_EVP, OPT_DECRYPT, OPT_ENGINE, OPT_MULTI,
  321. OPT_MR, OPT_MB, OPT_MISALIGN
  322. } OPTION_CHOICE;
  323. OPTIONS speed_options[] = {
  324. {OPT_HELP_STR, 1, '-', "Usage: %s [options] ciphers...\n"},
  325. {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
  326. {"help", OPT_HELP, '-', "Display this summary"},
  327. #if defined(TIMES) || defined(USE_TOD)
  328. {"elapsed", OPT_ELAPSED, '-',
  329. "Measure time in real time instead of CPU user time"},
  330. #endif
  331. {"evp", OPT_EVP, 's', "Use specified EVP cipher"},
  332. {"decrypt", OPT_DECRYPT, '-',
  333. "Time decryption instead of encryption (only EVP)"},
  334. #ifndef NO_FORK
  335. {"multi", OPT_MULTI, 'p', "Run benchmarks in parallel"},
  336. #endif
  337. {"mr", OPT_MR, '-', "Produce machine readable output"},
  338. {"mb", OPT_MB, '-'},
  339. {"misalign", OPT_MISALIGN, 'n', "Amount to mis-align buffers"},
  340. #ifndef OPENSSL_NO_ENGINE
  341. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  342. #endif
  343. };
  344. #define D_MD2 0
  345. #define D_MDC2 1
  346. #define D_MD4 2
  347. #define D_MD5 3
  348. #define D_HMAC 4
  349. #define D_SHA1 5
  350. #define D_RMD160 6
  351. #define D_RC4 7
  352. #define D_CBC_DES 8
  353. #define D_EDE3_DES 9
  354. #define D_CBC_IDEA 10
  355. #define D_CBC_SEED 11
  356. #define D_CBC_RC2 12
  357. #define D_CBC_RC5 13
  358. #define D_CBC_BF 14
  359. #define D_CBC_CAST 15
  360. #define D_CBC_128_AES 16
  361. #define D_CBC_192_AES 17
  362. #define D_CBC_256_AES 18
  363. #define D_CBC_128_CML 19
  364. #define D_CBC_192_CML 20
  365. #define D_CBC_256_CML 21
  366. #define D_EVP 22
  367. #define D_SHA256 23
  368. #define D_SHA512 24
  369. #define D_WHIRLPOOL 25
  370. #define D_IGE_128_AES 26
  371. #define D_IGE_192_AES 27
  372. #define D_IGE_256_AES 28
  373. #define D_GHASH 29
  374. OPT_PAIR doit_choices[] = {
  375. #ifndef OPENSSL_NO_MD2
  376. {"md2", D_MD2},
  377. #endif
  378. #ifndef OPENSSL_NO_MDC2
  379. {"mdc2", D_MDC2},
  380. #endif
  381. #ifndef OPENSSL_NO_MD4
  382. {"md4", D_MD4},
  383. #endif
  384. #ifndef OPENSSL_NO_MD5
  385. {"md5", D_MD5},
  386. #endif
  387. #ifndef OPENSSL_NO_MD5
  388. {"hmac", D_HMAC},
  389. #endif
  390. {"sha1", D_SHA1},
  391. {"sha256", D_SHA256},
  392. {"sha512", D_SHA512},
  393. #ifndef OPENSSL_NO_WHIRLPOOL
  394. {"whirlpool", D_WHIRLPOOL},
  395. #endif
  396. #ifndef OPENSSL_NO_RIPEMD
  397. {"ripemd", D_RMD160},
  398. {"rmd160", D_RMD160},
  399. {"ripemd160", D_RMD160},
  400. #endif
  401. #ifndef OPENSSL_NO_RC4
  402. {"rc4", D_RC4},
  403. #endif
  404. #ifndef OPENSSL_NO_DES
  405. {"des-cbc", D_CBC_DES},
  406. {"des-ede3", D_EDE3_DES},
  407. #endif
  408. #ifndef OPENSSL_NO_AES
  409. {"aes-128-cbc", D_CBC_128_AES},
  410. {"aes-192-cbc", D_CBC_192_AES},
  411. {"aes-256-cbc", D_CBC_256_AES},
  412. {"aes-128-ige", D_IGE_128_AES},
  413. {"aes-192-ige", D_IGE_192_AES},
  414. {"aes-256-ige", D_IGE_256_AES},
  415. #endif
  416. #ifndef OPENSSL_NO_RC2
  417. {"rc2-cbc", D_CBC_RC2},
  418. {"rc2", D_CBC_RC2},
  419. #endif
  420. #ifndef OPENSSL_NO_RC5
  421. {"rc5-cbc", D_CBC_RC5},
  422. {"rc5", D_CBC_RC5},
  423. #endif
  424. #ifndef OPENSSL_NO_IDEA
  425. {"idea-cbc", D_CBC_IDEA},
  426. {"idea", D_CBC_IDEA},
  427. #endif
  428. #ifndef OPENSSL_NO_SEED
  429. {"seed-cbc", D_CBC_SEED},
  430. {"seed", D_CBC_SEED},
  431. #endif
  432. #ifndef OPENSSL_NO_BF
  433. {"bf-cbc", D_CBC_BF},
  434. {"blowfish", D_CBC_BF},
  435. {"bf", D_CBC_BF},
  436. #endif
  437. #ifndef OPENSSL_NO_CAST
  438. {"cast-cbc", D_CBC_CAST},
  439. {"cast", D_CBC_CAST},
  440. {"cast5", D_CBC_CAST},
  441. #endif
  442. {"ghash", D_GHASH},
  443. {NULL}
  444. };
  445. #define R_DSA_512 0
  446. #define R_DSA_1024 1
  447. #define R_DSA_2048 2
  448. static OPT_PAIR dsa_choices[] = {
  449. {"dsa512", R_DSA_512},
  450. {"dsa1024", R_DSA_1024},
  451. {"dsa2048", R_DSA_2048},
  452. {NULL},
  453. };
  454. #define R_RSA_512 0
  455. #define R_RSA_1024 1
  456. #define R_RSA_2048 2
  457. #define R_RSA_3072 3
  458. #define R_RSA_4096 4
  459. #define R_RSA_7680 5
  460. #define R_RSA_15360 6
  461. static OPT_PAIR rsa_choices[] = {
  462. {"rsa512", R_RSA_512},
  463. {"rsa1024", R_RSA_1024},
  464. {"rsa2048", R_RSA_2048},
  465. {"rsa3072", R_RSA_3072},
  466. {"rsa4096", R_RSA_4096},
  467. {"rsa7680", R_RSA_7680},
  468. {"rsa15360", R_RSA_15360},
  469. {NULL}
  470. };
  471. #define R_EC_P160 0
  472. #define R_EC_P192 1
  473. #define R_EC_P224 2
  474. #define R_EC_P256 3
  475. #define R_EC_P384 4
  476. #define R_EC_P521 5
  477. #define R_EC_K163 6
  478. #define R_EC_K233 7
  479. #define R_EC_K283 8
  480. #define R_EC_K409 9
  481. #define R_EC_K571 10
  482. #define R_EC_B163 11
  483. #define R_EC_B233 12
  484. #define R_EC_B283 13
  485. #define R_EC_B409 14
  486. #define R_EC_B571 15
  487. #ifndef OPENSSL_NO_ECA
  488. static OPT_PAIR ecdsa_choices[] = {
  489. {"ecdsap160", R_EC_P160},
  490. {"ecdsap192", R_EC_P192},
  491. {"ecdsap224", R_EC_P224},
  492. {"ecdsap256", R_EC_P256},
  493. {"ecdsap384", R_EC_P384},
  494. {"ecdsap521", R_EC_P521},
  495. {"ecdsak163", R_EC_K163},
  496. {"ecdsak233", R_EC_K233},
  497. {"ecdsak283", R_EC_K283},
  498. {"ecdsak409", R_EC_K409},
  499. {"ecdsak571", R_EC_K571},
  500. {"ecdsab163", R_EC_B163},
  501. {"ecdsab233", R_EC_B233},
  502. {"ecdsab283", R_EC_B283},
  503. {"ecdsab409", R_EC_B409},
  504. {"ecdsab571", R_EC_B571},
  505. {NULL}
  506. };
  507. static OPT_PAIR ecdh_choices[] = {
  508. {"ecdhp160", R_EC_P160},
  509. {"ecdhp192", R_EC_P192},
  510. {"ecdhp224", R_EC_P224},
  511. {"ecdhp256", R_EC_P256},
  512. {"ecdhp384", R_EC_P384},
  513. {"ecdhp521", R_EC_P521},
  514. {"ecdhk163", R_EC_K163},
  515. {"ecdhk233", R_EC_K233},
  516. {"ecdhk283", R_EC_K283},
  517. {"ecdhk409", R_EC_K409},
  518. {"ecdhk571", R_EC_K571},
  519. {"ecdhb163", R_EC_B163},
  520. {"ecdhb233", R_EC_B233},
  521. {"ecdhb283", R_EC_B283},
  522. {"ecdhb409", R_EC_B409},
  523. {"ecdhb571", R_EC_B571},
  524. {NULL}
  525. };
  526. #endif
  527. int speed_main(int argc, char **argv)
  528. {
  529. char *prog;
  530. const EVP_CIPHER *evp_cipher = NULL;
  531. const EVP_MD *evp_md = NULL;
  532. double d = 0.0;
  533. OPTION_CHOICE o;
  534. int decrypt = 0, multiblock = 0, doit[ALGOR_NUM], pr_header = 0;
  535. int dsa_doit[DSA_NUM], rsa_doit[RSA_NUM];
  536. int ret = 1, i, j, k, misalign = MAX_MISALIGNMENT + 1;
  537. long c[ALGOR_NUM][SIZE_NUM], count = 0, save_count = 0;
  538. unsigned char *buf_malloc = NULL, *buf2_malloc = NULL;
  539. unsigned char *buf = NULL, *buf2 = NULL;
  540. unsigned char *save_buf = NULL, *save_buf2 = NULL;
  541. unsigned char md[EVP_MAX_MD_SIZE];
  542. #ifndef NO_FORK
  543. int multi = 0;
  544. #endif
  545. /* What follows are the buffers and key material. */
  546. #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA)
  547. long rsa_count;
  548. #endif
  549. #ifndef OPENSSL_NO_MD2
  550. unsigned char md2[MD2_DIGEST_LENGTH];
  551. #endif
  552. #ifndef OPENSSL_NO_MDC2
  553. unsigned char mdc2[MDC2_DIGEST_LENGTH];
  554. #endif
  555. #ifndef OPENSSL_NO_MD4
  556. unsigned char md4[MD4_DIGEST_LENGTH];
  557. #endif
  558. #ifndef OPENSSL_NO_MD5
  559. unsigned char md5[MD5_DIGEST_LENGTH];
  560. unsigned char hmac[MD5_DIGEST_LENGTH];
  561. #endif
  562. unsigned char sha[SHA_DIGEST_LENGTH];
  563. unsigned char sha256[SHA256_DIGEST_LENGTH];
  564. unsigned char sha512[SHA512_DIGEST_LENGTH];
  565. #ifndef OPENSSL_NO_WHIRLPOOL
  566. unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH];
  567. #endif
  568. #ifndef OPENSSL_NO_RIPEMD
  569. unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
  570. #endif
  571. #ifndef OPENSSL_NO_RC4
  572. RC4_KEY rc4_ks;
  573. #endif
  574. #ifndef OPENSSL_NO_RC5
  575. RC5_32_KEY rc5_ks;
  576. #endif
  577. #ifndef OPENSSL_NO_RC2
  578. RC2_KEY rc2_ks;
  579. #endif
  580. #ifndef OPENSSL_NO_IDEA
  581. IDEA_KEY_SCHEDULE idea_ks;
  582. #endif
  583. #ifndef OPENSSL_NO_SEED
  584. SEED_KEY_SCHEDULE seed_ks;
  585. #endif
  586. #ifndef OPENSSL_NO_BF
  587. BF_KEY bf_ks;
  588. #endif
  589. #ifndef OPENSSL_NO_CAST
  590. CAST_KEY cast_ks;
  591. #endif
  592. static const unsigned char key16[16] = {
  593. 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
  594. 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
  595. };
  596. #ifndef OPENSSL_NO_AES
  597. static const unsigned char key24[24] = {
  598. 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
  599. 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
  600. 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
  601. };
  602. static const unsigned char key32[32] = {
  603. 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
  604. 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
  605. 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
  606. 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
  607. };
  608. #endif
  609. #ifndef OPENSSL_NO_CAMELLIA
  610. static const unsigned char ckey24[24] = {
  611. 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
  612. 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
  613. 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
  614. };
  615. static const unsigned char ckey32[32] = {
  616. 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
  617. 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
  618. 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
  619. 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
  620. };
  621. CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;
  622. #endif
  623. #ifndef OPENSSL_NO_AES
  624. # define MAX_BLOCK_SIZE 128
  625. #else
  626. # define MAX_BLOCK_SIZE 64
  627. #endif
  628. unsigned char DES_iv[8];
  629. unsigned char iv[2 * MAX_BLOCK_SIZE / 8];
  630. #ifndef OPENSSL_NO_DES
  631. static DES_cblock key = {
  632. 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
  633. };
  634. static DES_cblock key2 = {
  635. 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
  636. };
  637. static DES_cblock key3 = {
  638. 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
  639. };
  640. DES_key_schedule sch;
  641. DES_key_schedule sch2;
  642. DES_key_schedule sch3;
  643. #endif
  644. #ifndef OPENSSL_NO_AES
  645. AES_KEY aes_ks1, aes_ks2, aes_ks3;
  646. #endif
  647. #ifndef OPENSSL_NO_RSA
  648. unsigned rsa_num;
  649. RSA *rsa_key[RSA_NUM];
  650. long rsa_c[RSA_NUM][2];
  651. static unsigned int rsa_bits[RSA_NUM] = {
  652. 512, 1024, 2048, 3072, 4096, 7680, 15360
  653. };
  654. static unsigned char *rsa_data[RSA_NUM] = {
  655. test512, test1024, test2048, test3072, test4096, test7680, test15360
  656. };
  657. static int rsa_data_length[RSA_NUM] = {
  658. sizeof(test512), sizeof(test1024),
  659. sizeof(test2048), sizeof(test3072),
  660. sizeof(test4096), sizeof(test7680),
  661. sizeof(test15360)
  662. };
  663. #endif
  664. #ifndef OPENSSL_NO_DSA
  665. DSA *dsa_key[DSA_NUM];
  666. long dsa_c[DSA_NUM][2];
  667. static unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
  668. #endif
  669. #ifndef OPENSSL_NO_EC
  670. /*
  671. * We only test over the following curves as they are representative, To
  672. * add tests over more curves, simply add the curve NID and curve name to
  673. * the following arrays and increase the EC_NUM value accordingly.
  674. */
  675. static unsigned int test_curves[EC_NUM] = {
  676. /* Prime Curves */
  677. NID_secp160r1, NID_X9_62_prime192v1, NID_secp224r1,
  678. NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1,
  679. /* Binary Curves */
  680. NID_sect163k1, NID_sect233k1, NID_sect283k1,
  681. NID_sect409k1, NID_sect571k1, NID_sect163r2,
  682. NID_sect233r1, NID_sect283r1, NID_sect409r1,
  683. NID_sect571r1
  684. };
  685. static const char *test_curves_names[EC_NUM] = {
  686. /* Prime Curves */
  687. "secp160r1", "nistp192", "nistp224",
  688. "nistp256", "nistp384", "nistp521",
  689. /* Binary Curves */
  690. "nistk163", "nistk233", "nistk283",
  691. "nistk409", "nistk571", "nistb163",
  692. "nistb233", "nistb283", "nistb409",
  693. "nistb571"
  694. };
  695. static int test_curves_bits[EC_NUM] = {
  696. 160, 192, 224,
  697. 256, 384, 521,
  698. 163, 233, 283,
  699. 409, 571, 163,
  700. 233, 283, 409,
  701. 571
  702. };
  703. #endif
  704. #ifndef OPENSSL_NO_EC
  705. unsigned char ecdsasig[256];
  706. unsigned int ecdsasiglen;
  707. EC_KEY *ecdsa[EC_NUM];
  708. long ecdsa_c[EC_NUM][2];
  709. int ecdsa_doit[EC_NUM];
  710. EC_KEY *ecdh_a[EC_NUM], *ecdh_b[EC_NUM];
  711. unsigned char secret_a[MAX_ECDH_SIZE], secret_b[MAX_ECDH_SIZE];
  712. int secret_size_a, secret_size_b;
  713. int ecdh_checks = 0;
  714. int secret_idx = 0;
  715. long ecdh_c[EC_NUM][2];
  716. int ecdh_doit[EC_NUM];
  717. #endif
  718. #ifndef TIMES
  719. usertime = -1;
  720. #endif
  721. memset(results, 0, sizeof(results));
  722. #ifndef OPENSSL_NO_DSA
  723. memset(dsa_key, 0, sizeof(dsa_key));
  724. #endif
  725. #ifndef OPENSSL_NO_EC
  726. for (i = 0; i < EC_NUM; i++)
  727. ecdsa[i] = NULL;
  728. for (i = 0; i < EC_NUM; i++)
  729. ecdh_a[i] = ecdh_b[i] = NULL;
  730. #endif
  731. #ifndef OPENSSL_NO_RSA
  732. memset(rsa_key, 0, sizeof(rsa_key));
  733. for (i = 0; i < RSA_NUM; i++)
  734. rsa_key[i] = NULL;
  735. #endif
  736. memset(c, 0, sizeof(c));
  737. memset(DES_iv, 0, sizeof(DES_iv));
  738. memset(iv, 0, sizeof(iv));
  739. for (i = 0; i < ALGOR_NUM; i++)
  740. doit[i] = 0;
  741. for (i = 0; i < RSA_NUM; i++)
  742. rsa_doit[i] = 0;
  743. for (i = 0; i < DSA_NUM; i++)
  744. dsa_doit[i] = 0;
  745. #ifndef OPENSSL_NO_EC
  746. for (i = 0; i < EC_NUM; i++)
  747. ecdsa_doit[i] = 0;
  748. for (i = 0; i < EC_NUM; i++)
  749. ecdh_doit[i] = 0;
  750. #endif
  751. buf = buf_malloc = app_malloc((int)BUFSIZE + misalign, "input buffer");
  752. buf2 = buf2_malloc = app_malloc((int)BUFSIZE + misalign, "output buffer");
  753. misalign = 0;
  754. prog = opt_init(argc, argv, speed_options);
  755. while ((o = opt_next()) != OPT_EOF) {
  756. switch (o) {
  757. case OPT_EOF:
  758. case OPT_ERR:
  759. opterr:
  760. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  761. goto end;
  762. case OPT_HELP:
  763. opt_help(speed_options);
  764. ret = 0;
  765. goto end;
  766. case OPT_ELAPSED:
  767. usertime = 0;
  768. break;
  769. case OPT_EVP:
  770. evp_cipher = EVP_get_cipherbyname(opt_arg());
  771. if (evp_cipher == NULL)
  772. evp_md = EVP_get_digestbyname(opt_arg());
  773. if (evp_cipher == NULL && evp_md == NULL) {
  774. BIO_printf(bio_err,
  775. "%s: %s an unknown cipher or digest\n",
  776. prog, opt_arg());
  777. goto end;
  778. }
  779. doit[D_EVP] = 1;
  780. break;
  781. case OPT_DECRYPT:
  782. decrypt = 1;
  783. break;
  784. case OPT_ENGINE:
  785. (void)setup_engine(opt_arg(), 0);
  786. break;
  787. #ifndef NO_FORK
  788. case OPT_MULTI:
  789. multi = atoi(opt_arg());
  790. break;
  791. #endif
  792. case OPT_MISALIGN:
  793. if (!opt_int(opt_arg(), &misalign))
  794. goto end;
  795. if (misalign > MISALIGN) {
  796. BIO_printf(bio_err,
  797. "%s: Maximum offset is %d\n", prog, MISALIGN);
  798. goto opterr;
  799. }
  800. buf = buf_malloc + misalign;
  801. buf2 = buf2_malloc + misalign;
  802. break;
  803. case OPT_MR:
  804. mr = 1;
  805. break;
  806. case OPT_MB:
  807. multiblock = 1;
  808. break;
  809. }
  810. }
  811. argc = opt_num_rest();
  812. argv = opt_rest();
  813. /* Remaining arguments are algorithms. */
  814. for ( ; *argv; argv++) {
  815. if (found(*argv, doit_choices, &i)) {
  816. doit[i] = 1;
  817. continue;
  818. }
  819. #ifndef OPENSSL_NO_DES
  820. if (strcmp(*argv, "des") == 0) {
  821. doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
  822. continue;
  823. }
  824. #endif
  825. if (strcmp(*argv, "sha") == 0) {
  826. doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
  827. continue;
  828. }
  829. #ifndef OPENSSL_NO_RSA
  830. # ifndef RSA_NULL
  831. if (strcmp(*argv, "openssl") == 0) {
  832. RSA_set_default_method(RSA_PKCS1_SSLeay());
  833. continue;
  834. }
  835. # endif
  836. if (strcmp(*argv, "rsa") == 0) {
  837. rsa_doit[R_RSA_512] = rsa_doit[R_RSA_1024] =
  838. rsa_doit[R_RSA_2048] = rsa_doit[R_RSA_3072] =
  839. rsa_doit[R_RSA_4096] = rsa_doit[R_RSA_7680] =
  840. rsa_doit[R_RSA_15360] = 1;
  841. continue;
  842. }
  843. if (found(*argv, rsa_choices, &i)) {
  844. rsa_doit[i] = 1;
  845. continue;
  846. }
  847. #endif
  848. #ifndef OPENSSL_NO_DSA
  849. if (strcmp(*argv, "dsa") == 0) {
  850. dsa_doit[R_DSA_512] = dsa_doit[R_DSA_1024] =
  851. dsa_doit[R_DSA_2048] = 1;
  852. continue;
  853. }
  854. if (found(*argv, dsa_choices, &i)) {
  855. dsa_doit[i] = 2;
  856. continue;
  857. }
  858. #endif
  859. #ifndef OPENSSL_NO_AES
  860. if (strcmp(*argv, "aes") == 0) {
  861. doit[D_CBC_128_AES] = doit[D_CBC_192_AES] =
  862. doit[D_CBC_256_AES] = 1;
  863. continue;
  864. }
  865. #endif
  866. #ifndef OPENSSL_NO_CAMELLIA
  867. if (strcmp(*argv, "camellia") == 0) {
  868. doit[D_CBC_128_CML] = doit[D_CBC_192_CML] =
  869. doit[D_CBC_256_CML] = 1;
  870. continue;
  871. }
  872. #endif
  873. #ifndef OPENSSL_NO_EC
  874. if (strcmp(*argv, "ecdsa") == 0) {
  875. for (i = 0; i < EC_NUM; i++)
  876. ecdsa_doit[i] = 1;
  877. continue;
  878. }
  879. if (found(*argv, ecdsa_choices, &i)) {
  880. ecdsa_doit[i] = 2;
  881. continue;
  882. }
  883. if (strcmp(*argv, "ecdh") == 0) {
  884. for (i = 0; i < EC_NUM; i++)
  885. ecdh_doit[i] = 1;
  886. continue;
  887. }
  888. if (found(*argv, ecdh_choices, &i)) {
  889. ecdh_doit[i] = 2;
  890. continue;
  891. }
  892. #endif
  893. BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, *argv);
  894. goto end;
  895. }
  896. #ifndef NO_FORK
  897. if (multi && do_multi(multi))
  898. goto show_res;
  899. #endif
  900. /* No parameters; turn on everything. */
  901. if (argc == 0) {
  902. for (i = 0; i < ALGOR_NUM; i++)
  903. if (i != D_EVP)
  904. doit[i] = 1;
  905. for (i = 0; i < RSA_NUM; i++)
  906. rsa_doit[i] = 1;
  907. for (i = 0; i < DSA_NUM; i++)
  908. dsa_doit[i] = 1;
  909. #ifndef OPENSSL_NO_EC
  910. for (i = 0; i < EC_NUM; i++)
  911. ecdsa_doit[i] = 1;
  912. for (i = 0; i < EC_NUM; i++)
  913. ecdh_doit[i] = 1;
  914. #endif
  915. }
  916. for (i = 0; i < ALGOR_NUM; i++)
  917. if (doit[i])
  918. pr_header++;
  919. if (usertime == 0 && !mr)
  920. BIO_printf(bio_err,
  921. "You have chosen to measure elapsed time "
  922. "instead of user CPU time.\n");
  923. #ifndef OPENSSL_NO_RSA
  924. for (i = 0; i < RSA_NUM; i++) {
  925. const unsigned char *p;
  926. p = rsa_data[i];
  927. rsa_key[i] = d2i_RSAPrivateKey(NULL, &p, rsa_data_length[i]);
  928. if (rsa_key[i] == NULL) {
  929. BIO_printf(bio_err, "internal error loading RSA key number %d\n",
  930. i);
  931. goto end;
  932. }
  933. }
  934. #endif
  935. #ifndef OPENSSL_NO_DSA
  936. dsa_key[0] = get_dsa512();
  937. dsa_key[1] = get_dsa1024();
  938. dsa_key[2] = get_dsa2048();
  939. #endif
  940. #ifndef OPENSSL_NO_DES
  941. DES_set_key_unchecked(&key, &sch);
  942. DES_set_key_unchecked(&key2, &sch2);
  943. DES_set_key_unchecked(&key3, &sch3);
  944. #endif
  945. #ifndef OPENSSL_NO_AES
  946. AES_set_encrypt_key(key16, 128, &aes_ks1);
  947. AES_set_encrypt_key(key24, 192, &aes_ks2);
  948. AES_set_encrypt_key(key32, 256, &aes_ks3);
  949. #endif
  950. #ifndef OPENSSL_NO_CAMELLIA
  951. Camellia_set_key(key16, 128, &camellia_ks1);
  952. Camellia_set_key(ckey24, 192, &camellia_ks2);
  953. Camellia_set_key(ckey32, 256, &camellia_ks3);
  954. #endif
  955. #ifndef OPENSSL_NO_IDEA
  956. idea_set_encrypt_key(key16, &idea_ks);
  957. #endif
  958. #ifndef OPENSSL_NO_SEED
  959. SEED_set_key(key16, &seed_ks);
  960. #endif
  961. #ifndef OPENSSL_NO_RC4
  962. RC4_set_key(&rc4_ks, 16, key16);
  963. #endif
  964. #ifndef OPENSSL_NO_RC2
  965. RC2_set_key(&rc2_ks, 16, key16, 128);
  966. #endif
  967. #ifndef OPENSSL_NO_RC5
  968. RC5_32_set_key(&rc5_ks, 16, key16, 12);
  969. #endif
  970. #ifndef OPENSSL_NO_BF
  971. BF_set_key(&bf_ks, 16, key16);
  972. #endif
  973. #ifndef OPENSSL_NO_CAST
  974. CAST_set_key(&cast_ks, 16, key16);
  975. #endif
  976. #ifndef OPENSSL_NO_RSA
  977. memset(rsa_c, 0, sizeof(rsa_c));
  978. #endif
  979. #ifndef SIGALRM
  980. # ifndef OPENSSL_NO_DES
  981. BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
  982. count = 10;
  983. do {
  984. long it;
  985. count *= 2;
  986. Time_F(START);
  987. for (it = count; it; it--)
  988. DES_ecb_encrypt((DES_cblock *)buf,
  989. (DES_cblock *)buf, &sch, DES_ENCRYPT);
  990. d = Time_F(STOP);
  991. } while (d < 3);
  992. save_count = count;
  993. c[D_MD2][0] = count / 10;
  994. c[D_MDC2][0] = count / 10;
  995. c[D_MD4][0] = count;
  996. c[D_MD5][0] = count;
  997. c[D_HMAC][0] = count;
  998. c[D_SHA1][0] = count;
  999. c[D_RMD160][0] = count;
  1000. c[D_RC4][0] = count * 5;
  1001. c[D_CBC_DES][0] = count;
  1002. c[D_EDE3_DES][0] = count / 3;
  1003. c[D_CBC_IDEA][0] = count;
  1004. c[D_CBC_SEED][0] = count;
  1005. c[D_CBC_RC2][0] = count;
  1006. c[D_CBC_RC5][0] = count;
  1007. c[D_CBC_BF][0] = count;
  1008. c[D_CBC_CAST][0] = count;
  1009. c[D_CBC_128_AES][0] = count;
  1010. c[D_CBC_192_AES][0] = count;
  1011. c[D_CBC_256_AES][0] = count;
  1012. c[D_CBC_128_CML][0] = count;
  1013. c[D_CBC_192_CML][0] = count;
  1014. c[D_CBC_256_CML][0] = count;
  1015. c[D_SHA256][0] = count;
  1016. c[D_SHA512][0] = count;
  1017. c[D_WHIRLPOOL][0] = count;
  1018. c[D_IGE_128_AES][0] = count;
  1019. c[D_IGE_192_AES][0] = count;
  1020. c[D_IGE_256_AES][0] = count;
  1021. c[D_GHASH][0] = count;
  1022. for (i = 1; i < SIZE_NUM; i++) {
  1023. long l0, l1;
  1024. l0 = (long)lengths[0];
  1025. l1 = (long)lengths[i];
  1026. c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
  1027. c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
  1028. c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
  1029. c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
  1030. c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
  1031. c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
  1032. c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
  1033. c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
  1034. c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
  1035. c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
  1036. l0 = (long)lengths[i - 1];
  1037. c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
  1038. c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
  1039. c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
  1040. c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
  1041. c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
  1042. c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
  1043. c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
  1044. c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
  1045. c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
  1046. c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
  1047. c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
  1048. c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
  1049. c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
  1050. c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
  1051. c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
  1052. c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
  1053. c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
  1054. c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
  1055. }
  1056. # ifndef OPENSSL_NO_RSA
  1057. rsa_c[R_RSA_512][0] = count / 2000;
  1058. rsa_c[R_RSA_512][1] = count / 400;
  1059. for (i = 1; i < RSA_NUM; i++) {
  1060. rsa_c[i][0] = rsa_c[i - 1][0] / 8;
  1061. rsa_c[i][1] = rsa_c[i - 1][1] / 4;
  1062. if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))
  1063. rsa_doit[i] = 0;
  1064. else {
  1065. if (rsa_c[i][0] == 0) {
  1066. rsa_c[i][0] = 1;
  1067. rsa_c[i][1] = 20;
  1068. }
  1069. }
  1070. }
  1071. # endif
  1072. # ifndef OPENSSL_NO_DSA
  1073. dsa_c[R_DSA_512][0] = count / 1000;
  1074. dsa_c[R_DSA_512][1] = count / 1000 / 2;
  1075. for (i = 1; i < DSA_NUM; i++) {
  1076. dsa_c[i][0] = dsa_c[i - 1][0] / 4;
  1077. dsa_c[i][1] = dsa_c[i - 1][1] / 4;
  1078. if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))
  1079. dsa_doit[i] = 0;
  1080. else {
  1081. if (dsa_c[i] == 0) {
  1082. dsa_c[i][0] = 1;
  1083. dsa_c[i][1] = 1;
  1084. }
  1085. }
  1086. }
  1087. # endif
  1088. # ifndef OPENSSL_NO_EC
  1089. ecdsa_c[R_EC_P160][0] = count / 1000;
  1090. ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
  1091. for (i = R_EC_P192; i <= R_EC_P521; i++) {
  1092. ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
  1093. ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
  1094. if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
  1095. ecdsa_doit[i] = 0;
  1096. else {
  1097. if (ecdsa_c[i] == 0) {
  1098. ecdsa_c[i][0] = 1;
  1099. ecdsa_c[i][1] = 1;
  1100. }
  1101. }
  1102. }
  1103. ecdsa_c[R_EC_K163][0] = count / 1000;
  1104. ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
  1105. for (i = R_EC_K233; i <= R_EC_K571; i++) {
  1106. ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
  1107. ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
  1108. if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
  1109. ecdsa_doit[i] = 0;
  1110. else {
  1111. if (ecdsa_c[i] == 0) {
  1112. ecdsa_c[i][0] = 1;
  1113. ecdsa_c[i][1] = 1;
  1114. }
  1115. }
  1116. }
  1117. ecdsa_c[R_EC_B163][0] = count / 1000;
  1118. ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
  1119. for (i = R_EC_B233; i <= R_EC_B571; i++) {
  1120. ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
  1121. ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
  1122. if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
  1123. ecdsa_doit[i] = 0;
  1124. else {
  1125. if (ecdsa_c[i] == 0) {
  1126. ecdsa_c[i][0] = 1;
  1127. ecdsa_c[i][1] = 1;
  1128. }
  1129. }
  1130. }
  1131. ecdh_c[R_EC_P160][0] = count / 1000;
  1132. ecdh_c[R_EC_P160][1] = count / 1000;
  1133. for (i = R_EC_P192; i <= R_EC_P521; i++) {
  1134. ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
  1135. ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
  1136. if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
  1137. ecdh_doit[i] = 0;
  1138. else {
  1139. if (ecdh_c[i] == 0) {
  1140. ecdh_c[i][0] = 1;
  1141. ecdh_c[i][1] = 1;
  1142. }
  1143. }
  1144. }
  1145. ecdh_c[R_EC_K163][0] = count / 1000;
  1146. ecdh_c[R_EC_K163][1] = count / 1000;
  1147. for (i = R_EC_K233; i <= R_EC_K571; i++) {
  1148. ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
  1149. ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
  1150. if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
  1151. ecdh_doit[i] = 0;
  1152. else {
  1153. if (ecdh_c[i] == 0) {
  1154. ecdh_c[i][0] = 1;
  1155. ecdh_c[i][1] = 1;
  1156. }
  1157. }
  1158. }
  1159. ecdh_c[R_EC_B163][0] = count / 1000;
  1160. ecdh_c[R_EC_B163][1] = count / 1000;
  1161. for (i = R_EC_B233; i <= R_EC_B571; i++) {
  1162. ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
  1163. ecdh_c[i][1] = ecdh_c[i - 1][1] / 2;
  1164. if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
  1165. ecdh_doit[i] = 0;
  1166. else {
  1167. if (ecdh_c[i] == 0) {
  1168. ecdh_c[i][0] = 1;
  1169. ecdh_c[i][1] = 1;
  1170. }
  1171. }
  1172. }
  1173. # endif
  1174. # define COND(d) (count < (d))
  1175. # define COUNT(d) (d)
  1176. # else
  1177. /* not worth fixing */
  1178. # error "You cannot disable DES on systems without SIGALRM."
  1179. # endif /* OPENSSL_NO_DES */
  1180. #else
  1181. # define COND(c) (run && count<0x7fffffff)
  1182. # define COUNT(d) (count)
  1183. # ifndef _WIN32
  1184. signal(SIGALRM, sig_done);
  1185. # endif
  1186. #endif /* SIGALRM */
  1187. #ifndef OPENSSL_NO_MD2
  1188. if (doit[D_MD2]) {
  1189. for (j = 0; j < SIZE_NUM; j++) {
  1190. print_message(names[D_MD2], c[D_MD2][j], lengths[j]);
  1191. Time_F(START);
  1192. for (count = 0, run = 1; COND(c[D_MD2][j]); count++)
  1193. EVP_Digest(buf, (unsigned long)lengths[j], &(md2[0]), NULL,
  1194. EVP_md2(), NULL);
  1195. d = Time_F(STOP);
  1196. print_result(D_MD2, j, count, d);
  1197. }
  1198. }
  1199. #endif
  1200. #ifndef OPENSSL_NO_MDC2
  1201. if (doit[D_MDC2]) {
  1202. for (j = 0; j < SIZE_NUM; j++) {
  1203. print_message(names[D_MDC2], c[D_MDC2][j], lengths[j]);
  1204. Time_F(START);
  1205. for (count = 0, run = 1; COND(c[D_MDC2][j]); count++)
  1206. EVP_Digest(buf, (unsigned long)lengths[j], &(mdc2[0]), NULL,
  1207. EVP_mdc2(), NULL);
  1208. d = Time_F(STOP);
  1209. print_result(D_MDC2, j, count, d);
  1210. }
  1211. }
  1212. #endif
  1213. #ifndef OPENSSL_NO_MD4
  1214. if (doit[D_MD4]) {
  1215. for (j = 0; j < SIZE_NUM; j++) {
  1216. print_message(names[D_MD4], c[D_MD4][j], lengths[j]);
  1217. Time_F(START);
  1218. for (count = 0, run = 1; COND(c[D_MD4][j]); count++)
  1219. EVP_Digest(&(buf[0]), (unsigned long)lengths[j], &(md4[0]),
  1220. NULL, EVP_md4(), NULL);
  1221. d = Time_F(STOP);
  1222. print_result(D_MD4, j, count, d);
  1223. }
  1224. }
  1225. #endif
  1226. #ifndef OPENSSL_NO_MD5
  1227. if (doit[D_MD5]) {
  1228. for (j = 0; j < SIZE_NUM; j++) {
  1229. print_message(names[D_MD5], c[D_MD5][j], lengths[j]);
  1230. Time_F(START);
  1231. for (count = 0, run = 1; COND(c[D_MD5][j]); count++)
  1232. MD5(buf, lengths[j], md5);
  1233. d = Time_F(STOP);
  1234. print_result(D_MD5, j, count, d);
  1235. }
  1236. }
  1237. #endif
  1238. #if !defined(OPENSSL_NO_MD5)
  1239. if (doit[D_HMAC]) {
  1240. HMAC_CTX hctx;
  1241. HMAC_CTX_init(&hctx);
  1242. HMAC_Init_ex(&hctx, (unsigned char *)"This is a key...",
  1243. 16, EVP_md5(), NULL);
  1244. for (j = 0; j < SIZE_NUM; j++) {
  1245. print_message(names[D_HMAC], c[D_HMAC][j], lengths[j]);
  1246. Time_F(START);
  1247. for (count = 0, run = 1; COND(c[D_HMAC][j]); count++) {
  1248. HMAC_Init_ex(&hctx, NULL, 0, NULL, NULL);
  1249. HMAC_Update(&hctx, buf, lengths[j]);
  1250. HMAC_Final(&hctx, &(hmac[0]), NULL);
  1251. }
  1252. d = Time_F(STOP);
  1253. print_result(D_HMAC, j, count, d);
  1254. }
  1255. HMAC_CTX_cleanup(&hctx);
  1256. }
  1257. #endif
  1258. if (doit[D_SHA1]) {
  1259. for (j = 0; j < SIZE_NUM; j++) {
  1260. print_message(names[D_SHA1], c[D_SHA1][j], lengths[j]);
  1261. Time_F(START);
  1262. for (count = 0, run = 1; COND(c[D_SHA1][j]); count++)
  1263. SHA1(buf, lengths[j], sha);
  1264. d = Time_F(STOP);
  1265. print_result(D_SHA1, j, count, d);
  1266. }
  1267. }
  1268. if (doit[D_SHA256]) {
  1269. for (j = 0; j < SIZE_NUM; j++) {
  1270. print_message(names[D_SHA256], c[D_SHA256][j], lengths[j]);
  1271. Time_F(START);
  1272. for (count = 0, run = 1; COND(c[D_SHA256][j]); count++)
  1273. SHA256(buf, lengths[j], sha256);
  1274. d = Time_F(STOP);
  1275. print_result(D_SHA256, j, count, d);
  1276. }
  1277. }
  1278. if (doit[D_SHA512]) {
  1279. for (j = 0; j < SIZE_NUM; j++) {
  1280. print_message(names[D_SHA512], c[D_SHA512][j], lengths[j]);
  1281. Time_F(START);
  1282. for (count = 0, run = 1; COND(c[D_SHA512][j]); count++)
  1283. SHA512(buf, lengths[j], sha512);
  1284. d = Time_F(STOP);
  1285. print_result(D_SHA512, j, count, d);
  1286. }
  1287. }
  1288. #ifndef OPENSSL_NO_WHIRLPOOL
  1289. if (doit[D_WHIRLPOOL]) {
  1290. for (j = 0; j < SIZE_NUM; j++) {
  1291. print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][j], lengths[j]);
  1292. Time_F(START);
  1293. for (count = 0, run = 1; COND(c[D_WHIRLPOOL][j]); count++)
  1294. WHIRLPOOL(buf, lengths[j], whirlpool);
  1295. d = Time_F(STOP);
  1296. print_result(D_WHIRLPOOL, j, count, d);
  1297. }
  1298. }
  1299. #endif
  1300. #ifndef OPENSSL_NO_RMD160
  1301. if (doit[D_RMD160]) {
  1302. for (j = 0; j < SIZE_NUM; j++) {
  1303. print_message(names[D_RMD160], c[D_RMD160][j], lengths[j]);
  1304. Time_F(START);
  1305. for (count = 0, run = 1; COND(c[D_RMD160][j]); count++)
  1306. EVP_Digest(buf, (unsigned long)lengths[j], &(rmd160[0]), NULL,
  1307. EVP_ripemd160(), NULL);
  1308. d = Time_F(STOP);
  1309. print_result(D_RMD160, j, count, d);
  1310. }
  1311. }
  1312. #endif
  1313. #ifndef OPENSSL_NO_RC4
  1314. if (doit[D_RC4]) {
  1315. for (j = 0; j < SIZE_NUM; j++) {
  1316. print_message(names[D_RC4], c[D_RC4][j], lengths[j]);
  1317. Time_F(START);
  1318. for (count = 0, run = 1; COND(c[D_RC4][j]); count++)
  1319. RC4(&rc4_ks, (unsigned int)lengths[j], buf, buf);
  1320. d = Time_F(STOP);
  1321. print_result(D_RC4, j, count, d);
  1322. }
  1323. }
  1324. #endif
  1325. #ifndef OPENSSL_NO_DES
  1326. if (doit[D_CBC_DES]) {
  1327. for (j = 0; j < SIZE_NUM; j++) {
  1328. print_message(names[D_CBC_DES], c[D_CBC_DES][j], lengths[j]);
  1329. Time_F(START);
  1330. for (count = 0, run = 1; COND(c[D_CBC_DES][j]); count++)
  1331. DES_ncbc_encrypt(buf, buf, lengths[j], &sch,
  1332. &DES_iv, DES_ENCRYPT);
  1333. d = Time_F(STOP);
  1334. print_result(D_CBC_DES, j, count, d);
  1335. }
  1336. }
  1337. if (doit[D_EDE3_DES]) {
  1338. for (j = 0; j < SIZE_NUM; j++) {
  1339. print_message(names[D_EDE3_DES], c[D_EDE3_DES][j], lengths[j]);
  1340. Time_F(START);
  1341. for (count = 0, run = 1; COND(c[D_EDE3_DES][j]); count++)
  1342. DES_ede3_cbc_encrypt(buf, buf, lengths[j],
  1343. &sch, &sch2, &sch3,
  1344. &DES_iv, DES_ENCRYPT);
  1345. d = Time_F(STOP);
  1346. print_result(D_EDE3_DES, j, count, d);
  1347. }
  1348. }
  1349. #endif
  1350. #ifndef OPENSSL_NO_AES
  1351. if (doit[D_CBC_128_AES]) {
  1352. for (j = 0; j < SIZE_NUM; j++) {
  1353. print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][j],
  1354. lengths[j]);
  1355. Time_F(START);
  1356. for (count = 0, run = 1; COND(c[D_CBC_128_AES][j]); count++)
  1357. AES_cbc_encrypt(buf, buf,
  1358. (unsigned long)lengths[j], &aes_ks1,
  1359. iv, AES_ENCRYPT);
  1360. d = Time_F(STOP);
  1361. print_result(D_CBC_128_AES, j, count, d);
  1362. }
  1363. }
  1364. if (doit[D_CBC_192_AES]) {
  1365. for (j = 0; j < SIZE_NUM; j++) {
  1366. print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][j],
  1367. lengths[j]);
  1368. Time_F(START);
  1369. for (count = 0, run = 1; COND(c[D_CBC_192_AES][j]); count++)
  1370. AES_cbc_encrypt(buf, buf,
  1371. (unsigned long)lengths[j], &aes_ks2,
  1372. iv, AES_ENCRYPT);
  1373. d = Time_F(STOP);
  1374. print_result(D_CBC_192_AES, j, count, d);
  1375. }
  1376. }
  1377. if (doit[D_CBC_256_AES]) {
  1378. for (j = 0; j < SIZE_NUM; j++) {
  1379. print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][j],
  1380. lengths[j]);
  1381. Time_F(START);
  1382. for (count = 0, run = 1; COND(c[D_CBC_256_AES][j]); count++)
  1383. AES_cbc_encrypt(buf, buf,
  1384. (unsigned long)lengths[j], &aes_ks3,
  1385. iv, AES_ENCRYPT);
  1386. d = Time_F(STOP);
  1387. print_result(D_CBC_256_AES, j, count, d);
  1388. }
  1389. }
  1390. if (doit[D_IGE_128_AES]) {
  1391. for (j = 0; j < SIZE_NUM; j++) {
  1392. print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][j],
  1393. lengths[j]);
  1394. Time_F(START);
  1395. for (count = 0, run = 1; COND(c[D_IGE_128_AES][j]); count++)
  1396. AES_ige_encrypt(buf, buf2,
  1397. (unsigned long)lengths[j], &aes_ks1,
  1398. iv, AES_ENCRYPT);
  1399. d = Time_F(STOP);
  1400. print_result(D_IGE_128_AES, j, count, d);
  1401. }
  1402. }
  1403. if (doit[D_IGE_192_AES]) {
  1404. for (j = 0; j < SIZE_NUM; j++) {
  1405. print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][j],
  1406. lengths[j]);
  1407. Time_F(START);
  1408. for (count = 0, run = 1; COND(c[D_IGE_192_AES][j]); count++)
  1409. AES_ige_encrypt(buf, buf2,
  1410. (unsigned long)lengths[j], &aes_ks2,
  1411. iv, AES_ENCRYPT);
  1412. d = Time_F(STOP);
  1413. print_result(D_IGE_192_AES, j, count, d);
  1414. }
  1415. }
  1416. if (doit[D_IGE_256_AES]) {
  1417. for (j = 0; j < SIZE_NUM; j++) {
  1418. print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][j],
  1419. lengths[j]);
  1420. Time_F(START);
  1421. for (count = 0, run = 1; COND(c[D_IGE_256_AES][j]); count++)
  1422. AES_ige_encrypt(buf, buf2,
  1423. (unsigned long)lengths[j], &aes_ks3,
  1424. iv, AES_ENCRYPT);
  1425. d = Time_F(STOP);
  1426. print_result(D_IGE_256_AES, j, count, d);
  1427. }
  1428. }
  1429. if (doit[D_GHASH]) {
  1430. GCM128_CONTEXT *ctx =
  1431. CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
  1432. CRYPTO_gcm128_setiv(ctx, (unsigned char *)"0123456789ab", 12);
  1433. for (j = 0; j < SIZE_NUM; j++) {
  1434. print_message(names[D_GHASH], c[D_GHASH][j], lengths[j]);
  1435. Time_F(START);
  1436. for (count = 0, run = 1; COND(c[D_GHASH][j]); count++)
  1437. CRYPTO_gcm128_aad(ctx, buf, lengths[j]);
  1438. d = Time_F(STOP);
  1439. print_result(D_GHASH, j, count, d);
  1440. }
  1441. CRYPTO_gcm128_release(ctx);
  1442. }
  1443. #endif
  1444. #ifndef OPENSSL_NO_CAMELLIA
  1445. if (doit[D_CBC_128_CML]) {
  1446. for (j = 0; j < SIZE_NUM; j++) {
  1447. print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][j],
  1448. lengths[j]);
  1449. Time_F(START);
  1450. for (count = 0, run = 1; COND(c[D_CBC_128_CML][j]); count++)
  1451. Camellia_cbc_encrypt(buf, buf,
  1452. (unsigned long)lengths[j], &camellia_ks1,
  1453. iv, CAMELLIA_ENCRYPT);
  1454. d = Time_F(STOP);
  1455. print_result(D_CBC_128_CML, j, count, d);
  1456. }
  1457. }
  1458. if (doit[D_CBC_192_CML]) {
  1459. for (j = 0; j < SIZE_NUM; j++) {
  1460. print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][j],
  1461. lengths[j]);
  1462. Time_F(START);
  1463. for (count = 0, run = 1; COND(c[D_CBC_192_CML][j]); count++)
  1464. Camellia_cbc_encrypt(buf, buf,
  1465. (unsigned long)lengths[j], &camellia_ks2,
  1466. iv, CAMELLIA_ENCRYPT);
  1467. d = Time_F(STOP);
  1468. print_result(D_CBC_192_CML, j, count, d);
  1469. }
  1470. }
  1471. if (doit[D_CBC_256_CML]) {
  1472. for (j = 0; j < SIZE_NUM; j++) {
  1473. print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][j],
  1474. lengths[j]);
  1475. Time_F(START);
  1476. for (count = 0, run = 1; COND(c[D_CBC_256_CML][j]); count++)
  1477. Camellia_cbc_encrypt(buf, buf,
  1478. (unsigned long)lengths[j], &camellia_ks3,
  1479. iv, CAMELLIA_ENCRYPT);
  1480. d = Time_F(STOP);
  1481. print_result(D_CBC_256_CML, j, count, d);
  1482. }
  1483. }
  1484. #endif
  1485. #ifndef OPENSSL_NO_IDEA
  1486. if (doit[D_CBC_IDEA]) {
  1487. for (j = 0; j < SIZE_NUM; j++) {
  1488. print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][j], lengths[j]);
  1489. Time_F(START);
  1490. for (count = 0, run = 1; COND(c[D_CBC_IDEA][j]); count++)
  1491. idea_cbc_encrypt(buf, buf,
  1492. (unsigned long)lengths[j], &idea_ks,
  1493. iv, IDEA_ENCRYPT);
  1494. d = Time_F(STOP);
  1495. print_result(D_CBC_IDEA, j, count, d);
  1496. }
  1497. }
  1498. #endif
  1499. #ifndef OPENSSL_NO_SEED
  1500. if (doit[D_CBC_SEED]) {
  1501. for (j = 0; j < SIZE_NUM; j++) {
  1502. print_message(names[D_CBC_SEED], c[D_CBC_SEED][j], lengths[j]);
  1503. Time_F(START);
  1504. for (count = 0, run = 1; COND(c[D_CBC_SEED][j]); count++)
  1505. SEED_cbc_encrypt(buf, buf,
  1506. (unsigned long)lengths[j], &seed_ks, iv, 1);
  1507. d = Time_F(STOP);
  1508. print_result(D_CBC_SEED, j, count, d);
  1509. }
  1510. }
  1511. #endif
  1512. #ifndef OPENSSL_NO_RC2
  1513. if (doit[D_CBC_RC2]) {
  1514. for (j = 0; j < SIZE_NUM; j++) {
  1515. print_message(names[D_CBC_RC2], c[D_CBC_RC2][j], lengths[j]);
  1516. Time_F(START);
  1517. for (count = 0, run = 1; COND(c[D_CBC_RC2][j]); count++)
  1518. RC2_cbc_encrypt(buf, buf,
  1519. (unsigned long)lengths[j], &rc2_ks,
  1520. iv, RC2_ENCRYPT);
  1521. d = Time_F(STOP);
  1522. print_result(D_CBC_RC2, j, count, d);
  1523. }
  1524. }
  1525. #endif
  1526. #ifndef OPENSSL_NO_RC5
  1527. if (doit[D_CBC_RC5]) {
  1528. for (j = 0; j < SIZE_NUM; j++) {
  1529. print_message(names[D_CBC_RC5], c[D_CBC_RC5][j], lengths[j]);
  1530. Time_F(START);
  1531. for (count = 0, run = 1; COND(c[D_CBC_RC5][j]); count++)
  1532. RC5_32_cbc_encrypt(buf, buf,
  1533. (unsigned long)lengths[j], &rc5_ks,
  1534. iv, RC5_ENCRYPT);
  1535. d = Time_F(STOP);
  1536. print_result(D_CBC_RC5, j, count, d);
  1537. }
  1538. }
  1539. #endif
  1540. #ifndef OPENSSL_NO_BF
  1541. if (doit[D_CBC_BF]) {
  1542. for (j = 0; j < SIZE_NUM; j++) {
  1543. print_message(names[D_CBC_BF], c[D_CBC_BF][j], lengths[j]);
  1544. Time_F(START);
  1545. for (count = 0, run = 1; COND(c[D_CBC_BF][j]); count++)
  1546. BF_cbc_encrypt(buf, buf,
  1547. (unsigned long)lengths[j], &bf_ks,
  1548. iv, BF_ENCRYPT);
  1549. d = Time_F(STOP);
  1550. print_result(D_CBC_BF, j, count, d);
  1551. }
  1552. }
  1553. #endif
  1554. #ifndef OPENSSL_NO_CAST
  1555. if (doit[D_CBC_CAST]) {
  1556. for (j = 0; j < SIZE_NUM; j++) {
  1557. print_message(names[D_CBC_CAST], c[D_CBC_CAST][j], lengths[j]);
  1558. Time_F(START);
  1559. for (count = 0, run = 1; COND(c[D_CBC_CAST][j]); count++)
  1560. CAST_cbc_encrypt(buf, buf,
  1561. (unsigned long)lengths[j], &cast_ks,
  1562. iv, CAST_ENCRYPT);
  1563. d = Time_F(STOP);
  1564. print_result(D_CBC_CAST, j, count, d);
  1565. }
  1566. }
  1567. #endif
  1568. if (doit[D_EVP]) {
  1569. #ifdef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
  1570. if (multiblock && evp_cipher) {
  1571. if (!
  1572. (EVP_CIPHER_flags(evp_cipher) &
  1573. EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
  1574. fprintf(stderr, "%s is not multi-block capable\n",
  1575. OBJ_nid2ln(evp_cipher->nid));
  1576. goto end;
  1577. }
  1578. multiblock_speed(evp_cipher);
  1579. ret = 0;
  1580. goto end;
  1581. }
  1582. #endif
  1583. for (j = 0; j < SIZE_NUM; j++) {
  1584. if (evp_cipher) {
  1585. EVP_CIPHER_CTX ctx;
  1586. int outl;
  1587. names[D_EVP] = OBJ_nid2ln(evp_cipher->nid);
  1588. /*
  1589. * -O3 -fschedule-insns messes up an optimization here!
  1590. * names[D_EVP] somehow becomes NULL
  1591. */
  1592. print_message(names[D_EVP], save_count, lengths[j]);
  1593. EVP_CIPHER_CTX_init(&ctx);
  1594. if (decrypt)
  1595. EVP_DecryptInit_ex(&ctx, evp_cipher, NULL, key16, iv);
  1596. else
  1597. EVP_EncryptInit_ex(&ctx, evp_cipher, NULL, key16, iv);
  1598. EVP_CIPHER_CTX_set_padding(&ctx, 0);
  1599. Time_F(START);
  1600. if (decrypt)
  1601. for (count = 0, run = 1;
  1602. COND(save_count * 4 * lengths[0] / lengths[j]);
  1603. count++)
  1604. EVP_DecryptUpdate(&ctx, buf, &outl, buf, lengths[j]);
  1605. else
  1606. for (count = 0, run = 1;
  1607. COND(save_count * 4 * lengths[0] / lengths[j]);
  1608. count++)
  1609. EVP_EncryptUpdate(&ctx, buf, &outl, buf, lengths[j]);
  1610. if (decrypt)
  1611. EVP_DecryptFinal_ex(&ctx, buf, &outl);
  1612. else
  1613. EVP_EncryptFinal_ex(&ctx, buf, &outl);
  1614. d = Time_F(STOP);
  1615. EVP_CIPHER_CTX_cleanup(&ctx);
  1616. }
  1617. if (evp_md) {
  1618. names[D_EVP] = OBJ_nid2ln(evp_md->type);
  1619. print_message(names[D_EVP], save_count, lengths[j]);
  1620. Time_F(START);
  1621. for (count = 0, run = 1;
  1622. COND(save_count * 4 * lengths[0] / lengths[j]); count++)
  1623. EVP_Digest(buf, lengths[j], &(md[0]), NULL, evp_md, NULL);
  1624. d = Time_F(STOP);
  1625. }
  1626. print_result(D_EVP, j, count, d);
  1627. }
  1628. }
  1629. RAND_bytes(buf, 36);
  1630. #ifndef OPENSSL_NO_RSA
  1631. for (j = 0; j < RSA_NUM; j++) {
  1632. int st;
  1633. if (!rsa_doit[j])
  1634. continue;
  1635. st = RSA_sign(NID_md5_sha1, buf, 36, buf2, &rsa_num, rsa_key[j]);
  1636. if (st == 0) {
  1637. BIO_printf(bio_err,
  1638. "RSA sign failure. No RSA sign will be done.\n");
  1639. ERR_print_errors(bio_err);
  1640. rsa_count = 1;
  1641. } else {
  1642. pkey_print_message("private", "rsa",
  1643. rsa_c[j][0], rsa_bits[j], RSA_SECONDS);
  1644. /* RSA_blinding_on(rsa_key[j],NULL); */
  1645. Time_F(START);
  1646. for (count = 0, run = 1; COND(rsa_c[j][0]); count++) {
  1647. st = RSA_sign(NID_md5_sha1, buf, 36, buf2,
  1648. &rsa_num, rsa_key[j]);
  1649. if (st == 0) {
  1650. BIO_printf(bio_err, "RSA sign failure\n");
  1651. ERR_print_errors(bio_err);
  1652. count = 1;
  1653. break;
  1654. }
  1655. }
  1656. d = Time_F(STOP);
  1657. BIO_printf(bio_err,
  1658. mr ? "+R1:%ld:%d:%.2f\n"
  1659. : "%ld %d bit private RSA's in %.2fs\n",
  1660. count, rsa_bits[j], d);
  1661. rsa_results[j][0] = d / (double)count;
  1662. rsa_count = count;
  1663. }
  1664. st = RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[j]);
  1665. if (st <= 0) {
  1666. BIO_printf(bio_err,
  1667. "RSA verify failure. No RSA verify will be done.\n");
  1668. ERR_print_errors(bio_err);
  1669. rsa_doit[j] = 0;
  1670. } else {
  1671. pkey_print_message("public", "rsa",
  1672. rsa_c[j][1], rsa_bits[j], RSA_SECONDS);
  1673. Time_F(START);
  1674. for (count = 0, run = 1; COND(rsa_c[j][1]); count++) {
  1675. st = RSA_verify(NID_md5_sha1, buf, 36, buf2,
  1676. rsa_num, rsa_key[j]);
  1677. if (st <= 0) {
  1678. BIO_printf(bio_err, "RSA verify failure\n");
  1679. ERR_print_errors(bio_err);
  1680. count = 1;
  1681. break;
  1682. }
  1683. }
  1684. d = Time_F(STOP);
  1685. BIO_printf(bio_err,
  1686. mr ? "+R2:%ld:%d:%.2f\n"
  1687. : "%ld %d bit public RSA's in %.2fs\n",
  1688. count, rsa_bits[j], d);
  1689. rsa_results[j][1] = d / (double)count;
  1690. }
  1691. if (rsa_count <= 1) {
  1692. /* if longer than 10s, don't do any more */
  1693. for (j++; j < RSA_NUM; j++)
  1694. rsa_doit[j] = 0;
  1695. }
  1696. }
  1697. #endif
  1698. RAND_bytes(buf, 20);
  1699. #ifndef OPENSSL_NO_DSA
  1700. if (RAND_status() != 1) {
  1701. RAND_seed(rnd_seed, sizeof rnd_seed);
  1702. rnd_fake = 1;
  1703. }
  1704. for (j = 0; j < DSA_NUM; j++) {
  1705. unsigned int kk;
  1706. int st;
  1707. if (!dsa_doit[j])
  1708. continue;
  1709. /* DSA_generate_key(dsa_key[j]); */
  1710. /* DSA_sign_setup(dsa_key[j],NULL); */
  1711. st = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, &kk, dsa_key[j]);
  1712. if (st == 0) {
  1713. BIO_printf(bio_err,
  1714. "DSA sign failure. No DSA sign will be done.\n");
  1715. ERR_print_errors(bio_err);
  1716. rsa_count = 1;
  1717. } else {
  1718. pkey_print_message("sign", "dsa",
  1719. dsa_c[j][0], dsa_bits[j], DSA_SECONDS);
  1720. Time_F(START);
  1721. for (count = 0, run = 1; COND(dsa_c[j][0]); count++) {
  1722. st = DSA_sign(EVP_PKEY_DSA, buf, 20, buf2, &kk, dsa_key[j]);
  1723. if (st == 0) {
  1724. BIO_printf(bio_err, "DSA sign failure\n");
  1725. ERR_print_errors(bio_err);
  1726. count = 1;
  1727. break;
  1728. }
  1729. }
  1730. d = Time_F(STOP);
  1731. BIO_printf(bio_err,
  1732. mr ? "+R3:%ld:%d:%.2f\n"
  1733. : "%ld %d bit DSA signs in %.2fs\n",
  1734. count, dsa_bits[j], d);
  1735. dsa_results[j][0] = d / (double)count;
  1736. rsa_count = count;
  1737. }
  1738. st = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, kk, dsa_key[j]);
  1739. if (st <= 0) {
  1740. BIO_printf(bio_err,
  1741. "DSA verify failure. No DSA verify will be done.\n");
  1742. ERR_print_errors(bio_err);
  1743. dsa_doit[j] = 0;
  1744. } else {
  1745. pkey_print_message("verify", "dsa",
  1746. dsa_c[j][1], dsa_bits[j], DSA_SECONDS);
  1747. Time_F(START);
  1748. for (count = 0, run = 1; COND(dsa_c[j][1]); count++) {
  1749. st = DSA_verify(EVP_PKEY_DSA, buf, 20, buf2, kk, dsa_key[j]);
  1750. if (st <= 0) {
  1751. BIO_printf(bio_err, "DSA verify failure\n");
  1752. ERR_print_errors(bio_err);
  1753. count = 1;
  1754. break;
  1755. }
  1756. }
  1757. d = Time_F(STOP);
  1758. BIO_printf(bio_err,
  1759. mr ? "+R4:%ld:%d:%.2f\n"
  1760. : "%ld %d bit DSA verify in %.2fs\n",
  1761. count, dsa_bits[j], d);
  1762. dsa_results[j][1] = d / (double)count;
  1763. }
  1764. if (rsa_count <= 1) {
  1765. /* if longer than 10s, don't do any more */
  1766. for (j++; j < DSA_NUM; j++)
  1767. dsa_doit[j] = 0;
  1768. }
  1769. }
  1770. if (rnd_fake)
  1771. RAND_cleanup();
  1772. #endif
  1773. #ifndef OPENSSL_NO_EC
  1774. if (RAND_status() != 1) {
  1775. RAND_seed(rnd_seed, sizeof rnd_seed);
  1776. rnd_fake = 1;
  1777. }
  1778. for (j = 0; j < EC_NUM; j++) {
  1779. int st;
  1780. if (!ecdsa_doit[j])
  1781. continue; /* Ignore Curve */
  1782. ecdsa[j] = EC_KEY_new_by_curve_name(test_curves[j]);
  1783. if (ecdsa[j] == NULL) {
  1784. BIO_printf(bio_err, "ECDSA failure.\n");
  1785. ERR_print_errors(bio_err);
  1786. rsa_count = 1;
  1787. } else {
  1788. EC_KEY_precompute_mult(ecdsa[j], NULL);
  1789. /* Perform ECDSA signature test */
  1790. EC_KEY_generate_key(ecdsa[j]);
  1791. st = ECDSA_sign(0, buf, 20, ecdsasig, &ecdsasiglen, ecdsa[j]);
  1792. if (st == 0) {
  1793. BIO_printf(bio_err,
  1794. "ECDSA sign failure. No ECDSA sign will be done.\n");
  1795. ERR_print_errors(bio_err);
  1796. rsa_count = 1;
  1797. } else {
  1798. pkey_print_message("sign", "ecdsa",
  1799. ecdsa_c[j][0],
  1800. test_curves_bits[j], ECDSA_SECONDS);
  1801. Time_F(START);
  1802. for (count = 0, run = 1; COND(ecdsa_c[j][0]); count++) {
  1803. st = ECDSA_sign(0, buf, 20,
  1804. ecdsasig, &ecdsasiglen, ecdsa[j]);
  1805. if (st == 0) {
  1806. BIO_printf(bio_err, "ECDSA sign failure\n");
  1807. ERR_print_errors(bio_err);
  1808. count = 1;
  1809. break;
  1810. }
  1811. }
  1812. d = Time_F(STOP);
  1813. BIO_printf(bio_err,
  1814. mr ? "+R5:%ld:%d:%.2f\n" :
  1815. "%ld %d bit ECDSA signs in %.2fs \n",
  1816. count, test_curves_bits[j], d);
  1817. ecdsa_results[j][0] = d / (double)count;
  1818. rsa_count = count;
  1819. }
  1820. /* Perform ECDSA verification test */
  1821. st = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[j]);
  1822. if (st != 1) {
  1823. BIO_printf(bio_err,
  1824. "ECDSA verify failure. No ECDSA verify will be done.\n");
  1825. ERR_print_errors(bio_err);
  1826. ecdsa_doit[j] = 0;
  1827. } else {
  1828. pkey_print_message("verify", "ecdsa",
  1829. ecdsa_c[j][1],
  1830. test_curves_bits[j], ECDSA_SECONDS);
  1831. Time_F(START);
  1832. for (count = 0, run = 1; COND(ecdsa_c[j][1]); count++) {
  1833. st = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen,
  1834. ecdsa[j]);
  1835. if (st != 1) {
  1836. BIO_printf(bio_err, "ECDSA verify failure\n");
  1837. ERR_print_errors(bio_err);
  1838. count = 1;
  1839. break;
  1840. }
  1841. }
  1842. d = Time_F(STOP);
  1843. BIO_printf(bio_err,
  1844. mr ? "+R6:%ld:%d:%.2f\n"
  1845. : "%ld %d bit ECDSA verify in %.2fs\n",
  1846. count, test_curves_bits[j], d);
  1847. ecdsa_results[j][1] = d / (double)count;
  1848. }
  1849. if (rsa_count <= 1) {
  1850. /* if longer than 10s, don't do any more */
  1851. for (j++; j < EC_NUM; j++)
  1852. ecdsa_doit[j] = 0;
  1853. }
  1854. }
  1855. }
  1856. if (rnd_fake)
  1857. RAND_cleanup();
  1858. #endif
  1859. #ifndef OPENSSL_NO_EC
  1860. if (RAND_status() != 1) {
  1861. RAND_seed(rnd_seed, sizeof rnd_seed);
  1862. rnd_fake = 1;
  1863. }
  1864. for (j = 0; j < EC_NUM; j++) {
  1865. if (!ecdh_doit[j])
  1866. continue;
  1867. ecdh_a[j] = EC_KEY_new_by_curve_name(test_curves[j]);
  1868. ecdh_b[j] = EC_KEY_new_by_curve_name(test_curves[j]);
  1869. if ((ecdh_a[j] == NULL) || (ecdh_b[j] == NULL)) {
  1870. BIO_printf(bio_err, "ECDH failure.\n");
  1871. ERR_print_errors(bio_err);
  1872. rsa_count = 1;
  1873. } else {
  1874. /* generate two ECDH key pairs */
  1875. if (!EC_KEY_generate_key(ecdh_a[j]) ||
  1876. !EC_KEY_generate_key(ecdh_b[j])) {
  1877. BIO_printf(bio_err, "ECDH key generation failure.\n");
  1878. ERR_print_errors(bio_err);
  1879. rsa_count = 1;
  1880. } else {
  1881. /*
  1882. * If field size is not more than 24 octets, then use SHA-1
  1883. * hash of result; otherwise, use result (see section 4.8 of
  1884. * draft-ietf-tls-ecc-03.txt).
  1885. */
  1886. int field_size, outlen;
  1887. void *(*kdf) (const void *in, size_t inlen, void *out,
  1888. size_t *xoutlen);
  1889. field_size =
  1890. EC_GROUP_get_degree(EC_KEY_get0_group(ecdh_a[j]));
  1891. if (field_size <= 24 * 8) {
  1892. outlen = KDF1_SHA1_len;
  1893. kdf = KDF1_SHA1;
  1894. } else {
  1895. outlen = (field_size + 7) / 8;
  1896. kdf = NULL;
  1897. }
  1898. secret_size_a =
  1899. ECDH_compute_key(secret_a, outlen,
  1900. EC_KEY_get0_public_key(ecdh_b[j]),
  1901. ecdh_a[j], kdf);
  1902. secret_size_b =
  1903. ECDH_compute_key(secret_b, outlen,
  1904. EC_KEY_get0_public_key(ecdh_a[j]),
  1905. ecdh_b[j], kdf);
  1906. if (secret_size_a != secret_size_b)
  1907. ecdh_checks = 0;
  1908. else
  1909. ecdh_checks = 1;
  1910. for (secret_idx = 0; (secret_idx < secret_size_a)
  1911. && (ecdh_checks == 1); secret_idx++) {
  1912. if (secret_a[secret_idx] != secret_b[secret_idx])
  1913. ecdh_checks = 0;
  1914. }
  1915. if (ecdh_checks == 0) {
  1916. BIO_printf(bio_err, "ECDH computations don't match.\n");
  1917. ERR_print_errors(bio_err);
  1918. rsa_count = 1;
  1919. }
  1920. pkey_print_message("", "ecdh",
  1921. ecdh_c[j][0],
  1922. test_curves_bits[j], ECDH_SECONDS);
  1923. Time_F(START);
  1924. for (count = 0, run = 1; COND(ecdh_c[j][0]); count++) {
  1925. ECDH_compute_key(secret_a, outlen,
  1926. EC_KEY_get0_public_key(ecdh_b[j]),
  1927. ecdh_a[j], kdf);
  1928. }
  1929. d = Time_F(STOP);
  1930. BIO_printf(bio_err,
  1931. mr ? "+R7:%ld:%d:%.2f\n" :
  1932. "%ld %d-bit ECDH ops in %.2fs\n", count,
  1933. test_curves_bits[j], d);
  1934. ecdh_results[j][0] = d / (double)count;
  1935. rsa_count = count;
  1936. }
  1937. }
  1938. if (rsa_count <= 1) {
  1939. /* if longer than 10s, don't do any more */
  1940. for (j++; j < EC_NUM; j++)
  1941. ecdh_doit[j] = 0;
  1942. }
  1943. }
  1944. if (rnd_fake)
  1945. RAND_cleanup();
  1946. #endif
  1947. #ifndef NO_FORK
  1948. show_res:
  1949. #endif
  1950. if (!mr) {
  1951. printf("%s\n", SSLeay_version(SSLEAY_VERSION));
  1952. printf("%s\n", SSLeay_version(SSLEAY_BUILT_ON));
  1953. printf("options:");
  1954. printf("%s ", BN_options());
  1955. #ifndef OPENSSL_NO_MD2
  1956. printf("%s ", MD2_options());
  1957. #endif
  1958. #ifndef OPENSSL_NO_RC4
  1959. printf("%s ", RC4_options());
  1960. #endif
  1961. #ifndef OPENSSL_NO_DES
  1962. printf("%s ", DES_options());
  1963. #endif
  1964. #ifndef OPENSSL_NO_AES
  1965. printf("%s ", AES_options());
  1966. #endif
  1967. #ifndef OPENSSL_NO_IDEA
  1968. printf("%s ", idea_options());
  1969. #endif
  1970. #ifndef OPENSSL_NO_BF
  1971. printf("%s ", BF_options());
  1972. #endif
  1973. printf("\n%s\n", SSLeay_version(SSLEAY_CFLAGS));
  1974. }
  1975. if (pr_header) {
  1976. if (mr)
  1977. printf("+H");
  1978. else {
  1979. printf
  1980. ("The 'numbers' are in 1000s of bytes per second processed.\n");
  1981. printf("type ");
  1982. }
  1983. for (j = 0; j < SIZE_NUM; j++)
  1984. printf(mr ? ":%d" : "%7d bytes", lengths[j]);
  1985. printf("\n");
  1986. }
  1987. for (k = 0; k < ALGOR_NUM; k++) {
  1988. if (!doit[k])
  1989. continue;
  1990. if (mr)
  1991. printf("+F:%d:%s", k, names[k]);
  1992. else
  1993. printf("%-13s", names[k]);
  1994. for (j = 0; j < SIZE_NUM; j++) {
  1995. if (results[k][j] > 10000 && !mr)
  1996. printf(" %11.2fk", results[k][j] / 1e3);
  1997. else
  1998. printf(mr ? ":%.2f" : " %11.2f ", results[k][j]);
  1999. }
  2000. printf("\n");
  2001. }
  2002. #ifndef OPENSSL_NO_RSA
  2003. j = 1;
  2004. for (k = 0; k < RSA_NUM; k++) {
  2005. if (!rsa_doit[k])
  2006. continue;
  2007. if (j && !mr) {
  2008. printf("%18ssign verify sign/s verify/s\n", " ");
  2009. j = 0;
  2010. }
  2011. if (mr)
  2012. printf("+F2:%u:%u:%f:%f\n",
  2013. k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
  2014. else
  2015. printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
  2016. rsa_bits[k], rsa_results[k][0], rsa_results[k][1],
  2017. 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1]);
  2018. }
  2019. #endif
  2020. #ifndef OPENSSL_NO_DSA
  2021. j = 1;
  2022. for (k = 0; k < DSA_NUM; k++) {
  2023. if (!dsa_doit[k])
  2024. continue;
  2025. if (j && !mr) {
  2026. printf("%18ssign verify sign/s verify/s\n", " ");
  2027. j = 0;
  2028. }
  2029. if (mr)
  2030. printf("+F3:%u:%u:%f:%f\n",
  2031. k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
  2032. else
  2033. printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
  2034. dsa_bits[k], dsa_results[k][0], dsa_results[k][1],
  2035. 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1]);
  2036. }
  2037. #endif
  2038. #ifndef OPENSSL_NO_EC
  2039. j = 1;
  2040. for (k = 0; k < EC_NUM; k++) {
  2041. if (!ecdsa_doit[k])
  2042. continue;
  2043. if (j && !mr) {
  2044. printf("%30ssign verify sign/s verify/s\n", " ");
  2045. j = 0;
  2046. }
  2047. if (mr)
  2048. printf("+F4:%u:%u:%f:%f\n",
  2049. k, test_curves_bits[k],
  2050. ecdsa_results[k][0], ecdsa_results[k][1]);
  2051. else
  2052. printf("%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
  2053. test_curves_bits[k],
  2054. test_curves_names[k],
  2055. ecdsa_results[k][0], ecdsa_results[k][1],
  2056. 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1]);
  2057. }
  2058. #endif
  2059. #ifndef OPENSSL_NO_EC
  2060. j = 1;
  2061. for (k = 0; k < EC_NUM; k++) {
  2062. if (!ecdh_doit[k])
  2063. continue;
  2064. if (j && !mr) {
  2065. printf("%30sop op/s\n", " ");
  2066. j = 0;
  2067. }
  2068. if (mr)
  2069. printf("+F5:%u:%u:%f:%f\n",
  2070. k, test_curves_bits[k],
  2071. ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
  2072. else
  2073. printf("%4u bit ecdh (%s) %8.4fs %8.1f\n",
  2074. test_curves_bits[k],
  2075. test_curves_names[k],
  2076. ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
  2077. }
  2078. #endif
  2079. ret = 0;
  2080. end:
  2081. ERR_print_errors(bio_err);
  2082. OPENSSL_free(save_buf);
  2083. OPENSSL_free(save_buf2);
  2084. #ifndef OPENSSL_NO_RSA
  2085. for (i = 0; i < RSA_NUM; i++)
  2086. RSA_free(rsa_key[i]);
  2087. #endif
  2088. #ifndef OPENSSL_NO_DSA
  2089. for (i = 0; i < DSA_NUM; i++)
  2090. DSA_free(dsa_key[i]);
  2091. #endif
  2092. #ifndef OPENSSL_NO_EC
  2093. for (i = 0; i < EC_NUM; i++) {
  2094. EC_KEY_free(ecdsa[i]);
  2095. EC_KEY_free(ecdh_a[i]);
  2096. EC_KEY_free(ecdh_b[i]);
  2097. }
  2098. #endif
  2099. return (ret);
  2100. }
  2101. static void print_message(const char *s, long num, int length)
  2102. {
  2103. #ifdef SIGALRM
  2104. BIO_printf(bio_err,
  2105. mr ? "+DT:%s:%d:%d\n"
  2106. : "Doing %s for %ds on %d size blocks: ", s, SECONDS, length);
  2107. (void)BIO_flush(bio_err);
  2108. alarm(SECONDS);
  2109. #else
  2110. BIO_printf(bio_err,
  2111. mr ? "+DN:%s:%ld:%d\n"
  2112. : "Doing %s %ld times on %d size blocks: ", s, num, length);
  2113. (void)BIO_flush(bio_err);
  2114. #endif
  2115. }
  2116. static void pkey_print_message(const char *str, const char *str2, long num,
  2117. int bits, int tm)
  2118. {
  2119. #ifdef SIGALRM
  2120. BIO_printf(bio_err,
  2121. mr ? "+DTP:%d:%s:%s:%d\n"
  2122. : "Doing %d bit %s %s's for %ds: ", bits, str, str2, tm);
  2123. (void)BIO_flush(bio_err);
  2124. alarm(tm);
  2125. #else
  2126. BIO_printf(bio_err,
  2127. mr ? "+DNP:%ld:%d:%s:%s\n"
  2128. : "Doing %ld %d bit %s %s's: ", num, bits, str, str2);
  2129. (void)BIO_flush(bio_err);
  2130. #endif
  2131. }
  2132. static void print_result(int alg, int run_no, int count, double time_used)
  2133. {
  2134. BIO_printf(bio_err,
  2135. mr ? "+R:%d:%s:%f\n"
  2136. : "%d %s's in %.2fs\n", count, names[alg], time_used);
  2137. results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
  2138. }
  2139. #ifndef NO_FORK
  2140. static char *sstrsep(char **string, const char *delim)
  2141. {
  2142. char isdelim[256];
  2143. char *token = *string;
  2144. if (**string == 0)
  2145. return NULL;
  2146. memset(isdelim, 0, sizeof isdelim);
  2147. isdelim[0] = 1;
  2148. while (*delim) {
  2149. isdelim[(unsigned char)(*delim)] = 1;
  2150. delim++;
  2151. }
  2152. while (!isdelim[(unsigned char)(**string)]) {
  2153. (*string)++;
  2154. }
  2155. if (**string) {
  2156. **string = 0;
  2157. (*string)++;
  2158. }
  2159. return token;
  2160. }
  2161. static int do_multi(int multi)
  2162. {
  2163. int n;
  2164. int fd[2];
  2165. int *fds;
  2166. static char sep[] = ":";
  2167. fds = malloc(sizeof(*fds) * multi);
  2168. for (n = 0; n < multi; ++n) {
  2169. if (pipe(fd) == -1) {
  2170. fprintf(stderr, "pipe failure\n");
  2171. exit(1);
  2172. }
  2173. fflush(stdout);
  2174. fflush(stderr);
  2175. if (fork()) {
  2176. close(fd[1]);
  2177. fds[n] = fd[0];
  2178. } else {
  2179. close(fd[0]);
  2180. close(1);
  2181. if (dup(fd[1]) == -1) {
  2182. fprintf(stderr, "dup failed\n");
  2183. exit(1);
  2184. }
  2185. close(fd[1]);
  2186. mr = 1;
  2187. usertime = 0;
  2188. free(fds);
  2189. return 0;
  2190. }
  2191. printf("Forked child %d\n", n);
  2192. }
  2193. /* for now, assume the pipe is long enough to take all the output */
  2194. for (n = 0; n < multi; ++n) {
  2195. FILE *f;
  2196. char buf[1024];
  2197. char *p;
  2198. f = fdopen(fds[n], "r");
  2199. while (fgets(buf, sizeof buf, f)) {
  2200. p = strchr(buf, '\n');
  2201. if (p)
  2202. *p = '\0';
  2203. if (buf[0] != '+') {
  2204. fprintf(stderr, "Don't understand line '%s' from child %d\n",
  2205. buf, n);
  2206. continue;
  2207. }
  2208. printf("Got: %s from %d\n", buf, n);
  2209. if (strncmp(buf, "+F:", 3) == 0) {
  2210. int alg;
  2211. int j;
  2212. p = buf + 3;
  2213. alg = atoi(sstrsep(&p, sep));
  2214. sstrsep(&p, sep);
  2215. for (j = 0; j < SIZE_NUM; ++j)
  2216. results[alg][j] += atof(sstrsep(&p, sep));
  2217. } else if (strncmp(buf, "+F2:", 4) == 0) {
  2218. int k;
  2219. double d;
  2220. p = buf + 4;
  2221. k = atoi(sstrsep(&p, sep));
  2222. sstrsep(&p, sep);
  2223. d = atof(sstrsep(&p, sep));
  2224. if (n)
  2225. rsa_results[k][0] = 1 / (1 / rsa_results[k][0] + 1 / d);
  2226. else
  2227. rsa_results[k][0] = d;
  2228. d = atof(sstrsep(&p, sep));
  2229. if (n)
  2230. rsa_results[k][1] = 1 / (1 / rsa_results[k][1] + 1 / d);
  2231. else
  2232. rsa_results[k][1] = d;
  2233. }
  2234. # ifndef OPENSSL_NO_DSA
  2235. else if (strncmp(buf, "+F3:", 4) == 0) {
  2236. int k;
  2237. double d;
  2238. p = buf + 4;
  2239. k = atoi(sstrsep(&p, sep));
  2240. sstrsep(&p, sep);
  2241. d = atof(sstrsep(&p, sep));
  2242. if (n)
  2243. dsa_results[k][0] = 1 / (1 / dsa_results[k][0] + 1 / d);
  2244. else
  2245. dsa_results[k][0] = d;
  2246. d = atof(sstrsep(&p, sep));
  2247. if (n)
  2248. dsa_results[k][1] = 1 / (1 / dsa_results[k][1] + 1 / d);
  2249. else
  2250. dsa_results[k][1] = d;
  2251. }
  2252. # endif
  2253. # ifndef OPENSSL_NO_EC
  2254. else if (strncmp(buf, "+F4:", 4) == 0) {
  2255. int k;
  2256. double d;
  2257. p = buf + 4;
  2258. k = atoi(sstrsep(&p, sep));
  2259. sstrsep(&p, sep);
  2260. d = atof(sstrsep(&p, sep));
  2261. if (n)
  2262. ecdsa_results[k][0] =
  2263. 1 / (1 / ecdsa_results[k][0] + 1 / d);
  2264. else
  2265. ecdsa_results[k][0] = d;
  2266. d = atof(sstrsep(&p, sep));
  2267. if (n)
  2268. ecdsa_results[k][1] =
  2269. 1 / (1 / ecdsa_results[k][1] + 1 / d);
  2270. else
  2271. ecdsa_results[k][1] = d;
  2272. }
  2273. # endif
  2274. # ifndef OPENSSL_NO_EC
  2275. else if (strncmp(buf, "+F5:", 4) == 0) {
  2276. int k;
  2277. double d;
  2278. p = buf + 4;
  2279. k = atoi(sstrsep(&p, sep));
  2280. sstrsep(&p, sep);
  2281. d = atof(sstrsep(&p, sep));
  2282. if (n)
  2283. ecdh_results[k][0] = 1 / (1 / ecdh_results[k][0] + 1 / d);
  2284. else
  2285. ecdh_results[k][0] = d;
  2286. }
  2287. # endif
  2288. else if (strncmp(buf, "+H:", 3) == 0) {
  2289. ;
  2290. } else
  2291. fprintf(stderr, "Unknown type '%s' from child %d\n", buf, n);
  2292. }
  2293. fclose(f);
  2294. }
  2295. free(fds);
  2296. return 1;
  2297. }
  2298. #endif
  2299. static void multiblock_speed(const EVP_CIPHER *evp_cipher)
  2300. {
  2301. static int mblengths[] =
  2302. { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
  2303. int j, count, num = OSSL_NELEM(lengths);
  2304. const char *alg_name;
  2305. unsigned char *inp, *out, no_key[32], no_iv[16];
  2306. EVP_CIPHER_CTX ctx;
  2307. double d = 0.0;
  2308. inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
  2309. out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
  2310. EVP_CIPHER_CTX_init(&ctx);
  2311. EVP_EncryptInit_ex(&ctx, evp_cipher, NULL, no_key, no_iv);
  2312. EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key),
  2313. no_key);
  2314. alg_name = OBJ_nid2ln(evp_cipher->nid);
  2315. for (j = 0; j < num; j++) {
  2316. print_message(alg_name, 0, mblengths[j]);
  2317. Time_F(START);
  2318. for (count = 0, run = 1; run && count < 0x7fffffff; count++) {
  2319. unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
  2320. EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
  2321. size_t len = mblengths[j];
  2322. int packlen;
  2323. memset(aad, 0, 8); /* avoid uninitialized values */
  2324. aad[8] = 23; /* SSL3_RT_APPLICATION_DATA */
  2325. aad[9] = 3; /* version */
  2326. aad[10] = 2;
  2327. aad[11] = 0; /* length */
  2328. aad[12] = 0;
  2329. mb_param.out = NULL;
  2330. mb_param.inp = aad;
  2331. mb_param.len = len;
  2332. mb_param.interleave = 8;
  2333. packlen = EVP_CIPHER_CTX_ctrl(&ctx,
  2334. EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
  2335. sizeof(mb_param), &mb_param);
  2336. if (packlen > 0) {
  2337. mb_param.out = out;
  2338. mb_param.inp = inp;
  2339. mb_param.len = len;
  2340. EVP_CIPHER_CTX_ctrl(&ctx,
  2341. EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
  2342. sizeof(mb_param), &mb_param);
  2343. } else {
  2344. int pad;
  2345. RAND_bytes(out, 16);
  2346. len += 16;
  2347. aad[11] = len >> 8;
  2348. aad[12] = len;
  2349. pad = EVP_CIPHER_CTX_ctrl(&ctx,
  2350. EVP_CTRL_AEAD_TLS1_AAD,
  2351. EVP_AEAD_TLS1_AAD_LEN, aad);
  2352. EVP_Cipher(&ctx, out, inp, len + pad);
  2353. }
  2354. }
  2355. d = Time_F(STOP);
  2356. BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
  2357. : "%d %s's in %.2fs\n", count, "evp", d);
  2358. results[D_EVP][j] = ((double)count) / d * mblengths[j];
  2359. }
  2360. if (mr) {
  2361. fprintf(stdout, "+H");
  2362. for (j = 0; j < num; j++)
  2363. fprintf(stdout, ":%d", mblengths[j]);
  2364. fprintf(stdout, "\n");
  2365. fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
  2366. for (j = 0; j < num; j++)
  2367. fprintf(stdout, ":%.2f", results[D_EVP][j]);
  2368. fprintf(stdout, "\n");
  2369. } else {
  2370. fprintf(stdout,
  2371. "The 'numbers' are in 1000s of bytes per second processed.\n");
  2372. fprintf(stdout, "type ");
  2373. for (j = 0; j < num; j++)
  2374. fprintf(stdout, "%7d bytes", mblengths[j]);
  2375. fprintf(stdout, "\n");
  2376. fprintf(stdout, "%-24s", alg_name);
  2377. for (j = 0; j < num; j++) {
  2378. if (results[D_EVP][j] > 10000)
  2379. fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
  2380. else
  2381. fprintf(stdout, " %11.2f ", results[D_EVP][j]);
  2382. }
  2383. fprintf(stdout, "\n");
  2384. }
  2385. OPENSSL_free(inp);
  2386. OPENSSL_free(out);
  2387. }