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