From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=79.135.106.28 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796419604; b=U3mSMCJrH4GaIuBL3h5qhVnJ664T8uq6WrE7CJzosnVXvyUctD6cWAF8o+HtF7HszCM+ bc6UDBOows2KJnAiwapIjXKh1LRQWwv0jmH0kbv6pNF+X2A9ZBijSqI3uzHr+i/jmeEoV RQHFGmGjD9eUyew0fTJhXWve7fwp1Zyw4iFzwqIJrrK+YnLOlFsRIAL/NAiVrylCA5J8y /y1nV8y+ue0SkANcqzE4omN7ee+aVDCHm1/MG+VPBDh42QoqrrtoalhWJDJIncz7IaBgT OCDTUmi6AuDfF04WuAmbstKBChZc0/OrI82xa2se9tkFnmsCDX+J/Yd3pNKK0S/EJGQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796419604; h=DMARC-Filter:DKIM-Signature:Date:To:From:Cc:Subject:Message-ID: In-Reply-To:References:Feedback-ID:X-Pm-Message-ID:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=YWWKvDKRDn1hHUowo2at1VmbK3immV9LxoHiyGmdb+c=; b=BIt26LG7abgclsuocFyGFdCtBj8HcQDZy1UT8A32yi3T+cifY9CsyPtbP+2aKNwrAsz0 ZDNRRtBywYSZLz57o5T7eXNdtT5vcLNgSMgH9RhoBPaMFkvwFPcU8MhKsOVx2f5Ye0i4Z pH2aKllZSV6tWtUo6p0pt/5rsV/JnEcikP/nBKB4KIQSkdijH7OFcwqCOXsJaAzJqs3sK 4NxQzuUFwdzDOzzjB0ue3qdKaQo5Yf1LQdVpgMBdR6CPPAD6Oulc8eAko3NDztVdO+U5b V1QSSZObE85YmIRE7YVZdTtBQQLS/O2thFe4va9qKxL7sKMvoi7U9q4wMvMfyC98OfQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=protonmail.com; spf=pass smtp.mailfrom=protonmail.com; dkim=pass header.d=protonmail.com header.i=@protonmail.com header.a=rsa-sha256 header.s=protonmail3 header.b=NCd+DktQ; arc=none smtp.remote-ip=79.135.106.28 Received: from mail-10628.protonmail.ch (mail-10628.protonmail.ch [79.135.106.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 73944606A322 for ; Fri, 4 Dec 2026 21:26:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 73944606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=protonmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=protonmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=protonmail.com header.i=@protonmail.com header.a=rsa-sha256 header.s=protonmail3 header.b=NCd+DktQ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1796419601; x=1796678801; bh=YWWKvDKRDn1hHUowo2at1VmbK3immV9LxoHiyGmdb+c=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=NCd+DktQYDZnO9lcTgPMtnTMcciEkrER7Zm5gfhdUJpkIVDB4aGx2NYcxTg553RtK t0fZ0mjIuCF92Klsa5+6vUfWrGbk5zFmQq0brhmIA5uOOOpyFu/k1iJYIp6QM7zlVf XrqF9cjEnwKkjBWCtcuKmHTeKwrMq5F1gZud6f7cK1h/h2TSJd7HrmwNvrSEly1b/b mcvvo8f/YYnsFIy+9zSj7K3Uw20Ouy4XraE1aEdDFLo02y7pD/x7iaKsMzufqP1Ap5 mAWhe2Shj23tgD4W4hOjn4IIauzA36YieHSlTqyhl1iDQ2b7I12k3UaYCHNJAIv+AI F2VFVvrW7qkkQ== Date: Fri, 04 Dec 2026 21:26:33 +0000 To: Herbert Xu From: Ryan Prinz Cc: Giovanni Cabiddu , Zhiqi Song , Longfang Liu , "David S . Miller" , qat-linux@intel.com, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, Ryan Prinz Subject: [PATCH 1/3] crypto: dh - add shared DH public key range check Message-ID: <20261204212522.667547-2-rc4d5a@protonmail.com> In-Reply-To: <20261204212522.667547-1-rc4d5a@protonmail.com> References: <20261204212522.667547-1-rc4d5a@protonmail.com> Feedback-ID: 190731814:user:proton X-Pm-Message-ID: a9bf398a48588ee2e71af35551650a5343584766 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Add a shared helper for the DH public key range check from SP800-56A rev3 5.6.2.3.2 (2 <=3D y <=3D 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 --- 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 =3D 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 =3D 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, co= nst struct dh *params) =09return 0; } +/* + * Partial public key validation check from SP800-56A rev3 section + * 5.6.2.3.2: a DH public value is invalid unless 2 <=3D y <=3D 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, +=09=09=09 unsigned int p_size) +{ +=09bool is_zero_or_one =3D true; +=09unsigned int i; + +=09if (!p_size) +=09=09return false; + +=09for (i =3D 0; i < p_size - 1; i++) { +=09=09if (y[i]) { +=09=09=09is_zero_or_one =3D false; +=09=09=09break; +=09=09} +=09} + +=09if (is_zero_or_one && y[p_size - 1] <=3D 1) +=09=09return false; + +=09return 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 *par= ams) { =09int 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 l= en, struct dh *params) int __crypto_dh_decode_key(const char *buf, unsigned int len, =09=09=09 struct dh *params); +/** + * crypto_dh_pubkey_in_range() - range-check a DH public value + * @y:=09=09Big-endian public value to check, zero-padded to @p_size + *=09=09bytes by the caller + * @p_minus_1:=09Big-endian (p - 1) for the same group as @y, @p_size byte= s + * @p_size:=09Size 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 <=3D y <=3D 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 <=3D y <=3D p - 2, false otherwise. + */ +bool crypto_dh_pubkey_in_range(const u8 *y, const u8 *p_minus_1, +=09=09=09 unsigned int p_size); + #endif --=20 2.51.2