Skip to content

Commit f4b481f

Browse files
authored
Merge pull request ibmruntimes#209 from shanchao95/openj9
Modify RSA Key for multithread performance
2 parents a4434d5 + 4c8fd89 commit f4b481f

File tree

3 files changed

+63
-48
lines changed

3 files changed

+63
-48
lines changed

closed/src/java.base/share/classes/sun/security/rsa/NativeRSACore.java

+23-19
Original file line numberDiff line numberDiff line change
@@ -94,51 +94,55 @@ public static byte[] rsa(byte[] msg,sun.security.rsa.RSAPrivateCrtKeyImpl key, b
9494
/**
9595
* RSA public key ops. Simple modPow().
9696
*/
97-
synchronized private static byte[] crypt_Native(byte[] msg, sun.security.rsa.RSAPublicKeyImpl key)
97+
private static byte[] crypt_Native(byte[] msg, sun.security.rsa.RSAPublicKeyImpl key)
9898
throws BadPaddingException {
99-
10099
long nativePtr = key.getNativePtr();
101-
102100
if (nativePtr == -1) {
103101
return null;
104102
}
105103

106-
BigInteger n = key.getModulus();
107-
byte[] output = new byte[getByteLength(n)];
104+
byte[] output;
105+
int outputLen;
108106

109-
int outputLen = nativeCrypto.RSAEP(msg, msg.length, output, nativePtr);
107+
try {
108+
BigInteger n = key.getModulus();
109+
output = new byte[getByteLength(n)];
110+
outputLen = nativeCrypto.RSAEP(msg, msg.length, output, nativePtr);
111+
} finally {
112+
key.returnNativePtr(nativePtr);
113+
}
110114

111115
if (outputLen == -1) {
112116
return null;
113117
}
118+
114119
return output;
115120
}
116121

117122
/**
118123
* RSA private key operations with CRT. Algorithm and variable naming
119124
* are taken from PKCS#1 v2.1, section 5.1.2.
120125
*/
121-
synchronized private static byte[] crtCrypt_Native(byte[] msg, sun.security.rsa.RSAPrivateCrtKeyImpl key,
126+
private static byte[] crtCrypt_Native(byte[] msg, sun.security.rsa.RSAPrivateCrtKeyImpl key,
122127
boolean verify) throws BadPaddingException {
123128
long nativePtr = key.getNativePtr();
124-
125129
if (nativePtr == -1) {
126130
return null;
127131
}
128132

129-
int verifyInt;
130-
BigInteger n = key.getModulus();
131-
int outputLen = getByteLength(n);
132-
byte[] output = new byte[outputLen];
133-
134-
if(verify) {
135-
verifyInt = outputLen;
136-
} else {
137-
verifyInt = -1;
133+
int outputLen;
134+
byte[] output;
135+
136+
try {
137+
BigInteger n = key.getModulus();
138+
outputLen = getByteLength(n);
139+
output = new byte[outputLen];
140+
int verifyInt = verify ? outputLen : -1;
141+
outputLen = nativeCrypto.RSADP(msg, msg.length, output, verifyInt, nativePtr);
142+
} finally {
143+
key.returnNativePtr(nativePtr);
138144
}
139145

140-
outputLen = nativeCrypto.RSADP(msg, msg.length, output, verifyInt, nativePtr);
141-
142146
if (outputLen == -1) {
143147
return null;
144148
} else if (outputLen == -2) {

src/java.base/share/classes/sun/security/rsa/RSAPrivateCrtKeyImpl.java

+20-14
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import java.io.IOException;
3434
import java.math.BigInteger;
35+
import java.util.concurrent.ConcurrentLinkedQueue;
3536

3637
import java.security.*;
3738
import java.security.spec.*;
@@ -63,6 +64,8 @@ public final class RSAPrivateCrtKeyImpl
6364

6465
private static final long serialVersionUID = -1326088454257084918L;
6566

67+
private final ConcurrentLinkedQueue<Long> keyQ = new ConcurrentLinkedQueue<>();
68+
6669
private BigInteger n; // modulus
6770
private BigInteger e; // public exponent
6871
private BigInteger d; // private exponent
@@ -245,16 +248,7 @@ public BigInteger getCrtCoefficient() {
245248
return coeff;
246249
}
247250

248-
private long nativeRSAKey = 0x0;
249-
250-
/**
251-
* Get native RSA Public Key context pointer.
252-
* Create native context if uninitialized.
253-
*/
254-
protected long getNativePtr() {
255-
if (nativeRSAKey != 0x0) {
256-
return nativeRSAKey;
257-
}
251+
private long RSAPrivateKey_generate() {
258252

259253
BigInteger n = this.getModulus();
260254
BigInteger d = this.getPrivateExponent();
@@ -276,16 +270,28 @@ protected long getNativePtr() {
276270
byte[] dQ_2c = dQ.toByteArray();
277271
byte[] qInv_2c = qInv.toByteArray();
278272

279-
nativeRSAKey = nativeCrypto.createRSAPrivateCrtKey(n_2c,n_2c.length, d_2c, d_2c.length, e_2c, e_2c.length,
273+
return nativeCrypto.createRSAPrivateCrtKey(n_2c, n_2c.length, d_2c, d_2c.length, e_2c, e_2c.length,
280274
p_2c, p_2c.length, q_2c, q_2c.length,
281275
dP_2c, dP_2c.length, dQ_2c, dQ_2c.length, qInv_2c, qInv_2c.length);
282-
return nativeRSAKey;
276+
}
277+
278+
protected long getNativePtr() {
279+
Long ptr = keyQ.poll();
280+
if (ptr == null) {
281+
return RSAPrivateKey_generate();
282+
}
283+
return ptr;
284+
}
285+
286+
protected void returnNativePtr(long ptr) {
287+
keyQ.add(ptr);
283288
}
284289

285290
@Override
286291
public void finalize() {
287-
if (nativeRSAKey != 0x0 && nativeRSAKey != -1) {
288-
nativeCrypto.destroyRSAKey(nativeRSAKey);
292+
Long itr;
293+
while ((itr = keyQ.poll()) != null) {
294+
nativeCrypto.destroyRSAKey(itr);
289295
}
290296
}
291297

src/java.base/share/classes/sun/security/rsa/RSAPublicKeyImpl.java

+20-15
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import java.io.IOException;
3434
import java.math.BigInteger;
35+
import java.util.concurrent.ConcurrentLinkedQueue;
3536

3637
import java.security.*;
3738
import java.security.spec.*;
@@ -61,6 +62,8 @@ public final class RSAPublicKeyImpl extends X509Key implements RSAPublicKey {
6162
private static final long serialVersionUID = 2644735423591199609L;
6263
private static final BigInteger THREE = BigInteger.valueOf(3);
6364

65+
private final ConcurrentLinkedQueue<Long> keyQ = new ConcurrentLinkedQueue<>();
66+
6467
private BigInteger n; // modulus
6568
private BigInteger e; // public exponent
6669

@@ -217,31 +220,33 @@ protected Object writeReplace() throws java.io.ObjectStreamException {
217220
getEncoded());
218221
}
219222

220-
private long nativeRSAKey = 0x0;
221-
222-
/**
223-
* Get native RSA Public Key context pointer.
224-
* Create native context if uninitialized.
225-
*/
226-
protected long getNativePtr() {
227-
if (nativeRSAKey != 0x0) {
228-
return nativeRSAKey;
229-
}
230-
223+
private long RSAPublicKey_generate() {
231224
BigInteger n = this.getModulus();
232225
BigInteger e = this.getPublicExponent();
233226

234227
byte[] n_2c = n.toByteArray();
235228
byte[] e_2c = e.toByteArray();
236229

237-
nativeRSAKey = nativeCrypto.createRSAPublicKey(n_2c,n_2c.length, e_2c, e_2c.length);
238-
return nativeRSAKey;
230+
return nativeCrypto.createRSAPublicKey(n_2c, n_2c.length, e_2c, e_2c.length);
231+
}
232+
233+
protected long getNativePtr() {
234+
Long ptr = keyQ.poll();
235+
if (ptr == null) {
236+
return RSAPublicKey_generate();
237+
}
238+
return ptr;
239+
}
240+
241+
protected void returnNativePtr(long ptr) {
242+
keyQ.add(ptr);
239243
}
240244

241245
@Override
242246
public void finalize() {
243-
if (nativeRSAKey != 0x0 && nativeRSAKey != -1) {
244-
nativeCrypto.destroyRSAKey(nativeRSAKey);
247+
Long itr;
248+
while ((itr = keyQ.poll()) != null) {
249+
nativeCrypto.destroyRSAKey(itr);
245250
}
246251
}
247252
}

0 commit comments

Comments
 (0)