Proper use of Java’s SecureRandom

by Cigital on Friday, August 14, 2009

This is a guest post by Amit Sethi, Senior Consultant at Cigital.

When generating random numbers in Java for cryptographic purposes, many developers often use the class. And while the class is designed to generate cryptographically secure random numbers, there are a few subtleties in the API, and if it is used improperly the output can become predictable. At Cigital we have witnessed a number of cases where this is true. The following is a guide to the proper use of Java’s class.

First, let’s take a quick look at how the API works. The class does not actually implement a pseudorandom number generator (PRNG) itself. It uses PRNG implementations in other classes to generate random numbers. A number of actual PRNGs may actually be used when an instance of is created. The PRNGs are part of Java cryptographic service providers (CSPs). In Sun’s Java implementation, the SUN CSP is used by default. On Windows, the SUN CSP uses the SHA1PRNG implemented in by default. On Solaris and Linux, the SUN CSP default is to use which simply provides the output of the /dev/urandom PRNG provided by the operating system. If however, on Solaris/Linux, the configuration file in the JRE is modified such that securerandom.source is set to something other than file:/dev/urandom, then the SHA1PRNG implemented in is used, as on Windows. Of course, an application can choose not to use the defaults, and can always specify a particular PRNG implemented by a particular cryptographic provider. In Cigital’s experience, most Java applications that use actually use the SHA1PRNG provided by the SUN CSP under the cover.

Now, an application will end up with an instance of SHA1PRNG implemented in using the following calls:

Note that according to Sun’s documentation, the returned instance is not seeded by any of these calls. If after one of these calls,[]) is called, then the PRNG is seeded using a secure mechanism provided by the underlying operating system (starting with JRE 1.4.1 in Windows and JRE 1.4.2 in Linux and Solaris). If or[]) is called before a call to[]), then the internal seeding mechanism is bypassed, and only the provided seed is used to generate random numbers.

For those who are not familiar with the inner workings of cryptographic PRNGs, their job is to take a relatively small random seed and use it to produce deterministic output that seems random to anybody that does not know what the seed is. The PRNG tries to ensure that the output does not reveal any information about the seed, and that somebody observing the output cannot predict future outputs without knowing the seed.

By bypassing the internal secure seeding mechanism of the SHA1PRNG, you may compromise the security of your PRNG output. If you seed it with anything that an attacker can potentially predict (e.g. the time when the PRNG instance was created), then using may not provide the level of security that you need.

Finally, regardless of how well the PRNG is seeded, it should not be used indefinitely without reseeding. There are two approaches that can be used for longer-term security of PRNG output:

  • Periodically throw away the existing instance and create a new one. This will generate a new instance with a new seed.
  • Periodically add new random material to the PRNG seed by making a call to

In summary, keep the following in mind when using

  • Always specify the exact PRNG and provider that you wish to use. If you just use the default PRNG, you may end up with different PRNGs on different installations of your application that may need to be called differently in order to work properly. Using the following code to get a PRNG instance is appropriate:
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "SUN");
  • When using the SHA1PRNG, always call[]) immediately after creating a new instance of the PRNG. This will force the PRNG to seed itself securely. If for testing purposes, you need predictable output, ignoring this rule and seeding the PRNG with hard-coded/predictable values may be appropriate.
  • Use at least JRE 1.4.1 on Windows and at least JRE 1.4.2 on Solaris and Linux. Earlier versions do not seed the SHA1PRNG securely.
  • Periodically reseed your PRNG as observing a large amount of PRNG output generated using one seed may allow the attacker to determine the seed and thus predict all future outputs.

9 Responses to “Proper use of Java’s SecureRandom”

  1. Sebastian says:

    Hi Amit, thanks for sharing your insights, very interesting!

  2. […] As we’re prone to say, “much ink has been spilt over the release of password digests” from LinkedIn and others. I’m, as is typical, profoundly disappointed in that amount of misinformation I’ve heard in security folks’ commentary on the problem and the underlying workings of digests, HMACs, and so forth. This blog entry represents a roll-up of a great discussion we had internally on @Cigital‘s SSG mailing list. Special thanks to CKetkar, JDelgrosso, and ASethi. […]

  3. Eric says:

    Actually, SecureRandom uses /dev/random and not /dev/urandom.

    • Amit Sethi says:

      SecureRandom is just an API. Actual implementations can do whatever they choose. The default implementation on Linux in the SUN CSP is NativePRNG, which XORs the output of the SHA1PRNG with /dev/urandom. SHA1PRNG on Linux is seeded with /dev/random. The following comment is taken directly from
      * nextBytes() reads the bytes directly from /dev/urandom (and then
      * mixes them with bytes from the SHA1PRNG

      There may be implementations of the SecureRandom API that only read from /dev/random; however, doing that would be risky because /dev/random can block if it runs out of entropy.

      • bob2222222 says:

        However, doing that would be risky because /dev/random can block if it runs out of entropy.

        It would be far more risky to not block should it run out of entropy.

    • bob2222222 says:

      Actually there is no difference between the two on FreeBSD. On Linux there is a difference, but since the random generator is broken completely in first place this isn’t the most important concern.

  4. […] “This is a guest post by Amit Sethi, Senior Consultant at Cigital. When generating random numbers in Java for cryptographic purposes, many developers often use the class.”  […]