Team Fortress 2 Source Code as on 22/4/2020
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.

428 lines
18 KiB

  1. // seckey.h - written and placed in the public domain by Wei Dai
  2. //! \file
  3. //! \brief Classes and functions for implementing secret key algorithms.
  4. #ifndef CRYPTOPP_SECKEY_H
  5. #define CRYPTOPP_SECKEY_H
  6. #include "config.h"
  7. #if CRYPTOPP_MSC_VERSION
  8. # pragma warning(push)
  9. # pragma warning(disable: 4189)
  10. #endif
  11. #include "cryptlib.h"
  12. #include "misc.h"
  13. #include "simple.h"
  14. NAMESPACE_BEGIN(CryptoPP)
  15. //! \brief Inverts the cipher's direction
  16. //! \param dir the cipher's direction
  17. //! \returns DECRYPTION if dir is ENCRYPTION, DECRYPTION otherwise
  18. inline CipherDir ReverseCipherDir(CipherDir dir)
  19. {
  20. return (dir == ENCRYPTION) ? DECRYPTION : ENCRYPTION;
  21. }
  22. //! \class FixedBlockSize
  23. //! \brief Inherited by block ciphers with fixed block size
  24. //! \tparam N the blocksize of the cipher
  25. template <unsigned int N>
  26. class FixedBlockSize
  27. {
  28. public:
  29. //! \brief The block size of the cipher provided as a constant.
  30. CRYPTOPP_CONSTANT(BLOCKSIZE = N)
  31. };
  32. // ************** rounds ***************
  33. //! \class FixedRounds
  34. //! \brief Inherited by ciphers with fixed number of rounds
  35. //! \tparam R the number of rounds used by the cipher
  36. template <unsigned int R>
  37. class FixedRounds
  38. {
  39. public:
  40. //! \brief The number of rounds for the cipher provided as a constant.
  41. CRYPTOPP_CONSTANT(ROUNDS = R)
  42. };
  43. //! \class VariableRounds
  44. //! \brief Inherited by ciphers with variable number of rounds
  45. //! \tparam D Default number of rounds
  46. //! \tparam N Minimum number of rounds
  47. //! \tparam D Maximum number of rounds
  48. template <unsigned int D, unsigned int N=1, unsigned int M=INT_MAX> // use INT_MAX here because enums are treated as signed ints
  49. class VariableRounds
  50. {
  51. public:
  52. //! \brief The default number of rounds for the cipher provided as a constant.
  53. CRYPTOPP_CONSTANT(DEFAULT_ROUNDS = D)
  54. //! \brief The minimum number of rounds for the cipher provided as a constant.
  55. CRYPTOPP_CONSTANT(MIN_ROUNDS = N)
  56. //! \brief The maximum number of rounds for the cipher provided as a constant.
  57. CRYPTOPP_CONSTANT(MAX_ROUNDS = M)
  58. //! \brief The default number of rounds for the cipher based on key length
  59. //! provided by a static function.
  60. //! \param keylength the size of the key, in bytes
  61. //! \details keylength is unused in the default implementation.
  62. static unsigned int StaticGetDefaultRounds(size_t keylength)
  63. {CRYPTOPP_UNUSED(keylength); return DEFAULT_ROUNDS;}
  64. protected:
  65. //! \brief Validates the number of rounds for a cipher.
  66. //! \param rounds the canddiate number of rounds
  67. //! \param alg an Algorithm object used if the number of rounds are invalid
  68. //! \throws InvalidRounds if the number of rounds are invalid
  69. inline void ThrowIfInvalidRounds(int rounds, const Algorithm *alg)
  70. {
  71. #if (M==INT_MAX) // Coverity and result_independent_of_operands
  72. if (rounds < MIN_ROUNDS)
  73. throw InvalidRounds(alg ? alg->AlgorithmName() : "VariableRounds", rounds);
  74. #else
  75. if (rounds < MIN_ROUNDS || rounds > MAX_ROUNDS)
  76. throw InvalidRounds(alg ? alg->AlgorithmName() : "VariableRounds", rounds);
  77. #endif
  78. }
  79. //! \brief Validates the number of rounds for a cipher
  80. //! \param param the canddiate number of rounds
  81. //! \param alg an Algorithm object used if the number of rounds are invalid
  82. //! \returns the number of rounds for the cipher
  83. //! \throws InvalidRounds if the number of rounds are invalid
  84. inline unsigned int GetRoundsAndThrowIfInvalid(const NameValuePairs &param, const Algorithm *alg)
  85. {
  86. int rounds = param.GetIntValueWithDefault("Rounds", DEFAULT_ROUNDS);
  87. ThrowIfInvalidRounds(rounds, alg);
  88. return (unsigned int)rounds;
  89. }
  90. };
  91. // ************** key length ***************
  92. //! \class FixedKeyLength
  93. //! \brief Inherited by keyed algorithms with fixed key length
  94. //! \tparam N Default key length, in bytes
  95. //! \tparam IV_REQ The IV requirements. See IV_Requirement in cryptlib.h for allowed values
  96. //! \tparam IV_L Default IV length, in bytes
  97. template <unsigned int N, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE, unsigned int IV_L = 0>
  98. class FixedKeyLength
  99. {
  100. public:
  101. //! \brief The default key length used by the cipher provided as a constant
  102. //! \details KEYLENGTH is provided in bytes, not bits
  103. CRYPTOPP_CONSTANT(KEYLENGTH=N)
  104. //! \brief The minimum key length used by the cipher provided as a constant
  105. //! \details MIN_KEYLENGTH is provided in bytes, not bits
  106. CRYPTOPP_CONSTANT(MIN_KEYLENGTH=N)
  107. //! \brief The maximum key length used by the cipher provided as a constant
  108. //! \details MAX_KEYLENGTH is provided in bytes, not bits
  109. CRYPTOPP_CONSTANT(MAX_KEYLENGTH=N)
  110. //! \brief The default key length used by the cipher provided as a constant
  111. //! \details DEFAULT_KEYLENGTH is provided in bytes, not bits
  112. CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=N)
  113. //! \brief The default IV requirements for the cipher provided as a constant
  114. //! \details The default value is NOT_RESYNCHRONIZABLE. See IV_Requirement
  115. //! in cryptlib.h for allowed values.
  116. CRYPTOPP_CONSTANT(IV_REQUIREMENT = IV_REQ)
  117. //! \brief The default IV length used by the cipher provided as a constant
  118. //! \details IV_LENGTH is provided in bytes, not bits. The default implementation uses 0.
  119. CRYPTOPP_CONSTANT(IV_LENGTH = IV_L)
  120. //! \brief The default key length for the cipher provided by a static function.
  121. //! \param keylength the size of the key, in bytes
  122. //! \details The default implementation returns KEYLENGTH. keylength is unused
  123. //! in the default implementation.
  124. static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
  125. {CRYPTOPP_UNUSED(keylength); return KEYLENGTH;}
  126. };
  127. //! \class VariableKeyLength
  128. //! \brief Inherited by keyed algorithms with variable key length
  129. //! \tparam D Default key length, in bytes
  130. //! \tparam N Minimum key length, in bytes
  131. //! \tparam M Maximum key length, in bytes
  132. //! \tparam M Default key length multiple, in bytes. The default multiple is 1.
  133. //! \tparam IV_REQ The IV requirements. See IV_Requirement in cryptlib.h for allowed values
  134. //! \tparam IV_L Default IV length, in bytes. The default length is 0.
  135. template <unsigned int D, unsigned int N, unsigned int M, unsigned int Q = 1, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE, unsigned int IV_L = 0>
  136. class VariableKeyLength
  137. {
  138. // Make these private to avoid Doxygen documenting them in all derived classes
  139. CRYPTOPP_COMPILE_ASSERT(Q > 0);
  140. CRYPTOPP_COMPILE_ASSERT(N % Q == 0);
  141. CRYPTOPP_COMPILE_ASSERT(M % Q == 0);
  142. CRYPTOPP_COMPILE_ASSERT(N < M);
  143. CRYPTOPP_COMPILE_ASSERT(D >= N);
  144. CRYPTOPP_COMPILE_ASSERT(M >= D);
  145. public:
  146. //! \brief The minimum key length used by the cipher provided as a constant
  147. //! \details MIN_KEYLENGTH is provided in bytes, not bits
  148. CRYPTOPP_CONSTANT(MIN_KEYLENGTH=N)
  149. //! \brief The maximum key length used by the cipher provided as a constant
  150. //! \details MAX_KEYLENGTH is provided in bytes, not bits
  151. CRYPTOPP_CONSTANT(MAX_KEYLENGTH=M)
  152. //! \brief The default key length used by the cipher provided as a constant
  153. //! \details DEFAULT_KEYLENGTH is provided in bytes, not bits
  154. CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=D)
  155. //! \brief The key length multiple used by the cipher provided as a constant
  156. //! \details MAX_KEYLENGTH is provided in bytes, not bits
  157. CRYPTOPP_CONSTANT(KEYLENGTH_MULTIPLE=Q)
  158. //! \brief The default IV requirements for the cipher provided as a constant
  159. //! \details The default value is NOT_RESYNCHRONIZABLE. See IV_Requirement
  160. //! in cryptlib.h for allowed values.
  161. CRYPTOPP_CONSTANT(IV_REQUIREMENT=IV_REQ)
  162. //! \brief The default initialization vector length for the cipher provided as a constant
  163. //! \details IV_LENGTH is provided in bytes, not bits. The default implementation uses 0.
  164. CRYPTOPP_CONSTANT(IV_LENGTH=IV_L)
  165. //! \brief Provides a valid key length for the cipher provided by a static function.
  166. //! \param keylength the size of the key, in bytes
  167. //! \details If keylength is less than MIN_KEYLENGTH, then the function returns
  168. //! MIN_KEYLENGTH. If keylength is greater than MAX_KEYLENGTH, then the function
  169. //! returns MAX_KEYLENGTH. If keylength is a multiple of KEYLENGTH_MULTIPLE,
  170. //! then keylength is returned. Otherwise, the function returns keylength rounded
  171. //! \a down to the next smaller multiple of KEYLENGTH_MULTIPLE.
  172. //! \details keylength is provided in bytes, not bits.
  173. static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
  174. {
  175. #if MIN_KEYLENGTH > 0
  176. if (keylength < (size_t)MIN_KEYLENGTH)
  177. return MIN_KEYLENGTH;
  178. else
  179. #endif
  180. if (keylength > (size_t)MAX_KEYLENGTH)
  181. return (size_t)MAX_KEYLENGTH;
  182. else
  183. {
  184. keylength += KEYLENGTH_MULTIPLE-1;
  185. return keylength - keylength%KEYLENGTH_MULTIPLE;
  186. }
  187. }
  188. };
  189. //! \class SameKeyLengthAs
  190. //! \brief Provides key lengths based on another class's key length
  191. //! \tparam T another FixedKeyLength or VariableKeyLength class
  192. //! \tparam IV_REQ The IV requirements. See IV_Requirement in cryptlib.h for allowed values
  193. //! \tparam IV_L Default IV length, in bytes
  194. template <class T, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE, unsigned int IV_L = 0>
  195. class SameKeyLengthAs
  196. {
  197. public:
  198. //! \brief The minimum key length used by the cipher provided as a constant
  199. //! \details MIN_KEYLENGTH is provided in bytes, not bits
  200. CRYPTOPP_CONSTANT(MIN_KEYLENGTH=T::MIN_KEYLENGTH)
  201. //! \brief The maximum key length used by the cipher provided as a constant
  202. //! \details MIN_KEYLENGTH is provided in bytes, not bits
  203. CRYPTOPP_CONSTANT(MAX_KEYLENGTH=T::MAX_KEYLENGTH)
  204. //! \brief The default key length used by the cipher provided as a constant
  205. //! \details MIN_KEYLENGTH is provided in bytes, not bits
  206. CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=T::DEFAULT_KEYLENGTH)
  207. //! \brief The default IV requirements for the cipher provided as a constant
  208. //! \details The default value is NOT_RESYNCHRONIZABLE. See IV_Requirement
  209. //! in cryptlib.h for allowed values.
  210. CRYPTOPP_CONSTANT(IV_REQUIREMENT=IV_REQ)
  211. //! \brief The default initialization vector length for the cipher provided as a constant
  212. //! \details IV_LENGTH is provided in bytes, not bits. The default implementation uses 0.
  213. CRYPTOPP_CONSTANT(IV_LENGTH=IV_L)
  214. //! \brief Provides a valid key length for the cipher provided by a static function.
  215. //! \param keylength the size of the key, in bytes
  216. //! \details If keylength is less than MIN_KEYLENGTH, then the function returns
  217. //! MIN_KEYLENGTH. If keylength is greater than MAX_KEYLENGTH, then the function
  218. //! returns MAX_KEYLENGTH. If keylength is a multiple of KEYLENGTH_MULTIPLE,
  219. //! then keylength is returned. Otherwise, the function returns keylength rounded
  220. //! \a down to the next smaller multiple of KEYLENGTH_MULTIPLE.
  221. //! \details keylength is provided in bytes, not bits.
  222. static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
  223. {return T::StaticGetValidKeyLength(keylength);}
  224. };
  225. // ************** implementation helper for SimpleKeyed ***************
  226. //! \class SimpleKeyingInterfaceImpl
  227. //! \brief Provides class member functions to access SimpleKeyingInterface constants
  228. //! \tparam BASE a SimpleKeyingInterface derived class
  229. //! \tparam INFO a SimpleKeyingInterface derived class
  230. template <class BASE, class INFO = BASE>
  231. class CRYPTOPP_NO_VTABLE SimpleKeyingInterfaceImpl : public BASE
  232. {
  233. public:
  234. //! \brief The minimum key length used by the cipher
  235. size_t MinKeyLength() const
  236. {return INFO::MIN_KEYLENGTH;}
  237. //! \brief The maximum key length used by the cipher
  238. size_t MaxKeyLength() const
  239. {return (size_t)INFO::MAX_KEYLENGTH;}
  240. //! \brief The default key length used by the cipher
  241. size_t DefaultKeyLength() const
  242. {return INFO::DEFAULT_KEYLENGTH;}
  243. //! \brief Provides a valid key length for the cipher
  244. //! \param keylength the size of the key, in bytes
  245. //! \details keylength is provided in bytes, not bits. If keylength is less than MIN_KEYLENGTH,
  246. //! then the function returns MIN_KEYLENGTH. If keylength is greater than MAX_KEYLENGTH,
  247. //! then the function returns MAX_KEYLENGTH. if If keylength is a multiple of KEYLENGTH_MULTIPLE,
  248. //! then keylength is returned. Otherwise, the function returns a \a lower multiple of
  249. //! KEYLENGTH_MULTIPLE.
  250. size_t GetValidKeyLength(size_t keylength) const {return INFO::StaticGetValidKeyLength(keylength);}
  251. //! \brief The default IV requirements for the cipher
  252. //! \details The default value is NOT_RESYNCHRONIZABLE. See IV_Requirement
  253. //! in cryptlib.h for allowed values.
  254. SimpleKeyingInterface::IV_Requirement IVRequirement() const
  255. {return (SimpleKeyingInterface::IV_Requirement)INFO::IV_REQUIREMENT;}
  256. //! \brief The default initialization vector length for the cipher
  257. //! \details IVSize is provided in bytes, not bits. The default implementation uses IV_LENGTH, which is 0.
  258. unsigned int IVSize() const
  259. {return INFO::IV_LENGTH;}
  260. };
  261. //! \class BlockCipherImpl
  262. //! \brief Provides class member functions to access BlockCipher constants
  263. //! \tparam INFO a SimpleKeyingInterface derived class
  264. //! \tparam BASE a SimpleKeyingInterface derived class
  265. template <class INFO, class BASE = BlockCipher>
  266. class CRYPTOPP_NO_VTABLE BlockCipherImpl : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<BASE, INFO> > >
  267. {
  268. public:
  269. //! Provides the block size of the cipher
  270. //! \returns the block size of the cipher, in bytes
  271. unsigned int BlockSize() const {return this->BLOCKSIZE;}
  272. };
  273. //! \class BlockCipherFinal
  274. //! \brief Provides class member functions to key a block cipher
  275. //! \tparam DIR a CipherDir
  276. //! \tparam BASE a BlockCipherImpl derived class
  277. template <CipherDir DIR, class BASE>
  278. class BlockCipherFinal : public ClonableImpl<BlockCipherFinal<DIR, BASE>, BASE>
  279. {
  280. public:
  281. //! \brief Construct a default BlockCipherFinal
  282. //! \details The cipher is not keyed.
  283. BlockCipherFinal() {}
  284. //! \brief Construct a BlockCipherFinal
  285. //! \param key a byte array used to key the cipher
  286. //! \details key must be at least DEFAULT_KEYLENGTH in length. Internally, the function calls
  287. //! SimpleKeyingInterface::SetKey.
  288. BlockCipherFinal(const byte *key)
  289. {this->SetKey(key, this->DEFAULT_KEYLENGTH);}
  290. //! \brief Construct a BlockCipherFinal
  291. //! \param key a byte array used to key the cipher
  292. //! \param length the length of the byte array
  293. //! \details key must be at least DEFAULT_KEYLENGTH in length. Internally, the function calls
  294. //! SimpleKeyingInterface::SetKey.
  295. BlockCipherFinal(const byte *key, size_t length)
  296. {this->SetKey(key, length);}
  297. //! \brief Construct a BlockCipherFinal
  298. //! \param key a byte array used to key the cipher
  299. //! \param length the length of the byte array
  300. //! \param rounds the number of rounds
  301. //! \details key must be at least DEFAULT_KEYLENGTH in length. Internally, the function calls
  302. //! SimpleKeyingInterface::SetKeyWithRounds.
  303. BlockCipherFinal(const byte *key, size_t length, unsigned int rounds)
  304. {this->SetKeyWithRounds(key, length, rounds);}
  305. //! \brief Provides the direction of the cipher
  306. //! \returns true if DIR is ENCRYPTION, false otherwise
  307. //! \sa IsForwardTransformation(), IsPermutation(), GetCipherDirection()
  308. bool IsForwardTransformation() const {return DIR == ENCRYPTION;}
  309. };
  310. //! \class MessageAuthenticationCodeImpl
  311. //! \brief Provides class member functions to access MessageAuthenticationCode constants
  312. //! \tparam INFO a SimpleKeyingInterface derived class
  313. //! \tparam BASE a SimpleKeyingInterface derived class
  314. template <class BASE, class INFO = BASE>
  315. class MessageAuthenticationCodeImpl : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BASE, INFO>, INFO>
  316. {
  317. };
  318. //! \class MessageAuthenticationCodeFinal
  319. //! \brief Provides class member functions to key a message authentication code
  320. //! \tparam DIR a CipherDir
  321. //! \tparam BASE a BlockCipherImpl derived class
  322. template <class BASE>
  323. class MessageAuthenticationCodeFinal : public ClonableImpl<MessageAuthenticationCodeFinal<BASE>, MessageAuthenticationCodeImpl<BASE> >
  324. {
  325. public:
  326. //! \brief Construct a default MessageAuthenticationCodeFinal
  327. //! \details The message authentication code is not keyed.
  328. MessageAuthenticationCodeFinal() {}
  329. //! \brief Construct a BlockCipherFinal
  330. //! \param key a byte array used to key the cipher
  331. //! \details key must be at least DEFAULT_KEYLENGTH in length. Internally, the function calls
  332. //! SimpleKeyingInterface::SetKey.
  333. MessageAuthenticationCodeFinal(const byte *key)
  334. {this->SetKey(key, this->DEFAULT_KEYLENGTH);}
  335. //! \brief Construct a BlockCipherFinal
  336. //! \param key a byte array used to key the cipher
  337. //! \param length the length of the byte array
  338. //! \details key must be at least DEFAULT_KEYLENGTH in length. Internally, the function calls
  339. //! SimpleKeyingInterface::SetKey.
  340. MessageAuthenticationCodeFinal(const byte *key, size_t length)
  341. {this->SetKey(key, length);}
  342. };
  343. // ************** documentation ***************
  344. //! \class BlockCipherDocumentation
  345. //! \brief Provides Encryption and Decryption typedefs used by derived classes to
  346. //! implement a block cipher
  347. //! \details These objects usually should not be used directly. See CipherModeDocumentation
  348. //! instead. Each class derived from this one defines two types, Encryption and Decryption,
  349. //! both of which implement the BlockCipher interface.
  350. struct BlockCipherDocumentation
  351. {
  352. //! implements the BlockCipher interface
  353. typedef BlockCipher Encryption;
  354. //! implements the BlockCipher interface
  355. typedef BlockCipher Decryption;
  356. };
  357. //! \class SymmetricCipherDocumentation
  358. //! \brief Provides Encryption and Decryption typedefs used by derived classes to
  359. //! implement a symmetric cipher
  360. //! \details Each class derived from this one defines two types, Encryption and Decryption,
  361. //! both of which implement the SymmetricCipher interface. Two types of classes derive
  362. //! from this class: stream ciphers and block cipher modes. Stream ciphers can be used
  363. //! alone, cipher mode classes need to be used with a block cipher. See CipherModeDocumentation
  364. //! for more for information about using cipher modes and block ciphers.
  365. struct SymmetricCipherDocumentation
  366. {
  367. //! implements the SymmetricCipher interface
  368. typedef SymmetricCipher Encryption;
  369. //! implements the SymmetricCipher interface
  370. typedef SymmetricCipher Decryption;
  371. };
  372. //! \class AuthenticatedSymmetricCipherDocumentation
  373. //! \brief Provides Encryption and Decryption typedefs used by derived classes to
  374. //! implement an authenticated encryption cipher
  375. //! \details Each class derived from this one defines two types, Encryption and Decryption,
  376. //! both of which implement the AuthenticatedSymmetricCipher interface.
  377. struct AuthenticatedSymmetricCipherDocumentation
  378. {
  379. //! implements the AuthenticatedSymmetricCipher interface
  380. typedef AuthenticatedSymmetricCipher Encryption;
  381. //! implements the AuthenticatedSymmetricCipher interface
  382. typedef AuthenticatedSymmetricCipher Decryption;
  383. };
  384. NAMESPACE_END
  385. #if CRYPTOPP_MSC_VERSION
  386. # pragma warning(pop)
  387. #endif
  388. #endif