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.

159 lines
3.7 KiB

  1. // tea.cpp - modified by Wei Dai from code in the original paper
  2. #include "pch.h"
  3. #include "tea.h"
  4. #include "misc.h"
  5. NAMESPACE_BEGIN(CryptoPP)
  6. static const word32 DELTA = 0x9e3779b9;
  7. typedef BlockGetAndPut<word32, BigEndian> Block;
  8. void TEA::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
  9. {
  10. AssertValidKeyLength(length);
  11. GetUserKey(BIG_ENDIAN_ORDER, m_k.begin(), 4, userKey, KEYLENGTH);
  12. m_limit = GetRoundsAndThrowIfInvalid(params, this) * DELTA;
  13. }
  14. void TEA::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  15. {
  16. word32 y, z;
  17. Block::Get(inBlock)(y)(z);
  18. word32 sum = 0;
  19. while (sum != m_limit)
  20. {
  21. sum += DELTA;
  22. y += (z << 4) + m_k[0] ^ z + sum ^ (z >> 5) + m_k[1];
  23. z += (y << 4) + m_k[2] ^ y + sum ^ (y >> 5) + m_k[3];
  24. }
  25. Block::Put(xorBlock, outBlock)(y)(z);
  26. }
  27. void TEA::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  28. {
  29. word32 y, z;
  30. Block::Get(inBlock)(y)(z);
  31. word32 sum = m_limit;
  32. while (sum != 0)
  33. {
  34. z -= (y << 4) + m_k[2] ^ y + sum ^ (y >> 5) + m_k[3];
  35. y -= (z << 4) + m_k[0] ^ z + sum ^ (z >> 5) + m_k[1];
  36. sum -= DELTA;
  37. }
  38. Block::Put(xorBlock, outBlock)(y)(z);
  39. }
  40. void XTEA::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
  41. {
  42. AssertValidKeyLength(length);
  43. GetUserKey(BIG_ENDIAN_ORDER, m_k.begin(), 4, userKey, KEYLENGTH);
  44. m_limit = GetRoundsAndThrowIfInvalid(params, this) * DELTA;
  45. }
  46. void XTEA::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  47. {
  48. word32 y, z;
  49. Block::Get(inBlock)(y)(z);
  50. #ifdef __SUNPRO_CC
  51. // workaround needed on Sun Studio 12u1 Sun C++ 5.10 SunOS_i386 128229-02 2009/09/21
  52. size_t sum = 0;
  53. while ((sum&0xffffffff) != m_limit)
  54. #else
  55. word32 sum = 0;
  56. while (sum != m_limit)
  57. #endif
  58. {
  59. y += (z<<4 ^ z>>5) + z ^ sum + m_k[sum&3];
  60. sum += DELTA;
  61. z += (y<<4 ^ y>>5) + y ^ sum + m_k[sum>>11 & 3];
  62. }
  63. Block::Put(xorBlock, outBlock)(y)(z);
  64. }
  65. void XTEA::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  66. {
  67. word32 y, z;
  68. Block::Get(inBlock)(y)(z);
  69. #ifdef __SUNPRO_CC
  70. // workaround needed on Sun Studio 12u1 Sun C++ 5.10 SunOS_i386 128229-02 2009/09/21
  71. size_t sum = m_limit;
  72. while ((sum&0xffffffff) != 0)
  73. #else
  74. word32 sum = m_limit;
  75. while (sum != 0)
  76. #endif
  77. {
  78. z -= (y<<4 ^ y>>5) + y ^ sum + m_k[sum>>11 & 3];
  79. sum -= DELTA;
  80. y -= (z<<4 ^ z>>5) + z ^ sum + m_k[sum&3];
  81. }
  82. Block::Put(xorBlock, outBlock)(y)(z);
  83. }
  84. #define MX (z>>5^y<<2)+(y>>3^z<<4)^(sum^y)+(m_k[p&3^e]^z)
  85. void BTEA::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  86. {
  87. unsigned int n = m_blockSize / 4;
  88. word32 *v = (word32*)outBlock;
  89. ConditionalByteReverse(BIG_ENDIAN_ORDER, v, (const word32*)inBlock, m_blockSize);
  90. word32 y = v[0], z = v[n-1], e;
  91. word32 p, q = 6+52/n;
  92. word32 sum = 0;
  93. while (q-- > 0)
  94. {
  95. sum += DELTA;
  96. e = sum>>2 & 3;
  97. for (p = 0; p < n-1; p++)
  98. {
  99. y = v[p+1];
  100. z = v[p] += MX;
  101. }
  102. y = v[0];
  103. z = v[n-1] += MX;
  104. }
  105. ConditionalByteReverse(BIG_ENDIAN_ORDER, v, v, m_blockSize);
  106. }
  107. void BTEA::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  108. {
  109. unsigned int n = m_blockSize / 4;
  110. word32 *v = (word32*)outBlock;
  111. ConditionalByteReverse(BIG_ENDIAN_ORDER, v, (const word32*)inBlock, m_blockSize);
  112. word32 y = v[0], z = v[n-1], e;
  113. word32 p, q = 6+52/n;
  114. word32 sum = q * DELTA;
  115. while (sum != 0)
  116. {
  117. e = sum>>2 & 3;
  118. for (p = n-1; p > 0; p--)
  119. {
  120. z = v[p-1];
  121. y = v[p] -= MX;
  122. }
  123. z = v[n-1];
  124. y = v[0] -= MX;
  125. sum -= DELTA;
  126. }
  127. ConditionalByteReverse(BIG_ENDIAN_ORDER, v, v, m_blockSize);
  128. }
  129. NAMESPACE_END