Skip to content

Commit 466f304

Browse files
committed
JCE: set default RSA KeyPairGenerator key size and params if not explicitly set
1 parent 2fd62a2 commit 466f304

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ private WolfCryptKeyPairGenerator(KeyType type) {
8383

8484
this.type = type;
8585

86+
/* Set default parameters for RSA key generation */
87+
if (type == KeyType.WC_RSA) {
88+
this.keysize = 2048; /* Default RSA key size */
89+
this.publicExponent = Rsa.getDefaultRsaExponent();
90+
91+
/* Initialize RNG for default key generation */
92+
synchronized (rngLock) {
93+
if (this.rng == null) {
94+
this.rng = new Rng();
95+
this.rng.init();
96+
}
97+
}
98+
}
99+
86100
if (WolfCryptDebug.DEBUG) {
87101
algString = typeToString(type);
88102
}

src/test/java/com/wolfssl/provider/jce/test/WolfCryptKeyPairGeneratorTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
import java.security.spec.ECGenParameterSpec;
5050
import java.security.spec.X509EncodedKeySpec;
5151
import java.security.spec.PKCS8EncodedKeySpec;
52+
import java.security.interfaces.RSAPrivateKey;
53+
import java.security.interfaces.RSAPublicKey;
5254

5355
import com.wolfssl.wolfcrypt.Rsa;
5456
import com.wolfssl.wolfcrypt.Ecc;
@@ -556,6 +558,40 @@ public void testKeyPairGeneratorDhMultipleKeyGen()
556558
assertNotNull(kp2);
557559
}
558560

561+
@Test
562+
public void testKeyPairGeneratorRsaDefaultKeySize()
563+
throws NoSuchProviderException, NoSuchAlgorithmException {
564+
565+
/* Test that RSA KeyPairGenerator works with default parameters
566+
* without explicit initialization */
567+
KeyPairGenerator kpg =
568+
KeyPairGenerator.getInstance("RSA", "wolfJCE");
569+
570+
/* Generate key pair without calling initialize() first */
571+
KeyPair kp = kpg.generateKeyPair();
572+
assertNotNull(kp);
573+
assertNotNull(kp.getPublic());
574+
assertNotNull(kp.getPrivate());
575+
576+
/* Verify the generated key is RSA and has expected default size */
577+
assertTrue(kp.getPublic() instanceof RSAPublicKey);
578+
assertTrue(kp.getPrivate() instanceof RSAPrivateKey);
579+
580+
RSAPublicKey pubKey = (RSAPublicKey) kp.getPublic();
581+
RSAPrivateKey privKey = (RSAPrivateKey) kp.getPrivate();
582+
583+
/* Default key size should be 2048 bits */
584+
assertEquals("Default RSA key size should be 2048 bits",
585+
2048, pubKey.getModulus().bitLength());
586+
assertEquals("Private key modulus should match public key",
587+
pubKey.getModulus(), privKey.getModulus());
588+
589+
/* Verify the default public exponent */
590+
assertEquals("Default RSA public exponent should match wolfSSL default",
591+
BigInteger.valueOf(Rsa.getDefaultRsaExponent()),
592+
pubKey.getPublicExponent());
593+
}
594+
559595
@Test
560596
public void testKeyPairGeneratorRsassaPssKeyGeneration()
561597
throws NoSuchProviderException, NoSuchAlgorithmException,

0 commit comments

Comments
 (0)