* [PATCH 00/11] libcrypto: Provide more __cleanup functions for zeroizing data
@ 2026-08-13 13:49 Thomas Huth
2026-08-13 13:49 ` [PATCH 01/11] lib/crypto: aes: Provide a wrapper function for zeroizing crypto_aes_ctx Thomas Huth
` (10 more replies)
0 siblings, 11 replies; 12+ messages in thread
From: Thomas Huth @ 2026-08-13 13:49 UTC (permalink / raw)
To: Eric Biggers, Jason A. Donenfeld, Ard Biesheuvel
Cc: x86, Herbert Xu, David S. Miller, linux-crypto, linux-kernel
Code that uses crypto-related structures (containing keys or context data)
should zeroize their local structures on the stack after use to avoid
leaking this sensitive material via the stack when the function returns.
Using the __cleanup() marker is a very elegant way to assert that the
data is zeroized without having to painfully verify that each early return
in a function might miss it.
Thus this series introduces zeroization functions for many crypto-related
structures that can be used with __cleanup(). The series focuses on the
introduction of the functions - most call sights will be adjusted to use
these new functions in separate patch series later (since each subsystem
needs separate review from the corresponding maintainer). However, I
already included the two "safexcel" patches, since they already got ack'ed
by the maintainer Antoine, so I think they should be fine to go via the
libcrypto tree.
Note there is one minor ugliness in patch 10: Since sha2.h is also used
in the x86 purgatory code, and that code ships with its own implementation
of string functions, we have to compile the purgatory.c file with
-D__NO_FORTIFY now to be able to include <linux/string.h> in sha2.h.
I hope that solution is OK (especially since the sha256.c file in the
same folder gets that treatment already, too), if not - I'm certainly
open for other suggestions here!
Thomas Huth (11):
lib/crypto: aes: Provide a wrapper function for zeroizing
crypto_aes_ctx
crypto: safexcel - Simplify the check for a valid AES key
crypto: safexcel - zeroize crypto_aes_ctx with
__cleanup(aes_zeroize_ctx)
lib/crypto: aes: Provide functions for zeroizing aes_key and
aes_enckey
lib/crypto: aes: Use aes_zeroize_*key() instead of memzero_explicit()
lib/crypto: md5: Provide a function for zeroizing hmac_md5_ctx
structures
lib/crypto: md5: Use hmac_md5_zeroize_ctx() instead of
memzero_explicit()
lib/crypto: sha1: Provide a wrapper for zeroizing hmac_sha1_ctx
lib/crypto: sha1: Use hmac_sha1_zeroize_ctx() instead of
memzero_explicit()
x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY
lib/crypto: sha2: Provide wrappers for zeroizing SHA2 hmac_sha*_ctx
structures
arch/x86/purgatory/Makefile | 1 +
.../crypto/inside-secure/safexcel_cipher.c | 16 ++----
drivers/crypto/inside-secure/safexcel_hash.c | 3 +-
include/crypto/aes.h | 40 +++++++++++++
include/crypto/md5.h | 15 +++++
include/crypto/sha1.h | 17 ++++++
include/crypto/sha2.h | 57 +++++++++++++++++++
lib/crypto/aes.c | 10 ++--
lib/crypto/md5.c | 2 +-
lib/crypto/sha1.c | 2 +-
10 files changed, 143 insertions(+), 20 deletions(-)
--
2.55.0
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH 01/11] lib/crypto: aes: Provide a wrapper function for zeroizing crypto_aes_ctx
2026-08-13 13:49 [PATCH 00/11] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
@ 2026-08-13 13:49 ` Thomas Huth
2026-08-13 13:49 ` [PATCH 02/11] crypto: safexcel - Simplify the check for a valid AES key Thomas Huth
` (9 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Thomas Huth @ 2026-08-13 13:49 UTC (permalink / raw)
To: Eric Biggers, Jason A. Donenfeld, Ard Biesheuvel, Herbert Xu,
David S. Miller
Cc: x86, linux-crypto, linux-kernel
From: Thomas Huth <thuth@redhat.com>
Several crypto drivers need to zeroize their local crypto_aes_ctx
structures after use to avoid leaking key material on the stack.
Currently some call sites do this with their own memzero_explicit()
call, which is error-prone since it is easy to miss a return path
(what already happened in some drivers). Some other call sites miss
to clear crypto_aes_ctx completely.
Provide an aes_zeroize_ctx() helper that can be used with __cleanup()
to automatically zeroize the context when it goes out of scope.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
include/crypto/aes.h | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/include/crypto/aes.h b/include/crypto/aes.h
index 16fbfd93e2bd0..faf1d1b75a15f 100644
--- a/include/crypto/aes.h
+++ b/include/crypto/aes.h
@@ -8,6 +8,7 @@
#include <linux/types.h>
#include <linux/crypto.h>
+#include <linux/string.h>
#define AES_MIN_KEY_SIZE 16
#define AES_MAX_KEY_SIZE 32
@@ -125,6 +126,19 @@ struct crypto_aes_ctx {
u32 key_length;
};
+/**
+ * aes_zeroize_ctx - Clear a crypto_aes_ctx structure
+ * @ctx: The location of the context that should be zeroized
+ *
+ * This function explicitly fills the crypto_aes_ctx with zeroes. For
+ * example, use it with __cleanup() for local crypto_aes_ctx structures on
+ * the stack to avoid that their content is leaked when the context is left.
+ */
+static inline void aes_zeroize_ctx(struct crypto_aes_ctx *ctx)
+{
+ memzero_explicit(ctx, sizeof(*ctx));
+}
+
/*
* validate key length for AES algorithms
*/
--
2.55.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH 02/11] crypto: safexcel - Simplify the check for a valid AES key
2026-08-13 13:49 [PATCH 00/11] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
2026-08-13 13:49 ` [PATCH 01/11] lib/crypto: aes: Provide a wrapper function for zeroizing crypto_aes_ctx Thomas Huth
@ 2026-08-13 13:49 ` Thomas Huth
2026-08-13 13:49 ` [PATCH 03/11] crypto: safexcel - zeroize crypto_aes_ctx with __cleanup(aes_zeroize_ctx) Thomas Huth
` (8 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Thomas Huth @ 2026-08-13 13:49 UTC (permalink / raw)
To: Eric Biggers, Jason A. Donenfeld, Ard Biesheuvel, Antoine Tenart,
Herbert Xu, David S. Miller
Cc: x86, linux-crypto, linux-kernel
From: Thomas Huth <thuth@redhat.com>
safexcel_aead_setkey() currently uses aes_expandkey() to check for a valid
AES key, but then does not use the crypto_aes_ctx afterwards anymore,
i.e. this is just a wasteful way of checking the key length, and thus
aes_check_keylen() should be used instead.
This also fixes a potential leak of sensitive data via the stack, since
this function forgot to zeroize crypto_aes_ctx before returning to the
caller.
Suggested-by: Antoine Tenart <atenart@kernel.org>
Acked-by: Antoine Tenart <atenart@kernel.org>
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
drivers/crypto/inside-secure/safexcel_cipher.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/drivers/crypto/inside-secure/safexcel_cipher.c b/drivers/crypto/inside-secure/safexcel_cipher.c
index a8349b684693e..f07d043c67d45 100644
--- a/drivers/crypto/inside-secure/safexcel_cipher.c
+++ b/drivers/crypto/inside-secure/safexcel_cipher.c
@@ -407,7 +407,6 @@ static int safexcel_aead_setkey(struct crypto_aead *ctfm, const u8 *key,
struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
struct safexcel_crypto_priv *priv = ctx->base.priv;
struct crypto_authenc_keys keys;
- struct crypto_aes_ctx aes;
int err = -EINVAL, i;
const char *alg;
@@ -438,7 +437,7 @@ static int safexcel_aead_setkey(struct crypto_aead *ctfm, const u8 *key,
goto badkey;
break;
case SAFEXCEL_AES:
- err = aes_expandkey(&aes, keys.enckey, keys.enckeylen);
+ err = aes_check_keylen(keys.enckeylen);
if (unlikely(err))
goto badkey;
break;
--
2.55.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH 03/11] crypto: safexcel - zeroize crypto_aes_ctx with __cleanup(aes_zeroize_ctx)
2026-08-13 13:49 [PATCH 00/11] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
2026-08-13 13:49 ` [PATCH 01/11] lib/crypto: aes: Provide a wrapper function for zeroizing crypto_aes_ctx Thomas Huth
2026-08-13 13:49 ` [PATCH 02/11] crypto: safexcel - Simplify the check for a valid AES key Thomas Huth
@ 2026-08-13 13:49 ` Thomas Huth
2026-08-13 13:49 ` [PATCH 04/11] lib/crypto: aes: Provide functions for zeroizing aes_key and aes_enckey Thomas Huth
` (7 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Thomas Huth @ 2026-08-13 13:49 UTC (permalink / raw)
To: Eric Biggers, Jason A. Donenfeld, Ard Biesheuvel, Antoine Tenart,
Herbert Xu, David S. Miller
Cc: x86, linux-crypto, linux-kernel
From: Thomas Huth <thuth@redhat.com>
The code clears the crypto_aes_ctx in most cases already with
memzero_explicit(), but safexcel_skcipher_aesxts_setkey() runs
aes_expandkey() twice, and in case the second call fails, the
context from the first call is leaked.
To fix this issue and to avoid future similar problems, let's use
the new __cleanup(aes_zeroize_ctx) mechanism to make sure that we
always clear the crypto_aes_ctx in all cases.
Acked-by: Antoine Tenart <atenart@kernel.org>
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
drivers/crypto/inside-secure/safexcel_cipher.c | 13 ++++---------
drivers/crypto/inside-secure/safexcel_hash.c | 3 +--
2 files changed, 5 insertions(+), 11 deletions(-)
diff --git a/drivers/crypto/inside-secure/safexcel_cipher.c b/drivers/crypto/inside-secure/safexcel_cipher.c
index f07d043c67d45..b031cb9652ec3 100644
--- a/drivers/crypto/inside-secure/safexcel_cipher.c
+++ b/drivers/crypto/inside-secure/safexcel_cipher.c
@@ -375,7 +375,7 @@ static int safexcel_skcipher_aes_setkey(struct crypto_skcipher *ctfm,
struct crypto_tfm *tfm = crypto_skcipher_tfm(ctfm);
struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
struct safexcel_crypto_priv *priv = ctx->base.priv;
- struct crypto_aes_ctx aes;
+ struct crypto_aes_ctx aes __cleanup(aes_zeroize_ctx);
int ret, i;
ret = aes_expandkey(&aes, key, len);
@@ -396,7 +396,6 @@ static int safexcel_skcipher_aes_setkey(struct crypto_skcipher *ctfm,
ctx->key_len = len;
- memzero_explicit(&aes, sizeof(aes));
return 0;
}
@@ -1361,7 +1360,7 @@ static int safexcel_skcipher_aesctr_setkey(struct crypto_skcipher *ctfm,
struct crypto_tfm *tfm = crypto_skcipher_tfm(ctfm);
struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
struct safexcel_crypto_priv *priv = ctx->base.priv;
- struct crypto_aes_ctx aes;
+ struct crypto_aes_ctx aes __cleanup(aes_zeroize_ctx);
int ret, i;
unsigned int keylen;
@@ -1387,7 +1386,6 @@ static int safexcel_skcipher_aesctr_setkey(struct crypto_skcipher *ctfm,
ctx->key_len = keylen;
- memzero_explicit(&aes, sizeof(aes));
return 0;
}
@@ -2541,7 +2539,7 @@ static int safexcel_skcipher_aesxts_setkey(struct crypto_skcipher *ctfm,
struct crypto_tfm *tfm = crypto_skcipher_tfm(ctfm);
struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
struct safexcel_crypto_priv *priv = ctx->base.priv;
- struct crypto_aes_ctx aes;
+ struct crypto_aes_ctx aes __cleanup(aes_zeroize_ctx);
int ret, i;
unsigned int keylen;
@@ -2589,7 +2587,6 @@ static int safexcel_skcipher_aesxts_setkey(struct crypto_skcipher *ctfm,
ctx->key_len = keylen << 1;
- memzero_explicit(&aes, sizeof(aes));
return 0;
}
@@ -2755,12 +2752,11 @@ static int safexcel_aead_ccm_setkey(struct crypto_aead *ctfm, const u8 *key,
struct crypto_tfm *tfm = crypto_aead_tfm(ctfm);
struct safexcel_cipher_ctx *ctx = crypto_tfm_ctx(tfm);
struct safexcel_crypto_priv *priv = ctx->base.priv;
- struct crypto_aes_ctx aes;
+ struct crypto_aes_ctx aes __cleanup(aes_zeroize_ctx);
int ret, i;
ret = aes_expandkey(&aes, key, len);
if (ret) {
- memzero_explicit(&aes, sizeof(aes));
return ret;
}
@@ -2789,7 +2785,6 @@ static int safexcel_aead_ccm_setkey(struct crypto_aead *ctfm, const u8 *key,
else
ctx->hash_alg = CONTEXT_CONTROL_CRYPTO_ALG_XCBC128;
- memzero_explicit(&aes, sizeof(aes));
return 0;
}
diff --git a/drivers/crypto/inside-secure/safexcel_hash.c b/drivers/crypto/inside-secure/safexcel_hash.c
index 3402e570d045c..20c17eb09495e 100644
--- a/drivers/crypto/inside-secure/safexcel_hash.c
+++ b/drivers/crypto/inside-secure/safexcel_hash.c
@@ -1905,7 +1905,7 @@ static int safexcel_cbcmac_setkey(struct crypto_ahash *tfm, const u8 *key,
unsigned int len)
{
struct safexcel_ahash_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
- struct crypto_aes_ctx aes;
+ struct crypto_aes_ctx aes __cleanup(aes_zeroize_ctx);
int ret, i;
ret = aes_expandkey(&aes, key, len);
@@ -1928,7 +1928,6 @@ static int safexcel_cbcmac_setkey(struct crypto_ahash *tfm, const u8 *key,
}
ctx->cbcmac = true;
- memzero_explicit(&aes, sizeof(aes));
return 0;
}
--
2.55.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH 04/11] lib/crypto: aes: Provide functions for zeroizing aes_key and aes_enckey
2026-08-13 13:49 [PATCH 00/11] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (2 preceding siblings ...)
2026-08-13 13:49 ` [PATCH 03/11] crypto: safexcel - zeroize crypto_aes_ctx with __cleanup(aes_zeroize_ctx) Thomas Huth
@ 2026-08-13 13:49 ` Thomas Huth
2026-08-13 13:49 ` [PATCH 05/11] lib/crypto: aes: Use aes_zeroize_*key() instead of memzero_explicit() Thomas Huth
` (6 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Thomas Huth @ 2026-08-13 13:49 UTC (permalink / raw)
To: Eric Biggers, Jason A. Donenfeld, Ard Biesheuvel, Herbert Xu,
David S. Miller
Cc: x86, linux-crypto, linux-kernel
From: Thomas Huth <thuth@redhat.com>
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.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
include/crypto/aes.h | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/include/crypto/aes.h b/include/crypto/aes.h
index faf1d1b75a15f..d00d88b71690b 100644
--- a/include/crypto/aes.h
+++ b/include/crypto/aes.h
@@ -102,6 +102,19 @@ struct aes_enckey {
union aes_enckey_arch k;
};
+/**
+ * aes_zeroize_enckey() - Zeroize an aes_enckey structure
+ * @key: The location of the key structure that should be zeroized
+ *
+ * Explicitly fills the aes_enckey with zeroes. For example, use it with
+ * __cleanup() for local aes_enckey structures on the stack, so that their
+ * content is not leaked when the context is left.
+ */
+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
@@ -116,6 +129,19 @@ struct aes_key {
union aes_invkey_arch inv_k;
};
+/**
+ * aes_zeroize_key() - Zeroize an aes_key structure
+ * @key: The location of the key structure that should be zeroized
+ *
+ * Explicitly fills the aes_key with zeroes. For example, use it with
+ * __cleanup() for local aes_key structures on the stack, so that their
+ * content is not leaked when the context is left.
+ */
+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!
--
2.55.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH 05/11] lib/crypto: aes: Use aes_zeroize_*key() instead of memzero_explicit()
2026-08-13 13:49 [PATCH 00/11] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (3 preceding siblings ...)
2026-08-13 13:49 ` [PATCH 04/11] lib/crypto: aes: Provide functions for zeroizing aes_key and aes_enckey Thomas Huth
@ 2026-08-13 13:49 ` Thomas Huth
2026-08-13 13:49 ` [PATCH 06/11] lib/crypto: md5: Provide a function for zeroizing hmac_md5_ctx structures Thomas Huth
` (5 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Thomas Huth @ 2026-08-13 13:49 UTC (permalink / raw)
To: Eric Biggers, Jason A. Donenfeld, Ard Biesheuvel
Cc: x86, Herbert Xu, David S. Miller, linux-crypto, linux-kernel
From: Thomas Huth <thuth@redhat.com>
It's only cosmetics here, but since we have the new aes_zeroize_key()
and aes_zeroize_enckey() functions anyway, we can also use them here.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
lib/crypto/aes.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
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 related [flat|nested] 12+ messages in thread
* [PATCH 06/11] lib/crypto: md5: Provide a function for zeroizing hmac_md5_ctx structures
2026-08-13 13:49 [PATCH 00/11] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (4 preceding siblings ...)
2026-08-13 13:49 ` [PATCH 05/11] lib/crypto: aes: Use aes_zeroize_*key() instead of memzero_explicit() Thomas Huth
@ 2026-08-13 13:49 ` Thomas Huth
2026-08-13 13:49 ` [PATCH 07/11] lib/crypto: md5: Use hmac_md5_zeroize_ctx() instead of memzero_explicit() Thomas Huth
` (4 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Thomas Huth @ 2026-08-13 13:49 UTC (permalink / raw)
To: Eric Biggers, Jason A. Donenfeld, Ard Biesheuvel, Herbert Xu,
David S. Miller
Cc: x86, linux-crypto, linux-kernel
From: Thomas Huth <thuth@redhat.com>
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@redhat.com>
---
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
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH 07/11] lib/crypto: md5: Use hmac_md5_zeroize_ctx() instead of memzero_explicit()
2026-08-13 13:49 [PATCH 00/11] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (5 preceding siblings ...)
2026-08-13 13:49 ` [PATCH 06/11] lib/crypto: md5: Provide a function for zeroizing hmac_md5_ctx structures Thomas Huth
@ 2026-08-13 13:49 ` Thomas Huth
2026-08-13 13:49 ` [PATCH 08/11] lib/crypto: sha1: Provide a wrapper for zeroizing hmac_sha1_ctx Thomas Huth
` (3 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Thomas Huth @ 2026-08-13 13:49 UTC (permalink / raw)
To: Eric Biggers, Jason A. Donenfeld, Ard Biesheuvel
Cc: x86, Herbert Xu, David S. Miller, linux-crypto, linux-kernel
From: Thomas Huth <thuth@redhat.com>
It's only cosmetics, but since we have the new hmac_md5_zeroize_ctx()
function anyway, we can also use it here.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
lib/crypto/md5.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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 related [flat|nested] 12+ messages in thread
* [PATCH 08/11] lib/crypto: sha1: Provide a wrapper for zeroizing hmac_sha1_ctx
2026-08-13 13:49 [PATCH 00/11] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (6 preceding siblings ...)
2026-08-13 13:49 ` [PATCH 07/11] lib/crypto: md5: Use hmac_md5_zeroize_ctx() instead of memzero_explicit() Thomas Huth
@ 2026-08-13 13:49 ` Thomas Huth
2026-08-13 13:49 ` [PATCH 09/11] lib/crypto: sha1: Use hmac_sha1_zeroize_ctx() instead of memzero_explicit() Thomas Huth
` (2 subsequent siblings)
10 siblings, 0 replies; 12+ messages in thread
From: Thomas Huth @ 2026-08-13 13:49 UTC (permalink / raw)
To: Eric Biggers, Jason A. Donenfeld, Ard Biesheuvel, Herbert Xu,
David S. Miller
Cc: x86, linux-crypto, linux-kernel
From: Thomas Huth <thuth@redhat.com>
Some kernel code needs to zeroize their local hmac_sha1_ctx structures
after use to avoid leaking sensitive material on the stack.
Provide an hmac_sha1_zeroize_ctx() helper that can be used with __cleanup()
to automatically zeroize the context when it goes out of scope.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
include/crypto/sha1.h | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/include/crypto/sha1.h b/include/crypto/sha1.h
index 4d973e016cd69..888dfc62e1c65 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
@@ -106,6 +107,22 @@ struct hmac_sha1_ctx {
struct sha1_block_state ostate;
};
+/**
+ * hmac_sha1_zeroize_ctx() - Zeroize an hmac_sha1_ctx structure
+ * @ctx: The location of the context that should be zeroized
+ *
+ * This function explicitly fills the hmac_sha1_ctx with zeroes. For
+ * example, it can be used with __cleanup() for local hmac_sha1_ctx
+ * structures on the stack, so that their content is not leaked via the
+ * stack when the context is left. Note: This is only required when not
+ * using hmac_sha1_final() that already zeroizes the structure at the
+ * end.
+ */
+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
--
2.55.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH 09/11] lib/crypto: sha1: Use hmac_sha1_zeroize_ctx() instead of memzero_explicit()
2026-08-13 13:49 [PATCH 00/11] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (7 preceding siblings ...)
2026-08-13 13:49 ` [PATCH 08/11] lib/crypto: sha1: Provide a wrapper for zeroizing hmac_sha1_ctx Thomas Huth
@ 2026-08-13 13:49 ` Thomas Huth
2026-08-13 13:49 ` [PATCH 10/11] x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY Thomas Huth
2026-08-13 13:49 ` [PATCH 11/11] lib/crypto: sha2: Provide wrappers for zeroizing SHA2 hmac_sha*_ctx structures Thomas Huth
10 siblings, 0 replies; 12+ messages in thread
From: Thomas Huth @ 2026-08-13 13:49 UTC (permalink / raw)
To: Eric Biggers, Jason A. Donenfeld, Ard Biesheuvel
Cc: x86, Herbert Xu, David S. Miller, linux-crypto, linux-kernel
From: Thomas Huth <thuth@redhat.com>
It's only cosmetics, but since we have the new hmac_sha1_zeroize_ctx()
function anyway, we can also use it here.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
lib/crypto/sha1.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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 related [flat|nested] 12+ messages in thread
* [PATCH 10/11] x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY
2026-08-13 13:49 [PATCH 00/11] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (8 preceding siblings ...)
2026-08-13 13:49 ` [PATCH 09/11] lib/crypto: sha1: Use hmac_sha1_zeroize_ctx() instead of memzero_explicit() Thomas Huth
@ 2026-08-13 13:49 ` Thomas Huth
2026-08-13 13:49 ` [PATCH 11/11] lib/crypto: sha2: Provide wrappers for zeroizing SHA2 hmac_sha*_ctx structures Thomas Huth
10 siblings, 0 replies; 12+ messages in thread
From: Thomas Huth @ 2026-08-13 13:49 UTC (permalink / raw)
To: Eric Biggers, Jason A. Donenfeld, Ard Biesheuvel, Thomas Gleixner,
Ingo Molnar, Borislav Petkov, Dave Hansen, x86
Cc: Herbert Xu, David S. Miller, linux-crypto, linux-kernel,
H. Peter Anvin
From: Thomas Huth <thuth@redhat.com>
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>.
The next patch will add #include <linux/string.h> to sha2.h to be able
to use memzero_explicit() there, so we have to compile the code in
purgatory.c with -D__NO_FORTIFY to avoid compilation problems in this
file.
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 related [flat|nested] 12+ messages in thread
* [PATCH 11/11] lib/crypto: sha2: Provide wrappers for zeroizing SHA2 hmac_sha*_ctx structures
2026-08-13 13:49 [PATCH 00/11] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
` (9 preceding siblings ...)
2026-08-13 13:49 ` [PATCH 10/11] x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY Thomas Huth
@ 2026-08-13 13:49 ` Thomas Huth
10 siblings, 0 replies; 12+ messages in thread
From: Thomas Huth @ 2026-08-13 13:49 UTC (permalink / raw)
To: Eric Biggers, Jason A. Donenfeld, Ard Biesheuvel, Herbert Xu,
David S. Miller
Cc: x86, linux-crypto, linux-kernel
From: Thomas Huth <thuth@redhat.com>
Some crypto code functions need to zeroize their local SHA2 hmac_sha*_ctx
structures after use to avoid leaking sensitive material on the stack.
Provide hmac_sha*_zeroize_ctx() helper functions that can be used with
__cleanup() to automatically zeroize the context when it goes out of
scope.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
Note: These will be useful in some spots in the fs/smb/ code later.
include/crypto/sha2.h | 57 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 57 insertions(+)
diff --git a/include/crypto/sha2.h b/include/crypto/sha2.h
index 7bb8fe169daf2..2b2b06ebff993 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
@@ -218,6 +219,20 @@ struct hmac_sha224_ctx {
struct __hmac_sha256_ctx ctx;
};
+/**
+ * hmac_sha224_zeroize_ctx() - Zeroize an hmac_sha224_ctx structure
+ * @ctx: The location of the context that should be zeroized
+ *
+ * This function explicitly fills the hmac_sha224_ctx with zeroes. For
+ * example, use it with __cleanup() for local hmac_sha224_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_sha224_final().
+ */
+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
@@ -422,6 +437,20 @@ struct hmac_sha256_ctx {
struct __hmac_sha256_ctx ctx;
};
+/**
+ * hmac_sha256_zeroize_ctx() - Zeroize an hmac_sha256_ctx structure
+ * @ctx: The location of the context that should be zeroized
+ *
+ * This function explicitly fills the hmac_sha256_ctx with zeroes. For
+ * example, use it with __cleanup() for local hmac_sha256_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_sha256_final().
+ */
+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
@@ -631,6 +660,20 @@ struct hmac_sha384_ctx {
struct __hmac_sha512_ctx ctx;
};
+/**
+ * hmac_sha384_zeroize_ctx() - Zeroize an hmac_sha384_ctx structure
+ * @ctx: The location of the context that should be zeroized
+ *
+ * This function explicitly fills the hmac_sha384_ctx with zeroes. For
+ * example, use it with __cleanup() for local hmac_sha384_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_sha384_final().
+ */
+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
@@ -806,6 +849,20 @@ struct hmac_sha512_ctx {
struct __hmac_sha512_ctx ctx;
};
+/**
+ * hmac_sha512_zeroize_ctx() - Zeroize an hmac_sha512_ctx structure
+ * @ctx: The location of the context that should be zeroized
+ *
+ * This function explicitly fills the hmac_sha512_ctx with zeroes. For
+ * example, use it with __cleanup() for local hmac_sha512_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_sha512_final().
+ */
+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 related [flat|nested] 12+ messages in thread
end of thread, other threads:[~2026-08-13 13:51 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-13 13:49 [PATCH 00/11] libcrypto: Provide more __cleanup functions for zeroizing data Thomas Huth
2026-08-13 13:49 ` [PATCH 01/11] lib/crypto: aes: Provide a wrapper function for zeroizing crypto_aes_ctx Thomas Huth
2026-08-13 13:49 ` [PATCH 02/11] crypto: safexcel - Simplify the check for a valid AES key Thomas Huth
2026-08-13 13:49 ` [PATCH 03/11] crypto: safexcel - zeroize crypto_aes_ctx with __cleanup(aes_zeroize_ctx) Thomas Huth
2026-08-13 13:49 ` [PATCH 04/11] lib/crypto: aes: Provide functions for zeroizing aes_key and aes_enckey Thomas Huth
2026-08-13 13:49 ` [PATCH 05/11] lib/crypto: aes: Use aes_zeroize_*key() instead of memzero_explicit() Thomas Huth
2026-08-13 13:49 ` [PATCH 06/11] lib/crypto: md5: Provide a function for zeroizing hmac_md5_ctx structures Thomas Huth
2026-08-13 13:49 ` [PATCH 07/11] lib/crypto: md5: Use hmac_md5_zeroize_ctx() instead of memzero_explicit() Thomas Huth
2026-08-13 13:49 ` [PATCH 08/11] lib/crypto: sha1: Provide a wrapper for zeroizing hmac_sha1_ctx Thomas Huth
2026-08-13 13:49 ` [PATCH 09/11] lib/crypto: sha1: Use hmac_sha1_zeroize_ctx() instead of memzero_explicit() Thomas Huth
2026-08-13 13:49 ` [PATCH 10/11] x86/purgatory: Compile purgatory.c with -D__NO_FORTIFY Thomas Huth
2026-08-13 13:49 ` [PATCH 11/11] lib/crypto: sha2: Provide wrappers for zeroizing SHA2 hmac_sha*_ctx structures Thomas Huth
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox