summaryrefslogtreecommitdiff
path: root/filters.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'filters.cpp')
-rw-r--r--filters.cpp84
1 files changed, 76 insertions, 8 deletions
diff --git a/filters.cpp b/filters.cpp
index 537318d1..deed638f 100644
--- a/filters.cpp
+++ b/filters.cpp
@@ -106,6 +106,9 @@ void Filter::PropagateInitialize(const NameValuePairs &parameters, int propagati
size_t Filter::OutputModifiable(int outputSite, byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel)
{
+ assert((inString && length) || (!inString && !length));
+ if (inString == NULL) { length = 0; }
+
if (messageEnd)
messageEnd--;
size_t result = AttachedTransformation()->ChannelPutModifiable2(channel, inString, length, messageEnd, blocking);
@@ -115,6 +118,9 @@ size_t Filter::OutputModifiable(int outputSite, byte *inString, size_t length, i
size_t Filter::Output(int outputSite, const byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel)
{
+ assert((inString && length) || (!inString && !length));
+ if (inString == NULL) { length = 0; }
+
if (messageEnd)
messageEnd--;
size_t result = AttachedTransformation()->ChannelPut2(channel, inString, length, messageEnd, blocking);
@@ -274,6 +280,8 @@ byte *FilterWithBufferedInput::BlockQueue::GetContigousBlocks(size_t &numberOfBy
size_t FilterWithBufferedInput::BlockQueue::GetAll(byte *outString)
{
+ if (!outString) return 0;
+
size_t size = m_size;
size_t numberOfBytes = m_maxBlocks*m_blockSize;
const byte *ptr = GetContigousBlocks(numberOfBytes);
@@ -283,8 +291,11 @@ size_t FilterWithBufferedInput::BlockQueue::GetAll(byte *outString)
return size;
}
-void FilterWithBufferedInput::BlockQueue::Put(const byte *inString, size_t length)
+size_t FilterWithBufferedInput::BlockQueue::Put(const byte *inString, size_t length)
{
+ if (!inString || !length) return length;
+ if (!m_buffer.data()) return length;
+
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));
@@ -292,6 +303,7 @@ void FilterWithBufferedInput::BlockQueue::Put(const byte *inString, size_t lengt
if (len < length)
memcpy(m_buffer, inString+len, length-len);
m_size += length;
+ return 0;
}
FilterWithBufferedInput::FilterWithBufferedInput(BufferedTransformation *attachment)
@@ -332,6 +344,9 @@ bool FilterWithBufferedInput::IsolatedFlush(bool hardFlush, bool blocking)
size_t FilterWithBufferedInput::PutMaybeModifiable(byte *inString, size_t length, int messageEnd, bool blocking, bool modifiable)
{
+ assert((inString && length) || (!inString && !length));
+ if (inString == NULL) { length = 0; }
+
if (!blocking)
throw BlockingInputOnly("FilterWithBufferedInput");
@@ -440,6 +455,9 @@ void FilterWithBufferedInput::ForceNextPut()
void FilterWithBufferedInput::NextPutMultiple(const byte *inString, size_t length)
{
+ assert((inString && length) || (!inString && !length));
+ if (inString == NULL) { length = 0; }
+
assert(m_blockSize > 1); // m_blockSize = 1 should always override this function
while (length > 0)
{
@@ -514,10 +532,13 @@ size_t RandomNumberSink::Put2(const byte *begin, size_t length, int messageEnd,
size_t ArraySink::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
{
- if(!begin || !length) return length;
+ if (!begin || !length) return length;
+ if (!m_buf) return length;
+
if (m_buf+m_total != begin)
memcpy(m_buf+m_total, begin, STDMIN(length, SaturatingSubtract(m_size, m_total)));
m_total += length;
+
return 0;
}
@@ -539,6 +560,9 @@ void ArraySink::IsolatedInitialize(const NameValuePairs &parameters)
size_t ArrayXorSink::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
{
+ if (!begin || !length) return length;
+ if (!m_buf) return length;
+
xorbuf(m_buf+m_total, begin, STDMIN(length, SaturatingSubtract(m_size, m_total)));
m_total += length;
return 0;
@@ -588,14 +612,15 @@ void StreamTransformationFilter::InitializeDerivedAndReturnNewSizes(const NameVa
void StreamTransformationFilter::FirstPut(const byte *inString)
{
+ assert(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;
+ assert((inString && length) || (!inString && !length));
+ if (inString == NULL) { length = 0; }
size_t s = m_cipher.MandatoryBlockSize();
@@ -610,7 +635,9 @@ void StreamTransformationFilter::NextPutMultiple(const byte *inString, size_t le
len = RoundDownToMultipleOf(len, s);
}
else
+ {
len = length;
+ }
m_cipher.ProcessString(space, inString, len);
AttachedTransformation()->PutModifiable(space, len);
inString += len;
@@ -621,12 +648,18 @@ void StreamTransformationFilter::NextPutMultiple(const byte *inString, size_t le
void StreamTransformationFilter::NextPutModifiable(byte *inString, size_t length)
{
+ assert((inString && length) || (!inString && !length));
+ if (inString == NULL) { length = 0; }
+
m_cipher.ProcessString(inString, length);
AttachedTransformation()->PutModifiable(inString, length);
}
void StreamTransformationFilter::LastPut(const byte *inString, size_t length)
{
+ assert((inString && length) || (!inString && !length));
+ if (inString == NULL) { length = 0; }
+
byte *space = NULL;
switch (m_padding)
@@ -643,7 +676,8 @@ void StreamTransformationFilter::LastPut(const byte *inString, size_t length)
// do padding
size_t blockSize = STDMAX(minLastBlockSize, (size_t)m_cipher.MandatoryBlockSize());
space = HelpCreatePutSpace(*AttachedTransformation(), DEFAULT_CHANNEL, blockSize);
- memcpy(space, inString, length);
+ if (inString && length)
+ memcpy(space, inString, length);
memset(space + length, 0, blockSize - length);
m_cipher.ProcessLastBlock(space, space, blockSize);
AttachedTransformation()->Put(space, blockSize);
@@ -674,7 +708,8 @@ void StreamTransformationFilter::LastPut(const byte *inString, size_t length)
if (m_cipher.IsForwardTransformation())
{
assert(length < s);
- memcpy(space, inString, length);
+ if (inString && length)
+ memcpy(space, inString, length);
if (m_padding == PKCS_PADDING)
{
assert(s < 256);
@@ -697,7 +732,7 @@ void StreamTransformationFilter::LastPut(const byte *inString, size_t length)
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)
+ 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;
}
@@ -735,6 +770,9 @@ void HashFilter::IsolatedInitialize(const NameValuePairs &parameters)
size_t HashFilter::Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
{
+ assert((inString && length) || (!inString && !length));
+ if (inString == NULL) { length = 0; }
+
FILTER_BEGIN;
if (m_putMessage)
FILTER_OUTPUT3(1, 0, inString, length, 0, m_messagePutChannel);
@@ -773,10 +811,14 @@ void HashVerificationFilter::InitializeDerivedAndReturnNewSizes(const NameValueP
void HashVerificationFilter::FirstPut(const byte *inString)
{
+ assert(inString);
+
if (m_flags & HASH_AT_BEGIN)
{
m_expectedHash.New(m_digestSize);
- memcpy(m_expectedHash, inString, m_expectedHash.size());
+
+ if(inString)
+ memcpy(m_expectedHash, inString, m_expectedHash.size());
if (m_flags & PUT_HASH)
AttachedTransformation()->Put(inString, m_expectedHash.size());
}
@@ -784,6 +826,9 @@ void HashVerificationFilter::FirstPut(const byte *inString)
void HashVerificationFilter::NextPutMultiple(const byte *inString, size_t length)
{
+ assert((inString && length) || (!inString && !length));
+ if (inString == NULL) { length = 0; }
+
m_hashModule.Update(inString, length);
if (m_flags & PUT_MESSAGE)
AttachedTransformation()->Put(inString, length);
@@ -791,6 +836,9 @@ void HashVerificationFilter::NextPutMultiple(const byte *inString, size_t length
void HashVerificationFilter::LastPut(const byte *inString, size_t length)
{
+ assert((inString && length) || (!inString && !length));
+ if (inString == NULL) { length = 0; }
+
if (m_flags & HASH_AT_BEGIN)
{
assert(length == 0);
@@ -850,6 +898,9 @@ size_t AuthenticatedEncryptionFilter::ChannelPut2(const std::string &channel, co
void AuthenticatedEncryptionFilter::LastPut(const byte *inString, size_t length)
{
+ assert((inString && length) || (!inString && !length));
+ if (inString == NULL) { length = 0; }
+
StreamTransformationFilter::LastPut(inString, length);
m_hf.MessageEnd();
}
@@ -905,16 +956,23 @@ size_t AuthenticatedDecryptionFilter::ChannelPut2(const std::string &channel, co
void AuthenticatedDecryptionFilter::FirstPut(const byte *inString)
{
+ assert(inString && length);
m_hashVerifier.Put(inString, m_firstSize);
}
void AuthenticatedDecryptionFilter::NextPutMultiple(const byte *inString, size_t length)
{
+ assert((inString && length) || (!inString && !length));
+ if (inString == NULL) { length = 0; }
+
m_streamFilter.Put(inString, length);
}
void AuthenticatedDecryptionFilter::LastPut(const byte *inString, size_t length)
{
+ assert((inString && length) || (!inString && !length));
+ if (inString == NULL) { length = 0; }
+
m_streamFilter.MessageEnd();
m_hashVerifier.PutMessageEnd(inString, length);
}
@@ -929,6 +987,9 @@ void SignerFilter::IsolatedInitialize(const NameValuePairs &parameters)
size_t SignerFilter::Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
{
+ assert((inString && length) || (!inString && !length));
+ if (inString == NULL) { length = 0; }
+
FILTER_BEGIN;
m_messageAccumulator->Update(inString, length);
if (m_putMessage)
@@ -964,6 +1025,7 @@ void SignatureVerificationFilter::InitializeDerivedAndReturnNewSizes(const NameV
void SignatureVerificationFilter::FirstPut(const byte *inString)
{
+ assert(inString);
if (m_flags & SIGNATURE_AT_BEGIN)
{
if (m_verifier.SignatureUpfront())
@@ -985,6 +1047,9 @@ void SignatureVerificationFilter::FirstPut(const byte *inString)
void SignatureVerificationFilter::NextPutMultiple(const byte *inString, size_t length)
{
+ assert((inString && length) || (!inString && !length));
+ if (inString == NULL) { length = 0; }
+
m_messageAccumulator->Update(inString, length);
if (m_flags & PUT_MESSAGE)
AttachedTransformation()->Put(inString, length);
@@ -992,6 +1057,9 @@ void SignatureVerificationFilter::NextPutMultiple(const byte *inString, size_t l
void SignatureVerificationFilter::LastPut(const byte *inString, size_t length)
{
+ assert((inString && length) || (!inString && !length));
+ if (inString == NULL) { length = 0; }
+
if (m_flags & SIGNATURE_AT_BEGIN)
{
assert(length == 0);