X9Curve.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. public X9Curve(
  46. X9FieldID fieldID,
  47. Asn1Sequence seq)
  48. : this(fieldID, null, null, seq)
  49. {
  50. }
  51. public X9Curve(
  52. X9FieldID fieldID,
  53. BigInteger order,
  54. BigInteger cofactor,
  55. Asn1Sequence seq)
  56. {
  57. if (fieldID == null)
  58. throw new ArgumentNullException("fieldID");
  59. if (seq == null)
  60. throw new ArgumentNullException("seq");
  61. this.fieldIdentifier = fieldID.Identifier;
  62. if (fieldIdentifier.Equals(X9ObjectIdentifiers.PrimeField))
  63. {
  64. BigInteger p = ((DerInteger)fieldID.Parameters).Value;
  65. BigInteger A = new BigInteger(1, Asn1OctetString.GetInstance(seq[0]).GetOctets());
  66. BigInteger B = new BigInteger(1, Asn1OctetString.GetInstance(seq[1]).GetOctets());
  67. curve = new FpCurve(p, A, B, order, cofactor);
  68. }
  69. else if (fieldIdentifier.Equals(X9ObjectIdentifiers.CharacteristicTwoField))
  70. {
  71. // Characteristic two field
  72. DerSequence parameters = (DerSequence)fieldID.Parameters;
  73. int m = ((DerInteger)parameters[0]).IntValueExact;
  74. DerObjectIdentifier representation = (DerObjectIdentifier)parameters[1];
  75. int k1 = 0;
  76. int k2 = 0;
  77. int k3 = 0;
  78. if (representation.Equals(X9ObjectIdentifiers.TPBasis))
  79. {
  80. // Trinomial basis representation
  81. k1 = ((DerInteger)parameters[2]).IntValueExact;
  82. }
  83. else
  84. {
  85. // Pentanomial basis representation
  86. DerSequence pentanomial = (DerSequence) parameters[2];
  87. k1 = ((DerInteger)pentanomial[0]).IntValueExact;
  88. k2 = ((DerInteger)pentanomial[1]).IntValueExact;
  89. k3 = ((DerInteger)pentanomial[2]).IntValueExact;
  90. }
  91. BigInteger A = new BigInteger(1, Asn1OctetString.GetInstance(seq[0]).GetOctets());
  92. BigInteger B = new BigInteger(1, Asn1OctetString.GetInstance(seq[1]).GetOctets());
  93. curve = new F2mCurve(m, k1, k2, k3, A, B, order, cofactor);
  94. }
  95. else
  96. {
  97. throw new ArgumentException("This type of ECCurve is not implemented");
  98. }
  99. if (seq.Count == 3)
  100. {
  101. seed = ((DerBitString)seq[2]).GetBytes();
  102. }
  103. }
  104. public ECCurve Curve
  105. {
  106. get { return curve; }
  107. }
  108. public byte[] GetSeed()
  109. {
  110. return Arrays.Clone(seed);
  111. }
  112. /**
  113. * Produce an object suitable for an Asn1OutputStream.
  114. * <pre>
  115. * Curve ::= Sequence {
  116. * a FieldElement,
  117. * b FieldElement,
  118. * seed BIT STRING OPTIONAL
  119. * }
  120. * </pre>
  121. */
  122. public override Asn1Object ToAsn1Object()
  123. {
  124. Asn1EncodableVector v = new Asn1EncodableVector();
  125. if (fieldIdentifier.Equals(X9ObjectIdentifiers.PrimeField)
  126. || fieldIdentifier.Equals(X9ObjectIdentifiers.CharacteristicTwoField))
  127. {
  128. v.Add(new X9FieldElement(curve.A).ToAsn1Object());
  129. v.Add(new X9FieldElement(curve.B).ToAsn1Object());
  130. }
  131. if (seed != null)
  132. {
  133. v.Add(new DerBitString(seed));
  134. }
  135. return new DerSequence(v);
  136. }
  137. }
  138. }
  139. #pragma warning restore
  140. #endif