5 #ifndef CRYPTOPP_IMPORTS
16 #if !defined(NDEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
17 void Modes_TestInstantiations()
29 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
30 void CipherModeBase::ResizeBuffers()
36 void CFB_ModePolicy::Iterate(
byte *output,
const byte *input,
CipherDir dir,
size_t iterationCount)
40 assert(m_cipher->IsForwardTransformation());
46 m_cipher->ProcessAndXorBlock(m_register, input, output);
47 if (iterationCount > 1)
48 m_cipher->AdvancedProcessBlocks(output, input+s, output+s, (iterationCount-1)*s, 0);
49 memcpy(m_register, output+(iterationCount-1)*s, s);
53 memcpy(m_temp, input+(iterationCount-1)*s, s);
54 if (iterationCount > 1)
56 m_cipher->ProcessAndXorBlock(m_register, input, output);
57 memcpy(m_register, m_temp, s);
61 void CFB_ModePolicy::TransformRegister()
63 assert(m_cipher->IsForwardTransformation());
64 m_cipher->ProcessBlock(m_register, m_temp);
65 unsigned int updateSize =
BlockSize()-m_feedbackSize;
66 memmove_s(m_register, m_register.size(), m_register+m_feedbackSize, updateSize);
67 memcpy_s(m_register+updateSize, m_register.size()-updateSize, m_temp, m_feedbackSize);
70 void CFB_ModePolicy::CipherResynchronize(
const byte *iv,
size_t length)
73 CopyOrZero(m_register, iv, length);
77 void CFB_ModePolicy::SetFeedbackSize(
unsigned int feedbackSize)
81 m_feedbackSize = feedbackSize ? feedbackSize :
BlockSize();
84 void CFB_ModePolicy::ResizeBuffers()
86 CipherModeBase::ResizeBuffers();
90 void OFB_ModePolicy::WriteKeystream(
byte *keystreamBuffer,
size_t iterationCount)
92 assert(m_cipher->IsForwardTransformation());
94 m_cipher->ProcessBlock(m_register, keystreamBuffer);
95 if (iterationCount > 1)
96 m_cipher->AdvancedProcessBlocks(keystreamBuffer, NULL, keystreamBuffer+s, s*(iterationCount-1), 0);
97 memcpy(m_register, keystreamBuffer+s*(iterationCount-1), s);
100 void OFB_ModePolicy::CipherResynchronize(
byte *keystreamBuffer,
const byte *iv,
size_t length)
102 CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(length);
105 CopyOrZero(m_register, iv, length);
108 void CTR_ModePolicy::SeekToIteration(lword iterationCount)
113 unsigned int sum = m_register[i] + byte(iterationCount) + carry;
114 m_counterArray[i] = (byte) sum;
116 iterationCount >>= 8;
120 void CTR_ModePolicy::IncrementCounterBy256()
125 void CTR_ModePolicy::OperateKeystream(
KeystreamOperation ,
byte *output,
const byte *input,
size_t iterationCount)
127 assert(m_cipher->IsForwardTransformation());
129 unsigned int inputIncrement = input ? s : 0;
131 while (iterationCount)
133 byte lsb = m_counterArray[s-1];
134 size_t blocks =
UnsignedMin(iterationCount, 256U-lsb);
136 if ((m_counterArray[s-1] = lsb + (
byte)blocks) == 0)
137 IncrementCounterBy256();
140 input += blocks*inputIncrement;
141 iterationCount -= blocks;
145 void CTR_ModePolicy::CipherResynchronize(
byte *keystreamBuffer,
const byte *iv,
size_t length)
147 CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(length);
150 CopyOrZero(m_register, iv, length);
151 m_counterArray = m_register;
156 m_cipher->SetKey(key, length, params);
161 const byte *iv = GetIVAndThrowIfInvalid(params, ivLength);
167 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
168 void BlockOrientedCipherModeBase::ResizeBuffers()
170 CipherModeBase::ResizeBuffers();
171 m_buffer.
New(BlockSize());
177 assert(length%BlockSize()==0);
178 m_cipher->AdvancedProcessBlocks(inString, NULL, outString, length, BlockTransformation::BT_AllowParallel);
185 assert(length%BlockSize()==0);
187 unsigned int blockSize = BlockSize();
188 m_cipher->AdvancedProcessBlocks(inString, m_register, outString, blockSize, BlockTransformation::BT_XorInput);
189 if (length > blockSize)
190 m_cipher->AdvancedProcessBlocks(inString+blockSize, outString, outString+blockSize, length-blockSize, BlockTransformation::BT_XorInput);
191 memcpy(m_register, outString + length - blockSize, blockSize);
196 if (length <= BlockSize())
199 throw InvalidArgument(
"CBC_Encryption: message is too short for ciphertext stealing");
202 memcpy(outString, m_register, length);
203 outString = m_stolenIV;
208 xorbuf(m_register, inString, BlockSize());
209 m_cipher->ProcessBlock(m_register);
210 inString += BlockSize();
211 length -= BlockSize();
212 memcpy(outString+BlockSize(), m_register, length);
216 xorbuf(m_register, inString, length);
217 m_cipher->ProcessBlock(m_register);
218 memcpy(outString, m_register, BlockSize());
222 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
223 void CBC_Decryption::ResizeBuffers()
225 BlockOrientedCipherModeBase::ResizeBuffers();
226 m_temp.
New(BlockSize());
234 assert(length%BlockSize()==0);
236 unsigned int blockSize = BlockSize();
237 memcpy(m_temp, inString+length-blockSize, blockSize);
238 if (length > blockSize)
240 m_cipher->ProcessAndXorBlock(inString, m_register, outString);
241 m_register.swap(m_temp);
246 const byte *pn, *pn1;
247 bool stealIV = length <= BlockSize();
256 pn = inString + BlockSize();
258 length -= BlockSize();
262 memcpy(m_temp, pn1, BlockSize());
263 m_cipher->ProcessBlock(m_temp);
264 xorbuf(m_temp, pn, length);
267 memcpy(outString, m_temp, length);
270 memcpy(outString+BlockSize(), m_temp, length);
272 memcpy(m_temp, pn, length);
273 m_cipher->ProcessBlock(m_temp);
274 xorbuf(outString, m_temp, m_register, BlockSize());