Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

simple.h

Go to the documentation of this file.
00001 // simple.h - written and placed in the public domain by Wei Dai
00002 /*! \file
00003         Simple non-interface classes derived from classes in cryptlib.h.
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 CRYPTOPP_DLL 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 CRYPTOPP_DLL 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 CRYPTOPP_DLL 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 // TODO: look into this virtual inheritance
00044 class CRYPTOPP_DLL 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         // shouldn't be calling these functions on this class
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 &parameters=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 &parameters) {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 &parameters, 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         void MessageSeriesEnd(int propagation)
00138                 {ChannelMessageSeriesEnd(NULL_CHANNEL, propagation);}
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 *begin, byte *end, int messageEnd, bool blocking)
00144                 {return ChannelPutModifiable2(NULL_CHANNEL, begin, end, messageEnd, blocking);}
00145 
00146 //      void ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1)
00147 //              {PropagateMessageSeriesEnd(propagation, channel);}
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 
00155         virtual void ChannelInitialize(const std::string &channel, const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1) =0;
00156         virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true) =0;
00157 };
00158 
00159 template <class T>
00160 class AutoSignaling : public T
00161 {
00162 public:
00163         AutoSignaling(int propagation=-1) : m_autoSignalPropagation(propagation) {}
00164         AutoSignaling(BufferedTransformation *q, int propagation=-1) : T(q), m_autoSignalPropagation(propagation) {}
00165 
00166         void SetAutoSignalPropagation(int propagation)
00167                 {m_autoSignalPropagation = propagation;}
00168         int GetAutoSignalPropagation() const
00169                 {return m_autoSignalPropagation;}
00170 
00171 private:
00172         int m_autoSignalPropagation;
00173 };
00174 
00175 //! A BufferedTransformation that only contains pre-existing data as "output"
00176 class CRYPTOPP_DLL Store : public AutoSignaling<InputRejecting<BufferedTransformation> >
00177 {
00178 public:
00179         Store() : m_messageEnd(false) {}
00180 
00181         void IsolatedInitialize(const NameValuePairs &parameters)
00182         {
00183                 m_messageEnd = false;
00184                 StoreInitialize(parameters);
00185         }
00186 
00187         unsigned int NumberOfMessages() const {return m_messageEnd ? 0 : 1;}
00188         bool GetNextMessage();
00189         unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=NULL_CHANNEL) const;
00190 
00191 protected:
00192         virtual void StoreInitialize(const NameValuePairs &parameters) =0;
00193 
00194         bool m_messageEnd;
00195 };
00196 
00197 //! A BufferedTransformation that doesn't produce any retrievable output
00198 class CRYPTOPP_DLL Sink : public BufferedTransformation
00199 {
00200 protected:
00201         // make these functions protected to help prevent unintentional calls to them
00202         BufferedTransformation::Get;
00203         BufferedTransformation::Peek;
00204         BufferedTransformation::TransferTo;
00205         BufferedTransformation::CopyTo;
00206         BufferedTransformation::CopyRangeTo;
00207         BufferedTransformation::TransferMessagesTo;
00208         BufferedTransformation::CopyMessagesTo;
00209         BufferedTransformation::TransferAllTo;
00210         BufferedTransformation::CopyAllTo;
00211         unsigned int TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true)
00212                 {transferBytes = 0; return 0;}
00213         unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const
00214                 {return 0;}
00215 };
00216 
00217 class CRYPTOPP_DLL BitBucket : public Bufferless<Sink>
00218 {
00219 public:
00220         std::string AlgorithmName() const {return "BitBucket";}
00221         void IsolatedInitialize(const NameValuePairs &parameters) {}
00222         unsigned int Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking)
00223                 {return 0;}
00224 };
00225 
00226 NAMESPACE_END
00227 
00228 #endif

Generated on Tue Jul 8 23:34:25 2003 for Crypto++ by doxygen 1.3.2