All Implemented Interfaces:
IRandomStandalone, Serializable, Cloneable
Direct Known Subclasses:
AsyncFortunaStandalone

public class FortunaStandaloneextends BasePRNGStandalone implements Serializable
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 Serializable in 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.
Modified by jrandom for I2P to use a standalone gnu-crypto SHA256, Cryptix's AES, to strip out some unnecessary dependencies and increase the buffer size. Renamed from Fortuna to FortunaStandalone so it doesn't conflict with the gnu-crypto implementation, which has been imported into GNU/classpath NOTE: As of 0.8.8, uses the java.security.MessageDigest instead of GNU Sha256Standalone
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static class 
    The Fortuna generator function.
  • Field Summary

    Modifier and Type
    Field
    Description
    protected final IRandomStandalone
    The underlying PRNG generator.
    protected long
    Timestamp of last reseed.
    (package private) static final int
    Minimum bytes in pool 0 before reseed.
    (package private) static final int
    Number of entropy pools.
    private int
    Current pool index for entropy distribution.
    protected int
    Bytes added to pool 0.
    protected final MessageDigest[]
    Null if using DevRandom.
    protected int
    Number of reseeds performed.
    static final String
    Property name for the seed attribute.
    private static final int
    Bytes stored in the seed file.
    private static final long
    Fixed id, the generator is never serialized across versions.

    Fields inherited from class BasePRNGStandalone

    buffer, initialised, name, ndx
    Modifier and Type
    Field
    Description
    protected byte[]
    A temporary buffer to serve random bytes.
    protected boolean
    Indicate if this instance has already been initialised or not.
    protected final String
    The canonical name prefix of the PRNG algorithm.
    protected int
    The index into buffer of where the next byte will come from.
  • Constructor Summary

    Constructors
    Constructor
    Description
    With 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 Type
    Method
    Description
    void
    addRandomByte(byte b)
    Add a random byte to the entropy pool.
    void
    addRandomBytes(byte[] buf, int offset, int length)
    Add random bytes to the entropy pool.
    Return a copy of this object.
    void
    Unused, see AsyncFortunaStandalone.
    private void
    Read object.
    void
    seed(byte[] val)
    void
    setup(Map<String,byte[]> attributes)
    Set up the PRNG instance.
    private void
    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.

    Methods inherited from class BasePRNGStandalone

    addRandomBytes, init, isInitialised, name, nextByte, nextBytes, nextBytes
    Modifier and Type
    Method
    Description
    void
    addRandomBytes(byte[] buffer)
    Supplement, or possibly replace, the random state of this PRNG with a sequence of new random bytes.
    void
    init(Map<String,byte[]> attributes)
    Initialises the pseudo-random number generator scheme with the appropriate attributes.
    boolean
    Check whether this instance has been initialised.
    Returns the canonical name of this instance.
    byte
    Returns the next 8 bits of random data generated from this instance.
    void
    nextBytes(byte[] out)
    Fill the output byte array with random bytes.
    void
    nextBytes(byte[] out, int offset, int length)
    Fills the designated byte array, starting from byte at index offset, for a maximum of length bytes with the output of this generator instance.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Fixed id, the generator is never serialized across versions.
      See Also:
    • SEED_FILE_SIZE

      private static final int SEED_FILE_SIZE
      Bytes stored in the seed file.
      See Also:
    • NUM_POOLS

      static final int NUM_POOLS
      Number of entropy pools.
      See Also:
    • MIN_POOL_SIZE

      static final int MIN_POOL_SIZE
      Minimum bytes in pool 0 before reseed.
      See Also:
    • generator

      protected final IRandomStandalone generator
      The underlying PRNG generator.
    • pools

      protected final MessageDigest[] pools
      Null if using DevRandom.
    • lastReseed

      protected long lastReseed
      Timestamp of last reseed.
    • pool

      private int pool
      Current pool index for entropy distribution.
    • pool0Count

      protected int pool0Count
      Bytes added to pool 0.
    • reseedCount

      protected int reseedCount
      Number of reseeds performed.
    • SEED

      public static final String SEED
      Property name for the seed attribute.
      See Also:
  • 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)
      Parameters:
      val - the seed data (unused)
    • setup

      public void setup(Map<String,byte[]> attributes)
      Set up the PRNG instance.
      Specified by:
      setup in class BasePRNGStandalone
      Parameters:
      attributes - the attributes for setup
    • fillBlock

      public void fillBlock()
      Unused, see AsyncFortunaStandalone.
      Specified by:
      fillBlock in class BasePRNGStandalone
    • clone

      public Object clone()
      Return a copy of this object.
      Specified by:
      clone in interface IRandomStandalone
      Overrides:
      clone in class BasePRNGStandalone
      Returns:
      a clone copy of this instance.
    • addRandomByte

      public void addRandomByte(byte b)
      Add a random byte to the entropy pool.
      Specified by:
      addRandomByte in interface IRandomStandalone
      Overrides:
      addRandomByte in class BasePRNGStandalone
      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:
      addRandomBytes in interface IRandomStandalone
      Overrides:
      addRandomBytes in class BasePRNGStandalone
      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

      private void writeObject(ObjectOutputStream out) throws IOException
      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

      private void readObject(ObjectInputStream in) throws IOException
      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