argon2.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /**
  2. * Argon2 KDF from RFC 9106. Can be used to create a key from password and salt.
  3. * We suggest to use Scrypt. JS Argon is 2-10x slower than native code because of 64-bitness:
  4. * * argon uses uint64, but JS doesn't have fast uint64array
  5. * * uint64 multiplication is 1/3 of time
  6. * * `P` function would be very nice with u64, because most of value will be in registers,
  7. * hovewer with u32 it will require 32 registers, which is too much.
  8. * * JS arrays do slow bound checks, so reading from `A2_BUF` slows it down
  9. * @module
  10. */
  11. import { add3H, add3L, rotr32H, rotr32L, rotrBH, rotrBL, rotrSH, rotrSL } from "./_u64.js";
  12. import { blake2b } from "./blake2.js";
  13. import { abytes, clean, kdfInputToBytes, nextTick, u32, u8 } from "./utils.js";
  14. const AT = { Argond2d: 0, Argon2i: 1, Argon2id: 2 };
  15. const ARGON2_SYNC_POINTS = 4;
  16. const abytesOrZero = (buf) => {
  17. if (buf === undefined)
  18. return Uint8Array.of();
  19. return kdfInputToBytes(buf);
  20. };
  21. // u32 * u32 = u64
  22. function mul(a, b) {
  23. const aL = a & 0xffff;
  24. const aH = a >>> 16;
  25. const bL = b & 0xffff;
  26. const bH = b >>> 16;
  27. const ll = Math.imul(aL, bL);
  28. const hl = Math.imul(aH, bL);
  29. const lh = Math.imul(aL, bH);
  30. const hh = Math.imul(aH, bH);
  31. const carry = (ll >>> 16) + (hl & 0xffff) + lh;
  32. const high = (hh + (hl >>> 16) + (carry >>> 16)) | 0;
  33. const low = (carry << 16) | (ll & 0xffff);
  34. return { h: high, l: low };
  35. }
  36. function mul2(a, b) {
  37. // 2 * a * b (via shifts)
  38. const { h, l } = mul(a, b);
  39. return { h: ((h << 1) | (l >>> 31)) & 4294967295, l: (l << 1) & 4294967295 };
  40. }
  41. // BlaMka permutation for Argon2
  42. // A + B + (2 * u32(A) * u32(B))
  43. function blamka(Ah, Al, Bh, Bl) {
  44. const { h: Ch, l: Cl } = mul2(Al, Bl);
  45. // A + B + (2 * A * B)
  46. const Rll = add3L(Al, Bl, Cl);
  47. return { h: add3H(Rll, Ah, Bh, Ch), l: Rll | 0 };
  48. }
  49. // Temporary block buffer
  50. const A2_BUF = new Uint32Array(256); // 1024 bytes (matrix 16x16)
  51. function G(a, b, c, d) {
  52. let Al = A2_BUF[2 * a], Ah = A2_BUF[2 * a + 1]; // prettier-ignore
  53. let Bl = A2_BUF[2 * b], Bh = A2_BUF[2 * b + 1]; // prettier-ignore
  54. let Cl = A2_BUF[2 * c], Ch = A2_BUF[2 * c + 1]; // prettier-ignore
  55. let Dl = A2_BUF[2 * d], Dh = A2_BUF[2 * d + 1]; // prettier-ignore
  56. ({ h: Ah, l: Al } = blamka(Ah, Al, Bh, Bl));
  57. ({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
  58. ({ Dh, Dl } = { Dh: rotr32H(Dh, Dl), Dl: rotr32L(Dh, Dl) });
  59. ({ h: Ch, l: Cl } = blamka(Ch, Cl, Dh, Dl));
  60. ({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
  61. ({ Bh, Bl } = { Bh: rotrSH(Bh, Bl, 24), Bl: rotrSL(Bh, Bl, 24) });
  62. ({ h: Ah, l: Al } = blamka(Ah, Al, Bh, Bl));
  63. ({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
  64. ({ Dh, Dl } = { Dh: rotrSH(Dh, Dl, 16), Dl: rotrSL(Dh, Dl, 16) });
  65. ({ h: Ch, l: Cl } = blamka(Ch, Cl, Dh, Dl));
  66. ({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
  67. ({ Bh, Bl } = { Bh: rotrBH(Bh, Bl, 63), Bl: rotrBL(Bh, Bl, 63) });
  68. (A2_BUF[2 * a] = Al), (A2_BUF[2 * a + 1] = Ah);
  69. (A2_BUF[2 * b] = Bl), (A2_BUF[2 * b + 1] = Bh);
  70. (A2_BUF[2 * c] = Cl), (A2_BUF[2 * c + 1] = Ch);
  71. (A2_BUF[2 * d] = Dl), (A2_BUF[2 * d + 1] = Dh);
  72. }
  73. // prettier-ignore
  74. function P(v00, v01, v02, v03, v04, v05, v06, v07, v08, v09, v10, v11, v12, v13, v14, v15) {
  75. G(v00, v04, v08, v12);
  76. G(v01, v05, v09, v13);
  77. G(v02, v06, v10, v14);
  78. G(v03, v07, v11, v15);
  79. G(v00, v05, v10, v15);
  80. G(v01, v06, v11, v12);
  81. G(v02, v07, v08, v13);
  82. G(v03, v04, v09, v14);
  83. }
  84. function block(x, xPos, yPos, outPos, needXor) {
  85. for (let i = 0; i < 256; i++)
  86. A2_BUF[i] = x[xPos + i] ^ x[yPos + i];
  87. // columns (8)
  88. for (let i = 0; i < 128; i += 16) {
  89. // prettier-ignore
  90. P(i, i + 1, i + 2, i + 3, i + 4, i + 5, i + 6, i + 7, i + 8, i + 9, i + 10, i + 11, i + 12, i + 13, i + 14, i + 15);
  91. }
  92. // rows (8)
  93. for (let i = 0; i < 16; i += 2) {
  94. // prettier-ignore
  95. P(i, i + 1, i + 16, i + 17, i + 32, i + 33, i + 48, i + 49, i + 64, i + 65, i + 80, i + 81, i + 96, i + 97, i + 112, i + 113);
  96. }
  97. if (needXor)
  98. for (let i = 0; i < 256; i++)
  99. x[outPos + i] ^= A2_BUF[i] ^ x[xPos + i] ^ x[yPos + i];
  100. else
  101. for (let i = 0; i < 256; i++)
  102. x[outPos + i] = A2_BUF[i] ^ x[xPos + i] ^ x[yPos + i];
  103. clean(A2_BUF);
  104. }
  105. // Variable-Length Hash Function H'
  106. function Hp(A, dkLen) {
  107. const A8 = u8(A);
  108. const T = new Uint32Array(1);
  109. const T8 = u8(T);
  110. T[0] = dkLen;
  111. // Fast path
  112. if (dkLen <= 64)
  113. return blake2b.create({ dkLen }).update(T8).update(A8).digest();
  114. const out = new Uint8Array(dkLen);
  115. let V = blake2b.create({}).update(T8).update(A8).digest();
  116. let pos = 0;
  117. // First block
  118. out.set(V.subarray(0, 32));
  119. pos += 32;
  120. // Rest blocks
  121. for (; dkLen - pos > 64; pos += 32) {
  122. const Vh = blake2b.create({}).update(V);
  123. Vh.digestInto(V);
  124. Vh.destroy();
  125. out.set(V.subarray(0, 32), pos);
  126. }
  127. // Last block
  128. out.set(blake2b(V, { dkLen: dkLen - pos }), pos);
  129. clean(V, T);
  130. return u32(out);
  131. }
  132. // Used only inside process block!
  133. function indexAlpha(r, s, laneLen, segmentLen, index, randL, sameLane = false) {
  134. // This is ugly, but close enough to reference implementation.
  135. let area;
  136. if (r === 0) {
  137. if (s === 0)
  138. area = index - 1;
  139. else if (sameLane)
  140. area = s * segmentLen + index - 1;
  141. else
  142. area = s * segmentLen + (index == 0 ? -1 : 0);
  143. }
  144. else if (sameLane)
  145. area = laneLen - segmentLen + index - 1;
  146. else
  147. area = laneLen - segmentLen + (index == 0 ? -1 : 0);
  148. const startPos = r !== 0 && s !== ARGON2_SYNC_POINTS - 1 ? (s + 1) * segmentLen : 0;
  149. const rel = area - 1 - mul(area, mul(randL, randL).h).h;
  150. return (startPos + rel) % laneLen;
  151. }
  152. const maxUint32 = Math.pow(2, 32);
  153. function isU32(num) {
  154. return Number.isSafeInteger(num) && num >= 0 && num < maxUint32;
  155. }
  156. function argon2Opts(opts) {
  157. const merged = {
  158. version: 0x13,
  159. dkLen: 32,
  160. maxmem: maxUint32 - 1,
  161. asyncTick: 10,
  162. };
  163. for (let [k, v] of Object.entries(opts))
  164. if (v != null)
  165. merged[k] = v;
  166. const { dkLen, p, m, t, version, onProgress } = merged;
  167. if (!isU32(dkLen) || dkLen < 4)
  168. throw new Error('dkLen should be at least 4 bytes');
  169. if (!isU32(p) || p < 1 || p >= Math.pow(2, 24))
  170. throw new Error('p should be 1 <= p < 2^24');
  171. if (!isU32(m))
  172. throw new Error('m should be 0 <= m < 2^32');
  173. if (!isU32(t) || t < 1)
  174. throw new Error('t (iterations) should be 1 <= t < 2^32');
  175. if (onProgress !== undefined && typeof onProgress !== 'function')
  176. throw new Error('progressCb should be function');
  177. /*
  178. Memory size m MUST be an integer number of kibibytes from 8*p to 2^(32)-1. The actual number of blocks is m', which is m rounded down to the nearest multiple of 4*p.
  179. */
  180. if (!isU32(m) || m < 8 * p)
  181. throw new Error('memory should be at least 8*p bytes');
  182. if (version !== 0x10 && version !== 0x13)
  183. throw new Error('unknown version=' + version);
  184. return merged;
  185. }
  186. function argon2Init(password, salt, type, opts) {
  187. password = kdfInputToBytes(password);
  188. salt = kdfInputToBytes(salt);
  189. abytes(password);
  190. abytes(salt);
  191. if (!isU32(password.length))
  192. throw new Error('password should be less than 4 GB');
  193. if (!isU32(salt.length) || salt.length < 8)
  194. throw new Error('salt should be at least 8 bytes and less than 4 GB');
  195. if (!Object.values(AT).includes(type))
  196. throw new Error('invalid type');
  197. let { p, dkLen, m, t, version, key, personalization, maxmem, onProgress, asyncTick } = argon2Opts(opts);
  198. // Validation
  199. key = abytesOrZero(key);
  200. personalization = abytesOrZero(personalization);
  201. // H_0 = H^(64)(LE32(p) || LE32(T) || LE32(m) || LE32(t) ||
  202. // LE32(v) || LE32(y) || LE32(length(P)) || P ||
  203. // LE32(length(S)) || S || LE32(length(K)) || K ||
  204. // LE32(length(X)) || X)
  205. const h = blake2b.create({});
  206. const BUF = new Uint32Array(1);
  207. const BUF8 = u8(BUF);
  208. for (let item of [p, dkLen, m, t, version, type]) {
  209. BUF[0] = item;
  210. h.update(BUF8);
  211. }
  212. for (let i of [password, salt, key, personalization]) {
  213. BUF[0] = i.length; // BUF is u32 array, this is valid
  214. h.update(BUF8).update(i);
  215. }
  216. const H0 = new Uint32Array(18);
  217. const H0_8 = u8(H0);
  218. h.digestInto(H0_8);
  219. // 256 u32 = 1024 (BLOCK_SIZE), fills A2_BUF on processing
  220. // Params
  221. const lanes = p;
  222. // m' = 4 * p * floor (m / 4p)
  223. const mP = 4 * p * Math.floor(m / (ARGON2_SYNC_POINTS * p));
  224. //q = m' / p columns
  225. const laneLen = Math.floor(mP / p);
  226. const segmentLen = Math.floor(laneLen / ARGON2_SYNC_POINTS);
  227. const memUsed = mP * 256;
  228. if (!isU32(maxmem) || memUsed > maxmem)
  229. throw new Error('mem should be less than 2**32, got: maxmem=' + maxmem + ', memused=' + memUsed);
  230. const B = new Uint32Array(memUsed);
  231. // Fill first blocks
  232. for (let l = 0; l < p; l++) {
  233. const i = 256 * laneLen * l;
  234. // B[i][0] = H'^(1024)(H_0 || LE32(0) || LE32(i))
  235. H0[17] = l;
  236. H0[16] = 0;
  237. B.set(Hp(H0, 1024), i);
  238. // B[i][1] = H'^(1024)(H_0 || LE32(1) || LE32(i))
  239. H0[16] = 1;
  240. B.set(Hp(H0, 1024), i + 256);
  241. }
  242. let perBlock = () => { };
  243. if (onProgress) {
  244. const totalBlock = t * ARGON2_SYNC_POINTS * p * segmentLen;
  245. // Invoke callback if progress changes from 10.01 to 10.02
  246. // Allows to draw smooth progress bar on up to 8K screen
  247. const callbackPer = Math.max(Math.floor(totalBlock / 10000), 1);
  248. let blockCnt = 0;
  249. perBlock = () => {
  250. blockCnt++;
  251. if (onProgress && (!(blockCnt % callbackPer) || blockCnt === totalBlock))
  252. onProgress(blockCnt / totalBlock);
  253. };
  254. }
  255. clean(BUF, H0);
  256. return { type, mP, p, t, version, B, laneLen, lanes, segmentLen, dkLen, perBlock, asyncTick };
  257. }
  258. function argon2Output(B, p, laneLen, dkLen) {
  259. const B_final = new Uint32Array(256);
  260. for (let l = 0; l < p; l++)
  261. for (let j = 0; j < 256; j++)
  262. B_final[j] ^= B[256 * (laneLen * l + laneLen - 1) + j];
  263. const res = u8(Hp(B_final, dkLen));
  264. clean(B_final);
  265. return res;
  266. }
  267. function processBlock(B, address, l, r, s, index, laneLen, segmentLen, lanes, offset, prev, dataIndependent, needXor) {
  268. if (offset % laneLen)
  269. prev = offset - 1;
  270. let randL, randH;
  271. if (dataIndependent) {
  272. let i128 = index % 128;
  273. if (i128 === 0) {
  274. address[256 + 12]++;
  275. block(address, 256, 2 * 256, 0, false);
  276. block(address, 0, 2 * 256, 0, false);
  277. }
  278. randL = address[2 * i128];
  279. randH = address[2 * i128 + 1];
  280. }
  281. else {
  282. const T = 256 * prev;
  283. randL = B[T];
  284. randH = B[T + 1];
  285. }
  286. // address block
  287. const refLane = r === 0 && s === 0 ? l : randH % lanes;
  288. const refPos = indexAlpha(r, s, laneLen, segmentLen, index, randL, refLane == l);
  289. const refBlock = laneLen * refLane + refPos;
  290. // B[i][j] = G(B[i][j-1], B[l][z])
  291. block(B, 256 * prev, 256 * refBlock, offset * 256, needXor);
  292. }
  293. function argon2(type, password, salt, opts) {
  294. const { mP, p, t, version, B, laneLen, lanes, segmentLen, dkLen, perBlock } = argon2Init(password, salt, type, opts);
  295. // Pre-loop setup
  296. // [address, input, zero_block] format so we can pass single U32 to block function
  297. const address = new Uint32Array(3 * 256);
  298. address[256 + 6] = mP;
  299. address[256 + 8] = t;
  300. address[256 + 10] = type;
  301. for (let r = 0; r < t; r++) {
  302. const needXor = r !== 0 && version === 0x13;
  303. address[256 + 0] = r;
  304. for (let s = 0; s < ARGON2_SYNC_POINTS; s++) {
  305. address[256 + 4] = s;
  306. const dataIndependent = type == AT.Argon2i || (type == AT.Argon2id && r === 0 && s < 2);
  307. for (let l = 0; l < p; l++) {
  308. address[256 + 2] = l;
  309. address[256 + 12] = 0;
  310. let startPos = 0;
  311. if (r === 0 && s === 0) {
  312. startPos = 2;
  313. if (dataIndependent) {
  314. address[256 + 12]++;
  315. block(address, 256, 2 * 256, 0, false);
  316. block(address, 0, 2 * 256, 0, false);
  317. }
  318. }
  319. // current block postion
  320. let offset = l * laneLen + s * segmentLen + startPos;
  321. // previous block position
  322. let prev = offset % laneLen ? offset - 1 : offset + laneLen - 1;
  323. for (let index = startPos; index < segmentLen; index++, offset++, prev++) {
  324. perBlock();
  325. processBlock(B, address, l, r, s, index, laneLen, segmentLen, lanes, offset, prev, dataIndependent, needXor);
  326. }
  327. }
  328. }
  329. }
  330. clean(address);
  331. return argon2Output(B, p, laneLen, dkLen);
  332. }
  333. /** argon2d GPU-resistant version. */
  334. export const argon2d = (password, salt, opts) => argon2(AT.Argond2d, password, salt, opts);
  335. /** argon2i side-channel-resistant version. */
  336. export const argon2i = (password, salt, opts) => argon2(AT.Argon2i, password, salt, opts);
  337. /** argon2id, combining i+d, the most popular version from RFC 9106 */
  338. export const argon2id = (password, salt, opts) => argon2(AT.Argon2id, password, salt, opts);
  339. async function argon2Async(type, password, salt, opts) {
  340. const { mP, p, t, version, B, laneLen, lanes, segmentLen, dkLen, perBlock, asyncTick } = argon2Init(password, salt, type, opts);
  341. // Pre-loop setup
  342. // [address, input, zero_block] format so we can pass single U32 to block function
  343. const address = new Uint32Array(3 * 256);
  344. address[256 + 6] = mP;
  345. address[256 + 8] = t;
  346. address[256 + 10] = type;
  347. let ts = Date.now();
  348. for (let r = 0; r < t; r++) {
  349. const needXor = r !== 0 && version === 0x13;
  350. address[256 + 0] = r;
  351. for (let s = 0; s < ARGON2_SYNC_POINTS; s++) {
  352. address[256 + 4] = s;
  353. const dataIndependent = type == AT.Argon2i || (type == AT.Argon2id && r === 0 && s < 2);
  354. for (let l = 0; l < p; l++) {
  355. address[256 + 2] = l;
  356. address[256 + 12] = 0;
  357. let startPos = 0;
  358. if (r === 0 && s === 0) {
  359. startPos = 2;
  360. if (dataIndependent) {
  361. address[256 + 12]++;
  362. block(address, 256, 2 * 256, 0, false);
  363. block(address, 0, 2 * 256, 0, false);
  364. }
  365. }
  366. // current block postion
  367. let offset = l * laneLen + s * segmentLen + startPos;
  368. // previous block position
  369. let prev = offset % laneLen ? offset - 1 : offset + laneLen - 1;
  370. for (let index = startPos; index < segmentLen; index++, offset++, prev++) {
  371. perBlock();
  372. processBlock(B, address, l, r, s, index, laneLen, segmentLen, lanes, offset, prev, dataIndependent, needXor);
  373. // Date.now() is not monotonic, so in case if clock goes backwards we return return control too
  374. const diff = Date.now() - ts;
  375. if (!(diff >= 0 && diff < asyncTick)) {
  376. await nextTick();
  377. ts += diff;
  378. }
  379. }
  380. }
  381. }
  382. }
  383. clean(address);
  384. return argon2Output(B, p, laneLen, dkLen);
  385. }
  386. /** argon2d async GPU-resistant version. */
  387. export const argon2dAsync = (password, salt, opts) => argon2Async(AT.Argond2d, password, salt, opts);
  388. /** argon2i async side-channel-resistant version. */
  389. export const argon2iAsync = (password, salt, opts) => argon2Async(AT.Argon2i, password, salt, opts);
  390. /** argon2id async, combining i+d, the most popular version from RFC 9106 */
  391. export const argon2idAsync = (password, salt, opts) => argon2Async(AT.Argon2id, password, salt, opts);
  392. //# sourceMappingURL=argon2.js.map