5 #ifndef CRYPTOPP_IMPORTS 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);
45 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
47 if (length >= bytesPerIteration)
49 size_t iterations = length / bytesPerIteration;
50 policy.WriteKeystream(outString, iterations);
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);
84 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
86 if (policy.CanOperateKeystream() && length >= bytesPerIteration)
88 size_t iterations = length / bytesPerIteration;
89 unsigned int alignment = policy.GetAlignment();
92 policy.OperateKeystream(operation, outString, inString, iterations);
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)
107 policy.WriteKeystream(m_buffer, bufferIterations);
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));
131 policy.CipherResynchronize(m_buffer, iv, this->ThrowIfInvalidIVLength(length));
134 template <
class BASE>
138 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
140 policy.SeekToIteration(position / bytesPerIteration);
141 position %= bytesPerIteration;
145 policy.WriteKeystream(KeystreamBufferEnd()-bytesPerIteration, 1);
146 m_leftOver = bytesPerIteration - (
unsigned int)position;
152 template <
class BASE>
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>
172 policy.CipherResynchronize(iv, this->ThrowIfInvalidIVLength(length));
173 m_leftOver = policy.GetBytesPerIteration();
176 template <
class BASE>
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;
BASE::PolicyInterface PolicyInterface
void Resynchronize(const byte *iv, int length=-1)
Resynchronize the cipher.
#define NAMESPACE_BEGIN(x)
void Seek(lword position)
Seeks to a random position in the stream.
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.
void ProcessData(byte *outString, const byte *inString, size_t length)
Apply keystream to data.
BASE::PolicyInterface PolicyInterface
CipherDir GetCipherDir(const T &obj)
Returns the direction the cipher is being operated.
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)
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)
Classes for implementing stream ciphers.
void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length)
void * memcpy(void *a, const void *b, size_t c)
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
KeystreamOperation
Keystream operation flags.
void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length)
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.