Skip to contentSkip to conversation

Add support for hashing algorithms in TI DTHE V2

PATCH Superseded T Pratham 2 participants 9 messages May 26 – Jul 13

Subsystems (2)

v4 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

T Pratham

author maintainer
#0 lore raw

v4 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 and MD5 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

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

[3]: DTHEv2 kernel self-tests logs

Changelog:
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:
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 SHA224/256/384/512 in DTHEv2 driver
crypto: ti - Add support for MD5 in DTHEv2 Hashing Engine driver
crypto: ti - Add support for HMAC in DTHEv2 Hashing Engine driver

 drivers/crypto/ti/Kconfig         |    4 +
 drivers/crypto/ti/Makefile        |    2 +-
 drivers/crypto/ti/dthev2-aes.c    |    6 +-
 drivers/crypto/ti/dthev2-common.c |   43 +-
 drivers/crypto/ti/dthev2-common.h |   58 +-
 drivers/crypto/ti/dthev2-hash.c   | 1022 +++++++++++++++++++++++++++++
 6 files changed, 1117 insertions(+), 18 deletions(-)
 create mode 100644 drivers/crypto/ti/dthev2-hash.c
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v4 1/3 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    |   6 +-
 drivers/crypto/ti/dthev2-common.c |  43 +-
 drivers/crypto/ti/dthev2-common.h |  47 ++-
 drivers/crypto/ti/dthev2-hash.c   | 631 ++++++++++++++++++++++++++++++
 6 files changed, 717 insertions(+), 14 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 +3 -3
index eb5cd902dfb59..164e72c07ee3f 100644
@@ -512,7 +512,7 @@ static int dthe_aes_run(struct crypto_engine *engine, void *areq)
 	}
 
 	local_bh_disable();
-	crypto_finalize_skcipher_request(dev_data->engine, req, ret);
+	crypto_finalize_skcipher_request(engine, req, ret);
 	local_bh_enable();
 	return 0;
 }
@@ -547,7 +547,7 @@ static int dthe_aes_crypt(struct skcipher_request *req)
 		return 0;
 	}
 
-	engine = dev_data->engine;
+	engine = dev_data->aes_engine;
 	return crypto_transfer_skcipher_request_to_engine(engine, req);
 }
 
@@ -1183,7 +1183,7 @@ static int dthe_aead_crypt(struct aead_request *req)
 	    (ctx->aes_mode == DTHE_AES_CCM && !is_zero_ctr))
 		return dthe_aead_do_fallback(req);
 
-	engine = dev_data->engine;
+	engine = dev_data->aes_engine;
 	return crypto_transfer_aead_request_to_engine(engine, req);
 }
 
drivers/crypto/ti/dthev2-common.c +36 -7

82 lines not on this page · raw message

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

93 lines not on this page · raw message

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

631 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#2 lore raw

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

Quoted 11 lines · T Pratham
drivers/crypto/ti/dthev2-hash.c new file +10 -0
+static int dthe_hash_final(struct ahash_request *req)
+{
+	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
+	struct dthe_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
+	struct dthe_hash_req_ctx *rctx = ahash_request_ctx(req);
+	struct dthe_data *dev_data = dthe_get_dev(ctx);
+	struct crypto_engine *engine = dev_data->hash_engine;
+
+	/**
+	 * We are always buffering data in update, except when nbytes = 0.

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

Quoted 10 lines
drivers/crypto/ti/dthev2-hash.c new file +10 -0
+	 * So, either we get the buffered data here (nbytes > 0) or
+	 * it is the case that we got zero message to begin with
+	 */
+	if (req->nbytes > 0) {
+		rctx->flags = DTHE_HASH_OP_FINUP;
+
+		return crypto_transfer_hash_request_to_engine(engine, req);
+	}
+
+	dthe_hash_write_zero_message(ctx->hash_mode, req->result);

This doesn't look right. If I do an update of 64 bytes, and then call final with req->nbytes == 0, this will give me a zero-length hash.

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

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

Quoted 27 lines · Herbert Xu
Quoted 11 lines · T Pratham
drivers/crypto/ti/dthev2-hash.c new file +10 -0
+static int dthe_hash_final(struct ahash_request *req)
+{
+	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
+	struct dthe_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
+	struct dthe_hash_req_ctx *rctx = ahash_request_ctx(req);
+	struct dthe_data *dev_data = dthe_get_dev(ctx);
+	struct crypto_engine *engine = dev_data->hash_engine;
+
+	/**
+	 * We are always buffering data in update, except when nbytes = 0.

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

Quoted 10 lines
drivers/crypto/ti/dthev2-hash.c new file +10 -0
+	 * So, either we get the buffered data here (nbytes > 0) or
+	 * it is the case that we got zero message to begin with
+	 */
+	if (req->nbytes > 0) {
+		rctx->flags = DTHE_HASH_OP_FINUP;
+
+		return crypto_transfer_hash_request_to_engine(engine, req);
+	}
+
+	dthe_hash_write_zero_message(ctx->hash_mode, req->result);

This doesn't look right. If I do an update of 64 bytes, and then call final with req->nbytes == 0, this will give me a zero-length hash.

Quoted 1 line

Cheers,

.cra_flags sets CRYPTO_AHASH_ALG_BLOCK_ONLY and CRYPTO_AHASH_ALG_FINAL_NONZERO flags. An update of 64 bytes will do an update of block size and carry over at least one byte to final. We always go into this if block when there is non-zero data coming into update.

Signature · 2 lines
Regards
T Pratham <[email protected]>
maintainer
#4 lore raw

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

Quoted 5 lines · T Pratham

.cra_flags sets CRYPTO_AHASH_ALG_BLOCK_ONLY and CRYPTO_AHASH_ALG_FINAL_NONZERO flags. An update of 64 bytes will do an update of block size and carry over at least one byte to final. We always go into this if block when there is non-zero data coming into update.

For AHASH_BLOCK_ONLY algorithms, the export format must be identical between different implementations.

Therefore FINAL_NONZERO cannot be used for only one implementation since the user can import the partial state from a different implementation which does not have FINAL_NONZERO set.

For sha you cannot use FINAL_NONZERO since the generic implementation doesn't use it.

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
#5 lore raw

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

Quoted 10 lines · Herbert Xu
Quoted 5 lines · T Pratham

.cra_flags sets CRYPTO_AHASH_ALG_BLOCK_ONLY and CRYPTO_AHASH_ALG_FINAL_NONZERO flags. An update of 64 bytes will do an update of block size and carry over at least one byte to final. We always go into this if block when there is non-zero data coming into update.

For AHASH_BLOCK_ONLY algorithms, the export format must be identical between different implementations.

Quoted 8 lines

Therefore FINAL_NONZERO cannot be used for only one implementation since the user can import the partial state from a different implementation which does not have FINAL_NONZERO set.

For sha you cannot use FINAL_NONZERO since the generic implementation doesn't use it.

Cheers,

Thanks for letting me know of the export format constraint.

However, this sounds contradictory to the stated purpose of the flag itself. Commit 7650f826f7b2 ("crypto: shash - Handle partial blocks in API"), in which you introduced FINAL_NONZERO, says:

"This will come in handy when this is extended to ahash where hardware often can't deal with a zero-length final."

This is exactly the case for DTHEv2 hashing engine hardware. Along with this, using BLOCK_ONLY is also necessary for DTHEv2 to delegate the partial block management to the crypto layer. So you can see that most hardware face both these constraints together. The above commit gives the impression of solving both these issues for hardware hash engines.

If these flags, and the surrounding framework was introduced with hash drivers in mind, this new constraint of import/export format consistency makes it completely unusable for any standard hash algorithm like SHA. In practice, this makes any hardware hash driver unable to utilize the FINAL_NONZERO feature.

This looks to me like a gap in the framework; the implementation of the feature does not exactly match the intention. What do you suggest should be the approach here, and going forward?

Signature · 2 lines
Regards
T Pratham <[email protected]>
maintainer
#6 lore raw

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

Quoted 7 lines · T Pratham

However, this sounds contradictory to the stated purpose of the flag itself. Commit 7650f826f7b2 ("crypto: shash - Handle partial blocks in API"), in which you introduced FINAL_NONZERO, says:

"This will come in handy when this is extended to ahash where hardware often can't deal with a zero-length final."

That was the intention.

Quoted 3 lines

This looks to me like a gap in the framework; the implementation of the feature does not exactly match the intention. What do you suggest should be the approach here, and going forward?

The issue is that to make it easier for the hardware, with the existing frame-work we would have to penalise the software implementation and make it withhold an extra block too. Because software is so much faster than hardware, it makes no sense to do that.

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
#7 lore raw

PATCH v4 2/3 crypto: ti - Add support for MD5 in DTHEv2 Hashing Engine driver

In reply to T Pratham

Add support for MD5 algorithm in the hashing engine of DTHEv2 hardware cryptographic engine.

---
 drivers/crypto/ti/Kconfig         |  1 +
 drivers/crypto/ti/dthev2-common.h |  1 +
 drivers/crypto/ti/dthev2-hash.c   | 49 +++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+)
drivers/crypto/ti/Kconfig +1 -0
index 90af2c7cb1c55..9c2aa50cfbfbe 100644
@@ -12,6 +12,7 @@ config CRYPTO_DEV_TI_DTHEV2
 	select CRYPTO_CCM
 	select CRYPTO_SHA256
 	select CRYPTO_SHA512
+	select CRYPTO_MD5
 	select SG_SPLIT
 	help
 	  This enables support for the TI DTHE V2 hw cryptography engine
drivers/crypto/ti/dthev2-common.h +1 -0
index bfe7f2de4415c..24799007ea81f 100644
@@ -17,6 +17,7 @@
 #include <crypto/internal/aead.h>
 #include <crypto/internal/hash.h>
 #include <crypto/internal/skcipher.h>
+#include <crypto/md5.h>
 #include <crypto/sha2.h>
 
 #include <linux/delay.h>
drivers/crypto/ti/dthev2-hash.c +49 -0

91 lines not on this page · raw message

Signature · 1 line
2.34.1
author maintainer
#8 lore raw

PATCH v4 3/3 crypto: ti - Add support for HMAC in DTHEv2 Hashing Engine driver

In reply to T Pratham

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

---
 drivers/crypto/ti/Kconfig         |   1 +
 drivers/crypto/ti/dthev2-common.h |  10 +-
 drivers/crypto/ti/dthev2-hash.c   | 346 +++++++++++++++++++++++++++++-
 3 files changed, 351 insertions(+), 6 deletions(-)
drivers/crypto/ti/Kconfig +1 -0
index 9c2aa50cfbfbe..68dccf92f5382 100644
@@ -13,6 +13,7 @@ config CRYPTO_DEV_TI_DTHEV2
 	select CRYPTO_SHA256
 	select CRYPTO_SHA512
 	select CRYPTO_MD5
+	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 24799007ea81f..847804ed74736 100644
@@ -31,9 +31,9 @@
 #define DTHE_DMA_TIMEOUT_MS	2000
 /*
  * 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,
@@ -93,9 +93,9 @@ 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
+ * @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
@@ -135,6 +135,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
  * @phash_available: flag indicating if a partial hash from a previous operation is available
  * @flags: flags for internal use
  * @padding: padding buffer for handling unaligned data
@@ -143,6 +144,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)];
 	u8 phash_available;
 	u8 flags;
 	u8 padding[SHA512_BLOCK_SIZE];
drivers/crypto/ti/dthev2-hash.c +344 -2

446 lines not on this page · raw message

Signature · 1 line
2.34.1