1 // dh2.h - originally written and placed in the public domain by Wei Dai
2 
3 /// \file dh2.h
4 /// \brief Classes for Unified Diffie-Hellman key exchange
5 /// \since Crypto++ 3.0
6 
7 #ifndef CRYPTOPP_DH2_H
8 #define CRYPTOPP_DH2_H
9 
10 #include "cryptlib.h"
11 
NAMESPACE_BEGIN(CryptoPP)12 NAMESPACE_BEGIN(CryptoPP)
13 
14 /// \brief Unified Diffie-Hellman in GF(p)
15 /// \details A Diffie-Hellman domain is a set of parameters that must be shared
16 ///   by two parties in a key agreement protocol, along with the algorithms
17 ///   for generating key pairs and deriving agreed values.
18 /// \sa AuthenticatedKeyAgreementDomain, <a href="http://www.weidai.com/scan-mirror/ka.html#DH2">Unified Diffie-Hellman</a>
19 /// \since Crypto++ 3.0
20 class DH2 : public AuthenticatedKeyAgreementDomain
21 {
22 public:
23 	virtual ~DH2() {}
24 
25 	/// \brief Construct a DH2
26 	DH2(SimpleKeyAgreementDomain &domain)
27 		: d1(domain), d2(domain) {}
28 	/// \brief Construct a DH2
29 	DH2(SimpleKeyAgreementDomain &staticDomain, SimpleKeyAgreementDomain &ephemeralDomain)
30 		: d1(staticDomain), d2(ephemeralDomain) {}
31 
32 	CryptoParameters & AccessCryptoParameters() {return d1.AccessCryptoParameters();}
33 
34 	unsigned int AgreedValueLength() const
35 		{return d1.AgreedValueLength() + d2.AgreedValueLength();}
36 
37 	unsigned int StaticPrivateKeyLength() const
38 		{return d1.PrivateKeyLength();}
39 	unsigned int StaticPublicKeyLength() const
40 		{return d1.PublicKeyLength();}
41 	void GenerateStaticPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
42 		{d1.GeneratePrivateKey(rng, privateKey);}
43 	void GenerateStaticPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
44 		{d1.GeneratePublicKey(rng, privateKey, publicKey);}
45 	void GenerateStaticKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const
46 		{d1.GenerateKeyPair(rng, privateKey, publicKey);}
47 
48 	unsigned int EphemeralPrivateKeyLength() const
49 		{return d2.PrivateKeyLength();}
50 	unsigned int EphemeralPublicKeyLength() const
51 		{return d2.PublicKeyLength();}
52 	void GenerateEphemeralPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
53 		{d2.GeneratePrivateKey(rng, privateKey);}
54 	void GenerateEphemeralPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
55 		{d2.GeneratePublicKey(rng, privateKey, publicKey);}
56 	void GenerateEphemeralKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const
57 		{d2.GenerateKeyPair(rng, privateKey, publicKey);}
58 
59 	bool Agree(byte *agreedValue,
60 		const byte *staticPrivateKey, const byte *ephemeralPrivateKey,
61 		const byte *staticOtherPublicKey, const byte *ephemeralOtherPublicKey,
62 		bool validateStaticOtherPublicKey=true) const;
63 
64 protected:
65 	SimpleKeyAgreementDomain &d1, &d2;
66 };
67 
68 NAMESPACE_END
69 
70 #endif
71