APIdia Launch Partner
Top Description Inners Fields Constructors Methods
sun.security.provider

public final Class NativePRNG

extends SecureRandomSpi
Class Inheritance
Imports
java.io.*, java.net.*, java.security.*, java.util.Arrays, sun.security.util.Debug

Native PRNG implementation for Linux/MacOS.

It obtains seed and random numbers by reading system files such as the special device files /dev/random and /dev/urandom. This implementation respects the securerandom.source Security property and java.security.egd System property for obtaining seed material. If the file specified by the properties does not exist, /dev/random is the default seed source. /dev/urandom is the default source of random numbers.

On some Unix platforms, /dev/random may block until enough entropy is available, but that may negatively impact the perceived startup time. By selecting these sources, this implementation tries to strike a balance between performance and security.

generateSeed() and setSeed() attempt to directly read/write to the seed source. However, this file may only be writable by root in many configurations. Because we cannot just ignore bytes specified via setSeed(), we keep a SHA1PRNG around in parallel.

nextBytes() reads the bytes directly from the source of random numbers (and then mixes them with bytes from the SHA1PRNG for the reasons explained above). Reading bytes from the random generator means that we are generally getting entropy from the operating system. This is a notable advantage over the SHA1PRNG model, which acquires entropy only initially during startup although the VM may be running for months.

Also note for nextBytes() that we do not need any initial pure random seed from /dev/random. This is an advantage because on some versions of Linux entropy can be exhausted very quickly and could thus impact startup time.

Finally, note that we use a singleton for the actual work (RandomIO) to avoid having to open and close /dev/[u]random constantly. However, there may be many NativePRNG instances created by the JCA framework.

Author
Andreas Sterbenz
Since
1.5

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
NativePRNG.Blocking

A NativePRNG-like class that uses /dev/random for both seed and random material.

public static class
NativePRNG.NonBlocking

A NativePRNG-like class that uses /dev/urandom for both seed and random material.

private static class
NativePRNG.RandomIO

Nested class doing the actual work.

private static enum

Field Summary

Modifier and TypeField and Description
private static final Debug
private static final NativePRNG.RandomIO
private static final String
private static final String
private static final long

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected byte[]
engineGenerateSeed(int numBytes)

Implements abstract java.security.SecureRandomSpi.engineGenerateSeed.

Returns the given number of seed bytes.

protected void
engineNextBytes(byte[] bytes)

Implements abstract java.security.SecureRandomSpi.engineNextBytes.

Generates a user-specified number of random bytes.

protected void
engineSetSeed(byte[] seed)

Implements abstract java.security.SecureRandomSpi.engineSetSeed.

Reseeds this random object with the given seed.

private static URL

Returns:

the URL or null if not available.
getEgdUrl
()

Get the System egd source (if defined).

private static NativePRNG.RandomIO
initIO(final NativePRNG.Variant v)

Create a RandomIO object for all I/O of this Variant type.

pack-priv static boolean
Inherited from java.security.SecureRandomSpi:
engineGetParametersengineNextBytesengineReseedtoString

Field Detail

debugback to summary
private static final Debug debug
INSTANCEback to summary
private static final NativePRNG.RandomIO INSTANCE
NAME_RANDOMback to summary
private static final String NAME_RANDOM
NAME_URANDOMback to summary
private static final String NAME_URANDOM
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.security.SecureRandomSpi.serialVersionUID.

Constructor Detail

NativePRNGback to summary
public NativePRNG(SecureRandomParameters params)

Method Detail

engineGenerateSeedback to summary
protected byte[] engineGenerateSeed(int numBytes)

Implements abstract java.security.SecureRandomSpi.engineGenerateSeed.

Doc from java.security.SecureRandomSpi.engineGenerateSeed.

Returns the given number of seed bytes. This call may be used to seed other random number generators.

Annotations
@Override
engineNextBytesback to summary
protected void engineNextBytes(byte[] bytes)

Implements abstract java.security.SecureRandomSpi.engineNextBytes.

Doc from java.security.SecureRandomSpi.engineNextBytes.

Generates a user-specified number of random bytes.

Some random number generators can only generate a limited amount of random bytes per invocation. If the size of bytes is greater than this limit, the implementation should invoke its generation process multiple times to completely fill the buffer before returning from this method.

Annotations
@Override
engineSetSeedback to summary
protected void engineSetSeed(byte[] seed)

Implements abstract java.security.SecureRandomSpi.engineSetSeed.

Doc from java.security.SecureRandomSpi.engineSetSeed.

Reseeds this random object with the given seed. The seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness.

Annotations
@Override
getEgdUrlback to summary
private static URL getEgdUrl()

Get the System egd source (if defined). We only allow "file:" URLs for now. If there is a egd value, parse it.

Returns:URL

the URL or null if not available.

initIOback to summary
private static NativePRNG.RandomIO initIO(final NativePRNG.Variant v)

Create a RandomIO object for all I/O of this Variant type.

isAvailableback to summary
pack-priv static boolean isAvailable()
sun.security.provider back to summary

public final Class NativePRNG.Blocking

extends SecureRandomSpi
Class Inheritance

A NativePRNG-like class that uses /dev/random for both seed and random material. Note that it does not respect the egd properties, since we have no way of knowing what those qualities are. This is very similar to the outer NativePRNG class, minimizing any breakage to the serialization of the existing implementation.
Since
1.8

Field Summary

Modifier and TypeField and Description
private static final NativePRNG.RandomIO
private static final long

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected byte[]
engineGenerateSeed(int numBytes)

Implements abstract java.security.SecureRandomSpi.engineGenerateSeed.

Returns the given number of seed bytes.

protected void
engineNextBytes(byte[] bytes)

Implements abstract java.security.SecureRandomSpi.engineNextBytes.

Generates a user-specified number of random bytes.

protected void
engineSetSeed(byte[] seed)

Implements abstract java.security.SecureRandomSpi.engineSetSeed.

Reseeds this random object with the given seed.

pack-priv static boolean
Inherited from java.security.SecureRandomSpi:
engineGetParametersengineNextBytesengineReseedtoString

Field Detail

INSTANCEback to summary
private static final NativePRNG.RandomIO INSTANCE
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.security.SecureRandomSpi.serialVersionUID.

Constructor Detail

Blockingback to summary
public Blocking(SecureRandomParameters params)

Method Detail

engineGenerateSeedback to summary
protected byte[] engineGenerateSeed(int numBytes)

Implements abstract java.security.SecureRandomSpi.engineGenerateSeed.

Doc from java.security.SecureRandomSpi.engineGenerateSeed.

Returns the given number of seed bytes. This call may be used to seed other random number generators.

Annotations
@Override
engineNextBytesback to summary
protected void engineNextBytes(byte[] bytes)

Implements abstract java.security.SecureRandomSpi.engineNextBytes.

Doc from java.security.SecureRandomSpi.engineNextBytes.

Generates a user-specified number of random bytes.

Some random number generators can only generate a limited amount of random bytes per invocation. If the size of bytes is greater than this limit, the implementation should invoke its generation process multiple times to completely fill the buffer before returning from this method.

Annotations
@Override
engineSetSeedback to summary
protected void engineSetSeed(byte[] seed)

Implements abstract java.security.SecureRandomSpi.engineSetSeed.

Doc from java.security.SecureRandomSpi.engineSetSeed.

Reseeds this random object with the given seed. The seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness.

Annotations
@Override
isAvailableback to summary
pack-priv static boolean isAvailable()
sun.security.provider back to summary

public final Class NativePRNG.NonBlocking

extends SecureRandomSpi
Class Inheritance

A NativePRNG-like class that uses /dev/urandom for both seed and random material. Note that it does not respect the egd properties, since we have no way of knowing what those qualities are. This is very similar to the outer NativePRNG class, minimizing any breakage to the serialization of the existing implementation.
Since
1.8

Field Summary

Modifier and TypeField and Description
private static final NativePRNG.RandomIO
private static final long

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected byte[]
engineGenerateSeed(int numBytes)

Implements abstract java.security.SecureRandomSpi.engineGenerateSeed.

Returns the given number of seed bytes.

protected void
engineNextBytes(byte[] bytes)

Implements abstract java.security.SecureRandomSpi.engineNextBytes.

Generates a user-specified number of random bytes.

protected void
engineSetSeed(byte[] seed)

Implements abstract java.security.SecureRandomSpi.engineSetSeed.

Reseeds this random object with the given seed.

pack-priv static boolean
Inherited from java.security.SecureRandomSpi:
engineGetParametersengineNextBytesengineReseedtoString

Field Detail

INSTANCEback to summary
private static final NativePRNG.RandomIO INSTANCE
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.security.SecureRandomSpi.serialVersionUID.

Constructor Detail

NonBlockingback to summary
public NonBlocking(SecureRandomParameters params)

Method Detail

engineGenerateSeedback to summary
protected byte[] engineGenerateSeed(int numBytes)

Implements abstract java.security.SecureRandomSpi.engineGenerateSeed.

Doc from java.security.SecureRandomSpi.engineGenerateSeed.

Returns the given number of seed bytes. This call may be used to seed other random number generators.

Annotations
@Override
engineNextBytesback to summary
protected void engineNextBytes(byte[] bytes)

Implements abstract java.security.SecureRandomSpi.engineNextBytes.

Doc from java.security.SecureRandomSpi.engineNextBytes.

Generates a user-specified number of random bytes.

Some random number generators can only generate a limited amount of random bytes per invocation. If the size of bytes is greater than this limit, the implementation should invoke its generation process multiple times to completely fill the buffer before returning from this method.

Annotations
@Override
engineSetSeedback to summary
protected void engineSetSeed(byte[] seed)

Implements abstract java.security.SecureRandomSpi.engineSetSeed.

Doc from java.security.SecureRandomSpi.engineSetSeed.

Reseeds this random object with the given seed. The seed supplements, rather than replaces, the existing seed. Thus, repeated calls are guaranteed never to reduce randomness.

Annotations
@Override
isAvailableback to summary
pack-priv static boolean isAvailable()
sun.security.provider back to summary

private Class NativePRNG.RandomIO

extends Object
Class Inheritance

Nested class doing the actual work. Singleton, see INSTANCE above.

Field Summary

Modifier and TypeField and Description
private int
private int
private int
private long
private final Object
private final Object
private final Object
private static final int
private static final long
private static final int
private volatile SecureRandom
private byte[]
private final InputStream
private static final int
private static final int
pack-priv File
private final InputStream
private OutputStream
private boolean

Constructor Summary

AccessConstructor and Description
private
RandomIO(File seedFile, File nextFile)

Method Summary

Modifier and TypeMethod and Description
private void
private SecureRandom
private byte[]
implGenerateSeed(int numBytes)

private void
implNextBytes(byte[] data)

private void
implSetSeed(byte[] seed)

private static void
readFully(InputStream in, byte[] data)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bufferedback to summary
private int buffered
bufferSizeback to summary
private int bufferSize
change_bufferback to summary
private int change_buffer
lastReadback to summary
private long lastRead
LOCK_GET_BYTESback to summary
private final Object LOCK_GET_BYTES
LOCK_GET_SEEDback to summary
private final Object LOCK_GET_SEED
LOCK_SET_SEEDback to summary
private final Object LOCK_SET_SEED
MAX_BUFFER_SIZEback to summary
private static final int MAX_BUFFER_SIZE
MAX_BUFFER_TIMEback to summary
private static final long MAX_BUFFER_TIME
MIN_BUFFER_SIZEback to summary
private static final int MIN_BUFFER_SIZE
mixRandomback to summary
private volatile SecureRandom mixRandom
nextBufferback to summary
private byte[] nextBuffer
nextInback to summary
private final InputStream nextIn
REQ_LIMIT_DECback to summary
private static final int REQ_LIMIT_DEC
REQ_LIMIT_INCback to summary
private static final int REQ_LIMIT_INC
seedFileback to summary
pack-priv File seedFile
seedInback to summary
private final InputStream seedIn
seedOutback to summary
private OutputStream seedOut
seedOutInitializedback to summary
private boolean seedOutInitialized

Constructor Detail

RandomIOback to summary
private RandomIO(File seedFile, File nextFile) throws IOException

Method Detail

ensureBufferValidback to summary
private void ensureBufferValid() throws IOException
getMixRandomback to summary
private SecureRandom getMixRandom()
implGenerateSeedback to summary
private byte[] implGenerateSeed(int numBytes)
implNextBytesback to summary
private void implNextBytes(byte[] data)
implSetSeedback to summary
private void implSetSeed(byte[] seed)
readFullyback to summary
private static void readFully(InputStream in, byte[] data) throws IOException
sun.security.provider back to summary

private final Enum NativePRNG.Variant

extends Enum<NativePRNG.Variant>
Class Inheritance

Field Summary

Modifier and TypeField and Description
public static final NativePRNG.Variant
public static final NativePRNG.Variant
public static final NativePRNG.Variant

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static NativePRNG.Variant
public static NativePRNG.Variant[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

BLOCKINGback to summary
public static final NativePRNG.Variant BLOCKING
MIXEDback to summary
public static final NativePRNG.Variant MIXED
NONBLOCKINGback to summary
public static final NativePRNG.Variant NONBLOCKING

Constructor Detail

Variantback to summary
private Variant()

Method Detail

valueOfback to summary
public static NativePRNG.Variant valueOf(String name)
valuesback to summary
public static NativePRNG.Variant[] values()