Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/crypto/provider/KeyAgreement/SupportedDHParamGens.java
38867 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* @run main/timeout=300 SupportedDHParamGens 51228* @run main/timeout=300 SupportedDHParamGens 76829* @run main/timeout=300 SupportedDHParamGens 83230* @run main/timeout=300 SupportedDHParamGens 102431* @run main/timeout=600 SupportedDHParamGens 204832* @run main/timeout=700 SupportedDHParamGens 307233*/3435import java.math.BigInteger;3637import java.security.*;38import javax.crypto.*;39import javax.crypto.interfaces.*;40import javax.crypto.spec.*;4142public class SupportedDHParamGens {4344public static void main(String[] args) throws Exception {45int primeSize = Integer.valueOf(args[0]).intValue();4647System.out.println("Checking " + primeSize + " ...");48AlgorithmParameterGenerator apg =49AlgorithmParameterGenerator.getInstance("DH", "SunJCE");50apg.init(primeSize);51AlgorithmParameters ap = apg.generateParameters();52DHParameterSpec spec = ap.getParameterSpec(DHParameterSpec.class);5354KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH", "SunJCE");55kpg.initialize(spec);56KeyPair kp = kpg.generateKeyPair();57checkKeyPair(kp, primeSize);58}5960private static void checkKeyPair(KeyPair kp, int pSize) throws Exception {6162DHPrivateKey privateKey = (DHPrivateKey)kp.getPrivate();63BigInteger p = privateKey.getParams().getP();64if (p.bitLength() != pSize) {65throw new Exception(66"Invalid modulus size: " + p.bitLength() + "/" + pSize);67}6869if (!p.isProbablePrime(128)) {70throw new Exception("Good luck, the modulus is composite!");71}7273DHPublicKey publicKey = (DHPublicKey)kp.getPublic();74p = publicKey.getParams().getP();75if (p.bitLength() != pSize) {76throw new Exception(77"Invalid modulus size: " + p.bitLength() + "/" + pSize);78}7980BigInteger leftOpen = BigInteger.ONE;81BigInteger rightOpen = p.subtract(BigInteger.ONE);8283BigInteger x = privateKey.getX();84if ((x.compareTo(leftOpen) <= 0) ||85(x.compareTo(rightOpen) >= 0)) {86throw new Exception(87"X outside range [2, p - 2]: x: " + x + " p: " + p);88}8990BigInteger y = publicKey.getY();91if ((y.compareTo(leftOpen) <= 0) ||92(y.compareTo(rightOpen) >= 0)) {93throw new Exception(94"Y outside range [2, p - 2]: x: " + x + " p: " + p);95}96}97}9899100