[PATCH 04/11] lib/crypto: aes: Provide functions for zeroizing aes_key and aes_enckey
From: Thomas Huth
Date: Thu Aug 13 2026 - 09:53:51 EST
From: Thomas Huth <thuth@xxxxxxxxxx>
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@xxxxxxxxxx>
---
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