Fabcoin Core  0.16.2
P2P Digital Currency
filters.cpp
Go to the documentation of this file.
1 // filters.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 #include "config.h"
5 
6 #if CRYPTOPP_MSC_VERSION
7 # pragma warning(disable: 4100 4189)
8 #endif
9 
10 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
11 # pragma GCC diagnostic ignored "-Wunused-value"
12 #endif
13 
14 #ifndef CRYPTOPP_IMPORTS
15 
16 #include "filters.h"
17 #include "mqueue.h"
18 #include "fltrimpl.h"
19 #include "argnames.h"
20 #include "smartptr.h"
21 #include "stdcpp.h"
22 #include "misc.h"
23 
25 
27  : m_attachment(attachment), m_inputPosition(0), m_continueAt(0)
28 {
29 }
30 
32 {
33  return new MessageQueue;
34 }
35 
37 {
38  if (m_attachment.get() == NULL)
40  return m_attachment.get();
41 }
42 
44 {
45  if (m_attachment.get() == NULL)
46  const_cast<Filter *>(this)->m_attachment.reset(NewDefaultAttachment());
47  return m_attachment.get();
48 }
49 
51 {
52  m_attachment.reset(newOut);
53 }
54 
55 void Filter::Insert(Filter *filter)
56 {
58  m_attachment.reset(filter);
59 }
60 
61 size_t Filter::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
62 {
63  return AttachedTransformation()->CopyRangeTo2(target, begin, end, channel, blocking);
64 }
65 
66 size_t Filter::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
67 {
68  return AttachedTransformation()->TransferTo2(target, transferBytes, channel, blocking);
69 }
70 
71 void Filter::Initialize(const NameValuePairs &parameters, int propagation)
72 {
74  IsolatedInitialize(parameters);
75  PropagateInitialize(parameters, propagation);
76 }
77 
78 bool Filter::Flush(bool hardFlush, int propagation, bool blocking)
79 {
80  switch (m_continueAt)
81  {
82  case 0:
83  if (IsolatedFlush(hardFlush, blocking))
84  return true;
85  // fall through
86  case 1:
87  if (OutputFlush(1, hardFlush, propagation, blocking))
88  return true;
89  // fall through
90  default: ;;
91  }
92  return false;
93 }
94 
95 bool Filter::MessageSeriesEnd(int propagation, bool blocking)
96 {
97  switch (m_continueAt)
98  {
99  case 0:
100  if (IsolatedMessageSeriesEnd(blocking))
101  return true;
102  // fall through
103  case 1:
104  if (ShouldPropagateMessageSeriesEnd() && OutputMessageSeriesEnd(1, propagation, blocking))
105  return true;
106  // fall through
107  default: ;;
108  }
109  return false;
110 }
111 
113 {
114  if (propagation)
115  AttachedTransformation()->Initialize(parameters, propagation-1);
116 }
117 
118 size_t Filter::OutputModifiable(int outputSite, byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel)
119 {
120  if (messageEnd)
121  messageEnd--;
122  size_t result = AttachedTransformation()->ChannelPutModifiable2(channel, inString, length, messageEnd, blocking);
123  m_continueAt = result ? outputSite : 0;
124  return result;
125 }
126 
127 size_t Filter::Output(int outputSite, const byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel)
128 {
129  if (messageEnd)
130  messageEnd--;
131  size_t result = AttachedTransformation()->ChannelPut2(channel, inString, length, messageEnd, blocking);
132  m_continueAt = result ? outputSite : 0;
133  return result;
134 }
135 
136 bool Filter::OutputFlush(int outputSite, bool hardFlush, int propagation, bool blocking, const std::string &channel)
137 {
138  if (propagation && AttachedTransformation()->ChannelFlush(channel, hardFlush, propagation-1, blocking))
139  {
140  m_continueAt = outputSite;
141  return true;
142  }
143  m_continueAt = 0;
144  return false;
145 }
146 
147 bool Filter::OutputMessageSeriesEnd(int outputSite, int propagation, bool blocking, const std::string &channel)
148 {
149  if (propagation && AttachedTransformation()->ChannelMessageSeriesEnd(channel, propagation-1, blocking))
150  {
151  m_continueAt = outputSite;
152  return true;
153  }
154  m_continueAt = 0;
155  return false;
156 }
157 
158 // *************************************************************
159 
161 {
162  m_currentMessageBytes = m_totalBytes = m_currentSeriesMessages = m_totalMessages = m_totalMessageSeries = 0;
163  m_rangesToSkip.clear();
164 }
165 
166 void MeterFilter::AddRangeToSkip(unsigned int message, lword position, lword size, bool sortNow)
167 {
168  MessageRange r = {message, position, size};
169  m_rangesToSkip.push_back(r);
170  if (sortNow)
171  std::sort(m_rangesToSkip.begin(), m_rangesToSkip.end());
172 }
173 
174 size_t MeterFilter::PutMaybeModifiable(byte *begin, size_t length, int messageEnd, bool blocking, bool modifiable)
175 {
176  if (!m_transparent)
177  return 0;
178 
179  size_t t;
180  FILTER_BEGIN;
181 
182  m_begin = begin;
183  m_length = length;
184 
185  while (m_length > 0 || messageEnd)
186  {
187  if (m_length > 0 && !m_rangesToSkip.empty() && m_rangesToSkip.front().message == m_totalMessages && m_currentMessageBytes + m_length > m_rangesToSkip.front().position)
188  {
189  FILTER_OUTPUT_MAYBE_MODIFIABLE(1, m_begin, t = (size_t)SaturatingSubtract(m_rangesToSkip.front().position, m_currentMessageBytes), false, modifiable);
190 
191  CRYPTOPP_ASSERT(t < m_length);
192  m_begin += t;
193  m_length -= t;
194  m_currentMessageBytes += t;
195  m_totalBytes += t;
196 
197  if (m_currentMessageBytes + m_length < m_rangesToSkip.front().position + m_rangesToSkip.front().size)
198  t = m_length;
199  else
200  {
201  t = (size_t)SaturatingSubtract(m_rangesToSkip.front().position + m_rangesToSkip.front().size, m_currentMessageBytes);
202  CRYPTOPP_ASSERT(t <= m_length);
203  m_rangesToSkip.pop_front();
204  }
205 
206  m_begin += t;
207  m_length -= t;
208  m_currentMessageBytes += t;
209  m_totalBytes += t;
210  }
211  else
212  {
213  FILTER_OUTPUT_MAYBE_MODIFIABLE(2, m_begin, m_length, messageEnd, modifiable);
214 
215  m_currentMessageBytes += m_length;
216  m_totalBytes += m_length;
217  m_length = 0;
218 
219  if (messageEnd)
220  {
221  m_currentMessageBytes = 0;
222  m_currentSeriesMessages++;
223  m_totalMessages++;
224  messageEnd = false;
225  }
226  }
227  }
228 
230 }
231 
232 size_t MeterFilter::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
233 {
234  return PutMaybeModifiable(const_cast<byte *>(begin), length, messageEnd, blocking, false);
235 }
236 
237 size_t MeterFilter::PutModifiable2(byte *begin, size_t length, int messageEnd, bool blocking)
238 {
239  return PutMaybeModifiable(begin, length, messageEnd, blocking, true);
240 }
241 
243 {
244  CRYPTOPP_UNUSED(blocking);
245  m_currentMessageBytes = 0;
246  m_currentSeriesMessages = 0;
247  m_totalMessageSeries++;
248  return false;
249 }
250 
251 // *************************************************************
252 
253 void FilterWithBufferedInput::BlockQueue::ResetQueue(size_t blockSize, size_t maxBlocks)
254 {
255  m_buffer.New(blockSize * maxBlocks);
256  m_blockSize = blockSize;
257  m_maxBlocks = maxBlocks;
258  m_size = 0;
259  m_begin = m_buffer;
260 }
261 
263 {
264  if (m_size >= m_blockSize)
265  {
266  byte *ptr = m_begin;
267  if ((m_begin+=m_blockSize) == m_buffer.end())
268  m_begin = m_buffer;
269  m_size -= m_blockSize;
270  return ptr;
271  }
272  else
273  return NULL;
274 }
275 
277 {
278  numberOfBytes = STDMIN(numberOfBytes, STDMIN(size_t(m_buffer.end()-m_begin), m_size));
279  byte *ptr = m_begin;
280  m_begin += numberOfBytes;
281  m_size -= numberOfBytes;
282  if (m_size == 0 || m_begin == m_buffer.end())
283  m_begin = m_buffer;
284  return ptr;
285 }
286 
288 {
289  // Avoid passing NULL pointer to memcpy
290  if (!outString) return 0;
291 
292  size_t size = m_size;
293  size_t numberOfBytes = m_maxBlocks*m_blockSize;
294  const byte *ptr = GetContigousBlocks(numberOfBytes);
295  memcpy(outString, ptr, numberOfBytes);
296  memcpy(outString+numberOfBytes, m_begin, m_size);
297  m_size = 0;
298  return size;
299 }
300 
301 void FilterWithBufferedInput::BlockQueue::Put(const byte *inString, size_t length)
302 {
303  // Avoid passing NULL pointer to memcpy
304  if (!inString || !length) return;
305 
306  CRYPTOPP_ASSERT(m_size + length <= m_buffer.size());
307  byte *end = (m_size < size_t(m_buffer.end()-m_begin)) ? m_begin + m_size : m_begin + m_size - m_buffer.size();
308  size_t len = STDMIN(length, size_t(m_buffer.end()-end));
309  memcpy(end, inString, len);
310  if (len < length)
311  memcpy(m_buffer, inString+len, length-len);
312  m_size += length;
313 }
314 
316  : Filter(attachment), m_firstSize(SIZE_MAX), m_blockSize(0), m_lastSize(SIZE_MAX), m_firstInputDone(false)
317 {
318 }
319 
320 FilterWithBufferedInput::FilterWithBufferedInput(size_t firstSize, size_t blockSize, size_t lastSize, BufferedTransformation *attachment)
321  : Filter(attachment), m_firstSize(firstSize), m_blockSize(blockSize), m_lastSize(lastSize), m_firstInputDone(false)
322 {
323  if (m_firstSize == SIZE_MAX || m_blockSize < 1 || m_lastSize == SIZE_MAX)
324  throw InvalidArgument("FilterWithBufferedInput: invalid buffer size");
325 
327 }
328 
330 {
332  if (m_firstSize == SIZE_MAX || m_blockSize < 1 || m_lastSize == SIZE_MAX)
333  throw InvalidArgument("FilterWithBufferedInput: invalid buffer size");
335  m_firstInputDone = false;
336 }
337 
338 bool FilterWithBufferedInput::IsolatedFlush(bool hardFlush, bool blocking)
339 {
340  if (!blocking)
341  throw BlockingInputOnly("FilterWithBufferedInput");
342 
343  if (hardFlush)
344  ForceNextPut();
345  FlushDerived();
346 
347  return false;
348 }
349 
350 size_t FilterWithBufferedInput::PutMaybeModifiable(byte *inString, size_t length, int messageEnd, bool blocking, bool modifiable)
351 {
352  if (!blocking)
353  throw BlockingInputOnly("FilterWithBufferedInput");
354 
355  if (length != 0)
356  {
357  size_t newLength = m_queue.CurrentSize() + length;
358 
359  if (!m_firstInputDone && newLength >= m_firstSize)
360  {
361  size_t len = m_firstSize - m_queue.CurrentSize();
362  m_queue.Put(inString, len);
366 
367  inString += len;
368  newLength -= m_firstSize;
369  m_firstInputDone = true;
370  }
371 
372  if (m_firstInputDone)
373  {
374  if (m_blockSize == 1)
375  {
376  while (newLength > m_lastSize && m_queue.CurrentSize() > 0)
377  {
378  size_t len = newLength - m_lastSize;
379  byte *ptr = m_queue.GetContigousBlocks(len);
380  NextPutModifiable(ptr, len);
381  newLength -= len;
382  }
383 
384  if (newLength > m_lastSize)
385  {
386  size_t len = newLength - m_lastSize;
387  NextPutMaybeModifiable(inString, len, modifiable);
388  inString += len;
389  newLength -= len;
390  }
391  }
392  else
393  {
394  while (newLength >= m_blockSize + m_lastSize && m_queue.CurrentSize() >= m_blockSize)
395  {
397  newLength -= m_blockSize;
398  }
399 
400  if (newLength >= m_blockSize + m_lastSize && m_queue.CurrentSize() > 0)
401  {
403  size_t len = m_blockSize - m_queue.CurrentSize();
404  m_queue.Put(inString, len);
405  inString += len;
407  newLength -= m_blockSize;
408  }
409 
410  if (newLength >= m_blockSize + m_lastSize)
411  {
412  size_t len = RoundDownToMultipleOf(newLength - m_lastSize, m_blockSize);
413  NextPutMaybeModifiable(inString, len, modifiable);
414  inString += len;
415  newLength -= len;
416  }
417  }
418  }
419 
420  m_queue.Put(inString, newLength - m_queue.CurrentSize());
421  }
422 
423  if (messageEnd)
424  {
425  if (!m_firstInputDone && m_firstSize==0)
426  FirstPut(NULL);
427 
429  m_queue.GetAll(temp);
430  LastPut(temp, temp.size());
431 
432  m_firstInputDone = false;
434 
435  // Cast to void to suppress Coverity finding
436  (void)Output(1, NULL, 0, messageEnd, blocking);
437  }
438  return 0;
439 }
440 
442 {
443  if (!m_firstInputDone)
444  return;
445 
446  if (m_blockSize > 1)
447  {
448  while (m_queue.CurrentSize() >= m_blockSize)
450  }
451  else
452  {
453  size_t len;
454  while ((len = m_queue.CurrentSize()) > 0)
456  }
457 }
458 
459 void FilterWithBufferedInput::NextPutMultiple(const byte *inString, size_t length)
460 {
461  CRYPTOPP_ASSERT(m_blockSize > 1); // m_blockSize = 1 should always override this function
462  while (length > 0)
463  {
464  CRYPTOPP_ASSERT(length >= m_blockSize);
465  NextPutSingle(inString);
466  inString += m_blockSize;
467  length -= m_blockSize;
468  }
469 }
470 
471 // *************************************************************
472 
473 void Redirector::Initialize(const NameValuePairs &parameters, int propagation)
474 {
475  m_target = parameters.GetValueWithDefault("RedirectionTargetPointer", (BufferedTransformation*)NULL);
476  m_behavior = parameters.GetIntValueWithDefault("RedirectionBehavior", PASS_EVERYTHING);
477 
478  if (m_target && GetPassSignals())
479  m_target->Initialize(parameters, propagation);
480 }
481 
482 // *************************************************************
483 
484 ProxyFilter::ProxyFilter(BufferedTransformation *filter, size_t firstSize, size_t lastSize, BufferedTransformation *attachment)
485  : FilterWithBufferedInput(firstSize, 1, lastSize, attachment), m_filter(filter)
486 {
487  if (m_filter.get())
488  m_filter->Attach(new OutputProxy(*this, false));
489 }
490 
491 bool ProxyFilter::IsolatedFlush(bool hardFlush, bool blocking)
492 {
493  return m_filter.get() ? m_filter->Flush(hardFlush, -1, blocking) : false;
494 }
495 
497 {
498  m_filter.reset(filter);
499  if (filter)
500  {
501  OutputProxy *proxy;
502  member_ptr<OutputProxy> temp(proxy = new OutputProxy(*this, false));
503  m_filter->TransferAllTo(*proxy);
504  m_filter->Attach(temp.release());
505  }
506 }
507 
508 void ProxyFilter::NextPutMultiple(const byte *s, size_t len)
509 {
510  if (m_filter.get())
511  m_filter->Put(s, len);
512 }
513 
515 {
516  if (m_filter.get())
517  m_filter->PutModifiable(s, len);
518 }
519 
520 // *************************************************************
521 
523 {
524  parameters.GetRequiredParameter("RandomNumberSink", "RandomNumberGeneratorPointer", m_rng);
525 }
526 
527 size_t RandomNumberSink::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
528 {
529  CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking);
530  m_rng->IncorporateEntropy(begin, length);
531  return 0;
532 }
533 
534 size_t ArraySink::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
535 {
536  CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking);
537 
538  // Avoid passing NULL pointer to memcpy. Using memmove due to
539  // Valgrind finding on overlapping buffers.
540  size_t copied = 0;
541  if (m_buf && begin)
542  {
543  copied = STDMIN(length, SaturatingSubtract(m_size, m_total));
544  memmove(m_buf+m_total, begin, copied);
545  }
546  m_total += copied;
547  return length - copied;
548 }
549 
551 {
552  size = SaturatingSubtract(m_size, m_total);
553  return m_buf + m_total;
554 }
555 
557 {
558  ByteArrayParameter array;
559  if (!parameters.GetValue(Name::OutputBuffer(), array))
560  throw InvalidArgument("ArraySink: missing OutputBuffer argument");
561  m_buf = array.begin();
562  m_size = array.size();
563 }
564 
565 size_t ArrayXorSink::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
566 {
567  CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking);
568 
569  // Avoid passing NULL pointer to xorbuf
570  size_t copied = 0;
571  if (m_buf && begin)
572  {
573  copied = STDMIN(length, SaturatingSubtract(m_size, m_total));
574  xorbuf(m_buf+m_total, begin, copied);
575  }
576  m_total += copied;
577  return length - copied;
578 }
579 
580 // *************************************************************
581 
582 StreamTransformationFilter::StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment, BlockPaddingScheme padding, bool allowAuthenticatedSymmetricCipher)
583  : FilterWithBufferedInput(attachment)
584  , m_cipher(c), m_padding(DEFAULT_PADDING), m_optimalBufferSize(0)
585 {
587 
588  if (!allowAuthenticatedSymmetricCipher && dynamic_cast<AuthenticatedSymmetricCipher *>(&c) != 0)
589  throw InvalidArgument("StreamTransformationFilter: please use AuthenticatedEncryptionFilter and AuthenticatedDecryptionFilter for AuthenticatedSymmetricCipher");
590 
591  IsolatedInitialize(MakeParameters(Name::BlockPaddingScheme(), padding));
592 }
593 
595 {
596  if (c.MinLastBlockSize() > 0)
597  return c.MinLastBlockSize();
598  else if (c.MandatoryBlockSize() > 1 && !c.IsForwardTransformation() && padding != NO_PADDING && padding != ZEROS_PADDING)
599  return c.MandatoryBlockSize();
600  else
601  return 0;
602 }
603 
604 void StreamTransformationFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
605 {
606  BlockPaddingScheme padding = parameters.GetValueWithDefault(Name::BlockPaddingScheme(), DEFAULT_PADDING);
607  bool isBlockCipher = (m_cipher.MandatoryBlockSize() > 1 && m_cipher.MinLastBlockSize() == 0);
608 
609  if (padding == DEFAULT_PADDING)
610  m_padding = isBlockCipher ? PKCS_PADDING : NO_PADDING;
611  else
612  m_padding = padding;
613 
614  if (!isBlockCipher && (m_padding == PKCS_PADDING || m_padding == ONE_AND_ZEROS_PADDING))
615  throw InvalidArgument("StreamTransformationFilter: PKCS_PADDING and ONE_AND_ZEROS_PADDING cannot be used with " + m_cipher.AlgorithmName());
616 
617  firstSize = 0;
618  blockSize = m_cipher.MandatoryBlockSize();
619  lastSize = LastBlockSize(m_cipher, m_padding);
620 }
621 
623 {
624  CRYPTOPP_UNUSED(inString);
627 }
628 
629 void StreamTransformationFilter::NextPutMultiple(const byte *inString, size_t length)
630 {
631  if (!length)
632  return;
633 
634  size_t s = m_cipher.MandatoryBlockSize();
635 
636  do
637  {
638  size_t len = m_optimalBufferSize;
639  byte *space = HelpCreatePutSpace(*AttachedTransformation(), DEFAULT_CHANNEL, s, length, len);
640  if (len < length)
641  {
642  if (len == m_optimalBufferSize)
644  len = RoundDownToMultipleOf(len, s);
645  }
646  else
647  len = length;
648  m_cipher.ProcessString(space, inString, len);
649  AttachedTransformation()->PutModifiable(space, len);
650  inString += len;
651  length -= len;
652  }
653  while (length > 0);
654 }
655 
657 {
658  m_cipher.ProcessString(inString, length);
659  AttachedTransformation()->PutModifiable(inString, length);
660 }
661 
662 void StreamTransformationFilter::LastPut(const byte *inString, size_t length)
663 {
664  byte *space = NULL;
665 
666  switch (m_padding)
667  {
668  case NO_PADDING:
669  case ZEROS_PADDING:
670  if (length > 0)
671  {
672  size_t minLastBlockSize = m_cipher.MinLastBlockSize();
673  bool isForwardTransformation = m_cipher.IsForwardTransformation();
674 
675  if (isForwardTransformation && m_padding == ZEROS_PADDING && (minLastBlockSize == 0 || length < minLastBlockSize))
676  {
677  // do padding
678  size_t blockSize = STDMAX(minLastBlockSize, (size_t)m_cipher.MandatoryBlockSize());
680  if (inString) {memcpy(space, inString, length);}
681  memset(space + length, 0, blockSize - length);
682  m_cipher.ProcessLastBlock(space, space, blockSize);
683  AttachedTransformation()->Put(space, blockSize);
684  }
685  else
686  {
687  if (minLastBlockSize == 0)
688  {
689  if (isForwardTransformation)
690  throw InvalidDataFormat("StreamTransformationFilter: plaintext length is not a multiple of block size and NO_PADDING is specified");
691  else
692  throw InvalidCiphertext("StreamTransformationFilter: ciphertext length is not a multiple of block size");
693  }
694 
696  m_cipher.ProcessLastBlock(space, inString, length);
697  AttachedTransformation()->Put(space, length);
698  }
699  }
700  break;
701 
702  case PKCS_PADDING:
704  unsigned int s;
706  CRYPTOPP_ASSERT(s > 1);
709  {
710  CRYPTOPP_ASSERT(length < s);
711  if (inString) {memcpy(space, inString, length);}
712  if (m_padding == PKCS_PADDING)
713  {
714  CRYPTOPP_ASSERT(s < 256);
715  byte pad = byte(s-length);
716  memset(space+length, pad, s-length);
717  }
718  else
719  {
720  space[length] = 0x80;
721  memset(space+length+1, 0, s-length-1);
722  }
723  m_cipher.ProcessData(space, space, s);
724  AttachedTransformation()->Put(space, s);
725  }
726  else
727  {
728  if (length != s)
729  throw InvalidCiphertext("StreamTransformationFilter: ciphertext length is not a multiple of block size");
730  m_cipher.ProcessData(space, inString, s);
731  if (m_padding == PKCS_PADDING)
732  {
733  byte pad = space[s-1];
734  if (pad < 1 || pad > s || std::find_if(space+s-pad, space+s, std::bind2nd(std::not_equal_to<byte>(), pad)) != space+s)
735  throw InvalidCiphertext("StreamTransformationFilter: invalid PKCS #7 block padding found");
736  length = s-pad;
737  }
738  else
739  {
740  while (length > 1 && space[length-1] == 0)
741  --length;
742  if (space[--length] != 0x80)
743  throw InvalidCiphertext("StreamTransformationFilter: invalid ones-and-zeros padding found");
744  }
745  AttachedTransformation()->Put(space, length);
746  }
747  break;
748 
749  default:
750  CRYPTOPP_ASSERT(false);
751  }
752 }
753 
754 // *************************************************************
755 
756 HashFilter::HashFilter(HashTransformation &hm, BufferedTransformation *attachment, bool putMessage, int truncatedDigestSize, const std::string &messagePutChannel, const std::string &hashPutChannel)
757  : m_hashModule(hm), m_putMessage(putMessage), m_digestSize(0), m_space(NULL)
758  , m_messagePutChannel(messagePutChannel), m_hashPutChannel(hashPutChannel)
759 {
760  m_digestSize = truncatedDigestSize < 0 ? m_hashModule.DigestSize() : truncatedDigestSize;
761  Detach(attachment);
762 }
763 
765 {
766  m_putMessage = parameters.GetValueWithDefault(Name::PutMessage(), false);
767  int s = parameters.GetIntValueWithDefault(Name::TruncatedDigestSize(), -1);
768  m_digestSize = s < 0 ? m_hashModule.DigestSize() : s;
769 }
770 
771 size_t HashFilter::Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
772 {
773  FILTER_BEGIN;
774  if (m_putMessage)
775  FILTER_OUTPUT3(1, 0, inString, length, 0, m_messagePutChannel);
776  if (inString && length)
777  m_hashModule.Update(inString, length);
778  if (messageEnd)
779  {
780  {
781  size_t size;
784  }
786  }
788 }
789 
790 // *************************************************************
791 
793  : FilterWithBufferedInput(attachment)
794  , m_hashModule(hm), m_flags(0), m_digestSize(0), m_verified(false)
795 {
796  IsolatedInitialize(MakeParameters(Name::HashVerificationFilterFlags(), flags)(Name::TruncatedDigestSize(), truncatedDigestSize));
797 }
798 
799 void HashVerificationFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
800 {
801  m_flags = parameters.GetValueWithDefault(Name::HashVerificationFilterFlags(), (word32)DEFAULT_FLAGS);
802  int s = parameters.GetIntValueWithDefault(Name::TruncatedDigestSize(), -1);
803  m_digestSize = s < 0 ? m_hashModule.DigestSize() : s;
804  m_verified = false;
805  firstSize = m_flags & HASH_AT_BEGIN ? m_digestSize : 0;
806  blockSize = 1;
807  lastSize = m_flags & HASH_AT_BEGIN ? 0 : m_digestSize;
808 }
809 
811 {
812  if (m_flags & HASH_AT_BEGIN)
813  {
815  if (inString) {memcpy(m_expectedHash, inString, m_expectedHash.size());}
816  if (m_flags & PUT_HASH)
818  }
819 }
820 
821 void HashVerificationFilter::NextPutMultiple(const byte *inString, size_t length)
822 {
823  m_hashModule.Update(inString, length);
824  if (m_flags & PUT_MESSAGE)
825  AttachedTransformation()->Put(inString, length);
826 }
827 
828 void HashVerificationFilter::LastPut(const byte *inString, size_t length)
829 {
830  if (m_flags & HASH_AT_BEGIN)
831  {
832  CRYPTOPP_ASSERT(length == 0);
834  }
835  else
836  {
837  m_verified = (length==m_digestSize && m_hashModule.TruncatedVerify(inString, length));
838  if (m_flags & PUT_HASH)
839  AttachedTransformation()->Put(inString, length);
840  }
841 
842  if (m_flags & PUT_RESULT)
844 
845  if ((m_flags & THROW_EXCEPTION) && !m_verified)
846  throw HashVerificationFailed();
847 }
848 
849 // *************************************************************
850 
852  bool putAAD, int truncatedDigestSize, const std::string &macChannel, BlockPaddingScheme padding)
853  : StreamTransformationFilter(c, attachment, padding, true)
854  , m_hf(c, new OutputProxy(*this, false), putAAD, truncatedDigestSize, AAD_CHANNEL, macChannel)
855 {
857 }
858 
860 {
861  m_hf.IsolatedInitialize(parameters);
863 }
864 
865 byte * AuthenticatedEncryptionFilter::ChannelCreatePutSpace(const std::string &channel, size_t &size)
866 {
867  if (channel.empty())
869 
870  if (channel == AAD_CHANNEL)
871  return m_hf.CreatePutSpace(size);
872 
873  throw InvalidChannelName("AuthenticatedEncryptionFilter", channel);
874 }
875 
876 size_t AuthenticatedEncryptionFilter::ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
877 {
878  if (channel.empty())
879  return StreamTransformationFilter::Put2(begin, length, messageEnd, blocking);
880 
881  if (channel == AAD_CHANNEL)
882  return m_hf.Put2(begin, length, 0, blocking);
883 
884  throw InvalidChannelName("AuthenticatedEncryptionFilter", channel);
885 }
886 
887 void AuthenticatedEncryptionFilter::LastPut(const byte *inString, size_t length)
888 {
889  StreamTransformationFilter::LastPut(inString, length);
890  m_hf.MessageEnd();
891 }
892 
893 // *************************************************************
894 
896  : FilterWithBufferedInput(attachment)
897  , m_hashVerifier(c, new OutputProxy(*this, false))
898  , m_streamFilter(c, new OutputProxy(*this, false), padding, true)
899 {
901  IsolatedInitialize(MakeParameters(Name::BlockPaddingScheme(), padding)(Name::AuthenticatedDecryptionFilterFlags(), flags)(Name::TruncatedDigestSize(), truncatedDigestSize));
902 }
903 
904 void AuthenticatedDecryptionFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
905 {
906  word32 flags = parameters.GetValueWithDefault(Name::AuthenticatedDecryptionFilterFlags(), (word32)DEFAULT_FLAGS);
907 
908  m_hashVerifier.Initialize(CombinedNameValuePairs(parameters, MakeParameters(Name::HashVerificationFilterFlags(), flags)));
909  m_streamFilter.Initialize(parameters);
910 
911  firstSize = m_hashVerifier.m_firstSize;
912  blockSize = 1;
913  lastSize = m_hashVerifier.m_lastSize;
914 }
915 
916 byte * AuthenticatedDecryptionFilter::ChannelCreatePutSpace(const std::string &channel, size_t &size)
917 {
918  if (channel.empty())
919  return m_streamFilter.CreatePutSpace(size);
920 
921  if (channel == AAD_CHANNEL)
922  return m_hashVerifier.CreatePutSpace(size);
923 
924  throw InvalidChannelName("AuthenticatedDecryptionFilter", channel);
925 }
926 
927 size_t AuthenticatedDecryptionFilter::ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
928 {
929  if (channel.empty())
930  {
931  if (m_lastSize > 0)
933  return FilterWithBufferedInput::Put2(begin, length, messageEnd, blocking);
934  }
935 
936  if (channel == AAD_CHANNEL)
937  return m_hashVerifier.Put2(begin, length, 0, blocking);
938 
939  throw InvalidChannelName("AuthenticatedDecryptionFilter", channel);
940 }
941 
943 {
944  m_hashVerifier.Put(inString, m_firstSize);
945 }
946 
947 void AuthenticatedDecryptionFilter::NextPutMultiple(const byte *inString, size_t length)
948 {
949  m_streamFilter.Put(inString, length);
950 }
951 
952 void AuthenticatedDecryptionFilter::LastPut(const byte *inString, size_t length)
953 {
955  m_hashVerifier.PutMessageEnd(inString, length);
956 }
957 
958 // *************************************************************
959 
961 {
962  m_putMessage = parameters.GetValueWithDefault(Name::PutMessage(), false);
963  m_messageAccumulator.reset(m_signer.NewSignatureAccumulator(m_rng));
964 }
965 
966 size_t SignerFilter::Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
967 {
968  FILTER_BEGIN;
969  m_messageAccumulator->Update(inString, length);
970  if (m_putMessage)
971  FILTER_OUTPUT(1, inString, length, 0);
972  if (messageEnd)
973  {
974  m_buf.New(m_signer.SignatureLength());
975  m_signer.Sign(m_rng, m_messageAccumulator.release(), m_buf);
976  FILTER_OUTPUT(2, m_buf, m_buf.size(), messageEnd);
977  m_messageAccumulator.reset(m_signer.NewSignatureAccumulator(m_rng));
978  }
980 }
981 
983  : FilterWithBufferedInput(attachment)
984  , m_verifier(verifier), m_flags(0), m_verified(0)
985 {
986  IsolatedInitialize(MakeParameters(Name::SignatureVerificationFilterFlags(), flags));
987 }
988 
989 void SignatureVerificationFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
990 {
991  m_flags = parameters.GetValueWithDefault(Name::SignatureVerificationFilterFlags(), (word32)DEFAULT_FLAGS);
993  size_t size = m_verifier.SignatureLength();
994  CRYPTOPP_ASSERT(size != 0); // TODO: handle recoverable signature scheme
995  m_verified = false;
996  firstSize = m_flags & SIGNATURE_AT_BEGIN ? size : 0;
997  blockSize = 1;
998  lastSize = m_flags & SIGNATURE_AT_BEGIN ? 0 : size;
999 }
1000 
1002 {
1004  {
1007  else
1008  {
1010  if (inString) {memcpy(m_signature, inString, m_signature.size());}
1011  }
1012 
1013  if (m_flags & PUT_SIGNATURE)
1014  AttachedTransformation()->Put(inString, m_signature.size());
1015  }
1016  else
1017  {
1019  }
1020 }
1021 
1022 void SignatureVerificationFilter::NextPutMultiple(const byte *inString, size_t length)
1023 {
1024  m_messageAccumulator->Update(inString, length);
1025  if (m_flags & PUT_MESSAGE)
1026  AttachedTransformation()->Put(inString, length);
1027 }
1028 
1029 void SignatureVerificationFilter::LastPut(const byte *inString, size_t length)
1030 {
1032  {
1033  CRYPTOPP_ASSERT(length == 0);
1036  }
1037  else
1038  {
1039  m_verifier.InputSignature(*m_messageAccumulator, inString, length);
1041  if (m_flags & PUT_SIGNATURE)
1042  AttachedTransformation()->Put(inString, length);
1043  }
1044 
1045  if (m_flags & PUT_RESULT)
1047 
1048  if ((m_flags & THROW_EXCEPTION) && !m_verified)
1050 }
1051 
1052 // *************************************************************
1053 
1054 size_t Source::PumpAll2(bool blocking)
1055 {
1056  unsigned int messageCount = UINT_MAX;
1057  do {
1058  RETURN_IF_NONZERO(PumpMessages2(messageCount, blocking));
1059  } while(messageCount == UINT_MAX);
1060 
1061  return 0;
1062 }
1063 
1065 {
1066  if (!m_messageEnd && !AnyRetrievable())
1067  {
1068  m_messageEnd=true;
1069  return true;
1070  }
1071  else
1072  return false;
1073 }
1074 
1075 unsigned int Store::CopyMessagesTo(BufferedTransformation &target, unsigned int count, const std::string &channel) const
1076 {
1077  if (m_messageEnd || count == 0)
1078  return 0;
1079  else
1080  {
1081  CopyTo(target, ULONG_MAX, channel);
1083  target.ChannelMessageEnd(channel, GetAutoSignalPropagation()-1);
1084  return 1;
1085  }
1086 }
1087 
1089 {
1091  if (!parameters.GetValue(Name::InputBuffer(), array))
1092  throw InvalidArgument("StringStore: missing InputBuffer argument");
1093  m_store = array.begin();
1094  m_length = array.size();
1095  m_count = 0;
1096 }
1097 
1098 size_t StringStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
1099 {
1100  lword position = 0;
1101  size_t blockedBytes = CopyRangeTo2(target, position, transferBytes, channel, blocking);
1102  m_count += (size_t)position;
1103  transferBytes = position;
1104  return blockedBytes;
1105 }
1106 
1107 size_t StringStore::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
1108 {
1109  size_t i = UnsignedMin(m_length, m_count+begin);
1110  size_t len = UnsignedMin(m_length-i, end-begin);
1111  size_t blockedBytes = target.ChannelPut2(channel, m_store+i, len, 0, blocking);
1112  if (!blockedBytes)
1113  begin += len;
1114  return blockedBytes;
1115 }
1116 
1118 {
1119  parameters.GetRequiredParameter("RandomNumberStore", "RandomNumberGeneratorPointer", m_rng);
1120  int length;
1121  parameters.GetRequiredIntParameter("RandomNumberStore", "RandomNumberStoreSize", length);
1122  m_length = length;
1123 }
1124 
1125 size_t RandomNumberStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
1126 {
1127  if (!blocking)
1128  throw NotImplemented("RandomNumberStore: nonblocking transfer is not implemented by this object");
1129 
1130  transferBytes = UnsignedMin(transferBytes, m_length - m_count);
1131  m_rng->GenerateIntoBufferedTransformation(target, channel, transferBytes);
1132  m_count += transferBytes;
1133 
1134  return 0;
1135 }
1136 
1137 size_t NullStore::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
1138 {
1139  static const byte nullBytes[128] = {0};
1140  while (begin < end)
1141  {
1142  size_t len = (size_t)STDMIN(end-begin, lword(128));
1143  size_t blockedBytes = target.ChannelPut2(channel, nullBytes, len, 0, blocking);
1144  if (blockedBytes)
1145  return blockedBytes;
1146  begin += len;
1147  }
1148  return 0;
1149 }
1150 
1151 size_t NullStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
1152 {
1153  lword begin = 0;
1154  size_t blockedBytes = NullStore::CopyRangeTo2(target, begin, transferBytes, channel, blocking);
1155  transferBytes = begin;
1156  m_size -= begin;
1157  return blockedBytes;
1158 }
1159 
1161 
1162 #endif
virtual void ProcessLastBlock(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt the last block of data.
Definition: cryptlib.cpp:244
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:29
size_t PutMaybeModifiable(byte *begin, size_t length, int messageEnd, bool blocking, bool modifiable)
Definition: filters.cpp:350
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: filters.cpp:232
bool OutputMessageSeriesEnd(int outputSite, int propagation, bool blocking, const std::string &channel=DEFAULT_CHANNEL)
Marks the end of a series of messages, with signal propagation.
Definition: filters.cpp:147
Standard names for retrieving values by name when working with NameValuePairs.
void FirstPut(const byte *inString)
Definition: filters.cpp:942
An invalid argument was detected.
Definition: cryptlib.h:184
virtual size_t ChannelPutModifiable2(const std::string &channel, byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes that may be modified by callee on a channel.
Definition: cryptlib.cpp:472
Indicates the hash is at the beginning of the message (i.e., concatenation of hash+message) ...
Definition: filters.h:571
HashFilter(HashTransformation &hm, BufferedTransformation *attachment=NULL, bool putMessage=false, int truncatedDigestSize=-1, const std::string &messagePutChannel=DEFAULT_CHANNEL, const std::string &hashPutChannel=DEFAULT_CHANNEL)
Construct a HashFilter.
Definition: filters.cpp:756
#define FILTER_END_NO_MESSAGE_END
Definition: fltrimpl.h:26
HashTransformation & m_hashModule
Definition: filters.h:541
virtual bool AnyRetrievable() const
Determines whether bytes are ready for retrieval.
Definition: cryptlib.cpp:504
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: filters.cpp:522
void AddRangeToSkip(unsigned int message, lword position, lword size, bool sortNow=true)
Adds a range to skip during processing.
Definition: filters.cpp:166
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: filters.cpp:771
byte * m_space
Definition: filters.h:544
uint8_t byte
Definition: Common.h:57
void GetRequiredParameter(const char *className, const char *name, T &value) const
Retrieves a required name/value pair.
Definition: cryptlib.h:408
virtual void ProcessData(byte *outString, const byte *inString, size_t length)=0
Encrypt or decrypt an array of bytes.
Utility functions for the Crypto++ library.
size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing on a channel.
Definition: filters.cpp:927
void InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
Definition: filters.cpp:799
virtual size_t PumpAll2(bool blocking=true)
Pump all data to attached transformation.
Definition: filters.cpp:1054
BlockPaddingScheme
Padding schemes used for block ciphers.
Definition: filters.h:474
member_ptr< BufferedTransformation > m_filter
Definition: filters.h:972
Interface for one direction (encryption or decryption) of a stream cipher or block cipher mode with a...
Definition: cryptlib.h:1121
CRYPTOPP_DLL void GetRequiredIntParameter(const char *className, const char *name, int &value) const
Retrieves a required name/value pair.
Definition: cryptlib.h:423
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
Definition: cryptlib.h:350
Indicates the filter should throw a HashVerificationFailed if a failure is encountered.
Definition: filters.h:579
SecByteBlock m_expectedHash
Definition: filters.h:608
Message Queue.
Definition: mqueue.h:14
Indicates the result of the verification should be passed to an attached transformation.
Definition: filters.h:577
virtual void TruncatedFinal(byte *digest, size_t digestSize)=0
Computes the hash of the current message.
virtual void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: cryptlib.h:1503
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: filters.cpp:329
void ResetQueue(size_t blockSize, size_t maxBlocks)
Definition: filters.cpp:253
Filter class that is a proxy for a sink.
Definition: filters.h:903
void LastPut(const byte *inString, size_t length)
Input the last block of data.
Definition: filters.cpp:1029
#define FILTER_OUTPUT(site, output, length, messageEnd)
Definition: fltrimpl.h:49
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
virtual void InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
Definition: filters.h:362
void PropagateInitialize(const NameValuePairs &parameters, int propagation)
Definition: filters.cpp:112
size_t count
Definition: ExecStats.cpp:37
Default padding scheme.
Definition: filters.h:484
bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
Marks the end of a series of messages, with signal propagation.
Definition: filters.cpp:95
AuthenticatedDecryptionFilter(AuthenticatedSymmetricCipher &c, BufferedTransformation *attachment=NULL, word32 flags=DEFAULT_FLAGS, int truncatedDigestSize=-1, BlockPaddingScheme padding=DEFAULT_PADDING)
Construct a AuthenticatedDecryptionFilter.
Definition: filters.cpp:895
void NextPutMultiple(const byte *inString, size_t length)
Definition: filters.cpp:821
member_ptr< BufferedTransformation > m_attachment
Definition: filters.h:151
size_type size() const
Provides the count of elements in the SecBlock.
Definition: secblock.h:524
void ResetMeter()
Resets the meter.
Definition: filters.cpp:160
void NextPutMultiple(const byte *inString, size_t length)
Definition: filters.cpp:629
Indicates the filter should throw a HashVerificationFailed if a failure is encountered.
Definition: filters.h:771
virtual void NextPutSingle(const byte *inString)
Definition: filters.h:371
StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment=NULL, BlockPaddingScheme padding=DEFAULT_PADDING, bool allowAuthenticatedSymmetricCipher=false)
Construct a StreamTransformationFilter.
Definition: filters.cpp:582
Classes for automatic resource management.
size_t size() const
Length of the memory block.
Definition: algparam.h:93
byte * GetContigousBlocks(size_t &numberOfBytes)
Definition: filters.cpp:276
virtual size_t TransferTo2(BufferedTransformation &target, lword &byteCount, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)=0
Transfer bytes from this object to another BufferedTransformation.
#define c(i)
Indicates the message should be passed to an attached transformation.
Definition: filters.h:765
Library configuration file.
void FirstPut(const byte *inString)
Definition: filters.cpp:1001
byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
Request space which can be written into by the caller.
Definition: filters.cpp:865
void ProcessString(byte *inoutString, size_t length)
Encrypt or decrypt a string of bytes.
Definition: cryptlib.h:877
StreamTransformationFilter m_streamFilter
Definition: filters.h:712
bool GetNextMessage()
Start retrieving the next message.
Definition: filters.cpp:1064
void New(size_type newSize)
Change size without preserving contents.
Definition: secblock.h:647
Combines two sets of NameValuePairs.
Definition: algparam.h:135
void Insert(Filter *nextFilter)
Definition: filters.cpp:55
virtual bool VerifyAndRestart(PK_MessageAccumulator &messageAccumulator) const =0
Check whether messageAccumulator contains a valid signature and message, and restart messageAccumulat...
virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output on a channel.
Definition: cryptlib.cpp:480
virtual bool TruncatedVerify(const byte *digest, size_t digestLength)
Verifies the hash of the current message.
Definition: cryptlib.cpp:408
virtual size_t SignatureLength() const =0
Provides the signature length if it only depends on the key.
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:104
Interface for buffered transformations.
Definition: cryptlib.h:1352
void NextPutMultiple(const byte *s, size_t len)
Definition: filters.cpp:508
virtual int GetAutoSignalPropagation() const
Retrieve automatic signal propagation value.
Definition: cryptlib.h:1566
const byte * begin() const
Pointer to the first byte in the memory block.
Definition: algparam.h:89
void FirstPut(const byte *inString)
Definition: filters.cpp:810
Exception thrown when an invalid signature is encountered.
Definition: filters.h:749
virtual void LastPut(const byte *inString, size_t length)=0
Input the last block of data.
virtual bool ShouldPropagateMessageSeriesEnd() const
Definition: filters.h:84
SignatureVerificationFilter(const PK_Verifier &verifier, BufferedTransformation *attachment=NULL, word32 flags=DEFAULT_FLAGS)
Construct a SignatureVerificationFilter.
Definition: filters.cpp:982
unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=DEFAULT_CHANNEL) const
Definition: filters.cpp:1075
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: filters.cpp:960
HashTransformation & m_hashModule
Definition: filters.h:604
Pointer that overloads operator ->
Definition: smartptr.h:39
const std::string DEFAULT_CHANNEL
Default channel for BufferedTransformation.
Definition: cryptlib.cpp:59
size_t Output(int outputSite, const byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel=DEFAULT_CHANNEL)
Forward processed data on to attached transformation.
Definition: filters.cpp:127
Default flags using THROW_EXCEPTION.
Definition: filters.h:673
void LastPut(const byte *inString, size_t length)
Input the last block of data.
Definition: filters.cpp:887
virtual unsigned int GetOptimalBlockSizeUsed() const
Provides the number of bytes used in the current block when processing at optimal block size...
Definition: cryptlib.h:846
size_t PutModifiable(byte *inString, size_t length, bool blocking=true)
Input multiple bytes that may be modified by callee.
Definition: cryptlib.h:1426
Exception thrown when a data integrity check failure is encountered.
Definition: filters.h:557
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: filters.cpp:859
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:61
1 and 0&#39;s padding added to a block
Definition: filters.h:482
#define FILTER_BEGIN
Definition: fltrimpl.h:14
HashVerificationFilter(HashTransformation &hm, BufferedTransformation *attachment=NULL, word32 flags=DEFAULT_FLAGS, int truncatedDigestSize=-1)
Construct a HashVerificationFilter.
Definition: filters.cpp:792
bool MessageEnd(int propagation=-1, bool blocking=true)
Signals the end of messages to the object.
Definition: cryptlib.h:1434
size_t GetAll(byte *outString)
Definition: filters.cpp:287
void NextPutModifiable(byte *inString, size_t length)
Definition: filters.cpp:656
bool m_putMessage
Definition: filters.h:542
bool OutputFlush(int outputSite, bool hardFlush, int propagation, bool blocking, const std::string &channel=DEFAULT_CHANNEL)
Flush buffered input and/or output, with signal propagation.
Definition: filters.cpp:136
bool GetValue(const char *name, T &value) const
Get a named value.
Definition: cryptlib.h:337
virtual void Attach(BufferedTransformation *newAttachment)
Add newAttachment to the end of attachment chain.
Definition: cryptlib.cpp:772
A method was called which was not implemented.
Definition: cryptlib.h:205
std::string m_hashPutChannel
Definition: filters.h:545
int m_continueAt
Definition: filters.h:155
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: filters.cpp:534
size_t Put(byte inByte, bool blocking=true)
Input a byte for processing.
Definition: cryptlib.h:1376
virtual bool IsForwardTransformation() const =0
Determines if the cipher is being operated in its forward direction.
virtual unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this cipher.
Definition: cryptlib.h:842
const PK_Verifier & m_verifier
Definition: filters.h:797
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: filters.cpp:966
void Detach(BufferedTransformation *newAttachment=NULL)
Replace an attached transformation.
Definition: filters.cpp:50
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:498
virtual bool IsSelfInverting() const =0
Determines whether the cipher is self-inverting.
StreamTransformation & m_cipher
Definition: filters.h:514
void NextPutMultiple(const byte *inString, size_t length)
Definition: filters.cpp:947
void NextPutMaybeModifiable(byte *inString, size_t length, bool modifiable)
Definition: filters.h:394
virtual bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true)
Marks the end of a series of messages on a channel.
Definition: cryptlib.cpp:488
virtual void FlushDerived()
Definition: filters.h:390
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:1137
CRYPTOPP_DLL int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
Definition: cryptlib.h:382
virtual unsigned int MandatoryBlockSize() const
Provides the mandatory block size of the cipher.
Definition: cryptlib.h:835
virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition: cryptlib.cpp:442
BufferedTransformation * AttachedTransformation()
Retrieve attached transformation.
Definition: filters.cpp:36
size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes that may be modified by callee.
Definition: filters.cpp:237
Indicates the message should be passed to an attached transformation.
Definition: filters.h:573
static size_t LastBlockSize(StreamTransformation &c, BlockPaddingScheme padding)
Definition: filters.cpp:594
virtual void NextPutModifiable(byte *inString, size_t length)
Definition: filters.h:377
size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing on a channel.
Definition: filters.cpp:876
byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
Request space which can be written into by the caller.
Definition: filters.cpp:916
Exception thrown when a filter does not recognize a named channel.
Definition: cryptlib.h:1855
size_t PutMaybeModifiable(byte *inString, size_t length, int messageEnd, bool blocking, bool modifiable)
Definition: filters.cpp:174
byte * begin() const
Pointer to the first byte in the memory block.
Definition: algparam.h:119
byte * CreatePutSpace(size_t &size)
Request space which can be written into by the caller.
Definition: filters.cpp:550
virtual void FirstPut(const byte *inString)=0
#define FILTER_OUTPUT_MAYBE_MODIFIABLE(site, output, length, messageEnd, modifiable)
Definition: fltrimpl.h:74
size_t OutputModifiable(int outputSite, byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel=DEFAULT_CHANNEL)
Output multiple bytes that may be modified by callee.
Definition: filters.cpp:118
unsigned int m_digestSize
Definition: filters.h:606
virtual std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: cryptlib.h:518
T1 SaturatingSubtract(const T1 &a, const T2 &b)
Performs a saturating subtract clamped at 0.
Definition: misc.h:847
const std::string AAD_CHANNEL
Channel for additional authenticated data.
Definition: cryptlib.cpp:60
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
Definition: misc.h:512
virtual size_t ChannelPut2(const std::string &channel, const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing on a channel.
Definition: cryptlib.cpp:464
Indicates the hash should be passed to an attached transformation.
Definition: filters.h:575
bool IsolatedMessageSeriesEnd(bool blocking)
Marks the end of a series of messages, without signal propagation.
Definition: filters.cpp:242
size_t size() const
Length of the memory block.
Definition: algparam.h:123
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: filters.cpp:565
member_ptr< PK_MessageAccumulator > m_messageAccumulator
Definition: filters.h:798
unsigned int m_digestSize
Definition: filters.h:543
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: filters.cpp:556
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:1151
Interface for the data processing portion of stream ciphers.
Definition: cryptlib.h:823
void LastPut(const byte *inString, size_t length)
Input the last block of data.
Definition: filters.cpp:952
const T * get() const
Definition: smartptr.h:52
void LastPut(const byte *inString, size_t length)
Input the last block of data.
Definition: filters.cpp:828
virtual size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const =0
Copy bytes from this object to another BufferedTransformation.
void InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
Definition: filters.cpp:604
void LastPut(const byte *inString, size_t length)
Input the last block of data.
Definition: filters.cpp:662
Divides an input stream into discrete blocks.
Definition: filters.h:317
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition: misc.h:477
#define CRYPTOPP_ASSERT(exp)
Definition: trap.h:92
BlockPaddingScheme m_padding
Definition: filters.h:515
void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
Definition: misc.cpp:28
virtual bool IsolatedMessageSeriesEnd(bool blocking)
Marks the end of a series of messages, without signal propagation.
Definition: cryptlib.h:1517
CRYPTOPP_DLL void StoreInitialize(const NameValuePairs &parameters)
Definition: filters.cpp:1088
virtual PK_MessageAccumulator * NewVerificationAccumulator() const =0
Create a new HashTransformation to accumulate the message to be verified.
virtual bool IsolatedFlush(bool hardFlush, bool blocking)=0
Flushes data buffered by this object, without signal propagation.
Implementation of BufferedTransformation&#39;s attachment interface.
void FirstPut(const byte *inString)
Definition: filters.cpp:622
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: filters.cpp:527
A decryption filter encountered invalid ciphertext.
Definition: cryptlib.h:198
Exception thrown by objects that have not implemented nonblocking input processing.
Definition: cryptlib.h:1470
Filter wrapper for StreamTransformation.
Definition: filters.h:491
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:66
byte * CreatePutSpace(size_t &size)
Request space which can be written into by the caller.
Definition: filters.h:538
FilterWithBufferedInput(BufferedTransformation *attachment)
Construct a FilterWithBufferedInput with an attached transformation.
Definition: filters.cpp:315
void TransferAllTo(BufferedTransformation &target, const std::string &channel=DEFAULT_CHANNEL)
Transfer all bytes from this object to another BufferedTransformation.
Definition: cryptlib.h:1763
Interface for public-key signature verifiers.
Definition: cryptlib.h:2592
T * release()
Definition: smartptr.h:55
virtual unsigned int DigestSize() const =0
Provides the digest size of the hash.
uint8_t const size_t const size
Definition: sha3.h:20
void * memcpy(void *a, const void *b, size_t c)
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
HashVerificationFilter m_hashVerifier
Definition: filters.h:711
Indicates the result of the verification should be passed to an attached transformation.
Definition: filters.h:769
virtual BufferedTransformation * NewDefaultAttachment() const
Definition: filters.cpp:31
ProxyFilter(BufferedTransformation *filter, size_t firstSize, size_t lastSize, BufferedTransformation *attachment)
Construct a ProxyFilter.
Definition: filters.cpp:484
void Put(const byte *inString, size_t length)
Definition: filters.cpp:301
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:1125
virtual bool SignatureUpfront() const
Determines whether the signature must be input before the message.
Definition: cryptlib.h:2497
void ForceNextPut()
Flushes data buffered by this object.
Definition: filters.cpp:441
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:930
CRYPTOPP_DLL 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:1107
virtual byte * CreatePutSpace(size_t &size)
Request space which can be written into by the caller.
Definition: cryptlib.h:1411
bool IsolatedFlush(bool hardFlush, bool blocking)
Flushes data buffered by this object, without signal propagation.
Definition: filters.cpp:491
0&#39;s padding added to a block
Definition: filters.h:478
uint8_t byte
Definition: Common.h:10
Implementation of BufferedTransformation&#39;s attachment interface.
Definition: filters.h:36
unsigned int m_optimalBufferSize
Definition: filters.h:516
size_t m_inputPosition
Definition: filters.h:154
void StoreInitialize(const NameValuePairs &parameters)
Definition: filters.cpp:1117
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
Definition: misc.h:487
size_t PutMessageEnd(const byte *inString, size_t length, int propagation=-1, bool blocking=true)
Input multiple bytes for processing and signal the end of a message.
Definition: cryptlib.h:1447
BlockQueue m_queue
Definition: filters.h:424
#define NAMESPACE_END
Definition: config.h:201
std::vector< char * > parameters
Definition: boostTest.cpp:46
No padding added to a block.
Definition: filters.h:476
void InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
Definition: filters.cpp:904
void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1)
Initialize or reinitialize this object, with signal propagation.
Definition: filters.cpp:71
void Initialize(const NameValuePairs &parameters, int propagation)
Initialize or reinitialize this object, with signal propagation.
Definition: filters.cpp:473
CRYPTOPP_DLL 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:1098
PKCS #5 padding added to a block.
Definition: filters.h:480
lword CopyTo(BufferedTransformation &target, lword copyMax=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL) const
copy copyMax bytes of the buffered output to target as input
Definition: cryptlib.h:1679
Indicates the signature is at the beginning of the message (i.e., concatenation of signature+message)...
Definition: filters.h:763
word64 lword
Definition: config.h:245
void InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
Definition: filters.cpp:989
Indicates the signature should be passed to an attached transformation.
Definition: filters.h:767
void NextPutMultiple(const byte *inString, size_t length)
Definition: filters.cpp:1022
AuthenticatedEncryptionFilter(AuthenticatedSymmetricCipher &c, BufferedTransformation *attachment=NULL, bool putAAD=false, int truncatedDigestSize=-1, const std::string &macChannel=DEFAULT_CHANNEL, BlockPaddingScheme padding=DEFAULT_PADDING)
Construct a AuthenticatedEncryptionFilter.
Definition: filters.cpp:851
virtual void InputSignature(PK_MessageAccumulator &messageAccumulator, const byte *signature, size_t signatureLength) const =0
Input signature into a message accumulator.
Default flags using SIGNATURE_AT_BEGIN and PUT_RESULT.
Definition: filters.h:773
#define FILTER_OUTPUT3(site, statement, output, length, messageEnd, channel)
Definition: fltrimpl.h:38
unsigned int word32
Definition: config.h:231
bool IsolatedFlush(bool hardFlush, bool blocking)
Flushes data buffered by this object, without signal propagation.
Definition: filters.cpp:338
virtual void NextPutMultiple(const byte *inString, size_t length)
Definition: filters.cpp:459
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:177
virtual void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1)
Initialize or reinitialize this object, with signal propagation.
Definition: cryptlib.cpp:435
Input data was received that did not conform to expected format.
Definition: cryptlib.h:191
void * memmove(void *a, const void *b, size_t c)
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: filters.h:335
std::string m_messagePutChannel
Definition: filters.h:545
void SetFilter(Filter *filter)
Sets the OutputProxy filter.
Definition: filters.cpp:496
void reset(T *p=0)
Definition: smartptr.h:72
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition: filters.cpp:78
virtual void Update(const byte *input, size_t length)=0
Updates a hash with additional input.
virtual unsigned int MinLastBlockSize() const
Provides the size of the last block.
Definition: cryptlib.h:871
T1 RoundDownToMultipleOf(const T1 &n, const T2 &m)
Rounds a value down to a multiple of a second value.
Definition: misc.h:889
Default flags using HASH_AT_BEGIN and PUT_RESULT.
Definition: filters.h:581
void NextPutModifiable(byte *inString, size_t length)
Definition: filters.cpp:514
#define RETURN_IF_NONZERO(x)
Definition: misc.h:607
bool ChannelMessageEnd(const std::string &channel, int propagation=-1, bool blocking=true)
Signal the end of a message.
Definition: cryptlib.h:1913
#define SIZE_MAX
Definition: misc.h:113
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: filters.cpp:764
Interface for retrieving values given their names.
Definition: cryptlib.h:279
SecByteBlock m_signature
Definition: filters.h:800