Skip to content

Commit

Permalink
Merge pull request #252 from ibmruntimes/openj9
Browse files Browse the repository at this point in the history
Merge latest OpenJ9 changes to 0.41
  • Loading branch information
JasonFengJ9 authored Sep 6, 2023
2 parents 2101e68 + 31de43f commit 7e7cdc3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
/*
* ===========================================================================
* (c) Copyright IBM Corp. 2018, 2021 All Rights Reserved
* (c) Copyright IBM Corp. 2018, 2023 All Rights Reserved
* ===========================================================================
*/
package com.sun.crypto.provider;
Expand Down Expand Up @@ -93,6 +93,9 @@ abstract class NativeChaCha20Cipher extends CipherSpi {
private static final Cleaner contextCleaner;
private final long context;

// The previous mode, initialized to an illegal value.
private int prevMode = Cipher.WRAP_MODE;

private final ByteArrayOutputStream aadBuf;

static {
Expand Down Expand Up @@ -600,7 +603,18 @@ private void init(int opmode, Key key, byte[] newNonce)
aadDone = false;
initialized = true;

int ret = nativeCrypto.ChaCha20Init(context, ossl_mode, openssl_iv, openssl_iv.length, keyBytes, keyBytes.length);
// Optimize the initialization of chacha20-poly1305 when they have the same ossl_mode
// otherwise, we have to reinitialize based upon the ossl_mode.
int ret;
if (prevMode == ossl_mode) {
ret = nativeCrypto.ChaCha20Init(context, ossl_mode, openssl_iv, openssl_iv.length, keyBytes, keyBytes.length, true);
} else {
ret = nativeCrypto.ChaCha20Init(context, ossl_mode, openssl_iv, openssl_iv.length, keyBytes, keyBytes.length, false);
prevMode = ossl_mode;
}
if (ret == -1) {
throw new ProviderException("Error in Native ChaCha20Cipher");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,8 @@ public final native int ChaCha20Init(long context,
byte[] iv,
int ivlen,
byte[] key,
int keylen);
int keylen,
boolean doReset);

public final native int ChaCha20Update(long context,
byte[] input,
Expand Down
28 changes: 18 additions & 10 deletions closed/src/java.base/share/native/libjncrypto/NativeCrypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -2153,11 +2153,11 @@ int OSSL102_RSA_set0_crt_params(RSA *r2, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqm
/*
* Class: jdk_crypto_jniprovider_NativeCrypto
* Method: ChaCha20Init
* Signature: (JI[BI[BI)I
* Signature: (JI[BI[BIZ)I
*/
JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_ChaCha20Init
(JNIEnv *env, jobject thisObj, jlong c, jint mode, jbyteArray iv, jint ivLen,
jbyteArray key, jint key_len)
jbyteArray key, jint key_len, jboolean doReset)
{
EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX*)(intptr_t) c;
unsigned char *ivNative = NULL;
Expand All @@ -2170,12 +2170,18 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_ChaCha20Init
}

if ((0 == mode) || (1 == mode)) {
evp_cipher1 = (*OSSL_chacha20_poly1305)();
/* Use the existing evp_cipher? */
if (JNI_FALSE == doReset) {
evp_cipher1 = (*OSSL_chacha20_poly1305)();
}
encrypt = mode;
} else if (2 == mode) {
/* Use the existing evp_cipher? */
if (JNI_FALSE == doReset) {
evp_cipher1 = (*OSSL_chacha20)();
}
/* encrypt or decrypt does not matter */
encrypt = 1;
evp_cipher1 = (*OSSL_chacha20)();
} else {
return -1;
}
Expand All @@ -2200,12 +2206,14 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_ChaCha20Init
}

/* if using Poly1305 */
if (2 != mode) {
if (1 != (*OSSL_CIPHER_CTX_ctrl)(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivLen, NULL)) {
printErrors();
(*env)->ReleaseByteArrayElements(env, iv, (jbyte*)ivNative, JNI_ABORT);
(*env)->ReleaseByteArrayElements(env, key, (jbyte*)keyNative, JNI_ABORT);
return -1;
if (JNI_FALSE == doReset) {
if (2 != mode) {
if (1 != (*OSSL_CIPHER_CTX_ctrl)(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivLen, NULL)) {
printErrors();
(*env)->ReleaseByteArrayElements(env, iv, (jbyte*)ivNative, JNI_ABORT);
(*env)->ReleaseByteArrayElements(env, key, (jbyte*)keyNative, JNI_ABORT);
return -1;
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/jdk.crypto.ec/share/classes/sun/security/ec/SunEC.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import jdk.crypto.jniprovider.NativeCrypto;

import sun.security.action.GetPropertyAction;
import sun.security.ec.ed.EdDSAKeyFactory;
import sun.security.ec.ed.EdDSAKeyPairGenerator;
import sun.security.ec.ed.EdDSASignature;
Expand All @@ -66,7 +67,7 @@ public final class SunEC extends Provider {
private static final boolean nativeCryptTrace = NativeCrypto.isTraceEnabled();

// Flag indicating whether the operating system is AIX.
private static final boolean isAIX = "AIX".equals(System.getProperty("os.name"));
private static final boolean isAIX = "AIX".equals(GetPropertyAction.privilegedGetProperty("os.name"));

/* The property 'jdk.nativeEC' is used to control enablement of the native
* ECDH implementation.
Expand Down

0 comments on commit 7e7cdc3

Please sign in to comment.