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.

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