5#ifndef CRYPTOPP_IMPORTS
11void AuthenticatedSymmetricCipherBase::AuthenticateData(
const byte *input,
size_t len)
15 if(!input || !len)
return;
17 unsigned int blockSize = AuthenticationBlockSize();
18 unsigned int &num = m_bufferedDataLength;
19 byte* data = m_buffer.
begin();
23 if (num+len >= blockSize)
25 std::memcpy(data+num, input, blockSize-num);
26 AuthenticateBlocks(data, blockSize);
27 input += (blockSize-num);
28 len -= (blockSize-num);
34 std::memcpy(data+num, input, len);
35 num += (
unsigned int)len;
43 size_t leftOver = AuthenticateBlocks(input, len);
44 input += (len - leftOver);
49 std::memcpy(data, input, len);
50 num = (
unsigned int)len;
55 m_bufferedDataLength = 0;
56 m_state = State_Start;
58 this->SetKeyWithoutResync(userKey, keylength, params);
59 m_state = State_KeySet;
62 const byte *iv = GetIVAndThrowIfInvalid(params, length);
69 if (m_state < State_KeySet)
70 throw BadState(
AlgorithmName(),
"Resynchronize",
"key is set");
72 m_bufferedDataLength = 0;
73 m_totalHeaderLength = m_totalMessageLength = m_totalFooterLength = 0;
74 m_state = State_KeySet;
76 Resync(iv, this->ThrowIfInvalidIVLength(length));
77 m_state = State_IVSet;
83 if (length == 0) {
return;}
89 throw BadState(
AlgorithmName(),
"Update",
"setting key and IV");
91 AuthenticateData(input, length);
92 m_totalHeaderLength += length;
94 case State_AuthUntransformed:
95 case State_AuthTransformed:
96 AuthenticateLastConfidentialBlock();
97 m_bufferedDataLength = 0;
98 m_state = State_AuthFooter;
100 case State_AuthFooter:
101 AuthenticateData(input, length);
102 m_totalFooterLength += length;
111 if (m_state >= State_IVSet && length >
MaxMessageLength()-m_totalMessageLength)
113 m_totalMessageLength += length;
120 throw BadState(
AlgorithmName(),
"ProcessData",
"setting key and IV");
121 case State_AuthFooter:
122 throw BadState(
AlgorithmName(),
"ProcessData was called after footer input has started");
124 AuthenticateLastHeaderBlock();
125 m_bufferedDataLength = 0;
126 m_state = AuthenticationIsOnPlaintext()==
IsForwardTransformation() ? State_AuthUntransformed : State_AuthTransformed;
128 case State_AuthUntransformed:
129 AuthenticateData(inString, length);
130 AccessSymmetricCipher().
ProcessData(outString, inString, length);
132 case State_AuthTransformed:
133 AccessSymmetricCipher().
ProcessData(outString, inString, length);
134 AuthenticateData(outString, length);
144 this->ThrowIfInvalidTruncatedSize(macSize);
152 throw InvalidArgument(
AlgorithmName() +
": additional authenticated data (AAD) cannot be input after data to be encrypted or decrypted");
161 throw BadState(
AlgorithmName(),
"TruncatedFinal",
"setting key and IV");
164 AuthenticateLastHeaderBlock();
165 m_bufferedDataLength = 0;
168 case State_AuthUntransformed:
169 case State_AuthTransformed:
170 AuthenticateLastConfidentialBlock();
171 m_bufferedDataLength = 0;
174 case State_AuthFooter:
175 AuthenticateLastFooterBlock(mac, macSize);
176 m_bufferedDataLength = 0;
183 m_state = State_KeySet;
Classes for authenticated encryption modes of operation.
void Resynchronize(const byte *iv, int length=-1)
Resynchronize with an IV.
void ProcessData(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt an array of bytes.
void Update(const byte *input, size_t length)
Updates a hash with additional input.
void TruncatedFinal(byte *mac, size_t macSize)
Computes the hash of the current message.
void SetKey(const byte *userKey, size_t keylength, const NameValuePairs ¶ms)
Sets or reset the key of this object.
virtual lword MaxHeaderLength() const =0
Provides the maximum length of AAD that can be input.
virtual lword MaxFooterLength() const
Provides the maximum length of AAD.
virtual lword MaxMessageLength() const =0
Provides the maximum length of encrypted data.
virtual std::string AlgorithmName() const
Provides the name of this algorithm.
An invalid argument was detected.
Interface for retrieving values given their names.
iterator begin()
Provides an iterator pointing to the first element in the memory block.
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Crypto++ library namespace.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.