Reduce header interdependencies, initially in engine.h (the rest of the
[openssl.git] / crypto / evp / evp_enc.c
index be0758a87965feeb7b0c0494c3002213cd5571e9..c49520028214243af4faf8fb36dbd4f565d5b490 100644 (file)
@@ -60,6 +60,7 @@
 #include "cryptlib.h"
 #include <openssl/evp.h>
 #include <openssl/err.h>
+#include <openssl/rand.h>
 #ifndef OPENSSL_NO_ENGINE
 #include <openssl/engine.h>
 #endif
@@ -148,7 +149,19 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *imp
 #endif
 
                ctx->cipher=cipher;
-               ctx->cipher_data=OPENSSL_malloc(ctx->cipher->ctx_size);
+               if (ctx->cipher->ctx_size)
+                       {
+                       ctx->cipher_data=OPENSSL_malloc(ctx->cipher->ctx_size);
+                       if (!ctx->cipher_data)
+                               {
+                               EVPerr(EVP_F_EVP_CIPHERINIT, ERR_R_MALLOC_FAILURE);
+                               return 0;
+                               }
+                       }
+               else
+                       {
+                       ctx->cipher_data = NULL;
+                       }
                ctx->key_len = cipher->key_len;
                ctx->flags = 0;
                if(ctx->cipher->flags & EVP_CIPH_CTRL_INIT)
@@ -187,7 +200,8 @@ skip_to_init:
 
                        case EVP_CIPH_CBC_MODE:
 
-                       OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof ctx->iv);
+                       OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <=
+                                       (int)sizeof(ctx->iv));
                        if(iv) memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
                        memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
                        break;
@@ -274,7 +288,7 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
                }
        i=ctx->buf_len;
        bl=ctx->cipher->block_size;
-       OPENSSL_assert(bl <= sizeof ctx->buf);
+       OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
        if (i != 0)
                {
                if (i+inl < bl)
@@ -320,7 +334,8 @@ int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
 
 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
        {
-       int i,n,b,bl,ret;
+       int n,ret;
+       unsigned int i, b, bl;
 
        b=ctx->cipher->block_size;
        OPENSSL_assert(b <= sizeof ctx->buf);
@@ -356,7 +371,8 @@ int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
             const unsigned char *in, int inl)
        {
-       int b, fix_len;
+       int fix_len;
+       unsigned int b;
 
        if (inl == 0)
                {
@@ -409,8 +425,8 @@ int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
 
 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
        {
-       int i,b;
-       int n;
+       int i,n;
+       unsigned int b;
 
        *outl=0;
        b=ctx->cipher->block_size;
@@ -433,7 +449,7 @@ int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
                        }
                OPENSSL_assert(b <= sizeof ctx->final);
                n=ctx->final[b-1];
-               if (n > b)
+               if (n > (int)b)
                        {
                        EVPerr(EVP_F_EVP_DECRYPTFINAL,EVP_R_BAD_DECRYPT);
                        return(0);
@@ -519,3 +535,13 @@ int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
        }
        return ret;
 }
+
+int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
+       {
+       if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
+               return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
+       if (RAND_bytes(key, ctx->key_len) <= 0)
+               return 0;
+       return 1;
+       }
+