Fabcoin Core  0.16.2
P2P Digital Currency
dh.h
Go to the documentation of this file.
1 // dh.h - written and placed in the public domain by Wei Dai
2 
5 
6 #ifndef CRYPTOPP_DH_H
7 #define CRYPTOPP_DH_H
8 
9 #include "cryptlib.h"
10 #include "gfpcrypt.h"
11 #include "algebra.h"
12 
14 
15 template <class GROUP_PARAMETERS, class COFACTOR_OPTION = typename GROUP_PARAMETERS::DefaultCofactorOption>
25 class DH_Domain : public DL_SimpleKeyAgreementDomainBase<typename GROUP_PARAMETERS::Element>
26 {
28 
29 public:
30  typedef GROUP_PARAMETERS GroupParameters;
31  typedef typename GroupParameters::Element Element;
34 
35  virtual ~DH_Domain() {}
36 
38  DH_Domain() {}
39 
42  DH_Domain(const GroupParameters &params)
43  : m_groupParameters(params) {}
44 
48  {m_groupParameters.BERDecode(bt);}
49 
55  template <class T2>
57  {m_groupParameters.Initialize(v1, v2);}
58 
66  template <class T2, class T3>
67  DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3)
68  {m_groupParameters.Initialize(v1, v2, v3);}
69 
79  template <class T2, class T3, class T4>
80  DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3, const T4 &v4)
81  {m_groupParameters.Initialize(v1, v2, v3, v4);}
82 
89  template <class T1, class T2>
90  DH_Domain(const T1 &v1, const T2 &v2)
91  {m_groupParameters.Initialize(v1, v2);}
92 
101  template <class T1, class T2, class T3>
102  DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3)
103  {m_groupParameters.Initialize(v1, v2, v3);}
104 
115  template <class T1, class T2, class T3, class T4>
116  DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4)
117  {m_groupParameters.Initialize(v1, v2, v3, v4);}
118 
121  const GroupParameters & GetGroupParameters() const {return m_groupParameters;}
124  GroupParameters & AccessGroupParameters() {return m_groupParameters;}
125 
135  void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
136  {
137  Base::GeneratePublicKey(rng, privateKey, publicKey);
138 
140  {
141  SecByteBlock privateKey2(this->PrivateKeyLength());
142  this->GeneratePrivateKey(rng, privateKey2);
143 
144  SecByteBlock publicKey2(this->PublicKeyLength());
145  Base::GeneratePublicKey(rng, privateKey2, publicKey2);
146 
147  SecByteBlock agreedValue(this->AgreedValueLength()), agreedValue2(this->AgreedValueLength());
148  bool agreed1 = this->Agree(agreedValue, privateKey, publicKey2);
149  bool agreed2 = this->Agree(agreedValue2, privateKey2, publicKey);
150 
151  if (!agreed1 || !agreed2 || agreedValue != agreedValue2)
152  throw SelfTestFailure(this->AlgorithmName() + ": pairwise consistency test failed");
153  }
154  }
155 
156  static std::string CRYPTOPP_API StaticAlgorithmName()
157  {return GroupParameters::StaticAlgorithmNamePrefix() + DH_Algorithm::StaticAlgorithmName();}
158  std::string AlgorithmName() const {return StaticAlgorithmName();}
159 
160 private:
162  {return Singleton<DH_Algorithm>().Ref();}
164  {return m_groupParameters;}
165 
166  GroupParameters m_groupParameters;
167 };
168 
170 
210 #if defined(CRYPTOPP_DOXYGEN_PROCESSING)
211 struct DH : public DH_Domain<DL_GroupParameters_GFP_DefaultSafePrime>
212 {
214  typedef GroupParameters::Element Element;
215 
216  virtual ~DH() {}
217 
219  DH() : DH_Domain() {}
220 
223  DH(BufferedTransformation &bt) : DH_Domain(bt) {}
224 
227  DH(const GroupParameters &params) : DH_Domain(params) {}
228 
234  DH(RandomNumberGenerator &rng, unsigned int modulusBits) : DH_Domain(rng, modulusBits) {}
235 
239  DH(const Integer &p, const Integer &g) : DH_Domain(p, g) {}
240 
245  DH(const Integer &p, const Integer &q, const Integer &g) : DH_Domain(p, q, g) {}
246 
252  void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
253  {AccessGroupParameters().Initialize(rng, modulusBits);}
254 
258  void Initialize(const Integer &p, const Integer &g)
259  {AccessGroupParameters().Initialize(p, g);}
260 
265  void Initialize(const Integer &p, const Integer &q, const Integer &g)
266  {AccessGroupParameters().Initialize(p, q, g);}
267 };
268 #else
269 // The real DH class is a typedef.
271 #endif
272 
274 
275 #endif
#define T1
Definition: integer.cpp:2170
void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
Generate a public key from a private key in this domain.
Definition: dh.h:135
GroupParameters m_groupParameters
Definition: dh.h:166
uint8_t byte
Definition: Common.h:57
Diffie-Hellman key agreement algorithm.
Definition: pubkey.h:1933
#define T2
Definition: integer.cpp:2171
Restricts the instantiation of a class to one static object without locks.
Definition: misc.h:274
GROUP_PARAMETERS GroupParameters
Definition: dh.h:30
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: dh.h:158
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
bool FIPS_140_2_ComplianceEnabled()
Determines whether the library provides FIPS validated cryptography.
Definition: fips140.cpp:29
Interface for Discrete Log (DL) group parameters.
Definition: pubkey.h:736
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Definition: config.h:720
DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3, const T4 &v4)
Create a Diffie-Hellman domain.
Definition: dh.h:80
DH_Domain(BufferedTransformation &bt)
Construct a Diffie-Hellman domain.
Definition: dh.h:47
Abstract base classes that provide a uniform interface to this library.
#define g(i)
Definition: sha.cpp:735
DL_KeyAgreementAlgorithm_DH< Element, COFACTOR_OPTION > DH_Algorithm
Definition: dh.h:32
Interface for random number generators.
Definition: cryptlib.h:1188
const DL_KeyAgreementAlgorithm< Element > & GetKeyAgreementAlgorithm() const
Definition: dh.h:161
DH_Domain< GROUP_PARAMETERS, COFACTOR_OPTION > Domain
Definition: dh.h:33
Classes for performing mathematics over different fields.
Interface for buffered transformations.
Definition: cryptlib.h:1352
virtual ~DH_Domain()
Definition: dh.h:35
DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4)
Construct a Diffie-Hellman domain.
Definition: dh.h:116
DH_Domain(RandomNumberGenerator &v1, const T2 &v2)
Create a Diffie-Hellman domain.
Definition: dh.h:56
Exception thrown when a crypto algorithm is used after a self test fails.
Definition: fips140.h:23
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
GroupParameters::Element Element
Definition: dh.h:31
#define CRYPTOPP_API
Definition: config.h:705
Classes and functions for schemes based on Discrete Logs (DL) over GF(p)
DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3)
Construct a Diffie-Hellman domain.
Definition: dh.h:102
Diffie-Hellman domain.
Definition: dh.h:25
DH_Domain(const GroupParameters &params)
Construct a Diffie-Hellman domain.
Definition: dh.h:42
DH_Domain< DL_GroupParameters_GFP_DefaultSafePrime > DH
Diffie-Hellman in GF(p)
Definition: dh.h:270
const GroupParameters & GetGroupParameters() const
Retrieves the group parameters for this domain.
Definition: dh.h:121
DH_Domain()
Construct a Diffie-Hellman domain.
Definition: dh.h:38
DL_GroupParameters< Element > & AccessAbstractGroupParameters()
Definition: dh.h:163
DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3)
Create a Diffie-Hellman domain.
Definition: dh.h:67
static std::string CRYPTOPP_API StaticAlgorithmName()
Definition: dh.h:156
#define NAMESPACE_END
Definition: config.h:201
#define T3
Definition: integer.cpp:2172
GroupParameters & AccessGroupParameters()
Retrieves the group parameters for this domain.
Definition: dh.h:124
Interface for DL key agreement algorithms.
Definition: pubkey.h:1291
Discrete Log (DL) simple key agreement base implementation.
Definition: pubkey.h:1855
DL_SimpleKeyAgreementDomainBase< typename GROUP_PARAMETERS::Element > Base
Definition: dh.h:27
DH_Domain(const T1 &v1, const T2 &v2)
Construct a Diffie-Hellman domain.
Definition: dh.h:90