DHParameters.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.Utilities;
  6. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters
  7. {
  8. public class DHParameters
  9. : ICipherParameters
  10. {
  11. private const int DefaultMinimumLength = 160;
  12. private readonly BigInteger p, g, q, j;
  13. private readonly int m, l;
  14. private readonly DHValidationParameters validation;
  15. private static int GetDefaultMParam(
  16. int lParam)
  17. {
  18. if (lParam == 0)
  19. return DefaultMinimumLength;
  20. return System.Math.Min(lParam, DefaultMinimumLength);
  21. }
  22. public DHParameters(
  23. BigInteger p,
  24. BigInteger g)
  25. : this(p, g, null, 0)
  26. {
  27. }
  28. public DHParameters(
  29. BigInteger p,
  30. BigInteger g,
  31. BigInteger q)
  32. : this(p, g, q, 0)
  33. {
  34. }
  35. public DHParameters(
  36. BigInteger p,
  37. BigInteger g,
  38. BigInteger q,
  39. int l)
  40. : this(p, g, q, GetDefaultMParam(l), l, null, null)
  41. {
  42. }
  43. public DHParameters(
  44. BigInteger p,
  45. BigInteger g,
  46. BigInteger q,
  47. int m,
  48. int l)
  49. : this(p, g, q, m, l, null, null)
  50. {
  51. }
  52. public DHParameters(
  53. BigInteger p,
  54. BigInteger g,
  55. BigInteger q,
  56. BigInteger j,
  57. DHValidationParameters validation)
  58. : this(p, g, q, DefaultMinimumLength, 0, j, validation)
  59. {
  60. }
  61. public DHParameters(
  62. BigInteger p,
  63. BigInteger g,
  64. BigInteger q,
  65. int m,
  66. int l,
  67. BigInteger j,
  68. DHValidationParameters validation)
  69. {
  70. if (p == null)
  71. throw new ArgumentNullException("p");
  72. if (g == null)
  73. throw new ArgumentNullException("g");
  74. if (!p.TestBit(0))
  75. throw new ArgumentException("field must be an odd prime", "p");
  76. if (g.CompareTo(BigInteger.Two) < 0
  77. || g.CompareTo(p.Subtract(BigInteger.Two)) > 0)
  78. throw new ArgumentException("generator must in the range [2, p - 2]", "g");
  79. if (q != null && q.BitLength >= p.BitLength)
  80. throw new ArgumentException("q too big to be a factor of (p-1)", "q");
  81. if (m >= p.BitLength)
  82. throw new ArgumentException("m value must be < bitlength of p", "m");
  83. if (l != 0)
  84. {
  85. // TODO Check this against the Java version, which has 'l > p.BitLength' here
  86. if (l >= p.BitLength)
  87. throw new ArgumentException("when l value specified, it must be less than bitlength(p)", "l");
  88. if (l < m)
  89. throw new ArgumentException("when l value specified, it may not be less than m value", "l");
  90. }
  91. if (j != null && j.CompareTo(BigInteger.Two) < 0)
  92. throw new ArgumentException("subgroup factor must be >= 2", "j");
  93. // TODO If q, j both provided, validate p = jq + 1 ?
  94. this.p = p;
  95. this.g = g;
  96. this.q = q;
  97. this.m = m;
  98. this.l = l;
  99. this.j = j;
  100. this.validation = validation;
  101. }
  102. public BigInteger P
  103. {
  104. get { return p; }
  105. }
  106. public BigInteger G
  107. {
  108. get { return g; }
  109. }
  110. public BigInteger Q
  111. {
  112. get { return q; }
  113. }
  114. public BigInteger J
  115. {
  116. get { return j; }
  117. }
  118. /// <summary>The minimum bitlength of the private value.</summary>
  119. public int M
  120. {
  121. get { return m; }
  122. }
  123. /// <summary>The bitlength of the private value.</summary>
  124. public int L
  125. {
  126. get { return l; }
  127. }
  128. public DHValidationParameters ValidationParameters
  129. {
  130. get { return validation; }
  131. }
  132. public override bool Equals(
  133. object obj)
  134. {
  135. if (obj == this)
  136. return true;
  137. DHParameters other = obj as DHParameters;
  138. if (other == null)
  139. return false;
  140. return Equals(other);
  141. }
  142. protected virtual bool Equals(
  143. DHParameters other)
  144. {
  145. return p.Equals(other.p)
  146. && g.Equals(other.g)
  147. && BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Equals(q, other.q);
  148. }
  149. public override int GetHashCode()
  150. {
  151. int hc = p.GetHashCode() ^ g.GetHashCode();
  152. if (q != null)
  153. {
  154. hc ^= q.GetHashCode();
  155. }
  156. return hc;
  157. }
  158. }
  159. }
  160. #pragma warning restore
  161. #endif