⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cryptlib.cpp

📁 lots Elliptic curve cryptography codes. Use Visual c++ to compile
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// cryptlib.cpp - written and placed in the public domain by Wei Dai#include "pch.h"#ifndef CRYPTOPP_IMPORTS#include "cryptlib.h"#include "misc.h"#include "filters.h"#include "algparam.h"#include "fips140.h"#include "argnames.h"#include "fltrimpl.h"#include "trdlocal.h"#include "osrng.h"#include <memory>NAMESPACE_BEGIN(CryptoPP)CRYPTOPP_COMPILE_ASSERT(sizeof(byte) == 1);CRYPTOPP_COMPILE_ASSERT(sizeof(word16) == 2);CRYPTOPP_COMPILE_ASSERT(sizeof(word32) == 4);CRYPTOPP_COMPILE_ASSERT(sizeof(word64) == 8);#ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLECRYPTOPP_COMPILE_ASSERT(sizeof(dword) == 2*sizeof(word));#endifconst std::string DEFAULT_CHANNEL;const std::string AAD_CHANNEL = "AAD";const std::string &BufferedTransformation::NULL_CHANNEL = DEFAULT_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 a power-up self test failed.");	}}void SimpleKeyingInterface::SetKey(const byte *key, size_t length, const NameValuePairs &params){	this->ThrowIfInvalidKeyLength(length);	this->UncheckedSetKey(key, (unsigned int)length, params);}void SimpleKeyingInterface::SetKeyWithRounds(const byte *key, size_t length, int rounds){	SetKey(key, length, MakeParameters(Name::Rounds(), rounds));}void SimpleKeyingInterface::SetKeyWithIV(const byte *key, size_t length, const byte *iv, size_t ivLength){	SetKey(key, length, MakeParameters(Name::IV(), ConstByteArrayParameter(iv, ivLength)));}void SimpleKeyingInterface::ThrowIfInvalidKeyLength(size_t length){	if (!IsValidKeyLength(length))		throw InvalidKeyLength(GetAlgorithm().AlgorithmName(), length);}void SimpleKeyingInterface::ThrowIfResynchronizable(){	if (IsResynchronizable())		throw InvalidArgument(GetAlgorithm().AlgorithmName() + ": this object requires an IV");}void SimpleKeyingInterface::ThrowIfInvalidIV(const byte *iv){	if (!iv && !(IVRequirement() == INTERNALLY_GENERATED_IV || IVRequirement() == UNIQUE_IV || !IsResynchronizable()))		throw InvalidArgument(GetAlgorithm().AlgorithmName() + ": this object cannot use a null IV");}size_t SimpleKeyingInterface::ThrowIfInvalidIVLength(int size){	if (size < 0)		return IVSize();	else if ((size_t)size < MinIVLength())		throw InvalidArgument(GetAlgorithm().AlgorithmName() + ": IV length " + IntToString(size) + " is less than the minimum of " + IntToString(MinIVLength()));	else if ((size_t)size > MaxIVLength())		throw InvalidArgument(GetAlgorithm().AlgorithmName() + ": IV length " + IntToString(size) + " exceeds the maximum of " + IntToString(MaxIVLength()));	else		return size;}const byte * SimpleKeyingInterface::GetIVAndThrowIfInvalid(const NameValuePairs &params, size_t &size){	ConstByteArrayParameter ivWithLength;	const byte *iv;	bool found = false;	try {found = params.GetValue(Name::IV(), ivWithLength);}	catch (const NameValuePairs::ValueTypeMismatch &) {}	if (found)	{		iv = ivWithLength.begin();		ThrowIfInvalidIV(iv);		size = ThrowIfInvalidIVLength((int)ivWithLength.size());		return iv;	}	else if (params.GetValue(Name::IV(), iv))	{		ThrowIfInvalidIV(iv);		size = IVSize();		return iv;	}	else	{		ThrowIfResynchronizable();		size = 0;		return NULL;	}}void SimpleKeyingInterface::GetNextIV(RandomNumberGenerator &rng, byte *IV){	rng.GenerateBlock(IV, IVSize());}size_t BlockTransformation::AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const{	size_t blockSize = BlockSize();	size_t inIncrement = (flags & (BT_InBlockIsCounter|BT_DontIncrementInOutPointers)) ? 0 : blockSize;	size_t xorIncrement = xorBlocks ? blockSize : 0;	size_t outIncrement = (flags & BT_DontIncrementInOutPointers) ? 0 : blockSize;	if (flags & BT_ReverseDirection)	{		assert(length % blockSize == 0);		inBlocks += length - blockSize;		xorBlocks += length - blockSize;		outBlocks += length - blockSize;		inIncrement = 0-inIncrement;		xorIncrement = 0-xorIncrement;		outIncrement = 0-outIncrement;	}	while (length >= blockSize)	{		if (flags & BT_XorInput)		{			xorbuf(outBlocks, xorBlocks, inBlocks, blockSize);			ProcessBlock(outBlocks);		}		else			ProcessAndXorBlock(inBlocks, xorBlocks, outBlocks);		if (flags & BT_InBlockIsCounter)			const_cast<byte *>(inBlocks)[blockSize-1]++;		inBlocks += inIncrement;		outBlocks += outIncrement;		xorBlocks += xorIncrement;		length -= blockSize;	}	return length;}unsigned int BlockTransformation::OptimalDataAlignment() const{	return GetAlignmentOf<word32>();}unsigned int StreamTransformation::OptimalDataAlignment() const{	return GetAlignmentOf<word32>();}unsigned int HashTransformation::OptimalDataAlignment() const{	return GetAlignmentOf<word32>();}void StreamTransformation::ProcessLastBlock(byte *outString, const byte *inString, size_t length){	assert(MinLastBlockSize() == 0);	// this function should be overriden otherwise	if (length == MandatoryBlockSize())		ProcessData(outString, inString, length);	else if (length != 0)		throw NotImplemented(AlgorithmName() + ": this object does't support a special last block");}void AuthenticatedSymmetricCipher::SpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength){	if (headerLength > MaxHeaderLength())		throw InvalidArgument(GetAlgorithm().AlgorithmName() + ": header length " + IntToString(headerLength) + " exceeds the maximum of " + IntToString(MaxHeaderLength()));	if (messageLength > MaxMessageLength())		throw InvalidArgument(GetAlgorithm().AlgorithmName() + ": message length " + IntToString(messageLength) + " exceeds the maximum of " + IntToString(MaxMessageLength()));			if (footerLength > MaxFooterLength())		throw InvalidArgument(GetAlgorithm().AlgorithmName() + ": footer length " + IntToString(footerLength) + " exceeds the maximum of " + IntToString(MaxFooterLength()));	UncheckedSpecifyDataLengths(headerLength, messageLength, footerLength);}void AuthenticatedSymmetricCipher::EncryptAndAuthenticate(byte *ciphertext, byte *mac, size_t macSize, const byte *iv, int ivLength, const byte *header, size_t headerLength, const byte *message, size_t messageLength){	Resynchronize(iv, ivLength);	SpecifyDataLengths(headerLength, messageLength);	Update(header, headerLength);	ProcessString(ciphertext, message, messageLength);	TruncatedFinal(mac, macSize);}bool AuthenticatedSymmetricCipher::DecryptAndVerify(byte *message, const byte *mac, size_t macLength, const byte *iv, int ivLength, const byte *header, size_t headerLength, const byte *ciphertext, size_t ciphertextLength){	Resynchronize(iv, ivLength);	SpecifyDataLengths(headerLength, ciphertextLength);	Update(header, headerLength);	ProcessString(message, ciphertext, ciphertextLength);	return TruncatedVerify(mac, macLength);}unsigned int RandomNumberGenerator::GenerateBit(){	return GenerateByte() & 1;}byte RandomNumberGenerator::GenerateByte(){	byte b;	GenerateBlock(&b, 1);	return b;}word32 RandomNumberGenerator::GenerateWord32(word32 min, word32 max){	word32 range = max-min;	const int maxBits = BitPrecision(range);	word32 value;	do	{		GenerateBlock((byte *)&value, sizeof(value));		value = Crop(value, maxBits);	} while (value > range);	return value+min;}void RandomNumberGenerator::GenerateBlock(byte *output, size_t size){	ArraySink s(output, size);	GenerateIntoBufferedTransformation(s, DEFAULT_CHANNEL, size);}void RandomNumberGenerator::DiscardBytes(size_t n){	GenerateIntoBufferedTransformation(TheBitBucket(), DEFAULT_CHANNEL, n);}void RandomNumberGenerator::GenerateIntoBufferedTransformation(BufferedTransformation &target, const std::string &channel, lword length){	FixedSizeSecBlock<byte, 256> buffer;	while (length)	{		size_t len = UnsignedMin(buffer.size(), length);		GenerateBlock(buffer, len);		target.ChannelPut(channel, buffer, len);		length -= len;	}}//! see NullRNG()class ClassNullRNG : public RandomNumberGenerator{public:	std::string AlgorithmName() const {return "NullRNG";}	void GenerateBlock(byte *output, size_t size) {throw NotImplemented("NullRNG: NullRNG should only be passed to functions that don't need to generate random bytes");}};RandomNumberGenerator & NullRNG(){	static ClassNullRNG s_nullRNG;	return s_nullRNG;}bool HashTransformation::TruncatedVerify(const byte *digestIn, size_t digestLength){	ThrowIfInvalidTruncatedSize(digestLength);	SecByteBlock digest(digestLength);	TruncatedFinal(digest, digestLength);	return VerifyBufsEqual(digest, digestIn, digestLength);}void HashTransformation::ThrowIfInvalidTruncatedSize(size_t 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, CallStack const& callStack){	BufferedTransformation *t = AttachedTransformation();	if (t)		t->GetWaitObjects(container, callStack);  // reduce clutter by not adding to stack here}void BufferedTransformation::Initialize(const NameValuePairs &parameters, 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, size_t &size){	if (channel.empty())		return CreatePutSpace(size);	else		throw NoChannelSupport(AlgorithmName());}size_t BufferedTransformation::ChannelPut2(const std::string &channel, const byte *begin, size_t length, int messageEnd, bool blocking){	if (channel.empty())		return Put2(begin, length, messageEnd, blocking);	else		throw NoChannelSupport(AlgorithmName());}size_t BufferedTransformation::ChannelPutModifiable2(const std::string &channel, byte *begin, size_t length, int messageEnd, bool blocking){	if (channel.empty())		return PutModifiable2(begin, length, messageEnd, blocking);	else		return ChannelPut2(channel, begin, length, messageEnd, blocking);}bool BufferedTransformation::ChannelFlush(const std::string &channel, bool completeFlush, int propagation, bool blocking){	if (channel.empty())		return Flush(completeFlush, propagation, blocking);	else		throw NoChannelSupport(AlgorithmName());}bool BufferedTransformation::ChannelMessageSeriesEnd(const std::string &channel, int propagation, bool blocking){	if (channel.empty())		return MessageSeriesEnd(propagation, blocking);	else		throw NoChannelSupport(AlgorithmName());}lword 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;	}}size_t BufferedTransformation::Get(byte &outByte){	if (AttachedTransformation())		return AttachedTransformation()->Get(outByte);	else		return Get(&outByte, 1);}size_t BufferedTransformation::Get(byte *outString, size_t getMax){	if (AttachedTransformation())		return AttachedTransformation()->Get(outString, getMax);	else	{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -