Remove some unnecessary recursive includes from the internal apps.h header,
[openssl.git] / apps / speed.c
index 1d83adccfab6898f8303c19e6d53097a696f07c6..4008930454ace278ed4fb058a9b0f236eb40c590 100644 (file)
@@ -71,6 +71,8 @@
 
 /* most of this code has been pilfered from my libdes speed.c program */
 
+#ifndef OPENSSL_NO_SPEED
+
 #undef SECONDS
 #define SECONDS                3       
 #define RSA_SECONDS    10
@@ -86,7 +88,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <signal.h>
+
 #include <string.h>
 #include <math.h>
 #include "apps.h"
 #include OPENSSL_UNISTD
 #endif
 
+#ifndef OPENSSL_SYS_NETWARE
+#include <signal.h>
+#endif
+
 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(OPENSSL_SYS_MACOSX)
 # define USE_TOD
 #elif !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VXWORKS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC))
 # define TIMEB
 #endif
 
+#if defined(OPENSSL_SYS_NETWARE)
+#undef TIMES
+#undef TIMEB
+#include <time.h>
+#endif
+
 #ifndef _IRIX
 # include <time.h>
 #endif
 #include <sys/timeb.h>
 #endif
 
-#if !defined(TIMES) && !defined(TIMEB) && !defined(USE_TOD) && !defined(OPENSSL_SYS_VXWORKS)
+#if !defined(TIMES) && !defined(TIMEB) && !defined(USE_TOD) && !defined(OPENSSL_SYS_VXWORKS) && !defined(OPENSSL_SYS_NETWARE)
 #error "It seems neither struct tms nor struct timeb is supported in this platform!"
 #endif
 
 #include <sys/param.h>
 #endif
 
+#include <openssl/bn.h>
 #ifndef OPENSSL_NO_DES
 #include <openssl/des.h>
 #endif
 #endif
 #include <openssl/x509.h>
 #ifndef OPENSSL_NO_DSA
+#include <openssl/dsa.h>
 #include "./testdsa.h"
 #endif
 #ifndef OPENSSL_NO_ECDSA
 #include <openssl/ecdh.h>
 #endif
 
+/*
+ * The following "HZ" timing stuff should be sync'd up with the code in
+ * crypto/tmdiff.[ch]. That appears to try to do the same job, though I think
+ * this code is more up to date than libcrypto's so there may be features to
+ * migrate over first. This is used in two places further down AFAICS. 
+ * The point is that nothing in openssl actually *uses* that tmdiff stuff, so
+ * either speed.c should be using it or it should go because it's obviously not
+ * useful enough. Anyone want to do a janitorial job on this?
+ */
+
 /* The following if from times(3) man page.  It may need to be changed */
 #ifndef HZ
-# ifdef _SC_CLK_TCK
-#  define HZ ((double)sysconf(_SC_CLK_TCK))
+# if defined(_SC_CLK_TCK) \
+     && (!defined(OPENSSL_SYS_VMS) || __CTRL_VER >= 70000000)
+#  define HZ sysconf(_SC_CLK_TCK)
 # else
 #  ifndef CLK_TCK
 #   ifndef _BSD_CLK_TCK_ /* FreeBSD hack */
 # endif
 #endif
 
-#if !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(OPENSSL_SYS_OS2)
+#if !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_WINDOWS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(OPENSSL_SYS_OS2) && !defined(OPENSSL_SYS_NETWARE)
 # define HAVE_FORK 1
 #endif
 
@@ -285,13 +310,39 @@ static SIGRETTYPE sig_done(int sig)
 #define START  0
 #define STOP   1
 
+#if defined(OPENSSL_SYS_NETWARE)
+
+   /* for NetWare the best we can do is use clock() which returns the
+    * time, in hundredths of a second, since the NLM began executing
+   */
+static double Time_F(int s)
+       {
+       double ret;
+
+   static clock_t tstart,tend;
+
+   if (s == START)
+   {
+      tstart=clock();
+      return(0);
+   }
+   else
+   {
+      tend=clock();
+      ret=(double)((double)(tend)-(double)(tstart));
+      return((ret < 0.001)?0.001:ret);
+   }
+   }
+
+#else
+
 static double Time_F(int s)
        {
        double ret;
 
 #ifdef USE_TOD
        if(usertime)
-           {
+               {
                static struct rusage tstart,tend;
 
                getrusage_used = 1;
@@ -346,7 +397,8 @@ static double Time_F(int s)
                else
                        {
                        times(&tend);
-                       ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
+                       ret = HZ;
+                       ret=(double)(tend.tms_utime-tstart.tms_utime) / ret;
                        return((ret < 1e-3)?1e-3:ret);
                        }
                }
@@ -392,12 +444,29 @@ static double Time_F(int s)
 # endif
 #endif
        }
+#endif /* if defined(OPENSSL_SYS_NETWARE) */
+
+
+static const int KDF1_SHA1_len = 20;
+static void *KDF1_SHA1(void *in, size_t inlen, void *out, size_t outlen)
+       {
+#ifndef OPENSSL_NO_SHA
+       if (outlen != SHA_DIGEST_LENGTH)
+               return NULL;
+       return SHA1(in, inlen, out);
+#else
+       return NULL;
+#endif
+       }
+
 
 int MAIN(int, char **);
 
 int MAIN(int argc, char **argv)
        {
+#ifndef OPENSSL_NO_ENGINE
        ENGINE *e = NULL;
+#endif
        unsigned char *buf=NULL,*buf2=NULL;
        int mret=1;
        long count=0,save_count=0;
@@ -730,6 +799,7 @@ int MAIN(int argc, char **argv)
                        j--;    /* Otherwise, -elapsed gets confused with
                                   an algorithm. */
                        }
+#ifndef OPENSSL_NO_ENGINE
                else if ((argc > 0) && (strcmp(*argv,"-engine") == 0))
                        {
                        argc--;
@@ -746,6 +816,7 @@ int MAIN(int argc, char **argv)
                           means all of them should be run) */
                        j--;
                        }
+#endif
 #ifdef HAVE_FORK
                else if ((argc > 0) && (strcmp(*argv,"-multi") == 0))
                        {
@@ -907,6 +978,7 @@ int MAIN(int argc, char **argv)
                        {
                        dsa_doit[R_DSA_512]=1;
                        dsa_doit[R_DSA_1024]=1;
+                       dsa_doit[R_DSA_2048]=1;
                        }
                else
 #endif
@@ -1063,7 +1135,9 @@ int MAIN(int argc, char **argv)
 #if defined(TIMES) || defined(USE_TOD)
                        BIO_printf(bio_err,"-elapsed        measure time in real time instead of CPU user time.\n");
 #endif
+#ifndef OPENSSL_NO_ENGINE
                        BIO_printf(bio_err,"-engine e       use engine e, possibly a hardware device.\n");
+#endif
                        BIO_printf(bio_err,"-evp e          use EVP e.\n");
                        BIO_printf(bio_err,"-decrypt        time decryption instead of encryption (only EVP).\n");
                        BIO_printf(bio_err,"-mr             produce machine readable output.\n");
@@ -1196,6 +1270,9 @@ int MAIN(int argc, char **argv)
        c[D_CBC_RC5][0]=count;
        c[D_CBC_BF][0]=count;
        c[D_CBC_CAST][0]=count;
+       c[D_CBC_128_AES][0]=count;
+       c[D_CBC_192_AES][0]=count;
+       c[D_CBC_256_AES][0]=count;
 
        for (i=1; i<SIZE_NUM; i++)
                {
@@ -1221,6 +1298,9 @@ int MAIN(int argc, char **argv)
                c[D_CBC_RC5][i]=c[D_CBC_RC5][i-1]*l0/l1;
                c[D_CBC_BF][i]=c[D_CBC_BF][i-1]*l0/l1;
                c[D_CBC_CAST][i]=c[D_CBC_CAST][i-1]*l0/l1;
+               c[D_CBC_128_AES][i]=c[D_CBC_128_AES][i-1]*l0/l1;
+               c[D_CBC_192_AES][i]=c[D_CBC_192_AES][i-1]*l0/l1;
+               c[D_CBC_256_AES][i]=c[D_CBC_256_AES][i-1]*l0/l1;
                }
 #ifndef OPENSSL_NO_RSA
        rsa_c[R_RSA_512][0]=count/2000;
@@ -1699,6 +1779,7 @@ int MAIN(int argc, char **argv)
                                else
                                        EVP_EncryptFinal_ex(&ctx,buf,&outl);
                                d=Time_F(STOP);
+                               EVP_CIPHER_CTX_cleanup(&ctx);
                                }
                        if (evp_md)
                                {
@@ -1925,6 +2006,9 @@ int MAIN(int argc, char **argv)
                                } 
                        else 
                                {
+#if 1
+                               EC_GROUP_precompute_mult(ecdsa[j]->group, NULL);
+#endif
                                /* Perform ECDSA signature test */
                                EC_KEY_generate_key(ecdsa[j]);
                                ret = ECDSA_sign(0, buf, 20, ecdsasig, 
@@ -2040,7 +2124,7 @@ int MAIN(int argc, char **argv)
                                }
                        else
                                {
-                               ecdh_b[j]->group = ecdh_a[j]->group;
+                               ecdh_b[j]->group = EC_GROUP_dup(ecdh_a[j]->group);
 
                                /* generate two ECDH key pairs */
                                if (!EC_KEY_generate_key(ecdh_a[j]) ||
@@ -2052,12 +2136,28 @@ int MAIN(int argc, char **argv)
                                        }
                                else
                                        {
-                                       secret_size_a = ECDH_compute_key(secret_a, 
+                                       /* If field size is not more than 24 octets, then use SHA-1 hash of result;
+                                        * otherwise, use result (see section 4.8 of draft-ietf-tls-ecc-03.txt).
+                                        */
+                                       int field_size, outlen;
+                                       void *(*kdf)(void *in, size_t inlen, void *out, size_t xoutlen);
+                                       field_size = EC_GROUP_get_degree(ecdh_a[j]->group);
+                                       if (field_size <= 24 * 8)
+                                               {
+                                               outlen = KDF1_SHA1_len;
+                                               kdf = KDF1_SHA1;
+                                               }
+                                       else
+                                               {
+                                               outlen = (field_size+7)/8;
+                                               kdf = NULL;
+                                               }
+                                       secret_size_a = ECDH_compute_key(secret_a, outlen,
                                                ecdh_b[j]->pub_key,
-                                               ecdh_a[j]);
-                                       secret_size_b = ECDH_compute_key(secret_b, 
+                                               ecdh_a[j], kdf);
+                                       secret_size_b = ECDH_compute_key(secret_b, outlen,
                                                ecdh_a[j]->pub_key,
-                                               ecdh_b[j]);
+                                               ecdh_b[j], kdf);
                                        if (secret_size_a != secret_size_b) 
                                                ecdh_checks = 0;
                                        else
@@ -2086,9 +2186,9 @@ int MAIN(int argc, char **argv)
                                        Time_F(START);
                                        for (count=0,run=1; COND(ecdh_c[j][0]); count++)
                                                {
-                                               ECDH_compute_key(secret_a, 
+                                               ECDH_compute_key(secret_a, outlen,
                                                ecdh_b[j]->pub_key,
-                                               ecdh_a[j]);
+                                               ecdh_a[j], kdf);
                                                }
                                        d=Time_F(STOP);
                                        BIO_printf(bio_err, mr ? "+R7:%ld:%d:%.2f\n" :"%ld %d-bit ECDH ops in %.2fs\n",
@@ -2148,7 +2248,10 @@ show_res:
 #endif
 #ifdef HZ
 #define as_string(s) (#s)
-               printf("HZ=%g", (double)HZ);
+               {
+               double dbl = HZ;
+               printf("HZ=%g", dbl);
+               }
 # ifdef _SC_CLK_TCK
                printf(" [sysconf value]");
 # endif
@@ -2315,7 +2418,7 @@ end:
 #endif
 
        apps_shutdown();
-       EXIT(mret);
+       OPENSSL_EXIT(mret);
        }
 
 static void print_message(const char *s, long num, int length)
@@ -2355,8 +2458,8 @@ static void pkey_print_message(char *str, char *str2, long num, int bits,
 
 static void print_result(int alg,int run_no,int count,double time_used)
        {
-       BIO_printf(bio_err,mr ? "+R:%ld:%s:%f\n"
-                  : "%ld %s's in %.2fs\n",count,names[alg],time_used);
+       BIO_printf(bio_err,mr ? "+R:%d:%s:%f\n"
+                  : "%d %s's in %.2fs\n",count,names[alg],time_used);
        results[alg][run_no]=((double)count)/time_used*lengths[run_no];
        }
 
@@ -2568,3 +2671,4 @@ static int do_multi(int multi)
        return 1;
        }
 #endif
+#endif