MCPcopy Index your code
hub / github.com/nodejs/node / ecdhDeriveBits

Function ecdhDeriveBits

lib/internal/crypto/diffiehellman.js:374–430  ·  view source on GitHub ↗
(algorithm, baseKey, length)

Source from the content-addressed store, hash-verified

372// The ecdhDeriveBits function is part of the Web Crypto API and serves both
373// deriveKeys and deriveBits functions.
374function ecdhDeriveBits(algorithm, baseKey, length) {
375 const { 'public': key } = algorithm;
376
377 if (getCryptoKeyType(baseKey) !== 'private') {
378 throw lazyDOMException(
379 'baseKey must be a private key', 'InvalidAccessError');
380 }
381
382 const keyAlgorithm = getCryptoKeyAlgorithm(key);
383 const baseKeyAlgorithm = getCryptoKeyAlgorithm(baseKey);
384 if (keyAlgorithm.name !== baseKeyAlgorithm.name) {
385 throw lazyDOMException(
386 'The public and private keys must be of the same type',
387 'InvalidAccessError');
388 }
389
390 if (
391 keyAlgorithm.name === 'ECDH' &&
392 keyAlgorithm.namedCurve !== baseKeyAlgorithm.namedCurve
393 ) {
394 throw lazyDOMException('Named curve mismatch', 'InvalidAccessError');
395 }
396
397 const bits = jobPromise(() => new DHBitsJob(
398 kCryptoJobWebCrypto,
399 getCryptoKeyHandle(key),
400 undefined,
401 undefined,
402 undefined,
403 undefined,
404 getCryptoKeyHandle(baseKey),
405 undefined,
406 undefined,
407 undefined,
408 undefined));
409
410 // If a length is not specified, return the full derived secret
411 if (length === null)
412 return bits;
413
414 return jobPromiseThen(bits, (bits) => {
415 const sliceLength = numBitsToBytes(length);
416
417 const { byteLength } = bits;
418 // If the length is larger than the derived secret, throw.
419 if (byteLength < sliceLength)
420 throw lazyDOMException('derived bit length is too small', 'OperationError');
421
422 if (length % 8 === 0) {
423 if (byteLength === sliceLength)
424 return bits;
425 return ArrayBufferPrototypeSlice(bits, 0, sliceLength);
426 }
427
428 return TypedArrayPrototypeGetBuffer(truncateToBitLength(length, bits));
429 });
430}
431

Callers

nothing calls this directly

Calls 8

getCryptoKeyTypeFunction · 0.85
lazyDOMExceptionFunction · 0.85
getCryptoKeyAlgorithmFunction · 0.85
jobPromiseFunction · 0.85
getCryptoKeyHandleFunction · 0.85
jobPromiseThenFunction · 0.85
numBitsToBytesFunction · 0.85
truncateToBitLengthFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…