Skip to content

Commit

Permalink
crypto: simplify webcrypto ECDH deriveBits
Browse files Browse the repository at this point in the history
PR-URL: #44946
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
  • Loading branch information
panva authored and danielleadams committed Jan 3, 2023
1 parent 2882e60 commit 631421e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 31 deletions.
38 changes: 9 additions & 29 deletions lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

const {
ArrayBufferPrototypeSlice,
FunctionPrototypeCall,
MathCeil,
ObjectDefineProperty,
Promise,
SafeSet,
} = primordials;

Expand Down Expand Up @@ -33,7 +31,6 @@ const {
} = require('internal/errors');

const {
validateFunction,
validateInt32,
validateObject,
validateString,
Expand All @@ -57,6 +54,7 @@ const {
const {
getArrayBufferOrView,
getDefaultEncoding,
jobPromise,
toBuf,
kHandle,
kKeyObject,
Expand Down Expand Up @@ -317,22 +315,9 @@ function diffieHellman(options) {
return statelessDH(privateKey[kHandle], publicKey[kHandle]);
}

// The deriveBitsECDH function is part of the Web Crypto API and serves both
// The ecdhDeriveBits function is part of the Web Crypto API and serves both
// deriveKeys and deriveBits functions.
function deriveBitsECDH(name, publicKey, privateKey, callback) {
validateString(name, 'name');
validateObject(publicKey, 'publicKey');
validateObject(privateKey, 'privateKey');
validateFunction(callback, 'callback');
const job = new ECDHBitsJob(kCryptoJobAsync, name, publicKey, privateKey);
job.ondone = (error, bits) => {
if (error) return FunctionPrototypeCall(callback, job, error);
FunctionPrototypeCall(callback, job, null, bits);
};
job.run();
}

async function asyncDeriveBitsECDH(algorithm, baseKey, length) {
async function ecdhDeriveBits(algorithm, baseKey, length) {
const { 'public': key } = algorithm;

// Null means that we're not asking for a specific number of bits, just
Expand Down Expand Up @@ -372,15 +357,11 @@ async function asyncDeriveBitsECDH(algorithm, baseKey, length) {
throw lazyDOMException('Named curve mismatch', 'InvalidAccessError');
}

const bits = await new Promise((resolve, reject) => {
deriveBitsECDH(
key.algorithm.name === 'ECDH' ? baseKey.algorithm.namedCurve : baseKey.algorithm.name,
key[kKeyObject][kHandle],
baseKey[kKeyObject][kHandle], (err, bits) => {
if (err) return reject(err);
resolve(bits);
});
});
const bits = await jobPromise(new ECDHBitsJob(
kCryptoJobAsync,
key.algorithm.name === 'ECDH' ? baseKey.algorithm.namedCurve : baseKey.algorithm.name,
key[kKeyObject][kHandle],
baseKey[kKeyObject][kHandle]));

// If a length is not specified, return the full derived secret
if (length === null)
Expand All @@ -407,6 +388,5 @@ module.exports = {
DiffieHellmanGroup,
ECDH,
diffieHellman,
deriveBitsECDH,
asyncDeriveBitsECDH,
ecdhDeriveBits,
};
4 changes: 2 additions & 2 deletions lib/internal/crypto/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ async function deriveBits(algorithm, baseKey, length) {
// Fall through
case 'ECDH':
return lazyRequire('internal/crypto/diffiehellman')
.asyncDeriveBitsECDH(algorithm, baseKey, length);
.ecdhDeriveBits(algorithm, baseKey, length);
case 'HKDF':
return lazyRequire('internal/crypto/hkdf')
.hkdfDeriveBits(algorithm, baseKey, length);
Expand Down Expand Up @@ -251,7 +251,7 @@ async function deriveKey(
// Fall through
case 'ECDH':
bits = await lazyRequire('internal/crypto/diffiehellman')
.asyncDeriveBitsECDH(algorithm, baseKey, length);
.ecdhDeriveBits(algorithm, baseKey, length);
break;
case 'HKDF':
bits = await lazyRequire('internal/crypto/hkdf')
Expand Down

0 comments on commit 631421e

Please sign in to comment.