fix: support EC keys in diffieHellman() and validate curve match#960
Merged
Conversation
The diffieHellman() function only accepted x25519/x448 keys, throwing "Unknown curve type: ec" for EC keys like secp384r1. Route EC keys through the ECDH class to compute the shared secret.
Add curve mismatch validation in checkDiffieHellmanOptions for EC keys so mismatched curves (e.g. P-256 vs P-384) throw a clear error instead of an opaque OpenSSL failure. Also fix ecDeriveBits to decode JWK parameters with 'base64url' per RFC 7517 instead of 'base64'.
Cover P-256, P-384, P-521 via crypto.diffieHellman() with tests for shared secret size, symmetry, determinism, uniqueness across key pairs, and curve mismatch error.
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. |
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
Adds EC key support (P-256, P-384, P-521) to the
diffieHellman()API and fixes related correctness issues. Previously,diffieHellman()only supported x25519/x448 keys and threwUnknown curve type: ecfor EC keys.Fixes #959
Changes
diffieHellman()to a newecDiffieHellman()handler that leverages the existingECDHclass via JWK export/importcheckDiffieHellmanOptions()validation to the top-leveldiffieHellman()dispatcher so it applies to all key types'ec'to the allowed key types incheckDiffieHellmanOptions()ecDeriveBits()to decode JWK parameters with'base64url'(per RFC 7517) instead of'base64'Testing
crypto.generateKeyPairSync('ec', { namedCurve: '...' })crypto.diffieHellman({ privateKey, publicKey })with EC keysdiffieHellman()still works unchanged