From: Rosen Penev <rosenp@gmail.com>
To: linux-crypto@vger.kernel.org, Herbert Xu <herbert@gondor.apana.org.au>
Cc: Srujana Challa <schalla@marvell.com>,
Bharat Bhushan <bbhushan2@marvell.com>,
"David S. Miller" <davem@davemloft.net>,
Rosen Penev <rosenp@gmail.com>,
Arnaud Ebalard <arno@natisbad.org>,
Boris Brezillon <bbrezillon@kernel.org>,
Russell King <rmk+kernel@arm.linux.org.uk>,
Romain Perier <romain.perier@free-electrons.com>,
stable@vger.kernel.org
Subject: [PATCH 4/8] crypto: cesa - finish the HMAC outer hash when padding by hand
Date: Fri, 27 Nov 2026 20:40:46 -0800 [thread overview]
Message-ID: <20261128044050.722617-5-rosenp@gmail.com> (raw)
In-Reply-To: <20261128044050.722617-1-rosenp@gmail.com>
The engine applies the HMAC outer hash only when it finalises the
message itself, i.e. on a last or unfragmented operation that has data
and whose total length fits the descriptor field (0xffff). Otherwise
the driver pads the message by hand and runs the padding as a middle
fragment, which leaves the engine with a plain inner hash state. For
HMAC that is wrong twice over:
- the padding encodes the message length only, while the inner hash
also covers the ipad block, and
- the outer hash is never computed, so the result read back from the
IVDIG registers is H(ipad || m) instead of HMAC(k, m).
This hits every HMAC whose last request has no data left to hash, i.e.
any update()/final() sequence over a block-aligned length and any
finup() or digest() of an empty message, and every HMAC of 64 KiB or
more.
Count the ipad block in the manual padding of HMAC requests, and when
the result comes from the IVDIG registers on the last request of an
HMAC, finish the outer hash in software: import the opad state computed
at setkey time into a software hash, with one block already counted,
and hash the inner digest. Each HMAC transform allocates that software
hash at cra_init(). Select the software hashes so they are always
available.
Verified on an Armada 385 (WRT3200ACM) against the software HMACs,
single threaded with random lengths and update splits: about 0.5% of
HMAC-SHA1 and HMAC-SHA256 digests were wrong before, all of them with a
block-aligned total length, and none of about 195000 after, nor of
30000 more with lengths up to 140000 bytes. HMAC-MD5 was not tested:
the test kernel has no software MD5.
Fixes: db509a45339f ("crypto: marvell/cesa - add TDMA support")
Cc: stable@vger.kernel.org
Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
drivers/crypto/marvell/Kconfig | 3 +
drivers/crypto/marvell/cesa/cesa.h | 9 +++
drivers/crypto/marvell/cesa/hash.c | 113 +++++++++++++++++++++++++++--
3 files changed, 119 insertions(+), 6 deletions(-)
diff --git a/drivers/crypto/marvell/Kconfig b/drivers/crypto/marvell/Kconfig
index aa269abb0499..965061abec3c 100644
--- a/drivers/crypto/marvell/Kconfig
+++ b/drivers/crypto/marvell/Kconfig
@@ -12,6 +12,9 @@ config CRYPTO_DEV_MARVELL_CESA
select CRYPTO_LIB_DES
select CRYPTO_SKCIPHER
select CRYPTO_HASH
+ select CRYPTO_MD5
+ select CRYPTO_SHA1
+ select CRYPTO_SHA256
select SRAM
select CRYPTO_DEV_MARVELL
help
diff --git a/drivers/crypto/marvell/cesa/cesa.h b/drivers/crypto/marvell/cesa/cesa.h
index 44351b252861..906d55fb6a3a 100644
--- a/drivers/crypto/marvell/cesa/cesa.h
+++ b/drivers/crypto/marvell/cesa/cesa.h
@@ -514,12 +514,15 @@ struct mv_cesa_hash_ctx {
* struct mv_cesa_hash_ctx - CESA hmac operation context
* @base: base context structure
* @iv: initialization vectors
+ * @outer: software hash used to finish the outer hash when the engine
+ * could not finalise the message itself
*
* HMAC context structure.
*/
struct mv_cesa_hmac_ctx {
struct mv_cesa_ctx base;
__be32 iv[16];
+ struct crypto_shash *outer;
};
/**
@@ -609,6 +612,10 @@ struct mv_cesa_ahash_dma_req {
* @src_nents: number of entries in the scatterlist
* @last_req: define whether the current operation is the last one
* or not
+ * @hmac: HMAC request: manual padding must account for the ipad
+ * block and the outer hash is finished in software
+ * @sw_outer: the driver padded the last request by hand, so the
+ * engine did not apply the HMAC outer hash
* @state: hash state
*/
struct mv_cesa_ahash_req {
@@ -624,6 +631,8 @@ struct mv_cesa_ahash_req {
int src_nents;
bool last_req;
bool algo_le;
+ bool hmac;
+ bool sw_outer;
u32 state[8];
};
diff --git a/drivers/crypto/marvell/cesa/hash.c b/drivers/crypto/marvell/cesa/hash.c
index 9fd3d5c32312..f6894ed7c1cc 100644
--- a/drivers/crypto/marvell/cesa/hash.c
+++ b/drivers/crypto/marvell/cesa/hash.c
@@ -140,6 +140,14 @@ static int mv_cesa_ahash_pad_len(struct mv_cesa_ahash_req *creq)
static int mv_cesa_ahash_pad_req(struct mv_cesa_ahash_req *creq, u8 *buf)
{
unsigned int padlen;
+ u64 len = creq->len;
+
+ /* The HMAC inner hash also covers the ipad block. */
+ if (creq->hmac)
+ len += CESA_MAX_HASH_BLOCK_SIZE;
+
+ /* The engine will not apply the HMAC outer hash to this. */
+ creq->sw_outer = true;
buf[0] = 0x80;
/* Pad out to 56 mod 64 */
@@ -147,11 +155,11 @@ static int mv_cesa_ahash_pad_req(struct mv_cesa_ahash_req *creq, u8 *buf)
memset(buf + 1, 0, padlen - 1);
if (creq->algo_le) {
- __le64 bits = cpu_to_le64(creq->len << 3);
+ __le64 bits = cpu_to_le64(len << 3);
memcpy(buf + padlen, &bits, sizeof(bits));
} else {
- __be64 bits = cpu_to_be64(creq->len << 3);
+ __be64 bits = cpu_to_be64(len << 3);
memcpy(buf + padlen, &bits, sizeof(bits));
}
@@ -353,6 +361,59 @@ static int mv_cesa_ahash_process(struct crypto_async_request *req, u32 status)
return mv_cesa_ahash_std_process(ahashreq, status);
}
+/*
+ * The engine only applies the HMAC outer hash when it finalises the message
+ * itself. When the driver had to pad by hand instead (no data left for the
+ * last fragment, or a message longer than the engine's total length field),
+ * the IVDIG registers hold the inner digest. Finish the outer hash in
+ * software, starting from the opad state computed at setkey time.
+ */
+static void mv_cesa_ahmac_outer(struct ahash_request *req)
+{
+ struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
+ unsigned int digsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
+ SHASH_DESC_ON_STACK(desc, ctx->outer);
+ u8 inner[SHA256_DIGEST_SIZE];
+ union {
+ struct md5_state md5;
+ struct sha1_state sha1;
+ struct sha256_state sha256;
+ } st = {};
+ u32 *state;
+ u64 *count;
+ int i, ret;
+
+ switch (digsize) {
+ case MD5_DIGEST_SIZE:
+ state = st.md5.hash;
+ count = &st.md5.byte_count;
+ break;
+ case SHA1_DIGEST_SIZE:
+ state = st.sha1.state;
+ count = &st.sha1.count;
+ break;
+ default:
+ state = st.sha256.state;
+ count = &st.sha256.count;
+ break;
+ }
+
+ for (i = 0; i < digsize / 4; i++)
+ state[i] = be32_to_cpu(ctx->iv[8 + i]);
+ *count = CESA_MAX_HASH_BLOCK_SIZE;
+
+ memcpy(inner, req->result, digsize);
+ desc->tfm = ctx->outer;
+ ret = crypto_shash_import_core(desc, &st);
+ if (!ret)
+ ret = crypto_shash_finup(desc, inner, digsize, req->result);
+ WARN_ON_ONCE(ret);
+
+ shash_desc_zero(desc);
+ memzero_explicit(inner, sizeof(inner));
+ memzero_explicit(&st, sizeof(st));
+}
+
static void mv_cesa_ahash_complete(struct crypto_async_request *req)
{
struct ahash_request *ahashreq = ahash_request_cast(req);
@@ -394,6 +455,9 @@ static void mv_cesa_ahash_complete(struct crypto_async_request *req)
for (i = 0; i < digsize / 4; i++)
result[i] = cpu_to_be32(creq->state[i]);
}
+
+ if (creq->hmac && creq->sw_outer)
+ mv_cesa_ahmac_outer(ahashreq);
}
}
}
@@ -452,6 +516,14 @@ static void mv_cesa_ahash_init(struct ahash_request *req,
creq->op_tmpl = *tmpl;
creq->len = 0;
creq->algo_le = algo_le;
+
+ switch (mv_cesa_get_op_cfg(tmpl) & CESA_SA_DESC_CFG_MACM_MSK) {
+ case CESA_SA_DESC_CFG_MACM_HMAC_MD5:
+ case CESA_SA_DESC_CFG_MACM_HMAC_SHA1:
+ case CESA_SA_DESC_CFG_MACM_HMAC_SHA256:
+ creq->hmac = true;
+ break;
+ }
}
static inline int mv_cesa_ahash_cra_init(struct crypto_tfm *tfm)
@@ -1242,10 +1314,14 @@ static int mv_cesa_ahmac_setkey(const char *hash_alg_name,
return ret;
}
-static int mv_cesa_ahmac_cra_init(struct crypto_tfm *tfm)
+static int mv_cesa_ahmac_cra_init(struct crypto_tfm *tfm, const char *hash)
{
struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(tfm);
+ ctx->outer = crypto_alloc_shash(hash, 0, 0);
+ if (IS_ERR(ctx->outer))
+ return PTR_ERR(ctx->outer);
+
ctx->base.ops = &mv_cesa_ahash_req_ops;
crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
@@ -1253,6 +1329,28 @@ static int mv_cesa_ahmac_cra_init(struct crypto_tfm *tfm)
return 0;
}
+static int mv_cesa_ahmac_md5_cra_init(struct crypto_tfm *tfm)
+{
+ return mv_cesa_ahmac_cra_init(tfm, "md5");
+}
+
+static int mv_cesa_ahmac_sha1_cra_init(struct crypto_tfm *tfm)
+{
+ return mv_cesa_ahmac_cra_init(tfm, "sha1");
+}
+
+static int mv_cesa_ahmac_sha256_cra_init(struct crypto_tfm *tfm)
+{
+ return mv_cesa_ahmac_cra_init(tfm, "sha256");
+}
+
+static void mv_cesa_ahmac_cra_exit(struct crypto_tfm *tfm)
+{
+ struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(tfm);
+
+ crypto_free_shash(ctx->outer);
+}
+
static int mv_cesa_ahmac_md5_init(struct ahash_request *req)
{
struct mv_cesa_hmac_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
@@ -1318,7 +1416,8 @@ struct ahash_alg mv_ahmac_md5_alg = {
CRYPTO_ALG_KERN_DRIVER_ONLY,
.cra_blocksize = MD5_HMAC_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx),
- .cra_init = mv_cesa_ahmac_cra_init,
+ .cra_init = mv_cesa_ahmac_md5_cra_init,
+ .cra_exit = mv_cesa_ahmac_cra_exit,
.cra_module = THIS_MODULE,
}
}
@@ -1389,7 +1488,8 @@ struct ahash_alg mv_ahmac_sha1_alg = {
CRYPTO_ALG_KERN_DRIVER_ONLY,
.cra_blocksize = SHA1_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx),
- .cra_init = mv_cesa_ahmac_cra_init,
+ .cra_init = mv_cesa_ahmac_sha1_cra_init,
+ .cra_exit = mv_cesa_ahmac_cra_exit,
.cra_module = THIS_MODULE,
}
}
@@ -1460,7 +1560,8 @@ struct ahash_alg mv_ahmac_sha256_alg = {
CRYPTO_ALG_KERN_DRIVER_ONLY,
.cra_blocksize = SHA256_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct mv_cesa_hmac_ctx),
- .cra_init = mv_cesa_ahmac_cra_init,
+ .cra_init = mv_cesa_ahmac_sha256_cra_init,
+ .cra_exit = mv_cesa_ahmac_cra_exit,
.cra_module = THIS_MODULE,
}
}
--
2.56.1
next prev parent reply other threads:[~2026-11-28 4:40 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-28 4:40 [PATCH 0/8] crypto: cesa - fixes for the Marvell CESA driver Rosen Penev
2026-11-28 4:40 ` [PATCH 1/8] crypto: cesa - clear the IRQ affinity hint before freeing the IRQ Rosen Penev
2026-11-28 4:40 ` [PATCH 2/8] crypto: cesa - do not allow unbinding the device Rosen Penev
2026-11-28 4:40 ` [PATCH 3/8] crypto: cesa - hash the last full SRAM chunk before the padding Rosen Penev
2026-11-28 4:40 ` Rosen Penev [this message]
2026-11-28 4:52 ` [PATCH 4/8] crypto: cesa - finish the HMAC outer hash when padding by hand sashiko-bot
2026-11-28 4:40 ` [PATCH 5/8] crypto: cesa - keep the chained IV on the non-TDMA cipher path Rosen Penev
2026-11-28 4:40 ` [PATCH 6/8] crypto: cesa - put the non-TDMA hash padding behind the data Rosen Penev
2026-11-28 4:40 ` [PATCH 7/8] crypto: cesa - do not step empty hash operations on the non-TDMA path Rosen Penev
2026-11-28 4:40 ` [PATCH 8/8] crypto: cesa - finish long hashes " Rosen Penev
2026-11-28 4:52 ` sashiko-bot
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=20261128044050.722617-5-rosenp@gmail.com \
--to=rosenp@gmail.com \
--cc=arno@natisbad.org \
--cc=bbhushan2@marvell.com \
--cc=bbrezillon@kernel.org \
--cc=davem@davemloft.net \
--cc=herbert@gondor.apana.org.au \
--cc=linux-crypto@vger.kernel.org \
--cc=rmk+kernel@arm.linux.org.uk \
--cc=romain.perier@free-electrons.com \
--cc=schalla@marvell.com \
--cc=stable@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