* [PATCH] crypto: hisilicon/hpre - fix ECDH compute_shared_secret dst_len requirement
@ 2026-11-24 10:19 Ryan Prinz
2026-11-24 10:37 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Ryan Prinz @ 2026-11-24 10:19 UTC (permalink / raw)
To: Zhiqi Song, Longfang Liu
Cc: Herbert Xu, David S . Miller, linux-crypto, linux-kernel,
Ryan Prinz
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
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [PATCH] crypto: hisilicon/hpre - fix ECDH compute_shared_secret dst_len requirement
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
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-24 10:37 UTC (permalink / raw)
To: Ryan Prinz, Zhiqi Song, Longfang Liu
Cc: Herbert Xu, David S . Miller, linux-crypto
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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-24 10:37 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox