blake2s.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { BLAKE, BlakeOpts, SIGMA } from './_blake.js';
  2. import { fromBig } from './_u64.js';
  3. import { rotr, toBytes, wrapConstructorWithOpts, u32, byteSwapIfBE } from './utils.js';
  4. // Initial state: same as SHA256
  5. // first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19
  6. // prettier-ignore
  7. export const B2S_IV = /* @__PURE__ */ new Uint32Array([
  8. 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
  9. ]);
  10. // Mixing function G splitted in two halfs
  11. function G1s(a: number, b: number, c: number, d: number, x: number) {
  12. a = (a + b + x) | 0;
  13. d = rotr(d ^ a, 16);
  14. c = (c + d) | 0;
  15. b = rotr(b ^ c, 12);
  16. return { a, b, c, d };
  17. }
  18. function G2s(a: number, b: number, c: number, d: number, x: number) {
  19. a = (a + b + x) | 0;
  20. d = rotr(d ^ a, 8);
  21. c = (c + d) | 0;
  22. b = rotr(b ^ c, 7);
  23. return { a, b, c, d };
  24. }
  25. // prettier-ignore
  26. export function compress(s: Uint8Array, offset: number, msg: Uint32Array, rounds: number,
  27. v0: number, v1: number, v2: number, v3: number, v4: number, v5: number, v6: number, v7: number,
  28. v8: number, v9: number, v10: number, v11: number, v12: number, v13: number, v14: number, v15: number,
  29. ) {
  30. let j = 0;
  31. for (let i = 0; i < rounds; i++) {
  32. ({ a: v0, b: v4, c: v8, d: v12 } = G1s(v0, v4, v8, v12, msg[offset + s[j++]]));
  33. ({ a: v0, b: v4, c: v8, d: v12 } = G2s(v0, v4, v8, v12, msg[offset + s[j++]]));
  34. ({ a: v1, b: v5, c: v9, d: v13 } = G1s(v1, v5, v9, v13, msg[offset + s[j++]]));
  35. ({ a: v1, b: v5, c: v9, d: v13 } = G2s(v1, v5, v9, v13, msg[offset + s[j++]]));
  36. ({ a: v2, b: v6, c: v10, d: v14 } = G1s(v2, v6, v10, v14, msg[offset + s[j++]]));
  37. ({ a: v2, b: v6, c: v10, d: v14 } = G2s(v2, v6, v10, v14, msg[offset + s[j++]]));
  38. ({ a: v3, b: v7, c: v11, d: v15 } = G1s(v3, v7, v11, v15, msg[offset + s[j++]]));
  39. ({ a: v3, b: v7, c: v11, d: v15 } = G2s(v3, v7, v11, v15, msg[offset + s[j++]]));
  40. ({ a: v0, b: v5, c: v10, d: v15 } = G1s(v0, v5, v10, v15, msg[offset + s[j++]]));
  41. ({ a: v0, b: v5, c: v10, d: v15 } = G2s(v0, v5, v10, v15, msg[offset + s[j++]]));
  42. ({ a: v1, b: v6, c: v11, d: v12 } = G1s(v1, v6, v11, v12, msg[offset + s[j++]]));
  43. ({ a: v1, b: v6, c: v11, d: v12 } = G2s(v1, v6, v11, v12, msg[offset + s[j++]]));
  44. ({ a: v2, b: v7, c: v8, d: v13 } = G1s(v2, v7, v8, v13, msg[offset + s[j++]]));
  45. ({ a: v2, b: v7, c: v8, d: v13 } = G2s(v2, v7, v8, v13, msg[offset + s[j++]]));
  46. ({ a: v3, b: v4, c: v9, d: v14 } = G1s(v3, v4, v9, v14, msg[offset + s[j++]]));
  47. ({ a: v3, b: v4, c: v9, d: v14 } = G2s(v3, v4, v9, v14, msg[offset + s[j++]]));
  48. }
  49. return { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 };
  50. }
  51. class BLAKE2s extends BLAKE<BLAKE2s> {
  52. // Internal state, same as SHA-256
  53. private v0 = B2S_IV[0] | 0;
  54. private v1 = B2S_IV[1] | 0;
  55. private v2 = B2S_IV[2] | 0;
  56. private v3 = B2S_IV[3] | 0;
  57. private v4 = B2S_IV[4] | 0;
  58. private v5 = B2S_IV[5] | 0;
  59. private v6 = B2S_IV[6] | 0;
  60. private v7 = B2S_IV[7] | 0;
  61. constructor(opts: BlakeOpts = {}) {
  62. super(64, opts.dkLen === undefined ? 32 : opts.dkLen, opts, 32, 8, 8);
  63. const keyLength = opts.key ? opts.key.length : 0;
  64. this.v0 ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
  65. if (opts.salt) {
  66. const salt = u32(toBytes(opts.salt));
  67. this.v4 ^= byteSwapIfBE(salt[0]);
  68. this.v5 ^= byteSwapIfBE(salt[1]);
  69. }
  70. if (opts.personalization) {
  71. const pers = u32(toBytes(opts.personalization));
  72. this.v6 ^= byteSwapIfBE(pers[0]);
  73. this.v7 ^= byteSwapIfBE(pers[1]);
  74. }
  75. if (opts.key) {
  76. // Pad to blockLen and update
  77. const tmp = new Uint8Array(this.blockLen);
  78. tmp.set(toBytes(opts.key));
  79. this.update(tmp);
  80. }
  81. }
  82. protected get(): [number, number, number, number, number, number, number, number] {
  83. const { v0, v1, v2, v3, v4, v5, v6, v7 } = this;
  84. return [v0, v1, v2, v3, v4, v5, v6, v7];
  85. }
  86. // prettier-ignore
  87. protected set(
  88. v0: number, v1: number, v2: number, v3: number, v4: number, v5: number, v6: number, v7: number
  89. ) {
  90. this.v0 = v0 | 0;
  91. this.v1 = v1 | 0;
  92. this.v2 = v2 | 0;
  93. this.v3 = v3 | 0;
  94. this.v4 = v4 | 0;
  95. this.v5 = v5 | 0;
  96. this.v6 = v6 | 0;
  97. this.v7 = v7 | 0;
  98. }
  99. protected compress(msg: Uint32Array, offset: number, isLast: boolean) {
  100. const { h, l } = fromBig(BigInt(this.length));
  101. // prettier-ignore
  102. const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } =
  103. compress(
  104. SIGMA, offset, msg, 10,
  105. this.v0, this.v1, this.v2, this.v3, this.v4, this.v5, this.v6, this.v7,
  106. B2S_IV[0], B2S_IV[1], B2S_IV[2], B2S_IV[3], l ^ B2S_IV[4], h ^ B2S_IV[5], isLast ? ~B2S_IV[6] : B2S_IV[6], B2S_IV[7]
  107. );
  108. this.v0 ^= v0 ^ v8;
  109. this.v1 ^= v1 ^ v9;
  110. this.v2 ^= v2 ^ v10;
  111. this.v3 ^= v3 ^ v11;
  112. this.v4 ^= v4 ^ v12;
  113. this.v5 ^= v5 ^ v13;
  114. this.v6 ^= v6 ^ v14;
  115. this.v7 ^= v7 ^ v15;
  116. }
  117. destroy() {
  118. this.destroyed = true;
  119. this.buffer32.fill(0);
  120. this.set(0, 0, 0, 0, 0, 0, 0, 0);
  121. }
  122. }
  123. /**
  124. * BLAKE2s - optimized for 32-bit platforms. JS doesn't have uint64, so it's faster than BLAKE2b.
  125. * @param msg - message that would be hashed
  126. * @param opts - dkLen, key, salt, personalization
  127. */
  128. export const blake2s = /* @__PURE__ */ wrapConstructorWithOpts<BLAKE2s, BlakeOpts>(
  129. (opts) => new BLAKE2s(opts)
  130. );