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