X931SecureRandomBuilder.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Parameters;
  5. using BestHTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities;
  6. using BestHTTP.SecureProtocol.Org.BouncyCastle.Security;
  7. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Date;
  8. namespace BestHTTP.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(new SecureRandom(), 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. this.mRandom = entropySource;
  40. this.mEntropySourceProvider = new BasicEntropySourceProvider(mRandom, predictionResistant);
  41. }
  42. /**
  43. * Create a builder which makes creates the SecureRandom objects from a specified entropy source provider.
  44. * <p>
  45. * <b>Note:</b> If this constructor is used any calls to setSeed() in the resulting SecureRandom will be ignored.
  46. * </p>
  47. * @param entropySourceProvider a provider of EntropySource objects.
  48. */
  49. public X931SecureRandomBuilder(IEntropySourceProvider entropySourceProvider)
  50. {
  51. this.mRandom = null;
  52. this.mEntropySourceProvider = entropySourceProvider;
  53. }
  54. public X931SecureRandomBuilder SetDateTimeVector(byte[] dateTimeVector)
  55. {
  56. this.mDateTimeVector = dateTimeVector;
  57. return this;
  58. }
  59. /**
  60. * Construct a X9.31 secure random generator using the passed in engine and key. If predictionResistant is true the
  61. * generator will be reseeded on each request.
  62. *
  63. * @param engine a block cipher to use as the operator.
  64. * @param key the block cipher key to initialise engine with.
  65. * @param predictionResistant true if engine to be reseeded on each use, false otherwise.
  66. * @return a SecureRandom.
  67. */
  68. public X931SecureRandom Build(IBlockCipher engine, KeyParameter key, bool predictionResistant)
  69. {
  70. if (mDateTimeVector == null)
  71. {
  72. mDateTimeVector = new byte[engine.GetBlockSize()];
  73. Pack.UInt64_To_BE((ulong)DateTimeUtilities.CurrentUnixMs(), mDateTimeVector, 0);
  74. }
  75. engine.Init(true, key);
  76. return new X931SecureRandom(mRandom, new X931Rng(engine, mDateTimeVector, mEntropySourceProvider.Get(engine.GetBlockSize() * 8)), predictionResistant);
  77. }
  78. }
  79. }
  80. #pragma warning restore
  81. #endif