pbkdf2.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.pbkdf2 = pbkdf2;
  4. exports.pbkdf2Async = pbkdf2Async;
  5. /**
  6. * PBKDF (RFC 2898). Can be used to create a key from password and salt.
  7. * @module
  8. */
  9. const hmac_ts_1 = require("./hmac.js");
  10. // prettier-ignore
  11. const utils_ts_1 = require("./utils.js");
  12. // Common prologue and epilogue for sync/async functions
  13. function pbkdf2Init(hash, _password, _salt, _opts) {
  14. (0, utils_ts_1.ahash)(hash);
  15. const opts = (0, utils_ts_1.checkOpts)({ dkLen: 32, asyncTick: 10 }, _opts);
  16. const { c, dkLen, asyncTick } = opts;
  17. (0, utils_ts_1.anumber)(c);
  18. (0, utils_ts_1.anumber)(dkLen);
  19. (0, utils_ts_1.anumber)(asyncTick);
  20. if (c < 1)
  21. throw new Error('iterations (c) should be >= 1');
  22. const password = (0, utils_ts_1.kdfInputToBytes)(_password);
  23. const salt = (0, utils_ts_1.kdfInputToBytes)(_salt);
  24. // DK = PBKDF2(PRF, Password, Salt, c, dkLen);
  25. const DK = new Uint8Array(dkLen);
  26. // U1 = PRF(Password, Salt + INT_32_BE(i))
  27. const PRF = hmac_ts_1.hmac.create(hash, password);
  28. const PRFSalt = PRF._cloneInto().update(salt);
  29. return { c, dkLen, asyncTick, DK, PRF, PRFSalt };
  30. }
  31. function pbkdf2Output(PRF, PRFSalt, DK, prfW, u) {
  32. PRF.destroy();
  33. PRFSalt.destroy();
  34. if (prfW)
  35. prfW.destroy();
  36. (0, utils_ts_1.clean)(u);
  37. return DK;
  38. }
  39. /**
  40. * PBKDF2-HMAC: RFC 2898 key derivation function
  41. * @param hash - hash function that would be used e.g. sha256
  42. * @param password - password from which a derived key is generated
  43. * @param salt - cryptographic salt
  44. * @param opts - {c, dkLen} where c is work factor and dkLen is output message size
  45. * @example
  46. * const key = pbkdf2(sha256, 'password', 'salt', { dkLen: 32, c: Math.pow(2, 18) });
  47. */
  48. function pbkdf2(hash, password, salt, opts) {
  49. const { c, dkLen, DK, PRF, PRFSalt } = pbkdf2Init(hash, password, salt, opts);
  50. let prfW; // Working copy
  51. const arr = new Uint8Array(4);
  52. const view = (0, utils_ts_1.createView)(arr);
  53. const u = new Uint8Array(PRF.outputLen);
  54. // DK = T1 + T2 + ⋯ + Tdklen/hlen
  55. for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {
  56. // Ti = F(Password, Salt, c, i)
  57. const Ti = DK.subarray(pos, pos + PRF.outputLen);
  58. view.setInt32(0, ti, false);
  59. // F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc
  60. // U1 = PRF(Password, Salt + INT_32_BE(i))
  61. (prfW = PRFSalt._cloneInto(prfW)).update(arr).digestInto(u);
  62. Ti.set(u.subarray(0, Ti.length));
  63. for (let ui = 1; ui < c; ui++) {
  64. // Uc = PRF(Password, Uc−1)
  65. PRF._cloneInto(prfW).update(u).digestInto(u);
  66. for (let i = 0; i < Ti.length; i++)
  67. Ti[i] ^= u[i];
  68. }
  69. }
  70. return pbkdf2Output(PRF, PRFSalt, DK, prfW, u);
  71. }
  72. /**
  73. * PBKDF2-HMAC: RFC 2898 key derivation function. Async version.
  74. * @example
  75. * await pbkdf2Async(sha256, 'password', 'salt', { dkLen: 32, c: 500_000 });
  76. */
  77. async function pbkdf2Async(hash, password, salt, opts) {
  78. const { c, dkLen, asyncTick, DK, PRF, PRFSalt } = pbkdf2Init(hash, password, salt, opts);
  79. let prfW; // Working copy
  80. const arr = new Uint8Array(4);
  81. const view = (0, utils_ts_1.createView)(arr);
  82. const u = new Uint8Array(PRF.outputLen);
  83. // DK = T1 + T2 + ⋯ + Tdklen/hlen
  84. for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {
  85. // Ti = F(Password, Salt, c, i)
  86. const Ti = DK.subarray(pos, pos + PRF.outputLen);
  87. view.setInt32(0, ti, false);
  88. // F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc
  89. // U1 = PRF(Password, Salt + INT_32_BE(i))
  90. (prfW = PRFSalt._cloneInto(prfW)).update(arr).digestInto(u);
  91. Ti.set(u.subarray(0, Ti.length));
  92. await (0, utils_ts_1.asyncLoop)(c - 1, asyncTick, () => {
  93. // Uc = PRF(Password, Uc−1)
  94. PRF._cloneInto(prfW).update(u).digestInto(u);
  95. for (let i = 0; i < Ti.length; i++)
  96. Ti[i] ^= u[i];
  97. });
  98. }
  99. return pbkdf2Output(PRF, PRFSalt, DK, prfW, u);
  100. }
  101. //# sourceMappingURL=pbkdf2.js.map