dh2.h 2.6 KB

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