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