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