Re: [PATCH v2] crypto: qat - fix missing DH public key validation

From: rc4d5a

Date: Wed Nov 25 2026 - 18:31:32 EST


I agree, fixed and sent as a separate patch, "crypto: dh - add shared DH public key range check", exporting crypto_dh_pubkey_in_range() from crypto/dh_helper.c and declaring it in dh.h. The QAT fix is now v3 and HPRE's is now v4, both calling that instead of carrying their own copy of the same range check. Each driver still does its own zero-padding and (p - 1) computation, since that part depends on how each already has the data; only the actual comparison was duplicated, so that's the part that moved.

The generic software "dh" implementation (crypto/dh.c) had the same
gap: dh_is_pubkey_valid() returned 0 immediately whenever fips_enabled was false, so a non-FIPS kernel never checked the peer's value there either. Sent a separate patch for that too, "crypto: dh - fix missing DH public key validation on non-FIPS kernels": it tightens the existing range check from y < p to y <= p - 2 (the y < p shortcut relied on the FIPS-only y^q mod p test to reject p - 1) and runs it before the FIPS gate instead of behind it, so it's a complete check on its own regardless of mode. The y^q mod p check stays FIPS-only, unchanged.

Thanks,
Ryan



On Wednesday, November 25th, 2026 at 7:04 PM, Giovanni Cabiddu <giovanni.cabiddu@xxxxxxxxx> wrote:

> On Tue, Nov 24, 2026 at 02:12:54PM +0000, Ryan Prinz wrote:
> > 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.
> Would it make sense to have a provider-independent check for the public
> key in crypto/dh_helper.c instead?
>
> This way, software DH, QAT and HiSilicon HPRE, which all register "dh",
> could use the same check (regardless of FIPS mode).
>
> Thanks,
>
> --
> Giovanni
>