From dbe0df7e44af1fdec8f0d632f41091cd254ec8fa Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Tue, 24 Sep 2024 16:28:49 -0600 Subject: [PATCH] JNI/JSSE: add sample Android Studio project for build tests --- .gitignore | 5 + IDE/Android/.gitignore | 13 + IDE/Android/.idea/.gitignore | 3 + IDE/Android/.idea/codeStyles/Project.xml | 29 ++ IDE/Android/.idea/compiler.xml | 6 + IDE/Android/.idea/gradle.xml | 19 + IDE/Android/.idea/jarRepositories.xml | 25 ++ IDE/Android/.idea/misc.xml | 22 ++ IDE/Android/.idea/vcs.xml | 7 + IDE/Android/README.md | 92 +++++ IDE/Android/app/.gitignore | 1 + IDE/Android/app/build.gradle | 50 +++ IDE/Android/app/proguard-rules.pro | 21 ++ IDE/Android/app/src/main/AndroidManifest.xml | 22 ++ IDE/Android/app/src/main/cpp/CMakeLists.txt | 344 ++++++++++++++++++ .../com/example/wolfssl/MainActivity.java | 95 +++++ .../drawable-v24/ic_launcher_foreground.xml | 34 ++ .../res/drawable/ic_launcher_background.xml | 170 +++++++++ .../app/src/main/res/layout/activity_main.xml | 34 ++ .../res/mipmap-anydpi-v26/ic_launcher.xml | 6 + .../src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 5066 bytes .../src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2973 bytes .../src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 7584 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 12238 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 18702 bytes .../app/src/main/res/values/colors.xml | 6 + .../app/src/main/res/values/strings.xml | 3 + .../app/src/main/res/values/styles.xml | 11 + IDE/Android/build.gradle | 32 ++ IDE/Android/gradle.properties | 17 + IDE/Android/gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 54329 bytes .../gradle/wrapper/gradle-wrapper.properties | 6 + IDE/Android/gradlew | 172 +++++++++ IDE/Android/gradlew.bat | 84 +++++ IDE/Android/settings.gradle | 1 + 35 files changed, 1330 insertions(+) create mode 100644 IDE/Android/.gitignore create mode 100644 IDE/Android/.idea/.gitignore create mode 100644 IDE/Android/.idea/codeStyles/Project.xml create mode 100644 IDE/Android/.idea/compiler.xml create mode 100644 IDE/Android/.idea/gradle.xml create mode 100644 IDE/Android/.idea/jarRepositories.xml create mode 100644 IDE/Android/.idea/misc.xml create mode 100644 IDE/Android/.idea/vcs.xml create mode 100644 IDE/Android/README.md create mode 100644 IDE/Android/app/.gitignore create mode 100644 IDE/Android/app/build.gradle create mode 100644 IDE/Android/app/proguard-rules.pro create mode 100644 IDE/Android/app/src/main/AndroidManifest.xml create mode 100644 IDE/Android/app/src/main/cpp/CMakeLists.txt create mode 100644 IDE/Android/app/src/main/java/com/example/wolfssl/MainActivity.java create mode 100644 IDE/Android/app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 IDE/Android/app/src/main/res/drawable/ic_launcher_background.xml create mode 100644 IDE/Android/app/src/main/res/layout/activity_main.xml create mode 100644 IDE/Android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 IDE/Android/app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 IDE/Android/app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 IDE/Android/app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 IDE/Android/app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 IDE/Android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 IDE/Android/app/src/main/res/values/colors.xml create mode 100644 IDE/Android/app/src/main/res/values/strings.xml create mode 100644 IDE/Android/app/src/main/res/values/styles.xml create mode 100644 IDE/Android/build.gradle create mode 100644 IDE/Android/gradle.properties create mode 100644 IDE/Android/gradle/wrapper/gradle-wrapper.jar create mode 100644 IDE/Android/gradle/wrapper/gradle-wrapper.properties create mode 100755 IDE/Android/gradlew create mode 100644 IDE/Android/gradlew.bat create mode 100644 IDE/Android/settings.gradle diff --git a/.gitignore b/.gitignore index 0779f1d..53c6290 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,11 @@ rpm/spec *.rpm wolfcrypt*.tar.gz +# Android +IDE/Android/.idea/deploymentTargetDropDown.xml +IDE/Android/app/.cxx/ +IDE/Android/app/src/main/cpp/wolfssl + # Windows IDE/WIN/.vs IDE/WIN/wolfcryptjni.vcxproj.user diff --git a/IDE/Android/.gitignore b/IDE/Android/.gitignore new file mode 100644 index 0000000..2b75303 --- /dev/null +++ b/IDE/Android/.gitignore @@ -0,0 +1,13 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/IDE/Android/.idea/.gitignore b/IDE/Android/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/IDE/Android/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/IDE/Android/.idea/codeStyles/Project.xml b/IDE/Android/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..30aa626 --- /dev/null +++ b/IDE/Android/.idea/codeStyles/Project.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/IDE/Android/.idea/compiler.xml b/IDE/Android/.idea/compiler.xml new file mode 100644 index 0000000..b589d56 --- /dev/null +++ b/IDE/Android/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/IDE/Android/.idea/gradle.xml b/IDE/Android/.idea/gradle.xml new file mode 100644 index 0000000..0897082 --- /dev/null +++ b/IDE/Android/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/IDE/Android/.idea/jarRepositories.xml b/IDE/Android/.idea/jarRepositories.xml new file mode 100644 index 0000000..a5f05cd --- /dev/null +++ b/IDE/Android/.idea/jarRepositories.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/IDE/Android/.idea/misc.xml b/IDE/Android/.idea/misc.xml new file mode 100644 index 0000000..3e48d41 --- /dev/null +++ b/IDE/Android/.idea/misc.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/IDE/Android/.idea/vcs.xml b/IDE/Android/.idea/vcs.xml new file mode 100644 index 0000000..f81dde8 --- /dev/null +++ b/IDE/Android/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/IDE/Android/README.md b/IDE/Android/README.md new file mode 100644 index 0000000..73c47a2 --- /dev/null +++ b/IDE/Android/README.md @@ -0,0 +1,92 @@ +# Android Studio Example Project + +This is an example Android Studio project file for wolfcrypt-jni / wolfJCE. +This project should be used for reference only. + +Tool and version information used when testing this project: + +- Ubuntu 20.04.3 LTS +- Android Studio Chipmunk 2021.2.1 +- Android Gradle Plugin Version: 4.2.2 +- Gradle Version: 7.1.3 +- API 30: Android 11 +- Emulator: Pixel 5 API 31 + +The following sections outline steps required to run this example on an +Android device or emulator. + +## 1. Add Native wolfSSL Library Source Code to Project + +This example project is already set up to compile and build the native +wolfSSL library source files, but the wolfSSL files themselves have not been +included in this package. You must download or link an appropriate version +of wolfSSL to this project using one of the options below. + +The project looks for the directory +`wolfcrypt-jni/IDE/Android/app/src/main/cpp/wolfssl` for wolfSSL source code. +This can added in multiple ways: + +- OPTION A: Download the latest wolfSSL library release from www.wolfssl.com, +unzip it, rename it to `wolfssl`, and place it in the direcotry +`wolfcrypt-jni/IDE/Android/app/src/main/cpp/`. + +``` +$ unzip wolfssl-X.X.X.zip +$ mv wolfssl-X.X.X wolfcrypt-jni/IDE/Android/app/src/main/cpp/wolfssl +``` + +- OPTION B: Alternatively GitHub can be used to clone wolfSSL: + +``` +$ cd /IDE/Android/app/src/main/cpp/ +$ git clone https://github.com/wolfssl/wolfssl +$ cp wolfssl/options.h.in wolfssl/options.h +``` + +- OPTION C: A symbolic link to a wolfssl directory on the system by using: + +``` +$ cd /IDE/Android/app/src/main/cpp/ +$ ln -s /path/to/local/wolfssl ./wolfssl +``` + +## 2. Update Java Symbolic Links (Only applies to Windows Users) + +The following Java source directory is a Unix/Linux symlink: + +``` +wolfcrypt-jni/IDE/Android/app/src/main/java/com/wolfssl +``` + +This will not work correctly on Windows, and a new Windows symbolic link needs +to be created in this location. To do so: + +1) Open Windows Command Prompt (Right click, and "Run as Administrator") +2) Navigate to `wolfcrypt-jni\IDE\Android\app\src\main\java\com` +3) Delete the existing symlink file (it shows up as a file called "wolfssl") + +``` +del wolfssl +``` + +4) Create a new relative symbolic link with `mklink`: + +``` +mklink /D wolfssl ..\..\..\..\..\..\..\src\java\com\wolfssl\ +``` + +## 3. Import and Build the Example Project with Android Studio + +1) Open the Android Studio project by double clicking on the `Android` folder +in wolfcrypt-jni/IDE/. Or, from inside Android Studio, open the `Android` +project located in the wolfcrypt-jni/IDE directory. + +2) Build the project and run MainActivity from app -> java/com/example.wolfssl. +This will ask for permissions to access the certificates in the /sdcard/ +directory and then print out the server certificate information on success. + +## Support + +Please contact wolfSSL support at support@wolfssl.com with any questions or +feedback. + diff --git a/IDE/Android/app/.gitignore b/IDE/Android/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/IDE/Android/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/IDE/Android/app/build.gradle b/IDE/Android/app/build.gradle new file mode 100644 index 0000000..2d61afd --- /dev/null +++ b/IDE/Android/app/build.gradle @@ -0,0 +1,50 @@ +apply plugin: 'com.android.application' + +android { + compileSdk 33 + defaultConfig { + applicationId "com.example.wolfssl" + /* Min SDK should stay at 24 to detect if we try to use newer APIs + * than were available in that Android SDK. We have users who are still + on SDK 24 (ref ZD 18311) */ + minSdkVersion 24 + targetSdkVersion 33 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + externalNativeBuild { + cmake { + cppFlags "" + } + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_11 + targetCompatibility JavaVersion.VERSION_11 + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + externalNativeBuild { + cmake { + path "src/main/cpp/CMakeLists.txt" + } + } + sourceSets { + main.java.srcDirs += '../../../src/main/java' + test.java.srcDirs += '../../../src/main/test' + } + namespace 'com.example.wolfssl' +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'com.android.support:appcompat-v7:28.0.0' + implementation 'com.android.support.constraint:constraint-layout:2.0.4' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'com.android.support.test:runner:1.0.2' + androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' +} diff --git a/IDE/Android/app/proguard-rules.pro b/IDE/Android/app/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/IDE/Android/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/IDE/Android/app/src/main/AndroidManifest.xml b/IDE/Android/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..c102629 --- /dev/null +++ b/IDE/Android/app/src/main/AndroidManifest.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/IDE/Android/app/src/main/cpp/CMakeLists.txt b/IDE/Android/app/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000..e3dd587 --- /dev/null +++ b/IDE/Android/app/src/main/cpp/CMakeLists.txt @@ -0,0 +1,344 @@ +# For more information about using CMake with Android Studio, read the +# documentation: https://d.android.com/studio/projects/add-native-code.html + +# Sets the minimum version of CMake required to build the native library. +cmake_minimum_required(VERSION 3.4.1) + +# Define project as both C and ASM for cases for SP has assembly enabled +project("wolfcryptjni-gradle" C ASM) + +# set wolfCrypt JNI location as environment variable, change if needed +set(wolfcryptjni_DIR ${CMAKE_SOURCE_DIR}/../../../../../../) +set(wolfssl_DIR ${CMAKE_SOURCE_DIR}/wolfssl/) + +# set warnings as errors, used in this example project but may be different +# in production apps/environments. +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror") + +# ---------------- wolfSSL Normal vs. FIPS Ready Selection --------------------- +# Select if wolfSSL is normal ("normal") or FIPS Ready ("fipsready") +# wolfSSL FIPS Ready is available for download on the wolfssl.com download page. +# For more information on wolfSSL FIPS Ready see: +# +# https://www.wolfssl.com/license/fips/ +# +# FIPS Ready requires different preprocessor flags, files, and a specific file +# ordering as set up below. +set(WOLFSSL_PKG_TYPE "normal") + +# -------------------------- Math Library Selection ---------------------------- +# wolfCrypt math library selection, used to switch on below. Should be one of: +# fastmath +# spmath +set(WOLFSSL_MATH_LIB "spmath") + +# Add header directories to include paths +include_directories( + ${wolfssl_DIR} + ${wolfcryptjni_DIR}/jni/include +) + +# ---------------------------- Preprocessor Defines ---------------------------- + +if ("${WOLFSSL_PKG_TYPE}" MATCHES "normal") + # Add preprocessor defines to CFLAGS, these match those placed into + # wolfssl/options.h by configure if using: "./configure --enable-jni". + # This list may be configurable depending on use case and desired + # optimizations. + add_definitions(-DWC_RSA_BLINDING -DWOLFSSL_SHA224 -DWOLFSSL_SHA384 + -DWOLFSSL_SHA512 -DHAVE_HKDF -DNO_DSA -DHAVE_ECC + -DECC_SHAMIR -DWC_RSA_PSS -DWOLFSSL_BASE64_ENCODE + -DWOLFSSL_SHA3 -DHAVE_POLY1305 -DHAVE_CHACHA -DHAVE_HASHDRBG + -DHAVE_TLS_EXTENSIONS -DHAVE_SUPPORTED_CURVES -DHAVE_FFDHE_2048 + -DWOLFSSL_TLS13 -DHAVE_EXTENDED_MASTER -DWOLFSSL_JNI + -DHAVE_EX_DATA -DWOLFSSL_DTLS -DOPENSSL_EXTRA -DOPENSSL_ALL + -DHAVE_CRL -DHAVE_OCSP -DHAVE_CRL_MONITOR + -DPERSIST_SESSION_CACHE -DPERSIST_CERT_CACHE -DATOMIC_USER + -DHAVE_PK_CALLBACKS -DWOLFSSL_CERT_EXT -DWOLFSSL_CERT_GEN + -DHAVE_SNI -DHAVE_ALPN -DNO_RC4 -DHAVE_ENCRYPT_THEN_MAC + -DNO_MD4 -DWOLFSSL_ENCRYPTED_KEYS -DHAVE_DH_DEFAULT_PARAMS + -DNO_ERROR_QUEUE -DWOLFSSL_EITHER_SIDE -DWC_RSA_NO_PADDING + -DWC_RSA_PSS -DWOLFSSL_PSS_LONG_SALT -DWOLFSSL_TICKET_HAVE_ID + -DWOLFSSL_ERROR_CODE_OPENSSL -DWOLFSSL_ALWAYS_VERIFY_CB + -DWOLFSSL_VERIFY_CB_ALL_CERTS -DWOLFSSL_EXTRA_ALERTS + -DHAVE_EXT_CACHE -DWOLFSSL_FORCE_CACHE_ON_TICKET + -DWOLFSSL_AKID_NAME -DHAVE_CTS -DNO_DES3 -DGCM_TABLE_4BIT + -DTFM_TIMING_RESISTANT -DECC_TIMING_RESISTANT + -DHAVE_AESGCM -DSIZEOF_LONG=4 -DSIZEOF_LONG_LONG=8 + + # For gethostbyname() + -DHAVE_NETDB_H + + # Defines added for debugging. These can be removed if debug + # logging is not needed and will increase performance and reduce + # library footprint size if removed. + #-DDEBUG_WOLFSSL -DWOLFSSL_ANDROID_DEBUG + + # Defines added for wolfCrypt test and benchmark only, may not + # be needed for your own application. Add -DNO_FILESYSTEM to + # disable file system use for wolfCrypt test, but make sure + # to remove this define in production applications as + # filesystem access is required for wolfJCE use. + -DUSE_CERT_BUFFERS_2048 -DUSE_CERT_BUFFERS_256 + -DNO_WRITE_TEMP_FILES -DNO_MAIN_DRIVER + ) + +elseif("${WOLFSSL_PKG_TYPE}" MATCHES "fipsready") + # The wolfCrypt FIPS In Core Integrity checksum will vary with compiler + # versions, runtime library versions, target hardware, and build type. + # Anytime the module is shifted up or down in memory or loaded from a + # new memory location the hash will change. This is expected during + # normal development cycles but should be stable in a production + # deployment. The verifyCore[] hash can be manually updated in + # 'wolfcrypt/src/fips_test.c' and the app recompiled. Or, the define + # WOLFCRYPT_FIPS_CORE_HASH_VALUE can be set below, which is helpful + # for Android Studio, which builds for all these architectures in the + # same build. + # + # The hash values below are only for reference and will need to + # be updated to match your build. To update this value: + # + # 1. Build and install your app which contains wolfCrypt FIPS + # 2. Run your app on each architecture, looking at the logcat output + # to see the expected verifyCore[] hash value. + # 3. Copy the expected hash value from logcat to the proper + # architecture section below. + # 4. Re-build your application now that the expected hash value has + # been set. + # 5. Re-install and re-run your application on each architecture to + # confirm the hash is stable and application runs as expected. + # + # NOTE: If using wolfSSL FIPS Ready or FIPS proper with this sample + # application and run into the scenario where the verifyCore[] hash + # output at runtime is empty, consider checking/increasing the size of + # the MAX_FIPS_DATA_SZ define in 'wolfcrypt/src/fips_test.c'. + + if("${ANDROID_ABI}" MATCHES "arm64-v8a") + # https://developer.android.com/ndk/guides/abis#arm64-v8a + add_definitions(-DWOLFCRYPT_FIPS_CORE_HASH_VALUE=DF2FF40654C405467072356FBA6C02A88F17E79B08A1A8F3A887C0F6AB4E4650) + elseif("${ANDROID_ABI}" MATCHES "armeabi-v7a") + # https://developer.android.com/ndk/guides/abis#v7a + add_definitions(-DWOLFCRYPT_FIPS_CORE_HASH_VALUE=DF2FF40654C405467072356FBA6C02A88F17E79B08A1A8F3A887C0F6AB4E4650) + elseif("${ANDROID_ABI}" MATCHES "x86_64") + # https://developer.android.com/ndk/guides/abis#86-64 + add_definitions(-DWOLFCRYPT_FIPS_CORE_HASH_VALUE=DF2FF40654C405467072356FBA6C02A88F17E79B08A1A8F3A887C0F6AB4E4650) + elseif("${ANDROID_ABI}" MATCHES "x86") + # https://developer.android.com/ndk/guides/abis#x86 + add_definitions(-DWOLFCRYPT_FIPS_CORE_HASH_VALUE=DF2FF40654C405467072356FBA6C02A88F17E79B08A1A8F3A887C0F6AB4E4650) + endif() + + # Add preprocessor defines to CFLAGS, these match those placed into + # wolfssl/options.h by configure if using: "./configure" on a Unix/Linux + # platform. The options below have been chosen to match a FIPS Ready + # build, and are based on the example user_settings.h file located here: + # https://github.com/wolfSSL/wolfssl/blob/master/examples/configs/user_settings_fipsv5.h + # This list may be configurable depending on use case and desired + # optimizations, being careful not to break FIPS compatibility if + # targeting FIPS proper in the future. + add_definitions(-DHAVE_FIPS -DHAVE_FIPS_VERSION=5 -DHAVE_FIPS_VERSION_MINOR=3 + -DHAVE_HASHDRBG -DHAVE_THREAD_LS -DHAVE_REPRODUCIBLE_BUILD + -DFP_MAX_BITS=16384 -DSP_INT_BITS=8192 -DWOLFSSL_PUBLIC_MP + -DTFM_TIMING_RESISTANT -DECC_TIMING_RESISTANT -DWC_RSA_BLINDING + -DWC_RNG_SEED_CB -DWOLFSSL_VALIDATE_ECC_IMPORT + -DWOLFSSL_VALIDATE_ECC_KEYGEN -DWOLFSSL_VALIDATE_FFC_IMPORT + -DWOLFSSL_TLS13 -DHAVE_TLS_EXTENSIONS -DHAVE_ENCRYPT_THEN_MAC + -DHAVE_SUPPORTED_CURVES -DHAVE_EXTENDED_MASTER -DHAVE_ONE_TIME_AUTH + -DHAVE_SECURE_RENEGOTIATION -DHAVE_SERVER_RENEGOTIATION_INFO -DHAVE_SESSION_TICKET + -DWOLFSSL_USE_ALIGN -DWOLFSSL_BASE64_ENCODE -DHAVE_CRL + -DHAVE_EXT_CACHE -DWOLFSSL_VERIFY_CB_ALL_CERTS -DWOLFSSL_ALWAYS_VERIFY_CB + -DWOLFSSL_DH_EXTRA -DWOLFSSL_WOLFSSH -DOPENSSL_EXTRA -DOPENSSL_ALL + -DHAVE_FFDHE_Q -DHAVE_FFDHE_2048 -DHAVE_FFDHE_3072 -DHAVE_FFDHE_4096 + -DHAVE_FFDHE_6144 -DHAVE_FFDHE_8192 -DHAVE_DH_DEFAULT_PARAMS -DHAVE_PUBLIC_FFDHE + -DHAVE_ECC -DTFM_ECC256 -DECC_SHAMIR -DHAVE_ECC_CDH -DECC_USER_CURVES + -DHAVE_ECC256 -DHAVE_ECC384 -DHAVE_ECC521 -DWOLFSSL_ECDSA_SET_K + -DWC_RSA_PSS -DWOLFSSL_KEY_GEN -DWC_RSA_NO_PADDING + -DWOLFSSL_AES_COUNTER -DHAVE_AESCCM -DHAVE_AES_ECB -DWOLFSSL_AES_DIRECT + -DWOLFSSL_AES_OFB -DHAVE_AESGCM -DGCM_TABLE_4BIT -DWOLFSSL_CMAC + -DWOLFSSL_SHA224 -DWOLFSSL_SHA512 -DWOLFSSL_SHA384 -DWOLFSSL_NO_SHAKE256 + -DWOLFSSL_NOSHA512_224 -DWOLFSSL_NOSHA512_256 -DWOLFSSL_SHA3 -DHAVE_HKDF + -DNO_OLD_TLS -DNO_PSK -DNO_DO178 + -DNO_RC4 -DNO_MD4 -DNO_MD5 -DNO_DES3 -DNO_DSA -DNO_RABBIT + + -DWOLFSSL_JNI -DHAVE_EX_DATA -DHAVE_OCSP -DHAVE_CRL_MONITOR + -DWOLFSSL_CERT_EXT -DWOLFSSL_CERT_GEN -DHAVE_SNI -DHAVE_ALPN + -DWOLFSSL_ENCRYPTED_KEYS -DNO_ERROR_QUEUE -DWOLFSSL_EITHER_SIDE + -DWOLFSSL_PSS_LONG_SALT -DWOLFSSL_TICKET_HAVE_ID + -DWOLFSSL_ERROR_CODE_OPENSSL -DWOLFSSL_EXTRA_ALERTS + -DWOLFSSL_FORCE_CACHE_ON_TICKET -DWOLFSSL_AKID_NAME -DHAVE_CTS + -DKEEP_PEER_CERT -DSESSION_CERTS + -DSIZEOF_LONG=4 -DSIZEOF_LONG_LONG=8 + + # For gethostbyname() + -DHAVE_NETDB_H + + # Enable below options for wolfSSL debug logging + #-DDEBUG_WOLFSSL -DWOLFSSL_ANDROID_DEBUG + + # Below options are added only for wolfCrypt test and benchmark + # applications. These can be left off / removed when integrating + # into a real-world application. Add -DNO_FILESYSTEM to disable + # file system use for wolfCrypt test, but make sure to remove + # this define in production applications as filesystem access + # is required for wolfJCE use. + -DUSE_CERT_BUFFERS_2048 -DUSE_CERT_BUFFERS_256 + -DNO_WRITE_TEMP_FILES -DNO_MAIN_DRIVER + ) +endif() + +if ("$WOLFSSL_MATH_LIB" MATCHES "fastmath") + # Use fastmath library + add_definitions(-DUSE_FAST_MATH -DTFM_ECC256 -DTFM_NO_ASM) + +elseif("${WOLFSSL_MATH_LIB}" MATCHES "spmath") + # Use SP math Library + add_definitions( + -DWOLFSSL_HAVE_SP_RSA -DWOLFSSL_SP_4096 + -DWOLFSSL_HAVE_SP_DH + -DWOLFSSL_HAVE_SP_ECC -DWOLFSSL_SP_384 -DWOLFSSL_SP_521 + -DWOLFSSL_SP_LARGE_CODE) + + # SP Math architecture-specific settings (ex: assembly optimizations) + if("${ANDROID_ABI}" MATCHES "arm64-v8a") + # Using ASM for SP, need to use WOLFSSL_SP_MATH instead of WOLFSSL_SP_MATH_ALL + add_definitions(-DWOLFSSL_SP_MATH) + add_definitions(-DWOLFSSL_SP_ASM -DWOLFSSL_SP_ARM64 -DWOLFSSL_SP_ARM64_ASM -DHAVE___UINT128_T) + elseif("${ANDROID_ABI}" MATCHES "armeabi-v7a") + # Add SP optimizations for ARMv7 here when available. + # Not using ASM, need to use WOLFSSL_SP_MATH_ALL for SW-only implementation + add_definitions(-DWOLFSSL_SP_MATH_ALL) + elseif("${ANDROID_ABI}" MATCHES "x86_64") + # Using ASM for SP, need to use WOLFSSL_SP_MATH instead of WOLFSSL_SP_MATH_ALL + add_definitions(-DWOLFSSL_SP_MATH) + add_definitions(-DWOLFSSL_SP_ASM -DWOLFSSL_SP_X86_64 -DWOLFSSL_SP_X86_64_ASM -DHAVE___UINT128_T) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/sp_x86_64_asm.S) + elseif("${ANDROID_ABI}" MATCHES "x86") + # Add SP optimizations for X86 here when available. + # Not using ASM, need to use WOLFSSL_SP_MATH_ALL for SW-only implementation + add_definitions(-DWOLFSSL_SP_MATH_ALL) + else() + # Not using ASM, need to use WOLFSSL_SP_MATH_ALL for SW-only implementation + add_definitions(-DWOLFSSL_SP_MATH_ALL) + endif() +endif() + +# --------------------------- wolfSSL and wolfCrypt Source Files ----------------------------------- +# Add TLS sources to TLS_SOURCES list and remove files that are included inline by other files +aux_source_directory(${wolfssl_DIR}/src TLS_SOURCES) +list(REMOVE_ITEM TLS_SOURCES ${wolfssl_DIR}/src/bio.c) +list(REMOVE_ITEM TLS_SOURCES ${wolfssl_DIR}/src/conf.c) +list(REMOVE_ITEM TLS_SOURCES ${wolfssl_DIR}/src/pk.c) +list(REMOVE_ITEM TLS_SOURCES ${wolfssl_DIR}/src/ssl_bn.c) +list(REMOVE_ITEM TLS_SOURCES ${wolfssl_DIR}/src/ssl_asn1.c) +list(REMOVE_ITEM TLS_SOURCES ${wolfssl_DIR}/src/ssl_certman.c) +list(REMOVE_ITEM TLS_SOURCES ${wolfssl_DIR}/src/ssl_crypto.c) +list(REMOVE_ITEM TLS_SOURCES ${wolfssl_DIR}/src/ssl_load.c) +list(REMOVE_ITEM TLS_SOURCES ${wolfssl_DIR}/src/ssl_misc.c) +list(REMOVE_ITEM TLS_SOURCES ${wolfssl_DIR}/src/ssl_p7p12.c) +list(REMOVE_ITEM TLS_SOURCES ${wolfssl_DIR}/src/ssl_sess.c) +list(REMOVE_ITEM TLS_SOURCES ${wolfssl_DIR}/src/x509.c) +list(REMOVE_ITEM TLS_SOURCES ${wolfssl_DIR}/src/x509_str.c) + +if ("${WOLFSSL_PKG_TYPE}" MATCHES "normal") + # Add crypto sources to CRYPTO_SOURCES, remove files that are included inline by other files + aux_source_directory(${wolfssl_DIR}/wolfcrypt/src CRYPTO_SOURCES) + list(REMOVE_ITEM CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/evp.c) + list(REMOVE_ITEM CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/misc.c) + +elseif("${WOLFSSL_PKG_TYPE}" MATCHES "fipsready") + # FIPS Ready needs to explicitly order files for in-core integrity check to work properly. + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/wolfcrypt_first.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/hmac.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/random.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/sha256.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/kdf.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/rsa.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/ecc.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/aes.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/sha.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/sha512.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/sha3.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/dh.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/cmac.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/fips.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/fips_test.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/wolfcrypt_last.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/hash.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/cpuid.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/logging.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/wc_port.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/error.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/wc_encrypt.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/signature.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/sp_arm32.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/sp_arm64.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/sp_armthumb.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/sp_c32.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/sp_c64.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/sp_cortexm.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/sp_dsp32.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/sp_int.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/sp_x86_64.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/wolfmath.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/memory.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/asn.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/coding.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/pwdbased.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/pkcs12.c) + list(APPEND CRYPTO_SOURCES ${wolfssl_DIR}/wolfcrypt/src/tfm.c) +endif() + +# wolfSSL will be compiled as a SHARED library +add_library(wolfssl SHARED + ${CRYPTO_SOURCES} + ${TLS_SOURCES} +) + +# set_target_properties(wolfssl PROPERTIES LIBRARY_OUTPUT_DIRECTORY +# ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}) + +# wolfCrypt JNI/JCE library wil be compiled as SHARED library +# wolfCrypt JNI Java files are tied into build in Module build.gradle file +add_library(wolfcryptjni SHARED + ${wolfcryptjni_DIR}/jni/jni_aes.c + ${wolfcryptjni_DIR}/jni/jni_aesgcm.c + ${wolfcryptjni_DIR}/jni/jni_asn.c + ${wolfcryptjni_DIR}/jni/jni_chacha.c + ${wolfcryptjni_DIR}/jni/jni_curve25519.c + ${wolfcryptjni_DIR}/jni/jni_des3.c + ${wolfcryptjni_DIR}/jni/jni_dh.c + ${wolfcryptjni_DIR}/jni/jni_ecc.c + ${wolfcryptjni_DIR}/jni/jni_ed25519.c + ${wolfcryptjni_DIR}/jni/jni_error.c + ${wolfcryptjni_DIR}/jni/jni_feature_detect.c + ${wolfcryptjni_DIR}/jni/jni_fips.c + ${wolfcryptjni_DIR}/jni/jni_hmac.c + ${wolfcryptjni_DIR}/jni/jni_jce_wolfsslkeystore.c + ${wolfcryptjni_DIR}/jni/jni_logging.c + ${wolfcryptjni_DIR}/jni/jni_md5.c + ${wolfcryptjni_DIR}/jni/jni_native_struct.c + ${wolfcryptjni_DIR}/jni/jni_pwdbased.c + ${wolfcryptjni_DIR}/jni/jni_rng.c + ${wolfcryptjni_DIR}/jni/jni_rsa.c + ${wolfcryptjni_DIR}/jni/jni_sha.c + ${wolfcryptjni_DIR}/jni/jni_wolfcrypt.c + ${wolfcryptjni_DIR}/jni/jni_wolfobject.c + ${wolfcryptjni_DIR}/jni/jni_wolfssl_cert_manager.c +) + +# set_target_properties(wolfcryptjni PROPERTIES LIBRARY_OUTPUT_DIRECTORY +# ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}) + +# Find the android log library, store into variable ${lib-log} +find_library(lib-log log) + +# Link libwolfssl to android log library +target_link_libraries( + wolfssl + ${lib-log} +) + +# Link libwolfcryptjni to libwolfssl +target_link_libraries( + wolfcryptjni + wolfssl +) diff --git a/IDE/Android/app/src/main/java/com/example/wolfssl/MainActivity.java b/IDE/Android/app/src/main/java/com/example/wolfssl/MainActivity.java new file mode 100644 index 0000000..41403f4 --- /dev/null +++ b/IDE/Android/app/src/main/java/com/example/wolfssl/MainActivity.java @@ -0,0 +1,95 @@ +/* MainActivity.java + * + * Copyright (C) 2006-2024 wolfSSL Inc. + * + * This file is part of wolfSSL. + * + * wolfSSL is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * wolfSSL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA + */ + + +package com.example.wolfssl; + +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.Provider; +import java.security.Security; + +import com.wolfssl.provider.jce.WolfCryptProvider; + +public class MainActivity extends AppCompatActivity { + + private View.OnClickListener buttonListener = new View.OnClickListener() { + @Override + public void onClick(View v) { + TextView tv = (TextView) findViewById(R.id.sample_text); + + try { + testFindProvider(tv); + } catch (Exception e) { + e.printStackTrace(); + } + } + }; + + private void setDisplayText(String s) + { + runOnUiThread(() -> { + TextView tv = (TextView) findViewById(R.id.sample_text); + tv.setText(s); + }); + } + + private void appendDisplayText(String s) + { + runOnUiThread(() -> { + TextView tv = (TextView) findViewById(R.id.sample_text); + tv.append(s); + }); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + Button button = (Button) findViewById(R.id.button); + button.setOnClickListener(buttonListener); + + setDisplayText("wolfCrypt JNI/JCE Android Studio Example app\n"); + } + + public void testFindProvider(TextView tv) + throws NoSuchProviderException, NoSuchAlgorithmException { + + Security.addProvider(new WolfCryptProvider()); + + Provider p = Security.getProvider("wolfJCE"); + if (p == null) { + appendDisplayText("Unable to find wolfJCE provider\n"); + return; + } + else { + appendDisplayText("Successfully found wolfJCE provider\n"); + return; + } + } +} diff --git a/IDE/Android/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/IDE/Android/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..1f6bb29 --- /dev/null +++ b/IDE/Android/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/IDE/Android/app/src/main/res/drawable/ic_launcher_background.xml b/IDE/Android/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..0d025f9 --- /dev/null +++ b/IDE/Android/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/IDE/Android/app/src/main/res/layout/activity_main.xml b/IDE/Android/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..e9e3b41 --- /dev/null +++ b/IDE/Android/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,34 @@ + + + +