5#ifndef CRYPTOPP_IMPORTS
11#if defined(CRYPTOPP_DEBUG)
17#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
18void Modes_TestInstantiations()
29void CipherModeBase::ResizeBuffers()
34void CFB_ModePolicy::Iterate(
byte *output,
const byte *input,
CipherDir dir,
size_t iterationCount)
45 m_cipher->ProcessAndXorBlock(m_register, input, output);
46 if (iterationCount > 1)
47 m_cipher->AdvancedProcessBlocks(output,
PtrAdd(input,s),
PtrAdd(output,s), (iterationCount-1)*s, 0);
48 std::memcpy(m_register,
PtrAdd(output,(iterationCount-1)*s), s);
53 std::memcpy(m_temp,
PtrAdd(input,(iterationCount-1)*s), s);
54 if (iterationCount > 1)
56 m_cipher->ProcessAndXorBlock(m_register, input, output);
57 std::memcpy(m_register, m_temp, s);
61void CFB_ModePolicy::TransformRegister()
67 const ptrdiff_t updateSize =
BlockSize()-m_feedbackSize;
68 m_cipher->ProcessBlock(m_register, m_temp);
69 memmove_s(m_register, m_register.size(),
PtrAdd(m_register.begin(),m_feedbackSize), updateSize);
70 memcpy_s(
PtrAdd(m_register.begin(),updateSize), m_register.size()-updateSize, m_temp, m_feedbackSize);
73void CFB_ModePolicy::CipherResynchronize(
const byte *iv,
size_t length)
78 CopyOrZero(m_register, m_register.size(), iv, length);
82void CFB_ModePolicy::SetFeedbackSize(
unsigned int feedbackSize)
87 m_feedbackSize = feedbackSize ? feedbackSize :
BlockSize();
90void CFB_ModePolicy::ResizeBuffers()
92 CipherModeBase::ResizeBuffers();
96byte* CFB_ModePolicy::GetRegisterBegin()
103void OFB_ModePolicy::WriteKeystream(
byte *keystreamBuffer,
size_t iterationCount)
110 m_cipher->ProcessBlock(m_register, keystreamBuffer);
111 if (iterationCount > 1)
112 m_cipher->AdvancedProcessBlocks(keystreamBuffer, NULLPTR,
PtrAdd(keystreamBuffer, s), s*(iterationCount-1), 0);
113 std::memcpy(m_register,
PtrAdd(keystreamBuffer, (iterationCount-1)*s), s);
116void OFB_ModePolicy::CipherResynchronize(
byte *keystreamBuffer,
const byte *iv,
size_t length)
118 CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(length);
122 CopyOrZero(m_register, m_register.size(), iv, length);
125void CTR_ModePolicy::SeekToIteration(
lword iterationCount)
130 unsigned int sum = m_register[i] + (
byte)iterationCount + carry;
131 m_counterArray[i] =
byte(sum & 0xff);
133 iterationCount >>= 8;
137void CTR_ModePolicy::IncrementCounterBy256()
142void CTR_ModePolicy::OperateKeystream(
KeystreamOperation operation,
byte *output,
const byte *input,
size_t iterationCount)
148 CRYPTOPP_UNUSED(operation);
152 while (iterationCount)
154 const byte lsb = m_counterArray[s-1];
155 const size_t blocks =
UnsignedMin(iterationCount, 256u-lsb);
156 const size_t outIncrement = output ? blocks*s : 0;
157 const size_t inIncrement = input ? blocks*s : 0;
160 if ((m_counterArray[s-1] =
static_cast<byte>(lsb + blocks)) == 0)
161 IncrementCounterBy256();
163 output =
PtrAdd(output, outIncrement);
164 input =
PtrAdd(input, inIncrement);
165 iterationCount -= blocks;
169void CTR_ModePolicy::CipherResynchronize(
byte *keystreamBuffer,
const byte *iv,
size_t length)
171 CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(length);
175 CopyOrZero(m_register, m_register.size(), iv, length);
176 m_counterArray.
Assign(m_register.begin(), m_register.size());
181 m_cipher->SetKey(key, length, params);
186 const byte *iv = GetIVAndThrowIfInvalid(params, ivLength);
191void BlockOrientedCipherModeBase::ResizeBuffers()
193 CipherModeBase::ResizeBuffers();
209 const unsigned int blockSize =
BlockSize();
211 if (length > blockSize)
213 std::memcpy(m_register,
PtrAdd(outString, length - blockSize), blockSize);
218 CRYPTOPP_UNUSED(outLength);
219 const size_t used = inLength;
220 const unsigned int blockSize =
BlockSize();
222 if (inLength <= blockSize)
225 throw InvalidArgument(
"CBC_Encryption: message is too short for ciphertext stealing");
228 std::memcpy(outString, m_register, inLength);
229 outString = m_stolenIV;
234 xorbuf(m_register, inString, blockSize);
235 m_cipher->ProcessBlock(m_register);
236 inString =
PtrAdd(inString, blockSize);
237 inLength -= blockSize;
238 std::memcpy(
PtrAdd(outString, blockSize), m_register, inLength);
242 xorbuf(m_register, inString, inLength);
243 m_cipher->ProcessBlock(m_register);
244 std::memcpy(outString, m_register, blockSize);
249void CBC_Decryption::ResizeBuffers()
251 BlockOrientedCipherModeBase::ResizeBuffers();
258 if (!length) {
return;}
261 const unsigned int blockSize =
BlockSize();
262 std::memcpy(m_temp,
PtrAdd(inString, length-blockSize), blockSize);
263 if (length > blockSize)
265 m_cipher->ProcessAndXorBlock(inString, m_register, outString);
266 m_register.swap(m_temp);
271 CRYPTOPP_UNUSED(outLength);
272 const byte *pn1, *pn2;
273 const size_t used = inLength;
274 const bool stealIV = inLength <=
BlockSize();
275 const unsigned int blockSize =
BlockSize();
284 pn1 =
PtrAdd(inString, blockSize);
286 inLength -= blockSize;
290 std::memcpy(m_temp, pn2, blockSize);
291 m_cipher->ProcessBlock(m_temp);
292 xorbuf(m_temp, pn1, inLength);
296 std::memcpy(outString, m_temp, inLength);
300 std::memcpy(
PtrAdd(outString, blockSize), m_temp, inLength);
302 std::memcpy(m_temp, pn1, inLength);
303 m_cipher->ProcessBlock(m_temp);
304 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.
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 New(size_type newSize)
Change size without preserving contents.
void Assign(const T *ptr, size_type len)
Set contents and size from an array.
size_type size() const
Provides the count of elements in the SecBlock.
bool IsResynchronizable() const
Determines if the object can be resynchronized.
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()
CRYPTOPP_DLL void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
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.
Crypto++ library namespace.
const char * BlockSize()
int, in bytes
Classes for implementing stream ciphers.
KeystreamOperation
Keystream operation flags.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.