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