dh.h
Go to the documentation of this file.
#ifndef CRYPTOPP_DH_H
#define CRYPTOPP_DH_H
/** \file
*/
#include "gfpcrypt.h"
NAMESPACE_BEGIN(CryptoPP)
//! ,
template <class GROUP_PARAMETERS, class COFACTOR_OPTION = CPP_TYPENAME GROUP_PARAMETERS::DefaultCofactorOption>
class DH_Domain : public DL_SimpleKeyAgreementDomainBase<typename GROUP_PARAMETERS::Element>
{
typedef DL_SimpleKeyAgreementDomainBase<typename GROUP_PARAMETERS::Element> Base;
public:
typedef GROUP_PARAMETERS GroupParameters;
typedef typename GroupParameters::Element Element;
typedef DL_KeyAgreementAlgorithm_DH<Element, COFACTOR_OPTION> DH_Algorithm;
typedef DH_Domain<GROUP_PARAMETERS, COFACTOR_OPTION> Domain;
DH_Domain() {}
DH_Domain(const GroupParameters &params)
: m_groupParameters(params) {}
DH_Domain(BufferedTransformation &bt)
{m_groupParameters.BERDecode(bt);}
template <class T2>
DH_Domain(RandomNumberGenerator &v1, const T2 &v2)
{m_groupParameters.Initialize(v1, v2);}
template <class T2, class T3>
DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3)
{m_groupParameters.Initialize(v1, v2, v3);}
template <class T2, class T3, class T4>
DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3, const T4 &v4)
{m_groupParameters.Initialize(v1, v2, v3, v4);}
template <class T1, class T2>
DH_Domain(const T1 &v1, const T2 &v2)
{m_groupParameters.Initialize(v1, v2);}
template <class T1, class T2, class T3>
DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3)
{m_groupParameters.Initialize(v1, v2, v3);}
template <class T1, class T2, class T3, class T4>
DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4)
{m_groupParameters.Initialize(v1, v2, v3, v4);}
const GroupParameters & GetGroupParameters() const {return m_groupParameters;}
GroupParameters & AccessGroupParameters() {return m_groupParameters;}
void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
{
Base::GeneratePublicKey(rng, privateKey, publicKey);
if (FIPS_140_2_ComplianceEnabled())
{
SecByteBlock privateKey2(this->PrivateKeyLength());
this->GeneratePrivateKey(rng, privateKey2);
SecByteBlock publicKey2(this->PublicKeyLength());
Base::GeneratePublicKey(rng, privateKey2, publicKey2);
SecByteBlock agreedValue(this->AgreedValueLength()), agreedValue2(this->AgreedValueLength());
bool agreed1 = this->Agree(agreedValue, privateKey, publicKey2);
bool agreed2 = this->Agree(agreedValue2, privateKey2, publicKey);
if (!agreed1 || !agreed2 || agreedValue != agreedValue2)
throw SelfTestFailure(this->AlgorithmName() + ": pairwise consistency test failed");
}
}
static std::string CRYPTOPP_API StaticAlgorithmName()
{return GroupParameters::StaticAlgorithmNamePrefix() + DH_Algorithm::StaticAlgorithmName();}
std::string AlgorithmName() const {return StaticAlgorithmName();}
private:
const DL_KeyAgreementAlgorithm<Element> & GetKeyAgreementAlgorithm() const
{return Singleton<DH_Algorithm>().Ref();}
DL_GroupParameters<Element> & AccessAbstractGroupParameters()
{return m_groupParameters;}
GroupParameters m_groupParameters;
};
CRYPTOPP_DLL_TEMPLATE_CLASS DH_Domain<DL_GroupParameters_GFP_DefaultSafePrime>;
//! <a href="http://www.weidai.com/scan-mirror/ka.html#DH">Diffie-Hellman</a> in GF(p) with key validation
typedef DH_Domain<DL_GroupParameters_GFP_DefaultSafePrime> DH;
NAMESPACE_END
#endif
DH_Domain::GeneratePublicKey
void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
generate public key
Definition: dh.h:58
DL_KeyAgreementAlgorithm_DH
DH key agreement algorithm.
Definition: pubkey.h:1434
Singleton
Definition: misc.h:109
DH_Domain::AlgorithmName
std::string AlgorithmName() const
returns name of this algorithm, not universally implemented yet
Definition: dh.h:81
DL_GroupParameters
interface for DL group parameters
Definition: pubkey.h:521
AsymmetricAlgorithm::BERDecode
void BERDecode(BufferedTransformation &bt)
for backwards compatibility, calls AccessMaterial().Load(bt)
Definition: cryptlib.h:1142
SecBlock< byte >
RandomNumberGenerator
interface for random number generators
Definition: cryptlib.h:668
BufferedTransformation
interface for buffered transformations
Definition: cryptlib.h:770
FIPS_140_2_ComplianceEnabled
bool FIPS_140_2_ComplianceEnabled()
returns whether FIPS 140-2 compliance features were enabled at compile time
SelfTestFailure
exception thrown when a crypto algorithm is used after a self test fails
Definition: fips140.h:14
gfpcrypt.h
Implementation of schemes based on DL over GF(p)
DH_Domain
,
Definition: dh.h:13
DH
DH_Domain< DL_GroupParameters_GFP_DefaultSafePrime > DH
Diffie-Hellman in GF(p) with key validation
Definition: dh.h:95
DL_KeyAgreementAlgorithm
interface for DL key agreement algorithms
Definition: pubkey.h:915
DL_SimpleKeyAgreementDomainBase
_
Definition: pubkey.h:1376