5 #ifndef CRYPTOPP_IMPORTS
9 NAMESPACE_BEGIN(CryptoPP)
14 PolicyInterface &policy = this->AccessPolicy();
15 policy.CipherSetKey(params, key, length);
17 unsigned int bufferByteSize = policy.CanOperateKeystream() ? GetBufferByteSize(policy) : RoundUpToMultipleOf(1024U, GetBufferByteSize(policy));
18 m_buffer.New(bufferByteSize);
20 if (this->IsResynchronizable())
23 const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
24 policy.CipherResynchronize(m_buffer, iv, ivLength);
33 size_t len = STDMIN(m_leftOver, length);
34 memcpy(outString, KeystreamBufferEnd()-m_leftOver, len);
42 assert(m_leftOver == 0);
44 PolicyInterface &policy = this->AccessPolicy();
45 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
47 if (length >= bytesPerIteration)
49 size_t iterations = length / bytesPerIteration;
50 policy.WriteKeystream(outString, iterations);
51 outString += iterations * bytesPerIteration;
52 length -= iterations * bytesPerIteration;
57 size_t bufferByteSize = RoundUpToMultipleOf(length, bytesPerIteration);
58 size_t bufferIterations = bufferByteSize / bytesPerIteration;
60 policy.WriteKeystream(KeystreamBufferEnd()-bufferByteSize, bufferIterations);
61 memcpy(outString, KeystreamBufferEnd()-bufferByteSize, length);
62 m_leftOver = bufferByteSize - length;
71 size_t len = STDMIN(m_leftOver, length);
72 xorbuf(outString, inString, KeystreamBufferEnd()-m_leftOver, len);
81 assert(m_leftOver == 0);
83 PolicyInterface &policy = this->AccessPolicy();
84 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
86 if (policy.CanOperateKeystream() && length >= bytesPerIteration)
88 size_t iterations = length / bytesPerIteration;
89 unsigned int alignment = policy.GetAlignment();
90 KeystreamOperation operation = KeystreamOperation((IsAlignedOn(inString, alignment) * 2) | (
int)IsAlignedOn(outString, alignment));
92 policy.OperateKeystream(operation, outString, inString, iterations);
94 inString += iterations * bytesPerIteration;
95 outString += iterations * bytesPerIteration;
96 length -= iterations * bytesPerIteration;
102 size_t bufferByteSize = m_buffer.size();
103 size_t bufferIterations = bufferByteSize / bytesPerIteration;
105 while (length >= bufferByteSize)
107 policy.WriteKeystream(m_buffer, bufferIterations);
108 xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
109 length -= bufferByteSize;
110 inString += bufferByteSize;
111 outString += bufferByteSize;
116 bufferByteSize = RoundUpToMultipleOf(length, bytesPerIteration);
117 bufferIterations = bufferByteSize / bytesPerIteration;
119 policy.WriteKeystream(KeystreamBufferEnd()-bufferByteSize, bufferIterations);
120 xorbuf(outString, inString, KeystreamBufferEnd()-bufferByteSize, length);
121 m_leftOver = bufferByteSize - length;
128 PolicyInterface &policy = this->AccessPolicy();
130 m_buffer.New(GetBufferByteSize(policy));
131 policy.CipherResynchronize(m_buffer, iv, this->ThrowIfInvalidIVLength(length));
134 template <
class BASE>
137 PolicyInterface &policy = this->AccessPolicy();
138 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
140 policy.SeekToIteration(position / bytesPerIteration);
141 position %= bytesPerIteration;
145 policy.WriteKeystream(KeystreamBufferEnd()-bytesPerIteration, 1);
146 m_leftOver = bytesPerIteration - (
unsigned int)position;
152 template <
class BASE>
155 PolicyInterface &policy = this->AccessPolicy();
156 policy.CipherSetKey(params, key, length);
158 if (this->IsResynchronizable())
161 const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
162 policy.CipherResynchronize(iv, ivLength);
165 m_leftOver = policy.GetBytesPerIteration();
168 template <
class BASE>
171 PolicyInterface &policy = this->AccessPolicy();
172 policy.CipherResynchronize(iv, this->ThrowIfInvalidIVLength(length));
173 m_leftOver = policy.GetBytesPerIteration();
176 template <
class BASE>
179 assert(length % this->MandatoryBlockSize() == 0);
181 PolicyInterface &policy = this->AccessPolicy();
182 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
183 unsigned int alignment = policy.GetAlignment();
184 byte *reg = policy.GetRegisterBegin();
188 size_t len = STDMIN(m_leftOver, length);
189 CombineMessageAndShiftRegister(outString, reg + bytesPerIteration - m_leftOver, inString, len);
199 assert(m_leftOver == 0);
201 if (policy.CanIterate() && length >= bytesPerIteration && IsAlignedOn(outString, alignment))
203 if (IsAlignedOn(inString, alignment))
204 policy.Iterate(outString, inString, GetCipherDir(*
this), length / bytesPerIteration);
207 memcpy(outString, inString, length);
208 policy.Iterate(outString, outString, GetCipherDir(*
this), length / bytesPerIteration);
210 inString += length - length % bytesPerIteration;
211 outString += length - length % bytesPerIteration;
212 length %= bytesPerIteration;
215 while (length >= bytesPerIteration)
217 policy.TransformRegister();
218 CombineMessageAndShiftRegister(outString, reg, inString, bytesPerIteration);
219 length -= bytesPerIteration;
220 inString += bytesPerIteration;
221 outString += bytesPerIteration;
226 policy.TransformRegister();
227 CombineMessageAndShiftRegister(outString, reg, inString, length);
228 m_leftOver = bytesPerIteration - length;
232 template <
class BASE>
235 xorbuf(reg, message, length);
236 memcpy(output, reg, length);
239 template <
class BASE>
242 for (
unsigned int i=0; i<length; i++)
245 output[i] = reg[i] ^ b;