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)
40 const unsigned int s = BlockSize();
44 if (iterationCount > 1)
46 memcpy(m_register, output+(iterationCount-1)*s, s);
50 memcpy(m_temp, input+(iterationCount-1)*s, s);
51 if (iterationCount > 1)
54 memcpy(m_register, m_temp, s);
58 void CFB_ModePolicy::TransformRegister()
62 unsigned int updateSize = BlockSize()-m_feedbackSize;
63 memmove_s(m_register, m_register.
size(), m_register+m_feedbackSize, updateSize);
64 memcpy_s(m_register+updateSize, m_register.
size()-updateSize, m_temp, m_feedbackSize);
67 void CFB_ModePolicy::CipherResynchronize(
const byte *iv,
size_t length)
70 CopyOrZero(m_register, m_register.
size(), iv, length);
74 void CFB_ModePolicy::SetFeedbackSize(
unsigned int feedbackSize)
76 if (feedbackSize > BlockSize())
78 m_feedbackSize = feedbackSize ? feedbackSize : BlockSize();
81 void CFB_ModePolicy::ResizeBuffers()
83 CipherModeBase::ResizeBuffers();
84 m_temp.New(BlockSize());
87 void OFB_ModePolicy::WriteKeystream(byte *keystreamBuffer,
size_t iterationCount)
90 unsigned int s = BlockSize();
92 if (iterationCount > 1)
93 m_cipher->
AdvancedProcessBlocks(keystreamBuffer, NULLPTR, keystreamBuffer+s, s*(iterationCount-1), 0);
94 memcpy(m_register, keystreamBuffer+s*(iterationCount-1), s);
97 void OFB_ModePolicy::CipherResynchronize(byte *keystreamBuffer,
const byte *iv,
size_t length)
99 CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(length);
102 CopyOrZero(m_register, m_register.
size(), iv, length);
105 void CTR_ModePolicy::SeekToIteration(lword iterationCount)
108 for (
int i=BlockSize()-1; i>=0; i--)
110 unsigned int sum = m_register[i] + byte(iterationCount) + carry;
111 m_counterArray[i] = (byte) sum;
113 iterationCount >>= 8;
117 void CTR_ModePolicy::IncrementCounterBy256()
122 void CTR_ModePolicy::OperateKeystream(
KeystreamOperation , byte *output,
const byte *input,
size_t iterationCount)
125 unsigned int s = BlockSize();
126 unsigned int inputIncrement = input ? s : 0;
128 while (iterationCount)
130 byte lsb = m_counterArray[s-1];
131 size_t blocks =
UnsignedMin(iterationCount, 256U-lsb);
133 if ((m_counterArray[s-1] = lsb + (byte)blocks) == 0)
134 IncrementCounterBy256();
137 input += blocks*inputIncrement;
138 iterationCount -= blocks;
142 void CTR_ModePolicy::CipherResynchronize(byte *keystreamBuffer,
const byte *iv,
size_t length)
144 CRYPTOPP_UNUSED(keystreamBuffer), CRYPTOPP_UNUSED(length);
147 CopyOrZero(m_register, m_register.
size(), iv, length);
148 m_counterArray = m_register;
153 m_cipher->
SetKey(key, length, params);
158 const byte *iv = GetIVAndThrowIfInvalid(params, ivLength);
163 void BlockOrientedCipherModeBase::ResizeBuffers()
165 CipherModeBase::ResizeBuffers();
166 m_buffer.New(BlockSize());
180 const unsigned int blockSize = BlockSize();
182 if (length > blockSize)
184 memcpy(m_register, outString + length - blockSize, blockSize);
189 CRYPTOPP_UNUSED(outLength);
190 size_t used = inLength;
191 if (inLength <= BlockSize())
194 throw InvalidArgument(
"CBC_Encryption: message is too short for ciphertext stealing");
197 memcpy(outString, m_register, inLength);
198 outString = m_stolenIV;
203 xorbuf(m_register, inString, BlockSize());
205 inString += BlockSize();
206 inLength -= BlockSize();
207 memcpy(outString+BlockSize(), m_register, inLength);
211 xorbuf(m_register, inString, inLength);
213 memcpy(outString, m_register, BlockSize());
218 void CBC_Decryption::ResizeBuffers()
220 BlockOrientedCipherModeBase::ResizeBuffers();
221 m_temp.New(BlockSize());
230 const unsigned int blockSize = BlockSize();
231 memcpy(m_temp, inString+length-blockSize, blockSize);
232 if (length > blockSize)
235 m_register.
swap(m_temp);
240 CRYPTOPP_UNUSED(outLength);
241 const byte *pn1, *pn2;
242 bool stealIV = inLength <= BlockSize();
243 size_t used = inLength;
252 pn1 = inString + BlockSize();
254 inLength -= BlockSize();
258 memcpy(m_temp, pn2, BlockSize());
260 xorbuf(m_temp, pn1, inLength);
264 memcpy(outString, m_temp, inLength);
268 memcpy(outString+BlockSize(), m_temp, inLength);
270 memcpy(m_temp, pn1, inLength);
272 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 swap(SecBlock< T, A > &b)
Swap contents with another SecBlock.
Utility functions for the Crypto++ library.
virtual void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms=g_nullNameValuePairs)
Sets or reset the key of this object.
void IncrementCounterByOne(byte *inout, unsigned int size)
Performs an addition with carry on a block of bytes.
Classes for block cipher 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.
size_t ProcessLastBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength)
Encrypt or decrypt the last block of data.
the cipher is performing encryption
Block cipher mode of operation aggregate.
virtual void Resynchronize(const byte *iv, int ivLength=-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.
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.
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.
Crypto++ library namespace.
bool IsResynchronizable() const
Determines if the object can be resynchronized.
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.
size_type size() const
Provides the count of elements in the SecBlock.
Interface for retrieving values given their names.