, private FilterPutSpaceHelper
{
public:
virtual ~HashFilter() {}
/// \brief Construct a HashFilter
/// \param hm reference to a HashTransformation
/// \param attachment an optional attached transformation
/// \param putMessage flag indicating whether the original message should be passed to an attached transformation
/// \param truncatedDigestSize the size of the digest
/// \param messagePutChannel the channel on which the message should be output
/// \param hashPutChannel the channel on which the digest should be output
HashFilter(HashTransformation &hm, BufferedTransformation *attachment = NULLPTR, bool putMessage=false, int truncatedDigestSize=-1, const std::string &messagePutChannel=DEFAULT_CHANNEL, const std::string &hashPutChannel=DEFAULT_CHANNEL);
std::string AlgorithmName() const {return m_hashModule.AlgorithmName();}
void IsolatedInitialize(const NameValuePairs ¶meters);
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
byte * CreatePutSpace(size_t &size) {return m_hashModule.CreateUpdateSpace(size);}
private:
HashTransformation &m_hashModule;
bool m_putMessage;
unsigned int m_digestSize;
byte *m_space;
std::string m_messagePutChannel, m_hashPutChannel;
};
/// \brief Filter wrapper for HashTransformation
/// \since Crypto++ 4.0
class CRYPTOPP_DLL HashVerificationFilter : public FilterWithBufferedInput
{
public:
virtual ~HashVerificationFilter() {}
/// \brief Exception thrown when a data integrity check failure is encountered
class HashVerificationFailed : public Exception
{
public:
HashVerificationFailed()
: Exception(DATA_INTEGRITY_CHECK_FAILED, "HashVerificationFilter: message hash or MAC not valid") {}
};
/// \enum Flags
/// \brief Flags controlling filter behavior.
/// \details The flags are a bitmask and can be OR'd together.
enum Flags {
/// \brief Indicates the hash is at the end of the message (i.e., concatenation of message+hash)
HASH_AT_END=0,
/// \brief Indicates the hash is at the beginning of the message (i.e., concatenation of hash+message)
HASH_AT_BEGIN=1,
/// \brief Indicates the message should be passed to an attached transformation
PUT_MESSAGE=2,
/// \brief Indicates the hash should be passed to an attached transformation
PUT_HASH=4,
/// \brief Indicates the result of the verification should be passed to an attached transformation
PUT_RESULT=8,
/// \brief Indicates the filter should throw a HashVerificationFailed if a failure is encountered
THROW_EXCEPTION=16,
/// \brief Default flags using \p HASH_AT_BEGIN and \p PUT_RESULT
DEFAULT_FLAGS = HASH_AT_BEGIN | PUT_RESULT
};
/// \brief Construct a HashVerificationFilter
/// \param hm reference to a HashTransformation
/// \param attachment an optional attached transformation
/// \param flags flags indicating behaviors for the filter
/// \param truncatedDigestSize the size of the digest
/// \details truncatedDigestSize = -1 indicates \ref HashTransformation::DigestSize() "DigestSize" should be used.
HashVerificationFilter(HashTransformation &hm, BufferedTransformation *attachment = NULLPTR, word32 flags = DEFAULT_FLAGS, int truncatedDigestSize=-1);
std::string AlgorithmName() const {return m_hashModule.AlgorithmName();}
bool GetLastResult() const {return m_verified;}
protected:
void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
void FirstPut(const byte *inString);
void NextPutMultiple(const byte *inString, size_t length);
void LastPut(const byte *inString, size_t length);
private:
friend class AuthenticatedDecryptionFilter;
HashTransformation &m_hashModule;
word32 m_flags;
unsigned int m_digestSize;
bool m_verified;
SecByteBlock m_expectedHash;
};
/// \brief Filter wrapper for encrypting with AuthenticatedSymmetricCipher
/// \details AuthenticatedEncryptionFilter() is a wrapper for encrypting with AuthenticatedSymmetricCipher(),
/// optionally handling padding/unpadding when needed.
/// \sa AuthenticatedDecryptionFilter, EAX, CCM, GCM, AuthenticatedSymmetricCipher
/// \since Crypto++ 5.6.0
class CRYPTOPP_DLL AuthenticatedEncryptionFilter : public StreamTransformationFilter
{
public:
virtual ~AuthenticatedEncryptionFilter() {}
/// \brief Construct a AuthenticatedEncryptionFilter
/// \param c reference to a AuthenticatedSymmetricCipher
/// \param attachment an optional attached transformation
/// \param putAAD flag indicating whether the AAD should be passed to an attached transformation
/// \param truncatedDigestSize the size of the digest
/// \param macChannel the channel on which the MAC should be output
/// \param padding the \ref BlockPaddingSchemeDef "padding scheme"
/// \details truncatedDigestSize = -1 indicates \ref HashTransformation::DigestSize() "DigestSize" should be used.
/// \since Crypto++ 5.6.0
AuthenticatedEncryptionFilter(AuthenticatedSymmetricCipher &c, BufferedTransformation *attachment = NULLPTR, bool putAAD=false, int truncatedDigestSize=-1, const std::string &macChannel=DEFAULT_CHANNEL, BlockPaddingScheme padding = DEFAULT_PADDING);
void IsolatedInitialize(const NameValuePairs ¶meters);
byte * ChannelCreatePutSpace(const std::string &channel, size_t &size);
size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking);
/// \brief Input the last block of data
/// \param inString the input byte buffer
/// \param length the size of the input buffer, in bytes
/// \details LastPut() processes the last block of data and signals attached filters to do the same.
/// LastPut() is always called. The pseudo algorithm for the logic is:
///
/// if totalLength < firstSize then length == totalLength
/// else if totalLength <= firstSize+lastSize then length == totalLength-firstSize
/// else lastSize <= length < lastSize+blockSize
///
void LastPut(const byte *inString, size_t length);
protected:
HashFilter m_hf;
};
/// \brief Filter wrapper for decrypting with AuthenticatedSymmetricCipher
/// \details AuthenticatedDecryptionFilter() is a wrapper for decrypting with AuthenticatedSymmetricCipher(),
/// optionally handling padding/unpadding when needed.
/// \sa AuthenticatedEncryptionFilter, EAX, CCM, GCM, AuthenticatedSymmetricCipher
/// \since Crypto++ 5.6.0
class CRYPTOPP_DLL AuthenticatedDecryptionFilter : public FilterWithBufferedInput, public BlockPaddingSchemeDef
{
public:
/// \enum Flags
/// \brief Flags controlling filter behavior.
/// \details The flags are a bitmask and can be OR'd together.
enum Flags {
/// \brief Indicates the MAC is at the end of the message (i.e., concatenation of message+mac)
MAC_AT_END=0,
/// \brief Indicates the MAC is at the beginning of the message (i.e., concatenation of mac+message)
MAC_AT_BEGIN=1,
/// \brief Indicates the filter should throw a HashVerificationFailed if a failure is encountered
THROW_EXCEPTION=16,
/// \brief Default flags using \p THROW_EXCEPTION
DEFAULT_FLAGS = THROW_EXCEPTION
};
virtual ~AuthenticatedDecryptionFilter() {}
/// \brief Construct a AuthenticatedDecryptionFilter
/// \param c reference to a AuthenticatedSymmetricCipher
/// \param attachment an optional attached transformation
/// \param flags flags indicating behaviors for the filter
/// \param truncatedDigestSize the size of the digest
/// \param padding the \ref BlockPaddingSchemeDef "padding scheme"
/// \details Additional authenticated data should be given in channel "AAD".
/// \details truncatedDigestSize = -1 indicates \ref HashTransformation::DigestSize() "DigestSize" should be used.
/// \since Crypto++ 5.6.0
AuthenticatedDecryptionFilter(AuthenticatedSymmetricCipher &c, BufferedTransformation *attachment = NULLPTR, word32 flags = DEFAULT_FLAGS, int truncatedDigestSize=-1, BlockPaddingScheme padding = DEFAULT_PADDING);
std::string AlgorithmName() const {return m_hashVerifier.AlgorithmName();}
byte * ChannelCreatePutSpace(const std::string &channel, size_t &size);
size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking);
bool GetLastResult() const {return m_hashVerifier.GetLastResult();}
protected:
void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
void FirstPut(const byte *inString);
void NextPutMultiple(const byte *inString, size_t length);
/// \brief Input the last block of data
/// \param inString the input byte buffer
/// \param length the size of the input buffer, in bytes
/// \details LastPut() processes the last block of data and signals attached filters to do the same.
/// LastPut() is always called. The pseudo algorithm for the logic is:
///
/// if totalLength < firstSize then length == totalLength
/// else if totalLength <= firstSize+lastSize then length == totalLength-firstSize
/// else lastSize <= length < lastSize+blockSize
///
void LastPut(const byte *inString, size_t length);
HashVerificationFilter m_hashVerifier;
StreamTransformationFilter m_streamFilter;
};
/// \brief Filter wrapper for PK_Signer
/// \since Crypto++ 4.0
class CRYPTOPP_DLL SignerFilter : public Unflushable
{
public:
virtual ~SignerFilter() {}
/// \brief Construct a SignerFilter
/// \param rng a RandomNumberGenerator derived class
/// \param signer a PK_Signer derived class
/// \param attachment an optional attached transformation
/// \param putMessage flag indicating whether the original message should be passed to an attached transformation
SignerFilter(RandomNumberGenerator &rng, const PK_Signer &signer, BufferedTransformation *attachment = NULLPTR, bool putMessage=false)
: m_rng(rng), m_signer(signer), m_messageAccumulator(signer.NewSignatureAccumulator(rng)), m_putMessage(putMessage) {Detach(attachment);}
std::string AlgorithmName() const {return m_signer.AlgorithmName();}
void IsolatedInitialize(const NameValuePairs ¶meters);
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
private:
RandomNumberGenerator &m_rng;
const PK_Signer &m_signer;
member_ptr m_messageAccumulator;
bool m_putMessage;
SecByteBlock m_buf;
};
/// \brief Filter wrapper for PK_Verifier
/// \details This filter was formerly named VerifierFilter. The name changed at Crypto++ 5.0.
/// \since Crypto++ 4.0
class CRYPTOPP_DLL SignatureVerificationFilter : public FilterWithBufferedInput
{
public:
/// \brief Exception thrown when an invalid signature is encountered
class SignatureVerificationFailed : public Exception
{
public:
SignatureVerificationFailed()
: Exception(DATA_INTEGRITY_CHECK_FAILED, "VerifierFilter: digital signature not valid") {}
};
/// \enum Flags
/// \brief Flags controlling filter behavior.
/// \details The flags are a bitmask and can be OR'd together.
enum Flags {
/// \brief Indicates the signature is at the end of the message (i.e., concatenation of message+signature)
SIGNATURE_AT_END=0,
/// \brief Indicates the signature is at the beginning of the message (i.e., concatenation of signature+message)
SIGNATURE_AT_BEGIN=1,
/// \brief Indicates the message should be passed to an attached transformation
PUT_MESSAGE=2,
/// \brief Indicates the signature should be passed to an attached transformation
PUT_SIGNATURE=4,
/// \brief Indicates the result of the verification should be passed to an attached transformation
PUT_RESULT=8,
/// \brief Indicates the filter should throw a HashVerificationFailed if a failure is encountered
THROW_EXCEPTION=16,
/// \brief Default flags using \p SIGNATURE_AT_BEGIN and \p PUT_RESULT
DEFAULT_FLAGS = SIGNATURE_AT_BEGIN | PUT_RESULT
};
virtual ~SignatureVerificationFilter() {}
/// \brief Construct a SignatureVerificationFilter
/// \param verifier a PK_Verifier derived class
/// \param attachment an optional attached transformation
/// \param flags flags indicating behaviors for the filter
SignatureVerificationFilter(const PK_Verifier &verifier, BufferedTransformation *attachment = NULLPTR, word32 flags = DEFAULT_FLAGS);
std::string AlgorithmName() const {return m_verifier.AlgorithmName();}
/// \brief Retrieves the result of the last verification
/// \returns true if the signature on the previosus message was valid, false otherwise
bool GetLastResult() const {return m_verified;}
protected:
void InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize);
void FirstPut(const byte *inString);
void NextPutMultiple(const byte *inString, size_t length);
void LastPut(const byte *inString, size_t length);
private:
const PK_Verifier &m_verifier;
member_ptr m_messageAccumulator;
word32 m_flags;
SecByteBlock m_signature;
bool m_verified;
};
/// \brief Redirect input to another BufferedTransformation without owning it
/// \since Crypto++ 4.0
class CRYPTOPP_DLL Redirector : public CustomSignalPropagation
{
public:
/// \enum Behavior
/// \brief Controls signal propagation behavior
enum Behavior
{
/// \brief Pass data only
DATA_ONLY = 0x00,
/// \brief Pass signals
PASS_SIGNALS = 0x01,
/// \brief Pass wait events
PASS_WAIT_OBJECTS = 0x02,
/// \brief Pass everything
/// \details PASS_EVERYTHING is default
PASS_EVERYTHING = PASS_SIGNALS | PASS_WAIT_OBJECTS
};
virtual ~Redirector() {}
/// \brief Construct a Redirector
Redirector() : m_target(NULLPTR), m_behavior(PASS_EVERYTHING) {}
/// \brief Construct a Redirector
/// \param target the destination BufferedTransformation
/// \param behavior Behavior "flags" specifying signal propagation
Redirector(BufferedTransformation &target, Behavior behavior=PASS_EVERYTHING)
: m_target(&target), m_behavior(behavior) {}
/// \brief Redirect input to another BufferedTransformation
/// \param target the destination BufferedTransformation
void Redirect(BufferedTransformation &target) {m_target = ⌖}
/// \brief Stop redirecting input
void StopRedirection() {m_target = NULLPTR;}
Behavior GetBehavior() {return static_cast(m_behavior);}
void SetBehavior(Behavior behavior) {m_behavior=behavior;}
bool GetPassSignals() const {return (m_behavior & PASS_SIGNALS) != 0;}
void SetPassSignals(bool pass) { if (pass) m_behavior |= PASS_SIGNALS; else m_behavior &= ~static_cast(PASS_SIGNALS); }
bool GetPassWaitObjects() const {return (m_behavior & PASS_WAIT_OBJECTS) != 0;}
void SetPassWaitObjects(bool pass) { if (pass) m_behavior |= PASS_WAIT_OBJECTS; else m_behavior &= ~static_cast(PASS_WAIT_OBJECTS); }
bool CanModifyInput() const
{return m_target ? m_target->CanModifyInput() : false;}
void Initialize(const NameValuePairs ¶meters, int propagation);
byte * CreatePutSpace(size_t &size)
{
if (m_target)
return m_target->CreatePutSpace(size);
else
{
size = 0;
return NULLPTR;
}
}
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
{return m_target ? m_target->Put2(inString, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
{return m_target && GetPassSignals() ? m_target->Flush(hardFlush, propagation, blocking) : false;}
bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
{return m_target && GetPassSignals() ? m_target->MessageSeriesEnd(propagation, blocking) : false;}
byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
{
if (m_target)
return m_target->ChannelCreatePutSpace(channel, size);
else
{
size = 0;
return NULLPTR;
}
}
size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
{return m_target ? m_target->ChannelPut2(channel, begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
{return m_target ? m_target->ChannelPutModifiable2(channel, begin, length, GetPassSignals() ? messageEnd : 0, blocking) : 0;}
bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true)
{return m_target && GetPassSignals() ? m_target->ChannelFlush(channel, completeFlush, propagation, blocking) : false;}
bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true)
{return m_target && GetPassSignals() ? m_target->ChannelMessageSeriesEnd(channel, propagation, blocking) : false;}
unsigned int GetMaxWaitObjectCount() const
{ return m_target && GetPassWaitObjects() ? m_target->GetMaxWaitObjectCount() : 0; }
void GetWaitObjects(WaitObjectContainer &container, CallStack const& callStack)
{ if (m_target && GetPassWaitObjects()) m_target->GetWaitObjects(container, callStack); }
private:
BufferedTransformation *m_target;
word32 m_behavior;
};
/// \brief Filter class that is a proxy for a sink
/// \details Used By ProxyFilter
/// \since Crypto++ 4.0
class CRYPTOPP_DLL OutputProxy : public CustomSignalPropagation
{
public:
virtual ~OutputProxy() {}
/// \brief Construct an OutputProxy
/// \param owner the owning transformation
/// \param passSignal flag indicating if signals should be passed
OutputProxy(BufferedTransformation &owner, bool passSignal) : m_owner(owner), m_passSignal(passSignal) {}
/// \brief Retrieve passSignal flag
/// \returns flag indicating if signals should be passed
bool GetPassSignal() const {return m_passSignal;}
/// \brief Set passSignal flag
/// \param passSignal flag indicating if signals should be passed
void SetPassSignal(bool passSignal) {m_passSignal = passSignal;}
byte * CreatePutSpace(size_t &size)
{return m_owner.AttachedTransformation()->CreatePutSpace(size);}
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
{return m_owner.AttachedTransformation()->Put2(inString, length, m_passSignal ? messageEnd : 0, blocking);}
size_t PutModifiable2(byte *begin, size_t length, int messageEnd, bool blocking)
{return m_owner.AttachedTransformation()->PutModifiable2(begin, length, m_passSignal ? messageEnd : 0, blocking);}
void Initialize(const NameValuePairs ¶meters=g_nullNameValuePairs, int propagation=-1)
{if (m_passSignal) m_owner.AttachedTransformation()->Initialize(parameters, propagation);}
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
{return m_passSignal ? m_owner.AttachedTransformation()->Flush(hardFlush, propagation, blocking) : false;}
bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
{return m_passSignal ? m_owner.AttachedTransformation()->MessageSeriesEnd(propagation, blocking) : false;}
byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
{return m_owner.AttachedTransformation()->ChannelCreatePutSpace(channel, size);}
size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
{return m_owner.AttachedTransformation()->ChannelPut2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking);}
size_t ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking)
{return m_owner.AttachedTransformation()->ChannelPutModifiable2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking);}
bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true)
{return m_passSignal ? m_owner.AttachedTransformation()->ChannelFlush(channel, completeFlush, propagation, blocking) : false;}
bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true)
{return m_passSignal ? m_owner.AttachedTransformation()->ChannelMessageSeriesEnd(channel, propagation, blocking) : false;}
private:
BufferedTransformation &m_owner;
bool m_passSignal;
};
/// \brief Base class for Filter classes that are proxies for a chain of other filters
/// \since Crypto++ 4.0
class CRYPTOPP_DLL ProxyFilter : public FilterWithBufferedInput
{
public:
virtual ~ProxyFilter() {}
/// \brief Construct a ProxyFilter
/// \param filter an output filter
/// \param firstSize the first Put size
/// \param lastSize the last Put size
/// \param attachment an attached transformation
ProxyFilter(BufferedTransformation *filter, size_t firstSize, size_t lastSize, BufferedTransformation *attachment);
bool IsolatedFlush(bool hardFlush, bool blocking);
/// \brief Sets the OutputProxy filter
/// \param filter an OutputProxy filter
void SetFilter(Filter *filter);
void NextPutMultiple(const byte *s, size_t len);
void NextPutModifiable(byte *inString, size_t length);
protected:
member_ptr m_filter;
};
/// \brief Proxy filter that doesn't modify the underlying filter's input or output
/// \since Crypto++ 5.0
class CRYPTOPP_DLL SimpleProxyFilter : public ProxyFilter
{
public:
/// \brief Construct a SimpleProxyFilter
/// \param filter an output filter
/// \param attachment an attached transformation
SimpleProxyFilter(BufferedTransformation *filter, BufferedTransformation *attachment)
: ProxyFilter(filter, 0, 0, attachment) {}
void FirstPut(const byte * inString)
{CRYPTOPP_UNUSED(inString);}
/// \brief Input the last block of data
/// \param inString the input byte buffer
/// \param length the size of the input buffer, in bytes
/// \details LastPut() processes the last block of data and signals attached filters to do the same.
/// LastPut() is always called. The pseudo algorithm for the logic is:
///
/// if totalLength < firstSize then length == totalLength
/// else if totalLength <= firstSize+lastSize then length == totalLength-firstSize
/// else lastSize <= length < lastSize+blockSize
///
void LastPut(const byte *inString, size_t length)
{CRYPTOPP_UNUSED(inString), CRYPTOPP_UNUSED(length); m_filter->MessageEnd();}
};
/// \brief Filter wrapper for PK_Encryptor
/// \details PK_DecryptorFilter is a proxy for the filter created by PK_Encryptor::CreateEncryptionFilter.
/// This class provides symmetry with VerifierFilter.
/// \since Crypto++ 5.0
class CRYPTOPP_DLL PK_EncryptorFilter : public SimpleProxyFilter
{
public:
/// \brief Construct a PK_EncryptorFilter
/// \param rng a RandomNumberGenerator derived class
/// \param encryptor a PK_Encryptor derived class
/// \param attachment an optional attached transformation
PK_EncryptorFilter(RandomNumberGenerator &rng, const PK_Encryptor &encryptor, BufferedTransformation *attachment = NULLPTR)
: SimpleProxyFilter(encryptor.CreateEncryptionFilter(rng), attachment) {}
};
/// \brief Filter wrapper for PK_Decryptor
/// \details PK_DecryptorFilter is a proxy for the filter created by PK_Decryptor::CreateDecryptionFilter.
/// This class provides symmetry with SignerFilter.
/// \since Crypto++ 5.0
class CRYPTOPP_DLL PK_DecryptorFilter : public SimpleProxyFilter
{
public:
/// \brief Construct a PK_DecryptorFilter
/// \param rng a RandomNumberGenerator derived class
/// \param decryptor a PK_Decryptor derived class
/// \param attachment an optional attached transformation
PK_DecryptorFilter(RandomNumberGenerator &rng, const PK_Decryptor &decryptor, BufferedTransformation *attachment = NULLPTR)
: SimpleProxyFilter(decryptor.CreateDecryptionFilter(rng), attachment) {}
};
/// \brief Append input to a string object
/// \tparam T std::basic_string type
/// \details StringSinkTemplate is a StringSinkTemplate typedef
/// \since Crypto++ 5.0
template
class StringSinkTemplate : public Bufferless
{
public:
typedef typename T::value_type value_type;
virtual ~StringSinkTemplate() {}
/// \brief Construct a StringSinkTemplate
/// \param output std::basic_string or std::vector type
StringSinkTemplate(T &output)
: m_output(&output) {CRYPTOPP_ASSERT(sizeof(value_type)==1);}
void IsolatedInitialize(const NameValuePairs ¶meters)
{if (!parameters.GetValue("OutputStringPointer", m_output)) throw InvalidArgument("StringSink: OutputStringPointer not specified");}
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
{
CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking);
if (length > 0)
{
typename T::size_type size = m_output->size();
if (length < size && size + length > m_output->capacity())
m_output->reserve(2*size);
m_output->insert(m_output->end(), (const value_type *)inString, (const value_type *)inString+length);
}
return 0;
}
private:
T *m_output;
};
/// \brief Append input to a string object
/// \details StringSink is a typedef for StringSinkTemplate.
/// \sa ArraySink, ArrayXorSink
/// \since Crypto++ 4.0
DOCUMENTED_TYPEDEF(StringSinkTemplate, StringSink)
CRYPTOPP_DLL_TEMPLATE_CLASS StringSinkTemplate;
/// \brief Append input to a std::vector object
/// \details VectorSink is a typedef for StringSinkTemplate >.
DOCUMENTED_TYPEDEF(StringSinkTemplate >, VectorSink)
CRYPTOPP_DLL_TEMPLATE_CLASS StringSinkTemplate >;
/// \brief Incorporates input into RNG as additional entropy
/// \since Crypto++ 4.0
class RandomNumberSink : public Bufferless
{
public:
virtual ~RandomNumberSink() {}
/// \brief Construct a RandomNumberSink
RandomNumberSink()
: m_rng(NULLPTR) {}
/// \brief Construct a RandomNumberSink
/// \param rng a RandomNumberGenerator derived class
RandomNumberSink(RandomNumberGenerator &rng)
: m_rng(&rng) {}
void IsolatedInitialize(const NameValuePairs ¶meters);
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
private:
RandomNumberGenerator *m_rng;
};
/// \brief Copy input to a memory buffer
/// \details ArraySink wraps a fixed size buffer. The buffer is full once Put returns non-0.
/// When used in a pipleline, ArraySink silently discards input if the buffer is full.
/// AvailableSize() can be used to determine how much space remains in the buffer.
/// TotalPutLength() can be used to determine how many bytes were processed.
/// \sa StringSink, ArrayXorSink
/// \since Crypto++ 4.0
class CRYPTOPP_DLL ArraySink : public Bufferless
{
public:
virtual ~ArraySink() {}
/// \brief Construct an ArraySink
/// \param parameters a set of NameValuePairs to initialize this object
/// \details Name::OutputBuffer() is a mandatory parameter using this constructor.
ArraySink(const NameValuePairs ¶meters = g_nullNameValuePairs)
: m_buf(NULLPTR), m_size(0), m_total(0) {IsolatedInitialize(parameters);}
/// \brief Construct an ArraySink
/// \param buf pointer to a memory buffer
/// \param size length of the memory buffer
ArraySink(byte *buf, size_t size)
: m_buf(buf), m_size(size), m_total(0) {}
/// \brief Provides the size remaining in the Sink
/// \returns size remaining in the Sink, in bytes
size_t AvailableSize() {return SaturatingSubtract(m_size, m_total);}
/// \brief Provides the number of bytes written to the Sink
/// \returns number of bytes written to the Sink, in bytes
lword TotalPutLength() {return m_total;}
void IsolatedInitialize(const NameValuePairs ¶meters);
byte * CreatePutSpace(size_t &size);
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
protected:
byte *m_buf;
size_t m_size;
lword m_total;
};
/// \brief Xor input to a memory buffer
/// \details ArrayXorSink wraps a fixed size buffer. The buffer is full once Put returns non-0.
/// When used in a pipleline, ArrayXorSink silently discards input if the buffer is full.
/// AvailableSize() can be used to determine how much space remains in the buffer.
/// TotalPutLength() can be used to determine how many bytes were processed.
/// \sa StringSink, ArraySink
/// \since Crypto++ 4.0
class CRYPTOPP_DLL ArrayXorSink : public ArraySink
{
public:
virtual ~ArrayXorSink() {}
/// \brief Construct an ArrayXorSink
/// \param buf pointer to a memory buffer
/// \param size length of the memory buffer
ArrayXorSink(byte *buf, size_t size)
: ArraySink(buf, size) {}
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking);
byte * CreatePutSpace(size_t &size) {return BufferedTransformation::CreatePutSpace(size);}
};
/// \brief String-based implementation of Store interface
/// \since Crypto++ 4.0
class StringStore : public Store
{
public:
/// \brief Construct a StringStore
/// \param string pointer to a C-String
StringStore(const char *string = NULLPTR)
{StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
/// \brief Construct a StringStore
/// \param string pointer to a memory buffer
/// \param length size of the memory buffer
StringStore(const byte *string, size_t length)
{StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string, length)));}
/// \brief Construct a StringStore
/// \tparam T std::basic_string type
/// \param string reference to a std::basic_string type
template StringStore(const T &string)
{StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
CRYPTOPP_DLL size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
CRYPTOPP_DLL size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
private:
CRYPTOPP_DLL void StoreInitialize(const NameValuePairs ¶meters);
const byte *m_store;
size_t m_length, m_count;
};
/// \brief RNG-based implementation of Source interface
/// \since Crypto++ 4.0
class CRYPTOPP_DLL RandomNumberStore : public Store
{
public:
virtual ~RandomNumberStore() {}
RandomNumberStore()
: m_rng(NULLPTR), m_length(0), m_count(0) {}
RandomNumberStore(RandomNumberGenerator &rng, lword length)
: m_rng(&rng), m_length(length), m_count(0) {}
bool AnyRetrievable() const {return MaxRetrievable() != 0;}
lword MaxRetrievable() const {return m_length-m_count;}
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
{
CRYPTOPP_UNUSED(target); CRYPTOPP_UNUSED(begin); CRYPTOPP_UNUSED(end); CRYPTOPP_UNUSED(channel); CRYPTOPP_UNUSED(blocking);
throw NotImplemented("RandomNumberStore: CopyRangeTo2() is not supported by this store");
}
private:
void StoreInitialize(const NameValuePairs ¶meters);
RandomNumberGenerator *m_rng;
lword m_length, m_count;
};
/// \brief Empty store
/// \since Crypto++ 5.0
class CRYPTOPP_DLL NullStore : public Store
{
public:
NullStore(lword size = ULONG_MAX) : m_size(size) {}
void StoreInitialize(const NameValuePairs ¶meters)
{CRYPTOPP_UNUSED(parameters);}
lword MaxRetrievable() const {return m_size;}
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true);
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const;
private:
lword m_size;
};
/// \brief Implementation of BufferedTransformation's attachment interface
/// \details Source is a cornerstone of the Pipeline trinitiy. Data flows from
/// Sources, through Filters, and then terminates in Sinks. The difference
/// between a Source and Filter is a Source \a pumps data, while a Filter does
/// not. The difference between a Filter and a Sink is a Filter allows an
/// attached transformation, while a Sink does not.
/// \details See the discussion of BufferedTransformation in cryptlib.h for
/// more details.
/// \sa Store and SourceTemplate
/// \since Crypto++ 1.0
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Source : public InputRejecting
{
public:
virtual ~Source() {}
/// \brief Construct a Source
/// \param attachment an optional attached transformation
Source(BufferedTransformation *attachment = NULLPTR)
{Source::Detach(attachment);}
/// \name PIPELINE
//@{
/// \brief Pump data to attached transformation
/// \param pumpMax the maximum number of bytes to pump
/// \returns the number of bytes that remain in the block (i.e., bytes not processed)
/// \details Internally, Pump() calls Pump2().
/// \note pumpMax is a lword, which is a 64-bit value that typically uses
/// LWORD_MAX. The default argument is SIZE_MAX, and it can be
/// 32-bits or 64-bits.
/// \sa Pump2, PumpAll, AnyRetrievable, MaxRetrievable
lword Pump(lword pumpMax=SIZE_MAX)
{Pump2(pumpMax); return pumpMax;}
/// \brief Pump messages to attached transformation
/// \param count the maximum number of messages to pump
/// \returns TODO
/// \details Internally, PumpMessages() calls PumpMessages2().
unsigned int PumpMessages(unsigned int count=UINT_MAX)
{PumpMessages2(count); return count;}
/// \brief Pump all data to attached transformation
/// \details Pumps all data to the attached transformation and signal the end of the current
/// message. To avoid the MessageEnd() signal call \ref Pump "Pump(LWORD_MAX)" or \ref Pump2
/// "Pump2(LWORD_MAX, bool)".
/// \details Internally, PumpAll() calls PumpAll2(), which calls PumpMessages().
/// \sa Pump, Pump2, AnyRetrievable, MaxRetrievable
void PumpAll()
{PumpAll2();}
/// \brief Pump data to attached transformation
/// \param byteCount the maximum number of bytes to pump
/// \param blocking specifies whether the object should block when processing input
/// \returns the number of bytes that remain in the block (i.e., bytes not processed)
/// \details byteCount is an \a IN and \a OUT parameter. When the call is made, byteCount is the
/// requested size of the pump. When the call returns, byteCount is the number of bytes that
/// were pumped.
/// \sa Pump, PumpAll, AnyRetrievable, MaxRetrievable
virtual size_t Pump2(lword &byteCount, bool blocking=true) =0;
/// \brief Pump messages to attached transformation
/// \param messageCount the maximum number of messages to pump
/// \param blocking specifies whether the object should block when processing input
/// \details messageCount is an IN and OUT parameter.
virtual size_t PumpMessages2(unsigned int &messageCount, bool blocking=true) =0;
/// \brief Pump all data to attached transformation
/// \param blocking specifies whether the object should block when processing input
/// \returns the number of bytes that remain in the block (i.e., bytes not processed)
/// \sa Pump, Pump2, AnyRetrievable, MaxRetrievable
virtual size_t PumpAll2(bool blocking=true);
/// \brief Determines if the Source is exhausted
/// \returns true if the source has been exhausted
virtual bool SourceExhausted() const =0;
//@}
protected:
void SourceInitialize(bool pumpAll, const NameValuePairs ¶meters)
{
IsolatedInitialize(parameters);
if (pumpAll)
PumpAll();
}
};
/// \brief Transform a Store into a Source
/// \tparam T the class or type
/// \since Crypto++ 5.0
template
class SourceTemplate : public Source
{
public:
virtual ~SourceTemplate() {}
/// \brief Construct a SourceTemplate
/// \param attachment an attached transformation
SourceTemplate(BufferedTransformation *attachment)
: Source(attachment) {}
void IsolatedInitialize(const NameValuePairs ¶meters)
{m_store.IsolatedInitialize(parameters);}
size_t Pump2(lword &byteCount, bool blocking=true)
{return m_store.TransferTo2(*AttachedTransformation(), byteCount, DEFAULT_CHANNEL, blocking);}
size_t PumpMessages2(unsigned int &messageCount, bool blocking=true)
{return m_store.TransferMessagesTo2(*AttachedTransformation(), messageCount, DEFAULT_CHANNEL, blocking);}
size_t PumpAll2(bool blocking=true)
{return m_store.TransferAllTo2(*AttachedTransformation(), DEFAULT_CHANNEL, blocking);}
bool SourceExhausted() const
{return !m_store.AnyRetrievable() && !m_store.AnyMessages();}
void SetAutoSignalPropagation(int propagation)
{m_store.SetAutoSignalPropagation(propagation);}
int GetAutoSignalPropagation() const
{return m_store.GetAutoSignalPropagation();}
protected:
T m_store;
};
/// \brief String-based implementation of the Source interface
/// \since Crypto++ 4.0
class CRYPTOPP_DLL StringSource : public SourceTemplate
{
public:
/// \brief Construct a StringSource
/// \param attachment an optional attached transformation
StringSource(BufferedTransformation *attachment = NULLPTR)
: SourceTemplate(attachment) {}
/// \brief Construct a StringSource
/// \param string C-String
/// \param pumpAll flag indicating if source data should be pumped to its attached transformation
/// \param attachment an optional attached transformation
StringSource(const char *string, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
: SourceTemplate(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
/// \brief Construct a StringSource
/// \param string binary byte array
/// \param length size of the byte array
/// \param pumpAll flag indicating if source data should be pumped to its attached transformation
/// \param attachment an optional attached transformation
StringSource(const byte *string, size_t length, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
: SourceTemplate(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string, length)));}
/// \brief Construct a StringSource
/// \param string std::string
/// \param pumpAll flag indicating if source data should be pumped to its attached transformation
/// \param attachment an optional attached transformation
StringSource(const std::string &string, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
: SourceTemplate(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
};
/// \brief Pointer-based implementation of the Source interface
/// \details ArraySource is a typedef for StringSource. Use the third constructor for an array source.
/// The third constructor takes a pointer and length.
/// \since Crypto++ 5.6.0
DOCUMENTED_TYPEDEF(StringSource, ArraySource)
/// \brief std::vector-based implementation of the Source interface
/// \since Crypto++ 8.0
class CRYPTOPP_DLL VectorSource : public SourceTemplate
{
public:
/// \brief Construct a VectorSource
/// \param attachment an optional attached transformation
VectorSource(BufferedTransformation *attachment = NULLPTR)
: SourceTemplate(attachment) {}
/// \brief Construct a VectorSource
/// \param vec vector of bytes
/// \param pumpAll flag indicating if source data should be pumped to its attached transformation
/// \param attachment an optional attached transformation
VectorSource(const std::vector &vec, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
: SourceTemplate(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(vec)));}
};
/// \brief RNG-based implementation of Source interface
/// \since Crypto++ 4.0
class CRYPTOPP_DLL RandomNumberSource : public SourceTemplate
{
public:
RandomNumberSource(RandomNumberGenerator &rng, int length, bool pumpAll, BufferedTransformation *attachment = NULLPTR)
: SourceTemplate(attachment)
{SourceInitialize(pumpAll, MakeParameters("RandomNumberGeneratorPointer", &rng)("RandomNumberStoreSize", length));}
};
NAMESPACE_END
#if CRYPTOPP_MSC_VERSION
# pragma warning(pop)
#endif
#endif