Path: blob/master/test/jdk/sun/security/provider/NSASuiteB/TestDSAGenParameterSpec.java
41153 views
/*1* Copyright (c) 2015, 2021, 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*/2223import java.security.AlgorithmParameterGenerator;24import java.security.AlgorithmParameters;25import java.security.InvalidAlgorithmParameterException;26import java.security.InvalidParameterException;27import java.security.KeyPairGenerator;28import java.security.NoSuchAlgorithmException;29import java.security.NoSuchProviderException;30import java.security.spec.DSAGenParameterSpec;31import java.security.spec.DSAParameterSpec;32import java.security.spec.InvalidParameterSpecException;3334/*35* @test36* @bug 8075286 816349837* @summary Verify that DSAGenParameterSpec can and can only be used to generate38* DSA within some certain range of key sizes as described in the class39* specification (L, N) as (1024, 160), (2048, 224), (2048, 256) and40* (3072, 256) should be OK for DSAGenParameterSpec.41* This test has been split based on lower/higher key sizes in order to42* reduce individual execution times and run in parallel43* (see TestDSAGenParameterSpecLongKey.java)44* @run main TestDSAGenParameterSpec 512 16045* @run main TestDSAGenParameterSpec 1024 160 true46* @run main TestDSAGenParameterSpec 1024 22447* @run main TestDSAGenParameterSpec 2048 16048* @run main/timeout=300 TestDSAGenParameterSpec 2048 224 true49* @run main/timeout=300 TestDSAGenParameterSpec 2048 256 true50* @run main TestDSAGenParameterSpec 3072 22451*/52public class TestDSAGenParameterSpec {5354private static final String ALGORITHM_NAME = "DSA";55private static final String PROVIDER_NAME = "SUN";5657private static void testDSAGenParameterSpec(DataTuple dataTuple)58throws NoSuchAlgorithmException, NoSuchProviderException,59InvalidParameterSpecException, InvalidAlgorithmParameterException {60System.out.printf("Test case: primePLen=%d, " + "subprimeQLen=%d%n",61dataTuple.primePLen, dataTuple.subprimeQLen);6263AlgorithmParameterGenerator apg64= AlgorithmParameterGenerator.getInstance(ALGORITHM_NAME,65PROVIDER_NAME);6667DSAGenParameterSpec genParamSpec = createGenParameterSpec(dataTuple);68// genParamSpec will be null if IllegalAE is thrown when expected.69if (genParamSpec == null) {70return;71}7273try {74apg.init(genParamSpec, null);75AlgorithmParameters param = apg.generateParameters();7677checkParam(param, genParamSpec);78System.out.println("Test case passed");79} catch (InvalidParameterException ipe) {80throw new RuntimeException("Test case failed.", ipe);81}82}8384private static void checkParam(AlgorithmParameters param,85DSAGenParameterSpec genParam) throws InvalidParameterSpecException,86NoSuchAlgorithmException, NoSuchProviderException,87InvalidAlgorithmParameterException {88String algorithm = param.getAlgorithm();89if (!algorithm.equalsIgnoreCase(ALGORITHM_NAME)) {90throw new RuntimeException(91"Unexpected type of parameters: " + algorithm);92}9394DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class);95int valueL = spec.getP().bitLength();96int strengthP = genParam.getPrimePLength();97if (strengthP != valueL) {98System.out.printf("P: Expected %d but actual %d%n", strengthP,99valueL);100throw new RuntimeException("Wrong P strength");101}102103int valueN = spec.getQ().bitLength();104int strengthQ = genParam.getSubprimeQLength();105if (strengthQ != valueN) {106System.out.printf("Q: Expected %d but actual %d%n", strengthQ,107valueN);108throw new RuntimeException("Wrong Q strength");109}110111if (genParam.getSubprimeQLength() != genParam.getSeedLength()) {112System.out.println("Defaut seed length should be the same as Q.");113throw new RuntimeException("Wrong seed length");114}115116KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM_NAME,117PROVIDER_NAME);118keyGen.initialize(spec);119}120121private static DSAGenParameterSpec createGenParameterSpec(122DataTuple dataTuple) {123DSAGenParameterSpec genParamSpec = null;124try {125genParamSpec = new DSAGenParameterSpec(dataTuple.primePLen,126dataTuple.subprimeQLen);127if (!dataTuple.isDSASpecSupported) {128throw new RuntimeException(129"Test case failed: the key length must not supported");130}131} catch (IllegalArgumentException e) {132if (!dataTuple.isDSASpecSupported) {133System.out.println("Test case passed: expected "134+ "IllegalArgumentException is caught");135} else {136throw new RuntimeException("Test case failed: unexpected "137+ "IllegalArgumentException is thrown", e);138}139}140141return genParamSpec;142}143144public static void main(String[] args) throws Exception {145if (args == null || args.length < 2) {146throw new RuntimeException("Invalid number of arguments to generate"147+ " DSA parameter.");148}149DataTuple dataTuple = null;150switch (args.length) {151case 3:152dataTuple = new DataTuple(Integer.valueOf(args[0]),153Integer.valueOf(args[1]), Boolean.valueOf(args[2]));154break;155case 2:156dataTuple = new DataTuple(Integer.valueOf(args[0]),157Integer.valueOf(args[1]), false);158break;159default:160throw new RuntimeException("Unsupported arguments found.");161}162testDSAGenParameterSpec(dataTuple);163164}165166private static class DataTuple {167168private int primePLen;169private int subprimeQLen;170private boolean isDSASpecSupported;171172private DataTuple(int primePLen, int subprimeQLen,173boolean isDSASpecSupported) {174this.primePLen = primePLen;175this.subprimeQLen = subprimeQLen;176this.isDSASpecSupported = isDSASpecSupported;177}178}179}180181182