Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / java / security / key / dss / FIPS186.java
blob74be626f58e2462106513d97a92c8334dd1ac9af
1 /* FIPS186.java --
2 Copyright 2001, 2002, 2003, 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
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.
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.
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
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.
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. */
39 package gnu.java.security.key.dss;
41 import gnu.java.security.hash.Sha160;
42 import gnu.java.security.util.PRNG;
43 import gnu.java.security.util.Prime2;
45 import java.math.BigInteger;
46 import java.security.SecureRandom;
48 /**
49 * <p>An implementation of the DSA parameters generation as described in
50 * FIPS-186.</p>
52 * References:<br>
53 * <a href="http://www.itl.nist.gov/fipspubs/fip186.htm">Digital Signature
54 * Standard (DSS)</a>, Federal Information Processing Standards Publication 186.
55 * National Institute of Standards and Technology.
57 * @version $Revision: 1.2 $
59 public class FIPS186
62 // Constants and variables
63 // -------------------------------------------------------------------------
65 public static final int DSA_PARAMS_SEED = 0;
67 public static final int DSA_PARAMS_COUNTER = 1;
69 public static final int DSA_PARAMS_Q = 2;
71 public static final int DSA_PARAMS_P = 3;
73 public static final int DSA_PARAMS_E = 4;
75 public static final int DSA_PARAMS_G = 5;
77 /** The BigInteger constant 2. */
78 private static final BigInteger TWO = new BigInteger("2");
80 private static final BigInteger TWO_POW_160 = TWO.pow(160);
82 /** The SHA instance to use. */
83 private Sha160 sha = new Sha160();
85 /** The length of the modulus of DSS keys generated by this instance. */
86 private int L;
88 /** The optional {@link SecureRandom} instance to use. */
89 private SecureRandom rnd = null;
91 /** Our default source of randomness. */
92 private PRNG prng = null;
94 // Constructor(s)
95 // -------------------------------------------------------------------------
97 public FIPS186(int L, SecureRandom rnd)
99 super();
101 this.L = L;
102 this.rnd = rnd;
105 // Class methods
106 // -------------------------------------------------------------------------
108 // Instance methods
109 // -------------------------------------------------------------------------
112 * This method generates the DSS <code>p</code>, <code>q</code>, and
113 * <code>g</code> parameters only when <code>L</code> (the modulus length)
114 * is not one of the following: <code>512</code>, <code>768</code> and
115 * <code>1024</code>. For those values of <code>L</code>, this implementation
116 * uses pre-computed values of <code>p</code>, <code>q</code>, and
117 * <code>g</code> given in the document <i>CryptoSpec</i> included in the
118 * security guide documentation of the standard JDK distribution.<p>
120 * The DSS requires two primes , <code>p</code> and <code>q</code>,
121 * satisfying the following three conditions:
123 * <ul>
124 * <li><code>2<sup>159</sup> &lt; q &lt; 2<sup>160</sup></code></li>
125 * <li><code>2<sup>L-1</sup> &lt; p &lt; 2<sup>L</sup></code> for a
126 * specified <code>L</code>, where <code>L = 512 + 64j</code> for some
127 * <code>0 &lt;= j &lt;= 8</code></li>
128 * <li>q divides p - 1.</li>
129 * </ul>
131 * The algorithm used to find these primes is as described in FIPS-186,
132 * section 2.2: GENERATION OF PRIMES. This prime generation scheme starts by
133 * using the {@link Sha160} and a user supplied <i>SEED</i>
134 * to construct a prime, <code>q</code>, in the range 2<sup>159</sup> &lt; q
135 * &lt; 2<sup>160</sup>. Once this is accomplished, the same <i>SEED</i>
136 * value is used to construct an <code>X</code> in the range <code>2<sup>L-1
137 * </sup> &lt; X &lt; 2<sup>L</sup>. The prime, <code>p</code>, is then
138 * formed by rounding <code>X</code> to a number congruent to <code>1 mod
139 * 2q</code>. In this implementation we use the same <i>SEED</i> value given
140 * in FIPS-186, Appendix 5.
142 public BigInteger[] generateParameters()
144 int counter, offset;
145 BigInteger SEED, alpha, U, q, OFFSET, SEED_PLUS_OFFSET, W, X, p, c, g;
146 byte[] a, u;
147 byte[] kb = new byte[20]; // to hold 160 bits of randomness
149 // Let L-1 = n*160 + b, where b and n are integers and 0 <= b < 160.
150 int b = (L - 1) % 160;
151 int n = (L - 1 - b) / 160;
152 BigInteger[] V = new BigInteger[n + 1];
153 algorithm: while (true)
155 step1: while (true)
157 // 1. Choose an arbitrary sequence of at least 160 bits and
158 // call it SEED.
159 nextRandomBytes(kb);
160 SEED = new BigInteger(1, kb).setBit(159).setBit(0);
161 // Let g be the length of SEED in bits. here always 160
162 // 2. Compute: U = SHA[SEED] XOR SHA[(SEED+1) mod 2**g]
163 alpha = SEED.add(BigInteger.ONE).mod(TWO_POW_160);
164 synchronized (sha)
166 a = SEED.toByteArray();
167 sha.update(a, 0, a.length);
168 a = sha.digest();
169 u = alpha.toByteArray();
170 sha.update(u, 0, u.length);
171 u = sha.digest();
173 for (int i = 0; i < a.length; i++)
175 a[i] ^= u[i];
177 U = new BigInteger(1, a);
178 // 3. Form q from U by setting the most significant bit (the
179 // 2**159 bit) and the least significant bit to 1. In terms of
180 // boolean operations, q = U OR 2**159 OR 1. Note that
181 // 2**159 < q < 2**160.
182 q = U.setBit(159).setBit(0);
183 // 4. Use a robust primality testing algorithm to test whether
184 // q is prime(1). A robust primality test is one where the
185 // probability of a non-prime number passing the test is at
186 // most 1/2**80.
187 // 5. If q is not prime, go to step 1.
188 if (Prime2.isProbablePrime(q))
190 break step1;
192 } // step1
194 // 6. Let counter = 0 and offset = 2.
195 counter = 0;
196 offset = 2;
197 step7: while (true)
199 OFFSET = BigInteger.valueOf(offset & 0xFFFFFFFFL);
200 SEED_PLUS_OFFSET = SEED.add(OFFSET);
201 // 7. For k = 0,...,n let V[k] = SHA[(SEED + offset + k) mod 2**g].
202 synchronized (sha)
204 for (int k = 0; k <= n; k++)
206 a = SEED_PLUS_OFFSET.add(
207 BigInteger.valueOf(k & 0xFFFFFFFFL)).mod(
208 TWO_POW_160).toByteArray();
209 sha.update(a, 0, a.length);
210 V[k] = new BigInteger(1, sha.digest());
213 // 8. Let W be the integer:
214 // V[0]+V[1]*2**160+...+V[n-1]*2**((n-1)*160)+(V[n]mod2**b)*2**(n*160)
215 // and let : X = W + 2**(L-1).
216 // Note that 0 <= W < 2**(L-1) and hence 2**(L-1) <= X < 2**L.
217 W = V[0];
218 for (int k = 1; k < n; k++)
220 W = W.add(V[k].multiply(TWO.pow(k * 160)));
222 W = W.add(V[n].mod(TWO.pow(b)).multiply(TWO.pow(n * 160)));
223 X = W.add(TWO.pow(L - 1));
224 // 9. Let c = X mod 2q and set p = X - (c - 1).
225 // Note that p is congruent to 1 mod 2q.
226 c = X.mod(TWO.multiply(q));
227 p = X.subtract(c.subtract(BigInteger.ONE));
228 // 10. If p < 2**(L-1), then go to step 13.
229 if (p.compareTo(TWO.pow(L - 1)) >= 0)
231 // 11. Perform a robust primality test on p.
232 // 12. If p passes the test performed in step 11, go to step 15.
233 if (Prime2.isProbablePrime(p))
235 break algorithm;
238 // 13. Let counter = counter + 1 and offset = offset + n + 1.
239 counter++;
240 offset += n + 1;
241 // 14. If counter >= 4096 go to step 1, otherwise go to step 7.
242 if (counter >= 4096)
244 continue algorithm;
246 } // step7
247 } // algorithm
249 // compute g. from FIPS-186, Appendix 4:
250 // 1. Generate p and q as specified in Appendix 2.
251 // 2. Let e = (p - 1) / q
252 BigInteger e = p.subtract(BigInteger.ONE).divide(q);
253 BigInteger h = TWO;
254 BigInteger p_minus_1 = p.subtract(BigInteger.ONE);
255 g = TWO;
256 // 3. Set h = any integer, where 1 < h < p - 1 and
257 // h differs from any value previously tried
258 for (; h.compareTo(p_minus_1) < 0; h = h.add(BigInteger.ONE))
260 // 4. Set g = h**e mod p
261 g = h.modPow(e, p);
262 // 5. If g = 1, go to step 3
263 if (!g.equals(BigInteger.ONE))
265 break;
269 return new BigInteger[] { SEED, BigInteger.valueOf(counter), q, p, e, g };
272 // helper methods ----------------------------------------------------------
275 * Fills the designated byte array with random data.
277 * @param buffer the byte array to fill with random data.
279 private void nextRandomBytes(byte[] buffer)
281 if (rnd != null)
283 rnd.nextBytes(buffer);
285 else
286 getDefaultPRNG().nextBytes(buffer);
289 private PRNG getDefaultPRNG()
291 if (prng == null)
292 prng = PRNG.getInstance();
294 return prng;