From 772496c030fa2f7026d47dcbd59587c01d735e61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Wed, 16 Aug 2023 17:23:06 +0200 Subject: [PATCH] crypto: remove default encoding from DiffieHellman getDefaultEncoding() always returns 'buffer' in Node.js 20. In diffiehellman.js, this value is always used as input to either toBuf(), encode(), or getArrayBufferOrView(). All of these functions treat any falsy encoding just like 'buffer', so we can safely remove the calls to getDefaultEncoding(). Refs: https://github.com/nodejs/node/pull/47182 PR-URL: https://github.com/nodejs/node/pull/49169 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Filip Skokan --- lib/internal/crypto/diffiehellman.js | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/lib/internal/crypto/diffiehellman.js b/lib/internal/crypto/diffiehellman.js index 3f52e78d7a3036..59bbf8ff71233c 100644 --- a/lib/internal/crypto/diffiehellman.js +++ b/lib/internal/crypto/diffiehellman.js @@ -51,7 +51,6 @@ const { const { getArrayBufferOrView, - getDefaultEncoding, jobPromise, toBuf, kHandle, @@ -97,10 +96,6 @@ function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) { keyEncoding = false; } - const encoding = getDefaultEncoding(); - keyEncoding = keyEncoding || encoding; - genEncoding = genEncoding || encoding; - if (typeof sizeOrKey !== 'number') sizeOrKey = toBuf(sizeOrKey, keyEncoding); @@ -148,7 +143,6 @@ DiffieHellmanGroup.prototype.generateKeys = function dhGenerateKeys(encoding) { const keys = this[kHandle].generateKeys(); - encoding = encoding || getDefaultEncoding(); return encode(keys, encoding); } @@ -158,9 +152,6 @@ DiffieHellmanGroup.prototype.computeSecret = dhComputeSecret; function dhComputeSecret(key, inEnc, outEnc) { - const encoding = getDefaultEncoding(); - inEnc = inEnc || encoding; - outEnc = outEnc || encoding; key = getArrayBufferOrView(key, 'key', inEnc); const ret = this[kHandle].computeSecret(key); if (typeof ret === 'string') @@ -175,7 +166,6 @@ DiffieHellmanGroup.prototype.getPrime = function dhGetPrime(encoding) { const prime = this[kHandle].getPrime(); - encoding = encoding || getDefaultEncoding(); return encode(prime, encoding); } @@ -186,7 +176,6 @@ DiffieHellmanGroup.prototype.getGenerator = function dhGetGenerator(encoding) { const generator = this[kHandle].getGenerator(); - encoding = encoding || getDefaultEncoding(); return encode(generator, encoding); } @@ -197,7 +186,6 @@ DiffieHellmanGroup.prototype.getPublicKey = function dhGetPublicKey(encoding) { const key = this[kHandle].getPublicKey(); - encoding = encoding || getDefaultEncoding(); return encode(key, encoding); } @@ -208,13 +196,11 @@ DiffieHellmanGroup.prototype.getPrivateKey = function dhGetPrivateKey(encoding) { const key = this[kHandle].getPrivateKey(); - encoding = encoding || getDefaultEncoding(); return encode(key, encoding); } DiffieHellman.prototype.setPublicKey = function setPublicKey(key, encoding) { - encoding = encoding || getDefaultEncoding(); key = getArrayBufferOrView(key, 'key', encoding); this[kHandle].setPublicKey(key); return this; @@ -222,7 +208,6 @@ DiffieHellman.prototype.setPublicKey = function setPublicKey(key, encoding) { DiffieHellman.prototype.setPrivateKey = function setPrivateKey(key, encoding) { - encoding = encoding || getDefaultEncoding(); key = getArrayBufferOrView(key, 'key', encoding); this[kHandle].setPrivateKey(key); return this; @@ -251,15 +236,12 @@ ECDH.prototype.generateKeys = function generateKeys(encoding, format) { ECDH.prototype.getPublicKey = function getPublicKey(encoding, format) { const f = getFormat(format); const key = this[kHandle].getPublicKey(f); - encoding = encoding || getDefaultEncoding(); return encode(key, encoding); }; ECDH.convertKey = function convertKey(key, curve, inEnc, outEnc, format) { validateString(curve, 'curve'); - const encoding = inEnc || getDefaultEncoding(); - key = getArrayBufferOrView(key, 'key', encoding); - outEnc = outEnc || encoding; + key = getArrayBufferOrView(key, 'key', inEnc); const f = getFormat(format); const convertedKey = _ECDHConvertKey(key, curve, f); return encode(convertedKey, outEnc);