Team Fortress 2 Source Code as on 22/4/2020
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.

107 lines
3.5 KiB

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