1 // dh2.cpp - originally written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 #include "cryptlib.h"
5 #include "misc.h"
6 #include "dh2.h"
7 
8 NAMESPACE_BEGIN(CryptoPP)
9 
10 #if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
11 struct NullCryptoParameters : public CryptoParameters
12 {
AssignFromNullCryptoParameters13 	void AssignFrom(const NameValuePairs &source) {
14 		CRYPTOPP_UNUSED(source);
15 	}
ValidateNullCryptoParameters16 	bool Validate(RandomNumberGenerator &rng, unsigned int level) const {
17 		CRYPTOPP_UNUSED(rng); CRYPTOPP_UNUSED(level);
18 		return false;
19 	}
GetVoidValueNullCryptoParameters20 	bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const {
21 		CRYPTOPP_UNUSED(name); CRYPTOPP_UNUSED(valueType); CRYPTOPP_UNUSED(pValue);
22 		return false;
23 	}
24 };
25 
26 struct NullSimpleKeyAgreementDomain : public TwoBases<NullCryptoParameters, SimpleKeyAgreementDomain>
27 {
AccessCryptoParametersNullSimpleKeyAgreementDomain28 	CryptoParameters & AccessCryptoParameters() {
29 		return *this;
30 	}
AgreedValueLengthNullSimpleKeyAgreementDomain31 	unsigned int AgreedValueLength() const {
32 		return 1;
33 	}
PrivateKeyLengthNullSimpleKeyAgreementDomain34 	unsigned int PrivateKeyLength() const {
35 		return 1;
36 	}
PublicKeyLengthNullSimpleKeyAgreementDomain37 	unsigned int PublicKeyLength() const {
38 		return 1;
39 	}
GeneratePrivateKeyNullSimpleKeyAgreementDomain40 	void GeneratePrivateKey(RandomNumberGenerator &rng, byte *privateKey) const {
41 		CRYPTOPP_UNUSED(rng); CRYPTOPP_UNUSED(privateKey);
42 	}
GeneratePublicKeyNullSimpleKeyAgreementDomain43 	void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const {
44 		CRYPTOPP_UNUSED(rng); CRYPTOPP_UNUSED(privateKey); CRYPTOPP_UNUSED(publicKey);
45 	}
AgreeNullSimpleKeyAgreementDomain46 	bool Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey=true) const {
47 		CRYPTOPP_UNUSED(agreedValue); CRYPTOPP_UNUSED(privateKey);
48 		CRYPTOPP_UNUSED(otherPublicKey); CRYPTOPP_UNUSED(validateOtherPublicKey);
49 		return false;
50 	}
51 };
52 
DH2_TestInstantiations()53 void DH2_TestInstantiations()
54 {
55 	NullSimpleKeyAgreementDomain dom;
56 	DH2 dh(dom);
57 }
58 #endif
59 
Agree(byte * agreedValue,const byte * staticSecretKey,const byte * ephemeralSecretKey,const byte * staticOtherPublicKey,const byte * ephemeralOtherPublicKey,bool validateStaticOtherPublicKey) const60 bool DH2::Agree(byte *agreedValue,
61 		const byte *staticSecretKey, const byte *ephemeralSecretKey,
62 		const byte *staticOtherPublicKey, const byte *ephemeralOtherPublicKey,
63 		bool validateStaticOtherPublicKey) const
64 {
65 	return d1.Agree(agreedValue, staticSecretKey, staticOtherPublicKey, validateStaticOtherPublicKey)
66 		&& d2.Agree(agreedValue+d1.AgreedValueLength(), ephemeralSecretKey, ephemeralOtherPublicKey, true);
67 }
68 
69 NAMESPACE_END
70