Team Fortress 2 Source Code as on 22/4/2020
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.

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