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

Quick Search    Search Deep

Source code: gnu/java/security/prng/IRandom.java


1   /* IRandom.java -- 
2      Copyright (C) 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
3   
4   This file is a part of GNU Classpath.
5   
6   GNU Classpath is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2 of the License, or (at
9   your option) any later version.
10  
11  GNU Classpath is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License
17  along with GNU Classpath; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19  USA
20  
21  Linking this library statically or dynamically with other modules is
22  making a combined work based on this library.  Thus, the terms and
23  conditions of the GNU General Public License cover the whole
24  combination.
25  
26  As a special exception, the copyright holders of this library give you
27  permission to link this library with independent modules to produce an
28  executable, regardless of the license terms of these independent
29  modules, and to copy and distribute the resulting executable under
30  terms of your choice, provided that you also meet, for each linked
31  independent module, the terms and conditions of the license of that
32  module.  An independent module is a module which is not derived from
33  or based on this library.  If you modify this library, you may extend
34  this exception to your version of the library, but you are not
35  obligated to do so.  If you do not wish to do so, delete this
36  exception statement from your version.  */
37  
38  
39  package gnu.java.security.prng;
40  
41  import java.util.Map;
42  
43  /**
44   * <p>The basic visible methods of any pseudo-random number generator.</p>
45   *
46   * <p>The [HAC] defines a PRNG (as implemented in this library) as follows:</p>
47   *
48   * <ul>
49   *    <li>"5.6 Definition: A pseudorandom bit generator (PRBG) is said to pass
50   *    the <em>next-bit test</em> if there is no polynomial-time algorithm which,
51   *    on input of the first <code>L</code> bits of an output sequence <code>S</code>,
52   *    can predict the <code>(L+1)</code>st bit of <code>S</code> with a
53   *    probability significantly grater than <code>1/2</code>."</li>
54   *
55   *    <li>"5.8 Definition: A PRBG that passes the <em>next-bit test</em>
56   *    (possibly under some plausible but unproved mathematical assumption such
57   *    as the intractability of factoring integers) is called a
58   *    <em>cryptographically secure pseudorandom bit generator</em> (CSPRBG)."</li>
59   * </ul>
60   *
61   * <p><b>IMPLEMENTATION NOTE</b>: Although all the concrete classes in this
62   * package implement the {@link Cloneable} interface, it is important to note
63   * here that such an operation, for those algorithms that use an underlting
64   * symmetric key block cipher, <b>DOES NOT</b> clone any session key material
65   * that may have been used in initialising the source PRNG (the instance to be
66   * cloned). Instead a clone of an already initialised PRNG, that uses and
67   * underlying symmetric key block cipher, is another instance with a clone of
68   * the same cipher that operates with the <b>same block size</b> but without any
69   * knowledge of neither key material nor key size.</p>
70   *
71   * <p>References:</p>
72   *
73   * <ol>
74   *    <li><a href="http://www.cacr.math.uwaterloo.ca/hac">[HAC]</a>: Handbook of
75   *    Applied Cryptography.<br>
76   *    CRC Press, Inc. ISBN 0-8493-8523-7, 1997<br>
77   *    Menezes, A., van Oorschot, P. and S. Vanstone.</li>
78   * </ol>
79   */
80  public interface IRandom extends Cloneable
81  {
82  
83    // Constants
84    // -------------------------------------------------------------------------
85  
86    // Methods
87    // -------------------------------------------------------------------------
88  
89    /**
90     * <p>Returns the canonical name of this instance.</p>
91     *
92     * @return the canonical name of this instance. */
93    String name();
94  
95    /**
96     * <p>Initialises the pseudo-random number generator scheme with the
97     * appropriate attributes.</p>
98     *
99     * @param attributes a set of name-value pairs that describe the desired
100    * future instance behaviour.
101    * @exception IllegalArgumentException if at least one of the defined name/
102    * value pairs contains invalid data.
103    */
104   void init(Map attributes);
105 
106   /**
107    * <p>Returns the next 8 bits of random data generated from this instance.</p>
108    *
109    * @return the next 8 bits of random data generated from this instance.
110    * @exception IllegalStateException if the instance is not yet initialised.
111    * @exception LimitReachedException if this instance has reached its
112    * theoretical limit for generating non-repetitive pseudo-random data.
113    */
114   byte nextByte() throws IllegalStateException, LimitReachedException;
115 
116   /**
117    * <p>Fills the designated byte array, starting from byte at index
118    * <code>offset</code>, for a maximum of <code>length</code> bytes with the
119    * output of this generator instance.
120    *
121    * @param out the placeholder to contain the generated random bytes.
122    * @param offset the starting index in <i>out</i> to consider. This method
123    * does nothing if this parameter is not within <code>0</code> and
124    * <code>out.length</code>.
125    * @param length the maximum number of required random bytes. This method
126    * does nothing if this parameter is less than <code>1</code>.
127    * @exception IllegalStateException if the instance is not yet initialised.
128    * @exception LimitReachedException if this instance has reached its
129    * theoretical limit for generating non-repetitive pseudo-random data.
130    */
131   void nextBytes(byte[] out, int offset, int length)
132       throws IllegalStateException, LimitReachedException;
133 
134   /**
135    * <p>Supplement, or possibly replace, the random state of this PRNG with
136    * a random byte.</p>
137    *
138    * <p>Implementations are not required to implement this method in any
139    * meaningful way; this may be a no-operation, and implementations may
140    * throw an {@link UnsupportedOperationException}.</p>
141    *
142    * @param b The byte to add.
143    */
144   void addRandomByte(byte b);
145 
146   /**
147    * <p>Supplement, or possibly replace, the random state of this PRNG with
148    * a sequence of new random bytes.</p>
149    *
150    * <p>Implementations are not required to implement this method in any
151    * meaningful way; this may be a no-operation, and implementations may
152    * throw an {@link UnsupportedOperationException}.</p>
153    *
154    * @param in The buffer of new random bytes to add.
155    */
156   void addRandomBytes(byte[] in);
157 
158   /**
159    * <p>Supplement, or possibly replace, the random state of this PRNG with
160    * a sequence of new random bytes.</p>
161    *
162    * <p>Implementations are not required to implement this method in any
163    * meaningful way; this may be a no-operation, and implementations may
164    * throw an {@link UnsupportedOperationException}.</p>
165    *
166    * @param in The buffer of new random bytes to add.
167    * @param offset The offset from whence to begin reading random bytes.
168    * @param length The number of random bytes to add.
169    * @exception IndexOutOfBoundsException If <i>offset</i>, <i>length</i>,
170    * or <i>offset</i>+<i>length</i> is out of bounds.
171    */
172   void addRandomBytes(byte[] in, int offset, int length);
173 
174   /**
175    * <p>Returns a clone copy of this instance.</p>
176    *
177    * @return a clone copy of this instance.
178    */
179   Object clone() throws CloneNotSupportedException;
180 }