hkdf.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { hash as assertHash, number as assertNumber } from './_assert.js';
  2. import { 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, ikm, salt) {
  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)
  20. salt = new Uint8Array(hash.outputLen); // if not provided, it is set to a string of HashLen zeros
  21. return hmac(hash, toBytes(salt), toBytes(ikm));
  22. }
  23. // HKDF-Expand(PRK, info, L) -> OKM
  24. const HKDF_COUNTER = /* @__PURE__ */ new Uint8Array([0]);
  25. const EMPTY_BUFFER = /* @__PURE__ */ new Uint8Array();
  26. /**
  27. * HKDF-expand from the spec.
  28. * @param prk - a pseudorandom key of at least HashLen octets (usually, the output from the extract step)
  29. * @param info - optional context and application specific information (can be a zero-length string)
  30. * @param length - length of output keying material in octets
  31. */
  32. export function expand(hash, prk, info, length = 32) {
  33. assertHash(hash);
  34. assertNumber(length);
  35. if (length > 255 * hash.outputLen)
  36. throw new Error('Length should be <= 255*HashLen');
  37. const blocks = Math.ceil(length / hash.outputLen);
  38. if (info === undefined)
  39. info = EMPTY_BUFFER;
  40. // first L(ength) octets of T
  41. const okm = new Uint8Array(blocks * hash.outputLen);
  42. // Re-use HMAC instance between blocks
  43. const HMAC = hmac.create(hash, prk);
  44. const HMACTmp = HMAC._cloneInto();
  45. const T = new Uint8Array(HMAC.outputLen);
  46. for (let counter = 0; counter < blocks; counter++) {
  47. HKDF_COUNTER[0] = counter + 1;
  48. // T(0) = empty string (zero length)
  49. // T(N) = HMAC-Hash(PRK, T(N-1) | info | N)
  50. HMACTmp.update(counter === 0 ? EMPTY_BUFFER : T)
  51. .update(info)
  52. .update(HKDF_COUNTER)
  53. .digestInto(T);
  54. okm.set(T, hash.outputLen * counter);
  55. HMAC._cloneInto(HMACTmp);
  56. }
  57. HMAC.destroy();
  58. HMACTmp.destroy();
  59. T.fill(0);
  60. HKDF_COUNTER.fill(0);
  61. return okm.slice(0, length);
  62. }
  63. /**
  64. * HKDF (RFC 5869): extract + expand in one step.
  65. * @param hash - hash function that would be used (e.g. sha256)
  66. * @param ikm - input keying material, the initial key
  67. * @param salt - optional salt value (a non-secret random value)
  68. * @param info - optional context and application specific information
  69. * @param length - length of output keying material in octets
  70. */
  71. export const hkdf = (hash, ikm, salt, info, length) => expand(hash, extract(hash, ikm, salt), info, length);
  72. //# sourceMappingURL=hkdf.js.map