X9Curve.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.Utilities;
  7. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9
  8. {
  9. /**
  10. * ASN.1 def for Elliptic-Curve Curve structure. See
  11. * X9.62, for further details.
  12. */
  13. public class X9Curve
  14. : Asn1Encodable
  15. {
  16. private readonly ECCurve curve;
  17. private readonly byte[] seed;
  18. private readonly DerObjectIdentifier fieldIdentifier;
  19. public X9Curve(
  20. ECCurve curve)
  21. : this(curve, null)
  22. {
  23. }
  24. public X9Curve(
  25. ECCurve curve,
  26. byte[] seed)
  27. {
  28. if (curve == null)
  29. throw new ArgumentNullException("curve");
  30. this.curve = curve;
  31. this.seed = Arrays.Clone(seed);
  32. if (ECAlgorithms.IsFpCurve(curve))
  33. {
  34. this.fieldIdentifier = X9ObjectIdentifiers.PrimeField;
  35. }
  36. else if (ECAlgorithms.IsF2mCurve(curve))
  37. {
  38. this.fieldIdentifier = X9ObjectIdentifiers.CharacteristicTwoField;
  39. }
  40. else
  41. {
  42. throw new ArgumentException("This type of ECCurve is not implemented");
  43. }
  44. }
  45. [Obsolete("Use constructor including order/cofactor")]
  46. public X9Curve(
  47. X9FieldID fieldID,
  48. Asn1Sequence seq)
  49. : this(fieldID, null, null, seq)
  50. {
  51. }
  52. public X9Curve(
  53. X9FieldID fieldID,
  54. BigInteger order,
  55. BigInteger cofactor,
  56. Asn1Sequence seq)
  57. {
  58. if (fieldID == null)
  59. throw new ArgumentNullException("fieldID");
  60. if (seq == null)
  61. throw new ArgumentNullException("seq");
  62. this.fieldIdentifier = fieldID.Identifier;
  63. if (fieldIdentifier.Equals(X9ObjectIdentifiers.PrimeField))
  64. {
  65. BigInteger p = ((DerInteger)fieldID.Parameters).Value;
  66. BigInteger A = new BigInteger(1, Asn1OctetString.GetInstance(seq[0]).GetOctets());
  67. BigInteger B = new BigInteger(1, Asn1OctetString.GetInstance(seq[1]).GetOctets());
  68. curve = new FpCurve(p, A, B, order, cofactor);
  69. }
  70. else if (fieldIdentifier.Equals(X9ObjectIdentifiers.CharacteristicTwoField))
  71. {
  72. // Characteristic two field
  73. DerSequence parameters = (DerSequence)fieldID.Parameters;
  74. int m = ((DerInteger)parameters[0]).Value.IntValue;
  75. DerObjectIdentifier representation
  76. = (DerObjectIdentifier)parameters[1];
  77. int k1 = 0;
  78. int k2 = 0;
  79. int k3 = 0;
  80. if (representation.Equals(X9ObjectIdentifiers.TPBasis))
  81. {
  82. // Trinomial basis representation
  83. k1 = ((DerInteger)parameters[2]).Value.IntValue;
  84. }
  85. else
  86. {
  87. // Pentanomial basis representation
  88. DerSequence pentanomial = (DerSequence) parameters[2];
  89. k1 = ((DerInteger) pentanomial[0]).Value.IntValue;
  90. k2 = ((DerInteger) pentanomial[1]).Value.IntValue;
  91. k3 = ((DerInteger) pentanomial[2]).Value.IntValue;
  92. }
  93. BigInteger A = new BigInteger(1, Asn1OctetString.GetInstance(seq[0]).GetOctets());
  94. BigInteger B = new BigInteger(1, Asn1OctetString.GetInstance(seq[1]).GetOctets());
  95. curve = new F2mCurve(m, k1, k2, k3, A, B, order, cofactor);
  96. }
  97. else
  98. {
  99. throw new ArgumentException("This type of ECCurve is not implemented");
  100. }
  101. if (seq.Count == 3)
  102. {
  103. seed = ((DerBitString)seq[2]).GetBytes();
  104. }
  105. }
  106. public ECCurve Curve
  107. {
  108. get { return curve; }
  109. }
  110. public byte[] GetSeed()
  111. {
  112. return Arrays.Clone(seed);
  113. }
  114. /**
  115. * Produce an object suitable for an Asn1OutputStream.
  116. * <pre>
  117. * Curve ::= Sequence {
  118. * a FieldElement,
  119. * b FieldElement,
  120. * seed BIT STRING OPTIONAL
  121. * }
  122. * </pre>
  123. */
  124. public override Asn1Object ToAsn1Object()
  125. {
  126. Asn1EncodableVector v = new Asn1EncodableVector();
  127. if (fieldIdentifier.Equals(X9ObjectIdentifiers.PrimeField)
  128. || fieldIdentifier.Equals(X9ObjectIdentifiers.CharacteristicTwoField))
  129. {
  130. v.Add(new X9FieldElement(curve.A).ToAsn1Object());
  131. v.Add(new X9FieldElement(curve.B).ToAsn1Object());
  132. }
  133. if (seed != null)
  134. {
  135. v.Add(new DerBitString(seed));
  136. }
  137. return new DerSequence(v);
  138. }
  139. }
  140. }
  141. #pragma warning restore
  142. #endif