Security Scol plugin
dh.h
Go to the documentation of this file.
1// dh.h - originally 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
13NAMESPACE_BEGIN(CryptoPP)
14
15
24template <class GROUP_PARAMETERS, class COFACTOR_OPTION = typename GROUP_PARAMETERS::DefaultCofactorOption>
25class DH_Domain : public DL_SimpleKeyAgreementDomainBase<typename GROUP_PARAMETERS::Element>
26{
28
29public:
30 typedef GROUP_PARAMETERS GroupParameters;
31 typedef typename GroupParameters::Element Element;
34
35 virtual ~DH_Domain() {}
36
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
139 if (FIPS_140_2_ComplianceEnabled())
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
160private:
161 const DL_KeyAgreementAlgorithm<Element> & GetKeyAgreementAlgorithm() const
162 {return Singleton<DH_Algorithm>().Ref();}
163 DL_GroupParameters<Element> & AccessAbstractGroupParameters()
164 {return m_groupParameters;}
165
166 GroupParameters m_groupParameters;
167};
168
170
210#if defined(CRYPTOPP_DOXYGEN_PROCESSING)
211struct DH : public DH_Domain<DL_GroupParameters_GFP_DefaultSafePrime>
212{
214 typedef GroupParameters::Element Element;
215
216 virtual ~DH() {}
217
219 DH() : DH_Domain() {}
220
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
273NAMESPACE_END
274
275#endif
Classes for performing mathematics over different fields.
Interface for buffered transformations.
Definition cryptlib.h:1652
Diffie-Hellman domain.
Definition dh.h:26
const GroupParameters & GetGroupParameters() const
Retrieves the group parameters for this domain.
Definition dh.h:121
DH_Domain(BufferedTransformation &bt)
Construct a Diffie-Hellman domain.
Definition dh.h:47
DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3)
Create a Diffie-Hellman domain.
Definition dh.h:67
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition dh.h:158
DH_Domain(const GroupParameters &params)
Construct a Diffie-Hellman domain.
Definition dh.h:42
DH_Domain(RandomNumberGenerator &v1, const T2 &v2)
Create a Diffie-Hellman domain.
Definition dh.h:56
DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3, const T4 &v4)
Create a Diffie-Hellman domain.
Definition dh.h:80
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
DH_Domain()
Construct a Diffie-Hellman domain.
Definition dh.h:38
DH_Domain(const T1 &v1, const T2 &v2)
Construct a Diffie-Hellman domain.
Definition dh.h:90
GroupParameters & AccessGroupParameters()
Retrieves the group parameters for this domain.
Definition dh.h:124
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(const T1 &v1, const T2 &v2, const T3 &v3)
Construct a Diffie-Hellman domain.
Definition dh.h:102
Interface for Discrete Log (DL) group parameters.
Definition pubkey.h:782
Diffie-Hellman key agreement algorithm.
Definition pubkey.h:2142
Interface for DL key agreement algorithms.
Definition pubkey.h:1477
Discrete Log (DL) simple key agreement base implementation.
Definition pubkey.h:2064
bool Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey=true) const
Definition pubkey.h:2090
void GeneratePrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
Definition pubkey.h:2075
void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
Generate a public key from a private key in this domain.
Definition pubkey.h:2081
Multiple precision integer with arithmetic operations.
Definition integer.h:50
Interface for random number generators.
Definition cryptlib.h:1435
Restricts the instantiation of a class to one static object without locks.
Definition misc.h:307
CRYPTOPP_NOINLINE const T & Ref(CRYPTOPP_NOINLINE_DOTDOTDOT) const
Return a reference to the inner Singleton object.
Definition misc.h:327
Abstract base classes that provide a uniform interface to this library.
DH_Domain< DL_GroupParameters_GFP_DefaultSafePrime > DH
Diffie-Hellman in GF(p)
Definition dh.h:270
Classes and functions for schemes based on Discrete Logs (DL) over GF(p)