[PATCH 04/11] lib/crypto: aes: Provide functions for zeroizing aes_key and aes_enckey

Thomas Huth <[email protected]>
Newsgroups org.kernel.vger.linux-crypto,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
From: Thomas Huth <[email protected]>

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 <[email protected]>
---
 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.