* [PATCH 0/3] crypto: share and use the DH public key range check
@ 2026-12-04 21:25 Ryan Prinz
2026-12-04 21:26 ` [PATCH 1/3] crypto: dh - add shared " Ryan Prinz
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Ryan Prinz @ 2026-12-04 21:25 UTC (permalink / raw)
To: Herbert Xu
Cc: Giovanni Cabiddu, Zhiqi Song, Longfang Liu, David S . Miller,
qat-linux, linux-crypto, linux-kernel
This regroups three previously-independent patches into one series,
per Herbert's request on the QAT thread so Sashiko (and everyone
else) can review them together: patches 2 and 3 both depend on the
helper patch 1 adds.
1/3 adds a shared SP800-56A rev3 5.6.2.3.2 range check,
crypto_dh_pubkey_in_range(), in crypto/dh_helper.c, instead of
QAT and HPRE each carrying a byte-for-byte identical private
copy.
2/3 is the QAT missing-DH-validation fix (previously posted alone as
v1-v3), now calling the shared helper from 1/3 instead of its own
copy of the comparison.
3/3 is the HPRE missing-DH/ECDH-validation fix (previously posted
alone as v1-v4), with its DH half now calling the shared helper
from 1/3 the same way; the ECDH half (ecc_is_pubkey_valid_partial())
is unchanged.
No functional changes from the last individually-posted revision of
any of the three patches; this is just resending as a linked series.
Ryan Prinz (3):
crypto: dh - add shared DH public key range check
crypto: qat - fix missing DH public key validation
crypto: hisilicon/hpre - fix missing DH/ECDH public key validation
crypto/dh_helper.c | 32 ++++++++
drivers/crypto/hisilicon/hpre/hpre_crypto.c | 124 +++++++++++++++++++
drivers/crypto/intel/qat/qat_common/qat_asym_algs.c | 43 +++++++
include/crypto/dh.h | 22 ++++
4 files changed, 221 insertions(+)
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH 1/3] crypto: dh - add shared DH public key range check
2026-12-04 21:25 [PATCH 0/3] crypto: share and use the DH public key range check Ryan Prinz
@ 2026-12-04 21:26 ` Ryan Prinz
2026-12-04 21:26 ` [PATCH v3 2/3] crypto: qat - fix missing DH public key validation Ryan Prinz
2026-12-04 21:26 ` [PATCH v4 3/3] crypto: hisilicon/hpre - fix missing DH/ECDH " Ryan Prinz
2 siblings, 0 replies; 4+ messages in thread
From: Ryan Prinz @ 2026-12-04 21:26 UTC (permalink / raw)
To: Herbert Xu
Cc: Giovanni Cabiddu, Zhiqi Song, Longfang Liu, David S . Miller,
qat-linux, linux-crypto, linux-kernel, Ryan Prinz
Add a shared helper for the DH public key range check from SP800-56A
rev3 5.6.2.3.2 (2 <= y <= p - 2), operating on raw big-endian buffers.
Any "dh" backend can call this instead of carrying its own copy.
Zero-padding the peer's value and computing p - 1 stay the caller's
job, since that depends on how each backend already has the data;
only the comparison itself lives here.
Signed-off-by: Ryan Prinz <rc4d5a@protonmail.com>
---
Verified by building crypto/dh_helper.o directly, confirming it
compiles cleanly with no new warnings or errors. Also verified with a
standalone build of the function under AddressSanitizer/UBSan,
exercising y = 0, 1, 2, p-2, p-1, p, a value larger than p, and an
ordinary interior value, each at both a multi-byte and a single-byte
p_size, plus p_size = 0 itself. All cases produced the expected
accept/reject result with no sanitizer reports.
AI Disclosure: An AI assistant was used to assist with the
analysis and testing. I have verified the fix and the test results
myself and take responsibility for this contribution, per
Documentation/process/coding-assistants.rst.
crypto/dh_helper.c | 32 ++++++++++++++++++++++++++++++++
include/crypto/dh.h | 22 ++++++++++++++++++++++
2 files changed, 54 insertions(+)
diff --git a/crypto/dh_helper.c b/crypto/dh_helper.c
--- a/crypto/dh_helper.c
+++ b/crypto/dh_helper.c
@@ -91,6 +91,38 @@ int crypto_dh_encode_key(char *buf, unsigned int len, const struct dh *params)
return 0;
}
+/*
+ * Partial public key validation check from SP800-56A rev3 section
+ * 5.6.2.3.2: a DH public value is invalid unless 2 <= y <= p - 2.
+ * Does not depend on FIPS mode.
+ *
+ * @y and @p_minus_1 must both already be exactly @p_size bytes,
+ * big-endian; callers are responsible for zero-extending a shorter
+ * public value before calling this function.
+ */
+bool crypto_dh_pubkey_in_range(const u8 *y, const u8 *p_minus_1,
+ unsigned int p_size)
+{
+ bool is_zero_or_one = true;
+ unsigned int i;
+
+ if (!p_size)
+ return false;
+
+ for (i = 0; i < p_size - 1; i++) {
+ if (y[i]) {
+ is_zero_or_one = false;
+ break;
+ }
+ }
+
+ if (is_zero_or_one && y[p_size - 1] <= 1)
+ return false;
+
+ return memcmp(y, p_minus_1, p_size) < 0;
+}
+EXPORT_SYMBOL_GPL(crypto_dh_pubkey_in_range);
+
int crypto_dh_decode_key(const char *buf, unsigned int len, struct dh *params)
{
int err;
diff --git a/include/crypto/dh.h b/include/crypto/dh.h
--- a/include/crypto/dh.h
+++ b/include/crypto/dh.h
@@ -95,4 +95,26 @@ int crypto_dh_decode_key(const char *buf, unsigned int len, struct dh *params)
int __crypto_dh_decode_key(const char *buf, unsigned int len,
struct dh *params);
+/**
+ * crypto_dh_pubkey_in_range() - range-check a DH public value
+ * @y: Big-endian public value to check, zero-padded to @p_size
+ * bytes by the caller
+ * @p_minus_1: Big-endian (p - 1) for the same group as @y, @p_size bytes
+ * @p_size: Size of both @y and @p_minus_1 in bytes
+ *
+ * This is the partial public key validation check from SP800-56A rev3
+ * section 5.6.2.3.2: a DH public value is invalid unless 2 <= y <= p - 2.
+ * It does not depend on FIPS mode and is meant to be used by any backend
+ * that implements the "dh" kpp algorithm, hardware or software, so the
+ * check does not have to be reimplemented per backend.
+ *
+ * Both buffers must already be exactly @p_size bytes; callers are
+ * responsible for zero-extending a shorter public value before calling
+ * this function.
+ *
+ * Return: true if 2 <= y <= p - 2, false otherwise.
+ */
+bool crypto_dh_pubkey_in_range(const u8 *y, const u8 *p_minus_1,
+ unsigned int p_size);
+
#endif
--
2.51.2
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v3 2/3] crypto: qat - fix missing DH public key validation
2026-12-04 21:25 [PATCH 0/3] crypto: share and use the DH public key range check Ryan Prinz
2026-12-04 21:26 ` [PATCH 1/3] crypto: dh - add shared " Ryan Prinz
@ 2026-12-04 21:26 ` Ryan Prinz
2026-12-04 21:26 ` [PATCH v4 3/3] crypto: hisilicon/hpre - fix missing DH/ECDH " Ryan Prinz
2 siblings, 0 replies; 4+ messages in thread
From: Ryan Prinz @ 2026-12-04 21:26 UTC (permalink / raw)
To: Herbert Xu
Cc: Giovanni Cabiddu, David S . Miller, qat-linux, linux-crypto,
linux-kernel, Ryan Prinz
qat_dh_compute_value() passes the peer's DH public value into the
hardware queue without checking it. The only checks on req->src are
on length (req->src_len > ctx->p_size); never on the value itself.
crypto/dh.c's dh_is_pubkey_valid() is the generic software check for
this: it rejects a value outside [2, p-2], but only when FIPS mode
is enabled, and only for callers using the software implementation.
QAT's hardware path has no equivalent check in either mode.
QAT registers "dh" at cra_priority 1000, versus the generic software
implementation's 100, so any caller requesting "dh" gets this
unvalidated path whenever a QAT device is present and the requested
modulus size (1536/2048/3072/4096 bits) is one QAT supports;
qat_dh_set_secret() only falls back to software for sizes outside
that set.
drivers/nvme/common/auth.c requests "ffdheNNNN(dh)" for NVMe-oF's
in-band DH-HMAC-CHAP authentication. Three of its five supported
group sizes, 2048, 3072 and 4096 bits, match QAT's hardware sizes.
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.
Fixed by validating the peer-supplied value before it reaches the
hardware queue, rejecting any value outside [2, p-2].
Fixes: c9839143ebbf ("crypto: qat - Add DH support")
Signed-off-by: Ryan Prinz <rc4d5a@protonmail.com>
---
v3:
- Giovanni pointed out that this range check was about to exist
twice, byte-for-byte identical, once here and once in the
HiSilicon HPRE fix for the same bug. Moved the comparison itself
into crypto_dh_pubkey_in_range() in crypto/dh_helper.c, exported
via dh.h, so this and the HPRE fix (and the generic software path,
if that's wired up too) share one implementation instead of each
carrying a private copy. This driver's own zero-padding and (p - 1)
computation are unchanged; only the final comparison call moved.
Depends on patch 1/3 of this series ("crypto: dh - add shared DH
public key range check").
v2:
- Corrected the Fixes: tag, found by Sashiko's automated review:
2acbb8771f6a only added the req->src_len > ctx->p_size length
check, to fix an unrelated integer underflow; it's unrelated to
the missing value check this patch adds. That's been missing
since DH support was first added, in c9839143ebbf, which is now
the correct tag.
No Intel QuickAssist hardware was available to reproduce this.
Verified by building the modified driver (along with crypto/dh.c,
crypto/dh_helper.c and crypto/kpp.c) against an unmodified kernel
tree on x86_64, confirming the module compiles and links cleanly with
no new warnings; and verifying the range-check arithmetic in a
standalone test exercising the boundary values 0, 1, 2, p-2, p-1, p
and a value larger than p, which all produced the expected
accept/reject result. The comparison is now crypto_dh_pubkey_in_range()
unmodified rather than a private copy; this patch's own buffer
handling (zero-padding, p - 1 computation) is unchanged from v2.
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/intel/qat/qat_common/qat_asym_algs.c | 43 ++++++++++++++++++
1 file changed, 43 insertions(+)
diff --git a/drivers/crypto/intel/qat/qat_common/qat_asym_algs.c b/drivers/crypto/intel/qat/qat_common/qat_asym_algs.c
--- a/drivers/crypto/intel/qat/qat_common/qat_asym_algs.c
+++ b/drivers/crypto/intel/qat/qat_common/qat_asym_algs.c
@@ -216,6 +216,43 @@ static unsigned long qat_dh_fn_id(unsigned int len, bool g2)
}
}
+static int qat_dh_peer_pubkey_valid(struct qat_dh_ctx *ctx,
+ struct scatterlist *src,
+ unsigned int src_len, gfp_t flags)
+{
+ unsigned int p_size = ctx->p_size;
+ u8 *p_minus_1, *y;
+ int i, ret = 0;
+
+ if (!src_len || src_len > p_size)
+ return -EINVAL;
+
+ y = kzalloc(p_size, flags);
+ if (!y)
+ return -ENOMEM;
+
+ scatterwalk_map_and_copy(y + (p_size - src_len), src, 0, src_len, 0);
+
+ p_minus_1 = kmemdup(ctx->p, p_size, flags);
+ if (!p_minus_1) {
+ ret = -ENOMEM;
+ goto free_y;
+ }
+
+ for (i = p_size - 1; i >= 0; i--) {
+ if (p_minus_1[i]-- != 0)
+ break;
+ }
+
+ if (!crypto_dh_pubkey_in_range(y, p_minus_1, p_size))
+ ret = -EINVAL;
+
+ kfree_sensitive(p_minus_1);
+free_y:
+ kfree_sensitive(y);
+ return ret;
+}
+
static int qat_dh_compute_value(struct kpp_request *req)
{
struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
@@ -241,6 +278,12 @@ static int qat_dh_compute_value(struct kpp_request *req)
if (req->src_len > ctx->p_size)
return -EINVAL;
+ if (req->src) {
+ ret = qat_dh_peer_pubkey_valid(ctx, req->src, req->src_len, flags);
+ if (unlikely(ret))
+ return ret;
+ }
+
memset(msg, '\0', sizeof(*msg));
ICP_QAT_FW_PKE_HDR_VALID_FLAG_SET(msg->pke_hdr,
ICP_QAT_FW_COMN_REQ_FLAG_SET);
--
2.51.2
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v4 3/3] crypto: hisilicon/hpre - fix missing DH/ECDH public key validation
2026-12-04 21:25 [PATCH 0/3] crypto: share and use the DH public key range check Ryan Prinz
2026-12-04 21:26 ` [PATCH 1/3] crypto: dh - add shared " Ryan Prinz
2026-12-04 21:26 ` [PATCH v3 2/3] crypto: qat - fix missing DH public key validation Ryan Prinz
@ 2026-12-04 21:26 ` Ryan Prinz
2 siblings, 0 replies; 4+ messages in thread
From: Ryan Prinz @ 2026-12-04 21:26 UTC (permalink / raw)
To: Herbert Xu
Cc: Zhiqi Song, Longfang Liu, David S . Miller, linux-crypto,
linux-kernel, Ryan Prinz
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, exposing the target's 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 the remote
device's public key before calling compute_shared_secret(); it
relies on the crypto_kpp backend. On real HPRE hardware this call
fails before reaching that point today, for an unrelated reason:
hpre_ecdh_msg_request_set() requires dst_len of 2 * key_sz for
compute_shared_secret() as well as generate_public_key(), while
compute_ecdh_secret() only ever provides key_sz. That's a
pre-existing bug, not something this patch depends on or fixes.
Once it's corrected, or for any caller unaffected by it, a peer
can send an invalid public key 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>
---
v4:
- Giovanni pointed out (reviewing the separate QAT fix for the same
bug) that the [2, p-2] range check was about to exist twice,
byte-for-byte identical, in two different drivers. Moved it into
crypto_dh_pubkey_in_range() in crypto/dh_helper.c, exported via
dh.h, so DH backends share one implementation instead of each
reimplementing it. This patch now calls that shared function
instead of carrying its own private copy; the QAT fix has been
updated the same way. The ECDH check already reused a shared
function (ecc_is_pubkey_valid_partial()) as of v1, so that part is
unchanged. Depends on patch 1/3 of this series ("crypto: dh - add
shared DH public key range check").
v3:
- Corrected the net/bluetooth/smp.c paragraph above: on real HPRE
hardware, that call fails before reaching this patch's code, for
an unrelated, pre-existing reason (a dst_len mismatch in
hpre_ecdh_msg_request_set(), not something this patch depends on
or fixes). No code change.
v2:
- Fixed a bug Sashiko's automated review found: ctx->key_sz is the
hardware slot width, not the curve's own coordinate size (HPRE
pads P-192's 24-byte coordinates into the 32-byte P-256 slot), so
reading ecc_swap_digits() from the start of that padded region
shifted every P-192 coordinate and rejected every valid P-192
public key, including testmgr's self-test.
- Fixed a second bug Sashiko's review found: ecc_alloc_point()
allocates with GFP_KERNEL unconditionally, which could sleep in a
context where CRYPTO_TFM_REQ_MAY_SLEEP is clear. Switched to a
stack-allocated ecc_point via ECC_POINT_INIT(), which removes the
allocation (and the sleep question) entirely.
- Fixed a third bug found during further review of the above fix:
for P-192, the extra bytes between the caller's half-width and the
curve's own 24-byte coordinate size were accepted without checking
they were zero. A value with non-zero content there is wider than
P-192's prime once it reaches the hardware at its full submitted
width, which is the out-of-range case this patch fixes. Now
rejected unless those extra bytes are zero.
- Resent as a new thread rather than a reply to the v1 thread.
No HiSilicon HPRE hardware was available to reproduce this. Verified
by applying this patch to an unmodified kernel tree and building
drivers/crypto/hisilicon/hpre/hpre_crypto.o directly (along with the
crypto_dh_pubkey_in_range() change in crypto/dh_helper.c), confirming
both compile and link cleanly with no new warnings or errors. The DH
fix now reuses crypto_dh_pubkey_in_range() unmodified (same range-check
logic as before, just no longer a private copy); a standalone test of
that function exercising the boundary values 0, 1, 2, p-2, p-1 and p
produced the expected accept/reject result, same as prior to this
move. 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. A third
standalone test confirmed the coordinate-extraction offset correctly
accounts for curves whose coordinate size differs from the hardware
slot width (P-192's 24-byte coordinates inside the 32-byte P-256
slot), while P-256 and P-384 (where the two sizes already match) are
unaffected. A fourth standalone test confirmed the gap-byte check
rejects a crafted P-192 value with a non-zero byte outside its real
24-byte coordinate while still accepting both a plain 24-byte encoding
and a caller zero-padded out to the full 32-byte slot.
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 | 124 +++++++++++++++++++++++++++
1 file changed, 124 insertions(+)
diff --git a/drivers/crypto/hisilicon/hpre/hpre_crypto.c b/drivers/crypto/hisilicon/hpre/hpre_crypto.c
--- 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,49 @@ static int hpre_send(struct hpre_ctx *ctx, struct hpre_sqe *msg)
return ret;
}
+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 (!crypto_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 +529,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;
@@ -1574,6 +1627,71 @@ 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] = { };
+ u64 x[ECC_MAX_DIGITS], y[ECC_MAX_DIGITS];
+ struct ecc_point pk;
+ unsigned int cbytes;
+ size_t copied;
+
+ if (!curve)
+ return -EINVAL;
+
+ /* ctx->key_sz is the hardware slot width, not the curve's own
+ * coordinate size: HPRE pads P-192 (24-byte coordinates) out to
+ * the wider 32-byte P-256 slot.
+ */
+ cbytes = curve->g.ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
+ if (cbytes > nbytes)
+ 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;
+
+ /*
+ * If half is wider than the curve's own coordinate size (only
+ * possible for P-192 inside the wider P-256 slot), the extra
+ * leading bytes of each half must be zero, or this isn't a
+ * value this curve can represent.
+ */
+ if (half > cbytes &&
+ (memchr_inv(coord_buf + (nbytes - half), 0, half - cbytes) ||
+ memchr_inv(coord_buf + nbytes + (nbytes - half), 0, half - cbytes)))
+ return -EINVAL;
+
+ /*
+ * Extract each coordinate's cbytes-wide value from the end of
+ * its nbytes-wide zero-padded region.
+ */
+ ecc_swap_digits(coord_buf + (nbytes - cbytes), x, curve->g.ndigits);
+ ecc_swap_digits(coord_buf + nbytes + (nbytes - cbytes), y, curve->g.ndigits);
+ pk = ECC_POINT_INIT(x, y, curve->g.ndigits);
+
+ return ecc_is_pubkey_valid_partial(curve, &pk);
+}
+
static int hpre_ecdh_compute_value(struct kpp_request *req)
{
struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
@@ -1591,6 +1709,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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-04 21:27 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-04 21:25 [PATCH 0/3] crypto: share and use the DH public key range check Ryan Prinz
2026-12-04 21:26 ` [PATCH 1/3] crypto: dh - add shared " Ryan Prinz
2026-12-04 21:26 ` [PATCH v3 2/3] crypto: qat - fix missing DH public key validation Ryan Prinz
2026-12-04 21:26 ` [PATCH v4 3/3] crypto: hisilicon/hpre - fix missing DH/ECDH " Ryan Prinz
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®