X9FieldID.cs 4.2 KB

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