@@ -25,8 +25,12 @@ public final class DHKeyPairGenerator extends KeyPairGeneratorSpi {
2525 private int keySize = 2048 ;
2626 private DHParameterSpec params ;
2727
28- public DHKeyPairGenerator (OpenJCEPlusProvider provider ) {
28+ public final static int MIN_KEYSIZE_NONFIPS = 512 ;
29+ public final static int MAX_KEYSIZE_NONFIPS = 8192 ;
30+ public final static int MIN_KEYSIZE_FIPS = 2048 ;
31+ public final static int MAX_KEYSIZE_FIPS = 8192 ;
2932
33+ public DHKeyPairGenerator (OpenJCEPlusProvider provider ) {
3034
3135 if (!OpenJCEPlusProvider .verifySelfIntegrity (this )) {
3236 throw new SecurityException ("Integrity check failed for: " + provider .getName ());
@@ -37,7 +41,6 @@ public DHKeyPairGenerator(OpenJCEPlusProvider provider) {
3741
3842 }
3943
40-
4144 /**
4245 * Initialize the receiver to use a given secure random generator, and
4346 * generate keys of a certain size.
@@ -71,11 +74,9 @@ public void initialize(int keySize, SecureRandom random) throws InvalidParameter
7174 private void initialize (int keySize , boolean genParams , java .security .SecureRandom random ) {
7275
7376 if (provider .isFIPS ()) {
74- DHKeyFactory .checkKeyLengths (keySize , DHKeyFactory .MIN_KEYSIZE_FIPS ,
75- DHKeyFactory .MAX_KEYSIZE_FIPS );
77+ checkKeySize (keySize , MIN_KEYSIZE_FIPS , MAX_KEYSIZE_FIPS , 0 );
7678 } else {
77- DHKeyFactory .checkKeyLengths (keySize , DHKeyFactory .MIN_KEYSIZE_NONFIPS ,
78- DHKeyFactory .MAX_KEYSIZE_NONFIPS );
79+ checkKeySize (keySize , MIN_KEYSIZE_NONFIPS , MAX_KEYSIZE_NONFIPS , 0 );
7980 }
8081
8182 if (genParams ) {
@@ -116,11 +117,9 @@ private void initialize(DHParameterSpec params, java.security.SecureRandom rando
116117 throws InvalidParameterException {
117118 int keySize = params .getP ().bitLength ();
118119 if (provider .isFIPS ()) {
119- DHKeyFactory .checkKeyLengths (keySize , DHKeyFactory .MIN_KEYSIZE_FIPS ,
120- DHKeyFactory .MAX_KEYSIZE_FIPS );
120+ checkKeySize (keySize , MIN_KEYSIZE_FIPS , MAX_KEYSIZE_FIPS , params .getL ());
121121 } else {
122- DHKeyFactory .checkKeyLengths (keySize , DHKeyFactory .MIN_KEYSIZE_NONFIPS ,
123- DHKeyFactory .MAX_KEYSIZE_NONFIPS );
122+ checkKeySize (keySize , MIN_KEYSIZE_NONFIPS , MAX_KEYSIZE_NONFIPS , params .getL ());
124123 }
125124 this .keySize = keySize ;
126125 this .params = params ;
@@ -168,4 +167,38 @@ public KeyPair generateKeyPair() {
168167 }
169168 }
170169
170+ /**
171+ * Check the length of an DH key modulus/exponent to make sure it is not
172+ * too short or long. Some impls have their own min and max key sizes that
173+ * may or may not match with a system defined value.
174+ *
175+ * @param keySize
176+ * the bit length of the modulus.
177+ * @param minSize
178+ * the minimum length of the modulus.
179+ * @param maxSize
180+ * the maximum length of the modulus.
181+ * @param expSize
182+ * the bit length of the exponent.
183+ *
184+ * @throws InvalidParameterException
185+ * if any of the values are unacceptable.
186+ */
187+ static void checkKeySize (int keySize , int minSize , int maxSize , int expSize )
188+ throws InvalidParameterException {
189+
190+ if ((keySize < minSize ) || (keySize > maxSize ) || ((keySize & 0x3F ) != 0 )) {
191+ throw new InvalidParameterException (
192+ "DH key size must be multiple of 64, and can only range " +
193+ "from " + minSize + " to " + maxSize + " (inclusive). " +
194+ "The specific key size " + keySize + " is not supported" );
195+ }
196+
197+ // optional, could be 0 if not specified
198+ if ((expSize < 0 ) || (expSize > keySize )) {
199+ throw new InvalidParameterException ("Exponent size must be positive and no larger than" +
200+ " modulus size" );
201+ }
202+ }
203+
171204}
0 commit comments