📄 pubkey.h
字号:
typedef T Element;
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(PublicElement);
}
void AssignFrom(const NameValuePairs &source);
// non-inherited
virtual const Element & GetPublicElement() const {return GetPublicPrecomputation().GetBase(this->GetAbstractGroupParameters().GetGroupPrecomputation());}
virtual void SetPublicElement(const Element &y) {AccessPublicPrecomputation().SetBase(this->GetAbstractGroupParameters().GetGroupPrecomputation(), y);}
virtual Element ExponentiatePublicElement(const Integer &exponent) const
{
const DL_GroupParameters<T> ¶ms = this->GetAbstractGroupParameters();
return GetPublicPrecomputation().Exponentiate(params.GetGroupPrecomputation(), exponent);
}
virtual Element CascadeExponentiateBaseAndPublicElement(const Integer &baseExp, const Integer &publicExp) const
{
const DL_GroupParameters<T> ¶ms = this->GetAbstractGroupParameters();
return params.GetBasePrecomputation().CascadeExponentiate(params.GetGroupPrecomputation(), baseExp, GetPublicPrecomputation(), publicExp);
}
virtual const DL_FixedBasePrecomputation<T> & GetPublicPrecomputation() const =0;
virtual DL_FixedBasePrecomputation<T> & AccessPublicPrecomputation() =0;
};
//! interface for DL private keys
template <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 BERDecodeKey(BufferedTransformation &bt)
{m_x.BERDecode(bt);}
void DEREncodeKey(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 algorithms
template <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 unsigned int RLen(const DL_GroupParameters<T> ¶ms) const
{return params.GetSubgroupOrder().ByteCount();}
virtual unsigned int SLen(const DL_GroupParameters<T> ¶ms) const
{return params.GetSubgroupOrder().ByteCount();}
};
//! interface for DL key agreement algorithms
template <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 cryptosystems
template <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, unsigned int derivedLength, const T &agreedElement, const T &ephemeralPublicKey, const NameValuePairs &derivationParams) const =0;
};
//! interface for symmetric encryption algorithms used in DL cryptosystems
class CRYPTOPP_NO_VTABLE DL_SymmetricEncryptionAlgorithm
{
public:
virtual bool ParameterSupported(const char *name) const {return false;}
virtual unsigned int GetSymmetricKeyLength(unsigned int plaintextLength) const =0;
virtual unsigned int GetSymmetricCiphertextLength(unsigned int plaintextLength) const =0;
virtual unsigned int GetMaxSymmetricPlaintextLength(unsigned int ciphertextLength) const =0;
virtual void SymmetricEncrypt(RandomNumberGenerator &rng, const byte *key, const byte *plaintext, unsigned int plaintextLength, byte *ciphertext, const NameValuePairs ¶meters) const =0;
virtual DecodingResult SymmetricDecrypt(const byte *key, const byte *ciphertext, unsigned int 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:
unsigned int SignatureLength() const
{
return GetSignatureAlgorithm().RLen(this->GetAbstractGroupParameters())
+ GetSignatureAlgorithm().SLen(this->GetAbstractGroupParameters());
}
unsigned int MaxRecoverableLength() const
{return GetMessageEncodingInterface().MaxRecoverableLength(0, GetHashIdentifier().second, GetDigestSize());}
unsigned int MaxRecoverableLengthFromSignatureLength(unsigned int signatureLength) const
{assert(false); return 0;} // TODO
bool IsProbabilistic() const
{return true;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -