From 40253cc1c8926c285b099481b644e68d54f7cd36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Fri, 3 Jan 2020 12:01:34 +0100 Subject: [PATCH] crypto: add crypto.diffieHellman Currently, Node.js has separate (stateful) APIs for DH/ECDH, and no support for ECDH-ES. This commit adds a single stateless function to compute the DH/ECDH/ECDH-ES secret based on two KeyObjects. PR-URL: https://github.com/nodejs/node/pull/31178 Reviewed-By: Sam Roberts --- doc/api/crypto.md | 14 ++ doc/api/errors.md | 5 + lib/crypto.js | 4 +- lib/internal/crypto/diffiehellman.js | 44 ++++- lib/internal/errors.js | 1 + src/node_crypto.cc | 69 ++++++- test/parallel/test-crypto-dh-stateless.js | 222 ++++++++++++++++++++++ 7 files changed, 345 insertions(+), 14 deletions(-) create mode 100644 test/parallel/test-crypto-dh-stateless.js diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 1d411f8526365f..83f584ad882db2 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -2089,6 +2089,20 @@ the corresponding digest algorithm. This does not work for all signature algorithms, such as `'ecdsa-with-SHA256'`, so it is best to always use digest algorithm names. +### `crypto.diffieHellman(options)` + + +* `options`: {Object} + * `privateKey`: {KeyObject} + * `publicKey`: {KeyObject} +* Returns: {Buffer} + +Computes the Diffie-Hellman secret based on a `privateKey` and a `publicKey`. +Both keys must have the same `asymmetricKeyType`, which must be one of `'dh'` +(for Diffie-Hellman), `'ec'` (for ECDH), `'x448'`, or `'x25519'` (for ECDH-ES). + ### `crypto.generateKeyPair(type, options, callback)`