ECDomainParameters.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X9;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Math.EC;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  8. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters
  9. {
  10. public class ECDomainParameters
  11. {
  12. private readonly ECCurve curve;
  13. private readonly byte[] seed;
  14. private readonly ECPoint g;
  15. private readonly BigInteger n;
  16. private readonly BigInteger h;
  17. private BigInteger hInv;
  18. public ECDomainParameters(X9ECParameters x9)
  19. : this(x9.Curve, x9.G, x9.N, x9.H, x9.GetSeed())
  20. {
  21. }
  22. public ECDomainParameters(
  23. ECCurve curve,
  24. ECPoint g,
  25. BigInteger n)
  26. : this(curve, g, n, BigInteger.One, null)
  27. {
  28. }
  29. public ECDomainParameters(
  30. ECCurve curve,
  31. ECPoint g,
  32. BigInteger n,
  33. BigInteger h)
  34. : this(curve, g, n, h, null)
  35. {
  36. }
  37. public ECDomainParameters(
  38. ECCurve curve,
  39. ECPoint g,
  40. BigInteger n,
  41. BigInteger h,
  42. byte[] seed)
  43. {
  44. if (curve == null)
  45. throw new ArgumentNullException("curve");
  46. if (g == null)
  47. throw new ArgumentNullException("g");
  48. if (n == null)
  49. throw new ArgumentNullException("n");
  50. // we can't check for h == null here as h is optional in X9.62 as it is not required for ECDSA
  51. this.curve = curve;
  52. this.g = ValidatePublicPoint(curve, g);
  53. this.n = n;
  54. this.h = h;
  55. this.seed = Arrays.Clone(seed);
  56. }
  57. public ECCurve Curve
  58. {
  59. get { return curve; }
  60. }
  61. public ECPoint G
  62. {
  63. get { return g; }
  64. }
  65. public BigInteger N
  66. {
  67. get { return n; }
  68. }
  69. public BigInteger H
  70. {
  71. get { return h; }
  72. }
  73. public BigInteger HInv
  74. {
  75. get
  76. {
  77. lock (this)
  78. {
  79. if (hInv == null)
  80. {
  81. hInv = BigIntegers.ModOddInverseVar(n, h);
  82. }
  83. return hInv;
  84. }
  85. }
  86. }
  87. public byte[] GetSeed()
  88. {
  89. return Arrays.Clone(seed);
  90. }
  91. public override bool Equals(
  92. object obj)
  93. {
  94. if (obj == this)
  95. return true;
  96. ECDomainParameters other = obj as ECDomainParameters;
  97. if (other == null)
  98. return false;
  99. return Equals(other);
  100. }
  101. protected virtual bool Equals(
  102. ECDomainParameters other)
  103. {
  104. return curve.Equals(other.curve)
  105. && g.Equals(other.g)
  106. && n.Equals(other.n);
  107. }
  108. public override int GetHashCode()
  109. {
  110. //return Arrays.GetHashCode(new object[]{ curve, g, n });
  111. int hc = 4;
  112. hc *= 257;
  113. hc ^= curve.GetHashCode();
  114. hc *= 257;
  115. hc ^= g.GetHashCode();
  116. hc *= 257;
  117. hc ^= n.GetHashCode();
  118. return hc;
  119. }
  120. public BigInteger ValidatePrivateScalar(BigInteger d)
  121. {
  122. if (null == d)
  123. throw new ArgumentNullException("d", "Scalar cannot be null");
  124. if (d.CompareTo(BigInteger.One) < 0 || (d.CompareTo(N) >= 0))
  125. throw new ArgumentException("Scalar is not in the interval [1, n - 1]", "d");
  126. return d;
  127. }
  128. public ECPoint ValidatePublicPoint(ECPoint q)
  129. {
  130. return ValidatePublicPoint(Curve, q);
  131. }
  132. internal static ECPoint ValidatePublicPoint(ECCurve c, ECPoint q)
  133. {
  134. if (null == q)
  135. throw new ArgumentNullException("q", "Point cannot be null");
  136. q = ECAlgorithms.ImportPoint(c, q).Normalize();
  137. if (q.IsInfinity)
  138. throw new ArgumentException("Point at infinity", "q");
  139. if (!q.IsValid())
  140. throw new ArgumentException("Point not on curve", "q");
  141. return q;
  142. }
  143. }
  144. }
  145. #pragma warning restore
  146. #endif