Linux cryptographic layer development
 help / color / mirror / Atom feed
From: Ryan Prinz <rc4d5a@protonmail.com>
To: 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, linux-kernel@vger.kernel.org,
	Ryan Prinz <rc4d5a@protonmail.com>
Subject: [PATCH] crypto: hisilicon/hpre - fix ECDH compute_shared_secret dst_len requirement
Date: Tue, 24 Nov 2026 10:19:31 +0000	[thread overview]
Message-ID: <20261124101912.1718740-1-rc4d5a@protonmail.com> (raw)

hpre_ecdh_msg_request_set() requires req->dst_len to be at least
2 * ctx->key_sz, regardless of which operation is being performed.
The generic KPP convention only needs that much for
generate_public_key(); a shared secret is conventionally just the X
coordinate, ctx->key_sz bytes.

Both in-tree callers of the generic ecdh-nist-pNNN names provide
exactly that smaller buffer for compute_shared_secret():
net/bluetooth/ecdh_helper.c's compute_ecdh_secret() allocates
secret[32] for P-256, and drivers/char/tpm/tpm2-sessions.c's session
salt derivation does kpp_request_set_output(req, d, EC_PT_SZ). Both
calls fail with -EOVERFLOW on HPRE hardware before the peer's public
key is looked at.

Found by Sashiko's automated review of a related patch, as a
pre-existing issue that patch doesn't introduce.

Fixed by requiring only the curve's own coordinate size for
compute_shared_secret(), and using a bounce buffer for that case:
hpre_ecdh_dst_data_init() DMA-maps an internal 2 * key_sz buffer
instead of the caller's, and hpre_ecdh_hw_data_clr_all() copies the
X coordinate to the caller's destination. generate_public_key() is
unaffected; it still requires, and writes directly into, a
2 * key_sz-sized caller buffer. areq->dst_len is also now set to
whatever was written (curve_sz or 2 * key_sz) instead of reporting
2 * key_sz.

Fixes: 05e7b906aa7c ("crypto: hisilicon/hpre - add 'ECDH' algorithm")
Signed-off-by: Ryan Prinz <rc4d5a@protonmail.com>
---
No HiSilicon HPRE hardware was available to reproduce this. Verified
by building the modified driver against an unmodified kernel tree,
confirming the module compiles and links cleanly with no new
warnings. Verified the sizing and offset arithmetic in a standalone
test across all three supported curves (P-192, P-256,
P-384): the new compute_shared_secret() minimum (curve_sz) and
unchanged generate_public_key() minimum (2 * key_sz) come out
distinct and correct for each, and every read/write offset used by
both the bounce-buffer copy-out and the existing direct-buffer
compaction stays inside its respective buffer.

AI Disclosure: An AI assistant was used to assist with the
analysis and testing. I have verified the vulnerability, the fix, and
the test results myself and take responsibility for this contribution,
per Documentation/process/coding-assistants.rst.

 drivers/crypto/hisilicon/hpre/hpre_crypto.c | 70 +++++++++++++++++++++++------
 1 file changed, 56 insertions(+), 14 deletions(-)

diff --git a/drivers/crypto/hisilicon/hpre/hpre_crypto.c b/drivers/crypto/hisilicon/hpre/hpre_crypto.c
index 66a276786a7d..a3fec7c7549d 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);
+	}
 }

 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;

 	overtime_thrhld = atomic64_read(&dfx[HPRE_OVERTIME_THRHLD].value);
 	if (overtime_thrhld && hpre_is_bd_timeout(req, overtime_thrhld))
@@ -1430,10 +1447,6 @@ static void hpre_ecdh_cb(struct hpre_ctx *ctx, void *resp)
 	/* Do unmap before data processing */
 	hpre_ecdh_hw_data_clr_all(ctx, req, areq->dst, areq->src);

-	p = sg_virt(areq->dst);
-	memmove(p, p + ctx->key_sz - curve_sz, curve_sz);
-	memmove(p + curve_sz, p + areq->dst_len - curve_sz, curve_sz);
-
 	kpp_request_complete(areq, ret);

 	atomic64_inc(&dfx[HPRE_RECV_CNT].value);
@@ -1446,7 +1459,17 @@ static int hpre_ecdh_msg_request_set(struct hpre_ctx *ctx,
 	struct hpre_sqe *msg;
 	void *tmp;

-	if (req->dst_len < ctx->key_sz << 1) {
+	if (req->src) {
+		unsigned int curve_sz = hpre_ecdh_get_curvesz(ctx->curve_id);
+
+		if (!curve_sz)
+			return -EINVAL;
+
+		if (req->dst_len < curve_sz) {
+			req->dst_len = curve_sz;
+			return -EOVERFLOW;
+		}
+	} else if (req->dst_len < ctx->key_sz << 1) {
 		req->dst_len = ctx->key_sz << 1;
 		return -EOVERFLOW;
 	}
@@ -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");
@@ -1516,10 +1542,26 @@ static int hpre_ecdh_dst_data_init(struct hpre_asym_request *hpre_req,
 	}

 	hpre_req->dst = NULL;
-	dma = dma_map_single(dev, sg_virt(data), ctx->key_sz << 1, DMA_FROM_DEVICE);
-	if (unlikely(dma_mapping_error(dev, dma))) {
-		dev_err(dev, "dma map data err!\n");
-		return -ENOMEM;
+
+	/*
+	 * compute_shared_secret() may be given a buffer sized for just
+	 * the X coordinate, per the generic KPP convention, but the
+	 * hardware always produces both X and Y. Use a bounce buffer so
+	 * the output has somewhere safe to land; hpre_ecdh_hw_data_clr_all()
+	 * copies out just X afterward.
+	 */
+	if (is_shared_secret) {
+		ptr = dma_alloc_coherent(dev, ctx->key_sz << 1, &dma, gfp);
+		if (unlikely(!ptr))
+			return -ENOMEM;
+		hpre_req->dst = ptr;
+	} else {
+		dma = dma_map_single(dev, sg_virt(data), ctx->key_sz << 1,
+				     DMA_FROM_DEVICE);
+		if (unlikely(dma_mapping_error(dev, dma))) {
+			dev_err(dev, "dma map data err!\n");
+			return -ENOMEM;
+		}
 	}

 	msg->out = cpu_to_le64(dma);
@@ -1552,7 +1594,7 @@ static int hpre_ecdh_compute_value(struct kpp_request *req)
 		msg->in = cpu_to_le64(ctx->ecdh.dma_g);
 	}

-	ret = hpre_ecdh_dst_data_init(hpre_req, req->dst, req->dst_len);
+	ret = hpre_ecdh_dst_data_init(hpre_req, req->dst, req->dst_len, !!req->src);
 	if (unlikely(ret)) {
 		dev_err(dev, "failed to init dst data, ret = %d!\n", ret);
 		goto clear_all;
--
2.51.2


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

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 10:19 Ryan Prinz [this message]
2026-11-24 10:37 ` [PATCH] crypto: hisilicon/hpre - fix ECDH compute_shared_secret dst_len requirement sashiko-bot

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=20261124101912.1718740-1-rc4d5a@protonmail.com \
    --to=rc4d5a@protonmail.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liulongfang@huawei.com \
    --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