Skip to content

Commit 279b2d4

Browse files
Merge pull request #322 from cconlon/androidSampleExpansion
Android: expand Android Studio sample app with wolfCrypt test and benchmark, SP options
2 parents f4f6a42 + ef893b5 commit 279b2d4

13 files changed

Lines changed: 518 additions & 119 deletions

File tree

android/README.md

Lines changed: 171 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ wolfCrypt JNI/JCE and wolfSSL JNI/JSSE can be integrated into an
55
Android Studio project using the Gradle build system to compile both
66
shared libraries and the application.
77

8-
[wolfcryptjni-ndk-gradle](./wolfcryptjni-ndk-gradle)
8+
1) [wolfcryptjni-ndk-gradle](./wolfcryptjni-ndk-gradle)
99

10-
[wolfssljni-ndk-gradle](./wolfssljni-ndk-gradle)
10+
2) [wolfssljni-ndk-gradle](./wolfssljni-ndk-gradle)
1111

1212
This directory also contains one older example of how wolfSSL JNI/JSSE
1313
could be integrated into an older standalone NDK toolchain project's Android.mk
1414
build file. The above Android Studio projects are the recommended approach
1515
unless using an older standalone NDK toolchain setup.
1616

17-
[wolfssljni-ndk-sample](./wolfssljni-ndk-sample)
17+
3) [wolfssljni-ndk-sample](./wolfssljni-ndk-sample)
1818

1919
wolfSSL also maintains an Android Studio example project that is shipped
2020
as part of the wolfSSL JNI/JSSE bundle. This example project is set up to
21-
run the wolfSSL JNI/JSSE tests on an emulator as well, and is located here:
21+
run the wolfSSL JNI/JSSE tests on an emulator and is located here:
2222

2323
[wolfSSL JNI/JSSE IDE/Android Example](https://github.com/wolfSSL/wolfssljni/tree/master/IDE/Android)
2424

@@ -40,7 +40,7 @@ wolfCrypt JNI/JCE + wolfSSL bundle on the Emulator:
4040

4141
2) Checkout wolfssl and wolfssljni git submodules
4242

43-
3) Create stub options.h (since we're using the GitHub repo for wolfSSL)
43+
3) Create stub options.h (when using GitHub repo for wolfSSL)
4444

4545
4) Open "wolfssljni-ndk-gradle" project in Android Studio and build project
4646

@@ -71,9 +71,9 @@ $ git checkout vX.X.X-stable
7171
```
7272

7373
To install and run the application in an Android emulator, set one up
74-
in Android Studio, then click the Debug application button on the Android
75-
Studio toolbar. This will allow you to choose what Android virtual machine
76-
you would like to use.
74+
in the Android Studio Device Manager, then click the Debug application button
75+
on the Android Studio toolbar. This will allow you to choose what Android
76+
virtual machine you would like to use.
7777

7878
Logcat output from the emulator can be viewed by opening a new terminal window
7979
while the emulator is running and issuing:
@@ -82,6 +82,169 @@ while the emulator is running and issuing:
8282
$ adb logcat
8383
```
8484

85+
### Sample Application Functionality
86+
87+
#### Native wolfCrypt Test Application
88+
89+
The sample application has a button that runs the native wolfCrypt tests. This
90+
wraps and runs the wolfCrypt test from wolfSSL (./wolfcrypt/test/test.c). Ouput
91+
will be printed to the logcat log.
92+
93+
After clicking this button, wolfCrypt tests for all enabled algorithms will
94+
run and print the test status to the logcat log:
95+
96+
```
97+
[WOLFCRYPT]: ------------------------------------------------------------------------------
98+
[WOLFCRYPT]: wolfSSL version 5.3.0
99+
[WOLFCRYPT]: ------------------------------------------------------------------------------
100+
[WOLFCRYPT]: error test passed!
101+
[WOLFCRYPT]: MEMORY test passed!
102+
[WOLFCRYPT]: base64 test passed!
103+
[WOLFCRYPT]: base16 test passed!
104+
[WOLFCRYPT]: asn test passed!
105+
[WOLFCRYPT]: RANDOM test passed!
106+
[WOLFCRYPT]: MD5 test passed!
107+
[WOLFCRYPT]: SHA test passed!
108+
[WOLFCRYPT]: SHA-224 test passed!
109+
[WOLFCRYPT]: SHA-256 test passed!
110+
[WOLFCRYPT]: SHA-384 test passed!
111+
[WOLFCRYPT]: SHA-512 test passed!
112+
[WOLFCRYPT]: SHA-3 test passed!
113+
[WOLFCRYPT]: Hash test passed!
114+
[WOLFCRYPT]: HMAC-MD5 test passed!
115+
[WOLFCRYPT]: HMAC-SHA test passed!
116+
[WOLFCRYPT]: HMAC-SHA224 test passed!
117+
[WOLFCRYPT]: HMAC-SHA256 test passed!
118+
[WOLFCRYPT]: HMAC-SHA384 test passed!
119+
[WOLFCRYPT]: HMAC-SHA512 test passed!
120+
[WOLFCRYPT]: HMAC-SHA3 test passed!
121+
[WOLFCRYPT]: HMAC-KDF test passed!
122+
[WOLFCRYPT]: TLSv1.3 KDF test passed!
123+
[WOLFCRYPT]: GMAC test passed!
124+
[WOLFCRYPT]: Chacha test passed!
125+
[WOLFCRYPT]: POLY1305 test passed!
126+
[WOLFCRYPT]: ChaCha20-Poly1305 AEAD test passed!
127+
[WOLFCRYPT]: AES test passed!
128+
[WOLFCRYPT]: AES192 test passed!
129+
[WOLFCRYPT]: AES256 test passed!
130+
[WOLFCRYPT]: AES-GCM test passed!
131+
[WOLFCRYPT]: RSA NOPAD test passed!
132+
[WOLFCRYPT]: RSA test passed!
133+
[WOLFCRYPT]: DH test passed!
134+
[WOLFCRYPT]: PWDBASED test passed!
135+
[WOLFCRYPT]: OPENSSL test passed!
136+
[WOLFCRYPT]: OPENSSL (EVP MD) passed!
137+
[WOLFCRYPT]: OPENSSL (PKEY0) passed!
138+
[WOLFCRYPT]: OPENSSL (PKEY1) passed!
139+
[WOLFCRYPT]: OPENSSL (EVP Sign/Verify) passed!
140+
[WOLFCRYPT]: ECC test passed!
141+
[WOLFCRYPT]: ECC buffer test passed!
142+
[WOLFCRYPT]: logging test passed!
143+
[WOLFCRYPT]: time test passed!
144+
[WOLFCRYPT]: mutex test passed!
145+
[WOLFCRYPT]: memcb test passed!
146+
[WOLFCRYPT]: Test complete
147+
```
148+
149+
#### Native wolfCrypt Benchmark Application
150+
151+
The sample application has a button that runs the native wolfCrypt benchmarks.
152+
This wraps the wolfCrypt benchmark from wolfSSL
153+
(./wolfcrypt/benchmark/benchmark.c). Output will be printed to the logcat log.
154+
155+
After clicking on this button, wolfCrypt benchmarks will be run for all
156+
enabled algorithms and be printed to the logcat log.
157+
158+
The following is example output when run on a Pixel 5 API 31
159+
(Android 12, Google APIs) x86\_64 emulator with SP math enabled:
160+
161+
```
162+
[WOLFCRYPT]: wolfCrypt Benchmark (block bytes 1048576, min 1.0 sec each)
163+
[WOLFCRYPT]: RNG 15 MB took 1.149 seconds, 13.052 MB/s Cycles per byte = 218.79
164+
[WOLFCRYPT]: AES-128-CBC-enc 40 MB took 1.009 seconds, 39.660 MB/s Cycles per byte = 72.00
165+
[WOLFCRYPT]: AES-128-CBC-dec 40 MB took 1.056 seconds, 37.864 MB/s Cycles per byte = 75.42
166+
[WOLFCRYPT]: AES-192-CBC-enc 40 MB took 1.032 seconds, 38.777 MB/s Cycles per byte = 73.64
167+
[WOLFCRYPT]: AES-192-CBC-dec 40 MB took 1.076 seconds, 37.162 MB/s Cycles per byte = 76.84
168+
[WOLFCRYPT]: AES-256-CBC-enc 40 MB took 1.092 seconds, 36.617 MB/s Cycles per byte = 77.99
169+
[WOLFCRYPT]: AES-256-CBC-dec 40 MB took 1.121 seconds, 35.697 MB/s Cycles per byte = 80.00
170+
[WOLFCRYPT]: AES-128-GCM-enc 30 MB took 1.172 seconds, 25.607 MB/s Cycles per byte = 111.52
171+
[WOLFCRYPT]: AES-128-GCM-dec 30 MB took 1.169 seconds, 25.656 MB/s Cycles per byte = 111.31
172+
[WOLFCRYPT]: AES-192-GCM-enc 25 MB took 1.214 seconds, 20.589 MB/s Cycles per byte = 138.70
173+
[WOLFCRYPT]: AES-192-GCM-dec 25 MB took 1.008 seconds, 24.803 MB/s Cycles per byte = 115.13
174+
[WOLFCRYPT]: AES-256-GCM-enc 25 MB took 1.059 seconds, 23.601 MB/s Cycles per byte = 121.00
175+
[WOLFCRYPT]: AES-256-GCM-dec 25 MB took 1.045 seconds, 23.935 MB/s Cycles per byte = 119.31
176+
[WOLFCRYPT]: GMAC Table 4-bit 69 MB took 1.000 seconds, 68.992 MB/s Cycles per byte = 41.39
177+
[WOLFCRYPT]: CHACHA 35 MB took 1.031 seconds, 33.954 MB/s Cycles per byte = 84.10
178+
[WOLFCRYPT]: CHA-POLY 35 MB took 1.068 seconds, 32.778 MB/s Cycles per byte = 87.12
179+
[WOLFCRYPT]: MD5 135 MB took 1.018 seconds, 132.625 MB/s Cycles per byte = 21.53
180+
[WOLFCRYPT]: POLY1305 395 MB took 1.001 seconds, 394.472 MB/s Cycles per byte = 7.24
181+
[WOLFCRYPT]: SHA 75 MB took 1.065 seconds, 70.436 MB/s Cycles per byte = 40.54
182+
[WOLFCRYPT]: SHA-224 30 MB took 1.010 seconds, 29.705 MB/s Cycles per byte = 96.13
183+
[WOLFCRYPT]: SHA-256 35 MB took 1.162 seconds, 30.129 MB/s Cycles per byte = 94.78
184+
[WOLFCRYPT]: SHA-384 45 MB took 1.051 seconds, 42.811 MB/s Cycles per byte = 66.70
185+
[WOLFCRYPT]: SHA-512 45 MB took 1.053 seconds, 42.745 MB/s Cycles per byte = 66.81
186+
[WOLFCRYPT]: SHA3-224 85 MB took 1.063 seconds, 80.000 MB/s Cycles per byte = 35.70
187+
[WOLFCRYPT]: SHA3-256 80 MB took 1.046 seconds, 76.462 MB/s Cycles per byte = 37.35
188+
[WOLFCRYPT]: SHA3-384 60 MB took 1.002 seconds, 59.866 MB/s Cycles per byte = 47.70
189+
[WOLFCRYPT]: SHA3-512 45 MB took 1.074 seconds, 41.909 MB/s Cycles per byte = 68.14
190+
[WOLFCRYPT]: HMAC-MD5 135 MB took 1.012 seconds, 133.384 MB/s Cycles per byte = 21.41
191+
[WOLFCRYPT]: HMAC-SHA 75 MB took 1.058 seconds, 70.917 MB/s Cycles per byte = 40.27
192+
[WOLFCRYPT]: HMAC-SHA224 35 MB took 1.136 seconds, 30.822 MB/s Cycles per byte = 92.65
193+
[WOLFCRYPT]: HMAC-SHA256 30 MB took 1.004 seconds, 29.888 MB/s Cycles per byte = 95.54
194+
[WOLFCRYPT]: HMAC-SHA384 45 MB took 1.062 seconds, 42.370 MB/s Cycles per byte = 67.40
195+
[WOLFCRYPT]: HMAC-SHA512 45 MB took 1.069 seconds, 42.093 MB/s Cycles per byte = 67.84
196+
[WOLFCRYPT]: PBKDF2 4 KB took 1.008 seconds, 3.598 KB/s Cycles per byte = 812776.94
197+
[WOLFCRYPT]: RSA 2048 public 18700 ops took 1.000 sec, avg 0.053 ms, 18693.849 ops/sec
198+
[WOLFCRYPT]: RSA 2048 private 900 ops took 1.001 sec, avg 1.112 ms, 899.441 ops/sec
199+
[WOLFCRYPT]: DH 2048 key gen 1761 ops took 1.000 sec, avg 0.568 ms, 1760.757 ops/sec
200+
[WOLFCRYPT]: DH 2048 agree 1800 ops took 1.004 sec, avg 0.558 ms, 1792.190 ops/sec
201+
[WOLFCRYPT]: ECC [ SECP256R1] 256 key gen 33100 ops took 1.003 sec, avg 0.030 ms, 33004.652 ops/sec
202+
[WOLFCRYPT]: ECDHE [ SECP256R1] 256 agree 12200 ops took 1.001 sec, avg 0.082 ms, 12185.694 ops/sec
203+
[WOLFCRYPT]: ECDSA [ SECP256R1] 256 sign 21400 ops took 1.001 sec, avg 0.047 ms, 21373.222 ops/sec
204+
[WOLFCRYPT]: ECDSA [ SECP256R1] 256 verify 11300 ops took 1.006 sec, avg 0.089 ms, 11237.251 ops/sec
205+
[WOLFCRYPT]: Benchmark complete
206+
```
207+
208+
This can be a good tool when optimizing wolfSSL and wolfCrypt for performance.
209+
210+
#### Simple SSLSocket Connection
211+
212+
The sample application has a button that makes a simple SSL/TLS connection
213+
to `wolfssl.com:443` using the SSLSocket class. It does not explicitly load
214+
CA certificates from a KeyStore, since wolfJSSE automatically loads the
215+
Android KeyStore root certificates as trusted, and those are able to
216+
authenticate wolfssl.com.
217+
218+
After clicking on this button, wolfJSSE debug output will be printed to the
219+
logcat log.
220+
221+
#### How to Change wolfSSL Library Configuration
222+
223+
This sample Android application builds native wolfSSL and wolfCrypt sources
224+
into a shared library. That build is done with cmake and controlled by the
225+
CMakeLists.txt file located at:
226+
227+
android/wolfssljni-ndk-gradle/app/CMakeLists.txt
228+
229+
wolfSSL CFLAGS are defined using the `add_definition()` function, and have
230+
been pre-populated in this sample to match those defined when building wolfSSL
231+
with `./configure --enable-jni`.
232+
233+
This CMakeLists.txt has been designed to allow easy switching between wolfSSL's
234+
fastmath math library and the newer SP math library. SP math can offer
235+
performance advantages over the fastmath library, especially when assembly
236+
optimizations are available for a given platform.
237+
238+
To control the math library used, change the variable `WOLFSSL_MATH_LIB`
239+
in CMakeLists.txt:
240+
241+
```
242+
# Math library selection, used to switch on below. Should be one of:
243+
# fastmath
244+
# spmath
245+
set(WOLFSSL_MATH_LIB "spmath")
246+
```
247+
85248
## wolfSSL NDK Standalone Toolchain Example
86249

87250
### Prerequisites for successful installation
Submodule wolfssl updated 282 files

0 commit comments

Comments
 (0)