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.

213 lines
4.2 KiB

  1. // seal.cpp - written and placed in the public domain by Wei Dai
  2. // updated to SEAL 3.0 by Leonard Janke
  3. #include "pch.h"
  4. #include "seal.h"
  5. #include "sha.h"
  6. #include "misc.h"
  7. NAMESPACE_BEGIN(CryptoPP)
  8. void SEAL_TestInstantiations()
  9. {
  10. SEAL<>::Encryption x;
  11. }
  12. struct SEAL_Gamma
  13. {
  14. SEAL_Gamma(const byte *key)
  15. : H(5), Z(5), D(16), lastIndex(0xffffffff)
  16. {
  17. GetUserKey(BIG_ENDIAN_ORDER, H.begin(), 5, key, 20);
  18. memset(D, 0, 64);
  19. }
  20. word32 Apply(word32 i);
  21. SecBlock<word32> H, Z, D;
  22. word32 lastIndex;
  23. };
  24. word32 SEAL_Gamma::Apply(word32 i)
  25. {
  26. word32 shaIndex = i/5;
  27. if (shaIndex != lastIndex)
  28. {
  29. memcpy(Z, H, 20);
  30. D[0] = shaIndex;
  31. SHA::Transform(Z, D);
  32. lastIndex = shaIndex;
  33. }
  34. return Z[i%5];
  35. }
  36. template <class B>
  37. void SEAL_Policy<B>::CipherSetKey(const NameValuePairs &params, const byte *key, size_t length)
  38. {
  39. m_insideCounter = m_outsideCounter = m_startCount = 0;
  40. unsigned int L = params.GetIntValueWithDefault("NumberOfOutputBitsPerPositionIndex", 32*1024);
  41. m_iterationsPerCount = L / 8192;
  42. SEAL_Gamma gamma(key);
  43. unsigned int i;
  44. for (i=0; i<512; i++)
  45. m_T[i] = gamma.Apply(i);
  46. for (i=0; i<256; i++)
  47. m_S[i] = gamma.Apply(0x1000+i);
  48. m_R.New(4*(L/8192));
  49. for (i=0; i<m_R.size(); i++)
  50. m_R[i] = gamma.Apply(0x2000+i);
  51. }
  52. template <class B>
  53. void SEAL_Policy<B>::CipherResynchronize(byte *keystreamBuffer, const byte *IV, size_t length)
  54. {
  55. assert(length==4);
  56. m_outsideCounter = IV ? GetWord<word32>(false, BIG_ENDIAN_ORDER, IV) : 0;
  57. m_startCount = m_outsideCounter;
  58. m_insideCounter = 0;
  59. }
  60. template <class B>
  61. void SEAL_Policy<B>::SeekToIteration(lword iterationCount)
  62. {
  63. m_outsideCounter = m_startCount + (unsigned int)(iterationCount / m_iterationsPerCount);
  64. m_insideCounter = (unsigned int)(iterationCount % m_iterationsPerCount);
  65. }
  66. template <class B>
  67. void SEAL_Policy<B>::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
  68. {
  69. word32 a, b, c, d, n1, n2, n3, n4;
  70. unsigned int p, q;
  71. for (size_t iteration = 0; iteration < iterationCount; ++iteration)
  72. {
  73. #define Ttab(x) *(word32 *)((byte *)m_T.begin()+x)
  74. a = m_outsideCounter ^ m_R[4*m_insideCounter];
  75. b = rotrFixed(m_outsideCounter, 8U) ^ m_R[4*m_insideCounter+1];
  76. c = rotrFixed(m_outsideCounter, 16U) ^ m_R[4*m_insideCounter+2];
  77. d = rotrFixed(m_outsideCounter, 24U) ^ m_R[4*m_insideCounter+3];
  78. for (unsigned int j=0; j<2; j++)
  79. {
  80. p = a & 0x7fc;
  81. b += Ttab(p);
  82. a = rotrFixed(a, 9U);
  83. p = b & 0x7fc;
  84. c += Ttab(p);
  85. b = rotrFixed(b, 9U);
  86. p = c & 0x7fc;
  87. d += Ttab(p);
  88. c = rotrFixed(c, 9U);
  89. p = d & 0x7fc;
  90. a += Ttab(p);
  91. d = rotrFixed(d, 9U);
  92. }
  93. n1 = d, n2 = b, n3 = a, n4 = c;
  94. p = a & 0x7fc;
  95. b += Ttab(p);
  96. a = rotrFixed(a, 9U);
  97. p = b & 0x7fc;
  98. c += Ttab(p);
  99. b = rotrFixed(b, 9U);
  100. p = c & 0x7fc;
  101. d += Ttab(p);
  102. c = rotrFixed(c, 9U);
  103. p = d & 0x7fc;
  104. a += Ttab(p);
  105. d = rotrFixed(d, 9U);
  106. // generate 8192 bits
  107. for (unsigned int i=0; i<64; i++)
  108. {
  109. p = a & 0x7fc;
  110. a = rotrFixed(a, 9U);
  111. b += Ttab(p);
  112. b ^= a;
  113. q = b & 0x7fc;
  114. b = rotrFixed(b, 9U);
  115. c ^= Ttab(q);
  116. c += b;
  117. p = (p+c) & 0x7fc;
  118. c = rotrFixed(c, 9U);
  119. d += Ttab(p);
  120. d ^= c;
  121. q = (q+d) & 0x7fc;
  122. d = rotrFixed(d, 9U);
  123. a ^= Ttab(q);
  124. a += d;
  125. p = (p+a) & 0x7fc;
  126. b ^= Ttab(p);
  127. a = rotrFixed(a, 9U);
  128. q = (q+b) & 0x7fc;
  129. c += Ttab(q);
  130. b = rotrFixed(b, 9U);
  131. p = (p+c) & 0x7fc;
  132. d ^= Ttab(p);
  133. c = rotrFixed(c, 9U);
  134. q = (q+d) & 0x7fc;
  135. d = rotrFixed(d, 9U);
  136. a += Ttab(q);
  137. #define SEAL_OUTPUT(x) \
  138. CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 0, b + m_S[4*i+0]);\
  139. CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 1, c ^ m_S[4*i+1]);\
  140. CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 2, d + m_S[4*i+2]);\
  141. CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, B::ToEnum(), 3, a ^ m_S[4*i+3]);
  142. CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(SEAL_OUTPUT, 4*4);
  143. if (i & 1)
  144. {
  145. a += n3;
  146. b += n4;
  147. c ^= n3;
  148. d ^= n4;
  149. }
  150. else
  151. {
  152. a += n1;
  153. b += n2;
  154. c ^= n1;
  155. d ^= n2;
  156. }
  157. }
  158. if (++m_insideCounter == m_iterationsPerCount)
  159. {
  160. ++m_outsideCounter;
  161. m_insideCounter = 0;
  162. }
  163. }
  164. a = b = c = d = n1 = n2 = n3 = n4 = 0;
  165. p = q = 0;
  166. }
  167. template class SEAL_Policy<BigEndian>;
  168. template class SEAL_Policy<LittleEndian>;
  169. NAMESPACE_END