Revert "RT3425: constant-time evp_enc"
[oweals/openssl.git] / crypto / evp / openbsd_hw.c
index b4ac72dbcbfe9e87ae1c5a1647b46cf79fb396a5..e5252e2e937a6accbc602d601872ce2df0ae0fab 100644 (file)
  * OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <openssl/evp.h>
+#include <openssl/objects.h>
+#include <openssl/rsa.h>
+#include "evp_locl.h"
+
+/* This stuff should now all be supported through
+ * crypto/engine/hw_openbsd_dev_crypto.c unless I botched it up */
+static void *dummy=&dummy;
+
+#if 0
+
+/* check flag after OpenSSL headers to ensure make depend works */
+#ifdef OPENSSL_OPENBSD_DEV_CRYPTO
+
 #include <fcntl.h>
 #include <stdio.h>
 #include <errno.h>
 #include <sys/ioctl.h>
 #include <crypto/cryptodev.h>
 #include <unistd.h>
-#include <openssl/evp.h>
-#include <openssl/objects.h>
-#include <openssl/rsa.h>
-#include "evp_locl.h"
 #include <assert.h>
 
-/* check flag after headers to ensure make depend works */
-#ifdef OPENSSL_OPENBSD_DEV_CRYPTO
-
 /* longest key supported in hardware */
 #define MAX_HW_KEY     24
 #define MAX_HW_IV      8
@@ -127,6 +134,8 @@ static int dev_crypto_init_key(EVP_CIPHER_CTX *ctx,int cipher,
        return 0;
 
     CDATA(ctx)->key=OPENSSL_malloc(MAX_HW_KEY);
+    if (CDATA(ctx)->key == NULL)
+        return 0;
 
     assert(ctx->cipher->iv_len <= MAX_HW_IV);
 
@@ -184,6 +193,8 @@ static int dev_crypto_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
            if(((unsigned long)in&3) || cinl != inl)
                {
                cin=OPENSSL_malloc(cinl);
+               if (cin == NULL)
+                   return 0;
                memcpy(cin,in,inl);
                cryp.src=cin;
                }
@@ -191,6 +202,12 @@ static int dev_crypto_cipher(EVP_CIPHER_CTX *ctx,unsigned char *out,
            if(((unsigned long)out&3) || cinl != inl)
                {
                cout=OPENSSL_malloc(cinl);
+               if (cout == NULL)
+                   {
+                   if (cin != NULL)
+                       OPENSSL_free(cin);
+                   return 0;
+                   }
                cryp.dst=cout;
                }
 
@@ -290,6 +307,17 @@ static int dev_crypto_init_digest(MD_DATA *md_data,int mac)
     return 1;
     }
 
+static int dev_crypto_cleanup_digest(MD_DATA *md_data)
+    {
+    if (ioctl(fd,CIOCFSESSION,&md_data->sess.ses) == -1)
+       {
+       err("CIOCFSESSION failed");
+       return 0;
+       }
+
+    return 1;
+    }
+
 /* FIXME: if device can do chained MACs, then don't accumulate */
 /* FIXME: move accumulation to the framework */
 static int dev_crypto_md5_init(EVP_MD_CTX *ctx)
@@ -298,7 +326,7 @@ static int dev_crypto_md5_init(EVP_MD_CTX *ctx)
 static int do_digest(int ses,unsigned char *md,const void *data,int len)
     {
     struct crypt_op cryp;
-    static unsigned char md5zero[16]=
+    static const unsigned char md5zero[16]=
        {
        0xd4,0x1d,0x8c,0xd9,0x8f,0x00,0xb2,0x04,
        0xe9,0x80,0x09,0x98,0xec,0xf8,0x42,0x7e
@@ -345,7 +373,7 @@ static int do_digest(int ses,unsigned char *md,const void *data,int len)
            return 0;
            }
        }
-    printf("done\n");
+    //    printf("done\n");
 
     return 1;
     }
@@ -354,11 +382,15 @@ static int dev_crypto_md5_update(EVP_MD_CTX *ctx,const void *data,
                                 unsigned long len)
     {
     MD_DATA *md_data=ctx->md_data;
+    char *tmp_md_data;
 
     if(ctx->flags&EVP_MD_CTX_FLAG_ONESHOT)
        return do_digest(md_data->sess.ses,md_data->md,data,len);
 
-    md_data->data=OPENSSL_realloc(md_data->data,md_data->len+len);
+    tmp_md_data=OPENSSL_realloc(md_data->data,md_data->len+len);
+    if (tmp_md_data == NULL)
+       return 0;
+    md_data->data=tmp_md_data;
     memcpy(md_data->data+md_data->len,data,len);
     md_data->len+=len;
 
@@ -373,13 +405,15 @@ static int dev_crypto_md5_final(EVP_MD_CTX *ctx,unsigned char *md)
     if(ctx->flags&EVP_MD_CTX_FLAG_ONESHOT)
        {
        memcpy(md,md_data->md,MD5_DIGEST_LENGTH);
-       return 1;
+       ret=1;
+       }
+    else
+       {
+       ret=do_digest(md_data->sess.ses,md,md_data->data,md_data->len);
+       OPENSSL_free(md_data->data);
+       md_data->data=NULL;
+       md_data->len=0;
        }
-
-    ret=do_digest(md_data->sess.ses,md,md_data->data,md_data->len);
-    OPENSSL_free(md_data->data);
-    md_data->data=NULL;
-    md_data->len=0;
 
     return ret;
     }
@@ -393,11 +427,18 @@ static int dev_crypto_md5_copy(EVP_MD_CTX *to,const EVP_MD_CTX *from)
     assert(from->digest->flags&EVP_MD_FLAG_ONESHOT);
 
     to_md->data=OPENSSL_malloc(from_md->len);
+    if (to_md->data == NULL)
+       return 0;
     memcpy(to_md->data,from_md->data,from_md->len);
 
     return 1;
     }
 
+static int dev_crypto_md5_cleanup(EVP_MD_CTX *ctx)
+    {
+    return dev_crypto_cleanup_digest(ctx->md_data);
+    }
+
 static const EVP_MD md5_md=
     {
     NID_md5,
@@ -408,6 +449,7 @@ static const EVP_MD md5_md=
     dev_crypto_md5_update,
     dev_crypto_md5_final,
     dev_crypto_md5_copy,
+    dev_crypto_md5_cleanup,
     EVP_PKEY_RSA_method,
     MD5_CBLOCK,
     sizeof(MD_DATA),
@@ -417,3 +459,4 @@ const EVP_MD *EVP_dev_crypto_md5(void)
     { return &md5_md; }
 
 #endif
+#endif