Fix several issues in DTHEv2 driver
T Pratham
maintainerv10 cover Fix several issues in DTHEv2 driver
This series fixes a handful of independent and mostly benign till now issues in the DTHEv2 driver which were flagged by bots and AI agents. Except for the fifth patch in this series which was found in an internal testing.
The Sashiko review on DTHEv2 hashing driver patch series v5 pointed out some pre-existing issues which needed to be fixed [1].
One of the issues had already been flagged by kernel test bot and a fix sent on the list by Mert [2].
I included this patch in this series with minor edits. Then the Sashiko review page for [2] had more comments [3]
Overall this resulted in a bunch of fixes patches which would have deviated the DTHEv2 hashing driver series had they been included with it. So I clubbed them together in a new patch series which is this.
---
Links:
[1]: https://sashiko.dev/#/patchset/20260807110501.975130-1-t-pratham%40ti.com
[2]: https://lore.kernel.org/all/[email protected]/
[3]: https://sashiko.dev/#/patchset/20260613085858.32580-1-mertsftl%40gmail.com
Changelog:
v10:
- Corrected typo (dev --> ctx->dev)
v9:
- Changed extensive completion re-init based wait to using kref in patch 04/15
v8:
- Minor fix based on Sashiko comment (corrected spinlock)
v7:
- Fixes based on Sashiko comments (added locking around dthe_put_drvdata)
v6:
- Fixes based on Sashiko comments (waitqueue -> completion; DMA alignment for cache coherency)
v5:
- Fix spinlock inconsistency patch
v4:
- Fixes based on Sashiko comments
v3:
- Fixes based on Sashiko comments
- Some reordering of patches
v2:
- Fixes based on Sashiko comments
Link to previous versions:
v9: https://lore.kernel.org/all/[email protected]/
v8: https://lore.kernel.org/all/[email protected]/
v7: https://lore.kernel.org/all/[email protected]/
v6: https://lore.kernel.org/all/[email protected]/
v5: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
---
Mert Seftali (1):
crypto: ti - Use list_first_entry_or_null() in dthe_get_dev()
T Pratham (14):
crypto: ti - Fix spinlock inconsistency in DTHEv2
crypto: ti - Fix potential memory corruption on highmem pages
crypto: ti - Fix use-after-free of dev_data on DTHEv2 driver removal
crypto: ti - Trim scatterlists to correct length in AES
crypto: ti - Align buffers to cacheline for DMA
crypto: ti - Separate padding buffer for src and dst
crypto: ti - Validate sg_nents_for_len() return value in DTHEv2 AES
crypto: ti - Validate sg_nents_for_len() return value in DTHEv2 AEAD
crypto: ti - Terminate DMA on all error paths in AES to clear
descriptors
crypto: ti - Terminate DMA on all error paths in AEAD to clear
descriptors
crypto: ti - Do AEAD software fallback on only ENOMEM
crypto: ti - Correct AEAD tag operations against dma cache
invalidation
crypto: ti - Change lengths in AES to u64 to avoid potential overflows
crypto: ti - Change lengths in AEAD to u64 to avoid potential
overflows
drivers/crypto/ti/dthev2-aes.c | 368 +++++++++++++++++++----------- drivers/crypto/ti/dthev2-common.c | 65 ++++-- drivers/crypto/ti/dthev2-common.h | 66 +++++- 3 files changed, 344 insertions(+), 155 deletions(-)
Signature · 1 line
2.34.1
T Pratham
maintainerPATCH v10 1/15 crypto: ti - Use list_first_entry_or_null() in dthe_get_dev()
From: Mert Seftali <[email protected]>
dthe_get_dev() fetches a device from the global device list with list_first_entry() and then checks the result for NULL. However, list_first_entry() never returns NULL: on an empty list it returns a bogus pointer computed from the list head. The NULL check is therefore dead code, and an empty list would be treated as a valid entry and moved around as if it were a real device.
Use list_first_entry_or_null() so the existing NULL check works as intended and an empty list is handled gracefully.
[pratham:]
Add null checks on dev_data in callers of dthe_get_dev().
--- drivers/crypto/ti/dthev2-aes.c | 12 ++++++++++-- drivers/crypto/ti/dthev2-common.c | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-)
drivers/crypto/ti/dthev2-aes.c +10 -2
@@ -112,6 +112,9 @@ 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 ;
@@ -124,6 +127,9 @@ static int dthe_cipher_init_tfm_fallback(struct crypto_skcipher *tfm)struct dthe_data *dev_data =dthe_get_dev (ctx);const char *alg_name =crypto_tfm_alg_name (crypto_skcipher_tfm (tfm)); +if (!dev_data) +return -ENODEV ; + ctx->dev_data = dev_data; ctx->keylen =0 ;
@@ -571,10 +577,12 @@ static int dthe_aead_init_tfm(struct crypto_aead *tfm) {struct dthe_tfm_ctx *ctx =crypto_aead_ctx (tfm);struct dthe_data *dev_data =dthe_get_dev (ctx); +const char *alg_name =crypto_tfm_alg_name (crypto_aead_tfm (tfm)); - ctx->dev_data = dev_data; +if (!dev_data) +return -ENODEV ; -const char *alg_name =crypto_tfm_alg_name (crypto_aead_tfm (tfm)); + ctx->dev_data = dev_data; ctx->aead_fb =crypto_alloc_sync_aead (alg_name,0 ,CRYPTO_ALG_NEED_FALLBACK );
drivers/crypto/ti/dthev2-common.c +1 -1
@@ -39,7 +39,7 @@ struct dthe_data *dthe_get_dev(struct dthe_tfm_ctx *ctx)return ctx->dev_data;spin_lock_bh (&dthe_dev_list.lock); - dev_data = list_first_entry(&dthe_dev_list.dev_list, struct dthe_data, list); + dev_data = list_first_entry_or_null(&dthe_dev_list.dev_list, struct dthe_data, list);if (dev_data)list_move_tail (&dev_data->list, &dthe_dev_list.dev_list);spin_unlock_bh (&dthe_dev_list.lock);
Signature · 1 line
2.34.1
T Pratham
maintainerPATCH v10 2/15 crypto: ti - Fix spinlock inconsistency in DTHEv2
In reply to T PrathamProbe and remove functions use spin_(un)lock for acquiring the dthe_dev_list.lock. But dthe_get_dev uses the bh variant. Change spin_(un)lock to spin_(un)lock_bh to avoid potential deadlock when a softIRQ process tries to acquire the already held lock.
--- drivers/crypto/ti/dthev2-common.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/crypto/ti/dthev2-common.c +6 -6
@@ -154,9 +154,9 @@ static int dthe_probe(struct platform_device *pdev)platform_set_drvdata (pdev, dev_data); -spin_lock (&dthe_dev_list.lock); +spin_lock_bh (&dthe_dev_list.lock);list_add (&dev_data->list, &dthe_dev_list.dev_list); -spin_unlock (&dthe_dev_list.lock); +spin_unlock_bh (&dthe_dev_list.lock); ret =dthe_dma_init (dev_data);if (ret)
@@ -189,9 +189,9 @@ static int dthe_probe(struct platform_device *pdev)dma_release_channel (dev_data->dma_aes_tx);dma_release_channel (dev_data->dma_sha_tx); probe_dma_err: -spin_lock (&dthe_dev_list.lock); +spin_lock_bh (&dthe_dev_list.lock);list_del (&dev_data->list); -spin_unlock (&dthe_dev_list.lock); +spin_unlock_bh (&dthe_dev_list.lock);return ret; }
@@ -200,9 +200,9 @@ static void dthe_remove(struct platform_device *pdev) {struct dthe_data *dev_data =platform_get_drvdata (pdev); -spin_lock (&dthe_dev_list.lock); +spin_lock_bh (&dthe_dev_list.lock);list_del (&dev_data->list); -spin_unlock (&dthe_dev_list.lock); +spin_unlock_bh (&dthe_dev_list.lock);dthe_unregister_algs ();
Signature · 1 line
2.34.1
T Pratham
maintainerPATCH v10 3/15 crypto: ti - Fix potential memory corruption on highmem pages
In reply to T PrathamChange sg_set_buf to sg_set_page in DTHEv2 dthe_copy_sg function to avoid using sg_virt() on scatterlists. For scatterlists containing a highmem page, sg_virt() yields invalid or null adrdess, causing potential memory corruption. While we are here, also change function signature to change buflen from int to unsigned int.
--- drivers/crypto/ti/dthev2-common.c | 4 ++-- drivers/crypto/ti/dthev2-common.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/crypto/ti/dthev2-common.c +2 -2
@@ -49,7 +49,7 @@ struct dthe_data *dthe_get_dev(struct dthe_tfm_ctx *ctx)struct scatterlist *dthe_copy_sg (struct scatterlist *dst,struct scatterlist *src, -int buflen) +unsigned int buflen) {struct scatterlist *from_sg, *to_sg;int sglen;
@@ -58,7 +58,7 @@ struct scatterlist *dthe_copy_sg(struct scatterlist *dst, sglen = from_sg->length;if (sglen > buflen) sglen = buflen; -sg_set_buf (to_sg,sg_virt (from_sg), sglen); +sg_set_page (to_sg,sg_page (from_sg), sglen, from_sg->offset); from_sg =sg_next (from_sg); to_sg =sg_next (to_sg); }
drivers/crypto/ti/dthev2-common.h +1 -1
@@ -126,7 +126,7 @@ struct dthe_data *dthe_get_dev(struct dthe_tfm_ctx *ctx);**/ struct scatterlist *dthe_copy_sg (struct scatterlist *dst,struct scatterlist *src, -int buflen); +unsigned int buflen);int dthe_register_aes_algs (void );void dthe_unregister_aes_algs (void );
Signature · 1 line
2.34.1
T Pratham
maintainerPATCH v10 4/15 crypto: ti - Fix use-after-free of dev_data on DTHEv2 driver removal
In reply to T PrathamEach *_init_tfm() caches a pointer to the per-instance struct dthe_data in its transform context (ctx->dev_data), but never takes a reference on it. If there are tfms in progress when dthe_remove() is called, the devm allocated dev_data gets freed. Then ctx->dev_data will point to a memory that has been freed.
Move dthe_data to req_ctx structs of algorithms, and store the device pointer in tfm_ctx. Add a kref refcnt to struct dthe_data, which maintains a count of number of requests enqueued in the crypto engine queue which reference the dthe_data object.
On reaching zero, this counter triggers a completion which is waited on in dthe_remove() before doing the driver teardown.
--- drivers/crypto/ti/dthev2-aes.c | 74 +++++++++++++++++++++---------- drivers/crypto/ti/dthev2-common.c | 49 +++++++++++++++++--- drivers/crypto/ti/dthev2-common.h | 52 ++++++++++++++++++++-- 3 files changed, 143 insertions(+), 32 deletions(-)
drivers/crypto/ti/dthev2-aes.c +50 -24
@@ -110,12 +110,11 @@ enum aes_ctrl_mode_masks {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) + ctx->dev =dthe_get_dev (); +if (!ctx->dev)return -ENODEV ; - ctx->dev_data = dev_data; ctx->keylen =0 ;return 0 ;
@@ -124,20 +123,19 @@ static int dthe_cipher_init_tfm(struct crypto_skcipher *tfm)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);const char *alg_name =crypto_tfm_alg_name (crypto_skcipher_tfm (tfm)); -if (!dev_data) + ctx->dev =dthe_get_dev (); +if (!ctx->dev)return -ENODEV ; - ctx->dev_data = dev_data; ctx->keylen =0 ; - ctx->skcipher_fb =crypto_alloc_sync_skcipher (alg_name,0 ,CRYPTO_ALG_NEED_FALLBACK );if (IS_ERR (ctx->skcipher_fb)) { -dev_err (dev_data->dev,"fallback driver %s couldn't be loaded\n" , +dev_err (ctx->dev,"fallback driver %s couldn't be loaded\n" , alg_name); +dthe_put_dev (ctx->dev);return PTR_ERR (ctx->skcipher_fb); }
@@ -149,6 +147,7 @@ static void dthe_cipher_exit_tfm(struct crypto_skcipher *tfm)struct dthe_tfm_ctx *ctx =crypto_skcipher_ctx (tfm);crypto_free_sync_skcipher (ctx->skcipher_fb); +dthe_put_dev (ctx->dev); }static int dthe_aes_setkey (struct crypto_skcipher *tfm,const u8 *key,unsigned int keylen)
@@ -225,7 +224,7 @@ static void dthe_aes_set_ctrl_key(struct dthe_tfm_ctx *ctx,struct dthe_aes_req_ctx *rctx, u32 *iv_in) { - struct dthe_data *dev_data = dthe_get_dev(ctx); + struct dthe_data *dev_data = rctx->dev_data;void __iomem *aes_base_reg = dev_data->regs +DTHE_P_AES_BASE ;u32 ctrl_val =0 ;
@@ -337,8 +336,8 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq) { struct skcipher_request *req = container_of(areq, struct skcipher_request, base);struct dthe_tfm_ctx *ctx =crypto_skcipher_ctx (crypto_skcipher_reqtfm (req)); -struct dthe_data *dev_data =dthe_get_dev (ctx);struct dthe_aes_req_ctx *rctx =skcipher_request_ctx (req); +struct dthe_data *dev_data = rctx->dev_data;unsigned int len = req->cryptlen;struct scatterlist *src = req->src;
@@ -520,14 +519,17 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)local_bh_disable ();crypto_finalize_skcipher_request (dev_data->engine, req, ret);local_bh_enable (); +dthe_put_drvdata (dev_data);return 0 ; }static int dthe_aes_crypt (struct skcipher_request *req) {struct dthe_tfm_ctx *ctx =crypto_skcipher_ctx (crypto_skcipher_reqtfm (req)); -struct dthe_data *dev_data =dthe_get_dev (ctx); +struct dthe_aes_req_ctx *rctx =skcipher_request_ctx (req); +struct dthe_data *dev_data;struct crypto_engine *engine; +int ret;/* * If data is not a multiple of AES_BLOCK_SIZE:
@@ -553,8 +555,18 @@ static int dthe_aes_crypt(struct skcipher_request *req)return 0 ; } + dev_data =dthe_get_drvdata (ctx->dev); +if (!dev_data) +return -ENODEV ; + + rctx->dev_data = dev_data; + engine = dev_data->engine; -return crypto_transfer_skcipher_request_to_engine (engine, req); + ret =crypto_transfer_skcipher_request_to_engine (engine, req); +if (ret != -EINPROGRESS && ret != -EBUSY ) +dthe_put_drvdata (dev_data); + +return ret; }static int dthe_aes_encrypt (struct skcipher_request *req)
@@ -576,19 +588,18 @@ static int dthe_aes_decrypt(struct skcipher_request *req)static int dthe_aead_init_tfm (struct crypto_aead *tfm) {struct dthe_tfm_ctx *ctx =crypto_aead_ctx (tfm); -struct dthe_data *dev_data =dthe_get_dev (ctx);const char *alg_name =crypto_tfm_alg_name (crypto_aead_tfm (tfm)); -if (!dev_data) + ctx->dev =dthe_get_dev (); +if (!ctx->dev)return -ENODEV ; - ctx->dev_data = dev_data; - ctx->aead_fb =crypto_alloc_sync_aead (alg_name,0 ,CRYPTO_ALG_NEED_FALLBACK );if (IS_ERR (ctx->aead_fb)) { -dev_err (dev_data->dev,"fallback driver %s couldn't be loaded\n" , +dev_err (ctx->dev,"fallback driver %s couldn't be loaded\n" , alg_name); +dthe_put_dev (ctx->dev);return PTR_ERR (ctx->aead_fb); }
@@ -600,6 +611,7 @@ static void dthe_aead_exit_tfm(struct crypto_aead *tfm)struct dthe_tfm_ctx *ctx =crypto_aead_ctx (tfm);crypto_free_sync_aead (ctx->aead_fb); +dthe_put_dev (ctx->dev); }/**
@@ -711,9 +723,9 @@ static struct scatterlist *dthe_aead_prep_crypt(struct scatterlist *sg,return crypt_sg; } -static int dthe_aead_read_tag (struct dthe_tfm_ctx *ctx,u32 *tag) +static int dthe_aead_read_tag (struct dthe_aes_req_ctx *rctx,u32 *tag) { -struct dthe_data *dev_data =dthe_get_dev (ctx); +struct dthe_data *dev_data = rctx->dev_data;void __iomem *aes_base_reg = dev_data->regs +DTHE_P_AES_BASE ;u32 val;int ret;
@@ -734,11 +746,12 @@ static int dthe_aead_read_tag(struct dthe_tfm_ctx *ctx, u32 *tag)static int dthe_aead_enc_get_tag (struct aead_request *req) {struct dthe_tfm_ctx *ctx =crypto_aead_ctx (crypto_aead_reqtfm (req)); +struct dthe_aes_req_ctx *rctx =aead_request_ctx (req);u32 tag[AES_BLOCK_WORDS ];int nents;int ret; - ret =dthe_aead_read_tag (ctx, tag); + ret =dthe_aead_read_tag (rctx, tag);if (ret)return ret;
@@ -753,12 +766,13 @@ static int dthe_aead_enc_get_tag(struct aead_request *req)static int dthe_aead_dec_verify_tag (struct aead_request *req) {struct dthe_tfm_ctx *ctx =crypto_aead_ctx (crypto_aead_reqtfm (req)); +struct dthe_aes_req_ctx *rctx =aead_request_ctx (req);u32 tag_out[AES_BLOCK_WORDS ];u32 tag_in[AES_BLOCK_WORDS ];int nents;int ret; - ret =dthe_aead_read_tag (ctx, tag_out); + ret =dthe_aead_read_tag (rctx, tag_out);if (ret)return ret;
@@ -859,7 +873,7 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq) struct aead_request *req = container_of(areq, struct aead_request, base);struct dthe_tfm_ctx *ctx =crypto_aead_ctx (crypto_aead_reqtfm (req));struct dthe_aes_req_ctx *rctx =aead_request_ctx (req); -struct dthe_data *dev_data =dthe_get_dev (ctx); +struct dthe_data *dev_data = rctx->dev_data;unsigned int cryptlen = req->cryptlen;unsigned int assoclen = req->assoclen;
@@ -1130,6 +1144,7 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq)local_bh_disable ();crypto_finalize_aead_request (engine, req, ret);local_bh_enable (); +dthe_put_drvdata (dev_data);return 0 ; }
@@ -1137,10 +1152,11 @@ static int dthe_aead_crypt(struct aead_request *req) {struct dthe_tfm_ctx *ctx =crypto_aead_ctx (crypto_aead_reqtfm (req));struct dthe_aes_req_ctx *rctx =aead_request_ctx (req); -struct dthe_data *dev_data =dthe_get_dev (ctx); +struct dthe_data *dev_data;struct crypto_engine *engine;unsigned int cryptlen = req->cryptlen;bool is_zero_ctr = true; +int ret;/* In decryption, last authsize bytes are the TAG */ if (!rctx->enc)
@@ -1191,8 +1207,18 @@ static int dthe_aead_crypt(struct aead_request *req) (ctx->aes_mode == DTHE_AES_CCM && !is_zero_ctr))return dthe_aead_do_fallback (req); + dev_data =dthe_get_drvdata (ctx->dev); +if (!dev_data) +return -ENODEV ; + + rctx->dev_data = dev_data; + engine = dev_data->engine; -return crypto_transfer_aead_request_to_engine (engine, req); + ret =crypto_transfer_aead_request_to_engine (engine, req); +if (ret != -EINPROGRESS && ret != -EBUSY ) +dthe_put_drvdata (dev_data); + +return ret; }static int dthe_aead_encrypt (struct aead_request *req)
drivers/crypto/ti/dthev2-common.c +44 -5
82 lines not on this page · raw message
drivers/crypto/ti/dthev2-common.h +49 -3
97 lines not on this page · raw message
Signature · 1 line
2.34.1
T Pratham
maintainerPATCH v10 5/15 crypto: ti - Trim scatterlists to correct length in AES
In reply to T PrathamAES 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.
--- drivers/crypto/ti/dthev2-aes.c | 145 ++++++++++++++++----------------- 1 file changed, 69 insertions(+), 76 deletions(-)
drivers/crypto/ti/dthev2-aes.c +69 -76
@@ -108,19 +108,6 @@ enum aes_ctrl_mode_masks {#define POLL_TIMEOUT_INTERVAL HZstatic int dthe_cipher_init_tfm (struct crypto_skcipher *tfm) -{ -struct dthe_tfm_ctx *ctx =crypto_skcipher_ctx (tfm); - - ctx->dev =dthe_get_dev (); -if (!ctx->dev) -return -ENODEV ; - - 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);const char *alg_name =crypto_tfm_alg_name (crypto_skcipher_tfm (tfm));
@@ -154,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);
@@ -176,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);
@@ -184,24 +179,19 @@ static int dthe_aes_cbc_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigstatic 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 &&
@@ -209,15 +199,12 @@ static int dthe_aes_xts_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigreturn -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,
@@ -340,8 +327,10 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)struct dthe_data *dev_data = rctx->dev_data;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);
@@ -384,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);
@@ -502,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);
@@ -1240,6 +1229,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,
@@ -1251,7 +1241,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),
@@ -1262,6 +1253,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,
@@ -1274,7 +1266,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),
@@ -1284,7 +1277,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,
@@ -1309,7 +1302,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,
Signature · 1 line
2.34.1
T Pratham
maintainerPATCH v10 6/15 crypto: ti - Align buffers to cacheline for DMA
In reply to T PrathamAlign padding buffer which are DMA mapped to cache line to avoid any potential cache coherence problem. Add DMA padding to reqctx and use req_ctx_dma helpers instead of req_ctx.
--- drivers/crypto/ti/dthev2-aes.c | 46 +++++++++++++++++-------------- drivers/crypto/ti/dthev2-common.h | 2 ++ 2 files changed, 28 insertions(+), 20 deletions(-)
drivers/crypto/ti/dthev2-aes.c +26 -20
@@ -298,7 +298,7 @@ static void dthe_aes_set_ctrl_key(struct dthe_tfm_ctx *ctx,static int dthe_aes_do_fallback (struct skcipher_request *req) {struct dthe_tfm_ctx *ctx =crypto_skcipher_ctx (crypto_skcipher_reqtfm (req)); -struct dthe_aes_req_ctx *rctx =skcipher_request_ctx (req); +struct dthe_aes_req_ctx *rctx =skcipher_request_ctx_dma (req);SYNC_SKCIPHER_REQUEST_ON_STACK (subreq, ctx->skcipher_fb);
@@ -314,7 +314,7 @@ static int dthe_aes_do_fallback(struct skcipher_request *req)static void dthe_aes_dma_in_callback (void *data) {struct skcipher_request *req = (struct skcipher_request *)data; -struct dthe_aes_req_ctx *rctx =skcipher_request_ctx (req); +struct dthe_aes_req_ctx *rctx =skcipher_request_ctx_dma (req);complete (&rctx->aes_compl); }
@@ -323,7 +323,7 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq) { struct skcipher_request *req = container_of(areq, struct skcipher_request, base);struct dthe_tfm_ctx *ctx =crypto_skcipher_ctx (crypto_skcipher_reqtfm (req)); -struct dthe_aes_req_ctx *rctx =skcipher_request_ctx (req); +struct dthe_aes_req_ctx *rctx =skcipher_request_ctx_dma (req);struct dthe_data *dev_data = rctx->dev_data;unsigned int len = req->cryptlen;
@@ -515,7 +515,7 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)static int dthe_aes_crypt (struct skcipher_request *req) {struct dthe_tfm_ctx *ctx =crypto_skcipher_ctx (crypto_skcipher_reqtfm (req)); -struct dthe_aes_req_ctx *rctx =skcipher_request_ctx (req); +struct dthe_aes_req_ctx *rctx =skcipher_request_ctx_dma (req);struct dthe_data *dev_data;struct crypto_engine *engine;int ret;
@@ -560,7 +560,7 @@ static int dthe_aes_crypt(struct skcipher_request *req)static int dthe_aes_encrypt (struct skcipher_request *req) { -struct dthe_aes_req_ctx *rctx =skcipher_request_ctx (req); +struct dthe_aes_req_ctx *rctx =skcipher_request_ctx_dma (req); rctx->enc =1 ;return dthe_aes_crypt (req);
@@ -568,7 +568,7 @@ static int dthe_aes_encrypt(struct skcipher_request *req)static int dthe_aes_decrypt (struct skcipher_request *req) { -struct dthe_aes_req_ctx *rctx =skcipher_request_ctx (req); +struct dthe_aes_req_ctx *rctx =skcipher_request_ctx_dma (req); rctx->enc =0 ;return dthe_aes_crypt (req);
@@ -735,7 +735,7 @@ static int dthe_aead_read_tag(struct dthe_aes_req_ctx *rctx, u32 *tag)static int dthe_aead_enc_get_tag (struct aead_request *req) {struct dthe_tfm_ctx *ctx =crypto_aead_ctx (crypto_aead_reqtfm (req)); -struct dthe_aes_req_ctx *rctx =aead_request_ctx (req); +struct dthe_aes_req_ctx *rctx =aead_request_ctx_dma (req);u32 tag[AES_BLOCK_WORDS ];int nents;int ret;
@@ -755,7 +755,7 @@ static int dthe_aead_enc_get_tag(struct aead_request *req)static int dthe_aead_dec_verify_tag (struct aead_request *req) {struct dthe_tfm_ctx *ctx =crypto_aead_ctx (crypto_aead_reqtfm (req)); -struct dthe_aes_req_ctx *rctx =aead_request_ctx (req); +struct dthe_aes_req_ctx *rctx =aead_request_ctx_dma (req);u32 tag_out[AES_BLOCK_WORDS ];u32 tag_in[AES_BLOCK_WORDS ];int nents;
@@ -836,7 +836,7 @@ static int dthe_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)static int dthe_aead_do_fallback (struct aead_request *req) {struct dthe_tfm_ctx *ctx =crypto_aead_ctx (crypto_aead_reqtfm (req)); -struct dthe_aes_req_ctx *rctx =aead_request_ctx (req); +struct dthe_aes_req_ctx *rctx =aead_request_ctx_dma (req);SYNC_AEAD_REQUEST_ON_STACK (subreq, ctx->aead_fb);
@@ -852,7 +852,7 @@ static int dthe_aead_do_fallback(struct aead_request *req)static void dthe_aead_dma_in_callback (void *data) {struct aead_request *req = (struct aead_request *)data; -struct dthe_aes_req_ctx *rctx =aead_request_ctx (req); +struct dthe_aes_req_ctx *rctx =aead_request_ctx_dma (req);complete (&rctx->aes_compl); }
@@ -861,7 +861,7 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq) { struct aead_request *req = container_of(areq, struct aead_request, base);struct dthe_tfm_ctx *ctx =crypto_aead_ctx (crypto_aead_reqtfm (req)); -struct dthe_aes_req_ctx *rctx =aead_request_ctx (req); +struct dthe_aes_req_ctx *rctx =aead_request_ctx_dma (req);struct dthe_data *dev_data = rctx->dev_data;unsigned int cryptlen = req->cryptlen;
@@ -1140,7 +1140,7 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq)static int dthe_aead_crypt (struct aead_request *req) {struct dthe_tfm_ctx *ctx =crypto_aead_ctx (crypto_aead_reqtfm (req)); -struct dthe_aes_req_ctx *rctx =aead_request_ctx (req); +struct dthe_aes_req_ctx *rctx =aead_request_ctx_dma (req);struct dthe_data *dev_data;struct crypto_engine *engine;unsigned int cryptlen = req->cryptlen;
@@ -1212,7 +1212,7 @@ static int dthe_aead_crypt(struct aead_request *req)static int dthe_aead_encrypt (struct aead_request *req) { -struct dthe_aes_req_ctx *rctx =aead_request_ctx (req); +struct dthe_aes_req_ctx *rctx =aead_request_ctx_dma (req); rctx->enc =1 ;return dthe_aead_crypt (req);
@@ -1220,7 +1220,7 @@ static int dthe_aead_encrypt(struct aead_request *req)static int dthe_aead_decrypt (struct aead_request *req) { -struct dthe_aes_req_ctx *rctx =aead_request_ctx (req); +struct dthe_aes_req_ctx *rctx =aead_request_ctx_dma (req); rctx->enc =0 ;return dthe_aead_crypt (req);
@@ -1246,7 +1246,8 @@ static struct skcipher_engine_alg cipher_algs[] = { .cra_alignmask = AES_BLOCK_SIZE - 1, .cra_blocksize = AES_BLOCK_SIZE, .cra_ctxsize = sizeof(struct dthe_tfm_ctx), - .cra_reqsize = sizeof(struct dthe_aes_req_ctx), + .cra_reqsize =sizeof (struct dthe_aes_req_ctx ) + + CRYPTO_DMA_PADDING, .cra_module = THIS_MODULE, }, .op.do_one_request = dthe_aes_run,
@@ -1271,7 +1272,8 @@ static struct skcipher_engine_alg cipher_algs[] = { .cra_alignmask = AES_BLOCK_SIZE - 1, .cra_blocksize = AES_BLOCK_SIZE, .cra_ctxsize = sizeof(struct dthe_tfm_ctx), - .cra_reqsize = sizeof(struct dthe_aes_req_ctx), + .cra_reqsize =sizeof (struct dthe_aes_req_ctx ) + + CRYPTO_DMA_PADDING, .cra_module = THIS_MODULE, }, .op.do_one_request = dthe_aes_run,
@@ -1296,7 +1298,8 @@ static struct skcipher_engine_alg cipher_algs[] = { CRYPTO_ALG_NEED_FALLBACK, .cra_blocksize = 1, .cra_ctxsize = sizeof(struct dthe_tfm_ctx), - .cra_reqsize = sizeof(struct dthe_aes_req_ctx), + .cra_reqsize =sizeof (struct dthe_aes_req_ctx ) + + CRYPTO_DMA_PADDING, .cra_module = THIS_MODULE, }, .op.do_one_request = dthe_aes_run,
@@ -1321,7 +1324,8 @@ static struct skcipher_engine_alg cipher_algs[] = { .cra_alignmask = AES_BLOCK_SIZE - 1, .cra_blocksize = AES_BLOCK_SIZE, .cra_ctxsize = sizeof(struct dthe_tfm_ctx), - .cra_reqsize = sizeof(struct dthe_aes_req_ctx), + .cra_reqsize =sizeof (struct dthe_aes_req_ctx ) + + CRYPTO_DMA_PADDING, .cra_module = THIS_MODULE, }, .op.do_one_request = dthe_aes_run,
@@ -1349,7 +1353,8 @@ static struct aead_engine_alg aead_algs[] = { CRYPTO_ALG_NEED_FALLBACK, .cra_blocksize = 1, .cra_ctxsize = sizeof(struct dthe_tfm_ctx), - .cra_reqsize = sizeof(struct dthe_aes_req_ctx), + .cra_reqsize =sizeof (struct dthe_aes_req_ctx ) + + CRYPTO_DMA_PADDING, .cra_module = THIS_MODULE, }, .op.do_one_request = dthe_aead_run,
@@ -1374,7 +1379,8 @@ static struct aead_engine_alg aead_algs[] = { CRYPTO_ALG_NEED_FALLBACK, .cra_blocksize = 1, .cra_ctxsize = sizeof(struct dthe_tfm_ctx), - .cra_reqsize = sizeof(struct dthe_aes_req_ctx), + .cra_reqsize =sizeof (struct dthe_aes_req_ctx ) + + CRYPTO_DMA_PADDING, .cra_module = THIS_MODULE, }, .op.do_one_request = dthe_aead_run,
drivers/crypto/ti/dthev2-common.h +2 -0
@@ -114,7 +114,9 @@ struct dthe_tfm_ctx {*/ struct dthe_aes_req_ctx {int enc; + __dma_from_device_group_begin();u8 padding[2 *AES_BLOCK_SIZE ]; + __dma_from_device_group_end();struct completion aes_compl;struct dthe_data *dev_data; };
Signature · 1 line
2.34.1
T Pratham
maintainerPATCH v10 7/15 crypto: ti - Separate padding buffer for src and dst
In reply to T PrathamSeparate the padding buffer, which is allocated in the req ctx, for src and dst scatterlist to avoid any DMA mapping issues when not doing inline operations. In such cases, the current code was mapping the same padding buffer twice which might cause issues. Also move them into differnt groups in __dma_from_device_group_begin/end() to separate the cache lines for transports of different directions.
--- drivers/crypto/ti/dthev2-aes.c | 29 +++++++++++++++++++---------- drivers/crypto/ti/dthev2-common.h | 14 ++++++++++---- 2 files changed, 29 insertions(+), 14 deletions(-)
drivers/crypto/ti/dthev2-aes.c +19 -10
@@ -338,6 +338,9 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)int src_mapped_nents;int dst_mapped_nents; +u8 *src_padding = rctx->src_padding; +u8 *dst_padding = rctx->dst_padding; +bool diff_dst;enum dma_data_direction src_dir, dst_dir;
@@ -388,8 +391,8 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)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); +memzero_explicit (src_padding,AES_BLOCK_SIZE ); +sg_set_buf (sg, src_padding, pad_len); }if (diff_dst) {
@@ -401,8 +404,10 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)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); +if (pad_len >0 ) { +memzero_explicit (dst_padding,AES_BLOCK_SIZE ); +sg_set_buf (sg, dst_padding, pad_len); + } }else { dst = src; }
@@ -491,8 +496,10 @@ 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 ) -memzero_explicit (rctx->padding,AES_BLOCK_SIZE ); +if (ctx->aes_mode ==DTHE_AES_CTR && req->cryptlen %AES_BLOCK_SIZE ) { +memzero_explicit (rctx->src_padding,AES_BLOCK_SIZE ); +memzero_explicit (rctx->dst_padding,AES_BLOCK_SIZE ); + }if (diff_dst)kfree (dst);
@@ -880,9 +887,9 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq)int src_mapped_nents =0 ;int dst_mapped_nents =0 ; -u8 *src_assoc_padbuf = rctx->padding; -u8 *src_crypt_padbuf = rctx->padding +AES_BLOCK_SIZE ; -u8 *dst_crypt_padbuf = rctx->padding +AES_BLOCK_SIZE ; +u8 *src_assoc_padbuf = rctx->aad_padding; +u8 *src_crypt_padbuf = rctx->src_padding; +u8 *dst_crypt_padbuf = rctx->dst_padding;bool diff_dst;enum dma_data_direction aad_dir, src_dir, dst_dir;
@@ -1125,7 +1132,9 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq)kfree (aad_sg); aead_prep_aad_err: -memzero_explicit (rctx->padding,2 *AES_BLOCK_SIZE ); +memzero_explicit (rctx->aad_padding,AES_BLOCK_SIZE ); +memzero_explicit (rctx->src_padding,AES_BLOCK_SIZE ); +memzero_explicit (rctx->dst_padding,AES_BLOCK_SIZE );if (ret) ret =dthe_aead_do_fallback (req);
drivers/crypto/ti/dthev2-common.h +10 -4
@@ -108,15 +108,21 @@ struct dthe_tfm_ctx {/** * struct dthe_aes_req_ctx - AES engine req ctx struct * @enc: flag indicating encryption or decryption operation -* @padding: padding buffer for handling unaligned data +* @src_padding: padding buffer for handling unaligned data in src +* @aad_padding: padding buffer for handling unaligned data in aad +* @dst_padding: padding buffer for handling unaligned data in dst * @aes_compl: Completion variable for use in manual completion in case of DMA callback failure * @dev_data: Device data struct pointer */ struct dthe_aes_req_ctx {int enc; - __dma_from_device_group_begin(); -u8 padding[2 *AES_BLOCK_SIZE ]; - __dma_from_device_group_end(); +__dma_from_device_group_begin (to_device); +u8 src_padding[AES_BLOCK_SIZE ]; +u8 aad_padding[AES_BLOCK_SIZE ]; +__dma_from_device_group_end (to_device); +__dma_from_device_group_begin (from_device); +u8 dst_padding[AES_BLOCK_SIZE ]; +__dma_from_device_group_end (from_device);struct completion aes_compl;struct dthe_data *dev_data; };
Signature · 1 line
2.34.1
T Pratham
maintainerPATCH v10 8/15 crypto: ti - Validate sg_nents_for_len() return value in DTHEv2 AES
In reply to T Prathamsg_nents_for_len() returns -EINVAL if the supplied scatterlist is shorter than the requested length.
Add explicit checks after each sg_nents_for_len() call in DTHEv2 AES driver.
--- drivers/crypto/ti/dthev2-aes.c | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/crypto/ti/dthev2-aes.c +6 -0
@@ -354,6 +354,11 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)u32 aes_irqenable_val =readl_relaxed (aes_base_reg +DTHE_P_AES_IRQENABLE );u32 aes_sysconfig_val =readl_relaxed (aes_base_reg +DTHE_P_AES_SYSCONFIG ); +if (src_nents <0 || dst_nents <0 ) { + ret = -EINVAL ; + goto aes_inval_nent_err; + } + aes_sysconfig_val |=DTHE_AES_SYSCONFIG_DMA_DATA_IN_OUT_EN ;writel_relaxed (aes_sysconfig_val, aes_base_reg +DTHE_P_AES_SYSCONFIG );
@@ -512,6 +517,7 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)if (ret == -ENOMEM ) ret =dthe_aes_do_fallback (req); +aes_inval_nent_err:local_bh_disable ();crypto_finalize_skcipher_request (dev_data->engine, req, ret);local_bh_enable ();
Signature · 1 line
2.34.1
T Pratham
maintainerPATCH v10 9/15 crypto: ti - Validate sg_nents_for_len() return value in DTHEv2 AEAD
In reply to T Prathamsg_nents_for_len() returns -EINVAL if the supplied scatterlist is shorter than the requested length.
Add explicit checks after each sg_nents_for_len() call in DTHEv2 AEAD driver.
--- drivers/crypto/ti/dthev2-aes.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
drivers/crypto/ti/dthev2-aes.c +22 -0
@@ -643,6 +643,8 @@ static struct scatterlist *dthe_aead_prep_aad(struct scatterlist *sg,return NULL ; aad_nents =sg_nents_for_len (sg, assoclen); +if (aad_nents <0 ) +return ERR_PTR (aad_nents);if (assoclen %AES_BLOCK_SIZE ) aad_nents++;
@@ -699,6 +701,10 @@ static struct scatterlist *dthe_aead_prep_crypt(struct scatterlist *sg, goto dthe_aead_prep_crypt_split_err; crypt_nents =sg_nents_for_len (out_sg[0 ], cryptlen); +if (crypt_nents <0 ) { + err = crypt_nents; + goto dthe_aead_prep_crypt_mem_err; + }if (cryptlen %AES_BLOCK_SIZE ) crypt_nents++;
@@ -758,6 +764,8 @@ static int dthe_aead_enc_get_tag(struct aead_request *req)return ret; nents =sg_nents_for_len (req->dst, req->cryptlen + req->assoclen + ctx->authsize); +if (nents <0 ) +return nents;sg_pcopy_from_buffer (req->dst, nents, tag, ctx->authsize, req->assoclen + req->cryptlen);
@@ -779,6 +787,8 @@ static int dthe_aead_dec_verify_tag(struct aead_request *req)return ret; nents =sg_nents_for_len (req->src, req->assoclen + req->cryptlen); +if (nents <0 ) +return nents;sg_pcopy_to_buffer (req->src, nents, tag_in, ctx->authsize, req->assoclen + req->cryptlen - ctx->authsize);
@@ -975,6 +985,10 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq)if (assoclen !=0 ) {/* Map AAD for TX only */ aad_nents =sg_nents_for_len (aad_sg, assoclen); +if (aad_nents <0 ) { + ret = aad_nents; + goto aead_dma_map_aad_err; + } aad_mapped_nents =dma_map_sg (tx_dev, aad_sg, aad_nents, aad_dir);if (aad_mapped_nents ==0 ) {dev_err (dev_data->dev,"Failed to map AAD for TX\n" );
@@ -996,6 +1010,10 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq)if (cryptlen !=0 ) {/* Map ciphertext src for TX (BIDIRECTIONAL if in-place) */ src_nents =sg_nents_for_len (src, cryptlen); +if (src_nents <0 ) { + ret = src_nents; + goto aead_dma_prep_aad_err; + } src_mapped_nents =dma_map_sg (tx_dev, src, src_nents, src_dir);if (src_mapped_nents ==0 ) {dev_err (dev_data->dev,"Failed to map ciphertext src for TX\n" );
@@ -1016,6 +1034,10 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq)/* Map ciphertext dst for RX (only if separate dst) */ if (diff_dst) { dst_nents =sg_nents_for_len (dst, cryptlen); +if (dst_nents <0 ) { + ret = dst_nents; + goto aead_dma_prep_src_err; + } dst_mapped_nents =dma_map_sg (rx_dev, dst, dst_nents, dst_dir);if (dst_mapped_nents ==0 ) {dev_err (dev_data->dev,"Failed to map ciphertext dst for RX\n" );
Signature · 1 line
2.34.1
T Pratham
maintainerPATCH v10 10/15 crypto: ti - Terminate DMA on all error paths in AES to clear descriptors
In reply to T Prathamdmaengine_prep_slave_sg() allocates a DMA descriptor which is freed on either successful dmaengine_submit() or on dmaengine_terminate_sync().
The error paths after descriptor allocation was not clearing them, leaving the descriptors orphaned and leaking memory in case of failure. Add terminate calls in dthe_aes_run() to appropriately clean the DMA descriptors.
--- drivers/crypto/ti/dthev2-aes.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/crypto/ti/dthev2-aes.c +2 -0
@@ -449,6 +449,8 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)if (!desc_out) {dev_err (dev_data->dev,"OUT prep_slave_sg() failed\n" ); ret = -EINVAL ; +/* terminate to free the orphaned desc_in descriptor */ +dmaengine_terminate_sync (dev_data->dma_aes_rx); goto aes_prep_err; }
Signature · 1 line
2.34.1
T Pratham
maintainerPATCH v10 11/15 crypto: ti - Terminate DMA on all error paths in AEAD to clear descriptors
In reply to T Prathamdmaengine_prep_slave_sg() allocates a DMA descriptor which is freed on either successful dmaengine_submit() or on dmaengine_terminate_sync().
The error paths after descriptor allocation was not clearing them, leaving the descriptors orphaned and leaking memory in case of failure. Add terminate calls in dthe_aead_run() to appropriately clean the DMA descriptors.
--- drivers/crypto/ti/dthev2-aes.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/crypto/ti/dthev2-aes.c +13 -2
@@ -914,6 +914,7 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq)struct device *tx_dev, *rx_dev;struct dma_async_tx_descriptor *desc_in, *desc_out, *desc_aad_out; +bool cleanup_tx_chan = false;int ret;int err;
@@ -1014,13 +1015,15 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq) src_nents =sg_nents_for_len (src, cryptlen);if (src_nents <0 ) { ret = src_nents; - goto aead_dma_prep_aad_err; + cleanup_tx_chan = (assoclen !=0 ); + goto aead_dma_map_src_err; } src_mapped_nents =dma_map_sg (tx_dev, src, src_nents, src_dir);if (src_mapped_nents ==0 ) {dev_err (dev_data->dev,"Failed to map ciphertext src for TX\n" ); ret = -EINVAL ; - goto aead_dma_prep_aad_err; + cleanup_tx_chan = (assoclen !=0 ); + goto aead_dma_map_src_err; }/* Prepare DMA descriptors for ciphertext TX */
@@ -1030,6 +1033,7 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq)if (!desc_out) {dev_err (dev_data->dev,"Ciphertext TX prep_slave_sg() failed\n" ); ret = -EINVAL ; + cleanup_tx_chan = (assoclen !=0 ); goto aead_dma_prep_src_err; }
@@ -1038,12 +1042,14 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq) dst_nents =sg_nents_for_len (dst, cryptlen);if (dst_nents <0 ) { ret = dst_nents; + cleanup_tx_chan = true; goto aead_dma_prep_src_err; } dst_mapped_nents =dma_map_sg (rx_dev, dst, dst_nents, dst_dir);if (dst_mapped_nents ==0 ) {dev_err (dev_data->dev,"Failed to map ciphertext dst for RX\n" ); ret = -EINVAL ; + cleanup_tx_chan = true; goto aead_dma_prep_src_err; } }else {
@@ -1058,6 +1064,7 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq)if (!desc_in) {dev_err (dev_data->dev,"Ciphertext RX prep_slave_sg() failed\n" ); ret = -EINVAL ; + cleanup_tx_chan = true; goto aead_dma_prep_dst_err; }
@@ -1147,6 +1154,10 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq) aead_dma_prep_src_err:if (cryptlen !=0 )dma_unmap_sg (tx_dev, src, src_nents, src_dir); +aead_dma_map_src_err: +/* Free any descriptor prepared on dma_aes_tx but never submitted */ +if (cleanup_tx_chan) +dmaengine_terminate_sync (dev_data->dma_aes_tx); aead_dma_prep_aad_err:if (assoclen !=0 )dma_unmap_sg (tx_dev, aad_sg, aad_nents, aad_dir);
Signature · 1 line
2.34.1
T Pratham
maintainerPATCH v10 12/15 crypto: ti - Do AEAD software fallback on only ENOMEM
In reply to T PrathamSimilarly in how AES part of this driver, we are falling back to software when memory allocation fails (we get -ENOMEM), fix the AEAD part also to only do fallback when we fail only due to -ENOMEM. This is because we are doing memory allocations with GFP_ATOMIC in the beginning which might fail. Any other failure paths might have modified the data or buffers already. Doing a software fallback on them might give incorrect or corrupt results.
--- drivers/crypto/ti/dthev2-aes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/crypto/ti/dthev2-aes.c +1 -1
@@ -1177,7 +1177,7 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq)memzero_explicit (rctx->src_padding,AES_BLOCK_SIZE );memzero_explicit (rctx->dst_padding,AES_BLOCK_SIZE ); -if (ret) +if (ret == -ENOMEM ) ret =dthe_aead_do_fallback (req);local_bh_disable ();
Signature · 1 line
2.34.1
T Pratham
maintainerPATCH v10 13/15 crypto: ti - Correct AEAD tag operations against dma cache invalidation
In reply to T PrathamThere are two issues wrt tag operations (tag read/write in AEAD algorithms) pointed out by Sashiko:
1. dma_sync_sg_for_cpu() before tag operations always uses rx_dev. But dst is mapped with tx_dev in case of inline operations. This is "technically" a no-issue right now as both devices use different channels from the same dma device. But it is nice to be correct.
2. dma_unmap_sg() is being called after tag operations. But between them, there is no synchronisation step. So tag operations writing TAG into the dst scatterlist could in theory be lost in the cache invalidation done as part of unmapping scatterlists.
Now, in the current code, we can correct both of these with some conditional logic and repeating the sync (this time with sync_for_device). However, it felt more elegant to move the TAG operations to occur after all the scatterlists are unmapped. This ensures all buffers are properly synced as well as avoids unnecessary repeated syncing of caches.
--- drivers/crypto/ti/dthev2-aes.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-)
drivers/crypto/ti/dthev2-aes.c +16 -9
@@ -915,6 +915,7 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq)struct device *tx_dev, *rx_dev;struct dma_async_tx_descriptor *desc_in, *desc_out, *desc_aad_out;bool cleanup_tx_chan = false; +bool do_tag_ops = false;int ret;int err;
@@ -1138,15 +1139,11 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq) ret =0 ; } -if (cryptlen !=0 ) -dma_sync_sg_for_cpu (rx_dev, dst, dst_nents, dst_dir); - -if (rctx->enc) - err =dthe_aead_enc_get_tag (req); -else - err =dthe_aead_dec_verify_tag (req); - - ret = (ret) ? ret : err; +/* +* Need to read TAG registers if data is submitted, even in case +* of DMA timeout, to clear the hardware states +*/ + do_tag_ops = true; aead_dma_prep_dst_err:if (diff_dst && cryptlen !=0 )
@@ -1162,6 +1159,16 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq)if (assoclen !=0 )dma_unmap_sg (tx_dev, aad_sg, aad_nents, aad_dir); +/* Do tag ops after scatterlist unmapping syncs caches */ +if (do_tag_ops) { +if (rctx->enc) + err =dthe_aead_enc_get_tag (req); +else + err =dthe_aead_dec_verify_tag (req); + + ret = (ret) ? ret : err; + } + aead_dma_map_aad_err:if (diff_dst && cryptlen !=0 )kfree (dst);
Signature · 1 line
2.34.1
T Pratham
maintainerPATCH v10 14/15 crypto: ti - Change lengths in AES to u64 to avoid potential overflows
In reply to T PrathamChange cryptlen from unsigned int to u64 to avoid overflow when added in padding length calculation.
--- drivers/crypto/ti/dthev2-aes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/crypto/ti/dthev2-aes.c +2 -2
@@ -326,8 +326,8 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)struct dthe_aes_req_ctx *rctx =skcipher_request_ctx_dma (req);struct dthe_data *dev_data = rctx->dev_data; -unsigned int len = req->cryptlen; -unsigned int pad_len =0 ; +u64 len = req->cryptlen; +u64 pad_len =0 ;struct scatterlist *src = req->src;struct scatterlist *dst = req->dst;struct scatterlist *sg;
Signature · 1 line
2.34.1
T Pratham
maintainerPATCH v10 15/15 crypto: ti - Change lengths in AEAD to u64 to avoid potential overflows
In reply to T PrathamChange assoclen, cryptlen, authsize from unsigned int to u64 to avoid overflow when they are added (in TAG offset and padding length calculation)
--- drivers/crypto/ti/dthev2-aes.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-)
drivers/crypto/ti/dthev2-aes.c +17 -11
@@ -758,6 +758,9 @@ static int dthe_aead_enc_get_tag(struct aead_request *req)struct dthe_tfm_ctx *ctx =crypto_aead_ctx (crypto_aead_reqtfm (req));struct dthe_aes_req_ctx *rctx =aead_request_ctx_dma (req);u32 tag[AES_BLOCK_WORDS ]; +u64 assoclen = req->assoclen; +u64 cryptlen = req->cryptlen; +u64 authsize = ctx->authsize;int nents;int ret;
@@ -765,12 +768,12 @@ static int dthe_aead_enc_get_tag(struct aead_request *req)if (ret)return ret; - nents =sg_nents_for_len (req->dst, req->cryptlen + req->assoclen + ctx->authsize); + nents =sg_nents_for_len (req->dst, cryptlen + assoclen + authsize);if (nents <0 )return nents; -sg_pcopy_from_buffer (req->dst, nents, tag, ctx->authsize, - req->assoclen + req->cryptlen); +sg_pcopy_from_buffer (req->dst, nents, tag, authsize, + assoclen + cryptlen);return 0 ; }
@@ -781,6 +784,9 @@ static int dthe_aead_dec_verify_tag(struct aead_request *req)struct dthe_aes_req_ctx *rctx =aead_request_ctx_dma (req);u32 tag_out[AES_BLOCK_WORDS ];u32 tag_in[AES_BLOCK_WORDS ]; +u64 assoclen = req->assoclen; +u64 cryptlen = req->cryptlen; +u64 authsize = ctx->authsize;int nents;int ret;
@@ -788,14 +794,14 @@ static int dthe_aead_dec_verify_tag(struct aead_request *req)if (ret)return ret; - nents =sg_nents_for_len (req->src, req->assoclen + req->cryptlen); + nents =sg_nents_for_len (req->src, assoclen + cryptlen);if (nents <0 )return nents; -sg_pcopy_to_buffer (req->src, nents, tag_in, ctx->authsize, - req->assoclen + req->cryptlen - ctx->authsize); +sg_pcopy_to_buffer (req->src, nents, tag_in, authsize, + assoclen + cryptlen - authsize); -if (crypto_memneq (tag_in, tag_out, ctx->authsize)) +if (crypto_memneq (tag_in, tag_out, authsize))return -EBADMSG ;else return 0 ;
@@ -889,10 +895,10 @@ static int dthe_aead_run(struct crypto_engine *engine, void *areq)struct dthe_aes_req_ctx *rctx =aead_request_ctx_dma (req);struct dthe_data *dev_data = rctx->dev_data; -unsigned int cryptlen = req->cryptlen; -unsigned int assoclen = req->assoclen; -unsigned int authsize = ctx->authsize; -unsigned int unpadded_cryptlen; +u64 cryptlen = req->cryptlen; +u64 assoclen = req->assoclen; +u64 authsize = ctx->authsize; +u64 unpadded_cryptlen;struct scatterlist *src =NULL ;struct scatterlist *dst =NULL ;struct scatterlist *aad_sg =NULL ;
Signature · 1 line
2.34.1
Herbert Xu
maintainerQuoted 11 lines · T Pratham
This series fixes a handful of independent and mostly benign till now issues in the DTHEv2 driver which were flagged by bots and AI agents. Except for the fifth patch in this series which was found in an internal testing.
The Sashiko review on DTHEv2 hashing driver patch series v5 pointed out some pre-existing issues which needed to be fixed [1].
One of the issues had already been flagged by kernel test bot and a fix sent on the list by Mert [2].
Quoted 8 lines
I included this patch in this series with minor edits. Then the Sashiko review page for [2] had more comments [3]
Overall this resulted in a bunch of fixes patches which would have deviated the DTHEv2 hashing driver series had they been included with it. So I clubbed them together in a new patch series which is this.
Signed-off-by T Pratham <[email protected]>
Please respond to the Sashiko comments on this series:
https://sashiko.dev/#/patchset/20261016124456.1802080-1-t-pratham%40ti.com
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt