GeezerVison is showing github.com — crypto: simplify webcrypto ECDH deriveBits · nodejs/node@631421e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/diffiehellman.js +9-29Lines changed: 9 additions & 29 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 |
| 3 | 3 | const { | 4 | 4 | ArrayBufferPrototypeSlice, | 5 | | - FunctionPrototypeCall, | 6 | 5 | MathCeil, | 7 | 6 | ObjectDefineProperty, | 8 | | - Promise, | 9 | 7 | SafeSet, | 10 | 8 | } = primordials; | 11 | 9 |
| @@ -33,7 +31,6 @@ const { | 33 | 31 | } = require('internal/errors'); | 34 | 32 |
| 35 | 33 | const { | 36 | | - validateFunction, | 37 | 34 | validateInt32, | 38 | 35 | validateObject, | 39 | 36 | validateString, | @@ -57,6 +54,7 @@ const { | 57 | 54 | const { | 58 | 55 | getArrayBufferOrView, | 59 | 56 | getDefaultEncoding, | | 57 | + jobPromise, | 60 | 58 | toBuf, | 61 | 59 | kHandle, | 62 | 60 | kKeyObject, | @@ -317,22 +315,9 @@ function diffieHellman(options) { | 317 | 315 | return statelessDH(privateKey[kHandle], publicKey[kHandle]); | 318 | 316 | } | 319 | 317 |
| 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 | 321 | 319 | // 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) { | 336 | 321 | const { 'public': key } = algorithm; | 337 | 322 |
| 338 | 323 | // Null means that we're not asking for a specific number of bits, just | @@ -372,15 +357,11 @@ async function asyncDeriveBitsECDH(algorithm, baseKey, length) { | 372 | 357 | throw lazyDOMException('Named curve mismatch', 'InvalidAccessError'); | 373 | 358 | } | 374 | 359 |
| 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])); | 384 | 365 |
| 385 | 366 | // If a length is not specified, return the full derived secret | 386 | 367 | if (length === null) | @@ -407,6 +388,5 @@ module.exports = { | 407 | 388 | DiffieHellmanGroup, | 408 | 389 | ECDH, | 409 | 390 | diffieHellman, | 410 | | - deriveBitsECDH, | 411 | | - asyncDeriveBitsECDH, | | 391 | + ecdhDeriveBits, | 412 | 392 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/webcrypto.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -172,7 +172,7 @@ async function deriveBits(algorithm, baseKey, length) { | 172 | 172 | // Fall through | 173 | 173 | case 'ECDH': | 174 | 174 | return lazyRequire('internal/crypto/diffiehellman') | 175 | | - .asyncDeriveBitsECDH(algorithm, baseKey, length); | | 175 | + .ecdhDeriveBits(algorithm, baseKey, length); | 176 | 176 | case 'HKDF': | 177 | 177 | return lazyRequire('internal/crypto/hkdf') | 178 | 178 | .hkdfDeriveBits(algorithm, baseKey, length); | @@ -251,7 +251,7 @@ async function deriveKey( | 251 | 251 | // Fall through | 252 | 252 | case 'ECDH': | 253 | 253 | bits = await lazyRequire('internal/crypto/diffiehellman') | 254 | | - .asyncDeriveBitsECDH(algorithm, baseKey, length); | | 254 | + .ecdhDeriveBits(algorithm, baseKey, length); | 255 | 255 | break; | 256 | 256 | case 'HKDF': | 257 | 257 | bits = await lazyRequire('internal/crypto/hkdf') | |
|