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.

154 lines
3.5 KiB

  1. // mars.cpp - written and placed in the public domain by Wei Dai
  2. // includes IBM's key setup "tweak" proposed in August 1999 (http://www.research.ibm.com/security/key-setup.txt)
  3. #include "pch.h"
  4. #include "mars.h"
  5. #include "misc.h"
  6. NAMESPACE_BEGIN(CryptoPP)
  7. void MARS::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)
  8. {
  9. AssertValidKeyLength(length);
  10. // Initialize T[] with the key data
  11. FixedSizeSecBlock<word32, 15> T;
  12. GetUserKey(LITTLE_ENDIAN_ORDER, T.begin(), 15, userKey, length);
  13. T[length/4] = length/4;
  14. for (unsigned int j=0; j<4; j++) // compute 10 words of K[] in each iteration
  15. {
  16. unsigned int i;
  17. // Do linear transformation
  18. for (i=0; i<15; i++)
  19. T[i] = T[i] ^ rotlFixed(T[(i+8)%15] ^ T[(i+13)%15], 3) ^ (4*i+j);
  20. // Do four rounds of stirring
  21. for (unsigned int k=0; k<4; k++)
  22. for (i=0; i<15; i++)
  23. T[i] = rotlFixed(T[i] + Sbox[T[(i+14)%15]%512], 9);
  24. // Store next 10 key words into K[]
  25. for (i=0; i<10; i++)
  26. m_k[10*j+i] = T[4*i%15];
  27. }
  28. // Modify multiplication key-words
  29. for(unsigned int i = 5; i < 37; i += 2)
  30. {
  31. word32 m, w = m_k[i] | 3;
  32. m = (~w ^ (w<<1)) & (~w ^ (w>>1)) & 0x7ffffffe;
  33. m &= m>>1; m &= m>>2; m &= m>>4;
  34. m |= m<<1; m |= m<<2; m |= m<<4;
  35. m &= 0x7ffffffc;
  36. w ^= rotlMod(Sbox[265 + (m_k[i] & 3)], m_k[i-1]) & m;
  37. m_k[i] = w;
  38. }
  39. }
  40. #define S(a) Sbox[(a)&0x1ff]
  41. #define S0(a) Sbox[(a)&0xff]
  42. #define S1(a) Sbox[((a)&0xff) + 256]
  43. typedef BlockGetAndPut<word32, LittleEndian> Block;
  44. void MARS::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  45. {
  46. unsigned int i;
  47. word32 a, b, c, d, l, m, r, t;
  48. const word32 *k = m_k;
  49. Block::Get(inBlock)(a)(b)(c)(d);
  50. a += k[0]; b += k[1]; c += k[2]; d += k[3];
  51. for (i=0; i<8; i++)
  52. {
  53. b = (b ^ S0(a)) + S1(a>>8);
  54. c += S0(a>>16);
  55. a = rotrFixed(a, 24);
  56. d ^= S1(a);
  57. a += (i%4==0) ? d : 0;
  58. a += (i%4==1) ? b : 0;
  59. t = a; a = b; b = c; c = d; d = t;
  60. }
  61. for (i=0; i<16; i++)
  62. {
  63. t = rotlFixed(a, 13);
  64. r = rotlFixed(t * k[2*i+5], 10);
  65. m = a + k[2*i+4];
  66. l = rotlMod((S(m) ^ rotrFixed(r, 5) ^ r), r);
  67. c += rotlMod(m, rotrFixed(r, 5));
  68. (i<8 ? b : d) += l;
  69. (i<8 ? d : b) ^= r;
  70. a = b; b = c; c = d; d = t;
  71. }
  72. for (i=0; i<8; i++)
  73. {
  74. a -= (i%4==2) ? d : 0;
  75. a -= (i%4==3) ? b : 0;
  76. b ^= S1(a);
  77. c -= S0(a>>24);
  78. t = rotlFixed(a, 24);
  79. d = (d - S1(a>>16)) ^ S0(t);
  80. a = b; b = c; c = d; d = t;
  81. }
  82. a -= k[36]; b -= k[37]; c -= k[38]; d -= k[39];
  83. Block::Put(xorBlock, outBlock)(a)(b)(c)(d);
  84. }
  85. void MARS::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  86. {
  87. unsigned int i;
  88. word32 a, b, c, d, l, m, r, t;
  89. const word32 *k = m_k;
  90. Block::Get(inBlock)(d)(c)(b)(a);
  91. d += k[36]; c += k[37]; b += k[38]; a += k[39];
  92. for (i=0; i<8; i++)
  93. {
  94. b = (b ^ S0(a)) + S1(a>>8);
  95. c += S0(a>>16);
  96. a = rotrFixed(a, 24);
  97. d ^= S1(a);
  98. a += (i%4==0) ? d : 0;
  99. a += (i%4==1) ? b : 0;
  100. t = a; a = b; b = c; c = d; d = t;
  101. }
  102. for (i=0; i<16; i++)
  103. {
  104. t = rotrFixed(a, 13);
  105. r = rotlFixed(a * k[35-2*i], 10);
  106. m = t + k[34-2*i];
  107. l = rotlMod((S(m) ^ rotrFixed(r, 5) ^ r), r);
  108. c -= rotlMod(m, rotrFixed(r, 5));
  109. (i<8 ? b : d) -= l;
  110. (i<8 ? d : b) ^= r;
  111. a = b; b = c; c = d; d = t;
  112. }
  113. for (i=0; i<8; i++)
  114. {
  115. a -= (i%4==2) ? d : 0;
  116. a -= (i%4==3) ? b : 0;
  117. b ^= S1(a);
  118. c -= S0(a>>24);
  119. t = rotlFixed(a, 24);
  120. d = (d - S1(a>>16)) ^ S0(t);
  121. a = b; b = c; c = d; d = t;
  122. }
  123. d -= k[0]; c -= k[1]; b -= k[2]; a -= k[3];
  124. Block::Put(xorBlock, outBlock)(d)(c)(b)(a);
  125. }
  126. NAMESPACE_END