lubyrack.h 3.2 KB

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