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