Commit 345e2b2fd73 for nodejs
commit 345e2b2fd73f58f0aedd26ec99f57c9a2d2836f4
Author: James M Snell <jasnell@gmail.com>
Date: Sun Nov 15 16:51:57 2026 +0000
crypto: move DEP0031 to End-of-Life
Remove ecdh.setPublicKey(). ECDH only requires a private key and the
other party's public key to compute a shared secret, and
ecdh.setPrivateKey() already derives and sets the associated public
key. The native ECDH setPublicKey binding is removed as well.
diffieHellman.setPublicKey() is unaffected.
This has been a runtime deprecation since v25.0.0 and a
documentation-only deprecation since v5.2.0.
Refs: https://github.com/nodejs/node/pull/58620
Refs: https://github.com/nodejs/node/pull/3511
Assisted-by: Claude
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67493
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
diff --git a/doc/api/crypto.md b/doc/api/crypto.md
index cb8e2186c42..152dddde2c1 100644
--- a/doc/api/crypto.md
+++ b/doc/api/crypto.md
@@ -1799,85 +1799,6 @@ If `privateKey` is not valid for the curve specified when the `ECDH` object was
created, an error is thrown. Upon setting the private key, the associated
public point (key) is also generated and set in the `ECDH` object.
-### `ecdh.setPublicKey(publicKey[, encoding])`
-
-<!-- YAML
-added: v0.11.14
-deprecated: v5.2.0
--->
-
-> Stability: 0 - Deprecated
-
-* `publicKey` {string|ArrayBuffer|Buffer|TypedArray|DataView}
-* `encoding` {string} The [encoding][] of the `publicKey` string.
-
-Sets the EC Diffie-Hellman public key.
-If `encoding` is provided `publicKey` is expected to
-be a string; otherwise a [`Buffer`][], `TypedArray`, or `DataView` is expected.
-
-There is not normally a reason to call this method because `ECDH`
-only requires a private key and the other party's public key to compute the
-shared secret. Typically either [`ecdh.generateKeys()`][] or
-[`ecdh.setPrivateKey()`][] will be called. The [`ecdh.setPrivateKey()`][] method
-attempts to generate the public point/key associated with the private key being
-set.
-
-Example (obtaining a shared secret):
-
-```mjs
-const {
- createECDH,
- createHash,
-} = await import('node:crypto');
-
-const alice = createECDH('secp256k1');
-const bob = createECDH('secp256k1');
-
-// This is a shortcut way of specifying one of Alice's previous private
-// keys. It would be unwise to use such a predictable private key in a real
-// application.
-alice.setPrivateKey(
- createHash('sha256').update('alice', 'utf8').digest(),
-);
-
-// Bob uses a newly generated cryptographically strong
-// pseudorandom key pair
-bob.generateKeys();
-
-const aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
-const bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
-
-// aliceSecret and bobSecret should be the same shared secret value
-console.log(aliceSecret === bobSecret);
-```
-
-```cjs
-const {
- createECDH,
- createHash,
-} = require('node:crypto');
-
-const alice = createECDH('secp256k1');
-const bob = createECDH('secp256k1');
-
-// This is a shortcut way of specifying one of Alice's previous private
-// keys. It would be unwise to use such a predictable private key in a real
-// application.
-alice.setPrivateKey(
- createHash('sha256').update('alice', 'utf8').digest(),
-);
-
-// Bob uses a newly generated cryptographically strong
-// pseudorandom key pair
-bob.generateKeys();
-
-const aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
-const bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
-
-// aliceSecret and bobSecret should be the same shared secret value
-console.log(aliceSecret === bobSecret);
-```
-
## Class: `Hash`
<!-- YAML
@@ -8107,8 +8028,6 @@ See the [list of SSL OP Flags][] for details.
[`diffieHellman.generateKeys()`]: #diffiehellmangeneratekeysencoding
[`diffieHellman.setPrivateKey()`]: #diffiehellmansetprivatekeyprivatekey-encoding
[`diffieHellman.setPublicKey()`]: #diffiehellmansetpublickeypublickey-encoding
-[`ecdh.generateKeys()`]: #ecdhgeneratekeysencoding-format
-[`ecdh.setPrivateKey()`]: #ecdhsetprivatekeyprivatekey-encoding
[`hash.digest()`]: #hashdigestencoding
[`hash.update()`]: #hashupdatedata-inputencoding
[`hmac.digest()`]: #hmacdigestencoding
diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md
index 9f0afb750a9..65ad4578d6c 100644
--- a/doc/api/deprecations.md
+++ b/doc/api/deprecations.md
@@ -754,6 +754,9 @@ npx codemod@latest @nodejs/slow-buffer-to-buffer-alloc-unsafe-slow
<!-- YAML
changes:
+ - version: REPLACEME
+ pr-url: https://github.com/nodejs/node/pull/67493
+ description: End-of-Life.
- version: v25.0.0
pr-url: https://github.com/nodejs/node/pull/58620
description: Runtime deprecation.
@@ -765,10 +768,13 @@ changes:
description: Documentation-only deprecation.
-->
-Type: Runtime
+Type: End-of-Life
-The [`ecdh.setPublicKey()`][] method is now deprecated as its inclusion in
-the API is not useful.
+The `ecdh.setPublicKey()` method has been removed as its inclusion in the API
+was not useful. `ECDH` only requires a private key and the other party's public
+key to compute the shared secret. Use [`ecdh.generateKeys()`][] or
+[`ecdh.setPrivateKey()`][] instead; the latter also sets the associated public
+key.
### DEP0032: `node:domain` module
@@ -4995,7 +5001,8 @@ This affects multiple TLS APIs:
[`dns.lookup()`]: dns.md#dnslookuphostname-options-callback
[`dnsPromises.lookup()`]: dns.md#dnspromiseslookuphostname-options
[`domain`]: domain.md
-[`ecdh.setPublicKey()`]: crypto.md#ecdhsetpublickeypublickey-encoding
+[`ecdh.generateKeys()`]: crypto.md#ecdhgeneratekeysencoding-format
+[`ecdh.setPrivateKey()`]: crypto.md#ecdhsetprivatekeyprivatekey-encoding
[`emitter.listenerCount(eventName)`]: events.md#emitterlistenercounteventname-listener
[`events.listenerCount(emitter, eventName)`]: events.md#eventslistenercountemitterortarget-eventname
[`fs.Dir`]: fs.md#class-fsdir
diff --git a/lib/internal/crypto/diffiehellman.js b/lib/internal/crypto/diffiehellman.js
index 46659419c8d..7a18019224d 100644
--- a/lib/internal/crypto/diffiehellman.js
+++ b/lib/internal/crypto/diffiehellman.js
@@ -41,7 +41,6 @@ const {
} = require('internal/util/types');
const {
- deprecate,
lazyDOMException,
} = require('internal/util');
@@ -234,9 +233,6 @@ function ECDH(curve) {
ECDH.prototype.computeSecret = DiffieHellman.prototype.computeSecret;
ECDH.prototype.setPrivateKey = DiffieHellman.prototype.setPrivateKey;
-ECDH.prototype.setPublicKey = deprecate(DiffieHellman.prototype.setPublicKey,
- 'ecdh.setPublicKey() is deprecated.',
- 'DEP0031');
ECDH.prototype.getPrivateKey = DiffieHellman.prototype.getPrivateKey;
ECDH.prototype.generateKeys = function generateKeys(encoding, format) {
diff --git a/src/crypto/crypto_ec.cc b/src/crypto/crypto_ec.cc
index f51d311846c..c9fdfefd4d2 100644
--- a/src/crypto/crypto_ec.cc
+++ b/src/crypto/crypto_ec.cc
@@ -58,7 +58,6 @@ void ECDH::Initialize(Environment* env, Local<Object> target) {
SetProtoMethod(isolate, t, "computeSecret", ComputeSecret);
SetProtoMethodNoSideEffect(isolate, t, "getPublicKey", GetPublicKey);
SetProtoMethodNoSideEffect(isolate, t, "getPrivateKey", GetPrivateKey);
- SetProtoMethod(isolate, t, "setPublicKey", SetPublicKey);
SetProtoMethod(isolate, t, "setPrivateKey", SetPrivateKey);
SetConstructorFunction(context, target, "ECDH", t);
@@ -78,7 +77,6 @@ void ECDH::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(ComputeSecret);
registry->Register(GetPublicKey);
registry->Register(GetPrivateKey);
- registry->Register(SetPublicKey);
registry->Register(SetPrivateKey);
registry->Register(ECDH::ConvertKey);
registry->Register(ECDH::GetCurves);
@@ -312,29 +310,6 @@ void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
ecdh->has_valid_key_pair_ = false;
}
-void ECDH::SetPublicKey(const FunctionCallbackInfo<Value>& args) {
- Environment* env = Environment::GetCurrent(args);
-
- ECDH* ecdh;
- ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.This());
-
- CHECK(IsAnyBufferSource(args[0]));
-
- MarkPopErrorOnReturn mark_pop_error_on_return;
-
- auto pub = ECDH::BufferToPoint(env, ecdh->group_, args[0]);
- if (!pub) {
- return THROW_ERR_CRYPTO_OPERATION_FAILED(env,
- "Failed to convert Buffer to EC_POINT");
- }
-
- ecdh->has_valid_key_pair_ = false;
- if (!ecdh->key_.setPublicKey(pub)) {
- return THROW_ERR_CRYPTO_OPERATION_FAILED(env,
- "Failed to set EC_POINT as the public key");
- }
-}
-
bool ECDH::IsKeyValidForCurve(const BignumPointer& private_key) {
CHECK(group_);
CHECK(private_key);
diff --git a/src/crypto/crypto_ec.h b/src/crypto/crypto_ec.h
index 5b8fbf3aa59..553b4e31fc4 100644
--- a/src/crypto/crypto_ec.h
+++ b/src/crypto/crypto_ec.h
@@ -48,7 +48,6 @@ class ECDH final : public BaseObject {
static void GetPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetPublicKey(const v8::FunctionCallbackInfo<v8::Value>& args);
- static void SetPublicKey(const v8::FunctionCallbackInfo<v8::Value>& args);
void MaybeCacheValidKeyPair(uint64_t generation);
bool IsKeyPairValid();
diff --git a/test/parallel/test-crypto-dh-curves.js b/test/parallel/test-crypto-dh-curves.js
index 0b2270b37a3..01405977cc0 100644
--- a/test/parallel/test-crypto-dh-curves.js
+++ b/test/parallel/test-crypto-dh-curves.js
@@ -125,11 +125,13 @@ if (availableCurves.has('prime256v1')) {
message: 'Invalid ECDH format: 10'
});
- // ECDH should allow .setPrivateKey()/.setPublicKey()
+ // ECDH should allow .setPrivateKey()
const ecdh4 = crypto.createECDH('prime256v1');
ecdh4.setPrivateKey(ecdh1.getPrivateKey());
- ecdh4.setPublicKey(ecdh1.getPublicKey());
+
+ // ecdh.setPublicKey() has been removed (DEP0031).
+ assert.strictEqual(ecdh4.setPublicKey, undefined);
const ecdh4Secret = ecdh4.computeSecret(ecdh2.getPublicKey());
assert.deepStrictEqual(ecdh4.computeSecret(ecdh2.getPublicKey()), ecdh4Secret);
@@ -152,12 +154,6 @@ if (availableCurves.has('prime256v1')) {
message: 'Public key is not valid for specified curve'
});
- assert.throws(() => {
- ecdh4.setPublicKey(ecdh3.getPublicKey());
- }, { message: 'Failed to convert Buffer to EC_POINT' });
- assert.deepStrictEqual(ecdh4.computeSecret(ecdh2.getPublicKey()),
- ecdh4Secret);
-
// Verify that we can use ECDH without having to use newly generated keys.
const ecdh5 = crypto.createECDH('secp256k1');
@@ -172,9 +168,7 @@ if (availableCurves.has('prime256v1')) {
// A valid private key for the secp256k1 curve.
const cafebabeKey = 'cafebabe'.repeat(8);
- // Associated compressed and uncompressed public keys (points).
- const cafebabePubPtComp =
- '03672a31bfc59d3f04548ec9b7daeeba2f61814e8ccc40448045007f5479f693a3';
+ // Associated uncompressed public key (point).
const cafebabePubPtUnComp =
'04672a31bfc59d3f04548ec9b7daeeba2f61814e8ccc40448045007f5479f693a3' +
'2e02c7f93d13dc2732b760ca377a5897b9dd41a1c1b29dc0442fdce6d0a04d1d';
@@ -207,29 +201,6 @@ if (availableCurves.has('prime256v1')) {
assert.strictEqual(ecdh5.getPrivateKey('hex'), cafebabeKey);
assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);
- // Verify setting and getting compressed and non-compressed serializations.
- ecdh5.setPublicKey(cafebabePubPtComp, 'hex');
- assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);
- assert.strictEqual(
- ecdh5.getPublicKey('hex', 'compressed'),
- cafebabePubPtComp
- );
- ecdh5.setPublicKey(cafebabePubPtUnComp, 'hex');
- assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);
- assert.strictEqual(
- ecdh5.getPublicKey('hex', 'compressed'),
- cafebabePubPtComp
- );
-
- // Show why allowing the public key to be set on this type
- // does not make sense.
- ecdh5.setPublicKey(peerPubPtComp, 'hex');
- assert.strictEqual(ecdh5.getPublicKey('hex'), peerPubPtUnComp);
- assert.throws(() => {
- // Error because the public key does not match the private key anymore.
- ecdh5.computeSecret(peerPubPtComp, 'hex', 'hex');
- }, /Invalid key pair/);
-
// Set to a valid key to show that later attempts to set an invalid key are
// rejected.
ecdh5.setPrivateKey(cafebabeKey, 'hex');
diff --git a/test/parallel/test-crypto-ecdh-setpublickey-deprecation.js b/test/parallel/test-crypto-ecdh-setpublickey-deprecation.js
deleted file mode 100644
index 317c219f501..00000000000
--- a/test/parallel/test-crypto-ecdh-setpublickey-deprecation.js
+++ /dev/null
@@ -1,24 +0,0 @@
-// Flags: --no-warnings
-'use strict';
-
-const common = require('../common');
-
-if (!common.hasCrypto) {
- common.skip('missing crypto');
-}
-
-const crypto = require('crypto');
-
-common.expectWarning(
- 'DeprecationWarning',
- 'ecdh.setPublicKey() is deprecated.', 'DEP0031');
-
-const ec = crypto.createECDH('prime256v1');
-try {
- // This will throw but we don't care about the error,
- // we just want to verify that the deprecation warning
- // is emitted.
- ec.setPublicKey(Buffer.from([123]));
-} catch {
- // Intentionally ignore the error
-}