15 #ifndef CRYPTOPP_IMPORTS
20 #ifndef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
21 extern const char STRCIPHER_FNAME[] = __FILE__;
29 PolicyInterface &policy = this->AccessPolicy();
30 policy.CipherSetKey(params, key, length);
32 unsigned int bufferByteSize = policy.CanOperateKeystream() ? GetBufferByteSize(policy) :
RoundUpToMultipleOf(1024U, GetBufferByteSize(policy));
33 m_buffer.New(bufferByteSize);
35 if (this->IsResynchronizable())
38 const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
39 policy.CipherResynchronize(m_buffer, iv, ivLength);
48 const size_t len =
STDMIN(m_leftOver, length);
49 std::memcpy(outString,
PtrSub(KeystreamBufferEnd(), m_leftOver), len);
51 length -= len; m_leftOver -= len;
52 outString =
PtrAdd(outString, len);
53 if (!length) {
return;}
56 PolicyInterface &policy = this->AccessPolicy();
57 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
59 if (length >= bytesPerIteration)
61 const size_t iterations = length / bytesPerIteration;
62 policy.WriteKeystream(outString, iterations);
63 length -= iterations * bytesPerIteration;
64 outString =
PtrAdd(outString, iterations * bytesPerIteration);
70 size_t bufferIterations = bufferByteSize / bytesPerIteration;
72 policy.WriteKeystream(
PtrSub(KeystreamBufferEnd(), bufferByteSize), bufferIterations);
73 std::memcpy(outString,
PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
74 m_leftOver = bufferByteSize - length;
84 PolicyInterface &policy = this->AccessPolicy();
85 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
95 byte* savedOutString = outString;
96 size_t savedLength = length;
99 if (inString == outString)
103 m_tempOutString.New(length);
104 m_tempOutString.SetMark(0);
105 outString = m_tempOutString.BytePtr();
111 const size_t len =
STDMIN(m_leftOver, length);
112 xorbuf(outString, inString,
PtrSub(KeystreamBufferEnd(), m_leftOver), len);
114 inString =
PtrAdd(inString, len);
115 outString =
PtrAdd(outString, len);
116 length -= len; m_leftOver -= len;
121 std::memcpy(savedOutString, m_tempOutString.BytePtr(), savedLength);
125 const unsigned int alignment = policy.GetAlignment();
126 const bool inAligned =
IsAlignedOn(inString, alignment);
127 const bool outAligned =
IsAlignedOn(outString, alignment);
128 CRYPTOPP_UNUSED(inAligned); CRYPTOPP_UNUSED(outAligned);
130 if (policy.CanOperateKeystream() && length >= bytesPerIteration)
132 const size_t iterations = length / bytesPerIteration;
136 policy.OperateKeystream(operation, outString, inString, iterations);
138 inString =
PtrAdd(inString, iterations * bytesPerIteration);
139 outString =
PtrAdd(outString, iterations * bytesPerIteration);
140 length -= iterations * bytesPerIteration;
143 size_t bufferByteSize = m_buffer.size();
144 size_t bufferIterations = bufferByteSize / bytesPerIteration;
146 while (length >= bufferByteSize)
148 policy.WriteKeystream(m_buffer, bufferIterations);
149 xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
151 inString =
PtrAdd(inString, bufferByteSize);
152 outString =
PtrAdd(outString, bufferByteSize);
153 length -= bufferByteSize;
159 bufferIterations = bufferByteSize / bytesPerIteration;
161 policy.WriteKeystream(
PtrSub(KeystreamBufferEnd(), bufferByteSize), bufferIterations);
162 xorbuf(outString, inString,
PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
164 m_leftOver = bufferByteSize - length;
168 std::memcpy(savedOutString, m_tempOutString.BytePtr(), savedLength);
174 PolicyInterface &policy = this->AccessPolicy();
176 m_buffer.New(GetBufferByteSize(policy));
177 policy.CipherResynchronize(m_buffer, iv, this->ThrowIfInvalidIVLength(length));
180 template <
class BASE>
183 PolicyInterface &policy = this->AccessPolicy();
184 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
186 policy.SeekToIteration(position / bytesPerIteration);
187 position %= bytesPerIteration;
191 policy.WriteKeystream(
PtrSub(KeystreamBufferEnd(), bytesPerIteration), 1);
192 m_leftOver = bytesPerIteration -
static_cast<unsigned int>(position);
198 template <
class BASE>
201 PolicyInterface &policy = this->AccessPolicy();
202 policy.CipherSetKey(params, key, length);
204 if (this->IsResynchronizable())
207 const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
208 policy.CipherResynchronize(iv, ivLength);
211 m_leftOver = policy.GetBytesPerIteration();
214 template <
class BASE>
217 PolicyInterface &policy = this->AccessPolicy();
218 policy.CipherResynchronize(iv, this->ThrowIfInvalidIVLength(length));
219 m_leftOver = policy.GetBytesPerIteration();
222 template <
class BASE>
228 PolicyInterface &policy = this->AccessPolicy();
229 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
230 byte *reg = policy.GetRegisterBegin();
241 byte* savedOutString = outString;
242 size_t savedLength = length;
243 bool copyOut =
false;
245 if (inString == outString)
249 m_tempOutString.New(length);
250 m_tempOutString.SetMark(0);
251 outString = m_tempOutString.BytePtr();
257 const size_t len =
STDMIN(m_leftOver, length);
258 CombineMessageAndShiftRegister(outString,
PtrAdd(reg, bytesPerIteration - m_leftOver), inString, len);
260 inString =
PtrAdd(inString, len);
261 outString =
PtrAdd(outString, len);
262 m_leftOver -= len; length -= len;
267 std::memcpy(savedOutString, m_tempOutString.BytePtr(), savedLength);
271 const unsigned int alignment = policy.GetAlignment();
272 const bool inAligned =
IsAlignedOn(inString, alignment);
273 const bool outAligned =
IsAlignedOn(outString, alignment);
274 CRYPTOPP_UNUSED(inAligned); CRYPTOPP_UNUSED(outAligned);
276 if (policy.CanIterate() && length >= bytesPerIteration && outAligned)
279 policy.Iterate(outString, inString, cipherDir, length / bytesPerIteration);
281 const size_t remainder = length % bytesPerIteration;
282 inString =
PtrAdd(inString, length - remainder);
283 outString =
PtrAdd(outString, length - remainder);
287 while (length >= bytesPerIteration)
289 policy.TransformRegister();
290 CombineMessageAndShiftRegister(outString, reg, inString, bytesPerIteration);
292 inString =
PtrAdd(inString, bytesPerIteration);
293 outString =
PtrAdd(outString, bytesPerIteration);
294 length -= bytesPerIteration;
299 policy.TransformRegister();
300 CombineMessageAndShiftRegister(outString, reg, inString, length);
301 m_leftOver = bytesPerIteration - length;
305 std::memcpy(savedOutString, m_tempOutString.BytePtr(), savedLength);
308 template <
class BASE>
311 xorbuf(reg, message, length);
312 std::memcpy(output, reg, length);
315 template <
class BASE>
318 for (
size_t i=0; i<length; i++)
321 output[i] = reg[i] ^ b;