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