All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] crypto: aes - Add import_core/export_core to cmac/xcbc/cbcmac
@ 2026-10-09 11:45 Herbert Xu
  2026-10-10  0:11 ` kernel test robot
  2026-10-12 10:41 ` Pavitrakumar Managutte
  0 siblings, 2 replies; 5+ messages in thread
From: Herbert Xu @ 2026-10-09 11:45 UTC (permalink / raw)
  To: Linux Crypto Mailing List, Pavitrakumar Managutte

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

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-10-15 18:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-09 11:45 [PATCH] crypto: aes - Add import_core/export_core to cmac/xcbc/cbcmac Herbert Xu
2026-10-10  0:11 ` kernel test robot
2026-10-12 10:41 ` Pavitrakumar Managutte
2026-10-15 17:42   ` Pavitrakumar Managutte
2026-10-15 18:34     ` Eric Biggers

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.