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