Linux cryptographic layer development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ryan Prinz" <rc4d5a@protonmail.com>,
	"Zhiqi Song" <songzhiqi1@huawei.com>,
	"Longfang Liu" <liulongfang@huawei.com>
Cc: "Herbert Xu" <herbert@gondor.apana.org.au>,
	"David S . Miller" <davem@davemloft.net>,
	linux-crypto@vger.kernel.org
Subject: Re: [PATCH] crypto: hisilicon/hpre - fix ECDH compute_shared_secret dst_len requirement
Date: Tue, 24 Nov 2026 10:37:09 +0000	[thread overview]
Message-ID: <sashiko-outbox-230968@kernel.org> (raw)
In-Reply-To: <20261124101912.1718740-1-rc4d5a@protonmail.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] crypto: hpre: hpre_ecdh_hw_data_clr_all() corrupts dst buffer
  on error paths
- [Medium] crypto: hpre: hpre_ecdh_cb() reports wrong dst_len for P-192
  public key
- [Medium] crypto: hisilicon/hpre - hpre_ecdh_dst_data_init() rejects
  multi-entry sg
--

commit 7c1c914742d923b9ff38c5599d17f759c401716d
Author: Ryan Prinz <rc4d5a@protonmail.com>

crypto: hisilicon/hpre - fix ECDH compute_shared_secret dst_len requirement

This commit lowers the minimum dst_len required for ECDH
compute_shared_secret() to curve_sz by allocating a 2 * ctx->key_sz DMA
bounce buffer and copying out the X coordinate on completion.

> diff --git a/drivers/crypto/hisilicon/hpre/hpre_crypto.c b/drivers/crypto/hisilicon/hpre/hpre_crypto.c
> index 66a276786a7d0..a3fec7c7549dd 100644
> --- a/drivers/crypto/hisilicon/hpre/hpre_crypto.c
> +++ b/drivers/crypto/hisilicon/hpre/hpre_crypto.c
> @@ -1390,9 +1390,11 @@ static void hpre_ecdh_hw_data_clr_all(struct hpre_ctx *ctx,
>  				      struct scatterlist *dst,
>  				      struct scatterlist *src)
>  {
> +	unsigned int curve_sz = hpre_ecdh_get_curvesz(ctx->curve_id);
>  	struct device *dev = ctx->dev;
>  	struct hpre_sqe *sqe = &req->req;
>  	dma_addr_t dma;
> +	char *p;
>  
>  	dma = le64_to_cpu(sqe->in);
>  	if (unlikely(dma_mapping_error(dev, dma)))
> @@ -1405,8 +1407,24 @@ static void hpre_ecdh_hw_data_clr_all(struct hpre_ctx *ctx,
>  	if (unlikely(dma_mapping_error(dev, dma)))
>  		return;
>  
> -	if (dst)
> +	if (req->dst) {
> +		/*
> +		 * Bounce buffer for compute_shared_secret(): holds the
> +		 * hardware's full, hw-slot-padded X||Y output. Copy out
> +		 * just the tightly-packed X coordinate to the caller's
> +		 * own (possibly smaller) buffer.
> +		 */
> +		p = req->dst;
> +		if (dst)
> +			scatterwalk_map_and_copy(p + ctx->key_sz - curve_sz,
> +						 dst, 0, curve_sz, 1);
> +		dma_free_coherent(dev, ctx->key_sz << 1, req->dst, dma);
> +	} else if (dst) {
>  		dma_unmap_single(dev, dma, ctx->key_sz << 1, DMA_FROM_DEVICE);
> +		p = sg_virt(dst);
> +		memmove(p, p + ctx->key_sz - curve_sz, curve_sz);
> +		memmove(p + curve_sz, p + (ctx->key_sz << 1) - curve_sz, curve_sz);
> +	}
>  }

[Severity: Medium]
Does moving the destination buffer copy and memmove() compaction into
hpre_ecdh_hw_data_clr_all() corrupt the caller's req->dst buffer on error
paths?

When hpre_send() fails in hpre_ecdh_compute_value() after
hpre_ecdh_dst_data_init() succeeds, the clear_all label calls
hpre_ecdh_hw_data_clr_all(ctx, hpre_req, req->dst, req->src), which now
copies zeros from the unused bounce buffer into req->dst or shifts
sg_virt(req->dst) in place via memmove().  The same happens in
hpre_ecdh_cb() when hpre_alg_res_post_hf() reports a hardware error.
Also, because dma_unmap_single() is inside the else if (dst) branch,
callers cannot simply pass dst = NULL on error without leaking the DMA
mapping for generate_public_key().

>  
>  static void hpre_ecdh_cb(struct hpre_ctx *ctx, void *resp)
> @@ -1416,12 +1434,11 @@ static void hpre_ecdh_cb(struct hpre_ctx *ctx, void *resp)
>  	struct hpre_asym_request *req = NULL;
>  	struct kpp_request *areq;
>  	u64 overtime_thrhld;
> -	char *p;
>  	int ret;
>  
>  	ret = hpre_alg_res_post_hf(ctx, resp, (void **)&req);
>  	areq = req->areq.ecdh;
> -	areq->dst_len = ctx->key_sz << 1;
> +	areq->dst_len = req->dst ? curve_sz : ctx->key_sz << 1;

[Severity: Medium]
Should the false branch here be curve_sz << 1 instead of ctx->key_sz << 1?

For ECC_CURVE_NIST_P192, ctx->key_sz is 32 (HPRE_ECC_HW256_KSZ_B) while
curve_sz is 24.  On generate_public_key(), the two memmove() calls compact
X and Y into 2 * curve_sz = 48 bytes at the start of dst, so setting
areq->dst_len to ctx->key_sz << 1 reports 64 bytes including 16 trailing
uncompacted bytes.

[ ... ]

> @@ -1503,12 +1526,15 @@ static int hpre_ecdh_src_data_init(struct hpre_asym_request *hpre_req,
>  }
>  
>  static int hpre_ecdh_dst_data_init(struct hpre_asym_request *hpre_req,
> -				   struct scatterlist *data, unsigned int len)
> +				   struct scatterlist *data, unsigned int len,
> +				   bool is_shared_secret)
>  {
> +	gfp_t gfp = hpre_req->flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL : GFP_ATOMIC;
>  	struct hpre_sqe *msg = &hpre_req->req;
>  	struct hpre_ctx *ctx = hpre_req->ctx;
>  	struct device *dev = ctx->dev;
>  	dma_addr_t dma;
> +	void *ptr;
>  
>  	if (unlikely(!data || !sg_is_last(data))) {
>  		dev_err(dev, "data is illegal!\n");

[Severity: Medium]
Does checking !sg_is_last(data) here still cause TPM2 session salt
derivation in tpm_buf_append_salt() to fail with -EINVAL, as well as
rejecting multi-entry destination scatterlists on the bounce-buffered
compute_shared_secret() path?

Before calling crypto_kpp_compute_shared_secret(), tpm_buf_append_salt()
calls crypto_kpp_generate_public_key() with a 2-entry scatterlist for X
and Y:

drivers/char/tpm/tpm2-sessions.c:tpm_buf_append_salt() {
    ...
	sg_init_table(s, 2);
	sg_set_buf(&s[0], x, EC_PT_SZ);
	sg_set_buf(&s[1], y, EC_PT_SZ);
    ...
	kpp_request_set_input(req, NULL, 0);
	kpp_request_set_output(req, s, EC_PT_SZ*2);
	rc = crypto_wait_req(crypto_kpp_generate_public_key(req), &wait);
    ...
}

Because s has two entries, !sg_is_last(data) rejects the
generate_public_key() call before tpm_buf_append_salt() reaches
compute_shared_secret().  In addition, when is_shared_secret is true, the
output is copied via scatterwalk_map_and_copy() from a bounce buffer, so
requiring sg_is_last(data) rejects multi-segment destination scatterlists
there too (and leaves len unused).

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124101912.1718740-1-rc4d5a@protonmail.com?part=1

      reply	other threads:[~2026-11-24 10:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 10:19 [PATCH] crypto: hisilicon/hpre - fix ECDH compute_shared_secret dst_len requirement Ryan Prinz
2026-11-24 10:37 ` 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-230968@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=liulongfang@huawei.com \
    --cc=rc4d5a@protonmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=songzhiqi1@huawei.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