Skip to content

Commit 595a071

Browse files
committed
JCE: replace non-wolfJCE SecureRandom in FIPS mode instead of throwing
1 parent 4f89eda commit 595a071

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

src/main/java/com/wolfssl/provider/jce/WolfCryptKeyGenerator.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,39 @@ private void sanitizeKeySize(int keysize)
156156
* Sanitize SecureRandom object if in FIPS mode to ensure we are
157157
* using wolfCrypt FIPS DRBG.
158158
*
159+
* If wolfCrypt FIPS is enabled and the provided SecureRandom is
160+
* not from the wolfJCE provider, this method will replace it with
161+
* a wolfJCE SecureRandom (HashDRBG) to maintain FIPS compliance.
162+
* This handles the case where the JDK auto-provides a platform
163+
* default SecureRandom (e.g. AndroidOpenSSL) when the user calls
164+
* KeyGenerator.init(int keysize) without specifying a SecureRandom.
165+
*
159166
* @param random SecureRandom object used for key generation.
160167
*
168+
* @return original SecureRandom if non-FIPS or already wolfJCE,
169+
* otherwise a new wolfJCE SecureRandom (HashDRBG).
170+
*
161171
* @throws InvalidParameterException if on top of wolfCrypt FIPS
162-
* and SecureRandom provider is not wolfJCE.
172+
* and unable to get wolfJCE SecureRandom.
163173
*/
164-
private void sanitizeSecureRandom(SecureRandom random)
174+
private SecureRandom sanitizeSecureRandom(SecureRandom random)
165175
throws InvalidParameterException {
166176

167177
if (Fips.enabled && (random != null)) {
168178
String randomProvider = random.getProvider().getName();
169179
if (!randomProvider.equals("wolfJCE")) {
170-
throw new InvalidParameterException(
171-
"SecureRandom provider must be wolfJCE if " +
172-
"using wolfCrypt FIPS, current = " + randomProvider);
180+
try {
181+
random = SecureRandom.getInstance("HashDRBG", "wolfJCE");
182+
183+
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
184+
throw new InvalidParameterException(
185+
"wolfCrypt FIPS requires wolfJCE SecureRandom but " +
186+
"unable to obtain: " + e.getMessage());
187+
}
173188
}
174189
}
190+
191+
return random;
175192
}
176193

177194
/**
@@ -220,10 +237,9 @@ protected void engineInit(int keysize, SecureRandom random)
220237
sanitizeKeySize(keysize);
221238

222239
/* If using wolfCrypt FIPS, make sure this is our SecureRandom */
223-
sanitizeSecureRandom(random);
240+
this.random = sanitizeSecureRandom(random);
224241

225242
this.keySizeBits = keysize;
226-
this.random = random;
227243
}
228244

229245
/**

0 commit comments

Comments
 (0)