Skip to contentSkip to conversation

Add support for more AES modes in TI DTHEv2

PATCH Superseded T Pratham 2 participants 5 messages Nov 26, 2025 – Jan 12

Subsystems (2)

v7 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

T Pratham

author maintainer
#0 lore raw

v7 cover Add support for more AES modes in TI DTHEv2

DTHEv2 is a new cryptography engine introduced in TI AM62L SoC. The features of DTHEv2 and details of AES modes supported were detailed in [1]. Additional hardware details available in SoC TRM [2].

This patch series adds support for the following AES modes:
- AES-CTR
- AES-GCM
- AES-CCM

The driver is tested using full kernel crypto selftests (CRYPTO_SELFTESTS_FULL) which all pass successfully [3].

---
[1]: [PATCH v7 0/2] Add support for Texas Instruments DTHEv2 Crypto Engine

[2]: Section 14.6.3 (DMA Control Registers -> DMASS_DTHE)

[3]: DTHEv2 AES Engine kernel self-tests logs

Change log:
v7:
- Moved padding buffer to inside request ctx.
- Removed already merged AES-XTS patch.
- Moved dthe_copy_sg() helper from CTR patch to GCM patch, where it is being used for first time.
v6:
- Removed memory alloc calls on the data path (CTR padding in aes_run), replaced with scatterlist chaining for added a pad buffer. Added two accompanying helpers dthe_chain_pad_sg() and dthe_unchain_padded_sg().
- Replaced GFP_KERNEL to GFP_ATOMIC in AEAD src and dst scatterlist prep functions to avoid deadlock in data path.
- Added fallback to software in AEADs on failure.
v5:
- Simplified AES-XTS fallback allocation, directly using xts(aes) for alg_name
- Changed fallback to sync and allocated on stack
v4:
- Return -EINVAL in AES-XTS when cryptlen = 0
- Added software fallback for AES-XTS when ciphertext stealing is required (cryptlen is not multiple of AES_BLOCK_SIZE)
- Changed DTHE_MAX_KEYSIZE definition to use AES_MAX_KEY_SIZE instead of AES_KEYSIZE_256
- In AES-CTR, also pad dst scatterlist when padding src scatterlist
- Changed polling for TAG ready to use readl_relaxed_poll_timeout()
- Used crypto API functions to access struct members instead of directly accessing them (crypto_aead_tfm and aead_request_flags)
- Allocated padding buffers in AEAD algos on the stack.
- Changed helper functions dthe_aead_prep_* to return ERR_PTR on error
- Changed some error labels in dthe_aead_run to improve clarity
- Moved iv_in[] declaration from middle of the function to the top
- Corrected setting CCM M value in the hardware register
- Added checks for CCM L value input in the algorithm from IV.
- Added more fallback cases for CCM where hardware has limitations
v3:
- Added header files to remove implicit declaration error.
- Corrected assignment of src_nents and dst_nents in dthe_aead_run (Ran the lkp kernel test bot script locally to ensure no more such errors are present)
v2:
- Corrected assignment of variable unpadded_cryptlen in dthe_aead_run.
- Removed some if conditions which are always false, and documented the cases in comments.
- Moved polling of TAG ready register to a separate function and returning -ETIMEDOUT on poll timeout.
- Corrected comments to adhere to kernel coding guidelines.

Link to previous version:

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]/
---

T Pratham (3):
crypto: ti - Add support for AES-CTR in DTHEv2 driver
crypto: ti - Add support for AES-GCM in DTHEv2 driver
crypto: ti - Add support for AES-CCM in DTHEv2 driver

 drivers/crypto/ti/Kconfig         |   4 +
 drivers/crypto/ti/dthev2-aes.c    | 832 +++++++++++++++++++++++++++++-
 drivers/crypto/ti/dthev2-common.c |  19 +
 drivers/crypto/ti/dthev2-common.h |  28 +-
 4 files changed, 872 insertions(+), 11 deletions(-)

base-commit: ebbdf6466b30e3b37f3b360826efd21f0633fb9e

Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v7 1/3 crypto: ti - Add support for AES-CTR in DTHEv2 driver

Add support for CTR mode of operation for AES algorithm in the AES Engine of the DTHEv2 hardware cryptographic engine.

---
 drivers/crypto/ti/Kconfig         |   1 +
 drivers/crypto/ti/dthev2-aes.c    | 140 ++++++++++++++++++++++++++++--
 drivers/crypto/ti/dthev2-common.h |   4 +
 3 files changed, 136 insertions(+), 9 deletions(-)
drivers/crypto/ti/Kconfig +1 -0
index a3692ceec49bc..6027e12de279d 100644
@@ -6,6 +6,7 @@ config CRYPTO_DEV_TI_DTHEV2
 	select CRYPTO_SKCIPHER
 	select CRYPTO_ECB
 	select CRYPTO_CBC
+	select CRYPTO_CTR
 	select CRYPTO_XTS
 	help
 	  This enables support for the TI DTHE V2 hw cryptography engine
drivers/crypto/ti/dthev2-aes.c +131 -9

228 lines not on this page · raw message

drivers/crypto/ti/dthev2-common.h +4 -0
index c7a06a4c353ff..e8841fda9a46f 100644
@@ -32,10 +32,12 @@
  * This is currently the keysize of XTS-AES-256 which is 512 bits (64 bytes)
  */
 #define DTHE_MAX_KEYSIZE	(AES_MAX_KEY_SIZE * 2)
+#define DTHE_MAX_PADSIZE	(AES_BLOCK_SIZE)
 
 enum dthe_aes_mode {
 	DTHE_AES_ECB = 0,
 	DTHE_AES_CBC,
+	DTHE_AES_CTR,
 	DTHE_AES_XTS,
 };
 
@@ -92,10 +94,12 @@ 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
  * @aes_compl: Completion variable for use in manual completion in case of DMA callback failure
  */
 struct dthe_aes_req_ctx {
 	int enc;
+	u8 padding[DTHE_MAX_PADSIZE];
 	struct completion aes_compl;
 };
 
Signature · 1 line
2.43.0
maintainer
#2 lore raw

Re: [PATCH v7 1/3] crypto: ti - Add support for AES-CTR in DTHEv2 driver

Quoted 8 lines · T Pratham
drivers/crypto/ti/dthev2-aes.c +3 -0
@@ -295,6 +356,32 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)
 	aes_irqenable_val |= DTHE_AES_IRQENABLE_EN_ALL;
 	writel_relaxed(aes_irqenable_val, aes_base_reg + DTHE_P_AES_IRQENABLE);
 
+	if (ctx->aes_mode == DTHE_AES_CTR) {
+		/*
+		 * CTR mode can operate on any input length, but the hardware

drivers/crypto/ti/dthev2-aes.c static int dthe_aes_run(struct crypto_engine *engine, void *areq) · line 371

Quoted 10 lines
drivers/crypto/ti/dthev2-aes.c +10 -0
+		 * requires input length to be a multiple of the block size.
+		 * We need to handle the padding in the driver.
+		 */
+		if (req->cryptlen % AES_BLOCK_SIZE) {
+			/* Need to create a new SG list with padding */
+			pad_len = ALIGN(req->cryptlen, AES_BLOCK_SIZE) - req->cryptlen;
+			memset(pad_buf, 0, pad_len);
+
+			src = dthe_chain_pad_sg(req->src, src_nents, src_pad, pad_buf, pad_len);
+			src_nents++;

This is too complicated and may break if the user supplies an SG list that's much longer than what is being encrypted. For example, the user could have an SG list spanning many pages, and only the first of half of which are to be encrypted. If you break it up here you will risk corrupting the bigger SG list.

I think it's much simpler to just stop at the last block boundary, and then do the last partial block manually with lib/crypto aes.

Cheers,

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
author maintainer
#3 lore raw

PATCH v7 2/3 crypto: ti - Add support for AES-GCM in DTHEv2 driver

In reply to T Pratham

AES-GCM is an AEAD algorithm supporting both encryption and authentication of data. This patch introduces support for AES-GCM as the first AEAD algorithm supported by the DTHEv2 driver.

---
 drivers/crypto/ti/Kconfig         |   2 +
 drivers/crypto/ti/dthev2-aes.c    | 595 +++++++++++++++++++++++++++++-
 drivers/crypto/ti/dthev2-common.c |  19 +
 drivers/crypto/ti/dthev2-common.h |  25 +-
 4 files changed, 638 insertions(+), 3 deletions(-)
drivers/crypto/ti/Kconfig +2 -0
index 6027e12de279d..221e483737439 100644
@@ -8,6 +8,8 @@ config CRYPTO_DEV_TI_DTHEV2
 	select CRYPTO_CBC
 	select CRYPTO_CTR
 	select CRYPTO_XTS
+	select CRYPTO_GCM
+	select SG_SPLIT
 	help
 	  This enables support for the TI DTHE V2 hw cryptography engine
 	  which can be found on TI K3 SOCs. Selecting this enables use
drivers/crypto/ti/dthev2-aes.c +594 -1

648 lines not on this page · raw message

drivers/crypto/ti/dthev2-common.c +19 -0
index c39d37933b9ee..a2ad79bec105a 100644
@@ -48,6 +48,25 @@ struct dthe_data *dthe_get_dev(struct dthe_tfm_ctx *ctx)
 	return dev_data;
 }
 
+struct scatterlist *dthe_copy_sg(struct scatterlist *dst,
+				 struct scatterlist *src,
+				 int buflen)
+{
+	struct scatterlist *from_sg, *to_sg;
+	int sglen;
+
+	for (to_sg = dst, from_sg = src; buflen && from_sg; buflen -= sglen) {
+		sglen = from_sg->length;
+		if (sglen > buflen)
+			sglen = buflen;
+		sg_set_buf(to_sg, sg_virt(from_sg), sglen);
+		from_sg = sg_next(from_sg);
+		to_sg = sg_next(to_sg);
+	}
+
+	return to_sg;
+}
+
 static int dthe_dma_init(struct dthe_data *dev_data)
 {
 	int ret;
drivers/crypto/ti/dthev2-common.h +23 -2
index e8841fda9a46f..6a08061f382fe 100644
@@ -32,13 +32,14 @@
  * This is currently the keysize of XTS-AES-256 which is 512 bits (64 bytes)
  */
 #define DTHE_MAX_KEYSIZE	(AES_MAX_KEY_SIZE * 2)
-#define DTHE_MAX_PADSIZE	(AES_BLOCK_SIZE)
+#define DTHE_MAX_PADSIZE	(AES_BLOCK_SIZE * 3)
 
 enum dthe_aes_mode {
 	DTHE_AES_ECB = 0,
 	DTHE_AES_CBC,
 	DTHE_AES_CTR,
 	DTHE_AES_XTS,
+	DTHE_AES_GCM,
 };
 
 /* Driver specific struct definitions */
@@ -79,16 +80,22 @@ struct dthe_list {
  * struct dthe_tfm_ctx - Transform ctx struct containing ctx for all sub-components of DTHE V2
  * @dev_data: Device data struct pointer
  * @keylen: AES key length
+ * @authsize: Authentication size for modes with authentication
  * @key: AES key
  * @aes_mode: AES mode
+ * @aead_fb: Fallback crypto aead handle
  * @skcipher_fb: Fallback crypto skcipher handle for AES-XTS mode
  */
 struct dthe_tfm_ctx {
 	struct dthe_data *dev_data;
 	unsigned int keylen;
+	unsigned int authsize;
 	u32 key[DTHE_MAX_KEYSIZE / sizeof(u32)];
 	enum dthe_aes_mode aes_mode;
-	struct crypto_sync_skcipher *skcipher_fb;
+	union {
+		struct crypto_sync_aead *aead_fb;
+		struct crypto_sync_skcipher *skcipher_fb;
+	};
 };
 
 /**
@@ -107,6 +114,20 @@ struct dthe_aes_req_ctx {
 
 struct dthe_data *dthe_get_dev(struct dthe_tfm_ctx *ctx);
 
+/**
+ * dthe_copy_sg - Copy sg entries from src to dst
+ * @dst: Destination sg to be filled
+ * @src: Source sg to be copied from
+ * @buflen: Number of bytes to be copied
+ *
+ * Description:
+ *   Copy buflen bytes of data from src to dst.
+ *
+ **/
+struct scatterlist *dthe_copy_sg(struct scatterlist *dst,
+				 struct scatterlist *src,
+				 int buflen);
+
 int dthe_register_aes_algs(void);
 void dthe_unregister_aes_algs(void);
 
Signature · 1 line
2.43.0
author maintainer
#4 lore raw

PATCH v7 3/3 crypto: ti - Add support for AES-CCM in DTHEv2 driver

In reply to T Pratham

AES-CCM is an AEAD algorithm supporting both encryption and authentication of data. This patch introduces support for AES-CCM AEAD algorithm in the DTHEv2 driver.

---
 drivers/crypto/ti/Kconfig         |   1 +
 drivers/crypto/ti/dthev2-aes.c    | 129 ++++++++++++++++++++++++++----
 drivers/crypto/ti/dthev2-common.h |   1 +
 3 files changed, 115 insertions(+), 16 deletions(-)
drivers/crypto/ti/Kconfig +1 -0
index 221e483737439..1a3a571ac8cef 100644
@@ -9,6 +9,7 @@ config CRYPTO_DEV_TI_DTHEV2
 	select CRYPTO_CTR
 	select CRYPTO_XTS
 	select CRYPTO_GCM
+	select CRYPTO_CCM
 	select SG_SPLIT
 	help
 	  This enables support for the TI DTHE V2 hw cryptography engine
drivers/crypto/ti/dthev2-aes.c +113 -16

206 lines not on this page · raw message

drivers/crypto/ti/dthev2-common.h +1 -0
index 6a08061f382fe..6ce1225a0a61e 100644
@@ -40,6 +40,7 @@ enum dthe_aes_mode {
 	DTHE_AES_CTR,
 	DTHE_AES_XTS,
 	DTHE_AES_GCM,
+	DTHE_AES_CCM,
 };
 
 /* Driver specific struct definitions */
Signature · 1 line
2.43.0