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