Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

423 lines
17 KiB

  1. #ifndef CRYPTOPP_MODES_H
  2. #define CRYPTOPP_MODES_H
  3. /*! \file
  4. */
  5. #include "cryptlib.h"
  6. #include "secblock.h"
  7. #include "misc.h"
  8. #include "strciphr.h"
  9. #include "argnames.h"
  10. #include "algparam.h"
  11. NAMESPACE_BEGIN(CryptoPP)
  12. //! Cipher modes documentation. See NIST SP 800-38A for definitions of these modes. See AuthenticatedSymmetricCipherDocumentation for authenticated encryption modes.
  13. /*! Each class derived from this one defines two types, Encryption and Decryption,
  14. both of which implement the SymmetricCipher interface.
  15. For each mode there are two classes, one of which is a template class,
  16. and the other one has a name that ends in "_ExternalCipher".
  17. The "external cipher" mode objects hold a reference to the underlying block cipher,
  18. instead of holding an instance of it. The reference must be passed in to the constructor.
  19. For the "cipher holder" classes, the CIPHER template parameter should be a class
  20. derived from BlockCipherDocumentation, for example DES or AES.
  21. */
  22. struct CipherModeDocumentation : public SymmetricCipherDocumentation
  23. {
  24. };
  25. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CipherModeBase : public SymmetricCipher
  26. {
  27. public:
  28. size_t MinKeyLength() const {return m_cipher->MinKeyLength();}
  29. size_t MaxKeyLength() const {return m_cipher->MaxKeyLength();}
  30. size_t DefaultKeyLength() const {return m_cipher->DefaultKeyLength();}
  31. size_t GetValidKeyLength(size_t n) const {return m_cipher->GetValidKeyLength(n);}
  32. bool IsValidKeyLength(size_t n) const {return m_cipher->IsValidKeyLength(n);}
  33. unsigned int OptimalDataAlignment() const {return m_cipher->OptimalDataAlignment();}
  34. unsigned int IVSize() const {return BlockSize();}
  35. virtual IV_Requirement IVRequirement() const =0;
  36. void SetCipher(BlockCipher &cipher)
  37. {
  38. this->ThrowIfResynchronizable();
  39. this->m_cipher = &cipher;
  40. this->ResizeBuffers();
  41. }
  42. void SetCipherWithIV(BlockCipher &cipher, const byte *iv, int feedbackSize = 0)
  43. {
  44. this->ThrowIfInvalidIV(iv);
  45. this->m_cipher = &cipher;
  46. this->ResizeBuffers();
  47. this->SetFeedbackSize(feedbackSize);
  48. if (this->IsResynchronizable())
  49. this->Resynchronize(iv);
  50. }
  51. protected:
  52. CipherModeBase() : m_cipher(NULL) {}
  53. inline unsigned int BlockSize() const {assert(m_register.size() > 0); return (unsigned int)m_register.size();}
  54. virtual void SetFeedbackSize(unsigned int feedbackSize)
  55. {
  56. if (!(feedbackSize == 0 || feedbackSize == BlockSize()))
  57. throw InvalidArgument("CipherModeBase: feedback size cannot be specified for this cipher mode");
  58. }
  59. virtual void ResizeBuffers()
  60. {
  61. m_register.New(m_cipher->BlockSize());
  62. }
  63. BlockCipher *m_cipher;
  64. AlignedSecByteBlock m_register;
  65. };
  66. template <class POLICY_INTERFACE>
  67. class CRYPTOPP_NO_VTABLE ModePolicyCommonTemplate : public CipherModeBase, public POLICY_INTERFACE
  68. {
  69. unsigned int GetAlignment() const {return m_cipher->OptimalDataAlignment();}
  70. void CipherSetKey(const NameValuePairs &params, const byte *key, size_t length);
  71. };
  72. template <class POLICY_INTERFACE>
  73. void ModePolicyCommonTemplate<POLICY_INTERFACE>::CipherSetKey(const NameValuePairs &params, const byte *key, size_t length)
  74. {
  75. m_cipher->SetKey(key, length, params);
  76. ResizeBuffers();
  77. int feedbackSize = params.GetIntValueWithDefault(Name::FeedbackSize(), 0);
  78. SetFeedbackSize(feedbackSize);
  79. }
  80. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CFB_ModePolicy : public ModePolicyCommonTemplate<CFB_CipherAbstractPolicy>
  81. {
  82. public:
  83. IV_Requirement IVRequirement() const {return RANDOM_IV;}
  84. static const char * CRYPTOPP_API StaticAlgorithmName() {return "CFB";}
  85. protected:
  86. unsigned int GetBytesPerIteration() const {return m_feedbackSize;}
  87. // VALVE: clang requires m_register.BytePtr() instead of m_register
  88. byte * GetRegisterBegin() {return m_register.BytePtr() + BlockSize() - m_feedbackSize;}
  89. bool CanIterate() const {return m_feedbackSize == BlockSize();}
  90. void Iterate(byte *output, const byte *input, CipherDir dir, size_t iterationCount);
  91. void TransformRegister();
  92. void CipherResynchronize(const byte *iv, size_t length);
  93. void SetFeedbackSize(unsigned int feedbackSize);
  94. void ResizeBuffers();
  95. SecByteBlock m_temp;
  96. unsigned int m_feedbackSize;
  97. };
  98. inline void CopyOrZero(void *dest, const void *src, size_t s)
  99. {
  100. if (src)
  101. memcpy_s(dest, s, src, s);
  102. else
  103. memset(dest, 0, s);
  104. }
  105. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE OFB_ModePolicy : public ModePolicyCommonTemplate<AdditiveCipherAbstractPolicy>
  106. {
  107. public:
  108. bool CipherIsRandomAccess() const {return false;}
  109. IV_Requirement IVRequirement() const {return UNIQUE_IV;}
  110. static const char * CRYPTOPP_API StaticAlgorithmName() {return "OFB";}
  111. private:
  112. unsigned int GetBytesPerIteration() const {return BlockSize();}
  113. unsigned int GetIterationsToBuffer() const {return m_cipher->OptimalNumberOfParallelBlocks();}
  114. void WriteKeystream(byte *keystreamBuffer, size_t iterationCount);
  115. void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
  116. };
  117. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CTR_ModePolicy : public ModePolicyCommonTemplate<AdditiveCipherAbstractPolicy>
  118. {
  119. public:
  120. bool CipherIsRandomAccess() const {return true;}
  121. IV_Requirement IVRequirement() const {return RANDOM_IV;}
  122. static const char * CRYPTOPP_API StaticAlgorithmName() {return "CTR";}
  123. protected:
  124. virtual void IncrementCounterBy256();
  125. unsigned int GetAlignment() const {return m_cipher->OptimalDataAlignment();}
  126. unsigned int GetBytesPerIteration() const {return BlockSize();}
  127. unsigned int GetIterationsToBuffer() const {return m_cipher->OptimalNumberOfParallelBlocks();}
  128. void WriteKeystream(byte *buffer, size_t iterationCount)
  129. {OperateKeystream(WRITE_KEYSTREAM, buffer, NULL, iterationCount);}
  130. bool CanOperateKeystream() const {return true;}
  131. void OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount);
  132. void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
  133. void SeekToIteration(lword iterationCount);
  134. AlignedSecByteBlock m_counterArray;
  135. };
  136. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE BlockOrientedCipherModeBase : public CipherModeBase
  137. {
  138. public:
  139. void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params);
  140. unsigned int MandatoryBlockSize() const {return BlockSize();}
  141. bool IsRandomAccess() const {return false;}
  142. bool IsSelfInverting() const {return false;}
  143. bool IsForwardTransformation() const {return m_cipher->IsForwardTransformation();}
  144. void Resynchronize(const byte *iv, int length=-1) {memcpy_s(m_register, m_register.size(), iv, ThrowIfInvalidIVLength(length));}
  145. protected:
  146. bool RequireAlignedInput() const {return true;}
  147. void ResizeBuffers()
  148. {
  149. CipherModeBase::ResizeBuffers();
  150. m_buffer.New(BlockSize());
  151. }
  152. SecByteBlock m_buffer;
  153. };
  154. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE ECB_OneWay : public BlockOrientedCipherModeBase
  155. {
  156. public:
  157. void SetKey(const byte *key, size_t length, const NameValuePairs &params = g_nullNameValuePairs)
  158. {m_cipher->SetKey(key, length, params); BlockOrientedCipherModeBase::ResizeBuffers();}
  159. IV_Requirement IVRequirement() const {return NOT_RESYNCHRONIZABLE;}
  160. unsigned int OptimalBlockSize() const {return BlockSize() * m_cipher->OptimalNumberOfParallelBlocks();}
  161. void ProcessData(byte *outString, const byte *inString, size_t length);
  162. static const char * CRYPTOPP_API StaticAlgorithmName() {return "ECB";}
  163. };
  164. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_ModeBase : public BlockOrientedCipherModeBase
  165. {
  166. public:
  167. IV_Requirement IVRequirement() const {return UNPREDICTABLE_RANDOM_IV;}
  168. bool RequireAlignedInput() const {return false;}
  169. unsigned int MinLastBlockSize() const {return 0;}
  170. static const char * CRYPTOPP_API StaticAlgorithmName() {return "CBC";}
  171. };
  172. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_Encryption : public CBC_ModeBase
  173. {
  174. public:
  175. void ProcessData(byte *outString, const byte *inString, size_t length);
  176. };
  177. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_CTS_Encryption : public CBC_Encryption
  178. {
  179. public:
  180. void SetStolenIV(byte *iv) {m_stolenIV = iv;}
  181. unsigned int MinLastBlockSize() const {return BlockSize()+1;}
  182. void ProcessLastBlock(byte *outString, const byte *inString, size_t length);
  183. static const char * CRYPTOPP_API StaticAlgorithmName() {return "CBC/CTS";}
  184. protected:
  185. void UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &params)
  186. {
  187. CBC_Encryption::UncheckedSetKey(key, length, params);
  188. m_stolenIV = params.GetValueWithDefault(Name::StolenIV(), (byte *)NULL);
  189. }
  190. byte *m_stolenIV;
  191. };
  192. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_Decryption : public CBC_ModeBase
  193. {
  194. public:
  195. void ProcessData(byte *outString, const byte *inString, size_t length);
  196. protected:
  197. void ResizeBuffers()
  198. {
  199. BlockOrientedCipherModeBase::ResizeBuffers();
  200. m_temp.New(BlockSize());
  201. }
  202. AlignedSecByteBlock m_temp;
  203. };
  204. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_CTS_Decryption : public CBC_Decryption
  205. {
  206. public:
  207. unsigned int MinLastBlockSize() const {return BlockSize()+1;}
  208. void ProcessLastBlock(byte *outString, const byte *inString, size_t length);
  209. };
  210. //! _
  211. template <class CIPHER, class BASE>
  212. class CipherModeFinalTemplate_CipherHolder : protected ObjectHolder<CIPHER>, public AlgorithmImpl<BASE, CipherModeFinalTemplate_CipherHolder<CIPHER, BASE> >
  213. {
  214. public:
  215. CipherModeFinalTemplate_CipherHolder()
  216. {
  217. this->m_cipher = &this->m_object;
  218. this->ResizeBuffers();
  219. }
  220. CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length)
  221. {
  222. this->m_cipher = &this->m_object;
  223. this->SetKey(key, length);
  224. }
  225. CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv)
  226. {
  227. this->m_cipher = &this->m_object;
  228. this->SetKey(key, length, MakeParameters(Name::IV(), ConstByteArrayParameter(iv, this->m_cipher->BlockSize())));
  229. }
  230. CipherModeFinalTemplate_CipherHolder(const byte *key, size_t length, const byte *iv, int feedbackSize)
  231. {
  232. this->m_cipher = &this->m_object;
  233. this->SetKey(key, length, MakeParameters(Name::IV(), ConstByteArrayParameter(iv, this->m_cipher->BlockSize()))(Name::FeedbackSize(), feedbackSize));
  234. }
  235. static std::string CRYPTOPP_API StaticAlgorithmName()
  236. {return CIPHER::StaticAlgorithmName() + "/" + BASE::StaticAlgorithmName();}
  237. };
  238. //! _
  239. template <class BASE>
  240. class CipherModeFinalTemplate_ExternalCipher : public BASE
  241. {
  242. public:
  243. CipherModeFinalTemplate_ExternalCipher() {}
  244. CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher)
  245. {this->SetCipher(cipher);}
  246. CipherModeFinalTemplate_ExternalCipher(BlockCipher &cipher, const byte *iv, int feedbackSize = 0)
  247. {this->SetCipherWithIV(cipher, iv, feedbackSize);}
  248. std::string AlgorithmName() const
  249. {return (this->m_cipher ? this->m_cipher->AlgorithmName() + "/" : std::string("")) + BASE::StaticAlgorithmName();}
  250. };
  251. CRYPTOPP_DLL_TEMPLATE_CLASS CFB_CipherTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
  252. CRYPTOPP_DLL_TEMPLATE_CLASS CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
  253. CRYPTOPP_DLL_TEMPLATE_CLASS CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> >;
  254. //! CFB mode
  255. template <class CIPHER>
  256. struct CFB_Mode : public CipherModeDocumentation
  257. {
  258. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, ConcretePolicyHolder<Empty, CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > Encryption;
  259. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, ConcretePolicyHolder<Empty, CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > Decryption;
  260. };
  261. //! CFB mode, external cipher
  262. struct CFB_Mode_ExternalCipher : public CipherModeDocumentation
  263. {
  264. typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > Encryption;
  265. typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > Decryption;
  266. };
  267. //! CFB mode FIPS variant, requiring full block plaintext according to FIPS 800-38A
  268. template <class CIPHER>
  269. struct CFB_FIPS_Mode : public CipherModeDocumentation
  270. {
  271. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, ConcretePolicyHolder<Empty, CFB_RequireFullDataBlocks<CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > > Encryption;
  272. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, ConcretePolicyHolder<Empty, CFB_RequireFullDataBlocks<CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > > Decryption;
  273. };
  274. //! CFB mode FIPS variant, requiring full block plaintext according to FIPS 800-38A, external cipher
  275. struct CFB_FIPS_Mode_ExternalCipher : public CipherModeDocumentation
  276. {
  277. typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, CFB_RequireFullDataBlocks<CFB_EncryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > > Encryption;
  278. typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, CFB_RequireFullDataBlocks<CFB_DecryptionTemplate<AbstractPolicyHolder<CFB_CipherAbstractPolicy, CFB_ModePolicy> > > > > Decryption;
  279. };
  280. CRYPTOPP_DLL_TEMPLATE_CLASS AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, OFB_ModePolicy> >;
  281. //! OFB mode
  282. template <class CIPHER>
  283. struct OFB_Mode : public CipherModeDocumentation
  284. {
  285. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, OFB_ModePolicy> > > > Encryption;
  286. typedef Encryption Decryption;
  287. };
  288. //! OFB mode, external cipher
  289. struct OFB_Mode_ExternalCipher : public CipherModeDocumentation
  290. {
  291. typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, OFB_ModePolicy> > > > Encryption;
  292. typedef Encryption Decryption;
  293. };
  294. CRYPTOPP_DLL_TEMPLATE_CLASS AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, CTR_ModePolicy> >;
  295. CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, CTR_ModePolicy> > > >;
  296. //! CTR mode
  297. template <class CIPHER>
  298. struct CTR_Mode : public CipherModeDocumentation
  299. {
  300. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, CTR_ModePolicy> > > > Encryption;
  301. typedef Encryption Decryption;
  302. };
  303. //! CTR mode, external cipher
  304. struct CTR_Mode_ExternalCipher : public CipherModeDocumentation
  305. {
  306. typedef CipherModeFinalTemplate_ExternalCipher<ConcretePolicyHolder<Empty, AdditiveCipherTemplate<AbstractPolicyHolder<AdditiveCipherAbstractPolicy, CTR_ModePolicy> > > > Encryption;
  307. typedef Encryption Decryption;
  308. };
  309. //! ECB mode
  310. template <class CIPHER>
  311. struct ECB_Mode : public CipherModeDocumentation
  312. {
  313. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, ECB_OneWay> Encryption;
  314. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Decryption, ECB_OneWay> Decryption;
  315. };
  316. CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<ECB_OneWay>;
  317. //! ECB mode, external cipher
  318. struct ECB_Mode_ExternalCipher : public CipherModeDocumentation
  319. {
  320. typedef CipherModeFinalTemplate_ExternalCipher<ECB_OneWay> Encryption;
  321. typedef Encryption Decryption;
  322. };
  323. //! CBC mode
  324. template <class CIPHER>
  325. struct CBC_Mode : public CipherModeDocumentation
  326. {
  327. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, CBC_Encryption> Encryption;
  328. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Decryption, CBC_Decryption> Decryption;
  329. };
  330. CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<CBC_Encryption>;
  331. CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<CBC_Decryption>;
  332. //! CBC mode, external cipher
  333. struct CBC_Mode_ExternalCipher : public CipherModeDocumentation
  334. {
  335. typedef CipherModeFinalTemplate_ExternalCipher<CBC_Encryption> Encryption;
  336. typedef CipherModeFinalTemplate_ExternalCipher<CBC_Decryption> Decryption;
  337. };
  338. //! CBC mode with ciphertext stealing
  339. template <class CIPHER>
  340. struct CBC_CTS_Mode : public CipherModeDocumentation
  341. {
  342. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Encryption, CBC_CTS_Encryption> Encryption;
  343. typedef CipherModeFinalTemplate_CipherHolder<CPP_TYPENAME CIPHER::Decryption, CBC_CTS_Decryption> Decryption;
  344. };
  345. CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<CBC_CTS_Encryption>;
  346. CRYPTOPP_DLL_TEMPLATE_CLASS CipherModeFinalTemplate_ExternalCipher<CBC_CTS_Decryption>;
  347. //! CBC mode with ciphertext stealing, external cipher
  348. struct CBC_CTS_Mode_ExternalCipher : public CipherModeDocumentation
  349. {
  350. typedef CipherModeFinalTemplate_ExternalCipher<CBC_CTS_Encryption> Encryption;
  351. typedef CipherModeFinalTemplate_ExternalCipher<CBC_CTS_Decryption> Decryption;
  352. };
  353. #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
  354. typedef CFB_Mode_ExternalCipher::Encryption CFBEncryption;
  355. typedef CFB_Mode_ExternalCipher::Decryption CFBDecryption;
  356. typedef OFB_Mode_ExternalCipher::Encryption OFB;
  357. typedef CTR_Mode_ExternalCipher::Encryption CounterMode;
  358. #endif
  359. NAMESPACE_END
  360. #endif