Skip to content

Commit 4cfd112

Browse files
authored
Merge pull request #172 from cconlon/perfImprovements
RSA KeyFactory and JUnit Test Performance Improvements
2 parents ce88449 + a28c0f4 commit 4cfd112

File tree

6 files changed

+261
-214
lines changed

6 files changed

+261
-214
lines changed

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,6 @@ private PublicKey generatePublicFromX509(X509EncodedKeySpec keySpec)
361361
throws InvalidKeySpecException {
362362

363363
byte[] x509Der = null;
364-
byte[] pubDer = null;
365364
Rsa rsa = null;
366365

367366
try {
@@ -378,19 +377,12 @@ private PublicKey generatePublicFromX509(X509EncodedKeySpec keySpec)
378377

379378
log("decoding X509 public key, length: " + x509Der.length);
380379

381-
/* Import X509 key into Rsa, validates DER */
380+
/* Import X509 key into Rsa to validate DER structure */
382381
rsa = new Rsa();
383382
rsa.decodePublicKey(x509Der);
384383

385-
/* Export as X509 to get wolfCrypt DER format */
386-
pubDer = rsa.exportPublicDer();
387-
if (pubDer == null) {
388-
throw new InvalidKeySpecException(
389-
"Failed to export public key as DER from Rsa object");
390-
}
391-
392-
/* Create wolfJCE RSAPublicKey object */
393-
return new WolfCryptRSAPublicKey(pubDer);
384+
/* Create wolfJCE RSAPublicKey object using original encoding */
385+
return new WolfCryptRSAPublicKey(x509Der);
394386

395387
} catch (WolfCryptException e) {
396388
throw new InvalidKeySpecException(
@@ -401,9 +393,6 @@ private PublicKey generatePublicFromX509(X509EncodedKeySpec keySpec)
401393
if (rsa != null) {
402394
rsa.releaseNativeStruct();
403395
}
404-
if (pubDer != null) {
405-
Arrays.fill(pubDer, (byte)0);
406-
}
407396
}
408397
}
409398

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

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ public class WolfCryptCipherTest {
116116
/* One static SecureRandom to share */
117117
private static SecureRandom secureRandom = new SecureRandom();
118118

119+
/* Pre-generated RSA key pair to share across all RSA cipher tests to
120+
* reduce test execution time. Key generation is expensive, especially
121+
* for RSA-2048. This is generated once in @BeforeClass and reused
122+
* across all RSA encryption/decryption tests. */
123+
private static KeyPair rsaPair = null;
124+
119125
@Rule(order = Integer.MIN_VALUE)
120126
public TestRule testWatcher = TimedTestWatcher.create();
121127

@@ -162,6 +168,22 @@ public static void testProviderInstallationAtRuntime()
162168
if (p != null) {
163169
interopProvider = "SunJCE";
164170
}
171+
172+
/* Generate RSA key pair once up front to reduce test execution time. */
173+
if (enabledJCEAlgos.contains("RSA") ||
174+
enabledJCEAlgos.contains("RSA/ECB/PKCS1Padding")) {
175+
try {
176+
KeyPairGenerator keyGen =
177+
KeyPairGenerator.getInstance("RSA");
178+
keyGen.initialize(2048, secureRandom);
179+
rsaPair = keyGen.generateKeyPair();
180+
} catch (Exception e) {
181+
/* If key generation fails, tests will fail with appropriate
182+
* error when they try to use the null key pair */
183+
System.err.println("Failed to generate RSA key pair in " +
184+
"@BeforeClass: " + e.getMessage());
185+
}
186+
}
165187
}
166188

167189
@Test
@@ -3983,10 +4005,9 @@ private void testRSAPublicPrivateEncryptDecrypt(String algo)
39834005
byte[] ciphertext = null;
39844006
byte[] plaintext = null;
39854007

3986-
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
3987-
keyGen.initialize(2048, secureRandom);
3988-
3989-
KeyPair pair = keyGen.generateKeyPair();
4008+
/* Use pre-generated RSA key pair */
4009+
KeyPair pair = rsaPair;
4010+
assertNotNull("RSA key pair should not be null", pair);
39904011
PrivateKey priv = pair.getPrivate();
39914012
PublicKey pub = pair.getPublic();
39924013

@@ -4041,10 +4062,9 @@ private void testRSAWithUpdateSizes(String algo)
40414062
byte[] ciphertext = null;
40424063
byte[] plaintext = null;
40434064

4044-
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
4045-
keyGen.initialize(2048, secureRandom);
4046-
4047-
KeyPair pair = keyGen.generateKeyPair();
4065+
/* Use pre-generated RSA key pair */
4066+
KeyPair pair = rsaPair;
4067+
assertNotNull("RSA key pair should not be null", pair);
40484068
PrivateKey priv = pair.getPrivate();
40494069
PublicKey pub = pair.getPublic();
40504070

@@ -4132,10 +4152,9 @@ private void testRSAWithUpdateVerifyFinalResetsState(String algo)
41324152
byte[] plaintextA = null;
41334153
byte[] plaintextB = null;
41344154

4135-
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
4136-
keyGen.initialize(2048, secureRandom);
4137-
4138-
KeyPair pair = keyGen.generateKeyPair();
4155+
/* Use pre-generated RSA key pair */
4156+
KeyPair pair = rsaPair;
4157+
assertNotNull("RSA key pair should not be null", pair);
41394158
PrivateKey priv = pair.getPrivate();
41404159
PublicKey pub = pair.getPublic();
41414160

@@ -4246,10 +4265,9 @@ private void testRSAWithTooBigData(String algo)
42464265
byte[] inputA = new byte[2048];
42474266
byte[] inputB = new byte[100];
42484267

4249-
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
4250-
keyGen.initialize(2048, secureRandom);
4251-
4252-
KeyPair pair = keyGen.generateKeyPair();
4268+
/* Use pre-generated RSA key pair */
4269+
KeyPair pair = rsaPair;
4270+
assertNotNull("RSA key pair should not be null", pair);
42534271
PrivateKey priv = pair.getPrivate();
42544272
PublicKey pub = pair.getPublic();
42554273

@@ -4364,10 +4382,9 @@ private void testRSAInterop(String algo)
43644382
byte[] ciphertext = null;
43654383
byte[] plaintext = null;
43664384

4367-
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
4368-
keyGen.initialize(2048, secureRandom);
4369-
4370-
KeyPair pair = keyGen.generateKeyPair();
4385+
/* Use pre-generated RSA key pair */
4386+
KeyPair pair = rsaPair;
4387+
assertNotNull("RSA key pair should not be null", pair);
43714388
PrivateKey priv = pair.getPrivate();
43724389
PublicKey pub = pair.getPublic();
43734390

@@ -5655,10 +5672,9 @@ private void testGetParametersRsa()
56555672
throws NoSuchAlgorithmException, NoSuchProviderException,
56565673
InvalidKeyException, NoSuchPaddingException {
56575674

5658-
/* Generate RSA key pair for testing */
5659-
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
5660-
kpg.initialize(2048);
5661-
KeyPair keyPair = kpg.generateKeyPair();
5675+
/* Use pre-generated RSA key pair */
5676+
KeyPair keyPair = rsaPair;
5677+
assertNotNull("RSA key pair should not be null", keyPair);
56625678

56635679
/* Test RSA/ECB/PKCS1Padding with public key */
56645680
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", jceProvider);
@@ -7182,9 +7198,9 @@ public void testNoOpUpdateBehavior()
71827198
/* Test RSA no-op update behavior */
71837199
if (enabledJCEAlgos.contains("RSA/ECB/PKCS1Padding")) {
71847200
try {
7185-
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
7186-
keyGen.initialize(2048);
7187-
KeyPair keyPair = keyGen.generateKeyPair();
7201+
/* Use pre-generated RSA key pair */
7202+
KeyPair keyPair = rsaPair;
7203+
assertNotNull("RSA key pair should not be null", keyPair);
71887204

71897205
Cipher cipher =
71907206
Cipher.getInstance("RSA/ECB/PKCS1Padding", jceProvider);
@@ -7463,10 +7479,9 @@ public void testRSACrtKeySupported()
74637479
return;
74647480
}
74657481

7466-
/* Generate RSA key pair - these are CRT keys by default */
7467-
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
7468-
keyGen.initialize(2048, secureRandom);
7469-
KeyPair pair = keyGen.generateKeyPair();
7482+
/* Use pre-generated RSA key pair - these are CRT keys by default */
7483+
KeyPair pair = rsaPair;
7484+
assertNotNull("RSA key pair should not be null", pair);
74707485
RSAPrivateKey privKey = (RSAPrivateKey) pair.getPrivate();
74717486
RSAPublicKey pubKey = (RSAPublicKey) pair.getPublic();
74727487

@@ -7503,10 +7518,9 @@ public void testRSANonCrtKeyRejected()
75037518
return;
75047519
}
75057520

7506-
/* Generate RSA key pair first */
7507-
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
7508-
keyGen.initialize(2048, secureRandom);
7509-
KeyPair pair = keyGen.generateKeyPair();
7521+
/* Use pre-generated RSA key pair */
7522+
KeyPair pair = rsaPair;
7523+
assertNotNull("RSA key pair should not be null", pair);
75107524

75117525
/* Extract RSA components */
75127526
RSAPublicKey rsaPub = (RSAPublicKey) pair.getPublic();
@@ -7562,11 +7576,9 @@ public void testRSAPublicKeyStillWorks()
75627576
return;
75637577
}
75647578

7565-
/* Generate RSA key pair */
7566-
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
7567-
keyGen.initialize(2048, secureRandom);
7568-
7569-
KeyPair pair = keyGen.generateKeyPair();
7579+
/* Use pre-generated RSA key pair */
7580+
KeyPair pair = rsaPair;
7581+
assertNotNull("RSA key pair should not be null", pair);
75707582
RSAPrivateKey privKey = (RSAPrivateKey) pair.getPrivate();
75717583
RSAPublicKey pubKey = (RSAPublicKey) pair.getPublic();
75727584

0 commit comments

Comments
 (0)