* [PATCH] crypto: dh - add shared DH public key range check
@ 2026-11-25 23:18 Ryan Prinz
0 siblings, 0 replies; only message in thread
From: Ryan Prinz @ 2026-11-25 23:18 UTC (permalink / raw)
To: Giovanni Cabiddu
Cc: Herbert Xu, David S . Miller, Zhiqi Song, Longfang Liu,
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] only message in thread
only message in thread, other threads:[~2026-11-25 23:18 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 23:18 [PATCH] crypto: dh - add shared DH public key range check 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®