X931SecureRandomBuilder.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.Parameters;
  5. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  6. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Security;
  7. using Best.HTTP.SecureProtocol.Org.BouncyCastle.Utilities.Date;
  8. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Prng
  9. {
  10. public class X931SecureRandomBuilder
  11. {
  12. private readonly SecureRandom mRandom; // JDK 1.1 complains on final.
  13. private IEntropySourceProvider mEntropySourceProvider;
  14. private byte[] mDateTimeVector;
  15. /**
  16. * Basic constructor, creates a builder using an EntropySourceProvider based on the default SecureRandom with
  17. * predictionResistant set to false.
  18. * <p>
  19. * Any SecureRandom created from a builder constructed like this will make use of input passed to SecureRandom.setSeed() if
  20. * the default SecureRandom does for its generateSeed() call.
  21. * </p>
  22. */
  23. public X931SecureRandomBuilder()
  24. : this(CryptoServicesRegistrar.GetSecureRandom(), false)
  25. {
  26. }
  27. /**
  28. * Construct a builder with an EntropySourceProvider based on the passed in SecureRandom and the passed in value
  29. * for prediction resistance.
  30. * <p>
  31. * Any SecureRandom created from a builder constructed like this will make use of input passed to SecureRandom.setSeed() if
  32. * the passed in SecureRandom does for its generateSeed() call.
  33. * </p>
  34. * @param entropySource
  35. * @param predictionResistant
  36. */
  37. public X931SecureRandomBuilder(SecureRandom entropySource, bool predictionResistant)
  38. {
  39. if (entropySource == null)
  40. throw new ArgumentNullException(nameof(entropySource));
  41. this.mRandom = entropySource;
  42. this.mEntropySourceProvider = new BasicEntropySourceProvider(mRandom, predictionResistant);
  43. }
  44. /**
  45. * Create a builder which makes creates the SecureRandom objects from a specified entropy source provider.
  46. * <p>
  47. * <b>Note:</b> If this constructor is used any calls to setSeed() in the resulting SecureRandom will be ignored.
  48. * </p>
  49. * @param entropySourceProvider a provider of EntropySource objects.
  50. */
  51. public X931SecureRandomBuilder(IEntropySourceProvider entropySourceProvider)
  52. {
  53. this.mRandom = null;
  54. this.mEntropySourceProvider = entropySourceProvider;
  55. }
  56. public X931SecureRandomBuilder SetDateTimeVector(byte[] dateTimeVector)
  57. {
  58. this.mDateTimeVector = dateTimeVector;
  59. return this;
  60. }
  61. /**
  62. * Construct a X9.31 secure random generator using the passed in engine and key. If predictionResistant is true the
  63. * generator will be reseeded on each request.
  64. *
  65. * @param engine a block cipher to use as the operator.
  66. * @param key the block cipher key to initialise engine with.
  67. * @param predictionResistant true if engine to be reseeded on each use, false otherwise.
  68. * @return a SecureRandom.
  69. */
  70. public X931SecureRandom Build(IBlockCipher engine, KeyParameter key, bool predictionResistant)
  71. {
  72. if (mDateTimeVector == null)
  73. {
  74. mDateTimeVector = new byte[engine.GetBlockSize()];
  75. Pack.UInt64_To_BE((ulong)DateTimeUtilities.CurrentUnixMs(), mDateTimeVector, 0);
  76. }
  77. engine.Init(true, key);
  78. return new X931SecureRandom(mRandom, new X931Rng(engine, mDateTimeVector, mEntropySourceProvider.Get(engine.GetBlockSize() * 8)), predictionResistant);
  79. }
  80. }
  81. }
  82. #pragma warning restore
  83. #endif