Make ciphers and digests obtain an ENGINE functional reference
[openssl.git] / crypto / evp / evp_enc.c
index 3fbe5c6527736d6fc046bd4d1295c752385e112b..22a7b745c174ac3d68bd36698da6d8cf57fa82f5 100644 (file)
@@ -73,11 +73,15 @@ void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx)
        /* ctx->cipher=NULL; */
        }
 
+
 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
             const unsigned char *key, const unsigned char *iv, int enc)
        {
+       if (cipher)
+               EVP_CIPHER_CTX_init(ctx);
        return EVP_CipherInit_ex(ctx,cipher,NULL,key,iv,enc);
        }
+
 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
             const unsigned char *key, const unsigned char *iv, int enc)
        {
@@ -96,7 +100,15 @@ int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *imp
                 * ENGINE and EVP_CIPHER could be used). */
                if(ctx->engine)
                        ENGINE_finish(ctx->engine);
-               if(!impl)
+               if(impl)
+                       {
+                       if (!ENGINE_init(impl))
+                               {
+                               EVPerr(EVP_F_EVP_CIPHERINIT, EVP_R_INITIALIZATION_ERROR);
+                               return 0;
+                               }
+                       }
+               else
                        /* Ask if an ENGINE is reserved for this job */
                        impl = ENGINE_get_cipher_engine(cipher->nid);
                if(impl)
@@ -187,6 +199,13 @@ int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
        else    return EVP_DecryptUpdate(ctx,out,outl,in,inl);
        }
 
+int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
+       {
+       if (ctx->encrypt)
+               return EVP_EncryptFinal_ex(ctx,out,outl);
+       else    return EVP_DecryptFinal_ex(ctx,out,outl);
+       }
+
 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
        {
        if (ctx->encrypt)
@@ -200,10 +219,22 @@ int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
        return EVP_CipherInit(ctx, cipher, key, iv, 1);
        }
 
+int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx,const EVP_CIPHER *cipher, ENGINE *impl,
+               const unsigned char *key, const unsigned char *iv)
+       {
+       return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
+       }
+
 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
             const unsigned char *key, const unsigned char *iv)
        {
-       return EVP_CipherInit(ctx, cipher, key, iv, 0);
+       return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 0);
+       }
+
+int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, ENGINE *impl,
+            const unsigned char *key, const unsigned char *iv)
+       {
+       return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
        }
 
 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
@@ -263,6 +294,14 @@ int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
        }
 
 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
+       {
+       int ret;
+       ret = EVP_EncryptFinal_ex(ctx, out, outl);
+       EVP_CIPHER_CTX_cleanup(ctx);
+       return ret;
+       }
+
+int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
        {
        int i,n,b,bl,ret;
 
@@ -346,6 +385,14 @@ int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
        }
 
 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
+       {
+       int ret;
+       ret = EVP_DecryptFinal_ex(ctx, out, outl);
+       EVP_CIPHER_CTX_cleanup(ctx);
+       return ret;
+       }
+
+int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
        {
        int i,b;
        int n;