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