blake2b.ts 7.6 KB

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