legacy.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. SHA1 (RFC 3174), MD5 (RFC 1321) and RIPEMD160 (RFC 2286) legacy, weak hash functions.
  3. Don't use them in a new protocol. What "weak" means:
  4. - Collisions can be made with 2^18 effort in MD5, 2^60 in SHA1, 2^80 in RIPEMD160.
  5. - No practical pre-image attacks (only theoretical, 2^123.4)
  6. - HMAC seems kinda ok: https://datatracker.ietf.org/doc/html/rfc6151
  7. * @module
  8. */
  9. import { Chi, HashMD, Maj } from "./_md.js";
  10. import { clean, createHasher, rotl } from "./utils.js";
  11. /** Initial SHA1 state */
  12. const SHA1_IV = /* @__PURE__ */ Uint32Array.from([
  13. 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0,
  14. ]);
  15. // Reusable temporary buffer
  16. const SHA1_W = /* @__PURE__ */ new Uint32Array(80);
  17. /** SHA1 legacy hash class. */
  18. export class SHA1 extends HashMD {
  19. constructor() {
  20. super(64, 20, 8, false);
  21. this.A = SHA1_IV[0] | 0;
  22. this.B = SHA1_IV[1] | 0;
  23. this.C = SHA1_IV[2] | 0;
  24. this.D = SHA1_IV[3] | 0;
  25. this.E = SHA1_IV[4] | 0;
  26. }
  27. get() {
  28. const { A, B, C, D, E } = this;
  29. return [A, B, C, D, E];
  30. }
  31. set(A, B, C, D, E) {
  32. this.A = A | 0;
  33. this.B = B | 0;
  34. this.C = C | 0;
  35. this.D = D | 0;
  36. this.E = E | 0;
  37. }
  38. process(view, offset) {
  39. for (let i = 0; i < 16; i++, offset += 4)
  40. SHA1_W[i] = view.getUint32(offset, false);
  41. for (let i = 16; i < 80; i++)
  42. SHA1_W[i] = rotl(SHA1_W[i - 3] ^ SHA1_W[i - 8] ^ SHA1_W[i - 14] ^ SHA1_W[i - 16], 1);
  43. // Compression function main loop, 80 rounds
  44. let { A, B, C, D, E } = this;
  45. for (let i = 0; i < 80; i++) {
  46. let F, K;
  47. if (i < 20) {
  48. F = Chi(B, C, D);
  49. K = 0x5a827999;
  50. }
  51. else if (i < 40) {
  52. F = B ^ C ^ D;
  53. K = 0x6ed9eba1;
  54. }
  55. else if (i < 60) {
  56. F = Maj(B, C, D);
  57. K = 0x8f1bbcdc;
  58. }
  59. else {
  60. F = B ^ C ^ D;
  61. K = 0xca62c1d6;
  62. }
  63. const T = (rotl(A, 5) + F + E + K + SHA1_W[i]) | 0;
  64. E = D;
  65. D = C;
  66. C = rotl(B, 30);
  67. B = A;
  68. A = T;
  69. }
  70. // Add the compressed chunk to the current hash value
  71. A = (A + this.A) | 0;
  72. B = (B + this.B) | 0;
  73. C = (C + this.C) | 0;
  74. D = (D + this.D) | 0;
  75. E = (E + this.E) | 0;
  76. this.set(A, B, C, D, E);
  77. }
  78. roundClean() {
  79. clean(SHA1_W);
  80. }
  81. destroy() {
  82. this.set(0, 0, 0, 0, 0);
  83. clean(this.buffer);
  84. }
  85. }
  86. /** SHA1 (RFC 3174) legacy hash function. It was cryptographically broken. */
  87. export const sha1 = /* @__PURE__ */ createHasher(() => new SHA1());
  88. /** Per-round constants */
  89. const p32 = /* @__PURE__ */ Math.pow(2, 32);
  90. const K = /* @__PURE__ */ Array.from({ length: 64 }, (_, i) => Math.floor(p32 * Math.abs(Math.sin(i + 1))));
  91. /** md5 initial state: same as sha1, but 4 u32 instead of 5. */
  92. const MD5_IV = /* @__PURE__ */ SHA1_IV.slice(0, 4);
  93. // Reusable temporary buffer
  94. const MD5_W = /* @__PURE__ */ new Uint32Array(16);
  95. /** MD5 legacy hash class. */
  96. export class MD5 extends HashMD {
  97. constructor() {
  98. super(64, 16, 8, true);
  99. this.A = MD5_IV[0] | 0;
  100. this.B = MD5_IV[1] | 0;
  101. this.C = MD5_IV[2] | 0;
  102. this.D = MD5_IV[3] | 0;
  103. }
  104. get() {
  105. const { A, B, C, D } = this;
  106. return [A, B, C, D];
  107. }
  108. set(A, B, C, D) {
  109. this.A = A | 0;
  110. this.B = B | 0;
  111. this.C = C | 0;
  112. this.D = D | 0;
  113. }
  114. process(view, offset) {
  115. for (let i = 0; i < 16; i++, offset += 4)
  116. MD5_W[i] = view.getUint32(offset, true);
  117. // Compression function main loop, 64 rounds
  118. let { A, B, C, D } = this;
  119. for (let i = 0; i < 64; i++) {
  120. let F, g, s;
  121. if (i < 16) {
  122. F = Chi(B, C, D);
  123. g = i;
  124. s = [7, 12, 17, 22];
  125. }
  126. else if (i < 32) {
  127. F = Chi(D, B, C);
  128. g = (5 * i + 1) % 16;
  129. s = [5, 9, 14, 20];
  130. }
  131. else if (i < 48) {
  132. F = B ^ C ^ D;
  133. g = (3 * i + 5) % 16;
  134. s = [4, 11, 16, 23];
  135. }
  136. else {
  137. F = C ^ (B | ~D);
  138. g = (7 * i) % 16;
  139. s = [6, 10, 15, 21];
  140. }
  141. F = F + A + K[i] + MD5_W[g];
  142. A = D;
  143. D = C;
  144. C = B;
  145. B = B + rotl(F, s[i % 4]);
  146. }
  147. // Add the compressed chunk to the current hash value
  148. A = (A + this.A) | 0;
  149. B = (B + this.B) | 0;
  150. C = (C + this.C) | 0;
  151. D = (D + this.D) | 0;
  152. this.set(A, B, C, D);
  153. }
  154. roundClean() {
  155. clean(MD5_W);
  156. }
  157. destroy() {
  158. this.set(0, 0, 0, 0);
  159. clean(this.buffer);
  160. }
  161. }
  162. /**
  163. * MD5 (RFC 1321) legacy hash function. It was cryptographically broken.
  164. * MD5 architecture is similar to SHA1, with some differences:
  165. * - Reduced output length: 16 bytes (128 bit) instead of 20
  166. * - 64 rounds, instead of 80
  167. * - Little-endian: could be faster, but will require more code
  168. * - Non-linear index selection: huge speed-up for unroll
  169. * - Per round constants: more memory accesses, additional speed-up for unroll
  170. */
  171. export const md5 = /* @__PURE__ */ createHasher(() => new MD5());
  172. // RIPEMD-160
  173. const Rho160 = /* @__PURE__ */ Uint8Array.from([
  174. 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
  175. ]);
  176. const Id160 = /* @__PURE__ */ (() => Uint8Array.from(new Array(16).fill(0).map((_, i) => i)))();
  177. const Pi160 = /* @__PURE__ */ (() => Id160.map((i) => (9 * i + 5) % 16))();
  178. const idxLR = /* @__PURE__ */ (() => {
  179. const L = [Id160];
  180. const R = [Pi160];
  181. const res = [L, R];
  182. for (let i = 0; i < 4; i++)
  183. for (let j of res)
  184. j.push(j[i].map((k) => Rho160[k]));
  185. return res;
  186. })();
  187. const idxL = /* @__PURE__ */ (() => idxLR[0])();
  188. const idxR = /* @__PURE__ */ (() => idxLR[1])();
  189. // const [idxL, idxR] = idxLR;
  190. const shifts160 = /* @__PURE__ */ [
  191. [11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8],
  192. [12, 13, 11, 15, 6, 9, 9, 7, 12, 15, 11, 13, 7, 8, 7, 7],
  193. [13, 15, 14, 11, 7, 7, 6, 8, 13, 14, 13, 12, 5, 5, 6, 9],
  194. [14, 11, 12, 14, 8, 6, 5, 5, 15, 12, 15, 14, 9, 9, 8, 6],
  195. [15, 12, 13, 13, 9, 5, 8, 6, 14, 11, 12, 11, 8, 6, 5, 5],
  196. ].map((i) => Uint8Array.from(i));
  197. const shiftsL160 = /* @__PURE__ */ idxL.map((idx, i) => idx.map((j) => shifts160[i][j]));
  198. const shiftsR160 = /* @__PURE__ */ idxR.map((idx, i) => idx.map((j) => shifts160[i][j]));
  199. const Kl160 = /* @__PURE__ */ Uint32Array.from([
  200. 0x00000000, 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xa953fd4e,
  201. ]);
  202. const Kr160 = /* @__PURE__ */ Uint32Array.from([
  203. 0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9, 0x00000000,
  204. ]);
  205. // It's called f() in spec.
  206. function ripemd_f(group, x, y, z) {
  207. if (group === 0)
  208. return x ^ y ^ z;
  209. if (group === 1)
  210. return (x & y) | (~x & z);
  211. if (group === 2)
  212. return (x | ~y) ^ z;
  213. if (group === 3)
  214. return (x & z) | (y & ~z);
  215. return x ^ (y | ~z);
  216. }
  217. // Reusable temporary buffer
  218. const BUF_160 = /* @__PURE__ */ new Uint32Array(16);
  219. export class RIPEMD160 extends HashMD {
  220. constructor() {
  221. super(64, 20, 8, true);
  222. this.h0 = 0x67452301 | 0;
  223. this.h1 = 0xefcdab89 | 0;
  224. this.h2 = 0x98badcfe | 0;
  225. this.h3 = 0x10325476 | 0;
  226. this.h4 = 0xc3d2e1f0 | 0;
  227. }
  228. get() {
  229. const { h0, h1, h2, h3, h4 } = this;
  230. return [h0, h1, h2, h3, h4];
  231. }
  232. set(h0, h1, h2, h3, h4) {
  233. this.h0 = h0 | 0;
  234. this.h1 = h1 | 0;
  235. this.h2 = h2 | 0;
  236. this.h3 = h3 | 0;
  237. this.h4 = h4 | 0;
  238. }
  239. process(view, offset) {
  240. for (let i = 0; i < 16; i++, offset += 4)
  241. BUF_160[i] = view.getUint32(offset, true);
  242. // prettier-ignore
  243. let al = this.h0 | 0, ar = al, bl = this.h1 | 0, br = bl, cl = this.h2 | 0, cr = cl, dl = this.h3 | 0, dr = dl, el = this.h4 | 0, er = el;
  244. // Instead of iterating 0 to 80, we split it into 5 groups
  245. // And use the groups in constants, functions, etc. Much simpler
  246. for (let group = 0; group < 5; group++) {
  247. const rGroup = 4 - group;
  248. const hbl = Kl160[group], hbr = Kr160[group]; // prettier-ignore
  249. const rl = idxL[group], rr = idxR[group]; // prettier-ignore
  250. const sl = shiftsL160[group], sr = shiftsR160[group]; // prettier-ignore
  251. for (let i = 0; i < 16; i++) {
  252. const tl = (rotl(al + ripemd_f(group, bl, cl, dl) + BUF_160[rl[i]] + hbl, sl[i]) + el) | 0;
  253. al = el, el = dl, dl = rotl(cl, 10) | 0, cl = bl, bl = tl; // prettier-ignore
  254. }
  255. // 2 loops are 10% faster
  256. for (let i = 0; i < 16; i++) {
  257. const tr = (rotl(ar + ripemd_f(rGroup, br, cr, dr) + BUF_160[rr[i]] + hbr, sr[i]) + er) | 0;
  258. ar = er, er = dr, dr = rotl(cr, 10) | 0, cr = br, br = tr; // prettier-ignore
  259. }
  260. }
  261. // Add the compressed chunk to the current hash value
  262. this.set((this.h1 + cl + dr) | 0, (this.h2 + dl + er) | 0, (this.h3 + el + ar) | 0, (this.h4 + al + br) | 0, (this.h0 + bl + cr) | 0);
  263. }
  264. roundClean() {
  265. clean(BUF_160);
  266. }
  267. destroy() {
  268. this.destroyed = true;
  269. clean(this.buffer);
  270. this.set(0, 0, 0, 0, 0);
  271. }
  272. }
  273. /**
  274. * RIPEMD-160 - a legacy hash function from 1990s.
  275. * * https://homes.esat.kuleuven.be/~bosselae/ripemd160.html
  276. * * https://homes.esat.kuleuven.be/~bosselae/ripemd160/pdf/AB-9601/AB-9601.pdf
  277. */
  278. export const ripemd160 = /* @__PURE__ */ createHasher(() => new RIPEMD160());
  279. //# sourceMappingURL=legacy.js.map