Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | 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> KeyAgreementAlgorithm;
    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 T2 &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 T2 &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(PrivateKeyLength());
    00065                         GeneratePrivateKey(rng, privateKey2);
    00066
    00067                         SecByteBlock publicKey2(PublicKeyLength());
    00068                         Base::GeneratePublicKey(rng, privateKey2, publicKey2);
    00069
    00070                         SecByteBlock agreedValue(AgreedValueLength()), agreedValue2(AgreedValueLength());
    00071                         Agree(agreedValue, privateKey, publicKey2);
    00072                         Agree(agreedValue2, privateKey2, publicKey);
    00073
    00074                         if (agreedValue != agreedValue2)
    00075                                 throw SelfTestFailure(AlgorithmName() + ": pairwise consistency test failed");
    00076                 }
    00077         }
    00078
    00079 private:
    00080         const DL_KeyAgreementAlgorithm<Element> & GetKeyAgreementAlgorithm() const
    00081                 {static KeyAgreementAlgorithm a; return a;}
    00082         DL_GroupParameters<Element> & AccessAbstractGroupParameters()
    00083                 {return m_groupParameters;}
    00084
    00085         GroupParameters m_groupParameters;
    00086 };
    00087
    00088 CRYPTOPP_DLL_TEMPLATE_CLASS DH_Domain<DL_GroupParameters_GFP_DefaultSafePrime>;
    00089
    00090 //! <a href="http://www.weidai.com/scan-mirror/ka.html#DH">Diffie-Hellman</a> in GF(p) with key validation
    00091 typedef DH_Domain<DL_GroupParameters_GFP_DefaultSafePrime> DH;
    00092
    00093 NAMESPACE_END
    00094
    00095 #endif