From: Herbert Xu <herbert@gondor.apana.org.au>
To: Linux Crypto Mailing List <linux-crypto@vger.kernel.org>,
Pavitrakumar Managutte <pavitrakumarm@vayavyalabs.com>
Subject: [PATCH] crypto: aes - Add import_core/export_core to cmac/xcbc/cbcmac
Date: Fri, 9 Oct 2026 22:45:55 +1100 [thread overview]
Message-ID: <asjT87HAZ_UUbbvm@gondor.apana.org.au> (raw)
The export format for cmac-aes-lib, xcbc-aes-lib and cbcmac-aes-lib
did not match that of the generic templates. Add custom export and
import functions to align with the generic tempaltes.
For cmac and xcbc, perform an extra encryption on the withheld block
for export_core so that the information isn't lost.
Reported-by: Pavitrakumar Managutte <pavitrakumarm@vayavyalabs.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
diff --git a/crypto/aes.c b/crypto/aes.c
index 94791f481e98..a1af4d5a7608 100644
--- a/crypto/aes.c
+++ b/crypto/aes.c
@@ -18,7 +18,10 @@
#include <crypto/internal/hash.h>
#include <crypto/internal/skcipher.h>
#include <crypto/scatterwalk.h>
+#include <crypto/utils.h>
+#include <linux/kernel.h>
#include <linux/module.h>
+#include <linux/string.h>
static_assert(__alignof__(struct aes_key) <= CRYPTO_MINALIGN);
static_assert(__alignof__(struct aes_enckey) <= CRYPTO_MINALIGN);
@@ -95,6 +98,71 @@ static int __maybe_unused crypto_aes_cmac_digest(struct shash_desc *desc,
return 0;
}
+static int crypto_cmac_export_core(struct shash_desc *desc, void *out)
+{
+ struct aes_cmac_ctx *ctx = AES_CMAC_CTX(desc);
+ struct aes_cmac_ctx ctx1;
+ u8 *h = ctx->h;
+
+ if (!ctx->partial_len)
+ ;
+ else if (ctx->partial_len != AES_BLOCK_SIZE)
+ return -EINVAL;
+ else {
+ union {
+ long l;
+ char c;
+ } u = {};
+
+ BUILD_BUG_ON(sizeof(u) >= AES_BLOCK_SIZE);
+ ctx1 = *ctx;
+ aes_cmac_update(&ctx1, &u.c, sizeof(u));
+ h = ctx1.h;
+ }
+
+ memcpy(out, h, AES_BLOCK_SIZE);
+ memzero_explicit(&ctx1, sizeof(ctx1));
+ return 0;
+}
+
+static int crypto_cmac_import_core(struct shash_desc *desc, const void *in)
+{
+ struct aes_cmac_ctx *ctx = AES_CMAC_CTX(desc);
+
+ ctx->key = AES_CMAC_KEY(desc->tfm);
+ memcpy(ctx->h, in, AES_BLOCK_SIZE);
+ ctx->partial_len = 0;
+ return 0;
+}
+
+static int crypto_cmac_export(struct shash_desc *desc, void *out)
+{
+ struct aes_cmac_ctx *ctx = AES_CMAC_CTX(desc);
+ unsigned int partial = ctx->partial_len;
+ u8 *p = out;
+
+ memcpy(p, ctx->h, AES_BLOCK_SIZE);
+ memset(p + AES_BLOCK_SIZE, 0, AES_BLOCK_SIZE);
+ p[AES_BLOCK_SIZE * 2] = partial;
+ return 0;
+}
+
+static int crypto_cmac_import(struct shash_desc *desc, const void *in)
+{
+ struct aes_cmac_ctx *ctx = AES_CMAC_CTX(desc);
+ unsigned int partial;
+ const u8 *p = in;
+
+ crypto_cmac_import_core(desc, p);
+ p += AES_BLOCK_SIZE;
+ partial = p[AES_BLOCK_SIZE];
+ if (partial > AES_BLOCK_SIZE)
+ return -EOVERFLOW;
+ crypto_xor(ctx->h, p, partial);
+ ctx->partial_len = partial;
+ return 0;
+}
+
#define AES_CBCMAC_KEY(tfm) ((struct aes_enckey *)crypto_shash_ctx(tfm))
#define AES_CBCMAC_CTX(desc) ((struct aes_cbcmac_ctx *)shash_desc_ctx(desc))
@@ -136,6 +204,49 @@ static int __maybe_unused crypto_aes_cbcmac_digest(struct shash_desc *desc,
return 0;
}
+static int crypto_cbcmac_export_core(struct shash_desc *desc, void *out)
+{
+ memcpy(out, AES_CBCMAC_CTX(desc)->h, AES_BLOCK_SIZE);
+ return 0;
+}
+
+static int crypto_cbcmac_import_core(struct shash_desc *desc, const void *in)
+{
+ struct aes_cbcmac_ctx *ctx = AES_CBCMAC_CTX(desc);
+
+ ctx->key = AES_CBCMAC_KEY(desc->tfm);
+ memcpy(ctx->h, in, AES_BLOCK_SIZE);
+ ctx->partial_len = 0;
+ return 0;
+}
+
+static int crypto_cbcmac_export(struct shash_desc *desc, void *out)
+{
+ struct aes_cbcmac_ctx *ctx = AES_CBCMAC_CTX(desc);
+ unsigned int partial = ctx->partial_len;
+ u8 *p = out;
+
+ memset(p + AES_BLOCK_SIZE, 0, AES_BLOCK_SIZE);
+ p[AES_BLOCK_SIZE * 2] = partial;
+ return crypto_cbcmac_export_core(desc, p);
+}
+
+static int crypto_cbcmac_import(struct shash_desc *desc, const void *in)
+{
+ struct aes_cbcmac_ctx *ctx = AES_CBCMAC_CTX(desc);
+ unsigned int partial;
+ const u8 *p = in;
+
+ crypto_cbcmac_import_core(desc, p);
+ p += AES_BLOCK_SIZE;
+ partial = p[AES_BLOCK_SIZE];
+ if (partial > AES_BLOCK_SIZE)
+ return -EOVERFLOW;
+ crypto_xor(ctx->h, p, partial);
+ ctx->partial_len = partial;
+ return 0;
+}
+
static struct crypto_alg alg = {
.cra_name = "aes",
.cra_driver_name = "aes-lib",
@@ -166,7 +277,12 @@ static struct shash_alg mac_algs[] = {
.update = crypto_aes_cmac_update,
.final = crypto_aes_cmac_final,
.digest = crypto_aes_cmac_digest,
+ .export = crypto_cmac_export,
+ .import = crypto_cmac_import,
+ .export_core = crypto_cmac_export_core,
+ .import_core = crypto_cmac_import_core,
.descsize = sizeof(struct aes_cmac_ctx),
+ .statesize = AES_BLOCK_SIZE * 2 + 1,
},
#endif
#if IS_ENABLED(CONFIG_CRYPTO_XCBC)
@@ -187,7 +303,12 @@ static struct shash_alg mac_algs[] = {
.update = crypto_aes_cmac_update,
.final = crypto_aes_cmac_final,
.digest = crypto_aes_cmac_digest,
+ .export = crypto_cmac_export,
+ .import = crypto_cmac_import,
+ .export_core = crypto_cmac_export_core,
+ .import_core = crypto_cmac_import_core,
.descsize = sizeof(struct aes_cmac_ctx),
+ .statesize = AES_BLOCK_SIZE * 2 + 1,
},
#endif
#if IS_ENABLED(CONFIG_CRYPTO_CCM)
@@ -204,7 +325,12 @@ static struct shash_alg mac_algs[] = {
.update = crypto_aes_cbcmac_update,
.final = crypto_aes_cbcmac_final,
.digest = crypto_aes_cbcmac_digest,
+ .export = crypto_cbcmac_export,
+ .import = crypto_cbcmac_import,
+ .export_core = crypto_cbcmac_export_core,
+ .import_core = crypto_cbcmac_import_core,
.descsize = sizeof(struct aes_cbcmac_ctx),
+ .statesize = AES_BLOCK_SIZE * 2 + 1,
},
#endif
};
--
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
next reply other threads:[~2026-10-09 11:46 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-09 11:45 Herbert Xu [this message]
2026-10-10 0:11 ` [PATCH] crypto: aes - Add import_core/export_core to cmac/xcbc/cbcmac kernel test robot
2026-10-12 10:41 ` Pavitrakumar Managutte
2026-10-15 17:42 ` Pavitrakumar Managutte
2026-10-15 18:34 ` Eric Biggers
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=asjT87HAZ_UUbbvm@gondor.apana.org.au \
--to=herbert@gondor.apana.org.au \
--cc=linux-crypto@vger.kernel.org \
--cc=pavitrakumarm@vayavyalabs.com \
/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