DHParametersHelper.cs 5.4 KB

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