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