1 /*
2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package java.security.spec;
26 
27 /**
28  * This immutable class specifies the set of parameters used for
29  * generating DSA parameters as specified in
30  * <a href="http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf">FIPS 186-3 Digital Signature Standard (DSS)</a>.
31  *
32  * @see AlgorithmParameterSpec
33  *
34  * @since 1.8
35  */
36 public final class DSAGenParameterSpec implements AlgorithmParameterSpec {
37 
38     private final int pLen;
39     private final int qLen;
40     private final int seedLen;
41 
42     /**
43      * Creates a domain parameter specification for DSA parameter
44      * generation using {@code primePLen} and {@code subprimeQLen}.
45      * The value of {@code subprimeQLen} is also used as the default
46      * length of the domain parameter seed in bits.
47      * @param primePLen the desired length of the prime P in bits.
48      * @param subprimeQLen the desired length of the sub-prime Q in bits.
49      * @throws    IllegalArgumentException if {@code primePLen}
50      * or {@code subprimeQLen} is illegal per the specification of
51      * FIPS 186-3.
52      */
DSAGenParameterSpec(int primePLen, int subprimeQLen)53     public DSAGenParameterSpec(int primePLen, int subprimeQLen) {
54         this(primePLen, subprimeQLen, subprimeQLen);
55     }
56 
57     /**
58      * Creates a domain parameter specification for DSA parameter
59      * generation using {@code primePLen}, {@code subprimeQLen},
60      * and {@code seedLen}.
61      * @param primePLen the desired length of the prime P in bits.
62      * @param subprimeQLen the desired length of the sub-prime Q in bits.
63      * @param seedLen the desired length of the domain parameter seed in bits,
64      * shall be equal to or greater than {@code subprimeQLen}.
65      * @throws    IllegalArgumentException if {@code primePLenLen},
66      * {@code subprimeQLen}, or {@code seedLen} is illegal per the
67      * specification of FIPS 186-3.
68      */
DSAGenParameterSpec(int primePLen, int subprimeQLen, int seedLen)69     public DSAGenParameterSpec(int primePLen, int subprimeQLen, int seedLen) {
70         switch (primePLen) {
71         case 1024:
72             if (subprimeQLen != 160) {
73                 throw new IllegalArgumentException
74                     ("subprimeQLen must be 160 when primePLen=1024");
75             }
76             break;
77         case 2048:
78             if (subprimeQLen != 224 && subprimeQLen != 256) {
79                throw new IllegalArgumentException
80                    ("subprimeQLen must be 224 or 256 when primePLen=2048");
81             }
82             break;
83         case 3072:
84             if (subprimeQLen != 256) {
85                 throw new IllegalArgumentException
86                     ("subprimeQLen must be 256 when primePLen=3072");
87             }
88             break;
89         default:
90             throw new IllegalArgumentException
91                 ("primePLen must be 1024, 2048, or 3072");
92         }
93         if (seedLen < subprimeQLen) {
94             throw new IllegalArgumentException
95                 ("seedLen must be equal to or greater than subprimeQLen");
96         }
97         this.pLen = primePLen;
98         this.qLen = subprimeQLen;
99         this.seedLen = seedLen;
100     }
101 
102     /**
103      * Returns the desired length of the prime P of the
104      * to-be-generated DSA domain parameters in bits.
105      * @return the length of the prime P.
106      */
getPrimePLength()107     public int getPrimePLength() {
108         return pLen;
109     }
110 
111     /**
112      * Returns the desired length of the sub-prime Q of the
113      * to-be-generated DSA domain parameters in bits.
114      * @return the length of the sub-prime Q.
115      */
getSubprimeQLength()116     public int getSubprimeQLength() {
117         return qLen;
118     }
119 
120     /**
121      * Returns the desired length of the domain parameter seed in bits.
122      * @return the length of the domain parameter seed.
123      */
getSeedLength()124     public int getSeedLength() {
125         return seedLen;
126     }
127 }
128