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