Cryptographic operations often rely on unpredictable random numbers to enhance security. These random numbers are created by cryptographically secure pseudo-random number generators (CSPRNG). It is important not to use a predictable seed with these random number generators otherwise the random numbers will also become predictable.
Random number generators are often used to generate random values for cryptographic algorithms. When a random number generator is used for cryptographic purposes, the generated numbers must be as random and unpredictable as possible. When the random number generator is improperly seeded with a constant or a predictable value, its output will also be predictable.
This can have severe security implications for cryptographic operations that rely on the randomness of the generated numbers. By using a predictable seed, an attacker can potentially guess or deduce the generated numbers, compromising the security of whatever cryptographic algorithm relies on the random number generator.
It is crucial to understand that the strength of cryptographic algorithms heavily relies on the quality of the random numbers used. By improperly seeding a CSPRNG, we introduce a significant weakness that can be exploited by attackers.
One of the primary use cases for CSPRNGs is generating cryptographic keys. If an attacker can predict the seed used to initialize the random number generator, they may be able to derive the same keys. Depending on the use case, this can lead to multiple severe outcomes, such as:
Another scenario where this vulnerability can be exploited is in the generation of session tokens or nonces for secure communication protocols. If an attacker can predict the seed used to generate these tokens, they can impersonate legitimate users or intercept sensitive information.
BouncyCastle provides several random number generators implementations. Most of these will automatically create unpredictable output.
The remaining random number generators require seeding with an unpredictable value before they will produce unpredictable outputs. These should be seeded with at least 16 bytes of random data to ensure unpredictable output and that the random seed cannot be guessed using a brute-force attack.
SecureRandom instances created with GetInstance() are seeded by default. Disabling seeding results in predictable
output.
using Org.BouncyCastle.Security;
byte[] random = new byte[8];
SecureRandom sr = SecureRandom.GetInstance("SHA256PRNG", false);
sr.NextBytes(random); // Noncompliant
DigestRandomGenerator and VmpcRandomGenerator instances require seeding before use. Predictable seed values will result
in predictable outputs.
using Org.BouncyCastle.Crypto.Digest;
using Org.BouncyCastle.Crypto.Prng;
byte[] random = new byte[8];
IRandomGenerator digest = new DigestRandomGenerator(new Sha256Digest());
digest.AddSeedMaterial(Encoding.UTF8.GetBytes("predictable seed value"));
digest.NextBytes(random); // Noncompliant
IRandomGenerator vmpc = new VmpcRandomGenerator();
vmpc.AddSeedMaterial(Convert.FromBase64String("2hq9pkyqLQJkrYTrEv1eNw=="));
vmpc.NextBytes(random); // Noncompliant
When a SecureRandom is created using an unseeded DigestRandomGenerator and VmpcRandomGenerator instance, the
SecureRandom will create predictable outputs.
using Org.BouncyCastle.Crypto.Digest; using Org.BouncyCastle.Crypto.Prng; using Org.BouncyCastle.Security; byte[] random = new byte[8]; IRandomGenerator digest = new DigestRandomGenerator(new Sha256Digest()); SecureRandom sr = new SecureRandom(digest); sr.NextBytes(random); // Noncompliant
Allow SecureRandom.GetInstance() to automatically seed new SecureRandom instances.
using Org.BouncyCastle.Security;
byte[] random = new byte[8];
SecureRandom sr = SecureRandom.GetInstance("SHA256PRNG");
sr.NextBytes(random);
Use unpredictable values to seed DigestRandomGenerator and VmpcRandomGenerator instances. The
SecureRandom.GenerateSeed() method is designed for this purpose.
using Org.BouncyCastle.Crypto.Digest; using Org.BouncyCastle.Crypto.Prng; using Org.BouncyCastle.Security; byte[] random = new byte[8]; IRandomGenerator digest = new DigestRandomGenerator(new Sha256Digest()); digest.AddSeedMaterial(SecureRandom.GenerateSeed(16)); digest.NextBytes(random); IRandomGenerator vmpc = new VmpcRandomGenerator(); vmpc.AddSeedMaterial(SecureRandom.GenerateSeed(16)); vmpc.NextBytes(random);
An overload of the SecureRandom constructor will automatically seed the underlying IRandomGenerator with an unpredictable
value.
using Org.BouncyCastle.Crypto.Digest; using Org.BouncyCastle.Crypto.Prng; using Org.BouncyCastle.Security; byte[] random = new byte[8]; IRandomGenerator digest = new DigestRandomGenerator(new Sha256Digest()); SecureRandom sr = new SecureRandom(digest, 16); sr.NextBytes(random);
(visible only on this page)
When the random number generator’s output is not predictable by default:
Change this seed value to something unpredictable, or remove the seed.
When the random number generator’s output is predictable by default:
Set an unpredictable seed before generating random values.
When the random number generator’s output is not predictable by default:
When the random number generator’s output is predictable by default:
If the factory method or constructor is not already highlighted, it should become a secondary highlight.