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.

96 lines
2.2 KiB

  1. // rc6.cpp - written and placed in the public domain by Sean Woods
  2. // based on Wei Dai's RC5 code.
  3. #include "pch.h"
  4. #include "rc6.h"
  5. #include "misc.h"
  6. NAMESPACE_BEGIN(CryptoPP)
  7. void RC6::Base::UncheckedSetKey(const byte *k, unsigned int keylen, const NameValuePairs &params)
  8. {
  9. AssertValidKeyLength(keylen);
  10. r = GetRoundsAndThrowIfInvalid(params, this);
  11. sTable.New(2*(r+2));
  12. static const RC6_WORD MAGIC_P = 0xb7e15163L; // magic constant P for wordsize
  13. static const RC6_WORD MAGIC_Q = 0x9e3779b9L; // magic constant Q for wordsize
  14. static const int U=sizeof(RC6_WORD);
  15. const unsigned int c = STDMAX((keylen+U-1)/U, 1U); // RC6 paper says c=1 if keylen==0
  16. SecBlock<RC6_WORD> l(c);
  17. GetUserKey(LITTLE_ENDIAN_ORDER, l.begin(), c, k, keylen);
  18. sTable[0] = MAGIC_P;
  19. for (unsigned j=1; j<sTable.size();j++)
  20. sTable[j] = sTable[j-1] + MAGIC_Q;
  21. RC6_WORD a=0, b=0;
  22. const unsigned n = 3*STDMAX((unsigned int)sTable.size(), c);
  23. for (unsigned h=0; h < n; h++)
  24. {
  25. a = sTable[h % sTable.size()] = rotlFixed((sTable[h % sTable.size()] + a + b), 3);
  26. b = l[h % c] = rotlMod((l[h % c] + a + b), (a+b));
  27. }
  28. }
  29. typedef BlockGetAndPut<RC6::RC6_WORD, LittleEndian> Block;
  30. void RC6::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  31. {
  32. const RC6_WORD *sptr = sTable;
  33. RC6_WORD a, b, c, d, t, u;
  34. Block::Get(inBlock)(a)(b)(c)(d);
  35. b += sptr[0];
  36. d += sptr[1];
  37. sptr += 2;
  38. for(unsigned i=0; i<r; i++)
  39. {
  40. t = rotlFixed(b*(2*b+1), 5);
  41. u = rotlFixed(d*(2*d+1), 5);
  42. a = rotlMod(a^t,u) + sptr[0];
  43. c = rotlMod(c^u,t) + sptr[1];
  44. t = a; a = b; b = c; c = d; d = t;
  45. sptr += 2;
  46. }
  47. a += sptr[0];
  48. c += sptr[1];
  49. Block::Put(xorBlock, outBlock)(a)(b)(c)(d);
  50. }
  51. void RC6::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  52. {
  53. const RC6_WORD *sptr = sTable.end();
  54. RC6_WORD a, b, c, d, t, u;
  55. Block::Get(inBlock)(a)(b)(c)(d);
  56. sptr -= 2;
  57. c -= sptr[1];
  58. a -= sptr[0];
  59. for (unsigned i=0; i < r; i++)
  60. {
  61. sptr -= 2;
  62. t = a; a = d; d = c; c = b; b = t;
  63. u = rotlFixed(d*(2*d+1), 5);
  64. t = rotlFixed(b*(2*b+1), 5);
  65. c = rotrMod(c-sptr[1], t) ^ u;
  66. a = rotrMod(a-sptr[0], u) ^ t;
  67. }
  68. sptr -= 2;
  69. d -= sTable[1];
  70. b -= sTable[0];
  71. Block::Put(xorBlock, outBlock)(a)(b)(c)(d);
  72. }
  73. NAMESPACE_END