File indexing completed on 2025-01-18 09:55:03
0001
0002
0003
0004
0005
0006 #ifndef CRYPTOPP_FILTERS_H
0007 #define CRYPTOPP_FILTERS_H
0008
0009 #include "config.h"
0010
0011 #if CRYPTOPP_MSC_VERSION
0012 # pragma warning(push)
0013 # pragma warning(disable: 4127 4189 4231 4275 4514)
0014 #endif
0015
0016 #include "cryptlib.h"
0017 #include "simple.h"
0018 #include "secblock.h"
0019 #include "misc.h"
0020 #include "smartptr.h"
0021 #include "queue.h"
0022 #include "algparam.h"
0023 #include "stdcpp.h"
0024
0025 NAMESPACE_BEGIN(CryptoPP)
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Filter : public BufferedTransformation, public NotCopyable
0036 {
0037 public:
0038 virtual ~Filter() {}
0039
0040
0041
0042
0043
0044
0045
0046 Filter(BufferedTransformation *attachment = NULLPTR);
0047
0048
0049
0050
0051 bool Attachable() {return true;}
0052
0053
0054
0055 BufferedTransformation *AttachedTransformation();
0056
0057
0058
0059 const BufferedTransformation *AttachedTransformation() const;
0060
0061
0062
0063
0064
0065 void Detach(BufferedTransformation *newAttachment = NULLPTR);
0066
0067
0068
0069
0070
0071
0072
0073 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
0074 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
0075
0076
0077
0078
0079
0080
0081
0082 void Initialize(const NameValuePairs ¶meters=g_nullNameValuePairs, int propagation=-1);
0083 bool Flush(bool hardFlush, int propagation=-1, bool blocking=true);
0084 bool MessageSeriesEnd(int propagation=-1, bool blocking=true);
0085
0086
0087
0088 protected:
0089 virtual BufferedTransformation * NewDefaultAttachment() const;
0090 void Insert(Filter *nextFilter);
0091
0092 virtual bool ShouldPropagateMessageEnd() const {return true;}
0093 virtual bool ShouldPropagateMessageSeriesEnd() const {return true;}
0094
0095 void PropagateInitialize(const NameValuePairs ¶meters, int propagation);
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106 size_t Output(int outputSite, const byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel=DEFAULT_CHANNEL);
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117 size_t OutputModifiable(int outputSite, byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel=DEFAULT_CHANNEL);
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127 bool OutputMessageEnd(int outputSite, int propagation, bool blocking, const std::string &channel=DEFAULT_CHANNEL);
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146 bool OutputFlush(int outputSite, bool hardFlush, int propagation, bool blocking, const std::string &channel=DEFAULT_CHANNEL);
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159 bool OutputMessageSeriesEnd(int outputSite, int propagation, bool blocking, const std::string &channel=DEFAULT_CHANNEL);
0160
0161 private:
0162 member_ptr<BufferedTransformation> m_attachment;
0163
0164 protected:
0165 size_t m_inputPosition;
0166 int m_continueAt;
0167 };
0168
0169
0170 struct CRYPTOPP_DLL FilterPutSpaceHelper
0171 {
0172 virtual ~FilterPutSpaceHelper() {}
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187 byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize, size_t desiredSize, size_t &bufferSize)
0188 {
0189 CRYPTOPP_ASSERT(desiredSize >= minSize && bufferSize >= minSize);
0190 if (m_tempSpace.size() < minSize)
0191 {
0192 byte *result = target.ChannelCreatePutSpace(channel, desiredSize);
0193 if (desiredSize >= minSize)
0194 {
0195 bufferSize = desiredSize;
0196 return result;
0197 }
0198 m_tempSpace.New(bufferSize);
0199 }
0200
0201 bufferSize = m_tempSpace.size();
0202 return m_tempSpace.begin();
0203 }
0204
0205
0206
0207
0208
0209
0210
0211
0212 byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize)
0213 {return HelpCreatePutSpace(target, channel, minSize, minSize, minSize);}
0214
0215
0216
0217
0218
0219
0220
0221
0222 byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize, size_t bufferSize)
0223 {return HelpCreatePutSpace(target, channel, minSize, minSize, bufferSize);}
0224
0225
0226 SecByteBlock m_tempSpace;
0227 };
0228
0229
0230
0231
0232 class CRYPTOPP_DLL MeterFilter : public Bufferless<Filter>
0233 {
0234 public:
0235 virtual ~MeterFilter() {}
0236
0237
0238
0239
0240
0241
0242 MeterFilter(BufferedTransformation *attachment=NULLPTR, bool transparent=true)
0243 : m_transparent(transparent), m_currentMessageBytes(0), m_totalBytes(0)
0244 , m_currentSeriesMessages(0), m_totalMessages(0), m_totalMessageSeries(0)
0245 , m_begin(NULLPTR), m_length(0) {Detach(attachment); ResetMeter();}
0246
0247
0248
0249 void SetTransparent(bool transparent) {m_transparent = transparent;}
0250
0251
0252
0253
0254
0255
0256
0257
0258 void AddRangeToSkip(unsigned int message, lword position, lword size, bool sortNow = true);
0259
0260
0261
0262
0263 void ResetMeter();
0264
0265
0266 void IsolatedInitialize(const NameValuePairs ¶meters)
0267 {CRYPTOPP_UNUSED(parameters); ResetMeter();}
0268
0269
0270
0271 lword GetCurrentMessageBytes() const {return m_currentMessageBytes;}
0272
0273
0274
0275 lword GetTotalBytes() const {return m_totalBytes;}
0276
0277
0278
0279 unsigned int GetCurrentSeriesMessages() const {return m_currentSeriesMessages;}
0280
0281
0282
0283 unsigned int GetTotalMessages() const {return m_totalMessages;}
0284
0285
0286
0287 unsigned int GetTotalMessageSeries() const {return m_totalMessageSeries;}
0288
0289
0290 byte * CreatePutSpace(size_t &size) {return AttachedTransformation()->CreatePutSpace(size);}
0291 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
0292 size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking);
0293 bool IsolatedMessageSeriesEnd(bool blocking);
0294
0295 private:
0296 size_t PutMaybeModifiable(byte *inString, size_t length, int messageEnd, bool blocking, bool modifiable);
0297 bool ShouldPropagateMessageEnd() const {return m_transparent;}
0298 bool ShouldPropagateMessageSeriesEnd() const {return m_transparent;}
0299
0300 struct MessageRange
0301 {
0302 inline bool operator<(const MessageRange &b) const
0303 {return message < b.message || (message == b.message && position < b.position);}
0304 unsigned int message; lword position; lword size;
0305 };
0306
0307 bool m_transparent;
0308 lword m_currentMessageBytes, m_totalBytes;
0309 unsigned int m_currentSeriesMessages, m_totalMessages, m_totalMessageSeries;
0310 std::deque<MessageRange> m_rangesToSkip;
0311 byte *m_begin;
0312 size_t m_length;
0313 };
0314
0315
0316
0317 class CRYPTOPP_DLL TransparentFilter : public MeterFilter
0318 {
0319 public:
0320
0321
0322 TransparentFilter(BufferedTransformation *attachment=NULLPTR) : MeterFilter(attachment, true) {}
0323 };
0324
0325
0326
0327 class CRYPTOPP_DLL OpaqueFilter : public MeterFilter
0328 {
0329 public:
0330
0331
0332 OpaqueFilter(BufferedTransformation *attachment=NULLPTR) : MeterFilter(attachment, false) {}
0333 };
0334
0335
0336
0337
0338
0339
0340
0341 class CRYPTOPP_DLL FilterWithBufferedInput : public Filter
0342 {
0343 public:
0344 virtual ~FilterWithBufferedInput() {}
0345
0346
0347
0348 FilterWithBufferedInput(BufferedTransformation *attachment);
0349
0350
0351
0352
0353
0354
0355
0356 FilterWithBufferedInput(size_t firstSize, size_t blockSize, size_t lastSize, BufferedTransformation *attachment);
0357
0358 void IsolatedInitialize(const NameValuePairs ¶meters);
0359 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
0360 {
0361 return PutMaybeModifiable(const_cast<byte *>(inString), length, messageEnd, blocking, false);
0362 }
0363
0364 size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
0365 {
0366 return PutMaybeModifiable(inString, length, messageEnd, blocking, true);
0367 }
0368
0369
0370
0371
0372
0373
0374
0375 bool IsolatedFlush(bool hardFlush, bool blocking);
0376
0377
0378
0379
0380 void ForceNextPut();
0381
0382 protected:
0383 virtual bool DidFirstPut() const {return m_firstInputDone;}
0384 virtual size_t GetFirstPutSize() const {return m_firstSize;}
0385 virtual size_t GetBlockPutSize() const {return m_blockSize;}
0386 virtual size_t GetLastPutSize() const {return m_lastSize;}
0387
0388 virtual void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
0389 {CRYPTOPP_UNUSED(parameters); CRYPTOPP_UNUSED(firstSize); CRYPTOPP_UNUSED(blockSize); CRYPTOPP_UNUSED(lastSize); InitializeDerived(parameters);}
0390 virtual void InitializeDerived(const NameValuePairs ¶meters)
0391 {CRYPTOPP_UNUSED(parameters);}
0392
0393
0394
0395 virtual void FirstPut(const byte *inString) =0;
0396
0397 virtual void NextPutSingle(const byte *inString)
0398 {CRYPTOPP_UNUSED(inString); CRYPTOPP_ASSERT(false);}
0399
0400
0401 virtual void NextPutMultiple(const byte *inString, size_t length);
0402
0403 virtual void NextPutModifiable(byte *inString, size_t length)
0404 {NextPutMultiple(inString, length);}
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415 virtual void LastPut(const byte *inString, size_t length) =0;
0416 virtual void FlushDerived() {}
0417
0418 protected:
0419 size_t PutMaybeModifiable(byte *begin, size_t length, int messageEnd, bool blocking, bool modifiable);
0420 void NextPutMaybeModifiable(byte *inString, size_t length, bool modifiable)
0421 {
0422 if (modifiable) NextPutModifiable(inString, length);
0423 else NextPutMultiple(inString, length);
0424 }
0425
0426
0427
0428 virtual int NextPut(const byte *inString, size_t length)
0429 {CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length); CRYPTOPP_ASSERT(false); return 0;}
0430
0431 class BlockQueue
0432 {
0433 public:
0434 void ResetQueue(size_t blockSize, size_t maxBlocks);
0435 byte *GetBlock();
0436 byte *GetContigousBlocks(size_t &numberOfBytes);
0437 size_t GetAll(byte *outString);
0438 void Put(const byte *inString, size_t length);
0439 size_t CurrentSize() const {return m_size;}
0440 size_t MaxSize() const {return m_buffer.size();}
0441
0442 private:
0443 SecByteBlock m_buffer;
0444 size_t m_blockSize, m_maxBlocks, m_size;
0445 byte *m_begin;
0446 };
0447
0448 size_t m_firstSize, m_blockSize, m_lastSize;
0449 bool m_firstInputDone;
0450 BlockQueue m_queue;
0451 };
0452
0453
0454
0455
0456
0457 class CRYPTOPP_DLL FilterWithInputQueue : public Filter
0458 {
0459 public:
0460 virtual ~FilterWithInputQueue() {}
0461
0462
0463
0464 FilterWithInputQueue(BufferedTransformation *attachment=NULLPTR) : Filter(attachment) {}
0465
0466 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
0467 {
0468 if (!blocking)
0469 throw BlockingInputOnly("FilterWithInputQueue");
0470
0471 m_inQueue.Put(inString, length);
0472 if (messageEnd)
0473 {
0474 IsolatedMessageEnd(blocking);
0475 Output(0, NULLPTR, 0, messageEnd, blocking);
0476 }
0477 return 0;
0478 }
0479
0480 protected:
0481 virtual bool IsolatedMessageEnd(bool blocking) =0;
0482 void IsolatedInitialize(const NameValuePairs ¶meters)
0483 {CRYPTOPP_UNUSED(parameters); m_inQueue.Clear();}
0484
0485 ByteQueue m_inQueue;
0486 };
0487
0488
0489
0490
0491 struct BlockPaddingSchemeDef
0492 {
0493
0494
0495
0496
0497
0498
0499
0500
0501 enum BlockPaddingScheme {
0502
0503
0504 NO_PADDING,
0505
0506
0507 ZEROS_PADDING,
0508
0509
0510 PKCS_PADDING,
0511
0512
0513 ONE_AND_ZEROS_PADDING,
0514
0515
0516
0517
0518 W3C_PADDING,
0519
0520
0521 DEFAULT_PADDING
0522 };
0523 };
0524
0525
0526
0527
0528
0529
0530
0531 class CRYPTOPP_DLL StreamTransformationFilter : public FilterWithBufferedInput, public BlockPaddingSchemeDef, private FilterPutSpaceHelper
0532 {
0533 public:
0534 virtual ~StreamTransformationFilter() {}
0535
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545 StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment = NULLPTR, BlockPaddingScheme padding = DEFAULT_PADDING);
0546
0547 std::string AlgorithmName() const {return m_cipher.AlgorithmName();}
0548
0549 protected:
0550
0551 friend class AuthenticatedEncryptionFilter;
0552 friend class AuthenticatedDecryptionFilter;
0553
0554
0555
0556
0557
0558
0559
0560
0561 StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment, BlockPaddingScheme padding, bool authenticated);
0562
0563 void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
0564 void FirstPut(const byte *inString);
0565 void NextPutMultiple(const byte *inString, size_t length);
0566 void NextPutModifiable(byte *inString, size_t length);
0567 void LastPut(const byte *inString, size_t length);
0568
0569 static size_t LastBlockSize(StreamTransformation &c, BlockPaddingScheme padding);
0570
0571 StreamTransformation &m_cipher;
0572 BlockPaddingScheme m_padding;
0573 unsigned int m_mandatoryBlockSize;
0574 unsigned int m_optimalBufferSize;
0575 unsigned int m_reservedBufferSize;
0576 bool m_isSpecial;
0577 };
0578
0579
0580
0581 class CRYPTOPP_DLL HashFilter : public Bufferless<Filter>, private FilterPutSpaceHelper
0582 {
0583 public:
0584 virtual ~HashFilter() {}
0585
0586
0587
0588
0589
0590
0591
0592
0593 HashFilter(HashTransformation &hm, BufferedTransformation *attachment = NULLPTR, bool putMessage=false, int truncatedDigestSize=-1, const std::string &messagePutChannel=DEFAULT_CHANNEL, const std::string &hashPutChannel=DEFAULT_CHANNEL);
0594
0595 std::string AlgorithmName() const {return m_hashModule.AlgorithmName();}
0596 void IsolatedInitialize(const NameValuePairs ¶meters);
0597 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
0598 byte * CreatePutSpace(size_t &size) {return m_hashModule.CreateUpdateSpace(size);}
0599
0600 private:
0601 HashTransformation &m_hashModule;
0602 bool m_putMessage;
0603 unsigned int m_digestSize;
0604 byte *m_space;
0605 std::string m_messagePutChannel, m_hashPutChannel;
0606 };
0607
0608
0609
0610 class CRYPTOPP_DLL HashVerificationFilter : public FilterWithBufferedInput
0611 {
0612 public:
0613 virtual ~HashVerificationFilter() {}
0614
0615
0616 class HashVerificationFailed : public Exception
0617 {
0618 public:
0619 HashVerificationFailed()
0620 : Exception(DATA_INTEGRITY_CHECK_FAILED, "HashVerificationFilter: message hash or MAC not valid") {}
0621 };
0622
0623
0624
0625
0626 enum Flags {
0627
0628 HASH_AT_END=0,
0629
0630 HASH_AT_BEGIN=1,
0631
0632 PUT_MESSAGE=2,
0633
0634 PUT_HASH=4,
0635
0636 PUT_RESULT=8,
0637
0638 THROW_EXCEPTION=16,
0639
0640 DEFAULT_FLAGS = HASH_AT_BEGIN | PUT_RESULT
0641 };
0642
0643
0644
0645
0646
0647
0648
0649 HashVerificationFilter(HashTransformation &hm, BufferedTransformation *attachment = NULLPTR, word32 flags = DEFAULT_FLAGS, int truncatedDigestSize=-1);
0650
0651 std::string AlgorithmName() const {return m_hashModule.AlgorithmName();}
0652 bool GetLastResult() const {return m_verified;}
0653
0654 protected:
0655 void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
0656 void FirstPut(const byte *inString);
0657 void NextPutMultiple(const byte *inString, size_t length);
0658 void LastPut(const byte *inString, size_t length);
0659
0660 private:
0661 friend class AuthenticatedDecryptionFilter;
0662
0663 HashTransformation &m_hashModule;
0664 word32 m_flags;
0665 unsigned int m_digestSize;
0666 bool m_verified;
0667 SecByteBlock m_expectedHash;
0668 };
0669
0670
0671
0672
0673
0674
0675
0676
0677
0678
0679
0680
0681
0682 class CRYPTOPP_DLL AuthenticatedEncryptionFilter : public StreamTransformationFilter
0683 {
0684 public:
0685 virtual ~AuthenticatedEncryptionFilter() {}
0686
0687
0688
0689
0690
0691
0692
0693
0694
0695
0696 AuthenticatedEncryptionFilter(AuthenticatedSymmetricCipher &c, BufferedTransformation *attachment = NULLPTR, bool putAAD=false, int truncatedDigestSize=-1, const std::string &macChannel=DEFAULT_CHANNEL, BlockPaddingScheme padding = DEFAULT_PADDING);
0697
0698 void IsolatedInitialize(const NameValuePairs ¶meters);
0699 byte * ChannelCreatePutSpace(const std::string &channel, size_t &size);
0700 size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking);
0701
0702
0703
0704
0705
0706
0707
0708
0709
0710
0711
0712 void LastPut(const byte *inString, size_t length);
0713
0714 protected:
0715 HashFilter m_hf;
0716 };
0717
0718
0719
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731 class CRYPTOPP_DLL AuthenticatedDecryptionFilter : public FilterWithBufferedInput, public BlockPaddingSchemeDef
0732 {
0733 public:
0734
0735
0736
0737 enum Flags {
0738
0739 MAC_AT_END=0,
0740
0741 MAC_AT_BEGIN=1,
0742
0743 THROW_EXCEPTION=16,
0744
0745 DEFAULT_FLAGS = THROW_EXCEPTION
0746 };
0747
0748 virtual ~AuthenticatedDecryptionFilter() {}
0749
0750
0751
0752
0753
0754
0755
0756
0757
0758
0759 AuthenticatedDecryptionFilter(AuthenticatedSymmetricCipher &c, BufferedTransformation *attachment = NULLPTR, word32 flags = DEFAULT_FLAGS, int truncatedDigestSize=-1, BlockPaddingScheme padding = DEFAULT_PADDING);
0760
0761 std::string AlgorithmName() const {return m_hashVerifier.AlgorithmName();}
0762 byte * ChannelCreatePutSpace(const std::string &channel, size_t &size);
0763 size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking);
0764 size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
0765 { return ChannelPut2(channel, begin, length, messageEnd, blocking); }
0766
0767
0768 bool GetLastResult() const {return m_hashVerifier.GetLastResult();}
0769
0770 protected:
0771 void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
0772 void FirstPut(const byte *inString);
0773 void NextPutMultiple(const byte *inString, size_t length);
0774
0775
0776
0777
0778
0779
0780
0781
0782
0783
0784
0785 void LastPut(const byte *inString, size_t length);
0786
0787 HashVerificationFilter m_hashVerifier;
0788 StreamTransformationFilter m_streamFilter;
0789 };
0790
0791
0792
0793 class CRYPTOPP_DLL SignerFilter : public Unflushable<Filter>
0794 {
0795 public:
0796 virtual ~SignerFilter() {}
0797
0798
0799
0800
0801
0802
0803 SignerFilter(RandomNumberGenerator &rng, const PK_Signer &signer, BufferedTransformation *attachment = NULLPTR, bool putMessage=false)
0804 : m_rng(rng), m_signer(signer), m_messageAccumulator(signer.NewSignatureAccumulator(rng)), m_putMessage(putMessage) {Detach(attachment);}
0805
0806 std::string AlgorithmName() const {return m_signer.AlgorithmName();}
0807
0808 void IsolatedInitialize(const NameValuePairs ¶meters);
0809 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
0810
0811 private:
0812 RandomNumberGenerator &m_rng;
0813 const PK_Signer &m_signer;
0814 member_ptr<PK_MessageAccumulator> m_messageAccumulator;
0815 bool m_putMessage;
0816 SecByteBlock m_buf;
0817 };
0818
0819
0820
0821
0822 class CRYPTOPP_DLL SignatureVerificationFilter : public FilterWithBufferedInput
0823 {
0824 public:
0825
0826 class SignatureVerificationFailed : public Exception
0827 {
0828 public:
0829 SignatureVerificationFailed()
0830 : Exception(DATA_INTEGRITY_CHECK_FAILED, "VerifierFilter: digital signature not valid") {}
0831 };
0832
0833
0834
0835
0836 enum Flags {
0837
0838 SIGNATURE_AT_END=0,
0839
0840 SIGNATURE_AT_BEGIN=1,
0841
0842 PUT_MESSAGE=2,
0843
0844 PUT_SIGNATURE=4,
0845
0846 PUT_RESULT=8,
0847
0848 THROW_EXCEPTION=16,
0849
0850 DEFAULT_FLAGS = SIGNATURE_AT_BEGIN | PUT_RESULT
0851 };
0852
0853 virtual ~SignatureVerificationFilter() {}
0854
0855
0856
0857
0858
0859 SignatureVerificationFilter(const PK_Verifier &verifier, BufferedTransformation *attachment = NULLPTR, word32 flags = DEFAULT_FLAGS);
0860
0861 std::string AlgorithmName() const {return m_verifier.AlgorithmName();}
0862
0863
0864
0865 bool GetLastResult() const {return m_verified;}
0866
0867 protected:
0868 void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
0869 void FirstPut(const byte *inString);
0870 void NextPutMultiple(const byte *inString, size_t length);
0871 void LastPut(const byte *inString, size_t length);
0872
0873 private:
0874 const PK_Verifier &m_verifier;
0875 member_ptr<PK_MessageAccumulator> m_messageAccumulator;
0876 word32 m_flags;
0877 SecByteBlock m_signature;
0878 bool m_verified;
0879 };
0880
0881
0882
0883 class CRYPTOPP_DLL Redirector : public CustomSignalPropagation<Sink>
0884 {
0885 public:
0886
0887
0888 enum Behavior
0889 {
0890
0891 DATA_ONLY = 0x00,
0892
0893 PASS_SIGNALS = 0x01,
0894
0895 PASS_WAIT_OBJECTS = 0x02,
0896
0897
0898 PASS_EVERYTHING = PASS_SIGNALS | PASS_WAIT_OBJECTS
0899 };
0900
0901 virtual ~Redirector() {}
0902
0903
0904 Redirector() : m_target(NULLPTR), m_behavior(PASS_EVERYTHING) {}
0905
0906
0907
0908
0909 Redirector(BufferedTransformation &target, Behavior behavior=PASS_EVERYTHING)
0910 : m_target(&target), m_behavior(behavior) {}
0911
0912
0913
0914 void Redirect(BufferedTransformation &target) {m_target = ⌖}
0915
0916 void StopRedirection() {m_target = NULLPTR;}
0917
0918
0919
0920 Behavior GetBehavior() {return static_cast<Behavior>(m_behavior);}
0921
0922
0923 void SetBehavior(Behavior behavior) {m_behavior=behavior;}
0924
0925
0926 bool GetPassSignals() const {return (m_behavior & PASS_SIGNALS) != 0;}
0927
0928
0929 void SetPassSignals(bool pass) { if (pass) m_behavior |= PASS_SIGNALS; else m_behavior &= ~static_cast<word32>(PASS_SIGNALS); }
0930
0931
0932 bool GetPassWaitObjects() const {return (m_behavior & PASS_WAIT_OBJECTS) != 0;}
0933
0934
0935 void SetPassWaitObjects(bool pass) { if (pass) m_behavior |= PASS_WAIT_OBJECTS; else m_behavior &= ~static_cast<word32>(PASS_WAIT_OBJECTS); }
0936
0937 bool CanModifyInput() const
0938 {return m_target ? m_target->CanModifyInput() : false;}
0939
0940 void Initialize(const NameValuePairs ¶meters, int propagation);
0941 byte * CreatePutSpace(size_t &size)
0942 {
0943 if (m_target)
0944 return m_target->CreatePutSpace(size);
0945 else
0946 {
0947 size = 0;
0948 return NULLPTR;
0949 }
0950 }
0951 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
0952 {return m_target ? m_target->Put2(inString, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
0953 bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
0954 {return m_target && GetPassSignals() ? m_target->Flush(hardFlush, propagation, blocking) : false;}
0955 bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
0956 {return m_target && GetPassSignals() ? m_target->MessageSeriesEnd(propagation, blocking) : false;}
0957
0958 byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
0959 {
0960 if (m_target)
0961 return m_target->ChannelCreatePutSpace(channel, size);
0962 else
0963 {
0964 size = 0;
0965 return NULLPTR;
0966 }
0967 }
0968 size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
0969 {return m_target ? m_target->ChannelPut2(channel, begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
0970 size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
0971 {return m_target ? m_target->ChannelPutModifiable2(channel, begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
0972 bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true)
0973 {return m_target && GetPassSignals() ? m_target->ChannelFlush(channel, completeFlush, propagation, blocking) : false;}
0974 bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true)
0975 {return m_target && GetPassSignals() ? m_target->ChannelMessageSeriesEnd(channel, propagation, blocking) : false;}
0976
0977 unsigned int GetMaxWaitObjectCount() const
0978 { return m_target && GetPassWaitObjects() ? m_target->GetMaxWaitObjectCount() : 0; }
0979 void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack)
0980 { if (m_target && GetPassWaitObjects()) m_target->GetWaitObjects(container, callStack); }
0981
0982 private:
0983 BufferedTransformation *m_target;
0984 word32 m_behavior;
0985 };
0986
0987
0988
0989
0990 class CRYPTOPP_DLL OutputProxy : public CustomSignalPropagation<Sink>
0991 {
0992 public:
0993 virtual ~OutputProxy() {}
0994
0995
0996
0997
0998 OutputProxy(BufferedTransformation &owner, bool passSignal) : m_owner(owner), m_passSignal(passSignal) {}
0999
1000
1001
1002 bool GetPassSignal() const {return m_passSignal;}
1003
1004
1005 void SetPassSignal(bool passSignal) {m_passSignal = passSignal;}
1006
1007 byte * CreatePutSpace(size_t &size)
1008 {return m_owner.AttachedTransformation()->CreatePutSpace(size);}
1009 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
1010 {return m_owner.AttachedTransformation()->Put2(inString, length, m_passSignal ? messageEnd : 0, blocking);}
1011 size_t PutModifiable2(byte *begin, size_t length, int messageEnd, bool blocking)
1012 {return m_owner.AttachedTransformation()->PutModifiable2(begin, length, m_passSignal ? messageEnd : 0, blocking);}
1013 void Initialize(const NameValuePairs ¶meters=g_nullNameValuePairs, int propagation=-1)
1014 {if (m_passSignal) m_owner.AttachedTransformation()->Initialize(parameters, propagation);}
1015 bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
1016 {return m_passSignal ? m_owner.AttachedTransformation()->Flush(hardFlush, propagation, blocking) : false;}
1017 bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
1018 {return m_passSignal ? m_owner.AttachedTransformation()->MessageSeriesEnd(propagation, blocking) : false;}
1019
1020 byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
1021 {return m_owner.AttachedTransformation()->ChannelCreatePutSpace(channel, size);}
1022 size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
1023 {return m_owner.AttachedTransformation()->ChannelPut2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking);}
1024 size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
1025 {return m_owner.AttachedTransformation()->ChannelPutModifiable2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking);}
1026 bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true)
1027 {return m_passSignal ? m_owner.AttachedTransformation()->ChannelFlush(channel, completeFlush, propagation, blocking) : false;}
1028 bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true)
1029 {return m_passSignal ? m_owner.AttachedTransformation()->ChannelMessageSeriesEnd(channel, propagation, blocking) : false;}
1030
1031 private:
1032 BufferedTransformation &m_owner;
1033 bool m_passSignal;
1034 };
1035
1036
1037
1038 class CRYPTOPP_DLL ProxyFilter : public FilterWithBufferedInput
1039 {
1040 public:
1041 virtual ~ProxyFilter() {}
1042
1043
1044
1045
1046
1047
1048 ProxyFilter(BufferedTransformation *filter, size_t firstSize, size_t lastSize, BufferedTransformation *attachment);
1049
1050 bool IsolatedFlush(bool hardFlush, bool blocking);
1051
1052
1053
1054 void SetFilter(Filter *filter);
1055 void NextPutMultiple(const byte *s, size_t len);
1056 void NextPutModifiable(byte *inString, size_t length);
1057
1058 protected:
1059 member_ptr<BufferedTransformation> m_filter;
1060 };
1061
1062
1063
1064 class CRYPTOPP_DLL SimpleProxyFilter : public ProxyFilter
1065 {
1066 public:
1067
1068
1069
1070 SimpleProxyFilter(BufferedTransformation *filter, BufferedTransformation *attachment)
1071 : ProxyFilter(filter, 0, 0, attachment) {}
1072
1073 void FirstPut(const byte * inString)
1074 {CRYPTOPP_UNUSED(inString);}
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086 void LastPut(const byte *inString, size_t length)
1087 {CRYPTOPP_UNUSED(inString), CRYPTOPP_UNUSED(length); m_filter->MessageEnd();}
1088 };
1089
1090
1091
1092
1093
1094 class CRYPTOPP_DLL PK_EncryptorFilter : public SimpleProxyFilter
1095 {
1096 public:
1097
1098
1099
1100
1101 PK_EncryptorFilter(RandomNumberGenerator &rng, const PK_Encryptor &encryptor, BufferedTransformation *attachment = NULLPTR)
1102 : SimpleProxyFilter(encryptor.CreateEncryptionFilter(rng), attachment) {}
1103 };
1104
1105
1106
1107
1108
1109 class CRYPTOPP_DLL PK_DecryptorFilter : public SimpleProxyFilter
1110 {
1111 public:
1112
1113
1114
1115
1116 PK_DecryptorFilter(RandomNumberGenerator &rng, const PK_Decryptor &decryptor, BufferedTransformation *attachment = NULLPTR)
1117 : SimpleProxyFilter(decryptor.CreateDecryptionFilter(rng), attachment) {}
1118 };
1119
1120
1121
1122
1123
1124 template <class T>
1125 class StringSinkTemplate : public Bufferless<Sink>
1126 {
1127 public:
1128 typedef typename T::value_type value_type;
1129 virtual ~StringSinkTemplate() {}
1130
1131
1132
1133 StringSinkTemplate(T &output)
1134 : m_output(&output) {CRYPTOPP_ASSERT(sizeof(value_type)==1);}
1135
1136 void IsolatedInitialize(const NameValuePairs ¶meters)
1137 {if (!parameters.GetValue("OutputStringPointer", m_output)) throw InvalidArgument("StringSink: OutputStringPointer not specified");}
1138
1139 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
1140 {
1141 CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking);
1142 if (length > 0)
1143 {
1144 typename T::size_type size = m_output->size();
1145 if (length < size && size + length > m_output->capacity())
1146 m_output->reserve(2*size);
1147 m_output->insert(m_output->end(), (const value_type *)inString, (const value_type *)inString+length);
1148 }
1149 return 0;
1150 }
1151
1152 private:
1153 T *m_output;
1154 };
1155
1156
1157
1158
1159
1160 DOCUMENTED_TYPEDEF(StringSinkTemplate<std::string>, StringSink);
1161 CRYPTOPP_DLL_TEMPLATE_CLASS StringSinkTemplate<std::string>;
1162
1163
1164
1165
1166 DOCUMENTED_TYPEDEF(StringSinkTemplate<std::vector<byte> >, VectorSink);
1167 CRYPTOPP_DLL_TEMPLATE_CLASS StringSinkTemplate<std::vector<byte> >;
1168
1169
1170
1171 class RandomNumberSink : public Bufferless<Sink>
1172 {
1173 public:
1174 virtual ~RandomNumberSink() {}
1175
1176
1177 RandomNumberSink()
1178 : m_rng(NULLPTR) {}
1179
1180
1181
1182 RandomNumberSink(RandomNumberGenerator &rng)
1183 : m_rng(&rng) {}
1184
1185 void IsolatedInitialize(const NameValuePairs ¶meters);
1186 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
1187
1188 private:
1189 RandomNumberGenerator *m_rng;
1190 };
1191
1192
1193
1194
1195
1196
1197
1198
1199 class CRYPTOPP_DLL ArraySink : public Bufferless<Sink>
1200 {
1201 public:
1202 virtual ~ArraySink() {}
1203
1204
1205
1206
1207 ArraySink(const NameValuePairs ¶meters = g_nullNameValuePairs)
1208 : m_buf(NULLPTR), m_size(0), m_total(0) {IsolatedInitialize(parameters);}
1209
1210
1211
1212
1213 ArraySink(byte *buf, size_t size)
1214 : m_buf(buf), m_size(size), m_total(0) {}
1215
1216
1217
1218 size_t AvailableSize() {return SaturatingSubtract(m_size, m_total);}
1219
1220
1221
1222 lword TotalPutLength() {return m_total;}
1223
1224 void IsolatedInitialize(const NameValuePairs ¶meters);
1225 byte * CreatePutSpace(size_t &size);
1226 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
1227
1228 protected:
1229 byte *m_buf;
1230 size_t m_size;
1231 lword m_total;
1232 };
1233
1234
1235
1236
1237
1238
1239
1240
1241 class CRYPTOPP_DLL ArrayXorSink : public ArraySink
1242 {
1243 public:
1244 virtual ~ArrayXorSink() {}
1245
1246
1247
1248
1249 ArrayXorSink(byte *buf, size_t size)
1250 : ArraySink(buf, size) {}
1251
1252 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
1253 byte * CreatePutSpace(size_t &size) {return BufferedTransformation::CreatePutSpace(size);}
1254 };
1255
1256
1257
1258 class StringStore : public Store
1259 {
1260 public:
1261
1262
1263 StringStore(const char *string = NULLPTR)
1264 {StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
1265
1266
1267
1268
1269 StringStore(const byte *string, size_t length)
1270 {StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string, length)));}
1271
1272
1273
1274
1275 template <class T> StringStore(const T &string)
1276 {StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
1277
1278 CRYPTOPP_DLL size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
1279 CRYPTOPP_DLL size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
1280
1281 private:
1282 CRYPTOPP_DLL void StoreInitialize(const NameValuePairs ¶meters);
1283
1284 const byte *m_store;
1285 size_t m_length, m_count;
1286 };
1287
1288
1289
1290 class CRYPTOPP_DLL RandomNumberStore : public Store
1291 {
1292 public:
1293 virtual ~RandomNumberStore() {}
1294
1295 RandomNumberStore()
1296 : m_rng(NULLPTR), m_length(0), m_count(0) {}
1297
1298 RandomNumberStore(RandomNumberGenerator &rng, lword length)
1299 : m_rng(&rng), m_length(length), m_count(0) {}
1300
1301 bool AnyRetrievable() const {return MaxRetrievable() != 0;}
1302 lword MaxRetrievable() const {return m_length-m_count;}
1303
1304 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
1305 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
1306 {
1307 CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(begin); CRYPTOPP_UNUSED(end); CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking);
1308 throw NotImplemented("RandomNumberStore: CopyRangeTo2() is not supported by this store");
1309 }
1310
1311 private:
1312 void StoreInitialize(const NameValuePairs ¶meters);
1313
1314 RandomNumberGenerator *m_rng;
1315 lword m_length, m_count;
1316 };
1317
1318
1319
1320 class CRYPTOPP_DLL NullStore : public Store
1321 {
1322 public:
1323 NullStore(lword size = ULONG_MAX) : m_size(size) {}
1324 void StoreInitialize(const NameValuePairs ¶meters)
1325 {CRYPTOPP_UNUSED(parameters);}
1326 lword MaxRetrievable() const {return m_size;}
1327 size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
1328 size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
1329
1330 private:
1331 lword m_size;
1332 };
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Source : public InputRejecting<Filter>
1345 {
1346 public:
1347 virtual ~Source() {}
1348
1349
1350
1351 Source(BufferedTransformation *attachment = NULLPTR)
1352 {Source::Detach(attachment);}
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366 lword Pump(lword pumpMax=SIZE_MAX)
1367 {Pump2(pumpMax); return pumpMax;}
1368
1369
1370
1371
1372
1373 unsigned int PumpMessages(unsigned int count=UINT_MAX)
1374 {PumpMessages2(count); return count;}
1375
1376
1377
1378
1379
1380
1381
1382 void PumpAll()
1383 {PumpAll2();}
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394 virtual size_t Pump2(lword &byteCount, bool blocking=true) =0;
1395
1396
1397
1398
1399
1400 virtual size_t PumpMessages2(unsigned int &messageCount, bool blocking=true) =0;
1401
1402
1403
1404
1405
1406
1407 virtual size_t PumpAll2(bool blocking=true);
1408
1409
1410
1411 virtual bool SourceExhausted() const =0;
1412
1413
1414
1415 protected:
1416 void SourceInitialize(bool pumpAll, const NameValuePairs ¶meters)
1417 {
1418 IsolatedInitialize(parameters);
1419 if (pumpAll)
1420 PumpAll();
1421 }
1422 };
1423
1424
1425
1426
1427 template <class T>
1428 class SourceTemplate : public Source
1429 {
1430 public:
1431 virtual ~SourceTemplate() {}
1432
1433
1434
1435 SourceTemplate(BufferedTransformation *attachment)
1436 : Source(attachment) {}
1437 void IsolatedInitialize(const NameValuePairs ¶meters)
1438 {m_store.IsolatedInitialize(parameters);}
1439 size_t Pump2(lword &byteCount, bool blocking=true)
1440 {return m_store.TransferTo2(*AttachedTransformation(), byteCount, DEFAULT_CHANNEL, blocking);}
1441 size_t PumpMessages2(unsigned int &messageCount, bool blocking=true)
1442 {return m_store.TransferMessagesTo2(*AttachedTransformation(), messageCount, DEFAULT_CHANNEL, blocking);}
1443 size_t PumpAll2(bool blocking=true)
1444 {return m_store.TransferAllTo2(*AttachedTransformation(), DEFAULT_CHANNEL, blocking);}
1445 bool SourceExhausted() const
1446 {return !m_store.AnyRetrievable() && !m_store.AnyMessages();}
1447 void SetAutoSignalPropagation(int propagation)
1448 {m_store.SetAutoSignalPropagation(propagation);}
1449 int GetAutoSignalPropagation() const
1450 {return m_store.GetAutoSignalPropagation();}
1451
1452 protected:
1453 T m_store;
1454 };
1455
1456
1457
1458 class CRYPTOPP_DLL StringSource : public SourceTemplate<StringStore>
1459 {
1460 public:
1461
1462
1463 StringSource(BufferedTransformation *attachment = NULLPTR)
1464 : SourceTemplate<StringStore>(attachment) {}
1465
1466
1467
1468
1469
1470 StringSource(const char *string, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
1471 : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
1472
1473
1474
1475
1476
1477
1478 StringSource(const byte *string, size_t length, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
1479 : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string, length)));}
1480
1481
1482
1483
1484
1485 StringSource(const std::string &string, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
1486 : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
1487 };
1488
1489
1490
1491
1492
1493 DOCUMENTED_TYPEDEF(StringSource, ArraySource);
1494
1495
1496
1497 class CRYPTOPP_DLL VectorSource : public SourceTemplate<StringStore>
1498 {
1499 public:
1500
1501
1502 VectorSource(BufferedTransformation *attachment = NULLPTR)
1503 : SourceTemplate<StringStore>(attachment) {}
1504
1505
1506
1507
1508
1509 VectorSource(const std::vector<byte> &vec, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
1510 : SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(vec)));}
1511 };
1512
1513
1514
1515 class CRYPTOPP_DLL RandomNumberSource : public SourceTemplate<RandomNumberStore>
1516 {
1517 public:
1518 RandomNumberSource(RandomNumberGenerator &rng, int length, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
1519 : SourceTemplate<RandomNumberStore>(attachment)
1520 {SourceInitialize(pumpAll, MakeParameters("RandomNumberGeneratorPointer", &rng)("RandomNumberStoreSize", length));}
1521 };
1522
1523 NAMESPACE_END
1524
1525 #if CRYPTOPP_MSC_VERSION
1526 # pragma warning(pop)
1527 #endif
1528
1529 #endif