Skip to contentSkip to conversation

Add support for hashing algorithms in TI DTHE V2

PATCH New T Pratham 2 participants 5 messages Nov 23

Subsystems (2)
lore
Share & tools
lore

T Pratham

author maintainer
#0 lore raw

v10 cover Add support for hashing algorithms in TI DTHE V2

DTHEv2 is a new cryptography engine introduced in TI AM62L SoC. The features of DTHEv2 were detailed in [1]. Additional hardware details available in SoC TRM [2]. DTHEv2 includes, among its various sub-components, a hashing engine which is compliant with various IETF and NIST standards, including FIPS 180-3 and FIPS PUB 198.

This patch series adds support for the hashing algorithms SHA224/256/384/512 as well as their HMAC counnterparts for the hashing engine of Texas Instruments DTHE V2 crypto driver.

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
Link:
https://lore.kernel.org/all/[email protected]/

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

[3]: DTHEv2 kernel self-tests logs

[4]: [PATCH v13 0/15] Fix several issues in DTHEv2 driver

Changelog:
v10:
- Rebase on top of latest tree.
- Changed reqsize in algorithms to adjust for DMA padding.
v9:
- Added overflow check on addition of lengths
v8:
- Minor fixes based on Sashiko comments on [4] and changes in [4]
v7:
- Minor fixes based on Sashiko comments on [4]
v6:
- Dropped MD5 and HMAC(MD5)
- Fixed errors pointed by Sashiko
v5:
- Removed CRYPTO_AHASH_ALG_FINAL_NON_ZERO and FINUP_MAX. Implemented the same mechanism in the driver itself by witholding one block in the req ctx.
v4:
- Export initial state when phash_available = 0
v3:
- Check for error in registration individualy for hash and aes driver algos.
- Fixed comment style in dthev2-common.c
v2:
- Completely revamped the driver to use the new CRYPTO_AHASH_BLOCK_ONLY framework for handling partial blocks and crypto_engine for queueing.
- Added HMAC as well.

Link to previous versions:
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]/
---

T Pratham (2):
crypto: ti - Add support for SHA224/256/384/512 in DTHEv2 driver
crypto: ti - Add support for HMAC in DTHEv2 driver

 drivers/crypto/ti/Kconfig         |    3 +
 drivers/crypto/ti/Makefile        |    2 +-
 drivers/crypto/ti/dthev2-aes.c    |   10 +-
 drivers/crypto/ti/dthev2-common.c |   43 +-
 drivers/crypto/ti/dthev2-common.h |   68 +-
 drivers/crypto/ti/dthev2-hash.c   | 1059 +++++++++++++++++++++++++++++
 6 files changed, 1164 insertions(+), 21 deletions(-)
 create mode 100644 drivers/crypto/ti/dthev2-hash.c
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v10 1/2 crypto: ti - Add support for SHA224/256/384/512 in DTHEv2 driver

Add support for SHA224, SHA256, SHA384, SHA512 algorithms in the Hashing Engine of the DTHEv2 hardware cryptographic engine.

---
 drivers/crypto/ti/Kconfig         |   2 +
 drivers/crypto/ti/Makefile        |   2 +-
 drivers/crypto/ti/dthev2-aes.c    |  10 +-
 drivers/crypto/ti/dthev2-common.c |  43 +-
 drivers/crypto/ti/dthev2-common.h |  58 ++-
 drivers/crypto/ti/dthev2-hash.c   | 749 ++++++++++++++++++++++++++++++
 6 files changed, 847 insertions(+), 17 deletions(-)
 create mode 100644 drivers/crypto/ti/dthev2-hash.c
drivers/crypto/ti/Kconfig +2 -0
index 1a3a571ac8cef..90af2c7cb1c55 100644
@@ -10,6 +10,8 @@ config CRYPTO_DEV_TI_DTHEV2
 	select CRYPTO_XTS
 	select CRYPTO_GCM
 	select CRYPTO_CCM
+	select CRYPTO_SHA256
+	select CRYPTO_SHA512
 	select SG_SPLIT
 	help
 	  This enables support for the TI DTHE V2 hw cryptography engine
drivers/crypto/ti/Makefile +1 -1
index b883078f203d7..a90bc97a52321 100644
@@ -1,3 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-$(CONFIG_CRYPTO_DEV_TI_DTHEV2) += dthev2.o
-dthev2-objs := dthev2-common.o dthev2-aes.o
+dthev2-objs := dthev2-common.o dthev2-aes.o dthev2-hash.o
drivers/crypto/ti/dthev2-aes.c +4 -6
index 3d494de552656..dc76eb34e6068 100644
@@ -21,7 +21,6 @@
 #include <linux/dmaengine.h>
 #include <linux/dma-mapping.h>
 #include <linux/io.h>
-#include <linux/iopoll.h>
 #include <linux/scatterlist.h>
 
 /* Registers */
@@ -105,7 +104,6 @@ enum aes_ctrl_mode_masks {
 #define DTHE_AES_GCM_AAD_MAXLEN			(BIT_ULL(32) - 1)
 #define DTHE_AES_CCM_AAD_MAXLEN			(BIT(16) - BIT(8))
 #define DTHE_AES_CCM_CRYPT_MAXLEN		(BIT_ULL(61) - 1)
-#define POLL_TIMEOUT_INTERVAL			HZ
 
 static int dthe_cipher_init_tfm(struct crypto_skcipher *tfm)
 {
@@ -520,7 +518,7 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)
 
 aes_inval_nent_err:
 	local_bh_disable();
-	crypto_finalize_skcipher_request(dev_data->engine, req, ret);
+	crypto_finalize_skcipher_request(engine, req, ret);
 	local_bh_enable();
 	dthe_put_drvdata(dev_data);
 	return 0;
@@ -564,7 +562,7 @@ static int dthe_aes_crypt(struct skcipher_request *req)
 
 	rctx->dev_data = dev_data;
 
-	engine = dev_data->engine;
+	engine = dev_data->aes_engine;
 	ret = crypto_transfer_skcipher_request_to_engine(engine, req);
 	if (ret != -EINPROGRESS && ret != -EBUSY)
 		dthe_put_drvdata(dev_data);
@@ -741,7 +739,7 @@ static int dthe_aead_read_tag(struct dthe_aes_req_ctx *rctx, u32 *tag)
 
 	ret = readl_relaxed_poll_timeout(aes_base_reg + DTHE_P_AES_CTRL, val,
 					 (val & DTHE_AES_CTRL_SAVED_CTX_READY),
-					 0, POLL_TIMEOUT_INTERVAL);
+					 0, DTHE_POLL_TIMEOUT_INTERVAL);
 	if (ret)
 		return ret;
 
@@ -1272,7 +1270,7 @@ static int dthe_aead_crypt(struct aead_request *req)
 
 	rctx->dev_data = dev_data;
 
-	engine = dev_data->engine;
+	engine = dev_data->aes_engine;
 	ret = crypto_transfer_aead_request_to_engine(engine, req);
 	if (ret != -EINPROGRESS && ret != -EBUSY)
 		dthe_put_drvdata(dev_data);
drivers/crypto/ti/dthev2-common.c +36 -7

82 lines not on this page · raw message

drivers/crypto/ti/dthev2-common.h +55 -3

114 lines not on this page · raw message

drivers/crypto/ti/dthev2-hash.c new file +749 -0

749 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v10 1/2] crypto: ti - Add support for SHA224/256/384/512 in DTHEv2 driver

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] crypto: ti - HZ passed as microsecond timeout to readl_relaxed_poll_timeout() - [Low] crypto: ti…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] crypto: ti - HZ passed as microsecond timeout to readl_relaxed_poll_timeout()
- [Low] crypto: ti - open-coded DMA callback assignment in dthe_hash_dma_start()
- [Low] crypto: ti - kernel-doc warnings from /** comments in dthev2-hash.c
--

commit 126808907819f62702aad01a36f54c290b6711fe
Author: T Pratham <[email protected]>

crypto: ti - Add support for SHA224/256/384/512 in DTHEv2 driver

This commit adds ahash support for SHA224, SHA256, SHA384, and SHA512 using the DTHEv2 hashing engine and separates the AES and hash crypto engine instances.

Quoted 5 lines
drivers/crypto/ti/dthev2-common.h +0 -0
index f1b0396b322a0..8d243006d4dd2 100644

drivers/crypto/ti/dthev2-common.h line 33

Quoted 9 lines
drivers/crypto/ti/dthev2-common.h +2 -0
@@ -24,16 +25,28 @@
 #include <linux/dmapool.h>
 #include <linux/dma-mapping.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/scatterlist.h>
 
 #define DTHE_REG_SIZE		4
 #define DTHE_DMA_TIMEOUT_MS	2000
+#define DTHE_POLL_TIMEOUT_INTERVAL	HZ

[Severity: Medium]
Should DTHE_POLL_TIMEOUT_INTERVAL be defined in microseconds (such as USEC_PER_SEC) instead of HZ?

dthe_hash_dma_start() and dthe_aead_read_tag() pass DTHE_POLL_TIMEOUT_INTERVAL as the timeout_us argument to readl_relaxed_poll_timeout(), which evaluates the timeout in microseconds rather than jiffies. A timeout of HZ microseconds (100 to 1000 us) can cause spurious -ETIMEDOUT failures if the crypto engine worker thread is preempted while polling for hardware completion.

[ ... ]

Quoted 5 lines
drivers/crypto/ti/dthev2-hash.c new file +0 -0
new file mode 100644
index 0000000000000..ac6119e4e4d68

[ ... ]

Quoted 3 lines
drivers/crypto/ti/dthev2-hash.c new file +3 -0
+static int dthe_hash_dma_start(struct ahash_request *req, struct scatterlist *src,
+			       int src_nents, size_t len)
+{

[ ... ]

drivers/crypto/ti/dthev2-hash.c line 257

Quoted 10 lines
drivers/crypto/ti/dthev2-hash.c new file +10 -0
+	desc_out = dmaengine_prep_slave_sg(dev_data->dma_sha_tx, src, mapped_nents,
+					   DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
+	if (!desc_out) {
+		dev_err(dev_data->dev, "OUT prep_slave_sg() failed\n");
+		ret = -EINVAL;
+		goto hash_prep_err;
+	}
+
+	desc_out->callback = dthe_hash_dma_in_callback;
+	desc_out->callback_param = req;

[Severity: Low]
This isn't a bug, but should this use dmaengine_desc_set_callback() like dthe_aes_run() does, instead of accessing desc_out->callback and desc_out->callback_param directly?

[ ... ]

drivers/crypto/ti/dthev2-hash.c line 305

Quoted 11 lines
drivers/crypto/ti/dthev2-hash.c new file +11 -0
+	/* Poll output ready status register */
+	if (!ret)
+		ret = readl_relaxed_poll_timeout(sha_base_reg + DTHE_P_HASH_IRQSTATUS, _status,
+						 (_status & DTHE_HASH_IRQSTATUS_OP_READY),
+						 0, DTHE_POLL_TIMEOUT_INTERVAL);
+
+	/**
+	 * We need to read the output registers even in case of failure (ret != 0) to clear
+	 * them properly for future operations. But the function returning a non-zero value
+	 * should indicate that something went wrong and the result value should not be used.
+	 */

[Severity: Low]
This comment and a similar comment in dthe_hash_final() start with "/**" instead of "/*", which causes scripts/kernel-doc warnings under W=1 builds.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

PATCH v10 2/2 crypto: ti - Add support for HMAC in DTHEv2 driver

In reply to T Pratham

Add support for HMAC-SHA512/384/256/224 algorithms in the hashing engine driver of the DTHEv2 hardware cryptographic engine.

---
 drivers/crypto/ti/Kconfig         |   1 +
 drivers/crypto/ti/dthev2-common.h |  10 +-
 drivers/crypto/ti/dthev2-hash.c   | 314 +++++++++++++++++++++++++++++-
 3 files changed, 319 insertions(+), 6 deletions(-)
drivers/crypto/ti/Kconfig +1 -0
index 90af2c7cb1c55..200728b919fca 100644
@@ -12,6 +12,7 @@ config CRYPTO_DEV_TI_DTHEV2
 	select CRYPTO_CCM
 	select CRYPTO_SHA256
 	select CRYPTO_SHA512
+	select CRYPTO_HMAC
 	select SG_SPLIT
 	help
 	  This enables support for the TI DTHE V2 hw cryptography engine
drivers/crypto/ti/dthev2-common.h +6 -4
index 8d243006d4dd2..333823b21eb3a 100644
@@ -33,9 +33,9 @@
 #define DTHE_POLL_TIMEOUT_INTERVAL	HZ
 /*
  * Size of largest possible key (of all algorithms) to be stored in dthe_tfm_ctx
- * This is currently the keysize of XTS-AES-256 which is 512 bits (64 bytes)
+ * This is currently the keysize of HMAC-SHA512 which is 1024 bits (128 bytes)
  */
-#define DTHE_MAX_KEYSIZE	(AES_MAX_KEY_SIZE * 2)
+#define DTHE_MAX_KEYSIZE	(SHA512_BLOCK_SIZE)
 
 enum dthe_hash_alg_sel {
 	DTHE_HASH_MD5		= 0,
@@ -101,9 +101,9 @@ struct dthe_list {
 /**
  * struct dthe_tfm_ctx - Transform ctx struct containing ctx for all sub-components of DTHE V2
  * @dev: Device this transform is bound to.
- * @keylen: AES key length
+ * @keylen: Key length for algorithms that use a key
  * @authsize: Authentication size for modes with authentication
- * @key: AES key
+ * @key: Buffer storing the key
  * @aes_mode: AES mode
  * @hash_mode: Hashing Engine mode
  * @phash_size: partial hash size of the hash algorithm selected
@@ -153,6 +153,7 @@ struct dthe_aes_req_ctx {
  * @phash: buffer to store a partial hash from a previous operation
  * @digestcnt: stores the digest count from a previous operation; currently hardware only provides
  *             a single 32-bit value even for SHA384/512
+ * @odigest: buffer to store the outer digest from a previous operation
  * @padding: padding buffer for handling unaligned data
  * @data: buffer to store one block of data to defer operations for non-zero final.
  * @flags: bitfield of flags for internal use.
@@ -165,6 +166,7 @@ struct dthe_aes_req_ctx {
 struct dthe_hash_req_ctx {
 	u32 phash[SHA512_DIGEST_SIZE / sizeof(u32)];
 	u64 digestcnt[2];
+	u32 odigest[SHA512_DIGEST_SIZE / sizeof(u32)];
 
 	__dma_from_device_group_begin();
 	u8 padding[SHA512_BLOCK_SIZE];
drivers/crypto/ti/dthev2-hash.c +312 -2

427 lines not on this page · raw message

Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v10 2/2] crypto: ti - Add support for HMAC in DTHEv2 driver

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] crypto: ti - sleeping crypto_alloc_shash() call in dthe_hash_export() - [Low] crypto: ti - redundant…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] crypto: ti - sleeping crypto_alloc_shash() call in dthe_hash_export()
- [Low] crypto: ti - redundant fallback setkey and shash alloc in dthe_hmac_setkey()
--

commit 325575ece1b4fbdd9ca3a408597f30635e4f5362
Author: T Pratham <[email protected]>

crypto: ti - Add support for HMAC in DTHEv2 driver

This commit adds HMAC-SHA512, HMAC-SHA384, HMAC-SHA256, and HMAC-SHA224 support to the DTHEv2 hashing engine driver.

Quoted 4 lines
drivers/crypto/ti/dthev2-hash.c +0 -0
index ac6119e4e4d68..02ff7a5e6812a 100644

[ ... ]

Quoted 56 lines
drivers/crypto/ti/dthev2-hash.c +47 -0
@@ -551,6 +618,53 @@ static const void *dthe_hash_get_init_state(struct dthe_tfm_ctx *ctx)
 	}
 }
 
+/*
+ * Compute the HMAC inner or outer pad state (the intermediate hash state after
+ * processing one block of key XOR pad_byte) and write it to @out. Only the
+ * first ctx->phash_size bytes are written, which is always the raw hash state
+ * at the start of the shash export struct.
+ */
+static int dthe_hmac_compute_pad_state(struct dthe_tfm_ctx *ctx, u8 pad_byte,
+				       void *out)
+{
+	const char *alg_name = dthe_hash_get_alg_name(ctx);
+	struct crypto_shash *ktfm;
+	u8 data[SHA512_BLOCK_SIZE];
+	unsigned int ss;
+	u8 *state;
+	int ret;
+
+	ktfm = crypto_alloc_shash(alg_name, 0, 0);
+	if (IS_ERR(ktfm))
+		return PTR_ERR(ktfm);
+
+	ss = crypto_shash_statesize(ktfm);
+	state = kmalloc(ss, GFP_ATOMIC);
+	if (!state) {
+		crypto_free_shash(ktfm);
+		return -ENOMEM;
+	}
+
+	SHASH_DESC_ON_STACK(desc, ktfm);
+
+	desc->tfm = ktfm;
+	for (int i = 0; i < ctx->keylen; i++)
+		data[i] = ((u8 *)ctx->key)[i] ^ pad_byte;
+
+	ret = crypto_shash_init(desc) ?:
+	      crypto_shash_update(desc, data, ctx->keylen) ?:
+	      crypto_shash_export(desc, state);
+
+	if (!ret)
+		memcpy(out, state, ctx->phash_size);
+
+	memzero_explicit(state, ss);
+	kfree(state);
+	crypto_free_shash(ktfm);
+	memzero_explicit(data, sizeof(data));
+	return ret;
+}
+
 static int dthe_hash_export(struct ahash_request *req, void *out)
 {
 	struct dthe_hash_req_ctx *rctx = ahash_request_ctx_dma(req);
@@ -561,9 +675,12 @@ static int dthe_hash_export(struct ahash_request *req, void *out)
 		u8 *u8;

drivers/crypto/ti/dthev2-hash.c static int dthe_hash_export(struct ahash_request *req, void *out) · line 685

Quoted 10 lines
drivers/crypto/ti/dthev2-hash.c +3 -0
 		u64 *u64;
 	} p = { .u8 = out };
+	int ret = 0;
 
 	if (dthe_hash_has_flag(rctx, DTHE_HASH_FLAG_PHASH_AVAIL))
 		memcpy(out, rctx->phash, ctx->phash_size);
+	else if (ctx->keylen > 0)
+		ret = dthe_hmac_compute_pad_state(ctx, HMAC_IPAD_VALUE, out);
 	else
 		memcpy(out, dthe_hash_get_init_state(ctx), ctx->phash_size);

[Severity: High]
Can calling crypto_alloc_shash() in dthe_hmac_compute_pad_state() sleep when dthe_hash_export() runs in atomic context?

crypto_ahash_export() and crypto_ahash_export_core() may be called from softirq or atomic context (as the GFP_ATOMIC allocation of state reflects), but crypto_alloc_shash() acquires the sleeping crypto_alg_sem rw_semaphore and allocates the transform with GFP_KERNEL.

In addition, computing the inner pad state here appears unnecessary:
CRYPTO_AHASH_ALG_NO_EXPORT_CORE is set, so exported state is only consumed by dthe_hash_import(). Because rctx->flags is exported with DTHE_HASH_FLAG_PHASH_AVAIL clear, dthe_hash_dma_start() after import ignores rctx->phash and loads ctx->key with DTHE_HASH_MODE_HMAC_KEY_PROCESSING. The on-stack desc in dthe_hmac_compute_pad_state() is also not cleared with shash_desc_zero() before returning.

[ ... ]

Quoted 40 lines
drivers/crypto/ti/dthev2-hash.c +36 -0
@@ -603,6 +730,53 @@ static int dthe_hash_import(struct ahash_request *req, const void *in)
 	return 0;
 }
 
+static int dthe_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
+			    unsigned int keylen)
+{
+	struct dthe_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
+	struct crypto_ahash *fb = crypto_ahash_fb(tfm);
+	const char *hash_alg_name = dthe_hash_get_alg_name(ctx);
+	unsigned int max_keysize;
+
+	memzero_explicit(ctx->key, sizeof(ctx->key));
+
+	switch (ctx->hash_mode) {
+	case DTHE_HASH_SHA512:
+	case DTHE_HASH_SHA384:
+		max_keysize = DTHE_HMAC_SHA512_MAX_KEYSIZE;
+		break;
+	case DTHE_HASH_SHA256:
+	case DTHE_HASH_SHA224:
+		max_keysize = DTHE_HMAC_SHA256_MAX_KEYSIZE;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	if (keylen > max_keysize) {
+		struct crypto_shash *ktfm;
+		int err;
+
+		ktfm = crypto_alloc_shash(hash_alg_name, 0, 0);
+		if (IS_ERR(ktfm))
+			return PTR_ERR(ktfm);
+
+		SHASH_DESC_ON_STACK(desc, ktfm);
+
+		desc->tfm = ktfm;
+		err = crypto_shash_digest(desc, key, keylen, (u8 *)ctx->key);
+		crypto_free_shash(ktfm);

drivers/crypto/ti/dthev2-hash.c static int dthe_hash_import(struct ahash_request *req, const void *in) · line 778

Quoted 10 lines
drivers/crypto/ti/dthev2-hash.c +10 -0
+		if (err)
+			return err;
+	} else {
+		memcpy(ctx->key, key, keylen);
+	}
+
+	ctx->keylen = max_keysize;
+
+	return crypto_ahash_setkey(fb, key, keylen);
+}

[Severity: Low]
Does dthe_hmac_setkey() need to call crypto_ahash_setkey(fb, key, keylen) or allocate a crypto_shash transform for long keys?

Because CRYPTO_ALG_NEED_FALLBACK is set, crypto_ahash_setkey() in crypto/ahash.c already propagates the key to crypto_ahash_fb(tfm) after alg->setkey() succeeds, so calling it here sets the fallback key twice. Also, since CRYPTO_SHA256 and CRYPTO_SHA512 are selected in Kconfig and <crypto/sha2.h> is included, sha224(), sha256(), sha384(), and sha512() can hash keys longer than max_keysize directly without allocating a temporary shash transform, and ctx->keylen should be reset to 0 when ctx->key is zeroed at the start so a failed setkey does not leave a stale non-zero ctx->keylen with an all-zero key.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2