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