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 std::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 std::memcpy(outString,
PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
64 m_leftOver = bufferByteSize - length;
73 const size_t len =
STDMIN(m_leftOver, length);
74 xorbuf(outString, inString,
PtrSub(KeystreamBufferEnd(), m_leftOver), len);
76 length -= len; m_leftOver -= len;
77 inString =
PtrAdd(inString, len);
78 outString =
PtrAdd(outString, len);
80 if (!length) {
return;}
88 const size_t iterations = length / bytesPerIteration;
90 volatile int inAligned =
IsAlignedOn(inString, alignment) << 1;
91 volatile int outAligned =
IsAlignedOn(outString, alignment) << 0;
96 inString =
PtrAdd(inString, iterations * bytesPerIteration);
97 outString =
PtrAdd(outString, iterations * bytesPerIteration);
98 length -= iterations * bytesPerIteration;
100 if (!length) {
return;}
103 size_t bufferByteSize = m_buffer.size();
104 size_t bufferIterations = bufferByteSize / bytesPerIteration;
106 while (length >= bufferByteSize)
109 xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
111 length -= bufferByteSize;
112 inString =
PtrAdd(inString, bufferByteSize);
113 outString =
PtrAdd(outString, bufferByteSize);
119 bufferIterations = bufferByteSize / bytesPerIteration;
122 xorbuf(outString, inString,
PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
123 m_leftOver = bufferByteSize - length;
132 m_buffer.New(GetBufferByteSize(policy));
136 template <
class BASE>
143 position %= bytesPerIteration;
148 m_leftOver = bytesPerIteration -
static_cast<word32
>(position);
154 template <
class BASE>
160 if (this->IsResynchronizable())
163 const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
170 template <
class BASE>
173 PolicyInterface &policy = this->AccessPolicy();
174 policy.CipherResynchronize(iv, this->ThrowIfInvalidIVLength(length));
175 m_leftOver = policy.GetBytesPerIteration();
178 template <
class BASE>
184 PolicyInterface &policy = this->AccessPolicy();
185 word32 bytesPerIteration = policy.GetBytesPerIteration();
186 byte *reg = policy.GetRegisterBegin();
190 const size_t len =
STDMIN(m_leftOver, length);
191 CombineMessageAndShiftRegister(outString,
PtrAdd(reg, bytesPerIteration - m_leftOver), inString, len);
193 m_leftOver -= len; length -= len;
194 inString =
PtrAdd(inString, len);
195 outString =
PtrAdd(outString, len);
198 if (!length) {
return;}
209 const unsigned int alignment = policy.GetAlignment();
210 volatile bool inAligned =
IsAlignedOn(inString, alignment);
211 volatile bool outAligned =
IsAlignedOn(outString, alignment);
213 if (policy.CanIterate() && length >= bytesPerIteration && outAligned)
217 policy.Iterate(outString, inString, cipherDir, length / bytesPerIteration);
240 std::memcpy(outString, inString, length);
241 policy.Iterate(outString, outString, cipherDir, length / bytesPerIteration);
243 const size_t remainder = length % bytesPerIteration;
244 inString =
PtrAdd(inString, length - remainder);
245 outString =
PtrAdd(outString, length - remainder);
249 while (length >= bytesPerIteration)
251 policy.TransformRegister();
252 CombineMessageAndShiftRegister(outString, reg, inString, bytesPerIteration);
253 length -= bytesPerIteration;
254 inString =
PtrAdd(inString, bytesPerIteration);
255 outString =
PtrAdd(outString, bytesPerIteration);
260 policy.TransformRegister();
261 CombineMessageAndShiftRegister(outString, reg, inString, length);
262 m_leftOver = bytesPerIteration - length;
266 template <
class BASE>
269 xorbuf(reg, message, length);
270 std::memcpy(output, reg, length);
273 template <
class BASE>
276 for (
size_t i=0; i<length; i++)
279 output[i] = reg[i] ^ b;
void Resynchronize(const byte *iv, int length=-1)
Resynchronize the cipher.
virtual void CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length)=0
Key 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.
virtual void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
Resynchronize the cipher.
CRYPTOPP_DLL void CRYPTOPP_API xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
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.