From 986fa08a69e0a3cda319a9a7b71d7c44cae1fee2 Mon Sep 17 00:00:00 2001 From: mrdcvlsc Date: Tue, 16 May 2023 18:47:29 +0800 Subject: [PATCH] Fix cmake for native code in release builds --- app/build.gradle | 4 +- app/src/main/cpp/CMakeLists.txt | 14 ++- app/src/main/cpp/native-lib.cpp | 118 +----------------- .../application/bethela/BethelaActivity.java | 18 +-- 4 files changed, 29 insertions(+), 125 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c9c5c08..bea49d4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,7 +8,7 @@ android { defaultConfig { applicationId "com.application.bethela" - minSdk 30 + minSdk 26 targetSdk 33 versionCode 1 versionName "1.0" @@ -20,7 +20,7 @@ android { // arguments "-DANDROID_STL=c++_shared" // Set C++ version - cppFlags '-std=c++14', '-march=armv8-a+crypto', '-O3' + cppFlags '-std=c++14' } } } diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index bb06d02..f82d3aa 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -39,7 +39,6 @@ find_library( # Sets the name of the path variable. # Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in this # build script, prebuilt third-party libraries, or system libraries. - target_link_libraries( # Specifies the target library. bethela @@ -47,6 +46,19 @@ target_link_libraries( # Specifies the target library. # included in the NDK. ${log-lib}) +if(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes -O3") +elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a+crypto -O3") +elseif (${ANDROID_ABI} STREQUAL "arm64-v8a") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8-a+crypto -O3") +#else() +# message(FATAL_ERROR "Unsupported architecture: ${CMAKE_SYSTEM_PROCESSOR}") +endif() + # Enable Address Sanitizer - NOT FOR RELEASE (IT'S NOT WORKING BECAUSE I'M AN IDIOT!) #target_compile_options(${TARGET} -fsanitize=hwaddress -fno-omit-frame-pointer) #set_target_properties(${TARGET} PROPERTIES LINK_FLAGS -fsanitize=hwaddress) \ No newline at end of file diff --git a/app/src/main/cpp/native-lib.cpp b/app/src/main/cpp/native-lib.cpp index bf8945d..f1c5a74 100644 --- a/app/src/main/cpp/native-lib.cpp +++ b/app/src/main/cpp/native-lib.cpp @@ -10,7 +10,11 @@ #include #include +#if defined(__x86_64__) || defined(_M_X64) +#define USE_AESNI +#elif defined(__aarch64__) || defined(_M_ARM64) #define USE_ARM_AES +#endif #include "Krypt/src/Krypt.hpp" #include "jpp.hpp" @@ -35,120 +39,6 @@ constexpr static jint FILE_SIGNATURE_SIZE = 7; /// the size of one AES block in bytes. constexpr static size_t AES_BLOCK_SIZE = 16; -extern "C" JNIEXPORT jstring JNICALL Java_com_application_bethela_BethelaActivity_doubleString ( - JNIEnv *env, - jobject, - jstring arg -) { - jboolean isCopy = 1; - const char *c_str_arg = env->GetStringUTFChars(arg, &isCopy); - std::string test(c_str_arg); - test += " | 2" + test + " : "; - jstring doubleString = env->NewStringUTF(test.c_str()); - delete[] c_str_arg; - return doubleString; -} - -extern "C" JNIEXPORT jbyteArray JNICALL Java_com_application_bethela_BethelaActivity_doubleByte ( - JNIEnv *env, - jobject, - jbyteArray arg, - jint size -) { - jbyte *read_only = env->GetByteArrayElements(arg, nullptr); - - jbyteArray doubleArray = env->NewByteArray(size * 2); - env->SetByteArrayRegion(doubleArray, 0, size, read_only); - env->SetByteArrayRegion(doubleArray, size, size, read_only); - - env->ReleaseByteArrayElements(arg, read_only, 0); - - return doubleArray; -} - -extern "C" JNIEXPORT jobjectArray JNICALL Java_com_application_bethela_BethelaActivity_transpose ( - JNIEnv *env, - jobject, - jobjectArray arr, - jint row, - jint column -) { - // Create a vector that will hold the original jintArray rows - std::vector original_rows; - - // Allocate an array of pointers that will contain the copy/reference of the rows - jint **row_elements = new jint *[row]; - - for (jsize i = 0; i < row; ++i) { - // get the `jintArray` row at index `i` of the `jobjectArray arr`, push it to a vector - original_rows.push_back((jintArray) env->GetObjectArrayElement(arr, i)); - - // create a copy/reference buffer of the aquired `jintArray` - row_elements[i] = env->GetIntArrayElements(original_rows.back(), nullptr); - } - - // Allocate a new C 2D array for the transpose and apply the transpose - jint **transposed_row_elements = new jint *[column]; - for (jsize i = 0; i < column; ++i) { - transposed_row_elements[i] = new jint[row]; - for (jsize j = 0; j < row; ++j) { - transposed_row_elements[i][j] = row_elements[j][i]; - } - } - - // create a new jobjectArray that will contain the transpose 2D array values - jclass jintArrayClass = env->FindClass("[I"); - jobjectArray transposed = env->NewObjectArray(column, jintArrayClass, nullptr); - - for (jsize i = 0; i < column; ++i) { - // create a new jintArray - jintArray curr_row = env->NewIntArray(row); - - // set the values of the new jintArray using the values of the transposed matrix - env->SetIntArrayRegion(curr_row, 0, row, transposed_row_elements[i]); - - // add the row jintArray to the main jobjectArray transpose matrix - env->SetObjectArrayElement(transposed, i, curr_row); - } - - // release the copy/reference buffers of the original rows that we got from the first loop - for (jsize i = 0; i < row; ++i) { - env->ReleaseIntArrayElements(original_rows[i], row_elements[i], 0); - } - - // deallocate all of the C++ array that we allocated using C++ convention - for (jsize i = 0; i < column; ++i) { - delete[] transposed_row_elements[i]; - } - - delete[] transposed_row_elements; - delete[] row_elements; - - return transposed; -} - -extern "C" JNIEXPORT jintArray JNICALL Java_com_application_bethela_BethelaActivity_reverse ( - JNIEnv *env, - jobject, - jintArray arr, - jint size -) { - // A C array that could be a copy or a direct pointer to `arr` - jint *reverse_array = env->GetIntArrayElements(arr, nullptr); - - // reverse the array - for (jsize i = 0; i < size / 2; ++i) { - jint temp = reverse_array[i]; - reverse_array[i] = reverse_array[size - 1 - i]; - reverse_array[size - 1 - i] = temp; - } - - // free the C array and apply the changes back to the `arr` - env->ReleaseIntArrayElements(arr, reverse_array, 0); - - return arr; -} - namespace RESULT_CODE { jint INVALID_INTERNAL_BUFFER_SIZE = -1; jint THREAD_ATTACHMENT_FAILED = -2; diff --git a/app/src/main/java/com/application/bethela/BethelaActivity.java b/app/src/main/java/com/application/bethela/BethelaActivity.java index 8cf3d80..bc83429 100644 --- a/app/src/main/java/com/application/bethela/BethelaActivity.java +++ b/app/src/main/java/com/application/bethela/BethelaActivity.java @@ -97,6 +97,8 @@ protected void onCreate(Bundle savedInstanceState) { Toast.makeText(this, "AES - Portable C++ (Slow)", Toast.LENGTH_LONG).show(); } else if (checkAesCodeImplementation() == 1) { Toast.makeText(this, "AES - ARM Neon (fast)", Toast.LENGTH_LONG).show(); + } else if (checkAesCodeImplementation() == 2) { + Toast.makeText(this, "AES - AES-NI (fast)", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "AES - cannot detect implementation", Toast.LENGTH_LONG).show(); } @@ -108,14 +110,14 @@ protected void onCreate(Bundle savedInstanceState) { private void allBtnSetEnabled(boolean enable) { imgBtnSelectKeyFile.setEnabled(enable); - imgBtnSelectPassword.setEnabled(enable);; - imgBtnGenerateKey.setEnabled(enable);; - imgBtnClearKeys.setEnabled(enable);; - imgBtnSelectFiles.setEnabled(enable);; - imgBtnClearFiles.setEnabled(enable);; - imgBtnSelectLocation.setEnabled(enable);; - imgBtnEncrypt.setEnabled(enable);; - imgBtnDecrypt.setEnabled(enable);; + imgBtnSelectPassword.setEnabled(enable); + imgBtnGenerateKey.setEnabled(enable); + imgBtnClearKeys.setEnabled(enable); + imgBtnSelectFiles.setEnabled(enable); + imgBtnClearFiles.setEnabled(enable); + imgBtnSelectLocation.setEnabled(enable); + imgBtnEncrypt.setEnabled(enable); + imgBtnDecrypt.setEnabled(enable); } // ##################################################################################