filters.cpp
// filters.cpp - written and placed in the public domain by Wei Dai
#include "pch.h"
#include "config.h"
#if CRYPTOPP_MSC_VERSION
# pragma warning(disable: 4100 4189)
#endif
#if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
# pragma GCC diagnostic ignored "-Wunused-value"
#endif
#ifndef CRYPTOPP_IMPORTS
#include "filters.h"
#include "mqueue.h"
#include "fltrimpl.h"
#include "argnames.h"
#include "smartptr.h"
#include "stdcpp.h"
#include "misc.h"
NAMESPACE_BEGIN(CryptoPP)
Filter::Filter(BufferedTransformation *attachment)
: m_attachment(attachment), m_inputPosition(0), m_continueAt(0)
{
}
BufferedTransformation * Filter::NewDefaultAttachment() const
{
return new MessageQueue;
}
BufferedTransformation * Filter::AttachedTransformation()
{
if (m_attachment.get() == NULL)
m_attachment.reset(NewDefaultAttachment());
return m_attachment.get();
}
const BufferedTransformation *Filter::AttachedTransformation() const
{
if (m_attachment.get() == NULL)
const_cast<Filter *>(this)->m_attachment.reset(NewDefaultAttachment());
return m_attachment.get();
}
void Filter::Detach(BufferedTransformation *newOut)
{
m_attachment.reset(newOut);
}
void Filter::Insert(Filter *filter)
{
filter->m_attachment.reset(m_attachment.release());
m_attachment.reset(filter);
}
size_t Filter::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
{
return AttachedTransformation()->CopyRangeTo2(target, begin, end, channel, blocking);
}
size_t Filter::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
{
return AttachedTransformation()->TransferTo2(target, transferBytes, channel, blocking);
}
void Filter::Initialize(const NameValuePairs &parameters, int propagation)
{
m_inputPosition = m_continueAt = 0;
IsolatedInitialize(parameters);
PropagateInitialize(parameters, propagation);
}
bool Filter::Flush(bool hardFlush, int propagation, bool blocking)
{
switch (m_continueAt)
{
case 0:
if (IsolatedFlush(hardFlush, blocking))
return true;
// fall through
case 1:
if (OutputFlush(1, hardFlush, propagation, blocking))
return true;
// fall through
default: ;;
}
return false;
}
bool Filter::MessageSeriesEnd(int propagation, bool blocking)
{
switch (m_continueAt)
{
case 0:
if (IsolatedMessageSeriesEnd(blocking))
return true;
// fall through
case 1:
if (ShouldPropagateMessageSeriesEnd() && OutputMessageSeriesEnd(1, propagation, blocking))
return true;
// fall through
default: ;;
}
return false;
}
void Filter::PropagateInitialize(const NameValuePairs &parameters, int propagation)
{
if (propagation)
AttachedTransformation()->Initialize(parameters, propagation-1);
}
size_t Filter::OutputModifiable(int outputSite, byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel)
{
if (messageEnd)
messageEnd--;
size_t result = AttachedTransformation()->ChannelPutModifiable2(channel, inString, length, messageEnd, blocking);
m_continueAt = result ? outputSite : 0;
return result;
}
size_t Filter::Output(int outputSite, const byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel)
{
if (messageEnd)
messageEnd--;
size_t result = AttachedTransformation()->ChannelPut2(channel, inString, length, messageEnd, blocking);
m_continueAt = result ? outputSite : 0;
return result;
}
bool Filter::OutputFlush(int outputSite, bool hardFlush, int propagation, bool blocking, const std::string &channel)
{
if (propagation && AttachedTransformation()->ChannelFlush(channel, hardFlush, propagation-1, blocking))
{
m_continueAt = outputSite;
return true;
}
m_continueAt = 0;
return false;
}
bool Filter::OutputMessageSeriesEnd(int outputSite, int propagation, bool blocking, const std::string &channel)
{
if (propagation && AttachedTransformation()->ChannelMessageSeriesEnd(channel, propagation-1, blocking))
{
m_continueAt = outputSite;
return true;
}
m_continueAt = 0;
return false;
}
// *************************************************************
void MeterFilter::ResetMeter()
{
m_currentMessageBytes = m_totalBytes = m_currentSeriesMessages = m_totalMessages = m_totalMessageSeries = 0;
m_rangesToSkip.clear();
}
void MeterFilter::AddRangeToSkip(unsigned int message, lword position, lword size, bool sortNow)
{
MessageRange r = {message, position, size};
m_rangesToSkip.push_back(r);
if (sortNow)
std::sort(m_rangesToSkip.begin(), m_rangesToSkip.end());
}
size_t MeterFilter::PutMaybeModifiable(byte *begin, size_t length, int messageEnd, bool blocking, bool modifiable)
{
if (!m_transparent)
return 0;
size_t t;
FILTER_BEGIN;
m_begin = begin;
m_length = length;
while (m_length > 0 || messageEnd)
{
if (m_length > 0 && !m_rangesToSkip.empty() && m_rangesToSkip.front().message == m_totalMessages && m_currentMessageBytes + m_length > m_rangesToSkip.front().position)
{
FILTER_OUTPUT_MAYBE_MODIFIABLE(1, m_begin, t = (size_t)SaturatingSubtract(m_rangesToSkip.front().position, m_currentMessageBytes), false, modifiable);
assert(t < m_length);
m_begin += t;
m_length -= t;
m_currentMessageBytes += t;
m_totalBytes += t;
if (m_currentMessageBytes + m_length < m_rangesToSkip.front().position + m_rangesToSkip.front().size)
t = m_length;
else
{
t = (size_t)SaturatingSubtract(m_rangesToSkip.front().position + m_rangesToSkip.front().size, m_currentMessageBytes);
assert(t <= m_length);
m_rangesToSkip.pop_front();
}
m_begin += t;
m_length -= t;
m_currentMessageBytes += t;
m_totalBytes += t;
}
else
{
FILTER_OUTPUT_MAYBE_MODIFIABLE(2, m_begin, m_length, messageEnd, modifiable);
m_currentMessageBytes += m_length;
m_totalBytes += m_length;
m_length = 0;
if (messageEnd)
{
m_currentMessageBytes = 0;
m_currentSeriesMessages++;
m_totalMessages++;
messageEnd = false;
}
}
}
FILTER_END_NO_MESSAGE_END;
}
size_t MeterFilter::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
{
return PutMaybeModifiable(const_cast<byte *>(begin), length, messageEnd, blocking, false);
}
size_t MeterFilter::PutModifiable2(byte *begin, size_t length, int messageEnd, bool blocking)
{
return PutMaybeModifiable(begin, length, messageEnd, blocking, true);
}
bool MeterFilter::IsolatedMessageSeriesEnd(bool blocking)
{
CRYPTOPP_UNUSED(blocking);
m_currentMessageBytes = 0;
m_currentSeriesMessages = 0;
m_totalMessageSeries++;
return false;
}
// *************************************************************
void FilterWithBufferedInput::BlockQueue::ResetQueue(size_t blockSize, size_t maxBlocks)
{
m_buffer.New(blockSize * maxBlocks);
m_blockSize = blockSize;
m_maxBlocks = maxBlocks;
m_size = 0;
m_begin = m_buffer;
}
byte *FilterWithBufferedInput::BlockQueue::GetBlock()
{
if (m_size >= m_blockSize)
{
byte *ptr = m_begin;
if ((m_begin+=m_blockSize) == m_buffer.end())
m_begin = m_buffer;
m_size -= m_blockSize;
return ptr;
}
else
return NULL;
}
byte *FilterWithBufferedInput::BlockQueue::GetContigousBlocks(size_t &numberOfBytes)
{
numberOfBytes = STDMIN(numberOfBytes, STDMIN(size_t(m_buffer.end()-m_begin), m_size));
byte *ptr = m_begin;
m_begin += numberOfBytes;
m_size -= numberOfBytes;
if (m_size == 0 || m_begin == m_buffer.end())
m_begin = m_buffer;
return ptr;
}
size_t FilterWithBufferedInput::BlockQueue::GetAll(byte *outString)
{
// Avoid passing NULL pointer to memcpy
if (!outString) return 0;
size_t size = m_size;
size_t numberOfBytes = m_maxBlocks*m_blockSize;
const byte *ptr = GetContigousBlocks(numberOfBytes);
memcpy(outString, ptr, numberOfBytes);
memcpy(outString+numberOfBytes, m_begin, m_size);
m_size = 0;
return size;
}
void FilterWithBufferedInput::BlockQueue::Put(const byte *inString, size_t length)
{
// Avoid passing NULL pointer to memcpy
if (!inString || !length) return;
assert(m_size + length <= m_buffer.size());
byte *end = (m_size < size_t(m_buffer.end()-m_begin)) ? m_begin + m_size : m_begin + m_size - m_buffer.size();
size_t len = STDMIN(length, size_t(m_buffer.end()-end));
memcpy(end, inString, len);
if (len < length)
memcpy(m_buffer, inString+len, length-len);
m_size += length;
}
#if !defined(CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562)
FilterWithBufferedInput::FilterWithBufferedInput()
: Filter(), m_firstSize(SIZE_MAX), m_blockSize(0), m_lastSize(SIZE_MAX), m_firstInputDone(false)
{
}
#endif
FilterWithBufferedInput::FilterWithBufferedInput(BufferedTransformation *attachment)
: Filter(attachment), m_firstSize(SIZE_MAX), m_blockSize(0), m_lastSize(SIZE_MAX), m_firstInputDone(false)
{
}
FilterWithBufferedInput::FilterWithBufferedInput(size_t firstSize, size_t blockSize, size_t lastSize, BufferedTransformation *attachment)
: Filter(attachment), m_firstSize(firstSize), m_blockSize(blockSize), m_lastSize(lastSize), m_firstInputDone(false)
{
if (m_firstSize == SIZE_MAX || m_blockSize < 1 || m_lastSize == SIZE_MAX)
throw InvalidArgument("FilterWithBufferedInput: invalid buffer size");
m_queue.ResetQueue(1, m_firstSize);
}
void FilterWithBufferedInput::IsolatedInitialize(const NameValuePairs &parameters)
{
InitializeDerivedAndReturnNewSizes(parameters, m_firstSize, m_blockSize, m_lastSize);
if (m_firstSize == SIZE_MAX || m_blockSize < 1 || m_lastSize == SIZE_MAX)
throw InvalidArgument("FilterWithBufferedInput: invalid buffer size");
m_queue.ResetQueue(1, m_firstSize);
m_firstInputDone = false;
}
bool FilterWithBufferedInput::IsolatedFlush(bool hardFlush, bool blocking)
{
if (!blocking)
throw BlockingInputOnly("FilterWithBufferedInput");
if (hardFlush)
ForceNextPut();
FlushDerived();
return false;
}
size_t FilterWithBufferedInput::PutMaybeModifiable(byte *inString, size_t length, int messageEnd, bool blocking, bool modifiable)
{
if (!blocking)
throw BlockingInputOnly("FilterWithBufferedInput");
if (length != 0)
{
size_t newLength = m_queue.CurrentSize() + length;
if (!m_firstInputDone && newLength >= m_firstSize)
{
size_t len = m_firstSize - m_queue.CurrentSize();
m_queue.Put(inString, len);
FirstPut(m_queue.GetContigousBlocks(m_firstSize));
assert(m_queue.CurrentSize() == 0);
m_queue.ResetQueue(m_blockSize, (2*m_blockSize+m_lastSize-2)/m_blockSize);
inString += len;
newLength -= m_firstSize;
m_firstInputDone = true;
}
if (m_firstInputDone)
{
if (m_blockSize == 1)
{
while (newLength > m_lastSize && m_queue.CurrentSize() > 0)
{
size_t len = newLength - m_lastSize;
byte *ptr = m_queue.GetContigousBlocks(len);
NextPutModifiable(ptr, len);
newLength -= len;
}
if (newLength > m_lastSize)
{
size_t len = newLength - m_lastSize;
NextPutMaybeModifiable(inString, len, modifiable);
inString += len;
newLength -= len;
}
}
else
{
while (newLength >= m_blockSize + m_lastSize && m_queue.CurrentSize() >= m_blockSize)
{
NextPutModifiable(m_queue.GetBlock(), m_blockSize);
newLength -= m_blockSize;
}
if (newLength >= m_blockSize + m_lastSize && m_queue.CurrentSize() > 0)
{
assert(m_queue.CurrentSize() < m_blockSize);
size_t len = m_blockSize - m_queue.CurrentSize();
m_queue.Put(inString, len);
inString += len;
NextPutModifiable(m_queue.GetBlock(), m_blockSize);
newLength -= m_blockSize;
}
if (newLength >= m_blockSize + m_lastSize)
{
size_t len = RoundDownToMultipleOf(newLength - m_lastSize, m_blockSize);
NextPutMaybeModifiable(inString, len, modifiable);
inString += len;
newLength -= len;
}
}
}
m_queue.Put(inString, newLength - m_queue.CurrentSize());
}
if (messageEnd)
{
if (!m_firstInputDone && m_firstSize==0)
FirstPut(NULL);
SecByteBlock temp(m_queue.CurrentSize());
m_queue.GetAll(temp);
LastPut(temp, temp.size());
m_firstInputDone = false;
m_queue.ResetQueue(1, m_firstSize);
// Cast to void to supress Coverity finding
(void)Output(1, NULL, 0, messageEnd, blocking);
}
return 0;
}
void FilterWithBufferedInput::ForceNextPut()
{
if (!m_firstInputDone)
return;
if (m_blockSize > 1)
{
while (m_queue.CurrentSize() >= m_blockSize)
NextPutModifiable(m_queue.GetBlock(), m_blockSize);
}
else
{
size_t len;
while ((len = m_queue.CurrentSize()) > 0)
NextPutModifiable(m_queue.GetContigousBlocks(len), len);
}
}
void FilterWithBufferedInput::NextPutMultiple(const byte *inString, size_t length)
{
assert(m_blockSize > 1); // m_blockSize = 1 should always override this function
while (length > 0)
{
assert(length >= m_blockSize);
NextPutSingle(inString);
inString += m_blockSize;
length -= m_blockSize;
}
}
// *************************************************************
void Redirector::Initialize(const NameValuePairs &parameters, int propagation)
{
m_target = parameters.GetValueWithDefault("RedirectionTargetPointer", (BufferedTransformation*)NULL);
m_behavior = parameters.GetIntValueWithDefault("RedirectionBehavior", PASS_EVERYTHING);
if (m_target && GetPassSignals())
m_target->Initialize(parameters, propagation);
}
// *************************************************************
ProxyFilter::ProxyFilter(BufferedTransformation *filter, size_t firstSize, size_t lastSize, BufferedTransformation *attachment)
: FilterWithBufferedInput(firstSize, 1, lastSize, attachment), m_filter(filter)
{
if (m_filter.get())
m_filter->Attach(new OutputProxy(*this, false));
}
bool ProxyFilter::IsolatedFlush(bool hardFlush, bool blocking)
{
return m_filter.get() ? m_filter->Flush(hardFlush, -1, blocking) : false;
}
void ProxyFilter::SetFilter(Filter *filter)
{
m_filter.reset(filter);
if (filter)
{
OutputProxy *proxy;
member_ptr<OutputProxy> temp(proxy = new OutputProxy(*this, false));
m_filter->TransferAllTo(*proxy);
m_filter->Attach(temp.release());
}
}
void ProxyFilter::NextPutMultiple(const byte *s, size_t len)
{
if (m_filter.get())
m_filter->Put(s, len);
}
void ProxyFilter::NextPutModifiable(byte *s, size_t len)
{
if (m_filter.get())
m_filter->PutModifiable(s, len);
}
// *************************************************************
void RandomNumberSink::IsolatedInitialize(const NameValuePairs &parameters)
{
parameters.GetRequiredParameter("RandomNumberSink", "RandomNumberGeneratorPointer", m_rng);
}
size_t RandomNumberSink::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
{
CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking);
m_rng->IncorporateEntropy(begin, length);
return 0;
}
size_t ArraySink::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
{
CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking);
// Avoid passing NULL pointer to memcpy. Using memmove due to
// Valgrind finding on overlapping buffers.
size_t copied = 0;
if (m_buf && begin)
{
copied = STDMIN(length, SaturatingSubtract(m_size, m_total));
memmove(m_buf+m_total, begin, copied);
}
m_total += copied;
return length - copied;
}
byte * ArraySink::CreatePutSpace(size_t &size)
{
size = SaturatingSubtract(m_size, m_total);
return m_buf + m_total;
}
void ArraySink::IsolatedInitialize(const NameValuePairs &parameters)
{
ByteArrayParameter array;
if (!parameters.GetValue(Name::OutputBuffer(), array))
throw InvalidArgument("ArraySink: missing OutputBuffer argument");
m_buf = array.begin();
m_size = array.size();
}
size_t ArrayXorSink::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
{
CRYPTOPP_UNUSED(messageEnd); CRYPTOPP_UNUSED(blocking);
// Avoid passing NULL pointer to xorbuf
size_t copied = 0;
if (m_buf && begin)
{
copied = STDMIN(length, SaturatingSubtract(m_size, m_total));
xorbuf(m_buf+m_total, begin, copied);
}
m_total += copied;
return length - copied;
}
// *************************************************************
StreamTransformationFilter::StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment, BlockPaddingScheme padding, bool allowAuthenticatedSymmetricCipher)
: FilterWithBufferedInput(attachment)
, m_cipher(c), m_padding(DEFAULT_PADDING), m_optimalBufferSize(0)
{
assert(c.MinLastBlockSize() == 0 || c.MinLastBlockSize() > c.MandatoryBlockSize());
if (!allowAuthenticatedSymmetricCipher && dynamic_cast<AuthenticatedSymmetricCipher *>(&c) != 0)
throw InvalidArgument("StreamTransformationFilter: please use AuthenticatedEncryptionFilter and AuthenticatedDecryptionFilter for AuthenticatedSymmetricCipher");
IsolatedInitialize(MakeParameters(Name::BlockPaddingScheme(), padding));
}
size_t StreamTransformationFilter::LastBlockSize(StreamTransformation &c, BlockPaddingScheme padding)
{
if (c.MinLastBlockSize() > 0)
return c.MinLastBlockSize();
else if (c.MandatoryBlockSize() > 1 && !c.IsForwardTransformation() && padding != NO_PADDING && padding != ZEROS_PADDING)
return c.MandatoryBlockSize();
else
return 0;
}
void StreamTransformationFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
{
BlockPaddingScheme padding = parameters.GetValueWithDefault(Name::BlockPaddingScheme(), DEFAULT_PADDING);
bool isBlockCipher = (m_cipher.MandatoryBlockSize() > 1 && m_cipher.MinLastBlockSize() == 0);
if (padding == DEFAULT_PADDING)
m_padding = isBlockCipher ? PKCS_PADDING : NO_PADDING;
else
m_padding = padding;
if (!isBlockCipher && (m_padding == PKCS_PADDING || m_padding == ONE_AND_ZEROS_PADDING))
throw InvalidArgument("StreamTransformationFilter: PKCS_PADDING and ONE_AND_ZEROS_PADDING cannot be used with " + m_cipher.AlgorithmName());
firstSize = 0;
blockSize = m_cipher.MandatoryBlockSize();
lastSize = LastBlockSize(m_cipher, m_padding);
}
void StreamTransformationFilter::FirstPut(const byte* inString)
{
CRYPTOPP_UNUSED(inString);
m_optimalBufferSize = m_cipher.OptimalBlockSize();
m_optimalBufferSize = (unsigned int)STDMAX(m_optimalBufferSize, RoundDownToMultipleOf(4096U, m_optimalBufferSize));
}
void StreamTransformationFilter::NextPutMultiple(const byte *inString, size_t length)
{
if (!length)
return;
size_t s = m_cipher.MandatoryBlockSize();
do
{
size_t len = m_optimalBufferSize;
byte *space = HelpCreatePutSpace(*AttachedTransformation(), DEFAULT_CHANNEL, s, length, len);
if (len < length)
{
if (len == m_optimalBufferSize)
len -= m_cipher.GetOptimalBlockSizeUsed();
len = RoundDownToMultipleOf(len, s);
}
else
len = length;
m_cipher.ProcessString(space, inString, len);
AttachedTransformation()->PutModifiable(space, len);
inString += len;
length -= len;
}
while (length > 0);
}
void StreamTransformationFilter::NextPutModifiable(byte *inString, size_t length)
{
m_cipher.ProcessString(inString, length);
AttachedTransformation()->PutModifiable(inString, length);
}
void StreamTransformationFilter::LastPut(const byte *inString, size_t length)
{
byte *space = NULL;
switch (m_padding)
{
case NO_PADDING:
case ZEROS_PADDING:
if (length > 0)
{
size_t minLastBlockSize = m_cipher.MinLastBlockSize();
bool isForwardTransformation = m_cipher.IsForwardTransformation();
if (isForwardTransformation && m_padding == ZEROS_PADDING && (minLastBlockSize == 0 || length < minLastBlockSize))
{
// do padding
size_t blockSize = STDMAX(minLastBlockSize, (size_t)m_cipher.MandatoryBlockSize());
space = HelpCreatePutSpace(*AttachedTransformation(), DEFAULT_CHANNEL, blockSize);
if (inString) {memcpy(space, inString, length);}
memset(space + length, 0, blockSize - length);
m_cipher.ProcessLastBlock(space, space, blockSize);
AttachedTransformation()->Put(space, blockSize);
}
else
{
if (minLastBlockSize == 0)
{
if (isForwardTransformation)
throw InvalidDataFormat("StreamTransformationFilter: plaintext length is not a multiple of block size and NO_PADDING is specified");
else
throw InvalidCiphertext("StreamTransformationFilter: ciphertext length is not a multiple of block size");
}
space = HelpCreatePutSpace(*AttachedTransformation(), DEFAULT_CHANNEL, length, m_optimalBufferSize);
m_cipher.ProcessLastBlock(space, inString, length);
AttachedTransformation()->Put(space, length);
}
}
break;
case PKCS_PADDING:
case ONE_AND_ZEROS_PADDING:
unsigned int s;
s = m_cipher.MandatoryBlockSize();
assert(s > 1);
space = HelpCreatePutSpace(*AttachedTransformation(), DEFAULT_CHANNEL, s, m_optimalBufferSize);
if (m_cipher.IsForwardTransformation())
{
assert(length < s);
if (inString) {memcpy(space, inString, length);}
if (m_padding == PKCS_PADDING)
{
assert(s < 256);
byte pad = byte(s-length);
memset(space+length, pad, s-length);
}
else
{
space[length] = 0x80;
memset(space+length+1, 0, s-length-1);
}
m_cipher.ProcessData(space, space, s);
AttachedTransformation()->Put(space, s);
}
else
{
if (length != s)
throw InvalidCiphertext("StreamTransformationFilter: ciphertext length is not a multiple of block size");
m_cipher.ProcessData(space, inString, s);
if (m_padding == PKCS_PADDING)
{
byte pad = space[s-1];
if (pad < 1 || pad > s || std::find_if(space+s-pad, space+s, std::bind2nd(std::not_equal_to<byte>(), pad)) != space+s)
throw InvalidCiphertext("StreamTransformationFilter: invalid PKCS #7 block padding found");
length = s-pad;
}
else
{
while (length > 1 && space[length-1] == 0)
--length;
if (space[--length] != 0x80)
throw InvalidCiphertext("StreamTransformationFilter: invalid ones-and-zeros padding found");
}
AttachedTransformation()->Put(space, length);
}
break;
default:
assert(false);
}
}
// *************************************************************
HashFilter::HashFilter(HashTransformation &hm, BufferedTransformation *attachment, bool putMessage, int truncatedDigestSize, const std::string &messagePutChannel, const std::string &hashPutChannel)
: m_hashModule(hm), m_putMessage(putMessage), m_digestSize(0), m_space(NULL)
, m_messagePutChannel(messagePutChannel), m_hashPutChannel(hashPutChannel)
{
m_digestSize = truncatedDigestSize < 0 ? m_hashModule.DigestSize() : truncatedDigestSize;
Detach(attachment);
}
void HashFilter::IsolatedInitialize(const NameValuePairs &parameters)
{
m_putMessage = parameters.GetValueWithDefault(Name::PutMessage(), false);
int s = parameters.GetIntValueWithDefault(Name::TruncatedDigestSize(), -1);
m_digestSize = s < 0 ? m_hashModule.DigestSize() : s;
}
size_t HashFilter::Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
{
FILTER_BEGIN;
if (m_putMessage)
FILTER_OUTPUT3(1, 0, inString, length, 0, m_messagePutChannel);
if (inString && length)
m_hashModule.Update(inString, length);
if (messageEnd)
{
{
size_t size;
m_space = HelpCreatePutSpace(*AttachedTransformation(), m_hashPutChannel, m_digestSize, m_digestSize, size = m_digestSize);
m_hashModule.TruncatedFinal(m_space, m_digestSize);
}
FILTER_OUTPUT3(2, 0, m_space, m_digestSize, messageEnd, m_hashPutChannel);
}
FILTER_END_NO_MESSAGE_END;
}
// *************************************************************
HashVerificationFilter::HashVerificationFilter(HashTransformation &hm, BufferedTransformation *attachment, word32 flags, int truncatedDigestSize)
: FilterWithBufferedInput(attachment)
, m_hashModule(hm), m_flags(0), m_digestSize(0), m_verified(false)
{
IsolatedInitialize(MakeParameters(Name::HashVerificationFilterFlags(), flags)(Name::TruncatedDigestSize(), truncatedDigestSize));
}
void HashVerificationFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
{
m_flags = parameters.GetValueWithDefault(Name::HashVerificationFilterFlags(), (word32)DEFAULT_FLAGS);
int s = parameters.GetIntValueWithDefault(Name::TruncatedDigestSize(), -1);
m_digestSize = s < 0 ? m_hashModule.DigestSize() : s;
m_verified = false;
firstSize = m_flags & HASH_AT_BEGIN ? m_digestSize : 0;
blockSize = 1;
lastSize = m_flags & HASH_AT_BEGIN ? 0 : m_digestSize;
}
void HashVerificationFilter::FirstPut(const byte *inString)
{
if (m_flags & HASH_AT_BEGIN)
{
m_expectedHash.New(m_digestSize);
if (inString) {memcpy(m_expectedHash, inString, m_expectedHash.size());}
if (m_flags & PUT_HASH)
AttachedTransformation()->Put(inString, m_expectedHash.size());
}
}
void HashVerificationFilter::NextPutMultiple(const byte *inString, size_t length)
{
m_hashModule.Update(inString, length);
if (m_flags & PUT_MESSAGE)
AttachedTransformation()->Put(inString, length);
}
void HashVerificationFilter::LastPut(const byte *inString, size_t length)
{
if (m_flags & HASH_AT_BEGIN)
{
assert(length == 0);
m_verified = m_hashModule.TruncatedVerify(m_expectedHash, m_digestSize);
}
else
{
m_verified = (length==m_digestSize && m_hashModule.TruncatedVerify(inString, length));
if (m_flags & PUT_HASH)
AttachedTransformation()->Put(inString, length);
}
if (m_flags & PUT_RESULT)
AttachedTransformation()->Put(m_verified);
if ((m_flags & THROW_EXCEPTION) && !m_verified)
throw HashVerificationFailed();
}
// *************************************************************
AuthenticatedEncryptionFilter::AuthenticatedEncryptionFilter(AuthenticatedSymmetricCipher &c, BufferedTransformation *attachment,
bool putAAD, int truncatedDigestSize, const std::string &macChannel, BlockPaddingScheme padding)
: StreamTransformationFilter(c, attachment, padding, true)
, m_hf(c, new OutputProxy(*this, false), putAAD, truncatedDigestSize, AAD_CHANNEL, macChannel)
{
assert(c.IsForwardTransformation());
}
void AuthenticatedEncryptionFilter::IsolatedInitialize(const NameValuePairs &parameters)
{
m_hf.IsolatedInitialize(parameters);
StreamTransformationFilter::IsolatedInitialize(parameters);
}
byte * AuthenticatedEncryptionFilter::ChannelCreatePutSpace(const std::string &channel, size_t &size)
{
if (channel.empty())
return StreamTransformationFilter::CreatePutSpace(size);
if (channel == AAD_CHANNEL)
return m_hf.CreatePutSpace(size);
throw InvalidChannelName("AuthenticatedEncryptionFilter", channel);
}
size_t AuthenticatedEncryptionFilter::ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
{
if (channel.empty())
return StreamTransformationFilter::Put2(begin, length, messageEnd, blocking);
if (channel == AAD_CHANNEL)
return m_hf.Put2(begin, length, 0, blocking);
throw InvalidChannelName("AuthenticatedEncryptionFilter", channel);
}
void AuthenticatedEncryptionFilter::LastPut(const byte *inString, size_t length)
{
StreamTransformationFilter::LastPut(inString, length);
m_hf.MessageEnd();
}
// *************************************************************
AuthenticatedDecryptionFilter::AuthenticatedDecryptionFilter(AuthenticatedSymmetricCipher &c, BufferedTransformation *attachment, word32 flags, int truncatedDigestSize, BlockPaddingScheme padding)
: FilterWithBufferedInput(attachment)
, m_hashVerifier(c, new OutputProxy(*this, false))
, m_streamFilter(c, new OutputProxy(*this, false), padding, true)
{
assert(!c.IsForwardTransformation() || c.IsSelfInverting());
IsolatedInitialize(MakeParameters(Name::BlockPaddingScheme(), padding)(Name::AuthenticatedDecryptionFilterFlags(), flags)(Name::TruncatedDigestSize(), truncatedDigestSize));
}
void AuthenticatedDecryptionFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
{
word32 flags = parameters.GetValueWithDefault(Name::AuthenticatedDecryptionFilterFlags(), (word32)DEFAULT_FLAGS);
m_hashVerifier.Initialize(CombinedNameValuePairs(parameters, MakeParameters(Name::HashVerificationFilterFlags(), flags)));
m_streamFilter.Initialize(parameters);
firstSize = m_hashVerifier.m_firstSize;
blockSize = 1;
lastSize = m_hashVerifier.m_lastSize;
}
byte * AuthenticatedDecryptionFilter::ChannelCreatePutSpace(const std::string &channel, size_t &size)
{
if (channel.empty())
return m_streamFilter.CreatePutSpace(size);
if (channel == AAD_CHANNEL)
return m_hashVerifier.CreatePutSpace(size);
throw InvalidChannelName("AuthenticatedDecryptionFilter", channel);
}
size_t AuthenticatedDecryptionFilter::ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
{
if (channel.empty())
{
if (m_lastSize > 0)
m_hashVerifier.ForceNextPut();
return FilterWithBufferedInput::Put2(begin, length, messageEnd, blocking);
}
if (channel == AAD_CHANNEL)
return m_hashVerifier.Put2(begin, length, 0, blocking);
throw InvalidChannelName("AuthenticatedDecryptionFilter", channel);
}
void AuthenticatedDecryptionFilter::FirstPut(const byte *inString)
{
m_hashVerifier.Put(inString, m_firstSize);
}
void AuthenticatedDecryptionFilter::NextPutMultiple(const byte *inString, size_t length)
{
m_streamFilter.Put(inString, length);
}
void AuthenticatedDecryptionFilter::LastPut(const byte *inString, size_t length)
{
m_streamFilter.MessageEnd();
m_hashVerifier.PutMessageEnd(inString, length);
}
// *************************************************************
void SignerFilter::IsolatedInitialize(const NameValuePairs &parameters)
{
m_putMessage = parameters.GetValueWithDefault(Name::PutMessage(), false);
m_messageAccumulator.reset(m_signer.NewSignatureAccumulator(m_rng));
}
size_t SignerFilter::Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
{
FILTER_BEGIN;
m_messageAccumulator->Update(inString, length);
if (m_putMessage)
FILTER_OUTPUT(1, inString, length, 0);
if (messageEnd)
{
m_buf.New(m_signer.SignatureLength());
m_signer.Sign(m_rng, m_messageAccumulator.release(), m_buf);
FILTER_OUTPUT(2, m_buf, m_buf.size(), messageEnd);
m_messageAccumulator.reset(m_signer.NewSignatureAccumulator(m_rng));
}
FILTER_END_NO_MESSAGE_END;
}
SignatureVerificationFilter::SignatureVerificationFilter(const PK_Verifier &verifier, BufferedTransformation *attachment, word32 flags)
: FilterWithBufferedInput(attachment)
, m_verifier(verifier), m_flags(0), m_verified(0)
{
IsolatedInitialize(MakeParameters(Name::SignatureVerificationFilterFlags(), flags));
}
void SignatureVerificationFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
{
m_flags = parameters.GetValueWithDefault(Name::SignatureVerificationFilterFlags(), (word32)DEFAULT_FLAGS);
m_messageAccumulator.reset(m_verifier.NewVerificationAccumulator());
size_t size = m_verifier.SignatureLength();
assert(size != 0); // TODO: handle recoverable signature scheme
m_verified = false;
firstSize = m_flags & SIGNATURE_AT_BEGIN ? size : 0;
blockSize = 1;
lastSize = m_flags & SIGNATURE_AT_BEGIN ? 0 : size;
}
void SignatureVerificationFilter::FirstPut(const byte *inString)
{
if (m_flags & SIGNATURE_AT_BEGIN)
{
if (m_verifier.SignatureUpfront())
m_verifier.InputSignature(*m_messageAccumulator, inString, m_verifier.SignatureLength());
else
{
m_signature.New(m_verifier.SignatureLength());
if (inString) {memcpy(m_signature, inString, m_signature.size());}
}
if (m_flags & PUT_SIGNATURE)
AttachedTransformation()->Put(inString, m_signature.size());
}
else
{
assert(!m_verifier.SignatureUpfront());
}
}
void SignatureVerificationFilter::NextPutMultiple(const byte *inString, size_t length)
{
m_messageAccumulator->Update(inString, length);
if (m_flags & PUT_MESSAGE)
AttachedTransformation()->Put(inString, length);
}
void SignatureVerificationFilter::LastPut(const byte *inString, size_t length)
{
if (m_flags & SIGNATURE_AT_BEGIN)
{
assert(length == 0);
m_verifier.InputSignature(*m_messageAccumulator, m_signature, m_signature.size());
m_verified = m_verifier.VerifyAndRestart(*m_messageAccumulator);
}
else
{
m_verifier.InputSignature(*m_messageAccumulator, inString, length);
m_verified = m_verifier.VerifyAndRestart(*m_messageAccumulator);
if (m_flags & PUT_SIGNATURE)
AttachedTransformation()->Put(inString, length);
}
if (m_flags & PUT_RESULT)
AttachedTransformation()->Put(m_verified);
if ((m_flags & THROW_EXCEPTION) && !m_verified)
throw SignatureVerificationFailed();
}
// *************************************************************
size_t Source::PumpAll2(bool blocking)
{
unsigned int messageCount = UINT_MAX;
do {
RETURN_IF_NONZERO(PumpMessages2(messageCount, blocking));
} while(messageCount == UINT_MAX);
return 0;
}
bool Store::GetNextMessage()
{
if (!m_messageEnd && !AnyRetrievable())
{
m_messageEnd=true;
return true;
}
else
return false;
}
unsigned int Store::CopyMessagesTo(BufferedTransformation &target, unsigned int count, const std::string &channel) const
{
if (m_messageEnd || count == 0)
return 0;
else
{
CopyTo(target, ULONG_MAX, channel);
if (GetAutoSignalPropagation())
target.ChannelMessageEnd(channel, GetAutoSignalPropagation()-1);
return 1;
}
}
void StringStore::StoreInitialize(const NameValuePairs &parameters)
{
ConstByteArrayParameter array;
if (!parameters.GetValue(Name::InputBuffer(), array))
throw InvalidArgument("StringStore: missing InputBuffer argument");
m_store = array.begin();
m_length = array.size();
m_count = 0;
}
size_t StringStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
{
lword position = 0;
size_t blockedBytes = CopyRangeTo2(target, position, transferBytes, channel, blocking);
m_count += (size_t)position;
transferBytes = position;
return blockedBytes;
}
size_t StringStore::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
{
size_t i = UnsignedMin(m_length, m_count+begin);
size_t len = UnsignedMin(m_length-i, end-begin);
size_t blockedBytes = target.ChannelPut2(channel, m_store+i, len, 0, blocking);
if (!blockedBytes)
begin += len;
return blockedBytes;
}
void RandomNumberStore::StoreInitialize(const NameValuePairs &parameters)
{
parameters.GetRequiredParameter("RandomNumberStore", "RandomNumberGeneratorPointer", m_rng);
int length;
parameters.GetRequiredIntParameter("RandomNumberStore", "RandomNumberStoreSize", length);
m_length = length;
}
size_t RandomNumberStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
{
if (!blocking)
throw NotImplemented("RandomNumberStore: nonblocking transfer is not implemented by this object");
transferBytes = UnsignedMin(transferBytes, m_length - m_count);
m_rng->GenerateIntoBufferedTransformation(target, channel, transferBytes);
m_count += transferBytes;
return 0;
}
size_t NullStore::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
{
static const byte nullBytes[128] = {0};
while (begin < end)
{
size_t len = (size_t)STDMIN(end-begin, lword(128));
size_t blockedBytes = target.ChannelPut2(channel, nullBytes, len, 0, blocking);
if (blockedBytes)
return blockedBytes;
begin += len;
}
return 0;
}
size_t NullStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
{
lword begin = 0;
size_t blockedBytes = NullStore::CopyRangeTo2(target, begin, transferBytes, channel, blocking);
transferBytes = begin;
m_size -= begin;
return blockedBytes;
}
NAMESPACE_END
#endif
BlockPaddingSchemeDef::PKCS_PADDING
@ PKCS_PADDING
PKCS #5 padding added to a block.
Definition: filters.h:465
BlockPaddingSchemeDef::ZEROS_PADDING
@ ZEROS_PADDING
0's padding added to a block
Definition: filters.h:463
RandomNumberGenerator::GenerateIntoBufferedTransformation
virtual void GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword length)
Generate random bytes into a BufferedTransformation.
Definition: cryptlib.cpp:348
HashVerificationFilter::PUT_HASH
@ PUT_HASH
Indicates the hash should be passed to an attached transformation.
Definition: filters.h:558
FilterWithBufferedInput::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: filters.cpp:336
ConstByteArrayParameter::size
size_t size() const
Length of the memory block.
Definition: algparam.h:93
Source::PumpMessages2
virtual size_t PumpMessages2(unsigned int &messageCount, bool blocking=true)=0
Pump messages to attached transformation.
xorbuf
void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
Definition: misc.cpp:28
HashVerificationFilter::HashVerificationFilter
HashVerificationFilter(HashTransformation &hm, BufferedTransformation *attachment=NULL, word32 flags=DEFAULT_FLAGS, int truncatedDigestSize=-1)
Construct a HashVerificationFilter.
Definition: filters.cpp:799
SignatureVerificationFilter::PUT_RESULT
@ PUT_RESULT
Indicates the result of the verification should be passed to an attached transformation.
Definition: filters.h:718
NameValuePairs::GetValueWithDefault
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
Definition: cryptlib.h:348
MakeParameters
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:554
BufferedTransformation::PutModifiable
size_t PutModifiable(byte *inString, size_t length, bool blocking=true)
Input multiple bytes that may be modified by callee.
Definition: cryptlib.h:1428
MeterFilter::ResetMeter
void ResetMeter()
Resets the meter.
Definition: filters.cpp:160
Algorithm::AlgorithmName
virtual std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: cryptlib.h:518
member_ptr
Pointer that overloads operator ->
Definition: smartptr.h:39
NotImplemented
A method was called which was not implemented.
Definition: cryptlib.h:203
SignerFilter::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: filters.cpp:967
BufferedTransformation::AnyRetrievable
virtual bool AnyRetrievable() const
Determines whether bytes are ready for retrieval.
Definition: cryptlib.cpp:508
BlockPaddingSchemeDef::BlockPaddingScheme
BlockPaddingScheme
Padding schemes used for block ciphers.
Definition: filters.h:459
MeterFilter::IsolatedMessageSeriesEnd
bool IsolatedMessageSeriesEnd(bool blocking)
Marks the end of a series of messages, without signal propagation.
Definition: filters.cpp:242
AuthenticatedEncryptionFilter::ChannelCreatePutSpace
byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
Request space which can be written into by the caller.
Definition: filters.cpp:872
ByteArrayParameter::begin
byte * begin() const
Pointer to the first byte in the memory block.
Definition: algparam.h:119
StreamTransformation::MandatoryBlockSize
virtual unsigned int MandatoryBlockSize() const
Provides the mandatory block size of the cipher.
Definition: cryptlib.h:830
BufferedTransformation::Attach
virtual void Attach(BufferedTransformation *newAttachment)
Add newAttachment to the end of attachment chain.
Definition: cryptlib.cpp:764
HashTransformation::TruncatedFinal
virtual void TruncatedFinal(byte *digest, size_t digestSize)=0
Computes the hash of the current message.
BufferedTransformation::ChannelMessageEnd
bool ChannelMessageEnd(const std::string &channel, int propagation=-1, bool blocking=true)
Signal the end of a message.
Definition: cryptlib.h:1914
BufferedTransformation::IsolatedInitialize
virtual void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: cryptlib.h:1505
RandomNumberSink::Put2
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: filters.cpp:534
NameValuePairs::GetIntValueWithDefault
int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
Definition: cryptlib.h:380
SignatureVerificationFilter::PUT_MESSAGE
@ PUT_MESSAGE
Indicates the message should be passed to an attached transformation.
Definition: filters.h:714
OutputProxy
Definition: filters.h:846
FilterWithBufferedInput::IsolatedFlush
bool IsolatedFlush(bool hardFlush, bool blocking)
Definition: filters.cpp:345
Source::PumpAll2
virtual size_t PumpAll2(bool blocking=true)
Pump all data to attached transformation.
Definition: filters.cpp:1061
Filter::TransferTo2
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
Transfer bytes from this object to another BufferedTransformation.
Definition: filters.cpp:66
HashTransformation
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:922
HashFilter::Put2
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: filters.cpp:778
StreamTransformationFilter::StreamTransformationFilter
StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment=NULL, BlockPaddingScheme padding=DEFAULT_PADDING, bool allowAuthenticatedSymmetricCipher=false)
Construct a StreamTransformationFilter.
Definition: filters.cpp:589
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1352
ProxyFilter::SetFilter
void SetFilter(Filter *filter)
Sets the OutputProxy filter.
Definition: filters.cpp:503
PK_SignatureScheme::SignatureLength
virtual size_t SignatureLength() const =0
Provides the signature length if it only depends on the key.
Filter::CopyRangeTo2
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
Copy bytes from this object to another BufferedTransformation.
Definition: filters.cpp:61
SecByteBlock
SecBlock<byte> typedef.
Definition: secblock.h:731
ByteArrayParameter
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:104
BufferedTransformation::PutMessageEnd
size_t PutMessageEnd(const byte *inString, size_t length, int propagation=-1, bool blocking=true)
Input multiple bytes for processing and signal the end of a message.
Definition: cryptlib.h:1449
BufferedTransformation::BlockingInputOnly
Exception thrown by objects that have not implemented nonblocking input processing.
Definition: cryptlib.h:1472
ProxyFilter::ProxyFilter
ProxyFilter(BufferedTransformation *filter, size_t firstSize, size_t lastSize, BufferedTransformation *attachment)
Construct a ProxyFilter.
Definition: filters.cpp:491
StreamTransformation::ProcessLastBlock
virtual void ProcessLastBlock(byte *outString, const byte *inString, size_t length)
Encrypt or decrypt the last block of data.
Definition: cryptlib.cpp:248
UnsignedMin
const T1 UnsignedMin(const T1 &a, const T2 &b)
Safe comparison of values that could be neagtive and incorrectly promoted.
Definition: misc.h:503
InvalidDataFormat
Input data was received that did not conform to expected format.
Definition: cryptlib.h:189
smartptr.h
Classes for automatic resource management.
RandomNumberStore::TransferTo2
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
Transfer bytes from this object to another BufferedTransformation.
Definition: filters.cpp:1132
ArraySink::CreatePutSpace
byte * CreatePutSpace(size_t &size)
Request space which can be written into by the caller.
Definition: filters.cpp:557
AuthenticatedEncryptionFilter::AuthenticatedEncryptionFilter
AuthenticatedEncryptionFilter(AuthenticatedSymmetricCipher &c, BufferedTransformation *attachment=NULL, bool putAAD=false, int truncatedDigestSize=-1, const std::string &macChannel=DEFAULT_CHANNEL, BlockPaddingScheme padding=DEFAULT_PADDING)
Construct a AuthenticatedEncryptionFilter.
Definition: filters.cpp:858
BufferedTransformation::InvalidChannelName
Exception thrown when a filter does not recognize a named channel.
Definition: cryptlib.h:1856
SIZE_MAX
#define SIZE_MAX
The maximum value of a machine word.
Definition: misc.h:95
BufferedTransformation::Put
size_t Put(byte inByte, bool blocking=true)
Input a byte for processing.
Definition: cryptlib.h:1378
BufferedTransformation::TransferAllTo
void TransferAllTo(BufferedTransformation &target, const std::string &channel=DEFAULT_CHANNEL)
Transfer all bytes from this object to another BufferedTransformation.
Definition: cryptlib.h:1764
ArraySink::Put2
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: filters.cpp:541
DEFAULT_CHANNEL
const std::string DEFAULT_CHANNEL
Default channel for BufferedTransformation.
Definition: cryptlib.cpp:63
NullStore::CopyRangeTo2
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
Copy bytes from this object to another BufferedTransformation.
Definition: filters.cpp:1144
StringStore::TransferTo2
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
Transfer bytes from this object to another BufferedTransformation.
Definition: filters.cpp:1105
BufferedTransformation::ChannelPutModifiable2
virtual size_t ChannelPutModifiable2(const std::string &channel, byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes that may be modified by callee on a channel.
Definition: cryptlib.cpp:476
FilterWithBufferedInput::Put2
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: filters.h:337
SignatureVerificationFilter::PUT_SIGNATURE
@ PUT_SIGNATURE
Indicates the signature should be passed to an attached transformation.
Definition: filters.h:716
BlockPaddingSchemeDef::DEFAULT_PADDING
@ DEFAULT_PADDING
Default padding scheme.
Definition: filters.h:469
SignatureVerificationFilter::DEFAULT_FLAGS
@ DEFAULT_FLAGS
Default flags using SIGNATURE_AT_BEGIN and PUT_RESULT.
Definition: filters.h:722
AAD_CHANNEL
const std::string AAD_CHANNEL
Channel for additional authenticated data.
Definition: cryptlib.cpp:64
filters.h
Implementation of BufferedTransformation's attachment interface.
SignerFilter::Put2
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: filters.cpp:973
argnames.h
Standard names for retrieving values by name when working with NameValuePairs.
BufferedTransformation::CopyRangeTo2
virtual size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const =0
Copy bytes from this object to another BufferedTransformation.
AuthenticatedDecryptionFilter::ChannelCreatePutSpace
byte * ChannelCreatePutSpace(const std::string &channel, size_t &size)
Request space which can be written into by the caller.
Definition: filters.cpp:923
BufferedTransformation::ChannelPut2
virtual size_t ChannelPut2(const std::string &channel, const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing on a channel.
Definition: cryptlib.cpp:468
misc.h
Utility functions for the Crypto++ library.
NullStore::TransferTo2
size_t TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)
Transfer bytes from this object to another BufferedTransformation.
Definition: filters.cpp:1158
ArraySink::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: filters.cpp:563
Filter::Flush
bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition: filters.cpp:78
Store::GetNextMessage
bool GetNextMessage()
Start retrieving the next message.
Definition: filters.cpp:1071
Filter
Implementation of BufferedTransformation's attachment interface.
Definition: filters.h:36
BufferedTransformation::CopyTo
lword CopyTo(BufferedTransformation &target, lword copyMax=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL) const
copy copyMax bytes of the buffered output to target as input
Definition: cryptlib.h:1680
STDMIN
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition: misc.h:467
Filter::MessageSeriesEnd
bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
Marks the end of a series of messages, with signal propagation.
Definition: filters.cpp:95
HashFilter::CreatePutSpace
byte * CreatePutSpace(size_t &size)
Request space which can be written into by the caller.
Definition: filters.h:523
FilterPutSpaceHelper::HelpCreatePutSpace
byte * HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, size_t minSize, size_t desiredSize, size_t &bufferSize)
Create a working space in a BufferedTransformation.
Definition: filters.h:177
NameValuePairs::GetRequiredParameter
void GetRequiredParameter(const char *className, const char *name, T &value) const
Retrieves a required name/value pair.
Definition: cryptlib.h:406
Filter::Initialize
void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1)
Initialize or reinitialize this object, with signal propagation.
Definition: filters.cpp:71
STDMAX
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
Definition: misc.h:477
HashVerificationFilter::DEFAULT_FLAGS
@ DEFAULT_FLAGS
Default flags using HASH_AT_BEGIN and PUT_RESULT.
Definition: filters.h:564
AuthenticatedDecryptionFilter::DEFAULT_FLAGS
@ DEFAULT_FLAGS
Default flags using THROW_EXCEPTION.
Definition: filters.h:638
PK_Signer::NewSignatureAccumulator
virtual PK_MessageAccumulator * NewSignatureAccumulator(RandomNumberGenerator &rng) const =0
Create a new HashTransformation to accumulate the message to be signed.
ArrayXorSink::Put2
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: filters.cpp:572
MeterFilter::AddRangeToSkip
void AddRangeToSkip(unsigned int message, lword position, lword size, bool sortNow=true)
Adds a range to skip during processing.
Definition: filters.cpp:166
StreamTransformation::OptimalBlockSize
virtual unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this cipher.
Definition: cryptlib.h:837
HashVerificationFilter::HASH_AT_BEGIN
@ HASH_AT_BEGIN
Indicates the hash is at the beginning of the message (i.e., concatenation of hash+message)
Definition: filters.h:554
Filter::Detach
void Detach(BufferedTransformation *newAttachment=NULL)
Replace an attached transformation.
Definition: filters.cpp:50
HashVerificationFilter::THROW_EXCEPTION
@ THROW_EXCEPTION
Indicates the filter should throw a HashVerificationFailed if a failure is encountered.
Definition: filters.h:562
AuthenticatedEncryptionFilter::ChannelPut2
size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing on a channel.
Definition: filters.cpp:883
StreamTransformation::ProcessString
void ProcessString(byte *inoutString, size_t length)
Encrypt or decrypt a string of bytes.
Definition: cryptlib.h:869
StreamTransformation::MinLastBlockSize
virtual unsigned int MinLastBlockSize() const
returns the minimum size of the last block, 0 indicating the last block is not special
Definition: cryptlib.h:863
SecBlock::New
void New(size_type newSize)
Change size without preserving contents.
Definition: secblock.h:647
BufferedTransformation::Initialize
virtual void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1)
Initialize or reinitialize this object, with signal propagation.
Definition: cryptlib.cpp:439
StreamTransformationFilter
Filter wrapper for StreamTransformation.
Definition: filters.h:476
FilterWithBufferedInput::FilterWithBufferedInput
FilterWithBufferedInput(BufferedTransformation *attachment)
Construct a FilterWithBufferedInput with an attached transformation.
Definition: filters.cpp:322
MeterFilter::Put2
size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing.
Definition: filters.cpp:232
PK_Verifier::NewVerificationAccumulator
virtual PK_MessageAccumulator * NewVerificationAccumulator() const =0
Create a new HashTransformation to accumulate the message to be verified.
FilterWithBufferedInput
Divides an input stream into discrete blocks.
Definition: filters.h:315
BlockPaddingSchemeDef::NO_PADDING
@ NO_PADDING
No padding added to a block.
Definition: filters.h:461
SecBlock::size
size_type size() const
Provides the count of elements in the SecBlock.
Definition: secblock.h:524
StreamTransformation::IsForwardTransformation
virtual bool IsForwardTransformation() const =0
Determines if the cipher is being operated in its forward direction.
BufferedTransformation::TransferTo2
virtual size_t TransferTo2(BufferedTransformation &target, lword &byteCount, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true)=0
Transfer bytes from this object to another BufferedTransformation.
SignatureVerificationFilter::THROW_EXCEPTION
@ THROW_EXCEPTION
Indicates the filter should throw a HashVerificationFailed if a failure is encountered.
Definition: filters.h:720
HashTransformation::DigestSize
virtual unsigned int DigestSize() const =0
Provides the digest size of the hash.
InvalidArgument
An invalid argument was detected.
Definition: cryptlib.h:182
BufferedTransformation::Flush
virtual bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output, with signal propagation.
Definition: cryptlib.cpp:446
BufferedTransformation::ChannelMessageSeriesEnd
virtual bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true)
Marks the end of a series of messages on a channel.
Definition: cryptlib.cpp:492
RandomNumberGenerator::IncorporateEntropy
virtual void IncorporateEntropy(const byte *input, size_t length)
Update RNG state with additional unpredictable values.
Definition: cryptlib.h:1201
StreamTransformation::GetOptimalBlockSizeUsed
virtual unsigned int GetOptimalBlockSizeUsed() const
Provides the number of bytes used in the current block when processing at optimal block size.
Definition: cryptlib.h:841
SignatureVerificationFilter::SignatureVerificationFilter
SignatureVerificationFilter(const PK_Verifier &verifier, BufferedTransformation *attachment=NULL, word32 flags=DEFAULT_FLAGS)
Construct a SignatureVerificationFilter.
Definition: filters.cpp:989
ConstByteArrayParameter::begin
const byte * begin() const
Pointer to the first byte in the memory block.
Definition: algparam.h:89
FilterWithBufferedInput::ForceNextPut
void ForceNextPut()
Definition: filters.cpp:448
BufferedTransformation::IsolatedFlush
virtual bool IsolatedFlush(bool hardFlush, bool blocking)=0
Flushes data buffered by this object, without signal propagation.
PK_Verifier
Interface for public-key signature verifiers.
Definition: cryptlib.h:2608
MessageQueue
Message Queue.
Definition: mqueue.h:14
Filter::AttachedTransformation
BufferedTransformation * AttachedTransformation()
Retrieve attached transformation.
Definition: filters.cpp:36
HashFilter::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: filters.cpp:771
HashVerificationFilter::PUT_RESULT
@ PUT_RESULT
Indicates the result of the verification should be passed to an attached transformation.
Definition: filters.h:560
CryptoPP
Crypto++ library namespace.
RandomNumberSink::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: filters.cpp:529
Redirector::Initialize
void Initialize(const NameValuePairs &parameters, int propagation)
Initialize or reinitialize this object, with signal propagation.
Definition: filters.cpp:480
NameValuePairs::GetRequiredIntParameter
void GetRequiredIntParameter(const char *className, const char *name, int &value) const
Retrieves a required name/value pair.
Definition: cryptlib.h:421
NameValuePairs::GetValue
bool GetValue(const char *name, T &value) const
Get a named value.
Definition: cryptlib.h:335
RoundDownToMultipleOf
T1 RoundDownToMultipleOf(const T1 &n, const T2 &m)
Rounds a value down to a multiple of a second value.
Definition: misc.h:849
StreamTransformation::ProcessData
virtual void ProcessData(byte *outString, const byte *inString, size_t length)=0
Encrypt or decrypt an array of bytes.
config.h
Library configuration file.
PK_Verifier::VerifyAndRestart
virtual bool VerifyAndRestart(PK_MessageAccumulator &messageAccumulator) const =0
Check whether messageAccumulator contains a valid signature and message, and restart messageAccumulat...
CombinedNameValuePairs
Combines two sets of NameValuePairs.
Definition: algparam.h:135
HashFilter::HashFilter
HashFilter(HashTransformation &hm, BufferedTransformation *attachment=NULL, bool putMessage=false, int truncatedDigestSize=-1, const std::string &messagePutChannel=DEFAULT_CHANNEL, const std::string &hashPutChannel=DEFAULT_CHANNEL)
Construct a HashFilter.
Definition: filters.cpp:763
SaturatingSubtract
T1 SaturatingSubtract(const T1 &a, const T2 &b)
Performs a saturating subtract clamped at 0.
Definition: misc.h:961
MeterFilter::PutModifiable2
size_t PutModifiable2(byte *inString, size_t length, int messageEnd, bool blocking)
Input multiple bytes that may be modified by callee.
Definition: filters.cpp:237
BufferedTransformation::MessageEnd
bool MessageEnd(int propagation=-1, bool blocking=true)
Signals the end of messages to the object.
Definition: cryptlib.h:1436
AuthenticatedDecryptionFilter::ChannelPut2
size_t ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking)
Input multiple bytes for processing on a channel.
Definition: filters.cpp:934
HashVerificationFilter::PUT_MESSAGE
@ PUT_MESSAGE
Indicates the message should be passed to an attached transformation.
Definition: filters.h:556
StreamTransformation
Interface for the data processing portion of stream ciphers.
Definition: cryptlib.h:816
PK_Verifier::InputSignature
virtual void InputSignature(PK_MessageAccumulator &messageAccumulator, const byte *signature, size_t signatureLength) const =0
Input signature into a message accumulator.
AuthenticatedDecryptionFilter::AuthenticatedDecryptionFilter
AuthenticatedDecryptionFilter(AuthenticatedSymmetricCipher &c, BufferedTransformation *attachment=NULL, word32 flags=DEFAULT_FLAGS, int truncatedDigestSize=-1, BlockPaddingScheme padding=DEFAULT_PADDING)
Construct a AuthenticatedDecryptionFilter.
Definition: filters.cpp:902
HashTransformation::TruncatedVerify
virtual bool TruncatedVerify(const byte *digest, size_t digestLength)
Verifies the hash of the current message.
Definition: cryptlib.cpp:412
BufferedTransformation::CreatePutSpace
virtual byte * CreatePutSpace(size_t &size)
Request space which can be written into by the caller.
Definition: cryptlib.h:1413
BlockPaddingSchemeDef::ONE_AND_ZEROS_PADDING
@ ONE_AND_ZEROS_PADDING
1 and 0's padding added to a block
Definition: filters.h:467
BufferedTransformation::ChannelFlush
virtual bool ChannelFlush(const std::string &channel, bool hardFlush, int propagation=-1, bool blocking=true)
Flush buffered input and/or output on a channel.
Definition: cryptlib.cpp:484
BufferedTransformation::IsolatedMessageSeriesEnd
virtual bool IsolatedMessageSeriesEnd(bool blocking)
Marks the end of a series of messages, without signal propagation.
Definition: cryptlib.h:1519
AuthenticatedEncryptionFilter::IsolatedInitialize
void IsolatedInitialize(const NameValuePairs &parameters)
Initialize or reinitialize this object, without signal propagation.
Definition: filters.cpp:866
PK_SignatureScheme::SignatureUpfront
virtual bool SignatureUpfront() const
Determines whether the signature must be input before the message.
Definition: cryptlib.h:2511
ProxyFilter::IsolatedFlush
bool IsolatedFlush(bool hardFlush, bool blocking)
Definition: filters.cpp:498
StringStore::CopyRangeTo2
size_t CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end=LWORD_MAX, const std::string &channel=DEFAULT_CHANNEL, bool blocking=true) const
Copy bytes from this object to another BufferedTransformation.
Definition: filters.cpp:1114
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:277
ConstByteArrayParameter
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:29
Redirector::PASS_EVERYTHING
@ PASS_EVERYTHING
Pass everything.
Definition: filters.h:770
SignatureVerificationFilter::SIGNATURE_AT_BEGIN
@ SIGNATURE_AT_BEGIN
Indicates the signature is at the beginning of the message (i.e., concatenation of signature+message)
Definition: filters.h:712
PK_Signer::Sign
virtual size_t Sign(RandomNumberGenerator &rng, PK_MessageAccumulator *messageAccumulator, byte *signature) const
Sign and delete the messageAccumulator.
Definition: cryptlib.cpp:870
HashTransformation::Update
virtual void Update(const byte *input, size_t length)=0
Updates a hash with additional input.
AuthenticatedSymmetricCipher
Interface for one direction (encryption or decryption) of a stream cipher or block cipher mode with a...
Definition: cryptlib.h:1117
ByteArrayParameter::size
size_t size() const
Length of the memory block.
Definition: algparam.h:123
InvalidCiphertext
A decryption filter encountered invalid ciphertext.
Definition: cryptlib.h:196