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

📄 pubkey.h

📁 lots Elliptic curve cryptography codes. Use Visual c++ to compile
💻 H
📖 第 1 页 / 共 5 页
字号:
	{		PK_MessageAccumulatorBase &ma = static_cast<PK_MessageAccumulatorBase &>(messageAccumulator);		ma.m_recoverableMessage.Assign(recoverableMessage, recoverableMessageLength);		this->GetMessageEncodingInterface().ProcessRecoverableMessage(ma.AccessHash(), 			recoverableMessage, recoverableMessageLength, 			ma.m_presignature, ma.m_presignature.size(),			ma.m_semisignature);	}	size_t SignAndRestart(RandomNumberGenerator &rng, PK_MessageAccumulator &messageAccumulator, byte *signature, bool restart) const	{		this->GetMaterial().DoQuickSanityCheck();		PK_MessageAccumulatorBase &ma = static_cast<PK_MessageAccumulatorBase &>(messageAccumulator);		const DL_ElgamalLikeSignatureAlgorithm<T> &alg = this->GetSignatureAlgorithm();		const DL_GroupParameters<T> &params = this->GetAbstractGroupParameters();		const DL_PrivateKey<T> &key = this->GetKeyInterface();		SecByteBlock representative(this->MessageRepresentativeLength());		this->GetMessageEncodingInterface().ComputeMessageRepresentative(			rng, 			ma.m_recoverableMessage, ma.m_recoverableMessage.size(), 			ma.AccessHash(), this->GetHashIdentifier(), ma.m_empty, 			representative, this->MessageRepresentativeBitLength());		ma.m_empty = true;		Integer e(representative, representative.size());		// hash message digest into random number k to prevent reusing the same k on a different messages		// after virtual machine rollback		if (rng.CanIncorporateEntropy())			rng.IncorporateEntropy(representative, representative.size());		Integer k(rng, 1, params.GetSubgroupOrder()-1);		Integer r, s;		r = params.ConvertElementToInteger(params.ExponentiateBase(k));		alg.Sign(params, key.GetPrivateExponent(), k, e, r, s);		/*		Integer r, s;		if (this->MaxRecoverableLength() > 0)			r.Decode(ma.m_semisignature, ma.m_semisignature.size());		else			r.Decode(ma.m_presignature, ma.m_presignature.size());		alg.Sign(params, key.GetPrivateExponent(), ma.m_k, e, r, s);		*/		size_t rLen = alg.RLen(params);		r.Encode(signature, rLen);		s.Encode(signature+rLen, alg.SLen(params));		if (restart)			RestartMessageAccumulator(rng, ma);		return this->SignatureLength();	}protected:	void RestartMessageAccumulator(RandomNumberGenerator &rng, PK_MessageAccumulatorBase &ma) const	{		// k needs to be generated before hashing for signature schemes with recovery		// but to defend against VM rollbacks we need to generate k after hashing.		// so this code is commented out, since no DL-based signature scheme with recovery		// has been implemented in Crypto++ anyway		/*		const DL_ElgamalLikeSignatureAlgorithm<T> &alg = this->GetSignatureAlgorithm();		const DL_GroupParameters<T> &params = this->GetAbstractGroupParameters();		ma.m_k.Randomize(rng, 1, params.GetSubgroupOrder()-1);		ma.m_presignature.New(params.GetEncodedElementSize(false));		params.ConvertElementToInteger(params.ExponentiateBase(ma.m_k)).Encode(ma.m_presignature, ma.m_presignature.size());		*/	}};//! _template <class T>class CRYPTOPP_NO_VTABLE DL_VerifierBase : public DL_SignatureSchemeBase<PK_Verifier, DL_PublicKey<T> >{public:	void InputSignature(PK_MessageAccumulator &messageAccumulator, const byte *signature, size_t signatureLength) const	{		PK_MessageAccumulatorBase &ma = static_cast<PK_MessageAccumulatorBase &>(messageAccumulator);		const DL_ElgamalLikeSignatureAlgorithm<T> &alg = this->GetSignatureAlgorithm();		const DL_GroupParameters<T> &params = this->GetAbstractGroupParameters();		size_t rLen = alg.RLen(params);		ma.m_semisignature.Assign(signature, rLen);		ma.m_s.Decode(signature+rLen, alg.SLen(params));		this->GetMessageEncodingInterface().ProcessSemisignature(ma.AccessHash(), ma.m_semisignature, ma.m_semisignature.size());	}		bool VerifyAndRestart(PK_MessageAccumulator &messageAccumulator) const	{		this->GetMaterial().DoQuickSanityCheck();		PK_MessageAccumulatorBase &ma = static_cast<PK_MessageAccumulatorBase &>(messageAccumulator);		const DL_ElgamalLikeSignatureAlgorithm<T> &alg = this->GetSignatureAlgorithm();		const DL_GroupParameters<T> &params = this->GetAbstractGroupParameters();		const DL_PublicKey<T> &key = this->GetKeyInterface();		SecByteBlock representative(this->MessageRepresentativeLength());		this->GetMessageEncodingInterface().ComputeMessageRepresentative(NullRNG(), ma.m_recoverableMessage, ma.m_recoverableMessage.size(), 			ma.AccessHash(), this->GetHashIdentifier(), ma.m_empty,			representative, this->MessageRepresentativeBitLength());		ma.m_empty = true;		Integer e(representative, representative.size());		Integer r(ma.m_semisignature, ma.m_semisignature.size());		return alg.Verify(params, key, e, r, ma.m_s);	}	DecodingResult RecoverAndRestart(byte *recoveredMessage, PK_MessageAccumulator &messageAccumulator) const	{		this->GetMaterial().DoQuickSanityCheck();		PK_MessageAccumulatorBase &ma = static_cast<PK_MessageAccumulatorBase &>(messageAccumulator);		const DL_ElgamalLikeSignatureAlgorithm<T> &alg = this->GetSignatureAlgorithm();		const DL_GroupParameters<T> &params = this->GetAbstractGroupParameters();		const DL_PublicKey<T> &key = this->GetKeyInterface();		SecByteBlock representative(this->MessageRepresentativeLength());		this->GetMessageEncodingInterface().ComputeMessageRepresentative(			NullRNG(), 			ma.m_recoverableMessage, ma.m_recoverableMessage.size(), 			ma.AccessHash(), this->GetHashIdentifier(), ma.m_empty,			representative, this->MessageRepresentativeBitLength());		ma.m_empty = true;		Integer e(representative, representative.size());		ma.m_presignature.New(params.GetEncodedElementSize(false));		Integer r(ma.m_semisignature, ma.m_semisignature.size());		alg.RecoverPresignature(params, key, r, ma.m_s).Encode(ma.m_presignature, ma.m_presignature.size());		return this->GetMessageEncodingInterface().RecoverMessageFromSemisignature(			ma.AccessHash(), this->GetHashIdentifier(),			ma.m_presignature, ma.m_presignature.size(),			ma.m_semisignature, ma.m_semisignature.size(),			recoveredMessage);	}};//! _template <class PK, class KI>class CRYPTOPP_NO_VTABLE DL_CryptoSystemBase : public PK, public DL_Base<KI>{public:	typedef typename DL_Base<KI>::Element Element;	size_t MaxPlaintextLength(size_t ciphertextLength) const	{		unsigned int minLen = this->GetAbstractGroupParameters().GetEncodedElementSize(true);		return ciphertextLength < minLen ? 0 : GetSymmetricEncryptionAlgorithm().GetMaxSymmetricPlaintextLength(ciphertextLength - minLen);	}	size_t CiphertextLength(size_t plaintextLength) const	{		size_t len = GetSymmetricEncryptionAlgorithm().GetSymmetricCiphertextLength(plaintextLength);		return len == 0 ? 0 : this->GetAbstractGroupParameters().GetEncodedElementSize(true) + len;	}	bool ParameterSupported(const char *name) const		{return GetKeyDerivationAlgorithm().ParameterSupported(name) || GetSymmetricEncryptionAlgorithm().ParameterSupported(name);}protected:	virtual const DL_KeyAgreementAlgorithm<Element> & GetKeyAgreementAlgorithm() const =0;	virtual const DL_KeyDerivationAlgorithm<Element> & GetKeyDerivationAlgorithm() const =0;	virtual const DL_SymmetricEncryptionAlgorithm & GetSymmetricEncryptionAlgorithm() const =0;};//! _template <class T>class CRYPTOPP_NO_VTABLE DL_DecryptorBase : public DL_CryptoSystemBase<PK_Decryptor, DL_PrivateKey<T> >{public:	typedef T Element;	DecodingResult Decrypt(RandomNumberGenerator &rng, const byte *ciphertext, size_t ciphertextLength, byte *plaintext, const NameValuePairs &parameters = g_nullNameValuePairs) const	{		try		{			const DL_KeyAgreementAlgorithm<T> &agreeAlg = this->GetKeyAgreementAlgorithm();			const DL_KeyDerivationAlgorithm<T> &derivAlg = this->GetKeyDerivationAlgorithm();			const DL_SymmetricEncryptionAlgorithm &encAlg = this->GetSymmetricEncryptionAlgorithm();			const DL_GroupParameters<T> &params = this->GetAbstractGroupParameters();			const DL_PrivateKey<T> &key = this->GetKeyInterface();			Element q = params.DecodeElement(ciphertext, true);			size_t elementSize = params.GetEncodedElementSize(true);			ciphertext += elementSize;			ciphertextLength -= elementSize;			Element z = agreeAlg.AgreeWithStaticPrivateKey(params, q, true, key.GetPrivateExponent());			SecByteBlock derivedKey(encAlg.GetSymmetricKeyLength(encAlg.GetMaxSymmetricPlaintextLength(ciphertextLength)));			derivAlg.Derive(params, derivedKey, derivedKey.size(), z, q, parameters);			return encAlg.SymmetricDecrypt(derivedKey, ciphertext, ciphertextLength, plaintext, parameters);		}		catch (DL_BadElement &)		{			return DecodingResult();		}	}};//! _template <class T>class CRYPTOPP_NO_VTABLE DL_EncryptorBase : public DL_CryptoSystemBase<PK_Encryptor, DL_PublicKey<T> >{public:	typedef T Element;	void Encrypt(RandomNumberGenerator &rng, const byte *plaintext, size_t plaintextLength, byte *ciphertext, const NameValuePairs &parameters = g_nullNameValuePairs) const	{		const DL_KeyAgreementAlgorithm<T> &agreeAlg = this->GetKeyAgreementAlgorithm();		const DL_KeyDerivationAlgorithm<T> &derivAlg = this->GetKeyDerivationAlgorithm();		const DL_SymmetricEncryptionAlgorithm &encAlg = this->GetSymmetricEncryptionAlgorithm();		const DL_GroupParameters<T> &params = this->GetAbstractGroupParameters();		const DL_PublicKey<T> &key = this->GetKeyInterface();		Integer x(rng, Integer::One(), params.GetMaxExponent());		Element q = params.ExponentiateBase(x);		params.EncodeElement(true, q, ciphertext);		unsigned int elementSize = params.GetEncodedElementSize(true);		ciphertext += elementSize;		Element z = agreeAlg.AgreeWithEphemeralPrivateKey(params, key.GetPublicPrecomputation(), x);		SecByteBlock derivedKey(encAlg.GetSymmetricKeyLength(plaintextLength));		derivAlg.Derive(params, derivedKey, derivedKey.size(), z, q, parameters);		encAlg.SymmetricEncrypt(rng, derivedKey, plaintext, plaintextLength, ciphertext, parameters);	}};//! _template <class T1, class T2>struct DL_SchemeOptionsBase{	typedef T1 AlgorithmInfo;	typedef T2 GroupParameters;	typedef typename GroupParameters::Element Element;};//! _template <class T1, class T2>struct DL_KeyedSchemeOptions : public DL_SchemeOptionsBase<T1, typename T2::PublicKey::GroupParameters>{	typedef T2 Keys;	typedef typename Keys::PrivateKey PrivateKey;	typedef typename Keys::PublicKey PublicKey;};//! _template <class T1, class T2, class T3, class T4, class T5>struct DL_SignatureSchemeOptions : public DL_KeyedSchemeOptions<T1, T2>{	typedef T3 SignatureAlgorithm;	typedef T4 MessageEncodingMethod;	typedef T5 HashFunction;};//! _template <class T1, class T2, class T3, class T4, class T5>struct DL_CryptoSchemeOptions : public DL_KeyedSchemeOptions<T1, T2>{	typedef T3 KeyAgreementAlgorithm;	typedef T4 KeyDerivationAlgorithm;	typedef T5 SymmetricEncryptionAlgorithm;};//! _template <class BASE, class SCHEME_OPTIONS, class KEY>class CRYPTOPP_NO_VTABLE DL_ObjectImplBase : public AlgorithmImpl<BASE, typename SCHEME_OPTIONS::AlgorithmInfo>{public:	typedef SCHEME_OPTIONS SchemeOptions;	typedef typename KEY::Element Element;	PrivateKey & AccessPrivateKey() {return m_key;}	PublicKey & AccessPublicKey() {return m_key;}	// KeyAccessor	const KEY & GetKey() const {return m_key;}	KEY & AccessKey() {return m_key;}protected:	typename BASE::KeyInterface & AccessKeyInterface() {return m_key;}	const typename BASE::KeyInterface & GetKeyInterface() const {return m_key;}	// for signature scheme	HashIdentifier GetHashIdentifier() const	{		typedef typename SchemeOptions::MessageEncodingMethod::HashIdentifierLookup HashLookup;		return HashLookup::template HashIdentifierLookup2<CPP_TYPENAME SchemeOptions::HashFunction>::Lookup();	}	size_t GetDigestSize() const	{		typedef CPP_TYPENAME SchemeOptions::HashFunction H;		return H::DIGESTSIZE;	}private:	KEY m_key;};//! _template <class BASE, class SCHEME_OPTIONS, class KEY>class CRYPTOPP_NO_VTABLE DL_ObjectImpl : public DL_ObjectImplBase<BASE, SCHEME_OPTIONS, KEY>{public:	typedef typename KEY::Element Element;protected:	const DL_ElgamalLikeSignatureAlgorithm<Element> & GetSignatureAlgorithm() const		{return Singleton<CPP_TYPENAME SCHEME_OPTIONS::SignatureAlgorithm>().Ref();}	const DL_KeyAgreementAlgorithm<Element> & GetKeyAgreementAlgorithm() const		{return Singleton<CPP_TYPENAME SCHEME_OPTIONS::KeyAgreementAlgorithm>().Ref();}	const DL_KeyDerivationAlgorithm<Element> & GetKeyDerivationAlgorithm() const		{return Singleton<CPP_TYPENAME SCHEME_OPTIONS::KeyDerivationAlgorithm>().Ref();}	const DL_SymmetricEncryptionAlgorithm & GetSymmetricEncryptionAlgorithm() const		{return Singleton<CPP_TYPENAME SCHEME_OPTIONS::SymmetricEncryptionAlgorithm>().Ref();}	HashIdentifier GetHashIdentifier() const		{return HashIdentifier();}	const PK_SignatureMessageEncodingMethod & GetMessageEncodingInterface() const 		{return Singleton<CPP_TYPENAME SCHEME_OPTIONS::MessageEncodingMethod>().Ref();}};//! _template <class SCHEME_OPTIONS>class DL_SignerImpl : public DL_ObjectImpl<DL_SignerBase<typename SCHEME_OPTIONS::Element>, SCHEME_OPTIONS, typename SCHEME_OPTIONS::PrivateKey>{public:	PK_MessageAccumulator * NewSignatureAccumulator(RandomNumberGenerator &rng) const	{		std::auto_ptr<PK_MessageAccumulatorBase> p(new PK_MessageAccumulatorImpl<CPP_TYPENAME SCHEME_OPTIONS::HashFunction>);		this->RestartMessageAccumulator(rng, *p);

⌨️ 快捷键说明

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