1#ifndef CRYPTOPP_FILTERS_H 2#define CRYPTOPP_FILTERS_H 3 4#include "simple.h" 5#include "secblock.h" 6#include "misc.h" 7#include "smartptr.h" 8#include "queue.h" 9#include "algparam.h" 10#include <deque> 11 12NAMESPACE_BEGIN(CryptoPP) 13 14/// provides an implementation of BufferedTransformation's attachment interface 15class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Filter : public BufferedTransformation, public NotCopyable 16{ 17public: 18 Filter(BufferedTransformation *attachment = NULL); 19 20 bool Attachable() {return true;} 21 BufferedTransformation *AttachedTransformation(); 22 const BufferedTransformation *AttachedTransformation() const; 23 void Detach(BufferedTransformation *newAttachment = NULL); 24 25 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true); 26 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const; 27 28 void Initialize(const NameValuePairs ¶meters=g_nullNameValuePairs, int propagation=-1); 29 bool Flush(bool hardFlush, int propagation=-1, bool blocking=true); 30 bool MessageSeriesEnd(int propagation=-1, bool blocking=true); 31 32protected: 33 virtual BufferedTransformation * NewDefaultAttachment() const; 34 void Insert(Filter *nextFilter); // insert filter after this one 35 36 virtual bool ShouldPropagateMessageEnd() const {return true;} 37 virtual bool ShouldPropagateMessageSeriesEnd() const {return true;} 38 39 void PropagateInitialize(const NameValuePairs ¶meters, int propagation); 40 41 size_t Output(int outputSite, const byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel=DEFAULT_CHANNEL); 42 size_t OutputModifiable(int outputSite, byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel=DEFAULT_CHANNEL); 43 bool OutputMessageEnd(int outputSite, int propagation, bool blocking, const std::string &channel=DEFAULT_CHANNEL); 44 bool OutputFlush(int outputSite, bool hardFlush, int propagation, bool blocking, const std::string &channel=DEFAULT_CHANNEL); 45 bool OutputMessageSeriesEnd(int outputSite, int propagation, bool blocking, const std::string &channel=DEFAULT_CHANNEL); 46 47private: 48 member_ptr<BufferedTransformation> m_attachment; 49 50protected: 51 size_t m_inputPosition; 52 int m_continueAt; 53}; 54 55struct CRYPTOPP_DLL FilterPutSpaceHelper 56{ 57 // desiredSize is how much to ask target, bufferSize is how much to allocate in m_tempSpace 58 byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize, size_t desiredSize, size_t &bufferSize) 59 { 60 assert(desiredSize >= minSize && bufferSize >= minSize); 61 if (m_tempSpace.size() < minSize) 62 { 63 byte *result = target.ChannelCreatePutSpace(channel, desiredSize); 64 if (desiredSize >= minSize) 65 { 66 bufferSize = desiredSize; 67 return result; 68 } 69 m_tempSpace.New(bufferSize); 70 } 71 72 bufferSize = m_tempSpace.size(); 73 return m_tempSpace.begin(); 74 } 75 byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize) 76 {return HelpCreatePutSpace(target, channel, minSize, minSize, minSize);} 77 byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize, size_t bufferSize) 78 {return HelpCreatePutSpace(target, channel, minSize, minSize, bufferSize);} 79 SecByteBlock m_tempSpace; 80}; 81 82//! measure how many byte and messages pass through, also serves as valve 83class CRYPTOPP_DLL MeterFilter : public Bufferless<Filter> 84{ 85public: 86 MeterFilter(BufferedTransformation *attachment=NULL, bool transparent=true) 87 : m_transparent(transparent) {Detach(attachment); ResetMeter();} 88 89 void SetTransparent(bool transparent) {m_transparent = transparent;} 90 void AddRangeToSkip(unsigned int message, lword position, lword size, bool sortNow = true); 91 void ResetMeter(); 92 void IsolatedInitialize(const NameValuePairs ¶meters) {ResetMeter();} 93 94 lword GetCurrentMessageBytes() const {return m_currentMessageBytes;} 95 lword GetTotalBytes() {return m_totalBytes;} 96 unsigned int GetCurrentSeriesMessages() {return m_currentSeriesMessages;} 97 unsigned int GetTotalMessages() {return m_totalMessages;} 98 unsigned int GetTotalMessageSeries() {return m_totalMessageSeries;} 99 100 byte * CreatePutSpace(size_t &size) 101 {return AttachedTransformation()->CreatePutSpace(size);} 102 size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking); 103 size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking); 104 bool IsolatedMessageSeriesEnd(bool blocking); 105 106private: 107 size_t PutMaybeModifiable(byte *inString, size_t length, int messageEnd, bool blocking, bool modifiable); 108 bool ShouldPropagateMessageEnd() const {return m_transparent;} 109 bool ShouldPropagateMessageSeriesEnd() const {return m_transparent;} 110 111 struct MessageRange 112 { 113 inline bool operator<(const MessageRange &b) const // BCB2006 workaround: this has to be a member function 114 {return message < b.message || (message == b.message && position < b.position);} 115 unsigned int message; lword position; lword size; 116 }; 117 118 bool m_transparent; 119 lword m_currentMessageBytes, m_totalBytes; 120 unsigned int m_currentSeriesMessages, m_totalMessages, m_totalMessageSeries; 121 std::deque<MessageRange> m_rangesToSkip; 122 byte *m_begin; 123 size_t m_length; 124}; 125 126//! _ 127class CRYPTOPP_DLL TransparentFilter : public MeterFilter 128{ 129public: 130 TransparentFilter(BufferedTransformation *attachment=NULL) : MeterFilter(attachment, true) {} 131}; 132 133//! _ 134class CRYPTOPP_DLL OpaqueFilter : public MeterFilter 135{ 136public: 137 OpaqueFilter(BufferedTransformation *attachment=NULL) : MeterFilter(attachment, false) {} 138}; 139 140/*! FilterWithBufferedInput divides up the input stream into 141 a first block, a number of middle blocks, and a last block. 142 First and last blocks are optional, and middle blocks may 143 be a stream instead (i.e. blockSize == 1). 144*/ 145class CRYPTOPP_DLL FilterWithBufferedInput : public Filter 146{ 147public: 148 FilterWithBufferedInput(BufferedTransformation *attachment); 149 //! firstSize and lastSize may be 0, blockSize must be at least 1 150 FilterWithBufferedInput(size_t firstSize, size_t blockSize, size_t lastSize, BufferedTransformation *attachment); 151 152 void IsolatedInitialize(const NameValuePairs ¶meters); 153 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking) 154 { 155 return PutMaybeModifiable(const_cast<byte *>(inString), length, messageEnd, blocking, false); 156 } 157 size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking) 158 { 159 return PutMaybeModifiable(inString, length, messageEnd, blocking, true); 160 } 161 /*! calls ForceNextPut() if hardFlush is true */ 162 bool IsolatedFlush(bool hardFlush, bool blocking); 163 164 /*! The input buffer may contain more than blockSize bytes if lastSize != 0. 165 ForceNextPut() forces a call to NextPut() if this is the case. 166 */ 167 void ForceNextPut(); 168 169protected: 170 bool DidFirstPut() {return m_firstInputDone;} 171 172 virtual void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize) 173 {InitializeDerived(parameters);} 174 virtual void InitializeDerived(const NameValuePairs ¶meters) {} 175 // FirstPut() is called if (firstSize != 0 and totalLength >= firstSize) 176 // or (firstSize == 0 and (totalLength > 0 or a MessageEnd() is received)) 177 virtual void FirstPut(const byte *inString) =0; 178 // NextPut() is called if totalLength >= firstSize+blockSize+lastSize 179 virtual void NextPutSingle(const byte *inString) {assert(false);} 180 // Same as NextPut() except length can be a multiple of blockSize 181 // Either NextPut() or NextPutMultiple() must be overriden 182 virtual void NextPutMultiple(const byte *inString, size_t length); 183 // Same as NextPutMultiple(), but inString can be modified 184 virtual void NextPutModifiable(byte *inString, size_t length) 185 {NextPutMultiple(inString, length);} 186 // LastPut() is always called 187 // if totalLength < firstSize then length == totalLength 188 // else if totalLength <= firstSize+lastSize then length == totalLength-firstSize 189 // else lastSize <= length < lastSize+blockSize 190 virtual void LastPut(const byte *inString, size_t length) =0; 191 virtual void FlushDerived() {} 192 193protected: 194 size_t PutMaybeModifiable(byte *begin, size_t length, int messageEnd, bool blocking, bool modifiable); 195 void NextPutMaybeModifiable(byte *inString, size_t length, bool modifiable) 196 { 197 if (modifiable) NextPutModifiable(inString, length); 198 else NextPutMultiple(inString, length); 199 } 200 201 // This function should no longer be used, put this here to cause a compiler error 202 // if someone tries to override NextPut(). 203 virtual int NextPut(const byte *inString, size_t length) {assert(false); return 0;} 204 205 class BlockQueue 206 { 207 public: 208 void ResetQueue(size_t blockSize, size_t maxBlocks); 209 byte *GetBlock(); 210 byte *GetContigousBlocks(size_t &numberOfBytes); 211 size_t GetAll(byte *outString); 212 void Put(const byte *inString, size_t length); 213 size_t CurrentSize() const {return m_size;} 214 size_t MaxSize() const {return m_buffer.size();} 215 216 private: 217 SecByteBlock m_buffer; 218 size_t m_blockSize, m_maxBlocks, m_size; 219 byte *m_begin; 220 }; 221 222 size_t m_firstSize, m_blockSize, m_lastSize; 223 bool m_firstInputDone; 224 BlockQueue m_queue; 225}; 226 227//! _ 228class CRYPTOPP_DLL FilterWithInputQueue : public Filter 229{ 230public: 231 FilterWithInputQueue(BufferedTransformation *attachment=NULL) : Filter(attachment) {} 232 233 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking) 234 { 235 if (!blocking) 236 throw BlockingInputOnly("FilterWithInputQueue"); 237 238 m_inQueue.Put(inString, length); 239 if (messageEnd) 240 { 241 IsolatedMessageEnd(blocking); 242 Output(0, NULL, 0, messageEnd, blocking); 243 } 244 return 0; 245 } 246 247protected: 248 virtual bool IsolatedMessageEnd(bool blocking) =0; 249 void IsolatedInitialize(const NameValuePairs ¶meters) {m_inQueue.Clear();} 250 251 ByteQueue m_inQueue; 252}; 253 254struct BlockPaddingSchemeDef 255{ 256 enum BlockPaddingScheme {NO_PADDING, ZEROS_PADDING, PKCS_PADDING, ONE_AND_ZEROS_PADDING, DEFAULT_PADDING}; 257}; 258 259//! Filter Wrapper for StreamTransformation, optionally handling padding/unpadding when needed 260class CRYPTOPP_DLL StreamTransformationFilter : public FilterWithBufferedInput, public BlockPaddingSchemeDef, private FilterPutSpaceHelper 261{ 262public: 263 /*! DEFAULT_PADDING means PKCS_PADDING if c.MandatoryBlockSize() > 1 && c.MinLastBlockSize() == 0 (e.g. ECB or CBC mode), 264 otherwise NO_PADDING (OFB, CFB, CTR, CBC-CTS modes). 265 See http://www.weidai.com/scan-mirror/csp.html for details of the padding schemes. */ 266 StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment = NULL, BlockPaddingScheme padding = DEFAULT_PADDING, bool allowAuthenticatedSymmetricCipher = false); 267 268 std::string AlgorithmName() const {return m_cipher.AlgorithmName();} 269 270protected: 271 void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize); 272 void FirstPut(const byte *inString); 273 void NextPutMultiple(const byte *inString, size_t length); 274 void NextPutModifiable(byte *inString, size_t length); 275 void LastPut(const byte *inString, size_t length); 276 277 static size_t LastBlockSize(StreamTransformation &c, BlockPaddingScheme padding); 278 279 StreamTransformation &m_cipher; 280 BlockPaddingScheme m_padding; 281 unsigned int m_optimalBufferSize; 282}; 283 284#ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY 285typedef StreamTransformationFilter StreamCipherFilter; 286#endif 287 288//! Filter Wrapper for HashTransformation 289class CRYPTOPP_DLL HashFilter : public Bufferless<Filter>, private FilterPutSpaceHelper 290{ 291public: 292 HashFilter(HashTransformation &hm, BufferedTransformation *attachment = NULL, bool putMessage=false, int truncatedDigestSize=-1, const std::string &messagePutChannel=DEFAULT_CHANNEL, const std::string &hashPutChannel=DEFAULT_CHANNEL); 293 294 std::string AlgorithmName() const {return m_hashModule.AlgorithmName();} 295 void IsolatedInitialize(const NameValuePairs ¶meters); 296 size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking); 297 byte * CreatePutSpace(size_t &size) {return m_hashModule.CreateUpdateSpace(size);} 298 299private: 300 HashTransformation &m_hashModule; 301 bool m_putMessage; 302 unsigned int m_digestSize; 303 byte *m_space; 304 std::string m_messagePutChannel, m_hashPutChannel; 305}; 306 307//! Filter Wrapper for HashTransformation 308class CRYPTOPP_DLL HashVerificationFilter : public FilterWithBufferedInput 309{ 310public: 311 class HashVerificationFailed : public Exception 312 { 313 public: 314 HashVerificationFailed() 315 : Exception(DATA_INTEGRITY_CHECK_FAILED, "HashVerificationFilter: message hash or MAC not valid") {} 316 }; 317 318 enum Flags {HASH_AT_END=0, HASH_AT_BEGIN=1, PUT_MESSAGE=2, PUT_HASH=4, PUT_RESULT=8, THROW_EXCEPTION=16, DEFAULT_FLAGS = HASH_AT_BEGIN | PUT_RESULT}; 319 HashVerificationFilter(HashTransformation &hm, BufferedTransformation *attachment = NULL, word32 flags = DEFAULT_FLAGS, int truncatedDigestSize=-1); 320 321 std::string AlgorithmName() const {return m_hashModule.AlgorithmName();} 322 bool GetLastResult() const {return m_verified;} 323 324protected: 325 void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize); 326 void FirstPut(const byte *inString); 327 void NextPutMultiple(const byte *inString, size_t length); 328 void LastPut(const byte *inString, size_t length); 329 330private: 331 friend class AuthenticatedDecryptionFilter; 332 333 HashTransformation &m_hashModule; 334 word32 m_flags; 335 unsigned int m_digestSize; 336 bool m_verified; 337 SecByteBlock m_expectedHash; 338}; 339 340typedef HashVerificationFilter HashVerifier; // for backwards compatibility 341 342//! Filter wrapper for encrypting with AuthenticatedSymmetricCipher, optionally handling padding/unpadding when needed 343/*! Additional authenticated data should be given in channel "AAD". If putAAD is true, AAD will be Put() to the attached BufferedTransformation in channel "AAD". */ 344class CRYPTOPP_DLL AuthenticatedEncryptionFilter : public StreamTransformationFilter 345{ 346public: 347 /*! See StreamTransformationFilter for documentation on BlockPaddingScheme */ 348 AuthenticatedEncryptionFilter(AuthenticatedSymmetricCipher &c, BufferedTransformation *attachment = NULL, bool putAAD=false, int truncatedDigestSize=-1, const std::string &macChannel=DEFAULT_CHANNEL, BlockPaddingScheme padding = DEFAULT_PADDING); 349 350 void IsolatedInitialize(const NameValuePairs ¶meters); 351 byte * ChannelCreatePutSpace(const std::string &channel, size_t &size); 352 size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking); 353 void LastPut(const byte *inString, size_t length); 354 355protected: 356 HashFilter m_hf; 357}; 358 359//! Filter wrapper for decrypting with AuthenticatedSymmetricCipher, optionally handling padding/unpadding when needed 360/*! Additional authenticated data should be given in channel "AAD". */ 361class CRYPTOPP_DLL AuthenticatedDecryptionFilter : public FilterWithBufferedInput, public BlockPaddingSchemeDef 362{ 363public: 364 enum Flags {MAC_AT_END=0, MAC_AT_BEGIN=1, THROW_EXCEPTION=16, DEFAULT_FLAGS = THROW_EXCEPTION}; 365 366 /*! See StreamTransformationFilter for documentation on BlockPaddingScheme */ 367 AuthenticatedDecryptionFilter(AuthenticatedSymmetricCipher &c, BufferedTransformation *attachment = NULL, word32 flags = DEFAULT_FLAGS, int truncatedDigestSize=-1, BlockPaddingScheme padding = DEFAULT_PADDING); 368 369 std::string AlgorithmName() const {return m_hashVerifier.AlgorithmName();} 370 byte * ChannelCreatePutSpace(const std::string &channel, size_t &size); 371 size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking); 372 bool GetLastResult() const {return m_hashVerifier.GetLastResult();} 373 374protected: 375 void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize); 376 void FirstPut(const byte *inString); 377 void NextPutMultiple(const byte *inString, size_t length); 378 void LastPut(const byte *inString, size_t length); 379 380 HashVerificationFilter m_hashVerifier; 381 StreamTransformationFilter m_streamFilter; 382}; 383 384//! Filter Wrapper for PK_Signer 385class CRYPTOPP_DLL SignerFilter : public Unflushable<Filter> 386{ 387public: 388 SignerFilter(RandomNumberGenerator &rng, const PK_Signer &signer, BufferedTransformation *attachment = NULL, bool putMessage=false) 389 : m_rng(rng), m_signer(signer), m_messageAccumulator(signer.NewSignatureAccumulator(rng)), m_putMessage(putMessage) {Detach(attachment);} 390 391 std::string AlgorithmName() const {return m_signer.AlgorithmName();} 392 393 void IsolatedInitialize(const NameValuePairs ¶meters); 394 size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking); 395 396private: 397 RandomNumberGenerator &m_rng; 398 const PK_Signer &m_signer; 399 member_ptr<PK_MessageAccumulator> m_messageAccumulator; 400 bool m_putMessage; 401 SecByteBlock m_buf; 402}; 403 404//! Filter Wrapper for PK_Verifier 405class CRYPTOPP_DLL SignatureVerificationFilter : public FilterWithBufferedInput 406{ 407public: 408 class SignatureVerificationFailed : public Exception 409 { 410 public: 411 SignatureVerificationFailed() 412 : Exception(DATA_INTEGRITY_CHECK_FAILED, "VerifierFilter: digital signature not valid") {} 413 }; 414 415 enum Flags {SIGNATURE_AT_END=0, SIGNATURE_AT_BEGIN=1, PUT_MESSAGE=2, PUT_SIGNATURE=4, PUT_RESULT=8, THROW_EXCEPTION=16, DEFAULT_FLAGS = SIGNATURE_AT_BEGIN | PUT_RESULT}; 416 SignatureVerificationFilter(const PK_Verifier &verifier, BufferedTransformation *attachment = NULL, word32 flags = DEFAULT_FLAGS); 417 418 std::string AlgorithmName() const {return m_verifier.AlgorithmName();} 419 420 bool GetLastResult() const {return m_verified;} 421 422protected: 423 void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize); 424 void FirstPut(const byte *inString); 425 void NextPutMultiple(const byte *inString, size_t length); 426 void LastPut(const byte *inString, size_t length); 427 428private: 429 const PK_Verifier &m_verifier; 430 member_ptr<PK_MessageAccumulator> m_messageAccumulator; 431 word32 m_flags; 432 SecByteBlock m_signature; 433 bool m_verified; 434}; 435 436typedef SignatureVerificationFilter VerifierFilter; // for backwards compatibility 437 438//! Redirect input to another BufferedTransformation without owning it 439class CRYPTOPP_DLL Redirector : public CustomSignalPropagation<Sink> 440{ 441public: 442 enum Behavior 443 { 444 DATA_ONLY = 0x00, 445 PASS_SIGNALS = 0x01, 446 PASS_WAIT_OBJECTS = 0x02, 447 PASS_EVERYTHING = PASS_SIGNALS | PASS_WAIT_OBJECTS 448 }; 449 450 Redirector() : m_target(NULL), m_behavior(PASS_EVERYTHING) {} 451 Redirector(BufferedTransformation &target, Behavior behavior=PASS_EVERYTHING) 452 : m_target(&target), m_behavior(behavior) {} 453 454 void Redirect(BufferedTransformation &target) {m_target = ⌖} 455 void StopRedirection() {m_target = NULL;} 456 457 Behavior GetBehavior() {return (Behavior) m_behavior;} 458 void SetBehavior(Behavior behavior) {m_behavior=behavior;} 459 bool GetPassSignals() const {return (m_behavior & PASS_SIGNALS) != 0;} 460 void SetPassSignals(bool pass) { if (pass) m_behavior |= PASS_SIGNALS; else m_behavior &= ~(word32) PASS_SIGNALS; } 461 bool GetPassWaitObjects() const {return (m_behavior & PASS_WAIT_OBJECTS) != 0;} 462 void SetPassWaitObjects(bool pass) { if (pass) m_behavior |= PASS_WAIT_OBJECTS; else m_behavior &= ~(word32) PASS_WAIT_OBJECTS; } 463 464 bool CanModifyInput() const 465 {return m_target ? m_target->CanModifyInput() : false;} 466 467 void Initialize(const NameValuePairs ¶meters, int propagation); 468 byte * CreatePutSpace(size_t &size) 469 {return m_target ? m_target->CreatePutSpace(size) : (byte *)(size=0, NULL);} 470 size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking) 471 {return m_target ? m_target->Put2(begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;} 472 bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) 473 {return m_target && GetPassSignals() ? m_target->Flush(hardFlush, propagation, blocking) : false;} 474 bool MessageSeriesEnd(int propagation=-1, bool blocking=true) 475 {return m_target && GetPassSignals() ? m_target->MessageSeriesEnd(propagation, blocking) : false;} 476 477 byte * ChannelCreatePutSpace(const std::string &channel, size_t &size) 478 {return m_target ? m_target->ChannelCreatePutSpace(channel, size) : (byte *)(size=0, NULL);} 479 size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking) 480 {return m_target ? m_target->ChannelPut2(channel, begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;} 481 size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking) 482 {return m_target ? m_target->ChannelPutModifiable2(channel, begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;} 483 bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true) 484 {return m_target && GetPassSignals() ? m_target->ChannelFlush(channel, completeFlush, propagation, blocking) : false;} 485 bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true) 486 {return m_target && GetPassSignals() ? m_target->ChannelMessageSeriesEnd(channel, propagation, blocking) : false;} 487 488 unsigned int GetMaxWaitObjectCount() const 489 { return m_target && GetPassWaitObjects() ? m_target->GetMaxWaitObjectCount() : 0; } 490 void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack) 491 { if (m_target && GetPassWaitObjects()) m_target->GetWaitObjects(container, callStack); } 492 493private: 494 BufferedTransformation *m_target; 495 word32 m_behavior; 496}; 497 498// Used By ProxyFilter 499class CRYPTOPP_DLL OutputProxy : public CustomSignalPropagation<Sink> 500{ 501public: 502 OutputProxy(BufferedTransformation &owner, bool passSignal) : m_owner(owner), m_passSignal(passSignal) {} 503 504 bool GetPassSignal() const {return m_passSignal;} 505 void SetPassSignal(bool passSignal) {m_passSignal = passSignal;} 506 507 byte * CreatePutSpace(size_t &size) 508 {return m_owner.AttachedTransformation()->CreatePutSpace(size);} 509 size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking) 510 {return m_owner.AttachedTransformation()->Put2(begin, length, m_passSignal ? messageEnd : 0, blocking);} 511 size_t PutModifiable2(byte *begin, size_t length, int messageEnd, bool blocking) 512 {return m_owner.AttachedTransformation()->PutModifiable2(begin, length, m_passSignal ? messageEnd : 0, blocking);} 513 void Initialize(const NameValuePairs ¶meters=g_nullNameValuePairs, int propagation=-1) 514 {if (m_passSignal) m_owner.AttachedTransformation()->Initialize(parameters, propagation);} 515 bool Flush(bool hardFlush, int propagation=-1, bool blocking=true) 516 {return m_passSignal ? m_owner.AttachedTransformation()->Flush(hardFlush, propagation, blocking) : false;} 517 bool MessageSeriesEnd(int propagation=-1, bool blocking=true) 518 {return m_passSignal ? m_owner.AttachedTransformation()->MessageSeriesEnd(propagation, blocking) : false;} 519 520 byte * ChannelCreatePutSpace(const std::string &channel, size_t &size) 521 {return m_owner.AttachedTransformation()->ChannelCreatePutSpace(channel, size);} 522 size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking) 523 {return m_owner.AttachedTransformation()->ChannelPut2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking);} 524 size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking) 525 {return m_owner.AttachedTransformation()->ChannelPutModifiable2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking);} 526 bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true) 527 {return m_passSignal ? m_owner.AttachedTransformation()->ChannelFlush(channel, completeFlush, propagation, blocking) : false;} 528 bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true) 529 {return m_passSignal ? m_owner.AttachedTransformation()->ChannelMessageSeriesEnd(channel, propagation, blocking) : false;} 530 531private: 532 BufferedTransformation &m_owner; 533 bool m_passSignal; 534}; 535 536//! Base class for Filter classes that are proxies for a chain of other filters. 537class CRYPTOPP_DLL ProxyFilter : public FilterWithBufferedInput 538{ 539public: 540 ProxyFilter(BufferedTransformation *filter, size_t firstSize, size_t lastSize, BufferedTransformation *attachment); 541 542 bool IsolatedFlush(bool hardFlush, bool blocking); 543 544 void SetFilter(Filter *filter); 545 void NextPutMultiple(const byte *s, size_t len); 546 void NextPutModifiable(byte *inString, size_t length); 547 548protected: 549 member_ptr<BufferedTransformation> m_filter; 550}; 551 552//! simple proxy filter that doesn't modify the underlying filter's input or output 553class CRYPTOPP_DLL SimpleProxyFilter : public ProxyFilter 554{ 555public: 556 SimpleProxyFilter(BufferedTransformation *filter, BufferedTransformation *attachment) 557 : ProxyFilter(filter, 0, 0, attachment) {} 558 559 void FirstPut(const byte *) {} 560 void LastPut(const byte *, size_t) {m_filter->MessageEnd();} 561}; 562 563//! proxy for the filter created by PK_Encryptor::CreateEncryptionFilter 564/*! This class is here just to provide symmetry with VerifierFilter. */ 565class CRYPTOPP_DLL PK_EncryptorFilter : public SimpleProxyFilter 566{ 567public: 568 PK_EncryptorFilter(RandomNumberGenerator &rng, const PK_Encryptor &encryptor, BufferedTransformation *attachment = NULL) 569 : SimpleProxyFilter(encryptor.CreateEncryptionFilter(rng), attachment) {} 570}; 571 572//! proxy for the filter created by PK_Decryptor::CreateDecryptionFilter 573/*! This class is here just to provide symmetry with SignerFilter. */ 574class CRYPTOPP_DLL PK_DecryptorFilter : public SimpleProxyFilter 575{ 576public: 577 PK_DecryptorFilter(RandomNumberGenerator &rng, const PK_Decryptor &decryptor, BufferedTransformation *attachment = NULL) 578 : SimpleProxyFilter(decryptor.CreateDecryptionFilter(rng), attachment) {} 579}; 580 581//! Append input to a string object 582template <class T> 583class StringSinkTemplate : public Bufferless<Sink> 584{ 585public: 586 // VC60 workaround: no T::char_type 587 typedef typename T::traits_type::char_type char_type; 588 589 StringSinkTemplate(T &output) 590 : m_output(&output) {assert(sizeof(output[0])==1);} 591 592 void IsolatedInitialize(const NameValuePairs ¶meters) 593 {if (!parameters.GetValue("OutputStringPointer", m_output)) throw InvalidArgument("StringSink: OutputStringPointer not specified");} 594 595 size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking) 596 { 597 if (length > 0) 598 { 599 typename T::size_type size = m_output->size(); 600 if (length < size && size + length > m_output->capacity()) 601 m_output->reserve(2*size); 602 m_output->append((const char_type *)begin, (const char_type *)begin+length); 603 } 604 return 0; 605 } 606 607private: 608 T *m_output; 609}; 610 611//! Append input to an std::string 612CRYPTOPP_DLL_TEMPLATE_CLASS StringSinkTemplate<std::string>; 613typedef StringSinkTemplate<std::string> StringSink; 614 615//! incorporates input into RNG as additional entropy 616class RandomNumberSink : public Bufferless<Sink> 617{ 618public: 619 RandomNumberSink() 620 : m_rng(NULL) {} 621 622 RandomNumberSink(RandomNumberGenerator &rng) 623 : m_rng(&rng) {} 624 625 void IsolatedInitialize(const NameValuePairs ¶meters); 626 size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking); 627 628private: 629 RandomNumberGenerator *m_rng; 630}; 631 632//! Copy input to a memory buffer 633class CRYPTOPP_DLL ArraySink : public Bufferless<Sink> 634{ 635public: 636 ArraySink(const NameValuePairs ¶meters = g_nullNameValuePairs) {IsolatedInitialize(parameters);} 637 ArraySink(byte *buf, size_t size) : m_buf(buf), m_size(size), m_total(0) {} 638 639 size_t AvailableSize() {return SaturatingSubtract(m_size, m_total);} 640 lword TotalPutLength() {return m_total;} 641 642 void IsolatedInitialize(const NameValuePairs ¶meters); 643 byte * CreatePutSpace(size_t &size); 644 size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking); 645 646protected: 647 byte *m_buf; 648 size_t m_size; 649 lword m_total; 650}; 651 652//! Xor input to a memory buffer 653class CRYPTOPP_DLL ArrayXorSink : public ArraySink 654{ 655public: 656 ArrayXorSink(byte *buf, size_t size) 657 : ArraySink(buf, size) {} 658 659 size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking); 660 byte * CreatePutSpace(size_t &size) {return BufferedTransformation::CreatePutSpace(size);} 661}; 662 663//! string-based implementation of Store interface 664class StringStore : public Store 665{ 666public: 667 StringStore(const char *string = NULL) 668 {StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string)));} 669 StringStore(const byte *string, size_t length) 670 {StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string, length)));} 671 template <class T> StringStore(const T &string) 672 {StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string)));} 673 674 CRYPTOPP_DLL size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true); 675 CRYPTOPP_DLL size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const; 676 677private: 678 CRYPTOPP_DLL void StoreInitialize(const NameValuePairs ¶meters); 679 680 const byte *m_store; 681 size_t m_length, m_count; 682}; 683 684//! RNG-based implementation of Source interface 685class CRYPTOPP_DLL RandomNumberStore : public Store 686{ 687public: 688 RandomNumberStore() 689 : m_rng(NULL), m_length(0), m_count(0) {} 690 691 RandomNumberStore(RandomNumberGenerator &rng, lword length) 692 : m_rng(&rng), m_length(length), m_count(0) {} 693 694 bool AnyRetrievable() const {return MaxRetrievable() != 0;} 695 lword MaxRetrievable() const {return m_length-m_count;} 696 697 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true); 698 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const 699 { 700 throw NotImplemented("RandomNumberStore: CopyRangeTo2() is not supported by this store"); 701 } 702 703private: 704 void StoreInitialize(const NameValuePairs ¶meters); 705 706 RandomNumberGenerator *m_rng; 707 lword m_length, m_count; 708}; 709 710//! empty store 711class CRYPTOPP_DLL NullStore : public Store 712{ 713public: 714 NullStore(lword size = ULONG_MAX) : m_size(size) {} 715 void StoreInitialize(const NameValuePairs ¶meters) {} 716 lword MaxRetrievable() const {return m_size;} 717 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true); 718 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const; 719 720private: 721 lword m_size; 722}; 723 724//! A Filter that pumps data into its attachment as input 725class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Source : public InputRejecting<Filter> 726{ 727public: 728 Source(BufferedTransformation *attachment = NULL) 729 {Source::Detach(attachment);} 730 731 lword Pump(lword pumpMax=size_t(0)-1) 732 {Pump2(pumpMax); return pumpMax;} 733 unsigned int PumpMessages(unsigned int count=UINT_MAX) 734 {PumpMessages2(count); return count;} 735 void PumpAll() 736 {PumpAll2();} 737 virtual size_t Pump2(lword &byteCount, bool blocking=true) =0; 738 virtual size_t PumpMessages2(unsigned int &messageCount, bool blocking=true) =0; 739 virtual size_t PumpAll2(bool blocking=true); 740 virtual bool SourceExhausted() const =0; 741 742protected: 743 void SourceInitialize(bool pumpAll, const NameValuePairs ¶meters) 744 { 745 IsolatedInitialize(parameters); 746 if (pumpAll) 747 PumpAll(); 748 } 749}; 750 751//! Turn a Store into a Source 752template <class T> 753class SourceTemplate : public Source 754{ 755public: 756 SourceTemplate<T>(BufferedTransformation *attachment) 757 : Source(attachment) {} 758 void IsolatedInitialize(const NameValuePairs ¶meters) 759 {m_store.IsolatedInitialize(parameters);} 760 size_t Pump2(lword &byteCount, bool blocking=true) 761 {return m_store.TransferTo2(*AttachedTransformation(), byteCount, DEFAULT_CHANNEL, blocking);} 762 size_t PumpMessages2(unsigned int &messageCount, bool blocking=true) 763 {return m_store.TransferMessagesTo2(*AttachedTransformation(), messageCount, DEFAULT_CHANNEL, blocking);} 764 size_t PumpAll2(bool blocking=true) 765 {return m_store.TransferAllTo2(*AttachedTransformation(), DEFAULT_CHANNEL, blocking);} 766 bool SourceExhausted() const 767 {return !m_store.AnyRetrievable() && !m_store.AnyMessages();} 768 void SetAutoSignalPropagation(int propagation) 769 {m_store.SetAutoSignalPropagation(propagation);} 770 int GetAutoSignalPropagation() const 771 {return m_store.GetAutoSignalPropagation();} 772 773protected: 774 T m_store; 775}; 776 777//! string-based implementation of Source interface 778class CRYPTOPP_DLL StringSource : public SourceTemplate<StringStore> 779{ 780public: 781 StringSource(BufferedTransformation *attachment = NULL) 782 : SourceTemplate<StringStore>(attachment) {} 783 //! zero terminated string as source 784 StringSource(const char *string, bool pumpAll, BufferedTransformation *attachment = NULL) 785 : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));} 786 //! binary byte array as source 787 StringSource(const byte *string, size_t length, bool pumpAll, BufferedTransformation *attachment = NULL) 788 : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string, length)));} 789 //! std::string as source 790 StringSource(const std::string &string, bool pumpAll, BufferedTransformation *attachment = NULL) 791 : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));} 792}; 793 794//! use the third constructor for an array source 795typedef StringSource ArraySource; 796 797//! RNG-based implementation of Source interface 798class CRYPTOPP_DLL RandomNumberSource : public SourceTemplate<RandomNumberStore> 799{ 800public: 801 RandomNumberSource(RandomNumberGenerator &rng, int length, bool pumpAll, BufferedTransformation *attachment = NULL) 802 : SourceTemplate<RandomNumberStore>(attachment) 803 {SourceInitialize(pumpAll, MakeParameters("RandomNumberGeneratorPointer", &rng)("RandomNumberStoreSize", length));} 804}; 805 806NAMESPACE_END 807 808#endif 809