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)
41 assert(m_feedbackSize == BlockSize());
43 const unsigned int s = BlockSize();
44 if (dir == ENCRYPTION)
47 if (iterationCount > 1)
49 memcpy(m_register, output+(iterationCount-1)*s, s);
53 memcpy(m_temp, input+(iterationCount-1)*s, s);
54 if (iterationCount > 1)
57 memcpy(m_register, m_temp, s);
61 void CFB_ModePolicy::TransformRegister()
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)
72 assert(length == BlockSize());
73 CopyOrZero(m_register, iv, length);
77 void CFB_ModePolicy::SetFeedbackSize(
unsigned int feedbackSize)
79 if (feedbackSize > BlockSize())
81 m_feedbackSize = feedbackSize ? feedbackSize : BlockSize();
84 void CFB_ModePolicy::ResizeBuffers()
86 CipherModeBase::ResizeBuffers();
87 m_temp.New(BlockSize());
90 void OFB_ModePolicy::WriteKeystream(byte *keystreamBuffer,
size_t iterationCount)
93 unsigned int s = BlockSize();
95 if (iterationCount > 1)
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);
103 assert(length == BlockSize());
105 CopyOrZero(m_register, iv, length);
108 void CTR_ModePolicy::SeekToIteration(lword iterationCount)
111 for (
int i=BlockSize()-1; i>=0; i--)
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)
128 unsigned int s = BlockSize();
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);
148 assert(length == BlockSize());
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());
210 inString += BlockSize();
211 length -= BlockSize();
212 memcpy(outString+BlockSize(), m_register, length);
216 xorbuf(m_register, inString, length);
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)
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());
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);
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 ProcessLastBlock(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt the last block of data.
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.
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 ProcessLastBlock(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt the last block of data.
bool IsResynchronizable() const
returns whether the object can be resynchronized (i.e. supports initialization vectors) ...
virtual void Resynchronize(const byte *iv, int ivLength=-1)
resynchronize with an IV. ivLength=-1 means use IVSize()
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
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.
Crypto++ library namespace.
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.