Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/provider/NSASuiteB/TestDSAGenParameterSpec.java
38854 views
/*1* Copyright (c) 2015, 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 807528637* @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) and (2048, 256)40* should be OK for DSAGenParameterSpec.41* @run main TestDSAGenParameterSpec 512 16042* @run main TestDSAGenParameterSpec 1024 160 true43* @run main TestDSAGenParameterSpec 1024 22444* @run main TestDSAGenParameterSpec 2048 16045* @run main/timeout=300 TestDSAGenParameterSpec 2048 224 true46* @run main/timeout=300 TestDSAGenParameterSpec 2048 256 true47*/48public class TestDSAGenParameterSpec {4950private static final String ALGORITHM_NAME = "DSA";51private static final String PROVIDER_NAME = "SUN";5253private static void testDSAGenParameterSpec(DataTuple dataTuple)54throws NoSuchAlgorithmException, NoSuchProviderException,55InvalidParameterSpecException, InvalidAlgorithmParameterException {56System.out.printf("Test case: primePLen=%d, " + "subprimeQLen=%d%n",57dataTuple.primePLen, dataTuple.subprimeQLen);5859AlgorithmParameterGenerator apg60= AlgorithmParameterGenerator.getInstance(ALGORITHM_NAME,61PROVIDER_NAME);6263DSAGenParameterSpec genParamSpec = createGenParameterSpec(dataTuple);64// genParamSpec will be null if IllegalAE is thrown when expected.65if (genParamSpec == null) {66return;67}6869try {70apg.init(genParamSpec, null);71AlgorithmParameters param = apg.generateParameters();7273checkParam(param, genParamSpec);74System.out.println("Test case passed");75} catch (InvalidParameterException ipe) {76throw new RuntimeException("Test case failed.", ipe);77}78}7980private static void checkParam(AlgorithmParameters param,81DSAGenParameterSpec genParam) throws InvalidParameterSpecException,82NoSuchAlgorithmException, NoSuchProviderException,83InvalidAlgorithmParameterException {84String algorithm = param.getAlgorithm();85if (!algorithm.equalsIgnoreCase(ALGORITHM_NAME)) {86throw new RuntimeException(87"Unexpected type of parameters: " + algorithm);88}8990DSAParameterSpec spec = param.getParameterSpec(DSAParameterSpec.class);91int valueL = spec.getP().bitLength();92int strengthP = genParam.getPrimePLength();93if (strengthP != valueL) {94System.out.printf("P: Expected %d but actual %d%n", strengthP,95valueL);96throw new RuntimeException("Wrong P strength");97}9899int valueN = spec.getQ().bitLength();100int strengthQ = genParam.getSubprimeQLength();101if (strengthQ != valueN) {102System.out.printf("Q: Expected %d but actual %d%n", strengthQ,103valueN);104throw new RuntimeException("Wrong Q strength");105}106107if (genParam.getSubprimeQLength() != genParam.getSeedLength()) {108System.out.println("Defaut seed length should be the same as Q.");109throw new RuntimeException("Wrong seed length");110}111112KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM_NAME,113PROVIDER_NAME);114keyGen.initialize(spec);115}116117private static DSAGenParameterSpec createGenParameterSpec(118DataTuple dataTuple) {119DSAGenParameterSpec genParamSpec = null;120try {121genParamSpec = new DSAGenParameterSpec(dataTuple.primePLen,122dataTuple.subprimeQLen);123if (!dataTuple.isDSASpecSupported) {124throw new RuntimeException(125"Test case failed: the key length must not supported");126}127} catch (IllegalArgumentException e) {128if (!dataTuple.isDSASpecSupported) {129System.out.println("Test case passed: expected "130+ "IllegalArgumentException is caught");131} else {132throw new RuntimeException("Test case failed: unexpected "133+ "IllegalArgumentException is thrown", e);134}135}136137return genParamSpec;138}139140public static void main(String[] args) throws Exception {141if (args == null || args.length < 2) {142throw new RuntimeException("Invalid number of arguments to generate"143+ " DSA parameter.");144}145DataTuple dataTuple = null;146switch (args.length) {147case 3:148dataTuple = new DataTuple(Integer.valueOf(args[0]),149Integer.valueOf(args[1]), Boolean.valueOf(args[2]));150break;151case 2:152dataTuple = new DataTuple(Integer.valueOf(args[0]),153Integer.valueOf(args[1]), false);154break;155default:156throw new RuntimeException("Unsupported arguments found.");157}158testDSAGenParameterSpec(dataTuple);159160}161162private static class DataTuple {163164private int primePLen;165private int subprimeQLen;166private boolean isDSASpecSupported;167168private DataTuple(int primePLen, int subprimeQLen,169boolean isDSASpecSupported) {170this.primePLen = primePLen;171this.subprimeQLen = subprimeQLen;172this.isDSASpecSupported = isDSASpecSupported;173}174}175}176177178