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