?? filters.h
字號:
#ifndef CRYPTOPP_FILTERS_H#define CRYPTOPP_FILTERS_H#include "simple.h"#include "secblock.h"#include "misc.h"#include "smartptr.h"#include "queue.h"#include "algparam.h"NAMESPACE_BEGIN(CryptoPP)/// provides an implementation of BufferedTransformation's attachment interfaceclass Filter : public BufferedTransformation, public NotCopyable{public: Filter(BufferedTransformation *attachment); bool Attachable() {return true;} BufferedTransformation *AttachedTransformation(); const BufferedTransformation *AttachedTransformation() const; void Detach(BufferedTransformation *newAttachment = NULL); unsigned int TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true); unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const; void Initialize(const NameValuePairs ¶meters=g_nullNameValuePairs, int propagation=-1); bool Flush(bool hardFlush, int propagation=-1, bool blocking=true); bool MessageSeriesEnd(int propagation=-1, bool blocking=true);protected: virtual void NotifyAttachmentChange() {} virtual BufferedTransformation * NewDefaultAttachment() const; void Insert(Filter *nextFilter); // insert filter after this one virtual bool ShouldPropagateMessageEnd() const {return true;} virtual bool ShouldPropagateMessageSeriesEnd() const {return true;} void PropagateInitialize(const NameValuePairs ¶meters, int propagation, const std::string &channel=NULL_CHANNEL); unsigned int Output(int outputSite, const byte *inString, unsigned int length, int messageEnd, bool blocking, const std::string &channel=NULL_CHANNEL); bool OutputMessageEnd(int outputSite, int propagation, bool blocking, const std::string &channel=NULL_CHANNEL); bool OutputFlush(int outputSite, bool hardFlush, int propagation, bool blocking, const std::string &channel=NULL_CHANNEL); bool OutputMessageSeriesEnd(int outputSite, int propagation, bool blocking, const std::string &channel=NULL_CHANNEL);private: member_ptr<BufferedTransformation> m_attachment; protected: unsigned int m_inputPosition; int m_continueAt;};struct FilterPutSpaceHelper{ // desiredSize is how much to ask target, bufferSize is how much to allocate in m_tempSpace byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, unsigned int minSize, unsigned int desiredSize, unsigned int &bufferSize) { assert(desiredSize >= minSize && bufferSize >= minSize); if (m_tempSpace.size() < minSize) { byte *result = target.ChannelCreatePutSpace(channel, desiredSize); if (desiredSize >= minSize) { bufferSize = desiredSize; return result; } m_tempSpace.New(bufferSize); } bufferSize = m_tempSpace.size(); return m_tempSpace.begin(); } byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, unsigned int minSize) {return HelpCreatePutSpace(target, channel, minSize, minSize, minSize);} byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, unsigned int minSize, unsigned int bufferSize) {return HelpCreatePutSpace(target, channel, minSize, minSize, bufferSize);} SecByteBlock m_tempSpace;};//! measure how many byte and messages pass through, also serves as valveclass MeterFilter : public Bufferless<Filter>{public: MeterFilter(BufferedTransformation *attachment=NULL, bool transparent=true) : Bufferless<Filter>(attachment), m_transparent(transparent) {ResetMeter();} void SetTransparent(bool transparent) {m_transparent = transparent;} void ResetMeter() {m_currentMessageBytes = m_totalBytes = m_currentSeriesMessages = m_totalMessages = m_totalMessageSeries = 0;} unsigned long GetCurrentMessageBytes() const {return m_currentMessageBytes;} unsigned long GetTotalBytes() {return m_totalBytes;} unsigned int GetCurrentSeriesMessages() {return m_currentSeriesMessages;} unsigned int GetTotalMessages() {return m_totalMessages;} unsigned int GetTotalMessageSeries() {return m_totalMessageSeries;} unsigned int Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking); bool IsolatedMessageSeriesEnd(bool blocking);private: bool ShouldPropagateMessageEnd() const {return m_transparent;} bool ShouldPropagateMessageSeriesEnd() const {return m_transparent;} bool m_transparent; unsigned long m_currentMessageBytes, m_totalBytes; unsigned int m_currentSeriesMessages, m_totalMessages, m_totalMessageSeries;};//! .class TransparentFilter : public MeterFilter{public: TransparentFilter(BufferedTransformation *attachment=NULL) : MeterFilter(attachment, true) {}};//! .class OpaqueFilter : public MeterFilter{public: OpaqueFilter(BufferedTransformation *attachment=NULL) : MeterFilter(attachment, false) {}};/*! FilterWithBufferedInput divides up the input stream into a first block, a number of middle blocks, and a last block. First and last blocks are optional, and middle blocks may be a stream instead (i.e. blockSize == 1).*/class FilterWithBufferedInput : public Filter{public: FilterWithBufferedInput(BufferedTransformation *attachment); //! firstSize and lastSize may be 0, blockSize must be at least 1 FilterWithBufferedInput(unsigned int firstSize, unsigned int blockSize, unsigned int lastSize, BufferedTransformation *attachment); void IsolatedInitialize(const NameValuePairs ¶meters); unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking) { return PutMaybeModifiable(const_cast<byte *>(inString), length, messageEnd, blocking, false); } unsigned int PutModifiable2(byte *inString, unsigned int length, int messageEnd, bool blocking) { return PutMaybeModifiable(inString, length, messageEnd, blocking, true); } /*! calls ForceNextPut() if hardFlush is true */ bool IsolatedFlush(bool hardFlush, bool blocking); /*! The input buffer may contain more than blockSize bytes if lastSize != 0. ForceNextPut() forces a call to NextPut() if this is the case. */ void ForceNextPut();protected: bool DidFirstPut() {return m_firstInputDone;} virtual void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, unsigned int &firstSize, unsigned int &blockSize, unsigned int &lastSize) {InitializeDerived(parameters);} virtual void InitializeDerived(const NameValuePairs ¶meters) {} // FirstPut() is called if (firstSize != 0 and totalLength >= firstSize) // or (firstSize == 0 and (totalLength > 0 or a MessageEnd() is received)) virtual void FirstPut(const byte *inString) =0; // NextPut() is called if totalLength >= firstSize+blockSize+lastSize virtual void NextPutSingle(const byte *inString) {assert(false);} // Same as NextPut() except length can be a multiple of blockSize // Either NextPut() or NextPutMultiple() must be overriden virtual void NextPutMultiple(const byte *inString, unsigned int length); // Same as NextPutMultiple(), but inString can be modified virtual void NextPutModifiable(byte *inString, unsigned int length) {NextPutMultiple(inString, length);} // LastPut() is always called // if totalLength < firstSize then length == totalLength // else if totalLength <= firstSize+lastSize then length == totalLength-firstSize // else lastSize <= length < lastSize+blockSize virtual void LastPut(const byte *inString, unsigned int length) =0; virtual void FlushDerived() {}private: unsigned int PutMaybeModifiable(byte *begin, unsigned int length, int messageEnd, bool blocking, bool modifiable); void NextPutMaybeModifiable(byte *inString, unsigned int length, bool modifiable) { if (modifiable) NextPutModifiable(inString, length); else NextPutMultiple(inString, length); } // This function should no longer be used, put this here to cause a compiler error // if someone tries to override NextPut(). virtual int NextPut(const byte *inString, unsigned int length) {assert(false); return 0;} class BlockQueue { public: void ResetQueue(unsigned int blockSize, unsigned int maxBlocks); byte *GetBlock(); byte *GetContigousBlocks(unsigned int &numberOfBytes); unsigned int GetAll(byte *outString); void Put(const byte *inString, unsigned int length); unsigned int CurrentSize() const {return m_size;} unsigned int MaxSize() const {return m_buffer.size();} private: SecByteBlock m_buffer; unsigned int m_blockSize, m_maxBlocks, m_size; byte *m_begin; }; unsigned int m_firstSize, m_blockSize, m_lastSize; bool m_firstInputDone; BlockQueue m_queue;};//! .class FilterWithInputQueue : public Filter{public: FilterWithInputQueue(BufferedTransformation *attachment) : Filter(attachment) {} unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking) { if (!blocking) throw BlockingInputOnly("FilterWithInputQueue"); m_inQueue.Put(inString, length); if (messageEnd) { IsolatedMessageEnd(blocking); Output(0, NULL, 0, messageEnd, blocking); } return 0; }protected: virtual bool IsolatedMessageEnd(bool blocking) =0; void IsolatedInitialize(const NameValuePairs ¶meters) {m_inQueue.Clear();} ByteQueue m_inQueue;};//! Filter Wrapper for StreamTransformationclass StreamTransformationFilter : public FilterWithBufferedInput, private FilterPutSpaceHelper{public: enum BlockPaddingScheme {NO_PADDING, ZEROS_PADDING, PKCS_PADDING, ONE_AND_ZEROS_PADDING, DEFAULT_PADDING}; /*! DEFAULT_PADDING means PKCS_PADDING if c.MandatoryBlockSize() > 1 && c.MinLastBlockSize() == 0 (e.g. ECB or CBC mode), otherwise NO_PADDING (OFB, CFB, CTR, CBC-CTS modes) */ StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment = NULL, BlockPaddingScheme padding = DEFAULT_PADDING); void FirstPut(const byte *inString); void NextPutMultiple(const byte *inString, unsigned int length); void NextPutModifiable(byte *inString, unsigned int length); void LastPut(const byte *inString, unsigned int length);// byte * CreatePutSpace(unsigned int &size);protected: static unsigned int LastBlockSize(StreamTransformation &c, BlockPaddingScheme padding); StreamTransformation &m_cipher; BlockPaddingScheme m_padding; unsigned int m_optimalBufferSize;};#ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITYtypedef StreamTransformationFilter StreamCipherFilter;#endif//! Filter Wrapper for HashTransformationclass HashFilter : public Bufferless<Filter>, private FilterPutSpaceHelper{public: HashFilter(HashTransformation &hm, BufferedTransformation *attachment = NULL, bool putMessage=false) : Bufferless<Filter>(attachment), m_hashModule(hm), m_putMessage(putMessage) {} void IsolatedInitialize(const NameValuePairs ¶meters); unsigned int Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking); byte * CreatePutSpace(unsigned int &size) {return m_hashModule.CreateUpdateSpace(size);}private: HashTransformation &m_hashModule; bool m_putMessage; byte *m_space;};//! Filter Wrapper for HashTransformationclass HashVerificationFilter : public FilterWithBufferedInput{public: class HashVerificationFailed : public Exception { public: HashVerificationFailed() : Exception(DATA_INTEGRITY_CHECK_FAILED, "HashVerifier: message hash not valid") {} }; 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}; HashVerificationFilter(HashTransformation &hm, BufferedTransformation *attachment = NULL, word32 flags = DEFAULT_FLAGS); bool GetLastResult() const {return m_verified;}protected: void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, unsigned int &firstSize, unsigned int &blockSize, unsigned int &lastSize); void FirstPut(const byte *inString); void NextPutMultiple(const byte *inString, unsigned int length); void LastPut(const byte *inString, unsigned int length);private: static inline unsigned int FirstSize(word32 flags, HashTransformation &hm) {return flags & HASH_AT_BEGIN ? hm.DigestSize() : 0;} static inline unsigned int LastSize(word32 flags, HashTransformation &hm) {return flags & HASH_AT_BEGIN ? 0 : hm.DigestSize();} HashTransformation &m_hashModule; word32 m_flags; SecByteBlock m_expectedHash; bool m_verified;};typedef HashVerificationFilter HashVerifier; // for backwards compatibility//! Filter Wrapper for PK_Signerclass SignerFilter : public Unflushable<Filter>{public: SignerFilter(RandomNumberGenerator &rng, const PK_Signer &signer, BufferedTransformation *attachment = NULL, bool putMessage=false) : Unflushable<Filter>(attachment), m_rng(rng), m_signer(signer), m_messageAccumulator(signer.NewSignatureAccumulator()), m_putMessage(putMessage) {} void IsolatedInitialize(const NameValuePairs ¶meters); unsigned int Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking);private: RandomNumberGenerator &m_rng; const PK_Signer &m_signer; member_ptr<PK_MessageAccumulator> m_messageAccumulator; bool m_putMessage; SecByteBlock m_buf;};//! Filter Wrapper for PK_Verifierclass SignatureVerificationFilter : public FilterWithBufferedInput{public: class SignatureVerificationFailed : public Exception { public: SignatureVerificationFailed() : Exception(DATA_INTEGRITY_CHECK_FAILED, "VerifierFilter: digital signature not valid") {} }; 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}; SignatureVerificationFilter(const PK_Verifier &verifier, BufferedTransformation *attachment = NULL, word32 flags = DEFAULT_FLAGS);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -