mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
crypto: simplify webcrypto ECDH deriveBits
PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
1 parent
2882e6042e
commit
631421e8d0
2 files changed
+11
-31
No files matched your search
@@ -2,10 +2,8 @@
|
||||
|
||||
const {
|
||||
ArrayBufferPrototypeSlice,
|
||||
FunctionPrototypeCall,
|
||||
MathCeil,
|
||||
ObjectDefineProperty,
|
||||
Promise,
|
||||
SafeSet,
|
||||
} = primordials;
|
||||
|
||||
@@ -33,7 +31,6 @@ const {
|
||||
} = require('internal/errors');
|
||||
|
||||
const {
|
||||
validateFunction,
|
||||
validateInt32,
|
||||
validateObject,
|
||||
validateString,
|
||||
@@ -57,6 +54,7 @@ const {
|
||||
const {
|
||||
getArrayBufferOrView,
|
||||
getDefaultEncoding,
|
||||
jobPromise,
|
||||
toBuf,
|
||||
kHandle,
|
||||
kKeyObject,
|
||||
@@ -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
|
||||
@@ -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)
|
||||
@@ -407,6 +388,5 @@ module.exports = {
|
||||
DiffieHellmanGroup,
|
||||
ECDH,
|
||||
diffieHellman,
|
||||
deriveBitsECDH,
|
||||
asyncDeriveBitsECDH,
|
||||
ecdhDeriveBits,
|
||||
};
|
||||
@@ -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);
|
||||
@@ -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')
|
||||
|
||||
Reference in new issue
Block a user