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