Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / java / security / jce / sig / DSSParametersGenerator.java
blob09c13861042d597366d3895cf1df2056843d0fed
1 /* DSSParametersGenerator.java -- JCE Adapter for a generator of DSS parameters
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is 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, or (at your option)
9 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; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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.jce.sig;
41 import gnu.java.security.Registry;
42 import gnu.java.security.key.dss.DSSKeyPairGenerator;
43 import gnu.java.security.key.dss.FIPS186;
44 import gnu.java.security.provider.Gnu;
46 import java.math.BigInteger;
47 import java.security.AlgorithmParameterGeneratorSpi;
48 import java.security.AlgorithmParameters;
49 import java.security.InvalidAlgorithmParameterException;
50 import java.security.InvalidParameterException;
51 import java.security.NoSuchAlgorithmException;
52 import java.security.Provider;
53 import java.security.SecureRandom;
54 import java.security.spec.AlgorithmParameterSpec;
55 import java.security.spec.DSAParameterSpec;
56 import java.security.spec.InvalidParameterSpecException;
58 /**
59 * A JCE Adapter for a generator of DSS parameters.
61 public class DSSParametersGenerator
62 extends AlgorithmParameterGeneratorSpi
64 private static final Provider GNU = new Gnu();
66 /** Size of the public modulus in bits. */
67 private int modulusLength = -1;
69 /** User specified source of randomness. */
70 private SecureRandom rnd;
72 /** Our concrete DSS parameters generator. */
73 private FIPS186 fips;
75 // default 0-arguments constructor
77 protected void engineInit(int size, SecureRandom random)
79 if ((size % 64) != 0 || size < 512 || size > 1024)
80 throw new InvalidParameterException("Modulus size/length (in bits) MUST "
81 + "be a multiple of 64, greater than "
82 + "or equal to 512, and less than or "
83 + "equal to 1024");
84 this.modulusLength = size;
85 this.rnd = random;
88 protected void engineInit(AlgorithmParameterSpec spec, SecureRandom random)
89 throws InvalidAlgorithmParameterException
91 if (! (spec instanceof DSAParameterSpec))
92 throw new InvalidAlgorithmParameterException("Wrong AlgorithmParameterSpec type: "
93 + spec.getClass().getName());
94 DSAParameterSpec dsaSpec = (DSAParameterSpec) spec;
95 BigInteger p = dsaSpec.getP();
96 int size = p.bitLength();
97 this.engineInit(size, random);
100 protected AlgorithmParameters engineGenerateParameters()
102 if (modulusLength < 1)
103 modulusLength = DSSKeyPairGenerator.DEFAULT_MODULUS_LENGTH;
105 fips = new FIPS186(modulusLength, rnd);
106 BigInteger[] params = fips.generateParameters();
107 BigInteger p = params[3];
108 BigInteger q = params[2];
109 BigInteger g = params[5];
110 DSAParameterSpec spec = new DSAParameterSpec(p, q, g);
111 AlgorithmParameters result = null;
114 result = AlgorithmParameters.getInstance(Registry.DSS_KPG, GNU);
115 result.init(spec);
117 catch (NoSuchAlgorithmException ignore)
120 catch (InvalidParameterSpecException ignore)
123 return result;