SupportedDSAParamGen.java revision 14179:e8b09982c198
1/*
2 * Copyright (c) 2016, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8072452
27 * @summary Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
28 * @run main/timeout=300 SupportedDSAParamGen 1024 160
29 * @run main/timeout=300 SupportedDSAParamGen 2048 224
30 * @run main/timeout=300 SupportedDSAParamGen 2048 256
31 * @run main/timeout=450 SupportedDSAParamGen 3072 256
32 */
33import java.security.*;
34import java.security.spec.*;
35import java.security.interfaces.*;
36
37public class SupportedDSAParamGen {
38
39    public static void main(String[] args) throws Exception {
40        AlgorithmParameterGenerator apg =
41            AlgorithmParameterGenerator.getInstance("DSA", "SUN");
42
43        DSAGenParameterSpec spec = new DSAGenParameterSpec(
44                Integer.valueOf(args[0]).intValue(),
45                Integer.valueOf(args[1]).intValue());
46
47        System.out.println("Generating (" + spec.getPrimePLength() +
48                ", " + spec.getSubprimeQLength() + ") DSA Parameters");
49        long start = System.currentTimeMillis();
50        apg.init(spec, null);
51        AlgorithmParameters param = apg.generateParameters();
52        long stop = System.currentTimeMillis();
53        System.out.println("Time: " + (stop - start) + " ms.");
54        checkParamStrength(param, spec);
55    }
56
57    private static void checkParamStrength(AlgorithmParameters param,
58            DSAGenParameterSpec genParam) throws Exception {
59
60        String algo = param.getAlgorithm();
61        if (!algo.equalsIgnoreCase("DSA")) {
62            throw new Exception("Unexpected type of parameters: " + algo);
63        }
64
65        DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class);
66        int valueL = spec.getP().bitLength();
67        int strength = genParam.getPrimePLength();
68        if (strength != valueL) {
69            System.out.println(
70                    "P: Expected " + strength + " but actual " + valueL);
71            throw new Exception("Wrong P strength");
72        }
73
74        int valueN = spec.getQ().bitLength();
75        strength = genParam.getSubprimeQLength();
76        if (strength != valueN) {
77            System.out.println(
78                    "Q: Expected " + strength + " but actual " + valueN);
79            throw new Exception("Wrong Q strength");
80        }
81    }
82}
83