Path: blob/master/test/jdk/sun/security/pkcs11/KeyPairGenerator/TestDH2048.java
41153 views
/*1* Copyright (c) 2013, 2018, 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 /test/lib ..29* @modules jdk.crypto.cryptoki30* @run main/othervm TestDH204831* @run main/othervm -Djava.security.manager=allow TestDH2048 sm32*/3334import java.security.InvalidParameterException;35import java.security.KeyPair;36import java.security.KeyPairGenerator;37import java.security.Provider;3839public class TestDH2048 extends PKCS11Test {4041private static void checkUnsupportedKeySize(KeyPairGenerator kpg, int ks)42throws Exception {43try {44kpg.initialize(ks);45throw new Exception("Expected IPE not thrown for " + ks);46} catch (InvalidParameterException ipe) {47}48}4950@Override51public void main(Provider p) throws Exception {52if (p.getService("KeyPairGenerator", "DH") == null) {53System.out.println("KPG for DH not supported, skipping");54return;55}56KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", p);57kpg.initialize(512);58KeyPair kp1 = kpg.generateKeyPair();5960kpg.initialize(768);61kp1 = kpg.generateKeyPair();6263kpg.initialize(1024);64kp1 = kpg.generateKeyPair();6566kpg.initialize(1536);67kp1 = kpg.generateKeyPair();6869kpg.initialize(2048);70kp1 = kpg.generateKeyPair();7172try {73kpg.initialize(3072);74kp1 = kpg.generateKeyPair();7576kpg.initialize(4096);77kp1 = kpg.generateKeyPair();7879kpg.initialize(6144);80kp1 = kpg.generateKeyPair();8182kpg.initialize(8192);83kp1 = kpg.generateKeyPair();84} catch (InvalidParameterException ipe) {85// NSS (as of version 3.13) has a hard coded maximum limit86// of 2236 or 3072 bits for DHE keys.87System.out.println("4096-bit DH key pair generation: " + ipe);88if (!p.getName().equals("SunPKCS11-NSS")) {89throw ipe;90}91}9293// key size must be multiples of 64 though94checkUnsupportedKeySize(kpg, 2048 + 63);95checkUnsupportedKeySize(kpg, 3072 + 32);96}9798public static void main(String[] args) throws Exception {99main(new TestDH2048(), args);100}101}102103104