X9Curve.cs 5.0 KB

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