ECDomainParameters.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.Crypto.Parameters
  8. {
  9. public class ECDomainParameters
  10. {
  11. internal ECCurve curve;
  12. internal byte[] seed;
  13. internal ECPoint g;
  14. internal BigInteger n;
  15. internal BigInteger h;
  16. internal BigInteger hInv;
  17. public ECDomainParameters(
  18. ECCurve curve,
  19. ECPoint g,
  20. BigInteger n)
  21. : this(curve, g, n, BigInteger.One, null)
  22. {
  23. }
  24. public ECDomainParameters(
  25. ECCurve curve,
  26. ECPoint g,
  27. BigInteger n,
  28. BigInteger h)
  29. : this(curve, g, n, h, null)
  30. {
  31. }
  32. public ECDomainParameters(
  33. ECCurve curve,
  34. ECPoint g,
  35. BigInteger n,
  36. BigInteger h,
  37. byte[] seed)
  38. {
  39. if (curve == null)
  40. throw new ArgumentNullException("curve");
  41. if (g == null)
  42. throw new ArgumentNullException("g");
  43. if (n == null)
  44. throw new ArgumentNullException("n");
  45. // we can't check for h == null here as h is optional in X9.62 as it is not required for ECDSA
  46. this.curve = curve;
  47. this.g = Validate(curve, g);
  48. this.n = n;
  49. this.h = h;
  50. this.seed = Arrays.Clone(seed);
  51. }
  52. public ECCurve Curve
  53. {
  54. get { return curve; }
  55. }
  56. public ECPoint G
  57. {
  58. get { return g; }
  59. }
  60. public BigInteger N
  61. {
  62. get { return n; }
  63. }
  64. public BigInteger H
  65. {
  66. get { return h; }
  67. }
  68. public BigInteger HInv
  69. {
  70. get
  71. {
  72. lock (this)
  73. {
  74. if (hInv == null)
  75. {
  76. hInv = h.ModInverse(n);
  77. }
  78. return hInv;
  79. }
  80. }
  81. }
  82. public byte[] GetSeed()
  83. {
  84. return Arrays.Clone(seed);
  85. }
  86. public override bool Equals(
  87. object obj)
  88. {
  89. if (obj == this)
  90. return true;
  91. ECDomainParameters other = obj as ECDomainParameters;
  92. if (other == null)
  93. return false;
  94. return Equals(other);
  95. }
  96. protected virtual bool Equals(
  97. ECDomainParameters other)
  98. {
  99. return curve.Equals(other.curve)
  100. && g.Equals(other.g)
  101. && n.Equals(other.n)
  102. && h.Equals(other.h);
  103. }
  104. public override int GetHashCode()
  105. {
  106. int hc = curve.GetHashCode();
  107. hc *= 37;
  108. hc ^= g.GetHashCode();
  109. hc *= 37;
  110. hc ^= n.GetHashCode();
  111. hc *= 37;
  112. hc ^= h.GetHashCode();
  113. return hc;
  114. }
  115. internal static ECPoint Validate(ECCurve c, ECPoint q)
  116. {
  117. if (q == null)
  118. throw new ArgumentException("Point has null value", "q");
  119. q = ECAlgorithms.ImportPoint(c, q).Normalize();
  120. if (q.IsInfinity)
  121. throw new ArgumentException("Point at infinity", "q");
  122. if (!q.IsValid())
  123. throw new ArgumentException("Point not on curve", "q");
  124. return q;
  125. }
  126. }
  127. }
  128. #pragma warning restore
  129. #endif