From 8baf9968dfd8ef2bc20cf2bf3de09304eb2213c5 Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Sun, 13 Dec 2015 16:03:02 +0100 Subject: Make EVP_CIPHER_CTX opaque and renew the creator / destructor functions Following the method used for EVP_MD_CTX and HMAC_CTX, EVP_CIPHER_CTX_init and EVP_CIPHER_CTX_cleanup are joined together into one function, EVP_CIPHER_CTX_reset, with EVP_CIPHER_CTX_init kept as an alias. EVP_CIPHER_CTX_cleanup fills no purpose of its own any more and is therefore removed. Reviewed-by: Rich Salz --- crypto/evp/evp_enc.c | 64 +++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 36 deletions(-) (limited to 'crypto/evp/evp_enc.c') diff --git a/crypto/evp/evp_enc.c b/crypto/evp/evp_enc.c index 6523bf16cc..45237545a8 100644 --- a/crypto/evp/evp_enc.c +++ b/crypto/evp/evp_enc.c @@ -66,17 +66,39 @@ #endif #include "evp_locl.h" -void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx) +int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *c) { - memset(ctx, 0, sizeof(*ctx)); + if (c == NULL) + return 1; + if (c->cipher != NULL) { + if (c->cipher->cleanup && !c->cipher->cleanup(c)) + return 0; + /* Cleanse cipher context data */ + if (c->cipher_data && c->cipher->ctx_size) + OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size); + } + OPENSSL_free(c->cipher_data); +#ifndef OPENSSL_NO_ENGINE + if (c->engine) + /* + * The EVP_CIPHER we used belongs to an ENGINE, release the + * functional reference we held for this reason. + */ + ENGINE_finish(c->engine); +#endif + memset(c, 0, sizeof(*c)); + return 1; } EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) { - EVP_CIPHER_CTX *ctx = OPENSSL_malloc(sizeof(*ctx)); - if (ctx != NULL) - EVP_CIPHER_CTX_init(ctx); - return ctx; + return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX)); +} + +void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) +{ + EVP_CIPHER_CTX_reset(ctx); + OPENSSL_free(ctx); } int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher, @@ -515,36 +537,6 @@ int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) return (1); } -void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) -{ - EVP_CIPHER_CTX_cleanup(ctx); - OPENSSL_free(ctx); -} - -int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c) -{ - if (!c) - return 0; - if (c->cipher != NULL) { - if (c->cipher->cleanup && !c->cipher->cleanup(c)) - return 0; - /* Cleanse cipher context data */ - if (c->cipher_data && c->cipher->ctx_size) - OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size); - } - OPENSSL_free(c->cipher_data); -#ifndef OPENSSL_NO_ENGINE - if (c->engine) - /* - * The EVP_CIPHER we used belongs to an ENGINE, release the - * functional reference we held for this reason. - */ - ENGINE_finish(c->engine); -#endif - memset(c, 0, sizeof(*c)); - return 1; -} - int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen) { if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH) -- cgit v1.2.3