sha512.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import { Hasher } from './core.js';
  2. import {
  3. X64Word,
  4. X64WordArray,
  5. } from './x64-core.js';
  6. // Constants
  7. const K = [
  8. new X64Word(0x428a2f98, 0xd728ae22),
  9. new X64Word(0x71374491, 0x23ef65cd),
  10. new X64Word(0xb5c0fbcf, 0xec4d3b2f),
  11. new X64Word(0xe9b5dba5, 0x8189dbbc),
  12. new X64Word(0x3956c25b, 0xf348b538),
  13. new X64Word(0x59f111f1, 0xb605d019),
  14. new X64Word(0x923f82a4, 0xaf194f9b),
  15. new X64Word(0xab1c5ed5, 0xda6d8118),
  16. new X64Word(0xd807aa98, 0xa3030242),
  17. new X64Word(0x12835b01, 0x45706fbe),
  18. new X64Word(0x243185be, 0x4ee4b28c),
  19. new X64Word(0x550c7dc3, 0xd5ffb4e2),
  20. new X64Word(0x72be5d74, 0xf27b896f),
  21. new X64Word(0x80deb1fe, 0x3b1696b1),
  22. new X64Word(0x9bdc06a7, 0x25c71235),
  23. new X64Word(0xc19bf174, 0xcf692694),
  24. new X64Word(0xe49b69c1, 0x9ef14ad2),
  25. new X64Word(0xefbe4786, 0x384f25e3),
  26. new X64Word(0x0fc19dc6, 0x8b8cd5b5),
  27. new X64Word(0x240ca1cc, 0x77ac9c65),
  28. new X64Word(0x2de92c6f, 0x592b0275),
  29. new X64Word(0x4a7484aa, 0x6ea6e483),
  30. new X64Word(0x5cb0a9dc, 0xbd41fbd4),
  31. new X64Word(0x76f988da, 0x831153b5),
  32. new X64Word(0x983e5152, 0xee66dfab),
  33. new X64Word(0xa831c66d, 0x2db43210),
  34. new X64Word(0xb00327c8, 0x98fb213f),
  35. new X64Word(0xbf597fc7, 0xbeef0ee4),
  36. new X64Word(0xc6e00bf3, 0x3da88fc2),
  37. new X64Word(0xd5a79147, 0x930aa725),
  38. new X64Word(0x06ca6351, 0xe003826f),
  39. new X64Word(0x14292967, 0x0a0e6e70),
  40. new X64Word(0x27b70a85, 0x46d22ffc),
  41. new X64Word(0x2e1b2138, 0x5c26c926),
  42. new X64Word(0x4d2c6dfc, 0x5ac42aed),
  43. new X64Word(0x53380d13, 0x9d95b3df),
  44. new X64Word(0x650a7354, 0x8baf63de),
  45. new X64Word(0x766a0abb, 0x3c77b2a8),
  46. new X64Word(0x81c2c92e, 0x47edaee6),
  47. new X64Word(0x92722c85, 0x1482353b),
  48. new X64Word(0xa2bfe8a1, 0x4cf10364),
  49. new X64Word(0xa81a664b, 0xbc423001),
  50. new X64Word(0xc24b8b70, 0xd0f89791),
  51. new X64Word(0xc76c51a3, 0x0654be30),
  52. new X64Word(0xd192e819, 0xd6ef5218),
  53. new X64Word(0xd6990624, 0x5565a910),
  54. new X64Word(0xf40e3585, 0x5771202a),
  55. new X64Word(0x106aa070, 0x32bbd1b8),
  56. new X64Word(0x19a4c116, 0xb8d2d0c8),
  57. new X64Word(0x1e376c08, 0x5141ab53),
  58. new X64Word(0x2748774c, 0xdf8eeb99),
  59. new X64Word(0x34b0bcb5, 0xe19b48a8),
  60. new X64Word(0x391c0cb3, 0xc5c95a63),
  61. new X64Word(0x4ed8aa4a, 0xe3418acb),
  62. new X64Word(0x5b9cca4f, 0x7763e373),
  63. new X64Word(0x682e6ff3, 0xd6b2b8a3),
  64. new X64Word(0x748f82ee, 0x5defb2fc),
  65. new X64Word(0x78a5636f, 0x43172f60),
  66. new X64Word(0x84c87814, 0xa1f0ab72),
  67. new X64Word(0x8cc70208, 0x1a6439ec),
  68. new X64Word(0x90befffa, 0x23631e28),
  69. new X64Word(0xa4506ceb, 0xde82bde9),
  70. new X64Word(0xbef9a3f7, 0xb2c67915),
  71. new X64Word(0xc67178f2, 0xe372532b),
  72. new X64Word(0xca273ece, 0xea26619c),
  73. new X64Word(0xd186b8c7, 0x21c0c207),
  74. new X64Word(0xeada7dd6, 0xcde0eb1e),
  75. new X64Word(0xf57d4f7f, 0xee6ed178),
  76. new X64Word(0x06f067aa, 0x72176fba),
  77. new X64Word(0x0a637dc5, 0xa2c898a6),
  78. new X64Word(0x113f9804, 0xbef90dae),
  79. new X64Word(0x1b710b35, 0x131c471b),
  80. new X64Word(0x28db77f5, 0x23047d84),
  81. new X64Word(0x32caab7b, 0x40c72493),
  82. new X64Word(0x3c9ebe0a, 0x15c9bebc),
  83. new X64Word(0x431d67c4, 0x9c100d4c),
  84. new X64Word(0x4cc5d4be, 0xcb3e42b6),
  85. new X64Word(0x597f299c, 0xfc657e2a),
  86. new X64Word(0x5fcb6fab, 0x3ad6faec),
  87. new X64Word(0x6c44198c, 0x4a475817),
  88. ];
  89. // Reusable objects
  90. const W = [];
  91. for (let i = 0; i < 80; i += 1) {
  92. W[i] = new X64Word();
  93. }
  94. /**
  95. * SHA-512 hash algorithm.
  96. */
  97. export class SHA512Algo extends Hasher {
  98. constructor() {
  99. super();
  100. this.blockSize = 1024 / 32;
  101. }
  102. _doReset() {
  103. this._hash = new X64WordArray([
  104. new X64Word(0x6a09e667, 0xf3bcc908),
  105. new X64Word(0xbb67ae85, 0x84caa73b),
  106. new X64Word(0x3c6ef372, 0xfe94f82b),
  107. new X64Word(0xa54ff53a, 0x5f1d36f1),
  108. new X64Word(0x510e527f, 0xade682d1),
  109. new X64Word(0x9b05688c, 0x2b3e6c1f),
  110. new X64Word(0x1f83d9ab, 0xfb41bd6b),
  111. new X64Word(0x5be0cd19, 0x137e2179),
  112. ]);
  113. }
  114. _doProcessBlock(M, offset) {
  115. // Shortcuts
  116. const H = this._hash.words;
  117. const H0 = H[0];
  118. const H1 = H[1];
  119. const H2 = H[2];
  120. const H3 = H[3];
  121. const H4 = H[4];
  122. const H5 = H[5];
  123. const H6 = H[6];
  124. const H7 = H[7];
  125. const H0h = H0.high;
  126. let H0l = H0.low;
  127. const H1h = H1.high;
  128. let H1l = H1.low;
  129. const H2h = H2.high;
  130. let H2l = H2.low;
  131. const H3h = H3.high;
  132. let H3l = H3.low;
  133. const H4h = H4.high;
  134. let H4l = H4.low;
  135. const H5h = H5.high;
  136. let H5l = H5.low;
  137. const H6h = H6.high;
  138. let H6l = H6.low;
  139. const H7h = H7.high;
  140. let H7l = H7.low;
  141. // Working variables
  142. let ah = H0h;
  143. let al = H0l;
  144. let bh = H1h;
  145. let bl = H1l;
  146. let ch = H2h;
  147. let cl = H2l;
  148. let dh = H3h;
  149. let dl = H3l;
  150. let eh = H4h;
  151. let el = H4l;
  152. let fh = H5h;
  153. let fl = H5l;
  154. let gh = H6h;
  155. let gl = H6l;
  156. let hh = H7h;
  157. let hl = H7l;
  158. // Rounds
  159. for (let i = 0; i < 80; i += 1) {
  160. let Wil;
  161. let Wih;
  162. // Shortcut
  163. const Wi = W[i];
  164. // Extend message
  165. if (i < 16) {
  166. Wi.high = M[offset + i * 2] | 0;
  167. Wih = Wi.high;
  168. Wi.low = M[offset + i * 2 + 1] | 0;
  169. Wil = Wi.low;
  170. } else {
  171. // Gamma0
  172. const gamma0x = W[i - 15];
  173. const gamma0xh = gamma0x.high;
  174. const gamma0xl = gamma0x.low;
  175. const gamma0h = ((gamma0xh >>> 1) | (gamma0xl << 31))
  176. ^ ((gamma0xh >>> 8) | (gamma0xl << 24))
  177. ^ (gamma0xh >>> 7);
  178. const gamma0l = ((gamma0xl >>> 1) | (gamma0xh << 31))
  179. ^ ((gamma0xl >>> 8) | (gamma0xh << 24))
  180. ^ ((gamma0xl >>> 7) | (gamma0xh << 25));
  181. // Gamma1
  182. const gamma1x = W[i - 2];
  183. const gamma1xh = gamma1x.high;
  184. const gamma1xl = gamma1x.low;
  185. const gamma1h = ((gamma1xh >>> 19) | (gamma1xl << 13))
  186. ^ ((gamma1xh << 3) | (gamma1xl >>> 29))
  187. ^ (gamma1xh >>> 6);
  188. const gamma1l = ((gamma1xl >>> 19) | (gamma1xh << 13))
  189. ^ ((gamma1xl << 3) | (gamma1xh >>> 29))
  190. ^ ((gamma1xl >>> 6) | (gamma1xh << 26));
  191. // W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16]
  192. const Wi7 = W[i - 7];
  193. const Wi7h = Wi7.high;
  194. const Wi7l = Wi7.low;
  195. const Wi16 = W[i - 16];
  196. const Wi16h = Wi16.high;
  197. const Wi16l = Wi16.low;
  198. Wil = gamma0l + Wi7l;
  199. Wih = gamma0h + Wi7h + ((Wil >>> 0) < (gamma0l >>> 0) ? 1 : 0);
  200. Wil += gamma1l;
  201. Wih = Wih + gamma1h + ((Wil >>> 0) < (gamma1l >>> 0) ? 1 : 0);
  202. Wil += Wi16l;
  203. Wih = Wih + Wi16h + ((Wil >>> 0) < (Wi16l >>> 0) ? 1 : 0);
  204. Wi.high = Wih;
  205. Wi.low = Wil;
  206. }
  207. const chh = (eh & fh) ^ (~eh & gh);
  208. const chl = (el & fl) ^ (~el & gl);
  209. const majh = (ah & bh) ^ (ah & ch) ^ (bh & ch);
  210. const majl = (al & bl) ^ (al & cl) ^ (bl & cl);
  211. const sigma0h = ((ah >>> 28) | (al << 4))
  212. ^ ((ah << 30) | (al >>> 2))
  213. ^ ((ah << 25) | (al >>> 7));
  214. const sigma0l = ((al >>> 28) | (ah << 4))
  215. ^ ((al << 30) | (ah >>> 2))
  216. ^ ((al << 25) | (ah >>> 7));
  217. const sigma1h = ((eh >>> 14) | (el << 18))
  218. ^ ((eh >>> 18) | (el << 14))
  219. ^ ((eh << 23) | (el >>> 9));
  220. const sigma1l = ((el >>> 14) | (eh << 18))
  221. ^ ((el >>> 18) | (eh << 14))
  222. ^ ((el << 23) | (eh >>> 9));
  223. // t1 = h + sigma1 + ch + K[i] + W[i]
  224. const Ki = K[i];
  225. const Kih = Ki.high;
  226. const Kil = Ki.low;
  227. let t1l = hl + sigma1l;
  228. let t1h = hh + sigma1h + ((t1l >>> 0) < (hl >>> 0) ? 1 : 0);
  229. t1l += chl;
  230. t1h = t1h + chh + ((t1l >>> 0) < (chl >>> 0) ? 1 : 0);
  231. t1l += Kil;
  232. t1h = t1h + Kih + ((t1l >>> 0) < (Kil >>> 0) ? 1 : 0);
  233. t1l += Wil;
  234. t1h = t1h + Wih + ((t1l >>> 0) < (Wil >>> 0) ? 1 : 0);
  235. // t2 = sigma0 + maj
  236. const t2l = sigma0l + majl;
  237. const t2h = sigma0h + majh + ((t2l >>> 0) < (sigma0l >>> 0) ? 1 : 0);
  238. // Update working variables
  239. hh = gh;
  240. hl = gl;
  241. gh = fh;
  242. gl = fl;
  243. fh = eh;
  244. fl = el;
  245. el = (dl + t1l) | 0;
  246. eh = (dh + t1h + ((el >>> 0) < (dl >>> 0) ? 1 : 0)) | 0;
  247. dh = ch;
  248. dl = cl;
  249. ch = bh;
  250. cl = bl;
  251. bh = ah;
  252. bl = al;
  253. al = (t1l + t2l) | 0;
  254. ah = (t1h + t2h + ((al >>> 0) < (t1l >>> 0) ? 1 : 0)) | 0;
  255. }
  256. // Intermediate hash value
  257. H0.low = (H0l + al);
  258. H0l = H0.low;
  259. H0.high = (H0h + ah + ((H0l >>> 0) < (al >>> 0) ? 1 : 0));
  260. H1.low = (H1l + bl);
  261. H1l = H1.low;
  262. H1.high = (H1h + bh + ((H1l >>> 0) < (bl >>> 0) ? 1 : 0));
  263. H2.low = (H2l + cl);
  264. H2l = H2.low;
  265. H2.high = (H2h + ch + ((H2l >>> 0) < (cl >>> 0) ? 1 : 0));
  266. H3.low = (H3l + dl);
  267. H3l = H3.low;
  268. H3.high = (H3h + dh + ((H3l >>> 0) < (dl >>> 0) ? 1 : 0));
  269. H4.low = (H4l + el);
  270. H4l = H4.low;
  271. H4.high = (H4h + eh + ((H4l >>> 0) < (el >>> 0) ? 1 : 0));
  272. H5.low = (H5l + fl);
  273. H5l = H5.low;
  274. H5.high = (H5h + fh + ((H5l >>> 0) < (fl >>> 0) ? 1 : 0));
  275. H6.low = (H6l + gl);
  276. H6l = H6.low;
  277. H6.high = (H6h + gh + ((H6l >>> 0) < (gl >>> 0) ? 1 : 0));
  278. H7.low = (H7l + hl);
  279. H7l = H7.low;
  280. H7.high = (H7h + hh + ((H7l >>> 0) < (hl >>> 0) ? 1 : 0));
  281. }
  282. _doFinalize() {
  283. // Shortcuts
  284. const data = this._data;
  285. const dataWords = data.words;
  286. const nBitsTotal = this._nDataBytes * 8;
  287. const nBitsLeft = data.sigBytes * 8;
  288. // Add padding
  289. dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - (nBitsLeft % 32));
  290. dataWords[(((nBitsLeft + 128) >>> 10) << 5) + 30] = Math.floor(nBitsTotal / 0x100000000);
  291. dataWords[(((nBitsLeft + 128) >>> 10) << 5) + 31] = nBitsTotal;
  292. data.sigBytes = dataWords.length * 4;
  293. // Hash final blocks
  294. this._process();
  295. // Convert hash to 32-bit word array before returning
  296. const hash = this._hash.toX32();
  297. // Return final computed hash
  298. return hash;
  299. }
  300. clone() {
  301. const clone = super.clone.call(this);
  302. clone._hash = this._hash.clone();
  303. return clone;
  304. }
  305. }
  306. /**
  307. * Shortcut function to the hasher's object interface.
  308. *
  309. * @param {WordArray|string} message The message to hash.
  310. *
  311. * @return {WordArray} The hash.
  312. *
  313. * @static
  314. *
  315. * @example
  316. *
  317. * var hash = CryptoJS.SHA512('message');
  318. * var hash = CryptoJS.SHA512(wordArray);
  319. */
  320. export const SHA512 = Hasher._createHelper(SHA512Algo);
  321. /**
  322. * Shortcut function to the HMAC's object interface.
  323. *
  324. * @param {WordArray|string} message The message to hash.
  325. * @param {WordArray|string} key The secret key.
  326. *
  327. * @return {WordArray} The HMAC.
  328. *
  329. * @static
  330. *
  331. * @example
  332. *
  333. * var hmac = CryptoJS.HmacSHA512(message, key);
  334. */
  335. export const HmacSHA512 = Hasher._createHmacHelper(SHA512Algo);