Path: blob/master/test/jdk/sun/security/pkcs11/KeyAgreement/SupportedDHKeys.java
41155 views
/*1* Copyright (c) 2016, 2020, 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 /test/lib ..28* @modules jdk.crypto.cryptoki29* @run main/othervm SupportedDHKeys30* @run main/othervm -Djava.security.manager=allow SupportedDHKeys sm31*/3233import java.math.BigInteger;34import java.security.KeyPair;35import java.security.KeyPairGenerator;36import java.security.Provider;37import javax.crypto.interfaces.DHPrivateKey;38import javax.crypto.interfaces.DHPublicKey;39import javax.crypto.spec.DHParameterSpec;4041public class SupportedDHKeys extends PKCS11Test {4243/*44* Sizes and values for various lengths.45*/46private enum SupportedKeySize {47dhp512(512), dhp768(768), dhp832(832),48dhp1024(1024), dhp1536(1536), dhp2048(2048);4950// the underlying pkcs11 may not support the following sizes yet51//52// dhp3072(3072), dhp4096(4096), dhp6144(6144),53// dhp8192(8192);5455final int primeSize;5657SupportedKeySize(int primeSize) {58this.primeSize = primeSize;59}60}6162@Override63public void main(Provider provider) throws Exception {64if (provider.getService("KeyPairGenerator", "DiffieHellman") == null) {65System.out.println("No support of DH KeyPairGenerator, skipping");66return;67}6869for (SupportedKeySize keySize : SupportedKeySize.values()) {70System.out.println("Checking " + keySize.primeSize + " ...");71KeyPairGenerator kpg =72KeyPairGenerator.getInstance("DiffieHellman", provider);73kpg.initialize(keySize.primeSize);74KeyPair kp = kpg.generateKeyPair();75checkKeyPair(kp, keySize.primeSize, provider);7677DHPublicKey publicKey = (DHPublicKey)kp.getPublic();78BigInteger p = publicKey.getParams().getP();79BigInteger g = publicKey.getParams().getG();80kpg.initialize(new DHParameterSpec(p, g));81kp = kpg.generateKeyPair();82checkKeyPair(kp, keySize.primeSize, provider);83}84}8586private static void checkKeyPair(KeyPair kp, int pSize,87Provider provider) throws Exception {8889DHPrivateKey privateKey = (DHPrivateKey)kp.getPrivate();90BigInteger p = privateKey.getParams().getP();91if (p.bitLength() != pSize) {92throw new Exception(93"Invalid modulus size: " + p.bitLength() + "/" + pSize);94}9596// System.out.println("P(" + pSize + "): " + p.toString());97if (!p.isProbablePrime(128)) {98throw new Exception("Good luck, the modulus is composite!");99}100101DHPublicKey publicKey = (DHPublicKey)kp.getPublic();102p = publicKey.getParams().getP();103if (p.bitLength() != pSize) {104throw new Exception(105"Invalid modulus size: " + p.bitLength() + "/" + pSize);106}107108BigInteger leftOpen = BigInteger.ONE;109BigInteger rightOpen = p.subtract(BigInteger.ONE);110111BigInteger 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}117118BigInteger y = publicKey.getY();119if ((y.compareTo(leftOpen) <= 0) ||120(y.compareTo(rightOpen) >= 0)) {121throw new Exception(122"Y outside range [2, p - 2]: y: " + y + " p: " + p);123}124}125126public static void main(String[] args) throws Exception {127main(new SupportedDHKeys(), args);128}129}130131132