scrypt.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.scrypt = scrypt;
  4. exports.scryptAsync = scryptAsync;
  5. /**
  6. * RFC 7914 Scrypt KDF. Can be used to create a key from password and salt.
  7. * @module
  8. */
  9. const pbkdf2_ts_1 = require("./pbkdf2.js");
  10. const sha2_ts_1 = require("./sha2.js");
  11. // prettier-ignore
  12. const utils_ts_1 = require("./utils.js");
  13. // The main Scrypt loop: uses Salsa extensively.
  14. // Six versions of the function were tried, this is the fastest one.
  15. // prettier-ignore
  16. function XorAndSalsa(prev, pi, input, ii, out, oi) {
  17. // Based on https://cr.yp.to/salsa20.html
  18. // Xor blocks
  19. let y00 = prev[pi++] ^ input[ii++], y01 = prev[pi++] ^ input[ii++];
  20. let y02 = prev[pi++] ^ input[ii++], y03 = prev[pi++] ^ input[ii++];
  21. let y04 = prev[pi++] ^ input[ii++], y05 = prev[pi++] ^ input[ii++];
  22. let y06 = prev[pi++] ^ input[ii++], y07 = prev[pi++] ^ input[ii++];
  23. let y08 = prev[pi++] ^ input[ii++], y09 = prev[pi++] ^ input[ii++];
  24. let y10 = prev[pi++] ^ input[ii++], y11 = prev[pi++] ^ input[ii++];
  25. let y12 = prev[pi++] ^ input[ii++], y13 = prev[pi++] ^ input[ii++];
  26. let y14 = prev[pi++] ^ input[ii++], y15 = prev[pi++] ^ input[ii++];
  27. // Save state to temporary variables (salsa)
  28. let x00 = y00, x01 = y01, x02 = y02, x03 = y03, x04 = y04, x05 = y05, x06 = y06, x07 = y07, x08 = y08, x09 = y09, x10 = y10, x11 = y11, x12 = y12, x13 = y13, x14 = y14, x15 = y15;
  29. // Main loop (salsa)
  30. for (let i = 0; i < 8; i += 2) {
  31. x04 ^= (0, utils_ts_1.rotl)(x00 + x12 | 0, 7);
  32. x08 ^= (0, utils_ts_1.rotl)(x04 + x00 | 0, 9);
  33. x12 ^= (0, utils_ts_1.rotl)(x08 + x04 | 0, 13);
  34. x00 ^= (0, utils_ts_1.rotl)(x12 + x08 | 0, 18);
  35. x09 ^= (0, utils_ts_1.rotl)(x05 + x01 | 0, 7);
  36. x13 ^= (0, utils_ts_1.rotl)(x09 + x05 | 0, 9);
  37. x01 ^= (0, utils_ts_1.rotl)(x13 + x09 | 0, 13);
  38. x05 ^= (0, utils_ts_1.rotl)(x01 + x13 | 0, 18);
  39. x14 ^= (0, utils_ts_1.rotl)(x10 + x06 | 0, 7);
  40. x02 ^= (0, utils_ts_1.rotl)(x14 + x10 | 0, 9);
  41. x06 ^= (0, utils_ts_1.rotl)(x02 + x14 | 0, 13);
  42. x10 ^= (0, utils_ts_1.rotl)(x06 + x02 | 0, 18);
  43. x03 ^= (0, utils_ts_1.rotl)(x15 + x11 | 0, 7);
  44. x07 ^= (0, utils_ts_1.rotl)(x03 + x15 | 0, 9);
  45. x11 ^= (0, utils_ts_1.rotl)(x07 + x03 | 0, 13);
  46. x15 ^= (0, utils_ts_1.rotl)(x11 + x07 | 0, 18);
  47. x01 ^= (0, utils_ts_1.rotl)(x00 + x03 | 0, 7);
  48. x02 ^= (0, utils_ts_1.rotl)(x01 + x00 | 0, 9);
  49. x03 ^= (0, utils_ts_1.rotl)(x02 + x01 | 0, 13);
  50. x00 ^= (0, utils_ts_1.rotl)(x03 + x02 | 0, 18);
  51. x06 ^= (0, utils_ts_1.rotl)(x05 + x04 | 0, 7);
  52. x07 ^= (0, utils_ts_1.rotl)(x06 + x05 | 0, 9);
  53. x04 ^= (0, utils_ts_1.rotl)(x07 + x06 | 0, 13);
  54. x05 ^= (0, utils_ts_1.rotl)(x04 + x07 | 0, 18);
  55. x11 ^= (0, utils_ts_1.rotl)(x10 + x09 | 0, 7);
  56. x08 ^= (0, utils_ts_1.rotl)(x11 + x10 | 0, 9);
  57. x09 ^= (0, utils_ts_1.rotl)(x08 + x11 | 0, 13);
  58. x10 ^= (0, utils_ts_1.rotl)(x09 + x08 | 0, 18);
  59. x12 ^= (0, utils_ts_1.rotl)(x15 + x14 | 0, 7);
  60. x13 ^= (0, utils_ts_1.rotl)(x12 + x15 | 0, 9);
  61. x14 ^= (0, utils_ts_1.rotl)(x13 + x12 | 0, 13);
  62. x15 ^= (0, utils_ts_1.rotl)(x14 + x13 | 0, 18);
  63. }
  64. // Write output (salsa)
  65. out[oi++] = (y00 + x00) | 0;
  66. out[oi++] = (y01 + x01) | 0;
  67. out[oi++] = (y02 + x02) | 0;
  68. out[oi++] = (y03 + x03) | 0;
  69. out[oi++] = (y04 + x04) | 0;
  70. out[oi++] = (y05 + x05) | 0;
  71. out[oi++] = (y06 + x06) | 0;
  72. out[oi++] = (y07 + x07) | 0;
  73. out[oi++] = (y08 + x08) | 0;
  74. out[oi++] = (y09 + x09) | 0;
  75. out[oi++] = (y10 + x10) | 0;
  76. out[oi++] = (y11 + x11) | 0;
  77. out[oi++] = (y12 + x12) | 0;
  78. out[oi++] = (y13 + x13) | 0;
  79. out[oi++] = (y14 + x14) | 0;
  80. out[oi++] = (y15 + x15) | 0;
  81. }
  82. function BlockMix(input, ii, out, oi, r) {
  83. // The block B is r 128-byte chunks (which is equivalent of 2r 64-byte chunks)
  84. let head = oi + 0;
  85. let tail = oi + 16 * r;
  86. for (let i = 0; i < 16; i++)
  87. out[tail + i] = input[ii + (2 * r - 1) * 16 + i]; // X ← B[2r−1]
  88. for (let i = 0; i < r; i++, head += 16, ii += 16) {
  89. // We write odd & even Yi at same time. Even: 0bXXXXX0 Odd: 0bXXXXX1
  90. XorAndSalsa(out, tail, input, ii, out, head); // head[i] = Salsa(blockIn[2*i] ^ tail[i-1])
  91. if (i > 0)
  92. tail += 16; // First iteration overwrites tmp value in tail
  93. XorAndSalsa(out, head, input, (ii += 16), out, tail); // tail[i] = Salsa(blockIn[2*i+1] ^ head[i])
  94. }
  95. }
  96. // Common prologue and epilogue for sync/async functions
  97. function scryptInit(password, salt, _opts) {
  98. // Maxmem - 1GB+1KB by default
  99. const opts = (0, utils_ts_1.checkOpts)({
  100. dkLen: 32,
  101. asyncTick: 10,
  102. maxmem: 1024 ** 3 + 1024,
  103. }, _opts);
  104. const { N, r, p, dkLen, asyncTick, maxmem, onProgress } = opts;
  105. (0, utils_ts_1.anumber)(N);
  106. (0, utils_ts_1.anumber)(r);
  107. (0, utils_ts_1.anumber)(p);
  108. (0, utils_ts_1.anumber)(dkLen);
  109. (0, utils_ts_1.anumber)(asyncTick);
  110. (0, utils_ts_1.anumber)(maxmem);
  111. if (onProgress !== undefined && typeof onProgress !== 'function')
  112. throw new Error('progressCb should be function');
  113. const blockSize = 128 * r;
  114. const blockSize32 = blockSize / 4;
  115. // Max N is 2^32 (Integrify is 32-bit). Real limit is 2^22: JS engines Uint8Array limit is 4GB in 2024.
  116. // Spec check `N >= 2^(blockSize / 8)` is not done for compat with popular libs,
  117. // which used incorrect r: 1, p: 8. Also, the check seems to be a spec error:
  118. // https://www.rfc-editor.org/errata_search.php?rfc=7914
  119. const pow32 = Math.pow(2, 32);
  120. if (N <= 1 || (N & (N - 1)) !== 0 || N > pow32) {
  121. throw new Error('Scrypt: N must be larger than 1, a power of 2, and less than 2^32');
  122. }
  123. if (p < 0 || p > ((pow32 - 1) * 32) / blockSize) {
  124. throw new Error('Scrypt: p must be a positive integer less than or equal to ((2^32 - 1) * 32) / (128 * r)');
  125. }
  126. if (dkLen < 0 || dkLen > (pow32 - 1) * 32) {
  127. throw new Error('Scrypt: dkLen should be positive integer less than or equal to (2^32 - 1) * 32');
  128. }
  129. const memUsed = blockSize * (N + p);
  130. if (memUsed > maxmem) {
  131. throw new Error('Scrypt: memused is bigger than maxMem. Expected 128 * r * (N + p) > maxmem of ' + maxmem);
  132. }
  133. // [B0...Bp−1] ← PBKDF2HMAC-SHA256(Passphrase, Salt, 1, blockSize*ParallelizationFactor)
  134. // Since it has only one iteration there is no reason to use async variant
  135. const B = (0, pbkdf2_ts_1.pbkdf2)(sha2_ts_1.sha256, password, salt, { c: 1, dkLen: blockSize * p });
  136. const B32 = (0, utils_ts_1.u32)(B);
  137. // Re-used between parallel iterations. Array(iterations) of B
  138. const V = (0, utils_ts_1.u32)(new Uint8Array(blockSize * N));
  139. const tmp = (0, utils_ts_1.u32)(new Uint8Array(blockSize));
  140. let blockMixCb = () => { };
  141. if (onProgress) {
  142. const totalBlockMix = 2 * N * p;
  143. // Invoke callback if progress changes from 10.01 to 10.02
  144. // Allows to draw smooth progress bar on up to 8K screen
  145. const callbackPer = Math.max(Math.floor(totalBlockMix / 10000), 1);
  146. let blockMixCnt = 0;
  147. blockMixCb = () => {
  148. blockMixCnt++;
  149. if (onProgress && (!(blockMixCnt % callbackPer) || blockMixCnt === totalBlockMix))
  150. onProgress(blockMixCnt / totalBlockMix);
  151. };
  152. }
  153. return { N, r, p, dkLen, blockSize32, V, B32, B, tmp, blockMixCb, asyncTick };
  154. }
  155. function scryptOutput(password, dkLen, B, V, tmp) {
  156. const res = (0, pbkdf2_ts_1.pbkdf2)(sha2_ts_1.sha256, password, B, { c: 1, dkLen });
  157. (0, utils_ts_1.clean)(B, V, tmp);
  158. return res;
  159. }
  160. /**
  161. * Scrypt KDF from RFC 7914.
  162. * @param password - pass
  163. * @param salt - salt
  164. * @param opts - parameters
  165. * - `N` is cpu/mem work factor (power of 2 e.g. 2**18)
  166. * - `r` is block size (8 is common), fine-tunes sequential memory read size and performance
  167. * - `p` is parallelization factor (1 is common)
  168. * - `dkLen` is output key length in bytes e.g. 32.
  169. * - `asyncTick` - (default: 10) max time in ms for which async function can block execution
  170. * - `maxmem` - (default: `1024 ** 3 + 1024` aka 1GB+1KB). A limit that the app could use for scrypt
  171. * - `onProgress` - callback function that would be executed for progress report
  172. * @returns Derived key
  173. * @example
  174. * scrypt('password', 'salt', { N: 2**18, r: 8, p: 1, dkLen: 32 });
  175. */
  176. function scrypt(password, salt, opts) {
  177. const { N, r, p, dkLen, blockSize32, V, B32, B, tmp, blockMixCb } = scryptInit(password, salt, opts);
  178. (0, utils_ts_1.swap32IfBE)(B32);
  179. for (let pi = 0; pi < p; pi++) {
  180. const Pi = blockSize32 * pi;
  181. for (let i = 0; i < blockSize32; i++)
  182. V[i] = B32[Pi + i]; // V[0] = B[i]
  183. for (let i = 0, pos = 0; i < N - 1; i++) {
  184. BlockMix(V, pos, V, (pos += blockSize32), r); // V[i] = BlockMix(V[i-1]);
  185. blockMixCb();
  186. }
  187. BlockMix(V, (N - 1) * blockSize32, B32, Pi, r); // Process last element
  188. blockMixCb();
  189. for (let i = 0; i < N; i++) {
  190. // First u32 of the last 64-byte block (u32 is LE)
  191. const j = B32[Pi + blockSize32 - 16] % N; // j = Integrify(X) % iterations
  192. for (let k = 0; k < blockSize32; k++)
  193. tmp[k] = B32[Pi + k] ^ V[j * blockSize32 + k]; // tmp = B ^ V[j]
  194. BlockMix(tmp, 0, B32, Pi, r); // B = BlockMix(B ^ V[j])
  195. blockMixCb();
  196. }
  197. }
  198. (0, utils_ts_1.swap32IfBE)(B32);
  199. return scryptOutput(password, dkLen, B, V, tmp);
  200. }
  201. /**
  202. * Scrypt KDF from RFC 7914. Async version.
  203. * @example
  204. * await scryptAsync('password', 'salt', { N: 2**18, r: 8, p: 1, dkLen: 32 });
  205. */
  206. async function scryptAsync(password, salt, opts) {
  207. const { N, r, p, dkLen, blockSize32, V, B32, B, tmp, blockMixCb, asyncTick } = scryptInit(password, salt, opts);
  208. (0, utils_ts_1.swap32IfBE)(B32);
  209. for (let pi = 0; pi < p; pi++) {
  210. const Pi = blockSize32 * pi;
  211. for (let i = 0; i < blockSize32; i++)
  212. V[i] = B32[Pi + i]; // V[0] = B[i]
  213. let pos = 0;
  214. await (0, utils_ts_1.asyncLoop)(N - 1, asyncTick, () => {
  215. BlockMix(V, pos, V, (pos += blockSize32), r); // V[i] = BlockMix(V[i-1]);
  216. blockMixCb();
  217. });
  218. BlockMix(V, (N - 1) * blockSize32, B32, Pi, r); // Process last element
  219. blockMixCb();
  220. await (0, utils_ts_1.asyncLoop)(N, asyncTick, () => {
  221. // First u32 of the last 64-byte block (u32 is LE)
  222. const j = B32[Pi + blockSize32 - 16] % N; // j = Integrify(X) % iterations
  223. for (let k = 0; k < blockSize32; k++)
  224. tmp[k] = B32[Pi + k] ^ V[j * blockSize32 + k]; // tmp = B ^ V[j]
  225. BlockMix(tmp, 0, B32, Pi, r); // B = BlockMix(B ^ V[j])
  226. blockMixCb();
  227. });
  228. }
  229. (0, utils_ts_1.swap32IfBE)(B32);
  230. return scryptOutput(password, dkLen, B, V, tmp);
  231. }
  232. //# sourceMappingURL=scrypt.js.map