b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | From d6364b8128439a8c0e381f80c38667de9f15eef8 Mon Sep 17 00:00:00 2001 |
| 2 | From: Eneas U de Queiroz <cotequeiroz@gmail.com> |
| 3 | Date: Fri, 7 Feb 2020 12:02:25 -0300 |
| 4 | Subject: [PATCH 09/11] crypto: qce - use cryptlen when adding extra sgl |
| 5 | |
| 6 | The qce crypto driver appends an extra entry to the dst sgl, to maintain |
| 7 | private state information. |
| 8 | |
| 9 | When the gcm driver sends requests to the ctr skcipher, it passes the |
| 10 | authentication tag after the actual crypto payload, but it must not be |
| 11 | touched. |
| 12 | |
| 13 | Commit 1336c2221bee ("crypto: qce - save a sg table slot for result |
| 14 | buf") limited the destination sgl to avoid overwriting the |
| 15 | authentication tag but it assumed the tag would be in a separate sgl |
| 16 | entry. |
| 17 | |
| 18 | This is not always the case, so it is better to limit the length of the |
| 19 | destination buffer to req->cryptlen before appending the result buf. |
| 20 | |
| 21 | Signed-off-by: Eneas U de Queiroz <cotequeiroz@gmail.com> |
| 22 | Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au> |
| 23 | --- |
| 24 | drivers/crypto/qce/dma.c | 11 ++++++----- |
| 25 | drivers/crypto/qce/dma.h | 2 +- |
| 26 | drivers/crypto/qce/skcipher.c | 5 +++-- |
| 27 | 3 files changed, 10 insertions(+), 8 deletions(-) |
| 28 | |
| 29 | --- a/drivers/crypto/qce/dma.c |
| 30 | +++ b/drivers/crypto/qce/dma.c |
| 31 | @@ -48,9 +48,10 @@ void qce_dma_release(struct qce_dma_data |
| 32 | |
| 33 | struct scatterlist * |
| 34 | qce_sgtable_add(struct sg_table *sgt, struct scatterlist *new_sgl, |
| 35 | - int max_ents) |
| 36 | + unsigned int max_len) |
| 37 | { |
| 38 | struct scatterlist *sg = sgt->sgl, *sg_last = NULL; |
| 39 | + unsigned int new_len; |
| 40 | |
| 41 | while (sg) { |
| 42 | if (!sg_page(sg)) |
| 43 | @@ -61,13 +62,13 @@ qce_sgtable_add(struct sg_table *sgt, st |
| 44 | if (!sg) |
| 45 | return ERR_PTR(-EINVAL); |
| 46 | |
| 47 | - while (new_sgl && sg && max_ents) { |
| 48 | - sg_set_page(sg, sg_page(new_sgl), new_sgl->length, |
| 49 | - new_sgl->offset); |
| 50 | + while (new_sgl && sg && max_len) { |
| 51 | + new_len = new_sgl->length > max_len ? max_len : new_sgl->length; |
| 52 | + sg_set_page(sg, sg_page(new_sgl), new_len, new_sgl->offset); |
| 53 | sg_last = sg; |
| 54 | sg = sg_next(sg); |
| 55 | new_sgl = sg_next(new_sgl); |
| 56 | - max_ents--; |
| 57 | + max_len -= new_len; |
| 58 | } |
| 59 | |
| 60 | return sg_last; |
| 61 | --- a/drivers/crypto/qce/dma.h |
| 62 | +++ b/drivers/crypto/qce/dma.h |
| 63 | @@ -43,6 +43,6 @@ void qce_dma_issue_pending(struct qce_dm |
| 64 | int qce_dma_terminate_all(struct qce_dma_data *dma); |
| 65 | struct scatterlist * |
| 66 | qce_sgtable_add(struct sg_table *sgt, struct scatterlist *sg_add, |
| 67 | - int max_ents); |
| 68 | + unsigned int max_len); |
| 69 | |
| 70 | #endif /* _DMA_H_ */ |
| 71 | --- a/drivers/crypto/qce/skcipher.c |
| 72 | +++ b/drivers/crypto/qce/skcipher.c |
| 73 | @@ -97,13 +97,14 @@ qce_skcipher_async_req_handle(struct cry |
| 74 | |
| 75 | sg_init_one(&rctx->result_sg, qce->dma.result_buf, QCE_RESULT_BUF_SZ); |
| 76 | |
| 77 | - sg = qce_sgtable_add(&rctx->dst_tbl, req->dst, rctx->dst_nents - 1); |
| 78 | + sg = qce_sgtable_add(&rctx->dst_tbl, req->dst, req->cryptlen); |
| 79 | if (IS_ERR(sg)) { |
| 80 | ret = PTR_ERR(sg); |
| 81 | goto error_free; |
| 82 | } |
| 83 | |
| 84 | - sg = qce_sgtable_add(&rctx->dst_tbl, &rctx->result_sg, 1); |
| 85 | + sg = qce_sgtable_add(&rctx->dst_tbl, &rctx->result_sg, |
| 86 | + QCE_RESULT_BUF_SZ); |
| 87 | if (IS_ERR(sg)) { |
| 88 | ret = PTR_ERR(sg); |
| 89 | goto error_free; |