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.

169 lines
4.1 KiB

  1. // twofish.cpp - modified by Wei Dai from Matthew Skala's twofish.c
  2. // The original code and all modifications are in the public domain.
  3. #include "pch.h"
  4. #include "twofish.h"
  5. #include "secblock.h"
  6. #include "misc.h"
  7. NAMESPACE_BEGIN(CryptoPP)
  8. // compute (c * x^4) mod (x^4 + (a + 1/a) * x^3 + a * x^2 + (a + 1/a) * x + 1)
  9. // over GF(256)
  10. static inline unsigned int Mod(unsigned int c)
  11. {
  12. static const unsigned int modulus = 0x14d;
  13. unsigned int c2 = (c<<1) ^ ((c & 0x80) ? modulus : 0);
  14. unsigned int c1 = c2 ^ (c>>1) ^ ((c & 1) ? (modulus>>1) : 0);
  15. return c | (c1 << 8) | (c2 << 16) | (c1 << 24);
  16. }
  17. // compute RS(12,8) code with the above polynomial as generator
  18. // this is equivalent to multiplying by the RS matrix
  19. static word32 ReedSolomon(word32 high, word32 low)
  20. {
  21. for (unsigned int i=0; i<8; i++)
  22. {
  23. high = Mod(high>>24) ^ (high<<8) ^ (low>>24);
  24. low <<= 8;
  25. }
  26. return high;
  27. }
  28. inline word32 Twofish::Base::h0(word32 x, const word32 *key, unsigned int kLen)
  29. {
  30. x = x | (x<<8) | (x<<16) | (x<<24);
  31. switch(kLen)
  32. {
  33. #define Q(a, b, c, d, t) q[a][GETBYTE(t,0)] ^ (q[b][GETBYTE(t,1)] << 8) ^ (q[c][GETBYTE(t,2)] << 16) ^ (q[d][GETBYTE(t,3)] << 24)
  34. case 4: x = Q(1, 0, 0, 1, x) ^ key[6];
  35. case 3: x = Q(1, 1, 0, 0, x) ^ key[4];
  36. case 2: x = Q(0, 1, 0, 1, x) ^ key[2];
  37. x = Q(0, 0, 1, 1, x) ^ key[0];
  38. }
  39. return x;
  40. }
  41. inline word32 Twofish::Base::h(word32 x, const word32 *key, unsigned int kLen)
  42. {
  43. x = h0(x, key, kLen);
  44. return mds[0][GETBYTE(x,0)] ^ mds[1][GETBYTE(x,1)] ^ mds[2][GETBYTE(x,2)] ^ mds[3][GETBYTE(x,3)];
  45. }
  46. void Twofish::Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &)
  47. {
  48. AssertValidKeyLength(keylength);
  49. unsigned int len = (keylength <= 16 ? 2 : (keylength <= 24 ? 3 : 4));
  50. SecBlock<word32> key(len*2);
  51. GetUserKey(LITTLE_ENDIAN_ORDER, key.begin(), len*2, userKey, keylength);
  52. unsigned int i;
  53. for (i=0; i<40; i+=2)
  54. {
  55. word32 a = h(i, key, len);
  56. word32 b = rotlFixed(h(i+1, key+1, len), 8);
  57. m_k[i] = a+b;
  58. m_k[i+1] = rotlFixed(a+2*b, 9);
  59. }
  60. SecBlock<word32> svec(2*len);
  61. for (i=0; i<len; i++)
  62. svec[2*(len-i-1)] = ReedSolomon(key[2*i+1], key[2*i]);
  63. for (i=0; i<256; i++)
  64. {
  65. word32 t = h0(i, svec, len);
  66. m_s[0*256+i] = mds[0][GETBYTE(t, 0)];
  67. m_s[1*256+i] = mds[1][GETBYTE(t, 1)];
  68. m_s[2*256+i] = mds[2][GETBYTE(t, 2)];
  69. m_s[3*256+i] = mds[3][GETBYTE(t, 3)];
  70. }
  71. }
  72. #define G1(x) (m_s[0*256+GETBYTE(x,0)] ^ m_s[1*256+GETBYTE(x,1)] ^ m_s[2*256+GETBYTE(x,2)] ^ m_s[3*256+GETBYTE(x,3)])
  73. #define G2(x) (m_s[0*256+GETBYTE(x,3)] ^ m_s[1*256+GETBYTE(x,0)] ^ m_s[2*256+GETBYTE(x,1)] ^ m_s[3*256+GETBYTE(x,2)])
  74. #define ENCROUND(n, a, b, c, d) \
  75. x = G1 (a); y = G2 (b); \
  76. x += y; y += x + k[2 * (n) + 1]; \
  77. (c) ^= x + k[2 * (n)]; \
  78. (c) = rotrFixed(c, 1); \
  79. (d) = rotlFixed(d, 1) ^ y
  80. #define ENCCYCLE(n) \
  81. ENCROUND (2 * (n), a, b, c, d); \
  82. ENCROUND (2 * (n) + 1, c, d, a, b)
  83. #define DECROUND(n, a, b, c, d) \
  84. x = G1 (a); y = G2 (b); \
  85. x += y; y += x; \
  86. (d) ^= y + k[2 * (n) + 1]; \
  87. (d) = rotrFixed(d, 1); \
  88. (c) = rotlFixed(c, 1); \
  89. (c) ^= (x + k[2 * (n)])
  90. #define DECCYCLE(n) \
  91. DECROUND (2 * (n) + 1, c, d, a, b); \
  92. DECROUND (2 * (n), a, b, c, d)
  93. typedef BlockGetAndPut<word32, LittleEndian> Block;
  94. void Twofish::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  95. {
  96. word32 x, y, a, b, c, d;
  97. Block::Get(inBlock)(a)(b)(c)(d);
  98. a ^= m_k[0];
  99. b ^= m_k[1];
  100. c ^= m_k[2];
  101. d ^= m_k[3];
  102. const word32 *k = m_k+8;
  103. ENCCYCLE (0);
  104. ENCCYCLE (1);
  105. ENCCYCLE (2);
  106. ENCCYCLE (3);
  107. ENCCYCLE (4);
  108. ENCCYCLE (5);
  109. ENCCYCLE (6);
  110. ENCCYCLE (7);
  111. c ^= m_k[4];
  112. d ^= m_k[5];
  113. a ^= m_k[6];
  114. b ^= m_k[7];
  115. Block::Put(xorBlock, outBlock)(c)(d)(a)(b);
  116. }
  117. void Twofish::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  118. {
  119. word32 x, y, a, b, c, d;
  120. Block::Get(inBlock)(c)(d)(a)(b);
  121. c ^= m_k[4];
  122. d ^= m_k[5];
  123. a ^= m_k[6];
  124. b ^= m_k[7];
  125. const word32 *k = m_k+8;
  126. DECCYCLE (7);
  127. DECCYCLE (6);
  128. DECCYCLE (5);
  129. DECCYCLE (4);
  130. DECCYCLE (3);
  131. DECCYCLE (2);
  132. DECCYCLE (1);
  133. DECCYCLE (0);
  134. a ^= m_k[0];
  135. b ^= m_k[1];
  136. c ^= m_k[2];
  137. d ^= m_k[3];
  138. Block::Put(xorBlock, outBlock)(a)(b)(c)(d);
  139. }
  140. NAMESPACE_END