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.

221 lines
7.2 KiB

  1. // seckey.h - written and placed in the public domain by Wei Dai
  2. // This file contains helper classes/functions for implementing secret key algorithms.
  3. #ifndef CRYPTOPP_SECKEY_H
  4. #define CRYPTOPP_SECKEY_H
  5. #include "cryptlib.h"
  6. #include "misc.h"
  7. #include "simple.h"
  8. NAMESPACE_BEGIN(CryptoPP)
  9. inline CipherDir ReverseCipherDir(CipherDir dir)
  10. {
  11. return (dir == ENCRYPTION) ? DECRYPTION : ENCRYPTION;
  12. }
  13. //! to be inherited by block ciphers with fixed block size
  14. template <unsigned int N>
  15. class FixedBlockSize
  16. {
  17. public:
  18. CRYPTOPP_CONSTANT(BLOCKSIZE = N)
  19. };
  20. // ************** rounds ***************
  21. //! to be inherited by ciphers with fixed number of rounds
  22. template <unsigned int R>
  23. class FixedRounds
  24. {
  25. public:
  26. CRYPTOPP_CONSTANT(ROUNDS = R)
  27. };
  28. //! to be inherited by ciphers with variable number of rounds
  29. template <unsigned int D, unsigned int N=1, unsigned int M=INT_MAX> // use INT_MAX here because enums are treated as signed ints
  30. class VariableRounds
  31. {
  32. public:
  33. CRYPTOPP_CONSTANT(DEFAULT_ROUNDS = D)
  34. CRYPTOPP_CONSTANT(MIN_ROUNDS = N)
  35. CRYPTOPP_CONSTANT(MAX_ROUNDS = M)
  36. static unsigned int StaticGetDefaultRounds(size_t keylength) {return DEFAULT_ROUNDS;}
  37. protected:
  38. inline void ThrowIfInvalidRounds(int rounds, const Algorithm *alg)
  39. {
  40. if (rounds < MIN_ROUNDS || rounds > MAX_ROUNDS)
  41. throw InvalidRounds(alg->AlgorithmName(), rounds);
  42. }
  43. inline unsigned int GetRoundsAndThrowIfInvalid(const NameValuePairs &param, const Algorithm *alg)
  44. {
  45. int rounds = param.GetIntValueWithDefault("Rounds", DEFAULT_ROUNDS);
  46. ThrowIfInvalidRounds(rounds, alg);
  47. return (unsigned int)rounds;
  48. }
  49. };
  50. // ************** key length ***************
  51. //! to be inherited by keyed algorithms with fixed key length
  52. template <unsigned int N, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE, unsigned int IV_L = 0>
  53. class FixedKeyLength
  54. {
  55. public:
  56. CRYPTOPP_CONSTANT(KEYLENGTH=N)
  57. CRYPTOPP_CONSTANT(MIN_KEYLENGTH=N)
  58. CRYPTOPP_CONSTANT(MAX_KEYLENGTH=N)
  59. CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=N)
  60. CRYPTOPP_CONSTANT(IV_REQUIREMENT = IV_REQ)
  61. CRYPTOPP_CONSTANT(IV_LENGTH = IV_L)
  62. static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t) {return KEYLENGTH;}
  63. };
  64. /// support query of variable key length, template parameters are default, min, max, multiple (default multiple 1)
  65. 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>
  66. class VariableKeyLength
  67. {
  68. // make these private to avoid Doxygen documenting them in all derived classes
  69. CRYPTOPP_COMPILE_ASSERT(Q > 0);
  70. CRYPTOPP_COMPILE_ASSERT(N % Q == 0);
  71. CRYPTOPP_COMPILE_ASSERT(M % Q == 0);
  72. CRYPTOPP_COMPILE_ASSERT(N < M);
  73. CRYPTOPP_COMPILE_ASSERT(D >= N);
  74. CRYPTOPP_COMPILE_ASSERT(M >= D);
  75. public:
  76. CRYPTOPP_CONSTANT(MIN_KEYLENGTH=N)
  77. CRYPTOPP_CONSTANT(MAX_KEYLENGTH=M)
  78. CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=D)
  79. CRYPTOPP_CONSTANT(KEYLENGTH_MULTIPLE=Q)
  80. CRYPTOPP_CONSTANT(IV_REQUIREMENT=IV_REQ)
  81. CRYPTOPP_CONSTANT(IV_LENGTH=IV_L)
  82. static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t n)
  83. {
  84. if (n < (size_t)MIN_KEYLENGTH)
  85. return MIN_KEYLENGTH;
  86. else if (n > (size_t)MAX_KEYLENGTH)
  87. return (size_t)MAX_KEYLENGTH;
  88. else
  89. {
  90. n += KEYLENGTH_MULTIPLE-1;
  91. return n - n%KEYLENGTH_MULTIPLE;
  92. }
  93. }
  94. };
  95. /// support query of key length that's the same as another class
  96. template <class T, unsigned int IV_REQ = SimpleKeyingInterface::NOT_RESYNCHRONIZABLE, unsigned int IV_L = 0>
  97. class SameKeyLengthAs
  98. {
  99. public:
  100. CRYPTOPP_CONSTANT(MIN_KEYLENGTH=T::MIN_KEYLENGTH)
  101. CRYPTOPP_CONSTANT(MAX_KEYLENGTH=T::MAX_KEYLENGTH)
  102. CRYPTOPP_CONSTANT(DEFAULT_KEYLENGTH=T::DEFAULT_KEYLENGTH)
  103. CRYPTOPP_CONSTANT(IV_REQUIREMENT=IV_REQ)
  104. CRYPTOPP_CONSTANT(IV_LENGTH=IV_L)
  105. static size_t CRYPTOPP_API StaticGetValidKeyLength(size_t keylength)
  106. {return T::StaticGetValidKeyLength(keylength);}
  107. };
  108. // ************** implementation helper for SimpleKeyed ***************
  109. //! _
  110. template <class BASE, class INFO = BASE>
  111. class CRYPTOPP_NO_VTABLE SimpleKeyingInterfaceImpl : public BASE
  112. {
  113. public:
  114. size_t MinKeyLength() const {return INFO::MIN_KEYLENGTH;}
  115. size_t MaxKeyLength() const {return (size_t)INFO::MAX_KEYLENGTH;}
  116. size_t DefaultKeyLength() const {return INFO::DEFAULT_KEYLENGTH;}
  117. size_t GetValidKeyLength(size_t n) const {return INFO::StaticGetValidKeyLength(n);}
  118. SimpleKeyingInterface::IV_Requirement IVRequirement() const {return (SimpleKeyingInterface::IV_Requirement)INFO::IV_REQUIREMENT;}
  119. unsigned int IVSize() const {return INFO::IV_LENGTH;}
  120. };
  121. template <class INFO, class BASE = BlockCipher>
  122. class CRYPTOPP_NO_VTABLE BlockCipherImpl : public AlgorithmImpl<SimpleKeyingInterfaceImpl<TwoBases<BASE, INFO> > >
  123. {
  124. public:
  125. unsigned int BlockSize() const {return this->BLOCKSIZE;}
  126. };
  127. //! _
  128. template <CipherDir DIR, class BASE>
  129. class BlockCipherFinal : public ClonableImpl<BlockCipherFinal<DIR, BASE>, BASE>
  130. {
  131. public:
  132. BlockCipherFinal() {}
  133. BlockCipherFinal(const byte *key)
  134. {this->SetKey(key, this->DEFAULT_KEYLENGTH);}
  135. BlockCipherFinal(const byte *key, size_t length)
  136. {this->SetKey(key, length);}
  137. BlockCipherFinal(const byte *key, size_t length, unsigned int rounds)
  138. {this->SetKeyWithRounds(key, length, rounds);}
  139. bool IsForwardTransformation() const {return DIR == ENCRYPTION;}
  140. };
  141. //! _
  142. template <class BASE, class INFO = BASE>
  143. class MessageAuthenticationCodeImpl : public AlgorithmImpl<SimpleKeyingInterfaceImpl<BASE, INFO>, INFO>
  144. {
  145. };
  146. //! _
  147. template <class BASE>
  148. class MessageAuthenticationCodeFinal : public ClonableImpl<MessageAuthenticationCodeFinal<BASE>, MessageAuthenticationCodeImpl<BASE> >
  149. {
  150. public:
  151. MessageAuthenticationCodeFinal() {}
  152. MessageAuthenticationCodeFinal(const byte *key)
  153. {this->SetKey(key, this->DEFAULT_KEYLENGTH);}
  154. MessageAuthenticationCodeFinal(const byte *key, size_t length)
  155. {this->SetKey(key, length);}
  156. };
  157. // ************** documentation ***************
  158. //! These objects usually should not be used directly. See CipherModeDocumentation instead.
  159. /*! Each class derived from this one defines two types, Encryption and Decryption,
  160. both of which implement the BlockCipher interface. */
  161. struct BlockCipherDocumentation
  162. {
  163. //! implements the BlockCipher interface
  164. typedef BlockCipher Encryption;
  165. //! implements the BlockCipher interface
  166. typedef BlockCipher Decryption;
  167. };
  168. /*! \brief Each class derived from this one defines two types, Encryption and Decryption,
  169. both of which implement the SymmetricCipher interface. Two types of classes derive
  170. from this class: stream ciphers and block cipher modes. Stream ciphers can be used
  171. alone, cipher mode classes need to be used with a block cipher. See CipherModeDocumentation
  172. for more for information about using cipher modes and block ciphers. */
  173. struct SymmetricCipherDocumentation
  174. {
  175. //! implements the SymmetricCipher interface
  176. typedef SymmetricCipher Encryption;
  177. //! implements the SymmetricCipher interface
  178. typedef SymmetricCipher Decryption;
  179. };
  180. /*! \brief Each class derived from this one defines two types, Encryption and Decryption,
  181. both of which implement the AuthenticatedSymmetricCipher interface. */
  182. struct AuthenticatedSymmetricCipherDocumentation
  183. {
  184. //! implements the AuthenticatedSymmetricCipher interface
  185. typedef AuthenticatedSymmetricCipher Encryption;
  186. //! implements the AuthenticatedSymmetricCipher interface
  187. typedef AuthenticatedSymmetricCipher Decryption;
  188. };
  189. NAMESPACE_END
  190. #endif