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 12NAMESPACE_BEGIN(CryptoPP) 13 14//! _ 15template <class DERIVED, class BASE> 16class CRYPTOPP_NO_VTABLE ClonableImpl : public BASE 17{ 18public: 19 Clonable * Clone() const {return new DERIVED(*static_cast<const DERIVED *>(this));} 20}; 21 22//! _ 23template <class BASE, class ALGORITHM_INFO=BASE> 24class CRYPTOPP_NO_VTABLE AlgorithmImpl : public BASE 25{ 26public: 27 static std::string CRYPTOPP_API StaticAlgorithmName() {return ALGORITHM_INFO::StaticAlgorithmName();} 28 std::string AlgorithmName() const {return ALGORITHM_INFO::StaticAlgorithmName();} 29}; 30 31//! _ 32class CRYPTOPP_DLL InvalidKeyLength : public InvalidArgument 33{ 34public: 35 explicit InvalidKeyLength(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid key length") {} 36}; 37 38//! _ 39class CRYPTOPP_DLL InvalidRounds : public InvalidArgument 40{ 41public: 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//! _ 48template <class T> 49class CRYPTOPP_NO_VTABLE Bufferless : public T 50{ 51public: 52 bool IsolatedFlush(bool hardFlush, bool blocking) {return false;} 53}; 54 55//! _ 56template <class T> 57class CRYPTOPP_NO_VTABLE Unflushable : public T 58{ 59public: 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 75protected: 76 virtual bool InputBufferIsEmpty() const {return false;} 77}; 78 79//! _ 80template <class T> 81class CRYPTOPP_NO_VTABLE InputRejecting : public T 82{ 83public: 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//! _ 99template <class T> 100class CRYPTOPP_NO_VTABLE CustomFlushPropagation : public T 101{ 102public: 103 virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) =0; 104 105private: 106 bool IsolatedFlush(bool hardFlush, bool blocking) {assert(false); return false;} 107}; 108 109//! _ 110template <class T> 111class CRYPTOPP_NO_VTABLE CustomSignalPropagation : public CustomFlushPropagation<T> 112{ 113public: 114 virtual void Initialize(const NameValuePairs ¶meters=g_nullNameValuePairs, int propagation=-1) =0; 115 116private: 117 void IsolatedInitialize(const NameValuePairs ¶meters) {assert(false);} 118}; 119 120//! _ 121template <class T> 122class CRYPTOPP_NO_VTABLE Multichannel : public CustomFlushPropagation<T> 123{ 124public: 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//! _ 151template <class T> 152class CRYPTOPP_NO_VTABLE AutoSignaling : public T 153{ 154public: 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 162private: 163 int m_autoSignalPropagation; 164}; 165 166//! A BufferedTransformation that only contains pre-existing data as "output" 167class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Store : public AutoSignaling<InputRejecting<BufferedTransformation> > 168{ 169public: 170 Store() : m_messageEnd(false) {} 171 172 void IsolatedInitialize(const NameValuePairs ¶meters) 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 182protected: 183 virtual void StoreInitialize(const NameValuePairs ¶meters) =0; 184 185 bool m_messageEnd; 186}; 187 188//! A BufferedTransformation that doesn't produce any retrievable output 189class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Sink : public BufferedTransformation 190{ 191public: 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 198class CRYPTOPP_DLL BitBucket : public Bufferless<Sink> 199{ 200public: 201 std::string AlgorithmName() const {return "BitBucket";} 202 void IsolatedInitialize(const NameValuePairs ¶meters) {} 203 size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking) 204 {return 0;} 205}; 206 207NAMESPACE_END 208 209#endif 210