Path: blob/master/test/jdk/sun/security/provider/DSA/TestKeyPairGenerator.java
41154 views
/*1* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4800108 8072452 818104826* @summary verify that precomputed DSA parameters are always used (512, 768,27* 1024, 2048, 3072 bit)28* @run main/othervm/timeout=15 TestKeyPairGenerator29*/3031//32// This fix is really a performance fix, so this test is not foolproof.33// Without the precomputed parameters, it will take a minute or more34// (unless you have a very fast machine). With the fix, the test should35// complete in less than 2 seconds. Use 15 second timeout to leave some room.36//3738import java.security.*;39import java.security.interfaces.*;4041public class TestKeyPairGenerator {4243private static void checkKeyLength(KeyPair kp, int len) throws Exception {44DSAPublicKey key = (DSAPublicKey)kp.getPublic();45int n = key.getParams().getP().bitLength();46System.out.println("Key length: " + n);47if (len != n) {48throw new Exception("Wrong key length");49}50}5152public static void main(String[] args) throws Exception {53long start = System.currentTimeMillis();54KeyPairGenerator kpg;55KeyPair kp;56// problem was when not calling initialize()57// do that twice to artifically inflate the time58// on JDKs that do not have the fix59kpg = KeyPairGenerator.getInstance("DSA", "SUN");60kp = kpg.generateKeyPair();6162kpg = KeyPairGenerator.getInstance("DSA", "SUN");63kp = kpg.generateKeyPair();6465// some other basic tests66kp = kpg.generateKeyPair();6768kpg.initialize(1024);69kp = kpg.generateKeyPair();70checkKeyLength(kp, 1024);7172kpg.initialize(768);73kp = kpg.generateKeyPair();74checkKeyLength(kp, 768);7576kpg.initialize(512);77kp = kpg.generateKeyPair();78checkKeyLength(kp, 512);7980kpg.initialize(2048);81kp = kpg.generateKeyPair();82checkKeyLength(kp, 2048);8384kpg.initialize(3072);85kp = kpg.generateKeyPair();86checkKeyLength(kp, 3072);8788long stop = System.currentTimeMillis();89System.out.println("Time: " + (stop - start) + " ms.");90}91}929394