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