Crypto++  5.6.3
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 // *****************************
62 
63 //! \class Bufferless
64 //! \brief Base class for bufferless filters
65 //! \tparam T the class or type
66 template <class T>
67 class CRYPTOPP_NO_VTABLE Bufferless : public T
68 {
69 public:
70  bool IsolatedFlush(bool hardFlush, bool blocking)
71  {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); return false;}
72 };
73 
74 //! \class Unflushable
75 //! \brief Base class for unflushable filters
76 //! \tparam T the class or type
77 template <class T>
78 class CRYPTOPP_NO_VTABLE Unflushable : public T
79 {
80 public:
81  bool Flush(bool completeFlush, int propagation=-1, bool blocking=true)
82  {return ChannelFlush(DEFAULT_CHANNEL, completeFlush, propagation, blocking);}
83  bool IsolatedFlush(bool hardFlush, bool blocking)
84  {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); assert(false); return false;}
85  bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true)
86  {
87  if (hardFlush && !InputBufferIsEmpty())
88  throw CannotFlush("Unflushable<T>: this object has buffered input that cannot be flushed");
89  else
90  {
91  BufferedTransformation *attached = this->AttachedTransformation();
92  return attached && propagation ? attached->ChannelFlush(channel, hardFlush, propagation-1, blocking) : false;
93  }
94  }
95 
96 protected:
97  virtual bool InputBufferIsEmpty() const {return false;}
98 };
99 
100 //! \class InputRejecting
101 //! \brief Base class for input rejecting filters
102 //! \tparam T the class or type
103 //! \details T should be a BufferedTransformation derived class
104 template <class T>
105 class CRYPTOPP_NO_VTABLE InputRejecting : public T
106 {
107 public:
109  {InputRejected() : NotImplemented("BufferedTransformation: this object doesn't allow input") {}};
110 
111  //! \name INPUT
112  //@{
113 
114  //! \brief Input a byte array for processing
115  //! \param inString the byte array to process
116  //! \param length the size of the string, in bytes
117  //! \param messageEnd means how many filters to signal MessageEnd() to, including this one
118  //! \param blocking specifies whether the object should block when processing input
119  //! \throws InputRejected
120  //! \returns the number of bytes that remain in the block (i.e., bytes not processed)
121  //! \details Internally, the default implmentation throws InputRejected.
122  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
123  {CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); throw InputRejected();}
124  //@}
125 
126  //! \name SIGNALS
127  //@{
128  bool IsolatedFlush(bool hardFlush, bool blocking)
129  {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); return false;}
130  bool IsolatedMessageSeriesEnd(bool blocking)
131  {CRYPTOPP_UNUSED(blocking); throw InputRejected();}
132  size_t ChannelPut2(const std::string &channel, const byte *inString, size_t length, int messageEnd, bool blocking)
133  {CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); throw InputRejected();}
134  bool ChannelMessageSeriesEnd(const std::string& channel, int messageEnd, bool blocking)
135  {CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); throw InputRejected();}
136  //@}
137 };
138 
139 //! \class CustomFlushPropagation
140 //! \brief Provides interface for custom flush signals
141 //! \tparam T the class or type
142 //! \details T should be a BufferedTransformation derived class
143 template <class T>
144 class CRYPTOPP_NO_VTABLE CustomFlushPropagation : public T
145 {
146 public:
147  //! \name SIGNALS
148  //@{
149  virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) =0;
150  //@}
151 
152 private:
153  bool IsolatedFlush(bool hardFlush, bool blocking)
154  {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); assert(false); return false;}
155 };
156 
157 //! \class CustomSignalPropagation
158 //! \brief Provides interface for initialization of derived filters
159 //! \tparam T the class or type
160 //! \details T should be a BufferedTransformation derived class
161 template <class T>
162 class CRYPTOPP_NO_VTABLE CustomSignalPropagation : public CustomFlushPropagation<T>
163 {
164 public:
165  virtual void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1) =0;
166 
167 private:
168  void IsolatedInitialize(const NameValuePairs &parameters)
169  {CRYPTOPP_UNUSED(parameters); assert(false);}
170 };
171 
172 //! \class Multichannel
173 //! \brief Provides multiple channels support for custom flush signal processing
174 //! \tparam T the class or type
175 //! \details T should be a BufferedTransformation derived class
176 template <class T>
177 class CRYPTOPP_NO_VTABLE Multichannel : public CustomFlushPropagation<T>
178 {
179 public:
180  bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
181  {return this->ChannelFlush(DEFAULT_CHANNEL, hardFlush, propagation, blocking);}
182  bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
183  {return this->ChannelMessageSeriesEnd(DEFAULT_CHANNEL, propagation, blocking);}
184  byte * CreatePutSpace(size_t &size)
185  {return this->ChannelCreatePutSpace(DEFAULT_CHANNEL, size);}
186  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
187  {return this->ChannelPut2(DEFAULT_CHANNEL, inString, length, messageEnd, blocking);}
188  size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
189  {return this->ChannelPutModifiable2(DEFAULT_CHANNEL, inString, length, messageEnd, blocking);}
190 
191 // void ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1)
192 // {PropagateMessageSeriesEnd(propagation, channel);}
193  byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
194  {CRYPTOPP_UNUSED(channel); size = 0; return NULL;}
195  bool ChannelPutModifiable(const std::string &channel, byte *inString, size_t length)
196  {this->ChannelPut(channel, inString, length); return false;}
197 
198  virtual size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking) =0;
199  size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
200  {return ChannelPut2(channel, begin, length, messageEnd, blocking);}
201 
202  virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true) =0;
203 };
204 
205 //! \class AutoSignaling
206 //! \brief Provides auto signaling support
207 //! \tparam T the class or type
208 //! \details T should be a BufferedTransformation derived class
209 template <class T>
210 class CRYPTOPP_NO_VTABLE AutoSignaling : public T
211 {
212 public:
213  AutoSignaling(int propagation=-1) : m_autoSignalPropagation(propagation) {}
214 
215  void SetAutoSignalPropagation(int propagation)
216  {m_autoSignalPropagation = propagation;}
217  int GetAutoSignalPropagation() const
218  {return m_autoSignalPropagation;}
219 
220 private:
221  int m_autoSignalPropagation;
222 };
223 
224 //! \class Store
225 //! \brief Acts as a Source for pre-existing, static data
226 //! \tparam T the class or type
227 //! \details A BufferedTransformation that only contains pre-existing data as "output"
228 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Store : public AutoSignaling<InputRejecting<BufferedTransformation> >
229 {
230 public:
231  //! \brief Construct a Store
232  Store() : m_messageEnd(false) {}
233 
234  void IsolatedInitialize(const NameValuePairs &parameters)
235  {
236  m_messageEnd = false;
237  StoreInitialize(parameters);
238  }
239 
240  unsigned int NumberOfMessages() const {return m_messageEnd ? 0 : 1;}
241  bool GetNextMessage();
242  unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=DEFAULT_CHANNEL) const;
243 
244 protected:
245  virtual void StoreInitialize(const NameValuePairs &parameters) =0;
246 
247  bool m_messageEnd;
248 };
249 
250 //! \class Sink
251 //! \brief Implementation of BufferedTransformation's attachment interface
252 //! \details Sink is a cornerstone of the Pipeline trinitiy. Data flows from
253 //! Sources, through Filters, and then terminates in Sinks. The difference
254 //! between a Source and Filter is a Source \a pumps data, while a Filter does
255 //! not. The difference between a Filter and a Sink is a Filter allows an
256 //! attached transformation, while a Sink does not.
257 //! \details A Sink doesnot produce any retrievable output.
258 //! \details See the discussion of BufferedTransformation in cryptlib.h for
259 //! more details.
260 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Sink : public BufferedTransformation
261 {
262 public:
263  size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
264  {CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(transferBytes); CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking); transferBytes = 0; return 0;}
265  size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
266  {CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(begin); CRYPTOPP_UNUSED(end); CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking); return 0;}
267 };
268 
269 //! \class BitBucket
270 //! \brief Acts as an input discarding Filter or Sink
271 //! \tparam T the class or type
272 //! \details The BitBucket discards all input and returns 0 to the caller
273 //! to indicate all data was processed.
274 class CRYPTOPP_DLL BitBucket : public Bufferless<Sink>
275 {
276 public:
277  std::string AlgorithmName() const {return "BitBucket";}
278  void IsolatedInitialize(const NameValuePairs &params)
279  {CRYPTOPP_UNUSED(params);}
280  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
281  {CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); return 0;}
282 };
283 
284 NAMESPACE_END
285 
286 #if CRYPTOPP_MSC_VERSION
287 # pragma warning(pop)
288 #endif
289 
290 #endif
An invalid argument was detected.
Definition: cryptlib.h:182
Utility functions for the Crypto++ library.
Store()
Construct a Store.
Definition: simple.h:232
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:210
Abstract base classes that provide a uniform interface to this library.
void IsolatedInitialize(const NameValuePairs &params)
Initialize or reinitialize this object, without signal propagation.
Definition: simple.h:278
Library configuration file.
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: simple.h:277
Acts as a Source for pre-existing, static data.
Definition: simple.h:228
Base class for input rejecting filters.
Definition: simple.h:105
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:483
Interface for buffered transformations.
Definition: cryptlib.h:1342
Interface for cloning objects.
Definition: cryptlib.h:470
Provides interface for custom flush signals.
Definition: simple.h:144
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: simple.h:234
A method was called which was not implemented.
Definition: cryptlib.h:203
const std::string DEFAULT_CHANNEL
Default channel for BufferedTransformation.
Definition: cryptlib.cpp:41
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:263
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
Definition: cryptlib.cpp:79
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input a byte array for processing.
Definition: simple.h:122
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:265
Base class for unflushable filters.
Definition: simple.h:78
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: simple.h:280
Implementation of BufferedTransformation's attachment interface.
Definition: simple.h:260
Provides auto signaling support.
Definition: simple.h:210
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:460
Acts as an input discarding Filter or Sink.
Definition: simple.h:274
Crypto++ library namespace.
unsigned int NumberOfMessages() const
Provides the number of meesages processed by this object.
Definition: simple.h:240
Provides multiple channels support for custom flush signal processing.
Definition: simple.h:177
Provides interface for initialization of derived filters.
Definition: simple.h:162
Base class for bufferless filters.
Definition: simple.h:67
Interface for retrieving values given their names.
Definition: cryptlib.h:277
Base class for identifying alogorithm.
Definition: simple.h:38