X9ECParameters.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.EC;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Math.Field;
  7. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9
  8. {
  9. /**
  10. * ASN.1 def for Elliptic-Curve ECParameters structure. See
  11. * X9.62, for further details.
  12. */
  13. public class X9ECParameters
  14. : Asn1Encodable
  15. {
  16. private X9FieldID fieldID;
  17. private ECCurve curve;
  18. private X9ECPoint g;
  19. private BigInteger n;
  20. private BigInteger h;
  21. private byte[] seed;
  22. public static X9ECParameters GetInstance(object obj)
  23. {
  24. if (obj is X9ECParameters)
  25. return (X9ECParameters)obj;
  26. if (obj != null)
  27. return new X9ECParameters(Asn1Sequence.GetInstance(obj));
  28. return null;
  29. }
  30. public X9ECParameters(
  31. Asn1Sequence seq)
  32. {
  33. if (!(seq[0] is DerInteger)
  34. || !((DerInteger)seq[0]).HasValue(1))
  35. {
  36. throw new ArgumentException("bad version in X9ECParameters");
  37. }
  38. this.n = ((DerInteger)seq[4]).Value;
  39. if (seq.Count == 6)
  40. {
  41. this.h = ((DerInteger)seq[5]).Value;
  42. }
  43. X9Curve x9c = new X9Curve(
  44. X9FieldID.GetInstance(seq[1]), n, h,
  45. Asn1Sequence.GetInstance(seq[2]));
  46. this.curve = x9c.Curve;
  47. object p = seq[3];
  48. if (p is X9ECPoint)
  49. {
  50. this.g = (X9ECPoint)p;
  51. }
  52. else
  53. {
  54. this.g = new X9ECPoint(curve, (Asn1OctetString)p);
  55. }
  56. this.seed = x9c.GetSeed();
  57. }
  58. public X9ECParameters(
  59. ECCurve curve,
  60. X9ECPoint g,
  61. BigInteger n)
  62. : this(curve, g, n, null, null)
  63. {
  64. }
  65. public X9ECParameters(
  66. ECCurve curve,
  67. X9ECPoint g,
  68. BigInteger n,
  69. BigInteger h)
  70. : this(curve, g, n, h, null)
  71. {
  72. }
  73. public X9ECParameters(
  74. ECCurve curve,
  75. X9ECPoint g,
  76. BigInteger n,
  77. BigInteger h,
  78. byte[] seed)
  79. {
  80. this.curve = curve;
  81. this.g = g;
  82. this.n = n;
  83. this.h = h;
  84. this.seed = seed;
  85. if (ECAlgorithms.IsFpCurve(curve))
  86. {
  87. this.fieldID = new X9FieldID(curve.Field.Characteristic);
  88. }
  89. else if (ECAlgorithms.IsF2mCurve(curve))
  90. {
  91. IPolynomialExtensionField field = (IPolynomialExtensionField)curve.Field;
  92. int[] exponents = field.MinimalPolynomial.GetExponentsPresent();
  93. if (exponents.Length == 3)
  94. {
  95. this.fieldID = new X9FieldID(exponents[2], exponents[1]);
  96. }
  97. else if (exponents.Length == 5)
  98. {
  99. this.fieldID = new X9FieldID(exponents[4], exponents[1], exponents[2], exponents[3]);
  100. }
  101. else
  102. {
  103. throw new ArgumentException("Only trinomial and pentomial curves are supported");
  104. }
  105. }
  106. else
  107. {
  108. throw new ArgumentException("'curve' is of an unsupported type");
  109. }
  110. }
  111. public ECCurve Curve
  112. {
  113. get { return curve; }
  114. }
  115. public ECPoint G
  116. {
  117. get { return g.Point; }
  118. }
  119. public BigInteger N
  120. {
  121. get { return n; }
  122. }
  123. public BigInteger H
  124. {
  125. get { return h; }
  126. }
  127. public byte[] GetSeed()
  128. {
  129. return seed;
  130. }
  131. /**
  132. * Return the ASN.1 entry representing the Curve.
  133. *
  134. * @return the X9Curve for the curve in these parameters.
  135. */
  136. public X9Curve CurveEntry
  137. {
  138. get { return new X9Curve(curve, seed); }
  139. }
  140. /**
  141. * Return the ASN.1 entry representing the FieldID.
  142. *
  143. * @return the X9FieldID for the FieldID in these parameters.
  144. */
  145. public X9FieldID FieldIDEntry
  146. {
  147. get { return fieldID; }
  148. }
  149. /**
  150. * Return the ASN.1 entry representing the base point G.
  151. *
  152. * @return the X9ECPoint for the base point in these parameters.
  153. */
  154. public X9ECPoint BaseEntry
  155. {
  156. get { return g; }
  157. }
  158. /**
  159. * Produce an object suitable for an Asn1OutputStream.
  160. * <pre>
  161. * ECParameters ::= Sequence {
  162. * version Integer { ecpVer1(1) } (ecpVer1),
  163. * fieldID FieldID {{FieldTypes}},
  164. * curve X9Curve,
  165. * base X9ECPoint,
  166. * order Integer,
  167. * cofactor Integer OPTIONAL
  168. * }
  169. * </pre>
  170. */
  171. public override Asn1Object ToAsn1Object()
  172. {
  173. Asn1EncodableVector v = new Asn1EncodableVector(
  174. new DerInteger(BigInteger.One),
  175. fieldID,
  176. new X9Curve(curve, seed),
  177. g,
  178. new DerInteger(n));
  179. if (h != null)
  180. {
  181. v.Add(new DerInteger(h));
  182. }
  183. return new DerSequence(v);
  184. }
  185. }
  186. }
  187. #pragma warning restore
  188. #endif