scrypt.js 9.7 KB

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