From: Ryan Prinz <rc4d5a@protonmail.com>
To: Herbert Xu <herbert@gondor.apana.org.au>
Cc: "David S . Miller" <davem@davemloft.net>,
Giovanni Cabiddu <giovanni.cabiddu@intel.com>,
linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org,
Ryan Prinz <rc4d5a@protonmail.com>
Subject: [PATCH] crypto: dh - fix missing DH public key validation on non-FIPS kernels
Date: Wed, 25 Nov 2026 23:20:13 +0000 [thread overview]
Message-ID: <20261125231954.1857828-1-rc4d5a@protonmail.com> (raw)
dh_is_pubkey_valid() returns 0 immediately when fips_enabled is
false, so on a non-FIPS kernel dh_compute_value() never checks the
peer's public value at all before using it to derive the shared
secret.
That gating was added in the Fixes: commit because the function's
own range check (2 <= y <= p - 2) used to take a shortcut: it tested
y < p instead of y < p - 1, relying on the y^q mod p test further
down to reject y == p - 1. That test only runs in FIPS mode, so the
range check alone wasn't safe to run unconditionally, and gating the
whole function was the simplest way to keep the two consistent.
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).
Fixed by computing p - 2 directly and checking against that instead
of the y < p shortcut, so the range check is a complete partial
validation (SP800-56A section 5.6.2.3.2) on its own and can run
before the FIPS gate rather than behind it. The y^q mod p check
(SP800-56A section 5.6.2.3.1's additional step for safe-prime groups)
is unchanged and stays FIPS-only.
Fixes: f66a211e8c5d ("crypto: dh - Make public key test FIPS-only")
Signed-off-by: Ryan Prinz <rc4d5a@protonmail.com>
---
Verified by building crypto/dh.o directly, confirming it compiles
cleanly with no new warnings or errors.
Also verified step 1 itself under AddressSanitizer/UBSan outside the
kernel, by building lib/crypto/mpi/'s arithmetic files standalone and
running the new code against mpi_sub_ui()/mpi_cmp_ui()/mpi_cmp().
Using p and b_public from crypto/testmgr.h's dh_tv_template[], y = 0,
1, 2, p-2, p-1, p, and p+1 all produced the expected accept/reject
result.
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.
crypto/dh.c | 40 +++++++++++++++++++++++++---------------
1 file changed, 25 insertions(+), 15 deletions(-)
diff --git a/crypto/dh.c b/crypto/dh.c
--- a/crypto/dh.c
+++ b/crypto/dh.c
@@ -97,34 +97,44 @@
/*
* SP800-56A public key verification:
*
- * * For the safe-prime groups in FIPS mode, Q can be computed
- * trivially from P and a full validation according to SP800-56A
- * section 5.6.2.3.1 is performed.
+ * * Step 1, the partial validation from SP800-56A section 5.6.2.3.2
+ * (2 <= y <= p - 2), is performed unconditionally, regardless of
+ * FIPS mode.
*
- * * For all other sets of group parameters, only a partial validation
- * according to SP800-56A section 5.6.2.3.2 is performed.
+ * * For the safe-prime groups in FIPS mode, Q can be computed
+ * trivially from P, so step 2, completing the full validation
+ * according to SP800-56A section 5.6.2.3.1, is also performed.
*/
static int dh_is_pubkey_valid(struct dh_ctx *ctx, MPI y)
{
- MPI val, q;
+ MPI val, q, p_minus_2;
int ret;
- if (!fips_enabled)
- return 0;
-
if (unlikely(!ctx->p))
return -EINVAL;
/*
* Step 1: Verify that 2 <= y <= p - 2.
*
- * The upper limit check is actually y < p instead of y < p - 1
- * in order to save one mpi_sub_ui() invocation here. Note that
- * p - 1 is the non-trivial element of the subgroup of order 2 and
- * thus, the check on y^q below would fail if y == p - 1.
+ * Unlike step 2 below, this has to hold regardless of FIPS mode, so
+ * the upper limit is p - 2 itself rather than the old y < p
+ * shortcut, which relied on step 2 to reject p - 1 and only runs
+ * in FIPS mode.
*/
- if (mpi_cmp_ui(y, 1) < 1 || mpi_cmp(y, ctx->p) >= 0)
- return -EINVAL;
+ p_minus_2 = mpi_alloc(0);
+ if (!p_minus_2)
+ return -ENOMEM;
+
+ ret = mpi_sub_ui(p_minus_2, ctx->p, 2);
+ if (!ret && (mpi_cmp_ui(y, 2) < 0 || mpi_cmp(y, p_minus_2) > 0))
+ ret = -EINVAL;
+
+ mpi_free(p_minus_2);
+ if (ret)
+ return ret;
+
+ if (!fips_enabled)
+ return 0;
/*
* Step 2: Verify that 1 = y^q mod p
--
2.51.2
reply other threads:[~2026-11-25 23:20 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=20261125231954.1857828-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 \
/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®