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