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.

192 lines
3.8 KiB

  1. // idea.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #include "idea.h"
  4. #include "misc.h"
  5. NAMESPACE_BEGIN(CryptoPP)
  6. static const int IDEA_KEYLEN=(6*IDEA::ROUNDS+4); // key schedule length in # of word16s
  7. #define low16(x) ((x)&0xffff) // compiler should be able to optimize this away if word is 16 bits
  8. #define high16(x) ((x)>>16)
  9. CRYPTOPP_COMPILE_ASSERT(sizeof(IDEA::Word) >= 2);
  10. // should use an inline function but macros are still faster in MSVC 4.0
  11. #define DirectMUL(a,b) \
  12. { \
  13. assert(b <= 0xffff); \
  14. \
  15. word32 p=(word32)low16(a)*b; \
  16. \
  17. if (p) \
  18. { \
  19. p = low16(p) - high16(p); \
  20. a = (IDEA::Word)p - (IDEA::Word)high16(p); \
  21. } \
  22. else \
  23. a = 1-a-b; \
  24. }
  25. #ifdef IDEA_LARGECACHE
  26. volatile bool IDEA::Base::tablesBuilt = false;
  27. word16 IDEA::Base::log[0x10000];
  28. word16 IDEA::Base::antilog[0x10000];
  29. void IDEA::Base::BuildLogTables()
  30. {
  31. if (tablesBuilt)
  32. return;
  33. else
  34. {
  35. tablesBuilt = true;
  36. IDEA::Word x=1;
  37. word32 i;
  38. for (i=0; i<0x10000; i++)
  39. {
  40. antilog[i] = (word16)x;
  41. DirectMUL(x, 3);
  42. }
  43. for (i=0; i<0x10000; i++)
  44. log[antilog[i]] = (word16)i;
  45. }
  46. }
  47. void IDEA::Base::LookupKeyLogs()
  48. {
  49. IDEA::Word* Z=key;
  50. int r=ROUNDS;
  51. do
  52. {
  53. Z[0] = log[Z[0]];
  54. Z[3] = log[Z[3]];
  55. Z[4] = log[Z[4]];
  56. Z[5] = log[Z[5]];
  57. Z+=6;
  58. } while (--r);
  59. Z[0] = log[Z[0]];
  60. Z[3] = log[Z[3]];
  61. }
  62. inline void IDEA::Base::LookupMUL(IDEA::Word &a, IDEA::Word b)
  63. {
  64. a = antilog[low16(log[low16(a)]+b)];
  65. }
  66. #endif // IDEA_LARGECACHE
  67. void IDEA::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)
  68. {
  69. AssertValidKeyLength(length);
  70. #ifdef IDEA_LARGECACHE
  71. BuildLogTables();
  72. #endif
  73. EnKey(userKey);
  74. if (!IsForwardTransformation())
  75. DeKey();
  76. #ifdef IDEA_LARGECACHE
  77. LookupKeyLogs();
  78. #endif
  79. }
  80. void IDEA::Base::EnKey (const byte *userKey)
  81. {
  82. unsigned int i;
  83. for (i=0; i<8; i++)
  84. m_key[i] = ((IDEA::Word)userKey[2*i]<<8) | userKey[2*i+1];
  85. for (; i<IDEA_KEYLEN; i++)
  86. {
  87. unsigned int j = RoundDownToMultipleOf(i,8U)-8;
  88. m_key[i] = low16((m_key[j+(i+1)%8] << 9) | (m_key[j+(i+2)%8] >> 7));
  89. }
  90. }
  91. static IDEA::Word MulInv(IDEA::Word x)
  92. {
  93. IDEA::Word y=x;
  94. for (unsigned i=0; i<15; i++)
  95. {
  96. DirectMUL(y,low16(y));
  97. DirectMUL(y,x);
  98. }
  99. return low16(y);
  100. }
  101. static inline IDEA::Word AddInv(IDEA::Word x)
  102. {
  103. return low16(0-x);
  104. }
  105. void IDEA::Base::DeKey()
  106. {
  107. FixedSizeSecBlock<IDEA::Word, 6*ROUNDS+4> tempkey;
  108. size_t i;
  109. for (i=0; i<ROUNDS; i++)
  110. {
  111. tempkey[i*6+0] = MulInv(m_key[(ROUNDS-i)*6+0]);
  112. tempkey[i*6+1] = AddInv(m_key[(ROUNDS-i)*6+1+(i>0)]);
  113. tempkey[i*6+2] = AddInv(m_key[(ROUNDS-i)*6+2-(i>0)]);
  114. tempkey[i*6+3] = MulInv(m_key[(ROUNDS-i)*6+3]);
  115. tempkey[i*6+4] = m_key[(ROUNDS-1-i)*6+4];
  116. tempkey[i*6+5] = m_key[(ROUNDS-1-i)*6+5];
  117. }
  118. tempkey[i*6+0] = MulInv(m_key[(ROUNDS-i)*6+0]);
  119. tempkey[i*6+1] = AddInv(m_key[(ROUNDS-i)*6+1]);
  120. tempkey[i*6+2] = AddInv(m_key[(ROUNDS-i)*6+2]);
  121. tempkey[i*6+3] = MulInv(m_key[(ROUNDS-i)*6+3]);
  122. m_key = tempkey;
  123. }
  124. #ifdef IDEA_LARGECACHE
  125. #define MUL(a,b) LookupMUL(a,b)
  126. #else
  127. #define MUL(a,b) DirectMUL(a,b)
  128. #endif
  129. void IDEA::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  130. {
  131. typedef BlockGetAndPut<word16, BigEndian> Block;
  132. const IDEA::Word *key = m_key;
  133. IDEA::Word x0,x1,x2,x3,t0,t1;
  134. Block::Get(inBlock)(x0)(x1)(x2)(x3);
  135. for (unsigned int i=0; i<ROUNDS; i++)
  136. {
  137. MUL(x0, key[i*6+0]);
  138. x1 += key[i*6+1];
  139. x2 += key[i*6+2];
  140. MUL(x3, key[i*6+3]);
  141. t0 = x0^x2;
  142. MUL(t0, key[i*6+4]);
  143. t1 = t0 + (x1^x3);
  144. MUL(t1, key[i*6+5]);
  145. t0 += t1;
  146. x0 ^= t1;
  147. x3 ^= t0;
  148. t0 ^= x1;
  149. x1 = x2^t1;
  150. x2 = t0;
  151. }
  152. MUL(x0, key[ROUNDS*6+0]);
  153. x2 += key[ROUNDS*6+1];
  154. x1 += key[ROUNDS*6+2];
  155. MUL(x3, key[ROUNDS*6+3]);
  156. Block::Put(xorBlock, outBlock)(x0)(x2)(x1)(x3);
  157. }
  158. NAMESPACE_END