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