Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
11 KiB

  1. // ttmac.cpp - written and placed in the public domain by Kevin Springle
  2. #include "pch.h"
  3. #include "ttmac.h"
  4. #include "misc.h"
  5. NAMESPACE_BEGIN(CryptoPP)
  6. void TTMAC_Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &)
  7. {
  8. AssertValidKeyLength(keylength);
  9. memcpy(m_key, userKey, KEYLENGTH);
  10. CorrectEndianess(m_key, m_key, KEYLENGTH);
  11. Init();
  12. }
  13. void TTMAC_Base::Init()
  14. {
  15. m_digest[0] = m_digest[5] = m_key[0];
  16. m_digest[1] = m_digest[6] = m_key[1];
  17. m_digest[2] = m_digest[7] = m_key[2];
  18. m_digest[3] = m_digest[8] = m_key[3];
  19. m_digest[4] = m_digest[9] = m_key[4];
  20. }
  21. void TTMAC_Base::TruncatedFinal(byte *hash, size_t size)
  22. {
  23. PadLastBlock(BlockSize() - 2*sizeof(HashWordType));
  24. CorrectEndianess(m_data, m_data, BlockSize() - 2*sizeof(HashWordType));
  25. m_data[m_data.size()-2] = GetBitCountLo();
  26. m_data[m_data.size()-1] = GetBitCountHi();
  27. Transform(m_digest, m_data, true);
  28. word32 t2 = m_digest[2];
  29. word32 t3 = m_digest[3];
  30. if (size != DIGESTSIZE)
  31. {
  32. switch (size)
  33. {
  34. case 16:
  35. m_digest[3] += m_digest[1] + m_digest[4];
  36. case 12:
  37. m_digest[2] += m_digest[0] + t3;
  38. case 8:
  39. m_digest[0] += m_digest[1] + t3;
  40. m_digest[1] += m_digest[4] + t2;
  41. break;
  42. case 4:
  43. m_digest[0] +=
  44. m_digest[1] +
  45. m_digest[2] +
  46. m_digest[3] +
  47. m_digest[4];
  48. break;
  49. case 0:
  50. // Used by HashTransformation::Restart()
  51. break;
  52. default:
  53. throw InvalidArgument("TTMAC_Base: can't truncate a Two-Track-MAC 20 byte digest to " + IntToString(size) + " bytes");
  54. break;
  55. }
  56. }
  57. CorrectEndianess(m_digest, m_digest, size);
  58. memcpy(hash, m_digest, size);
  59. Restart(); // reinit for next use
  60. }
  61. // RIPEMD-160 definitions used by Two-Track-MAC
  62. #define F(x, y, z) (x ^ y ^ z)
  63. #define G(x, y, z) (z ^ (x & (y^z)))
  64. #define H(x, y, z) (z ^ (x | ~y))
  65. #define I(x, y, z) (y ^ (z & (x^y)))
  66. #define J(x, y, z) (x ^ (y | ~z))
  67. #define k0 0
  68. #define k1 0x5a827999UL
  69. #define k2 0x6ed9eba1UL
  70. #define k3 0x8f1bbcdcUL
  71. #define k4 0xa953fd4eUL
  72. #define k5 0x50a28be6UL
  73. #define k6 0x5c4dd124UL
  74. #define k7 0x6d703ef3UL
  75. #define k8 0x7a6d76e9UL
  76. #define k9 0
  77. void TTMAC_Base::Transform(word32 *digest, const word32 *X, bool last)
  78. {
  79. #define Subround(f, a, b, c, d, e, x, s, k) \
  80. a += f(b, c, d) + x + k;\
  81. a = rotlFixed((word32)a, s) + e;\
  82. c = rotlFixed((word32)c, 10U)
  83. word32 a1, b1, c1, d1, e1, a2, b2, c2, d2, e2;
  84. word32 *trackA, *trackB;
  85. if (!last)
  86. {
  87. trackA = digest;
  88. trackB = digest+5;
  89. }
  90. else
  91. {
  92. trackB = digest;
  93. trackA = digest+5;
  94. }
  95. a1 = trackA[0];
  96. b1 = trackA[1];
  97. c1 = trackA[2];
  98. d1 = trackA[3];
  99. e1 = trackA[4];
  100. a2 = trackB[0];
  101. b2 = trackB[1];
  102. c2 = trackB[2];
  103. d2 = trackB[3];
  104. e2 = trackB[4];
  105. Subround(F, a1, b1, c1, d1, e1, X[ 0], 11, k0);
  106. Subround(F, e1, a1, b1, c1, d1, X[ 1], 14, k0);
  107. Subround(F, d1, e1, a1, b1, c1, X[ 2], 15, k0);
  108. Subround(F, c1, d1, e1, a1, b1, X[ 3], 12, k0);
  109. Subround(F, b1, c1, d1, e1, a1, X[ 4], 5, k0);
  110. Subround(F, a1, b1, c1, d1, e1, X[ 5], 8, k0);
  111. Subround(F, e1, a1, b1, c1, d1, X[ 6], 7, k0);
  112. Subround(F, d1, e1, a1, b1, c1, X[ 7], 9, k0);
  113. Subround(F, c1, d1, e1, a1, b1, X[ 8], 11, k0);
  114. Subround(F, b1, c1, d1, e1, a1, X[ 9], 13, k0);
  115. Subround(F, a1, b1, c1, d1, e1, X[10], 14, k0);
  116. Subround(F, e1, a1, b1, c1, d1, X[11], 15, k0);
  117. Subround(F, d1, e1, a1, b1, c1, X[12], 6, k0);
  118. Subround(F, c1, d1, e1, a1, b1, X[13], 7, k0);
  119. Subround(F, b1, c1, d1, e1, a1, X[14], 9, k0);
  120. Subround(F, a1, b1, c1, d1, e1, X[15], 8, k0);
  121. Subround(G, e1, a1, b1, c1, d1, X[ 7], 7, k1);
  122. Subround(G, d1, e1, a1, b1, c1, X[ 4], 6, k1);
  123. Subround(G, c1, d1, e1, a1, b1, X[13], 8, k1);
  124. Subround(G, b1, c1, d1, e1, a1, X[ 1], 13, k1);
  125. Subround(G, a1, b1, c1, d1, e1, X[10], 11, k1);
  126. Subround(G, e1, a1, b1, c1, d1, X[ 6], 9, k1);
  127. Subround(G, d1, e1, a1, b1, c1, X[15], 7, k1);
  128. Subround(G, c1, d1, e1, a1, b1, X[ 3], 15, k1);
  129. Subround(G, b1, c1, d1, e1, a1, X[12], 7, k1);
  130. Subround(G, a1, b1, c1, d1, e1, X[ 0], 12, k1);
  131. Subround(G, e1, a1, b1, c1, d1, X[ 9], 15, k1);
  132. Subround(G, d1, e1, a1, b1, c1, X[ 5], 9, k1);
  133. Subround(G, c1, d1, e1, a1, b1, X[ 2], 11, k1);
  134. Subround(G, b1, c1, d1, e1, a1, X[14], 7, k1);
  135. Subround(G, a1, b1, c1, d1, e1, X[11], 13, k1);
  136. Subround(G, e1, a1, b1, c1, d1, X[ 8], 12, k1);
  137. Subround(H, d1, e1, a1, b1, c1, X[ 3], 11, k2);
  138. Subround(H, c1, d1, e1, a1, b1, X[10], 13, k2);
  139. Subround(H, b1, c1, d1, e1, a1, X[14], 6, k2);
  140. Subround(H, a1, b1, c1, d1, e1, X[ 4], 7, k2);
  141. Subround(H, e1, a1, b1, c1, d1, X[ 9], 14, k2);
  142. Subround(H, d1, e1, a1, b1, c1, X[15], 9, k2);
  143. Subround(H, c1, d1, e1, a1, b1, X[ 8], 13, k2);
  144. Subround(H, b1, c1, d1, e1, a1, X[ 1], 15, k2);
  145. Subround(H, a1, b1, c1, d1, e1, X[ 2], 14, k2);
  146. Subround(H, e1, a1, b1, c1, d1, X[ 7], 8, k2);
  147. Subround(H, d1, e1, a1, b1, c1, X[ 0], 13, k2);
  148. Subround(H, c1, d1, e1, a1, b1, X[ 6], 6, k2);
  149. Subround(H, b1, c1, d1, e1, a1, X[13], 5, k2);
  150. Subround(H, a1, b1, c1, d1, e1, X[11], 12, k2);
  151. Subround(H, e1, a1, b1, c1, d1, X[ 5], 7, k2);
  152. Subround(H, d1, e1, a1, b1, c1, X[12], 5, k2);
  153. Subround(I, c1, d1, e1, a1, b1, X[ 1], 11, k3);
  154. Subround(I, b1, c1, d1, e1, a1, X[ 9], 12, k3);
  155. Subround(I, a1, b1, c1, d1, e1, X[11], 14, k3);
  156. Subround(I, e1, a1, b1, c1, d1, X[10], 15, k3);
  157. Subround(I, d1, e1, a1, b1, c1, X[ 0], 14, k3);
  158. Subround(I, c1, d1, e1, a1, b1, X[ 8], 15, k3);
  159. Subround(I, b1, c1, d1, e1, a1, X[12], 9, k3);
  160. Subround(I, a1, b1, c1, d1, e1, X[ 4], 8, k3);
  161. Subround(I, e1, a1, b1, c1, d1, X[13], 9, k3);
  162. Subround(I, d1, e1, a1, b1, c1, X[ 3], 14, k3);
  163. Subround(I, c1, d1, e1, a1, b1, X[ 7], 5, k3);
  164. Subround(I, b1, c1, d1, e1, a1, X[15], 6, k3);
  165. Subround(I, a1, b1, c1, d1, e1, X[14], 8, k3);
  166. Subround(I, e1, a1, b1, c1, d1, X[ 5], 6, k3);
  167. Subround(I, d1, e1, a1, b1, c1, X[ 6], 5, k3);
  168. Subround(I, c1, d1, e1, a1, b1, X[ 2], 12, k3);
  169. Subround(J, b1, c1, d1, e1, a1, X[ 4], 9, k4);
  170. Subround(J, a1, b1, c1, d1, e1, X[ 0], 15, k4);
  171. Subround(J, e1, a1, b1, c1, d1, X[ 5], 5, k4);
  172. Subround(J, d1, e1, a1, b1, c1, X[ 9], 11, k4);
  173. Subround(J, c1, d1, e1, a1, b1, X[ 7], 6, k4);
  174. Subround(J, b1, c1, d1, e1, a1, X[12], 8, k4);
  175. Subround(J, a1, b1, c1, d1, e1, X[ 2], 13, k4);
  176. Subround(J, e1, a1, b1, c1, d1, X[10], 12, k4);
  177. Subround(J, d1, e1, a1, b1, c1, X[14], 5, k4);
  178. Subround(J, c1, d1, e1, a1, b1, X[ 1], 12, k4);
  179. Subround(J, b1, c1, d1, e1, a1, X[ 3], 13, k4);
  180. Subround(J, a1, b1, c1, d1, e1, X[ 8], 14, k4);
  181. Subround(J, e1, a1, b1, c1, d1, X[11], 11, k4);
  182. Subround(J, d1, e1, a1, b1, c1, X[ 6], 8, k4);
  183. Subround(J, c1, d1, e1, a1, b1, X[15], 5, k4);
  184. Subround(J, b1, c1, d1, e1, a1, X[13], 6, k4);
  185. Subround(J, a2, b2, c2, d2, e2, X[ 5], 8, k5);
  186. Subround(J, e2, a2, b2, c2, d2, X[14], 9, k5);
  187. Subround(J, d2, e2, a2, b2, c2, X[ 7], 9, k5);
  188. Subround(J, c2, d2, e2, a2, b2, X[ 0], 11, k5);
  189. Subround(J, b2, c2, d2, e2, a2, X[ 9], 13, k5);
  190. Subround(J, a2, b2, c2, d2, e2, X[ 2], 15, k5);
  191. Subround(J, e2, a2, b2, c2, d2, X[11], 15, k5);
  192. Subround(J, d2, e2, a2, b2, c2, X[ 4], 5, k5);
  193. Subround(J, c2, d2, e2, a2, b2, X[13], 7, k5);
  194. Subround(J, b2, c2, d2, e2, a2, X[ 6], 7, k5);
  195. Subround(J, a2, b2, c2, d2, e2, X[15], 8, k5);
  196. Subround(J, e2, a2, b2, c2, d2, X[ 8], 11, k5);
  197. Subround(J, d2, e2, a2, b2, c2, X[ 1], 14, k5);
  198. Subround(J, c2, d2, e2, a2, b2, X[10], 14, k5);
  199. Subround(J, b2, c2, d2, e2, a2, X[ 3], 12, k5);
  200. Subround(J, a2, b2, c2, d2, e2, X[12], 6, k5);
  201. Subround(I, e2, a2, b2, c2, d2, X[ 6], 9, k6);
  202. Subround(I, d2, e2, a2, b2, c2, X[11], 13, k6);
  203. Subround(I, c2, d2, e2, a2, b2, X[ 3], 15, k6);
  204. Subround(I, b2, c2, d2, e2, a2, X[ 7], 7, k6);
  205. Subround(I, a2, b2, c2, d2, e2, X[ 0], 12, k6);
  206. Subround(I, e2, a2, b2, c2, d2, X[13], 8, k6);
  207. Subround(I, d2, e2, a2, b2, c2, X[ 5], 9, k6);
  208. Subround(I, c2, d2, e2, a2, b2, X[10], 11, k6);
  209. Subround(I, b2, c2, d2, e2, a2, X[14], 7, k6);
  210. Subround(I, a2, b2, c2, d2, e2, X[15], 7, k6);
  211. Subround(I, e2, a2, b2, c2, d2, X[ 8], 12, k6);
  212. Subround(I, d2, e2, a2, b2, c2, X[12], 7, k6);
  213. Subround(I, c2, d2, e2, a2, b2, X[ 4], 6, k6);
  214. Subround(I, b2, c2, d2, e2, a2, X[ 9], 15, k6);
  215. Subround(I, a2, b2, c2, d2, e2, X[ 1], 13, k6);
  216. Subround(I, e2, a2, b2, c2, d2, X[ 2], 11, k6);
  217. Subround(H, d2, e2, a2, b2, c2, X[15], 9, k7);
  218. Subround(H, c2, d2, e2, a2, b2, X[ 5], 7, k7);
  219. Subround(H, b2, c2, d2, e2, a2, X[ 1], 15, k7);
  220. Subround(H, a2, b2, c2, d2, e2, X[ 3], 11, k7);
  221. Subround(H, e2, a2, b2, c2, d2, X[ 7], 8, k7);
  222. Subround(H, d2, e2, a2, b2, c2, X[14], 6, k7);
  223. Subround(H, c2, d2, e2, a2, b2, X[ 6], 6, k7);
  224. Subround(H, b2, c2, d2, e2, a2, X[ 9], 14, k7);
  225. Subround(H, a2, b2, c2, d2, e2, X[11], 12, k7);
  226. Subround(H, e2, a2, b2, c2, d2, X[ 8], 13, k7);
  227. Subround(H, d2, e2, a2, b2, c2, X[12], 5, k7);
  228. Subround(H, c2, d2, e2, a2, b2, X[ 2], 14, k7);
  229. Subround(H, b2, c2, d2, e2, a2, X[10], 13, k7);
  230. Subround(H, a2, b2, c2, d2, e2, X[ 0], 13, k7);
  231. Subround(H, e2, a2, b2, c2, d2, X[ 4], 7, k7);
  232. Subround(H, d2, e2, a2, b2, c2, X[13], 5, k7);
  233. Subround(G, c2, d2, e2, a2, b2, X[ 8], 15, k8);
  234. Subround(G, b2, c2, d2, e2, a2, X[ 6], 5, k8);
  235. Subround(G, a2, b2, c2, d2, e2, X[ 4], 8, k8);
  236. Subround(G, e2, a2, b2, c2, d2, X[ 1], 11, k8);
  237. Subround(G, d2, e2, a2, b2, c2, X[ 3], 14, k8);
  238. Subround(G, c2, d2, e2, a2, b2, X[11], 14, k8);
  239. Subround(G, b2, c2, d2, e2, a2, X[15], 6, k8);
  240. Subround(G, a2, b2, c2, d2, e2, X[ 0], 14, k8);
  241. Subround(G, e2, a2, b2, c2, d2, X[ 5], 6, k8);
  242. Subround(G, d2, e2, a2, b2, c2, X[12], 9, k8);
  243. Subround(G, c2, d2, e2, a2, b2, X[ 2], 12, k8);
  244. Subround(G, b2, c2, d2, e2, a2, X[13], 9, k8);
  245. Subround(G, a2, b2, c2, d2, e2, X[ 9], 12, k8);
  246. Subround(G, e2, a2, b2, c2, d2, X[ 7], 5, k8);
  247. Subround(G, d2, e2, a2, b2, c2, X[10], 15, k8);
  248. Subround(G, c2, d2, e2, a2, b2, X[14], 8, k8);
  249. Subround(F, b2, c2, d2, e2, a2, X[12], 8, k9);
  250. Subround(F, a2, b2, c2, d2, e2, X[15], 5, k9);
  251. Subround(F, e2, a2, b2, c2, d2, X[10], 12, k9);
  252. Subround(F, d2, e2, a2, b2, c2, X[ 4], 9, k9);
  253. Subround(F, c2, d2, e2, a2, b2, X[ 1], 12, k9);
  254. Subround(F, b2, c2, d2, e2, a2, X[ 5], 5, k9);
  255. Subround(F, a2, b2, c2, d2, e2, X[ 8], 14, k9);
  256. Subround(F, e2, a2, b2, c2, d2, X[ 7], 6, k9);
  257. Subround(F, d2, e2, a2, b2, c2, X[ 6], 8, k9);
  258. Subround(F, c2, d2, e2, a2, b2, X[ 2], 13, k9);
  259. Subround(F, b2, c2, d2, e2, a2, X[13], 6, k9);
  260. Subround(F, a2, b2, c2, d2, e2, X[14], 5, k9);
  261. Subround(F, e2, a2, b2, c2, d2, X[ 0], 15, k9);
  262. Subround(F, d2, e2, a2, b2, c2, X[ 3], 13, k9);
  263. Subround(F, c2, d2, e2, a2, b2, X[ 9], 11, k9);
  264. Subround(F, b2, c2, d2, e2, a2, X[11], 11, k9);
  265. a1 -= trackA[0];
  266. b1 -= trackA[1];
  267. c1 -= trackA[2];
  268. d1 -= trackA[3];
  269. e1 -= trackA[4];
  270. a2 -= trackB[0];
  271. b2 -= trackB[1];
  272. c2 -= trackB[2];
  273. d2 -= trackB[3];
  274. e2 -= trackB[4];
  275. if (!last)
  276. {
  277. trackA[0] = (b1 + e1) - d2;
  278. trackA[1] = c1 - e2;
  279. trackA[2] = d1 - a2;
  280. trackA[3] = e1 - b2;
  281. trackA[4] = a1 - c2;
  282. trackB[0] = d1 - e2;
  283. trackB[1] = (e1 + c1) - a2;
  284. trackB[2] = a1 - b2;
  285. trackB[3] = b1 - c2;
  286. trackB[4] = c1 - d2;
  287. }
  288. else
  289. {
  290. trackB[0] = a2 - a1;
  291. trackB[1] = b2 - b1;
  292. trackB[2] = c2 - c1;
  293. trackB[3] = d2 - d1;
  294. trackB[4] = e2 - e1;
  295. trackA[0] = 0;
  296. trackA[1] = 0;
  297. trackA[2] = 0;
  298. trackA[3] = 0;
  299. trackA[4] = 0;
  300. }
  301. }
  302. NAMESPACE_END