Skip to content

Commit

Permalink
crypto: remove default encoding from DiffieHellman
Browse files Browse the repository at this point in the history
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: nodejs#47182
PR-URL: nodejs#49169
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
  • Loading branch information
tniessen committed Aug 16, 2023
1 parent 8ed4397 commit 7215176
Showing 1 changed file with 1 addition and 19 deletions.
20 changes: 1 addition & 19 deletions lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const {

const {
getArrayBufferOrView,
getDefaultEncoding,
jobPromise,
toBuf,
kHandle,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -148,7 +143,6 @@ DiffieHellmanGroup.prototype.generateKeys =

function dhGenerateKeys(encoding) {
const keys = this[kHandle].generateKeys();
encoding = encoding || getDefaultEncoding();
return encode(keys, encoding);
}

Expand All @@ -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')
Expand All @@ -175,7 +166,6 @@ DiffieHellmanGroup.prototype.getPrime =

function dhGetPrime(encoding) {
const prime = this[kHandle].getPrime();
encoding = encoding || getDefaultEncoding();
return encode(prime, encoding);
}

Expand All @@ -186,7 +176,6 @@ DiffieHellmanGroup.prototype.getGenerator =

function dhGetGenerator(encoding) {
const generator = this[kHandle].getGenerator();
encoding = encoding || getDefaultEncoding();
return encode(generator, encoding);
}

Expand All @@ -197,7 +186,6 @@ DiffieHellmanGroup.prototype.getPublicKey =

function dhGetPublicKey(encoding) {
const key = this[kHandle].getPublicKey();
encoding = encoding || getDefaultEncoding();
return encode(key, encoding);
}

Expand All @@ -208,21 +196,18 @@ 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;
};


DiffieHellman.prototype.setPrivateKey = function setPrivateKey(key, encoding) {
encoding = encoding || getDefaultEncoding();
key = getArrayBufferOrView(key, 'key', encoding);
this[kHandle].setPrivateKey(key);
return this;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7215176

Please sign in to comment.