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