DH Struct Reference

Diffie-Hellman in GF(p) More...

`#include <dh.h>`

Inheritance diagram for DH:

## Public Types

typedef DH_Domain< DL_GroupParameters_GFP_DefaultSafePrime > | GroupParameters
typedef GroupParameters::Element | Element
Public Types inherited from DH_Domain< DL_GroupParameters_GFP_DefaultSafePrime >
typedef DL_GroupParameters_GFP_DefaultSafePrime | GroupParameters
typedef GroupParameters::Element | Element
typedef DL_KeyAgreementAlgorithm_DH< Element, typename GROUP_PARAMETERS::DefaultCofactorOption > | DH_Algorithm
typedef DH_Domain< DL_GroupParameters_GFP_DefaultSafePrime, typename GROUP_PARAMETERS::DefaultCofactorOption > | Domain
Public Types inherited from DL_SimpleKeyAgreementDomainBase< GROUP_PARAMETERS::Element >
typedef GROUP_PARAMETERS::Element | Element

## Public Member Functions

| DH ()
| Create an uninitialized Diffie-Hellman object.
| DH (BufferedTransformation &bt)
| Initialize a Diffie-Hellman object.
| DH (const GroupParameters &params)
| Initialize a Diffie-Hellman object.
| DH (RandomNumberGenerator &rng, unsigned int modulusBits)
| Create a Diffie-Hellman object.
| DH (const Integer &p, const Integer &g)
| Initialize a Diffie-Hellman object.
| DH (const Integer &p, const Integer &q, const Integer &g)
| Initialize a Diffie-Hellman object.
void | Initialize (RandomNumberGenerator &rng, unsigned int modulusBits)
| Creates a Diffie-Hellman object.
void | Initialize (const Integer &p, const Integer &g)
| Initialize a Diffie-Hellman object.
void | Initialize (const Integer &p, const Integer &q, const Integer &g)
| Initialize a Diffie-Hellman object.
Public Member Functions inherited from DH_Domain< DL_GroupParameters_GFP_DefaultSafePrime >
| DH_Domain ()
| Construct a Diffie-Hellman domain.
| DH_Domain (const GroupParameters &params)
| Construct a Diffie-Hellman domain.
| DH_Domain (BufferedTransformation &bt)
| Construct a Diffie-Hellman domain.
| DH_Domain (RandomNumberGenerator &v1, const T2 &v2)
| Create a Diffie-Hellman domain.
| DH_Domain (RandomNumberGenerator &v1, const T2 &v2, const T3 &v3)
| Create a Diffie-Hellman domain.
| DH_Domain (RandomNumberGenerator &v1, const T2 &v2, const T3 &v3, const T4 &v4)
| Create a Diffie-Hellman domain.
| DH_Domain (const T1 &v1, const T2 &v2)
| Construct a Diffie-Hellman domain.
| DH_Domain (const T1 &v1, const T2 &v2, const T3 &v3)
| Construct a Diffie-Hellman domain.
| DH_Domain (const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4)
| Construct a Diffie-Hellman domain.
const GroupParameters & | GetGroupParameters () const
| Retrieves the group parameters for this domain.
GroupParameters & | AccessGroupParameters ()
| Retrieves the group parameters for this domain.
void | GeneratePublicKey (RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
| Generate a public key from a private key in this domain.
std::string | AlgorithmName () const
| Provides the name of this algorithm.
Public Member Functions inherited from DL_SimpleKeyAgreementDomainBase< GROUP_PARAMETERS::Element >
CryptoParameters & | AccessCryptoParameters ()
| Retrieves a reference to Crypto Parameters.
unsigned int | AgreedValueLength () const
| Provides the size of the agreed value.
unsigned int | PrivateKeyLength () const
| Provides the size of the private key.
unsigned int | PublicKeyLength () const
| Provides the size of the public key.
void | GeneratePrivateKey (RandomNumberGenerator &rng, byte *privateKey) const
| Generate private key in this domain.
void | GeneratePublicKey (RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
| Generate a public key from a private key in this domain.
bool | Agree (byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey=true) const
| Derive agreed value.
const Element & | GetGenerator () const
| Retrieves a reference to the group generator.
Public Member Functions inherited from SimpleKeyAgreementDomain
virtual void | GenerateKeyPair (RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const
| Generate a private/public key pair.
Public Member Functions inherited from KeyAgreementAlgorithm
CryptoMaterial & | AccessMaterial ()
| Retrieves a reference to Crypto Parameters.
const CryptoMaterial & | GetMaterial () const
| Retrieves a reference to Crypto Parameters.
virtual const CryptoParameters & | GetCryptoParameters () const
| Retrieves a reference to Crypto Parameters.
Public Member Functions inherited from Algorithm
| Algorithm (bool checkSelfTestStatus=true)
| Interface for all crypto algorithms.
virtual std::string | AlgorithmProvider () const
| Retrieve the provider of this algorithm.
Public Member Functions inherited from Clonable
virtual Clonable * | Clone () const
| Copies this object.

## Additional Inherited Members

Static Public Member Functions inherited from DH_Domain< DL_GroupParameters_GFP_DefaultSafePrime >
static std::string | StaticAlgorithmName ()

## Detailed Description

Diffie-Hellman in GF(p)

DH() class is a typedef of DH_Domain(). The documentation that follows does not exist. Rather the documentation was created in response to Issue 328, Diffie-Hellman example code not compiling.

Generally speaking, a DH() object is ephemeral and is intended to execute one instance of the Diffie-Hellman protocol. The private and public key parts are not intended to be set or persisted. Rather, a new set of domain parameters are generated each time an object is created.

Once a DH() object is created, once can retrieve the ephemeral public key for the other party with code similar to the following.

       AutoSeededRandomPool prng;
      Integer p, q, g;
      PrimeAndGenerator pg;

      pg.Generate(1, prng, 512, 511);
      p = pg.Prime();
      q = pg.SubPrime();
      g = pg.Generator();

      DH dh(p, q, g);
      SecByteBlock t1(dh.PrivateKeyLength()), t2(dh.PublicKeyLength());
      dh.GenerateKeyPair(prng, t1, t2);
      Integer k1(t1, t1.size()), k2(t2, t2.size());

      cout << "Private key:\n";
      cout << hex << k1 << endl;

      cout << "Public key:\n";
      cout << hex << k2 << endl;

Output of the program above will be similar to the following.

       $ ./cryptest.exe
      Private key:
      72b45a42371545e9d4880f48589aefh
      Public key:
      45fdb13f97b1840626f0250cec1dba4a23b894100b51fb5d2dd13693d789948f8bfc88f9200014b2
      ba8dd8a6debc471c69ef1e2326c61184a2eca88ec866346bh

See also
  Diffie-Hellman on the Crypto++ wiki and Diffie-Hellman in GF(p) with key validation

Since
  Crypto++ 1.0

Definition at line 211 of file dh.h.

## Member Typedef Documentation

## GroupParameters

typedef DH_Domain<DL_GroupParameters_GFP_DefaultSafePrime> DH::GroupParameters

Definition at line 213 of file dh.h.

## Element

typedef GroupParameters::Element DH::Element

Definition at line 214 of file dh.h.

## Constructor & Destructor Documentation

## ~DH()

virtual DH::~DH | ( |  | ) |

inline virtual

Definition at line 216 of file dh.h.

## DH() [1/6]

DH::DH | ( |  | ) |

inline

Create an uninitialized Diffie-Hellman object.

Definition at line 219 of file dh.h.

## DH() [2/6]

DH::DH | ( | BufferedTransformation & | bt | ) |

inline

Initialize a Diffie-Hellman object.

Parameters

  bt | BufferedTransformation with group parameters and options

Definition at line 223 of file dh.h.

## DH() [3/6]

DH::DH | ( | const GroupParameters & | params | ) |

inline

Initialize a Diffie-Hellman object.

Parameters

  params | group parameters and options

Definition at line 227 of file dh.h.

## DH() [4/6]

DH::DH | ( | RandomNumberGenerator & | rng,
|  | unsigned int | modulusBits
| ) |  |

inline

Create a Diffie-Hellman object.

Parameters

  rng | a RandomNumberGenerator derived class
  modulusBits | the size of the modulus, in bits

This function overload of Initialize() creates a new Diffie-Hellman object because it takes a RandomNumberGenerator() as a parameter.

Definition at line 234 of file dh.h.

## DH() [5/6]

DH::DH | ( | const Integer & | p,
|  | const Integer & | g
| ) |  |

inline

Initialize a Diffie-Hellman object.

Parameters

  p | the modulus
  g | the generator

Definition at line 239 of file dh.h.

## DH() [6/6]

DH::DH | ( | const Integer & | p,
|  | const Integer & | q,
|  | const Integer & | g
| ) |  |

inline

Initialize a Diffie-Hellman object.

Parameters

  p | the modulus
  q | the subgroup order
  g | the generator

Definition at line 245 of file dh.h.

## Member Function Documentation

## Initialize() [1/3]

void DH::Initialize | ( | RandomNumberGenerator & | rng,
|  | unsigned int | modulusBits
| ) |  |

inline

Creates a Diffie-Hellman object.

Parameters

  rng | a RandomNumberGenerator derived class
  modulusBits | the size of the modulus, in bits

This function overload of Initialize() creates a new Diffie-Hellman object because it takes a RandomNumberGenerator() as a parameter.

Definition at line 252 of file dh.h.

## Initialize() [2/3]

void DH::Initialize | ( | const Integer & | p,
|  | const Integer & | g
| ) |  |

inline

Initialize a Diffie-Hellman object.

Parameters

  p | the modulus
  g | the generator

Definition at line 258 of file dh.h.

## Initialize() [3/3]

void DH::Initialize | ( | const Integer & | p,
|  | const Integer & | q,
|  | const Integer & | g
| ) |  |

inline

Initialize a Diffie-Hellman object.

Parameters

  p | the modulus
  q | the subgroup order
  g | the generator

Definition at line 265 of file dh.h.

The documentation for this struct was generated from the following file:

- dh.h