hkdf.js 3.1 KB

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