Allow world to build with gcc3, basically force openssl to use gettimeofday
[dragonfly.git] / secure / usr.bin / openssl / 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 /* most of this code has been pilfered from my libdes speed.c program */
60
61 /* $DragonFly: src/secure/usr.bin/openssl/apps/Attic/speed.c,v 1.1 2004/02/05 04:22:22 rob Exp $
62 */
63 #ifndef OPENSSL_NO_SPEED
64
65 #undef SECONDS
66 #define SECONDS         3       
67 #define RSA_SECONDS     10
68 #define DSA_SECONDS     10
69
70 /* 11-Sep-92 Andrew Daviel   Support for Silicon Graphics IRIX added */
71 /* 06-Apr-92 Luke Brennan    Support for VMS and add extra signal calls */
72
73 #undef PROG
74 #define PROG speed_main
75
76 #include <stdio.h>
77 #include <stdlib.h>
78 #include <signal.h>
79 #include <string.h>
80 #include <math.h>
81 #include "apps.h"
82 #ifdef OPENSSL_NO_STDIO
83 #define APPS_WIN16
84 #endif
85 #include <openssl/crypto.h>
86 #include <openssl/rand.h>
87 #include <openssl/err.h>
88 #include <openssl/evp.h>
89 #include <openssl/objects.h>
90 #if !defined(OPENSSL_SYS_MSDOS)
91 #include OPENSSL_UNISTD
92 #endif
93
94 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(OPENSSL_SYS_MACOSX) || defined (__DragonFLY)
95 # define USE_TOD
96 #elif !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VXWORKS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC))
97 # define TIMES
98 #endif
99 #if !defined(_UNICOS) && !defined(__OpenBSD__) && !defined(sgi) && !defined(__FreeBSD__) && !(defined(__bsdi) || defined(__bsdi__)) && !defined(_AIX) && !defined(OPENSSL_SYS_MPE) && !defined(__NetBSD__) && !defined(OPENSSL_SYS_VXWORKS) && !defined (__DragonFly) /* FIXME */
100 # define TIMEB
101 #endif
102
103 #ifndef _IRIX
104 # include <time.h>
105 #endif
106 #ifdef TIMES
107 # include <sys/types.h>
108 # include <sys/times.h>
109 #endif
110 #ifdef USE_TOD
111 # include <sys/time.h>
112 # include <sys/resource.h>
113 #endif
114
115 /* Depending on the VMS version, the tms structure is perhaps defined.
116    The __TMS macro will show if it was.  If it wasn't defined, we should
117    undefine TIMES, since that tells the rest of the program how things
118    should be handled.                           -- Richard Levitte */
119 #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
120 #undef TIMES
121 #endif
122
123 #ifdef TIMEB
124 #include <sys/timeb.h>
125 #endif
126
127 #if !defined(TIMES) && !defined(TIMEB) && !defined(USE_TOD) && !defined(OPENSSL_SYS_VXWORKS)
128 #error "It seems neither struct tms nor struct timeb is supported in this platform!"
129 #endif
130
131 #if defined(sun) || defined(__ultrix)
132 #define _POSIX_SOURCE
133 #include <limits.h>
134 #include <sys/param.h>
135 #endif
136
137 #ifndef OPENSSL_NO_DES
138 #include <openssl/des.h>
139 #endif
140 #ifndef OPENSSL_NO_AES
141 #include <openssl/aes.h>
142 #endif
143 #ifndef OPENSSL_NO_MD2
144 #include <openssl/md2.h>
145 #endif
146 #ifndef OPENSSL_NO_MDC2
147 #include <openssl/mdc2.h>
148 #endif
149 #ifndef OPENSSL_NO_MD4
150 #include <openssl/md4.h>
151 #endif
152 #ifndef OPENSSL_NO_MD5
153 #include <openssl/md5.h>
154 #endif
155 #ifndef OPENSSL_NO_HMAC
156 #include <openssl/hmac.h>
157 #endif
158 #include <openssl/evp.h>
159 #ifndef OPENSSL_NO_SHA
160 #include <openssl/sha.h>
161 #endif
162 #ifndef OPENSSL_NO_RIPEMD
163 #include <openssl/ripemd.h>
164 #endif
165 #ifndef OPENSSL_NO_RC4
166 #include <openssl/rc4.h>
167 #endif
168 #ifndef OPENSSL_NO_RC5
169 #include <openssl/rc5.h>
170 #endif
171 #ifndef OPENSSL_NO_RC2
172 #include <openssl/rc2.h>
173 #endif
174 #ifndef OPENSSL_NO_IDEA
175 #include <openssl/idea.h>
176 #endif
177 #ifndef OPENSSL_NO_BF
178 #include <openssl/blowfish.h>
179 #endif
180 #ifndef OPENSSL_NO_CAST
181 #include <openssl/cast.h>
182 #endif
183 #ifndef OPENSSL_NO_RSA
184 #include <openssl/rsa.h>
185 #include "./testrsa.h"
186 #endif
187 #include <openssl/x509.h>
188 #ifndef OPENSSL_NO_DSA
189 #include "./testdsa.h"
190 #endif
191
192 /* The following if from times(3) man page.  It may need to be changed */
193 #ifndef HZ
194 # if defined(_SC_CLK_TCK) \
195      && (!defined(OPENSSL_SYS_VMS) || __CTRL_VER >= 70000000)
196 #  define HZ ((double)sysconf(_SC_CLK_TCK))
197 # else
198 #  ifndef CLK_TCK
199 #   ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
200 #    define HZ  100.0
201 #   else /* _BSD_CLK_TCK_ */
202 #    define HZ ((double)_BSD_CLK_TCK_)
203 #   endif
204 #  else /* CLK_TCK */
205 #   define HZ ((double)CLK_TCK)
206 #  endif
207 # endif
208 #endif
209
210 #if !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(OPENSSL_SYS_OS2)
211 # define HAVE_FORK 1
212 #endif
213
214 #undef BUFSIZE
215 #define BUFSIZE ((long)1024*8+1)
216 int run=0;
217
218 static char ftime_used = 0, times_used = 0, gettimeofday_used = 1, getrusage_used = 0;
219 static int mr=0;
220 static int usertime=1;
221
222 static double Time_F(int s);
223 static void print_message(const char *s,long num,int length);
224 static void pkey_print_message(char *str,char *str2,long num,int bits,int sec);
225 static void print_result(int alg,int run_no,int count,double time_used);
226 #ifdef HAVE_FORK
227 static int do_multi(int multi);
228 #endif
229
230 #define ALGOR_NUM       19
231 #define SIZE_NUM        5
232 #define RSA_NUM         4
233 #define DSA_NUM         3
234 static const char *names[ALGOR_NUM]={
235   "md2","mdc2","md4","md5","hmac(md5)","sha1","rmd160","rc4",
236   "des cbc","des ede3","idea cbc",
237   "rc2 cbc","rc5-32/12 cbc","blowfish cbc","cast cbc",
238   "aes-128 cbc","aes-192 cbc","aes-256 cbc"};
239 static double results[ALGOR_NUM][SIZE_NUM];
240 static int lengths[SIZE_NUM]={16,64,256,1024,8*1024};
241 static double rsa_results[RSA_NUM][2];
242 static double dsa_results[DSA_NUM][2];
243
244 #ifdef SIGALRM
245 #if defined(__STDC__) || defined(sgi) || defined(_AIX)
246 #define SIGRETTYPE void
247 #else
248 #define SIGRETTYPE int
249 #endif 
250
251 static SIGRETTYPE sig_done(int sig);
252 static SIGRETTYPE sig_done(int sig)
253         {
254         signal(SIGALRM,sig_done);
255         run=0;
256 #ifdef LINT
257         sig=sig;
258 #endif
259         }
260 #endif
261
262 #define START   0
263 #define STOP    1
264
265 static double Time_F(int s)
266         {
267         double ret;
268
269 #ifdef USE_TOD
270         if(usertime)
271             {
272                 static struct rusage tstart,tend;
273
274                 getrusage_used = 1;
275                 if (s == START)
276                         {
277                         getrusage(RUSAGE_SELF,&tstart);
278                         return(0);
279                         }
280                 else
281                         {
282                         long i;
283
284                         getrusage(RUSAGE_SELF,&tend);
285                         i=(long)tend.ru_utime.tv_usec-(long)tstart.ru_utime.tv_usec;
286                         ret=((double)(tend.ru_utime.tv_sec-tstart.ru_utime.tv_sec))
287                           +((double)i)/1000000.0;
288                         return((ret < 0.001)?0.001:ret);
289                         }
290                 }
291         else
292                 {
293                 static struct timeval tstart,tend;
294                 long i;
295
296                 gettimeofday_used = 1;
297                 if (s == START)
298                         {
299                         gettimeofday(&tstart,NULL);
300                         return(0);
301                         }
302                 else
303                         {
304                         gettimeofday(&tend,NULL);
305                         i=(long)tend.tv_usec-(long)tstart.tv_usec;
306                         ret=((double)(tend.tv_sec-tstart.tv_sec))+((double)i)/1000000.0;
307                         return((ret < 0.001)?0.001:ret);
308                         }
309                 }
310 #else  /* ndef USE_TOD */
311                 
312 # ifdef TIMES
313         if (usertime)
314                 {
315                 static struct tms tstart,tend;
316
317                 times_used = 1;
318                 if (s == START)
319                         {
320                         times(&tstart);
321                         return(0);
322                         }
323                 else
324                         {
325                         times(&tend);
326                         ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
327                         return((ret < 1e-3)?1e-3:ret);
328                         }
329                 }
330 # endif /* times() */
331 # if defined(TIMES) && defined(TIMEB)
332         else
333 # endif
334 # ifdef OPENSSL_SYS_VXWORKS
335                 {
336                 static unsigned long tick_start, tick_end;
337
338                 if( s == START )
339                         {
340                         tick_start = tickGet();
341                         return 0;
342                         }
343                 else
344                         {
345                         tick_end = tickGet();
346                         ret = (double)(tick_end - tick_start) / (double)sysClkRateGet();
347                         return((ret < 0.001)?0.001:ret);
348                         }
349                 }
350 # elif defined(TIMEB)
351                 {
352                 static struct timeb tstart,tend;
353                 long i;
354
355                 ftime_used = 0;
356                 if (s == START)
357                         {
358                         ftime(&tstart);
359                         return(0);
360                         }
361                 else
362                         {
363                         ftime(&tend);
364                         i=(long)tend.millitm-(long)tstart.millitm;
365                         ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
366                         return((ret < 0.001)?0.001:ret);
367                         }
368                 }
369 # endif
370 #endif
371         }
372
373 int MAIN(int, char **);
374
375 int MAIN(int argc, char **argv)
376         {
377 #ifndef OPENSSL_NO_ENGINE
378         ENGINE *e = NULL;
379 #endif
380         unsigned char *buf=NULL,*buf2=NULL;
381         int mret=1;
382         long count=0,save_count=0;
383         int i,j,k;
384 #if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA)
385         long rsa_count;
386 #endif
387 #ifndef OPENSSL_NO_RSA
388         unsigned rsa_num;
389 #endif
390         unsigned char md[EVP_MAX_MD_SIZE];
391 #ifndef OPENSSL_NO_MD2
392         unsigned char md2[MD2_DIGEST_LENGTH];
393 #endif
394 #ifndef OPENSSL_NO_MDC2
395         unsigned char mdc2[MDC2_DIGEST_LENGTH];
396 #endif
397 #ifndef OPENSSL_NO_MD4
398         unsigned char md4[MD4_DIGEST_LENGTH];
399 #endif
400 #ifndef OPENSSL_NO_MD5
401         unsigned char md5[MD5_DIGEST_LENGTH];
402         unsigned char hmac[MD5_DIGEST_LENGTH];
403 #endif
404 #ifndef OPENSSL_NO_SHA
405         unsigned char sha[SHA_DIGEST_LENGTH];
406 #endif
407 #ifndef OPENSSL_NO_RIPEMD
408         unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
409 #endif
410 #ifndef OPENSSL_NO_RC4
411         RC4_KEY rc4_ks;
412 #endif
413 #ifndef OPENSSL_NO_RC5
414         RC5_32_KEY rc5_ks;
415 #endif
416 #ifndef OPENSSL_NO_RC2
417         RC2_KEY rc2_ks;
418 #endif
419 #ifndef OPENSSL_NO_IDEA
420         IDEA_KEY_SCHEDULE idea_ks;
421 #endif
422 #ifndef OPENSSL_NO_BF
423         BF_KEY bf_ks;
424 #endif
425 #ifndef OPENSSL_NO_CAST
426         CAST_KEY cast_ks;
427 #endif
428         static const unsigned char key16[16]=
429                 {0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
430                  0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
431         static const unsigned char key24[24]=
432                 {0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
433                  0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,
434                  0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
435         static const unsigned char key32[32]=
436                 {0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
437                  0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,
438                  0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34,
439                  0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34,0x56};
440 #ifndef OPENSSL_NO_AES
441 #define MAX_BLOCK_SIZE 128
442 #else
443 #define MAX_BLOCK_SIZE 64
444 #endif
445         unsigned char DES_iv[8];
446         unsigned char iv[MAX_BLOCK_SIZE/8];
447 #ifndef OPENSSL_NO_DES
448         DES_cblock *buf_as_des_cblock = NULL;
449         static DES_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
450         static DES_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
451         static DES_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
452         DES_key_schedule sch;
453         DES_key_schedule sch2;
454         DES_key_schedule sch3;
455 #endif
456 #ifndef OPENSSL_NO_AES
457         AES_KEY aes_ks1, aes_ks2, aes_ks3;
458 #endif
459 #define D_MD2           0
460 #define D_MDC2          1
461 #define D_MD4           2
462 #define D_MD5           3
463 #define D_HMAC          4
464 #define D_SHA1          5
465 #define D_RMD160        6
466 #define D_RC4           7
467 #define D_CBC_DES       8
468 #define D_EDE3_DES      9
469 #define D_CBC_IDEA      10
470 #define D_CBC_RC2       11
471 #define D_CBC_RC5       12
472 #define D_CBC_BF        13
473 #define D_CBC_CAST      14
474 #define D_CBC_128_AES   15
475 #define D_CBC_192_AES   16
476 #define D_CBC_256_AES   17
477 #define D_EVP           18
478         double d=0.0;
479         long c[ALGOR_NUM][SIZE_NUM];
480 #define R_DSA_512       0
481 #define R_DSA_1024      1
482 #define R_DSA_2048      2
483 #define R_RSA_512       0
484 #define R_RSA_1024      1
485 #define R_RSA_2048      2
486 #define R_RSA_4096      3
487 #ifndef OPENSSL_NO_RSA
488         RSA *rsa_key[RSA_NUM];
489         long rsa_c[RSA_NUM][2];
490         static unsigned int rsa_bits[RSA_NUM]={512,1024,2048,4096};
491         static unsigned char *rsa_data[RSA_NUM]=
492                 {test512,test1024,test2048,test4096};
493         static int rsa_data_length[RSA_NUM]={
494                 sizeof(test512),sizeof(test1024),
495                 sizeof(test2048),sizeof(test4096)};
496 #endif
497 #ifndef OPENSSL_NO_DSA
498         DSA *dsa_key[DSA_NUM];
499         long dsa_c[DSA_NUM][2];
500         static unsigned int dsa_bits[DSA_NUM]={512,1024,2048};
501 #endif
502         int rsa_doit[RSA_NUM];
503         int dsa_doit[DSA_NUM];
504         int doit[ALGOR_NUM];
505         int pr_header=0;
506         const EVP_CIPHER *evp_cipher=NULL;
507         const EVP_MD *evp_md=NULL;
508         int decrypt=0;
509 #ifdef HAVE_FORK
510         int multi=0;
511 #endif
512
513 #ifndef TIMES
514         usertime=-1;
515 #endif
516
517         apps_startup();
518         memset(results, 0, sizeof(results));
519 #ifndef OPENSSL_NO_DSA
520         memset(dsa_key,0,sizeof(dsa_key));
521 #endif
522
523         if (bio_err == NULL)
524                 if ((bio_err=BIO_new(BIO_s_file())) != NULL)
525                         BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
526
527         if (!load_config(bio_err, NULL))
528                 goto end;
529
530 #ifndef OPENSSL_NO_RSA
531         memset(rsa_key,0,sizeof(rsa_key));
532         for (i=0; i<RSA_NUM; i++)
533                 rsa_key[i]=NULL;
534 #endif
535
536         if ((buf=(unsigned char *)OPENSSL_malloc((int)BUFSIZE)) == NULL)
537                 {
538                 BIO_printf(bio_err,"out of memory\n");
539                 goto end;
540                 }
541 #ifndef OPENSSL_NO_DES
542         buf_as_des_cblock = (DES_cblock *)buf;
543 #endif
544         if ((buf2=(unsigned char *)OPENSSL_malloc((int)BUFSIZE)) == NULL)
545                 {
546                 BIO_printf(bio_err,"out of memory\n");
547                 goto end;
548                 }
549
550         memset(c,0,sizeof(c));
551         memset(DES_iv,0,sizeof(DES_iv));
552         memset(iv,0,sizeof(iv));
553
554         for (i=0; i<ALGOR_NUM; i++)
555                 doit[i]=0;
556         for (i=0; i<RSA_NUM; i++)
557                 rsa_doit[i]=0;
558         for (i=0; i<DSA_NUM; i++)
559                 dsa_doit[i]=0;
560         
561         j=0;
562         argc--;
563         argv++;
564         while (argc)
565                 {
566                 if      ((argc > 0) && (strcmp(*argv,"-elapsed") == 0))
567                         {
568                         usertime = 0;
569                         j--;    /* Otherwise, -elapsed gets confused with
570                                    an algorithm. */
571                         }
572                 else if ((argc > 0) && (strcmp(*argv,"-evp") == 0))
573                         {
574                         argc--;
575                         argv++;
576                         if(argc == 0)
577                                 {
578                                 BIO_printf(bio_err,"no EVP given\n");
579                                 goto end;
580                                 }
581                         evp_cipher=EVP_get_cipherbyname(*argv);
582                         if(!evp_cipher)
583                                 {
584                                 evp_md=EVP_get_digestbyname(*argv);
585                                 }
586                         if(!evp_cipher && !evp_md)
587                                 {
588                                 BIO_printf(bio_err,"%s is an unknown cipher or digest\n",*argv);
589                                 goto end;
590                                 }
591                         doit[D_EVP]=1;
592                         }
593                 else if (argc > 0 && !strcmp(*argv,"-decrypt"))
594                         {
595                         decrypt=1;
596                         j--;    /* Otherwise, -elapsed gets confused with
597                                    an algorithm. */
598                         }
599 #ifndef OPENSSL_NO_ENGINE
600                 else if ((argc > 0) && (strcmp(*argv,"-engine") == 0))
601                         {
602                         argc--;
603                         argv++;
604                         if(argc == 0)
605                                 {
606                                 BIO_printf(bio_err,"no engine given\n");
607                                 goto end;
608                                 }
609                         e = setup_engine(bio_err, *argv, 0);
610                         /* j will be increased again further down.  We just
611                            don't want speed to confuse an engine with an
612                            algorithm, especially when none is given (which
613                            means all of them should be run) */
614                         j--;
615                         }
616 #endif
617 #ifdef HAVE_FORK
618                 else if ((argc > 0) && (strcmp(*argv,"-multi") == 0))
619                         {
620                         argc--;
621                         argv++;
622                         if(argc == 0)
623                                 {
624                                 BIO_printf(bio_err,"no multi count given\n");
625                                 goto end;
626                                 }
627                         multi=atoi(argv[0]);
628                         if(multi <= 0)
629                             {
630                                 BIO_printf(bio_err,"bad multi count\n");
631                                 goto end;
632                                 }                               
633                         j--;    /* Otherwise, -mr gets confused with
634                                    an algorithm. */
635                         }
636 #endif
637                 else if (argc > 0 && !strcmp(*argv,"-mr"))
638                         {
639                         mr=1;
640                         j--;    /* Otherwise, -mr gets confused with
641                                    an algorithm. */
642                         }
643                 else
644 #ifndef OPENSSL_NO_MD2
645                 if      (strcmp(*argv,"md2") == 0) doit[D_MD2]=1;
646                 else
647 #endif
648 #ifndef OPENSSL_NO_MDC2
649                         if (strcmp(*argv,"mdc2") == 0) doit[D_MDC2]=1;
650                 else
651 #endif
652 #ifndef OPENSSL_NO_MD4
653                         if (strcmp(*argv,"md4") == 0) doit[D_MD4]=1;
654                 else
655 #endif
656 #ifndef OPENSSL_NO_MD5
657                         if (strcmp(*argv,"md5") == 0) doit[D_MD5]=1;
658                 else
659 #endif
660 #ifndef OPENSSL_NO_MD5
661                         if (strcmp(*argv,"hmac") == 0) doit[D_HMAC]=1;
662                 else
663 #endif
664 #ifndef OPENSSL_NO_SHA
665                         if (strcmp(*argv,"sha1") == 0) doit[D_SHA1]=1;
666                 else
667                         if (strcmp(*argv,"sha") == 0) doit[D_SHA1]=1;
668                 else
669 #endif
670 #ifndef OPENSSL_NO_RIPEMD
671                         if (strcmp(*argv,"ripemd") == 0) doit[D_RMD160]=1;
672                 else
673                         if (strcmp(*argv,"rmd160") == 0) doit[D_RMD160]=1;
674                 else
675                         if (strcmp(*argv,"ripemd160") == 0) doit[D_RMD160]=1;
676                 else
677 #endif
678 #ifndef OPENSSL_NO_RC4
679                         if (strcmp(*argv,"rc4") == 0) doit[D_RC4]=1;
680                 else 
681 #endif
682 #ifndef OPENSSL_NO_DES
683                         if (strcmp(*argv,"des-cbc") == 0) doit[D_CBC_DES]=1;
684                 else    if (strcmp(*argv,"des-ede3") == 0) doit[D_EDE3_DES]=1;
685                 else
686 #endif
687 #ifndef OPENSSL_NO_AES
688                         if (strcmp(*argv,"aes-128-cbc") == 0) doit[D_CBC_128_AES]=1;
689                 else    if (strcmp(*argv,"aes-192-cbc") == 0) doit[D_CBC_192_AES]=1;
690                 else    if (strcmp(*argv,"aes-256-cbc") == 0) doit[D_CBC_256_AES]=1;
691                 else
692 #endif
693 #ifndef OPENSSL_NO_RSA
694 #if 0 /* was: #ifdef RSAref */
695                         if (strcmp(*argv,"rsaref") == 0) 
696                         {
697                         RSA_set_default_openssl_method(RSA_PKCS1_RSAref());
698                         j--;
699                         }
700                 else
701 #endif
702 #ifndef RSA_NULL
703                         if (strcmp(*argv,"openssl") == 0) 
704                         {
705                         RSA_set_default_method(RSA_PKCS1_SSLeay());
706                         j--;
707                         }
708                 else
709 #endif
710 #endif /* !OPENSSL_NO_RSA */
711                      if (strcmp(*argv,"dsa512") == 0) dsa_doit[R_DSA_512]=2;
712                 else if (strcmp(*argv,"dsa1024") == 0) dsa_doit[R_DSA_1024]=2;
713                 else if (strcmp(*argv,"dsa2048") == 0) dsa_doit[R_DSA_2048]=2;
714                 else if (strcmp(*argv,"rsa512") == 0) rsa_doit[R_RSA_512]=2;
715                 else if (strcmp(*argv,"rsa1024") == 0) rsa_doit[R_RSA_1024]=2;
716                 else if (strcmp(*argv,"rsa2048") == 0) rsa_doit[R_RSA_2048]=2;
717                 else if (strcmp(*argv,"rsa4096") == 0) rsa_doit[R_RSA_4096]=2;
718                 else
719 #ifndef OPENSSL_NO_RC2
720                      if (strcmp(*argv,"rc2-cbc") == 0) doit[D_CBC_RC2]=1;
721                 else if (strcmp(*argv,"rc2") == 0) doit[D_CBC_RC2]=1;
722                 else
723 #endif
724 #ifndef OPENSSL_NO_RC5
725                      if (strcmp(*argv,"rc5-cbc") == 0) doit[D_CBC_RC5]=1;
726                 else if (strcmp(*argv,"rc5") == 0) doit[D_CBC_RC5]=1;
727                 else
728 #endif
729 #ifndef OPENSSL_NO_IDEA
730                      if (strcmp(*argv,"idea-cbc") == 0) doit[D_CBC_IDEA]=1;
731                 else if (strcmp(*argv,"idea") == 0) doit[D_CBC_IDEA]=1;
732                 else
733 #endif
734 #ifndef OPENSSL_NO_BF
735                      if (strcmp(*argv,"bf-cbc") == 0) doit[D_CBC_BF]=1;
736                 else if (strcmp(*argv,"blowfish") == 0) doit[D_CBC_BF]=1;
737                 else if (strcmp(*argv,"bf") == 0) doit[D_CBC_BF]=1;
738                 else
739 #endif
740 #ifndef OPENSSL_NO_CAST
741                      if (strcmp(*argv,"cast-cbc") == 0) doit[D_CBC_CAST]=1;
742                 else if (strcmp(*argv,"cast") == 0) doit[D_CBC_CAST]=1;
743                 else if (strcmp(*argv,"cast5") == 0) doit[D_CBC_CAST]=1;
744                 else
745 #endif
746 #ifndef OPENSSL_NO_DES
747                         if (strcmp(*argv,"des") == 0)
748                         {
749                         doit[D_CBC_DES]=1;
750                         doit[D_EDE3_DES]=1;
751                         }
752                 else
753 #endif
754 #ifndef OPENSSL_NO_AES
755                         if (strcmp(*argv,"aes") == 0)
756                         {
757                         doit[D_CBC_128_AES]=1;
758                         doit[D_CBC_192_AES]=1;
759                         doit[D_CBC_256_AES]=1;
760                         }
761                 else
762 #endif
763 #ifndef OPENSSL_NO_RSA
764                         if (strcmp(*argv,"rsa") == 0)
765                         {
766                         rsa_doit[R_RSA_512]=1;
767                         rsa_doit[R_RSA_1024]=1;
768                         rsa_doit[R_RSA_2048]=1;
769                         rsa_doit[R_RSA_4096]=1;
770                         }
771                 else
772 #endif
773 #ifndef OPENSSL_NO_DSA
774                         if (strcmp(*argv,"dsa") == 0)
775                         {
776                         dsa_doit[R_DSA_512]=1;
777                         dsa_doit[R_DSA_1024]=1;
778                         }
779                 else
780 #endif
781                         {
782                         BIO_printf(bio_err,"Error: bad option or value\n");
783                         BIO_printf(bio_err,"\n");
784                         BIO_printf(bio_err,"Available values:\n");
785 #ifndef OPENSSL_NO_MD2
786                         BIO_printf(bio_err,"md2      ");
787 #endif
788 #ifndef OPENSSL_NO_MDC2
789                         BIO_printf(bio_err,"mdc2     ");
790 #endif
791 #ifndef OPENSSL_NO_MD4
792                         BIO_printf(bio_err,"md4      ");
793 #endif
794 #ifndef OPENSSL_NO_MD5
795                         BIO_printf(bio_err,"md5      ");
796 #ifndef OPENSSL_NO_HMAC
797                         BIO_printf(bio_err,"hmac     ");
798 #endif
799 #endif
800 #ifndef OPENSSL_NO_SHA1
801                         BIO_printf(bio_err,"sha1     ");
802 #endif
803 #ifndef OPENSSL_NO_RIPEMD160
804                         BIO_printf(bio_err,"rmd160");
805 #endif
806 #if !defined(OPENSSL_NO_MD2) || !defined(OPENSSL_NO_MDC2) || \
807     !defined(OPENSSL_NO_MD4) || !defined(OPENSSL_NO_MD5) || \
808     !defined(OPENSSL_NO_SHA1) || !defined(OPENSSL_NO_RIPEMD160)
809                         BIO_printf(bio_err,"\n");
810 #endif
811
812 #ifndef OPENSSL_NO_IDEA
813                         BIO_printf(bio_err,"idea-cbc ");
814 #endif
815 #ifndef OPENSSL_NO_RC2
816                         BIO_printf(bio_err,"rc2-cbc  ");
817 #endif
818 #ifndef OPENSSL_NO_RC5
819                         BIO_printf(bio_err,"rc5-cbc  ");
820 #endif
821 #ifndef OPENSSL_NO_BF
822                         BIO_printf(bio_err,"bf-cbc");
823 #endif
824 #if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_RC2) || \
825     !defined(OPENSSL_NO_BF) || !defined(OPENSSL_NO_RC5)
826                         BIO_printf(bio_err,"\n");
827 #endif
828 #ifndef OPENSSL_NO_DES
829                         BIO_printf(bio_err,"des-cbc  des-ede3 ");
830 #endif
831 #ifndef OPENSSL_NO_AES
832                         BIO_printf(bio_err,"aes-128-cbc aes-192-cbc aes-256-cbc ");
833 #endif
834 #ifndef OPENSSL_NO_RC4
835                         BIO_printf(bio_err,"rc4");
836 #endif
837                         BIO_printf(bio_err,"\n");
838
839 #ifndef OPENSSL_NO_RSA
840                         BIO_printf(bio_err,"rsa512   rsa1024  rsa2048  rsa4096\n");
841 #endif
842
843 #ifndef OPENSSL_NO_DSA
844                         BIO_printf(bio_err,"dsa512   dsa1024  dsa2048\n");
845 #endif
846
847 #ifndef OPENSSL_NO_IDEA
848                         BIO_printf(bio_err,"idea     ");
849 #endif
850 #ifndef OPENSSL_NO_RC2
851                         BIO_printf(bio_err,"rc2      ");
852 #endif
853 #ifndef OPENSSL_NO_DES
854                         BIO_printf(bio_err,"des      ");
855 #endif
856 #ifndef OPENSSL_NO_AES
857                         BIO_printf(bio_err,"aes      ");
858 #endif
859 #ifndef OPENSSL_NO_RSA
860                         BIO_printf(bio_err,"rsa      ");
861 #endif
862 #ifndef OPENSSL_NO_BF
863                         BIO_printf(bio_err,"blowfish");
864 #endif
865 #if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_RC2) || \
866     !defined(OPENSSL_NO_DES) || !defined(OPENSSL_NO_RSA) || \
867     !defined(OPENSSL_NO_BF) || !defined(OPENSSL_NO_AES)
868                         BIO_printf(bio_err,"\n");
869 #endif
870
871                         BIO_printf(bio_err,"\n");
872                         BIO_printf(bio_err,"Available options:\n");
873 #if defined(TIMES) || defined(USE_TOD)
874                         BIO_printf(bio_err,"-elapsed        measure time in real time instead of CPU user time.\n");
875 #endif
876 #ifndef OPENSSL_NO_ENGINE
877                         BIO_printf(bio_err,"-engine e       use engine e, possibly a hardware device.\n");
878 #endif
879                         BIO_printf(bio_err,"-evp e          use EVP e.\n");
880                         BIO_printf(bio_err,"-decrypt        time decryption instead of encryption (only EVP).\n");
881                         BIO_printf(bio_err,"-mr             produce machine readable output.\n");
882 #ifdef HAVE_FORK
883                         BIO_printf(bio_err,"-multi n        run n benchmarks in parallel.\n");
884 #endif
885                         goto end;
886                         }
887                 argc--;
888                 argv++;
889                 j++;
890                 }
891
892 #ifdef HAVE_FORK
893         if(multi && do_multi(multi))
894                 goto show_res;
895 #endif
896
897         if (j == 0)
898                 {
899                 for (i=0; i<ALGOR_NUM; i++)
900                         {
901                         if (i != D_EVP)
902                                 doit[i]=1;
903                         }
904                 for (i=0; i<RSA_NUM; i++)
905                         rsa_doit[i]=1;
906                 for (i=0; i<DSA_NUM; i++)
907                         dsa_doit[i]=1;
908                 }
909         for (i=0; i<ALGOR_NUM; i++)
910                 if (doit[i]) pr_header++;
911
912         if (usertime == 0 && !mr)
913                 BIO_printf(bio_err,"You have chosen to measure elapsed time instead of user CPU time.\n");
914         if (usertime <= 0 && !mr)
915                 {
916                 BIO_printf(bio_err,"To get the most accurate results, try to run this\n");
917                 BIO_printf(bio_err,"program when this computer is idle.\n");
918                 }
919
920 #ifndef OPENSSL_NO_RSA
921         for (i=0; i<RSA_NUM; i++)
922                 {
923                 const unsigned char *p;
924
925                 p=rsa_data[i];
926                 rsa_key[i]=d2i_RSAPrivateKey(NULL,&p,rsa_data_length[i]);
927                 if (rsa_key[i] == NULL)
928                         {
929                         BIO_printf(bio_err,"internal error loading RSA key number %d\n",i);
930                         goto end;
931                         }
932 #if 0
933                 else
934                         {
935                         BIO_printf(bio_err,mr ? "+RK:%d:"
936                                    : "Loaded RSA key, %d bit modulus and e= 0x",
937                                    BN_num_bits(rsa_key[i]->n));
938                         BN_print(bio_err,rsa_key[i]->e);
939                         BIO_printf(bio_err,"\n");
940                         }
941 #endif
942                 }
943 #endif
944
945 #ifndef OPENSSL_NO_DSA
946         dsa_key[0]=get_dsa512();
947         dsa_key[1]=get_dsa1024();
948         dsa_key[2]=get_dsa2048();
949 #endif
950
951 #ifndef OPENSSL_NO_DES
952         DES_set_key_unchecked(&key,&sch);
953         DES_set_key_unchecked(&key2,&sch2);
954         DES_set_key_unchecked(&key3,&sch3);
955 #endif
956 #ifndef OPENSSL_NO_AES
957         AES_set_encrypt_key(key16,128,&aes_ks1);
958         AES_set_encrypt_key(key24,192,&aes_ks2);
959         AES_set_encrypt_key(key32,256,&aes_ks3);
960 #endif
961 #ifndef OPENSSL_NO_IDEA
962         idea_set_encrypt_key(key16,&idea_ks);
963 #endif
964 #ifndef OPENSSL_NO_RC4
965         RC4_set_key(&rc4_ks,16,key16);
966 #endif
967 #ifndef OPENSSL_NO_RC2
968         RC2_set_key(&rc2_ks,16,key16,128);
969 #endif
970 #ifndef OPENSSL_NO_RC5
971         RC5_32_set_key(&rc5_ks,16,key16,12);
972 #endif
973 #ifndef OPENSSL_NO_BF
974         BF_set_key(&bf_ks,16,key16);
975 #endif
976 #ifndef OPENSSL_NO_CAST
977         CAST_set_key(&cast_ks,16,key16);
978 #endif
979 #ifndef OPENSSL_NO_RSA
980         memset(rsa_c,0,sizeof(rsa_c));
981 #endif
982 #ifndef SIGALRM
983 #ifndef OPENSSL_NO_DES
984         BIO_printf(bio_err,"First we calculate the approximate speed ...\n");
985         count=10;
986         do      {
987                 long i;
988                 count*=2;
989                 Time_F(START);
990                 for (i=count; i; i--)
991                         DES_ecb_encrypt(buf_as_des_cblock,buf_as_des_cblock,
992                                 &sch,DES_ENCRYPT);
993                 d=Time_F(STOP);
994                 } while (d <3);
995         save_count=count;
996         c[D_MD2][0]=count/10;
997         c[D_MDC2][0]=count/10;
998         c[D_MD4][0]=count;
999         c[D_MD5][0]=count;
1000         c[D_HMAC][0]=count;
1001         c[D_SHA1][0]=count;
1002         c[D_RMD160][0]=count;
1003         c[D_RC4][0]=count*5;
1004         c[D_CBC_DES][0]=count;
1005         c[D_EDE3_DES][0]=count/3;
1006         c[D_CBC_IDEA][0]=count;
1007         c[D_CBC_RC2][0]=count;
1008         c[D_CBC_RC5][0]=count;
1009         c[D_CBC_BF][0]=count;
1010         c[D_CBC_CAST][0]=count;
1011
1012         for (i=1; i<SIZE_NUM; i++)
1013                 {
1014                 c[D_MD2][i]=c[D_MD2][0]*4*lengths[0]/lengths[i];
1015                 c[D_MDC2][i]=c[D_MDC2][0]*4*lengths[0]/lengths[i];
1016                 c[D_MD4][i]=c[D_MD4][0]*4*lengths[0]/lengths[i];
1017                 c[D_MD5][i]=c[D_MD5][0]*4*lengths[0]/lengths[i];
1018                 c[D_HMAC][i]=c[D_HMAC][0]*4*lengths[0]/lengths[i];
1019                 c[D_SHA1][i]=c[D_SHA1][0]*4*lengths[0]/lengths[i];
1020                 c[D_RMD160][i]=c[D_RMD160][0]*4*lengths[0]/lengths[i];
1021                 }
1022         for (i=1; i<SIZE_NUM; i++)
1023                 {
1024                 long l0,l1;
1025
1026                 l0=(long)lengths[i-1];
1027                 l1=(long)lengths[i];
1028                 c[D_RC4][i]=c[D_RC4][i-1]*l0/l1;
1029                 c[D_CBC_DES][i]=c[D_CBC_DES][i-1]*l0/l1;
1030                 c[D_EDE3_DES][i]=c[D_EDE3_DES][i-1]*l0/l1;
1031                 c[D_CBC_IDEA][i]=c[D_CBC_IDEA][i-1]*l0/l1;
1032                 c[D_CBC_RC2][i]=c[D_CBC_RC2][i-1]*l0/l1;
1033                 c[D_CBC_RC5][i]=c[D_CBC_RC5][i-1]*l0/l1;
1034                 c[D_CBC_BF][i]=c[D_CBC_BF][i-1]*l0/l1;
1035                 c[D_CBC_CAST][i]=c[D_CBC_CAST][i-1]*l0/l1;
1036                 }
1037 #ifndef OPENSSL_NO_RSA
1038         rsa_c[R_RSA_512][0]=count/2000;
1039         rsa_c[R_RSA_512][1]=count/400;
1040         for (i=1; i<RSA_NUM; i++)
1041                 {
1042                 rsa_c[i][0]=rsa_c[i-1][0]/8;
1043                 rsa_c[i][1]=rsa_c[i-1][1]/4;
1044                 if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))
1045                         rsa_doit[i]=0;
1046                 else
1047                         {
1048                         if (rsa_c[i][0] == 0)
1049                                 {
1050                                 rsa_c[i][0]=1;
1051                                 rsa_c[i][1]=20;
1052                                 }
1053                         }                               
1054                 }
1055 #endif
1056
1057 #ifndef OPENSSL_NO_DSA
1058         dsa_c[R_DSA_512][0]=count/1000;
1059         dsa_c[R_DSA_512][1]=count/1000/2;
1060         for (i=1; i<DSA_NUM; i++)
1061                 {
1062                 dsa_c[i][0]=dsa_c[i-1][0]/4;
1063                 dsa_c[i][1]=dsa_c[i-1][1]/4;
1064                 if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))
1065                         dsa_doit[i]=0;
1066                 else
1067                         {
1068                         if (dsa_c[i] == 0)
1069                                 {
1070                                 dsa_c[i][0]=1;
1071                                 dsa_c[i][1]=1;
1072                                 }
1073                         }                               
1074                 }
1075 #endif
1076
1077 #define COND(d) (count < (d))
1078 #define COUNT(d) (d)
1079 #else
1080 /* not worth fixing */
1081 # error "You cannot disable DES on systems without SIGALRM."
1082 #endif /* OPENSSL_NO_DES */
1083 #else
1084 #define COND(c) (run)
1085 #define COUNT(d) (count)
1086         signal(SIGALRM,sig_done);
1087 #endif /* SIGALRM */
1088
1089 #ifndef OPENSSL_NO_MD2
1090         if (doit[D_MD2])
1091                 {
1092                 for (j=0; j<SIZE_NUM; j++)
1093                         {
1094                         print_message(names[D_MD2],c[D_MD2][j],lengths[j]);
1095                         Time_F(START);
1096                         for (count=0,run=1; COND(c[D_MD2][j]); count++)
1097                                 EVP_Digest(buf,(unsigned long)lengths[j],&(md2[0]),NULL,EVP_md2(),NULL);
1098                         d=Time_F(STOP);
1099                         print_result(D_MD2,j,count,d);
1100                         }
1101                 }
1102 #endif
1103 #ifndef OPENSSL_NO_MDC2
1104         if (doit[D_MDC2])
1105                 {
1106                 for (j=0; j<SIZE_NUM; j++)
1107                         {
1108                         print_message(names[D_MDC2],c[D_MDC2][j],lengths[j]);
1109                         Time_F(START);
1110                         for (count=0,run=1; COND(c[D_MDC2][j]); count++)
1111                                 EVP_Digest(buf,(unsigned long)lengths[j],&(mdc2[0]),NULL,EVP_mdc2(),NULL);
1112                         d=Time_F(STOP);
1113                         print_result(D_MDC2,j,count,d);
1114                         }
1115                 }
1116 #endif
1117
1118 #ifndef OPENSSL_NO_MD4
1119         if (doit[D_MD4])
1120                 {
1121                 for (j=0; j<SIZE_NUM; j++)
1122                         {
1123                         print_message(names[D_MD4],c[D_MD4][j],lengths[j]);
1124                         Time_F(START);
1125                         for (count=0,run=1; COND(c[D_MD4][j]); count++)
1126                                 EVP_Digest(&(buf[0]),(unsigned long)lengths[j],&(md4[0]),NULL,EVP_md4(),NULL);
1127                         d=Time_F(STOP);
1128                         print_result(D_MD4,j,count,d);
1129                         }
1130                 }
1131 #endif
1132
1133 #ifndef OPENSSL_NO_MD5
1134         if (doit[D_MD5])
1135                 {
1136                 for (j=0; j<SIZE_NUM; j++)
1137                         {
1138                         print_message(names[D_MD5],c[D_MD5][j],lengths[j]);
1139                         Time_F(START);
1140                         for (count=0,run=1; COND(c[D_MD5][j]); count++)
1141                                 EVP_Digest(&(buf[0]),(unsigned long)lengths[j],&(md5[0]),NULL,EVP_get_digestbyname("md5"),NULL);
1142                         d=Time_F(STOP);
1143                         print_result(D_MD5,j,count,d);
1144                         }
1145                 }
1146 #endif
1147
1148 #if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_HMAC)
1149         if (doit[D_HMAC])
1150                 {
1151                 HMAC_CTX hctx;
1152
1153                 HMAC_CTX_init(&hctx);
1154                 HMAC_Init_ex(&hctx,(unsigned char *)"This is a key...",
1155                         16,EVP_md5(), NULL);
1156
1157                 for (j=0; j<SIZE_NUM; j++)
1158                         {
1159                         print_message(names[D_HMAC],c[D_HMAC][j],lengths[j]);
1160                         Time_F(START);
1161                         for (count=0,run=1; COND(c[D_HMAC][j]); count++)
1162                                 {
1163                                 HMAC_Init_ex(&hctx,NULL,0,NULL,NULL);
1164                                 HMAC_Update(&hctx,buf,lengths[j]);
1165                                 HMAC_Final(&hctx,&(hmac[0]),NULL);
1166                                 }
1167                         d=Time_F(STOP);
1168                         print_result(D_HMAC,j,count,d);
1169                         }
1170                 HMAC_CTX_cleanup(&hctx);
1171                 }
1172 #endif
1173 #ifndef OPENSSL_NO_SHA
1174         if (doit[D_SHA1])
1175                 {
1176                 for (j=0; j<SIZE_NUM; j++)
1177                         {
1178                         print_message(names[D_SHA1],c[D_SHA1][j],lengths[j]);
1179                         Time_F(START);
1180                         for (count=0,run=1; COND(c[D_SHA1][j]); count++)
1181                                 EVP_Digest(buf,(unsigned long)lengths[j],&(sha[0]),NULL,EVP_sha1(),NULL);
1182                         d=Time_F(STOP);
1183                         print_result(D_SHA1,j,count,d);
1184                         }
1185                 }
1186 #endif
1187 #ifndef OPENSSL_NO_RIPEMD
1188         if (doit[D_RMD160])
1189                 {
1190                 for (j=0; j<SIZE_NUM; j++)
1191                         {
1192                         print_message(names[D_RMD160],c[D_RMD160][j],lengths[j]);
1193                         Time_F(START);
1194                         for (count=0,run=1; COND(c[D_RMD160][j]); count++)
1195                                 EVP_Digest(buf,(unsigned long)lengths[j],&(rmd160[0]),NULL,EVP_ripemd160(),NULL);
1196                         d=Time_F(STOP);
1197                         print_result(D_RMD160,j,count,d);
1198                         }
1199                 }
1200 #endif
1201 #ifndef OPENSSL_NO_RC4
1202         if (doit[D_RC4])
1203                 {
1204                 for (j=0; j<SIZE_NUM; j++)
1205                         {
1206                         print_message(names[D_RC4],c[D_RC4][j],lengths[j]);
1207                         Time_F(START);
1208                         for (count=0,run=1; COND(c[D_RC4][j]); count++)
1209                                 RC4(&rc4_ks,(unsigned int)lengths[j],
1210                                         buf,buf);
1211                         d=Time_F(STOP);
1212                         print_result(D_RC4,j,count,d);
1213                         }
1214                 }
1215 #endif
1216 #ifndef OPENSSL_NO_DES
1217         if (doit[D_CBC_DES])
1218                 {
1219                 for (j=0; j<SIZE_NUM; j++)
1220                         {
1221                         print_message(names[D_CBC_DES],c[D_CBC_DES][j],lengths[j]);
1222                         Time_F(START);
1223                         for (count=0,run=1; COND(c[D_CBC_DES][j]); count++)
1224                                 DES_ncbc_encrypt(buf,buf,lengths[j],&sch,
1225                                                  &DES_iv,DES_ENCRYPT);
1226                         d=Time_F(STOP);
1227                         print_result(D_CBC_DES,j,count,d);
1228                         }
1229                 }
1230
1231         if (doit[D_EDE3_DES])
1232                 {
1233                 for (j=0; j<SIZE_NUM; j++)
1234                         {
1235                         print_message(names[D_EDE3_DES],c[D_EDE3_DES][j],lengths[j]);
1236                         Time_F(START);
1237                         for (count=0,run=1; COND(c[D_EDE3_DES][j]); count++)
1238                                 DES_ede3_cbc_encrypt(buf,buf,lengths[j],
1239                                                      &sch,&sch2,&sch3,
1240                                                      &DES_iv,DES_ENCRYPT);
1241                         d=Time_F(STOP);
1242                         print_result(D_EDE3_DES,j,count,d);
1243                         }
1244                 }
1245 #endif
1246 #ifndef OPENSSL_NO_AES
1247         if (doit[D_CBC_128_AES])
1248                 {
1249                 for (j=0; j<SIZE_NUM; j++)
1250                         {
1251                         print_message(names[D_CBC_128_AES],c[D_CBC_128_AES][j],lengths[j]);
1252                         Time_F(START);
1253                         for (count=0,run=1; COND(c[D_CBC_128_AES][j]); count++)
1254                                 AES_cbc_encrypt(buf,buf,
1255                                         (unsigned long)lengths[j],&aes_ks1,
1256                                         iv,AES_ENCRYPT);
1257                         d=Time_F(STOP);
1258                         print_result(D_CBC_128_AES,j,count,d);
1259                         }
1260                 }
1261         if (doit[D_CBC_192_AES])
1262                 {
1263                 for (j=0; j<SIZE_NUM; j++)
1264                         {
1265                         print_message(names[D_CBC_192_AES],c[D_CBC_192_AES][j],lengths[j]);
1266                         Time_F(START);
1267                         for (count=0,run=1; COND(c[D_CBC_192_AES][j]); count++)
1268                                 AES_cbc_encrypt(buf,buf,
1269                                         (unsigned long)lengths[j],&aes_ks2,
1270                                         iv,AES_ENCRYPT);
1271                         d=Time_F(STOP);
1272                         print_result(D_CBC_192_AES,j,count,d);
1273                         }
1274                 }
1275         if (doit[D_CBC_256_AES])
1276                 {
1277                 for (j=0; j<SIZE_NUM; j++)
1278                         {
1279                         print_message(names[D_CBC_256_AES],c[D_CBC_256_AES][j],lengths[j]);
1280                         Time_F(START);
1281                         for (count=0,run=1; COND(c[D_CBC_256_AES][j]); count++)
1282                                 AES_cbc_encrypt(buf,buf,
1283                                         (unsigned long)lengths[j],&aes_ks3,
1284                                         iv,AES_ENCRYPT);
1285                         d=Time_F(STOP);
1286                         print_result(D_CBC_256_AES,j,count,d);
1287                         }
1288                 }
1289
1290 #endif
1291 #ifndef OPENSSL_NO_IDEA
1292         if (doit[D_CBC_IDEA])
1293                 {
1294                 for (j=0; j<SIZE_NUM; j++)
1295                         {
1296                         print_message(names[D_CBC_IDEA],c[D_CBC_IDEA][j],lengths[j]);
1297                         Time_F(START);
1298                         for (count=0,run=1; COND(c[D_CBC_IDEA][j]); count++)
1299                                 idea_cbc_encrypt(buf,buf,
1300                                         (unsigned long)lengths[j],&idea_ks,
1301                                         iv,IDEA_ENCRYPT);
1302                         d=Time_F(STOP);
1303                         print_result(D_CBC_IDEA,j,count,d);
1304                         }
1305                 }
1306 #endif
1307 #ifndef OPENSSL_NO_RC2
1308         if (doit[D_CBC_RC2])
1309                 {
1310                 for (j=0; j<SIZE_NUM; j++)
1311                         {
1312                         print_message(names[D_CBC_RC2],c[D_CBC_RC2][j],lengths[j]);
1313                         Time_F(START);
1314                         for (count=0,run=1; COND(c[D_CBC_RC2][j]); count++)
1315                                 RC2_cbc_encrypt(buf,buf,
1316                                         (unsigned long)lengths[j],&rc2_ks,
1317                                         iv,RC2_ENCRYPT);
1318                         d=Time_F(STOP);
1319                         print_result(D_CBC_RC2,j,count,d);
1320                         }
1321                 }
1322 #endif
1323 #ifndef OPENSSL_NO_RC5
1324         if (doit[D_CBC_RC5])
1325                 {
1326                 for (j=0; j<SIZE_NUM; j++)
1327                         {
1328                         print_message(names[D_CBC_RC5],c[D_CBC_RC5][j],lengths[j]);
1329                         Time_F(START);
1330                         for (count=0,run=1; COND(c[D_CBC_RC5][j]); count++)
1331                                 RC5_32_cbc_encrypt(buf,buf,
1332                                         (unsigned long)lengths[j],&rc5_ks,
1333                                         iv,RC5_ENCRYPT);
1334                         d=Time_F(STOP);
1335                         print_result(D_CBC_RC5,j,count,d);
1336                         }
1337                 }
1338 #endif
1339 #ifndef OPENSSL_NO_BF
1340         if (doit[D_CBC_BF])
1341                 {
1342                 for (j=0; j<SIZE_NUM; j++)
1343                         {
1344                         print_message(names[D_CBC_BF],c[D_CBC_BF][j],lengths[j]);
1345                         Time_F(START);
1346                         for (count=0,run=1; COND(c[D_CBC_BF][j]); count++)
1347                                 BF_cbc_encrypt(buf,buf,
1348                                         (unsigned long)lengths[j],&bf_ks,
1349                                         iv,BF_ENCRYPT);
1350                         d=Time_F(STOP);
1351                         print_result(D_CBC_BF,j,count,d);
1352                         }
1353                 }
1354 #endif
1355 #ifndef OPENSSL_NO_CAST
1356         if (doit[D_CBC_CAST])
1357                 {
1358                 for (j=0; j<SIZE_NUM; j++)
1359                         {
1360                         print_message(names[D_CBC_CAST],c[D_CBC_CAST][j],lengths[j]);
1361                         Time_F(START);
1362                         for (count=0,run=1; COND(c[D_CBC_CAST][j]); count++)
1363                                 CAST_cbc_encrypt(buf,buf,
1364                                         (unsigned long)lengths[j],&cast_ks,
1365                                         iv,CAST_ENCRYPT);
1366                         d=Time_F(STOP);
1367                         print_result(D_CBC_CAST,j,count,d);
1368                         }
1369                 }
1370 #endif
1371
1372         if (doit[D_EVP])
1373                 {
1374                 for (j=0; j<SIZE_NUM; j++)
1375                         {
1376                         if (evp_cipher)
1377                                 {
1378                                 EVP_CIPHER_CTX ctx;
1379                                 int outl;
1380
1381                                 names[D_EVP]=OBJ_nid2ln(evp_cipher->nid);
1382                                 /* -O3 -fschedule-insns messes up an
1383                                  * optimization here!  names[D_EVP]
1384                                  * somehow becomes NULL */
1385                                 print_message(names[D_EVP],save_count,
1386                                         lengths[j]);
1387
1388                                 EVP_CIPHER_CTX_init(&ctx);
1389                                 if(decrypt)
1390                                         EVP_DecryptInit_ex(&ctx,evp_cipher,NULL,key16,iv);
1391                                 else
1392                                         EVP_EncryptInit_ex(&ctx,evp_cipher,NULL,key16,iv);
1393
1394                                 Time_F(START);
1395                                 if(decrypt)
1396                                         for (count=0,run=1; COND(save_count*4*lengths[0]/lengths[j]); count++)
1397                                                 EVP_DecryptUpdate(&ctx,buf,&outl,buf,lengths[j]);
1398                                 else
1399                                         for (count=0,run=1; COND(save_count*4*lengths[0]/lengths[j]); count++)
1400                                                 EVP_EncryptUpdate(&ctx,buf,&outl,buf,lengths[j]);
1401                                 if(decrypt)
1402                                         EVP_DecryptFinal_ex(&ctx,buf,&outl);
1403                                 else
1404                                         EVP_EncryptFinal_ex(&ctx,buf,&outl);
1405                                 d=Time_F(STOP);
1406                                 EVP_CIPHER_CTX_cleanup(&ctx);
1407                                 }
1408                         if (evp_md)
1409                                 {
1410                                 names[D_EVP]=OBJ_nid2ln(evp_md->type);
1411                                 print_message(names[D_EVP],save_count,
1412                                         lengths[j]);
1413
1414                                 Time_F(START);
1415                                 for (count=0,run=1; COND(save_count*4*lengths[0]/lengths[j]); count++)
1416                                         EVP_Digest(buf,lengths[j],&(md[0]),NULL,evp_md,NULL);
1417
1418                                 d=Time_F(STOP);
1419                                 }
1420                         print_result(D_EVP,j,count,d);
1421                         }
1422                 }
1423
1424         RAND_pseudo_bytes(buf,36);
1425 #ifndef OPENSSL_NO_RSA
1426         for (j=0; j<RSA_NUM; j++)
1427                 {
1428                 int ret;
1429                 if (!rsa_doit[j]) continue;
1430                 ret=RSA_sign(NID_md5_sha1, buf,36, buf2, &rsa_num, rsa_key[j]);
1431                 if (ret == 0)
1432                         {
1433                         BIO_printf(bio_err,"RSA sign failure.  No RSA sign will be done.\n");
1434                         ERR_print_errors(bio_err);
1435                         rsa_count=1;
1436                         }
1437                 else
1438                         {
1439                         pkey_print_message("private","rsa",
1440                                 rsa_c[j][0],rsa_bits[j],
1441                                 RSA_SECONDS);
1442 /*                      RSA_blinding_on(rsa_key[j],NULL); */
1443                         Time_F(START);
1444                         for (count=0,run=1; COND(rsa_c[j][0]); count++)
1445                                 {
1446                                 ret=RSA_sign(NID_md5_sha1, buf,36, buf2,
1447                                         &rsa_num, rsa_key[j]);
1448                                 if (ret == 0)
1449                                         {
1450                                         BIO_printf(bio_err,
1451                                                 "RSA sign failure\n");
1452                                         ERR_print_errors(bio_err);
1453                                         count=1;
1454                                         break;
1455                                         }
1456                                 }
1457                         d=Time_F(STOP);
1458                         BIO_printf(bio_err,mr ? "+R1:%ld:%d:%.2f\n"
1459                                    : "%ld %d bit private RSA's in %.2fs\n",
1460                                    count,rsa_bits[j],d);
1461                         rsa_results[j][0]=d/(double)count;
1462                         rsa_count=count;
1463                         }
1464
1465 #if 1
1466                 ret=RSA_verify(NID_md5_sha1, buf,36, buf2, rsa_num, rsa_key[j]);
1467                 if (ret <= 0)
1468                         {
1469                         BIO_printf(bio_err,"RSA verify failure.  No RSA verify will be done.\n");
1470                         ERR_print_errors(bio_err);
1471                         rsa_doit[j] = 0;
1472                         }
1473                 else
1474                         {
1475                         pkey_print_message("public","rsa",
1476                                 rsa_c[j][1],rsa_bits[j],
1477                                 RSA_SECONDS);
1478                         Time_F(START);
1479                         for (count=0,run=1; COND(rsa_c[j][1]); count++)
1480                                 {
1481                                 ret=RSA_verify(NID_md5_sha1, buf,36, buf2,
1482                                         rsa_num, rsa_key[j]);
1483                                 if (ret == 0)
1484                                         {
1485                                         BIO_printf(bio_err,
1486                                                 "RSA verify failure\n");
1487                                         ERR_print_errors(bio_err);
1488                                         count=1;
1489                                         break;
1490                                         }
1491                                 }
1492                         d=Time_F(STOP);
1493                         BIO_printf(bio_err,mr ? "+R2:%ld:%d:%.2f\n"
1494                                    : "%ld %d bit public RSA's in %.2fs\n",
1495                                    count,rsa_bits[j],d);
1496                         rsa_results[j][1]=d/(double)count;
1497                         }
1498 #endif
1499
1500                 if (rsa_count <= 1)
1501                         {
1502                         /* if longer than 10s, don't do any more */
1503                         for (j++; j<RSA_NUM; j++)
1504                                 rsa_doit[j]=0;
1505                         }
1506                 }
1507 #endif
1508
1509         RAND_pseudo_bytes(buf,20);
1510 #ifndef OPENSSL_NO_DSA
1511         if (RAND_status() != 1)
1512                 {
1513                 RAND_seed(rnd_seed, sizeof rnd_seed);
1514                 rnd_fake = 1;
1515                 }
1516         for (j=0; j<DSA_NUM; j++)
1517                 {
1518                 unsigned int kk;
1519                 int ret;
1520
1521                 if (!dsa_doit[j]) continue;
1522 /*              DSA_generate_key(dsa_key[j]); */
1523 /*              DSA_sign_setup(dsa_key[j],NULL); */
1524                 ret=DSA_sign(EVP_PKEY_DSA,buf,20,buf2,
1525                         &kk,dsa_key[j]);
1526                 if (ret == 0)
1527                         {
1528                         BIO_printf(bio_err,"DSA sign failure.  No DSA sign will be done.\n");
1529                         ERR_print_errors(bio_err);
1530                         rsa_count=1;
1531                         }
1532                 else
1533                         {
1534                         pkey_print_message("sign","dsa",
1535                                 dsa_c[j][0],dsa_bits[j],
1536                                 DSA_SECONDS);
1537                         Time_F(START);
1538                         for (count=0,run=1; COND(dsa_c[j][0]); count++)
1539                                 {
1540                                 ret=DSA_sign(EVP_PKEY_DSA,buf,20,buf2,
1541                                         &kk,dsa_key[j]);
1542                                 if (ret == 0)
1543                                         {
1544                                         BIO_printf(bio_err,
1545                                                 "DSA sign failure\n");
1546                                         ERR_print_errors(bio_err);
1547                                         count=1;
1548                                         break;
1549                                         }
1550                                 }
1551                         d=Time_F(STOP);
1552                         BIO_printf(bio_err,mr ? "+R3:%ld:%d:%.2f\n"
1553                                    : "%ld %d bit DSA signs in %.2fs\n",
1554                                    count,dsa_bits[j],d);
1555                         dsa_results[j][0]=d/(double)count;
1556                         rsa_count=count;
1557                         }
1558
1559                 ret=DSA_verify(EVP_PKEY_DSA,buf,20,buf2,
1560                         kk,dsa_key[j]);
1561                 if (ret <= 0)
1562                         {
1563                         BIO_printf(bio_err,"DSA verify failure.  No DSA verify will be done.\n");
1564                         ERR_print_errors(bio_err);
1565                         dsa_doit[j] = 0;
1566                         }
1567                 else
1568                         {
1569                         pkey_print_message("verify","dsa",
1570                                 dsa_c[j][1],dsa_bits[j],
1571                                 DSA_SECONDS);
1572                         Time_F(START);
1573                         for (count=0,run=1; COND(dsa_c[j][1]); count++)
1574                                 {
1575                                 ret=DSA_verify(EVP_PKEY_DSA,buf,20,buf2,
1576                                         kk,dsa_key[j]);
1577                                 if (ret <= 0)
1578                                         {
1579                                         BIO_printf(bio_err,
1580                                                 "DSA verify failure\n");
1581                                         ERR_print_errors(bio_err);
1582                                         count=1;
1583                                         break;
1584                                         }
1585                                 }
1586                         d=Time_F(STOP);
1587                         BIO_printf(bio_err,mr ? "+R4:%ld:%d:%.2f\n"
1588                                    : "%ld %d bit DSA verify in %.2fs\n",
1589                                    count,dsa_bits[j],d);
1590                         dsa_results[j][1]=d/(double)count;
1591                         }
1592
1593                 if (rsa_count <= 1)
1594                         {
1595                         /* if longer than 10s, don't do any more */
1596                         for (j++; j<DSA_NUM; j++)
1597                                 dsa_doit[j]=0;
1598                         }
1599                 }
1600         if (rnd_fake) RAND_cleanup();
1601 #endif
1602 #ifdef HAVE_FORK
1603 show_res:
1604 #endif
1605         if(!mr)
1606                 {
1607                 fprintf(stdout,"%s\n",SSLeay_version(SSLEAY_VERSION));
1608         fprintf(stdout,"%s\n",SSLeay_version(SSLEAY_BUILT_ON));
1609                 printf("options:");
1610                 printf("%s ",BN_options());
1611 #ifndef OPENSSL_NO_MD2
1612                 printf("%s ",MD2_options());
1613 #endif
1614 #ifndef OPENSSL_NO_RC4
1615                 printf("%s ",RC4_options());
1616 #endif
1617 #ifndef OPENSSL_NO_DES
1618                 printf("%s ",DES_options());
1619 #endif
1620 #ifndef OPENSSL_NO_AES
1621                 printf("%s ",AES_options());
1622 #endif
1623 #ifndef OPENSSL_NO_IDEA
1624                 printf("%s ",idea_options());
1625 #endif
1626 #ifndef OPENSSL_NO_BF
1627                 printf("%s ",BF_options());
1628 #endif
1629                 fprintf(stdout,"\n%s\n",SSLeay_version(SSLEAY_CFLAGS));
1630                 printf("available timing options: ");
1631 #ifdef TIMES
1632                 printf("TIMES ");
1633 #endif
1634 #ifdef TIMEB
1635                 printf("TIMEB ");
1636 #endif
1637 #ifdef USE_TOD
1638                 printf("USE_TOD ");
1639 #endif
1640 #ifdef HZ
1641 #define as_string(s) (#s)
1642                 printf("HZ=%g", (double)HZ);
1643 # ifdef _SC_CLK_TCK
1644                 printf(" [sysconf value]");
1645 # endif
1646 #endif
1647                 printf("\n");
1648                 printf("timing function used: %s%s%s%s%s%s%s\n",
1649                        (ftime_used ? "ftime" : ""),
1650                        (ftime_used + times_used > 1 ? "," : ""),
1651                        (times_used ? "times" : ""),
1652                        (ftime_used + times_used + gettimeofday_used > 1 ? "," : ""),
1653                        (gettimeofday_used ? "gettimeofday" : ""),
1654                        (ftime_used + times_used + gettimeofday_used + getrusage_used > 1 ? "," : ""),
1655                        (getrusage_used ? "getrusage" : ""));
1656                 }
1657
1658         if (pr_header)
1659                 {
1660                 if(mr)
1661                         fprintf(stdout,"+H");
1662                 else
1663                         {
1664                         fprintf(stdout,"The 'numbers' are in 1000s of bytes per second processed.\n"); 
1665                         fprintf(stdout,"type        ");
1666                         }
1667                 for (j=0;  j<SIZE_NUM; j++)
1668                         fprintf(stdout,mr ? ":%d" : "%7d bytes",lengths[j]);
1669                 fprintf(stdout,"\n");
1670                 }
1671
1672         for (k=0; k<ALGOR_NUM; k++)
1673                 {
1674                 if (!doit[k]) continue;
1675                 if(mr)
1676                         fprintf(stdout,"+F:%d:%s",k,names[k]);
1677                 else
1678                         fprintf(stdout,"%-13s",names[k]);
1679                 for (j=0; j<SIZE_NUM; j++)
1680                         {
1681                         if (results[k][j] > 10000 && !mr)
1682                                 fprintf(stdout," %11.2fk",results[k][j]/1e3);
1683                         else
1684                                 fprintf(stdout,mr ? ":%.2f" : " %11.2f ",results[k][j]);
1685                         }
1686                 fprintf(stdout,"\n");
1687                 }
1688 #ifndef OPENSSL_NO_RSA
1689         j=1;
1690         for (k=0; k<RSA_NUM; k++)
1691                 {
1692                 if (!rsa_doit[k]) continue;
1693                 if (j && !mr)
1694                         {
1695                         printf("%18ssign    verify    sign/s verify/s\n"," ");
1696                         j=0;
1697                         }
1698                 if(mr)
1699                         fprintf(stdout,"+F2:%u:%u:%f:%f\n",
1700                                 k,rsa_bits[k],rsa_results[k][0],
1701                                 rsa_results[k][1]);
1702                 else
1703                         fprintf(stdout,"rsa %4u bits %8.4fs %8.4fs %8.1f %8.1f\n",
1704                                 rsa_bits[k],rsa_results[k][0],rsa_results[k][1],
1705                                 1.0/rsa_results[k][0],1.0/rsa_results[k][1]);
1706                 }
1707 #endif
1708 #ifndef OPENSSL_NO_DSA
1709         j=1;
1710         for (k=0; k<DSA_NUM; k++)
1711                 {
1712                 if (!dsa_doit[k]) continue;
1713                 if (j && !mr)
1714                         {
1715                         printf("%18ssign    verify    sign/s verify/s\n"," ");
1716                         j=0;
1717                         }
1718                 if(mr)
1719                         fprintf(stdout,"+F3:%u:%u:%f:%f\n",
1720                                 k,dsa_bits[k],dsa_results[k][0],dsa_results[k][1]);
1721                 else
1722                         fprintf(stdout,"dsa %4u bits %8.4fs %8.4fs %8.1f %8.1f\n",
1723                                 dsa_bits[k],dsa_results[k][0],dsa_results[k][1],
1724                                 1.0/dsa_results[k][0],1.0/dsa_results[k][1]);
1725                 }
1726 #endif
1727         mret=0;
1728 end:
1729         ERR_print_errors(bio_err);
1730         if (buf != NULL) OPENSSL_free(buf);
1731         if (buf2 != NULL) OPENSSL_free(buf2);
1732 #ifndef OPENSSL_NO_RSA
1733         for (i=0; i<RSA_NUM; i++)
1734                 if (rsa_key[i] != NULL)
1735                         RSA_free(rsa_key[i]);
1736 #endif
1737 #ifndef OPENSSL_NO_DSA
1738         for (i=0; i<DSA_NUM; i++)
1739                 if (dsa_key[i] != NULL)
1740                         DSA_free(dsa_key[i]);
1741 #endif
1742         apps_shutdown();
1743         OPENSSL_EXIT(mret);
1744         }
1745
1746 static void print_message(const char *s, long num, int length)
1747         {
1748 #ifdef SIGALRM
1749         BIO_printf(bio_err,mr ? "+DT:%s:%d:%d\n"
1750                    : "Doing %s for %ds on %d size blocks: ",s,SECONDS,length);
1751         (void)BIO_flush(bio_err);
1752         alarm(SECONDS);
1753 #else
1754         BIO_printf(bio_err,mr ? "+DN:%s:%ld:%d\n"
1755                    : "Doing %s %ld times on %d size blocks: ",s,num,length);
1756         (void)BIO_flush(bio_err);
1757 #endif
1758 #ifdef LINT
1759         num=num;
1760 #endif
1761         }
1762
1763 static void pkey_print_message(char *str, char *str2, long num, int bits,
1764              int tm)
1765         {
1766 #ifdef SIGALRM
1767         BIO_printf(bio_err,mr ? "+DTP:%d:%s:%s:%d\n"
1768                            : "Doing %d bit %s %s's for %ds: ",bits,str,str2,tm);
1769         (void)BIO_flush(bio_err);
1770         alarm(RSA_SECONDS);
1771 #else
1772         BIO_printf(bio_err,mr ? "+DNP:%ld:%d:%s:%s\n"
1773                            : "Doing %ld %d bit %s %s's: ",num,bits,str,str2);
1774         (void)BIO_flush(bio_err);
1775 #endif
1776 #ifdef LINT
1777         num=num;
1778 #endif
1779         }
1780
1781 static void print_result(int alg,int run_no,int count,double time_used)
1782         {
1783         BIO_printf(bio_err,mr ? "+R:%ld:%s:%f\n"
1784                    : "%ld %s's in %.2fs\n",count,names[alg],time_used);
1785         results[alg][run_no]=((double)count)/time_used*lengths[run_no];
1786         }
1787
1788 static char *sstrsep(char **string, const char *delim)
1789     {
1790     char isdelim[256];
1791     char *token = *string;
1792
1793     if (**string == 0)
1794         return NULL;
1795
1796     memset(isdelim, 0, sizeof isdelim);
1797     isdelim[0] = 1;
1798
1799     while (*delim)
1800         {
1801         isdelim[(unsigned char)(*delim)] = 1;
1802         delim++;
1803         }
1804
1805     while (!isdelim[(unsigned char)(**string)])
1806         {
1807         (*string)++;
1808         }
1809
1810     if (**string)
1811         {
1812         **string = 0;
1813         (*string)++;
1814         }
1815
1816     return token;
1817     }
1818
1819 #ifdef HAVE_FORK
1820 static int do_multi(int multi)
1821         {
1822         int n;
1823         int fd[2];
1824         int *fds;
1825         static char sep[]=":";
1826
1827         fds=malloc(multi*sizeof *fds);
1828         for(n=0 ; n < multi ; ++n)
1829                 {
1830                 pipe(fd);
1831                 if(fork())
1832                         {
1833                         close(fd[1]);
1834                         fds[n]=fd[0];
1835                         }
1836                 else
1837                         {
1838                         close(fd[0]);
1839                         close(1);
1840                         dup(fd[1]);
1841                         close(fd[1]);
1842                         mr=1;
1843                         usertime=0;
1844                         return 0;
1845                         }
1846                 printf("Forked child %d\n",n);
1847                 }
1848
1849         /* for now, assume the pipe is long enough to take all the output */
1850         for(n=0 ; n < multi ; ++n)
1851                 {
1852                 FILE *f;
1853                 char buf[1024];
1854                 char *p;
1855
1856                 f=fdopen(fds[n],"r");
1857                 while(fgets(buf,sizeof buf,f))
1858                         {
1859                         p=strchr(buf,'\n');
1860                         if(p)
1861                                 *p='\0';
1862                         if(buf[0] != '+')
1863                                 {
1864                                 fprintf(stderr,"Don't understand line '%s' from child %d\n",
1865                                                 buf,n);
1866                                 continue;
1867                                 }
1868                         printf("Got: %s from %d\n",buf,n);
1869                         if(!strncmp(buf,"+F:",3))
1870                                 {
1871                                 int alg;
1872                                 int j;
1873
1874                                 p=buf+3;
1875                                 alg=atoi(sstrsep(&p,sep));
1876                                 sstrsep(&p,sep);
1877                                 for(j=0 ; j < SIZE_NUM ; ++j)
1878                                         results[alg][j]+=atof(sstrsep(&p,sep));
1879                                 }
1880                         else if(!strncmp(buf,"+F2:",4))
1881                                 {
1882                                 int k;
1883                                 double d;
1884                                 
1885                                 p=buf+4;
1886                                 k=atoi(sstrsep(&p,sep));
1887                                 sstrsep(&p,sep);
1888
1889                                 d=atof(sstrsep(&p,sep));
1890                                 if(n)
1891                                         rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);
1892                                 else
1893                                         rsa_results[k][0]=d;
1894
1895                                 d=atof(sstrsep(&p,sep));
1896                                 if(n)
1897                                         rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);
1898                                 else
1899                                         rsa_results[k][1]=d;
1900                                 }
1901                         else if(!strncmp(buf,"+F2:",4))
1902                                 {
1903                                 int k;
1904                                 double d;
1905                                 
1906                                 p=buf+4;
1907                                 k=atoi(sstrsep(&p,sep));
1908                                 sstrsep(&p,sep);
1909
1910                                 d=atof(sstrsep(&p,sep));
1911                                 if(n)
1912                                         rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);
1913                                 else
1914                                         rsa_results[k][0]=d;
1915
1916                                 d=atof(sstrsep(&p,sep));
1917                                 if(n)
1918                                         rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);
1919                                 else
1920                                         rsa_results[k][1]=d;
1921                                 }
1922                         else if(!strncmp(buf,"+F3:",4))
1923                                 {
1924                                 int k;
1925                                 double d;
1926                                 
1927                                 p=buf+4;
1928                                 k=atoi(sstrsep(&p,sep));
1929                                 sstrsep(&p,sep);
1930
1931                                 d=atof(sstrsep(&p,sep));
1932                                 if(n)
1933                                         dsa_results[k][0]=1/(1/dsa_results[k][0]+1/d);
1934                                 else
1935                                         dsa_results[k][0]=d;
1936
1937                                 d=atof(sstrsep(&p,sep));
1938                                 if(n)
1939                                         dsa_results[k][1]=1/(1/dsa_results[k][1]+1/d);
1940                                 else
1941                                         dsa_results[k][1]=d;
1942                                 }
1943                         else if(!strncmp(buf,"+H:",3))
1944                                 {
1945                                 }
1946                         else
1947                                 fprintf(stderr,"Unknown type '%s' from child %d\n",buf,n);
1948                         }
1949                 }
1950         return 1;
1951         }
1952 #endif
1953 #endif