speed.c 61.3 KB
Newer Older
1
/* apps/speed.c -*- mode:C; c-file-style: "eay" -*- */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
 * All rights reserved.
 *
 * This package is an SSL implementation written
 * by Eric Young (eay@cryptsoft.com).
 * The implementation was written so as to conform with Netscapes SSL.
 * 
 * This library is free for commercial and non-commercial use as long as
 * the following conditions are aheared to.  The following conditions
 * apply to all code found in this distribution, be it the RC4, RSA,
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
 * included with this distribution is covered by the same copyright terms
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
 * 
 * Copyright remains Eric Young's, and as such any Copyright notices in
 * the code are not to be removed.
 * If this package is used in a product, Eric Young should be given attribution
 * as the author of the parts of the library used.
 * This can be in the form of a textual message at program startup or
 * in documentation (online or textual) provided with the package.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    "This product includes cryptographic software written by
 *     Eric Young (eay@cryptsoft.com)"
 *    The word 'cryptographic' can be left out if the rouines from the library
 *    being used are not cryptographic related :-).
 * 4. If you include any Windows specific code (or a derivative thereof) from 
 *    the apps directory (application code) you must include an acknowledgement:
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
 * 
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * 
 * The licence and distribution terms for any publically available version or
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
 * copied and put under another distribution licence
 * [including the GNU Public Licence.]
 */
B
Bodo Möller 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
/* ====================================================================
 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
 *
 * Portions of the attached software ("Contribution") are developed by 
 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
 *
 * The Contribution is licensed pursuant to the OpenSSL open source
 * license provided above.
 *
 * In addition, Sun covenants to all licensees who provide a reciprocal
 * covenant with respect to their own patents if any, not to sue under
 * current and future patent claims necessarily infringed by the making,
 * using, practicing, selling, offering for sale and/or otherwise
 * disposing of the Contribution as delivered hereunder 
 * (or portions thereof), provided that such covenant shall not apply:
 *  1) for code that a licensee deletes from the Contribution;
 *  2) separates from the Contribution; or
 *  3) for infringements caused by:
 *       i) the modification of the Contribution or
 *      ii) the combination of the Contribution with other software or
 *          devices where such combination causes the infringement.
 *
 * The ECDH and ECDSA speed test software is originally written by 
 * Sumit Gupta of Sun Microsystems Laboratories.
 *
 */
84 85 86 87 88

/* most of this code has been pilfered from my libdes speed.c program */

#undef SECONDS
#define SECONDS		3	
89 90
#define RSA_SECONDS	10
#define DSA_SECONDS	10
B
Bodo Möller 已提交
91 92
#define ECDSA_SECONDS   10
#define ECDH_SECONDS    10
93 94 95 96 97 98 99 100 101 102 103

/* 11-Sep-92 Andrew Daviel   Support for Silicon Graphics IRIX added */
/* 06-Apr-92 Luke Brennan    Support for VMS and add extra signal calls */

#undef PROG
#define PROG speed_main

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
104
#include <math.h>
105
#include "apps.h"
106
#ifdef OPENSSL_NO_STDIO
107 108
#define APPS_WIN16
#endif
109 110 111
#include <openssl/crypto.h>
#include <openssl/rand.h>
#include <openssl/err.h>
112 113
#include <openssl/evp.h>
#include <openssl/objects.h>
114
#if !defined(OPENSSL_SYS_MSDOS)
115
#include OPENSSL_UNISTD
116
#endif
117

118
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(OPENSSL_SYS_MACOSX)
B
Ben Laurie 已提交
119
# define USE_TOD
120
#elif !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VXWORKS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC))
B
Ben Laurie 已提交
121
# define TIMES
122
#endif
123
#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 */
124 125
# define TIMEB
#endif
126 127

#ifndef _IRIX
B
Ben Laurie 已提交
128
# include <time.h>
129 130
#endif
#ifdef TIMES
B
Ben Laurie 已提交
131 132 133 134 135 136
# include <sys/types.h>
# include <sys/times.h>
#endif
#ifdef USE_TOD
# include <sys/time.h>
# include <sys/resource.h>
137
#endif
U
Ulf Möller 已提交
138 139 140 141 142

/* Depending on the VMS version, the tms structure is perhaps defined.
   The __TMS macro will show if it was.  If it wasn't defined, we should
   undefine TIMES, since that tells the rest of the program how things
   should be handled.				-- Richard Levitte */
143
#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
U
Ulf Möller 已提交
144
#undef TIMES
145
#endif
U
Ulf Möller 已提交
146

147
#ifdef TIMEB
148
#include <sys/timeb.h>
149 150
#endif

151
#if !defined(TIMES) && !defined(TIMEB) && !defined(USE_TOD) && !defined(OPENSSL_SYS_VXWORKS)
152 153
#error "It seems neither struct tms nor struct timeb is supported in this platform!"
#endif
154

155 156
#if defined(sun) || defined(__ultrix)
#define _POSIX_SOURCE
157 158 159 160
#include <limits.h>
#include <sys/param.h>
#endif

161
#ifndef OPENSSL_NO_DES
162
#include <openssl/des.h>
163
#endif
164 165 166
#ifndef OPENSSL_NO_AES
#include <openssl/aes.h>
#endif
167
#ifndef OPENSSL_NO_MD2
168
#include <openssl/md2.h>
169
#endif
170
#ifndef OPENSSL_NO_MDC2
171
#include <openssl/mdc2.h>
172
#endif
173
#ifndef OPENSSL_NO_MD4
174 175
#include <openssl/md4.h>
#endif
176
#ifndef OPENSSL_NO_MD5
177
#include <openssl/md5.h>
U
Ulf Möller 已提交
178
#endif
179
#ifndef OPENSSL_NO_HMAC
180
#include <openssl/hmac.h>
181
#endif
U
Ulf Möller 已提交
182
#include <openssl/evp.h>
183
#ifndef OPENSSL_NO_SHA
184
#include <openssl/sha.h>
185
#endif
186
#ifndef OPENSSL_NO_RIPEMD
187
#include <openssl/ripemd.h>
188
#endif
189
#ifndef OPENSSL_NO_RC4
190
#include <openssl/rc4.h>
191
#endif
192
#ifndef OPENSSL_NO_RC5
193
#include <openssl/rc5.h>
194
#endif
195
#ifndef OPENSSL_NO_RC2
196
#include <openssl/rc2.h>
197
#endif
198
#ifndef OPENSSL_NO_IDEA
199
#include <openssl/idea.h>
200
#endif
201
#ifndef OPENSSL_NO_BF
202
#include <openssl/blowfish.h>
203
#endif
204
#ifndef OPENSSL_NO_CAST
205
#include <openssl/cast.h>
206
#endif
207
#ifndef OPENSSL_NO_RSA
208
#include <openssl/rsa.h>
209
#include "./testrsa.h"
210
#endif
211
#include <openssl/x509.h>
212
#ifndef OPENSSL_NO_DSA
213 214
#include "./testdsa.h"
#endif
B
Bodo Möller 已提交
215 216 217 218 219 220
#ifndef OPENSSL_NO_ECDSA
#include <openssl/ecdsa.h>
#endif
#ifndef OPENSSL_NO_ECDH
#include <openssl/ecdh.h>
#endif
221 222 223

/* The following if from times(3) man page.  It may need to be changed */
#ifndef HZ
224 225 226 227 228 229 230 231 232 233
# ifdef _SC_CLK_TCK
#  define HZ ((double)sysconf(_SC_CLK_TCK))
# else
#  ifndef CLK_TCK
#   ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
#    define HZ	100.0
#   else /* _BSD_CLK_TCK_ */
#    define HZ ((double)_BSD_CLK_TCK_)
#   endif
#  else /* CLK_TCK */
234 235
#   define HZ ((double)CLK_TCK)
#  endif
236 237 238
# endif
#endif

239 240 241 242
#if !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(OPENSSL_SYS_OS2)
# define HAVE_FORK 1
#endif

243
#undef BUFSIZE
244
#define BUFSIZE	((long)1024*8+1)
245 246
int run=0;

247
static char ftime_used = 0, times_used = 0, gettimeofday_used = 0, getrusage_used = 0;
248 249
static int mr=0;
static int usertime=1;
250

251
static double Time_F(int s);
252
static void print_message(const char *s,long num,int length);
253
static void pkey_print_message(char *str,char *str2,long num,int bits,int sec);
254
static void print_result(int alg,int run_no,int count,double time_used);
255
#ifdef HAVE_FORK
256
static int do_multi(int multi);
257
#endif
258

259
#define ALGOR_NUM	19
260 261 262
#define SIZE_NUM	5
#define RSA_NUM		4
#define DSA_NUM		3
B
Bodo Möller 已提交
263

B
Bodo Möller 已提交
264
#define EC_NUM       16
B
Bodo Möller 已提交
265 266
#define MAX_ECDH_SIZE 256

267 268 269
static const char *names[ALGOR_NUM]={
  "md2","mdc2","md4","md5","hmac(md5)","sha1","rmd160","rc4",
  "des cbc","des ede3","idea cbc",
270 271
  "rc2 cbc","rc5-32/12 cbc","blowfish cbc","cast cbc",
  "aes-128 cbc","aes-192 cbc","aes-256 cbc"};
272
static double results[ALGOR_NUM][SIZE_NUM];
273
static int lengths[SIZE_NUM]={16,64,256,1024,8*1024};
274 275
static double rsa_results[RSA_NUM][2];
static double dsa_results[DSA_NUM][2];
B
Bodo Möller 已提交
276 277 278
static double ecdsa_results[EC_NUM][2];
static double ecdh_results[EC_NUM][1];

279

D
Dr. Stephen Henson 已提交
280 281 282 283 284 285 286
#ifdef SIGALRM
#if defined(__STDC__) || defined(sgi) || defined(_AIX)
#define SIGRETTYPE void
#else
#define SIGRETTYPE int
#endif 

287
static SIGRETTYPE sig_done(int sig);
U
Ulf Möller 已提交
288
static SIGRETTYPE sig_done(int sig)
289 290 291 292 293 294 295 296 297 298 299 300
	{
	signal(SIGALRM,sig_done);
	run=0;
#ifdef LINT
	sig=sig;
#endif
	}
#endif

#define START	0
#define STOP	1

301
static double Time_F(int s)
302 303
	{
	double ret;
B
Ben Laurie 已提交
304 305 306 307 308 309

#ifdef USE_TOD
	if(usertime)
	    {
		static struct rusage tstart,tend;

310
		getrusage_used = 1;
B
Ben Laurie 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
		if (s == START)
			{
			getrusage(RUSAGE_SELF,&tstart);
			return(0);
			}
		else
			{
			long i;

			getrusage(RUSAGE_SELF,&tend);
			i=(long)tend.ru_utime.tv_usec-(long)tstart.ru_utime.tv_usec;
			ret=((double)(tend.ru_utime.tv_sec-tstart.ru_utime.tv_sec))
			  +((double)i)/1000000.0;
			return((ret < 0.001)?0.001:ret);
			}
		}
	else
		{
		static struct timeval tstart,tend;
		long i;

332
		gettimeofday_used = 1;
B
Ben Laurie 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
		if (s == START)
			{
			gettimeofday(&tstart,NULL);
			return(0);
			}
		else
			{
			gettimeofday(&tend,NULL);
			i=(long)tend.tv_usec-(long)tstart.tv_usec;
			ret=((double)(tend.tv_sec-tstart.tv_sec))+((double)i)/1000000.0;
			return((ret < 0.001)?0.001:ret);
			}
		}
#else  /* ndef USE_TOD */
		
# ifdef TIMES
349
	if (usertime)
350
		{
351
		static struct tms tstart,tend;
352

353
		times_used = 1;
354 355 356 357 358 359 360 361 362 363 364
		if (s == START)
			{
			times(&tstart);
			return(0);
			}
		else
			{
			times(&tend);
			ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
			return((ret < 1e-3)?1e-3:ret);
			}
365
		}
B
Ben Laurie 已提交
366
# endif /* times() */
367 368 369
# if defined(TIMES) && defined(TIMEB)
	else
# endif
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
# ifdef OPENSSL_SYS_VXWORKS
                {
		static unsigned long tick_start, tick_end;

		if( s == START )
			{
			tick_start = tickGet();
			return 0;
			}
		else
			{
			tick_end = tickGet();
			ret = (double)(tick_end - tick_start) / (double)sysClkRateGet();
			return((ret < 0.001)?0.001:ret);
			}
                }
# elif defined(TIMEB)
387
		{
388 389 390
		static struct timeb tstart,tend;
		long i;

391
		ftime_used = 1;
392 393 394 395 396 397 398 399 400 401 402 403
		if (s == START)
			{
			ftime(&tstart);
			return(0);
			}
		else
			{
			ftime(&tend);
			i=(long)tend.millitm-(long)tstart.millitm;
			ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
			return((ret < 0.001)?0.001:ret);
			}
404
		}
405
# endif
B
Ben Laurie 已提交
406
#endif
407 408
	}

409 410
int MAIN(int, char **);

U
Ulf Möller 已提交
411
int MAIN(int argc, char **argv)
412
	{
413
	ENGINE *e = NULL;
414
	unsigned char *buf=NULL,*buf2=NULL;
B
Ben Laurie 已提交
415
	int mret=1;
B
Bodo Möller 已提交
416
	long count=0,save_count=0;
B
Ben Laurie 已提交
417
	int i,j,k;
B
Bodo Möller 已提交
418 419 420
#if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA)
	long rsa_count;
#endif
421
#ifndef OPENSSL_NO_RSA
422
	unsigned rsa_num;
U
Ulf Möller 已提交
423
#endif
424
	unsigned char md[EVP_MAX_MD_SIZE];
425
#ifndef OPENSSL_NO_MD2
426 427
	unsigned char md2[MD2_DIGEST_LENGTH];
#endif
428
#ifndef OPENSSL_NO_MDC2
429 430
	unsigned char mdc2[MDC2_DIGEST_LENGTH];
#endif
431
#ifndef OPENSSL_NO_MD4
432 433
	unsigned char md4[MD4_DIGEST_LENGTH];
#endif
434
#ifndef OPENSSL_NO_MD5
435
	unsigned char md5[MD5_DIGEST_LENGTH];
436
	unsigned char hmac[MD5_DIGEST_LENGTH];
437
#endif
438
#ifndef OPENSSL_NO_SHA
439 440
	unsigned char sha[SHA_DIGEST_LENGTH];
#endif
441
#ifndef OPENSSL_NO_RIPEMD
442 443
	unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
#endif
444
#ifndef OPENSSL_NO_RC4
445 446
	RC4_KEY rc4_ks;
#endif
447
#ifndef OPENSSL_NO_RC5
448 449
	RC5_32_KEY rc5_ks;
#endif
450
#ifndef OPENSSL_NO_RC2
451 452
	RC2_KEY rc2_ks;
#endif
453
#ifndef OPENSSL_NO_IDEA
454 455
	IDEA_KEY_SCHEDULE idea_ks;
#endif
456
#ifndef OPENSSL_NO_BF
457
	BF_KEY bf_ks;
458
#endif
459
#ifndef OPENSSL_NO_CAST
460
	CAST_KEY cast_ks;
461
#endif
462
	static const unsigned char key16[16]=
463 464
		{0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
		 0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
	static const unsigned char key24[24]=
		{0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
		 0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,
		 0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
	static const unsigned char key32[32]=
		{0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
		 0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,
		 0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34,
		 0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34,0x56};
#ifndef OPENSSL_NO_AES
#define MAX_BLOCK_SIZE 128
#else
#define MAX_BLOCK_SIZE 64
#endif
	unsigned char DES_iv[8];
	unsigned char iv[MAX_BLOCK_SIZE/8];
481
#ifndef OPENSSL_NO_DES
482
	DES_cblock *buf_as_des_cblock = NULL;
483 484 485
	static DES_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
	static DES_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
	static DES_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
486 487 488
	DES_key_schedule sch;
	DES_key_schedule sch2;
	DES_key_schedule sch3;
489
#endif
490 491 492
#ifndef OPENSSL_NO_AES
	AES_KEY aes_ks1, aes_ks2, aes_ks3;
#endif
493 494
#define	D_MD2		0
#define	D_MDC2		1
495 496 497 498 499 500 501 502 503 504 505 506 507
#define	D_MD4		2
#define	D_MD5		3
#define	D_HMAC		4
#define	D_SHA1		5
#define D_RMD160	6
#define	D_RC4		7
#define	D_CBC_DES	8
#define	D_EDE3_DES	9
#define	D_CBC_IDEA	10
#define	D_CBC_RC2	11
#define	D_CBC_RC5	12
#define	D_CBC_BF	13
#define	D_CBC_CAST	14
508 509 510 511 512
#define D_CBC_128_AES	15
#define D_CBC_192_AES	16
#define D_CBC_256_AES	17
#define D_EVP		18
	double d=0.0;
513 514 515 516 517 518 519 520
	long c[ALGOR_NUM][SIZE_NUM];
#define	R_DSA_512	0
#define	R_DSA_1024	1
#define	R_DSA_2048	2
#define	R_RSA_512	0
#define	R_RSA_1024	1
#define	R_RSA_2048	2
#define	R_RSA_4096	3
B
Bodo Möller 已提交
521 522

#define R_EC_P160    0
B
Bodo Möller 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
#define R_EC_P192    1	
#define R_EC_P224    2
#define R_EC_P256    3
#define R_EC_P384    4
#define R_EC_P521    5
#define R_EC_K163    6
#define R_EC_K233    7
#define R_EC_K283    8
#define R_EC_K409    9
#define R_EC_K571    10
#define R_EC_B163    11
#define R_EC_B233    12
#define R_EC_B283    13
#define R_EC_B409    14
#define R_EC_B571    15
B
Bodo Möller 已提交
538

539
#ifndef OPENSSL_NO_RSA
540 541 542 543 544 545 546 547
	RSA *rsa_key[RSA_NUM];
	long rsa_c[RSA_NUM][2];
	static unsigned int rsa_bits[RSA_NUM]={512,1024,2048,4096};
	static unsigned char *rsa_data[RSA_NUM]=
		{test512,test1024,test2048,test4096};
	static int rsa_data_length[RSA_NUM]={
		sizeof(test512),sizeof(test1024),
		sizeof(test2048),sizeof(test4096)};
548
#endif
549
#ifndef OPENSSL_NO_DSA
550 551 552 553
	DSA *dsa_key[DSA_NUM];
	long dsa_c[DSA_NUM][2];
	static unsigned int dsa_bits[DSA_NUM]={512,1024,2048};
#endif
B
Bodo Möller 已提交
554 555 556 557 558 559 560 561 562
#ifndef OPENSSL_NO_EC
	/* We only test over the following curves as they are representative, 
	 * To add tests over more curves, simply add the curve NID
	 * and curve name to the following arrays and increase the 
	 * EC_NUM value accordingly. 
	 */
	static unsigned int test_curves[EC_NUM] = 
	{	
	/* Prime Curves */
563
	NID_secp160r1,
B
Bodo Möller 已提交
564
	NID_X9_62_prime192v1,
565 566 567 568
	NID_secp224r1,
	NID_X9_62_prime256v1,
	NID_secp384r1,
	NID_secp521r1,
B
Bodo Möller 已提交
569
	/* Binary Curves */
570 571 572 573 574 575 576 577 578 579
	NID_sect163k1,
	NID_sect233k1,
	NID_sect283k1,
	NID_sect409k1,
	NID_sect571k1,
	NID_sect163r2,
	NID_sect233r1,
	NID_sect283r1,
	NID_sect409r1,
	NID_sect571r1
B
Bodo Möller 已提交
580 581 582 583 584
	}; 
	static char * test_curves_names[EC_NUM] = 
	{
	/* Prime Curves */
	"secp160r1",
B
Bodo Möller 已提交
585
	"nistp192",
B
Bodo Möller 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	"nistp224",
	"nistp256",
	"nistp384",
	"nistp521",
	/* Binary Curves */
	"nistk163",
	"nistk233",
	"nistk283",
	"nistk409",
	"nistk571",
	"nistb163",
	"nistb233",
	"nistb283",
	"nistb409",
	"nistb571"
	};
	static int test_curves_bits[EC_NUM] =
        {
B
Bodo Möller 已提交
604
        160, 192, 224, 256, 384, 521,
B
Bodo Möller 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
        163, 233, 283, 409, 571,
        163, 233, 283, 409, 571
        };

#endif

#ifndef OPENSSL_NO_ECDSA
        unsigned char ecdsasig[256];
        unsigned int ecdsasiglen;
        EC_KEY *ecdsa[EC_NUM];
        long ecdsa_c[EC_NUM][2];
#endif

#ifndef OPENSSL_NO_ECDH
        EC_KEY *ecdh_a[EC_NUM], *ecdh_b[EC_NUM];
        unsigned char secret_a[MAX_ECDH_SIZE], secret_b[MAX_ECDH_SIZE];
        int secret_size_a, secret_size_b;
B
Bodo Möller 已提交
622
        int ecdh_checks = 0;
B
Bodo Möller 已提交
623 624 625 626
        int secret_idx = 0;
        long ecdh_c[EC_NUM][2];
#endif

627 628
	int rsa_doit[RSA_NUM];
	int dsa_doit[DSA_NUM];
B
Bodo Möller 已提交
629 630
	int ecdsa_doit[EC_NUM];
        int ecdh_doit[EC_NUM];
631
	int doit[ALGOR_NUM];
632
	int pr_header=0;
633 634
	const EVP_CIPHER *evp_cipher=NULL;
	const EVP_MD *evp_md=NULL;
B
Ben Laurie 已提交
635
	int decrypt=0;
636
#ifdef HAVE_FORK
637
	int multi=0;
638
#endif
639 640 641 642

#ifndef TIMES
	usertime=-1;
#endif
643 644

	apps_startup();
645
	memset(results, 0, sizeof(results));
646
#ifndef OPENSSL_NO_DSA
647 648
	memset(dsa_key,0,sizeof(dsa_key));
#endif
B
Bodo Möller 已提交
649 650 651 652 653 654 655 656 657 658 659
#ifndef OPENSSL_NO_ECDSA
	for (i=0; i<EC_NUM; i++) ecdsa[i] = NULL;
#endif
#ifndef OPENSSL_NO_ECDH
	for (i=0; i<EC_NUM; i++)
		{
		ecdh_a[i] = NULL;
		ecdh_b[i] = NULL;
		}
#endif

660 661 662

	if (bio_err == NULL)
		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
663
			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
664

D
Dr. Stephen Henson 已提交
665 666 667
	if (!load_config(bio_err, NULL))
		goto end;

668
#ifndef OPENSSL_NO_RSA
669
	memset(rsa_key,0,sizeof(rsa_key));
670 671
	for (i=0; i<RSA_NUM; i++)
		rsa_key[i]=NULL;
672
#endif
673

674
	if ((buf=(unsigned char *)OPENSSL_malloc((int)BUFSIZE)) == NULL)
675 676 677 678
		{
		BIO_printf(bio_err,"out of memory\n");
		goto end;
		}
679
#ifndef OPENSSL_NO_DES
680
	buf_as_des_cblock = (DES_cblock *)buf;
U
Ulf Möller 已提交
681
#endif
682
	if ((buf2=(unsigned char *)OPENSSL_malloc((int)BUFSIZE)) == NULL)
683 684 685 686 687 688
		{
		BIO_printf(bio_err,"out of memory\n");
		goto end;
		}

	memset(c,0,sizeof(c));
689
	memset(DES_iv,0,sizeof(DES_iv));
690 691 692 693 694 695 696 697
	memset(iv,0,sizeof(iv));

	for (i=0; i<ALGOR_NUM; i++)
		doit[i]=0;
	for (i=0; i<RSA_NUM; i++)
		rsa_doit[i]=0;
	for (i=0; i<DSA_NUM; i++)
		dsa_doit[i]=0;
B
Bodo Möller 已提交
698 699 700 701 702 703 704 705 706
#ifndef OPENSSL_NO_ECDSA
	for (i=0; i<EC_NUM; i++)
		ecdsa_doit[i]=0;
#endif
#ifndef OPENSSL_NO_ECDH
	for (i=0; i<EC_NUM; i++)
		ecdh_doit[i]=0;
#endif

707 708 709 710 711 712
	
	j=0;
	argc--;
	argv++;
	while (argc)
		{
713
		if	((argc > 0) && (strcmp(*argv,"-elapsed") == 0))
714
			{
715
			usertime = 0;
716 717 718
			j--;	/* Otherwise, -elapsed gets confused with
				   an algorithm. */
			}
719 720 721 722 723 724 725 726 727
		else if	((argc > 0) && (strcmp(*argv,"-evp") == 0))
			{
			argc--;
			argv++;
			if(argc == 0)
				{
				BIO_printf(bio_err,"no EVP given\n");
				goto end;
				}
728 729
			evp_cipher=EVP_get_cipherbyname(*argv);
			if(!evp_cipher)
730
				{
731 732 733 734 735
				evp_md=EVP_get_digestbyname(*argv);
				}
			if(!evp_cipher && !evp_md)
				{
				BIO_printf(bio_err,"%s is an unknown cipher or digest\n",*argv);
736 737 738 739
				goto end;
				}
			doit[D_EVP]=1;
			}
740 741
		else if (argc > 0 && !strcmp(*argv,"-decrypt"))
			{
B
Ben Laurie 已提交
742
			decrypt=1;
743 744 745 746
			j--;	/* Otherwise, -elapsed gets confused with
				   an algorithm. */
			}
		else if	((argc > 0) && (strcmp(*argv,"-engine") == 0))
747 748 749 750 751 752 753 754
			{
			argc--;
			argv++;
			if(argc == 0)
				{
				BIO_printf(bio_err,"no engine given\n");
				goto end;
				}
755 756
                        e = setup_engine(bio_err, *argv, 0);
			/* j will be increased again further down.  We just
757 758 759 760 761
			   don't want speed to confuse an engine with an
			   algorithm, especially when none is given (which
			   means all of them should be run) */
			j--;
			}
762
#ifdef HAVE_FORK
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
		else if	((argc > 0) && (strcmp(*argv,"-multi") == 0))
			{
			argc--;
			argv++;
			if(argc == 0)
				{
				BIO_printf(bio_err,"no multi count given\n");
				goto end;
				}
			multi=atoi(argv[0]);
			if(multi <= 0)
			    {
				BIO_printf(bio_err,"bad multi count\n");
				goto end;
				}				
778 779
			j--;	/* Otherwise, -mr gets confused with
				   an algorithm. */
780
			}
781
#endif
782 783 784 785 786 787
		else if (argc > 0 && !strcmp(*argv,"-mr"))
			{
			mr=1;
			j--;	/* Otherwise, -mr gets confused with
				   an algorithm. */
			}
788
		else
789
#ifndef OPENSSL_NO_MD2
790 791 792
		if	(strcmp(*argv,"md2") == 0) doit[D_MD2]=1;
		else
#endif
793
#ifndef OPENSSL_NO_MDC2
794 795 796
			if (strcmp(*argv,"mdc2") == 0) doit[D_MDC2]=1;
		else
#endif
797
#ifndef OPENSSL_NO_MD4
798 799 800
			if (strcmp(*argv,"md4") == 0) doit[D_MD4]=1;
		else
#endif
801
#ifndef OPENSSL_NO_MD5
802 803 804
			if (strcmp(*argv,"md5") == 0) doit[D_MD5]=1;
		else
#endif
805
#ifndef OPENSSL_NO_MD5
806
			if (strcmp(*argv,"hmac") == 0) doit[D_HMAC]=1;
807 808
		else
#endif
809
#ifndef OPENSSL_NO_SHA
810 811
			if (strcmp(*argv,"sha1") == 0) doit[D_SHA1]=1;
		else
812 813 814
			if (strcmp(*argv,"sha") == 0) doit[D_SHA1]=1;
		else
#endif
815
#ifndef OPENSSL_NO_RIPEMD
816 817 818 819 820 821
			if (strcmp(*argv,"ripemd") == 0) doit[D_RMD160]=1;
		else
			if (strcmp(*argv,"rmd160") == 0) doit[D_RMD160]=1;
		else
			if (strcmp(*argv,"ripemd160") == 0) doit[D_RMD160]=1;
		else
822
#endif
823
#ifndef OPENSSL_NO_RC4
824 825 826
			if (strcmp(*argv,"rc4") == 0) doit[D_RC4]=1;
		else 
#endif
827
#ifndef OPENSSL_NO_DES
828 829 830 831
			if (strcmp(*argv,"des-cbc") == 0) doit[D_CBC_DES]=1;
		else	if (strcmp(*argv,"des-ede3") == 0) doit[D_EDE3_DES]=1;
		else
#endif
832 833 834 835 836 837
#ifndef OPENSSL_NO_AES
			if (strcmp(*argv,"aes-128-cbc") == 0) doit[D_CBC_128_AES]=1;
		else	if (strcmp(*argv,"aes-192-cbc") == 0) doit[D_CBC_192_AES]=1;
		else	if (strcmp(*argv,"aes-256-cbc") == 0) doit[D_CBC_256_AES]=1;
		else
#endif
838
#ifndef OPENSSL_NO_RSA
839
#if 0 /* was: #ifdef RSAref */
840 841
			if (strcmp(*argv,"rsaref") == 0) 
			{
842
			RSA_set_default_openssl_method(RSA_PKCS1_RSAref());
843 844 845 846
			j--;
			}
		else
#endif
847
#ifndef RSA_NULL
848
			if (strcmp(*argv,"openssl") == 0) 
849
			{
850
			RSA_set_default_method(RSA_PKCS1_SSLeay());
851 852 853
			j--;
			}
		else
854
#endif
855
#endif /* !OPENSSL_NO_RSA */
856 857 858 859 860 861 862 863
		     if (strcmp(*argv,"dsa512") == 0) dsa_doit[R_DSA_512]=2;
		else if (strcmp(*argv,"dsa1024") == 0) dsa_doit[R_DSA_1024]=2;
		else if (strcmp(*argv,"dsa2048") == 0) dsa_doit[R_DSA_2048]=2;
		else if (strcmp(*argv,"rsa512") == 0) rsa_doit[R_RSA_512]=2;
		else if (strcmp(*argv,"rsa1024") == 0) rsa_doit[R_RSA_1024]=2;
		else if (strcmp(*argv,"rsa2048") == 0) rsa_doit[R_RSA_2048]=2;
		else if (strcmp(*argv,"rsa4096") == 0) rsa_doit[R_RSA_4096]=2;
		else
864
#ifndef OPENSSL_NO_RC2
865 866 867 868
		     if (strcmp(*argv,"rc2-cbc") == 0) doit[D_CBC_RC2]=1;
		else if (strcmp(*argv,"rc2") == 0) doit[D_CBC_RC2]=1;
		else
#endif
869
#ifndef OPENSSL_NO_RC5
870 871 872 873
		     if (strcmp(*argv,"rc5-cbc") == 0) doit[D_CBC_RC5]=1;
		else if (strcmp(*argv,"rc5") == 0) doit[D_CBC_RC5]=1;
		else
#endif
874
#ifndef OPENSSL_NO_IDEA
875 876 877 878
		     if (strcmp(*argv,"idea-cbc") == 0) doit[D_CBC_IDEA]=1;
		else if (strcmp(*argv,"idea") == 0) doit[D_CBC_IDEA]=1;
		else
#endif
879
#ifndef OPENSSL_NO_BF
880 881
		     if (strcmp(*argv,"bf-cbc") == 0) doit[D_CBC_BF]=1;
		else if (strcmp(*argv,"blowfish") == 0) doit[D_CBC_BF]=1;
882 883 884
		else if (strcmp(*argv,"bf") == 0) doit[D_CBC_BF]=1;
		else
#endif
885
#ifndef OPENSSL_NO_CAST
886 887 888
		     if (strcmp(*argv,"cast-cbc") == 0) doit[D_CBC_CAST]=1;
		else if (strcmp(*argv,"cast") == 0) doit[D_CBC_CAST]=1;
		else if (strcmp(*argv,"cast5") == 0) doit[D_CBC_CAST]=1;
889 890
		else
#endif
891
#ifndef OPENSSL_NO_DES
892 893 894 895 896 897 898
			if (strcmp(*argv,"des") == 0)
			{
			doit[D_CBC_DES]=1;
			doit[D_EDE3_DES]=1;
			}
		else
#endif
899 900 901 902 903 904 905 906 907
#ifndef OPENSSL_NO_AES
			if (strcmp(*argv,"aes") == 0)
			{
			doit[D_CBC_128_AES]=1;
			doit[D_CBC_192_AES]=1;
			doit[D_CBC_256_AES]=1;
			}
		else
#endif
908
#ifndef OPENSSL_NO_RSA
909 910 911 912 913 914 915 916 917
			if (strcmp(*argv,"rsa") == 0)
			{
			rsa_doit[R_RSA_512]=1;
			rsa_doit[R_RSA_1024]=1;
			rsa_doit[R_RSA_2048]=1;
			rsa_doit[R_RSA_4096]=1;
			}
		else
#endif
918
#ifndef OPENSSL_NO_DSA
919 920 921 922 923 924
			if (strcmp(*argv,"dsa") == 0)
			{
			dsa_doit[R_DSA_512]=1;
			dsa_doit[R_DSA_1024]=1;
			}
		else
B
Bodo Möller 已提交
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
#endif
#ifndef OPENSSL_NO_ECDSA
		     if (strcmp(*argv,"ecdsap160") == 0) ecdsa_doit[R_EC_P160]=2;
		else if (strcmp(*argv,"ecdsap224") == 0) ecdsa_doit[R_EC_P224]=2;
		else if (strcmp(*argv,"ecdsap256") == 0) ecdsa_doit[R_EC_P256]=2;
		else if (strcmp(*argv,"ecdsap384") == 0) ecdsa_doit[R_EC_P384]=2;
		else if (strcmp(*argv,"ecdsap521") == 0) ecdsa_doit[R_EC_P521]=2;
		else if (strcmp(*argv,"ecdsak163") == 0) ecdsa_doit[R_EC_K163]=2;
		else if (strcmp(*argv,"ecdsak233") == 0) ecdsa_doit[R_EC_K233]=2;
		else if (strcmp(*argv,"ecdsak283") == 0) ecdsa_doit[R_EC_K283]=2;
		else if (strcmp(*argv,"ecdsak409") == 0) ecdsa_doit[R_EC_K409]=2;
		else if (strcmp(*argv,"ecdsak571") == 0) ecdsa_doit[R_EC_K571]=2;
		else if (strcmp(*argv,"ecdsab163") == 0) ecdsa_doit[R_EC_B163]=2;
		else if (strcmp(*argv,"ecdsab233") == 0) ecdsa_doit[R_EC_B233]=2;
		else if (strcmp(*argv,"ecdsab283") == 0) ecdsa_doit[R_EC_B283]=2;
		else if (strcmp(*argv,"ecdsab409") == 0) ecdsa_doit[R_EC_B409]=2;
		else if (strcmp(*argv,"ecdsab571") == 0) ecdsa_doit[R_EC_B571]=2;
		else if (strcmp(*argv,"ecdsa") == 0)
			{
			for (i=0; i < EC_NUM; i++)
				ecdsa_doit[i]=1;
			}
		else
#endif
#ifndef OPENSSL_NO_ECDH
		     if (strcmp(*argv,"ecdhp160") == 0) ecdh_doit[R_EC_P160]=2;
		else if (strcmp(*argv,"ecdhp224") == 0) ecdh_doit[R_EC_P224]=2;
		else if (strcmp(*argv,"ecdhp256") == 0) ecdh_doit[R_EC_P256]=2;
		else if (strcmp(*argv,"ecdhp384") == 0) ecdh_doit[R_EC_P384]=2;
		else if (strcmp(*argv,"ecdhp521") == 0) ecdh_doit[R_EC_P521]=2;
		else if (strcmp(*argv,"ecdhk163") == 0) ecdh_doit[R_EC_K163]=2;
		else if (strcmp(*argv,"ecdhk233") == 0) ecdh_doit[R_EC_K233]=2;
		else if (strcmp(*argv,"ecdhk283") == 0) ecdh_doit[R_EC_K283]=2;
		else if (strcmp(*argv,"ecdhk409") == 0) ecdh_doit[R_EC_K409]=2;
		else if (strcmp(*argv,"ecdhk571") == 0) ecdh_doit[R_EC_K571]=2;
		else if (strcmp(*argv,"ecdhb163") == 0) ecdh_doit[R_EC_B163]=2;
		else if (strcmp(*argv,"ecdhb233") == 0) ecdh_doit[R_EC_B233]=2;
		else if (strcmp(*argv,"ecdhb283") == 0) ecdh_doit[R_EC_B283]=2;
		else if (strcmp(*argv,"ecdhb409") == 0) ecdh_doit[R_EC_B409]=2;
		else if (strcmp(*argv,"ecdhb571") == 0) ecdh_doit[R_EC_B571]=2;
		else if (strcmp(*argv,"ecdh") == 0)
			{
			for (i=0; i < EC_NUM; i++)
				ecdh_doit[i]=1;
			}
		else
971 972
#endif
			{
973 974 975
			BIO_printf(bio_err,"Error: bad option or value\n");
			BIO_printf(bio_err,"\n");
			BIO_printf(bio_err,"Available values:\n");
976
#ifndef OPENSSL_NO_MD2
977 978
			BIO_printf(bio_err,"md2      ");
#endif
979
#ifndef OPENSSL_NO_MDC2
980 981
			BIO_printf(bio_err,"mdc2     ");
#endif
982
#ifndef OPENSSL_NO_MD4
983 984
			BIO_printf(bio_err,"md4      ");
#endif
985
#ifndef OPENSSL_NO_MD5
986
			BIO_printf(bio_err,"md5      ");
987
#ifndef OPENSSL_NO_HMAC
988 989 990
			BIO_printf(bio_err,"hmac     ");
#endif
#endif
991
#ifndef OPENSSL_NO_SHA1
992 993
			BIO_printf(bio_err,"sha1     ");
#endif
994
#ifndef OPENSSL_NO_RIPEMD160
995 996
			BIO_printf(bio_err,"rmd160");
#endif
997 998 999
#if !defined(OPENSSL_NO_MD2) || !defined(OPENSSL_NO_MDC2) || \
    !defined(OPENSSL_NO_MD4) || !defined(OPENSSL_NO_MD5) || \
    !defined(OPENSSL_NO_SHA1) || !defined(OPENSSL_NO_RIPEMD160)
1000 1001 1002
			BIO_printf(bio_err,"\n");
#endif

1003
#ifndef OPENSSL_NO_IDEA
1004 1005
			BIO_printf(bio_err,"idea-cbc ");
#endif
1006
#ifndef OPENSSL_NO_RC2
1007 1008
			BIO_printf(bio_err,"rc2-cbc  ");
#endif
1009
#ifndef OPENSSL_NO_RC5
1010 1011
			BIO_printf(bio_err,"rc5-cbc  ");
#endif
1012
#ifndef OPENSSL_NO_BF
1013 1014
			BIO_printf(bio_err,"bf-cbc");
#endif
1015 1016
#if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_RC2) || \
    !defined(OPENSSL_NO_BF) || !defined(OPENSSL_NO_RC5)
1017 1018
			BIO_printf(bio_err,"\n");
#endif
1019
#ifndef OPENSSL_NO_DES
1020
			BIO_printf(bio_err,"des-cbc  des-ede3 ");
1021 1022 1023 1024
#endif
#ifndef OPENSSL_NO_AES
			BIO_printf(bio_err,"aes-128-cbc aes-192-cbc aes-256-cbc ");
#endif
1025
#ifndef OPENSSL_NO_RC4
1026 1027
			BIO_printf(bio_err,"rc4");
#endif
1028 1029
			BIO_printf(bio_err,"\n");

1030
#ifndef OPENSSL_NO_RSA
1031
			BIO_printf(bio_err,"rsa512   rsa1024  rsa2048  rsa4096\n");
1032
#endif
1033

1034
#ifndef OPENSSL_NO_DSA
1035 1036
			BIO_printf(bio_err,"dsa512   dsa1024  dsa2048\n");
#endif
B
Bodo Möller 已提交
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
#ifndef OPENSSL_NO_ECDSA
			BIO_printf(bio_err,"ecdsap160 ecdsap224 ecdsap256 ecdsap384 ecdsap521\n");
			BIO_printf(bio_err,"ecdsak163 ecdsak233 ecdsak283 ecdsak409 ecdsak571\n");
			BIO_printf(bio_err,"ecdsab163 ecdsab233 ecdsab283 ecdsab409 ecdsab571\n");
			BIO_printf(bio_err,"ecdsa\n");
#endif
#ifndef OPENSSL_NO_ECDH
			BIO_printf(bio_err,"ecdhp160  ecdhp224  ecdhp256  ecdhp384  ecdhp521\n");
			BIO_printf(bio_err,"ecdhk163  ecdhk233  ecdhk283  ecdhk409  ecdhk571\n");
			BIO_printf(bio_err,"ecdhb163  ecdhb233  ecdhb283  ecdhb409  ecdhb571\n");
			BIO_printf(bio_err,"ecdh\n");
#endif
1049

1050
#ifndef OPENSSL_NO_IDEA
1051 1052
			BIO_printf(bio_err,"idea     ");
#endif
1053
#ifndef OPENSSL_NO_RC2
1054 1055
			BIO_printf(bio_err,"rc2      ");
#endif
1056
#ifndef OPENSSL_NO_DES
1057
			BIO_printf(bio_err,"des      ");
1058
#endif
1059 1060 1061
#ifndef OPENSSL_NO_AES
			BIO_printf(bio_err,"aes      ");
#endif
1062
#ifndef OPENSSL_NO_RSA
1063 1064
			BIO_printf(bio_err,"rsa      ");
#endif
1065
#ifndef OPENSSL_NO_BF
1066 1067
			BIO_printf(bio_err,"blowfish");
#endif
1068 1069
#if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_RC2) || \
    !defined(OPENSSL_NO_DES) || !defined(OPENSSL_NO_RSA) || \
1070
    !defined(OPENSSL_NO_BF) || !defined(OPENSSL_NO_AES)
1071
			BIO_printf(bio_err,"\n");
1072 1073
#endif

1074
			BIO_printf(bio_err,"\n");
1075
			BIO_printf(bio_err,"Available options:\n");
1076
#if defined(TIMES) || defined(USE_TOD)
1077
			BIO_printf(bio_err,"-elapsed        measure time in real time instead of CPU user time.\n");
1078
#endif
1079
			BIO_printf(bio_err,"-engine e       use engine e, possibly a hardware device.\n");
1080 1081 1082
			BIO_printf(bio_err,"-evp e          use EVP e.\n");
			BIO_printf(bio_err,"-decrypt        time decryption instead of encryption (only EVP).\n");
			BIO_printf(bio_err,"-mr             produce machine readable output.\n");
1083
#ifdef HAVE_FORK
1084
			BIO_printf(bio_err,"-multi n        run n benchmarks in parallel.\n");
1085
#endif
1086 1087 1088 1089 1090 1091 1092
			goto end;
			}
		argc--;
		argv++;
		j++;
		}

1093
#ifdef HAVE_FORK
1094 1095
	if(multi && do_multi(multi))
		goto show_res;
1096
#endif
1097

1098 1099 1100
	if (j == 0)
		{
		for (i=0; i<ALGOR_NUM; i++)
B
Bodo Möller 已提交
1101 1102 1103 1104
			{
			if (i != D_EVP)
				doit[i]=1;
			}
1105 1106 1107 1108 1109 1110 1111 1112
		for (i=0; i<RSA_NUM; i++)
			rsa_doit[i]=1;
		for (i=0; i<DSA_NUM; i++)
			dsa_doit[i]=1;
		}
	for (i=0; i<ALGOR_NUM; i++)
		if (doit[i]) pr_header++;

1113
	if (usertime == 0 && !mr)
1114
		BIO_printf(bio_err,"You have chosen to measure elapsed time instead of user CPU time.\n");
1115
	if (usertime <= 0 && !mr)
1116 1117 1118 1119
		{
		BIO_printf(bio_err,"To get the most accurate results, try to run this\n");
		BIO_printf(bio_err,"program when this computer is idle.\n");
		}
1120

1121
#ifndef OPENSSL_NO_RSA
1122 1123
	for (i=0; i<RSA_NUM; i++)
		{
1124
		const unsigned char *p;
1125 1126 1127 1128 1129 1130 1131 1132

		p=rsa_data[i];
		rsa_key[i]=d2i_RSAPrivateKey(NULL,&p,rsa_data_length[i]);
		if (rsa_key[i] == NULL)
			{
			BIO_printf(bio_err,"internal error loading RSA key number %d\n",i);
			goto end;
			}
1133 1134 1135
#if 0
		else
			{
1136 1137 1138
			BIO_printf(bio_err,mr ? "+RK:%d:"
				   : "Loaded RSA key, %d bit modulus and e= 0x",
				   BN_num_bits(rsa_key[i]->n));
1139 1140 1141 1142
			BN_print(bio_err,rsa_key[i]->e);
			BIO_printf(bio_err,"\n");
			}
#endif
1143 1144 1145
		}
#endif

1146
#ifndef OPENSSL_NO_DSA
1147 1148 1149 1150 1151
	dsa_key[0]=get_dsa512();
	dsa_key[1]=get_dsa1024();
	dsa_key[2]=get_dsa2048();
#endif

1152
#ifndef OPENSSL_NO_DES
1153 1154 1155
	DES_set_key_unchecked(&key,&sch);
	DES_set_key_unchecked(&key2,&sch2);
	DES_set_key_unchecked(&key3,&sch3);
1156
#endif
1157 1158 1159 1160 1161
#ifndef OPENSSL_NO_AES
	AES_set_encrypt_key(key16,128,&aes_ks1);
	AES_set_encrypt_key(key24,192,&aes_ks2);
	AES_set_encrypt_key(key32,256,&aes_ks3);
#endif
1162
#ifndef OPENSSL_NO_IDEA
1163 1164
	idea_set_encrypt_key(key16,&idea_ks);
#endif
1165
#ifndef OPENSSL_NO_RC4
1166 1167
	RC4_set_key(&rc4_ks,16,key16);
#endif
1168
#ifndef OPENSSL_NO_RC2
1169 1170
	RC2_set_key(&rc2_ks,16,key16,128);
#endif
1171
#ifndef OPENSSL_NO_RC5
1172 1173
	RC5_32_set_key(&rc5_ks,16,key16,12);
#endif
1174
#ifndef OPENSSL_NO_BF
1175 1176
	BF_set_key(&bf_ks,16,key16);
#endif
1177
#ifndef OPENSSL_NO_CAST
1178 1179
	CAST_set_key(&cast_ks,16,key16);
#endif
1180
#ifndef OPENSSL_NO_RSA
1181
	memset(rsa_c,0,sizeof(rsa_c));
1182
#endif
1183
#ifndef SIGALRM
1184
#ifndef OPENSSL_NO_DES
1185 1186 1187 1188 1189
	BIO_printf(bio_err,"First we calculate the approximate speed ...\n");
	count=10;
	do	{
		long i;
		count*=2;
D
Dr. Stephen Henson 已提交
1190
		Time_F(START);
1191
		for (i=count; i; i--)
D
Dr. Stephen Henson 已提交
1192
			DES_ecb_encrypt(buf_as_des_cblock,buf_as_des_cblock,
1193
				&sch,DES_ENCRYPT);
1194
		d=Time_F(STOP);
1195
		} while (d <3);
1196
	save_count=count;
1197 1198
	c[D_MD2][0]=count/10;
	c[D_MDC2][0]=count/10;
1199
	c[D_MD4][0]=count;
1200
	c[D_MD5][0]=count;
1201
	c[D_HMAC][0]=count;
1202
	c[D_SHA1][0]=count;
1203
	c[D_RMD160][0]=count;
1204 1205 1206 1207 1208
	c[D_RC4][0]=count*5;
	c[D_CBC_DES][0]=count;
	c[D_EDE3_DES][0]=count/3;
	c[D_CBC_IDEA][0]=count;
	c[D_CBC_RC2][0]=count;
1209
	c[D_CBC_RC5][0]=count;
1210
	c[D_CBC_BF][0]=count;
1211
	c[D_CBC_CAST][0]=count;
1212 1213 1214 1215 1216

	for (i=1; i<SIZE_NUM; i++)
		{
		c[D_MD2][i]=c[D_MD2][0]*4*lengths[0]/lengths[i];
		c[D_MDC2][i]=c[D_MDC2][0]*4*lengths[0]/lengths[i];
1217
		c[D_MD4][i]=c[D_MD4][0]*4*lengths[0]/lengths[i];
1218
		c[D_MD5][i]=c[D_MD5][0]*4*lengths[0]/lengths[i];
1219
		c[D_HMAC][i]=c[D_HMAC][0]*4*lengths[0]/lengths[i];
1220
		c[D_SHA1][i]=c[D_SHA1][0]*4*lengths[0]/lengths[i];
1221
		c[D_RMD160][i]=c[D_RMD160][0]*4*lengths[0]/lengths[i];
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
		}
	for (i=1; i<SIZE_NUM; i++)
		{
		long l0,l1;

		l0=(long)lengths[i-1];
		l1=(long)lengths[i];
		c[D_RC4][i]=c[D_RC4][i-1]*l0/l1;
		c[D_CBC_DES][i]=c[D_CBC_DES][i-1]*l0/l1;
		c[D_EDE3_DES][i]=c[D_EDE3_DES][i-1]*l0/l1;
		c[D_CBC_IDEA][i]=c[D_CBC_IDEA][i-1]*l0/l1;
		c[D_CBC_RC2][i]=c[D_CBC_RC2][i-1]*l0/l1;
1234
		c[D_CBC_RC5][i]=c[D_CBC_RC5][i-1]*l0/l1;
1235
		c[D_CBC_BF][i]=c[D_CBC_BF][i-1]*l0/l1;
1236
		c[D_CBC_CAST][i]=c[D_CBC_CAST][i-1]*l0/l1;
1237
		}
1238
#ifndef OPENSSL_NO_RSA
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
	rsa_c[R_RSA_512][0]=count/2000;
	rsa_c[R_RSA_512][1]=count/400;
	for (i=1; i<RSA_NUM; i++)
		{
		rsa_c[i][0]=rsa_c[i-1][0]/8;
		rsa_c[i][1]=rsa_c[i-1][1]/4;
		if ((rsa_doit[i] <= 1) && (rsa_c[i][0] == 0))
			rsa_doit[i]=0;
		else
			{
1249
			if (rsa_c[i][0] == 0)
1250 1251 1252 1253 1254 1255
				{
				rsa_c[i][0]=1;
				rsa_c[i][1]=20;
				}
			}				
		}
1256
#endif
1257

1258
#ifndef OPENSSL_NO_DSA
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
	dsa_c[R_DSA_512][0]=count/1000;
	dsa_c[R_DSA_512][1]=count/1000/2;
	for (i=1; i<DSA_NUM; i++)
		{
		dsa_c[i][0]=dsa_c[i-1][0]/4;
		dsa_c[i][1]=dsa_c[i-1][1]/4;
		if ((dsa_doit[i] <= 1) && (dsa_c[i][0] == 0))
			dsa_doit[i]=0;
		else
			{
			if (dsa_c[i] == 0)
				{
				dsa_c[i][0]=1;
				dsa_c[i][1]=1;
				}
			}				
		}
1276
#endif
1277

B
Bodo Möller 已提交
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
#ifndef OPENSSL_NO_ECDSA
	ecdsa_c[R_EC_P160][0]=count/1000;
	ecdsa_c[R_EC_P160][1]=count/1000/2;
	for (i=R_EC_P224; i<=R_EC_P521; i++)
		{
		ecdsa_c[i][0]=ecdsa_c[i-1][0]/2;
		ecdsa_c[i][1]=ecdsa_c[i-1][1]/2;
		if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
			ecdsa_doit[i]=0;
		else
			{
			if (ecdsa_c[i] == 0)
				{
				ecdsa_c[i][0]=1;
				ecdsa_c[i][1]=1;
				}
			}
		}
	ecdsa_c[R_EC_K163][0]=count/1000;
	ecdsa_c[R_EC_K163][1]=count/1000/2;
	for (i=R_EC_K233; i<=R_EC_K571; i++)
		{
		ecdsa_c[i][0]=ecdsa_c[i-1][0]/2;
		ecdsa_c[i][1]=ecdsa_c[i-1][1]/2;
		if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
			ecdsa_doit[i]=0;
		else
			{
			if (ecdsa_c[i] == 0)
				{
				ecdsa_c[i][0]=1;
				ecdsa_c[i][1]=1;
				}
			}
		}
	ecdsa_c[R_EC_B163][0]=count/1000;
	ecdsa_c[R_EC_B163][1]=count/1000/2;
	for (i=R_EC_B233; i<=R_EC_B571; i++)
		{
		ecdsa_c[i][0]=ecdsa_c[i-1][0]/2;
		ecdsa_c[i][1]=ecdsa_c[i-1][1]/2;
		if ((ecdsa_doit[i] <= 1) && (ecdsa_c[i][0] == 0))
			ecdsa_doit[i]=0;
		else
			{
			if (ecdsa_c[i] == 0)
				{
				ecdsa_c[i][0]=1;
				ecdsa_c[i][1]=1;
				}
			}
		}
#endif

#ifndef OPENSSL_NO_ECDH
	ecdh_c[R_EC_P160][0]=count/1000;
	ecdh_c[R_EC_P160][1]=count/1000;
	for (i=R_EC_P224; i<=R_EC_P521; i++)
		{
		ecdh_c[i][0]=ecdh_c[i-1][0]/2;
		ecdh_c[i][1]=ecdh_c[i-1][1]/2;
		if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
			ecdh_doit[i]=0;
		else
			{
			if (ecdh_c[i] == 0)
				{
				ecdh_c[i][0]=1;
				ecdh_c[i][1]=1;
				}
			}
		}
	ecdh_c[R_EC_K163][0]=count/1000;
	ecdh_c[R_EC_K163][1]=count/1000;
	for (i=R_EC_K233; i<=R_EC_K571; i++)
		{
		ecdh_c[i][0]=ecdh_c[i-1][0]/2;
		ecdh_c[i][1]=ecdh_c[i-1][1]/2;
		if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
			ecdh_doit[i]=0;
		else
			{
			if (ecdh_c[i] == 0)
				{
				ecdh_c[i][0]=1;
				ecdh_c[i][1]=1;
				}
			}
		}
	ecdh_c[R_EC_B163][0]=count/1000;
	ecdh_c[R_EC_B163][1]=count/1000;
	for (i=R_EC_B233; i<=R_EC_B571; i++)
		{
		ecdh_c[i][0]=ecdh_c[i-1][0]/2;
		ecdh_c[i][1]=ecdh_c[i-1][1]/2;
		if ((ecdh_doit[i] <= 1) && (ecdh_c[i][0] == 0))
			ecdh_doit[i]=0;
		else
			{
			if (ecdh_c[i] == 0)
				{
				ecdh_c[i][0]=1;
				ecdh_c[i][1]=1;
				}
			}
		}
#endif

1386
#define COND(d)	(count < (d))
1387 1388
#define COUNT(d) (d)
#else
B
Bodo Möller 已提交
1389 1390
/* not worth fixing */
# error "You cannot disable DES on systems without SIGALRM."
1391
#endif /* OPENSSL_NO_DES */
B
Bodo Möller 已提交
1392
#else
1393 1394 1395
#define COND(c)	(run)
#define COUNT(d) (count)
	signal(SIGALRM,sig_done);
B
Bodo Möller 已提交
1396
#endif /* SIGALRM */
1397

1398
#ifndef OPENSSL_NO_MD2
1399 1400 1401 1402 1403
	if (doit[D_MD2])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_MD2],c[D_MD2][j],lengths[j]);
1404
			Time_F(START);
1405
			for (count=0,run=1; COND(c[D_MD2][j]); count++)
1406 1407 1408
				EVP_Digest(buf,(unsigned long)lengths[j],&(md2[0]),NULL,EVP_md2(),NULL);
			d=Time_F(STOP);
			print_result(D_MD2,j,count,d);
1409 1410 1411
			}
		}
#endif
1412
#ifndef OPENSSL_NO_MDC2
1413 1414 1415 1416 1417
	if (doit[D_MDC2])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_MDC2],c[D_MDC2][j],lengths[j]);
1418
			Time_F(START);
1419
			for (count=0,run=1; COND(c[D_MDC2][j]); count++)
1420 1421 1422
				EVP_Digest(buf,(unsigned long)lengths[j],&(mdc2[0]),NULL,EVP_mdc2(),NULL);
			d=Time_F(STOP);
			print_result(D_MDC2,j,count,d);
1423 1424 1425 1426
			}
		}
#endif

1427
#ifndef OPENSSL_NO_MD4
1428 1429 1430 1431 1432
	if (doit[D_MD4])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_MD4],c[D_MD4][j],lengths[j]);
1433
			Time_F(START);
1434
			for (count=0,run=1; COND(c[D_MD4][j]); count++)
1435 1436 1437
				EVP_Digest(&(buf[0]),(unsigned long)lengths[j],&(md4[0]),NULL,EVP_md4(),NULL);
			d=Time_F(STOP);
			print_result(D_MD4,j,count,d);
1438 1439 1440 1441
			}
		}
#endif

1442
#ifndef OPENSSL_NO_MD5
1443 1444 1445 1446 1447
	if (doit[D_MD5])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_MD5],c[D_MD5][j],lengths[j]);
1448
			Time_F(START);
1449
			for (count=0,run=1; COND(c[D_MD5][j]); count++)
1450 1451 1452
				EVP_Digest(&(buf[0]),(unsigned long)lengths[j],&(md5[0]),NULL,EVP_get_digestbyname("md5"),NULL);
			d=Time_F(STOP);
			print_result(D_MD5,j,count,d);
1453 1454 1455 1456
			}
		}
#endif

1457
#if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_HMAC)
1458
	if (doit[D_HMAC])
1459
		{
1460
		HMAC_CTX hctx;
1461 1462

		HMAC_CTX_init(&hctx);
B
Ben Laurie 已提交
1463
		HMAC_Init_ex(&hctx,(unsigned char *)"This is a key...",
D
 
Dr. Stephen Henson 已提交
1464
			16,EVP_md5(), NULL);
1465

1466 1467
		for (j=0; j<SIZE_NUM; j++)
			{
1468
			print_message(names[D_HMAC],c[D_HMAC][j],lengths[j]);
1469
			Time_F(START);
1470 1471
			for (count=0,run=1; COND(c[D_HMAC][j]); count++)
				{
D
 
Dr. Stephen Henson 已提交
1472 1473 1474
				HMAC_Init_ex(&hctx,NULL,0,NULL,NULL);
				HMAC_Update(&hctx,buf,lengths[j]);
				HMAC_Final(&hctx,&(hmac[0]),NULL);
1475
				}
1476 1477
			d=Time_F(STOP);
			print_result(D_HMAC,j,count,d);
1478
			}
1479
		HMAC_CTX_cleanup(&hctx);
1480 1481
		}
#endif
1482
#ifndef OPENSSL_NO_SHA
1483 1484 1485 1486 1487
	if (doit[D_SHA1])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_SHA1],c[D_SHA1][j],lengths[j]);
1488
			Time_F(START);
1489
			for (count=0,run=1; COND(c[D_SHA1][j]); count++)
1490 1491 1492
				EVP_Digest(buf,(unsigned long)lengths[j],&(sha[0]),NULL,EVP_sha1(),NULL);
			d=Time_F(STOP);
			print_result(D_SHA1,j,count,d);
1493 1494 1495
			}
		}
#endif
1496
#ifndef OPENSSL_NO_RIPEMD
1497 1498 1499 1500 1501
	if (doit[D_RMD160])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_RMD160],c[D_RMD160][j],lengths[j]);
1502
			Time_F(START);
1503
			for (count=0,run=1; COND(c[D_RMD160][j]); count++)
1504 1505 1506
				EVP_Digest(buf,(unsigned long)lengths[j],&(rmd160[0]),NULL,EVP_ripemd160(),NULL);
			d=Time_F(STOP);
			print_result(D_RMD160,j,count,d);
1507 1508 1509
			}
		}
#endif
1510
#ifndef OPENSSL_NO_RC4
1511 1512 1513 1514 1515
	if (doit[D_RC4])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_RC4],c[D_RC4][j],lengths[j]);
1516
			Time_F(START);
1517 1518 1519
			for (count=0,run=1; COND(c[D_RC4][j]); count++)
				RC4(&rc4_ks,(unsigned int)lengths[j],
					buf,buf);
1520 1521
			d=Time_F(STOP);
			print_result(D_RC4,j,count,d);
1522 1523 1524
			}
		}
#endif
1525
#ifndef OPENSSL_NO_DES
1526 1527 1528 1529 1530
	if (doit[D_CBC_DES])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_CBC_DES],c[D_CBC_DES][j],lengths[j]);
1531
			Time_F(START);
1532
			for (count=0,run=1; COND(c[D_CBC_DES][j]); count++)
1533
				DES_ncbc_encrypt(buf,buf,lengths[j],&sch,
1534
						 &DES_iv,DES_ENCRYPT);
1535 1536
			d=Time_F(STOP);
			print_result(D_CBC_DES,j,count,d);
1537 1538 1539 1540 1541 1542 1543 1544
			}
		}

	if (doit[D_EDE3_DES])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_EDE3_DES],c[D_EDE3_DES][j],lengths[j]);
1545
			Time_F(START);
1546
			for (count=0,run=1; COND(c[D_EDE3_DES][j]); count++)
1547
				DES_ede3_cbc_encrypt(buf,buf,lengths[j],
1548
						     &sch,&sch2,&sch3,
1549
						     &DES_iv,DES_ENCRYPT);
1550 1551
			d=Time_F(STOP);
			print_result(D_EDE3_DES,j,count,d);
1552 1553 1554
			}
		}
#endif
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
#ifndef OPENSSL_NO_AES
	if (doit[D_CBC_128_AES])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_CBC_128_AES],c[D_CBC_128_AES][j],lengths[j]);
			Time_F(START);
			for (count=0,run=1; COND(c[D_CBC_128_AES][j]); count++)
				AES_cbc_encrypt(buf,buf,
					(unsigned long)lengths[j],&aes_ks1,
					iv,AES_ENCRYPT);
			d=Time_F(STOP);
			print_result(D_CBC_128_AES,j,count,d);
			}
		}
	if (doit[D_CBC_192_AES])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_CBC_192_AES],c[D_CBC_192_AES][j],lengths[j]);
			Time_F(START);
			for (count=0,run=1; COND(c[D_CBC_192_AES][j]); count++)
				AES_cbc_encrypt(buf,buf,
					(unsigned long)lengths[j],&aes_ks2,
					iv,AES_ENCRYPT);
			d=Time_F(STOP);
			print_result(D_CBC_192_AES,j,count,d);
			}
		}
	if (doit[D_CBC_256_AES])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_CBC_256_AES],c[D_CBC_256_AES][j],lengths[j]);
			Time_F(START);
			for (count=0,run=1; COND(c[D_CBC_256_AES][j]); count++)
				AES_cbc_encrypt(buf,buf,
					(unsigned long)lengths[j],&aes_ks3,
					iv,AES_ENCRYPT);
			d=Time_F(STOP);
			print_result(D_CBC_256_AES,j,count,d);
			}
		}

#endif
1600
#ifndef OPENSSL_NO_IDEA
1601 1602 1603 1604 1605
	if (doit[D_CBC_IDEA])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_CBC_IDEA],c[D_CBC_IDEA][j],lengths[j]);
1606
			Time_F(START);
1607 1608 1609
			for (count=0,run=1; COND(c[D_CBC_IDEA][j]); count++)
				idea_cbc_encrypt(buf,buf,
					(unsigned long)lengths[j],&idea_ks,
1610
					iv,IDEA_ENCRYPT);
1611 1612
			d=Time_F(STOP);
			print_result(D_CBC_IDEA,j,count,d);
1613 1614 1615
			}
		}
#endif
1616
#ifndef OPENSSL_NO_RC2
1617 1618 1619 1620 1621
	if (doit[D_CBC_RC2])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_CBC_RC2],c[D_CBC_RC2][j],lengths[j]);
1622
			Time_F(START);
1623 1624 1625
			for (count=0,run=1; COND(c[D_CBC_RC2][j]); count++)
				RC2_cbc_encrypt(buf,buf,
					(unsigned long)lengths[j],&rc2_ks,
1626
					iv,RC2_ENCRYPT);
1627 1628
			d=Time_F(STOP);
			print_result(D_CBC_RC2,j,count,d);
1629 1630 1631
			}
		}
#endif
1632
#ifndef OPENSSL_NO_RC5
1633 1634 1635 1636 1637
	if (doit[D_CBC_RC5])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_CBC_RC5],c[D_CBC_RC5][j],lengths[j]);
1638
			Time_F(START);
1639 1640 1641
			for (count=0,run=1; COND(c[D_CBC_RC5][j]); count++)
				RC5_32_cbc_encrypt(buf,buf,
					(unsigned long)lengths[j],&rc5_ks,
1642
					iv,RC5_ENCRYPT);
1643 1644
			d=Time_F(STOP);
			print_result(D_CBC_RC5,j,count,d);
1645 1646 1647
			}
		}
#endif
1648
#ifndef OPENSSL_NO_BF
1649 1650 1651 1652 1653
	if (doit[D_CBC_BF])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_CBC_BF],c[D_CBC_BF][j],lengths[j]);
1654
			Time_F(START);
1655 1656 1657
			for (count=0,run=1; COND(c[D_CBC_BF][j]); count++)
				BF_cbc_encrypt(buf,buf,
					(unsigned long)lengths[j],&bf_ks,
1658
					iv,BF_ENCRYPT);
1659 1660
			d=Time_F(STOP);
			print_result(D_CBC_BF,j,count,d);
1661 1662 1663
			}
		}
#endif
1664
#ifndef OPENSSL_NO_CAST
1665 1666 1667 1668 1669
	if (doit[D_CBC_CAST])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_CBC_CAST],c[D_CBC_CAST][j],lengths[j]);
1670
			Time_F(START);
1671 1672 1673
			for (count=0,run=1; COND(c[D_CBC_CAST][j]); count++)
				CAST_cbc_encrypt(buf,buf,
					(unsigned long)lengths[j],&cast_ks,
1674
					iv,CAST_ENCRYPT);
1675 1676
			d=Time_F(STOP);
			print_result(D_CBC_CAST,j,count,d);
1677 1678 1679
			}
		}
#endif
1680

1681 1682 1683 1684
	if (doit[D_EVP])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
1685 1686 1687 1688 1689 1690
			if (evp_cipher)
				{
				EVP_CIPHER_CTX ctx;
				int outl;

				names[D_EVP]=OBJ_nid2ln(evp_cipher->nid);
1691 1692 1693
				/* -O3 -fschedule-insns messes up an
				 * optimization here!  names[D_EVP]
				 * somehow becomes NULL */
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
				print_message(names[D_EVP],save_count,
					lengths[j]);

				EVP_CIPHER_CTX_init(&ctx);
				if(decrypt)
					EVP_DecryptInit_ex(&ctx,evp_cipher,NULL,key16,iv);
				else
					EVP_EncryptInit_ex(&ctx,evp_cipher,NULL,key16,iv);

				Time_F(START);
				if(decrypt)
					for (count=0,run=1; COND(save_count*4*lengths[0]/lengths[j]); count++)
						EVP_DecryptUpdate(&ctx,buf,&outl,buf,lengths[j]);
				else
					for (count=0,run=1; COND(save_count*4*lengths[0]/lengths[j]); count++)
						EVP_EncryptUpdate(&ctx,buf,&outl,buf,lengths[j]);
				if(decrypt)
					EVP_DecryptFinal_ex(&ctx,buf,&outl);
				else
					EVP_EncryptFinal_ex(&ctx,buf,&outl);
				d=Time_F(STOP);
				}
			if (evp_md)
				{
				names[D_EVP]=OBJ_nid2ln(evp_md->type);
				print_message(names[D_EVP],save_count,
					lengths[j]);

				Time_F(START);
B
Ben Laurie 已提交
1723
				for (count=0,run=1; COND(save_count*4*lengths[0]/lengths[j]); count++)
1724 1725 1726 1727
					EVP_Digest(buf,lengths[j],&(md[0]),NULL,evp_md,NULL);

				d=Time_F(STOP);
				}
1728
			print_result(D_EVP,j,count,d);
1729 1730 1731
			}
		}

1732
	RAND_pseudo_bytes(buf,36);
1733
#ifndef OPENSSL_NO_RSA
1734 1735
	for (j=0; j<RSA_NUM; j++)
		{
D
 
Dr. Stephen Henson 已提交
1736
		int ret;
1737
		if (!rsa_doit[j]) continue;
D
 
Dr. Stephen Henson 已提交
1738
		ret=RSA_sign(NID_md5_sha1, buf,36, buf2, &rsa_num, rsa_key[j]);
1739 1740 1741 1742 1743 1744 1745
		if (ret == 0)
			{
			BIO_printf(bio_err,"RSA sign failure.  No RSA sign will be done.\n");
			ERR_print_errors(bio_err);
			rsa_count=1;
			}
		else
1746
			{
1747 1748 1749 1750
			pkey_print_message("private","rsa",
				rsa_c[j][0],rsa_bits[j],
				RSA_SECONDS);
/*			RSA_blinding_on(rsa_key[j],NULL); */
1751
			Time_F(START);
1752
			for (count=0,run=1; COND(rsa_c[j][0]); count++)
1753
				{
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
				ret=RSA_sign(NID_md5_sha1, buf,36, buf2,
					&rsa_num, rsa_key[j]);
				if (ret == 0)
					{
					BIO_printf(bio_err,
						"RSA sign failure\n");
					ERR_print_errors(bio_err);
					count=1;
					break;
					}
1764
				}
1765 1766 1767 1768
			d=Time_F(STOP);
			BIO_printf(bio_err,mr ? "+R1:%ld:%d:%.2f\n"
				   : "%ld %d bit private RSA's in %.2fs\n",
				   count,rsa_bits[j],d);
1769 1770
			rsa_results[j][0]=d/(double)count;
			rsa_count=count;
1771 1772
			}

1773
#if 1
D
 
Dr. Stephen Henson 已提交
1774
		ret=RSA_verify(NID_md5_sha1, buf,36, buf2, rsa_num, rsa_key[j]);
1775
		if (ret <= 0)
1776
			{
1777 1778
			BIO_printf(bio_err,"RSA verify failure.  No RSA verify will be done.\n");
			ERR_print_errors(bio_err);
1779
			rsa_doit[j] = 0;
1780 1781 1782 1783 1784 1785
			}
		else
			{
			pkey_print_message("public","rsa",
				rsa_c[j][1],rsa_bits[j],
				RSA_SECONDS);
1786
			Time_F(START);
1787
			for (count=0,run=1; COND(rsa_c[j][1]); count++)
1788
				{
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
				ret=RSA_verify(NID_md5_sha1, buf,36, buf2,
					rsa_num, rsa_key[j]);
				if (ret == 0)
					{
					BIO_printf(bio_err,
						"RSA verify failure\n");
					ERR_print_errors(bio_err);
					count=1;
					break;
					}
1799
				}
1800 1801 1802 1803
			d=Time_F(STOP);
			BIO_printf(bio_err,mr ? "+R2:%ld:%d:%.2f\n"
				   : "%ld %d bit public RSA's in %.2fs\n",
				   count,rsa_bits[j],d);
1804
			rsa_results[j][1]=d/(double)count;
1805
			}
1806
#endif
1807 1808 1809 1810 1811 1812 1813 1814 1815 1816

		if (rsa_count <= 1)
			{
			/* if longer than 10s, don't do any more */
			for (j++; j<RSA_NUM; j++)
				rsa_doit[j]=0;
			}
		}
#endif

1817
	RAND_pseudo_bytes(buf,20);
1818
#ifndef OPENSSL_NO_DSA
1819 1820 1821 1822 1823
	if (RAND_status() != 1)
		{
		RAND_seed(rnd_seed, sizeof rnd_seed);
		rnd_fake = 1;
		}
1824 1825
	for (j=0; j<DSA_NUM; j++)
		{
1826
		unsigned int kk;
1827
		int ret;
1828

1829
		if (!dsa_doit[j]) continue;
1830
/*		DSA_generate_key(dsa_key[j]); */
1831
/*		DSA_sign_setup(dsa_key[j],NULL); */
1832
		ret=DSA_sign(EVP_PKEY_DSA,buf,20,buf2,
1833
			&kk,dsa_key[j]);
1834 1835 1836 1837 1838 1839 1840
		if (ret == 0)
			{
			BIO_printf(bio_err,"DSA sign failure.  No DSA sign will be done.\n");
			ERR_print_errors(bio_err);
			rsa_count=1;
			}
		else
1841
			{
1842 1843 1844
			pkey_print_message("sign","dsa",
				dsa_c[j][0],dsa_bits[j],
				DSA_SECONDS);
1845
			Time_F(START);
1846
			for (count=0,run=1; COND(dsa_c[j][0]); count++)
1847
				{
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
				ret=DSA_sign(EVP_PKEY_DSA,buf,20,buf2,
					&kk,dsa_key[j]);
				if (ret == 0)
					{
					BIO_printf(bio_err,
						"DSA sign failure\n");
					ERR_print_errors(bio_err);
					count=1;
					break;
					}
1858
				}
1859 1860 1861 1862
			d=Time_F(STOP);
			BIO_printf(bio_err,mr ? "+R3:%ld:%d:%.2f\n"
				   : "%ld %d bit DSA signs in %.2fs\n",
				   count,dsa_bits[j],d);
1863 1864
			dsa_results[j][0]=d/(double)count;
			rsa_count=count;
1865 1866
			}

1867
		ret=DSA_verify(EVP_PKEY_DSA,buf,20,buf2,
1868
			kk,dsa_key[j]);
1869 1870 1871 1872 1873 1874 1875
		if (ret <= 0)
			{
			BIO_printf(bio_err,"DSA verify failure.  No DSA verify will be done.\n");
			ERR_print_errors(bio_err);
			dsa_doit[j] = 0;
			}
		else
1876
			{
1877 1878 1879
			pkey_print_message("verify","dsa",
				dsa_c[j][1],dsa_bits[j],
				DSA_SECONDS);
1880
			Time_F(START);
1881
			for (count=0,run=1; COND(dsa_c[j][1]); count++)
1882
				{
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
				ret=DSA_verify(EVP_PKEY_DSA,buf,20,buf2,
					kk,dsa_key[j]);
				if (ret <= 0)
					{
					BIO_printf(bio_err,
						"DSA verify failure\n");
					ERR_print_errors(bio_err);
					count=1;
					break;
					}
1893
				}
1894 1895 1896 1897
			d=Time_F(STOP);
			BIO_printf(bio_err,mr ? "+R4:%ld:%d:%.2f\n"
				   : "%ld %d bit DSA verify in %.2fs\n",
				   count,dsa_bits[j],d);
1898
			dsa_results[j][1]=d/(double)count;
1899 1900 1901 1902 1903 1904 1905 1906 1907
			}

		if (rsa_count <= 1)
			{
			/* if longer than 10s, don't do any more */
			for (j++; j<DSA_NUM; j++)
				dsa_doit[j]=0;
			}
		}
1908
	if (rnd_fake) RAND_cleanup();
1909
#endif
B
Bodo Möller 已提交
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942

#ifndef OPENSSL_NO_ECDSA
	if (RAND_status() != 1) 
		{
		RAND_seed(rnd_seed, sizeof rnd_seed);
		rnd_fake = 1;
		}
	for (j=0; j<EC_NUM; j++) 
		{
		int ret;

		if (!ecdsa_doit[j]) continue; /* Ignore Curve */ 
		ecdsa[j] = EC_KEY_new();
		if (ecdsa[j] == NULL) 
			{
			BIO_printf(bio_err,"ECDSA failure.\n");
			ERR_print_errors(bio_err);
			rsa_count=1;
			} 
		else 
			{
			ecdsa[j]->group = EC_GROUP_new_by_nid(test_curves[j]);
			/* Could not obtain group information */
			if (ecdsa[j]->group == NULL) 
				{
				BIO_printf(bio_err,"ECDSA failure.Could not obtain group information\n");
				ERR_print_errors(bio_err);
				rsa_count=1;
				} 
			else 
				{
				/* Perform ECDSA signature test */
				EC_KEY_generate_key(ecdsa[j]);
1943
				ret = ECDSA_sign(0, buf, 20, ecdsasig, 
B
Bodo Möller 已提交
1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
					&ecdsasiglen, ecdsa[j]);
				if (ret == 0) 
					{
					BIO_printf(bio_err,"ECDSA sign failure.  No ECDSA sign will be done.\n");
					ERR_print_errors(bio_err);
					rsa_count=1;
					} 
				else 
					{
					pkey_print_message("sign","ecdsa",
						ecdsa_c[j][0], 
						test_curves_bits[j],
						ECDSA_SECONDS);

					Time_F(START);
1959 1960
					for (count=0,run=1; COND(ecdsa_c[j][0]);
						count++) 
B
Bodo Möller 已提交
1961
						{
1962 1963 1964
						ret=ECDSA_sign(0, buf, 20, 
							ecdsasig, &ecdsasiglen,
							ecdsa[j]);
B
Bodo Möller 已提交
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982
						if (ret == 0) 
							{
							BIO_printf(bio_err, "ECDSA sign failure\n");
							ERR_print_errors(bio_err);
							count=1;
							break;
							}
						}
						d=Time_F(STOP);

						BIO_printf(bio_err, mr ? "+R5:%ld:%d:%.2f\n" :
						"%ld %d bit ECDSA signs in %.2fs \n", 
						count, test_curves_bits[j], d);
						ecdsa_results[j][0]=d/(double)count;
						rsa_count=count;
					}

				/* Perform ECDSA verification test */
1983 1984
				ret=ECDSA_verify(0, buf, 20, ecdsasig, 
					ecdsasiglen, ecdsa[j]);
B
Bodo Möller 已提交
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074
				if (ret != 1) 
					{
					BIO_printf(bio_err,"ECDSA verify failure.  No ECDSA verify will be done.\n");
					ERR_print_errors(bio_err);
					ecdsa_doit[j] = 0;
					} 
				else 
					{
					pkey_print_message("verify","ecdsa",
					ecdsa_c[j][1],
					test_curves_bits[j],
					ECDSA_SECONDS);
					Time_F(START);
					for (count=0,run=1; COND(ecdsa_c[j][1]); count++) 
						{
						ret=ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[j]);
						if (ret != 1) 
							{
							BIO_printf(bio_err, "ECDSA verify failure\n");
							ERR_print_errors(bio_err);
							count=1;
							break;
							}
						}
						d=Time_F(STOP);
						BIO_printf(bio_err, mr? "+R6:%ld:%d:%.2f\n"
							: "%ld %d bit ECDSA verify in %.2fs\n",
						count, test_curves_bits[j], d);
						ecdsa_results[j][1]=d/(double)count;
					}

				if (rsa_count <= 1) 
					{
					/* if longer than 10s, don't do any more */
					for (j++; j<EC_NUM; j++)
					ecdsa_doit[j]=0;
					}
				}
			}
		}
	if (rnd_fake) RAND_cleanup();
#endif

#ifndef OPENSSL_NO_ECDH
	if (RAND_status() != 1)
		{
		RAND_seed(rnd_seed, sizeof rnd_seed);
		rnd_fake = 1;
		}
	for (j=0; j<EC_NUM; j++)
		{
		if (!ecdh_doit[j]) continue;
		ecdh_a[j] = EC_KEY_new();
		ecdh_b[j] = EC_KEY_new();
		if ((ecdh_a[j] == NULL) || (ecdh_b[j] == NULL))
			{
			BIO_printf(bio_err,"ECDH failure.\n");
			ERR_print_errors(bio_err);
			rsa_count=1;
			}
		else
			{
			ecdh_a[j]->group = EC_GROUP_new_by_nid(test_curves[j]);
			if (ecdh_a[j]->group == NULL)
				{
				BIO_printf(bio_err,"ECDH failure.\n");
				ERR_print_errors(bio_err);
				rsa_count=1;
				}
			else
				{
				ecdh_b[j]->group = ecdh_a[j]->group;

				/* generate two ECDH key pairs */
				if (!EC_KEY_generate_key(ecdh_a[j]) ||
					!EC_KEY_generate_key(ecdh_b[j]))
					{
					BIO_printf(bio_err,"ECDH key generation failure.\n");
					ERR_print_errors(bio_err);
					rsa_count=1;		
					}
				else
					{
					secret_size_a = ECDH_compute_key(secret_a, 
						ecdh_b[j]->pub_key,
						ecdh_a[j]);
					secret_size_b = ECDH_compute_key(secret_b, 
						ecdh_a[j]->pub_key,
						ecdh_b[j]);
					if (secret_size_a != secret_size_b) 
B
Bodo Möller 已提交
2075
						ecdh_checks = 0;
B
Bodo Möller 已提交
2076
					else
B
Bodo Möller 已提交
2077
						ecdh_checks = 1;
B
Bodo Möller 已提交
2078 2079

					for (secret_idx = 0; 
B
Bodo Möller 已提交
2080 2081
					    (secret_idx < secret_size_a)
						&& (ecdh_checks == 1);
B
Bodo Möller 已提交
2082 2083 2084
					    secret_idx++)
						{
						if (secret_a[secret_idx] != secret_b[secret_idx])
B
Bodo Möller 已提交
2085
						ecdh_checks = 0;
B
Bodo Möller 已提交
2086 2087
						}

B
Bodo Möller 已提交
2088
					if (ecdh_checks == 0)
B
Bodo Möller 已提交
2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
						{
						BIO_printf(bio_err,"ECDH computations don't match.\n");
						ERR_print_errors(bio_err);
						rsa_count=1;		
						}

					pkey_print_message("","ecdh",
					ecdh_c[j][0], 
					test_curves_bits[j],
					ECDH_SECONDS);
					Time_F(START);
					for (count=0,run=1; COND(ecdh_c[j][0]); count++)
						{
						ECDH_compute_key(secret_a, 
						ecdh_b[j]->pub_key,
						ecdh_a[j]);
						}
					d=Time_F(STOP);
					BIO_printf(bio_err, mr ? "+R7:%ld:%d:%.2f\n" :"%ld %d-bit ECDH ops in %.2fs\n",
					count, test_curves_bits[j], d);
					ecdh_results[j][0]=d/(double)count;
					rsa_count=count;
					}
				}
			}

		if (rsa_count <= 1)
			{
			/* if longer than 10s, don't do any more */
			for (j++; j<EC_NUM; j++)
			ecdh_doit[j]=0;
			}
		}
	if (rnd_fake) RAND_cleanup();
#endif
D
Dr. Stephen Henson 已提交
2124
#ifdef HAVE_FORK
2125
show_res:
D
Dr. Stephen Henson 已提交
2126
#endif
2127 2128 2129
	if(!mr)
		{
		fprintf(stdout,"%s\n",SSLeay_version(SSLEAY_VERSION));
2130
        fprintf(stdout,"%s\n",SSLeay_version(SSLEAY_BUILT_ON));
2131 2132
		printf("options:");
		printf("%s ",BN_options());
2133
#ifndef OPENSSL_NO_MD2
2134
		printf("%s ",MD2_options());
2135
#endif
2136
#ifndef OPENSSL_NO_RC4
2137
		printf("%s ",RC4_options());
2138
#endif
2139
#ifndef OPENSSL_NO_DES
2140
		printf("%s ",DES_options());
2141
#endif
2142 2143 2144
#ifndef OPENSSL_NO_AES
		printf("%s ",AES_options());
#endif
2145
#ifndef OPENSSL_NO_IDEA
2146
		printf("%s ",idea_options());
2147
#endif
2148
#ifndef OPENSSL_NO_BF
2149
		printf("%s ",BF_options());
2150
#endif
2151 2152
		fprintf(stdout,"\n%s\n",SSLeay_version(SSLEAY_CFLAGS));
		printf("available timing options: ");
2153
#ifdef TIMES
2154
		printf("TIMES ");
2155 2156
#endif
#ifdef TIMEB
2157
		printf("TIMEB ");
2158 2159
#endif
#ifdef USE_TOD
2160
		printf("USE_TOD ");
2161 2162 2163
#endif
#ifdef HZ
#define as_string(s) (#s)
B
Bodo Möller 已提交
2164
		printf("HZ=%g", (double)HZ);
2165
# ifdef _SC_CLK_TCK
2166
		printf(" [sysconf value]");
2167
# endif
2168
#endif
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
		printf("\n");
		printf("timing function used: %s%s%s%s%s%s%s\n",
		       (ftime_used ? "ftime" : ""),
		       (ftime_used + times_used > 1 ? "," : ""),
		       (times_used ? "times" : ""),
		       (ftime_used + times_used + gettimeofday_used > 1 ? "," : ""),
		       (gettimeofday_used ? "gettimeofday" : ""),
		       (ftime_used + times_used + gettimeofday_used + getrusage_used > 1 ? "," : ""),
		       (getrusage_used ? "getrusage" : ""));
		}
2179 2180 2181

	if (pr_header)
		{
2182 2183 2184 2185 2186 2187 2188
		if(mr)
			fprintf(stdout,"+H");
		else
			{
			fprintf(stdout,"The 'numbers' are in 1000s of bytes per second processed.\n"); 
			fprintf(stdout,"type        ");
			}
2189
		for (j=0;  j<SIZE_NUM; j++)
2190
			fprintf(stdout,mr ? ":%d" : "%7d bytes",lengths[j]);
2191 2192 2193 2194 2195 2196
		fprintf(stdout,"\n");
		}

	for (k=0; k<ALGOR_NUM; k++)
		{
		if (!doit[k]) continue;
2197 2198 2199 2200
		if(mr)
			fprintf(stdout,"+F:%d:%s",k,names[k]);
		else
			fprintf(stdout,"%-13s",names[k]);
2201 2202
		for (j=0; j<SIZE_NUM; j++)
			{
2203
			if (results[k][j] > 10000 && !mr)
2204 2205
				fprintf(stdout," %11.2fk",results[k][j]/1e3);
			else
2206
				fprintf(stdout,mr ? ":%.2f" : " %11.2f ",results[k][j]);
2207 2208 2209
			}
		fprintf(stdout,"\n");
		}
2210
#ifndef OPENSSL_NO_RSA
2211 2212 2213 2214
	j=1;
	for (k=0; k<RSA_NUM; k++)
		{
		if (!rsa_doit[k]) continue;
2215
		if (j && !mr)
2216 2217 2218 2219
			{
			printf("%18ssign    verify    sign/s verify/s\n"," ");
			j=0;
			}
2220 2221 2222 2223 2224 2225 2226 2227
		if(mr)
			fprintf(stdout,"+F2:%u:%u:%f:%f\n",
				k,rsa_bits[k],rsa_results[k][0],
				rsa_results[k][1]);
		else
			fprintf(stdout,"rsa %4u bits %8.4fs %8.4fs %8.1f %8.1f\n",
				rsa_bits[k],rsa_results[k][0],rsa_results[k][1],
				1.0/rsa_results[k][0],1.0/rsa_results[k][1]);
2228 2229
		}
#endif
2230
#ifndef OPENSSL_NO_DSA
2231 2232 2233 2234
	j=1;
	for (k=0; k<DSA_NUM; k++)
		{
		if (!dsa_doit[k]) continue;
2235 2236
		if (j && !mr)
			{
2237 2238 2239
			printf("%18ssign    verify    sign/s verify/s\n"," ");
			j=0;
			}
2240 2241 2242 2243 2244 2245 2246
		if(mr)
			fprintf(stdout,"+F3:%u:%u:%f:%f\n",
				k,dsa_bits[k],dsa_results[k][0],dsa_results[k][1]);
		else
			fprintf(stdout,"dsa %4u bits %8.4fs %8.4fs %8.1f %8.1f\n",
				dsa_bits[k],dsa_results[k][0],dsa_results[k][1],
				1.0/dsa_results[k][0],1.0/dsa_results[k][1]);
2247 2248
		}
#endif
B
Bodo Möller 已提交
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
#ifndef OPENSSL_NO_ECDSA
	j=1;
	for (k=0; k<EC_NUM; k++)
		{
		if (!ecdsa_doit[k]) continue;
		if (j && !mr)
			{
			printf("%30ssign    verify    sign/s verify/s\n"," ");
			j=0;
			}

		if (mr)
			fprintf(stdout,"+F4:%u:%u:%f:%f\n", 
				k, test_curves_bits[k],
				ecdsa_results[k][0],ecdsa_results[k][1]);
		else
			fprintf(stdout,
				"%4u bit ecdsa (%s) %8.4fs %8.4fs %8.1f %8.1f\n", 
				test_curves_bits[k],
				test_curves_names[k],
				ecdsa_results[k][0],ecdsa_results[k][1], 
				1.0/ecdsa_results[k][0],1.0/ecdsa_results[k][1]);
		}
#endif


#ifndef OPENSSL_NO_ECDH
	j=1;
	for (k=0; k<EC_NUM; k++)
		{
		if (!ecdh_doit[k]) continue;
		if (j && !mr)
			{
			printf("%30sop      op/s\n"," ");
			j=0;
			}
		if (mr)
			fprintf(stdout,"+F5:%u:%u:%f:%f\n",
				k, test_curves_bits[k],
				ecdh_results[k][0], 1.0/ecdh_results[k][0]);

		else
			fprintf(stdout,"%4u bit ecdh (%s) %8.4fs %8.1f\n",
				test_curves_bits[k],
				test_curves_names[k],
				ecdh_results[k][0], 1.0/ecdh_results[k][0]);
		}
#endif

B
Ben Laurie 已提交
2298
	mret=0;
B
Bodo Möller 已提交
2299

2300
end:
2301
	ERR_print_errors(bio_err);
2302 2303
	if (buf != NULL) OPENSSL_free(buf);
	if (buf2 != NULL) OPENSSL_free(buf2);
2304
#ifndef OPENSSL_NO_RSA
2305 2306 2307 2308
	for (i=0; i<RSA_NUM; i++)
		if (rsa_key[i] != NULL)
			RSA_free(rsa_key[i]);
#endif
2309
#ifndef OPENSSL_NO_DSA
2310 2311 2312 2313
	for (i=0; i<DSA_NUM; i++)
		if (dsa_key[i] != NULL)
			DSA_free(dsa_key[i]);
#endif
B
Bodo Möller 已提交
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329

#ifndef OPENSSL_NO_ECDSA
	for (i=0; i<EC_NUM; i++)
		if (ecdsa[i] != NULL)
			EC_KEY_free(ecdsa[i]);
#endif
#ifndef OPENSSL_NO_ECDH
	for (i=0; i<EC_NUM; i++)
	{
		if (ecdh_a[i] != NULL)
			EC_KEY_free(ecdh_a[i]);
		if (ecdh_b[i] != NULL)
			EC_KEY_free(ecdh_b[i]);
	}
#endif

2330
	apps_shutdown();
B
Ben Laurie 已提交
2331
	EXIT(mret);
2332 2333
	}

2334
static void print_message(const char *s, long num, int length)
2335 2336
	{
#ifdef SIGALRM
2337 2338
	BIO_printf(bio_err,mr ? "+DT:%s:%d:%d\n"
		   : "Doing %s for %ds on %d size blocks: ",s,SECONDS,length);
B
Bodo Möller 已提交
2339
	(void)BIO_flush(bio_err);
2340 2341
	alarm(SECONDS);
#else
2342 2343
	BIO_printf(bio_err,mr ? "+DN:%s:%ld:%d\n"
		   : "Doing %s %ld times on %d size blocks: ",s,num,length);
B
Bodo Möller 已提交
2344
	(void)BIO_flush(bio_err);
2345 2346 2347 2348 2349 2350
#endif
#ifdef LINT
	num=num;
#endif
	}

U
Ulf Möller 已提交
2351 2352
static void pkey_print_message(char *str, char *str2, long num, int bits,
	     int tm)
2353 2354
	{
#ifdef SIGALRM
2355 2356
	BIO_printf(bio_err,mr ? "+DTP:%d:%s:%s:%d\n"
			   : "Doing %d bit %s %s's for %ds: ",bits,str,str2,tm);
B
Bodo Möller 已提交
2357
	(void)BIO_flush(bio_err);
2358 2359
	alarm(RSA_SECONDS);
#else
2360 2361
	BIO_printf(bio_err,mr ? "+DNP:%ld:%d:%s:%s\n"
			   : "Doing %ld %d bit %s %s's: ",num,bits,str,str2);
B
Bodo Möller 已提交
2362
	(void)BIO_flush(bio_err);
2363 2364 2365 2366 2367
#endif
#ifdef LINT
	num=num;
#endif
	}
2368

2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
static void print_result(int alg,int run_no,int count,double time_used)
	{
	BIO_printf(bio_err,mr ? "+R:%ld:%s:%f\n"
		   : "%ld %s's in %.2fs\n",count,names[alg],time_used);
	results[alg][run_no]=((double)count)/time_used*lengths[run_no];
	}

static char *sstrsep(char **string, const char *delim)
    {
    char isdelim[256];
    char *token = *string;

    if (**string == 0)
        return NULL;

    memset(isdelim, 0, 256);
    isdelim[0] = 1;

    while (*delim)
        {
        isdelim[(unsigned char)(*delim)] = 1;
        delim++;
        }

    while (!isdelim[(unsigned char)(**string)])
        {
        (*string)++;
        }

    if (**string)
        {
        **string = 0;
        (*string)++;
        }

    return token;
    }

2407
#ifdef HAVE_FORK
2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530
static int do_multi(int multi)
	{
	int n;
	int fd[2];
	int *fds;
	static char sep[]=":";

	fds=malloc(multi*sizeof *fds);
	for(n=0 ; n < multi ; ++n)
		{
		pipe(fd);
		if(fork())
			{
			close(fd[1]);
			fds[n]=fd[0];
			}
		else
			{
			close(fd[0]);
			close(1);
			dup(fd[1]);
			close(fd[1]);
			mr=1;
			usertime=0;
			return 0;
			}
		printf("Forked child %d\n",n);
		}

	/* for now, assume the pipe is long enough to take all the output */
	for(n=0 ; n < multi ; ++n)
		{
		FILE *f;
		char buf[1024];
		char *p;

		f=fdopen(fds[n],"r");
		while(fgets(buf,sizeof buf,f))
			{
			p=strchr(buf,'\n');
			if(p)
				*p='\0';
			if(buf[0] != '+')
				{
				fprintf(stderr,"Don't understand line '%s' from child %d\n",
						buf,n);
				continue;
				}
			printf("Got: %s from %d\n",buf,n);
			if(!strncmp(buf,"+F:",3))
				{
				int alg;
				int j;

				p=buf+3;
				alg=atoi(sstrsep(&p,sep));
				sstrsep(&p,sep);
				for(j=0 ; j < SIZE_NUM ; ++j)
					results[alg][j]+=atof(sstrsep(&p,sep));
				}
			else if(!strncmp(buf,"+F2:",4))
				{
				int k;
				double d;
				
				p=buf+4;
				k=atoi(sstrsep(&p,sep));
				sstrsep(&p,sep);

				d=atof(sstrsep(&p,sep));
				if(n)
					rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);
				else
					rsa_results[k][0]=d;

				d=atof(sstrsep(&p,sep));
				if(n)
					rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);
				else
					rsa_results[k][1]=d;
				}
			else if(!strncmp(buf,"+F2:",4))
				{
				int k;
				double d;
				
				p=buf+4;
				k=atoi(sstrsep(&p,sep));
				sstrsep(&p,sep);

				d=atof(sstrsep(&p,sep));
				if(n)
					rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d);
				else
					rsa_results[k][0]=d;

				d=atof(sstrsep(&p,sep));
				if(n)
					rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d);
				else
					rsa_results[k][1]=d;
				}
			else if(!strncmp(buf,"+F3:",4))
				{
				int k;
				double d;
				
				p=buf+4;
				k=atoi(sstrsep(&p,sep));
				sstrsep(&p,sep);

				d=atof(sstrsep(&p,sep));
				if(n)
					dsa_results[k][0]=1/(1/dsa_results[k][0]+1/d);
				else
					dsa_results[k][0]=d;

				d=atof(sstrsep(&p,sep));
				if(n)
					dsa_results[k][1]=1/(1/dsa_results[k][1]+1/d);
				else
					dsa_results[k][1]=d;
				}
B
Bodo Möller 已提交
2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
#ifndef OPENSSL_NO_ECDSA
			else if(!strncmp(buf,"+F4:",4))
				{
				int k;
				double d;
				
				p=buf+4;
				k=atoi(sstrsep(&p,sep));
				sstrsep(&p,sep);

				d=atof(sstrsep(&p,sep));
				if(n)
					ecdsa_results[k][0]=1/(1/ecdsa_results[k][0]+1/d);
				else
					ecdsa_results[k][0]=d;

				d=atof(sstrsep(&p,sep));
				if(n)
					ecdsa_results[k][1]=1/(1/ecdsa_results[k][1]+1/d);
				else
					ecdsa_results[k][1]=d;
				}
#endif 

#ifndef OPENSSL_NO_ECDH
			else if(!strncmp(buf,"+F5:",4))
				{
				int k;
				double d;
				
				p=buf+4;
				k=atoi(sstrsep(&p,sep));
				sstrsep(&p,sep);

				d=atof(sstrsep(&p,sep));
				if(n)
					ecdh_results[k][0]=1/(1/ecdh_results[k][0]+1/d);
				else
					ecdh_results[k][0]=d;

				}
#endif

2574 2575 2576 2577 2578 2579 2580 2581 2582
			else if(!strncmp(buf,"+H:",3))
				{
				}
			else
				fprintf(stderr,"Unknown type '%s' from child %d\n",buf,n);
			}
		}
	return 1;
	}
2583
#endif