]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/speed.c
Reintroduce preliminary sanity check in ECDH speed and remove further checks in the...
[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
ed7377db
NT
1034static int ECDH_EVP_derive_key_loop(void *args)
1035{
1036 loopargs_t *tempargs = *(loopargs_t **) args;
ed7377db
NT
1037 EVP_PKEY_CTX *ctx = tempargs->ecdh_ctx[testnum];
1038 unsigned char *derived_secret = tempargs->secret_a;
358558eb 1039 int count;
cc98e639 1040 size_t *outlen = &(tempargs->outlen[testnum]);
3331e43b 1041
db1dd936 1042 for (count = 0; COND(ecdh_c[testnum][0]); count++)
f7d984dd
NT
1043 EVP_PKEY_derive(ctx, derived_secret, outlen);
1044
8b0b80d9
AG
1045 return count;
1046}
5f986ed3 1047
d6073e27 1048#endif /* OPENSSL_NO_EC */
8b0b80d9 1049
700b8145 1050static int run_benchmark(int async_jobs,
29dd15b1 1051 int (*loop_function) (void *), loopargs_t * loopargs)
8b0b80d9
AG
1052{
1053 int job_op_count = 0;
1054 int total_op_count = 0;
1055 int num_inprogress = 0;
700b8145 1056 int error = 0, i = 0, ret = 0;
1e613922
AG
1057 OSSL_ASYNC_FD job_fd = 0;
1058 size_t num_job_fds = 0;
8b0b80d9
AG
1059
1060 run = 1;
1061
0ff43435 1062 if (async_jobs == 0) {
fb2141c7 1063 return loop_function((void *)&loopargs);
8b0b80d9
AG
1064 }
1065
1066 for (i = 0; i < async_jobs && !error; i++) {
fb2141c7
F
1067 loopargs_t *looparg_item = loopargs + i;
1068
1069 /* Copy pointer content (looparg_t item address) into async context */
700b8145
F
1070 ret = ASYNC_start_job(&loopargs[i].inprogress_job, loopargs[i].wait_ctx,
1071 &job_op_count, loop_function,
fb2141c7 1072 (void *)&looparg_item, sizeof(looparg_item));
700b8145 1073 switch (ret) {
fd4b0c08
F
1074 case ASYNC_PAUSE:
1075 ++num_inprogress;
1076 break;
1077 case ASYNC_FINISH:
1078 if (job_op_count == -1) {
8b0b80d9 1079 error = 1;
fd4b0c08
F
1080 } else {
1081 total_op_count += job_op_count;
1082 }
1083 break;
1084 case ASYNC_NO_JOBS:
1085 case ASYNC_ERR:
1086 BIO_printf(bio_err, "Failure in the job\n");
1087 ERR_print_errors(bio_err);
1088 error = 1;
1089 break;
8b0b80d9
AG
1090 }
1091 }
1092
1093 while (num_inprogress > 0) {
2ea92604 1094#if defined(OPENSSL_SYS_WINDOWS)
564e1029 1095 DWORD avail = 0;
2ea92604 1096#elif defined(OPENSSL_SYS_UNIX)
8b0b80d9 1097 int select_result = 0;
564e1029
AG
1098 OSSL_ASYNC_FD max_fd = 0;
1099 fd_set waitfdset;
363a1fc6 1100
564e1029 1101 FD_ZERO(&waitfdset);
1e613922 1102
564e1029
AG
1103 for (i = 0; i < async_jobs && num_inprogress > 0; i++) {
1104 if (loopargs[i].inprogress_job == NULL)
1105 continue;
1e613922 1106
29dd15b1
NT
1107 if (!ASYNC_WAIT_CTX_get_all_fds
1108 (loopargs[i].wait_ctx, NULL, &num_job_fds)
1109 || num_job_fds > 1) {
564e1029
AG
1110 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1111 ERR_print_errors(bio_err);
1112 error = 1;
1113 break;
8b0b80d9 1114 }
29dd15b1
NT
1115 ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
1116 &num_job_fds);
564e1029
AG
1117 FD_SET(job_fd, &waitfdset);
1118 if (job_fd > max_fd)
1119 max_fd = job_fd;
8b0b80d9 1120 }
8b0b80d9 1121
402ec2f5 1122 if (max_fd >= (OSSL_ASYNC_FD)FD_SETSIZE) {
570c0716 1123 BIO_printf(bio_err,
29dd15b1
NT
1124 "Error: max_fd (%d) must be smaller than FD_SETSIZE (%d). "
1125 "Decrease the value of async_jobs\n",
1126 max_fd, FD_SETSIZE);
570c0716
AG
1127 ERR_print_errors(bio_err);
1128 error = 1;
1129 break;
1130 }
1131
564e1029 1132 select_result = select(max_fd + 1, &waitfdset, NULL, NULL, NULL);
8b0b80d9
AG
1133 if (select_result == -1 && errno == EINTR)
1134 continue;
1135
1136 if (select_result == -1) {
564e1029
AG
1137 BIO_printf(bio_err, "Failure in the select\n");
1138 ERR_print_errors(bio_err);
1139 error = 1;
1140 break;
8b0b80d9
AG
1141 }
1142
1143 if (select_result == 0)
1144 continue;
8b0b80d9
AG
1145#endif
1146
1147 for (i = 0; i < async_jobs; i++) {
1148 if (loopargs[i].inprogress_job == NULL)
1149 continue;
1150
29dd15b1
NT
1151 if (!ASYNC_WAIT_CTX_get_all_fds
1152 (loopargs[i].wait_ctx, NULL, &num_job_fds)
1153 || num_job_fds > 1) {
1e613922
AG
1154 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1155 ERR_print_errors(bio_err);
1156 error = 1;
1157 break;
1158 }
29dd15b1
NT
1159 ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
1160 &num_job_fds);
8b0b80d9 1161
667867cc 1162#if defined(OPENSSL_SYS_UNIX)
1e613922 1163 if (num_job_fds == 1 && !FD_ISSET(job_fd, &waitfdset))
8b0b80d9 1164 continue;
667867cc 1165#elif defined(OPENSSL_SYS_WINDOWS)
fd4b0c08 1166 if (num_job_fds == 1
700b8145 1167 && !PeekNamedPipe(job_fd, NULL, 0, NULL, &avail, NULL)
fd4b0c08 1168 && avail > 0)
8b0b80d9
AG
1169 continue;
1170#endif
1171
609b0852 1172 ret = ASYNC_start_job(&loopargs[i].inprogress_job,
29dd15b1
NT
1173 loopargs[i].wait_ctx, &job_op_count,
1174 loop_function, (void *)(loopargs + i),
1175 sizeof(loopargs_t));
700b8145 1176 switch (ret) {
fd4b0c08
F
1177 case ASYNC_PAUSE:
1178 break;
1179 case ASYNC_FINISH:
1180 if (job_op_count == -1) {
8b0b80d9 1181 error = 1;
fd4b0c08
F
1182 } else {
1183 total_op_count += job_op_count;
1184 }
1185 --num_inprogress;
1186 loopargs[i].inprogress_job = NULL;
1187 break;
1188 case ASYNC_NO_JOBS:
1189 case ASYNC_ERR:
1190 --num_inprogress;
1191 loopargs[i].inprogress_job = NULL;
1192 BIO_printf(bio_err, "Failure in the job\n");
1193 ERR_print_errors(bio_err);
1194 error = 1;
1195 break;
8b0b80d9
AG
1196 }
1197 }
1198 }
1199
1200 return error ? -1 : total_op_count;
1201}
1202
1203int speed_main(int argc, char **argv)
1204{
dd1abd44 1205 ENGINE *e = NULL;
8b0b80d9 1206 loopargs_t *loopargs = NULL;
dab1f5fe 1207 int async_init = 0;
8b0b80d9
AG
1208 int loopargs_len = 0;
1209 char *prog;
19075d58 1210 const char *engine_id = NULL;
8b0b80d9
AG
1211 const EVP_CIPHER *evp_cipher = NULL;
1212 double d = 0.0;
1213 OPTION_CHOICE o;
4d82c58b
F
1214 int multiblock = 0, pr_header = 0;
1215 int doit[ALGOR_NUM] = { 0 };
8b0b80d9 1216 int ret = 1, i, k, misalign = 0;
19075d58 1217 long count = 0;
8b0b80d9
AG
1218#ifndef NO_FORK
1219 int multi = 0;
1220#endif
1221 int async_jobs = 0;
5f986ed3
F
1222#if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA) \
1223 || !defined(OPENSSL_NO_EC)
0ff43435 1224 long rsa_count = 1;
a00ae6c4 1225#endif
5f986ed3
F
1226
1227 /* What follows are the buffers and key material. */
a00ae6c4 1228#ifndef OPENSSL_NO_RC5
0f113f3e 1229 RC5_32_KEY rc5_ks;
a00ae6c4
RS
1230#endif
1231#ifndef OPENSSL_NO_RC2
0f113f3e 1232 RC2_KEY rc2_ks;
a00ae6c4
RS
1233#endif
1234#ifndef OPENSSL_NO_IDEA
0f113f3e 1235 IDEA_KEY_SCHEDULE idea_ks;
a00ae6c4
RS
1236#endif
1237#ifndef OPENSSL_NO_SEED
0f113f3e 1238 SEED_KEY_SCHEDULE seed_ks;
a00ae6c4
RS
1239#endif
1240#ifndef OPENSSL_NO_BF
0f113f3e 1241 BF_KEY bf_ks;
a00ae6c4
RS
1242#endif
1243#ifndef OPENSSL_NO_CAST
0f113f3e 1244 CAST_KEY cast_ks;
a00ae6c4 1245#endif
0f113f3e
MC
1246 static const unsigned char key16[16] = {
1247 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1248 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
1249 };
0f113f3e
MC
1250 static const unsigned char key24[24] = {
1251 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1252 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1253 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1254 };
1255 static const unsigned char key32[32] = {
1256 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1257 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1258 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1259 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1260 };
a00ae6c4 1261#ifndef OPENSSL_NO_CAMELLIA
0f113f3e
MC
1262 static const unsigned char ckey24[24] = {
1263 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1264 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1265 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1266 };
1267 static const unsigned char ckey32[32] = {
1268 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1269 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1270 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1271 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1272 };
7e1b7485 1273 CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;
a00ae6c4 1274#endif
a00ae6c4 1275#ifndef OPENSSL_NO_DES
7e1b7485
RS
1276 static DES_cblock key = {
1277 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0
1278 };
1279 static DES_cblock key2 = {
1280 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
1281 };
1282 static DES_cblock key3 = {
1283 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1284 };
a00ae6c4 1285#endif
a00ae6c4 1286#ifndef OPENSSL_NO_RSA
4d82c58b 1287 static const unsigned int rsa_bits[RSA_NUM] = {
0f113f3e
MC
1288 512, 1024, 2048, 3072, 4096, 7680, 15360
1289 };
4d82c58b 1290 static const unsigned char *rsa_data[RSA_NUM] = {
0f113f3e
MC
1291 test512, test1024, test2048, test3072, test4096, test7680, test15360
1292 };
4d82c58b 1293 static const int rsa_data_length[RSA_NUM] = {
0f113f3e
MC
1294 sizeof(test512), sizeof(test1024),
1295 sizeof(test2048), sizeof(test3072),
1296 sizeof(test4096), sizeof(test7680),
1297 sizeof(test15360)
1298 };
5f986ed3 1299 int rsa_doit[RSA_NUM] = { 0 };
a00ae6c4
RS
1300#endif
1301#ifndef OPENSSL_NO_DSA
4d82c58b 1302 static const unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
5f986ed3 1303 int dsa_doit[DSA_NUM] = { 0 };
a00ae6c4
RS
1304#endif
1305#ifndef OPENSSL_NO_EC
0f113f3e
MC
1306 /*
1307 * We only test over the following curves as they are representative, To
1308 * add tests over more curves, simply add the curve NID and curve name to
1309 * the following arrays and increase the EC_NUM value accordingly.
1310 */
4d82c58b 1311 static const unsigned int test_curves[EC_NUM] = {
0f113f3e 1312 /* Prime Curves */
7e1b7485
RS
1313 NID_secp160r1, NID_X9_62_prime192v1, NID_secp224r1,
1314 NID_X9_62_prime256v1, NID_secp384r1, NID_secp521r1,
0f113f3e 1315 /* Binary Curves */
7e1b7485
RS
1316 NID_sect163k1, NID_sect233k1, NID_sect283k1,
1317 NID_sect409k1, NID_sect571k1, NID_sect163r2,
1318 NID_sect233r1, NID_sect283r1, NID_sect409r1,
db50c1da
DSH
1319 NID_sect571r1,
1320 /* Other */
1321 NID_X25519
0f113f3e
MC
1322 };
1323 static const char *test_curves_names[EC_NUM] = {
1324 /* Prime Curves */
7e1b7485
RS
1325 "secp160r1", "nistp192", "nistp224",
1326 "nistp256", "nistp384", "nistp521",
0f113f3e 1327 /* Binary Curves */
7e1b7485
RS
1328 "nistk163", "nistk233", "nistk283",
1329 "nistk409", "nistk571", "nistb163",
1330 "nistb233", "nistb283", "nistb409",
db50c1da
DSH
1331 "nistb571",
1332 /* Other */
1333 "X25519"
0f113f3e 1334 };
4d82c58b 1335 static const int test_curves_bits[EC_NUM] = {
7e1b7485
RS
1336 160, 192, 224,
1337 256, 384, 521,
1338 163, 233, 283,
1339 409, 571, 163,
1340 233, 283, 409,
29dd15b1 1341 571, 253 /* X25519 */
0f113f3e 1342 };
d02b48c6 1343
4d82c58b
F
1344 int ecdsa_doit[EC_NUM] = { 0 };
1345 int ecdh_doit[EC_NUM] = { 0 };
d6073e27 1346#endif /* ndef OPENSSL_NO_EC */
7e1b7485
RS
1347
1348 prog = opt_init(argc, argv, speed_options);
1349 while ((o = opt_next()) != OPT_EOF) {
1350 switch (o) {
1351 case OPT_EOF:
1352 case OPT_ERR:
1353 opterr:
1354 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
1355 goto end;
1356 case OPT_HELP:
1357 opt_help(speed_options);
1358 ret = 0;
1359 goto end;
1360 case OPT_ELAPSED:
0f113f3e 1361 usertime = 0;
7e1b7485
RS
1362 break;
1363 case OPT_EVP:
1364 evp_cipher = EVP_get_cipherbyname(opt_arg());
1365 if (evp_cipher == NULL)
1366 evp_md = EVP_get_digestbyname(opt_arg());
1367 if (evp_cipher == NULL && evp_md == NULL) {
1368 BIO_printf(bio_err,
55b09fe6 1369 "%s: %s is an unknown cipher or digest\n",
7e1b7485 1370 prog, opt_arg());
0f113f3e
MC
1371 goto end;
1372 }
1373 doit[D_EVP] = 1;
7e1b7485
RS
1374 break;
1375 case OPT_DECRYPT:
0f113f3e 1376 decrypt = 1;
7e1b7485 1377 break;
7e1b7485 1378 case OPT_ENGINE:
8b0b80d9
AG
1379 /*
1380 * In a forked execution, an engine might need to be
1381 * initialised by each child process, not by the parent.
1382 * So store the name here and run setup_engine() later on.
1383 */
1384 engine_id = opt_arg();
7e1b7485 1385 break;
7e1b7485 1386 case OPT_MULTI:
9c3bcfa0 1387#ifndef NO_FORK
7e1b7485 1388 multi = atoi(opt_arg());
8b0b80d9
AG
1389#endif
1390 break;
1391 case OPT_ASYNCJOBS:
667867cc 1392#ifndef OPENSSL_NO_ASYNC
8b0b80d9 1393 async_jobs = atoi(opt_arg());
667867cc
MC
1394 if (!ASYNC_is_capable()) {
1395 BIO_printf(bio_err,
1396 "%s: async_jobs specified but async not supported\n",
1397 prog);
1398 goto opterr;
1399 }
a00ae6c4 1400#endif
9c3bcfa0 1401 break;
7e1b7485
RS
1402 case OPT_MISALIGN:
1403 if (!opt_int(opt_arg(), &misalign))
0f113f3e 1404 goto end;
7e1b7485 1405 if (misalign > MISALIGN) {
0f113f3e 1406 BIO_printf(bio_err,
7e1b7485
RS
1407 "%s: Maximum offset is %d\n", prog, MISALIGN);
1408 goto opterr;
0f113f3e 1409 }
7e1b7485
RS
1410 break;
1411 case OPT_MR:
1412 mr = 1;
1413 break;
1414 case OPT_MB:
1415 multiblock = 1;
cfd451d4
F
1416#ifdef OPENSSL_NO_MULTIBLOCK
1417 BIO_printf(bio_err,
1418 "%s: -mb specified but multi-block support is disabled\n",
1419 prog);
1420 goto end;
1421#endif
7e1b7485
RS
1422 break;
1423 }
1424 }
1425 argc = opt_num_rest();
1426 argv = opt_rest();
1427
1428 /* Remaining arguments are algorithms. */
29dd15b1 1429 for (; *argv; argv++) {
7e1b7485
RS
1430 if (found(*argv, doit_choices, &i)) {
1431 doit[i] = 1;
1432 continue;
1433 }
a00ae6c4 1434#ifndef OPENSSL_NO_DES
7e1b7485
RS
1435 if (strcmp(*argv, "des") == 0) {
1436 doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
1437 continue;
1438 }
a00ae6c4 1439#endif
7e1b7485
RS
1440 if (strcmp(*argv, "sha") == 0) {
1441 doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
1442 continue;
1443 }
a00ae6c4 1444#ifndef OPENSSL_NO_RSA
a00ae6c4 1445# ifndef RSA_NULL
0f113f3e 1446 if (strcmp(*argv, "openssl") == 0) {
b0700d2c 1447 RSA_set_default_method(RSA_PKCS1_OpenSSL());
7e1b7485
RS
1448 continue;
1449 }
a00ae6c4 1450# endif
7e1b7485
RS
1451 if (strcmp(*argv, "rsa") == 0) {
1452 rsa_doit[R_RSA_512] = rsa_doit[R_RSA_1024] =
1453 rsa_doit[R_RSA_2048] = rsa_doit[R_RSA_3072] =
1454 rsa_doit[R_RSA_4096] = rsa_doit[R_RSA_7680] =
1455 rsa_doit[R_RSA_15360] = 1;
1456 continue;
1457 }
1458 if (found(*argv, rsa_choices, &i)) {
1459 rsa_doit[i] = 1;
1460 continue;
1461 }
a00ae6c4 1462#endif
7e1b7485
RS
1463#ifndef OPENSSL_NO_DSA
1464 if (strcmp(*argv, "dsa") == 0) {
1465 dsa_doit[R_DSA_512] = dsa_doit[R_DSA_1024] =
1466 dsa_doit[R_DSA_2048] = 1;
1467 continue;
1468 }
1469 if (found(*argv, dsa_choices, &i)) {
1470 dsa_doit[i] = 2;
1471 continue;
1472 }
a00ae6c4 1473#endif
0f113f3e 1474 if (strcmp(*argv, "aes") == 0) {
29dd15b1 1475 doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;
7e1b7485
RS
1476 continue;
1477 }
a00ae6c4 1478#ifndef OPENSSL_NO_CAMELLIA
0f113f3e 1479 if (strcmp(*argv, "camellia") == 0) {
29dd15b1 1480 doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = doit[D_CBC_256_CML] = 1;
7e1b7485
RS
1481 continue;
1482 }
a00ae6c4 1483#endif
10bf4fc2 1484#ifndef OPENSSL_NO_EC
7e1b7485 1485 if (strcmp(*argv, "ecdsa") == 0) {
0f113f3e
MC
1486 for (i = 0; i < EC_NUM; i++)
1487 ecdsa_doit[i] = 1;
7e1b7485
RS
1488 continue;
1489 }
1490 if (found(*argv, ecdsa_choices, &i)) {
1491 ecdsa_doit[i] = 2;
1492 continue;
1493 }
1494 if (strcmp(*argv, "ecdh") == 0) {
0f113f3e
MC
1495 for (i = 0; i < EC_NUM; i++)
1496 ecdh_doit[i] = 1;
7e1b7485
RS
1497 continue;
1498 }
1499 if (found(*argv, ecdh_choices, &i)) {
1500 ecdh_doit[i] = 2;
1501 continue;
0f113f3e 1502 }
7e1b7485
RS
1503#endif
1504 BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, *argv);
1505 goto end;
0f113f3e 1506 }
d02b48c6 1507
8b0b80d9
AG
1508 /* Initialize the job pool if async mode is enabled */
1509 if (async_jobs > 0) {
dab1f5fe
CS
1510 async_init = ASYNC_init_thread(async_jobs, async_jobs);
1511 if (!async_init) {
8b0b80d9
AG
1512 BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
1513 goto end;
1514 }
1515 }
1516
1517 loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
29dd15b1
NT
1518 loopargs =
1519 app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
8b0b80d9
AG
1520 memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));
1521
0ff43435 1522 for (i = 0; i < loopargs_len; i++) {
1e613922
AG
1523 if (async_jobs > 0) {
1524 loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
1525 if (loopargs[i].wait_ctx == NULL) {
1526 BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
1527 goto end;
1528 }
1529 }
1530
29dd15b1
NT
1531 loopargs[i].buf_malloc =
1532 app_malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1, "input buffer");
1533 loopargs[i].buf2_malloc =
1534 app_malloc((int)BUFSIZE + MAX_MISALIGNMENT + 1, "input buffer");
8b0b80d9
AG
1535 /* Align the start of buffers on a 64 byte boundary */
1536 loopargs[i].buf = loopargs[i].buf_malloc + misalign;
1537 loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
0ff43435 1538#ifndef OPENSSL_NO_EC
0ff43435
AG
1539 loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
1540 loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
1541#endif
8b0b80d9
AG
1542 }
1543
a00ae6c4 1544#ifndef NO_FORK
0f113f3e
MC
1545 if (multi && do_multi(multi))
1546 goto show_res;
a00ae6c4 1547#endif
d02b48c6 1548
8b0b80d9 1549 /* Initialize the engine after the fork */
dd1abd44 1550 e = setup_engine(engine_id, 0);
8b0b80d9 1551
7e1b7485 1552 /* No parameters; turn on everything. */
dfba17b4 1553 if ((argc == 0) && !doit[D_EVP]) {
7e1b7485 1554 for (i = 0; i < ALGOR_NUM; i++)
0f113f3e
MC
1555 if (i != D_EVP)
1556 doit[i] = 1;
d6073e27 1557#ifndef OPENSSL_NO_RSA
0f113f3e
MC
1558 for (i = 0; i < RSA_NUM; i++)
1559 rsa_doit[i] = 1;
d6073e27 1560#endif
83ae8124 1561#ifndef OPENSSL_NO_DSA
0f113f3e
MC
1562 for (i = 0; i < DSA_NUM; i++)
1563 dsa_doit[i] = 1;
83ae8124 1564#endif
10bf4fc2 1565#ifndef OPENSSL_NO_EC
0f113f3e
MC
1566 for (i = 0; i < EC_NUM; i++)
1567 ecdsa_doit[i] = 1;
0f113f3e
MC
1568 for (i = 0; i < EC_NUM; i++)
1569 ecdh_doit[i] = 1;
a00ae6c4 1570#endif
0f113f3e
MC
1571 }
1572 for (i = 0; i < ALGOR_NUM; i++)
1573 if (doit[i])
1574 pr_header++;
1575
1576 if (usertime == 0 && !mr)
1577 BIO_printf(bio_err,
1578 "You have chosen to measure elapsed time "
1579 "instead of user CPU time.\n");
1580
a00ae6c4 1581#ifndef OPENSSL_NO_RSA
0ff43435
AG
1582 for (i = 0; i < loopargs_len; i++) {
1583 for (k = 0; k < RSA_NUM; k++) {
1584 const unsigned char *p;
1585
1586 p = rsa_data[k];
29dd15b1
NT
1587 loopargs[i].rsa_key[k] =
1588 d2i_RSAPrivateKey(NULL, &p, rsa_data_length[k]);
0ff43435 1589 if (loopargs[i].rsa_key[k] == NULL) {
29dd15b1
NT
1590 BIO_printf(bio_err,
1591 "internal error loading RSA key number %d\n", k);
0ff43435
AG
1592 goto end;
1593 }
0f113f3e 1594 }
a00ae6c4
RS
1595 }
1596#endif
a00ae6c4 1597#ifndef OPENSSL_NO_DSA
0ff43435
AG
1598 for (i = 0; i < loopargs_len; i++) {
1599 loopargs[i].dsa_key[0] = get_dsa512();
1600 loopargs[i].dsa_key[1] = get_dsa1024();
1601 loopargs[i].dsa_key[2] = get_dsa2048();
1602 }
a00ae6c4 1603#endif
a00ae6c4 1604#ifndef OPENSSL_NO_DES
0f113f3e
MC
1605 DES_set_key_unchecked(&key, &sch);
1606 DES_set_key_unchecked(&key2, &sch2);
1607 DES_set_key_unchecked(&key3, &sch3);
a00ae6c4 1608#endif
0f113f3e
MC
1609 AES_set_encrypt_key(key16, 128, &aes_ks1);
1610 AES_set_encrypt_key(key24, 192, &aes_ks2);
1611 AES_set_encrypt_key(key32, 256, &aes_ks3);
a00ae6c4 1612#ifndef OPENSSL_NO_CAMELLIA
0f113f3e
MC
1613 Camellia_set_key(key16, 128, &camellia_ks1);
1614 Camellia_set_key(ckey24, 192, &camellia_ks2);
1615 Camellia_set_key(ckey32, 256, &camellia_ks3);
a00ae6c4
RS
1616#endif
1617#ifndef OPENSSL_NO_IDEA
9021a5df 1618 IDEA_set_encrypt_key(key16, &idea_ks);
a00ae6c4
RS
1619#endif
1620#ifndef OPENSSL_NO_SEED
0f113f3e 1621 SEED_set_key(key16, &seed_ks);
a00ae6c4
RS
1622#endif
1623#ifndef OPENSSL_NO_RC4
0f113f3e 1624 RC4_set_key(&rc4_ks, 16, key16);
a00ae6c4
RS
1625#endif
1626#ifndef OPENSSL_NO_RC2
0f113f3e 1627 RC2_set_key(&rc2_ks, 16, key16, 128);
a00ae6c4
RS
1628#endif
1629#ifndef OPENSSL_NO_RC5
0f113f3e 1630 RC5_32_set_key(&rc5_ks, 16, key16, 12);
a00ae6c4
RS
1631#endif
1632#ifndef OPENSSL_NO_BF
0f113f3e 1633 BF_set_key(&bf_ks, 16, key16);
a00ae6c4
RS
1634#endif
1635#ifndef OPENSSL_NO_CAST
0f113f3e 1636 CAST_set_key(&cast_ks, 16, key16);
a00ae6c4 1637#endif
a00ae6c4
RS
1638#ifndef SIGALRM
1639# ifndef OPENSSL_NO_DES
0f113f3e
MC
1640 BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
1641 count = 10;
1642 do {
1643 long it;
1644 count *= 2;
1645 Time_F(START);
1646 for (it = count; it; it--)
8b0b80d9
AG
1647 DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
1648 (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
0f113f3e
MC
1649 d = Time_F(STOP);
1650 } while (d < 3);
1651 save_count = count;
1652 c[D_MD2][0] = count / 10;
1653 c[D_MDC2][0] = count / 10;
1654 c[D_MD4][0] = count;
1655 c[D_MD5][0] = count;
1656 c[D_HMAC][0] = count;
1657 c[D_SHA1][0] = count;
1658 c[D_RMD160][0] = count;
1659 c[D_RC4][0] = count * 5;
1660 c[D_CBC_DES][0] = count;
1661 c[D_EDE3_DES][0] = count / 3;
1662 c[D_CBC_IDEA][0] = count;
1663 c[D_CBC_SEED][0] = count;
1664 c[D_CBC_RC2][0] = count;
1665 c[D_CBC_RC5][0] = count;
1666 c[D_CBC_BF][0] = count;
1667 c[D_CBC_CAST][0] = count;
1668 c[D_CBC_128_AES][0] = count;
1669 c[D_CBC_192_AES][0] = count;
1670 c[D_CBC_256_AES][0] = count;
1671 c[D_CBC_128_CML][0] = count;
1672 c[D_CBC_192_CML][0] = count;
1673 c[D_CBC_256_CML][0] = count;
1674 c[D_SHA256][0] = count;
1675 c[D_SHA512][0] = count;
1676 c[D_WHIRLPOOL][0] = count;
1677 c[D_IGE_128_AES][0] = count;
1678 c[D_IGE_192_AES][0] = count;
1679 c[D_IGE_256_AES][0] = count;
1680 c[D_GHASH][0] = count;
1681
1682 for (i = 1; i < SIZE_NUM; i++) {
1683 long l0, l1;
1684
1685 l0 = (long)lengths[0];
1686 l1 = (long)lengths[i];
1687
1688 c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
1689 c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
1690 c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
1691 c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
1692 c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
1693 c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
1694 c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
1695 c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
1696 c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
1697 c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
6d9843e7 1698 c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
0f113f3e
MC
1699
1700 l0 = (long)lengths[i - 1];
1701
1702 c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
1703 c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
1704 c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
1705 c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
1706 c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
1707 c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
1708 c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
1709 c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
1710 c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
1711 c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
1712 c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
1713 c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
1714 c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
1715 c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
1716 c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
1717 c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
1718 c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
1719 c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
1720 }
e172d60d 1721
a00ae6c4 1722# ifndef OPENSSL_NO_RSA
0f113f3e
MC
1723 rsa_c[R_RSA_512][0] = count / 2000;
1724 rsa_c[R_RSA_512][1] = count / 400;
1725 for (i = 1; i < RSA_NUM; i++) {
1726 rsa_c[i][0] = rsa_c[i - 1][0] / 8;
1727 rsa_c[i][1] = rsa_c[i - 1][1] / 4;
70c4e156 1728 if (rsa_doit[i] <= 1 && rsa_c[i][0] == 0)
0f113f3e
MC
1729 rsa_doit[i] = 0;
1730 else {
1731 if (rsa_c[i][0] == 0) {
29dd15b1 1732 rsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
0f113f3e
MC
1733 rsa_c[i][1] = 20;
1734 }
1735 }
1736 }
a00ae6c4 1737# endif
0f113f3e 1738
a00ae6c4 1739# ifndef OPENSSL_NO_DSA
0f113f3e
MC
1740 dsa_c[R_DSA_512][0] = count / 1000;
1741 dsa_c[R_DSA_512][1] = count / 1000 / 2;
1742 for (i = 1; i < DSA_NUM; i++) {
1743 dsa_c[i][0] = dsa_c[i - 1][0] / 4;
1744 dsa_c[i][1] = dsa_c[i - 1][1] / 4;
70c4e156 1745 if (dsa_doit[i] <= 1 && dsa_c[i][0] == 0)
0f113f3e
MC
1746 dsa_doit[i] = 0;
1747 else {
70c4e156 1748 if (dsa_c[i][0] == 0) {
29dd15b1 1749 dsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
0f113f3e
MC
1750 dsa_c[i][1] = 1;
1751 }
1752 }
1753 }
a00ae6c4 1754# endif
0f113f3e 1755
10bf4fc2 1756# ifndef OPENSSL_NO_EC
0f113f3e
MC
1757 ecdsa_c[R_EC_P160][0] = count / 1000;
1758 ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
1759 for (i = R_EC_P192; i <= R_EC_P521; i++) {
1760 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1761 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
70c4e156 1762 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
0f113f3e
MC
1763 ecdsa_doit[i] = 0;
1764 else {
70c4e156 1765 if (ecdsa_c[i][0] == 0) {
0f113f3e
MC
1766 ecdsa_c[i][0] = 1;
1767 ecdsa_c[i][1] = 1;
1768 }
1769 }
1770 }
1771 ecdsa_c[R_EC_K163][0] = count / 1000;
1772 ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
1773 for (i = R_EC_K233; i <= R_EC_K571; i++) {
1774 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1775 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
70c4e156 1776 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
0f113f3e
MC
1777 ecdsa_doit[i] = 0;
1778 else {
70c4e156 1779 if (ecdsa_c[i][0] == 0) {
0f113f3e
MC
1780 ecdsa_c[i][0] = 1;
1781 ecdsa_c[i][1] = 1;
1782 }
1783 }
1784 }
1785 ecdsa_c[R_EC_B163][0] = count / 1000;
1786 ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
1787 for (i = R_EC_B233; i <= R_EC_B571; i++) {
1788 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
1789 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
70c4e156 1790 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
0f113f3e
MC
1791 ecdsa_doit[i] = 0;
1792 else {
70c4e156 1793 if (ecdsa_c[i][0] == 0) {
0f113f3e
MC
1794 ecdsa_c[i][0] = 1;
1795 ecdsa_c[i][1] = 1;
1796 }
1797 }
1798 }
7e1b7485 1799
0f113f3e 1800 ecdh_c[R_EC_P160][0] = count / 1000;
0f113f3e
MC
1801 for (i = R_EC_P192; i <= R_EC_P521; i++) {
1802 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
70c4e156 1803 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
0f113f3e
MC
1804 ecdh_doit[i] = 0;
1805 else {
70c4e156 1806 if (ecdh_c[i][0] == 0) {
0f113f3e 1807 ecdh_c[i][0] = 1;
0f113f3e
MC
1808 }
1809 }
1810 }
1811 ecdh_c[R_EC_K163][0] = count / 1000;
0f113f3e
MC
1812 for (i = R_EC_K233; i <= R_EC_K571; i++) {
1813 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
70c4e156 1814 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
0f113f3e
MC
1815 ecdh_doit[i] = 0;
1816 else {
70c4e156 1817 if (ecdh_c[i][0] == 0) {
0f113f3e 1818 ecdh_c[i][0] = 1;
0f113f3e
MC
1819 }
1820 }
1821 }
1822 ecdh_c[R_EC_B163][0] = count / 1000;
0f113f3e
MC
1823 for (i = R_EC_B233; i <= R_EC_B571; i++) {
1824 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
70c4e156 1825 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
0f113f3e
MC
1826 ecdh_doit[i] = 0;
1827 else {
70c4e156 1828 if (ecdh_c[i][0] == 0) {
0f113f3e 1829 ecdh_c[i][0] = 1;
0f113f3e
MC
1830 }
1831 }
1832 }
a00ae6c4 1833# endif
e172d60d 1834
0f113f3e 1835# else
a00ae6c4
RS
1836/* not worth fixing */
1837# error "You cannot disable DES on systems without SIGALRM."
29dd15b1 1838# endif /* OPENSSL_NO_DES */
a00ae6c4 1839#else
a00ae6c4 1840# ifndef _WIN32
0f113f3e 1841 signal(SIGALRM, sig_done);
a00ae6c4 1842# endif
29dd15b1 1843#endif /* SIGALRM */
0f113f3e 1844
a00ae6c4 1845#ifndef OPENSSL_NO_MD2
0f113f3e 1846 if (doit[D_MD2]) {
8b0b80d9
AG
1847 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1848 print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum]);
0f113f3e 1849 Time_F(START);
8b0b80d9 1850 count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
0f113f3e 1851 d = Time_F(STOP);
8b0b80d9 1852 print_result(D_MD2, testnum, count, d);
0f113f3e
MC
1853 }
1854 }
a00ae6c4
RS
1855#endif
1856#ifndef OPENSSL_NO_MDC2
0f113f3e 1857 if (doit[D_MDC2]) {
8b0b80d9
AG
1858 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1859 print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum]);
0f113f3e 1860 Time_F(START);
8b0b80d9 1861 count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
0f113f3e 1862 d = Time_F(STOP);
8b0b80d9 1863 print_result(D_MDC2, testnum, count, d);
0f113f3e
MC
1864 }
1865 }
a00ae6c4 1866#endif
d02b48c6 1867
a00ae6c4 1868#ifndef OPENSSL_NO_MD4
0f113f3e 1869 if (doit[D_MD4]) {
8b0b80d9
AG
1870 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1871 print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum]);
0f113f3e 1872 Time_F(START);
8b0b80d9 1873 count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
0f113f3e 1874 d = Time_F(STOP);
8b0b80d9 1875 print_result(D_MD4, testnum, count, d);
0f113f3e
MC
1876 }
1877 }
a00ae6c4 1878#endif
3009458e 1879
a00ae6c4 1880#ifndef OPENSSL_NO_MD5
0f113f3e 1881 if (doit[D_MD5]) {
8b0b80d9
AG
1882 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1883 print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum]);
0f113f3e 1884 Time_F(START);
8b0b80d9 1885 count = run_benchmark(async_jobs, MD5_loop, loopargs);
0f113f3e 1886 d = Time_F(STOP);
8b0b80d9 1887 print_result(D_MD5, testnum, count, d);
0f113f3e
MC
1888 }
1889 }
d02b48c6 1890
0f113f3e 1891 if (doit[D_HMAC]) {
d6073e27 1892 static const char hmac_key[] = "This is a key...";
8829ce30
F
1893 int len = strlen(hmac_key);
1894
0ff43435 1895 for (i = 0; i < loopargs_len; i++) {
8b0b80d9
AG
1896 loopargs[i].hctx = HMAC_CTX_new();
1897 if (loopargs[i].hctx == NULL) {
1898 BIO_printf(bio_err, "HMAC malloc failure, exiting...");
1899 exit(1);
1900 }
0f113f3e 1901
8829ce30 1902 HMAC_Init_ex(loopargs[i].hctx, hmac_key, len, EVP_md5(), NULL);
bf7c6817 1903 }
8b0b80d9
AG
1904 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1905 print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum]);
0f113f3e 1906 Time_F(START);
8b0b80d9 1907 count = run_benchmark(async_jobs, HMAC_loop, loopargs);
0f113f3e 1908 d = Time_F(STOP);
8b0b80d9
AG
1909 print_result(D_HMAC, testnum, count, d);
1910 }
0ff43435 1911 for (i = 0; i < loopargs_len; i++) {
8b0b80d9 1912 HMAC_CTX_free(loopargs[i].hctx);
0f113f3e 1913 }
0f113f3e 1914 }
a00ae6c4 1915#endif
0f113f3e 1916 if (doit[D_SHA1]) {
8b0b80d9
AG
1917 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1918 print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum]);
0f113f3e 1919 Time_F(START);
8b0b80d9 1920 count = run_benchmark(async_jobs, SHA1_loop, loopargs);
0f113f3e 1921 d = Time_F(STOP);
8b0b80d9 1922 print_result(D_SHA1, testnum, count, d);
0f113f3e
MC
1923 }
1924 }
0f113f3e 1925 if (doit[D_SHA256]) {
8b0b80d9 1926 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
29dd15b1
NT
1927 print_message(names[D_SHA256], c[D_SHA256][testnum],
1928 lengths[testnum]);
0f113f3e 1929 Time_F(START);
8b0b80d9 1930 count = run_benchmark(async_jobs, SHA256_loop, loopargs);
0f113f3e 1931 d = Time_F(STOP);
8b0b80d9 1932 print_result(D_SHA256, testnum, count, d);
0f113f3e
MC
1933 }
1934 }
0f113f3e 1935 if (doit[D_SHA512]) {
8b0b80d9 1936 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
29dd15b1
NT
1937 print_message(names[D_SHA512], c[D_SHA512][testnum],
1938 lengths[testnum]);
0f113f3e 1939 Time_F(START);
8b0b80d9 1940 count = run_benchmark(async_jobs, SHA512_loop, loopargs);
0f113f3e 1941 d = Time_F(STOP);
8b0b80d9 1942 print_result(D_SHA512, testnum, count, d);
0f113f3e
MC
1943 }
1944 }
a00ae6c4 1945#ifndef OPENSSL_NO_WHIRLPOOL
0f113f3e 1946 if (doit[D_WHIRLPOOL]) {
8b0b80d9 1947 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
29dd15b1
NT
1948 print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum],
1949 lengths[testnum]);
0f113f3e 1950 Time_F(START);
8b0b80d9 1951 count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
0f113f3e 1952 d = Time_F(STOP);
8b0b80d9 1953 print_result(D_WHIRLPOOL, testnum, count, d);
0f113f3e
MC
1954 }
1955 }
a00ae6c4 1956#endif
c88f8f76 1957
a00ae6c4 1958#ifndef OPENSSL_NO_RMD160
0f113f3e 1959 if (doit[D_RMD160]) {
8b0b80d9 1960 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
29dd15b1
NT
1961 print_message(names[D_RMD160], c[D_RMD160][testnum],
1962 lengths[testnum]);
0f113f3e 1963 Time_F(START);
8b0b80d9 1964 count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
0f113f3e 1965 d = Time_F(STOP);
8b0b80d9 1966 print_result(D_RMD160, testnum, count, d);
0f113f3e
MC
1967 }
1968 }
a00ae6c4
RS
1969#endif
1970#ifndef OPENSSL_NO_RC4
0f113f3e 1971 if (doit[D_RC4]) {
8b0b80d9
AG
1972 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
1973 print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum]);
0f113f3e 1974 Time_F(START);
8b0b80d9 1975 count = run_benchmark(async_jobs, RC4_loop, loopargs);
0f113f3e 1976 d = Time_F(STOP);
8b0b80d9 1977 print_result(D_RC4, testnum, count, d);
0f113f3e
MC
1978 }
1979 }
a00ae6c4
RS
1980#endif
1981#ifndef OPENSSL_NO_DES
0f113f3e 1982 if (doit[D_CBC_DES]) {
8b0b80d9 1983 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
29dd15b1
NT
1984 print_message(names[D_CBC_DES], c[D_CBC_DES][testnum],
1985 lengths[testnum]);
0f113f3e 1986 Time_F(START);
8b0b80d9 1987 count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
0f113f3e 1988 d = Time_F(STOP);
8b0b80d9 1989 print_result(D_CBC_DES, testnum, count, d);
0f113f3e
MC
1990 }
1991 }
ae93dc13 1992
0f113f3e 1993 if (doit[D_EDE3_DES]) {
8b0b80d9 1994 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
29dd15b1
NT
1995 print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum],
1996 lengths[testnum]);
0f113f3e 1997 Time_F(START);
29dd15b1
NT
1998 count =
1999 run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
0f113f3e 2000 d = Time_F(STOP);
8b0b80d9 2001 print_result(D_EDE3_DES, testnum, count, d);
0f113f3e
MC
2002 }
2003 }
a00ae6c4 2004#endif
5158c763 2005
0f113f3e 2006 if (doit[D_CBC_128_AES]) {
8b0b80d9
AG
2007 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2008 print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
2009 lengths[testnum]);
0f113f3e 2010 Time_F(START);
29dd15b1
NT
2011 count =
2012 run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
0f113f3e 2013 d = Time_F(STOP);
8b0b80d9 2014 print_result(D_CBC_128_AES, testnum, count, d);
0f113f3e
MC
2015 }
2016 }
2017 if (doit[D_CBC_192_AES]) {
8b0b80d9
AG
2018 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2019 print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
2020 lengths[testnum]);
0f113f3e 2021 Time_F(START);
29dd15b1
NT
2022 count =
2023 run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
0f113f3e 2024 d = Time_F(STOP);
8b0b80d9 2025 print_result(D_CBC_192_AES, testnum, count, d);
0f113f3e
MC
2026 }
2027 }
2028 if (doit[D_CBC_256_AES]) {
8b0b80d9
AG
2029 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2030 print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
2031 lengths[testnum]);
0f113f3e 2032 Time_F(START);
29dd15b1
NT
2033 count =
2034 run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
0f113f3e 2035 d = Time_F(STOP);
8b0b80d9 2036 print_result(D_CBC_256_AES, testnum, count, d);
0f113f3e
MC
2037 }
2038 }
5f09d0ec 2039
0f113f3e 2040 if (doit[D_IGE_128_AES]) {
8b0b80d9
AG
2041 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2042 print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
2043 lengths[testnum]);
0f113f3e 2044 Time_F(START);
29dd15b1
NT
2045 count =
2046 run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
0f113f3e 2047 d = Time_F(STOP);
8b0b80d9 2048 print_result(D_IGE_128_AES, testnum, count, d);
0f113f3e
MC
2049 }
2050 }
2051 if (doit[D_IGE_192_AES]) {
8b0b80d9
AG
2052 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2053 print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
2054 lengths[testnum]);
0f113f3e 2055 Time_F(START);
29dd15b1
NT
2056 count =
2057 run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
0f113f3e 2058 d = Time_F(STOP);
8b0b80d9 2059 print_result(D_IGE_192_AES, testnum, count, d);
0f113f3e
MC
2060 }
2061 }
2062 if (doit[D_IGE_256_AES]) {
8b0b80d9
AG
2063 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2064 print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
2065 lengths[testnum]);
0f113f3e 2066 Time_F(START);
29dd15b1
NT
2067 count =
2068 run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
0f113f3e 2069 d = Time_F(STOP);
8b0b80d9 2070 print_result(D_IGE_256_AES, testnum, count, d);
0f113f3e
MC
2071 }
2072 }
2073 if (doit[D_GHASH]) {
0ff43435 2074 for (i = 0; i < loopargs_len; i++) {
29dd15b1
NT
2075 loopargs[i].gcm_ctx =
2076 CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
2077 CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx,
2078 (unsigned char *)"0123456789ab", 12);
8b0b80d9 2079 }
0f113f3e 2080
8b0b80d9 2081 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
29dd15b1
NT
2082 print_message(names[D_GHASH], c[D_GHASH][testnum],
2083 lengths[testnum]);
0f113f3e 2084 Time_F(START);
8b0b80d9 2085 count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
0f113f3e 2086 d = Time_F(STOP);
8b0b80d9 2087 print_result(D_GHASH, testnum, count, d);
0f113f3e 2088 }
0ff43435 2089 for (i = 0; i < loopargs_len; i++)
8b0b80d9 2090 CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
0f113f3e 2091 }
a00ae6c4 2092#ifndef OPENSSL_NO_CAMELLIA
0f113f3e 2093 if (doit[D_CBC_128_CML]) {
e5972607
F
2094 if (async_jobs > 0) {
2095 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2096 names[D_CBC_128_CML]);
2097 doit[D_CBC_128_CML] = 0;
2098 }
2099 for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
8b0b80d9
AG
2100 print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
2101 lengths[testnum]);
0f113f3e 2102 Time_F(START);
8b0b80d9
AG
2103 for (count = 0, run = 1; COND(c[D_CBC_128_CML][testnum]); count++)
2104 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
8829ce30 2105 (size_t)lengths[testnum], &camellia_ks1,
0f113f3e
MC
2106 iv, CAMELLIA_ENCRYPT);
2107 d = Time_F(STOP);
8b0b80d9 2108 print_result(D_CBC_128_CML, testnum, count, d);
0f113f3e
MC
2109 }
2110 }
2111 if (doit[D_CBC_192_CML]) {
e5972607
F
2112 if (async_jobs > 0) {
2113 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2114 names[D_CBC_192_CML]);
2115 doit[D_CBC_192_CML] = 0;
2116 }
2117 for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
8b0b80d9
AG
2118 print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
2119 lengths[testnum]);
2120 if (async_jobs > 0) {
2121 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2122 exit(1);
2123 }
0f113f3e 2124 Time_F(START);
8b0b80d9
AG
2125 for (count = 0, run = 1; COND(c[D_CBC_192_CML][testnum]); count++)
2126 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
8829ce30 2127 (size_t)lengths[testnum], &camellia_ks2,
0f113f3e
MC
2128 iv, CAMELLIA_ENCRYPT);
2129 d = Time_F(STOP);
8b0b80d9 2130 print_result(D_CBC_192_CML, testnum, count, d);
0f113f3e
MC
2131 }
2132 }
2133 if (doit[D_CBC_256_CML]) {
e5972607
F
2134 if (async_jobs > 0) {
2135 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2136 names[D_CBC_256_CML]);
2137 doit[D_CBC_256_CML] = 0;
2138 }
2139 for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
8b0b80d9
AG
2140 print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
2141 lengths[testnum]);
0f113f3e 2142 Time_F(START);
8b0b80d9
AG
2143 for (count = 0, run = 1; COND(c[D_CBC_256_CML][testnum]); count++)
2144 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
8829ce30 2145 (size_t)lengths[testnum], &camellia_ks3,
0f113f3e
MC
2146 iv, CAMELLIA_ENCRYPT);
2147 d = Time_F(STOP);
8b0b80d9 2148 print_result(D_CBC_256_CML, testnum, count, d);
0f113f3e
MC
2149 }
2150 }
a00ae6c4
RS
2151#endif
2152#ifndef OPENSSL_NO_IDEA
0f113f3e 2153 if (doit[D_CBC_IDEA]) {
e5972607
F
2154 if (async_jobs > 0) {
2155 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2156 names[D_CBC_IDEA]);
2157 doit[D_CBC_IDEA] = 0;
2158 }
2159 for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
29dd15b1
NT
2160 print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum],
2161 lengths[testnum]);
0f113f3e 2162 Time_F(START);
8b0b80d9 2163 for (count = 0, run = 1; COND(c[D_CBC_IDEA][testnum]); count++)
9021a5df 2164 IDEA_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
8829ce30 2165 (size_t)lengths[testnum], &idea_ks,
0f113f3e
MC
2166 iv, IDEA_ENCRYPT);
2167 d = Time_F(STOP);
8b0b80d9 2168 print_result(D_CBC_IDEA, testnum, count, d);
0f113f3e
MC
2169 }
2170 }
a00ae6c4
RS
2171#endif
2172#ifndef OPENSSL_NO_SEED
0f113f3e 2173 if (doit[D_CBC_SEED]) {
e5972607
F
2174 if (async_jobs > 0) {
2175 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2176 names[D_CBC_SEED]);
2177 doit[D_CBC_SEED] = 0;
2178 }
2179 for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
29dd15b1
NT
2180 print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum],
2181 lengths[testnum]);
0f113f3e 2182 Time_F(START);
8b0b80d9
AG
2183 for (count = 0, run = 1; COND(c[D_CBC_SEED][testnum]); count++)
2184 SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
8829ce30 2185 (size_t)lengths[testnum], &seed_ks, iv, 1);
0f113f3e 2186 d = Time_F(STOP);
8b0b80d9 2187 print_result(D_CBC_SEED, testnum, count, d);
0f113f3e
MC
2188 }
2189 }
a00ae6c4
RS
2190#endif
2191#ifndef OPENSSL_NO_RC2
0f113f3e 2192 if (doit[D_CBC_RC2]) {
e5972607
F
2193 if (async_jobs > 0) {
2194 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2195 names[D_CBC_RC2]);
2196 doit[D_CBC_RC2] = 0;
2197 }
2198 for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
29dd15b1
NT
2199 print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum],
2200 lengths[testnum]);
8b0b80d9
AG
2201 if (async_jobs > 0) {
2202 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2203 exit(1);
2204 }
0f113f3e 2205 Time_F(START);
8b0b80d9
AG
2206 for (count = 0, run = 1; COND(c[D_CBC_RC2][testnum]); count++)
2207 RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
8829ce30 2208 (size_t)lengths[testnum], &rc2_ks,
0f113f3e
MC
2209 iv, RC2_ENCRYPT);
2210 d = Time_F(STOP);
8b0b80d9 2211 print_result(D_CBC_RC2, testnum, count, d);
0f113f3e
MC
2212 }
2213 }
a00ae6c4
RS
2214#endif
2215#ifndef OPENSSL_NO_RC5
0f113f3e 2216 if (doit[D_CBC_RC5]) {
e5972607
F
2217 if (async_jobs > 0) {
2218 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2219 names[D_CBC_RC5]);
2220 doit[D_CBC_RC5] = 0;
2221 }
2222 for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
29dd15b1
NT
2223 print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum],
2224 lengths[testnum]);
8b0b80d9
AG
2225 if (async_jobs > 0) {
2226 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2227 exit(1);
2228 }
0f113f3e 2229 Time_F(START);
8b0b80d9
AG
2230 for (count = 0, run = 1; COND(c[D_CBC_RC5][testnum]); count++)
2231 RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
8829ce30 2232 (size_t)lengths[testnum], &rc5_ks,
0f113f3e
MC
2233 iv, RC5_ENCRYPT);
2234 d = Time_F(STOP);
8b0b80d9 2235 print_result(D_CBC_RC5, testnum, count, d);
0f113f3e
MC
2236 }
2237 }
a00ae6c4
RS
2238#endif
2239#ifndef OPENSSL_NO_BF
0f113f3e 2240 if (doit[D_CBC_BF]) {
e5972607
F
2241 if (async_jobs > 0) {
2242 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2243 names[D_CBC_BF]);
2244 doit[D_CBC_BF] = 0;
2245 }
2246 for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
29dd15b1
NT
2247 print_message(names[D_CBC_BF], c[D_CBC_BF][testnum],
2248 lengths[testnum]);
0f113f3e 2249 Time_F(START);
8b0b80d9
AG
2250 for (count = 0, run = 1; COND(c[D_CBC_BF][testnum]); count++)
2251 BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
8829ce30 2252 (size_t)lengths[testnum], &bf_ks,
0f113f3e
MC
2253 iv, BF_ENCRYPT);
2254 d = Time_F(STOP);
8b0b80d9 2255 print_result(D_CBC_BF, testnum, count, d);
0f113f3e
MC
2256 }
2257 }
a00ae6c4
RS
2258#endif
2259#ifndef OPENSSL_NO_CAST
0f113f3e 2260 if (doit[D_CBC_CAST]) {
e5972607
F
2261 if (async_jobs > 0) {
2262 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2263 names[D_CBC_CAST]);
2264 doit[D_CBC_CAST] = 0;
2265 }
2266 for (testnum = 0; testnum < SIZE_NUM && async_init == 0; testnum++) {
29dd15b1
NT
2267 print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum],
2268 lengths[testnum]);
0f113f3e 2269 Time_F(START);
8b0b80d9
AG
2270 for (count = 0, run = 1; COND(c[D_CBC_CAST][testnum]); count++)
2271 CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
8829ce30 2272 (size_t)lengths[testnum], &cast_ks,
0f113f3e
MC
2273 iv, CAST_ENCRYPT);
2274 d = Time_F(STOP);
8b0b80d9 2275 print_result(D_CBC_CAST, testnum, count, d);
0f113f3e
MC
2276 }
2277 }
a00ae6c4 2278#endif
f3dea9a5 2279
0f113f3e 2280 if (doit[D_EVP]) {
a00ae6c4 2281#ifdef EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK
0f113f3e
MC
2282 if (multiblock && evp_cipher) {
2283 if (!
2284 (EVP_CIPHER_flags(evp_cipher) &
2285 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
7768e116 2286 BIO_printf(bio_err, "%s is not multi-block capable\n",
6c2ff56e 2287 OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
0f113f3e
MC
2288 goto end;
2289 }
8b0b80d9
AG
2290 if (async_jobs > 0) {
2291 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2292 exit(1);
2293 }
0f113f3e 2294 multiblock_speed(evp_cipher);
7e1b7485 2295 ret = 0;
0f113f3e
MC
2296 goto end;
2297 }
a00ae6c4 2298#endif
8b0b80d9 2299 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
0f113f3e 2300 if (evp_cipher) {
0f113f3e 2301
6c2ff56e 2302 names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
0f113f3e
MC
2303 /*
2304 * -O3 -fschedule-insns messes up an optimization here!
2305 * names[D_EVP] somehow becomes NULL
2306 */
8b0b80d9
AG
2307 print_message(names[D_EVP], save_count, lengths[testnum]);
2308
2309 for (k = 0; k < loopargs_len; k++) {
2310 loopargs[k].ctx = EVP_CIPHER_CTX_new();
2311 if (decrypt)
29dd15b1
NT
2312 EVP_DecryptInit_ex(loopargs[k].ctx, evp_cipher, NULL,
2313 key16, iv);
8b0b80d9 2314 else
29dd15b1
NT
2315 EVP_EncryptInit_ex(loopargs[k].ctx, evp_cipher, NULL,
2316 key16, iv);
8b0b80d9
AG
2317 EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
2318 }
0f113f3e
MC
2319
2320 Time_F(START);
8b0b80d9 2321 count = run_benchmark(async_jobs, EVP_Update_loop, loopargs);
0f113f3e 2322 d = Time_F(STOP);
8b0b80d9
AG
2323 for (k = 0; k < loopargs_len; k++) {
2324 EVP_CIPHER_CTX_free(loopargs[k].ctx);
2325 }
0f113f3e
MC
2326 }
2327 if (evp_md) {
cc9d6655 2328 names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
8b0b80d9 2329 print_message(names[D_EVP], save_count, lengths[testnum]);
0f113f3e 2330 Time_F(START);
8b0b80d9 2331 count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
0f113f3e
MC
2332 d = Time_F(STOP);
2333 }
8b0b80d9 2334 print_result(D_EVP, testnum, count, d);
0f113f3e
MC
2335 }
2336 }
7e1b7485 2337
0ff43435 2338 for (i = 0; i < loopargs_len; i++)
8b0b80d9
AG
2339 RAND_bytes(loopargs[i].buf, 36);
2340
a00ae6c4 2341#ifndef OPENSSL_NO_RSA
8b0b80d9
AG
2342 for (testnum = 0; testnum < RSA_NUM; testnum++) {
2343 int st = 0;
2344 if (!rsa_doit[testnum])
0f113f3e 2345 continue;
0ff43435
AG
2346 for (i = 0; i < loopargs_len; i++) {
2347 st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
0930e07d 2348 &loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
8b0b80d9
AG
2349 if (st == 0)
2350 break;
2351 }
7e1b7485 2352 if (st == 0) {
0f113f3e
MC
2353 BIO_printf(bio_err,
2354 "RSA sign failure. No RSA sign will be done.\n");
2355 ERR_print_errors(bio_err);
2356 rsa_count = 1;
2357 } else {
2358 pkey_print_message("private", "rsa",
29dd15b1
NT
2359 rsa_c[testnum][0], rsa_bits[testnum],
2360 RSA_SECONDS);
8b0b80d9 2361 /* RSA_blinding_on(rsa_key[testnum],NULL); */
0f113f3e 2362 Time_F(START);
8b0b80d9 2363 count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
0f113f3e
MC
2364 d = Time_F(STOP);
2365 BIO_printf(bio_err,
2366 mr ? "+R1:%ld:%d:%.2f\n"
2367 : "%ld %d bit private RSA's in %.2fs\n",
8b0b80d9
AG
2368 count, rsa_bits[testnum], d);
2369 rsa_results[testnum][0] = d / (double)count;
0f113f3e
MC
2370 rsa_count = count;
2371 }
d02b48c6 2372
0ff43435
AG
2373 for (i = 0; i < loopargs_len; i++) {
2374 st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
0930e07d 2375 loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
8b0b80d9
AG
2376 if (st <= 0)
2377 break;
2378 }
7e1b7485 2379 if (st <= 0) {
0f113f3e
MC
2380 BIO_printf(bio_err,
2381 "RSA verify failure. No RSA verify will be done.\n");
2382 ERR_print_errors(bio_err);
8b0b80d9 2383 rsa_doit[testnum] = 0;
0f113f3e
MC
2384 } else {
2385 pkey_print_message("public", "rsa",
29dd15b1
NT
2386 rsa_c[testnum][1], rsa_bits[testnum],
2387 RSA_SECONDS);
0f113f3e 2388 Time_F(START);
8b0b80d9 2389 count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
0f113f3e
MC
2390 d = Time_F(STOP);
2391 BIO_printf(bio_err,
2392 mr ? "+R2:%ld:%d:%.2f\n"
2393 : "%ld %d bit public RSA's in %.2fs\n",
8b0b80d9
AG
2394 count, rsa_bits[testnum], d);
2395 rsa_results[testnum][1] = d / (double)count;
0f113f3e 2396 }
d02b48c6 2397
0f113f3e
MC
2398 if (rsa_count <= 1) {
2399 /* if longer than 10s, don't do any more */
8b0b80d9
AG
2400 for (testnum++; testnum < RSA_NUM; testnum++)
2401 rsa_doit[testnum] = 0;
0f113f3e
MC
2402 }
2403 }
d6073e27 2404#endif /* OPENSSL_NO_RSA */
d02b48c6 2405
0ff43435 2406 for (i = 0; i < loopargs_len; i++)
8b0b80d9
AG
2407 RAND_bytes(loopargs[i].buf, 36);
2408
a00ae6c4 2409#ifndef OPENSSL_NO_DSA
0f113f3e
MC
2410 if (RAND_status() != 1) {
2411 RAND_seed(rnd_seed, sizeof rnd_seed);
0f113f3e 2412 }
8b0b80d9
AG
2413 for (testnum = 0; testnum < DSA_NUM; testnum++) {
2414 int st = 0;
2415 if (!dsa_doit[testnum])
0f113f3e
MC
2416 continue;
2417
8b0b80d9
AG
2418 /* DSA_generate_key(dsa_key[testnum]); */
2419 /* DSA_sign_setup(dsa_key[testnum],NULL); */
0ff43435
AG
2420 for (i = 0; i < loopargs_len; i++) {
2421 st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
0930e07d 2422 &loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
8b0b80d9
AG
2423 if (st == 0)
2424 break;
2425 }
7e1b7485 2426 if (st == 0) {
0f113f3e
MC
2427 BIO_printf(bio_err,
2428 "DSA sign failure. No DSA sign will be done.\n");
2429 ERR_print_errors(bio_err);
2430 rsa_count = 1;
2431 } else {
2432 pkey_print_message("sign", "dsa",
29dd15b1
NT
2433 dsa_c[testnum][0], dsa_bits[testnum],
2434 DSA_SECONDS);
0f113f3e 2435 Time_F(START);
8b0b80d9 2436 count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
0f113f3e
MC
2437 d = Time_F(STOP);
2438 BIO_printf(bio_err,
2439 mr ? "+R3:%ld:%d:%.2f\n"
2440 : "%ld %d bit DSA signs in %.2fs\n",
8b0b80d9
AG
2441 count, dsa_bits[testnum], d);
2442 dsa_results[testnum][0] = d / (double)count;
0f113f3e
MC
2443 rsa_count = count;
2444 }
e172d60d 2445
0ff43435
AG
2446 for (i = 0; i < loopargs_len; i++) {
2447 st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
0930e07d 2448 loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
8b0b80d9
AG
2449 if (st <= 0)
2450 break;
2451 }
7e1b7485 2452 if (st <= 0) {
0f113f3e
MC
2453 BIO_printf(bio_err,
2454 "DSA verify failure. No DSA verify will be done.\n");
2455 ERR_print_errors(bio_err);
8b0b80d9 2456 dsa_doit[testnum] = 0;
0f113f3e
MC
2457 } else {
2458 pkey_print_message("verify", "dsa",
29dd15b1
NT
2459 dsa_c[testnum][1], dsa_bits[testnum],
2460 DSA_SECONDS);
0f113f3e 2461 Time_F(START);
8b0b80d9 2462 count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
0f113f3e
MC
2463 d = Time_F(STOP);
2464 BIO_printf(bio_err,
2465 mr ? "+R4:%ld:%d:%.2f\n"
2466 : "%ld %d bit DSA verify in %.2fs\n",
8b0b80d9
AG
2467 count, dsa_bits[testnum], d);
2468 dsa_results[testnum][1] = d / (double)count;
0f113f3e 2469 }
e172d60d 2470
0f113f3e
MC
2471 if (rsa_count <= 1) {
2472 /* if longer than 10s, don't do any more */
8b0b80d9
AG
2473 for (testnum++; testnum < DSA_NUM; testnum++)
2474 dsa_doit[testnum] = 0;
0f113f3e
MC
2475 }
2476 }
d6073e27 2477#endif /* OPENSSL_NO_DSA */
e172d60d 2478
10bf4fc2 2479#ifndef OPENSSL_NO_EC
0f113f3e
MC
2480 if (RAND_status() != 1) {
2481 RAND_seed(rnd_seed, sizeof rnd_seed);
0f113f3e 2482 }
8b0b80d9 2483 for (testnum = 0; testnum < EC_NUM; testnum++) {
0ff43435 2484 int st = 1;
0f113f3e 2485
8b0b80d9 2486 if (!ecdsa_doit[testnum])
0f113f3e 2487 continue; /* Ignore Curve */
0ff43435 2488 for (i = 0; i < loopargs_len; i++) {
29dd15b1
NT
2489 loopargs[i].ecdsa[testnum] =
2490 EC_KEY_new_by_curve_name(test_curves[testnum]);
0ff43435
AG
2491 if (loopargs[i].ecdsa[testnum] == NULL) {
2492 st = 0;
2493 break;
2494 }
2495 }
2496 if (st == 0) {
0f113f3e
MC
2497 BIO_printf(bio_err, "ECDSA failure.\n");
2498 ERR_print_errors(bio_err);
2499 rsa_count = 1;
2500 } else {
0ff43435
AG
2501 for (i = 0; i < loopargs_len; i++) {
2502 EC_KEY_precompute_mult(loopargs[i].ecdsa[testnum], NULL);
2503 /* Perform ECDSA signature test */
2504 EC_KEY_generate_key(loopargs[i].ecdsa[testnum]);
2505 st = ECDSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
29dd15b1
NT
2506 &loopargs[i].siglen,
2507 loopargs[i].ecdsa[testnum]);
8b0b80d9
AG
2508 if (st == 0)
2509 break;
2510 }
7e1b7485 2511 if (st == 0) {
0f113f3e
MC
2512 BIO_printf(bio_err,
2513 "ECDSA sign failure. No ECDSA sign will be done.\n");
2514 ERR_print_errors(bio_err);
2515 rsa_count = 1;
2516 } else {
2517 pkey_print_message("sign", "ecdsa",
8b0b80d9
AG
2518 ecdsa_c[testnum][0],
2519 test_curves_bits[testnum], ECDSA_SECONDS);
0f113f3e 2520 Time_F(START);
8b0b80d9 2521 count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
0f113f3e
MC
2522 d = Time_F(STOP);
2523
2524 BIO_printf(bio_err,
2525 mr ? "+R5:%ld:%d:%.2f\n" :
2526 "%ld %d bit ECDSA signs in %.2fs \n",
8b0b80d9
AG
2527 count, test_curves_bits[testnum], d);
2528 ecdsa_results[testnum][0] = d / (double)count;
0f113f3e
MC
2529 rsa_count = count;
2530 }
2531
2532 /* Perform ECDSA verification test */
0ff43435
AG
2533 for (i = 0; i < loopargs_len; i++) {
2534 st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
29dd15b1
NT
2535 loopargs[i].siglen,
2536 loopargs[i].ecdsa[testnum]);
8b0b80d9
AG
2537 if (st != 1)
2538 break;
2539 }
7e1b7485 2540 if (st != 1) {
0f113f3e
MC
2541 BIO_printf(bio_err,
2542 "ECDSA verify failure. No ECDSA verify will be done.\n");
2543 ERR_print_errors(bio_err);
8b0b80d9 2544 ecdsa_doit[testnum] = 0;
0f113f3e
MC
2545 } else {
2546 pkey_print_message("verify", "ecdsa",
8b0b80d9
AG
2547 ecdsa_c[testnum][1],
2548 test_curves_bits[testnum], ECDSA_SECONDS);
0f113f3e 2549 Time_F(START);
8b0b80d9 2550 count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
0f113f3e
MC
2551 d = Time_F(STOP);
2552 BIO_printf(bio_err,
2553 mr ? "+R6:%ld:%d:%.2f\n"
2554 : "%ld %d bit ECDSA verify in %.2fs\n",
8b0b80d9
AG
2555 count, test_curves_bits[testnum], d);
2556 ecdsa_results[testnum][1] = d / (double)count;
0f113f3e
MC
2557 }
2558
2559 if (rsa_count <= 1) {
2560 /* if longer than 10s, don't do any more */
8b0b80d9
AG
2561 for (testnum++; testnum < EC_NUM; testnum++)
2562 ecdsa_doit[testnum] = 0;
0f113f3e
MC
2563 }
2564 }
2565 }
7e1b7485 2566
0f113f3e
MC
2567 if (RAND_status() != 1) {
2568 RAND_seed(rnd_seed, sizeof rnd_seed);
0f113f3e 2569 }
8b0b80d9 2570 for (testnum = 0; testnum < EC_NUM; testnum++) {
4d82c58b
F
2571 int ecdh_checks = 1;
2572
8b0b80d9 2573 if (!ecdh_doit[testnum])
0f113f3e 2574 continue;
ed7377db 2575
0ff43435 2576 for (i = 0; i < loopargs_len; i++) {
2e4c3b5c 2577 EVP_PKEY_CTX *kctx = NULL;
f7d984dd 2578 EVP_PKEY_CTX *test_ctx = NULL;
2e4c3b5c
NT
2579 EVP_PKEY_CTX *ctx = NULL;
2580 EVP_PKEY *key_A = NULL;
2581 EVP_PKEY *key_B = NULL;
cc98e639 2582 size_t outlen;
f7d984dd 2583 size_t test_outlen;
ed7377db
NT
2584
2585 if (testnum == R_EC_X25519) {
c29c7aad 2586 kctx = EVP_PKEY_CTX_new_id(test_curves[testnum], NULL); /* keygen ctx from NID */
ed7377db
NT
2587 } else {
2588 EVP_PKEY_CTX *pctx = NULL;
2589 EVP_PKEY *params = NULL;
2590
29dd15b1
NT
2591 if ( /* Create the context for parameter generation */
2592 !(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) ||
2593 /* Initialise the parameter generation */
2594 !EVP_PKEY_paramgen_init(pctx) ||
2595 /* Set the curve by NID */
2596 !EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
2597 test_curves
2598 [testnum]) ||
2599 /* Create the parameter object params */
2e4c3b5c 2600 !EVP_PKEY_paramgen(pctx, &params)) {
0f113f3e 2601 ecdh_checks = 0;
ed7377db
NT
2602 BIO_printf(bio_err, "ECDH init failure.\n");
2603 ERR_print_errors(bio_err);
0ff43435 2604 rsa_count = 1;
ed7377db 2605 break;
0ff43435 2606 }
ed7377db
NT
2607 /* Create the context for the key generation */
2608 kctx = EVP_PKEY_CTX_new(params, NULL);
2609
29dd15b1
NT
2610 EVP_PKEY_free(params);
2611 params = NULL;
2612 EVP_PKEY_CTX_free(pctx);
2613 pctx = NULL;
447402e6 2614 }
29dd15b1 2615 if (!kctx || /* keygen ctx is not null */
2e4c3b5c 2616 !EVP_PKEY_keygen_init(kctx) /* init keygen ctx */ ) {
ed7377db
NT
2617 ecdh_checks = 0;
2618 BIO_printf(bio_err, "ECDH keygen failure.\n");
2619 ERR_print_errors(bio_err);
2620 rsa_count = 1;
2621 break;
0f113f3e 2622 }
ed7377db 2623
29dd15b1
NT
2624 if (!EVP_PKEY_keygen(kctx, &key_A) || /* generate secret key A */
2625 !EVP_PKEY_keygen(kctx, &key_B) || /* generate secret key B */
2626 !(ctx = EVP_PKEY_CTX_new(key_A, NULL)) || /* derivation ctx from skeyA */
2627 !EVP_PKEY_derive_init(ctx) || /* init derivation ctx */
2628 !EVP_PKEY_derive_set_peer(ctx, key_B) || /* set peer pubkey in ctx */
2629 !EVP_PKEY_derive(ctx, NULL, &outlen) || /* determine max length */
f7d984dd 2630 outlen == 0 || /* ensure outlen is a valid size */
2e4c3b5c 2631 outlen > MAX_ECDH_SIZE /* avoid buffer overflow */ ) {
ed7377db
NT
2632 ecdh_checks = 0;
2633 BIO_printf(bio_err, "ECDH key generation failure.\n");
2634 ERR_print_errors(bio_err);
2635 rsa_count = 1;
2636 break;
2637 }
2638
f7d984dd
NT
2639 /* Here we perform a test run, comparing the output of a*B and b*A;
2640 * we try this here and assume that further EVP_PKEY_derive calls
2641 * never fail, so we can skip checks in the actually benchmarked
2642 * code, for maximum performance. */
2643 if (!(test_ctx = EVP_PKEY_CTX_new(key_B, NULL)) || /* test ctx from skeyB */
2644 !EVP_PKEY_derive_init(test_ctx) || /* init derivation test_ctx */
2645 !EVP_PKEY_derive_set_peer(test_ctx, key_A) || /* set peer pubkey in test_ctx */
2646 !EVP_PKEY_derive(test_ctx, NULL, &test_outlen) || /* determine max length */
2647 !EVP_PKEY_derive(ctx, loopargs[i].secret_a, &outlen) || /* compute a*B */
2648 !EVP_PKEY_derive(test_ctx, loopargs[i].secret_b, &test_outlen) || /* compute b*A */
2649 test_outlen != outlen /* compare output length */ ) {
2650 ecdh_checks = 0;
2651 BIO_printf(bio_err, "ECDH computation failure.\n");
2652 ERR_print_errors(bio_err);
2653 rsa_count = 1;
2654 break;
2655 }
2656 for (k = 0; (unsigned int)k < test_outlen && ecdh_checks == 1; k++) {
2657 if (loopargs[i].secret_a[k] != loopargs[i].secret_b[k])
2658 ecdh_checks = 0;
2659 }
2660 if (ecdh_checks == 0) {
2661 BIO_printf(bio_err, "ECDH computations don't match.\n");
2662 ERR_print_errors(bio_err);
2663 rsa_count = 1;
2664 break;
2665 }
2666
ed7377db 2667 loopargs[i].ecdh_ctx[testnum] = ctx;
cc98e639 2668 loopargs[i].outlen[testnum] = outlen;
ed7377db 2669
29dd15b1
NT
2670 EVP_PKEY_CTX_free(kctx);
2671 kctx = NULL;
f7d984dd
NT
2672 EVP_PKEY_CTX_free(test_ctx);
2673 test_ctx = NULL;
ed7377db
NT
2674 }
2675 if (ecdh_checks != 0) {
2676 pkey_print_message("", "ecdh",
29dd15b1
NT
2677 ecdh_c[testnum][0],
2678 test_curves_bits[testnum], ECDH_SECONDS);
ed7377db 2679 Time_F(START);
29dd15b1
NT
2680 count =
2681 run_benchmark(async_jobs, ECDH_EVP_derive_key_loop, loopargs);
ed7377db
NT
2682 d = Time_F(STOP);
2683 BIO_printf(bio_err,
29dd15b1
NT
2684 mr ? "+R7:%ld:%d:%.2f\n" :
2685 "%ld %d-bit ECDH ops in %.2fs\n", count,
2686 test_curves_bits[testnum], d);
ed7377db
NT
2687 ecdh_results[testnum][0] = d / (double)count;
2688 rsa_count = count;
0f113f3e 2689 }
e172d60d 2690
0f113f3e
MC
2691 if (rsa_count <= 1) {
2692 /* if longer than 10s, don't do any more */
8b0b80d9
AG
2693 for (testnum++; testnum < EC_NUM; testnum++)
2694 ecdh_doit[testnum] = 0;
0f113f3e
MC
2695 }
2696 }
d6073e27 2697#endif /* OPENSSL_NO_EC */
a00ae6c4 2698#ifndef NO_FORK
0f113f3e 2699 show_res:
a00ae6c4 2700#endif
0f113f3e 2701 if (!mr) {
b0700d2c
RS
2702 printf("%s\n", OpenSSL_version(OPENSSL_VERSION));
2703 printf("%s\n", OpenSSL_version(OPENSSL_BUILT_ON));
0f113f3e
MC
2704 printf("options:");
2705 printf("%s ", BN_options());
a00ae6c4 2706#ifndef OPENSSL_NO_MD2
0f113f3e 2707 printf("%s ", MD2_options());
a00ae6c4
RS
2708#endif
2709#ifndef OPENSSL_NO_RC4
0f113f3e 2710 printf("%s ", RC4_options());
a00ae6c4
RS
2711#endif
2712#ifndef OPENSSL_NO_DES
0f113f3e 2713 printf("%s ", DES_options());
a00ae6c4 2714#endif
0f113f3e 2715 printf("%s ", AES_options());
a00ae6c4 2716#ifndef OPENSSL_NO_IDEA
9021a5df 2717 printf("%s ", IDEA_options());
a00ae6c4
RS
2718#endif
2719#ifndef OPENSSL_NO_BF
0f113f3e 2720 printf("%s ", BF_options());
a00ae6c4 2721#endif
b0700d2c 2722 printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
0f113f3e 2723 }
e172d60d 2724
0f113f3e
MC
2725 if (pr_header) {
2726 if (mr)
7e1b7485 2727 printf("+H");
0f113f3e 2728 else {
7e1b7485
RS
2729 printf
2730 ("The 'numbers' are in 1000s of bytes per second processed.\n");
2731 printf("type ");
0f113f3e 2732 }
8b0b80d9
AG
2733 for (testnum = 0; testnum < SIZE_NUM; testnum++)
2734 printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
7e1b7485 2735 printf("\n");
0f113f3e 2736 }
e172d60d 2737
0f113f3e
MC
2738 for (k = 0; k < ALGOR_NUM; k++) {
2739 if (!doit[k])
2740 continue;
2741 if (mr)
7e1b7485 2742 printf("+F:%d:%s", k, names[k]);
0f113f3e 2743 else
7e1b7485 2744 printf("%-13s", names[k]);
8b0b80d9
AG
2745 for (testnum = 0; testnum < SIZE_NUM; testnum++) {
2746 if (results[k][testnum] > 10000 && !mr)
2747 printf(" %11.2fk", results[k][testnum] / 1e3);
0f113f3e 2748 else
8b0b80d9 2749 printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
0f113f3e 2750 }
7e1b7485 2751 printf("\n");
0f113f3e 2752 }
a00ae6c4 2753#ifndef OPENSSL_NO_RSA
8b0b80d9 2754 testnum = 1;
0f113f3e
MC
2755 for (k = 0; k < RSA_NUM; k++) {
2756 if (!rsa_doit[k])
2757 continue;
8b0b80d9 2758 if (testnum && !mr) {
0f113f3e 2759 printf("%18ssign verify sign/s verify/s\n", " ");
8b0b80d9 2760 testnum = 0;
0f113f3e
MC
2761 }
2762 if (mr)
7e1b7485
RS
2763 printf("+F2:%u:%u:%f:%f\n",
2764 k, rsa_bits[k], rsa_results[k][0], rsa_results[k][1]);
0f113f3e 2765 else
7e1b7485
RS
2766 printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2767 rsa_bits[k], rsa_results[k][0], rsa_results[k][1],
2768 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1]);
0f113f3e 2769 }
a00ae6c4
RS
2770#endif
2771#ifndef OPENSSL_NO_DSA
8b0b80d9 2772 testnum = 1;
0f113f3e
MC
2773 for (k = 0; k < DSA_NUM; k++) {
2774 if (!dsa_doit[k])
2775 continue;
8b0b80d9 2776 if (testnum && !mr) {
0f113f3e 2777 printf("%18ssign verify sign/s verify/s\n", " ");
8b0b80d9 2778 testnum = 0;
0f113f3e
MC
2779 }
2780 if (mr)
7e1b7485
RS
2781 printf("+F3:%u:%u:%f:%f\n",
2782 k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
0f113f3e 2783 else
7e1b7485
RS
2784 printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2785 dsa_bits[k], dsa_results[k][0], dsa_results[k][1],
2786 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1]);
0f113f3e 2787 }
a00ae6c4 2788#endif
10bf4fc2 2789#ifndef OPENSSL_NO_EC
8b0b80d9 2790 testnum = 1;
0f113f3e
MC
2791 for (k = 0; k < EC_NUM; k++) {
2792 if (!ecdsa_doit[k])
2793 continue;
8b0b80d9 2794 if (testnum && !mr) {
0f113f3e 2795 printf("%30ssign verify sign/s verify/s\n", " ");
8b0b80d9 2796 testnum = 0;
0f113f3e
MC
2797 }
2798
2799 if (mr)
7e1b7485
RS
2800 printf("+F4:%u:%u:%f:%f\n",
2801 k, test_curves_bits[k],
2802 ecdsa_results[k][0], ecdsa_results[k][1]);
0f113f3e 2803 else
7e1b7485
RS
2804 printf("%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
2805 test_curves_bits[k],
2806 test_curves_names[k],
2807 ecdsa_results[k][0], ecdsa_results[k][1],
2808 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1]);
0f113f3e 2809 }
7e1b7485 2810
8b0b80d9 2811 testnum = 1;
0f113f3e
MC
2812 for (k = 0; k < EC_NUM; k++) {
2813 if (!ecdh_doit[k])
2814 continue;
8b0b80d9 2815 if (testnum && !mr) {
0f113f3e 2816 printf("%30sop op/s\n", " ");
8b0b80d9 2817 testnum = 0;
0f113f3e
MC
2818 }
2819 if (mr)
7e1b7485
RS
2820 printf("+F5:%u:%u:%f:%f\n",
2821 k, test_curves_bits[k],
2822 ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
0f113f3e
MC
2823
2824 else
7e1b7485
RS
2825 printf("%4u bit ecdh (%s) %8.4fs %8.1f\n",
2826 test_curves_bits[k],
2827 test_curves_names[k],
2828 ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
0f113f3e 2829 }
a00ae6c4 2830#endif
0f113f3e 2831
7e1b7485 2832 ret = 0;
0f113f3e
MC
2833
2834 end:
2835 ERR_print_errors(bio_err);
0ff43435 2836 for (i = 0; i < loopargs_len; i++) {
b2839683
AG
2837 OPENSSL_free(loopargs[i].buf_malloc);
2838 OPENSSL_free(loopargs[i].buf2_malloc);
5f986ed3 2839
a00ae6c4 2840#ifndef OPENSSL_NO_RSA
0ff43435
AG
2841 for (k = 0; k < RSA_NUM; k++)
2842 RSA_free(loopargs[i].rsa_key[k]);
a00ae6c4
RS
2843#endif
2844#ifndef OPENSSL_NO_DSA
0ff43435
AG
2845 for (k = 0; k < DSA_NUM; k++)
2846 DSA_free(loopargs[i].dsa_key[k]);
a00ae6c4 2847#endif
10bf4fc2 2848#ifndef OPENSSL_NO_EC
0ff43435
AG
2849 for (k = 0; k < EC_NUM; k++) {
2850 EC_KEY_free(loopargs[i].ecdsa[k]);
ed7377db 2851 EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
0ff43435 2852 }
b2839683
AG
2853 OPENSSL_free(loopargs[i].secret_a);
2854 OPENSSL_free(loopargs[i].secret_b);
a00ae6c4 2855#endif
5f986ed3
F
2856 }
2857
1e613922
AG
2858 if (async_jobs > 0) {
2859 for (i = 0; i < loopargs_len; i++)
2860 ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
dab1f5fe 2861 }
1e613922 2862
dab1f5fe 2863 if (async_init) {
8b0b80d9 2864 ASYNC_cleanup_thread();
1e613922
AG
2865 }
2866 OPENSSL_free(loopargs);
dd1abd44 2867 release_engine(e);
7e1b7485 2868 return (ret);
0f113f3e 2869}
d02b48c6 2870
646d5695 2871static void print_message(const char *s, long num, int length)
0f113f3e 2872{
a00ae6c4 2873#ifdef SIGALRM
0f113f3e
MC
2874 BIO_printf(bio_err,
2875 mr ? "+DT:%s:%d:%d\n"
2876 : "Doing %s for %ds on %d size blocks: ", s, SECONDS, length);
2877 (void)BIO_flush(bio_err);
2878 alarm(SECONDS);
a00ae6c4 2879#else
0f113f3e
MC
2880 BIO_printf(bio_err,
2881 mr ? "+DN:%s:%ld:%d\n"
2882 : "Doing %s %ld times on %d size blocks: ", s, num, length);
2883 (void)BIO_flush(bio_err);
a00ae6c4 2884#endif
0f113f3e 2885}
d02b48c6 2886
689c6f25 2887static void pkey_print_message(const char *str, const char *str2, long num,
0f113f3e
MC
2888 int bits, int tm)
2889{
a00ae6c4 2890#ifdef SIGALRM
0f113f3e
MC
2891 BIO_printf(bio_err,
2892 mr ? "+DTP:%d:%s:%s:%d\n"
2893 : "Doing %d bit %s %s's for %ds: ", bits, str, str2, tm);
2894 (void)BIO_flush(bio_err);
2895 alarm(tm);
a00ae6c4 2896#else
0f113f3e
MC
2897 BIO_printf(bio_err,
2898 mr ? "+DNP:%ld:%d:%s:%s\n"
2899 : "Doing %ld %d bit %s %s's: ", num, bits, str, str2);
2900 (void)BIO_flush(bio_err);
a00ae6c4 2901#endif
0f113f3e 2902}
58964a49 2903
0f113f3e
MC
2904static void print_result(int alg, int run_no, int count, double time_used)
2905{
d166ed8c
DSH
2906 if (count == -1) {
2907 BIO_puts(bio_err, "EVP error!\n");
2908 exit(1);
2909 }
0f113f3e
MC
2910 BIO_printf(bio_err,
2911 mr ? "+R:%d:%s:%f\n"
2912 : "%d %s's in %.2fs\n", count, names[alg], time_used);
2913 results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
2914}
0e211563 2915
a00ae6c4 2916#ifndef NO_FORK
0e211563 2917static char *sstrsep(char **string, const char *delim)
0f113f3e 2918{
0e211563
BL
2919 char isdelim[256];
2920 char *token = *string;
2921
2922 if (**string == 0)
2923 return NULL;
2924
54a656ef 2925 memset(isdelim, 0, sizeof isdelim);
0e211563
BL
2926 isdelim[0] = 1;
2927
0f113f3e 2928 while (*delim) {
0e211563
BL
2929 isdelim[(unsigned char)(*delim)] = 1;
2930 delim++;
0f113f3e 2931 }
0e211563 2932
0f113f3e 2933 while (!isdelim[(unsigned char)(**string)]) {
0e211563 2934 (*string)++;
0f113f3e 2935 }
0e211563 2936
0f113f3e 2937 if (**string) {
0e211563
BL
2938 **string = 0;
2939 (*string)++;
0f113f3e 2940 }
0e211563
BL
2941
2942 return token;
0f113f3e 2943}
0e211563
BL
2944
2945static int do_multi(int multi)
0f113f3e
MC
2946{
2947 int n;
2948 int fd[2];
2949 int *fds;
2950 static char sep[] = ":";
2951
b4faea50 2952 fds = malloc(sizeof(*fds) * multi);
0f113f3e
MC
2953 for (n = 0; n < multi; ++n) {
2954 if (pipe(fd) == -1) {
7768e116 2955 BIO_printf(bio_err, "pipe failure\n");
0f113f3e
MC
2956 exit(1);
2957 }
2958 fflush(stdout);
7768e116 2959 (void)BIO_flush(bio_err);
0f113f3e
MC
2960 if (fork()) {
2961 close(fd[1]);
2962 fds[n] = fd[0];
2963 } else {
2964 close(fd[0]);
2965 close(1);
2966 if (dup(fd[1]) == -1) {
7768e116 2967 BIO_printf(bio_err, "dup failed\n");
0f113f3e
MC
2968 exit(1);
2969 }
2970 close(fd[1]);
2971 mr = 1;
2972 usertime = 0;
2973 free(fds);
2974 return 0;
2975 }
2976 printf("Forked child %d\n", n);
2977 }
e172d60d 2978
0f113f3e
MC
2979 /* for now, assume the pipe is long enough to take all the output */
2980 for (n = 0; n < multi; ++n) {
2981 FILE *f;
2982 char buf[1024];
2983 char *p;
2984
2985 f = fdopen(fds[n], "r");
2986 while (fgets(buf, sizeof buf, f)) {
2987 p = strchr(buf, '\n');
2988 if (p)
2989 *p = '\0';
2990 if (buf[0] != '+') {
29dd15b1
NT
2991 BIO_printf(bio_err,
2992 "Don't understand line '%s' from child %d\n", buf,
2993 n);
0f113f3e
MC
2994 continue;
2995 }
2996 printf("Got: %s from %d\n", buf, n);
86885c28 2997 if (strncmp(buf, "+F:", 3) == 0) {
0f113f3e
MC
2998 int alg;
2999 int j;
3000
3001 p = buf + 3;
3002 alg = atoi(sstrsep(&p, sep));
3003 sstrsep(&p, sep);
3004 for (j = 0; j < SIZE_NUM; ++j)
3005 results[alg][j] += atof(sstrsep(&p, sep));
86885c28 3006 } else if (strncmp(buf, "+F2:", 4) == 0) {
0f113f3e
MC
3007 int k;
3008 double d;
3009
3010 p = buf + 4;
3011 k = atoi(sstrsep(&p, sep));
3012 sstrsep(&p, sep);
3013
0f113f3e
MC
3014 d = atof(sstrsep(&p, sep));
3015 if (n)
3016 rsa_results[k][0] = 1 / (1 / rsa_results[k][0] + 1 / d);
3017 else
3018 rsa_results[k][0] = d;
3019
3020 d = atof(sstrsep(&p, sep));
3021 if (n)
3022 rsa_results[k][1] = 1 / (1 / rsa_results[k][1] + 1 / d);
3023 else
3024 rsa_results[k][1] = d;
3025 }
a00ae6c4 3026# ifndef OPENSSL_NO_DSA
86885c28 3027 else if (strncmp(buf, "+F3:", 4) == 0) {
0f113f3e
MC
3028 int k;
3029 double d;
3030
3031 p = buf + 4;
3032 k = atoi(sstrsep(&p, sep));
3033 sstrsep(&p, sep);
3034
3035 d = atof(sstrsep(&p, sep));
3036 if (n)
3037 dsa_results[k][0] = 1 / (1 / dsa_results[k][0] + 1 / d);
3038 else
3039 dsa_results[k][0] = d;
3040
3041 d = atof(sstrsep(&p, sep));
3042 if (n)
3043 dsa_results[k][1] = 1 / (1 / dsa_results[k][1] + 1 / d);
3044 else
3045 dsa_results[k][1] = d;
3046 }
a00ae6c4 3047# endif
10bf4fc2 3048# ifndef OPENSSL_NO_EC
86885c28 3049 else if (strncmp(buf, "+F4:", 4) == 0) {
0f113f3e
MC
3050 int k;
3051 double d;
3052
3053 p = buf + 4;
3054 k = atoi(sstrsep(&p, sep));
3055 sstrsep(&p, sep);
3056
3057 d = atof(sstrsep(&p, sep));
3058 if (n)
29dd15b1 3059 ecdsa_results[k][0] = 1 / (1 / ecdsa_results[k][0] + 1 / d);
0f113f3e
MC
3060 else
3061 ecdsa_results[k][0] = d;
3062
3063 d = atof(sstrsep(&p, sep));
3064 if (n)
29dd15b1 3065 ecdsa_results[k][1] = 1 / (1 / ecdsa_results[k][1] + 1 / d);
0f113f3e
MC
3066 else
3067 ecdsa_results[k][1] = d;
d6073e27 3068 } else if (strncmp(buf, "+F5:", 4) == 0) {
0f113f3e
MC
3069 int k;
3070 double d;
3071
3072 p = buf + 4;
3073 k = atoi(sstrsep(&p, sep));
3074 sstrsep(&p, sep);
3075
3076 d = atof(sstrsep(&p, sep));
3077 if (n)
3078 ecdh_results[k][0] = 1 / (1 / ecdh_results[k][0] + 1 / d);
3079 else
3080 ecdh_results[k][0] = d;
3081
3082 }
a00ae6c4 3083# endif
0f113f3e 3084
86885c28 3085 else if (strncmp(buf, "+H:", 3) == 0) {
7e1b7485 3086 ;
0f113f3e 3087 } else
29dd15b1
NT
3088 BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
3089 n);
0f113f3e
MC
3090 }
3091
3092 fclose(f);
3093 }
3094 free(fds);
3095 return 1;
3096}
a00ae6c4 3097#endif
375a64e3
AP
3098
3099static void multiblock_speed(const EVP_CIPHER *evp_cipher)
0f113f3e
MC
3100{
3101 static int mblengths[] =
3102 { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
8b0b80d9 3103 int j, count, num = OSSL_NELEM(mblengths);
0f113f3e
MC
3104 const char *alg_name;
3105 unsigned char *inp, *out, no_key[32], no_iv[16];
846ec07d 3106 EVP_CIPHER_CTX *ctx;
0f113f3e
MC
3107 double d = 0.0;
3108
68dc6824
RS
3109 inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
3110 out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
846ec07d
RL
3111 ctx = EVP_CIPHER_CTX_new();
3112 EVP_EncryptInit_ex(ctx, evp_cipher, NULL, no_key, no_iv);
29dd15b1 3113 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key), no_key);
6c2ff56e 3114 alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
0f113f3e
MC
3115
3116 for (j = 0; j < num; j++) {
3117 print_message(alg_name, 0, mblengths[j]);
3118 Time_F(START);
3119 for (count = 0, run = 1; run && count < 0x7fffffff; count++) {
c8269881 3120 unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
0f113f3e
MC
3121 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
3122 size_t len = mblengths[j];
3123 int packlen;
3124
3125 memset(aad, 0, 8); /* avoid uninitialized values */
3126 aad[8] = 23; /* SSL3_RT_APPLICATION_DATA */
3127 aad[9] = 3; /* version */
3128 aad[10] = 2;
3129 aad[11] = 0; /* length */
3130 aad[12] = 0;
3131 mb_param.out = NULL;
3132 mb_param.inp = aad;
3133 mb_param.len = len;
3134 mb_param.interleave = 8;
3135
846ec07d 3136 packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
0f113f3e
MC
3137 sizeof(mb_param), &mb_param);
3138
3139 if (packlen > 0) {
3140 mb_param.out = out;
3141 mb_param.inp = inp;
3142 mb_param.len = len;
846ec07d 3143 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
0f113f3e
MC
3144 sizeof(mb_param), &mb_param);
3145 } else {
3146 int pad;
3147
3148 RAND_bytes(out, 16);
3149 len += 16;
3150 aad[11] = len >> 8;
3151 aad[12] = len;
846ec07d 3152 pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
c8269881 3153 EVP_AEAD_TLS1_AAD_LEN, aad);
846ec07d 3154 EVP_Cipher(ctx, out, inp, len + pad);
0f113f3e
MC
3155 }
3156 }
3157 d = Time_F(STOP);
7e1b7485 3158 BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
0f113f3e
MC
3159 : "%d %s's in %.2fs\n", count, "evp", d);
3160 results[D_EVP][j] = ((double)count) / d * mblengths[j];
3161 }
3162
3163 if (mr) {
3164 fprintf(stdout, "+H");
3165 for (j = 0; j < num; j++)
3166 fprintf(stdout, ":%d", mblengths[j]);
3167 fprintf(stdout, "\n");
3168 fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
3169 for (j = 0; j < num; j++)
3170 fprintf(stdout, ":%.2f", results[D_EVP][j]);
3171 fprintf(stdout, "\n");
3172 } else {
3173 fprintf(stdout,
3174 "The 'numbers' are in 1000s of bytes per second processed.\n");
3175 fprintf(stdout, "type ");
3176 for (j = 0; j < num; j++)
3177 fprintf(stdout, "%7d bytes", mblengths[j]);
3178 fprintf(stdout, "\n");
3179 fprintf(stdout, "%-24s", alg_name);
3180
3181 for (j = 0; j < num; j++) {
3182 if (results[D_EVP][j] > 10000)
3183 fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
3184 else
3185 fprintf(stdout, " %11.2f ", results[D_EVP][j]);
3186 }
3187 fprintf(stdout, "\n");
3188 }
3189
b548a1f1
RS
3190 OPENSSL_free(inp);
3191 OPENSSL_free(out);
846ec07d 3192 EVP_CIPHER_CTX_free(ctx);
0f113f3e 3193}