📄 pubkey.h
字号:
//! interface for DL private keystemplate <class T>class CRYPTOPP_NO_VTABLE DL_PrivateKey : public DL_Key<T>{ typedef DL_PrivateKey<T> ThisClass;public: typedef T Element; void MakePublicKey(DL_PublicKey<T> &pub) const { pub.AccessAbstractGroupParameters().AssignFrom(this->GetAbstractGroupParameters()); pub.SetPublicElement(this->GetAbstractGroupParameters().ExponentiateBase(GetPrivateExponent())); } bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const { return GetValueHelper(this, name, valueType, pValue, &this->GetAbstractGroupParameters()) CRYPTOPP_GET_FUNCTION_ENTRY(PrivateExponent); } void AssignFrom(const NameValuePairs &source) { this->AccessAbstractGroupParameters().AssignFrom(source); AssignFromHelper(this, source) CRYPTOPP_SET_FUNCTION_ENTRY(PrivateExponent); } virtual const Integer & GetPrivateExponent() const =0; virtual void SetPrivateExponent(const Integer &x) =0;};template <class T>void DL_PublicKey<T>::AssignFrom(const NameValuePairs &source){ DL_PrivateKey<T> *pPrivateKey = NULL; if (source.GetThisPointer(pPrivateKey)) pPrivateKey->MakePublicKey(*this); else { this->AccessAbstractGroupParameters().AssignFrom(source); AssignFromHelper(this, source) CRYPTOPP_SET_FUNCTION_ENTRY(PublicElement); }}class OID;//! _template <class PK, class GP, class O = OID>class DL_KeyImpl : public PK{public: typedef GP GroupParameters; O GetAlgorithmID() const {return GetGroupParameters().GetAlgorithmID();}// void BERDecode(BufferedTransformation &bt)// {PK::BERDecode(bt);}// void DEREncode(BufferedTransformation &bt) const// {PK::DEREncode(bt);} bool BERDecodeAlgorithmParameters(BufferedTransformation &bt) {AccessGroupParameters().BERDecode(bt); return true;} bool DEREncodeAlgorithmParameters(BufferedTransformation &bt) const {GetGroupParameters().DEREncode(bt); return true;} const GP & GetGroupParameters() const {return m_groupParameters;} GP & AccessGroupParameters() {return m_groupParameters;}private: GP m_groupParameters;};class X509PublicKey;class PKCS8PrivateKey;//! _template <class GP>class DL_PrivateKeyImpl : public DL_PrivateKey<CPP_TYPENAME GP::Element>, public DL_KeyImpl<PKCS8PrivateKey, GP>{public: typedef typename GP::Element Element; // GeneratableCryptoMaterial bool Validate(RandomNumberGenerator &rng, unsigned int level) const { bool pass = GetAbstractGroupParameters().Validate(rng, level); const Integer &q = GetAbstractGroupParameters().GetSubgroupOrder(); const Integer &x = GetPrivateExponent(); pass = pass && x.IsPositive() && x < q; if (level >= 1) pass = pass && Integer::Gcd(x, q) == Integer::One(); return pass; } bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const { return GetValueHelper<DL_PrivateKey<Element> >(this, name, valueType, pValue).Assignable(); } void AssignFrom(const NameValuePairs &source) { AssignFromHelper<DL_PrivateKey<Element> >(this, source); } void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs ¶ms) { if (!params.GetThisObject(this->AccessGroupParameters())) this->AccessGroupParameters().GenerateRandom(rng, params);// std::pair<const byte *, int> seed; Integer x(rng, Integer::One(), GetAbstractGroupParameters().GetMaxExponent());// Integer::ANY, Integer::Zero(), Integer::One(),// params.GetValue("DeterministicKeyGenerationSeed", seed) ? &seed : NULL); SetPrivateExponent(x); } bool SupportsPrecomputation() const {return true;} void Precompute(unsigned int precomputationStorage=16) {AccessAbstractGroupParameters().Precompute(precomputationStorage);} void LoadPrecomputation(BufferedTransformation &storedPrecomputation) {AccessAbstractGroupParameters().LoadPrecomputation(storedPrecomputation);} void SavePrecomputation(BufferedTransformation &storedPrecomputation) const {GetAbstractGroupParameters().SavePrecomputation(storedPrecomputation);} // DL_Key const DL_GroupParameters<Element> & GetAbstractGroupParameters() const {return this->GetGroupParameters();} DL_GroupParameters<Element> & AccessAbstractGroupParameters() {return this->AccessGroupParameters();} // DL_PrivateKey const Integer & GetPrivateExponent() const {return m_x;} void SetPrivateExponent(const Integer &x) {m_x = x;} // PKCS8PrivateKey void BERDecodePrivateKey(BufferedTransformation &bt, bool, size_t) {m_x.BERDecode(bt);} void DEREncodePrivateKey(BufferedTransformation &bt) const {m_x.DEREncode(bt);}private: Integer m_x;};//! _template <class BASE, class SIGNATURE_SCHEME>class DL_PrivateKey_WithSignaturePairwiseConsistencyTest : public BASE{public: void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs ¶ms) { BASE::GenerateRandom(rng, params); if (FIPS_140_2_ComplianceEnabled()) { typename SIGNATURE_SCHEME::Signer signer(*this); typename SIGNATURE_SCHEME::Verifier verifier(signer); SignaturePairwiseConsistencyTest_FIPS_140_Only(signer, verifier); } }};//! _template <class GP>class DL_PublicKeyImpl : public DL_PublicKey<typename GP::Element>, public DL_KeyImpl<X509PublicKey, GP>{public: typedef typename GP::Element Element; // CryptoMaterial bool Validate(RandomNumberGenerator &rng, unsigned int level) const { bool pass = GetAbstractGroupParameters().Validate(rng, level); pass = pass && GetAbstractGroupParameters().ValidateElement(level, this->GetPublicElement(), &GetPublicPrecomputation()); return pass; } bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const { return GetValueHelper<DL_PublicKey<Element> >(this, name, valueType, pValue).Assignable(); } void AssignFrom(const NameValuePairs &source) { AssignFromHelper<DL_PublicKey<Element> >(this, source); } bool SupportsPrecomputation() const {return true;} void Precompute(unsigned int precomputationStorage=16) { AccessAbstractGroupParameters().Precompute(precomputationStorage); AccessPublicPrecomputation().Precompute(GetAbstractGroupParameters().GetGroupPrecomputation(), GetAbstractGroupParameters().GetSubgroupOrder().BitCount(), precomputationStorage); } void LoadPrecomputation(BufferedTransformation &storedPrecomputation) { AccessAbstractGroupParameters().LoadPrecomputation(storedPrecomputation); AccessPublicPrecomputation().Load(GetAbstractGroupParameters().GetGroupPrecomputation(), storedPrecomputation); } void SavePrecomputation(BufferedTransformation &storedPrecomputation) const { GetAbstractGroupParameters().SavePrecomputation(storedPrecomputation); GetPublicPrecomputation().Save(GetAbstractGroupParameters().GetGroupPrecomputation(), storedPrecomputation); } // DL_Key const DL_GroupParameters<Element> & GetAbstractGroupParameters() const {return this->GetGroupParameters();} DL_GroupParameters<Element> & AccessAbstractGroupParameters() {return this->AccessGroupParameters();} // DL_PublicKey const DL_FixedBasePrecomputation<Element> & GetPublicPrecomputation() const {return m_ypc;} DL_FixedBasePrecomputation<Element> & AccessPublicPrecomputation() {return m_ypc;} // non-inherited bool operator==(const DL_PublicKeyImpl<GP> &rhs) const {return this->GetGroupParameters() == rhs.GetGroupParameters() && this->GetPublicElement() == rhs.GetPublicElement();}private: typename GP::BasePrecomputation m_ypc;};//! interface for Elgamal-like signature algorithmstemplate <class T>class CRYPTOPP_NO_VTABLE DL_ElgamalLikeSignatureAlgorithm{public: virtual void Sign(const DL_GroupParameters<T> ¶ms, const Integer &privateKey, const Integer &k, const Integer &e, Integer &r, Integer &s) const =0; virtual bool Verify(const DL_GroupParameters<T> ¶ms, const DL_PublicKey<T> &publicKey, const Integer &e, const Integer &r, const Integer &s) const =0; virtual Integer RecoverPresignature(const DL_GroupParameters<T> ¶ms, const DL_PublicKey<T> &publicKey, const Integer &r, const Integer &s) const {throw NotImplemented("DL_ElgamalLikeSignatureAlgorithm: this signature scheme does not support message recovery");} virtual size_t RLen(const DL_GroupParameters<T> ¶ms) const {return params.GetSubgroupOrder().ByteCount();} virtual size_t SLen(const DL_GroupParameters<T> ¶ms) const {return params.GetSubgroupOrder().ByteCount();}};//! interface for DL key agreement algorithmstemplate <class T>class CRYPTOPP_NO_VTABLE DL_KeyAgreementAlgorithm{public: typedef T Element; virtual Element AgreeWithEphemeralPrivateKey(const DL_GroupParameters<Element> ¶ms, const DL_FixedBasePrecomputation<Element> &publicPrecomputation, const Integer &privateExponent) const =0; virtual Element AgreeWithStaticPrivateKey(const DL_GroupParameters<Element> ¶ms, const Element &publicElement, bool validateOtherPublicKey, const Integer &privateExponent) const =0;};//! interface for key derivation algorithms used in DL cryptosystemstemplate <class T>class CRYPTOPP_NO_VTABLE DL_KeyDerivationAlgorithm{public: virtual bool ParameterSupported(const char *name) const {return false;} virtual void Derive(const DL_GroupParameters<T> &groupParams, byte *derivedKey, size_t derivedLength, const T &agreedElement, const T &ephemeralPublicKey, const NameValuePairs &derivationParams) const =0;};//! interface for symmetric encryption algorithms used in DL cryptosystemsclass CRYPTOPP_NO_VTABLE DL_SymmetricEncryptionAlgorithm{public: virtual bool ParameterSupported(const char *name) const {return false;} virtual size_t GetSymmetricKeyLength(size_t plaintextLength) const =0; virtual size_t GetSymmetricCiphertextLength(size_t plaintextLength) const =0; virtual size_t GetMaxSymmetricPlaintextLength(size_t ciphertextLength) const =0; virtual void SymmetricEncrypt(RandomNumberGenerator &rng, const byte *key, const byte *plaintext, size_t plaintextLength, byte *ciphertext, const NameValuePairs ¶meters) const =0; virtual DecodingResult SymmetricDecrypt(const byte *key, const byte *ciphertext, size_t ciphertextLength, byte *plaintext, const NameValuePairs ¶meters) const =0;};//! _template <class KI>class CRYPTOPP_NO_VTABLE DL_Base{protected: typedef KI KeyInterface; typedef typename KI::Element Element; const DL_GroupParameters<Element> & GetAbstractGroupParameters() const {return GetKeyInterface().GetAbstractGroupParameters();} DL_GroupParameters<Element> & AccessAbstractGroupParameters() {return AccessKeyInterface().AccessAbstractGroupParameters();} virtual KeyInterface & AccessKeyInterface() =0; virtual const KeyInterface & GetKeyInterface() const =0;};//! _template <class INTERFACE, class KEY_INTERFACE>class CRYPTOPP_NO_VTABLE DL_SignatureSchemeBase : public INTERFACE, public DL_Base<KEY_INTERFACE>{public: size_t SignatureLength() const { return GetSignatureAlgorithm().RLen(this->GetAbstractGroupParameters()) + GetSignatureAlgorithm().SLen(this->GetAbstractGroupParameters()); } size_t MaxRecoverableLength() const {return GetMessageEncodingInterface().MaxRecoverableLength(0, GetHashIdentifier().second, GetDigestSize());} size_t MaxRecoverableLengthFromSignatureLength(size_t signatureLength) const {assert(false); return 0;} // TODO bool IsProbabilistic() const {return true;} bool AllowNonrecoverablePart() const {return GetMessageEncodingInterface().AllowNonrecoverablePart();} bool RecoverablePartFirst() const {return GetMessageEncodingInterface().RecoverablePartFirst();}protected: size_t MessageRepresentativeLength() const {return BitsToBytes(MessageRepresentativeBitLength());} size_t MessageRepresentativeBitLength() const {return this->GetAbstractGroupParameters().GetSubgroupOrder().BitCount();} virtual const DL_ElgamalLikeSignatureAlgorithm<CPP_TYPENAME KEY_INTERFACE::Element> & GetSignatureAlgorithm() const =0; virtual const PK_SignatureMessageEncodingMethod & GetMessageEncodingInterface() const =0; virtual HashIdentifier GetHashIdentifier() const =0; virtual size_t GetDigestSize() const =0;};//! _template <class T>class CRYPTOPP_NO_VTABLE DL_SignerBase : public DL_SignatureSchemeBase<PK_Signer, DL_PrivateKey<T> >{public: // for validation testing void RawSign(const Integer &k, const Integer &e, Integer &r, Integer &s) const { const DL_ElgamalLikeSignatureAlgorithm<T> &alg = this->GetSignatureAlgorithm(); const DL_GroupParameters<T> ¶ms = this->GetAbstractGroupParameters(); const DL_PrivateKey<T> &key = this->GetKeyInterface(); r = params.ConvertElementToInteger(params.ExponentiateBase(k)); alg.Sign(params, key.GetPrivateExponent(), k, e, r, s); } void InputRecoverableMessage(PK_MessageAccumulator &messageAccumulator, const byte *recoverableMessage, size_t recoverableMessageLength) const
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -