sha3.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { bytes, exists, number, output } from './_assert.js';
  2. import { rotlBH, rotlBL, rotlSH, rotlSL, split } from './_u64.js';
  3. import { Hash, u32, toBytes, wrapConstructor, wrapXOFConstructorWithOpts, isLE, byteSwap32, } from './utils.js';
  4. // SHA3 (keccak) is based on a new design: basically, the internal state is bigger than output size.
  5. // It's called a sponge function.
  6. // Various per round constants calculations
  7. const SHA3_PI = [];
  8. const SHA3_ROTL = [];
  9. const _SHA3_IOTA = [];
  10. const _0n = /* @__PURE__ */ BigInt(0);
  11. const _1n = /* @__PURE__ */ BigInt(1);
  12. const _2n = /* @__PURE__ */ BigInt(2);
  13. const _7n = /* @__PURE__ */ BigInt(7);
  14. const _256n = /* @__PURE__ */ BigInt(256);
  15. const _0x71n = /* @__PURE__ */ BigInt(0x71);
  16. for (let round = 0, R = _1n, x = 1, y = 0; round < 24; round++) {
  17. // Pi
  18. [x, y] = [y, (2 * x + 3 * y) % 5];
  19. SHA3_PI.push(2 * (5 * y + x));
  20. // Rotational
  21. SHA3_ROTL.push((((round + 1) * (round + 2)) / 2) % 64);
  22. // Iota
  23. let t = _0n;
  24. for (let j = 0; j < 7; j++) {
  25. R = ((R << _1n) ^ ((R >> _7n) * _0x71n)) % _256n;
  26. if (R & _2n)
  27. t ^= _1n << ((_1n << /* @__PURE__ */ BigInt(j)) - _1n);
  28. }
  29. _SHA3_IOTA.push(t);
  30. }
  31. const [SHA3_IOTA_H, SHA3_IOTA_L] = /* @__PURE__ */ split(_SHA3_IOTA, true);
  32. // Left rotation (without 0, 32, 64)
  33. const rotlH = (h, l, s) => (s > 32 ? rotlBH(h, l, s) : rotlSH(h, l, s));
  34. const rotlL = (h, l, s) => (s > 32 ? rotlBL(h, l, s) : rotlSL(h, l, s));
  35. // Same as keccakf1600, but allows to skip some rounds
  36. export function keccakP(s, rounds = 24) {
  37. const B = new Uint32Array(5 * 2);
  38. // NOTE: all indices are x2 since we store state as u32 instead of u64 (bigints to slow in js)
  39. for (let round = 24 - rounds; round < 24; round++) {
  40. // Theta θ
  41. for (let x = 0; x < 10; x++)
  42. B[x] = s[x] ^ s[x + 10] ^ s[x + 20] ^ s[x + 30] ^ s[x + 40];
  43. for (let x = 0; x < 10; x += 2) {
  44. const idx1 = (x + 8) % 10;
  45. const idx0 = (x + 2) % 10;
  46. const B0 = B[idx0];
  47. const B1 = B[idx0 + 1];
  48. const Th = rotlH(B0, B1, 1) ^ B[idx1];
  49. const Tl = rotlL(B0, B1, 1) ^ B[idx1 + 1];
  50. for (let y = 0; y < 50; y += 10) {
  51. s[x + y] ^= Th;
  52. s[x + y + 1] ^= Tl;
  53. }
  54. }
  55. // Rho (ρ) and Pi (π)
  56. let curH = s[2];
  57. let curL = s[3];
  58. for (let t = 0; t < 24; t++) {
  59. const shift = SHA3_ROTL[t];
  60. const Th = rotlH(curH, curL, shift);
  61. const Tl = rotlL(curH, curL, shift);
  62. const PI = SHA3_PI[t];
  63. curH = s[PI];
  64. curL = s[PI + 1];
  65. s[PI] = Th;
  66. s[PI + 1] = Tl;
  67. }
  68. // Chi (χ)
  69. for (let y = 0; y < 50; y += 10) {
  70. for (let x = 0; x < 10; x++)
  71. B[x] = s[y + x];
  72. for (let x = 0; x < 10; x++)
  73. s[y + x] ^= ~B[(x + 2) % 10] & B[(x + 4) % 10];
  74. }
  75. // Iota (ι)
  76. s[0] ^= SHA3_IOTA_H[round];
  77. s[1] ^= SHA3_IOTA_L[round];
  78. }
  79. B.fill(0);
  80. }
  81. export class Keccak extends Hash {
  82. // NOTE: we accept arguments in bytes instead of bits here.
  83. constructor(blockLen, suffix, outputLen, enableXOF = false, rounds = 24) {
  84. super();
  85. this.blockLen = blockLen;
  86. this.suffix = suffix;
  87. this.outputLen = outputLen;
  88. this.enableXOF = enableXOF;
  89. this.rounds = rounds;
  90. this.pos = 0;
  91. this.posOut = 0;
  92. this.finished = false;
  93. this.destroyed = false;
  94. // Can be passed from user as dkLen
  95. number(outputLen);
  96. // 1600 = 5x5 matrix of 64bit. 1600 bits === 200 bytes
  97. if (0 >= this.blockLen || this.blockLen >= 200)
  98. throw new Error('Sha3 supports only keccak-f1600 function');
  99. this.state = new Uint8Array(200);
  100. this.state32 = u32(this.state);
  101. }
  102. keccak() {
  103. if (!isLE)
  104. byteSwap32(this.state32);
  105. keccakP(this.state32, this.rounds);
  106. if (!isLE)
  107. byteSwap32(this.state32);
  108. this.posOut = 0;
  109. this.pos = 0;
  110. }
  111. update(data) {
  112. exists(this);
  113. const { blockLen, state } = this;
  114. data = toBytes(data);
  115. const len = data.length;
  116. for (let pos = 0; pos < len;) {
  117. const take = Math.min(blockLen - this.pos, len - pos);
  118. for (let i = 0; i < take; i++)
  119. state[this.pos++] ^= data[pos++];
  120. if (this.pos === blockLen)
  121. this.keccak();
  122. }
  123. return this;
  124. }
  125. finish() {
  126. if (this.finished)
  127. return;
  128. this.finished = true;
  129. const { state, suffix, pos, blockLen } = this;
  130. // Do the padding
  131. state[pos] ^= suffix;
  132. if ((suffix & 0x80) !== 0 && pos === blockLen - 1)
  133. this.keccak();
  134. state[blockLen - 1] ^= 0x80;
  135. this.keccak();
  136. }
  137. writeInto(out) {
  138. exists(this, false);
  139. bytes(out);
  140. this.finish();
  141. const bufferOut = this.state;
  142. const { blockLen } = this;
  143. for (let pos = 0, len = out.length; pos < len;) {
  144. if (this.posOut >= blockLen)
  145. this.keccak();
  146. const take = Math.min(blockLen - this.posOut, len - pos);
  147. out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
  148. this.posOut += take;
  149. pos += take;
  150. }
  151. return out;
  152. }
  153. xofInto(out) {
  154. // Sha3/Keccak usage with XOF is probably mistake, only SHAKE instances can do XOF
  155. if (!this.enableXOF)
  156. throw new Error('XOF is not possible for this instance');
  157. return this.writeInto(out);
  158. }
  159. xof(bytes) {
  160. number(bytes);
  161. return this.xofInto(new Uint8Array(bytes));
  162. }
  163. digestInto(out) {
  164. output(out, this);
  165. if (this.finished)
  166. throw new Error('digest() was already called');
  167. this.writeInto(out);
  168. this.destroy();
  169. return out;
  170. }
  171. digest() {
  172. return this.digestInto(new Uint8Array(this.outputLen));
  173. }
  174. destroy() {
  175. this.destroyed = true;
  176. this.state.fill(0);
  177. }
  178. _cloneInto(to) {
  179. const { blockLen, suffix, outputLen, rounds, enableXOF } = this;
  180. to || (to = new Keccak(blockLen, suffix, outputLen, enableXOF, rounds));
  181. to.state32.set(this.state32);
  182. to.pos = this.pos;
  183. to.posOut = this.posOut;
  184. to.finished = this.finished;
  185. to.rounds = rounds;
  186. // Suffix can change in cSHAKE
  187. to.suffix = suffix;
  188. to.outputLen = outputLen;
  189. to.enableXOF = enableXOF;
  190. to.destroyed = this.destroyed;
  191. return to;
  192. }
  193. }
  194. const gen = (suffix, blockLen, outputLen) => wrapConstructor(() => new Keccak(blockLen, suffix, outputLen));
  195. export const sha3_224 = /* @__PURE__ */ gen(0x06, 144, 224 / 8);
  196. /**
  197. * SHA3-256 hash function
  198. * @param message - that would be hashed
  199. */
  200. export const sha3_256 = /* @__PURE__ */ gen(0x06, 136, 256 / 8);
  201. export const sha3_384 = /* @__PURE__ */ gen(0x06, 104, 384 / 8);
  202. export const sha3_512 = /* @__PURE__ */ gen(0x06, 72, 512 / 8);
  203. export const keccak_224 = /* @__PURE__ */ gen(0x01, 144, 224 / 8);
  204. /**
  205. * keccak-256 hash function. Different from SHA3-256.
  206. * @param message - that would be hashed
  207. */
  208. export const keccak_256 = /* @__PURE__ */ gen(0x01, 136, 256 / 8);
  209. export const keccak_384 = /* @__PURE__ */ gen(0x01, 104, 384 / 8);
  210. export const keccak_512 = /* @__PURE__ */ gen(0x01, 72, 512 / 8);
  211. const genShake = (suffix, blockLen, outputLen) => wrapXOFConstructorWithOpts((opts = {}) => new Keccak(blockLen, suffix, opts.dkLen === undefined ? outputLen : opts.dkLen, true));
  212. export const shake128 = /* @__PURE__ */ genShake(0x1f, 168, 128 / 8);
  213. export const shake256 = /* @__PURE__ */ genShake(0x1f, 136, 256 / 8);
  214. //# sourceMappingURL=sha3.js.map