60
60
virtual unsigned int GetAlignment() const =0;
61
61
virtual unsigned int GetBytesPerIteration() const =0;
62
62
virtual unsigned int GetIterationsToBuffer() const =0;
63
virtual void WriteKeystream(byte *keystreamBuffer, size_t iterationCount) =0;
63
virtual void WriteKeystream(byte *keystreamBuffer, unsigned int iterationCount) =0;
64
64
virtual bool CanOperateKeystream() const {return false;}
65
virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount) {assert(false);}
66
virtual void CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length) =0;
65
virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, unsigned int iterationCount) {assert(false);}
66
virtual void CipherSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length) =0;
67
67
virtual void CipherResynchronize(byte *keystreamBuffer, const byte *iv) {throw NotImplemented("StreamTransformation: this object doesn't support resynchronization");}
68
68
virtual bool IsRandomAccess() const =0;
69
69
virtual void SeekToIteration(lword iterationCount) {assert(!IsRandomAccess()); throw NotImplemented("StreamTransformation: this object doesn't support random access");}
77
77
unsigned int GetAlignment() const {return sizeof(WordType);}
78
78
unsigned int GetBytesPerIteration() const {return sizeof(WordType) * W;}
79
79
unsigned int GetIterationsToBuffer() const {return X;}
80
void WriteKeystream(byte *buffer, size_t iterationCount)
80
void WriteKeystream(byte *buffer, unsigned int iterationCount)
81
81
{OperateKeystream(WRITE_KEYSTREAM, buffer, NULL, iterationCount);}
82
82
bool CanOperateKeystream() const {return true;}
83
virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount) =0;
83
virtual void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, unsigned int iterationCount) =0;
86
86
struct KeystreamOutput
124
124
byte GenerateByte();
125
void ProcessData(byte *outString, const byte *inString, size_t length);
125
void ProcessData(byte *outString, const byte *inString, unsigned int length);
126
126
void Resynchronize(const byte *iv);
127
unsigned int OptimalBlockSize() const {return this->GetPolicy().GetBytesPerIteration();}
128
unsigned int GetOptimalNextBlockSize() const {return (unsigned int)this->m_leftOver;}
129
unsigned int OptimalDataAlignment() const {return this->GetPolicy().GetAlignment();}
127
unsigned int OptimalBlockSize() const {return GetPolicy().GetBytesPerIteration();}
128
unsigned int GetOptimalNextBlockSize() const {return m_leftOver;}
129
unsigned int OptimalDataAlignment() const {return GetPolicy().GetAlignment();}
130
130
bool IsSelfInverting() const {return true;}
131
131
bool IsForwardTransformation() const {return true;}
132
bool IsRandomAccess() const {return this->GetPolicy().IsRandomAccess();}
132
bool IsRandomAccess() const {return GetPolicy().IsRandomAccess();}
133
133
void Seek(lword position);
135
135
typedef typename BASE::PolicyInterface PolicyInterface;
154
158
virtual byte * GetRegisterBegin() =0;
155
159
virtual void TransformRegister() =0;
156
160
virtual bool CanIterate() const {return false;}
157
virtual void Iterate(byte *output, const byte *input, CipherDir dir, size_t iterationCount) {assert(false);}
158
virtual void CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length) =0;
161
virtual void Iterate(byte *output, const byte *input, CipherDir dir, unsigned int iterationCount) {assert(false);}
162
virtual void CipherSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length) =0;
159
163
virtual void CipherResynchronize(const byte *iv) {throw NotImplemented("StreamTransformation: this object doesn't support resynchronization");}
220
219
class CRYPTOPP_NO_VTABLE CFB_CipherTemplate : public BASE
223
void ProcessData(byte *outString, const byte *inString, size_t length);
222
void ProcessData(byte *outString, const byte *inString, unsigned int length);
224
223
void Resynchronize(const byte *iv);
225
unsigned int OptimalBlockSize() const {return this->GetPolicy().GetBytesPerIteration();}
226
unsigned int GetOptimalNextBlockSize() const {return (unsigned int)m_leftOver;}
227
unsigned int OptimalDataAlignment() const {return this->GetPolicy().GetAlignment();}
224
unsigned int OptimalBlockSize() const {return GetPolicy().GetBytesPerIteration();}
225
unsigned int GetOptimalNextBlockSize() const {return m_leftOver;}
226
unsigned int OptimalDataAlignment() const {return GetPolicy().GetAlignment();}
228
227
bool IsRandomAccess() const {return false;}
229
228
bool IsSelfInverting() const {return false;}
231
230
typedef typename BASE::PolicyInterface PolicyInterface;
234
virtual void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length) =0;
233
virtual void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, unsigned int length) =0;
236
235
void UncheckedSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length, const byte *iv);
237
unsigned int m_leftOver;
241
240
template <class BASE = AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >
242
241
class CRYPTOPP_NO_VTABLE CFB_EncryptionTemplate : public CFB_CipherTemplate<BASE>
244
243
bool IsForwardTransformation() const {return true;}
245
void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length);
244
void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, unsigned int length);
248
247
template <class BASE = AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >
249
248
class CRYPTOPP_NO_VTABLE CFB_DecryptionTemplate : public CFB_CipherTemplate<BASE>
251
250
bool IsForwardTransformation() const {return false;}
252
void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, size_t length);
255
template <class BASE>
256
class CFB_RequireFullDataBlocks : public BASE
259
unsigned int MandatoryBlockSize() const {return this->OptimalBlockSize();}
251
void CombineMessageAndShiftRegister(byte *output, byte *reg, const byte *message, unsigned int length);
254
CRYPTOPP_DLL_TEMPLATE_CLASS AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher>;
255
CRYPTOPP_DLL_TEMPLATE_CLASS CFB_CipherTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, SymmetricCipher> >;
256
CRYPTOPP_DLL_TEMPLATE_CLASS CFB_EncryptionTemplate<>;
257
CRYPTOPP_DLL_TEMPLATE_CLASS CFB_DecryptionTemplate<>;
263
259
template <class BASE, class INFO = BASE>
264
260
class SymmetricCipherFinal : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BASE, INFO>, INFO>
267
263
SymmetricCipherFinal() {}
268
264
SymmetricCipherFinal(const byte *key)
269
{SetKey(key, this->DEFAULT_KEYLENGTH);}
270
SymmetricCipherFinal(const byte *key, size_t length)
265
{SetKey(key, DEFAULT_KEYLENGTH);}
266
SymmetricCipherFinal(const byte *key, unsigned int length)
271
267
{SetKey(key, length);}
272
SymmetricCipherFinal(const byte *key, size_t length, const byte *iv)
273
{this->SetKeyWithIV(key, length, iv);}
268
SymmetricCipherFinal(const byte *key, unsigned int length, const byte *iv)
269
{SetKeyWithIV(key, length, iv);}
275
void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms = g_nullNameValuePairs)
271
void SetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms = g_nullNameValuePairs)
277
this->ThrowIfInvalidKeyLength(length);
278
this->UncheckedSetKey(params, key, (unsigned int)length, this->GetIVAndThrowIfInvalid(params));
273
ThrowIfInvalidKeyLength(length);
274
UncheckedSetKey(params, key, length, GetIVAndThrowIfInvalid(params));
281
277
Clonable * Clone() const {return static_cast<SymmetricCipher *>(new SymmetricCipherFinal<BASE, INFO>(*this));}
284
280
template <class S>
285
281
void AdditiveCipherTemplate<S>::UncheckedSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length, const byte *iv)
287
PolicyInterface &policy = this->AccessPolicy();
283
PolicyInterface &policy = AccessPolicy();
288
284
policy.CipherSetKey(params, key, length);
290
286
m_buffer.New(GetBufferByteSize(policy));
292
if (this->IsResynchronizable())
288
if (IsResynchronizable())
293
289
policy.CipherResynchronize(m_buffer, iv);
296
292
template <class BASE>
297
293
void CFB_CipherTemplate<BASE>::UncheckedSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length, const byte *iv)
299
PolicyInterface &policy = this->AccessPolicy();
295
PolicyInterface &policy = AccessPolicy();
300
296
policy.CipherSetKey(params, key, length);
302
if (this->IsResynchronizable())
298
if (IsResynchronizable())
303
299
policy.CipherResynchronize(iv);
305
301
m_leftOver = policy.GetBytesPerIteration();