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;}
46 PolicyInterface &policy = this->AccessPolicy();
47 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
49 if (length >= bytesPerIteration)
51 const size_t iterations = length / bytesPerIteration;
52 policy.WriteKeystream(outString, iterations);
53 length -= iterations * bytesPerIteration;
54 outString =
PtrAdd(outString, iterations * bytesPerIteration);
60 size_t bufferIterations = bufferByteSize / bytesPerIteration;
62 policy.WriteKeystream(
PtrSub(KeystreamBufferEnd(), bufferByteSize), bufferIterations);
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);
81 PolicyInterface &policy = this->AccessPolicy();
82 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
84 if (policy.CanOperateKeystream() && length >= bytesPerIteration)
86 const size_t iterations = length / bytesPerIteration;
87 unsigned int alignment = policy.GetAlignment();
89 policy.OperateKeystream(operation, outString, inString, iterations);
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)
101 policy.WriteKeystream(m_buffer, bufferIterations);
102 xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
104 length -= bufferByteSize;
105 inString =
PtrAdd(inString, bufferByteSize);
106 outString =
PtrAdd(outString, bufferByteSize);
112 bufferIterations = bufferByteSize / bytesPerIteration;
114 policy.WriteKeystream(
PtrSub(KeystreamBufferEnd(), bufferByteSize), bufferIterations);
115 xorbuf(outString, inString,
PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
116 m_leftOver = bufferByteSize - length;
123 PolicyInterface &policy = this->AccessPolicy();
125 m_buffer.New(GetBufferByteSize(policy));
126 policy.CipherResynchronize(m_buffer, iv, this->ThrowIfInvalidIVLength(length));
129 template <
class BASE>
132 PolicyInterface &policy = this->AccessPolicy();
133 word32 bytesPerIteration = policy.GetBytesPerIteration();
135 policy.SeekToIteration(position / bytesPerIteration);
136 position %= bytesPerIteration;
140 policy.WriteKeystream(
PtrSub(KeystreamBufferEnd(), bytesPerIteration), 1);
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;
Base class for additive stream ciphers with SymmetricCipher interface.
void ProcessData(byte *outString, const byte *inString, size_t length)
Apply keystream to data.
void Seek(lword position)
Seeks to a random position in the stream.
void Resynchronize(const byte *iv, int length=-1)
Resynchronize the cipher.
void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Base class for feedback based stream ciphers with SymmetricCipher interface.
void Resynchronize(const byte *iv, int length=-1)
Resynchronize the cipher.
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.
Base class for feedback based stream ciphers in the forward direction with SymmetricCipher interface.
Interface for retrieving values given their names.
CipherDir
Specifies a direction for a cipher to operate.
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
PTR PtrSub(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.
bool IsAlignedOn(const void *ptr, unsigned int alignment)
Determines whether ptr is aligned to a minimum value.
PTR PtrAdd(PTR pointer, OFF offset)
Create a pointer with an offset.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
CipherDir GetCipherDir(const T &obj)
Returns the direction the cipher is being operated.
Crypto++ library namespace.
Classes for implementing stream ciphers.
KeystreamOperation
Keystream operation flags.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.