[PATCH v15 27/35] crypto: amlogic: fix setup descriptor lengths and CBC IV reload

Jose Nieto jnieto.alaber at gmail.com
Wed Dec 2 02:10:28 PST 2026


The key/IV setup descriptors were misprogrammed after the descriptor
refactoring:

  - meson_setup_keyiv_descs() always emitted setup_desc_cnt descriptors
    and set each length to the full key/IV length, so on the
    three-descriptor platforms (GXL, AXG) the later descriptors read past
    the DMA-mapped buffer.  Emit only as many descriptors as needed to
    cover the key/IV (one per 16 bytes, capped at setup_desc_cnt) and set
    each length accordingly.  For ECB this yields one or two descriptors
    instead of three, avoiding the out-of-bounds DMA read.

  - The CBC IV has to be reloaded into the key/IV buffer on every hardware
    kick: a request can span several descriptor batches and the IV is
    updated (chaining) after each one.  Keep the CBC IV copy and DMA sync
    in meson_setup_keyiv_descs(), which runs once per batch, and drop the
    now redundant copy done before the loop in meson_cipher().

Signed-off-by: Jose Nieto <jnieto.alaber at gmail.com>
---
 drivers/crypto/amlogic/amlogic-gxl-cipher.c | 13 ++++++++-----
 drivers/crypto/amlogic/amlogic-gxl-hasher.c |  3 ++-
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/drivers/crypto/amlogic/amlogic-gxl-cipher.c b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
index c11ccbaf920d..53fd339ea7d7 100644
--- a/drivers/crypto/amlogic/amlogic-gxl-cipher.c
+++ b/drivers/crypto/amlogic/amlogic-gxl-cipher.c
@@ -154,6 +154,7 @@ static void meson_setup_keyiv_descs(struct cipher_ctx *ctx)
 	struct meson_dev *mc = op->mc;
 	unsigned int ivsize = crypto_skcipher_ivsize(tfm);
 	unsigned int blockmode = algt->blockmode;
+	unsigned int desc_cnt;
 	int i;
 
 	if (ctx->tloffset)
@@ -165,14 +166,18 @@ static void meson_setup_keyiv_descs(struct cipher_ctx *ctx)
 					   ctx->keyiv.len, DMA_TO_DEVICE);
 	}
 
-	for (i = 0; i < mc->pdata->setup_desc_cnt; i++) {
+	desc_cnt = min(mc->pdata->setup_desc_cnt,
+		       DIV_ROUND_UP(ctx->keyiv.len, 16));
+
+	for (i = 0; i < desc_cnt; i++) {
 		struct meson_desc *desc =
 			&mc->chanlist[rctx->flow].tl[ctx->tloffset];
 		int offset = i * 16;
 
 		desc->t_src = cpu_to_le32(ctx->keyiv.addr + offset);
 		desc->t_dst = cpu_to_le32(offset);
-		desc->t_status = cpu_to_le32(DESC_OWN | DESC_MODE_KEY | ctx->keyiv.len);
+		desc->t_status = cpu_to_le32(DESC_OWN | DESC_MODE_KEY |
+					     ctx->keyiv.len / desc_cnt);
 
 		ctx->tloffset++;
 	}
@@ -328,10 +333,8 @@ static int meson_cipher(struct skcipher_request *areq)
 	if (ctx.keyiv.len == AES_KEYSIZE_192)
 		ctx.keyiv.len = AES_MAX_KEY_SIZE;
 
-	if (algt->blockmode == DESC_OPMODE_CBC) {
-		memcpy(op->keyiv + AES_MAX_KEY_SIZE, areq->iv, ivsize);
+	if (algt->blockmode == DESC_OPMODE_CBC)
 		ctx.keyiv.len = AES_MAX_KEY_SIZE + ivsize;
-	}
 
 	ctx.keyiv.addr = dma_map_single(mc->dev, op->keyiv, ctx.keyiv.len,
 					DMA_TO_DEVICE);
diff --git a/drivers/crypto/amlogic/amlogic-gxl-hasher.c b/drivers/crypto/amlogic/amlogic-gxl-hasher.c
index 978cdcd8a4f9..710dd413bd51 100644
--- a/drivers/crypto/amlogic/amlogic-gxl-hasher.c
+++ b/drivers/crypto/amlogic/amlogic-gxl-hasher.c
@@ -253,7 +253,8 @@ static void meson_setup_state_descs(struct hasher_ctx *ctx)
 		desc = &mc->chanlist[rctx->flow].tl[ctx->tloffset];
 		desc->t_src = cpu_to_le32(ctx->state_addr + offset);
 		desc->t_dst = cpu_to_le32(offset);
-		desc->t_status = cpu_to_le32(MESON_SHA_BUFFER_SIZE |
+		desc->t_status = cpu_to_le32(MESON_SHA_BUFFER_SIZE /
+					     mc->pdata->setup_desc_cnt |
 					     DESC_MODE_KEY | DESC_OWN);
 
 		ctx->tloffset++;
-- 
2.47.3




More information about the linux-arm-kernel mailing list