1 // filters.h - originally written and placed in the public domain by Wei Dai
2
3 /// \file filters.h
4 /// \brief Implementation of BufferedTransformation's attachment interface.
5
6 #ifndef CRYPTOPP_FILTERS_H
7 #define CRYPTOPP_FILTERS_H
8
9 #include "config.h"
10
11 #if CRYPTOPP_MSC_VERSION
12 # pragma warning(push)
13 # pragma warning(disable: 4127 4189 4231 4275 4514)
14 #endif
15
16 #include "cryptlib.h"
17 #include "simple.h"
18 #include "secblock.h"
19 #include "misc.h"
20 #include "smartptr.h"
21 #include "queue.h"
22 #include "algparam.h"
23 #include "stdcpp.h"
24
NAMESPACE_BEGIN(CryptoPP)25 NAMESPACE_BEGIN(CryptoPP)
26
27 /// \brief Implementation of BufferedTransformation's attachment interface
28 /// \details Filter is a cornerstone of the Pipeline trinitiy. Data flows from
29 /// Sources, through Filters, and then terminates in Sinks. The difference
30 /// between a Source and Filter is a Source \a pumps data, while a Filter does
31 /// not. The difference between a Filter and a Sink is a Filter allows an
32 /// attached transformation, while a Sink does not.
33 /// \details See the discussion of BufferedTransformation in cryptlib.h for
34 /// more details.
35 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Filter : public BufferedTransformation, public NotCopyable
36 {
37 public:
38 virtual ~Filter() {}
39
40 /// \name ATTACHMENT
41 //@{
42
43 /// \brief Construct a Filter
44 /// \param attachment an optional attached transformation
45 /// \details attachment can be NULL.
46 Filter(BufferedTransformation *attachment = NULLPTR);
47
48 /// \brief Determine if attachable
49 /// \return true if the object allows attached transformations, false otherwise.
50 /// \note Source and Filter offer attached transformations; while Sink does not.
51 bool Attachable() {return true;}
52
53 /// \brief Retrieve attached transformation
54 /// \return pointer to a BufferedTransformation if there is an attached transformation, NULL otherwise.
55 BufferedTransformation *AttachedTransformation();
56
57 /// \brief Retrieve attached transformation
58 /// \return pointer to a BufferedTransformation if there is an attached transformation, NULL otherwise.
59 const BufferedTransformation *AttachedTransformation() const;
60
61 /// \brief Replace an attached transformation
62 /// \param newAttachment an optional attached transformation
63 /// \details newAttachment can be a single filter, a chain of filters or NULL.
64 /// Pass NULL to remove an existing BufferedTransformation or chain of filters
65 void Detach(BufferedTransformation *newAttachment = NULLPTR);
66
67 //@}
68
69 /// \name RETRIEVAL OF ONE MESSAGE
70 //@{
71
72 // BufferedTransformation in cryptlib.h
73 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
74 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
75
76 //@}
77
78 /// \name SIGNALS
79 //@{
80
81 // BufferedTransformation in cryptlib.h
82 void Initialize(const NameValuePairs ¶meters=g_nullNameValuePairs, int propagation=-1);
83 bool Flush(bool hardFlush, int propagation=-1, bool blocking=true);
84 bool MessageSeriesEnd(int propagation=-1, bool blocking=true);
85
86 //@}
87
88 protected:
89 virtual BufferedTransformation * NewDefaultAttachment() const;
90 void Insert(Filter *nextFilter); // insert filter after this one
91
92 virtual bool ShouldPropagateMessageEnd() const {return true;}
93 virtual bool ShouldPropagateMessageSeriesEnd() const {return true;}
94
95 void PropagateInitialize(const NameValuePairs ¶meters, int propagation);
96
97 /// \brief Forward processed data on to attached transformation
98 /// \param outputSite unknown, system crash between keyboard and chair...
99 /// \param inString the byte buffer to process
100 /// \param length the size of the string, in bytes
101 /// \param messageEnd means how many filters to signal MessageEnd() to, including this one
102 /// \param blocking specifies whether the object should block when processing input
103 /// \param channel the channel to process the data
104 /// \return the number of bytes that remain to be processed (i.e., bytes not processed).
105 /// 0 indicates all bytes were processed.
106 size_t Output(int outputSite, const byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel=DEFAULT_CHANNEL);
107
108 /// \brief Output multiple bytes that may be modified by callee.
109 /// \param outputSite unknown, system crash between keyboard and chair...
110 /// \param inString the byte buffer to process
111 /// \param length the size of the string, in bytes
112 /// \param messageEnd means how many filters to signal MessageEnd() to, including this one
113 /// \param blocking specifies whether the object should block when processing input
114 /// \param channel the channel to process the data
115 /// \return the number of bytes that remain to be processed (i.e., bytes not processed).
116 /// 0 indicates all bytes were processed.
117 size_t OutputModifiable(int outputSite, byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel=DEFAULT_CHANNEL);
118
119 /// \brief Signals the end of messages to the object
120 /// \param outputSite unknown, system crash between keyboard and chair...
121 /// \param propagation the number of attached transformations the MessageEnd() signal should be passed
122 /// \param blocking specifies whether the object should block when processing input
123 /// \param channel the channel to process the data
124 /// \return true is the MessageEnd signal was successful, false otherwise.
125 /// \details propagation count includes this object. Setting propagation to <tt>1</tt> means this
126 /// object only. Setting propagation to <tt>-1</tt> means unlimited propagation.
127 bool OutputMessageEnd(int outputSite, int propagation, bool blocking, const std::string &channel=DEFAULT_CHANNEL);
128
129 /// \brief Flush buffered input and/or output, with signal propagation
130 /// \param outputSite unknown, system crash between keyboard and chair...
131 /// \param hardFlush is used to indicate whether all data should be flushed
132 /// \param propagation the number of attached transformations the Flush() signal should be passed
133 /// \param blocking specifies whether the object should block when processing input
134 /// \param channel the channel to process the data
135 /// \return true is the Flush signal was successful, false otherwise.
136 /// \details propagation count includes this object. Setting propagation to <tt>1</tt> means this
137 /// object only. Setting propagation to <tt>-1</tt> means unlimited propagation.
138 /// \note Hard flushes must be used with care. It means try to process and output everything, even if
139 /// there may not be enough data to complete the action. For example, hard flushing a HexDecoder
140 /// would cause an error if you do it after inputing an odd number of hex encoded characters.
141 /// \note For some types of filters, like ZlibDecompressor, hard flushes can only
142 /// be done at "synchronization points". These synchronization points are positions in the data
143 /// stream that are created by hard flushes on the corresponding reverse filters, in this
144 /// example ZlibCompressor. This is useful when zlib compressed data is moved across a
145 /// network in packets and compression state is preserved across packets, as in the SSH2 protocol.
146 bool OutputFlush(int outputSite, bool hardFlush, int propagation, bool blocking, const std::string &channel=DEFAULT_CHANNEL);
147
148 /// \brief Marks the end of a series of messages, with signal propagation
149 /// \param outputSite unknown, system crash between keyboard and chair...
150 /// \param propagation the number of attached transformations the MessageSeriesEnd() signal should be passed
151 /// \param blocking specifies whether the object should block when processing input
152 /// \param channel the channel to process the data
153 /// \return true is the MessageEnd signal was successful, false otherwise.
154 /// \details Each object that receives the signal will perform its processing, decrement
155 /// propagation, and then pass the signal on to attached transformations if the value is not 0.
156 /// \details propagation count includes this object. Setting propagation to <tt>1</tt> means this
157 /// object only. Setting propagation to <tt>-1</tt> means unlimited propagation.
158 /// \note There should be a MessageEnd() immediately before MessageSeriesEnd().
159 bool OutputMessageSeriesEnd(int outputSite, int propagation, bool blocking, const std::string &channel=DEFAULT_CHANNEL);
160
161 private:
162 member_ptr<BufferedTransformation> m_attachment;
163
164 protected:
165 size_t m_inputPosition;
166 int m_continueAt;
167 };
168
169 /// \brief Create a working space in a BufferedTransformation
170 struct CRYPTOPP_DLL FilterPutSpaceHelper
171 {
~FilterPutSpaceHelperFilterPutSpaceHelper172 virtual ~FilterPutSpaceHelper() {}
173
174 /// \brief Create a working space in a BufferedTransformation
175 /// \param target BufferedTransformation for the working space
176 /// \param channel channel for the working space
177 /// \param minSize minimum size of the allocation, in bytes
178 /// \param desiredSize preferred size of the allocation, in bytes
179 /// \param bufferSize actual size of the allocation, in bytes
180 /// \pre <tt>desiredSize >= minSize</tt> and <tt>bufferSize >= minSize</tt>.
181 /// \details bufferSize is an IN and OUT parameter. If HelpCreatePutSpace() returns a non-NULL value, then
182 /// bufferSize is valid and provides the size of the working space created for the caller.
183 /// \details Internally, HelpCreatePutSpace() calls \ref BufferedTransformation::ChannelCreatePutSpace
184 /// "ChannelCreatePutSpace()" using desiredSize. If the target returns desiredSize with a size less
185 /// than minSize (i.e., the request could not be fulfilled), then an internal SecByteBlock
186 /// called m_tempSpace is resized and used for the caller.
HelpCreatePutSpaceFilterPutSpaceHelper187 byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize, size_t desiredSize, size_t &bufferSize)
188 {
189 CRYPTOPP_ASSERT(desiredSize >= minSize && bufferSize >= minSize);
190 if (m_tempSpace.size() < minSize)
191 {
192 byte *result = target.ChannelCreatePutSpace(channel, desiredSize);
193 if (desiredSize >= minSize)
194 {
195 bufferSize = desiredSize;
196 return result;
197 }
198 m_tempSpace.New(bufferSize);
199 }
200
201 bufferSize = m_tempSpace.size();
202 return m_tempSpace.begin();
203 }
204
205 /// \brief Create a working space in a BufferedTransformation
206 /// \param target the BufferedTransformation for the working space
207 /// \param channel channel for the working space
208 /// \param minSize minimum size of the allocation, in bytes
209 /// \return pointer to the created space
210 /// \details Internally, the overload calls HelpCreatePutSpace() using minSize for missing arguments.
211 /// \details The filter will delete the space. The caller does not need to delete the space.
HelpCreatePutSpaceFilterPutSpaceHelper212 byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize)
213 {return HelpCreatePutSpace(target, channel, minSize, minSize, minSize);}
214
215 /// \brief Create a working space in a BufferedTransformation
216 /// \param target the BufferedTransformation for the working space
217 /// \param channel channel for the working space
218 /// \param minSize minimum size of the allocation, in bytes
219 /// \param bufferSize the actual size of the allocation, in bytes
220 /// \details Internally, the overload calls HelpCreatePutSpace() using minSize for missing arguments.
221 /// \details The filter will delete the space. The caller does not need to delete the space.
HelpCreatePutSpaceFilterPutSpaceHelper222 byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize, size_t bufferSize)
223 {return HelpCreatePutSpace(target, channel, minSize, minSize, bufferSize);}
224
225 /// \brief Temporay working space
226 SecByteBlock m_tempSpace;
227 };
228
229 /// \brief Measure how many bytes and messages pass through the filter
230 /// \details measure how many bytes and messages pass through the filter. The filter also serves as valve by
231 /// maintaining a list of ranges to skip during processing.
232 class CRYPTOPP_DLL MeterFilter : public Bufferless<Filter>
233 {
234 public:
~MeterFilter()235 virtual ~MeterFilter() {}
236
237 /// \brief Construct a MeterFilter
238 /// \param attachment an optional attached transformation
239 /// \param transparent flag indicating if the filter should function transparently
240 /// \details attachment can be NULL. The filter is transparent by default. If the filter is
241 /// transparent, then PutMaybeModifiable() does not process a request and always returns 0.
242 MeterFilter(BufferedTransformation *attachment=NULLPTR, bool transparent=true)
m_transparent(transparent)243 : m_transparent(transparent), m_currentMessageBytes(0), m_totalBytes(0)
244 , m_currentSeriesMessages(0), m_totalMessages(0), m_totalMessageSeries(0)
245 , m_begin(NULLPTR), m_length(0) {Detach(attachment); ResetMeter();}
246
247 /// \brief Set or change the transparent mode of this object
248 /// \param transparent the new transparent mode
SetTransparent(bool transparent)249 void SetTransparent(bool transparent) {m_transparent = transparent;}
250
251 /// \brief Adds a range to skip during processing
252 /// \param message the message to apply the range
253 /// \param position the 0-based index in the current stream
254 /// \param size the length of the range
255 /// \param sortNow flag indicating whether the range should be sorted
256 /// \details Internally, MeterFilter maitains a deque of ranges to skip. As messages are processed,
257 /// ranges of bytes are skipped according to the list of ranges.
258 void AddRangeToSkip(unsigned int message, lword position, lword size, bool sortNow = true);
259
260 /// \brief Resets the meter
261 /// \details ResetMeter() reinitializes the meter by setting counters to 0 and removing previous
262 /// skip ranges.
263 void ResetMeter();
264
265 // BufferedTransformation in cryptlib.h
IsolatedInitialize(const NameValuePairs & parameters)266 void IsolatedInitialize(const NameValuePairs ¶meters)
267 {CRYPTOPP_UNUSED(parameters); ResetMeter();}
268
269 /// \brief Number of bytes in the current message
270 /// \return the number of bytes in the current message
GetCurrentMessageBytes()271 lword GetCurrentMessageBytes() const {return m_currentMessageBytes;}
272
273 /// \brief Number of bytes processed by the filter
274 /// \return the number of bytes processed by the filter
GetTotalBytes()275 lword GetTotalBytes() const {return m_totalBytes;}
276
277 /// \brief Message number in the series
278 /// \return the message number in the series
GetCurrentSeriesMessages()279 unsigned int GetCurrentSeriesMessages() const {return m_currentSeriesMessages;}
280
281 /// \brief Number of messages in the message series
282 /// \return the number of messages in the message series
GetTotalMessages()283 unsigned int GetTotalMessages() const {return m_totalMessages;}
284
285 /// \brief Number of messages processed by the filter
286 /// \return the number of messages processed by the filter
GetTotalMessageSeries()287 unsigned int GetTotalMessageSeries() const {return m_totalMessageSeries;}
288
289 // BufferedTransformation in cryptlib.h
CreatePutSpace(size_t & size)290 byte * CreatePutSpace(size_t &size) {return AttachedTransformation()->CreatePutSpace(size);}
291 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
292 size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking);
293 bool IsolatedMessageSeriesEnd(bool blocking);
294
295 private:
296 size_t PutMaybeModifiable(byte *inString, size_t length, int messageEnd, bool blocking, bool modifiable);
ShouldPropagateMessageEnd()297 bool ShouldPropagateMessageEnd() const {return m_transparent;}
ShouldPropagateMessageSeriesEnd()298 bool ShouldPropagateMessageSeriesEnd() const {return m_transparent;}
299
300 struct MessageRange
301 {
302 inline bool operator<(const MessageRange &b) const // BCB2006 workaround: this has to be a member function
303 {return message < b.message || (message == b.message && position < b.position);}
304 unsigned int message; lword position; lword size;
305 };
306
307 bool m_transparent;
308 lword m_currentMessageBytes, m_totalBytes;
309 unsigned int m_currentSeriesMessages, m_totalMessages, m_totalMessageSeries;
310 std::deque<MessageRange> m_rangesToSkip;
311 byte *m_begin;
312 size_t m_length;
313 };
314
315 /// \brief A transparent MeterFilter
316 /// \sa MeterFilter, OpaqueFilter
317 class CRYPTOPP_DLL TransparentFilter : public MeterFilter
318 {
319 public:
320 /// \brief Construct a TransparentFilter
321 /// \param attachment an optional attached transformation
MeterFilter(attachment,true)322 TransparentFilter(BufferedTransformation *attachment=NULLPTR) : MeterFilter(attachment, true) {}
323 };
324
325 /// \brief A non-transparent MeterFilter
326 /// \sa MeterFilter, TransparentFilter
327 class CRYPTOPP_DLL OpaqueFilter : public MeterFilter
328 {
329 public:
330 /// \brief Construct an OpaqueFilter
331 /// \param attachment an optional attached transformation
MeterFilter(attachment,false)332 OpaqueFilter(BufferedTransformation *attachment=NULLPTR) : MeterFilter(attachment, false) {}
333 };
334
335 /// \brief Divides an input stream into discrete blocks
336 /// \details FilterWithBufferedInput divides the input stream into a first block, a number of
337 /// middle blocks, and a last block. First and last blocks are optional, and middle blocks may
338 /// be a stream instead (i.e. <tt>blockSize == 1</tt>).
339 /// \sa AuthenticatedEncryptionFilter, AuthenticatedDecryptionFilter, HashVerificationFilter,
340 /// SignatureVerificationFilter, StreamTransformationFilter
341 class CRYPTOPP_DLL FilterWithBufferedInput : public Filter
342 {
343 public:
~FilterWithBufferedInput()344 virtual ~FilterWithBufferedInput() {}
345
346 /// \brief Construct a FilterWithBufferedInput with an attached transformation
347 /// \param attachment an attached transformation
348 FilterWithBufferedInput(BufferedTransformation *attachment);
349
350 /// \brief Construct a FilterWithBufferedInput with an attached transformation
351 /// \param firstSize the size of the first block
352 /// \param blockSize the size of middle blocks
353 /// \param lastSize the size of the last block
354 /// \param attachment an attached transformation
355 /// \details firstSize and lastSize may be 0. blockSize must be at least 1.
356 FilterWithBufferedInput(size_t firstSize, size_t blockSize, size_t lastSize, BufferedTransformation *attachment);
357
358 void IsolatedInitialize(const NameValuePairs ¶meters);
Put2(const byte * inString,size_t length,int messageEnd,bool blocking)359 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
360 {
361 return PutMaybeModifiable(const_cast<byte *>(inString), length, messageEnd, blocking, false);
362 }
363
PutModifiable2(byte * inString,size_t length,int messageEnd,bool blocking)364 size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
365 {
366 return PutMaybeModifiable(inString, length, messageEnd, blocking, true);
367 }
368
369 /// \brief Flushes data buffered by this object, without signal propagation
370 /// \param hardFlush indicates whether all data should be flushed
371 /// \param blocking specifies whether the object should block when processing input
372 /// \return true if the Flush was successful, false otherwise
373 /// \details IsolatedFlush() calls ForceNextPut() if hardFlush is true
374 /// \note hardFlush must be used with care
375 bool IsolatedFlush(bool hardFlush, bool blocking);
376
377 /// \brief Flushes data buffered by this object
378 /// \details The input buffer may contain more than blockSize bytes if <tt>lastSize != 0</tt>.
379 /// ForceNextPut() forces a call to NextPut() if this is the case.
380 void ForceNextPut();
381
382 protected:
DidFirstPut()383 virtual bool DidFirstPut() const {return m_firstInputDone;}
GetFirstPutSize()384 virtual size_t GetFirstPutSize() const {return m_firstSize;}
GetBlockPutSize()385 virtual size_t GetBlockPutSize() const {return m_blockSize;}
GetLastPutSize()386 virtual size_t GetLastPutSize() const {return m_lastSize;}
387
InitializeDerivedAndReturnNewSizes(const NameValuePairs & parameters,size_t & firstSize,size_t & blockSize,size_t & lastSize)388 virtual void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
389 {CRYPTOPP_UNUSED(parameters); CRYPTOPP_UNUSED(firstSize); CRYPTOPP_UNUSED(blockSize); CRYPTOPP_UNUSED(lastSize); InitializeDerived(parameters);}
InitializeDerived(const NameValuePairs & parameters)390 virtual void InitializeDerived(const NameValuePairs ¶meters)
391 {CRYPTOPP_UNUSED(parameters);}
392 // FirstPut() is called if (firstSize != 0 and totalLength >= firstSize)
393 // or (firstSize == 0 and (totalLength > 0 or a MessageEnd() is received)).
394 // inString is m_firstSize in length.
395 virtual void FirstPut(const byte *inString) =0;
396 // NextPut() is called if totalLength >= firstSize+blockSize+lastSize
NextPutSingle(const byte * inString)397 virtual void NextPutSingle(const byte *inString)
398 {CRYPTOPP_UNUSED(inString); CRYPTOPP_ASSERT(false);}
399 // Same as NextPut() except length can be a multiple of blockSize
400 // Either NextPut() or NextPutMultiple() must be overridden
401 virtual void NextPutMultiple(const byte *inString, size_t length);
402 // Same as NextPutMultiple(), but inString can be modified
NextPutModifiable(byte * inString,size_t length)403 virtual void NextPutModifiable(byte *inString, size_t length)
404 {NextPutMultiple(inString, length);}
405 /// \brief Input the last block of data
406 /// \param inString the input byte buffer
407 /// \param length the size of the input buffer, in bytes
408 /// \details LastPut() processes the last block of data and signals attached filters to do the same.
409 /// LastPut() is always called. The pseudo algorithm for the logic is:
410 /// <pre>
411 /// if totalLength < firstSize then length == totalLength
412 /// else if totalLength <= firstSize+lastSize then length == totalLength-firstSize
413 /// else lastSize <= length < lastSize+blockSize
414 /// </pre>
415 virtual void LastPut(const byte *inString, size_t length) =0;
FlushDerived()416 virtual void FlushDerived() {}
417
418 protected:
419 size_t PutMaybeModifiable(byte *begin, size_t length, int messageEnd, bool blocking, bool modifiable);
NextPutMaybeModifiable(byte * inString,size_t length,bool modifiable)420 void NextPutMaybeModifiable(byte *inString, size_t length, bool modifiable)
421 {
422 if (modifiable) NextPutModifiable(inString, length);
423 else NextPutMultiple(inString, length);
424 }
425
426 // This function should no longer be used, put this here to cause a compiler error
427 // if someone tries to override NextPut().
NextPut(const byte * inString,size_t length)428 virtual int NextPut(const byte *inString, size_t length)
429 {CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_ASSERT(false); return 0;}
430
431 class BlockQueue
432 {
433 public:
434 void ResetQueue(size_t blockSize, size_t maxBlocks);
435 byte *GetBlock();
436 byte *GetContigousBlocks(size_t &numberOfBytes);
437 size_t GetAll(byte *outString);
438 void Put(const byte *inString, size_t length);
CurrentSize()439 size_t CurrentSize() const {return m_size;}
MaxSize()440 size_t MaxSize() const {return m_buffer.size();}
441
442 private:
443 SecByteBlock m_buffer;
444 size_t m_blockSize, m_maxBlocks, m_size;
445 byte *m_begin;
446 };
447
448 size_t m_firstSize, m_blockSize, m_lastSize;
449 bool m_firstInputDone;
450 BlockQueue m_queue;
451 };
452
453 /// \brief A filter that buffers input using a ByteQueue
454 /// \details FilterWithInputQueue will buffer input using a ByteQueue. When the filter receives
455 /// a \ref BufferedTransformation::MessageEnd() "MessageEnd()" signal it will pass the data
456 /// on to its attached transformation.
457 class CRYPTOPP_DLL FilterWithInputQueue : public Filter
458 {
459 public:
~FilterWithInputQueue()460 virtual ~FilterWithInputQueue() {}
461
462 /// \brief Construct a FilterWithInputQueue
463 /// \param attachment an optional attached transformation
Filter(attachment)464 FilterWithInputQueue(BufferedTransformation *attachment=NULLPTR) : Filter(attachment) {}
465
Put2(const byte * inString,size_t length,int messageEnd,bool blocking)466 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
467 {
468 if (!blocking)
469 throw BlockingInputOnly("FilterWithInputQueue");
470
471 m_inQueue.Put(inString, length);
472 if (messageEnd)
473 {
474 IsolatedMessageEnd(blocking);
475 Output(0, NULLPTR, 0, messageEnd, blocking);
476 }
477 return 0;
478 }
479
480 protected:
481 virtual bool IsolatedMessageEnd(bool blocking) =0;
IsolatedInitialize(const NameValuePairs & parameters)482 void IsolatedInitialize(const NameValuePairs ¶meters)
483 {CRYPTOPP_UNUSED(parameters); m_inQueue.Clear();}
484
485 ByteQueue m_inQueue;
486 };
487
488 /// \struct BlockPaddingSchemeDef
489 /// \brief Padding schemes used for block ciphers
490 /// \since Crypto++ 5.0
491 struct BlockPaddingSchemeDef
492 {
493 /// \enum BlockPaddingScheme
494 /// \brief Padding schemes used for block ciphers.
495 /// \details DEFAULT_PADDING means PKCS_PADDING if <tt>cipher.MandatoryBlockSize() > 1 &&
496 /// cipher.MinLastBlockSize() == 0</tt>, which holds for ECB or CBC mode. Otherwise,
497 /// NO_PADDING for modes like OFB, CFB, CTR, CBC-CTS.
498 /// \sa <A HREF="http://www.weidai.com/scan-mirror/csp.html">Block Cipher Padding</A> for
499 /// additional details.
500 /// \since Crypto++ 5.0
501 enum BlockPaddingScheme {
502 /// \brief No padding added to a block
503 /// \since Crypto++ 5.0
504 NO_PADDING,
505 /// \brief 0's padding added to a block
506 /// \since Crypto++ 5.0
507 ZEROS_PADDING,
508 /// \brief PKCS padding added to a block
509 /// \since Crypto++ 5.0
510 PKCS_PADDING,
511 /// \brief 1 and 0's padding added to a block
512 /// \since Crypto++ 5.0
513 ONE_AND_ZEROS_PADDING,
514 /// \brief W3C padding added to a block
515 /// \sa <A HREF="http://www.w3.org/TR/2002/REC-xmlenc-core-20021210/Overview.html">XML
516 /// Encryption Syntax and Processing</A>
517 /// \since Crypto++ 6.0
518 W3C_PADDING,
519 /// \brief Default padding scheme
520 /// \since Crypto++ 5.0
521 DEFAULT_PADDING
522 };
523 };
524
525 /// \brief Filter wrapper for StreamTransformation
526 /// \details StreamTransformationFilter() is a filter wrapper for StreamTransformation(). It is used when
527 /// pipelining data for stream ciphers and confidentiality-only block ciphers. The filter will optionally
528 /// handle padding and unpadding when needed. If you are using an authenticated encryption mode of operation,
529 /// then use AuthenticatedEncryptionFilter() and AuthenticatedDecryptionFilter()
530 /// \since Crypto++ 5.0
531 class CRYPTOPP_DLL StreamTransformationFilter : public FilterWithBufferedInput, public BlockPaddingSchemeDef, private FilterPutSpaceHelper
532 {
533 public:
~StreamTransformationFilter()534 virtual ~StreamTransformationFilter() {}
535
536 /// \brief Construct a StreamTransformationFilter
537 /// \param c reference to a StreamTransformation
538 /// \param attachment an optional attached transformation
539 /// \param padding the \ref BlockPaddingSchemeDef "padding scheme"
540 /// \details This contructor creates a StreamTransformationFilter() for stream ciphers and
541 /// confidentiality-only block cipher modes of operation. If you are using an authenticated
542 /// encryption mode of operation, then use either AuthenticatedEncryptionFilter() or
543 /// AuthenticatedDecryptionFilter().
544 /// \sa AuthenticatedEncryptionFilter() and AuthenticatedDecryptionFilter()
545 StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment = NULLPTR, BlockPaddingScheme padding = DEFAULT_PADDING);
546
AlgorithmName()547 std::string AlgorithmName() const {return m_cipher.AlgorithmName();}
548
549 protected:
550
551 friend class AuthenticatedEncryptionFilter;
552 friend class AuthenticatedDecryptionFilter;
553
554 /// \brief Construct a StreamTransformationFilter
555 /// \param c reference to a StreamTransformation
556 /// \param attachment an optional attached transformation
557 /// \param padding the \ref BlockPaddingSchemeDef "padding scheme"
558 /// \param authenticated flag indicating whether the filter should allow authenticated encryption schemes
559 /// \details This constructor is used for authenticated encryption mode of operation and by
560 /// AuthenticatedEncryptionFilter() and AuthenticatedDecryptionFilter().
561 StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment, BlockPaddingScheme padding, bool authenticated);
562
563 void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
564 void FirstPut(const byte *inString);
565 void NextPutMultiple(const byte *inString, size_t length);
566 void NextPutModifiable(byte *inString, size_t length);
567 void LastPut(const byte *inString, size_t length);
568
569 static size_t LastBlockSize(StreamTransformation &c, BlockPaddingScheme padding);
570
571 StreamTransformation &m_cipher;
572 BlockPaddingScheme m_padding;
573 unsigned int m_mandatoryBlockSize;
574 unsigned int m_optimalBufferSize;
575 unsigned int m_reservedBufferSize;
576 bool m_isSpecial;
577 };
578
579 /// \brief Filter wrapper for HashTransformation
580 /// \since Crypto++ 1.0
581 class CRYPTOPP_DLL HashFilter : public Bufferless<Filter>, private FilterPutSpaceHelper
582 {
583 public:
~HashFilter()584 virtual ~HashFilter() {}
585
586 /// \brief Construct a HashFilter
587 /// \param hm reference to a HashTransformation
588 /// \param attachment an optional attached transformation
589 /// \param putMessage flag indicating whether the original message should be passed to an attached transformation
590 /// \param truncatedDigestSize the size of the digest
591 /// \param messagePutChannel the channel on which the message should be output
592 /// \param hashPutChannel the channel on which the digest should be output
593 HashFilter(HashTransformation &hm, BufferedTransformation *attachment = NULLPTR, bool putMessage=false, int truncatedDigestSize=-1, const std::string &messagePutChannel=DEFAULT_CHANNEL, const std::string &hashPutChannel=DEFAULT_CHANNEL);
594
AlgorithmName()595 std::string AlgorithmName() const {return m_hashModule.AlgorithmName();}
596 void IsolatedInitialize(const NameValuePairs ¶meters);
597 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
CreatePutSpace(size_t & size)598 byte * CreatePutSpace(size_t &size) {return m_hashModule.CreateUpdateSpace(size);}
599
600 private:
601 HashTransformation &m_hashModule;
602 bool m_putMessage;
603 unsigned int m_digestSize;
604 byte *m_space;
605 std::string m_messagePutChannel, m_hashPutChannel;
606 };
607
608 /// \brief Filter wrapper for HashTransformation
609 /// \since Crypto++ 4.0
610 class CRYPTOPP_DLL HashVerificationFilter : public FilterWithBufferedInput
611 {
612 public:
~HashVerificationFilter()613 virtual ~HashVerificationFilter() {}
614
615 /// \brief Exception thrown when a data integrity check failure is encountered
616 class HashVerificationFailed : public Exception
617 {
618 public:
HashVerificationFailed()619 HashVerificationFailed()
620 : Exception(DATA_INTEGRITY_CHECK_FAILED, "HashVerificationFilter: message hash or MAC not valid") {}
621 };
622
623 /// \enum Flags
624 /// \brief Flags controlling filter behavior.
625 /// \details The flags are a bitmask and can be OR'd together.
626 enum Flags {
627 /// \brief The hash is at the end of the message (i.e., concatenation of message+hash)
628 HASH_AT_END=0,
629 /// \brief The hash is at the beginning of the message (i.e., concatenation of hash+message)
630 HASH_AT_BEGIN=1,
631 /// \brief The message should be passed to an attached transformation
632 PUT_MESSAGE=2,
633 /// \brief The hash should be passed to an attached transformation
634 PUT_HASH=4,
635 /// \brief The result of the verification should be passed to an attached transformation
636 PUT_RESULT=8,
637 /// \brief The filter should throw a HashVerificationFailed if a failure is encountered
638 THROW_EXCEPTION=16,
639 /// \brief Default flags using HASH_AT_BEGIN and PUT_RESULT
640 DEFAULT_FLAGS = HASH_AT_BEGIN | PUT_RESULT
641 };
642
643 /// \brief Construct a HashVerificationFilter
644 /// \param hm reference to a HashTransformation
645 /// \param attachment an optional attached transformation
646 /// \param flags flags indicating behaviors for the filter
647 /// \param truncatedDigestSize the size of the digest
648 /// \details <tt>truncatedDigestSize = -1</tt> indicates \ref HashTransformation::DigestSize() "DigestSize" should be used.
649 HashVerificationFilter(HashTransformation &hm, BufferedTransformation *attachment = NULLPTR, word32 flags = DEFAULT_FLAGS, int truncatedDigestSize=-1);
650
AlgorithmName()651 std::string AlgorithmName() const {return m_hashModule.AlgorithmName();}
GetLastResult()652 bool GetLastResult() const {return m_verified;}
653
654 protected:
655 void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
656 void FirstPut(const byte *inString);
657 void NextPutMultiple(const byte *inString, size_t length);
658 void LastPut(const byte *inString, size_t length);
659
660 private:
661 friend class AuthenticatedDecryptionFilter;
662
663 HashTransformation &m_hashModule;
664 word32 m_flags;
665 unsigned int m_digestSize;
666 bool m_verified;
667 SecByteBlock m_expectedHash;
668 };
669
670 /// \brief Filter wrapper for encrypting with AuthenticatedSymmetricCipher
671 /// \details AuthenticatedEncryptionFilter() is a wrapper for encrypting with
672 /// AuthenticatedSymmetricCipher(), optionally handling padding/unpadding when needed.
673 /// \details AuthenticatedDecryptionFilter() for Crypto++ 8.2 and earlier
674 /// had a bug where a FileSource() would cause an exception, but a StringSource()
675 /// was OK. Also see <A HREF=
676 /// "https://github.com/weidai11/cryptopp/issues/817">Issue 817</A> and <A HREF=
677 /// "https://github.com/weidai11/cryptopp/commit/ff110c6e183e">Commit ff110c6e183e</A>.
678 /// \sa AuthenticatedSymmetricCipher, AuthenticatedDecryptionFilter, EAX, CCM, GCM,
679 /// and <A HREF="https://www.cryptopp.com/wiki/AadSource">AadSource</A> on the
680 /// Crypto++ wiki.
681 /// \since Crypto++ 5.6.0
682 class CRYPTOPP_DLL AuthenticatedEncryptionFilter : public StreamTransformationFilter
683 {
684 public:
~AuthenticatedEncryptionFilter()685 virtual ~AuthenticatedEncryptionFilter() {}
686
687 /// \brief Construct a AuthenticatedEncryptionFilter
688 /// \param c reference to a AuthenticatedSymmetricCipher
689 /// \param attachment an optional attached transformation
690 /// \param putAAD flag indicating whether the AAD should be passed to an attached transformation
691 /// \param truncatedDigestSize the size of the digest
692 /// \param macChannel the channel on which the MAC should be output
693 /// \param padding the \ref BlockPaddingSchemeDef "padding scheme"
694 /// \details <tt>truncatedDigestSize = -1</tt> indicates \ref HashTransformation::DigestSize() "DigestSize" should be used.
695 /// \since Crypto++ 5.6.0
696 AuthenticatedEncryptionFilter(AuthenticatedSymmetricCipher &c, BufferedTransformation *attachment = NULLPTR, bool putAAD=false, int truncatedDigestSize=-1, const std::string &macChannel=DEFAULT_CHANNEL, BlockPaddingScheme padding = DEFAULT_PADDING);
697
698 void IsolatedInitialize(const NameValuePairs ¶meters);
699 byte * ChannelCreatePutSpace(const std::string &channel, size_t &size);
700 size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking);
701
702 /// \brief Input the last block of data
703 /// \param inString the input byte buffer
704 /// \param length the size of the input buffer, in bytes
705 /// \details LastPut() processes the last block of data and signals attached filters to do the same.
706 /// LastPut() is always called. The pseudo algorithm for the logic is:
707 /// <pre>
708 /// if totalLength < firstSize then length == totalLength
709 /// else if totalLength <= firstSize+lastSize then length == totalLength-firstSize
710 /// else lastSize <= length < lastSize+blockSize
711 /// </pre>
712 void LastPut(const byte *inString, size_t length);
713
714 protected:
715 HashFilter m_hf;
716 };
717
718 /// \brief Filter wrapper for decrypting with AuthenticatedSymmetricCipher
719 /// \details AuthenticatedDecryptionFilter() is a wrapper for decrypting with
720 /// AuthenticatedSymmetricCipher(), optionally handling padding/unpadding when
721 /// needed.
722 /// \details AuthenticatedDecryptionFilter() for Crypto++ 8.2 and earlier
723 /// had a bug where a FileSource() would cause an exception, but a StringSource()
724 /// was OK. Also see <A HREF=
725 /// "https://github.com/weidai11/cryptopp/issues/817">Issue 817</A> and <A HREF=
726 /// "https://github.com/weidai11/cryptopp/commit/ff110c6e183e">Commit ff110c6e183e</A>.
727 /// \sa AuthenticatedSymmetricCipher, AuthenticatedEncryptionFilter, EAX, CCM, GCM,
728 /// and <A HREF="https://www.cryptopp.com/wiki/AadSource">AadSource</A> on the
729 /// Crypto++ wiki.
730 /// \since Crypto++ 5.6.0
731 class CRYPTOPP_DLL AuthenticatedDecryptionFilter : public FilterWithBufferedInput, public BlockPaddingSchemeDef
732 {
733 public:
734 /// \enum Flags
735 /// \brief Flags controlling filter behavior.
736 /// \details The flags are a bitmask and can be OR'd together.
737 enum Flags {
738 /// \brief The MAC is at the end of the message (i.e., concatenation of message+mac)
739 MAC_AT_END=0,
740 /// \brief The MAC is at the beginning of the message (i.e., concatenation of mac+message)
741 MAC_AT_BEGIN=1,
742 /// \brief The filter should throw a HashVerificationFailed if a failure is encountered
743 THROW_EXCEPTION=16,
744 /// \brief Default flags using THROW_EXCEPTION
745 DEFAULT_FLAGS = THROW_EXCEPTION
746 };
747
~AuthenticatedDecryptionFilter()748 virtual ~AuthenticatedDecryptionFilter() {}
749
750 /// \brief Construct a AuthenticatedDecryptionFilter
751 /// \param c reference to a AuthenticatedSymmetricCipher
752 /// \param attachment an optional attached transformation
753 /// \param flags flags indicating behaviors for the filter
754 /// \param truncatedDigestSize the size of the digest
755 /// \param padding the \ref BlockPaddingSchemeDef "padding scheme"
756 /// \details Additional authenticated data should be given in channel "AAD".
757 /// \details <tt>truncatedDigestSize = -1</tt> indicates \ref HashTransformation::DigestSize() "DigestSize" should be used.
758 /// \since Crypto++ 5.6.0
759 AuthenticatedDecryptionFilter(AuthenticatedSymmetricCipher &c, BufferedTransformation *attachment = NULLPTR, word32 flags = DEFAULT_FLAGS, int truncatedDigestSize=-1, BlockPaddingScheme padding = DEFAULT_PADDING);
760
AlgorithmName()761 std::string AlgorithmName() const {return m_hashVerifier.AlgorithmName();}
762 byte * ChannelCreatePutSpace(const std::string &channel, size_t &size);
763 size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking);
ChannelPutModifiable2(const std::string & channel,byte * begin,size_t length,int messageEnd,bool blocking)764 size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
765 { return ChannelPut2(channel, begin, length, messageEnd, blocking); }
766 /// \brief Get verifier result
767 /// \return true if the digest on the previosus message was valid, false otherwise
GetLastResult()768 bool GetLastResult() const {return m_hashVerifier.GetLastResult();}
769
770 protected:
771 void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
772 void FirstPut(const byte *inString);
773 void NextPutMultiple(const byte *inString, size_t length);
774
775 /// \brief Input the last block of data
776 /// \param inString the input byte buffer
777 /// \param length the size of the input buffer, in bytes
778 /// \details LastPut() processes the last block of data and signals attached filters to do the same.
779 /// LastPut() is always called. The pseudo algorithm for the logic is:
780 /// <pre>
781 /// if totalLength < firstSize then length == totalLength
782 /// else if totalLength <= firstSize+lastSize then length == totalLength-firstSize
783 /// else lastSize <= length < lastSize+blockSize
784 /// </pre>
785 void LastPut(const byte *inString, size_t length);
786
787 HashVerificationFilter m_hashVerifier;
788 StreamTransformationFilter m_streamFilter;
789 };
790
791 /// \brief Filter wrapper for PK_Signer
792 /// \since Crypto++ 4.0
793 class CRYPTOPP_DLL SignerFilter : public Unflushable<Filter>
794 {
795 public:
~SignerFilter()796 virtual ~SignerFilter() {}
797
798 /// \brief Construct a SignerFilter
799 /// \param rng a RandomNumberGenerator derived class
800 /// \param signer a PK_Signer derived class
801 /// \param attachment an optional attached transformation
802 /// \param putMessage flag indicating whether the original message should be passed to an attached transformation
803 SignerFilter(RandomNumberGenerator &rng, const PK_Signer &signer, BufferedTransformation *attachment = NULLPTR, bool putMessage=false)
m_rng(rng)804 : m_rng(rng), m_signer(signer), m_messageAccumulator(signer.NewSignatureAccumulator(rng)), m_putMessage(putMessage) {Detach(attachment);}
805
AlgorithmName()806 std::string AlgorithmName() const {return m_signer.AlgorithmName();}
807
808 void IsolatedInitialize(const NameValuePairs ¶meters);
809 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
810
811 private:
812 RandomNumberGenerator &m_rng;
813 const PK_Signer &m_signer;
814 member_ptr<PK_MessageAccumulator> m_messageAccumulator;
815 bool m_putMessage;
816 SecByteBlock m_buf;
817 };
818
819 /// \brief Filter wrapper for PK_Verifier
820 /// \details This filter was formerly named <tt>VerifierFilter</tt>. The name changed at Crypto++ 5.0.
821 /// \since Crypto++ 4.0
822 class CRYPTOPP_DLL SignatureVerificationFilter : public FilterWithBufferedInput
823 {
824 public:
825 /// \brief Exception thrown when an invalid signature is encountered
826 class SignatureVerificationFailed : public Exception
827 {
828 public:
SignatureVerificationFailed()829 SignatureVerificationFailed()
830 : Exception(DATA_INTEGRITY_CHECK_FAILED, "VerifierFilter: digital signature not valid") {}
831 };
832
833 /// \enum Flags
834 /// \brief Flags controlling filter behavior.
835 /// \details The flags are a bitmask and can be OR'd together.
836 enum Flags {
837 /// \brief The signature is at the end of the message (i.e., concatenation of message+signature)
838 SIGNATURE_AT_END=0,
839 /// \brief The signature is at the beginning of the message (i.e., concatenation of signature+message)
840 SIGNATURE_AT_BEGIN=1,
841 /// \brief The message should be passed to an attached transformation
842 PUT_MESSAGE=2,
843 /// \brief The signature should be passed to an attached transformation
844 PUT_SIGNATURE=4,
845 /// \brief The result of the verification should be passed to an attached transformation
846 PUT_RESULT=8,
847 /// \brief The filter should throw a HashVerificationFailed if a failure is encountered
848 THROW_EXCEPTION=16,
849 /// \brief Default flags using SIGNATURE_AT_BEGIN and PUT_RESULT
850 DEFAULT_FLAGS = SIGNATURE_AT_BEGIN | PUT_RESULT
851 };
852
~SignatureVerificationFilter()853 virtual ~SignatureVerificationFilter() {}
854
855 /// \brief Construct a SignatureVerificationFilter
856 /// \param verifier a PK_Verifier derived class
857 /// \param attachment an optional attached transformation
858 /// \param flags flags indicating behaviors for the filter
859 SignatureVerificationFilter(const PK_Verifier &verifier, BufferedTransformation *attachment = NULLPTR, word32 flags = DEFAULT_FLAGS);
860
AlgorithmName()861 std::string AlgorithmName() const {return m_verifier.AlgorithmName();}
862
863 /// \brief Retrieves the result of the last verification
864 /// \return true if the signature on the previosus message was valid, false otherwise
GetLastResult()865 bool GetLastResult() const {return m_verified;}
866
867 protected:
868 void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
869 void FirstPut(const byte *inString);
870 void NextPutMultiple(const byte *inString, size_t length);
871 void LastPut(const byte *inString, size_t length);
872
873 private:
874 const PK_Verifier &m_verifier;
875 member_ptr<PK_MessageAccumulator> m_messageAccumulator;
876 word32 m_flags;
877 SecByteBlock m_signature;
878 bool m_verified;
879 };
880
881 /// \brief Redirect input to another BufferedTransformation without owning it
882 /// \since Crypto++ 4.0
883 class CRYPTOPP_DLL Redirector : public CustomSignalPropagation<Sink>
884 {
885 public:
886 /// \enum Behavior
887 /// \brief Controls signal propagation behavior
888 enum Behavior
889 {
890 /// \brief Pass data only
891 DATA_ONLY = 0x00,
892 /// \brief Pass signals
893 PASS_SIGNALS = 0x01,
894 /// \brief Pass wait events
895 PASS_WAIT_OBJECTS = 0x02,
896 /// \brief Pass everything
897 /// \details PASS_EVERYTHING is default
898 PASS_EVERYTHING = PASS_SIGNALS | PASS_WAIT_OBJECTS
899 };
900
~Redirector()901 virtual ~Redirector() {}
902
903 /// \brief Construct a Redirector
Redirector()904 Redirector() : m_target(NULLPTR), m_behavior(PASS_EVERYTHING) {}
905
906 /// \brief Construct a Redirector
907 /// \param target the destination BufferedTransformation
908 /// \param behavior Behavior "flags" specifying signal propagation
909 Redirector(BufferedTransformation &target, Behavior behavior=PASS_EVERYTHING)
910 : m_target(&target), m_behavior(behavior) {}
911
912 /// \brief Redirect input to another BufferedTransformation
913 /// \param target the destination BufferedTransformation
Redirect(BufferedTransformation & target)914 void Redirect(BufferedTransformation &target) {m_target = ⌖}
915 /// \brief Stop redirecting input
StopRedirection()916 void StopRedirection() {m_target = NULLPTR;}
917
918 /// \brief Retrieve signal propagation behavior
919 /// \return the current signal propagation behavior
GetBehavior()920 Behavior GetBehavior() {return static_cast<Behavior>(m_behavior);}
921 /// \brief Set signal propagation behavior
922 /// \param behavior the new signal propagation behavior
SetBehavior(Behavior behavior)923 void SetBehavior(Behavior behavior) {m_behavior=behavior;}
924 /// \brief Retrieve signal propagation behavior
925 /// \return true if the Redirector passes signals, false otherwise.
GetPassSignals()926 bool GetPassSignals() const {return (m_behavior & PASS_SIGNALS) != 0;}
927 /// \brief Set signal propagation behavior
928 /// \param pass flag indicating if the Redirector should pass signals
SetPassSignals(bool pass)929 void SetPassSignals(bool pass) { if (pass) m_behavior |= PASS_SIGNALS; else m_behavior &= ~static_cast<word32>(PASS_SIGNALS); }
930 /// \brief Retrieve signal propagation behavior
931 /// \return true if the Redirector passes wait objects, false otherwise.
GetPassWaitObjects()932 bool GetPassWaitObjects() const {return (m_behavior & PASS_WAIT_OBJECTS) != 0;}
933 /// \brief Set signal propagation behavior
934 /// \param pass flag indicating if the Redirector should pass wait objects
SetPassWaitObjects(bool pass)935 void SetPassWaitObjects(bool pass) { if (pass) m_behavior |= PASS_WAIT_OBJECTS; else m_behavior &= ~static_cast<word32>(PASS_WAIT_OBJECTS); }
936
CanModifyInput()937 bool CanModifyInput() const
938 {return m_target ? m_target->CanModifyInput() : false;}
939
940 void Initialize(const NameValuePairs ¶meters, int propagation);
CreatePutSpace(size_t & size)941 byte * CreatePutSpace(size_t &size)
942 {
943 if (m_target)
944 return m_target->CreatePutSpace(size);
945 else
946 {
947 size = 0;
948 return NULLPTR;
949 }
950 }
Put2(const byte * inString,size_t length,int messageEnd,bool blocking)951 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
952 {return m_target ? m_target->Put2(inString, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
953 bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
954 {return m_target && GetPassSignals() ? m_target->Flush(hardFlush, propagation, blocking) : false;}
955 bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
956 {return m_target && GetPassSignals() ? m_target->MessageSeriesEnd(propagation, blocking) : false;}
957
ChannelCreatePutSpace(const std::string & channel,size_t & size)958 byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
959 {
960 if (m_target)
961 return m_target->ChannelCreatePutSpace(channel, size);
962 else
963 {
964 size = 0;
965 return NULLPTR;
966 }
967 }
ChannelPut2(const std::string & channel,const byte * begin,size_t length,int messageEnd,bool blocking)968 size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
969 {return m_target ? m_target->ChannelPut2(channel, begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
ChannelPutModifiable2(const std::string & channel,byte * begin,size_t length,int messageEnd,bool blocking)970 size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
971 {return m_target ? m_target->ChannelPutModifiable2(channel, begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
972 bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true)
973 {return m_target && GetPassSignals() ? m_target->ChannelFlush(channel, completeFlush, propagation, blocking) : false;}
974 bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true)
975 {return m_target && GetPassSignals() ? m_target->ChannelMessageSeriesEnd(channel, propagation, blocking) : false;}
976
GetMaxWaitObjectCount()977 unsigned int GetMaxWaitObjectCount() const
978 { return m_target && GetPassWaitObjects() ? m_target->GetMaxWaitObjectCount() : 0; }
GetWaitObjects(WaitObjectContainer & container,CallStack const & callStack)979 void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack)
980 { if (m_target && GetPassWaitObjects()) m_target->GetWaitObjects(container, callStack); }
981
982 private:
983 BufferedTransformation *m_target;
984 word32 m_behavior;
985 };
986
987 /// \brief Filter class that is a proxy for a sink
988 /// \details Used By ProxyFilter
989 /// \since Crypto++ 4.0
990 class CRYPTOPP_DLL OutputProxy : public CustomSignalPropagation<Sink>
991 {
992 public:
~OutputProxy()993 virtual ~OutputProxy() {}
994
995 /// \brief Construct an OutputProxy
996 /// \param owner the owning transformation
997 /// \param passSignal flag indicating if signals should be passed
OutputProxy(BufferedTransformation & owner,bool passSignal)998 OutputProxy(BufferedTransformation &owner, bool passSignal) : m_owner(owner), m_passSignal(passSignal) {}
999
1000 /// \brief Retrieve passSignal flag
1001 /// \return flag indicating if signals should be passed
GetPassSignal()1002 bool GetPassSignal() const {return m_passSignal;}
1003 /// \brief Set passSignal flag
1004 /// \param passSignal flag indicating if signals should be passed
SetPassSignal(bool passSignal)1005 void SetPassSignal(bool passSignal) {m_passSignal = passSignal;}
1006
CreatePutSpace(size_t & size)1007 byte * CreatePutSpace(size_t &size)
1008 {return m_owner.AttachedTransformation()->CreatePutSpace(size);}
Put2(const byte * inString,size_t length,int messageEnd,bool blocking)1009 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
1010 {return m_owner.AttachedTransformation()->Put2(inString, length, m_passSignal ? messageEnd : 0, blocking);}
PutModifiable2(byte * begin,size_t length,int messageEnd,bool blocking)1011 size_t PutModifiable2(byte *begin, size_t length, int messageEnd, bool blocking)
1012 {return m_owner.AttachedTransformation()->PutModifiable2(begin, length, m_passSignal ? messageEnd : 0, blocking);}
1013 void Initialize(const NameValuePairs ¶meters=g_nullNameValuePairs, int propagation=-1)
1014 {if (m_passSignal) m_owner.AttachedTransformation()->Initialize(parameters, propagation);}
1015 bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
1016 {return m_passSignal ? m_owner.AttachedTransformation()->Flush(hardFlush, propagation, blocking) : false;}
1017 bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
1018 {return m_passSignal ? m_owner.AttachedTransformation()->MessageSeriesEnd(propagation, blocking) : false;}
1019
ChannelCreatePutSpace(const std::string & channel,size_t & size)1020 byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
1021 {return m_owner.AttachedTransformation()->ChannelCreatePutSpace(channel, size);}
ChannelPut2(const std::string & channel,const byte * begin,size_t length,int messageEnd,bool blocking)1022 size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
1023 {return m_owner.AttachedTransformation()->ChannelPut2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking);}
ChannelPutModifiable2(const std::string & channel,byte * begin,size_t length,int messageEnd,bool blocking)1024 size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
1025 {return m_owner.AttachedTransformation()->ChannelPutModifiable2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking);}
1026 bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true)
1027 {return m_passSignal ? m_owner.AttachedTransformation()->ChannelFlush(channel, completeFlush, propagation, blocking) : false;}
1028 bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true)
1029 {return m_passSignal ? m_owner.AttachedTransformation()->ChannelMessageSeriesEnd(channel, propagation, blocking) : false;}
1030
1031 private:
1032 BufferedTransformation &m_owner;
1033 bool m_passSignal;
1034 };
1035
1036 /// \brief Base class for Filter classes that are proxies for a chain of other filters
1037 /// \since Crypto++ 4.0
1038 class CRYPTOPP_DLL ProxyFilter : public FilterWithBufferedInput
1039 {
1040 public:
~ProxyFilter()1041 virtual ~ProxyFilter() {}
1042
1043 /// \brief Construct a ProxyFilter
1044 /// \param filter an output filter
1045 /// \param firstSize the first Put size
1046 /// \param lastSize the last Put size
1047 /// \param attachment an attached transformation
1048 ProxyFilter(BufferedTransformation *filter, size_t firstSize, size_t lastSize, BufferedTransformation *attachment);
1049
1050 bool IsolatedFlush(bool hardFlush, bool blocking);
1051
1052 /// \brief Sets the OutputProxy filter
1053 /// \param filter an OutputProxy filter
1054 void SetFilter(Filter *filter);
1055 void NextPutMultiple(const byte *s, size_t len);
1056 void NextPutModifiable(byte *inString, size_t length);
1057
1058 protected:
1059 member_ptr<BufferedTransformation> m_filter;
1060 };
1061
1062 /// \brief Proxy filter that doesn't modify the underlying filter's input or output
1063 /// \since Crypto++ 5.0
1064 class CRYPTOPP_DLL SimpleProxyFilter : public ProxyFilter
1065 {
1066 public:
1067 /// \brief Construct a SimpleProxyFilter
1068 /// \param filter an output filter
1069 /// \param attachment an attached transformation
SimpleProxyFilter(BufferedTransformation * filter,BufferedTransformation * attachment)1070 SimpleProxyFilter(BufferedTransformation *filter, BufferedTransformation *attachment)
1071 : ProxyFilter(filter, 0, 0, attachment) {}
1072
FirstPut(const byte * inString)1073 void FirstPut(const byte * inString)
1074 {CRYPTOPP_UNUSED(inString);}
1075
1076 /// \brief Input the last block of data
1077 /// \param inString the input byte buffer
1078 /// \param length the size of the input buffer, in bytes
1079 /// \details LastPut() processes the last block of data and signals attached filters to do the same.
1080 /// LastPut() is always called. The pseudo algorithm for the logic is:
1081 /// <pre>
1082 /// if totalLength < firstSize then length == totalLength
1083 /// else if totalLength <= firstSize+lastSize then length == totalLength-firstSize
1084 /// else lastSize <= length < lastSize+blockSize
1085 /// </pre>
LastPut(const byte * inString,size_t length)1086 void LastPut(const byte *inString, size_t length)
1087 {CRYPTOPP_UNUSED(inString), CRYPTOPP_UNUSED(length); m_filter->MessageEnd();}
1088 };
1089
1090 /// \brief Filter wrapper for PK_Encryptor
1091 /// \details PK_DecryptorFilter is a proxy for the filter created by PK_Encryptor::CreateEncryptionFilter.
1092 /// This class provides symmetry with VerifierFilter.
1093 /// \since Crypto++ 5.0
1094 class CRYPTOPP_DLL PK_EncryptorFilter : public SimpleProxyFilter
1095 {
1096 public:
1097 /// \brief Construct a PK_EncryptorFilter
1098 /// \param rng a RandomNumberGenerator derived class
1099 /// \param encryptor a PK_Encryptor derived class
1100 /// \param attachment an optional attached transformation
1101 PK_EncryptorFilter(RandomNumberGenerator &rng, const PK_Encryptor &encryptor, BufferedTransformation *attachment = NULLPTR)
1102 : SimpleProxyFilter(encryptor.CreateEncryptionFilter(rng), attachment) {}
1103 };
1104
1105 /// \brief Filter wrapper for PK_Decryptor
1106 /// \details PK_DecryptorFilter is a proxy for the filter created by PK_Decryptor::CreateDecryptionFilter.
1107 /// This class provides symmetry with SignerFilter.
1108 /// \since Crypto++ 5.0
1109 class CRYPTOPP_DLL PK_DecryptorFilter : public SimpleProxyFilter
1110 {
1111 public:
1112 /// \brief Construct a PK_DecryptorFilter
1113 /// \param rng a RandomNumberGenerator derived class
1114 /// \param decryptor a PK_Decryptor derived class
1115 /// \param attachment an optional attached transformation
1116 PK_DecryptorFilter(RandomNumberGenerator &rng, const PK_Decryptor &decryptor, BufferedTransformation *attachment = NULLPTR)
1117 : SimpleProxyFilter(decryptor.CreateDecryptionFilter(rng), attachment) {}
1118 };
1119
1120 /// \brief Append input to a string object
1121 /// \tparam T std::basic_string<char> type
1122 /// \details StringSinkTemplate is a StringSinkTemplate typedef
1123 /// \since Crypto++ 5.0
1124 template <class T>
1125 class StringSinkTemplate : public Bufferless<Sink>
1126 {
1127 public:
1128 typedef typename T::value_type value_type;
~StringSinkTemplate()1129 virtual ~StringSinkTemplate() {}
1130
1131 /// \brief Construct a StringSinkTemplate
1132 /// \param output std::basic_string<char> or std::vector<byte> type
StringSinkTemplate(T & output)1133 StringSinkTemplate(T &output)
1134 : m_output(&output) {CRYPTOPP_ASSERT(sizeof(value_type)==1);}
1135
IsolatedInitialize(const NameValuePairs & parameters)1136 void IsolatedInitialize(const NameValuePairs ¶meters)
1137 {if (!parameters.GetValue("OutputStringPointer", m_output)) throw InvalidArgument("StringSink: OutputStringPointer not specified");}
1138
Put2(const byte * inString,size_t length,int messageEnd,bool blocking)1139 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
1140 {
1141 CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking);
1142 if (length > 0)
1143 {
1144 typename T::size_type size = m_output->size();
1145 if (length < size && size + length > m_output->capacity())
1146 m_output->reserve(2*size);
1147 m_output->insert(m_output->end(), (const value_type *)inString, (const value_type *)inString+length);
1148 }
1149 return 0;
1150 }
1151
1152 private:
1153 T *m_output;
1154 };
1155
1156 /// \brief Append input to a string object
1157 /// \details StringSink is a typedef for StringSinkTemplate<std::string>.
1158 /// \sa ArraySink, ArrayXorSink
1159 /// \since Crypto++ 4.0
1160 DOCUMENTED_TYPEDEF(StringSinkTemplate<std::string>, StringSink);
1161 CRYPTOPP_DLL_TEMPLATE_CLASS StringSinkTemplate<std::string>;
1162
1163 /// \brief Append input to a std::vector<byte> object
1164 /// \details VectorSink is a typedef for StringSinkTemplate<std::vector<byte> >.
1165 /// \since Crypto++ 8.0
1166 DOCUMENTED_TYPEDEF(StringSinkTemplate<std::vector<byte> >, VectorSink);
1167 CRYPTOPP_DLL_TEMPLATE_CLASS StringSinkTemplate<std::vector<byte> >;
1168
1169 /// \brief Incorporates input into RNG as additional entropy
1170 /// \since Crypto++ 4.0
1171 class RandomNumberSink : public Bufferless<Sink>
1172 {
1173 public:
~RandomNumberSink()1174 virtual ~RandomNumberSink() {}
1175
1176 /// \brief Construct a RandomNumberSink
RandomNumberSink()1177 RandomNumberSink()
1178 : m_rng(NULLPTR) {}
1179
1180 /// \brief Construct a RandomNumberSink
1181 /// \param rng a RandomNumberGenerator derived class
RandomNumberSink(RandomNumberGenerator & rng)1182 RandomNumberSink(RandomNumberGenerator &rng)
1183 : m_rng(&rng) {}
1184
1185 void IsolatedInitialize(const NameValuePairs ¶meters);
1186 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
1187
1188 private:
1189 RandomNumberGenerator *m_rng;
1190 };
1191
1192 /// \brief Copy input to a memory buffer
1193 /// \details ArraySink wraps a fixed size buffer. The buffer is full once Put returns non-0.
1194 /// When used in a pipleline, ArraySink silently discards input if the buffer is full.
1195 /// AvailableSize() can be used to determine how much space remains in the buffer.
1196 /// TotalPutLength() can be used to determine how many bytes were processed.
1197 /// \sa StringSink, ArrayXorSink
1198 /// \since Crypto++ 4.0
1199 class CRYPTOPP_DLL ArraySink : public Bufferless<Sink>
1200 {
1201 public:
~ArraySink()1202 virtual ~ArraySink() {}
1203
1204 /// \brief Construct an ArraySink
1205 /// \param parameters a set of NameValuePairs to initialize this object
1206 /// \details Name::OutputBuffer() is a mandatory parameter using this constructor.
1207 ArraySink(const NameValuePairs ¶meters = g_nullNameValuePairs)
m_buf(NULLPTR)1208 : m_buf(NULLPTR), m_size(0), m_total(0) {IsolatedInitialize(parameters);}
1209
1210 /// \brief Construct an ArraySink
1211 /// \param buf pointer to a memory buffer
1212 /// \param size length of the memory buffer
ArraySink(byte * buf,size_t size)1213 ArraySink(byte *buf, size_t size)
1214 : m_buf(buf), m_size(size), m_total(0) {}
1215
1216 /// \brief Provides the size remaining in the Sink
1217 /// \return size remaining in the Sink, in bytes
AvailableSize()1218 size_t AvailableSize() {return SaturatingSubtract(m_size, m_total);}
1219
1220 /// \brief Provides the number of bytes written to the Sink
1221 /// \return number of bytes written to the Sink, in bytes
TotalPutLength()1222 lword TotalPutLength() {return m_total;}
1223
1224 void IsolatedInitialize(const NameValuePairs ¶meters);
1225 byte * CreatePutSpace(size_t &size);
1226 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
1227
1228 protected:
1229 byte *m_buf;
1230 size_t m_size;
1231 lword m_total;
1232 };
1233
1234 /// \brief Xor input to a memory buffer
1235 /// \details ArrayXorSink wraps a fixed size buffer. The buffer is full once Put returns non-0.
1236 /// When used in a pipleline, ArrayXorSink silently discards input if the buffer is full.
1237 /// AvailableSize() can be used to determine how much space remains in the buffer.
1238 /// TotalPutLength() can be used to determine how many bytes were processed.
1239 /// \sa StringSink, ArraySink
1240 /// \since Crypto++ 4.0
1241 class CRYPTOPP_DLL ArrayXorSink : public ArraySink
1242 {
1243 public:
~ArrayXorSink()1244 virtual ~ArrayXorSink() {}
1245
1246 /// \brief Construct an ArrayXorSink
1247 /// \param buf pointer to a memory buffer
1248 /// \param size length of the memory buffer
ArrayXorSink(byte * buf,size_t size)1249 ArrayXorSink(byte *buf, size_t size)
1250 : ArraySink(buf, size) {}
1251
1252 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
CreatePutSpace(size_t & size)1253 byte * CreatePutSpace(size_t &size) {return BufferedTransformation::CreatePutSpace(size);}
1254 };
1255
1256 /// \brief String-based implementation of Store interface
1257 /// \since Crypto++ 4.0
1258 class StringStore : public Store
1259 {
1260 public:
1261 /// \brief Construct a StringStore
1262 /// \param string pointer to a C-String
1263 StringStore(const char *string = NULLPTR)
1264 {StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
1265
1266 /// \brief Construct a StringStore
1267 /// \param string pointer to a memory buffer
1268 /// \param length size of the memory buffer
StringStore(const byte * string,size_t length)1269 StringStore(const byte *string, size_t length)
1270 {StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string, length)));}
1271
1272 /// \brief Construct a StringStore
1273 /// \tparam T std::basic_string<char> type
1274 /// \param string reference to a std::basic_string<char> type
StringStore(const T & string)1275 template <class T> StringStore(const T &string)
1276 {StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
1277
1278 CRYPTOPP_DLL size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
1279 CRYPTOPP_DLL size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
1280
1281 private:
1282 CRYPTOPP_DLL void StoreInitialize(const NameValuePairs ¶meters);
1283
1284 const byte *m_store;
1285 size_t m_length, m_count;
1286 };
1287
1288 /// \brief RNG-based implementation of Source interface
1289 /// \since Crypto++ 4.0
1290 class CRYPTOPP_DLL RandomNumberStore : public Store
1291 {
1292 public:
~RandomNumberStore()1293 virtual ~RandomNumberStore() {}
1294
RandomNumberStore()1295 RandomNumberStore()
1296 : m_rng(NULLPTR), m_length(0), m_count(0) {}
1297
RandomNumberStore(RandomNumberGenerator & rng,lword length)1298 RandomNumberStore(RandomNumberGenerator &rng, lword length)
1299 : m_rng(&rng), m_length(length), m_count(0) {}
1300
AnyRetrievable()1301 bool AnyRetrievable() const {return MaxRetrievable() != 0;}
MaxRetrievable()1302 lword MaxRetrievable() const {return m_length-m_count;}
1303
1304 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
1305 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
1306 {
1307 CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(begin); CRYPTOPP_UNUSED(end); CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking);
1308 throw NotImplemented("RandomNumberStore: CopyRangeTo2() is not supported by this store");
1309 }
1310
1311 private:
1312 void StoreInitialize(const NameValuePairs ¶meters);
1313
1314 RandomNumberGenerator *m_rng;
1315 lword m_length, m_count;
1316 };
1317
1318 /// \brief Empty store
1319 /// \since Crypto++ 5.0
1320 class CRYPTOPP_DLL NullStore : public Store
1321 {
1322 public:
m_size(size)1323 NullStore(lword size = ULONG_MAX) : m_size(size) {}
StoreInitialize(const NameValuePairs & parameters)1324 void StoreInitialize(const NameValuePairs ¶meters)
1325 {CRYPTOPP_UNUSED(parameters);}
MaxRetrievable()1326 lword MaxRetrievable() const {return m_size;}
1327 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
1328 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
1329
1330 private:
1331 lword m_size;
1332 };
1333
1334 /// \brief Implementation of BufferedTransformation's attachment interface
1335 /// \details Source is a cornerstone of the Pipeline trinitiy. Data flows from
1336 /// Sources, through Filters, and then terminates in Sinks. The difference
1337 /// between a Source and Filter is a Source \a pumps data, while a Filter does
1338 /// not. The difference between a Filter and a Sink is a Filter allows an
1339 /// attached transformation, while a Sink does not.
1340 /// \details See the discussion of BufferedTransformation in cryptlib.h for
1341 /// more details.
1342 /// \sa Store and SourceTemplate
1343 /// \since Crypto++ 1.0
1344 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Source : public InputRejecting<Filter>
1345 {
1346 public:
~Source()1347 virtual ~Source() {}
1348
1349 /// \brief Construct a Source
1350 /// \param attachment an optional attached transformation
1351 Source(BufferedTransformation *attachment = NULLPTR)
1352 {Source::Detach(attachment);}
1353
1354 /// \name PIPELINE
1355 //@{
1356
1357 /// \brief Pump data to attached transformation
1358 /// \param pumpMax the maximum number of bytes to pump
1359 /// \return the number of bytes that remain to be processed (i.e., bytes not processed).
1360 /// 0 indicates all bytes were processed.
1361 /// \details Internally, Pump() calls Pump2().
1362 /// \note pumpMax is a <tt>lword</tt>, which is a 64-bit value that typically uses
1363 /// <tt>LWORD_MAX</tt>. The default argument is <tt>SIZE_MAX</tt>, and it can be
1364 /// 32-bits or 64-bits.
1365 /// \sa Pump2, PumpAll, AnyRetrievable, MaxRetrievable
1366 lword Pump(lword pumpMax=SIZE_MAX)
1367 {Pump2(pumpMax); return pumpMax;}
1368
1369 /// \brief Pump messages to attached transformation
1370 /// \param count the maximum number of messages to pump
1371 /// \return TODO
1372 /// \details Internally, PumpMessages() calls PumpMessages2().
1373 unsigned int PumpMessages(unsigned int count=UINT_MAX)
1374 {PumpMessages2(count); return count;}
1375
1376 /// \brief Pump all data to attached transformation
1377 /// \details Pumps all data to the attached transformation and signal the end of the current
1378 /// message. To avoid the MessageEnd() signal call \ref Pump "Pump(LWORD_MAX)" or \ref Pump2
1379 /// "Pump2(LWORD_MAX, bool)".
1380 /// \details Internally, PumpAll() calls PumpAll2(), which calls PumpMessages().
1381 /// \sa Pump, Pump2, AnyRetrievable, MaxRetrievable
PumpAll()1382 void PumpAll()
1383 {PumpAll2();}
1384
1385 /// \brief Pump data to attached transformation
1386 /// \param byteCount the maximum number of bytes to pump
1387 /// \param blocking specifies whether the object should block when processing input
1388 /// \return the number of bytes that remain to be processed (i.e., bytes not processed).
1389 /// 0 indicates all bytes were processed.
1390 /// \details byteCount is an \a IN and \a OUT parameter. When the call is made, byteCount is the
1391 /// requested size of the pump. When the call returns, byteCount is the number of bytes that
1392 /// were pumped.
1393 /// \sa Pump, PumpAll, AnyRetrievable, MaxRetrievable
1394 virtual size_t Pump2(lword &byteCount, bool blocking=true) =0;
1395
1396 /// \brief Pump messages to attached transformation
1397 /// \param messageCount the maximum number of messages to pump
1398 /// \param blocking specifies whether the object should block when processing input
1399 /// \details messageCount is an IN and OUT parameter.
1400 virtual size_t PumpMessages2(unsigned int &messageCount, bool blocking=true) =0;
1401
1402 /// \brief Pump all data to attached transformation
1403 /// \param blocking specifies whether the object should block when processing input
1404 /// \return the number of bytes that remain to be processed (i.e., bytes not processed).
1405 /// 0 indicates all bytes were processed.
1406 /// \sa Pump, Pump2, AnyRetrievable, MaxRetrievable
1407 virtual size_t PumpAll2(bool blocking=true);
1408
1409 /// \brief Determines if the Source is exhausted
1410 /// \return true if the source has been exhausted
1411 virtual bool SourceExhausted() const =0;
1412
1413 //@}
1414
1415 protected:
SourceInitialize(bool pumpAll,const NameValuePairs & parameters)1416 void SourceInitialize(bool pumpAll, const NameValuePairs ¶meters)
1417 {
1418 IsolatedInitialize(parameters);
1419 if (pumpAll)
1420 PumpAll();
1421 }
1422 };
1423
1424 /// \brief Transform a Store into a Source
1425 /// \tparam T the class or type
1426 /// \since Crypto++ 5.0
1427 template <class T>
1428 class SourceTemplate : public Source
1429 {
1430 public:
~SourceTemplate()1431 virtual ~SourceTemplate() {}
1432
1433 /// \brief Construct a SourceTemplate
1434 /// \param attachment an attached transformation
SourceTemplate(BufferedTransformation * attachment)1435 SourceTemplate(BufferedTransformation *attachment)
1436 : Source(attachment) {}
IsolatedInitialize(const NameValuePairs & parameters)1437 void IsolatedInitialize(const NameValuePairs ¶meters)
1438 {m_store.IsolatedInitialize(parameters);}
1439 size_t Pump2(lword &byteCount, bool blocking=true)
1440 {return m_store.TransferTo2(*AttachedTransformation(), byteCount, DEFAULT_CHANNEL, blocking);}
1441 size_t PumpMessages2(unsigned int &messageCount, bool blocking=true)
1442 {return m_store.TransferMessagesTo2(*AttachedTransformation(), messageCount, DEFAULT_CHANNEL, blocking);}
1443 size_t PumpAll2(bool blocking=true)
1444 {return m_store.TransferAllTo2(*AttachedTransformation(), DEFAULT_CHANNEL, blocking);}
SourceExhausted()1445 bool SourceExhausted() const
1446 {return !m_store.AnyRetrievable() && !m_store.AnyMessages();}
SetAutoSignalPropagation(int propagation)1447 void SetAutoSignalPropagation(int propagation)
1448 {m_store.SetAutoSignalPropagation(propagation);}
GetAutoSignalPropagation()1449 int GetAutoSignalPropagation() const
1450 {return m_store.GetAutoSignalPropagation();}
1451
1452 protected:
1453 T m_store;
1454 };
1455
1456 /// \brief String-based implementation of the Source interface
1457 /// \since Crypto++ 4.0
1458 class CRYPTOPP_DLL StringSource : public SourceTemplate<StringStore>
1459 {
1460 public:
1461 /// \brief Construct a StringSource
1462 /// \param attachment an optional attached transformation
1463 StringSource(BufferedTransformation *attachment = NULLPTR)
1464 : SourceTemplate<StringStore>(attachment) {}
1465
1466 /// \brief Construct a StringSource
1467 /// \param string C-String
1468 /// \param pumpAll flag indicating if source data should be pumped to its attached transformation
1469 /// \param attachment an optional attached transformation
1470 StringSource(const char *string, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
1471 : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
1472
1473 /// \brief Construct a StringSource
1474 /// \param string binary byte array
1475 /// \param length size of the byte array
1476 /// \param pumpAll flag indicating if source data should be pumped to its attached transformation
1477 /// \param attachment an optional attached transformation
1478 StringSource(const byte *string, size_t length, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
1479 : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string, length)));}
1480
1481 /// \brief Construct a StringSource
1482 /// \param string std::string
1483 /// \param pumpAll flag indicating if source data should be pumped to its attached transformation
1484 /// \param attachment an optional attached transformation
1485 StringSource(const std::string &string, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
1486 : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
1487 };
1488
1489 /// \brief Pointer-based implementation of the Source interface
1490 /// \details ArraySource is a typedef for StringSource. Use the third constructor for an array source.
1491 /// The third constructor takes a pointer and length.
1492 /// \since Crypto++ 5.6.0
1493 DOCUMENTED_TYPEDEF(StringSource, ArraySource);
1494
1495 /// \brief std::vector-based implementation of the Source interface
1496 /// \since Crypto++ 8.0
1497 class CRYPTOPP_DLL VectorSource : public SourceTemplate<StringStore>
1498 {
1499 public:
1500 /// \brief Construct a VectorSource
1501 /// \param attachment an optional attached transformation
1502 VectorSource(BufferedTransformation *attachment = NULLPTR)
1503 : SourceTemplate<StringStore>(attachment) {}
1504
1505 /// \brief Construct a VectorSource
1506 /// \param vec vector of bytes
1507 /// \param pumpAll flag indicating if source data should be pumped to its attached transformation
1508 /// \param attachment an optional attached transformation
1509 VectorSource(const std::vector<byte> &vec, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
1510 : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(vec)));}
1511 };
1512
1513 /// \brief RNG-based implementation of Source interface
1514 /// \since Crypto++ 4.0
1515 class CRYPTOPP_DLL RandomNumberSource : public SourceTemplate<RandomNumberStore>
1516 {
1517 public:
1518 RandomNumberSource(RandomNumberGenerator &rng, int length, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
1519 : SourceTemplate<RandomNumberStore>(attachment)
1520 {SourceInitialize(pumpAll, MakeParameters("RandomNumberGeneratorPointer", &rng)("RandomNumberStoreSize", length));}
1521 };
1522
1523 NAMESPACE_END
1524
1525 #if CRYPTOPP_MSC_VERSION
1526 # pragma warning(pop)
1527 #endif
1528
1529 #endif
1530