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.

79 lines
1.9 KiB

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