Skip to content

Commit 25a8e20

Browse files
committed
Update the DH key size check to include the exponent size check
Move the checkKeyLengths() method from DHKeyFactory to DHKeyPairGenerator, rename it to checkKeySize(), and add a check for the exponent size, same as OpenJDK did. Fixes #418 Signed-off-by: Tao Liu <tao.liu@ibm.com> Update according to code review
1 parent 8d34b3d commit 25a8e20

File tree

2 files changed

+43
-46
lines changed

2 files changed

+43
-46
lines changed

src/main/java/com/ibm/crypto/plus/provider/DHKeyFactory.java

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import java.io.IOException;
1212
import java.security.InvalidKeyException;
13-
import java.security.InvalidParameterException;
1413
import java.security.Key;
1514
import java.security.KeyFactorySpi;
1615
import java.security.PrivateKey;
@@ -30,10 +29,6 @@
3029
public final class DHKeyFactory extends KeyFactorySpi {
3130

3231
private OpenJCEPlusProvider provider;
33-
public final static int MIN_KEYSIZE_NONFIPS = 512;
34-
public final static int MAX_KEYSIZE_NONFIPS = 8192;
35-
public final static int MIN_KEYSIZE_FIPS = 2048;
36-
public final static int MAX_KEYSIZE_FIPS = 8192;
3732

3833
static DHKey toDHKey(OpenJCEPlusProvider provider, Key key) throws InvalidKeyException {
3934
return (DHKey) new DHKeyFactory(provider).engineTranslateKey(key);
@@ -225,35 +220,4 @@ protected Key engineTranslateKey(Key key) throws InvalidKeyException {
225220
throw new InvalidKeyException("Cannot translate key", e);
226221
}
227222
}
228-
229-
/**
230-
* Check the length of an RSA key modulus/exponent to make sure it is not
231-
* too short or long. Some impls have their own min and max key sizes that
232-
* may or may not match with a system defined value.
233-
*
234-
* @param modulusLen
235-
* the bit length of the RSA modulus.
236-
* @param exponent
237-
* the RSA exponent
238-
* @param minModulusLen
239-
* if > 0, check to see if modulusLen is at least this long,
240-
* otherwise unused.
241-
* @param maxModulusLen
242-
* caller will allow this max number of bits. Allow the smaller
243-
* of the system-defined maximum and this param.
244-
*
245-
* @throws InvalidKeyException
246-
* if any of the values are unacceptable.
247-
*/
248-
static void checkKeyLengths(int keysize, int minsize, int maxsize)
249-
throws InvalidParameterException {
250-
251-
if ((keysize < minsize) || (keysize > maxsize) || ((keysize & 0x3F) != 0)) {
252-
throw new InvalidParameterException(
253-
"DH key size must be multiple of 64, and can only range "
254-
+ "from 512 to 8192 (inclusive). " + "The specific key size " + keysize
255-
+ " is not supported");
256-
}
257-
}
258-
259223
}

src/main/java/com/ibm/crypto/plus/provider/DHKeyPairGenerator.java

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)