5#ifndef CRYPTOPP_IMPORTS
10#if defined(CRYPTOPP_DEBUG)
14NAMESPACE_BEGIN(CryptoPP)
16#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
17void Modes_TestInstantiations()
28void CipherModeBase::ResizeBuffers()
35 CRYPTOPP_ASSERT(input); CRYPTOPP_ASSERT(output);
37 CRYPTOPP_ASSERT(m_register.
size() == BlockSize());
38 CRYPTOPP_ASSERT(m_temp.
size() == BlockSize());
39 CRYPTOPP_ASSERT(iterationCount > 0);
41 const unsigned int s = BlockSize();
45 if (iterationCount > 1)
47 memcpy(m_register,
PtrAdd(output,(iterationCount-1)*s), s);
52 memcpy(m_temp,
PtrAdd(input,(iterationCount-1)*s), s);
53 if (iterationCount > 1)
56 memcpy(m_register, m_temp, s);
63 CRYPTOPP_ASSERT(m_register.
size() == BlockSize());
64 CRYPTOPP_ASSERT(m_temp.
size() == BlockSize());
66 const ptrdiff_t updateSize = BlockSize()-m_feedbackSize;
74 CRYPTOPP_ASSERT(length == BlockSize());
75 CRYPTOPP_ASSERT(m_register.
size() == BlockSize());
81void CFB_ModePolicy::SetFeedbackSize(
unsigned int feedbackSize)
83 if (feedbackSize > BlockSize())
85 m_feedbackSize = feedbackSize ? feedbackSize : BlockSize();
88void CFB_ModePolicy::ResizeBuffers()
90 CipherModeBase::ResizeBuffers();
91 m_temp.
New(BlockSize());
96 CRYPTOPP_ASSERT(!m_register.
empty());
97 CRYPTOPP_ASSERT(BlockSize() >= m_feedbackSize);
98 return PtrAdd(m_register.
begin(), BlockSize() - m_feedbackSize);
104 CRYPTOPP_ASSERT(m_register.
size() == BlockSize());
105 CRYPTOPP_ASSERT(iterationCount > 0);
107 const unsigned int s = BlockSize();
109 if (iterationCount > 1)
111 memcpy(m_register,
PtrAdd(keystreamBuffer, (iterationCount-1)*s), s);
116 CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(length);
117 CRYPTOPP_ASSERT(m_register.
size() == BlockSize());
118 CRYPTOPP_ASSERT(length == BlockSize());
126 for (
int i=BlockSize()-1; i>=0; i--)
128 unsigned int sum = m_register[i] + (
byte)iterationCount + carry;
129 m_counterArray[i] =
byte(sum & 0xff);
131 iterationCount >>= 8;
135void CTR_ModePolicy::IncrementCounterBy256()
143 CRYPTOPP_ASSERT(m_counterArray.
size() == BlockSize());
145 const unsigned int s = BlockSize();
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;
165 CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(length);
166 CRYPTOPP_ASSERT(m_register.
size() == BlockSize());
167 CRYPTOPP_ASSERT(length == BlockSize());
175 m_cipher->
SetKey(key, length, params);
185void BlockOrientedCipherModeBase::ResizeBuffers()
187 CipherModeBase::ResizeBuffers();
188 m_buffer.
New(BlockSize());
193 CRYPTOPP_ASSERT(length%BlockSize()==0);
199 CRYPTOPP_ASSERT(length%BlockSize()==0);
200 CRYPTOPP_ASSERT(m_register.
size() == 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);
230 inString =
PtrAdd(inString, blockSize);
231 inLength -= blockSize;
232 memcpy(
PtrAdd(outString, blockSize), m_register, inLength);
236 xorbuf(m_register, inString, inLength);
238 memcpy(outString, m_register, blockSize);
243void CBC_Decryption::ResizeBuffers()
245 BlockOrientedCipherModeBase::ResizeBuffers();
246 m_temp.
New(BlockSize());
251 CRYPTOPP_ASSERT(length%BlockSize()==0);
252 if (!length) {
return;}
255 const unsigned int blockSize = BlockSize();
256 memcpy(m_temp,
PtrAdd(inString, length-blockSize), blockSize);
257 if (length > blockSize)
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);
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);
298 xorbuf(outString, m_temp, m_register, blockSize);
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms)
Sets the key for this object without performing parameter validation.
void Resynchronize(const byte *iv, int length=-1)
Resynchronize with an IV.
size_t ProcessLastBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength)
Encrypt or decrypt the last block of data.
size_t ProcessLastBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength)
Encrypt or decrypt the last block of data.
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.
void CipherResynchronize(const byte *iv, size_t length)
Resynchronize the cipher.
void Iterate(byte *output, const byte *input, CipherDir dir, size_t iterationCount)
Iterate the cipher.
byte * GetRegisterBegin()
Access the feedback register.
void TransformRegister()
TODO.
void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
Resynchronize the cipher.
void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
Operates the keystream.
void SeekToIteration(lword iterationCount)
Seeks to a random position in the stream.
Block cipher mode of operation aggregate.
void ProcessData(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt an array of bytes.
An invalid argument was detected.
Interface for retrieving values given their names.
void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
Resynchronize the cipher.
void WriteKeystream(byte *keystreamBuffer, size_t iterationCount)
Generate the keystream.
iterator begin()
Provides an iterator pointing to the first element in the memory block.
void swap(SecBlock< T, A > &b)
Swap contents with another SecBlock.
void New(size_type newSize)
Change size without preserving contents.
void Assign(const T *ptr, size_type len)
Set contents and size from an array.
bool empty() const
Determines if the SecBlock is empty.
size_type size() const
Provides the count of elements in the SecBlock.
const byte * GetIVAndThrowIfInvalid(const NameValuePairs ¶ms, size_t &size)
Retrieves and validates the IV.
bool IsResynchronizable() const
Determines if the object can be resynchronized.
virtual void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms=g_nullNameValuePairs)
Sets or reset the key of this object.
unsigned char byte
8-bit unsigned datatype
word64 lword
Large word type.
CipherDir
Specifies a direction for a cipher to operate.
@ ENCRYPTION
the cipher is performing encryption
Classes for DES, 2-key Triple-DES, 3-key Triple-DES and DESX.
Utility functions for the Crypto++ library.
void IncrementCounterByOne(byte *inout, unsigned int size)
Performs an addition with carry on a block of bytes.
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
PTR PtrAdd(PTR pointer, OFF offset)
Create a pointer with an offset.
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be negative and incorrectly promoted.
void memmove_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memmove()
Classes for block cipher modes of operation.
void CopyOrZero(void *dest, size_t dsize, const void *src, size_t ssize)
Initialize a block of memory.
KeystreamOperation
Keystream operation flags.