rc2.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // rc2.h - originally written and placed in the public domain by Wei Dai
  2. /// \file rc2.h
  3. /// \brief Classes for the RC2 block cipher
  4. /// \since Crypto++ 3.0
  5. #ifndef CRYPTOPP_RC2_H
  6. #define CRYPTOPP_RC2_H
  7. #include "seckey.h"
  8. #include "secblock.h"
  9. #include "algparam.h"
  10. NAMESPACE_BEGIN(CryptoPP)
  11. /// \brief RC2 block cipher information
  12. /// \since Crypto++ 3.0
  13. struct RC2_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 1, 128>
  14. {
  15. CRYPTOPP_CONSTANT(DEFAULT_EFFECTIVE_KEYLENGTH = 1024);
  16. CRYPTOPP_CONSTANT(MAX_EFFECTIVE_KEYLENGTH = 1024);
  17. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "RC2";}
  18. };
  19. /// \brief RC2 block cipher
  20. /// \sa <a href="http://www.cryptopp.com/wiki/RC2">RC2</a> on the Crypto Lounge.
  21. /// \since Crypto++ 3.0
  22. class RC2 : public RC2_Info, public BlockCipherDocumentation
  23. {
  24. /// \brief Class specific methods used to operate the cipher.
  25. /// \details Implementations and overrides in \p Base apply to both \p ENCRYPTION and \p DECRYPTION directions
  26. class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<RC2_Info>
  27. {
  28. public:
  29. void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
  30. unsigned int OptimalDataAlignment() const {return GetAlignmentOf<word16>();}
  31. protected:
  32. FixedSizeSecBlock<word16, 64> K; // expanded key table
  33. };
  34. /// \brief Class specific methods used to operate the cipher in the forward direction.
  35. /// \details Implementations and overrides in \p Enc apply to \p ENCRYPTION.
  36. class CRYPTOPP_NO_VTABLE Enc : public Base
  37. {
  38. public:
  39. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  40. };
  41. /// \brief Class specific methods used to operate the cipher in the reverse direction.
  42. /// \details Implementations and overrides in \p Dec apply to \p DECRYPTION.
  43. class CRYPTOPP_NO_VTABLE Dec : public Base
  44. {
  45. public:
  46. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  47. };
  48. public:
  49. /// \brief Class specific methods used to operate the cipher in the forward direction.
  50. /// \details Implementations and overrides in \p Encryption apply to \p ENCRYPTION.
  51. class Encryption : public BlockCipherFinal<ENCRYPTION, Enc>
  52. {
  53. public:
  54. Encryption() {}
  55. Encryption(const byte *key, size_t keyLen=DEFAULT_KEYLENGTH)
  56. {SetKey(key, keyLen);}
  57. Encryption(const byte *key, size_t keyLen, int effectiveKeyLen)
  58. {SetKey(key, keyLen, MakeParameters("EffectiveKeyLength", effectiveKeyLen));}
  59. };
  60. /// \brief Class specific methods used to operate the cipher in the reverse direction.
  61. /// \details Implementations and overrides in \p Decryption apply to \p DECRYPTION.
  62. class Decryption : public BlockCipherFinal<DECRYPTION, Dec>
  63. {
  64. public:
  65. Decryption() {}
  66. Decryption(const byte *key, size_t keyLen=DEFAULT_KEYLENGTH)
  67. {SetKey(key, keyLen);}
  68. Decryption(const byte *key, size_t keyLen, int effectiveKeyLen)
  69. {SetKey(key, keyLen, MakeParameters("EffectiveKeyLength", effectiveKeyLen));}
  70. };
  71. };
  72. typedef RC2::Encryption RC2Encryption;
  73. typedef RC2::Decryption RC2Decryption;
  74. NAMESPACE_END
  75. #endif