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.

136 lines
4.2 KiB

  1. // shark.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #include "shark.h"
  4. #include "misc.h"
  5. #include "modes.h"
  6. #include "gf256.h"
  7. NAMESPACE_BEGIN(CryptoPP)
  8. static word64 SHARKTransform(word64 a)
  9. {
  10. static const byte iG[8][8] = {
  11. 0xe7, 0x30, 0x90, 0x85, 0xd0, 0x4b, 0x91, 0x41,
  12. 0x53, 0x95, 0x9b, 0xa5, 0x96, 0xbc, 0xa1, 0x68,
  13. 0x02, 0x45, 0xf7, 0x65, 0x5c, 0x1f, 0xb6, 0x52,
  14. 0xa2, 0xca, 0x22, 0x94, 0x44, 0x63, 0x2a, 0xa2,
  15. 0xfc, 0x67, 0x8e, 0x10, 0x29, 0x75, 0x85, 0x71,
  16. 0x24, 0x45, 0xa2, 0xcf, 0x2f, 0x22, 0xc1, 0x0e,
  17. 0xa1, 0xf1, 0x71, 0x40, 0x91, 0x27, 0x18, 0xa5,
  18. 0x56, 0xf4, 0xaf, 0x32, 0xd2, 0xa4, 0xdc, 0x71,
  19. };
  20. word64 result=0;
  21. GF256 gf256(0xf5);
  22. for (unsigned int i=0; i<8; i++)
  23. for(unsigned int j=0; j<8; j++)
  24. result ^= word64(gf256.Multiply(iG[i][j], GF256::Element(a>>(56-8*j)))) << (56-8*i);
  25. return result;
  26. }
  27. void SHARK::Base::UncheckedSetKey(const byte *key, unsigned int keyLen, const NameValuePairs &params)
  28. {
  29. AssertValidKeyLength(keyLen);
  30. m_rounds = GetRoundsAndThrowIfInvalid(params, this);
  31. m_roundKeys.New(m_rounds+1);
  32. // concatenate key enought times to fill a
  33. for (unsigned int i=0; i<(m_rounds+1)*8; i++)
  34. ((byte *)m_roundKeys.begin())[i] = key[i%keyLen];
  35. SHARK::Encryption e;
  36. e.InitForKeySetup();
  37. byte IV[8] = {0,0,0,0,0,0,0,0};
  38. CFB_Mode_ExternalCipher::Encryption cfb(e, IV);
  39. cfb.ProcessString((byte *)m_roundKeys.begin(), (m_rounds+1)*8);
  40. ConditionalByteReverse(BIG_ENDIAN_ORDER, m_roundKeys.begin(), m_roundKeys.begin(), (m_rounds+1)*8);
  41. m_roundKeys[m_rounds] = SHARKTransform(m_roundKeys[m_rounds]);
  42. if (!IsForwardTransformation())
  43. {
  44. unsigned int i;
  45. // transform encryption round keys into decryption round keys
  46. for (i=0; i<m_rounds/2; i++)
  47. std::swap(m_roundKeys[i], m_roundKeys[m_rounds-i]);
  48. for (i=1; i<m_rounds; i++)
  49. m_roundKeys[i] = SHARKTransform(m_roundKeys[i]);
  50. }
  51. #ifdef IS_LITTLE_ENDIAN
  52. m_roundKeys[0] = ByteReverse(m_roundKeys[0]);
  53. m_roundKeys[m_rounds] = ByteReverse(m_roundKeys[m_rounds]);
  54. #endif
  55. }
  56. // construct an SHARK_Enc object with fixed round keys, to be used to initialize actual round keys
  57. void SHARK::Enc::InitForKeySetup()
  58. {
  59. m_rounds = DEFAULT_ROUNDS;
  60. m_roundKeys.New(DEFAULT_ROUNDS+1);
  61. for (unsigned int i=0; i<DEFAULT_ROUNDS; i++)
  62. m_roundKeys[i] = cbox[0][i];
  63. m_roundKeys[DEFAULT_ROUNDS] = SHARKTransform(cbox[0][DEFAULT_ROUNDS]);
  64. #ifdef IS_LITTLE_ENDIAN
  65. m_roundKeys[0] = ByteReverse(m_roundKeys[0]);
  66. m_roundKeys[m_rounds] = ByteReverse(m_roundKeys[m_rounds]);
  67. #endif
  68. }
  69. typedef word64 ArrayOf256Word64s[256];
  70. template <const byte *sbox, const ArrayOf256Word64s *cbox>
  71. struct SharkProcessAndXorBlock{ // VC60 workaround: problem with template functions
  72. inline SharkProcessAndXorBlock(const word64 *roundKeys, unsigned int rounds, const byte *inBlock, const byte *xorBlock, byte *outBlock)
  73. {
  74. word64 tmp = *(word64 *)inBlock ^ roundKeys[0];
  75. ByteOrder order = GetNativeByteOrder();
  76. tmp = cbox[0][GetByte(order, tmp, 0)] ^ cbox[1][GetByte(order, tmp, 1)]
  77. ^ cbox[2][GetByte(order, tmp, 2)] ^ cbox[3][GetByte(order, tmp, 3)]
  78. ^ cbox[4][GetByte(order, tmp, 4)] ^ cbox[5][GetByte(order, tmp, 5)]
  79. ^ cbox[6][GetByte(order, tmp, 6)] ^ cbox[7][GetByte(order, tmp, 7)]
  80. ^ roundKeys[1];
  81. for(unsigned int i=2; i<rounds; i++)
  82. {
  83. tmp = cbox[0][GETBYTE(tmp, 7)] ^ cbox[1][GETBYTE(tmp, 6)]
  84. ^ cbox[2][GETBYTE(tmp, 5)] ^ cbox[3][GETBYTE(tmp, 4)]
  85. ^ cbox[4][GETBYTE(tmp, 3)] ^ cbox[5][GETBYTE(tmp, 2)]
  86. ^ cbox[6][GETBYTE(tmp, 1)] ^ cbox[7][GETBYTE(tmp, 0)]
  87. ^ roundKeys[i];
  88. }
  89. PutBlock<byte, BigEndian>(xorBlock, outBlock)
  90. (sbox[GETBYTE(tmp, 7)])
  91. (sbox[GETBYTE(tmp, 6)])
  92. (sbox[GETBYTE(tmp, 5)])
  93. (sbox[GETBYTE(tmp, 4)])
  94. (sbox[GETBYTE(tmp, 3)])
  95. (sbox[GETBYTE(tmp, 2)])
  96. (sbox[GETBYTE(tmp, 1)])
  97. (sbox[GETBYTE(tmp, 0)]);
  98. *(word64 *)outBlock ^= roundKeys[rounds];
  99. }};
  100. void SHARK::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  101. {
  102. SharkProcessAndXorBlock<sbox, cbox>(m_roundKeys, m_rounds, inBlock, xorBlock, outBlock);
  103. }
  104. void SHARK::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  105. {
  106. SharkProcessAndXorBlock<sbox, cbox>(m_roundKeys, m_rounds, inBlock, xorBlock, outBlock);
  107. }
  108. NAMESPACE_END