Skip to content

Commit

Permalink
Use JNI critical arrays operations
Browse files Browse the repository at this point in the history
- Replaced [Get|Release]ByteArray with [Get|Release]PrimitiveArrayCritical

- Replaced release mode from 0 to JNI_ABORT
  • Loading branch information
mrdcvlsc committed May 15, 2023
1 parent 65484b6 commit 3979ba0
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions app/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,15 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_e
break;
}

prevBuffer = threadEnv->GetByteArrayElements(prevJniBuffer, NULL);
prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical(prevJniBuffer, NULL);
for (size_t index = 0; index < prevBufferSize; index += AES_BLOCK_SIZE) {
aes_scheme.blockEncrypt(
reinterpret_cast<unsigned char *>(prevBuffer + index),
reinterpret_cast<unsigned char *>(encryptedBuffer + index),
reinterpret_cast<unsigned char *>(iv)
);
}
threadEnv->ReleaseByteArrayElements(prevJniBuffer, prevBuffer, 0);
threadEnv->ReleasePrimitiveArrayCritical(prevJniBuffer, prevBuffer, JNI_ABORT);

threadEnv->SetByteArrayRegion(
prevJniBuffer, 0, prevBufferSize,
Expand All @@ -349,7 +349,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_e
bool excludeLastBlock = (remainingBlocks && remainingBytes == 0);

if (remainingBlocks) {
prevBuffer = threadEnv->GetByteArrayElements(prevJniBuffer, NULL);
prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical(prevJniBuffer, NULL);

for (; index < remainingBlocks - excludeLastBlock; ++index) {
aes_scheme.blockEncrypt(
Expand All @@ -358,7 +358,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_e
);
}

threadEnv->ReleaseByteArrayElements(prevJniBuffer, prevBuffer, 0);
threadEnv->ReleasePrimitiveArrayCritical(prevJniBuffer, prevBuffer, JNI_ABORT);
threadEnv->SetByteArrayRegion(
prevJniBuffer, 0, (remainingBlocks - excludeLastBlock) * AES_BLOCK_SIZE,
reinterpret_cast<jbyte *>(encryptedBuffer)
Expand All @@ -368,7 +368,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_e

Krypt::ByteArray cipher;

prevBuffer = threadEnv->GetByteArrayElements(prevJniBuffer, NULL);
prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical(prevJniBuffer, NULL);
if (excludeLastBlock) {
cipher = aes_scheme.encrypt(
reinterpret_cast<unsigned char *>(prevBuffer + (index * AES_BLOCK_SIZE)), AES_BLOCK_SIZE, iv
Expand All @@ -378,7 +378,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_e
reinterpret_cast<unsigned char *>(prevBuffer + (index * AES_BLOCK_SIZE)), remainingBytes, iv
);
}
threadEnv->ReleaseByteArrayElements(prevJniBuffer, prevBuffer, 0);
threadEnv->ReleasePrimitiveArrayCritical(prevJniBuffer, prevBuffer, JNI_ABORT);

threadEnv->SetByteArrayRegion(
prevJniBuffer, 0, cipher.length,
Expand Down Expand Up @@ -616,15 +616,15 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
break;
}

prevBuffer = threadEnv->GetByteArrayElements(prevJniBuffer, NULL);
prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical(prevJniBuffer, NULL);
for (size_t index = 0; index < prevBufferSize; index += AES_BLOCK_SIZE) {
aes_scheme.blockDecrypt(
reinterpret_cast<unsigned char *>(prevBuffer + index),
reinterpret_cast<unsigned char *>(decryptedBuffer + index),
reinterpret_cast<unsigned char *>(iv)
);
}
threadEnv->ReleaseByteArrayElements(prevJniBuffer, prevBuffer, 0);
threadEnv->ReleasePrimitiveArrayCritical(prevJniBuffer, prevBuffer, JNI_ABORT);

threadEnv->SetByteArrayRegion(
prevJniBuffer, 0, prevBufferSize,
Expand All @@ -644,7 +644,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
bool excludeLastBlock = (remainingBlocks && remainingBytes == 0);

if (remainingBlocks) {
prevBuffer = threadEnv->GetByteArrayElements(prevJniBuffer, NULL);
prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical(prevJniBuffer, NULL);

for (; index < remainingBlocks - excludeLastBlock; ++index) {
aes_scheme.blockDecrypt(
Expand All @@ -654,7 +654,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
);
}

threadEnv->ReleaseByteArrayElements(prevJniBuffer, prevBuffer, 0);
threadEnv->ReleasePrimitiveArrayCritical(prevJniBuffer, prevBuffer, JNI_ABORT);
threadEnv->SetByteArrayRegion(
prevJniBuffer, 0, (remainingBlocks - excludeLastBlock) * AES_BLOCK_SIZE,
reinterpret_cast<jbyte *>(decryptedBuffer)
Expand All @@ -665,7 +665,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d

Krypt::ByteArray recover;

prevBuffer = threadEnv->GetByteArrayElements(prevJniBuffer, NULL);
prevBuffer = (jbyte *) threadEnv->GetPrimitiveArrayCritical(prevJniBuffer, NULL);

if (excludeLastBlock) {
recover = aes_scheme.decrypt(
Expand All @@ -681,7 +681,7 @@ extern "C" JNIEXPORT jint JNICALL Java_com_application_bethela_BethelaActivity_d
);
}

threadEnv->ReleaseByteArrayElements(prevJniBuffer, prevBuffer, 0);
threadEnv->ReleasePrimitiveArrayCritical(prevJniBuffer, prevBuffer, JNI_ABORT);
threadEnv->ReleaseByteArrayElements(jniIV, iv, 0);

threadEnv->SetByteArrayRegion(
Expand Down

0 comments on commit 3979ba0

Please sign in to comment.