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.

210 lines
6.0 KiB

  1. // luc.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #include "luc.h"
  4. #include "asn.h"
  5. #include "nbtheory.h"
  6. #include "sha.h"
  7. #include "algparam.h"
  8. NAMESPACE_BEGIN(CryptoPP)
  9. void LUC_TestInstantiations()
  10. {
  11. LUC_HMP<SHA>::Signer t1;
  12. LUCFunction t2;
  13. InvertibleLUCFunction t3;
  14. }
  15. void DL_Algorithm_LUC_HMP::Sign(const DL_GroupParameters<Integer> &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
  16. {
  17. const Integer &q = params.GetSubgroupOrder();
  18. r = params.ExponentiateBase(k);
  19. s = (k + x*(r+e)) % q;
  20. }
  21. bool DL_Algorithm_LUC_HMP::Verify(const DL_GroupParameters<Integer> &params, const DL_PublicKey<Integer> &publicKey, const Integer &e, const Integer &r, const Integer &s) const
  22. {
  23. Integer p = params.GetGroupOrder()-1;
  24. const Integer &q = params.GetSubgroupOrder();
  25. Integer Vsg = params.ExponentiateBase(s);
  26. Integer Vry = publicKey.ExponentiatePublicElement((r+e)%q);
  27. return (Vsg*Vsg + Vry*Vry + r*r) % p == (Vsg * Vry * r + 4) % p;
  28. }
  29. Integer DL_BasePrecomputation_LUC::Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const
  30. {
  31. return Lucas(exponent, m_g, static_cast<const DL_GroupPrecomputation_LUC &>(group).GetModulus());
  32. }
  33. void DL_GroupParameters_LUC::SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
  34. {
  35. for (unsigned int i=0; i<exponentsCount; i++)
  36. results[i] = Lucas(exponents[i], base, GetModulus());
  37. }
  38. void LUCFunction::BERDecode(BufferedTransformation &bt)
  39. {
  40. BERSequenceDecoder seq(bt);
  41. m_n.BERDecode(seq);
  42. m_e.BERDecode(seq);
  43. seq.MessageEnd();
  44. }
  45. void LUCFunction::DEREncode(BufferedTransformation &bt) const
  46. {
  47. DERSequenceEncoder seq(bt);
  48. m_n.DEREncode(seq);
  49. m_e.DEREncode(seq);
  50. seq.MessageEnd();
  51. }
  52. Integer LUCFunction::ApplyFunction(const Integer &x) const
  53. {
  54. DoQuickSanityCheck();
  55. return Lucas(m_e, x, m_n);
  56. }
  57. bool LUCFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
  58. {
  59. bool pass = true;
  60. pass = pass && m_n > Integer::One() && m_n.IsOdd();
  61. pass = pass && m_e > Integer::One() && m_e.IsOdd() && m_e < m_n;
  62. return pass;
  63. }
  64. bool LUCFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
  65. {
  66. return GetValueHelper(this, name, valueType, pValue).Assignable()
  67. CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)
  68. CRYPTOPP_GET_FUNCTION_ENTRY(PublicExponent)
  69. ;
  70. }
  71. void LUCFunction::AssignFrom(const NameValuePairs &source)
  72. {
  73. AssignFromHelper(this, source)
  74. CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
  75. CRYPTOPP_SET_FUNCTION_ENTRY(PublicExponent)
  76. ;
  77. }
  78. // *****************************************************************************
  79. // private key operations:
  80. class LUCPrimeSelector : public PrimeSelector
  81. {
  82. public:
  83. LUCPrimeSelector(const Integer &e) : m_e(e) {}
  84. bool IsAcceptable(const Integer &candidate) const
  85. {
  86. return RelativelyPrime(m_e, candidate+1) && RelativelyPrime(m_e, candidate-1);
  87. }
  88. Integer m_e;
  89. };
  90. void InvertibleLUCFunction::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
  91. {
  92. int modulusSize = 2048;
  93. alg.GetIntValue("ModulusSize", modulusSize) || alg.GetIntValue("KeySize", modulusSize);
  94. if (modulusSize < 16)
  95. throw InvalidArgument("InvertibleLUCFunction: specified modulus size is too small");
  96. m_e = alg.GetValueWithDefault("PublicExponent", Integer(17));
  97. if (m_e < 5 || m_e.IsEven())
  98. throw InvalidArgument("InvertibleLUCFunction: invalid public exponent");
  99. LUCPrimeSelector selector(m_e);
  100. AlgorithmParameters primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize)
  101. ("PointerToPrimeSelector", selector.GetSelectorPointer());
  102. m_p.GenerateRandom(rng, primeParam);
  103. m_q.GenerateRandom(rng, primeParam);
  104. m_n = m_p * m_q;
  105. m_u = m_q.InverseMod(m_p);
  106. }
  107. void InvertibleLUCFunction::Initialize(RandomNumberGenerator &rng, unsigned int keybits, const Integer &e)
  108. {
  109. GenerateRandom(rng, MakeParameters("ModulusSize", (int)keybits)("PublicExponent", e));
  110. }
  111. void InvertibleLUCFunction::BERDecode(BufferedTransformation &bt)
  112. {
  113. BERSequenceDecoder seq(bt);
  114. Integer version(seq);
  115. if (!!version) // make sure version is 0
  116. BERDecodeError();
  117. m_n.BERDecode(seq);
  118. m_e.BERDecode(seq);
  119. m_p.BERDecode(seq);
  120. m_q.BERDecode(seq);
  121. m_u.BERDecode(seq);
  122. seq.MessageEnd();
  123. }
  124. void InvertibleLUCFunction::DEREncode(BufferedTransformation &bt) const
  125. {
  126. DERSequenceEncoder seq(bt);
  127. const byte version[] = {INTEGER, 1, 0};
  128. seq.Put(version, sizeof(version));
  129. m_n.DEREncode(seq);
  130. m_e.DEREncode(seq);
  131. m_p.DEREncode(seq);
  132. m_q.DEREncode(seq);
  133. m_u.DEREncode(seq);
  134. seq.MessageEnd();
  135. }
  136. Integer InvertibleLUCFunction::CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
  137. {
  138. // not clear how to do blinding with LUC
  139. DoQuickSanityCheck();
  140. return InverseLucas(m_e, x, m_q, m_p, m_u);
  141. }
  142. bool InvertibleLUCFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
  143. {
  144. bool pass = LUCFunction::Validate(rng, level);
  145. pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n;
  146. pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n;
  147. pass = pass && m_u.IsPositive() && m_u < m_p;
  148. if (level >= 1)
  149. {
  150. pass = pass && m_p * m_q == m_n;
  151. pass = pass && RelativelyPrime(m_e, m_p+1);
  152. pass = pass && RelativelyPrime(m_e, m_p-1);
  153. pass = pass && RelativelyPrime(m_e, m_q+1);
  154. pass = pass && RelativelyPrime(m_e, m_q-1);
  155. pass = pass && m_u * m_q % m_p == 1;
  156. }
  157. if (level >= 2)
  158. pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
  159. return pass;
  160. }
  161. bool InvertibleLUCFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
  162. {
  163. return GetValueHelper<LUCFunction>(this, name, valueType, pValue).Assignable()
  164. CRYPTOPP_GET_FUNCTION_ENTRY(Prime1)
  165. CRYPTOPP_GET_FUNCTION_ENTRY(Prime2)
  166. CRYPTOPP_GET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
  167. ;
  168. }
  169. void InvertibleLUCFunction::AssignFrom(const NameValuePairs &source)
  170. {
  171. AssignFromHelper<LUCFunction>(this, source)
  172. CRYPTOPP_SET_FUNCTION_ENTRY(Prime1)
  173. CRYPTOPP_SET_FUNCTION_ENTRY(Prime2)
  174. CRYPTOPP_SET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
  175. ;
  176. }
  177. NAMESPACE_END