Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: simplify webcrypto ECDH deriveBits #44946

Merged
merged 1 commit into from
Oct 12, 2022
Merged
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
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 @@ -177,7 +177,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 @@ -256,7 +256,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