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.

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