* [PATCH v11 0/2] Add support for hashing algorithms in TI DTHE V2
@ 2026-12-04 16:42 T Pratham
2026-12-04 16:42 ` [PATCH v11 1/2] crypto: ti - Add support for SHA224/256/384/512 in DTHEv2 driver T Pratham
2026-12-04 16:42 ` [PATCH v11 2/2] crypto: ti - Add support for HMAC " T Pratham
0 siblings, 2 replies; 5+ messages in thread
From: T Pratham @ 2026-12-04 16:42 UTC (permalink / raw)
To: Herbert Xu, David S. Miller
Cc: T Pratham, linux-crypto, linux-kernel, Sebin Francis,
Manorit Chawdhry, Vishal Mahaveer, Praneeth Bajjuri
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].
Signed-off-by: T Pratham <t-pratham@ti.com>
---
[1]: [PATCH v7 0/2] Add support for Texas Instruments DTHEv2 Crypto
Engine
Link: https://lore.kernel.org/all/20250820092710.3510788-1-t-pratham@ti.com/
[2]: Section 14.6.3 (DMA Control Registers -> DMASS_DTHE)
Link: https://www.ti.com/lit/ug/sprujb4/sprujb4.pdf
[3]: DTHEv2 kernel self-tests logs
Link: https://gist.github.com/Pratham-T/fd15b8e0ee815bcb420a60d451a0cf18
[4]: [PATCH v13 0/15] Fix several issues in DTHEv2 driver
Link: https://lore.kernel.org/all/20261112081204.1146395-1-t-pratham@ti.com/
Changelog:
v11:
- Separate AES and Hashing engine tfm_ctx structs.
- Revise hash state export/import, with only exporting/inporting
partial state and bytecount (which the software hash uses).
- Do endian-ness conversion of state exported/imported so that software
gets it in the expected format.
- Drop NO_EXPORT_CORE flag.
- HMAC Key preprocessing rework. The key preprocess now gets completed
within setkey function and not with the first request. As a result,
the ctx now has the processed ipad/opad values.
- Remove zero final handling through hardware by holding back a block.
Thus simplifying state handling. Do software fallback for zero sized
final/finup.
- Renaming of some variables.
- Re-arranging of some functions.
- Updated to using dmaengine_desc_set_callback() utility.
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:
v10:
https://lore.kernel.org/all/20261123142918.3451969-1-t-pratham@ti.com/
v9:
https://lore.kernel.org/all/20260910180424.2131193-1-t-pratham@ti.com/
v8:
https://lore.kernel.org/all/20260910105014.1552521-1-t-pratham@ti.com/
v7:
https://lore.kernel.org/all/20260827133542.619717-1-t-pratham@ti.com/
v6:
https://lore.kernel.org/all/20260827112653.530273-1-t-pratham@ti.com/
v5:
https://lore.kernel.org/all/20260807110501.975130-1-t-pratham@ti.com/
v4:
https://lore.kernel.org/all/20260526094355.555712-1-t-pratham@ti.com/
v3:
https://lore.kernel.org/all/20260226131103.3560884-1-t-pratham@ti.com/
v2:
https://lore.kernel.org/all/20260121132408.743777-1-t-pratham@ti.com/
v1:
https://lore.kernel.org/all/20250218104943.2304730-1-t-pratham@ti.com/
---
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 | 66 +-
drivers/crypto/ti/dthev2-common.c | 43 +-
drivers/crypto/ti/dthev2-common.h | 86 ++-
drivers/crypto/ti/dthev2-hash.c | 1112 +++++++++++++++++++++++++++++
6 files changed, 1263 insertions(+), 49 deletions(-)
create mode 100644 drivers/crypto/ti/dthev2-hash.c
--
2.34.1
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v11 1/2] crypto: ti - Add support for SHA224/256/384/512 in DTHEv2 driver
2026-12-04 16:42 [PATCH v11 0/2] Add support for hashing algorithms in TI DTHE V2 T Pratham
@ 2026-12-04 16:42 ` T Pratham
2026-12-04 17:21 ` sashiko-bot
2026-12-04 16:42 ` [PATCH v11 2/2] crypto: ti - Add support for HMAC " T Pratham
1 sibling, 1 reply; 5+ messages in thread
From: T Pratham @ 2026-12-04 16:42 UTC (permalink / raw)
To: T Pratham, Herbert Xu, David S. Miller
Cc: Sebin Francis, Manorit Chawdhry, Vishal Mahaveer,
Praneeth Bajjuri, linux-kernel, linux-crypto
Add support for SHA224, SHA256, SHA384, SHA512 algorithms in the Hashing
Engine of the DTHEv2 hardware cryptographic engine.
Signed-off-by: T Pratham <t-pratham@ti.com>
---
drivers/crypto/ti/Kconfig | 2 +
drivers/crypto/ti/Makefile | 2 +-
drivers/crypto/ti/dthev2-aes.c | 66 ++-
drivers/crypto/ti/dthev2-common.c | 43 +-
drivers/crypto/ti/dthev2-common.h | 65 ++-
drivers/crypto/ti/dthev2-hash.c | 780 ++++++++++++++++++++++++++++++
6 files changed, 911 insertions(+), 47 deletions(-)
create mode 100644 drivers/crypto/ti/dthev2-hash.c
diff --git a/drivers/crypto/ti/Kconfig b/drivers/crypto/ti/Kconfig
index 1a3a571ac8cef..90af2c7cb1c55 100644
--- a/drivers/crypto/ti/Kconfig
+++ b/drivers/crypto/ti/Kconfig
@@ -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
diff --git a/drivers/crypto/ti/Makefile b/drivers/crypto/ti/Makefile
index b883078f203d7..a90bc97a52321 100644
--- a/drivers/crypto/ti/Makefile
+++ b/drivers/crypto/ti/Makefile
@@ -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
diff --git a/drivers/crypto/ti/dthev2-aes.c b/drivers/crypto/ti/dthev2-aes.c
index 3d494de552656..60a9b5e14dc80 100644
--- a/drivers/crypto/ti/dthev2-aes.c
+++ b/drivers/crypto/ti/dthev2-aes.c
@@ -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,11 +104,10 @@ 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)
{
- struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
+ struct dthe_aes_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
const char *alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(tfm));
ctx->dev = dthe_get_dev();
@@ -131,7 +129,7 @@ static int dthe_cipher_init_tfm(struct crypto_skcipher *tfm)
static void dthe_cipher_exit_tfm(struct crypto_skcipher *tfm)
{
- struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
+ struct dthe_aes_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
crypto_free_sync_skcipher(ctx->skcipher_fb);
dthe_put_dev(ctx->dev);
@@ -139,7 +137,7 @@ static void dthe_cipher_exit_tfm(struct crypto_skcipher *tfm)
static int dthe_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen)
{
- struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
+ struct dthe_aes_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
ctx->keylen = keylen;
memcpy(ctx->key, key, keylen);
@@ -154,7 +152,7 @@ static int dthe_aes_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned
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);
+ struct dthe_aes_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_256)
return -EINVAL;
@@ -166,7 +164,7 @@ static int dthe_aes_ecb_setkey(struct crypto_skcipher *tfm, const u8 *key, unsig
static int dthe_aes_cbc_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen)
{
- struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
+ struct dthe_aes_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_256)
return -EINVAL;
@@ -178,7 +176,7 @@ static int dthe_aes_cbc_setkey(struct crypto_skcipher *tfm, const u8 *key, unsig
static int dthe_aes_ctr_setkey(struct crypto_skcipher *tfm, const u8 *key, unsigned int keylen)
{
- struct dthe_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
+ struct dthe_aes_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_256)
return -EINVAL;
@@ -190,7 +188,7 @@ static int dthe_aes_ctr_setkey(struct crypto_skcipher *tfm, const u8 *key, unsig
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);
+ struct dthe_aes_tfm_ctx *ctx = crypto_skcipher_ctx(tfm);
int ret;
if (keylen != 2 * AES_KEYSIZE_128 &&
@@ -207,7 +205,7 @@ static int dthe_aes_xts_setkey(struct crypto_skcipher *tfm, const u8 *key, unsig
return 0;
}
-static void dthe_aes_set_ctrl_key(struct dthe_tfm_ctx *ctx,
+static void dthe_aes_set_ctrl_key(struct dthe_aes_tfm_ctx *ctx,
struct dthe_aes_req_ctx *rctx,
u32 *iv_in)
{
@@ -297,7 +295,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_tfm_ctx *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
struct dthe_aes_req_ctx *rctx = skcipher_request_ctx_dma(req);
SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->skcipher_fb);
@@ -322,7 +320,7 @@ static void dthe_aes_dma_in_callback(void *data)
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_tfm_ctx *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
struct dthe_aes_req_ctx *rctx = skcipher_request_ctx_dma(req);
struct dthe_data *dev_data = rctx->dev_data;
@@ -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;
@@ -528,7 +526,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_tfm_ctx *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
struct dthe_aes_req_ctx *rctx = skcipher_request_ctx_dma(req);
struct dthe_data *dev_data;
struct crypto_engine *engine;
@@ -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);
@@ -590,7 +588,7 @@ 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_aes_tfm_ctx *ctx = crypto_aead_ctx(tfm);
const char *alg_name = crypto_tfm_alg_name(crypto_aead_tfm(tfm));
ctx->dev = dthe_get_dev();
@@ -611,7 +609,7 @@ static int dthe_aead_init_tfm(struct crypto_aead *tfm)
static void dthe_aead_exit_tfm(struct crypto_aead *tfm)
{
- struct dthe_tfm_ctx *ctx = crypto_aead_ctx(tfm);
+ struct dthe_aes_tfm_ctx *ctx = crypto_aead_ctx(tfm);
crypto_free_sync_aead(ctx->aead_fb);
dthe_put_dev(ctx->dev);
@@ -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;
@@ -754,7 +752,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_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;
@@ -784,7 +782,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_tfm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
struct dthe_aes_req_ctx *rctx = aead_request_ctx_dma(req);
u32 tag_out[AES_BLOCK_WORDS];
u32 tag_in[AES_BLOCK_WORDS];
@@ -818,7 +816,7 @@ static int dthe_aead_dec_verify_tag(struct aead_request *req)
static int dthe_aead_setkey(struct crypto_aead *tfm, const u8 *key, unsigned int keylen)
{
- struct dthe_tfm_ctx *ctx = crypto_aead_ctx(tfm);
+ struct dthe_aes_tfm_ctx *ctx = crypto_aead_ctx(tfm);
if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 && keylen != AES_KEYSIZE_256)
return -EINVAL;
@@ -833,7 +831,7 @@ static int dthe_aead_setkey(struct crypto_aead *tfm, const u8 *key, unsigned int
static int dthe_gcm_aes_setkey(struct crypto_aead *tfm, const u8 *key, unsigned int keylen)
{
- struct dthe_tfm_ctx *ctx = crypto_aead_ctx(tfm);
+ struct dthe_aes_tfm_ctx *ctx = crypto_aead_ctx(tfm);
int ret;
ret = dthe_aead_setkey(tfm, key, keylen);
@@ -849,7 +847,7 @@ static int dthe_gcm_aes_setkey(struct crypto_aead *tfm, const u8 *key, unsigned
static int dthe_ccm_aes_setkey(struct crypto_aead *tfm, const u8 *key, unsigned int keylen)
{
- struct dthe_tfm_ctx *ctx = crypto_aead_ctx(tfm);
+ struct dthe_aes_tfm_ctx *ctx = crypto_aead_ctx(tfm);
int ret;
ret = dthe_aead_setkey(tfm, key, keylen);
@@ -865,7 +863,7 @@ static int dthe_ccm_aes_setkey(struct crypto_aead *tfm, const u8 *key, unsigned
static int dthe_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
{
- struct dthe_tfm_ctx *ctx = crypto_aead_ctx(tfm);
+ struct dthe_aes_tfm_ctx *ctx = crypto_aead_ctx(tfm);
/* Invalid auth size will be handled by crypto_aead_setauthsize() */
ctx->authsize = authsize;
@@ -875,7 +873,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_tfm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
struct dthe_aes_req_ctx *rctx = aead_request_ctx_dma(req);
SYNC_AEAD_REQUEST_ON_STACK(subreq, ctx->aead_fb);
@@ -900,7 +898,7 @@ static void dthe_aead_dma_in_callback(void *data)
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_tfm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
struct dthe_aes_req_ctx *rctx = aead_request_ctx_dma(req);
struct dthe_data *dev_data = rctx->dev_data;
@@ -1209,7 +1207,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_tfm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
struct dthe_aes_req_ctx *rctx = aead_request_ctx_dma(req);
struct dthe_data *dev_data;
struct crypto_engine *engine;
@@ -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);
@@ -1315,7 +1313,7 @@ static struct skcipher_engine_alg cipher_algs[] = {
CRYPTO_ALG_NEED_FALLBACK,
.cra_alignmask = AES_BLOCK_SIZE - 1,
.cra_blocksize = AES_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct dthe_tfm_ctx),
+ .cra_ctxsize = sizeof(struct dthe_aes_tfm_ctx),
.cra_reqsize = sizeof(struct dthe_aes_req_ctx) +
CRYPTO_DMA_PADDING,
.cra_module = THIS_MODULE,
@@ -1341,7 +1339,7 @@ static struct skcipher_engine_alg cipher_algs[] = {
CRYPTO_ALG_NEED_FALLBACK,
.cra_alignmask = AES_BLOCK_SIZE - 1,
.cra_blocksize = AES_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct dthe_tfm_ctx),
+ .cra_ctxsize = sizeof(struct dthe_aes_tfm_ctx),
.cra_reqsize = sizeof(struct dthe_aes_req_ctx) +
CRYPTO_DMA_PADDING,
.cra_module = THIS_MODULE,
@@ -1367,7 +1365,7 @@ static struct skcipher_engine_alg cipher_algs[] = {
CRYPTO_ALG_KERN_DRIVER_ONLY |
CRYPTO_ALG_NEED_FALLBACK,
.cra_blocksize = 1,
- .cra_ctxsize = sizeof(struct dthe_tfm_ctx),
+ .cra_ctxsize = sizeof(struct dthe_aes_tfm_ctx),
.cra_reqsize = sizeof(struct dthe_aes_req_ctx) +
CRYPTO_DMA_PADDING,
.cra_module = THIS_MODULE,
@@ -1393,7 +1391,7 @@ static struct skcipher_engine_alg cipher_algs[] = {
CRYPTO_ALG_NEED_FALLBACK,
.cra_alignmask = AES_BLOCK_SIZE - 1,
.cra_blocksize = AES_BLOCK_SIZE,
- .cra_ctxsize = sizeof(struct dthe_tfm_ctx),
+ .cra_ctxsize = sizeof(struct dthe_aes_tfm_ctx),
.cra_reqsize = sizeof(struct dthe_aes_req_ctx) +
CRYPTO_DMA_PADDING,
.cra_module = THIS_MODULE,
@@ -1422,7 +1420,7 @@ static struct aead_engine_alg aead_algs[] = {
CRYPTO_ALG_ASYNC |
CRYPTO_ALG_NEED_FALLBACK,
.cra_blocksize = 1,
- .cra_ctxsize = sizeof(struct dthe_tfm_ctx),
+ .cra_ctxsize = sizeof(struct dthe_aes_tfm_ctx),
.cra_reqsize = sizeof(struct dthe_aes_req_ctx) +
CRYPTO_DMA_PADDING,
.cra_module = THIS_MODULE,
@@ -1448,7 +1446,7 @@ static struct aead_engine_alg aead_algs[] = {
CRYPTO_ALG_ASYNC |
CRYPTO_ALG_NEED_FALLBACK,
.cra_blocksize = 1,
- .cra_ctxsize = sizeof(struct dthe_tfm_ctx),
+ .cra_ctxsize = sizeof(struct dthe_aes_tfm_ctx),
.cra_reqsize = sizeof(struct dthe_aes_req_ctx) +
CRYPTO_DMA_PADDING,
.cra_module = THIS_MODULE,
diff --git a/drivers/crypto/ti/dthev2-common.c b/drivers/crypto/ti/dthev2-common.c
index 0507bcf952c29..4bac096870632 100644
--- a/drivers/crypto/ti/dthev2-common.c
+++ b/drivers/crypto/ti/dthev2-common.c
@@ -127,6 +127,11 @@ static int dthe_dma_init(struct dthe_data *dev_data)
goto err_dma_sha_tx;
}
+ /*
+ * Do AES Rx and Tx channel config here because it is invariant of AES mode
+ * SHA Tx channel config is done before DMA transfer depending on hashing algorithm
+ */
+
memzero_explicit(&cfg, sizeof(cfg));
cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
@@ -161,11 +166,21 @@ static int dthe_dma_init(struct dthe_data *dev_data)
static int dthe_register_algs(void)
{
- return dthe_register_aes_algs();
+ int ret = 0;
+
+ ret = dthe_register_hash_algs();
+ if (ret)
+ return ret;
+ ret = dthe_register_aes_algs();
+ if (ret)
+ dthe_unregister_hash_algs();
+
+ return ret;
}
static void dthe_unregister_algs(void)
{
+ dthe_unregister_hash_algs();
dthe_unregister_aes_algs();
}
@@ -188,15 +203,26 @@ static int dthe_probe(struct platform_device *pdev)
if (ret)
return ret;
- dev_data->engine = crypto_engine_alloc_init(dev, 1);
- if (!dev_data->engine) {
+ dev_data->aes_engine = crypto_engine_alloc_init(dev, 1);
+ if (!dev_data->aes_engine) {
ret = -ENOMEM;
goto probe_engine_err;
}
+ dev_data->hash_engine = crypto_engine_alloc_init(dev, 1);
+ if (!dev_data->hash_engine) {
+ ret = -ENOMEM;
+ goto probe_hash_engine_err;
+ }
+
+ ret = crypto_engine_start(dev_data->aes_engine);
+ if (ret) {
+ dev_err(dev, "Failed to start crypto engine for AES\n");
+ goto probe_engine_start_err;
+ }
- ret = crypto_engine_start(dev_data->engine);
+ ret = crypto_engine_start(dev_data->hash_engine);
if (ret) {
- dev_err(dev, "Failed to start crypto engine\n");
+ dev_err(dev, "Failed to start crypto engine for hash\n");
goto probe_engine_start_err;
}
@@ -225,7 +251,9 @@ static int dthe_probe(struct platform_device *pdev)
dthe_put_drvdata(dev_data);
wait_for_completion(&dev_data->drain_compl);
probe_engine_start_err:
- crypto_engine_exit(dev_data->engine);
+ crypto_engine_exit(dev_data->hash_engine);
+probe_hash_engine_err:
+ crypto_engine_exit(dev_data->aes_engine);
probe_engine_err:
dma_release_channel(dev_data->dma_aes_rx);
dma_release_channel(dev_data->dma_aes_tx);
@@ -248,7 +276,8 @@ static void dthe_remove(struct platform_device *pdev)
dthe_put_drvdata(dev_data);
wait_for_completion(&dev_data->drain_compl);
- crypto_engine_exit(dev_data->engine);
+ crypto_engine_exit(dev_data->aes_engine);
+ crypto_engine_exit(dev_data->hash_engine);
dma_release_channel(dev_data->dma_aes_rx);
dma_release_channel(dev_data->dma_aes_tx);
diff --git a/drivers/crypto/ti/dthev2-common.h b/drivers/crypto/ti/dthev2-common.h
index f1b0396b322a0..ec067da996fb9 100644
--- a/drivers/crypto/ti/dthev2-common.h
+++ b/drivers/crypto/ti/dthev2-common.h
@@ -17,6 +17,7 @@
#include <crypto/internal/aead.h>
#include <crypto/internal/hash.h>
#include <crypto/internal/skcipher.h>
+#include <crypto/sha2.h>
#include <linux/completion.h>
#include <linux/delay.h>
@@ -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 (10 * USEC_PER_MSEC)
/*
- * Size of largest possible key (of all algorithms) to be stored in dthe_tfm_ctx
+ * Size of largest possible key (of all algorithms) to be stored in dthe_aes_tfm_ctx
* This is currently the keysize of XTS-AES-256 which is 512 bits (64 bytes)
*/
#define DTHE_MAX_KEYSIZE (AES_MAX_KEY_SIZE * 2)
+enum dthe_hash_alg_sel {
+ DTHE_HASH_MD5 = 0,
+ DTHE_HASH_SHA1 = BIT(1),
+ DTHE_HASH_SHA224 = BIT(2),
+ DTHE_HASH_SHA256 = BIT(1) | BIT(2),
+ DTHE_HASH_SHA384 = BIT(0),
+ DTHE_HASH_SHA512 = BIT(0) | BIT(1),
+ DTHE_HASH_ERR = BIT(0) | BIT(1) | BIT(2),
+};
+
enum dthe_aes_mode {
DTHE_AES_ECB = 0,
DTHE_AES_CBC,
@@ -50,7 +63,8 @@ enum dthe_aes_mode {
* @dev: Device pointer
* @regs: Base address of the register space
* @list: list node for dev
- * @engine: Crypto engine instance
+ * @aes_engine: Crypto engine instance for AES Engine
+ * @hash_engine: Crypto engine instance for Hashing Engine
* @dma_aes_rx: AES Rx DMA Channel
* @dma_aes_tx: AES Tx DMA Channel
* @dma_sha_tx: SHA Tx DMA Channel
@@ -62,7 +76,8 @@ struct dthe_data {
struct device *dev;
void __iomem *regs;
struct list_head list;
- struct crypto_engine *engine;
+ struct crypto_engine *aes_engine;
+ struct crypto_engine *hash_engine;
struct dma_chan *dma_aes_rx;
struct dma_chan *dma_aes_tx;
@@ -84,7 +99,7 @@ struct dthe_list {
};
/**
- * struct dthe_tfm_ctx - Transform ctx struct containing ctx for all sub-components of DTHE V2
+ * struct dthe_aes_tfm_ctx - Transform ctx struct for AES engine
* @dev: Device this transform is bound to.
* @keylen: AES key length
* @authsize: Authentication size for modes with authentication
@@ -93,7 +108,7 @@ struct dthe_list {
* @aead_fb: Fallback crypto aead handle
* @skcipher_fb: Fallback crypto skcipher handle for AES-XTS mode
*/
-struct dthe_tfm_ctx {
+struct dthe_aes_tfm_ctx {
struct device *dev;
unsigned int keylen;
unsigned int authsize;
@@ -127,6 +142,43 @@ struct dthe_aes_req_ctx {
struct dthe_data *dev_data;
};
+/**
+ * struct dthe_hash_tfm_ctx - Transform ctx struct for hashing engine
+ * @dev: Device this transform is bound to.
+ * @hash_mode: Hashing Engine mode
+ * @idigest_size: intermediate digest size in bytes of the hash algorithm selected
+ */
+struct dthe_hash_tfm_ctx {
+ struct device *dev;
+ enum dthe_hash_alg_sel hash_mode;
+ unsigned int idigest_size;
+};
+
+/**
+ * struct dthe_hash_req_ctx - Hashing engine ctx struct
+ * @idigest: buffer to store intermediate digest from a previous operation
+ * @digestcnt: stores the digest count (in bytes) from a previous operation; hardware only
+ * provides a single 32-bit value even for SHA384/512
+ * @padding: padding buffer for handling unaligned data
+ * @flags: bitfield of flags for internal use.
+ * bit 0-1: Current operation (update/finup/key preprocessing)
+ * bit 2: If intermediate digest is available from a previous operation
+ * @hash_compl: Completion variable for use in manual completion in case of DMA callback failure
+ * @dev_data: Device data struct pointer
+ */
+struct dthe_hash_req_ctx {
+ u32 idigest[SHA512_DIGEST_SIZE / sizeof(u32)];
+ u32 digestcnt;
+
+ __dma_from_device_group_begin();
+ u8 padding[SHA512_BLOCK_SIZE];
+ __dma_from_device_group_end();
+
+ u8 flags;
+ struct completion hash_compl;
+ struct dthe_data *dev_data;
+};
+
/* Struct definitions end */
/**
@@ -185,4 +237,7 @@ struct scatterlist *dthe_copy_sg(struct scatterlist *dst,
int dthe_register_aes_algs(void);
void dthe_unregister_aes_algs(void);
+int dthe_register_hash_algs(void);
+void dthe_unregister_hash_algs(void);
+
#endif
diff --git a/drivers/crypto/ti/dthev2-hash.c b/drivers/crypto/ti/dthev2-hash.c
new file mode 100644
index 0000000000000..bc5931de55378
--- /dev/null
+++ b/drivers/crypto/ti/dthev2-hash.c
@@ -0,0 +1,780 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * K3 DTHE V2 crypto accelerator driver
+ *
+ * Copyright (C) Texas Instruments 2025 - https://www.ti.com
+ * Author: T Pratham <t-pratham@ti.com>
+ */
+
+#include <crypto/algapi.h>
+#include <crypto/hash.h>
+#include <crypto/internal/hash.h>
+#include <crypto/sha2.h>
+
+#include "dthev2-common.h"
+
+#include <linux/bitfield.h>
+#include <linux/delay.h>
+#include <linux/dmaengine.h>
+#include <linux/dma-mapping.h>
+#include <linux/io.h>
+#include <linux/scatterlist.h>
+
+/* Registers */
+
+#define DTHE_P_HASH_BASE 0x5000
+#define DTHE_P_HASH512_IDIGEST_A 0x0240
+#define DTHE_P_HASH512_DIGEST_COUNT 0x0280
+#define DTHE_P_HASH512_MODE 0x0284
+#define DTHE_P_HASH512_LENGTH 0x0288
+#define DTHE_P_HASH512_DATA_IN_START 0x0080
+#define DTHE_P_HASH512_DATA_IN_END 0x00FC
+
+#define DTHE_P_HASH_SYSCONFIG 0x0110
+#define DTHE_P_HASH_IRQSTATUS 0x0118
+#define DTHE_P_HASH_IRQENABLE 0x011C
+
+/* Register write values and macros */
+#define DTHE_HASH_SYSCONFIG_INT_EN BIT(2)
+#define DTHE_HASH_SYSCONFIG_DMA_EN BIT(3)
+#define DTHE_HASH_IRQENABLE_EN_ALL GENMASK(3, 0)
+#define DTHE_HASH_IRQSTATUS_OP_READY BIT(0)
+#define DTHE_HASH_IRQSTATUS_IP_READY BIT(1)
+#define DTHE_HASH_IRQSTATUS_PH_READY BIT(2)
+#define DTHE_HASH_IRQSTATUS_CTX_READY BIT(3)
+
+#define DTHE_HASH_MODE_USE_ALG_CONST BIT(3)
+#define DTHE_HASH_MODE_CLOSE_HASH BIT(4)
+
+/* Size of the exported state: hash state + byte count. */
+#define DTHE_SHA256_STATESIZE (SHA256_DIGEST_SIZE + sizeof(u64))
+#define DTHE_SHA512_STATESIZE (SHA512_DIGEST_SIZE + 2 * sizeof(u64))
+
+static_assert(offsetof(struct __sha256_ctx, buf) == DTHE_SHA256_STATESIZE);
+static_assert(offsetof(struct __sha512_ctx, buf) == DTHE_SHA512_STATESIZE);
+
+/* rctx flag bitfields */
+#define DTHE_HASH_FLAG_OP BIT(0)
+#define DTHE_HASH_FLAG_IDIGEST_AVAIL BIT(1)
+
+enum dthe_hash_op {
+ DTHE_HASH_OP_UPDATE = 0,
+ DTHE_HASH_OP_FINUP = 1,
+};
+
+static inline bool dthe_hash_has_flag(struct dthe_hash_req_ctx *rctx, u8 flag)
+{
+ return rctx->flags & flag;
+}
+
+static inline void dthe_hash_flag_set(struct dthe_hash_req_ctx *rctx, u8 flag)
+{
+ rctx->flags |= flag;
+}
+
+static inline void dthe_hash_flag_clear(struct dthe_hash_req_ctx *rctx, u8 flag)
+{
+ rctx->flags &= ~flag;
+}
+
+static inline bool dthe_hash_flag_is_op(struct dthe_hash_req_ctx *rctx, enum dthe_hash_op op)
+{
+ return FIELD_GET(DTHE_HASH_FLAG_OP, rctx->flags) == op;
+}
+
+static inline void dthe_hash_flag_set_op(struct dthe_hash_req_ctx *rctx, enum dthe_hash_op op)
+{
+ FIELD_MODIFY(DTHE_HASH_FLAG_OP, &rctx->flags, op);
+}
+
+static enum dthe_hash_alg_sel dthe_hash_get_hash_mode(struct crypto_ahash *tfm)
+{
+ unsigned int ds = crypto_ahash_digestsize(tfm);
+ enum dthe_hash_alg_sel hash_mode;
+
+ /*
+ * Currently, all hash algorithms supported by DTHEv2 have unique digest sizes.
+ * So we can do this. Otherwise, we would have to get the algorithm from the
+ * alg_name and do a strcmp.
+ */
+ switch (ds) {
+ case SHA512_DIGEST_SIZE:
+ hash_mode = DTHE_HASH_SHA512;
+ break;
+ case SHA384_DIGEST_SIZE:
+ hash_mode = DTHE_HASH_SHA384;
+ break;
+ case SHA256_DIGEST_SIZE:
+ hash_mode = DTHE_HASH_SHA256;
+ break;
+ case SHA224_DIGEST_SIZE:
+ hash_mode = DTHE_HASH_SHA224;
+ break;
+ default:
+ hash_mode = DTHE_HASH_ERR;
+ break;
+ }
+
+ return hash_mode;
+}
+
+static unsigned int dthe_hash_get_idigest_size(struct dthe_hash_tfm_ctx *ctx)
+{
+ unsigned int idigest_size = 0;
+
+ switch (ctx->hash_mode) {
+ case DTHE_HASH_SHA512:
+ case DTHE_HASH_SHA384:
+ idigest_size = SHA512_DIGEST_SIZE;
+ break;
+ case DTHE_HASH_SHA256:
+ case DTHE_HASH_SHA224:
+ idigest_size = SHA256_DIGEST_SIZE;
+ break;
+ default:
+ /* We should not reach here. Print a warning */
+ dev_warn(ctx->dev, "hash_mode not supported or corrupted! Results might be incorrect\n");
+ break;
+ }
+
+ return idigest_size;
+}
+
+static const u32 sha224_init_state[SHA256_DIGEST_SIZE / sizeof(u32)] = {
+ SHA224_H0, SHA224_H1, SHA224_H2, SHA224_H3,
+ SHA224_H4, SHA224_H5, SHA224_H6, SHA224_H7
+};
+
+static const u32 sha256_init_state[SHA256_DIGEST_SIZE / sizeof(u32)] = {
+ SHA256_H0, SHA256_H1, SHA256_H2, SHA256_H3,
+ SHA256_H4, SHA256_H5, SHA256_H6, SHA256_H7
+};
+
+static const u64 sha384_init_state[SHA512_DIGEST_SIZE / sizeof(u64)] = {
+ SHA384_H0, SHA384_H1, SHA384_H2, SHA384_H3,
+ SHA384_H4, SHA384_H5, SHA384_H6, SHA384_H7
+};
+
+static const u64 sha512_init_state[SHA512_DIGEST_SIZE / sizeof(u64)] = {
+ SHA512_H0, SHA512_H1, SHA512_H2, SHA512_H3,
+ SHA512_H4, SHA512_H5, SHA512_H6, SHA512_H7
+};
+
+static const void *dthe_hash_get_init_state(struct dthe_hash_tfm_ctx *ctx)
+{
+ switch (ctx->hash_mode) {
+ case DTHE_HASH_SHA224:
+ return sha224_init_state;
+ case DTHE_HASH_SHA256:
+ return sha256_init_state;
+ case DTHE_HASH_SHA384:
+ return sha384_init_state;
+ case DTHE_HASH_SHA512:
+ return sha512_init_state;
+ default:
+ return NULL;
+ }
+}
+
+static int dthe_hash_init_tfm(struct crypto_ahash *tfm)
+{
+ struct dthe_hash_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
+
+ ctx->dev = dthe_get_dev();
+ if (!ctx->dev)
+ return -ENODEV;
+
+ ctx->hash_mode = dthe_hash_get_hash_mode(tfm);
+ if (ctx->hash_mode == DTHE_HASH_ERR) {
+ dthe_put_dev(ctx->dev);
+ return -EINVAL;
+ }
+
+ ctx->idigest_size = dthe_hash_get_idigest_size(ctx);
+
+ return 0;
+}
+
+static void dthe_hash_exit_tfm(struct crypto_ahash *tfm)
+{
+ struct dthe_hash_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
+
+ dthe_put_dev(ctx->dev);
+}
+
+static int dthe_hash_config_dma_chan(struct dma_chan *chan, struct crypto_ahash *tfm)
+{
+ struct dma_slave_config cfg;
+ int bs = crypto_ahash_blocksize(tfm);
+
+ memzero_explicit(&cfg, sizeof(cfg));
+
+ cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
+ cfg.dst_maxburst = bs / 4;
+
+ return dmaengine_slave_config(chan, &cfg);
+}
+
+static void dthe_hash_dma_in_callback(void *data)
+{
+ struct ahash_request *req = (struct ahash_request *)data;
+ struct dthe_hash_req_ctx *rctx = ahash_request_ctx_dma(req);
+
+ complete(&rctx->hash_compl);
+}
+
+static int dthe_hash_run_dma(struct ahash_request *req, struct scatterlist *src,
+ int src_nents, size_t len)
+{
+ struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
+ struct dthe_hash_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
+ struct dthe_hash_req_ctx *rctx = ahash_request_ctx_dma(req);
+ struct dthe_data *dev_data = rctx->dev_data;
+ struct device *tx_dev;
+ struct dma_async_tx_descriptor *desc_out;
+ int mapped_nents;
+ enum dma_data_direction src_dir = DMA_TO_DEVICE;
+ u32 hash_mode;
+ int ds = crypto_ahash_digestsize(tfm);
+ int ret = 0;
+ u32 *dst;
+ u32 dst_len;
+ u32 _status;
+ void __iomem *sha_base_reg = dev_data->regs + DTHE_P_HASH_BASE;
+
+ u32 hash_sysconfig_val = DTHE_HASH_SYSCONFIG_INT_EN | DTHE_HASH_SYSCONFIG_DMA_EN;
+ u32 hash_irqenable_val = DTHE_HASH_IRQENABLE_EN_ALL;
+
+ writel_relaxed(hash_sysconfig_val, sha_base_reg + DTHE_P_HASH_SYSCONFIG);
+ writel_relaxed(hash_irqenable_val, sha_base_reg + DTHE_P_HASH_IRQENABLE);
+
+ /* Config SHA DMA channel as per SHA mode */
+ ret = dthe_hash_config_dma_chan(dev_data->dma_sha_tx, tfm);
+ if (ret) {
+ dev_err(dev_data->dev, "Can't configure sha_tx dmaengine slave: %d\n", ret);
+ goto hash_err;
+ }
+
+ tx_dev = dmaengine_get_dma_device(dev_data->dma_sha_tx);
+ if (!tx_dev) {
+ ret = -ENODEV;
+ goto hash_err;
+ }
+
+ mapped_nents = dma_map_sg(tx_dev, src, src_nents, src_dir);
+ if (mapped_nents == 0) {
+ ret = -EINVAL;
+ goto hash_err;
+ }
+
+ 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;
+ }
+
+ dmaengine_desc_set_callback(desc_out, dthe_hash_dma_in_callback, NULL, req);
+
+ init_completion(&rctx->hash_compl);
+
+ hash_mode = ctx->hash_mode;
+
+ if (dthe_hash_flag_is_op(rctx, DTHE_HASH_OP_FINUP))
+ hash_mode |= DTHE_HASH_MODE_CLOSE_HASH;
+
+ if (dthe_hash_has_flag(rctx, DTHE_HASH_FLAG_IDIGEST_AVAIL)) {
+ for (int i = 0; i < ctx->idigest_size / sizeof(u32); ++i)
+ writel_relaxed(rctx->idigest[i],
+ sha_base_reg +
+ DTHE_P_HASH512_IDIGEST_A +
+ (DTHE_REG_SIZE * i));
+
+ writel_relaxed(rctx->digestcnt,
+ sha_base_reg + DTHE_P_HASH512_DIGEST_COUNT);
+ } else {
+ hash_mode |= DTHE_HASH_MODE_USE_ALG_CONST;
+ }
+
+ writel_relaxed(hash_mode, sha_base_reg + DTHE_P_HASH512_MODE);
+ writel_relaxed(len, sha_base_reg + DTHE_P_HASH512_LENGTH);
+
+ dmaengine_submit(desc_out);
+
+ dma_async_issue_pending(dev_data->dma_sha_tx);
+
+ ret = wait_for_completion_timeout(&rctx->hash_compl,
+ msecs_to_jiffies(DTHE_DMA_TIMEOUT_MS));
+ if (!ret) {
+ dmaengine_terminate_sync(dev_data->dma_sha_tx);
+ ret = -ETIMEDOUT;
+ } else {
+ ret = 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.
+ */
+ if (dthe_hash_flag_is_op(rctx, DTHE_HASH_OP_UPDATE)) {
+ /* If coming from update, we need to read the idigest and store it for future */
+ dst = rctx->idigest;
+ dst_len = ctx->idigest_size / sizeof(u32);
+ } else {
+ /* If coming from finup or final, we need to read the final digest */
+ dst = (u32 *)req->result;
+ dst_len = ds / sizeof(u32);
+ }
+
+ for (int i = 0; i < dst_len; ++i)
+ put_unaligned(readl_relaxed(sha_base_reg +
+ DTHE_P_HASH512_IDIGEST_A +
+ (DTHE_REG_SIZE * i)),
+ &dst[i]);
+
+ rctx->digestcnt = readl_relaxed(sha_base_reg + DTHE_P_HASH512_DIGEST_COUNT);
+ dthe_hash_flag_set(rctx, DTHE_HASH_FLAG_IDIGEST_AVAIL);
+
+hash_prep_err:
+ dma_unmap_sg(tx_dev, src, src_nents, src_dir);
+hash_err:
+ return ret;
+}
+
+static int dthe_hash_run(struct crypto_engine *engine, void *areq)
+{
+ struct ahash_request *req = container_of(areq, struct ahash_request, base);
+ struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
+ struct dthe_hash_req_ctx *rctx = ahash_request_ctx_dma(req);
+ struct dthe_data *dev_data = rctx->dev_data;
+
+ struct scatterlist *src, *sg;
+ int src_nents = 0;
+ unsigned int bs = crypto_ahash_blocksize(tfm);
+ unsigned int tot_len = req->nbytes;
+ unsigned int len_to_process;
+ unsigned int len_to_buffer;
+ unsigned int pad_len = 0;
+ u8 *pad_buf = rctx->padding;
+ int ret = 0;
+
+ if (dthe_hash_flag_is_op(rctx, DTHE_HASH_OP_UPDATE)) {
+ len_to_buffer = tot_len % bs;
+ len_to_process = tot_len - len_to_buffer;
+
+ if (len_to_process == 0) {
+ ret = len_to_buffer;
+ goto hash_buf_all;
+ }
+ } else {
+ len_to_process = tot_len;
+ len_to_buffer = 0;
+ }
+
+ src_nents = sg_nents_for_len(req->src, len_to_process);
+ if (src_nents < 0) {
+ ret = src_nents;
+ goto hash_buf_all;
+ }
+
+ /*
+ * Certain DMA restrictions forced us to send data in multiples of BLOCK_SIZE
+ * bytes. So, add a padding 0s at the end of src scatterlist if data is not a
+ * multiple of block_size bytes (Can only happen in final or finup). The extra
+ * data is ignored by the DTHE hardware.
+ */
+ if (len_to_process % bs) {
+ pad_len = bs - (len_to_process % bs);
+ src_nents++;
+ }
+
+ src = kzalloc_objs(*src, src_nents, GFP_KERNEL);
+ if (!src) {
+ ret = -ENOMEM;
+ goto hash_buf_all;
+ }
+
+ sg_init_table(src, src_nents);
+ sg = dthe_copy_sg(src, req->src, len_to_process);
+ if (pad_len > 0) {
+ memset(pad_buf, 0, pad_len);
+ sg_set_buf(sg, pad_buf, pad_len);
+ }
+
+ ret = dthe_hash_run_dma(req, src, src_nents, len_to_process);
+ if (!ret)
+ ret = len_to_buffer;
+
+ kfree(src);
+
+hash_buf_all:
+ local_bh_disable();
+ crypto_finalize_hash_request(engine, req, ret);
+ local_bh_enable();
+ dthe_put_drvdata(dev_data);
+ return 0;
+}
+
+static int dthe_hash_transfer_to_engine(struct dthe_hash_tfm_ctx *ctx, struct ahash_request *req)
+{
+ struct dthe_hash_req_ctx *rctx = ahash_request_ctx_dma(req);
+ struct dthe_data *dev_data;
+ int ret;
+
+ dev_data = dthe_get_drvdata(ctx->dev);
+ if (!dev_data)
+ return -ENODEV;
+
+ rctx->dev_data = dev_data;
+
+ ret = crypto_transfer_hash_request_to_engine(dev_data->hash_engine, req);
+ if (ret != -EINPROGRESS && ret != -EBUSY)
+ dthe_put_drvdata(dev_data);
+
+ return ret;
+}
+
+/*
+ * DTHE uses the same digest-byte stream order for every algorithm; convert it
+ * to the native software state words using each hash's word encoding.
+ */
+static void dthe_hash_state_to_sw(struct dthe_hash_tfm_ctx *ctx,
+ const u32 *hw_state, void *sw_state)
+{
+ union {
+ const u32 *u32;
+ const u64 *u64;
+ } src = { .u32 = hw_state };
+ union {
+ u32 *u32;
+ u64 *u64;
+ } dst = { .u32 = sw_state };
+ int i;
+
+ switch (ctx->hash_mode) {
+ case DTHE_HASH_SHA512:
+ case DTHE_HASH_SHA384:
+ for (i = 0; i < ctx->idigest_size / sizeof(u64); i++)
+ put_unaligned(get_unaligned_be64(&src.u64[i]), &dst.u64[i]);
+ break;
+ case DTHE_HASH_SHA256:
+ case DTHE_HASH_SHA224:
+ for (i = 0; i < ctx->idigest_size / sizeof(u32); i++)
+ put_unaligned(get_unaligned_be32(&src.u32[i]), &dst.u32[i]);
+ break;
+ default:
+ /* We should not reach here. Print a warning */
+ dev_warn(ctx->dev, "hash_mode not supported or corrupted! Results might be incorrect\n");
+ break;
+ }
+}
+
+static void dthe_hash_state_from_sw(struct dthe_hash_tfm_ctx *ctx,
+ const void *sw_state, u32 *hw_state)
+{
+ union {
+ const u32 *u32;
+ const u64 *u64;
+ } src = { .u32 = sw_state };
+ union {
+ u32 *u32;
+ u64 *u64;
+ } dst = { .u32 = hw_state };
+ int i;
+
+ switch (ctx->hash_mode) {
+ case DTHE_HASH_SHA512:
+ case DTHE_HASH_SHA384:
+ for (i = 0; i < ctx->idigest_size / sizeof(u64); i++)
+ put_unaligned_be64(get_unaligned(&src.u64[i]), &dst.u64[i]);
+ break;
+ case DTHE_HASH_SHA256:
+ case DTHE_HASH_SHA224:
+ for (i = 0; i < ctx->idigest_size / sizeof(u32); i++)
+ put_unaligned_be32(get_unaligned(&src.u32[i]), &dst.u32[i]);
+ break;
+ default:
+ /* We should not reach here. Print a warning */
+ dev_warn(ctx->dev, "hash_mode not supported or corrupted! Results might be incorrect\n");
+ break;
+ }
+}
+
+static int dthe_hash_export(struct ahash_request *req, void *out)
+{
+ struct dthe_hash_req_ctx *rctx = ahash_request_ctx_dma(req);
+ struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
+ struct dthe_hash_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
+ union {
+ u8 *u8;
+ u64 *u64;
+ } p = { .u8 = out };
+
+ if (dthe_hash_has_flag(rctx, DTHE_HASH_FLAG_IDIGEST_AVAIL))
+ dthe_hash_state_to_sw(ctx, rctx->idigest, out);
+ else
+ memcpy(out, dthe_hash_get_init_state(ctx), ctx->idigest_size);
+
+ p.u8 += ctx->idigest_size;
+ put_unaligned(rctx->digestcnt, p.u64++);
+
+ if (ctx->idigest_size == SHA512_DIGEST_SIZE)
+ put_unaligned(0, p.u64);
+
+ return 0;
+}
+
+static int dthe_hash_import(struct ahash_request *req, const void *in)
+{
+ struct dthe_hash_req_ctx *rctx = ahash_request_ctx_dma(req);
+ struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
+ struct dthe_hash_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
+ unsigned int bs = crypto_ahash_blocksize(tfm);
+ union {
+ const u8 *u8;
+ const u64 *u64;
+ } p = { .u8 = in };
+ u64 bytecnt;
+
+ p.u8 += ctx->idigest_size;
+ bytecnt = get_unaligned(p.u64++);
+
+ /* Hardware digest count is a 32-bit byte count, including the HMAC key block. */
+ if (ctx->idigest_size == SHA512_DIGEST_SIZE && get_unaligned(p.u64))
+ return -EOVERFLOW;
+ if (bytecnt > U32_MAX)
+ return -EOVERFLOW;
+ if (bytecnt % bs)
+ return -EINVAL;
+
+ rctx->flags = 0;
+ rctx->digestcnt = bytecnt;
+
+ if (bytecnt == 0)
+ return 0;
+
+ dthe_hash_state_from_sw(ctx, in, rctx->idigest);
+ dthe_hash_flag_set(rctx, DTHE_HASH_FLAG_IDIGEST_AVAIL);
+
+ return 0;
+}
+
+static int dthe_hash_init(struct ahash_request *req)
+{
+ struct dthe_hash_req_ctx *rctx = ahash_request_ctx_dma(req);
+
+ rctx->flags = 0;
+ rctx->digestcnt = 0;
+
+ return 0;
+}
+
+static int dthe_hash_update(struct ahash_request *req)
+{
+ struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
+ struct dthe_hash_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
+ struct dthe_hash_req_ctx *rctx = ahash_request_ctx_dma(req);
+
+ if (req->nbytes == 0)
+ return 0;
+
+ dthe_hash_flag_set_op(rctx, DTHE_HASH_OP_UPDATE);
+
+ return dthe_hash_transfer_to_engine(ctx, req);
+}
+
+/*
+ * Hardware cannot do CLOSE_HASH on zero bytes of data. Fallback to software for this.
+ */
+static int dthe_hash_final_zero_fb(struct ahash_request *req)
+{
+ HASH_FBREQ_ON_STACK(fbreq, req);
+ u8 state[DTHE_SHA512_STATESIZE];
+ int ret;
+
+ ahash_request_set_crypt(fbreq, NULL, req->result, 0);
+
+ ret = dthe_hash_export(req, state) ?:
+ crypto_ahash_import_core(fbreq, state) ?:
+ crypto_ahash_final(fbreq);
+
+ memzero_explicit(state, sizeof(state));
+ HASH_REQUEST_ZERO(fbreq);
+ return ret;
+}
+
+static int dthe_hash_final(struct ahash_request *req)
+{
+ struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
+ struct dthe_hash_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
+ struct dthe_hash_req_ctx *rctx = ahash_request_ctx_dma(req);
+
+ /* With AHASH_ALG_BLOCK_ONLY, nbytes includes the API's partial block. */
+ if (req->nbytes == 0)
+ return dthe_hash_final_zero_fb(req);
+
+ dthe_hash_flag_set_op(rctx, DTHE_HASH_OP_FINUP);
+
+ return dthe_hash_transfer_to_engine(ctx, req);
+}
+
+static int dthe_hash_finup(struct ahash_request *req)
+{
+ /* With AHASH_ALG_BLOCK_ONLY, final becomes same as finup. */
+ return dthe_hash_final(req);
+}
+
+static int dthe_hash_digest(struct ahash_request *req)
+{
+ dthe_hash_init(req);
+ return dthe_hash_finup(req);
+}
+
+static struct ahash_engine_alg hash_algs[] = {
+ {
+ .base.init_tfm = dthe_hash_init_tfm,
+ .base.exit_tfm = dthe_hash_exit_tfm,
+ .base.init = dthe_hash_init,
+ .base.update = dthe_hash_update,
+ .base.final = dthe_hash_final,
+ .base.finup = dthe_hash_finup,
+ .base.digest = dthe_hash_digest,
+ .base.export = dthe_hash_export,
+ .base.import = dthe_hash_import,
+ .base.halg = {
+ .digestsize = SHA512_DIGEST_SIZE,
+ .statesize = DTHE_SHA512_STATESIZE,
+ .base = {
+ .cra_name = "sha512",
+ .cra_driver_name = "sha512-dthev2",
+ .cra_priority = 299,
+ .cra_flags = CRYPTO_ALG_TYPE_AHASH |
+ CRYPTO_ALG_ASYNC |
+ CRYPTO_ALG_OPTIONAL_KEY |
+ CRYPTO_ALG_KERN_DRIVER_ONLY |
+ CRYPTO_ALG_ALLOCATES_MEMORY |
+ CRYPTO_AHASH_ALG_BLOCK_ONLY,
+ .cra_blocksize = SHA512_BLOCK_SIZE,
+ .cra_ctxsize = sizeof(struct dthe_hash_tfm_ctx),
+ .cra_reqsize = sizeof(struct dthe_hash_req_ctx) +
+ CRYPTO_DMA_PADDING,
+ .cra_module = THIS_MODULE,
+ }
+ },
+ .op.do_one_request = dthe_hash_run,
+ },
+ {
+ .base.init_tfm = dthe_hash_init_tfm,
+ .base.exit_tfm = dthe_hash_exit_tfm,
+ .base.init = dthe_hash_init,
+ .base.update = dthe_hash_update,
+ .base.final = dthe_hash_final,
+ .base.finup = dthe_hash_finup,
+ .base.digest = dthe_hash_digest,
+ .base.export = dthe_hash_export,
+ .base.import = dthe_hash_import,
+ .base.halg = {
+ .digestsize = SHA384_DIGEST_SIZE,
+ .statesize = DTHE_SHA512_STATESIZE,
+ .base = {
+ .cra_name = "sha384",
+ .cra_driver_name = "sha384-dthev2",
+ .cra_priority = 299,
+ .cra_flags = CRYPTO_ALG_TYPE_AHASH |
+ CRYPTO_ALG_ASYNC |
+ CRYPTO_ALG_OPTIONAL_KEY |
+ CRYPTO_ALG_KERN_DRIVER_ONLY |
+ CRYPTO_ALG_ALLOCATES_MEMORY |
+ CRYPTO_AHASH_ALG_BLOCK_ONLY,
+ .cra_blocksize = SHA384_BLOCK_SIZE,
+ .cra_ctxsize = sizeof(struct dthe_hash_tfm_ctx),
+ .cra_reqsize = sizeof(struct dthe_hash_req_ctx) +
+ CRYPTO_DMA_PADDING,
+ .cra_module = THIS_MODULE,
+ }
+ },
+ .op.do_one_request = dthe_hash_run,
+ },
+ {
+ .base.init_tfm = dthe_hash_init_tfm,
+ .base.exit_tfm = dthe_hash_exit_tfm,
+ .base.init = dthe_hash_init,
+ .base.update = dthe_hash_update,
+ .base.final = dthe_hash_final,
+ .base.finup = dthe_hash_finup,
+ .base.digest = dthe_hash_digest,
+ .base.export = dthe_hash_export,
+ .base.import = dthe_hash_import,
+ .base.halg = {
+ .digestsize = SHA256_DIGEST_SIZE,
+ .statesize = DTHE_SHA256_STATESIZE,
+ .base = {
+ .cra_name = "sha256",
+ .cra_driver_name = "sha256-dthev2",
+ .cra_priority = 299,
+ .cra_flags = CRYPTO_ALG_TYPE_AHASH |
+ CRYPTO_ALG_ASYNC |
+ CRYPTO_ALG_OPTIONAL_KEY |
+ CRYPTO_ALG_KERN_DRIVER_ONLY |
+ CRYPTO_ALG_ALLOCATES_MEMORY |
+ CRYPTO_AHASH_ALG_BLOCK_ONLY,
+ .cra_blocksize = SHA256_BLOCK_SIZE,
+ .cra_ctxsize = sizeof(struct dthe_hash_tfm_ctx),
+ .cra_reqsize = sizeof(struct dthe_hash_req_ctx) +
+ CRYPTO_DMA_PADDING,
+ .cra_module = THIS_MODULE,
+ }
+ },
+ .op.do_one_request = dthe_hash_run,
+ },
+ {
+ .base.init_tfm = dthe_hash_init_tfm,
+ .base.exit_tfm = dthe_hash_exit_tfm,
+ .base.init = dthe_hash_init,
+ .base.update = dthe_hash_update,
+ .base.final = dthe_hash_final,
+ .base.finup = dthe_hash_finup,
+ .base.digest = dthe_hash_digest,
+ .base.export = dthe_hash_export,
+ .base.import = dthe_hash_import,
+ .base.halg = {
+ .digestsize = SHA224_DIGEST_SIZE,
+ .statesize = DTHE_SHA256_STATESIZE,
+ .base = {
+ .cra_name = "sha224",
+ .cra_driver_name = "sha224-dthev2",
+ .cra_priority = 299,
+ .cra_flags = CRYPTO_ALG_TYPE_AHASH |
+ CRYPTO_ALG_ASYNC |
+ CRYPTO_ALG_OPTIONAL_KEY |
+ CRYPTO_ALG_KERN_DRIVER_ONLY |
+ CRYPTO_ALG_ALLOCATES_MEMORY |
+ CRYPTO_AHASH_ALG_BLOCK_ONLY,
+ .cra_blocksize = SHA224_BLOCK_SIZE,
+ .cra_ctxsize = sizeof(struct dthe_hash_tfm_ctx),
+ .cra_reqsize = sizeof(struct dthe_hash_req_ctx) +
+ CRYPTO_DMA_PADDING,
+ .cra_module = THIS_MODULE,
+ }
+ },
+ .op.do_one_request = dthe_hash_run,
+ },
+};
+
+int dthe_register_hash_algs(void)
+{
+ return crypto_engine_register_ahashes(hash_algs, ARRAY_SIZE(hash_algs));
+}
+
+void dthe_unregister_hash_algs(void)
+{
+ crypto_engine_unregister_ahashes(hash_algs, ARRAY_SIZE(hash_algs));
+}
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH v11 2/2] crypto: ti - Add support for HMAC in DTHEv2 driver
2026-12-04 16:42 [PATCH v11 0/2] Add support for hashing algorithms in TI DTHE V2 T Pratham
2026-12-04 16:42 ` [PATCH v11 1/2] crypto: ti - Add support for SHA224/256/384/512 in DTHEv2 driver T Pratham
@ 2026-12-04 16:42 ` T Pratham
2026-12-04 17:47 ` sashiko-bot
1 sibling, 1 reply; 5+ messages in thread
From: T Pratham @ 2026-12-04 16:42 UTC (permalink / raw)
To: T Pratham, Herbert Xu, David S. Miller
Cc: Sebin Francis, Manorit Chawdhry, Vishal Mahaveer,
Praneeth Bajjuri, linux-crypto, linux-kernel
Add support for HMAC-SHA512/384/256/224 algorithms in the hashing engine
driver of the DTHEv2 hardware cryptographic engine.
Signed-off-by: T Pratham <t-pratham@ti.com>
---
drivers/crypto/ti/Kconfig | 1 +
drivers/crypto/ti/dthev2-common.h | 23 +-
drivers/crypto/ti/dthev2-hash.c | 344 +++++++++++++++++++++++++++++-
3 files changed, 359 insertions(+), 9 deletions(-)
diff --git a/drivers/crypto/ti/Kconfig b/drivers/crypto/ti/Kconfig
index 90af2c7cb1c55..200728b919fca 100644
--- a/drivers/crypto/ti/Kconfig
+++ b/drivers/crypto/ti/Kconfig
@@ -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
diff --git a/drivers/crypto/ti/dthev2-common.h b/drivers/crypto/ti/dthev2-common.h
index ec067da996fb9..8aed29dd26c71 100644
--- a/drivers/crypto/ti/dthev2-common.h
+++ b/drivers/crypto/ti/dthev2-common.h
@@ -32,10 +32,15 @@
#define DTHE_DMA_TIMEOUT_MS 2000
#define DTHE_POLL_TIMEOUT_INTERVAL (10 * USEC_PER_MSEC)
/*
- * Size of largest possible key (of all algorithms) to be stored in dthe_aes_tfm_ctx
+ * Size of largest possible AES key to be stored in dthe_aes_tfm_ctx
* 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_AES_KEYSIZE (AES_MAX_KEY_SIZE * 2)
+/*
+ * Size of largest possible HMAC key to be stored in dthe_hash_tfm_ctx
+ * This is currently the keysize of HMAC-SHA512 which is 1024 bits (128 bytes)
+ */
+#define DTHE_MAX_HMAC_KEYSIZE (SHA512_BLOCK_SIZE)
enum dthe_hash_alg_sel {
DTHE_HASH_MD5 = 0,
@@ -112,7 +117,7 @@ struct dthe_aes_tfm_ctx {
struct device *dev;
unsigned int keylen;
unsigned int authsize;
- u32 key[DTHE_MAX_KEYSIZE / sizeof(u32)];
+ u32 key[DTHE_MAX_AES_KEYSIZE / sizeof(u32)];
enum dthe_aes_mode aes_mode;
union {
struct crypto_sync_aead *aead_fb;
@@ -147,11 +152,23 @@ struct dthe_aes_req_ctx {
* @dev: Device this transform is bound to.
* @hash_mode: Hashing Engine mode
* @idigest_size: intermediate digest size in bytes of the hash algorithm selected
+ * @keylen: Key length for HMAC algorithms
+ * @key: Buffer storing the raw HMAC key
+ * @istate: buffer to store the HMAC inner digest/ipad, in hardware register format
+ * @ostate: buffer to store the HMAC outer digest/opad, in hardware register
*/
struct dthe_hash_tfm_ctx {
struct device *dev;
enum dthe_hash_alg_sel hash_mode;
unsigned int idigest_size;
+ unsigned int keylen;
+ union {
+ u32 key[DTHE_MAX_HMAC_KEYSIZE / sizeof(u32)];
+ struct {
+ u32 istate[SHA512_DIGEST_SIZE / sizeof(u32)];
+ u32 ostate[SHA512_DIGEST_SIZE / sizeof(u32)];
+ };
+ };
};
/**
diff --git a/drivers/crypto/ti/dthev2-hash.c b/drivers/crypto/ti/dthev2-hash.c
index bc5931de55378..12cfebf251628 100644
--- a/drivers/crypto/ti/dthev2-hash.c
+++ b/drivers/crypto/ti/dthev2-hash.c
@@ -23,6 +23,7 @@
/* Registers */
#define DTHE_P_HASH_BASE 0x5000
+#define DTHE_P_HASH512_ODIGEST_A 0x0200
#define DTHE_P_HASH512_IDIGEST_A 0x0240
#define DTHE_P_HASH512_DIGEST_COUNT 0x0280
#define DTHE_P_HASH512_MODE 0x0284
@@ -45,6 +46,12 @@
#define DTHE_HASH_MODE_USE_ALG_CONST BIT(3)
#define DTHE_HASH_MODE_CLOSE_HASH BIT(4)
+#define DTHE_HASH_MODE_HMAC_KEY_PROCESSING BIT(5)
+#define DTHE_HASH_MODE_HMAC_OUTER_HASH BIT(7)
+
+/* Misc */
+#define DTHE_HMAC_SHA512_MAX_KEYSIZE (SHA512_BLOCK_SIZE)
+#define DTHE_HMAC_SHA256_MAX_KEYSIZE (SHA256_BLOCK_SIZE)
/* Size of the exported state: hash state + byte count. */
#define DTHE_SHA256_STATESIZE (SHA256_DIGEST_SIZE + sizeof(u64))
@@ -54,12 +61,13 @@ static_assert(offsetof(struct __sha256_ctx, buf) == DTHE_SHA256_STATESIZE);
static_assert(offsetof(struct __sha512_ctx, buf) == DTHE_SHA512_STATESIZE);
/* rctx flag bitfields */
-#define DTHE_HASH_FLAG_OP BIT(0)
-#define DTHE_HASH_FLAG_IDIGEST_AVAIL BIT(1)
+#define DTHE_HASH_FLAG_OP GENMASK(1, 0)
+#define DTHE_HASH_FLAG_IDIGEST_AVAIL BIT(2)
enum dthe_hash_op {
DTHE_HASH_OP_UPDATE = 0,
DTHE_HASH_OP_FINUP = 1,
+ DTHE_HASH_OP_KEY_PREPROCESS = 2,
};
static inline bool dthe_hash_has_flag(struct dthe_hash_req_ctx *rctx, u8 flag)
@@ -236,6 +244,7 @@ static int dthe_hash_run_dma(struct ahash_request *req, struct scatterlist *src,
enum dma_data_direction src_dir = DMA_TO_DEVICE;
u32 hash_mode;
int ds = crypto_ahash_digestsize(tfm);
+ bool is_hmac = (ctx->keylen > 0);
int ret = 0;
u32 *dst;
u32 dst_len;
@@ -281,8 +290,17 @@ static int dthe_hash_run_dma(struct ahash_request *req, struct scatterlist *src,
hash_mode = ctx->hash_mode;
- if (dthe_hash_flag_is_op(rctx, DTHE_HASH_OP_FINUP))
+ if (dthe_hash_flag_is_op(rctx, DTHE_HASH_OP_FINUP)) {
hash_mode |= DTHE_HASH_MODE_CLOSE_HASH;
+ if (is_hmac) {
+ hash_mode |= DTHE_HASH_MODE_HMAC_OUTER_HASH;
+ for (int i = 0; i < ctx->idigest_size / sizeof(u32); ++i)
+ writel_relaxed(ctx->ostate[i],
+ sha_base_reg +
+ DTHE_P_HASH512_ODIGEST_A +
+ (DTHE_REG_SIZE * i));
+ }
+ }
if (dthe_hash_has_flag(rctx, DTHE_HASH_FLAG_IDIGEST_AVAIL)) {
for (int i = 0; i < ctx->idigest_size / sizeof(u32); ++i)
@@ -293,6 +311,14 @@ static int dthe_hash_run_dma(struct ahash_request *req, struct scatterlist *src,
writel_relaxed(rctx->digestcnt,
sha_base_reg + DTHE_P_HASH512_DIGEST_COUNT);
+ } else if (is_hmac) {
+ for (int i = 0; i < ctx->idigest_size / sizeof(u32); ++i)
+ writel_relaxed(ctx->istate[i],
+ sha_base_reg +
+ DTHE_P_HASH512_IDIGEST_A +
+ (DTHE_REG_SIZE * i));
+ writel_relaxed(rctx->digestcnt,
+ sha_base_reg + DTHE_P_HASH512_DIGEST_COUNT);
} else {
hash_mode |= DTHE_HASH_MODE_USE_ALG_CONST;
}
@@ -349,6 +375,47 @@ static int dthe_hash_run_dma(struct ahash_request *req, struct scatterlist *src,
return ret;
}
+static int dthe_hmac_run_key_preprocess(struct crypto_ahash *tfm, struct dthe_hash_req_ctx *rctx)
+{
+ struct dthe_data *dev_data = rctx->dev_data;
+ struct dthe_hash_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
+ u32 hash_mode = ctx->hash_mode | DTHE_HASH_MODE_HMAC_KEY_PROCESSING;
+ void __iomem *sha_base_reg = dev_data->regs + DTHE_P_HASH_BASE;
+ u32 _status;
+ int ret;
+
+ for (int i = 0; i < (ctx->keylen / 2) / sizeof(u32); ++i)
+ writel_relaxed(ctx->key[i], sha_base_reg +
+ DTHE_P_HASH512_ODIGEST_A +
+ (DTHE_REG_SIZE * i));
+ for (int i = 0; i < (ctx->keylen / 2) / sizeof(u32); ++i)
+ writel_relaxed(ctx->key[i + (ctx->keylen / 2) / sizeof(u32)],
+ sha_base_reg +
+ DTHE_P_HASH512_IDIGEST_A +
+ (DTHE_REG_SIZE * i));
+ writel_relaxed(hash_mode, sha_base_reg + DTHE_P_HASH512_MODE);
+ writel_relaxed(0, sha_base_reg + DTHE_P_HASH512_LENGTH);
+
+ ret = readl_relaxed_poll_timeout(sha_base_reg + DTHE_P_HASH_IRQSTATUS, _status,
+ (_status & DTHE_HASH_IRQSTATUS_OP_READY),
+ 0, DTHE_POLL_TIMEOUT_INTERVAL);
+
+ for (int i = 0; i < ctx->idigest_size / sizeof(u32); ++i)
+ ctx->istate[i] = readl_relaxed(sha_base_reg +
+ DTHE_P_HASH512_IDIGEST_A +
+ (DTHE_REG_SIZE * i));
+
+ for (int i = 0; i < ctx->idigest_size / sizeof(u32); ++i)
+ ctx->ostate[i] = readl_relaxed(sha_base_reg +
+ DTHE_P_HASH512_ODIGEST_A +
+ (DTHE_REG_SIZE * i));
+
+ /* Need to read this to trigger completion */
+ rctx->digestcnt = readl_relaxed(sha_base_reg + DTHE_P_HASH512_DIGEST_COUNT);
+
+ return ret;
+}
+
static int dthe_hash_run(struct crypto_engine *engine, void *areq)
{
struct ahash_request *req = container_of(areq, struct ahash_request, base);
@@ -366,6 +433,11 @@ static int dthe_hash_run(struct crypto_engine *engine, void *areq)
u8 *pad_buf = rctx->padding;
int ret = 0;
+ if (dthe_hash_flag_is_op(rctx, DTHE_HASH_OP_KEY_PREPROCESS)) {
+ ret = dthe_hmac_run_key_preprocess(tfm, rctx);
+ goto hash_buf_all;
+ }
+
if (dthe_hash_flag_is_op(rctx, DTHE_HASH_OP_UPDATE)) {
len_to_buffer = tot_len % bs;
len_to_process = tot_len - len_to_buffer;
@@ -520,6 +592,8 @@ static int dthe_hash_export(struct ahash_request *req, void *out)
if (dthe_hash_has_flag(rctx, DTHE_HASH_FLAG_IDIGEST_AVAIL))
dthe_hash_state_to_sw(ctx, rctx->idigest, out);
+ else if (ctx->keylen > 0)
+ dthe_hash_state_to_sw(ctx, ctx->istate, out);
else
memcpy(out, dthe_hash_get_init_state(ctx), ctx->idigest_size);
@@ -538,6 +612,7 @@ static int dthe_hash_import(struct ahash_request *req, const void *in)
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
struct dthe_hash_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
unsigned int bs = crypto_ahash_blocksize(tfm);
+ unsigned int freshcnt = ctx->keylen > 0 ? bs : 0;
union {
const u8 *u8;
const u64 *u64;
@@ -552,14 +627,20 @@ static int dthe_hash_import(struct ahash_request *req, const void *in)
return -EOVERFLOW;
if (bytecnt > U32_MAX)
return -EOVERFLOW;
- if (bytecnt % bs)
+ if (bytecnt < freshcnt || bytecnt % bs)
return -EINVAL;
rctx->flags = 0;
rctx->digestcnt = bytecnt;
- if (bytecnt == 0)
+ if (bytecnt == freshcnt) {
+ if (ctx->keylen > 0) {
+ dthe_hash_state_from_sw(ctx, in, rctx->idigest);
+ if (memcmp(rctx->idigest, ctx->istate, ctx->idigest_size))
+ return -EINVAL;
+ }
return 0;
+ }
dthe_hash_state_from_sw(ctx, in, rctx->idigest);
dthe_hash_flag_set(rctx, DTHE_HASH_FLAG_IDIGEST_AVAIL);
@@ -569,10 +650,12 @@ static int dthe_hash_import(struct ahash_request *req, const void *in)
static int dthe_hash_init(struct ahash_request *req)
{
+ struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
struct dthe_hash_req_ctx *rctx = ahash_request_ctx_dma(req);
+ struct dthe_hash_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
rctx->flags = 0;
- rctx->digestcnt = 0;
+ rctx->digestcnt = (ctx->keylen > 0) ? crypto_ahash_blocksize(tfm) : 0;
return 0;
}
@@ -638,6 +721,123 @@ static int dthe_hash_digest(struct ahash_request *req)
return dthe_hash_finup(req);
}
+static int dthe_hmac_preparekey_hw(struct crypto_ahash *tfm)
+{
+ struct dthe_hash_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
+ struct dthe_hash_req_ctx *rctx;
+ struct ahash_request *req;
+ DECLARE_CRYPTO_WAIT(wait);
+ int ret;
+
+ might_sleep();
+
+ /* Send key preprocessing to hardware through a dummy request */
+ req = ahash_request_alloc(tfm, GFP_KERNEL);
+ if (!req)
+ return -ENOMEM;
+
+ ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
+ CRYPTO_TFM_REQ_MAY_SLEEP,
+ crypto_req_done, &wait);
+ ahash_request_set_crypt(req, NULL, NULL, 0);
+
+ rctx = ahash_request_ctx_dma(req);
+ rctx->flags = 0;
+ rctx->digestcnt = 0;
+ dthe_hash_flag_set_op(rctx, DTHE_HASH_OP_KEY_PREPROCESS);
+
+ ret = dthe_hash_transfer_to_engine(ctx, req);
+ ret = crypto_wait_req(ret, &wait);
+
+ ahash_request_free(req);
+ return ret;
+}
+
+static void dthe_hmac_preparekey_sw(struct dthe_hash_tfm_ctx *ctx)
+{
+ const u8 *key = (const u8 *)ctx->key;
+ union {
+ struct hmac_sha224_key sha224;
+ struct hmac_sha256_key sha256;
+ struct hmac_sha384_key sha384;
+ struct hmac_sha512_key sha512;
+ } k;
+ const void *istate = NULL;
+ const void *ostate = NULL;
+
+ switch (ctx->hash_mode) {
+ case DTHE_HASH_SHA224:
+ hmac_sha224_preparekey(&k.sha224, key, ctx->keylen);
+ istate = &k.sha224.key.istate;
+ ostate = &k.sha224.key.ostate;
+ break;
+ case DTHE_HASH_SHA256:
+ hmac_sha256_preparekey(&k.sha256, key, ctx->keylen);
+ istate = &k.sha256.key.istate;
+ ostate = &k.sha256.key.ostate;
+ break;
+ case DTHE_HASH_SHA384:
+ hmac_sha384_preparekey(&k.sha384, key, ctx->keylen);
+ istate = &k.sha384.key.istate;
+ ostate = &k.sha384.key.ostate;
+ break;
+ case DTHE_HASH_SHA512:
+ hmac_sha512_preparekey(&k.sha512, key, ctx->keylen);
+ istate = &k.sha512.key.istate;
+ ostate = &k.sha512.key.ostate;
+ break;
+ default:
+ break;
+ }
+
+ dthe_hash_state_from_sw(ctx, istate, ctx->istate);
+ dthe_hash_state_from_sw(ctx, ostate, ctx->ostate);
+
+ memzero_explicit(&k, sizeof(k));
+}
+
+static int dthe_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
+ unsigned int keylen)
+{
+ struct dthe_hash_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
+ u8 *k = (u8 *)ctx->key;
+ unsigned int max_keysize;
+
+ memzero_explicit(ctx->key, sizeof(ctx->key));
+ ctx->keylen = 0;
+
+ 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)
+ memcpy(k, key, keylen);
+ else if (ctx->hash_mode == DTHE_HASH_SHA512)
+ sha512(key, keylen, k);
+ else if (ctx->hash_mode == DTHE_HASH_SHA384)
+ sha384(key, keylen, k);
+ else if (ctx->hash_mode == DTHE_HASH_SHA256)
+ sha256(key, keylen, k);
+ else
+ sha224(key, keylen, k);
+
+ ctx->keylen = max_keysize;
+
+ if (dthe_hmac_preparekey_hw(tfm))
+ dthe_hmac_preparekey_sw(ctx);
+
+ return 0;
+}
+
static struct ahash_engine_alg hash_algs[] = {
{
.base.init_tfm = dthe_hash_init_tfm,
@@ -767,6 +967,138 @@ static struct ahash_engine_alg hash_algs[] = {
},
.op.do_one_request = dthe_hash_run,
},
+ {
+ .base.init_tfm = dthe_hash_init_tfm,
+ .base.exit_tfm = dthe_hash_exit_tfm,
+ .base.init = dthe_hash_init,
+ .base.update = dthe_hash_update,
+ .base.final = dthe_hash_final,
+ .base.finup = dthe_hash_finup,
+ .base.digest = dthe_hash_digest,
+ .base.export = dthe_hash_export,
+ .base.import = dthe_hash_import,
+ .base.setkey = dthe_hmac_setkey,
+ .base.halg = {
+ .digestsize = SHA512_DIGEST_SIZE,
+ .statesize = DTHE_SHA512_STATESIZE,
+ .base = {
+ .cra_name = "hmac(sha512)",
+ .cra_driver_name = "hmac-sha512-dthev2",
+ .cra_priority = 299,
+ .cra_flags = CRYPTO_ALG_TYPE_AHASH |
+ CRYPTO_ALG_ASYNC |
+ CRYPTO_ALG_NEED_FALLBACK |
+ CRYPTO_ALG_KERN_DRIVER_ONLY |
+ CRYPTO_ALG_ALLOCATES_MEMORY |
+ CRYPTO_AHASH_ALG_BLOCK_ONLY,
+ .cra_blocksize = SHA512_BLOCK_SIZE,
+ .cra_ctxsize = sizeof(struct dthe_hash_tfm_ctx),
+ .cra_reqsize = sizeof(struct dthe_hash_req_ctx) +
+ CRYPTO_DMA_PADDING,
+ .cra_module = THIS_MODULE,
+ }
+ },
+ .op.do_one_request = dthe_hash_run,
+ },
+ {
+ .base.init_tfm = dthe_hash_init_tfm,
+ .base.exit_tfm = dthe_hash_exit_tfm,
+ .base.init = dthe_hash_init,
+ .base.update = dthe_hash_update,
+ .base.final = dthe_hash_final,
+ .base.finup = dthe_hash_finup,
+ .base.digest = dthe_hash_digest,
+ .base.export = dthe_hash_export,
+ .base.import = dthe_hash_import,
+ .base.setkey = dthe_hmac_setkey,
+ .base.halg = {
+ .digestsize = SHA384_DIGEST_SIZE,
+ .statesize = DTHE_SHA512_STATESIZE,
+ .base = {
+ .cra_name = "hmac(sha384)",
+ .cra_driver_name = "hmac-sha384-dthev2",
+ .cra_priority = 299,
+ .cra_flags = CRYPTO_ALG_TYPE_AHASH |
+ CRYPTO_ALG_ASYNC |
+ CRYPTO_ALG_NEED_FALLBACK |
+ CRYPTO_ALG_KERN_DRIVER_ONLY |
+ CRYPTO_ALG_ALLOCATES_MEMORY |
+ CRYPTO_AHASH_ALG_BLOCK_ONLY,
+ .cra_blocksize = SHA384_BLOCK_SIZE,
+ .cra_ctxsize = sizeof(struct dthe_hash_tfm_ctx),
+ .cra_reqsize = sizeof(struct dthe_hash_req_ctx) +
+ CRYPTO_DMA_PADDING,
+ .cra_module = THIS_MODULE,
+ }
+ },
+ .op.do_one_request = dthe_hash_run,
+ },
+ {
+ .base.init_tfm = dthe_hash_init_tfm,
+ .base.exit_tfm = dthe_hash_exit_tfm,
+ .base.init = dthe_hash_init,
+ .base.update = dthe_hash_update,
+ .base.final = dthe_hash_final,
+ .base.finup = dthe_hash_finup,
+ .base.digest = dthe_hash_digest,
+ .base.export = dthe_hash_export,
+ .base.import = dthe_hash_import,
+ .base.setkey = dthe_hmac_setkey,
+ .base.halg = {
+ .digestsize = SHA256_DIGEST_SIZE,
+ .statesize = DTHE_SHA256_STATESIZE,
+ .base = {
+ .cra_name = "hmac(sha256)",
+ .cra_driver_name = "hmac-sha256-dthev2",
+ .cra_priority = 299,
+ .cra_flags = CRYPTO_ALG_TYPE_AHASH |
+ CRYPTO_ALG_ASYNC |
+ CRYPTO_ALG_NEED_FALLBACK |
+ CRYPTO_ALG_KERN_DRIVER_ONLY |
+ CRYPTO_ALG_ALLOCATES_MEMORY |
+ CRYPTO_AHASH_ALG_BLOCK_ONLY,
+ .cra_blocksize = SHA256_BLOCK_SIZE,
+ .cra_ctxsize = sizeof(struct dthe_hash_tfm_ctx),
+ .cra_reqsize = sizeof(struct dthe_hash_req_ctx) +
+ CRYPTO_DMA_PADDING,
+ .cra_module = THIS_MODULE,
+ }
+ },
+ .op.do_one_request = dthe_hash_run,
+ },
+ {
+ .base.init_tfm = dthe_hash_init_tfm,
+ .base.exit_tfm = dthe_hash_exit_tfm,
+ .base.init = dthe_hash_init,
+ .base.update = dthe_hash_update,
+ .base.final = dthe_hash_final,
+ .base.finup = dthe_hash_finup,
+ .base.digest = dthe_hash_digest,
+ .base.export = dthe_hash_export,
+ .base.import = dthe_hash_import,
+ .base.setkey = dthe_hmac_setkey,
+ .base.halg = {
+ .digestsize = SHA224_DIGEST_SIZE,
+ .statesize = DTHE_SHA256_STATESIZE,
+ .base = {
+ .cra_name = "hmac(sha224)",
+ .cra_driver_name = "hmac-sha224-dthev2",
+ .cra_priority = 299,
+ .cra_flags = CRYPTO_ALG_TYPE_AHASH |
+ CRYPTO_ALG_ASYNC |
+ CRYPTO_ALG_NEED_FALLBACK |
+ CRYPTO_ALG_KERN_DRIVER_ONLY |
+ CRYPTO_ALG_ALLOCATES_MEMORY |
+ CRYPTO_AHASH_ALG_BLOCK_ONLY,
+ .cra_blocksize = SHA224_BLOCK_SIZE,
+ .cra_ctxsize = sizeof(struct dthe_hash_tfm_ctx),
+ .cra_reqsize = sizeof(struct dthe_hash_req_ctx) +
+ CRYPTO_DMA_PADDING,
+ .cra_module = THIS_MODULE,
+ }
+ },
+ .op.do_one_request = dthe_hash_run,
+ },
};
int dthe_register_hash_algs(void)
--
2.34.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v11 1/2] crypto: ti - Add support for SHA224/256/384/512 in DTHEv2 driver
2026-12-04 16:42 ` [PATCH v11 1/2] crypto: ti - Add support for SHA224/256/384/512 in DTHEv2 driver T Pratham
@ 2026-12-04 17:21 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-04 17:21 UTC (permalink / raw)
To: T Pratham, Herbert Xu, David S. Miller
Cc: Sebin Francis, Manorit Chawdhry, Vishal Mahaveer,
Praneeth Bajjuri, linux-crypto
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] crypto: ti: re-registering hash_algs inflates statesize and
cra_reqsize
- [High] crypto: ti: unchecked 32-bit digestcnt overflow corrupts >= 4
GiB hashes
- [Medium] crypto: ti - high-order kzalloc_objs() and double-zeroing in
dthe_hash_run()
- [Medium] crypto: ti: 10 ms busy-poll with sleep_us=0 in SCHED_FIFO
engine kthreads
- [Low] crypto: ti: dthev2: spurious CRYPTO_ALG_OPTIONAL_KEY on unkeyed
SHA-2 algs
--
commit 114a10ac679742bd6b521e9f3aec91e9406695f5
Author: T Pratham <t-pratham@ti.com>
crypto: ti - Add support for SHA224/256/384/512 in DTHEv2 driver
This commit adds SHA224, SHA256, SHA384, and SHA512 hashing engine support
to the TI DTHEv2 crypto driver, splitting the crypto engine into separate
AES and hash engine instances.
> diff --git a/drivers/crypto/ti/dthev2-hash.c b/drivers/crypto/ti/dthev2-hash.c
> new file mode 100644
> index 0000000000000..bc5931de55378
> --- /dev/null
> +++ b/drivers/crypto/ti/dthev2-hash.c
> @@ -0,0 +1,780 @@
[ ... ]
> + /* 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);
[Severity: Medium]
Should this poll pass a non-zero sleep_us instead of busy-waiting for up to
10 ms inside a SCHED_FIFO kthread?
Both hash_engine and aes_engine are created with crypto_engine_alloc_init(dev,
1), so dthe_hash_run_dma() and dthe_aead_read_tag() run at SCHED_FIFO
priority. With DTHE_POLL_TIMEOUT_INTERVAL set to 10 * USEC_PER_MSEC and
sleep_us = 0, a slow or stalled operation (or an AEAD DMA timeout, where
dthe_aead_run() still calls dthe_aead_read_tag()) spins on the CPU for up to
10 ms, starving normal tasks, workqueues, and ksoftirqd.
Since readl_relaxed_poll_timeout() reads the register before sleeping, a
small non-zero sleep_us such as 10 us avoids extra latency when the status
bit is already set while preventing multi-millisecond real-time spins.
[ ... ]
> + rctx->digestcnt = readl_relaxed(sha_base_reg + DTHE_P_HASH512_DIGEST_COUNT);
> + dthe_hash_flag_set(rctx, DTHE_HASH_FLAG_IDIGEST_AVAIL);
[Severity: High]
Can streaming 4 GiB or more silently overflow rctx->digestcnt and return a
corrupted digest?
DTHE_P_HASH512_DIGEST_COUNT and rctx->digestcnt are 32-bit byte counters,
which dthe_hash_import() already enforces with a U32_MAX check. Neither
dthe_hash_run() nor dthe_hash_run_dma() checks whether
(u64)rctx->digestcnt + len_to_process exceeds U32_MAX, so a streamed message
of 4 GiB or larger wraps the hardware counter modulo 2^32 and finalizes with
the wrong length padding while returning 0.
If a state exported at an exact 4 GiB boundary wraps to 0, dthe_hash_import()
also takes the early return for bytecnt == 0 and silently resets the hash to
its initial state without restoring rctx->idigest.
[ ... ]
> + src = kzalloc_objs(*src, src_nents, GFP_KERNEL);
> + if (!src) {
> + ret = -ENOMEM;
> + goto hash_buf_all;
> + }
> +
> + sg_init_table(src, src_nents);
[Severity: Medium]
Can large multi-page scatterlists trigger costly high-order GFP_KERNEL
allocations here, and does sg_init_table() redundantly zero the array a
second time?
For non-contiguous requests larger than 4 MB (over 1024 4 KB pages),
kzalloc_objs() requests a high-order contiguous allocation inside the
SCHED_FIFO hash_engine kthread, which can stall all queued hash requests in
direct reclaim/compaction and fail with -ENOMEM under fragmentation since
dthe_hash_run() has no software fallback. Switching to sg_alloc_table() and
sg_free_table() avoids both the contiguous allocation and the double-zeroing,
as dthe_copy_sg() and k3-udma already walk chained scatterlists with
sg_next() and for_each_sg().
[ ... ]
> + .cra_flags = CRYPTO_ALG_TYPE_AHASH |
> + CRYPTO_ALG_ASYNC |
> + CRYPTO_ALG_OPTIONAL_KEY |
> + CRYPTO_ALG_KERN_DRIVER_ONLY |
> + CRYPTO_ALG_ALLOCATES_MEMORY |
> + CRYPTO_AHASH_ALG_BLOCK_ONLY,
[Severity: Low]
Why is CRYPTO_ALG_OPTIONAL_KEY set on the four unkeyed SHA-2 entries in
hash_algs[] when none of them define a .setkey callback?
CRYPTO_ALG_OPTIONAL_KEY is only for algorithms that provide a ->setkey()
method with a default key; setting it here exposes a bogus flag to userspace
via crypto_report_one() and would fail registration if ahash_prepare_alg()
adds a consistency check.
[ ... ]
> +int dthe_register_hash_algs(void)
> +{
> + return crypto_engine_register_ahashes(hash_algs, ARRAY_SIZE(hash_algs));
> +}
> +
> +void dthe_unregister_hash_algs(void)
> +{
> + crypto_engine_unregister_ahashes(hash_algs, ARRAY_SIZE(hash_algs));
> +}
[Severity: High]
Does re-registering the static hash_algs[] array inflate statesize and
cra_reqsize on every unbind/rebind?
Because hash_algs[] sets CRYPTO_AHASH_ALG_BLOCK_ONLY, ahash_prepare_alg()
adds cra_blocksize + 1 to both fields in place on every registration, and
crypto_unregister_ahash() does not undo it:
crypto/ahash.c:ahash_prepare_alg() {
...
if (base->cra_flags & CRYPTO_AHASH_ALG_BLOCK_ONLY) {
...
base->cra_reqsize += base->cra_blocksize + 1;
alg->halg.statesize += base->cra_blocksize + 1;
...
}
After a single sysfs unbind/rebind (or probe retry after
dthe_register_aes_algs() fails), sha512-dthev2 statesize grows from 209 to
338, so crypto_ahash_export() copies the partial block to offsets 209..337
while dthe_hash_export() only writes offsets 0..79. That leaves 129 bytes
of uninitialized stack memory in ahash_do_req_chain()'s state buffer to be
imported and hashed into the fallback digest, and a third rebind pushes
statesize past HASH_MAX_STATESIZE (345) and breaks ahash_do_req_chain() with
-ENOSYS.
Probing a second DTHEv2 instance while one is bound also calls
dthe_register_algs() unconditionally, which mutates the live hash_algs[] and
runs INIT_LIST_HEAD(&alg->cra_users) in __crypto_register_alg() on the
already-registered algorithm before returning -EEXIST.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204164236.489112-1-t-pratham@ti.com?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v11 2/2] crypto: ti - Add support for HMAC in DTHEv2 driver
2026-12-04 16:42 ` [PATCH v11 2/2] crypto: ti - Add support for HMAC " T Pratham
@ 2026-12-04 17:47 ` sashiko-bot
0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-04 17:47 UTC (permalink / raw)
To: T Pratham, Herbert Xu, David S. Miller
Cc: Sebin Francis, Manorit Chawdhry, Vishal Mahaveer,
Praneeth Bajjuri, linux-crypto
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] crypto: ti: HMAC key preprocess timeout clobbers aliased key
before fallback
- [Medium] crypto: ti: unnecessary synchronous engine round-trip in
dthe_hmac_setkey()
- [Medium] crypto: ti: unchecked 32-bit digestcnt wraparound on >= 4 GiB
hash/HMAC inputs
- [Medium] crypto: ti: missing SYSCONFIG setup in
dthe_hmac_run_key_preprocess()
--
commit 4ec372e68a21e24f2a8b0d6b5d5729e1692ea3c5
Author: T Pratham <t-pratham@ti.com>
crypto: ti - Add support for HMAC in DTHEv2 driver
This commit adds HMAC-SHA224/256/384/512 support to the TI DTHEv2 hashing
engine driver, including hardware and software key preprocessing, state
export/import handling, and algorithm registration.
> diff --git a/drivers/crypto/ti/dthev2-hash.c b/drivers/crypto/ti/dthev2-hash.c
> index bc5931de55378..12cfebf251628 100644
> --- a/drivers/crypto/ti/dthev2-hash.c
> +++ b/drivers/crypto/ti/dthev2-hash.c
[ ... ]
> @@ -349,6 +375,47 @@ static int dthe_hash_run_dma(struct ahash_request *req, struct scatterlist *src,
> return ret;
> }
>
> +static int dthe_hmac_run_key_preprocess(struct crypto_ahash *tfm, struct dthe_hash_req_ctx *rctx)
> +{
> + struct dthe_data *dev_data = rctx->dev_data;
> + struct dthe_hash_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
> + u32 hash_mode = ctx->hash_mode | DTHE_HASH_MODE_HMAC_KEY_PROCESSING;
> + void __iomem *sha_base_reg = dev_data->regs + DTHE_P_HASH_BASE;
> + u32 _status;
> + int ret;
> +
> + for (int i = 0; i < (ctx->keylen / 2) / sizeof(u32); ++i)
> + writel_relaxed(ctx->key[i], sha_base_reg +
> + DTHE_P_HASH512_ODIGEST_A +
> + (DTHE_REG_SIZE * i));
> + for (int i = 0; i < (ctx->keylen / 2) / sizeof(u32); ++i)
> + writel_relaxed(ctx->key[i + (ctx->keylen / 2) / sizeof(u32)],
> + sha_base_reg +
> + DTHE_P_HASH512_IDIGEST_A +
> + (DTHE_REG_SIZE * i));
> + writel_relaxed(hash_mode, sha_base_reg + DTHE_P_HASH512_MODE);
> + writel_relaxed(0, sha_base_reg + DTHE_P_HASH512_LENGTH);
> +
> + ret = readl_relaxed_poll_timeout(sha_base_reg + DTHE_P_HASH_IRQSTATUS, _status,
> + (_status & DTHE_HASH_IRQSTATUS_OP_READY),
> + 0, DTHE_POLL_TIMEOUT_INTERVAL);
[Severity: Medium]
Should dthe_hmac_run_key_preprocess() configure DTHE_P_HASH_SYSCONFIG and
DTHE_P_HASH_IRQENABLE before triggering key preprocessing and polling
DTHE_P_HASH_IRQSTATUS?
dthe_probe() never initializes those registers, so an HMAC setkey() before
any DMA hash leaves them at reset defaults. After a DMA hash,
dthe_hash_run_dma() leaves DTHE_HASH_SYSCONFIG_DMA_EN set during this
register-only operation. By contrast, the sibling EIP-57 driver in
drivers/crypto/omap-sham.c (omap_sham_write_ctrl_omap4()) explicitly
configures SHA_REG_IRQENA and clears DMA_EN for non-DMA operations.
> +
> + for (int i = 0; i < ctx->idigest_size / sizeof(u32); ++i)
> + ctx->istate[i] = readl_relaxed(sha_base_reg +
> + DTHE_P_HASH512_IDIGEST_A +
> + (DTHE_REG_SIZE * i));
> +
> + for (int i = 0; i < ctx->idigest_size / sizeof(u32); ++i)
> + ctx->ostate[i] = readl_relaxed(sha_base_reg +
> + DTHE_P_HASH512_ODIGEST_A +
> + (DTHE_REG_SIZE * i));
> +
> + /* Need to read this to trigger completion */
> + rctx->digestcnt = readl_relaxed(sha_base_reg + DTHE_P_HASH512_DIGEST_COUNT);
> +
> + return ret;
> +}
[Severity: High]
Does a timeout in readl_relaxed_poll_timeout() clobber ctx->key before the
software fallback runs?
In struct dthe_hash_tfm_ctx, ctx->istate and ctx->ostate share a union with
ctx->key. Because these loops write into ctx->istate and ctx->ostate even
when ret is -ETIMEDOUT, dthe_hmac_setkey() then calls
dthe_hmac_preparekey_sw(ctx) on corrupted key bytes and returns 0.
Subsequent hardware HMAC operations will silently compute invalid tags and
disagree with crypto_ahash_fb(tfm), which received the original key. On
error, the registers should be drained into a scratch variable instead of
overwriting ctx->istate and ctx->ostate (or key should not alias them).
[ ... ]
> @@ -552,14 +627,20 @@ static int dthe_hash_import(struct ahash_request *req, const void *in)
> return -EOVERFLOW;
> if (bytecnt > U32_MAX)
> return -EOVERFLOW;
> - if (bytecnt % bs)
> + if (bytecnt < freshcnt || bytecnt % bs)
> return -EINVAL;
>
> rctx->flags = 0;
> rctx->digestcnt = bytecnt;
>
> - if (bytecnt == 0)
> + if (bytecnt == freshcnt) {
> + if (ctx->keylen > 0) {
> + dthe_hash_state_from_sw(ctx, in, rctx->idigest);
> + if (memcmp(rctx->idigest, ctx->istate, ctx->idigest_size))
> + return -EINVAL;
> + }
> return 0;
> + }
>
> dthe_hash_state_from_sw(ctx, in, rctx->idigest);
> dthe_hash_flag_set(rctx, DTHE_HASH_FLAG_IDIGEST_AVAIL);
> @@ -569,10 +650,12 @@ static int dthe_hash_init(struct ahash_request *req)
> {
> + struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
> struct dthe_hash_req_ctx *rctx = ahash_request_ctx_dma(req);
> + struct dthe_hash_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
>
> rctx->flags = 0;
> - rctx->digestcnt = 0;
> + rctx->digestcnt = (ctx->keylen > 0) ? crypto_ahash_blocksize(tfm) : 0;
>
> return 0;
> }
[Severity: Medium]
Can streaming 4 GiB (or 4 GiB minus blocksize for HMAC) silently wrap the
32-bit rctx->digestcnt?
dthe_hash_import() rejects bytecnt > U32_MAX with -EOVERFLOW, but none of
dthe_hash_update(), dthe_hash_final(), dthe_hash_run(), or
dthe_hash_run_dma() check whether (u64)rctx->digestcnt + len_to_process
exceeds U32_MAX.
When DTHE_P_HASH512_DIGEST_COUNT wraps modulo 2^32, finalization computes
padding with the wrapped length and silently returns a wrong digest or
HMAC tag, and re-importing an HMAC state that wrapped to 0 fails in
dthe_hash_import() with -EINVAL.
[ ... ]
> +static int dthe_hmac_setkey(struct crypto_ahash *tfm, const u8 *key,
> + unsigned int keylen)
> +{
> + struct dthe_hash_tfm_ctx *ctx = crypto_ahash_ctx(tfm);
> + u8 *k = (u8 *)ctx->key;
> + unsigned int max_keysize;
> +
> + memzero_explicit(ctx->key, sizeof(ctx->key));
> + ctx->keylen = 0;
> +
> + 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)
> + memcpy(k, key, keylen);
> + else if (ctx->hash_mode == DTHE_HASH_SHA512)
> + sha512(key, keylen, k);
> + else if (ctx->hash_mode == DTHE_HASH_SHA384)
> + sha384(key, keylen, k);
> + else if (ctx->hash_mode == DTHE_HASH_SHA256)
> + sha256(key, keylen, k);
> + else
> + sha224(key, keylen, k);
> +
> + ctx->keylen = max_keysize;
> +
> + if (dthe_hmac_preparekey_hw(tfm))
> + dthe_hmac_preparekey_sw(ctx);
> +
> + return 0;
> +}
[Severity: Medium]
Is the synchronous hardware round-trip in dthe_hmac_preparekey_hw() needed
here when dthe_hmac_preparekey_sw() already exists?
Every HMAC setkey() allocates a dummy request, contends on
dthe_dev_list.lock and hash_engine->queue_lock, blocks behind queued bulk
DMA hash requests on the single hash_engine kthread, and busy-polls
DTHE_P_HASH_IRQSTATUS for up to 10 ms just to compute two SHA blocks.
Because the HMAC algorithms set CRYPTO_ALG_NEED_FALLBACK,
crypto_ahash_setkey() unconditionally prepares the key in software on
crypto_ahash_fb(tfm) right afterward anyway.
Calling dthe_hmac_preparekey_sw() directly with the caller's key and
keylen (since hmac_sha*_preparekey() already hashes oversized keys) would
avoid the contention and latency while removing dthe_hmac_preparekey_hw(),
dthe_hmac_run_key_preprocess(), and the ctx->key union.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204164236.489112-1-t-pratham@ti.com?part=2
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-04 17:47 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 16:42 [PATCH v11 0/2] Add support for hashing algorithms in TI DTHE V2 T Pratham
2026-12-04 16:42 ` [PATCH v11 1/2] crypto: ti - Add support for SHA224/256/384/512 in DTHEv2 driver T Pratham
2026-12-04 17:21 ` sashiko-bot
2026-12-04 16:42 ` [PATCH v11 2/2] crypto: ti - Add support for HMAC " T Pratham
2026-12-04 17:47 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox