5 #ifndef CRYPTOPP_IMPORTS 10 #ifndef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES 11 extern const char STRCIPHER_FNAME[] = __FILE__;
19 PolicyInterface &policy = this->AccessPolicy();
20 policy.CipherSetKey(params, key, length);
22 unsigned int bufferByteSize = policy.CanOperateKeystream() ? GetBufferByteSize(policy) :
RoundUpToMultipleOf(1024U, GetBufferByteSize(policy));
23 m_buffer.New(bufferByteSize);
25 if (this->IsResynchronizable())
28 const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
29 policy.CipherResynchronize(m_buffer, iv, ivLength);
38 const size_t len =
STDMIN(m_leftOver, length);
39 memcpy(outString,
PtrSub(KeystreamBufferEnd(), m_leftOver), len);
41 length -= len; m_leftOver -= len;
42 outString =
PtrAdd(outString, len);
43 if (!length) {
return;}
49 if (length >= bytesPerIteration)
51 const size_t iterations = length / bytesPerIteration;
53 length -= iterations * bytesPerIteration;
54 outString =
PtrAdd(outString, iterations * bytesPerIteration);
60 size_t bufferIterations = bufferByteSize / bytesPerIteration;
63 memcpy(outString,
PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
64 m_leftOver = bufferByteSize - length;
73 const size_t len =
STDMIN(m_leftOver, length);
74 xorbuf(outString, inString, KeystreamBufferEnd()-m_leftOver, len);
76 length -= len; m_leftOver -= len;
77 inString =
PtrAdd(inString, len);
78 outString =
PtrAdd(outString, len);
86 const size_t iterations = length / bytesPerIteration;
91 inString =
PtrAdd(inString, iterations * bytesPerIteration);
92 outString =
PtrAdd(outString, iterations * bytesPerIteration);
93 length -= iterations * bytesPerIteration;
96 size_t bufferByteSize = m_buffer.size();
97 size_t bufferIterations = bufferByteSize / bytesPerIteration;
99 while (length >= bufferByteSize)
102 xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
104 length -= bufferByteSize;
105 inString =
PtrAdd(inString, bufferByteSize);
106 outString =
PtrAdd(outString, bufferByteSize);
112 bufferIterations = bufferByteSize / bytesPerIteration;
115 xorbuf(outString, inString,
PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
116 m_leftOver = bufferByteSize - length;
125 m_buffer.New(GetBufferByteSize(policy));
129 template <
class BASE>
136 position %= bytesPerIteration;
141 m_leftOver = bytesPerIteration -
static_cast<word32
>(position);
147 template <
class BASE>
150 PolicyInterface &policy = this->AccessPolicy();
151 policy.CipherSetKey(params, key, length);
153 if (this->IsResynchronizable())
156 const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
157 policy.CipherResynchronize(iv, ivLength);
160 m_leftOver = policy.GetBytesPerIteration();
163 template <
class BASE>
166 PolicyInterface &policy = this->AccessPolicy();
167 policy.CipherResynchronize(iv, this->ThrowIfInvalidIVLength(length));
168 m_leftOver = policy.GetBytesPerIteration();
171 template <
class BASE>
177 PolicyInterface &policy = this->AccessPolicy();
178 word32 bytesPerIteration = policy.GetBytesPerIteration();
179 byte *reg = policy.GetRegisterBegin();
183 const size_t len =
STDMIN(m_leftOver, length);
184 CombineMessageAndShiftRegister(outString,
PtrAdd(reg, bytesPerIteration - m_leftOver), inString, len);
186 m_leftOver -= len; length -= len;
187 inString =
PtrAdd(inString, len);
188 outString =
PtrAdd(outString, len);
205 const unsigned int alignment = policy.GetAlignment();
206 volatile bool isAligned =
IsAlignedOn(outString, alignment);
207 if (policy.CanIterate() && length >= bytesPerIteration && isAligned)
212 policy.Iterate(outString, inString, cipherDir, length / bytesPerIteration);
235 memcpy(outString, inString, length);
236 policy.Iterate(outString, outString, cipherDir, length / bytesPerIteration);
238 const size_t remainder = length % bytesPerIteration;
239 inString =
PtrAdd(inString, length - remainder);
240 outString =
PtrAdd(outString, length - remainder);
244 while (length >= bytesPerIteration)
246 policy.TransformRegister();
247 CombineMessageAndShiftRegister(outString, reg, inString, bytesPerIteration);
248 length -= bytesPerIteration;
249 inString =
PtrAdd(inString, bytesPerIteration);
250 outString =
PtrAdd(outString, bytesPerIteration);
255 policy.TransformRegister();
256 CombineMessageAndShiftRegister(outString, reg, inString, length);
257 m_leftOver = bytesPerIteration - length;
261 template <
class BASE>
264 xorbuf(reg, message, length);
265 memcpy(output, reg, length);
268 template <
class BASE>
271 for (
size_t i=0; i<length; i++)
274 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.
CipherDir
Specifies a direction for a cipher to operate.
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.
PTR PtrSub(PTR pointer, OFF offset)
Create a pointer with an offset.
PTR PtrAdd(PTR pointer, OFF offset)
Create a pointer with an offset.
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.