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