2017-01-27 12:05:45 +00:00
// simple.h - originally written and placed in the public domain by Wei Dai
2015-11-18 20:32:28 +00:00
2017-11-29 15:54:33 +00:00
/// \file simple.h
/// \brief Classes providing basic library services.
2015-11-05 06:59:46 +00:00
# ifndef CRYPTOPP_SIMPLE_H
# define CRYPTOPP_SIMPLE_H
# include "config.h"
# if CRYPTOPP_MSC_VERSION
# pragma warning(push)
# pragma warning(disable: 4127 4189)
# endif
# include "cryptlib.h"
# include "misc.h"
NAMESPACE_BEGIN ( CryptoPP )
2017-11-29 15:54:33 +00:00
/// \brief Base class for identifying alogorithm
/// \tparam BASE base class from which to derive
/// \tparam DERIVED class which to clone
2015-11-05 06:59:46 +00:00
template < class DERIVED , class BASE >
class CRYPTOPP_NO_VTABLE ClonableImpl : public BASE
{
public :
2019-08-17 18:49:03 +00:00
/// \brief Create a copy of this object
2020-01-01 03:32:54 +00:00
/// \return a copy of this object
2019-08-17 18:49:03 +00:00
/// \details The caller is responsible for freeing the object.
2015-11-05 06:59:46 +00:00
Clonable * Clone ( ) const { return new DERIVED ( * static_cast < const DERIVED * > ( this ) ) ; }
} ;
2017-11-29 15:54:33 +00:00
/// \brief Base class information
/// \tparam BASE an Algorithm derived class
/// \tparam ALGORITHM_INFO an Algorithm derived class
/// \details AlgorithmImpl provides StaticAlgorithmName from the template parameter BASE
2015-11-05 06:59:46 +00:00
template < class BASE , class ALGORITHM_INFO = BASE >
class CRYPTOPP_NO_VTABLE AlgorithmImpl : public BASE
{
public :
2017-11-29 15:54:33 +00:00
/// \brief The algorithm name
2020-01-01 03:32:54 +00:00
/// \return the algorithm name
2017-11-29 15:54:33 +00:00
/// \details StaticAlgorithmName returns the algorithm's name as a static member function.
2020-01-01 03:51:32 +00:00
/// The name is taken from information provided by BASE.
2015-11-05 06:59:46 +00:00
static std : : string CRYPTOPP_API StaticAlgorithmName ( ) { return ALGORITHM_INFO : : StaticAlgorithmName ( ) ; }
2017-11-29 15:54:33 +00:00
/// \brief The algorithm name
2020-01-01 03:32:54 +00:00
/// \return the algorithm name
2017-11-29 15:54:33 +00:00
/// \details AlgorithmName returns the algorithm's name as a member function.
2020-01-01 03:51:32 +00:00
/// The name is is acquired by calling StaticAlgorithmName.
2015-11-05 06:59:46 +00:00
std : : string AlgorithmName ( ) const { return ALGORITHM_INFO : : StaticAlgorithmName ( ) ; }
} ;
2017-11-29 15:54:33 +00:00
/// \brief Exception thrown when an invalid key length is encountered
2015-11-05 06:59:46 +00:00
class CRYPTOPP_DLL InvalidKeyLength : public InvalidArgument
{
public :
2019-08-17 18:49:03 +00:00
/// \brief Construct an InvalidKeyLength
/// \param algorithm the Algorithm associated with the exception
/// \param length the key size associated with the exception
2015-11-05 06:59:46 +00:00
explicit InvalidKeyLength ( const std : : string & algorithm , size_t length ) : InvalidArgument ( algorithm + " : " + IntToString ( length ) + " is not a valid key length " ) { }
} ;
2017-11-29 15:54:33 +00:00
/// \brief Exception thrown when an invalid number of rounds is encountered
2015-11-05 06:59:46 +00:00
class CRYPTOPP_DLL InvalidRounds : public InvalidArgument
{
public :
2019-08-17 18:49:03 +00:00
/// \brief Construct an InvalidRounds
/// \param algorithm the Algorithm associated with the exception
/// \param rounds the number of rounds associated with the exception
2015-11-05 06:59:46 +00:00
explicit InvalidRounds ( const std : : string & algorithm , unsigned int rounds ) : InvalidArgument ( algorithm + " : " + IntToString ( rounds ) + " is not a valid number of rounds " ) { }
} ;
2017-11-29 15:54:33 +00:00
/// \brief Exception thrown when an invalid block size is encountered
Add variable block size support for block ciphers
This should lead the way for more modern block ciphers like Threefish and Kalyna. It tested well with both regular cipher modes (the mode has an instance of the cipher) and external cipher modes (the cipher and mode are distinct objects, and the mode holds a reference to the cipher).
We still have to work out the details of naming a cipher. For example, Kalyna with a 128-bit key can use a 128-bit or 256-bit block size. Kalyna-128 is not enough to describe the algorithm and locate it in the object registry. Kalyna-128-128 looks kind of weird; maybe Kalyna-128(128) or Kalyna-128(256) would be better.
Here are the initial test cases to verify functionality:
byte key[64] = {}, iv[32] = {};
ECB_Mode<Kalyna>::Encryption enc1;
enc1.SetKey(key, 16);
CBC_Mode<Kalyna>::Encryption enc2;
enc2.SetKeyWithIV(key, 16, iv);
AlgorithmParameters params = MakeParameters
(Name::BlockSize(), 32)
(Name::IV(), ConstByteArrayParameter(iv, 32));
CTR_Mode<Kalyna>::Encryption enc3;
enc3.SetKey(key, 16, params);
CBC_Mode<Kalyna>::Encryption enc4;
enc4.SetKey(key, 32, params);
Kalyna::Encryption enc5;
ECB_Mode_ExternalCipher::Encryption ecb(enc5);
ecb.SetKey(key, 16, params);
Kalyna::Encryption enc6;
ECB_Mode_ExternalCipher::Encryption cbc(enc6);
cbc.SetKey(key, 32, params);
2017-05-01 20:23:57 +00:00
class CRYPTOPP_DLL InvalidBlockSize : public InvalidArgument
{
public :
2019-08-17 18:49:03 +00:00
/// \brief Construct an InvalidBlockSize
/// \param algorithm the Algorithm associated with the exception
/// \param length the block size associated with the exception
Add variable block size support for block ciphers
This should lead the way for more modern block ciphers like Threefish and Kalyna. It tested well with both regular cipher modes (the mode has an instance of the cipher) and external cipher modes (the cipher and mode are distinct objects, and the mode holds a reference to the cipher).
We still have to work out the details of naming a cipher. For example, Kalyna with a 128-bit key can use a 128-bit or 256-bit block size. Kalyna-128 is not enough to describe the algorithm and locate it in the object registry. Kalyna-128-128 looks kind of weird; maybe Kalyna-128(128) or Kalyna-128(256) would be better.
Here are the initial test cases to verify functionality:
byte key[64] = {}, iv[32] = {};
ECB_Mode<Kalyna>::Encryption enc1;
enc1.SetKey(key, 16);
CBC_Mode<Kalyna>::Encryption enc2;
enc2.SetKeyWithIV(key, 16, iv);
AlgorithmParameters params = MakeParameters
(Name::BlockSize(), 32)
(Name::IV(), ConstByteArrayParameter(iv, 32));
CTR_Mode<Kalyna>::Encryption enc3;
enc3.SetKey(key, 16, params);
CBC_Mode<Kalyna>::Encryption enc4;
enc4.SetKey(key, 32, params);
Kalyna::Encryption enc5;
ECB_Mode_ExternalCipher::Encryption ecb(enc5);
ecb.SetKey(key, 16, params);
Kalyna::Encryption enc6;
ECB_Mode_ExternalCipher::Encryption cbc(enc6);
cbc.SetKey(key, 32, params);
2017-05-01 20:23:57 +00:00
explicit InvalidBlockSize ( const std : : string & algorithm , size_t length ) : InvalidArgument ( algorithm + " : " + IntToString ( length ) + " is not a valid block size " ) { }
} ;
2018-03-30 00:18:27 +00:00
/// \brief Exception thrown when an invalid derived key length is encountered
2019-08-16 11:12:14 +00:00
class CRYPTOPP_DLL InvalidDerivedKeyLength : public InvalidArgument
2018-03-30 00:18:27 +00:00
{
public :
2019-08-17 18:49:03 +00:00
/// \brief Construct an InvalidDerivedKeyLength
/// \param algorithm the Algorithm associated with the exception
/// \param length the size associated with the exception
2019-08-16 11:12:14 +00:00
explicit InvalidDerivedKeyLength ( const std : : string & algorithm , size_t length ) : InvalidArgument ( algorithm + " : " + IntToString ( length ) + " is not a valid derived key length " ) { }
2018-03-30 00:18:27 +00:00
} ;
2017-11-29 15:54:33 +00:00
/// \brief Exception thrown when an invalid personalization string length is encountered
2016-04-17 07:06:28 +00:00
class CRYPTOPP_DLL InvalidPersonalizationLength : public InvalidArgument
{
public :
2019-08-17 18:49:03 +00:00
/// \brief Construct an InvalidPersonalizationLength
/// \param algorithm the Algorithm associated with the exception
/// \param length the personalization size associated with the exception
2016-04-17 07:06:28 +00:00
explicit InvalidPersonalizationLength ( const std : : string & algorithm , size_t length ) : InvalidArgument ( algorithm + " : " + IntToString ( length ) + " is not a valid salt length " ) { }
} ;
2017-11-29 15:54:33 +00:00
/// \brief Exception thrown when an invalid salt length is encountered
2016-04-17 07:06:28 +00:00
class CRYPTOPP_DLL InvalidSaltLength : public InvalidArgument
{
public :
2019-08-17 18:49:03 +00:00
/// \brief Construct an InvalidSaltLength
/// \param algorithm the Algorithm associated with the exception
/// \param length the salt size associated with the exception
2016-04-17 07:06:28 +00:00
explicit InvalidSaltLength ( const std : : string & algorithm , size_t length ) : InvalidArgument ( algorithm + " : " + IntToString ( length ) + " is not a valid salt length " ) { }
} ;
2015-11-05 06:59:46 +00:00
// *****************************
2017-11-29 15:54:33 +00:00
/// \brief Base class for bufferless filters
/// \tparam T the class or type
2015-11-05 06:59:46 +00:00
template < class T >
class CRYPTOPP_NO_VTABLE Bufferless : public T
{
public :
2019-08-17 18:49:03 +00:00
/// \brief Flushes data buffered by this object, without signal propagation
/// \param hardFlush indicates whether all data should be flushed
/// \param blocking specifies whether the object should block when processing input
/// \note hardFlush must be used with care
2015-11-05 06:59:46 +00:00
bool IsolatedFlush ( bool hardFlush , bool blocking )
{ CRYPTOPP_UNUSED ( hardFlush ) ; CRYPTOPP_UNUSED ( blocking ) ; return false ; }
} ;
2017-11-29 15:54:33 +00:00
/// \brief Base class for unflushable filters
/// \tparam T the class or type
2015-11-05 06:59:46 +00:00
template < class T >
class CRYPTOPP_NO_VTABLE Unflushable : public T
{
public :
2019-08-17 18:49:03 +00:00
/// \brief Flush buffered input and/or output, with signal propagation
2019-09-26 17:45:31 +00:00
/// \param completeFlush is used to indicate whether all data should be flushed
2019-08-17 18:49:03 +00:00
/// \param propagation the number of attached transformations the Flush()
/// signal should be passed
/// \param blocking specifies whether the object should block when processing
/// input
/// \details propagation count includes this object. Setting propagation to
/// <tt>1</tt> means this object only. Setting propagation to <tt>-1</tt>
/// means unlimited propagation.
/// \note Hard flushes must be used with care. It means try to process and
/// output everything, even if there may not be enough data to complete the
/// action. For example, hard flushing a HexDecoder would cause an error if
/// you do it after inputing an odd number of hex encoded characters.
/// \note For some types of filters, like ZlibDecompressor, hard flushes can
/// only be done at "synchronization points". These synchronization points
/// are positions in the data stream that are created by hard flushes on the
/// corresponding reverse filters, in this example ZlibCompressor. This is
/// useful when zlib compressed data is moved across a network in packets
/// and compression state is preserved across packets, as in the SSH2 protocol.
2015-11-05 06:59:46 +00:00
bool Flush ( bool completeFlush , int propagation = - 1 , bool blocking = true )
{ return ChannelFlush ( DEFAULT_CHANNEL , completeFlush , propagation , blocking ) ; }
2019-08-17 18:49:03 +00:00
/// \brief Flushes data buffered by this object, without signal propagation
/// \param hardFlush indicates whether all data should be flushed
/// \param blocking specifies whether the object should block when processing input
/// \note hardFlush must be used with care
2015-11-05 06:59:46 +00:00
bool IsolatedFlush ( bool hardFlush , bool blocking )
2016-09-16 15:27:15 +00:00
{ CRYPTOPP_UNUSED ( hardFlush ) ; CRYPTOPP_UNUSED ( blocking ) ; CRYPTOPP_ASSERT ( false ) ; return false ; }
2019-08-17 18:49:03 +00:00
/// \brief Flush buffered input and/or output on a channel
/// \param channel the channel to flush the data
/// \param hardFlush is used to indicate whether all data should be flushed
/// \param propagation the number of attached transformations the ChannelFlush()
/// signal should be passed
/// \param blocking specifies whether the object should block when processing input
/// \return true of the Flush was successful
/// \details propagation count includes this object. Setting propagation to
/// <tt>1</tt> means this object only. Setting propagation to <tt>-1</tt> means
/// unlimited propagation.
2015-11-05 06:59:46 +00:00
bool ChannelFlush ( const std : : string & channel , bool hardFlush , int propagation = - 1 , bool blocking = true )
{
if ( hardFlush & & ! InputBufferIsEmpty ( ) )
throw CannotFlush ( " Unflushable<T>: this object has buffered input that cannot be flushed " ) ;
2016-09-10 08:57:48 +00:00
else
2015-11-05 06:59:46 +00:00
{
BufferedTransformation * attached = this - > AttachedTransformation ( ) ;
return attached & & propagation ? attached - > ChannelFlush ( channel , hardFlush , propagation - 1 , blocking ) : false ;
}
}
protected :
virtual bool InputBufferIsEmpty ( ) const { return false ; }
} ;
2017-11-29 15:54:33 +00:00
/// \brief Base class for input rejecting filters
/// \tparam T the class or type
/// \details T should be a BufferedTransformation derived class
2015-11-05 06:59:46 +00:00
template < class T >
class CRYPTOPP_NO_VTABLE InputRejecting : public T
{
public :
struct InputRejected : public NotImplemented
{ InputRejected ( ) : NotImplemented ( " BufferedTransformation: this object doesn't allow input " ) { } } ;
2017-11-29 15:54:33 +00:00
/// \name INPUT
2015-11-18 20:32:28 +00:00
//@{
2017-11-29 15:54:33 +00:00
/// \brief Input a byte array for processing
/// \param inString the byte array to process
/// \param length the size of the string, in bytes
/// \param messageEnd means how many filters to signal MessageEnd() to, including this one
/// \param blocking specifies whether the object should block when processing input
/// \throws InputRejected
2020-01-01 03:32:54 +00:00
/// \return the number of bytes that remain to be processed (i.e., bytes not processed)
2017-11-29 15:54:33 +00:00
/// \details Internally, the default implementation throws InputRejected.
2015-11-18 20:32:28 +00:00
size_t Put2 ( const byte * inString , size_t length , int messageEnd , bool blocking )
{ CRYPTOPP_UNUSED ( inString ) ; CRYPTOPP_UNUSED ( length ) ; CRYPTOPP_UNUSED ( messageEnd ) ; CRYPTOPP_UNUSED ( blocking ) ; throw InputRejected ( ) ; }
//@}
2017-11-29 15:54:33 +00:00
/// \name SIGNALS
2015-11-18 20:32:28 +00:00
//@{
2020-01-01 03:25:23 +00:00
/// \brief Flushes data buffered by this object, without signal propagation
/// \param hardFlush indicates whether all data should be flushed
/// \param blocking specifies whether the object should block when processing input
/// \note hardFlush must be used with care
2015-11-05 06:59:46 +00:00
bool IsolatedFlush ( bool hardFlush , bool blocking )
{ CRYPTOPP_UNUSED ( hardFlush ) ; CRYPTOPP_UNUSED ( blocking ) ; return false ; }
2020-01-01 03:25:23 +00:00
/// \brief Marks the end of a series of messages, without signal propagation
/// \param blocking specifies whether the object should block when completing the processing on
/// the current series of messages
/// \return true if the message was successful, false otherwise
2015-11-05 06:59:46 +00:00
bool IsolatedMessageSeriesEnd ( bool blocking )
{ CRYPTOPP_UNUSED ( blocking ) ; throw InputRejected ( ) ; }
2020-01-01 03:25:23 +00:00
/// \brief Input multiple bytes for processing on a channel.
/// \param channel the channel to process the data.
/// \param inString the byte buffer to process.
/// \param length the size of the string, in bytes.
/// \param messageEnd means how many filters to signal MessageEnd() to, including this one.
/// \param blocking specifies whether the object should block when processing input.
/// \return the number of bytes that remain to be processed (i.e., bytes not processed)
2015-11-18 20:32:28 +00:00
size_t ChannelPut2 ( const std : : string & channel , const byte * inString , size_t length , int messageEnd , bool blocking )
2020-01-01 03:25:23 +00:00
{ CRYPTOPP_UNUSED ( channel ) ; CRYPTOPP_UNUSED ( inString ) ; CRYPTOPP_UNUSED ( length ) ;
CRYPTOPP_UNUSED ( messageEnd ) ; CRYPTOPP_UNUSED ( blocking ) ; throw InputRejected ( ) ; }
/// \brief Marks the end of a series of messages on a channel
/// \param channel the channel to signal the end of a series of messages
/// \param messageEnd the number of attached transformations the ChannelMessageSeriesEnd() signal should be passed
/// \param blocking specifies whether the object should block when processing input
/// \return true if the message was successful, false otherwise
/// \details Each object that receives the signal will perform its processing, decrement
/// propagation, and then pass the signal on to attached transformations if the value is not 0.
/// \details propagation count includes this object. Setting propagation to <tt>1</tt> means this
/// object only. Setting propagation to <tt>-1</tt> means unlimited propagation.
/// \note There should be a MessageEnd() immediately before MessageSeriesEnd().
2015-11-05 06:59:46 +00:00
bool ChannelMessageSeriesEnd ( const std : : string & channel , int messageEnd , bool blocking )
{ CRYPTOPP_UNUSED ( channel ) ; CRYPTOPP_UNUSED ( messageEnd ) ; CRYPTOPP_UNUSED ( blocking ) ; throw InputRejected ( ) ; }
2015-11-18 20:32:28 +00:00
//@}
2015-11-05 06:59:46 +00:00
} ;
2017-11-29 15:54:33 +00:00
/// \brief Interface for custom flush signals propagation
/// \tparam T BufferedTransformation derived class
2015-11-05 06:59:46 +00:00
template < class T >
class CRYPTOPP_NO_VTABLE CustomFlushPropagation : public T
{
public :
2017-11-29 15:54:33 +00:00
/// \name SIGNALS
2015-11-18 20:32:28 +00:00
//@{
2016-10-12 06:57:28 +00:00
2017-11-29 15:54:33 +00:00
/// \brief Flush buffered input and/or output, with signal propagation
/// \param hardFlush is used to indicate whether all data should be flushed
/// \param propagation the number of attached transformations the Flush() signal should be passed
/// \param blocking specifies whether the object should block when processing input
/// \details propagation count includes this object. Setting propagation to <tt>1</tt> means this
2020-01-01 03:25:23 +00:00
/// object only. Setting propagation to <tt>-1</tt> means unlimited propagation.
2017-11-29 15:54:33 +00:00
/// \note Hard flushes must be used with care. It means try to process and output everything, even if
2020-01-01 03:25:23 +00:00
/// there may not be enough data to complete the action. For example, hard flushing a HexDecoder
/// would cause an error if you do it after inputing an odd number of hex encoded characters.
2017-11-29 15:54:33 +00:00
/// \note For some types of filters, like ZlibDecompressor, hard flushes can only
2020-01-01 03:25:23 +00:00
/// be done at "synchronization points". These synchronization points are positions in the data
/// stream that are created by hard flushes on the corresponding reverse filters, in this
/// example ZlibCompressor. This is useful when zlib compressed data is moved across a
/// network in packets and compression state is preserved across packets, as in the SSH2 protocol.
2015-11-05 06:59:46 +00:00
virtual bool Flush ( bool hardFlush , int propagation = - 1 , bool blocking = true ) = 0 ;
2016-10-12 06:57:28 +00:00
2015-11-18 20:32:28 +00:00
//@}
2015-11-05 06:59:46 +00:00
private :
bool IsolatedFlush ( bool hardFlush , bool blocking )
2016-09-16 15:27:15 +00:00
{ CRYPTOPP_UNUSED ( hardFlush ) ; CRYPTOPP_UNUSED ( blocking ) ; CRYPTOPP_ASSERT ( false ) ; return false ; }
2015-11-05 06:59:46 +00:00
} ;
2017-11-29 15:54:33 +00:00
/// \brief Interface for custom flush signals
/// \tparam T BufferedTransformation derived class
2015-11-05 06:59:46 +00:00
template < class T >
class CRYPTOPP_NO_VTABLE CustomSignalPropagation : public CustomFlushPropagation < T >
{
public :
2017-11-29 15:54:33 +00:00
/// \brief Initialize or reinitialize this object, with signal propagation
/// \param parameters a set of NameValuePairs to initialize or reinitialize this object
/// \param propagation the number of attached transformations the Initialize() signal should be passed
/// \details Initialize() is used to initialize or reinitialize an object using a variable number of
2020-01-01 03:25:23 +00:00
/// arbitrarily typed arguments. The function avoids the need for multiple constructors providing
/// all possible combintations of configurable parameters.
2017-11-29 15:54:33 +00:00
/// \details propagation count includes this object. Setting propagation to <tt>1</tt> means this
2020-01-01 03:25:23 +00:00
/// object only. Setting propagation to <tt>-1</tt> means unlimited propagation.
2015-11-05 06:59:46 +00:00
virtual void Initialize ( const NameValuePairs & parameters = g_nullNameValuePairs , int propagation = - 1 ) = 0 ;
private :
void IsolatedInitialize ( const NameValuePairs & parameters )
2016-09-16 15:27:15 +00:00
{ CRYPTOPP_UNUSED ( parameters ) ; CRYPTOPP_ASSERT ( false ) ; }
2015-11-05 06:59:46 +00:00
} ;
2017-11-29 15:54:33 +00:00
/// \brief Multiple channels support for custom signal processing
/// \tparam T the class or type
/// \details T should be a BufferedTransformation derived class
2015-11-05 06:59:46 +00:00
template < class T >
class CRYPTOPP_NO_VTABLE Multichannel : public CustomFlushPropagation < T >
{
public :
bool Flush ( bool hardFlush , int propagation = - 1 , bool blocking = true )
{ return this - > ChannelFlush ( DEFAULT_CHANNEL , hardFlush , propagation , blocking ) ; }
2016-10-12 06:57:28 +00:00
2017-11-29 15:54:33 +00:00
/// \brief Marks the end of a series of messages, with signal propagation
/// \param propagation the number of attached transformations the MessageSeriesEnd() signal should be passed
/// \param blocking specifies whether the object should block when processing input
/// \details Each object that receives the signal will perform its processing, decrement
2020-01-01 03:25:23 +00:00
/// propagation, and then pass the signal on to attached transformations if the value is not 0.
2017-11-29 15:54:33 +00:00
/// \details propagation count includes this object. Setting propagation to <tt>1</tt> means this
2020-01-01 03:25:23 +00:00
/// object only. Setting propagation to <tt>-1</tt> means unlimited propagation.
2017-11-29 15:54:33 +00:00
/// \note There should be a MessageEnd() immediately before MessageSeriesEnd().
2015-11-05 06:59:46 +00:00
bool MessageSeriesEnd ( int propagation = - 1 , bool blocking = true )
{ return this - > ChannelMessageSeriesEnd ( DEFAULT_CHANNEL , propagation , blocking ) ; }
2016-10-12 06:57:28 +00:00
2017-11-29 15:54:33 +00:00
/// \brief Request space which can be written into by the caller
/// \param size the requested size of the buffer
/// \details The purpose of this method is to help avoid extra memory allocations.
/// \details size is an \a IN and \a OUT parameter and used as a hint. When the call is made,
2020-01-01 03:25:23 +00:00
/// size is the requested size of the buffer. When the call returns, size is the size of
/// the array returned to the caller.
2017-11-29 15:54:33 +00:00
/// \details The base class implementation sets size to 0 and returns NULL.
/// \note Some objects, like ArraySink, cannot create a space because its fixed. In the case of
2020-01-01 03:25:23 +00:00
/// an ArraySink, the pointer to the array is returned and the size is remaining size.
2015-11-05 06:59:46 +00:00
byte * CreatePutSpace ( size_t & size )
{ return this - > ChannelCreatePutSpace ( DEFAULT_CHANNEL , size ) ; }
2016-10-12 06:57:28 +00:00
2017-11-29 15:54:33 +00:00
/// \brief Input multiple bytes for processing
/// \param inString the byte buffer to process
/// \param length the size of the string, in bytes
/// \param messageEnd means how many filters to signal MessageEnd() to, including this one
/// \param blocking specifies whether the object should block when processing input
2020-01-01 03:32:54 +00:00
/// \return the number of bytes that remain to be processed (i.e., bytes not processed)
2017-11-29 15:54:33 +00:00
/// \details Derived classes must implement Put2().
2015-11-18 20:32:28 +00:00
size_t Put2 ( const byte * inString , size_t length , int messageEnd , bool blocking )
{ return this - > ChannelPut2 ( DEFAULT_CHANNEL , inString , length , messageEnd , blocking ) ; }
2016-10-12 06:57:28 +00:00
2017-11-29 15:54:33 +00:00
/// \brief Input multiple bytes that may be modified by callee.
/// \param inString the byte buffer to process.
/// \param length the size of the string, in bytes.
/// \param messageEnd means how many filters to signal MessageEnd() to, including this one.
/// \param blocking specifies whether the object should block when processing input.
2020-01-01 03:32:54 +00:00
/// \return the number of bytes that remain to be processed (i.e., bytes not processed)
2017-11-29 15:54:33 +00:00
/// \details Internally, PutModifiable2() calls Put2().
2015-11-05 06:59:46 +00:00
size_t PutModifiable2 ( byte * inString , size_t length , int messageEnd , bool blocking )
{ return this - > ChannelPutModifiable2 ( DEFAULT_CHANNEL , inString , length , messageEnd , blocking ) ; }
2020-01-01 03:25:23 +00:00
// void ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1)
// {PropagateMessageSeriesEnd(propagation, channel);}
/// \brief Request space which can be written into by the caller
/// \param channel the channel to process the data
/// \param size the requested size of the buffer
/// \return a pointer to a memory block with length size
/// \details The purpose of this method is to help avoid extra memory allocations.
/// \details size is an \a IN and \a OUT parameter and used as a hint. When the call is made,
/// size is the requested size of the buffer. When the call returns, size is the size of
/// the array returned to the caller.
/// \details The base class implementation sets size to 0 and returns NULL.
/// \note Some objects, like ArraySink(), cannot create a space because its fixed. In the case of
/// an ArraySink(), the pointer to the array is returned and the size is remaining size.
2015-11-05 06:59:46 +00:00
byte * ChannelCreatePutSpace ( const std : : string & channel , size_t & size )
2017-03-01 11:10:06 +00:00
{ CRYPTOPP_UNUSED ( channel ) ; size = 0 ; return NULLPTR ; }
2020-01-01 03:25:23 +00:00
/// \brief Input multiple bytes that may be modified by callee on a channel
/// \param channel the channel to process the data.
/// \param inString the byte buffer to process
/// \param length the size of the string, in bytes
/// \return true if all bytes were processed, false otherwise.
2015-11-05 06:59:46 +00:00
bool ChannelPutModifiable ( const std : : string & channel , byte * inString , size_t length )
{ this - > ChannelPut ( channel , inString , length ) ; return false ; }
2020-01-01 03:25:23 +00:00
/// \brief Input multiple bytes for processing on a channel.
/// \param channel the channel to process the data.
/// \param begin the byte buffer to process.
/// \param length the size of the string, in bytes.
/// \param messageEnd means how many filters to signal MessageEnd() to, including this one.
/// \param blocking specifies whether the object should block when processing input.
/// \return the number of bytes that remain to be processed (i.e., bytes not processed)
2015-11-05 06:59:46 +00:00
virtual size_t ChannelPut2 ( const std : : string & channel , const byte * begin , size_t length , int messageEnd , bool blocking ) = 0 ;
2020-01-01 03:25:23 +00:00
/// \brief Input multiple bytes that may be modified by callee on a channel
/// \param channel the channel to process the data
/// \param begin the byte buffer to process
/// \param length the size of the string, in bytes
/// \param messageEnd means how many filters to signal MessageEnd() to, including this one
/// \param blocking specifies whether the object should block when processing input
/// \return the number of bytes that remain to be processed (i.e., bytes not processed)
2015-11-05 06:59:46 +00:00
size_t ChannelPutModifiable2 ( const std : : string & channel , byte * begin , size_t length , int messageEnd , bool blocking )
{ return ChannelPut2 ( channel , begin , length , messageEnd , blocking ) ; }
2020-01-01 03:25:23 +00:00
/// \brief Flush buffered input and/or output on a channel
/// \param channel the channel to flush the data
/// \param hardFlush is used to indicate whether all data should be flushed
/// \param propagation the number of attached transformations the ChannelFlush() signal should be passed
/// \param blocking specifies whether the object should block when processing input
/// \return true of the Flush was successful
/// \details propagation count includes this object. Setting propagation to <tt>1</tt> means this
/// object only. Setting propagation to <tt>-1</tt> means unlimited propagation.
2015-11-05 06:59:46 +00:00
virtual bool ChannelFlush ( const std : : string & channel , bool hardFlush , int propagation = - 1 , bool blocking = true ) = 0 ;
} ;
2017-11-29 15:54:33 +00:00
/// \brief Provides auto signaling support
/// \tparam T BufferedTransformation derived class
2015-11-05 06:59:46 +00:00
template < class T >
class CRYPTOPP_NO_VTABLE AutoSignaling : public T
{
public :
2017-11-29 15:54:33 +00:00
/// \brief Construct an AutoSignaling
/// \param propagation the propagation count
2015-11-05 06:59:46 +00:00
AutoSignaling ( int propagation = - 1 ) : m_autoSignalPropagation ( propagation ) { }
2020-01-01 03:25:23 +00:00
/// \brief Set propagation of automatically generated and transferred signals
/// \param propagation then new value
/// \details Setting propagation to <tt>0</tt> means do not automatically generate signals. Setting
/// propagation to <tt>-1</tt> means unlimited propagation.
2015-11-05 06:59:46 +00:00
void SetAutoSignalPropagation ( int propagation )
{ m_autoSignalPropagation = propagation ; }
2020-01-01 03:25:23 +00:00
/// \brief Retrieve automatic signal propagation value
/// \return the number of attached transformations the signal is propagated to. 0 indicates
/// the signal is only witnessed by this object
2015-11-05 06:59:46 +00:00
int GetAutoSignalPropagation ( ) const
{ return m_autoSignalPropagation ; }
private :
int m_autoSignalPropagation ;
} ;
2017-11-29 15:54:33 +00:00
/// \brief Acts as a Source for pre-existing, static data
2015-11-05 06:59:46 +00:00
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Store : public AutoSignaling < InputRejecting < BufferedTransformation > >
{
public :
2017-11-29 15:54:33 +00:00
/// \brief Construct a Store
2015-11-05 06:59:46 +00:00
Store ( ) : m_messageEnd ( false ) { }
void IsolatedInitialize ( const NameValuePairs & parameters )
{
m_messageEnd = false ;
StoreInitialize ( parameters ) ;
}
unsigned int NumberOfMessages ( ) const { return m_messageEnd ? 0 : 1 ; }
bool GetNextMessage ( ) ;
unsigned int CopyMessagesTo ( BufferedTransformation & target , unsigned int count = UINT_MAX , const std : : string & channel = DEFAULT_CHANNEL ) const ;
protected :
virtual void StoreInitialize ( const NameValuePairs & parameters ) = 0 ;
bool m_messageEnd ;
} ;
2017-11-29 15:54:33 +00:00
/// \brief Implementation of BufferedTransformation's attachment interface
/// \details Sink is a cornerstone of the Pipeline trinitiy. Data flows from
2020-01-01 03:51:32 +00:00
/// 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.
2017-11-29 15:54:33 +00:00
/// \details A Sink doesnot produce any retrievable output.
/// \details See the discussion of BufferedTransformation in cryptlib.h for
2020-01-01 03:51:32 +00:00
/// more details.
2015-11-05 06:59:46 +00:00
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Sink : public BufferedTransformation
{
public :
size_t TransferTo2 ( BufferedTransformation & target , lword & transferBytes , const std : : string & channel = DEFAULT_CHANNEL , bool blocking = true )
{ CRYPTOPP_UNUSED ( target ) ; CRYPTOPP_UNUSED ( transferBytes ) ; CRYPTOPP_UNUSED ( channel ) ; CRYPTOPP_UNUSED ( blocking ) ; transferBytes = 0 ; return 0 ; }
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 ) ; return 0 ; }
} ;
2017-11-29 15:54:33 +00:00
/// \brief Acts as an input discarding Filter or Sink
/// \details The BitBucket discards all input and returns 0 to the caller
2020-01-01 03:51:32 +00:00
/// to indicate all data was processed.
2015-11-05 06:59:46 +00:00
class CRYPTOPP_DLL BitBucket : public Bufferless < Sink >
{
public :
std : : string AlgorithmName ( ) const { return " BitBucket " ; }
void IsolatedInitialize ( const NameValuePairs & params )
{ CRYPTOPP_UNUSED ( params ) ; }
2015-11-18 20:32:28 +00:00
size_t Put2 ( const byte * inString , size_t length , int messageEnd , bool blocking )
{ CRYPTOPP_UNUSED ( inString ) ; CRYPTOPP_UNUSED ( length ) ; CRYPTOPP_UNUSED ( messageEnd ) ; CRYPTOPP_UNUSED ( blocking ) ; return 0 ; }
2015-11-05 06:59:46 +00:00
} ;
NAMESPACE_END
# if CRYPTOPP_MSC_VERSION
# pragma warning(pop)
# endif
# endif