Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does engineGeneratePublic need to use SecureRandom? #1520

Closed
artoonie opened this issue Oct 30, 2023 · 2 comments
Closed

Why does engineGeneratePublic need to use SecureRandom? #1520

artoonie opened this issue Oct 30, 2023 · 2 comments

Comments

@artoonie
Copy link

Given the following:

import java.security.PublicKey;
import java.security.spec.RSAPublicKeySpec;
RSAPublicKeySpec spec = new RSAPublicKeySpec(rsaModulus, rsaExponent);
PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(spec);

Why does generatePublic need to generate a random number? This operation is fully deterministic.

The following is the stack trace starting at generatePublic:

	at org.bouncycastle.fips.core/org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider$CoreSecureRandom.<init>(Unknown Source)
	at org.bouncycastle.fips.core/org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider$4.run(Unknown Source)
	at org.bouncycastle.fips.core/org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider$4.run(Unknown Source)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
	at org.bouncycastle.fips.core/org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider.getCoreSecureRandom(Unknown Source)
	at org.bouncycastle.fips.core/org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider.access$600(Unknown Source)
	at org.bouncycastle.fips.core/org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider$2.run(Unknown Source)
	at org.bouncycastle.fips.core/org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider$2.run(Unknown Source)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
	at org.bouncycastle.fips.core/org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider.getEntropySourceProvider(Unknown Source)
	at org.bouncycastle.fips.core/org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider$PooledSecureRandomProvider.get(Unknown Source)
	at org.bouncycastle.fips.core/org.bouncycastle.crypto.CryptoServicesRegistrar.getSecureRandomIfSet(Unknown Source)
	at org.bouncycastle.fips.core/org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider.getDefaultSecureRandom(Unknown Source)
	at org.bouncycastle.fips.core/org.bouncycastle.jcajce.provider.ProvRandom$1.createInstance(Unknown Source)
	at org.bouncycastle.fips.core/org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider$BcService.newInstance(Unknown Source)
	at java.base/java.security.SecureRandom.getDefaultPRNG(SecureRandom.java:296)
	at java.base/java.security.SecureRandom.<init>(SecureRandom.java:225)
	at org.bouncycastle.fips.core/org.bouncycastle.crypto.asymmetric.KeyUtils.validatedModulus(Unknown Source)
	at org.bouncycastle.fips.core/org.bouncycastle.crypto.asymmetric.KeyUtils.validated(Unknown Source)
	at org.bouncycastle.fips.core/org.bouncycastle.crypto.asymmetric.AsymmetricRSAPublicKey.<init>(Unknown Source)
	at org.bouncycastle.fips.core/org.bouncycastle.jcajce.provider.ProvRSAPublicKey.<init>(Unknown Source)
	at org.bouncycastle.fips.core/org.bouncycastle.jcajce.provider.ProvRSA$RSAKeyFactory.engineGeneratePublic(Unknown Source)
	at java.base/java.security.KeyFactory.generatePublic(KeyFactory.java:351)

Forgive me if this is a more basic RSA question that belongs on stackoverflow, and not one specific to bouncycastle.

@peterdettman
Copy link
Collaborator

It is used in public key validation, specifically for Miller-Rabin tests on the modulus.

@artoonie
Copy link
Author

Gotcha, thank you!

I may have questions later about how to get a FIPS-certified SecureRandom implementation if needed, but I'll follow the proper FIPS channels for that question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants