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