Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.3 KiB

  1. #ifndef CRYPTOPP_DH_H
  2. #define CRYPTOPP_DH_H
  3. /** \file
  4. */
  5. #include "gfpcrypt.h"
  6. NAMESPACE_BEGIN(CryptoPP)
  7. //! ,
  8. template <class GROUP_PARAMETERS, class COFACTOR_OPTION = CPP_TYPENAME GROUP_PARAMETERS::DefaultCofactorOption>
  9. class DH_Domain : public DL_SimpleKeyAgreementDomainBase<typename GROUP_PARAMETERS::Element>
  10. {
  11. typedef DL_SimpleKeyAgreementDomainBase<typename GROUP_PARAMETERS::Element> Base;
  12. public:
  13. typedef GROUP_PARAMETERS GroupParameters;
  14. typedef typename GroupParameters::Element Element;
  15. typedef DL_KeyAgreementAlgorithm_DH<Element, COFACTOR_OPTION> DH_Algorithm;
  16. typedef DH_Domain<GROUP_PARAMETERS, COFACTOR_OPTION> Domain;
  17. DH_Domain() {}
  18. DH_Domain(const GroupParameters &params)
  19. : m_groupParameters(params) {}
  20. DH_Domain(BufferedTransformation &bt)
  21. {m_groupParameters.BERDecode(bt);}
  22. template <class T2>
  23. DH_Domain(RandomNumberGenerator &v1, const T2 &v2)
  24. {m_groupParameters.Initialize(v1, v2);}
  25. template <class T2, class T3>
  26. DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3)
  27. {m_groupParameters.Initialize(v1, v2, v3);}
  28. template <class T2, class T3, class T4>
  29. DH_Domain(RandomNumberGenerator &v1, const T2 &v2, const T3 &v3, const T4 &v4)
  30. {m_groupParameters.Initialize(v1, v2, v3, v4);}
  31. template <class T1, class T2>
  32. DH_Domain(const T1 &v1, const T2 &v2)
  33. {m_groupParameters.Initialize(v1, v2);}
  34. template <class T1, class T2, class T3>
  35. DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3)
  36. {m_groupParameters.Initialize(v1, v2, v3);}
  37. template <class T1, class T2, class T3, class T4>
  38. DH_Domain(const T1 &v1, const T2 &v2, const T3 &v3, const T4 &v4)
  39. {m_groupParameters.Initialize(v1, v2, v3, v4);}
  40. const GroupParameters & GetGroupParameters() const {return m_groupParameters;}
  41. GroupParameters & AccessGroupParameters() {return m_groupParameters;}
  42. void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
  43. {
  44. Base::GeneratePublicKey(rng, privateKey, publicKey);
  45. if (FIPS_140_2_ComplianceEnabled())
  46. {
  47. SecByteBlock privateKey2(this->PrivateKeyLength());
  48. this->GeneratePrivateKey(rng, privateKey2);
  49. SecByteBlock publicKey2(this->PublicKeyLength());
  50. Base::GeneratePublicKey(rng, privateKey2, publicKey2);
  51. SecByteBlock agreedValue(this->AgreedValueLength()), agreedValue2(this->AgreedValueLength());
  52. bool agreed1 = this->Agree(agreedValue, privateKey, publicKey2);
  53. bool agreed2 = this->Agree(agreedValue2, privateKey2, publicKey);
  54. if (!agreed1 || !agreed2 || agreedValue != agreedValue2)
  55. throw SelfTestFailure(this->AlgorithmName() + ": pairwise consistency test failed");
  56. }
  57. }
  58. static std::string CRYPTOPP_API StaticAlgorithmName()
  59. {return GroupParameters::StaticAlgorithmNamePrefix() + DH_Algorithm::StaticAlgorithmName();}
  60. std::string AlgorithmName() const {return StaticAlgorithmName();}
  61. private:
  62. const DL_KeyAgreementAlgorithm<Element> & GetKeyAgreementAlgorithm() const
  63. {return Singleton<DH_Algorithm>().Ref();}
  64. DL_GroupParameters<Element> & AccessAbstractGroupParameters()
  65. {return m_groupParameters;}
  66. GroupParameters m_groupParameters;
  67. };
  68. CRYPTOPP_DLL_TEMPLATE_CLASS DH_Domain<DL_GroupParameters_GFP_DefaultSafePrime>;
  69. //! <a href="http://www.weidai.com/scan-mirror/ka.html#DH">Diffie-Hellman</a> in GF(p) with key validation
  70. typedef DH_Domain<DL_GroupParameters_GFP_DefaultSafePrime> DH;
  71. NAMESPACE_END
  72. #endif