The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Thomas Huth <thuth@redhat.com>
To: Herbert Xu <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Biggers <ebiggers@kernel.org>
Cc: linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 1/6] crypto: Provide wrapper functions for zeroizing aes_cmac_key and aes_cmac_ctx
Date: Wed,  5 Aug 2026 16:36:04 +0200	[thread overview]
Message-ID: <20260805143611.818559-2-thuth@redhat.com> (raw)
In-Reply-To: <20260805143611.818559-1-thuth@redhat.com>

From: Thomas Huth <thuth@redhat.com>

Code that uses AES-CMAC might need to zeroize their local aes_cmac_key
and/or aes_cmac_ctx structures after use to avoid leaking sensitive
material on the stack.

Provide an aes_cmac_zeroize_key() and an aes_cmac_zeroize_ctx() helper
function that can be used with __cleanup() to automatically clear
the key and context when they go out of scope.

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 include/crypto/aes-cbc-macs.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/include/crypto/aes-cbc-macs.h b/include/crypto/aes-cbc-macs.h
index e61df108b926d..b2f3b8be7886b 100644
--- a/include/crypto/aes-cbc-macs.h
+++ b/include/crypto/aes-cbc-macs.h
@@ -109,6 +109,32 @@ void aes_cmac_update(struct aes_cmac_ctx *ctx, const u8 *data, size_t data_len);
  */
 void aes_cmac_final(struct aes_cmac_ctx *ctx, u8 out[at_least AES_BLOCK_SIZE]);
 
+/**
+ * aes_cmac_zeroize_key - Clear a aes_cmac_key structure
+ * @ctx: The location of the key structure that should be zeroized
+ *
+ * Explicitly fills the aes_cmac_key with zeroes. This should be done once
+ * the key is not required anymore to avoid that its contents are leaked
+ * on the stack or heap.
+ */
+static inline void aes_cmac_zeroize_key(struct aes_cmac_key *key)
+{
+	memzero_explicit(key, sizeof(*key));
+}
+
+/**
+ * aes_cmac_zeroize_ctx - Clear a aes_cmac_ctx structure
+ * @ctx: The location of the context that should be zeroized
+ *
+ * Explicitly fills the aes_cmac_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. Only required if not using aes_cmac_final().
+ */
+static inline void aes_cmac_zeroize_ctx(struct aes_cmac_ctx *ctx)
+{
+	memzero_explicit(ctx, sizeof(*ctx));
+}
+
 /**
  * aes_cmac() - Compute AES-CMAC or AES-XCBC-MAC in one shot
  * @key: The key to use
-- 
2.55.0


  reply	other threads:[~2026-08-05 14:36 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-05 14:36 [PATCH 0/6] crypto: Add __cleanup functions for zeroizing aes_cmac_key & aes_cmac_ctx Thomas Huth
2026-08-05 14:36 ` Thomas Huth [this message]
2026-08-05 20:37   ` [PATCH 1/6] crypto: Provide wrapper functions for zeroizing aes_cmac_key and aes_cmac_ctx Eric Biggers
2026-08-05 20:46   ` Eric Biggers
2026-08-05 14:36 ` [PATCH 2/6] smb: clear the aes_cmac_key and aes_cmac_ctx when done Thomas Huth
2026-08-05 21:12   ` Eric Biggers
2026-08-05 14:36 ` [PATCH 3/6] net/tcp-ao: clear the aes_cmac_key " Thomas Huth
2026-08-05 21:02   ` Eric Biggers
2026-08-06 13:06     ` Thomas Huth
2026-08-05 14:36 ` [PATCH 4/6] Bluetooth: SMP: " Thomas Huth
2026-08-05 20:46   ` Eric Biggers
2026-08-06 13:00     ` Thomas Huth
2026-08-05 14:36 ` [PATCH 5/6] lib/crypto: aes: Use _cleanup() for aes_cmac_key instead of memzero_explicit() Thomas Huth
2026-08-05 14:36 ` [PATCH 6/6] mac80211: fils_aead: Use _cleanup for aes_cmac_key instead of memzero_explicit Thomas Huth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260805143611.818559-2-thuth@redhat.com \
    --to=thuth@redhat.com \
    --cc=davem@davemloft.net \
    --cc=ebiggers@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox