DHParameters.cs 3.9 KB

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