source: rtems-libbsd/freebsd/crypto/openssl/apps/speed.c @ 0fecf49

5
Last change on this file since 0fecf49 was 0fecf49, checked in by Christian Mauderer <christian.mauderer@…>, on 03/26/19 at 09:19:22

bin/openssl: Import from FreeBSD.

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