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