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