]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/speed.c
CRYPTO_gcm128_decrypt: fix mac or tag calculation
[thirdparty/openssl.git] / apps / speed.c
CommitLineData
846e33c7 1/*
4333b89f 2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
aa8f3d76 3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
d02b48c6 4 *
dffa7520 5 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
d02b48c6 9 */
846e33c7 10
a00ae6c4
RS
11#undef SECONDS
12#define SECONDS 3
a00ae6c4
RS
13#define RSA_SECONDS 10
14#define DSA_SECONDS 10
15#define ECDSA_SECONDS 10
16#define ECDH_SECONDS 10
d3a9fb10 17#define EdDSA_SECONDS 10
a56f68ad 18#define SM2_SECONDS 10
60d3b5b9 19#define FFDH_SECONDS 10
a00ae6c4 20
7573fe1a
MC
21/* We need to use some deprecated APIs */
22#define OPENSSL_SUPPRESS_DEPRECATED
23
a00ae6c4
RS
24#include <stdio.h>
25#include <stdlib.h>
a00ae6c4
RS
26#include <string.h>
27#include <math.h>
28#include "apps.h"
dab2cd68 29#include "progs.h"
a00ae6c4
RS
30#include <openssl/crypto.h>
31#include <openssl/rand.h>
32#include <openssl/err.h>
33#include <openssl/evp.h>
34#include <openssl/objects.h>
8b0b80d9 35#include <openssl/async.h>
a00ae6c4 36#if !defined(OPENSSL_SYS_MSDOS)
6b10d29c 37# include <unistd.h>
a00ae6c4 38#endif
d02b48c6 39
08073700
RB
40#if defined(__TANDEM)
41# if defined(OPENSSL_TANDEM_FLOSS)
42# include <floss.h(floss_fork)>
43# endif
44#endif
45
8d35ceb9 46#if defined(_WIN32)
a00ae6c4 47# include <windows.h>
a00ae6c4 48#endif
d02b48c6 49
a00ae6c4
RS
50#include <openssl/bn.h>
51#ifndef OPENSSL_NO_DES
52# include <openssl/des.h>
53#endif
c72fa255 54#ifndef OPENSSL_NO_DEPRECATED_3_0
5158c763 55#include <openssl/aes.h>
c72fa255 56#endif
a00ae6c4
RS
57#ifndef OPENSSL_NO_CAMELLIA
58# include <openssl/camellia.h>
59#endif
60#ifndef OPENSSL_NO_MD2
61# include <openssl/md2.h>
62#endif
63#ifndef OPENSSL_NO_MDC2
64# include <openssl/mdc2.h>
65#endif
66#ifndef OPENSSL_NO_MD4
67# include <openssl/md4.h>
68#endif
69#ifndef OPENSSL_NO_MD5
70# include <openssl/md5.h>
71#endif
7e1b7485 72#include <openssl/hmac.h>
9bba2c4c
BE
73#ifndef OPENSSL_NO_CMAC
74#include <openssl/cmac.h>
75#endif
7e1b7485 76#include <openssl/sha.h>
a00ae6c4
RS
77#ifndef OPENSSL_NO_RMD160
78# include <openssl/ripemd.h>
79#endif
80#ifndef OPENSSL_NO_WHIRLPOOL
81# include <openssl/whrlpool.h>
82#endif
83#ifndef OPENSSL_NO_RC4
84# include <openssl/rc4.h>
85#endif
86#ifndef OPENSSL_NO_RC5
87# include <openssl/rc5.h>
88#endif
89#ifndef OPENSSL_NO_RC2
90# include <openssl/rc2.h>
91#endif
92#ifndef OPENSSL_NO_IDEA
93# include <openssl/idea.h>
94#endif
95#ifndef OPENSSL_NO_SEED
96# include <openssl/seed.h>
97#endif
98#ifndef OPENSSL_NO_BF
99# include <openssl/blowfish.h>
100#endif
101#ifndef OPENSSL_NO_CAST
102# include <openssl/cast.h>
103#endif
3a1ee3c1 104#ifndef OPENSSL_NO_DEPRECATED_3_0
a00ae6c4
RS
105# include <openssl/rsa.h>
106# include "./testrsa.h"
107#endif
60d3b5b9
HK
108#ifndef OPENSSL_NO_DH
109# include <openssl/dh.h>
110#endif
a00ae6c4 111#include <openssl/x509.h>
1ddf2594 112#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
a00ae6c4
RS
113# include <openssl/dsa.h>
114# include "./testdsa.h"
115#endif
10bf4fc2 116#ifndef OPENSSL_NO_EC
fb29bb59 117# include <openssl/ec.h>
a00ae6c4
RS
118#endif
119#include <openssl/modes.h>
b5419b81 120
a00ae6c4 121#ifndef HAVE_FORK
5c8b7b4c 122# if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_VXWORKS)
a00ae6c4 123# define HAVE_FORK 0
0f113f3e 124# else
a00ae6c4 125# define HAVE_FORK 1
0f113f3e 126# endif
a00ae6c4 127#endif
66d3e748 128
a00ae6c4
RS
129#if HAVE_FORK
130# undef NO_FORK
131#else
132# define NO_FORK
133#endif
134
a00ae6c4 135#define MAX_MISALIGNMENT 63
0ff43435
AG
136#define MAX_ECDH_SIZE 256
137#define MISALIGN 64
60d3b5b9 138#define MAX_FFDH_SIZE 1024
0ff43435 139
8f26f9d5 140typedef struct openssl_speed_sec_st {
64daf14d
PS
141 int sym;
142 int rsa;
143 int dsa;
144 int ecdsa;
145 int ecdh;
d3a9fb10 146 int eddsa;
a56f68ad 147 int sm2;
60d3b5b9 148 int ffdh;
8f26f9d5 149} openssl_speed_sec_t;
64daf14d 150
0f113f3e 151static volatile int run = 0;
d02b48c6 152
1352e0ff 153static int mr = 0; /* machine-readeable output format to merge fork results */
0f113f3e 154static int usertime = 1;
7876e448 155
0e211563 156static double Time_F(int s);
64daf14d 157static void print_message(const char *s, long num, int length, int tm);
3a1ee3c1
RL
158#if !defined(OPENSSL_NO_DEPRECATED_3_0) \
159 || !defined(OPENSSL_NO_DSA) \
160 || !defined(OPENSSL_NO_DH) \
161 || !defined(OPENSSL_NO_EC)
689c6f25 162static void pkey_print_message(const char *str, const char *str2,
48bc0d99 163 long num, unsigned int bits, int sec);
8c885145 164#endif
0f113f3e 165static void print_result(int alg, int run_no, int count, double time_used);
a00ae6c4 166#ifndef NO_FORK
64daf14d 167static int do_multi(int multi, int size_num);
a00ae6c4 168#endif
0f113f3e 169
64daf14d
PS
170static const int lengths_list[] = {
171 16, 64, 256, 1024, 8 * 1024, 16 * 1024
172};
1352e0ff 173#define SIZE_NUM OSSL_NELEM(lengths_list)
64daf14d
PS
174static const int *lengths = lengths_list;
175
44ca7565
AP
176static const int aead_lengths_list[] = {
177 2, 31, 136, 1024, 8 * 1024, 16 * 1024
178};
179
ffcca684
AP
180#define START 0
181#define STOP 1
182
a00ae6c4 183#ifdef SIGALRM
b83eddc5 184
ffcca684 185static void alarmed(int sig)
0f113f3e 186{
ffcca684 187 signal(SIGALRM, alarmed);
0f113f3e
MC
188 run = 0;
189}
d02b48c6 190
ffcca684
AP
191static double Time_F(int s)
192{
193 double ret = app_tminterval(s, usertime);
194 if (s == STOP)
195 alarm(0);
196 return ret;
197}
d02b48c6 198
ffcca684
AP
199#elif defined(_WIN32)
200
201# define SIGALRM -1
4d8743f4 202
e0de4dd5
XL
203static unsigned int lapse;
204static volatile unsigned int schlock;
0f113f3e
MC
205static void alarm_win32(unsigned int secs)
206{
207 lapse = secs * 1000;
208}
4d8743f4 209
a00ae6c4 210# define alarm alarm_win32
0f113f3e
MC
211
212static DWORD WINAPI sleepy(VOID * arg)
213{
214 schlock = 1;
215 Sleep(lapse);
216 run = 0;
217 return 0;
218}
4e74239c 219
0a39d8f2 220static double Time_F(int s)
0f113f3e
MC
221{
222 double ret;
223 static HANDLE thr;
224
225 if (s == START) {
226 schlock = 0;
227 thr = CreateThread(NULL, 4096, sleepy, NULL, 0, NULL);
228 if (thr == NULL) {
db40a14e
AP
229 DWORD err = GetLastError();
230 BIO_printf(bio_err, "unable to CreateThread (%lu)", err);
f219a1b0 231 ExitProcess(err);
0f113f3e
MC
232 }
233 while (!schlock)
234 Sleep(0); /* scheduler spinlock */
235 ret = app_tminterval(s, usertime);
236 } else {
237 ret = app_tminterval(s, usertime);
238 if (run)
239 TerminateThread(thr, 0);
240 CloseHandle(thr);
241 }
242
243 return ret;
244}
a00ae6c4 245#else
0f113f3e
MC
246static double Time_F(int s)
247{
ffcca684 248 return app_tminterval(s, usertime);
0f113f3e 249}
a00ae6c4 250#endif
176f31dd 251
5c6a69f5 252static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
8f26f9d5 253 const openssl_speed_sec_t *seconds);
176f31dd 254
5c6a69f5
F
255static int opt_found(const char *name, unsigned int *result,
256 const OPT_PAIR pairs[], unsigned int nbelem)
7e1b7485 257{
5c6a69f5
F
258 unsigned int idx;
259
260 for (idx = 0; idx < nbelem; ++idx, pairs++)
7e1b7485
RS
261 if (strcmp(name, pairs->name) == 0) {
262 *result = pairs->retval;
263 return 1;
264 }
265 return 0;
266}
1352e0ff
F
267#define opt_found(value, pairs, result)\
268 opt_found(value, result, pairs, OSSL_NELEM(pairs))
7e1b7485
RS
269
270typedef enum OPTION_choice {
271 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
f88b9b79 272 OPT_ELAPSED, OPT_EVP, OPT_HMAC, OPT_DECRYPT, OPT_ENGINE, OPT_MULTI,
6bd4e3f2 273 OPT_MR, OPT_MB, OPT_MISALIGN, OPT_ASYNCJOBS, OPT_R_ENUM, OPT_PROV_ENUM,
9bba2c4c 274 OPT_PRIMES, OPT_SECONDS, OPT_BYTES, OPT_AEAD, OPT_CMAC
7e1b7485
RS
275} OPTION_CHOICE;
276
44c83ebd 277const OPTIONS speed_options[] = {
92de469f 278 {OPT_HELP_STR, 1, '-', "Usage: %s [options] [algorithm...]\n"},
5388f986
RS
279
280 OPT_SECTION("General"),
7e1b7485 281 {"help", OPT_HELP, '-', "Display this summary"},
700b8145 282 {"mb", OPT_MB, '-',
44ca7565
AP
283 "Enable (tls1>=1) multi-block mode on EVP-named cipher"},
284 {"mr", OPT_MR, '-', "Produce machine readable output"},
7e1b7485
RS
285#ifndef NO_FORK
286 {"multi", OPT_MULTI, 'p', "Run benchmarks in parallel"},
287#endif
667867cc 288#ifndef OPENSSL_NO_ASYNC
d6073e27 289 {"async_jobs", OPT_ASYNCJOBS, 'p',
44ca7565 290 "Enable async mode and start specified number of jobs"},
8b0b80d9 291#endif
7e1b7485
RS
292#ifndef OPENSSL_NO_ENGINE
293 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
294#endif
5388f986
RS
295 {"primes", OPT_PRIMES, 'p', "Specify number of primes (for RSA only)"},
296
297 OPT_SECTION("Selection"),
298 {"evp", OPT_EVP, 's', "Use EVP-named cipher or digest"},
dbde4726 299#ifndef OPENSSL_NO_DEPRECATED_3_0
5388f986 300 {"hmac", OPT_HMAC, 's', "HMAC using EVP-named digest"},
dbde4726 301#endif
a6d572e6 302#if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
5388f986
RS
303 {"cmac", OPT_CMAC, 's', "CMAC using EVP-named cipher"},
304#endif
305 {"decrypt", OPT_DECRYPT, '-',
306 "Time decryption instead of encryption (only EVP)"},
307 {"aead", OPT_AEAD, '-',
308 "Benchmark EVP-named AEAD cipher in TLS-like sequence"},
309
310 OPT_SECTION("Timing"),
44ca7565
AP
311 {"elapsed", OPT_ELAPSED, '-',
312 "Use wall-clock time instead of CPU user time as divisor"},
64daf14d 313 {"seconds", OPT_SECONDS, 'p',
44ca7565 314 "Run benchmarks for specified amount of seconds"},
64daf14d 315 {"bytes", OPT_BYTES, 'p',
44ca7565
AP
316 "Run [non-PKI] benchmarks on custom-sized buffer"},
317 {"misalign", OPT_MISALIGN, 'p',
318 "Use specified offset to mis-align buffers"},
5388f986
RS
319
320 OPT_R_OPTIONS,
6bd4e3f2 321 OPT_PROV_OPTIONS,
92de469f
RS
322
323 OPT_PARAMETERS(),
324 {"algorithm", 0, 0, "Algorithm(s) to test (optional; otherwise tests all)"},
5c6a69f5 325 {NULL}
7e1b7485
RS
326};
327
1352e0ff
F
328enum {
329 D_MD2, D_MDC2, D_MD4, D_MD5 , D_HMAC, D_SHA1, D_RMD160, D_RC4,
330 D_CBC_DES, D_EDE3_DES, D_CBC_IDEA, D_CBC_SEED,
331 D_CBC_RC2, D_CBC_RC5, D_CBC_BF, D_CBC_CAST,
332 D_CBC_128_AES, D_CBC_192_AES, D_CBC_256_AES,
333 D_CBC_128_CML, D_CBC_192_CML, D_CBC_256_CML,
334 D_EVP, D_SHA256, D_SHA512, D_WHIRLPOOL,
335 D_IGE_128_AES, D_IGE_192_AES, D_IGE_256_AES,
336 D_GHASH, D_RAND, D_EVP_HMAC, D_EVP_CMAC, ALGOR_NUM
337};
338/* name of algorithms to test. MUST BE KEEP IN SYNC with above enum ! */
339static const char *names[ALGOR_NUM] = {
5c6a69f5
F
340 "md2", "mdc2", "md4", "md5", "hmac(md5)", "sha1", "rmd160", "rc4",
341 "des cbc", "des ede3", "idea cbc", "seed cbc",
342 "rc2 cbc", "rc5-32/12 cbc", "blowfish cbc", "cast cbc",
343 "aes-128 cbc", "aes-192 cbc", "aes-256 cbc",
344 "camellia-128 cbc", "camellia-192 cbc", "camellia-256 cbc",
345 "evp", "sha256", "sha512", "whirlpool",
346 "aes-128 ige", "aes-192 ige", "aes-256 ige", "ghash",
9bba2c4c 347 "rand", "hmac", "cmac"
5c6a69f5 348};
5c6a69f5 349
1352e0ff 350/* list of configured algorithm (remaining), with some few alias */
5c6a69f5 351static const OPT_PAIR doit_choices[] = {
58e1f3d6 352#if !defined(OPENSSL_NO_MD2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
7e1b7485
RS
353 {"md2", D_MD2},
354#endif
33ee9ae0 355#if !defined(OPENSSL_NO_MDC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
7e1b7485
RS
356 {"mdc2", D_MDC2},
357#endif
8ffb20ce 358#if !defined(OPENSSL_NO_MD4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
7e1b7485
RS
359 {"md4", D_MD4},
360#endif
781aa7ab 361#if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
7e1b7485 362 {"md5", D_MD5},
dbde4726 363# ifndef OPENSSL_NO_DEPRECATED_3_0
7e1b7485 364 {"hmac", D_HMAC},
dbde4726 365# endif
7e1b7485 366#endif
85d843c8 367#ifndef OPENSSL_NO_DEPRECATED_3_0
7e1b7485
RS
368 {"sha1", D_SHA1},
369 {"sha256", D_SHA256},
370 {"sha512", D_SHA512},
85d843c8 371#endif
7c3aa39f 372#if !defined(OPENSSL_NO_WHIRLPOOL) && !defined(OPENSSL_NO_DEPRECATED_3_0)
7e1b7485
RS
373 {"whirlpool", D_WHIRLPOOL},
374#endif
601fca17 375#if !defined(OPENSSL_NO_RMD160) && !defined(OPENSSL_NO_DEPRECATED_3_0)
7e1b7485
RS
376 {"ripemd", D_RMD160},
377 {"rmd160", D_RMD160},
378 {"ripemd160", D_RMD160},
379#endif
a8fca728 380#if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
7e1b7485
RS
381 {"rc4", D_RC4},
382#endif
c6fec81b 383#if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
7e1b7485
RS
384 {"des-cbc", D_CBC_DES},
385 {"des-ede3", D_EDE3_DES},
386#endif
c72fa255 387#ifndef OPENSSL_NO_DEPRECATED_3_0
7e1b7485
RS
388 {"aes-128-cbc", D_CBC_128_AES},
389 {"aes-192-cbc", D_CBC_192_AES},
390 {"aes-256-cbc", D_CBC_256_AES},
391 {"aes-128-ige", D_IGE_128_AES},
392 {"aes-192-ige", D_IGE_192_AES},
393 {"aes-256-ige", D_IGE_256_AES},
fd367b4c 394#endif
ee2993ab 395#if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
7e1b7485
RS
396 {"rc2-cbc", D_CBC_RC2},
397 {"rc2", D_CBC_RC2},
398#endif
62c3fed0 399#if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
7e1b7485
RS
400 {"rc5-cbc", D_CBC_RC5},
401 {"rc5", D_CBC_RC5},
402#endif
da2d32f6 403#if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
7e1b7485
RS
404 {"idea-cbc", D_CBC_IDEA},
405 {"idea", D_CBC_IDEA},
406#endif
28c690cb 407#if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
7e1b7485
RS
408 {"seed-cbc", D_CBC_SEED},
409 {"seed", D_CBC_SEED},
410#endif
03047e7b 411#if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
7e1b7485
RS
412 {"bf-cbc", D_CBC_BF},
413 {"blowfish", D_CBC_BF},
414 {"bf", D_CBC_BF},
415#endif
0ae5d4d6 416#if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
7e1b7485
RS
417 {"cast-cbc", D_CBC_CAST},
418 {"cast", D_CBC_CAST},
419 {"cast5", D_CBC_CAST},
420#endif
421 {"ghash", D_GHASH},
5c6a69f5 422 {"rand", D_RAND}
7e1b7485
RS
423};
424
1352e0ff 425static double results[ALGOR_NUM][SIZE_NUM];
5c6a69f5 426
1ddf2594 427#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
1352e0ff
F
428enum { R_DSA_512, R_DSA_1024, R_DSA_2048, DSA_NUM };
429static const OPT_PAIR dsa_choices[DSA_NUM] = {
7e1b7485
RS
430 {"dsa512", R_DSA_512},
431 {"dsa1024", R_DSA_1024},
5c6a69f5 432 {"dsa2048", R_DSA_2048}
7e1b7485 433};
5c6a69f5
F
434static double dsa_results[DSA_NUM][2]; /* 2 ops: sign then verify */
435#endif /* OPENSSL_NO_DSA */
667ac4ec 436
3a1ee3c1 437#ifndef OPENSSL_NO_DEPRECATED_3_0
1352e0ff
F
438enum {
439 R_RSA_512, R_RSA_1024, R_RSA_2048, R_RSA_3072, R_RSA_4096, R_RSA_7680,
440 R_RSA_15360, RSA_NUM
441};
442static const OPT_PAIR rsa_choices[RSA_NUM] = {
7e1b7485
RS
443 {"rsa512", R_RSA_512},
444 {"rsa1024", R_RSA_1024},
445 {"rsa2048", R_RSA_2048},
446 {"rsa3072", R_RSA_3072},
447 {"rsa4096", R_RSA_4096},
448 {"rsa7680", R_RSA_7680},
5c6a69f5 449 {"rsa15360", R_RSA_15360}
7e1b7485 450};
5c6a69f5
F
451
452static double rsa_results[RSA_NUM][2]; /* 2 ops: sign then verify */
3a1ee3c1 453#endif /* OPENSSL_NO_DEPRECATED_3_0 */
7e1b7485 454
60d3b5b9
HK
455#ifndef OPENSSL_NO_DH
456enum ff_params_t {
457 R_FFDH_2048, R_FFDH_3072, R_FFDH_4096, R_FFDH_6144, R_FFDH_8192, FFDH_NUM
458};
459
460static const OPT_PAIR ffdh_choices[FFDH_NUM] = {
461 {"ffdh2048", R_FFDH_2048},
462 {"ffdh3072", R_FFDH_3072},
463 {"ffdh4096", R_FFDH_4096},
464 {"ffdh6144", R_FFDH_6144},
465 {"ffdh8192", R_FFDH_8192},
466};
467
468static double ffdh_results[FFDH_NUM][1]; /* 1 op: derivation */
469#endif /* OPENSSL_NO_DH */
470
f5349f8c 471#ifndef OPENSSL_NO_EC
1352e0ff
F
472enum ec_curves_t {
473 R_EC_P160, R_EC_P192, R_EC_P224, R_EC_P256, R_EC_P384, R_EC_P521,
474# ifndef OPENSSL_NO_EC2M
475 R_EC_K163, R_EC_K233, R_EC_K283, R_EC_K409, R_EC_K571,
476 R_EC_B163, R_EC_B233, R_EC_B283, R_EC_B409, R_EC_B571,
477# endif
478 R_EC_BRP256R1, R_EC_BRP256T1, R_EC_BRP384R1, R_EC_BRP384T1,
479 R_EC_BRP512R1, R_EC_BRP512T1, ECDSA_NUM
480};
481/* list of ecdsa curves */
482static const OPT_PAIR ecdsa_choices[ECDSA_NUM] = {
7e1b7485
RS
483 {"ecdsap160", R_EC_P160},
484 {"ecdsap192", R_EC_P192},
485 {"ecdsap224", R_EC_P224},
486 {"ecdsap256", R_EC_P256},
487 {"ecdsap384", R_EC_P384},
488 {"ecdsap521", R_EC_P521},
f5c99167 489# ifndef OPENSSL_NO_EC2M
7e1b7485
RS
490 {"ecdsak163", R_EC_K163},
491 {"ecdsak233", R_EC_K233},
492 {"ecdsak283", R_EC_K283},
493 {"ecdsak409", R_EC_K409},
494 {"ecdsak571", R_EC_K571},
495 {"ecdsab163", R_EC_B163},
496 {"ecdsab233", R_EC_B233},
497 {"ecdsab283", R_EC_B283},
498 {"ecdsab409", R_EC_B409},
1c534560 499 {"ecdsab571", R_EC_B571},
f5c99167 500# endif
1c534560
F
501 {"ecdsabrp256r1", R_EC_BRP256R1},
502 {"ecdsabrp256t1", R_EC_BRP256T1},
503 {"ecdsabrp384r1", R_EC_BRP384R1},
504 {"ecdsabrp384t1", R_EC_BRP384T1},
505 {"ecdsabrp512r1", R_EC_BRP512R1},
506 {"ecdsabrp512t1", R_EC_BRP512T1}
7e1b7485 507};
1352e0ff
F
508enum { R_EC_X25519 = ECDSA_NUM, R_EC_X448, EC_NUM };
509/* list of ecdh curves, extension of |ecdsa_choices| list above */
510static const OPT_PAIR ecdh_choices[EC_NUM] = {
7e1b7485
RS
511 {"ecdhp160", R_EC_P160},
512 {"ecdhp192", R_EC_P192},
513 {"ecdhp224", R_EC_P224},
514 {"ecdhp256", R_EC_P256},
515 {"ecdhp384", R_EC_P384},
516 {"ecdhp521", R_EC_P521},
f5c99167 517# ifndef OPENSSL_NO_EC2M
7e1b7485
RS
518 {"ecdhk163", R_EC_K163},
519 {"ecdhk233", R_EC_K233},
520 {"ecdhk283", R_EC_K283},
521 {"ecdhk409", R_EC_K409},
522 {"ecdhk571", R_EC_K571},
523 {"ecdhb163", R_EC_B163},
524 {"ecdhb233", R_EC_B233},
525 {"ecdhb283", R_EC_B283},
526 {"ecdhb409", R_EC_B409},
527 {"ecdhb571", R_EC_B571},
f5c99167 528# endif
1c534560
F
529 {"ecdhbrp256r1", R_EC_BRP256R1},
530 {"ecdhbrp256t1", R_EC_BRP256T1},
531 {"ecdhbrp384r1", R_EC_BRP384R1},
532 {"ecdhbrp384t1", R_EC_BRP384T1},
533 {"ecdhbrp512r1", R_EC_BRP512R1},
534 {"ecdhbrp512t1", R_EC_BRP512T1},
db50c1da 535 {"ecdhx25519", R_EC_X25519},
5c6a69f5 536 {"ecdhx448", R_EC_X448}
7e1b7485 537};
5c6a69f5 538
1352e0ff
F
539static double ecdh_results[EC_NUM][1]; /* 1 op: derivation */
540static double ecdsa_results[ECDSA_NUM][2]; /* 2 ops: sign then verify */
d3a9fb10 541
1352e0ff
F
542enum { R_EC_Ed25519, R_EC_Ed448, EdDSA_NUM };
543static const OPT_PAIR eddsa_choices[EdDSA_NUM] = {
d3a9fb10
PY
544 {"ed25519", R_EC_Ed25519},
545 {"ed448", R_EC_Ed448}
d3a9fb10 546
1352e0ff 547};
d3a9fb10 548static double eddsa_results[EdDSA_NUM][2]; /* 2 ops: sign then verify */
a56f68ad
PY
549
550# ifndef OPENSSL_NO_SM2
1352e0ff
F
551enum { R_EC_CURVESM2, SM2_NUM };
552static const OPT_PAIR sm2_choices[SM2_NUM] = {
a56f68ad
PY
553 {"curveSM2", R_EC_CURVESM2}
554};
555# define SM2_ID "TLSv1.3+GM+Cipher+Suite"
556# define SM2_ID_LEN sizeof("TLSv1.3+GM+Cipher+Suite") - 1
a56f68ad
PY
557static double sm2_results[SM2_NUM][2]; /* 2 ops: sign then verify */
558# endif /* OPENSSL_NO_SM2 */
5c6a69f5 559#endif /* OPENSSL_NO_EC */
7e1b7485 560
8b0b80d9
AG
561#ifndef SIGALRM
562# define COND(d) (count < (d))
563# define COUNT(d) (d)
564#else
19075d58 565# define COND(unused_cond) (run && count<0x7fffffff)
8b0b80d9 566# define COUNT(d) (count)
29dd15b1 567#endif /* SIGALRM */
8b0b80d9 568
5c6a69f5
F
569typedef struct loopargs_st {
570 ASYNC_JOB *inprogress_job;
571 ASYNC_WAIT_CTX *wait_ctx;
572 unsigned char *buf;
573 unsigned char *buf2;
574 unsigned char *buf_malloc;
575 unsigned char *buf2_malloc;
576 unsigned char *key;
577 unsigned int siglen;
52307f94 578 size_t sigsize;
3a1ee3c1 579#ifndef OPENSSL_NO_DEPRECATED_3_0
5c6a69f5
F
580 RSA *rsa_key[RSA_NUM];
581#endif
1ddf2594 582#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
5c6a69f5
F
583 DSA *dsa_key[DSA_NUM];
584#endif
585#ifndef OPENSSL_NO_EC
5b5eea4b 586# ifndef OPENSSL_NO_DEPRECATED_3_0
5c6a69f5 587 EC_KEY *ecdsa[ECDSA_NUM];
5b5eea4b 588# endif
5c6a69f5 589 EVP_PKEY_CTX *ecdh_ctx[EC_NUM];
d3a9fb10 590 EVP_MD_CTX *eddsa_ctx[EdDSA_NUM];
1154ffbf 591 EVP_MD_CTX *eddsa_ctx2[EdDSA_NUM];
a56f68ad
PY
592# ifndef OPENSSL_NO_SM2
593 EVP_MD_CTX *sm2_ctx[SM2_NUM];
594 EVP_MD_CTX *sm2_vfy_ctx[SM2_NUM];
595 EVP_PKEY *sm2_pkey[SM2_NUM];
596# endif
5c6a69f5
F
597 unsigned char *secret_a;
598 unsigned char *secret_b;
599 size_t outlen[EC_NUM];
60d3b5b9
HK
600#endif
601#ifndef OPENSSL_NO_DH
602 EVP_PKEY_CTX *ffdh_ctx[FFDH_NUM];
603 unsigned char *secret_ff_a;
604 unsigned char *secret_ff_b;
5c6a69f5
F
605#endif
606 EVP_CIPHER_CTX *ctx;
dbde4726 607#ifndef OPENSSL_NO_DEPRECATED_3_0
5c6a69f5 608 HMAC_CTX *hctx;
dbde4726 609#endif
a6d572e6 610#if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
9bba2c4c
BE
611 CMAC_CTX *cmac_ctx;
612#endif
5c6a69f5
F
613 GCM128_CONTEXT *gcm_ctx;
614} loopargs_t;
615static int run_benchmark(int async_jobs, int (*loop_function) (void *),
616 loopargs_t * loopargs);
617
618static unsigned int testnum;
8b0b80d9 619
70c4e156 620/* Nb of iterations to do per algorithm and key-size */
1352e0ff 621static long c[ALGOR_NUM][SIZE_NUM];
8b0b80d9 622
58e1f3d6 623#if !defined(OPENSSL_NO_MD2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
8b0b80d9
AG
624static int EVP_Digest_MD2_loop(void *args)
625{
29dd15b1 626 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9 627 unsigned char *buf = tempargs->buf;
0f113f3e 628 unsigned char md2[MD2_DIGEST_LENGTH];
8b0b80d9 629 int count;
8829ce30 630
d166ed8c 631 for (count = 0; COND(c[D_MD2][testnum]); count++) {
8829ce30 632 if (!EVP_Digest(buf, (size_t)lengths[testnum], md2, NULL, EVP_md2(),
29dd15b1 633 NULL))
d166ed8c
DSH
634 return -1;
635 }
8b0b80d9
AG
636 return count;
637}
a00ae6c4 638#endif
8b0b80d9 639
33ee9ae0 640#if !defined(OPENSSL_NO_MDC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
8b0b80d9
AG
641static int EVP_Digest_MDC2_loop(void *args)
642{
29dd15b1 643 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9 644 unsigned char *buf = tempargs->buf;
0f113f3e 645 unsigned char mdc2[MDC2_DIGEST_LENGTH];
8b0b80d9 646 int count;
8829ce30 647
d166ed8c 648 for (count = 0; COND(c[D_MDC2][testnum]); count++) {
8829ce30 649 if (!EVP_Digest(buf, (size_t)lengths[testnum], mdc2, NULL, EVP_mdc2(),
29dd15b1 650 NULL))
d166ed8c
DSH
651 return -1;
652 }
8b0b80d9
AG
653 return count;
654}
a00ae6c4 655#endif
8b0b80d9 656
8ffb20ce 657#if !defined(OPENSSL_NO_MD4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
8b0b80d9
AG
658static int EVP_Digest_MD4_loop(void *args)
659{
29dd15b1 660 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9 661 unsigned char *buf = tempargs->buf;
0f113f3e 662 unsigned char md4[MD4_DIGEST_LENGTH];
8b0b80d9 663 int count;
8829ce30 664
d166ed8c 665 for (count = 0; COND(c[D_MD4][testnum]); count++) {
8829ce30 666 if (!EVP_Digest(buf, (size_t)lengths[testnum], md4, NULL, EVP_md4(),
29dd15b1 667 NULL))
d166ed8c
DSH
668 return -1;
669 }
8b0b80d9
AG
670 return count;
671}
a00ae6c4 672#endif
8b0b80d9 673
781aa7ab 674#if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
8b0b80d9
AG
675static int MD5_loop(void *args)
676{
29dd15b1 677 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9 678 unsigned char *buf = tempargs->buf;
0f113f3e 679 unsigned char md5[MD5_DIGEST_LENGTH];
8b0b80d9
AG
680 int count;
681 for (count = 0; COND(c[D_MD5][testnum]); count++)
682 MD5(buf, lengths[testnum], md5);
683 return count;
684}
685
dbde4726 686# ifndef OPENSSL_NO_DEPRECATED_3_0
8b0b80d9
AG
687static int HMAC_loop(void *args)
688{
29dd15b1 689 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
690 unsigned char *buf = tempargs->buf;
691 HMAC_CTX *hctx = tempargs->hctx;
0f113f3e 692 unsigned char hmac[MD5_DIGEST_LENGTH];
8b0b80d9 693 int count;
8829ce30 694
8b0b80d9
AG
695 for (count = 0; COND(c[D_HMAC][testnum]); count++) {
696 HMAC_Init_ex(hctx, NULL, 0, NULL, NULL);
697 HMAC_Update(hctx, buf, lengths[testnum]);
8829ce30 698 HMAC_Final(hctx, hmac, NULL);
8b0b80d9
AG
699 }
700 return count;
701}
dbde4726 702# endif
a00ae6c4 703#endif
8b0b80d9 704
85d843c8 705#ifndef OPENSSL_NO_DEPRECATED_3_0
8b0b80d9
AG
706static int SHA1_loop(void *args)
707{
29dd15b1 708 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9 709 unsigned char *buf = tempargs->buf;
0f113f3e 710 unsigned char sha[SHA_DIGEST_LENGTH];
8b0b80d9
AG
711 int count;
712 for (count = 0; COND(c[D_SHA1][testnum]); count++)
713 SHA1(buf, lengths[testnum], sha);
714 return count;
715}
716
717static int SHA256_loop(void *args)
718{
29dd15b1 719 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9 720 unsigned char *buf = tempargs->buf;
0f113f3e 721 unsigned char sha256[SHA256_DIGEST_LENGTH];
8b0b80d9
AG
722 int count;
723 for (count = 0; COND(c[D_SHA256][testnum]); count++)
724 SHA256(buf, lengths[testnum], sha256);
725 return count;
726}
727
728static int SHA512_loop(void *args)
729{
29dd15b1 730 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9 731 unsigned char *buf = tempargs->buf;
0f113f3e 732 unsigned char sha512[SHA512_DIGEST_LENGTH];
8b0b80d9
AG
733 int count;
734 for (count = 0; COND(c[D_SHA512][testnum]); count++)
735 SHA512(buf, lengths[testnum], sha512);
736 return count;
737}
85d843c8 738#endif
8b0b80d9 739
7c3aa39f 740#if !defined(OPENSSL_NO_WHIRLPOOL) && !defined(OPENSSL_NO_DEPRECATED_3_0)
8b0b80d9
AG
741static int WHIRLPOOL_loop(void *args)
742{
29dd15b1 743 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9 744 unsigned char *buf = tempargs->buf;
0f113f3e 745 unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH];
8b0b80d9
AG
746 int count;
747 for (count = 0; COND(c[D_WHIRLPOOL][testnum]); count++)
748 WHIRLPOOL(buf, lengths[testnum], whirlpool);
749 return count;
750}
a00ae6c4 751#endif
8b0b80d9 752
601fca17 753#if !defined(OPENSSL_NO_RMD160) && !defined(OPENSSL_NO_DEPRECATED_3_0)
8b0b80d9
AG
754static int EVP_Digest_RMD160_loop(void *args)
755{
29dd15b1 756 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9 757 unsigned char *buf = tempargs->buf;
0f113f3e 758 unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
8b0b80d9 759 int count;
d166ed8c 760 for (count = 0; COND(c[D_RMD160][testnum]); count++) {
8829ce30 761 if (!EVP_Digest(buf, (size_t)lengths[testnum], &(rmd160[0]),
29dd15b1 762 NULL, EVP_ripemd160(), NULL))
d166ed8c
DSH
763 return -1;
764 }
8b0b80d9
AG
765 return count;
766}
a00ae6c4 767#endif
8b0b80d9 768
a8fca728 769#if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
8b0b80d9
AG
770static RC4_KEY rc4_ks;
771static int RC4_loop(void *args)
772{
29dd15b1 773 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
774 unsigned char *buf = tempargs->buf;
775 int count;
776 for (count = 0; COND(c[D_RC4][testnum]); count++)
8829ce30 777 RC4(&rc4_ks, (size_t)lengths[testnum], buf, buf);
8b0b80d9
AG
778 return count;
779}
780#endif
781
c6fec81b 782#if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
8b0b80d9 783static unsigned char DES_iv[8];
0609658f 784static DES_key_schedule sch[3];
8b0b80d9
AG
785static int DES_ncbc_encrypt_loop(void *args)
786{
29dd15b1 787 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
788 unsigned char *buf = tempargs->buf;
789 int count;
790 for (count = 0; COND(c[D_CBC_DES][testnum]); count++)
0609658f 791 DES_ncbc_encrypt(buf, buf, lengths[testnum], &sch[0],
29dd15b1 792 &DES_iv, DES_ENCRYPT);
8b0b80d9
AG
793 return count;
794}
795
796static int DES_ede3_cbc_encrypt_loop(void *args)
797{
29dd15b1 798 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
799 unsigned char *buf = tempargs->buf;
800 int count;
801 for (count = 0; COND(c[D_EDE3_DES][testnum]); count++)
802 DES_ede3_cbc_encrypt(buf, buf, lengths[testnum],
0609658f 803 &sch[0], &sch[1], &sch[2], &DES_iv, DES_ENCRYPT);
8b0b80d9
AG
804 return count;
805}
806#endif
807
5158c763 808#define MAX_BLOCK_SIZE 128
8b0b80d9
AG
809
810static unsigned char iv[2 * MAX_BLOCK_SIZE / 8];
c72fa255
MC
811
812#ifndef OPENSSL_NO_DEPRECATED_3_0
8b0b80d9
AG
813static AES_KEY aes_ks1, aes_ks2, aes_ks3;
814static int AES_cbc_128_encrypt_loop(void *args)
815{
29dd15b1 816 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
817 unsigned char *buf = tempargs->buf;
818 int count;
819 for (count = 0; COND(c[D_CBC_128_AES][testnum]); count++)
820 AES_cbc_encrypt(buf, buf,
29dd15b1 821 (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
8b0b80d9
AG
822 return count;
823}
824
825static int AES_cbc_192_encrypt_loop(void *args)
826{
29dd15b1 827 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
828 unsigned char *buf = tempargs->buf;
829 int count;
830 for (count = 0; COND(c[D_CBC_192_AES][testnum]); count++)
831 AES_cbc_encrypt(buf, buf,
29dd15b1 832 (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
8b0b80d9
AG
833 return count;
834}
835
836static int AES_cbc_256_encrypt_loop(void *args)
837{
29dd15b1 838 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
839 unsigned char *buf = tempargs->buf;
840 int count;
841 for (count = 0; COND(c[D_CBC_256_AES][testnum]); count++)
842 AES_cbc_encrypt(buf, buf,
29dd15b1 843 (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
8b0b80d9
AG
844 return count;
845}
846
847static int AES_ige_128_encrypt_loop(void *args)
848{
29dd15b1 849 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
850 unsigned char *buf = tempargs->buf;
851 unsigned char *buf2 = tempargs->buf2;
852 int count;
853 for (count = 0; COND(c[D_IGE_128_AES][testnum]); count++)
854 AES_ige_encrypt(buf, buf2,
29dd15b1 855 (size_t)lengths[testnum], &aes_ks1, iv, AES_ENCRYPT);
8b0b80d9
AG
856 return count;
857}
858
859static int AES_ige_192_encrypt_loop(void *args)
860{
29dd15b1 861 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
862 unsigned char *buf = tempargs->buf;
863 unsigned char *buf2 = tempargs->buf2;
864 int count;
865 for (count = 0; COND(c[D_IGE_192_AES][testnum]); count++)
866 AES_ige_encrypt(buf, buf2,
29dd15b1 867 (size_t)lengths[testnum], &aes_ks2, iv, AES_ENCRYPT);
8b0b80d9
AG
868 return count;
869}
870
871static int AES_ige_256_encrypt_loop(void *args)
872{
29dd15b1 873 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
874 unsigned char *buf = tempargs->buf;
875 unsigned char *buf2 = tempargs->buf2;
876 int count;
877 for (count = 0; COND(c[D_IGE_256_AES][testnum]); count++)
878 AES_ige_encrypt(buf, buf2,
29dd15b1 879 (size_t)lengths[testnum], &aes_ks3, iv, AES_ENCRYPT);
8b0b80d9
AG
880 return count;
881}
882
883static int CRYPTO_gcm128_aad_loop(void *args)
884{
29dd15b1 885 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
886 unsigned char *buf = tempargs->buf;
887 GCM128_CONTEXT *gcm_ctx = tempargs->gcm_ctx;
888 int count;
889 for (count = 0; COND(c[D_GHASH][testnum]); count++)
890 CRYPTO_gcm128_aad(gcm_ctx, buf, lengths[testnum]);
891 return count;
892}
c72fa255 893#endif
8b0b80d9 894
65e6b9a4
PS
895static int RAND_bytes_loop(void *args)
896{
897 loopargs_t *tempargs = *(loopargs_t **) args;
898 unsigned char *buf = tempargs->buf;
899 int count;
900
901 for (count = 0; COND(c[D_RAND][testnum]); count++)
902 RAND_bytes(buf, lengths[testnum]);
903 return count;
904}
905
8b0b80d9
AG
906static int decrypt = 0;
907static int EVP_Update_loop(void *args)
908{
29dd15b1 909 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
910 unsigned char *buf = tempargs->buf;
911 EVP_CIPHER_CTX *ctx = tempargs->ctx;
723a7c5a 912 int outl, count, rc;
d02b7e09 913
723a7c5a 914 if (decrypt) {
d02b7e09 915 for (count = 0; COND(c[D_EVP][testnum]); count++) {
723a7c5a 916 rc = EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
7da84e0f
PS
917 if (rc != 1) {
918 /* reset iv in case of counter overflow */
723a7c5a 919 EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
7da84e0f 920 }
723a7c5a
PS
921 }
922 } else {
d02b7e09 923 for (count = 0; COND(c[D_EVP][testnum]); count++) {
723a7c5a 924 rc = EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
7da84e0f
PS
925 if (rc != 1) {
926 /* reset iv in case of counter overflow */
723a7c5a 927 EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, -1);
7da84e0f 928 }
723a7c5a
PS
929 }
930 }
8b0b80d9
AG
931 if (decrypt)
932 EVP_DecryptFinal_ex(ctx, buf, &outl);
933 else
934 EVP_EncryptFinal_ex(ctx, buf, &outl);
935 return count;
936}
44ca7565 937
fe4f66d2
PS
938/*
939 * CCM does not support streaming. For the purpose of performance measurement,
940 * each message is encrypted using the same (key,iv)-pair. Do not use this
941 * code in your application.
942 */
943static int EVP_Update_loop_ccm(void *args)
944{
945 loopargs_t *tempargs = *(loopargs_t **) args;
946 unsigned char *buf = tempargs->buf;
947 EVP_CIPHER_CTX *ctx = tempargs->ctx;
948 int outl, count;
949 unsigned char tag[12];
d02b7e09 950
fe4f66d2 951 if (decrypt) {
d02b7e09 952 for (count = 0; COND(c[D_EVP][testnum]); count++) {
fe4f66d2 953 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag), tag);
7da84e0f
PS
954 /* reset iv */
955 EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
956 /* counter is reset on every update */
fe4f66d2 957 EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
fe4f66d2
PS
958 }
959 } else {
d02b7e09 960 for (count = 0; COND(c[D_EVP][testnum]); count++) {
7da84e0f 961 /* restore iv length field */
fe4f66d2 962 EVP_EncryptUpdate(ctx, NULL, &outl, NULL, lengths[testnum]);
7da84e0f 963 /* counter is reset on every update */
fe4f66d2 964 EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
fe4f66d2
PS
965 }
966 }
7da84e0f
PS
967 if (decrypt)
968 EVP_DecryptFinal_ex(ctx, buf, &outl);
969 else
970 EVP_EncryptFinal_ex(ctx, buf, &outl);
fe4f66d2
PS
971 return count;
972}
8b0b80d9 973
44ca7565
AP
974/*
975 * To make AEAD benchmarking more relevant perform TLS-like operations,
976 * 13-byte AAD followed by payload. But don't use TLS-formatted AAD, as
977 * payload length is not actually limited by 16KB...
978 */
979static int EVP_Update_loop_aead(void *args)
980{
981 loopargs_t *tempargs = *(loopargs_t **) args;
982 unsigned char *buf = tempargs->buf;
983 EVP_CIPHER_CTX *ctx = tempargs->ctx;
984 int outl, count;
985 unsigned char aad[13] = { 0xcc };
986 unsigned char faketag[16] = { 0xcc };
d02b7e09 987
44ca7565 988 if (decrypt) {
d02b7e09 989 for (count = 0; COND(c[D_EVP][testnum]); count++) {
44ca7565
AP
990 EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv);
991 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
992 sizeof(faketag), faketag);
993 EVP_DecryptUpdate(ctx, NULL, &outl, aad, sizeof(aad));
994 EVP_DecryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
995 EVP_DecryptFinal_ex(ctx, buf + outl, &outl);
996 }
997 } else {
d02b7e09 998 for (count = 0; COND(c[D_EVP][testnum]); count++) {
44ca7565
AP
999 EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv);
1000 EVP_EncryptUpdate(ctx, NULL, &outl, aad, sizeof(aad));
1001 EVP_EncryptUpdate(ctx, buf, &outl, buf, lengths[testnum]);
1002 EVP_EncryptFinal_ex(ctx, buf + outl, &outl);
1003 }
1004 }
1005 return count;
1006}
1007
128d25ba
DB
1008static EVP_MD *evp_md = NULL;
1009static int fetched_alg = 0;
1010
8b0b80d9
AG
1011static int EVP_Digest_loop(void *args)
1012{
29dd15b1 1013 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
1014 unsigned char *buf = tempargs->buf;
1015 unsigned char md[EVP_MAX_MD_SIZE];
1016 int count;
19075d58 1017
d02b7e09 1018 for (count = 0; COND(c[D_EVP][testnum]); count++) {
19075d58 1019 if (!EVP_Digest(buf, lengths[testnum], md, NULL, evp_md, NULL))
d166ed8c
DSH
1020 return -1;
1021 }
8b0b80d9
AG
1022 return count;
1023}
1024
dbde4726 1025#ifndef OPENSSL_NO_DEPRECATED_3_0
f88b9b79
P
1026static const EVP_MD *evp_hmac_md = NULL;
1027static char *evp_hmac_name = NULL;
1028static int EVP_HMAC_loop(void *args)
1029{
1030 loopargs_t *tempargs = *(loopargs_t **) args;
1031 unsigned char *buf = tempargs->buf;
1032 unsigned char no_key[32];
1033 int count;
f88b9b79 1034
d02b7e09 1035 for (count = 0; COND(c[D_EVP_HMAC][testnum]); count++) {
f88b9b79
P
1036 if (HMAC(evp_hmac_md, no_key, sizeof(no_key), buf, lengths[testnum],
1037 NULL, NULL) == NULL)
1038 return -1;
1039 }
1040 return count;
1041}
dbde4726 1042#endif
f88b9b79 1043
a6d572e6 1044#if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
9bba2c4c
BE
1045static const EVP_CIPHER *evp_cmac_cipher = NULL;
1046static char *evp_cmac_name = NULL;
1047
1048static int EVP_CMAC_loop(void *args)
1049{
1050 loopargs_t *tempargs = *(loopargs_t **) args;
1051 unsigned char *buf = tempargs->buf;
1052 CMAC_CTX *cmac_ctx = tempargs->cmac_ctx;
1053 static const char key[16] = "This is a key...";
1054 unsigned char mac[16];
1055 size_t len = sizeof(mac);
1056 int count;
9bba2c4c 1057
d02b7e09 1058 for (count = 0; COND(c[D_EVP_CMAC][testnum]); count++) {
9bba2c4c
BE
1059 if (!CMAC_Init(cmac_ctx, key, sizeof(key), evp_cmac_cipher, NULL)
1060 || !CMAC_Update(cmac_ctx, buf, lengths[testnum])
1061 || !CMAC_Final(cmac_ctx, mac, &len))
1062 return -1;
1063 }
1064 return count;
1065}
1066#endif
1067
3a1ee3c1 1068#ifndef OPENSSL_NO_DEPRECATED_3_0
70c4e156 1069static long rsa_c[RSA_NUM][2]; /* # RSA iteration test */
8b0b80d9
AG
1070
1071static int RSA_sign_loop(void *args)
1072{
29dd15b1 1073 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
1074 unsigned char *buf = tempargs->buf;
1075 unsigned char *buf2 = tempargs->buf2;
0930e07d 1076 unsigned int *rsa_num = &tempargs->siglen;
0ff43435 1077 RSA **rsa_key = tempargs->rsa_key;
8b0b80d9
AG
1078 int ret, count;
1079 for (count = 0; COND(rsa_c[testnum][0]); count++) {
0ff43435 1080 ret = RSA_sign(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
8b0b80d9
AG
1081 if (ret == 0) {
1082 BIO_printf(bio_err, "RSA sign failure\n");
1083 ERR_print_errors(bio_err);
1084 count = -1;
1085 break;
1086 }
1087 }
1088 return count;
1089}
1090
1091static int RSA_verify_loop(void *args)
1092{
29dd15b1 1093 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
1094 unsigned char *buf = tempargs->buf;
1095 unsigned char *buf2 = tempargs->buf2;
0930e07d 1096 unsigned int rsa_num = tempargs->siglen;
0ff43435 1097 RSA **rsa_key = tempargs->rsa_key;
8b0b80d9
AG
1098 int ret, count;
1099 for (count = 0; COND(rsa_c[testnum][1]); count++) {
29dd15b1
NT
1100 ret =
1101 RSA_verify(NID_md5_sha1, buf, 36, buf2, rsa_num, rsa_key[testnum]);
8b0b80d9
AG
1102 if (ret <= 0) {
1103 BIO_printf(bio_err, "RSA verify failure\n");
1104 ERR_print_errors(bio_err);
1105 count = -1;
1106 break;
1107 }
1108 }
1109 return count;
1110}
1111#endif
1112
60d3b5b9
HK
1113#ifndef OPENSSL_NO_DH
1114static long ffdh_c[FFDH_NUM][1];
1115
1116static int FFDH_derive_key_loop(void *args)
1117{
1118 loopargs_t *tempargs = *(loopargs_t **) args;
1119 EVP_PKEY_CTX *ffdh_ctx = tempargs->ffdh_ctx[testnum];
1120 unsigned char *derived_secret = tempargs->secret_ff_a;
1121 size_t outlen = MAX_FFDH_SIZE;
1122 int count;
1123
1124 for (count = 0; COND(ffdh_c[testnum][0]); count++)
1125 EVP_PKEY_derive(ffdh_ctx, derived_secret, &outlen);
1126
1127 return count;
1128}
1129#endif /* OPENSSL_NO_DH */
1130
1ddf2594 1131#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
8b0b80d9
AG
1132static long dsa_c[DSA_NUM][2];
1133static int DSA_sign_loop(void *args)
1134{
29dd15b1 1135 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
1136 unsigned char *buf = tempargs->buf;
1137 unsigned char *buf2 = tempargs->buf2;
0ff43435 1138 DSA **dsa_key = tempargs->dsa_key;
0930e07d 1139 unsigned int *siglen = &tempargs->siglen;
8b0b80d9
AG
1140 int ret, count;
1141 for (count = 0; COND(dsa_c[testnum][0]); count++) {
1142 ret = DSA_sign(0, buf, 20, buf2, siglen, dsa_key[testnum]);
1143 if (ret == 0) {
1144 BIO_printf(bio_err, "DSA sign failure\n");
1145 ERR_print_errors(bio_err);
0ff43435 1146 count = -1;
8b0b80d9
AG
1147 break;
1148 }
1149 }
1150 return count;
1151}
1152
1153static int DSA_verify_loop(void *args)
1154{
29dd15b1 1155 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9
AG
1156 unsigned char *buf = tempargs->buf;
1157 unsigned char *buf2 = tempargs->buf2;
0ff43435 1158 DSA **dsa_key = tempargs->dsa_key;
0930e07d 1159 unsigned int siglen = tempargs->siglen;
8b0b80d9
AG
1160 int ret, count;
1161 for (count = 0; COND(dsa_c[testnum][1]); count++) {
1162 ret = DSA_verify(0, buf, 20, buf2, siglen, dsa_key[testnum]);
1163 if (ret <= 0) {
1164 BIO_printf(bio_err, "DSA verify failure\n");
1165 ERR_print_errors(bio_err);
0ff43435 1166 count = -1;
8b0b80d9
AG
1167 break;
1168 }
1169 }
1170 return count;
1171}
1172#endif
1173
1174#ifndef OPENSSL_NO_EC
579422c8 1175# ifndef OPENSSL_NO_DEPRECATED_3_0
5c6a69f5 1176static long ecdsa_c[ECDSA_NUM][2];
8b0b80d9
AG
1177static int ECDSA_sign_loop(void *args)
1178{
29dd15b1 1179 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9 1180 unsigned char *buf = tempargs->buf;
0ff43435
AG
1181 EC_KEY **ecdsa = tempargs->ecdsa;
1182 unsigned char *ecdsasig = tempargs->buf2;
0930e07d 1183 unsigned int *ecdsasiglen = &tempargs->siglen;
8b0b80d9
AG
1184 int ret, count;
1185 for (count = 0; COND(ecdsa_c[testnum][0]); count++) {
29dd15b1 1186 ret = ECDSA_sign(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
8b0b80d9
AG
1187 if (ret == 0) {
1188 BIO_printf(bio_err, "ECDSA sign failure\n");
1189 ERR_print_errors(bio_err);
0ff43435 1190 count = -1;
8b0b80d9
AG
1191 break;
1192 }
1193 }
1194 return count;
1195}
1196
1197static int ECDSA_verify_loop(void *args)
1198{
29dd15b1 1199 loopargs_t *tempargs = *(loopargs_t **) args;
8b0b80d9 1200 unsigned char *buf = tempargs->buf;
0ff43435
AG
1201 EC_KEY **ecdsa = tempargs->ecdsa;
1202 unsigned char *ecdsasig = tempargs->buf2;
0930e07d 1203 unsigned int ecdsasiglen = tempargs->siglen;
8b0b80d9
AG
1204 int ret, count;
1205 for (count = 0; COND(ecdsa_c[testnum][1]); count++) {
29dd15b1 1206 ret = ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[testnum]);
8b0b80d9
AG
1207 if (ret != 1) {
1208 BIO_printf(bio_err, "ECDSA verify failure\n");
1209 ERR_print_errors(bio_err);
0ff43435 1210 count = -1;
8b0b80d9
AG
1211 break;
1212 }
1213 }
1214 return count;
1215}
579422c8 1216# endif
8b0b80d9 1217
19075d58 1218/* ******************************************************************** */
c5baa266
F
1219static long ecdh_c[EC_NUM][1];
1220
ed7377db
NT
1221static int ECDH_EVP_derive_key_loop(void *args)
1222{
1223 loopargs_t *tempargs = *(loopargs_t **) args;
ed7377db
NT
1224 EVP_PKEY_CTX *ctx = tempargs->ecdh_ctx[testnum];
1225 unsigned char *derived_secret = tempargs->secret_a;
358558eb 1226 int count;
cc98e639 1227 size_t *outlen = &(tempargs->outlen[testnum]);
3331e43b 1228
db1dd936 1229 for (count = 0; COND(ecdh_c[testnum][0]); count++)
f7d984dd
NT
1230 EVP_PKEY_derive(ctx, derived_secret, outlen);
1231
8b0b80d9
AG
1232 return count;
1233}
5f986ed3 1234
d3a9fb10
PY
1235static long eddsa_c[EdDSA_NUM][2];
1236static int EdDSA_sign_loop(void *args)
1237{
1238 loopargs_t *tempargs = *(loopargs_t **) args;
1239 unsigned char *buf = tempargs->buf;
1240 EVP_MD_CTX **edctx = tempargs->eddsa_ctx;
1241 unsigned char *eddsasig = tempargs->buf2;
52307f94 1242 size_t *eddsasigsize = &tempargs->sigsize;
d3a9fb10
PY
1243 int ret, count;
1244
1245 for (count = 0; COND(eddsa_c[testnum][0]); count++) {
52307f94 1246 ret = EVP_DigestSign(edctx[testnum], eddsasig, eddsasigsize, buf, 20);
d3a9fb10
PY
1247 if (ret == 0) {
1248 BIO_printf(bio_err, "EdDSA sign failure\n");
1249 ERR_print_errors(bio_err);
1250 count = -1;
1251 break;
1252 }
1253 }
1254 return count;
1255}
1256
1257static int EdDSA_verify_loop(void *args)
1258{
1259 loopargs_t *tempargs = *(loopargs_t **) args;
1260 unsigned char *buf = tempargs->buf;
1154ffbf 1261 EVP_MD_CTX **edctx = tempargs->eddsa_ctx2;
d3a9fb10 1262 unsigned char *eddsasig = tempargs->buf2;
52307f94 1263 size_t eddsasigsize = tempargs->sigsize;
d3a9fb10
PY
1264 int ret, count;
1265
1266 for (count = 0; COND(eddsa_c[testnum][1]); count++) {
52307f94 1267 ret = EVP_DigestVerify(edctx[testnum], eddsasig, eddsasigsize, buf, 20);
d3a9fb10
PY
1268 if (ret != 1) {
1269 BIO_printf(bio_err, "EdDSA verify failure\n");
1270 ERR_print_errors(bio_err);
1271 count = -1;
1272 break;
1273 }
1274 }
1275 return count;
1276}
a56f68ad
PY
1277
1278# ifndef OPENSSL_NO_SM2
1279static long sm2_c[SM2_NUM][2];
1280static int SM2_sign_loop(void *args)
1281{
1282 loopargs_t *tempargs = *(loopargs_t **) args;
1283 unsigned char *buf = tempargs->buf;
1284 EVP_MD_CTX **sm2ctx = tempargs->sm2_ctx;
1285 unsigned char *sm2sig = tempargs->buf2;
1286 size_t sm2sigsize = tempargs->sigsize;
1287 const size_t max_size = tempargs->sigsize;
1288 int ret, count;
1289 EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
1290
1291 for (count = 0; COND(sm2_c[testnum][0]); count++) {
1292 if (!EVP_DigestSignInit(sm2ctx[testnum], NULL, EVP_sm3(),
1293 NULL, sm2_pkey[testnum])) {
1294 BIO_printf(bio_err, "SM2 init sign failure\n");
1295 ERR_print_errors(bio_err);
1296 count = -1;
1297 break;
1298 }
1299 ret = EVP_DigestSign(sm2ctx[testnum], sm2sig, &sm2sigsize,
1300 buf, 20);
1301 if (ret == 0) {
1302 BIO_printf(bio_err, "SM2 sign failure\n");
1303 ERR_print_errors(bio_err);
1304 count = -1;
1305 break;
1306 }
1307 /* update the latest returned size and always use the fixed buffer size */
1308 tempargs->sigsize = sm2sigsize;
1309 sm2sigsize = max_size;
1310 }
1311
1312 return count;
1313}
1314
1315static int SM2_verify_loop(void *args)
1316{
1317 loopargs_t *tempargs = *(loopargs_t **) args;
1318 unsigned char *buf = tempargs->buf;
1319 EVP_MD_CTX **sm2ctx = tempargs->sm2_vfy_ctx;
1320 unsigned char *sm2sig = tempargs->buf2;
1321 size_t sm2sigsize = tempargs->sigsize;
1322 int ret, count;
1323 EVP_PKEY **sm2_pkey = tempargs->sm2_pkey;
1324
1325 for (count = 0; COND(sm2_c[testnum][1]); count++) {
1326 if (!EVP_DigestVerifyInit(sm2ctx[testnum], NULL, EVP_sm3(),
1327 NULL, sm2_pkey[testnum])) {
1328 BIO_printf(bio_err, "SM2 verify init failure\n");
1329 ERR_print_errors(bio_err);
1330 count = -1;
1331 break;
1332 }
1333 ret = EVP_DigestVerify(sm2ctx[testnum], sm2sig, sm2sigsize,
1334 buf, 20);
1335 if (ret != 1) {
1336 BIO_printf(bio_err, "SM2 verify failure\n");
1337 ERR_print_errors(bio_err);
1338 count = -1;
1339 break;
1340 }
1341 }
1342 return count;
1343}
1344# endif /* OPENSSL_NO_SM2 */
d6073e27 1345#endif /* OPENSSL_NO_EC */
8b0b80d9 1346
700b8145 1347static int run_benchmark(int async_jobs,
29dd15b1 1348 int (*loop_function) (void *), loopargs_t * loopargs)
8b0b80d9
AG
1349{
1350 int job_op_count = 0;
1351 int total_op_count = 0;
1352 int num_inprogress = 0;
700b8145 1353 int error = 0, i = 0, ret = 0;
1e613922
AG
1354 OSSL_ASYNC_FD job_fd = 0;
1355 size_t num_job_fds = 0;
8b0b80d9 1356
0ff43435 1357 if (async_jobs == 0) {
fb2141c7 1358 return loop_function((void *)&loopargs);
8b0b80d9
AG
1359 }
1360
1361 for (i = 0; i < async_jobs && !error; i++) {
fb2141c7
F
1362 loopargs_t *looparg_item = loopargs + i;
1363
1364 /* Copy pointer content (looparg_t item address) into async context */
700b8145
F
1365 ret = ASYNC_start_job(&loopargs[i].inprogress_job, loopargs[i].wait_ctx,
1366 &job_op_count, loop_function,
fb2141c7 1367 (void *)&looparg_item, sizeof(looparg_item));
700b8145 1368 switch (ret) {
fd4b0c08
F
1369 case ASYNC_PAUSE:
1370 ++num_inprogress;
1371 break;
1372 case ASYNC_FINISH:
1373 if (job_op_count == -1) {
8b0b80d9 1374 error = 1;
fd4b0c08
F
1375 } else {
1376 total_op_count += job_op_count;
1377 }
1378 break;
1379 case ASYNC_NO_JOBS:
1380 case ASYNC_ERR:
1381 BIO_printf(bio_err, "Failure in the job\n");
1382 ERR_print_errors(bio_err);
1383 error = 1;
1384 break;
8b0b80d9
AG
1385 }
1386 }
1387
1388 while (num_inprogress > 0) {
2ea92604 1389#if defined(OPENSSL_SYS_WINDOWS)
564e1029 1390 DWORD avail = 0;
2ea92604 1391#elif defined(OPENSSL_SYS_UNIX)
8b0b80d9 1392 int select_result = 0;
564e1029
AG
1393 OSSL_ASYNC_FD max_fd = 0;
1394 fd_set waitfdset;
363a1fc6 1395
564e1029 1396 FD_ZERO(&waitfdset);
1e613922 1397
564e1029
AG
1398 for (i = 0; i < async_jobs && num_inprogress > 0; i++) {
1399 if (loopargs[i].inprogress_job == NULL)
1400 continue;
1e613922 1401
29dd15b1
NT
1402 if (!ASYNC_WAIT_CTX_get_all_fds
1403 (loopargs[i].wait_ctx, NULL, &num_job_fds)
1404 || num_job_fds > 1) {
564e1029
AG
1405 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1406 ERR_print_errors(bio_err);
1407 error = 1;
1408 break;
8b0b80d9 1409 }
29dd15b1
NT
1410 ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
1411 &num_job_fds);
564e1029
AG
1412 FD_SET(job_fd, &waitfdset);
1413 if (job_fd > max_fd)
1414 max_fd = job_fd;
8b0b80d9 1415 }
8b0b80d9 1416
402ec2f5 1417 if (max_fd >= (OSSL_ASYNC_FD)FD_SETSIZE) {
570c0716 1418 BIO_printf(bio_err,
29dd15b1
NT
1419 "Error: max_fd (%d) must be smaller than FD_SETSIZE (%d). "
1420 "Decrease the value of async_jobs\n",
1421 max_fd, FD_SETSIZE);
570c0716
AG
1422 ERR_print_errors(bio_err);
1423 error = 1;
1424 break;
1425 }
1426
564e1029 1427 select_result = select(max_fd + 1, &waitfdset, NULL, NULL, NULL);
8b0b80d9
AG
1428 if (select_result == -1 && errno == EINTR)
1429 continue;
1430
1431 if (select_result == -1) {
564e1029
AG
1432 BIO_printf(bio_err, "Failure in the select\n");
1433 ERR_print_errors(bio_err);
1434 error = 1;
1435 break;
8b0b80d9
AG
1436 }
1437
1438 if (select_result == 0)
1439 continue;
8b0b80d9
AG
1440#endif
1441
1442 for (i = 0; i < async_jobs; i++) {
1443 if (loopargs[i].inprogress_job == NULL)
1444 continue;
1445
29dd15b1
NT
1446 if (!ASYNC_WAIT_CTX_get_all_fds
1447 (loopargs[i].wait_ctx, NULL, &num_job_fds)
1448 || num_job_fds > 1) {
1e613922
AG
1449 BIO_printf(bio_err, "Too many fds in ASYNC_WAIT_CTX\n");
1450 ERR_print_errors(bio_err);
1451 error = 1;
1452 break;
1453 }
29dd15b1
NT
1454 ASYNC_WAIT_CTX_get_all_fds(loopargs[i].wait_ctx, &job_fd,
1455 &num_job_fds);
8b0b80d9 1456
667867cc 1457#if defined(OPENSSL_SYS_UNIX)
1e613922 1458 if (num_job_fds == 1 && !FD_ISSET(job_fd, &waitfdset))
8b0b80d9 1459 continue;
667867cc 1460#elif defined(OPENSSL_SYS_WINDOWS)
fd4b0c08 1461 if (num_job_fds == 1
700b8145 1462 && !PeekNamedPipe(job_fd, NULL, 0, NULL, &avail, NULL)
fd4b0c08 1463 && avail > 0)
8b0b80d9
AG
1464 continue;
1465#endif
1466
609b0852 1467 ret = ASYNC_start_job(&loopargs[i].inprogress_job,
29dd15b1
NT
1468 loopargs[i].wait_ctx, &job_op_count,
1469 loop_function, (void *)(loopargs + i),
1470 sizeof(loopargs_t));
700b8145 1471 switch (ret) {
fd4b0c08
F
1472 case ASYNC_PAUSE:
1473 break;
1474 case ASYNC_FINISH:
1475 if (job_op_count == -1) {
8b0b80d9 1476 error = 1;
fd4b0c08
F
1477 } else {
1478 total_op_count += job_op_count;
1479 }
1480 --num_inprogress;
1481 loopargs[i].inprogress_job = NULL;
1482 break;
1483 case ASYNC_NO_JOBS:
1484 case ASYNC_ERR:
1485 --num_inprogress;
1486 loopargs[i].inprogress_job = NULL;
1487 BIO_printf(bio_err, "Failure in the job\n");
1488 ERR_print_errors(bio_err);
1489 error = 1;
1490 break;
8b0b80d9
AG
1491 }
1492 }
1493 }
1494
1495 return error ? -1 : total_op_count;
1496}
1497
128d25ba
DB
1498static EVP_MD *obtain_md(const char *name)
1499{
1500 EVP_MD *md = NULL;
1501
1502 /* Look through providers' digests */
1503 ERR_set_mark();
1504 md = EVP_MD_fetch(NULL, name, NULL);
1505 ERR_pop_to_mark();
1506 if (md != NULL) {
1507 fetched_alg = 1;
1508 return md;
1509 }
1510
1511 return (EVP_MD *)EVP_get_digestbyname(name);
1512}
1513
1514static EVP_CIPHER *obtain_cipher(const char *name)
1515{
1516 EVP_CIPHER *cipher = NULL;
1517
1518 /* Look through providers' ciphers */
1519 ERR_set_mark();
1520 cipher = EVP_CIPHER_fetch(NULL, name, NULL);
1521 ERR_pop_to_mark();
1522 if (cipher != NULL) {
1523 fetched_alg = 1;
1524 return cipher;
1525 }
1526
1527 return (EVP_CIPHER *)EVP_get_cipherbyname(name);
1528}
1529
f607f6ea
F
1530#define stop_it(do_it, test_num)\
1531 memset(do_it + test_num, 0, OSSL_NELEM(do_it) - test_num);
1532
8b0b80d9
AG
1533int speed_main(int argc, char **argv)
1534{
dd1abd44 1535 ENGINE *e = NULL;
8b0b80d9 1536 loopargs_t *loopargs = NULL;
5c6a69f5 1537 const char *prog;
19075d58 1538 const char *engine_id = NULL;
128d25ba 1539 EVP_CIPHER *evp_cipher = NULL;
8b0b80d9
AG
1540 double d = 0.0;
1541 OPTION_CHOICE o;
5c6a69f5 1542 int async_init = 0, multiblock = 0, pr_header = 0;
f607f6ea 1543 uint8_t doit[ALGOR_NUM] = { 0 };
44ca7565 1544 int ret = 1, misalign = 0, lengths_single = 0, aead = 0;
19075d58 1545 long count = 0;
1352e0ff 1546 unsigned int size_num = SIZE_NUM;
f607f6ea 1547 unsigned int i, k, loopargs_len = 0, async_jobs = 0;
6b1fe3d0 1548 int keylen;
397e23f8 1549 int buflen;
8b0b80d9
AG
1550#ifndef NO_FORK
1551 int multi = 0;
1552#endif
3a1ee3c1
RL
1553#if !defined(OPENSSL_NO_DEPRECATED_3_0) \
1554 || !defined(OPENSSL_NO_DSA) \
1555 || !defined(OPENSSL_NO_DH) \
1556 || !defined(OPENSSL_NO_EC)
9d0854f4 1557 long op_count = 1;
a00ae6c4 1558#endif
5c6a69f5 1559 openssl_speed_sec_t seconds = { SECONDS, RSA_SECONDS, DSA_SECONDS,
d3a9fb10 1560 ECDSA_SECONDS, ECDH_SECONDS,
60d3b5b9
HK
1561 EdDSA_SECONDS, SM2_SECONDS,
1562 FFDH_SECONDS };
5f986ed3
F
1563
1564 /* What follows are the buffers and key material. */
62c3fed0 1565#if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 1566 RC5_32_KEY rc5_ks;
a00ae6c4 1567#endif
ee2993ab 1568#if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 1569 RC2_KEY rc2_ks;
a00ae6c4 1570#endif
da2d32f6 1571#if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 1572 IDEA_KEY_SCHEDULE idea_ks;
a00ae6c4 1573#endif
28c690cb 1574#if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 1575 SEED_KEY_SCHEDULE seed_ks;
a00ae6c4 1576#endif
03047e7b 1577#if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 1578 BF_KEY bf_ks;
a00ae6c4 1579#endif
0ae5d4d6 1580#if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 1581 CAST_KEY cast_ks;
a00ae6c4 1582#endif
08bff785 1583#ifndef OPENSSL_NO_DEPRECATED_3_0
0f113f3e
MC
1584 static const unsigned char key16[16] = {
1585 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1586 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12
1587 };
0f113f3e
MC
1588 static const unsigned char key24[24] = {
1589 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1590 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1591 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34
1592 };
1593 static const unsigned char key32[32] = {
1594 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0,
1595 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12,
1596 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34,
1597 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56
1598 };
291850b4
MC
1599#endif
1600#if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0609658f 1601 CAMELLIA_KEY camellia_ks[3];
a00ae6c4 1602#endif
3a1ee3c1 1603#ifndef OPENSSL_NO_DEPRECATED_3_0
d63d89ea
F
1604 static const struct {
1605 const unsigned char *data;
1606 unsigned int length;
1607 unsigned int bits;
1608 } rsa_keys[] = {
1609 { test512, sizeof(test512), 512 },
1610 { test1024, sizeof(test1024), 1024 },
1611 { test2048, sizeof(test2048), 2048 },
1612 { test3072, sizeof(test3072), 3072 },
1613 { test4096, sizeof(test4096), 4092 },
1614 { test7680, sizeof(test7680), 7680 },
1615 { test15360, sizeof(test15360), 15360 }
0f113f3e 1616 };
f607f6ea 1617 uint8_t rsa_doit[RSA_NUM] = { 0 };
665d899f 1618 int primes = RSA_DEFAULT_PRIME_NUM;
a00ae6c4 1619#endif
60d3b5b9
HK
1620#ifndef OPENSSL_NO_DH
1621 typedef struct ffdh_params_st {
1622 const char *name;
1623 unsigned int nid;
1624 unsigned int bits;
1625 } FFDH_PARAMS;
1626
1627 static const FFDH_PARAMS ffdh_params[FFDH_NUM] = {
1628 {"ffdh2048", NID_ffdhe2048, 2048},
1629 {"ffdh3072", NID_ffdhe3072, 3072},
1630 {"ffdh4096", NID_ffdhe4096, 4096},
1631 {"ffdh6144", NID_ffdhe6144, 6144},
1632 {"ffdh8192", NID_ffdhe8192, 8192}
1633 };
1634 uint8_t ffdh_doit[FFDH_NUM] = { 0 };
1635
1636#endif /* OPENSSL_NO_DH */
1ddf2594 1637#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
4d82c58b 1638 static const unsigned int dsa_bits[DSA_NUM] = { 512, 1024, 2048 };
f607f6ea 1639 uint8_t dsa_doit[DSA_NUM] = { 0 };
a00ae6c4
RS
1640#endif
1641#ifndef OPENSSL_NO_EC
d63d89ea
F
1642 typedef struct ec_curve_st {
1643 const char *name;
1644 unsigned int nid;
1645 unsigned int bits;
1646 size_t sigsize; /* only used for EdDSA curves */
1647 } EC_CURVE;
0f113f3e
MC
1648 /*
1649 * We only test over the following curves as they are representative, To
1650 * add tests over more curves, simply add the curve NID and curve name to
1352e0ff
F
1651 * the following arrays and increase the |ecdh_choices| and |ecdsa_choices|
1652 * lists accordingly.
0f113f3e 1653 */
d63d89ea 1654 static const EC_CURVE ec_curves[EC_NUM] = {
0f113f3e 1655 /* Prime Curves */
48bc0d99
F
1656 {"secp160r1", NID_secp160r1, 160},
1657 {"nistp192", NID_X9_62_prime192v1, 192},
1658 {"nistp224", NID_secp224r1, 224},
1659 {"nistp256", NID_X9_62_prime256v1, 256},
5c8b7b4c 1660 {"nistp384", NID_secp384r1, 384},
48bc0d99 1661 {"nistp521", NID_secp521r1, 521},
f5c99167 1662# ifndef OPENSSL_NO_EC2M
0f113f3e 1663 /* Binary Curves */
48bc0d99 1664 {"nistk163", NID_sect163k1, 163},
5c8b7b4c 1665 {"nistk233", NID_sect233k1, 233},
48bc0d99
F
1666 {"nistk283", NID_sect283k1, 283},
1667 {"nistk409", NID_sect409k1, 409},
1668 {"nistk571", NID_sect571k1, 571},
1669 {"nistb163", NID_sect163r2, 163},
1670 {"nistb233", NID_sect233r1, 233},
1671 {"nistb283", NID_sect283r1, 283},
1672 {"nistb409", NID_sect409r1, 409},
1673 {"nistb571", NID_sect571r1, 571},
f5c99167 1674# endif
1c534560
F
1675 {"brainpoolP256r1", NID_brainpoolP256r1, 256},
1676 {"brainpoolP256t1", NID_brainpoolP256t1, 256},
1677 {"brainpoolP384r1", NID_brainpoolP384r1, 384},
1678 {"brainpoolP384t1", NID_brainpoolP384t1, 384},
1679 {"brainpoolP512r1", NID_brainpoolP512r1, 512},
1680 {"brainpoolP512t1", NID_brainpoolP512t1, 512},
5c6a69f5 1681 /* Other and ECDH only ones */
48bc0d99
F
1682 {"X25519", NID_X25519, 253},
1683 {"X448", NID_X448, 448}
0f113f3e 1684 };
d63d89ea 1685 static const EC_CURVE ed_curves[EdDSA_NUM] = {
d3a9fb10
PY
1686 /* EdDSA */
1687 {"Ed25519", NID_ED25519, 253, 64},
1688 {"Ed448", NID_ED448, 456, 114}
1689 };
a56f68ad 1690# ifndef OPENSSL_NO_SM2
d63d89ea 1691 static const EC_CURVE sm2_curves[SM2_NUM] = {
a56f68ad
PY
1692 /* SM2 */
1693 {"CurveSM2", NID_sm2, 256}
1694 };
f607f6ea 1695 uint8_t sm2_doit[SM2_NUM] = { 0 };
a56f68ad 1696# endif
f607f6ea
F
1697 uint8_t ecdsa_doit[ECDSA_NUM] = { 0 };
1698 uint8_t ecdh_doit[EC_NUM] = { 0 };
1699 uint8_t eddsa_doit[EdDSA_NUM] = { 0 };
1352e0ff
F
1700
1701 /* checks declarated curves against choices list. */
1702 OPENSSL_assert(ed_curves[EdDSA_NUM - 1].nid == NID_ED448);
1703 OPENSSL_assert(strcmp(eddsa_choices[EdDSA_NUM - 1].name, "ed448") == 0);
1704
1705 OPENSSL_assert(ec_curves[EC_NUM - 1].nid == NID_X448);
1706 OPENSSL_assert(strcmp(ecdh_choices[EC_NUM - 1].name, "ecdhx448") == 0);
1707
1708 OPENSSL_assert(ec_curves[ECDSA_NUM - 1].nid == NID_brainpoolP512t1);
1709 OPENSSL_assert(strcmp(ecdsa_choices[ECDSA_NUM - 1].name, "ecdsabrp512t1") == 0);
1710
dcea51af 1711# ifndef OPENSSL_NO_SM2
1352e0ff
F
1712 OPENSSL_assert(sm2_curves[SM2_NUM - 1].nid == NID_sm2);
1713 OPENSSL_assert(strcmp(sm2_choices[SM2_NUM - 1].name, "curveSM2") == 0);
dcea51af 1714# endif
d6073e27 1715#endif /* ndef OPENSSL_NO_EC */
7e1b7485
RS
1716
1717 prog = opt_init(argc, argv, speed_options);
1718 while ((o = opt_next()) != OPT_EOF) {
1719 switch (o) {
1720 case OPT_EOF:
1721 case OPT_ERR:
1722 opterr:
1723 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
1724 goto end;
1725 case OPT_HELP:
1726 opt_help(speed_options);
1727 ret = 0;
1728 goto end;
1729 case OPT_ELAPSED:
0f113f3e 1730 usertime = 0;
7e1b7485
RS
1731 break;
1732 case OPT_EVP:
128d25ba
DB
1733 if (doit[D_EVP]) {
1734 BIO_printf(bio_err, "%s: -evp option cannot be used more than once\n", prog);
1735 goto opterr;
1736 }
9ae4e664 1737 evp_md = NULL;
128d25ba 1738 evp_cipher = obtain_cipher(opt_arg());
7e1b7485 1739 if (evp_cipher == NULL)
128d25ba 1740 evp_md = obtain_md(opt_arg());
7e1b7485
RS
1741 if (evp_cipher == NULL && evp_md == NULL) {
1742 BIO_printf(bio_err,
55b09fe6 1743 "%s: %s is an unknown cipher or digest\n",
7e1b7485 1744 prog, opt_arg());
0f113f3e
MC
1745 goto end;
1746 }
1747 doit[D_EVP] = 1;
7e1b7485 1748 break;
f88b9b79 1749 case OPT_HMAC:
dbde4726 1750#ifndef OPENSSL_NO_DEPRECATED_3_0
f88b9b79
P
1751 evp_hmac_md = EVP_get_digestbyname(opt_arg());
1752 if (evp_hmac_md == NULL) {
1753 BIO_printf(bio_err, "%s: %s is an unknown digest\n",
1754 prog, opt_arg());
1755 goto end;
1756 }
1757 doit[D_EVP_HMAC] = 1;
1758 break;
dbde4726 1759#endif
9bba2c4c 1760 case OPT_CMAC:
a6d572e6 1761#if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
9bba2c4c
BE
1762 evp_cmac_cipher = EVP_get_cipherbyname(opt_arg());
1763 if (evp_cmac_cipher == NULL) {
1764 BIO_printf(bio_err, "%s: %s is an unknown cipher\n",
1765 prog, opt_arg());
1766 goto end;
1767 }
1768 doit[D_EVP_CMAC] = 1;
1769#endif
1770 break;
7e1b7485 1771 case OPT_DECRYPT:
0f113f3e 1772 decrypt = 1;
7e1b7485 1773 break;
7e1b7485 1774 case OPT_ENGINE:
8b0b80d9
AG
1775 /*
1776 * In a forked execution, an engine might need to be
1777 * initialised by each child process, not by the parent.
1778 * So store the name here and run setup_engine() later on.
1779 */
1780 engine_id = opt_arg();
7e1b7485 1781 break;
7e1b7485 1782 case OPT_MULTI:
9c3bcfa0 1783#ifndef NO_FORK
7e1b7485 1784 multi = atoi(opt_arg());
8b0b80d9
AG
1785#endif
1786 break;
1787 case OPT_ASYNCJOBS:
667867cc 1788#ifndef OPENSSL_NO_ASYNC
8b0b80d9 1789 async_jobs = atoi(opt_arg());
667867cc
MC
1790 if (!ASYNC_is_capable()) {
1791 BIO_printf(bio_err,
1792 "%s: async_jobs specified but async not supported\n",
1793 prog);
1794 goto opterr;
1795 }
f8aa1572 1796 if (async_jobs > 99999) {
5c6a69f5 1797 BIO_printf(bio_err, "%s: too many async_jobs\n", prog);
f8aa1572
BE
1798 goto opterr;
1799 }
a00ae6c4 1800#endif
9c3bcfa0 1801 break;
7e1b7485
RS
1802 case OPT_MISALIGN:
1803 if (!opt_int(opt_arg(), &misalign))
0f113f3e 1804 goto end;
7e1b7485 1805 if (misalign > MISALIGN) {
0f113f3e 1806 BIO_printf(bio_err,
7e1b7485
RS
1807 "%s: Maximum offset is %d\n", prog, MISALIGN);
1808 goto opterr;
0f113f3e 1809 }
7e1b7485
RS
1810 break;
1811 case OPT_MR:
1812 mr = 1;
1813 break;
1814 case OPT_MB:
1815 multiblock = 1;
cfd451d4
F
1816#ifdef OPENSSL_NO_MULTIBLOCK
1817 BIO_printf(bio_err,
1818 "%s: -mb specified but multi-block support is disabled\n",
1819 prog);
1820 goto end;
1821#endif
7e1b7485 1822 break;
3ee1eac2
RS
1823 case OPT_R_CASES:
1824 if (!opt_rand(o))
1825 goto end;
1826 break;
6bd4e3f2
P
1827 case OPT_PROV_CASES:
1828 if (!opt_provider(o))
1829 goto end;
1830 break;
665d899f 1831 case OPT_PRIMES:
c5f87134 1832#ifndef OPENSSL_NO_DEPRECATED_3_0
665d899f
PY
1833 if (!opt_int(opt_arg(), &primes))
1834 goto end;
c5f87134 1835#endif
665d899f 1836 break;
64daf14d
PS
1837 case OPT_SECONDS:
1838 seconds.sym = seconds.rsa = seconds.dsa = seconds.ecdsa
a56f68ad 1839 = seconds.ecdh = seconds.eddsa
60d3b5b9 1840 = seconds.sm2 = seconds.ffdh = atoi(opt_arg());
64daf14d
PS
1841 break;
1842 case OPT_BYTES:
1843 lengths_single = atoi(opt_arg());
1844 lengths = &lengths_single;
1845 size_num = 1;
1846 break;
44ca7565
AP
1847 case OPT_AEAD:
1848 aead = 1;
1849 break;
7e1b7485
RS
1850 }
1851 }
021410ea
RS
1852
1853 /* Remaining arguments are algorithms. */
7e1b7485
RS
1854 argc = opt_num_rest();
1855 argv = opt_rest();
1856
51e5df0e 1857 app_RAND_load();
29dd15b1 1858 for (; *argv; argv++) {
f607f6ea
F
1859 const char *algo = *argv;
1860
1352e0ff 1861 if (opt_found(algo, doit_choices, &i)) {
7e1b7485
RS
1862 doit[i] = 1;
1863 continue;
1864 }
c6fec81b 1865#if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
f607f6ea 1866 if (strcmp(algo, "des") == 0) {
7e1b7485
RS
1867 doit[D_CBC_DES] = doit[D_EDE3_DES] = 1;
1868 continue;
1869 }
a00ae6c4 1870#endif
f607f6ea 1871 if (strcmp(algo, "sha") == 0) {
7e1b7485
RS
1872 doit[D_SHA1] = doit[D_SHA256] = doit[D_SHA512] = 1;
1873 continue;
1874 }
3a1ee3c1 1875#ifndef OPENSSL_NO_DEPRECATED_3_0
f607f6ea 1876 if (strcmp(algo, "openssl") == 0) /* just for compatibility */
7e1b7485 1877 continue;
f607f6ea
F
1878 if (strncmp(algo, "rsa", 3) == 0) {
1879 if (algo[3] == '\0') {
1880 memset(rsa_doit, 1, sizeof(rsa_doit));
1881 continue;
1882 }
1352e0ff 1883 if (opt_found(algo, rsa_choices, &i)) {
f607f6ea
F
1884 rsa_doit[i] = 1;
1885 continue;
1886 }
7e1b7485 1887 }
a00ae6c4 1888#endif
60d3b5b9
HK
1889#ifndef OPENSSL_NO_DH
1890 if (strncmp(algo, "ffdh", 4) == 0) {
1891 if (algo[4] == '\0') {
1892 memset(ffdh_doit, 1, sizeof(ffdh_doit));
1893 continue;
1894 }
1895 if (opt_found(algo, ffdh_choices, &i)) {
1896 ffdh_doit[i] = 2;
1897 continue;
1898 }
1899 }
1900#endif
1ddf2594 1901#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
f607f6ea
F
1902 if (strncmp(algo, "dsa", 3) == 0) {
1903 if (algo[3] == '\0') {
1904 memset(dsa_doit, 1, sizeof(dsa_doit));
1905 continue;
1906 }
1352e0ff 1907 if (opt_found(algo, dsa_choices, &i)) {
f607f6ea
F
1908 dsa_doit[i] = 2;
1909 continue;
1910 }
7e1b7485 1911 }
a00ae6c4 1912#endif
c72fa255 1913#ifndef OPENSSL_NO_DEPRECATED_3_0
f607f6ea 1914 if (strcmp(algo, "aes") == 0) {
29dd15b1 1915 doit[D_CBC_128_AES] = doit[D_CBC_192_AES] = doit[D_CBC_256_AES] = 1;
7e1b7485
RS
1916 continue;
1917 }
c72fa255 1918#endif
291850b4 1919#if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
f607f6ea 1920 if (strcmp(algo, "camellia") == 0) {
29dd15b1 1921 doit[D_CBC_128_CML] = doit[D_CBC_192_CML] = doit[D_CBC_256_CML] = 1;
7e1b7485
RS
1922 continue;
1923 }
a00ae6c4 1924#endif
10bf4fc2 1925#ifndef OPENSSL_NO_EC
f607f6ea
F
1926 if (strncmp(algo, "ecdsa", 5) == 0) {
1927 if (algo[5] == '\0') {
1928 memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
1929 continue;
1930 }
1352e0ff 1931 if (opt_found(algo, ecdsa_choices, &i)) {
f607f6ea
F
1932 ecdsa_doit[i] = 2;
1933 continue;
1934 }
0f113f3e 1935 }
f607f6ea
F
1936 if (strncmp(algo, "ecdh", 4) == 0) {
1937 if (algo[4] == '\0') {
1938 memset(ecdh_doit, 1, sizeof(ecdh_doit));
1939 continue;
1940 }
1352e0ff 1941 if (opt_found(algo, ecdh_choices, &i)) {
f607f6ea
F
1942 ecdh_doit[i] = 2;
1943 continue;
1944 }
d3a9fb10 1945 }
1352e0ff
F
1946 if (strcmp(algo, "eddsa") == 0) {
1947 memset(eddsa_doit, 1, sizeof(eddsa_doit));
1948 continue;
1949 }
1950 if (opt_found(algo, eddsa_choices, &i)) {
1951 eddsa_doit[i] = 2;
1952 continue;
d3a9fb10 1953 }
a56f68ad 1954# ifndef OPENSSL_NO_SM2
f607f6ea
F
1955 if (strcmp(algo, "sm2") == 0) {
1956 memset(sm2_doit, 1, sizeof(sm2_doit));
a56f68ad
PY
1957 continue;
1958 }
1352e0ff 1959 if (opt_found(algo, sm2_choices, &i)) {
a56f68ad
PY
1960 sm2_doit[i] = 2;
1961 continue;
1962 }
1963# endif
1352e0ff 1964#endif /* OPENSSL_NO_EC */
f607f6ea 1965 BIO_printf(bio_err, "%s: Unknown algorithm %s\n", prog, algo);
7e1b7485 1966 goto end;
0f113f3e 1967 }
d02b48c6 1968
44ca7565
AP
1969 /* Sanity checks */
1970 if (aead) {
1971 if (evp_cipher == NULL) {
1972 BIO_printf(bio_err, "-aead can be used only with an AEAD cipher\n");
1973 goto end;
1974 } else if (!(EVP_CIPHER_flags(evp_cipher) &
1975 EVP_CIPH_FLAG_AEAD_CIPHER)) {
1976 BIO_printf(bio_err, "%s is not an AEAD cipher\n",
1977 OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
1978 goto end;
1979 }
1980 }
1981 if (multiblock) {
1982 if (evp_cipher == NULL) {
1983 BIO_printf(bio_err,"-mb can be used only with a multi-block"
1984 " capable cipher\n");
1985 goto end;
1986 } else if (!(EVP_CIPHER_flags(evp_cipher) &
1987 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
1988 BIO_printf(bio_err, "%s is not a multi-block capable\n",
1989 OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher)));
1990 goto end;
1991 } else if (async_jobs > 0) {
1992 BIO_printf(bio_err, "Async mode is not supported with -mb");
1993 goto end;
1994 }
1995 }
1996
8b0b80d9
AG
1997 /* Initialize the job pool if async mode is enabled */
1998 if (async_jobs > 0) {
dab1f5fe
CS
1999 async_init = ASYNC_init_thread(async_jobs, async_jobs);
2000 if (!async_init) {
8b0b80d9
AG
2001 BIO_printf(bio_err, "Error creating the ASYNC job pool\n");
2002 goto end;
2003 }
2004 }
2005
2006 loopargs_len = (async_jobs == 0 ? 1 : async_jobs);
29dd15b1
NT
2007 loopargs =
2008 app_malloc(loopargs_len * sizeof(loopargs_t), "array of loopargs");
8b0b80d9
AG
2009 memset(loopargs, 0, loopargs_len * sizeof(loopargs_t));
2010
0ff43435 2011 for (i = 0; i < loopargs_len; i++) {
1e613922
AG
2012 if (async_jobs > 0) {
2013 loopargs[i].wait_ctx = ASYNC_WAIT_CTX_new();
2014 if (loopargs[i].wait_ctx == NULL) {
2015 BIO_printf(bio_err, "Error creating the ASYNC_WAIT_CTX\n");
2016 goto end;
2017 }
2018 }
2019
2fc45cb8 2020 buflen = lengths[size_num - 1];
c2969ff6 2021 if (buflen < 36) /* size of random vector in RSA benchmark */
2fc45cb8
AP
2022 buflen = 36;
2023 buflen += MAX_MISALIGNMENT + 1;
397e23f8
PS
2024 loopargs[i].buf_malloc = app_malloc(buflen, "input buffer");
2025 loopargs[i].buf2_malloc = app_malloc(buflen, "input buffer");
2026 memset(loopargs[i].buf_malloc, 0, buflen);
2027 memset(loopargs[i].buf2_malloc, 0, buflen);
2028
8b0b80d9
AG
2029 /* Align the start of buffers on a 64 byte boundary */
2030 loopargs[i].buf = loopargs[i].buf_malloc + misalign;
2031 loopargs[i].buf2 = loopargs[i].buf2_malloc + misalign;
0ff43435 2032#ifndef OPENSSL_NO_EC
0ff43435
AG
2033 loopargs[i].secret_a = app_malloc(MAX_ECDH_SIZE, "ECDH secret a");
2034 loopargs[i].secret_b = app_malloc(MAX_ECDH_SIZE, "ECDH secret b");
60d3b5b9
HK
2035#endif
2036#ifndef OPENSSL_NO_DH
2037 loopargs[i].secret_ff_a = app_malloc(MAX_FFDH_SIZE, "FFDH secret a");
2038 loopargs[i].secret_ff_b = app_malloc(MAX_FFDH_SIZE, "FFDH secret b");
0ff43435 2039#endif
8b0b80d9
AG
2040 }
2041
a00ae6c4 2042#ifndef NO_FORK
64daf14d 2043 if (multi && do_multi(multi, size_num))
0f113f3e 2044 goto show_res;
a00ae6c4 2045#endif
d02b48c6 2046
8b0b80d9 2047 /* Initialize the engine after the fork */
dd1abd44 2048 e = setup_engine(engine_id, 0);
8b0b80d9 2049
7e1b7485 2050 /* No parameters; turn on everything. */
9bba2c4c 2051 if (argc == 0 && !doit[D_EVP] && !doit[D_EVP_HMAC] && !doit[D_EVP_CMAC]) {
f607f6ea
F
2052 memset(doit, 1, sizeof(doit));
2053 doit[D_EVP] = doit[D_EVP_HMAC] = doit[D_EVP_CMAC] = 0;
42b2797e 2054#if !defined(OPENSSL_NO_MDC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3a1ee3c1 2055 doit[D_MDC2] = 0;
42b2797e
SAS
2056#endif
2057#if !defined(OPENSSL_NO_MD4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3a1ee3c1 2058 doit[D_MD4] = 0;
42b2797e
SAS
2059#endif
2060#if !defined(OPENSSL_NO_RMD160) && !defined(OPENSSL_NO_DEPRECATED_3_0)
3a1ee3c1 2061 doit[D_RMD160] = 0;
42b2797e 2062#endif
3a1ee3c1 2063#ifndef OPENSSL_NO_DEPRECATED_3_0
f607f6ea 2064 memset(rsa_doit, 1, sizeof(rsa_doit));
d6073e27 2065#endif
60d3b5b9
HK
2066#ifndef OPENSSL_NO_DH
2067 memset(ffdh_doit, 1, sizeof(ffdh_doit));
2068#endif
1ddf2594 2069#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
f607f6ea 2070 memset(dsa_doit, 1, sizeof(dsa_doit));
83ae8124 2071#endif
10bf4fc2 2072#ifndef OPENSSL_NO_EC
f607f6ea
F
2073 memset(ecdsa_doit, 1, sizeof(ecdsa_doit));
2074 memset(ecdh_doit, 1, sizeof(ecdh_doit));
2075 memset(eddsa_doit, 1, sizeof(eddsa_doit));
a56f68ad 2076# ifndef OPENSSL_NO_SM2
f607f6ea 2077 memset(sm2_doit, 1, sizeof(sm2_doit));
a56f68ad 2078# endif
a00ae6c4 2079#endif
0f113f3e
MC
2080 }
2081 for (i = 0; i < ALGOR_NUM; i++)
2082 if (doit[i])
2083 pr_header++;
2084
2085 if (usertime == 0 && !mr)
2086 BIO_printf(bio_err,
2087 "You have chosen to measure elapsed time "
2088 "instead of user CPU time.\n");
2089
3a1ee3c1 2090#ifndef OPENSSL_NO_DEPRECATED_3_0
0ff43435 2091 for (i = 0; i < loopargs_len; i++) {
665d899f
PY
2092 if (primes > RSA_DEFAULT_PRIME_NUM) {
2093 /* for multi-prime RSA, skip this */
2094 break;
2095 }
0ff43435 2096 for (k = 0; k < RSA_NUM; k++) {
d63d89ea 2097 const unsigned char *p = rsa_keys[k].data;
0ff43435 2098
29dd15b1 2099 loopargs[i].rsa_key[k] =
d63d89ea 2100 d2i_RSAPrivateKey(NULL, &p, rsa_keys[k].length);
0ff43435 2101 if (loopargs[i].rsa_key[k] == NULL) {
29dd15b1
NT
2102 BIO_printf(bio_err,
2103 "internal error loading RSA key number %d\n", k);
0ff43435
AG
2104 goto end;
2105 }
0f113f3e 2106 }
a00ae6c4
RS
2107 }
2108#endif
1ddf2594 2109#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0ff43435 2110 for (i = 0; i < loopargs_len; i++) {
0848e01b
PY
2111 loopargs[i].dsa_key[0] = get_dsa(512);
2112 loopargs[i].dsa_key[1] = get_dsa(1024);
2113 loopargs[i].dsa_key[2] = get_dsa(2048);
0ff43435 2114 }
a00ae6c4 2115#endif
c6fec81b 2116#if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0609658f
F
2117 if (doit[D_CBC_DES] || doit[D_EDE3_DES]) {
2118 static DES_cblock keys[] = {
2119 { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0 }, /* keys[0] */
2120 { 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12 }, /* keys[1] */
2121 { 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34 } /* keys[3] */
2122 };
2123 DES_set_key_unchecked(&keys[0], &sch[0]);
2124 DES_set_key_unchecked(&keys[1], &sch[1]);
2125 DES_set_key_unchecked(&keys[2], &sch[2]);
2126 }
a00ae6c4 2127#endif
c72fa255 2128#ifndef OPENSSL_NO_DEPRECATED_3_0
0f113f3e
MC
2129 AES_set_encrypt_key(key16, 128, &aes_ks1);
2130 AES_set_encrypt_key(key24, 192, &aes_ks2);
2131 AES_set_encrypt_key(key32, 256, &aes_ks3);
c72fa255 2132#endif
291850b4 2133#if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0609658f
F
2134 if (doit[D_CBC_128_CML] || doit[D_CBC_192_CML] || doit[D_CBC_256_CML]) {
2135 Camellia_set_key(key16, 128, &camellia_ks[0]);
2136 Camellia_set_key(key24, 192, &camellia_ks[1]);
2137 Camellia_set_key(key32, 256, &camellia_ks[2]);
2138 }
a00ae6c4 2139#endif
da2d32f6 2140#if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0609658f
F
2141 if (doit[D_CBC_IDEA])
2142 IDEA_set_encrypt_key(key16, &idea_ks);
a00ae6c4 2143#endif
28c690cb 2144#if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0609658f
F
2145 if (doit[D_CBC_SEED])
2146 SEED_set_key(key16, &seed_ks);
a00ae6c4 2147#endif
a8fca728 2148#if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0609658f
F
2149 if (doit[D_RC4])
2150 RC4_set_key(&rc4_ks, 16, key16);
a00ae6c4 2151#endif
ee2993ab 2152#if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0609658f
F
2153 if (doit[D_CBC_RC2])
2154 RC2_set_key(&rc2_ks, 16, key16, 128);
a00ae6c4 2155#endif
62c3fed0 2156#if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0609658f
F
2157 if (doit[D_CBC_RC5])
2158 if (!RC5_32_set_key(&rc5_ks, 16, key16, 12)) {
2159 BIO_printf(bio_err, "Failed setting RC5 key\n");
2160 goto end;
2161 }
a00ae6c4 2162#endif
03047e7b 2163#if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0609658f
F
2164 if (doit[D_CBC_BF])
2165 BF_set_key(&bf_ks, 16, key16);
a00ae6c4 2166#endif
0ae5d4d6 2167#if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0609658f
F
2168 if (doit[D_CBC_CAST])
2169 CAST_set_key(&cast_ks, 16, key16);
a00ae6c4 2170#endif
a00ae6c4 2171#ifndef SIGALRM
c6fec81b 2172#if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e
MC
2173 BIO_printf(bio_err, "First we calculate the approximate speed ...\n");
2174 count = 10;
2175 do {
2176 long it;
2177 count *= 2;
2178 Time_F(START);
2179 for (it = count; it; it--)
8b0b80d9
AG
2180 DES_ecb_encrypt((DES_cblock *)loopargs[0].buf,
2181 (DES_cblock *)loopargs[0].buf, &sch, DES_ENCRYPT);
0f113f3e
MC
2182 d = Time_F(STOP);
2183 } while (d < 3);
0f113f3e
MC
2184 c[D_MD2][0] = count / 10;
2185 c[D_MDC2][0] = count / 10;
2186 c[D_MD4][0] = count;
2187 c[D_MD5][0] = count;
2188 c[D_HMAC][0] = count;
2189 c[D_SHA1][0] = count;
2190 c[D_RMD160][0] = count;
2191 c[D_RC4][0] = count * 5;
2192 c[D_CBC_DES][0] = count;
2193 c[D_EDE3_DES][0] = count / 3;
2194 c[D_CBC_IDEA][0] = count;
2195 c[D_CBC_SEED][0] = count;
2196 c[D_CBC_RC2][0] = count;
2197 c[D_CBC_RC5][0] = count;
2198 c[D_CBC_BF][0] = count;
2199 c[D_CBC_CAST][0] = count;
2200 c[D_CBC_128_AES][0] = count;
2201 c[D_CBC_192_AES][0] = count;
2202 c[D_CBC_256_AES][0] = count;
2203 c[D_CBC_128_CML][0] = count;
2204 c[D_CBC_192_CML][0] = count;
2205 c[D_CBC_256_CML][0] = count;
d02b7e09 2206 c[D_EVP][0] = count;
0f113f3e
MC
2207 c[D_SHA256][0] = count;
2208 c[D_SHA512][0] = count;
2209 c[D_WHIRLPOOL][0] = count;
2210 c[D_IGE_128_AES][0] = count;
2211 c[D_IGE_192_AES][0] = count;
2212 c[D_IGE_256_AES][0] = count;
2213 c[D_GHASH][0] = count;
65e6b9a4 2214 c[D_RAND][0] = count;
d02b7e09
F
2215 c[D_EVP_HMAC][0] = count;
2216 c[D_EVP_CMAC][0] = count;
0f113f3e 2217
64daf14d 2218 for (i = 1; i < size_num; i++) {
d02b7e09
F
2219 long l0 = (long)lengths[0];
2220 long l1 = (long)lengths[i];
0f113f3e
MC
2221
2222 c[D_MD2][i] = c[D_MD2][0] * 4 * l0 / l1;
2223 c[D_MDC2][i] = c[D_MDC2][0] * 4 * l0 / l1;
2224 c[D_MD4][i] = c[D_MD4][0] * 4 * l0 / l1;
2225 c[D_MD5][i] = c[D_MD5][0] * 4 * l0 / l1;
2226 c[D_HMAC][i] = c[D_HMAC][0] * 4 * l0 / l1;
2227 c[D_SHA1][i] = c[D_SHA1][0] * 4 * l0 / l1;
2228 c[D_RMD160][i] = c[D_RMD160][0] * 4 * l0 / l1;
d02b7e09 2229 c[D_EVP][i] = = c[D_EVP][0] * 4 * l0 / l1;
0f113f3e
MC
2230 c[D_SHA256][i] = c[D_SHA256][0] * 4 * l0 / l1;
2231 c[D_SHA512][i] = c[D_SHA512][0] * 4 * l0 / l1;
2232 c[D_WHIRLPOOL][i] = c[D_WHIRLPOOL][0] * 4 * l0 / l1;
6d9843e7 2233 c[D_GHASH][i] = c[D_GHASH][0] * 4 * l0 / l1;
65e6b9a4 2234 c[D_RAND][i] = c[D_RAND][0] * 4 * l0 / l1;
d02b7e09
F
2235 c[D_EVP_HMAC][i] = = c[D_EVP_HMAC][0] * 4 * l0 / l1;
2236 c[D_EVP_CMAC][i] = = c[D_EVP_CMAC][0] * 4 * l0 / l1;
0f113f3e
MC
2237
2238 l0 = (long)lengths[i - 1];
2239
2240 c[D_RC4][i] = c[D_RC4][i - 1] * l0 / l1;
2241 c[D_CBC_DES][i] = c[D_CBC_DES][i - 1] * l0 / l1;
2242 c[D_EDE3_DES][i] = c[D_EDE3_DES][i - 1] * l0 / l1;
2243 c[D_CBC_IDEA][i] = c[D_CBC_IDEA][i - 1] * l0 / l1;
2244 c[D_CBC_SEED][i] = c[D_CBC_SEED][i - 1] * l0 / l1;
2245 c[D_CBC_RC2][i] = c[D_CBC_RC2][i - 1] * l0 / l1;
2246 c[D_CBC_RC5][i] = c[D_CBC_RC5][i - 1] * l0 / l1;
2247 c[D_CBC_BF][i] = c[D_CBC_BF][i - 1] * l0 / l1;
2248 c[D_CBC_CAST][i] = c[D_CBC_CAST][i - 1] * l0 / l1;
2249 c[D_CBC_128_AES][i] = c[D_CBC_128_AES][i - 1] * l0 / l1;
2250 c[D_CBC_192_AES][i] = c[D_CBC_192_AES][i - 1] * l0 / l1;
2251 c[D_CBC_256_AES][i] = c[D_CBC_256_AES][i - 1] * l0 / l1;
2252 c[D_CBC_128_CML][i] = c[D_CBC_128_CML][i - 1] * l0 / l1;
2253 c[D_CBC_192_CML][i] = c[D_CBC_192_CML][i - 1] * l0 / l1;
2254 c[D_CBC_256_CML][i] = c[D_CBC_256_CML][i - 1] * l0 / l1;
2255 c[D_IGE_128_AES][i] = c[D_IGE_128_AES][i - 1] * l0 / l1;
2256 c[D_IGE_192_AES][i] = c[D_IGE_192_AES][i - 1] * l0 / l1;
2257 c[D_IGE_256_AES][i] = c[D_IGE_256_AES][i - 1] * l0 / l1;
2258 }
e172d60d 2259
3a1ee3c1 2260# ifndef OPENSSL_NO_DEPRECATED_3_0
0f113f3e
MC
2261 rsa_c[R_RSA_512][0] = count / 2000;
2262 rsa_c[R_RSA_512][1] = count / 400;
2263 for (i = 1; i < RSA_NUM; i++) {
2264 rsa_c[i][0] = rsa_c[i - 1][0] / 8;
2265 rsa_c[i][1] = rsa_c[i - 1][1] / 4;
70c4e156 2266 if (rsa_doit[i] <= 1 && rsa_c[i][0] == 0)
0f113f3e
MC
2267 rsa_doit[i] = 0;
2268 else {
2269 if (rsa_c[i][0] == 0) {
29dd15b1 2270 rsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
0f113f3e
MC
2271 rsa_c[i][1] = 20;
2272 }
2273 }
2274 }
a00ae6c4 2275# endif
0f113f3e 2276
1ddf2594 2277# if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e
MC
2278 dsa_c[R_DSA_512][0] = count / 1000;
2279 dsa_c[R_DSA_512][1] = count / 1000 / 2;
2280 for (i = 1; i < DSA_NUM; i++) {
2281 dsa_c[i][0] = dsa_c[i - 1][0] / 4;
2282 dsa_c[i][1] = dsa_c[i - 1][1] / 4;
70c4e156 2283 if (dsa_doit[i] <= 1 && dsa_c[i][0] == 0)
0f113f3e
MC
2284 dsa_doit[i] = 0;
2285 else {
70c4e156 2286 if (dsa_c[i][0] == 0) {
29dd15b1 2287 dsa_c[i][0] = 1; /* Set minimum iteration Nb to 1. */
0f113f3e
MC
2288 dsa_c[i][1] = 1;
2289 }
2290 }
2291 }
a00ae6c4 2292# endif
0f113f3e 2293
10bf4fc2 2294# ifndef OPENSSL_NO_EC
0f113f3e
MC
2295 ecdsa_c[R_EC_P160][0] = count / 1000;
2296 ecdsa_c[R_EC_P160][1] = count / 1000 / 2;
2297 for (i = R_EC_P192; i <= R_EC_P521; i++) {
2298 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2299 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
70c4e156 2300 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
0f113f3e
MC
2301 ecdsa_doit[i] = 0;
2302 else {
70c4e156 2303 if (ecdsa_c[i][0] == 0) {
0f113f3e
MC
2304 ecdsa_c[i][0] = 1;
2305 ecdsa_c[i][1] = 1;
2306 }
2307 }
2308 }
f5c99167 2309# ifndef OPENSSL_NO_EC2M
0f113f3e
MC
2310 ecdsa_c[R_EC_K163][0] = count / 1000;
2311 ecdsa_c[R_EC_K163][1] = count / 1000 / 2;
2312 for (i = R_EC_K233; i <= R_EC_K571; i++) {
2313 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2314 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
70c4e156 2315 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
0f113f3e
MC
2316 ecdsa_doit[i] = 0;
2317 else {
70c4e156 2318 if (ecdsa_c[i][0] == 0) {
0f113f3e
MC
2319 ecdsa_c[i][0] = 1;
2320 ecdsa_c[i][1] = 1;
2321 }
2322 }
2323 }
2324 ecdsa_c[R_EC_B163][0] = count / 1000;
2325 ecdsa_c[R_EC_B163][1] = count / 1000 / 2;
2326 for (i = R_EC_B233; i <= R_EC_B571; i++) {
2327 ecdsa_c[i][0] = ecdsa_c[i - 1][0] / 2;
2328 ecdsa_c[i][1] = ecdsa_c[i - 1][1] / 2;
70c4e156 2329 if (ecdsa_doit[i] <= 1 && ecdsa_c[i][0] == 0)
0f113f3e
MC
2330 ecdsa_doit[i] = 0;
2331 else {
70c4e156 2332 if (ecdsa_c[i][0] == 0) {
0f113f3e
MC
2333 ecdsa_c[i][0] = 1;
2334 ecdsa_c[i][1] = 1;
2335 }
2336 }
2337 }
f5c99167 2338# endif
7e1b7485 2339
0f113f3e 2340 ecdh_c[R_EC_P160][0] = count / 1000;
0f113f3e
MC
2341 for (i = R_EC_P192; i <= R_EC_P521; i++) {
2342 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
70c4e156 2343 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
0f113f3e
MC
2344 ecdh_doit[i] = 0;
2345 else {
70c4e156 2346 if (ecdh_c[i][0] == 0) {
0f113f3e 2347 ecdh_c[i][0] = 1;
0f113f3e
MC
2348 }
2349 }
2350 }
f5c99167 2351# ifndef OPENSSL_NO_EC2M
0f113f3e 2352 ecdh_c[R_EC_K163][0] = count / 1000;
0f113f3e
MC
2353 for (i = R_EC_K233; i <= R_EC_K571; i++) {
2354 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
70c4e156 2355 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
0f113f3e
MC
2356 ecdh_doit[i] = 0;
2357 else {
70c4e156 2358 if (ecdh_c[i][0] == 0) {
0f113f3e 2359 ecdh_c[i][0] = 1;
0f113f3e
MC
2360 }
2361 }
2362 }
2363 ecdh_c[R_EC_B163][0] = count / 1000;
0f113f3e
MC
2364 for (i = R_EC_B233; i <= R_EC_B571; i++) {
2365 ecdh_c[i][0] = ecdh_c[i - 1][0] / 2;
70c4e156 2366 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
0f113f3e
MC
2367 ecdh_doit[i] = 0;
2368 else {
70c4e156 2369 if (ecdh_c[i][0] == 0) {
0f113f3e 2370 ecdh_c[i][0] = 1;
0f113f3e
MC
2371 }
2372 }
2373 }
f5c99167 2374# endif
5c6a69f5
F
2375 /* repeated code good to factorize */
2376 ecdh_c[R_EC_BRP256R1][0] = count / 1000;
2377 for (i = R_EC_BRP384R1; i <= R_EC_BRP512R1; i += 2) {
2378 ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
2379 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2380 ecdh_doit[i] = 0;
2381 else {
2382 if (ecdh_c[i][0] == 0) {
2383 ecdh_c[i][0] = 1;
2384 }
2385 }
2386 }
2387 ecdh_c[R_EC_BRP256T1][0] = count / 1000;
2388 for (i = R_EC_BRP384T1; i <= R_EC_BRP512T1; i += 2) {
2389 ecdh_c[i][0] = ecdh_c[i - 2][0] / 2;
2390 if (ecdh_doit[i] <= 1 && ecdh_c[i][0] == 0)
2391 ecdh_doit[i] = 0;
2392 else {
2393 if (ecdh_c[i][0] == 0) {
2394 ecdh_c[i][0] = 1;
2395 }
2396 }
2397 }
2398 /* default iteration count for the last two EC Curves */
2399 ecdh_c[R_EC_X25519][0] = count / 1800;
2400 ecdh_c[R_EC_X448][0] = count / 7200;
d3a9fb10
PY
2401
2402 eddsa_c[R_EC_Ed25519][0] = count / 1800;
2403 eddsa_c[R_EC_Ed448][0] = count / 7200;
a56f68ad
PY
2404
2405# ifndef OPENSSL_NO_SM2
2406 sm2_c[R_EC_SM2P256][0] = count / 1800;
2407# endif
1352e0ff 2408# endif /* OPENSSL_NO_EC */
e172d60d 2409
60d3b5b9
HK
2410# ifndef OPENSSL_NO_DH
2411 ffdh_c[R_FFDH_2048][0] = count / 1000;
2412 for (i = R_FFDH_3072; i <= R_FFDH_8192; i++) {
2413 ffdh_c[i][0] = ffdh_c[i - 1][0] / 2;
2414 if (ffdh_doit[i] <= 1 && ffdh_c[i][0] == 0) {
2415 ffdh_doit[i] = 0;
2416 } else {
2417 if (ffdh_c[i][0] == 0)
2418 ffdh_c[i][0] = 1;
2419 }
2420 }
2421# endif /* OPENSSL_NO_DH */
2422
0f113f3e 2423# else
a00ae6c4
RS
2424/* not worth fixing */
2425# error "You cannot disable DES on systems without SIGALRM."
29dd15b1 2426# endif /* OPENSSL_NO_DES */
ffcca684
AP
2427#elif SIGALRM > 0
2428 signal(SIGALRM, alarmed);
29dd15b1 2429#endif /* SIGALRM */
0f113f3e 2430
58e1f3d6 2431#if !defined(OPENSSL_NO_MD2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 2432 if (doit[D_MD2]) {
64daf14d
PS
2433 for (testnum = 0; testnum < size_num; testnum++) {
2434 print_message(names[D_MD2], c[D_MD2][testnum], lengths[testnum],
2435 seconds.sym);
0f113f3e 2436 Time_F(START);
8b0b80d9 2437 count = run_benchmark(async_jobs, EVP_Digest_MD2_loop, loopargs);
0f113f3e 2438 d = Time_F(STOP);
8b0b80d9 2439 print_result(D_MD2, testnum, count, d);
0f113f3e
MC
2440 }
2441 }
a00ae6c4 2442#endif
33ee9ae0 2443#if !defined(OPENSSL_NO_MDC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 2444 if (doit[D_MDC2]) {
64daf14d
PS
2445 for (testnum = 0; testnum < size_num; testnum++) {
2446 print_message(names[D_MDC2], c[D_MDC2][testnum], lengths[testnum],
2447 seconds.sym);
0f113f3e 2448 Time_F(START);
8b0b80d9 2449 count = run_benchmark(async_jobs, EVP_Digest_MDC2_loop, loopargs);
0f113f3e 2450 d = Time_F(STOP);
8b0b80d9 2451 print_result(D_MDC2, testnum, count, d);
af0857f0
F
2452 if (count < 0)
2453 break;
0f113f3e
MC
2454 }
2455 }
a00ae6c4 2456#endif
d02b48c6 2457
8ffb20ce 2458#if !defined(OPENSSL_NO_MD4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 2459 if (doit[D_MD4]) {
64daf14d
PS
2460 for (testnum = 0; testnum < size_num; testnum++) {
2461 print_message(names[D_MD4], c[D_MD4][testnum], lengths[testnum],
2462 seconds.sym);
0f113f3e 2463 Time_F(START);
8b0b80d9 2464 count = run_benchmark(async_jobs, EVP_Digest_MD4_loop, loopargs);
0f113f3e 2465 d = Time_F(STOP);
8b0b80d9 2466 print_result(D_MD4, testnum, count, d);
af0857f0
F
2467 if (count < 0)
2468 break;
0f113f3e
MC
2469 }
2470 }
a00ae6c4 2471#endif
3009458e 2472
781aa7ab 2473#if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 2474 if (doit[D_MD5]) {
64daf14d
PS
2475 for (testnum = 0; testnum < size_num; testnum++) {
2476 print_message(names[D_MD5], c[D_MD5][testnum], lengths[testnum],
2477 seconds.sym);
0f113f3e 2478 Time_F(START);
8b0b80d9 2479 count = run_benchmark(async_jobs, MD5_loop, loopargs);
0f113f3e 2480 d = Time_F(STOP);
8b0b80d9 2481 print_result(D_MD5, testnum, count, d);
0f113f3e
MC
2482 }
2483 }
d02b48c6 2484
dbde4726 2485# ifndef OPENSSL_NO_DEPRECATED_3_0
0f113f3e 2486 if (doit[D_HMAC]) {
d6073e27 2487 static const char hmac_key[] = "This is a key...";
8829ce30
F
2488 int len = strlen(hmac_key);
2489
0ff43435 2490 for (i = 0; i < loopargs_len; i++) {
8b0b80d9
AG
2491 loopargs[i].hctx = HMAC_CTX_new();
2492 if (loopargs[i].hctx == NULL) {
2493 BIO_printf(bio_err, "HMAC malloc failure, exiting...");
2494 exit(1);
2495 }
0f113f3e 2496
8829ce30 2497 HMAC_Init_ex(loopargs[i].hctx, hmac_key, len, EVP_md5(), NULL);
bf7c6817 2498 }
64daf14d
PS
2499 for (testnum = 0; testnum < size_num; testnum++) {
2500 print_message(names[D_HMAC], c[D_HMAC][testnum], lengths[testnum],
2501 seconds.sym);
0f113f3e 2502 Time_F(START);
8b0b80d9 2503 count = run_benchmark(async_jobs, HMAC_loop, loopargs);
0f113f3e 2504 d = Time_F(STOP);
8b0b80d9
AG
2505 print_result(D_HMAC, testnum, count, d);
2506 }
d02b7e09 2507 for (i = 0; i < loopargs_len; i++)
8b0b80d9 2508 HMAC_CTX_free(loopargs[i].hctx);
0f113f3e 2509 }
dbde4726 2510# endif
a00ae6c4 2511#endif
85d843c8 2512#ifndef OPENSSL_NO_DEPRECATED_3_0
0f113f3e 2513 if (doit[D_SHA1]) {
64daf14d
PS
2514 for (testnum = 0; testnum < size_num; testnum++) {
2515 print_message(names[D_SHA1], c[D_SHA1][testnum], lengths[testnum],
2516 seconds.sym);
0f113f3e 2517 Time_F(START);
8b0b80d9 2518 count = run_benchmark(async_jobs, SHA1_loop, loopargs);
0f113f3e 2519 d = Time_F(STOP);
8b0b80d9 2520 print_result(D_SHA1, testnum, count, d);
0f113f3e
MC
2521 }
2522 }
0f113f3e 2523 if (doit[D_SHA256]) {
64daf14d 2524 for (testnum = 0; testnum < size_num; testnum++) {
29dd15b1 2525 print_message(names[D_SHA256], c[D_SHA256][testnum],
64daf14d 2526 lengths[testnum], seconds.sym);
0f113f3e 2527 Time_F(START);
8b0b80d9 2528 count = run_benchmark(async_jobs, SHA256_loop, loopargs);
0f113f3e 2529 d = Time_F(STOP);
8b0b80d9 2530 print_result(D_SHA256, testnum, count, d);
0f113f3e
MC
2531 }
2532 }
0f113f3e 2533 if (doit[D_SHA512]) {
64daf14d 2534 for (testnum = 0; testnum < size_num; testnum++) {
29dd15b1 2535 print_message(names[D_SHA512], c[D_SHA512][testnum],
64daf14d 2536 lengths[testnum], seconds.sym);
0f113f3e 2537 Time_F(START);
8b0b80d9 2538 count = run_benchmark(async_jobs, SHA512_loop, loopargs);
0f113f3e 2539 d = Time_F(STOP);
8b0b80d9 2540 print_result(D_SHA512, testnum, count, d);
0f113f3e
MC
2541 }
2542 }
85d843c8 2543#endif
7c3aa39f 2544#if !defined(OPENSSL_NO_WHIRLPOOL) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 2545 if (doit[D_WHIRLPOOL]) {
64daf14d 2546 for (testnum = 0; testnum < size_num; testnum++) {
29dd15b1 2547 print_message(names[D_WHIRLPOOL], c[D_WHIRLPOOL][testnum],
64daf14d 2548 lengths[testnum], seconds.sym);
0f113f3e 2549 Time_F(START);
8b0b80d9 2550 count = run_benchmark(async_jobs, WHIRLPOOL_loop, loopargs);
0f113f3e 2551 d = Time_F(STOP);
8b0b80d9 2552 print_result(D_WHIRLPOOL, testnum, count, d);
0f113f3e
MC
2553 }
2554 }
a00ae6c4 2555#endif
c88f8f76 2556
601fca17 2557#if !defined(OPENSSL_NO_RMD160) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 2558 if (doit[D_RMD160]) {
64daf14d 2559 for (testnum = 0; testnum < size_num; testnum++) {
29dd15b1 2560 print_message(names[D_RMD160], c[D_RMD160][testnum],
64daf14d 2561 lengths[testnum], seconds.sym);
0f113f3e 2562 Time_F(START);
8b0b80d9 2563 count = run_benchmark(async_jobs, EVP_Digest_RMD160_loop, loopargs);
0f113f3e 2564 d = Time_F(STOP);
8b0b80d9 2565 print_result(D_RMD160, testnum, count, d);
af0857f0
F
2566 if (count < 0)
2567 break;
0f113f3e
MC
2568 }
2569 }
a00ae6c4 2570#endif
a8fca728 2571#if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 2572 if (doit[D_RC4]) {
64daf14d
PS
2573 for (testnum = 0; testnum < size_num; testnum++) {
2574 print_message(names[D_RC4], c[D_RC4][testnum], lengths[testnum],
2575 seconds.sym);
0f113f3e 2576 Time_F(START);
8b0b80d9 2577 count = run_benchmark(async_jobs, RC4_loop, loopargs);
0f113f3e 2578 d = Time_F(STOP);
8b0b80d9 2579 print_result(D_RC4, testnum, count, d);
0f113f3e
MC
2580 }
2581 }
a00ae6c4 2582#endif
c6fec81b 2583#if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 2584 if (doit[D_CBC_DES]) {
64daf14d 2585 for (testnum = 0; testnum < size_num; testnum++) {
29dd15b1 2586 print_message(names[D_CBC_DES], c[D_CBC_DES][testnum],
64daf14d 2587 lengths[testnum], seconds.sym);
0f113f3e 2588 Time_F(START);
8b0b80d9 2589 count = run_benchmark(async_jobs, DES_ncbc_encrypt_loop, loopargs);
0f113f3e 2590 d = Time_F(STOP);
8b0b80d9 2591 print_result(D_CBC_DES, testnum, count, d);
0f113f3e
MC
2592 }
2593 }
ae93dc13 2594
0f113f3e 2595 if (doit[D_EDE3_DES]) {
64daf14d 2596 for (testnum = 0; testnum < size_num; testnum++) {
29dd15b1 2597 print_message(names[D_EDE3_DES], c[D_EDE3_DES][testnum],
64daf14d 2598 lengths[testnum], seconds.sym);
0f113f3e 2599 Time_F(START);
29dd15b1
NT
2600 count =
2601 run_benchmark(async_jobs, DES_ede3_cbc_encrypt_loop, loopargs);
0f113f3e 2602 d = Time_F(STOP);
8b0b80d9 2603 print_result(D_EDE3_DES, testnum, count, d);
0f113f3e
MC
2604 }
2605 }
a00ae6c4 2606#endif
5158c763 2607
c72fa255 2608#ifndef OPENSSL_NO_DEPRECATED_3_0
0f113f3e 2609 if (doit[D_CBC_128_AES]) {
64daf14d 2610 for (testnum = 0; testnum < size_num; testnum++) {
8b0b80d9 2611 print_message(names[D_CBC_128_AES], c[D_CBC_128_AES][testnum],
64daf14d 2612 lengths[testnum], seconds.sym);
0f113f3e 2613 Time_F(START);
29dd15b1
NT
2614 count =
2615 run_benchmark(async_jobs, AES_cbc_128_encrypt_loop, loopargs);
0f113f3e 2616 d = Time_F(STOP);
8b0b80d9 2617 print_result(D_CBC_128_AES, testnum, count, d);
0f113f3e
MC
2618 }
2619 }
2620 if (doit[D_CBC_192_AES]) {
64daf14d 2621 for (testnum = 0; testnum < size_num; testnum++) {
8b0b80d9 2622 print_message(names[D_CBC_192_AES], c[D_CBC_192_AES][testnum],
64daf14d 2623 lengths[testnum], seconds.sym);
0f113f3e 2624 Time_F(START);
29dd15b1
NT
2625 count =
2626 run_benchmark(async_jobs, AES_cbc_192_encrypt_loop, loopargs);
0f113f3e 2627 d = Time_F(STOP);
8b0b80d9 2628 print_result(D_CBC_192_AES, testnum, count, d);
0f113f3e
MC
2629 }
2630 }
2631 if (doit[D_CBC_256_AES]) {
64daf14d 2632 for (testnum = 0; testnum < size_num; testnum++) {
8b0b80d9 2633 print_message(names[D_CBC_256_AES], c[D_CBC_256_AES][testnum],
64daf14d 2634 lengths[testnum], seconds.sym);
0f113f3e 2635 Time_F(START);
29dd15b1
NT
2636 count =
2637 run_benchmark(async_jobs, AES_cbc_256_encrypt_loop, loopargs);
0f113f3e 2638 d = Time_F(STOP);
8b0b80d9 2639 print_result(D_CBC_256_AES, testnum, count, d);
0f113f3e
MC
2640 }
2641 }
5f09d0ec 2642
c72fa255 2643
0f113f3e 2644 if (doit[D_IGE_128_AES]) {
64daf14d 2645 for (testnum = 0; testnum < size_num; testnum++) {
8b0b80d9 2646 print_message(names[D_IGE_128_AES], c[D_IGE_128_AES][testnum],
64daf14d 2647 lengths[testnum], seconds.sym);
0f113f3e 2648 Time_F(START);
29dd15b1
NT
2649 count =
2650 run_benchmark(async_jobs, AES_ige_128_encrypt_loop, loopargs);
0f113f3e 2651 d = Time_F(STOP);
8b0b80d9 2652 print_result(D_IGE_128_AES, testnum, count, d);
0f113f3e
MC
2653 }
2654 }
2655 if (doit[D_IGE_192_AES]) {
64daf14d 2656 for (testnum = 0; testnum < size_num; testnum++) {
8b0b80d9 2657 print_message(names[D_IGE_192_AES], c[D_IGE_192_AES][testnum],
64daf14d 2658 lengths[testnum], seconds.sym);
0f113f3e 2659 Time_F(START);
29dd15b1
NT
2660 count =
2661 run_benchmark(async_jobs, AES_ige_192_encrypt_loop, loopargs);
0f113f3e 2662 d = Time_F(STOP);
8b0b80d9 2663 print_result(D_IGE_192_AES, testnum, count, d);
0f113f3e
MC
2664 }
2665 }
2666 if (doit[D_IGE_256_AES]) {
64daf14d 2667 for (testnum = 0; testnum < size_num; testnum++) {
8b0b80d9 2668 print_message(names[D_IGE_256_AES], c[D_IGE_256_AES][testnum],
64daf14d 2669 lengths[testnum], seconds.sym);
0f113f3e 2670 Time_F(START);
29dd15b1
NT
2671 count =
2672 run_benchmark(async_jobs, AES_ige_256_encrypt_loop, loopargs);
0f113f3e 2673 d = Time_F(STOP);
8b0b80d9 2674 print_result(D_IGE_256_AES, testnum, count, d);
0f113f3e
MC
2675 }
2676 }
2677 if (doit[D_GHASH]) {
0ff43435 2678 for (i = 0; i < loopargs_len; i++) {
29dd15b1
NT
2679 loopargs[i].gcm_ctx =
2680 CRYPTO_gcm128_new(&aes_ks1, (block128_f) AES_encrypt);
2681 CRYPTO_gcm128_setiv(loopargs[i].gcm_ctx,
2682 (unsigned char *)"0123456789ab", 12);
8b0b80d9 2683 }
0f113f3e 2684
64daf14d 2685 for (testnum = 0; testnum < size_num; testnum++) {
29dd15b1 2686 print_message(names[D_GHASH], c[D_GHASH][testnum],
64daf14d 2687 lengths[testnum], seconds.sym);
0f113f3e 2688 Time_F(START);
8b0b80d9 2689 count = run_benchmark(async_jobs, CRYPTO_gcm128_aad_loop, loopargs);
0f113f3e 2690 d = Time_F(STOP);
8b0b80d9 2691 print_result(D_GHASH, testnum, count, d);
0f113f3e 2692 }
0ff43435 2693 for (i = 0; i < loopargs_len; i++)
8b0b80d9 2694 CRYPTO_gcm128_release(loopargs[i].gcm_ctx);
0f113f3e 2695 }
c72fa255 2696#endif /* OPENSSL_NO_DEPRECATED_3_0 */
291850b4 2697#if !defined(OPENSSL_NO_CAMELLIA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 2698 if (doit[D_CBC_128_CML]) {
e5972607
F
2699 if (async_jobs > 0) {
2700 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2701 names[D_CBC_128_CML]);
2702 doit[D_CBC_128_CML] = 0;
2703 }
64daf14d 2704 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
8b0b80d9 2705 print_message(names[D_CBC_128_CML], c[D_CBC_128_CML][testnum],
64daf14d 2706 lengths[testnum], seconds.sym);
0f113f3e 2707 Time_F(START);
f3fdfbf7 2708 for (count = 0; COND(c[D_CBC_128_CML][testnum]); count++)
8b0b80d9 2709 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
0609658f 2710 (size_t)lengths[testnum], &camellia_ks[0],
0f113f3e
MC
2711 iv, CAMELLIA_ENCRYPT);
2712 d = Time_F(STOP);
8b0b80d9 2713 print_result(D_CBC_128_CML, testnum, count, d);
0f113f3e
MC
2714 }
2715 }
2716 if (doit[D_CBC_192_CML]) {
e5972607
F
2717 if (async_jobs > 0) {
2718 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2719 names[D_CBC_192_CML]);
2720 doit[D_CBC_192_CML] = 0;
2721 }
64daf14d 2722 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
8b0b80d9 2723 print_message(names[D_CBC_192_CML], c[D_CBC_192_CML][testnum],
64daf14d 2724 lengths[testnum], seconds.sym);
8b0b80d9
AG
2725 if (async_jobs > 0) {
2726 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2727 exit(1);
2728 }
0f113f3e 2729 Time_F(START);
f3fdfbf7 2730 for (count = 0; COND(c[D_CBC_192_CML][testnum]); count++)
8b0b80d9 2731 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
0609658f 2732 (size_t)lengths[testnum], &camellia_ks[1],
0f113f3e
MC
2733 iv, CAMELLIA_ENCRYPT);
2734 d = Time_F(STOP);
8b0b80d9 2735 print_result(D_CBC_192_CML, testnum, count, d);
0f113f3e
MC
2736 }
2737 }
2738 if (doit[D_CBC_256_CML]) {
e5972607
F
2739 if (async_jobs > 0) {
2740 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2741 names[D_CBC_256_CML]);
2742 doit[D_CBC_256_CML] = 0;
2743 }
64daf14d 2744 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
8b0b80d9 2745 print_message(names[D_CBC_256_CML], c[D_CBC_256_CML][testnum],
64daf14d 2746 lengths[testnum], seconds.sym);
0f113f3e 2747 Time_F(START);
f3fdfbf7 2748 for (count = 0; COND(c[D_CBC_256_CML][testnum]); count++)
8b0b80d9 2749 Camellia_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
0609658f 2750 (size_t)lengths[testnum], &camellia_ks[2],
0f113f3e
MC
2751 iv, CAMELLIA_ENCRYPT);
2752 d = Time_F(STOP);
8b0b80d9 2753 print_result(D_CBC_256_CML, testnum, count, d);
0f113f3e
MC
2754 }
2755 }
a00ae6c4 2756#endif
da2d32f6 2757#if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 2758 if (doit[D_CBC_IDEA]) {
e5972607
F
2759 if (async_jobs > 0) {
2760 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2761 names[D_CBC_IDEA]);
2762 doit[D_CBC_IDEA] = 0;
2763 }
64daf14d 2764 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
29dd15b1 2765 print_message(names[D_CBC_IDEA], c[D_CBC_IDEA][testnum],
64daf14d 2766 lengths[testnum], seconds.sym);
0f113f3e 2767 Time_F(START);
f3fdfbf7 2768 for (count = 0; COND(c[D_CBC_IDEA][testnum]); count++)
9021a5df 2769 IDEA_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
8829ce30 2770 (size_t)lengths[testnum], &idea_ks,
0f113f3e
MC
2771 iv, IDEA_ENCRYPT);
2772 d = Time_F(STOP);
8b0b80d9 2773 print_result(D_CBC_IDEA, testnum, count, d);
0f113f3e
MC
2774 }
2775 }
a00ae6c4 2776#endif
28c690cb 2777#if !defined(OPENSSL_NO_SEED) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 2778 if (doit[D_CBC_SEED]) {
e5972607
F
2779 if (async_jobs > 0) {
2780 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2781 names[D_CBC_SEED]);
2782 doit[D_CBC_SEED] = 0;
2783 }
64daf14d 2784 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
29dd15b1 2785 print_message(names[D_CBC_SEED], c[D_CBC_SEED][testnum],
64daf14d 2786 lengths[testnum], seconds.sym);
0f113f3e 2787 Time_F(START);
f3fdfbf7 2788 for (count = 0; COND(c[D_CBC_SEED][testnum]); count++)
8b0b80d9 2789 SEED_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
8829ce30 2790 (size_t)lengths[testnum], &seed_ks, iv, 1);
0f113f3e 2791 d = Time_F(STOP);
8b0b80d9 2792 print_result(D_CBC_SEED, testnum, count, d);
0f113f3e
MC
2793 }
2794 }
a00ae6c4 2795#endif
ee2993ab 2796#if !defined(OPENSSL_NO_RC2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 2797 if (doit[D_CBC_RC2]) {
e5972607
F
2798 if (async_jobs > 0) {
2799 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2800 names[D_CBC_RC2]);
2801 doit[D_CBC_RC2] = 0;
2802 }
64daf14d 2803 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
29dd15b1 2804 print_message(names[D_CBC_RC2], c[D_CBC_RC2][testnum],
64daf14d 2805 lengths[testnum], seconds.sym);
8b0b80d9
AG
2806 if (async_jobs > 0) {
2807 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2808 exit(1);
2809 }
0f113f3e 2810 Time_F(START);
f3fdfbf7 2811 for (count = 0; COND(c[D_CBC_RC2][testnum]); count++)
8b0b80d9 2812 RC2_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
8829ce30 2813 (size_t)lengths[testnum], &rc2_ks,
0f113f3e
MC
2814 iv, RC2_ENCRYPT);
2815 d = Time_F(STOP);
8b0b80d9 2816 print_result(D_CBC_RC2, testnum, count, d);
0f113f3e
MC
2817 }
2818 }
a00ae6c4 2819#endif
62c3fed0 2820#if !defined(OPENSSL_NO_RC5) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 2821 if (doit[D_CBC_RC5]) {
e5972607
F
2822 if (async_jobs > 0) {
2823 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2824 names[D_CBC_RC5]);
2825 doit[D_CBC_RC5] = 0;
2826 }
64daf14d 2827 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
29dd15b1 2828 print_message(names[D_CBC_RC5], c[D_CBC_RC5][testnum],
64daf14d 2829 lengths[testnum], seconds.sym);
8b0b80d9
AG
2830 if (async_jobs > 0) {
2831 BIO_printf(bio_err, "Async mode is not supported, exiting...");
2832 exit(1);
2833 }
0f113f3e 2834 Time_F(START);
f3fdfbf7 2835 for (count = 0; COND(c[D_CBC_RC5][testnum]); count++)
8b0b80d9 2836 RC5_32_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
8829ce30 2837 (size_t)lengths[testnum], &rc5_ks,
0f113f3e
MC
2838 iv, RC5_ENCRYPT);
2839 d = Time_F(STOP);
8b0b80d9 2840 print_result(D_CBC_RC5, testnum, count, d);
0f113f3e
MC
2841 }
2842 }
a00ae6c4 2843#endif
03047e7b 2844#if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 2845 if (doit[D_CBC_BF]) {
e5972607
F
2846 if (async_jobs > 0) {
2847 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2848 names[D_CBC_BF]);
2849 doit[D_CBC_BF] = 0;
2850 }
64daf14d 2851 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
29dd15b1 2852 print_message(names[D_CBC_BF], c[D_CBC_BF][testnum],
64daf14d 2853 lengths[testnum], seconds.sym);
0f113f3e 2854 Time_F(START);
f3fdfbf7 2855 for (count = 0; COND(c[D_CBC_BF][testnum]); count++)
8b0b80d9 2856 BF_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
8829ce30 2857 (size_t)lengths[testnum], &bf_ks,
0f113f3e
MC
2858 iv, BF_ENCRYPT);
2859 d = Time_F(STOP);
8b0b80d9 2860 print_result(D_CBC_BF, testnum, count, d);
0f113f3e
MC
2861 }
2862 }
a00ae6c4 2863#endif
0ae5d4d6 2864#if !defined(OPENSSL_NO_CAST) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 2865 if (doit[D_CBC_CAST]) {
e5972607
F
2866 if (async_jobs > 0) {
2867 BIO_printf(bio_err, "Async mode is not supported with %s\n",
2868 names[D_CBC_CAST]);
2869 doit[D_CBC_CAST] = 0;
2870 }
64daf14d 2871 for (testnum = 0; testnum < size_num && async_init == 0; testnum++) {
29dd15b1 2872 print_message(names[D_CBC_CAST], c[D_CBC_CAST][testnum],
64daf14d 2873 lengths[testnum], seconds.sym);
0f113f3e 2874 Time_F(START);
f3fdfbf7 2875 for (count = 0; COND(c[D_CBC_CAST][testnum]); count++)
8b0b80d9 2876 CAST_cbc_encrypt(loopargs[0].buf, loopargs[0].buf,
8829ce30 2877 (size_t)lengths[testnum], &cast_ks,
0f113f3e
MC
2878 iv, CAST_ENCRYPT);
2879 d = Time_F(STOP);
8b0b80d9 2880 print_result(D_CBC_CAST, testnum, count, d);
0f113f3e
MC
2881 }
2882 }
a00ae6c4 2883#endif
65e6b9a4 2884 if (doit[D_RAND]) {
64daf14d
PS
2885 for (testnum = 0; testnum < size_num; testnum++) {
2886 print_message(names[D_RAND], c[D_RAND][testnum], lengths[testnum],
2887 seconds.sym);
65e6b9a4
PS
2888 Time_F(START);
2889 count = run_benchmark(async_jobs, RAND_bytes_loop, loopargs);
2890 d = Time_F(STOP);
2891 print_result(D_RAND, testnum, count, d);
2892 }
2893 }
f3dea9a5 2894
0f113f3e 2895 if (doit[D_EVP]) {
44ca7565 2896 if (evp_cipher != NULL) {
d02b7e09 2897 int (*loopfunc) (void *) = EVP_Update_loop;
44ca7565
AP
2898
2899 if (multiblock && (EVP_CIPHER_flags(evp_cipher) &
2900 EVP_CIPH_FLAG_TLS1_1_MULTIBLOCK)) {
2901 multiblock_speed(evp_cipher, lengths_single, &seconds);
2902 ret = 0;
0f113f3e
MC
2903 goto end;
2904 }
44ca7565
AP
2905
2906 names[D_EVP] = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
2907
2908 if (EVP_CIPHER_mode(evp_cipher) == EVP_CIPH_CCM_MODE) {
2909 loopfunc = EVP_Update_loop_ccm;
2910 } else if (aead && (EVP_CIPHER_flags(evp_cipher) &
2911 EVP_CIPH_FLAG_AEAD_CIPHER)) {
2912 loopfunc = EVP_Update_loop_aead;
2913 if (lengths == lengths_list) {
2914 lengths = aead_lengths_list;
2915 size_num = OSSL_NELEM(aead_lengths_list);
2916 }
8b0b80d9 2917 }
0f113f3e 2918
44ca7565 2919 for (testnum = 0; testnum < size_num; testnum++) {
d02b7e09 2920 print_message(names[D_EVP], c[D_EVP][testnum], lengths[testnum],
64daf14d 2921 seconds.sym);
8b0b80d9
AG
2922
2923 for (k = 0; k < loopargs_len; k++) {
2924 loopargs[k].ctx = EVP_CIPHER_CTX_new();
5d238a10
BE
2925 if (loopargs[k].ctx == NULL) {
2926 BIO_printf(bio_err, "\nEVP_CIPHER_CTX_new failure\n");
2927 exit(1);
2928 }
2929 if (!EVP_CipherInit_ex(loopargs[k].ctx, evp_cipher, NULL,
2930 NULL, iv, decrypt ? 0 : 1)) {
2931 BIO_printf(bio_err, "\nEVP_CipherInit_ex failure\n");
2932 ERR_print_errors(bio_err);
2933 exit(1);
2934 }
6b1fe3d0 2935
8b0b80d9 2936 EVP_CIPHER_CTX_set_padding(loopargs[k].ctx, 0);
6b1fe3d0
PS
2937
2938 keylen = EVP_CIPHER_CTX_key_length(loopargs[k].ctx);
2939 loopargs[k].key = app_malloc(keylen, "evp_cipher key");
2940 EVP_CIPHER_CTX_rand_key(loopargs[k].ctx, loopargs[k].key);
5d238a10
BE
2941 if (!EVP_CipherInit_ex(loopargs[k].ctx, NULL, NULL,
2942 loopargs[k].key, NULL, -1)) {
2943 BIO_printf(bio_err, "\nEVP_CipherInit_ex failure\n");
2944 ERR_print_errors(bio_err);
2945 exit(1);
2946 }
6b1fe3d0 2947 OPENSSL_clear_free(loopargs[k].key, keylen);
b1ceb439
TS
2948
2949 /* SIV mode only allows for a single Update operation */
2950 if (EVP_CIPHER_mode(evp_cipher) == EVP_CIPH_SIV_MODE)
2951 EVP_CIPHER_CTX_ctrl(loopargs[k].ctx, EVP_CTRL_SET_SPEED, 1, NULL);
8b0b80d9 2952 }
0f113f3e
MC
2953
2954 Time_F(START);
fe4f66d2 2955 count = run_benchmark(async_jobs, loopfunc, loopargs);
0f113f3e 2956 d = Time_F(STOP);
8b0b80d9
AG
2957 for (k = 0; k < loopargs_len; k++) {
2958 EVP_CIPHER_CTX_free(loopargs[k].ctx);
2959 }
44ca7565 2960 print_result(D_EVP, testnum, count, d);
0f113f3e 2961 }
44ca7565
AP
2962 } else if (evp_md != NULL) {
2963 names[D_EVP] = OBJ_nid2ln(EVP_MD_type(evp_md));
2964
2965 for (testnum = 0; testnum < size_num; testnum++) {
d02b7e09 2966 print_message(names[D_EVP], c[D_EVP][testnum], lengths[testnum],
64daf14d 2967 seconds.sym);
0f113f3e 2968 Time_F(START);
8b0b80d9 2969 count = run_benchmark(async_jobs, EVP_Digest_loop, loopargs);
0f113f3e 2970 d = Time_F(STOP);
44ca7565 2971 print_result(D_EVP, testnum, count, d);
0f113f3e 2972 }
0f113f3e
MC
2973 }
2974 }
7e1b7485 2975
dbde4726 2976#ifndef OPENSSL_NO_DEPRECATED_3_0
d02b7e09
F
2977 if (doit[D_EVP_HMAC] && evp_hmac_md != NULL) {
2978 const char *md_name = OBJ_nid2ln(EVP_MD_type(evp_hmac_md));
1352e0ff 2979
d02b7e09
F
2980 evp_hmac_name = app_malloc(sizeof("HMAC()") + strlen(md_name),
2981 "HMAC name");
2982 sprintf(evp_hmac_name, "HMAC(%s)", md_name);
2983 names[D_EVP_HMAC] = evp_hmac_name;
f88b9b79 2984
d02b7e09
F
2985 for (testnum = 0; testnum < size_num; testnum++) {
2986 print_message(names[D_EVP_HMAC], c[D_EVP_HMAC][testnum], lengths[testnum],
2987 seconds.sym);
2988 Time_F(START);
2989 count = run_benchmark(async_jobs, EVP_HMAC_loop, loopargs);
2990 d = Time_F(STOP);
2991 print_result(D_EVP_HMAC, testnum, count, d);
f88b9b79
P
2992 }
2993 }
dbde4726 2994#endif
f88b9b79 2995
a6d572e6 2996#if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
d02b7e09
F
2997 if (doit[D_EVP_CMAC] && evp_cmac_cipher != NULL) {
2998 const char *cipher_name = OBJ_nid2ln(EVP_CIPHER_type(evp_cmac_cipher));
2999
3000 evp_cmac_name = app_malloc(sizeof("CMAC()") + strlen(cipher_name),
3001 "CMAC name");
3002 sprintf(evp_cmac_name, "CMAC(%s)", cipher_name);
3003 names[D_EVP_CMAC] = evp_cmac_name;
3004
3005 for (i = 0; i < loopargs_len; i++) {
3006 loopargs[i].cmac_ctx = CMAC_CTX_new();
3007 if (loopargs[i].cmac_ctx == NULL) {
3008 BIO_printf(bio_err, "CMAC malloc failure, exiting...");
3009 exit(1);
9bba2c4c 3010 }
9bba2c4c 3011 }
d02b7e09
F
3012 for (testnum = 0; testnum < size_num; testnum++) {
3013 print_message(names[D_EVP_CMAC], c[D_EVP_CMAC][testnum], lengths[testnum],
3014 seconds.sym);
3015 Time_F(START);
3016 count = run_benchmark(async_jobs, EVP_CMAC_loop, loopargs);
3017 d = Time_F(STOP);
3018 print_result(D_EVP_CMAC, testnum, count, d);
3019 }
3020 for (i = 0; i < loopargs_len; i++)
3021 CMAC_CTX_free(loopargs[i].cmac_ctx);
9bba2c4c
BE
3022 }
3023#endif
3024
0ff43435 3025 for (i = 0; i < loopargs_len; i++)
3445872e 3026 if (RAND_bytes(loopargs[i].buf, 36) <= 0)
3027 goto end;
8b0b80d9 3028
3a1ee3c1 3029#ifndef OPENSSL_NO_DEPRECATED_3_0
8b0b80d9
AG
3030 for (testnum = 0; testnum < RSA_NUM; testnum++) {
3031 int st = 0;
3032 if (!rsa_doit[testnum])
0f113f3e 3033 continue;
0ff43435 3034 for (i = 0; i < loopargs_len; i++) {
1352e0ff 3035 if (primes > RSA_DEFAULT_PRIME_NUM) {
665d899f
PY
3036 /* we haven't set keys yet, generate multi-prime RSA keys */
3037 BIGNUM *bn = BN_new();
3038
3039 if (bn == NULL)
3040 goto end;
3041 if (!BN_set_word(bn, RSA_F4)) {
3042 BN_free(bn);
3043 goto end;
3044 }
3045
3046 BIO_printf(bio_err, "Generate multi-prime RSA key for %s\n",
3047 rsa_choices[testnum].name);
3048
3049 loopargs[i].rsa_key[testnum] = RSA_new();
3050 if (loopargs[i].rsa_key[testnum] == NULL) {
3051 BN_free(bn);
3052 goto end;
3053 }
3054
3055 if (!RSA_generate_multi_prime_key(loopargs[i].rsa_key[testnum],
d63d89ea 3056 rsa_keys[testnum].bits,
665d899f
PY
3057 primes, bn, NULL)) {
3058 BN_free(bn);
3059 goto end;
3060 }
3061 BN_free(bn);
3062 }
0ff43435 3063 st = RSA_sign(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
0930e07d 3064 &loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
8b0b80d9
AG
3065 if (st == 0)
3066 break;
3067 }
7e1b7485 3068 if (st == 0) {
0f113f3e
MC
3069 BIO_printf(bio_err,
3070 "RSA sign failure. No RSA sign will be done.\n");
3071 ERR_print_errors(bio_err);
9d0854f4 3072 op_count = 1;
0f113f3e
MC
3073 } else {
3074 pkey_print_message("private", "rsa",
d63d89ea 3075 rsa_c[testnum][0], rsa_keys[testnum].bits,
64daf14d 3076 seconds.rsa);
8b0b80d9 3077 /* RSA_blinding_on(rsa_key[testnum],NULL); */
0f113f3e 3078 Time_F(START);
8b0b80d9 3079 count = run_benchmark(async_jobs, RSA_sign_loop, loopargs);
0f113f3e
MC
3080 d = Time_F(STOP);
3081 BIO_printf(bio_err,
3082 mr ? "+R1:%ld:%d:%.2f\n"
48bc0d99 3083 : "%ld %u bits private RSA's in %.2fs\n",
d63d89ea 3084 count, rsa_keys[testnum].bits, d);
8ac2d1ab 3085 rsa_results[testnum][0] = (double)count / d;
9d0854f4 3086 op_count = count;
0f113f3e 3087 }
d02b48c6 3088
0ff43435
AG
3089 for (i = 0; i < loopargs_len; i++) {
3090 st = RSA_verify(NID_md5_sha1, loopargs[i].buf, 36, loopargs[i].buf2,
0930e07d 3091 loopargs[i].siglen, loopargs[i].rsa_key[testnum]);
8b0b80d9
AG
3092 if (st <= 0)
3093 break;
3094 }
7e1b7485 3095 if (st <= 0) {
0f113f3e
MC
3096 BIO_printf(bio_err,
3097 "RSA verify failure. No RSA verify will be done.\n");
3098 ERR_print_errors(bio_err);
8b0b80d9 3099 rsa_doit[testnum] = 0;
0f113f3e
MC
3100 } else {
3101 pkey_print_message("public", "rsa",
d63d89ea 3102 rsa_c[testnum][1], rsa_keys[testnum].bits,
64daf14d 3103 seconds.rsa);
0f113f3e 3104 Time_F(START);
8b0b80d9 3105 count = run_benchmark(async_jobs, RSA_verify_loop, loopargs);
0f113f3e
MC
3106 d = Time_F(STOP);
3107 BIO_printf(bio_err,
3108 mr ? "+R2:%ld:%d:%.2f\n"
48bc0d99 3109 : "%ld %u bits public RSA's in %.2fs\n",
d63d89ea 3110 count, rsa_keys[testnum].bits, d);
8ac2d1ab 3111 rsa_results[testnum][1] = (double)count / d;
0f113f3e 3112 }
d02b48c6 3113
9d0854f4 3114 if (op_count <= 1) {
0f113f3e 3115 /* if longer than 10s, don't do any more */
f607f6ea 3116 stop_it(rsa_doit, testnum);
0f113f3e
MC
3117 }
3118 }
3a1ee3c1 3119#endif /* OPENSSL_NO_DEPRECATED_3_0 */
d02b48c6 3120
0ff43435 3121 for (i = 0; i < loopargs_len; i++)
3445872e 3122 if (RAND_bytes(loopargs[i].buf, 36) <= 0)
3123 goto end;
8b0b80d9 3124
1ddf2594 3125#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
8b0b80d9
AG
3126 for (testnum = 0; testnum < DSA_NUM; testnum++) {
3127 int st = 0;
3128 if (!dsa_doit[testnum])
0f113f3e
MC
3129 continue;
3130
8b0b80d9
AG
3131 /* DSA_generate_key(dsa_key[testnum]); */
3132 /* DSA_sign_setup(dsa_key[testnum],NULL); */
0ff43435
AG
3133 for (i = 0; i < loopargs_len; i++) {
3134 st = DSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
0930e07d 3135 &loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
8b0b80d9
AG
3136 if (st == 0)
3137 break;
3138 }
7e1b7485 3139 if (st == 0) {
0f113f3e
MC
3140 BIO_printf(bio_err,
3141 "DSA sign failure. No DSA sign will be done.\n");
3142 ERR_print_errors(bio_err);
9d0854f4 3143 op_count = 1;
0f113f3e
MC
3144 } else {
3145 pkey_print_message("sign", "dsa",
29dd15b1 3146 dsa_c[testnum][0], dsa_bits[testnum],
64daf14d 3147 seconds.dsa);
0f113f3e 3148 Time_F(START);
8b0b80d9 3149 count = run_benchmark(async_jobs, DSA_sign_loop, loopargs);
0f113f3e
MC
3150 d = Time_F(STOP);
3151 BIO_printf(bio_err,
48bc0d99
F
3152 mr ? "+R3:%ld:%u:%.2f\n"
3153 : "%ld %u bits DSA signs in %.2fs\n",
8b0b80d9 3154 count, dsa_bits[testnum], d);
0d4de756 3155 dsa_results[testnum][0] = (double)count / d;
9d0854f4 3156 op_count = count;
0f113f3e 3157 }
e172d60d 3158
0ff43435
AG
3159 for (i = 0; i < loopargs_len; i++) {
3160 st = DSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
0930e07d 3161 loopargs[i].siglen, loopargs[i].dsa_key[testnum]);
8b0b80d9
AG
3162 if (st <= 0)
3163 break;
3164 }
7e1b7485 3165 if (st <= 0) {
0f113f3e
MC
3166 BIO_printf(bio_err,
3167 "DSA verify failure. No DSA verify will be done.\n");
3168 ERR_print_errors(bio_err);
8b0b80d9 3169 dsa_doit[testnum] = 0;
0f113f3e
MC
3170 } else {
3171 pkey_print_message("verify", "dsa",
29dd15b1 3172 dsa_c[testnum][1], dsa_bits[testnum],
64daf14d 3173 seconds.dsa);
0f113f3e 3174 Time_F(START);
8b0b80d9 3175 count = run_benchmark(async_jobs, DSA_verify_loop, loopargs);
0f113f3e
MC
3176 d = Time_F(STOP);
3177 BIO_printf(bio_err,
48bc0d99
F
3178 mr ? "+R4:%ld:%u:%.2f\n"
3179 : "%ld %u bits DSA verify in %.2fs\n",
8b0b80d9 3180 count, dsa_bits[testnum], d);
0d4de756 3181 dsa_results[testnum][1] = (double)count / d;
0f113f3e 3182 }
e172d60d 3183
9d0854f4 3184 if (op_count <= 1) {
0f113f3e 3185 /* if longer than 10s, don't do any more */
f607f6ea 3186 stop_it(dsa_doit, testnum);
0f113f3e
MC
3187 }
3188 }
d6073e27 3189#endif /* OPENSSL_NO_DSA */
e172d60d 3190
10bf4fc2 3191#ifndef OPENSSL_NO_EC
579422c8 3192# ifndef OPENSSL_NO_DEPRECATED_3_0
5c6a69f5 3193 for (testnum = 0; testnum < ECDSA_NUM; testnum++) {
0ff43435 3194 int st = 1;
0f113f3e 3195
8b0b80d9 3196 if (!ecdsa_doit[testnum])
0f113f3e 3197 continue; /* Ignore Curve */
0ff43435 3198 for (i = 0; i < loopargs_len; i++) {
29dd15b1 3199 loopargs[i].ecdsa[testnum] =
d63d89ea 3200 EC_KEY_new_by_curve_name(ec_curves[testnum].nid);
0ff43435
AG
3201 if (loopargs[i].ecdsa[testnum] == NULL) {
3202 st = 0;
3203 break;
3204 }
3205 }
3206 if (st == 0) {
0f113f3e
MC
3207 BIO_printf(bio_err, "ECDSA failure.\n");
3208 ERR_print_errors(bio_err);
9d0854f4 3209 op_count = 1;
0f113f3e 3210 } else {
0ff43435 3211 for (i = 0; i < loopargs_len; i++) {
0ff43435
AG
3212 /* Perform ECDSA signature test */
3213 EC_KEY_generate_key(loopargs[i].ecdsa[testnum]);
3214 st = ECDSA_sign(0, loopargs[i].buf, 20, loopargs[i].buf2,
29dd15b1
NT
3215 &loopargs[i].siglen,
3216 loopargs[i].ecdsa[testnum]);
8b0b80d9
AG
3217 if (st == 0)
3218 break;
3219 }
7e1b7485 3220 if (st == 0) {
0f113f3e
MC
3221 BIO_printf(bio_err,
3222 "ECDSA sign failure. No ECDSA sign will be done.\n");
3223 ERR_print_errors(bio_err);
9d0854f4 3224 op_count = 1;
0f113f3e
MC
3225 } else {
3226 pkey_print_message("sign", "ecdsa",
8b0b80d9 3227 ecdsa_c[testnum][0],
d63d89ea 3228 ec_curves[testnum].bits, seconds.ecdsa);
0f113f3e 3229 Time_F(START);
8b0b80d9 3230 count = run_benchmark(async_jobs, ECDSA_sign_loop, loopargs);
0f113f3e
MC
3231 d = Time_F(STOP);
3232
3233 BIO_printf(bio_err,
48bc0d99
F
3234 mr ? "+R5:%ld:%u:%.2f\n" :
3235 "%ld %u bits ECDSA signs in %.2fs \n",
d63d89ea 3236 count, ec_curves[testnum].bits, d);
c8bff7ad 3237 ecdsa_results[testnum][0] = (double)count / d;
9d0854f4 3238 op_count = count;
0f113f3e
MC
3239 }
3240
3241 /* Perform ECDSA verification test */
0ff43435
AG
3242 for (i = 0; i < loopargs_len; i++) {
3243 st = ECDSA_verify(0, loopargs[i].buf, 20, loopargs[i].buf2,
29dd15b1
NT
3244 loopargs[i].siglen,
3245 loopargs[i].ecdsa[testnum]);
8b0b80d9
AG
3246 if (st != 1)
3247 break;
3248 }
7e1b7485 3249 if (st != 1) {
0f113f3e
MC
3250 BIO_printf(bio_err,
3251 "ECDSA verify failure. No ECDSA verify will be done.\n");
3252 ERR_print_errors(bio_err);
8b0b80d9 3253 ecdsa_doit[testnum] = 0;
0f113f3e
MC
3254 } else {
3255 pkey_print_message("verify", "ecdsa",
8b0b80d9 3256 ecdsa_c[testnum][1],
d63d89ea 3257 ec_curves[testnum].bits, seconds.ecdsa);
0f113f3e 3258 Time_F(START);
8b0b80d9 3259 count = run_benchmark(async_jobs, ECDSA_verify_loop, loopargs);
0f113f3e
MC
3260 d = Time_F(STOP);
3261 BIO_printf(bio_err,
48bc0d99
F
3262 mr ? "+R6:%ld:%u:%.2f\n"
3263 : "%ld %u bits ECDSA verify in %.2fs\n",
d63d89ea 3264 count, ec_curves[testnum].bits, d);
c8bff7ad 3265 ecdsa_results[testnum][1] = (double)count / d;
0f113f3e
MC
3266 }
3267
9d0854f4 3268 if (op_count <= 1) {
0f113f3e 3269 /* if longer than 10s, don't do any more */
f607f6ea 3270 stop_it(ecdsa_doit, testnum);
0f113f3e
MC
3271 }
3272 }
3273 }
8c885145 3274# endif /* OPENSSL_NO_DEPRECATED_3_0 */
7e1b7485 3275
8b0b80d9 3276 for (testnum = 0; testnum < EC_NUM; testnum++) {
4d82c58b
F
3277 int ecdh_checks = 1;
3278
8b0b80d9 3279 if (!ecdh_doit[testnum])
0f113f3e 3280 continue;
ed7377db 3281
0ff43435 3282 for (i = 0; i < loopargs_len; i++) {
2e4c3b5c 3283 EVP_PKEY_CTX *kctx = NULL;
f7d984dd 3284 EVP_PKEY_CTX *test_ctx = NULL;
2e4c3b5c
NT
3285 EVP_PKEY_CTX *ctx = NULL;
3286 EVP_PKEY *key_A = NULL;
3287 EVP_PKEY *key_B = NULL;
cc98e639 3288 size_t outlen;
f7d984dd 3289 size_t test_outlen;
ed7377db 3290
86ff6cc6
NT
3291 /* Ensure that the error queue is empty */
3292 if (ERR_peek_error()) {
3293 BIO_printf(bio_err,
3294 "WARNING: the error queue contains previous unhandled errors.\n");
3295 ERR_print_errors(bio_err);
3296 }
3297
b756d694
NT
3298 /* Let's try to create a ctx directly from the NID: this works for
3299 * curves like Curve25519 that are not implemented through the low
3300 * level EC interface.
3301 * If this fails we try creating a EVP_PKEY_EC generic param ctx,
3302 * then we set the curve by NID before deriving the actual keygen
3303 * ctx for that specific curve. */
d63d89ea 3304 kctx = EVP_PKEY_CTX_new_id(ec_curves[testnum].nid, NULL); /* keygen ctx from NID */
b756d694 3305 if (!kctx) {
ed7377db
NT
3306 EVP_PKEY_CTX *pctx = NULL;
3307 EVP_PKEY *params = NULL;
3308
86ff6cc6
NT
3309 /* If we reach this code EVP_PKEY_CTX_new_id() failed and a
3310 * "int_ctx_new:unsupported algorithm" error was added to the
3311 * error queue.
3312 * We remove it from the error queue as we are handling it. */
3313 unsigned long error = ERR_peek_error(); /* peek the latest error in the queue */
3314 if (error == ERR_peek_last_error() && /* oldest and latest errors match */
3315 /* check that the error origin matches */
3316 ERR_GET_LIB(error) == ERR_LIB_EVP &&
86ff6cc6
NT
3317 ERR_GET_REASON(error) == EVP_R_UNSUPPORTED_ALGORITHM)
3318 ERR_get_error(); /* pop error from queue */
3319 if (ERR_peek_error()) {
3320 BIO_printf(bio_err,
3321 "Unhandled error in the error queue during ECDH init.\n");
3322 ERR_print_errors(bio_err);
9d0854f4 3323 op_count = 1;
86ff6cc6
NT
3324 break;
3325 }
3326
d63d89ea
F
3327 /* Create the context for parameter generation */
3328 if (!(pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) ||
29dd15b1
NT
3329 /* Initialise the parameter generation */
3330 !EVP_PKEY_paramgen_init(pctx) ||
3331 /* Set the curve by NID */
3332 !EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
d63d89ea 3333 ec_curves
48bc0d99 3334 [testnum].nid) ||
29dd15b1 3335 /* Create the parameter object params */
2e4c3b5c 3336 !EVP_PKEY_paramgen(pctx, &params)) {
0f113f3e 3337 ecdh_checks = 0;
b756d694 3338 BIO_printf(bio_err, "ECDH EC params init failure.\n");
ed7377db 3339 ERR_print_errors(bio_err);
9d0854f4 3340 op_count = 1;
ed7377db 3341 break;
0ff43435 3342 }
ed7377db
NT
3343 /* Create the context for the key generation */
3344 kctx = EVP_PKEY_CTX_new(params, NULL);
3345
29dd15b1
NT
3346 EVP_PKEY_free(params);
3347 params = NULL;
3348 EVP_PKEY_CTX_free(pctx);
3349 pctx = NULL;
447402e6 3350 }
2234212c 3351 if (kctx == NULL || /* keygen ctx is not null */
7e3ae248 3352 EVP_PKEY_keygen_init(kctx) <= 0/* init keygen ctx */ ) {
ed7377db
NT
3353 ecdh_checks = 0;
3354 BIO_printf(bio_err, "ECDH keygen failure.\n");
3355 ERR_print_errors(bio_err);
9d0854f4 3356 op_count = 1;
ed7377db 3357 break;
0f113f3e 3358 }
ed7377db 3359
7e3ae248
PY
3360 if (EVP_PKEY_keygen(kctx, &key_A) <= 0 || /* generate secret key A */
3361 EVP_PKEY_keygen(kctx, &key_B) <= 0 || /* generate secret key B */
29dd15b1 3362 !(ctx = EVP_PKEY_CTX_new(key_A, NULL)) || /* derivation ctx from skeyA */
7e3ae248
PY
3363 EVP_PKEY_derive_init(ctx) <= 0 || /* init derivation ctx */
3364 EVP_PKEY_derive_set_peer(ctx, key_B) <= 0 || /* set peer pubkey in ctx */
3365 EVP_PKEY_derive(ctx, NULL, &outlen) <= 0 || /* determine max length */
f7d984dd 3366 outlen == 0 || /* ensure outlen is a valid size */
2e4c3b5c 3367 outlen > MAX_ECDH_SIZE /* avoid buffer overflow */ ) {
ed7377db
NT
3368 ecdh_checks = 0;
3369 BIO_printf(bio_err, "ECDH key generation failure.\n");
3370 ERR_print_errors(bio_err);
9d0854f4 3371 op_count = 1;
ed7377db
NT
3372 break;
3373 }
3374
f7d984dd
NT
3375 /* Here we perform a test run, comparing the output of a*B and b*A;
3376 * we try this here and assume that further EVP_PKEY_derive calls
3377 * never fail, so we can skip checks in the actually benchmarked
3378 * code, for maximum performance. */
3379 if (!(test_ctx = EVP_PKEY_CTX_new(key_B, NULL)) || /* test ctx from skeyB */
3380 !EVP_PKEY_derive_init(test_ctx) || /* init derivation test_ctx */
3381 !EVP_PKEY_derive_set_peer(test_ctx, key_A) || /* set peer pubkey in test_ctx */
3382 !EVP_PKEY_derive(test_ctx, NULL, &test_outlen) || /* determine max length */
3383 !EVP_PKEY_derive(ctx, loopargs[i].secret_a, &outlen) || /* compute a*B */
3384 !EVP_PKEY_derive(test_ctx, loopargs[i].secret_b, &test_outlen) || /* compute b*A */
3385 test_outlen != outlen /* compare output length */ ) {
3386 ecdh_checks = 0;
3387 BIO_printf(bio_err, "ECDH computation failure.\n");
3388 ERR_print_errors(bio_err);
9d0854f4 3389 op_count = 1;
f7d984dd
NT
3390 break;
3391 }
9bffdebc
NT
3392
3393 /* Compare the computation results: CRYPTO_memcmp() returns 0 if equal */
3394 if (CRYPTO_memcmp(loopargs[i].secret_a,
3395 loopargs[i].secret_b, outlen)) {
3396 ecdh_checks = 0;
f7d984dd
NT
3397 BIO_printf(bio_err, "ECDH computations don't match.\n");
3398 ERR_print_errors(bio_err);
9d0854f4 3399 op_count = 1;
f7d984dd
NT
3400 break;
3401 }
3402
ed7377db 3403 loopargs[i].ecdh_ctx[testnum] = ctx;
cc98e639 3404 loopargs[i].outlen[testnum] = outlen;
ed7377db 3405
a00cceb2
PS
3406 EVP_PKEY_free(key_A);
3407 EVP_PKEY_free(key_B);
29dd15b1
NT
3408 EVP_PKEY_CTX_free(kctx);
3409 kctx = NULL;
f7d984dd
NT
3410 EVP_PKEY_CTX_free(test_ctx);
3411 test_ctx = NULL;
ed7377db
NT
3412 }
3413 if (ecdh_checks != 0) {
3414 pkey_print_message("", "ecdh",
29dd15b1 3415 ecdh_c[testnum][0],
d63d89ea 3416 ec_curves[testnum].bits, seconds.ecdh);
ed7377db 3417 Time_F(START);
29dd15b1
NT
3418 count =
3419 run_benchmark(async_jobs, ECDH_EVP_derive_key_loop, loopargs);
ed7377db
NT
3420 d = Time_F(STOP);
3421 BIO_printf(bio_err,
29dd15b1 3422 mr ? "+R7:%ld:%d:%.2f\n" :
48bc0d99 3423 "%ld %u-bits ECDH ops in %.2fs\n", count,
d63d89ea 3424 ec_curves[testnum].bits, d);
222c3da3 3425 ecdh_results[testnum][0] = (double)count / d;
9d0854f4 3426 op_count = count;
0f113f3e 3427 }
e172d60d 3428
9d0854f4 3429 if (op_count <= 1) {
0f113f3e 3430 /* if longer than 10s, don't do any more */
f607f6ea 3431 stop_it(ecdh_doit, testnum);
0f113f3e
MC
3432 }
3433 }
d3a9fb10
PY
3434
3435 for (testnum = 0; testnum < EdDSA_NUM; testnum++) {
3436 int st = 1;
3437 EVP_PKEY *ed_pkey = NULL;
3438 EVP_PKEY_CTX *ed_pctx = NULL;
3439
3440 if (!eddsa_doit[testnum])
3441 continue; /* Ignore Curve */
3442 for (i = 0; i < loopargs_len; i++) {
3443 loopargs[i].eddsa_ctx[testnum] = EVP_MD_CTX_new();
3444 if (loopargs[i].eddsa_ctx[testnum] == NULL) {
3445 st = 0;
3446 break;
3447 }
1154ffbf
SAS
3448 loopargs[i].eddsa_ctx2[testnum] = EVP_MD_CTX_new();
3449 if (loopargs[i].eddsa_ctx2[testnum] == NULL) {
3450 st = 0;
3451 break;
3452 }
d3a9fb10 3453
d63d89ea 3454 if ((ed_pctx = EVP_PKEY_CTX_new_id(ed_curves[testnum].nid, NULL))
d3a9fb10 3455 == NULL
94bd168a
PY
3456 || EVP_PKEY_keygen_init(ed_pctx) <= 0
3457 || EVP_PKEY_keygen(ed_pctx, &ed_pkey) <= 0) {
d3a9fb10
PY
3458 st = 0;
3459 EVP_PKEY_CTX_free(ed_pctx);
3460 break;
3461 }
3462 EVP_PKEY_CTX_free(ed_pctx);
3463
3464 if (!EVP_DigestSignInit(loopargs[i].eddsa_ctx[testnum], NULL, NULL,
3465 NULL, ed_pkey)) {
3466 st = 0;
3467 EVP_PKEY_free(ed_pkey);
3468 break;
3469 }
1154ffbf
SAS
3470 if (!EVP_DigestVerifyInit(loopargs[i].eddsa_ctx2[testnum], NULL,
3471 NULL, NULL, ed_pkey)) {
3472 st = 0;
3473 EVP_PKEY_free(ed_pkey);
3474 break;
3475 }
3476
d3a9fb10 3477 EVP_PKEY_free(ed_pkey);
4f5b222b 3478 ed_pkey = NULL;
d3a9fb10
PY
3479 }
3480 if (st == 0) {
3481 BIO_printf(bio_err, "EdDSA failure.\n");
3482 ERR_print_errors(bio_err);
9d0854f4 3483 op_count = 1;
d3a9fb10
PY
3484 } else {
3485 for (i = 0; i < loopargs_len; i++) {
3486 /* Perform EdDSA signature test */
d63d89ea 3487 loopargs[i].sigsize = ed_curves[testnum].sigsize;
d3a9fb10 3488 st = EVP_DigestSign(loopargs[i].eddsa_ctx[testnum],
52307f94 3489 loopargs[i].buf2, &loopargs[i].sigsize,
d3a9fb10
PY
3490 loopargs[i].buf, 20);
3491 if (st == 0)
3492 break;
3493 }
3494 if (st == 0) {
3495 BIO_printf(bio_err,
3496 "EdDSA sign failure. No EdDSA sign will be done.\n");
3497 ERR_print_errors(bio_err);
9d0854f4 3498 op_count = 1;
d3a9fb10 3499 } else {
d63d89ea 3500 pkey_print_message("sign", ed_curves[testnum].name,
d3a9fb10 3501 eddsa_c[testnum][0],
d63d89ea 3502 ed_curves[testnum].bits, seconds.eddsa);
d3a9fb10
PY
3503 Time_F(START);
3504 count = run_benchmark(async_jobs, EdDSA_sign_loop, loopargs);
3505 d = Time_F(STOP);
3506
3507 BIO_printf(bio_err,
3508 mr ? "+R8:%ld:%u:%s:%.2f\n" :
3509 "%ld %u bits %s signs in %.2fs \n",
d63d89ea
F
3510 count, ed_curves[testnum].bits,
3511 ed_curves[testnum].name, d);
d3a9fb10 3512 eddsa_results[testnum][0] = (double)count / d;
9d0854f4 3513 op_count = count;
d3a9fb10 3514 }
d3a9fb10
PY
3515 /* Perform EdDSA verification test */
3516 for (i = 0; i < loopargs_len; i++) {
1154ffbf 3517 st = EVP_DigestVerify(loopargs[i].eddsa_ctx2[testnum],
52307f94 3518 loopargs[i].buf2, loopargs[i].sigsize,
d3a9fb10
PY
3519 loopargs[i].buf, 20);
3520 if (st != 1)
3521 break;
3522 }
3523 if (st != 1) {
3524 BIO_printf(bio_err,
3525 "EdDSA verify failure. No EdDSA verify will be done.\n");
3526 ERR_print_errors(bio_err);
3527 eddsa_doit[testnum] = 0;
3528 } else {
d63d89ea 3529 pkey_print_message("verify", ed_curves[testnum].name,
d3a9fb10 3530 eddsa_c[testnum][1],
d63d89ea 3531 ed_curves[testnum].bits, seconds.eddsa);
d3a9fb10
PY
3532 Time_F(START);
3533 count = run_benchmark(async_jobs, EdDSA_verify_loop, loopargs);
3534 d = Time_F(STOP);
3535 BIO_printf(bio_err,
3536 mr ? "+R9:%ld:%u:%s:%.2f\n"
3537 : "%ld %u bits %s verify in %.2fs\n",
d63d89ea
F
3538 count, ed_curves[testnum].bits,
3539 ed_curves[testnum].name, d);
d3a9fb10
PY
3540 eddsa_results[testnum][1] = (double)count / d;
3541 }
3542
9d0854f4 3543 if (op_count <= 1) {
d3a9fb10 3544 /* if longer than 10s, don't do any more */
f607f6ea 3545 stop_it(eddsa_doit, testnum);
d3a9fb10
PY
3546 }
3547 }
3548 }
3549
a56f68ad
PY
3550# ifndef OPENSSL_NO_SM2
3551 for (testnum = 0; testnum < SM2_NUM; testnum++) {
3552 int st = 1;
3553 EVP_PKEY *sm2_pkey = NULL;
a56f68ad
PY
3554
3555 if (!sm2_doit[testnum])
3556 continue; /* Ignore Curve */
3557 /* Init signing and verification */
3558 for (i = 0; i < loopargs_len; i++) {
001d5e2c
F
3559 EVP_PKEY_CTX *sm2_pctx = NULL;
3560 EVP_PKEY_CTX *sm2_vfy_pctx = NULL;
3561 EVP_PKEY_CTX *pctx = NULL;
3562 st = 0;
3563
a56f68ad 3564 loopargs[i].sm2_ctx[testnum] = EVP_MD_CTX_new();
a56f68ad 3565 loopargs[i].sm2_vfy_ctx[testnum] = EVP_MD_CTX_new();
001d5e2c
F
3566 if (loopargs[i].sm2_ctx[testnum] == NULL
3567 || loopargs[i].sm2_vfy_ctx[testnum] == NULL)
a56f68ad 3568 break;
a56f68ad
PY
3569
3570 /* SM2 keys are generated as normal EC keys with a special curve */
001d5e2c 3571 st = !((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL)) == NULL
a56f68ad
PY
3572 || EVP_PKEY_keygen_init(pctx) <= 0
3573 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx,
d63d89ea 3574 sm2_curves[testnum].nid) <= 0
001d5e2c 3575 || EVP_PKEY_keygen(pctx, &sm2_pkey) <= 0);
a56f68ad 3576 EVP_PKEY_CTX_free(pctx);
001d5e2c
F
3577 if (st == 0)
3578 break;
a56f68ad 3579
001d5e2c
F
3580 st = 0; /* set back to zero */
3581 /* attach it sooner to rely on main final cleanup */
3582 loopargs[i].sm2_pkey[testnum] = sm2_pkey;
579422c8 3583 loopargs[i].sigsize = EVP_PKEY_size(sm2_pkey);
a56f68ad
PY
3584
3585 sm2_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
a56f68ad 3586 sm2_vfy_pctx = EVP_PKEY_CTX_new(sm2_pkey, NULL);
001d5e2c
F
3587 if (sm2_pctx == NULL || sm2_vfy_pctx == NULL) {
3588 EVP_PKEY_CTX_free(sm2_vfy_pctx);
a56f68ad
PY
3589 break;
3590 }
579422c8 3591
001d5e2c
F
3592 /* attach them directly to respective ctx */
3593 EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_ctx[testnum], sm2_pctx);
3594 EVP_MD_CTX_set_pkey_ctx(loopargs[i].sm2_vfy_ctx[testnum], sm2_vfy_pctx);
3595
a56f68ad
PY
3596 /*
3597 * No need to allow user to set an explicit ID here, just use
3598 * the one defined in the 'draft-yang-tls-tl13-sm-suites' I-D.
3599 */
001d5e2c
F
3600 if (EVP_PKEY_CTX_set1_id(sm2_pctx, SM2_ID, SM2_ID_LEN) != 1
3601 || EVP_PKEY_CTX_set1_id(sm2_vfy_pctx, SM2_ID, SM2_ID_LEN) != 1)
a56f68ad 3602 break;
a56f68ad
PY
3603
3604 if (!EVP_DigestSignInit(loopargs[i].sm2_ctx[testnum], NULL,
001d5e2c 3605 EVP_sm3(), NULL, sm2_pkey))
a56f68ad 3606 break;
a56f68ad 3607 if (!EVP_DigestVerifyInit(loopargs[i].sm2_vfy_ctx[testnum], NULL,
001d5e2c 3608 EVP_sm3(), NULL, sm2_pkey))
a56f68ad 3609 break;
001d5e2c 3610 st = 1; /* mark loop as succeeded */
a56f68ad
PY
3611 }
3612 if (st == 0) {
001d5e2c 3613 BIO_printf(bio_err, "SM2 init failure.\n");
a56f68ad 3614 ERR_print_errors(bio_err);
9d0854f4 3615 op_count = 1;
a56f68ad
PY
3616 } else {
3617 for (i = 0; i < loopargs_len; i++) {
001d5e2c
F
3618 size_t sm2_sigsize = loopargs[i].sigsize;
3619
a56f68ad
PY
3620 /* Perform SM2 signature test */
3621 st = EVP_DigestSign(loopargs[i].sm2_ctx[testnum],
3622 loopargs[i].buf2, &sm2_sigsize,
3623 loopargs[i].buf, 20);
3624 if (st == 0)
3625 break;
3626 }
3627 if (st == 0) {
3628 BIO_printf(bio_err,
3629 "SM2 sign failure. No SM2 sign will be done.\n");
3630 ERR_print_errors(bio_err);
9d0854f4 3631 op_count = 1;
a56f68ad 3632 } else {
d63d89ea 3633 pkey_print_message("sign", sm2_curves[testnum].name,
a56f68ad 3634 sm2_c[testnum][0],
d63d89ea 3635 sm2_curves[testnum].bits, seconds.sm2);
a56f68ad
PY
3636 Time_F(START);
3637 count = run_benchmark(async_jobs, SM2_sign_loop, loopargs);
3638 d = Time_F(STOP);
3639
3640 BIO_printf(bio_err,
031c9bd3 3641 mr ? "+R10:%ld:%u:%s:%.2f\n" :
a56f68ad 3642 "%ld %u bits %s signs in %.2fs \n",
d63d89ea
F
3643 count, sm2_curves[testnum].bits,
3644 sm2_curves[testnum].name, d);
a56f68ad 3645 sm2_results[testnum][0] = (double)count / d;
9d0854f4 3646 op_count = count;
a56f68ad
PY
3647 }
3648
3649 /* Perform SM2 verification test */
3650 for (i = 0; i < loopargs_len; i++) {
3651 st = EVP_DigestVerify(loopargs[i].sm2_vfy_ctx[testnum],
3652 loopargs[i].buf2, loopargs[i].sigsize,
3653 loopargs[i].buf, 20);
3654 if (st != 1)
3655 break;
3656 }
3657 if (st != 1) {
3658 BIO_printf(bio_err,
3659 "SM2 verify failure. No SM2 verify will be done.\n");
3660 ERR_print_errors(bio_err);
3661 sm2_doit[testnum] = 0;
3662 } else {
d63d89ea 3663 pkey_print_message("verify", sm2_curves[testnum].name,
a56f68ad 3664 sm2_c[testnum][1],
d63d89ea 3665 sm2_curves[testnum].bits, seconds.sm2);
a56f68ad
PY
3666 Time_F(START);
3667 count = run_benchmark(async_jobs, SM2_verify_loop, loopargs);
3668 d = Time_F(STOP);
3669 BIO_printf(bio_err,
031c9bd3 3670 mr ? "+R11:%ld:%u:%s:%.2f\n"
a56f68ad 3671 : "%ld %u bits %s verify in %.2fs\n",
d63d89ea
F
3672 count, sm2_curves[testnum].bits,
3673 sm2_curves[testnum].name, d);
a56f68ad
PY
3674 sm2_results[testnum][1] = (double)count / d;
3675 }
3676
9d0854f4 3677 if (op_count <= 1) {
a56f68ad
PY
3678 /* if longer than 10s, don't do any more */
3679 for (testnum++; testnum < SM2_NUM; testnum++)
3680 sm2_doit[testnum] = 0;
3681 }
3682 }
3683 }
3684# endif /* OPENSSL_NO_SM2 */
d6073e27 3685#endif /* OPENSSL_NO_EC */
60d3b5b9
HK
3686
3687#ifndef OPENSSL_NO_DH
3688 for (testnum = 0; testnum < FFDH_NUM; testnum++) {
3689 int ffdh_checks = 1;
3690
3691 if (!ffdh_doit[testnum])
3692 continue;
3693
3694 for (i = 0; i < loopargs_len; i++) {
3695 EVP_PKEY *pkey_A = NULL;
3696 EVP_PKEY *pkey_B = NULL;
3697 EVP_PKEY_CTX *ffdh_ctx = NULL;
3698 EVP_PKEY_CTX *test_ctx = NULL;
3699 size_t secret_size;
3700 size_t test_out;
3701
3702 /* Ensure that the error queue is empty */
3703 if (ERR_peek_error()) {
3704 BIO_printf(bio_err,
3705 "WARNING: the error queue contains previous unhandled errors.\n");
3706 ERR_print_errors(bio_err);
3707 }
3708
3709 pkey_A = EVP_PKEY_new();
3710 if (!pkey_A) {
3711 BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
3712 ERR_print_errors(bio_err);
9d0854f4 3713 op_count = 1;
60d3b5b9
HK
3714 ffdh_checks = 0;
3715 break;
3716 }
3717 pkey_B = EVP_PKEY_new();
3718 if (!pkey_B) {
3719 BIO_printf(bio_err, "Error while initialising EVP_PKEY (out of memory?).\n");
3720 ERR_print_errors(bio_err);
9d0854f4 3721 op_count = 1;
60d3b5b9
HK
3722 ffdh_checks = 0;
3723 break;
3724 }
3725
3726 ffdh_ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_DH, NULL);
3727 if (!ffdh_ctx) {
3728 BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
3729 ERR_print_errors(bio_err);
9d0854f4 3730 op_count = 1;
60d3b5b9
HK
3731 ffdh_checks = 0;
3732 break;
3733 }
3734
3735 if (EVP_PKEY_keygen_init(ffdh_ctx) <= 0) {
3736 BIO_printf(bio_err, "Error while initialising EVP_PKEY_CTX.\n");
3737 ERR_print_errors(bio_err);
9d0854f4 3738 op_count = 1;
60d3b5b9
HK
3739 ffdh_checks = 0;
3740 break;
3741 }
3742 if (EVP_PKEY_CTX_set_dh_nid(ffdh_ctx, ffdh_params[testnum].nid) <= 0) {
3743 BIO_printf(bio_err, "Error setting DH key size for keygen.\n");
3744 ERR_print_errors(bio_err);
9d0854f4 3745 op_count = 1;
60d3b5b9
HK
3746 ffdh_checks = 0;
3747 break;
3748 }
3749
3750 if (EVP_PKEY_keygen(ffdh_ctx, &pkey_A) <= 0 ||
3751 EVP_PKEY_keygen(ffdh_ctx, &pkey_B) <= 0) {
3752 BIO_printf(bio_err, "FFDH key generation failure.\n");
3753 ERR_print_errors(bio_err);
9d0854f4 3754 op_count = 1;
60d3b5b9
HK
3755 ffdh_checks = 0;
3756 break;
3757 }
3758
3759 EVP_PKEY_CTX_free(ffdh_ctx);
3760
3761 /* check if the derivation works correctly both ways so that
3762 * we know if future derive calls will fail, and we can skip
3763 * error checking in benchmarked code */
3764 ffdh_ctx = EVP_PKEY_CTX_new(pkey_A, NULL);
3765 if (!ffdh_ctx) {
3766 BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
3767 ERR_print_errors(bio_err);
9d0854f4 3768 op_count = 1;
60d3b5b9
HK
3769 ffdh_checks = 0;
3770 break;
3771 }
3772 if (EVP_PKEY_derive_init(ffdh_ctx) <= 0) {
3773 BIO_printf(bio_err, "FFDH derivation context init failure.\n");
3774 ERR_print_errors(bio_err);
9d0854f4 3775 op_count = 1;
60d3b5b9
HK
3776 ffdh_checks = 0;
3777 break;
3778 }
3779 if (EVP_PKEY_derive_set_peer(ffdh_ctx, pkey_B) <= 0) {
3780 BIO_printf(bio_err, "Assigning peer key for derivation failed.\n");
3781 ERR_print_errors(bio_err);
9d0854f4 3782 op_count = 1;
60d3b5b9
HK
3783 ffdh_checks = 0;
3784 break;
3785 }
3786 if (EVP_PKEY_derive(ffdh_ctx, NULL, &secret_size) <= 0) {
3787 BIO_printf(bio_err, "Checking size of shared secret failed.\n");
3788 ERR_print_errors(bio_err);
9d0854f4 3789 op_count = 1;
60d3b5b9
HK
3790 ffdh_checks = 0;
3791 break;
3792 }
3793 if (secret_size > MAX_FFDH_SIZE) {
3794 BIO_printf(bio_err, "Assertion failure: shared secret too large.\n");
9d0854f4 3795 op_count = 1;
60d3b5b9
HK
3796 ffdh_checks = 0;
3797 break;
3798 }
3799 if (EVP_PKEY_derive(ffdh_ctx,
3800 loopargs[i].secret_ff_a,
3801 &secret_size) <= 0) {
3802 BIO_printf(bio_err, "Shared secret derive failure.\n");
3803 ERR_print_errors(bio_err);
9d0854f4 3804 op_count = 1;
60d3b5b9
HK
3805 ffdh_checks = 0;
3806 break;
3807 }
3808 /* Now check from side B */
3809 test_ctx = EVP_PKEY_CTX_new(pkey_B, NULL);
3810 if (!test_ctx) {
3811 BIO_printf(bio_err, "Error while allocating EVP_PKEY_CTX.\n");
3812 ERR_print_errors(bio_err);
9d0854f4 3813 op_count = 1;
60d3b5b9
HK
3814 ffdh_checks = 0;
3815 break;
3816 }
3817 if (!EVP_PKEY_derive_init(test_ctx) ||
3818 !EVP_PKEY_derive_set_peer(test_ctx, pkey_A) ||
3819 !EVP_PKEY_derive(test_ctx, NULL, &test_out) ||
3820 !EVP_PKEY_derive(test_ctx, loopargs[i].secret_ff_b, &test_out) ||
3821 test_out != secret_size) {
3822 BIO_printf(bio_err, "FFDH computation failure.\n");
9d0854f4 3823 op_count = 1;
60d3b5b9
HK
3824 ffdh_checks = 0;
3825 break;
3826 }
3827
3828 /* compare the computed secrets */
3829 if (CRYPTO_memcmp(loopargs[i].secret_ff_a,
3830 loopargs[i].secret_ff_b, secret_size)) {
3831 BIO_printf(bio_err, "FFDH computations don't match.\n");
3832 ERR_print_errors(bio_err);
9d0854f4 3833 op_count = 1;
60d3b5b9
HK
3834 ffdh_checks = 0;
3835 break;
3836 }
3837
3838 loopargs[i].ffdh_ctx[testnum] = ffdh_ctx;
3839
3840 EVP_PKEY_free(pkey_A);
3841 pkey_A = NULL;
3842 EVP_PKEY_free(pkey_B);
3843 pkey_B = NULL;
3844 EVP_PKEY_CTX_free(test_ctx);
3845 test_ctx = NULL;
3846 }
3847 if (ffdh_checks != 0) {
3848 pkey_print_message("", "ffdh", ffdh_c[testnum][0],
3849 ffdh_params[testnum].bits, seconds.ffdh);
3850 Time_F(START);
3851 count =
3852 run_benchmark(async_jobs, FFDH_derive_key_loop, loopargs);
3853 d = Time_F(STOP);
3854 BIO_printf(bio_err,
3855 mr ? "+R12:%ld:%d:%.2f\n" :
3856 "%ld %u-bits FFDH ops in %.2fs\n", count,
3857 ffdh_params[testnum].bits, d);
3858 ffdh_results[testnum][0] = (double)count / d;
9d0854f4 3859 op_count = count;
60d3b5b9 3860 };
9d0854f4 3861 if (op_count <= 1) {
60d3b5b9
HK
3862 /* if longer than 10s, don't do any more */
3863 stop_it(ffdh_doit, testnum);
3864 }
3865 }
3866#endif /* OPENSSL_NO_DH */
a00ae6c4 3867#ifndef NO_FORK
0f113f3e 3868 show_res:
a00ae6c4 3869#endif
0f113f3e 3870 if (!mr) {
3a63dbef
RL
3871 printf("version: %s\n", OpenSSL_version(OPENSSL_FULL_VERSION_STRING));
3872 printf("built on: %s\n", OpenSSL_version(OPENSSL_BUILT_ON));
0f113f3e
MC
3873 printf("options:");
3874 printf("%s ", BN_options());
58e1f3d6 3875#if !defined(OPENSSL_NO_MD2) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 3876 printf("%s ", MD2_options());
a00ae6c4 3877#endif
a8fca728 3878#if !defined(OPENSSL_NO_RC4) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 3879 printf("%s ", RC4_options());
a00ae6c4 3880#endif
c6fec81b 3881#if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 3882 printf("%s ", DES_options());
a00ae6c4 3883#endif
c72fa255 3884#ifndef OPENSSL_NO_DEPRECATED_3_0
0f113f3e 3885 printf("%s ", AES_options());
c72fa255 3886#endif
da2d32f6 3887#if !defined(OPENSSL_NO_IDEA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
9021a5df 3888 printf("%s ", IDEA_options());
a00ae6c4 3889#endif
03047e7b 3890#if !defined(OPENSSL_NO_BF) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0f113f3e 3891 printf("%s ", BF_options());
a00ae6c4 3892#endif
b0700d2c 3893 printf("\n%s\n", OpenSSL_version(OPENSSL_CFLAGS));
363e941e 3894 printf("%s\n", OpenSSL_version(OPENSSL_CPU_INFO));
0f113f3e 3895 }
e172d60d 3896
0f113f3e
MC
3897 if (pr_header) {
3898 if (mr)
7e1b7485 3899 printf("+H");
0f113f3e 3900 else {
7e1b7485
RS
3901 printf
3902 ("The 'numbers' are in 1000s of bytes per second processed.\n");
3903 printf("type ");
0f113f3e 3904 }
64daf14d 3905 for (testnum = 0; testnum < size_num; testnum++)
8b0b80d9 3906 printf(mr ? ":%d" : "%7d bytes", lengths[testnum]);
7e1b7485 3907 printf("\n");
0f113f3e 3908 }
e172d60d 3909
0f113f3e
MC
3910 for (k = 0; k < ALGOR_NUM; k++) {
3911 if (!doit[k])
3912 continue;
3913 if (mr)
5c6a69f5 3914 printf("+F:%u:%s", k, names[k]);
0f113f3e 3915 else
7e1b7485 3916 printf("%-13s", names[k]);
64daf14d 3917 for (testnum = 0; testnum < size_num; testnum++) {
8b0b80d9
AG
3918 if (results[k][testnum] > 10000 && !mr)
3919 printf(" %11.2fk", results[k][testnum] / 1e3);
0f113f3e 3920 else
8b0b80d9 3921 printf(mr ? ":%.2f" : " %11.2f ", results[k][testnum]);
0f113f3e 3922 }
7e1b7485 3923 printf("\n");
0f113f3e 3924 }
3a1ee3c1 3925#ifndef OPENSSL_NO_DEPRECATED_3_0
8b0b80d9 3926 testnum = 1;
0f113f3e
MC
3927 for (k = 0; k < RSA_NUM; k++) {
3928 if (!rsa_doit[k])
3929 continue;
8b0b80d9 3930 if (testnum && !mr) {
0f113f3e 3931 printf("%18ssign verify sign/s verify/s\n", " ");
8b0b80d9 3932 testnum = 0;
0f113f3e
MC
3933 }
3934 if (mr)
7e1b7485 3935 printf("+F2:%u:%u:%f:%f\n",
d63d89ea 3936 k, rsa_keys[k].bits, rsa_results[k][0], rsa_results[k][1]);
0f113f3e 3937 else
7e1b7485 3938 printf("rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
d63d89ea 3939 rsa_keys[k].bits, 1.0 / rsa_results[k][0], 1.0 / rsa_results[k][1],
8ac2d1ab 3940 rsa_results[k][0], rsa_results[k][1]);
0f113f3e 3941 }
a00ae6c4 3942#endif
1ddf2594 3943#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
8b0b80d9 3944 testnum = 1;
0f113f3e
MC
3945 for (k = 0; k < DSA_NUM; k++) {
3946 if (!dsa_doit[k])
3947 continue;
8b0b80d9 3948 if (testnum && !mr) {
0f113f3e 3949 printf("%18ssign verify sign/s verify/s\n", " ");
8b0b80d9 3950 testnum = 0;
0f113f3e
MC
3951 }
3952 if (mr)
7e1b7485
RS
3953 printf("+F3:%u:%u:%f:%f\n",
3954 k, dsa_bits[k], dsa_results[k][0], dsa_results[k][1]);
0f113f3e 3955 else
7e1b7485 3956 printf("dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
0d4de756
CS
3957 dsa_bits[k], 1.0 / dsa_results[k][0], 1.0 / dsa_results[k][1],
3958 dsa_results[k][0], dsa_results[k][1]);
0f113f3e 3959 }
a00ae6c4 3960#endif
10bf4fc2 3961#ifndef OPENSSL_NO_EC
8b0b80d9 3962 testnum = 1;
5c6a69f5 3963 for (k = 0; k < OSSL_NELEM(ecdsa_doit); k++) {
0f113f3e
MC
3964 if (!ecdsa_doit[k])
3965 continue;
8b0b80d9 3966 if (testnum && !mr) {
0f113f3e 3967 printf("%30ssign verify sign/s verify/s\n", " ");
8b0b80d9 3968 testnum = 0;
0f113f3e
MC
3969 }
3970
3971 if (mr)
7e1b7485 3972 printf("+F4:%u:%u:%f:%f\n",
d63d89ea 3973 k, ec_curves[k].bits,
7e1b7485 3974 ecdsa_results[k][0], ecdsa_results[k][1]);
0f113f3e 3975 else
48bc0d99 3976 printf("%4u bits ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
d63d89ea 3977 ec_curves[k].bits, ec_curves[k].name,
c8bff7ad
CS
3978 1.0 / ecdsa_results[k][0], 1.0 / ecdsa_results[k][1],
3979 ecdsa_results[k][0], ecdsa_results[k][1]);
0f113f3e 3980 }
7e1b7485 3981
8b0b80d9 3982 testnum = 1;
0f113f3e
MC
3983 for (k = 0; k < EC_NUM; k++) {
3984 if (!ecdh_doit[k])
3985 continue;
8b0b80d9 3986 if (testnum && !mr) {
0f113f3e 3987 printf("%30sop op/s\n", " ");
8b0b80d9 3988 testnum = 0;
0f113f3e
MC
3989 }
3990 if (mr)
7e1b7485 3991 printf("+F5:%u:%u:%f:%f\n",
d63d89ea 3992 k, ec_curves[k].bits,
7e1b7485 3993 ecdh_results[k][0], 1.0 / ecdh_results[k][0]);
0f113f3e
MC
3994
3995 else
48bc0d99 3996 printf("%4u bits ecdh (%s) %8.4fs %8.1f\n",
d63d89ea 3997 ec_curves[k].bits, ec_curves[k].name,
222c3da3 3998 1.0 / ecdh_results[k][0], ecdh_results[k][0]);
0f113f3e 3999 }
d3a9fb10
PY
4000
4001 testnum = 1;
4002 for (k = 0; k < OSSL_NELEM(eddsa_doit); k++) {
4003 if (!eddsa_doit[k])
4004 continue;
4005 if (testnum && !mr) {
4006 printf("%30ssign verify sign/s verify/s\n", " ");
4007 testnum = 0;
4008 }
4009
4010 if (mr)
4011 printf("+F6:%u:%u:%s:%f:%f\n",
d63d89ea 4012 k, ed_curves[k].bits, ed_curves[k].name,
d3a9fb10
PY
4013 eddsa_results[k][0], eddsa_results[k][1]);
4014 else
4015 printf("%4u bits EdDSA (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
d63d89ea 4016 ed_curves[k].bits, ed_curves[k].name,
d3a9fb10
PY
4017 1.0 / eddsa_results[k][0], 1.0 / eddsa_results[k][1],
4018 eddsa_results[k][0], eddsa_results[k][1]);
4019 }
a56f68ad
PY
4020
4021# ifndef OPENSSL_NO_SM2
4022 testnum = 1;
4023 for (k = 0; k < OSSL_NELEM(sm2_doit); k++) {
4024 if (!sm2_doit[k])
4025 continue;
4026 if (testnum && !mr) {
4027 printf("%30ssign verify sign/s verify/s\n", " ");
4028 testnum = 0;
4029 }
4030
4031 if (mr)
031c9bd3 4032 printf("+F7:%u:%u:%s:%f:%f\n",
d63d89ea 4033 k, sm2_curves[k].bits, sm2_curves[k].name,
a56f68ad
PY
4034 sm2_results[k][0], sm2_results[k][1]);
4035 else
4036 printf("%4u bits SM2 (%s) %8.4fs %8.4fs %8.1f %8.1f\n",
d63d89ea 4037 sm2_curves[k].bits, sm2_curves[k].name,
a56f68ad
PY
4038 1.0 / sm2_results[k][0], 1.0 / sm2_results[k][1],
4039 sm2_results[k][0], sm2_results[k][1]);
4040 }
4041# endif
1352e0ff 4042#endif /* OPENSSL_NO_EC */
60d3b5b9
HK
4043#ifndef OPENSSL_NO_DH
4044 testnum = 1;
4045 for (k = 0; k < FFDH_NUM; k++) {
4046 if (!ffdh_doit[k])
4047 continue;
4048 if (testnum && !mr) {
4049 printf("%23sop op/s\n", " ");
4050 testnum = 0;
4051 }
4052 if (mr)
4053 printf("+F8:%u:%u:%f:%f\n",
4054 k, ffdh_params[k].bits,
4055 ffdh_results[k][0], 1.0 / ffdh_results[k][0]);
4056
4057 else
4058 printf("%4u bits ffdh %8.4fs %8.1f\n",
4059 ffdh_params[k].bits,
4060 1.0 / ffdh_results[k][0], ffdh_results[k][0]);
4061 }
4062#endif /* OPENSSL_NO_DH */
0f113f3e 4063
7e1b7485 4064 ret = 0;
0f113f3e
MC
4065
4066 end:
4067 ERR_print_errors(bio_err);
0ff43435 4068 for (i = 0; i < loopargs_len; i++) {
b2839683
AG
4069 OPENSSL_free(loopargs[i].buf_malloc);
4070 OPENSSL_free(loopargs[i].buf2_malloc);
5f986ed3 4071
3a1ee3c1 4072#ifndef OPENSSL_NO_DEPRECATED_3_0
0ff43435
AG
4073 for (k = 0; k < RSA_NUM; k++)
4074 RSA_free(loopargs[i].rsa_key[k]);
a00ae6c4 4075#endif
60d3b5b9
HK
4076#ifndef OPENSSL_NO_DH
4077 OPENSSL_free(loopargs[i].secret_ff_a);
4078 OPENSSL_free(loopargs[i].secret_ff_b);
4079 for (k = 0; k < FFDH_NUM; k++) {
4080 EVP_PKEY_CTX_free(loopargs[i].ffdh_ctx[k]);
4081 }
4082#endif
1ddf2594 4083#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
0ff43435
AG
4084 for (k = 0; k < DSA_NUM; k++)
4085 DSA_free(loopargs[i].dsa_key[k]);
a00ae6c4 4086#endif
10bf4fc2 4087#ifndef OPENSSL_NO_EC
5b5eea4b 4088# if !defined(OPENSSL_NO_DEPRECATED_3_0)
5c6a69f5 4089 for (k = 0; k < ECDSA_NUM; k++)
0ff43435 4090 EC_KEY_free(loopargs[i].ecdsa[k]);
5b5eea4b 4091# endif
5c6a69f5 4092 for (k = 0; k < EC_NUM; k++)
ed7377db 4093 EVP_PKEY_CTX_free(loopargs[i].ecdh_ctx[k]);
1154ffbf 4094 for (k = 0; k < EdDSA_NUM; k++) {
d3a9fb10 4095 EVP_MD_CTX_free(loopargs[i].eddsa_ctx[k]);
1154ffbf
SAS
4096 EVP_MD_CTX_free(loopargs[i].eddsa_ctx2[k]);
4097 }
a56f68ad
PY
4098# ifndef OPENSSL_NO_SM2
4099 for (k = 0; k < SM2_NUM; k++) {
4100 EVP_PKEY_CTX *pctx = NULL;
4101
4102 /* free signing ctx */
4103 if (loopargs[i].sm2_ctx[k] != NULL
4104 && (pctx = EVP_MD_CTX_pkey_ctx(loopargs[i].sm2_ctx[k])) != NULL)
4105 EVP_PKEY_CTX_free(pctx);
4106 EVP_MD_CTX_free(loopargs[i].sm2_ctx[k]);
4107 /* free verification ctx */
4108 if (loopargs[i].sm2_vfy_ctx[k] != NULL
4109 && (pctx = EVP_MD_CTX_pkey_ctx(loopargs[i].sm2_vfy_ctx[k])) != NULL)
4110 EVP_PKEY_CTX_free(pctx);
4111 EVP_MD_CTX_free(loopargs[i].sm2_vfy_ctx[k]);
4112 /* free pkey */
4113 EVP_PKEY_free(loopargs[i].sm2_pkey[k]);
4114 }
4115# endif
b2839683
AG
4116 OPENSSL_free(loopargs[i].secret_a);
4117 OPENSSL_free(loopargs[i].secret_b);
a00ae6c4 4118#endif
5f986ed3 4119 }
dbde4726 4120#ifndef OPENSSL_NO_DEPRECATED_3_0
f88b9b79 4121 OPENSSL_free(evp_hmac_name);
dbde4726 4122#endif
a6d572e6 4123#if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
9bba2c4c
BE
4124 OPENSSL_free(evp_cmac_name);
4125#endif
5f986ed3 4126
1e613922
AG
4127 if (async_jobs > 0) {
4128 for (i = 0; i < loopargs_len; i++)
4129 ASYNC_WAIT_CTX_free(loopargs[i].wait_ctx);
dab1f5fe 4130 }
1e613922 4131
dab1f5fe 4132 if (async_init) {
8b0b80d9 4133 ASYNC_cleanup_thread();
1e613922
AG
4134 }
4135 OPENSSL_free(loopargs);
dd1abd44 4136 release_engine(e);
128d25ba
DB
4137 if (fetched_alg) {
4138 EVP_MD_free(evp_md);
4139 EVP_CIPHER_free(evp_cipher);
4140 }
26a7d938 4141 return ret;
0f113f3e 4142}
d02b48c6 4143
64daf14d 4144static void print_message(const char *s, long num, int length, int tm)
0f113f3e 4145{
a00ae6c4 4146#ifdef SIGALRM
0f113f3e
MC
4147 BIO_printf(bio_err,
4148 mr ? "+DT:%s:%d:%d\n"
64daf14d 4149 : "Doing %s for %ds on %d size blocks: ", s, tm, length);
0f113f3e 4150 (void)BIO_flush(bio_err);
f3fdfbf7 4151 run = 1;
64daf14d 4152 alarm(tm);
a00ae6c4 4153#else
0f113f3e
MC
4154 BIO_printf(bio_err,
4155 mr ? "+DN:%s:%ld:%d\n"
4156 : "Doing %s %ld times on %d size blocks: ", s, num, length);
4157 (void)BIO_flush(bio_err);
a00ae6c4 4158#endif
0f113f3e 4159}
d02b48c6 4160
3a1ee3c1
RL
4161#if !defined(OPENSSL_NO_DEPRECATED_3_0) \
4162 || !defined(OPENSSL_NO_DSA) \
4163 || !defined(OPENSSL_NO_DH) \
4164 || !defined(OPENSSL_NO_EC)
689c6f25 4165static void pkey_print_message(const char *str, const char *str2, long num,
48bc0d99 4166 unsigned int bits, int tm)
0f113f3e 4167{
8c885145 4168# ifdef SIGALRM
0f113f3e
MC
4169 BIO_printf(bio_err,
4170 mr ? "+DTP:%d:%s:%s:%d\n"
48bc0d99 4171 : "Doing %u bits %s %s's for %ds: ", bits, str, str2, tm);
0f113f3e 4172 (void)BIO_flush(bio_err);
6e49b514 4173 run = 1;
0f113f3e 4174 alarm(tm);
8c885145 4175# else
0f113f3e
MC
4176 BIO_printf(bio_err,
4177 mr ? "+DNP:%ld:%d:%s:%s\n"
48bc0d99 4178 : "Doing %ld %u bits %s %s's: ", num, bits, str, str2);
0f113f3e 4179 (void)BIO_flush(bio_err);
8c885145 4180# endif
0f113f3e 4181}
8c885145 4182#endif
58964a49 4183
0f113f3e
MC
4184static void print_result(int alg, int run_no, int count, double time_used)
4185{
d166ed8c 4186 if (count == -1) {
af0857f0
F
4187 BIO_printf(bio_err, "%s error!\n", names[alg]);
4188 ERR_print_errors(bio_err);
4189 /* exit(1); disable exit until default provider enabled */
4190 return;
d166ed8c 4191 }
0f113f3e
MC
4192 BIO_printf(bio_err,
4193 mr ? "+R:%d:%s:%f\n"
4194 : "%d %s's in %.2fs\n", count, names[alg], time_used);
4195 results[alg][run_no] = ((double)count) / time_used * lengths[run_no];
4196}
0e211563 4197
a00ae6c4 4198#ifndef NO_FORK
0e211563 4199static char *sstrsep(char **string, const char *delim)
0f113f3e 4200{
0e211563
BL
4201 char isdelim[256];
4202 char *token = *string;
4203
4204 if (**string == 0)
4205 return NULL;
4206
cbe29648 4207 memset(isdelim, 0, sizeof(isdelim));
0e211563
BL
4208 isdelim[0] = 1;
4209
0f113f3e 4210 while (*delim) {
0e211563
BL
4211 isdelim[(unsigned char)(*delim)] = 1;
4212 delim++;
0f113f3e 4213 }
0e211563 4214
0f113f3e 4215 while (!isdelim[(unsigned char)(**string)]) {
0e211563 4216 (*string)++;
0f113f3e 4217 }
0e211563 4218
0f113f3e 4219 if (**string) {
0e211563
BL
4220 **string = 0;
4221 (*string)++;
0f113f3e 4222 }
0e211563
BL
4223
4224 return token;
0f113f3e 4225}
0e211563 4226
64daf14d 4227static int do_multi(int multi, int size_num)
0f113f3e
MC
4228{
4229 int n;
4230 int fd[2];
4231 int *fds;
4232 static char sep[] = ":";
4233
8e51a340 4234 fds = app_malloc(sizeof(*fds) * multi, "fd buffer for do_multi");
0f113f3e
MC
4235 for (n = 0; n < multi; ++n) {
4236 if (pipe(fd) == -1) {
7768e116 4237 BIO_printf(bio_err, "pipe failure\n");
0f113f3e
MC
4238 exit(1);
4239 }
4240 fflush(stdout);
7768e116 4241 (void)BIO_flush(bio_err);
0f113f3e
MC
4242 if (fork()) {
4243 close(fd[1]);
4244 fds[n] = fd[0];
4245 } else {
4246 close(fd[0]);
4247 close(1);
4248 if (dup(fd[1]) == -1) {
7768e116 4249 BIO_printf(bio_err, "dup failed\n");
0f113f3e
MC
4250 exit(1);
4251 }
4252 close(fd[1]);
4253 mr = 1;
4254 usertime = 0;
b481fbe6 4255 OPENSSL_free(fds);
0f113f3e
MC
4256 return 0;
4257 }
4258 printf("Forked child %d\n", n);
4259 }
e172d60d 4260
0f113f3e
MC
4261 /* for now, assume the pipe is long enough to take all the output */
4262 for (n = 0; n < multi; ++n) {
4263 FILE *f;
4264 char buf[1024];
4265 char *p;
4266
4267 f = fdopen(fds[n], "r");
cbe29648 4268 while (fgets(buf, sizeof(buf), f)) {
0f113f3e
MC
4269 p = strchr(buf, '\n');
4270 if (p)
4271 *p = '\0';
4272 if (buf[0] != '+') {
29dd15b1
NT
4273 BIO_printf(bio_err,
4274 "Don't understand line '%s' from child %d\n", buf,
4275 n);
0f113f3e
MC
4276 continue;
4277 }
4278 printf("Got: %s from %d\n", buf, n);
86885c28 4279 if (strncmp(buf, "+F:", 3) == 0) {
0f113f3e
MC
4280 int alg;
4281 int j;
4282
4283 p = buf + 3;
4284 alg = atoi(sstrsep(&p, sep));
4285 sstrsep(&p, sep);
64daf14d 4286 for (j = 0; j < size_num; ++j)
0f113f3e 4287 results[alg][j] += atof(sstrsep(&p, sep));
c5f87134 4288 }
3a1ee3c1 4289#ifndef OPENSSL_NO_DEPRECATED_3_0
c5f87134 4290 else if (strncmp(buf, "+F2:", 4) == 0) {
0f113f3e
MC
4291 int k;
4292 double d;
4293
4294 p = buf + 4;
4295 k = atoi(sstrsep(&p, sep));
4296 sstrsep(&p, sep);
4297
0f113f3e 4298 d = atof(sstrsep(&p, sep));
8ac2d1ab 4299 rsa_results[k][0] += d;
0f113f3e
MC
4300
4301 d = atof(sstrsep(&p, sep));
8ac2d1ab 4302 rsa_results[k][1] += d;
0f113f3e 4303 }
c5f87134 4304#endif
1ddf2594 4305#if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
86885c28 4306 else if (strncmp(buf, "+F3:", 4) == 0) {
0f113f3e
MC
4307 int k;
4308 double d;
4309
4310 p = buf + 4;
4311 k = atoi(sstrsep(&p, sep));
4312 sstrsep(&p, sep);
4313
4314 d = atof(sstrsep(&p, sep));
0d4de756 4315 dsa_results[k][0] += d;
0f113f3e
MC
4316
4317 d = atof(sstrsep(&p, sep));
0d4de756 4318 dsa_results[k][1] += d;
0f113f3e 4319 }
a00ae6c4 4320# endif
10bf4fc2 4321# ifndef OPENSSL_NO_EC
86885c28 4322 else if (strncmp(buf, "+F4:", 4) == 0) {
0f113f3e
MC
4323 int k;
4324 double d;
4325
4326 p = buf + 4;
4327 k = atoi(sstrsep(&p, sep));
4328 sstrsep(&p, sep);
4329
4330 d = atof(sstrsep(&p, sep));
c8bff7ad 4331 ecdsa_results[k][0] += d;
0f113f3e
MC
4332
4333 d = atof(sstrsep(&p, sep));
c8bff7ad 4334 ecdsa_results[k][1] += d;
d6073e27 4335 } else if (strncmp(buf, "+F5:", 4) == 0) {
0f113f3e
MC
4336 int k;
4337 double d;
4338
4339 p = buf + 4;
4340 k = atoi(sstrsep(&p, sep));
4341 sstrsep(&p, sep);
4342
4343 d = atof(sstrsep(&p, sep));
222c3da3 4344 ecdh_results[k][0] += d;
d3a9fb10
PY
4345 } else if (strncmp(buf, "+F6:", 4) == 0) {
4346 int k;
4347 double d;
4348
4349 p = buf + 4;
4350 k = atoi(sstrsep(&p, sep));
4351 sstrsep(&p, sep);
1ac7e153 4352 sstrsep(&p, sep);
d3a9fb10
PY
4353
4354 d = atof(sstrsep(&p, sep));
4355 eddsa_results[k][0] += d;
4356
4357 d = atof(sstrsep(&p, sep));
4358 eddsa_results[k][1] += d;
0f113f3e 4359 }
a56f68ad
PY
4360# ifndef OPENSSL_NO_SM2
4361 else if (strncmp(buf, "+F7:", 4) == 0) {
4362 int k;
4363 double d;
4364
4365 p = buf + 4;
4366 k = atoi(sstrsep(&p, sep));
4367 sstrsep(&p, sep);
031c9bd3 4368 sstrsep(&p, sep);
a56f68ad
PY
4369
4370 d = atof(sstrsep(&p, sep));
4371 sm2_results[k][0] += d;
4372
4373 d = atof(sstrsep(&p, sep));
4374 sm2_results[k][1] += d;
4375 }
4376# endif /* OPENSSL_NO_SM2 */
60d3b5b9
HK
4377# endif /* OPENSSL_NO_EC */
4378# ifndef OPENSSL_NO_DH
4379 else if (strncmp(buf, "+F8:", 4) == 0) {
4380 int k;
4381 double d;
4382
4383 p = buf + 4;
4384 k = atoi(sstrsep(&p, sep));
4385 sstrsep(&p, sep);
4386
4387 d = atof(sstrsep(&p, sep));
4388 ffdh_results[k][0] += d;
4389 }
4390# endif /* OPENSSL_NO_DH */
0f113f3e 4391
86885c28 4392 else if (strncmp(buf, "+H:", 3) == 0) {
7e1b7485 4393 ;
0f113f3e 4394 } else
29dd15b1
NT
4395 BIO_printf(bio_err, "Unknown type '%s' from child %d\n", buf,
4396 n);
0f113f3e
MC
4397 }
4398
4399 fclose(f);
4400 }
b481fbe6 4401 OPENSSL_free(fds);
0f113f3e
MC
4402 return 1;
4403}
a00ae6c4 4404#endif
375a64e3 4405
5c6a69f5 4406static void multiblock_speed(const EVP_CIPHER *evp_cipher, int lengths_single,
8f26f9d5 4407 const openssl_speed_sec_t *seconds)
0f113f3e 4408{
64daf14d 4409 static const int mblengths_list[] =
0f113f3e 4410 { 8 * 1024, 2 * 8 * 1024, 4 * 8 * 1024, 8 * 8 * 1024, 8 * 16 * 1024 };
64daf14d 4411 const int *mblengths = mblengths_list;
6b1fe3d0 4412 int j, count, keylen, num = OSSL_NELEM(mblengths_list);
0f113f3e 4413 const char *alg_name;
6b1fe3d0 4414 unsigned char *inp, *out, *key, no_key[32], no_iv[16];
846ec07d 4415 EVP_CIPHER_CTX *ctx;
0f113f3e
MC
4416 double d = 0.0;
4417
64daf14d
PS
4418 if (lengths_single) {
4419 mblengths = &lengths_single;
4420 num = 1;
4421 }
4422
68dc6824
RS
4423 inp = app_malloc(mblengths[num - 1], "multiblock input buffer");
4424 out = app_malloc(mblengths[num - 1] + 1024, "multiblock output buffer");
846ec07d 4425 ctx = EVP_CIPHER_CTX_new();
6b1fe3d0
PS
4426 EVP_EncryptInit_ex(ctx, evp_cipher, NULL, NULL, no_iv);
4427
4428 keylen = EVP_CIPHER_CTX_key_length(ctx);
4429 key = app_malloc(keylen, "evp_cipher key");
4430 EVP_CIPHER_CTX_rand_key(ctx, key);
4431 EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL);
4432 OPENSSL_clear_free(key, keylen);
4433
29dd15b1 4434 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_MAC_KEY, sizeof(no_key), no_key);
6c2ff56e 4435 alg_name = OBJ_nid2ln(EVP_CIPHER_nid(evp_cipher));
0f113f3e
MC
4436
4437 for (j = 0; j < num; j++) {
64daf14d 4438 print_message(alg_name, 0, mblengths[j], seconds->sym);
0f113f3e 4439 Time_F(START);
f3fdfbf7 4440 for (count = 0; run && count < 0x7fffffff; count++) {
c8269881 4441 unsigned char aad[EVP_AEAD_TLS1_AAD_LEN];
0f113f3e
MC
4442 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM mb_param;
4443 size_t len = mblengths[j];
4444 int packlen;
4445
4446 memset(aad, 0, 8); /* avoid uninitialized values */
4447 aad[8] = 23; /* SSL3_RT_APPLICATION_DATA */
4448 aad[9] = 3; /* version */
4449 aad[10] = 2;
4450 aad[11] = 0; /* length */
4451 aad[12] = 0;
4452 mb_param.out = NULL;
4453 mb_param.inp = aad;
4454 mb_param.len = len;
4455 mb_param.interleave = 8;
4456
846ec07d 4457 packlen = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_AAD,
0f113f3e
MC
4458 sizeof(mb_param), &mb_param);
4459
4460 if (packlen > 0) {
4461 mb_param.out = out;
4462 mb_param.inp = inp;
4463 mb_param.len = len;
846ec07d 4464 EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT,
0f113f3e
MC
4465 sizeof(mb_param), &mb_param);
4466 } else {
4467 int pad;
4468
4469 RAND_bytes(out, 16);
4470 len += 16;
3a63c0ed
AP
4471 aad[11] = (unsigned char)(len >> 8);
4472 aad[12] = (unsigned char)(len);
846ec07d 4473 pad = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD,
c8269881 4474 EVP_AEAD_TLS1_AAD_LEN, aad);
846ec07d 4475 EVP_Cipher(ctx, out, inp, len + pad);
0f113f3e
MC
4476 }
4477 }
4478 d = Time_F(STOP);
7e1b7485 4479 BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n"
0f113f3e
MC
4480 : "%d %s's in %.2fs\n", count, "evp", d);
4481 results[D_EVP][j] = ((double)count) / d * mblengths[j];
4482 }
4483
4484 if (mr) {
4485 fprintf(stdout, "+H");
4486 for (j = 0; j < num; j++)
4487 fprintf(stdout, ":%d", mblengths[j]);
4488 fprintf(stdout, "\n");
4489 fprintf(stdout, "+F:%d:%s", D_EVP, alg_name);
4490 for (j = 0; j < num; j++)
4491 fprintf(stdout, ":%.2f", results[D_EVP][j]);
4492 fprintf(stdout, "\n");
4493 } else {
4494 fprintf(stdout,
4495 "The 'numbers' are in 1000s of bytes per second processed.\n");
4496 fprintf(stdout, "type ");
4497 for (j = 0; j < num; j++)
4498 fprintf(stdout, "%7d bytes", mblengths[j]);
4499 fprintf(stdout, "\n");
4500 fprintf(stdout, "%-24s", alg_name);
4501
4502 for (j = 0; j < num; j++) {
4503 if (results[D_EVP][j] > 10000)
4504 fprintf(stdout, " %11.2fk", results[D_EVP][j] / 1e3);
4505 else
4506 fprintf(stdout, " %11.2f ", results[D_EVP][j]);
4507 }
4508 fprintf(stdout, "\n");
4509 }
4510
b548a1f1
RS
4511 OPENSSL_free(inp);
4512 OPENSSL_free(out);
846ec07d 4513 EVP_CIPHER_CTX_free(ctx);
0f113f3e 4514}