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