All files / lib/internal/crypto pbkdf2.js

100% Statements 146/146
85.18% Branches 23/27
100% Functions 5/5
100% Lines 146/146

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 14723x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 1898x 1898x 1x 1x 1x 1898x 1898x 1898x 1898x 1898x 1898x 1898x 1898x 1898x 1898x 1898x 1898x 1898x 1898x 1898x 1898x 1871x 1871x 1871x 1871x 1871x 5x 1898x 1898x 1898x 1898x 23x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 55x 25x 25x 25x 55x 55x 23x 1953x 1953x 1953x 1953x 1953x 1953x 1953x 1953x 1953x 1953x 1953x 1953x 23x 2777x 2777x 2777x 2777x 2777x 2777x 2777x 2777x 2777x 612x 612x 2165x 2165x 2165x 2165x 2165x 2165x 2165x 2165x 2165x 2057x 2165x 1949x 2165x 108x 108x 108x 108x 1841x 1841x 1841x 1841x 1841x 1841x 1841x 1841x 1841x 2777x 23x 23x 23x 23x 23x 23x  
'use strict';
 
const {
  FunctionPrototypeCall,
  Promise,
} = primordials;
 
const { Buffer } = require('buffer');
 
const {
  PBKDF2Job,
  kCryptoJobAsync,
  kCryptoJobSync,
} = internalBinding('crypto');
 
const {
  validateFunction,
  validateInt32,
  validateInteger,
  validateString,
  validateUint32,
} = require('internal/validators');
 
const { ERR_MISSING_OPTION } = require('internal/errors').codes;
 
const {
  getArrayBufferOrView,
  getDefaultEncoding,
  normalizeHashName,
  kKeyObject,
} = require('internal/crypto/util');
 
const {
  lazyDOMException,
} = require('internal/util');
 
function pbkdf2(password, salt, iterations, keylen, digest, callback) {
  if (typeof digest === 'function') {
    callback = digest;
    digest = undefined;
  }
 
  ({ password, salt, iterations, keylen, digest } =
    check(password, salt, iterations, keylen, digest));
 
  validateFunction(callback, 'callback');
 
  const job = new PBKDF2Job(
    kCryptoJobAsync,
    password,
    salt,
    iterations,
    keylen,
    digest);
 
  const encoding = getDefaultEncoding();
  job.ondone = (err, result) => {
    if (err !== undefined)
      return FunctionPrototypeCall(callback, job, err);
    const buf = Buffer.from(result);
    if (encoding === 'buffer')
      return FunctionPrototypeCall(callback, job, null, buf);
    FunctionPrototypeCall(callback, job, null, buf.toString(encoding));
  };
 
  job.run();
}
 
function pbkdf2Sync(password, salt, iterations, keylen, digest) {
  ({ password, salt, iterations, keylen, digest } =
    check(password, salt, iterations, keylen, digest));
 
  const job = new PBKDF2Job(
    kCryptoJobSync,
    password,
    salt,
    iterations,
    keylen,
    digest);
 
  const { 0: err, 1: result } = job.run();
  if (err !== undefined)
    throw err;
 
  const buf = Buffer.from(result);
  const encoding = getDefaultEncoding();
  return encoding === 'buffer' ? buf : buf.toString(encoding);
}
 
function check(password, salt, iterations, keylen, digest) {
  validateString(digest, 'digest');
 
  password = getArrayBufferOrView(password, 'password');
  salt = getArrayBufferOrView(salt, 'salt');
  // OpenSSL uses a signed int to represent these values, so we are restricted
  // to the 31-bit range here (which is plenty).
  validateInt32(iterations, 'iterations', 1);
  validateInt32(keylen, 'keylen', 0);
 
  return { password, salt, iterations, keylen, digest };
}
 
async function pbkdf2DeriveBits(algorithm, baseKey, length) {
  const { iterations } = algorithm;
  let { hash } = algorithm;
  const salt = getArrayBufferOrView(algorithm.salt, 'algorithm.salt');
  if (hash === undefined)
    throw new ERR_MISSING_OPTION('algorithm.hash');
  validateInteger(iterations, 'algorithm.iterations');
  if (iterations === 0)
    throw lazyDOMException(
      'iterations cannot be zero',
      'OperationError');
 
  hash = normalizeHashName(hash.name);
 
  const raw = baseKey[kKeyObject].export();
 
  let byteLength = 64;  // the default
  if (length !== undefined) {
    if (length === 0)
      throw lazyDOMException('length cannot be zero', 'OperationError');
    if (length === null)
      throw lazyDOMException('length cannot be null', 'OperationError');
    validateUint32(length, 'length');
    if (length % 8) {
      throw lazyDOMException(
        'length must be a multiple of 8',
        'OperationError');
    }
    byteLength = length / 8;
  }
 
  return new Promise((resolve, reject) => {
    pbkdf2(raw, salt, iterations, byteLength, hash, (err, result) => {
      if (err) return reject(err);
      resolve(result.buffer);
    });
  });
}
 
module.exports = {
  pbkdf2,
  pbkdf2Sync,
  pbkdf2DeriveBits,
};