java.lang.Object
gnu.crypto.prng.BasePRNGStandalone
gnu.crypto.prng.FortunaStandalone
- All Implemented Interfaces:
IRandomStandalone, Serializable, Cloneable
- Direct Known Subclasses:
AsyncFortunaStandalone
The Fortuna continuously-seeded pseudo-random number generator. This generator is composed of two major pieces: the entropy accumulator and the generator function. The former takes in random bits and incorporates them into the generator's state. The latter takes this base entropy and generates pseudo-random bits from it.
There are some things users of this class must be aware of:
- Adding Random Data
- This class does not do any polling of random sources, but rather provides an interface for adding random events. Applications that use this code must provide this mechanism. We use this design because an application writer who knows the system he is targeting is in a better position to judge what random data is available.
- Storing the Seed
- This class implements
Serializablein such a way that it writes a 64 byte seed to the stream, and reads it back again when being deserialized. This is the extent of seed file management, however, and those using this class are encouraged to think deeply about when, how often, and where to store the seed.
References:
- Niels Ferguson and Bruce Schneier, Practical Cryptography, pp. 155--184. Wiley Publishing, Indianapolis. (2003 Niels Ferguson and Bruce Schneier). ISBN 0-471-22357-3.
- See Also:
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classThe Fortuna generator function.Field Summary
Modifier and TypeFieldDescriptionprotected final IRandomStandaloneThe underlying PRNG generator.protected longTimestamp of last reseed.(package private) static final intMinimum bytes in pool 0 before reseed.(package private) static final intNumber of entropy pools.private intCurrent pool index for entropy distribution.protected intBytes added to pool 0.protected final MessageDigest[]Null if usingDevRandom.protected intNumber of reseeds performed.static final StringProperty name for the seed attribute.private static final intBytes stored in the seed file.private static final longFixed id, the generator is never serialized across versions.Fields inherited from class BasePRNGStandalone
buffer, initialised, name, ndxModifier and TypeFieldDescriptionprotected byte[]A temporary buffer to serve random bytes.protected booleanIndicate if this instance has already been initialised or not.protected final StringThe canonical name prefix of the PRNG algorithm.protected intThe index into buffer of where the next byte will come from.Constructor Summary
ConstructorsConstructorDescriptionWith DevRandom disabled.FortunaStandalone(boolean useDevRandom) Create a Fortuna instance backed either by the /dev/random device or by the built-in generator with its 32 SHA-256 entropy pools.Method Summary
Modifier and TypeMethodDescriptionvoidaddRandomByte(byte b) Add a random byte to the entropy pool.voidaddRandomBytes(byte[] buf, int offset, int length) Add random bytes to the entropy pool.clone()Return a copy of this object.voidUnused, see AsyncFortunaStandalone.private voidRead object.voidseed(byte[] val) Unused, seeAsyncFortunaStandalone.voidSet up the PRNG instance.private voidWrites the generator state as SEED_FILE_SIZE bytes of seed, so that reading and writing this object is equivalent to storing and retrieving the seed.Methods inherited from class BasePRNGStandalone
addRandomBytes, init, isInitialised, name, nextByte, nextBytes, nextBytesModifier and TypeMethodDescriptionvoidaddRandomBytes(byte[] buffer) Supplement, or possibly replace, the random state of this PRNG with a sequence of new random bytes.voidInitialises the pseudo-random number generator scheme with the appropriate attributes.booleanCheck whether this instance has been initialised.name()Returns the canonical name of this instance.bytenextByte()Returns the next 8 bits of random data generated from this instance.voidnextBytes(byte[] out) Fill the output byte array with random bytes.voidnextBytes(byte[] out, int offset, int length) Fills the designated byte array, starting from byte at indexoffset, for a maximum oflengthbytes with the output of this generator instance.
Field Details
serialVersionUID
private static final long serialVersionUIDFixed id, the generator is never serialized across versions.- See Also:
SEED_FILE_SIZE
NUM_POOLS
MIN_POOL_SIZE
generator
The underlying PRNG generator.pools
Null if usingDevRandom.lastReseed
protected long lastReseedTimestamp of last reseed.pool
private int poolCurrent pool index for entropy distribution.pool0Count
protected int pool0CountBytes added to pool 0.reseedCount
protected int reseedCountNumber of reseeds performed.SEED
Constructor Details
FortunaStandalone
public FortunaStandalone()With DevRandom disabled.FortunaStandalone
public FortunaStandalone(boolean useDevRandom) Create a Fortuna instance backed either by the /dev/random device or by the built-in generator with its 32 SHA-256 entropy pools.- Parameters:
useDevRandom- if true, use DevRandom instead of the Fortuna Generator
Method Details
seed
public void seed(byte[] val) Unused, seeAsyncFortunaStandalone.- Parameters:
val- the seed data (unused)
setup
Set up the PRNG instance.- Specified by:
setupin classBasePRNGStandalone- Parameters:
attributes- the attributes for setup
fillBlock
public void fillBlock()Unused, see AsyncFortunaStandalone.- Specified by:
fillBlockin classBasePRNGStandalone
clone
Return a copy of this object.- Specified by:
clonein interfaceIRandomStandalone- Overrides:
clonein classBasePRNGStandalone- Returns:
- a clone copy of this instance.
addRandomByte
public void addRandomByte(byte b) Add a random byte to the entropy pool.- Specified by:
addRandomBytein interfaceIRandomStandalone- Overrides:
addRandomBytein classBasePRNGStandalone- Parameters:
b- The byte to add.
addRandomBytes
public void addRandomBytes(byte[] buf, int offset, int length) Add random bytes to the entropy pool.- Specified by:
addRandomBytesin interfaceIRandomStandalone- Overrides:
addRandomBytesin classBasePRNGStandalone- Parameters:
buf- The buffer of new random bytes to add.offset- The offset from whence to begin reading random bytes.length- The number of random bytes to add.
writeObject
Writes the generator state as SEED_FILE_SIZE bytes of seed, so that reading and writing this object is equivalent to storing and retrieving the seed.- Parameters:
out- the stream the seed bytes are written to- Throws:
IOException- if the stream rejects the seed bytes or the write fails
readObject
Read object.- Parameters:
in- the stream holding the previously written SEED_FILE_SIZE bytes of generator state- Throws:
IOException- if the stream ends early or the read fails