5 #ifndef CRYPTOPP_IMPORTS
11 void 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 memcpy(data+num, input, blockSize-num);
26 AuthenticateBlocks(data, blockSize);
27 input += (blockSize-num);
28 len -= (blockSize-num);
34 memcpy(data+num, input, len);
35 num += (
unsigned int)len;
43 size_t leftOver = AuthenticateBlocks(input, len);
44 input += (len - leftOver);
49 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;