Crypto++  5.6.3
Free C++ class library of cryptographic schemes
dh.h
Go to the documentation of this file.
1 // dh.h - written and placed in the public domain by Wei Dai
2 
3 //! \file
4 //! \headerfile dh.h
5 //! \brief Classes for Diffie-Hellman key exchange
6 
7 #ifndef CRYPTOPP_DH_H
8 #define CRYPTOPP_DH_H
9 
10 #include "cryptlib.h"
11 #include "gfpcrypt.h"
12 
13 NAMESPACE_BEGIN(CryptoPP)
14 
15 //! ,
16 template <class GROUP_PARAMETERS, class COFACTOR_OPTION = CPP_TYPENAME GROUP_PARAMETERS::DefaultCofactorOption>
17 class DH_Domain : public DL_SimpleKeyAgreementDomainBase<typename GROUP_PARAMETERS::Element>
18 {
20 
21 public:
22  typedef GROUP_PARAMETERS GroupParameters;
23  typedef typename GroupParameters::Element Element;
26 
27  DH_Domain() {}
28 
29  DH_Domain(const GroupParameters &params)
30  : m_groupParameters(params) {}
31 
33  {m_groupParameters.BERDecode(bt);}
34 
35  template <class T2>
36  DH_Domain(RandomNumberGenerator &v1, const T2 &v2)
37  {m_groupParameters.Initialize(v1, v2);}
38 
39  template <class T2, class T3>
40  DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3)
41  {m_groupParameters.Initialize(v1, v2, v3);}
42 
43  template <class T2, class T3, class T4>
44  DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3, const T4 &v4)
45  {m_groupParameters.Initialize(v1, v2, v3, v4);}
46 
47  template <class T1, class T2>
48  DH_Domain(const T1 &v1, const T2 &v2)
49  {m_groupParameters.Initialize(v1, v2);}
50 
51  template <class T1, class T2, class T3>
52  DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3)
53  {m_groupParameters.Initialize(v1, v2, v3);}
54 
55  template <class T1, class T2, class T3, class T4>
56  DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4)
57  {m_groupParameters.Initialize(v1, v2, v3, v4);}
58 
59  const GroupParameters & GetGroupParameters() const {return m_groupParameters;}
60  GroupParameters & AccessGroupParameters() {return m_groupParameters;}
61 
62  void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
63  {
64  Base::GeneratePublicKey(rng, privateKey, publicKey);
65 
67  {
68  SecByteBlock privateKey2(this->PrivateKeyLength());
69  this->GeneratePrivateKey(rng, privateKey2);
70 
71  SecByteBlock publicKey2(this->PublicKeyLength());
72  Base::GeneratePublicKey(rng, privateKey2, publicKey2);
73 
74  SecByteBlock agreedValue(this->AgreedValueLength()), agreedValue2(this->AgreedValueLength());
75  bool agreed1 = this->Agree(agreedValue, privateKey, publicKey2);
76  bool agreed2 = this->Agree(agreedValue2, privateKey2, publicKey);
77 
78  if (!agreed1 || !agreed2 || agreedValue != agreedValue2)
79  throw SelfTestFailure(this->AlgorithmName() + ": pairwise consistency test failed");
80  }
81  }
82 
83  static std::string CRYPTOPP_API StaticAlgorithmName()
84  {return GroupParameters::StaticAlgorithmNamePrefix() + DH_Algorithm::StaticAlgorithmName();}
85  std::string AlgorithmName() const {return StaticAlgorithmName();}
86 
87 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
88  virtual ~DH_Domain() {}
89 #endif
90 
91 private:
92  const DL_KeyAgreementAlgorithm<Element> & GetKeyAgreementAlgorithm() const
93  {return Singleton<DH_Algorithm>().Ref();}
94  DL_GroupParameters<Element> & AccessAbstractGroupParameters()
95  {return m_groupParameters;}
96 
97  GroupParameters m_groupParameters;
98 };
99 
100 CRYPTOPP_DLL_TEMPLATE_CLASS DH_Domain<DL_GroupParameters_GFP_DefaultSafePrime>;
101 
102 //! <a href="http://www.weidai.com/scan-mirror/ka.html#DH">Diffie-Hellman</a> in GF(p) with key validation
104 
105 NAMESPACE_END
106 
107 #endif
void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
generate public key
Definition: dh.h:62
DH key agreement algorithm.
Definition: pubkey.h:1712
Restricts the instantiation of a class to one static object without locks.
Definition: misc.h:233
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: dh.h:85
interface for DL group parameters
Definition: pubkey.h:722
void BERDecode(BufferedTransformation &bt)
for backwards compatibility, calls AccessMaterial().Load(bt)
Definition: cryptlib.h:2065
Abstract base classes that provide a uniform interface to this library.
Interface for random number generators.
Definition: cryptlib.h:1085
SecByteBlock is a SecBlock<byte> typedef.
Definition: secblock.h:719
Interface for buffered transformations.
Definition: cryptlib.h:1247
bool FIPS_140_2_ComplianceEnabled()
Determines whether the library provides FIPS validated cryptography.
Definition: fips140.cpp:29
Exception thrown when a crypto algorithm is used after a self test fails.
Definition: fips140.h:23
Implementation of schemes based on DL over GF(p)
,
Definition: dh.h:17
DH_Domain< DL_GroupParameters_GFP_DefaultSafePrime > DH
Diffie-Hellman in GF(p) with key validation
Definition: dh.h:103
Crypto++ library namespace.
interface for DL key agreement algorithms
Definition: pubkey.h:1135