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