1 /*
2 * Copyright (c) 2003, 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 4800108
27 * @summary verify that precomputed DSA parameters are always used (512, 768, 1024, 2048 bit)
28 * @run main/othervm/timeout=15 TestKeyPairGenerator
29 */
30
31 // this fix is really a performance fix, so this test is not foolproof
32 // without it, it will take a minute or more (unless you have a very fast machine)
33 // with the fix, the test should complete in <2 seconds
34 // use 15 second timeout to leave some room
35
36 import java.security.*;
37 import java.security.interfaces.*;
38
39 public class TestKeyPairGenerator {
40
41 private static void checkKeyLength(KeyPair kp, int len) throws Exception {
42 DSAPublicKey key = (DSAPublicKey)kp.getPublic();
43 int n = key.getParams().getP().bitLength();
44 System.out.println("Key length: " + n);
45 if (len != n) {
46 throw new Exception("Wrong key length");
47 }
48 }
49
50 public static void main(String[] args) throws Exception {
51 long start = System.currentTimeMillis();
52 KeyPairGenerator kpg;
53 KeyPair kp;
54 // problem was when not calling initialize()
55 // do that twice to artifically inflate the time
56 // on JDKs that do not have the fix
57 kpg = KeyPairGenerator.getInstance("DSA", "SUN");
58 kp = kpg.generateKeyPair();
59 checkKeyLength(kp, 1024);
60
61 kpg = KeyPairGenerator.getInstance("DSA", "SUN");
62 kp = kpg.generateKeyPair();
63 checkKeyLength(kp, 1024);
64
65 // some other basic tests
66 kp = kpg.generateKeyPair();
67 checkKeyLength(kp, 1024);
68
69 kpg.initialize(1024);
70 kp = kpg.generateKeyPair();
71 checkKeyLength(kp, 1024);
72
73 kpg.initialize(768);
74 kp = kpg.generateKeyPair();
75 checkKeyLength(kp, 768);
76
77 kpg.initialize(512);
78 kp = kpg.generateKeyPair();
79 checkKeyLength(kp, 512);
80
81 kpg.initialize(2048);
82 kp = kpg.generateKeyPair();
83 checkKeyLength(kp, 2048);
84
85 long stop = System.currentTimeMillis();
86 System.out.println("Time: " + (stop - start) + " ms.");
87 }
|
1 /*
2 * Copyright (c) 2003, 2017, 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 4800108 8181048
27 * @summary verify that precomputed DSA parameters are always used (512, 768, 1024, 2048 bit)
28 * @run main/othervm/timeout=15 TestKeyPairGenerator
29 */
30
31 // this fix is really a performance fix, so this test is not foolproof
32 // without it, it will take a minute or more (unless you have a very fast machine)
33 // with the fix, the test should complete in <2 seconds
34 // use 15 second timeout to leave some room
35
36 import java.security.*;
37 import java.security.interfaces.*;
38
39 public class TestKeyPairGenerator {
40
41 private static void checkKeyLength(KeyPair kp, int len) throws Exception {
42 DSAPublicKey key = (DSAPublicKey)kp.getPublic();
43 int n = key.getParams().getP().bitLength();
44 System.out.println("Key length: " + n);
45 if (len != n) {
46 throw new Exception("Wrong key length");
47 }
48 }
49
50 public static void main(String[] args) throws Exception {
51 long start = System.currentTimeMillis();
52 KeyPairGenerator kpg;
53 KeyPair kp;
54 // problem was when not calling initialize()
55 // do that twice to artifically inflate the time
56 // on JDKs that do not have the fix
57 kpg = KeyPairGenerator.getInstance("DSA", "SUN");
58 kp = kpg.generateKeyPair();
59
60 kpg = KeyPairGenerator.getInstance("DSA", "SUN");
61 kp = kpg.generateKeyPair();
62
63 // some other basic tests
64 kp = kpg.generateKeyPair();
65
66 kpg.initialize(1024);
67 kp = kpg.generateKeyPair();
68 checkKeyLength(kp, 1024);
69
70 kpg.initialize(768);
71 kp = kpg.generateKeyPair();
72 checkKeyLength(kp, 768);
73
74 kpg.initialize(512);
75 kp = kpg.generateKeyPair();
76 checkKeyLength(kp, 512);
77
78 kpg.initialize(2048);
79 kp = kpg.generateKeyPair();
80 checkKeyLength(kp, 2048);
81
82 long stop = System.currentTimeMillis();
83 System.out.println("Time: " + (stop - start) + " ms.");
84 }
|