|
1 /* |
|
2 * Copyright (c) 2012, 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 7044060 |
|
27 * @summary verify that DSA parameter generation works |
|
28 * @run main/othervm/timeout=300 TestAlgParameterGenerator |
|
29 */ |
|
30 import java.security.*; |
|
31 import java.security.spec.*; |
|
32 import java.security.interfaces.*; |
|
33 |
|
34 public class TestAlgParameterGenerator { |
|
35 |
|
36 private static void checkParamStrength(AlgorithmParameters param, |
|
37 int strength) throws Exception { |
|
38 String algo = param.getAlgorithm(); |
|
39 if (!algo.equalsIgnoreCase("DSA")) { |
|
40 throw new Exception("Unexpected type of parameters: " + algo); |
|
41 } |
|
42 DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class); |
|
43 int valueL = spec.getP().bitLength(); |
|
44 if (strength != valueL) { |
|
45 System.out.println("Expected " + strength + " but actual " + valueL); |
|
46 throw new Exception("Wrong P strength"); |
|
47 } |
|
48 } |
|
49 private static void checkParamStrength(AlgorithmParameters param, |
|
50 DSAGenParameterSpec genParam) |
|
51 throws Exception { |
|
52 String algo = param.getAlgorithm(); |
|
53 if (!algo.equalsIgnoreCase("DSA")) { |
|
54 throw new Exception("Unexpected type of parameters: " + algo); |
|
55 } |
|
56 DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class); |
|
57 int valueL = spec.getP().bitLength(); |
|
58 int strength = genParam.getPrimePLength(); |
|
59 if (strength != valueL) { |
|
60 System.out.println("P: Expected " + strength + " but actual " + valueL); |
|
61 throw new Exception("Wrong P strength"); |
|
62 } |
|
63 int valueN = spec.getQ().bitLength(); |
|
64 strength = genParam.getSubprimeQLength(); |
|
65 if (strength != valueN) { |
|
66 System.out.println("Q: Expected " + strength + " but actual " + valueN); |
|
67 throw new Exception("Wrong Q strength"); |
|
68 } |
|
69 } |
|
70 |
|
71 public static void main(String[] args) throws Exception { |
|
72 AlgorithmParameterGenerator apg = |
|
73 AlgorithmParameterGenerator.getInstance("DSA", "SUN"); |
|
74 |
|
75 long start, stop; |
|
76 // make sure no-init still works |
|
77 start = System.currentTimeMillis(); |
|
78 AlgorithmParameters param = apg.generateParameters(); |
|
79 stop = System.currentTimeMillis(); |
|
80 System.out.println("Time: " + (stop - start) + " ms."); |
|
81 checkParamStrength(param, 1024); |
|
82 |
|
83 // make sure the old model works |
|
84 int[] strengths = { 512, 768, 1024 }; |
|
85 for (int i = 0; i < strengths.length; i++) { |
|
86 int sizeP = strengths[i]; |
|
87 System.out.println("Generating " + sizeP + "-bit DSA Parameters"); |
|
88 start = System.currentTimeMillis(); |
|
89 apg.init(sizeP); |
|
90 param = apg.generateParameters(); |
|
91 stop = System.currentTimeMillis(); |
|
92 System.out.println("Time: " + (stop - start) + " ms."); |
|
93 checkParamStrength(param, sizeP); |
|
94 } |
|
95 |
|
96 // now the newer model |
|
97 DSAGenParameterSpec spec1 = new DSAGenParameterSpec(1024, 160); |
|
98 DSAGenParameterSpec spec2 = new DSAGenParameterSpec(2048, 224); |
|
99 DSAGenParameterSpec spec3 = new DSAGenParameterSpec(2048, 256); |
|
100 //DSAGenParameterSpec spec4 = new DSAGenParameterSpec(3072, 256); |
|
101 DSAGenParameterSpec[] specSet = { |
|
102 spec1, spec2, spec3//, spec4 |
|
103 }; |
|
104 for (int i = 0; i < specSet.length; i++) { |
|
105 DSAGenParameterSpec genParam = specSet[i]; |
|
106 System.out.println("Generating (" + genParam.getPrimePLength() + |
|
107 ", " + genParam.getSubprimeQLength() + |
|
108 ") DSA Parameters"); |
|
109 start = System.currentTimeMillis(); |
|
110 apg.init(genParam, null); |
|
111 param = apg.generateParameters(); |
|
112 stop = System.currentTimeMillis(); |
|
113 System.out.println("Time: " + (stop - start) + " ms."); |
|
114 checkParamStrength(param, genParam); |
|
115 } |
|
116 } |
|
117 } |