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.

141 lines
3.2 KiB

  1. // lubyrack.h - written and placed in the public domain by Wei Dai
  2. #ifndef CRYPTOPP_LUBYRACK_H
  3. #define CRYPTOPP_LUBYRACK_H
  4. /** \file */
  5. #include "simple.h"
  6. #include "secblock.h"
  7. NAMESPACE_BEGIN(CryptoPP)
  8. template <class T> struct DigestSizeDoubleWorkaround // VC60 workaround
  9. {
  10. CRYPTOPP_CONSTANT(RESULT = 2*T::DIGESTSIZE)
  11. };
  12. //! algorithm info
  13. template <class T>
  14. struct LR_Info : public VariableKeyLength<16, 0, 2*(INT_MAX/2), 2>, public FixedBlockSize<DigestSizeDoubleWorkaround<T>::RESULT>
  15. {
  16. static std::string StaticAlgorithmName() {return std::string("LR/")+T::StaticAlgorithmName();}
  17. };
  18. //! Luby-Rackoff
  19. template <class T>
  20. class LR : public LR_Info<T>, public BlockCipherDocumentation
  21. {
  22. class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<LR_Info<T> >
  23. {
  24. public:
  25. // VC60 workaround: have to define these functions within class definition
  26. void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
  27. {
  28. this->AssertValidKeyLength(length);
  29. L = length/2;
  30. buffer.New(2*S);
  31. digest.New(S);
  32. key.Assign(userKey, 2*L);
  33. }
  34. protected:
  35. CRYPTOPP_CONSTANT(S=T::DIGESTSIZE)
  36. unsigned int L; // key length / 2
  37. SecByteBlock key;
  38. mutable T hm;
  39. mutable SecByteBlock buffer, digest;
  40. };
  41. class CRYPTOPP_NO_VTABLE Enc : public Base
  42. {
  43. public:
  44. #define KL this->key
  45. #define KR this->key+this->L
  46. #define BL this->buffer
  47. #define BR this->buffer+this->S
  48. #define IL inBlock
  49. #define IR inBlock+this->S
  50. #define OL outBlock
  51. #define OR outBlock+this->S
  52. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  53. {
  54. this->hm.Update(KL, this->L);
  55. this->hm.Update(IL, this->S);
  56. this->hm.Final(BR);
  57. xorbuf(BR, IR, this->S);
  58. this->hm.Update(KR, this->L);
  59. this->hm.Update(BR, this->S);
  60. this->hm.Final(BL);
  61. xorbuf(BL, IL, this->S);
  62. this->hm.Update(KL, this->L);
  63. this->hm.Update(BL, this->S);
  64. this->hm.Final(this->digest);
  65. xorbuf(BR, this->digest, this->S);
  66. this->hm.Update(KR, this->L);
  67. this->hm.Update(OR, this->S);
  68. this->hm.Final(this->digest);
  69. xorbuf(BL, this->digest, this->S);
  70. if (xorBlock)
  71. xorbuf(outBlock, xorBlock, this->buffer, 2*this->S);
  72. else
  73. memcpy_s(outBlock, 2*this->S, this->buffer, 2*this->S);
  74. }
  75. };
  76. class CRYPTOPP_NO_VTABLE Dec : public Base
  77. {
  78. public:
  79. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
  80. {
  81. this->hm.Update(KR, this->L);
  82. this->hm.Update(IR, this->S);
  83. this->hm.Final(BL);
  84. xorbuf(BL, IL, this->S);
  85. this->hm.Update(KL, this->L);
  86. this->hm.Update(BL, this->S);
  87. this->hm.Final(BR);
  88. xorbuf(BR, IR, this->S);
  89. this->hm.Update(KR, this->L);
  90. this->hm.Update(BR, this->S);
  91. this->hm.Final(this->digest);
  92. xorbuf(BL, this->digest, this->S);
  93. this->hm.Update(KL, this->L);
  94. this->hm.Update(OL, this->S);
  95. this->hm.Final(this->digest);
  96. xorbuf(BR, this->digest, this->S);
  97. if (xorBlock)
  98. xorbuf(outBlock, xorBlock, this->buffer, 2*this->S);
  99. else
  100. memcpy(outBlock, this->buffer, 2*this->S);
  101. }
  102. #undef KL
  103. #undef KR
  104. #undef BL
  105. #undef BR
  106. #undef IL
  107. #undef IR
  108. #undef OL
  109. #undef OR
  110. };
  111. public:
  112. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  113. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  114. };
  115. NAMESPACE_END
  116. #endif