blake2b.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.blake2b = void 0;
  4. const _blake_js_1 = require("./_blake.js");
  5. const _u64_js_1 = require("./_u64.js");
  6. const utils_js_1 = require("./utils.js");
  7. // Same as SHA-512 but LE
  8. // prettier-ignore
  9. const B2B_IV = /* @__PURE__ */ new Uint32Array([
  10. 0xf3bcc908, 0x6a09e667, 0x84caa73b, 0xbb67ae85, 0xfe94f82b, 0x3c6ef372, 0x5f1d36f1, 0xa54ff53a,
  11. 0xade682d1, 0x510e527f, 0x2b3e6c1f, 0x9b05688c, 0xfb41bd6b, 0x1f83d9ab, 0x137e2179, 0x5be0cd19
  12. ]);
  13. // Temporary buffer
  14. const BBUF = /* @__PURE__ */ new Uint32Array(32);
  15. // Mixing function G splitted in two halfs
  16. function G1b(a, b, c, d, msg, x) {
  17. // NOTE: V is LE here
  18. const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
  19. let Al = BBUF[2 * a], Ah = BBUF[2 * a + 1]; // prettier-ignore
  20. let Bl = BBUF[2 * b], Bh = BBUF[2 * b + 1]; // prettier-ignore
  21. let Cl = BBUF[2 * c], Ch = BBUF[2 * c + 1]; // prettier-ignore
  22. let Dl = BBUF[2 * d], Dh = BBUF[2 * d + 1]; // prettier-ignore
  23. // v[a] = (v[a] + v[b] + x) | 0;
  24. let ll = _u64_js_1.default.add3L(Al, Bl, Xl);
  25. Ah = _u64_js_1.default.add3H(ll, Ah, Bh, Xh);
  26. Al = ll | 0;
  27. // v[d] = rotr(v[d] ^ v[a], 32)
  28. ({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
  29. ({ Dh, Dl } = { Dh: _u64_js_1.default.rotr32H(Dh, Dl), Dl: _u64_js_1.default.rotr32L(Dh, Dl) });
  30. // v[c] = (v[c] + v[d]) | 0;
  31. ({ h: Ch, l: Cl } = _u64_js_1.default.add(Ch, Cl, Dh, Dl));
  32. // v[b] = rotr(v[b] ^ v[c], 24)
  33. ({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
  34. ({ Bh, Bl } = { Bh: _u64_js_1.default.rotrSH(Bh, Bl, 24), Bl: _u64_js_1.default.rotrSL(Bh, Bl, 24) });
  35. (BBUF[2 * a] = Al), (BBUF[2 * a + 1] = Ah);
  36. (BBUF[2 * b] = Bl), (BBUF[2 * b + 1] = Bh);
  37. (BBUF[2 * c] = Cl), (BBUF[2 * c + 1] = Ch);
  38. (BBUF[2 * d] = Dl), (BBUF[2 * d + 1] = Dh);
  39. }
  40. function G2b(a, b, c, d, msg, x) {
  41. // NOTE: V is LE here
  42. const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
  43. let Al = BBUF[2 * a], Ah = BBUF[2 * a + 1]; // prettier-ignore
  44. let Bl = BBUF[2 * b], Bh = BBUF[2 * b + 1]; // prettier-ignore
  45. let Cl = BBUF[2 * c], Ch = BBUF[2 * c + 1]; // prettier-ignore
  46. let Dl = BBUF[2 * d], Dh = BBUF[2 * d + 1]; // prettier-ignore
  47. // v[a] = (v[a] + v[b] + x) | 0;
  48. let ll = _u64_js_1.default.add3L(Al, Bl, Xl);
  49. Ah = _u64_js_1.default.add3H(ll, Ah, Bh, Xh);
  50. Al = ll | 0;
  51. // v[d] = rotr(v[d] ^ v[a], 16)
  52. ({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
  53. ({ Dh, Dl } = { Dh: _u64_js_1.default.rotrSH(Dh, Dl, 16), Dl: _u64_js_1.default.rotrSL(Dh, Dl, 16) });
  54. // v[c] = (v[c] + v[d]) | 0;
  55. ({ h: Ch, l: Cl } = _u64_js_1.default.add(Ch, Cl, Dh, Dl));
  56. // v[b] = rotr(v[b] ^ v[c], 63)
  57. ({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
  58. ({ Bh, Bl } = { Bh: _u64_js_1.default.rotrBH(Bh, Bl, 63), Bl: _u64_js_1.default.rotrBL(Bh, Bl, 63) });
  59. (BBUF[2 * a] = Al), (BBUF[2 * a + 1] = Ah);
  60. (BBUF[2 * b] = Bl), (BBUF[2 * b + 1] = Bh);
  61. (BBUF[2 * c] = Cl), (BBUF[2 * c + 1] = Ch);
  62. (BBUF[2 * d] = Dl), (BBUF[2 * d + 1] = Dh);
  63. }
  64. class BLAKE2b extends _blake_js_1.BLAKE {
  65. constructor(opts = {}) {
  66. super(128, opts.dkLen === undefined ? 64 : opts.dkLen, opts, 64, 16, 16);
  67. // Same as SHA-512, but LE
  68. this.v0l = B2B_IV[0] | 0;
  69. this.v0h = B2B_IV[1] | 0;
  70. this.v1l = B2B_IV[2] | 0;
  71. this.v1h = B2B_IV[3] | 0;
  72. this.v2l = B2B_IV[4] | 0;
  73. this.v2h = B2B_IV[5] | 0;
  74. this.v3l = B2B_IV[6] | 0;
  75. this.v3h = B2B_IV[7] | 0;
  76. this.v4l = B2B_IV[8] | 0;
  77. this.v4h = B2B_IV[9] | 0;
  78. this.v5l = B2B_IV[10] | 0;
  79. this.v5h = B2B_IV[11] | 0;
  80. this.v6l = B2B_IV[12] | 0;
  81. this.v6h = B2B_IV[13] | 0;
  82. this.v7l = B2B_IV[14] | 0;
  83. this.v7h = B2B_IV[15] | 0;
  84. const keyLength = opts.key ? opts.key.length : 0;
  85. this.v0l ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
  86. if (opts.salt) {
  87. const salt = (0, utils_js_1.u32)((0, utils_js_1.toBytes)(opts.salt));
  88. this.v4l ^= (0, utils_js_1.byteSwapIfBE)(salt[0]);
  89. this.v4h ^= (0, utils_js_1.byteSwapIfBE)(salt[1]);
  90. this.v5l ^= (0, utils_js_1.byteSwapIfBE)(salt[2]);
  91. this.v5h ^= (0, utils_js_1.byteSwapIfBE)(salt[3]);
  92. }
  93. if (opts.personalization) {
  94. const pers = (0, utils_js_1.u32)((0, utils_js_1.toBytes)(opts.personalization));
  95. this.v6l ^= (0, utils_js_1.byteSwapIfBE)(pers[0]);
  96. this.v6h ^= (0, utils_js_1.byteSwapIfBE)(pers[1]);
  97. this.v7l ^= (0, utils_js_1.byteSwapIfBE)(pers[2]);
  98. this.v7h ^= (0, utils_js_1.byteSwapIfBE)(pers[3]);
  99. }
  100. if (opts.key) {
  101. // Pad to blockLen and update
  102. const tmp = new Uint8Array(this.blockLen);
  103. tmp.set((0, utils_js_1.toBytes)(opts.key));
  104. this.update(tmp);
  105. }
  106. }
  107. // prettier-ignore
  108. get() {
  109. let { v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h } = this;
  110. return [v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h];
  111. }
  112. // prettier-ignore
  113. set(v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h) {
  114. this.v0l = v0l | 0;
  115. this.v0h = v0h | 0;
  116. this.v1l = v1l | 0;
  117. this.v1h = v1h | 0;
  118. this.v2l = v2l | 0;
  119. this.v2h = v2h | 0;
  120. this.v3l = v3l | 0;
  121. this.v3h = v3h | 0;
  122. this.v4l = v4l | 0;
  123. this.v4h = v4h | 0;
  124. this.v5l = v5l | 0;
  125. this.v5h = v5h | 0;
  126. this.v6l = v6l | 0;
  127. this.v6h = v6h | 0;
  128. this.v7l = v7l | 0;
  129. this.v7h = v7h | 0;
  130. }
  131. compress(msg, offset, isLast) {
  132. this.get().forEach((v, i) => (BBUF[i] = v)); // First half from state.
  133. BBUF.set(B2B_IV, 16); // Second half from IV.
  134. let { h, l } = _u64_js_1.default.fromBig(BigInt(this.length));
  135. BBUF[24] = B2B_IV[8] ^ l; // Low word of the offset.
  136. BBUF[25] = B2B_IV[9] ^ h; // High word.
  137. // Invert all bits for last block
  138. if (isLast) {
  139. BBUF[28] = ~BBUF[28];
  140. BBUF[29] = ~BBUF[29];
  141. }
  142. let j = 0;
  143. const s = _blake_js_1.SIGMA;
  144. for (let i = 0; i < 12; i++) {
  145. G1b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
  146. G2b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
  147. G1b(1, 5, 9, 13, msg, offset + 2 * s[j++]);
  148. G2b(1, 5, 9, 13, msg, offset + 2 * s[j++]);
  149. G1b(2, 6, 10, 14, msg, offset + 2 * s[j++]);
  150. G2b(2, 6, 10, 14, msg, offset + 2 * s[j++]);
  151. G1b(3, 7, 11, 15, msg, offset + 2 * s[j++]);
  152. G2b(3, 7, 11, 15, msg, offset + 2 * s[j++]);
  153. G1b(0, 5, 10, 15, msg, offset + 2 * s[j++]);
  154. G2b(0, 5, 10, 15, msg, offset + 2 * s[j++]);
  155. G1b(1, 6, 11, 12, msg, offset + 2 * s[j++]);
  156. G2b(1, 6, 11, 12, msg, offset + 2 * s[j++]);
  157. G1b(2, 7, 8, 13, msg, offset + 2 * s[j++]);
  158. G2b(2, 7, 8, 13, msg, offset + 2 * s[j++]);
  159. G1b(3, 4, 9, 14, msg, offset + 2 * s[j++]);
  160. G2b(3, 4, 9, 14, msg, offset + 2 * s[j++]);
  161. }
  162. this.v0l ^= BBUF[0] ^ BBUF[16];
  163. this.v0h ^= BBUF[1] ^ BBUF[17];
  164. this.v1l ^= BBUF[2] ^ BBUF[18];
  165. this.v1h ^= BBUF[3] ^ BBUF[19];
  166. this.v2l ^= BBUF[4] ^ BBUF[20];
  167. this.v2h ^= BBUF[5] ^ BBUF[21];
  168. this.v3l ^= BBUF[6] ^ BBUF[22];
  169. this.v3h ^= BBUF[7] ^ BBUF[23];
  170. this.v4l ^= BBUF[8] ^ BBUF[24];
  171. this.v4h ^= BBUF[9] ^ BBUF[25];
  172. this.v5l ^= BBUF[10] ^ BBUF[26];
  173. this.v5h ^= BBUF[11] ^ BBUF[27];
  174. this.v6l ^= BBUF[12] ^ BBUF[28];
  175. this.v6h ^= BBUF[13] ^ BBUF[29];
  176. this.v7l ^= BBUF[14] ^ BBUF[30];
  177. this.v7h ^= BBUF[15] ^ BBUF[31];
  178. BBUF.fill(0);
  179. }
  180. destroy() {
  181. this.destroyed = true;
  182. this.buffer32.fill(0);
  183. this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  184. }
  185. }
  186. /**
  187. * BLAKE2b - optimized for 64-bit platforms. JS doesn't have uint64, so it's slower than BLAKE2s.
  188. * @param msg - message that would be hashed
  189. * @param opts - dkLen, key, salt, personalization
  190. */
  191. exports.blake2b = (0, utils_js_1.wrapConstructorWithOpts)((opts) => new BLAKE2b(opts));
  192. //# sourceMappingURL=blake2b.js.map