📄 pubkey.h
字号:
// 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(AccessGroupParameters()))
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 GetGroupParameters();}
DL_GroupParameters<Element> & AccessAbstractGroupParameters() {return 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(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, 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 GetGroupParameters();}
DL_GroupParameters<Element> & AccessAbstractGroupParameters() {return 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 GetGroupParameters() == rhs.GetGroupParameters() && GetPublicElement() == rhs.GetPublicElement();}
private:
typename GP::BasePrecomputation m_ypc;
};
//! .
template <class T>
class DL_ElgamalLikeSignatureAlgorithm
{
public:
virtual Integer EncodeDigest(unsigned int modulusBits, const byte *digest, unsigned int digestLength) const =0;
virtual bool 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 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();}
};
//! .
template <class T>
class 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;
};
//! .
template <class T>
class DL_KeyDerivationAlgorithm
{
public:
virtual void Derive(const DL_GroupParameters<T> ¶ms, byte *derivedKey, unsigned int derivedLength, const T &agreedElement, const T &ephemeralPublicKey) const =0;
};
//! .
class DL_SymmetricEncryptionAlgorithm
{
public:
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 =0;
virtual DecodingResult SymmetricDecrypt(const byte *key, const byte *cipherText, unsigned int cipherTextLength, byte *plainText) const =0;
};
//! .
template <class KI>
class 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 DL_DigestSignatureSystemBase : public INTERFACE, public DL_Base<KEY_INTERFACE>
{
public:
unsigned int MaxDigestLength() const {return UINT_MAX;}
unsigned int DigestSignatureLength() const
{
return GetSignatureAlgorithm().RLen(GetAbstractGroupParameters())
+ GetSignatureAlgorithm().SLen(GetAbstractGroupParameters());
}
protected:
virtual const DL_ElgamalLikeSignatureAlgorithm<CPP_TYPENAME KEY_INTERFACE::Element> & GetSignatureAlgorithm() const =0;
};
//! .
template <class T>
class DL_DigestSignerBase : public DL_DigestSignatureSystemBase<DigestSigner, 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 = GetSignatureAlgorithm();
const DL_GroupParameters<T> ¶ms = GetAbstractGroupParameters();
const DL_PrivateKey<T> &key = GetKeyInterface();
alg.Sign(params, key.GetPrivateExponent(), k, e, r, s);
}
void SignDigest(RandomNumberGenerator &rng, const byte *digest, unsigned int digestLength, byte *signature) const
{
const DL_ElgamalLikeSignatureAlgorithm<T> &alg = GetSignatureAlgorithm();
const DL_GroupParameters<T> ¶ms = GetAbstractGroupParameters();
const DL_PrivateKey<T> &key = GetKeyInterface();
GetMaterial().DoQuickSanityCheck();
const Integer &q = params.GetSubgroupOrder();
Integer e = alg.EncodeDigest(q.BitCount(), digest, digestLength);
Integer k, r, s;
do {k.Randomize(rng, 1, params.GetSubgroupOrder()-1);}
while (!alg.Sign(params, key.GetPrivateExponent(), k, e, r, s));
unsigned int rLen = alg.RLen(params);
r.Encode(signature, rLen);
s.Encode(signature+rLen, alg.SLen(params));
}
};
//! .
template <class T>
class DL_DigestVerifierBase : public DL_DigestSignatureSystemBase<DigestVerifier, DL_PublicKey<T> >
{
public:
bool VerifyDigest(const byte *digest, unsigned int digestLength, const byte *signature) const
{
const DL_ElgamalLikeSignatureAlgorithm<T> &alg = GetSignatureAlgorithm();
const DL_GroupParameters<T> ¶ms = GetAbstractGroupParameters();
const DL_PublicKey<T> &key = GetKeyInterface();
GetMaterial().DoQuickSanityCheck();
const Integer &q = params.GetSubgroupOrder();
Integer e = alg.EncodeDigest(q.BitCount(), digest, digestLength);
unsigned int rLen = alg.RLen(params);
Integer r(signature, rLen);
Integer s(signature+rLen, alg.SLen(params));
return alg.Verify(params, key, e, r, s);
}
};
//! .
template <class PK, class KI>
class DL_CryptoSystemBase : public PK, public DL_Base<KI>
{
public:
typedef typename DL_Base<KI>::Element Element;
unsigned int MaxPlaintextLength(unsigned int cipherTextLength) const
{
unsigned int minLen = GetAbstractGroupParameters().GetEncodedElementSize(true);
return cipherTextLength < minLen ? 0 : GetSymmetricEncryptionAlgorithm().GetMaxSymmetricPlaintextLength(cipherTextLength - minLen);
}
unsigned int CiphertextLength(unsigned int plainTextLength) const
{
unsigned int len = GetSymmetricEncryptionAlgorithm().GetSymmetricCiphertextLength(plainTextLength);
return len == 0 ? 0 : GetAbstractGroupParameters().GetEncodedElementSize(true) + len;
}
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 PK = PK_Decryptor>
class DL_DecryptorBase : public DL_CryptoSystemBase<PK, DL_PrivateKey<T> >
{
public:
typedef T Element;
DecodingResult Decrypt(const byte *cipherText, unsigned int cipherTextLength, byte *plainText) const
{
try
{
const DL_KeyAgreementAlgorithm<T> &agreeAlg = GetKeyAgreementAlgorithm();
const DL_KeyDerivationAlgorithm<T> &derivAlg = GetKeyDerivationAlgorithm();
const DL_SymmetricEncryptionAlgorithm &encAlg = GetSymmetricEncryptionAlgorithm();
const DL_GroupParameters<T> ¶ms = GetAbstractGroupParameters();
const DL_PrivateKey<T> &key = GetKeyInterface();
Element q = params.DecodeElement(cipherText, true);
unsigned int 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);
return encAlg.SymmetricDecrypt(derivedKey, cipherText, cipherTextLength, plainText);
}
catch (DL_BadElement &)
{
return DecodingResult();
}
}
};
//! .
template <class T, class PK = PK_Encryptor>
class DL_EncryptorBase : public DL_CryptoSystemBase<PK, DL_PublicKey<T> >
{
public:
typedef T Element;
void Encrypt(RandomNumberGenerator &rng, const byte *plainText, unsigned int plainTextLength, byte *cipherText) const
{
const DL_KeyAgreementAlgorithm<T> &agreeAlg = GetKeyAgreementAlgorithm();
const DL_KeyDerivationAlgorithm<T> &derivAlg = GetKeyDerivationAlgorithm();
const DL_SymmetricEncryptionAlgorithm &encAlg = GetSymmetricEncryptionAlgorithm();
const DL_GroupParameters<T> ¶ms = GetAbstractGroupParameters();
const DL_PublicKey<T> &key = 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);
encAlg.SymmetricEncrypt(rng, derivedKey, plainText, plainTextLength, cipherText);
}
};
//! .
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>
struct DL_SignatureSchemeOptions : public DL_KeyedSchemeOptions<T1, T2>
{
typedef T3 SignatureAlgorithm;
};
//! .
template <class T1, class T2, class T3, class T4, class T5>
struct DL_CryptoSchemeOptions : public DL_KeyedSchemeOptions<T1, T2>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -