From: Ryan Prinz <rc4d5a@protonmail.com>
To: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>,
"David S . Miller" <davem@davemloft.net>,
Zhiqi Song <songzhiqi1@huawei.com>,
Longfang Liu <liulongfang@huawei.com>,
linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org,
Ryan Prinz <rc4d5a@protonmail.com>
Subject: [PATCH] crypto: dh - add shared DH public key range check
Date: Wed, 25 Nov 2026 23:18:43 +0000 [thread overview]
Message-ID: <20261125231825.1857253-1-rc4d5a@protonmail.com> (raw)
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
reply other threads:[~2026-11-25 23:18 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=20261125231825.1857253-1-rc4d5a@protonmail.com \
--to=rc4d5a@protonmail.com \
--cc=davem@davemloft.net \
--cc=giovanni.cabiddu@intel.com \
--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®