Crypto++  5.6.5
Free C++ class library of cryptographic schemes
cryptlib.cpp
1 // cryptlib.cpp - originally 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: 4127 4189 4459)
8 #endif
9 
10 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
11 # pragma GCC diagnostic ignored "-Wunused-value"
12 # pragma GCC diagnostic ignored "-Wunused-variable"
13 # pragma GCC diagnostic ignored "-Wunused-parameter"
14 #endif
15 
16 #ifndef CRYPTOPP_IMPORTS
17 
18 #include "cryptlib.h"
19 #include "misc.h"
20 #include "filters.h"
21 #include "algparam.h"
22 #include "fips140.h"
23 #include "argnames.h"
24 #include "fltrimpl.h"
25 #include "trdlocal.h"
26 #include "osrng.h"
27 #include "secblock.h"
28 #include "smartptr.h"
29 
30 // http://www.cygwin.com/faq.html#faq.api.winsock
31 #if (defined(__CYGWIN__) || defined(__CYGWIN32__)) && defined(PREFER_WINDOWS_STYLE_SOCKETS)
32 # error Cygwin does not support Windows style sockets. See http://www.cygwin.com/faq.html#faq.api.winsock
33 #endif
34 
35 NAMESPACE_BEGIN(CryptoPP)
36 
37 CRYPTOPP_COMPILE_ASSERT(sizeof(byte) == 1);
38 CRYPTOPP_COMPILE_ASSERT(sizeof(word16) == 2);
39 CRYPTOPP_COMPILE_ASSERT(sizeof(word32) == 4);
40 CRYPTOPP_COMPILE_ASSERT(sizeof(word64) == 8);
41 #ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLE
42 CRYPTOPP_COMPILE_ASSERT(sizeof(dword) == 2*sizeof(word));
43 #endif
44 
46 {
47 public:
48  NullNameValuePairs() {} // Clang complains a default ctor must be avilable
49  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
50  {CRYPTOPP_UNUSED(name); CRYPTOPP_UNUSED(valueType); CRYPTOPP_UNUSED(pValue); return false;}
51 };
52 
54 {
55  static BitBucket bitBucket;
56  return bitBucket;
57 }
58 
59 Algorithm::Algorithm(bool checkSelfTestStatus)
60 {
61  if (checkSelfTestStatus && FIPS_140_2_ComplianceEnabled())
62  {
63  if (GetPowerUpSelfTestStatus() == POWER_UP_SELF_TEST_NOT_DONE && !PowerUpSelfTestInProgressOnThisThread())
64  throw SelfTestFailure("Cryptographic algorithms are disabled before the power-up self tests are performed.");
65 
67  throw SelfTestFailure("Cryptographic algorithms are disabled after a power-up self test failed.");
68  }
69 }
70 
71 void SimpleKeyingInterface::SetKey(const byte *key, size_t length, const NameValuePairs &params)
72 {
73  this->ThrowIfInvalidKeyLength(length);
74  this->UncheckedSetKey(key, static_cast<unsigned int>(length), params);
75 }
76 
77 void SimpleKeyingInterface::SetKeyWithRounds(const byte *key, size_t length, int rounds)
78 {
79  SetKey(key, length, MakeParameters(Name::Rounds(), rounds));
80 }
81 
82 void SimpleKeyingInterface::SetKeyWithIV(const byte *key, size_t length, const byte *iv, size_t ivLength)
83 {
84  SetKey(key, length, MakeParameters(Name::IV(), ConstByteArrayParameter(iv, ivLength)));
85 }
86 
87 void SimpleKeyingInterface::ThrowIfInvalidKeyLength(size_t length)
88 {
89  if (!IsValidKeyLength(length))
90  throw InvalidKeyLength(GetAlgorithm().AlgorithmName(), length);
91 }
92 
93 void SimpleKeyingInterface::ThrowIfResynchronizable()
94 {
95  if (IsResynchronizable())
96  throw InvalidArgument(GetAlgorithm().AlgorithmName() + ": this object requires an IV");
97 }
98 
99 void SimpleKeyingInterface::ThrowIfInvalidIV(const byte *iv)
100 {
101  if (!iv && IVRequirement() == UNPREDICTABLE_RANDOM_IV)
102  throw InvalidArgument(GetAlgorithm().AlgorithmName() + ": this object cannot use a null IV");
103 }
104 
105 size_t SimpleKeyingInterface::ThrowIfInvalidIVLength(int size)
106 {
107  if (size < 0)
108  return (size_t)IVSize();
109  else if ((size_t)size < MinIVLength())
110  throw InvalidArgument(GetAlgorithm().AlgorithmName() + ": IV length " + IntToString(size) + " is less than the minimum of " + IntToString(MinIVLength()));
111  else if ((size_t)size > MaxIVLength())
112  throw InvalidArgument(GetAlgorithm().AlgorithmName() + ": IV length " + IntToString(size) + " exceeds the maximum of " + IntToString(MaxIVLength()));
113  else
114  return (size_t)size;
115 }
116 
117 const byte * SimpleKeyingInterface::GetIVAndThrowIfInvalid(const NameValuePairs &params, size_t &size)
118 {
119  ConstByteArrayParameter ivWithLength;
120  const byte *iv;
121  bool found = false;
122 
123  try {found = params.GetValue(Name::IV(), ivWithLength);}
124  catch (const NameValuePairs::ValueTypeMismatch &) {}
125 
126  if (found)
127  {
128  iv = ivWithLength.begin();
129  ThrowIfInvalidIV(iv);
130  size = ThrowIfInvalidIVLength(static_cast<int>(ivWithLength.size()));
131  return iv;
132  }
133  else if (params.GetValue(Name::IV(), iv))
134  {
135  ThrowIfInvalidIV(iv);
136  size = IVSize();
137  return iv;
138  }
139  else
140  {
141  ThrowIfResynchronizable();
142  size = 0;
143  return NULLPTR;
144  }
145 }
146 
148 {
149  rng.GenerateBlock(IV, IVSize());
150 }
151 
152 size_t BlockTransformation::AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const
153 {
154  CRYPTOPP_ASSERT(inBlocks);
155  CRYPTOPP_ASSERT(outBlocks);
156  CRYPTOPP_ASSERT(length);
157 
158  size_t blockSize = BlockSize();
159  size_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize;
160  size_t xorIncrement = xorBlocks ? blockSize : 0;
161  size_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize;
162 
163  if (flags & BT_ReverseDirection)
164  {
165  CRYPTOPP_ASSERT(length % blockSize == 0);
166  inBlocks += length - blockSize;
167  xorBlocks += length - blockSize;
168  outBlocks += length - blockSize;
169  inIncrement = 0-inIncrement;
170  xorIncrement = 0-xorIncrement;
171  outIncrement = 0-outIncrement;
172  }
173 
174  // Coverity finding.
175  bool xorFlag = xorBlocks && (flags & BT_XorInput);
176  while (length >= blockSize)
177  {
178  if (xorFlag)
179  {
180  // xorBlocks non-NULL and with BT_XorInput.
181  xorbuf(outBlocks, xorBlocks, inBlocks, blockSize);
182  ProcessBlock(outBlocks);
183  }
184  else
185  {
186  // xorBlocks may be non-NULL and without BT_XorInput.
187  ProcessAndXorBlock(inBlocks, xorBlocks, outBlocks);
188  }
189 
190  if (flags & BT_InBlockIsCounter)
191  const_cast<byte *>(inBlocks)[blockSize-1]++;
192  inBlocks += inIncrement;
193  outBlocks += outIncrement;
194  xorBlocks += xorIncrement;
195  length -= blockSize;
196  }
197 
198  return length;
199 }
200 
202 {
203  return GetAlignmentOf<word32>();
204 }
205 
207 {
208  return GetAlignmentOf<word32>();
209 }
210 
212 {
213  return GetAlignmentOf<word32>();
214 }
215 
216 void StreamTransformation::ProcessLastBlock(byte *outString, const byte *inString, size_t length)
217 {
218  CRYPTOPP_ASSERT(MinLastBlockSize() == 0); // this function should be overridden otherwise
219 
220  if (length == MandatoryBlockSize())
221  ProcessData(outString, inString, length);
222  else if (length != 0)
223  throw NotImplemented(AlgorithmName() + ": this object doesn't support a special last block");
224 }
225 
226 void AuthenticatedSymmetricCipher::SpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength)
227 {
228  if (headerLength > MaxHeaderLength())
229  throw InvalidArgument(GetAlgorithm().AlgorithmName() + ": header length " + IntToString(headerLength) + " exceeds the maximum of " + IntToString(MaxHeaderLength()));
230 
231  if (messageLength > MaxMessageLength())
232  throw InvalidArgument(GetAlgorithm().AlgorithmName() + ": message length " + IntToString(messageLength) + " exceeds the maximum of " + IntToString(MaxMessageLength()));
233 
234  if (footerLength > MaxFooterLength())
235  throw InvalidArgument(GetAlgorithm().AlgorithmName() + ": footer length " + IntToString(footerLength) + " exceeds the maximum of " + IntToString(MaxFooterLength()));
236 
237  UncheckedSpecifyDataLengths(headerLength, messageLength, footerLength);
238 }
239 
240 void AuthenticatedSymmetricCipher::EncryptAndAuthenticate(byte *ciphertext, byte *mac, size_t macSize, const byte *iv, int ivLength, const byte *header, size_t headerLength, const byte *message, size_t messageLength)
241 {
242  Resynchronize(iv, ivLength);
243  SpecifyDataLengths(headerLength, messageLength);
244  Update(header, headerLength);
245  ProcessString(ciphertext, message, messageLength);
246  TruncatedFinal(mac, macSize);
247 }
248 
249 bool AuthenticatedSymmetricCipher::DecryptAndVerify(byte *message, const byte *mac, size_t macLength, const byte *iv, int ivLength, const byte *header, size_t headerLength, const byte *ciphertext, size_t ciphertextLength)
250 {
251  Resynchronize(iv, ivLength);
252  SpecifyDataLengths(headerLength, ciphertextLength);
253  Update(header, headerLength);
254  ProcessString(message, ciphertext, ciphertextLength);
255  return TruncatedVerify(mac, macLength);
256 }
257 
259 {
260  return GenerateByte() & 1;
261 }
262 
264 {
265  byte b;
266  GenerateBlock(&b, 1);
267  return b;
268 }
269 
270 word32 RandomNumberGenerator::GenerateWord32(word32 min, word32 max)
271 {
272  const word32 range = max-min;
273  const unsigned int maxBits = BitPrecision(range);
274 
275  word32 value;
276 
277  do
278  {
279  GenerateBlock((byte *)&value, sizeof(value));
280  value = Crop(value, maxBits);
281  } while (value > range);
282 
283  return value+min;
284 }
285 
286 // Stack recursion below... GenerateIntoBufferedTransformation calls GenerateBlock,
287 // and GenerateBlock calls GenerateIntoBufferedTransformation. Ad infinitum. Also
288 // see http://github.com/weidai11/cryptopp/issues/38.
289 //
290 // According to Wei, RandomNumberGenerator is an interface, and it should not
291 // be instantiable. Its now spilt milk, and we are going to CRYPTOPP_ASSERT it in Debug
292 // builds to alert the programmer and throw in Release builds. Developers have
293 // a reference implementation in case its needed. If a programmer
294 // unintentionally lands here, then they should ensure use of a
295 // RandomNumberGenerator pointer or reference so polymorphism can provide the
296 // proper runtime dispatching.
297 
298 void RandomNumberGenerator::GenerateBlock(byte *output, size_t size)
299 {
300  CRYPTOPP_UNUSED(output), CRYPTOPP_UNUSED(size);
301 
302 #if 0
303  // This breaks AutoSeededX917RNG<T> generators.
304  throw NotImplemented("RandomNumberGenerator: GenerateBlock not implemented");
305 #endif
306 
307  ArraySink s(output, size);
308  GenerateIntoBufferedTransformation(s, DEFAULT_CHANNEL, size);
309 }
310 
312 {
313  GenerateIntoBufferedTransformation(TheBitBucket(), DEFAULT_CHANNEL, n);
314 }
315 
316 void RandomNumberGenerator::GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword length)
317 {
319  while (length)
320  {
321  size_t len = UnsignedMin(buffer.size(), length);
322  GenerateBlock(buffer, len);
323  size_t rem = target.ChannelPut(channel, buffer, len);
324  CRYPTOPP_UNUSED(rem); CRYPTOPP_ASSERT(rem == 0);
325  length -= len;
326  }
327 }
328 
329 //! \class ClassNullRNG
330 //! \brief Random Number Generator that does not produce random numbers
331 //! \details ClassNullRNG can be used for functions that require a RandomNumberGenerator
332 //! but don't actually use it. The class throws NotImplemented when a generation function is called.
333 //! \sa NullRNG()
335 {
336 public:
337  //! \brief The name of the generator
338  //! \returns the string \a NullRNGs
339  std::string AlgorithmName() const {return "NullRNG";}
340 
341 #if defined(CRYPTOPP_DOXYGEN_PROCESSING)
342  //! \brief An implementation that throws NotImplemented
343  byte GenerateByte () {}
344  //! \brief An implementation that throws NotImplemented
345  unsigned int GenerateBit () {}
346  //! \brief An implementation that throws NotImplemented
347  word32 GenerateWord32 (word32 min, word32 max) {}
348 #endif
349 
350  //! \brief An implementation that throws NotImplemented
351  void GenerateBlock(byte *output, size_t size)
352  {
353  CRYPTOPP_UNUSED(output); CRYPTOPP_UNUSED(size);
354  throw NotImplemented("NullRNG: NullRNG should only be passed to functions that don't need to generate random bytes");
355  }
356 
357 #if defined(CRYPTOPP_DOXYGEN_PROCESSING)
358  //! \brief An implementation that throws NotImplemented
359  void GenerateIntoBufferedTransformation (BufferedTransformation &target, const std::string &channel, lword length) {}
360  //! \brief An implementation that throws NotImplemented
361  void IncorporateEntropy (const byte *input, size_t length) {}
362  //! \brief An implementation that returns \p false
363  bool CanIncorporateEntropy () const {}
364  //! \brief An implementation that does nothing
365  void DiscardBytes (size_t n) {}
366  //! \brief An implementation that does nothing
367  void Shuffle (IT begin, IT end) {}
368 
369 private:
370  Clonable* Clone () const { return NULLPTR; }
371 #endif
372 };
373 
375 {
376  static ClassNullRNG s_nullRNG;
377  return s_nullRNG;
378 }
379 
380 bool HashTransformation::TruncatedVerify(const byte *digestIn, size_t digestLength)
381 {
382  ThrowIfInvalidTruncatedSize(digestLength);
383  SecByteBlock digest(digestLength);
384  TruncatedFinal(digest, digestLength);
385  return VerifyBufsEqual(digest, digestIn, digestLength);
386 }
387 
388 void HashTransformation::ThrowIfInvalidTruncatedSize(size_t size) const
389 {
390  if (size > DigestSize())
391  throw InvalidArgument("HashTransformation: can't truncate a " + IntToString(DigestSize()) + " byte digest to " + IntToString(size) + " bytes");
392 }
393 
395 {
396  const BufferedTransformation *t = AttachedTransformation();
397  return t ? t->GetMaxWaitObjectCount() : 0;
398 }
399 
401 {
402  BufferedTransformation *t = AttachedTransformation();
403  if (t)
404  t->GetWaitObjects(container, callStack); // reduce clutter by not adding to stack here
405 }
406 
407 void BufferedTransformation::Initialize(const NameValuePairs &parameters, int propagation)
408 {
409  CRYPTOPP_UNUSED(propagation);
410  CRYPTOPP_ASSERT(!AttachedTransformation());
411  IsolatedInitialize(parameters);
412 }
413 
414 bool BufferedTransformation::Flush(bool hardFlush, int propagation, bool blocking)
415 {
416  CRYPTOPP_UNUSED(propagation);
417  CRYPTOPP_ASSERT(!AttachedTransformation());
418  return IsolatedFlush(hardFlush, blocking);
419 }
420 
421 bool BufferedTransformation::MessageSeriesEnd(int propagation, bool blocking)
422 {
423  CRYPTOPP_UNUSED(propagation);
424  CRYPTOPP_ASSERT(!AttachedTransformation());
425  return IsolatedMessageSeriesEnd(blocking);
426 }
427 
428 byte * BufferedTransformation::ChannelCreatePutSpace(const std::string &channel, size_t &size)
429 {
430  if (channel.empty())
431  return CreatePutSpace(size);
432  else
433  throw NoChannelSupport(AlgorithmName());
434 }
435 
436 size_t BufferedTransformation::ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
437 {
438  if (channel.empty())
439  return Put2(begin, length, messageEnd, blocking);
440  else
441  throw NoChannelSupport(AlgorithmName());
442 }
443 
444 size_t BufferedTransformation::ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
445 {
446  if (channel.empty())
447  return PutModifiable2(begin, length, messageEnd, blocking);
448  else
449  return ChannelPut2(channel, begin, length, messageEnd, blocking);
450 }
451 
452 bool BufferedTransformation::ChannelFlush(const std::string &channel, bool completeFlush, int propagation, bool blocking)
453 {
454  if (channel.empty())
455  return Flush(completeFlush, propagation, blocking);
456  else
457  throw NoChannelSupport(AlgorithmName());
458 }
459 
460 bool BufferedTransformation::ChannelMessageSeriesEnd(const std::string &channel, int propagation, bool blocking)
461 {
462  if (channel.empty())
463  return MessageSeriesEnd(propagation, blocking);
464  else
465  throw NoChannelSupport(AlgorithmName());
466 }
467 
469 {
470  if (AttachedTransformation())
471  return AttachedTransformation()->MaxRetrievable();
472  else
473  return CopyTo(TheBitBucket());
474 }
475 
477 {
478  if (AttachedTransformation())
479  return AttachedTransformation()->AnyRetrievable();
480  else
481  {
482  byte b;
483  return Peek(b) != 0;
484  }
485 }
486 
487 size_t BufferedTransformation::Get(byte &outByte)
488 {
489  if (AttachedTransformation())
490  return AttachedTransformation()->Get(outByte);
491  else
492  return Get(&outByte, 1);
493 }
494 
495 size_t BufferedTransformation::Get(byte *outString, size_t getMax)
496 {
497  if (AttachedTransformation())
498  return AttachedTransformation()->Get(outString, getMax);
499  else
500  {
501  ArraySink arraySink(outString, getMax);
502  return (size_t)TransferTo(arraySink, getMax);
503  }
504 }
505 
506 size_t BufferedTransformation::Peek(byte &outByte) const
507 {
508  if (AttachedTransformation())
509  return AttachedTransformation()->Peek(outByte);
510  else
511  return Peek(&outByte, 1);
512 }
513 
514 size_t BufferedTransformation::Peek(byte *outString, size_t peekMax) const
515 {
516  if (AttachedTransformation())
517  return AttachedTransformation()->Peek(outString, peekMax);
518  else
519  {
520  ArraySink arraySink(outString, peekMax);
521  return (size_t)CopyTo(arraySink, peekMax);
522  }
523 }
524 
525 lword BufferedTransformation::Skip(lword skipMax)
526 {
527  if (AttachedTransformation())
528  return AttachedTransformation()->Skip(skipMax);
529  else
530  return TransferTo(TheBitBucket(), skipMax);
531 }
532 
534 {
535  if (AttachedTransformation())
536  return AttachedTransformation()->TotalBytesRetrievable();
537  else
538  return MaxRetrievable();
539 }
540 
542 {
543  if (AttachedTransformation())
544  return AttachedTransformation()->NumberOfMessages();
545  else
546  return CopyMessagesTo(TheBitBucket());
547 }
548 
550 {
551  if (AttachedTransformation())
552  return AttachedTransformation()->AnyMessages();
553  else
554  return NumberOfMessages() != 0;
555 }
556 
558 {
559  if (AttachedTransformation())
560  return AttachedTransformation()->GetNextMessage();
561  else
562  {
563  CRYPTOPP_ASSERT(!AnyMessages());
564  return false;
565  }
566 }
567 
568 unsigned int BufferedTransformation::SkipMessages(unsigned int count)
569 {
570  if (AttachedTransformation())
571  return AttachedTransformation()->SkipMessages(count);
572  else
573  return TransferMessagesTo(TheBitBucket(), count);
574 }
575 
576 size_t BufferedTransformation::TransferMessagesTo2(BufferedTransformation &target, unsigned int &messageCount, const std::string &channel, bool blocking)
577 {
578  if (AttachedTransformation())
579  return AttachedTransformation()->TransferMessagesTo2(target, messageCount, channel, blocking);
580  else
581  {
582  unsigned int maxMessages = messageCount;
583  for (messageCount=0; messageCount < maxMessages && AnyMessages(); messageCount++)
584  {
585  size_t blockedBytes;
586  lword transferredBytes;
587 
588  while (AnyRetrievable())
589  {
590  transferredBytes = LWORD_MAX;
591  blockedBytes = TransferTo2(target, transferredBytes, channel, blocking);
592  if (blockedBytes > 0)
593  return blockedBytes;
594  }
595 
596  if (target.ChannelMessageEnd(channel, GetAutoSignalPropagation(), blocking))
597  return 1;
598 
599  bool result = GetNextMessage();
600  CRYPTOPP_UNUSED(result); CRYPTOPP_ASSERT(result);
601  }
602  return 0;
603  }
604 }
605 
606 unsigned int BufferedTransformation::CopyMessagesTo(BufferedTransformation &target, unsigned int count, const std::string &channel) const
607 {
608  if (AttachedTransformation())
609  return AttachedTransformation()->CopyMessagesTo(target, count, channel);
610  else
611  return 0;
612 }
613 
615 {
616  if (AttachedTransformation())
617  AttachedTransformation()->SkipAll();
618  else
619  {
620  while (SkipMessages()) {}
621  while (Skip()) {}
622  }
623 }
624 
625 size_t BufferedTransformation::TransferAllTo2(BufferedTransformation &target, const std::string &channel, bool blocking)
626 {
627  if (AttachedTransformation())
628  return AttachedTransformation()->TransferAllTo2(target, channel, blocking);
629  else
630  {
631  CRYPTOPP_ASSERT(!NumberOfMessageSeries());
632 
633  unsigned int messageCount;
634  do
635  {
636  messageCount = UINT_MAX;
637  size_t blockedBytes = TransferMessagesTo2(target, messageCount, channel, blocking);
638  if (blockedBytes)
639  return blockedBytes;
640  }
641  while (messageCount != 0);
642 
643  lword byteCount;
644  do
645  {
646  byteCount = ULONG_MAX;
647  size_t blockedBytes = TransferTo2(target, byteCount, channel, blocking);
648  if (blockedBytes)
649  return blockedBytes;
650  }
651  while (byteCount != 0);
652 
653  return 0;
654  }
655 }
656 
657 void BufferedTransformation::CopyAllTo(BufferedTransformation &target, const std::string &channel) const
658 {
659  if (AttachedTransformation())
660  AttachedTransformation()->CopyAllTo(target, channel);
661  else
662  {
663  CRYPTOPP_ASSERT(!NumberOfMessageSeries());
664  while (CopyMessagesTo(target, UINT_MAX, channel)) {}
665  }
666 }
667 
668 void BufferedTransformation::SetRetrievalChannel(const std::string &channel)
669 {
670  if (AttachedTransformation())
671  AttachedTransformation()->SetRetrievalChannel(channel);
672 }
673 
674 size_t BufferedTransformation::ChannelPutWord16(const std::string &channel, word16 value, ByteOrder order, bool blocking)
675 {
676  PutWord(false, order, m_buf, value);
677  return ChannelPut(channel, m_buf, 2, blocking);
678 }
679 
680 size_t BufferedTransformation::ChannelPutWord32(const std::string &channel, word32 value, ByteOrder order, bool blocking)
681 {
682  PutWord(false, order, m_buf, value);
683  return ChannelPut(channel, m_buf, 4, blocking);
684 }
685 
686 size_t BufferedTransformation::PutWord16(word16 value, ByteOrder order, bool blocking)
687 {
688  return ChannelPutWord16(DEFAULT_CHANNEL, value, order, blocking);
689 }
690 
691 size_t BufferedTransformation::PutWord32(word32 value, ByteOrder order, bool blocking)
692 {
693  return ChannelPutWord32(DEFAULT_CHANNEL, value, order, blocking);
694 }
695 
696 // Issue 340
697 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
698 # pragma GCC diagnostic push
699 # pragma GCC diagnostic ignored "-Wconversion"
700 # pragma GCC diagnostic ignored "-Wsign-conversion"
701 #endif
702 
703 size_t BufferedTransformation::PeekWord16(word16 &value, ByteOrder order) const
704 {
705  byte buf[2] = {0, 0};
706  size_t len = Peek(buf, 2);
707 
708  if (order)
709  value = (buf[0] << 8) | buf[1];
710  else
711  value = (buf[1] << 8) | buf[0];
712 
713  return len;
714 }
715 
716 size_t BufferedTransformation::PeekWord32(word32 &value, ByteOrder order) const
717 {
718  byte buf[4] = {0, 0, 0, 0};
719  size_t len = Peek(buf, 4);
720 
721  if (order)
722  value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf [3];
723  else
724  value = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf [0];
725 
726  return len;
727 }
728 
729 // Issue 340
730 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
731 # pragma GCC diagnostic pop
732 #endif
733 
734 size_t BufferedTransformation::GetWord16(word16 &value, ByteOrder order)
735 {
736  return (size_t)Skip(PeekWord16(value, order));
737 }
738 
739 size_t BufferedTransformation::GetWord32(word32 &value, ByteOrder order)
740 {
741  return (size_t)Skip(PeekWord32(value, order));
742 }
743 
745 {
746  if (AttachedTransformation() && AttachedTransformation()->Attachable())
747  AttachedTransformation()->Attach(newOut);
748  else
749  Detach(newOut);
750 }
751 
753 {
754  GenerateRandom(rng, MakeParameters("KeySize", (int)keySize));
755 }
756 
758 {
759 public:
760  PK_DefaultEncryptionFilter(RandomNumberGenerator &rng, const PK_Encryptor &encryptor, BufferedTransformation *attachment, const NameValuePairs &parameters)
761  : m_rng(rng), m_encryptor(encryptor), m_parameters(parameters)
762  {
763  Detach(attachment);
764  }
765 
766  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
767  {
768  FILTER_BEGIN;
769  m_plaintextQueue.Put(inString, length);
770 
771  if (messageEnd)
772  {
773  {
774  size_t plaintextLength;
775  if (!SafeConvert(m_plaintextQueue.CurrentSize(), plaintextLength))
776  throw InvalidArgument("PK_DefaultEncryptionFilter: plaintext too long");
777  size_t ciphertextLength = m_encryptor.CiphertextLength(plaintextLength);
778 
779  SecByteBlock plaintext(plaintextLength);
780  m_plaintextQueue.Get(plaintext, plaintextLength);
781  m_ciphertext.resize(ciphertextLength);
782  m_encryptor.Encrypt(m_rng, plaintext, plaintextLength, m_ciphertext, m_parameters);
783  }
784 
785  FILTER_OUTPUT(1, m_ciphertext, m_ciphertext.size(), messageEnd);
786  }
787  FILTER_END_NO_MESSAGE_END;
788  }
789 
790  RandomNumberGenerator &m_rng;
791  const PK_Encryptor &m_encryptor;
792  const NameValuePairs &m_parameters;
793  ByteQueue m_plaintextQueue;
794  SecByteBlock m_ciphertext;
795 };
796 
798 {
799  return new PK_DefaultEncryptionFilter(rng, *this, attachment, parameters);
800 }
801 
803 {
804 public:
805  PK_DefaultDecryptionFilter(RandomNumberGenerator &rng, const PK_Decryptor &decryptor, BufferedTransformation *attachment, const NameValuePairs &parameters)
806  : m_rng(rng), m_decryptor(decryptor), m_parameters(parameters)
807  {
808  Detach(attachment);
809  }
810 
811  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
812  {
813  FILTER_BEGIN;
814  m_ciphertextQueue.Put(inString, length);
815 
816  if (messageEnd)
817  {
818  {
819  size_t ciphertextLength;
820  if (!SafeConvert(m_ciphertextQueue.CurrentSize(), ciphertextLength))
821  throw InvalidArgument("PK_DefaultDecryptionFilter: ciphertext too long");
822  size_t maxPlaintextLength = m_decryptor.MaxPlaintextLength(ciphertextLength);
823 
824  SecByteBlock ciphertext(ciphertextLength);
825  m_ciphertextQueue.Get(ciphertext, ciphertextLength);
826  m_plaintext.resize(maxPlaintextLength);
827  m_result = m_decryptor.Decrypt(m_rng, ciphertext, ciphertextLength, m_plaintext, m_parameters);
828  if (!m_result.isValidCoding)
829  throw InvalidCiphertext(m_decryptor.AlgorithmName() + ": invalid ciphertext");
830  }
831 
832  FILTER_OUTPUT(1, m_plaintext, m_result.messageLength, messageEnd);
833  }
834  FILTER_END_NO_MESSAGE_END;
835  }
836 
837  RandomNumberGenerator &m_rng;
838  const PK_Decryptor &m_decryptor;
839  const NameValuePairs &m_parameters;
840  ByteQueue m_ciphertextQueue;
841  SecByteBlock m_plaintext;
842  DecodingResult m_result;
843 };
844 
846 {
847  return new PK_DefaultDecryptionFilter(rng, *this, attachment, parameters);
848 }
849 
850 size_t PK_Signer::Sign(RandomNumberGenerator &rng, PK_MessageAccumulator *messageAccumulator, byte *signature) const
851 {
852  member_ptr<PK_MessageAccumulator> m(messageAccumulator);
853  return SignAndRestart(rng, *m, signature, false);
854 }
855 
856 size_t PK_Signer::SignMessage(RandomNumberGenerator &rng, const byte *message, size_t messageLen, byte *signature) const
857 {
858  member_ptr<PK_MessageAccumulator> m(NewSignatureAccumulator(rng));
859  m->Update(message, messageLen);
860  return SignAndRestart(rng, *m, signature, false);
861 }
862 
863 size_t PK_Signer::SignMessageWithRecovery(RandomNumberGenerator &rng, const byte *recoverableMessage, size_t recoverableMessageLength,
864  const byte *nonrecoverableMessage, size_t nonrecoverableMessageLength, byte *signature) const
865 {
866  member_ptr<PK_MessageAccumulator> m(NewSignatureAccumulator(rng));
867  InputRecoverableMessage(*m, recoverableMessage, recoverableMessageLength);
868  m->Update(nonrecoverableMessage, nonrecoverableMessageLength);
869  return SignAndRestart(rng, *m, signature, false);
870 }
871 
872 bool PK_Verifier::Verify(PK_MessageAccumulator *messageAccumulator) const
873 {
874  member_ptr<PK_MessageAccumulator> m(messageAccumulator);
875  return VerifyAndRestart(*m);
876 }
877 
878 bool PK_Verifier::VerifyMessage(const byte *message, size_t messageLen, const byte *signature, size_t signatureLength) const
879 {
880  member_ptr<PK_MessageAccumulator> m(NewVerificationAccumulator());
881  InputSignature(*m, signature, signatureLength);
882  m->Update(message, messageLen);
883  return VerifyAndRestart(*m);
884 }
885 
886 DecodingResult PK_Verifier::Recover(byte *recoveredMessage, PK_MessageAccumulator *messageAccumulator) const
887 {
888  member_ptr<PK_MessageAccumulator> m(messageAccumulator);
889  return RecoverAndRestart(recoveredMessage, *m);
890 }
891 
893  const byte *nonrecoverableMessage, size_t nonrecoverableMessageLength,
894  const byte *signature, size_t signatureLength) const
895 {
896  member_ptr<PK_MessageAccumulator> m(NewVerificationAccumulator());
897  InputSignature(*m, signature, signatureLength);
898  m->Update(nonrecoverableMessage, nonrecoverableMessageLength);
899  return RecoverAndRestart(recoveredMessage, *m);
900 }
901 
902 void SimpleKeyAgreementDomain::GenerateKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const
903 {
904  GeneratePrivateKey(rng, privateKey);
905  GeneratePublicKey(rng, privateKey, publicKey);
906 }
907 
908 void AuthenticatedKeyAgreementDomain::GenerateStaticKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const
909 {
910  GenerateStaticPrivateKey(rng, privateKey);
911  GenerateStaticPublicKey(rng, privateKey, publicKey);
912 }
913 
914 void AuthenticatedKeyAgreementDomain::GenerateEphemeralKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const
915 {
916  GenerateEphemeralPrivateKey(rng, privateKey);
917  GenerateEphemeralPublicKey(rng, privateKey, publicKey);
918 }
919 
920 // Allow a distro or packager to override the build-time version
921 // http://github.com/weidai11/cryptopp/issues/371
922 #ifndef CRYPTOPP_BUILD_VERSION
923 # define CRYPTOPP_BUILD_VERSION CRYPTOPP_VERSION
924 #endif
925 int LibraryVersion(CRYPTOPP_NOINLINE_DOTDOTDOT)
926 {
927  return CRYPTOPP_BUILD_VERSION;
928 }
929 
930 // ***************** C++ Static Initialization ********************
931 // We can't put these in the anonymous namespace. DEFAULT_CHANNEL,
932 // AAD_CHANNEL and g_nullNameValuePairs must be defined in CryptoPP.
933 
934 #if HAVE_GCC_INIT_PRIORITY
935 const std::string DEFAULT_CHANNEL __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 10))) = "";
936 const std::string AAD_CHANNEL __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 11))) = "AAD";
937 const NullNameValuePairs s_nullNameValuePairs __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 12)));
938 const NameValuePairs &g_nullNameValuePairs = dynamic_cast<const NameValuePairs&>(s_nullNameValuePairs);
939 #elif HAVE_MSC_INIT_PRIORITY
940 #pragma warning(disable: 4075)
941 #pragma init_seg(".CRT$XCU-010")
942 const std::string DEFAULT_CHANNEL("");
943 const std::string AAD_CHANNEL("AAD");
944 const NullNameValuePairs s_nullNameValuePairs;
945 const NameValuePairs &g_nullNameValuePairs = dynamic_cast<const NameValuePairs&>(s_nullNameValuePairs);
946 #pragma warning(default: 4075)
947 #else
948 const std::string DEFAULT_CHANNEL = "";
949 const std::string AAD_CHANNEL = "AAD";
950 const simple_ptr<NullNameValuePairs> s_pNullNameValuePairs(new NullNameValuePairs);
951 const NameValuePairs &g_nullNameValuePairs = *s_pNullNameValuePairs.m_p;
952 #endif
953 
954 NAMESPACE_END // CryptoPP
955 
956 #endif // CRYPTOPP_IMPORTS
virtual void ProcessLastBlock(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt the last block of data.
Definition: cryptlib.cpp:216
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:29
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: cryptlib.cpp:49
Standard names for retrieving values by name when working with NameValuePairs.
const char * DigestSize()
int, in bytes
Definition: argnames.h:79
An invalid argument was detected.
Definition: cryptlib.h:194
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:444
virtual void GenerateKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const
Generate a private/public key pair.
Definition: cryptlib.cpp:902
size_t ChannelPutWord32(const std::string &channel, word32 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true)
Input a 32-bit word for processing on a channel.
Definition: cryptlib.cpp:680
container of wait objects
Definition: wait.h:169
size_t TransferAllTo2(BufferedTransformation &target, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
Transfer all bytes from this object to another BufferedTransformation.
Definition: cryptlib.cpp:625
Classes for working with NameValuePairs.
word32 GenerateWord32(word32 min, word32 max)
An implementation that throws NotImplemented.
Definition: cryptlib.cpp:347
bool SafeConvert(T1 from, T2 &to)
Tests whether a conversion from -> to is safe to perform.
Definition: misc.h:568
size_t TransferMessagesTo2(BufferedTransformation &target, unsigned int &messageCount, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
Transfer messages from this object to another BufferedTransformation.
Definition: cryptlib.cpp:576
Utility functions for the Crypto++ library.
virtual void SetKey(const byte *key, size_t length, const NameValuePairs &params=g_nullNameValuePairs)
Sets or reset the key of this object.
Definition: cryptlib.cpp:71
const char * Rounds()
int
Definition: argnames.h:24
virtual size_t Peek(byte &outByte) const
Peek a 8-bit byte.
Definition: cryptlib.cpp:506
ByteOrder
Provides the byte ordering.
Definition: cryptlib.h:134
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: cryptlib.cpp:298
size_t size() const
Length of the memory block.
Definition: algparam.h:93
size_t ChannelPut(const std::string &channel, byte inByte, bool blocking=true)
Input a byte for processing on a channel.
Definition: cryptlib.h:1843
virtual bool Verify(PK_MessageAccumulator *messageAccumulator) const
Check whether messageAccumulator contains a valid signature and message.
Definition: cryptlib.cpp:872
virtual size_t SignMessage(RandomNumberGenerator &rng, const byte *message, size_t messageLen, byte *signature) const
Sign a message.
Definition: cryptlib.cpp:856
unsigned int GetMaxWaitObjectCount() const
Retrieves the maximum number of waitable objects.
Definition: cryptlib.cpp:394
Exception thrown when an invalid key length is encountered.
Definition: simple.h:47
void PutWord(bool assumeAligned, ByteOrder order, byte *block, T value, const byte *xorBlock=NULL)
Access a block of memory.
Definition: misc.h:2235
virtual void GenerateStaticKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const
Generate a static private/public key pair.
Definition: cryptlib.cpp:908
Interface for public-key encryptors.
Definition: cryptlib.h:2310
byte GenerateByte()
An implementation that throws NotImplemented.
Definition: cryptlib.cpp:343
virtual word32 GenerateWord32(word32 min=0, word32 max=0xffffffffUL)
Generate a random 32 bit word in the range min to max, inclusive.
Definition: cryptlib.cpp:270
Abstract base classes that provide a uniform interface to this library.
Thrown when an unexpected type is encountered.
Definition: cryptlib.h:293
BufferedTransformation & TheBitBucket()
An input discarding BufferedTransformation.
Definition: cryptlib.cpp:53
void GenerateBlock(byte *output, size_t size)
An implementation that throws NotImplemented.
Definition: cryptlib.cpp:351
virtual void DiscardBytes(size_t n)
Generate and discard n bytes.
Definition: cryptlib.cpp:311
The self tests were executed via DoPowerUpSelfTest() or DoDllPowerUpSelfTest(), but the result was fa...
Definition: fips140.h:44
Classes for automatic resource management.
void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword length)
An implementation that throws NotImplemented.
Definition: cryptlib.cpp:359
Library configuration file.
Interface for random number generators.
Definition: cryptlib.h:1188
virtual bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
Marks the end of a series of messages, with signal propagation.
Definition: cryptlib.cpp:421
void SetKeyWithRounds(const byte *key, size_t length, int rounds)
Sets or reset the key of this object.
Definition: cryptlib.cpp:77
virtual void GenerateEphemeralKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const
Generate private/public key pair.
Definition: cryptlib.cpp:914
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:452
virtual bool TruncatedVerify(const byte *digest, size_t digestLength)
Verifies the hash of the current message.
Definition: cryptlib.cpp:380
SecBlock<byte> typedef.
Definition: secblock.h:810
virtual unsigned int SkipMessages(unsigned int count=UINT_MAX)
Skip a number of meessages.
Definition: cryptlib.cpp:568
virtual void SetRetrievalChannel(const std::string &channel)
Sets the default retrieval channel.
Definition: cryptlib.cpp:668
Interface for buffered transformations.
Definition: cryptlib.h:1343
virtual unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: cryptlib.cpp:201
void CopyAllTo(BufferedTransformation &target, const std::string &channel=DEFAULT_CHANNEL) const
Copy messages from this object to another BufferedTransformation.
Definition: cryptlib.cpp:657
Interface for cloning objects.
Definition: cryptlib.h:488
virtual size_t SignMessageWithRecovery(RandomNumberGenerator &rng, const byte *recoverableMessage, size_t recoverableMessageLength, const byte *nonrecoverableMessage, size_t nonrecoverableMessageLength, byte *signature) const
Sign a recoverable message.
Definition: cryptlib.cpp:863
std::string AlgorithmName() const
The name of the generator.
Definition: cryptlib.cpp:339
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: cryptlib.cpp:811
Classes and functions for secure memory allocations.
bool FIPS_140_2_ComplianceEnabled()
Determines whether the library provides FIPS validated cryptography.
Definition: fips140.cpp:29
Copy input to a memory buffer.
Definition: filters.h:1126
Exception thrown when a filter does not support named channels.
Definition: cryptlib.h:1831
Returns a decoding results.
Definition: cryptlib.h:248
Algorithm(bool checkSelfTestStatus=true)
Interface for all crypto algorithms.
Definition: cryptlib.cpp:59
size_t PeekWord32(word32 &value, ByteOrder order=BIG_ENDIAN_ORDER) const
Peek a 32-bit word.
Definition: cryptlib.cpp:716
virtual void Attach(BufferedTransformation *newAttachment)
Add newAttachment to the end of attachment chain.
Definition: cryptlib.cpp:744
Interface for public-key decryptors.
Definition: cryptlib.h:2346
void Shuffle(IT begin, IT end)
An implementation that does nothing.
Definition: cryptlib.cpp:367
A method was called which was not implemented.
Definition: cryptlib.h:215
const byte * begin() const
Pointer to the first byte in the memory block.
Definition: algparam.h:89
const std::string DEFAULT_CHANNEL
Default channel for BufferedTransformation.
Definition: cryptlib.cpp:948
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:502
virtual unsigned int NumberOfMessages() const
Provides the number of meesages processed by this object.
Definition: cryptlib.cpp:541
Manages resources for a single object.
Definition: smartptr.h:20
virtual lword TotalBytesRetrievable() const
Provides the number of bytes ready for retrieval.
Definition: cryptlib.cpp:533
Exception thrown when a crypto algorithm is used after a self test fails.
Definition: fips140.h:23
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:460
virtual DecodingResult RecoverMessage(byte *recoveredMessage, const byte *nonrecoverableMessage, size_t nonrecoverableMessageLength, const byte *signature, size_t signatureLength) const
Recover a message from its signature.
Definition: cryptlib.cpp:892
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
Definition: misc.h:159
virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition: cryptlib.cpp:414
virtual byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
Request space which can be written into by the caller.
Definition: cryptlib.cpp:428
T Crop(T value, size_t bits)
Truncates the value to the specified number of bits.
Definition: misc.h:784
virtual size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const
Encrypt and xor multiple blocks using additional flags.
Definition: cryptlib.cpp:152
virtual bool VerifyMessage(const byte *message, size_t messageLen, const byte *signature, size_t signatureLen) const
Check whether input signature is a valid signature for input message.
Definition: cryptlib.cpp:878
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
Definition: misc.h:552
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:436
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
Definition: cryptlib.cpp:951
unsigned int GenerateBit()
An implementation that throws NotImplemented.
Definition: cryptlib.cpp:345
virtual BufferedTransformation * CreateDecryptionFilter(RandomNumberGenerator &rng, BufferedTransformation *attachment=NULL, const NameValuePairs &parameters=g_nullNameValuePairs) const
Create a new decryption filter.
Definition: cryptlib.cpp:845
virtual unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: cryptlib.cpp:206
virtual lword Skip(lword skipMax=LWORD_MAX)
Discard skipMax bytes from the output buffer.
Definition: cryptlib.cpp:525
RandomNumberGenerator & NullRNG()
Random Number Generator that does not produce random numbers.
Definition: cryptlib.cpp:374
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:60
virtual byte GenerateByte()
Generate new random byte and return it.
Definition: cryptlib.cpp:263
virtual bool AnyMessages() const
Determines if any messages are available for retrieval.
Definition: cryptlib.cpp:549
Data structure used to store byte strings.
Definition: queue.h:19
const char * BlockSize()
int, in bytes
Definition: argnames.h:27
virtual bool AnyRetrievable() const
Determines whether bytes are ready for retrieval.
Definition: cryptlib.cpp:476
size_t GetWord16(word16 &value, ByteOrder order=BIG_ENDIAN_ORDER)
Retrieve a 16-bit word.
Definition: cryptlib.cpp:734
PowerUpSelfTestStatus GetPowerUpSelfTestStatus()
Provides the current power-up self test status.
Definition: fips140.cpp:39
void GetWaitObjects(WaitObjectContainer &container, CallStack const &callStack)
Retrieves waitable objects.
Definition: cryptlib.cpp:400
Random Number Generator that does not produce random numbers.
Definition: cryptlib.cpp:334
virtual unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: cryptlib.cpp:211
Implementation of BufferedTransformation&#39;s attachment interface.
const char * IV()
ConstByteArrayParameter, also accepts const byte * for backwards compatibility.
Definition: argnames.h:21
The self tests have not been performed.
Definition: fips140.h:41
Interface for accumulating messages to be signed or verified.
Definition: cryptlib.h:2478
unsigned int CopyMessagesTo(BufferedTransformation &target, unsigned int count=UINT_MAX, const std::string &channel=DEFAULT_CHANNEL) const
Copy messages from this object to another BufferedTransformation.
Definition: cryptlib.cpp:606
A decryption filter encountered invalid ciphertext.
Definition: cryptlib.h:208
size_t PutWord32(word32 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true)
Input a 32-bit word for processing.
Definition: cryptlib.cpp:691
void SetKeyWithIV(const byte *key, size_t length, const byte *iv, size_t ivLength)
Sets or reset the key of this object.
Definition: cryptlib.cpp:82
virtual lword MaxRetrievable() const
Provides the number of bytes ready for retrieval.
Definition: cryptlib.cpp:468
size_t GetWord32(word32 &value, ByteOrder order=BIG_ENDIAN_ORDER)
Retrieve a 32-bit word.
Definition: cryptlib.cpp:739
virtual unsigned int GenerateBit()
Generate new random bit and return it.
Definition: cryptlib.cpp:258
Base class for unflushable filters.
Definition: simple.h:102
virtual size_t Sign(RandomNumberGenerator &rng, PK_MessageAccumulator *messageAccumulator, byte *signature) const
Sign and delete the messageAccumulator.
Definition: cryptlib.cpp:850
virtual BufferedTransformation * CreateEncryptionFilter(RandomNumberGenerator &rng, BufferedTransformation *attachment=NULL, const NameValuePairs &parameters=g_nullNameValuePairs) const
Create a new encryption filter.
Definition: cryptlib.cpp:797
Classes and functions for the FIPS 140-2 validated library.
virtual void EncryptAndAuthenticate(byte *ciphertext, byte *mac, size_t macSize, const byte *iv, int ivLength, const byte *header, size_t headerLength, const byte *message, size_t messageLength)
Encrypts and calculates a MAC in one call.
Definition: cryptlib.cpp:240
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 DecryptAndVerify(byte *message, const byte *mac, size_t macLength, const byte *iv, int ivLength, const byte *header, size_t headerLength, const byte *ciphertext, size_t ciphertextLength)
Decrypts and verifies a MAC in one call.
Definition: cryptlib.cpp:249
virtual void SkipAll()
Skip all messages in the series.
Definition: cryptlib.cpp:614
void GenerateRandomWithKeySize(RandomNumberGenerator &rng, unsigned int keySize)
Generate a random key or crypto parameters.
Definition: cryptlib.cpp:752
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:582
size_t PutWord16(word16 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true)
Input a 16-bit word for processing.
Definition: cryptlib.cpp:686
bool VerifyBufsEqual(const byte *buf1, const byte *buf2, size_t count)
Performs a near constant-time comparison of two equally sized buffers.
Definition: misc.cpp:96
virtual bool GetNextMessage()
Start retrieving the next message.
Definition: cryptlib.cpp:557
size_t PeekWord16(word16 &value, ByteOrder order=BIG_ENDIAN_ORDER) const
Peek a 16-bit word.
Definition: cryptlib.cpp:703
void SpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength=0)
Prespecifies the data lengths.
Definition: cryptlib.cpp:226
Acts as an input discarding Filter or Sink.
Definition: simple.h:353
virtual size_t Get(byte &outByte)
Retrieve a 8-bit byte.
Definition: cryptlib.cpp:487
int LibraryVersion(...)
Specifies the build-time version of the library.
Definition: cryptlib.cpp:925
Crypto++ library namespace.
bool GetValue(const char *name, T &value) const
Get a named value.
Definition: cryptlib.h:343
virtual DecodingResult Recover(byte *recoveredMessage, PK_MessageAccumulator *messageAccumulator) const
Recover a message from its signature.
Definition: cryptlib.cpp:886
bool CanIncorporateEntropy() const
An implementation that returns false.
Definition: cryptlib.cpp:363
void IncorporateEntropy(const byte *input, size_t length)
An implementation that throws NotImplemented.
Definition: cryptlib.cpp:361
const std::string AAD_CHANNEL
Channel for additional authenticated data.
Definition: cryptlib.cpp:949
virtual void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1)
Initialize or reinitialize this object, with signal propagation.
Definition: cryptlib.cpp:407
size_t ChannelPutWord16(const std::string &channel, word16 value, ByteOrder order=BIG_ENDIAN_ORDER, bool blocking=true)
Input a 16-bit word for processing on a channel.
Definition: cryptlib.cpp:674
void DiscardBytes(size_t n)
An implementation that does nothing.
Definition: cryptlib.cpp:365
virtual void GetNextIV(RandomNumberGenerator &rng, byte *iv)
Retrieves a secure IV for the next message.
Definition: cryptlib.cpp:147
virtual void Update(const byte *input, size_t length)=0
Updates a hash with additional input.
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.
Definition: misc.h:700
size_type size() const
Provides the count of elements in the SecBlock.
Definition: secblock.h:570
virtual void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword length)
Generate random bytes into a BufferedTransformation.
Definition: cryptlib.cpp:316
Classes for access to the operating system&#39;s random number generators.
bool ChannelMessageEnd(const std::string &channel, int propagation=-1, bool blocking=true)
Signal the end of a message.
Definition: cryptlib.h:1892
Interface for retrieving values given their names.
Definition: cryptlib.h:285
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: cryptlib.cpp:766