Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
KostasTsiounis committed Jun 6, 2023
1 parent 2c58f1b commit d81c941
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,20 @@
*/
public final class NativeECKeyPairGenerator extends KeyPairGeneratorSpi {

private static final int KEY_SIZE_MIN = 112; // min bits (see ecc_impl.h)
private static final int KEY_SIZE_MAX = 571; // max bits (see ecc_impl.h)
private static final int KEY_SIZE_MIN = 112;
private static final int KEY_SIZE_MAX = 571;

private static NativeCrypto nativeCrypto;
private static final boolean nativeCryptTrace = NativeCrypto.isTraceEnabled();

// used to seed the keypair generator
/* used to seed the keypair generator */
private SecureRandom random;

// size of the key to generate, KEY_SIZE_MIN <= keySize <= KEY_SIZE_MAX
/* size of the key to generate, KEY_SIZE_MIN <= keySize <= KEY_SIZE_MAX */
private int keySize;

// parameters specified via init, if any
private ECParameterSpec params = null;
/* parameters specified via init, if any */
private ECParameterSpec params;

/* the type of EC curve */
private String curve;
Expand All @@ -88,20 +88,26 @@ public final class NativeECKeyPairGenerator extends KeyPairGeneratorSpi {
private ECKeyPairGenerator javaImplementation;

/**
* Constructs a new ECKeyPairGenerator.
* Constructs a new NativeECKeyPairGenerator.
*/
public NativeECKeyPairGenerator() {
// initialize to default in case the app does not call initialize()
initialize(DEF_EC_KEY_SIZE, null);
}

// initialize the generator. See JCA doc
@Override
public void initialize(int keySize, SecureRandom random) {

checkKeySize(keySize);
if (keySize < KEY_SIZE_MIN) {
throw new InvalidParameterException
("Key size must be at least " + KEY_SIZE_MIN + " bits");
}
if (keySize > KEY_SIZE_MAX) {
throw new InvalidParameterException
("Key size must be at most " + KEY_SIZE_MAX + " bits");
}
this.keySize = keySize;
this.params = ECUtil.getECParameterSpec(null, keySize);
if (params == null) {
if (this.params == null) {
throw new InvalidParameterException(
"No EC parameters available for key size " + keySize + " bits");
}
Expand All @@ -116,11 +122,9 @@ public void initialize(int keySize, SecureRandom random) {
}
}

// second initialize method. See JCA doc
@Override
public void initialize(AlgorithmParameterSpec params, SecureRandom random)
throws InvalidAlgorithmParameterException {

ECParameterSpec ecSpec = null;

if (params instanceof ECParameterSpec) {
Expand All @@ -131,7 +135,8 @@ public void initialize(AlgorithmParameterSpec params, SecureRandom random)
"Unsupported curve: " + params);
}
} else if (params instanceof ECGenParameterSpec) {
String name = ((ECGenParameterSpec) params).getName();
ECGenParameterSpec ecGenParams = (ECGenParameterSpec) params;
String name = ecGenParams.getName();
ecSpec = ECUtil.getECParameterSpec(null, name);
if (ecSpec == null) {
throw new InvalidAlgorithmParameterException(
Expand All @@ -157,10 +162,8 @@ public void initialize(AlgorithmParameterSpec params, SecureRandom random)
}
}

// generate the keypair. See JCA doc
@Override
public KeyPair generateKeyPair() {

if (this.javaImplementation != null) {
return this.javaImplementation.generateKeyPair();
}
Expand Down Expand Up @@ -259,18 +262,6 @@ public KeyPair generateKeyPair() {
return new KeyPair(publicKey, privateKey);
}

private void checkKeySize(int keySize) throws InvalidParameterException {
if (keySize < KEY_SIZE_MIN) {
throw new InvalidParameterException
("Key size must be at least " + KEY_SIZE_MIN + " bits");
}
if (keySize > KEY_SIZE_MAX) {
throw new InvalidParameterException
("Key size must be at most " + KEY_SIZE_MAX + " bits");
}
this.keySize = keySize;
}

/**
* Initializes the java implementation.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@ JNIEXPORT jlong JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_loadCrypto
((NULL == OSSL_OPENSSL_malloc) && (ossl_ver < OPENSSL_VERSION_1_1_0)) ||
((NULL == OSSL_OPENSSL_free) && (ossl_ver < OPENSSL_VERSION_1_1_0)) ||
((NULL == OSSL_CRYPTO_THREADID_set_callback) && (ossl_ver < OPENSSL_VERSION_1_1_0)) ||
((NULL == OSSL_CRYPTO_set_locking_callback) && (ossl_ver < OPENSSL_VERSION_1_1_0))) {
((NULL == OSSL_CRYPTO_set_locking_callback) && (ossl_ver < OPENSSL_VERSION_1_1_0))
) {
#if 0
fprintf(stderr, "One or more of the required symbols are missing in the crypto library\n");
fflush(stderr);
Expand Down Expand Up @@ -2234,7 +2235,7 @@ Java_jdk_crypto_jniprovider_NativeCrypto_ECCreatePublicKey
}
ret = 1;

cleanup:
cleanup:
if (NULL != nativeX) {
(*env)->ReleasePrimitiveArrayCritical(env, x, nativeX, JNI_ABORT);
}
Expand Down
8 changes: 0 additions & 8 deletions jdk/src/share/classes/sun/security/ec/ECPrivateKeyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,6 @@ protected void parseKeyBits() throws InvalidKeyException {
}
}

/**
* Returns true if this key's EC field is an instance of ECFieldF2m.
* @return true if the field is an instance of ECFieldF2m, false otherwise
*/
boolean isECFieldF2m() {
return this.params.getCurve().getField() instanceof ECFieldF2m;
}

/**
* Returns the native EC public key context pointer.
* @return the native EC public key context pointer or -1 on error
Expand Down
8 changes: 0 additions & 8 deletions jdk/src/share/classes/sun/security/ec/ECPublicKeyImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,6 @@ protected Object writeReplace() throws java.io.ObjectStreamException {
getEncoded());
}

/**
* Returns true if this key's EC field is an instance of ECFieldF2m.
* @return true if the field is an instance of ECFieldF2m, false otherwise
*/
boolean isECFieldF2m() {
return this.params.getCurve().getField() instanceof ECFieldF2m;
}

/**
* Returns the native EC public key context pointer.
* @return the native EC public key context pointer or -1 on error
Expand Down
4 changes: 2 additions & 2 deletions jdk/src/share/classes/sun/security/ec/SunECEntries.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ static void putEntries(Map<Object, Object> map,
* Key Pair Generator engine
*/
if (useNativeECKeyGen
&& (NativeCrypto.getVersion() >= NativeCrypto.OPENSSL_VERSION_1_1_0)
&& !isAIX
&& (NativeCrypto.getVersion() >= NativeCrypto.OPENSSL_VERSION_1_1_0)
&& !isAIX
) {
map.put("KeyPairGenerator.EC", "sun.security.ec.NativeECKeyPairGenerator");
} else {
Expand Down

0 comments on commit d81c941

Please sign in to comment.