fix: preserve existing keys in DH generateKeys()#954
Merged
Conversation
Use DH_generate_key() directly instead of EVP_PKEY_keygen(), matching Node.js/ncrypto behavior. DH_generate_key() preserves an already-set private key and only derives the public key from it, whereas EVP_PKEY_keygen() unconditionally generates a fresh keypair. Fixes #952
EVP_PKEY_get0_DH can return a provider-cached copy in OpenSSL 3.x, so mutating it via DH_generate_key doesn't persist. Switch to EVP_PKEY_get1_DH to get an owned copy, generate keys on it, then re-wrap in a fresh EVP_PKEY.
Contributor
🤖 End-to-End Test Results - AndroidStatus: ✅ Passed 📸 Final Test ScreenshotScreenshot automatically captured from End-to-End tests and will expire in 30 days This comment is automatically updated on each test run. |
Contributor
🤖 End-to-End Test Results - iOSStatus: ✅ Passed 📸 Final Test ScreenshotScreenshot automatically captured from End-to-End tests and will expire in 30 days This comment is automatically updated on each test run. |
boorad
added a commit
that referenced
this pull request
Feb 24, 2026
New pages: PQC (ML-DSA/ML-KEM), Argon2, KMAC, Certificate (SPKAC), and Utilities (one-shot hash, timingSafeEqual, primes, introspection). Updated 8 existing pages with missing API sections: SubtleCrypto (deriveBits, deriveKey, wrapKey/unwrapKey, encapsulation), Keys (KeyObject.from, equals, toCryptoKey), Signing (standalone sign/verify), DiffieHellman (diffieHellman function), Ed25519 (Ed448/X448), Hash (crypto.hash one-shot, SHA3), ECDH (convertKey), and reorganized the API index into Core/Key Exchange/Key Derivation/Advanced sections. Annotated 7 pages with behavioral notes from recent fix PRs (#929, #930, #932, #933, #939, #948, #949, #951, #954, #955): cipher single-use warning, generateKeys preservation, PBKDF2 validation, OAEP hash default, randomFill view correctness, RSA-* aliases, and flexible curve names. Added llms.txt index route and fixed llms-full.txt JSX stripping in source.ts to produce clean LLM-friendly output.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
diffieHellman.generateKeys()was overwriting existing keys instead of preserving a previously set private key and computing the public key from it. This fixes the behavior to match Node.js semantics.Fixes #952
Changes
EVP_PKEY_get1_DH(owned copy) instead ofEVP_PKEY_get0_DH(internal pointer) ingenerateKeys()— OpenSSL 3.x's provider layer can return a cached copy fromget0, so mutations viaDH_generate_keydon't persistEVP_PKEY_keygen(which always generates a new key pair) withDH_generate_key(which preserves an existing private key and only computes the public key)generateKeys()callsworkspace:*for rnqc dependency in example appTesting
generateKeys should preserve a previously set private keygenerateKeys should not regenerate keys on second call