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