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.

123 lines
2.8 KiB

  1. // serpent.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #include "serpent.h"
  4. #include "misc.h"
  5. #include "serpentp.h"
  6. NAMESPACE_BEGIN(CryptoPP)
  7. void Serpent_KeySchedule(word32 *k, unsigned int rounds, const byte *userKey, size_t keylen)
  8. {
  9. FixedSizeSecBlock<word32, 8> k0;
  10. GetUserKey(LITTLE_ENDIAN_ORDER, k0.begin(), 8, userKey, keylen);
  11. if (keylen < 32)
  12. k0[keylen/4] |= word32(1) << ((keylen%4)*8);
  13. word32 t = k0[7];
  14. unsigned int i;
  15. for (i = 0; i < 8; ++i)
  16. k[i] = k0[i] = t = rotlFixed(k0[i] ^ k0[(i+3)%8] ^ k0[(i+5)%8] ^ t ^ 0x9e3779b9 ^ i, 11);
  17. for (i = 8; i < 4*(rounds+1); ++i)
  18. k[i] = t = rotlFixed(k[i-8] ^ k[i-5] ^ k[i-3] ^ t ^ 0x9e3779b9 ^ i, 11);
  19. k -= 20;
  20. word32 a,b,c,d,e;
  21. for (i=0; i<rounds/8; i++)
  22. {
  23. afterS2(LK); afterS2(S3); afterS3(SK);
  24. afterS1(LK); afterS1(S2); afterS2(SK);
  25. afterS0(LK); afterS0(S1); afterS1(SK);
  26. beforeS0(LK); beforeS0(S0); afterS0(SK);
  27. k += 8*4;
  28. afterS6(LK); afterS6(S7); afterS7(SK);
  29. afterS5(LK); afterS5(S6); afterS6(SK);
  30. afterS4(LK); afterS4(S5); afterS5(SK);
  31. afterS3(LK); afterS3(S4); afterS4(SK);
  32. }
  33. afterS2(LK); afterS2(S3); afterS3(SK);
  34. }
  35. void Serpent::Base::UncheckedSetKey(const byte *userKey, unsigned int keylen, const NameValuePairs &)
  36. {
  37. AssertValidKeyLength(keylen);
  38. Serpent_KeySchedule(m_key, 32, userKey, keylen);
  39. }
  40. typedef BlockGetAndPut<word32, LittleEndian> Block;
  41. void Serpent::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  42. {
  43. word32 a, b, c, d, e;
  44. Block::Get(inBlock)(a)(b)(c)(d);
  45. const word32 *k = m_key;
  46. unsigned int i=1;
  47. do
  48. {
  49. beforeS0(KX); beforeS0(S0); afterS0(LT);
  50. afterS0(KX); afterS0(S1); afterS1(LT);
  51. afterS1(KX); afterS1(S2); afterS2(LT);
  52. afterS2(KX); afterS2(S3); afterS3(LT);
  53. afterS3(KX); afterS3(S4); afterS4(LT);
  54. afterS4(KX); afterS4(S5); afterS5(LT);
  55. afterS5(KX); afterS5(S6); afterS6(LT);
  56. afterS6(KX); afterS6(S7);
  57. if (i == 4)
  58. break;
  59. ++i;
  60. c = b;
  61. b = e;
  62. e = d;
  63. d = a;
  64. a = e;
  65. k += 32;
  66. beforeS0(LT);
  67. }
  68. while (true);
  69. afterS7(KX);
  70. Block::Put(xorBlock, outBlock)(d)(e)(b)(a);
  71. }
  72. void Serpent::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  73. {
  74. word32 a, b, c, d, e;
  75. Block::Get(inBlock)(a)(b)(c)(d);
  76. const word32 *k = m_key + 96;
  77. unsigned int i=4;
  78. beforeI7(KX);
  79. goto start;
  80. do
  81. {
  82. c = b;
  83. b = d;
  84. d = e;
  85. k -= 32;
  86. beforeI7(ILT);
  87. start:
  88. beforeI7(I7); afterI7(KX);
  89. afterI7(ILT); afterI7(I6); afterI6(KX);
  90. afterI6(ILT); afterI6(I5); afterI5(KX);
  91. afterI5(ILT); afterI5(I4); afterI4(KX);
  92. afterI4(ILT); afterI4(I3); afterI3(KX);
  93. afterI3(ILT); afterI3(I2); afterI2(KX);
  94. afterI2(ILT); afterI2(I1); afterI1(KX);
  95. afterI1(ILT); afterI1(I0); afterI0(KX);
  96. }
  97. while (--i != 0);
  98. Block::Put(xorBlock, outBlock)(a)(d)(b)(e);
  99. }
  100. NAMESPACE_END