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.

139 lines
3.2 KiB

  1. // 3way.cpp - modifed by Wei Dai from Joan Daemen's 3way.c
  2. // The original code and all modifications are in the public domain.
  3. #include "pch.h"
  4. #include "3way.h"
  5. #include "misc.h"
  6. NAMESPACE_BEGIN(CryptoPP)
  7. void ThreeWay_TestInstantiations()
  8. {
  9. ThreeWay::Encryption x1;
  10. ThreeWay::Decryption x2;
  11. }
  12. static const word32 START_E = 0x0b0b; // round constant of first encryption round
  13. static const word32 START_D = 0xb1b1; // round constant of first decryption round
  14. static const word32 RC_MODULUS = 0x11011;
  15. static inline word32 reverseBits(word32 a)
  16. {
  17. a = ((a & 0xAAAAAAAA) >> 1) | ((a & 0x55555555) << 1);
  18. a = ((a & 0xCCCCCCCC) >> 2) | ((a & 0x33333333) << 2);
  19. return ((a & 0xF0F0F0F0) >> 4) | ((a & 0x0F0F0F0F) << 4);
  20. }
  21. #define mu(a0, a1, a2) \
  22. { \
  23. a1 = reverseBits(a1); \
  24. word32 t = reverseBits(a0); \
  25. a0 = reverseBits(a2); \
  26. a2 = t; \
  27. }
  28. #define pi_gamma_pi(a0, a1, a2) \
  29. { \
  30. word32 b0, b2; \
  31. b2 = rotlFixed(a2, 1U); \
  32. b0 = rotlFixed(a0, 22U); \
  33. a0 = rotlFixed(b0 ^ (a1|(~b2)), 1U); \
  34. a2 = rotlFixed(b2 ^ (b0|(~a1)), 22U);\
  35. a1 ^= (b2|(~b0)); \
  36. }
  37. // thanks to Paulo Barreto for this optimized theta()
  38. #define theta(a0, a1, a2) \
  39. { \
  40. word32 b0, b1, c; \
  41. c = a0 ^ a1 ^ a2; \
  42. c = rotlFixed(c, 16U) ^ rotlFixed(c, 8U); \
  43. b0 = (a0 << 24) ^ (a2 >> 8) ^ (a1 << 8) ^ (a0 >> 24); \
  44. b1 = (a1 << 24) ^ (a0 >> 8) ^ (a2 << 8) ^ (a1 >> 24); \
  45. a0 ^= c ^ b0; \
  46. a1 ^= c ^ b1; \
  47. a2 ^= c ^ (b0 >> 16) ^ (b1 << 16); \
  48. }
  49. #define rho(a0, a1, a2) \
  50. { \
  51. theta(a0, a1, a2); \
  52. pi_gamma_pi(a0, a1, a2); \
  53. }
  54. void ThreeWay::Base::UncheckedSetKey(const byte *uk, unsigned int length, const NameValuePairs &params)
  55. {
  56. AssertValidKeyLength(length);
  57. m_rounds = GetRoundsAndThrowIfInvalid(params, this);
  58. for (unsigned int i=0; i<3; i++)
  59. m_k[i] = (word32)uk[4*i+3] | ((word32)uk[4*i+2]<<8) | ((word32)uk[4*i+1]<<16) | ((word32)uk[4*i]<<24);
  60. if (!IsForwardTransformation())
  61. {
  62. theta(m_k[0], m_k[1], m_k[2]);
  63. mu(m_k[0], m_k[1], m_k[2]);
  64. m_k[0] = ByteReverse(m_k[0]);
  65. m_k[1] = ByteReverse(m_k[1]);
  66. m_k[2] = ByteReverse(m_k[2]);
  67. }
  68. }
  69. void ThreeWay::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  70. {
  71. typedef BlockGetAndPut<word32, BigEndian> Block;
  72. word32 a0, a1, a2;
  73. Block::Get(inBlock)(a0)(a1)(a2);
  74. word32 rc = START_E;
  75. for(unsigned i=0; i<m_rounds; i++)
  76. {
  77. a0 ^= m_k[0] ^ (rc<<16);
  78. a1 ^= m_k[1];
  79. a2 ^= m_k[2] ^ rc;
  80. rho(a0, a1, a2);
  81. rc <<= 1;
  82. if (rc&0x10000) rc ^= 0x11011;
  83. }
  84. a0 ^= m_k[0] ^ (rc<<16);
  85. a1 ^= m_k[1];
  86. a2 ^= m_k[2] ^ rc;
  87. theta(a0, a1, a2);
  88. Block::Put(xorBlock, outBlock)(a0)(a1)(a2);
  89. }
  90. void ThreeWay::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  91. {
  92. typedef BlockGetAndPut<word32, LittleEndian> Block;
  93. word32 a0, a1, a2;
  94. Block::Get(inBlock)(a0)(a1)(a2);
  95. word32 rc = START_D;
  96. mu(a0, a1, a2);
  97. for(unsigned i=0; i<m_rounds; i++)
  98. {
  99. a0 ^= m_k[0] ^ (rc<<16);
  100. a1 ^= m_k[1];
  101. a2 ^= m_k[2] ^ rc;
  102. rho(a0, a1, a2);
  103. rc <<= 1;
  104. if (rc&0x10000) rc ^= 0x11011;
  105. }
  106. a0 ^= m_k[0] ^ (rc<<16);
  107. a1 ^= m_k[1];
  108. a2 ^= m_k[2] ^ rc;
  109. theta(a0, a1, a2);
  110. mu(a0, a1, a2);
  111. Block::Put(xorBlock, outBlock)(a0)(a1)(a2);
  112. }
  113. NAMESPACE_END