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

5
Last change on this file since d1dac78 was d1dac78, checked in by Christian Mauderer <christian.mauderer@…>, on 03/26/19 at 10:08:47

bin/openssl: Port to RTEMS.

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