1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.crypto.provider;
27
28 import java.security.*;
29 import java.security.spec.*;
30 import javax.crypto.spec.DHParameterSpec;
31 import javax.crypto.spec.DHGenParameterSpec;
32
33 /*
34 * This class generates parameters for the Diffie-Hellman algorithm.
35 * The parameters are a prime, a base, and optionally the length in bits of
36 * the private value.
37 *
38 * <p>The Diffie-Hellman parameter generation accepts the size in bits of the
39 * prime modulus and the size in bits of the random exponent as input.
40 * The size of the prime modulus defaults to 1024 bits.
41 *
42 * @author Jan Luehe
43 *
44 *
45 * @see java.security.AlgorithmParameters
46 * @see java.security.spec.AlgorithmParameterSpec
47 * @see DHParameters
48 */
49 public final class DHParameterGenerator
50 extends AlgorithmParameterGeneratorSpi {
51
52 // The size in bits of the prime modulus
53 private int primeSize = 1024;
54
55 // The size in bits of the random exponent (private value)
56 private int exponentSize = 0;
57
58 // The source of randomness
59 private SecureRandom random = null;
60
61 private static void checkKeySize(int keysize)
62 throws InvalidParameterException {
63 if ((keysize != 2048) &&
64 ((keysize < 512) || (keysize > 1024) || (keysize % 64 != 0))) {
65 throw new InvalidParameterException(
66 "DH key size must be multiple of 64 and range " +
67 "from 512 to 1024 (inclusive), or 2048. " +
68 "The specific key size " + keysize + " is not supported");
69 }
70 }
71
72 /**
73 * Initializes this parameter generator for a certain keysize
|
1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package com.sun.crypto.provider;
27
28 import java.security.*;
29 import java.security.spec.*;
30 import javax.crypto.spec.DHParameterSpec;
31 import javax.crypto.spec.DHGenParameterSpec;
32
33 import static sun.security.util.SecurityProviderConstants.DEF_DH_KEY_SIZE;
34
35 /*
36 * This class generates parameters for the Diffie-Hellman algorithm.
37 * The parameters are a prime, a base, and optionally the length in bits of
38 * the private value.
39 *
40 * <p>The Diffie-Hellman parameter generation accepts the size in bits of the
41 * prime modulus and the size in bits of the random exponent as input.
42 *
43 * @author Jan Luehe
44 *
45 *
46 * @see java.security.AlgorithmParameters
47 * @see java.security.spec.AlgorithmParameterSpec
48 * @see DHParameters
49 */
50 public final class DHParameterGenerator
51 extends AlgorithmParameterGeneratorSpi {
52
53 // The size in bits of the prime modulus
54 private int primeSize = DEF_DH_KEY_SIZE;
55
56 // The size in bits of the random exponent (private value)
57 private int exponentSize = 0;
58
59 // The source of randomness
60 private SecureRandom random = null;
61
62 private static void checkKeySize(int keysize)
63 throws InvalidParameterException {
64 if ((keysize != 2048) &&
65 ((keysize < 512) || (keysize > 1024) || (keysize % 64 != 0))) {
66 throw new InvalidParameterException(
67 "DH key size must be multiple of 64 and range " +
68 "from 512 to 1024 (inclusive), or 2048. " +
69 "The specific key size " + keysize + " is not supported");
70 }
71 }
72
73 /**
74 * Initializes this parameter generator for a certain keysize
|