* [PATCH v2 01/13] lib/crypto: aes: Provide functions for zeroizing aes_key and aes_enckey
2026-09-09 11:54 [PATCH v2 00/13] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
@ 2026-09-09 11:54 ` Thomas Huth
2026-09-09 11:54 ` [PATCH v2 02/13] lib/crypto: aes-xts: Provide function for zeroizing aes_xts_key Thomas Huth
` (11 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Thomas Huth @ 2026-09-09 11:54 UTC (permalink / raw)
To: Eric Biggers, Herbert Xu, David S. Miller, Jason A. Donenfeld,
Ard Biesheuvel
Cc: linux-crypto, linux-kernel, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen
Some crypto functions need to zeroize their local aes_key or aes_enckey
structures after use to avoid leaking sensitive material on the stack.
Provide aes_zeroize_key() and aes_zeroize_enckey() helper functions that
can be used with __cleanup() to automatically zeroize the structs when
they go out of scope.
While we're at it, replace the memzero_explicit() calls in lib/crypto/aes.c
with the new helper functions.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
include/crypto/aes.h | 18 ++++++++++++++++++
lib/crypto/aes.c | 10 +++++-----
2 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/include/crypto/aes.h b/include/crypto/aes.h
index 3279cfa546085..9fe868161e1d3 100644
--- a/include/crypto/aes.h
+++ b/include/crypto/aes.h
@@ -101,6 +101,15 @@ struct aes_enckey {
union aes_enckey_arch k;
};
+/**
+ * aes_zeroize_enckey() - Zeroize an aes_enckey structure
+ * @key: The aes_enckey to zeroize
+ */
+static inline void aes_zeroize_enckey(struct aes_enckey *key)
+{
+ memzero_explicit(key, sizeof(*key));
+}
+
/**
* struct aes_key - An AES key prepared for encryption and decryption
* @aes_enckey: Common fields and the key prepared for encryption
@@ -115,6 +124,15 @@ struct aes_key {
union aes_invkey_arch inv_k;
};
+/**
+ * aes_zeroize_key() - Zeroize an aes_key structure
+ * @key: The aes_key to zeroize
+ */
+static inline void aes_zeroize_key(struct aes_key *key)
+{
+ memzero_explicit(key, sizeof(*key));
+}
+
/*
* Please ensure that the first two fields are 16-byte aligned
* relative to the start of the structure, i.e., don't move them!
diff --git a/lib/crypto/aes.c b/lib/crypto/aes.c
index f1549839b3de0..07c1d912ac365 100644
--- a/lib/crypto/aes.c
+++ b/lib/crypto/aes.c
@@ -539,7 +539,7 @@ static void __init aes_fips_test(void)
if (memcmp(fips_test_data, data, sizeof(data)) != 0)
panic("aes: FIPS self-test failed (wrong plaintext)\n");
- memzero_explicit(&key, sizeof(key));
+ aes_zeroize_key(&key);
}
#if IS_ENABLED(CONFIG_CRYPTO_LIB_AES_CBC_MACS)
@@ -827,7 +827,7 @@ static void __init aes_ecb_fips_test(void)
if (memcmp(fips_test_data, data, sizeof(data)) != 0)
panic("aes: ECB FIPS self-test failed (wrong plaintext)\n");
- memzero_explicit(&key, sizeof(key));
+ aes_zeroize_key(&key);
}
#else /* CONFIG_CRYPTO_LIB_AES_ECB */
static inline void aes_ecb_fips_test(void)
@@ -1040,7 +1040,7 @@ static void __init aes_cbc_fips_test(void)
if (memcmp(fips_test_data, data, sizeof(data)) != 0)
panic("aes: CBC FIPS self-test failed (wrong plaintext)\n");
- memzero_explicit(&key, sizeof(key));
+ aes_zeroize_key(&key);
}
/* FIPS cryptographic algorithm self-test for AES-CBC-CTS */
@@ -1069,7 +1069,7 @@ static void __init aes_cbc_cts_fips_test(void)
if (memcmp(ptext, data, data_len) != 0)
panic("aes: CBC-CTS FIPS self-test failed (wrong plaintext)\n");
- memzero_explicit(&key, sizeof(key));
+ aes_zeroize_key(&key);
}
#else /* CONFIG_CRYPTO_LIB_AES_CBC */
static inline void aes_cbc_fips_test(void)
@@ -1194,7 +1194,7 @@ static void __init aes_ctr_fips_test(void)
if (memcmp(fips_test_data, data, sizeof(data)) != 0)
panic("aes: CTR FIPS self-test failed (wrong plaintext)\n");
- memzero_explicit(&key, sizeof(key));
+ aes_zeroize_enckey(&key);
}
#else /* CONFIG_CRYPTO_LIB_AES_CTR */
static inline void aes_ctr_fips_test(void)
--
2.55.0
^ permalink raw reply [flat|nested] 16+ messages in thread* [PATCH v2 02/13] lib/crypto: aes-xts: Provide function for zeroizing aes_xts_key
2026-09-09 11:54 [PATCH v2 00/13] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
2026-09-09 11:54 ` [PATCH v2 01/13] lib/crypto: aes: Provide functions for zeroizing aes_key and aes_enckey Thomas Huth
@ 2026-09-09 11:54 ` Thomas Huth
2026-09-09 11:54 ` [PATCH v2 03/13] lib/crypto: aes-gcm: Provide functions for zeroizing aes_gcm* structures Thomas Huth
` (10 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Thomas Huth @ 2026-09-09 11:54 UTC (permalink / raw)
To: Eric Biggers, Herbert Xu, David S. Miller, Jason A. Donenfeld,
Ard Biesheuvel
Cc: linux-crypto, linux-kernel, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen
In certain cases crypto code functions need to zeroize their local
aes_xts_key structures after use to avoid leaking sensitive material.
Provide an aes_xts_zeroize_key() helper function that e.g. can be
used with __cleanup() to automatically zeroize the struct when it
goes out of scope.
While we're at it, replace the related memzero_explicit() call in
lib/crypto/aes.c with a call to the new helper function.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
include/crypto/aes-xts.h | 13 +++++++++++--
lib/crypto/aes.c | 2 +-
2 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/include/crypto/aes-xts.h b/include/crypto/aes-xts.h
index b9e828265e58a..3a52e1cf40b57 100644
--- a/include/crypto/aes-xts.h
+++ b/include/crypto/aes-xts.h
@@ -22,6 +22,15 @@ struct aes_xts_key {
struct aes_enckey tweak_key;
};
+/**
+ * aes_xts_zeroize_key() - Zeroize an aes_xts_key structure
+ * @key: The aes_xts_key to zeroize
+ */
+static inline void aes_xts_zeroize_key(struct aes_xts_key *key)
+{
+ memzero_explicit(key, sizeof(*key));
+}
+
/**
* aes_xts_preparekey() - Prepare a key for AES-XTS encryption and decryption
* @key: (output) The key structure to initialize
@@ -30,8 +39,8 @@ struct aes_xts_key {
* @flags: Optional flag XTS_FORBID_WEAK_KEYS to forbid keys whose two halves
* are the same.
*
- * Users should use memzero_explicit() to zeroize the key struct at the end of
- * its lifetime. (But if this function fails, zeroization is unnecessary.)
+ * Users should use aes_xts_zeroize_key() to zeroize the key struct at the end
+ * of its lifetime. (But if this function fails, zeroization is unnecessary.)
*
* Context: Any context.
* Return:
diff --git a/lib/crypto/aes.c b/lib/crypto/aes.c
index 07c1d912ac365..34ef5deca0a79 100644
--- a/lib/crypto/aes.c
+++ b/lib/crypto/aes.c
@@ -1223,7 +1223,7 @@ int aes_xts_preparekey(struct aes_xts_key *key, const u8 *in_key,
return 0;
out_zeroize:
- memzero_explicit(key, sizeof(*key));
+ aes_xts_zeroize_key(key);
return err;
}
EXPORT_SYMBOL_GPL(aes_xts_preparekey);
--
2.55.0
^ permalink raw reply [flat|nested] 16+ messages in thread* [PATCH v2 03/13] lib/crypto: aes-gcm: Provide functions for zeroizing aes_gcm* structures
2026-09-09 11:54 [PATCH v2 00/13] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
2026-09-09 11:54 ` [PATCH v2 01/13] lib/crypto: aes: Provide functions for zeroizing aes_key and aes_enckey Thomas Huth
2026-09-09 11:54 ` [PATCH v2 02/13] lib/crypto: aes-xts: Provide function for zeroizing aes_xts_key Thomas Huth
@ 2026-09-09 11:54 ` Thomas Huth
2026-09-09 11:54 ` [PATCH v2 04/13] lib/crypto: aes-ccm: Provide functions for zeroizing aes_ccm* structures Thomas Huth
` (9 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Thomas Huth @ 2026-09-09 11:54 UTC (permalink / raw)
To: Eric Biggers, Herbert Xu, David S. Miller, Jason A. Donenfeld,
Ard Biesheuvel
Cc: linux-crypto, linux-kernel, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen
In certain cases crypto code needs to zeroize their local aes_gcm_key
or aes_gcm_ctx structures after use to avoid leaking sensitive material.
Provide aes_gcm_zeroize_key() and aes_gcm_zeroize_ctx() helper functions
that e.g. can be used with __cleanup() to automatically zeroize the
structures when they go out of scope.
While we're at it, replace the related memzero_explicit() calls in
lib/crypto/aes.c with calls to the new helper functions.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
include/crypto/aes-gcm.h | 22 ++++++++++++++++++++--
lib/crypto/aes.c | 8 +++-----
2 files changed, 23 insertions(+), 7 deletions(-)
diff --git a/include/crypto/aes-gcm.h b/include/crypto/aes-gcm.h
index 2aee62f019891..a81b00fd8e27f 100644
--- a/include/crypto/aes-gcm.h
+++ b/include/crypto/aes-gcm.h
@@ -21,6 +21,15 @@ struct aes_gcm_key {
size_t authtag_len; /* Length of authentication tags in bytes */
};
+/**
+ * aes_gcm_zeroize_key() - Zeroize an aes_gcm_key structure
+ * @key: The aes_gcm_key to zeroize
+ */
+static inline void aes_gcm_zeroize_key(struct aes_gcm_key *key)
+{
+ memzero_explicit(key, sizeof(*key));
+}
+
/**
* struct aes_gcm_ctx - Context for incrementally en/decrypting a message
*/
@@ -58,6 +67,15 @@ struct aes_gcm_ctx {
u64 data_len;
};
+/**
+ * aes_gcm_zeroize_ctx() - Zeroize an aes_gcm_ctx structure
+ * @ctx: The aes_gcm_ctx to zeroize
+ */
+static inline void aes_gcm_zeroize_ctx(struct aes_gcm_ctx *ctx)
+{
+ memzero_explicit(ctx, sizeof(*ctx));
+}
+
/**
* aes_gcm_preparekey() - Prepare a key for AES-GCM encryption and decryption
* @key: (output) The key structure to initialize
@@ -66,8 +84,8 @@ struct aes_gcm_ctx {
* @authtag_len: Length of the authentication tag in bytes:
* 4, 8, 12, 13, 14, 15, or 16. 16 is recommended.
*
- * Users should use memzero_explicit() to zeroize the key struct at the end of
- * its lifetime. (But if this function fails, zeroization is unnecessary.)
+ * Users should use aes_gcm_zeroize_key() to zeroize the key struct at the end
+ * of its lifetime. (But if this function fails, zeroization is unnecessary.)
*
* Context: Any context.
* Return:
diff --git a/lib/crypto/aes.c b/lib/crypto/aes.c
index 34ef5deca0a79..0cb5d7355926e 100644
--- a/lib/crypto/aes.c
+++ b/lib/crypto/aes.c
@@ -1670,7 +1670,7 @@ void aes_gcm_encrypt_final(struct aes_gcm_ctx *ctx, u8 *authtag)
ghash_final(&ctx->ghash, ctx->ctr); /* Use ctr as temp buffer */
crypto_xor_cpy(authtag, ctx->ctr, ctx->j0_enc, ctx->key->authtag_len);
- memzero_explicit(ctx, sizeof(*ctx));
+ aes_gcm_zeroize_ctx(ctx);
}
EXPORT_SYMBOL_GPL(aes_gcm_encrypt_final);
@@ -1697,7 +1697,7 @@ int aes_gcm_decrypt_final(struct aes_gcm_ctx *ctx, const u8 *authtag)
-EBADMSG :
0;
out:
- memzero_explicit(ctx, sizeof(*ctx));
+ aes_gcm_zeroize_ctx(ctx);
return err;
}
EXPORT_SYMBOL_GPL(aes_gcm_decrypt_final);
@@ -1742,7 +1742,7 @@ static void __init aes_gcm_fips_test(void)
{
const size_t data_len = sizeof(fips_test_data);
u8 buf[sizeof(fips_test_data) + AES_BLOCK_SIZE];
- struct aes_gcm_key key;
+ struct aes_gcm_key key __cleanup(aes_gcm_zeroize_key);
int err;
if (aes_gcm_preparekey(&key, fips_test_key, sizeof(fips_test_key),
@@ -1760,8 +1760,6 @@ static void __init aes_gcm_fips_test(void)
panic("aes: GCM FIPS self-test failed (decryption failed)\n");
if (memcmp(fips_test_data, buf, data_len) != 0)
panic("aes: GCM FIPS self-test failed (wrong plaintext)\n");
-
- memzero_explicit(&key, sizeof(key));
}
#else /* CONFIG_CRYPTO_LIB_AES_GCM */
static inline void aes_gcm_fips_test(void)
--
2.55.0
^ permalink raw reply [flat|nested] 16+ messages in thread* [PATCH v2 04/13] lib/crypto: aes-ccm: Provide functions for zeroizing aes_ccm* structures
2026-09-09 11:54 [PATCH v2 00/13] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (2 preceding siblings ...)
2026-09-09 11:54 ` [PATCH v2 03/13] lib/crypto: aes-gcm: Provide functions for zeroizing aes_gcm* structures Thomas Huth
@ 2026-09-09 11:54 ` Thomas Huth
2026-09-09 11:54 ` [PATCH v2 05/13] lib/crypto: md5: Provide a function for zeroizing hmac_md5 structures Thomas Huth
` (8 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Thomas Huth @ 2026-09-09 11:54 UTC (permalink / raw)
To: Eric Biggers, Herbert Xu, David S. Miller, Jason A. Donenfeld,
Ard Biesheuvel
Cc: linux-crypto, linux-kernel, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen
In certain cases crypto code needs to zeroize their local aes_ccm_key
or aes_ccm_ctx structures after use to avoid leaking sensitive material.
Provide aes_ccm_zeroize_key() and aes_ccm_zeroize_ctx() helper functions
that e.g. can be used with __cleanup() to automatically zeroize the
structures when they go out of scope.
While we're at it, replace the related memzero_explicit() calls in
lib/crypto/aes.c with calls to the new helper functions.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
include/crypto/aes-ccm.h | 22 ++++++++++++++++++++--
lib/crypto/aes.c | 8 +++-----
2 files changed, 23 insertions(+), 7 deletions(-)
diff --git a/include/crypto/aes-ccm.h b/include/crypto/aes-ccm.h
index 8b00859ac4d6b..c52982dd91d2c 100644
--- a/include/crypto/aes-ccm.h
+++ b/include/crypto/aes-ccm.h
@@ -18,6 +18,15 @@ struct aes_ccm_key {
size_t authtag_len; /* Length of authentication tags in bytes */
};
+/**
+ * aes_ccm_zeroize_key() - Zeroize an aes_ccm_key structure
+ * @key: The aes_ccm_key to zeroize
+ */
+static inline void aes_ccm_zeroize_key(struct aes_ccm_key *key)
+{
+ memzero_explicit(key, sizeof(*key));
+}
+
/**
* struct aes_ccm_ctx - Context for incrementally en/decrypting a message
*/
@@ -50,6 +59,15 @@ struct aes_ccm_ctx {
bool ad_padded;
};
+/**
+ * aes_ccm_zeroize_ctx() - Zeroize an aes_ccm_ctx structure
+ * @ctx: The aes_ccm_ctx to zeroize
+ */
+static inline void aes_ccm_zeroize_ctx(struct aes_ccm_ctx *ctx)
+{
+ memzero_explicit(ctx, sizeof(*ctx));
+}
+
/**
* aes_ccm_preparekey() - Prepare a key for AES-CCM encryption and decryption
* @key: (output) The key structure to initialize
@@ -58,8 +76,8 @@ struct aes_ccm_ctx {
* @authtag_len: Length of the authentication tag in bytes:
* 4, 6, 8, 10, 12, 14, or 16. 16 is recommended.
*
- * Users should use memzero_explicit() to zeroize the key struct at the end of
- * its lifetime. (But if this function fails, zeroization is unnecessary.)
+ * Users should use aes_ccm_zeroize_key() to zeroize the key struct at the end
+ * of its lifetime. (But if this function fails, zeroization is unnecessary.)
*
* Context: Any context.
* Return:
diff --git a/lib/crypto/aes.c b/lib/crypto/aes.c
index 0cb5d7355926e..2d29adca79532 100644
--- a/lib/crypto/aes.c
+++ b/lib/crypto/aes.c
@@ -2011,7 +2011,7 @@ void aes_ccm_encrypt_final(struct aes_ccm_ctx *ctx, u8 *authtag)
if (ctx->partial_len)
aes_encrypt(&ctx->key->aes, ctx->mac, ctx->mac);
crypto_xor_cpy(authtag, ctx->mac, ctx->s0, ctx->key->authtag_len);
- memzero_explicit(ctx, sizeof(*ctx));
+ aes_ccm_zeroize_ctx(ctx);
}
EXPORT_SYMBOL_GPL(aes_ccm_encrypt_final);
@@ -2032,7 +2032,7 @@ int aes_ccm_decrypt_final(struct aes_ccm_ctx *ctx, const u8 *authtag)
-EBADMSG :
0;
out:
- memzero_explicit(ctx, sizeof(*ctx));
+ aes_ccm_zeroize_ctx(ctx);
return err;
}
EXPORT_SYMBOL_GPL(aes_ccm_decrypt_final);
@@ -2084,7 +2084,7 @@ static void __init aes_ccm_fips_test(void)
const size_t data_len = sizeof(fips_test_data);
const size_t nonce_len = 13;
u8 buf[sizeof(fips_test_data) + AES_BLOCK_SIZE];
- struct aes_ccm_key key;
+ struct aes_ccm_key key __cleanup(aes_ccm_zeroize_key);
int err;
if (aes_ccm_preparekey(&key, fips_test_key, sizeof(fips_test_key),
@@ -2106,8 +2106,6 @@ static void __init aes_ccm_fips_test(void)
panic("aes: CCM FIPS self-test failed (decryption failed)\n");
if (memcmp(fips_test_data, buf, data_len) != 0)
panic("aes: CCM FIPS self-test failed (wrong plaintext)\n");
-
- memzero_explicit(&key, sizeof(key));
}
#else /* CONFIG_CRYPTO_LIB_AES_CCM */
static inline void aes_ccm_fips_test(void)
--
2.55.0
^ permalink raw reply [flat|nested] 16+ messages in thread* [PATCH v2 05/13] lib/crypto: md5: Provide a function for zeroizing hmac_md5 structures
2026-09-09 11:54 [PATCH v2 00/13] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (3 preceding siblings ...)
2026-09-09 11:54 ` [PATCH v2 04/13] lib/crypto: aes-ccm: Provide functions for zeroizing aes_ccm* structures Thomas Huth
@ 2026-09-09 11:54 ` Thomas Huth
2026-09-09 11:54 ` [PATCH v2 06/13] lib/crypto: sm3: Provide a function for zeroizing the sm3_ctx structure Thomas Huth
` (7 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Thomas Huth @ 2026-09-09 11:54 UTC (permalink / raw)
To: Eric Biggers, Herbert Xu, David S. Miller, Jason A. Donenfeld,
Ard Biesheuvel
Cc: linux-crypto, linux-kernel, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen
In certain cases crypto code functions need to zeroize their local
hmac_md5_key or hmac_md5_ctx structures after use to avoid leaking
sensitive material on the stack.
Provide hmac_md5_zeroize_key() and hmac_md5_zeroize_ctx() helper
functions 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/md5.c with a call to the new helper function.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
include/crypto/md5.h | 19 +++++++++++++++++++
lib/crypto/md5.c | 2 +-
2 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/include/crypto/md5.h b/include/crypto/md5.h
index c47aedfe67ecd..1ed89c15b662c 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
@@ -98,6 +99,15 @@ struct hmac_md5_key {
struct md5_block_state ostate;
};
+/**
+ * hmac_md5_zeroize_key() - Zeroize an hmac_md5_key structure
+ * @key: The hmac_md5_key to zeroize
+ */
+static inline void hmac_md5_zeroize_key(struct hmac_md5_key *key)
+{
+ memzero_explicit(key, sizeof(*key));
+}
+
/**
* struct hmac_md5_ctx - Context for computing HMAC-MD5 of a message
* @hash_ctx: private
@@ -108,6 +118,15 @@ struct hmac_md5_ctx {
struct md5_block_state ostate;
};
+/**
+ * hmac_md5_zeroize_ctx() - Zeroize an hmac_md5_ctx structure
+ * @ctx: The hmac_md5_ctx context to zeroize
+ */
+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
diff --git a/lib/crypto/md5.c b/lib/crypto/md5.c
index 3d2b017a0525a..a8ee57600012d 100644
--- a/lib/crypto/md5.c
+++ b/lib/crypto/md5.c
@@ -271,7 +271,7 @@ void hmac_md5_final(struct hmac_md5_ctx *ctx, u8 out[MD5_DIGEST_SIZE])
cpu_to_le32_array(ctx->ostate.h, ARRAY_SIZE(ctx->ostate.h));
memcpy(out, ctx->ostate.h, MD5_DIGEST_SIZE);
- memzero_explicit(ctx, sizeof(*ctx));
+ hmac_md5_zeroize_ctx(ctx);
}
EXPORT_SYMBOL_GPL(hmac_md5_final);
--
2.55.0
^ permalink raw reply [flat|nested] 16+ messages in thread* [PATCH v2 06/13] lib/crypto: sm3: Provide a function for zeroizing the sm3_ctx structure
2026-09-09 11:54 [PATCH v2 00/13] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (4 preceding siblings ...)
2026-09-09 11:54 ` [PATCH v2 05/13] lib/crypto: md5: Provide a function for zeroizing hmac_md5 structures Thomas Huth
@ 2026-09-09 11:54 ` Thomas Huth
2026-09-09 11:54 ` [PATCH v2 07/13] lib/crypto: blake2: Provide functions for zeroizing blake2*_ctx structures Thomas Huth
` (6 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Thomas Huth @ 2026-09-09 11:54 UTC (permalink / raw)
To: Eric Biggers, Herbert Xu, David S. Miller, Jason A. Donenfeld,
Ard Biesheuvel
Cc: linux-crypto, linux-kernel, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen
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@redhat.com>
---
include/crypto/sm3.h | 9 +++++++++
lib/crypto/sm3.c | 2 +-
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/include/crypto/sm3.h b/include/crypto/sm3.h
index 371e8a6617054..1b41356ed0864 100644
--- a/include/crypto/sm3.h
+++ b/include/crypto/sm3.h
@@ -41,6 +41,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
^ permalink raw reply [flat|nested] 16+ messages in thread* [PATCH v2 07/13] lib/crypto: blake2: Provide functions for zeroizing blake2*_ctx structures
2026-09-09 11:54 [PATCH v2 00/13] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (5 preceding siblings ...)
2026-09-09 11:54 ` [PATCH v2 06/13] lib/crypto: sm3: Provide a function for zeroizing the sm3_ctx structure Thomas Huth
@ 2026-09-09 11:54 ` Thomas Huth
2026-09-09 11:54 ` [PATCH v2 08/13] lib/crypto: sha1: Provide functions for zeroizing hmac_sha1 structures Thomas Huth
` (5 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Thomas Huth @ 2026-09-09 11:54 UTC (permalink / raw)
To: Eric Biggers, Herbert Xu, David S. Miller, Jason A. Donenfeld,
Ard Biesheuvel
Cc: linux-crypto, linux-kernel, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen
In certain cases crypto code needs to zeroize their local blake2b_ctx
or blake2s_ctx structures after use to avoid leaking sensitive material.
Provide blake2b_zeroize_ctx() and blake2s_zeroize_ctx() helper functions
that e.g. can be used with __cleanup() to automatically zeroize the
structures when they go out of scope.
While we're at it, replace the related memzero_explicit() calls in
lib/crypto/blake2*.c with calls to the new helper functions.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
include/crypto/blake2b.h | 9 +++++++++
include/crypto/blake2s.h | 9 +++++++++
lib/crypto/blake2b.c | 2 +-
lib/crypto/blake2s.c | 2 +-
4 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/include/crypto/blake2b.h b/include/crypto/blake2b.h
index 3bc37fd103a7a..eda1604bce780 100644
--- a/include/crypto/blake2b.h
+++ b/include/crypto/blake2b.h
@@ -37,6 +37,15 @@ struct blake2b_ctx {
unsigned int outlen;
};
+/**
+ * blake2b_zeroize_ctx() - Zeroize a blake2b_ctx structure
+ * @ctx: The blake2b_ctx to zeroize
+ */
+static inline void blake2b_zeroize_ctx(struct blake2b_ctx *ctx)
+{
+ memzero_explicit(ctx, sizeof(*ctx));
+}
+
enum blake2b_iv {
BLAKE2B_IV0 = 0x6A09E667F3BCC908ULL,
BLAKE2B_IV1 = 0xBB67AE8584CAA73BULL,
diff --git a/include/crypto/blake2s.h b/include/crypto/blake2s.h
index 648cb78243588..bb4e6870ed196 100644
--- a/include/crypto/blake2s.h
+++ b/include/crypto/blake2s.h
@@ -41,6 +41,15 @@ struct blake2s_ctx {
unsigned int outlen;
};
+/**
+ * blake2s_zeroize_ctx() - Zeroize a blake2s_ctx structure
+ * @ctx: The blake2s_ctx to zeroize
+ */
+static inline void blake2s_zeroize_ctx(struct blake2s_ctx *ctx)
+{
+ memzero_explicit(ctx, sizeof(*ctx));
+}
+
enum blake2s_iv {
BLAKE2S_IV0 = 0x6A09E667UL,
BLAKE2S_IV1 = 0xBB67AE85UL,
diff --git a/lib/crypto/blake2b.c b/lib/crypto/blake2b.c
index 581b7f8486fae..55d6c437f311f 100644
--- a/lib/crypto/blake2b.c
+++ b/lib/crypto/blake2b.c
@@ -148,7 +148,7 @@ void blake2b_final(struct blake2b_ctx *ctx, u8 *out)
blake2b_compress(ctx, ctx->buf, 1, ctx->buflen);
cpu_to_le64_array(ctx->h, ARRAY_SIZE(ctx->h));
memcpy(out, ctx->h, ctx->outlen);
- memzero_explicit(ctx, sizeof(*ctx));
+ blake2b_zeroize_ctx(ctx);
}
EXPORT_SYMBOL(blake2b_final);
diff --git a/lib/crypto/blake2s.c b/lib/crypto/blake2s.c
index 71578a0847423..24f7f34334b01 100644
--- a/lib/crypto/blake2s.c
+++ b/lib/crypto/blake2s.c
@@ -142,7 +142,7 @@ void blake2s_final(struct blake2s_ctx *ctx, u8 *out)
blake2s_compress(ctx, ctx->buf, 1, ctx->buflen);
cpu_to_le32_array(ctx->h, ARRAY_SIZE(ctx->h));
memcpy(out, ctx->h, ctx->outlen);
- memzero_explicit(ctx, sizeof(*ctx));
+ blake2s_zeroize_ctx(ctx);
}
EXPORT_SYMBOL(blake2s_final);
--
2.55.0
^ permalink raw reply [flat|nested] 16+ messages in thread* [PATCH v2 08/13] lib/crypto: sha1: Provide functions for zeroizing hmac_sha1 structures
2026-09-09 11:54 [PATCH v2 00/13] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (6 preceding siblings ...)
2026-09-09 11:54 ` [PATCH v2 07/13] lib/crypto: blake2: Provide functions for zeroizing blake2*_ctx structures Thomas Huth
@ 2026-09-09 11:54 ` Thomas Huth
2026-09-09 11:54 ` [PATCH v2 09/13] security: keys: trusted: always clear the hmac_sha1_ctx before returning Thomas Huth
` (4 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Thomas Huth @ 2026-09-09 11:54 UTC (permalink / raw)
To: Eric Biggers, Herbert Xu, David S. Miller, Jason A. Donenfeld,
Ard Biesheuvel
Cc: linux-crypto, linux-kernel, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen
In certain cases crypto code needs to zeroize their local hmac_sha1_key
or hmac_sha1_ctx structures after use to avoid leaking sensitive material.
Provide hmac_sha1_zeroize_key() and hmac_sha1_zeroize_ctx() helper
functions that e.g. can be used with __cleanup() to automatically zeroize
the structures when they go out of scope.
While we're at it, replace the related memzero_explicit() call in
lib/crypto/sha1.c with a call to the new helper function.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
include/crypto/sha1.h | 19 +++++++++++++++++++
lib/crypto/sha1.c | 2 +-
2 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/include/crypto/sha1.h b/include/crypto/sha1.h
index 4d973e016cd69..bc0046bffeaee 100644
--- a/include/crypto/sha1.h
+++ b/include/crypto/sha1.h
@@ -7,6 +7,7 @@
#define _CRYPTO_SHA1_H
#include <linux/types.h>
+#include <linux/string.h>
#define SHA1_DIGEST_SIZE 20
#define SHA1_BLOCK_SIZE 64
@@ -96,6 +97,15 @@ struct hmac_sha1_key {
struct sha1_block_state ostate;
};
+/**
+ * hmac_sha1_zeroize_key() - Zeroize an hmac_sha1_key structure
+ * @key: The hmac_sha1_key to zeroize
+ */
+static inline void hmac_sha1_zeroize_key(struct hmac_sha1_key *key)
+{
+ memzero_explicit(key, sizeof(*key));
+}
+
/**
* struct hmac_sha1_ctx - Context for computing HMAC-SHA1 of a message
* @sha_ctx: private
@@ -106,6 +116,15 @@ struct hmac_sha1_ctx {
struct sha1_block_state ostate;
};
+/**
+ * hmac_sha1_zeroize_ctx() - Zeroize an hmac_sha1_ctx structure
+ * @ctx: The hmac_sha1_ctx context to zeroize
+ */
+static inline void hmac_sha1_zeroize_ctx(struct hmac_sha1_ctx *ctx)
+{
+ memzero_explicit(ctx, sizeof(*ctx));
+}
+
/**
* hmac_sha1_preparekey() - Prepare a key for HMAC-SHA1
* @key: (output) the key structure to initialize
diff --git a/lib/crypto/sha1.c b/lib/crypto/sha1.c
index b687b89d97cb4..c4361ef77166e 100644
--- a/lib/crypto/sha1.c
+++ b/lib/crypto/sha1.c
@@ -275,7 +275,7 @@ void hmac_sha1_final(struct hmac_sha1_ctx *ctx, u8 out[SHA1_DIGEST_SIZE])
for (size_t i = 0; i < SHA1_DIGEST_SIZE; i += 4)
put_unaligned_be32(ctx->ostate.h[i / 4], out + i);
- memzero_explicit(ctx, sizeof(*ctx));
+ hmac_sha1_zeroize_ctx(ctx);
}
EXPORT_SYMBOL_GPL(hmac_sha1_final);
--
2.55.0
^ permalink raw reply [flat|nested] 16+ messages in thread* [PATCH v2 09/13] security: keys: trusted: always clear the hmac_sha1_ctx before returning
2026-09-09 11:54 [PATCH v2 00/13] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (7 preceding siblings ...)
2026-09-09 11:54 ` [PATCH v2 08/13] lib/crypto: sha1: Provide functions for zeroizing hmac_sha1 structures Thomas Huth
@ 2026-09-09 11:54 ` Thomas Huth
2026-09-09 11:54 ` [PATCH v2 10/13] x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY Thomas Huth
` (3 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Thomas Huth @ 2026-09-09 11:54 UTC (permalink / raw)
To: Eric Biggers, Herbert Xu, David S. Miller, Jason A. Donenfeld,
Ard Biesheuvel, James Bottomley, Jarkko Sakkinen, Mimi Zohar,
David Howells, Paul Moore, James Morris, Serge E. Hallyn
Cc: linux-crypto, linux-kernel, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, linux-integrity, keyrings,
linux-security-module
Clear the hmac_sha1_ctx structure via __cleanup(hmac_sha1_zeroize_ctx)
to make sure that the function does not leak sensitive data on the stack
when returning without calling hmac_sha1_final().
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
security/keys/trusted-keys/trusted_tpm1.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c
index bf0bf7f369705..e5a904b5c1946 100644
--- a/security/keys/trusted-keys/trusted_tpm1.c
+++ b/security/keys/trusted-keys/trusted_tpm1.c
@@ -101,7 +101,7 @@ static inline void dump_tpm_buf(unsigned char *buf)
static int TSS_rawhmac(unsigned char *digest, const unsigned char *key,
unsigned int keylen, ...)
{
- struct hmac_sha1_ctx hmac_ctx;
+ struct hmac_sha1_ctx hmac_ctx __cleanup(hmac_sha1_zeroize_ctx);
va_list argp;
unsigned int dlen;
unsigned char *data;
--
2.55.0
^ permalink raw reply [flat|nested] 16+ messages in thread* [PATCH v2 10/13] x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY
2026-09-09 11:54 [PATCH v2 00/13] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (8 preceding siblings ...)
2026-09-09 11:54 ` [PATCH v2 09/13] security: keys: trusted: always clear the hmac_sha1_ctx before returning Thomas Huth
@ 2026-09-09 11:54 ` Thomas Huth
2026-09-09 11:54 ` [PATCH v2 11/13] lib/crypto: sha2: Provide functions for zeroizing SHA2 hmac_sha* structures Thomas Huth
` (2 subsequent siblings)
12 siblings, 0 replies; 16+ messages in thread
From: Thomas Huth @ 2026-09-09 11:54 UTC (permalink / raw)
To: Eric Biggers, Herbert Xu, David S. Miller, Jason A. Donenfeld,
Ard Biesheuvel, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
Dave Hansen, x86
Cc: linux-crypto, linux-kernel, H. Peter Anvin
A subsequent patch will add #include <linux/string.h> to <crypto/sha2.h>
to use memzero_explicit() there. This will introduce a conflict with the
purgatory code: purgatory.c includes both, the <crypto/sha2.h> header and
the arch/x86/boot/string.h header. The latter provides its own prototypes
for a lot of string functions which clash with the fortified macros
from <linux/string.h>. To avoid the problem, compile the code in
purgatory.c with -D__NO_FORTIFY, so <linux/string.h> can properly be
included from <crypto/sha2.h> in the purgatory, too.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
arch/x86/purgatory/Makefile | 1 +
1 file changed, 1 insertion(+)
diff --git a/arch/x86/purgatory/Makefile b/arch/x86/purgatory/Makefile
index 5ce1d42630000..9191e3cffc30b 100644
--- a/arch/x86/purgatory/Makefile
+++ b/arch/x86/purgatory/Makefile
@@ -12,6 +12,7 @@ $(obj)/sha256.o: $(srctree)/lib/crypto/sha256.c FORCE
$(call if_changed_rule,cc_o_c)
CFLAGS_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
+CFLAGS_purgatory.o += -D__NO_FORTIFY
# When profile-guided optimization is enabled, llvm emits two different
# overlapping text sections, which is not supported by kexec. Remove profile
--
2.55.0
^ permalink raw reply [flat|nested] 16+ messages in thread* [PATCH v2 11/13] lib/crypto: sha2: Provide functions for zeroizing SHA2 hmac_sha* structures
2026-09-09 11:54 [PATCH v2 00/13] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (9 preceding siblings ...)
2026-09-09 11:54 ` [PATCH v2 10/13] x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY Thomas Huth
@ 2026-09-09 11:54 ` Thomas Huth
2026-09-09 11:54 ` [PATCH v2 12/13] smb: client: Use hmac_sha256_zeroize_ctx function to clear hmac_sha256_ctx Thomas Huth
2026-09-09 11:54 ` [PATCH v2 13/13] lib/crypto: Add documentation about zeroization of key and context data Thomas Huth
12 siblings, 0 replies; 16+ messages in thread
From: Thomas Huth @ 2026-09-09 11:54 UTC (permalink / raw)
To: Eric Biggers, Herbert Xu, David S. Miller, Jason A. Donenfeld,
Ard Biesheuvel
Cc: linux-crypto, linux-kernel, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen
In certain cases crypto code functions need to zeroize their local SHA2
hmac_sha*_key or hmac_sha*_ctx structures after use to avoid leaking
sensitive material on the stack.
Provide hmac_sha*_zeroize_key() and hmac_sha*_zeroize_ctx() helper
functions that can be used with __cleanup() to automatically zeroize
the structure when it goes out of scope.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
include/crypto/sha2.h | 73 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 73 insertions(+)
diff --git a/include/crypto/sha2.h b/include/crypto/sha2.h
index 7bb8fe169daf2..22fbc37ae8407 100644
--- a/include/crypto/sha2.h
+++ b/include/crypto/sha2.h
@@ -7,6 +7,7 @@
#define _CRYPTO_SHA2_H
#include <linux/types.h>
+#include <linux/string.h>
#define SHA224_DIGEST_SIZE 28
#define SHA224_BLOCK_SIZE 64
@@ -210,6 +211,15 @@ struct hmac_sha224_key {
struct __hmac_sha256_key key;
};
+/**
+ * hmac_sha224_zeroize_key() - Zeroize an hmac_sha224_key structure
+ * @key: The hmac_sha224_key to zeroize
+ */
+static inline void hmac_sha224_zeroize_key(struct hmac_sha224_key *key)
+{
+ memzero_explicit(key, sizeof(*key));
+}
+
/**
* struct hmac_sha224_ctx - Context for computing HMAC-SHA224 of a message
* @ctx: private
@@ -218,6 +228,15 @@ struct hmac_sha224_ctx {
struct __hmac_sha256_ctx ctx;
};
+/**
+ * hmac_sha224_zeroize_ctx() - Zeroize an hmac_sha224_ctx structure
+ * @ctx: The hmac_sha224_ctx context to zeroize
+ */
+static inline void hmac_sha224_zeroize_ctx(struct hmac_sha224_ctx *ctx)
+{
+ memzero_explicit(ctx, sizeof(*ctx));
+}
+
/**
* hmac_sha224_preparekey() - Prepare a key for HMAC-SHA224
* @key: (output) the key structure to initialize
@@ -414,6 +433,15 @@ struct hmac_sha256_key {
struct __hmac_sha256_key key;
};
+/**
+ * hmac_sha256_zeroize_key() - Zeroize an hmac_sha256_key structure
+ * @key: The hmac_sha256_key to zeroize
+ */
+static inline void hmac_sha256_zeroize_key(struct hmac_sha256_key *key)
+{
+ memzero_explicit(key, sizeof(*key));
+}
+
/**
* struct hmac_sha256_ctx - Context for computing HMAC-SHA256 of a message
* @ctx: private
@@ -422,6 +450,15 @@ struct hmac_sha256_ctx {
struct __hmac_sha256_ctx ctx;
};
+/**
+ * hmac_sha256_zeroize_ctx() - Zeroize an hmac_sha256_ctx structure
+ * @ctx: The hmac_sha256_ctx context to zeroize
+ */
+static inline void hmac_sha256_zeroize_ctx(struct hmac_sha256_ctx *ctx)
+{
+ memzero_explicit(ctx, sizeof(*ctx));
+}
+
/**
* hmac_sha256_preparekey() - Prepare a key for HMAC-SHA256
* @key: (output) the key structure to initialize
@@ -623,6 +660,15 @@ struct hmac_sha384_key {
struct __hmac_sha512_key key;
};
+/**
+ * hmac_sha384_zeroize_key() - Zeroize an hmac_sha384_key structure
+ * @key: The hmac_sha384_key to zeroize
+ */
+static inline void hmac_sha384_zeroize_key(struct hmac_sha384_key *key)
+{
+ memzero_explicit(key, sizeof(*key));
+}
+
/**
* struct hmac_sha384_ctx - Context for computing HMAC-SHA384 of a message
* @ctx: private
@@ -631,6 +677,15 @@ struct hmac_sha384_ctx {
struct __hmac_sha512_ctx ctx;
};
+/**
+ * hmac_sha384_zeroize_ctx() - Zeroize an hmac_sha384_ctx structure
+ * @ctx: The hmac_sha384_ctx context to zeroize
+ */
+static inline void hmac_sha384_zeroize_ctx(struct hmac_sha384_ctx *ctx)
+{
+ memzero_explicit(ctx, sizeof(*ctx));
+}
+
/**
* hmac_sha384_preparekey() - Prepare a key for HMAC-SHA384
* @key: (output) the key structure to initialize
@@ -798,6 +853,15 @@ struct hmac_sha512_key {
struct __hmac_sha512_key key;
};
+/**
+ * hmac_sha512_zeroize_key() - Zeroize an hmac_sha512_key structure
+ * @key: The hmac_sha512_key to zeroize
+ */
+static inline void hmac_sha512_zeroize_key(struct hmac_sha512_key *key)
+{
+ memzero_explicit(key, sizeof(*key));
+}
+
/**
* struct hmac_sha512_ctx - Context for computing HMAC-SHA512 of a message
* @ctx: private
@@ -806,6 +870,15 @@ struct hmac_sha512_ctx {
struct __hmac_sha512_ctx ctx;
};
+/**
+ * hmac_sha512_zeroize_ctx() - Zeroize an hmac_sha512_ctx structure
+ * @ctx: The hmac_sha512_ctx context to zeroize
+ */
+static inline void hmac_sha512_zeroize_ctx(struct hmac_sha512_ctx *ctx)
+{
+ memzero_explicit(ctx, sizeof(*ctx));
+}
+
/**
* hmac_sha512_preparekey() - Prepare a key for HMAC-SHA512
* @key: (output) the key structure to initialize
--
2.55.0
^ permalink raw reply [flat|nested] 16+ messages in thread* [PATCH v2 12/13] smb: client: Use hmac_sha256_zeroize_ctx function to clear hmac_sha256_ctx
2026-09-09 11:54 [PATCH v2 00/13] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (10 preceding siblings ...)
2026-09-09 11:54 ` [PATCH v2 11/13] lib/crypto: sha2: Provide functions for zeroizing SHA2 hmac_sha* structures Thomas Huth
@ 2026-09-09 11:54 ` Thomas Huth
2026-09-09 11:54 ` [PATCH v2 13/13] lib/crypto: Add documentation about zeroization of key and context data Thomas Huth
12 siblings, 0 replies; 16+ messages in thread
From: Thomas Huth @ 2026-09-09 11:54 UTC (permalink / raw)
To: Eric Biggers, Herbert Xu, David S. Miller, Jason A. Donenfeld,
Ard Biesheuvel, Paulo Alcantara, Namjae Jeon
Cc: linux-crypto, linux-kernel, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, Ronnie Sahlberg, Shyam Prasad N,
Tom Talpey, Bharath SM, linux-cifs, samba-technical
It's just cosmetics, but now that we have a helper function for clearing
hmac_sha256_ctx with a __cleanup() statement, we can also use it in the smb
client code for good measure.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
fs/smb/client/smb2transport.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/fs/smb/client/smb2transport.c b/fs/smb/client/smb2transport.c
index c407f30e00401..080864b24a0c5 100644
--- a/fs/smb/client/smb2transport.c
+++ b/fs/smb/client/smb2transport.c
@@ -212,7 +212,7 @@ smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
unsigned char smb2_signature[SMB2_HMACSHA256_SIZE];
struct kvec *iov = rqst->rq_iov;
struct smb2_hdr *shdr = (struct smb2_hdr *)iov[0].iov_base;
- struct hmac_sha256_ctx hmac_ctx;
+ struct hmac_sha256_ctx hmac_ctx __cleanup(hmac_sha256_zeroize_ctx);
struct smb_rqst drqst;
__u64 sid = le64_to_cpu(shdr->SessionId);
u8 key[SMB2_NTLMV2_SESSKEY_SIZE];
@@ -250,7 +250,6 @@ smb2_calc_signature(struct smb_rqst *rqst, struct TCP_Server_Info *server)
memcpy(shdr->Signature, smb2_signature, SMB2_SIGNATURE_SIZE);
memzero_explicit(key, sizeof(key));
- memzero_explicit(&hmac_ctx, sizeof(hmac_ctx));
return rc;
}
--
2.55.0
^ permalink raw reply [flat|nested] 16+ messages in thread* [PATCH v2 13/13] lib/crypto: Add documentation about zeroization of key and context data
2026-09-09 11:54 [PATCH v2 00/13] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (11 preceding siblings ...)
2026-09-09 11:54 ` [PATCH v2 12/13] smb: client: Use hmac_sha256_zeroize_ctx function to clear hmac_sha256_ctx Thomas Huth
@ 2026-09-09 11:54 ` Thomas Huth
2026-09-09 13:22 ` Jonathan Corbet
12 siblings, 1 reply; 16+ messages in thread
From: Thomas Huth @ 2026-09-09 11:54 UTC (permalink / raw)
To: Eric Biggers, Herbert Xu, David S. Miller, Jason A. Donenfeld,
Ard Biesheuvel, Jonathan Corbet
Cc: linux-crypto, linux-kernel, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, Shuah Khan, Randy Dunlap,
linux-doc
Add a central document about zeroization in libcrypto so we don't
have to repeat this information in the individual kernel docs of
the zeroization functions all over the place.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
.../crypto/libcrypto-zeroization.rst | 129 ++++++++++++++++++
Documentation/crypto/libcrypto.rst | 1 +
2 files changed, 130 insertions(+)
create mode 100644 Documentation/crypto/libcrypto-zeroization.rst
diff --git a/Documentation/crypto/libcrypto-zeroization.rst b/Documentation/crypto/libcrypto-zeroization.rst
new file mode 100644
index 0000000000000..ba9b05320ad53
--- /dev/null
+++ b/Documentation/crypto/libcrypto-zeroization.rst
@@ -0,0 +1,129 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+
+Crypto Key Zeroization
+======================
+
+This document describes the conventions for zeroizing crypto structures in the
+kernel.
+
+.. contents::
+
+Overview
+--------
+
+Cryptographic key material and intermediate state (such as HMAC contexts) must
+be zeroized after use to prevent sensitive data from lingering on the stack or
+heap, where it could be leaked through memory disclosure vulnerabilities,
+crash dumps, or cold-boot attacks.
+
+For memory that has been allocated with kmalloc() or a similar function,
+kfree_sensitive() should be used instead of kfree() to release the memory.
+
+For other cases, the kernel provides ``memzero_explicit()`` for clearing the
+memory. Unlike plain ``memset()``, ``memzero_explicit()`` is guaranteed not
+to be optimized away by the compiler, even when the memory being cleared
+appears to be dead.
+
+The crypto library builds on ``memzero_explicit()`` by providing typed
+zeroization helpers for each key and context structure. These helpers serve
+two purposes:
+
+1. They make ``__cleanup()`` annotations possible, so that structures on
+ the stack are automatically zeroized when they go out of scope.
+
+2. They improve readability by replacing ``memzero_explicit(&key, sizeof(key))``
+ with a self-documenting call like ``aes_zeroize_key(&key)``.
+
+
+What to zeroize
+---------------
+
+The following types of structures hold sensitive material and should be
+zeroized after use:
+
+- **Key structures** (e.g. ``struct aes_key``, ``struct hmac_sha256_key``):
+ contain expanded round keys or prepared key material.
+
+- **HMAC/MAC context structures** (e.g. ``struct hmac_sha256_ctx``,
+ ``struct aes_cmac_ctx``): contain inner and outer hash states derived from
+ the key.
+
+- **Hash context structures** (e.g. ``struct sha256_ctx``): may contain
+ sensitive data being hashed.
+
+Not all of these require explicit cleanup by callers. Many ``..._final()``
+functions already zeroize their context internally (see `Automatic vs. manual
+zeroization`_ below).
+
+
+Zeroization helpers
+-------------------
+
+Each crypto structure that callers may need to zeroize should have a
+corresponding inline helper function. The naming convention is::
+
+ <algorithm>_zeroize_<type>(struct <algorithm>_<type> *p);
+
+For example::
+
+ void aes_zeroize_key(struct aes_key *key);
+ void aes_zeroize_enckey(struct aes_enckey *key);
+ void hmac_sha256_zeroize_ctx(struct hmac_sha256_ctx *ctx);
+ void aes_cmac_zeroize_key(struct aes_cmac_key *key);
+ void aes_cmac_zeroize_ctx(struct aes_cmac_ctx *ctx);
+
+Each helper is a ``static inline`` function in the algorithm's header that
+wraps ``memzero_explicit()``, for example::
+
+ static inline void hmac_sha256_zeroize_ctx(struct hmac_sha256_ctx *ctx)
+ {
+ memzero_explicit(ctx, sizeof(*ctx));
+ }
+
+These helpers should include kernel-doc comments following the standard
+conventions::
+
+ /**
+ * hmac_sha256_zeroize_ctx() - Zeroize an hmac_sha256_ctx structure
+ * @ctx: The hmac_sha256_ctx context to zeroize
+ */
+
+
+Using __cleanup for automatic zeroization
+-----------------------------------------
+
+The preferred way to zeroize stack-allocated key and context structures is
+with the ``__cleanup()`` attribute. This ensures zeroization happens on all
+exit paths, including error returns and early exits.
+
+Note that __cleanup() attributes should not be used in functions that use
+"goto" statements. The benefit of cleanup helpers is the removal of "gotos",
+and that "goto" statements can jump between scopes, so the expectation is
+that usage of "goto" and cleanup helpers is never mixed in the same function.
+
+
+Automatic vs. manual zeroization
+--------------------------------
+
+Many ``..._final()`` functions in the crypto library automatically zeroize
+their context before returning. When this is the case, the kernel-doc for the
+function documents it::
+
+ After finishing, this zeroizes @ctx. So the caller does not need to do it.
+
+In these cases, callers on simple code paths (where ``..._final()`` is always
+reached) do not need to add ``__cleanup()`` or explicit zeroization.
+However, ``__cleanup()`` is still recommended whenever there are error paths
+that bypass ``..._final()``, as it ensures zeroization on all paths.
+
+For algorithms where ``_final()`` does *not* zeroize the context (such as the
+SHAKE XOFs, where ``shake_squeeze()`` can be called multiple times), callers
+must explicitly zeroize the context by calling the appropriate helper or using
+``__cleanup()``, for example::
+
+ struct shake_ctx ctx __cleanup(shake_zeroize_ctx);
+
+ shake256_init(&ctx);
+ shake_update(&ctx, data, data_len);
+ shake_squeeze(&ctx, out, out_len);
+ /* ctx is automatically zeroized at end of scope */
diff --git a/Documentation/crypto/libcrypto.rst b/Documentation/crypto/libcrypto.rst
index e911e05215979..9533c12caa79d 100644
--- a/Documentation/crypto/libcrypto.rst
+++ b/Documentation/crypto/libcrypto.rst
@@ -165,4 +165,5 @@ API documentation
libcrypto-signature
libcrypto-unauth-encryption
libcrypto-utils
+ libcrypto-zeroization
sha3
--
2.55.0
^ permalink raw reply [flat|nested] 16+ messages in thread* Re: [PATCH v2 13/13] lib/crypto: Add documentation about zeroization of key and context data
2026-09-09 11:54 ` [PATCH v2 13/13] lib/crypto: Add documentation about zeroization of key and context data Thomas Huth
@ 2026-09-09 13:22 ` Jonathan Corbet
2026-09-10 9:09 ` Thomas Huth
0 siblings, 1 reply; 16+ messages in thread
From: Jonathan Corbet @ 2026-09-09 13:22 UTC (permalink / raw)
To: Thomas Huth, Eric Biggers, Herbert Xu, David S. Miller,
Jason A. Donenfeld, Ard Biesheuvel
Cc: linux-crypto, linux-kernel, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, Shuah Khan, Randy Dunlap,
linux-doc
Thomas Huth <thuth@redhat.com> writes:
> Add a central document about zeroization in libcrypto so we don't
> have to repeat this information in the individual kernel docs of
> the zeroization functions all over the place.
>
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
> .../crypto/libcrypto-zeroization.rst | 129 ++++++++++++++++++
> Documentation/crypto/libcrypto.rst | 1 +
> 2 files changed, 130 insertions(+)
> create mode 100644 Documentation/crypto/libcrypto-zeroization.rst
One nit...
> diff --git a/Documentation/crypto/libcrypto-zeroization.rst b/Documentation/crypto/libcrypto-zeroization.rst
> new file mode 100644
> index 0000000000000..ba9b05320ad53
> --- /dev/null
> +++ b/Documentation/crypto/libcrypto-zeroization.rst
> @@ -0,0 +1,129 @@
> +.. SPDX-License-Identifier: GPL-2.0-or-later
> +
> +Crypto Key Zeroization
> +======================
> +
> +This document describes the conventions for zeroizing crypto structures in the
> +kernel.
> +
> +.. contents::
> +
> +Overview
> +--------
> +
> +Cryptographic key material and intermediate state (such as HMAC contexts) must
> +be zeroized after use to prevent sensitive data from lingering on the stack or
> +heap, where it could be leaked through memory disclosure vulnerabilities,
> +crash dumps, or cold-boot attacks.
> +
> +For memory that has been allocated with kmalloc() or a similar function,
> +kfree_sensitive() should be used instead of kfree() to release the memory.
> +
> +For other cases, the kernel provides ``memzero_explicit()`` for clearing the
> +memory. Unlike plain ``memset()``, ``memzero_explicit()`` is guaranteed not
> +to be optimized away by the compiler, even when the memory being cleared
> +appears to be dead.
Don't mark up function names, just say memzero_explicit(). The
automarkup code will then do the right thing, including cross-references
when the functions have kerneldoc documentation.
Thanks,
jon
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v2 13/13] lib/crypto: Add documentation about zeroization of key and context data
2026-09-09 13:22 ` Jonathan Corbet
@ 2026-09-10 9:09 ` Thomas Huth
0 siblings, 0 replies; 16+ messages in thread
From: Thomas Huth @ 2026-09-10 9:09 UTC (permalink / raw)
To: Jonathan Corbet, Eric Biggers, Herbert Xu, David S. Miller,
Jason A. Donenfeld, Ard Biesheuvel
Cc: linux-crypto, linux-kernel, Thomas Gleixner, Ingo Molnar,
Borislav Petkov, Dave Hansen, Shuah Khan, Randy Dunlap,
linux-doc
On 09/09/2026 15.22, Jonathan Corbet wrote:
> Thomas Huth <thuth@redhat.com> writes:
...
>> +For other cases, the kernel provides ``memzero_explicit()`` for clearing the
>> +memory. Unlike plain ``memset()``, ``memzero_explicit()`` is guaranteed not
>> +to be optimized away by the compiler, even when the memory being cleared
>> +appears to be dead.
>
> Don't mark up function names, just say memzero_explicit(). The
> automarkup code will then do the right thing, including cross-references
> when the functions have kerneldoc documentation.
OK, thanks for the hint, I'll fix it in the next version!
Thomas
^ permalink raw reply [flat|nested] 16+ messages in thread