mirror of https://lore.kernel.org/lkml/
 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 missing DH/ECDH public key validation
Date: Mon, 23 Nov 2026 21:52:11 +0000	[thread overview]
Message-ID: <20261123215144.1677044-1-rc4d5a@protonmail.com> (raw)

hpre_dh_compute_value() and hpre_ecdh_compute_value() pass the peer's
DH/ECDH public value into the hardware queue without checking it
first. Neither hpre_hw_data_init() nor hpre_ecdh_src_data_init() do
any validation; they only format and DMA-map the buffer.

The generic software implementations this driver falls back to do
check it: crypto/dh.c's dh_is_pubkey_valid() rejects a DH public value
outside [2, p-2], and crypto/ecc.c's ecc_is_pubkey_valid_partial()
rejects an ECDH public point that is zero, out of range, or not on the
curve. HPRE's hardware-accelerated path is the default/primary path,
not the fallback, so the DH/ECDH value check is normally skipped
rather than normally run.

Both algorithms are registered under their generic crypto_kpp names
("dh", "ecdh-nist-p192/256/384"), so any caller requesting those names
gets this unvalidated path whenever the hardware is present.
Two in-tree callers are affected:

- drivers/nvme/common/auth.c requests "ffdheNNNN(dh)" for NVMe-oF's
  in-band DH-HMAC-CHAP authentication, used by both
  drivers/nvme/host/auth.c and drivers/nvme/target/auth.c. Forcing the
  derived session key Ks to a known value lets an attacker who holds
  a handshake transcript perform an offline brute-force attack against
  the pre-shared key, which is what the DH exchange is documented to
  protect against on top of plain CHAP. With bidirectional
  authentication enabled, an attacker can obtain a transcript by
  connecting to the target directly and requesting it without valid
  credentials of their own, exposing the target's own authentication
  key (dhchap_ctrl_secret). Cryptographic soundness then relies
  solely on the strength of the target's key.

- net/bluetooth/smp.c allocates "ecdh-nist-p256" for LE Secure
  Connections pairing and performs no validation of its own on the
  remote device's public key before calling compute_shared_secret();
  it relies on the crypto_kpp backend. A peer can send an invalid
  public key (for example, the point at infinity) and force the
  resulting DHKey to one of a small number of predictable values in a
  single pairing attempt.

Fixed by validating the peer-supplied value before it reaches the
hardware queue, in both compute_value() functions: for DH, rejecting
any value outside [2, p-2]; for ECDH, reusing ecc_is_pubkey_valid_partial()
after converting the raw big-endian coordinates HPRE already works
with into the digit form that function expects.

Fixes: c8b4b477079d ("crypto: hisilicon - add HiSilicon HPRE accelerator")
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
(along with crypto/dh.c, crypto/ecc.c, crypto/ecdh.c and crypto/kpp.c)
against an unmodified kernel tree, confirming the module compiles and
links cleanly with no new warnings; and separately verifying the new
DH range check's byte-buffer arithmetic in a standalone test exercising
the boundary values 0, 1, 2, p-2, p-1 and p, including across a
multi-byte borrow when computing p-1, which all produced the expected
accept/reject result. The ECDH fix reuses ecc_is_pubkey_valid_partial()
unmodified; a separate standalone test confirmed its zero-padding of a
shorter-than-full-length coordinate produces the same byte layout as
hpre_ecdh_src_data_init()'s existing shift-based padding, so this
patch does not narrow what was previously accepted.

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 | 127 ++++++++++++++++++++++++++++
 1 file changed, 127 insertions(+)

diff --git a/drivers/crypto/hisilicon/hpre/hpre_crypto.c b/drivers/crypto/hisilicon/hpre/hpre_crypto.c
index 66a276786a7d..a5702d2d73b3 100644
--- a/drivers/crypto/hisilicon/hpre/hpre_crypto.c
+++ b/drivers/crypto/hisilicon/hpre/hpre_crypto.c
@@ -4,6 +4,7 @@
 #include <crypto/dh.h>
 #include <crypto/ecc_curve.h>
 #include <crypto/ecdh.h>
+#include <crypto/internal/ecc.h>
 #include <crypto/rng.h>
 #include <crypto/internal/akcipher.h>
 #include <crypto/internal/kpp.h>
@@ -471,6 +472,69 @@ static int hpre_send(struct hpre_ctx *ctx, struct hpre_sqe *msg)
 	return ret;
 }
 
+/* Returns true iff 2 <= y <= p - 2, given big-endian buffers of key_sz bytes. */
+static bool hpre_dh_pubkey_in_range(const u8 *y, const u8 *p_minus_1,
+				    unsigned int key_sz)
+{
+	bool is_zero_or_one = true;
+	unsigned int i;
+
+	for (i = 0; i < key_sz - 1; i++) {
+		if (y[i]) {
+			is_zero_or_one = false;
+			break;
+		}
+	}
+
+	if (is_zero_or_one && y[key_sz - 1] <= 1)
+		return false;
+
+	return memcmp(y, p_minus_1, key_sz) < 0;
+}
+
+static int hpre_dh_peer_pubkey_valid(struct hpre_ctx *ctx,
+				     struct scatterlist *src,
+				     unsigned int src_len, gfp_t gfp)
+{
+	unsigned int key_sz = ctx->key_sz;
+	u8 *p_minus_1, *y;
+	size_t copied;
+	int i, ret = 0;
+
+	if (!src_len || src_len > key_sz)
+		return -EINVAL;
+
+	y = kzalloc(key_sz, gfp);
+	if (!y)
+		return -ENOMEM;
+
+	copied = sg_copy_to_buffer(src, sg_nents_for_len(src, src_len),
+				   y + (key_sz - src_len), src_len);
+	if (copied != src_len) {
+		ret = -EINVAL;
+		goto free_y;
+	}
+
+	p_minus_1 = kmemdup(ctx->dh.xa_p + key_sz, key_sz, gfp);
+	if (!p_minus_1) {
+		ret = -ENOMEM;
+		goto free_y;
+	}
+
+	for (i = key_sz - 1; i >= 0; i--) {
+		if (p_minus_1[i]-- != 0)
+			break;
+	}
+
+	if (!hpre_dh_pubkey_in_range(y, p_minus_1, key_sz))
+		ret = -EINVAL;
+
+	kfree(p_minus_1);
+free_y:
+	kfree(y);
+	return ret;
+}
+
 static int hpre_dh_compute_value(struct kpp_request *req)
 {
 	struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
@@ -485,6 +549,15 @@ static int hpre_dh_compute_value(struct kpp_request *req)
 		return ret;
 
 	if (req->src) {
+		gfp_t gfp = hpre_req->flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
+			    GFP_KERNEL : GFP_ATOMIC;
+
+		ret = hpre_dh_peer_pubkey_valid(ctx, req->src, req->src_len, gfp);
+		if (unlikely(ret)) {
+			dev_err(ctx->dev, "invalid peer public key, ret = %d!\n", ret);
+			return ret;
+		}
+
 		ret = hpre_hw_data_init(hpre_req, req->src, req->src_len, 1, 1);
 		if (unlikely(ret))
 			goto clear_all;
@@ -1526,6 +1599,54 @@ static int hpre_ecdh_dst_data_init(struct hpre_asym_request *hpre_req,
 	return 0;
 }
 
+static int hpre_ecdh_peer_pubkey_valid(struct hpre_ctx *ctx,
+				       struct scatterlist *src,
+				       unsigned int src_len)
+{
+	const struct ecc_curve *curve = ecc_get_curve(ctx->curve_id);
+	unsigned int nbytes = ctx->key_sz;
+	unsigned int half = src_len >> 1;
+	u8 coord_buf[2 * ECC_MAX_BYTES] = { };
+	struct ecc_point *pk;
+	size_t copied;
+	int ret;
+
+	if (!curve)
+		return -EINVAL;
+
+	/*
+	 * Src_data is gx and gy concatenated, each zero-padded up to
+	 * nbytes, matching the convention hpre_ecdh_src_data_init() uses
+	 * for the hardware submission path.
+	 */
+	if (!half || half > nbytes || 2 * nbytes > sizeof(coord_buf))
+		return -EINVAL;
+
+	copied = sg_pcopy_to_buffer(src, sg_nents_for_len(src, src_len),
+				    coord_buf + (nbytes - half), half, 0);
+	if (copied != half)
+		return -EINVAL;
+
+	copied = sg_pcopy_to_buffer(src, sg_nents_for_len(src, src_len),
+				    coord_buf + nbytes + (nbytes - half),
+				    half, half);
+	if (copied != half)
+		return -EINVAL;
+
+	pk = ecc_alloc_point(curve->g.ndigits);
+	if (!pk)
+		return -ENOMEM;
+
+	ecc_swap_digits(coord_buf, pk->x, curve->g.ndigits);
+	ecc_swap_digits(coord_buf + nbytes, pk->y, curve->g.ndigits);
+
+	ret = ecc_is_pubkey_valid_partial(curve, pk);
+
+	ecc_free_point(pk);
+
+	return ret;
+}
+
 static int hpre_ecdh_compute_value(struct kpp_request *req)
 {
 	struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
@@ -1543,6 +1664,12 @@ static int hpre_ecdh_compute_value(struct kpp_request *req)
 	}
 
 	if (req->src) {
+		ret = hpre_ecdh_peer_pubkey_valid(ctx, req->src, req->src_len);
+		if (unlikely(ret)) {
+			dev_err(dev, "invalid peer public key, ret = %d!\n", ret);
+			return ret;
+		}
+
 		ret = hpre_ecdh_src_data_init(hpre_req, req->src, req->src_len);
 		if (unlikely(ret)) {
 			dev_err(dev, "failed to init src data, ret = %d!\n", ret);
-- 
2.51.2


                 reply	other threads:[~2026-11-23 21:52 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261123215144.1677044-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

all inboxes | Powered by JetHome®