dh.h
Go to the documentation of this file.
// dh.h - written and placed in the public domain by Wei Dai
//! \file dh.h
//! \brief Classes for Diffie-Hellman key exchange
#ifndef CRYPTOPP_DH_H
#define CRYPTOPP_DH_H
#include "cryptlib.h"
#include "gfpcrypt.h"
#include "algebra.h"
NAMESPACE_BEGIN(CryptoPP)
//! \class DH_Domain
//! \brief Diffie-Hellman domain
//! \tparam GROUP_PARAMETERS group parameters
//! \tparam COFACTOR_OPTION \ref CofactorMultiplicationOption "cofactor multiplication option"
//! \details A Diffie-Hellman domain is a set of parameters that must be shared
//! by two parties in a key agreement protocol, along with the algorithms
//! for generating key pairs and deriving agreed values.
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;
//! \brief Construct a Diffie-Hellman domain
DH_Domain() {}
//! \brief Construct a Diffie-Hellman domain
//! \param params group parameters and options
DH_Domain(const GroupParameters &params)
: m_groupParameters(params) {}
//! \brief Construct a Diffie-Hellman domain
//! \param bt BufferedTransformation with group parameters and options
DH_Domain(BufferedTransformation &bt)
{m_groupParameters.BERDecode(bt);}
//! \brief Construct a Diffie-Hellman domain
//! \tparam T2 template parameter used as a constructor parameter
//! \param v1 RandomNumberGenerator derived class
//! \param v2 second parameter
//! \details v1 and v2 are passed directly to the GROUP_PARAMETERS object.
template <class T2>
DH_Domain(RandomNumberGenerator &v1, const T2 &v2)
{m_groupParameters.Initialize(v1, v2);}
//! \brief Construct a Diffie-Hellman domain
//! \tparam T2 template parameter used as a constructor parameter
//! \tparam T3 template parameter used as a constructor parameter
//! \param v1 RandomNumberGenerator derived class
//! \param v2 second parameter
//! \param v3 third parameter
//! \details v1, v2 and v3 are passed directly to the GROUP_PARAMETERS object.
template <class T2, class T3>
DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3)
{m_groupParameters.Initialize(v1, v2, v3);}
//! \brief Construct a Diffie-Hellman domain
//! \tparam T2 template parameter used as a constructor parameter
//! \tparam T3 template parameter used as a constructor parameter
//! \tparam T4 template parameter used as a constructor parameter
//! \param v1 RandomNumberGenerator derived class
//! \param v2 second parameter
//! \param v3 third parameter
//! \param v4 fourth parameter
//! \details v1, v2, v3 and v4 are passed directly to the GROUP_PARAMETERS object.
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);}
//! \brief Construct a Diffie-Hellman domain
//! \tparam T1 template parameter used as a constructor parameter
//! \tparam T2 template parameter used as a constructor parameter
//! \param v1 first parameter
//! \param v2 second parameter
//! \details v1 and v2 are passed directly to the GROUP_PARAMETERS object.
template <class T1, class T2>
DH_Domain(const T1 &v1, const T2 &v2)
{m_groupParameters.Initialize(v1, v2);}
//! \brief Construct a Diffie-Hellman domain
//! \tparam T1 template parameter used as a constructor parameter
//! \tparam T2 template parameter used as a constructor parameter
//! \tparam T3 template parameter used as a constructor parameter
//! \param v1 first parameter
//! \param v2 second parameter
//! \param v3 third parameter
//! \details v1, v2 and v3 are passed directly to the GROUP_PARAMETERS object.
template <class T1, class T2, class T3>
DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3)
{m_groupParameters.Initialize(v1, v2, v3);}
//! \brief Construct a Diffie-Hellman domain
//! \tparam T1 template parameter used as a constructor parameter
//! \tparam T2 template parameter used as a constructor parameter
//! \tparam T3 template parameter used as a constructor parameter
//! \tparam T4 template parameter used as a constructor parameter
//! \param v1 first parameter
//! \param v2 second parameter
//! \param v3 third parameter
//! \param v4 fourth parameter
//! \details v1, v2, v3 and v4 are passed directly to the GROUP_PARAMETERS object.
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);}
//! \brief Retrieves the group parameters for this domain
//! \return the group parameters for this domain as a const reference
const GroupParameters & GetGroupParameters() const {return m_groupParameters;}
//! \brief Retrieves the group parameters for this domain
//! \return the group parameters for this domain as a non-const reference
GroupParameters & AccessGroupParameters() {return m_groupParameters;}
//! \brief Generate a public key from a private key in this domain
//! \param rng RandomNumberGenerator derived class
//! \param privateKey byte buffer with the previously generated private key
//! \param publicKey byte buffer for the generated public key in this domain
//! \details If using a FIPS 140-2 validated library on Windows, then this class will perform
//! a self test to ensure the key pair is pairwise consistent. Non-FIPS and non-Windows
//! builds of the library do not provide FIPS validated cryptography, so the code should be
//! removed by the optimizer.
//! \pre <tt>COUNTOF(publicKey) == PublicKeyLength()</tt>
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();}
#ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
virtual ~DH_Domain() {}
#endif
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::GetGroupParameters
const GroupParameters & GetGroupParameters() const
Retrieves the group parameters for this domain.
Definition: dh.h:117
DL_KeyAgreementAlgorithm_DH
Definition: pubkey.h:1972
Singleton
Restricts the instantiation of a class to one static object without locks.
Definition: misc.h:264
DL_GroupParameters
Interface for Discrete Log (DL) group parameters.
Definition: pubkey.h:752
DH_Domain::DH_Domain
DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3, const T4 &v4)
Construct a Diffie-Hellman domain.
Definition: dh.h:76
DH_Domain::DH_Domain
DH_Domain(BufferedTransformation &bt)
Construct a Diffie-Hellman domain.
Definition: dh.h:43
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1186
DH_Domain::AlgorithmName
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: dh.h:154
SecByteBlock
SecBlock<byte> typedef.
Definition: secblock.h:731
DL_SimpleKeyAgreementDomainBase< GROUP_PARAMETERS::Element >::GeneratePrivateKey
void GeneratePrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
Definition: pubkey.h:1906
algebra.h
Classes for performing mathematics over different fields.
DL_SimpleKeyAgreementDomainBase< GROUP_PARAMETERS::Element >::AgreedValueLength
unsigned int AgreedValueLength() const
Definition: pubkey.h:1902
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1352
DH_Domain::DH_Domain
DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4)
Construct a Diffie-Hellman domain.
Definition: dh.h:112
FIPS_140_2_ComplianceEnabled
bool FIPS_140_2_ComplianceEnabled()
Determines whether the library provides FIPS validated cryptography.
Definition: fips140.cpp:29
DH_Domain::DH_Domain
DH_Domain(RandomNumberGenerator &v1, const T2 &v2)
Construct a Diffie-Hellman domain.
Definition: dh.h:52
DL_SimpleKeyAgreementDomainBase::GeneratePublicKey
void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
Generate a public key from a private key in this domain.
Definition: pubkey.h:1912
DH_Domain::GeneratePublicKey
void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
Generate a public key from a private key in this domain.
Definition: dh.h:131
SelfTestFailure
Exception thrown when a crypto algorithm is used after a self test fails.
Definition: fips140.h:23
gfpcrypt.h
Implementation of schemes based on DL over GF(p)
DL_SimpleKeyAgreementDomainBase< GROUP_PARAMETERS::Element >::PublicKeyLength
unsigned int PublicKeyLength() const
Definition: pubkey.h:1904
DL_SimpleKeyAgreementDomainBase< GROUP_PARAMETERS::Element >::Agree
bool Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey=true) const
Definition: pubkey.h:1921
DH_Domain::DH_Domain
DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3)
Construct a Diffie-Hellman domain.
Definition: dh.h:98
DH_Domain
Diffie-Hellman domain.
Definition: dh.h:23
DH_Domain::DH_Domain
DH_Domain(const GroupParameters &params)
Construct a Diffie-Hellman domain.
Definition: dh.h:38
DH
DH_Domain< DL_GroupParameters_GFP_DefaultSafePrime > DH
Diffie-Hellman in GF(p) with key validation
Definition: dh.h:172
DH_Domain::DH_Domain
DH_Domain()
Construct a Diffie-Hellman domain.
Definition: dh.h:34
DH_Domain::DH_Domain
DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3)
Construct a Diffie-Hellman domain.
Definition: dh.h:63
DL_SimpleKeyAgreementDomainBase< GROUP_PARAMETERS::Element >::PrivateKeyLength
unsigned int PrivateKeyLength() const
Definition: pubkey.h:1903
CryptoPP
Crypto++ library namespace.
DH_Domain::AccessGroupParameters
GroupParameters & AccessGroupParameters()
Retrieves the group parameters for this domain.
Definition: dh.h:120
DL_KeyAgreementAlgorithm
Interface for DL key agreement algorithms.
Definition: pubkey.h:1320
DL_SimpleKeyAgreementDomainBase
Discrete Log (DL) simple key agreement base implementation.
Definition: pubkey.h:1892
DH_Domain::DH_Domain
DH_Domain(const T1 &v1, const T2 &v2)
Construct a Diffie-Hellman domain.
Definition: dh.h:86