modes.h 26 KB

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