Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit eeec3eb

Browse filesBrowse files
authored
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 e23c256 commit eeec3eb
Copy full SHA for eeec3eb

File tree

Expand file treeCollapse file tree

2 files changed

+11
-31
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+11
-31
lines changed
Open diff view settings
Collapse file

‎lib/internal/crypto/diffiehellman.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/diffiehellman.js
+9-29Lines changed: 9 additions & 29 deletions
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
};
Collapse file

‎lib/internal/crypto/webcrypto.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/webcrypto.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ async function deriveBits(algorithm, baseKey, length) {
177177
// Fall through
178178
case 'ECDH':
179179
return lazyRequire('internal/crypto/diffiehellman')
180-
.asyncDeriveBitsECDH(algorithm, baseKey, length);
180+
.ecdhDeriveBits(algorithm, baseKey, length);
181181
case 'HKDF':
182182
return lazyRequire('internal/crypto/hkdf')
183183
.hkdfDeriveBits(algorithm, baseKey, length);
@@ -256,7 +256,7 @@ async function deriveKey(
256256
// Fall through
257257
case 'ECDH':
258258
bits = await lazyRequire('internal/crypto/diffiehellman')
259-
.asyncDeriveBitsECDH(algorithm, baseKey, length);
259+
.ecdhDeriveBits(algorithm, baseKey, length);
260260
break;
261261
case 'HKDF':
262262
bits = await lazyRequire('internal/crypto/hkdf')

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.