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