Crypto++

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

dh.h
Go to the documentation of this file.

    00001 #ifndef CRYPTOPP_DH_H
    00002 #define CRYPTOPP_DH_H
    00003
    00004 /** \file
    00005 */
    00006
    00007 #include "gfpcrypt.h"
    00008
    00009 NAMESPACE_BEGIN(CryptoPP)
    00010
    00011 //! ,
    00012 template <class GROUP_PARAMETERS, class COFACTOR_OPTION = CPP_TYPENAME GROUP_PARAMETERS::DefaultCofactorOption>
    00013 class DH_Domain : public DL_SimpleKeyAgreementDomainBase<typename GROUP_PARAMETERS::Element>
    00014 {
    00015     typedef DL_SimpleKeyAgreementDomainBase<typename GROUP_PARAMETERS::Element> Base;
    00016
    00017 public:
    00018     typedef GROUP_PARAMETERS GroupParameters;
    00019     typedef typename GroupParameters::Element Element;
    00020     typedef DL_KeyAgreementAlgorithm_DH<Element, COFACTOR_OPTION> DH_Algorithm;
    00021     typedef DH_Domain<GROUP_PARAMETERS, COFACTOR_OPTION> Domain;
    00022
    00023     DH_Domain() {}
    00024
    00025     DH_Domain(const GroupParameters &params)
    00026         : m_groupParameters(params) {}
    00027
    00028     DH_Domain(BufferedTransformation &bt)
    00029         {m_groupParameters.BERDecode(bt);}
    00030
    00031     template <class T2>
    00032     DH_Domain(RandomNumberGenerator &v1, const T2 &v2)
    00033         {m_groupParameters.Initialize(v1, v2);}
    00034
    00035     template <class T2, class T3>
    00036     DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3)
    00037         {m_groupParameters.Initialize(v1, v2, v3);}
    00038
    00039     template <class T2, class T3, class T4>
    00040     DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3, const T4 &v4)
    00041         {m_groupParameters.Initialize(v1, v2, v3, v4);}
    00042
    00043     template <class T1, class T2>
    00044     DH_Domain(const T1 &v1, const T2 &v2)
    00045         {m_groupParameters.Initialize(v1, v2);}
    00046
    00047     template <class T1, class T2, class T3>
    00048     DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3)
    00049         {m_groupParameters.Initialize(v1, v2, v3);}
    00050
    00051     template <class T1, class T2, class T3, class T4>
    00052     DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4)
    00053         {m_groupParameters.Initialize(v1, v2, v3, v4);}
    00054
    00055     const GroupParameters & GetGroupParameters() const {return m_groupParameters;}
    00056     GroupParameters & AccessGroupParameters() {return m_groupParameters;}
    00057
    00058     void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
    00059     {
    00060         Base::GeneratePublicKey(rng, privateKey, publicKey);
    00061
    00062         if (FIPS_140_2_ComplianceEnabled())
    00063         {
    00064             SecByteBlock privateKey2(this->PrivateKeyLength());
    00065             this->GeneratePrivateKey(rng, privateKey2);
    00066
    00067             SecByteBlock publicKey2(this->PublicKeyLength());
    00068             Base::GeneratePublicKey(rng, privateKey2, publicKey2);
    00069
    00070             SecByteBlock agreedValue(this->AgreedValueLength()), agreedValue2(this->AgreedValueLength());
    00071             bool agreed1 = this->Agree(agreedValue, privateKey, publicKey2);
    00072             bool agreed2 = this->Agree(agreedValue2, privateKey2, publicKey);
    00073
    00074             if (!agreed1 || !agreed2 || agreedValue != agreedValue2)
    00075                 throw SelfTestFailure(this->AlgorithmName() + ": pairwise consistency test failed");
    00076         }
    00077     }
    00078
    00079     static std::string CRYPTOPP_API StaticAlgorithmName()
    00080         {return GroupParameters::StaticAlgorithmNamePrefix() + DH_Algorithm::StaticAlgorithmName();}
    00081     std::string AlgorithmName() const {return StaticAlgorithmName();}
    00082
    00083 private:
    00084     const DL_KeyAgreementAlgorithm<Element> & GetKeyAgreementAlgorithm() const
    00085         {return Singleton<DH_Algorithm>().Ref();}
    00086     DL_GroupParameters<Element> & AccessAbstractGroupParameters()
    00087         {return m_groupParameters;}
    00088
    00089     GroupParameters m_groupParameters;
    00090 };
    00091
    00092 CRYPTOPP_DLL_TEMPLATE_CLASS DH_Domain<DL_GroupParameters_GFP_DefaultSafePrime>;
    00093
    00094 //! <a href="http://www.weidai.com/scan-mirror/ka.html#DH">Diffie-Hellman</a> in GF(p) with key validation
    00095 typedef DH_Domain<DL_GroupParameters_GFP_DefaultSafePrime> DH;
    00096
    00097 NAMESPACE_END
    00098
    00099 #endif