00001
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 ¶meters, 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 ¶meters, 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 ¶meters)
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);
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 ¶meters, 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 void RandomNumberSink::IsolatedInitialize(const NameValuePairs ¶meters)
00500 {
00501 parameters.GetRequiredParameter("RandomNumberSink", "RandomNumberGeneratorPointer", m_rng);
00502 }
00503
00504 size_t RandomNumberSink::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
00505 {
00506 m_rng->IncorporateEntropy(begin, length);
00507 return 0;
00508 }
00509
00510 size_t ArraySink::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
00511 {
00512 if (m_buf+m_total != begin)
00513 memcpy(m_buf+m_total, begin, STDMIN(length, SaturatingSubtract(m_size, m_total)));
00514 m_total += length;
00515 return 0;
00516 }
00517
00518 byte * ArraySink::CreatePutSpace(size_t &size)
00519 {
00520 size = SaturatingSubtract(m_size, m_total);
00521 return m_buf + m_total;
00522 }
00523
00524 void ArraySink::IsolatedInitialize(const NameValuePairs ¶meters)
00525 {
00526 ByteArrayParameter array;
00527 if (!parameters.GetValue(Name::OutputBuffer(), array))
00528 throw InvalidArgument("ArraySink: missing OutputBuffer argument");
00529 m_buf = array.begin();
00530 m_size = array.size();
00531 m_total = 0;
00532 }
00533
00534 size_t ArrayXorSink::Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
00535 {
00536 xorbuf(m_buf+m_total, begin, STDMIN(length, SaturatingSubtract(m_size, m_total)));
00537 m_total += length;
00538 return 0;
00539 }
00540
00541
00542
00543 size_t StreamTransformationFilter::LastBlockSize(StreamTransformation &c, BlockPaddingScheme padding)
00544 {
00545 if (c.MinLastBlockSize() > 0)
00546 return c.MinLastBlockSize();
00547 else if (c.MandatoryBlockSize() > 1 && !c.IsForwardTransformation() && padding != NO_PADDING && padding != ZEROS_PADDING)
00548 return c.MandatoryBlockSize();
00549 else
00550 return 0;
00551 }
00552
00553 StreamTransformationFilter::StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment, BlockPaddingScheme padding)
00554 : FilterWithBufferedInput(0, c.MandatoryBlockSize(), LastBlockSize(c, padding), attachment)
00555 , m_cipher(c)
00556 {
00557 assert(c.MinLastBlockSize() == 0 || c.MinLastBlockSize() > c.MandatoryBlockSize());
00558
00559 bool isBlockCipher = (c.MandatoryBlockSize() > 1 && c.MinLastBlockSize() == 0);
00560
00561 if (padding == DEFAULT_PADDING)
00562 {
00563 if (isBlockCipher)
00564 m_padding = PKCS_PADDING;
00565 else
00566 m_padding = NO_PADDING;
00567 }
00568 else
00569 m_padding = padding;
00570
00571 if (!isBlockCipher && (m_padding == PKCS_PADDING || m_padding == ONE_AND_ZEROS_PADDING))
00572 throw InvalidArgument("StreamTransformationFilter: PKCS_PADDING and ONE_AND_ZEROS_PADDING cannot be used with " + c.AlgorithmName());
00573 }
00574
00575 void StreamTransformationFilter::FirstPut(const byte *inString)
00576 {
00577 m_optimalBufferSize = m_cipher.OptimalBlockSize();
00578 m_optimalBufferSize = (unsigned int)STDMAX(m_optimalBufferSize, RoundDownToMultipleOf(4096U, m_optimalBufferSize));
00579 }
00580
00581 void StreamTransformationFilter::NextPutMultiple(const byte *inString, size_t length)
00582 {
00583 if (!length)
00584 return;
00585
00586 size_t s = m_cipher.MandatoryBlockSize();
00587
00588 do
00589 {
00590 size_t len = m_optimalBufferSize;
00591 byte *space = HelpCreatePutSpace(*AttachedTransformation(), NULL_CHANNEL, s, length, len);
00592 if (len < length)
00593 {
00594 if (len == m_optimalBufferSize)
00595 len -= m_cipher.GetOptimalBlockSizeUsed();
00596 len = RoundDownToMultipleOf(len, s);
00597 }
00598 else
00599 len = length;
00600 m_cipher.ProcessString(space, inString, len);
00601 AttachedTransformation()->PutModifiable(space, len);
00602 inString += len;
00603 length -= len;
00604 }
00605 while (length > 0);
00606 }
00607
00608 void StreamTransformationFilter::NextPutModifiable(byte *inString, size_t length)
00609 {
00610 m_cipher.ProcessString(inString, length);
00611 AttachedTransformation()->PutModifiable(inString, length);
00612 }
00613
00614 void StreamTransformationFilter::LastPut(const byte *inString, size_t length)
00615 {
00616 byte *space = NULL;
00617
00618 switch (m_padding)
00619 {
00620 case NO_PADDING:
00621 case ZEROS_PADDING:
00622 if (length > 0)
00623 {
00624 size_t minLastBlockSize = m_cipher.MinLastBlockSize();
00625 bool isForwardTransformation = m_cipher.IsForwardTransformation();
00626
00627 if (isForwardTransformation && m_padding == ZEROS_PADDING && (minLastBlockSize == 0 || length < minLastBlockSize))
00628 {
00629
00630 size_t blockSize = STDMAX(minLastBlockSize, (size_t)m_cipher.MandatoryBlockSize());
00631 space = HelpCreatePutSpace(*AttachedTransformation(), NULL_CHANNEL, blockSize);
00632 memcpy(space, inString, length);
00633 memset(space + length, 0, blockSize - length);
00634 m_cipher.ProcessLastBlock(space, space, blockSize);
00635 AttachedTransformation()->Put(space, blockSize);
00636 }
00637 else
00638 {
00639 if (minLastBlockSize == 0)
00640 {
00641 if (isForwardTransformation)
00642 throw InvalidDataFormat("StreamTransformationFilter: plaintext length is not a multiple of block size and NO_PADDING is specified");
00643 else
00644 throw InvalidCiphertext("StreamTransformationFilter: ciphertext length is not a multiple of block size");
00645 }
00646
00647 space = HelpCreatePutSpace(*AttachedTransformation(), NULL_CHANNEL, length, m_optimalBufferSize);
00648 m_cipher.ProcessLastBlock(space, inString, length);
00649 AttachedTransformation()->Put(space, length);
00650 }
00651 }
00652 break;
00653
00654 case PKCS_PADDING:
00655 case ONE_AND_ZEROS_PADDING:
00656 unsigned int s;
00657 s = m_cipher.MandatoryBlockSize();
00658 assert(s > 1);
00659 space = HelpCreatePutSpace(*AttachedTransformation(), NULL_CHANNEL, s, m_optimalBufferSize);
00660 if (m_cipher.IsForwardTransformation())
00661 {
00662 assert(length < s);
00663 memcpy(space, inString, length);
00664 if (m_padding == PKCS_PADDING)
00665 {
00666 assert(s < 256);
00667 byte pad = byte(s-length);
00668 memset(space+length, pad, s-length);
00669 }
00670 else
00671 {
00672 space[length] = 0x80;
00673 memset(space+length+1, 0, s-length-1);
00674 }
00675 m_cipher.ProcessData(space, space, s);
00676 AttachedTransformation()->Put(space, s);
00677 }
00678 else
00679 {
00680 if (length != s)
00681 throw InvalidCiphertext("StreamTransformationFilter: ciphertext length is not a multiple of block size");
00682 m_cipher.ProcessData(space, inString, s);
00683 if (m_padding == PKCS_PADDING)
00684 {
00685 byte pad = space[s-1];
00686 if (pad < 1 || pad > s || std::find_if(space+s-pad, space+s, std::bind2nd(std::not_equal_to<byte>(), pad)) != space+s)
00687 throw InvalidCiphertext("StreamTransformationFilter: invalid PKCS #7 block padding found");
00688 length = s-pad;
00689 }
00690 else
00691 {
00692 while (length > 1 && space[length-1] == 0)
00693 --length;
00694 if (space[--length] != 0x80)
00695 throw InvalidCiphertext("StreamTransformationFilter: invalid ones-and-zeros padding found");
00696 }
00697 AttachedTransformation()->Put(space, length);
00698 }
00699 break;
00700
00701 default:
00702 assert(false);
00703 }
00704 }
00705
00706
00707
00708 void HashFilter::IsolatedInitialize(const NameValuePairs ¶meters)
00709 {
00710 m_putMessage = parameters.GetValueWithDefault(Name::PutMessage(), false);
00711 m_truncatedDigestSize = parameters.GetIntValueWithDefault(Name::TruncatedDigestSize(), -1);
00712 m_hashModule.Restart();
00713 }
00714
00715 size_t HashFilter::Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
00716 {
00717 FILTER_BEGIN;
00718 m_hashModule.Update(inString, length);
00719 if (m_putMessage)
00720 FILTER_OUTPUT(1, inString, length, 0);
00721 if (messageEnd)
00722 {
00723 {
00724 size_t size;
00725 m_digestSize = m_hashModule.DigestSize();
00726 if (m_truncatedDigestSize >= 0 && (unsigned int)m_truncatedDigestSize < m_digestSize)
00727 m_digestSize = m_truncatedDigestSize;
00728 m_space = HelpCreatePutSpace(*AttachedTransformation(), NULL_CHANNEL, m_digestSize, m_digestSize, size = m_digestSize);
00729 m_hashModule.TruncatedFinal(m_space, m_digestSize);
00730 }
00731 FILTER_OUTPUT(2, m_space, m_digestSize, messageEnd);
00732 }
00733 FILTER_END_NO_MESSAGE_END;
00734 }
00735
00736
00737
00738 HashVerificationFilter::HashVerificationFilter(HashTransformation &hm, BufferedTransformation *attachment, word32 flags)
00739 : FilterWithBufferedInput(attachment)
00740 , m_hashModule(hm)
00741 {
00742 IsolatedInitialize(MakeParameters(Name::HashVerificationFilterFlags(), flags));
00743 }
00744
00745 void HashVerificationFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
00746 {
00747 m_flags = parameters.GetValueWithDefault(Name::HashVerificationFilterFlags(), (word32)DEFAULT_FLAGS);
00748 m_hashModule.Restart();
00749 size_t size = m_hashModule.DigestSize();
00750 m_verified = false;
00751 firstSize = m_flags & HASH_AT_BEGIN ? size : 0;
00752 blockSize = 1;
00753 lastSize = m_flags & HASH_AT_BEGIN ? 0 : size;
00754 }
00755
00756 void HashVerificationFilter::FirstPut(const byte *inString)
00757 {
00758 if (m_flags & HASH_AT_BEGIN)
00759 {
00760 m_expectedHash.New(m_hashModule.DigestSize());
00761 memcpy(m_expectedHash, inString, m_expectedHash.size());
00762 if (m_flags & PUT_HASH)
00763 AttachedTransformation()->Put(inString, m_expectedHash.size());
00764 }
00765 }
00766
00767 void HashVerificationFilter::NextPutMultiple(const byte *inString, size_t length)
00768 {
00769 m_hashModule.Update(inString, length);
00770 if (m_flags & PUT_MESSAGE)
00771 AttachedTransformation()->Put(inString, length);
00772 }
00773
00774 void HashVerificationFilter::LastPut(const byte *inString, size_t length)
00775 {
00776 if (m_flags & HASH_AT_BEGIN)
00777 {
00778 assert(length == 0);
00779 m_verified = m_hashModule.Verify(m_expectedHash);
00780 }
00781 else
00782 {
00783 m_verified = (length==m_hashModule.DigestSize() && m_hashModule.Verify(inString));
00784 if (m_flags & PUT_HASH)
00785 AttachedTransformation()->Put(inString, length);
00786 }
00787
00788 if (m_flags & PUT_RESULT)
00789 AttachedTransformation()->Put(m_verified);
00790
00791 if ((m_flags & THROW_EXCEPTION) && !m_verified)
00792 throw HashVerificationFailed();
00793 }
00794
00795
00796
00797 void SignerFilter::IsolatedInitialize(const NameValuePairs ¶meters)
00798 {
00799 m_putMessage = parameters.GetValueWithDefault(Name::PutMessage(), false);
00800 m_messageAccumulator.reset(m_signer.NewSignatureAccumulator(m_rng));
00801 }
00802
00803 size_t SignerFilter::Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
00804 {
00805 FILTER_BEGIN;
00806 m_messageAccumulator->Update(inString, length);
00807 if (m_putMessage)
00808 FILTER_OUTPUT(1, inString, length, 0);
00809 if (messageEnd)
00810 {
00811 m_buf.New(m_signer.SignatureLength());
00812 m_signer.Sign(m_rng, m_messageAccumulator.release(), m_buf);
00813 FILTER_OUTPUT(2, m_buf, m_buf.size(), messageEnd);
00814 m_messageAccumulator.reset(m_signer.NewSignatureAccumulator(m_rng));
00815 }
00816 FILTER_END_NO_MESSAGE_END;
00817 }
00818
00819 SignatureVerificationFilter::SignatureVerificationFilter(const PK_Verifier &verifier, BufferedTransformation *attachment, word32 flags)
00820 : FilterWithBufferedInput(attachment)
00821 , m_verifier(verifier)
00822 {
00823 IsolatedInitialize(MakeParameters(Name::SignatureVerificationFilterFlags(), flags));
00824 }
00825
00826 void SignatureVerificationFilter::InitializeDerivedAndReturnNewSizes(const NameValuePairs ¶meters, size_t &firstSize, size_t &blockSize, size_t &lastSize)
00827 {
00828 m_flags = parameters.GetValueWithDefault(Name::SignatureVerificationFilterFlags(), (word32)DEFAULT_FLAGS);
00829 m_messageAccumulator.reset(m_verifier.NewVerificationAccumulator());
00830 size_t size = m_verifier.SignatureLength();
00831 assert(size != 0);
00832 m_verified = false;
00833 firstSize = m_flags & SIGNATURE_AT_BEGIN ? size : 0;
00834 blockSize = 1;
00835 lastSize = m_flags & SIGNATURE_AT_BEGIN ? 0 : size;
00836 }
00837
00838 void SignatureVerificationFilter::FirstPut(const byte *inString)
00839 {
00840 if (m_flags & SIGNATURE_AT_BEGIN)
00841 {
00842 if (m_verifier.SignatureUpfront())
00843 m_verifier.InputSignature(*m_messageAccumulator, inString, m_verifier.SignatureLength());
00844 else
00845 {
00846 m_signature.New(m_verifier.SignatureLength());
00847 memcpy(m_signature, inString, m_signature.size());
00848 }
00849
00850 if (m_flags & PUT_SIGNATURE)
00851 AttachedTransformation()->Put(inString, m_signature.size());
00852 }
00853 else
00854 {
00855 assert(!m_verifier.SignatureUpfront());
00856 }
00857 }
00858
00859 void SignatureVerificationFilter::NextPutMultiple(const byte *inString, size_t length)
00860 {
00861 m_messageAccumulator->Update(inString, length);
00862 if (m_flags & PUT_MESSAGE)
00863 AttachedTransformation()->Put(inString, length);
00864 }
00865
00866 void SignatureVerificationFilter::LastPut(const byte *inString, size_t length)
00867 {
00868 if (m_flags & SIGNATURE_AT_BEGIN)
00869 {
00870 assert(length == 0);
00871 m_verifier.InputSignature(*m_messageAccumulator, m_signature, m_signature.size());
00872 m_verified = m_verifier.VerifyAndRestart(*m_messageAccumulator);
00873 }
00874 else
00875 {
00876 m_verifier.InputSignature(*m_messageAccumulator, inString, length);
00877 m_verified = m_verifier.VerifyAndRestart(*m_messageAccumulator);
00878 if (m_flags & PUT_SIGNATURE)
00879 AttachedTransformation()->Put(inString, length);
00880 }
00881
00882 if (m_flags & PUT_RESULT)
00883 AttachedTransformation()->Put(m_verified);
00884
00885 if ((m_flags & THROW_EXCEPTION) && !m_verified)
00886 throw SignatureVerificationFailed();
00887 }
00888
00889
00890
00891 size_t Source::PumpAll2(bool blocking)
00892 {
00893 unsigned int messageCount = UINT_MAX;
00894 do {
00895 RETURN_IF_NONZERO(PumpMessages2(messageCount, blocking));
00896 } while(messageCount == UINT_MAX);
00897
00898 return 0;
00899 }
00900
00901 bool Store::GetNextMessage()
00902 {
00903 if (!m_messageEnd && !AnyRetrievable())
00904 {
00905 m_messageEnd=true;
00906 return true;
00907 }
00908 else
00909 return false;
00910 }
00911
00912 unsigned int Store::CopyMessagesTo(BufferedTransformation &target, unsigned int count, const std::string &channel) const
00913 {
00914 if (m_messageEnd || count == 0)
00915 return 0;
00916 else
00917 {
00918 CopyTo(target, ULONG_MAX, channel);
00919 if (GetAutoSignalPropagation())
00920 target.ChannelMessageEnd(channel, GetAutoSignalPropagation()-1);
00921 return 1;
00922 }
00923 }
00924
00925 void StringStore::StoreInitialize(const NameValuePairs ¶meters)
00926 {
00927 ConstByteArrayParameter array;
00928 if (!parameters.GetValue(Name::InputBuffer(), array))
00929 throw InvalidArgument("StringStore: missing InputBuffer argument");
00930 m_store = array.begin();
00931 m_length = array.size();
00932 m_count = 0;
00933 }
00934
00935 size_t StringStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
00936 {
00937 lword position = 0;
00938 size_t blockedBytes = CopyRangeTo2(target, position, transferBytes, channel, blocking);
00939 m_count += (size_t)position;
00940 transferBytes = position;
00941 return blockedBytes;
00942 }
00943
00944 size_t StringStore::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
00945 {
00946 size_t i = UnsignedMin(m_length, m_count+begin);
00947 size_t len = UnsignedMin(m_length-i, end-begin);
00948 size_t blockedBytes = target.ChannelPut2(channel, m_store+i, len, 0, blocking);
00949 if (!blockedBytes)
00950 begin += len;
00951 return blockedBytes;
00952 }
00953
00954 void RandomNumberStore::StoreInitialize(const NameValuePairs ¶meters)
00955 {
00956 parameters.GetRequiredParameter("RandomNumberStore", "RandomNumberGeneratorPointer", m_rng);
00957 int length;
00958 parameters.GetRequiredIntParameter("RandomNumberStore", "RandomNumberStoreSize", length);
00959 m_length = length;
00960 }
00961
00962 size_t RandomNumberStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
00963 {
00964 if (!blocking)
00965 throw NotImplemented("RandomNumberStore: nonblocking transfer is not implemented by this object");
00966
00967 transferBytes = UnsignedMin(transferBytes, m_length - m_count);
00968 m_rng->GenerateIntoBufferedTransformation(target, channel, transferBytes);
00969 m_count += transferBytes;
00970
00971 return 0;
00972 }
00973
00974 size_t NullStore::CopyRangeTo2(BufferedTransformation &target, lword &begin, lword end, const std::string &channel, bool blocking) const
00975 {
00976 static const byte nullBytes[128] = {0};
00977 while (begin < end)
00978 {
00979 size_t len = (size_t)STDMIN(end-begin, lword(128));
00980 size_t blockedBytes = target.ChannelPut2(channel, nullBytes, len, 0, blocking);
00981 if (blockedBytes)
00982 return blockedBytes;
00983 begin += len;
00984 }
00985 return 0;
00986 }
00987
00988 size_t NullStore::TransferTo2(BufferedTransformation &target, lword &transferBytes, const std::string &channel, bool blocking)
00989 {
00990 lword begin = 0;
00991 size_t blockedBytes = NullStore::CopyRangeTo2(target, begin, transferBytes, channel, blocking);
00992 transferBytes = begin;
00993 m_size -= begin;
00994 return blockedBytes;
00995 }
00996
00997 NAMESPACE_END
00998
00999 #endif