Skip to content

Commit 631421e

Browse files
panvadanielleadams
authored andcommitted
crypto: simplify webcrypto ECDH deriveBits
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>
1 parent 2882e60 commit 631421e

File tree

2 files changed

+11
-31
lines changed

2 files changed

+11
-31
lines changed

lib/internal/crypto/diffiehellman.js

+9-29
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
const {
44
ArrayBufferPrototypeSlice,
5-
FunctionPrototypeCall,
65
MathCeil,
76
ObjectDefineProperty,
8-
Promise,
97
SafeSet,
108
} = primordials;
119

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

3533
const {
36-
validateFunction,
3734
validateInt32,
3835
validateObject,
3936
validateString,
@@ -57,6 +54,7 @@ const {
5754
const {
5855
getArrayBufferOrView,
5956
getDefaultEncoding,
57+
jobPromise,
6058
toBuf,
6159
kHandle,
6260
kKeyObject,
@@ -317,22 +315,9 @@ function diffieHellman(options) {
317315
return statelessDH(privateKey[kHandle], publicKey[kHandle]);
318316
}
319317

320-
// The deriveBitsECDH function is part of the Web Crypto API and serves both
318+
// The ecdhDeriveBits function is part of the Web Crypto API and serves both
321319
// deriveKeys and deriveBits functions.
322-
function deriveBitsECDH(name, publicKey, privateKey, callback) {
323-
validateString(name, 'name');
324-
validateObject(publicKey, 'publicKey');
325-
validateObject(privateKey, 'privateKey');
326-
validateFunction(callback, 'callback');
327-
const job = new ECDHBitsJob(kCryptoJobAsync, name, publicKey, privateKey);
328-
job.ondone = (error, bits) => {
329-
if (error) return FunctionPrototypeCall(callback, job, error);
330-
FunctionPrototypeCall(callback, job, null, bits);
331-
};
332-
job.run();
333-
}
334-
335-
async function asyncDeriveBitsECDH(algorithm, baseKey, length) {
320+
async function ecdhDeriveBits(algorithm, baseKey, length) {
336321
const { 'public': key } = algorithm;
337322

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

375-
const bits = await new Promise((resolve, reject) => {
376-
deriveBitsECDH(
377-
key.algorithm.name === 'ECDH' ? baseKey.algorithm.namedCurve : baseKey.algorithm.name,
378-
key[kKeyObject][kHandle],
379-
baseKey[kKeyObject][kHandle], (err, bits) => {
380-
if (err) return reject(err);
381-
resolve(bits);
382-
});
383-
});
360+
const bits = await jobPromise(new ECDHBitsJob(
361+
kCryptoJobAsync,
362+
key.algorithm.name === 'ECDH' ? baseKey.algorithm.namedCurve : baseKey.algorithm.name,
363+
key[kKeyObject][kHandle],
364+
baseKey[kKeyObject][kHandle]));
384365

385366
// If a length is not specified, return the full derived secret
386367
if (length === null)
@@ -407,6 +388,5 @@ module.exports = {
407388
DiffieHellmanGroup,
408389
ECDH,
409390
diffieHellman,
410-
deriveBitsECDH,
411-
asyncDeriveBitsECDH,
391+
ecdhDeriveBits,
412392
};

lib/internal/crypto/webcrypto.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ async function deriveBits(algorithm, baseKey, length) {
172172
// Fall through
173173
case 'ECDH':
174174
return lazyRequire('internal/crypto/diffiehellman')
175-
.asyncDeriveBitsECDH(algorithm, baseKey, length);
175+
.ecdhDeriveBits(algorithm, baseKey, length);
176176
case 'HKDF':
177177
return lazyRequire('internal/crypto/hkdf')
178178
.hkdfDeriveBits(algorithm, baseKey, length);
@@ -251,7 +251,7 @@ async function deriveKey(
251251
// Fall through
252252
case 'ECDH':
253253
bits = await lazyRequire('internal/crypto/diffiehellman')
254-
.asyncDeriveBitsECDH(algorithm, baseKey, length);
254+
.ecdhDeriveBits(algorithm, baseKey, length);
255255
break;
256256
case 'HKDF':
257257
bits = await lazyRequire('internal/crypto/hkdf')

0 commit comments

Comments
 (0)