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