[PATCH v2 04/13] lib/crypto: aes-ccm: Provide functions for zeroizing aes_ccm* structures

From: Thomas Huth

Date: Wed Sep 09 2026 - 08:11:51 EST


In certain cases crypto code needs to zeroize their local aes_ccm_key
or aes_ccm_ctx structures after use to avoid leaking sensitive material.
Provide aes_ccm_zeroize_key() and aes_ccm_zeroize_ctx() helper functions
that e.g. can be used with __cleanup() to automatically zeroize the
structures when they go out of scope.

While we're at it, replace the related memzero_explicit() calls in
lib/crypto/aes.c with calls to the new helper functions.

Signed-off-by: Thomas Huth <thuth@xxxxxxxxxx>
---
include/crypto/aes-ccm.h | 22 ++++++++++++++++++++--
lib/crypto/aes.c | 8 +++-----
2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/include/crypto/aes-ccm.h b/include/crypto/aes-ccm.h
index 8b00859ac4d6b..c52982dd91d2c 100644
--- a/include/crypto/aes-ccm.h
+++ b/include/crypto/aes-ccm.h
@@ -18,6 +18,15 @@ struct aes_ccm_key {
size_t authtag_len; /* Length of authentication tags in bytes */
};

+/**
+ * aes_ccm_zeroize_key() - Zeroize an aes_ccm_key structure
+ * @key: The aes_ccm_key to zeroize
+ */
+static inline void aes_ccm_zeroize_key(struct aes_ccm_key *key)
+{
+ memzero_explicit(key, sizeof(*key));
+}
+
/**
* struct aes_ccm_ctx - Context for incrementally en/decrypting a message
*/
@@ -50,6 +59,15 @@ struct aes_ccm_ctx {
bool ad_padded;
};

+/**
+ * aes_ccm_zeroize_ctx() - Zeroize an aes_ccm_ctx structure
+ * @ctx: The aes_ccm_ctx to zeroize
+ */
+static inline void aes_ccm_zeroize_ctx(struct aes_ccm_ctx *ctx)
+{
+ memzero_explicit(ctx, sizeof(*ctx));
+}
+
/**
* aes_ccm_preparekey() - Prepare a key for AES-CCM encryption and decryption
* @key: (output) The key structure to initialize
@@ -58,8 +76,8 @@ struct aes_ccm_ctx {
* @authtag_len: Length of the authentication tag in bytes:
* 4, 6, 8, 10, 12, 14, or 16. 16 is recommended.
*
- * Users should use memzero_explicit() to zeroize the key struct at the end of
- * its lifetime. (But if this function fails, zeroization is unnecessary.)
+ * Users should use aes_ccm_zeroize_key() to zeroize the key struct at the end
+ * of its lifetime. (But if this function fails, zeroization is unnecessary.)
*
* Context: Any context.
* Return:
diff --git a/lib/crypto/aes.c b/lib/crypto/aes.c
index 0cb5d7355926e..2d29adca79532 100644
--- a/lib/crypto/aes.c
+++ b/lib/crypto/aes.c
@@ -2011,7 +2011,7 @@ void aes_ccm_encrypt_final(struct aes_ccm_ctx *ctx, u8 *authtag)
if (ctx->partial_len)
aes_encrypt(&ctx->key->aes, ctx->mac, ctx->mac);
crypto_xor_cpy(authtag, ctx->mac, ctx->s0, ctx->key->authtag_len);
- memzero_explicit(ctx, sizeof(*ctx));
+ aes_ccm_zeroize_ctx(ctx);
}
EXPORT_SYMBOL_GPL(aes_ccm_encrypt_final);

@@ -2032,7 +2032,7 @@ int aes_ccm_decrypt_final(struct aes_ccm_ctx *ctx, const u8 *authtag)
-EBADMSG :
0;
out:
- memzero_explicit(ctx, sizeof(*ctx));
+ aes_ccm_zeroize_ctx(ctx);
return err;
}
EXPORT_SYMBOL_GPL(aes_ccm_decrypt_final);
@@ -2084,7 +2084,7 @@ static void __init aes_ccm_fips_test(void)
const size_t data_len = sizeof(fips_test_data);
const size_t nonce_len = 13;
u8 buf[sizeof(fips_test_data) + AES_BLOCK_SIZE];
- struct aes_ccm_key key;
+ struct aes_ccm_key key __cleanup(aes_ccm_zeroize_key);
int err;

if (aes_ccm_preparekey(&key, fips_test_key, sizeof(fips_test_key),
@@ -2106,8 +2106,6 @@ static void __init aes_ccm_fips_test(void)
panic("aes: CCM FIPS self-test failed (decryption failed)\n");
if (memcmp(fips_test_data, buf, data_len) != 0)
panic("aes: CCM FIPS self-test failed (wrong plaintext)\n");
-
- memzero_explicit(&key, sizeof(key));
}
#else /* CONFIG_CRYPTO_LIB_AES_CCM */
static inline void aes_ccm_fips_test(void)
--
2.55.0