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