5 #ifndef CRYPTOPP_IMPORTS 14 PolicyInterface &policy = this->AccessPolicy();
15 policy.CipherSetKey(params, key, length);
17 unsigned int bufferByteSize = policy.CanOperateKeystream() ? GetBufferByteSize(policy) :
RoundUpToMultipleOf(1024U, GetBufferByteSize(policy));
18 m_buffer.New(bufferByteSize);
20 if (this->IsResynchronizable())
23 const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
24 policy.CipherResynchronize(m_buffer, iv, ivLength);
33 size_t len =
STDMIN(m_leftOver, length);
34 memcpy(outString, KeystreamBufferEnd()-m_leftOver, len);
47 if (length >= bytesPerIteration)
49 size_t iterations = length / bytesPerIteration;
51 outString += iterations * bytesPerIteration;
52 length -= iterations * bytesPerIteration;
58 size_t bufferIterations = bufferByteSize / bytesPerIteration;
60 policy.
WriteKeystream(KeystreamBufferEnd()-bufferByteSize, bufferIterations);
61 memcpy(outString, KeystreamBufferEnd()-bufferByteSize, length);
62 m_leftOver = bufferByteSize - length;
71 size_t len =
STDMIN(m_leftOver, length);
72 xorbuf(outString, inString, KeystreamBufferEnd()-m_leftOver, len);
88 size_t iterations = length / bytesPerIteration;
94 inString += iterations * bytesPerIteration;
95 outString += iterations * bytesPerIteration;
96 length -= iterations * bytesPerIteration;
102 size_t bufferByteSize = m_buffer.size();
103 size_t bufferIterations = bufferByteSize / bytesPerIteration;
105 while (length >= bufferByteSize)
108 xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
109 length -= bufferByteSize;
110 inString += bufferByteSize;
111 outString += bufferByteSize;
117 bufferIterations = bufferByteSize / bytesPerIteration;
119 policy.
WriteKeystream(KeystreamBufferEnd()-bufferByteSize, bufferIterations);
120 xorbuf(outString, inString, KeystreamBufferEnd()-bufferByteSize, length);
121 m_leftOver = bufferByteSize - length;
130 m_buffer.New(GetBufferByteSize(policy));
134 template <
class BASE>
141 position %= bytesPerIteration;
146 m_leftOver = bytesPerIteration - (
unsigned int)position;
152 template <
class BASE>
155 PolicyInterface &policy = this->AccessPolicy();
156 policy.CipherSetKey(params, key, length);
158 if (this->IsResynchronizable())
161 const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
162 policy.CipherResynchronize(iv, ivLength);
165 m_leftOver = policy.GetBytesPerIteration();
168 template <
class BASE>
171 PolicyInterface &policy = this->AccessPolicy();
172 policy.CipherResynchronize(iv, this->ThrowIfInvalidIVLength(length));
173 m_leftOver = policy.GetBytesPerIteration();
176 template <
class BASE>
181 PolicyInterface &policy = this->AccessPolicy();
182 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
183 unsigned int alignment = policy.GetAlignment();
184 byte *reg = policy.GetRegisterBegin();
188 size_t len =
STDMIN(m_leftOver, length);
189 CombineMessageAndShiftRegister(outString, reg + bytesPerIteration - m_leftOver, inString, len);
201 if (policy.CanIterate() && length >= bytesPerIteration &&
IsAlignedOn(outString, alignment))
204 policy.Iterate(outString, inString,
GetCipherDir(*
this), length / bytesPerIteration);
207 memcpy(outString, inString, length);
208 policy.Iterate(outString, outString,
GetCipherDir(*
this), length / bytesPerIteration);
210 inString += length - length % bytesPerIteration;
211 outString += length - length % bytesPerIteration;
212 length %= bytesPerIteration;
215 while (length >= bytesPerIteration)
217 policy.TransformRegister();
218 CombineMessageAndShiftRegister(outString, reg, inString, bytesPerIteration);
219 length -= bytesPerIteration;
220 inString += bytesPerIteration;
221 outString += bytesPerIteration;
226 policy.TransformRegister();
227 CombineMessageAndShiftRegister(outString, reg, inString, length);
228 m_leftOver = bytesPerIteration - length;
232 template <
class BASE>
235 xorbuf(reg, message, length);
236 memcpy(output, reg, length);
239 template <
class BASE>
242 for (
unsigned int i=0; i<length; i++)
245 output[i] = reg[i] ^ b;
void Resynchronize(const byte *iv, int length=-1)
Resynchronize the cipher.
virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
Operates the keystream.
Base class for feedback based stream ciphers with SymmetricCipher interface.
virtual bool CanOperateKeystream() const
Flag indicating.
virtual void WriteKeystream(byte *keystream, size_t iterationCount)
Generate the keystream.
void Seek(lword position)
Seeks to a random position in the stream.
Policy object for additive stream ciphers.
bool IsAlignedOn(const void *ptr, unsigned int alignment)
Determines whether ptr is aligned to a minimum value.
void ProcessData(byte *outString, const byte *inString, size_t length)
Apply keystream to data.
Base class for feedback based stream ciphers in the reverse direction with SymmetricCipher interface...
void ProcessData(byte *outString, const byte *inString, size_t length)
Apply keystream to data.
virtual unsigned int GetAlignment() const
Provides data alignment requirements.
virtual unsigned int GetBytesPerIteration() const =0
Provides number of bytes operated upon during an iteration.
virtual void SeekToIteration(lword iterationCount)
Seeks to a random position in the stream.
CipherDir GetCipherDir(const T &obj)
Returns the direction the cipher is being operated.
Base class for feedback based stream ciphers in the forward direction with SymmetricCipher interface...
void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Classes for implementing stream ciphers.
void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
virtual void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
Resynchronize the cipher.
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
KeystreamOperation
Keystream operation flags.
Crypto++ library namespace.
void Resynchronize(const byte *iv, int length=-1)
Resynchronize the cipher.
Base class for additive stream ciphers with SymmetricCipher interface.
Interface for retrieving values given their names.