Crypto++ 8.4 Free C++ class library of cryptographic schemes

modes.h
Go to the documentation of this file.
1 // modes.h - originally written and placed in the public domain by Wei Dai
2
3 /// \file modes.h
4 /// \brief Classes for block cipher modes of operation
5
6 #ifndef CRYPTOPP_MODES_H
7 #define CRYPTOPP_MODES_H
8
9 #include "cryptlib.h"
10 #include "secblock.h"
11 #include "misc.h"
12 #include "strciphr.h"
13 #include "argnames.h"
14 #include "algparam.h"
15
16 // Issue 340
17 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
18 # pragma GCC diagnostic push
19 # pragma GCC diagnostic ignored "-Wconversion"
20 # pragma GCC diagnostic ignored "-Wsign-conversion"
21 #endif
22
23 #if CRYPTOPP_MSC_VERSION
24 # pragma warning(push)
25 # pragma warning(disable: 4231 4275)
26 # if (CRYPTOPP_MSC_VERSION >= 1400)
27 # pragma warning(disable: 6011 6386 28193)
28 # endif
29 #endif
30
31 NAMESPACE_BEGIN(CryptoPP)
32
33 /// \brief Block cipher mode of operation information
34 /// \details Each class derived from this one defines two types, Encryption and Decryption,
35 /// both of which implement the SymmetricCipher interface.
36 /// For each mode there are two classes, one of which is a template class,
37 /// and the other one has a name that ends in "_ExternalCipher".
38 /// The "external cipher" mode objects hold a reference to the underlying block cipher,
39 /// instead of holding an instance of it. The reference must be passed in to the constructor.
40 /// For the "cipher holder" classes, the CIPHER template parameter should be a class
41 /// derived from BlockCipherDocumentation, for example DES or AES.
42 /// \details See NIST SP 800-38A for definitions of these modes. See
43 /// AuthenticatedSymmetricCipherDocumentation for authenticated encryption modes.
44 struct CipherModeDocumentation : public SymmetricCipherDocumentation
45 {
46 };
47
48 /// \brief Block cipher mode of operation information
49 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CipherModeBase : public SymmetricCipher
50 {
51 public:
52 virtual ~CipherModeBase() {}
53
54 // Algorithm class
55 std::string AlgorithmProvider() const {
56 return m_cipher != NULLPTR ? m_cipher->AlgorithmProvider() : "C++";
57 }
58
59 /// \brief Returns smallest valid key length
60 /// \return the minimum key length, in bytes
61 size_t MinKeyLength() const {return m_cipher->MinKeyLength();}
62
63 /// \brief Returns largest valid key length
64 /// \return the maximum key length, in bytes
65 size_t MaxKeyLength() const {return m_cipher->MaxKeyLength();}
66
67 /// \brief Returns default key length
68 /// \return the default key length, in bytes
69 size_t DefaultKeyLength() const {return m_cipher->DefaultKeyLength();}
70
71 /// \brief Returns a valid key length for the algorithm
72 /// \param keylength the size of the key, in bytes
73 /// \return the valid key length, in bytes
74 /// \details keylength is provided in bytes, not bits. If keylength is less than MIN_KEYLENGTH,
75 /// then the function returns MIN_KEYLENGTH. If keylength is greater than MAX_KEYLENGTH,
76 /// then the function returns MAX_KEYLENGTH. if If keylength is a multiple of KEYLENGTH_MULTIPLE,
77 /// then keylength is returned. Otherwise, the function returns a \a lower multiple of
78 /// KEYLENGTH_MULTIPLE.
79 size_t GetValidKeyLength(size_t keylength) const {return m_cipher->GetValidKeyLength(keylength);}
80
81 /// \brief Returns whether keylength is a valid key length
82 /// \param keylength the requested keylength
83 /// \return true if keylength is valid, false otherwise
84 /// \details Internally the function calls GetValidKeyLength()
85 bool IsValidKeyLength(size_t keylength) const {return m_cipher->IsValidKeyLength(keylength);}
86
87 /// \brief Provides input and output data alignment for optimal performance.
88 /// \return the input data alignment that provides optimal performance
89 /// \sa GetAlignment() and OptimalBlockSize()
90 unsigned int OptimalDataAlignment() const {return m_cipher->OptimalDataAlignment();}
91
92 /// \brief Returns length of the IV accepted by this object
93 /// \return the size of an IV, in bytes
94 /// \throw NotImplemented() if the object does not support resynchronization
95 /// \details The default implementation throws NotImplemented
96 unsigned int IVSize() const {return BlockSize();}
97
98 /// \brief Minimal requirement for secure IVs
99 /// \return the secure IV requirement of the algorithm
100 virtual IV_Requirement IVRequirement() const =0;
101
102 /// \brief Set external block cipher
103 /// \param cipher An external block cipher
104 /// \details The cipher should be keyed.
105 void SetCipher(BlockCipher &cipher)
106 {
107 this->ThrowIfResynchronizable();
108 this->m_cipher = &cipher;
109 this->ResizeBuffers();
110 }
111
112 /// \brief Set external block cipher and IV
113 /// \param cipher An external block cipher
114 /// \param iv a byte array used to resynchronize the cipher
115 /// \param feedbackSize the feedback size, in bytes
116 /// \details The cipher should be keyed.
117 void SetCipherWithIV(BlockCipher &cipher, const byte *iv, int feedbackSize = 0)
118 {
119 this->ThrowIfInvalidIV(iv);
120 this->m_cipher = &cipher;
121 this->ResizeBuffers();
122 this->SetFeedbackSize(feedbackSize);
123 if (this->IsResynchronizable())
124 this->Resynchronize(iv);
125 }
126
127 protected:
128 CipherModeBase() : m_cipher(NULLPTR) {}
129 inline unsigned int BlockSize() const
130 {
131 CRYPTOPP_ASSERT(m_register.size() > 0);
132 return static_cast<unsigned int>(m_register.size());
133 }
134 virtual void SetFeedbackSize(unsigned int feedbackSize)
135 {
136 if (!(feedbackSize == 0 || feedbackSize == BlockSize()))
137 throw InvalidArgument("CipherModeBase: feedback size cannot be specified for this cipher mode");
138 }
139
140 virtual void ResizeBuffers();
141
142 BlockCipher *m_cipher;
143 SecByteBlock m_register;
144 };
145
146 /// \brief Block cipher mode of operation common operations
147 /// \tparam POLICY_INTERFACE common operations
148 template <class POLICY_INTERFACE>
149 class CRYPTOPP_NO_VTABLE ModePolicyCommonTemplate : public CipherModeBase, public POLICY_INTERFACE
150 {
151 unsigned int GetAlignment() const {return m_cipher->OptimalDataAlignment();}
152 void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
153 };
154
155 template <class POLICY_INTERFACE>
156 void ModePolicyCommonTemplate<POLICY_INTERFACE>::CipherSetKey(const NameValuePairs &params, const byte *key, size_t length)
157 {
158 m_cipher->SetKey(key, length, params);
159 ResizeBuffers();
160 int feedbackSize = params.GetIntValueWithDefault(Name::FeedbackSize(), 0);
161 SetFeedbackSize(feedbackSize);
162 }
163
164 /// \brief CFB block cipher mode of operation
165 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CFB_ModePolicy : public ModePolicyCommonTemplate<CFB_CipherAbstractPolicy>
166 {
167 public:
168 CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "CFB";}
169
170 virtual ~CFB_ModePolicy() {}
171 CFB_ModePolicy() : m_feedbackSize(0) {}
172 IV_Requirement IVRequirement() const {return RANDOM_IV;}
173
174 protected:
175 unsigned int GetBytesPerIteration() const {return m_feedbackSize;}
176 bool CanIterate() const {return m_feedbackSize == BlockSize();}
177 void Iterate(byte *output, const byte *input, CipherDir dir, size_t iterationCount);
178 void TransformRegister();
179 void CipherResynchronize(const byte *iv, size_t length);
180 void SetFeedbackSize(unsigned int feedbackSize);
181 void ResizeBuffers();
182 byte * GetRegisterBegin();
183
184 SecByteBlock m_temp;
185 unsigned int m_feedbackSize;
186 };
187
188 /// \brief Initialize a block of memory
189 /// \param dest the destination block of memory
190 /// \param dsize the size of the destination block, in bytes
191 /// \param src the source block of memory
192 /// \param ssize the size of the source block, in bytes
193 /// \details CopyOrZero copies ssize bytes from source to destination if
194 /// src is not NULL. If src is NULL then dest is zero'd. Bounds are not
195 /// checked at runtime. Debug builds assert if ssize exceeds dsize.
196 inline void CopyOrZero(void *dest, size_t dsize, const void *src, size_t ssize)
197 {
198 CRYPTOPP_ASSERT(dest);
199 CRYPTOPP_ASSERT(dsize >= ssize);
200
201 if (src != NULLPTR)
202 memcpy_s(dest, dsize, src, ssize);
203 else
204 memset(dest, 0, dsize);
205 }
206
207 /// \brief OFB block cipher mode of operation
208 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE OFB_ModePolicy : public ModePolicyCommonTemplate<AdditiveCipherAbstractPolicy>
209 {
210 public:
211 CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "OFB";}
212
213 bool CipherIsRandomAccess() const {return false;}
214 IV_Requirement IVRequirement() const {return UNIQUE_IV;}
215
216 protected:
217 unsigned int GetBytesPerIteration() const {return BlockSize();}
218 unsigned int GetIterationsToBuffer() const {return m_cipher->OptimalNumberOfParallelBlocks();}
219 void WriteKeystream(byte *keystreamBuffer, size_t iterationCount);
220 void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
221 };
222
223 /// \brief CTR block cipher mode of operation
224 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CTR_ModePolicy : public ModePolicyCommonTemplate<AdditiveCipherAbstractPolicy>
225 {
226 public:
227 CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "CTR";}
228
229 virtual ~CTR_ModePolicy() {}
230 bool CipherIsRandomAccess() const {return true;}
231 IV_Requirement IVRequirement() const {return RANDOM_IV;}
232
233 protected:
234 virtual void IncrementCounterBy256();
235 unsigned int GetAlignment() const {return m_cipher->OptimalDataAlignment();}
236 unsigned int GetBytesPerIteration() const {return BlockSize();}
237 unsigned int GetIterationsToBuffer() const {return m_cipher->OptimalNumberOfParallelBlocks();}
238 void WriteKeystream(byte *buffer, size_t iterationCount)
239 {OperateKeystream(WRITE_KEYSTREAM, buffer, NULLPTR, iterationCount);}
240 bool CanOperateKeystream() const {return true;}
241 void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
242 void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
243 void SeekToIteration(lword iterationCount);
244
245 // adv_simd.h increments the counter
246 mutable SecByteBlock m_counterArray;
247 };
248
249 /// \brief Block cipher mode of operation default implementation
250 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE BlockOrientedCipherModeBase : public CipherModeBase
251 {
252 public:
253 virtual ~BlockOrientedCipherModeBase() {}
254 void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
255 unsigned int MandatoryBlockSize() const {return BlockSize();}
256 bool IsRandomAccess() const {return false;}
257 bool IsSelfInverting() const {return false;}
258 bool IsForwardTransformation() const
259 {return m_cipher->IsForwardTransformation();}
260 void Resynchronize(const byte *iv, int length=-1)
261 {memcpy_s(m_register, m_register.size(), iv, ThrowIfInvalidIVLength(length));}
262
263 protected:
264 bool RequireAlignedInput() const {return true;}
265 virtual void ResizeBuffers();
266
267 SecByteBlock m_buffer;
268 };
269
270 /// \brief ECB block cipher mode of operation default implementation
271 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE ECB_OneWay : public BlockOrientedCipherModeBase
272 {
273 public:
274 CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "ECB";}
275
276 void SetKey(const byte *key, size_t length, const NameValuePairs &params = g_nullNameValuePairs)
277 {m_cipher->SetKey(key, length, params); BlockOrientedCipherModeBase::ResizeBuffers();}
278 IV_Requirement IVRequirement() const {return NOT_RESYNCHRONIZABLE;}
279 unsigned int OptimalBlockSize() const {return static_cast<unsigned int>(BlockSize() * m_cipher->OptimalNumberOfParallelBlocks());}
280 void ProcessData(byte *outString, const byte *inString, size_t length);
281 };
282
283 /// \brief CBC block cipher mode of operation default implementation
284 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_ModeBase : public BlockOrientedCipherModeBase
285 {
286 public:
287 CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "CBC";}
288
289 IV_Requirement IVRequirement() const {return UNPREDICTABLE_RANDOM_IV;}
290 bool RequireAlignedInput() const {return false;}
291 unsigned int MinLastBlockSize() const {return 0;}
292 };
293
294 /// \brief CBC block cipher mode of operation encryption operation
295 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_Encryption : public CBC_ModeBase
296 {
297 public:
298 void ProcessData(byte *outString, const byte *inString, size_t length);
299 };
300
301 /// \brief CBC-CTS block cipher mode of operation encryption operation
302 /// \since Crypto++ 3.0
303 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_CTS_Encryption : public CBC_Encryption
304 {
305 public:
306 CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "CBC/CTS";}
307
308 void SetStolenIV(byte *iv) {m_stolenIV = iv;}
309 unsigned int MinLastBlockSize() const {return BlockSize()+1;}
310 size_t ProcessLastBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength);
311
312 protected:
313 void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
314 {
315 CBC_Encryption::UncheckedSetKey(key, length, params);
316 m_stolenIV = params.GetValueWithDefault(Name::StolenIV(), static_cast<byte *>(NULLPTR));
317 }
318
319 byte *m_stolenIV;
320 };
321
322 /// \brief CBC block cipher mode of operation decryption operation
323 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_Decryption : public CBC_ModeBase
324 {
325 public:
326 virtual ~CBC_Decryption() {}
327 void ProcessData(byte *outString, const byte *inString, size_t length);
328
329 protected:
330 virtual void ResizeBuffers();
331
332 SecByteBlock m_temp;
333 };
334
335 /// \brief CBC-CTS block cipher mode of operation decryption operation
336 /// \since Crypto++ 3.0
337 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_CTS_Decryption : public CBC_Decryption
338 {
339 public:
340 unsigned int MinLastBlockSize() const {return BlockSize()+1;}
341 size_t ProcessLastBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength);
342 };
343
344 /// \brief Block cipher mode of operation aggregate
345 template <class CIPHER, class BASE>
346 class CipherModeFinalTemplate_CipherHolder : protected ObjectHolder<CIPHER>, public AlgorithmImpl<BASE, CipherModeFinalTemplate_CipherHolder<CIPHER, BASE> >
347 {
348 public:
349 /// \brief Provides the name of this algorithm
350 /// \return the standard algorithm name
351 /// \details The standard algorithm name can be a name like \a AES or \a AES/GCM. Some algorithms
352 /// do not have standard names yet. For example, there is no standard algorithm name for
353 /// Shoup's ECIES.
354 static std::string CRYPTOPP_API StaticAlgorithmName()
355 {return CIPHER::StaticAlgorithmName() + "/" + BASE::StaticAlgorithmName();}
356
357 /// \brief Construct a CipherModeFinalTemplate
358 CipherModeFinalTemplate_CipherHolder()
359 {
360 this->m_cipher = &this->m_object;
361 this->ResizeBuffers();
362 }
363
364 /// \brief Construct a CipherModeFinalTemplate
365 /// \param key a byte array used to key the cipher
366 /// \param length size of the key in bytes
367 /// \details key must be at least DEFAULT_KEYLENGTH in length. Internally, the function calls
368 /// SimpleKeyingInterface::SetKey.
369 CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length)
370 {
371 this->m_cipher = &this->m_object;
372 this->SetKey(key, length);
373 }
374
375 /// \brief Construct a CipherModeFinalTemplate
376 /// \param key a byte array used to key the cipher
377 /// \param length size of the key in bytes
378 /// \param iv a byte array used to resynchronize the cipher
379 /// \details key must be at least DEFAULT_KEYLENGTH in length. iv must be IVSize() or
380 /// BLOCKSIZE in length. Internally, the function calls SimpleKeyingInterface::SetKey.
381 CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv)
382 {
383 this->m_cipher = &this->m_object;
384 this->SetKey(key, length, MakeParameters(Name::IV(), ConstByteArrayParameter(iv, this->m_cipher->BlockSize())));
385 }
386
387 /// \brief Construct a CipherModeFinalTemplate
388 /// \param key a byte array used to key the cipher
389 /// \param length size of the key in bytes
390 /// \param iv a byte array used to resynchronize the cipher
391 /// \param feedbackSize the feedback size, in bytes
392 /// \details key must be at least DEFAULT_KEYLENGTH in length. iv must be IVSize() or
393 /// BLOCKSIZE in length. Internally, the function calls SimpleKeyingInterface::SetKey.
394 CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv, int feedbackSize)
395 {
396 this->m_cipher = &this->m_object;
397 this->SetKey(key, length, MakeParameters(Name::IV(), ConstByteArrayParameter(iv, this->m_cipher->BlockSize()))(Name::FeedbackSize(), feedbackSize));
398 }
399
400 // Algorithm class
401 std::string AlgorithmProvider() const {
402 return this->m_cipher->AlgorithmProvider();
403 }
404 };
405
406 /// \tparam BASE CipherModeFinalTemplate_CipherHolder base class
407 /// \details Base class for external mode cipher combinations
408 template <class BASE>
409 class CipherModeFinalTemplate_ExternalCipher : public BASE
410 {
411 public:
412 /// \brief Construct a default CipherModeFinalTemplate
413 /// \details The cipher is not keyed.
414 CipherModeFinalTemplate_ExternalCipher() {}
415
416 /// \brief Construct a CipherModeFinalTemplate
417 /// \param cipher An external block cipher
418 /// \details The cipher should be keyed.
419 CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher)
420 {this->SetCipher(cipher);}
421
422 /// \brief Construct a CipherModeFinalTemplate
423 /// \param cipher An external block cipher
424 /// \param iv a byte array used to resynchronize the cipher
425 /// \param feedbackSize the feedback size, in bytes
426 /// \details The cipher should be keyed.
427 CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher, const byte *iv, int feedbackSize = 0)
428 {this->SetCipherWithIV(cipher, iv, feedbackSize);}
429
430 /// \brief Provides the name of this algorithm
431 /// \return the standard algorithm name
432 /// \details The standard algorithm name can be a name like \a AES or \a AES/GCM. Some algorithms
433 /// do not have standard names yet. For example, there is no standard algorithm name for
434 /// Shoup's ECIES.
435 /// \note AlgorithmName is not universally implemented yet
436 std::string AlgorithmName() const
437 {return (this->m_cipher ? this->m_cipher->AlgorithmName() + "/" : std::string("")) + BASE::StaticAlgorithmName();}
438
439 // Algorithm class
440 std::string AlgorithmProvider() const
441 {return this->m_cipher->AlgorithmProvider();}
442 };
443
444 CRYPTOPP_DLL_TEMPLATE_CLASS CFB_CipherTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
445 CRYPTOPP_DLL_TEMPLATE_CLASS CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
446 CRYPTOPP_DLL_TEMPLATE_CLASS CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
447
448 /// \brief CFB block cipher mode of operation
449 /// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
450 /// on the Crypto++ wiki.
451 template <class CIPHER>
452 struct CFB_Mode : public CipherModeDocumentation
453 {
454 typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, ConcretePolicyHolder<Empty, CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > Encryption;
455 typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, ConcretePolicyHolder<Empty, CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > Decryption;
456 };
457
458 /// \brief CFB mode, external cipher.
459 /// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
460 /// on the Crypto++ wiki.
461 struct CFB_Mode_ExternalCipher : public CipherModeDocumentation
462 {
463 typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > Encryption;
464 typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > Decryption;
465 };
466
467 /// \brief CFB block cipher mode of operation providing FIPS validated cryptography.
468 /// \details Requires full block plaintext according to FIPS 800-38A
469 /// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
470 /// on the Crypto++ wiki.
471 template <class CIPHER>
472 struct CFB_FIPS_Mode : public CipherModeDocumentation
473 {
474 typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, ConcretePolicyHolder<Empty, CFB_RequireFullDataBlocks<CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > > Encryption;
475 typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, ConcretePolicyHolder<Empty, CFB_RequireFullDataBlocks<CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > > Decryption;
476 };
477
478 /// \brief CFB mode, external cipher, providing FIPS validated cryptography.
479 /// \details Requires full block plaintext according to FIPS 800-38A
480 /// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
481 /// on the Crypto++ wiki.
482 struct CFB_FIPS_Mode_ExternalCipher : public CipherModeDocumentation
483 {
484 typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, CFB_RequireFullDataBlocks<CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > > Encryption;
485 typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, CFB_RequireFullDataBlocks<CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > > Decryption;
486 };
487
488 CRYPTOPP_DLL_TEMPLATE_CLASS AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, OFB_ModePolicy> >;
489
490 /// \brief OFB block cipher mode of operation
491 /// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
492 /// on the Crypto++ wiki.
493 template <class CIPHER>
494 struct OFB_Mode : public CipherModeDocumentation
495 {
496 typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, OFB_ModePolicy> > > > Encryption;
497 typedef Encryption Decryption;
498 };
499
500 /// \brief OFB mode, external cipher.
501 /// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
502 /// on the Crypto++ wiki.
503 struct OFB_Mode_ExternalCipher : public CipherModeDocumentation
504 {
505 typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, OFB_ModePolicy> > > > Encryption;
506 typedef Encryption Decryption;
507 };
508
509 CRYPTOPP_DLL_TEMPLATE_CLASS AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, CTR_ModePolicy> >;
510 CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, CTR_ModePolicy> > > >;
511
512 /// \brief CTR block cipher mode of operation
513 /// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
514 /// on the Crypto++ wiki.
515 template <class CIPHER>
516 struct CTR_Mode : public CipherModeDocumentation
517 {
518 typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, CTR_ModePolicy> > > > Encryption;
519 typedef Encryption Decryption;
520 };
521
522 /// \brief CTR mode, external cipher.
523 /// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
524 /// on the Crypto++ wiki.
525 struct CTR_Mode_ExternalCipher : public CipherModeDocumentation
526 {
527 typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, CTR_ModePolicy> > > > Encryption;
528 typedef Encryption Decryption;
529 };
530
531 /// \brief ECB block cipher mode of operation
532 /// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
533 /// on the Crypto++ wiki.
534 template <class CIPHER>
535 struct ECB_Mode : public CipherModeDocumentation
536 {
537 typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, ECB_OneWay> Encryption;
538 typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Decryption, ECB_OneWay> Decryption;
539 };
540
541 CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<ECB_OneWay>;
542
543 /// \brief ECB mode, external cipher.
544 /// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
545 /// on the Crypto++ wiki.
546 struct ECB_Mode_ExternalCipher : public CipherModeDocumentation
547 {
548 typedef CipherModeFinalTemplate_ExternalCipher<ECB_OneWay> Encryption;
549 typedef Encryption Decryption;
550 };
551
552 /// \brief CBC block cipher mode of operation
553 /// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
554 /// on the Crypto++ wiki.
555 template <class CIPHER>
556 struct CBC_Mode : public CipherModeDocumentation
557 {
558 typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, CBC_Encryption> Encryption;
559 typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Decryption, CBC_Decryption> Decryption;
560 };
561
562 CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<CBC_Encryption>;
563 CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<CBC_Decryption>;
564
565 /// \brief CBC mode, external cipher
566 /// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
567 /// on the Crypto++ wiki.
568 struct CBC_Mode_ExternalCipher : public CipherModeDocumentation
569 {
570 typedef CipherModeFinalTemplate_ExternalCipher<CBC_Encryption> Encryption;
571 typedef CipherModeFinalTemplate_ExternalCipher<CBC_Decryption> Decryption;
572 };
573
574 /// \brief CBC-CTS block cipher mode of operation
575 /// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
576 /// on the Crypto++ wiki.
577 /// \since Crypto++ 3.0
578 template <class CIPHER>
579 struct CBC_CTS_Mode : public CipherModeDocumentation
580 {
581 typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Encryption, CBC_CTS_Encryption> Encryption;
582 typedef CipherModeFinalTemplate_CipherHolder<typename CIPHER::Decryption, CBC_CTS_Decryption> Decryption;
583 };
584
585 CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<CBC_CTS_Encryption>;
586 CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<CBC_CTS_Decryption>;
587
588 /// \brief CBC mode with ciphertext stealing, external cipher
589 /// \sa <A HREF="http://www.cryptopp.com/wiki/Modes_of_Operation">Modes of Operation</A>
590 /// on the Crypto++ wiki.
591 /// \since Crypto++ 3.0
592 struct CBC_CTS_Mode_ExternalCipher : public CipherModeDocumentation
593 {
594 typedef CipherModeFinalTemplate_ExternalCipher<CBC_CTS_Encryption> Encryption;
595 typedef CipherModeFinalTemplate_ExternalCipher<CBC_CTS_Decryption> Decryption;
596 };
597
598 NAMESPACE_END
599
600 // Issue 340
601 #if CRYPTOPP_MSC_VERSION
602 # pragma warning(pop)
603 #endif
604
605 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
606 # pragma GCC diagnostic pop
607 #endif
608
609 #endif
CipherModeBase::IVSize
unsigned int IVSize() const
Returns length of the IV accepted by this object.
Definition: modes.h:96
OFB_ModePolicy
OFB block cipher mode of operation.
Definition: modes.h:208
CipherModeBase::GetValidKeyLength
size_t GetValidKeyLength(size_t keylength) const
Returns a valid key length for the algorithm.
Definition: modes.h:79
SimpleKeyingInterface::SetKey
virtual void SetKey(const byte *key, size_t length, const NameValuePairs &params=g_nullNameValuePairs)
Sets or reset the key of this object.
SimpleKeyingInterface::UNPREDICTABLE_RANDOM_IV
@ UNPREDICTABLE_RANDOM_IV
The IV must be random and unpredictable.
Definition: cryptlib.h:725
NameValuePairs::GetValueWithDefault
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
Definition: cryptlib.h:392
MakeParameters
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:508
CBC_Decryption
CBC block cipher mode of operation decryption operation.
Definition: modes.h:323
CBC_CTS_Decryption::MinLastBlockSize
unsigned int MinLastBlockSize() const
Provides the size of the last block.
Definition: modes.h:340
CBC_Encryption
CBC block cipher mode of operation encryption operation.
Definition: modes.h:295
CTR_Mode_ExternalCipher
CTR mode, external cipher.
Definition: modes.h:525
SymmetricCipherDocumentation
Provides Encryption and Decryption typedefs used by derived classes to implement a symmetric cipher.
Definition: seckey.h:413
CFB_ModePolicy
CFB block cipher mode of operation.
Definition: modes.h:165
SimpleKeyingInterface::NOT_RESYNCHRONIZABLE
@ NOT_RESYNCHRONIZABLE
The object does not use an IV.
Definition: cryptlib.h:729
CipherModeBase::AlgorithmProvider
std::string AlgorithmProvider() const
Retrieve the provider of this algorithm.
Definition: modes.h:55
CipherModeBase::DefaultKeyLength
size_t DefaultKeyLength() const
Returns default key length.
Definition: modes.h:69
ObjectHolder
Uses encapsulation to hide an object in derived classes.
Definition: misc.h:225
CBC_CTS_Encryption::MinLastBlockSize
unsigned int MinLastBlockSize() const
Provides the size of the last block.
Definition: modes.h:309
ECB_OneWay::OptimalBlockSize
unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this cipher.
Definition: modes.h:279
CBC_CTS_Mode
CBC-CTS block cipher mode of operation.
Definition: modes.h:579
CFB_ModePolicy::IVRequirement
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
Definition: modes.h:172
CipherModeBase
Block cipher mode of operation information.
Definition: modes.h:49
SimpleKeyingInterface::IVRequirement
virtual IV_Requirement IVRequirement() const =0
Minimal requirement for secure IVs.
secblock.h
Classes and functions for secure memory allocations.
CipherModeBase::OptimalDataAlignment
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: modes.h:90
SymmetricCipher
Interface for one direction (encryption or decryption) of a stream cipher or cipher mode.
Definition: cryptlib.h:1290
CTR_Mode
CTR block cipher mode of operation.
Definition: modes.h:516
SecByteBlock
SecBlock<byte> typedef.
Definition: secblock.h:1115
ModePolicyCommonTemplate
Block cipher mode of operation common operations.
Definition: modes.h:149
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:68
CBC_ModeBase::IVRequirement
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
Definition: modes.h:289
OFB_ModePolicy::IVRequirement
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
Definition: modes.h:214
CipherModeBase::SetCipherWithIV
void SetCipherWithIV(BlockCipher &cipher, const byte *iv, int feedbackSize=0)
Set external block cipher and IV.
Definition: modes.h:117
CFB_FIPS_Mode_ExternalCipher
CFB mode, external cipher, providing FIPS validated cryptography.
Definition: modes.h:482
CipherModeFinalTemplate_ExternalCipher
Definition: modes.h:409
BlockOrientedCipherModeBase::UncheckedSetKey
void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
Sets the key for this object without performing parameter validation.
BlockOrientedCipherModeBase::Resynchronize
void Resynchronize(const byte *iv, int length=-1)
Resynchronize with an IV.
Definition: modes.h:260
CipherModeFinalTemplate_CipherHolder::CipherModeFinalTemplate_CipherHolder
CipherModeFinalTemplate_CipherHolder()
Construct a CipherModeFinalTemplate.
Definition: modes.h:358
ECB_Mode
ECB block cipher mode of operation.
Definition: modes.h:535
CipherModeFinalTemplate_ExternalCipher::CipherModeFinalTemplate_ExternalCipher
CipherModeFinalTemplate_ExternalCipher()
Construct a default CipherModeFinalTemplate.
Definition: modes.h:414
BlockOrientedCipherModeBase::IsSelfInverting
bool IsSelfInverting() const
Determines whether the cipher is self-inverting.
Definition: modes.h:257
SimpleKeyingInterface::UNIQUE_IV
@ UNIQUE_IV
The IV must be unique.
Definition: cryptlib.h:721
CBC_ModeBase::MinLastBlockSize
unsigned int MinLastBlockSize() const
Provides the size of the last block.
Definition: modes.h:291
CipherDir
CipherDir
Specifies a direction for a cipher to operate.
Definition: cryptlib.h:123
SimpleKeyingInterface::IV_Requirement
IV_Requirement
Secure IVs requirements as enumerated values.
Definition: cryptlib.h:719
CBC_CTS_Decryption
CBC-CTS block cipher mode of operation decryption operation.
Definition: modes.h:337
argnames.h
Standard names for retrieving values by name when working with NameValuePairs.
SimpleKeyingInterface::Resynchronize
virtual void Resynchronize(const byte *iv, int ivLength=-1)
Resynchronize with an IV.
Definition: cryptlib.h:783
misc.h
Utility functions for the Crypto++ library.
CBC_Mode
CBC block cipher mode of operation.
Definition: modes.h:556
CBC_CTS_Encryption
CBC-CTS block cipher mode of operation encryption operation.
Definition: modes.h:303
CipherModeDocumentation
Block cipher mode of operation information.
Definition: modes.h:44
AdditiveCipherTemplate
Base class for additive stream ciphers with SymmetricCipher interface.
Definition: strciphr.h:299
CipherModeBase::IsValidKeyLength
bool IsValidKeyLength(size_t keylength) const
Returns whether keylength is a valid key length.
Definition: modes.h:85
CipherModeFinalTemplate_ExternalCipher::AlgorithmName
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: modes.h:436
BlockOrientedCipherModeBase::MandatoryBlockSize
unsigned int MandatoryBlockSize() const
Provides the mandatory block size of the cipher.
Definition: modes.h:255
SimpleKeyingInterface::IsResynchronizable
bool IsResynchronizable() const
Determines if the object can be resynchronized.
Definition: cryptlib.h:740
WRITE_KEYSTREAM
@ WRITE_KEYSTREAM
Wirte the keystream to the output buffer, input is NULL.
Definition: strciphr.h:90
KeystreamOperation
KeystreamOperation
Keystream operation flags.
Definition: strciphr.h:88
CopyOrZero
void CopyOrZero(void *dest, size_t dsize, const void *src, size_t ssize)
Initialize a block of memory.
Definition: modes.h:196
CipherModeFinalTemplate_CipherHolder::CipherModeFinalTemplate_CipherHolder
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv)
Construct a CipherModeFinalTemplate.
Definition: modes.h:381
CFB_Mode
CFB block cipher mode of operation.
Definition: modes.h:452
BlockOrientedCipherModeBase::IsRandomAccess
bool IsRandomAccess() const
Determines whether the cipher supports random access.
Definition: modes.h:256
StreamTransformation::ProcessLastBlock
virtual size_t ProcessLastBlock(byte *outString, size_t outLength, const byte *inString, size_t inLength)
Encrypt or decrypt the last block of data.
CBC_Mode_ExternalCipher
CBC mode, external cipher.
Definition: modes.h:568
CipherModeBase::MaxKeyLength
size_t MaxKeyLength() const
Returns largest valid key length.
Definition: modes.h:65
CipherModeFinalTemplate_CipherHolder
Block cipher mode of operation aggregate.
Definition: modes.h:346
CipherModeFinalTemplate_CipherHolder::CipherModeFinalTemplate_CipherHolder
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length)
Construct a CipherModeFinalTemplate.
Definition: modes.h:369
CipherModeFinalTemplate_ExternalCipher::CipherModeFinalTemplate_ExternalCipher
CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher, const byte *iv, int feedbackSize=0)
Construct a CipherModeFinalTemplate.
Definition: modes.h:427
ECB_OneWay::SetKey
void SetKey(const byte *key, size_t length, const NameValuePairs &params=g_nullNameValuePairs)
Sets or reset the key of this object.
Definition: modes.h:276
OFB_ModePolicy::CipherIsRandomAccess
bool CipherIsRandomAccess() const
Flag indicating random access.
Definition: modes.h:213
CTR_ModePolicy::CipherIsRandomAccess
bool CipherIsRandomAccess() const
Flag indicating random access.
Definition: modes.h:230
CFB_CipherTemplate
Base class for feedback based stream ciphers with SymmetricCipher interface.
Definition: strciphr.h:563
lword
word64 lword
Large word type.
Definition: config_int.h:158
CTR_ModePolicy::IVRequirement
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
Definition: modes.h:231
CFB_DecryptionTemplate
Base class for feedback based stream ciphers in the reverse direction with SymmetricCipher interface.
Definition: strciphr.h:653
InvalidArgument
An invalid argument was detected.
Definition: cryptlib.h:202
OFB_Mode
OFB block cipher mode of operation.
Definition: modes.h:494
CipherModeFinalTemplate_CipherHolder::CipherModeFinalTemplate_CipherHolder
CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv, int feedbackSize)
Construct a CipherModeFinalTemplate.
Definition: modes.h:394
OFB_Mode_ExternalCipher
OFB mode, external cipher.
Definition: modes.h:503
g_nullNameValuePairs
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
Definition: cryptlib.h:529
ECB_OneWay::IVRequirement
IV_Requirement IVRequirement() const
Minimal requirement for secure IVs.
Definition: modes.h:278
CipherModeBase::SetCipher
void SetCipher(BlockCipher &cipher)
Set external block cipher.
Definition: modes.h:105
CryptoPP
Crypto++ library namespace.
CRYPTOPP_API
#define CRYPTOPP_API
Win32 calling convention.
Definition: config_dll.h:119
CFB_Mode_ExternalCipher
CFB mode, external cipher.
Definition: modes.h:461
CRYPTOPP_DLL_TEMPLATE_CLASS
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Instantiate templates in a dynamic library.
Definition: config_dll.h:72
CFB_FIPS_Mode
CFB block cipher mode of operation providing FIPS validated cryptography.
Definition: modes.h:472
StreamTransformation::ProcessData
virtual void ProcessData(byte *outString, const byte *inString, size_t length)=0
Encrypt or decrypt an array of bytes.
CBC_CTS_Mode_ExternalCipher
CBC mode with ciphertext stealing, external cipher.
Definition: modes.h:592
BlockCipher
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1282
BlockOrientedCipherModeBase::IsForwardTransformation
bool IsForwardTransformation() const
Determines if the cipher is being operated in its forward direction.
Definition: modes.h:258
ECB_Mode_ExternalCipher
ECB mode, external cipher.
Definition: modes.h:546
BlockOrientedCipherModeBase
Block cipher mode of operation default implementation.
Definition: modes.h:250
NameValuePairs::GetIntValueWithDefault
CRYPTOPP_DLL int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
Definition: cryptlib.h:424
memcpy_s
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
Definition: misc.h:506
CBC_ModeBase
CBC block cipher mode of operation default implementation.
Definition: modes.h:284
CTR_ModePolicy
CTR block cipher mode of operation.
Definition: modes.h:224
CipherModeFinalTemplate_ExternalCipher::CipherModeFinalTemplate_ExternalCipher
CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher)
Construct a CipherModeFinalTemplate.
Definition: modes.h:419
CFB_EncryptionTemplate
Base class for feedback based stream ciphers in the forward direction with SymmetricCipher interface.
Definition: strciphr.h:644
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:321
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
CipherModeBase::MinKeyLength
size_t MinKeyLength() const
Returns smallest valid key length.
Definition: modes.h:61
ECB_OneWay
ECB block cipher mode of operation default implementation.
Definition: modes.h:271
AlgorithmImpl
Base class information.
Definition: simple.h:39
ConstByteArrayParameter
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:24
algparam.h
Classes for working with NameValuePairs.
strciphr.h
Classes for implementing stream ciphers.
SimpleKeyingInterface::RANDOM_IV
@ RANDOM_IV
The IV must be random and possibly predictable.
Definition: cryptlib.h:723
CipherModeFinalTemplate_CipherHolder::StaticAlgorithmName
static std::string StaticAlgorithmName()
Provides the name of this algorithm.
Definition: modes.h:354