Skip to content

Commit eeec3eb

Browse 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

2 files changed

Lines changed: 11 additions & 31 deletions

File tree

‎lib/internal/crypto/diffiehellman.js‎

Lines 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
returnstatelessDH(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-
functionderiveBitsECDH(name,publicKey,privateKey,callback){
323-
validateString(name,'name');
324-
validateObject(publicKey,'publicKey');
325-
validateObject(privateKey,'privateKey');
326-
validateFunction(callback,'callback');
327-
constjob=newECDHBitsJob(kCryptoJobAsync,name,publicKey,privateKey);
328-
job.ondone=(error,bits)=>{
329-
if(error)returnFunctionPrototypeCall(callback,job,error);
330-
FunctionPrototypeCall(callback,job,null,bits);
331-
};
332-
job.run();
333-
}
334-
335-
asyncfunctionasyncDeriveBitsECDH(algorithm,baseKey,length){
320+
asyncfunctionecdhDeriveBits(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
throwlazyDOMException('Named curve mismatch','InvalidAccessError');
373358
}
374359

375-
constbits=awaitnewPromise((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)returnreject(err);
381-
resolve(bits);
382-
});
383-
});
360+
constbits=awaitjobPromise(newECDHBitsJob(
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‎

Lines 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
returnlazyRequire('internal/crypto/diffiehellman')
180-
.asyncDeriveBitsECDH(algorithm,baseKey,length);
180+
.ecdhDeriveBits(algorithm,baseKey,length);
181181
case'HKDF':
182182
returnlazyRequire('internal/crypto/hkdf')
183183
.hkdfDeriveBits(algorithm,baseKey,length);
@@ -256,7 +256,7 @@ async function deriveKey(
256256
// Fall through
257257
case'ECDH':
258258
bits=awaitlazyRequire('internal/crypto/diffiehellman')
259-
.asyncDeriveBitsECDH(algorithm,baseKey,length);
259+
.ecdhDeriveBits(algorithm,baseKey,length);
260260
break;
261261
case'HKDF':
262262
bits=awaitlazyRequire('internal/crypto/hkdf')

0 commit comments

Comments
 (0)