5 #ifndef CRYPTOPP_IMPORTS
13 NAMESPACE_BEGIN(CryptoPP)
16 void Modes_TestInstantiations()
27 void CFB_ModePolicy::Iterate(byte *output,
const byte *input,
CipherDir dir,
size_t iterationCount)
30 assert(m_feedbackSize == BlockSize());
32 unsigned int s = BlockSize();
33 if (dir == ENCRYPTION)
37 memcpy(m_register, output+(iterationCount-1)*s, s);
41 memcpy(m_temp, input+(iterationCount-1)*s, s);
42 m_cipher->
AdvancedProcessBlocks(input, input+s, output+s, (iterationCount-1)*s, BlockTransformation::BT_ReverseDirection);
44 memcpy(m_register, m_temp, s);
48 void CFB_ModePolicy::TransformRegister()
52 unsigned int updateSize = BlockSize()-m_feedbackSize;
53 memmove_s(m_register, m_register.size(), m_register+m_feedbackSize, updateSize);
54 memcpy_s(m_register+updateSize, m_register.size()-updateSize, m_temp, m_feedbackSize);
57 void CFB_ModePolicy::CipherResynchronize(
const byte *iv,
size_t length)
59 assert(length == BlockSize());
60 CopyOrZero(m_register, iv, length);
64 void CFB_ModePolicy::SetFeedbackSize(
unsigned int feedbackSize)
66 if (feedbackSize > BlockSize())
68 m_feedbackSize = feedbackSize ? feedbackSize : BlockSize();
71 void CFB_ModePolicy::ResizeBuffers()
73 CipherModeBase::ResizeBuffers();
74 m_temp.
New(BlockSize());
77 void OFB_ModePolicy::WriteKeystream(byte *keystreamBuffer,
size_t iterationCount)
80 unsigned int s = BlockSize();
82 if (iterationCount > 1)
84 memcpy(m_register, keystreamBuffer+s*(iterationCount-1), s);
87 void OFB_ModePolicy::CipherResynchronize(byte *keystreamBuffer,
const byte *iv,
size_t length)
89 assert(length == BlockSize());
90 CopyOrZero(m_register, iv, length);
93 void CTR_ModePolicy::SeekToIteration(lword iterationCount)
96 for (
int i=BlockSize()-1; i>=0; i--)
98 unsigned int sum = m_register[i] + byte(iterationCount) + carry;
99 m_counterArray[i] = (byte) sum;
101 iterationCount >>= 8;
105 void CTR_ModePolicy::IncrementCounterBy256()
107 IncrementCounterByOne(m_counterArray, BlockSize()-1);
110 void CTR_ModePolicy::OperateKeystream(KeystreamOperation operation, byte *output,
const byte *input,
size_t iterationCount)
113 unsigned int s = BlockSize();
114 unsigned int inputIncrement = input ? s : 0;
116 while (iterationCount)
118 byte lsb = m_counterArray[s-1];
119 size_t blocks = UnsignedMin(iterationCount, 256U-lsb);
120 m_cipher->
AdvancedProcessBlocks(m_counterArray, input, output, blocks*s, BlockTransformation::BT_InBlockIsCounter|BlockTransformation::BT_AllowParallel);
121 if ((m_counterArray[s-1] = lsb + (byte)blocks) == 0)
122 IncrementCounterBy256();
125 input += blocks*inputIncrement;
126 iterationCount -= blocks;
130 void CTR_ModePolicy::CipherResynchronize(byte *keystreamBuffer,
const byte *iv,
size_t length)
132 assert(length == BlockSize());
133 CopyOrZero(m_register, iv, length);
134 m_counterArray = m_register;
137 void BlockOrientedCipherModeBase::UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms)
139 m_cipher->
SetKey(key, length, params);
144 const byte *iv = GetIVAndThrowIfInvalid(params, ivLength);
151 assert(length%BlockSize()==0);
152 m_cipher->
AdvancedProcessBlocks(inString, NULL, outString, length, BlockTransformation::BT_AllowParallel);
159 assert(length%BlockSize()==0);
161 unsigned int blockSize = BlockSize();
162 m_cipher->
AdvancedProcessBlocks(inString, m_register, outString, blockSize, BlockTransformation::BT_XorInput);
163 if (length > blockSize)
164 m_cipher->
AdvancedProcessBlocks(inString+blockSize, outString, outString+blockSize, length-blockSize, BlockTransformation::BT_XorInput);
165 memcpy(m_register, outString + length - blockSize, blockSize);
170 if (length <= BlockSize())
173 throw InvalidArgument(
"CBC_Encryption: message is too short for ciphertext stealing");
176 memcpy(outString, m_register, length);
177 outString = m_stolenIV;
182 xorbuf(m_register, inString, BlockSize());
184 inString += BlockSize();
185 length -= BlockSize();
186 memcpy(outString+BlockSize(), m_register, length);
190 xorbuf(m_register, inString, length);
192 memcpy(outString, m_register, BlockSize());
199 assert(length%BlockSize()==0);
201 unsigned int blockSize = BlockSize();
202 memcpy(m_temp, inString+length-blockSize, blockSize);
203 if (length > blockSize)
204 m_cipher->
AdvancedProcessBlocks(inString+blockSize, inString, outString+blockSize, length-blockSize, BlockTransformation::BT_ReverseDirection|BlockTransformation::BT_AllowParallel);
206 m_register.
swap(m_temp);
211 const byte *pn, *pn1;
212 bool stealIV = length <= BlockSize();
221 pn = inString + BlockSize();
223 length -= BlockSize();
227 memcpy(m_temp, pn1, BlockSize());
229 xorbuf(m_temp, pn, length);
232 memcpy(outString, m_temp, length);
235 memcpy(outString+BlockSize(), m_temp, length);
237 memcpy(m_temp, pn, length);
239 xorbuf(outString, m_temp, m_register, BlockSize());
exception thrown when an invalid argument is detected
void ProcessData(byte *outString, const byte *inString, size_t length)
encrypt or decrypt an array of bytes of specified length
void Resynchronize(const byte *iv, int length=-1)
resynchronize with an IV. ivLength=-1 means use IVSize()
void ProcessLastBlock(byte *outString, const byte *inString, size_t length)
for ciphers where the last block of data is special, encrypt or decrypt the last block of data ...
void swap(SecBlock< T, A > &b)
swap contents and size with another SecBlock
virtual void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms=g_nullNameValuePairs)
set or reset the key of this object
CipherDir
used to specify a direction for a cipher to operate in (encrypt or decrypt)
void New(size_type newSize)
change size, without preserving contents
void ProcessLastBlock(byte *outString, const byte *inString, size_t length)
for ciphers where the last block of data is special, encrypt or decrypt the last block of data ...
bool IsResynchronizable() const
returns whether this object can be resynchronized (i.e. supports initialization vectors) ...
void ProcessData(byte *outString, const byte *inString, size_t length)
encrypt or decrypt an array of bytes of specified length
void ProcessData(byte *outString, const byte *inString, size_t length)
encrypt or decrypt an array of bytes of specified length
interface for retrieving values given their names