eax.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // eax.h - originally written and placed in the public domain by Wei Dai
  2. /// \file eax.h
  3. /// \brief EAX block cipher mode of operation
  4. #ifndef CRYPTOPP_EAX_H
  5. #define CRYPTOPP_EAX_H
  6. #include "authenc.h"
  7. #include "modes.h"
  8. #include "cmac.h"
  9. NAMESPACE_BEGIN(CryptoPP)
  10. /// \brief EAX block cipher base implementation
  11. /// \details Base implementation of the AuthenticatedSymmetricCipher interface
  12. /// \since Crypto++ 5.6.0
  13. class CRYPTOPP_NO_VTABLE EAX_Base : public AuthenticatedSymmetricCipherBase
  14. {
  15. public:
  16. // AuthenticatedSymmetricCipher
  17. std::string AlgorithmName() const
  18. {return GetMAC().GetCipher().AlgorithmName() + std::string("/EAX");}
  19. std::string AlgorithmProvider() const
  20. {return GetMAC().GetCipher().AlgorithmProvider();}
  21. size_t MinKeyLength() const
  22. {return GetMAC().MinKeyLength();}
  23. size_t MaxKeyLength() const
  24. {return GetMAC().MaxKeyLength();}
  25. size_t DefaultKeyLength() const
  26. {return GetMAC().DefaultKeyLength();}
  27. size_t GetValidKeyLength(size_t n) const
  28. {return GetMAC().GetValidKeyLength(n);}
  29. bool IsValidKeyLength(size_t n) const
  30. {return GetMAC().IsValidKeyLength(n);}
  31. unsigned int OptimalDataAlignment() const
  32. {return GetMAC().OptimalDataAlignment();}
  33. IV_Requirement IVRequirement() const
  34. {return UNIQUE_IV;}
  35. unsigned int IVSize() const
  36. {return GetMAC().TagSize();}
  37. unsigned int MinIVLength() const
  38. {return 0;}
  39. unsigned int MaxIVLength() const
  40. {return UINT_MAX;}
  41. unsigned int DigestSize() const
  42. {return GetMAC().TagSize();}
  43. lword MaxHeaderLength() const
  44. {return LWORD_MAX;}
  45. lword MaxMessageLength() const
  46. {return LWORD_MAX;}
  47. protected:
  48. // AuthenticatedSymmetricCipherBase
  49. bool AuthenticationIsOnPlaintext() const
  50. {return false;}
  51. unsigned int AuthenticationBlockSize() const
  52. {return 1;}
  53. void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs &params);
  54. void Resync(const byte *iv, size_t len);
  55. size_t AuthenticateBlocks(const byte *data, size_t len);
  56. void AuthenticateLastHeaderBlock();
  57. void AuthenticateLastFooterBlock(byte *mac, size_t macSize);
  58. SymmetricCipher & AccessSymmetricCipher() {return m_ctr;}
  59. const CMAC_Base & GetMAC() const {return const_cast<EAX_Base *>(this)->AccessMAC();}
  60. virtual CMAC_Base & AccessMAC() =0;
  61. CTR_Mode_ExternalCipher::Encryption m_ctr;
  62. };
  63. /// \brief EAX block cipher final implementation
  64. /// \tparam T_BlockCipher block cipher
  65. /// \tparam T_IsEncryption direction in which to operate the cipher
  66. /// \since Crypto++ 5.6.0
  67. template <class T_BlockCipher, bool T_IsEncryption>
  68. class EAX_Final : public EAX_Base
  69. {
  70. public:
  71. static std::string StaticAlgorithmName()
  72. {return T_BlockCipher::StaticAlgorithmName() + std::string("/EAX");}
  73. std::string AlgorithmProvider() const
  74. {return m_cmac.AlgorithmProvider();}
  75. bool IsForwardTransformation() const
  76. {return T_IsEncryption;}
  77. private:
  78. CMAC_Base & AccessMAC() {return m_cmac;}
  79. CMAC<T_BlockCipher> m_cmac;
  80. };
  81. #ifdef EAX // EAX is defined to 11 on GCC 3.4.3, OpenSolaris 8.11
  82. #undef EAX
  83. #endif
  84. /// \brief EAX block cipher mode of operation
  85. /// \tparam T_BlockCipher block cipher
  86. /// \details \p EAX provides the \p Encryption and \p Decryption typedef. See EAX_Base
  87. /// and EAX_Final for the AuthenticatedSymmetricCipher implementation.
  88. /// \sa <a href="http://www.cryptopp.com/wiki/EAX_Mode">EAX Mode</a> and
  89. /// <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
  90. /// on the Crypto++ wiki.
  91. /// \since Crypto++ 5.6.0
  92. template <class T_BlockCipher>
  93. struct EAX : public AuthenticatedSymmetricCipherDocumentation
  94. {
  95. typedef EAX_Final<T_BlockCipher, true> Encryption;
  96. typedef EAX_Final<T_BlockCipher, false> Decryption;
  97. };
  98. NAMESPACE_END
  99. #endif