John von Neumann's quote is widely known:
Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.
Pseudorandom number generators (PRNGs) use deterministic mathematical algorithms to produce a sequence of numbers with good statistical properties, but the sequences of numbers produced fail to achieve true randomness. PRNGs usually start with an arithmetic seed value. The algorithm uses this seed to generate an output value and a new seed, which is used to generate the next value, and so on.
The Java API provides a PRNG, the java.util.Random
class. This PRNG is portable and repeatable. Consequently, two instances of the java.util.Random
class that are created using the same seed will generate identical sequences of numbers in all Java implementations. Seed values are often reused on application initialization or after every system reboot. In other cases, the seed is derived from the current time obtained from the system clock. An attacker can learn the value of the seed by performing some reconnaissance on the vulnerable target, and can then build a lookup table for estimating future seed values.
Consequently, the java.util.Random
class must not be used either for security-critical applications or for protecting sensitive data. Use a more secure random number generator, such as the java.security.SecureRandom
class.
Noncompliant Code Example
This noncompliant code example uses the insecure java.util.Random
class. This class produces an identical sequence of numbers for each given seed value; consequently, the sequence of numbers fails to achieve true randomness.
import java.util.Random; // ... Random number = new Random(123L); //... for (int i = 0; i < 20; i++) { // Generate another random integer in the range [0, 20] int n = number.nextInt(21); System.out.println(n); }
Compliant Solution
This compliant solution uses the java.security.SecureRandom
class to produce high quality random numbers.
import java.security.SecureRandom; import java.security.NoSuchAlgorithmException; // ... public static void main (String args[]) { try { SecureRandom number = SecureRandom.getInstance("SHA1PRNG"); // Generate 20 integers 0..20 for (int i = 0; i < 20; i++) { System.out.println(number.nextInt(21)); } } catch (NoSuchAlgorithmException nsae) { // Forward to handler } }
Exceptions
MSC02-EX1: Using the default constructor for java.util.Random
applies a seed value that is "very likely to be distinct from any other invocation of this constructor" (API 2006), and may improve security marginally. As a result, it may only be used for non-critical applications operating on non-sensitive data. Java's default seed uses the system's time in milliseconds. When used, explicit documentation of this exception is required.
import java.util.Random; // ... Random number = new Random(); // only used for demo purposes int n; //... for (int i=0; i<20; i++) { // Re-seed generator number = new Random(); // Generate another random integer in the range [0, 20] n = number.nextInt(21); System.out.println(n); }
For noncritical cases, such as adding some randomness to a game or unit testing, the use of class Random
is acceptable. However, it is worth reiterating that the resulting low entropy random numbers are insufficiently random to be used for more serious applications, such as cryptography.
MSC02-EX1: There are cases where predictable sequences of pseudo-random numbers are required, such as when running regression tests of program behavior. Use of the insecure java.util.Random
class is permitted in such cases. However, security-related applications may invoke this exception only for testing purposes; it is inapplicable in a production context.
Risk Assessment
Predictable random number sequences can weaken the security of critical applications such as cryptography.
Rule |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
MSC02-J |
high |
probable |
medium |
P12 |
L1 |
Related Vulnerabilities
Other Languages
This rule appears in the C Secure Coding Standard as MSC30-C. Do not use the rand() function for generating pseudorandom numbers.
This rule appears in the C++ Secure Coding Standard as MSC30-CPP. Do not use the rand() function for generating pseudorandom numbers.
Related Guidelines
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="008d37b6-c713-4185-ae46-c7eea8cccdd1"><ac:plain-text-body><![CDATA[ |
[[MITRE 2009 |
AA. Bibliography#MITRE 09]] |
[CWE ID 330 |
http://cwe.mitre.org/data/definitions/330.html] "Use of Insufficiently Random Values" |
]]></ac:plain-text-body></ac:structured-macro> |
|
CWE ID 327 "Use of a Broken or Risky Cryptographic Algorithm" |
||||
|
CWE ID 330 "Use of Insufficiently Random Values" |
||||
|
CWE ID 333 "Improper Handling of Insufficient Entropy in TRNG" |
||||
|
CWE ID 332 "Insufficient Entropy in PRNG" |
||||
|
CWE ID 337 "Predictable Seed in PRNG" |
||||
|
CWE ID 336 "Same Seed in PRNG" |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b677728a-1f83-4e61-9e70-03637e523db9"><ac:plain-text-body><![CDATA[ |
[[API 2006 |
https://www.securecoding.cert.org/confluence/display/java/AA.+Java+References#AA.JavaReferences-API06]] |
[Class Random |
http://java.sun.com/javase/6/docs/api/java/util/Random.html] |
]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="114708b5-2f43-4e46-9b3d-f34b66a77295"><ac:plain-text-body><![CDATA[ |
[[API 2006 |
https://www.securecoding.cert.org/confluence/display/java/AA.+Java+References#AA.JavaReferences-API06]] |
[Class SecureRandom |
http://java.sun.com/javase/6/docs/api/java/security/SecureRandom.html] |
]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="9ebbb970-b176-45b6-9ff4-8c4b63be39f5"><ac:plain-text-body><![CDATA[ |
[[Find Bugs 2008 |
https://www.securecoding.cert.org/confluence/display/java/AA.+Java+References#AA.JavaReferences-FindBugs08]] |
BC: Random objects created and used only once |
]]></ac:plain-text-body></ac:structured-macro> |
|
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="c773a6de-9a46-4f3e-8f89-8c8379a41e09"><ac:plain-text-body><![CDATA[ |
[[Monsch 2006 |
AA. Bibliography#Monsch 06]] |
|
]]></ac:plain-text-body></ac:structured-macro> |
49. Miscellaneous (MSC) MSC03-J. Never hardcode sensitive information