Skip to content

Commit

Permalink
Merge pull request #209 from shanchao95/openj9
Browse files Browse the repository at this point in the history
Modify RSA Key for multithread performance
  • Loading branch information
keithc-ca authored Oct 4, 2019
2 parents a4434d5 + 4c8fd89 commit f4b481f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,51 +94,55 @@ public static byte[] rsa(byte[] msg,sun.security.rsa.RSAPrivateCrtKeyImpl key, b
/**
* RSA public key ops. Simple modPow().
*/
synchronized private static byte[] crypt_Native(byte[] msg, sun.security.rsa.RSAPublicKeyImpl key)
private static byte[] crypt_Native(byte[] msg, sun.security.rsa.RSAPublicKeyImpl key)
throws BadPaddingException {

long nativePtr = key.getNativePtr();

if (nativePtr == -1) {
return null;
}

BigInteger n = key.getModulus();
byte[] output = new byte[getByteLength(n)];
byte[] output;
int outputLen;

int outputLen = nativeCrypto.RSAEP(msg, msg.length, output, nativePtr);
try {
BigInteger n = key.getModulus();
output = new byte[getByteLength(n)];
outputLen = nativeCrypto.RSAEP(msg, msg.length, output, nativePtr);
} finally {
key.returnNativePtr(nativePtr);
}

if (outputLen == -1) {
return null;
}

return output;
}

/**
* RSA private key operations with CRT. Algorithm and variable naming
* are taken from PKCS#1 v2.1, section 5.1.2.
*/
synchronized private static byte[] crtCrypt_Native(byte[] msg, sun.security.rsa.RSAPrivateCrtKeyImpl key,
private static byte[] crtCrypt_Native(byte[] msg, sun.security.rsa.RSAPrivateCrtKeyImpl key,
boolean verify) throws BadPaddingException {
long nativePtr = key.getNativePtr();

if (nativePtr == -1) {
return null;
}

int verifyInt;
BigInteger n = key.getModulus();
int outputLen = getByteLength(n);
byte[] output = new byte[outputLen];

if(verify) {
verifyInt = outputLen;
} else {
verifyInt = -1;
int outputLen;
byte[] output;

try {
BigInteger n = key.getModulus();
outputLen = getByteLength(n);
output = new byte[outputLen];
int verifyInt = verify ? outputLen : -1;
outputLen = nativeCrypto.RSADP(msg, msg.length, output, verifyInt, nativePtr);
} finally {
key.returnNativePtr(nativePtr);
}

outputLen = nativeCrypto.RSADP(msg, msg.length, output, verifyInt, nativePtr);

if (outputLen == -1) {
return null;
} else if (outputLen == -2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.io.IOException;
import java.math.BigInteger;
import java.util.concurrent.ConcurrentLinkedQueue;

import java.security.*;
import java.security.spec.*;
Expand Down Expand Up @@ -63,6 +64,8 @@ public final class RSAPrivateCrtKeyImpl

private static final long serialVersionUID = -1326088454257084918L;

private final ConcurrentLinkedQueue<Long> keyQ = new ConcurrentLinkedQueue<>();

private BigInteger n; // modulus
private BigInteger e; // public exponent
private BigInteger d; // private exponent
Expand Down Expand Up @@ -245,16 +248,7 @@ public BigInteger getCrtCoefficient() {
return coeff;
}

private long nativeRSAKey = 0x0;

/**
* Get native RSA Public Key context pointer.
* Create native context if uninitialized.
*/
protected long getNativePtr() {
if (nativeRSAKey != 0x0) {
return nativeRSAKey;
}
private long RSAPrivateKey_generate() {

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

nativeRSAKey = nativeCrypto.createRSAPrivateCrtKey(n_2c,n_2c.length, d_2c, d_2c.length, e_2c, e_2c.length,
return nativeCrypto.createRSAPrivateCrtKey(n_2c, n_2c.length, d_2c, d_2c.length, e_2c, e_2c.length,
p_2c, p_2c.length, q_2c, q_2c.length,
dP_2c, dP_2c.length, dQ_2c, dQ_2c.length, qInv_2c, qInv_2c.length);
return nativeRSAKey;
}

protected long getNativePtr() {
Long ptr = keyQ.poll();
if (ptr == null) {
return RSAPrivateKey_generate();
}
return ptr;
}

protected void returnNativePtr(long ptr) {
keyQ.add(ptr);
}

@Override
public void finalize() {
if (nativeRSAKey != 0x0 && nativeRSAKey != -1) {
nativeCrypto.destroyRSAKey(nativeRSAKey);
Long itr;
while ((itr = keyQ.poll()) != null) {
nativeCrypto.destroyRSAKey(itr);
}
}

Expand Down
35 changes: 20 additions & 15 deletions src/java.base/share/classes/sun/security/rsa/RSAPublicKeyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.io.IOException;
import java.math.BigInteger;
import java.util.concurrent.ConcurrentLinkedQueue;

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

private final ConcurrentLinkedQueue<Long> keyQ = new ConcurrentLinkedQueue<>();

private BigInteger n; // modulus
private BigInteger e; // public exponent

Expand Down Expand Up @@ -217,31 +220,33 @@ protected Object writeReplace() throws java.io.ObjectStreamException {
getEncoded());
}

private long nativeRSAKey = 0x0;

/**
* Get native RSA Public Key context pointer.
* Create native context if uninitialized.
*/
protected long getNativePtr() {
if (nativeRSAKey != 0x0) {
return nativeRSAKey;
}

private long RSAPublicKey_generate() {
BigInteger n = this.getModulus();
BigInteger e = this.getPublicExponent();

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

nativeRSAKey = nativeCrypto.createRSAPublicKey(n_2c,n_2c.length, e_2c, e_2c.length);
return nativeRSAKey;
return nativeCrypto.createRSAPublicKey(n_2c, n_2c.length, e_2c, e_2c.length);
}

protected long getNativePtr() {
Long ptr = keyQ.poll();
if (ptr == null) {
return RSAPublicKey_generate();
}
return ptr;
}

protected void returnNativePtr(long ptr) {
keyQ.add(ptr);
}

@Override
public void finalize() {
if (nativeRSAKey != 0x0 && nativeRSAKey != -1) {
nativeCrypto.destroyRSAKey(nativeRSAKey);
Long itr;
while ((itr = keyQ.poll()) != null) {
nativeCrypto.destroyRSAKey(itr);
}
}
}

0 comments on commit f4b481f

Please sign in to comment.