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.

177 lines
5.1 KiB

  1. // square.cpp - written and placed in the public domain by Wei Dai
  2. // Based on Paulo S.L.M. Barreto's public domain implementation
  3. #include "pch.h"
  4. #include "square.h"
  5. #include "misc.h"
  6. #include "gf256.h"
  7. NAMESPACE_BEGIN(CryptoPP)
  8. // apply theta to a roundkey
  9. static void SquareTransform (word32 in[4], word32 out[4])
  10. {
  11. static const byte G[4][4] =
  12. {
  13. 0x02U, 0x01U, 0x01U, 0x03U,
  14. 0x03U, 0x02U, 0x01U, 0x01U,
  15. 0x01U, 0x03U, 0x02U, 0x01U,
  16. 0x01U, 0x01U, 0x03U, 0x02U
  17. };
  18. GF256 gf256(0xf5);
  19. for (int i = 0; i < 4; i++)
  20. {
  21. word32 temp = 0;
  22. for (int j = 0; j < 4; j++)
  23. for (int k = 0; k < 4; k++)
  24. temp ^= (word32)gf256.Multiply(GETBYTE(in[i], 3-k), G[k][j]) << ((3-j)*8);
  25. out[i] = temp;
  26. }
  27. }
  28. #define roundkeys(i, j) m_roundkeys[(i)*4+(j)]
  29. #define roundkeys4(i) (m_roundkeys+(i)*4)
  30. void Square::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)
  31. {
  32. AssertValidKeyLength(length);
  33. static const word32 offset[ROUNDS] = {
  34. 0x01000000UL, 0x02000000UL, 0x04000000UL, 0x08000000UL,
  35. 0x10000000UL, 0x20000000UL, 0x40000000UL, 0x80000000UL,
  36. };
  37. GetUserKey(BIG_ENDIAN_ORDER, m_roundkeys.data(), KEYLENGTH/4, userKey, KEYLENGTH);
  38. /* apply the key evolution function */
  39. for (int i = 1; i < ROUNDS+1; i++)
  40. {
  41. roundkeys(i, 0) = roundkeys(i-1, 0) ^ rotlFixed(roundkeys(i-1, 3), 8U) ^ offset[i-1];
  42. roundkeys(i, 1) = roundkeys(i-1, 1) ^ roundkeys(i, 0);
  43. roundkeys(i, 2) = roundkeys(i-1, 2) ^ roundkeys(i, 1);
  44. roundkeys(i, 3) = roundkeys(i-1, 3) ^ roundkeys(i, 2);
  45. }
  46. /* produce the round keys */
  47. if (IsForwardTransformation())
  48. {
  49. for (int i = 0; i < ROUNDS; i++)
  50. SquareTransform (roundkeys4(i), roundkeys4(i));
  51. }
  52. else
  53. {
  54. for (int i = 0; i < ROUNDS/2; i++)
  55. for (int j = 0; j < 4; j++)
  56. std::swap(roundkeys(i, j), roundkeys(ROUNDS-i, j));
  57. SquareTransform (roundkeys4(ROUNDS), roundkeys4(ROUNDS));
  58. }
  59. }
  60. #define MSB(x) (((x) >> 24) & 0xffU) /* most significant byte */
  61. #define SSB(x) (((x) >> 16) & 0xffU) /* second in significance */
  62. #define TSB(x) (((x) >> 8) & 0xffU) /* third in significance */
  63. #define LSB(x) (((x) ) & 0xffU) /* least significant byte */
  64. #define squareRound(text, temp, T0, T1, T2, T3, roundkey) \
  65. { \
  66. temp[0] = T0[MSB (text[0])] \
  67. ^ T1[MSB (text[1])] \
  68. ^ T2[MSB (text[2])] \
  69. ^ T3[MSB (text[3])] \
  70. ^ roundkey[0]; \
  71. temp[1] = T0[SSB (text[0])] \
  72. ^ T1[SSB (text[1])] \
  73. ^ T2[SSB (text[2])] \
  74. ^ T3[SSB (text[3])] \
  75. ^ roundkey[1]; \
  76. temp[2] = T0[TSB (text[0])] \
  77. ^ T1[TSB (text[1])] \
  78. ^ T2[TSB (text[2])] \
  79. ^ T3[TSB (text[3])] \
  80. ^ roundkey[2]; \
  81. temp[3] = T0[LSB (text[0])] \
  82. ^ T1[LSB (text[1])] \
  83. ^ T2[LSB (text[2])] \
  84. ^ T3[LSB (text[3])] \
  85. ^ roundkey[3]; \
  86. } /* squareRound */
  87. #define squareFinal(text, temp, S, roundkey) \
  88. { \
  89. text[0] = ((word32) (S[MSB (temp[0])]) << 24) \
  90. ^ ((word32) (S[MSB (temp[1])]) << 16) \
  91. ^ ((word32) (S[MSB (temp[2])]) << 8) \
  92. ^ (word32) (S[MSB (temp[3])]) \
  93. ^ roundkey[0]; \
  94. text[1] = ((word32) (S[SSB (temp[0])]) << 24) \
  95. ^ ((word32) (S[SSB (temp[1])]) << 16) \
  96. ^ ((word32) (S[SSB (temp[2])]) << 8) \
  97. ^ (word32) (S[SSB (temp[3])]) \
  98. ^ roundkey[1]; \
  99. text[2] = ((word32) (S[TSB (temp[0])]) << 24) \
  100. ^ ((word32) (S[TSB (temp[1])]) << 16) \
  101. ^ ((word32) (S[TSB (temp[2])]) << 8) \
  102. ^ (word32) (S[TSB (temp[3])]) \
  103. ^ roundkey[2]; \
  104. text[3] = ((word32) (S[LSB (temp[0])]) << 24) \
  105. ^ ((word32) (S[LSB (temp[1])]) << 16) \
  106. ^ ((word32) (S[LSB (temp[2])]) << 8) \
  107. ^ (word32) (S[LSB (temp[3])]) \
  108. ^ roundkey[3]; \
  109. } /* squareFinal */
  110. typedef BlockGetAndPut<word32, BigEndian> Block;
  111. void Square::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  112. {
  113. word32 text[4], temp[4];
  114. Block::Get(inBlock)(text[0])(text[1])(text[2])(text[3]);
  115. /* initial key addition */
  116. text[0] ^= roundkeys(0, 0);
  117. text[1] ^= roundkeys(0, 1);
  118. text[2] ^= roundkeys(0, 2);
  119. text[3] ^= roundkeys(0, 3);
  120. /* ROUNDS - 1 full rounds */
  121. for (int i=1; i+1<ROUNDS; i+=2)
  122. {
  123. squareRound (text, temp, Te[0], Te[1], Te[2], Te[3], roundkeys4(i));
  124. squareRound (temp, text, Te[0], Te[1], Te[2], Te[3], roundkeys4(i+1));
  125. }
  126. squareRound (text, temp, Te[0], Te[1], Te[2], Te[3], roundkeys4(ROUNDS-1));
  127. /* last round (diffusion becomes only transposition) */
  128. squareFinal (text, temp, Se, roundkeys4(ROUNDS));
  129. Block::Put(xorBlock, outBlock)(text[0])(text[1])(text[2])(text[3]);
  130. }
  131. void Square::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  132. {
  133. word32 text[4], temp[4];
  134. Block::Get(inBlock)(text[0])(text[1])(text[2])(text[3]);
  135. /* initial key addition */
  136. text[0] ^= roundkeys(0, 0);
  137. text[1] ^= roundkeys(0, 1);
  138. text[2] ^= roundkeys(0, 2);
  139. text[3] ^= roundkeys(0, 3);
  140. /* ROUNDS - 1 full rounds */
  141. for (int i=1; i+1<ROUNDS; i+=2)
  142. {
  143. squareRound (text, temp, Td[0], Td[1], Td[2], Td[3], roundkeys4(i));
  144. squareRound (temp, text, Td[0], Td[1], Td[2], Td[3], roundkeys4(i+1));
  145. }
  146. squareRound (text, temp, Td[0], Td[1], Td[2], Td[3], roundkeys4(ROUNDS-1));
  147. /* last round (diffusion becomes only transposition) */
  148. squareFinal (text, temp, Sd, roundkeys4(ROUNDS));
  149. Block::Put(xorBlock, outBlock)(text[0])(text[1])(text[2])(text[3]);
  150. }
  151. NAMESPACE_END