Crypto++  8.8
Free C++ class library of cryptographic schemes
dh2.cpp
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 {
13  void AssignFrom(const NameValuePairs &source) {
14  CRYPTOPP_UNUSED(source);
15  }
16  bool Validate(RandomNumberGenerator &rng, unsigned int level) const {
17  CRYPTOPP_UNUSED(rng); CRYPTOPP_UNUSED(level);
18  return false;
19  }
20  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 {
28  CryptoParameters & AccessCryptoParameters() {
29  return *this;
30  }
31  unsigned int AgreedValueLength() const {
32  return 1;
33  }
34  unsigned int PrivateKeyLength() const {
35  return 1;
36  }
37  unsigned int PublicKeyLength() const {
38  return 1;
39  }
40  void GeneratePrivateKey(RandomNumberGenerator &rng, byte *privateKey) const {
41  CRYPTOPP_UNUSED(rng); CRYPTOPP_UNUSED(privateKey);
42  }
43  void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const {
44  CRYPTOPP_UNUSED(rng); CRYPTOPP_UNUSED(privateKey); CRYPTOPP_UNUSED(publicKey);
45  }
46  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 
53 void DH2_TestInstantiations()
54 {
55  NullSimpleKeyAgreementDomain dom;
56  DH2 dh(dom);
57 }
58 #endif
59 
60 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
virtual void AssignFrom(const NameValuePairs &source)=0
Assign values to this object.
virtual bool Validate(RandomNumberGenerator &rng, unsigned int level) const =0
Check this object for errors.
Interface for crypto parameters.
Definition: cryptlib.h:2551
Unified Diffie-Hellman in GF(p)
Definition: dh2.h:21
bool Agree(byte *agreedValue, const byte *staticPrivateKey, const byte *ephemeralPrivateKey, const byte *staticOtherPublicKey, const byte *ephemeralOtherPublicKey, bool validateStaticOtherPublicKey=true) const
Derive agreed value.
Definition: dh2.cpp:60
Interface for retrieving values given their names.
Definition: cryptlib.h:327
virtual CRYPTOPP_DLL bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const =0
Get a named value.
Interface for random number generators.
Definition: cryptlib.h:1440
virtual bool Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey=true) const =0
Derive agreed value.
virtual unsigned int AgreedValueLength() const =0
Provides the size of the agreed value.
Abstract base classes that provide a uniform interface to this library.
Classes for Unified Diffie-Hellman key exchange.
Utility functions for the Crypto++ library.
Crypto++ library namespace.
Precompiled header file.