?? cryptlib.cpp
字號(hào):
// cryptlib.cpp - written and placed in the public domain by Wei Dai#include "pch.h"#include "cryptlib.h"#include "misc.h"#include "filters.h"#include "algparam.h"#include "fips140.h"#include "argnames.h"#include <memory>NAMESPACE_BEGIN(CryptoPP)CRYPTOPP_COMPILE_ASSERT(sizeof(byte) == 1);CRYPTOPP_COMPILE_ASSERT(sizeof(word16) == 2);CRYPTOPP_COMPILE_ASSERT(sizeof(word32) == 4);#ifdef WORD64_AVAILABLECRYPTOPP_COMPILE_ASSERT(sizeof(word64) == 8);#endifCRYPTOPP_COMPILE_ASSERT(sizeof(dword) == 2*sizeof(word));const std::string BufferedTransformation::NULL_CHANNEL;const NullNameValuePairs g_nullNameValuePairs;BufferedTransformation & TheBitBucket(){ static BitBucket bitBucket; return bitBucket;}Algorithm::Algorithm(bool checkSelfTestStatus){ if (checkSelfTestStatus && FIPS_140_2_ComplianceEnabled()) { if (GetPowerUpSelfTestStatus() == POWER_UP_SELF_TEST_NOT_DONE && !PowerUpSelfTestInProgressOnThisThread()) throw SelfTestFailure("Cryptographic algorithms are disabled before the power-up self tests are performed."); if (GetPowerUpSelfTestStatus() == POWER_UP_SELF_TEST_FAILED) throw SelfTestFailure("Cryptographic algorithms are disabled after power-up a self test failed."); }}void SimpleKeyingInterface::SetKeyWithRounds(const byte *key, unsigned int length, int rounds){ SetKey(key, length, MakeParameters(Name::Rounds(), rounds));}void SimpleKeyingInterface::SetKeyWithIV(const byte *key, unsigned int length, const byte *iv){ SetKey(key, length, MakeParameters(Name::IV(), iv));}void SimpleKeyingInterface::ThrowIfInvalidKeyLength(const Algorithm &algorithm, unsigned int length){ if (!IsValidKeyLength(length)) throw InvalidKeyLength(algorithm.AlgorithmName(), length);}void BlockTransformation::ProcessAndXorMultipleBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, unsigned int numberOfBlocks) const{ unsigned int blockSize = BlockSize(); while (numberOfBlocks--) { ProcessAndXorBlock(inBlocks, xorBlocks, outBlocks); inBlocks += blockSize; outBlocks += blockSize; if (xorBlocks) xorBlocks += blockSize; }}void StreamTransformation::ProcessLastBlock(byte *outString, const byte *inString, unsigned int length){ assert(MinLastBlockSize() == 0); // this function should be overriden otherwise if (length == MandatoryBlockSize()) ProcessData(outString, inString, length); else if (length != 0) throw NotImplemented("StreamTransformation: this object does't support a special last block");}unsigned int RandomNumberGenerator::GenerateBit(){ return Parity(GenerateByte());}void RandomNumberGenerator::GenerateBlock(byte *output, unsigned int size){ while (size--) *output++ = GenerateByte();}word32 RandomNumberGenerator::GenerateWord32(word32 min, word32 max){ word32 range = max-min; const int maxBytes = BytePrecision(range); const int maxBits = BitPrecision(range); word32 value; do { value = 0; for (int i=0; i<maxBytes; i++) value = (value << 8) | GenerateByte(); value = Crop(value, maxBits); } while (value > range); return value+min;}void RandomNumberGenerator::DiscardBytes(unsigned int n){ while (n--) GenerateByte();}RandomNumberGenerator & NullRNG(){ class NullRNG : public RandomNumberGenerator { public: std::string AlgorithmName() const {return "NullRNG";} byte GenerateByte() {throw NotImplemented("NullRNG: NullRNG should only be passed to functions that don't need to generate random bytes");} }; static NullRNG s_nullRNG; return s_nullRNG;}bool HashTransformation::TruncatedVerify(const byte *digestIn, unsigned int digestLength){ ThrowIfInvalidTruncatedSize(digestLength); SecByteBlock digest(digestLength); TruncatedFinal(digest, digestLength); return memcmp(digest, digestIn, digestLength) == 0;}void HashTransformation::ThrowIfInvalidTruncatedSize(unsigned int size) const{ if (size > DigestSize()) throw InvalidArgument("HashTransformation: can't truncate a " + IntToString(DigestSize()) + " byte digest to " + IntToString(size) + " bytes");}unsigned int BufferedTransformation::GetMaxWaitObjectCount() const{ const BufferedTransformation *t = AttachedTransformation(); return t ? t->GetMaxWaitObjectCount() : 0;}void BufferedTransformation::GetWaitObjects(WaitObjectContainer &container){ BufferedTransformation *t = AttachedTransformation(); if (t) t->GetWaitObjects(container);}void BufferedTransformation::Initialize(const NameValuePairs ¶meters, int propagation){ assert(!AttachedTransformation()); IsolatedInitialize(parameters);}bool BufferedTransformation::Flush(bool hardFlush, int propagation, bool blocking){ assert(!AttachedTransformation()); return IsolatedFlush(hardFlush, blocking);}bool BufferedTransformation::MessageSeriesEnd(int propagation, bool blocking){ assert(!AttachedTransformation()); return IsolatedMessageSeriesEnd(blocking);}byte * BufferedTransformation::ChannelCreatePutSpace(const std::string &channel, unsigned int &size){ if (channel.empty()) return CreatePutSpace(size); else throw NoChannelSupport();}unsigned int BufferedTransformation::ChannelPut2(const std::string &channel, const byte *begin, unsigned int length, int messageEnd, bool blocking){ if (channel.empty()) return Put2(begin, length, messageEnd, blocking); else throw NoChannelSupport();}unsigned int BufferedTransformation::ChannelPutModifiable2(const std::string &channel, byte *begin, unsigned int length, int messageEnd, bool blocking){ if (channel.empty()) return PutModifiable2(begin, length, messageEnd, blocking); else return ChannelPut2(channel, begin, length, messageEnd, blocking);}void BufferedTransformation::ChannelInitialize(const std::string &channel, const NameValuePairs ¶meters, int propagation){ if (channel.empty()) Initialize(parameters, propagation); else throw NoChannelSupport();}bool BufferedTransformation::ChannelFlush(const std::string &channel, bool completeFlush, int propagation, bool blocking){ if (channel.empty()) return Flush(completeFlush, propagation, blocking); else throw NoChannelSupport();}bool BufferedTransformation::ChannelMessageSeriesEnd(const std::string &channel, int propagation, bool blocking){ if (channel.empty()) return MessageSeriesEnd(propagation, blocking); else throw NoChannelSupport();}unsigned long BufferedTransformation::MaxRetrievable() const{ if (AttachedTransformation()) return AttachedTransformation()->MaxRetrievable(); else return CopyTo(TheBitBucket());}bool BufferedTransformation::AnyRetrievable() const{ if (AttachedTransformation()) return AttachedTransformation()->AnyRetrievable(); else { byte b; return Peek(b) != 0; }}unsigned int BufferedTransformation::Get(byte &outByte){ if (AttachedTransformation()) return AttachedTransformation()->Get(outByte); else return Get(&outByte, 1);}unsigned int BufferedTransformation::Get(byte *outString, unsigned int getMax){ if (AttachedTransformation()) return AttachedTransformation()->Get(outString, getMax); else { ArraySink arraySink(outString, getMax); return TransferTo(arraySink, getMax); }}unsigned int BufferedTransformation::Peek(byte &outByte) const{ if (AttachedTransformation()) return AttachedTransformation()->Peek(outByte); else return Peek(&outByte, 1);}unsigned int BufferedTransformation::Peek(byte *outString, unsigned int peekMax) const{ if (AttachedTransformation()) return AttachedTransformation()->Peek(outString, peekMax); else { ArraySink arraySink(outString, peekMax); return CopyTo(arraySink, peekMax); }}unsigned long BufferedTransformation::Skip(unsigned long skipMax){ if (AttachedTransformation()) return AttachedTransformation()->Skip(skipMax); else return TransferTo(TheBitBucket(), skipMax);}unsigned long BufferedTransformation::TotalBytesRetrievable() const{ if (AttachedTransformation()) return AttachedTransformation()->TotalBytesRetrievable(); else return MaxRetrievable();}unsigned int BufferedTransformation::NumberOfMessages() const{ if (AttachedTransformation()) return AttachedTransformation()->NumberOfMessages(); else return CopyMessagesTo(TheBitBucket());}bool BufferedTransformation::AnyMessages() const{ if (AttachedTransformation()) return AttachedTransformation()->AnyMessages(); else return NumberOfMessages() != 0;}bool BufferedTransformation::GetNextMessage(){ if (AttachedTransformation()) return AttachedTransformation()->GetNextMessage(); else { assert(!AnyMessages()); return false; }}unsigned int BufferedTransformation::SkipMessages(unsigned int count){ if (AttachedTransformation()) return AttachedTransformation()->SkipMessages(count); else return TransferMessagesTo(TheBitBucket(), count);}unsigned int BufferedTransformation::TransferMessagesTo2(BufferedTransformation &target, unsigned int &messageCount, const std::string &channel, bool blocking){ if (AttachedTransformation()) return AttachedTransformation()->TransferMessagesTo2(target, messageCount, channel, blocking); else { unsigned int maxMessages = messageCount; for (messageCount=0; messageCount < maxMessages && AnyMessages(); messageCount++) { unsigned int blockedBytes; unsigned long transferedBytes;
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -