Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

gnu.javax.crypto.prng
Class CSPRNG  view CSPRNG download CSPRNG.java

java.lang.Object
  extended bygnu.java.security.prng.BasePRNG
      extended bygnu.javax.crypto.prng.CSPRNG
All Implemented Interfaces:
java.lang.Cloneable, gnu.java.security.prng.IRandom

public class CSPRNG
extends gnu.java.security.prng.BasePRNG

An entropy pool-based pseudo-random number generator based on the PRNG in Peter Gutmann's cryptlib (http://www.cs.auckland.ac.nz/~pgut001/cryptlib/).

The basic properties of this generator are:

  1. The internal state cannot be determined by knowledge of the input.
  2. It is resistant to bias introduced by specific inputs.
  3. The output does not reveal the state of the generator.


Nested Class Summary
private  class CSPRNG.Poller
           
private static class CSPRNG.Spinner
          A simple thread that constantly updates a byte counter.
 
Field Summary
private static int ALLOC_SIZE
           
private static java.lang.String BLOCK
           
private  boolean blocking
          Whether or not to wait for the slow poll to complete.
static java.lang.String BLOCKING
          Property name for whether or not to wait for the slow poll to complete, passed as a java.lang.Boolean.
private  gnu.javax.crypto.cipher.IBlockCipher cipher
          The cipher (AES) used in the output masking function.
private static java.lang.String CIPHER
           
private static boolean DEBUG
           
static java.lang.String FILE_SOURCES
          Property name for the list of files to read for random values.
private  java.util.List files
          The list of file soures.
private static java.lang.String FILES
           
private  gnu.java.security.hash.IMessageDigest hash
          The message digest (SHA-1) used in the mixing function.
private static java.lang.String HASH_FUNCTION
           
private  int index
          The index of the next byte in the entropy pool.
private static int MIX_COUNT
           
private  int mixCount
          The number of times the pool has been mixed.
private  java.util.List other
          The list of other sources.
private static java.lang.String OTHER
           
static java.lang.String OTHER_SOURCES
          Property name for a list of other sources of entropy.
private static int OUTPUT_SIZE
           
private  CSPRNG.Poller poller
          The thread that polls for random data.
private  java.lang.Thread pollerThread
           
private  byte[] pool
          The entropy pool.
private static int POOL_SIZE
           
static java.lang.String PROGRAM_SOURCES
          Property name for the list of programs to execute, and use the output as new random bytes.
private  java.util.List progs
          The list of program sources.
private static java.lang.String PROGS
           
private  double quality
          The quality of the random pool (percentage).
private static int SPINNER_COUNT
           
private static java.lang.Thread[] SPINNER_THREADS
           
private static CSPRNG.Spinner[] SPINNERS
          The spinner group singleton.
static java.lang.String URL_SOURCES
          Property name for the list of URLs to poll for random values.
private  java.util.List urls
          The list of URL sources.
private static java.lang.String URLS
           
private static int X917_LIFETIME
           
private static int X917_POOL_SIZE
           
private  int x917count
          The number of iterations of the X9.17-like generators.
private  boolean x917init
          Whether or not the X9.17-like generator is initialized.
private  byte[] x917pool
          The pool for the X9.17-like generator.
 
Fields inherited from class gnu.java.security.prng.BasePRNG
buffer, initialised, name, ndx
 
Constructor Summary
CSPRNG()
           
 
Method Summary
(package private)  void addQuality(double quality)
           
 void addRandomByte(byte b)
          Add a single random byte to the randomness pool.
 void addRandomBytes(byte[] buf, int off, int len)
          Add an array of bytes into the randomness pool.
 java.lang.Object clone()
          Returns a clone copy of this instance.
private static void debug(java.lang.String msg)
           
private  void fastPoll()
          Add random data always immediately available into the random pool, such as the values of the eight asynchronous counters, the current time, the current memory usage, the calling thread name, and the current stack trace.
 void fillBlock()
           
protected  void finalize()
          Called on an object by the Virtual Machine at most once, at some point after the Object is determined unreachable but before it is destroyed.
private  void generateX917(byte[] buf)
           
private static java.lang.String getProperty(java.lang.String name)
           
(package private)  double getQuality()
           
static gnu.java.security.prng.IRandom getSystemInstance()
          Create and initialize a CSPRNG instance with the "system" parameters; the files, URLs, programs, and gnu.java.security.prng.EntropySource sources used by the instance are derived from properties set in the system gnu.java.security.Properties.
private  void mixRandomPool()
           
private  void mixRandomPool(byte[] buf)
          The mix operation.
private static java.util.List parseString(java.lang.String s)
           
private static java.util.List parseURL(java.lang.String s)
           
 void setup(java.util.Map attrib)
           
private  void slowPoll()
           
 
Methods inherited from class gnu.java.security.prng.BasePRNG
addRandomBytes, init, isInitialised, name, nextByte, nextBytes, nextBytes
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG

private static final boolean DEBUG
See Also:
Constant Field Values

FILE_SOURCES

public static final java.lang.String FILE_SOURCES
Property name for the list of files to read for random values. The mapped value is a list with the following values:
  1. A java.lang.Double, indicating the suggested quality of this source. This value must be between 0 and 100.
  2. An java.lang.Integer, indicating the number of bytes to skip in the file before reading bytes. This can be any nonnegative value.
  3. An java.lang.Integer, indicating the number of bytes to read.
  4. A java.lang.String, indicating the path to the file.

See Also:
gnu.crypto.util.SimpleList, Constant Field Values

URL_SOURCES

public static final java.lang.String URL_SOURCES
Property name for the list of URLs to poll for random values. The mapped value is a list formatted similarly as in FILE_SOURCES 55 , but the fourth member is a java.net.URL.

See Also:
Constant Field Values

PROGRAM_SOURCES

public static final java.lang.String PROGRAM_SOURCES
Property name for the list of programs to execute, and use the output as new random bytes. The mapped property is formatted similarly an in FILE_SOURCES 55 and URL_SOURCES 55 , except the fourth member is a java.lang.String of the program to execute.

See Also:
Constant Field Values

OTHER_SOURCES

public static final java.lang.String OTHER_SOURCES
Property name for a list of other sources of entropy. The mapped value must be a list of gnu.java.security.prng.EntropySource objects.

See Also:
Constant Field Values

BLOCKING

public static final java.lang.String BLOCKING
Property name for whether or not to wait for the slow poll to complete, passed as a java.lang.Boolean. The default value is true.

See Also:
Constant Field Values

FILES

private static final java.lang.String FILES
See Also:
Constant Field Values

URLS

private static final java.lang.String URLS
See Also:
Constant Field Values

PROGS

private static final java.lang.String PROGS
See Also:
Constant Field Values

OTHER

private static final java.lang.String OTHER
See Also:
Constant Field Values

BLOCK

private static final java.lang.String BLOCK
See Also:
Constant Field Values

POOL_SIZE

private static final int POOL_SIZE
See Also:
Constant Field Values

ALLOC_SIZE

private static final int ALLOC_SIZE
See Also:
Constant Field Values

OUTPUT_SIZE

private static final int OUTPUT_SIZE
See Also:
Constant Field Values

X917_POOL_SIZE

private static final int X917_POOL_SIZE
See Also:
Constant Field Values

HASH_FUNCTION

private static final java.lang.String HASH_FUNCTION
See Also:
Constant Field Values

CIPHER

private static final java.lang.String CIPHER
See Also:
Constant Field Values

MIX_COUNT

private static final int MIX_COUNT
See Also:
Constant Field Values

X917_LIFETIME

private static final int X917_LIFETIME
See Also:
Constant Field Values

SPINNER_COUNT

private static final int SPINNER_COUNT
See Also:
Constant Field Values

SPINNERS

private static final CSPRNG.Spinner[] SPINNERS
The spinner group singleton. We use this to add a small amount of randomness (in addition to the current time and the amount of free memory) based on the randomness (if any) present due to system load and thread scheduling.


SPINNER_THREADS

private static final java.lang.Thread[] SPINNER_THREADS

hash

private final gnu.java.security.hash.IMessageDigest hash
The message digest (SHA-1) used in the mixing function.


cipher

private final gnu.javax.crypto.cipher.IBlockCipher cipher
The cipher (AES) used in the output masking function.


mixCount

private int mixCount
The number of times the pool has been mixed.


pool

private final byte[] pool
The entropy pool.


quality

private double quality
The quality of the random pool (percentage).


index

private int index
The index of the next byte in the entropy pool.


x917pool

private byte[] x917pool
The pool for the X9.17-like generator.


x917count

private int x917count
The number of iterations of the X9.17-like generators.


x917init

private boolean x917init
Whether or not the X9.17-like generator is initialized.


files

private final java.util.List files
The list of file soures.


urls

private final java.util.List urls
The list of URL sources.


progs

private final java.util.List progs
The list of program sources.


other

private final java.util.List other
The list of other sources.


blocking

private boolean blocking
Whether or not to wait for the slow poll to complete.


poller

private CSPRNG.Poller poller
The thread that polls for random data.


pollerThread

private java.lang.Thread pollerThread
Constructor Detail

CSPRNG

public CSPRNG()
Method Detail

debug

private static void debug(java.lang.String msg)

getSystemInstance

public static gnu.java.security.prng.IRandom getSystemInstance()
                                                        throws java.lang.ClassNotFoundException,
                                                               java.net.MalformedURLException,
                                                               java.lang.NumberFormatException

Create and initialize a CSPRNG instance with the "system" parameters; the files, URLs, programs, and gnu.java.security.prng.EntropySource sources used by the instance are derived from properties set in the system gnu.java.security.Properties.

All properties are of the from name.N, where name is the name of the source, and N is an integer (staring at 1) that indicates the preference number for that source.

The following vales for name are used here:

gnu.crypto.csprng.file

These properties are file sources, passed as the FILE_SOURCES 55 parameter of the instance. The property value is a 4-tuple formatted as:

quality ; offset ; count ; path

The parameters are mapped to the parameters defined for FILE_SOURCES 55 . Leading or trailing spaces on any item are trimmed off.

gnu.crypto.csprng.url

These properties are URL sources, passed as the URL_SOURCES 55 parameter of the instance. The property is formatted the same way as file sources, but the path argument must be a valid URL.

gnu.crypto.csprng.program

These properties are program sources, passed as the PROGRAM_SOURCES 55 parameter of the instance. This property is formatted the same way as file and URL sources, but the last argument is a program and its arguments.

gnu.crypto.cspring.other

These properties are other sources, passed as the OTHER_SOURCES parameter of the instance. The property value must be the full name of a class that implements the gnu.java.security.prng.EntropySource interface and has a public no-argument constructor.

Finally, a boolean property "gnu.crypto.csprng.blocking" can be set to the desired value of BLOCKING 55 .

An example of valid properties would be:

 gnu.crypto.csprng.blocking=true

 gnu.crypto.csprng.file.1=75.0;0;256;/dev/random
 gnu.crypto.csprng.file.2=10.0;0;100;/home/user/file

 gnu.crypto.csprng.url.1=5.0;0;256;http://www.random.org/cgi-bin/randbyte?nbytes=256
 gnu.crypto.csprng.url.2=0;256;256;http://slashdot.org/

 gnu.crypto.csprng.program.1=0.5;0;10;last -n 50
 gnu.crypto.csprng.program.2=0.5;0;10;tcpdump -c 5

 gnu.crypto.csprng.other.1=foo.bar.MyEntropySource
 gnu.crypto.csprng.other.2=com.company.OtherEntropySource
 


getProperty

private static java.lang.String getProperty(java.lang.String name)

parseString

private static java.util.List parseString(java.lang.String s)
                                   throws java.lang.NumberFormatException

parseURL

private static java.util.List parseURL(java.lang.String s)
                                throws java.net.MalformedURLException,
                                       java.lang.NumberFormatException

clone

public java.lang.Object clone()
Description copied from interface: gnu.java.security.prng.IRandom

Returns a clone copy of this instance.


setup

public void setup(java.util.Map attrib)

fillBlock

public void fillBlock()
               throws gnu.java.security.prng.LimitReachedException

addRandomBytes

public void addRandomBytes(byte[] buf,
                           int off,
                           int len)
Add an array of bytes into the randomness pool. Note that this method will not increment the pool's quality counter (this can only be done via a source provided to the setup method).


addRandomByte

public void addRandomByte(byte b)
Add a single random byte to the randomness pool. Note that this method will not increment the pool's quality counter (this can only be done via a source provided to the setup method).


addQuality

void addQuality(double quality)

getQuality

double getQuality()

mixRandomPool

private void mixRandomPool(byte[] buf)
The mix operation. This method will, for every 20-byte block in the random pool, hash that block, the previous 20 bytes, and the next 44 bytes with SHA-1, writing the result back into that block.


mixRandomPool

private void mixRandomPool()

generateX917

private void generateX917(byte[] buf)

fastPoll

private void fastPoll()
Add random data always immediately available into the random pool, such as the values of the eight asynchronous counters, the current time, the current memory usage, the calling thread name, and the current stack trace.

This method does not alter the quality counter, and is provided more to maintain randomness, not to seriously improve the current random state.


slowPoll

private void slowPoll()
               throws gnu.java.security.prng.LimitReachedException

finalize

protected void finalize()
                 throws java.lang.Throwable
Description copied from class: java.lang.Object
Called on an object by the Virtual Machine at most once, at some point after the Object is determined unreachable but before it is destroyed. You would think that this means it eventually is called on every Object, but this is not necessarily the case. If execution terminates abnormally, garbage collection does not always happen. Thus you cannot rely on this method to always work. For finer control over garbage collection, use references from the java.lang.ref package.

Virtual Machines are free to not call this method if they can determine that it does nothing important; for example, if your class extends Object and overrides finalize to do simply super.finalize().

finalize() will be called by a java.lang.Thread that has no locks on any Objects, and may be called concurrently. There are no guarantees on the order in which multiple objects are finalized. This means that finalize() is usually unsuited for performing actions that must be thread-safe, and that your implementation must be use defensive programming if it is to always work.

If an Exception is thrown from finalize() during garbage collection, it will be patently ignored and the Object will still be destroyed.

It is allowed, although not typical, for user code to call finalize() directly. User invocation does not affect whether automatic invocation will occur. It is also permitted, although not recommended, for a finalize() method to "revive" an object by making it reachable from normal code again.

Unlike constructors, finalize() does not get called for an object's superclass unless the implementation specifically calls super.finalize().

The default implementation does nothing.