speed.c 69.8 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
 * Copyright 2002-2014 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
B
Bodo Möller 已提交
60 61 62 63 64 65 66 67 68 69 70
 *
 * 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.
 *
 * The ECDH and ECDSA speed test software is originally written by 
 * Sumit Gupta of Sun Microsystems Laboratories.
 *
 */
71 72 73

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

74 75
#ifndef OPENSSL_NO_SPEED

76
#undef SECONDS
77 78 79 80
#define SECONDS			3	
#define PRIME_SECONDS	10	
#define RSA_SECONDS		10
#define DSA_SECONDS		10
B
Bodo Möller 已提交
81 82
#define ECDSA_SECONDS   10
#define ECDH_SECONDS    10
83 84 85 86 87 88 89 90 91

/* 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>
R
Richard Levitte 已提交
92

93
#include <string.h>
94
#include <math.h>
95
#include "apps.h"
96
#ifdef OPENSSL_NO_STDIO
97 98
#define APPS_WIN16
#endif
99 100 101
#include <openssl/crypto.h>
#include <openssl/rand.h>
#include <openssl/err.h>
102 103
#include <openssl/evp.h>
#include <openssl/objects.h>
104
#if !defined(OPENSSL_SYS_MSDOS)
105
#include OPENSSL_UNISTD
106
#endif
107

R
Richard Levitte 已提交
108 109 110 111
#ifndef OPENSSL_SYS_NETWARE
#include <signal.h>
#endif

112
#if defined(_WIN32) || defined(__CYGWIN__)
113
#include <windows.h>
114
# if defined(__CYGWIN__) && !defined(_WIN32)
115 116 117 118 119
  /* <windows.h> should define _WIN32, which normally is mutually
   * exclusive with __CYGWIN__, but if it didn't... */
#  define _WIN32
  /* this is done because Cygwin alarm() fails sometimes. */
# endif
120 121
#endif

122
#include <openssl/bn.h>
123
#ifndef OPENSSL_NO_DES
124
#include <openssl/des.h>
125
#endif
126 127 128
#ifndef OPENSSL_NO_AES
#include <openssl/aes.h>
#endif
129 130 131
#ifndef OPENSSL_NO_CAMELLIA
#include <openssl/camellia.h>
#endif
132
#ifndef OPENSSL_NO_MD2
133
#include <openssl/md2.h>
134
#endif
135
#ifndef OPENSSL_NO_MDC2
136
#include <openssl/mdc2.h>
137
#endif
138
#ifndef OPENSSL_NO_MD4
139 140
#include <openssl/md4.h>
#endif
141
#ifndef OPENSSL_NO_MD5
142
#include <openssl/md5.h>
U
Ulf Möller 已提交
143
#endif
144
#ifndef OPENSSL_NO_HMAC
145
#include <openssl/hmac.h>
146
#endif
U
Ulf Möller 已提交
147
#include <openssl/evp.h>
148
#ifndef OPENSSL_NO_SHA
149
#include <openssl/sha.h>
150
#endif
151
#ifndef OPENSSL_NO_RIPEMD
152
#include <openssl/ripemd.h>
153
#endif
154 155 156
#ifndef OPENSSL_NO_WHIRLPOOL
#include <openssl/whrlpool.h>
#endif
157
#ifndef OPENSSL_NO_RC4
158
#include <openssl/rc4.h>
159
#endif
160
#ifndef OPENSSL_NO_RC5
161
#include <openssl/rc5.h>
162
#endif
163
#ifndef OPENSSL_NO_RC2
164
#include <openssl/rc2.h>
165
#endif
166
#ifndef OPENSSL_NO_IDEA
167
#include <openssl/idea.h>
168
#endif
B
Bodo Möller 已提交
169 170 171
#ifndef OPENSSL_NO_SEED
#include <openssl/seed.h>
#endif
172
#ifndef OPENSSL_NO_BF
173
#include <openssl/blowfish.h>
174
#endif
175
#ifndef OPENSSL_NO_CAST
176
#include <openssl/cast.h>
177
#endif
178
#ifndef OPENSSL_NO_RSA
179
#include <openssl/rsa.h>
180
#include "./testrsa.h"
181
#endif
182
#include <openssl/x509.h>
183
#ifndef OPENSSL_NO_DSA
G
Geoff Thorpe 已提交
184
#include <openssl/dsa.h>
185 186
#include "./testdsa.h"
#endif
B
Bodo Möller 已提交
187 188 189 190 191 192
#ifndef OPENSSL_NO_ECDSA
#include <openssl/ecdsa.h>
#endif
#ifndef OPENSSL_NO_ECDH
#include <openssl/ecdh.h>
#endif
193
#include <openssl/modes.h>
194

195 196
#include "../crypto/bn/bn_lcl.h"

D
Dr. Stephen Henson 已提交
197 198 199 200 201 202 203 204 205 206
#ifndef HAVE_FORK
# if defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_NETWARE)
#  define HAVE_FORK 0
# else
#  define HAVE_FORK 1
# endif
#endif

#if HAVE_FORK
#undef NO_FORK
D
Dr. Stephen Henson 已提交
207
#else
D
Dr. Stephen Henson 已提交
208
#define NO_FORK
D
Typo  
Dr. Stephen Henson 已提交
209
#endif
210

211
#undef BUFSIZE
212
#define BUFSIZE	((long)1024*8+1)
213 214
int run=0;

215 216
static int mr=0;
static int usertime=1;
217

218
static double Time_F(int s);
219
static void print_message(const char *s,long num,int length);
220
static void prime_print_message(const char *s, long num);
221 222
static void pkey_print_message(const char *str, const char *str2,
	long num, int bits, int sec);
223
static void print_result(int alg,int run_no,int count,double time_used);
224
static void prime_print_result(int alg, int count, double time_used);
D
Dr. Stephen Henson 已提交
225
#ifndef NO_FORK
226
static int do_multi(int multi);
227
#endif
228

229
#define ALGOR_NUM	30
230
#define SIZE_NUM	5
231
#define PRIME_NUM	2
232 233
#define RSA_NUM		4
#define DSA_NUM		3
B
Bodo Möller 已提交
234

B
Bodo Möller 已提交
235
#define EC_NUM       16
B
Bodo Möller 已提交
236 237
#define MAX_ECDH_SIZE 256

238 239
static const char *names[ALGOR_NUM]={
  "md2","mdc2","md4","md5","hmac(md5)","sha1","rmd160","rc4",
B
Bodo Möller 已提交
240
  "des cbc","des ede3","idea cbc","seed cbc",
241
  "rc2 cbc","rc5-32/12 cbc","blowfish cbc","cast cbc",
242 243
  "aes-128 cbc","aes-192 cbc","aes-256 cbc",
  "camellia-128 cbc","camellia-192 cbc","camellia-256 cbc",
B
Ben Laurie 已提交
244
  "evp","sha256","sha512","whirlpool",
245
  "aes-128 ige","aes-192 ige","aes-256 ige","ghash" };
246
static double results[ALGOR_NUM][SIZE_NUM];
247
static int lengths[SIZE_NUM]={16,64,256,1024,8*1024};
248 249
static const char *prime_names[PRIME_NUM]={
  "prime trial division", "prime coprime" };
D
Dr. Stephen Henson 已提交
250
#ifndef OPENSSL_NO_RSA
251
static double rsa_results[RSA_NUM][2];
D
Dr. Stephen Henson 已提交
252 253
#endif
#ifndef OPENSSL_NO_DSA
254
static double dsa_results[DSA_NUM][2];
D
Dr. Stephen Henson 已提交
255
#endif
N
make  
Nils Larsch 已提交
256
#ifndef OPENSSL_NO_ECDSA
B
Bodo Möller 已提交
257
static double ecdsa_results[EC_NUM][2];
N
make  
Nils Larsch 已提交
258 259
#endif
#ifndef OPENSSL_NO_ECDH
B
Bodo Möller 已提交
260
static double ecdh_results[EC_NUM][1];
N
make  
Nils Larsch 已提交
261
#endif
B
Bodo Möller 已提交
262

N
make  
Nils Larsch 已提交
263 264 265 266
#if defined(OPENSSL_NO_DSA) && !(defined(OPENSSL_NO_ECDSA) && defined(OPENSSL_NO_ECDH))
static const char rnd_seed[] = "string to make the random number generator think it has entropy";
static int rnd_fake = 0;
#endif
267

D
Dr. Stephen Henson 已提交
268 269 270 271 272 273 274
#ifdef SIGALRM
#if defined(__STDC__) || defined(sgi) || defined(_AIX)
#define SIGRETTYPE void
#else
#define SIGRETTYPE int
#endif 

275
static SIGRETTYPE sig_done(int sig);
U
Ulf Möller 已提交
276
static SIGRETTYPE sig_done(int sig)
277 278 279 280 281 282 283 284 285 286 287 288
	{
	signal(SIGALRM,sig_done);
	run=0;
#ifdef LINT
	sig=sig;
#endif
	}
#endif

#define START	0
#define STOP	1

289
#if defined(_WIN32)
R
Richard Levitte 已提交
290

291 292 293
#if !defined(SIGALRM)
# define SIGALRM
#endif
294
static unsigned int lapse,schlock;
295 296
static void alarm_win32(unsigned int secs) { lapse = secs*1000; }
#define alarm alarm_win32
R
Richard Levitte 已提交
297

298 299 300 301 302 303 304
static DWORD WINAPI sleepy(VOID *arg)
	{
	schlock = 1;
	Sleep(lapse);
	run = 0;
	return 0;
	}
R
Richard Levitte 已提交
305

306
static double Time_F(int s)
307
	{
308 309 310
	double ret;
	static HANDLE thr;

311
	if (s == START)
312
		{
313 314 315
		schlock = 0;
		thr = CreateThread(NULL,4096,sleepy,NULL,0,NULL);
		if (thr==NULL)
316
			{
317 318 319
			DWORD ret=GetLastError();
			BIO_printf(bio_err,"unable to CreateThread (%d)",ret);
			ExitProcess(ret);
320
			}
321
		while (!schlock) Sleep(0);	/* scheduler spinlock	*/
322 323 324 325 326 327 328
		ret = app_tminterval(s,usertime);
		}
	else
		{
		ret = app_tminterval(s,usertime);
		if (run) TerminateThread(thr,0);
		CloseHandle(thr);
329
		}
330

331
	return ret;
332 333
	}
#else
334

335 336
static double Time_F(int s)
	{
337 338 339
	double ret = app_tminterval(s,usertime);
	if (s == STOP) alarm(0);
	return ret;
340
	}
341
#endif
342

343

N
make  
Nils Larsch 已提交
344
#ifndef OPENSSL_NO_ECDH
345
static const int KDF1_SHA1_len = 20;
346
static void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen)
347 348
	{
#ifndef OPENSSL_NO_SHA
349
	if (*outlen < SHA_DIGEST_LENGTH)
350
		return NULL;
351 352
	else
		*outlen = SHA_DIGEST_LENGTH;
353 354 355
	return SHA1(in, inlen, out);
#else
	return NULL;
N
make  
Nils Larsch 已提交
356
#endif	/* OPENSSL_NO_SHA */
357
	}
N
make  
Nils Larsch 已提交
358
#endif	/* OPENSSL_NO_ECDH */
359 360


361 362
int MAIN(int, char **);

U
Ulf Möller 已提交
363
int MAIN(int argc, char **argv)
364 365
	{
	unsigned char *buf=NULL,*buf2=NULL;
B
Ben Laurie 已提交
366
	int mret=1;
B
Bodo Möller 已提交
367
	long count=0,save_count=0;
B
Ben Laurie 已提交
368
	int i,j,k;
B
Bodo Möller 已提交
369 370 371
#if !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_DSA)
	long rsa_count;
#endif
372
#ifndef OPENSSL_NO_RSA
373
	unsigned rsa_num;
U
Ulf Möller 已提交
374
#endif
375
	unsigned char md[EVP_MAX_MD_SIZE];
376
#ifndef OPENSSL_NO_MD2
377 378
	unsigned char md2[MD2_DIGEST_LENGTH];
#endif
379
#ifndef OPENSSL_NO_MDC2
380 381
	unsigned char mdc2[MDC2_DIGEST_LENGTH];
#endif
382
#ifndef OPENSSL_NO_MD4
383 384
	unsigned char md4[MD4_DIGEST_LENGTH];
#endif
385
#ifndef OPENSSL_NO_MD5
386
	unsigned char md5[MD5_DIGEST_LENGTH];
387
	unsigned char hmac[MD5_DIGEST_LENGTH];
388
#endif
389
#ifndef OPENSSL_NO_SHA
390
	unsigned char sha[SHA_DIGEST_LENGTH];
391
#ifndef OPENSSL_NO_SHA256
A
Andy Polyakov 已提交
392
	unsigned char sha256[SHA256_DIGEST_LENGTH];
393 394
#endif
#ifndef OPENSSL_NO_SHA512
A
Andy Polyakov 已提交
395
	unsigned char sha512[SHA512_DIGEST_LENGTH];
396
#endif
397
#endif
398 399 400
#ifndef OPENSSL_NO_WHIRLPOOL
	unsigned char whirlpool[WHIRLPOOL_DIGEST_LENGTH];
#endif
401
#ifndef OPENSSL_NO_RIPEMD
402 403
	unsigned char rmd160[RIPEMD160_DIGEST_LENGTH];
#endif
404
#ifndef OPENSSL_NO_RC4
405 406
	RC4_KEY rc4_ks;
#endif
407
#ifndef OPENSSL_NO_RC5
408 409
	RC5_32_KEY rc5_ks;
#endif
410
#ifndef OPENSSL_NO_RC2
411 412
	RC2_KEY rc2_ks;
#endif
413
#ifndef OPENSSL_NO_IDEA
414 415
	IDEA_KEY_SCHEDULE idea_ks;
#endif
B
Bodo Möller 已提交
416 417 418
#ifndef OPENSSL_NO_SEED
	SEED_KEY_SCHEDULE seed_ks;
#endif
419
#ifndef OPENSSL_NO_BF
420
	BF_KEY bf_ks;
421
#endif
422
#ifndef OPENSSL_NO_CAST
423
	CAST_KEY cast_ks;
424
#endif
425
	static const unsigned char key16[16]=
426 427
		{0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
		 0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
N
Nils Larsch 已提交
428
#ifndef OPENSSL_NO_AES
429 430 431 432 433 434 435 436 437
	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};
N
Nils Larsch 已提交
438
#endif
439 440 441 442 443 444 445 446 447 448 449
#ifndef OPENSSL_NO_CAMELLIA
	static const unsigned char ckey24[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 ckey32[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};
#endif
450 451 452 453 454 455
#ifndef OPENSSL_NO_AES
#define MAX_BLOCK_SIZE 128
#else
#define MAX_BLOCK_SIZE 64
#endif
	unsigned char DES_iv[8];
B
Ben Laurie 已提交
456
	unsigned char iv[2*MAX_BLOCK_SIZE/8];
457
#ifndef OPENSSL_NO_DES
458 459 460
	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};
461 462 463
	DES_key_schedule sch;
	DES_key_schedule sch2;
	DES_key_schedule sch3;
464
#endif
465 466 467
#ifndef OPENSSL_NO_AES
	AES_KEY aes_ks1, aes_ks2, aes_ks3;
#endif
468 469 470
#ifndef OPENSSL_NO_CAMELLIA
	CAMELLIA_KEY camellia_ks1, camellia_ks2, camellia_ks3;
#endif
471 472
#define	D_MD2		0
#define	D_MDC2		1
473 474 475 476 477 478 479 480 481
#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
B
Bodo Möller 已提交
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
#define	D_CBC_SEED	11
#define	D_CBC_RC2	12
#define	D_CBC_RC5	13
#define	D_CBC_BF	14
#define	D_CBC_CAST	15
#define D_CBC_128_AES	16
#define D_CBC_192_AES	17
#define D_CBC_256_AES	18
#define D_CBC_128_CML   19 
#define D_CBC_192_CML   20
#define D_CBC_256_CML   21 
#define D_EVP		22
#define D_SHA256	23	
#define D_SHA512	24
#define D_WHIRLPOOL	25
B
Ben Laurie 已提交
497 498 499
#define D_IGE_128_AES   26
#define D_IGE_192_AES   27
#define D_IGE_256_AES   28
500
#define D_GHASH		29
501
	double d=0.0;
502
	long c[ALGOR_NUM][SIZE_NUM];
503 504 505 506 507

#define D_PRIME_TRIAL_DIVISION	0
#define D_PRIME_COPRIME			1
	long prime_c[PRIME_NUM];

508 509 510 511 512 513 514
#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 已提交
515 516

#define R_EC_P160    0
B
Bodo Möller 已提交
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
#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 已提交
532

533
#ifndef OPENSSL_NO_RSA
534 535 536 537 538 539 540 541
	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)};
542
#endif
543
#ifndef OPENSSL_NO_DSA
544 545 546 547
	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 已提交
548 549 550 551 552 553 554 555 556
#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 */
557
	NID_secp160r1,
B
Bodo Möller 已提交
558
	NID_X9_62_prime192v1,
559 560 561 562
	NID_secp224r1,
	NID_X9_62_prime256v1,
	NID_secp384r1,
	NID_secp521r1,
B
Bodo Möller 已提交
563
	/* Binary Curves */
564 565 566 567 568 569 570 571 572 573
	NID_sect163k1,
	NID_sect233k1,
	NID_sect283k1,
	NID_sect409k1,
	NID_sect571k1,
	NID_sect163r2,
	NID_sect233r1,
	NID_sect283r1,
	NID_sect409r1,
	NID_sect571r1
B
Bodo Möller 已提交
574
	}; 
575
	static const char * test_curves_names[EC_NUM] = 
B
Bodo Möller 已提交
576 577 578
	{
	/* Prime Curves */
	"secp160r1",
B
Bodo Möller 已提交
579
	"nistp192",
B
Bodo Möller 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
	"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 已提交
598
        160, 192, 224, 256, 384, 521,
B
Bodo Möller 已提交
599 600 601 602 603 604 605
        163, 233, 283, 409, 571,
        163, 233, 283, 409, 571
        };

#endif

#ifndef OPENSSL_NO_ECDSA
606 607 608 609
	unsigned char ecdsasig[256];
	unsigned int ecdsasiglen;
	EC_KEY *ecdsa[EC_NUM];
	long ecdsa_c[EC_NUM][2];
B
Bodo Möller 已提交
610 611 612
#endif

#ifndef OPENSSL_NO_ECDH
613 614 615 616 617 618
	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;
	int ecdh_checks = 0;
	int secret_idx = 0;
	long ecdh_c[EC_NUM][2];
B
Bodo Möller 已提交
619 620
#endif

621
	int prime_doit[PRIME_NUM];
622 623
	int rsa_doit[RSA_NUM];
	int dsa_doit[DSA_NUM];
N
make  
Nils Larsch 已提交
624
#ifndef OPENSSL_NO_ECDSA
B
Bodo Möller 已提交
625
	int ecdsa_doit[EC_NUM];
N
make  
Nils Larsch 已提交
626 627
#endif
#ifndef OPENSSL_NO_ECDH
B
Bodo Möller 已提交
628
        int ecdh_doit[EC_NUM];
N
make  
Nils Larsch 已提交
629
#endif
630
	int doit[ALGOR_NUM];
631
	int pr_header=0;
632 633
	const EVP_CIPHER *evp_cipher=NULL;
	const EVP_MD *evp_md=NULL;
B
Ben Laurie 已提交
634
	int decrypt=0;
D
Dr. Stephen Henson 已提交
635
#ifndef NO_FORK
636
	int multi=0;
637
#endif
638 639 640 641

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

	apps_startup();
644
	memset(results, 0, sizeof(results));
645
#ifndef OPENSSL_NO_DSA
646 647
	memset(dsa_key,0,sizeof(dsa_key));
#endif
B
Bodo Möller 已提交
648 649 650 651 652 653 654 655 656 657 658
#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

659 660 661

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

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

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

673
	if ((buf=(unsigned char *)OPENSSL_malloc((int)BUFSIZE)) == NULL)
674 675 676 677
		{
		BIO_printf(bio_err,"out of memory\n");
		goto end;
		}
678
	if ((buf2=(unsigned char *)OPENSSL_malloc((int)BUFSIZE)) == NULL)
679 680 681 682 683 684
		{
		BIO_printf(bio_err,"out of memory\n");
		goto end;
		}

	memset(c,0,sizeof(c));
685
	memset(DES_iv,0,sizeof(DES_iv));
686 687 688 689 690 691 692 693
	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 已提交
694 695 696 697 698 699 700 701 702
#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

703 704 705 706 707 708
	
	j=0;
	argc--;
	argv++;
	while (argc)
		{
709
		if	((argc > 0) && (strcmp(*argv,"-elapsed") == 0))
710
			{
711
			usertime = 0;
712 713 714
			j--;	/* Otherwise, -elapsed gets confused with
				   an algorithm. */
			}
715 716 717 718 719 720 721 722 723
		else if	((argc > 0) && (strcmp(*argv,"-evp") == 0))
			{
			argc--;
			argv++;
			if(argc == 0)
				{
				BIO_printf(bio_err,"no EVP given\n");
				goto end;
				}
724 725
			evp_cipher=EVP_get_cipherbyname(*argv);
			if(!evp_cipher)
726
				{
727 728 729 730 731
				evp_md=EVP_get_digestbyname(*argv);
				}
			if(!evp_cipher && !evp_md)
				{
				BIO_printf(bio_err,"%s is an unknown cipher or digest\n",*argv);
732 733 734 735
				goto end;
				}
			doit[D_EVP]=1;
			}
736 737
		else if (argc > 0 && !strcmp(*argv,"-decrypt"))
			{
B
Ben Laurie 已提交
738
			decrypt=1;
739 740 741
			j--;	/* Otherwise, -elapsed gets confused with
				   an algorithm. */
			}
742
#ifndef OPENSSL_NO_ENGINE
743
		else if	((argc > 0) && (strcmp(*argv,"-engine") == 0))
744 745 746 747 748 749 750 751
			{
			argc--;
			argv++;
			if(argc == 0)
				{
				BIO_printf(bio_err,"no engine given\n");
				goto end;
				}
B
Ben Laurie 已提交
752
                        setup_engine(bio_err, *argv, 0);
753
			/* j will be increased again further down.  We just
754 755 756 757 758
			   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--;
			}
759
#endif
D
Dr. Stephen Henson 已提交
760
#ifndef NO_FORK
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
		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;
				}				
776 777
			j--;	/* Otherwise, -mr gets confused with
				   an algorithm. */
778
			}
779
#endif
780 781 782 783 784 785
		else if (argc > 0 && !strcmp(*argv,"-mr"))
			{
			mr=1;
			j--;	/* Otherwise, -mr gets confused with
				   an algorithm. */
			}
786
		else
787
#ifndef OPENSSL_NO_MD2
788 789 790
		if	(strcmp(*argv,"md2") == 0) doit[D_MD2]=1;
		else
#endif
791
#ifndef OPENSSL_NO_MDC2
792 793 794
			if (strcmp(*argv,"mdc2") == 0) doit[D_MDC2]=1;
		else
#endif
795
#ifndef OPENSSL_NO_MD4
796 797 798
			if (strcmp(*argv,"md4") == 0) doit[D_MD4]=1;
		else
#endif
799
#ifndef OPENSSL_NO_MD5
800 801 802
			if (strcmp(*argv,"md5") == 0) doit[D_MD5]=1;
		else
#endif
803
#ifndef OPENSSL_NO_MD5
804
			if (strcmp(*argv,"hmac") == 0) doit[D_HMAC]=1;
805 806
		else
#endif
807
#ifndef OPENSSL_NO_SHA
808 809
			if (strcmp(*argv,"sha1") == 0) doit[D_SHA1]=1;
		else
A
Andy Polyakov 已提交
810 811 812 813
			if (strcmp(*argv,"sha") == 0)	doit[D_SHA1]=1,
							doit[D_SHA256]=1,
							doit[D_SHA512]=1;
		else
814
#ifndef OPENSSL_NO_SHA256
815
			if (strcmp(*argv,"sha256") == 0) doit[D_SHA256]=1;
A
Andy Polyakov 已提交
816
		else
817 818
#endif
#ifndef OPENSSL_NO_SHA512
819
			if (strcmp(*argv,"sha512") == 0) doit[D_SHA512]=1;
820 821
		else
#endif
822
#endif
823 824 825 826
#ifndef OPENSSL_NO_WHIRLPOOL
			if (strcmp(*argv,"whirlpool") == 0) doit[D_WHIRLPOOL]=1;
		else
#endif
827
#ifndef OPENSSL_NO_RIPEMD
828 829 830 831 832 833
			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
834
#endif
835
#ifndef OPENSSL_NO_RC4
836 837 838
			if (strcmp(*argv,"rc4") == 0) doit[D_RC4]=1;
		else 
#endif
839
#ifndef OPENSSL_NO_DES
840 841 842 843
			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
844 845 846 847
#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;
B
Ben Laurie 已提交
848 849 850 851
		else    if (strcmp(*argv,"aes-128-ige") == 0) doit[D_IGE_128_AES]=1;
		else	if (strcmp(*argv,"aes-192-ige") == 0) doit[D_IGE_192_AES]=1;
		else	if (strcmp(*argv,"aes-256-ige") == 0) doit[D_IGE_256_AES]=1;
                else
852
#endif
853 854 855 856 857 858
#ifndef OPENSSL_NO_CAMELLIA
			if (strcmp(*argv,"camellia-128-cbc") == 0) doit[D_CBC_128_CML]=1;
		else    if (strcmp(*argv,"camellia-192-cbc") == 0) doit[D_CBC_192_CML]=1;
		else    if (strcmp(*argv,"camellia-256-cbc") == 0) doit[D_CBC_256_CML]=1;
		else
#endif
859
#ifndef OPENSSL_NO_RSA
860
#if 0 /* was: #ifdef RSAref */
861 862
			if (strcmp(*argv,"rsaref") == 0) 
			{
863
			RSA_set_default_openssl_method(RSA_PKCS1_RSAref());
864 865 866 867
			j--;
			}
		else
#endif
868
#ifndef RSA_NULL
869
			if (strcmp(*argv,"openssl") == 0) 
870
			{
871
			RSA_set_default_method(RSA_PKCS1_SSLeay());
872 873 874
			j--;
			}
		else
875
#endif
876
#endif /* !OPENSSL_NO_RSA */
877 878 879 880 881 882 883 884
		     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
885
#ifndef OPENSSL_NO_RC2
886 887 888 889
		     if (strcmp(*argv,"rc2-cbc") == 0) doit[D_CBC_RC2]=1;
		else if (strcmp(*argv,"rc2") == 0) doit[D_CBC_RC2]=1;
		else
#endif
890
#ifndef OPENSSL_NO_RC5
891 892 893 894
		     if (strcmp(*argv,"rc5-cbc") == 0) doit[D_CBC_RC5]=1;
		else if (strcmp(*argv,"rc5") == 0) doit[D_CBC_RC5]=1;
		else
#endif
895
#ifndef OPENSSL_NO_IDEA
896 897 898 899
		     if (strcmp(*argv,"idea-cbc") == 0) doit[D_CBC_IDEA]=1;
		else if (strcmp(*argv,"idea") == 0) doit[D_CBC_IDEA]=1;
		else
#endif
B
Bodo Möller 已提交
900 901 902 903 904
#ifndef OPENSSL_NO_SEED
		     if (strcmp(*argv,"seed-cbc") == 0) doit[D_CBC_SEED]=1;
		else if (strcmp(*argv,"seed") == 0) doit[D_CBC_SEED]=1;
		else
#endif
905
#ifndef OPENSSL_NO_BF
906 907
		     if (strcmp(*argv,"bf-cbc") == 0) doit[D_CBC_BF]=1;
		else if (strcmp(*argv,"blowfish") == 0) doit[D_CBC_BF]=1;
908 909 910
		else if (strcmp(*argv,"bf") == 0) doit[D_CBC_BF]=1;
		else
#endif
911
#ifndef OPENSSL_NO_CAST
912 913 914
		     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;
915 916
		else
#endif
917
#ifndef OPENSSL_NO_DES
918 919 920 921 922 923 924
			if (strcmp(*argv,"des") == 0)
			{
			doit[D_CBC_DES]=1;
			doit[D_EDE3_DES]=1;
			}
		else
#endif
925 926 927 928 929 930 931
#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;
			}
932 933 934 935
		else if (strcmp(*argv,"ghash") == 0)
			{
			doit[D_GHASH]=1;
			}
936 937
		else
#endif
938 939 940 941 942 943 944 945 946
#ifndef OPENSSL_NO_CAMELLIA
			if (strcmp(*argv,"camellia") == 0)
			{
			doit[D_CBC_128_CML]=1;
			doit[D_CBC_192_CML]=1;
			doit[D_CBC_256_CML]=1;
			}
		else
#endif
947
#ifndef OPENSSL_NO_RSA
948 949 950 951 952 953 954 955 956
			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
957
#ifndef OPENSSL_NO_DSA
958 959 960 961
			if (strcmp(*argv,"dsa") == 0)
			{
			dsa_doit[R_DSA_512]=1;
			dsa_doit[R_DSA_1024]=1;
962
			dsa_doit[R_DSA_2048]=1;
963 964
			}
		else
B
Bodo Möller 已提交
965 966 967
#endif
#ifndef OPENSSL_NO_ECDSA
		     if (strcmp(*argv,"ecdsap160") == 0) ecdsa_doit[R_EC_P160]=2;
968
		else if (strcmp(*argv,"ecdsap192") == 0) ecdsa_doit[R_EC_P192]=2;
B
Bodo Möller 已提交
969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
		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
991
			 if (strcmp(*argv,"ecdhp160") == 0) ecdh_doit[R_EC_P160]=2;
992
		else if (strcmp(*argv,"ecdhp192") == 0) ecdh_doit[R_EC_P192]=2;
B
Bodo Möller 已提交
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
		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
1013
#endif
1014
			 if (strcmp(*argv,"prime-trial-division") == 0) prime_doit[D_PRIME_TRIAL_DIVISION]=1;
1015
		else if (strcmp(*argv,"prime-coprime") == 0) 		prime_doit[D_PRIME_COPRIME]=1;
1016
		else
1017
			{
1018 1019 1020
			BIO_printf(bio_err,"Error: bad option or value\n");
			BIO_printf(bio_err,"\n");
			BIO_printf(bio_err,"Available values:\n");
1021
#ifndef OPENSSL_NO_MD2
1022 1023
			BIO_printf(bio_err,"md2      ");
#endif
1024
#ifndef OPENSSL_NO_MDC2
1025 1026
			BIO_printf(bio_err,"mdc2     ");
#endif
1027
#ifndef OPENSSL_NO_MD4
1028 1029
			BIO_printf(bio_err,"md4      ");
#endif
1030
#ifndef OPENSSL_NO_MD5
1031
			BIO_printf(bio_err,"md5      ");
1032
#ifndef OPENSSL_NO_HMAC
1033 1034 1035
			BIO_printf(bio_err,"hmac     ");
#endif
#endif
1036
#ifndef OPENSSL_NO_SHA1
1037
			BIO_printf(bio_err,"sha1     ");
1038 1039 1040 1041 1042 1043
#endif
#ifndef OPENSSL_NO_SHA256
			BIO_printf(bio_err,"sha256   ");
#endif
#ifndef OPENSSL_NO_SHA512
			BIO_printf(bio_err,"sha512   ");
1044
#endif
1045 1046 1047
#ifndef OPENSSL_NO_WHIRLPOOL
			BIO_printf(bio_err,"whirlpool");
#endif
1048
#ifndef OPENSSL_NO_RIPEMD160
1049 1050
			BIO_printf(bio_err,"rmd160");
#endif
1051 1052
#if !defined(OPENSSL_NO_MD2) || !defined(OPENSSL_NO_MDC2) || \
    !defined(OPENSSL_NO_MD4) || !defined(OPENSSL_NO_MD5) || \
1053 1054
    !defined(OPENSSL_NO_SHA1) || !defined(OPENSSL_NO_RIPEMD160) || \
    !defined(OPENSSL_NO_WHIRLPOOL)
1055 1056 1057
			BIO_printf(bio_err,"\n");
#endif

1058
#ifndef OPENSSL_NO_IDEA
1059 1060
			BIO_printf(bio_err,"idea-cbc ");
#endif
B
Bodo Möller 已提交
1061 1062 1063
#ifndef OPENSSL_NO_SEED
			BIO_printf(bio_err,"seed-cbc ");
#endif
1064
#ifndef OPENSSL_NO_RC2
1065 1066
			BIO_printf(bio_err,"rc2-cbc  ");
#endif
1067
#ifndef OPENSSL_NO_RC5
1068 1069
			BIO_printf(bio_err,"rc5-cbc  ");
#endif
1070
#ifndef OPENSSL_NO_BF
1071 1072
			BIO_printf(bio_err,"bf-cbc");
#endif
B
Bodo Möller 已提交
1073
#if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_SEED) || !defined(OPENSSL_NO_RC2) || \
1074
    !defined(OPENSSL_NO_BF) || !defined(OPENSSL_NO_RC5)
1075 1076
			BIO_printf(bio_err,"\n");
#endif
1077
#ifndef OPENSSL_NO_DES
1078
			BIO_printf(bio_err,"des-cbc  des-ede3 ");
1079 1080 1081
#endif
#ifndef OPENSSL_NO_AES
			BIO_printf(bio_err,"aes-128-cbc aes-192-cbc aes-256-cbc ");
B
Ben Laurie 已提交
1082
			BIO_printf(bio_err,"aes-128-ige aes-192-ige aes-256-ige ");
1083
#endif
1084 1085 1086 1087
#ifndef OPENSSL_NO_CAMELLIA
			BIO_printf(bio_err,"\n");
			BIO_printf(bio_err,"camellia-128-cbc camellia-192-cbc camellia-256-cbc ");
#endif
1088
#ifndef OPENSSL_NO_RC4
1089 1090
			BIO_printf(bio_err,"rc4");
#endif
1091 1092
			BIO_printf(bio_err,"\n");

1093
#ifndef OPENSSL_NO_RSA
1094
			BIO_printf(bio_err,"rsa512   rsa1024  rsa2048  rsa4096\n");
1095
#endif
1096

1097
#ifndef OPENSSL_NO_DSA
1098 1099
			BIO_printf(bio_err,"dsa512   dsa1024  dsa2048\n");
#endif
B
Bodo Möller 已提交
1100
#ifndef OPENSSL_NO_ECDSA
1101
			BIO_printf(bio_err,"ecdsap160 ecdsap192 ecdsap224 ecdsap256 ecdsap384 ecdsap521\n");
B
Bodo Möller 已提交
1102 1103 1104 1105 1106
			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
1107
			BIO_printf(bio_err,"ecdhp160  ecdhp192  ecdhp224  ecdhp256  ecdhp384  ecdhp521\n");
B
Bodo Möller 已提交
1108 1109 1110 1111
			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
1112

1113
#ifndef OPENSSL_NO_IDEA
1114 1115
			BIO_printf(bio_err,"idea     ");
#endif
B
Bodo Möller 已提交
1116 1117 1118
#ifndef OPENSSL_NO_SEED
			BIO_printf(bio_err,"seed     ");
#endif
1119
#ifndef OPENSSL_NO_RC2
1120 1121
			BIO_printf(bio_err,"rc2      ");
#endif
1122
#ifndef OPENSSL_NO_DES
1123
			BIO_printf(bio_err,"des      ");
1124
#endif
1125 1126 1127
#ifndef OPENSSL_NO_AES
			BIO_printf(bio_err,"aes      ");
#endif
1128 1129 1130
#ifndef OPENSSL_NO_CAMELLIA
			BIO_printf(bio_err,"camellia ");
#endif
1131
#ifndef OPENSSL_NO_RSA
1132 1133
			BIO_printf(bio_err,"rsa      ");
#endif
1134
#ifndef OPENSSL_NO_BF
1135 1136
			BIO_printf(bio_err,"blowfish");
#endif
B
Bodo Möller 已提交
1137 1138 1139 1140
#if !defined(OPENSSL_NO_IDEA) || !defined(OPENSSL_NO_SEED) || \
    !defined(OPENSSL_NO_RC2) || !defined(OPENSSL_NO_DES) || \
    !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_BF) || \
    !defined(OPENSSL_NO_AES) || !defined(OPENSSL_NO_CAMELLIA)
1141
			BIO_printf(bio_err,"\n");
1142
#endif
1143
			BIO_printf(bio_err,"prime-trial-division  prime-coprime\n");
1144

1145
			BIO_printf(bio_err,"\n");
1146
			BIO_printf(bio_err,"Available options:\n");
1147
#if defined(TIMES) || defined(USE_TOD)
1148
			BIO_printf(bio_err,"-elapsed        measure time in real time instead of CPU user time.\n");
1149
#endif
1150
#ifndef OPENSSL_NO_ENGINE
1151
			BIO_printf(bio_err,"-engine e       use engine e, possibly a hardware device.\n");
1152
#endif
1153 1154 1155
			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");
D
Dr. Stephen Henson 已提交
1156
#ifndef NO_FORK
1157
			BIO_printf(bio_err,"-multi n        run n benchmarks in parallel.\n");
1158
#endif
1159 1160 1161 1162 1163 1164 1165
			goto end;
			}
		argc--;
		argv++;
		j++;
		}

D
Dr. Stephen Henson 已提交
1166
#ifndef NO_FORK
1167 1168
	if(multi && do_multi(multi))
		goto show_res;
1169
#endif
1170

1171 1172 1173
	if (j == 0)
		{
		for (i=0; i<ALGOR_NUM; i++)
B
Bodo Möller 已提交
1174 1175 1176 1177
			{
			if (i != D_EVP)
				doit[i]=1;
			}
1178 1179 1180 1181
		for (i=0; i<RSA_NUM; i++)
			rsa_doit[i]=1;
		for (i=0; i<DSA_NUM; i++)
			dsa_doit[i]=1;
1182 1183 1184 1185 1186 1187 1188 1189
#ifndef OPENSSL_NO_ECDSA
		for (i=0; i<EC_NUM; i++)
			ecdsa_doit[i]=1;
#endif
#ifndef OPENSSL_NO_ECDH
		for (i=0; i<EC_NUM; i++)
			ecdh_doit[i]=1;
#endif
1190 1191 1192 1193
		}
	for (i=0; i<ALGOR_NUM; i++)
		if (doit[i]) pr_header++;

1194
	if (usertime == 0 && !mr)
1195
		BIO_printf(bio_err,"You have chosen to measure elapsed time instead of user CPU time.\n");
1196

1197
#ifndef OPENSSL_NO_RSA
1198 1199
	for (i=0; i<RSA_NUM; i++)
		{
1200
		const unsigned char *p;
1201 1202 1203 1204 1205 1206 1207 1208

		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;
			}
1209 1210 1211
#if 0
		else
			{
1212 1213 1214
			BIO_printf(bio_err,mr ? "+RK:%d:"
				   : "Loaded RSA key, %d bit modulus and e= 0x",
				   BN_num_bits(rsa_key[i]->n));
1215 1216 1217 1218
			BN_print(bio_err,rsa_key[i]->e);
			BIO_printf(bio_err,"\n");
			}
#endif
1219 1220 1221
		}
#endif

1222
#ifndef OPENSSL_NO_DSA
1223 1224 1225 1226 1227
	dsa_key[0]=get_dsa512();
	dsa_key[1]=get_dsa1024();
	dsa_key[2]=get_dsa2048();
#endif

1228
#ifndef OPENSSL_NO_DES
1229 1230 1231
	DES_set_key_unchecked(&key,&sch);
	DES_set_key_unchecked(&key2,&sch2);
	DES_set_key_unchecked(&key3,&sch3);
1232
#endif
1233 1234 1235 1236 1237
#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
1238 1239 1240 1241 1242
#ifndef OPENSSL_NO_CAMELLIA
	Camellia_set_key(key16,128,&camellia_ks1);
	Camellia_set_key(ckey24,192,&camellia_ks2);
	Camellia_set_key(ckey32,256,&camellia_ks3);
#endif
1243
#ifndef OPENSSL_NO_IDEA
1244 1245
	idea_set_encrypt_key(key16,&idea_ks);
#endif
B
Bodo Möller 已提交
1246 1247 1248
#ifndef OPENSSL_NO_SEED
	SEED_set_key(key16,&seed_ks);
#endif
1249
#ifndef OPENSSL_NO_RC4
1250 1251
	RC4_set_key(&rc4_ks,16,key16);
#endif
1252
#ifndef OPENSSL_NO_RC2
1253 1254
	RC2_set_key(&rc2_ks,16,key16,128);
#endif
1255
#ifndef OPENSSL_NO_RC5
1256 1257
	RC5_32_set_key(&rc5_ks,16,key16,12);
#endif
1258
#ifndef OPENSSL_NO_BF
1259 1260
	BF_set_key(&bf_ks,16,key16);
#endif
1261
#ifndef OPENSSL_NO_CAST
1262 1263
	CAST_set_key(&cast_ks,16,key16);
#endif
1264
#ifndef OPENSSL_NO_RSA
1265
	memset(rsa_c,0,sizeof(rsa_c));
1266
#endif
1267
#ifndef SIGALRM
1268
#ifndef OPENSSL_NO_DES
1269 1270 1271
	BIO_printf(bio_err,"First we calculate the approximate speed ...\n");
	count=10;
	do	{
1272
		long it;
1273
		count*=2;
D
Dr. Stephen Henson 已提交
1274
		Time_F(START);
1275
		for (it=count; it; it--)
1276 1277
			DES_ecb_encrypt((DES_cblock *)buf,
				(DES_cblock *)buf,
1278
				&sch,DES_ENCRYPT);
1279
		d=Time_F(STOP);
1280
		} while (d <3);
1281
	save_count=count;
1282 1283
	c[D_MD2][0]=count/10;
	c[D_MDC2][0]=count/10;
1284
	c[D_MD4][0]=count;
1285
	c[D_MD5][0]=count;
1286
	c[D_HMAC][0]=count;
1287
	c[D_SHA1][0]=count;
1288
	c[D_RMD160][0]=count;
1289 1290 1291 1292
	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;
B
Bodo Möller 已提交
1293
	c[D_CBC_SEED][0]=count;
1294
	c[D_CBC_RC2][0]=count;
1295
	c[D_CBC_RC5][0]=count;
1296
	c[D_CBC_BF][0]=count;
1297
	c[D_CBC_CAST][0]=count;
1298 1299 1300
	c[D_CBC_128_AES][0]=count;
	c[D_CBC_192_AES][0]=count;
	c[D_CBC_256_AES][0]=count;
1301 1302 1303
	c[D_CBC_128_CML][0]=count;
	c[D_CBC_192_CML][0]=count;
	c[D_CBC_256_CML][0]=count;
A
Andy Polyakov 已提交
1304 1305
	c[D_SHA256][0]=count;
	c[D_SHA512][0]=count;
1306
	c[D_WHIRLPOOL][0]=count;
B
Ben Laurie 已提交
1307 1308 1309
	c[D_IGE_128_AES][0]=count;
	c[D_IGE_192_AES][0]=count;
	c[D_IGE_256_AES][0]=count;
1310
	c[D_GHASH][0]=count;
1311 1312 1313 1314 1315

	for (i=1; i<SIZE_NUM; i++)
		{
		long l0,l1;

1316
		l0=(long)lengths[0];
1317
		l1=(long)lengths[i];
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
		
		c[D_MD2][i]=c[D_MD2][0]*4*l0/l1;
		c[D_MDC2][i]=c[D_MDC2][0]*4*l0/l1;
		c[D_MD4][i]=c[D_MD4][0]*4*l0/l1;
		c[D_MD5][i]=c[D_MD5][0]*4*l0/l1;
		c[D_HMAC][i]=c[D_HMAC][0]*4*l0/l1;
		c[D_SHA1][i]=c[D_SHA1][0]*4*l0/l1;
		c[D_RMD160][i]=c[D_RMD160][0]*4*l0/l1;
		c[D_SHA256][i]=c[D_SHA256][0]*4*l0/l1;
		c[D_SHA512][i]=c[D_SHA512][0]*4*l0/l1;
		c[D_WHIRLPOOL][i]=c[D_WHIRLPOOL][0]*4*l0/l1;

		l0=(long)lengths[i-1];
		
1332 1333 1334 1335
		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;
B
Bodo Möller 已提交
1336
		c[D_CBC_SEED][i]=c[D_CBC_SEED][i-1]*l0/l1;
1337
		c[D_CBC_RC2][i]=c[D_CBC_RC2][i-1]*l0/l1;
1338
		c[D_CBC_RC5][i]=c[D_CBC_RC5][i-1]*l0/l1;
1339
		c[D_CBC_BF][i]=c[D_CBC_BF][i-1]*l0/l1;
1340
		c[D_CBC_CAST][i]=c[D_CBC_CAST][i-1]*l0/l1;
1341 1342 1343
		c[D_CBC_128_AES][i]=c[D_CBC_128_AES][i-1]*l0/l1;
		c[D_CBC_192_AES][i]=c[D_CBC_192_AES][i-1]*l0/l1;
		c[D_CBC_256_AES][i]=c[D_CBC_256_AES][i-1]*l0/l1;
1344 1345 1346
 		c[D_CBC_128_CML][i]=c[D_CBC_128_CML][i-1]*l0/l1;
		c[D_CBC_192_CML][i]=c[D_CBC_192_CML][i-1]*l0/l1;
		c[D_CBC_256_CML][i]=c[D_CBC_256_CML][i-1]*l0/l1;
B
Ben Laurie 已提交
1347 1348 1349
		c[D_IGE_128_AES][i]=c[D_IGE_128_AES][i-1]*l0/l1;
		c[D_IGE_192_AES][i]=c[D_IGE_192_AES][i-1]*l0/l1;
		c[D_IGE_256_AES][i]=c[D_IGE_256_AES][i-1]*l0/l1;
1350
		}
1351 1352 1353 1354
		
	prime_c[D_PRIME_TRIAL_DIVISION]=count;
	prime_c[D_PRIME_COPRIME]=count;
	
1355
#ifndef OPENSSL_NO_RSA
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
	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
			{
1366
			if (rsa_c[i][0] == 0)
1367 1368 1369 1370 1371 1372
				{
				rsa_c[i][0]=1;
				rsa_c[i][1]=20;
				}
			}				
		}
1373
#endif
1374

1375
#ifndef OPENSSL_NO_DSA
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
	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;
				}
			}				
		}
1393
#endif
1394

B
Bodo Möller 已提交
1395 1396 1397
#ifndef OPENSSL_NO_ECDSA
	ecdsa_c[R_EC_P160][0]=count/1000;
	ecdsa_c[R_EC_P160][1]=count/1000/2;
1398
	for (i=R_EC_P192; i<=R_EC_P521; i++)
B
Bodo Möller 已提交
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
		{
		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;
1452
	for (i=R_EC_P192; i<=R_EC_P521; i++)
B
Bodo Möller 已提交
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
		{
		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

1503
#define COND(d)	(count < (d))
1504 1505
#define COUNT(d) (d)
#else
B
Bodo Möller 已提交
1506 1507
/* not worth fixing */
# error "You cannot disable DES on systems without SIGALRM."
1508
#endif /* OPENSSL_NO_DES */
B
Bodo Möller 已提交
1509
#else
1510
#define COND(c)	(run && count<0x7fffffff)
1511
#define COUNT(d) (count)
1512
#ifndef _WIN32
1513
	signal(SIGALRM,sig_done);
1514
#endif
B
Bodo Möller 已提交
1515
#endif /* SIGALRM */
1516

1517
#ifndef OPENSSL_NO_MD2
1518 1519 1520 1521 1522
	if (doit[D_MD2])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_MD2],c[D_MD2][j],lengths[j]);
1523
			Time_F(START);
1524
			for (count=0,run=1; COND(c[D_MD2][j]); count++)
1525 1526 1527
				EVP_Digest(buf,(unsigned long)lengths[j],&(md2[0]),NULL,EVP_md2(),NULL);
			d=Time_F(STOP);
			print_result(D_MD2,j,count,d);
1528 1529 1530
			}
		}
#endif
1531
#ifndef OPENSSL_NO_MDC2
1532 1533 1534 1535 1536
	if (doit[D_MDC2])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_MDC2],c[D_MDC2][j],lengths[j]);
1537
			Time_F(START);
1538
			for (count=0,run=1; COND(c[D_MDC2][j]); count++)
1539 1540 1541
				EVP_Digest(buf,(unsigned long)lengths[j],&(mdc2[0]),NULL,EVP_mdc2(),NULL);
			d=Time_F(STOP);
			print_result(D_MDC2,j,count,d);
1542 1543 1544 1545
			}
		}
#endif

1546
#ifndef OPENSSL_NO_MD4
1547 1548 1549 1550 1551
	if (doit[D_MD4])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_MD4],c[D_MD4][j],lengths[j]);
1552
			Time_F(START);
1553
			for (count=0,run=1; COND(c[D_MD4][j]); count++)
1554 1555 1556
				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);
1557 1558 1559 1560
			}
		}
#endif

1561
#ifndef OPENSSL_NO_MD5
1562 1563 1564 1565 1566
	if (doit[D_MD5])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_MD5],c[D_MD5][j],lengths[j]);
1567
			Time_F(START);
1568
			for (count=0,run=1; COND(c[D_MD5][j]); count++)
1569
				MD5(buf,lengths[j],md5);
1570 1571
			d=Time_F(STOP);
			print_result(D_MD5,j,count,d);
1572 1573 1574 1575
			}
		}
#endif

1576
#if !defined(OPENSSL_NO_MD5) && !defined(OPENSSL_NO_HMAC)
1577
	if (doit[D_HMAC])
1578
		{
1579
		HMAC_CTX hctx;
1580 1581

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

1585 1586
		for (j=0; j<SIZE_NUM; j++)
			{
1587
			print_message(names[D_HMAC],c[D_HMAC][j],lengths[j]);
1588
			Time_F(START);
1589 1590
			for (count=0,run=1; COND(c[D_HMAC][j]); count++)
				{
D
 
Dr. Stephen Henson 已提交
1591 1592 1593
				HMAC_Init_ex(&hctx,NULL,0,NULL,NULL);
				HMAC_Update(&hctx,buf,lengths[j]);
				HMAC_Final(&hctx,&(hmac[0]),NULL);
1594
				}
1595 1596
			d=Time_F(STOP);
			print_result(D_HMAC,j,count,d);
1597
			}
1598
		HMAC_CTX_cleanup(&hctx);
1599 1600
		}
#endif
1601
#ifndef OPENSSL_NO_SHA
1602 1603 1604 1605 1606
	if (doit[D_SHA1])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_SHA1],c[D_SHA1][j],lengths[j]);
1607
			Time_F(START);
1608
			for (count=0,run=1; COND(c[D_SHA1][j]); count++)
1609
#if 0
1610
				EVP_Digest(buf,(unsigned long)lengths[j],&(sha[0]),NULL,EVP_sha1(),NULL);
1611 1612 1613
#else
				SHA1(buf,lengths[j],sha);
#endif
1614 1615
			d=Time_F(STOP);
			print_result(D_SHA1,j,count,d);
1616 1617
			}
		}
A
Andy Polyakov 已提交
1618

1619
#ifndef OPENSSL_NO_SHA256
A
Andy Polyakov 已提交
1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
	if (doit[D_SHA256])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_SHA256],c[D_SHA256][j],lengths[j]);
			Time_F(START);
			for (count=0,run=1; COND(c[D_SHA256][j]); count++)
				SHA256(buf,lengths[j],sha256);
			d=Time_F(STOP);
			print_result(D_SHA256,j,count,d);
			}
		}
1632
#endif
A
Andy Polyakov 已提交
1633

1634
#ifndef OPENSSL_NO_SHA512
A
Andy Polyakov 已提交
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
	if (doit[D_SHA512])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_SHA512],c[D_SHA512][j],lengths[j]);
			Time_F(START);
			for (count=0,run=1; COND(c[D_SHA512][j]); count++)
				SHA512(buf,lengths[j],sha512);
			d=Time_F(STOP);
			print_result(D_SHA512,j,count,d);
			}
		}
1647
#endif
1648
#endif
1649

1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662
#ifndef OPENSSL_NO_WHIRLPOOL
	if (doit[D_WHIRLPOOL])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_WHIRLPOOL],c[D_WHIRLPOOL][j],lengths[j]);
			Time_F(START);
			for (count=0,run=1; COND(c[D_WHIRLPOOL][j]); count++)
				WHIRLPOOL(buf,lengths[j],whirlpool);
			d=Time_F(STOP);
			print_result(D_WHIRLPOOL,j,count,d);
			}
		}
1663
#endif
1664

1665
#ifndef OPENSSL_NO_RIPEMD
1666 1667 1668 1669 1670
	if (doit[D_RMD160])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_RMD160],c[D_RMD160][j],lengths[j]);
1671
			Time_F(START);
1672
			for (count=0,run=1; COND(c[D_RMD160][j]); count++)
1673 1674 1675
				EVP_Digest(buf,(unsigned long)lengths[j],&(rmd160[0]),NULL,EVP_ripemd160(),NULL);
			d=Time_F(STOP);
			print_result(D_RMD160,j,count,d);
1676 1677 1678
			}
		}
#endif
1679
#ifndef OPENSSL_NO_RC4
1680 1681 1682 1683 1684
	if (doit[D_RC4])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_RC4],c[D_RC4][j],lengths[j]);
1685
			Time_F(START);
1686 1687 1688
			for (count=0,run=1; COND(c[D_RC4][j]); count++)
				RC4(&rc4_ks,(unsigned int)lengths[j],
					buf,buf);
1689 1690
			d=Time_F(STOP);
			print_result(D_RC4,j,count,d);
1691 1692 1693
			}
		}
#endif
1694
#ifndef OPENSSL_NO_DES
1695 1696 1697 1698 1699
	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]);
1700
			Time_F(START);
1701
			for (count=0,run=1; COND(c[D_CBC_DES][j]); count++)
1702
				DES_ncbc_encrypt(buf,buf,lengths[j],&sch,
1703
						 &DES_iv,DES_ENCRYPT);
1704 1705
			d=Time_F(STOP);
			print_result(D_CBC_DES,j,count,d);
1706 1707 1708 1709 1710 1711 1712 1713
			}
		}

	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]);
1714
			Time_F(START);
1715
			for (count=0,run=1; COND(c[D_EDE3_DES][j]); count++)
1716
				DES_ede3_cbc_encrypt(buf,buf,lengths[j],
1717
						     &sch,&sch2,&sch3,
1718
						     &DES_iv,DES_ENCRYPT);
1719 1720
			d=Time_F(STOP);
			print_result(D_EDE3_DES,j,count,d);
1721 1722 1723
			}
		}
#endif
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
#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);
			}
		}

B
Ben Laurie 已提交
1768 1769 1770 1771 1772 1773 1774
	if (doit[D_IGE_128_AES])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_IGE_128_AES],c[D_IGE_128_AES][j],lengths[j]);
			Time_F(START);
			for (count=0,run=1; COND(c[D_IGE_128_AES][j]); count++)
B
Ben Laurie 已提交
1775
				AES_ige_encrypt(buf,buf2,
B
Ben Laurie 已提交
1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788
					(unsigned long)lengths[j],&aes_ks1,
					iv,AES_ENCRYPT);
			d=Time_F(STOP);
			print_result(D_IGE_128_AES,j,count,d);
			}
		}
	if (doit[D_IGE_192_AES])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_IGE_192_AES],c[D_IGE_192_AES][j],lengths[j]);
			Time_F(START);
			for (count=0,run=1; COND(c[D_IGE_192_AES][j]); count++)
B
Ben Laurie 已提交
1789
				AES_ige_encrypt(buf,buf2,
B
Ben Laurie 已提交
1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
					(unsigned long)lengths[j],&aes_ks2,
					iv,AES_ENCRYPT);
			d=Time_F(STOP);
			print_result(D_IGE_192_AES,j,count,d);
			}
		}
	if (doit[D_IGE_256_AES])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_IGE_256_AES],c[D_IGE_256_AES][j],lengths[j]);
			Time_F(START);
			for (count=0,run=1; COND(c[D_IGE_256_AES][j]); count++)
B
Ben Laurie 已提交
1803
				AES_ige_encrypt(buf,buf2,
B
Ben Laurie 已提交
1804 1805 1806 1807 1808 1809
					(unsigned long)lengths[j],&aes_ks3,
					iv,AES_ENCRYPT);
			d=Time_F(STOP);
			print_result(D_IGE_256_AES,j,count,d);
			}
		}
1810 1811 1812
	if (doit[D_GHASH])
		{
		GCM128_CONTEXT *ctx = CRYPTO_gcm128_new(&aes_ks1,(block128_f)AES_encrypt);
B
Ben Laurie 已提交
1813
		CRYPTO_gcm128_setiv (ctx,(unsigned char *)"0123456789ab",12);
B
Ben Laurie 已提交
1814

1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_GHASH],c[D_GHASH][j],lengths[j]);
			Time_F(START);
			for (count=0,run=1; COND(c[D_GHASH][j]); count++)
				CRYPTO_gcm128_aad(ctx,buf,lengths[j]);
			d=Time_F(STOP);
			print_result(D_GHASH,j,count,d);
			}
		CRYPTO_gcm128_release(ctx);
		}
B
Ben Laurie 已提交
1826

1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
#endif
#ifndef OPENSSL_NO_CAMELLIA
	if (doit[D_CBC_128_CML])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_CBC_128_CML],c[D_CBC_128_CML][j],lengths[j]);
			Time_F(START);
			for (count=0,run=1; COND(c[D_CBC_128_CML][j]); count++)
				Camellia_cbc_encrypt(buf,buf,
				        (unsigned long)lengths[j],&camellia_ks1,
				        iv,CAMELLIA_ENCRYPT);
			d=Time_F(STOP);
			print_result(D_CBC_128_CML,j,count,d);
			}
		}
	if (doit[D_CBC_192_CML])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_CBC_192_CML],c[D_CBC_192_CML][j],lengths[j]);
			Time_F(START);
			for (count=0,run=1; COND(c[D_CBC_192_CML][j]); count++)
				Camellia_cbc_encrypt(buf,buf,
				        (unsigned long)lengths[j],&camellia_ks2,
				        iv,CAMELLIA_ENCRYPT);
			d=Time_F(STOP);
			print_result(D_CBC_192_CML,j,count,d);
			}
		}
	if (doit[D_CBC_256_CML])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_CBC_256_CML],c[D_CBC_256_CML][j],lengths[j]);
			Time_F(START);
			for (count=0,run=1; COND(c[D_CBC_256_CML][j]); count++)
				Camellia_cbc_encrypt(buf,buf,
				        (unsigned long)lengths[j],&camellia_ks3,
				        iv,CAMELLIA_ENCRYPT);
			d=Time_F(STOP);
			print_result(D_CBC_256_CML,j,count,d);
			}
		}

1872
#endif
1873
#ifndef OPENSSL_NO_IDEA
1874 1875 1876 1877 1878
	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]);
1879
			Time_F(START);
1880 1881 1882
			for (count=0,run=1; COND(c[D_CBC_IDEA][j]); count++)
				idea_cbc_encrypt(buf,buf,
					(unsigned long)lengths[j],&idea_ks,
1883
					iv,IDEA_ENCRYPT);
1884 1885
			d=Time_F(STOP);
			print_result(D_CBC_IDEA,j,count,d);
1886 1887 1888
			}
		}
#endif
B
Bodo Möller 已提交
1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903
#ifndef OPENSSL_NO_SEED
	if (doit[D_CBC_SEED])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
			print_message(names[D_CBC_SEED],c[D_CBC_SEED][j],lengths[j]);
			Time_F(START);
			for (count=0,run=1; COND(c[D_CBC_SEED][j]); count++)
				SEED_cbc_encrypt(buf,buf,
					(unsigned long)lengths[j],&seed_ks,iv,1);
			d=Time_F(STOP);
			print_result(D_CBC_SEED,j,count,d);
			}
		}
#endif
1904
#ifndef OPENSSL_NO_RC2
1905 1906 1907 1908 1909
	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]);
1910
			Time_F(START);
1911 1912 1913
			for (count=0,run=1; COND(c[D_CBC_RC2][j]); count++)
				RC2_cbc_encrypt(buf,buf,
					(unsigned long)lengths[j],&rc2_ks,
1914
					iv,RC2_ENCRYPT);
1915 1916
			d=Time_F(STOP);
			print_result(D_CBC_RC2,j,count,d);
1917 1918 1919
			}
		}
#endif
1920
#ifndef OPENSSL_NO_RC5
1921 1922 1923 1924 1925
	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]);
1926
			Time_F(START);
1927 1928 1929
			for (count=0,run=1; COND(c[D_CBC_RC5][j]); count++)
				RC5_32_cbc_encrypt(buf,buf,
					(unsigned long)lengths[j],&rc5_ks,
1930
					iv,RC5_ENCRYPT);
1931 1932
			d=Time_F(STOP);
			print_result(D_CBC_RC5,j,count,d);
1933 1934 1935
			}
		}
#endif
1936
#ifndef OPENSSL_NO_BF
1937 1938 1939 1940 1941
	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]);
1942
			Time_F(START);
1943 1944 1945
			for (count=0,run=1; COND(c[D_CBC_BF][j]); count++)
				BF_cbc_encrypt(buf,buf,
					(unsigned long)lengths[j],&bf_ks,
1946
					iv,BF_ENCRYPT);
1947 1948
			d=Time_F(STOP);
			print_result(D_CBC_BF,j,count,d);
1949 1950 1951
			}
		}
#endif
1952
#ifndef OPENSSL_NO_CAST
1953 1954 1955 1956 1957
	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]);
1958
			Time_F(START);
1959 1960 1961
			for (count=0,run=1; COND(c[D_CBC_CAST][j]); count++)
				CAST_cbc_encrypt(buf,buf,
					(unsigned long)lengths[j],&cast_ks,
1962
					iv,CAST_ENCRYPT);
1963 1964
			d=Time_F(STOP);
			print_result(D_CBC_CAST,j,count,d);
1965 1966 1967
			}
		}
#endif
1968

1969 1970 1971 1972
	if (doit[D_EVP])
		{
		for (j=0; j<SIZE_NUM; j++)
			{
1973 1974 1975 1976 1977 1978
			if (evp_cipher)
				{
				EVP_CIPHER_CTX ctx;
				int outl;

				names[D_EVP]=OBJ_nid2ln(evp_cipher->nid);
1979 1980 1981
				/* -O3 -fschedule-insns messes up an
				 * optimization here!  names[D_EVP]
				 * somehow becomes NULL */
1982 1983 1984 1985 1986 1987 1988 1989
				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);
1990
				EVP_CIPHER_CTX_set_padding(&ctx, 0);
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003

				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);
D
Dr. Stephen Henson 已提交
2004
				EVP_CIPHER_CTX_cleanup(&ctx);
2005 2006 2007 2008 2009 2010 2011 2012
				}
			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 已提交
2013
				for (count=0,run=1; COND(save_count*4*lengths[0]/lengths[j]); count++)
2014 2015 2016 2017
					EVP_Digest(buf,lengths[j],&(md[0]),NULL,evp_md,NULL);

				d=Time_F(STOP);
				}
2018
			print_result(D_EVP,j,count,d);
2019 2020
			}
		}
2021 2022
	
	if (prime_doit[D_PRIME_TRIAL_DIVISION])
2023 2024 2025 2026 2027
		{
		BIGNUM *rnd = BN_new();
		BIGNUM *add = BN_new();
		BN_CTX *ctx = BN_CTX_new();
		
2028 2029 2030
		BN_set_word(add, 2);
		prime_print_message(prime_names[D_PRIME_TRIAL_DIVISION],
							prime_c[D_PRIME_TRIAL_DIVISION]);
2031
			
2032 2033 2034 2035 2036 2037
		Time_F(START);
		for (count=0, run=1; COND(prime_c[D_PRIME_TRIAL_DIVISION]); count++)
			bn_probable_prime_dh(rnd, 1024, add, NULL, ctx);
		
		d=Time_F(STOP);
		prime_print_result(D_PRIME_TRIAL_DIVISION, count, d);
2038 2039 2040 2041
		
		BN_CTX_free(ctx);
		BN_free(add);
		BN_free(rnd);
2042
		
2043
		}
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
	
	if (prime_doit[D_PRIME_COPRIME])
		{
		BIGNUM *rnd = BN_new();
		BIGNUM *add = BN_new();
		BN_CTX *ctx = BN_CTX_new();
		
		BN_set_word(add, 2);
		prime_print_message(prime_names[D_PRIME_COPRIME],
							prime_c[D_PRIME_COPRIME]);
			
		Time_F(START);
		for (count=0, run=1; COND(prime_c[D_PRIME_COPRIME]); count++)
2057
			bn_probable_prime_dh_coprime_safe(rnd, 1024, add, NULL, ctx);
2058 2059 2060 2061 2062 2063 2064 2065 2066
		
		d=Time_F(STOP);
		prime_print_result(D_PRIME_COPRIME, count, d);
		
		BN_CTX_free(ctx);
		BN_free(add);
		BN_free(rnd);
		
		}
2067

2068
	RAND_pseudo_bytes(buf,36);
2069
#ifndef OPENSSL_NO_RSA
2070 2071
	for (j=0; j<RSA_NUM; j++)
		{
D
 
Dr. Stephen Henson 已提交
2072
		int ret;
2073
		if (!rsa_doit[j]) continue;
D
 
Dr. Stephen Henson 已提交
2074
		ret=RSA_sign(NID_md5_sha1, buf,36, buf2, &rsa_num, rsa_key[j]);
2075 2076 2077 2078 2079 2080 2081
		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
2082
			{
2083 2084 2085 2086
			pkey_print_message("private","rsa",
				rsa_c[j][0],rsa_bits[j],
				RSA_SECONDS);
/*			RSA_blinding_on(rsa_key[j],NULL); */
2087
			Time_F(START);
2088
			for (count=0,run=1; COND(rsa_c[j][0]); count++)
2089
				{
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
				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;
					}
2100
				}
2101 2102 2103 2104
			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);
2105 2106
			rsa_results[j][0]=d/(double)count;
			rsa_count=count;
2107 2108
			}

2109
#if 1
D
 
Dr. Stephen Henson 已提交
2110
		ret=RSA_verify(NID_md5_sha1, buf,36, buf2, rsa_num, rsa_key[j]);
2111
		if (ret <= 0)
2112
			{
2113 2114
			BIO_printf(bio_err,"RSA verify failure.  No RSA verify will be done.\n");
			ERR_print_errors(bio_err);
2115
			rsa_doit[j] = 0;
2116 2117 2118 2119 2120 2121
			}
		else
			{
			pkey_print_message("public","rsa",
				rsa_c[j][1],rsa_bits[j],
				RSA_SECONDS);
2122
			Time_F(START);
2123
			for (count=0,run=1; COND(rsa_c[j][1]); count++)
2124
				{
2125 2126
				ret=RSA_verify(NID_md5_sha1, buf,36, buf2,
					rsa_num, rsa_key[j]);
D
Dr. Stephen Henson 已提交
2127
				if (ret <= 0)
2128 2129 2130 2131 2132 2133 2134
					{
					BIO_printf(bio_err,
						"RSA verify failure\n");
					ERR_print_errors(bio_err);
					count=1;
					break;
					}
2135
				}
2136 2137 2138 2139
			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);
2140
			rsa_results[j][1]=d/(double)count;
2141
			}
2142
#endif
2143 2144 2145 2146 2147 2148 2149 2150 2151 2152

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

2153
	RAND_pseudo_bytes(buf,20);
2154
#ifndef OPENSSL_NO_DSA
2155 2156 2157 2158 2159
	if (RAND_status() != 1)
		{
		RAND_seed(rnd_seed, sizeof rnd_seed);
		rnd_fake = 1;
		}
2160 2161
	for (j=0; j<DSA_NUM; j++)
		{
2162
		unsigned int kk;
2163
		int ret;
2164

2165
		if (!dsa_doit[j]) continue;
2166
/*		DSA_generate_key(dsa_key[j]); */
2167
/*		DSA_sign_setup(dsa_key[j],NULL); */
2168
		ret=DSA_sign(EVP_PKEY_DSA,buf,20,buf2,
2169
			&kk,dsa_key[j]);
2170 2171 2172 2173 2174 2175 2176
		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
2177
			{
2178 2179 2180
			pkey_print_message("sign","dsa",
				dsa_c[j][0],dsa_bits[j],
				DSA_SECONDS);
2181
			Time_F(START);
2182
			for (count=0,run=1; COND(dsa_c[j][0]); count++)
2183
				{
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193
				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;
					}
2194
				}
2195 2196 2197 2198
			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);
2199 2200
			dsa_results[j][0]=d/(double)count;
			rsa_count=count;
2201 2202
			}

2203
		ret=DSA_verify(EVP_PKEY_DSA,buf,20,buf2,
2204
			kk,dsa_key[j]);
2205 2206 2207 2208 2209 2210 2211
		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
2212
			{
2213 2214 2215
			pkey_print_message("verify","dsa",
				dsa_c[j][1],dsa_bits[j],
				DSA_SECONDS);
2216
			Time_F(START);
2217
			for (count=0,run=1; COND(dsa_c[j][1]); count++)
2218
				{
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228
				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;
					}
2229
				}
2230 2231 2232 2233
			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);
2234
			dsa_results[j][1]=d/(double)count;
2235 2236 2237 2238 2239 2240 2241 2242 2243
			}

		if (rsa_count <= 1)
			{
			/* if longer than 10s, don't do any more */
			for (j++; j<DSA_NUM; j++)
				dsa_doit[j]=0;
			}
		}
2244
	if (rnd_fake) RAND_cleanup();
2245
#endif
B
Bodo Möller 已提交
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257

#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 */ 
N
Nils Larsch 已提交
2258
		ecdsa[j] = EC_KEY_new_by_curve_name(test_curves[j]);
B
Bodo Möller 已提交
2259 2260 2261 2262 2263 2264 2265 2266
		if (ecdsa[j] == NULL) 
			{
			BIO_printf(bio_err,"ECDSA failure.\n");
			ERR_print_errors(bio_err);
			rsa_count=1;
			} 
		else 
			{
N
Nils Larsch 已提交
2267 2268 2269 2270 2271 2272 2273 2274
#if 1
			EC_KEY_precompute_mult(ecdsa[j], NULL);
#endif
			/* Perform ECDSA signature test */
			EC_KEY_generate_key(ecdsa[j]);
			ret = ECDSA_sign(0, buf, 20, ecdsasig, 
				&ecdsasiglen, ecdsa[j]);
			if (ret == 0) 
B
Bodo Möller 已提交
2275
				{
N
Nils Larsch 已提交
2276
				BIO_printf(bio_err,"ECDSA sign failure.  No ECDSA sign will be done.\n");
B
Bodo Möller 已提交
2277 2278 2279 2280 2281
				ERR_print_errors(bio_err);
				rsa_count=1;
				} 
			else 
				{
N
Nils Larsch 已提交
2282 2283 2284 2285 2286 2287 2288 2289
				pkey_print_message("sign","ecdsa",
					ecdsa_c[j][0], 
					test_curves_bits[j],
					ECDSA_SECONDS);

				Time_F(START);
				for (count=0,run=1; COND(ecdsa_c[j][0]);
					count++) 
B
Bodo Möller 已提交
2290
					{
N
Nils Larsch 已提交
2291 2292 2293 2294
					ret=ECDSA_sign(0, buf, 20, 
						ecdsasig, &ecdsasiglen,
						ecdsa[j]);
					if (ret == 0) 
B
Bodo Möller 已提交
2295
						{
N
Nils Larsch 已提交
2296 2297 2298 2299
						BIO_printf(bio_err, "ECDSA sign failure\n");
						ERR_print_errors(bio_err);
						count=1;
						break;
B
Bodo Möller 已提交
2300 2301
						}
					}
N
Nils Larsch 已提交
2302
				d=Time_F(STOP);
B
Bodo Möller 已提交
2303

N
Nils Larsch 已提交
2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327
				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 */
			ret=ECDSA_verify(0, buf, 20, ecdsasig, 
				ecdsasiglen, ecdsa[j]);
			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++) 
B
Bodo Möller 已提交
2328
					{
N
Nils Larsch 已提交
2329 2330
					ret=ECDSA_verify(0, buf, 20, ecdsasig, ecdsasiglen, ecdsa[j]);
					if (ret != 1) 
B
Bodo Möller 已提交
2331
						{
N
Nils Larsch 已提交
2332 2333 2334 2335
						BIO_printf(bio_err, "ECDSA verify failure\n");
						ERR_print_errors(bio_err);
						count=1;
						break;
B
Bodo Möller 已提交
2336 2337
						}
					}
N
Nils Larsch 已提交
2338 2339 2340 2341 2342 2343
				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;
				}
B
Bodo Möller 已提交
2344

N
Nils Larsch 已提交
2345 2346 2347 2348 2349
			if (rsa_count <= 1) 
				{
				/* if longer than 10s, don't do any more */
				for (j++; j<EC_NUM; j++)
				ecdsa_doit[j]=0;
B
Bodo Möller 已提交
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364
				}
			}
		}
	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;
N
Nils Larsch 已提交
2365 2366
		ecdh_a[j] = EC_KEY_new_by_curve_name(test_curves[j]);
		ecdh_b[j] = EC_KEY_new_by_curve_name(test_curves[j]);
B
Bodo Möller 已提交
2367 2368 2369 2370 2371 2372 2373 2374
		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
			{
N
Nils Larsch 已提交
2375 2376 2377
			/* generate two ECDH key pairs */
			if (!EC_KEY_generate_key(ecdh_a[j]) ||
				!EC_KEY_generate_key(ecdh_b[j]))
B
Bodo Möller 已提交
2378
				{
N
Nils Larsch 已提交
2379
				BIO_printf(bio_err,"ECDH key generation failure.\n");
B
Bodo Möller 已提交
2380
				ERR_print_errors(bio_err);
N
Nils Larsch 已提交
2381
				rsa_count=1;		
B
Bodo Möller 已提交
2382 2383 2384
				}
			else
				{
N
Nils Larsch 已提交
2385 2386 2387 2388 2389 2390 2391
				/* If field size is not more than 24 octets, then use SHA-1 hash of result;
				 * otherwise, use result (see section 4.8 of draft-ietf-tls-ecc-03.txt).
				 */
				int field_size, outlen;
				void *(*kdf)(const void *in, size_t inlen, void *out, size_t *xoutlen);
				field_size = EC_GROUP_get_degree(EC_KEY_get0_group(ecdh_a[j]));
				if (field_size <= 24 * 8)
B
Bodo Möller 已提交
2392
					{
N
Nils Larsch 已提交
2393 2394
					outlen = KDF1_SHA1_len;
					kdf = KDF1_SHA1;
B
Bodo Möller 已提交
2395 2396 2397
					}
				else
					{
N
Nils Larsch 已提交
2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410
					outlen = (field_size+7)/8;
					kdf = NULL;
					}
				secret_size_a = ECDH_compute_key(secret_a, outlen,
					EC_KEY_get0_public_key(ecdh_b[j]),
					ecdh_a[j], kdf);
				secret_size_b = ECDH_compute_key(secret_b, outlen,
					EC_KEY_get0_public_key(ecdh_a[j]),
					ecdh_b[j], kdf);
				if (secret_size_a != secret_size_b) 
					ecdh_checks = 0;
				else
					ecdh_checks = 1;
B
Bodo Möller 已提交
2411

N
Nils Larsch 已提交
2412 2413 2414 2415 2416 2417 2418 2419
				for (secret_idx = 0; 
				    (secret_idx < secret_size_a)
					&& (ecdh_checks == 1);
				    secret_idx++)
					{
					if (secret_a[secret_idx] != secret_b[secret_idx])
					ecdh_checks = 0;
					}
B
Bodo Möller 已提交
2420

N
Nils Larsch 已提交
2421 2422 2423 2424 2425
				if (ecdh_checks == 0)
					{
					BIO_printf(bio_err,"ECDH computations don't match.\n");
					ERR_print_errors(bio_err);
					rsa_count=1;		
B
Bodo Möller 已提交
2426
					}
N
Nils Larsch 已提交
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443

				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, outlen,
					EC_KEY_get0_public_key(ecdh_b[j]),
					ecdh_a[j], kdf);
					}
				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;
B
Bodo Möller 已提交
2444 2445 2446
				}
			}

N
Nils Larsch 已提交
2447

B
Bodo Möller 已提交
2448 2449 2450 2451 2452 2453 2454 2455 2456
		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 已提交
2457
#ifndef NO_FORK
2458
show_res:
D
Dr. Stephen Henson 已提交
2459
#endif
2460 2461 2462
	if(!mr)
		{
		fprintf(stdout,"%s\n",SSLeay_version(SSLEAY_VERSION));
2463
        fprintf(stdout,"%s\n",SSLeay_version(SSLEAY_BUILT_ON));
2464 2465
		printf("options:");
		printf("%s ",BN_options());
2466
#ifndef OPENSSL_NO_MD2
2467
		printf("%s ",MD2_options());
2468
#endif
2469
#ifndef OPENSSL_NO_RC4
2470
		printf("%s ",RC4_options());
2471
#endif
2472
#ifndef OPENSSL_NO_DES
2473
		printf("%s ",DES_options());
2474
#endif
2475 2476 2477
#ifndef OPENSSL_NO_AES
		printf("%s ",AES_options());
#endif
2478
#ifndef OPENSSL_NO_IDEA
2479
		printf("%s ",idea_options());
2480
#endif
2481
#ifndef OPENSSL_NO_BF
2482
		printf("%s ",BF_options());
2483
#endif
2484 2485
		fprintf(stdout,"\n%s\n",SSLeay_version(SSLEAY_CFLAGS));
		}
2486 2487 2488

	if (pr_header)
		{
2489 2490 2491 2492 2493 2494 2495
		if(mr)
			fprintf(stdout,"+H");
		else
			{
			fprintf(stdout,"The 'numbers' are in 1000s of bytes per second processed.\n"); 
			fprintf(stdout,"type        ");
			}
2496
		for (j=0;  j<SIZE_NUM; j++)
2497
			fprintf(stdout,mr ? ":%d" : "%7d bytes",lengths[j]);
2498 2499 2500 2501 2502 2503
		fprintf(stdout,"\n");
		}

	for (k=0; k<ALGOR_NUM; k++)
		{
		if (!doit[k]) continue;
2504 2505 2506 2507
		if(mr)
			fprintf(stdout,"+F:%d:%s",k,names[k]);
		else
			fprintf(stdout,"%-13s",names[k]);
2508 2509
		for (j=0; j<SIZE_NUM; j++)
			{
2510
			if (results[k][j] > 10000 && !mr)
2511 2512
				fprintf(stdout," %11.2fk",results[k][j]/1e3);
			else
2513
				fprintf(stdout,mr ? ":%.2f" : " %11.2f ",results[k][j]);
2514 2515 2516
			}
		fprintf(stdout,"\n");
		}
2517
#ifndef OPENSSL_NO_RSA
2518 2519 2520 2521
	j=1;
	for (k=0; k<RSA_NUM; k++)
		{
		if (!rsa_doit[k]) continue;
2522
		if (j && !mr)
2523 2524 2525 2526
			{
			printf("%18ssign    verify    sign/s verify/s\n"," ");
			j=0;
			}
2527 2528 2529 2530 2531
		if(mr)
			fprintf(stdout,"+F2:%u:%u:%f:%f\n",
				k,rsa_bits[k],rsa_results[k][0],
				rsa_results[k][1]);
		else
2532
			fprintf(stdout,"rsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2533 2534
				rsa_bits[k],rsa_results[k][0],rsa_results[k][1],
				1.0/rsa_results[k][0],1.0/rsa_results[k][1]);
2535 2536
		}
#endif
2537
#ifndef OPENSSL_NO_DSA
2538 2539 2540 2541
	j=1;
	for (k=0; k<DSA_NUM; k++)
		{
		if (!dsa_doit[k]) continue;
2542 2543
		if (j && !mr)
			{
2544 2545 2546
			printf("%18ssign    verify    sign/s verify/s\n"," ");
			j=0;
			}
2547 2548 2549 2550
		if(mr)
			fprintf(stdout,"+F3:%u:%u:%f:%f\n",
				k,dsa_bits[k],dsa_results[k][0],dsa_results[k][1]);
		else
2551
			fprintf(stdout,"dsa %4u bits %8.6fs %8.6fs %8.1f %8.1f\n",
2552 2553
				dsa_bits[k],dsa_results[k][0],dsa_results[k][1],
				1.0/dsa_results[k][0],1.0/dsa_results[k][1]);
2554 2555
		}
#endif
B
Bodo Möller 已提交
2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604
#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 已提交
2605
	mret=0;
B
Bodo Möller 已提交
2606

2607
end:
2608
	ERR_print_errors(bio_err);
2609 2610
	if (buf != NULL) OPENSSL_free(buf);
	if (buf2 != NULL) OPENSSL_free(buf2);
2611
#ifndef OPENSSL_NO_RSA
2612 2613 2614 2615
	for (i=0; i<RSA_NUM; i++)
		if (rsa_key[i] != NULL)
			RSA_free(rsa_key[i]);
#endif
2616
#ifndef OPENSSL_NO_DSA
2617 2618 2619 2620
	for (i=0; i<DSA_NUM; i++)
		if (dsa_key[i] != NULL)
			DSA_free(dsa_key[i]);
#endif
B
Bodo Möller 已提交
2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636

#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

2637
	apps_shutdown();
2638
	OPENSSL_EXIT(mret);
2639 2640
	}

2641
static void print_message(const char *s, long num, int length)
2642 2643
	{
#ifdef SIGALRM
2644 2645
	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 已提交
2646
	(void)BIO_flush(bio_err);
2647 2648
	alarm(SECONDS);
#else
2649 2650
	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 已提交
2651
	(void)BIO_flush(bio_err);
2652 2653 2654 2655 2656 2657
#endif
#ifdef LINT
	num=num;
#endif
	}

2658 2659 2660 2661
static void prime_print_message(const char *s, long num)
	{
#ifdef SIGALRM
	BIO_printf(bio_err,mr ? "+DT:%s:%d\n"
2662
		   : "Doing %s for %ds: ", s, PRIME_SECONDS);
2663
	(void)BIO_flush(bio_err);
2664
	alarm(PRIME_SECONDS);
2665 2666 2667 2668 2669 2670 2671 2672 2673 2674
#else
	BIO_printf(bio_err,mr ? "+DN:%s:%ld\n"
		   : "Doing %s %ld times: ", s, num);
	(void)BIO_flush(bio_err);
#endif
#ifdef LINT
	num=num;
#endif
	}

2675 2676
static void pkey_print_message(const char *str, const char *str2, long num,
	int bits, int tm)
2677 2678
	{
#ifdef SIGALRM
2679 2680
	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 已提交
2681
	(void)BIO_flush(bio_err);
2682
	alarm(tm);
2683
#else
2684 2685
	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 已提交
2686
	(void)BIO_flush(bio_err);
2687 2688 2689 2690 2691
#endif
#ifdef LINT
	num=num;
#endif
	}
2692

2693 2694
static void print_result(int alg,int run_no,int count,double time_used)
	{
2695 2696
	BIO_printf(bio_err,mr ? "+R:%d:%s:%f\n"
		   : "%d %s's in %.2fs\n",count,names[alg],time_used);
2697 2698 2699
	results[alg][run_no]=((double)count)/time_used*lengths[run_no];
	}

2700 2701 2702
static void prime_print_result(int alg, int count, double time_used)
	{
	BIO_printf(bio_err,
2703
			   mr ? "+R:%d:%s:%f:%f\n" : "%d %s's in %.2fs (%.2f microseconds / run)\n",
2704
			   count, prime_names[alg], time_used,
2705
			   time_used / ((double)count) * 1000000);
2706 2707
	}

D
Dr. Stephen Henson 已提交
2708
#ifndef NO_FORK
2709 2710 2711 2712 2713 2714 2715 2716
static char *sstrsep(char **string, const char *delim)
    {
    char isdelim[256];
    char *token = *string;

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

2717
    memset(isdelim, 0, sizeof isdelim);
2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749
    isdelim[0] = 1;

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

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

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

    return token;
    }

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)
		{
2750 2751 2752 2753 2754
		if (pipe(fd) == -1)
			{
			fprintf(stderr, "pipe failure\n");
			exit(1);
			}
2755 2756
		fflush(stdout);
		fflush(stderr);
2757 2758 2759 2760 2761 2762 2763 2764 2765
		if(fork())
			{
			close(fd[1]);
			fds[n]=fd[0];
			}
		else
			{
			close(fd[0]);
			close(1);
2766 2767 2768 2769 2770
			if (dup(fd[1]) == -1)
				{
				fprintf(stderr, "dup failed\n");
				exit(1);
				}
2771 2772 2773
			close(fd[1]);
			mr=1;
			usertime=0;
B
Ben Laurie 已提交
2774
			free(fds);
2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852
			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;
				}
D
Dr. Stephen Henson 已提交
2853
#ifndef OPENSSL_NO_DSA
2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874
			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;
				}
D
Dr. Stephen Henson 已提交
2875
#endif
B
Bodo Möller 已提交
2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918
#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

2919 2920 2921 2922 2923 2924
			else if(!strncmp(buf,"+H:",3))
				{
				}
			else
				fprintf(stderr,"Unknown type '%s' from child %d\n",buf,n);
			}
B
Ben Laurie 已提交
2925 2926

		fclose(f);
2927
		}
2928
	free(fds);
2929 2930
	return 1;
	}
2931
#endif
2932
#endif