hkdf.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { hash as assertHash, number as assertNumber } from './_assert.js';
  2. import { CHash, Input, toBytes } from './utils.js';
  3. import { hmac } from './hmac.js';
  4. // HKDF (RFC 5869)
  5. // https://soatok.blog/2021/11/17/understanding-hkdf/
  6. /**
  7. * HKDF-Extract(IKM, salt) -> PRK
  8. * Arguments position differs from spec (IKM is first one, since it is not optional)
  9. * @param hash
  10. * @param ikm
  11. * @param salt
  12. * @returns
  13. */
  14. export function extract(hash: CHash, ikm: Input, salt?: Input) {
  15. assertHash(hash);
  16. // NOTE: some libraries treat zero-length array as 'not provided';
  17. // we don't, since we have undefined as 'not provided'
  18. // https://github.com/RustCrypto/KDFs/issues/15
  19. if (salt === undefined) salt = new Uint8Array(hash.outputLen); // if not provided, it is set to a string of HashLen zeros
  20. return hmac(hash, toBytes(salt), toBytes(ikm));
  21. }
  22. // HKDF-Expand(PRK, info, L) -> OKM
  23. const HKDF_COUNTER = /* @__PURE__ */ new Uint8Array([0]);
  24. const EMPTY_BUFFER = /* @__PURE__ */ new Uint8Array();
  25. /**
  26. * HKDF-expand from the spec.
  27. * @param prk - a pseudorandom key of at least HashLen octets (usually, the output from the extract step)
  28. * @param info - optional context and application specific information (can be a zero-length string)
  29. * @param length - length of output keying material in octets
  30. */
  31. export function expand(hash: CHash, prk: Input, info?: Input, length: number = 32) {
  32. assertHash(hash);
  33. assertNumber(length);
  34. if (length > 255 * hash.outputLen) throw new Error('Length should be <= 255*HashLen');
  35. const blocks = Math.ceil(length / hash.outputLen);
  36. if (info === undefined) info = EMPTY_BUFFER;
  37. // first L(ength) octets of T
  38. const okm = new Uint8Array(blocks * hash.outputLen);
  39. // Re-use HMAC instance between blocks
  40. const HMAC = hmac.create(hash, prk);
  41. const HMACTmp = HMAC._cloneInto();
  42. const T = new Uint8Array(HMAC.outputLen);
  43. for (let counter = 0; counter < blocks; counter++) {
  44. HKDF_COUNTER[0] = counter + 1;
  45. // T(0) = empty string (zero length)
  46. // T(N) = HMAC-Hash(PRK, T(N-1) | info | N)
  47. HMACTmp.update(counter === 0 ? EMPTY_BUFFER : T)
  48. .update(info)
  49. .update(HKDF_COUNTER)
  50. .digestInto(T);
  51. okm.set(T, hash.outputLen * counter);
  52. HMAC._cloneInto(HMACTmp);
  53. }
  54. HMAC.destroy();
  55. HMACTmp.destroy();
  56. T.fill(0);
  57. HKDF_COUNTER.fill(0);
  58. return okm.slice(0, length);
  59. }
  60. /**
  61. * HKDF (RFC 5869): extract + expand in one step.
  62. * @param hash - hash function that would be used (e.g. sha256)
  63. * @param ikm - input keying material, the initial key
  64. * @param salt - optional salt value (a non-secret random value)
  65. * @param info - optional context and application specific information
  66. * @param length - length of output keying material in octets
  67. */
  68. export const hkdf = (
  69. hash: CHash,
  70. ikm: Input,
  71. salt: Input | undefined,
  72. info: Input | undefined,
  73. length: number
  74. ) => expand(hash, extract(hash, ikm, salt), info, length);