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