- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# filters.cpp

    00001 // filters.cpp - written and placed in the public domain by Wei Dai
    00002
    00003 #include "pch.h"
    00004
    00005 #ifndef CRYPTOPP_IMPORTS
    00006
    00007 #include "filters.h"
    00008 #include "mqueue.h"
    00009 #include "fltrimpl.h"
    00010 #include "argnames.h"
    00011 #include <memory>
    00012 #include <functional>
    00013
    00014 NAMESPACE_BEGIN(CryptoPP)
    00015
    00016 Filter::Filter(BufferedTransformation *attachment)
    00017         : m_attachment(attachment), m_continueAt(0)
    00018 {
    00019 }
    00020
    00021 BufferedTransformation * Filter::NewDefaultAttachment() const
    00022 {
    00023         return new MessageQueue;
    00024 }
    00025
    00026 BufferedTransformation * Filter::AttachedTransformation()
    00027 {
    00028         if (m_attachment.get() == NULL)
    00029                 m_attachment.reset(NewDefaultAttachment());
    00030         return m_attachment.get();
    00031 }
    00032
    00033 const BufferedTransformation *Filter::AttachedTransformation() const
    00034 {
    00035         if (m_attachment.get() == NULL)
    00036                 const_cast<Filter *>(this)->m_attachment.reset(NewDefaultAttachment());
    00037         return m_attachment.get();
    00038 }
    00039
    00040 void Filter::Detach(BufferedTransformation *newOut)
    00041 {
    00042         m_attachment.reset(newOut);
    00043 }
    00044
    00045 void Filter::Insert(Filter *filter)
    00046 {
    00047         filter->m_attachment.reset(m_attachment.release());
    00048         m_attachment.reset(filter);
    00049 }
    00050
    00051 size_t Filter::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
    00052 {
    00053         return AttachedTransformation()->CopyRangeTo2(target, begin, end, channel, blocking);
    00054 }
    00055
    00056 size_t Filter::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
    00057 {
    00058         return AttachedTransformation()->TransferTo2(target, transferBytes, channel, blocking);
    00059 }
    00060
    00061 void Filter::Initialize(const NameValuePairs &parameters, int propagation)
    00062 {
    00063         m_continueAt = 0;
    00064         IsolatedInitialize(parameters);
    00065         PropagateInitialize(parameters, propagation);
    00066 }
    00067
    00068 bool Filter::Flush(bool hardFlush, int propagation, bool blocking)
    00069 {
    00070         switch (m_continueAt)
    00071         {
    00072         case 0:
    00073                 if (IsolatedFlush(hardFlush, blocking))
    00074                         return true;
    00075         case 1:
    00076                 if (OutputFlush(1, hardFlush, propagation, blocking))
    00077                         return true;
    00078         }
    00079         return false;
    00080 }
    00081
    00082 bool Filter::MessageSeriesEnd(int propagation, bool blocking)
    00083 {
    00084         switch (m_continueAt)
    00085         {
    00086         case 0:
    00087                 if (IsolatedMessageSeriesEnd(blocking))
    00088                         return true;
    00089         case 1:
    00090                 if (ShouldPropagateMessageSeriesEnd() && OutputMessageSeriesEnd(1, propagation, blocking))
    00091                         return true;
    00092         }
    00093         return false;
    00094 }
    00095
    00096 void Filter::PropagateInitialize(const NameValuePairs &parameters, int propagation)
    00097 {
    00098         if (propagation)
    00099                 AttachedTransformation()->Initialize(parameters, propagation-1);
    00100 }
    00101
    00102 size_t Filter::OutputModifiable(int outputSite, byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel)
    00103 {
    00104         if (messageEnd)
    00105                 messageEnd--;
    00106         size_t result = AttachedTransformation()->PutModifiable2(inString, length, messageEnd, blocking);
    00107         m_continueAt = result ? outputSite : 0;
    00108         return result;
    00109 }
    00110
    00111 size_t Filter::Output(int outputSite, const byte *inString, size_t length, int messageEnd, bool blocking, const std::string &channel)
    00112 {
    00113         if (messageEnd)
    00114                 messageEnd--;
    00115         size_t result = AttachedTransformation()->Put2(inString, length, messageEnd, blocking);
    00116         m_continueAt = result ? outputSite : 0;
    00117         return result;
    00118 }
    00119
    00120 bool Filter::OutputFlush(int outputSite, bool hardFlush, int propagation, bool blocking, const std::string &channel)
    00121 {
    00122         if (propagation && AttachedTransformation()->ChannelFlush(channel, hardFlush, propagation-1, blocking))
    00123         {
    00124                 m_continueAt = outputSite;
    00125                 return true;
    00126         }
    00127         m_continueAt = 0;
    00128         return false;
    00129 }
    00130
    00131 bool Filter::OutputMessageSeriesEnd(int outputSite, int propagation, bool blocking, const std::string &channel)
    00132 {
    00133         if (propagation && AttachedTransformation()->ChannelMessageSeriesEnd(channel, propagation-1, blocking))
    00134         {
    00135                 m_continueAt = outputSite;
    00136                 return true;
    00137         }
    00138         m_continueAt = 0;
    00139         return false;
    00140 }
    00141
    00142 // *************************************************************
    00143
    00144 void MeterFilter::ResetMeter()
    00145 {
    00146         m_currentMessageBytes = m_totalBytes = m_currentSeriesMessages = m_totalMessages = m_totalMessageSeries = 0;
    00147         m_rangesToSkip.clear();
    00148 }
    00149
    00150 void MeterFilter::AddRangeToSkip(unsigned int message, lword position, lword size, bool sortNow)
    00151 {
    00152         MessageRange r = {message, position, size};
    00153         m_rangesToSkip.push_back(r);
    00154         if (sortNow)
    00155                 std::sort(m_rangesToSkip.begin(), m_rangesToSkip.end());
    00156 }
    00157
    00158 size_t MeterFilter::PutMaybeModifiable(byte *begin, size_t length, int messageEnd, bool blocking, bool modifiable)
    00159 {
    00160         if (!m_transparent)
    00161                 return 0;
    00162
    00163         size_t t;
    00164         FILTER_BEGIN;
    00165
    00166         m_begin = begin;
    00167         m_length = length;
    00168
    00169         while (m_length > 0 || messageEnd)
    00170         {
    00171                 if (m_length > 0  && !m_rangesToSkip.empty() && m_rangesToSkip.front().message == m_totalMessages && m_currentMessageBytes + m_length > m_rangesToSkip.front().position)
    00172                 {
    00173                         FILTER_OUTPUT_MAYBE_MODIFIABLE(1, m_begin, t = (size_t)SaturatingSubtract(m_rangesToSkip.front().position, m_currentMessageBytes), false, modifiable);
    00174
    00175                         assert(t < m_length);
    00176                         m_begin += t;
    00177                         m_length -= t;
    00178                         m_currentMessageBytes += t;
    00179                         m_totalBytes += t;
    00180
    00181                         if (m_currentMessageBytes + m_length < m_rangesToSkip.front().position + m_rangesToSkip.front().size)
    00182                                 t = m_length;
    00183                         else
    00184                         {
    00185                                 t = (size_t)SaturatingSubtract(m_rangesToSkip.front().position + m_rangesToSkip.front().size, m_currentMessageBytes);
    00186                                 assert(t <= m_length);
    00187                                 m_rangesToSkip.pop_front();
    00188                         }
    00189
    00190                         m_begin += t;
    00191                         m_length -= t;
    00192                         m_currentMessageBytes += t;
    00193                         m_totalBytes += t;
    00194                 }
    00195                 else
    00196                 {
    00197                         FILTER_OUTPUT_MAYBE_MODIFIABLE(2, m_begin, m_length, messageEnd, modifiable);
    00198
    00199                         m_currentMessageBytes += m_length;
    00200                         m_totalBytes += m_length;
    00201                         m_length = 0;
    00202
    00203                         if (messageEnd)
    00204                         {
    00205                                 m_currentMessageBytes = 0;
    00206                                 m_currentSeriesMessages++;
    00207                                 m_totalMessages++;
    00208                                 messageEnd = false;
    00209                         }
    00210                 }
    00211         }
    00212
    00213         FILTER_END_NO_MESSAGE_END;
    00214 }
    00215
    00216 size_t MeterFilter::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
    00217 {
    00218         return PutMaybeModifiable(const_cast<byte *>(begin), length, messageEnd, blocking, false);
    00219 }
    00220
    00221 size_t MeterFilter::PutModifiable2(byte *begin, size_t length, int messageEnd, bool blocking)
    00222 {
    00223         return PutMaybeModifiable(begin, length, messageEnd, blocking, true);
    00224 }
    00225
    00226 bool MeterFilter::IsolatedMessageSeriesEnd(bool blocking)
    00227 {
    00228         m_currentMessageBytes = 0;
    00229         m_currentSeriesMessages = 0;
    00230         m_totalMessageSeries++;
    00231         return false;
    00232 }
    00233
    00234 // *************************************************************
    00235
    00236 void FilterWithBufferedInput::BlockQueue::ResetQueue(size_t blockSize, size_t maxBlocks)
    00237 {
    00238         m_buffer.New(blockSize * maxBlocks);
    00239         m_blockSize = blockSize;
    00240         m_maxBlocks = maxBlocks;
    00241         m_size = 0;
    00242         m_begin = m_buffer;
    00243 }
    00244
    00245 byte *FilterWithBufferedInput::BlockQueue::GetBlock()
    00246 {
    00247         if (m_size >= m_blockSize)
    00248         {
    00249                 byte *ptr = m_begin;
    00250                 if ((m_begin+=m_blockSize) == m_buffer.end())
    00251                         m_begin = m_buffer;
    00252                 m_size -= m_blockSize;
    00253                 return ptr;
    00254         }
    00255         else
    00256                 return NULL;
    00257 }
    00258
    00259 byte *FilterWithBufferedInput::BlockQueue::GetContigousBlocks(size_t &numberOfBytes)
    00260 {
    00261         numberOfBytes = STDMIN(numberOfBytes, STDMIN(size_t(m_buffer.end()-m_begin), m_size));
    00262         byte *ptr = m_begin;
    00263         m_begin += numberOfBytes;
    00264         m_size -= numberOfBytes;
    00265         if (m_size == 0 || m_begin == m_buffer.end())
    00266                 m_begin = m_buffer;
    00267         return ptr;
    00268 }
    00269
    00270 size_t FilterWithBufferedInput::BlockQueue::GetAll(byte *outString)
    00271 {
    00272         size_t size = m_size;
    00273         size_t numberOfBytes = m_maxBlocks*m_blockSize;
    00274         const byte *ptr = GetContigousBlocks(numberOfBytes);
    00275         memcpy(outString, ptr, numberOfBytes);
    00276         memcpy(outString+numberOfBytes, m_begin, m_size);
    00277         m_size = 0;
    00278         return size;
    00279 }
    00280
    00281 void FilterWithBufferedInput::BlockQueue::Put(const byte *inString, size_t length)
    00282 {
    00283         assert(m_size + length <= m_buffer.size());
    00284         byte *end = (m_size < size_t(m_buffer.end()-m_begin)) ? m_begin + m_size : m_begin + m_size - m_buffer.size();
    00285         size_t len = STDMIN(length, size_t(m_buffer.end()-end));
    00286         memcpy(end, inString, len);
    00287         if (len < length)
    00288                 memcpy(m_buffer, inString+len, length-len);
    00289         m_size += length;
    00290 }
    00291
    00292 FilterWithBufferedInput::FilterWithBufferedInput(BufferedTransformation *attachment)
    00293         : Filter(attachment)
    00294 {
    00295 }
    00296
    00297 FilterWithBufferedInput::FilterWithBufferedInput(size_t firstSize, size_t blockSize, size_t lastSize, BufferedTransformation *attachment)
    00298         : Filter(attachment), m_firstSize(firstSize), m_blockSize(blockSize), m_lastSize(lastSize)
    00299         , m_firstInputDone(false)
    00300 {
    00301         if (m_firstSize < 0 || m_blockSize < 1 || m_lastSize < 0)
    00302                 throw InvalidArgument("FilterWithBufferedInput: invalid buffer size");
    00303
    00304         m_queue.ResetQueue(1, m_firstSize);
    00305 }
    00306
    00307 void FilterWithBufferedInput::IsolatedInitialize(const NameValuePairs &parameters)
    00308 {
    00309         InitializeDerivedAndReturnNewSizes(parameters, m_firstSize, m_blockSize, m_lastSize);
    00310         if (m_firstSize < 0 || m_blockSize < 1 || m_lastSize < 0)
    00311                 throw InvalidArgument("FilterWithBufferedInput: invalid buffer size");
    00312         m_queue.ResetQueue(1, m_firstSize);
    00313         m_firstInputDone = false;
    00314 }
    00315
    00316 bool FilterWithBufferedInput::IsolatedFlush(bool hardFlush, bool blocking)
    00317 {
    00318         if (!blocking)
    00319                 throw BlockingInputOnly("FilterWithBufferedInput");
    00320
    00321         if (hardFlush)
    00322                 ForceNextPut();
    00323         FlushDerived();
    00324
    00325         return false;
    00326 }
    00327
    00328 size_t FilterWithBufferedInput::PutMaybeModifiable(byte *inString, size_t length, int messageEnd, bool blocking, bool modifiable)
    00329 {
    00330         if (!blocking)
    00331                 throw BlockingInputOnly("FilterWithBufferedInput");
    00332
    00333         if (length != 0)
    00334         {
    00335                 size_t newLength = m_queue.CurrentSize() + length;
    00336
    00337                 if (!m_firstInputDone && newLength >= m_firstSize)
    00338                 {
    00339                         size_t len = m_firstSize - m_queue.CurrentSize();
    00340                         m_queue.Put(inString, len);
    00341                         FirstPut(m_queue.GetContigousBlocks(m_firstSize));
    00342                         assert(m_queue.CurrentSize() == 0);
    00343                         m_queue.ResetQueue(m_blockSize, (2*m_blockSize+m_lastSize-2)/m_blockSize);
    00344
    00345                         inString += len;
    00346                         newLength -= m_firstSize;
    00347                         m_firstInputDone = true;
    00348                 }
    00349
    00350                 if (m_firstInputDone)
    00351                 {
    00352                         if (m_blockSize == 1)
    00353                         {
    00354                                 while (newLength > m_lastSize && m_queue.CurrentSize() > 0)
    00355                                 {
    00356                                         size_t len = newLength - m_lastSize;
    00357                                         byte *ptr = m_queue.GetContigousBlocks(len);
    00358                                         NextPutModifiable(ptr, len);
    00359                                         newLength -= len;
    00360                                 }
    00361
    00362                                 if (newLength > m_lastSize)
    00363                                 {
    00364                                         size_t len = newLength - m_lastSize;
    00365                                         NextPutMaybeModifiable(inString, len, modifiable);
    00366                                         inString += len;
    00367                                         newLength -= len;
    00368                                 }
    00369                         }
    00370                         else
    00371                         {
    00372                                 while (newLength >= m_blockSize + m_lastSize && m_queue.CurrentSize() >= m_blockSize)
    00373                                 {
    00374                                         NextPutModifiable(m_queue.GetBlock(), m_blockSize);
    00375                                         newLength -= m_blockSize;
    00376                                 }
    00377
    00378                                 if (newLength >= m_blockSize + m_lastSize && m_queue.CurrentSize() > 0)
    00379                                 {
    00380                                         assert(m_queue.CurrentSize() < m_blockSize);
    00381                                         size_t len = m_blockSize - m_queue.CurrentSize();
    00382                                         m_queue.Put(inString, len);
    00383                                         inString += len;
    00384                                         NextPutModifiable(m_queue.GetBlock(), m_blockSize);
    00385                                         newLength -= m_blockSize;
    00386                                 }
    00387
    00388                                 if (newLength >= m_blockSize + m_lastSize)
    00389                                 {
    00390                                         size_t len = RoundDownToMultipleOf(newLength - m_lastSize, m_blockSize);
    00391                                         NextPutMaybeModifiable(inString, len, modifiable);
    00392                                         inString += len;
    00393                                         newLength -= len;
    00394                                 }
    00395                         }
    00396                 }
    00397
    00398                 m_queue.Put(inString, newLength - m_queue.CurrentSize());
    00399         }
    00400
    00401         if (messageEnd)
    00402         {
    00403                 if (!m_firstInputDone && m_firstSize==0)
    00404                         FirstPut(NULL);
    00405
    00406                 SecByteBlock temp(m_queue.CurrentSize());
    00407                 m_queue.GetAll(temp);
    00408                 LastPut(temp, temp.size());
    00409
    00410                 m_firstInputDone = false;
    00411                 m_queue.ResetQueue(1, m_firstSize);
    00412
    00413                 Output(1, NULL, 0, messageEnd, blocking);
    00414         }
    00415         return 0;
    00416 }
    00417
    00418 void FilterWithBufferedInput::ForceNextPut()
    00419 {
    00420         if (!m_firstInputDone)
    00421                 return;
    00422
    00423         if (m_blockSize > 1)
    00424         {
    00425                 while (m_queue.CurrentSize() >= m_blockSize)
    00426                         NextPutModifiable(m_queue.GetBlock(), m_blockSize);
    00427         }
    00428         else
    00429         {
    00430                 size_t len;
    00431                 while ((len = m_queue.CurrentSize()) > 0)
    00432                         NextPutModifiable(m_queue.GetContigousBlocks(len), len);
    00433         }
    00434 }
    00435
    00436 void FilterWithBufferedInput::NextPutMultiple(const byte *inString, size_t length)
    00437 {
    00438         assert(m_blockSize > 1);        // m_blockSize = 1 should always override this function
    00439         while (length > 0)
    00440         {
    00441                 assert(length >= m_blockSize);
    00442                 NextPutSingle(inString);
    00443                 inString += m_blockSize;
    00444                 length -= m_blockSize;
    00445         }
    00446 }
    00447
    00448 // *************************************************************
    00449
    00450 void Redirector::Initialize(const NameValuePairs &parameters, int propagation)
    00451 {
    00452         m_target = parameters.GetValueWithDefault("RedirectionTargetPointer", (BufferedTransformation*)NULL);
    00453         m_behavior = parameters.GetIntValueWithDefault("RedirectionBehavior", PASS_EVERYTHING);
    00454
    00455         if (m_target && GetPassSignals())
    00456                 m_target->Initialize(parameters, propagation);
    00457 }
    00458
    00459 // *************************************************************
    00460
    00461 ProxyFilter::ProxyFilter(BufferedTransformation *filter, size_t firstSize, size_t lastSize, BufferedTransformation *attachment)
    00462         : FilterWithBufferedInput(firstSize, 1, lastSize, attachment), m_filter(filter)
    00463 {
    00464         if (m_filter.get())
    00465                 m_filter->Attach(new OutputProxy(*this, false));
    00466 }
    00467
    00468 bool ProxyFilter::IsolatedFlush(bool hardFlush, bool blocking)
    00469 {
    00470         return m_filter.get() ? m_filter->Flush(hardFlush, -1, blocking) : false;
    00471 }
    00472
    00473 void ProxyFilter::SetFilter(Filter *filter)
    00474 {
    00475         m_filter.reset(filter);
    00476         if (filter)
    00477         {
    00478                 OutputProxy *proxy;
    00479                 std::auto_ptr<OutputProxy> temp(proxy = new OutputProxy(*this, false));
    00480                 m_filter->TransferAllTo(*proxy);
    00481                 m_filter->Attach(temp.release());
    00482         }
    00483 }
    00484
    00485 void ProxyFilter::NextPutMultiple(const byte *s, size_t len)
    00486 {
    00487         if (m_filter.get())
    00488                 m_filter->Put(s, len);
    00489 }
    00490
    00491 void ProxyFilter::NextPutModifiable(byte *s, size_t len)
    00492 {
    00493         if (m_filter.get())
    00494                 m_filter->PutModifiable(s, len);
    00495 }
    00496
    00497 // *************************************************************
    00498
    00499 size_t ArraySink::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
    00500 {
    00501         memcpy(m_buf+m_total, begin, STDMIN(length, SaturatingSubtract(m_size, m_total)));
    00502         m_total += length;
    00503         return 0;
    00504 }
    00505
    00506 byte * ArraySink::CreatePutSpace(size_t &size)
    00507 {
    00508         size = SaturatingSubtract(m_size, m_total);
    00509         return m_buf + m_total;
    00510 }
    00511
    00512 void ArraySink::IsolatedInitialize(const NameValuePairs &parameters)
    00513 {
    00514         ByteArrayParameter array;
    00515         if (!parameters.GetValue(Name::OutputBuffer(), array))
    00516                 throw InvalidArgument("ArraySink: missing OutputBuffer argument");
    00517         m_buf = array.begin();
    00518         m_size = array.size();
    00519         m_total = 0;
    00520 }
    00521
    00522 size_t ArrayXorSink::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
    00523 {
    00524         xorbuf(m_buf+m_total, begin, STDMIN(length, SaturatingSubtract(m_size, m_total)));
    00525         m_total += length;
    00526         return 0;
    00527 }
    00528
    00529 // *************************************************************
    00530
    00531 size_t StreamTransformationFilter::LastBlockSize(StreamTransformation &c, BlockPaddingScheme padding)
    00532 {
    00533         if (c.MinLastBlockSize() > 0)
    00534                 return c.MinLastBlockSize();
    00535         else if (c.MandatoryBlockSize() > 1 && !c.IsForwardTransformation() && padding != NO_PADDING && padding != ZEROS_PADDING)
    00536                 return c.MandatoryBlockSize();
    00537         else
    00538                 return 0;
    00539 }
    00540
    00541 StreamTransformationFilter::StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment, BlockPaddingScheme padding)
    00542    : FilterWithBufferedInput(0, c.MandatoryBlockSize(), LastBlockSize(c, padding), attachment)
    00543         , m_cipher(c)
    00544 {
    00545         assert(c.MinLastBlockSize() == 0 || c.MinLastBlockSize() > c.MandatoryBlockSize());
    00546
    00547         bool isBlockCipher = (c.MandatoryBlockSize() > 1 && c.MinLastBlockSize() == 0);
    00548
    00549         if (padding == DEFAULT_PADDING)
    00550         {
    00551                 if (isBlockCipher)
    00552                         m_padding = PKCS_PADDING;
    00553                 else
    00554                         m_padding = NO_PADDING;
    00555         }
    00556         else
    00557                 m_padding = padding;
    00558
    00559         if (!isBlockCipher && (m_padding == PKCS_PADDING || m_padding == ONE_AND_ZEROS_PADDING))
    00560                 throw InvalidArgument("StreamTransformationFilter: PKCS_PADDING and ONE_AND_ZEROS_PADDING cannot be used with " + c.AlgorithmName());
    00561 }
    00562
    00563 void StreamTransformationFilter::FirstPut(const byte *inString)
    00564 {
    00565         m_optimalBufferSize = m_cipher.OptimalBlockSize();
    00566         m_optimalBufferSize = (unsigned int)STDMAX(m_optimalBufferSize, RoundDownToMultipleOf(4096U, m_optimalBufferSize));
    00567 }
    00568
    00569 void StreamTransformationFilter::NextPutMultiple(const byte *inString, size_t length)
    00570 {
    00571         if (!length)
    00572                 return;
    00573
    00574         size_t s = m_cipher.MandatoryBlockSize();
    00575
    00576         do
    00577         {
    00578                 size_t len = m_optimalBufferSize;
    00579                 byte *space = HelpCreatePutSpace(*AttachedTransformation(), NULL_CHANNEL, s, length, len);
    00580                 if (len < length)
    00581                 {
    00582                         if (len == m_optimalBufferSize)
    00583                                 len -= m_cipher.GetOptimalBlockSizeUsed();
    00584                         len = RoundDownToMultipleOf(len, s);
    00585                 }
    00586                 else
    00587                         len = length;
    00588                 m_cipher.ProcessString(space, inString, len);
    00589                 AttachedTransformation()->PutModifiable(space, len);
    00590                 inString += len;
    00591                 length -= len;
    00592         }
    00593         while (length > 0);
    00594 }
    00595
    00596 void StreamTransformationFilter::NextPutModifiable(byte *inString, size_t length)
    00597 {
    00598         m_cipher.ProcessString(inString, length);
    00599         AttachedTransformation()->PutModifiable(inString, length);
    00600 }
    00601
    00602 void StreamTransformationFilter::LastPut(const byte *inString, size_t length)
    00603 {
    00604         byte *space = NULL;
    00605
    00606         switch (m_padding)
    00607         {
    00608         case NO_PADDING:
    00609         case ZEROS_PADDING:
    00610                 if (length > 0)
    00611                 {
    00612                         size_t minLastBlockSize = m_cipher.MinLastBlockSize();
    00613                         bool isForwardTransformation = m_cipher.IsForwardTransformation();
    00614
    00615                         if (isForwardTransformation && m_padding == ZEROS_PADDING && (minLastBlockSize == 0 || length < minLastBlockSize))
    00616                         {
    00617                                 // do padding
    00618                                 size_t blockSize = STDMAX(minLastBlockSize, (size_t)m_cipher.MandatoryBlockSize());
    00619                                 space = HelpCreatePutSpace(*AttachedTransformation(), NULL_CHANNEL, blockSize);
    00620                                 memcpy(space, inString, length);
    00621                                 memset(space + length, 0, blockSize - length);
    00622                                 m_cipher.ProcessLastBlock(space, space, blockSize);
    00623                                 AttachedTransformation()->Put(space, blockSize);
    00624                         }
    00625                         else
    00626                         {
    00627                                 if (minLastBlockSize == 0)
    00628                                 {
    00629                                         if (isForwardTransformation)
    00630                                                 throw InvalidDataFormat("StreamTransformationFilter: plaintext length is not a multiple of block size and NO_PADDING is specified");
    00631                                         else
    00632                                                 throw InvalidCiphertext("StreamTransformationFilter: ciphertext length is not a multiple of block size");
    00633                                 }
    00634
    00635                                 space = HelpCreatePutSpace(*AttachedTransformation(), NULL_CHANNEL, length, m_optimalBufferSize);
    00636                                 m_cipher.ProcessLastBlock(space, inString, length);
    00637                                 AttachedTransformation()->Put(space, length);
    00638                         }
    00639                 }
    00640                 break;
    00641
    00642         case PKCS_PADDING:
    00643         case ONE_AND_ZEROS_PADDING:
    00644                 unsigned int s;
    00645                 s = m_cipher.MandatoryBlockSize();
    00646                 assert(s > 1);
    00647                 space = HelpCreatePutSpace(*AttachedTransformation(), NULL_CHANNEL, s, m_optimalBufferSize);
    00648                 if (m_cipher.IsForwardTransformation())
    00649                 {
    00650                         assert(length < s);
    00651                         memcpy(space, inString, length);
    00652                         if (m_padding == PKCS_PADDING)
    00653                         {
    00654                                 assert(s < 256);
    00655                                 byte pad = byte(s-length);
    00656                                 memset(space+length, pad, s-length);
    00657                         }
    00658                         else
    00659                         {
    00660                                 space[length] = 0x80;
    00661                                 memset(space+length+1, 0, s-length-1);
    00662                         }
    00663                         m_cipher.ProcessData(space, space, s);
    00664                         AttachedTransformation()->Put(space, s);
    00665                 }
    00666                 else
    00667                 {
    00668                         if (length != s)
    00669                                 throw InvalidCiphertext("StreamTransformationFilter: ciphertext length is not a multiple of block size");
    00670                         m_cipher.ProcessData(space, inString, s);
    00671                         if (m_padding == PKCS_PADDING)
    00672                         {
    00673                                 byte pad = space[s-1];
    00674                                 if (pad < 1 || pad > s || std::find_if(space+s-pad, space+s, std::bind2nd(std::not_equal_to<byte>(), pad)) != space+s)
    00675                                         throw InvalidCiphertext("StreamTransformationFilter: invalid PKCS #7 block padding found");
    00676                                 length = s-pad;
    00677                         }
    00678                         else
    00679                         {
    00680                                 while (length > 1 && space[length-1] == 0)
    00681                                         --length;
    00682                                 if (space[--length] != 0x80)
    00683                                         throw InvalidCiphertext("StreamTransformationFilter: invalid ones-and-zeros padding found");
    00684                         }
    00685                         AttachedTransformation()->Put(space, length);
    00686                 }
    00687                 break;
    00688
    00689         default:
    00690                 assert(false);
    00691         }
    00692 }
    00693
    00694 // *************************************************************
    00695
    00696 void HashFilter::IsolatedInitialize(const NameValuePairs &parameters)
    00697 {
    00698         m_putMessage = parameters.GetValueWithDefault(Name::PutMessage(), false);
    00699         m_truncatedDigestSize = parameters.GetIntValueWithDefault(Name::TruncatedDigestSize(), -1);
    00700         m_hashModule.Restart();
    00701 }
    00702
    00703 size_t HashFilter::Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
    00704 {
    00705         FILTER_BEGIN;
    00706         m_hashModule.Update(inString, length);
    00707         if (m_putMessage)
    00708                 FILTER_OUTPUT(1, inString, length, 0);
    00709         if (messageEnd)
    00710         {
    00711                 {
    00712                         size_t size;
    00713                         m_digestSize = m_hashModule.DigestSize();
    00714                         if (m_truncatedDigestSize >= 0 && (unsigned int)m_truncatedDigestSize < m_digestSize)
    00715                                 m_digestSize = m_truncatedDigestSize;
    00716                         m_space = HelpCreatePutSpace(*AttachedTransformation(), NULL_CHANNEL, m_digestSize, m_digestSize, size = m_digestSize);
    00717                         m_hashModule.TruncatedFinal(m_space, m_digestSize);
    00718                 }
    00719                 FILTER_OUTPUT(2, m_space, m_digestSize, messageEnd);
    00720         }
    00721         FILTER_END_NO_MESSAGE_END;
    00722 }
    00723
    00724 // *************************************************************
    00725
    00726 HashVerificationFilter::HashVerificationFilter(HashTransformation &hm, BufferedTransformation *attachment, word32 flags)
    00727         : FilterWithBufferedInput(attachment)
    00728         , m_hashModule(hm)
    00729 {
    00730         IsolatedInitialize(MakeParameters(Name::HashVerificationFilterFlags(), flags));
    00731 }
    00732
    00733 void HashVerificationFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
    00734 {
    00735         m_flags = parameters.GetValueWithDefault(Name::HashVerificationFilterFlags(), (word32)DEFAULT_FLAGS);
    00736         m_hashModule.Restart();
    00737         size_t size = m_hashModule.DigestSize();
    00738         m_verified = false;
    00739         firstSize = m_flags & HASH_AT_BEGIN ? size : 0;
    00740         blockSize = 1;
    00741         lastSize = m_flags & HASH_AT_BEGIN ? 0 : size;
    00742 }
    00743
    00744 void HashVerificationFilter::FirstPut(const byte *inString)
    00745 {
    00746         if (m_flags & HASH_AT_BEGIN)
    00747         {
    00748                 m_expectedHash.New(m_hashModule.DigestSize());
    00749                 memcpy(m_expectedHash, inString, m_expectedHash.size());
    00750                 if (m_flags & PUT_HASH)
    00751                         AttachedTransformation()->Put(inString, m_expectedHash.size());
    00752         }
    00753 }
    00754
    00755 void HashVerificationFilter::NextPutMultiple(const byte *inString, size_t length)
    00756 {
    00757         m_hashModule.Update(inString, length);
    00758         if (m_flags & PUT_MESSAGE)
    00759                 AttachedTransformation()->Put(inString, length);
    00760 }
    00761
    00762 void HashVerificationFilter::LastPut(const byte *inString, size_t length)
    00763 {
    00764         if (m_flags & HASH_AT_BEGIN)
    00765         {
    00766                 assert(length == 0);
    00767                 m_verified = m_hashModule.Verify(m_expectedHash);
    00768         }
    00769         else
    00770         {
    00771                 m_verified = (length==m_hashModule.DigestSize() && m_hashModule.Verify(inString));
    00772                 if (m_flags & PUT_HASH)
    00773                         AttachedTransformation()->Put(inString, length);
    00774         }
    00775
    00776         if (m_flags & PUT_RESULT)
    00777                 AttachedTransformation()->Put(m_verified);
    00778
    00779         if ((m_flags & THROW_EXCEPTION) && !m_verified)
    00780                 throw HashVerificationFailed();
    00781 }
    00782
    00783 // *************************************************************
    00784
    00785 void SignerFilter::IsolatedInitialize(const NameValuePairs &parameters)
    00786 {
    00787         m_putMessage = parameters.GetValueWithDefault(Name::PutMessage(), false);
    00788         m_messageAccumulator.reset(m_signer.NewSignatureAccumulator(m_rng));
    00789 }
    00790
    00791 size_t SignerFilter::Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
    00792 {
    00793         FILTER_BEGIN;
    00794         m_messageAccumulator->Update(inString, length);
    00795         if (m_putMessage)
    00796                 FILTER_OUTPUT(1, inString, length, 0);
    00797         if (messageEnd)
    00798         {
    00799                 m_buf.New(m_signer.SignatureLength());
    00800                 m_signer.Sign(m_rng, m_messageAccumulator.release(), m_buf);
    00801                 FILTER_OUTPUT(2, m_buf, m_buf.size(), messageEnd);
    00802                 m_messageAccumulator.reset(m_signer.NewSignatureAccumulator(m_rng));
    00803         }
    00804         FILTER_END_NO_MESSAGE_END;
    00805 }
    00806
    00807 SignatureVerificationFilter::SignatureVerificationFilter(const PK_Verifier &verifier, BufferedTransformation *attachment, word32 flags)
    00808         : FilterWithBufferedInput(attachment)
    00809         , m_verifier(verifier)
    00810 {
    00811         IsolatedInitialize(MakeParameters(Name::SignatureVerificationFilterFlags(), flags));
    00812 }
    00813
    00814 void SignatureVerificationFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
    00815 {
    00816         m_flags = parameters.GetValueWithDefault(Name::SignatureVerificationFilterFlags(), (word32)DEFAULT_FLAGS);
    00817         m_messageAccumulator.reset(m_verifier.NewVerificationAccumulator());
    00818         size_t size = m_verifier.SignatureLength();
    00819         assert(size != 0);      // TODO: handle recoverable signature scheme
    00820         m_verified = false;
    00821         firstSize = m_flags & SIGNATURE_AT_BEGIN ? size : 0;
    00822         blockSize = 1;
    00823         lastSize = m_flags & SIGNATURE_AT_BEGIN ? 0 : size;
    00824 }
    00825
    00826 void SignatureVerificationFilter::FirstPut(const byte *inString)
    00827 {
    00828         if (m_flags & SIGNATURE_AT_BEGIN)
    00829         {
    00830                 if (m_verifier.SignatureUpfront())
    00831                         m_verifier.InputSignature(*m_messageAccumulator, inString, m_verifier.SignatureLength());
    00832                 else
    00833                 {
    00834                         m_signature.New(m_verifier.SignatureLength());
    00835                         memcpy(m_signature, inString, m_signature.size());
    00836                 }
    00837
    00838                 if (m_flags & PUT_SIGNATURE)
    00839                         AttachedTransformation()->Put(inString, m_signature.size());
    00840         }
    00841         else
    00842         {
    00843                 assert(!m_verifier.SignatureUpfront());
    00844         }
    00845 }
    00846
    00847 void SignatureVerificationFilter::NextPutMultiple(const byte *inString, size_t length)
    00848 {
    00849         m_messageAccumulator->Update(inString, length);
    00850         if (m_flags & PUT_MESSAGE)
    00851                 AttachedTransformation()->Put(inString, length);
    00852 }
    00853
    00854 void SignatureVerificationFilter::LastPut(const byte *inString, size_t length)
    00855 {
    00856         if (m_flags & SIGNATURE_AT_BEGIN)
    00857         {
    00858                 assert(length == 0);
    00859                 m_verifier.InputSignature(*m_messageAccumulator, m_signature, m_signature.size());
    00860                 m_verified = m_verifier.VerifyAndRestart(*m_messageAccumulator);
    00861         }
    00862         else
    00863         {
    00864                 m_verifier.InputSignature(*m_messageAccumulator, inString, length);
    00865                 m_verified = m_verifier.VerifyAndRestart(*m_messageAccumulator);
    00866                 if (m_flags & PUT_SIGNATURE)
    00867                         AttachedTransformation()->Put(inString, length);
    00868         }
    00869
    00870         if (m_flags & PUT_RESULT)
    00871                 AttachedTransformation()->Put(m_verified);
    00872
    00873         if ((m_flags & THROW_EXCEPTION) && !m_verified)
    00874                 throw SignatureVerificationFailed();
    00875 }
    00876
    00877 // *************************************************************
    00878
    00879 size_t Source::PumpAll2(bool blocking)
    00880 {
    00881         unsigned int messageCount = UINT_MAX;
    00882         do {
    00883                 RETURN_IF_NONZERO(PumpMessages2(messageCount, blocking));
    00884         } while(messageCount == UINT_MAX);
    00885
    00886         return 0;
    00887 }
    00888
    00889 bool Store::GetNextMessage()
    00890 {
    00891         if (!m_messageEnd && !AnyRetrievable())
    00892         {
    00893                 m_messageEnd=true;
    00894                 return true;
    00895         }
    00896         else
    00897                 return false;
    00898 }
    00899
    00900 unsigned int Store::CopyMessagesTo(BufferedTransformation &target, unsigned int count, const std::string &channel) const
    00901 {
    00902         if (m_messageEnd || count == 0)
    00903                 return 0;
    00904         else
    00905         {
    00906                 CopyTo(target, ULONG_MAX, channel);
    00907                 if (GetAutoSignalPropagation())
    00908                         target.ChannelMessageEnd(channel, GetAutoSignalPropagation()-1);
    00909                 return 1;
    00910         }
    00911 }
    00912
    00913 void StringStore::StoreInitialize(const NameValuePairs &parameters)
    00914 {
    00915         ConstByteArrayParameter array;
    00916         if (!parameters.GetValue(Name::InputBuffer(), array))
    00917                 throw InvalidArgument("StringStore: missing InputBuffer argument");
    00918         m_store = array.begin();
    00919         m_length = array.size();
    00920         m_count = 0;
    00921 }
    00922
    00923 size_t StringStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
    00924 {
    00925         lword position = 0;
    00926         size_t blockedBytes = CopyRangeTo2(target, position, transferBytes, channel, blocking);
    00927         m_count += (size_t)position;
    00928         transferBytes = position;
    00929         return blockedBytes;
    00930 }
    00931
    00932 size_t StringStore::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
    00933 {
    00934         size_t i = UnsignedMin(m_length, m_count+begin);
    00935         size_t len = UnsignedMin(m_length-i, end-begin);
    00936         size_t blockedBytes = target.ChannelPut2(channel, m_store+i, len, 0, blocking);
    00937         if (!blockedBytes)
    00938                 begin += len;
    00939         return blockedBytes;
    00940 }
    00941
    00942 void RandomNumberStore::StoreInitialize(const NameValuePairs &parameters)
    00943 {
    00944         parameters.GetRequiredParameter("RandomNumberStore", "RandomNumberGeneratorPointer", m_rng);
    00945         int length;
    00946         parameters.GetRequiredIntParameter("RandomNumberStore", "RandomNumberStoreSize", length);
    00947         m_length = length;
    00948 }
    00949
    00950 size_t RandomNumberStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
    00951 {
    00952         if (!blocking)
    00953                 throw NotImplemented("RandomNumberStore: nonblocking transfer is not implemented by this object");
    00954
    00955         lword transferMax = transferBytes;
    00956         for (transferBytes = 0; transferBytes<transferMax && m_count < (unsigned int)m_length; ++transferBytes, ++m_count)
    00957                 target.ChannelPut(channel, m_rng->GenerateByte());
    00958         return 0;
    00959 }
    00960
    00961 size_t NullStore::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
    00962 {
    00963         static const byte nullBytes[128] = {0};
    00964         while (begin < end)
    00965         {
    00966                 size_t len = (size_t)STDMIN(end-begin, lword(128));
    00967                 size_t blockedBytes = target.ChannelPut2(channel, nullBytes, len, 0, blocking);
    00968                 if (blockedBytes)
    00969                         return blockedBytes;
    00970                 begin += len;
    00971         }
    00972         return 0;
    00973 }
    00974
    00975 size_t NullStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
    00976 {
    00977         lword begin = 0;
    00978         size_t blockedBytes = NullStore::CopyRangeTo2(target, begin, transferBytes, channel, blocking);
    00979         transferBytes = begin;
    00980         m_size -= begin;
    00981         return blockedBytes;
    00982 }
    00983
    00984 NAMESPACE_END
    00985
    00986 #endif