diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 4d4226b22719..215c8a254251 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])` - - - -> 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` -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 @@ -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 diff --git a/lib/internal/crypto/diffiehellman.js b/lib/internal/crypto/diffiehellman.js index 46659419c8d6..7a18019224d4 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 f51d311846cd..c9fdfefd4d26 100644 --- a/src/crypto/crypto_ec.cc +++ b/src/crypto/crypto_ec.cc @@ -58,7 +58,6 @@ void ECDH::Initialize(Environment* env, Local 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& args) { ecdh->has_valid_key_pair_ = false; } -void ECDH::SetPublicKey(const FunctionCallbackInfo& 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 5b8fbf3aa591..553b4e31fc40 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& args); static void SetPrivateKey(const v8::FunctionCallbackInfo& args); static void GetPublicKey(const v8::FunctionCallbackInfo& args); - static void SetPublicKey(const v8::FunctionCallbackInfo& args); void MaybeCacheValidKeyPair(uint64_t generation); bool IsKeyPairValid(); diff --git a/test/doctool/test-doc-api-json.mjs b/test/doctool/test-doc-api-json.mjs index 104ef026c762..aa0609c44a21 100644 --- a/test/doctool/test-doc-api-json.mjs +++ b/test/doctool/test-doc-api-json.mjs @@ -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. diff --git a/test/parallel/test-crypto-dh-curves.js b/test/parallel/test-crypto-dh-curves.js index 0b2270b37a30..01405977cc04 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 317c219f501a..000000000000 --- 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 -}