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