diff options
Diffstat (limited to 'crypto/src')
-rw-r--r-- | crypto/src/crypto/prng/ThreadedSeedGenerator.cs | 138 | ||||
-rw-r--r-- | crypto/src/security/SecureRandom.cs | 37 |
2 files changed, 0 insertions, 175 deletions
diff --git a/crypto/src/crypto/prng/ThreadedSeedGenerator.cs b/crypto/src/crypto/prng/ThreadedSeedGenerator.cs deleted file mode 100644 index bc30458f5..000000000 --- a/crypto/src/crypto/prng/ThreadedSeedGenerator.cs +++ /dev/null @@ -1,138 +0,0 @@ -using System; -using System.Threading; - -#if NO_THREADS -using System.Threading.Tasks; -#endif - -using Org.BouncyCastle.Utilities; - -namespace Org.BouncyCastle.Crypto.Prng -{ - /** - * A thread based seed generator - one source of randomness. - * <p> - * Based on an idea from Marcus Lippert. - * </p> - */ - public class ThreadedSeedGenerator - { - private class SeedGenerator - { - private volatile int counter = 0; - private volatile bool stop = false; - - private void Run(object ignored) - { - while (!this.stop) - { - this.counter++; - } - } - - public byte[] GenerateSeed( - int numBytes, - bool fast) - { -#if PORTABLE - return DoGenerateSeed(numBytes, fast); -#else - ThreadPriority originalPriority = Thread.CurrentThread.Priority; - try - { - Thread.CurrentThread.Priority = ThreadPriority.Normal; - return DoGenerateSeed(numBytes, fast); - } - finally - { - Thread.CurrentThread.Priority = originalPriority; - } -#endif - } - - private byte[] DoGenerateSeed( - int numBytes, - bool fast) - { - this.counter = 0; - this.stop = false; - - byte[] result = new byte[numBytes]; - int last = 0; - int end = fast ? numBytes : numBytes * 8; - -#if NO_THREADS - Task.Factory.StartNew(() => Run(null), TaskCreationOptions.None); -#else - ThreadPool.QueueUserWorkItem(new WaitCallback(Run)); -#endif - -#if PORTABLE - AutoResetEvent autoResetEvent = new AutoResetEvent(false); -#endif - - try - { - for (int i = 0; i < end; i++) - { - while (this.counter == last) - { - try - { -#if PORTABLE - autoResetEvent.WaitOne(1); -#else - Thread.Sleep(1); -#endif - } - catch (Exception) - { - // ignore - } - } - - last = this.counter; - - if (fast) - { - result[i] = (byte)last; - } - else - { - int bytepos = i / 8; - result[bytepos] = (byte)((result[bytepos] << 1) | (last & 1)); - } - } - } - finally - { -#if PORTABLE - autoResetEvent.Dispose(); -#endif - } - - this.stop = true; - - return result; - } - } - - /** - * Generate seed bytes. Set fast to false for best quality. - * <p> - * If fast is set to true, the code should be round about 8 times faster when - * generating a long sequence of random bytes. 20 bytes of random values using - * the fast mode take less than half a second on a Nokia e70. If fast is set to false, - * it takes round about 2500 ms. - * </p> - * @param numBytes the number of bytes to generate - * @param fast true if fast mode should be used - */ - public byte[] GenerateSeed( - int numBytes, - bool fast) - { - return new SeedGenerator().GenerateSeed(numBytes, fast); - } - } -} diff --git a/crypto/src/security/SecureRandom.cs b/crypto/src/security/SecureRandom.cs index 4a16a6c4d..eb5d69618 100644 --- a/crypto/src/security/SecureRandom.cs +++ b/crypto/src/security/SecureRandom.cs @@ -2,7 +2,6 @@ using System; using System.Threading; using Org.BouncyCastle.Crypto; -using Org.BouncyCastle.Crypto.Digests; using Org.BouncyCastle.Crypto.Prng; using Org.BouncyCastle.Crypto.Utilities; using Org.BouncyCastle.Utilities; @@ -14,40 +13,6 @@ namespace Org.BouncyCastle.Security { private static long counter = Times.NanoTime(); -#if PORTABLE - private static object counterLock = new object(); - private static long NextCounterValue() - { - lock (counterLock) - { - return ++counter; - } - } - - private static readonly SecureRandom[] master = { null }; - private static SecureRandom Master - { - get - { - lock (master) - { - if (master[0] == null) - { - SecureRandom sr = master[0] = GetInstance("SHA256PRNG", false); - - // Even though Ticks has at most 8 or 14 bits of entropy, there's no harm in adding it. - sr.SetSeed(DateTime.Now.Ticks); - - // 32 will be enough when ThreadedSeedGenerator is fixed. Until then, ThreadedSeedGenerator returns low - // entropy, and this is not sufficient to be secure. http://www.bouncycastle.org/csharpdevmailarchive/msg00814.html - sr.SetSeed(new ThreadedSeedGenerator().GenerateSeed(32, true)); - } - - return master[0]; - } - } - } -#else private static long NextCounterValue() { return Interlocked.Increment(ref counter); @@ -58,7 +23,6 @@ namespace Org.BouncyCastle.Security { get { return master; } } -#endif private static DigestRandomGenerator CreatePrng(string digestName, bool autoSeed) { @@ -155,7 +119,6 @@ namespace Org.BouncyCastle.Security public override int Next(int maxValue) { - if (maxValue < 2) { if (maxValue < 0) |