X9ECParameters.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.Field;
  7. namespace BestHTTP.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. ECPoint 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. ECPoint g,
  76. BigInteger n,
  77. BigInteger h)
  78. : this(curve, g, n, h, null)
  79. {
  80. }
  81. public X9ECParameters(
  82. ECCurve curve,
  83. ECPoint g,
  84. BigInteger n,
  85. BigInteger h,
  86. byte[] seed)
  87. : this(curve, new X9ECPoint(g), n, h, seed)
  88. {
  89. }
  90. public X9ECParameters(
  91. ECCurve curve,
  92. X9ECPoint g,
  93. BigInteger n,
  94. BigInteger h,
  95. byte[] seed)
  96. {
  97. this.curve = curve;
  98. this.g = g;
  99. this.n = n;
  100. this.h = h;
  101. this.seed = seed;
  102. if (ECAlgorithms.IsFpCurve(curve))
  103. {
  104. this.fieldID = new X9FieldID(curve.Field.Characteristic);
  105. }
  106. else if (ECAlgorithms.IsF2mCurve(curve))
  107. {
  108. IPolynomialExtensionField field = (IPolynomialExtensionField)curve.Field;
  109. int[] exponents = field.MinimalPolynomial.GetExponentsPresent();
  110. if (exponents.Length == 3)
  111. {
  112. this.fieldID = new X9FieldID(exponents[2], exponents[1]);
  113. }
  114. else if (exponents.Length == 5)
  115. {
  116. this.fieldID = new X9FieldID(exponents[4], exponents[1], exponents[2], exponents[3]);
  117. }
  118. else
  119. {
  120. throw new ArgumentException("Only trinomial and pentomial curves are supported");
  121. }
  122. }
  123. else
  124. {
  125. throw new ArgumentException("'curve' is of an unsupported type");
  126. }
  127. }
  128. public ECCurve Curve
  129. {
  130. get { return curve; }
  131. }
  132. public ECPoint G
  133. {
  134. get { return g.Point; }
  135. }
  136. public BigInteger N
  137. {
  138. get { return n; }
  139. }
  140. public BigInteger H
  141. {
  142. get { return h; }
  143. }
  144. public byte[] GetSeed()
  145. {
  146. return seed;
  147. }
  148. /**
  149. * Return the ASN.1 entry representing the Curve.
  150. *
  151. * @return the X9Curve for the curve in these parameters.
  152. */
  153. public X9Curve CurveEntry
  154. {
  155. get { return new X9Curve(curve, seed); }
  156. }
  157. /**
  158. * Return the ASN.1 entry representing the FieldID.
  159. *
  160. * @return the X9FieldID for the FieldID in these parameters.
  161. */
  162. public X9FieldID FieldIDEntry
  163. {
  164. get { return fieldID; }
  165. }
  166. /**
  167. * Return the ASN.1 entry representing the base point G.
  168. *
  169. * @return the X9ECPoint for the base point in these parameters.
  170. */
  171. public X9ECPoint BaseEntry
  172. {
  173. get { return g; }
  174. }
  175. /**
  176. * Produce an object suitable for an Asn1OutputStream.
  177. * <pre>
  178. * ECParameters ::= Sequence {
  179. * version Integer { ecpVer1(1) } (ecpVer1),
  180. * fieldID FieldID {{FieldTypes}},
  181. * curve X9Curve,
  182. * base X9ECPoint,
  183. * order Integer,
  184. * cofactor Integer OPTIONAL
  185. * }
  186. * </pre>
  187. */
  188. public override Asn1Object ToAsn1Object()
  189. {
  190. Asn1EncodableVector v = new Asn1EncodableVector(
  191. new DerInteger(BigInteger.One),
  192. fieldID,
  193. new X9Curve(curve, seed),
  194. g,
  195. new DerInteger(n));
  196. if (h != null)
  197. {
  198. v.Add(new DerInteger(h));
  199. }
  200. return new DerSequence(v);
  201. }
  202. }
  203. }
  204. #pragma warning restore
  205. #endif