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;}
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 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;}
83 PolicyInterface &policy = this->AccessPolicy();
84 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
86 if (policy.CanOperateKeystream() && length >= bytesPerIteration)
88 const size_t iterations = length / bytesPerIteration;
89 unsigned int alignment = policy.GetAlignment();
90 volatile int inAligned =
IsAlignedOn(inString, alignment) << 1;
91 volatile int outAligned =
IsAlignedOn(outString, alignment) << 0;
94 policy.OperateKeystream(operation, outString, inString, iterations);
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)
108 policy.WriteKeystream(m_buffer, bufferIterations);
109 xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
111 length -= bufferByteSize;
112 inString =
PtrAdd(inString, bufferByteSize);
113 outString =
PtrAdd(outString, bufferByteSize);
119 bufferIterations = bufferByteSize / bytesPerIteration;
121 policy.WriteKeystream(
PtrSub(KeystreamBufferEnd(), bufferByteSize), bufferIterations);
122 xorbuf(outString, inString,
PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
123 m_leftOver = bufferByteSize - length;
130 PolicyInterface &policy = this->AccessPolicy();
132 m_buffer.New(GetBufferByteSize(policy));
133 policy.CipherResynchronize(m_buffer, iv, this->ThrowIfInvalidIVLength(length));
136 template <
class BASE>
139 PolicyInterface &policy = this->AccessPolicy();
140 word32 bytesPerIteration = policy.GetBytesPerIteration();
142 policy.SeekToIteration(position / bytesPerIteration);
143 position %= bytesPerIteration;
147 policy.WriteKeystream(
PtrSub(KeystreamBufferEnd(), bytesPerIteration), 1);
148 m_leftOver = bytesPerIteration -
static_cast<word32
>(position);
154 template <
class BASE>
157 PolicyInterface &policy = this->AccessPolicy();
158 policy.CipherSetKey(params, key, length);
160 if (this->IsResynchronizable())
163 const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
164 policy.CipherResynchronize(iv, ivLength);
167 m_leftOver = policy.GetBytesPerIteration();
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;