00001
00002
00003 #include "pch.h"
00004 #include "cryptlib.h"
00005 #include "misc.h"
00006 #include "filters.h"
00007
00008 #include <memory>
00009
00010 NAMESPACE_BEGIN(CryptoPP)
00011
00012 const std::string BufferedTransformation::NULL_CHANNEL;
00013
00014 unsigned int RandomNumberGenerator::GenerateBit()
00015 {
00016 return Parity(GetByte());
00017 }
00018
00019 void RandomNumberGenerator::GenerateBlock(byte *output, unsigned int size)
00020 {
00021 while (size--)
00022 *output++ = GetByte();
00023 }
00024
00025 word32 RandomNumberGenerator::GenerateWord32(word32 min, word32 max)
00026 {
00027 word32 range = max-min;
00028 const int maxBytes = BytePrecision(range);
00029 const int maxBits = BitPrecision(range);
00030
00031 word32 value;
00032
00033 do
00034 {
00035 value = 0;
00036 for (int i=0; i<maxBytes; i++)
00037 value = (value << 8) | GetByte();
00038
00039 value = Crop(value, maxBits);
00040 } while (value > range);
00041
00042 return value+min;
00043 }
00044
00045 void StreamCipher::ProcessString(byte *outString, const byte *inString, unsigned int length)
00046 {
00047 while(length--)
00048 *outString++ = ProcessByte(*inString++);
00049 }
00050
00051 void StreamCipher::ProcessString(byte *inoutString, unsigned int length)
00052 {
00053 while(length--)
00054 *inoutString++ = ProcessByte(*inoutString);
00055 }
00056
00057 bool HashModule::Verify(const byte *digestIn)
00058 {
00059 SecByteBlock digest(DigestSize());
00060 Final(digest);
00061 return memcmp(digest, digestIn, DigestSize()) == 0;
00062 }
00063
00064 BufferedTransformation::Err::Err(ErrorType errorType, const std::string &s)
00065 : Exception(s), m_errorType(errorType)
00066 {
00067 if (GetWhat() == "")
00068 {
00069 switch (errorType)
00070 {
00071 case CANNOT_FLUSH:
00072 SetWhat("BufferedTransformation: cannot flush buffer");
00073 break;
00074 case DATA_INTEGRITY_CHECK_FAILED:
00075 SetWhat("BufferedTransformation: data integrity check failed");
00076 break;
00077 case INVALID_DATA_FORMAT:
00078 SetWhat("BufferedTransformation: invalid data format");
00079 break;
00080 case OUTPUT_ERROR:
00081 SetWhat("BufferedTransformation: cannot write to output device");
00082 break;
00083 case OTHER_ERROR:
00084 SetWhat("BufferedTransformation: unknown error");
00085 break;
00086 default:
00087 assert(false);
00088 break;
00089 }
00090 }
00091 }
00092
00093 void BufferedTransformation::Put(byte b)
00094 {
00095 if (AttachedTransformation())
00096 AttachedTransformation()->Put(b);
00097 }
00098
00099 void BufferedTransformation::Put(const byte *inString, unsigned int length)
00100 {
00101 if (AttachedTransformation())
00102 AttachedTransformation()->Put(inString, length);
00103 }
00104
00105 void BufferedTransformation::Flush(bool completeFlush, int propagation)
00106 {
00107 if (AttachedTransformation() && propagation)
00108 AttachedTransformation()->Flush(completeFlush, propagation-1);
00109 }
00110
00111 void BufferedTransformation::MessageEnd(int propagation)
00112 {
00113 if (AttachedTransformation() && propagation)
00114 AttachedTransformation()->MessageEnd(propagation-1);
00115 }
00116
00117 void BufferedTransformation::MessageSeriesEnd(int propagation)
00118 {
00119 if (AttachedTransformation() && propagation)
00120 AttachedTransformation()->MessageSeriesEnd(propagation-1);
00121 }
00122
00123 void BufferedTransformation::PutMessageEnd(const byte *inString, unsigned int length, int propagation)
00124 {
00125 Put(inString, length);
00126 MessageEnd(propagation);
00127 }
00128
00129 void BufferedTransformation::ChannelFlush(const std::string &channel, bool completeFlush, int propagation)
00130 {
00131 if (channel.empty())
00132 Flush(completeFlush, propagation);
00133 else if (AttachedTransformation() && propagation)
00134 AttachedTransformation()->ChannelFlush(channel, completeFlush, propagation-1);
00135 }
00136
00137 void BufferedTransformation::ChannelMessageEnd(const std::string &channel, int propagation)
00138 {
00139 if (channel.empty())
00140 MessageEnd(propagation);
00141 else if (AttachedTransformation() && propagation)
00142 AttachedTransformation()->ChannelMessageEnd(channel, propagation-1);
00143 }
00144
00145 void BufferedTransformation::ChannelMessageSeriesEnd(const std::string &channel, int propagation)
00146 {
00147 if (channel.empty())
00148 MessageSeriesEnd(propagation);
00149 else if (AttachedTransformation() && propagation)
00150 AttachedTransformation()->ChannelMessageSeriesEnd(channel, propagation-1);
00151 }
00152
00153 void BufferedTransformation::ChannelPutMessageEnd(const std::string &channel, const byte *inString, unsigned int length, int propagation)
00154 {
00155 if (channel.empty())
00156 PutMessageEnd(inString, length, propagation);
00157 else
00158 {
00159 ChannelPut(channel, inString, length);
00160 ChannelMessageEnd(channel, propagation);
00161 }
00162 }
00163
00164 unsigned long BufferedTransformation::MaxRetrievable() const
00165 {
00166 if (AttachedTransformation())
00167 return AttachedTransformation()->MaxRetrievable();
00168 else
00169 return CopyTo(g_bitBucket);
00170 }
00171
00172 bool BufferedTransformation::AnyRetrievable() const
00173 {
00174 if (AttachedTransformation())
00175 return AttachedTransformation()->AnyRetrievable();
00176 else
00177 {
00178 byte b;
00179 return Peek(b) != 0;
00180 }
00181 }
00182
00183 unsigned int BufferedTransformation::Get(byte &outByte)
00184 {
00185 if (AttachedTransformation())
00186 return AttachedTransformation()->Get(outByte);
00187 else
00188 return Get(&outByte, 1);
00189 }
00190
00191 unsigned int BufferedTransformation::Get(byte *outString, unsigned int getMax)
00192 {
00193 if (AttachedTransformation())
00194 return AttachedTransformation()->Get(outString, getMax);
00195 else
00196 {
00197 ArraySink arraySink(outString, getMax);
00198 return TransferTo(arraySink, getMax);
00199 }
00200 }
00201
00202 unsigned int BufferedTransformation::Peek(byte &outByte) const
00203 {
00204 if (AttachedTransformation())
00205 return AttachedTransformation()->Peek(outByte);
00206 else
00207 return Peek(&outByte, 1);
00208 }
00209
00210 unsigned int BufferedTransformation::Peek(byte *outString, unsigned int peekMax) const
00211 {
00212 if (AttachedTransformation())
00213 return AttachedTransformation()->Peek(outString, peekMax);
00214 else
00215 {
00216 ArraySink arraySink(outString, peekMax);
00217 return CopyTo(arraySink, peekMax);
00218 }
00219 }
00220
00221 unsigned long BufferedTransformation::Skip(unsigned long skipMax)
00222 {
00223 if (AttachedTransformation())
00224 return AttachedTransformation()->Skip(skipMax);
00225 else
00226 return TransferTo(g_bitBucket, skipMax);
00227 }
00228
00229 unsigned long BufferedTransformation::CopyTo(BufferedTransformation &target, unsigned long copyMax) const
00230 {
00231 if (AttachedTransformation())
00232 return AttachedTransformation()->CopyTo(target, copyMax);
00233 else
00234 return 0;
00235 }
00236
00237 unsigned long BufferedTransformation::TransferTo(BufferedTransformation &target, unsigned long size)
00238 {
00239 if (AttachedTransformation())
00240 return AttachedTransformation()->TransferTo(target, size);
00241 else
00242 return 0;
00243 }
00244
00245 unsigned long BufferedTransformation::TotalBytesRetrievable() const
00246 {
00247 if (AttachedTransformation())
00248 return AttachedTransformation()->TotalBytesRetrievable();
00249 else
00250 return MaxRetrievable();
00251 }
00252
00253 unsigned int BufferedTransformation::NumberOfMessages() const
00254 {
00255 if (AttachedTransformation())
00256 return AttachedTransformation()->NumberOfMessages();
00257 else
00258 return CopyMessagesTo(g_bitBucket);
00259 }
00260
00261 bool BufferedTransformation::AnyMessages() const
00262 {
00263 if (AttachedTransformation())
00264 return AttachedTransformation()->NumberOfMessages();
00265 else
00266 return NumberOfMessages() != 0;
00267 }
00268
00269 bool BufferedTransformation::GetNextMessage()
00270 {
00271 if (AttachedTransformation())
00272 return AttachedTransformation()->GetNextMessage();
00273 else
00274 return false;
00275 }
00276
00277 unsigned int BufferedTransformation::SkipMessages(unsigned int count)
00278 {
00279 if (AttachedTransformation())
00280 return AttachedTransformation()->SkipMessages(count);
00281 else
00282 return TransferMessagesTo(g_bitBucket, count);
00283 }
00284
00285 unsigned int BufferedTransformation::TransferMessagesTo(BufferedTransformation &target, unsigned int count)
00286 {
00287 if (AttachedTransformation())
00288 return AttachedTransformation()->TransferMessagesTo(target, count);
00289 else
00290 {
00291 unsigned int i;
00292 for (i=0; i<count && AnyMessages(); i++)
00293 {
00294 while (TransferTo(target)) {}
00295 bool result = GetNextMessage();
00296 assert(result);
00297 target.MessageEnd(GetAutoSignalPropagation());
00298 }
00299 return i;
00300 }
00301 }
00302
00303 unsigned int BufferedTransformation::CopyMessagesTo(BufferedTransformation &target, unsigned int count) const
00304 {
00305 if (AttachedTransformation())
00306 return AttachedTransformation()->CopyMessagesTo(target, count);
00307 else
00308 return 0;
00309 }
00310
00311 void BufferedTransformation::SkipAll()
00312 {
00313 if (AttachedTransformation())
00314 AttachedTransformation()->SkipAll();
00315 else
00316 {
00317 while (SkipMessages()) {}
00318 while (Skip()) {}
00319 }
00320 }
00321
00322 void BufferedTransformation::TransferAllTo(BufferedTransformation &target)
00323 {
00324 if (AttachedTransformation())
00325 AttachedTransformation()->TransferAllTo(target);
00326 else
00327 {
00328 while (TransferMessagesTo(target)) {}
00329 while (TransferTo(target)) {}
00330 }
00331 }
00332
00333 void BufferedTransformation::CopyAllTo(BufferedTransformation &target) const
00334 {
00335 if (AttachedTransformation())
00336 AttachedTransformation()->CopyAllTo(target);
00337 else
00338 {
00339 CopyMessagesTo(target);
00340 CopyTo(target);
00341 }
00342 }
00343
00344 void BufferedTransformation::SetRetrievalChannel(const std::string &channel)
00345 {
00346 if (AttachedTransformation())
00347 AttachedTransformation()->SetRetrievalChannel(channel);
00348 }
00349
00350 void BufferedTransformation::ChannelPut(const std::string &channel, byte inByte)
00351 {
00352 if (channel.empty())
00353 Put(inByte);
00354 }
00355
00356 void BufferedTransformation::ChannelPut(const std::string &channel, const byte *inString, unsigned int length)
00357 {
00358 if (channel.empty())
00359 Put(inString, length);
00360 }
00361
00362 void BufferedTransformation::ChannelPutWord16(const std::string &channel, word16 value, bool highFirst)
00363 {
00364 if (highFirst)
00365 {
00366 ChannelPut(channel, value>>8);
00367 ChannelPut(channel, byte(value));
00368 }
00369 else
00370 {
00371 ChannelPut(channel, byte(value));
00372 ChannelPut(channel, value>>8);
00373 }
00374 }
00375
00376 void BufferedTransformation::ChannelPutWord32(const std::string &channel, word32 value, bool highFirst)
00377 {
00378 if (highFirst)
00379 {
00380 for (int i=0; i<4; i++)
00381 ChannelPut(channel, byte(value>>((3-i)*8)));
00382 }
00383 else
00384 {
00385 for (int i=0; i<4; i++)
00386 ChannelPut(channel, byte(value>>(i*8)));
00387 }
00388 }
00389
00390 void BufferedTransformation::PutWord16(word16 value, bool highFirst)
00391 {
00392 ChannelPutWord16(NULL_CHANNEL, value, highFirst);
00393 }
00394
00395 void BufferedTransformation::PutWord32(word32 value, bool highFirst)
00396 {
00397 ChannelPutWord32(NULL_CHANNEL, value, highFirst);
00398 }
00399
00400 unsigned int BufferedTransformation::PeekWord16(word16 &value, bool highFirst)
00401 {
00402 byte buf[2] = {0, 0};
00403 unsigned int len = Peek(buf, 2);
00404
00405 if (highFirst)
00406 value = (buf[0] << 8) | buf[1];
00407 else
00408 value = (buf[1] << 8) | buf[0];
00409
00410 return len;
00411 }
00412
00413 unsigned int BufferedTransformation::PeekWord32(word32 &value, bool highFirst)
00414 {
00415 byte buf[4] = {0, 0, 0, 0};
00416 unsigned int len = Peek(buf, 4);
00417
00418 if (highFirst)
00419 value = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf [3];
00420 else
00421 value = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf [0];
00422
00423 return len;
00424 }
00425
00426 unsigned int BufferedTransformation::GetWord16(word16 &value, bool highFirst)
00427 {
00428 return Skip(PeekWord16(value, highFirst));
00429 }
00430
00431 unsigned int BufferedTransformation::GetWord32(word32 &value, bool highFirst)
00432 {
00433 return Skip(PeekWord32(value, highFirst));
00434 }
00435
00436 void BufferedTransformation::Attach(BufferedTransformation *newOut)
00437 {
00438 if (!Attachable())
00439 return;
00440
00441 if (AttachedTransformation() && AttachedTransformation()->Attachable())
00442 AttachedTransformation()->Attach(newOut);
00443 else
00444 Detach(newOut);
00445 }
00446
00447 unsigned int PK_FixedLengthCryptoSystem::MaxPlainTextLength(unsigned int cipherTextLength) const
00448 {
00449 if (cipherTextLength == CipherTextLength())
00450 return MaxPlainTextLength();
00451 else
00452 return 0;
00453 }
00454
00455 unsigned int PK_FixedLengthCryptoSystem::CipherTextLength(unsigned int plainTextLength) const
00456 {
00457 if (plainTextLength <= MaxPlainTextLength())
00458 return CipherTextLength();
00459 else
00460 return 0;
00461 }
00462
00463 unsigned int PK_FixedLengthDecryptor::Decrypt(const byte *cipherText, unsigned int cipherTextLength, byte *plainText)
00464 {
00465 if (cipherTextLength != CipherTextLength())
00466 return 0;
00467
00468 return Decrypt(cipherText, plainText);
00469 }
00470
00471 void PK_Signer::SignMessage(RandomNumberGenerator &rng, const byte *message, unsigned int messageLen, byte *signature) const
00472 {
00473 std::auto_ptr<HashModule> accumulator(NewMessageAccumulator());
00474 accumulator->Update(message, messageLen);
00475 Sign(rng, accumulator.release(), signature);
00476 }
00477
00478 bool PK_Verifier::VerifyMessage(const byte *message, unsigned int messageLen, const byte *sig) const
00479 {
00480 std::auto_ptr<HashModule> accumulator(NewMessageAccumulator());
00481 accumulator->Update(message, messageLen);
00482 return Verify(accumulator.release(), sig);
00483 }
00484
00485 NAMESPACE_END