Skip to content

Check DiffieHellman parameters only when verifyError is read - #8931

Merged
danlapid merged 1 commit into
mainfrom
dlapid/lazy-dh-verify-error
Nov 26, 2026
Merged

danlapid merged 1 commit into
mainfrom
dlapid/lazy-dh-verify-error

Conversation

@danlapid

Copy link
Copy Markdown
Collaborator

DiffieHellmanHandle ran DH_check() in its constructor, so every createDiffieHellman(), createDiffieHellmanGroup() and getDiffieHellman() paid for primality tests of the prime and of (p-1)/2, whether or not the script ever read verifyError. For the 2048-bit and larger standard groups, those tests cost far more CPU than anything else the handle does. DH_check() reads only the prime and generator, which never change after construction, so getVerifyError() now runs it on first use and caches the result.

crypto_dh-test created a dozen such handles and also generated two random 1024-bit safe primes, whose search time varies widely from run to run. It took 6–13 s in debug builds and close to a minute on slow CI machines. Now:

  • dhKeygenTest generates a 512-bit prime. FIPS-mode BoringSSL accepts that too, despite what the test's comment said.
  • dhPubKeyValidation needs no generated prime, so it uses modp14's.
  • The test drops its enormous size.

Five runs of each variant: debug max 1.6 s, ASan 3.7 s, release 1.1 s.

🤖 Generated with Claude Code

DiffieHellmanHandle ran DH_check() in its constructor, so every
createDiffieHellman(), createDiffieHellmanGroup() and
getDiffieHellman() paid for primality tests of the prime and of
(p-1)/2, whether or not the script ever read verifyError. For the
2048-bit and larger standard groups, those tests cost far more CPU
than anything else the handle does. DH_check() reads only the prime
and generator, which never change after construction, so
getVerifyError() now runs it on first use and caches the result.

crypto_dh-test created a dozen such handles, and also generated two
random 1024-bit safe primes, whose search time varies widely from
run to run. It took 6-13 s in debug builds and close to a minute on
slow CI machines. dhKeygenTest now generates a 512-bit prime, which
FIPS-mode BoringSSL accepts too, despite the comment the test had.
dhPubKeyValidation, which needs no generated prime, uses modp14's.
The test now takes about a second, so it no longer needs the
enormous size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@danlapid
danlapid marked this pull request as ready for review November 26, 2026 05:22
@danlapid
danlapid requested review from a team as code owners November 26, 2026 05:22
@danlapid
danlapid merged commit fe1cceb into main Nov 26, 2026
24 checks passed
@danlapid
danlapid deleted the dlapid/lazy-dh-verify-error branch November 26, 2026 05:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant