Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:54

0001 // authenc.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file

0004 /// \brief Classes for authenticated encryption modes of operation

0005 /// \details Authenticated encryption (AE) schemes combine confidentiality and authenticity

0006 ///   into a single mode of operation They gained traction in the early 2000's because manually

0007 ///   combining them was error prone for the typical developer. Around that time, the desire to

0008 ///   authenticate but not ecrypt additional data (AAD) was also identified. When both features

0009 ///   are available from a scheme, the system is referred to as an AEAD scheme.

0010 /// \details Crypto++ provides four authenticated encryption modes of operation - CCM, EAX, GCM

0011 ///   and OCB mode. All modes derive from AuthenticatedSymmetricCipherBase() and the

0012 ///   motivation for the API, like calling AAD a "header", can be found in Bellare,

0013 ///   Rogaway and Wagner's <A HREF="http://web.cs.ucdavis.edu/~rogaway/papers/eax.pdf">The EAX

0014 ///   Mode of Operation</A>. The EAX paper suggested a basic API to help standardize AEAD

0015 ///   schemes in software and promote adoption of the modes.

0016 /// \sa <A HREF="http://www.cryptopp.com/wiki/Authenticated_Encryption">Authenticated

0017 ///   Encryption</A> on the Crypto++ wiki.

0018 /// \since Crypto++ 5.6.0

0019 
0020 #ifndef CRYPTOPP_AUTHENC_H
0021 #define CRYPTOPP_AUTHENC_H
0022 
0023 #include "cryptlib.h"
0024 #include "secblock.h"
0025 
0026 NAMESPACE_BEGIN(CryptoPP)
0027 
0028 /// \brief Base class for authenticated encryption modes of operation

0029 /// \details AuthenticatedSymmetricCipherBase() serves as a base implementation for one direction

0030 ///   (encryption or decryption) of a stream cipher or block cipher mode with authentication.

0031 /// \details Crypto++ provides four authenticated encryption modes of operation - CCM, EAX, GCM

0032 ///   and OCB mode. All modes derive from AuthenticatedSymmetricCipherBase() and the

0033 ///   motivation for the API, like calling AAD a &quot;header&quot;, can be found in Bellare,

0034 ///   Rogaway and Wagner's <A HREF="http://web.cs.ucdavis.edu/~rogaway/papers/eax.pdf">The EAX

0035 ///   Mode of Operation</A>. The EAX paper suggested a basic API to help standardize AEAD

0036 ///   schemes in software and promote adoption of the modes.

0037 /// \sa <A HREF="http://www.cryptopp.com/wiki/Authenticated_Encryption">Authenticated

0038 ///   Encryption</A> on the Crypto++ wiki.

0039 /// \since Crypto++ 5.6.0

0040 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE AuthenticatedSymmetricCipherBase : public AuthenticatedSymmetricCipher
0041 {
0042 public:
0043     AuthenticatedSymmetricCipherBase() : m_totalHeaderLength(0), m_totalMessageLength(0),
0044         m_totalFooterLength(0), m_bufferedDataLength(0), m_state(State_Start) {}
0045 
0046     // StreamTransformation interface

0047     bool IsRandomAccess() const {return false;}
0048     bool IsSelfInverting() const {return true;}
0049 
0050     void SetKey(const byte *userKey, size_t keylength, const NameValuePairs &params);
0051     void Restart() {if (m_state > State_KeySet) m_state = State_KeySet;}
0052     void Resynchronize(const byte *iv, int length=-1);
0053     void Update(const byte *input, size_t length);
0054     void ProcessData(byte *outString, const byte *inString, size_t length);
0055     void TruncatedFinal(byte *mac, size_t macSize);
0056 
0057 protected:
0058     void UncheckedSetKey(const byte * key, unsigned int length,const CryptoPP::NameValuePairs &params)
0059         {CRYPTOPP_UNUSED(key), CRYPTOPP_UNUSED(length), CRYPTOPP_UNUSED(params); CRYPTOPP_ASSERT(false);}
0060 
0061     void AuthenticateData(const byte *data, size_t len);
0062     const SymmetricCipher & GetSymmetricCipher() const
0063         {return const_cast<AuthenticatedSymmetricCipherBase *>(this)->AccessSymmetricCipher();}
0064 
0065     virtual SymmetricCipher & AccessSymmetricCipher() =0;
0066     virtual bool AuthenticationIsOnPlaintext() const =0;
0067     virtual unsigned int AuthenticationBlockSize() const =0;
0068     virtual void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs &params) =0;
0069     virtual void Resync(const byte *iv, size_t len) =0;
0070     virtual size_t AuthenticateBlocks(const byte *data, size_t len) =0;
0071     virtual void AuthenticateLastHeaderBlock() =0;
0072     virtual void AuthenticateLastConfidentialBlock() {}
0073     virtual void AuthenticateLastFooterBlock(byte *mac, size_t macSize) =0;
0074 
0075     // State_AuthUntransformed: authentication is applied to plain text (Authenticate-then-Encrypt)

0076     // State_AuthTransformed: authentication is applied to cipher text (Encrypt-then-Authenticate)

0077     enum State {State_Start, State_KeySet, State_IVSet, State_AuthUntransformed, State_AuthTransformed, State_AuthFooter};
0078 
0079     AlignedSecByteBlock m_buffer;
0080     lword m_totalHeaderLength, m_totalMessageLength, m_totalFooterLength;
0081     unsigned int m_bufferedDataLength;
0082     State m_state;
0083 };
0084 
0085 NAMESPACE_END
0086 
0087 #endif