Android: add wolfCrypt test and benchmark + SP math to sample app

pull/322/head
Chris Conlon 2022-06-15 11:25:02 -06:00
parent 18130fb268
commit 5f5d97698c
7 changed files with 512 additions and 113 deletions

View File

@ -5,20 +5,20 @@ wolfCrypt JNI/JCE and wolfSSL JNI/JSSE can be integrated into an
Android Studio project using the Gradle build system to compile both
shared libraries and the application.
[wolfcryptjni-ndk-gradle](./wolfcryptjni-ndk-gradle)
1) [wolfcryptjni-ndk-gradle](./wolfcryptjni-ndk-gradle)
[wolfssljni-ndk-gradle](./wolfssljni-ndk-gradle)
2) [wolfssljni-ndk-gradle](./wolfssljni-ndk-gradle)
This directory also contains one older example of how wolfSSL JNI/JSSE
could be integrated into an older standalone NDK toolchain project's Android.mk
build file. The above Android Studio projects are the recommended approach
unless using an older standalone NDK toolchain setup.
[wolfssljni-ndk-sample](./wolfssljni-ndk-sample)
3) [wolfssljni-ndk-sample](./wolfssljni-ndk-sample)
wolfSSL also maintains an Android Studio example project that is shipped
as part of the wolfSSL JNI/JSSE bundle. This example project is set up to
run the wolfSSL JNI/JSSE tests on an emulator as well, and is located here:
run the wolfSSL JNI/JSSE tests on an emulator and is located here:
[wolfSSL JNI/JSSE IDE/Android Example](https://github.com/wolfSSL/wolfssljni/tree/master/IDE/Android)
@ -40,7 +40,7 @@ wolfCrypt JNI/JCE + wolfSSL bundle on the Emulator:
2) Checkout wolfssl and wolfssljni git submodules
3) Create stub options.h (since we're using the GitHub repo for wolfSSL)
3) Create stub options.h (when using GitHub repo for wolfSSL)
4) Open "wolfssljni-ndk-gradle" project in Android Studio and build project
@ -71,9 +71,9 @@ $ git checkout vX.X.X-stable
```
To install and run the application in an Android emulator, set one up
in Android Studio, then click the Debug application button on the Android
Studio toolbar. This will allow you to choose what Android virtual machine
you would like to use.
in the Android Studio Device Manager, then click the Debug application button
on the Android Studio toolbar. This will allow you to choose what Android
virtual machine you would like to use.
Logcat output from the emulator can be viewed by opening a new terminal window
while the emulator is running and issuing:
@ -82,6 +82,169 @@ while the emulator is running and issuing:
$ adb logcat
```
### Sample Application Functionality
#### Native wolfCrypt Test Application
The sample application has a button that runs the native wolfCrypt tests. This
wraps and runs the wolfCrypt test from wolfSSL (./wolfcrypt/test/test.c). Ouput
will be printed to the logcat log.
After clicking this button, wolfCrypt tests for all enabled algorithms will
run and print the test status to the logcat log:
```
[WOLFCRYPT]: ------------------------------------------------------------------------------
[WOLFCRYPT]: wolfSSL version 5.3.0
[WOLFCRYPT]: ------------------------------------------------------------------------------
[WOLFCRYPT]: error test passed!
[WOLFCRYPT]: MEMORY test passed!
[WOLFCRYPT]: base64 test passed!
[WOLFCRYPT]: base16 test passed!
[WOLFCRYPT]: asn test passed!
[WOLFCRYPT]: RANDOM test passed!
[WOLFCRYPT]: MD5 test passed!
[WOLFCRYPT]: SHA test passed!
[WOLFCRYPT]: SHA-224 test passed!
[WOLFCRYPT]: SHA-256 test passed!
[WOLFCRYPT]: SHA-384 test passed!
[WOLFCRYPT]: SHA-512 test passed!
[WOLFCRYPT]: SHA-3 test passed!
[WOLFCRYPT]: Hash test passed!
[WOLFCRYPT]: HMAC-MD5 test passed!
[WOLFCRYPT]: HMAC-SHA test passed!
[WOLFCRYPT]: HMAC-SHA224 test passed!
[WOLFCRYPT]: HMAC-SHA256 test passed!
[WOLFCRYPT]: HMAC-SHA384 test passed!
[WOLFCRYPT]: HMAC-SHA512 test passed!
[WOLFCRYPT]: HMAC-SHA3 test passed!
[WOLFCRYPT]: HMAC-KDF test passed!
[WOLFCRYPT]: TLSv1.3 KDF test passed!
[WOLFCRYPT]: GMAC test passed!
[WOLFCRYPT]: Chacha test passed!
[WOLFCRYPT]: POLY1305 test passed!
[WOLFCRYPT]: ChaCha20-Poly1305 AEAD test passed!
[WOLFCRYPT]: AES test passed!
[WOLFCRYPT]: AES192 test passed!
[WOLFCRYPT]: AES256 test passed!
[WOLFCRYPT]: AES-GCM test passed!
[WOLFCRYPT]: RSA NOPAD test passed!
[WOLFCRYPT]: RSA test passed!
[WOLFCRYPT]: DH test passed!
[WOLFCRYPT]: PWDBASED test passed!
[WOLFCRYPT]: OPENSSL test passed!
[WOLFCRYPT]: OPENSSL (EVP MD) passed!
[WOLFCRYPT]: OPENSSL (PKEY0) passed!
[WOLFCRYPT]: OPENSSL (PKEY1) passed!
[WOLFCRYPT]: OPENSSL (EVP Sign/Verify) passed!
[WOLFCRYPT]: ECC test passed!
[WOLFCRYPT]: ECC buffer test passed!
[WOLFCRYPT]: logging test passed!
[WOLFCRYPT]: time test passed!
[WOLFCRYPT]: mutex test passed!
[WOLFCRYPT]: memcb test passed!
[WOLFCRYPT]: Test complete
```
#### Native wolfCrypt Benchmark Application
The sample application has a button that runs the native wolfCrypt benchmarks.
This wraps the wolfCrypt benchmark from wolfSSL
(./wolfcrypt/benchmark/benchmark.c). Output will be printed to the logcat log.
After clicking on this button, wolfCrypt benchmarks will be run for all
enabled algorithms and be printed to the logcat log.
The following is example output when run on a Pixel 5 API 31
(Android 12, Google APIs) x86\_64 emulator with SP math enabled:
```
[WOLFCRYPT]: wolfCrypt Benchmark (block bytes 1048576, min 1.0 sec each)
[WOLFCRYPT]: RNG 15 MB took 1.149 seconds, 13.052 MB/s Cycles per byte = 218.79
[WOLFCRYPT]: AES-128-CBC-enc 40 MB took 1.009 seconds, 39.660 MB/s Cycles per byte = 72.00
[WOLFCRYPT]: AES-128-CBC-dec 40 MB took 1.056 seconds, 37.864 MB/s Cycles per byte = 75.42
[WOLFCRYPT]: AES-192-CBC-enc 40 MB took 1.032 seconds, 38.777 MB/s Cycles per byte = 73.64
[WOLFCRYPT]: AES-192-CBC-dec 40 MB took 1.076 seconds, 37.162 MB/s Cycles per byte = 76.84
[WOLFCRYPT]: AES-256-CBC-enc 40 MB took 1.092 seconds, 36.617 MB/s Cycles per byte = 77.99
[WOLFCRYPT]: AES-256-CBC-dec 40 MB took 1.121 seconds, 35.697 MB/s Cycles per byte = 80.00
[WOLFCRYPT]: AES-128-GCM-enc 30 MB took 1.172 seconds, 25.607 MB/s Cycles per byte = 111.52
[WOLFCRYPT]: AES-128-GCM-dec 30 MB took 1.169 seconds, 25.656 MB/s Cycles per byte = 111.31
[WOLFCRYPT]: AES-192-GCM-enc 25 MB took 1.214 seconds, 20.589 MB/s Cycles per byte = 138.70
[WOLFCRYPT]: AES-192-GCM-dec 25 MB took 1.008 seconds, 24.803 MB/s Cycles per byte = 115.13
[WOLFCRYPT]: AES-256-GCM-enc 25 MB took 1.059 seconds, 23.601 MB/s Cycles per byte = 121.00
[WOLFCRYPT]: AES-256-GCM-dec 25 MB took 1.045 seconds, 23.935 MB/s Cycles per byte = 119.31
[WOLFCRYPT]: GMAC Table 4-bit 69 MB took 1.000 seconds, 68.992 MB/s Cycles per byte = 41.39
[WOLFCRYPT]: CHACHA 35 MB took 1.031 seconds, 33.954 MB/s Cycles per byte = 84.10
[WOLFCRYPT]: CHA-POLY 35 MB took 1.068 seconds, 32.778 MB/s Cycles per byte = 87.12
[WOLFCRYPT]: MD5 135 MB took 1.018 seconds, 132.625 MB/s Cycles per byte = 21.53
[WOLFCRYPT]: POLY1305 395 MB took 1.001 seconds, 394.472 MB/s Cycles per byte = 7.24
[WOLFCRYPT]: SHA 75 MB took 1.065 seconds, 70.436 MB/s Cycles per byte = 40.54
[WOLFCRYPT]: SHA-224 30 MB took 1.010 seconds, 29.705 MB/s Cycles per byte = 96.13
[WOLFCRYPT]: SHA-256 35 MB took 1.162 seconds, 30.129 MB/s Cycles per byte = 94.78
[WOLFCRYPT]: SHA-384 45 MB took 1.051 seconds, 42.811 MB/s Cycles per byte = 66.70
[WOLFCRYPT]: SHA-512 45 MB took 1.053 seconds, 42.745 MB/s Cycles per byte = 66.81
[WOLFCRYPT]: SHA3-224 85 MB took 1.063 seconds, 80.000 MB/s Cycles per byte = 35.70
[WOLFCRYPT]: SHA3-256 80 MB took 1.046 seconds, 76.462 MB/s Cycles per byte = 37.35
[WOLFCRYPT]: SHA3-384 60 MB took 1.002 seconds, 59.866 MB/s Cycles per byte = 47.70
[WOLFCRYPT]: SHA3-512 45 MB took 1.074 seconds, 41.909 MB/s Cycles per byte = 68.14
[WOLFCRYPT]: HMAC-MD5 135 MB took 1.012 seconds, 133.384 MB/s Cycles per byte = 21.41
[WOLFCRYPT]: HMAC-SHA 75 MB took 1.058 seconds, 70.917 MB/s Cycles per byte = 40.27
[WOLFCRYPT]: HMAC-SHA224 35 MB took 1.136 seconds, 30.822 MB/s Cycles per byte = 92.65
[WOLFCRYPT]: HMAC-SHA256 30 MB took 1.004 seconds, 29.888 MB/s Cycles per byte = 95.54
[WOLFCRYPT]: HMAC-SHA384 45 MB took 1.062 seconds, 42.370 MB/s Cycles per byte = 67.40
[WOLFCRYPT]: HMAC-SHA512 45 MB took 1.069 seconds, 42.093 MB/s Cycles per byte = 67.84
[WOLFCRYPT]: PBKDF2 4 KB took 1.008 seconds, 3.598 KB/s Cycles per byte = 812776.94
[WOLFCRYPT]: RSA 2048 public 18700 ops took 1.000 sec, avg 0.053 ms, 18693.849 ops/sec
[WOLFCRYPT]: RSA 2048 private 900 ops took 1.001 sec, avg 1.112 ms, 899.441 ops/sec
[WOLFCRYPT]: DH 2048 key gen 1761 ops took 1.000 sec, avg 0.568 ms, 1760.757 ops/sec
[WOLFCRYPT]: DH 2048 agree 1800 ops took 1.004 sec, avg 0.558 ms, 1792.190 ops/sec
[WOLFCRYPT]: ECC [ SECP256R1] 256 key gen 33100 ops took 1.003 sec, avg 0.030 ms, 33004.652 ops/sec
[WOLFCRYPT]: ECDHE [ SECP256R1] 256 agree 12200 ops took 1.001 sec, avg 0.082 ms, 12185.694 ops/sec
[WOLFCRYPT]: ECDSA [ SECP256R1] 256 sign 21400 ops took 1.001 sec, avg 0.047 ms, 21373.222 ops/sec
[WOLFCRYPT]: ECDSA [ SECP256R1] 256 verify 11300 ops took 1.006 sec, avg 0.089 ms, 11237.251 ops/sec
[WOLFCRYPT]: Benchmark complete
```
This can be a good tool when optimizing wolfSSL and wolfCrypt for performance.
#### Simple SSLSocket Connection
The sample application has a button that makes a simple SSL/TLS connection
to `wolfssl.com:443` using the SSLSocket class. It does not explicitly load
CA certificates from a KeyStore, since wolfJSSE automatically loads the
Android KeyStore root certificates as trusted, and those are able to
authenticate wolfssl.com.
After clicking on this button, wolfJSSE debug output will be printed to the
logcat log.
#### How to Change wolfSSL Library Configuration
This sample Android application builds native wolfSSL and wolfCrypt sources
into a shared library. That build is done with cmake and controlled by the
CMakeLists.txt file located at:
android/wolfssljni-ndk-gradle/app/CMakeLists.txt
wolfSSL CFLAGS are defined using the `add_definition()` function, and have
been pre-populated in this sample to match those defined when building wolfSSL
with `./configure --enable-jni`.
This CMakeLists.txt has been designed to allow easy switching between wolfSSL's
fastmath math library and the newer SP math library. SP math can offer
performance advantages over the fastmath library, especially when assembly
optimizations are available for a given platform.
To control the math library used, change the variable `WOLFSSL_MATH_LIB`
in CMakeLists.txt:
```
# Math library selection, used to switch on below. Should be one of:
# fastmath
# spmath
set(WOLFSSL_MATH_LIB "spmath")
```
## wolfSSL NDK Standalone Toolchain Example
### Prerequisites for successful installation

View File

@ -2,16 +2,44 @@
# 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)
project("wolfssljni-ndk-gradle")
# Define project as both C and ASM for cases for SP has assembly enabled
project("wolfssljni-ndk-gradle" C ASM)
# set wolfSSL JNI location as environment variable, change if needed
set(wolfssljni_DIR ${CMAKE_SOURCE_DIR}/../wolfssljni)
set(wolfssl_DIR ${CMAKE_SOURCE_DIR}/../wolfssl)
# 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}
${wolfssljni_DIR}/native
)
# 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/x509.c)
list(REMOVE_ITEM TLS_SOURCES ${wolfssl_DIR}/src/x509_str.c)
# Add crypto sources to CRYPTO_SOURCES list and 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)
# 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.
add_definitions(-DUSE_FAST_MATH -DTFM_TIMING_RESISTANT -DECC_TIMING_RESISTANT
-DWC_RSA_BLINDING -DWOLFSSL_SHA224 -DWOLFSSL_SHA384
-DWOLFSSL_SHA512 -DHAVE_HKDF -DNO_DSA -DHAVE_ECC -DTFM_ECC256
# 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
@ -28,94 +56,101 @@ add_definitions(-DUSE_FAST_MATH -DTFM_TIMING_RESISTANT -DECC_TIMING_RESISTANT
-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
-DHAVE_AESGCM -DSIZEOF_LONG=4 -DSIZEOF_LONG_LONG=8 -DTFM_NO_ASM)
-DTFM_TIMING_RESISTANT -DECC_TIMING_RESISTANT
-DHAVE_AESGCM -DSIZEOF_LONG=4 -DSIZEOF_LONG_LONG=8
# set wolfSSL JNI location as environment variable, change if needed
set(wolfssljni_DIR ${CMAKE_SOURCE_DIR}/../wolfssljni)
set(wolfssl_DIR ${CMAKE_SOURCE_DIR}/../wolfssl)
# Defines added for wolfCrypt test and benchmark only, may not be needed for your
# own application.
-DUSE_CERT_BUFFERS_2048 -DUSE_CERT_BUFFERS_256 -DNO_WRITE_TEMP_FILES
-DNO_FILESYSTEM -DNO_MAIN_DRIVER
# Add header directories to include paths
include_directories(
${wolfssl_DIR}
${wolfssljni_DIR}/native
)
# 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.
-DEBUG_WOLFSSL)
# Add wolfSSL library source files, to be compiled as SHARED library
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_SP_MATH -DWOLFSSL_SP_MATH_ALL
-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")
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.
elseif("${ANDROID_ABI}" MATCHES "x86_64")
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.
endif()
endif()
# wolfSSL will be compiled as a SHARED library
add_library(wolfssl SHARED
${wolfssl_DIR}/wolfcrypt/src/aes.c
${wolfssl_DIR}/wolfcrypt/src/arc4.c
${wolfssl_DIR}/wolfcrypt/src/asm.c
${wolfssl_DIR}/wolfcrypt/src/asn.c
${wolfssl_DIR}/wolfcrypt/src/blake2b.c
${wolfssl_DIR}/wolfcrypt/src/camellia.c
${wolfssl_DIR}/wolfcrypt/src/chacha.c
${wolfssl_DIR}/wolfcrypt/src/chacha20_poly1305.c
${wolfssl_DIR}/wolfcrypt/src/cmac.c
${wolfssl_DIR}/wolfcrypt/src/coding.c
${wolfssl_DIR}/wolfcrypt/src/compress.c
${wolfssl_DIR}/wolfcrypt/src/cpuid.c
${wolfssl_DIR}/wolfcrypt/src/curve25519.c
${wolfssl_DIR}/wolfcrypt/src/des3.c
${wolfssl_DIR}/wolfcrypt/src/dh.c
${wolfssl_DIR}/wolfcrypt/src/dsa.c
${wolfssl_DIR}/wolfcrypt/src/ecc.c
${wolfssl_DIR}/wolfcrypt/src/ecc_fp.c
${wolfssl_DIR}/wolfcrypt/src/ed25519.c
${wolfssl_DIR}/wolfcrypt/src/error.c
${wolfssl_DIR}/wolfcrypt/src/fe_low_mem.c
${wolfssl_DIR}/wolfcrypt/src/fe_operations.c
${wolfssl_DIR}/wolfcrypt/src/ge_low_mem.c
${wolfssl_DIR}/wolfcrypt/src/ge_operations.c
${wolfssl_DIR}/wolfcrypt/src/hash.c
${wolfssl_DIR}/wolfcrypt/src/hmac.c
${wolfssl_DIR}/wolfcrypt/src/integer.c
${wolfssl_DIR}/wolfcrypt/src/kdf.c
${wolfssl_DIR}/wolfcrypt/src/logging.c
${wolfssl_DIR}/wolfcrypt/src/md2.c
${wolfssl_DIR}/wolfcrypt/src/md4.c
${wolfssl_DIR}/wolfcrypt/src/md5.c
${wolfssl_DIR}/wolfcrypt/src/memory.c
${wolfssl_DIR}/wolfcrypt/src/pkcs12.c
${wolfssl_DIR}/wolfcrypt/src/pkcs7.c
${wolfssl_DIR}/wolfcrypt/src/poly1305.c
${wolfssl_DIR}/wolfcrypt/src/pwdbased.c
${wolfssl_DIR}/wolfcrypt/src/random.c
${wolfssl_DIR}/wolfcrypt/src/ripemd.c
${wolfssl_DIR}/wolfcrypt/src/rsa.c
${wolfssl_DIR}/wolfcrypt/src/sha.c
${wolfssl_DIR}/wolfcrypt/src/sha256.c
${wolfssl_DIR}/wolfcrypt/src/sha3.c
${wolfssl_DIR}/wolfcrypt/src/sha512.c
${wolfssl_DIR}/wolfcrypt/src/signature.c
${wolfssl_DIR}/wolfcrypt/src/srp.c
${wolfssl_DIR}/wolfcrypt/src/tfm.c
${wolfssl_DIR}/wolfcrypt/src/wc_encrypt.c
${wolfssl_DIR}/wolfcrypt/src/wc_port.c
${wolfssl_DIR}/wolfcrypt/src/wolfevent.c
${wolfssl_DIR}/wolfcrypt/src/wolfmath.c
${wolfssl_DIR}/src/crl.c
${wolfssl_DIR}/src/internal.c
${wolfssl_DIR}/src/wolfio.c
${wolfssl_DIR}/src/keys.c
${wolfssl_DIR}/src/ocsp.c
${wolfssl_DIR}/src/sniffer.c
${wolfssl_DIR}/src/ssl.c
${wolfssl_DIR}/src/tls.c
${wolfssl_DIR}/src/tls13.c
${CRYPTO_SOURCES}
${TLS_SOURCES}
)
# Add wolfSSL JNI library native source files, to be compiled as SHARED library
# wolfSSL JNI/JSSE library wil be compiled as SHARED library
# wolfSSL JNI Java files are tied into build in Module build.gradle file
add_library(wolfssljni SHARED
${wolfssljni_DIR}/native/com_wolfssl_wolfcrypt_ECC.c
${wolfssljni_DIR}/native/com_wolfssl_wolfcrypt_EccKey.c
${wolfssljni_DIR}/native/com_wolfssl_wolfcrypt_RSA.c
${wolfssljni_DIR}/native/com_wolfssl_WolfSSL.c
${wolfssljni_DIR}/native/com_wolfssl_WolfSSLCertificate.c
${wolfssljni_DIR}/native/com_wolfssl_WolfSSLCertManager.c
${wolfssljni_DIR}/native/com_wolfssl_WolfSSLContext.c
${wolfssljni_DIR}/native/com_wolfssl_WolfSSLSession.c
${wolfssljni_DIR}/native/com_wolfssl_WolfSSLX509StoreCtx.c
)
# Include libraries needed for wolfSSL and wolfSSL JNI libs
target_link_libraries(wolfssljni
wolfssl
android
log)
# Native library to hold sources for wolfCrypt test application. These include the
# wolfCrypt test and benchmark applications from within the wolfSSL proper source directory.
add_library(jnisample SHARED
${CMAKE_SOURCE_DIR}/src/main/cpp/NativeHelper.c
${wolfssl_DIR}/wolfcrypt/test/test.c
${wolfssl_DIR}/wolfcrypt/benchmark/benchmark.c
)
# 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 libjnisample to android log library
target_link_libraries(
jnisample
${lib-log}
)
# Link libjnisample to libwolfssl
target_link_libraries(
jnisample
wolfssl
)
# Link libwolfssljni to libwolfssl
target_link_libraries(
wolfssljni
wolfssl
)
# Link libjnisample to libwolfssljni and libwolfssl
target_link_libraries(
jnisample
wolfssljni
wolfssl
)

View File

@ -1,11 +1,11 @@
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
compileSdkVersion 30
defaultConfig {
applicationId "com.wolfssl.wolfssl_ndk_gradle"
minSdkVersion 23
targetSdkVersion 28
minSdkVersion 30
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@ -36,6 +36,7 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:design:28.0.0'
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})

View File

@ -1,11 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:label="wolfSSL JNI/JSSE Sample App"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"

View File

@ -0,0 +1,51 @@
/* NativeHelper.c
*
* Copyright (C) 2006-2022 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-1301, USA
*/
#include <jni.h>
#include <wolfssl/ssl.h>
#include <wolfcrypt/test/test.h>
#include <wolfcrypt/benchmark/benchmark.h>
JNIEXPORT jint JNICALL
Java_com_wolfssl_wolfssljni_1ndk_1gradle_MainActivity_testWolfCrypt(JNIEnv *env, jobject thiz) {
int ret;
ret = wolfCrypt_Init();
if (ret == 0) {
ret = wolfcrypt_test(NULL);
}
wolfCrypt_Cleanup();
return ret;
}
JNIEXPORT jint JNICALL
Java_com_wolfssl_wolfssljni_1ndk_1gradle_MainActivity_benchWolfCrypt(JNIEnv *env, jobject thiz) {
int ret = 0;
ret = wolfCrypt_Init();
if (ret == 0) {
ret = benchmark_test(NULL);
}
wolfCrypt_Cleanup();
return ret;
}

View File

@ -1,32 +1,144 @@
package com.wolfssl.wolfssljni_ndk_gradle;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.wolfssl.WolfSSL;
import com.wolfssl.provider.jsse.WolfSSLProvider;
import java.security.Security;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class MainActivity extends AppCompatActivity {
public native int testWolfCrypt();
public native int benchWolfCrypt();
private static String host = "www.wolfssl.com";
private static int port = 443;
private View.OnClickListener wcTestListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv = (TextView) findViewById(R.id.displayText);
tv.setText("Running wolfCrypt tests.\n" +
"See logcat output for details (adb logcat).\n" +
"...\n");
AsyncTask.execute(new Runnable() {
@Override
public void run() {
int ret = testWolfCrypt();
if (ret == 0) {
tv.append("TESTS PASSED\n");
} else {
tv.append("TESTS FAILED!\nInspect logcat.");
}
tv.append("\n");
tv.append("To adjust wolfSSL configuration, please edit\n" +
"add_definitions() in CMakeLists.txt.");
}
});
}
};
private View.OnClickListener wcBenchmarkListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv = (TextView) findViewById(R.id.displayText);
tv.setText("Running wolfCrypt benchmark.\n" +
"See logcat output for details (adb logcat).\n" +
"...\n");
AsyncTask.execute(new Runnable() {
@Override
public void run() {
int ret = benchWolfCrypt();
if (ret == 0) {
tv.append("BENCHMARK PASSED\n");
} else {
tv.append("BENCHMARK FAILED!\nInspect logcat.");
}
tv.append("\n");
tv.append("To adjust wolfSSL configuration, please edit\n" +
"add_definitions() in CMakeLists.txt.");
}
});
}
};
private View.OnClickListener sslSocketConnectListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView tv = (TextView) findViewById(R.id.displayText);
tv.setText("Making simple SSL/TLS connection to:\n" +
"www.wolfssl.com:443\n\n" +
"See logcat output for details (adb logcat).\n" +
"...\n");
AsyncTask.execute(new Runnable() {
@Override
public void run() {
try {
/* Enable wolfJSSE debug messages */
System.setProperty("wolfjsse.debug", "true");
Security.insertProviderAt(new WolfSSLProvider(), 1);
/* not setting up KeyStore or TrustStore, wolfJSSE will load
* CA certs from the Android system KeyStore by default. */
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, null, null);
SSLSocketFactory sf = ctx.getSocketFactory();
SSLSocket sock = (SSLSocket)sf.createSocket(host, port);
sock.startHandshake();
sock.close();
tv.append("TLS Connection Successful!");
} catch (Exception e) {
tv.append("TLS Connection Failed");
e.printStackTrace();
}
}
});
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText("wolfSSL JNI Android Studio Example App");
TextView tv = (TextView) findViewById(R.id.displayText);
tv.setText("Please make a selection below");
/* calling an API here to make sure library is linked */
try {
WolfSSL wolflib = new WolfSSL();
tv.append("\r\n... initialized WolfSSL object");
} catch (Exception e) {
e.printStackTrace();
}
Button wcTestButton = (Button) findViewById(R.id.btn_wc_test);
wcTestButton.setOnClickListener(wcTestListener);
Button wcBenchButton = (Button) findViewById(R.id.btn_wc_bench);
wcBenchButton.setOnClickListener(wcBenchmarkListener);
Button sslSocketConnectButton = (Button) findViewById(R.id.btn_sslsocket_connect);
sslSocketConnectButton.setOnClickListener(sslSocketConnectListener);
}
static {
System.loadLibrary("wolfssl");
System.loadLibrary("wolfssljni");
System.loadLibrary("jnisample");
}
}

View File

@ -6,14 +6,50 @@
android:layout_height="match_parent"
tools:context="com.wolfssl.wolfssljni_ndk_gradle.MainActivity">
<TextView
android:id="@+id/sample_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/displayText"
android:layout_width="match_parent"
android:layout_height="532dp"
android:layout_marginTop="10dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_wc_test"
android:layout_width="211dp"
android:layout_height="wrap_content"
android:text="wolfCrypt Test" />
<Button
android:id="@+id/btn_wc_bench"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="wolfCrypt Bench" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/btn_sslsocket_connect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="SSLSocket Connect (wolfssl.com)" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>