00001
00002
00003
00004
00005
00006 #ifndef CRYPTOPP_SIMPLE_H
00007 #define CRYPTOPP_SIMPLE_H
00008
00009 #include "config.h"
00010
00011 #if CRYPTOPP_MSC_VERSION
00012 # pragma warning(push)
00013 # pragma warning(disable: 4127 4189)
00014 #endif
00015
00016 #include "cryptlib.h"
00017 #include "misc.h"
00018
00019 NAMESPACE_BEGIN(CryptoPP)
00020
00021
00022
00023
00024
00025 template <class DERIVED, class BASE>
00026 class CRYPTOPP_NO_VTABLE ClonableImpl : public BASE
00027 {
00028 public:
00029 Clonable * Clone() const {return new DERIVED(*static_cast<const DERIVED *>(this));}
00030 };
00031
00032
00033
00034
00035
00036
00037 template <class BASE, class ALGORITHM_INFO=BASE>
00038 class CRYPTOPP_NO_VTABLE AlgorithmImpl : public BASE
00039 {
00040 public:
00041 static std::string CRYPTOPP_API StaticAlgorithmName() {return ALGORITHM_INFO::StaticAlgorithmName();}
00042 std::string AlgorithmName() const {return ALGORITHM_INFO::StaticAlgorithmName();}
00043 };
00044
00045
00046
00047 class CRYPTOPP_DLL InvalidKeyLength : public InvalidArgument
00048 {
00049 public:
00050 explicit InvalidKeyLength(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid key length") {}
00051 };
00052
00053
00054
00055 class CRYPTOPP_DLL InvalidRounds : public InvalidArgument
00056 {
00057 public:
00058 explicit InvalidRounds(const std::string &algorithm, unsigned int rounds) : InvalidArgument(algorithm + ": " + IntToString(rounds) + " is not a valid number of rounds") {}
00059 };
00060
00061
00062
00063
00064
00065
00066 template <class T>
00067 class CRYPTOPP_NO_VTABLE Bufferless : public T
00068 {
00069 public:
00070 bool IsolatedFlush(bool hardFlush, bool blocking)
00071 {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); return false;}
00072 };
00073
00074
00075
00076
00077 template <class T>
00078 class CRYPTOPP_NO_VTABLE Unflushable : public T
00079 {
00080 public:
00081 bool Flush(bool completeFlush, int propagation=-1, bool blocking=true)
00082 {return ChannelFlush(DEFAULT_CHANNEL, completeFlush, propagation, blocking);}
00083 bool IsolatedFlush(bool hardFlush, bool blocking)
00084 {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); assert(false); return false;}
00085 bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true)
00086 {
00087 if (hardFlush && !InputBufferIsEmpty())
00088 throw CannotFlush("Unflushable<T>: this object has buffered input that cannot be flushed");
00089 else
00090 {
00091 BufferedTransformation *attached = this->AttachedTransformation();
00092 return attached && propagation ? attached->ChannelFlush(channel, hardFlush, propagation-1, blocking) : false;
00093 }
00094 }
00095
00096 protected:
00097 virtual bool InputBufferIsEmpty() const {return false;}
00098 };
00099
00100
00101
00102
00103
00104 template <class T>
00105 class CRYPTOPP_NO_VTABLE InputRejecting : public T
00106 {
00107 public:
00108 struct InputRejected : public NotImplemented
00109 {InputRejected() : NotImplemented("BufferedTransformation: this object doesn't allow input") {}};
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
00123 {CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); throw InputRejected();}
00124
00125
00126
00127
00128 bool IsolatedFlush(bool hardFlush, bool blocking)
00129 {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); return false;}
00130 bool IsolatedMessageSeriesEnd(bool blocking)
00131 {CRYPTOPP_UNUSED(blocking); throw InputRejected();}
00132 size_t ChannelPut2(const std::string &channel, const byte *inString, size_t length, int messageEnd, bool blocking)
00133 {CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); throw InputRejected();}
00134 bool ChannelMessageSeriesEnd(const std::string& channel, int messageEnd, bool blocking)
00135 {CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); throw InputRejected();}
00136
00137 };
00138
00139
00140
00141
00142
00143 template <class T>
00144 class CRYPTOPP_NO_VTABLE CustomFlushPropagation : public T
00145 {
00146 public:
00147
00148
00149 virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) =0;
00150
00151
00152 private:
00153 bool IsolatedFlush(bool hardFlush, bool blocking)
00154 {CRYPTOPP_UNUSED(hardFlush); CRYPTOPP_UNUSED(blocking); assert(false); return false;}
00155 };
00156
00157
00158
00159
00160
00161 template <class T>
00162 class CRYPTOPP_NO_VTABLE CustomSignalPropagation : public CustomFlushPropagation<T>
00163 {
00164 public:
00165 virtual void Initialize(const NameValuePairs ¶meters=g_nullNameValuePairs, int propagation=-1) =0;
00166
00167 private:
00168 void IsolatedInitialize(const NameValuePairs ¶meters)
00169 {CRYPTOPP_UNUSED(parameters); assert(false);}
00170 };
00171
00172
00173
00174
00175
00176 template <class T>
00177 class CRYPTOPP_NO_VTABLE Multichannel : public CustomFlushPropagation<T>
00178 {
00179 public:
00180 bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
00181 {return this->ChannelFlush(DEFAULT_CHANNEL, hardFlush, propagation, blocking);}
00182 bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
00183 {return this->ChannelMessageSeriesEnd(DEFAULT_CHANNEL, propagation, blocking);}
00184 byte * CreatePutSpace(size_t &size)
00185 {return this->ChannelCreatePutSpace(DEFAULT_CHANNEL, size);}
00186 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
00187 {return this->ChannelPut2(DEFAULT_CHANNEL, inString, length, messageEnd, blocking);}
00188 size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
00189 {return this->ChannelPutModifiable2(DEFAULT_CHANNEL, inString, length, messageEnd, blocking);}
00190
00191
00192
00193 byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
00194 {CRYPTOPP_UNUSED(channel); size = 0; return NULL;}
00195 bool ChannelPutModifiable(const std::string &channel, byte *inString, size_t length)
00196 {this->ChannelPut(channel, inString, length); return false;}
00197
00198 virtual size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking) =0;
00199 size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
00200 {return ChannelPut2(channel, begin, length, messageEnd, blocking);}
00201
00202 virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true) =0;
00203 };
00204
00205
00206
00207
00208
00209 template <class T>
00210 class CRYPTOPP_NO_VTABLE AutoSignaling : public T
00211 {
00212 public:
00213 AutoSignaling(int propagation=-1) : m_autoSignalPropagation(propagation) {}
00214
00215 void SetAutoSignalPropagation(int propagation)
00216 {m_autoSignalPropagation = propagation;}
00217 int GetAutoSignalPropagation() const
00218 {return m_autoSignalPropagation;}
00219
00220 private:
00221 int m_autoSignalPropagation;
00222 };
00223
00224
00225
00226
00227
00228 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Store : public AutoSignaling<InputRejecting<BufferedTransformation> >
00229 {
00230 public:
00231 Store() : m_messageEnd(false) {}
00232
00233 void IsolatedInitialize(const NameValuePairs ¶meters)
00234 {
00235 m_messageEnd = false;
00236 StoreInitialize(parameters);
00237 }
00238
00239 unsigned int NumberOfMessages() const {return m_messageEnd ? 0 : 1;}
00240 bool GetNextMessage();
00241 unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=DEFAULT_CHANNEL) const;
00242
00243 protected:
00244 virtual void StoreInitialize(const NameValuePairs ¶meters) =0;
00245
00246 bool m_messageEnd;
00247 };
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Sink : public BufferedTransformation
00260 {
00261 public:
00262 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
00263 {CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(transferBytes); CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking); transferBytes = 0; return 0;}
00264 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
00265 {CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(begin); CRYPTOPP_UNUSED(end); CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking); return 0;}
00266 };
00267
00268
00269
00270
00271
00272
00273 class CRYPTOPP_DLL BitBucket : public Bufferless<Sink>
00274 {
00275 public:
00276 std::string AlgorithmName() const {return "BitBucket";}
00277 void IsolatedInitialize(const NameValuePairs ¶ms)
00278 {CRYPTOPP_UNUSED(params);}
00279 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
00280 {CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking); return 0;}
00281 };
00282
00283 NAMESPACE_END
00284
00285 #if CRYPTOPP_MSC_VERSION
00286 # pragma warning(pop)
00287 #endif
00288
00289 #endif