[PATCH 4/5] crypto: ti - Trim scatterlists to correct length in AES
From: T Pratham
Date: Thu Aug 27 2026 - 06:59:12 EST
AES functions were using src and dst scatterlists directly provided by
the request. This is problematic as it is not guaranteed that the input
scatterlist is exactly the length reqired. This problem was seen in
IPSec use case when the kernel provides the scatterlist which contains
space for plaintext/ciphertext and TAG.
The problem comes when the scatterlist is mapped and sent via DMA. The
K3 UDMA sends/waits for the amount of data equal to the length of
scatterlist mapped. So when the last mapped nent contains some extra
length, the DMA keeps waiting for the extra data and eventually times
out and crashes.
Mitigate this by copying the nents to a local scatterlist, copying only
exactly cryptlen of data. Note that this does not copy the whole data,
but rather only the scatterlist mapping. So it is not as penalising on
performance.
Fixes: 52f641bc63a4 ("crypto: ti - Add driver for DTHE V2 AES Engine (ECB, CBC)")
Signed-off-by: T Pratham <t-pratham@xxxxxx>
Reviewed-by: Kamlesh Gurudasani <kamlesh@xxxxxx>
---
drivers/crypto/ti/dthev2-aes.c | 146 ++++++++++++++++-----------------
1 file changed, 69 insertions(+), 77 deletions(-)
diff --git a/drivers/crypto/ti/dthev2-aes.c b/drivers/crypto/ti/dthev2-aes.c
index c025b08c49930..8899b53f032b1 100644
--- a/drivers/crypto/ti/dthev2-aes.c
+++ b/drivers/crypto/ti/dthev2-aes.c
@@ -108,20 +108,6 @@ enum aes_ctrl_mode_masks {
#define POLL_TIMEOUT_INTERVAL HZ
static int dthe_cipher_init_tfm(struct crypto_skcipher *tfm)
-{
- struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
- struct dthe_data *dev_data = dthe_get_dev(ctx);
-
- if (!dev_data)
- return -ENODEV;
-
- ctx->dev_data = dev_data;
- ctx->keylen = 0;
-
- return 0;
-}
-
-static int dthe_cipher_init_tfm_fallback(struct crypto_skcipher *tfm)
{
struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
struct dthe_data *dev_data = dthe_get_dev(ctx);
@@ -155,19 +141,24 @@ static int dthe_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned
{
struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
- if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_256)
- return -EINVAL;
-
ctx->keylen = keylen;
memcpy(ctx->key, key, keylen);
- return 0;
+ crypto_sync_skcipher_clear_flags(ctx->skcipher_fb, CRYPTO_TFM_REQ_MASK);
+ crypto_sync_skcipher_set_flags(ctx->skcipher_fb,
+ crypto_skcipher_get_flags(tfm) &
+ CRYPTO_TFM_REQ_MASK);
+
+ return crypto_sync_skcipher_setkey(ctx->skcipher_fb, key, keylen);
}
static int dthe_aes_ecb_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen)
{
struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
+ if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_256)
+ return -EINVAL;
+
ctx->aes_mode = DTHE_AES_ECB;
return dthe_aes_setkey(tfm, key, keylen);
@@ -177,6 +168,9 @@ static int dthe_aes_cbc_setkey(struct crypto_skcipher *tfm, const u8 *key, unsig
{
struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
+ if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_256)
+ return -EINVAL;
+
ctx->aes_mode = DTHE_AES_CBC;
return dthe_aes_setkey(tfm, key, keylen);
@@ -185,24 +179,19 @@ static int dthe_aes_cbc_setkey(struct crypto_skcipher *tfm, const u8 *key, unsig
static int dthe_aes_ctr_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen)
{
struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
- int ret = dthe_aes_setkey(tfm, key, keylen);
- if (ret)
- return ret;
+ if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_256)
+ return -EINVAL;
ctx->aes_mode = DTHE_AES_CTR;
- crypto_sync_skcipher_clear_flags(ctx->skcipher_fb, CRYPTO_TFM_REQ_MASK);
- crypto_sync_skcipher_set_flags(ctx->skcipher_fb,
- crypto_skcipher_get_flags(tfm) &
- CRYPTO_TFM_REQ_MASK);
-
- return crypto_sync_skcipher_setkey(ctx->skcipher_fb, key, keylen);
+ return dthe_aes_setkey(tfm, key, keylen);
}
static int dthe_aes_xts_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen)
{
struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
+ int ret;
if (keylen != 2 * AES_KEYSIZE_128 &&
keylen != 2 * AES_KEYSIZE_192 &&
@@ -210,15 +199,12 @@ static int dthe_aes_xts_setkey(struct crypto_skcipher *tfm, const u8 *key, unsig
return -EINVAL;
ctx->aes_mode = DTHE_AES_XTS;
- ctx->keylen = keylen / 2;
- memcpy(ctx->key, key, keylen);
-
- crypto_sync_skcipher_clear_flags(ctx->skcipher_fb, CRYPTO_TFM_REQ_MASK);
- crypto_sync_skcipher_set_flags(ctx->skcipher_fb,
- crypto_skcipher_get_flags(tfm) &
- CRYPTO_TFM_REQ_MASK);
+ ret = dthe_aes_setkey(tfm, key, keylen);
+ if (ret)
+ return ret;
- return crypto_sync_skcipher_setkey(ctx->skcipher_fb, key, keylen);
+ ctx->keylen = keylen / 2;
+ return 0;
}
static void dthe_aes_set_ctrl_key(struct dthe_tfm_ctx *ctx,
@@ -341,8 +327,10 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)
struct dthe_aes_req_ctx *rctx = skcipher_request_ctx(req);
unsigned int len = req->cryptlen;
+ unsigned int pad_len = 0;
struct scatterlist *src = req->src;
struct scatterlist *dst = req->dst;
+ struct scatterlist *sg;
int src_nents = sg_nents_for_len(src, len);
int dst_nents = sg_nents_for_len(dst, len);
@@ -385,38 +373,38 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)
* We need to handle the padding in the driver.
*/
if (ctx->aes_mode == DTHE_AES_CTR && req->cryptlen % AES_BLOCK_SIZE) {
- unsigned int pad_size = AES_BLOCK_SIZE - (req->cryptlen % AES_BLOCK_SIZE);
- u8 *pad_buf = rctx->padding;
- struct scatterlist *sg;
-
- len += pad_size;
+ pad_len = AES_BLOCK_SIZE - (req->cryptlen % AES_BLOCK_SIZE);
+ len += pad_len;
src_nents++;
dst_nents++;
+ }
- src = kmalloc_array(src_nents, sizeof(*src), GFP_ATOMIC);
- if (!src) {
- ret = -ENOMEM;
- goto aes_ctr_src_alloc_err;
- }
-
- sg_init_table(src, src_nents);
- sg = dthe_copy_sg(src, req->src, req->cryptlen);
- memzero_explicit(pad_buf, AES_BLOCK_SIZE);
- sg_set_buf(sg, pad_buf, pad_size);
+ src = kmalloc_array(src_nents, sizeof(*src), GFP_ATOMIC);
+ if (!src) {
+ ret = -ENOMEM;
+ goto aes_src_alloc_err;
+ }
- if (diff_dst) {
- dst = kmalloc_array(dst_nents, sizeof(*dst), GFP_ATOMIC);
- if (!dst) {
- ret = -ENOMEM;
- goto aes_ctr_dst_alloc_err;
- }
+ sg_init_table(src, src_nents);
+ sg = dthe_copy_sg(src, req->src, req->cryptlen);
+ if (pad_len > 0) {
+ memzero_explicit(rctx->padding, AES_BLOCK_SIZE);
+ sg_set_buf(sg, rctx->padding, pad_len);
+ }
- sg_init_table(dst, dst_nents);
- sg = dthe_copy_sg(dst, req->dst, req->cryptlen);
- sg_set_buf(sg, pad_buf, pad_size);
- } else {
- dst = src;
+ if (diff_dst) {
+ dst = kmalloc_array(dst_nents, sizeof(*dst), GFP_ATOMIC);
+ if (!dst) {
+ ret = -ENOMEM;
+ goto aes_dst_alloc_err;
}
+
+ sg_init_table(dst, dst_nents);
+ sg = dthe_copy_sg(dst, req->dst, req->cryptlen);
+ if (pad_len > 0)
+ sg_set_buf(sg, rctx->padding, pad_len);
+ } else {
+ dst = src;
}
tx_dev = dmaengine_get_dma_device(dev_data->dma_aes_tx);
@@ -503,19 +491,19 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)
dma_unmap_sg(tx_dev, src, src_nents, src_dir);
aes_map_src_err:
- if (ctx->aes_mode == DTHE_AES_CTR && req->cryptlen % AES_BLOCK_SIZE) {
+ if (ctx->aes_mode == DTHE_AES_CTR && req->cryptlen % AES_BLOCK_SIZE)
memzero_explicit(rctx->padding, AES_BLOCK_SIZE);
- if (diff_dst)
- kfree(dst);
-aes_ctr_dst_alloc_err:
- kfree(src);
-aes_ctr_src_alloc_err:
- /*
- * Fallback to software if ENOMEM
- */
- if (ret == -ENOMEM)
- ret = dthe_aes_do_fallback(req);
- }
+ if (diff_dst)
+ kfree(dst);
+
+aes_dst_alloc_err:
+ kfree(src);
+aes_src_alloc_err:
+ /*
+ * Fallback to software if ENOMEM
+ */
+ if (ret == -ENOMEM)
+ ret = dthe_aes_do_fallback(req);
local_bh_disable();
crypto_finalize_skcipher_request(dev_data->engine, req, ret);
@@ -1215,6 +1203,7 @@ static int dthe_aead_decrypt(struct aead_request *req)
static struct skcipher_engine_alg cipher_algs[] = {
{
.base.init = dthe_cipher_init_tfm,
+ .base.exit = dthe_cipher_exit_tfm,
.base.setkey = dthe_aes_ecb_setkey,
.base.encrypt = dthe_aes_encrypt,
.base.decrypt = dthe_aes_decrypt,
@@ -1226,7 +1215,8 @@ static struct skcipher_engine_alg cipher_algs[] = {
.cra_priority = 299,
.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER |
CRYPTO_ALG_ASYNC |
- CRYPTO_ALG_KERN_DRIVER_ONLY,
+ CRYPTO_ALG_KERN_DRIVER_ONLY |
+ CRYPTO_ALG_NEED_FALLBACK,
.cra_alignmask = AES_BLOCK_SIZE - 1,
.cra_blocksize = AES_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct dthe_tfm_ctx),
@@ -1237,6 +1227,7 @@ static struct skcipher_engine_alg cipher_algs[] = {
}, /* ECB AES */
{
.base.init = dthe_cipher_init_tfm,
+ .base.exit = dthe_cipher_exit_tfm,
.base.setkey = dthe_aes_cbc_setkey,
.base.encrypt = dthe_aes_encrypt,
.base.decrypt = dthe_aes_decrypt,
@@ -1249,7 +1240,8 @@ static struct skcipher_engine_alg cipher_algs[] = {
.cra_priority = 299,
.cra_flags = CRYPTO_ALG_TYPE_SKCIPHER |
CRYPTO_ALG_ASYNC |
- CRYPTO_ALG_KERN_DRIVER_ONLY,
+ CRYPTO_ALG_KERN_DRIVER_ONLY |
+ CRYPTO_ALG_NEED_FALLBACK,
.cra_alignmask = AES_BLOCK_SIZE - 1,
.cra_blocksize = AES_BLOCK_SIZE,
.cra_ctxsize = sizeof(struct dthe_tfm_ctx),
@@ -1259,7 +1251,7 @@ static struct skcipher_engine_alg cipher_algs[] = {
.op.do_one_request = dthe_aes_run,
}, /* CBC AES */
{
- .base.init = dthe_cipher_init_tfm_fallback,
+ .base.init = dthe_cipher_init_tfm,
.base.exit = dthe_cipher_exit_tfm,
.base.setkey = dthe_aes_ctr_setkey,
.base.encrypt = dthe_aes_encrypt,
@@ -1284,7 +1276,7 @@ static struct skcipher_engine_alg cipher_algs[] = {
.op.do_one_request = dthe_aes_run,
}, /* CTR AES */
{
- .base.init = dthe_cipher_init_tfm_fallback,
+ .base.init = dthe_cipher_init_tfm,
.base.exit = dthe_cipher_exit_tfm,
.base.setkey = dthe_aes_xts_setkey,
.base.encrypt = dthe_aes_encrypt,
--
2.34.1