authenc.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // authenc.h - originally written and placed in the public domain by Wei Dai
  2. /// \file
  3. /// \brief Classes for authenticated encryption modes of operation
  4. /// \details Authenticated encryption (AE) schemes combine confidentiality and authenticity
  5. /// into a single mode of operation They gained traction in the early 2000's because manually
  6. /// combining them was error prone for the typical developer. Around that time, the desire to
  7. /// authenticate but not ecrypt additional data (AAD) was also identified. When both features
  8. /// are available from a scheme, the system is referred to as an AEAD scheme.
  9. /// \details Crypto++ provides four authenticated encryption modes of operation - CCM, EAX, GCM
  10. /// and OCB mode. All modes derive from AuthenticatedSymmetricCipherBase() and the
  11. /// motivation for the API, like calling AAD a "header", can be found in Bellare,
  12. /// Rogaway and Wagner's <A HREF="http://web.cs.ucdavis.edu/~rogaway/papers/eax.pdf">The EAX
  13. /// Mode of Operation</A>. The EAX paper suggested a basic API to help standardize AEAD
  14. /// schemes in software and promote adoption of the modes.
  15. /// \sa <A HREF="http://www.cryptopp.com/wiki/Authenticated_Encryption">Authenticated
  16. /// Encryption</A> on the Crypto++ wiki.
  17. /// \since Crypto++ 5.6.0
  18. #ifndef CRYPTOPP_AUTHENC_H
  19. #define CRYPTOPP_AUTHENC_H
  20. #include "cryptlib.h"
  21. #include "secblock.h"
  22. NAMESPACE_BEGIN(CryptoPP)
  23. /// \brief Base class for authenticated encryption modes of operation
  24. /// \details AuthenticatedSymmetricCipherBase() serves as a base implementation for one direction
  25. /// (encryption or decryption) of a stream cipher or block cipher mode with authentication.
  26. /// \details Crypto++ provides four authenticated encryption modes of operation - CCM, EAX, GCM
  27. /// and OCB mode. All modes derive from AuthenticatedSymmetricCipherBase() and the
  28. /// motivation for the API, like calling AAD a &quot;header&quot;, can be found in Bellare,
  29. /// Rogaway and Wagner's <A HREF="http://web.cs.ucdavis.edu/~rogaway/papers/eax.pdf">The EAX
  30. /// Mode of Operation</A>. The EAX paper suggested a basic API to help standardize AEAD
  31. /// schemes in software and promote adoption of the modes.
  32. /// \sa <A HREF="http://www.cryptopp.com/wiki/Authenticated_Encryption">Authenticated
  33. /// Encryption</A> on the Crypto++ wiki.
  34. /// \since Crypto++ 5.6.0
  35. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE AuthenticatedSymmetricCipherBase : public AuthenticatedSymmetricCipher
  36. {
  37. public:
  38. AuthenticatedSymmetricCipherBase() : m_totalHeaderLength(0), m_totalMessageLength(0),
  39. m_totalFooterLength(0), m_bufferedDataLength(0), m_state(State_Start) {}
  40. // StreamTransformation interface
  41. bool IsRandomAccess() const {return false;}
  42. bool IsSelfInverting() const {return true;}
  43. void SetKey(const byte *userKey, size_t keylength, const NameValuePairs &params);
  44. void Restart() {if (m_state > State_KeySet) m_state = State_KeySet;}
  45. void Resynchronize(const byte *iv, int length=-1);
  46. void Update(const byte *input, size_t length);
  47. void ProcessData(byte *outString, const byte *inString, size_t length);
  48. void TruncatedFinal(byte *mac, size_t macSize);
  49. protected:
  50. void UncheckedSetKey(const byte * key, unsigned int length,const CryptoPP::NameValuePairs &params)
  51. {CRYPTOPP_UNUSED(key), CRYPTOPP_UNUSED(length), CRYPTOPP_UNUSED(params); CRYPTOPP_ASSERT(false);}
  52. void AuthenticateData(const byte *data, size_t len);
  53. const SymmetricCipher & GetSymmetricCipher() const
  54. {return const_cast<AuthenticatedSymmetricCipherBase *>(this)->AccessSymmetricCipher();}
  55. virtual SymmetricCipher & AccessSymmetricCipher() =0;
  56. virtual bool AuthenticationIsOnPlaintext() const =0;
  57. virtual unsigned int AuthenticationBlockSize() const =0;
  58. virtual void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs &params) =0;
  59. virtual void Resync(const byte *iv, size_t len) =0;
  60. virtual size_t AuthenticateBlocks(const byte *data, size_t len) =0;
  61. virtual void AuthenticateLastHeaderBlock() =0;
  62. virtual void AuthenticateLastConfidentialBlock() {}
  63. virtual void AuthenticateLastFooterBlock(byte *mac, size_t macSize) =0;
  64. // State_AuthUntransformed: authentication is applied to plain text (Authenticate-then-Encrypt)
  65. // State_AuthTransformed: authentication is applied to cipher text (Encrypt-then-Authenticate)
  66. enum State {State_Start, State_KeySet, State_IVSet, State_AuthUntransformed, State_AuthTransformed, State_AuthFooter};
  67. AlignedSecByteBlock m_buffer;
  68. lword m_totalHeaderLength, m_totalMessageLength, m_totalFooterLength;
  69. unsigned int m_bufferedDataLength;
  70. State m_state;
  71. };
  72. NAMESPACE_END
  73. #endif