00001
00002
00003
00004
00005
00006
#ifndef CRYPTOPP_SIMPLE_H
00007
#define CRYPTOPP_SIMPLE_H
00008
00009
#include "cryptlib.h"
00010
#include "misc.h"
00011
00012 NAMESPACE_BEGIN(CryptoPP)
00013
00014 template <class BASE, class ALGORITHM_INFO = BASE>
00015 class AlgorithmImpl : public BASE
00016 {
00017
public:
00018 std::string AlgorithmName()
const {
return ALGORITHM_INFO::StaticAlgorithmName();}
00019 };
00020
00021
00022 class InvalidKeyLength :
public InvalidArgument
00023 {
00024
public:
00025
explicit InvalidKeyLength(
const std::string &algorithm,
unsigned int length) :
InvalidArgument(algorithm +
": " + IntToString(length) +
" is not a valid key length") {}
00026 };
00027
00028
00029 class InvalidRounds :
public InvalidArgument
00030 {
00031
public:
00032
explicit InvalidRounds(
const std::string &algorithm,
unsigned int rounds) :
InvalidArgument(algorithm +
": " + IntToString(rounds) +
" is not a valid number of rounds") {}
00033 };
00034
00035
class HashTransformationWithDefaultTruncation :
public HashTransformation
00036 {
00037
public:
00038
virtual void Final(byte *digest) =0;
00039
void TruncatedFinal(byte *digest,
unsigned int digestSize);
00040 };
00041
00042
00043
00044 class ASN1CryptoMaterial :
virtual public ASN1Object,
virtual public CryptoMaterial
00045 {
00046
public:
00047 void Save(
BufferedTransformation &bt)
const
00048
{BEREncode(bt);}
00049 void Load(
BufferedTransformation &bt)
00050 {BERDecode(bt);}
00051 };
00052
00053
00054
00055
template <
class T>
00056
class Bufferless :
public T
00057 {
00058
public:
00059 Bufferless() {}
00060 Bufferless(
BufferedTransformation *q) : T(q) {}
00061
bool IsolatedFlush(
bool hardFlush,
bool blocking) {
return false;}
00062 };
00063
00064
template <
class T>
00065
class Unflushable :
public T
00066 {
00067
public:
00068 Unflushable() {}
00069 Unflushable(
BufferedTransformation *q) : T(q) {}
00070
bool Flush(
bool completeFlush,
int propagation=-1,
bool blocking=
true)
00071 {
return ChannelFlush(NULL_CHANNEL, completeFlush, propagation);}
00072
bool IsolatedFlush(
bool hardFlush,
bool blocking)
00073 {assert(
false);
return false;}
00074
bool ChannelFlush(
const std::string &channel,
bool hardFlush,
int propagation=-1,
bool blocking=
true)
00075 {
00076
if (hardFlush && !InputBufferIsEmpty())
00077
throw CannotFlush(
"Unflushable<T>: this object has buffered input that cannot be flushed");
00078
else
00079 {
00080
BufferedTransformation *attached = AttachedTransformation();
00081
return attached && propagation ? attached->
ChannelFlush(channel, hardFlush, propagation-1, blocking) : false;
00082 }
00083 }
00084
00085
protected:
00086
virtual bool InputBufferIsEmpty()
const {
return false;}
00087 };
00088
00089
template <
class T>
00090
class InputRejecting :
public T
00091 {
00092
public:
00093 InputRejecting() {}
00094 InputRejecting(
BufferedTransformation *q) : T(q) {}
00095
00096
protected:
00097
struct InputRejected :
public NotImplemented
00098 {InputRejected() :
NotImplemented("
BufferedTransformation: this object doesn't allow input") {}};
00099
00100
00101
unsigned int Put2(
const byte *begin,
unsigned int length,
int messageEnd,
bool blocking)
00102 {
throw InputRejected();}
00103
bool IsolatedFlush(
bool,
bool) {
return false;}
00104
bool IsolatedMessageSeriesEnd(
bool) {
throw InputRejected();}
00105
00106
unsigned int ChannelPut2(
const std::string &channel,
const byte *begin,
unsigned int length,
int messageEnd,
bool blocking)
00107 {
throw InputRejected();}
00108
bool ChannelMessageSeriesEnd(
const std::string &,
int,
bool) {
throw InputRejected();}
00109 };
00110
00111
template <
class T>
00112
class CustomSignalPropagation :
public T
00113 {
00114
public:
00115 CustomSignalPropagation() {}
00116 CustomSignalPropagation(
BufferedTransformation *q) : T(q) {}
00117
00118
virtual void Initialize(
const NameValuePairs ¶meters=g_nullNameValuePairs,
int propagation=-1) =0;
00119
virtual bool Flush(
bool hardFlush,
int propagation=-1,
bool blocking=
true) =0;
00120
00121
private:
00122
void IsolatedInitialize(
const NameValuePairs ¶meters) {assert(
false);}
00123
bool IsolatedFlush(
bool hardFlush,
bool blocking) {assert(
false);
return false;}
00124 };
00125
00126
template <
class T>
00127
class Multichannel :
public CustomSignalPropagation<T>
00128 {
00129
public:
00130 Multichannel() {}
00131 Multichannel(
BufferedTransformation *q) : CustomSignalPropagation<T>(q) {}
00132
00133
void Initialize(
const NameValuePairs ¶meters,
int propagation)
00134 {ChannelInitialize(NULL_CHANNEL, parameters, propagation);}
00135
bool Flush(
bool hardFlush,
int propagation=-1,
bool blocking=
true)
00136 {
return ChannelFlush(NULL_CHANNEL, hardFlush, propagation, blocking);}
00137
bool MessageSeriesEnd(
int propagation=-1,
bool blocking=
true)
00138 {
return ChannelMessageSeriesEnd(NULL_CHANNEL, propagation, blocking);}
00139 byte * CreatePutSpace(
unsigned int &size)
00140 {
return ChannelCreatePutSpace(NULL_CHANNEL, size);}
00141
unsigned int Put2(
const byte *begin,
unsigned int length,
int messageEnd,
bool blocking)
00142 {
return ChannelPut2(NULL_CHANNEL, begin, length, messageEnd, blocking);}
00143
unsigned int PutModifiable2(byte *inString,
unsigned int length,
int messageEnd,
bool blocking)
00144 {
return ChannelPutModifiable2(NULL_CHANNEL, inString, length, messageEnd, blocking);}
00145
00146
00147
00148 byte * ChannelCreatePutSpace(
const std::string &channel,
unsigned int &size)
00149 {size = 0;
return NULL;}
00150
bool ChannelPutModifiable(
const std::string &channel, byte *inString,
unsigned int length)
00151 {ChannelPut(channel, inString, length);
return false;}
00152
00153
virtual unsigned int ChannelPut2(
const std::string &channel,
const byte *begin,
unsigned int length,
int messageEnd,
bool blocking) =0;
00154
unsigned int ChannelPutModifiable2(
const std::string &channel, byte *begin,
unsigned int length,
int messageEnd,
bool blocking)
00155 {
return ChannelPut2(channel, begin, length, messageEnd, blocking);}
00156
00157
virtual void ChannelInitialize(
const std::string &channel,
const NameValuePairs ¶meters=g_nullNameValuePairs,
int propagation=-1) =0;
00158
virtual bool ChannelFlush(
const std::string &channel,
bool hardFlush,
int propagation=-1,
bool blocking=
true) =0;
00159 };
00160
00161
template <
class T>
00162
class AutoSignaling :
public T
00163 {
00164
public:
00165 AutoSignaling(
int propagation=-1) : m_autoSignalPropagation(propagation) {}
00166 AutoSignaling(
BufferedTransformation *q,
int propagation=-1) : T(q), m_autoSignalPropagation(propagation) {}
00167
00168
void SetAutoSignalPropagation(
int propagation)
00169 {m_autoSignalPropagation = propagation;}
00170
int GetAutoSignalPropagation()
const
00171
{
return m_autoSignalPropagation;}
00172
00173
private:
00174
int m_autoSignalPropagation;
00175 };
00176
00177
00178 class Store :
public AutoSignaling<InputRejecting<BufferedTransformation> >
00179 {
00180
public:
00181
Store() : m_messageEnd(
false) {}
00182
00183
void IsolatedInitialize(
const NameValuePairs ¶meters)
00184 {
00185 m_messageEnd =
false;
00186 StoreInitialize(parameters);
00187 }
00188
00189
unsigned int NumberOfMessages()
const {
return m_messageEnd ? 0 : 1;}
00190
bool GetNextMessage();
00191
unsigned int CopyMessagesTo(
BufferedTransformation &target,
unsigned int count=UINT_MAX,
const std::string &channel=NULL_CHANNEL)
const;
00192
00193
protected:
00194
virtual void StoreInitialize(
const NameValuePairs ¶meters) =0;
00195
00196
bool m_messageEnd;
00197 };
00198
00199
00200 class Sink :
public BufferedTransformation
00201 {
00202
protected:
00203
00204 BufferedTransformation::Get;
00205 BufferedTransformation::Peek;
00206 BufferedTransformation::TransferTo;
00207 BufferedTransformation::CopyTo;
00208 BufferedTransformation::CopyRangeTo;
00209 BufferedTransformation::TransferMessagesTo;
00210 BufferedTransformation::CopyMessagesTo;
00211 BufferedTransformation::TransferAllTo;
00212 BufferedTransformation::CopyAllTo;
00213 unsigned int TransferTo2(
BufferedTransformation &target,
unsigned long &transferBytes,
const std::string &channel=NULL_CHANNEL,
bool blocking=
true)
00214 {transferBytes = 0;
return 0;}
00215
unsigned int CopyRangeTo2(
BufferedTransformation &target,
unsigned long &begin,
unsigned long end=ULONG_MAX,
const std::string &channel=NULL_CHANNEL,
bool blocking=
true)
const
00216
{
return 0;}
00217 };
00218
00219
class BitBucket :
public Bufferless<Sink>
00220 {
00221
public:
00222 std::string AlgorithmName()
const {
return "BitBucket";}
00223
void IsolatedInitialize(
const NameValuePairs ¶meters) {}
00224
unsigned int Put2(
const byte *begin,
unsigned int length,
int messageEnd,
bool blocking)
00225 {
return 0;}
00226 };
00227
00228 NAMESPACE_END
00229
00230
#endif