Re: [PATCH v5 01/10] crypto: 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]>
On 10/08/2026 11.29, Thomas Huth wrote:
> 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.
> 
> Acked-by: Eric Biggers <[email protected]>
> Signed-off-by: Thomas Huth <[email protected]>
> ---
>   include/crypto/aes.h | 13 +++++++++++++
>   1 file changed, 13 insertions(+)
> 
> diff --git a/include/crypto/aes.h b/include/crypto/aes.h
> index 3279cfa546085..e70607249ae5c 100644
> --- a/include/crypto/aes.h
> +++ b/include/crypto/aes.h
> @@ -159,6 +159,19 @@ static inline int aes_check_keylen(size_t keylen)
>   int aes_expandkey(struct crypto_aes_ctx *ctx, const u8 *in_key,
>   		  unsigned int key_len);
>   
> +/**
> + * aes_zeroize_ctx - Clear a crypto_aes_ctx structure
> + * @ctx:	The location of the context that should be zeroized
> + *
> + * Explicitly fills the crypto_aes_ctx with zeroes. This should be done
> + * once the context is not required anymore to avoid that its contents
> + * are leaked on the stack or heap (if not using kfree_sensitive()).
> + */
> +static inline void aes_zeroize_ctx(struct crypto_aes_ctx *ctx)
> +{
> +	memzero_explicit(ctx, sizeof(*ctx));
> +}
  Hi Herbert!

Just a quick note: I've included an updated version of this patch in the 
series that targets libcrypto here:

  https://lore.kernel.org/linux-crypto/[email protected]/

... so I think I'll best resubmit the remaining patches of this series here 
once the libcrypto series got accepted.

  Thomas
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.