Skip to content

Commit

Permalink
Fix potential memory leak for do_hash
Browse files Browse the repository at this point in the history
Signed-off-by: Songling Han <[email protected]>
  • Loading branch information
songlingatpan committed Sep 22, 2024
1 parent 0c2345b commit 7f6ea1b
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/common/sha2/sha2_ossl.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@
#include "../ossl_helpers.h"

static void do_hash(uint8_t *output, const uint8_t *input, size_t inplen, const EVP_MD *md) {
EVP_MD_CTX *mdctx;
EVP_MD_CTX *mdctx = NULL;
unsigned int outlen;

mdctx = OSSL_FUNC(EVP_MD_CTX_new)();
OQS_EXIT_IF_NULLPTR(mdctx, "OpenSSL");
OQS_OPENSSL_GUARD(OSSL_FUNC(EVP_DigestInit_ex)(mdctx, md, NULL));
OQS_OPENSSL_GUARD(OSSL_FUNC(EVP_DigestUpdate)(mdctx, input, inplen));
OQS_OPENSSL_GUARD(OSSL_FUNC(EVP_DigestFinal_ex)(mdctx, output, &outlen));
if (mdctx == NULL) {
OQS_EXIT_IF_NULLPTR(mdctx, "OpenSSL");
return;
}

if (OSSL_FUNC(EVP_DigestInit_ex)(mdctx, md, NULL) != 1 ||
OSSL_FUNC(EVP_DigestUpdate)(mdctx, input, inplen) != 1 ||
OSSL_FUNC(EVP_DigestFinal_ex)(mdctx, output, &outlen) != 1) {
OSSL_FUNC(EVP_MD_CTX_free)(mdctx);
OQS_EXIT_IF_NULLPTR(NULL, "OpenSSL digest operation failed");
return;
}

OSSL_FUNC(EVP_MD_CTX_free)(mdctx);
}

Expand Down

0 comments on commit 7f6ea1b

Please sign in to comment.