Skip to content

Commit 351d207

Browse files
committed
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: #58620 Refs: #3511 Assisted-by: Claude Signed-off-by: James M Snell <jasnell@gmail.com>
1 parent f8dd0aa commit 351d207

8 files changed

Lines changed: 17 additions & 174 deletions

File tree

‎doc/api/crypto.md‎

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,85 +1799,6 @@ If `privateKey` is not valid for the curve specified when the `ECDH` object was
17991799
created, an error is thrown. Upon setting the private key, the associated
18001800
public point (key) is also generated and set in the `ECDH` object.
18011801

1802-
### `ecdh.setPublicKey(publicKey[, encoding])`
1803-
1804-
<!-- YAML
1805-
added: v0.11.14
1806-
deprecated: v5.2.0
1807-
-->
1808-
1809-
> Stability: 0 - Deprecated
1810-
1811-
* `publicKey` {string|ArrayBuffer|Buffer|TypedArray|DataView}
1812-
* `encoding` {string} The [encoding][] of the `publicKey` string.
1813-
1814-
Sets the EC Diffie-Hellman public key.
1815-
If `encoding` is provided `publicKey` is expected to
1816-
be a string; otherwise a [`Buffer`][], `TypedArray`, or `DataView` is expected.
1817-
1818-
There is not normally a reason to call this method because `ECDH`
1819-
only requires a private key and the other party's public key to compute the
1820-
shared secret. Typically either [`ecdh.generateKeys()`][] or
1821-
[`ecdh.setPrivateKey()`][] will be called. The [`ecdh.setPrivateKey()`][] method
1822-
attempts to generate the public point/key associated with the private key being
1823-
set.
1824-
1825-
Example (obtaining a shared secret):
1826-
1827-
```mjs
1828-
const {
1829-
createECDH,
1830-
createHash,
1831-
} = await import('node:crypto');
1832-
1833-
const alice = createECDH('secp256k1');
1834-
const bob = createECDH('secp256k1');
1835-
1836-
// This is a shortcut way of specifying one of Alice's previous private
1837-
// keys. It would be unwise to use such a predictable private key in a real
1838-
// application.
1839-
alice.setPrivateKey(
1840-
createHash('sha256').update('alice', 'utf8').digest(),
1841-
);
1842-
1843-
// Bob uses a newly generated cryptographically strong
1844-
// pseudorandom key pair
1845-
bob.generateKeys();
1846-
1847-
const aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
1848-
const bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
1849-
1850-
// aliceSecret and bobSecret should be the same shared secret value
1851-
console.log(aliceSecret === bobSecret);
1852-
```
1853-
1854-
```cjs
1855-
const {
1856-
createECDH,
1857-
createHash,
1858-
} = require('node:crypto');
1859-
1860-
const alice = createECDH('secp256k1');
1861-
const bob = createECDH('secp256k1');
1862-
1863-
// This is a shortcut way of specifying one of Alice's previous private
1864-
// keys. It would be unwise to use such a predictable private key in a real
1865-
// application.
1866-
alice.setPrivateKey(
1867-
createHash('sha256').update('alice', 'utf8').digest(),
1868-
);
1869-
1870-
// Bob uses a newly generated cryptographically strong
1871-
// pseudorandom key pair
1872-
bob.generateKeys();
1873-
1874-
const aliceSecret = alice.computeSecret(bob.getPublicKey(), null, 'hex');
1875-
const bobSecret = bob.computeSecret(alice.getPublicKey(), null, 'hex');
1876-
1877-
// aliceSecret and bobSecret should be the same shared secret value
1878-
console.log(aliceSecret === bobSecret);
1879-
```
1880-
18811802
## Class: `Hash`
18821803

18831804
<!-- YAML
@@ -8147,8 +8068,6 @@ See the [list of SSL OP Flags][] for details.
81478068
[`diffieHellman.generateKeys()`]: #diffiehellmangeneratekeysencoding
81488069
[`diffieHellman.setPrivateKey()`]: #diffiehellmansetprivatekeyprivatekey-encoding
81498070
[`diffieHellman.setPublicKey()`]: #diffiehellmansetpublickeypublickey-encoding
8150-
[`ecdh.generateKeys()`]: #ecdhgeneratekeysencoding-format
8151-
[`ecdh.setPrivateKey()`]: #ecdhsetprivatekeyprivatekey-encoding
81528071
[`hash.digest()`]: #hashdigestencoding
81538072
[`hash.update()`]: #hashupdatedata-inputencoding
81548073
[`hmac.digest()`]: #hmacdigestencoding

‎doc/api/deprecations.md‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,9 @@ npx codemod@latest @nodejs/slow-buffer-to-buffer-alloc-unsafe-slow
754754

755755
<!-- YAML
756756
changes:
757+
- version: REPLACEME
758+
pr-url: https://github.com/nodejs/node/pull/67493
759+
description: End-of-Life.
757760
- version: v25.0.0
758761
pr-url: https://github.com/nodejs/node/pull/58620
759762
description: Runtime deprecation.
@@ -765,10 +768,13 @@ changes:
765768
description: Documentation-only deprecation.
766769
-->
767770

768-
Type: Runtime
771+
Type: End-of-Life
769772

770-
The [`ecdh.setPublicKey()`][] method is now deprecated as its inclusion in
771-
the API is not useful.
773+
The `ecdh.setPublicKey()` method has been removed as its inclusion in the API
774+
was not useful. `ECDH` only requires a private key and the other party's public
775+
key to compute the shared secret. Use [`ecdh.generateKeys()`][] or
776+
[`ecdh.setPrivateKey()`][] instead; the latter also sets the associated public
777+
key.
772778

773779
### DEP0032: `node:domain` module
774780

@@ -4934,7 +4940,8 @@ Use global `TextDecoder` and `TextEncoder` instead.
49344940
[`dns.lookup()`]: dns.md#dnslookuphostname-options-callback
49354941
[`dnsPromises.lookup()`]: dns.md#dnspromiseslookuphostname-options
49364942
[`domain`]: domain.md
4937-
[`ecdh.setPublicKey()`]: crypto.md#ecdhsetpublickeypublickey-encoding
4943+
[`ecdh.generateKeys()`]: crypto.md#ecdhgeneratekeysencoding-format
4944+
[`ecdh.setPrivateKey()`]: crypto.md#ecdhsetprivatekeyprivatekey-encoding
49384945
[`emitter.listenerCount(eventName)`]: events.md#emitterlistenercounteventname-listener
49394946
[`events.listenerCount(emitter, eventName)`]: events.md#eventslistenercountemitterortarget-eventname
49404947
[`fs.Dir`]: fs.md#class-fsdir

‎lib/internal/crypto/diffiehellman.js‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const {
4141
} = require('internal/util/types');
4242

4343
const {
44-
deprecate,
4544
lazyDOMException,
4645
} = require('internal/util');
4746

@@ -234,9 +233,6 @@ function ECDH(curve) {
234233

235234
ECDH.prototype.computeSecret = DiffieHellman.prototype.computeSecret;
236235
ECDH.prototype.setPrivateKey = DiffieHellman.prototype.setPrivateKey;
237-
ECDH.prototype.setPublicKey = deprecate(DiffieHellman.prototype.setPublicKey,
238-
'ecdh.setPublicKey() is deprecated.',
239-
'DEP0031');
240236
ECDH.prototype.getPrivateKey = DiffieHellman.prototype.getPrivateKey;
241237

242238
ECDH.prototype.generateKeys = function generateKeys(encoding, format) {

‎src/crypto/crypto_ec.cc‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ void ECDH::Initialize(Environment* env, Local<Object> target) {
5858
SetProtoMethod(isolate, t, "computeSecret", ComputeSecret);
5959
SetProtoMethodNoSideEffect(isolate, t, "getPublicKey", GetPublicKey);
6060
SetProtoMethodNoSideEffect(isolate, t, "getPrivateKey", GetPrivateKey);
61-
SetProtoMethod(isolate, t, "setPublicKey", SetPublicKey);
6261
SetProtoMethod(isolate, t, "setPrivateKey", SetPrivateKey);
6362

6463
SetConstructorFunction(context, target, "ECDH", t);
@@ -78,7 +77,6 @@ void ECDH::RegisterExternalReferences(ExternalReferenceRegistry* registry) {
7877
registry->Register(ComputeSecret);
7978
registry->Register(GetPublicKey);
8079
registry->Register(GetPrivateKey);
81-
registry->Register(SetPublicKey);
8280
registry->Register(SetPrivateKey);
8381
registry->Register(ECDH::ConvertKey);
8482
registry->Register(ECDH::GetCurves);
@@ -312,29 +310,6 @@ void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
312310
ecdh->has_valid_key_pair_ = false;
313311
}
314312

315-
void ECDH::SetPublicKey(const FunctionCallbackInfo<Value>& args) {
316-
Environment* env = Environment::GetCurrent(args);
317-
318-
ECDH* ecdh;
319-
ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.This());
320-
321-
CHECK(IsAnyBufferSource(args[0]));
322-
323-
MarkPopErrorOnReturn mark_pop_error_on_return;
324-
325-
auto pub = ECDH::BufferToPoint(env, ecdh->group_, args[0]);
326-
if (!pub) {
327-
return THROW_ERR_CRYPTO_OPERATION_FAILED(env,
328-
"Failed to convert Buffer to EC_POINT");
329-
}
330-
331-
ecdh->has_valid_key_pair_ = false;
332-
if (!ecdh->key_.setPublicKey(pub)) {
333-
return THROW_ERR_CRYPTO_OPERATION_FAILED(env,
334-
"Failed to set EC_POINT as the public key");
335-
}
336-
}
337-
338313
bool ECDH::IsKeyValidForCurve(const BignumPointer& private_key) {
339314
CHECK(group_);
340315
CHECK(private_key);

‎src/crypto/crypto_ec.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class ECDH final : public BaseObject {
4848
static void GetPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args);
4949
static void SetPrivateKey(const v8::FunctionCallbackInfo<v8::Value>& args);
5050
static void GetPublicKey(const v8::FunctionCallbackInfo<v8::Value>& args);
51-
static void SetPublicKey(const v8::FunctionCallbackInfo<v8::Value>& args);
5251

5352
void MaybeCacheValidKeyPair(uint64_t generation);
5453
bool IsKeyPairValid();

‎test/doctool/test-doc-api-json.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,5 @@ for await (const dirent of await fs.opendir(new URL('../../out/doc/api/', import
158158
assert.partialDeepStrictEqual(allExpectedKeys, findAllKeys(json));
159159
}
160160

161-
assert.strictEqual(numberOfDeprecatedSections, 49); // Increase this number every time a new API is deprecated.
161+
assert.strictEqual(numberOfDeprecatedSections, 47); // Increase this number every time a new API is deprecated.
162162
assert.strictEqual(numberOfRemovedAPIs, 49); // Increase this number every time a section is marked as removed.

‎test/parallel/test-crypto-dh-curves.js‎

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,13 @@ if (availableCurves.has('prime256v1')) {
125125
message: 'Invalid ECDH format: 10'
126126
});
127127

128-
// ECDH should allow .setPrivateKey()/.setPublicKey()
128+
// ECDH should allow .setPrivateKey()
129129
const ecdh4 = crypto.createECDH('prime256v1');
130130

131131
ecdh4.setPrivateKey(ecdh1.getPrivateKey());
132-
ecdh4.setPublicKey(ecdh1.getPublicKey());
132+
133+
// ecdh.setPublicKey() has been removed (DEP0031).
134+
assert.strictEqual(ecdh4.setPublicKey, undefined);
133135

134136
const ecdh4Secret = ecdh4.computeSecret(ecdh2.getPublicKey());
135137
assert.deepStrictEqual(ecdh4.computeSecret(ecdh2.getPublicKey()), ecdh4Secret);
@@ -152,12 +154,6 @@ if (availableCurves.has('prime256v1')) {
152154
message: 'Public key is not valid for specified curve'
153155
});
154156

155-
assert.throws(() => {
156-
ecdh4.setPublicKey(ecdh3.getPublicKey());
157-
}, { message: 'Failed to convert Buffer to EC_POINT' });
158-
assert.deepStrictEqual(ecdh4.computeSecret(ecdh2.getPublicKey()),
159-
ecdh4Secret);
160-
161157
// Verify that we can use ECDH without having to use newly generated keys.
162158
const ecdh5 = crypto.createECDH('secp256k1');
163159

@@ -172,9 +168,7 @@ if (availableCurves.has('prime256v1')) {
172168

173169
// A valid private key for the secp256k1 curve.
174170
const cafebabeKey = 'cafebabe'.repeat(8);
175-
// Associated compressed and uncompressed public keys (points).
176-
const cafebabePubPtComp =
177-
'03672a31bfc59d3f04548ec9b7daeeba2f61814e8ccc40448045007f5479f693a3';
171+
// Associated uncompressed public key (point).
178172
const cafebabePubPtUnComp =
179173
'04672a31bfc59d3f04548ec9b7daeeba2f61814e8ccc40448045007f5479f693a3' +
180174
'2e02c7f93d13dc2732b760ca377a5897b9dd41a1c1b29dc0442fdce6d0a04d1d';
@@ -207,29 +201,6 @@ if (availableCurves.has('prime256v1')) {
207201
assert.strictEqual(ecdh5.getPrivateKey('hex'), cafebabeKey);
208202
assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);
209203

210-
// Verify setting and getting compressed and non-compressed serializations.
211-
ecdh5.setPublicKey(cafebabePubPtComp, 'hex');
212-
assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);
213-
assert.strictEqual(
214-
ecdh5.getPublicKey('hex', 'compressed'),
215-
cafebabePubPtComp
216-
);
217-
ecdh5.setPublicKey(cafebabePubPtUnComp, 'hex');
218-
assert.strictEqual(ecdh5.getPublicKey('hex'), cafebabePubPtUnComp);
219-
assert.strictEqual(
220-
ecdh5.getPublicKey('hex', 'compressed'),
221-
cafebabePubPtComp
222-
);
223-
224-
// Show why allowing the public key to be set on this type
225-
// does not make sense.
226-
ecdh5.setPublicKey(peerPubPtComp, 'hex');
227-
assert.strictEqual(ecdh5.getPublicKey('hex'), peerPubPtUnComp);
228-
assert.throws(() => {
229-
// Error because the public key does not match the private key anymore.
230-
ecdh5.computeSecret(peerPubPtComp, 'hex', 'hex');
231-
}, /Invalid key pair/);
232-
233204
// Set to a valid key to show that later attempts to set an invalid key are
234205
// rejected.
235206
ecdh5.setPrivateKey(cafebabeKey, 'hex');

‎test/parallel/test-crypto-ecdh-setpublickey-deprecation.js‎

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)