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.

108 lines
3.1 KiB

  1. // xtrcrypt.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #include "xtrcrypt.h"
  4. #include "nbtheory.h"
  5. #include "asn.h"
  6. #include "argnames.h"
  7. NAMESPACE_BEGIN(CryptoPP)
  8. XTR_DH::XTR_DH(const Integer &p, const Integer &q, const GFP2Element &g)
  9. : m_p(p), m_q(q), m_g(g)
  10. {
  11. }
  12. XTR_DH::XTR_DH(RandomNumberGenerator &rng, unsigned int pbits, unsigned int qbits)
  13. {
  14. XTR_FindPrimesAndGenerator(rng, m_p, m_q, m_g, pbits, qbits);
  15. }
  16. XTR_DH::XTR_DH(BufferedTransformation &bt)
  17. {
  18. BERSequenceDecoder seq(bt);
  19. m_p.BERDecode(seq);
  20. m_q.BERDecode(seq);
  21. m_g.c1.BERDecode(seq);
  22. m_g.c2.BERDecode(seq);
  23. seq.MessageEnd();
  24. }
  25. void XTR_DH::DEREncode(BufferedTransformation &bt) const
  26. {
  27. DERSequenceEncoder seq(bt);
  28. m_p.DEREncode(seq);
  29. m_q.DEREncode(seq);
  30. m_g.c1.DEREncode(seq);
  31. m_g.c2.DEREncode(seq);
  32. seq.MessageEnd();
  33. }
  34. bool XTR_DH::Validate(RandomNumberGenerator &rng, unsigned int level) const
  35. {
  36. bool pass = true;
  37. pass = pass && m_p > Integer::One() && m_p.IsOdd();
  38. pass = pass && m_q > Integer::One() && m_q.IsOdd();
  39. GFP2Element three = GFP2_ONB<ModularArithmetic>(m_p).ConvertIn(3);
  40. pass = pass && !(m_g.c1.IsNegative() || m_g.c2.IsNegative() || m_g.c1 >= m_p || m_g.c2 >= m_p || m_g == three);
  41. if (level >= 1)
  42. pass = pass && ((m_p.Squared()-m_p+1)%m_q).IsZero();
  43. if (level >= 2)
  44. {
  45. pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
  46. pass = pass && XTR_Exponentiate(m_g, (m_p.Squared()-m_p+1)/m_q, m_p) != three;
  47. pass = pass && XTR_Exponentiate(m_g, m_q, m_p) == three;
  48. }
  49. return pass;
  50. }
  51. bool XTR_DH::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
  52. {
  53. return GetValueHelper(this, name, valueType, pValue).Assignable()
  54. CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)
  55. CRYPTOPP_GET_FUNCTION_ENTRY(SubgroupOrder)
  56. CRYPTOPP_GET_FUNCTION_ENTRY(SubgroupGenerator)
  57. ;
  58. }
  59. void XTR_DH::AssignFrom(const NameValuePairs &source)
  60. {
  61. AssignFromHelper(this, source)
  62. CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
  63. CRYPTOPP_SET_FUNCTION_ENTRY(SubgroupOrder)
  64. CRYPTOPP_SET_FUNCTION_ENTRY(SubgroupGenerator)
  65. ;
  66. }
  67. void XTR_DH::GeneratePrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
  68. {
  69. Integer x(rng, Integer::Zero(), m_q-1);
  70. x.Encode(privateKey, PrivateKeyLength());
  71. }
  72. void XTR_DH::GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
  73. {
  74. Integer x(privateKey, PrivateKeyLength());
  75. GFP2Element y = XTR_Exponentiate(m_g, x, m_p);
  76. y.Encode(publicKey, PublicKeyLength());
  77. }
  78. bool XTR_DH::Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey) const
  79. {
  80. GFP2Element w(otherPublicKey, PublicKeyLength());
  81. if (validateOtherPublicKey)
  82. {
  83. GFP2_ONB<ModularArithmetic> gfp2(m_p);
  84. GFP2Element three = gfp2.ConvertIn(3);
  85. if (w.c1.IsNegative() || w.c2.IsNegative() || w.c1 >= m_p || w.c2 >= m_p || w == three)
  86. return false;
  87. if (XTR_Exponentiate(w, m_q, m_p) != three)
  88. return false;
  89. }
  90. Integer s(privateKey, PrivateKeyLength());
  91. GFP2Element z = XTR_Exponentiate(w, s, m_p);
  92. z.Encode(agreedValue, AgreedValueLength());
  93. return true;
  94. }
  95. NAMESPACE_END