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.math.BigInteger;
29 import java.security.*;
30 import java.security.spec.AlgorithmParameterSpec;
31 import java.security.spec.InvalidParameterSpecException;
32 import javax.crypto.spec.DHParameterSpec;
33 import javax.crypto.spec.DHGenParameterSpec;
34
35 import sun.security.provider.ParameterCache;
36
37 /**
38 * This class represents the key pair generator for Diffie-Hellman key pairs.
39 *
40 * <p>This key pair generator may be initialized in two different ways:
41 *
42 * <ul>
43 * <li>By providing the size in bits of the prime modulus -
44 * This will be used to create a prime modulus and base generator, which will
45 * then be used to create the Diffie-Hellman key pair. The default size of the
46 * prime modulus is 1024 bits.
47 * <li>By providing a prime modulus and base generator
48 * </ul>
49 *
50 * @author Jan Luehe
51 *
52 *
53 * @see java.security.KeyPairGenerator
54 */
55 public final class DHKeyPairGenerator extends KeyPairGeneratorSpi {
56
57 // parameters to use or null if not specified
58 private DHParameterSpec params;
59
60 // The size in bits of the prime modulus
61 private int pSize;
62
63 // The size in bits of the random exponent (private value)
64 private int lSize;
65
66 // The source of randomness
67 private SecureRandom random;
68
69 public DHKeyPairGenerator() {
70 super();
71 initialize(1024, null);
72 }
73
74 private static void checkKeySize(int keysize)
75 throws InvalidParameterException {
76
77 if ((keysize < 512) || (keysize > 2048) || ((keysize & 0x3F) != 0)) {
78 throw new InvalidParameterException(
79 "DH key size must be multiple of 64, and can only range " +
80 "from 512 to 2048 (inclusive). " +
81 "The specific key size " + keysize + " is not supported");
82 }
83 }
84
85 /**
86 * Initializes this key pair generator for a certain keysize and source of
87 * randomness.
88 * The keysize is specified as the size in bits of the prime modulus.
89 *
90 * @param keysize the keysize (size of prime modulus) in bits
91 * @param random the source of randomness
|
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.math.BigInteger;
29 import java.security.*;
30 import java.security.spec.AlgorithmParameterSpec;
31 import java.security.spec.InvalidParameterSpecException;
32 import javax.crypto.spec.DHParameterSpec;
33 import javax.crypto.spec.DHGenParameterSpec;
34
35 import sun.security.provider.ParameterCache;
36 import static sun.security.util.SecurityProviderConstants.DEF_DH_KEY_SIZE;
37
38 /**
39 * This class represents the key pair generator for Diffie-Hellman key pairs.
40 *
41 * <p>This key pair generator may be initialized in two different ways:
42 *
43 * <ul>
44 * <li>By providing the size in bits of the prime modulus -
45 * This will be used to create a prime modulus and base generator, which will
46 * then be used to create the Diffie-Hellman key pair.
47 * <li>By providing a prime modulus and base generator
48 * </ul>
49 *
50 * @author Jan Luehe
51 *
52 *
53 * @see java.security.KeyPairGenerator
54 */
55 public final class DHKeyPairGenerator extends KeyPairGeneratorSpi {
56
57 // parameters to use or null if not specified
58 private DHParameterSpec params;
59
60 // The size in bits of the prime modulus
61 private int pSize;
62
63 // The size in bits of the random exponent (private value)
64 private int lSize;
65
66 // The source of randomness
67 private SecureRandom random;
68
69 public DHKeyPairGenerator() {
70 super();
71 initialize(DEF_DH_KEY_SIZE, null);
72 }
73
74 private static void checkKeySize(int keysize)
75 throws InvalidParameterException {
76
77 if ((keysize < 512) || (keysize > 2048) || ((keysize & 0x3F) != 0)) {
78 throw new InvalidParameterException(
79 "DH key size must be multiple of 64, and can only range " +
80 "from 512 to 2048 (inclusive). " +
81 "The specific key size " + keysize + " is not supported");
82 }
83 }
84
85 /**
86 * Initializes this key pair generator for a certain keysize and source of
87 * randomness.
88 * The keysize is specified as the size in bits of the prime modulus.
89 *
90 * @param keysize the keysize (size of prime modulus) in bits
91 * @param random the source of randomness
|