arc4.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // arc4.h - originally written and placed in the public domain by Wei Dai
  2. /// \file arc4.h
  3. /// \brief Classes for ARC4 cipher
  4. /// \since Crypto++ 3.1
  5. #ifndef CRYPTOPP_ARC4_H
  6. #define CRYPTOPP_ARC4_H
  7. #include "cryptlib.h"
  8. #include "strciphr.h"
  9. #include "secblock.h"
  10. #include "smartptr.h"
  11. NAMESPACE_BEGIN(CryptoPP)
  12. namespace Weak1 {
  13. /// \brief ARC4 base class
  14. /// \details Implementations and overrides in \p Base apply to both \p ENCRYPTION and \p DECRYPTION directions
  15. /// \since Crypto++ 3.1
  16. class CRYPTOPP_NO_VTABLE ARC4_Base : public VariableKeyLength<16, 1, 256>, public RandomNumberGenerator, public SymmetricCipher, public SymmetricCipherDocumentation
  17. {
  18. public:
  19. ~ARC4_Base();
  20. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "ARC4";}
  21. void GenerateBlock(byte *output, size_t size);
  22. void DiscardBytes(size_t n);
  23. void ProcessData(byte *outString, const byte *inString, size_t length);
  24. bool IsRandomAccess() const {return false;}
  25. bool IsSelfInverting() const {return true;}
  26. bool IsForwardTransformation() const {return true;}
  27. typedef SymmetricCipherFinal<ARC4_Base> Encryption;
  28. typedef SymmetricCipherFinal<ARC4_Base> Decryption;
  29. protected:
  30. void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
  31. virtual unsigned int GetDefaultDiscardBytes() const {return 0;}
  32. FixedSizeSecBlock<byte, 256> m_state;
  33. byte m_x, m_y;
  34. };
  35. /// \brief Alleged RC4
  36. /// \sa <a href="http://www.cryptopp.com/wiki/RC4">Alleged RC4</a>
  37. /// \since Crypto++ 3.1
  38. DOCUMENTED_TYPEDEF(SymmetricCipherFinal<ARC4_Base>, ARC4);
  39. /// \brief MARC4 base class
  40. /// \details Implementations and overrides in \p Base apply to both \p ENCRYPTION and \p DECRYPTION directions
  41. /// \details MARC4 discards the first 256 bytes of keystream, which may be weaker than the rest
  42. /// \since Crypto++ 3.1
  43. class CRYPTOPP_NO_VTABLE MARC4_Base : public ARC4_Base
  44. {
  45. public:
  46. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "MARC4";}
  47. typedef SymmetricCipherFinal<MARC4_Base> Encryption;
  48. typedef SymmetricCipherFinal<MARC4_Base> Decryption;
  49. protected:
  50. unsigned int GetDefaultDiscardBytes() const {return 256;}
  51. };
  52. /// \brief Modified Alleged RC4
  53. /// \sa <a href="http://www.cryptopp.com/wiki/RC4">Alleged RC4</a>
  54. /// \since Crypto++ 3.1
  55. DOCUMENTED_TYPEDEF(SymmetricCipherFinal<MARC4_Base>, MARC4);
  56. }
  57. #if CRYPTOPP_ENABLE_NAMESPACE_WEAK >= 1
  58. namespace Weak {using namespace Weak1;} // import Weak1 into CryptoPP::Weak
  59. #else
  60. using namespace Weak1; // import Weak1 into CryptoPP with warning
  61. #ifdef __GNUC__
  62. #warning "You may be using a weak algorithm that has been retained for backwards compatibility. Please '#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1' before including this .h file and prepend the class name with 'Weak::' to remove this warning."
  63. #else
  64. #pragma message("You may be using a weak algorithm that has been retained for backwards compatibility. Please '#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1' before including this .h file and prepend the class name with 'Weak::' to remove this warning.")
  65. #endif
  66. #endif
  67. NAMESPACE_END
  68. #endif