1 #ifndef CRYPTOPP_FILTERS_H
2 #define CRYPTOPP_FILTERS_H
14 NAMESPACE_BEGIN(CryptoPP)
20 Filter(BufferedTransformation *attachment = NULL);
23 BufferedTransformation *AttachedTransformation();
24 const BufferedTransformation *AttachedTransformation()
const;
25 void Detach(BufferedTransformation *newAttachment = NULL);
27 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes,
const std::string &channel=DEFAULT_CHANNEL,
bool blocking=
true);
28 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX,
const std::string &channel=DEFAULT_CHANNEL,
bool blocking=
true)
const;
30 void Initialize(
const NameValuePairs ¶meters=g_nullNameValuePairs,
int propagation=-1);
31 bool Flush(
bool hardFlush,
int propagation=-1,
bool blocking=
true);
32 bool MessageSeriesEnd(
int propagation=-1,
bool blocking=
true);
35 virtual BufferedTransformation * NewDefaultAttachment()
const;
36 void Insert(
Filter *nextFilter);
38 virtual bool ShouldPropagateMessageEnd()
const {
return true;}
39 virtual bool ShouldPropagateMessageSeriesEnd()
const {
return true;}
41 void PropagateInitialize(
const NameValuePairs ¶meters,
int propagation);
43 size_t Output(
int outputSite,
const byte *inString,
size_t length,
int messageEnd,
bool blocking,
const std::string &channel=DEFAULT_CHANNEL);
44 size_t OutputModifiable(
int outputSite, byte *inString,
size_t length,
int messageEnd,
bool blocking,
const std::string &channel=DEFAULT_CHANNEL);
45 bool OutputMessageEnd(
int outputSite,
int propagation,
bool blocking,
const std::string &channel=DEFAULT_CHANNEL);
46 bool OutputFlush(
int outputSite,
bool hardFlush,
int propagation,
bool blocking,
const std::string &channel=DEFAULT_CHANNEL);
47 bool OutputMessageSeriesEnd(
int outputSite,
int propagation,
bool blocking,
const std::string &channel=DEFAULT_CHANNEL);
53 size_t m_inputPosition;
60 byte *HelpCreatePutSpace(
BufferedTransformation &target,
const std::string &channel,
size_t minSize,
size_t desiredSize,
size_t &bufferSize)
62 assert(desiredSize >= minSize && bufferSize >= minSize);
63 if (m_tempSpace.size() < minSize)
65 byte *result = target.ChannelCreatePutSpace(channel, desiredSize);
66 if (desiredSize >= minSize)
68 bufferSize = desiredSize;
71 m_tempSpace.New(bufferSize);
74 bufferSize = m_tempSpace.size();
75 return m_tempSpace.begin();
78 {
return HelpCreatePutSpace(target, channel, minSize, minSize, minSize);}
79 byte *HelpCreatePutSpace(
BufferedTransformation &target,
const std::string &channel,
size_t minSize,
size_t bufferSize)
80 {
return HelpCreatePutSpace(target, channel, minSize, minSize, bufferSize);}
89 : m_transparent(transparent) {Detach(attachment); ResetMeter();}
91 void SetTransparent(
bool transparent) {m_transparent = transparent;}
92 void AddRangeToSkip(
unsigned int message, lword position, lword size,
bool sortNow =
true);
94 void IsolatedInitialize(
const NameValuePairs ¶meters) {ResetMeter();}
96 lword GetCurrentMessageBytes()
const {
return m_currentMessageBytes;}
97 lword GetTotalBytes() {
return m_totalBytes;}
98 unsigned int GetCurrentSeriesMessages() {
return m_currentSeriesMessages;}
99 unsigned int GetTotalMessages() {
return m_totalMessages;}
100 unsigned int GetTotalMessageSeries() {
return m_totalMessageSeries;}
103 {
return AttachedTransformation()->CreatePutSpace(size);}
104 size_t Put2(
const byte *begin,
size_t length,
int messageEnd,
bool blocking);
105 size_t PutModifiable2(byte *inString,
size_t length,
int messageEnd,
bool blocking);
106 bool IsolatedMessageSeriesEnd(
bool blocking);
109 size_t PutMaybeModifiable(byte *inString,
size_t length,
int messageEnd,
bool blocking,
bool modifiable);
110 bool ShouldPropagateMessageEnd()
const {
return m_transparent;}
111 bool ShouldPropagateMessageSeriesEnd()
const {
return m_transparent;}
115 inline bool operator<(
const MessageRange &b)
const
116 {
return message < b.message || (message == b.message && position < b.position);}
117 unsigned int message; lword position; lword size;
121 lword m_currentMessageBytes, m_totalBytes;
122 unsigned int m_currentSeriesMessages, m_totalMessages, m_totalMessageSeries;
123 std::deque<MessageRange> m_rangesToSkip;
155 size_t Put2(
const byte *inString,
size_t length,
int messageEnd,
bool blocking)
157 return PutMaybeModifiable(const_cast<byte *>(inString), length, messageEnd, blocking,
false);
159 size_t PutModifiable2(byte *inString,
size_t length,
int messageEnd,
bool blocking)
161 return PutMaybeModifiable(inString, length, messageEnd, blocking,
true);
164 bool IsolatedFlush(
bool hardFlush,
bool blocking);
172 bool DidFirstPut() {
return m_firstInputDone;}
174 virtual void InitializeDerivedAndReturnNewSizes(
const NameValuePairs ¶meters,
size_t &firstSize,
size_t &blockSize,
size_t &lastSize)
175 {InitializeDerived(parameters);}
176 virtual void InitializeDerived(
const NameValuePairs ¶meters) {}
179 virtual void FirstPut(
const byte *inString) =0;
181 virtual void NextPutSingle(
const byte *inString) {assert(
false);}
184 virtual void NextPutMultiple(
const byte *inString,
size_t length);
186 virtual void NextPutModifiable(byte *inString,
size_t length)
187 {NextPutMultiple(inString, length);}
192 virtual void LastPut(
const byte *inString,
size_t length) =0;
193 virtual void FlushDerived() {}
196 size_t PutMaybeModifiable(byte *begin,
size_t length,
int messageEnd,
bool blocking,
bool modifiable);
197 void NextPutMaybeModifiable(byte *inString,
size_t length,
bool modifiable)
199 if (modifiable) NextPutModifiable(inString, length);
200 else NextPutMultiple(inString, length);
205 virtual int NextPut(
const byte *inString,
size_t length) {assert(
false);
return 0;}
210 void ResetQueue(
size_t blockSize,
size_t maxBlocks);
212 byte *GetContigousBlocks(
size_t &numberOfBytes);
213 size_t GetAll(byte *outString);
214 void Put(
const byte *inString,
size_t length);
215 size_t CurrentSize()
const {
return m_size;}
216 size_t MaxSize()
const {
return m_buffer.size();}
220 size_t m_blockSize, m_maxBlocks, m_size;
224 size_t m_firstSize, m_blockSize, m_lastSize;
225 bool m_firstInputDone;
235 size_t Put2(
const byte *inString,
size_t length,
int messageEnd,
bool blocking)
240 m_inQueue.Put(inString, length);
243 IsolatedMessageEnd(blocking);
244 Output(0, NULL, 0, messageEnd, blocking);
250 virtual bool IsolatedMessageEnd(
bool blocking) =0;
251 void IsolatedInitialize(
const NameValuePairs ¶meters) {m_inQueue.Clear();}
258 enum BlockPaddingScheme {NO_PADDING, ZEROS_PADDING, PKCS_PADDING, ONE_AND_ZEROS_PADDING, DEFAULT_PADDING};
273 void InitializeDerivedAndReturnNewSizes(
const NameValuePairs ¶meters,
size_t &firstSize,
size_t &blockSize,
size_t &lastSize);
274 void FirstPut(
const byte *inString);
275 void NextPutMultiple(
const byte *inString,
size_t length);
276 void NextPutModifiable(byte *inString,
size_t length);
277 void LastPut(
const byte *inString,
size_t length);
282 BlockPaddingScheme m_padding;
283 unsigned int m_optimalBufferSize;
286 #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
298 size_t Put2(
const byte *begin,
size_t length,
int messageEnd,
bool blocking);
299 byte *
CreatePutSpace(
size_t &size) {
return m_hashModule.CreateUpdateSpace(size);}
304 unsigned int m_digestSize;
306 std::string m_messagePutChannel, m_hashPutChannel;
317 :
Exception(DATA_INTEGRITY_CHECK_FAILED,
"HashVerificationFilter: message hash or MAC not valid") {}
320 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};
324 bool GetLastResult()
const {
return m_verified;}
327 void InitializeDerivedAndReturnNewSizes(
const NameValuePairs ¶meters,
size_t &firstSize,
size_t &blockSize,
size_t &lastSize);
328 void FirstPut(
const byte *inString);
329 void NextPutMultiple(
const byte *inString,
size_t length);
330 void LastPut(
const byte *inString,
size_t length);
337 unsigned int m_digestSize;
353 byte * ChannelCreatePutSpace(
const std::string &channel,
size_t &size);
354 size_t ChannelPut2(
const std::string &channel,
const byte *begin,
size_t length,
int messageEnd,
bool blocking);
355 void LastPut(
const byte *inString,
size_t length);
366 enum Flags {MAC_AT_END=0, MAC_AT_BEGIN=1, THROW_EXCEPTION=16, DEFAULT_FLAGS = THROW_EXCEPTION};
372 byte * ChannelCreatePutSpace(
const std::string &channel,
size_t &size);
373 size_t ChannelPut2(
const std::string &channel,
const byte *begin,
size_t length,
int messageEnd,
bool blocking);
374 bool GetLastResult()
const {
return m_hashVerifier.GetLastResult();}
377 void InitializeDerivedAndReturnNewSizes(
const NameValuePairs ¶meters,
size_t &firstSize,
size_t &blockSize,
size_t &lastSize);
378 void FirstPut(
const byte *inString);
379 void NextPutMultiple(
const byte *inString,
size_t length);
380 void LastPut(
const byte *inString,
size_t length);
391 : m_rng(rng), m_signer(signer), m_messageAccumulator(signer.
NewSignatureAccumulator(rng)), m_putMessage(putMessage) {Detach(attachment);}
396 size_t Put2(
const byte *begin,
size_t length,
int messageEnd,
bool blocking);
414 :
Exception(DATA_INTEGRITY_CHECK_FAILED,
"VerifierFilter: digital signature not valid") {}
417 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};
422 bool GetLastResult()
const {
return m_verified;}
425 void InitializeDerivedAndReturnNewSizes(
const NameValuePairs ¶meters,
size_t &firstSize,
size_t &blockSize,
size_t &lastSize);
426 void FirstPut(
const byte *inString);
427 void NextPutMultiple(
const byte *inString,
size_t length);
428 void LastPut(
const byte *inString,
size_t length);
448 PASS_WAIT_OBJECTS = 0x02,
449 PASS_EVERYTHING = PASS_SIGNALS | PASS_WAIT_OBJECTS
452 Redirector() : m_target(NULL), m_behavior(PASS_EVERYTHING) {}
454 : m_target(&target), m_behavior(behavior) {}
457 void StopRedirection() {m_target = NULL;}
459 Behavior GetBehavior() {
return (Behavior) m_behavior;}
460 void SetBehavior(Behavior behavior) {m_behavior=behavior;}
461 bool GetPassSignals()
const {
return (m_behavior & PASS_SIGNALS) != 0;}
462 void SetPassSignals(
bool pass) {
if (pass) m_behavior |= PASS_SIGNALS;
else m_behavior &= ~(word32) PASS_SIGNALS; }
463 bool GetPassWaitObjects()
const {
return (m_behavior & PASS_WAIT_OBJECTS) != 0;}
464 void SetPassWaitObjects(
bool pass) {
if (pass) m_behavior |= PASS_WAIT_OBJECTS;
else m_behavior &= ~(word32) PASS_WAIT_OBJECTS; }
466 bool CanModifyInput()
const
467 {
return m_target ? m_target->CanModifyInput() :
false;}
469 void Initialize(
const NameValuePairs ¶meters,
int propagation);
471 {
return m_target ? m_target->CreatePutSpace(size) : (byte *)(size=0, NULL);}
472 size_t Put2(
const byte *begin,
size_t length,
int messageEnd,
bool blocking)
473 {
return m_target ? m_target->Put2(begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
474 bool Flush(
bool hardFlush,
int propagation=-1,
bool blocking=
true)
475 {
return m_target && GetPassSignals() ? m_target->Flush(hardFlush, propagation, blocking) :
false;}
477 {
return m_target && GetPassSignals() ? m_target->MessageSeriesEnd(propagation, blocking) :
false;}
479 byte * ChannelCreatePutSpace(
const std::string &channel,
size_t &size)
480 {
return m_target ? m_target->ChannelCreatePutSpace(channel, size) : (byte *)(size=0, NULL);}
481 size_t ChannelPut2(
const std::string &channel,
const byte *begin,
size_t length,
int messageEnd,
bool blocking)
482 {
return m_target ? m_target->ChannelPut2(channel, begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
483 size_t ChannelPutModifiable2(
const std::string &channel, byte *begin,
size_t length,
int messageEnd,
bool blocking)
484 {
return m_target ? m_target->ChannelPutModifiable2(channel, begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
485 bool ChannelFlush(
const std::string &channel,
bool completeFlush,
int propagation=-1,
bool blocking=
true)
486 {
return m_target && GetPassSignals() ? m_target->ChannelFlush(channel, completeFlush, propagation, blocking) :
false;}
487 bool ChannelMessageSeriesEnd(
const std::string &channel,
int propagation=-1,
bool blocking=
true)
488 {
return m_target && GetPassSignals() ? m_target->ChannelMessageSeriesEnd(channel, propagation, blocking) :
false;}
491 {
return m_target && GetPassWaitObjects() ? m_target->GetMaxWaitObjectCount() : 0; }
493 {
if (m_target && GetPassWaitObjects()) m_target->GetWaitObjects(container, callStack); }
506 bool GetPassSignal()
const {
return m_passSignal;}
507 void SetPassSignal(
bool passSignal) {m_passSignal = passSignal;}
510 {
return m_owner.AttachedTransformation()->CreatePutSpace(size);}
511 size_t Put2(
const byte *begin,
size_t length,
int messageEnd,
bool blocking)
512 {
return m_owner.AttachedTransformation()->Put2(begin, length, m_passSignal ? messageEnd : 0, blocking);}
514 {
return m_owner.AttachedTransformation()->PutModifiable2(begin, length, m_passSignal ? messageEnd : 0, blocking);}
516 {
if (m_passSignal) m_owner.AttachedTransformation()->Initialize(parameters, propagation);}
517 bool Flush(
bool hardFlush,
int propagation=-1,
bool blocking=
true)
518 {
return m_passSignal ? m_owner.AttachedTransformation()->Flush(hardFlush, propagation, blocking) :
false;}
520 {
return m_passSignal ? m_owner.AttachedTransformation()->MessageSeriesEnd(propagation, blocking) :
false;}
522 byte * ChannelCreatePutSpace(
const std::string &channel,
size_t &size)
523 {
return m_owner.AttachedTransformation()->ChannelCreatePutSpace(channel, size);}
524 size_t ChannelPut2(
const std::string &channel,
const byte *begin,
size_t length,
int messageEnd,
bool blocking)
525 {
return m_owner.AttachedTransformation()->ChannelPut2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking);}
526 size_t ChannelPutModifiable2(
const std::string &channel, byte *begin,
size_t length,
int messageEnd,
bool blocking)
527 {
return m_owner.AttachedTransformation()->ChannelPutModifiable2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking);}
528 bool ChannelFlush(
const std::string &channel,
bool completeFlush,
int propagation=-1,
bool blocking=
true)
529 {
return m_passSignal ? m_owner.AttachedTransformation()->ChannelFlush(channel, completeFlush, propagation, blocking) :
false;}
530 bool ChannelMessageSeriesEnd(
const std::string &channel,
int propagation=-1,
bool blocking=
true)
531 {
return m_passSignal ? m_owner.AttachedTransformation()->ChannelMessageSeriesEnd(channel, propagation, blocking) :
false;}
546 void SetFilter(
Filter *filter);
547 void NextPutMultiple(
const byte *s,
size_t len);
548 void NextPutModifiable(byte *inString,
size_t length);
561 void FirstPut(
const byte *) {}
562 void LastPut(
const byte *,
size_t) {m_filter->MessageEnd();}
589 typedef typename T::traits_type::char_type char_type;
592 : m_output(&output) {assert(
sizeof(output[0])==1);}
595 {
if (!parameters.
GetValue(
"OutputStringPointer", m_output))
throw InvalidArgument(
"StringSink: OutputStringPointer not specified");}
597 size_t Put2(
const byte *begin,
size_t length,
int messageEnd,
bool blocking)
601 typename T::size_type size = m_output->size();
602 if (length < size && size + length > m_output->capacity())
603 m_output->reserve(2*size);
604 m_output->append((
const char_type *)begin, (
const char_type *)begin+length);
615 typedef StringSinkTemplate<std::string>
StringSink;
628 size_t Put2(
const byte *begin,
size_t length,
int messageEnd,
bool blocking);
639 ArraySink(byte *buf,
size_t size) : m_buf(buf), m_size(size), m_total(0) {}
641 size_t AvailableSize() {
return SaturatingSubtract(m_size, m_total);}
642 lword TotalPutLength() {
return m_total;}
645 byte * CreatePutSpace(
size_t &size);
646 size_t Put2(
const byte *begin,
size_t length,
int messageEnd,
bool blocking);
661 size_t Put2(
const byte *begin,
size_t length,
int messageEnd,
bool blocking);
680 CRYPTOPP_DLL
void StoreInitialize(
const NameValuePairs ¶meters);
683 size_t m_length, m_count;
691 : m_rng(NULL), m_length(0), m_count(0) {}
694 : m_rng(&rng), m_length(length), m_count(0) {}
702 throw NotImplemented(
"RandomNumberStore: CopyRangeTo2() is not supported by this store");
709 lword m_length, m_count;
716 NullStore(lword size = ULONG_MAX) : m_size(size) {}
733 lword Pump(lword pumpMax=
size_t(0)-1)
734 {Pump2(pumpMax);
return pumpMax;}
735 unsigned int PumpMessages(
unsigned int count=UINT_MAX)
736 {PumpMessages2(count);
return count;}
739 virtual size_t Pump2(lword &byteCount,
bool blocking=
true) =0;
740 virtual size_t PumpMessages2(
unsigned int &messageCount,
bool blocking=
true) =0;
741 virtual size_t PumpAll2(
bool blocking=
true);
742 virtual bool SourceExhausted()
const =0;
745 void SourceInitialize(
bool pumpAll,
const NameValuePairs ¶meters)
747 IsolatedInitialize(parameters);
761 {m_store.IsolatedInitialize(parameters);}
762 size_t Pump2(lword &byteCount,
bool blocking=
true)
764 size_t PumpMessages2(
unsigned int &messageCount,
bool blocking=
true)
766 size_t PumpAll2(
bool blocking=
true)
768 bool SourceExhausted()
const
769 {
return !m_store.AnyRetrievable() && !m_store.AnyMessages();}
771 {m_store.SetAutoSignalPropagation(propagation);}
772 int GetAutoSignalPropagation()
const
773 {
return m_store.GetAutoSignalPropagation();}
805 {SourceInitialize(pumpAll, MakeParameters(
"RandomNumberGeneratorPointer", &rng)(
"RandomNumberStoreSize", length));}