From d1806a523cbe866ff2b3a0ae94562106921e85d5 Mon Sep 17 00:00:00 2001 From: Dirkjan Bussink Date: Sat, 12 Dec 2020 17:23:07 +0100 Subject: [PATCH] Remove OPENSSL_zalloc helper This function is not needed, because in each case it is used, we follow it up immediately with an initialization function call. This means that the zeroing here is unneeded, since the initialization already guarantees things end up in the right state. It also swaps the reset call with a simpler init call, also because reset is implemented as init with a return value that is always 1. That means the more complex logic is not needed at all. Signed-off-by: Dirkjan Bussink Reviewed-by: Jakub Jelen --- src/libcrypto-compat.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/libcrypto-compat.c b/src/libcrypto-compat.c index d9947379..a42b3073 100644 --- a/src/libcrypto-compat.c +++ b/src/libcrypto-compat.c @@ -16,15 +16,6 @@ #include #endif -static void *OPENSSL_zalloc(size_t num) -{ - void *ret = OPENSSL_malloc(num); - - if (ret != NULL) - memset(ret, 0, num); - return ret; -} - int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) { /* If the fields n and e in r are NULL, the corresponding input @@ -236,7 +227,7 @@ int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) EVP_MD_CTX *EVP_MD_CTX_new(void) { - EVP_MD_CTX *ctx = OPENSSL_zalloc(sizeof(EVP_MD_CTX)); + EVP_MD_CTX *ctx = OPENSSL_malloc(sizeof(EVP_MD_CTX)); if (ctx != NULL) { EVP_MD_CTX_init(ctx); } @@ -292,13 +283,10 @@ int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx) HMAC_CTX *HMAC_CTX_new(void) { - HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX)); + HMAC_CTX *ctx = OPENSSL_malloc(sizeof(HMAC_CTX)); if (ctx != NULL) { - if (!HMAC_CTX_reset(ctx)) { - HMAC_CTX_free(ctx); - return NULL; - } + HMAC_CTX_init(ctx); } return ctx; } @@ -335,7 +323,11 @@ int HMAC_CTX_reset(HMAC_CTX *ctx) #ifndef HAVE_OPENSSL_EVP_CIPHER_CTX_NEW EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) { - return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX)); + EVP_CIPHER_CTX *ctx = OPENSSL_malloc(sizeof(EVP_CIPHER_CTX)); + if (ctx != NULL) { + EVP_CIPHER_CTX_init(ctx); + } + return ctx; } void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)