SP800SecureRandom.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Prng.Drbg;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  6. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Prng
  7. {
  8. public class SP800SecureRandom
  9. : SecureRandom
  10. {
  11. private readonly IDrbgProvider mDrbgProvider;
  12. private readonly bool mPredictionResistant;
  13. private readonly SecureRandom mRandomSource;
  14. private readonly IEntropySource mEntropySource;
  15. private ISP80090Drbg mDrbg;
  16. internal SP800SecureRandom(SecureRandom randomSource, IEntropySource entropySource, IDrbgProvider drbgProvider,
  17. bool predictionResistant)
  18. : base(null)
  19. {
  20. this.mRandomSource = randomSource;
  21. this.mEntropySource = entropySource;
  22. this.mDrbgProvider = drbgProvider;
  23. this.mPredictionResistant = predictionResistant;
  24. }
  25. public override void SetSeed(byte[] seed)
  26. {
  27. lock (this)
  28. {
  29. if (mRandomSource != null)
  30. {
  31. this.mRandomSource.SetSeed(seed);
  32. }
  33. }
  34. }
  35. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  36. public override void SetSeed(Span<byte> seed)
  37. {
  38. lock (this)
  39. {
  40. if (mRandomSource != null)
  41. {
  42. this.mRandomSource.SetSeed(seed);
  43. }
  44. }
  45. }
  46. #endif
  47. public override void SetSeed(long seed)
  48. {
  49. lock (this)
  50. {
  51. // this will happen when SecureRandom() is created
  52. if (mRandomSource != null)
  53. {
  54. this.mRandomSource.SetSeed(seed);
  55. }
  56. }
  57. }
  58. public override void NextBytes(byte[] bytes)
  59. {
  60. NextBytes(bytes, 0, bytes.Length);
  61. }
  62. public override void NextBytes(byte[] buf, int off, int len)
  63. {
  64. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  65. NextBytes(buf.AsSpan(off, len));
  66. #else
  67. lock (this)
  68. {
  69. if (mDrbg == null)
  70. {
  71. mDrbg = mDrbgProvider.Get(mEntropySource);
  72. }
  73. // check if a reseed is required...
  74. if (mDrbg.Generate(buf, off, len, null, mPredictionResistant) < 0)
  75. {
  76. mDrbg.Reseed(null);
  77. mDrbg.Generate(buf, off, len, null, mPredictionResistant);
  78. }
  79. }
  80. #endif
  81. }
  82. #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER || UNITY_2021_2_OR_NEWER
  83. public override void NextBytes(Span<byte> buffer)
  84. {
  85. lock (this)
  86. {
  87. if (mDrbg == null)
  88. {
  89. mDrbg = mDrbgProvider.Get(mEntropySource);
  90. }
  91. // check if a reseed is required...
  92. if (mDrbg.Generate(buffer, mPredictionResistant) < 0)
  93. {
  94. mDrbg.Reseed(ReadOnlySpan<byte>.Empty);
  95. mDrbg.Generate(buffer, mPredictionResistant);
  96. }
  97. }
  98. }
  99. #endif
  100. public override byte[] GenerateSeed(int numBytes)
  101. {
  102. return EntropyUtilities.GenerateSeed(mEntropySource, numBytes);
  103. }
  104. /// <summary>Force a reseed of the DRBG.</summary>
  105. /// <param name="additionalInput">optional additional input</param>
  106. public virtual void Reseed(byte[] additionalInput)
  107. {
  108. lock (this)
  109. {
  110. if (mDrbg == null)
  111. {
  112. mDrbg = mDrbgProvider.Get(mEntropySource);
  113. }
  114. mDrbg.Reseed(additionalInput);
  115. }
  116. }
  117. }
  118. }
  119. #pragma warning restore
  120. #endif