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.

265 lines
5.8 KiB

  1. // tiger.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #include "tiger.h"
  4. #include "misc.h"
  5. #include "cpu.h"
  6. NAMESPACE_BEGIN(CryptoPP)
  7. void Tiger::InitState(HashWordType *state)
  8. {
  9. state[0] = W64LIT(0x0123456789ABCDEF);
  10. state[1] = W64LIT(0xFEDCBA9876543210);
  11. state[2] = W64LIT(0xF096A5B4C3B2E187);
  12. }
  13. void Tiger::TruncatedFinal(byte *hash, size_t size)
  14. {
  15. ThrowIfInvalidTruncatedSize(size);
  16. PadLastBlock(56, 0x01);
  17. CorrectEndianess(m_data, m_data, 56);
  18. m_data[7] = GetBitCountLo();
  19. Transform(m_state, m_data);
  20. CorrectEndianess(m_state, m_state, DigestSize());
  21. memcpy(hash, m_state, size);
  22. Restart(); // reinit for next use
  23. }
  24. void Tiger::Transform (word64 *digest, const word64 *X)
  25. {
  26. #if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE && CRYPTOPP_BOOL_X86
  27. if (HasSSE2())
  28. {
  29. #ifdef __GNUC__
  30. __asm__ __volatile__
  31. (
  32. ".intel_syntax noprefix;"
  33. AS1( push ebx)
  34. #else
  35. #if _MSC_VER < 1300
  36. const word64 *t = table;
  37. AS2( mov edx, t)
  38. #else
  39. AS2( lea edx, [table])
  40. #endif
  41. AS2( mov eax, digest)
  42. AS2( mov esi, X)
  43. #endif
  44. AS2( movq mm0, [eax])
  45. AS2( movq mm1, [eax+1*8])
  46. AS2( movq mm5, mm1)
  47. AS2( movq mm2, [eax+2*8])
  48. AS2( movq mm7, [edx+4*2048+0*8])
  49. AS2( movq mm6, [edx+4*2048+1*8])
  50. AS2( mov ecx, esp)
  51. AS2( and esp, 0xfffffff0)
  52. AS2( sub esp, 8*8)
  53. AS1( push ecx)
  54. #define SSE2_round(a,b,c,x,mul) \
  55. AS2( pxor c, [x])\
  56. AS2( movd ecx, c)\
  57. AS2( movzx edi, cl)\
  58. AS2( movq mm3, [edx+0*2048+edi*8])\
  59. AS2( movzx edi, ch)\
  60. AS2( movq mm4, [edx+3*2048+edi*8])\
  61. AS2( shr ecx, 16)\
  62. AS2( movzx edi, cl)\
  63. AS2( pxor mm3, [edx+1*2048+edi*8])\
  64. AS2( movzx edi, ch)\
  65. AS2( pxor mm4, [edx+2*2048+edi*8])\
  66. AS3( pextrw ecx, c, 2)\
  67. AS2( movzx edi, cl)\
  68. AS2( pxor mm3, [edx+2*2048+edi*8])\
  69. AS2( movzx edi, ch)\
  70. AS2( pxor mm4, [edx+1*2048+edi*8])\
  71. AS3( pextrw ecx, c, 3)\
  72. AS2( movzx edi, cl)\
  73. AS2( pxor mm3, [edx+3*2048+edi*8])\
  74. AS2( psubq a, mm3)\
  75. AS2( movzx edi, ch)\
  76. AS2( pxor mm4, [edx+0*2048+edi*8])\
  77. AS2( paddq b, mm4)\
  78. SSE2_mul_##mul(b)
  79. #define SSE2_mul_5(b) \
  80. AS2( movq mm3, b)\
  81. AS2( psllq b, 2)\
  82. AS2( paddq b, mm3)
  83. #define SSE2_mul_7(b) \
  84. AS2( movq mm3, b)\
  85. AS2( psllq b, 3)\
  86. AS2( psubq b, mm3)
  87. #define SSE2_mul_9(b) \
  88. AS2( movq mm3, b)\
  89. AS2( psllq b, 3)\
  90. AS2( paddq b, mm3)
  91. #define label2_5 1
  92. #define label2_7 2
  93. #define label2_9 3
  94. #define SSE2_pass(A,B,C,mul,X) \
  95. AS2( xor ebx, ebx)\
  96. ASL(mul)\
  97. SSE2_round(A,B,C,X+0*8+ebx,mul)\
  98. SSE2_round(B,C,A,X+1*8+ebx,mul)\
  99. AS2( cmp ebx, 6*8)\
  100. ASJ( je, label2_##mul, f)\
  101. SSE2_round(C,A,B,X+2*8+ebx,mul)\
  102. AS2( add ebx, 3*8)\
  103. ASJ( jmp, mul, b)\
  104. ASL(label2_##mul)
  105. #define SSE2_key_schedule(Y,X) \
  106. AS2( movq mm3, [X+7*8])\
  107. AS2( pxor mm3, mm6)\
  108. AS2( movq mm4, [X+0*8])\
  109. AS2( psubq mm4, mm3)\
  110. AS2( movq [Y+0*8], mm4)\
  111. AS2( pxor mm4, [X+1*8])\
  112. AS2( movq mm3, mm4)\
  113. AS2( movq [Y+1*8], mm4)\
  114. AS2( paddq mm4, [X+2*8])\
  115. AS2( pxor mm3, mm7)\
  116. AS2( psllq mm3, 19)\
  117. AS2( movq [Y+2*8], mm4)\
  118. AS2( pxor mm3, mm4)\
  119. AS2( movq mm4, [X+3*8])\
  120. AS2( psubq mm4, mm3)\
  121. AS2( movq [Y+3*8], mm4)\
  122. AS2( pxor mm4, [X+4*8])\
  123. AS2( movq mm3, mm4)\
  124. AS2( movq [Y+4*8], mm4)\
  125. AS2( paddq mm4, [X+5*8])\
  126. AS2( pxor mm3, mm7)\
  127. AS2( psrlq mm3, 23)\
  128. AS2( movq [Y+5*8], mm4)\
  129. AS2( pxor mm3, mm4)\
  130. AS2( movq mm4, [X+6*8])\
  131. AS2( psubq mm4, mm3)\
  132. AS2( movq [Y+6*8], mm4)\
  133. AS2( pxor mm4, [X+7*8])\
  134. AS2( movq mm3, mm4)\
  135. AS2( movq [Y+7*8], mm4)\
  136. AS2( paddq mm4, [Y+0*8])\
  137. AS2( pxor mm3, mm7)\
  138. AS2( psllq mm3, 19)\
  139. AS2( movq [Y+0*8], mm4)\
  140. AS2( pxor mm3, mm4)\
  141. AS2( movq mm4, [Y+1*8])\
  142. AS2( psubq mm4, mm3)\
  143. AS2( movq [Y+1*8], mm4)\
  144. AS2( pxor mm4, [Y+2*8])\
  145. AS2( movq mm3, mm4)\
  146. AS2( movq [Y+2*8], mm4)\
  147. AS2( paddq mm4, [Y+3*8])\
  148. AS2( pxor mm3, mm7)\
  149. AS2( psrlq mm3, 23)\
  150. AS2( movq [Y+3*8], mm4)\
  151. AS2( pxor mm3, mm4)\
  152. AS2( movq mm4, [Y+4*8])\
  153. AS2( psubq mm4, mm3)\
  154. AS2( movq [Y+4*8], mm4)\
  155. AS2( pxor mm4, [Y+5*8])\
  156. AS2( movq [Y+5*8], mm4)\
  157. AS2( paddq mm4, [Y+6*8])\
  158. AS2( movq [Y+6*8], mm4)\
  159. AS2( pxor mm4, [edx+4*2048+2*8])\
  160. AS2( movq mm3, [Y+7*8])\
  161. AS2( psubq mm3, mm4)\
  162. AS2( movq [Y+7*8], mm3)
  163. SSE2_pass(mm0, mm1, mm2, 5, esi)
  164. SSE2_key_schedule(esp+4, esi)
  165. SSE2_pass(mm2, mm0, mm1, 7, esp+4)
  166. SSE2_key_schedule(esp+4, esp+4)
  167. SSE2_pass(mm1, mm2, mm0, 9, esp+4)
  168. AS2( pxor mm0, [eax+0*8])
  169. AS2( movq [eax+0*8], mm0)
  170. AS2( psubq mm1, mm5)
  171. AS2( movq [eax+1*8], mm1)
  172. AS2( paddq mm2, [eax+2*8])
  173. AS2( movq [eax+2*8], mm2)
  174. AS1( pop esp)
  175. AS1( emms)
  176. #ifdef __GNUC__
  177. AS1( pop ebx)
  178. ".att_syntax prefix;"
  179. :
  180. : "a" (digest), "S" (X), "d" (table)
  181. : "%ecx", "%edi", "memory", "cc"
  182. );
  183. #endif
  184. }
  185. else
  186. #endif
  187. {
  188. word64 a = digest[0];
  189. word64 b = digest[1];
  190. word64 c = digest[2];
  191. word64 Y[8];
  192. #define t1 (table)
  193. #define t2 (table+256)
  194. #define t3 (table+256*2)
  195. #define t4 (table+256*3)
  196. #define round(a,b,c,x,mul) \
  197. c ^= x; \
  198. a -= t1[GETBYTE(c,0)] ^ t2[GETBYTE(c,2)] ^ t3[GETBYTE(c,4)] ^ t4[GETBYTE(c,6)]; \
  199. b += t4[GETBYTE(c,1)] ^ t3[GETBYTE(c,3)] ^ t2[GETBYTE(c,5)] ^ t1[GETBYTE(c,7)]; \
  200. b *= mul
  201. #define pass(a,b,c,mul,X) {\
  202. int i=0;\
  203. while (true)\
  204. {\
  205. round(a,b,c,X[i+0],mul); \
  206. round(b,c,a,X[i+1],mul); \
  207. if (i==6)\
  208. break;\
  209. round(c,a,b,X[i+2],mul); \
  210. i+=3;\
  211. }}
  212. #define key_schedule(Y,X) \
  213. Y[0] = X[0] - (X[7]^W64LIT(0xA5A5A5A5A5A5A5A5)); \
  214. Y[1] = X[1] ^ Y[0]; \
  215. Y[2] = X[2] + Y[1]; \
  216. Y[3] = X[3] - (Y[2] ^ ((~Y[1])<<19)); \
  217. Y[4] = X[4] ^ Y[3]; \
  218. Y[5] = X[5] + Y[4]; \
  219. Y[6] = X[6] - (Y[5] ^ ((~Y[4])>>23)); \
  220. Y[7] = X[7] ^ Y[6]; \
  221. Y[0] += Y[7]; \
  222. Y[1] -= Y[0] ^ ((~Y[7])<<19); \
  223. Y[2] ^= Y[1]; \
  224. Y[3] += Y[2]; \
  225. Y[4] -= Y[3] ^ ((~Y[2])>>23); \
  226. Y[5] ^= Y[4]; \
  227. Y[6] += Y[5]; \
  228. Y[7] -= Y[6] ^ W64LIT(0x0123456789ABCDEF)
  229. pass(a,b,c,5,X);
  230. key_schedule(Y,X);
  231. pass(c,a,b,7,Y);
  232. key_schedule(Y,Y);
  233. pass(b,c,a,9,Y);
  234. digest[0] = a ^ digest[0];
  235. digest[1] = b - digest[1];
  236. digest[2] = c + digest[2];
  237. }
  238. }
  239. NAMESPACE_END