All files / lib/internal/crypto hkdf.js

100% Statements 182/182
86.2% Branches 25/29
100% Functions 5/5
100% Lines 182/182

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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 18329x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 29x 503x 503x 503x 503x 503x 503x 503x 503x 2x 2x 2x 2x 2x 448x 448x 448x 448x 448x 448x 448x 448x 29x 29x 492x 492x 492x 68x 68x 68x 72x 64x 64x 64x 72x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 54x 54x 492x 29x 462x 462x 462x 462x 462x 462x 462x 462x 462x 462x 462x 462x 462x 462x 432x 432x 462x 462x 462x 462x 29x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 41x 12x 12x 41x 29x 580x 580x 580x 580x 580x 580x 580x 484x 484x 484x 484x 484x 484x 32x 32x 32x 32x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 420x 580x 29x 29x 29x 29x 29x 29x  
'use strict';
 
const {
  FunctionPrototypeCall,
  Promise,
  Uint8Array,
} = primordials;
 
const {
  HKDFJob,
  kCryptoJobAsync,
  kCryptoJobSync,
} = internalBinding('crypto');
 
const {
  validateFunction,
  validateInteger,
  validateString,
  validateUint32,
} = require('internal/validators');
 
const { kMaxLength } = require('buffer');
 
const {
  getArrayBufferOrView,
  normalizeHashName,
  toBuf,
  validateByteSource,
  kKeyObject,
} = require('internal/crypto/util');
 
const {
  createSecretKey,
  isKeyObject,
} = require('internal/crypto/keys');
 
const {
  lazyDOMException,
} = require('internal/util');
 
const {
  isAnyArrayBuffer,
  isArrayBufferView,
} = require('internal/util/types');
 
const {
  codes: {
    ERR_INVALID_ARG_TYPE,
    ERR_OUT_OF_RANGE,
    ERR_MISSING_OPTION,
  },
  hideStackFrames,
} = require('internal/errors');
 
const validateParameters = hideStackFrames((hash, key, salt, info, length) => {
  validateString(hash, 'digest');
  key = prepareKey(key);
  salt = validateByteSource(salt, 'salt');
  info = validateByteSource(info, 'info');
 
  validateInteger(length, 'length', 0, kMaxLength);
 
  if (info.byteLength > 1024) {
    throw ERR_OUT_OF_RANGE(
      'info',
      'must not contain more than 1024 bytes',
      info.byteLength);
  }
 
  return {
    hash,
    key,
    salt,
    info,
    length,
  };
});
 
function prepareKey(key) {
  if (isKeyObject(key))
    return key;
 
  // TODO(@jasnell): createSecretKey should allow using an ArrayBuffer
  if (isAnyArrayBuffer(key))
    return createSecretKey(new Uint8Array(key));
 
  key = toBuf(key);
 
  if (!isArrayBufferView(key)) {
    throw new ERR_INVALID_ARG_TYPE(
      'ikm',
      [
        'string',
        'SecretKeyObject',
        'ArrayBuffer',
        'TypedArray',
        'DataView',
        'Buffer',
      ],
      key);
  }
 
  return createSecretKey(key);
}
 
function hkdf(hash, key, salt, info, length, callback) {
  ({
    hash,
    key,
    salt,
    info,
    length,
  } = validateParameters(hash, key, salt, info, length));
 
  validateFunction(callback, 'callback');
 
  const job = new HKDFJob(kCryptoJobAsync, hash, key, salt, info, length);
 
  job.ondone = (error, bits) => {
    if (error) return FunctionPrototypeCall(callback, job, error);
    FunctionPrototypeCall(callback, job, null, bits);
  };
 
  job.run();
}
 
function hkdfSync(hash, key, salt, info, length) {
  ({
    hash,
    key,
    salt,
    info,
    length,
  } = validateParameters(hash, key, salt, info, length));
 
  const job = new HKDFJob(kCryptoJobSync, hash, key, salt, info, length);
  const { 0: err, 1: bits } = job.run();
  if (err !== undefined)
    throw err;
 
  return bits;
}
 
async function hkdfDeriveBits(algorithm, baseKey, length) {
  validateUint32(length, 'length');
  const { hash } = algorithm;
  const salt = getArrayBufferOrView(algorithm.salt, 'algorithm.salt');
  const info = getArrayBufferOrView(algorithm.info, 'algorithm.info');
  if (hash === undefined)
    throw new ERR_MISSING_OPTION('algorithm.hash');
 
  let byteLength = 512 / 8;
  if (length !== undefined) {
    if (length === 0)
      throw lazyDOMException('length cannot be zero', 'OperationError');
    if (length % 8) {
      throw lazyDOMException(
        'length must be a multiple of 8',
        'OperationError');
    }
    byteLength = length / 8;
  }
 
  return new Promise((resolve, reject) => {
    hkdf(
      normalizeHashName(hash.name),
      baseKey[kKeyObject],
      salt,
      info,
      byteLength,
      (err, bits) => {
        if (err) return reject(err);
        resolve(bits);
      });
  });
}
 
module.exports = {
  hkdf,
  hkdfSync,
  hkdfDeriveBits,
};