X9FieldID.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9
  6. {
  7. /**
  8. * ASN.1 def for Elliptic-Curve Field ID structure. See
  9. * X9.62, for further details.
  10. */
  11. public class X9FieldID
  12. : Asn1Encodable
  13. {
  14. private readonly DerObjectIdentifier id;
  15. private readonly Asn1Object parameters;
  16. /**
  17. * Constructor for elliptic curves over prime fields
  18. * <code>F<sub>2</sub></code>.
  19. * @param primeP The prime <code>p</code> defining the prime field.
  20. */
  21. public X9FieldID(
  22. BigInteger primeP)
  23. {
  24. this.id = X9ObjectIdentifiers.PrimeField;
  25. this.parameters = new DerInteger(primeP);
  26. }
  27. /**
  28. * Constructor for elliptic curves over binary fields
  29. * <code>F<sub>2<sup>m</sup></sub></code>.
  30. * @param m The exponent <code>m</code> of
  31. * <code>F<sub>2<sup>m</sup></sub></code>.
  32. * @param k1 The integer <code>k1</code> where <code>x<sup>m</sup> +
  33. * x<sup>k1</sup> + 1</code>
  34. * represents the reduction polynomial <code>f(z)</code>.
  35. */
  36. public X9FieldID(int m, int k1)
  37. : this(m, k1, 0, 0)
  38. {
  39. }
  40. /**
  41. * Constructor for elliptic curves over binary fields
  42. * <code>F<sub>2<sup>m</sup></sub></code>.
  43. * @param m The exponent <code>m</code> of
  44. * <code>F<sub>2<sup>m</sup></sub></code>.
  45. * @param k1 The integer <code>k1</code> where <code>x<sup>m</sup> +
  46. * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
  47. * represents the reduction polynomial <code>f(z)</code>.
  48. * @param k2 The integer <code>k2</code> where <code>x<sup>m</sup> +
  49. * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
  50. * represents the reduction polynomial <code>f(z)</code>.
  51. * @param k3 The integer <code>k3</code> where <code>x<sup>m</sup> +
  52. * x<sup>k3</sup> + x<sup>k2</sup> + x<sup>k1</sup> + 1</code>
  53. * represents the reduction polynomial <code>f(z)</code>..
  54. */
  55. public X9FieldID(
  56. int m,
  57. int k1,
  58. int k2,
  59. int k3)
  60. {
  61. this.id = X9ObjectIdentifiers.CharacteristicTwoField;
  62. Asn1EncodableVector fieldIdParams = new Asn1EncodableVector(new DerInteger(m));
  63. if (k2 == 0)
  64. {
  65. if (k3 != 0)
  66. throw new ArgumentException("inconsistent k values");
  67. fieldIdParams.Add(
  68. X9ObjectIdentifiers.TPBasis,
  69. new DerInteger(k1));
  70. }
  71. else
  72. {
  73. if (k2 <= k1 || k3 <= k2)
  74. throw new ArgumentException("inconsistent k values");
  75. fieldIdParams.Add(
  76. X9ObjectIdentifiers.PPBasis,
  77. new DerSequence(
  78. new DerInteger(k1),
  79. new DerInteger(k2),
  80. new DerInteger(k3)));
  81. }
  82. this.parameters = new DerSequence(fieldIdParams);
  83. }
  84. private X9FieldID(Asn1Sequence seq)
  85. {
  86. this.id = DerObjectIdentifier.GetInstance(seq[0]);
  87. this.parameters = seq[1].ToAsn1Object();
  88. }
  89. public static X9FieldID GetInstance(object obj)
  90. {
  91. if (obj is X9FieldID)
  92. return (X9FieldID)obj;
  93. if (obj == null)
  94. return null;
  95. return new X9FieldID(Asn1Sequence.GetInstance(obj));
  96. }
  97. public DerObjectIdentifier Identifier
  98. {
  99. get { return id; }
  100. }
  101. public Asn1Object Parameters
  102. {
  103. get { return parameters; }
  104. }
  105. /**
  106. * Produce a Der encoding of the following structure.
  107. * <pre>
  108. * FieldID ::= Sequence {
  109. * fieldType FIELD-ID.&amp;id({IOSet}),
  110. * parameters FIELD-ID.&amp;Type({IOSet}{&#64;fieldType})
  111. * }
  112. * </pre>
  113. */
  114. public override Asn1Object ToAsn1Object()
  115. {
  116. return new DerSequence(id, parameters);
  117. }
  118. }
  119. }
  120. #pragma warning restore
  121. #endif