pbkdf2.js 3.8 KB

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