| 1 | // authenc.h - written and placed in the public domain by Wei Dai |
|---|
| 2 | |
|---|
| 3 | //! \file |
|---|
| 4 | //! \headerfile authenc.h |
|---|
| 5 | //! \brief Base classes for working with authenticated encryption modes of encryption |
|---|
| 6 | //! \since Crypto++ 5.6.0 |
|---|
| 7 | |
|---|
| 8 | #ifndef CRYPTOPP_AUTHENC_H |
|---|
| 9 | #define CRYPTOPP_AUTHENC_H |
|---|
| 10 | |
|---|
| 11 | #include "cryptlib.h" |
|---|
| 12 | #include "secblock.h" |
|---|
| 13 | |
|---|
| 14 | NAMESPACE_BEGIN(CryptoPP) |
|---|
| 15 | |
|---|
| 16 | //! \class AuthenticatedSymmetricCipherBase |
|---|
| 17 | //! \brief Base implementation for one direction (encryption or decryption) of a stream cipher or block cipher mode with authentication |
|---|
| 18 | //! \since Crypto++ 5.6.0 |
|---|
| 19 | class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE AuthenticatedSymmetricCipherBase : public AuthenticatedSymmetricCipher |
|---|
| 20 | { |
|---|
| 21 | public: |
|---|
| 22 | AuthenticatedSymmetricCipherBase() : m_state(State_Start), m_bufferedDataLength(0), |
|---|
| 23 | m_totalHeaderLength(0), m_totalMessageLength(0), m_totalFooterLength(0) {} |
|---|
| 24 | |
|---|
| 25 | bool IsRandomAccess() const {return false;} |
|---|
| 26 | bool IsSelfInverting() const {return true;} |
|---|
| 27 | |
|---|
| 28 | //! \brief Sets the key for this object without performing parameter validation |
|---|
| 29 | //! \param key a byte buffer used to key the cipher |
|---|
| 30 | //! \param length the length of the byte buffer |
|---|
| 31 | //! \param params additional parameters passed as NameValuePairs |
|---|
| 32 | //! \details key must be at least DEFAULT_KEYLENGTH in length. |
|---|
| 33 | void UncheckedSetKey(const byte * key, unsigned int length,const CryptoPP::NameValuePairs ¶ms) |
|---|
| 34 | {CRYPTOPP_UNUSED(key), CRYPTOPP_UNUSED(length), CRYPTOPP_UNUSED(params); CRYPTOPP_ASSERT(false);} |
|---|
| 35 | |
|---|
| 36 | void SetKey(const byte *userKey, size_t keylength, const NameValuePairs ¶ms); |
|---|
| 37 | void Restart() {if (m_state > State_KeySet) m_state = State_KeySet;} |
|---|
| 38 | void Resynchronize(const byte *iv, int length=-1); |
|---|
| 39 | void Update(const byte *input, size_t length); |
|---|
| 40 | void ProcessData(byte *outString, const byte *inString, size_t length); |
|---|
| 41 | void TruncatedFinal(byte *mac, size_t macSize); |
|---|
| 42 | |
|---|
| 43 | protected: |
|---|
| 44 | void AuthenticateData(const byte *data, size_t len); |
|---|
| 45 | const SymmetricCipher & GetSymmetricCipher() const {return const_cast<AuthenticatedSymmetricCipherBase *>(this)->AccessSymmetricCipher();}; |
|---|
| 46 | |
|---|
| 47 | virtual SymmetricCipher & AccessSymmetricCipher() =0; |
|---|
| 48 | virtual bool AuthenticationIsOnPlaintext() const =0; |
|---|
| 49 | virtual unsigned int AuthenticationBlockSize() const =0; |
|---|
| 50 | virtual void SetKeyWithoutResync(const byte *userKey, size_t keylength, const NameValuePairs ¶ms) =0; |
|---|
| 51 | virtual void Resync(const byte *iv, size_t len) =0; |
|---|
| 52 | virtual size_t AuthenticateBlocks(const byte *data, size_t len) =0; |
|---|
| 53 | virtual void AuthenticateLastHeaderBlock() =0; |
|---|
| 54 | virtual void AuthenticateLastConfidentialBlock() {} |
|---|
| 55 | virtual void AuthenticateLastFooterBlock(byte *mac, size_t macSize) =0; |
|---|
| 56 | |
|---|
| 57 | enum State {State_Start, State_KeySet, State_IVSet, State_AuthUntransformed, State_AuthTransformed, State_AuthFooter}; |
|---|
| 58 | State m_state; |
|---|
| 59 | unsigned int m_bufferedDataLength; |
|---|
| 60 | lword m_totalHeaderLength, m_totalMessageLength, m_totalFooterLength; |
|---|
| 61 | AlignedSecByteBlock m_buffer; |
|---|
| 62 | }; |
|---|
| 63 | |
|---|
| 64 | NAMESPACE_END |
|---|
| 65 | |
|---|
| 66 | #endif |
|---|