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