All files / src/derive key.js

100% Statements 70/70
100% Branches 36/36
100% Functions 4/4
100% Lines 67/67

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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204                    1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                               252x 252x 252x 251x 2x     249x 249x 249x   249x 446x 376x     375x 3x   372x 1x         371x 371x                   371x   371x 2x                   2x 64x 64x   2x   2x 1x         373x 373x 373x   70x 70x       443x 3x     243x     243x 50x                   193x                       243x   243x   243x 438x 372x                 372x       243x             243x 160x 160x       160x 8x     235x 177x 177x 177x 177x     235x           235x   1x  
/**
 * @file Multi-factor Key Derivation
 * @copyright Multifactor, Inc. 2022–2025
 *
 * @description
 * Derive a multi-factor derived key
 *
 * @author Vivek Nair (https://nair.me) <[email protected]>
 */
 
const Ajv = require('ajv')
const policySchema = require('./policy.json')
const combine = require('../secrets/combine').combine
const recover = require('../secrets/recover').recover
const { hkdfSync } = require('crypto')
const { argon2id } = require('hash-wasm')
const MFKDFDerivedKey = require('../classes/MFKDFDerivedKey')
const { decrypt } = require('../crypt')
const { extract } = require('../integrity')
const crypto = require('crypto')
 
/**
 * Derive a key from multiple factors of input
 *
 * @example
 * // setup 16 byte 2-of-3-factor multi-factor derived key with a password, HOTP code, and UUID recovery code
 * const setup = await mfkdf.setup.key([
 *   await mfkdf.setup.factors.password('password'),
 *   await mfkdf.setup.factors.hotp({ secret: Buffer.from('abcdefghijklmnopqrst') }),
 *   await mfkdf.setup.factors.uuid({ id: 'recovery', uuid: '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d' })
 * ], {threshold: 2})
 *
 * // derive key using 2 of the 3 factors
 * const derive = await mfkdf.derive.key(setup.policy, {
 *   password: mfkdf.derive.factors.password('password'),
 *   hotp: mfkdf.derive.factors.hotp(241063)
 * })
 *
 * setup.key.toString('hex') // -> 34d2…5771
 * derive.key.toString('hex') // -> 34d2…5771
 *
 * @param {Object} policy - The key policy for the key being derived
 * @param {Object.<string, MFKDFFactor>} factors - Factors used to derive this key
 * @param {boolean} [verify=true] - Whether to verify the integrity of the policy after deriving (recommended)
 * @returns {MFKDFDerivedKey} A multi-factor derived key object
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.9.0
 * @async
 * @memberOf derive
 */
async function key (policy, factors, verify = true, stack = false) {
  const ajv = new Ajv()
  const valid = ajv.validate(policySchema, policy)
  if (!valid) throw new TypeError('invalid key policy', ajv.errors)
  if (Object.keys(factors).length < policy.threshold) {
    throw new RangeError('insufficient factors provided to derive key')
  }
 
  const shares = []
  const newFactors = []
  const outputs = {}
 
  for (const factor of policy.factors) {
    if (factors[factor.id] && typeof factors[factor.id] === 'function') {
      const material = await factors[factor.id](factor.params)
      let share
 
      if (material.type === 'persisted') {
        share = material.data
      } else {
        if (material.type !== factor.type) {
          throw new TypeError(
            'wrong factor material function used for this factor type'
          )
        }
 
        const pad = Buffer.from(factor.pad, 'base64')
        const stretched = Buffer.from(
          hkdfSync(
            'sha256',
            material.data,
            Buffer.from(factor.salt, 'base64'),
            'mfkdf2:factor:pad:' + factor.id,
            32
          )
        )
 
        share = decrypt(pad, stretched)
 
        if (factor.hint) {
          const buffer = Buffer.from(
            hkdfSync(
              'sha256',
              stretched,
              Buffer.from(factor.salt, 'base64'),
              'mfkdf2:factor:hint:' + factor.id,
              32
            )
          )
 
          const binaryString = [...buffer]
            .map((byte) => byte.toString(2).padStart(8, '0'))
            .reduce((acc, bits) => acc + bits, '')
 
          const hint = binaryString.slice(-1 * factor.hint.length)
 
          if (hint !== factor.hint) {
            throw new RangeError('hint does not match for factor ' + factor.id)
          }
        }
      }
 
      shares.push(share)
      if (material.output) outputs[factor.id] = await material.output()
      newFactors.push(material.params)
    } else {
      shares.push(null)
      newFactors.push(null)
    }
  }
 
  if (shares.filter((x) => Buffer.isBuffer(x)).length < policy.threshold) {
    throw new RangeError('insufficient factors provided to derive key')
  }
 
  const secret = combine(shares, policy.threshold, policy.factors.length)
 
  let kek
  if (stack) {
    kek = Buffer.from(
      hkdfSync(
        'sha256',
        secret,
        Buffer.from(policy.salt, 'base64'),
        'mfkdf2:stack:' + policy.$id,
        32
      )
    )
  } else {
    kek = Buffer.from(
      await argon2id({
        password: secret,
        salt: Buffer.from(policy.salt, 'base64'),
        hashLength: 32,
        parallelism: 1,
        iterations: 2 + Math.max(0, parseInt(policy.time) || 0),
        memorySize: 19456 + Math.max(0, parseInt(policy.memory) || 0),
        outputType: 'binary'
      })
    )
  }
  const key = decrypt(Buffer.from(policy.key, 'base64'), kek)
 
  const newPolicy = JSON.parse(JSON.stringify(policy))
 
  for (const [index, factor] of newFactors.entries()) {
    if (typeof factor === 'function') {
      const paramsKey = Buffer.from(
        hkdfSync(
          'sha256',
          key,
          Buffer.from(newPolicy.factors[index].salt, 'base64'),
          'mfkdf2:factor:params:' + newPolicy.factors[index].id,
          32
        )
      )
      newPolicy.factors[index].params = await factor({ key: paramsKey })
    }
  }
 
  const integrityKey = hkdfSync(
    'sha256',
    key,
    Buffer.from(policy.salt, 'base64'),
    'mfkdf2:integrity',
    32
  )
  if (verify) {
    const integrityData = await extract(policy)
    const hmac = crypto
      .createHmac('sha256', integrityKey)
      .update(integrityData)
      .digest('base64')
    if (policy.hmac !== hmac) {
      throw new RangeError('key policy integrity check failed')
    }
  }
  if (policy.hmac) {
    const newPolicyData = await extract(newPolicy)
    const newHmac = crypto.createHmac('sha256', integrityKey)
    newHmac.update(newPolicyData)
    newPolicy.hmac = newHmac.digest('base64')
  }
 
  const originalShares = recover(
    shares,
    policy.threshold,
    policy.factors.length
  )
 
  return new MFKDFDerivedKey(newPolicy, key, secret, originalShares, outputs)
}
module.exports.key = key