CWE-335

Incorrect Usage of Seeds in Pseudo-Random Number Generator (PRNG). [Cryptographic-Issues, Random-Number-Issues, Protection-Mechanism-Failure]

Required inputs: IR

The product uses a Pseudo-Random Number Generator (PRNG) but does not correctly manage seeds.

PRNGs are deterministic and, while their output appears random, they cannot actually create entropy. They rely on cryptographically secure and unique seeds for entropy so proper seeding is critical to the secure operation of the PRNG.

Management of seeds could be broken down into two main areas:

  • (1) protecting seeds as cryptographic material (such as a cryptographic key);
  • (2) whenever possible, using a uniquely generated seed from a cryptographically secure source

PRNGs require a seed as input to generate a stream of numbers that are functionally indistinguishable from random numbers. While the output is, in many cases, sufficient for cryptographic uses, the output of any PRNG is directly determined by the seed provided as input. If the seed can be ascertained by a third party, the entire output of the PRNG can be made known to them. As such, the seed should be kept secret and should ideally not be able to be guessed. For example, the current time may be a poor seed. Knowing the approximate time the PRNG was seeded greatly reduces the possible key space.

Seeds do not necessarily need to be unique, but reusing seeds may open up attacks if the seed is discovered.

Demonstrative Examples
Example 1

The following code uses a statistical PRNG to generate account IDs.

Example Language:Java (Unsupported language for documentation only)
    private static final long SEED = 1234567890;
    public int generateAccountID() {
        Random random = new Random(SEED);
        return random.nextInt();
    }

Because the program uses the same seed value for every invocation of the PRNG, its values are predictable, making the system vulnerable to attack.

Example 2

Both of these examples use a statistical PRNG seeded with the current value of the system clock to generate a random number:

Example Language:Java (Unsupported language for documentation only)
    Random random = new Random(System.currentTimeMillis());
    int accountID = random.nextInt();
Example Language:C
    srand(time());
    int randNum = rand();

An attacker can easily predict the seed used by these PRNGs, and so also predict the stream of random numbers generated. Note these examples also exhibit CWE-338 (Use of Cryptographically Weak PRNG).

Example 3

This code grabs some random bytes and uses them for a seed in a PRNG, in order to generate a new cryptographic key.

Example Language:Python (Unsupported language for documentation only)
    # getting 2 bytes of randomness for the seeding the PRNG
    seed = os.urandom(2)
    random.seed(a=seed)
    key = random.getrandbits(128)

Since only 2 bytes are used as a seed, an attacker will only need to guess 2^16 (65,536) values before being able to replicate the state of the PRNG.

Excerpts from CWE [https://cwe.mitre.org], Copyright (C) 2006-2026, the MITRE Corporation. See section 9.4. "3rd-Party Licenses" in the documentation for full details.

Possible Messages

Key

Text

Severity

Disabled

predictable_seed

A Pseudo-Random Number Generator (PRNG) is initialized from a predictable seed, such as the process ID or system time.

None

False

same_seed

A Pseudo-Random Number Generator (PRNG) uses the same seed each time the product is initialized.

None

False

Options

prngs

prngs : set[bauhaus.analysis.config.QualifiedName] = {'srand', 'std::srand'}

Qualified names of calls to random number generators.
 

routines_returning_constant_value

routines_returning_constant_value : set[bauhaus.analysis.config.QualifiedName] = {'geteuid', 'getpwuid', 'getuid'}

Routines that should be considered as returning a constant seed (even though they might not in fact return the same value for each call).
 

routines_returning_predictable_values

routines_returning_predictable_values : set[bauhaus.analysis.config.QualifiedName] = {'getpid', 'time'}

Routines that should be considered as returning a predictable seed.
 

whitelist

whitelist : set[bauhaus.analysis.config.QualifiedName] | None = None

List of reliable (non-predictable) sources for seeds. If set, only calls on the list are allowed to provide seeds.