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