Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/KeyAgreement/SupportedDHKeys.java
38855 views
/*1* Copyright (c) 2016, 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 807245226* @summary Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits27* @library ..28* @run main/othervm SupportedDHKeys29*/3031import java.math.BigInteger;3233import java.security.*;34import javax.crypto.*;35import javax.crypto.interfaces.*;36import javax.crypto.spec.*;3738public class SupportedDHKeys extends PKCS11Test {3940/*41* Sizes and values for various lengths.42*/43private enum SupportedKeySize {44dhp512(512), dhp768(768), dhp832(832),45dhp1024(1024), dhp1536(1536), dhp2048(2048);4647// the underlying pkcs11 may not support the following sizes yet48//49// dhp3072(3072), dhp4096(4096), dhp6144(6144),50// dhp8192(8192);5152final int primeSize;5354SupportedKeySize(int primeSize) {55this.primeSize = primeSize;56}57}5859@Override60public void main(Provider provider) throws Exception {61if (provider.getService("KeyPairGenerator", "DiffieHellman") == null) {62System.out.println("No support of DH KeyPairGenerator, skipping");63return;64}6566for (SupportedKeySize keySize : SupportedKeySize.values()) {67System.out.println("Checking " + keySize.primeSize + " ...");68KeyPairGenerator kpg =69KeyPairGenerator.getInstance("DiffieHellman", provider);70kpg.initialize(keySize.primeSize);71KeyPair kp = kpg.generateKeyPair();72checkKeyPair(kp, keySize.primeSize, provider);7374DHPublicKey publicKey = (DHPublicKey)kp.getPublic();75BigInteger p = publicKey.getParams().getP();76BigInteger g = publicKey.getParams().getG();77kpg.initialize(new DHParameterSpec(p, g));78kp = kpg.generateKeyPair();79checkKeyPair(kp, keySize.primeSize, provider);80}81}8283private static void checkKeyPair(KeyPair kp, int pSize,84Provider provider) throws Exception {8586DHPrivateKey privateKey = (DHPrivateKey)kp.getPrivate();87BigInteger p = privateKey.getParams().getP();88if (p.bitLength() != pSize) {89throw new Exception(90"Invalid modulus size: " + p.bitLength() + "/" + pSize);91}9293// System.out.println("P(" + pSize + "): " + p.toString());94if (!p.isProbablePrime(128)) {95throw new Exception("Good luck, the modulus is composite!");96}9798DHPublicKey publicKey = (DHPublicKey)kp.getPublic();99p = publicKey.getParams().getP();100if (p.bitLength() != pSize) {101throw new Exception(102"Invalid modulus size: " + p.bitLength() + "/" + pSize);103}104105BigInteger leftOpen = BigInteger.ONE;106BigInteger rightOpen = p.subtract(BigInteger.ONE);107108// ignore the private key range checking on Solaris at present109if (provider.getName().equals("SunPKCS11-Solaris") &&110!System.getProperty("os.name").equals("SunOS")) {111BigInteger x = privateKey.getX();112if ((x.compareTo(leftOpen) <= 0) ||113(x.compareTo(rightOpen) >= 0)) {114throw new Exception(115"X outside range [2, p - 2]: x: " + x + " p: " + p);116}117}118119BigInteger y = publicKey.getY();120if ((y.compareTo(leftOpen) <= 0) ||121(y.compareTo(rightOpen) >= 0)) {122throw new Exception(123"Y outside range [2, p - 2]: y: " + y + " p: " + p);124}125}126127public static void main(String[] args) throws Exception {128main(new SupportedDHKeys());129}130}131132133