Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/KeyPairGenerator/TestDH2048.java
38855 views
/*1* Copyright (c) 2013, 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 7196382 807245226* @summary Ensure that DH key pairs can be generated for 512 - 8192 bits27* @author Valerie Peng28* @library ..29* @run main/othervm TestDH204830* @run main/othervm TestDH2048 sm31*/3233import java.security.InvalidParameterException;34import java.security.KeyPair;35import java.security.KeyPairGenerator;36import java.security.Provider;3738public class TestDH2048 extends PKCS11Test {3940private static void checkUnsupportedKeySize(KeyPairGenerator kpg, int ks)41throws Exception {42try {43kpg.initialize(ks);44throw new Exception("Expected IPE not thrown for " + ks);45} catch (InvalidParameterException ipe) {46}47}4849@Override50public void main(Provider p) throws Exception {51if (p.getService("KeyPairGenerator", "DH") == null) {52System.out.println("KPG for DH not supported, skipping");53return;54}55KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", p);56kpg.initialize(512);57KeyPair kp1 = kpg.generateKeyPair();5859int[] test_values = {768, 1024, 1536, 2048, 3072, 4096, 6144, 8192};60for (int i : test_values)61try {62kpg.initialize(i);63kp1 = kpg.generateKeyPair();64} catch (InvalidParameterException ipe) {65// NSS (as of version 3.13) has a hard coded maximum limit66// of 2236 or 3072 bits for DHE keys.67// SunPKCS11-Solaris has limit of 4096 on older systems68String prov = p.getName();69System.out.println(i + "-bit DH key pair generation: " + ipe);70if ((prov.equals("SunPKCS11-NSS") && i > 2048) ||71(prov.equals("SunPKCS11-Solaris") && i > 4096)) {72// OK73} else {74throw ipe;75}76}7778// key size must be multiples of 64 though79checkUnsupportedKeySize(kpg, 2048 + 63);80checkUnsupportedKeySize(kpg, 3072 + 32);81}8283public static void main(String[] args) throws Exception {84main(new TestDH2048(), args);85}86}878889