[PATCH v3 06/13] lib/crypto: sm3: Provide a function for zeroizing the sm3_ctx structure
From: Thomas Huth
Date: Thu Sep 10 2026 - 08:49:51 EST
In certain cases crypto code functions need to zeroize their local
sm3_ctx structure after use to avoid leaking sensitive material. Provide
a sm3_zeroize_ctx() helper function that e.g. can be used with __cleanup()
to automatically zeroize the structure when it goes out of scope.
While we're at it, replace the related memzero_explicit() call in
lib/crypto/sm3.c with a call to the new helper function.
Signed-off-by: Thomas Huth <thuth@xxxxxxxxxx>
---
include/crypto/sm3.h | 10 ++++++++++
lib/crypto/sm3.c | 2 +-
2 files changed, 11 insertions(+), 1 deletion(-)
diff --git a/include/crypto/sm3.h b/include/crypto/sm3.h
index 371e8a6617054..d044ca80213ba 100644
--- a/include/crypto/sm3.h
+++ b/include/crypto/sm3.h
@@ -11,6 +11,7 @@
#define _CRYPTO_SM3_H
#include <linux/types.h>
+#include <linux/string.h>
#define SM3_DIGEST_SIZE 32
#define SM3_BLOCK_SIZE 64
@@ -41,6 +42,15 @@ struct sm3_ctx {
u8 buf[SM3_BLOCK_SIZE] __aligned(__alignof__(__be64));
};
+/**
+ * sm3_zeroize_ctx() - Zeroize an sm3_ctx structure
+ * @ctx: The sm3_ctx to zeroize
+ */
+static inline void sm3_zeroize_ctx(struct sm3_ctx *ctx)
+{
+ memzero_explicit(ctx, sizeof(*ctx));
+}
+
/**
* sm3_init() - Initialize an SM3 context for a new message
* @ctx: the context to initialize
diff --git a/lib/crypto/sm3.c b/lib/crypto/sm3.c
index b02b8a247adf2..23059347b4493 100644
--- a/lib/crypto/sm3.c
+++ b/lib/crypto/sm3.c
@@ -258,7 +258,7 @@ static void __sm3_final(struct sm3_ctx *ctx, u8 out[SM3_DIGEST_SIZE])
void sm3_final(struct sm3_ctx *ctx, u8 out[SM3_DIGEST_SIZE])
{
__sm3_final(ctx, out);
- memzero_explicit(ctx, sizeof(*ctx));
+ sm3_zeroize_ctx(ctx);
}
EXPORT_SYMBOL_GPL(sm3_final);
--
2.55.0