Linux cryptographic layer development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "T Pratham" <t-pratham@ti.com>,
	"Herbert Xu" <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>
Cc: "Sebin Francis" <sebin.francis@ti.com>,
	"Manorit Chawdhry" <m-chawdhry@ti.com>,
	"Vishal Mahaveer" <vishalm@ti.com>,
	"Praneeth Bajjuri" <praneeth@ti.com>,
	linux-crypto@vger.kernel.org
Subject: Re: [PATCH v11 2/2] crypto: ti - Add support for HMAC in DTHEv2 driver
Date: Fri, 04 Dec 2026 17:47:23 +0000	[thread overview]
Message-ID: <sashiko-outbox-250343@kernel.org> (raw)
In-Reply-To: <20261204164236.489112-3-t-pratham@ti.com>

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

      reply	other threads:[~2026-12-04 17:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-250343@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=m-chawdhry@ti.com \
    --cc=praneeth@ti.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sebin.francis@ti.com \
    --cc=t-pratham@ti.com \
    --cc=vishalm@ti.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox