blake2s.js 5.6 KB

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