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