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