DHParametersHelper.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Math;
  4. using Org.BouncyCastle.Math.EC.Multiplier;
  5. using Org.BouncyCastle.Security;
  6. using Org.BouncyCastle.Utilities;
  7. namespace Org.BouncyCastle.Crypto.Generators
  8. {
  9. internal class DHParametersHelper
  10. {
  11. private static readonly BigInteger Six = BigInteger.ValueOf(6);
  12. private static readonly int[][] primeLists = BigInteger.primeLists;
  13. private static readonly int[] primeProducts = BigInteger.primeProducts;
  14. private static readonly BigInteger[] BigPrimeProducts = ConstructBigPrimeProducts(primeProducts);
  15. private static BigInteger[] ConstructBigPrimeProducts(int[] primeProducts)
  16. {
  17. BigInteger[] bpp = new BigInteger[primeProducts.Length];
  18. for (int i = 0; i < bpp.Length; ++i)
  19. {
  20. bpp[i] = BigInteger.ValueOf(primeProducts[i]);
  21. }
  22. return bpp;
  23. }
  24. /*
  25. * Finds a pair of prime BigInteger's {p, q: p = 2q + 1}
  26. *
  27. * (see: Handbook of Applied Cryptography 4.86)
  28. */
  29. internal static BigInteger[] GenerateSafePrimes(int size, int certainty, SecureRandom random)
  30. {
  31. BigInteger p, q;
  32. int qLength = size - 1;
  33. int minWeight = size >> 2;
  34. if (size <= 32)
  35. {
  36. for (;;)
  37. {
  38. q = new BigInteger(qLength, 2, random);
  39. p = q.ShiftLeft(1).Add(BigInteger.One);
  40. if (!p.IsProbablePrime(certainty, true))
  41. continue;
  42. if (certainty > 2 && !q.IsProbablePrime(certainty, true))
  43. continue;
  44. break;
  45. }
  46. }
  47. else
  48. {
  49. // Note: Modified from Java version for speed
  50. for (;;)
  51. {
  52. q = new BigInteger(qLength, 0, random);
  53. retry:
  54. for (int i = 0; i < primeLists.Length; ++i)
  55. {
  56. int test = q.Remainder(BigPrimeProducts[i]).IntValue;
  57. if (i == 0)
  58. {
  59. int rem3 = test % 3;
  60. if (rem3 != 2)
  61. {
  62. int diff = 2 * rem3 + 2;
  63. q = q.Add(BigInteger.ValueOf(diff));
  64. test = (test + diff) % primeProducts[i];
  65. }
  66. }
  67. int[] primeList = primeLists[i];
  68. for (int j = 0; j < primeList.Length; ++j)
  69. {
  70. int prime = primeList[j];
  71. int qRem = test % prime;
  72. if (qRem == 0 || qRem == (prime >> 1))
  73. {
  74. q = q.Add(Six);
  75. goto retry;
  76. }
  77. }
  78. }
  79. if (q.BitLength != qLength)
  80. continue;
  81. if (!q.RabinMillerTest(2, random, true))
  82. continue;
  83. p = q.ShiftLeft(1).Add(BigInteger.One);
  84. if (!p.RabinMillerTest(certainty, random, true))
  85. continue;
  86. if (certainty > 2 && !q.RabinMillerTest(certainty - 2, random, true))
  87. continue;
  88. /*
  89. * Require a minimum weight of the NAF representation, since low-weight primes may be
  90. * weak against a version of the number-field-sieve for the discrete-logarithm-problem.
  91. *
  92. * See "The number field sieve for integers of low weight", Oliver Schirokauer.
  93. */
  94. if (WNafUtilities.GetNafWeight(p) < minWeight)
  95. continue;
  96. break;
  97. }
  98. }
  99. return new BigInteger[] { p, q };
  100. }
  101. /*
  102. * Select a high order element of the multiplicative group Zp*
  103. *
  104. * p and q must be s.t. p = 2*q + 1, where p and q are prime (see generateSafePrimes)
  105. */
  106. internal static BigInteger SelectGenerator(BigInteger p, BigInteger q, SecureRandom random)
  107. {
  108. BigInteger pMinusTwo = p.Subtract(BigInteger.Two);
  109. BigInteger g;
  110. /*
  111. * (see: Handbook of Applied Cryptography 4.80)
  112. */
  113. // do
  114. // {
  115. // g = BigIntegers.CreateRandomInRange(BigInteger.Two, pMinusTwo, random);
  116. // }
  117. // while (g.ModPow(BigInteger.Two, p).Equals(BigInteger.One)
  118. // || g.ModPow(q, p).Equals(BigInteger.One));
  119. /*
  120. * RFC 2631 2.2.1.2 (and see: Handbook of Applied Cryptography 4.81)
  121. */
  122. do
  123. {
  124. BigInteger h = BigIntegers.CreateRandomInRange(BigInteger.Two, pMinusTwo, random);
  125. g = h.ModPow(BigInteger.Two, p);
  126. }
  127. while (g.Equals(BigInteger.One));
  128. return g;
  129. }
  130. }
  131. }
  132. #endif