Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 0 additions & 81 deletions doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -8137,8 +8058,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
Expand Down
15 changes: 11 additions & 4 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -4901,7 +4907,8 @@ Type: Documentation-only
[`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
Expand Down
4 changes: 0 additions & 4 deletions lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const {
} = require('internal/util/types');

const {
deprecate,
lazyDOMException,
} = require('internal/util');

Expand Down Expand Up @@ -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) {
Expand Down
25 changes: 0 additions & 25 deletions src/crypto/crypto_ec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/crypto/crypto_ec.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion test/doctool/test-doc-api-json.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,5 @@ for await (const dirent of await fs.opendir(new URL('../../out/doc/api/', import
assert.partialDeepStrictEqual(allExpectedKeys, findAllKeys(json));
}

assert.strictEqual(numberOfDeprecatedSections, 47); // Increase this number every time a new API is deprecated.
assert.strictEqual(numberOfDeprecatedSections, 46); // Increase this number every time a new API is deprecated.
assert.strictEqual(numberOfRemovedAPIs, 49); // Increase this number every time a section is marked as removed.
39 changes: 5 additions & 34 deletions test/parallel/test-crypto-dh-curves.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');

Expand All @@ -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';
Expand Down Expand Up @@ -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');
Expand Down
24 changes: 0 additions & 24 deletions test/parallel/test-crypto-ecdh-setpublickey-deprecation.js

This file was deleted.

Loading