public class FortunaGenerator extends Object implements org.bouncycastle.crypto.prng.RandomGenerator
This class is a modified variant of the original Fortuna generator in GNU Classpath (see note below). The class now uses the Bouncycastle hash and cipher classes and provides a Bouncycastle compliant interface.
Fortuna is a continuously-seeded pseudo-random number generator (PRNG) and 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.
Here an example how to use the FortunaGenerator (pseudo code):
...
new Random().nextBytes(firstSeed) // get some random data
FortunaGenerator fg = new FortunaGenerator(firstSeed);
...
fg.nextBytes(randomData)
...
fg.addSeedMaterial(entropyData)
After some time the application has done its work and exits. To enable
a fast restart of the FortunaGenerator you may store the seed status
and initialize the FortunaGenerator with this seed the next time. For
example:
... // use FortunaGenerator
seedStatus = fg.getSeedStatus()
// save seed status somewhere
// exit application
...
// restart application
if (saved seed status available)
read seed status data
FortunaGenerator fg = new FortunaGenerator()
fg.setSeedStatus(seed status data)
// alternatively you can do
FortunaGenerator = new FortunaGenerator(seed status data)
// use FortunaGenerator
There are some things users of this class must be aware of:
References:
License: the Bouncycastle license applies to this file. Also notice the GNU Classpath license exception (see below).
Copyright (C) 2010 Werner Dittmann (Werner.Dittmann@t-online.de)
Copyright (C) 2004, 2006 Free Software Foundation, Inc.
| Modifier and Type | Field and Description |
|---|---|
protected int |
ndx
The index into buffer of where the next byte will come from.
|
| Constructor and Description |
|---|
FortunaGenerator() |
FortunaGenerator(byte[] seed) |
| Modifier and Type | Method and Description |
|---|---|
void |
addSeedMaterial(byte[] buf)
Adds new random data (entropy) to an entropy pool.
|
void |
addSeedMaterial(byte[] buf,
int offset,
int length)
Adds new random data (entropy) to an entropy pool.
|
void |
addSeedMaterial(int poolNumber,
byte[] data,
int offset,
int length)
Adds new random data (entropy) to the specified entropy pool.
|
void |
addSeedMaterial(long b)
Adds new random data (entropy) to an entropy pool.
|
byte[] |
getSeedStatus()
Return the generator's seed status.
|
void |
nextBytes(byte[] out)
Get new random data.
|
void |
nextBytes(byte[] out,
int offset,
int length)
Get new random data.
|
void |
setSeedStatus(byte[] seedStatus)
Seed the generator with a previously saved seed.
|
public FortunaGenerator()
public FortunaGenerator(byte[] seed)
public void nextBytes(byte[] out)
This functions fills a byte buffer with new random data.
nextBytes in interface org.bouncycastle.crypto.prng.RandomGeneratorout - the buffer that receives the random datapublic void nextBytes(byte[] out,
int offset,
int length)
This functions returns new random data.
nextBytes in interface org.bouncycastle.crypto.prng.RandomGeneratorout - the buffer that receives the random dataoffset - offset into the bufferlength - number of random bytespublic void addSeedMaterial(long b)
This functions adds entropy data to the current pool. Fortuna uses 32 pools to gather entropy. After the function added the entropy to the pool it increments the current pool number modulo 32.
Only if pool 0 (zero) got enough entropy (min. 64 bytes) then Fortuna uses the pools to perform a real re-seed. If an application uses this function to add entropy it shall take this behaviour into consideration.
addSeedMaterial in interface org.bouncycastle.crypto.prng.RandomGeneratorb - a long with new entropy data. If the current pool is 0 then
the function adds the length of a long to the overall
entropy count that controls re-seed.public void addSeedMaterial(byte[] buf)
This functions adds entropy data to the current pool. Fortuna uses 32 pools to gather entropy. After the function added the entropy to the pool it increments the current pool number modulo 32.
Only if pool 0 (zero) got enough entropy (min. 64 bytes) then Fortuna uses the pools to perform a real re-seed. If an application uses this function to add entropy it shall take this behaviour into consideration.
addSeedMaterial in interface org.bouncycastle.crypto.prng.RandomGeneratorbuf - buffer with new entropy data. If the current pool is 0 then
the function adds the length of the buffer to the overall
entropy count that controls re-seed.public void addSeedMaterial(byte[] buf,
int offset,
int length)
This functions adds entropy data to the current pool. Fortuna uses 32 pools to gather entropy. After the function added the entropy to the pool it increments the current pool number modulo 32.
Only if pool 0 (zero) got enough entropy (min. 64 bytes) then Fortuna uses the pools to perform a real re-seed. If an application uses this function to add entropy it shall take this behaviour into consideration.
buf - buffer with new entropy data.offset - offset into the bufferlength - number of bytes to add to the current pool's entropy.
If the current pool is 0 then the function adds the length
of the buffer to the overall entropy count that controls
re-seed.public void addSeedMaterial(int poolNumber,
byte[] data,
int offset,
int length)
This functions adds entropy data to the the specified pool. Fortuna uses32 pools to gather entropy.
Only if pool 0 (zero) got enough entropy (min. 64 bytes) then Fortuna uses the pools to perform a real re-seed. If an application uses this function to add entropy it shall take this behaviour into consideration.
poolNumber - specifies which pool receives the entropy datadata - buffer with new entropy data.offset - offset into the bufferlength - number of bytes to add to the specified pool's entropy.
If the specified pool is 0 then the function adds the length
of the data to the overall entropy count that controls
re-seed.public byte[] getSeedStatus()
An application may get the seed status, store it in a safe place and retrieve it to seed a new Fortuna PRNG instance.
public void setSeedStatus(byte[] seedStatus)
seedStatus - the generator's seed.Copyright © 2015. All rights reserved.