net.vattp.security
Class ESecureRandom

java.lang.Object
  |
  +--java.util.Random
        |
        +--java.security.SecureRandom
              |
              +--net.vattp.security.ESecureRandom
All Implemented Interfaces:
Serializable

public class ESecureRandom
extends SecureRandom

Untamed: Cryptographically Strong Random Number Generator

This class provides a cryptographically strong random number generator based on user provided sources of entropy and the MD5 hash algorithm. It maintains an estimate of the amount of entropy supplied. If there is a request for secure random data, a call on nextBytes(), when there is less than 80 bits of randomness available, it will use super.getSeed() to bring the level up to 80 bits.

The calls inherited from Random and SecureRandom are implemented in terms of the strengthened functionality.

Note by msm: Beware, ESecureRandom inheits from SecureRandom, which synchronizes on itself, so ESecureRandom also syncronizes on itself.

Although this class isn't declared Persistent, the relevant instance is expected to be a scalpel-key.

Author:
Bill Frantz
See Also:
java.util.Random, java.security.SecureRandom, Serialized Form

Field Summary
private static int HASH_SIZE
           
private static int MAX_ENTROPY
          Size of the entropy pool in bits.
private static int MIN_ENTROPY
           
private  int myAvailableEntropy
           
private  int myBufferedEntropy
          The amount of entropy in the buffer we keep before adding it to the pool.
private  long myDigestNumber
           
private  byte[] myEntropyBuffer
          The buffer we keep for entropy before adding it to the pool
private  byte[] myEntropyPool
           
private  MessageDigest myMD
           
private  int myPoolCursor
           
private static int SWISSDATA_SIZE
           
private static ESecureRandom theESecureRandom
          The instance of this singleton class
private static int theLastDX
           
private static int theLastDY
           
private static int[] theLastKeys
          Last keys seen
private static int theLastX
           
private static int theLastY
           
private static TimerJitterEntropy theTimeJitterEntropy
          static entropy generator reference to start the entropy generation thread
 
Fields inherited from class java.security.SecureRandom
 
Fields inherited from class java.util.Random
 
Constructor Summary
private ESecureRandom(byte[] seed, int entropy)
          This constructor takes a user-provided seed and entropy estimate.
 
Method Summary
 int availableEntropy()
          Enabled: Get the amount of entropy the generator now holds.
static ESecureRandom getESecureRandom()
          Enabled: Return the singular instance of a SecureRandomCrew.
static ESecureRandom getESecureRandom(byte[] entropy, int bitEstimate)
          Enabled: Return the singular instance of a SecureRandomCrew.
private  byte[] long2bytes(long number)
           
 void nextBytes(byte[] bytes)
          Enabled: This method provides the secure random output.
 BigInteger nextSwiss()
          Enabled: MSM: Added this method, so this object acts as a capability for making new unguessable BigIntegers that have new entropy.
static void provideEntropy(byte[] entropy, int bitEstimate)
          Enabled: This method accepts entropy and adds it to the pool.
static void setKeySeed(int key, int modifiers, int type)
          Enabled: This method accepts a keyboard event to input entropy.
static void setMouseSeed(int x, int y, int type)
          Enabled: This method accepts a mouse event to input entropy.
 void setSeed(byte[] seed)
          Enabled: This method is included for compatibility with the super class.
 void setSeed(byte[] seed, int entropy)
          Enabled: This method is the preferred way to provide entropy for later use.
 
Methods inherited from class java.security.SecureRandom
generateSeed, getInstance, getInstance, getInstance, getProvider, getSeed, next, setSeed
 
Methods inherited from class java.util.Random
nextBoolean, nextDouble, nextFloat, nextGaussian, nextInt, nextInt, nextLong
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SWISSDATA_SIZE

private static final int SWISSDATA_SIZE

theTimeJitterEntropy

private static TimerJitterEntropy theTimeJitterEntropy
static entropy generator reference to start the entropy generation thread


theESecureRandom

private static ESecureRandom theESecureRandom
The instance of this singleton class


theLastKeys

private static final transient int[] theLastKeys
Last keys seen


theLastX

private static transient int theLastX

theLastY

private static transient int theLastY

theLastDX

private static transient int theLastDX

theLastDY

private static transient int theLastDY

HASH_SIZE

private static final int HASH_SIZE

MAX_ENTROPY

private static final int MAX_ENTROPY
Size of the entropy pool in bits.

This constant limits the amount of actual entropy that goes into building the secure random output. N.B. must be a multiple of HASH_SIZE.


MIN_ENTROPY

private static final int MIN_ENTROPY

myEntropyPool

private final transient byte[] myEntropyPool

myPoolCursor

private transient int myPoolCursor

myAvailableEntropy

private int myAvailableEntropy

myMD

private MessageDigest myMD

myDigestNumber

private long myDigestNumber

myBufferedEntropy

private int myBufferedEntropy
The amount of entropy in the buffer we keep before adding it to the pool. This pool protects against the State Compromise Extension Attack David Wagner identified as a weakness in the previous design. See http://www.cs.berkeley.edu/~daw/prngs-fse98.ps


myEntropyBuffer

private byte[] myEntropyBuffer
The buffer we keep for entropy before adding it to the pool

Constructor Detail

ESecureRandom

private ESecureRandom(byte[] seed,
                      int entropy)
This constructor takes a user-provided seed and entropy estimate. it is the preferred constructor call, if there is any initial entropy available.

Parameters:
seed - the seed.
entropy - an estimate of the amount of entropy in seed in bits.
Throws:
IllegalArgumentException - entropy estimate is less than 1 bit.
IllegalArgumentException - entropy estimate is greater than 8 bits for every byte of the seed.
Method Detail

availableEntropy

public int availableEntropy()
Enabled: Get the amount of entropy the generator now holds.

Returns:
the number of bits of entropy in the generator.

getESecureRandom

public static ESecureRandom getESecureRandom()
Enabled: Return the singular instance of a SecureRandomCrew. Creates it (using the thread yeald seeding) if necessary.


getESecureRandom

public static ESecureRandom getESecureRandom(byte[] entropy,
                                             int bitEstimate)
Enabled: Return the singular instance of a SecureRandomCrew. Creates it (using the thread yeald seeding) if necessary.


long2bytes

private byte[] long2bytes(long number)

nextBytes

public void nextBytes(byte[] bytes)
Enabled: This method provides the secure random output.

Overrides:
nextBytes in class SecureRandom
Parameters:
bytes - the byte array which will receive the secure random output.

provideEntropy

public static void provideEntropy(byte[] entropy,
                                  int bitEstimate)
Enabled: This method accepts entropy and adds it to the pool. If necessary, it creates the singleton instance of ESecureRandom.

Parameters:
entropy - a byte array containing the entropy.
bitEstimate - an estimate of the amount of entropy in entropy in bits.
Throws:
IllegalArgumentException - entropy estimate is less than 1 bit.
IllegalArgumentException - entropy estimate is greater than 8 bits for every byte of the seed.

setKeySeed

public static void setKeySeed(int key,
                              int modifiers,
                              int type)
Enabled: This method accepts a keyboard event to input entropy.

Parameters:
key - the keyboard code.
modifiers - the modifier keys pressed.
type - the mouse event type code.

setMouseSeed

public static void setMouseSeed(int x,
                                int y,
                                int type)
Enabled: This method accepts a mouse event to input entropy.

Parameters:
x - the mouse event x value.
y - the mouse event y value.
type - the mouse event type code.

setSeed

public void setSeed(byte[] seed)
Enabled: This method is included for compatibility with the super class. As a general statement, the setSeed(seed, entropy) method should be used in its place. This method assumes 1 bit of entropy for each byte of the seed. It is almost certain that this estimate will be wrong.

Overrides:
setSeed in class SecureRandom
Parameters:
seed - the seed.
See Also:
SecureRandom.getSeed(int)

setSeed

public void setSeed(byte[] seed,
                    int entropy)
Enabled: This method is the preferred way to provide entropy for later use. In practical systems, any source of entropy available can be mixed in using this method. Examples include: User interface events, disk I/O timings, and random data from quantum-mechanically based hardware.

Parameters:
seed - the seed.
entropy - an estimate of the amount of entropy in seed in bits.
Throws:
IllegalArgumentException - entropy estimate is less than 0 bits.
IllegalArgumentException - entropy estimate is greater than 8 bits for every byte of the seed.

nextSwiss

public BigInteger nextSwiss()
Enabled: MSM: Added this method, so this object acts as a capability for making new unguessable BigIntegers that have new entropy.



comments?