Team Fortress 2 Source Code as on 22/4/2020
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.

95 lines
3.1 KiB

  1. // rc2.h - written and placed in the public domain by Wei Dai
  2. //! \file rc2.h
  3. //! \brief Classes for the RC2 block cipher
  4. #ifndef CRYPTOPP_RC2_H
  5. #define CRYPTOPP_RC2_H
  6. #include "seckey.h"
  7. #include "secblock.h"
  8. #include "algparam.h"
  9. NAMESPACE_BEGIN(CryptoPP)
  10. //! \class RC2_Info
  11. //! \brief The RC2 cipher's key, iv, block size and name information.
  12. struct RC2_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 1, 128>
  13. {
  14. CRYPTOPP_CONSTANT(DEFAULT_EFFECTIVE_KEYLENGTH = 1024)
  15. CRYPTOPP_CONSTANT(MAX_EFFECTIVE_KEYLENGTH = 1024)
  16. static const char *StaticAlgorithmName() {return "RC2";}
  17. };
  18. //! \class RC2
  19. //! \brief The RC2 stream cipher
  20. //! \sa <a href="http://www.weidai.com/scan-mirror/cs.html#RC2">RC2</a> on the Crypto Lounge.
  21. class RC2 : public RC2_Info, public BlockCipherDocumentation
  22. {
  23. //! \class Base
  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. //! \class Enc
  35. //! \brief Class specific methods used to operate the cipher in the forward direction.
  36. //! \details Implementations and overrides in \p Enc apply to \p ENCRYPTION.
  37. class CRYPTOPP_NO_VTABLE Enc : public Base
  38. {
  39. public:
  40. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  41. };
  42. //! \class Dec
  43. //! \brief Class specific methods used to operate the cipher in the reverse direction.
  44. //! \details Implementations and overrides in \p Dec apply to \p DECRYPTION.
  45. class CRYPTOPP_NO_VTABLE Dec : public Base
  46. {
  47. public:
  48. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  49. };
  50. public:
  51. //! \class Encryption
  52. //! \brief Class specific methods used to operate the cipher in the forward direction.
  53. //! \details Implementations and overrides in \p Encryption apply to \p ENCRYPTION.
  54. class Encryption : public BlockCipherFinal<ENCRYPTION, Enc>
  55. {
  56. public:
  57. Encryption() {}
  58. Encryption(const byte *key, size_t keyLen=DEFAULT_KEYLENGTH)
  59. {SetKey(key, keyLen);}
  60. Encryption(const byte *key, size_t keyLen, int effectiveKeyLen)
  61. {SetKey(key, keyLen, MakeParameters("EffectiveKeyLength", effectiveKeyLen));}
  62. };
  63. //! \class Decryption
  64. //! \brief Class specific methods used to operate the cipher in the reverse direction.
  65. //! \details Implementations and overrides in \p Decryption apply to \p DECRYPTION.
  66. class Decryption : public BlockCipherFinal<DECRYPTION, Dec>
  67. {
  68. public:
  69. Decryption() {}
  70. Decryption(const byte *key, size_t keyLen=DEFAULT_KEYLENGTH)
  71. {SetKey(key, keyLen);}
  72. Decryption(const byte *key, size_t keyLen, int effectiveKeyLen)
  73. {SetKey(key, keyLen, MakeParameters("EffectiveKeyLength", effectiveKeyLen));}
  74. };
  75. };
  76. typedef RC2::Encryption RC2Encryption;
  77. typedef RC2::Decryption RC2Decryption;
  78. NAMESPACE_END
  79. #endif