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

Function deriveKeyImpl

lib/internal/crypto/webcrypto.js:347–421  ·  view source on GitHub ↗
(
  algorithm,
  baseKey,
  derivedKeyType,
  extractable,
  keyUsages)

Source from the content-addressed store, hash-verified

345}
346
347function deriveKeyImpl(
348 algorithm,
349 baseKey,
350 derivedKeyType,
351 extractable,
352 keyUsages) {
353 const prefix = prepareSubtleMethod(this, 'deriveKey', arguments.length, 5);
354 let i = 0;
355 algorithm = convertSubtleArgument(
356 prefix, 'AlgorithmIdentifier', algorithm, i++);
357 baseKey = convertSubtleArgument(prefix, 'CryptoKey', baseKey, i++);
358 derivedKeyType = convertSubtleArgument(
359 prefix, 'AlgorithmIdentifier', derivedKeyType, i++);
360 extractable = convertSubtleArgument(prefix, 'boolean', extractable, i++);
361 const usages = convertSubtleArgument(
362 prefix, 'sequence<KeyUsage>', keyUsages, i++);
363
364 const normalizedAlgorithm = normalizeAlgorithm(algorithm, 'deriveBits');
365 const normalizedDerivedKeyAlgorithmImport =
366 normalizeAlgorithm(derivedKeyType, 'importKey');
367 const normalizedDerivedKeyAlgorithmLength =
368 normalizeAlgorithm(derivedKeyType, 'get key length');
369 if (!hasCryptoKeyUsage(baseKey, 'deriveKey')) {
370 throw lazyDOMException(
371 'baseKey does not have deriveKey usage',
372 'InvalidAccessError');
373 }
374 if (getCryptoKeyAlgorithm(baseKey).name !== normalizedAlgorithm.name)
375 throw lazyDOMException('Key algorithm mismatch', 'InvalidAccessError');
376
377 const length = getKeyLength(normalizedDerivedKeyAlgorithmLength);
378 let secret;
379 switch (normalizedAlgorithm.name) {
380 case 'X25519':
381 // Fall through
382 case 'X448':
383 // Fall through
384 case 'ECDH':
385 secret = require('internal/crypto/diffiehellman')
386 .ecdhDeriveBits(normalizedAlgorithm, baseKey, length);
387 break;
388 case 'HKDF':
389 secret = require('internal/crypto/hkdf')
390 .hkdfDeriveBits(normalizedAlgorithm, baseKey, length);
391 break;
392 case 'PBKDF2':
393 secret = require('internal/crypto/pbkdf2')
394 .pbkdf2DeriveBits(normalizedAlgorithm, baseKey, length);
395 break;
396 case 'Argon2d':
397 // Fall through
398 case 'Argon2i':
399 // Fall through
400 case 'Argon2id':
401 secret = require('internal/crypto/argon2')
402 .argon2DeriveBits(normalizedAlgorithm, baseKey, length);
403 break;
404 /* c8 ignore start */

Callers

nothing calls this directly

Calls 10

prepareSubtleMethodFunction · 0.85
convertSubtleArgumentFunction · 0.85
hasCryptoKeyUsageFunction · 0.85
lazyDOMExceptionFunction · 0.85
getCryptoKeyAlgorithmFunction · 0.85
getKeyLengthFunction · 0.85
jobPromiseThenFunction · 0.85
normalizeAlgorithmFunction · 0.70
requireFunction · 0.50
failMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…