You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
π Bug Report β Runtime APIs: node:crypto diffieHellman fails for an imported X25519 public keyΒ #7743
With nodejs_compat, crypto.diffieHellman works for X25519 keys from generateKeyPairSync, and for a public key from createPublicKey(privateKeyObject). It throws Failed to derive shared diffie-hellman secret when the public key is imported from bytes, as SPKI DER or as JWK. The same code works in Node.js 22.
Seen with workerd 1.20261001.1 (wrangler 4.147.0), compatibility date 2026-10-01.
imported.asymmetricKeyType is 'x25519'. Importing as JWK ({ kty: 'OKP', crv: 'X25519', x }) fails the same way.
This blocks any protocol where the peer's public key arrives as bytes, such as a Noise handshake. We work around it with @noble/curves for the X25519 step.
Related: #3684, which added X25519 support to stateless diffieHellman.
It runs EVP_PKEY_derive_init on ourKey and EVP_PKEY_derive_set_peer on theirKey. So the derive context is built on the public key. That only works if the public key object also holds a private key.
That explains why the existing test passes. In generateEdKeyPair the public half is created with AsymmetricKey::NewPublic(generated.clone()), and clone() is EVP_PKEY_up_ref on the same EVP_PKEY, private part included. So a public key from generateKeyPairSync('x25519') can stand in for "our" key and the swap goes unnoticed. statelessDhX25591 in crypto_dh-test.js only uses generated pairs. A public key imported from SPKI, raw bytes or JWK has no private part, so derive_init fails, which is the "Failed to derive shared diffie-hellman secret" in this issue.
Repro with fixed inputs (RFC 7748 section 6.1), same results on workerd 2026-10-23 (wrangler 4.156.0), 2026-10-30 and 2026-11-01, compatibility_date 2026-10-01, nodejs_compat:
auto data = ncrypto::DHPointer::stateless(pvtKey, pubKey);
plus a test that imports the peer's public key (the RFC 7748 vector above makes a good known-answer test).
Separately, diffieHellman is a named export of node:crypto but is missing from its default export (src/node/crypto.ts), so import crypto from 'node:crypto'; crypto.diffieHellman is undefined, while it is defined in Node. Happy to open a separate issue for that if you prefer.
Activity
dial481 commentedon Nov 1, 2026
Encountered the same issue in the ssh2 library (curve25519-sha256 key exchange), and I think the cause is an argument swap in
statelessDH.src/workerd/api/node/crypto-keys.c++,CryptoImpl::statelessDH(line 872 at 6472e6e):auto data = ncrypto::DHPointer::stateless(pubKey, pvtKey);ncrypto's signature (v1.1.4, the version in
build/deps/gen/deps.MODULE.bazel) is:It runs
EVP_PKEY_derive_initonourKeyandEVP_PKEY_derive_set_peerontheirKey. So the derive context is built on the public key. That only works if the public key object also holds a private key.That explains why the existing test passes. In
generateEdKeyPairthe public half is created withAsymmetricKey::NewPublic(generated.clone()), andclone()isEVP_PKEY_up_refon the same EVP_PKEY, private part included. So a public key fromgenerateKeyPairSync('x25519')can stand in for "our" key and the swap goes unnoticed.statelessDhX25591incrypto_dh-test.jsonly uses generated pairs. A public key imported from SPKI, raw bytes or JWK has no private part, soderive_initfails, which is the "Failed to derive shared diffie-hellman secret" in this issue.Repro with fixed inputs (RFC 7748 section 6.1), same results on workerd 2026-10-23 (wrangler 4.156.0), 2026-10-30 and 2026-11-01, compatibility_date 2026-10-01, nodejs_compat:
Suggested fix (not built or tested on our side):
auto data = ncrypto::DHPointer::stateless(pvtKey, pubKey);plus a test that imports the peer's public key (the RFC 7748 vector above makes a good known-answer test).
Separately,
diffieHellmanis a named export ofnode:cryptobut is missing from its default export (src/node/crypto.ts), soimport crypto from 'node:crypto'; crypto.diffieHellmanis undefined, while it is defined in Node. Happy to open a separate issue for that if you prefer.Fix node:crypto diffieHellman with an imported public key