Crypto++
dh.h
Go to the documentation of this file.
1 #ifndef CRYPTOPP_DH_H
2 #define CRYPTOPP_DH_H
3 
4 /** \file
5 */
6 
7 #include "gfpcrypt.h"
8 
9 NAMESPACE_BEGIN(CryptoPP)
10 
11 //! ,
12 template <class GROUP_PARAMETERS, class COFACTOR_OPTION = CPP_TYPENAME GROUP_PARAMETERS::DefaultCofactorOption>
13 class DH_Domain : public DL_SimpleKeyAgreementDomainBase<typename GROUP_PARAMETERS::Element>
14 {
16 
17 public:
18  typedef GROUP_PARAMETERS GroupParameters;
19  typedef typename GroupParameters::Element Element;
22 
23  DH_Domain() {}
24 
25  DH_Domain(const GroupParameters &params)
26  : m_groupParameters(params) {}
27 
29  {m_groupParameters.BERDecode(bt);}
30 
31  template <class T2>
32  DH_Domain(RandomNumberGenerator &v1, const T2 &v2)
33  {m_groupParameters.Initialize(v1, v2);}
34 
35  template <class T2, class T3>
36  DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3)
37  {m_groupParameters.Initialize(v1, v2, v3);}
38 
39  template <class T2, class T3, class T4>
40  DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3, const T4 &v4)
41  {m_groupParameters.Initialize(v1, v2, v3, v4);}
42 
43  template <class T1, class T2>
44  DH_Domain(const T1 &v1, const T2 &v2)
45  {m_groupParameters.Initialize(v1, v2);}
46 
47  template <class T1, class T2, class T3>
48  DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3)
49  {m_groupParameters.Initialize(v1, v2, v3);}
50 
51  template <class T1, class T2, class T3, class T4>
52  DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4)
53  {m_groupParameters.Initialize(v1, v2, v3, v4);}
54 
55  const GroupParameters & GetGroupParameters() const {return m_groupParameters;}
56  GroupParameters & AccessGroupParameters() {return m_groupParameters;}
57 
58  void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
59  {
60  Base::GeneratePublicKey(rng, privateKey, publicKey);
61 
63  {
64  SecByteBlock privateKey2(this->PrivateKeyLength());
65  this->GeneratePrivateKey(rng, privateKey2);
66 
67  SecByteBlock publicKey2(this->PublicKeyLength());
68  Base::GeneratePublicKey(rng, privateKey2, publicKey2);
69 
70  SecByteBlock agreedValue(this->AgreedValueLength()), agreedValue2(this->AgreedValueLength());
71  bool agreed1 = this->Agree(agreedValue, privateKey, publicKey2);
72  bool agreed2 = this->Agree(agreedValue2, privateKey2, publicKey);
73 
74  if (!agreed1 || !agreed2 || agreedValue != agreedValue2)
75  throw SelfTestFailure(this->AlgorithmName() + ": pairwise consistency test failed");
76  }
77  }
78 
79  static std::string CRYPTOPP_API StaticAlgorithmName()
80  {return GroupParameters::StaticAlgorithmNamePrefix() + DH_Algorithm::StaticAlgorithmName();}
81  std::string AlgorithmName() const {return StaticAlgorithmName();}
82 
83 private:
84  const DL_KeyAgreementAlgorithm<Element> & GetKeyAgreementAlgorithm() const
85  {return Singleton<DH_Algorithm>().Ref();}
86  DL_GroupParameters<Element> & AccessAbstractGroupParameters()
87  {return m_groupParameters;}
88 
89  GroupParameters m_groupParameters;
90 };
91 
92 CRYPTOPP_DLL_TEMPLATE_CLASS DH_Domain<DL_GroupParameters_GFP_DefaultSafePrime>;
93 
94 //! <a href="http://www.weidai.com/scan-mirror/ka.html#DH">Diffie-Hellman</a> in GF(p) with key validation
96 
97 NAMESPACE_END
98 
99 #endif
void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
generate public key
Definition: dh.h:58
DH key agreement algorithm.
Definition: pubkey.h:1434
std::string AlgorithmName() const
returns name of this algorithm, not universally implemented yet
Definition: dh.h:81
interface for DL group parameters
Definition: pubkey.h:521
void BERDecode(BufferedTransformation &bt)
for backwards compatibility, calls AccessMaterial().Load(bt)
Definition: cryptlib.h:1142
interface for random number generators
Definition: cryptlib.h:668
interface for buffered transformations
Definition: cryptlib.h:770
bool FIPS_140_2_ComplianceEnabled()
returns whether FIPS 140-2 compliance features were enabled at compile time
exception thrown when a crypto algorithm is used after a self test fails
Definition: fips140.h:14
Implementation of schemes based on DL over GF(p)
,
Definition: dh.h:13
DH_Domain< DL_GroupParameters_GFP_DefaultSafePrime > DH
Diffie-Hellman in GF(p) with key validation
Definition: dh.h:95
interface for DL key agreement algorithms
Definition: pubkey.h:915