5 #ifndef CRYPTOPP_IMPORTS
10 #if defined(CRYPTOPP_DEBUG)
16 #if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
17 void Modes_TestInstantiations()
28 void CipherModeBase::ResizeBuffers()
33 void CFB_ModePolicy::Iterate(
byte *output,
const byte *input,
CipherDir dir,
size_t iterationCount)
44 m_cipher->ProcessAndXorBlock(m_register, input, output);
45 if (iterationCount > 1)
46 m_cipher->AdvancedProcessBlocks(output,
PtrAdd(input,s),
PtrAdd(output,s), (iterationCount-1)*s, 0);
47 memcpy(m_register,
PtrAdd(output,(iterationCount-1)*s), s);
52 memcpy(m_temp,
PtrAdd(input,(iterationCount-1)*s), s);
53 if (iterationCount > 1)
55 m_cipher->ProcessAndXorBlock(m_register, input, output);
56 memcpy(m_register, m_temp, s);
60 void CFB_ModePolicy::TransformRegister()
66 const ptrdiff_t updateSize =
BlockSize()-m_feedbackSize;
67 m_cipher->ProcessBlock(m_register, m_temp);
68 memmove_s(m_register, m_register.size(),
PtrAdd(m_register.begin(),m_feedbackSize), updateSize);
69 memcpy_s(
PtrAdd(m_register.begin(),updateSize), m_register.size()-updateSize, m_temp, m_feedbackSize);
72 void CFB_ModePolicy::CipherResynchronize(
const byte *iv,
size_t length)
77 CopyOrZero(m_register, m_register.size(), iv, length);
81 void CFB_ModePolicy::SetFeedbackSize(
unsigned int feedbackSize)
85 m_feedbackSize = feedbackSize ? feedbackSize :
BlockSize();
88 void CFB_ModePolicy::ResizeBuffers()
90 CipherModeBase::ResizeBuffers();
94 byte* CFB_ModePolicy::GetRegisterBegin()
101 void OFB_ModePolicy::WriteKeystream(
byte *keystreamBuffer,
size_t iterationCount)
108 m_cipher->ProcessBlock(m_register, keystreamBuffer);
109 if (iterationCount > 1)
110 m_cipher->AdvancedProcessBlocks(keystreamBuffer, NULLPTR,
PtrAdd(keystreamBuffer, s), s*(iterationCount-1), 0);
111 memcpy(m_register,
PtrAdd(keystreamBuffer, (iterationCount-1)*s), s);
114 void OFB_ModePolicy::CipherResynchronize(
byte *keystreamBuffer,
const byte *iv,
size_t length)
116 CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(length);
120 CopyOrZero(m_register, m_register.size(), iv, length);
123 void CTR_ModePolicy::SeekToIteration(lword iterationCount)
128 unsigned int sum = m_register[i] + (byte)iterationCount + carry;
129 m_counterArray[i] = byte(sum & 0xff);
131 iterationCount >>= 8;
135 void CTR_ModePolicy::IncrementCounterBy256()
140 void CTR_ModePolicy::OperateKeystream(
KeystreamOperation ,
byte *output,
const byte *input,
size_t iterationCount)
146 const unsigned int inputIncrement = input ? s : 0;
148 while (iterationCount)
150 const byte lsb = m_counterArray[s-1];
151 const size_t blocks =
UnsignedMin(iterationCount, 256U-lsb);
154 if ((m_counterArray[s-1] =
byte(lsb + blocks)) == 0)
155 IncrementCounterBy256();
157 output =
PtrAdd(output, blocks*s);
158 input =
PtrAdd(input, blocks*inputIncrement);
159 iterationCount -= blocks;
163 void CTR_ModePolicy::CipherResynchronize(
byte *keystreamBuffer,
const byte *iv,
size_t length)
165 CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(length);
169 CopyOrZero(m_register, m_register.size(), iv, length);
170 m_counterArray.
Assign(m_register.begin(), m_register.size());
175 m_cipher->SetKey(key, length, params);
180 const byte *iv = GetIVAndThrowIfInvalid(params, ivLength);
185 void BlockOrientedCipherModeBase::ResizeBuffers()
187 CipherModeBase::ResizeBuffers();
188 m_buffer.
New(BlockSize());
203 const unsigned int blockSize = BlockSize();
205 if (length > blockSize)
207 memcpy(m_register,
PtrAdd(outString, length - blockSize), blockSize);
212 CRYPTOPP_UNUSED(outLength);
213 const size_t used = inLength;
214 const unsigned int blockSize = BlockSize();
216 if (inLength <= blockSize)
219 throw InvalidArgument(
"CBC_Encryption: message is too short for ciphertext stealing");
222 memcpy(outString, m_register, inLength);
223 outString = m_stolenIV;
228 xorbuf(m_register, inString, blockSize);
229 m_cipher->ProcessBlock(m_register);
230 inString =
PtrAdd(inString, blockSize);
231 inLength -= blockSize;
232 memcpy(
PtrAdd(outString, blockSize), m_register, inLength);
236 xorbuf(m_register, inString, inLength);
237 m_cipher->ProcessBlock(m_register);
238 memcpy(outString, m_register, blockSize);
243 void CBC_Decryption::ResizeBuffers()
245 BlockOrientedCipherModeBase::ResizeBuffers();
246 m_temp.
New(BlockSize());
252 if (!length) {
return;}
255 const unsigned int blockSize = BlockSize();
256 memcpy(m_temp,
PtrAdd(inString,length-blockSize), blockSize);
257 if (length > blockSize)
259 m_cipher->ProcessAndXorBlock(inString, m_register, outString);
260 m_register.swap(m_temp);
265 CRYPTOPP_UNUSED(outLength);
266 const byte *pn1, *pn2;
267 const size_t used = inLength;
268 const bool stealIV = inLength <= BlockSize();
269 const unsigned int blockSize = BlockSize();
278 pn1 =
PtrAdd(inString, blockSize);
280 inLength -= blockSize;
284 memcpy(m_temp, pn2, blockSize);
285 m_cipher->ProcessBlock(m_temp);
286 xorbuf(m_temp, pn1, inLength);
290 memcpy(outString, m_temp, inLength);
294 memcpy(
PtrAdd(outString, blockSize), m_temp, inLength);
296 memcpy(m_temp, pn1, inLength);
297 m_cipher->ProcessBlock(m_temp);
298 xorbuf(outString, m_temp, m_register, blockSize);