Path: blob/master/test/jdk/com/sun/crypto/provider/KeyAgreement/SupportedDHKeys.java
41155 views
/*1* Copyright (c) 2016, 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*/2829import java.math.BigInteger;3031import java.security.*;32import javax.crypto.*;33import javax.crypto.interfaces.*;34import javax.crypto.spec.*;3536public class SupportedDHKeys {3738/*39* Sizes and values for various lengths.40*/41private enum SupportedKeySize {42dhp512(512), dhp768(768), dhp832(832),43dhp1024(1024), dhp1536(1536), dhp2048(2048),44dhp3072(3072), dhp4096(4096), dhp6144(6144),45dhp8192(8192);4647final int primeSize;4849SupportedKeySize(int primeSize) {50this.primeSize = primeSize;51}52}5354public static void main(String[] args) throws Exception {55for (SupportedKeySize keySize : SupportedKeySize.values()) {56System.out.println("Checking " + keySize.primeSize + " ...");57KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", "SunJCE");58kpg.initialize(keySize.primeSize);59KeyPair kp = kpg.generateKeyPair();60checkKeyPair(kp, keySize.primeSize);6162DHPublicKey publicKey = (DHPublicKey)kp.getPublic();63BigInteger p = publicKey.getParams().getP();64BigInteger g = publicKey.getParams().getG();65kpg.initialize(new DHParameterSpec(p, g));66kp = kpg.generateKeyPair();67checkKeyPair(kp, keySize.primeSize);68}69}7071private static void checkKeyPair(KeyPair kp, int pSize) throws Exception {7273DHPrivateKey privateKey = (DHPrivateKey)kp.getPrivate();74BigInteger p = privateKey.getParams().getP();75if (p.bitLength() != pSize) {76throw new Exception(77"Invalid modulus size: " + p.bitLength() + "/" + pSize);78}7980// System.out.println("P(" + pSize + "): " + p.toString());81if (!p.isProbablePrime(128)) {82throw new Exception("Good luck, the modulus is composite!");83}8485DHPublicKey publicKey = (DHPublicKey)kp.getPublic();86p = publicKey.getParams().getP();87if (p.bitLength() != pSize) {88throw new Exception(89"Invalid modulus size: " + p.bitLength() + "/" + pSize);90}9192BigInteger leftOpen = BigInteger.ONE;93BigInteger rightOpen = p.subtract(BigInteger.ONE);9495BigInteger x = privateKey.getX();96if ((x.compareTo(leftOpen) <= 0) ||97(x.compareTo(rightOpen) >= 0)) {98throw new Exception(99"X outside range [2, p - 2]: x: " + x + " p: " + p);100}101102BigInteger y = publicKey.getY();103if ((y.compareTo(leftOpen) <= 0) ||104(y.compareTo(rightOpen) >= 0)) {105throw new Exception(106"Y outside range [2, p - 2]: x: " + x + " p: " + p);107}108}109}110111112