blob: f5ad3525c0388a74f8f9027e78b846d7a23b0d5b [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4800108
27 * @summary verify that precomputed DSA parameters are always used (512, 768, 1024 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
36import java.security.*;
37import java.security.interfaces.*;
38
39public 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 long stop = System.currentTimeMillis();
82 System.out.println("Time: " + (stop - start) + " ms.");
83 }
84
85}