| | | 1 | | using System; |
| | | 2 | | using System.Threading; |
| | | 3 | | |
| | | 4 | | using Renci.SshNet.Security.Org.BouncyCastle.Crypto; |
| | | 5 | | using Renci.SshNet.Security.Org.BouncyCastle.Crypto.Digests; |
| | | 6 | | using Renci.SshNet.Security.Org.BouncyCastle.Crypto.Prng; |
| | | 7 | | using Renci.SshNet.Security.Org.BouncyCastle.Utilities; |
| | | 8 | | |
| | | 9 | | namespace Renci.SshNet.Security.Org.BouncyCastle.Security |
| | | 10 | | { |
| | | 11 | | internal class SecureRandom |
| | | 12 | | : Random |
| | | 13 | | { |
| | 1 | 14 | | private static long counter = Times.NanoTime(); |
| | | 15 | | |
| | | 16 | | private static long NextCounterValue() |
| | 10 | 17 | | { |
| | 10 | 18 | | return Interlocked.Increment(ref counter); |
| | 10 | 19 | | } |
| | | 20 | | |
| | 1 | 21 | | private static readonly SecureRandom master = new SecureRandom(new CryptoApiRandomGenerator()); |
| | | 22 | | private static SecureRandom Master |
| | | 23 | | { |
| | 30 | 24 | | get { return master; } |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | private static DigestRandomGenerator CreatePrng(IDigest digest, bool autoSeed) |
| | 10 | 28 | | { |
| | 10 | 29 | | DigestRandomGenerator prng = new DigestRandomGenerator(digest); |
| | 10 | 30 | | if (autoSeed) |
| | 10 | 31 | | { |
| | 10 | 32 | | prng.AddSeedMaterial(NextCounterValue()); |
| | 10 | 33 | | prng.AddSeedMaterial(GetNextBytes(Master, digest.GetDigestSize())); |
| | 10 | 34 | | } |
| | 10 | 35 | | return prng; |
| | 10 | 36 | | } |
| | | 37 | | |
| | | 38 | | public static byte[] GetNextBytes(SecureRandom secureRandom, int length) |
| | 10 | 39 | | { |
| | 10 | 40 | | byte[] result = new byte[length]; |
| | 10 | 41 | | secureRandom.NextBytes(result); |
| | 10 | 42 | | return result; |
| | 10 | 43 | | } |
| | | 44 | | |
| | | 45 | | protected readonly IRandomGenerator generator; |
| | | 46 | | |
| | | 47 | | public SecureRandom() |
| | 10 | 48 | | : this(CreatePrng(new Sha256Digest(), true)) |
| | 10 | 49 | | { |
| | 10 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary>Use the specified instance of IRandomGenerator as random source.</summary> |
| | | 53 | | /// <remarks> |
| | | 54 | | /// This constructor performs no seeding of either the <c>IRandomGenerator</c> or the |
| | | 55 | | /// constructed <c>SecureRandom</c>. It is the responsibility of the client to provide |
| | | 56 | | /// proper seed material as necessary/appropriate for the given <c>IRandomGenerator</c> |
| | | 57 | | /// implementation. |
| | | 58 | | /// </remarks> |
| | | 59 | | /// <param name="generator">The source to generate all random bytes from.</param> |
| | | 60 | | public SecureRandom(IRandomGenerator generator) |
| | | 61 | | #pragma warning disable CA5394 // Do not use insecure randomness |
| | 11 | 62 | | : base(0) |
| | | 63 | | #pragma warning restore CA5394 // Do not use insecure randomness |
| | 11 | 64 | | { |
| | 11 | 65 | | this.generator = generator; |
| | 11 | 66 | | } |
| | | 67 | | |
| | | 68 | | public virtual byte[] GenerateSeed(int length) |
| | 0 | 69 | | { |
| | 0 | 70 | | return GetNextBytes(Master, length); |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | public virtual void SetSeed(byte[] seed) |
| | 0 | 74 | | { |
| | 0 | 75 | | generator.AddSeedMaterial(seed); |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | public virtual void SetSeed(long seed) |
| | 0 | 79 | | { |
| | 0 | 80 | | generator.AddSeedMaterial(seed); |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | public override int Next() |
| | 0 | 84 | | { |
| | 0 | 85 | | return NextInt() & int.MaxValue; |
| | 0 | 86 | | } |
| | | 87 | | |
| | | 88 | | public override int Next(int maxValue) |
| | 0 | 89 | | { |
| | | 90 | | |
| | 0 | 91 | | if (maxValue < 2) |
| | 0 | 92 | | { |
| | 0 | 93 | | if (maxValue < 0) |
| | 0 | 94 | | throw new ArgumentOutOfRangeException("maxValue", "cannot be negative"); |
| | | 95 | | |
| | 0 | 96 | | return 0; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | int bits; |
| | | 100 | | |
| | | 101 | | // Test whether maxValue is a power of 2 |
| | 0 | 102 | | if ((maxValue & (maxValue - 1)) == 0) |
| | 0 | 103 | | { |
| | 0 | 104 | | bits = NextInt() & int.MaxValue; |
| | 0 | 105 | | return (int)(((long)bits * maxValue) >> 31); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | int result; |
| | | 109 | | do |
| | 0 | 110 | | { |
| | 0 | 111 | | bits = NextInt() & int.MaxValue; |
| | 0 | 112 | | result = bits % maxValue; |
| | 0 | 113 | | } |
| | 0 | 114 | | while (bits - result + (maxValue - 1) < 0); // Ignore results near overflow |
| | | 115 | | |
| | 0 | 116 | | return result; |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | public override int Next(int minValue, int maxValue) |
| | 0 | 120 | | { |
| | 0 | 121 | | if (maxValue <= minValue) |
| | 0 | 122 | | { |
| | 0 | 123 | | if (maxValue == minValue) |
| | 0 | 124 | | return minValue; |
| | | 125 | | |
| | 0 | 126 | | throw new ArgumentException("maxValue cannot be less than minValue"); |
| | | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | int diff = maxValue - minValue; |
| | 0 | 130 | | if (diff > 0) |
| | 0 | 131 | | return minValue + Next(diff); |
| | | 132 | | |
| | | 133 | | for (;;) |
| | 0 | 134 | | { |
| | 0 | 135 | | int i = NextInt(); |
| | | 136 | | |
| | 0 | 137 | | if (i >= minValue && i < maxValue) |
| | 0 | 138 | | return i; |
| | 0 | 139 | | } |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | public override void NextBytes(byte[] buf) |
| | 19 | 143 | | { |
| | 19 | 144 | | generator.NextBytes(buf); |
| | 19 | 145 | | } |
| | | 146 | | |
| | | 147 | | public virtual void NextBytes(byte[] buf, int off, int len) |
| | 0 | 148 | | { |
| | 0 | 149 | | generator.NextBytes(buf, off, len); |
| | 0 | 150 | | } |
| | | 151 | | |
| | 1 | 152 | | private static readonly double DoubleScale = System.Math.Pow(2.0, 64.0); |
| | | 153 | | |
| | | 154 | | public override double NextDouble() |
| | 0 | 155 | | { |
| | 0 | 156 | | return Convert.ToDouble((ulong) NextLong()) / DoubleScale; |
| | 0 | 157 | | } |
| | | 158 | | |
| | | 159 | | public virtual int NextInt() |
| | 0 | 160 | | { |
| | 0 | 161 | | byte[] bytes = new byte[4]; |
| | 0 | 162 | | NextBytes(bytes); |
| | | 163 | | |
| | 0 | 164 | | uint result = bytes[0]; |
| | 0 | 165 | | result <<= 8; |
| | 0 | 166 | | result |= bytes[1]; |
| | 0 | 167 | | result <<= 8; |
| | 0 | 168 | | result |= bytes[2]; |
| | 0 | 169 | | result <<= 8; |
| | 0 | 170 | | result |= bytes[3]; |
| | 0 | 171 | | return (int)result; |
| | 0 | 172 | | } |
| | | 173 | | |
| | | 174 | | public virtual long NextLong() |
| | 0 | 175 | | { |
| | 0 | 176 | | return ((long)(uint) NextInt() << 32) | (long)(uint) NextInt(); |
| | 0 | 177 | | } |
| | | 178 | | } |
| | | 179 | | } |