From edbee2a663454b7bd1f56222fee78bde94954694 Mon Sep 17 00:00:00 2001 From: Pauli Date: Tue, 2 Sep 2025 08:48:06 +1000 Subject: [PATCH] hmac: stop using secure memory for the HMAC key Secure memory is design for long term storage of private material. HMAC keys are not this. Secure memory use was introduced in July 2020 by commit 3fddbb264e87a8cef2903cbd7b02b8e1a39a2a99. Fixes #28346 Reviewed-by: Tomas Mraz Reviewed-by: Dmitry Belyavskiy (Merged from https://github.com/openssl/openssl/pull/28411) (cherry picked from commit 362739d771f671b444cb9a12a34accf2dce8220e) --- providers/implementations/macs/hmac_prov.c.in | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/providers/implementations/macs/hmac_prov.c.in b/providers/implementations/macs/hmac_prov.c.in index eb25ef94c2a..4d9bb3ec784 100644 --- a/providers/implementations/macs/hmac_prov.c.in +++ b/providers/implementations/macs/hmac_prov.c.in @@ -102,7 +102,7 @@ static void hmac_free(void *vmacctx) if (macctx != NULL) { HMAC_CTX_free(macctx->ctx); ossl_prov_digest_reset(&macctx->digest); - OPENSSL_secure_clear_free(macctx->key, macctx->keylen); + OPENSSL_clear_free(macctx->key, macctx->keylen); OPENSSL_free(macctx); } } @@ -131,13 +131,13 @@ static void *hmac_dup(void *vsrc) return NULL; } if (src->key != NULL) { - /* There is no "secure" OPENSSL_memdup */ - dst->key = OPENSSL_secure_malloc(src->keylen > 0 ? src->keylen : 1); + dst->key = OPENSSL_malloc(src->keylen > 0 ? src->keylen : 1); if (dst->key == NULL) { hmac_free(dst); return 0; } - memcpy(dst->key, src->key, src->keylen); + if (src->keylen > 0) + memcpy(dst->key, src->key, src->keylen); } return dst; } @@ -182,13 +182,14 @@ static int hmac_setkey(struct hmac_data_st *macctx, #endif if (macctx->key != NULL) - OPENSSL_secure_clear_free(macctx->key, macctx->keylen); + OPENSSL_clear_free(macctx->key, macctx->keylen); /* Keep a copy of the key in case we need it for TLS HMAC */ - macctx->key = OPENSSL_secure_malloc(keylen > 0 ? keylen : 1); + macctx->key = OPENSSL_malloc(keylen > 0 ? keylen : 1); if (macctx->key == NULL) return 0; - memcpy(macctx->key, key, keylen); + if (keylen > 0) + memcpy(macctx->key, key, keylen); macctx->keylen = keylen; digest = ossl_prov_digest_md(&macctx->digest); -- 2.47.3