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.

292 lines
7.0 KiB

  1. // ec2n.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #ifndef CRYPTOPP_IMPORTS
  4. #include "ec2n.h"
  5. #include "asn.h"
  6. #include "algebra.cpp"
  7. #include "eprecomp.cpp"
  8. NAMESPACE_BEGIN(CryptoPP)
  9. EC2N::EC2N(BufferedTransformation &bt)
  10. : m_field(BERDecodeGF2NP(bt))
  11. {
  12. BERSequenceDecoder seq(bt);
  13. m_field->BERDecodeElement(seq, m_a);
  14. m_field->BERDecodeElement(seq, m_b);
  15. // skip optional seed
  16. if (!seq.EndReached())
  17. {
  18. SecByteBlock seed;
  19. unsigned int unused;
  20. BERDecodeBitString(seq, seed, unused);
  21. }
  22. seq.MessageEnd();
  23. }
  24. void EC2N::DEREncode(BufferedTransformation &bt) const
  25. {
  26. m_field->DEREncode(bt);
  27. DERSequenceEncoder seq(bt);
  28. m_field->DEREncodeElement(seq, m_a);
  29. m_field->DEREncodeElement(seq, m_b);
  30. seq.MessageEnd();
  31. }
  32. bool EC2N::DecodePoint(EC2N::Point &P, const byte *encodedPoint, size_t encodedPointLen) const
  33. {
  34. StringStore store(encodedPoint, encodedPointLen);
  35. return DecodePoint(P, store, encodedPointLen);
  36. }
  37. bool EC2N::DecodePoint(EC2N::Point &P, BufferedTransformation &bt, size_t encodedPointLen) const
  38. {
  39. byte type;
  40. if (encodedPointLen < 1 || !bt.Get(type))
  41. return false;
  42. switch (type)
  43. {
  44. case 0:
  45. P.identity = true;
  46. return true;
  47. case 2:
  48. case 3:
  49. {
  50. if (encodedPointLen != EncodedPointSize(true))
  51. return false;
  52. P.identity = false;
  53. P.x.Decode(bt, m_field->MaxElementByteLength());
  54. if (P.x.IsZero())
  55. {
  56. P.y = m_field->SquareRoot(m_b);
  57. return true;
  58. }
  59. FieldElement z = m_field->Square(P.x);
  60. assert(P.x == m_field->SquareRoot(z));
  61. P.y = m_field->Divide(m_field->Add(m_field->Multiply(z, m_field->Add(P.x, m_a)), m_b), z);
  62. assert(P.x == m_field->Subtract(m_field->Divide(m_field->Subtract(m_field->Multiply(P.y, z), m_b), z), m_a));
  63. z = m_field->SolveQuadraticEquation(P.y);
  64. assert(m_field->Add(m_field->Square(z), z) == P.y);
  65. z.SetCoefficient(0, type & 1);
  66. P.y = m_field->Multiply(z, P.x);
  67. return true;
  68. }
  69. case 4:
  70. {
  71. if (encodedPointLen != EncodedPointSize(false))
  72. return false;
  73. unsigned int len = m_field->MaxElementByteLength();
  74. P.identity = false;
  75. P.x.Decode(bt, len);
  76. P.y.Decode(bt, len);
  77. return true;
  78. }
  79. default:
  80. return false;
  81. }
  82. }
  83. void EC2N::EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const
  84. {
  85. if (P.identity)
  86. NullStore().TransferTo(bt, EncodedPointSize(compressed));
  87. else if (compressed)
  88. {
  89. bt.Put(2 + (!P.x ? 0 : m_field->Divide(P.y, P.x).GetBit(0)));
  90. P.x.Encode(bt, m_field->MaxElementByteLength());
  91. }
  92. else
  93. {
  94. unsigned int len = m_field->MaxElementByteLength();
  95. bt.Put(4); // uncompressed
  96. P.x.Encode(bt, len);
  97. P.y.Encode(bt, len);
  98. }
  99. }
  100. void EC2N::EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const
  101. {
  102. ArraySink sink(encodedPoint, EncodedPointSize(compressed));
  103. EncodePoint(sink, P, compressed);
  104. assert(sink.TotalPutLength() == EncodedPointSize(compressed));
  105. }
  106. EC2N::Point EC2N::BERDecodePoint(BufferedTransformation &bt) const
  107. {
  108. SecByteBlock str;
  109. BERDecodeOctetString(bt, str);
  110. Point P;
  111. if (!DecodePoint(P, str, str.size()))
  112. BERDecodeError();
  113. return P;
  114. }
  115. void EC2N::DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const
  116. {
  117. SecByteBlock str(EncodedPointSize(compressed));
  118. EncodePoint(str, P, compressed);
  119. DEREncodeOctetString(bt, str);
  120. }
  121. bool EC2N::ValidateParameters(RandomNumberGenerator &rng, unsigned int level) const
  122. {
  123. bool pass = !!m_b;
  124. pass = pass && m_a.CoefficientCount() <= m_field->MaxElementBitLength();
  125. pass = pass && m_b.CoefficientCount() <= m_field->MaxElementBitLength();
  126. if (level >= 1)
  127. pass = pass && m_field->GetModulus().IsIrreducible();
  128. return pass;
  129. }
  130. bool EC2N::VerifyPoint(const Point &P) const
  131. {
  132. const FieldElement &x = P.x, &y = P.y;
  133. return P.identity ||
  134. (x.CoefficientCount() <= m_field->MaxElementBitLength()
  135. && y.CoefficientCount() <= m_field->MaxElementBitLength()
  136. && !(((x+m_a)*x*x+m_b-(x+y)*y)%m_field->GetModulus()));
  137. }
  138. bool EC2N::Equal(const Point &P, const Point &Q) const
  139. {
  140. if (P.identity && Q.identity)
  141. return true;
  142. if (P.identity && !Q.identity)
  143. return false;
  144. if (!P.identity && Q.identity)
  145. return false;
  146. return (m_field->Equal(P.x,Q.x) && m_field->Equal(P.y,Q.y));
  147. }
  148. const EC2N::Point& EC2N::Identity() const
  149. {
  150. return Singleton<Point>().Ref();
  151. }
  152. const EC2N::Point& EC2N::Inverse(const Point &P) const
  153. {
  154. if (P.identity)
  155. return P;
  156. else
  157. {
  158. m_R.identity = false;
  159. m_R.y = m_field->Add(P.x, P.y);
  160. m_R.x = P.x;
  161. return m_R;
  162. }
  163. }
  164. const EC2N::Point& EC2N::Add(const Point &P, const Point &Q) const
  165. {
  166. if (P.identity) return Q;
  167. if (Q.identity) return P;
  168. if (Equal(P, Q)) return Double(P);
  169. if (m_field->Equal(P.x, Q.x) && m_field->Equal(P.y, m_field->Add(Q.x, Q.y))) return Identity();
  170. FieldElement t = m_field->Add(P.y, Q.y);
  171. t = m_field->Divide(t, m_field->Add(P.x, Q.x));
  172. FieldElement x = m_field->Square(t);
  173. m_field->Accumulate(x, t);
  174. m_field->Accumulate(x, Q.x);
  175. m_field->Accumulate(x, m_a);
  176. m_R.y = m_field->Add(P.y, m_field->Multiply(t, x));
  177. m_field->Accumulate(x, P.x);
  178. m_field->Accumulate(m_R.y, x);
  179. m_R.x.swap(x);
  180. m_R.identity = false;
  181. return m_R;
  182. }
  183. const EC2N::Point& EC2N::Double(const Point &P) const
  184. {
  185. if (P.identity) return P;
  186. if (!m_field->IsUnit(P.x)) return Identity();
  187. FieldElement t = m_field->Divide(P.y, P.x);
  188. m_field->Accumulate(t, P.x);
  189. m_R.y = m_field->Square(P.x);
  190. m_R.x = m_field->Square(t);
  191. m_field->Accumulate(m_R.x, t);
  192. m_field->Accumulate(m_R.x, m_a);
  193. m_field->Accumulate(m_R.y, m_field->Multiply(t, m_R.x));
  194. m_field->Accumulate(m_R.y, m_R.x);
  195. m_R.identity = false;
  196. return m_R;
  197. }
  198. // ********************************************************
  199. /*
  200. EcPrecomputation<EC2N>& EcPrecomputation<EC2N>::operator=(const EcPrecomputation<EC2N> &rhs)
  201. {
  202. m_ec = rhs.m_ec;
  203. m_ep = rhs.m_ep;
  204. m_ep.m_group = m_ec.get();
  205. return *this;
  206. }
  207. void EcPrecomputation<EC2N>::SetCurveAndBase(const EC2N &ec, const EC2N::Point &base)
  208. {
  209. m_ec.reset(new EC2N(ec));
  210. m_ep.SetGroupAndBase(*m_ec, base);
  211. }
  212. void EcPrecomputation<EC2N>::Precompute(unsigned int maxExpBits, unsigned int storage)
  213. {
  214. m_ep.Precompute(maxExpBits, storage);
  215. }
  216. void EcPrecomputation<EC2N>::Load(BufferedTransformation &bt)
  217. {
  218. BERSequenceDecoder seq(bt);
  219. word32 version;
  220. BERDecodeUnsigned<word32>(seq, version, INTEGER, 1, 1);
  221. m_ep.m_exponentBase.BERDecode(seq);
  222. m_ep.m_windowSize = m_ep.m_exponentBase.BitCount() - 1;
  223. m_ep.m_bases.clear();
  224. while (!seq.EndReached())
  225. m_ep.m_bases.push_back(m_ec->BERDecodePoint(seq));
  226. seq.MessageEnd();
  227. }
  228. void EcPrecomputation<EC2N>::Save(BufferedTransformation &bt) const
  229. {
  230. DERSequenceEncoder seq(bt);
  231. DEREncodeUnsigned<word32>(seq, 1); // version
  232. m_ep.m_exponentBase.DEREncode(seq);
  233. for (unsigned i=0; i<m_ep.m_bases.size(); i++)
  234. m_ec->DEREncodePoint(seq, m_ep.m_bases[i]);
  235. seq.MessageEnd();
  236. }
  237. EC2N::Point EcPrecomputation<EC2N>::Exponentiate(const Integer &exponent) const
  238. {
  239. return m_ep.Exponentiate(exponent);
  240. }
  241. EC2N::Point EcPrecomputation<EC2N>::CascadeExponentiate(const Integer &exponent, const DL_FixedBasePrecomputation<Element> &pc2, const Integer &exponent2) const
  242. {
  243. return m_ep.CascadeExponentiate(exponent, static_cast<const EcPrecomputation<EC2N> &>(pc2).m_ep, exponent2);
  244. }
  245. */
  246. NAMESPACE_END
  247. #endif