Crypto++
simple.h
Go to the documentation of this file.
1 // simple.h - written and placed in the public domain by Wei Dai
2 /*! \file
3  Simple non-interface classes derived from classes in cryptlib.h.
4 */
5 
6 #ifndef CRYPTOPP_SIMPLE_H
7 #define CRYPTOPP_SIMPLE_H
8 
9 #include "cryptlib.h"
10 #include "misc.h"
11 
12 NAMESPACE_BEGIN(CryptoPP)
13 
14 //! _
15 template <class DERIVED, class BASE>
16 class CRYPTOPP_NO_VTABLE ClonableImpl : public BASE
17 {
18 public:
19  Clonable * Clone() const {return new DERIVED(*static_cast<const DERIVED *>(this));}
20 };
21 
22 //! _
23 template <class BASE, class ALGORITHM_INFO=BASE>
24 class CRYPTOPP_NO_VTABLE AlgorithmImpl : public BASE
25 {
26 public:
27  static std::string CRYPTOPP_API StaticAlgorithmName() {return ALGORITHM_INFO::StaticAlgorithmName();}
28  std::string AlgorithmName() const {return ALGORITHM_INFO::StaticAlgorithmName();}
29 };
30 
31 //! _
32 class CRYPTOPP_DLL InvalidKeyLength : public InvalidArgument
33 {
34 public:
35  explicit InvalidKeyLength(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid key length") {}
36 };
37 
38 //! _
39 class CRYPTOPP_DLL InvalidRounds : public InvalidArgument
40 {
41 public:
42  explicit InvalidRounds(const std::string &algorithm, unsigned int rounds) : InvalidArgument(algorithm + ": " + IntToString(rounds) + " is not a valid number of rounds") {}
43 };
44 
45 // *****************************
46 
47 //! _
48 template <class T>
49 class CRYPTOPP_NO_VTABLE Bufferless : public T
50 {
51 public:
52  bool IsolatedFlush(bool hardFlush, bool blocking) {return false;}
53 };
54 
55 //! _
56 template <class T>
57 class CRYPTOPP_NO_VTABLE Unflushable : public T
58 {
59 public:
60  bool Flush(bool completeFlush, int propagation=-1, bool blocking=true)
61  {return ChannelFlush(DEFAULT_CHANNEL, completeFlush, propagation, blocking);}
62  bool IsolatedFlush(bool hardFlush, bool blocking)
63  {assert(false); return false;}
64  bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true)
65  {
66  if (hardFlush && !InputBufferIsEmpty())
67  throw CannotFlush("Unflushable<T>: this object has buffered input that cannot be flushed");
68  else
69  {
70  BufferedTransformation *attached = this->AttachedTransformation();
71  return attached && propagation ? attached->ChannelFlush(channel, hardFlush, propagation-1, blocking) : false;
72  }
73  }
74 
75 protected:
76  virtual bool InputBufferIsEmpty() const {return false;}
77 };
78 
79 //! _
80 template <class T>
81 class CRYPTOPP_NO_VTABLE InputRejecting : public T
82 {
83 public:
84  struct InputRejected : public NotImplemented
85  {InputRejected() : NotImplemented("BufferedTransformation: this object doesn't allow input") {}};
86 
87  // shouldn't be calling these functions on this class
88  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
89  {throw InputRejected();}
90  bool IsolatedFlush(bool, bool) {return false;}
91  bool IsolatedMessageSeriesEnd(bool) {throw InputRejected();}
92 
93  size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
94  {throw InputRejected();}
95  bool ChannelMessageSeriesEnd(const std::string &, int, bool) {throw InputRejected();}
96 };
97 
98 //! _
99 template <class T>
100 class CRYPTOPP_NO_VTABLE CustomFlushPropagation : public T
101 {
102 public:
103  virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) =0;
104 
105 private:
106  bool IsolatedFlush(bool hardFlush, bool blocking) {assert(false); return false;}
107 };
108 
109 //! _
110 template <class T>
111 class CRYPTOPP_NO_VTABLE CustomSignalPropagation : public CustomFlushPropagation<T>
112 {
113 public:
114  virtual void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1) =0;
115 
116 private:
117  void IsolatedInitialize(const NameValuePairs &parameters) {assert(false);}
118 };
119 
120 //! _
121 template <class T>
122 class CRYPTOPP_NO_VTABLE Multichannel : public CustomFlushPropagation<T>
123 {
124 public:
125  bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
126  {return this->ChannelFlush(DEFAULT_CHANNEL, hardFlush, propagation, blocking);}
127  bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
128  {return this->ChannelMessageSeriesEnd(DEFAULT_CHANNEL, propagation, blocking);}
129  byte * CreatePutSpace(size_t &size)
130  {return this->ChannelCreatePutSpace(DEFAULT_CHANNEL, size);}
131  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
132  {return this->ChannelPut2(DEFAULT_CHANNEL, begin, length, messageEnd, blocking);}
133  size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
134  {return this->ChannelPutModifiable2(DEFAULT_CHANNEL, inString, length, messageEnd, blocking);}
135 
136 // void ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1)
137 // {PropagateMessageSeriesEnd(propagation, channel);}
138  byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
139  {size = 0; return NULL;}
140  bool ChannelPutModifiable(const std::string &channel, byte *inString, size_t length)
141  {this->ChannelPut(channel, inString, length); return false;}
142 
143  virtual size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking) =0;
144  size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
145  {return ChannelPut2(channel, begin, length, messageEnd, blocking);}
146 
147  virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true) =0;
148 };
149 
150 //! _
151 template <class T>
152 class CRYPTOPP_NO_VTABLE AutoSignaling : public T
153 {
154 public:
155  AutoSignaling(int propagation=-1) : m_autoSignalPropagation(propagation) {}
156 
157  void SetAutoSignalPropagation(int propagation)
158  {m_autoSignalPropagation = propagation;}
159  int GetAutoSignalPropagation() const
160  {return m_autoSignalPropagation;}
161 
162 private:
163  int m_autoSignalPropagation;
164 };
165 
166 //! A BufferedTransformation that only contains pre-existing data as "output"
167 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Store : public AutoSignaling<InputRejecting<BufferedTransformation> >
168 {
169 public:
170  Store() : m_messageEnd(false) {}
171 
172  void IsolatedInitialize(const NameValuePairs &parameters)
173  {
174  m_messageEnd = false;
175  StoreInitialize(parameters);
176  }
177 
178  unsigned int NumberOfMessages() const {return m_messageEnd ? 0 : 1;}
179  bool GetNextMessage();
180  unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=DEFAULT_CHANNEL) const;
181 
182 protected:
183  virtual void StoreInitialize(const NameValuePairs &parameters) =0;
184 
185  bool m_messageEnd;
186 };
187 
188 //! A BufferedTransformation that doesn't produce any retrievable output
189 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Sink : public BufferedTransformation
190 {
191 public:
192  size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
193  {transferBytes = 0; return 0;}
194  size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
195  {return 0;}
196 };
197 
198 class CRYPTOPP_DLL BitBucket : public Bufferless<Sink>
199 {
200 public:
201  std::string AlgorithmName() const {return "BitBucket";}
202  void IsolatedInitialize(const NameValuePairs &parameters) {}
203  size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
204  {return 0;}
205 };
206 
207 NAMESPACE_END
208 
209 #endif
exception thrown when an invalid argument is detected
Definition: cryptlib.h:144
exception thrown by a class when Flush(true) is called but it can't completely flush its buffers ...
Definition: cryptlib.h:172
std::string AlgorithmName() const
returns name of this algorithm, not universally implemented yet
Definition: simple.h:201
A BufferedTransformation that only contains pre-existing data as "output".
Definition: simple.h:167
interface for buffered transformations
Definition: cryptlib.h:770
interface for cloning objects, this is not implemented by most classes yet
Definition: cryptlib.h:325
size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
input multiple bytes for blocking or non-blocking processing
Definition: simple.h:203
exception thrown by a class if a non-implemented method is called
Definition: cryptlib.h:165
const std::string DEFAULT_CHANNEL
the default channel for BufferedTransformation, equal to the empty string
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
upon return, byteCount contains number of bytes that have finished being transfered, and returns the number of bytes left in the current transfer block
Definition: simple.h:192
const NameValuePairs & g_nullNameValuePairs
empty set of name-value pairs
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
upon return, begin contains the start position of data yet to be finished copying, and returns the number of bytes left in the current transfer block
Definition: simple.h:194
A BufferedTransformation that doesn't produce any retrievable output.
Definition: simple.h:189
unsigned int NumberOfMessages() const
number of times MessageEnd() has been received minus messages retrieved or skipped ...
Definition: simple.h:178
_
Definition: simple.h:49
interface for retrieving values given their names
Definition: cryptlib.h:224