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