[PATCH 01/11] lib/crypto: aes: Provide a wrapper function for zeroizing crypto_aes_ctx

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

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 <[email protected]>
---
 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
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.