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.

289 lines
11 KiB

  1. // simple.h - written and placed in the public domain by Wei Dai
  2. //! \file simple.h
  3. //! \brief Classes providing simple keying interfaces.
  4. #ifndef CRYPTOPP_SIMPLE_H
  5. #define CRYPTOPP_SIMPLE_H
  6. #include "config.h"
  7. #if CRYPTOPP_MSC_VERSION
  8. # pragma warning(push)
  9. # pragma warning(disable: 4127 4189)
  10. #endif
  11. #include "cryptlib.h"
  12. #include "misc.h"
  13. NAMESPACE_BEGIN(CryptoPP)
  14. //! \class ClonableImpl
  15. //! \brief Base class for identifying alogorithm
  16. //! \tparam BASE base class from which to derive
  17. //! \tparam DERIVED class which to clone
  18. template <class DERIVED, class BASE>
  19. class CRYPTOPP_NO_VTABLE ClonableImpl : public BASE
  20. {
  21. public:
  22. Clonable * Clone() const {return new DERIVED(*static_cast<const DERIVED *>(this));}
  23. };
  24. //! \class AlgorithmImpl
  25. //! \brief Base class for identifying alogorithm
  26. //! \tparam BASE an Algorithm derived class
  27. //! \tparam ALGORITHM_INFO an Algorithm derived class
  28. //! \details AlgorithmImpl provides StaticAlgorithmName from the template parameter BASE
  29. template <class BASE, class ALGORITHM_INFO=BASE>
  30. class CRYPTOPP_NO_VTABLE AlgorithmImpl : public BASE
  31. {
  32. public:
  33. static std::string CRYPTOPP_API StaticAlgorithmName() {return ALGORITHM_INFO::StaticAlgorithmName();}
  34. std::string AlgorithmName() const {return ALGORITHM_INFO::StaticAlgorithmName();}
  35. };
  36. //! \class InvalidKeyLength
  37. //! \brief Exception thrown when an invalid key length is encountered
  38. class CRYPTOPP_DLL InvalidKeyLength : public InvalidArgument
  39. {
  40. public:
  41. explicit InvalidKeyLength(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid key length") {}
  42. };
  43. //! \class InvalidRounds
  44. //! \brief Exception thrown when an invalid number of rounds is encountered
  45. class CRYPTOPP_DLL InvalidRounds : public InvalidArgument
  46. {
  47. public:
  48. explicit InvalidRounds(const std::string &algorithm, unsigned int rounds) : InvalidArgument(algorithm + ": " + IntToString(rounds) + " is not a valid number of rounds") {}
  49. };
  50. // *****************************
  51. //! \class Bufferless
  52. //! \brief Base class for bufferless filters
  53. //! \tparam T the class or type
  54. template <class T>
  55. class CRYPTOPP_NO_VTABLE Bufferless : public T
  56. {
  57. public:
  58. bool IsolatedFlush(bool hardFlush, bool blocking)
  59. {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); return false;}
  60. };
  61. //! \class Unflushable
  62. //! \brief Base class for unflushable filters
  63. //! \tparam T the class or type
  64. template <class T>
  65. class CRYPTOPP_NO_VTABLE Unflushable : public T
  66. {
  67. public:
  68. bool Flush(bool completeFlush, int propagation=-1, bool blocking=true)
  69. {return ChannelFlush(DEFAULT_CHANNEL, completeFlush, propagation, blocking);}
  70. bool IsolatedFlush(bool hardFlush, bool blocking)
  71. {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); assert(false); return false;}
  72. bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true)
  73. {
  74. if (hardFlush && !InputBufferIsEmpty())
  75. throw CannotFlush("Unflushable<T>: this object has buffered input that cannot be flushed");
  76. else
  77. {
  78. BufferedTransformation *attached = this->AttachedTransformation();
  79. return attached && propagation ? attached->ChannelFlush(channel, hardFlush, propagation-1, blocking) : false;
  80. }
  81. }
  82. protected:
  83. virtual bool InputBufferIsEmpty() const {return false;}
  84. };
  85. //! \class InputRejecting
  86. //! \brief Base class for input rejecting filters
  87. //! \tparam T the class or type
  88. //! \details T should be a BufferedTransformation derived class
  89. template <class T>
  90. class CRYPTOPP_NO_VTABLE InputRejecting : public T
  91. {
  92. public:
  93. struct InputRejected : public NotImplemented
  94. {InputRejected() : NotImplemented("BufferedTransformation: this object doesn't allow input") {}};
  95. //! \name INPUT
  96. //@{
  97. //! \brief Input a byte array for processing
  98. //! \param inString the byte array to process
  99. //! \param length the size of the string, in bytes
  100. //! \param messageEnd means how many filters to signal MessageEnd() to, including this one
  101. //! \param blocking specifies whether the object should block when processing input
  102. //! \throws InputRejected
  103. //! \returns the number of bytes that remain in the block (i.e., bytes not processed)
  104. //! \details Internally, the default implmentation throws InputRejected.
  105. size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
  106. {CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); throw InputRejected();}
  107. //@}
  108. //! \name SIGNALS
  109. //@{
  110. bool IsolatedFlush(bool hardFlush, bool blocking)
  111. {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); return false;}
  112. bool IsolatedMessageSeriesEnd(bool blocking)
  113. {CRYPTOPP_UNUSED(blocking); throw InputRejected();}
  114. size_t ChannelPut2(const std::string &channel, const byte *inString, size_t length, int messageEnd, bool blocking)
  115. {CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); throw InputRejected();}
  116. bool ChannelMessageSeriesEnd(const std::string& channel, int messageEnd, bool blocking)
  117. {CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); throw InputRejected();}
  118. //@}
  119. };
  120. //! \class CustomFlushPropagation
  121. //! \brief Provides interface for custom flush signals
  122. //! \tparam T the class or type
  123. //! \details T should be a BufferedTransformation derived class
  124. template <class T>
  125. class CRYPTOPP_NO_VTABLE CustomFlushPropagation : public T
  126. {
  127. public:
  128. //! \name SIGNALS
  129. //@{
  130. virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) =0;
  131. //@}
  132. private:
  133. bool IsolatedFlush(bool hardFlush, bool blocking)
  134. {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); assert(false); return false;}
  135. };
  136. //! \class CustomSignalPropagation
  137. //! \brief Provides interface for initialization of derived filters
  138. //! \tparam T the class or type
  139. //! \details T should be a BufferedTransformation derived class
  140. template <class T>
  141. class CRYPTOPP_NO_VTABLE CustomSignalPropagation : public CustomFlushPropagation<T>
  142. {
  143. public:
  144. virtual void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1) =0;
  145. private:
  146. void IsolatedInitialize(const NameValuePairs &parameters)
  147. {CRYPTOPP_UNUSED(parameters); assert(false);}
  148. };
  149. //! \class Multichannel
  150. //! \brief Provides multiple channels support for custom flush signal processing
  151. //! \tparam T the class or type
  152. //! \details T should be a BufferedTransformation derived class
  153. template <class T>
  154. class CRYPTOPP_NO_VTABLE Multichannel : public CustomFlushPropagation<T>
  155. {
  156. public:
  157. bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
  158. {return this->ChannelFlush(DEFAULT_CHANNEL, hardFlush, propagation, blocking);}
  159. bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
  160. {return this->ChannelMessageSeriesEnd(DEFAULT_CHANNEL, propagation, blocking);}
  161. byte * CreatePutSpace(size_t &size)
  162. {return this->ChannelCreatePutSpace(DEFAULT_CHANNEL, size);}
  163. size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
  164. {return this->ChannelPut2(DEFAULT_CHANNEL, inString, length, messageEnd, blocking);}
  165. size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
  166. {return this->ChannelPutModifiable2(DEFAULT_CHANNEL, inString, length, messageEnd, blocking);}
  167. // void ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1)
  168. // {PropagateMessageSeriesEnd(propagation, channel);}
  169. byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
  170. {CRYPTOPP_UNUSED(channel); size = 0; return NULL;}
  171. bool ChannelPutModifiable(const std::string &channel, byte *inString, size_t length)
  172. {this->ChannelPut(channel, inString, length); return false;}
  173. virtual size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking) =0;
  174. size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
  175. {return ChannelPut2(channel, begin, length, messageEnd, blocking);}
  176. virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true) =0;
  177. };
  178. //! \class AutoSignaling
  179. //! \brief Provides auto signaling support
  180. //! \tparam T the class or type
  181. //! \details T should be a BufferedTransformation derived class
  182. template <class T>
  183. class CRYPTOPP_NO_VTABLE AutoSignaling : public T
  184. {
  185. public:
  186. AutoSignaling(int propagation=-1) : m_autoSignalPropagation(propagation) {}
  187. void SetAutoSignalPropagation(int propagation)
  188. {m_autoSignalPropagation = propagation;}
  189. int GetAutoSignalPropagation() const
  190. {return m_autoSignalPropagation;}
  191. private:
  192. int m_autoSignalPropagation;
  193. };
  194. //! \class Store
  195. //! \brief Acts as a Source for pre-existing, static data
  196. //! \tparam T the class or type
  197. //! \details A BufferedTransformation that only contains pre-existing data as "output"
  198. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Store : public AutoSignaling<InputRejecting<BufferedTransformation> >
  199. {
  200. public:
  201. Store() : m_messageEnd(false) {}
  202. void IsolatedInitialize(const NameValuePairs &parameters)
  203. {
  204. m_messageEnd = false;
  205. StoreInitialize(parameters);
  206. }
  207. unsigned int NumberOfMessages() const {return m_messageEnd ? 0 : 1;}
  208. bool GetNextMessage();
  209. unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=DEFAULT_CHANNEL) const;
  210. protected:
  211. virtual void StoreInitialize(const NameValuePairs &parameters) =0;
  212. bool m_messageEnd;
  213. };
  214. //! \class Sink
  215. //! \brief Implementation of BufferedTransformation's attachment interface
  216. //! \details Sink is a cornerstone of the Pipeline trinitiy. Data flows from
  217. //! Sources, through Filters, and then terminates in Sinks. The difference
  218. //! between a Source and Filter is a Source \a pumps data, while a Filter does
  219. //! not. The difference between a Filter and a Sink is a Filter allows an
  220. //! attached transformation, while a Sink does not.
  221. //! \details A Sink doesnot produce any retrievable output.
  222. //! \details See the discussion of BufferedTransformation in cryptlib.h for
  223. //! more details.
  224. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Sink : public BufferedTransformation
  225. {
  226. public:
  227. size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
  228. {CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(transferBytes); CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking); transferBytes = 0; return 0;}
  229. size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
  230. {CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(begin); CRYPTOPP_UNUSED(end); CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking); return 0;}
  231. };
  232. //! \class BitBucket
  233. //! \brief Acts as an input discarding Filter or Sink
  234. //! \tparam T the class or type
  235. //! \details The BitBucket discards all input and returns 0 to the caller
  236. //! to indicate all data was processed.
  237. class CRYPTOPP_DLL BitBucket : public Bufferless<Sink>
  238. {
  239. public:
  240. std::string AlgorithmName() const {return "BitBucket";}
  241. void IsolatedInitialize(const NameValuePairs &params)
  242. {CRYPTOPP_UNUSED(params);}
  243. size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
  244. {CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); return 0;}
  245. };
  246. NAMESPACE_END
  247. #if CRYPTOPP_MSC_VERSION
  248. # pragma warning(pop)
  249. #endif
  250. #endif