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