Skip to content

πŸ› Bug Report β€” Runtime APIs: node:crypto diffieHellman fails for an imported X25519 public keyΒ #7743

Open

Description

@janicduplessis

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.

import { createPublicKey, diffieHellman, generateKeyPairSync } from 'node:crypto';

export default {
  fetch() {
    const a = generateKeyPairSync('x25519');
    const b = generateKeyPairSync('x25519');
    const der = b.publicKey.export({ format: 'der', type: 'spki' });
    const imported = createPublicKey({ key: der, format: 'der', type: 'spki' });
    diffieHellman({ privateKey: a.privateKey, publicKey: b.publicKey }); // works
    diffieHellman({ privateKey: a.privateKey, publicKey: imported }); // throws
    return new Response('ok');
  },
};

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions