5#ifndef CRYPTOPP_IMPORTS
10#ifndef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
11extern const char STRCIPHER_FNAME[] = __FILE__;
17void AdditiveCipherTemplate<S>::UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms)
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 size_t 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;
74 PolicyInterface &policy = this->AccessPolicy();
75 size_t bytesPerIteration = policy.GetBytesPerIteration();
79 const size_t len =
STDMIN(m_leftOver, length);
80 xorbuf(outString, inString,
PtrSub(KeystreamBufferEnd(), m_leftOver), len);
82 inString =
PtrAdd(inString, len);
83 outString =
PtrAdd(outString, len);
84 length -= len; m_leftOver -= len;
87 if (!length) {
return; }
89 const word32 alignment = policy.GetAlignment();
90 const bool inAligned =
IsAlignedOn(inString, alignment);
91 const bool outAligned =
IsAlignedOn(outString, alignment);
92 CRYPTOPP_UNUSED(inAligned); CRYPTOPP_UNUSED(outAligned);
94 if (policy.CanOperateKeystream() && length >= bytesPerIteration)
96 const size_t iterations = length / bytesPerIteration;
100 policy.OperateKeystream(operation, outString, inString, iterations);
102 inString =
PtrAdd(inString, iterations * bytesPerIteration);
103 outString =
PtrAdd(outString, iterations * bytesPerIteration);
104 length -= iterations * bytesPerIteration;
107 size_t bufferByteSize = m_buffer.size();
108 size_t bufferIterations = bufferByteSize / bytesPerIteration;
110 while (length >= bufferByteSize)
112 policy.WriteKeystream(m_buffer, bufferIterations);
113 xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
115 inString =
PtrAdd(inString, bufferByteSize);
116 outString =
PtrAdd(outString, bufferByteSize);
117 length -= bufferByteSize;
123 bufferIterations = bufferByteSize / bytesPerIteration;
125 policy.WriteKeystream(
PtrSub(KeystreamBufferEnd(), bufferByteSize), bufferIterations);
126 xorbuf(outString, inString,
PtrSub(KeystreamBufferEnd(), bufferByteSize), length);
128 m_leftOver = bufferByteSize - length;
135 PolicyInterface &policy = this->AccessPolicy();
137 m_buffer.New(GetBufferByteSize(policy));
138 policy.CipherResynchronize(m_buffer, iv, this->ThrowIfInvalidIVLength(length));
144 PolicyInterface &policy = this->AccessPolicy();
145 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
147 policy.SeekToIteration(position / bytesPerIteration);
148 position %= bytesPerIteration;
152 policy.WriteKeystream(
PtrSub(KeystreamBufferEnd(), bytesPerIteration), 1);
153 m_leftOver = bytesPerIteration -
static_cast<unsigned int>(position);
160void CFB_CipherTemplate<BASE>::UncheckedSetKey(
const byte *key,
unsigned int length,
const NameValuePairs ¶ms)
162 PolicyInterface &policy = this->AccessPolicy();
163 policy.CipherSetKey(params, key, length);
165 if (this->IsResynchronizable())
168 const byte *iv = this->GetIVAndThrowIfInvalid(params, ivLength);
169 policy.CipherResynchronize(iv, ivLength);
172 m_leftOver = policy.GetBytesPerIteration();
178 PolicyInterface &policy = this->AccessPolicy();
179 policy.CipherResynchronize(iv, this->ThrowIfInvalidIVLength(length));
180 m_leftOver = policy.GetBytesPerIteration();
189 PolicyInterface &policy = this->AccessPolicy();
190 unsigned int bytesPerIteration = policy.GetBytesPerIteration();
191 byte *reg = policy.GetRegisterBegin();
195 const size_t len =
STDMIN(m_leftOver, length);
196 CombineMessageAndShiftRegister(outString,
PtrAdd(reg, bytesPerIteration - m_leftOver), inString, len);
198 inString =
PtrAdd(inString, len);
199 outString =
PtrAdd(outString, len);
200 m_leftOver -= len; length -= len;
203 if (!length) {
return; }
205 const word32 alignment = policy.GetAlignment();
206 const bool inAligned =
IsAlignedOn(inString, alignment);
207 const bool outAligned =
IsAlignedOn(outString, alignment);
208 CRYPTOPP_UNUSED(inAligned); CRYPTOPP_UNUSED(outAligned);
210 if (policy.CanIterate() && length >= bytesPerIteration && outAligned)
213 policy.Iterate(outString, inString, cipherDir, length / bytesPerIteration);
215 const size_t remainder = length % bytesPerIteration;
216 inString =
PtrAdd(inString, length - remainder);
217 outString =
PtrAdd(outString, length - remainder);
221 while (length >= bytesPerIteration)
223 policy.TransformRegister();
224 CombineMessageAndShiftRegister(outString, reg, inString, bytesPerIteration);
226 inString =
PtrAdd(inString, bytesPerIteration);
227 outString =
PtrAdd(outString, bytesPerIteration);
228 length -= bytesPerIteration;
233 policy.TransformRegister();
234 CombineMessageAndShiftRegister(outString, reg, inString, length);
235 m_leftOver = bytesPerIteration - length;
240void CFB_EncryptionTemplate<BASE>::CombineMessageAndShiftRegister(
byte *output,
byte *reg,
const byte *message,
size_t length)
242 xorbuf(reg, message, length);
243 std::memcpy(output, reg, length);
247void CFB_DecryptionTemplate<BASE>::CombineMessageAndShiftRegister(
byte *output,
byte *reg,
const byte *message,
size_t length)
249 for (
size_t i=0; i<length; i++)
252 output[i] = reg[i] ^ b;
void ProcessData(byte *outString, const byte *inString, size_t length)
Apply keystream to data.
void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
void Seek(lword position)
Seeks to a random position in the stream.
void Resynchronize(const byte *iv, int length=-1)
Resynchronize the cipher.
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.
Interface for retrieving values given their names.
unsigned int word32
32-bit unsigned datatype
word64 lword
Large word type.
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.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
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.
#define EnumToInt(v)
Integer value.
CipherDir GetCipherDir(const T &obj)
Returns the direction the cipher is being operated.
CRYPTOPP_DLL void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
Crypto++ library namespace.
Classes for implementing stream ciphers.
KeystreamOperation
Keystream operation flags.
KeystreamOperationFlags
Keystream operation flags.
@ INPUT_ALIGNED
Input buffer is aligned.
@ OUTPUT_ALIGNED
Output buffer is aligned.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.