twofish.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // twofish.h - originally written and placed in the public domain by Wei Dai
  2. /// \file twofish.h
  3. /// \brief Classes for the Twofish block cipher
  4. #ifndef CRYPTOPP_TWOFISH_H
  5. #define CRYPTOPP_TWOFISH_H
  6. #include "seckey.h"
  7. #include "secblock.h"
  8. NAMESPACE_BEGIN(CryptoPP)
  9. /// \brief Twofish block cipher information
  10. /// \since Crypto++ 3.1
  11. struct Twofish_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16, 32, 8>, FixedRounds<16>
  12. {
  13. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "Twofish";}
  14. };
  15. /// \brief Twofish block cipher
  16. /// \sa <a href="http://www.cryptopp.com/wiki/Twofish">Twofish</a>
  17. /// \since Crypto++ 3.1
  18. class Twofish : public Twofish_Info, public BlockCipherDocumentation
  19. {
  20. class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<Twofish_Info>
  21. {
  22. public:
  23. void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
  24. protected:
  25. static word32 h0(word32 x, const word32 *key, unsigned int kLen);
  26. static word32 h(word32 x, const word32 *key, unsigned int kLen);
  27. static const byte q[2][256];
  28. static const word32 mds[4][256];
  29. FixedSizeSecBlock<word32, 40> m_k;
  30. FixedSizeSecBlock<word32, 4*256> m_s;
  31. };
  32. class CRYPTOPP_NO_VTABLE Enc : public Base
  33. {
  34. public:
  35. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  36. };
  37. class CRYPTOPP_NO_VTABLE Dec : public Base
  38. {
  39. public:
  40. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  41. };
  42. public:
  43. typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
  44. typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
  45. };
  46. typedef Twofish::Encryption TwofishEncryption;
  47. typedef Twofish::Decryption TwofishDecryption;
  48. NAMESPACE_END
  49. #endif