All Implemented Interfaces:
Serializable, java.util.random.RandomGenerator, EntropyHarvester

public class FortunaRandomSourceextends RandomSource implements EntropyHarvester
Wrapper around GNU-Crypto's Fortuna PRNG. This seeds from /dev/urandom and ./prngseed.rnd on startup (if they exist), writing a new seed to ./prngseed.rnd on an explicit call to saveSeed().
See Also:
  • Field Details

    • _fortuna

      private final AsyncFortunaStandalone _fortuna
      The Fortuna PRNG instance.
    • _nextGaussian

      private double _nextGaussian
      Next Gaussian value, cached for the following call.
    • _haveNextGaussian

      private boolean _haveNextGaussian
      Whether a cached Gaussian value is available.
  • Constructor Details

    • FortunaRandomSource

      public FortunaRandomSource(I2PAppContext context)
      May block up to 10 seconds or forever.
      Parameters:
      context - the router context
  • Method Details

    • mixedSeed

      private long mixedSeed()
      Mix as much varying system state into a seed value as can be found. Only used when every real entropy source has failed - it is not cryptographically strong on its own, but it beats a ~32 bit time seed.
      Returns:
      a seed mixed from as much varying system state as could be found
    • shutdown

      public void shutdown()
      Note - methods may hang or NPE or throw IllegalStateExceptions after this.
    • setSeed

      public void setSeed(byte[] buf)
      Seed the Fortuna PRNG.
      Overrides:
      setSeed in class SecureRandom
    • nextInt

      public int nextInt(int n)
      According to the java docs (Random.nextInt(int)) nextInt(n) should return a number between 0 and n (including 0 and excluding n). However, their pseudocode, as well as sun's, kaffe's, and classpath's implementation INCLUDES NEGATIVE VALUES. Ok, so we're going to have it return between 0 and n (including 0, excluding n), since that's what it has been used for.

      Unlike Java's Random, which throws IAE, this returns 0 if n is 0.

      Specified by:
      nextInt in interface java.util.random.RandomGenerator
      Overrides:
      nextInt in class RandomSource
      Parameters:
      n - non-negative
      Throws:
      IllegalArgumentException - if n is negative
    • nextInt

      public int nextInt()
      Non-negative random int.
      Specified by:
      nextInt in interface java.util.random.RandomGenerator
      Overrides:
      nextInt in class Random
    • signedNextInt

      public int signedNextInt()
      Next random int, signed.
      Overrides:
      signedNextInt in class RandomSource
      Returns:
      all possible int values, positive and negative
    • nextLong

      public long nextLong(long n)
      Like the modified nextInt, nextLong(n) returns a random number from 0 through n, including 0, excluding n.

      Unlike Java's Random, which throws IAE, this returns 0 if n is 0.

      Specified by:
      nextLong in interface java.util.random.RandomGenerator
      Overrides:
      nextLong in class RandomSource
      Parameters:
      n - non-negative
      Returns:
      a random long from 0 (inclusive) to n (exclusive)
      Throws:
      IllegalArgumentException - if n is negative
    • nextLong

      public long nextLong()
      All possible long values, positive and negative.
      Specified by:
      nextLong in interface java.util.random.RandomGenerator
      Overrides:
      nextLong in class SecureRandom
    • nextBoolean

      public boolean nextBoolean()
      Random boolean.
      Specified by:
      nextBoolean in interface java.util.random.RandomGenerator
      Overrides:
      nextBoolean in class Random
    • nextBytes

      public void nextBytes(byte[] buf)
      Fill buffer with random bytes.
      Specified by:
      nextBytes in interface java.util.random.RandomGenerator
      Overrides:
      nextBytes in class SecureRandom
    • nextBytes

      public void nextBytes(byte[] buf, int offset, int length)
      Not part of java.util.SecureRandom, but added for efficiency, since Fortuna supports it.
      Overrides:
      nextBytes in class RandomSource
      Parameters:
      buf - the array to fill with random bytes
      offset - where in the array to start writing
      length - how many bytes to write at offset
    • nextByte

      public byte nextByte()
      Not part of java.util.SecureRandom, but added for efficiency, since Fortuna supports it.
      Returns:
      a single random byte
    • nextDouble

      public double nextDouble()
      Implementation from sun's java.util.Random javadocs.
      Specified by:
      nextDouble in interface java.util.random.RandomGenerator
      Overrides:
      nextDouble in class Random
    • nextFloat

      public float nextFloat()
      Implementation from sun's java.util.Random javadocs.
      Specified by:
      nextFloat in interface java.util.random.RandomGenerator
      Overrides:
      nextFloat in class Random
    • nextGaussian

      public double nextGaussian()
      Implementation from sun's java.util.Random javadocs.
      Specified by:
      nextGaussian in interface java.util.random.RandomGenerator
      Overrides:
      nextGaussian in class Random
    • nextBits

      protected int nextBits(int numBits)
      Pull the next numBits of random data off the fortuna instance (returning 0 through 2^numBits-1

      Caller must synchronize!

      Parameters:
      numBits - how many random bits to return, at least 1
      Returns:
      a value in the range 0 through 2^numBits - 1
    • feedEntropy

      public void feedEntropy(String source, long data, int bitoffset, int bits)
      Reseed the fortuna.
      Specified by:
      feedEntropy in interface EntropyHarvester
      Overrides:
      feedEntropy in class RandomSource
      Parameters:
      source - origin of the entropy, allowing the harvester to determine how much to value the data
      data - value whose bits are to be harvested
      bitoffset - bit index into the data array to start (using java standard big-endian)
      bits - how many bits to use
    • feedEntropy

      public void feedEntropy(String source, byte[] data, int offset, int len)
      Reseed the fortuna.
      Specified by:
      feedEntropy in interface EntropyHarvester
      Overrides:
      feedEntropy in class RandomSource
      Parameters:
      source - origin of the entropy, allowing the harvester to determine how much to value the data
      data - array holding the entropy bytes to harvest
      offset - index into the data array to start
      len - how many bytes to use