export function buildAlgorithmForKDF()

in modules/material-management-browser/src/material_helpers.ts [322:375]


export function buildAlgorithmForKDF(
  suite: SupportedAlgorithmSuites,
  nonce: Uint8Array
) {
  const { kdf, kdfHash, commitmentLength, saltLengthBytes } = suite

  /* Precondition: Valid HKDF values must exist for browsers. */
  needs(
    kdf === 'HKDF' && kdfHash && nonce instanceof Uint8Array,
    'Invalid HKDF values.'
  )

  if (suite.commitment === 'NONE') {
    /* Precondition: The message ID length must match the specification. */
    needs(
      nonce.byteLength === MessageIdLength.V1,
      'Message id length does not match specification.'
    )
    const info = kdfInfo(suite.id, nonce)
    // https://developer.mozilla.org/en-US/docs/Web/API/HkdfParams
    return {
      name: kdf,
      hash: { name: kdfHash },
      info,
      salt: new Uint8Array(),
    }
  }

  /* Precondition UNTESTED: The suite must be well structured. */
  needs(
    suite.commitment === 'KEY' && commitmentLength && saltLengthBytes,
    'Malformed suite data.'
  )

  /* Precondition: The message id length must match the algorithm suite.
   * I am using the message id here,
   * but I must have enough entropy!
   */
  needs(
    nonce.byteLength === saltLengthBytes,
    'Message id length does not match specification.'
  )

  const { keyLabel: info } = kdfCommitKeyInfo(suite)
  const salt = nonce

  // https://developer.mozilla.org/en-US/docs/Web/API/HkdfParams
  return {
    name: kdf,
    hash: { name: kdfHash },
    info,
    salt,
  }
}