37
37
size_t GetValidKeyLength(size_t n) const {return m_cipher->GetValidKeyLength(n);}
38
38
bool IsValidKeyLength(size_t n) const {return m_cipher->IsValidKeyLength(n);}
40
void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms = g_nullNameValuePairs);
42
unsigned int OptimalDataAlignment() const {return BlockSize();}
40
unsigned int OptimalDataAlignment() const {return m_cipher->OptimalDataAlignment();}
44
42
unsigned int IVSize() const {return BlockSize();}
45
void GetNextIV(byte *IV);
46
43
virtual IV_Requirement IVRequirement() const =0;
45
void SetCipher(BlockCipher &cipher)
47
this->ThrowIfResynchronizable();
48
this->m_cipher = &cipher;
49
this->ResizeBuffers();
52
void SetCipherWithIV(BlockCipher &cipher, const byte *iv, int feedbackSize = 0)
54
this->ThrowIfInvalidIV(iv);
55
this->m_cipher = &cipher;
56
this->ResizeBuffers();
57
this->SetFeedbackSize(feedbackSize);
58
if (this->IsResynchronizable())
59
this->Resynchronize(iv);
63
CipherModeBase() : m_cipher(NULL) {}
49
64
inline unsigned int BlockSize() const {assert(m_register.size() > 0); return (unsigned int)m_register.size();}
50
65
virtual void SetFeedbackSize(unsigned int feedbackSize)
57
72
m_register.New(m_cipher->BlockSize());
59
virtual void UncheckedSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length, const byte *iv) =0;
61
75
BlockCipher *m_cipher;
62
SecByteBlock m_register;
76
AlignedSecByteBlock m_register;
65
79
template <class POLICY_INTERFACE>
66
80
class CRYPTOPP_NO_VTABLE ModePolicyCommonTemplate : public CipherModeBase, public POLICY_INTERFACE
68
unsigned int GetAlignment() const {return m_cipher->BlockAlignment();}
82
unsigned int GetAlignment() const {return m_cipher->OptimalDataAlignment();}
69
83
void CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length);
88
102
unsigned int GetBytesPerIteration() const {return m_feedbackSize;}
89
103
byte * GetRegisterBegin() {return m_register + BlockSize() - m_feedbackSize;}
90
void TransformRegister()
92
m_cipher->ProcessBlock(m_register, m_temp);
93
unsigned int updateSize = BlockSize()-m_feedbackSize;
94
memmove_s(m_register, m_register.size(), m_register+m_feedbackSize, updateSize);
95
memcpy_s(m_register+updateSize, m_register.size()-updateSize, m_temp, m_feedbackSize);
97
void CipherResynchronize(const byte *iv)
99
memcpy_s(m_register, m_register.size(), iv, BlockSize());
102
void SetFeedbackSize(unsigned int feedbackSize)
104
if (feedbackSize > BlockSize())
105
throw InvalidArgument("CFB_Mode: invalid feedback size");
106
m_feedbackSize = feedbackSize ? feedbackSize : BlockSize();
110
CipherModeBase::ResizeBuffers();
111
m_temp.New(BlockSize());
104
bool CanIterate() const {return m_feedbackSize == BlockSize();}
105
void Iterate(byte *output, const byte *input, CipherDir dir, size_t iterationCount);
106
void TransformRegister();
107
void CipherResynchronize(const byte *iv, size_t length);
108
void SetFeedbackSize(unsigned int feedbackSize);
109
void ResizeBuffers();
114
111
SecByteBlock m_temp;
115
112
unsigned int m_feedbackSize;
126
123
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE OFB_ModePolicy : public ModePolicyCommonTemplate<AdditiveCipherAbstractPolicy>
129
bool IsRandomAccess() const {return false;}
130
IV_Requirement IVRequirement() const {return STRUCTURED_IV;}
126
bool CipherIsRandomAccess() const {return false;}
127
IV_Requirement IVRequirement() const {return UNIQUE_IV;}
131
128
static const char * CRYPTOPP_API StaticAlgorithmName() {return "OFB";}
134
131
unsigned int GetBytesPerIteration() const {return BlockSize();}
135
unsigned int GetIterationsToBuffer() const {return 1;}
136
void WriteKeystream(byte *keystreamBuffer, size_t iterationCount)
138
assert(iterationCount == 1);
139
m_cipher->ProcessBlock(keystreamBuffer);
140
memcpy_s(m_register, m_register.size(), keystreamBuffer, BlockSize());
142
void CipherResynchronize(byte *keystreamBuffer, const byte *iv)
144
CopyOrZero(keystreamBuffer, iv, BlockSize());
132
unsigned int GetIterationsToBuffer() const {return m_cipher->OptimalNumberOfParallelBlocks();}
133
void WriteKeystream(byte *keystreamBuffer, size_t iterationCount);
134
void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
148
137
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CTR_ModePolicy : public ModePolicyCommonTemplate<AdditiveCipherAbstractPolicy>
151
bool IsRandomAccess() const {return true;}
152
IV_Requirement IVRequirement() const {return STRUCTURED_IV;}
153
void GetNextIV(byte *IV);
140
bool CipherIsRandomAccess() const {return true;}
141
IV_Requirement IVRequirement() const {return RANDOM_IV;}
154
142
static const char * CRYPTOPP_API StaticAlgorithmName() {return "CTR";}
145
virtual void IncrementCounterBy256();
147
unsigned int GetAlignment() const {return m_cipher->OptimalDataAlignment();}
157
148
unsigned int GetBytesPerIteration() const {return BlockSize();}
158
149
unsigned int GetIterationsToBuffer() const {return m_cipher->OptimalNumberOfParallelBlocks();}
159
150
void WriteKeystream(byte *buffer, size_t iterationCount)
160
151
{OperateKeystream(WRITE_KEYSTREAM, buffer, NULL, iterationCount);}
161
152
bool CanOperateKeystream() const {return true;}
162
153
void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
163
void CipherResynchronize(byte *keystreamBuffer, const byte *iv);
154
void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
164
155
void SeekToIteration(lword iterationCount);
166
inline void ProcessMultipleBlocks(byte *output, const byte *input, size_t n);
168
SecByteBlock m_counterArray;
157
AlignedSecByteBlock m_counterArray;
171
160
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE BlockOrientedCipherModeBase : public CipherModeBase
174
void UncheckedSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length, const byte *iv);
163
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms);
175
164
unsigned int MandatoryBlockSize() const {return BlockSize();}
176
165
bool IsRandomAccess() const {return false;}
177
166
bool IsSelfInverting() const {return false;}
178
167
bool IsForwardTransformation() const {return m_cipher->IsForwardTransformation();}
179
void Resynchronize(const byte *iv) {memcpy_s(m_register, m_register.size(), iv, BlockSize());}
180
void ProcessData(byte *outString, const byte *inString, size_t length);
168
void Resynchronize(const byte *iv, int length=-1) {memcpy_s(m_register, m_register.size(), iv, ThrowIfInvalidIVLength(length));}
183
171
bool RequireAlignedInput() const {return true;}
184
virtual void ProcessBlocks(byte *outString, const byte *inString, size_t numberOfBlocks) =0;
185
172
void ResizeBuffers()
187
174
CipherModeBase::ResizeBuffers();
194
181
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE ECB_OneWay : public BlockOrientedCipherModeBase
184
void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms = g_nullNameValuePairs)
185
{m_cipher->SetKey(key, length, params); BlockOrientedCipherModeBase::ResizeBuffers();}
197
186
IV_Requirement IVRequirement() const {return NOT_RESYNCHRONIZABLE;}
198
187
unsigned int OptimalBlockSize() const {return BlockSize() * m_cipher->OptimalNumberOfParallelBlocks();}
199
void ProcessBlocks(byte *outString, const byte *inString, size_t numberOfBlocks)
200
{m_cipher->ProcessAndXorMultipleBlocks(inString, NULL, outString, numberOfBlocks);}
188
void ProcessData(byte *outString, const byte *inString, size_t length);
201
189
static const char * CRYPTOPP_API StaticAlgorithmName() {return "ECB";}
225
213
static const char * CRYPTOPP_API StaticAlgorithmName() {return "CBC/CTS";}
228
void UncheckedSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length, const byte *iv)
216
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs ¶ms)
230
CBC_Encryption::UncheckedSetKey(params, key, length, iv);
218
CBC_Encryption::UncheckedSetKey(key, length, params);
231
219
m_stolenIV = params.GetValueWithDefault(Name::StolenIV(), (byte *)NULL);
273
261
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv)
275
263
this->m_cipher = &this->m_object;
276
this->SetKey(key, length, MakeParameters(Name::IV(), iv));
264
this->SetKey(key, length, MakeParameters(Name::IV(), ConstByteArrayParameter(iv, this->m_cipher->BlockSize())));
278
266
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv, int feedbackSize)
280
268
this->m_cipher = &this->m_object;
281
this->SetKey(key, length, MakeParameters(Name::IV(), iv)(Name::FeedbackSize(), feedbackSize));
269
this->SetKey(key, length, MakeParameters(Name::IV(), ConstByteArrayParameter(iv, this->m_cipher->BlockSize()))(Name::FeedbackSize(), feedbackSize));
284
272
static std::string CRYPTOPP_API StaticAlgorithmName()
293
281
CipherModeFinalTemplate_ExternalCipher() {}
294
282
CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher)
283
{this->SetCipher(cipher);}
296
284
CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher, const byte *iv, int feedbackSize = 0)
297
{SetCipherWithIV(cipher, iv, feedbackSize);}
285
{this->SetCipherWithIV(cipher, iv, feedbackSize);}
299
void SetCipher(BlockCipher &cipher);
300
void SetCipherWithIV(BlockCipher &cipher, const byte *iv, int feedbackSize = 0);
287
std::string AlgorithmName() const
288
{return (this->m_cipher ? this->m_cipher->AlgorithmName() + "/" : std::string("")) + BASE::StaticAlgorithmName();}
303
template <class BASE>
304
void CipherModeFinalTemplate_ExternalCipher<BASE>::SetCipher(BlockCipher &cipher)
306
this->ThrowIfResynchronizable();
307
this->m_cipher = &cipher;
308
this->ResizeBuffers();
311
template <class BASE>
312
void CipherModeFinalTemplate_ExternalCipher<BASE>::SetCipherWithIV(BlockCipher &cipher, const byte *iv, int feedbackSize)
314
this->ThrowIfInvalidIV(iv);
315
this->m_cipher = &cipher;
316
this->ResizeBuffers();
317
this->SetFeedbackSize(feedbackSize);
318
if (this->IsResynchronizable())
319
this->Resynchronize(iv);
322
291
CRYPTOPP_DLL_TEMPLATE_CLASS CFB_CipherTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
323
292
CRYPTOPP_DLL_TEMPLATE_CLASS CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
324
293
CRYPTOPP_DLL_TEMPLATE_CLASS CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;