Skip to content

Commit 328e589

Browse files
committed
Optimize ChaCha20-Poly1305 initialization
1 parent 1af4b9b commit 328e589

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

closed/src/java.base/share/classes/com/sun/crypto/provider/NativeChaCha20Cipher.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
/*
2626
* ===========================================================================
27-
* (c) Copyright IBM Corp. 2018, 2021 All Rights Reserved
27+
* (c) Copyright IBM Corp. 2018, 2023 All Rights Reserved
2828
* ===========================================================================
2929
*/
3030
package com.sun.crypto.provider;
@@ -93,6 +93,9 @@ abstract class NativeChaCha20Cipher extends CipherSpi {
9393
private static final Cleaner contextCleaner;
9494
private final long context;
9595

96+
// The previous mode, initialized to an illegal value.
97+
private int prevMode = Cipher.WRAP_MODE;
98+
9699
private final ByteArrayOutputStream aadBuf;
97100

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

603-
int ret = nativeCrypto.ChaCha20Init(context, ossl_mode, openssl_iv, openssl_iv.length, keyBytes, keyBytes.length);
606+
// Optimize the initialization of chacha20-poly1305 when they have the same ossl_mode
607+
// otherwise, we have to reinitialize based upon the ossl_mode.
608+
int ret;
609+
if (prevMode == ossl_mode) {
610+
ret = nativeCrypto.ChaCha20Init(context, ossl_mode, openssl_iv, openssl_iv.length, keyBytes, keyBytes.length, true);
611+
} else {
612+
ret = nativeCrypto.ChaCha20Init(context, ossl_mode, openssl_iv, openssl_iv.length, keyBytes, keyBytes.length, false);
613+
prevMode = ossl_mode;
614+
}
615+
if (ret == -1) {
616+
throw new ProviderException("Error in Native ChaCha20Cipher");
617+
}
604618
}
605619

606620
/**

closed/src/java.base/share/classes/jdk/crypto/jniprovider/NativeCrypto.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ public final native int ChaCha20Init(long context,
321321
byte[] iv,
322322
int ivlen,
323323
byte[] key,
324-
int keylen);
324+
int keylen,
325+
boolean doReset);
325326

326327
public final native int ChaCha20Update(long context,
327328
byte[] input,

closed/src/java.base/share/native/libjncrypto/NativeCrypto.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,11 +2153,11 @@ int OSSL102_RSA_set0_crt_params(RSA *r2, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqm
21532153
/*
21542154
* Class: jdk_crypto_jniprovider_NativeCrypto
21552155
* Method: ChaCha20Init
2156-
* Signature: (JI[BI[BI)I
2156+
* Signature: (JI[BI[BIZ)I
21572157
*/
21582158
JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_ChaCha20Init
21592159
(JNIEnv *env, jobject thisObj, jlong c, jint mode, jbyteArray iv, jint ivLen,
2160-
jbyteArray key, jint key_len)
2160+
jbyteArray key, jint key_len, jboolean doReset)
21612161
{
21622162
EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX*)(intptr_t) c;
21632163
unsigned char *ivNative = NULL;
@@ -2170,12 +2170,18 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_ChaCha20Init
21702170
}
21712171

21722172
if ((0 == mode) || (1 == mode)) {
2173-
evp_cipher1 = (*OSSL_chacha20_poly1305)();
2173+
/* Use the existing evp_cipher? */
2174+
if (JNI_FALSE == doReset) {
2175+
evp_cipher1 = (*OSSL_chacha20_poly1305)();
2176+
}
21742177
encrypt = mode;
21752178
} else if (2 == mode) {
2179+
/* Use the existing evp_cipher? */
2180+
if (JNI_FALSE == doReset) {
2181+
evp_cipher1 = (*OSSL_chacha20)();
2182+
}
21762183
/* encrypt or decrypt does not matter */
21772184
encrypt = 1;
2178-
evp_cipher1 = (*OSSL_chacha20)();
21792185
} else {
21802186
return -1;
21812187
}
@@ -2200,12 +2206,14 @@ JNIEXPORT jint JNICALL Java_jdk_crypto_jniprovider_NativeCrypto_ChaCha20Init
22002206
}
22012207

22022208
/* if using Poly1305 */
2203-
if (2 != mode) {
2204-
if (1 != (*OSSL_CIPHER_CTX_ctrl)(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivLen, NULL)) {
2205-
printErrors();
2206-
(*env)->ReleaseByteArrayElements(env, iv, (jbyte*)ivNative, JNI_ABORT);
2207-
(*env)->ReleaseByteArrayElements(env, key, (jbyte*)keyNative, JNI_ABORT);
2208-
return -1;
2209+
if (JNI_FALSE == doReset) {
2210+
if (2 != mode) {
2211+
if (1 != (*OSSL_CIPHER_CTX_ctrl)(ctx, EVP_CTRL_AEAD_SET_IVLEN, ivLen, NULL)) {
2212+
printErrors();
2213+
(*env)->ReleaseByteArrayElements(env, iv, (jbyte*)ivNative, JNI_ABORT);
2214+
(*env)->ReleaseByteArrayElements(env, key, (jbyte*)keyNative, JNI_ABORT);
2215+
return -1;
2216+
}
22092217
}
22102218
}
22112219

0 commit comments

Comments
 (0)