Path: blob/master/test/jdk/com/sun/crypto/provider/KeyAgreement/SupportedDHParamGens.java
41155 views
/*1* Copyright (c) 2016, 2021, 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 8072452 816349826* @summary Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits27* This test has been split based on lower/higher key sizes in order to28* reduce individual execution times and run in parallel29* (see SupportedDHParamGensLongKey.java)30* @run main/timeout=300 SupportedDHParamGens 51231* @run main/timeout=300 SupportedDHParamGens 76832* @run main/timeout=300 SupportedDHParamGens 83233* @run main/timeout=300 SupportedDHParamGens 102434* @run main/timeout=600 SupportedDHParamGens 204835*/3637import java.math.BigInteger;3839import java.security.*;40import javax.crypto.*;41import javax.crypto.interfaces.*;42import javax.crypto.spec.*;4344public class SupportedDHParamGens {4546public static void main(String[] args) throws Exception {47int primeSize = Integer.valueOf(args[0]).intValue();4849System.out.println("Checking " + primeSize + " ...");50AlgorithmParameterGenerator apg =51AlgorithmParameterGenerator.getInstance("DH", "SunJCE");52apg.init(primeSize);53AlgorithmParameters ap = apg.generateParameters();54DHParameterSpec spec = ap.getParameterSpec(DHParameterSpec.class);5556KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", "SunJCE");57kpg.initialize(spec);58KeyPair kp = kpg.generateKeyPair();59checkKeyPair(kp, primeSize);60}6162private static void checkKeyPair(KeyPair kp, int pSize) throws Exception {6364DHPrivateKey privateKey = (DHPrivateKey)kp.getPrivate();65BigInteger p = privateKey.getParams().getP();66if (p.bitLength() != pSize) {67throw new Exception(68"Invalid modulus size: " + p.bitLength() + "/" + pSize);69}7071if (!p.isProbablePrime(128)) {72throw new Exception("Good luck, the modulus is composite!");73}7475DHPublicKey publicKey = (DHPublicKey)kp.getPublic();76p = publicKey.getParams().getP();77if (p.bitLength() != pSize) {78throw new Exception(79"Invalid modulus size: " + p.bitLength() + "/" + pSize);80}8182BigInteger leftOpen = BigInteger.ONE;83BigInteger rightOpen = p.subtract(BigInteger.ONE);8485BigInteger x = privateKey.getX();86if ((x.compareTo(leftOpen) <= 0) ||87(x.compareTo(rightOpen) >= 0)) {88throw new Exception(89"X outside range [2, p - 2]: x: " + x + " p: " + p);90}9192BigInteger y = publicKey.getY();93if ((y.compareTo(leftOpen) <= 0) ||94(y.compareTo(rightOpen) >= 0)) {95throw new Exception(96"Y outside range [2, p - 2]: x: " + x + " p: " + p);97}98}99}100101102