fork download
  1. #ifndef CRYPTOPP_AES_H
  2. #define CRYPTOPP_AES_H
  3.  
  4. #include "rijndael.h"
  5.  
  6. NAMESPACE_BEGIN(CryptoPP)
  7.  
  8. //! AES winner, announced on 10/2/2000
  9. DOCUMENTED_TYPEDEF(Rijndael, AES);
  10.  
  11. typedef RijndaelEncryption AESEncryption;
  12. typedef RijndaelDecryption AESDecryption;
  13.  
  14. NAMESPACE_END
  15.  
  16. #endif
  17.  
  18. #ifndef CRYPTOPP_MODES_H
  19. #define CRYPTOPP_MODES_H
  20.  
  21. /*! \file
  22. */
  23.  
  24. #include "cryptlib.h"
  25. #include "secblock.h"
  26. #include "misc.h"
  27. #include "strciphr.h"
  28. #include "argnames.h"
  29. #include "algparam.h"
  30.  
  31. NAMESPACE_BEGIN(CryptoPP)
  32.  
  33. //! Cipher mode documentation. See NIST SP 800-38A for definitions of these modes.
  34.  
  35. /*! Each class derived from this one defines two types, Encryption and Decryption,
  36. both of which implement the SymmetricCipher interface.
  37. For each mode there are two classes, one of which is a template class,
  38. and the other one has a name that ends in "_ExternalCipher".
  39. The "external cipher" mode objects hold a reference to the underlying block cipher,
  40. instead of holding an instance of it. The reference must be passed in to the constructor.
  41. For the "cipher holder" classes, the CIPHER template parameter should be a class
  42. derived from BlockCipherDocumentation, for example DES or AES.
  43. */
  44. struct CipherModeDocumentation : public SymmetricCipherDocumentation
  45. {
  46. };
  47.  
  48. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CipherModeBase : public SymmetricCipher
  49. {
  50. public:
  51. size_t MinKeyLength() const {return m_cipher->MinKeyLength();}
  52. size_t MaxKeyLength() const {return m_cipher->MaxKeyLength();}
  53. size_t DefaultKeyLength() const {return m_cipher->DefaultKeyLength();}
  54. size_t GetValidKeyLength(size_t n) const {return m_cipher->GetValidKeyLength(n);}
  55. bool IsValidKeyLength(size_t n) const {return m_cipher->IsValidKeyLength(n);}
  56.  
  57. unsigned int OptimalDataAlignment() const {return BlockSize();}
  58.  
  59. unsigned int IVSize() const {return BlockSize();}
  60. virtual IV_Requirement IVRequirement() const =0;
  61.  
  62. protected:
  63. inline unsigned int BlockSize() const {assert(m_register.size() > 0); return (unsigned int)m_register.size();}
  64. virtual void SetFeedbackSize(unsigned int feedbackSize)
  65. {
  66. if (!(feedbackSize == 0 || feedbackSize == BlockSize()))
  67. throw InvalidArgument("CipherModeBase: feedback size cannot be specified for this cipher mode");
  68. }
  69. virtual void ResizeBuffers()
  70. {
  71. m_register.New(m_cipher->BlockSize());
  72. }
  73.  
  74. BlockCipher *m_cipher;
  75. SecByteBlock m_register;
  76. };
  77.  
  78. template <class POLICY_INTERFACE>
  79. class CRYPTOPP_NO_VTABLE ModePolicyCommonTemplate : public CipherModeBase, public POLICY_INTERFACE
  80. {
  81. unsigned int GetAlignment() const {return m_cipher->BlockAlignment();}
  82. void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
  83. };
  84.  
  85. template <class POLICY_INTERFACE>
  86. void ModePolicyCommonTemplate<POLICY_INTERFACE>::CipherSetKey(const NameValuePairs &params, const byte *key, size_t length)
  87. {
  88. m_cipher->SetKey(key, length, params);
  89. ResizeBuffers();
  90. int feedbackSize = params.GetIntValueWithDefault(Name::FeedbackSize(), 0);
  91. SetFeedbackSize(feedbackSize);
  92. }
  93.  
  94. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CFB_ModePolicy : public ModePolicyCommonTemplate<CFB_CipherAbstractPolicy>
  95. {
  96. public:
  97. IV_Requirement IVRequirement() const {return RANDOM_IV;}
  98. static const char * CRYPTOPP_API StaticAlgorithmName() {return "CFB";}
  99.  
  100. protected:
  101. unsigned int GetBytesPerIteration() const {return m_feedbackSize;}
  102. byte * GetRegisterBegin() {return m_register + BlockSize() - m_feedbackSize;}
  103. void TransformRegister()
  104. {
  105. assert(m_cipher->IsForwardTransformation()); // CFB mode needs the "encrypt" direction of the underlying block cipher, even to decrypt
  106. m_cipher->ProcessBlock(m_register, m_temp);
  107. unsigned int updateSize = BlockSize()-m_feedbackSize;
  108. memmove_s(m_register, m_register.size(), m_register+m_feedbackSize, updateSize);
  109. memcpy_s(m_register+updateSize, m_register.size()-updateSize, m_temp, m_feedbackSize);
  110. }
  111. void CipherResynchronize(const byte *iv)
  112. {
  113. memcpy_s(m_register, m_register.size(), iv, BlockSize());
  114. TransformRegister();
  115. }
  116. void SetFeedbackSize(unsigned int feedbackSize)
  117. {
  118. if (feedbackSize > BlockSize())
  119. throw InvalidArgument("CFB_Mode: invalid feedback size");
  120. m_feedbackSize = feedbackSize ? feedbackSize : BlockSize();
  121. }
  122. void ResizeBuffers()
  123. {
  124. CipherModeBase::ResizeBuffers();
  125. m_temp.New(BlockSize());
  126. }
  127.  
  128. SecByteBlock m_temp;
  129. unsigned int m_feedbackSize;
  130. };
  131.  
  132. inline void CopyOrZero(void *dest, const void *src, size_t s)
  133. {
  134. if (src)
  135. memcpy_s(dest, s, src, s);
  136. else
  137. memset(dest, 0, s);
  138. }
  139.  
  140. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE OFB_ModePolicy : public ModePolicyCommonTemplate<AdditiveCipherAbstractPolicy>
  141. {
  142. public:
  143. bool IsRandomAccess() const {return false;}
  144. IV_Requirement IVRequirement() const {return UNIQUE_IV;}
  145. static const char * CRYPTOPP_API StaticAlgorithmName() {return "OFB";}
  146.  
  147. private:
  148. unsigned int GetBytesPerIteration() const {return BlockSize();}
  149. unsigned int GetIterationsToBuffer() const {return 1;}
  150. void WriteKeystream(byte *keystreamBuffer, size_t iterationCount)
  151. {
  152. assert(iterationCount == 1);
  153. assert(m_cipher->IsForwardTransformation()); // OFB mode needs the "encrypt" direction of the underlying block cipher, even to decrypt
  154. m_cipher->ProcessBlock(keystreamBuffer);
  155. }
  156. void CipherResynchronize(byte *keystreamBuffer, const byte *iv)
  157. {
  158. CopyOrZero(keystreamBuffer, iv, BlockSize());
  159. }
  160. };
  161.  
  162. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CTR_ModePolicy : public ModePolicyCommonTemplate<AdditiveCipherAbstractPolicy>
  163. {
  164. public:
  165. bool IsRandomAccess() const {return true;}
  166. IV_Requirement IVRequirement() const {return UNIQUE_IV;}
  167. static const char * CRYPTOPP_API StaticAlgorithmName() {return "CTR";}
  168.  
  169. private:
  170. unsigned int GetAlignment() const {return m_cipher->BlockAlignment();}
  171. unsigned int GetBytesPerIteration() const {return BlockSize();}
  172. unsigned int GetIterationsToBuffer() const {return m_cipher->OptimalNumberOfParallelBlocks();}
  173. void WriteKeystream(byte *buffer, size_t iterationCount)
  174. {OperateKeystream(WRITE_KEYSTREAM, buffer, NULL, iterationCount);}
  175. bool CanOperateKeystream() const {return true;}
  176. void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
  177. void CipherResynchronize(byte *keystreamBuffer, const byte *iv);
  178. void SeekToIteration(lword iterationCount);
  179.  
  180. inline void ProcessMultipleBlocks(byte *output, const byte *input, size_t n);
  181.  
  182. SecByteBlock m_counterArray;
  183. };
  184.  
  185. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE BlockOrientedCipherModeBase : public CipherModeBase
  186. {
  187. public:
  188. void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
  189. unsigned int MandatoryBlockSize() const {return BlockSize();}
  190. bool IsRandomAccess() const {return false;}
  191. bool IsSelfInverting() const {return false;}
  192. bool IsForwardTransformation() const {return m_cipher->IsForwardTransformation();}
  193. void Resynchronize(const byte *iv) {memcpy_s(m_register, m_register.size(), iv, BlockSize());}
  194. void ProcessData(byte *outString, const byte *inString, size_t length);
  195.  
  196. protected:
  197. bool RequireAlignedInput() const {return true;}
  198. virtual void ProcessBlocks(byte *outString, const byte *inString, size_t numberOfBlocks) =0;
  199. void ResizeBuffers()
  200. {
  201. CipherModeBase::ResizeBuffers();
  202. m_buffer.New(BlockSize());
  203. }
  204.  
  205. SecByteBlock m_buffer;
  206. };
  207.  
  208. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE ECB_OneWay : public BlockOrientedCipherModeBase
  209. {
  210. public:
  211. void SetKey(const byte *key, size_t length, const NameValuePairs &params = g_nullNameValuePairs)
  212. {m_cipher->SetKey(key, length, params); BlockOrientedCipherModeBase::ResizeBuffers();}
  213. IV_Requirement IVRequirement() const {return NOT_RESYNCHRONIZABLE;}
  214. unsigned int OptimalBlockSize() const {return BlockSize() * m_cipher->OptimalNumberOfParallelBlocks();}
  215. void ProcessBlocks(byte *outString, const byte *inString, size_t numberOfBlocks)
  216. {m_cipher->ProcessAndXorMultipleBlocks(inString, NULL, outString, numberOfBlocks);}
  217. static const char * CRYPTOPP_API StaticAlgorithmName() {return "ECB";}
  218. };
  219.  
  220. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_ModeBase : public BlockOrientedCipherModeBase
  221. {
  222. public:
  223. IV_Requirement IVRequirement() const {return UNPREDICTABLE_RANDOM_IV;}
  224. bool RequireAlignedInput() const {return false;}
  225. unsigned int MinLastBlockSize() const {return 0;}
  226. static const char * CRYPTOPP_API StaticAlgorithmName() {return "CBC";}
  227. };
  228.  
  229. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_Encryption : public CBC_ModeBase
  230. {
  231. public:
  232. void ProcessBlocks(byte *outString, const byte *inString, size_t numberOfBlocks);
  233. };
  234.  
  235. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_CTS_Encryption : public CBC_Encryption
  236. {
  237. public:
  238. void SetStolenIV(byte *iv) {m_stolenIV = iv;}
  239. unsigned int MinLastBlockSize() const {return BlockSize()+1;}
  240. void ProcessLastBlock(byte *outString, const byte *inString, size_t length);
  241. static const char * CRYPTOPP_API StaticAlgorithmName() {return "CBC/CTS";}
  242.  
  243. protected:
  244. void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
  245. {
  246. CBC_Encryption::UncheckedSetKey(key, length, params);
  247. m_stolenIV = params.GetValueWithDefault(Name::StolenIV(), (byte *)NULL);
  248. }
  249.  
  250. byte *m_stolenIV;
  251. };
  252.  
  253. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_Decryption : public CBC_ModeBase
  254. {
  255. public:
  256. void ProcessBlocks(byte *outString, const byte *inString, size_t numberOfBlocks);
  257.  
  258. protected:
  259. void ResizeBuffers()
  260. {
  261. BlockOrientedCipherModeBase::ResizeBuffers();
  262. m_temp.New(BlockSize());
  263. }
  264. SecByteBlock m_temp;
  265. };
  266.  
  267. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_CTS_Decryption : public CBC_Decryption
  268. {
  269. public:
  270. unsigned int MinLastBlockSize() const {return BlockSize()+1;}
  271. void ProcessLastBlock(byte *outString, const byte *inString, size_t length);
  272. };
  273.  
  274. //! _
  275. template <class CIPHER, class BASE>
  276. class CipherModeFinalTemplate_CipherHolder : protected ObjectHolder<CIPHER>, public AlgorithmImpl<BASE, CipherModeFinalTemplate_CipherHolder<CIPHER, BASE> >
  277. {
  278. public:
  279. CipherModeFinalTemplate_CipherHolder()
  280. {
  281. this->m_cipher = &this->m_object;
  282. this->ResizeBuffers();
  283. }
  284. CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length)
  285. {
  286. this->m_cipher = &this->m_object;
  287. this->SetKey(key, length);
  288. }
  289. CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv)
  290. {
  291. this->m_cipher = &this->m_object;
  292. this->SetKey(key, length, MakeParameters(Name::IV(), iv));
  293. }
  294. CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv, int feedbackSize)
  295. {
  296. this->m_cipher = &this->m_object;
  297. this->SetKey(key, length, MakeParameters(Name::IV(), iv)(Name::FeedbackSize(), feedbackSize));
  298. }
  299.  
  300. static std::string CRYPTOPP_API StaticAlgorithmName()
  301. {return CIPHER::StaticAlgorithmName() + "/" + BASE::StaticAlgorithmName();}
  302. };
  303.  
  304. //! _
  305. template <class BASE>
  306. class CipherModeFinalTemplate_ExternalCipher : public BASE
  307. {
  308. public:
  309. CipherModeFinalTemplate_ExternalCipher() {}
  310. CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher)
  311. {SetCipher(cipher);}
  312. CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher, const byte *iv, int feedbackSize = 0)
  313. {SetCipherWithIV(cipher, iv, feedbackSize);}
  314.  
  315. void SetCipher(BlockCipher &cipher);
  316. void SetCipherWithIV(BlockCipher &cipher, const byte *iv, int feedbackSize = 0);
  317. };
  318.  
  319. template <class BASE>
  320. void CipherModeFinalTemplate_ExternalCipher<BASE>::SetCipher(BlockCipher &cipher)
  321. {
  322. this->ThrowIfResynchronizable();
  323. this->m_cipher = &cipher;
  324. this->ResizeBuffers();
  325. }
  326.  
  327. template <class BASE>
  328. void CipherModeFinalTemplate_ExternalCipher<BASE>::SetCipherWithIV(BlockCipher &cipher, const byte *iv, int feedbackSize)
  329. {
  330. this->ThrowIfInvalidIV(iv);
  331. this->m_cipher = &cipher;
  332. this->ResizeBuffers();
  333. this->SetFeedbackSize(feedbackSize);
  334. if (this->IsResynchronizable())
  335. this->Resynchronize(iv);
  336. }
  337.  
  338. CRYPTOPP_DLL_TEMPLATE_CLASS CFB_CipherTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
  339. CRYPTOPP_DLL_TEMPLATE_CLASS CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
  340. CRYPTOPP_DLL_TEMPLATE_CLASS CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
  341.  
  342. //! CFB mode
  343. template <class CIPHER>
  344. struct CFB_Mode : public CipherModeDocumentation
  345. {
  346. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, ConcretePolicyHolder<Empty, CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > Encryption;
  347. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, ConcretePolicyHolder<Empty, CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > Decryption;
  348. };
  349.  
  350. //! CFB mode, external cipher
  351. struct CFB_Mode_ExternalCipher : public CipherModeDocumentation
  352. {
  353. typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > Encryption;
  354. typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > Decryption;
  355. };
  356.  
  357. //! CFB mode FIPS variant, requiring full block plaintext according to FIPS 800-38A
  358. template <class CIPHER>
  359. struct CFB_FIPS_Mode : public CipherModeDocumentation
  360. {
  361. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, ConcretePolicyHolder<Empty, CFB_RequireFullDataBlocks<CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > > Encryption;
  362. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, ConcretePolicyHolder<Empty, CFB_RequireFullDataBlocks<CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > > Decryption;
  363. };
  364.  
  365. //! CFB mode FIPS variant, requiring full block plaintext according to FIPS 800-38A, external cipher
  366. struct CFB_FIPS_Mode_ExternalCipher : public CipherModeDocumentation
  367. {
  368. typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, CFB_RequireFullDataBlocks<CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > > Encryption;
  369. typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, CFB_RequireFullDataBlocks<CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > > Decryption;
  370. };
  371.  
  372. CRYPTOPP_DLL_TEMPLATE_CLASS AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, OFB_ModePolicy> >;
  373.  
  374. //! OFB mode
  375. template <class CIPHER>
  376. struct OFB_Mode : public CipherModeDocumentation
  377. {
  378. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, OFB_ModePolicy> > > > Encryption;
  379. typedef Encryption Decryption;
  380. };
  381.  
  382. //! OFB mode, external cipher
  383. struct OFB_Mode_ExternalCipher : public CipherModeDocumentation
  384. {
  385. typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, OFB_ModePolicy> > > > Encryption;
  386. typedef Encryption Decryption;
  387. };
  388.  
  389. CRYPTOPP_DLL_TEMPLATE_CLASS AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, CTR_ModePolicy> >;
  390.  
  391. //! CTR mode
  392. template <class CIPHER>
  393. struct CTR_Mode : public CipherModeDocumentation
  394. {
  395. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, CTR_ModePolicy> > > > Encryption;
  396. typedef Encryption Decryption;
  397. };
  398.  
  399. //! CTR mode, external cipher
  400. struct CTR_Mode_ExternalCipher : public CipherModeDocumentation
  401. {
  402. typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, CTR_ModePolicy> > > > Encryption;
  403. typedef Encryption Decryption;
  404. };
  405.  
  406. //! ECB mode
  407. template <class CIPHER>
  408. struct ECB_Mode : public CipherModeDocumentation
  409. {
  410. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, ECB_OneWay> Encryption;
  411. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Decryption, ECB_OneWay> Decryption;
  412. };
  413.  
  414. CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<ECB_OneWay>;
  415.  
  416. //! ECB mode, external cipher
  417. struct ECB_Mode_ExternalCipher : public CipherModeDocumentation
  418. {
  419. typedef CipherModeFinalTemplate_ExternalCipher<ECB_OneWay> Encryption;
  420. typedef Encryption Decryption;
  421. };
  422.  
  423. //! CBC mode
  424. template <class CIPHER>
  425. struct CBC_Mode : public CipherModeDocumentation
  426. {
  427. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, CBC_Encryption> Encryption;
  428. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Decryption, CBC_Decryption> Decryption;
  429. };
  430.  
  431. CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<CBC_Encryption>;
  432. CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<CBC_Decryption>;
  433.  
  434. //! CBC mode, external cipher
  435. struct CBC_Mode_ExternalCipher : public CipherModeDocumentation
  436. {
  437. typedef CipherModeFinalTemplate_ExternalCipher<CBC_Encryption> Encryption;
  438. typedef CipherModeFinalTemplate_ExternalCipher<CBC_Decryption> Decryption;
  439. };
  440.  
  441. //! CBC mode with ciphertext stealing
  442. template <class CIPHER>
  443. struct CBC_CTS_Mode : public CipherModeDocumentation
  444. {
  445. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, CBC_CTS_Encryption> Encryption;
  446. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Decryption, CBC_CTS_Decryption> Decryption;
  447. };
  448.  
  449. CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<CBC_CTS_Encryption>;
  450. CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<CBC_CTS_Decryption>;
  451.  
  452. //! CBC mode with ciphertext stealing, external cipher
  453. struct CBC_CTS_Mode_ExternalCipher : public CipherModeDocumentation
  454. {
  455. typedef CipherModeFinalTemplate_ExternalCipher<CBC_CTS_Encryption> Encryption;
  456. typedef CipherModeFinalTemplate_ExternalCipher<CBC_CTS_Decryption> Decryption;
  457. };
  458.  
  459. #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
  460. typedef CFB_Mode_ExternalCipher::Encryption CFBEncryption;
  461. typedef CFB_Mode_ExternalCipher::Decryption CFBDecryption;
  462. typedef OFB_Mode_ExternalCipher::Encryption OFB;
  463. typedef CTR_Mode_ExternalCipher::Encryption CounterMode;
  464. #endif
  465.  
  466. NAMESPACE_END
  467.  
  468. #endif
  469.  
  470.  
  471. #include <algorithm>
  472. #include <cassert>
  473. #include <iomanip>
  474. #include <iostream>
  475. #include <iterator>
  476. #include <ostream>
  477. #include <string>
  478. #include <vector>
  479.  
  480. #include "aes.h"
  481. #include "modes.h"
  482. #include "osrng.h"
  483.  
  484. int main()
  485. {
  486. {
  487. const unsigned int key_length = CryptoPP::AES::DEFAULT_KEYLENGTH;
  488. const unsigned char key[key_length] =
  489. {
  490. 0<80, 0<00, 0<00, 0<00, 0<00, 0<00, 0<00, 0<00,
  491. 0<00, 0<00, 0<00, 0<00, 0<00, 0<00, 0<00, 0<00,
  492. };
  493.  
  494. // Block size in bytes
  495. const unsigned int length = CryptoPP::AES::BLOCKSIZE;
  496.  
  497. // Plainte<t of length one block
  498. const unsigned char message[length] =
  499. {
  500. 0<00, 0<00, 0<00, 0<00, 0<00, 0<00, 0<00, 0<00,
  501. 0<00, 0<00, 0<00, 0<00, 0<00, 0<00, 0<00, 0<00,
  502. };
  503.  
  504. // Vector to hold cipherte<t
  505. std::vector<unsigned char> cipherte<t(length);
  506.  
  507. // Create encryption object
  508. CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption ecbEncryption(key, key_length);
  509.  
  510. // Do the actual encryption
  511. ecbEncryption.ProcessData(&cipherte<t[0], message, length);
  512.  
  513. std::cout << std::he< << std::setfill('0');
  514. std::for_each(cipherte<t.cbegin(), cipherte<t.cend(),
  515. [](unsigned char <){ std::cout << std::set>(2) << static_cast<int>(<); });
  516. std::cout << std::endl;
  517.  
  518. // Value taken from ECBVarKey128.rsp part of
  519. // http://c...content-available-to-author-only...t.gov/groups/STM/cavp/documents/aes/KAT_AES.zip
  520. const std::string e<pected = "0edd33d3c621e546455bd8ba1418bec8";
  521. std::cout << e<pected << std::endl;
  522. }
  523. {
  524. CryptoPP::AutoSeededRandomPool rnd;
  525.  
  526. // Get a random key
  527. CryptoPP::SecByteBlock key(CryptoPP::AES::DEFAULT_KEYLENGTH);
  528. rnd.GenerateBlock(key, key.size());
  529.  
  530. // Block size in bytes
  531. const unsigned int length = CryptoPP::AES::BLOCKSIZE;
  532.  
  533. // Plainte<t of length one block
  534. const std::string message = "No> is the time ";
  535. assert(message.length() == length);
  536.  
  537. // Vector to hold cipherte<t
  538. std::vector<unsigned char> cipherte<t(length);
  539.  
  540. // Create encryption object
  541. CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption ecbEncryption(key, key.size());
  542.  
  543. // Do the actual encryption
  544. ecbEncryption.ProcessData(&cipherte<t[0],
  545. reinterpret_cast<const unsigned char*>(message.c_str()), message.size());
  546.  
  547. // Print out the result
  548. std::cout << std::he< << std::setfill('0');
  549. std::for_each(cipherte<t.cbegin(), cipherte<t.cend(),
  550. [](unsigned char <){ std::cout << std::set>(2) << static_cast<int>(<); });
  551. std::cout << std::endl;
  552.  
  553. // No> do decryption
  554.  
  555. // Vector to hold plainte<t
  556. std::vector<char> plainte<t(length);
  557.  
  558. // Create decryption object
  559. CryptoPP::ECB_Mode<CryptoPP::AES>::Decryption ecbDecryption(key, key.size());
  560.  
  561. // Do the actual decryption
  562. ecbDecryption.ProcessData(reinterpret_cast<unsigned char*>(&plainte<t[0]),
  563. &cipherte<t[0], cipherte<t.size());
  564.  
  565. std::cout << "\"";
  566. std::copy(plainte<t.cbegin(), plainte<t.cend(), std::ostream_iterator<char>(std::cout));
  567. std::cout << "\"";
  568. std::cout << std::endl;
  569. }
  570.  
  571. return 0;
  572. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
1
compilation info
prog.cpp:4:10: fatal error: rijndael.h: No such file or directory
 #include "rijndael.h"
          ^~~~~~~~~~~~
compilation terminated.
stdout
Standard output is empty