filters.h

00001 #ifndef CRYPTOPP_FILTERS_H
00002 #define CRYPTOPP_FILTERS_H
00003 
00004 #include "simple.h"
00005 #include "secblock.h"
00006 #include "misc.h"
00007 #include "smartptr.h"
00008 #include "queue.h"
00009 #include "algparam.h"
00010 #include <deque>
00011 
00012 NAMESPACE_BEGIN(CryptoPP)
00013 
00014 /// provides an implementation of BufferedTransformation's attachment interface
00015 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Filter : public BufferedTransformation, public NotCopyable
00016 {
00017 public:
00018         Filter(BufferedTransformation *attachment = NULL);
00019 
00020         bool Attachable() {return true;}
00021         BufferedTransformation *AttachedTransformation();
00022         const BufferedTransformation *AttachedTransformation() const;
00023         void Detach(BufferedTransformation *newAttachment = NULL);
00024 
00025         size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true);
00026         size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const;
00027 
00028         void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1);
00029         bool Flush(bool hardFlush, int propagation=-1, bool blocking=true);
00030         bool MessageSeriesEnd(int propagation=-1, bool blocking=true);
00031 
00032 protected:
00033         virtual BufferedTransformation * NewDefaultAttachment() const;
00034         void Insert(Filter *nextFilter);        // insert filter after this one
00035 
00036         virtual bool ShouldPropagateMessageEnd() const {return true;}
00037         virtual bool ShouldPropagateMessageSeriesEnd() const {return true;}
00038 
00039         void PropagateInitialize(const NameValuePairs &parameters, int propagation);
00040 
00041         size_t Output(int outputSite, const byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel=NULL_CHANNEL);
00042         size_t OutputModifiable(int outputSite, byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel=NULL_CHANNEL);
00043         bool OutputMessageEnd(int outputSite, int propagation, bool blocking, const std::string &channel=NULL_CHANNEL);
00044         bool OutputFlush(int outputSite, bool hardFlush, int propagation, bool blocking, const std::string &channel=NULL_CHANNEL);
00045         bool OutputMessageSeriesEnd(int outputSite, int propagation, bool blocking, const std::string &channel=NULL_CHANNEL);
00046 
00047 private:
00048         member_ptr<BufferedTransformation> m_attachment;
00049         
00050 protected:
00051         size_t m_inputPosition;
00052         int m_continueAt;
00053 };
00054 
00055 struct CRYPTOPP_DLL FilterPutSpaceHelper
00056 {
00057         // desiredSize is how much to ask target, bufferSize is how much to allocate in m_tempSpace
00058         byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize, size_t desiredSize, size_t &bufferSize)
00059         {
00060                 assert(desiredSize >= minSize && bufferSize >= minSize);
00061                 if (m_tempSpace.size() < minSize)
00062                 {
00063                         byte *result = target.ChannelCreatePutSpace(channel, desiredSize);
00064                         if (desiredSize >= minSize)
00065                         {
00066                                 bufferSize = desiredSize;
00067                                 return result;
00068                         }
00069                         m_tempSpace.New(bufferSize);
00070                 }
00071 
00072                 bufferSize = m_tempSpace.size();
00073                 return m_tempSpace.begin();
00074         }
00075         byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize)
00076                 {return HelpCreatePutSpace(target, channel, minSize, minSize, minSize);}
00077         byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize, size_t bufferSize)
00078                 {return HelpCreatePutSpace(target, channel, minSize, minSize, bufferSize);}
00079         SecByteBlock m_tempSpace;
00080 };
00081 
00082 //! measure how many byte and messages pass through, also serves as valve
00083 class CRYPTOPP_DLL MeterFilter : public Bufferless<Filter>
00084 {
00085 public:
00086         MeterFilter(BufferedTransformation *attachment=NULL, bool transparent=true)
00087                 : m_transparent(transparent) {Detach(attachment); ResetMeter();}
00088 
00089         void SetTransparent(bool transparent) {m_transparent = transparent;}
00090         void AddRangeToSkip(unsigned int message, lword position, lword size, bool sortNow = true);
00091         void ResetMeter();
00092         void IsolatedInitialize(const NameValuePairs &parameters) {ResetMeter();}
00093 
00094         lword GetCurrentMessageBytes() const {return m_currentMessageBytes;}
00095         lword GetTotalBytes() {return m_totalBytes;}
00096         unsigned int GetCurrentSeriesMessages() {return m_currentSeriesMessages;}
00097         unsigned int GetTotalMessages() {return m_totalMessages;}
00098         unsigned int GetTotalMessageSeries() {return m_totalMessageSeries;}
00099 
00100         byte * CreatePutSpace(size_t &size)
00101                 {return AttachedTransformation()->CreatePutSpace(size);}
00102         size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
00103         size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking);
00104         bool IsolatedMessageSeriesEnd(bool blocking);
00105 
00106 private:
00107         size_t PutMaybeModifiable(byte *inString, size_t length, int messageEnd, bool blocking, bool modifiable);
00108         bool ShouldPropagateMessageEnd() const {return m_transparent;}
00109         bool ShouldPropagateMessageSeriesEnd() const {return m_transparent;}
00110 
00111         struct MessageRange {unsigned int message; lword position; lword size;};
00112         friend inline bool operator<(const MessageRange &a, const MessageRange &b)
00113                 {return a.message < b.message || (a.message == b.message && a.position < b.position);}
00114 
00115         bool m_transparent;
00116         lword m_currentMessageBytes, m_totalBytes;
00117         unsigned int m_currentSeriesMessages, m_totalMessages, m_totalMessageSeries;
00118         std::deque<MessageRange> m_rangesToSkip;
00119         byte *m_begin;
00120         size_t m_length;
00121 };
00122 
00123 //! _
00124 class CRYPTOPP_DLL TransparentFilter : public MeterFilter
00125 {
00126 public:
00127         TransparentFilter(BufferedTransformation *attachment=NULL) : MeterFilter(attachment, true) {}
00128 };
00129 
00130 //! _
00131 class CRYPTOPP_DLL OpaqueFilter : public MeterFilter
00132 {
00133 public:
00134         OpaqueFilter(BufferedTransformation *attachment=NULL) : MeterFilter(attachment, false) {}
00135 };
00136 
00137 /*! FilterWithBufferedInput divides up the input stream into
00138         a first block, a number of middle blocks, and a last block.
00139         First and last blocks are optional, and middle blocks may
00140         be a stream instead (i.e. blockSize == 1).
00141 */
00142 class CRYPTOPP_DLL FilterWithBufferedInput : public Filter
00143 {
00144 public:
00145         FilterWithBufferedInput(BufferedTransformation *attachment);
00146         //! firstSize and lastSize may be 0, blockSize must be at least 1
00147         FilterWithBufferedInput(size_t firstSize, size_t blockSize, size_t lastSize, BufferedTransformation *attachment);
00148 
00149         void IsolatedInitialize(const NameValuePairs &parameters);
00150         size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
00151         {
00152                 return PutMaybeModifiable(const_cast<byte *>(inString), length, messageEnd, blocking, false);
00153         }
00154         size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
00155         {
00156                 return PutMaybeModifiable(inString, length, messageEnd, blocking, true);
00157         }
00158         /*! calls ForceNextPut() if hardFlush is true */
00159         bool IsolatedFlush(bool hardFlush, bool blocking);
00160 
00161         /*! The input buffer may contain more than blockSize bytes if lastSize != 0.
00162                 ForceNextPut() forces a call to NextPut() if this is the case.
00163         */
00164         void ForceNextPut();
00165 
00166 protected:
00167         bool DidFirstPut() {return m_firstInputDone;}
00168 
00169         virtual void InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
00170                 {InitializeDerived(parameters);}
00171         virtual void InitializeDerived(const NameValuePairs &parameters) {}
00172         // FirstPut() is called if (firstSize != 0 and totalLength >= firstSize)
00173         // or (firstSize == 0 and (totalLength > 0 or a MessageEnd() is received))
00174         virtual void FirstPut(const byte *inString) =0;
00175         // NextPut() is called if totalLength >= firstSize+blockSize+lastSize
00176         virtual void NextPutSingle(const byte *inString) {assert(false);}
00177         // Same as NextPut() except length can be a multiple of blockSize
00178         // Either NextPut() or NextPutMultiple() must be overriden
00179         virtual void NextPutMultiple(const byte *inString, size_t length);
00180         // Same as NextPutMultiple(), but inString can be modified
00181         virtual void NextPutModifiable(byte *inString, size_t length)
00182                 {NextPutMultiple(inString, length);}
00183         // LastPut() is always called
00184         // if totalLength < firstSize then length == totalLength
00185         // else if totalLength <= firstSize+lastSize then length == totalLength-firstSize
00186         // else lastSize <= length < lastSize+blockSize
00187         virtual void LastPut(const byte *inString, size_t length) =0;
00188         virtual void FlushDerived() {}
00189 
00190 private:
00191         size_t PutMaybeModifiable(byte *begin, size_t length, int messageEnd, bool blocking, bool modifiable);
00192         void NextPutMaybeModifiable(byte *inString, size_t length, bool modifiable)
00193         {
00194                 if (modifiable) NextPutModifiable(inString, length);
00195                 else NextPutMultiple(inString, length);
00196         }
00197 
00198         // This function should no longer be used, put this here to cause a compiler error
00199         // if someone tries to override NextPut().
00200         virtual int NextPut(const byte *inString, size_t length) {assert(false); return 0;}
00201 
00202         class BlockQueue
00203         {
00204         public:
00205                 void ResetQueue(size_t blockSize, size_t maxBlocks);
00206                 byte *GetBlock();
00207                 byte *GetContigousBlocks(size_t &numberOfBytes);
00208                 size_t GetAll(byte *outString);
00209                 void Put(const byte *inString, size_t length);
00210                 size_t CurrentSize() const {return m_size;}
00211                 size_t MaxSize() const {return m_buffer.size();}
00212 
00213         private:
00214                 SecByteBlock m_buffer;
00215                 size_t m_blockSize, m_maxBlocks, m_size;
00216                 byte *m_begin;
00217         };
00218 
00219         size_t m_firstSize, m_blockSize, m_lastSize;
00220         bool m_firstInputDone;
00221         BlockQueue m_queue;
00222 };
00223 
00224 //! _
00225 class CRYPTOPP_DLL FilterWithInputQueue : public Filter
00226 {
00227 public:
00228         FilterWithInputQueue(BufferedTransformation *attachment=NULL) : Filter(attachment) {}
00229 
00230         size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
00231         {
00232                 if (!blocking)
00233                         throw BlockingInputOnly("FilterWithInputQueue");
00234                 
00235                 m_inQueue.Put(inString, length);
00236                 if (messageEnd)
00237                 {
00238                         IsolatedMessageEnd(blocking);
00239                         Output(0, NULL, 0, messageEnd, blocking);
00240                 }
00241                 return 0;
00242         }
00243 
00244 protected:
00245         virtual bool IsolatedMessageEnd(bool blocking) =0;
00246         void IsolatedInitialize(const NameValuePairs &parameters) {m_inQueue.Clear();}
00247 
00248         ByteQueue m_inQueue;
00249 };
00250 
00251 //! Filter Wrapper for StreamTransformation
00252 class CRYPTOPP_DLL StreamTransformationFilter : public FilterWithBufferedInput, private FilterPutSpaceHelper
00253 {
00254 public:
00255         enum BlockPaddingScheme {NO_PADDING, ZEROS_PADDING, PKCS_PADDING, ONE_AND_ZEROS_PADDING, DEFAULT_PADDING};
00256         /*! DEFAULT_PADDING means PKCS_PADDING if c.MandatoryBlockSize() > 1 && c.MinLastBlockSize() == 0 (e.g. ECB or CBC mode),
00257                 otherwise NO_PADDING (OFB, CFB, CTR, CBC-CTS modes) */
00258         StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment = NULL, BlockPaddingScheme padding = DEFAULT_PADDING);
00259 
00260         void FirstPut(const byte *inString);
00261         void NextPutMultiple(const byte *inString, size_t length);
00262         void NextPutModifiable(byte *inString, size_t length);
00263         void LastPut(const byte *inString, size_t length);
00264 //      byte * CreatePutSpace(size_t &size);
00265 
00266 protected:
00267         static size_t LastBlockSize(StreamTransformation &c, BlockPaddingScheme padding);
00268 
00269         StreamTransformation &m_cipher;
00270         BlockPaddingScheme m_padding;
00271         unsigned int m_optimalBufferSize;
00272 };
00273 
00274 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
00275 typedef StreamTransformationFilter StreamCipherFilter;
00276 #endif
00277 
00278 //! Filter Wrapper for HashTransformation
00279 class CRYPTOPP_DLL HashFilter : public Bufferless<Filter>, private FilterPutSpaceHelper
00280 {
00281 public:
00282         HashFilter(HashTransformation &hm, BufferedTransformation *attachment = NULL, bool putMessage=false, int truncatedDigestSize=-1)
00283                 : m_hashModule(hm), m_putMessage(putMessage), m_truncatedDigestSize(truncatedDigestSize) {Detach(attachment);}
00284 
00285         void IsolatedInitialize(const NameValuePairs &parameters);
00286         size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
00287 
00288         byte * CreatePutSpace(size_t &size) {return m_hashModule.CreateUpdateSpace(size);}
00289 
00290 private:
00291         HashTransformation &m_hashModule;
00292         bool m_putMessage;
00293         int m_truncatedDigestSize;
00294         byte *m_space;
00295         unsigned int m_digestSize;
00296 };
00297 
00298 //! Filter Wrapper for HashTransformation
00299 class CRYPTOPP_DLL HashVerificationFilter : public FilterWithBufferedInput
00300 {
00301 public:
00302         class HashVerificationFailed : public Exception
00303         {
00304         public:
00305                 HashVerificationFailed()
00306                         : Exception(DATA_INTEGRITY_CHECK_FAILED, "HashVerifier: message hash not valid") {}
00307         };
00308 
00309         enum Flags {HASH_AT_BEGIN=1, PUT_MESSAGE=2, PUT_HASH=4, PUT_RESULT=8, THROW_EXCEPTION=16, DEFAULT_FLAGS = HASH_AT_BEGIN | PUT_RESULT};
00310         HashVerificationFilter(HashTransformation &hm, BufferedTransformation *attachment = NULL, word32 flags = DEFAULT_FLAGS);
00311 
00312         bool GetLastResult() const {return m_verified;}
00313 
00314 protected:
00315         void InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
00316         void FirstPut(const byte *inString);
00317         void NextPutMultiple(const byte *inString, size_t length);
00318         void LastPut(const byte *inString, size_t length);
00319 
00320 private:
00321         static inline unsigned int FirstSize(word32 flags, HashTransformation &hm) {return flags & HASH_AT_BEGIN ? hm.DigestSize() : 0;}
00322         static inline unsigned int LastSize(word32 flags, HashTransformation &hm) {return flags & HASH_AT_BEGIN ? 0 : hm.DigestSize();}
00323 
00324         HashTransformation &m_hashModule;
00325         word32 m_flags;
00326         SecByteBlock m_expectedHash;
00327         bool m_verified;
00328 };
00329 
00330 typedef HashVerificationFilter HashVerifier;    // for backwards compatibility
00331 
00332 //! Filter Wrapper for PK_Signer
00333 class CRYPTOPP_DLL SignerFilter : public Unflushable<Filter>
00334 {
00335 public:
00336         SignerFilter(RandomNumberGenerator &rng, const PK_Signer &signer, BufferedTransformation *attachment = NULL, bool putMessage=false)
00337                 : m_rng(rng), m_signer(signer), m_messageAccumulator(signer.NewSignatureAccumulator(rng)), m_putMessage(putMessage) {Detach(attachment);}
00338 
00339         void IsolatedInitialize(const NameValuePairs &parameters);
00340         size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
00341 
00342 private:
00343         RandomNumberGenerator &m_rng;
00344         const PK_Signer &m_signer;
00345         member_ptr<PK_MessageAccumulator> m_messageAccumulator;
00346         bool m_putMessage;
00347         SecByteBlock m_buf;
00348 };
00349 
00350 //! Filter Wrapper for PK_Verifier
00351 class CRYPTOPP_DLL SignatureVerificationFilter : public FilterWithBufferedInput
00352 {
00353 public:
00354         class SignatureVerificationFailed : public Exception
00355         {
00356         public:
00357                 SignatureVerificationFailed()
00358                         : Exception(DATA_INTEGRITY_CHECK_FAILED, "VerifierFilter: digital signature not valid") {}
00359         };
00360 
00361         enum Flags {SIGNATURE_AT_BEGIN=1, PUT_MESSAGE=2, PUT_SIGNATURE=4, PUT_RESULT=8, THROW_EXCEPTION=16, DEFAULT_FLAGS = SIGNATURE_AT_BEGIN | PUT_RESULT};
00362         SignatureVerificationFilter(const PK_Verifier &verifier, BufferedTransformation *attachment = NULL, word32 flags = DEFAULT_FLAGS);
00363 
00364         bool GetLastResult() const {return m_verified;}
00365 
00366 protected:
00367         void InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
00368         void FirstPut(const byte *inString);
00369         void NextPutMultiple(const byte *inString, size_t length);
00370         void LastPut(const byte *inString, size_t length);
00371 
00372 private:
00373         const PK_Verifier &m_verifier;
00374         member_ptr<PK_MessageAccumulator> m_messageAccumulator;
00375         word32 m_flags;
00376         SecByteBlock m_signature;
00377         bool m_verified;
00378 };
00379 
00380 typedef SignatureVerificationFilter VerifierFilter;     // for backwards compatibility
00381 
00382 //! Redirect input to another BufferedTransformation without owning it
00383 class CRYPTOPP_DLL Redirector : public CustomSignalPropagation<Sink>
00384 {
00385 public:
00386         enum Behavior
00387         {
00388                 DATA_ONLY = 0x00,
00389                 PASS_SIGNALS = 0x01,
00390                 PASS_WAIT_OBJECTS = 0x02,
00391                 PASS_EVERYTHING = PASS_SIGNALS | PASS_WAIT_OBJECTS
00392         };
00393 
00394         Redirector() : m_target(NULL), m_behavior(PASS_EVERYTHING) {}
00395         Redirector(BufferedTransformation &target, Behavior behavior=PASS_EVERYTHING)
00396                 : m_target(&target), m_behavior(behavior) {}
00397 
00398         void Redirect(BufferedTransformation &target) {m_target = &target;}
00399         void StopRedirection() {m_target = NULL;}
00400 
00401         Behavior GetBehavior() {return (Behavior) m_behavior;}
00402         void SetBehavior(Behavior behavior) {m_behavior=behavior;}
00403         bool GetPassSignals() const {return (m_behavior & PASS_SIGNALS) != 0;}
00404         void SetPassSignals(bool pass) { if (pass) m_behavior |= PASS_SIGNALS; else m_behavior &= ~(word32) PASS_SIGNALS; }
00405         bool GetPassWaitObjects() const {return (m_behavior & PASS_WAIT_OBJECTS) != 0;}
00406         void SetPassWaitObjects(bool pass) { if (pass) m_behavior |= PASS_WAIT_OBJECTS; else m_behavior &= ~(word32) PASS_WAIT_OBJECTS; }
00407 
00408         bool CanModifyInput() const
00409                 {return m_target ? m_target->CanModifyInput() : false;}
00410 
00411         void Initialize(const NameValuePairs &parameters, int propagation);
00412         byte * CreatePutSpace(size_t &size)
00413                 {return m_target ? m_target->CreatePutSpace(size) : (byte *)(size=0, NULL);}
00414         size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
00415                 {return m_target ? m_target->Put2(begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
00416         bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
00417                 {return m_target && GetPassSignals() ? m_target->Flush(hardFlush, propagation, blocking) : false;}
00418         bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
00419                 {return m_target && GetPassSignals() ? m_target->MessageSeriesEnd(propagation, blocking) : false;}
00420 
00421         byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
00422                 {return m_target ? m_target->ChannelCreatePutSpace(channel, size) : (byte *)(size=0, NULL);}
00423         size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
00424                 {return m_target ? m_target->ChannelPut2(channel, begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
00425         size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
00426                 {return m_target ? m_target->ChannelPutModifiable2(channel, begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
00427         bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true)
00428                 {return m_target && GetPassSignals() ? m_target->ChannelFlush(channel, completeFlush, propagation, blocking) : false;}
00429         bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true)
00430                 {return m_target && GetPassSignals() ? m_target->ChannelMessageSeriesEnd(channel, propagation, blocking) : false;}
00431 
00432         unsigned int GetMaxWaitObjectCount() const
00433                 { return m_target && GetPassWaitObjects() ? m_target->GetMaxWaitObjectCount() : 0; }
00434         void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack)
00435                 { if (m_target && GetPassWaitObjects()) m_target->GetWaitObjects(container, callStack); }
00436 
00437 private:
00438         BufferedTransformation *m_target;
00439         word32 m_behavior;
00440 };
00441 
00442 // Used By ProxyFilter
00443 class CRYPTOPP_DLL OutputProxy : public CustomSignalPropagation<Sink>
00444 {
00445 public:
00446         OutputProxy(BufferedTransformation &owner, bool passSignal) : m_owner(owner), m_passSignal(passSignal) {}
00447 
00448         bool GetPassSignal() const {return m_passSignal;}
00449         void SetPassSignal(bool passSignal) {m_passSignal = passSignal;}
00450 
00451         byte * CreatePutSpace(size_t &size)
00452                 {return m_owner.AttachedTransformation()->CreatePutSpace(size);}
00453         size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
00454                 {return m_owner.AttachedTransformation()->Put2(begin, length, m_passSignal ? messageEnd : 0, blocking);}
00455         size_t PutModifiable2(byte *begin, size_t length, int messageEnd, bool blocking)
00456                 {return m_owner.AttachedTransformation()->PutModifiable2(begin, length, m_passSignal ? messageEnd : 0, blocking);}
00457         void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1)
00458                 {if (m_passSignal) m_owner.AttachedTransformation()->Initialize(parameters, propagation);}
00459         bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
00460                 {return m_passSignal ? m_owner.AttachedTransformation()->Flush(hardFlush, propagation, blocking) : false;}
00461         bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
00462                 {return m_passSignal ? m_owner.AttachedTransformation()->MessageSeriesEnd(propagation, blocking) : false;}
00463 
00464         size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
00465                 {return m_owner.AttachedTransformation()->ChannelPut2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking);}
00466         size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
00467                 {return m_owner.AttachedTransformation()->ChannelPutModifiable2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking);}
00468         bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true)
00469                 {return m_passSignal ? m_owner.AttachedTransformation()->ChannelFlush(channel, completeFlush, propagation, blocking) : false;}
00470         bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true)
00471                 {return m_passSignal ? m_owner.AttachedTransformation()->ChannelMessageSeriesEnd(channel, propagation, blocking) : false;}
00472 
00473 private:
00474         BufferedTransformation &m_owner;
00475         bool m_passSignal;
00476 };
00477 
00478 //! Base class for Filter classes that are proxies for a chain of other filters.
00479 class CRYPTOPP_DLL ProxyFilter : public FilterWithBufferedInput
00480 {
00481 public:
00482         ProxyFilter(BufferedTransformation *filter, size_t firstSize, size_t lastSize, BufferedTransformation *attachment);
00483 
00484         bool IsolatedFlush(bool hardFlush, bool blocking);
00485 
00486         void SetFilter(Filter *filter);
00487         void NextPutMultiple(const byte *s, size_t len);
00488         void NextPutModifiable(byte *inString, size_t length);
00489 
00490 protected:
00491         member_ptr<BufferedTransformation> m_filter;
00492 };
00493 
00494 //! simple proxy filter that doesn't modify the underlying filter's input or output
00495 class CRYPTOPP_DLL SimpleProxyFilter : public ProxyFilter
00496 {
00497 public:
00498         SimpleProxyFilter(BufferedTransformation *filter, BufferedTransformation *attachment)
00499                 : ProxyFilter(filter, 0, 0, attachment) {}
00500 
00501         void FirstPut(const byte *) {}
00502         void LastPut(const byte *, size_t) {m_filter->MessageEnd();}
00503 };
00504 
00505 //! proxy for the filter created by PK_Encryptor::CreateEncryptionFilter
00506 /*! This class is here just to provide symmetry with VerifierFilter. */
00507 class CRYPTOPP_DLL PK_EncryptorFilter : public SimpleProxyFilter
00508 {
00509 public:
00510         PK_EncryptorFilter(RandomNumberGenerator &rng, const PK_Encryptor &encryptor, BufferedTransformation *attachment = NULL)
00511                 : SimpleProxyFilter(encryptor.CreateEncryptionFilter(rng), attachment) {}
00512 };
00513 
00514 //! proxy for the filter created by PK_Decryptor::CreateDecryptionFilter
00515 /*! This class is here just to provide symmetry with SignerFilter. */
00516 class CRYPTOPP_DLL PK_DecryptorFilter : public SimpleProxyFilter
00517 {
00518 public:
00519         PK_DecryptorFilter(RandomNumberGenerator &rng, const PK_Decryptor &decryptor, BufferedTransformation *attachment = NULL)
00520                 : SimpleProxyFilter(decryptor.CreateDecryptionFilter(rng), attachment) {}
00521 };
00522 
00523 //! Append input to a string object
00524 template <class T>
00525 class StringSinkTemplate : public Bufferless<Sink>
00526 {
00527 public:
00528         // VC60 workaround: no T::char_type
00529         typedef typename T::traits_type::char_type char_type;
00530 
00531         StringSinkTemplate(T &output)
00532                 : m_output(&output) {assert(sizeof(output[0])==1);}
00533 
00534         void IsolatedInitialize(const NameValuePairs &parameters)
00535                 {if (!parameters.GetValue("OutputStringPointer", m_output)) throw InvalidArgument("StringSink: OutputStringPointer not specified");}
00536 
00537         size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
00538         {
00539                 if (length > 0)
00540                 {
00541                         typename T::size_type size = m_output->size();
00542                         if (length < size && size + length > m_output->capacity())
00543                                 m_output->reserve(2*size);
00544                 m_output->append((const char_type *)begin, (const char_type *)begin+length);
00545                 }
00546                 return 0;
00547         }
00548 
00549 private:        
00550         T *m_output;
00551 };
00552 
00553 //! Append input to an std::string
00554 CRYPTOPP_DLL_TEMPLATE_CLASS StringSinkTemplate<std::string>;
00555 typedef StringSinkTemplate<std::string> StringSink;
00556 
00557 //! Copy input to a memory buffer
00558 class CRYPTOPP_DLL ArraySink : public Bufferless<Sink>
00559 {
00560 public:
00561         ArraySink(const NameValuePairs &parameters = g_nullNameValuePairs) {IsolatedInitialize(parameters);}
00562         ArraySink(byte *buf, size_t size) : m_buf(buf), m_size(size), m_total(0) {}
00563 
00564         size_t AvailableSize() {return SaturatingSubtract(m_size, m_total);}
00565         lword TotalPutLength() {return m_total;}
00566 
00567         void IsolatedInitialize(const NameValuePairs &parameters);
00568         byte * CreatePutSpace(size_t &size);
00569         size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
00570 
00571 protected:
00572         byte *m_buf;
00573         size_t m_size;
00574         lword m_total;
00575 };
00576 
00577 //! Xor input to a memory buffer
00578 class CRYPTOPP_DLL ArrayXorSink : public ArraySink
00579 {
00580 public:
00581         ArrayXorSink(byte *buf, size_t size)
00582                 : ArraySink(buf, size) {}
00583 
00584         size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking);
00585         byte * CreatePutSpace(size_t &size) {return BufferedTransformation::CreatePutSpace(size);}
00586 };
00587 
00588 //! string-based implementation of Store interface
00589 class StringStore : public Store
00590 {
00591 public:
00592         StringStore(const char *string = NULL)
00593                 {StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
00594         StringStore(const byte *string, size_t length)
00595                 {StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string, length)));}
00596         template <class T> StringStore(const T &string)
00597                 {StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
00598 
00599         CRYPTOPP_DLL size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true);
00600         CRYPTOPP_DLL size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const;
00601 
00602 private:
00603         CRYPTOPP_DLL void StoreInitialize(const NameValuePairs &parameters);
00604 
00605         const byte *m_store;
00606         size_t m_length, m_count;
00607 };
00608 
00609 //! RNG-based implementation of Source interface
00610 class CRYPTOPP_DLL RandomNumberStore : public Store
00611 {
00612 public:
00613         RandomNumberStore()
00614                 : m_rng(NULL), m_length(0), m_count(0) {}
00615 
00616         RandomNumberStore(RandomNumberGenerator &rng, lword length)
00617                 : m_rng(&rng), m_length(length), m_count(0) {}
00618 
00619         bool AnyRetrievable() const {return MaxRetrievable() != 0;}
00620         lword MaxRetrievable() const {return m_length-m_count;}
00621 
00622         size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true);
00623         size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const
00624         {
00625                 throw NotImplemented("RandomNumberStore: CopyRangeTo2() is not supported by this store");
00626         }
00627 
00628 private:
00629         void StoreInitialize(const NameValuePairs &parameters);
00630 
00631         RandomNumberGenerator *m_rng;
00632         lword m_length, m_count;
00633 };
00634 
00635 //! empty store
00636 class CRYPTOPP_DLL NullStore : public Store
00637 {
00638 public:
00639         NullStore(lword size = ULONG_MAX) : m_size(size) {}
00640         void StoreInitialize(const NameValuePairs &parameters) {}
00641         lword MaxRetrievable() const {return m_size;}
00642         size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true);
00643         size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const;
00644 
00645 private:
00646         lword m_size;
00647 };
00648 
00649 //! A Filter that pumps data into its attachment as input
00650 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Source : public InputRejecting<Filter>
00651 {
00652 public:
00653         Source(BufferedTransformation *attachment = NULL)
00654                 {Source::Detach(attachment);}
00655 
00656         lword Pump(lword pumpMax=size_t(0)-1)
00657                 {Pump2(pumpMax); return pumpMax;}
00658         unsigned int PumpMessages(unsigned int count=UINT_MAX)
00659                 {PumpMessages2(count); return count;}
00660         void PumpAll()
00661                 {PumpAll2();}
00662         virtual size_t Pump2(lword &byteCount, bool blocking=true) =0;
00663         virtual size_t PumpMessages2(unsigned int &messageCount, bool blocking=true) =0;
00664         virtual size_t PumpAll2(bool blocking=true);
00665         virtual bool SourceExhausted() const =0;
00666 
00667 protected:
00668         void SourceInitialize(bool pumpAll, const NameValuePairs &parameters)
00669         {
00670                 IsolatedInitialize(parameters);
00671                 if (pumpAll)
00672                         PumpAll();
00673         }
00674 };
00675 
00676 //! Turn a Store into a Source
00677 template <class T>
00678 class SourceTemplate : public Source
00679 {
00680 public:
00681         SourceTemplate<T>(BufferedTransformation *attachment)
00682                 : Source(attachment) {}
00683         void IsolatedInitialize(const NameValuePairs &parameters)
00684                 {m_store.IsolatedInitialize(parameters);}
00685         size_t Pump2(lword &byteCount, bool blocking=true)
00686                 {return m_store.TransferTo2(*AttachedTransformation(), byteCount, NULL_CHANNEL, blocking);}
00687         size_t PumpMessages2(unsigned int &messageCount, bool blocking=true)
00688                 {return m_store.TransferMessagesTo2(*AttachedTransformation(), messageCount, NULL_CHANNEL, blocking);}
00689         size_t PumpAll2(bool blocking=true)
00690                 {return m_store.TransferAllTo2(*AttachedTransformation(), NULL_CHANNEL, blocking);}
00691         bool SourceExhausted() const
00692                 {return !m_store.AnyRetrievable() && !m_store.AnyMessages();}
00693         void SetAutoSignalPropagation(int propagation)
00694                 {m_store.SetAutoSignalPropagation(propagation);}
00695         int GetAutoSignalPropagation() const
00696                 {return m_store.GetAutoSignalPropagation();}
00697 
00698 protected:
00699         T m_store;
00700 };
00701 
00702 //! string-based implementation of Source interface
00703 class CRYPTOPP_DLL StringSource : public SourceTemplate<StringStore>
00704 {
00705 public:
00706         StringSource(BufferedTransformation *attachment = NULL)
00707                 : SourceTemplate<StringStore>(attachment) {}
00708         StringSource(const char *string, bool pumpAll, BufferedTransformation *attachment = NULL)
00709                 : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
00710         StringSource(const byte *string, size_t length, bool pumpAll, BufferedTransformation *attachment = NULL)
00711                 : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string, length)));}
00712         StringSource(const std::string &string, bool pumpAll, BufferedTransformation *attachment = NULL)
00713                 : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
00714 };
00715 
00716 //! RNG-based implementation of Source interface
00717 class CRYPTOPP_DLL RandomNumberSource : public SourceTemplate<RandomNumberStore>
00718 {
00719 public:
00720         RandomNumberSource(RandomNumberGenerator &rng, int length, bool pumpAll, BufferedTransformation *attachment = NULL)
00721                 : SourceTemplate<RandomNumberStore>(attachment) 
00722                 {SourceInitialize(pumpAll, MakeParameters("RandomNumberGeneratorPointer", &rng)("RandomNumberStoreSize", length));}
00723 };
00724 
00725 NAMESPACE_END
00726 
00727 #endif

Generated on Thu Nov 23 15:57:39 2006 for Crypto++ by  doxygen 1.5.1-p1