All of lore.kernel.org
 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 v10 2/2] crypto: ti - Add support for HMAC in DTHEv2 driver
Date: Mon, 23 Nov 2026 14:50:14 +0000	[thread overview]
Message-ID: <sashiko-outbox-229443@kernel.org> (raw)
In-Reply-To: <20261123142918.3451969-3-t-pratham@ti.com>

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

commit 325575ece1b4fbdd9ca3a408597f30635e4f5362
Author: T Pratham <t-pratham@ti.com>

crypto: ti - Add support for HMAC in DTHEv2 driver

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

> diff --git a/drivers/crypto/ti/dthev2-hash.c b/drivers/crypto/ti/dthev2-hash.c
> index ac6119e4e4d68..02ff7a5e6812a 100644
> --- a/drivers/crypto/ti/dthev2-hash.c
> +++ b/drivers/crypto/ti/dthev2-hash.c

[ ... ]

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

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

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

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

[ ... ]

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

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

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

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123142918.3451969-1-t-pratham@ti.com?part=2

      reply	other threads:[~2026-11-23 14:50 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 14:29 [PATCH v10 0/2] Add support for hashing algorithms in TI DTHE V2 T Pratham
2026-11-23 14:29 ` [PATCH v10 1/2] crypto: ti - Add support for SHA224/256/384/512 in DTHEv2 driver T Pratham
2026-11-23 14:51   ` sashiko-bot
2026-11-23 14:29 ` [PATCH v10 2/2] crypto: ti - Add support for HMAC " T Pratham
2026-11-23 14:50   ` 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-229443@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.