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