sha512.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.sha384 = exports.sha512_256 = exports.sha512_224 = exports.sha512 = exports.SHA512 = void 0;
  4. const _md_js_1 = require("./_md.js");
  5. const _u64_js_1 = require("./_u64.js");
  6. const utils_js_1 = require("./utils.js");
  7. // Round contants (first 32 bits of the fractional parts of the cube roots of the first 80 primes 2..409):
  8. // prettier-ignore
  9. const [SHA512_Kh, SHA512_Kl] = /* @__PURE__ */ (() => _u64_js_1.default.split([
  10. '0x428a2f98d728ae22', '0x7137449123ef65cd', '0xb5c0fbcfec4d3b2f', '0xe9b5dba58189dbbc',
  11. '0x3956c25bf348b538', '0x59f111f1b605d019', '0x923f82a4af194f9b', '0xab1c5ed5da6d8118',
  12. '0xd807aa98a3030242', '0x12835b0145706fbe', '0x243185be4ee4b28c', '0x550c7dc3d5ffb4e2',
  13. '0x72be5d74f27b896f', '0x80deb1fe3b1696b1', '0x9bdc06a725c71235', '0xc19bf174cf692694',
  14. '0xe49b69c19ef14ad2', '0xefbe4786384f25e3', '0x0fc19dc68b8cd5b5', '0x240ca1cc77ac9c65',
  15. '0x2de92c6f592b0275', '0x4a7484aa6ea6e483', '0x5cb0a9dcbd41fbd4', '0x76f988da831153b5',
  16. '0x983e5152ee66dfab', '0xa831c66d2db43210', '0xb00327c898fb213f', '0xbf597fc7beef0ee4',
  17. '0xc6e00bf33da88fc2', '0xd5a79147930aa725', '0x06ca6351e003826f', '0x142929670a0e6e70',
  18. '0x27b70a8546d22ffc', '0x2e1b21385c26c926', '0x4d2c6dfc5ac42aed', '0x53380d139d95b3df',
  19. '0x650a73548baf63de', '0x766a0abb3c77b2a8', '0x81c2c92e47edaee6', '0x92722c851482353b',
  20. '0xa2bfe8a14cf10364', '0xa81a664bbc423001', '0xc24b8b70d0f89791', '0xc76c51a30654be30',
  21. '0xd192e819d6ef5218', '0xd69906245565a910', '0xf40e35855771202a', '0x106aa07032bbd1b8',
  22. '0x19a4c116b8d2d0c8', '0x1e376c085141ab53', '0x2748774cdf8eeb99', '0x34b0bcb5e19b48a8',
  23. '0x391c0cb3c5c95a63', '0x4ed8aa4ae3418acb', '0x5b9cca4f7763e373', '0x682e6ff3d6b2b8a3',
  24. '0x748f82ee5defb2fc', '0x78a5636f43172f60', '0x84c87814a1f0ab72', '0x8cc702081a6439ec',
  25. '0x90befffa23631e28', '0xa4506cebde82bde9', '0xbef9a3f7b2c67915', '0xc67178f2e372532b',
  26. '0xca273eceea26619c', '0xd186b8c721c0c207', '0xeada7dd6cde0eb1e', '0xf57d4f7fee6ed178',
  27. '0x06f067aa72176fba', '0x0a637dc5a2c898a6', '0x113f9804bef90dae', '0x1b710b35131c471b',
  28. '0x28db77f523047d84', '0x32caab7b40c72493', '0x3c9ebe0a15c9bebc', '0x431d67c49c100d4c',
  29. '0x4cc5d4becb3e42b6', '0x597f299cfc657e2a', '0x5fcb6fab3ad6faec', '0x6c44198c4a475817'
  30. ].map(n => BigInt(n))))();
  31. // Temporary buffer, not used to store anything between runs
  32. const SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
  33. const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
  34. class SHA512 extends _md_js_1.HashMD {
  35. constructor() {
  36. super(128, 64, 16, false);
  37. // We cannot use array here since array allows indexing by variable which means optimizer/compiler cannot use registers.
  38. // Also looks cleaner and easier to verify with spec.
  39. // Initial state (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
  40. // h -- high 32 bits, l -- low 32 bits
  41. this.Ah = 0x6a09e667 | 0;
  42. this.Al = 0xf3bcc908 | 0;
  43. this.Bh = 0xbb67ae85 | 0;
  44. this.Bl = 0x84caa73b | 0;
  45. this.Ch = 0x3c6ef372 | 0;
  46. this.Cl = 0xfe94f82b | 0;
  47. this.Dh = 0xa54ff53a | 0;
  48. this.Dl = 0x5f1d36f1 | 0;
  49. this.Eh = 0x510e527f | 0;
  50. this.El = 0xade682d1 | 0;
  51. this.Fh = 0x9b05688c | 0;
  52. this.Fl = 0x2b3e6c1f | 0;
  53. this.Gh = 0x1f83d9ab | 0;
  54. this.Gl = 0xfb41bd6b | 0;
  55. this.Hh = 0x5be0cd19 | 0;
  56. this.Hl = 0x137e2179 | 0;
  57. }
  58. // prettier-ignore
  59. get() {
  60. const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
  61. return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
  62. }
  63. // prettier-ignore
  64. set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl) {
  65. this.Ah = Ah | 0;
  66. this.Al = Al | 0;
  67. this.Bh = Bh | 0;
  68. this.Bl = Bl | 0;
  69. this.Ch = Ch | 0;
  70. this.Cl = Cl | 0;
  71. this.Dh = Dh | 0;
  72. this.Dl = Dl | 0;
  73. this.Eh = Eh | 0;
  74. this.El = El | 0;
  75. this.Fh = Fh | 0;
  76. this.Fl = Fl | 0;
  77. this.Gh = Gh | 0;
  78. this.Gl = Gl | 0;
  79. this.Hh = Hh | 0;
  80. this.Hl = Hl | 0;
  81. }
  82. process(view, offset) {
  83. // Extend the first 16 words into the remaining 64 words w[16..79] of the message schedule array
  84. for (let i = 0; i < 16; i++, offset += 4) {
  85. SHA512_W_H[i] = view.getUint32(offset);
  86. SHA512_W_L[i] = view.getUint32((offset += 4));
  87. }
  88. for (let i = 16; i < 80; i++) {
  89. // s0 := (w[i-15] rightrotate 1) xor (w[i-15] rightrotate 8) xor (w[i-15] rightshift 7)
  90. const W15h = SHA512_W_H[i - 15] | 0;
  91. const W15l = SHA512_W_L[i - 15] | 0;
  92. const s0h = _u64_js_1.default.rotrSH(W15h, W15l, 1) ^ _u64_js_1.default.rotrSH(W15h, W15l, 8) ^ _u64_js_1.default.shrSH(W15h, W15l, 7);
  93. const s0l = _u64_js_1.default.rotrSL(W15h, W15l, 1) ^ _u64_js_1.default.rotrSL(W15h, W15l, 8) ^ _u64_js_1.default.shrSL(W15h, W15l, 7);
  94. // s1 := (w[i-2] rightrotate 19) xor (w[i-2] rightrotate 61) xor (w[i-2] rightshift 6)
  95. const W2h = SHA512_W_H[i - 2] | 0;
  96. const W2l = SHA512_W_L[i - 2] | 0;
  97. const s1h = _u64_js_1.default.rotrSH(W2h, W2l, 19) ^ _u64_js_1.default.rotrBH(W2h, W2l, 61) ^ _u64_js_1.default.shrSH(W2h, W2l, 6);
  98. const s1l = _u64_js_1.default.rotrSL(W2h, W2l, 19) ^ _u64_js_1.default.rotrBL(W2h, W2l, 61) ^ _u64_js_1.default.shrSL(W2h, W2l, 6);
  99. // SHA256_W[i] = s0 + s1 + SHA256_W[i - 7] + SHA256_W[i - 16];
  100. const SUMl = _u64_js_1.default.add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
  101. const SUMh = _u64_js_1.default.add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
  102. SHA512_W_H[i] = SUMh | 0;
  103. SHA512_W_L[i] = SUMl | 0;
  104. }
  105. let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
  106. // Compression function main loop, 80 rounds
  107. for (let i = 0; i < 80; i++) {
  108. // S1 := (e rightrotate 14) xor (e rightrotate 18) xor (e rightrotate 41)
  109. const sigma1h = _u64_js_1.default.rotrSH(Eh, El, 14) ^ _u64_js_1.default.rotrSH(Eh, El, 18) ^ _u64_js_1.default.rotrBH(Eh, El, 41);
  110. const sigma1l = _u64_js_1.default.rotrSL(Eh, El, 14) ^ _u64_js_1.default.rotrSL(Eh, El, 18) ^ _u64_js_1.default.rotrBL(Eh, El, 41);
  111. //const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
  112. const CHIh = (Eh & Fh) ^ (~Eh & Gh);
  113. const CHIl = (El & Fl) ^ (~El & Gl);
  114. // T1 = H + sigma1 + Chi(E, F, G) + SHA512_K[i] + SHA512_W[i]
  115. // prettier-ignore
  116. const T1ll = _u64_js_1.default.add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
  117. const T1h = _u64_js_1.default.add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
  118. const T1l = T1ll | 0;
  119. // S0 := (a rightrotate 28) xor (a rightrotate 34) xor (a rightrotate 39)
  120. const sigma0h = _u64_js_1.default.rotrSH(Ah, Al, 28) ^ _u64_js_1.default.rotrBH(Ah, Al, 34) ^ _u64_js_1.default.rotrBH(Ah, Al, 39);
  121. const sigma0l = _u64_js_1.default.rotrSL(Ah, Al, 28) ^ _u64_js_1.default.rotrBL(Ah, Al, 34) ^ _u64_js_1.default.rotrBL(Ah, Al, 39);
  122. const MAJh = (Ah & Bh) ^ (Ah & Ch) ^ (Bh & Ch);
  123. const MAJl = (Al & Bl) ^ (Al & Cl) ^ (Bl & Cl);
  124. Hh = Gh | 0;
  125. Hl = Gl | 0;
  126. Gh = Fh | 0;
  127. Gl = Fl | 0;
  128. Fh = Eh | 0;
  129. Fl = El | 0;
  130. ({ h: Eh, l: El } = _u64_js_1.default.add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
  131. Dh = Ch | 0;
  132. Dl = Cl | 0;
  133. Ch = Bh | 0;
  134. Cl = Bl | 0;
  135. Bh = Ah | 0;
  136. Bl = Al | 0;
  137. const All = _u64_js_1.default.add3L(T1l, sigma0l, MAJl);
  138. Ah = _u64_js_1.default.add3H(All, T1h, sigma0h, MAJh);
  139. Al = All | 0;
  140. }
  141. // Add the compressed chunk to the current hash value
  142. ({ h: Ah, l: Al } = _u64_js_1.default.add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
  143. ({ h: Bh, l: Bl } = _u64_js_1.default.add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
  144. ({ h: Ch, l: Cl } = _u64_js_1.default.add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
  145. ({ h: Dh, l: Dl } = _u64_js_1.default.add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
  146. ({ h: Eh, l: El } = _u64_js_1.default.add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
  147. ({ h: Fh, l: Fl } = _u64_js_1.default.add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
  148. ({ h: Gh, l: Gl } = _u64_js_1.default.add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
  149. ({ h: Hh, l: Hl } = _u64_js_1.default.add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
  150. this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
  151. }
  152. roundClean() {
  153. SHA512_W_H.fill(0);
  154. SHA512_W_L.fill(0);
  155. }
  156. destroy() {
  157. this.buffer.fill(0);
  158. this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  159. }
  160. }
  161. exports.SHA512 = SHA512;
  162. class SHA512_224 extends SHA512 {
  163. constructor() {
  164. super();
  165. // h -- high 32 bits, l -- low 32 bits
  166. this.Ah = 0x8c3d37c8 | 0;
  167. this.Al = 0x19544da2 | 0;
  168. this.Bh = 0x73e19966 | 0;
  169. this.Bl = 0x89dcd4d6 | 0;
  170. this.Ch = 0x1dfab7ae | 0;
  171. this.Cl = 0x32ff9c82 | 0;
  172. this.Dh = 0x679dd514 | 0;
  173. this.Dl = 0x582f9fcf | 0;
  174. this.Eh = 0x0f6d2b69 | 0;
  175. this.El = 0x7bd44da8 | 0;
  176. this.Fh = 0x77e36f73 | 0;
  177. this.Fl = 0x04c48942 | 0;
  178. this.Gh = 0x3f9d85a8 | 0;
  179. this.Gl = 0x6a1d36c8 | 0;
  180. this.Hh = 0x1112e6ad | 0;
  181. this.Hl = 0x91d692a1 | 0;
  182. this.outputLen = 28;
  183. }
  184. }
  185. class SHA512_256 extends SHA512 {
  186. constructor() {
  187. super();
  188. // h -- high 32 bits, l -- low 32 bits
  189. this.Ah = 0x22312194 | 0;
  190. this.Al = 0xfc2bf72c | 0;
  191. this.Bh = 0x9f555fa3 | 0;
  192. this.Bl = 0xc84c64c2 | 0;
  193. this.Ch = 0x2393b86b | 0;
  194. this.Cl = 0x6f53b151 | 0;
  195. this.Dh = 0x96387719 | 0;
  196. this.Dl = 0x5940eabd | 0;
  197. this.Eh = 0x96283ee2 | 0;
  198. this.El = 0xa88effe3 | 0;
  199. this.Fh = 0xbe5e1e25 | 0;
  200. this.Fl = 0x53863992 | 0;
  201. this.Gh = 0x2b0199fc | 0;
  202. this.Gl = 0x2c85b8aa | 0;
  203. this.Hh = 0x0eb72ddc | 0;
  204. this.Hl = 0x81c52ca2 | 0;
  205. this.outputLen = 32;
  206. }
  207. }
  208. class SHA384 extends SHA512 {
  209. constructor() {
  210. super();
  211. // h -- high 32 bits, l -- low 32 bits
  212. this.Ah = 0xcbbb9d5d | 0;
  213. this.Al = 0xc1059ed8 | 0;
  214. this.Bh = 0x629a292a | 0;
  215. this.Bl = 0x367cd507 | 0;
  216. this.Ch = 0x9159015a | 0;
  217. this.Cl = 0x3070dd17 | 0;
  218. this.Dh = 0x152fecd8 | 0;
  219. this.Dl = 0xf70e5939 | 0;
  220. this.Eh = 0x67332667 | 0;
  221. this.El = 0xffc00b31 | 0;
  222. this.Fh = 0x8eb44a87 | 0;
  223. this.Fl = 0x68581511 | 0;
  224. this.Gh = 0xdb0c2e0d | 0;
  225. this.Gl = 0x64f98fa7 | 0;
  226. this.Hh = 0x47b5481d | 0;
  227. this.Hl = 0xbefa4fa4 | 0;
  228. this.outputLen = 48;
  229. }
  230. }
  231. exports.sha512 = (0, utils_js_1.wrapConstructor)(() => new SHA512());
  232. exports.sha512_224 = (0, utils_js_1.wrapConstructor)(() => new SHA512_224());
  233. exports.sha512_256 = (0, utils_js_1.wrapConstructor)(() => new SHA512_256());
  234. exports.sha384 = (0, utils_js_1.wrapConstructor)(() => new SHA384());
  235. //# sourceMappingURL=sha512.js.map