Crypto++  5.6.5
Free C++ class library of cryptographic schemes
simple.h
Go to the documentation of this file.
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:
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 Interface for custom flush signals propagation
157 //! \tparam T BufferedTransformation derived class
158 template <class T>
159 class CRYPTOPP_NO_VTABLE CustomFlushPropagation : public T
160 {
161 public:
162  //! \name SIGNALS
163  //@{
164 
165  //! \brief Flush buffered input and/or output, with signal propagation
166  //! \param hardFlush is used to indicate whether all data should be flushed
167  //! \param propagation the number of attached transformations the Flush() signal should be passed
168  //! \param blocking specifies whether the object should block when processing input
169  //! \details propagation count includes this object. Setting propagation to <tt>1</tt> means this
170  //! object only. Setting propagation to <tt>-1</tt> means unlimited propagation.
171  //! \note Hard flushes must be used with care. It means try to process and output everything, even if
172  //! there may not be enough data to complete the action. For example, hard flushing a HexDecoder
173  //! would cause an error if you do it after inputing an odd number of hex encoded characters.
174  //! \note For some types of filters, like ZlibDecompressor, hard flushes can only
175  //! be done at "synchronization points". These synchronization points are positions in the data
176  //! stream that are created by hard flushes on the corresponding reverse filters, in this
177  //! example ZlibCompressor. This is useful when zlib compressed data is moved across a
178  //! network in packets and compression state is preserved across packets, as in the SSH2 protocol.
179  virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) =0;
180 
181  //@}
182 
183 private:
184  bool IsolatedFlush(bool hardFlush, bool blocking)
185  {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); CRYPTOPP_ASSERT(false); return false;}
186 };
187 
188 //! \class CustomSignalPropagation
189 //! \brief Interface for custom flush signals
190 //! \tparam T BufferedTransformation derived class
191 template <class T>
192 class CRYPTOPP_NO_VTABLE CustomSignalPropagation : public CustomFlushPropagation<T>
193 {
194 public:
195  //! \brief Initialize or reinitialize this object, with signal propagation
196  //! \param parameters a set of NameValuePairs to initialize or reinitialize this object
197  //! \param propagation the number of attached transformations the Initialize() signal should be passed
198  //! \details Initialize() is used to initialize or reinitialize an object using a variable number of
199  //! arbitrarily typed arguments. The function avoids the need for multiple constuctors providing
200  //! all possible combintations of configurable parameters.
201  //! \details propagation count includes this object. Setting propagation to <tt>1</tt> means this
202  //! object only. Setting propagation to <tt>-1</tt> means unlimited propagation.
203  virtual void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1) =0;
204 
205 private:
206  void IsolatedInitialize(const NameValuePairs &parameters)
207  {CRYPTOPP_UNUSED(parameters); CRYPTOPP_ASSERT(false);}
208 };
209 
210 //! \class Multichannel
211 //! \brief Multiple channels support for custom signal processing
212 //! \tparam T the class or type
213 //! \details T should be a BufferedTransformation derived class
214 template <class T>
215 class CRYPTOPP_NO_VTABLE Multichannel : public CustomFlushPropagation<T>
216 {
217 public:
218  bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
219  {return this->ChannelFlush(DEFAULT_CHANNEL, hardFlush, propagation, blocking);}
220 
221  //! \brief Marks the end of a series of messages, with signal propagation
222  //! \param propagation the number of attached transformations the MessageSeriesEnd() signal should be passed
223  //! \param blocking specifies whether the object should block when processing input
224  //! \details Each object that receives the signal will perform its processing, decrement
225  //! propagation, and then pass the signal on to attached transformations if the value is not 0.
226  //! \details propagation count includes this object. Setting propagation to <tt>1</tt> means this
227  //! object only. Setting propagation to <tt>-1</tt> means unlimited propagation.
228  //! \note There should be a MessageEnd() immediately before MessageSeriesEnd().
229  bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
230  {return this->ChannelMessageSeriesEnd(DEFAULT_CHANNEL, propagation, blocking);}
231 
232  //! \brief Request space which can be written into by the caller
233  //! \param size the requested size of the buffer
234  //! \details The purpose of this method is to help avoid extra memory allocations.
235  //! \details size is an \a IN and \a OUT parameter and used as a hint. When the call is made,
236  //! size is the requested size of the buffer. When the call returns, size is the size of
237  //! the array returned to the caller.
238  //! \details The base class implementation sets size to 0 and returns NULL.
239  //! \note Some objects, like ArraySink, cannot create a space because its fixed. In the case of
240  //! an ArraySink, the pointer to the array is returned and the size is remaining size.
241  byte * CreatePutSpace(size_t &size)
242  {return this->ChannelCreatePutSpace(DEFAULT_CHANNEL, size);}
243 
244  //! \brief Input multiple bytes for processing
245  //! \param inString the byte buffer to process
246  //! \param length the size of the string, in bytes
247  //! \param messageEnd means how many filters to signal MessageEnd() to, including this one
248  //! \param blocking specifies whether the object should block when processing input
249  //! \details Derived classes must implement Put2().
250  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
251  {return this->ChannelPut2(DEFAULT_CHANNEL, inString, length, messageEnd, blocking);}
252 
253  //! \brief Input multiple bytes that may be modified by callee.
254  //! \param inString the byte buffer to process.
255  //! \param length the size of the string, in bytes.
256  //! \param messageEnd means how many filters to signal MessageEnd() to, including this one.
257  //! \param blocking specifies whether the object should block when processing input.
258  //! \details Internally, PutModifiable2() calls Put2().
259  size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
260  {return this->ChannelPutModifiable2(DEFAULT_CHANNEL, inString, length, messageEnd, blocking);}
261 
262 // void ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1)
263 // {PropagateMessageSeriesEnd(propagation, channel);}
264  byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
265  {CRYPTOPP_UNUSED(channel); size = 0; return NULL;}
266  bool ChannelPutModifiable(const std::string &channel, byte *inString, size_t length)
267  {this->ChannelPut(channel, inString, length); return false;}
268 
269  virtual size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking) =0;
270  size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
271  {return ChannelPut2(channel, begin, length, messageEnd, blocking);}
272 
273  virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true) =0;
274 };
275 
276 //! \class AutoSignaling
277 //! \brief Provides auto signaling support
278 //! \tparam T BufferedTransformation derived class
279 template <class T>
280 class CRYPTOPP_NO_VTABLE AutoSignaling : public T
281 {
282 public:
283  //! \brief Construct an AutoSignaling
284  //! \param propagation the propagation count
285  AutoSignaling(int propagation=-1) : m_autoSignalPropagation(propagation) {}
286 
287  void SetAutoSignalPropagation(int propagation)
288  {m_autoSignalPropagation = propagation;}
289  int GetAutoSignalPropagation() const
290  {return m_autoSignalPropagation;}
291 
292 private:
293  int m_autoSignalPropagation;
294 };
295 
296 //! \class Store
297 //! \brief Acts as a Source for pre-existing, static data
298 //! \tparam T BufferedTransformation that only contains pre-existing data as "output"
299 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Store : public AutoSignaling<InputRejecting<BufferedTransformation> >
300 {
301 public:
302  //! \brief Construct a Store
303  Store() : m_messageEnd(false) {}
304 
305  void IsolatedInitialize(const NameValuePairs &parameters)
306  {
307  m_messageEnd = false;
308  StoreInitialize(parameters);
309  }
310 
311  unsigned int NumberOfMessages() const {return m_messageEnd ? 0 : 1;}
312  bool GetNextMessage();
313  unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=DEFAULT_CHANNEL) const;
314 
315 protected:
316  virtual void StoreInitialize(const NameValuePairs &parameters) =0;
317 
318  bool m_messageEnd;
319 };
320 
321 //! \class Sink
322 //! \brief Implementation of BufferedTransformation's attachment interface
323 //! \details Sink is a cornerstone of the Pipeline trinitiy. Data flows from
324 //! Sources, through Filters, and then terminates in Sinks. The difference
325 //! between a Source and Filter is a Source \a pumps data, while a Filter does
326 //! not. The difference between a Filter and a Sink is a Filter allows an
327 //! attached transformation, while a Sink does not.
328 //! \details A Sink doesnot produce any retrievable output.
329 //! \details See the discussion of BufferedTransformation in cryptlib.h for
330 //! more details.
331 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Sink : public BufferedTransformation
332 {
333 public:
334  size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
335  {CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(transferBytes); CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking); transferBytes = 0; return 0;}
336  size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
337  {CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(begin); CRYPTOPP_UNUSED(end); CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking); return 0;}
338 };
339 
340 //! \class BitBucket
341 //! \brief Acts as an input discarding Filter or Sink
342 //! \tparam T the class or type
343 //! \details The BitBucket discards all input and returns 0 to the caller
344 //! to indicate all data was processed.
345 class CRYPTOPP_DLL BitBucket : public Bufferless<Sink>
346 {
347 public:
348  std::string AlgorithmName() const {return "BitBucket";}
349  void IsolatedInitialize(const NameValuePairs &params)
350  {CRYPTOPP_UNUSED(params);}
351  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
352  {CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); return 0;}
353 };
354 
355 NAMESPACE_END
356 
357 #if CRYPTOPP_MSC_VERSION
358 # pragma warning(pop)
359 #endif
360 
361 #endif
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: simple.h:250
An invalid argument was detected.
Definition: cryptlib.h:183
Utility functions for the Crypto++ library.
Store()
Construct a Store.
Definition: simple.h:303
Base class for identifying alogorithm.
Definition: simple.h:26
Exception thrown when an invalid key length is encountered.
Definition: simple.h:47
Flush(true) was called but it can't completely flush its buffers.
Definition: cryptlib.h:211
Abstract base classes that provide a uniform interface to this library.
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition: simple.h:218
void IsolatedInitialize(const NameValuePairs &params)
Initialize or reinitialize this object, without signal propagation.
Definition: simple.h:349
Library configuration file.
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: simple.h:348
Acts as a Source for pre-existing, static data.
Definition: simple.h:299
Base class for input rejecting filters.
Definition: simple.h:121
virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output on a channel.
Definition: cryptlib.cpp:480
Interface for buffered transformations.
Definition: cryptlib.h:1363
Interface for cloning objects.
Definition: cryptlib.h:481
Interface for custom flush signals propagation.
Definition: simple.h:159
Exception thrown when an invalid salt length is encountered.
Definition: simple.h:71
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: simple.h:305
Exception thrown when an invalid personalization string length is encountered.
Definition: simple.h:63
A method was called which was not implemented.
Definition: cryptlib.h:204
const std::string DEFAULT_CHANNEL
Default channel for BufferedTransformation.
Definition: cryptlib.cpp:59
Exception thrown when an invalid number of rounds is encountered.
Definition: simple.h:55
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
Transfer bytes from this object to another BufferedTransformation.
Definition: simple.h:334
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
Definition: cryptlib.cpp:76
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:62
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input a byte array for processing.
Definition: simple.h:138
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
Copy bytes from this object to another BufferedTransformation.
Definition: simple.h:336
size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes that may be modified by callee.
Definition: simple.h:259
Base class for unflushable filters.
Definition: simple.h:94
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: simple.h:351
Implementation of BufferedTransformation's attachment interface.
Definition: simple.h:331
Provides auto signaling support.
Definition: simple.h:280
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:533
byte * CreatePutSpace(size_t &size)
Request space which can be written into by the caller.
Definition: simple.h:241
bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
Marks the end of a series of messages, with signal propagation.
Definition: simple.h:229
Acts as an input discarding Filter or Sink.
Definition: simple.h:345
Crypto++ library namespace.
unsigned int NumberOfMessages() const
Provides the number of meesages processed by this object.
Definition: simple.h:311
Multiple channels support for custom signal processing.
Definition: simple.h:215
Interface for custom flush signals.
Definition: simple.h:192
AutoSignaling(int propagation=-1)
Construct an AutoSignaling.
Definition: simple.h:285
Base class for bufferless filters.
Definition: simple.h:83
Interface for retrieving values given their names.
Definition: cryptlib.h:278
Base class for identifying alogorithm.
Definition: simple.h:38