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