speed.c 78 KB

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