5 #ifndef CRYPTOPP_IMPORTS
14 NAMESPACE_BEGIN(CryptoPP)
16 #if CRYPTOPP_DEBUG && !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)
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()
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)
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)
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());
178 m_cipher->AdvancedProcessBlocks(inString, NULL, outString, length, BlockTransformation::BT_AllowParallel);
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());
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());
An invalid argument was detected.
void ProcessData(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt an array of bytes.
void memmove_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memmove()
void Resynchronize(const byte *iv, int length=-1)
Resynchronize with an IV.
void ProcessLastBlock(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt the last block of data.
Utility functions for the Crypto++ library.
void IncrementCounterByOne(byte *inout, unsigned int size)
Performs an addition with carry on a block of bytes.
Class file for modes of operation.
CipherDir
Specifies a direction for a cipher to operate.
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
void New(size_type newSize)
Change size without preserving contents.
the cipher is performing encryption
void ProcessLastBlock(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt the last block of data.
Block cipher mode of operation aggregate.
bool IsResynchronizable() const
Determines if the object can be resynchronized.
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
const char * BlockSize()
int, in bytes
Classes for DES, 2-key Triple-DES, 3-key Triple-DES and DESX.
void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms)
Sets the key for this object without performing parameter validation.
KeystreamOperation
Keystream operation flags.
void ProcessData(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt an array of bytes.
void ProcessData(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt an array of bytes.
Interface for retrieving values given their names.