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>
171 template <
class BASE>
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);
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)
247 CombineMessageAndShiftRegister(outString, reg, inString, bytesPerIteration);
248 length -= bytesPerIteration;
249 inString =
PtrAdd(inString, bytesPerIteration);
250 outString =
PtrAdd(outString, bytesPerIteration);
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;