[PATCH 06/11] lib/crypto: md5: Provide a function for zeroizing hmac_md5_ctx structures

From: Thomas Huth

Date: Thu Aug 13 2026 - 09:53:15 EST


From: Thomas Huth <thuth@xxxxxxxxxx>

Some crypto code functions need to zeroize their local hmac_md5_ctx
structures after use to avoid leaking sensitive material on the stack.
Provide a hmac_md5_zeroize_ctx() helper function that can be used with
__cleanup() to automatically zeroize the context when it goes out of
scope.

Signed-off-by: Thomas Huth <thuth@xxxxxxxxxx>
---
include/crypto/md5.h | 15 +++++++++++++++
1 file changed, 15 insertions(+)

diff --git a/include/crypto/md5.h b/include/crypto/md5.h
index c47aedfe67ecd..8cf26fd965323 100644
--- a/include/crypto/md5.h
+++ b/include/crypto/md5.h
@@ -4,6 +4,7 @@

#include <crypto/hash.h>
#include <linux/types.h>
+#include <linux/string.h>

#define MD5_DIGEST_SIZE 16
#define MD5_HMAC_BLOCK_SIZE 64
@@ -108,6 +109,20 @@ struct hmac_md5_ctx {
struct md5_block_state ostate;
};

+/**
+ * hmac_md5_zeroize_ctx() - Zeroize an hmac_md5_ctx structure
+ * @ctx: The location of the context that should be zeroized
+ *
+ * This function explicitly fills the hmac_md5_ctx with zeroes. For
+ * example, use it with __cleanup() for local hmac_md5_ctx structures
+ * on the stack, so that their content is not leaked when the context is
+ * left. Note: This is only required when not using hmac_md5_final().
+ */
+static inline void hmac_md5_zeroize_ctx(struct hmac_md5_ctx *ctx)
+{
+ memzero_explicit(ctx, sizeof(*ctx));
+}
+
/**
* hmac_md5_preparekey() - Prepare a key for HMAC-MD5
* @key: (output) the key structure to initialize
--
2.55.0