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