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