0N/A/*
2362N/A * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.crypto.provider;
0N/A
0N/Aimport java.math.BigInteger;
0N/Aimport java.security.*;
0N/Aimport java.security.spec.*;
0N/Aimport javax.crypto.spec.DHParameterSpec;
0N/Aimport javax.crypto.spec.DHGenParameterSpec;
0N/A
0N/A/*
0N/A * This class generates parameters for the Diffie-Hellman algorithm.
0N/A * The parameters are a prime, a base, and optionally the length in bits of
0N/A * the private value.
0N/A *
0N/A * <p>The Diffie-Hellman parameter generation accepts the size in bits of the
0N/A * prime modulus and the size in bits of the random exponent as input.
0N/A * The size of the prime modulus defaults to 1024 bits.
0N/A *
0N/A * @author Jan Luehe
0N/A *
0N/A *
0N/A * @see java.security.AlgorithmParameters
0N/A * @see java.security.spec.AlgorithmParameterSpec
0N/A * @see DHParameters
0N/A */
0N/Apublic final class DHParameterGenerator
0N/Aextends AlgorithmParameterGeneratorSpi {
0N/A
0N/A // The size in bits of the prime modulus
0N/A private int primeSize = 1024;
0N/A
0N/A // The size in bits of the random exponent (private value)
0N/A private int exponentSize = 0;
0N/A
0N/A // The source of randomness
0N/A private SecureRandom random = null;
0N/A
0N/A /**
0N/A * Initializes this parameter generator for a certain keysize
0N/A * and source of randomness.
0N/A * The keysize is specified as the size in bits of the prime modulus.
0N/A *
0N/A * @param keysize the keysize (size of prime modulus) in bits
0N/A * @param random the source of randomness
0N/A */
0N/A protected void engineInit(int keysize, SecureRandom random) {
0N/A if ((keysize < 512) || (keysize > 1024) || (keysize % 64 != 0)) {
0N/A throw new InvalidParameterException("Keysize must be multiple "
0N/A + "of 64, and can only range "
0N/A + "from 512 to 1024 "
0N/A + "(inclusive)");
0N/A }
0N/A this.primeSize = keysize;
0N/A this.random = random;
0N/A }
0N/A
0N/A /**
0N/A * Initializes this parameter generator with a set of parameter
0N/A * generation values, which specify the size of the prime modulus and
0N/A * the size of the random exponent, both in bits.
0N/A *
0N/A * @param params the set of parameter generation values
0N/A * @param random the source of randomness
0N/A *
0N/A * @exception InvalidAlgorithmParameterException if the given parameter
0N/A * generation values are inappropriate for this parameter generator
0N/A */
0N/A protected void engineInit(AlgorithmParameterSpec genParamSpec,
0N/A SecureRandom random)
0N/A throws InvalidAlgorithmParameterException {
0N/A if (!(genParamSpec instanceof DHGenParameterSpec)) {
0N/A throw new InvalidAlgorithmParameterException
0N/A ("Inappropriate parameter type");
0N/A }
0N/A
0N/A DHGenParameterSpec dhParamSpec = (DHGenParameterSpec)genParamSpec;
0N/A
0N/A primeSize = dhParamSpec.getPrimeSize();
0N/A if ((primeSize<512) || (primeSize>1024) || (primeSize%64 != 0)) {
0N/A throw new InvalidAlgorithmParameterException
0N/A ("Modulus size must be multiple of 64, and can only range "
0N/A + "from 512 to 1024 (inclusive)");
0N/A }
0N/A
0N/A exponentSize = dhParamSpec.getExponentSize();
0N/A if (exponentSize <= 0) {
0N/A throw new InvalidAlgorithmParameterException
0N/A ("Exponent size must be greater than zero");
0N/A }
0N/A
0N/A // Require exponentSize < primeSize
0N/A if (exponentSize >= primeSize) {
0N/A throw new InvalidAlgorithmParameterException
0N/A ("Exponent size must be less than modulus size");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Generates the parameters.
0N/A *
0N/A * @return the new AlgorithmParameters object
0N/A */
0N/A protected AlgorithmParameters engineGenerateParameters() {
0N/A AlgorithmParameters algParams = null;
0N/A
0N/A if (this.exponentSize == 0) {
0N/A this.exponentSize = this.primeSize - 1;
0N/A }
0N/A
0N/A if (this.random == null)
0N/A this.random = SunJCE.RANDOM;
0N/A
0N/A try {
0N/A AlgorithmParameterGenerator paramGen;
0N/A DSAParameterSpec dsaParamSpec;
0N/A
0N/A paramGen = AlgorithmParameterGenerator.getInstance("DSA");
0N/A paramGen.init(this.primeSize, random);
0N/A algParams = paramGen.generateParameters();
0N/A dsaParamSpec = (DSAParameterSpec)
0N/A algParams.getParameterSpec(DSAParameterSpec.class);
0N/A
0N/A DHParameterSpec dhParamSpec;
0N/A if (this.exponentSize > 0) {
0N/A dhParamSpec = new DHParameterSpec(dsaParamSpec.getP(),
0N/A dsaParamSpec.getG(),
0N/A this.exponentSize);
0N/A } else {
0N/A dhParamSpec = new DHParameterSpec(dsaParamSpec.getP(),
0N/A dsaParamSpec.getG());
0N/A }
0N/A algParams = AlgorithmParameters.getInstance("DH", "SunJCE");
0N/A algParams.init(dhParamSpec);
0N/A } catch (InvalidParameterSpecException e) {
0N/A // this should never happen
0N/A throw new RuntimeException(e.getMessage());
0N/A } catch (NoSuchAlgorithmException e) {
0N/A // this should never happen, because we provide it
0N/A throw new RuntimeException(e.getMessage());
0N/A } catch (NoSuchProviderException e) {
0N/A // this should never happen, because we provide it
0N/A throw new RuntimeException(e.getMessage());
0N/A }
0N/A
0N/A return algParams;
0N/A }
0N/A}