Path: blob/master/test/jdk/sun/security/provider/DSA/TestAlgParameterGenerator.java
41154 views
/*1* Copyright (c) 2012, 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 7044060 8055351 818104826* @summary verify that DSA parameter generation works27* @run main/timeout=600 TestAlgParameterGenerator28*/2930import java.security.AlgorithmParameterGenerator;31import java.security.AlgorithmParameters;32import java.security.spec.DSAGenParameterSpec;33import java.security.spec.DSAParameterSpec;3435public class TestAlgParameterGenerator {3637private static void checkParamStrength(AlgorithmParameters param,38int strength) throws Exception {39String algo = param.getAlgorithm();40if (!algo.equalsIgnoreCase("DSA")) {41throw new RuntimeException("Unexpected type of parameters: " + algo);42}43DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class);44int valueL = spec.getP().bitLength();45if (strength != valueL) {46System.out.println("Expected " + strength + " but actual " + valueL);47throw new RuntimeException("Wrong P strength");48}49}5051private static void checkParamStrength(AlgorithmParameters param,52DSAGenParameterSpec genParam)53throws Exception {54String algo = param.getAlgorithm();55if (!algo.equalsIgnoreCase("DSA")) {56throw new RuntimeException("Unexpected type of parameters: " + algo);57}58DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class);59int valueL = spec.getP().bitLength();60int strength = genParam.getPrimePLength();61if (strength != valueL) {62System.out.println("P: Expected " + strength + " but actual " + valueL);63throw new RuntimeException("Wrong P strength");64}65int valueN = spec.getQ().bitLength();66strength = genParam.getSubprimeQLength();67if (strength != valueN) {68System.out.println("Q: Expected " + strength + " but actual " + valueN);69throw new RuntimeException("Wrong Q strength");70}71}7273public static void main(String[] args) throws Exception {74AlgorithmParameterGenerator apg75= AlgorithmParameterGenerator.getInstance("DSA", "SUN");76long start, stop;7778// make sure no-init still works79start = System.currentTimeMillis();80AlgorithmParameters param = apg.generateParameters();81stop = System.currentTimeMillis();82System.out.println("Time: " + (stop - start) + " ms.");8384// make sure the old model works85int[] strengths = {512, 768, 1024};86for (int sizeP : strengths) {87System.out.println("Generating " + sizeP + "-bit DSA Parameters");88start = System.currentTimeMillis();89apg.init(sizeP);90param = apg.generateParameters();91stop = System.currentTimeMillis();92System.out.println("Time: " + (stop - start) + " ms.");93checkParamStrength(param, sizeP);94}9596// now the newer model97DSAGenParameterSpec[] specSet = {98new DSAGenParameterSpec(1024, 160),99new DSAGenParameterSpec(2048, 224),100new DSAGenParameterSpec(2048, 256)101// no support for prime size 3072102// ,new DSAGenParameterSpec(3072, 256)103};104105for (DSAGenParameterSpec genParam : specSet) {106System.out.println("Generating (" + genParam.getPrimePLength()107+ ", " + genParam.getSubprimeQLength() + ") DSA Parameters");108start = System.currentTimeMillis();109apg.init(genParam, null);110param = apg.generateParameters();111stop = System.currentTimeMillis();112System.out.println("Time: " + (stop - start) + " ms.");113checkParamStrength(param, genParam);114}115}116}117118119