blake2s.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import { BLAKE, 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, b, c, d, x) {
  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, b, c, d, x) {
  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, offset, msg, rounds, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15) {
  27. let j = 0;
  28. for (let i = 0; i < rounds; i++) {
  29. ({ a: v0, b: v4, c: v8, d: v12 } = G1s(v0, v4, v8, v12, msg[offset + s[j++]]));
  30. ({ a: v0, b: v4, c: v8, d: v12 } = G2s(v0, v4, v8, v12, msg[offset + s[j++]]));
  31. ({ a: v1, b: v5, c: v9, d: v13 } = G1s(v1, v5, v9, v13, msg[offset + s[j++]]));
  32. ({ a: v1, b: v5, c: v9, d: v13 } = G2s(v1, v5, v9, v13, msg[offset + s[j++]]));
  33. ({ a: v2, b: v6, c: v10, d: v14 } = G1s(v2, v6, v10, v14, msg[offset + s[j++]]));
  34. ({ a: v2, b: v6, c: v10, d: v14 } = G2s(v2, v6, v10, v14, msg[offset + s[j++]]));
  35. ({ a: v3, b: v7, c: v11, d: v15 } = G1s(v3, v7, v11, v15, msg[offset + s[j++]]));
  36. ({ a: v3, b: v7, c: v11, d: v15 } = G2s(v3, v7, v11, v15, msg[offset + s[j++]]));
  37. ({ a: v0, b: v5, c: v10, d: v15 } = G1s(v0, v5, v10, v15, msg[offset + s[j++]]));
  38. ({ a: v0, b: v5, c: v10, d: v15 } = G2s(v0, v5, v10, v15, msg[offset + s[j++]]));
  39. ({ a: v1, b: v6, c: v11, d: v12 } = G1s(v1, v6, v11, v12, msg[offset + s[j++]]));
  40. ({ a: v1, b: v6, c: v11, d: v12 } = G2s(v1, v6, v11, v12, msg[offset + s[j++]]));
  41. ({ a: v2, b: v7, c: v8, d: v13 } = G1s(v2, v7, v8, v13, msg[offset + s[j++]]));
  42. ({ a: v2, b: v7, c: v8, d: v13 } = G2s(v2, v7, v8, v13, msg[offset + s[j++]]));
  43. ({ a: v3, b: v4, c: v9, d: v14 } = G1s(v3, v4, v9, v14, msg[offset + s[j++]]));
  44. ({ a: v3, b: v4, c: v9, d: v14 } = G2s(v3, v4, v9, v14, msg[offset + s[j++]]));
  45. }
  46. return { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 };
  47. }
  48. class BLAKE2s extends BLAKE {
  49. constructor(opts = {}) {
  50. super(64, opts.dkLen === undefined ? 32 : opts.dkLen, opts, 32, 8, 8);
  51. // Internal state, same as SHA-256
  52. this.v0 = B2S_IV[0] | 0;
  53. this.v1 = B2S_IV[1] | 0;
  54. this.v2 = B2S_IV[2] | 0;
  55. this.v3 = B2S_IV[3] | 0;
  56. this.v4 = B2S_IV[4] | 0;
  57. this.v5 = B2S_IV[5] | 0;
  58. this.v6 = B2S_IV[6] | 0;
  59. this.v7 = B2S_IV[7] | 0;
  60. const keyLength = opts.key ? opts.key.length : 0;
  61. this.v0 ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
  62. if (opts.salt) {
  63. const salt = u32(toBytes(opts.salt));
  64. this.v4 ^= byteSwapIfBE(salt[0]);
  65. this.v5 ^= byteSwapIfBE(salt[1]);
  66. }
  67. if (opts.personalization) {
  68. const pers = u32(toBytes(opts.personalization));
  69. this.v6 ^= byteSwapIfBE(pers[0]);
  70. this.v7 ^= byteSwapIfBE(pers[1]);
  71. }
  72. if (opts.key) {
  73. // Pad to blockLen and update
  74. const tmp = new Uint8Array(this.blockLen);
  75. tmp.set(toBytes(opts.key));
  76. this.update(tmp);
  77. }
  78. }
  79. get() {
  80. const { v0, v1, v2, v3, v4, v5, v6, v7 } = this;
  81. return [v0, v1, v2, v3, v4, v5, v6, v7];
  82. }
  83. // prettier-ignore
  84. set(v0, v1, v2, v3, v4, v5, v6, v7) {
  85. this.v0 = v0 | 0;
  86. this.v1 = v1 | 0;
  87. this.v2 = v2 | 0;
  88. this.v3 = v3 | 0;
  89. this.v4 = v4 | 0;
  90. this.v5 = v5 | 0;
  91. this.v6 = v6 | 0;
  92. this.v7 = v7 | 0;
  93. }
  94. compress(msg, offset, isLast) {
  95. const { h, l } = fromBig(BigInt(this.length));
  96. // prettier-ignore
  97. const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } = compress(SIGMA, offset, msg, 10, this.v0, this.v1, this.v2, this.v3, this.v4, this.v5, this.v6, this.v7, 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]);
  98. this.v0 ^= v0 ^ v8;
  99. this.v1 ^= v1 ^ v9;
  100. this.v2 ^= v2 ^ v10;
  101. this.v3 ^= v3 ^ v11;
  102. this.v4 ^= v4 ^ v12;
  103. this.v5 ^= v5 ^ v13;
  104. this.v6 ^= v6 ^ v14;
  105. this.v7 ^= v7 ^ v15;
  106. }
  107. destroy() {
  108. this.destroyed = true;
  109. this.buffer32.fill(0);
  110. this.set(0, 0, 0, 0, 0, 0, 0, 0);
  111. }
  112. }
  113. /**
  114. * BLAKE2s - optimized for 32-bit platforms. JS doesn't have uint64, so it's faster than BLAKE2b.
  115. * @param msg - message that would be hashed
  116. * @param opts - dkLen, key, salt, personalization
  117. */
  118. export const blake2s = /* @__PURE__ */ wrapConstructorWithOpts((opts) => new BLAKE2s(opts));
  119. //# sourceMappingURL=blake2s.js.map