Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:56

0001 // dh2.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file dh2.h

0004 /// \brief Classes for Unified Diffie-Hellman key exchange

0005 /// \since Crypto++ 3.0

0006 
0007 #ifndef CRYPTOPP_DH2_H
0008 #define CRYPTOPP_DH2_H
0009 
0010 #include "cryptlib.h"
0011 
0012 NAMESPACE_BEGIN(CryptoPP)
0013 
0014 /// \brief Unified Diffie-Hellman in GF(p)

0015 /// \details A Diffie-Hellman domain is a set of parameters that must be shared

0016 ///   by two parties in a key agreement protocol, along with the algorithms

0017 ///   for generating key pairs and deriving agreed values.

0018 /// \sa AuthenticatedKeyAgreementDomain, <a href="http://www.weidai.com/scan-mirror/ka.html#DH2">Unified Diffie-Hellman</a>

0019 /// \since Crypto++ 3.0

0020 class DH2 : public AuthenticatedKeyAgreementDomain
0021 {
0022 public:
0023     virtual ~DH2() {}
0024 
0025     /// \brief Construct a DH2

0026     DH2(SimpleKeyAgreementDomain &domain)
0027         : d1(domain), d2(domain) {}
0028     /// \brief Construct a DH2

0029     DH2(SimpleKeyAgreementDomain &staticDomain, SimpleKeyAgreementDomain &ephemeralDomain)
0030         : d1(staticDomain), d2(ephemeralDomain) {}
0031 
0032     CryptoParameters & AccessCryptoParameters() {return d1.AccessCryptoParameters();}
0033 
0034     unsigned int AgreedValueLength() const
0035         {return d1.AgreedValueLength() + d2.AgreedValueLength();}
0036 
0037     unsigned int StaticPrivateKeyLength() const
0038         {return d1.PrivateKeyLength();}
0039     unsigned int StaticPublicKeyLength() const
0040         {return d1.PublicKeyLength();}
0041     void GenerateStaticPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
0042         {d1.GeneratePrivateKey(rng, privateKey);}
0043     void GenerateStaticPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
0044         {d1.GeneratePublicKey(rng, privateKey, publicKey);}
0045     void GenerateStaticKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const
0046         {d1.GenerateKeyPair(rng, privateKey, publicKey);}
0047 
0048     unsigned int EphemeralPrivateKeyLength() const
0049         {return d2.PrivateKeyLength();}
0050     unsigned int EphemeralPublicKeyLength() const
0051         {return d2.PublicKeyLength();}
0052     void GenerateEphemeralPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
0053         {d2.GeneratePrivateKey(rng, privateKey);}
0054     void GenerateEphemeralPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
0055         {d2.GeneratePublicKey(rng, privateKey, publicKey);}
0056     void GenerateEphemeralKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const
0057         {d2.GenerateKeyPair(rng, privateKey, publicKey);}
0058 
0059     bool Agree(byte *agreedValue,
0060         const byte *staticPrivateKey, const byte *ephemeralPrivateKey,
0061         const byte *staticOtherPublicKey, const byte *ephemeralOtherPublicKey,
0062         bool validateStaticOtherPublicKey=true) const;
0063 
0064 protected:
0065     SimpleKeyAgreementDomain &d1, &d2;
0066 };
0067 
0068 NAMESPACE_END
0069 
0070 #endif