X9ECParameters.cs 5.9 KB

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