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