ext-cryptopp/filters.cpp

987 lines
28 KiB
C++
Raw Normal View History

2003-02-24 01:11:57 +00:00
// filters.cpp - written and placed in the public domain by Wei Dai
2002-10-04 17:31:41 +00:00
#include "pch.h"
#ifndef CRYPTOPP_IMPORTS
2002-10-04 17:31:41 +00:00
#include "filters.h"
#include "mqueue.h"
#include "fltrimpl.h"
#include "argnames.h"
#include <memory>
#include <functional>
NAMESPACE_BEGIN(CryptoPP)
Filter::Filter(BufferedTransformation *attachment)
2003-02-24 01:11:57 +00:00
: m_attachment(attachment), m_continueAt(0)
2002-10-04 17:31:41 +00:00
{
}
2003-02-24 01:11:57 +00:00
BufferedTransformation * Filter::NewDefaultAttachment() const
2002-10-04 17:31:41 +00:00
{
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);
}
2005-07-12 04:23:32 +00:00
size_t Filter::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
2002-10-04 17:31:41 +00:00
{
return AttachedTransformation()->CopyRangeTo2(target, begin, end, channel, blocking);
}
2005-07-12 04:23:32 +00:00
size_t Filter::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
return AttachedTransformation()->TransferTo2(target, transferBytes, channel, blocking);
2002-10-04 17:31:41 +00:00
}
void Filter::Initialize(const NameValuePairs &parameters, int propagation)
{
m_continueAt = 0;
IsolatedInitialize(parameters);
2003-02-24 01:11:57 +00:00
PropagateInitialize(parameters, propagation);
2002-10-04 17:31:41 +00:00
}
2003-02-24 01:11:57 +00:00
bool Filter::Flush(bool hardFlush, int propagation, bool blocking)
2002-10-04 17:31:41 +00:00
{
switch (m_continueAt)
{
case 0:
if (IsolatedFlush(hardFlush, blocking))
return true;
case 1:
if (OutputFlush(1, hardFlush, propagation, blocking))
return true;
}
return false;
}
2003-02-24 01:11:57 +00:00
bool Filter::MessageSeriesEnd(int propagation, bool blocking)
2002-10-04 17:31:41 +00:00
{
switch (m_continueAt)
{
case 0:
if (IsolatedMessageSeriesEnd(blocking))
return true;
case 1:
2003-02-24 01:11:57 +00:00
if (ShouldPropagateMessageSeriesEnd() && OutputMessageSeriesEnd(1, propagation, blocking))
2002-10-04 17:31:41 +00:00
return true;
}
return false;
}
void Filter::PropagateInitialize(const NameValuePairs &parameters, int propagation)
2002-10-04 17:31:41 +00:00
{
if (propagation)
AttachedTransformation()->Initialize(parameters, propagation-1);
2002-10-04 17:31:41 +00:00
}
2005-07-12 04:23:32 +00:00
size_t Filter::OutputModifiable(int outputSite, byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel)
2003-06-06 02:34:03 +00:00
{
if (messageEnd)
messageEnd--;
2005-07-12 04:23:32 +00:00
size_t result = AttachedTransformation()->PutModifiable2(inString, length, messageEnd, blocking);
2003-06-06 02:34:03 +00:00
m_continueAt = result ? outputSite : 0;
return result;
}
2005-07-12 04:23:32 +00:00
size_t Filter::Output(int outputSite, const byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel)
2002-10-04 17:31:41 +00:00
{
if (messageEnd)
messageEnd--;
2005-07-12 04:23:32 +00:00
size_t result = AttachedTransformation()->Put2(inString, length, messageEnd, blocking);
2003-02-24 01:11:57 +00:00
m_continueAt = result ? outputSite : 0;
2002-10-04 17:31:41 +00:00
return result;
}
bool Filter::OutputFlush(int outputSite, bool hardFlush, int propagation, bool blocking, const std::string &channel)
{
2003-02-24 01:11:57 +00:00
if (propagation && AttachedTransformation()->ChannelFlush(channel, hardFlush, propagation-1, blocking))
2002-10-04 17:31:41 +00:00
{
m_continueAt = outputSite;
return true;
}
m_continueAt = 0;
return false;
}
2003-02-24 01:11:57 +00:00
bool Filter::OutputMessageSeriesEnd(int outputSite, int propagation, bool blocking, const std::string &channel)
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
if (propagation && AttachedTransformation()->ChannelMessageSeriesEnd(channel, propagation-1, blocking))
2002-10-04 17:31:41 +00:00
{
m_continueAt = outputSite;
return true;
}
m_continueAt = 0;
return false;
}
// *************************************************************
void MeterFilter::ResetMeter()
2002-10-04 17:31:41 +00:00
{
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());
2002-10-04 17:31:41 +00:00
}
size_t MeterFilter::PutMaybeModifiable(byte *begin, size_t length, int messageEnd, bool blocking, bool modifiable)
2003-06-06 02:34:03 +00:00
{
if (!m_transparent)
return 0;
size_t t;
FILTER_BEGIN;
m_begin = begin;
m_length = length;
while (m_length > 0 || messageEnd)
2003-06-06 02:34:03 +00:00
{
2006-08-10 03:11:05 +00:00
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;
2003-06-06 02:34:03 +00:00
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
2003-06-06 02:34:03 +00:00
{
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;
}
2003-06-06 02:34:03 +00:00
}
}
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);
2003-06-06 02:34:03 +00:00
}
2003-02-24 01:11:57 +00:00
bool MeterFilter::IsolatedMessageSeriesEnd(bool blocking)
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
m_currentMessageBytes = 0;
m_currentSeriesMessages = 0;
2002-10-04 17:31:41 +00:00
m_totalMessageSeries++;
return false;
}
// *************************************************************
2005-07-12 04:23:32 +00:00
void FilterWithBufferedInput::BlockQueue::ResetQueue(size_t blockSize, size_t maxBlocks)
2002-10-04 17:31:41 +00:00
{
m_buffer.New(blockSize * maxBlocks);
2003-02-24 01:11:57 +00:00
m_blockSize = blockSize;
m_maxBlocks = maxBlocks;
2002-10-04 17:31:41 +00:00
m_size = 0;
2003-02-24 01:11:57 +00:00
m_begin = m_buffer;
2002-10-04 17:31:41 +00:00
}
byte *FilterWithBufferedInput::BlockQueue::GetBlock()
{
if (m_size >= m_blockSize)
{
2003-02-24 01:11:57 +00:00
byte *ptr = m_begin;
2002-10-04 17:31:41 +00:00
if ((m_begin+=m_blockSize) == m_buffer.end())
2003-02-24 01:11:57 +00:00
m_begin = m_buffer;
2002-10-04 17:31:41 +00:00
m_size -= m_blockSize;
return ptr;
}
else
return NULL;
}
2005-07-12 04:23:32 +00:00
byte *FilterWithBufferedInput::BlockQueue::GetContigousBlocks(size_t &numberOfBytes)
2002-10-04 17:31:41 +00:00
{
2005-07-12 04:23:32 +00:00
numberOfBytes = STDMIN(numberOfBytes, STDMIN(size_t(m_buffer.end()-m_begin), m_size));
2003-02-24 01:11:57 +00:00
byte *ptr = m_begin;
m_begin += numberOfBytes;
2002-10-04 17:31:41 +00:00
m_size -= numberOfBytes;
2003-02-24 01:11:57 +00:00
if (m_size == 0 || m_begin == m_buffer.end())
m_begin = m_buffer;
2002-10-04 17:31:41 +00:00
return ptr;
}
2005-07-12 04:23:32 +00:00
size_t FilterWithBufferedInput::BlockQueue::GetAll(byte *outString)
2002-10-04 17:31:41 +00:00
{
2005-07-12 04:23:32 +00:00
size_t size = m_size;
size_t numberOfBytes = m_maxBlocks*m_blockSize;
2003-02-24 01:11:57 +00:00
const byte *ptr = GetContigousBlocks(numberOfBytes);
2002-10-04 17:31:41 +00:00
memcpy(outString, ptr, numberOfBytes);
2003-02-24 01:11:57 +00:00
memcpy(outString+numberOfBytes, m_begin, m_size);
2002-10-04 17:31:41 +00:00
m_size = 0;
return size;
}
2005-07-12 04:23:32 +00:00
void FilterWithBufferedInput::BlockQueue::Put(const byte *inString, size_t length)
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
assert(m_size + length <= m_buffer.size());
2005-07-12 04:23:32 +00:00
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));
2003-02-24 01:11:57 +00:00
memcpy(end, inString, len);
if (len < length)
2002-10-04 17:31:41 +00:00
memcpy(m_buffer, inString+len, length-len);
m_size += length;
}
2003-02-24 01:11:57 +00:00
FilterWithBufferedInput::FilterWithBufferedInput(BufferedTransformation *attachment)
2002-10-04 17:31:41 +00:00
: Filter(attachment)
{
}
2005-07-12 04:23:32 +00:00
FilterWithBufferedInput::FilterWithBufferedInput(size_t firstSize, size_t blockSize, size_t lastSize, BufferedTransformation *attachment)
2002-10-04 17:31:41 +00:00
: Filter(attachment), m_firstSize(firstSize), m_blockSize(blockSize), m_lastSize(lastSize)
, m_firstInputDone(false)
{
2003-02-24 01:11:57 +00:00
if (m_firstSize < 0 || m_blockSize < 1 || m_lastSize < 0)
throw InvalidArgument("FilterWithBufferedInput: invalid buffer size");
2002-10-04 17:31:41 +00:00
m_queue.ResetQueue(1, m_firstSize);
}
void FilterWithBufferedInput::IsolatedInitialize(const NameValuePairs &parameters)
{
2003-02-24 01:11:57 +00:00
InitializeDerivedAndReturnNewSizes(parameters, m_firstSize, m_blockSize, m_lastSize);
if (m_firstSize < 0 || m_blockSize < 1 || m_lastSize < 0)
throw InvalidArgument("FilterWithBufferedInput: invalid buffer size");
2002-10-04 17:31:41 +00:00
m_queue.ResetQueue(1, m_firstSize);
m_firstInputDone = false;
}
2003-02-24 01:11:57 +00:00
bool FilterWithBufferedInput::IsolatedFlush(bool hardFlush, bool blocking)
2002-10-04 17:31:41 +00:00
{
if (!blocking)
throw BlockingInputOnly("FilterWithBufferedInput");
if (hardFlush)
ForceNextPut();
FlushDerived();
return false;
}
2005-07-12 04:23:32 +00:00
size_t FilterWithBufferedInput::PutMaybeModifiable(byte *inString, size_t length, int messageEnd, bool blocking, bool modifiable)
2002-10-04 17:31:41 +00:00
{
if (!blocking)
throw BlockingInputOnly("FilterWithBufferedInput");
if (length != 0)
{
2005-07-12 04:23:32 +00:00
size_t newLength = m_queue.CurrentSize() + length;
2002-10-04 17:31:41 +00:00
if (!m_firstInputDone && newLength >= m_firstSize)
{
2005-07-12 04:23:32 +00:00
size_t len = m_firstSize - m_queue.CurrentSize();
2002-10-04 17:31:41 +00:00
m_queue.Put(inString, len);
FirstPut(m_queue.GetContigousBlocks(m_firstSize));
2003-02-24 01:11:57 +00:00
assert(m_queue.CurrentSize() == 0);
m_queue.ResetQueue(m_blockSize, (2*m_blockSize+m_lastSize-2)/m_blockSize);
2002-10-04 17:31:41 +00:00
2003-02-24 01:11:57 +00:00
inString += len;
2002-10-04 17:31:41 +00:00
newLength -= m_firstSize;
m_firstInputDone = true;
}
if (m_firstInputDone)
{
2003-02-24 01:11:57 +00:00
if (m_blockSize == 1)
2002-10-04 17:31:41 +00:00
{
while (newLength > m_lastSize && m_queue.CurrentSize() > 0)
{
2005-07-12 04:23:32 +00:00
size_t len = newLength - m_lastSize;
2003-02-24 01:11:57 +00:00
byte *ptr = m_queue.GetContigousBlocks(len);
2002-10-04 17:31:41 +00:00
NextPutModifiable(ptr, len);
newLength -= len;
}
2003-02-24 01:11:57 +00:00
if (newLength > m_lastSize)
2002-10-04 17:31:41 +00:00
{
2005-07-12 04:23:32 +00:00
size_t len = newLength - m_lastSize;
2002-10-04 17:31:41 +00:00
NextPutMaybeModifiable(inString, len, modifiable);
2003-02-24 01:11:57 +00:00
inString += len;
2002-10-04 17:31:41 +00:00
newLength -= len;
}
}
else
{
2003-02-24 01:11:57 +00:00
while (newLength >= m_blockSize + m_lastSize && m_queue.CurrentSize() >= m_blockSize)
2002-10-04 17:31:41 +00:00
{
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);
2005-07-12 04:23:32 +00:00
size_t len = m_blockSize - m_queue.CurrentSize();
2002-10-04 17:31:41 +00:00
m_queue.Put(inString, len);
2003-02-24 01:11:57 +00:00
inString += len;
2002-10-04 17:31:41 +00:00
NextPutModifiable(m_queue.GetBlock(), m_blockSize);
newLength -= m_blockSize;
}
if (newLength >= m_blockSize + m_lastSize)
{
2005-07-12 04:23:32 +00:00
size_t len = RoundDownToMultipleOf(newLength - m_lastSize, m_blockSize);
2002-10-04 17:31:41 +00:00
NextPutMaybeModifiable(inString, len, modifiable);
2003-02-24 01:11:57 +00:00
inString += len;
2002-10-04 17:31:41 +00:00
newLength -= len;
}
}
}
2003-02-24 01:11:57 +00:00
m_queue.Put(inString, newLength - m_queue.CurrentSize());
2002-10-04 17:31:41 +00:00
}
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);
2003-02-24 01:11:57 +00:00
Output(1, NULL, 0, messageEnd, blocking);
2002-10-04 17:31:41 +00:00
}
return 0;
}
void FilterWithBufferedInput::ForceNextPut()
{
if (!m_firstInputDone)
return;
2003-02-24 01:11:57 +00:00
if (m_blockSize > 1)
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
while (m_queue.CurrentSize() >= m_blockSize)
2002-10-04 17:31:41 +00:00
NextPutModifiable(m_queue.GetBlock(), m_blockSize);
}
else
{
2005-07-12 04:23:32 +00:00
size_t len;
2003-02-24 01:11:57 +00:00
while ((len = m_queue.CurrentSize()) > 0)
2002-10-04 17:31:41 +00:00
NextPutModifiable(m_queue.GetContigousBlocks(len), len);
}
}
2005-07-12 04:23:32 +00:00
void FilterWithBufferedInput::NextPutMultiple(const byte *inString, size_t length)
2002-10-04 17:31:41 +00:00
{
assert(m_blockSize > 1); // m_blockSize = 1 should always override this function
2003-02-24 01:11:57 +00:00
while (length > 0)
2002-10-04 17:31:41 +00:00
{
assert(length >= m_blockSize);
NextPutSingle(inString);
2003-02-24 01:11:57 +00:00
inString += m_blockSize;
2002-10-04 17:31:41 +00:00
length -= m_blockSize;
}
}
// *************************************************************
void Redirector::Initialize(const NameValuePairs &parameters, int propagation)
2002-10-04 17:31:41 +00:00
{
m_target = parameters.GetValueWithDefault("RedirectionTargetPointer", (BufferedTransformation*)NULL);
m_behavior = parameters.GetIntValueWithDefault("RedirectionBehavior", PASS_EVERYTHING);
2002-10-04 17:31:41 +00:00
if (m_target && GetPassSignals())
m_target->Initialize(parameters, propagation);
2002-10-04 17:31:41 +00:00
}
// *************************************************************
2005-07-12 04:23:32 +00:00
ProxyFilter::ProxyFilter(BufferedTransformation *filter, size_t firstSize, size_t lastSize, BufferedTransformation *attachment)
2003-02-24 01:11:57 +00:00
: FilterWithBufferedInput(firstSize, 1, lastSize, attachment), m_filter(filter)
2002-10-04 17:31:41 +00:00
{
if (m_filter.get())
2002-10-06 03:23:16 +00:00
m_filter->Attach(new OutputProxy(*this, false));
2002-10-04 17:31:41 +00:00
}
2003-02-24 01:11:57 +00:00
bool ProxyFilter::IsolatedFlush(bool hardFlush, bool blocking)
2002-10-04 17:31:41 +00:00
{
2002-10-06 03:23:16 +00:00
return m_filter.get() ? m_filter->Flush(hardFlush, -1, blocking) : false;
2002-10-04 17:31:41 +00:00
}
void ProxyFilter::SetFilter(Filter *filter)
{
m_filter.reset(filter);
if (filter)
{
2003-02-24 01:11:57 +00:00
OutputProxy *proxy;
2002-10-06 03:23:16 +00:00
std::auto_ptr<OutputProxy> temp(proxy = new OutputProxy(*this, false));
m_filter->TransferAllTo(*proxy);
2002-10-04 17:31:41 +00:00
m_filter->Attach(temp.release());
}
}
2005-07-12 04:23:32 +00:00
void ProxyFilter::NextPutMultiple(const byte *s, size_t len)
2002-10-04 17:31:41 +00:00
{
if (m_filter.get())
m_filter->Put(s, len);
}
2005-07-12 04:23:32 +00:00
void ProxyFilter::NextPutModifiable(byte *s, size_t len)
2003-06-06 02:34:03 +00:00
{
if (m_filter.get())
m_filter->PutModifiable(s, len);
}
2002-10-04 17:31:41 +00:00
// *************************************************************
2005-07-12 04:23:32 +00:00
size_t ArraySink::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
memcpy(m_buf+m_total, begin, STDMIN(length, SaturatingSubtract(m_size, m_total)));
m_total += length;
2002-10-04 17:31:41 +00:00
return 0;
}
2005-07-12 04:23:32 +00:00
byte * ArraySink::CreatePutSpace(size_t &size)
2002-10-04 17:31:41 +00:00
{
2005-07-12 04:23:32 +00:00
size = SaturatingSubtract(m_size, m_total);
2002-10-04 17:31:41 +00:00
return m_buf + m_total;
}
2003-02-24 01:11:57 +00:00
void ArraySink::IsolatedInitialize(const NameValuePairs &parameters)
2002-10-04 17:31:41 +00:00
{
ByteArrayParameter array;
if (!parameters.GetValue(Name::OutputBuffer(), array))
throw InvalidArgument("ArraySink: missing OutputBuffer argument");
2003-02-24 01:11:57 +00:00
m_buf = array.begin();
2002-10-04 17:31:41 +00:00
m_size = array.size();
2003-02-24 01:11:57 +00:00
m_total = 0;
2002-10-04 17:31:41 +00:00
}
2005-07-12 04:23:32 +00:00
size_t ArrayXorSink::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
xorbuf(m_buf+m_total, begin, STDMIN(length, SaturatingSubtract(m_size, m_total)));
m_total += length;
2002-10-04 17:31:41 +00:00
return 0;
}
// *************************************************************
2005-07-12 04:23:32 +00:00
size_t StreamTransformationFilter::LastBlockSize(StreamTransformation &c, BlockPaddingScheme padding)
2002-10-04 17:31:41 +00:00
{
if (c.MinLastBlockSize() > 0)
return c.MinLastBlockSize();
2003-02-24 01:11:57 +00:00
else if (c.MandatoryBlockSize() > 1 && !c.IsForwardTransformation() && padding != NO_PADDING && padding != ZEROS_PADDING)
2002-10-04 17:31:41 +00:00
return c.MandatoryBlockSize();
else
return 0;
}
2003-02-24 01:11:57 +00:00
StreamTransformationFilter::StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment, BlockPaddingScheme padding)
: FilterWithBufferedInput(0, c.MandatoryBlockSize(), LastBlockSize(c, padding), attachment)
2002-10-04 17:31:41 +00:00
, m_cipher(c)
{
2003-02-24 01:11:57 +00:00
assert(c.MinLastBlockSize() == 0 || c.MinLastBlockSize() > c.MandatoryBlockSize());
2002-10-04 17:31:41 +00:00
2003-02-24 01:11:57 +00:00
bool isBlockCipher = (c.MandatoryBlockSize() > 1 && c.MinLastBlockSize() == 0);
2002-10-04 17:31:41 +00:00
2003-02-24 01:11:57 +00:00
if (padding == DEFAULT_PADDING)
2002-10-04 17:31:41 +00:00
{
if (isBlockCipher)
2003-02-24 01:11:57 +00:00
m_padding = PKCS_PADDING;
2002-10-04 17:31:41 +00:00
else
2003-02-24 01:11:57 +00:00
m_padding = NO_PADDING;
2002-10-04 17:31:41 +00:00
}
else
2003-02-24 01:11:57 +00:00
m_padding = padding;
2002-10-04 17:31:41 +00:00
2003-02-24 01:11:57 +00:00
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 " + c.AlgorithmName());
2002-10-04 17:31:41 +00:00
}
2003-02-24 01:11:57 +00:00
void StreamTransformationFilter::FirstPut(const byte *inString)
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
m_optimalBufferSize = m_cipher.OptimalBlockSize();
2005-07-12 04:23:32 +00:00
m_optimalBufferSize = (unsigned int)STDMAX(m_optimalBufferSize, RoundDownToMultipleOf(4096U, m_optimalBufferSize));
2002-10-04 17:31:41 +00:00
}
2005-07-12 04:23:32 +00:00
void StreamTransformationFilter::NextPutMultiple(const byte *inString, size_t length)
2002-10-04 17:31:41 +00:00
{
if (!length)
return;
2005-07-12 04:23:32 +00:00
size_t s = m_cipher.MandatoryBlockSize();
2002-10-04 17:31:41 +00:00
do
{
2005-07-12 04:23:32 +00:00
size_t len = m_optimalBufferSize;
2003-02-24 01:11:57 +00:00
byte *space = HelpCreatePutSpace(*AttachedTransformation(), NULL_CHANNEL, s, length, len);
if (len < length)
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
if (len == m_optimalBufferSize)
len -= m_cipher.GetOptimalBlockSizeUsed();
len = RoundDownToMultipleOf(len, s);
2002-10-04 17:31:41 +00:00
}
else
2003-02-24 01:11:57 +00:00
len = length;
m_cipher.ProcessString(space, inString, len);
2002-10-04 17:31:41 +00:00
AttachedTransformation()->PutModifiable(space, len);
2003-02-24 01:11:57 +00:00
inString += len;
2002-10-04 17:31:41 +00:00
length -= len;
}
2003-02-24 01:11:57 +00:00
while (length > 0);
2002-10-04 17:31:41 +00:00
}
2005-07-12 04:23:32 +00:00
void StreamTransformationFilter::NextPutModifiable(byte *inString, size_t length)
2002-10-04 17:31:41 +00:00
{
m_cipher.ProcessString(inString, length);
AttachedTransformation()->PutModifiable(inString, length);
}
2005-07-12 04:23:32 +00:00
void StreamTransformationFilter::LastPut(const byte *inString, size_t length)
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
byte *space = NULL;
2002-10-04 17:31:41 +00:00
switch (m_padding)
{
case NO_PADDING:
case ZEROS_PADDING:
if (length > 0)
{
2005-07-12 04:23:32 +00:00
size_t minLastBlockSize = m_cipher.MinLastBlockSize();
2002-10-04 17:31:41 +00:00
bool isForwardTransformation = m_cipher.IsForwardTransformation();
2003-02-24 01:11:57 +00:00
if (isForwardTransformation && m_padding == ZEROS_PADDING && (minLastBlockSize == 0 || length < minLastBlockSize))
2002-10-04 17:31:41 +00:00
{
// do padding
2005-07-12 04:23:32 +00:00
size_t blockSize = STDMAX(minLastBlockSize, (size_t)m_cipher.MandatoryBlockSize());
2003-02-24 01:11:57 +00:00
space = HelpCreatePutSpace(*AttachedTransformation(), NULL_CHANNEL, blockSize);
memcpy(space, inString, length);
memset(space + length, 0, blockSize - length);
m_cipher.ProcessLastBlock(space, space, blockSize);
2002-10-04 17:31:41 +00:00
AttachedTransformation()->Put(space, blockSize);
}
else
{
2003-02-24 01:11:57 +00:00
if (minLastBlockSize == 0)
2002-10-04 17:31:41 +00:00
{
if (isForwardTransformation)
2003-02-24 01:11:57 +00:00
throw InvalidDataFormat("StreamTransformationFilter: plaintext length is not a multiple of block size and NO_PADDING is specified");
2002-10-04 17:31:41 +00:00
else
2003-02-24 01:11:57 +00:00
throw InvalidCiphertext("StreamTransformationFilter: ciphertext length is not a multiple of block size");
2002-10-04 17:31:41 +00:00
}
2003-02-24 01:11:57 +00:00
space = HelpCreatePutSpace(*AttachedTransformation(), NULL_CHANNEL, length, m_optimalBufferSize);
2002-10-04 17:31:41 +00:00
m_cipher.ProcessLastBlock(space, inString, length);
AttachedTransformation()->Put(space, length);
}
}
break;
case PKCS_PADDING:
case ONE_AND_ZEROS_PADDING:
unsigned int s;
2003-02-24 01:11:57 +00:00
s = m_cipher.MandatoryBlockSize();
2002-10-04 17:31:41 +00:00
assert(s > 1);
2003-02-24 01:11:57 +00:00
space = HelpCreatePutSpace(*AttachedTransformation(), NULL_CHANNEL, s, m_optimalBufferSize);
2002-10-04 17:31:41 +00:00
if (m_cipher.IsForwardTransformation())
{
2003-02-24 01:11:57 +00:00
assert(length < s);
memcpy(space, inString, length);
2002-10-04 17:31:41 +00:00
if (m_padding == PKCS_PADDING)
{
assert(s < 256);
2005-07-12 04:23:32 +00:00
byte pad = byte(s-length);
2002-10-04 17:31:41 +00:00
memset(space+length, pad, s-length);
}
else
{
2004-02-05 03:35:13 +00:00
space[length] = 0x80;
2002-10-04 17:31:41 +00:00
memset(space+length+1, 0, s-length-1);
}
2003-02-24 01:11:57 +00:00
m_cipher.ProcessData(space, space, s);
2002-10-04 17:31:41 +00:00
AttachedTransformation()->Put(space, s);
}
else
{
if (length != s)
2003-02-24 01:11:57 +00:00
throw InvalidCiphertext("StreamTransformationFilter: ciphertext length is not a multiple of block size");
m_cipher.ProcessData(space, inString, s);
2002-10-04 17:31:41 +00:00
if (m_padding == PKCS_PADDING)
{
byte pad = space[s-1];
2003-02-24 01:11:57 +00:00
if (pad < 1 || pad > s || std::find_if(space+s-pad, space+s, std::bind2nd(std::not_equal_to<byte>(), pad)) != space+s)
2002-10-04 17:31:41 +00:00
throw InvalidCiphertext("StreamTransformationFilter: invalid PKCS #7 block padding found");
length = s-pad;
}
else
{
2004-02-05 03:35:13 +00:00
while (length > 1 && space[length-1] == 0)
2002-10-04 17:31:41 +00:00
--length;
2004-02-05 03:35:13 +00:00
if (space[--length] != 0x80)
2003-02-24 01:11:57 +00:00
throw InvalidCiphertext("StreamTransformationFilter: invalid ones-and-zeros padding found");
2002-10-04 17:31:41 +00:00
}
AttachedTransformation()->Put(space, length);
}
break;
default:
assert(false);
}
}
// *************************************************************
void HashFilter::IsolatedInitialize(const NameValuePairs &parameters)
{
m_putMessage = parameters.GetValueWithDefault(Name::PutMessage(), false);
m_truncatedDigestSize = parameters.GetIntValueWithDefault(Name::TruncatedDigestSize(), -1);
2002-10-04 17:31:41 +00:00
m_hashModule.Restart();
}
2005-07-12 04:23:32 +00:00
size_t HashFilter::Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
2002-10-04 17:31:41 +00:00
{
FILTER_BEGIN;
m_hashModule.Update(inString, length);
if (m_putMessage)
FILTER_OUTPUT(1, inString, length, 0);
if (messageEnd)
{
{
2005-07-12 04:23:32 +00:00
size_t size;
m_digestSize = m_hashModule.DigestSize();
if (m_truncatedDigestSize >= 0 && (unsigned int)m_truncatedDigestSize < m_digestSize)
m_digestSize = m_truncatedDigestSize;
m_space = HelpCreatePutSpace(*AttachedTransformation(), NULL_CHANNEL, m_digestSize, m_digestSize, size = m_digestSize);
m_hashModule.TruncatedFinal(m_space, m_digestSize);
2002-10-04 17:31:41 +00:00
}
FILTER_OUTPUT(2, m_space, m_digestSize, messageEnd);
2002-10-04 17:31:41 +00:00
}
FILTER_END_NO_MESSAGE_END;
}
// *************************************************************
HashVerificationFilter::HashVerificationFilter(HashTransformation &hm, BufferedTransformation *attachment, word32 flags)
: FilterWithBufferedInput(attachment)
, m_hashModule(hm)
{
IsolatedInitialize(MakeParameters(Name::HashVerificationFilterFlags(), flags));
}
2005-07-12 04:23:32 +00:00
void HashVerificationFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
m_flags = parameters.GetValueWithDefault(Name::HashVerificationFilterFlags(), (word32)DEFAULT_FLAGS);
2002-10-04 17:31:41 +00:00
m_hashModule.Restart();
2005-07-12 04:23:32 +00:00
size_t size = m_hashModule.DigestSize();
2002-10-04 17:31:41 +00:00
m_verified = false;
2003-02-24 01:11:57 +00:00
firstSize = m_flags & HASH_AT_BEGIN ? size : 0;
blockSize = 1;
2002-10-04 17:31:41 +00:00
lastSize = m_flags & HASH_AT_BEGIN ? 0 : size;
}
2003-02-24 01:11:57 +00:00
void HashVerificationFilter::FirstPut(const byte *inString)
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
if (m_flags & HASH_AT_BEGIN)
2002-10-04 17:31:41 +00:00
{
m_expectedHash.New(m_hashModule.DigestSize());
memcpy(m_expectedHash, inString, m_expectedHash.size());
2003-02-24 01:11:57 +00:00
if (m_flags & PUT_HASH)
AttachedTransformation()->Put(inString, m_expectedHash.size());
2002-10-04 17:31:41 +00:00
}
}
2005-07-12 04:23:32 +00:00
void HashVerificationFilter::NextPutMultiple(const byte *inString, size_t length)
2002-10-04 17:31:41 +00:00
{
m_hashModule.Update(inString, length);
2003-02-24 01:11:57 +00:00
if (m_flags & PUT_MESSAGE)
AttachedTransformation()->Put(inString, length);
2002-10-04 17:31:41 +00:00
}
2005-07-12 04:23:32 +00:00
void HashVerificationFilter::LastPut(const byte *inString, size_t length)
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
if (m_flags & HASH_AT_BEGIN)
2002-10-04 17:31:41 +00:00
{
assert(length == 0);
m_verified = m_hashModule.Verify(m_expectedHash);
}
else
{
2003-02-24 01:11:57 +00:00
m_verified = (length==m_hashModule.DigestSize() && m_hashModule.Verify(inString));
if (m_flags & PUT_HASH)
AttachedTransformation()->Put(inString, length);
2002-10-04 17:31:41 +00:00
}
2003-02-24 01:11:57 +00:00
if (m_flags & PUT_RESULT)
2002-10-04 17:31:41 +00:00
AttachedTransformation()->Put(m_verified);
2003-02-24 01:11:57 +00:00
if ((m_flags & THROW_EXCEPTION) && !m_verified)
2002-10-04 17:31:41 +00:00
throw HashVerificationFailed();
}
// *************************************************************
2003-02-24 01:11:57 +00:00
void SignerFilter::IsolatedInitialize(const NameValuePairs &parameters)
2002-10-04 17:31:41 +00:00
{
m_putMessage = parameters.GetValueWithDefault(Name::PutMessage(), false);
2003-04-22 00:12:41 +00:00
m_messageAccumulator.reset(m_signer.NewSignatureAccumulator(m_rng));
2002-10-04 17:31:41 +00:00
}
2005-07-12 04:23:32 +00:00
size_t SignerFilter::Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
2002-10-04 17:31:41 +00:00
{
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);
2003-02-24 01:11:57 +00:00
FILTER_OUTPUT(2, m_buf, m_buf.size(), messageEnd);
2003-04-22 00:12:41 +00:00
m_messageAccumulator.reset(m_signer.NewSignatureAccumulator(m_rng));
2002-10-04 17:31:41 +00:00
}
FILTER_END_NO_MESSAGE_END;
}
SignatureVerificationFilter::SignatureVerificationFilter(const PK_Verifier &verifier, BufferedTransformation *attachment, word32 flags)
: FilterWithBufferedInput(attachment)
, m_verifier(verifier)
{
2003-02-24 01:11:57 +00:00
IsolatedInitialize(MakeParameters(Name::SignatureVerificationFilterFlags(), flags));
2002-10-04 17:31:41 +00:00
}
2005-07-12 04:23:32 +00:00
void SignatureVerificationFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
m_flags = parameters.GetValueWithDefault(Name::SignatureVerificationFilterFlags(), (word32)DEFAULT_FLAGS);
2002-10-04 17:31:41 +00:00
m_messageAccumulator.reset(m_verifier.NewVerificationAccumulator());
2005-07-12 04:23:32 +00:00
size_t size = m_verifier.SignatureLength();
2003-03-20 01:24:12 +00:00
assert(size != 0); // TODO: handle recoverable signature scheme
2002-10-04 17:31:41 +00:00
m_verified = false;
2003-02-24 01:11:57 +00:00
firstSize = m_flags & SIGNATURE_AT_BEGIN ? size : 0;
blockSize = 1;
lastSize = m_flags & SIGNATURE_AT_BEGIN ? 0 : size;
2002-10-04 17:31:41 +00:00
}
void SignatureVerificationFilter::FirstPut(const byte *inString)
{
2003-02-24 01:11:57 +00:00
if (m_flags & SIGNATURE_AT_BEGIN)
2002-10-04 17:31:41 +00:00
{
2003-03-20 01:24:12 +00:00
if (m_verifier.SignatureUpfront())
m_verifier.InputSignature(*m_messageAccumulator, inString, m_verifier.SignatureLength());
2002-10-04 17:31:41 +00:00
else
{
m_signature.New(m_verifier.SignatureLength());
2003-02-24 01:11:57 +00:00
memcpy(m_signature, inString, m_signature.size());
2002-10-04 17:31:41 +00:00
}
2003-02-24 01:11:57 +00:00
if (m_flags & PUT_SIGNATURE)
AttachedTransformation()->Put(inString, m_signature.size());
2002-10-04 17:31:41 +00:00
}
else
{
2003-03-20 01:24:12 +00:00
assert(!m_verifier.SignatureUpfront());
2002-10-04 17:31:41 +00:00
}
}
2005-07-12 04:23:32 +00:00
void SignatureVerificationFilter::NextPutMultiple(const byte *inString, size_t length)
2002-10-04 17:31:41 +00:00
{
m_messageAccumulator->Update(inString, length);
2003-02-24 01:11:57 +00:00
if (m_flags & PUT_MESSAGE)
AttachedTransformation()->Put(inString, length);
2002-10-04 17:31:41 +00:00
}
2005-07-12 04:23:32 +00:00
void SignatureVerificationFilter::LastPut(const byte *inString, size_t length)
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
if (m_flags & SIGNATURE_AT_BEGIN)
2002-10-04 17:31:41 +00:00
{
assert(length == 0);
2003-03-20 01:24:12 +00:00
m_verifier.InputSignature(*m_messageAccumulator, m_signature, m_signature.size());
m_verified = m_verifier.VerifyAndRestart(*m_messageAccumulator);
2002-10-04 17:31:41 +00:00
}
else
{
2003-03-20 01:24:12 +00:00
m_verifier.InputSignature(*m_messageAccumulator, inString, length);
m_verified = m_verifier.VerifyAndRestart(*m_messageAccumulator);
2003-02-24 01:11:57 +00:00
if (m_flags & PUT_SIGNATURE)
AttachedTransformation()->Put(inString, length);
2002-10-04 17:31:41 +00:00
}
2003-02-24 01:11:57 +00:00
if (m_flags & PUT_RESULT)
2002-10-04 17:31:41 +00:00
AttachedTransformation()->Put(m_verified);
2003-02-24 01:11:57 +00:00
if ((m_flags & THROW_EXCEPTION) && !m_verified)
2002-10-04 17:31:41 +00:00
throw SignatureVerificationFailed();
}
// *************************************************************
2005-07-12 04:23:32 +00:00
size_t Source::PumpAll2(bool blocking)
2002-10-04 17:31:41 +00:00
{
2005-07-12 04:23:32 +00:00
unsigned int messageCount = UINT_MAX;
do {
RETURN_IF_NONZERO(PumpMessages2(messageCount, blocking));
} while(messageCount == UINT_MAX);
return 0;
2002-10-04 17:31:41 +00:00
}
bool Store::GetNextMessage()
{
if (!m_messageEnd && !AnyRetrievable())
{
m_messageEnd=true;
return true;
}
else
return false;
}
2003-02-24 01:11:57 +00:00
unsigned int Store::CopyMessagesTo(BufferedTransformation &target, unsigned int count, const std::string &channel) const
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
if (m_messageEnd || count == 0)
2002-10-04 17:31:41 +00:00
return 0;
else
{
CopyTo(target, ULONG_MAX, channel);
if (GetAutoSignalPropagation())
target.ChannelMessageEnd(channel, GetAutoSignalPropagation()-1);
return 1;
}
}
2003-02-24 01:11:57 +00:00
void StringStore::StoreInitialize(const NameValuePairs &parameters)
2002-10-04 17:31:41 +00:00
{
2003-02-24 01:11:57 +00:00
ConstByteArrayParameter array;
2002-10-04 17:31:41 +00:00
if (!parameters.GetValue(Name::InputBuffer(), array))
2003-02-24 01:11:57 +00:00
throw InvalidArgument("StringStore: missing InputBuffer argument");
m_store = array.begin();
2002-10-04 17:31:41 +00:00
m_length = array.size();
2003-02-24 01:11:57 +00:00
m_count = 0;
2002-10-04 17:31:41 +00:00
}
2005-07-12 04:23:32 +00:00
size_t StringStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
2002-10-04 17:31:41 +00:00
{
2005-07-12 04:23:32 +00:00
lword position = 0;
size_t blockedBytes = CopyRangeTo2(target, position, transferBytes, channel, blocking);
m_count += (size_t)position;
2003-02-24 01:11:57 +00:00
transferBytes = position;
2002-10-04 17:31:41 +00:00
return blockedBytes;
}
2005-07-12 04:23:32 +00:00
size_t StringStore::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
2002-10-04 17:31:41 +00:00
{
2005-07-12 04:23:32 +00:00
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);
2002-10-04 17:31:41 +00:00
if (!blockedBytes)
begin += len;
return blockedBytes;
}
2003-07-18 21:33:18 +00:00
void RandomNumberStore::StoreInitialize(const NameValuePairs &parameters)
{
parameters.GetRequiredParameter("RandomNumberStore", "RandomNumberGeneratorPointer", m_rng);
2005-07-12 04:23:32 +00:00
int length;
parameters.GetRequiredIntParameter("RandomNumberStore", "RandomNumberStoreSize", length);
m_length = length;
2003-07-18 21:33:18 +00:00
}
2005-07-12 04:23:32 +00:00
size_t RandomNumberStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
2002-10-04 17:31:41 +00:00
{
if (!blocking)
2003-02-24 01:11:57 +00:00
throw NotImplemented("RandomNumberStore: nonblocking transfer is not implemented by this object");
2002-10-04 17:31:41 +00:00
2005-07-12 04:23:32 +00:00
lword transferMax = transferBytes;
for (transferBytes = 0; transferBytes<transferMax && m_count < (unsigned int)m_length; ++transferBytes, ++m_count)
2003-07-18 21:33:18 +00:00
target.ChannelPut(channel, m_rng->GenerateByte());
2002-10-04 17:31:41 +00:00
return 0;
}
2005-07-12 04:23:32 +00:00
size_t NullStore::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
2002-10-04 17:31:41 +00:00
{
static const byte nullBytes[128] = {0};
while (begin < end)
{
2005-07-12 04:23:32 +00:00
size_t len = (size_t)STDMIN(end-begin, lword(128));
size_t blockedBytes = target.ChannelPut2(channel, nullBytes, len, 0, blocking);
2002-10-04 17:31:41 +00:00
if (blockedBytes)
return blockedBytes;
begin += len;
}
return 0;
}
2005-07-12 04:23:32 +00:00
size_t NullStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
2002-10-04 17:31:41 +00:00
{
2005-07-12 04:23:32 +00:00
lword begin = 0;
size_t blockedBytes = NullStore::CopyRangeTo2(target, begin, transferBytes, channel, blocking);
2003-02-24 01:11:57 +00:00
transferBytes = begin;
2002-10-04 17:31:41 +00:00
m_size -= begin;
return blockedBytes;
}
NAMESPACE_END
#endif