From 37ecee4d749146ec45d86f45fb3df4a6ed0e32f7 Mon Sep 17 00:00:00 2001 From: bruce Date: Tue, 2 Feb 2021 14:43:42 +0800 Subject: [PATCH 01/21] [LFS]:{set git lfs.} --- .gitattributes | 4 ++++ .gitignore | 5 +++++ README_zh.md | 8 +++++++- 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..0766315 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,4 @@ +*.so filter=lfs diff=lfs merge=lfs -text +*.a filter=lfs diff=lfs merge=lfs -text +*.la filter=lfs diff=lfs merge=lfs -text +*.lai filter=lfs diff=lfs merge=lfs -text diff --git a/.gitignore b/.gitignore index f54672d..bf421b6 100644 --- a/.gitignore +++ b/.gitignore @@ -56,4 +56,9 @@ captures/ # External native build folder generated in Android Studio 2.2 and later .externalNativeBuild +.cxx +#remote build +mainframer.sh +.mainframer + diff --git a/README_zh.md b/README_zh.md index 66da5a6..cf31bc8 100644 --- a/README_zh.md +++ b/README_zh.md @@ -3,7 +3,8 @@ [中文](https://github.com/BruceWind/AESJniEncrypt/blob/master/README_zh.md) # 追求极致的代码安全性保障 -- [x] ndk实现AES加密 +- [x] ~~ndk实现AES加密~~,性能不佳,已废弃此方式,TLS1.3 +- [ ] 使用chacha20加密,TLS1.3在移动端都用了chacha20了 - [x] 使用JniOnload 隐藏c函数 - [x] 使用签名校验避免被再次打包(这是绕过破解加密算法直接调用你的jni函数) - [x] ~~key存在符号表中,同时隐藏字符表~~ 该方案已经废弃,[废弃原因issues5](https://github.com/weizongwei5/AESJniEncrypt/issues/5),请看下一条 @@ -17,6 +18,11 @@ ``` char * key = "NMTIzNDU2Nzg5MGFiY2RlZg";//这里是key被做过处理存储在这里的,实际上真实的key是:"1234567890abcdef" ``` + +## 克隆代码准备 +先安装GIT-LFS: +https://git-lfs.github.com/ + ## 集成 a.先配置local.properties中ndk.dir 要求使用ndk版本必须11-13b,新版本ndk没有测试过,或许不能编译通过。 From 00a49fdc5a060775f800da850e14012c76566706 Mon Sep 17 00:00:00 2001 From: hide Date: Thu, 4 Feb 2021 21:21:13 +0800 Subject: [PATCH 02/21] [native]:{add shell script for building libsodium static library.} --- .gitattributes | 3 +- .gitignore | 6 +- aesjni/CMakeLists.txt | 1 + aesjni/build.gradle | 26 +- .../aesjni/SWIGTYPE_p_unsigned_long_long.java | 26 + .../java/com/androidyuan/aesjni/Sodium.java | 148 ++ .../androidyuan/aesjni/SodiumConstants.java | 40 + .../com/androidyuan/aesjni/SodiumJNI.java | 60 + aesjni/src/main/jni/Android.mk | 26 +- aesjni/src/main/jni/Application.mk | 13 +- aesjni/src/main/jni/JNIEncrypt.c | 16 +- aesjni/src/main/jni/OriginAndroid.mk | 46 + aesjni/src/main/jni/README.md | 0 aesjni/src/main/jni/TestAndroid.mk | 11 + aesjni/src/main/jni/aead_aes256gcm.c | 181 ++ aesjni/src/main/jni/aes.c | 594 ------- aesjni/src/main/jni/aes.h | 54 - aesjni/src/main/jni/base64.c | 0 aesjni/src/main/jni/base64.h | 0 .../build_libsodium_for_all_android_abi.sh | 88 + aesjni/src/main/jni/check_emulator.c | 0 aesjni/src/main/jni/check_emulator.h | 0 aesjni/src/main/jni/checksignature.c | 0 aesjni/src/main/jni/checksignature.h | 0 aesjni/src/main/jni/debugger.c | 0 aesjni/src/main/jni/debugger.h | 0 aesjni/src/main/jni/logger.h | 0 aesjni/src/main/jni/main_unit_test.c | 22 + aesjni/src/main/jni/rebuild.sh | 36 +- aesjni/src/main/jni/sodium.i | 294 ++++ aesjni/src/main/jni/sodium_include/README.md | 8 + aesjni/src/main/jni/sodium_wrap.c | 1512 +++++++++++++++++ aesjni/src/main/jni/swig_gen.sh | 59 + build.gradle | 3 +- gradle.properties | 1 + gradle/wrapper/gradle-wrapper.properties | 4 +- 36 files changed, 2571 insertions(+), 707 deletions(-) create mode 100644 aesjni/src/main/java/com/androidyuan/aesjni/SWIGTYPE_p_unsigned_long_long.java create mode 100644 aesjni/src/main/java/com/androidyuan/aesjni/Sodium.java create mode 100644 aesjni/src/main/java/com/androidyuan/aesjni/SodiumConstants.java create mode 100644 aesjni/src/main/java/com/androidyuan/aesjni/SodiumJNI.java mode change 100644 => 100755 aesjni/src/main/jni/Android.mk mode change 100644 => 100755 aesjni/src/main/jni/Application.mk mode change 100644 => 100755 aesjni/src/main/jni/JNIEncrypt.c create mode 100755 aesjni/src/main/jni/OriginAndroid.mk mode change 100644 => 100755 aesjni/src/main/jni/README.md create mode 100755 aesjni/src/main/jni/TestAndroid.mk create mode 100755 aesjni/src/main/jni/aead_aes256gcm.c delete mode 100644 aesjni/src/main/jni/aes.c delete mode 100644 aesjni/src/main/jni/aes.h mode change 100644 => 100755 aesjni/src/main/jni/base64.c mode change 100644 => 100755 aesjni/src/main/jni/base64.h create mode 100755 aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh mode change 100644 => 100755 aesjni/src/main/jni/check_emulator.c mode change 100644 => 100755 aesjni/src/main/jni/check_emulator.h mode change 100644 => 100755 aesjni/src/main/jni/checksignature.c mode change 100644 => 100755 aesjni/src/main/jni/checksignature.h mode change 100644 => 100755 aesjni/src/main/jni/debugger.c mode change 100644 => 100755 aesjni/src/main/jni/debugger.h mode change 100644 => 100755 aesjni/src/main/jni/logger.h create mode 100755 aesjni/src/main/jni/main_unit_test.c mode change 100644 => 100755 aesjni/src/main/jni/rebuild.sh create mode 100755 aesjni/src/main/jni/sodium.i create mode 100644 aesjni/src/main/jni/sodium_include/README.md create mode 100755 aesjni/src/main/jni/sodium_wrap.c create mode 100755 aesjni/src/main/jni/swig_gen.sh diff --git a/.gitattributes b/.gitattributes index 0766315..37d202a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,4 +1,3 @@ *.so filter=lfs diff=lfs merge=lfs -text *.a filter=lfs diff=lfs merge=lfs -text -*.la filter=lfs diff=lfs merge=lfs -text -*.lai filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text diff --git a/.gitignore b/.gitignore index bf421b6..7d6ac26 100644 --- a/.gitignore +++ b/.gitignore @@ -60,5 +60,7 @@ captures/ #remote build mainframer.sh .mainframer - - +aesjni/src/main/libs/ +aesjni/src/main/obj/ +aesjni/src/main/jni/Android.mk +aesjni/src/main/jni/sodium_include diff --git a/aesjni/CMakeLists.txt b/aesjni/CMakeLists.txt index f1be052..5fe5f73 100644 --- a/aesjni/CMakeLists.txt +++ b/aesjni/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 3.4.1) #普通版本的clang +#base on libsodium-1.0.18,which was downloaded from https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-stable.tar.gz; add_library( # Sets the name of the library. JNIEncrypt diff --git a/aesjni/build.gradle b/aesjni/build.gradle index 865e2d1..7bc1877 100644 --- a/aesjni/build.gradle +++ b/aesjni/build.gradle @@ -3,9 +3,10 @@ apply plugin: 'com.android.library' android { compileSdkVersion 25 buildToolsVersion "25.0.3" + ndkPath "/mnt/mylibs/sdk/android-ndk-r21d/" defaultConfig { - minSdkVersion 15 + minSdkVersion 21 targetSdkVersion 25 versionCode 1 versionName "1.0" @@ -20,7 +21,7 @@ android { ndk { // Specifies the ABI configurations of your native // libraries Gradle should build and package with your APK. - abiFilters 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a' + abiFilters 'arm64-v8a' } } @@ -33,18 +34,19 @@ android { } } + def USING_NDK = 1 // if you want to build with NDK, set it to 1. - //selection 1 this used obfuscator library to obfuscate the native code -// externalNativeBuild{ -// ndkBuild{ -// path file("src/main/jni/Android.mk") -// } -// } - - //selection 2 the doesn't obfuscate native code **** not recommand use cmake***** externalNativeBuild { - cmake { - path "CMakeLists.txt" + if (USING_NDK == 1) { + //selection 1 this used obfuscator to obfuscate the native code + ndkBuild { + path file("src/main/jni/Android.mk") + } + } else { + //selection 2 the selection doesn't obfuscate native code **** i don't recommand using cmake***** + cmake { + path "CMakeLists.txt" + } } } diff --git a/aesjni/src/main/java/com/androidyuan/aesjni/SWIGTYPE_p_unsigned_long_long.java b/aesjni/src/main/java/com/androidyuan/aesjni/SWIGTYPE_p_unsigned_long_long.java new file mode 100644 index 0000000..88f9fbe --- /dev/null +++ b/aesjni/src/main/java/com/androidyuan/aesjni/SWIGTYPE_p_unsigned_long_long.java @@ -0,0 +1,26 @@ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 4.0.2 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +package com.androidyuan.aesjni; + +public class SWIGTYPE_p_unsigned_long_long { + private transient long swigCPtr; + + protected SWIGTYPE_p_unsigned_long_long(long cPtr, @SuppressWarnings("unused") boolean futureUse) { + swigCPtr = cPtr; + } + + protected SWIGTYPE_p_unsigned_long_long() { + swigCPtr = 0; + } + + protected static long getCPtr(SWIGTYPE_p_unsigned_long_long obj) { + return (obj == null) ? 0 : obj.swigCPtr; + } +} + diff --git a/aesjni/src/main/java/com/androidyuan/aesjni/Sodium.java b/aesjni/src/main/java/com/androidyuan/aesjni/Sodium.java new file mode 100644 index 0000000..d43bb0a --- /dev/null +++ b/aesjni/src/main/java/com/androidyuan/aesjni/Sodium.java @@ -0,0 +1,148 @@ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 4.0.2 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +package com.androidyuan.aesjni; + +public class Sodium implements SodiumConstants { + public static int sodium_init() { + return SodiumJNI.sodium_init(); + } + + public static String sodium_version_string() { + return SodiumJNI.sodium_version_string(); + } + + public static void randombytes_buf(byte[] buf, int size) { + SodiumJNI.randombytes_buf(buf, size); + } + + public static int randombytes_random() { + return SodiumJNI.randombytes_random(); + } + + public static int randombytes_uniform(int upper_bound) { + return SodiumJNI.randombytes_uniform(upper_bound); + } + + public static int crypto_sign_keypair(byte[] pk, byte[] sk) { + return SodiumJNI.crypto_sign_keypair(pk, sk); + } + + public static int crypto_sign_seed_keypair(byte[] pk, byte[] sk, byte[] seed) { + return SodiumJNI.crypto_sign_seed_keypair(pk, sk, seed); + } + + public static int crypto_sign(byte[] sm, SWIGTYPE_p_unsigned_long_long smlem, byte[] m, long mlen, byte[] sk) { + return SodiumJNI.crypto_sign(sm, SWIGTYPE_p_unsigned_long_long.getCPtr(smlem), m, mlen, sk); + } + + public static int crypto_sign_open(byte[] m, SWIGTYPE_p_unsigned_long_long mlen, byte[] sm, long smlen, byte[] pk) { + return SodiumJNI.crypto_sign_open(m, SWIGTYPE_p_unsigned_long_long.getCPtr(mlen), sm, smlen, pk); + } + + public static int crypto_sign_detached(byte[] sig, SWIGTYPE_p_unsigned_long_long slen, byte[] m, long mlen, byte[] sk) { + return SodiumJNI.crypto_sign_detached(sig, SWIGTYPE_p_unsigned_long_long.getCPtr(slen), m, mlen, sk); + } + + public static int crypto_sign_ed25519_sk_to_curve25519(byte[] curve25519_sk, byte[] ed25519_sk) { + return SodiumJNI.crypto_sign_ed25519_sk_to_curve25519(curve25519_sk, ed25519_sk); + } + + public static int crypto_sign_ed25519_pk_to_curve25519(byte[] curve25519_pk, byte[] ed25519_pk) { + return SodiumJNI.crypto_sign_ed25519_pk_to_curve25519(curve25519_pk, ed25519_pk); + } + + public static int crypto_sign_verify_detached(byte[] sig, byte[] m, long mlen, byte[] pk) { + return SodiumJNI.crypto_sign_verify_detached(sig, m, mlen, pk); + } + + public static int crypto_sign_ed25519_sk_to_seed(byte[] seed, byte[] sk) { + return SodiumJNI.crypto_sign_ed25519_sk_to_seed(seed, sk); + } + + public static int crypto_sign_ed25519_sk_to_pk(byte[] pk, byte[] sk) { + return SodiumJNI.crypto_sign_ed25519_sk_to_pk(pk, sk); + } + + public static int crypto_box_seed_keypair(byte[] pk, byte[] sk, byte[] seed) { + return SodiumJNI.crypto_box_seed_keypair(pk, sk, seed); + } + + public static int crypto_box_keypair(byte[] pk, byte[] sk) { + return SodiumJNI.crypto_box_keypair(pk, sk); + } + + public static int crypto_box_easy(byte[] c, byte[] m, long mlen, byte[] n, byte[] pk, byte[] sk) { + return SodiumJNI.crypto_box_easy(c, m, mlen, n, pk, sk); + } + + public static int crypto_box_open_easy(byte[] m, byte[] c, long clen, byte[] n, byte[] pk, byte[] sk) { + return SodiumJNI.crypto_box_open_easy(m, c, clen, n, pk, sk); + } + + public static int crypto_box_detached(byte[] c, byte[] mac, byte[] m, long mlen, byte[] n, byte[] pk, byte[] sk) { + return SodiumJNI.crypto_box_detached(c, mac, m, mlen, n, pk, sk); + } + + public static int crypto_box_open_detached(byte[] m, byte[] c, byte[] mac, long clen, byte[] n, byte[] pk, byte[] sk) { + return SodiumJNI.crypto_box_open_detached(m, c, mac, clen, n, pk, sk); + } + + public static int crypto_secretbox_easy(byte[] c, byte[] m, long mlen, byte[] n, byte[] k) { + return SodiumJNI.crypto_secretbox_easy(c, m, mlen, n, k); + } + + public static int crypto_secretbox_open_easy(byte[] m, byte[] c, long clen, byte[] n, byte[] k) { + return SodiumJNI.crypto_secretbox_open_easy(m, c, clen, n, k); + } + + public static int crypto_secretbox_detached(byte[] c, byte[] mac, byte[] m, long mlen, byte[] n, byte[] k) { + return SodiumJNI.crypto_secretbox_detached(c, mac, m, mlen, n, k); + } + + public static int crypto_secretbox_open_detached(byte[] m, byte[] c, byte[] mac, long clen, byte[] n, byte[] k) { + return SodiumJNI.crypto_secretbox_open_detached(m, c, mac, clen, n, k); + } + + public static int crypto_auth(byte[] out, byte[] in, long inlen, byte[] k) { + return SodiumJNI.crypto_auth(out, in, inlen, k); + } + + public static int crypto_auth_verify(byte[] h, byte[] in, long inlen, byte[] k) { + return SodiumJNI.crypto_auth_verify(h, in, inlen, k); + } + + public static int crypto_aead_chacha20poly1305_encrypt(byte[] c, SWIGTYPE_p_unsigned_long_long clen, byte[] m, long mlen, byte[] ad, long adlen, byte[] nsec, byte[] npub, byte[] k) { + return SodiumJNI.crypto_aead_chacha20poly1305_encrypt(c, SWIGTYPE_p_unsigned_long_long.getCPtr(clen), m, mlen, ad, adlen, nsec, npub, k); + } + + public static int crypto_aead_chacha20poly1305_decrypt(byte[] m, SWIGTYPE_p_unsigned_long_long mlen, byte[] nsec, byte[] c, long clen, byte[] ad, long adlen, byte[] npub, byte[] k) { + return SodiumJNI.crypto_aead_chacha20poly1305_decrypt(m, SWIGTYPE_p_unsigned_long_long.getCPtr(mlen), nsec, c, clen, ad, adlen, npub, k); + } + + public static int crypto_pwhash_scryptsalsa208sha256(byte[] out, long outlen, String passwd, long passwdlen, byte[] salt, long opslimit, int memlimit) { + return SodiumJNI.crypto_pwhash_scryptsalsa208sha256(out, outlen, passwd, passwdlen, salt, opslimit, memlimit); + } + + public static int crypto_pwhash_scryptsalsa208sha256_str(byte[] out, String passwd, long passwdlen, long opslimit, int memlimit) { + return SodiumJNI.crypto_pwhash_scryptsalsa208sha256_str(out, passwd, passwdlen, opslimit, memlimit); + } + + public static int crypto_pwhash_scryptsalsa208sha256_str_verify(byte[] str, String passwd, long passwdlen) { + return SodiumJNI.crypto_pwhash_scryptsalsa208sha256_str_verify(str, passwd, passwdlen); + } + + public static int crypto_shorthash(byte[] out, byte[] in, long inlen, byte[] k) { + return SodiumJNI.crypto_shorthash(out, in, inlen, k); + } + + public static int crypto_generichash(byte[] out, int outlen, byte[] in, long inlen, byte[] key, int keylen) { + return SodiumJNI.crypto_generichash(out, outlen, in, inlen, key, keylen); + } + +} diff --git a/aesjni/src/main/java/com/androidyuan/aesjni/SodiumConstants.java b/aesjni/src/main/java/com/androidyuan/aesjni/SodiumConstants.java new file mode 100644 index 0000000..2de7a58 --- /dev/null +++ b/aesjni/src/main/java/com/androidyuan/aesjni/SodiumConstants.java @@ -0,0 +1,40 @@ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 4.0.2 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +package com.androidyuan.aesjni; + +public interface SodiumConstants { + public final static int CRYPTO_SIGN_BYTES = 64; + public final static int CRYPTO_SIGN_SEEDBYTES = 32; + public final static int CRYPTO_SIGN_PUBLICKEYBYTES = 32; + public final static int CRYPTO_SIGN_SECRETKEYBYTES = (32+32); + public final static int CRYPTO_BOX_SEEDBYTES = 32; + public final static int CRYPTO_BOX_PUBLICKEYBYTES = 32; + public final static int CRYPTO_BOX_SECRETKEYBYTES = 32; + public final static int CRYPTO_BOX_NONCEBYTES = 24; + public final static int CRYPTO_BOX_MACBYTES = 16; + public final static int CRYPTO_SECRETBOX_KEYBYTES = 32; + public final static int CRYPTO_SECRETBOX_MACBYTES = 16; + public final static int CRYPTO_SECRETBOX_NONCEBYTES = 24; + public final static int CRYPTO_AUTH_BYTES = 32; + public final static int CRYPTO_AUTH_KEYBYTES = 32; + public final static int CRYPTO_AED_CHACHA20POLY1305_KEYBYTES = 32; + public final static int CRYPTO_AED_CHACHA20POLY1305_NONCEBYTES = 32; + public final static int CRYPTO_AED_CHACHA20POLY1305_MACBYTES = 16; + public final static int CRYPTO_PWHASH_SALTBYTES = 32; + public final static int CRYPTO_PWHASH_HASHBYTES = 102; + public final static int CRYPTO_PWHASH_KEY_BYTES = 32; + public final static int CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE = 524288; + public final static int CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE = 16777216; + public final static int CRYPTO_PWHASH_OPSLIMIT_SENSITIVE = 33554432; + public final static int CRYPTO_PWHASH_MEMLIMIT_SENSITIVE = 1073741824; + public final static int CRYPTO_SHORTHASH_BYTES = 8; + public final static int CRYPTO_SHORTHASH_KEYBYTES = 16; + public final static int CRYPTO_GENERICHASH_BYTES = 32; + public final static int CRYPTO_GENERICHASH_KEYBYTES = 32; +} diff --git a/aesjni/src/main/java/com/androidyuan/aesjni/SodiumJNI.java b/aesjni/src/main/java/com/androidyuan/aesjni/SodiumJNI.java new file mode 100644 index 0000000..049ecbf --- /dev/null +++ b/aesjni/src/main/java/com/androidyuan/aesjni/SodiumJNI.java @@ -0,0 +1,60 @@ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 4.0.2 + * + * Do not make changes to this file unless you know what you are doing--modify + * the SWIG interface file instead. + * ----------------------------------------------------------------------------- */ + +package com.androidyuan.aesjni; + +public class SodiumJNI { + public final static native int sodium_init(); + public final static native String sodium_version_string(); + public final static native void randombytes_buf(byte[] jarg1, int jarg2); + public final static native int randombytes_random(); + public final static native int randombytes_uniform(int jarg1); + public final static native int crypto_sign_keypair(byte[] jarg1, byte[] jarg2); + public final static native int crypto_sign_seed_keypair(byte[] jarg1, byte[] jarg2, byte[] jarg3); + public final static native int crypto_sign(byte[] jarg1, long jarg2, byte[] jarg3, long jarg4, byte[] jarg5); + public final static native int crypto_sign_open(byte[] jarg1, long jarg2, byte[] jarg3, long jarg4, byte[] jarg5); + public final static native int crypto_sign_detached(byte[] jarg1, long jarg2, byte[] jarg3, long jarg4, byte[] jarg5); + public final static native int crypto_sign_ed25519_sk_to_curve25519(byte[] jarg1, byte[] jarg2); + public final static native int crypto_sign_ed25519_pk_to_curve25519(byte[] jarg1, byte[] jarg2); + public final static native int crypto_sign_verify_detached(byte[] jarg1, byte[] jarg2, long jarg3, byte[] jarg4); + public final static native int crypto_sign_ed25519_sk_to_seed(byte[] jarg1, byte[] jarg2); + public final static native int crypto_sign_ed25519_sk_to_pk(byte[] jarg1, byte[] jarg2); + public final static native int crypto_box_seed_keypair(byte[] jarg1, byte[] jarg2, byte[] jarg3); + public final static native int crypto_box_keypair(byte[] jarg1, byte[] jarg2); + public final static native int crypto_box_easy(byte[] jarg1, byte[] jarg2, long jarg3, byte[] jarg4, byte[] jarg5, byte[] jarg6); + public final static native int crypto_box_open_easy(byte[] jarg1, byte[] jarg2, long jarg3, byte[] jarg4, byte[] jarg5, byte[] jarg6); + public final static native int crypto_box_detached(byte[] jarg1, byte[] jarg2, byte[] jarg3, long jarg4, byte[] jarg5, byte[] jarg6, byte[] jarg7); + public final static native int crypto_box_open_detached(byte[] jarg1, byte[] jarg2, byte[] jarg3, long jarg4, byte[] jarg5, byte[] jarg6, byte[] jarg7); + public final static native int crypto_secretbox_easy(byte[] jarg1, byte[] jarg2, long jarg3, byte[] jarg4, byte[] jarg5); + public final static native int crypto_secretbox_open_easy(byte[] jarg1, byte[] jarg2, long jarg3, byte[] jarg4, byte[] jarg5); + public final static native int crypto_secretbox_detached(byte[] jarg1, byte[] jarg2, byte[] jarg3, long jarg4, byte[] jarg5, byte[] jarg6); + public final static native int crypto_secretbox_open_detached(byte[] jarg1, byte[] jarg2, byte[] jarg3, long jarg4, byte[] jarg5, byte[] jarg6); + public final static native int crypto_auth(byte[] jarg1, byte[] jarg2, long jarg3, byte[] jarg4); + public final static native int crypto_auth_verify(byte[] jarg1, byte[] jarg2, long jarg3, byte[] jarg4); + public final static native int crypto_aead_chacha20poly1305_encrypt(byte[] jarg1, long jarg2, byte[] jarg3, long jarg4, byte[] jarg5, long jarg6, byte[] jarg7, byte[] jarg8, byte[] jarg9); + public final static native int crypto_aead_chacha20poly1305_decrypt(byte[] jarg1, long jarg2, byte[] jarg3, byte[] jarg4, long jarg5, byte[] jarg6, long jarg7, byte[] jarg8, byte[] jarg9); + public final static native int crypto_pwhash_scryptsalsa208sha256(byte[] jarg1, long jarg2, String jarg3, long jarg4, byte[] jarg5, long jarg6, int jarg7); + public final static native int crypto_pwhash_scryptsalsa208sha256_str(byte[] jarg1, String jarg2, long jarg3, long jarg4, int jarg5); + public final static native int crypto_pwhash_scryptsalsa208sha256_str_verify(byte[] jarg1, String jarg2, long jarg3); + public final static native int crypto_shorthash(byte[] jarg1, byte[] jarg2, long jarg3, byte[] jarg4); + public final static native int crypto_generichash(byte[] jarg1, int jarg2, byte[] jarg3, long jarg4, byte[] jarg5, int jarg6); + + /* Load JNI library */ + static { + try { + System.loadLibrary("JNIEncrypt"); + if (sodium_init() == -1) { + throw new RuntimeException("Sodium could not be initialized."); + } + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } + +} diff --git a/aesjni/src/main/jni/Android.mk b/aesjni/src/main/jni/Android.mk old mode 100644 new mode 100755 index f3e4619..5298e20 --- a/aesjni/src/main/jni/Android.mk +++ b/aesjni/src/main/jni/Android.mk @@ -1,25 +1,11 @@ - LOCAL_PATH := $(call my-dir) -include $(CLEAR_VARS) - -LOCAL_MODULE := JNIEncrypt -LOCAL_SRC_FILES := JNIEncrypt.c \ - aes.c \ - checksignature.c \ - check_emulator.c \ - debugger.c \ - logger.h \ - base64.c - -LOCAL_CFLAGS := -fvisibility=hidden -mllvm -sub -mllvm -fla -mllvm -bcf +#... +include $(CLEAR_VARS) +LOCAL_MODULE := test # executable file name. +LOCAL_SRC_FILES := main_unit_test.c +LOCAL_LDLIBS := -llog # no need to specify path for liblog.so - -# LOCAL_SHARED_LIBRARIES := liblog libcutils -LOCAL_LDLIBS := -llog - -include $(BUILD_SHARED_LIBRARY) - -# include $(BUILD_EXECUTABLE) +include $(BUILD_EXECUTABLE) # <-- Use this to build an executable. \ No newline at end of file diff --git a/aesjni/src/main/jni/Application.mk b/aesjni/src/main/jni/Application.mk old mode 100644 new mode 100755 index d173d03..4253008 --- a/aesjni/src/main/jni/Application.mk +++ b/aesjni/src/main/jni/Application.mk @@ -1,10 +1,3 @@ - -LOCAL_PATH := $(call my-dir) - -include $(CLEAR_VARS) - -APP_ABI := armeabi armeabi-v7a arm64-v8a x86_64 - - -NDK_TOOLCHAIN_VERSION := clang-ollvm4.0 -include $(BUILD_EXECUTABLE) +# there is only armv8 is beacause I only push armv8 abi of libsodium static library.You can rebuild libsodium to generate static library of other ABIs. +APP_ABI := arm64-v8a +APP_PLATFORM := android-19 \ No newline at end of file diff --git a/aesjni/src/main/jni/JNIEncrypt.c b/aesjni/src/main/jni/JNIEncrypt.c old mode 100644 new mode 100755 index 3d446c2..0f815fc --- a/aesjni/src/main/jni/JNIEncrypt.c +++ b/aesjni/src/main/jni/JNIEncrypt.c @@ -1,5 +1,8 @@ #include -#include "aes.h" + + +#include + #include "checksignature.h" #include "check_emulator.h" #include @@ -74,11 +77,9 @@ JNIEXPORT jstring JNICALL encode(JNIEnv *env, jobject instance, jobject context, return charToJstring(env,str); } - uint8_t *AES_KEY = (uint8_t *) getKey(); - const char *in = (*env)->GetStringUTFChars(env, str_, JNI_FALSE); - char *baseResult = AES_128_ECB_PKCS5Padding_Encrypt(in, AES_KEY); - (*env)->ReleaseStringUTFChars(env, str_, in); - return (*env)->NewStringUTF(env, baseResult); + + + return (*env)->NewStringUTF(env, ""); } @@ -94,11 +95,10 @@ JNIEXPORT jstring JNICALL decode(JNIEnv *env, jobject instance, jobject context, uint8_t *AES_KEY = (uint8_t *) getKey(); const char *str = (*env)->GetStringUTFChars(env, str_, JNI_FALSE); - char *desResult = AES_128_ECB_PKCS5Padding_Decrypt(str, AES_KEY); (*env)->ReleaseStringUTFChars(env, str_, str); // return (*env)->NewStringUTF(env, desResult); //不用系统自带的方法NewStringUTF是因为如果desResult是乱码,会抛出异常 - return charToJstring(env,desResult); + return (*env)->NewStringUTF(env, ""); } diff --git a/aesjni/src/main/jni/OriginAndroid.mk b/aesjni/src/main/jni/OriginAndroid.mk new file mode 100755 index 0000000..899bfef --- /dev/null +++ b/aesjni/src/main/jni/OriginAndroid.mk @@ -0,0 +1,46 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) + ARCH_PREFIX := armv7-a +else ifeq ($(TARGET_ARCH_ABI),armeabi) + ARCH_PREFIX := armv6 +else ifeq ($(TARGET_ARCH_ABI),x86) + ARCH_PREFIX := i686 +else ifeq ($(TARGET_ARCH_ABI),mips) + ARCH_PREFIX := mips32 +else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) + ARCH_PREFIX := armv8-a +else + ARCH_PREFIX := $(TARGET_ARCH) +endif + +SODIUM_LIB_DIR := $(LOCAL_PATH)/libsodium/libsodium-android-$(ARCH_PREFIX) +SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include +SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a + +LOCAL_MODULE := sodium +LOCAL_SRC_FILES := $(SODIUM_LIB) +LOCAL_EXPORT_C_INCLUDES := $(SODIUM_INC_DIR) $(SODIUM_INC_DIR)/sodium +include $(PREBUILT_STATIC_LIBRARY) + +include $(CLEAR_VARS) + +LOCAL_MODULE := JNIEncrypt +LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ + checksignature.c \ + check_emulator.c \ + debugger.c \ + logger.h \ + base64.c +LOCAL_CFLAGS += -Wall -g -pedantic -std=c99 + +# if you want to print logs pls write liblog on this line. +LOCAL_STATIC_LIBRARIES := sodium liblog libcutils + +# LOCAL_SHARED_LIBRARIES := liblog libcutils +LOCAL_LDLIBS := -llog + +include $(BUILD_SHARED_LIBRARY) + diff --git a/aesjni/src/main/jni/README.md b/aesjni/src/main/jni/README.md old mode 100644 new mode 100755 diff --git a/aesjni/src/main/jni/TestAndroid.mk b/aesjni/src/main/jni/TestAndroid.mk new file mode 100755 index 0000000..5298e20 --- /dev/null +++ b/aesjni/src/main/jni/TestAndroid.mk @@ -0,0 +1,11 @@ +LOCAL_PATH := $(call my-dir) + +#... + +include $(CLEAR_VARS) + +LOCAL_MODULE := test # executable file name. +LOCAL_SRC_FILES := main_unit_test.c +LOCAL_LDLIBS := -llog # no need to specify path for liblog.so + +include $(BUILD_EXECUTABLE) # <-- Use this to build an executable. \ No newline at end of file diff --git a/aesjni/src/main/jni/aead_aes256gcm.c b/aesjni/src/main/jni/aead_aes256gcm.c new file mode 100755 index 0000000..89c5f43 --- /dev/null +++ b/aesjni/src/main/jni/aead_aes256gcm.c @@ -0,0 +1,181 @@ + +#define TEST_NAME "aead_aes256gcm" +#include "cmptest.h" + +static struct { + const char *key_hex; + const char *nonce_hex; + const char *mac_hex; +} tests[] = { + { + "b52c505a37d78eda5dd34f20c22540ea1b58963cf8e5bf8ffa85f9f2492505b4", + "516c33929df5a3284ff463d7", + "bdc1ac884d332457a1d2664f168c76f0", + } +}; + +static int +tv(void) +{ + unsigned char *ad; + unsigned char *ciphertext; + unsigned char *decrypted; + unsigned char *detached_ciphertext; + unsigned char *expected_ciphertext; + unsigned char *key; + unsigned char *message; + unsigned char *mac; + unsigned char *nonce; + char *hex; + unsigned long long found_ciphertext_len; + unsigned long long found_mac_len; + unsigned long long found_message_len; + size_t ad_len; + size_t ciphertext_len; + size_t detached_ciphertext_len; + size_t i = 0U; + size_t message_len; + + key = (unsigned char *) sodium_malloc(crypto_aead_aes256gcm_KEYBYTES); + nonce = (unsigned char *) sodium_malloc(crypto_aead_aes256gcm_NPUBBYTES); + mac = (unsigned char *) sodium_malloc(crypto_aead_aes256gcm_ABYTES); + + do { + assert(strlen(tests[i].key_hex) == 2 * crypto_aead_aes256gcm_KEYBYTES); + sodium_hex2bin(key, crypto_aead_aes256gcm_KEYBYTES, + tests[i].key_hex, strlen(tests[i].key_hex), + NULL, NULL, NULL); + assert(strlen(tests[i].nonce_hex) == 2 * crypto_aead_aes256gcm_NPUBBYTES); + sodium_hex2bin(nonce, crypto_aead_aes256gcm_NPUBBYTES, + tests[i].nonce_hex, strlen(tests[i].nonce_hex), + NULL, NULL, NULL); + message_len = strlen(tests[i].message_hex) / 2; + message = (unsigned char *) sodium_malloc(message_len); + sodium_hex2bin(message, message_len, + tests[i].message_hex, strlen(tests[i].message_hex), + NULL, NULL, NULL); + ad_len = strlen(tests[i].ad_hex) / 2; + ad = (unsigned char *) sodium_malloc(ad_len); + sodium_hex2bin(ad, ad_len, + tests[i].ad_hex, strlen(tests[i].ad_hex), + NULL, NULL, NULL); + ciphertext_len = message_len + crypto_aead_aes256gcm_ABYTES; + detached_ciphertext_len = message_len; + expected_ciphertext = (unsigned char *) sodium_malloc(ciphertext_len); + assert(strlen(tests[i].ciphertext_hex) == 2 * message_len); + sodium_hex2bin(expected_ciphertext, message_len, + tests[i].ciphertext_hex, strlen(tests[i].ciphertext_hex), + NULL, NULL, NULL); + assert(strlen(tests[i].mac_hex) == 2 * crypto_aead_aes256gcm_ABYTES); + sodium_hex2bin(expected_ciphertext + message_len, crypto_aead_aes256gcm_ABYTES, + tests[i].mac_hex, strlen(tests[i].mac_hex), + NULL, NULL, NULL); + ciphertext = (unsigned char *) sodium_malloc(ciphertext_len); + detached_ciphertext = (unsigned char *) sodium_malloc(detached_ciphertext_len); + + crypto_aead_aes256gcm_encrypt_detached(detached_ciphertext, mac, + &found_mac_len, + message, message_len, + ad, ad_len, NULL, nonce, key); + assert(found_mac_len == crypto_aead_aes256gcm_ABYTES); + if (memcmp(detached_ciphertext, expected_ciphertext, + detached_ciphertext_len) != 0 || + memcmp(mac, expected_ciphertext + message_len, + crypto_aead_aes256gcm_ABYTES) != 0) { + printf("Detached encryption of test vector #%u failed\n", (unsigned int) i); + hex = (char *) sodium_malloc((size_t) ciphertext_len * 2 + 1); + sodium_bin2hex(hex, (size_t) ciphertext_len * 2 + 1, + ciphertext, ciphertext_len); + printf("Computed: [%s]\n", hex); + sodium_free(hex); + } + + crypto_aead_aes256gcm_encrypt(ciphertext, &found_ciphertext_len, + message, message_len, + ad, ad_len, NULL, nonce, key); + + assert((size_t) found_ciphertext_len == ciphertext_len); + if (memcmp(ciphertext, expected_ciphertext, ciphertext_len) != 0) { + printf("Encryption of test vector #%u failed\n", (unsigned int) i); + hex = (char *) sodium_malloc((size_t) found_ciphertext_len * 2 + 1); + sodium_bin2hex(hex, (size_t) found_ciphertext_len * 2 + 1, + ciphertext, ciphertext_len); + printf("Computed: [%s]\n", hex); + sodium_free(hex); + } + + decrypted = (unsigned char *) sodium_malloc(message_len); + found_message_len = 1; + if (crypto_aead_aes256gcm_decrypt(decrypted, &found_message_len, + NULL, ciphertext, + randombytes_uniform((uint32_t) ciphertext_len), + ad, ad_len, nonce, key) != -1) { + printf("Verification of test vector #%u after truncation succeeded\n", + (unsigned int) i); + } + if (found_message_len != 0) { + printf("Message length should have been set to zero after a failure\n"); + } + if (crypto_aead_aes256gcm_decrypt(decrypted, &found_message_len, + NULL, guard_page, + randombytes_uniform(crypto_aead_aes256gcm_ABYTES), + ad, ad_len, nonce, key) != -1) { + printf("Verification of test vector #%u with a truncated tag failed\n", + (unsigned int) i); + } + if (i == 0 && crypto_aead_aes256gcm_decrypt(NULL, NULL, + NULL, ciphertext, ciphertext_len, + ad, ad_len, nonce, key) != 0) { + printf("Verification of test vector #%u's tag failed\n", (unsigned int) i); + } + if (crypto_aead_aes256gcm_decrypt(decrypted, &found_message_len, + NULL, ciphertext, ciphertext_len, + ad, ad_len, nonce, key) != 0) { + printf("Verification of test vector #%u failed\n", (unsigned int) i); + } + assert((size_t) found_message_len == message_len); + if (memcmp(decrypted, message, message_len) != 0) { + printf("Incorrect decryption of test vector #%u\n", (unsigned int) i); + } + memset(decrypted, 0xd0, message_len); + if (crypto_aead_aes256gcm_decrypt_detached(decrypted, + NULL, detached_ciphertext, + detached_ciphertext_len, + mac, ad, ad_len, nonce, key) != 0) { + printf("Detached verification of test vector #%u failed\n", (unsigned int) i); + } + if (memcmp(decrypted, message, message_len) != 0) { + printf("Incorrect decryption of test vector #%u\n", (unsigned int) i); + } + + sodium_free(message); + sodium_free(ad); + sodium_free(expected_ciphertext); + sodium_free(ciphertext); + sodium_free(decrypted); + sodium_free(detached_ciphertext); + } while (++i < (sizeof tests) / (sizeof tests[0])); + + sodium_free(key); + sodium_free(mac); + sodium_free(nonce); + + return 0; +} + +int +main(void) +{ + if (crypto_aead_aes256gcm_is_available()) { + tv(); + } + assert(crypto_aead_aes256gcm_keybytes() == crypto_aead_aes256gcm_KEYBYTES); + assert(crypto_aead_aes256gcm_nsecbytes() == crypto_aead_aes256gcm_NSECBYTES); + assert(crypto_aead_aes256gcm_npubbytes() == crypto_aead_aes256gcm_NPUBBYTES); + assert(crypto_aead_aes256gcm_abytes() == crypto_aead_aes256gcm_ABYTES); + assert(crypto_aead_aes256gcm_statebytes() >= sizeof(crypto_aead_aes256gcm_state)); + assert(crypto_aead_aes256gcm_messagebytes_max() == crypto_aead_aes256gcm_MESSAGEBYTES_MAX); + printf("OK\n"); + + return 0; +} diff --git a/aesjni/src/main/jni/aes.c b/aesjni/src/main/jni/aes.c deleted file mode 100644 index 5fd2197..0000000 --- a/aesjni/src/main/jni/aes.c +++ /dev/null @@ -1,594 +0,0 @@ -/* - -This is an implementation of the AES128 algorithm, specifically ECB and CBC mode. - -The implementation is verified against the test vectors in: - National Institute of Standards and Technology Special Publication 800-38A 2001 ED - -ECB-AES128 ----------- - - plain-text: - 6bc1bee22e409f96e93d7e117393172a - ae2d8a571e03ac9c9eb76fac45af8e51 - 30c81c46a35ce411e5fbc1191a0a52ef - f69f2445df4f9b17ad2b417be66c3710 - - key: - 2b7e151628aed2a6abf7158809cf4f3c - - resulting cipher - 3ad77bb40d7a3660a89ecaf32466ef97 - f5d3d58503b9699de785895a96fdbaaf - 43b1cd7f598ece23881b00e3ed030688 - 7b0c785e27e8ad3f8223207104725dd4 - - -NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0) - You should pad the end of the string with zeros if this is not the case. - -*/ - - -/*****************************************************************************/ -/* Includes: */ -/*****************************************************************************/ - -#include "aes.h" - - -/*****************************************************************************/ -/* Defines: */ -/*****************************************************************************/ -// The number of columns comprising a state in AES. This is a constant in AES. Value=4 -#define Nb 4 -// The number of 32 bit words in a key. -#define Nk 4 -// Key length in bytes [128 bit] -#define KEYLEN 16 -// The number of rounds in AES Cipher. -#define Nr 10 - -// jcallan@github points out that declaring Multiply as a function -// reduces code size considerably with the Keil ARM compiler. -// See this link for more information: https://github.com/kokke/tiny-AES128-C/pull/3 -#ifndef MULTIPLY_AS_A_FUNCTION -#define MULTIPLY_AS_A_FUNCTION 0 -#endif - - -/*****************************************************************************/ -/* Private variables: */ -/*****************************************************************************/ -// state - array holding the intermediate results during decryption. -typedef uint8_t state_t[4][4]; -static state_t* state; - -// The array that stores the round keys. -static uint8_t RoundKey[176]; - -// The Key input to the AES Program -static const uint8_t* Key; -#if defined(CBC) && CBC -// Initial Vector used only for CBC mode -static uint8_t* Iv; -#endif - -// The lookup-tables are marked const so they can be placed in read-only storage instead of RAM -// The numbers below can be computed dynamically trading ROM for RAM - -// This can be useful in (embedded) bootloader applications, where ROM is often limited. -static const uint8_t sbox[256] = { - //0 1 2 3 4 5 6 7 8 9 A B C D E F - 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, - 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, - 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, - 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, - 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, - 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, - 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, - 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, - 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, - 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, - 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, - 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, - 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, - 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, - 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, - 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; - -static const uint8_t rsbox[256] = - { 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, - 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, - 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, - 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, - 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, - 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, - 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, - 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, - 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, - 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, - 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, - 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, - 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, - 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, - 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, - 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d }; - - -// The round constant word array, Rcon[i], contains the values given by -// x to th e power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8) -// Note that i starts at 1, not 0). -static const uint8_t Rcon[255] = { - 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, - 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, - 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, - 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, - 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, - 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, - 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, - 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, - 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, - 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, - 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, - 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, - 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, - 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, - 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, - 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb }; - - -/*****************************************************************************/ -/* Private functions: */ -/*****************************************************************************/ -static uint8_t getSBoxValue(uint8_t num) -{ - return sbox[num]; -} - -static uint8_t getSBoxInvert(uint8_t num) -{ - return rsbox[num]; -} - -// This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states. -static void KeyExpansion(void) -{ - uint32_t i, j, k; - uint8_t tempa[4]; // Used for the column/row operations - - // The first round key is the key itself. - for(i = 0; i < Nk; ++i) - { - RoundKey[(i * 4) + 0] = Key[(i * 4) + 0]; - RoundKey[(i * 4) + 1] = Key[(i * 4) + 1]; - RoundKey[(i * 4) + 2] = Key[(i * 4) + 2]; - RoundKey[(i * 4) + 3] = Key[(i * 4) + 3]; - } - - // All other round keys are found from the previous round keys. - for(; (i < (Nb * (Nr + 1))); ++i) - { - for(j = 0; j < 4; ++j) - { - tempa[j]=RoundKey[(i-1) * 4 + j]; - } - if (i % Nk == 0) - { - // This function rotates the 4 bytes in a word to the left once. - // [a0,a1,a2,a3] becomes [a1,a2,a3,a0] - - // Function RotWord() - { - k = tempa[0]; - tempa[0] = tempa[1]; - tempa[1] = tempa[2]; - tempa[2] = tempa[3]; - tempa[3] = k; - } - - // SubWord() is a function that takes a four-byte input word and - // applies the S-box to each of the four bytes to produce an output word. - - // Function Subword() - { - tempa[0] = getSBoxValue(tempa[0]); - tempa[1] = getSBoxValue(tempa[1]); - tempa[2] = getSBoxValue(tempa[2]); - tempa[3] = getSBoxValue(tempa[3]); - } - - tempa[0] = tempa[0] ^ Rcon[i/Nk]; - } - else if (Nk > 6 && i % Nk == 4) - { - // Function Subword() - { - tempa[0] = getSBoxValue(tempa[0]); - tempa[1] = getSBoxValue(tempa[1]); - tempa[2] = getSBoxValue(tempa[2]); - tempa[3] = getSBoxValue(tempa[3]); - } - } - RoundKey[i * 4 + 0] = RoundKey[(i - Nk) * 4 + 0] ^ tempa[0]; - RoundKey[i * 4 + 1] = RoundKey[(i - Nk) * 4 + 1] ^ tempa[1]; - RoundKey[i * 4 + 2] = RoundKey[(i - Nk) * 4 + 2] ^ tempa[2]; - RoundKey[i * 4 + 3] = RoundKey[(i - Nk) * 4 + 3] ^ tempa[3]; - } -} - -// This function adds the round key to state. -// The round key is added to the state by an XOR function. -static void AddRoundKey(uint8_t round) -{ - uint8_t i,j; - for(i=0;i<4;++i) - { - for(j = 0; j < 4; ++j) - { - (*state)[i][j] ^= RoundKey[round * Nb * 4 + i * Nb + j]; - } - } -} - -// The SubBytes Function Substitutes the values in the -// state matrix with values in an S-box. -static void SubBytes(void) -{ - uint8_t i, j; - for(i = 0; i < 4; ++i) - { - for(j = 0; j < 4; ++j) - { - (*state)[j][i] = getSBoxValue((*state)[j][i]); - } - } -} - -// The ShiftRows() function shifts the rows in the state to the left. -// Each row is shifted with different offset. -// Offset = Row number. So the first row is not shifted. -static void ShiftRows(void) -{ - uint8_t temp; - - // Rotate first row 1 columns to left - temp = (*state)[0][1]; - (*state)[0][1] = (*state)[1][1]; - (*state)[1][1] = (*state)[2][1]; - (*state)[2][1] = (*state)[3][1]; - (*state)[3][1] = temp; - - // Rotate second row 2 columns to left - temp = (*state)[0][2]; - (*state)[0][2] = (*state)[2][2]; - (*state)[2][2] = temp; - - temp = (*state)[1][2]; - (*state)[1][2] = (*state)[3][2]; - (*state)[3][2] = temp; - - // Rotate third row 3 columns to left - temp = (*state)[0][3]; - (*state)[0][3] = (*state)[3][3]; - (*state)[3][3] = (*state)[2][3]; - (*state)[2][3] = (*state)[1][3]; - (*state)[1][3] = temp; -} - -static uint8_t xtime(uint8_t x) -{ - return ((x<<1) ^ (((x>>7) & 1) * 0x1b)); -} - -// MixColumns function mixes the columns of the state matrix -static void MixColumns(void) -{ - uint8_t i; - uint8_t Tmp,Tm,t; - for(i = 0; i < 4; ++i) - { - t = (*state)[i][0]; - Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ; - Tm = (*state)[i][0] ^ (*state)[i][1] ; Tm = xtime(Tm); (*state)[i][0] ^= Tm ^ Tmp ; - Tm = (*state)[i][1] ^ (*state)[i][2] ; Tm = xtime(Tm); (*state)[i][1] ^= Tm ^ Tmp ; - Tm = (*state)[i][2] ^ (*state)[i][3] ; Tm = xtime(Tm); (*state)[i][2] ^= Tm ^ Tmp ; - Tm = (*state)[i][3] ^ t ; Tm = xtime(Tm); (*state)[i][3] ^= Tm ^ Tmp ; - } -} - -// Multiply is used to multiply numbers in the field GF(2^8) -#if MULTIPLY_AS_A_FUNCTION -static uint8_t Multiply(uint8_t x, uint8_t y) -{ - return (((y & 1) * x) ^ - ((y>>1 & 1) * xtime(x)) ^ - ((y>>2 & 1) * xtime(xtime(x))) ^ - ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ - ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))); - } -#else -#define Multiply(x, y) \ - ( ((y & 1) * x) ^ \ - ((y>>1 & 1) * xtime(x)) ^ \ - ((y>>2 & 1) * xtime(xtime(x))) ^ \ - ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ \ - ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))) \ - -#endif - -// MixColumns function mixes the columns of the state matrix. -// The method used to multiply may be difficult to understand for the inexperienced. -// Please use the references to gain more information. -static void InvMixColumns(void) -{ - int i; - uint8_t a,b,c,d; - for(i=0;i<4;++i) - { - a = (*state)[i][0]; - b = (*state)[i][1]; - c = (*state)[i][2]; - d = (*state)[i][3]; - - (*state)[i][0] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09); - (*state)[i][1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d); - (*state)[i][2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b); - (*state)[i][3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e); - } -} - - -// The SubBytes Function Substitutes the values in the -// state matrix with values in an S-box. -static void InvSubBytes(void) -{ - uint8_t i,j; - for(i=0;i<4;++i) - { - for(j=0;j<4;++j) - { - (*state)[j][i] = getSBoxInvert((*state)[j][i]); - } - } -} - -static void InvShiftRows(void) -{ - uint8_t temp; - - // Rotate first row 1 columns to right - temp=(*state)[3][1]; - (*state)[3][1]=(*state)[2][1]; - (*state)[2][1]=(*state)[1][1]; - (*state)[1][1]=(*state)[0][1]; - (*state)[0][1]=temp; - - // Rotate second row 2 columns to right - temp=(*state)[0][2]; - (*state)[0][2]=(*state)[2][2]; - (*state)[2][2]=temp; - - temp=(*state)[1][2]; - (*state)[1][2]=(*state)[3][2]; - (*state)[3][2]=temp; - - // Rotate third row 3 columns to right - temp=(*state)[0][3]; - (*state)[0][3]=(*state)[1][3]; - (*state)[1][3]=(*state)[2][3]; - (*state)[2][3]=(*state)[3][3]; - (*state)[3][3]=temp; -} - - -// Cipher is the main function that encrypts the PlainText. -static void Cipher(void) -{ - uint8_t round = 0; - - // Add the First round key to the state before starting the rounds. - AddRoundKey(0); - - // There will be Nr rounds. - // The first Nr-1 rounds are identical. - // These Nr-1 rounds are executed in the loop below. - for(round = 1; round < Nr; ++round) - { - SubBytes(); - ShiftRows(); - MixColumns(); - AddRoundKey(round); - } - - // The last round is given below. - // The MixColumns function is not here in the last round. - SubBytes(); - ShiftRows(); - AddRoundKey(Nr); -} - -static void InvCipher(void) -{ - uint8_t round=0; - - // Add the First round key to the state before starting the rounds. - AddRoundKey(Nr); - - // There will be Nr rounds. - // The first Nr-1 rounds are identical. - // These Nr-1 rounds are executed in the loop below. - for(round=Nr-1;round>0;round--) - { - InvShiftRows(); - InvSubBytes(); - AddRoundKey(round); - InvMixColumns(); - } - - // The last round is given below. - // The MixColumns function is not here in the last round. - InvShiftRows(); - InvSubBytes(); - AddRoundKey(0); -} - -static void BlockCopy(uint8_t* output, uint8_t* input) -{ - uint8_t i; - for (i=0;istrlen) will crash. - memset(out+index, '\0', strlen(out)-index); - } - //去除结尾垃圾字符串 end - - - //LOGE("解密结果:"); - //LOGE(out); - free(inputDesBase64); - return (char *) out; -} - -/** - * 查找结果中的一些 多余字符串 - * @param str : 加密结果原文 - * @return int : 垃圾字符串的开始位置 - */ -int findPaddingIndex(uint8_t * str) -{ - return (int)(strlen(str) - str[strlen(str) - 1]); -} - - -/** - * - * 这里干掉了CBC 相关代码 ,这块代码是一个AES的一个带有向量的算法 - * 找寻这些代码 请移步 https://github.com/kokke/tiny-AES128-C - -#if defined(CBC) && CBC -#endif // #if defined(CBC) && CBC - -*/ - -#endif // #if defined(ECB) && ECB \ No newline at end of file diff --git a/aesjni/src/main/jni/aes.h b/aesjni/src/main/jni/aes.h deleted file mode 100644 index ede2d65..0000000 --- a/aesjni/src/main/jni/aes.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef _AES_H_ -#define _AES_H_ - -#include -#include -#include -#include -#include "base64.h" - -// #define the macros below to 1/0 to enable/disable the mode of operation. -// -// CBC enables AES128 encryption in CBC-mode of operation and handles 0-padding. -// ECB enables the basic ECB 16-byte block algorithm. Both can be enabled simultaneously. - -// The #ifndef-guard allows it to be configured before #include'ing or at compile time. -#ifndef CBC - #define CBC 1 -#endif - -#ifndef ECB - #define ECB 1 -#endif -#ifndef LOGE -#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,"native-jni",__VA_ARGS__) -#endif - -static const unsigned char HEX[16]={0x10,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}; - -//__attribute__((section (".mytext"))) -//static const uint8_t AES_KEY[]="1234567890abcdef"; - - -#if defined(ECB) && ECB - -void AES128_ECB_encrypt(uint8_t* input, const uint8_t* key, uint8_t *output); -void AES128_ECB_decrypt(uint8_t* input, const uint8_t* key, uint8_t *output); - -char* AES_128_ECB_PKCS5Padding_Encrypt(const char *in,const uint8_t *key); -char* AES_128_ECB_PKCS5Padding_Decrypt(const char *in, const uint8_t* key); - -int findPaddingIndex(uint8_t * str); -#endif // #if defined(ECB) && ECB - - -/* - * CBC是向量模式 暂不采用 - 如果需要使用 ,请移步 https://github.com/kokke/tiny-AES128-C - -#if defined(CBC) && CBC - -#endif // #if defined(CBC) && CBC - -*/ -#endif //_AES_H_ diff --git a/aesjni/src/main/jni/base64.c b/aesjni/src/main/jni/base64.c old mode 100644 new mode 100755 diff --git a/aesjni/src/main/jni/base64.h b/aesjni/src/main/jni/base64.h old mode 100644 new mode 100755 diff --git a/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh b/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh new file mode 100755 index 0000000..f713f1d --- /dev/null +++ b/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh @@ -0,0 +1,88 @@ +#!/bin/bash +# This shell script got success on some environments: +# ========================================================== +# 1.MacOS with ndk r13. +# 2.Ubuntu 20 with ndk-bundle. + +LIBSODIUM_GIT_TAG=1.0.18 # it is the last stable version. +ROOT_DIR=$(pwd) + +SODIUM_INCLUDE_DIR=$(pwd)/sodium_include +if [ -d $SODIUM_INCLUDE_DIR ]; then # if it exists,delete it. + rm -rf $SODIUM_INCLUDE_DIR +fi +mkdir sodium_include + +TEMP_DIR=$(pwd)/temp +if [ -d $TEMP_DIR ]; then # if it exists,delete it. + rm -rf $TEMP_DIR +fi +mkdir $TEMP_DIR + + + +SODIUM_CLONING_HOME=$TEMP_DIR/libsodium +# clone +git clone git@github.com:jedisct1/libsodium.git "$SODIUM_CLONING_HOME" +cd $SODIUM_CLONING_HOME +git fetch --tags +git checkout $LIBSODIUM_GIT_TAG # check specific verion. + + + +# && git pull + +# This can be removed once we pull from a release +if [ ! -e $SODIUM_CLONING_HOME/configure ]; then + ./autogen.sh +fi + +LIBSODIUM_ARMV6=$SODIUM_CLONING_HOME/libsodium-android-armv6 +LIBSODIUM_ARMV7A=$SODIUM_CLONING_HOME/libsodium-android-armv7-a +LIBSODIUM_I686=$SODIUM_CLONING_HOME/libsodium-android-i686 +LIBSODIUM_MIPS32=$SODIUM_CLONING_HOME/libsodium-android-mips32 +LIBSODIUM_ARMV8A=$SODIUM_CLONING_HOME/libsodium-android-armv8-a + +# Run the android builds +if [ ! -d $LIBSODIUM_ARMV6 ]; then + $SODIUM_CLONING_HOME/dist-build/android-arm.sh + echo "build-libsodium: built armv6!" +else + echo "build-libsodium: skipping armv6, already built!" +fi + +if [ ! -d $LIBSODIUM_ARMV7A ]; then + $SODIUM_CLONING_HOME/dist-build/android-armv7-a.sh + echo "build-libsodium: built armv7-a!" +else + echo "build-libsodium: skipping armv7-a, already built!" +fi + +if [ ! -d $LIBSODIUM_MIPS32 ]; then + $SODIUM_CLONING_HOME/dist-build/android-mips32.sh + echo "build-libsodium: built mips32!" +else + echo "build-libsodium: skipping mips32, already built!" +fi + +if [ ! -d $LIBSODIUM_I686 ]; then + $SODIUM_CLONING_HOME/dist-build/android-x86.sh + echo "build-libsodium: built x86!" +else + echo "build-libsodium: skipping x86, already built!" +fi + +if [ ! -d $LIBSODIUM_ARMV8A ]; then + $SODIUM_CLONING_HOME/dist-build/android-armv8-a.sh + echo "build-libsodium: built armv8-a!" +else + echo "build-libsodium: skipping armv8-a, already built!" +fi + + +if [ $? -eq 0 ]; then + mv -v $SODIUM_CLONING_HOME/libsodium-android-* $SODIUM_INCLUDE_DIR + printf 'all static libs move into %s.\n' $SODIUM_INCLUDE_DIR + rm -rf $TEMP_DIR +fi + diff --git a/aesjni/src/main/jni/check_emulator.c b/aesjni/src/main/jni/check_emulator.c old mode 100644 new mode 100755 diff --git a/aesjni/src/main/jni/check_emulator.h b/aesjni/src/main/jni/check_emulator.h old mode 100644 new mode 100755 diff --git a/aesjni/src/main/jni/checksignature.c b/aesjni/src/main/jni/checksignature.c old mode 100644 new mode 100755 diff --git a/aesjni/src/main/jni/checksignature.h b/aesjni/src/main/jni/checksignature.h old mode 100644 new mode 100755 diff --git a/aesjni/src/main/jni/debugger.c b/aesjni/src/main/jni/debugger.c old mode 100644 new mode 100755 diff --git a/aesjni/src/main/jni/debugger.h b/aesjni/src/main/jni/debugger.h old mode 100644 new mode 100755 diff --git a/aesjni/src/main/jni/logger.h b/aesjni/src/main/jni/logger.h old mode 100644 new mode 100755 diff --git a/aesjni/src/main/jni/main_unit_test.c b/aesjni/src/main/jni/main_unit_test.c new file mode 100755 index 0000000..865bded --- /dev/null +++ b/aesjni/src/main/jni/main_unit_test.c @@ -0,0 +1,22 @@ +/*=================================================== +this file is used to test native codes. +you can run a command in this directory: ./rebuild.sh test +===================================================*/ + +#define TEST_NAME "aead_aes256gcm" +#include +#include + +//run adb logcat | grep NATIVE_UT +#define TAG "NATIVE_UT" +void logd(char * str){ + __android_log_print(ANDROID_LOG_DEBUG , TAG, "%s", str); // the 3rd arg is a printf-style format string +} + +int main() { + printf("main() running.\n");//this string will be got from you command, + logd("test");//run adb logcat | grep NATIVE_UT + return 0; +} + + diff --git a/aesjni/src/main/jni/rebuild.sh b/aesjni/src/main/jni/rebuild.sh old mode 100644 new mode 100755 index 4057b08..3dc3e6f --- a/aesjni/src/main/jni/rebuild.sh +++ b/aesjni/src/main/jni/rebuild.sh @@ -1,7 +1,39 @@ #!/usr/bin/env bash - +# @author bruce: +# ========================Remind:======================== +# run this shell script must before, you computer must be set ANROID_NDK_HOME, +# And your computer must connect with a armv8 phone,or armv8 emulator beacause only build armv8 share library. +# ------------------------------------------------------- +# Build success environmental system records: +# Mac OS: +# It has been built success with NDK r19c. +# Ubuntu 20.0 : +# It has been built success with ndk-bundle. You could download sdk,then use sdkmanger command to downlaod ndk-bundle. +# And NDK-r13b got failed due to google not supporting the old ndk to be with new system. +# clear rm -r ../obj/ rm -r ../libs/ ndk-build clean -ndk-build \ No newline at end of file + +## run "./rebuild.sh test" +if [[ $1 == "test" ]]; then + printf "Start building......\n" + cp -f TestAndroid.mk Android.mk + ndk-build + if [ $? -ne 0 ]; then + exit # failed at ndk-build + fi + printf "adb push......\n" + adb push ../libs/arm64-v8a/test /data/local/tmp + printf "This is executable file print:\n" + printf "=========================\n" + adb shell /data/local/tmp/test + printf "=========================\n" +else + cp -f OriginAndroid.mk Android.mk + ndk-build +fi + + + diff --git a/aesjni/src/main/jni/sodium.i b/aesjni/src/main/jni/sodium.i new file mode 100755 index 0000000..61f5bc7 --- /dev/null +++ b/aesjni/src/main/jni/sodium.i @@ -0,0 +1,294 @@ +/* sodium.i */ +%module Sodium +%include "typemaps.i" + +/** + * Defines unsigned char * (C) -> byte[] (Java) + * + * Following is taken from SWIG's various.i and adapted for unsigned chars + */ +%typemap(jni) unsigned char *BYTE "jbyteArray" +%typemap(jtype) unsigned char *BYTE "byte[]" +%typemap(jstype) unsigned char *BYTE "byte[]" +%typemap(in) unsigned char *BYTE { + $1 = (unsigned char *) JCALL2(GetByteArrayElements, jenv, $input, 0); +} + +%typemap(argout) unsigned char *BYTE { + JCALL3(ReleaseByteArrayElements, jenv, $input, (jbyte *) $1, 0); +} + +%typemap(javain) unsigned char *BYTE "$javainput" + +/* Prevent default freearg typemap from being used */ +%typemap(freearg) unsigned char *BYTE "" + +%apply unsigned char *BYTE { unsigned char * }; + +/* See Above. + * Previous is taken from SWIG's various.i and adapted for unsigned chars + **/ + + +/* SWIG uses BigInts for unsigned long longs, but BigInts are obnoxious + * and so we'd like to use a java long. typemaps.i defines unsigned long as a java long, + * so although we do lose 'unsigned', hopefully we can rely on the callers not to pass in + * a negative size. + */ +%apply unsigned long { unsigned long long }; + +/* Since this is only used by the randombytes (so far), then we're limited by an int (buf.length) */ +%apply int { size_t }; + +%apply int { uint32_t }; +%apply unsigned char *BYTE { void * const }; + +/* Following needed to make sure we don't use String type for hashes */ +%apply unsigned char *BYTE { const char str[crypto_pwhash_scryptsalsa208sha256_STRBYTES] }; +%apply unsigned char *BYTE { char out[crypto_pwhash_scryptsalsa208sha256_STRBYTES] }; + +%javaconst(1); + +/* Start Sodium Definitions */ +%{ +#include "sodium.h" +%} + +/* core.h */ +int sodium_init(void); + +const char *sodium_version_string(void); + + +/* randombytes.h */ + +void randombytes_buf(void * const buf, const size_t size); + +uint32_t randombytes_random(void); + +uint32_t randombytes_uniform(const uint32_t upper_bound); + +/* crypto_sign.h & crypto_sign_*.h */ + +#define CRYPTO_SIGN_BYTES 64 +#define CRYPTO_SIGN_SEEDBYTES 32 +#define CRYPTO_SIGN_PUBLICKEYBYTES 32 +#define CRYPTO_SIGN_SECRETKEYBYTES (32 + 32) + +int crypto_sign_keypair(unsigned char *pk, unsigned char *sk); + +int crypto_sign_seed_keypair(unsigned char *pk, unsigned char *sk, + const unsigned char *seed); + +int crypto_sign(unsigned char *sm, unsigned long long *smlem, + const unsigned char *m, unsigned long long mlen, + const unsigned char *sk); + +int crypto_sign_open(unsigned char *m, unsigned long long *mlen, + const unsigned char *sm, unsigned long long smlen, + const unsigned char *pk); + +int crypto_sign_detached(unsigned char *sig, unsigned long long *slen, + const unsigned char *m, unsigned long long mlen, + const unsigned char *sk); + + +int crypto_sign_ed25519_sk_to_curve25519(unsigned char *curve25519_sk, + const unsigned char *ed25519_sk); + +int crypto_sign_ed25519_pk_to_curve25519(unsigned char *curve25519_pk, + const unsigned char *ed25519_pk); + +int crypto_sign_verify_detached(const unsigned char *sig, + const unsigned char *m, + unsigned long long mlen, + const unsigned char *pk); + +int crypto_sign_ed25519_sk_to_seed(unsigned char *seed, + const unsigned char *sk); + +int crypto_sign_ed25519_sk_to_pk(unsigned char *pk, const unsigned char *sk); + +/* crypto_box.h */ + +#define CRYPTO_BOX_SEEDBYTES 32 +#define CRYPTO_BOX_PUBLICKEYBYTES 32 +#define CRYPTO_BOX_SECRETKEYBYTES 32 +#define CRYPTO_BOX_NONCEBYTES 24 +#define CRYPTO_BOX_MACBYTES 16 + +int crypto_box_seed_keypair(unsigned char *pk, unsigned char *sk, + const unsigned char *seed); + +int crypto_box_keypair(unsigned char *pk, unsigned char *sk); + +int crypto_box_easy(unsigned char *c, const unsigned char *m, + unsigned long long mlen, const unsigned char *n, + const unsigned char *pk, const unsigned char *sk); + + +int crypto_box_open_easy(unsigned char *m, const unsigned char *c, + unsigned long long clen, const unsigned char *n, + const unsigned char *pk, const unsigned char *sk); + +int crypto_box_detached(unsigned char *c, unsigned char *mac, + const unsigned char *m, unsigned long long mlen, + const unsigned char *n, const unsigned char *pk, + const unsigned char *sk); + +int crypto_box_open_detached(unsigned char *m, const unsigned char *c, + const unsigned char *mac, + unsigned long long clen, + const unsigned char *n, + const unsigned char *pk, + const unsigned char *sk); + + +/* crypto_secret_box.h & crypto_secret_box_*.h */ + +#define CRYPTO_SECRETBOX_KEYBYTES 32 +#define CRYPTO_SECRETBOX_MACBYTES 16 +#define CRYPTO_SECRETBOX_NONCEBYTES 24 + +int crypto_secretbox_easy(unsigned char *c, const unsigned char *m, + unsigned long long mlen, const unsigned char *n, + const unsigned char *k); + +int crypto_secretbox_open_easy(unsigned char *m, const unsigned char *c, + unsigned long long clen, const unsigned char *n, + const unsigned char *k); + +int crypto_secretbox_detached(unsigned char *c, unsigned char *mac, + const unsigned char *m, + unsigned long long mlen, + const unsigned char *n, + const unsigned char *k); + +int crypto_secretbox_open_detached(unsigned char *m, + const unsigned char *c, + const unsigned char *mac, + unsigned long long clen, + const unsigned char *n, + const unsigned char *k); + +/* crypto_auth.h */ + +#define CRYPTO_AUTH_BYTES 32 +#define CRYPTO_AUTH_KEYBYTES 32 + +int crypto_auth(unsigned char *out, const unsigned char *in, + unsigned long long inlen, const unsigned char *k); + + +int crypto_auth_verify(const unsigned char *h, const unsigned char *in, + unsigned long long inlen, const unsigned char *k); + +/* crypto_aead.h */ + +#define CRYPTO_AED_CHACHA20POLY1305_KEYBYTES 32 +#define CRYPTO_AED_CHACHA20POLY1305_NONCEBYTES 32 +#define CRYPTO_AED_CHACHA20POLY1305_MACBYTES 16 + +int crypto_aead_chacha20poly1305_encrypt(unsigned char *c, + unsigned long long *clen, + const unsigned char *m, + unsigned long long mlen, + const unsigned char *ad, + unsigned long long adlen, + const unsigned char *nsec, + const unsigned char *npub, + const unsigned char *k); + +int crypto_aead_chacha20poly1305_decrypt(unsigned char *m, + unsigned long long *mlen, + unsigned char *nsec, + const unsigned char *c, + unsigned long long clen, + const unsigned char *ad, + unsigned long long adlen, + const unsigned char *npub, + const unsigned char *k); + +/* crypto_pwhash_*.h */ + +#define CRYPTO_PWHASH_SALTBYTES 32 +#define CRYPTO_PWHASH_HASHBYTES 102 +#define CRYPTO_PWHASH_KEY_BYTES 32 + +#define CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE 524288 +#define CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE 16777216 + +#define CRYPTO_PWHASH_OPSLIMIT_SENSITIVE 33554432 +#define CRYPTO_PWHASH_MEMLIMIT_SENSITIVE 1073741824 + +int crypto_pwhash_scryptsalsa208sha256(unsigned char * const out, + unsigned long long outlen, + const char * const passwd, + unsigned long long passwdlen, + const unsigned char * const salt, + unsigned long long opslimit, + size_t memlimit); + +int crypto_pwhash_scryptsalsa208sha256_str(char out[crypto_pwhash_scryptsalsa208sha256_STRBYTES], + const char * const passwd, + unsigned long long passwdlen, + unsigned long long opslimit, + size_t memlimit); + +int crypto_pwhash_scryptsalsa208sha256_str_verify(const char str[crypto_pwhash_scryptsalsa208sha256_STRBYTES], + const char * const passwd, + unsigned long long passwdlen); + +/* crypto_shorthash.h */ + +#define CRYPTO_SHORTHASH_BYTES 8 +#define CRYPTO_SHORTHASH_KEYBYTES 16 + +int crypto_shorthash(unsigned char *out, const unsigned char *in, + unsigned long long inlen, const unsigned char *k); + + +/* crypto_generichash.h */ + +#define CRYPTO_GENERICHASH_BYTES 32 +#define CRYPTO_GENERICHASH_KEYBYTES 32 + +int crypto_generichash(unsigned char *out, size_t outlen, + const unsigned char *in, unsigned long long inlen, + const unsigned char *key, size_t keylen); + + +/* TODO: Need to figure out how to represent crypto_generichash_state with SWIG + +int crypto_generichash_init(crypto_generichash_state *state, + const unsigned char *key, + const size_t keylen, const size_t outlen); + +int crypto_generichash_update(crypto_generichash_state *state, + const unsigned char *in, + unsigned long long inlen); + +int crypto_generichash_final(crypto_generichash_state *state, + unsigned char *out, const size_t outlen); + +*/ + + +/* */ + +/* Static library loader */ + +%pragma(java) jniclasscode = %{ + /* Load JNI library */ + static { + try { + System.loadLibrary("JNIEncrypt"); + if (sodium_init() == -1) { + throw new RuntimeException("Sodium could not be initialized."); + } + } catch (Exception e) { + e.printStackTrace(); + System.exit(1); + } + } +%} diff --git a/aesjni/src/main/jni/sodium_include/README.md b/aesjni/src/main/jni/sodium_include/README.md new file mode 100644 index 0000000..0bb0c76 --- /dev/null +++ b/aesjni/src/main/jni/sodium_include/README.md @@ -0,0 +1,8 @@ +### you need to generate libsodium static link file for other abis. +All of file below this directory was generated. +I only put armv8-abi into there that is not enough to be a release project.You need to generate all abis static file of **libsodium** into there. +You can delete all that, and regenerate from [libsodum](https://github.com/jedisct1/libsodium). + +### libsodium version +Other information what I need to remind was that the **libsodium static file** which I built was **v1.0.18**. +That version is the last stable version up to now. diff --git a/aesjni/src/main/jni/sodium_wrap.c b/aesjni/src/main/jni/sodium_wrap.c new file mode 100755 index 0000000..54fc1fe --- /dev/null +++ b/aesjni/src/main/jni/sodium_wrap.c @@ -0,0 +1,1512 @@ +/* ---------------------------------------------------------------------------- + * This file was automatically generated by SWIG (http://www.swig.org). + * Version 4.0.2 + * + * This file is not intended to be easily readable and contains a number of + * coding conventions designed to improve portability and efficiency. Do not make + * changes to this file unless you know what you are doing--modify the SWIG + * interface file instead. + * ----------------------------------------------------------------------------- */ + + +#ifndef SWIGJAVA +#define SWIGJAVA +#endif + + +/* ----------------------------------------------------------------------------- + * This section contains generic SWIG labels for method/variable + * declarations/attributes, and other compiler dependent labels. + * ----------------------------------------------------------------------------- */ + +/* template workaround for compilers that cannot correctly implement the C++ standard */ +#ifndef SWIGTEMPLATEDISAMBIGUATOR +# if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x560) +# define SWIGTEMPLATEDISAMBIGUATOR template +# elif defined(__HP_aCC) +/* Needed even with `aCC -AA' when `aCC -V' reports HP ANSI C++ B3910B A.03.55 */ +/* If we find a maximum version that requires this, the test would be __HP_aCC <= 35500 for A.03.55 */ +# define SWIGTEMPLATEDISAMBIGUATOR template +# else +# define SWIGTEMPLATEDISAMBIGUATOR +# endif +#endif + +/* inline attribute */ +#ifndef SWIGINLINE +# if defined(__cplusplus) || (defined(__GNUC__) && !defined(__STRICT_ANSI__)) +# define SWIGINLINE inline +# else +# define SWIGINLINE +# endif +#endif + +/* attribute recognised by some compilers to avoid 'unused' warnings */ +#ifndef SWIGUNUSED +# if defined(__GNUC__) +# if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) +# define SWIGUNUSED __attribute__ ((__unused__)) +# else +# define SWIGUNUSED +# endif +# elif defined(__ICC) +# define SWIGUNUSED __attribute__ ((__unused__)) +# else +# define SWIGUNUSED +# endif +#endif + +#ifndef SWIG_MSC_UNSUPPRESS_4505 +# if defined(_MSC_VER) +# pragma warning(disable : 4505) /* unreferenced local function has been removed */ +# endif +#endif + +#ifndef SWIGUNUSEDPARM +# ifdef __cplusplus +# define SWIGUNUSEDPARM(p) +# else +# define SWIGUNUSEDPARM(p) p SWIGUNUSED +# endif +#endif + +/* internal SWIG method */ +#ifndef SWIGINTERN +# define SWIGINTERN static SWIGUNUSED +#endif + +/* internal inline SWIG method */ +#ifndef SWIGINTERNINLINE +# define SWIGINTERNINLINE SWIGINTERN SWIGINLINE +#endif + +/* exporting methods */ +#if defined(__GNUC__) +# if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) +# ifndef GCC_HASCLASSVISIBILITY +# define GCC_HASCLASSVISIBILITY +# endif +# endif +#endif + +#ifndef SWIGEXPORT +# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) +# if defined(STATIC_LINKED) +# define SWIGEXPORT +# else +# define SWIGEXPORT __declspec(dllexport) +# endif +# else +# if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY) +# define SWIGEXPORT __attribute__ ((visibility("default"))) +# else +# define SWIGEXPORT +# endif +# endif +#endif + +/* calling conventions for Windows */ +#ifndef SWIGSTDCALL +# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) +# define SWIGSTDCALL __stdcall +# else +# define SWIGSTDCALL +# endif +#endif + +/* Deal with Microsoft's attempt at deprecating C standard runtime functions */ +#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE) +# define _CRT_SECURE_NO_DEPRECATE +#endif + +/* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */ +#if !defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_SCL_SECURE_NO_DEPRECATE) +# define _SCL_SECURE_NO_DEPRECATE +#endif + +/* Deal with Apple's deprecated 'AssertMacros.h' from Carbon-framework */ +#if defined(__APPLE__) && !defined(__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES) +# define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0 +#endif + +/* Intel's compiler complains if a variable which was never initialised is + * cast to void, which is a common idiom which we use to indicate that we + * are aware a variable isn't used. So we just silence that warning. + * See: https://github.com/swig/swig/issues/192 for more discussion. + */ +#ifdef __INTEL_COMPILER +# pragma warning disable 592 +#endif + + +/* Fix for jlong on some versions of gcc on Windows */ +#if defined(__GNUC__) && !defined(__INTEL_COMPILER) + typedef long long __int64; +#endif + +/* Fix for jlong on 64-bit x86 Solaris */ +#if defined(__x86_64) +# ifdef _LP64 +# undef _LP64 +# endif +#endif + +#include +#include +#include + + +/* Support for throwing Java exceptions */ +typedef enum { + SWIG_JavaOutOfMemoryError = 1, + SWIG_JavaIOException, + SWIG_JavaRuntimeException, + SWIG_JavaIndexOutOfBoundsException, + SWIG_JavaArithmeticException, + SWIG_JavaIllegalArgumentException, + SWIG_JavaNullPointerException, + SWIG_JavaDirectorPureVirtual, + SWIG_JavaUnknownError, + SWIG_JavaIllegalStateException, +} SWIG_JavaExceptionCodes; + +typedef struct { + SWIG_JavaExceptionCodes code; + const char *java_exception; +} SWIG_JavaExceptions_t; + + +static void SWIGUNUSED SWIG_JavaThrowException(JNIEnv *jenv, SWIG_JavaExceptionCodes code, const char *msg) { + jclass excep; + static const SWIG_JavaExceptions_t java_exceptions[] = { + { SWIG_JavaOutOfMemoryError, "java/lang/OutOfMemoryError" }, + { SWIG_JavaIOException, "java/io/IOException" }, + { SWIG_JavaRuntimeException, "java/lang/RuntimeException" }, + { SWIG_JavaIndexOutOfBoundsException, "java/lang/IndexOutOfBoundsException" }, + { SWIG_JavaArithmeticException, "java/lang/ArithmeticException" }, + { SWIG_JavaIllegalArgumentException, "java/lang/IllegalArgumentException" }, + { SWIG_JavaNullPointerException, "java/lang/NullPointerException" }, + { SWIG_JavaDirectorPureVirtual, "java/lang/RuntimeException" }, + { SWIG_JavaUnknownError, "java/lang/UnknownError" }, + { SWIG_JavaIllegalStateException, "java/lang/IllegalStateException" }, + { (SWIG_JavaExceptionCodes)0, "java/lang/UnknownError" } + }; + const SWIG_JavaExceptions_t *except_ptr = java_exceptions; + + while (except_ptr->code != code && except_ptr->code) + except_ptr++; + + (*jenv)->ExceptionClear(jenv); + excep = (*jenv)->FindClass(jenv, except_ptr->java_exception); + if (excep) + (*jenv)->ThrowNew(jenv, excep, msg); +} + + +/* Contract support */ + +#define SWIG_contract_assert(nullreturn, expr, msg) if (!(expr)) {SWIG_JavaThrowException(jenv, SWIG_JavaIllegalArgumentException, msg); return nullreturn; } else + + +#include "sodium.h" + + +#ifdef __cplusplus +extern "C" { +#endif + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_sodium_1init(JNIEnv *jenv, jclass jcls) { + jint jresult = 0 ; + int result; + + (void)jenv; + (void)jcls; + result = (int)sodium_init(); + jresult = (jint)result; + return jresult; +} + + +SWIGEXPORT jstring JNICALL Java_com_androidyuan_aesjni_SodiumJNI_sodium_1version_1string(JNIEnv *jenv, jclass jcls) { + jstring jresult = 0 ; + char *result = 0 ; + + (void)jenv; + (void)jcls; + result = (char *)sodium_version_string(); + if (result) jresult = (*jenv)->NewStringUTF(jenv, (const char *)result); + return jresult; +} + + +SWIGEXPORT void JNICALL Java_com_androidyuan_aesjni_SodiumJNI_randombytes_1buf(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jint jarg2) { + void *arg1 = (void *) (void *)0 ; + size_t arg2 ; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + arg2 = (size_t)jarg2; + randombytes_buf(arg1,arg2); + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_randombytes_1random(JNIEnv *jenv, jclass jcls) { + jint jresult = 0 ; + uint32_t result; + + (void)jenv; + (void)jcls; + result = randombytes_random(); + jresult = (jint)result; + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_randombytes_1uniform(JNIEnv *jenv, jclass jcls, jint jarg1) { + jint jresult = 0 ; + uint32_t arg1 ; + uint32_t result; + + (void)jenv; + (void)jcls; + arg1 = (uint32_t)jarg1; + result = randombytes_uniform(arg1); + jresult = (jint)result; + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1keypair(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + result = (int)crypto_sign_keypair(arg1,arg2); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1seed_1keypair(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jbyteArray jarg3) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + unsigned char *arg3 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + { + arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); + } + result = (int)crypto_sign_seed_keypair(arg1,arg2,(unsigned char const *)arg3); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); + } + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jlong jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned long long *arg2 = (unsigned long long *) 0 ; + unsigned char *arg3 = (unsigned char *) 0 ; + unsigned long long arg4 ; + unsigned char *arg5 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + arg2 = *(unsigned long long **)&jarg2; + { + arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); + } + arg4 = (unsigned long long)jarg4; + { + arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); + } + result = (int)crypto_sign(arg1,arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); + } + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1open(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jlong jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned long long *arg2 = (unsigned long long *) 0 ; + unsigned char *arg3 = (unsigned char *) 0 ; + unsigned long long arg4 ; + unsigned char *arg5 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + arg2 = *(unsigned long long **)&jarg2; + { + arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); + } + arg4 = (unsigned long long)jarg4; + { + arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); + } + result = (int)crypto_sign_open(arg1,arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); + } + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1detached(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jlong jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned long long *arg2 = (unsigned long long *) 0 ; + unsigned char *arg3 = (unsigned char *) 0 ; + unsigned long long arg4 ; + unsigned char *arg5 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + arg2 = *(unsigned long long **)&jarg2; + { + arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); + } + arg4 = (unsigned long long)jarg4; + { + arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); + } + result = (int)crypto_sign_detached(arg1,arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); + } + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1ed25519_1sk_1to_1curve25519(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + result = (int)crypto_sign_ed25519_sk_to_curve25519(arg1,(unsigned char const *)arg2); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1ed25519_1pk_1to_1curve25519(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + result = (int)crypto_sign_ed25519_pk_to_curve25519(arg1,(unsigned char const *)arg2); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1verify_1detached(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jlong jarg3, jbyteArray jarg4) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + unsigned long long arg3 ; + unsigned char *arg4 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + arg3 = (unsigned long long)jarg3; + { + arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); + } + result = (int)crypto_sign_verify_detached((unsigned char const *)arg1,(unsigned char const *)arg2,arg3,(unsigned char const *)arg4); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); + } + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1ed25519_1sk_1to_1seed(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + result = (int)crypto_sign_ed25519_sk_to_seed(arg1,(unsigned char const *)arg2); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1ed25519_1sk_1to_1pk(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + result = (int)crypto_sign_ed25519_sk_to_pk(arg1,(unsigned char const *)arg2); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1box_1seed_1keypair(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jbyteArray jarg3) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + unsigned char *arg3 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + { + arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); + } + result = (int)crypto_box_seed_keypair(arg1,arg2,(unsigned char const *)arg3); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); + } + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1box_1keypair(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + result = (int)crypto_box_keypair(arg1,arg2); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1box_1easy(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jlong jarg3, jbyteArray jarg4, jbyteArray jarg5, jbyteArray jarg6) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + unsigned long long arg3 ; + unsigned char *arg4 = (unsigned char *) 0 ; + unsigned char *arg5 = (unsigned char *) 0 ; + unsigned char *arg6 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + arg3 = (unsigned long long)jarg3; + { + arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); + } + { + arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); + } + { + arg6 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg6, 0); + } + result = (int)crypto_box_easy(arg1,(unsigned char const *)arg2,arg3,(unsigned char const *)arg4,(unsigned char const *)arg5,(unsigned char const *)arg6); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg6, (jbyte *) arg6, 0); + } + + + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1box_1open_1easy(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jlong jarg3, jbyteArray jarg4, jbyteArray jarg5, jbyteArray jarg6) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + unsigned long long arg3 ; + unsigned char *arg4 = (unsigned char *) 0 ; + unsigned char *arg5 = (unsigned char *) 0 ; + unsigned char *arg6 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + arg3 = (unsigned long long)jarg3; + { + arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); + } + { + arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); + } + { + arg6 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg6, 0); + } + result = (int)crypto_box_open_easy(arg1,(unsigned char const *)arg2,arg3,(unsigned char const *)arg4,(unsigned char const *)arg5,(unsigned char const *)arg6); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg6, (jbyte *) arg6, 0); + } + + + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1box_1detached(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5, jbyteArray jarg6, jbyteArray jarg7) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + unsigned char *arg3 = (unsigned char *) 0 ; + unsigned long long arg4 ; + unsigned char *arg5 = (unsigned char *) 0 ; + unsigned char *arg6 = (unsigned char *) 0 ; + unsigned char *arg7 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + { + arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); + } + arg4 = (unsigned long long)jarg4; + { + arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); + } + { + arg6 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg6, 0); + } + { + arg7 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg7, 0); + } + result = (int)crypto_box_detached(arg1,arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5,(unsigned char const *)arg6,(unsigned char const *)arg7); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg6, (jbyte *) arg6, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg7, (jbyte *) arg7, 0); + } + + + + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1box_1open_1detached(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5, jbyteArray jarg6, jbyteArray jarg7) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + unsigned char *arg3 = (unsigned char *) 0 ; + unsigned long long arg4 ; + unsigned char *arg5 = (unsigned char *) 0 ; + unsigned char *arg6 = (unsigned char *) 0 ; + unsigned char *arg7 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + { + arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); + } + arg4 = (unsigned long long)jarg4; + { + arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); + } + { + arg6 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg6, 0); + } + { + arg7 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg7, 0); + } + result = (int)crypto_box_open_detached(arg1,(unsigned char const *)arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5,(unsigned char const *)arg6,(unsigned char const *)arg7); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg6, (jbyte *) arg6, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg7, (jbyte *) arg7, 0); + } + + + + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1secretbox_1easy(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jlong jarg3, jbyteArray jarg4, jbyteArray jarg5) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + unsigned long long arg3 ; + unsigned char *arg4 = (unsigned char *) 0 ; + unsigned char *arg5 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + arg3 = (unsigned long long)jarg3; + { + arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); + } + { + arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); + } + result = (int)crypto_secretbox_easy(arg1,(unsigned char const *)arg2,arg3,(unsigned char const *)arg4,(unsigned char const *)arg5); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); + } + + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1secretbox_1open_1easy(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jlong jarg3, jbyteArray jarg4, jbyteArray jarg5) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + unsigned long long arg3 ; + unsigned char *arg4 = (unsigned char *) 0 ; + unsigned char *arg5 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + arg3 = (unsigned long long)jarg3; + { + arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); + } + { + arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); + } + result = (int)crypto_secretbox_open_easy(arg1,(unsigned char const *)arg2,arg3,(unsigned char const *)arg4,(unsigned char const *)arg5); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); + } + + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1secretbox_1detached(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5, jbyteArray jarg6) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + unsigned char *arg3 = (unsigned char *) 0 ; + unsigned long long arg4 ; + unsigned char *arg5 = (unsigned char *) 0 ; + unsigned char *arg6 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + { + arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); + } + arg4 = (unsigned long long)jarg4; + { + arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); + } + { + arg6 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg6, 0); + } + result = (int)crypto_secretbox_detached(arg1,arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5,(unsigned char const *)arg6); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg6, (jbyte *) arg6, 0); + } + + + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1secretbox_1open_1detached(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5, jbyteArray jarg6) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + unsigned char *arg3 = (unsigned char *) 0 ; + unsigned long long arg4 ; + unsigned char *arg5 = (unsigned char *) 0 ; + unsigned char *arg6 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + { + arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); + } + arg4 = (unsigned long long)jarg4; + { + arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); + } + { + arg6 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg6, 0); + } + result = (int)crypto_secretbox_open_detached(arg1,(unsigned char const *)arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5,(unsigned char const *)arg6); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg6, (jbyte *) arg6, 0); + } + + + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1auth(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jlong jarg3, jbyteArray jarg4) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + unsigned long long arg3 ; + unsigned char *arg4 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + arg3 = (unsigned long long)jarg3; + { + arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); + } + result = (int)crypto_auth(arg1,(unsigned char const *)arg2,arg3,(unsigned char const *)arg4); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); + } + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1auth_1verify(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jlong jarg3, jbyteArray jarg4) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + unsigned long long arg3 ; + unsigned char *arg4 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + arg3 = (unsigned long long)jarg3; + { + arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); + } + result = (int)crypto_auth_verify((unsigned char const *)arg1,(unsigned char const *)arg2,arg3,(unsigned char const *)arg4); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); + } + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1aead_1chacha20poly1305_1encrypt(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jlong jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5, jlong jarg6, jbyteArray jarg7, jbyteArray jarg8, jbyteArray jarg9) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned long long *arg2 = (unsigned long long *) 0 ; + unsigned char *arg3 = (unsigned char *) 0 ; + unsigned long long arg4 ; + unsigned char *arg5 = (unsigned char *) 0 ; + unsigned long long arg6 ; + unsigned char *arg7 = (unsigned char *) 0 ; + unsigned char *arg8 = (unsigned char *) 0 ; + unsigned char *arg9 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + arg2 = *(unsigned long long **)&jarg2; + { + arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); + } + arg4 = (unsigned long long)jarg4; + { + arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); + } + arg6 = (unsigned long long)jarg6; + { + arg7 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg7, 0); + } + { + arg8 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg8, 0); + } + { + arg9 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg9, 0); + } + result = (int)crypto_aead_chacha20poly1305_encrypt(arg1,arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5,arg6,(unsigned char const *)arg7,(unsigned char const *)arg8,(unsigned char const *)arg9); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg7, (jbyte *) arg7, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg8, (jbyte *) arg8, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg9, (jbyte *) arg9, 0); + } + + + + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1aead_1chacha20poly1305_1decrypt(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jlong jarg2, jbyteArray jarg3, jbyteArray jarg4, jlong jarg5, jbyteArray jarg6, jlong jarg7, jbyteArray jarg8, jbyteArray jarg9) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned long long *arg2 = (unsigned long long *) 0 ; + unsigned char *arg3 = (unsigned char *) 0 ; + unsigned char *arg4 = (unsigned char *) 0 ; + unsigned long long arg5 ; + unsigned char *arg6 = (unsigned char *) 0 ; + unsigned long long arg7 ; + unsigned char *arg8 = (unsigned char *) 0 ; + unsigned char *arg9 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + arg2 = *(unsigned long long **)&jarg2; + { + arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); + } + { + arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); + } + arg5 = (unsigned long long)jarg5; + { + arg6 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg6, 0); + } + arg7 = (unsigned long long)jarg7; + { + arg8 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg8, 0); + } + { + arg9 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg9, 0); + } + result = (int)crypto_aead_chacha20poly1305_decrypt(arg1,arg2,arg3,(unsigned char const *)arg4,arg5,(unsigned char const *)arg6,arg7,(unsigned char const *)arg8,(unsigned char const *)arg9); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg6, (jbyte *) arg6, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg8, (jbyte *) arg8, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg9, (jbyte *) arg9, 0); + } + + + + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1pwhash_1scryptsalsa208sha256(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jlong jarg2, jstring jarg3, jlong jarg4, jbyteArray jarg5, jlong jarg6, jint jarg7) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) (unsigned char *)0 ; + unsigned long long arg2 ; + char *arg3 = (char *) (char *)0 ; + unsigned long long arg4 ; + unsigned char *arg5 = (unsigned char *) (unsigned char *)0 ; + unsigned long long arg6 ; + size_t arg7 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + arg2 = (unsigned long long)jarg2; + arg3 = 0; + if (jarg3) { + arg3 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg3, 0); + if (!arg3) return 0; + } + arg4 = (unsigned long long)jarg4; + { + arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); + } + arg6 = (unsigned long long)jarg6; + arg7 = (size_t)jarg7; + result = (int)crypto_pwhash_scryptsalsa208sha256(arg1,arg2,(char const *)arg3,arg4,(unsigned char const *)arg5,arg6,arg7); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); + } + + if (arg3) (*jenv)->ReleaseStringUTFChars(jenv, jarg3, (const char *)arg3); + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1pwhash_1scryptsalsa208sha256_1str(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jstring jarg2, jlong jarg3, jlong jarg4, jint jarg5) { + jint jresult = 0 ; + char *arg1 ; + char *arg2 = (char *) (char *)0 ; + unsigned long long arg3 ; + unsigned long long arg4 ; + size_t arg5 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + arg2 = 0; + if (jarg2) { + arg2 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg2, 0); + if (!arg2) return 0; + } + arg3 = (unsigned long long)jarg3; + arg4 = (unsigned long long)jarg4; + arg5 = (size_t)jarg5; + result = (int)crypto_pwhash_scryptsalsa208sha256_str(arg1,(char const *)arg2,arg3,arg4,arg5); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + + if (arg2) (*jenv)->ReleaseStringUTFChars(jenv, jarg2, (const char *)arg2); + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1pwhash_1scryptsalsa208sha256_1str_1verify(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jstring jarg2, jlong jarg3) { + jint jresult = 0 ; + char *arg1 ; + char *arg2 = (char *) (char *)0 ; + unsigned long long arg3 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + arg2 = 0; + if (jarg2) { + arg2 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg2, 0); + if (!arg2) return 0; + } + arg3 = (unsigned long long)jarg3; + result = (int)crypto_pwhash_scryptsalsa208sha256_str_verify((char const (*))arg1,(char const *)arg2,arg3); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + + if (arg2) (*jenv)->ReleaseStringUTFChars(jenv, jarg2, (const char *)arg2); + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1shorthash(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jlong jarg3, jbyteArray jarg4) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + unsigned char *arg2 = (unsigned char *) 0 ; + unsigned long long arg3 ; + unsigned char *arg4 = (unsigned char *) 0 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + { + arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); + } + arg3 = (unsigned long long)jarg3; + { + arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); + } + result = (int)crypto_shorthash(arg1,(unsigned char const *)arg2,arg3,(unsigned char const *)arg4); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); + } + + + + return jresult; +} + + +SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1generichash(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jint jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5, jint jarg6) { + jint jresult = 0 ; + unsigned char *arg1 = (unsigned char *) 0 ; + size_t arg2 ; + unsigned char *arg3 = (unsigned char *) 0 ; + unsigned long long arg4 ; + unsigned char *arg5 = (unsigned char *) 0 ; + size_t arg6 ; + int result; + + (void)jenv; + (void)jcls; + { + arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); + } + arg2 = (size_t)jarg2; + { + arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); + } + arg4 = (unsigned long long)jarg4; + { + arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); + } + arg6 = (size_t)jarg6; + result = (int)crypto_generichash(arg1,arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5,arg6); + jresult = (jint)result; + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); + } + { + (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); + } + + + + return jresult; +} + + +#ifdef __cplusplus +} +#endif + diff --git a/aesjni/src/main/jni/swig_gen.sh b/aesjni/src/main/jni/swig_gen.sh new file mode 100755 index 0000000..f158536 --- /dev/null +++ b/aesjni/src/main/jni/swig_gen.sh @@ -0,0 +1,59 @@ +#!/bin/sh +# @Author: bruce + +command_exists() { + # check if command exists and fail otherwise + command -v "$1" >/dev/null 2>&1 + if [[ $? -ne 0 ]]; then #not found. + return 0 + else + return 1 + fi +} + +install_app_on_debian() { + echo "Debian try to install $1." + sudo apt update + sudo apt install $1 +} +install_app_on_mac() { + echo "This computer try to install $1." + brew install $1 +} + + +install_app() { + echo "try to install $1." + if [ -f /etc/debian_version ]; then + install_app_on_debian $1 + elif [[ "$OSTYPE" == "linux-gnu"* ]]; then + install_app_on_debian $1 + elif [[ "$OSTYPE" == "darwin"* ]]; then + install_app_on_mac $1 + else + echo "can't run on this system." + exit 1 + fi +} + + + + +try_install_app() { + command_exists $1 + is_app_installed=$? + if [ "$is_app_installed" -eq "0" ]; then + install_app $1 + command_exists $1 + is_app_installed=$? + if [ "$is_app_installed" -eq "0" ]; then + printf 'Error: %s has not been installed.\n' $1 + exit 1 + fi + fi +} + +# install swig if it has not been intalled. +try_install_app swig +# start generating to get c file and java class. +swig -java -package com.androidyuan.aesjni -outdir ../java/com/androidyuan/aesjni/ sodium.i \ No newline at end of file diff --git a/build.gradle b/build.gradle index 564ee83..3f9dfab 100644 --- a/build.gradle +++ b/build.gradle @@ -5,9 +5,10 @@ buildscript { maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } maven { url "https://jitpack.io" } jcenter() + google() } dependencies { - classpath 'com.android.tools.build:gradle:2.3.1' + classpath 'com.android.tools.build:gradle:4.1.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/gradle.properties b/gradle.properties index aac7c9b..8f62aea 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,3 +15,4 @@ org.gradle.jvmargs=-Xmx1536m # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true +android.useDeprecatedNdk = true \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a4d5bea..fe89291 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Fri Aug 11 15:40:50 CST 2017 +#Wed Feb 03 22:34:26 CST 2021 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip From 5c2cc6af2ccfd94de61826719b841c5bc8e604da Mon Sep 17 00:00:00 2001 From: bruce Date: Sun, 7 Feb 2021 16:44:17 +0800 Subject: [PATCH 03/21] [shell script]:{detecting environmental variable ANDROID_NDK_HOME before build libsodium.} --- .gitignore | 3 ++ .../build_libsodium_for_all_android_abi.sh | 49 +++++++++++-------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index 7d6ac26..45eeb28 100644 --- a/.gitignore +++ b/.gitignore @@ -60,7 +60,10 @@ captures/ #remote build mainframer.sh .mainframer + +#some files that is generated from building libsodium need to be add gitignore. aesjni/src/main/libs/ aesjni/src/main/obj/ aesjni/src/main/jni/Android.mk aesjni/src/main/jni/sodium_include +aesjni/src/main/jni/temp diff --git a/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh b/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh index f713f1d..ef5c306 100755 --- a/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh +++ b/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh @@ -5,26 +5,33 @@ # 2.Ubuntu 20 with ndk-bundle. LIBSODIUM_GIT_TAG=1.0.18 # it is the last stable version. -ROOT_DIR=$(pwd) + +#ROOT_DIR=$(pwd) + +# building libsodium needs to execute ndk-build,so there is detect whether $ANDROID_NDK_HOME exist. +if [ ! "$ANDROID_NDK_HOME" ];then + printf "ANDROID_NDK_HOME does not exist in environment. Please set ANDROID_NDK_HOME into your environment.\n" + exit +fi SODIUM_INCLUDE_DIR=$(pwd)/sodium_include -if [ -d $SODIUM_INCLUDE_DIR ]; then # if it exists,delete it. - rm -rf $SODIUM_INCLUDE_DIR +if [ -d "$SODIUM_INCLUDE_DIR" ]; then # if it exists,delete it. + rm -rf "$SODIUM_INCLUDE_DIR"/libsodium-android* fi mkdir sodium_include TEMP_DIR=$(pwd)/temp -if [ -d $TEMP_DIR ]; then # if it exists,delete it. - rm -rf $TEMP_DIR +if [ -d "$TEMP_DIR" ]; then # if it exists,delete it. + rm -rf "$TEMP_DIR" fi -mkdir $TEMP_DIR +mkdir "$TEMP_DIR" SODIUM_CLONING_HOME=$TEMP_DIR/libsodium # clone git clone git@github.com:jedisct1/libsodium.git "$SODIUM_CLONING_HOME" -cd $SODIUM_CLONING_HOME +cd "$SODIUM_CLONING_HOME" || exit git fetch --tags git checkout $LIBSODIUM_GIT_TAG # check specific verion. @@ -33,7 +40,7 @@ git checkout $LIBSODIUM_GIT_TAG # check specific verion. # && git pull # This can be removed once we pull from a release -if [ ! -e $SODIUM_CLONING_HOME/configure ]; then +if [ ! -e "$SODIUM_CLONING_HOME"/configure ]; then ./autogen.sh fi @@ -44,36 +51,36 @@ LIBSODIUM_MIPS32=$SODIUM_CLONING_HOME/libsodium-android-mips32 LIBSODIUM_ARMV8A=$SODIUM_CLONING_HOME/libsodium-android-armv8-a # Run the android builds -if [ ! -d $LIBSODIUM_ARMV6 ]; then - $SODIUM_CLONING_HOME/dist-build/android-arm.sh +if [ ! -d "$LIBSODIUM_ARMV6" ]; then + "$SODIUM_CLONING_HOME"/dist-build/android-arm.sh echo "build-libsodium: built armv6!" else echo "build-libsodium: skipping armv6, already built!" fi -if [ ! -d $LIBSODIUM_ARMV7A ]; then - $SODIUM_CLONING_HOME/dist-build/android-armv7-a.sh +if [ ! -d "$LIBSODIUM_ARMV7A" ]; then + "$SODIUM_CLONING_HOME"/dist-build/android-armv7-a.sh echo "build-libsodium: built armv7-a!" else echo "build-libsodium: skipping armv7-a, already built!" fi -if [ ! -d $LIBSODIUM_MIPS32 ]; then - $SODIUM_CLONING_HOME/dist-build/android-mips32.sh +if [ ! -d "$LIBSODIUM_MIPS32" ]; then + "$SODIUM_CLONING_HOME"/dist-build/android-mips32.sh echo "build-libsodium: built mips32!" else echo "build-libsodium: skipping mips32, already built!" fi -if [ ! -d $LIBSODIUM_I686 ]; then - $SODIUM_CLONING_HOME/dist-build/android-x86.sh +if [ ! -d "$LIBSODIUM_I686" ]; then + "$SODIUM_CLONING_HOME"/dist-build/android-x86.sh echo "build-libsodium: built x86!" else echo "build-libsodium: skipping x86, already built!" fi -if [ ! -d $LIBSODIUM_ARMV8A ]; then - $SODIUM_CLONING_HOME/dist-build/android-armv8-a.sh +if [ ! -d "$LIBSODIUM_ARMV8A" ]; then + "$SODIUM_CLONING_HOME"/dist-build/android-armv8-a.sh echo "build-libsodium: built armv8-a!" else echo "build-libsodium: skipping armv8-a, already built!" @@ -81,8 +88,8 @@ fi if [ $? -eq 0 ]; then - mv -v $SODIUM_CLONING_HOME/libsodium-android-* $SODIUM_INCLUDE_DIR - printf 'all static libs move into %s.\n' $SODIUM_INCLUDE_DIR - rm -rf $TEMP_DIR + mv -v "$SODIUM_CLONING_HOME"/libsodium-android-* "$SODIUM_INCLUDE_DIR" + printf 'all static libs move into %s.\n' "$SODIUM_INCLUDE_DIR" + rm -rf "$TEMP_DIR" fi From 5913e9faf51b4a142c2818812df3d6cbb26459be Mon Sep 17 00:00:00 2001 From: bruce Date: Sun, 7 Feb 2021 16:57:44 +0800 Subject: [PATCH 04/21] [shell script]:{check libsodium has already been built in rebuild.sh.} --- aesjni/src/main/jni/OriginAndroid.mk | 2 +- aesjni/src/main/jni/rebuild.sh | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/aesjni/src/main/jni/OriginAndroid.mk b/aesjni/src/main/jni/OriginAndroid.mk index 899bfef..62c56ff 100755 --- a/aesjni/src/main/jni/OriginAndroid.mk +++ b/aesjni/src/main/jni/OriginAndroid.mk @@ -16,7 +16,7 @@ else ARCH_PREFIX := $(TARGET_ARCH) endif -SODIUM_LIB_DIR := $(LOCAL_PATH)/libsodium/libsodium-android-$(ARCH_PREFIX) +SODIUM_LIB_DIR := $(LOCAL_PATH)/sodium_include/libsodium-android-$(ARCH_PREFIX) SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a diff --git a/aesjni/src/main/jni/rebuild.sh b/aesjni/src/main/jni/rebuild.sh index 3dc3e6f..4e89fe0 100755 --- a/aesjni/src/main/jni/rebuild.sh +++ b/aesjni/src/main/jni/rebuild.sh @@ -11,11 +11,21 @@ # It has been built success with ndk-bundle. You could download sdk,then use sdkmanger command to downlaod ndk-bundle. # And NDK-r13b got failed due to google not supporting the old ndk to be with new system. # + +# detect whether build_libsodium_for_all_android_abi.sh has already been called. +if [ ! -d ./sodium_include/libsodium-android-armv8-a ]; then + printf "Please run build_libsodium_for_all_android_abi.sh before.\n" + exit +fi + +# clear cache. clear rm -r ../obj/ rm -r ../libs/ ndk-build clean + + ## run "./rebuild.sh test" if [[ $1 == "test" ]]; then printf "Start building......\n" @@ -24,8 +34,14 @@ if [[ $1 == "test" ]]; then if [ $? -ne 0 ]; then exit # failed at ndk-build fi - printf "adb push......\n" + printf "adb push......\n" # push executable file into device or emulator. adb push ../libs/arm64-v8a/test /data/local/tmp + + if [ $? -ne 0 ]; then # failed from pushing executable file. + printf "can't push executable file, does you connect more than one device or emulator? \n" + exit + fi + printf "This is executable file print:\n" printf "=========================\n" adb shell /data/local/tmp/test From 1f50d17fd6c9e0302fad0ae8f5a1b2c4126b3261 Mon Sep 17 00:00:00 2001 From: bruce Date: Sun, 14 Feb 2021 20:46:27 +0800 Subject: [PATCH 05/21] [test]:{testing aes and chacha20.} --- .gitignore | 8 + aesjni/src/main/jni/Android.mk | 33 +++- aesjni/src/main/jni/Application.mk | 8 +- aesjni/src/main/jni/TestAndroid.mk | 33 +++- aesjni/src/main/jni/aead_aes256gcm.c | 2 +- .../build_libsodium_for_all_android_abi.sh | 11 +- aesjni/src/main/jni/main_unit_test.c | 156 +++++++++++++++++- aesjni/src/main/jni/rebuild.sh | 9 +- 8 files changed, 238 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 45eeb28..7120c0e 100644 --- a/.gitignore +++ b/.gitignore @@ -67,3 +67,11 @@ aesjni/src/main/obj/ aesjni/src/main/jni/Android.mk aesjni/src/main/jni/sodium_include aesjni/src/main/jni/temp + + +# VSCODE +.vscode + + +# VSCODE +.vscode diff --git a/aesjni/src/main/jni/Android.mk b/aesjni/src/main/jni/Android.mk index 5298e20..7fbfaa3 100755 --- a/aesjni/src/main/jni/Android.mk +++ b/aesjni/src/main/jni/Android.mk @@ -1,11 +1,40 @@ LOCAL_PATH := $(call my-dir) -#... +include $(CLEAR_VARS) + +ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) + ARCH_PREFIX := armv7-a +else ifeq ($(TARGET_ARCH_ABI),armeabi) + ARCH_PREFIX := armv6 +else ifeq ($(TARGET_ARCH_ABI),x86) + ARCH_PREFIX := i686 +else ifeq ($(TARGET_ARCH_ABI),mips) + ARCH_PREFIX := mips32 +else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) + ARCH_PREFIX := armv8-a +else + ARCH_PREFIX := $(TARGET_ARCH) +endif + +# ========== sodium ================== +#include $(CLEAR_VARS) +SODIUM_LIB_DIR := $(LOCAL_PATH)/sodium_include/libsodium-android-$(ARCH_PREFIX) +SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include +SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a +LOCAL_MODULE:= sodium +LOCAL_SRC_FILES:= $(SODIUM_LIB) +LOCAL_EXPORT_C_INCLUDES := $(SODIUM_INC_DIR) $(SODIUM_INC_DIR)/sodium +include $(PREBUILT_STATIC_LIBRARY) + + + include $(CLEAR_VARS) LOCAL_MODULE := test # executable file name. -LOCAL_SRC_FILES := main_unit_test.c +LOCAL_SRC_FILES := main_unit_test.c \ + base64.c +LOCAL_STATIC_LIBRARIES := sodium # tell ndk-build about the dependency LOCAL_LDLIBS := -llog # no need to specify path for liblog.so include $(BUILD_EXECUTABLE) # <-- Use this to build an executable. \ No newline at end of file diff --git a/aesjni/src/main/jni/Application.mk b/aesjni/src/main/jni/Application.mk index 4253008..5de6f32 100755 --- a/aesjni/src/main/jni/Application.mk +++ b/aesjni/src/main/jni/Application.mk @@ -1,3 +1,5 @@ -# there is only armv8 is beacause I only push armv8 abi of libsodium static library.You can rebuild libsodium to generate static library of other ABIs. -APP_ABI := arm64-v8a -APP_PLATFORM := android-19 \ No newline at end of file +# there is only armv8. You can rebuild libsodium to generate static library of other ABIs. +APP_ABI := x86 +APP_PLATFORM := android-19 + +APP_STL := c++_static \ No newline at end of file diff --git a/aesjni/src/main/jni/TestAndroid.mk b/aesjni/src/main/jni/TestAndroid.mk index 5298e20..7fbfaa3 100755 --- a/aesjni/src/main/jni/TestAndroid.mk +++ b/aesjni/src/main/jni/TestAndroid.mk @@ -1,11 +1,40 @@ LOCAL_PATH := $(call my-dir) -#... +include $(CLEAR_VARS) + +ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) + ARCH_PREFIX := armv7-a +else ifeq ($(TARGET_ARCH_ABI),armeabi) + ARCH_PREFIX := armv6 +else ifeq ($(TARGET_ARCH_ABI),x86) + ARCH_PREFIX := i686 +else ifeq ($(TARGET_ARCH_ABI),mips) + ARCH_PREFIX := mips32 +else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) + ARCH_PREFIX := armv8-a +else + ARCH_PREFIX := $(TARGET_ARCH) +endif + +# ========== sodium ================== +#include $(CLEAR_VARS) +SODIUM_LIB_DIR := $(LOCAL_PATH)/sodium_include/libsodium-android-$(ARCH_PREFIX) +SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include +SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a +LOCAL_MODULE:= sodium +LOCAL_SRC_FILES:= $(SODIUM_LIB) +LOCAL_EXPORT_C_INCLUDES := $(SODIUM_INC_DIR) $(SODIUM_INC_DIR)/sodium +include $(PREBUILT_STATIC_LIBRARY) + + + include $(CLEAR_VARS) LOCAL_MODULE := test # executable file name. -LOCAL_SRC_FILES := main_unit_test.c +LOCAL_SRC_FILES := main_unit_test.c \ + base64.c +LOCAL_STATIC_LIBRARIES := sodium # tell ndk-build about the dependency LOCAL_LDLIBS := -llog # no need to specify path for liblog.so include $(BUILD_EXECUTABLE) # <-- Use this to build an executable. \ No newline at end of file diff --git a/aesjni/src/main/jni/aead_aes256gcm.c b/aesjni/src/main/jni/aead_aes256gcm.c index 89c5f43..dda73b4 100755 --- a/aesjni/src/main/jni/aead_aes256gcm.c +++ b/aesjni/src/main/jni/aead_aes256gcm.c @@ -1,6 +1,6 @@ #define TEST_NAME "aead_aes256gcm" -#include "cmptest.h" +#include static struct { const char *key_hex; diff --git a/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh b/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh index ef5c306..13b428d 100755 --- a/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh +++ b/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh @@ -4,6 +4,11 @@ # 1.MacOS with ndk r13. # 2.Ubuntu 20 with ndk-bundle. +# ===========================Remind========================= +# If you are running this shell for the first time, Pls look at terminal. +# Some error might be thrown. You may need to install certain applications +# (such as python, libtool-bin) to resolve those errors. + LIBSODIUM_GIT_TAG=1.0.18 # it is the last stable version. #ROOT_DIR=$(pwd) @@ -17,8 +22,9 @@ fi SODIUM_INCLUDE_DIR=$(pwd)/sodium_include if [ -d "$SODIUM_INCLUDE_DIR" ]; then # if it exists,delete it. rm -rf "$SODIUM_INCLUDE_DIR"/libsodium-android* +else + mkdir sodium_include fi -mkdir sodium_include TEMP_DIR=$(pwd)/temp if [ -d "$TEMP_DIR" ]; then # if it exists,delete it. @@ -91,5 +97,4 @@ if [ $? -eq 0 ]; then mv -v "$SODIUM_CLONING_HOME"/libsodium-android-* "$SODIUM_INCLUDE_DIR" printf 'all static libs move into %s.\n' "$SODIUM_INCLUDE_DIR" rm -rf "$TEMP_DIR" -fi - +fi \ No newline at end of file diff --git a/aesjni/src/main/jni/main_unit_test.c b/aesjni/src/main/jni/main_unit_test.c index 865bded..308cb99 100755 --- a/aesjni/src/main/jni/main_unit_test.c +++ b/aesjni/src/main/jni/main_unit_test.c @@ -6,17 +6,159 @@ you can run a command in this directory: ./rebuild.sh test #define TEST_NAME "aead_aes256gcm" #include #include +#include +#include +#include +// #include + -//run adb logcat | grep NATIVE_UT #define TAG "NATIVE_UT" -void logd(char * str){ - __android_log_print(ANDROID_LOG_DEBUG , TAG, "%s", str); // the 3rd arg is a printf-style format string +void logd(char *str) +{ + //this string will be got from you command. + printf("%s \n", str); + //run "adb logcat | grep NATIVE_UT" in order to seek logs. + __android_log_print(ANDROID_LOG_DEBUG, TAG, "%s", str); } -int main() { - printf("main() running.\n");//this string will be got from you command, - logd("test");//run adb logcat | grep NATIVE_UT - return 0; +/* +There is a testing for AEAD AES256_GCM. +*/ +void test_aead_aes256gcm() +{ + unsigned char *ad; + unsigned char *ciphertext; + unsigned char *decrypted; + unsigned char *detached_ciphertext; + unsigned char *expected_ciphertext; + unsigned char *key; + unsigned char *mac; + unsigned char *nonce; + char *hex; + unsigned long long found_ciphertext_len; + unsigned long long found_mac_len; + unsigned long long found_message_len; + size_t ad_len; + size_t ciphertext_len; + size_t detached_ciphertext_len; + size_t i = 0U; + + /* + AES256-GCM is not high performance on ARM. + */ + + // to detect CPU has hardware-accelerated for AES256GCM. + if (crypto_aead_aes256gcm_is_available() == 0) + { + logd("The device doesn't support AES algorithm."); /* Not available on this CPU */ + } + else + { + logd("The device support AES algorithm."); + + const char *key_hex = "b52c505a37d78eda5dd34f20c22540ea1b58963cf8e5bf8ffa85f9f2492505b4"; + const char *nonce_hex = "516c33929df5a3284ff463d7"; + const char *mac_hex = "bdc1ac884d332457a1d2664f168c76f0"; + + const size_t message_len = 4; + const unsigned char *message = "test"; + + key = (unsigned char *)sodium_malloc(crypto_aead_aes256gcm_KEYBYTES); + nonce = (unsigned char *)sodium_malloc(crypto_aead_aes256gcm_NPUBBYTES); + mac = (unsigned char *)sodium_malloc(crypto_aead_aes256gcm_ABYTES); + + sodium_hex2bin(key, crypto_aead_aes256gcm_KEYBYTES, + key_hex, strlen(key_hex), + NULL, NULL, NULL); + sodium_hex2bin(nonce, crypto_aead_aes256gcm_NPUBBYTES, + nonce_hex, strlen(nonce_hex), + NULL, NULL, NULL); + + crypto_aead_aes256gcm_encrypt(ciphertext, &found_ciphertext_len, + message, message_len, + NULL, 0, NULL, nonce, key); + + crypto_aead_aes256gcm_decrypt(NULL, NULL, NULL, ciphertext, strlen(ciphertext), + NULL, 0, nonce, key); + } } +/* + ChaCha20-Poly1305 is performance on ARM. + */ +void test_chacha20() +{ + const unsigned char *PLAIN_TEXT = "test1234test1234test1234test1234END"; + const unsigned int PLAIN_LEN = 35; + const unsigned char *ADDITIONAL_DATA = NULL; + const unsigned int ADDITIONAL_DATA_LEN = 0; + + //static key and nonce with base64encode. + + // unsigned char nonce[crypto_aead_chacha20poly1305_NPUBBYTES]; + // unsigned char key[crypto_aead_chacha20poly1305_KEYBYTES]; + // crypto_aead_chacha20poly1305_keygen(key); + // randombytes_buf(nonce, sizeof nonce); + + //key & nonce were generated with random. + const unsigned char *BASE64_NONCE = "wzAeemDo5oY="; + const unsigned char *BASE64_KEY = "cU3wmpJ0cmfMHSWVKbmH4GkU4CBnfARePp1GdrSwQObDMB56YOjmhg=="; + char *nonce = b64_decode(BASE64_NONCE, strlen(BASE64_NONCE)); + char *key = b64_decode(BASE64_KEY, strlen(BASE64_KEY)); + + logd("key:"); logd(b64_encode(key, strlen(key))); + + char a =key[strlen(key)-1]; + int ia = (int)a; + printf("last of key: %d\n",ia); + + logd("nonce:"); logd(b64_encode(nonce, strlen(nonce))); + + unsigned char ciphertext[PLAIN_LEN + crypto_aead_chacha20poly1305_ABYTES]; + unsigned long long ciphertext_len; + + crypto_aead_chacha20poly1305_encrypt(ciphertext, &ciphertext_len, + PLAIN_TEXT, PLAIN_LEN, + ADDITIONAL_DATA, ADDITIONAL_DATA_LEN, //additional data is NULL. + NULL, nonce, key); + + logd("encrypted text: "); logd(b64_encode(ciphertext, ciphertext_len)); + + char decrypted[PLAIN_LEN]; + unsigned long long decrypted_len; + crypto_aead_chacha20poly1305_decrypt(decrypted, &decrypted_len, + NULL, + ciphertext, ciphertext_len, + ADDITIONAL_DATA,ADDITIONAL_DATA_LEN, + nonce, key); + logd("decrypted text:"); + logd(decrypted); + + if(strcmp(decrypted,PLAIN_TEXT)!=0){ + abort(); //you won't see "main() end" in terminal. + } + free(ciphertext); + free(decrypted); + free(PLAIN_TEXT); + + +} + +int main() +{ + + logd("main()...begin"); + + sodium_init(); + + logd("-----------------------aead_aes256gcm----------------------"); + test_aead_aes256gcm(); + + logd("-----------------------chacha20----------------------"); + test_chacha20(); + + logd("main()...end"); + + return 0; +} diff --git a/aesjni/src/main/jni/rebuild.sh b/aesjni/src/main/jni/rebuild.sh index 4e89fe0..a0244c5 100755 --- a/aesjni/src/main/jni/rebuild.sh +++ b/aesjni/src/main/jni/rebuild.sh @@ -2,7 +2,7 @@ # @author bruce: # ========================Remind:======================== # run this shell script must before, you computer must be set ANROID_NDK_HOME, -# And your computer must connect with a armv8 phone,or armv8 emulator beacause only build armv8 share library. +# And your computer must connect TODO . # ------------------------------------------------------- # Build success environmental system records: # Mac OS: @@ -12,6 +12,8 @@ # And NDK-r13b got failed due to google not supporting the old ndk to be with new system. # +BUILT_ABI=x86 +#BUILT_ABI=arm64-v8a # detect whether build_libsodium_for_all_android_abi.sh has already been called. if [ ! -d ./sodium_include/libsodium-android-armv8-a ]; then printf "Please run build_libsodium_for_all_android_abi.sh before.\n" @@ -25,7 +27,6 @@ rm -r ../libs/ ndk-build clean - ## run "./rebuild.sh test" if [[ $1 == "test" ]]; then printf "Start building......\n" @@ -35,10 +36,10 @@ if [[ $1 == "test" ]]; then exit # failed at ndk-build fi printf "adb push......\n" # push executable file into device or emulator. - adb push ../libs/arm64-v8a/test /data/local/tmp + adb push ../libs/$BUILT_ABI/test /data/local/tmp if [ $? -ne 0 ]; then # failed from pushing executable file. - printf "can't push executable file, does you connect more than one device or emulator? \n" + printf "can't push executable file. Do you connect device or emulator? \n" exit fi From 6a21fb53a7e7c6e6a24d10ae1976116e30fcdbfb Mon Sep 17 00:00:00 2001 From: bruce Date: Fri, 19 Feb 2021 17:47:52 +0800 Subject: [PATCH 06/21] [READNE]:{add a README file for locating crash tutorial.} --- FigureOutJNICrash.md | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 FigureOutJNICrash.md diff --git a/FigureOutJNICrash.md b/FigureOutJNICrash.md new file mode 100644 index 0000000..15b1844 --- /dev/null +++ b/FigureOutJNICrash.md @@ -0,0 +1,66 @@ +How to locate where native crash at? +------------ +1.ensure you ndk is set into environmental. + +2.run **ndk-stack** +``` +adb logcat | ndk-stack -sym ./obj/local/armeabi-v7a/ +``` + +### Example: + +I got crash logs: + +```logs +$ ./rebuild.sh test +2021-02-19 17:05:04.569 29543-29543/? A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x1 in tid 29543 (test), pid 29543 (test) +2021-02-19 17:05:04.621 29547-29547/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** +2021-02-19 17:05:04.621 29547-29547/? A/DEBUG: Build fingerprint: 'samsung/d1qzc/d1q:11/RP1A.200720.012/N9700ZCU3EUA6:user/release-keys' +2021-02-19 17:05:04.622 29547-29547/? A/DEBUG: Revision: '9' +2021-02-19 17:05:04.622 29547-29547/? A/DEBUG: ABI: 'arm' +2021-02-19 17:05:04.623 29547-29547/? A/DEBUG: Timestamp: 2021-02-19 17:05:04+0800 +2021-02-19 17:05:04.623 29547-29547/? A/DEBUG: pid: 29543, tid: 29543, name: test >>> /data/local/tmp/test <<< +2021-02-19 17:05:04.623 29547-29547/? A/DEBUG: uid: 2000 +2021-02-19 17:05:04.623 29547-29547/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x1 +2021-02-19 17:05:04.625 29547-29547/? A/DEBUG: Cause: null pointer dereference +2021-02-19 17:05:04.626 29547-29547/? A/DEBUG: r0 00000001 r1 ffdb37f3 r2 00000001 r3 0024c80e +2021-02-19 17:05:04.626 29547-29547/? A/DEBUG: r4 7fffffff r5 00000001 r6 00000001 r7 ffdb3800 +2021-02-19 17:05:04.626 29547-29547/? A/DEBUG: r8 ffdb3698 r9 ffdb3cac r10 ffdb37f3 r11 00000000 +2021-02-19 17:05:04.626 29547-29547/? A/DEBUG: ip 00000030 sp ffdb35e0 lr f65f212d pc f65b7ef0 +2021-02-19 17:05:04.646 29547-29547/? A/DEBUG: backtrace: +2021-02-19 17:05:04.647 29547-29547/? A/DEBUG: NOTE: Function names and BuildId information is missing for some frames due +2021-02-19 17:05:04.647 29547-29547/? A/DEBUG: NOTE: to unreadable libraries. For unwinds of apps, only shared libraries +2021-02-19 17:05:04.647 29547-29547/? A/DEBUG: NOTE: found under the lib/ directory are readable. +2021-02-19 17:05:04.647 29547-29547/? A/DEBUG: #00 pc 00036ef0 /apex/com.android.runtime/lib/bionic/libc.so (__memcpy_base_a55+488) (BuildId: 940ec10aefab667cc40b263303abf96e) +2021-02-19 17:05:04.647 29547-29547/? A/DEBUG: #01 pc 00071129 /apex/com.android.runtime/lib/bionic/libc.so (__sfvwrite+166) (BuildId: 940ec10aefab667cc40b263303abf96e) +2021-02-19 17:05:04.647 29547-29547/? A/DEBUG: #02 pc 0006af97 /apex/com.android.runtime/lib/bionic/libc.so (__vfprintf+6306) (BuildId: 940ec10aefab667cc40b263303abf96e) +2021-02-19 17:05:04.647 29547-29547/? A/DEBUG: #03 pc 0007fae1 /apex/com.android.runtime/lib/bionic/libc.so (sprintf+128) (BuildId: 940ec10aefab667cc40b263303abf96e) +2021-02-19 17:05:04.647 29547-29547/? A/DEBUG: #04 pc 00001045 /data/local/tmp/test +2021-02-19 17:05:04.648 29547-29547/? A/DEBUG: #05 pc 0000113d /data/local/tmp/test +2021-02-19 17:05:04.648 29547-29547/? A/DEBUG: #06 pc 0003382d /apex/com.android.runtime/lib/bionic/libc.so (__libc_init+68) (BuildId: 940ec10aefab667cc40b263303abf96e) + +``` + + +```log +$ adb logcat | ndk-stack -sym ./obj/local/armeabi-v7a/ +Crash dump is completed + +********** Crash dump: ********** +Build fingerprint: 'samsung/d1qzc/d1q:11/RP1A.200720.012/N9700ZCU3EUA6:user/release-keys' +pid: 29543, tid: 29543, name: test >>> /data/local/tmp/test <<< +signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x1 +Stack frame #00 pc 00036ef0 /apex/com.android.runtime/lib/bionic/libc.so (__memcpy_base_a55+488) (BuildId: 940ec10aefab667cc40b263303abf96e) +Stack frame #01 pc 00071129 /apex/com.android.runtime/lib/bionic/libc.so (__sfvwrite+166) (BuildId: 940ec10aefab667cc40b263303abf96e) +Stack frame #02 pc 0006af97 /apex/com.android.runtime/lib/bionic/libc.so (__vfprintf+6306) (BuildId: 940ec10aefab667cc40b263303abf96e) +Stack frame #03 pc 0007fae1 /apex/com.android.runtime/lib/bionic/libc.so (sprintf+128) (BuildId: 940ec10aefab667cc40b263303abf96e) +Stack frame #04 pc 00001045 /data/local/tmp/test: Routine test_chacha20 at /Users/bruce/Documents/git/AESJniEncrypt/aesjni/src/main/jni/main_unit_test.c:146 +Stack frame #05 pc 0000113d /data/local/tmp/test: Routine main at /Users/bruce/Documents/git/AESJniEncrypt/aesjni/src/main/jni/main_unit_test.c:170 + +``` + +My conclusion: `/Users/bruce/Documents/git/AESJniEncrypt/aesjni/src/main/jni/main_unit_test.c:146` is where the executable file crash at. + +## Remind +ndk-stack of NDK-R13 **only** supports armeabi-v7a. + From db5172546e7d2fe71304df3cbf52c9709f5550c6 Mon Sep 17 00:00:00 2001 From: bruce Date: Sat, 20 Feb 2021 18:10:00 +0800 Subject: [PATCH 07/21] [fix]:{1. fix that decrypt char* cant be print; 2.add key generation algorithm.} --- FigureOutJNICrash.md | 6 +- aesjni/src/main/jni/Android.mk | 40 ------ aesjni/src/main/jni/Application.mk | 2 +- aesjni/src/main/jni/TestAndroid.mk | 8 +- .../build_libsodium_for_all_android_abi.sh | 2 +- aesjni/src/main/jni/keys_generator.c | 47 +++++++ aesjni/src/main/jni/keys_generator.h | 16 +++ aesjni/src/main/jni/main_unit_test.c | 116 ++++++++---------- aesjni/src/main/jni/rebuild.sh | 8 +- aesjni/src/main/jni/str_utils.cpp | 43 +++++++ aesjni/src/main/jni/str_utils.h | 22 ++++ 11 files changed, 196 insertions(+), 114 deletions(-) delete mode 100755 aesjni/src/main/jni/Android.mk create mode 100644 aesjni/src/main/jni/keys_generator.c create mode 100644 aesjni/src/main/jni/keys_generator.h create mode 100644 aesjni/src/main/jni/str_utils.cpp create mode 100644 aesjni/src/main/jni/str_utils.h diff --git a/FigureOutJNICrash.md b/FigureOutJNICrash.md index 15b1844..79b1551 100644 --- a/FigureOutJNICrash.md +++ b/FigureOutJNICrash.md @@ -1,10 +1,10 @@ How to locate where native crash at? ------------ -1.ensure you ndk is set into environmental. +1.ensure you ndk is set into environment. 2.run **ndk-stack** ``` -adb logcat | ndk-stack -sym ./obj/local/armeabi-v7a/ +adb logcat | ndk-stack -sym ../obj/local/armeabi-v7a/ ``` ### Example: @@ -62,5 +62,5 @@ Stack frame #05 pc 0000113d /data/local/tmp/test: Routine main at /Users/bruce/ My conclusion: `/Users/bruce/Documents/git/AESJniEncrypt/aesjni/src/main/jni/main_unit_test.c:146` is where the executable file crash at. ## Remind -ndk-stack of NDK-R13 **only** supports armeabi-v7a. +*ndk-stack* of NDK-R13 **only supports** symbol file of armeabi-v7a. diff --git a/aesjni/src/main/jni/Android.mk b/aesjni/src/main/jni/Android.mk deleted file mode 100755 index 7fbfaa3..0000000 --- a/aesjni/src/main/jni/Android.mk +++ /dev/null @@ -1,40 +0,0 @@ -LOCAL_PATH := $(call my-dir) - -include $(CLEAR_VARS) - -ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) - ARCH_PREFIX := armv7-a -else ifeq ($(TARGET_ARCH_ABI),armeabi) - ARCH_PREFIX := armv6 -else ifeq ($(TARGET_ARCH_ABI),x86) - ARCH_PREFIX := i686 -else ifeq ($(TARGET_ARCH_ABI),mips) - ARCH_PREFIX := mips32 -else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) - ARCH_PREFIX := armv8-a -else - ARCH_PREFIX := $(TARGET_ARCH) -endif - -# ========== sodium ================== -#include $(CLEAR_VARS) -SODIUM_LIB_DIR := $(LOCAL_PATH)/sodium_include/libsodium-android-$(ARCH_PREFIX) -SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include -SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a -LOCAL_MODULE:= sodium -LOCAL_SRC_FILES:= $(SODIUM_LIB) -LOCAL_EXPORT_C_INCLUDES := $(SODIUM_INC_DIR) $(SODIUM_INC_DIR)/sodium -include $(PREBUILT_STATIC_LIBRARY) - - - - -include $(CLEAR_VARS) - -LOCAL_MODULE := test # executable file name. -LOCAL_SRC_FILES := main_unit_test.c \ - base64.c -LOCAL_STATIC_LIBRARIES := sodium # tell ndk-build about the dependency -LOCAL_LDLIBS := -llog # no need to specify path for liblog.so - -include $(BUILD_EXECUTABLE) # <-- Use this to build an executable. \ No newline at end of file diff --git a/aesjni/src/main/jni/Application.mk b/aesjni/src/main/jni/Application.mk index 5de6f32..25ef8c4 100755 --- a/aesjni/src/main/jni/Application.mk +++ b/aesjni/src/main/jni/Application.mk @@ -1,5 +1,5 @@ # there is only armv8. You can rebuild libsodium to generate static library of other ABIs. -APP_ABI := x86 +APP_ABI := armeabi-v7a,x86 APP_PLATFORM := android-19 APP_STL := c++_static \ No newline at end of file diff --git a/aesjni/src/main/jni/TestAndroid.mk b/aesjni/src/main/jni/TestAndroid.mk index 7fbfaa3..7d6be05 100755 --- a/aesjni/src/main/jni/TestAndroid.mk +++ b/aesjni/src/main/jni/TestAndroid.mk @@ -33,8 +33,12 @@ include $(CLEAR_VARS) LOCAL_MODULE := test # executable file name. LOCAL_SRC_FILES := main_unit_test.c \ - base64.c + logger.h \ + keys_generator.c \ + base64.c \ + str_utils.cpp LOCAL_STATIC_LIBRARIES := sodium # tell ndk-build about the dependency -LOCAL_LDLIBS := -llog # no need to specify path for liblog.so +LOCAL_LDLIBS := -llog #there is no need to specify path for liblog.so +LOCAL_CPPFLAGS := -fexceptions include $(BUILD_EXECUTABLE) # <-- Use this to build an executable. \ No newline at end of file diff --git a/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh b/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh index 13b428d..48f33d2 100755 --- a/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh +++ b/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh @@ -95,6 +95,6 @@ fi if [ $? -eq 0 ]; then mv -v "$SODIUM_CLONING_HOME"/libsodium-android-* "$SODIUM_INCLUDE_DIR" - printf 'all static libs move into %s.\n' "$SODIUM_INCLUDE_DIR" + printf 'all of static libs move into %s.\n' "$SODIUM_INCLUDE_DIR" rm -rf "$TEMP_DIR" fi \ No newline at end of file diff --git a/aesjni/src/main/jni/keys_generator.c b/aesjni/src/main/jni/keys_generator.c new file mode 100644 index 0000000..93be405 --- /dev/null +++ b/aesjni/src/main/jni/keys_generator.c @@ -0,0 +1,47 @@ +#include + +void generate_chacah20_and_print() +{ + unsigned char key[chacha20_key_len]; + unsigned char nonce[chacha20_nonce_len]; + randombytes_buf(nonce, sizeof nonce); + crypto_aead_chacha20poly1305_keygen(key); + + char *key_hex = (char *)sodium_malloc(chacha20_key_hex_len); + sodium_bin2hex(key_hex, (size_t)(chacha20_key_hex_len), key, chacha20_key_len); + printf("generate a key_hex \t:%s\n", key_hex); + //test key hex2bin + chacha20_hexnonce2bin(key_hex); + + char *nonce_hex = (char *)sodium_malloc(chacha20_nonce_hex_len); + sodium_bin2hex(nonce_hex, (size_t)(chacha20_nonce_hex_len), nonce, chacha20_nonce_len); + printf("generate a nonce_hex\t:%s\n", nonce_hex); + + //test nonce hex2bin + chacha20_hexnonce2bin(nonce_hex); +} + +//strlen(hex) must equal to chacha20_key_hex_len +unsigned char *chacha20_hexkey2bin(char *hex) +{ + unsigned char *out_key = (unsigned char *)sodium_malloc(chacha20_key_len); + sodium_hex2bin(out_key, chacha20_key_len, + hex, chacha20_key_hex_len, + NULL, NULL, NULL); + return out_key; +} + +//strlen(hex) must equal to chacha20_nonce_hex_len +unsigned char *chacha20_hexnonce2bin(char *hex) +{ + unsigned char *out_nonce = (unsigned char *)sodium_malloc(chacha20_nonce_len); + sodium_hex2bin(out_nonce, chacha20_nonce_len, + hex, chacha20_nonce_hex_len, + NULL, NULL, NULL); + return out_nonce; +} + +void generate_aes_and_print() +{ + //TODO +} \ No newline at end of file diff --git a/aesjni/src/main/jni/keys_generator.h b/aesjni/src/main/jni/keys_generator.h new file mode 100644 index 0000000..85373b5 --- /dev/null +++ b/aesjni/src/main/jni/keys_generator.h @@ -0,0 +1,16 @@ +#include +#include +// ----generate key and nonce---- + +static const unsigned int chacha20_key_len = crypto_aead_chacha20poly1305_KEYBYTES; +static const unsigned int chacha20_nonce_len = crypto_aead_chacha20poly1305_NPUBBYTES; +static const unsigned int chacha20_key_hex_len = chacha20_key_len * 2 + 1; +static const unsigned int chacha20_nonce_hex_len = chacha20_nonce_len * 2 + 1; +void generate_chacah20_and_print(); +unsigned char* chacha20_hexkey2bin(char * hex); +unsigned char* chacha20_hexnonce2bin(char * hex); + + +void generate_aes_and_print(); +unsigned char* aes_hexkey2bin(char * hex); +unsigned char* aes_hexnonce2bin(char * hex); diff --git a/aesjni/src/main/jni/main_unit_test.c b/aesjni/src/main/jni/main_unit_test.c index 308cb99..df316c7 100755 --- a/aesjni/src/main/jni/main_unit_test.c +++ b/aesjni/src/main/jni/main_unit_test.c @@ -5,21 +5,12 @@ you can run a command in this directory: ./rebuild.sh test #define TEST_NAME "aead_aes256gcm" #include -#include +#include #include #include #include -// #include - - -#define TAG "NATIVE_UT" -void logd(char *str) -{ - //this string will be got from you command. - printf("%s \n", str); - //run "adb logcat | grep NATIVE_UT" in order to seek logs. - __android_log_print(ANDROID_LOG_DEBUG, TAG, "%s", str); -} +#include +#include /* There is a testing for AEAD AES256_GCM. @@ -50,18 +41,18 @@ void test_aead_aes256gcm() // to detect CPU has hardware-accelerated for AES256GCM. if (crypto_aead_aes256gcm_is_available() == 0) { - logd("The device doesn't support AES algorithm."); /* Not available on this CPU */ + LOGD("The device doesn't support AES algorithm."); /* Not available on this CPU */ } else { - logd("The device support AES algorithm."); + LOGD("The device support AES algorithm."); - const char *key_hex = "b52c505a37d78eda5dd34f20c22540ea1b58963cf8e5bf8ffa85f9f2492505b4"; - const char *nonce_hex = "516c33929df5a3284ff463d7"; - const char *mac_hex = "bdc1ac884d332457a1d2664f168c76f0"; + const char *key_hex = "b52c505a37d78eda5dd34f20c22540ea1b58963cf8e5bf8ffa85f9f2492505b4"; + const char *nonce_hex = "516c33929df5a3284ff463d7"; + const char *mac_hex = "bdc1ac884d332457a1d2664f168c76f0"; - const size_t message_len = 4; - const unsigned char *message = "test"; + const size_t message_len = 4; + const unsigned char *message = "test"; key = (unsigned char *)sodium_malloc(crypto_aead_aes256gcm_KEYBYTES); nonce = (unsigned char *)sodium_malloc(crypto_aead_aes256gcm_NPUBBYTES); @@ -83,82 +74,81 @@ void test_aead_aes256gcm() } } +//------------------------------------------------------------------------------------------ + /* ChaCha20-Poly1305 is performance on ARM. */ void test_chacha20() { - const unsigned char *PLAIN_TEXT = "test1234test1234test1234test1234END"; - const unsigned int PLAIN_LEN = 35; - const unsigned char *ADDITIONAL_DATA = NULL; - const unsigned int ADDITIONAL_DATA_LEN = 0; - - //static key and nonce with base64encode. - - // unsigned char nonce[crypto_aead_chacha20poly1305_NPUBBYTES]; - // unsigned char key[crypto_aead_chacha20poly1305_KEYBYTES]; - // crypto_aead_chacha20poly1305_keygen(key); - // randombytes_buf(nonce, sizeof nonce); + const char *PLAIN_TEXT = "test123中文END000"; + const unsigned int PLAIN_LEN = 15; + const unsigned char *ADDITIONAL_DATA = NULL;//addtional data is used to verify data. + const unsigned int ADDITIONAL_DATA_LEN = 0; - //key & nonce were generated with random. - const unsigned char *BASE64_NONCE = "wzAeemDo5oY="; - const unsigned char *BASE64_KEY = "cU3wmpJ0cmfMHSWVKbmH4GkU4CBnfARePp1GdrSwQObDMB56YOjmhg=="; - char *nonce = b64_decode(BASE64_NONCE, strlen(BASE64_NONCE)); - char *key = b64_decode(BASE64_KEY, strlen(BASE64_KEY)); + //static key and nonce with hex. + const char *key_hex="9876c42f2f61bee24cc27ebd6155897c46950a83c9b0cc95a9650f9ae7421d07"; + const char *nonce_hex="611dec2f53524315"; + const unsigned char *NONCE = chacha20_hexnonce2bin(nonce_hex); + const unsigned char *KEY = chacha20_hexkey2bin(key_hex); + + LOGI("plain text:%s", PLAIN_TEXT); - logd("key:"); logd(b64_encode(key, strlen(key))); + printf("PLAIN_TEXT\t:"); + print_str(PLAIN_TEXT,(size_t)PLAIN_LEN); - char a =key[strlen(key)-1]; - int ia = (int)a; - printf("last of key: %d\n",ia); + printf("PLAIN_TEXT hex\t:"); + print_chars_in_hex(PLAIN_TEXT, (size_t)PLAIN_LEN); - logd("nonce:"); logd(b64_encode(nonce, strlen(nonce))); - - unsigned char ciphertext[PLAIN_LEN + crypto_aead_chacha20poly1305_ABYTES]; + unsigned char *ciphertext; + ciphertext = (unsigned char *)sodium_malloc(PLAIN_LEN + crypto_aead_chacha20poly1305_ABYTES); unsigned long long ciphertext_len; - crypto_aead_chacha20poly1305_encrypt(ciphertext, &ciphertext_len, PLAIN_TEXT, PLAIN_LEN, ADDITIONAL_DATA, ADDITIONAL_DATA_LEN, //additional data is NULL. - NULL, nonce, key); - - logd("encrypted text: "); logd(b64_encode(ciphertext, ciphertext_len)); + NULL, NONCE, KEY); - char decrypted[PLAIN_LEN]; + unsigned char *decrypted; + decrypted = (unsigned char *)sodium_malloc(PLAIN_LEN); unsigned long long decrypted_len; crypto_aead_chacha20poly1305_decrypt(decrypted, &decrypted_len, - NULL, - ciphertext, ciphertext_len, - ADDITIONAL_DATA,ADDITIONAL_DATA_LEN, - nonce, key); - logd("decrypted text:"); - logd(decrypted); - - if(strcmp(decrypted,PLAIN_TEXT)!=0){ + NULL, + ciphertext, ciphertext_len, + ADDITIONAL_DATA, ADDITIONAL_DATA_LEN, + NONCE, KEY); + if (decrypted_len != PLAIN_LEN) + { + printf("wrong\t:"); abort(); //you won't see "main() end" in terminal. } - free(ciphertext); - free(decrypted); - free(PLAIN_TEXT); + printf("decrypted\t:"); + print_str(decrypted,(size_t)decrypted_len); + printf("decrypted hex\t:"); + print_chars_in_hex(decrypted, (size_t)decrypted_len); + sodium_free(ciphertext); + sodium_free(decrypted); } int main() { - logd("main()...begin"); + LOGI("main()...begin"); sodium_init(); - logd("-----------------------aead_aes256gcm----------------------"); - test_aead_aes256gcm(); + LOGI("-----------------------generate_and_print----------------------"); + generate_chacah20_and_print(); + + LOGI("-----------------------aead_aes256gcm----------------------"); + //test_aead_aes256gcm(); - logd("-----------------------chacha20----------------------"); + LOGI("-----------------------chacha20----------------------"); test_chacha20(); - logd("main()...end"); + LOGI("main()...end"); return 0; } diff --git a/aesjni/src/main/jni/rebuild.sh b/aesjni/src/main/jni/rebuild.sh index a0244c5..d8c7509 100755 --- a/aesjni/src/main/jni/rebuild.sh +++ b/aesjni/src/main/jni/rebuild.sh @@ -1,4 +1,4 @@ -#!/usr/bin/env bash +#!/bin/bash # @author bruce: # ========================Remind:======================== # run this shell script must before, you computer must be set ANROID_NDK_HOME, @@ -12,8 +12,8 @@ # And NDK-r13b got failed due to google not supporting the old ndk to be with new system. # -BUILT_ABI=x86 -#BUILT_ABI=arm64-v8a +#BUILT_ABI=x86 # make it be x86 in order to emulator +BUILT_ABI=armeabi-v7a # make it be x86 in order to physical device # detect whether build_libsodium_for_all_android_abi.sh has already been called. if [ ! -d ./sodium_include/libsodium-android-armv8-a ]; then printf "Please run build_libsodium_for_all_android_abi.sh before.\n" @@ -43,7 +43,7 @@ if [[ $1 == "test" ]]; then exit fi - printf "This is executable file print:\n" + printf "Here is executable file printed:\n" printf "=========================\n" adb shell /data/local/tmp/test printf "=========================\n" diff --git a/aesjni/src/main/jni/str_utils.cpp b/aesjni/src/main/jni/str_utils.cpp new file mode 100644 index 0000000..e2ff2f1 --- /dev/null +++ b/aesjni/src/main/jni/str_utils.cpp @@ -0,0 +1,43 @@ +#include +#include "str_utils.h" +#ifdef __cplusplus +extern "C" +{ +#endif + //----------begin--------- + using namespace std; + + int compare_str(unsigned char *unsigned_str, char *str) + { + return strncmp(reinterpret_cast(unsigned_str), str, strlen(str)); + } + + /** + * Why this function so weird? + * If I use "cout << data << endl;" to print string, it printing empty beacause decrypted data does not set end char. + */ + void print_str(unsigned char *data, size_t len) + { + for (auto i(0U); i < len; i++) + { + cout << data[i]; + } + cout << endl; + } + + void print_chars_in_hex(unsigned char *data, size_t len) + { + if (len == 0) + throw std::exception(); //throw dependencies "-fexceptions" + + for (auto i(0U); i < len; ++i) + { + cout << (i == 0 ? "0x" : ",0x") << setw(2) << setfill('0') << hex << (unsigned int)data[i]; + } + cout << endl; + } + +//----------end--------- +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/aesjni/src/main/jni/str_utils.h b/aesjni/src/main/jni/str_utils.h new file mode 100644 index 0000000..e064b4f --- /dev/null +++ b/aesjni/src/main/jni/str_utils.h @@ -0,0 +1,22 @@ +/** + * @author Bruce. + * C++ operating char string is easier than C,so I operate some string and char in there. + */ + +#include +#include + +#ifdef __cplusplus +#define EXTERNC extern "C" +#else +#define EXTERNC +#endif +//----------begin--------- + +typedef void *mylibrary_mytype_t; +EXTERNC int compare_str(unsigned char *unsigned_str, char *str); + +EXTERNC void print_chars_in_hex(unsigned char *data, size_t len); +EXTERNC void print_str(unsigned char *data, size_t len); +//----------end--------- +#undef EXTERNC \ No newline at end of file From 718131669c2f8fed0025f79e8bab860c0abb88c4 Mon Sep 17 00:00:00 2001 From: bruce Date: Sat, 20 Feb 2021 18:25:49 +0800 Subject: [PATCH 08/21] [algrithm]:{print generated aes key.} --- aesjni/src/main/jni/keys_generator.c | 51 +++++++++++++++++++++++++--- aesjni/src/main/jni/keys_generator.h | 4 +++ aesjni/src/main/jni/main_unit_test.c | 1 + 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/aesjni/src/main/jni/keys_generator.c b/aesjni/src/main/jni/keys_generator.c index 93be405..097ff56 100644 --- a/aesjni/src/main/jni/keys_generator.c +++ b/aesjni/src/main/jni/keys_generator.c @@ -9,19 +9,23 @@ void generate_chacah20_and_print() char *key_hex = (char *)sodium_malloc(chacha20_key_hex_len); sodium_bin2hex(key_hex, (size_t)(chacha20_key_hex_len), key, chacha20_key_len); - printf("generate a key_hex \t:%s\n", key_hex); + printf("generate a chacha20 key_hex \t:%s\n", key_hex); //test key hex2bin chacha20_hexnonce2bin(key_hex); char *nonce_hex = (char *)sodium_malloc(chacha20_nonce_hex_len); sodium_bin2hex(nonce_hex, (size_t)(chacha20_nonce_hex_len), nonce, chacha20_nonce_len); - printf("generate a nonce_hex\t:%s\n", nonce_hex); + printf("generate a chacha20 nonce_hex\t:%s\n", nonce_hex); //test nonce hex2bin chacha20_hexnonce2bin(nonce_hex); + sodium_free(nonce_hex); + sodium_free(key_hex); } -//strlen(hex) must equal to chacha20_key_hex_len +/**strlen(hex) must equal to chacha20_key_hex_len + * dont forget to free char *. +*/ unsigned char *chacha20_hexkey2bin(char *hex) { unsigned char *out_key = (unsigned char *)sodium_malloc(chacha20_key_len); @@ -43,5 +47,44 @@ unsigned char *chacha20_hexnonce2bin(char *hex) void generate_aes_and_print() { - //TODO + unsigned char nonce[crypto_aead_aes256gcm_NPUBBYTES]; + unsigned char key[crypto_aead_aes256gcm_KEYBYTES]; + + sodium_init(); + if (crypto_aead_aes256gcm_is_available() == 0) + { + // return; + } + crypto_aead_aes256gcm_keygen(key); + randombytes_buf(nonce, sizeof nonce); + char *key_hex = (char *)sodium_malloc(aes_key_hex_len); + sodium_bin2hex(key_hex, (size_t)(aes_key_hex_len), key, aes_key_len); + printf("generate a AES key_hex\t\t:%s\n", key_hex); + // test keyhex + aes_hexkey2bin(key_hex); + + char *nonce_hex = (char *)sodium_malloc(aes_nonce_hex_len); + sodium_bin2hex(nonce_hex, (size_t)(aes_nonce_hex_len), nonce, aes_nonce_len); + printf("generate a AES nonce_hex\t:%s\n", nonce_hex); + + //test + aes_hexnonce2bin(nonce_hex); +} + +unsigned char *aes_hexkey2bin(char *hex) +{ + unsigned char *out_key = (unsigned char *)sodium_malloc(aes_key_len); + sodium_hex2bin(out_key, aes_key_len, + hex, aes_key_hex_len, + NULL, NULL, NULL); + return out_key; +} + +unsigned char *aes_hexnonce2bin(char *hex) +{ + unsigned char *out_nonce = (unsigned char *)sodium_malloc(aes_nonce_len); + sodium_hex2bin(out_nonce, aes_nonce_len, + hex, aes_nonce_hex_len, + NULL, NULL, NULL); + return out_nonce; } \ No newline at end of file diff --git a/aesjni/src/main/jni/keys_generator.h b/aesjni/src/main/jni/keys_generator.h index 85373b5..3b89c56 100644 --- a/aesjni/src/main/jni/keys_generator.h +++ b/aesjni/src/main/jni/keys_generator.h @@ -11,6 +11,10 @@ unsigned char* chacha20_hexkey2bin(char * hex); unsigned char* chacha20_hexnonce2bin(char * hex); +static const unsigned int aes_key_len = crypto_aead_aes256gcm_KEYBYTES; +static const unsigned int aes_nonce_len = crypto_aead_aes256gcm_NPUBBYTES; +static const unsigned int aes_key_hex_len = aes_key_len * 2 + 1; +static const unsigned int aes_nonce_hex_len = aes_nonce_len * 2 + 1; void generate_aes_and_print(); unsigned char* aes_hexkey2bin(char * hex); unsigned char* aes_hexnonce2bin(char * hex); diff --git a/aesjni/src/main/jni/main_unit_test.c b/aesjni/src/main/jni/main_unit_test.c index df316c7..6b8a801 100755 --- a/aesjni/src/main/jni/main_unit_test.c +++ b/aesjni/src/main/jni/main_unit_test.c @@ -141,6 +141,7 @@ int main() LOGI("-----------------------generate_and_print----------------------"); generate_chacah20_and_print(); + generate_aes_and_print(); LOGI("-----------------------aead_aes256gcm----------------------"); //test_aead_aes256gcm(); From 85fc4ef3f012842c9b798ee44267fa392473aa25 Mon Sep 17 00:00:00 2001 From: bruce Date: Mon, 22 Feb 2021 11:28:41 +0800 Subject: [PATCH 09/21] [AES]:{using aes256gcm context to encrypt&decrypt.} --- FigureOutJNICrash.md | 8 +-- aesjni/src/main/jni/keys_generator.c | 18 ++++--- aesjni/src/main/jni/keys_generator.h | 8 +++ aesjni/src/main/jni/main_unit_test.c | 78 +++++++++++++--------------- aesjni/src/main/jni/rebuild.sh | 9 ++-- 5 files changed, 64 insertions(+), 57 deletions(-) diff --git a/FigureOutJNICrash.md b/FigureOutJNICrash.md index 79b1551..7a526fb 100644 --- a/FigureOutJNICrash.md +++ b/FigureOutJNICrash.md @@ -2,11 +2,13 @@ How to locate where native crash at? ------------ 1.ensure you ndk is set into environment. -2.run **ndk-stack** +2.run **ndk-stack** to analyze logcat which code crash printed. ``` adb logcat | ndk-stack -sym ../obj/local/armeabi-v7a/ +//or +adb logcat | ndk-stack -sym ../obj/local/x86/ ``` - +> **../obj/local/x86/** and **../obj/local/armeabi-v7a/** is symbol file directory. ### Example: I got crash logs: @@ -59,7 +61,7 @@ Stack frame #05 pc 0000113d /data/local/tmp/test: Routine main at /Users/bruce/ ``` -My conclusion: `/Users/bruce/Documents/git/AESJniEncrypt/aesjni/src/main/jni/main_unit_test.c:146` is where the executable file crash at. +My conclusion: `/Users/******/git/AESJniEncrypt/aesjni/src/main/jni/main_unit_test.c:146` is where the executable file crash at. ## Remind *ndk-stack* of NDK-R13 **only supports** symbol file of armeabi-v7a. diff --git a/aesjni/src/main/jni/keys_generator.c b/aesjni/src/main/jni/keys_generator.c index 097ff56..8f3b6f8 100644 --- a/aesjni/src/main/jni/keys_generator.c +++ b/aesjni/src/main/jni/keys_generator.c @@ -45,16 +45,18 @@ unsigned char *chacha20_hexnonce2bin(char *hex) return out_nonce; } + +void init_aes256(){ + aes256gcm_ctx = sodium_malloc(sizeof(AES256GCM_CTX)); + memset(aes256gcm_ctx, 0, sizeof(AES256GCM_CTX)); +} + + void generate_aes_and_print() { - unsigned char nonce[crypto_aead_aes256gcm_NPUBBYTES]; - unsigned char key[crypto_aead_aes256gcm_KEYBYTES]; - - sodium_init(); - if (crypto_aead_aes256gcm_is_available() == 0) - { - // return; - } + unsigned char nonce[aes_nonce_len]; + unsigned char key[aes_key_len]; + crypto_aead_aes256gcm_keygen(key); randombytes_buf(nonce, sizeof nonce); char *key_hex = (char *)sodium_malloc(aes_key_hex_len); diff --git a/aesjni/src/main/jni/keys_generator.h b/aesjni/src/main/jni/keys_generator.h index 3b89c56..6924d57 100644 --- a/aesjni/src/main/jni/keys_generator.h +++ b/aesjni/src/main/jni/keys_generator.h @@ -11,6 +11,14 @@ unsigned char* chacha20_hexkey2bin(char * hex); unsigned char* chacha20_hexnonce2bin(char * hex); + +//it is important to be used for AES256. +typedef crypto_aead_aes256gcm_state AES256GCM_CTX; + +//only one aes256gcm context. +static AES256GCM_CTX *aes256gcm_ctx; +void init_aes256(); + static const unsigned int aes_key_len = crypto_aead_aes256gcm_KEYBYTES; static const unsigned int aes_nonce_len = crypto_aead_aes256gcm_NPUBBYTES; static const unsigned int aes_key_hex_len = aes_key_len * 2 + 1; diff --git a/aesjni/src/main/jni/main_unit_test.c b/aesjni/src/main/jni/main_unit_test.c index 6b8a801..cad415e 100755 --- a/aesjni/src/main/jni/main_unit_test.c +++ b/aesjni/src/main/jni/main_unit_test.c @@ -11,28 +11,16 @@ you can run a command in this directory: ./rebuild.sh test #include #include #include +#include /* There is a testing for AEAD AES256_GCM. + +generated AES key_hex :d56aca0c41806fded2bad76c680a6664156f8d868b2fc7c51fba0f858ec910f4 +generated AES nonce_hex :2a42a84c3a82a1dcf1d83b07 */ void test_aead_aes256gcm() { - unsigned char *ad; - unsigned char *ciphertext; - unsigned char *decrypted; - unsigned char *detached_ciphertext; - unsigned char *expected_ciphertext; - unsigned char *key; - unsigned char *mac; - unsigned char *nonce; - char *hex; - unsigned long long found_ciphertext_len; - unsigned long long found_mac_len; - unsigned long long found_message_len; - size_t ad_len; - size_t ciphertext_len; - size_t detached_ciphertext_len; - size_t i = 0U; /* AES256-GCM is not high performance on ARM. @@ -41,36 +29,41 @@ void test_aead_aes256gcm() // to detect CPU has hardware-accelerated for AES256GCM. if (crypto_aead_aes256gcm_is_available() == 0) { - LOGD("The device doesn't support AES algorithm."); /* Not available on this CPU */ + printf("This device doesn't support AES algorithm.\n="); + LOGI("This device doesn't support AES algorithm."); /* Not available on this CPU */ } else { - LOGD("The device support AES algorithm."); + LOGI("This device supports AES algorithm."); - const char *key_hex = "b52c505a37d78eda5dd34f20c22540ea1b58963cf8e5bf8ffa85f9f2492505b4"; - const char *nonce_hex = "516c33929df5a3284ff463d7"; - const char *mac_hex = "bdc1ac884d332457a1d2664f168c76f0"; + init_aes256(); + const char *key_hex = "d56aca0c41806fded2bad76c680a6664156f8d868b2fc7c51fba0f858ec910f4"; + const char *nonce_hex = "2a42a84c3a82a1dcf1d83b07"; + const unsigned char *key = aes_hexkey2bin(key_hex); + const unsigned char *nonce = aes_hexnonce2bin(nonce_hex); const size_t message_len = 4; const unsigned char *message = "test"; - key = (unsigned char *)sodium_malloc(crypto_aead_aes256gcm_KEYBYTES); - nonce = (unsigned char *)sodium_malloc(crypto_aead_aes256gcm_NPUBBYTES); - mac = (unsigned char *)sodium_malloc(crypto_aead_aes256gcm_ABYTES); + if(crypto_aead_aes256gcm_beforenm(aes256gcm_ctx,key)==0){ + printf("aes beforenm called."); + } - sodium_hex2bin(key, crypto_aead_aes256gcm_KEYBYTES, - key_hex, strlen(key_hex), - NULL, NULL, NULL); - sodium_hex2bin(nonce, crypto_aead_aes256gcm_NPUBBYTES, - nonce_hex, strlen(nonce_hex), - NULL, NULL, NULL); - - crypto_aead_aes256gcm_encrypt(ciphertext, &found_ciphertext_len, + unsigned char *ciphertext; + unsigned long long found_ciphertext_len; + crypto_aead_aes256gcm_encrypt_afternm(ciphertext, &found_ciphertext_len, message, message_len, - NULL, 0, NULL, nonce, key); + "test", 4, + NULL, nonce, aes256gcm_ctx); + + const unsigned char *decrypted; + unsigned long long found_dec_len; + crypto_aead_aes256gcm_decrypt_afternm(decrypted, &found_dec_len, NULL, + ciphertext, found_ciphertext_len, + "test", 4, + nonce, aes256gcm_ctx); - crypto_aead_aes256gcm_decrypt(NULL, NULL, NULL, ciphertext, strlen(ciphertext), - NULL, 0, nonce, key); + print_str(decrypted, (size_t)found_dec_len); } } @@ -83,19 +76,19 @@ void test_chacha20() { const char *PLAIN_TEXT = "test123中文END000"; const unsigned int PLAIN_LEN = 15; - const unsigned char *ADDITIONAL_DATA = NULL;//addtional data is used to verify data. + const unsigned char *ADDITIONAL_DATA = NULL; //addtional data is used to verify data. const unsigned int ADDITIONAL_DATA_LEN = 0; //static key and nonce with hex. - const char *key_hex="9876c42f2f61bee24cc27ebd6155897c46950a83c9b0cc95a9650f9ae7421d07"; - const char *nonce_hex="611dec2f53524315"; + const char *key_hex = "9876c42f2f61bee24cc27ebd6155897c46950a83c9b0cc95a9650f9ae7421d07"; + const char *nonce_hex = "611dec2f53524315"; const unsigned char *NONCE = chacha20_hexnonce2bin(nonce_hex); const unsigned char *KEY = chacha20_hexkey2bin(key_hex); - + LOGI("plain text:%s", PLAIN_TEXT); printf("PLAIN_TEXT\t:"); - print_str(PLAIN_TEXT,(size_t)PLAIN_LEN); + print_str(PLAIN_TEXT, (size_t)PLAIN_LEN); printf("PLAIN_TEXT hex\t:"); print_chars_in_hex(PLAIN_TEXT, (size_t)PLAIN_LEN); @@ -124,7 +117,7 @@ void test_chacha20() } printf("decrypted\t:"); - print_str(decrypted,(size_t)decrypted_len); + print_str(decrypted, (size_t)decrypted_len); printf("decrypted hex\t:"); print_chars_in_hex(decrypted, (size_t)decrypted_len); @@ -142,9 +135,10 @@ int main() LOGI("-----------------------generate_and_print----------------------"); generate_chacah20_and_print(); generate_aes_and_print(); + printf("\n"); LOGI("-----------------------aead_aes256gcm----------------------"); - //test_aead_aes256gcm(); + test_aead_aes256gcm(); LOGI("-----------------------chacha20----------------------"); test_chacha20(); diff --git a/aesjni/src/main/jni/rebuild.sh b/aesjni/src/main/jni/rebuild.sh index d8c7509..7ea8490 100755 --- a/aesjni/src/main/jni/rebuild.sh +++ b/aesjni/src/main/jni/rebuild.sh @@ -2,7 +2,8 @@ # @author bruce: # ========================Remind:======================== # run this shell script must before, you computer must be set ANROID_NDK_HOME, -# And your computer must connect TODO . +# And your computer must connect a device or emulator. +# Pls modify BUILT_ABI for build compatible you device. # ------------------------------------------------------- # Build success environmental system records: # Mac OS: @@ -12,10 +13,10 @@ # And NDK-r13b got failed due to google not supporting the old ndk to be with new system. # -#BUILT_ABI=x86 # make it be x86 in order to emulator -BUILT_ABI=armeabi-v7a # make it be x86 in order to physical device +#BUILT_ABI=x86 # make it compatible for emulator +BUILT_ABI=armeabi-v7a # make it compatible for physical device # detect whether build_libsodium_for_all_android_abi.sh has already been called. -if [ ! -d ./sodium_include/libsodium-android-armv8-a ]; then +if [ ! -d ./sodium_include/libsodium-android-armv7-a ]; then printf "Please run build_libsodium_for_all_android_abi.sh before.\n" exit fi From 5bb75346a0187fddec4d6d0a35e3ff0e4daa0405 Mon Sep 17 00:00:00 2001 From: bruce Date: Mon, 22 Feb 2021 15:14:42 +0800 Subject: [PATCH 10/21] [build]:{make libsodium encrypt&decrypt suc as app on x86 with NDK-r13 .} --- aesjni/build.gradle | 13 +- .../java/com/androidyuan/aesjni/JNITest.java | 29 +++ aesjni/src/main/cpp/JNIEncrypt.c | 10 +- aesjni/src/main/jni/Application.mk | 2 +- aesjni/src/main/jni/JNIEncrypt.c | 203 ++++++++++-------- aesjni/src/main/jni/OriginAndroid.mk | 16 +- aesjni/src/main/jni/TestAndroid.mk | 4 +- aesjni/src/main/jni/debugger.h | 2 +- aesjni/src/main/jni/keys_generator.c | 9 +- aesjni/src/main/jni/keys_generator.h | 4 +- aesjni/src/main/jni/main_unit_test.c | 4 +- app/build.gradle | 2 +- .../aesjniencrypt/MainActivity.java | 16 +- build.gradle | 2 + 14 files changed, 187 insertions(+), 129 deletions(-) create mode 100644 aesjni/src/androidTest/java/com/androidyuan/aesjni/JNITest.java diff --git a/aesjni/build.gradle b/aesjni/build.gradle index 7bc1877..739c565 100644 --- a/aesjni/build.gradle +++ b/aesjni/build.gradle @@ -2,9 +2,8 @@ apply plugin: 'com.android.library' android { compileSdkVersion 25 - buildToolsVersion "25.0.3" - ndkPath "/mnt/mylibs/sdk/android-ndk-r21d/" - + //ndkPath "/Users/bruce/Library/Android/sdk/ndk/16.1.4479499" +// ndkVersion '16.1.4479499' defaultConfig { minSdkVersion 21 targetSdkVersion 25 @@ -21,7 +20,7 @@ android { ndk { // Specifies the ABI configurations of your native // libraries Gradle should build and package with your APK. - abiFilters 'arm64-v8a' + abiFilters 'x86' } } @@ -55,10 +54,10 @@ android { dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) + implementation fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:25.0.1' - testCompile 'junit:junit:4.12' + implementation 'com.android.support:appcompat-v7:25.0.1' + testImplementation 'junit:junit:4.12' } diff --git a/aesjni/src/androidTest/java/com/androidyuan/aesjni/JNITest.java b/aesjni/src/androidTest/java/com/androidyuan/aesjni/JNITest.java new file mode 100644 index 0000000..1fdc042 --- /dev/null +++ b/aesjni/src/androidTest/java/com/androidyuan/aesjni/JNITest.java @@ -0,0 +1,29 @@ +package com.androidyuan.aesjni; + +import android.support.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertEquals; + +/** + * Instrumentation test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class JNITest { + + private final String plain = "123abcABC*%!~#+_/中文测试"; + private final String encodeStr = "1cad994119e4f3038fe29e34a4a12ce8a4b28f06db7682ab9d4f775c0e25659b01da27db09368eb3778ccea063"; + @Test + public void useAppContext() throws Exception { + + //plain:123abcABC&*(@#@#@)+_/中文测试 + final String code = AESEncrypt.encode(this, plain); + assertEquals(code,encodeStr); + final String decode = AESEncrypt.decode(this, encodeStr); + assertEquals(plain,decode); + } +} diff --git a/aesjni/src/main/cpp/JNIEncrypt.c b/aesjni/src/main/cpp/JNIEncrypt.c index 028f3dd..51b13e0 100644 --- a/aesjni/src/main/cpp/JNIEncrypt.c +++ b/aesjni/src/main/cpp/JNIEncrypt.c @@ -18,7 +18,7 @@ const char *UNSIGNATURE = "UNSIGNATURE"; -jstring charToJstring(JNIEnv *envPtr, char *src) { +jstring char2jstring(JNIEnv *envPtr, char *src) { JNIEnv env = *envPtr; jsize len = strlen(src); @@ -71,7 +71,7 @@ JNIEXPORT jstring JNICALL encode(JNIEnv *env, jobject instance, jobject context, if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) { char *str = UNSIGNATURE; // return (*env)->NewString(env, str, strlen(str)); - return charToJstring(env,str); + return char2jstring(env,str); } uint8_t *AES_KEY = (uint8_t *) getKey(); @@ -93,7 +93,7 @@ JNIEXPORT jstring JNICALL decode(JNIEnv *env, jobject instance, jobject context, if (check_signature(env, instance, context) != 1|| check_is_emulator(env) != 1) { char *str = UNSIGNATURE; // return (*env)->NewString(env, str, strlen(str)); - return charToJstring(env,str); + return char2jstring(env,str); } uint8_t *AES_KEY = (uint8_t *) getKey(); @@ -102,8 +102,8 @@ JNIEXPORT jstring JNICALL decode(JNIEnv *env, jobject instance, jobject context, (*env)->ReleaseStringUTFChars(env, str_, str); // return (*env)->NewStringUTF(env, desResult); //不用系统自带的方法NewStringUTF是因为如果desResult是乱码,会抛出异常 -// return charToJstring(env,desResult); - jstring result = charToJstring(env,desResult); +// return char2jstring(env,desResult); + jstring result = char2jstring(env,desResult); free(desResult); free(AES_KEY); return result; diff --git a/aesjni/src/main/jni/Application.mk b/aesjni/src/main/jni/Application.mk index 25ef8c4..2470dfb 100755 --- a/aesjni/src/main/jni/Application.mk +++ b/aesjni/src/main/jni/Application.mk @@ -1,5 +1,5 @@ # there is only armv8. You can rebuild libsodium to generate static library of other ABIs. -APP_ABI := armeabi-v7a,x86 +APP_ABI := armeabi-v7a,x86,arm64-v8a APP_PLATFORM := android-19 APP_STL := c++_static \ No newline at end of file diff --git a/aesjni/src/main/jni/JNIEncrypt.c b/aesjni/src/main/jni/JNIEncrypt.c index 0f815fc..9b5b5d3 100755 --- a/aesjni/src/main/jni/JNIEncrypt.c +++ b/aesjni/src/main/jni/JNIEncrypt.c @@ -1,27 +1,22 @@ #include - #include #include "checksignature.h" #include "check_emulator.h" +#include "keys_generator.h" #include #include -#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) -#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) - -#define CBC 1 -#define ECB 1 - // 获取数组的大小 -# define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) +#define NELEM(x) ((int)(sizeof(x) / sizeof((x)[0]))) // 指定要注册的类,对应完整的java类名 #define JNIREG_CLASS "com/androidyuan/aesjni/AESEncrypt" const char *UNSIGNATURE = "UNSIGNATURE"; -jstring charToJstring(JNIEnv *envPtr, char *src) { +jstring char2jstring(JNIEnv *envPtr, char *src) +{ JNIEnv env = *envPtr; jsize len = strlen(src); @@ -30,133 +25,161 @@ jstring charToJstring(JNIEnv *envPtr, char *src) { jmethodID mid = env->GetMethodID(envPtr, clsstring, "", "([BLjava/lang/String;)V"); jbyteArray barr = env->NewByteArray(envPtr, len); - env->SetByteArrayRegion(envPtr, barr, 0, len, (jbyte *) src); - - return (jstring) env->NewObject(envPtr, clsstring, mid, barr, strencode); -} - -//__attribute__((section (".mytext")))//隐藏字符表 并没有什么卵用 只是针对初阶hacker的一个小方案而已 -char *getKey() { - int n = 0; - char s[23];//"NMTIzNDU2Nzg5MGFiY2RlZg"; - - s[n++] = 'N'; - s[n++] = 'M'; - s[n++] = 'T'; - s[n++] = 'I'; - s[n++] = 'z'; - s[n++] = 'N'; - s[n++] = 'D'; - s[n++] = 'U'; - s[n++] = '2'; - s[n++] = 'N'; - s[n++] = 'z'; - s[n++] = 'g'; - s[n++] = '5'; - s[n++] = 'M'; - s[n++] = 'G'; - s[n++] = 'F'; - s[n++] = 'i'; - s[n++] = 'Y'; - s[n++] = '2'; - s[n++] = 'R'; - s[n++] = 'l'; - s[n++] = 'Z'; - s[n++] = 'g'; - char *encode_str = s + 1; - return b64_decode(encode_str, strlen(encode_str)); + env->SetByteArrayRegion(envPtr, barr, 0, len, (jbyte *)src); + return (jstring)env->NewObject(envPtr, clsstring, mid, barr, strencode); } -JNIEXPORT jstring JNICALL encode(JNIEnv *env, jobject instance, jobject context, jstring str_) { - - //先进行apk被 二次打包的校验 - if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) { - char *str = UNSIGNATURE; -// return (*env)->NewString(env, str, strlen(str)); - return charToJstring(env,str); - } - +jstring char2string_with_len(JNIEnv *envPtr, char *src, size_t str_len) +{ + JNIEnv env = *envPtr; + jsize len = (jsize)str_len; + jclass clsstring = env->FindClass(envPtr, "java/lang/String"); + jstring strencode = env->NewStringUTF(envPtr, "UTF-8"); + jmethodID mid = env->GetMethodID(envPtr, clsstring, "", + "([BLjava/lang/String;)V"); + jbyteArray barr = env->NewByteArray(envPtr, len); + env->SetByteArrayRegion(envPtr, barr, 0, len, (jbyte *)src); - return (*env)->NewStringUTF(env, ""); + return (jstring)env->NewObject(envPtr, clsstring, mid, barr, strencode); } +unsigned char *getNonce() +{ + const char *nonce_hex = "611dec2f53524315"; + const unsigned char *NONCE = chacha20_hexnonce2bin(nonce_hex); + return NONCE; +} -JNIEXPORT jstring JNICALL decode(JNIEnv *env, jobject instance, jobject context, jstring str_) { - +//__attribute__((section (".mytext")))//隐藏字符表 并没有什么卵用 只是针对初阶hacker的一个小方案而已 +unsigned char *getKey() +{ + const char *key_hex = "9876c42f2f61bee24cc27ebd6155897c46950a83c9b0cc95a9650f9ae7421d07"; + const unsigned char *KEY = chacha20_hexkey2bin(key_hex); + return KEY; +} +JNIEXPORT jstring JNICALL encode(JNIEnv *env, jobject instance, jobject context, jstring str_) +{ + sodium_init(); //先进行apk被 二次打包的校验 - if (check_signature(env, instance, context) != 1|| check_is_emulator(env) != 1) { - char *str = UNSIGNATURE; -// return (*env)->NewString(env, str, strlen(str)); - return charToJstring(env,str); + // if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) { + // char *str = UNSIGNATURE; + // return char2jstring(env,str); + // } + + const char *plain_str = (*env)->GetStringUTFChars(env, str_, JNI_FALSE); + (*env)->ReleaseStringUTFChars(env, str_, plain_str); + + unsigned char *ciphertext; + ciphertext = (unsigned char *)sodium_malloc(strlen(plain_str) + crypto_aead_chacha20poly1305_ABYTES); + unsigned long long ciphertext_len; + + crypto_aead_chacha20poly1305_encrypt(ciphertext, &ciphertext_len, + plain_str, strlen(plain_str), + NULL, 0, //additional data is NULL, you can change it. + NULL, getNonce(), getKey()); + if (ciphertext_len == 0) + { + abort(); } - uint8_t *AES_KEY = (uint8_t *) getKey(); - const char *str = (*env)->GetStringUTFChars(env, str_, JNI_FALSE); - (*env)->ReleaseStringUTFChars(env, str_, str); -// return (*env)->NewStringUTF(env, desResult); - //不用系统自带的方法NewStringUTF是因为如果desResult是乱码,会抛出异常 - return (*env)->NewStringUTF(env, ""); + char *result_hex = (char *)sodium_malloc(2 * ciphertext_len + 1); //return hex is easy to transport in internet. + sodium_bin2hex(result_hex, (size_t)(2 * ciphertext_len + 1), ciphertext, ciphertext_len); + return (*env)->NewStringUTF(env, result_hex); } +JNIEXPORT jstring JNICALL decode(JNIEnv *env, jobject instance, jobject context, jstring str_) +{ + + //security checking. + // if (check_signature(env, instance, context) != 1|| check_is_emulator(env) != 1) { + // char *str = UNSIGNATURE; + // return char2jstring(env,str); + // } + + //str_ must is hex. + const char *hex_str = (*env)->GetStringUTFChars(env, str_, JNI_FALSE); + (*env)->ReleaseStringUTFChars(env, str_, hex_str); + + int encrypt_len = strlen(hex_str) / 2; + unsigned char *encrypted_str = (unsigned char *)sodium_malloc(encrypt_len); + sodium_hex2bin(encrypted_str, encrypt_len, + hex_str, strlen(hex_str), + NULL, NULL, NULL); + + unsigned char *decrypted; + decrypted = (unsigned char *)sodium_malloc(encrypt_len); + unsigned long long decrypted_len; + crypto_aead_chacha20poly1305_decrypt(decrypted, &decrypted_len, + NULL, + encrypted_str, encrypt_len, + NULL, 0, + getNonce(), getKey()); + + //It cant use NewStringUTF, if decrypted is Garbled code ,use NewStringUTF will throw exception. + return char2string_with_len(env, decrypted, (size_t)decrypted_len); //decrypted doesnt has '\0',so I put decrypted_len. +} /** * if rerurn 1 ,is check pass. */ JNIEXPORT jint JNICALL -check_jni(JNIEnv *env, jobject instance, jobject con) { +check_jni(JNIEnv *env, jobject instance, jobject con) +{ return check_signature(env, instance, con); } - // Java和JNI函数的绑定表 static JNINativeMethod method_table[] = { - {"checkSignature", "(Ljava/lang/Object;)I", (void *) check_jni}, - {"decode", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/String;", (void *) decode}, - {"encode", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/String;", (void *) encode}, + {"checkSignature", "(Ljava/lang/Object;)I", (void *)check_jni}, + {"decode", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/String;", (void *)decode}, + {"encode", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/String;", (void *)encode}, }; // 注册native方法到java中 static int registerNativeMethods(JNIEnv *env, const char *className, - JNINativeMethod *gMethods, int numMethods) { + JNINativeMethod *gMethods, int numMethods) +{ jclass clazz; clazz = (*env)->FindClass(env, className); - if (clazz == NULL) { + if (clazz == NULL) + { return JNI_FALSE; } - if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) { + if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) + { return JNI_FALSE; } return JNI_TRUE; } -int register_ndk_load(JNIEnv *env) { +int register_ndk_load(JNIEnv *env) +{ // 调用注册方法 return registerNativeMethods(env, JNIREG_CLASS, method_table, NELEM(method_table)); } -JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { - -ptrace(PTRACE_TRACEME, 0, 0, 0);//反调试 -//这是一种比较简单的防止被调试的方案 -// 有更复杂更高明的方案,比如:不用这个ptrace而是每次执行加密解密签先去判断是否被trace,目前的版本不做更多的负载方案,您想做可以fork之后,自己去做 - +JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) +{ + ptrace(PTRACE_TRACEME, 0, 0, 0); //反调试 + //这是一种比较简单的防止被调试的方案 + // 有更复杂更高明的方案,比如:不用这个ptrace而是每次执行加密解密签先去判断是否被trace,目前的版本不做更多的负载方案,您想做可以fork之后,自己去做 -JNIEnv *env = NULL; -jint result = -1; + JNIEnv *env = NULL; + jint result = -1; -if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_4) != JNI_OK) { -return result; -} + if ((*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_4) != JNI_OK) + { + return result; + } -register_ndk_load(env); + register_ndk_load(env); -// 返回jni的版本 -return JNI_VERSION_1_4; + // 返回jni的版本 + return JNI_VERSION_1_4; } - diff --git a/aesjni/src/main/jni/OriginAndroid.mk b/aesjni/src/main/jni/OriginAndroid.mk index 62c56ff..5655abf 100755 --- a/aesjni/src/main/jni/OriginAndroid.mk +++ b/aesjni/src/main/jni/OriginAndroid.mk @@ -1,3 +1,4 @@ +# ========== static sodium begin ================== LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) @@ -19,21 +20,24 @@ endif SODIUM_LIB_DIR := $(LOCAL_PATH)/sodium_include/libsodium-android-$(ARCH_PREFIX) SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a - -LOCAL_MODULE := sodium -LOCAL_SRC_FILES := $(SODIUM_LIB) +LOCAL_MODULE:= sodium +LOCAL_SRC_FILES:= $(SODIUM_LIB) LOCAL_EXPORT_C_INCLUDES := $(SODIUM_INC_DIR) $(SODIUM_INC_DIR)/sodium include $(PREBUILT_STATIC_LIBRARY) +# ========== static sodium end ================== + include $(CLEAR_VARS) LOCAL_MODULE := JNIEncrypt LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ + base64.c \ + keys_generator.c \ + str_utils.cpp \ checksignature.c \ check_emulator.c \ debugger.c \ - logger.h \ - base64.c + logger.h LOCAL_CFLAGS += -Wall -g -pedantic -std=c99 # if you want to print logs pls write liblog on this line. @@ -41,6 +45,6 @@ LOCAL_STATIC_LIBRARIES := sodium liblog libcutils # LOCAL_SHARED_LIBRARIES := liblog libcutils LOCAL_LDLIBS := -llog - +LOCAL_CPPFLAGS := -fexceptions include $(BUILD_SHARED_LIBRARY) diff --git a/aesjni/src/main/jni/TestAndroid.mk b/aesjni/src/main/jni/TestAndroid.mk index 7d6be05..2cad59c 100755 --- a/aesjni/src/main/jni/TestAndroid.mk +++ b/aesjni/src/main/jni/TestAndroid.mk @@ -1,3 +1,4 @@ +# ========== static sodium begin ================== LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) @@ -16,8 +17,6 @@ else ARCH_PREFIX := $(TARGET_ARCH) endif -# ========== sodium ================== -#include $(CLEAR_VARS) SODIUM_LIB_DIR := $(LOCAL_PATH)/sodium_include/libsodium-android-$(ARCH_PREFIX) SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a @@ -26,6 +25,7 @@ LOCAL_SRC_FILES:= $(SODIUM_LIB) LOCAL_EXPORT_C_INCLUDES := $(SODIUM_INC_DIR) $(SODIUM_INC_DIR)/sodium include $(PREBUILT_STATIC_LIBRARY) +# ========== static sodium end ================== diff --git a/aesjni/src/main/jni/debugger.h b/aesjni/src/main/jni/debugger.h index 6652521..295f694 100755 --- a/aesjni/src/main/jni/debugger.h +++ b/aesjni/src/main/jni/debugger.h @@ -12,4 +12,4 @@ // result 1: isdebug 0: not debug int is_debug(); -#endif \ No newline at end of file +#endif diff --git a/aesjni/src/main/jni/keys_generator.c b/aesjni/src/main/jni/keys_generator.c index 8f3b6f8..bcc1230 100644 --- a/aesjni/src/main/jni/keys_generator.c +++ b/aesjni/src/main/jni/keys_generator.c @@ -46,6 +46,7 @@ unsigned char *chacha20_hexnonce2bin(char *hex) } +//call this must void init_aes256(){ aes256gcm_ctx = sodium_malloc(sizeof(AES256GCM_CTX)); memset(aes256gcm_ctx, 0, sizeof(AES256GCM_CTX)); @@ -63,17 +64,17 @@ void generate_aes_and_print() sodium_bin2hex(key_hex, (size_t)(aes_key_hex_len), key, aes_key_len); printf("generate a AES key_hex\t\t:%s\n", key_hex); // test keyhex - aes_hexkey2bin(key_hex); + aes_keyhex2bin(key_hex); char *nonce_hex = (char *)sodium_malloc(aes_nonce_hex_len); sodium_bin2hex(nonce_hex, (size_t)(aes_nonce_hex_len), nonce, aes_nonce_len); printf("generate a AES nonce_hex\t:%s\n", nonce_hex); //test - aes_hexnonce2bin(nonce_hex); + aes_noncehex2bin(nonce_hex); } -unsigned char *aes_hexkey2bin(char *hex) +unsigned char *aes_keyhex2bin(char *hex) { unsigned char *out_key = (unsigned char *)sodium_malloc(aes_key_len); sodium_hex2bin(out_key, aes_key_len, @@ -82,7 +83,7 @@ unsigned char *aes_hexkey2bin(char *hex) return out_key; } -unsigned char *aes_hexnonce2bin(char *hex) +unsigned char *aes_noncehex2bin(char *hex) { unsigned char *out_nonce = (unsigned char *)sodium_malloc(aes_nonce_len); sodium_hex2bin(out_nonce, aes_nonce_len, diff --git a/aesjni/src/main/jni/keys_generator.h b/aesjni/src/main/jni/keys_generator.h index 6924d57..0f303cd 100644 --- a/aesjni/src/main/jni/keys_generator.h +++ b/aesjni/src/main/jni/keys_generator.h @@ -24,5 +24,5 @@ static const unsigned int aes_nonce_len = crypto_aead_aes256gcm_NPUBBYTES; static const unsigned int aes_key_hex_len = aes_key_len * 2 + 1; static const unsigned int aes_nonce_hex_len = aes_nonce_len * 2 + 1; void generate_aes_and_print(); -unsigned char* aes_hexkey2bin(char * hex); -unsigned char* aes_hexnonce2bin(char * hex); +unsigned char* aes_keyhex2bin(char * hex); +unsigned char* aes_noncehex2bin(char * hex); diff --git a/aesjni/src/main/jni/main_unit_test.c b/aesjni/src/main/jni/main_unit_test.c index cad415e..0d57168 100755 --- a/aesjni/src/main/jni/main_unit_test.c +++ b/aesjni/src/main/jni/main_unit_test.c @@ -40,8 +40,8 @@ void test_aead_aes256gcm() const char *key_hex = "d56aca0c41806fded2bad76c680a6664156f8d868b2fc7c51fba0f858ec910f4"; const char *nonce_hex = "2a42a84c3a82a1dcf1d83b07"; - const unsigned char *key = aes_hexkey2bin(key_hex); - const unsigned char *nonce = aes_hexnonce2bin(nonce_hex); + const unsigned char *key = aes_keyhex2bin(key_hex); + const unsigned char *nonce = aes_noncehex2bin(nonce_hex); const size_t message_len = 4; const unsigned char *message = "test"; diff --git a/app/build.gradle b/app/build.gradle index d7fe2c8..05f174c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -5,7 +5,7 @@ android { buildToolsVersion "25.0.3" defaultConfig { applicationId "com.androidyuan.aesjniencrypt" - minSdkVersion 15 + minSdkVersion 21 targetSdkVersion 25 versionCode 1 versionName "1.0" diff --git a/app/src/main/java/com/androidyuan/aesjniencrypt/MainActivity.java b/app/src/main/java/com/androidyuan/aesjniencrypt/MainActivity.java index c01e4ce..3c8656b 100644 --- a/app/src/main/java/com/androidyuan/aesjniencrypt/MainActivity.java +++ b/app/src/main/java/com/androidyuan/aesjniencrypt/MainActivity.java @@ -1,17 +1,16 @@ package com.androidyuan.aesjniencrypt; -import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.util.Log; import android.widget.Toast; -import com.androidyuan.aesjni.AESEncrypt; -import com.androidyuan.aesjni.SignatureTool; -import android.util.Log; +import com.androidyuan.aesjni.AESEncrypt; public class MainActivity extends AppCompatActivity { private final String str = "123abcABC*%!~#+_/中文测试"; - private final String ecode = "SkiDk/JC5F/BXKf/np7rWNub7ibxzYMjKwkQ7A6AqPw="; + private final String encodeStr = "1cad994119e4f3038fe29e34a4a12ce8a4b28f06db7682ab9d4f775c0e25659b01da27db09368eb3778ccea063"; @Override protected void onCreate(Bundle savedInstanceState) { @@ -20,10 +19,11 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); //下面的密文对应的原文:123abcABC&*(@#@#@)+_/中文测试 - //String code = AESEncrypt.encode(this, str); - String code = AESEncrypt.decode(this, ecode); - + final String code = AESEncrypt.encode(this, str); Log.i("code", code + ""); + final String decode = AESEncrypt.decode(this, encodeStr); + Log.i("decode", decode + ""); + } diff --git a/build.gradle b/build.gradle index 3f9dfab..5afb593 100644 --- a/build.gradle +++ b/build.gradle @@ -2,6 +2,7 @@ buildscript { repositories { + //if you are in China, enable this line.It make you build fast. maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } maven { url "https://jitpack.io" } jcenter() @@ -19,6 +20,7 @@ allprojects { repositories { maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } maven { url "https://jitpack.io" } + google() jcenter() } } From 59ff93e881a735fd1025845021dfe1d477e825b0 Mon Sep 17 00:00:00 2001 From: bruce Date: Mon, 22 Feb 2021 18:40:58 +0800 Subject: [PATCH 11/21] [native]:{add shell script for building libsodium static library.} in addition,some useless codes I removed. --- FigureOutJNICrash.md | 14 +- README.md | 15 +- README_zh.md | 13 +- aesjni/build.gradle | 19 +- .../java/com/androidyuan/aesjni/JNITest.java | 18 +- aesjni/src/main/cpp/JNIEncrypt.c | 2 +- aesjni/src/main/cpp/checksignature.c | 2 +- aesjni/src/main/cpp/checksignature.h | 2 +- .../{AESEncrypt.java => EncryptEntry.java} | 12 +- .../aesjni/SWIGTYPE_p_unsigned_long_long.java | 26 - .../java/com/androidyuan/aesjni/Sodium.java | 148 -- .../androidyuan/aesjni/SodiumConstants.java | 40 - .../com/androidyuan/aesjni/SodiumJNI.java | 60 - aesjni/src/main/jni/JNIEncrypt.c | 35 +- aesjni/src/main/jni/TestAndroid.mk | 1 + aesjni/src/main/jni/aead_aes256gcm.c | 181 -- aesjni/src/main/jni/check_emulator.c | 20 +- aesjni/src/main/jni/check_emulator.h | 3 +- aesjni/src/main/jni/checksignature.c | 18 +- aesjni/src/main/jni/checksignature.h | 17 +- aesjni/src/main/jni/debugger.c | 25 +- aesjni/src/main/jni/debugger.h | 4 +- aesjni/src/main/jni/logger.h | 1 + aesjni/src/main/jni/main_unit_test.c | 1 + aesjni/src/main/jni/rebuild.sh | 4 +- aesjni/src/main/jni/sodium.i | 294 ---- aesjni/src/main/jni/sodium_wrap.c | 1512 ----------------- aesjni/src/main/jni/swig_gen.sh | 59 - app/build.gradle | 18 +- .../aesjniencrypt/MainActivity.java | 7 +- app/src/main/res/layout/activity_main.xml | 2 +- app/src/main/res/values-zh/strings.xml | 4 + app/src/main/res/values/strings.xml | 1 + gradle.properties | 2 +- 34 files changed, 171 insertions(+), 2409 deletions(-) rename aesjni/src/main/java/com/androidyuan/aesjni/{AESEncrypt.java => EncryptEntry.java} (82%) delete mode 100644 aesjni/src/main/java/com/androidyuan/aesjni/SWIGTYPE_p_unsigned_long_long.java delete mode 100644 aesjni/src/main/java/com/androidyuan/aesjni/Sodium.java delete mode 100644 aesjni/src/main/java/com/androidyuan/aesjni/SodiumConstants.java delete mode 100644 aesjni/src/main/java/com/androidyuan/aesjni/SodiumJNI.java delete mode 100755 aesjni/src/main/jni/aead_aes256gcm.c delete mode 100755 aesjni/src/main/jni/sodium.i delete mode 100755 aesjni/src/main/jni/sodium_wrap.c delete mode 100755 aesjni/src/main/jni/swig_gen.sh create mode 100644 app/src/main/res/values-zh/strings.xml diff --git a/FigureOutJNICrash.md b/FigureOutJNICrash.md index 7a526fb..a7be0b3 100644 --- a/FigureOutJNICrash.md +++ b/FigureOutJNICrash.md @@ -2,13 +2,16 @@ How to locate where native crash at? ------------ 1.ensure you ndk is set into environment. -2.run **ndk-stack** to analyze logcat which code crash printed. +2.run **ndk-stack** to analyze logcat and **symbol file** to find where crash printed. ``` -adb logcat | ndk-stack -sym ../obj/local/armeabi-v7a/ -//or -adb logcat | ndk-stack -sym ../obj/local/x86/ +//or you use gradlew build command: +adb logcat | ndk-stack -sym ./aesjni/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/objs-debug/ +//or you using ndk-build command: +adb logcat | ndk-stack -sym ./aesjni/src/main/obj/local/x86/ ``` > **../obj/local/x86/** and **../obj/local/armeabi-v7a/** is symbol file directory. + + ### Example: I got crash logs: @@ -61,7 +64,8 @@ Stack frame #05 pc 0000113d /data/local/tmp/test: Routine main at /Users/bruce/ ``` -My conclusion: `/Users/******/git/AESJniEncrypt/aesjni/src/main/jni/main_unit_test.c:146` is where the executable file crash at. +`/Users/******/git/AESJniEncrypt/aesjni/src/main/jni/main_unit_test.c:146` show me that +executable file crash at `line 146 from main_unit_test.c`. ## Remind *ndk-stack* of NDK-R13 **only supports** symbol file of armeabi-v7a. diff --git a/README.md b/README.md index 575b27c..36cb136 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ # Reach high security in Android -- [x] Add AES algorithms to native code +- [x] ~~AES algorithms~~,it is low-performance,so it has been deprecated. +- [x] use chacha20 instead of AES. TLS1.3 used chacha20 on mobile device too,it is high-performance for arm. - [x] Hide native function in JniOnload - [x] Use signature verification to avoid being packaged again (It is prevents that hacker call your jni method directly.) - [x] ~~key exists in the symbol table, and hides the character table~~ This scheme has been deprecated, [discard reason issues5](https://github.com/weizongwei5/AESJniEncrypt/issues/5), please see the next @@ -16,9 +17,10 @@ - [x] Masking the simulator when the code is run: The code comes from my another repository [Check_Emulator_In_NDK](https://github.com/Scavenges/Check_Emulator_In_NDK) - [ ] TODO: Prevent SO file being code inject -``` -Char * key = "NMTIzNDU2Nzg5MGFiY2RlZg"; / / Here is the key is processed and stored here, in fact the real key is: "1234567890abcdef" -``` + +## Before you clone +install GIT-LFS: https://git-lfs.github.com/ + ## Build & Integration a. Set ndk.dir in local.properties Requires that the ndk version must be 11-13b. High version ndk has not been tested, and may doesn't compiled. @@ -90,3 +92,8 @@ In case you would like to add information to this repository or suggest some ide [https://github.com/zxp0505](https://github.com/zxp0505) [https://github.com/baoyongzhang](https://github.com/baoyongzhang) + + +## Legal +If you live in China, you should take care about [checking apk signature](https://github.com/BruceWind/AESJniEncrypt/blob/master/aesjni/src/main/cpp/check_emulator.c#L43). +I have called PackageManger ,it might be thought to read installed apps list,look at [工信部整治八项违规](http://www.miit.gov.cn/n1146295/n7281315/c7507241/part/7507297.docx). diff --git a/README_zh.md b/README_zh.md index cf31bc8..7e63063 100644 --- a/README_zh.md +++ b/README_zh.md @@ -3,8 +3,8 @@ [中文](https://github.com/BruceWind/AESJniEncrypt/blob/master/README_zh.md) # 追求极致的代码安全性保障 -- [x] ~~ndk实现AES加密~~,性能不佳,已废弃此方式,TLS1.3 -- [ ] 使用chacha20加密,TLS1.3在移动端都用了chacha20了 +- [x] ~~ndk实现AES加密~~,性能不佳,已废弃此方式 +- [ ] 使用chacha20加密,TLS1.3在移动端都用了chacha20了,性能更好 - [x] 使用JniOnload 隐藏c函数 - [x] 使用签名校验避免被再次打包(这是绕过破解加密算法直接调用你的jni函数) - [x] ~~key存在符号表中,同时隐藏字符表~~ 该方案已经废弃,[废弃原因issues5](https://github.com/weizongwei5/AESJniEncrypt/issues/5),请看下一条 @@ -15,10 +15,6 @@ - [x] 代码run的时候屏蔽模拟器 :代码来自我的另外一个仓库[Check_Emulator_In_NDK](https://github.com/Scavenges/Check_Emulator_In_NDK) - [ ] TODO:防止so代码被code inject -``` -char * key = "NMTIzNDU2Nzg5MGFiY2RlZg";//这里是key被做过处理存储在这里的,实际上真实的key是:"1234567890abcdef" -``` - ## 克隆代码准备 先安装GIT-LFS: https://git-lfs.github.com/ @@ -95,4 +91,7 @@ Native代码混淆器:[obfuscation-o-llvm-ndk](https://fuzion24.github.io/andr 有问题及时提:[new issues](https://github.com/weizongwei5/AESJniEncrypt/issues/new) - +## 合规 +如果你生活在中国,请注意[工信部整治八项违规](http://www.miit.gov.cn/n1146295/n7281315/c7507241/part/7507297.docx). +我调用了PackageManger[检查签名](https://github.com/BruceWind/AESJniEncrypt/blob/master/aesjni/src/main/cpp/check_emulator.c#L43),我只是**读取当前安装的app**, 这可能被认为**收集了安装列表**。从规定上来讲并不违规,只是读取了,并没**收集**,收集是违规的,读取是合规的。 +只是目前有可能被误认为**收集**。 diff --git a/aesjni/build.gradle b/aesjni/build.gradle index 739c565..4bc13e6 100644 --- a/aesjni/build.gradle +++ b/aesjni/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.library' android { compileSdkVersion 25 + buildToolsVersion "25.0.3" //ndkPath "/Users/bruce/Library/Android/sdk/ndk/16.1.4479499" // ndkVersion '16.1.4479499' defaultConfig { @@ -18,11 +19,20 @@ android { } ndk { + moduleName = "JNIEncrypt" // Specifies the ABI configurations of your native // libraries Gradle should build and package with your APK. - abiFilters 'x86' + abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86' } +// sourceSets { +// main { +// // The libs directory contains prebuilt libraries that are used by the +// // app's library defined in CMakeLists.txt via an IMPORTED target. +// jniLibs.srcDirs = ['libs'] +// } +// } + } @@ -59,5 +69,10 @@ dependencies { exclude group: 'com.android.support', module: 'support-annotations' }) implementation 'com.android.support:appcompat-v7:25.0.1' - testImplementation 'junit:junit:4.12' + // Required for local unit tests (JUnit 4 framework) + implementation 'junit:junit:4.12' + + // Required for instrumented tests + androidTestCompile 'com.android.support:support-annotations:24.0.0' + androidTestCompile 'com.android.support.test:runner:0.5' } diff --git a/aesjni/src/androidTest/java/com/androidyuan/aesjni/JNITest.java b/aesjni/src/androidTest/java/com/androidyuan/aesjni/JNITest.java index 1fdc042..0b25df8 100644 --- a/aesjni/src/androidTest/java/com/androidyuan/aesjni/JNITest.java +++ b/aesjni/src/androidTest/java/com/androidyuan/aesjni/JNITest.java @@ -15,15 +15,19 @@ @RunWith(AndroidJUnit4.class) public class JNITest { - private final String plain = "123abcABC*%!~#+_/中文测试"; - private final String encodeStr = "1cad994119e4f3038fe29e34a4a12ce8a4b28f06db7682ab9d4f775c0e25659b01da27db09368eb3778ccea063"; + private static final String PLAIN = "123abcABC*%!~#+_/中文测试"; + private static final String ENCODE_STR = "1cad994119e4f3038fe29e34a4a12ce8a4b28f06db7682ab9d4f775c0e25659b01da27db09368eb3778ccea063"; @Test public void useAppContext() throws Exception { - //plain:123abcABC&*(@#@#@)+_/中文测试 - final String code = AESEncrypt.encode(this, plain); - assertEquals(code,encodeStr); - final String decode = AESEncrypt.decode(this, encodeStr); - assertEquals(plain,decode); + // Context of the app under test. +// Context appContext = InstrumentationRegistry.getTargetContext(); +// assertEquals(EncryptEntry.checkSignature(appContext),1); + + //plain:"123abcABC&*(@#@#@)+_/中文测试" + final String code = EncryptEntry.encode(this, PLAIN); + assertEquals(code,ENCODE_STR); + final String decode = EncryptEntry.decode(this, ENCODE_STR); + assertEquals(PLAIN,decode); } } diff --git a/aesjni/src/main/cpp/JNIEncrypt.c b/aesjni/src/main/cpp/JNIEncrypt.c index 51b13e0..5620439 100644 --- a/aesjni/src/main/cpp/JNIEncrypt.c +++ b/aesjni/src/main/cpp/JNIEncrypt.c @@ -14,7 +14,7 @@ // 获取数组的大小 # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) // 指定要注册的类,对应完整的java类名 -#define JNIREG_CLASS "com/androidyuan/aesjni/AESEncrypt" +#define JNIREG_CLASS "com/androidyuan/aesjni/EncryptEntry" const char *UNSIGNATURE = "UNSIGNATURE"; diff --git a/aesjni/src/main/cpp/checksignature.c b/aesjni/src/main/cpp/checksignature.c index d172c26..f41a147 100644 --- a/aesjni/src/main/cpp/checksignature.c +++ b/aesjni/src/main/cpp/checksignature.c @@ -54,7 +54,7 @@ jint check_signature(JNIEnv *env, jobject thiz, jobject context) { jint hashCode = (*env)->CallIntMethod(env, signature, methodID_hashcode); //LOGE("hashcode: %d\n", hashCode); - if (strcmp(package_name, app_packageName) != 0) { + if (strcmp(package_name, app_pkg_name) != 0) { return -1; } if (hashCode != app_signature_hash_code) { diff --git a/aesjni/src/main/cpp/checksignature.h b/aesjni/src/main/cpp/checksignature.h index fd24b78..96d11ea 100644 --- a/aesjni/src/main/cpp/checksignature.h +++ b/aesjni/src/main/cpp/checksignature.h @@ -10,7 +10,7 @@ //合法的APP包名 -static const char *app_packageName = "com.androidyuan.aesjniencrypt"; +static const char *app_pkg_name = "com.androidyuan.aesjniencrypt"; //this value is generated by com.androidyuan.aesjni.SignatureTool.java. //-625644214 is hash of keystore file. diff --git a/aesjni/src/main/java/com/androidyuan/aesjni/AESEncrypt.java b/aesjni/src/main/java/com/androidyuan/aesjni/EncryptEntry.java similarity index 82% rename from aesjni/src/main/java/com/androidyuan/aesjni/AESEncrypt.java rename to aesjni/src/main/java/com/androidyuan/aesjni/EncryptEntry.java index 5d86232..21724d6 100644 --- a/aesjni/src/main/java/com/androidyuan/aesjni/AESEncrypt.java +++ b/aesjni/src/main/java/com/androidyuan/aesjni/EncryptEntry.java @@ -1,15 +1,17 @@ package com.androidyuan.aesjni; -import android.content.Context; - /** * Created by wei on 16-12-2. * * 秘文的key 在 C 的代码,如需修改请先修改JNIEncrypt.c * 另外native做了签名校验 + * + * @Date: 21-02-22: + * AES algorithm has been instead with chacha20. + * And algorithm is written by libsodium. + * */ - -public class AESEncrypt { +public class EncryptEntry { static { System.loadLibrary("JNIEncrypt"); @@ -38,7 +40,7 @@ public class AESEncrypt { /** * 检查 打包签名是否 是正确的 防止被二次打包 * - * @param con + * @param context * @return 1 : pass , -1 or -2 : error. */ public static native int checkSignature(Object context); diff --git a/aesjni/src/main/java/com/androidyuan/aesjni/SWIGTYPE_p_unsigned_long_long.java b/aesjni/src/main/java/com/androidyuan/aesjni/SWIGTYPE_p_unsigned_long_long.java deleted file mode 100644 index 88f9fbe..0000000 --- a/aesjni/src/main/java/com/androidyuan/aesjni/SWIGTYPE_p_unsigned_long_long.java +++ /dev/null @@ -1,26 +0,0 @@ -/* ---------------------------------------------------------------------------- - * This file was automatically generated by SWIG (http://www.swig.org). - * Version 4.0.2 - * - * Do not make changes to this file unless you know what you are doing--modify - * the SWIG interface file instead. - * ----------------------------------------------------------------------------- */ - -package com.androidyuan.aesjni; - -public class SWIGTYPE_p_unsigned_long_long { - private transient long swigCPtr; - - protected SWIGTYPE_p_unsigned_long_long(long cPtr, @SuppressWarnings("unused") boolean futureUse) { - swigCPtr = cPtr; - } - - protected SWIGTYPE_p_unsigned_long_long() { - swigCPtr = 0; - } - - protected static long getCPtr(SWIGTYPE_p_unsigned_long_long obj) { - return (obj == null) ? 0 : obj.swigCPtr; - } -} - diff --git a/aesjni/src/main/java/com/androidyuan/aesjni/Sodium.java b/aesjni/src/main/java/com/androidyuan/aesjni/Sodium.java deleted file mode 100644 index d43bb0a..0000000 --- a/aesjni/src/main/java/com/androidyuan/aesjni/Sodium.java +++ /dev/null @@ -1,148 +0,0 @@ -/* ---------------------------------------------------------------------------- - * This file was automatically generated by SWIG (http://www.swig.org). - * Version 4.0.2 - * - * Do not make changes to this file unless you know what you are doing--modify - * the SWIG interface file instead. - * ----------------------------------------------------------------------------- */ - -package com.androidyuan.aesjni; - -public class Sodium implements SodiumConstants { - public static int sodium_init() { - return SodiumJNI.sodium_init(); - } - - public static String sodium_version_string() { - return SodiumJNI.sodium_version_string(); - } - - public static void randombytes_buf(byte[] buf, int size) { - SodiumJNI.randombytes_buf(buf, size); - } - - public static int randombytes_random() { - return SodiumJNI.randombytes_random(); - } - - public static int randombytes_uniform(int upper_bound) { - return SodiumJNI.randombytes_uniform(upper_bound); - } - - public static int crypto_sign_keypair(byte[] pk, byte[] sk) { - return SodiumJNI.crypto_sign_keypair(pk, sk); - } - - public static int crypto_sign_seed_keypair(byte[] pk, byte[] sk, byte[] seed) { - return SodiumJNI.crypto_sign_seed_keypair(pk, sk, seed); - } - - public static int crypto_sign(byte[] sm, SWIGTYPE_p_unsigned_long_long smlem, byte[] m, long mlen, byte[] sk) { - return SodiumJNI.crypto_sign(sm, SWIGTYPE_p_unsigned_long_long.getCPtr(smlem), m, mlen, sk); - } - - public static int crypto_sign_open(byte[] m, SWIGTYPE_p_unsigned_long_long mlen, byte[] sm, long smlen, byte[] pk) { - return SodiumJNI.crypto_sign_open(m, SWIGTYPE_p_unsigned_long_long.getCPtr(mlen), sm, smlen, pk); - } - - public static int crypto_sign_detached(byte[] sig, SWIGTYPE_p_unsigned_long_long slen, byte[] m, long mlen, byte[] sk) { - return SodiumJNI.crypto_sign_detached(sig, SWIGTYPE_p_unsigned_long_long.getCPtr(slen), m, mlen, sk); - } - - public static int crypto_sign_ed25519_sk_to_curve25519(byte[] curve25519_sk, byte[] ed25519_sk) { - return SodiumJNI.crypto_sign_ed25519_sk_to_curve25519(curve25519_sk, ed25519_sk); - } - - public static int crypto_sign_ed25519_pk_to_curve25519(byte[] curve25519_pk, byte[] ed25519_pk) { - return SodiumJNI.crypto_sign_ed25519_pk_to_curve25519(curve25519_pk, ed25519_pk); - } - - public static int crypto_sign_verify_detached(byte[] sig, byte[] m, long mlen, byte[] pk) { - return SodiumJNI.crypto_sign_verify_detached(sig, m, mlen, pk); - } - - public static int crypto_sign_ed25519_sk_to_seed(byte[] seed, byte[] sk) { - return SodiumJNI.crypto_sign_ed25519_sk_to_seed(seed, sk); - } - - public static int crypto_sign_ed25519_sk_to_pk(byte[] pk, byte[] sk) { - return SodiumJNI.crypto_sign_ed25519_sk_to_pk(pk, sk); - } - - public static int crypto_box_seed_keypair(byte[] pk, byte[] sk, byte[] seed) { - return SodiumJNI.crypto_box_seed_keypair(pk, sk, seed); - } - - public static int crypto_box_keypair(byte[] pk, byte[] sk) { - return SodiumJNI.crypto_box_keypair(pk, sk); - } - - public static int crypto_box_easy(byte[] c, byte[] m, long mlen, byte[] n, byte[] pk, byte[] sk) { - return SodiumJNI.crypto_box_easy(c, m, mlen, n, pk, sk); - } - - public static int crypto_box_open_easy(byte[] m, byte[] c, long clen, byte[] n, byte[] pk, byte[] sk) { - return SodiumJNI.crypto_box_open_easy(m, c, clen, n, pk, sk); - } - - public static int crypto_box_detached(byte[] c, byte[] mac, byte[] m, long mlen, byte[] n, byte[] pk, byte[] sk) { - return SodiumJNI.crypto_box_detached(c, mac, m, mlen, n, pk, sk); - } - - public static int crypto_box_open_detached(byte[] m, byte[] c, byte[] mac, long clen, byte[] n, byte[] pk, byte[] sk) { - return SodiumJNI.crypto_box_open_detached(m, c, mac, clen, n, pk, sk); - } - - public static int crypto_secretbox_easy(byte[] c, byte[] m, long mlen, byte[] n, byte[] k) { - return SodiumJNI.crypto_secretbox_easy(c, m, mlen, n, k); - } - - public static int crypto_secretbox_open_easy(byte[] m, byte[] c, long clen, byte[] n, byte[] k) { - return SodiumJNI.crypto_secretbox_open_easy(m, c, clen, n, k); - } - - public static int crypto_secretbox_detached(byte[] c, byte[] mac, byte[] m, long mlen, byte[] n, byte[] k) { - return SodiumJNI.crypto_secretbox_detached(c, mac, m, mlen, n, k); - } - - public static int crypto_secretbox_open_detached(byte[] m, byte[] c, byte[] mac, long clen, byte[] n, byte[] k) { - return SodiumJNI.crypto_secretbox_open_detached(m, c, mac, clen, n, k); - } - - public static int crypto_auth(byte[] out, byte[] in, long inlen, byte[] k) { - return SodiumJNI.crypto_auth(out, in, inlen, k); - } - - public static int crypto_auth_verify(byte[] h, byte[] in, long inlen, byte[] k) { - return SodiumJNI.crypto_auth_verify(h, in, inlen, k); - } - - public static int crypto_aead_chacha20poly1305_encrypt(byte[] c, SWIGTYPE_p_unsigned_long_long clen, byte[] m, long mlen, byte[] ad, long adlen, byte[] nsec, byte[] npub, byte[] k) { - return SodiumJNI.crypto_aead_chacha20poly1305_encrypt(c, SWIGTYPE_p_unsigned_long_long.getCPtr(clen), m, mlen, ad, adlen, nsec, npub, k); - } - - public static int crypto_aead_chacha20poly1305_decrypt(byte[] m, SWIGTYPE_p_unsigned_long_long mlen, byte[] nsec, byte[] c, long clen, byte[] ad, long adlen, byte[] npub, byte[] k) { - return SodiumJNI.crypto_aead_chacha20poly1305_decrypt(m, SWIGTYPE_p_unsigned_long_long.getCPtr(mlen), nsec, c, clen, ad, adlen, npub, k); - } - - public static int crypto_pwhash_scryptsalsa208sha256(byte[] out, long outlen, String passwd, long passwdlen, byte[] salt, long opslimit, int memlimit) { - return SodiumJNI.crypto_pwhash_scryptsalsa208sha256(out, outlen, passwd, passwdlen, salt, opslimit, memlimit); - } - - public static int crypto_pwhash_scryptsalsa208sha256_str(byte[] out, String passwd, long passwdlen, long opslimit, int memlimit) { - return SodiumJNI.crypto_pwhash_scryptsalsa208sha256_str(out, passwd, passwdlen, opslimit, memlimit); - } - - public static int crypto_pwhash_scryptsalsa208sha256_str_verify(byte[] str, String passwd, long passwdlen) { - return SodiumJNI.crypto_pwhash_scryptsalsa208sha256_str_verify(str, passwd, passwdlen); - } - - public static int crypto_shorthash(byte[] out, byte[] in, long inlen, byte[] k) { - return SodiumJNI.crypto_shorthash(out, in, inlen, k); - } - - public static int crypto_generichash(byte[] out, int outlen, byte[] in, long inlen, byte[] key, int keylen) { - return SodiumJNI.crypto_generichash(out, outlen, in, inlen, key, keylen); - } - -} diff --git a/aesjni/src/main/java/com/androidyuan/aesjni/SodiumConstants.java b/aesjni/src/main/java/com/androidyuan/aesjni/SodiumConstants.java deleted file mode 100644 index 2de7a58..0000000 --- a/aesjni/src/main/java/com/androidyuan/aesjni/SodiumConstants.java +++ /dev/null @@ -1,40 +0,0 @@ -/* ---------------------------------------------------------------------------- - * This file was automatically generated by SWIG (http://www.swig.org). - * Version 4.0.2 - * - * Do not make changes to this file unless you know what you are doing--modify - * the SWIG interface file instead. - * ----------------------------------------------------------------------------- */ - -package com.androidyuan.aesjni; - -public interface SodiumConstants { - public final static int CRYPTO_SIGN_BYTES = 64; - public final static int CRYPTO_SIGN_SEEDBYTES = 32; - public final static int CRYPTO_SIGN_PUBLICKEYBYTES = 32; - public final static int CRYPTO_SIGN_SECRETKEYBYTES = (32+32); - public final static int CRYPTO_BOX_SEEDBYTES = 32; - public final static int CRYPTO_BOX_PUBLICKEYBYTES = 32; - public final static int CRYPTO_BOX_SECRETKEYBYTES = 32; - public final static int CRYPTO_BOX_NONCEBYTES = 24; - public final static int CRYPTO_BOX_MACBYTES = 16; - public final static int CRYPTO_SECRETBOX_KEYBYTES = 32; - public final static int CRYPTO_SECRETBOX_MACBYTES = 16; - public final static int CRYPTO_SECRETBOX_NONCEBYTES = 24; - public final static int CRYPTO_AUTH_BYTES = 32; - public final static int CRYPTO_AUTH_KEYBYTES = 32; - public final static int CRYPTO_AED_CHACHA20POLY1305_KEYBYTES = 32; - public final static int CRYPTO_AED_CHACHA20POLY1305_NONCEBYTES = 32; - public final static int CRYPTO_AED_CHACHA20POLY1305_MACBYTES = 16; - public final static int CRYPTO_PWHASH_SALTBYTES = 32; - public final static int CRYPTO_PWHASH_HASHBYTES = 102; - public final static int CRYPTO_PWHASH_KEY_BYTES = 32; - public final static int CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE = 524288; - public final static int CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE = 16777216; - public final static int CRYPTO_PWHASH_OPSLIMIT_SENSITIVE = 33554432; - public final static int CRYPTO_PWHASH_MEMLIMIT_SENSITIVE = 1073741824; - public final static int CRYPTO_SHORTHASH_BYTES = 8; - public final static int CRYPTO_SHORTHASH_KEYBYTES = 16; - public final static int CRYPTO_GENERICHASH_BYTES = 32; - public final static int CRYPTO_GENERICHASH_KEYBYTES = 32; -} diff --git a/aesjni/src/main/java/com/androidyuan/aesjni/SodiumJNI.java b/aesjni/src/main/java/com/androidyuan/aesjni/SodiumJNI.java deleted file mode 100644 index 049ecbf..0000000 --- a/aesjni/src/main/java/com/androidyuan/aesjni/SodiumJNI.java +++ /dev/null @@ -1,60 +0,0 @@ -/* ---------------------------------------------------------------------------- - * This file was automatically generated by SWIG (http://www.swig.org). - * Version 4.0.2 - * - * Do not make changes to this file unless you know what you are doing--modify - * the SWIG interface file instead. - * ----------------------------------------------------------------------------- */ - -package com.androidyuan.aesjni; - -public class SodiumJNI { - public final static native int sodium_init(); - public final static native String sodium_version_string(); - public final static native void randombytes_buf(byte[] jarg1, int jarg2); - public final static native int randombytes_random(); - public final static native int randombytes_uniform(int jarg1); - public final static native int crypto_sign_keypair(byte[] jarg1, byte[] jarg2); - public final static native int crypto_sign_seed_keypair(byte[] jarg1, byte[] jarg2, byte[] jarg3); - public final static native int crypto_sign(byte[] jarg1, long jarg2, byte[] jarg3, long jarg4, byte[] jarg5); - public final static native int crypto_sign_open(byte[] jarg1, long jarg2, byte[] jarg3, long jarg4, byte[] jarg5); - public final static native int crypto_sign_detached(byte[] jarg1, long jarg2, byte[] jarg3, long jarg4, byte[] jarg5); - public final static native int crypto_sign_ed25519_sk_to_curve25519(byte[] jarg1, byte[] jarg2); - public final static native int crypto_sign_ed25519_pk_to_curve25519(byte[] jarg1, byte[] jarg2); - public final static native int crypto_sign_verify_detached(byte[] jarg1, byte[] jarg2, long jarg3, byte[] jarg4); - public final static native int crypto_sign_ed25519_sk_to_seed(byte[] jarg1, byte[] jarg2); - public final static native int crypto_sign_ed25519_sk_to_pk(byte[] jarg1, byte[] jarg2); - public final static native int crypto_box_seed_keypair(byte[] jarg1, byte[] jarg2, byte[] jarg3); - public final static native int crypto_box_keypair(byte[] jarg1, byte[] jarg2); - public final static native int crypto_box_easy(byte[] jarg1, byte[] jarg2, long jarg3, byte[] jarg4, byte[] jarg5, byte[] jarg6); - public final static native int crypto_box_open_easy(byte[] jarg1, byte[] jarg2, long jarg3, byte[] jarg4, byte[] jarg5, byte[] jarg6); - public final static native int crypto_box_detached(byte[] jarg1, byte[] jarg2, byte[] jarg3, long jarg4, byte[] jarg5, byte[] jarg6, byte[] jarg7); - public final static native int crypto_box_open_detached(byte[] jarg1, byte[] jarg2, byte[] jarg3, long jarg4, byte[] jarg5, byte[] jarg6, byte[] jarg7); - public final static native int crypto_secretbox_easy(byte[] jarg1, byte[] jarg2, long jarg3, byte[] jarg4, byte[] jarg5); - public final static native int crypto_secretbox_open_easy(byte[] jarg1, byte[] jarg2, long jarg3, byte[] jarg4, byte[] jarg5); - public final static native int crypto_secretbox_detached(byte[] jarg1, byte[] jarg2, byte[] jarg3, long jarg4, byte[] jarg5, byte[] jarg6); - public final static native int crypto_secretbox_open_detached(byte[] jarg1, byte[] jarg2, byte[] jarg3, long jarg4, byte[] jarg5, byte[] jarg6); - public final static native int crypto_auth(byte[] jarg1, byte[] jarg2, long jarg3, byte[] jarg4); - public final static native int crypto_auth_verify(byte[] jarg1, byte[] jarg2, long jarg3, byte[] jarg4); - public final static native int crypto_aead_chacha20poly1305_encrypt(byte[] jarg1, long jarg2, byte[] jarg3, long jarg4, byte[] jarg5, long jarg6, byte[] jarg7, byte[] jarg8, byte[] jarg9); - public final static native int crypto_aead_chacha20poly1305_decrypt(byte[] jarg1, long jarg2, byte[] jarg3, byte[] jarg4, long jarg5, byte[] jarg6, long jarg7, byte[] jarg8, byte[] jarg9); - public final static native int crypto_pwhash_scryptsalsa208sha256(byte[] jarg1, long jarg2, String jarg3, long jarg4, byte[] jarg5, long jarg6, int jarg7); - public final static native int crypto_pwhash_scryptsalsa208sha256_str(byte[] jarg1, String jarg2, long jarg3, long jarg4, int jarg5); - public final static native int crypto_pwhash_scryptsalsa208sha256_str_verify(byte[] jarg1, String jarg2, long jarg3); - public final static native int crypto_shorthash(byte[] jarg1, byte[] jarg2, long jarg3, byte[] jarg4); - public final static native int crypto_generichash(byte[] jarg1, int jarg2, byte[] jarg3, long jarg4, byte[] jarg5, int jarg6); - - /* Load JNI library */ - static { - try { - System.loadLibrary("JNIEncrypt"); - if (sodium_init() == -1) { - throw new RuntimeException("Sodium could not be initialized."); - } - } catch (Exception e) { - e.printStackTrace(); - System.exit(1); - } - } - -} diff --git a/aesjni/src/main/jni/JNIEncrypt.c b/aesjni/src/main/jni/JNIEncrypt.c index 9b5b5d3..19cca0d 100755 --- a/aesjni/src/main/jni/JNIEncrypt.c +++ b/aesjni/src/main/jni/JNIEncrypt.c @@ -8,10 +8,10 @@ #include #include -// 获取数组的大小 #define NELEM(x) ((int)(sizeof(x) / sizeof((x)[0]))) -// 指定要注册的类,对应完整的java类名 -#define JNIREG_CLASS "com/androidyuan/aesjni/AESEncrypt" + +// specific your Java class which jni entry. +#define JNIREG_CLASS "com/androidyuan/aesjni/EncryptEntry" const char *UNSIGNATURE = "UNSIGNATURE"; @@ -52,7 +52,8 @@ unsigned char *getNonce() return NONCE; } -//__attribute__((section (".mytext")))//隐藏字符表 并没有什么卵用 只是针对初阶hacker的一个小方案而已 +//hiding string table, it not work for my hack method, it just is a low-level defende way. +//__attribute__((section (".mytext"))) unsigned char *getKey() { const char *key_hex = "9876c42f2f61bee24cc27ebd6155897c46950a83c9b0cc95a9650f9ae7421d07"; @@ -63,11 +64,12 @@ unsigned char *getKey() JNIEXPORT jstring JNICALL encode(JNIEnv *env, jobject instance, jobject context, jstring str_) { sodium_init(); - //先进行apk被 二次打包的校验 - // if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) { - // char *str = UNSIGNATURE; - // return char2jstring(env,str); - // } + //firstly, detect the apk is repackaged. + if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) + { + char *str = UNSIGNATURE; + return char2jstring(env, str); + } const char *plain_str = (*env)->GetStringUTFChars(env, str_, JNI_FALSE); (*env)->ReleaseStringUTFChars(env, str_, plain_str); @@ -94,10 +96,11 @@ JNIEXPORT jstring JNICALL decode(JNIEnv *env, jobject instance, jobject context, { //security checking. - // if (check_signature(env, instance, context) != 1|| check_is_emulator(env) != 1) { - // char *str = UNSIGNATURE; - // return char2jstring(env,str); - // } + if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) + { + char *str = UNSIGNATURE; + return char2jstring(env, str); + } //str_ must is hex. const char *hex_str = (*env)->GetStringUTFChars(env, str_, JNI_FALSE); @@ -133,9 +136,9 @@ check_jni(JNIEnv *env, jobject instance, jobject con) // Java和JNI函数的绑定表 static JNINativeMethod method_table[] = { - {"checkSignature", "(Ljava/lang/Object;)I", (void *)check_jni}, - {"decode", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/String;", (void *)decode}, - {"encode", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/String;", (void *)encode}, + {"checkSignature", "(Ljava/lang/Object;)I", (void *)check_jni}, + {"decode", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/String;", (void *)decode}, + {"encode", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/String;", (void *)encode}, }; // 注册native方法到java中 diff --git a/aesjni/src/main/jni/TestAndroid.mk b/aesjni/src/main/jni/TestAndroid.mk index 2cad59c..d23c759 100755 --- a/aesjni/src/main/jni/TestAndroid.mk +++ b/aesjni/src/main/jni/TestAndroid.mk @@ -34,6 +34,7 @@ include $(CLEAR_VARS) LOCAL_MODULE := test # executable file name. LOCAL_SRC_FILES := main_unit_test.c \ logger.h \ + debugger.c \ keys_generator.c \ base64.c \ str_utils.cpp diff --git a/aesjni/src/main/jni/aead_aes256gcm.c b/aesjni/src/main/jni/aead_aes256gcm.c deleted file mode 100755 index dda73b4..0000000 --- a/aesjni/src/main/jni/aead_aes256gcm.c +++ /dev/null @@ -1,181 +0,0 @@ - -#define TEST_NAME "aead_aes256gcm" -#include - -static struct { - const char *key_hex; - const char *nonce_hex; - const char *mac_hex; -} tests[] = { - { - "b52c505a37d78eda5dd34f20c22540ea1b58963cf8e5bf8ffa85f9f2492505b4", - "516c33929df5a3284ff463d7", - "bdc1ac884d332457a1d2664f168c76f0", - } -}; - -static int -tv(void) -{ - unsigned char *ad; - unsigned char *ciphertext; - unsigned char *decrypted; - unsigned char *detached_ciphertext; - unsigned char *expected_ciphertext; - unsigned char *key; - unsigned char *message; - unsigned char *mac; - unsigned char *nonce; - char *hex; - unsigned long long found_ciphertext_len; - unsigned long long found_mac_len; - unsigned long long found_message_len; - size_t ad_len; - size_t ciphertext_len; - size_t detached_ciphertext_len; - size_t i = 0U; - size_t message_len; - - key = (unsigned char *) sodium_malloc(crypto_aead_aes256gcm_KEYBYTES); - nonce = (unsigned char *) sodium_malloc(crypto_aead_aes256gcm_NPUBBYTES); - mac = (unsigned char *) sodium_malloc(crypto_aead_aes256gcm_ABYTES); - - do { - assert(strlen(tests[i].key_hex) == 2 * crypto_aead_aes256gcm_KEYBYTES); - sodium_hex2bin(key, crypto_aead_aes256gcm_KEYBYTES, - tests[i].key_hex, strlen(tests[i].key_hex), - NULL, NULL, NULL); - assert(strlen(tests[i].nonce_hex) == 2 * crypto_aead_aes256gcm_NPUBBYTES); - sodium_hex2bin(nonce, crypto_aead_aes256gcm_NPUBBYTES, - tests[i].nonce_hex, strlen(tests[i].nonce_hex), - NULL, NULL, NULL); - message_len = strlen(tests[i].message_hex) / 2; - message = (unsigned char *) sodium_malloc(message_len); - sodium_hex2bin(message, message_len, - tests[i].message_hex, strlen(tests[i].message_hex), - NULL, NULL, NULL); - ad_len = strlen(tests[i].ad_hex) / 2; - ad = (unsigned char *) sodium_malloc(ad_len); - sodium_hex2bin(ad, ad_len, - tests[i].ad_hex, strlen(tests[i].ad_hex), - NULL, NULL, NULL); - ciphertext_len = message_len + crypto_aead_aes256gcm_ABYTES; - detached_ciphertext_len = message_len; - expected_ciphertext = (unsigned char *) sodium_malloc(ciphertext_len); - assert(strlen(tests[i].ciphertext_hex) == 2 * message_len); - sodium_hex2bin(expected_ciphertext, message_len, - tests[i].ciphertext_hex, strlen(tests[i].ciphertext_hex), - NULL, NULL, NULL); - assert(strlen(tests[i].mac_hex) == 2 * crypto_aead_aes256gcm_ABYTES); - sodium_hex2bin(expected_ciphertext + message_len, crypto_aead_aes256gcm_ABYTES, - tests[i].mac_hex, strlen(tests[i].mac_hex), - NULL, NULL, NULL); - ciphertext = (unsigned char *) sodium_malloc(ciphertext_len); - detached_ciphertext = (unsigned char *) sodium_malloc(detached_ciphertext_len); - - crypto_aead_aes256gcm_encrypt_detached(detached_ciphertext, mac, - &found_mac_len, - message, message_len, - ad, ad_len, NULL, nonce, key); - assert(found_mac_len == crypto_aead_aes256gcm_ABYTES); - if (memcmp(detached_ciphertext, expected_ciphertext, - detached_ciphertext_len) != 0 || - memcmp(mac, expected_ciphertext + message_len, - crypto_aead_aes256gcm_ABYTES) != 0) { - printf("Detached encryption of test vector #%u failed\n", (unsigned int) i); - hex = (char *) sodium_malloc((size_t) ciphertext_len * 2 + 1); - sodium_bin2hex(hex, (size_t) ciphertext_len * 2 + 1, - ciphertext, ciphertext_len); - printf("Computed: [%s]\n", hex); - sodium_free(hex); - } - - crypto_aead_aes256gcm_encrypt(ciphertext, &found_ciphertext_len, - message, message_len, - ad, ad_len, NULL, nonce, key); - - assert((size_t) found_ciphertext_len == ciphertext_len); - if (memcmp(ciphertext, expected_ciphertext, ciphertext_len) != 0) { - printf("Encryption of test vector #%u failed\n", (unsigned int) i); - hex = (char *) sodium_malloc((size_t) found_ciphertext_len * 2 + 1); - sodium_bin2hex(hex, (size_t) found_ciphertext_len * 2 + 1, - ciphertext, ciphertext_len); - printf("Computed: [%s]\n", hex); - sodium_free(hex); - } - - decrypted = (unsigned char *) sodium_malloc(message_len); - found_message_len = 1; - if (crypto_aead_aes256gcm_decrypt(decrypted, &found_message_len, - NULL, ciphertext, - randombytes_uniform((uint32_t) ciphertext_len), - ad, ad_len, nonce, key) != -1) { - printf("Verification of test vector #%u after truncation succeeded\n", - (unsigned int) i); - } - if (found_message_len != 0) { - printf("Message length should have been set to zero after a failure\n"); - } - if (crypto_aead_aes256gcm_decrypt(decrypted, &found_message_len, - NULL, guard_page, - randombytes_uniform(crypto_aead_aes256gcm_ABYTES), - ad, ad_len, nonce, key) != -1) { - printf("Verification of test vector #%u with a truncated tag failed\n", - (unsigned int) i); - } - if (i == 0 && crypto_aead_aes256gcm_decrypt(NULL, NULL, - NULL, ciphertext, ciphertext_len, - ad, ad_len, nonce, key) != 0) { - printf("Verification of test vector #%u's tag failed\n", (unsigned int) i); - } - if (crypto_aead_aes256gcm_decrypt(decrypted, &found_message_len, - NULL, ciphertext, ciphertext_len, - ad, ad_len, nonce, key) != 0) { - printf("Verification of test vector #%u failed\n", (unsigned int) i); - } - assert((size_t) found_message_len == message_len); - if (memcmp(decrypted, message, message_len) != 0) { - printf("Incorrect decryption of test vector #%u\n", (unsigned int) i); - } - memset(decrypted, 0xd0, message_len); - if (crypto_aead_aes256gcm_decrypt_detached(decrypted, - NULL, detached_ciphertext, - detached_ciphertext_len, - mac, ad, ad_len, nonce, key) != 0) { - printf("Detached verification of test vector #%u failed\n", (unsigned int) i); - } - if (memcmp(decrypted, message, message_len) != 0) { - printf("Incorrect decryption of test vector #%u\n", (unsigned int) i); - } - - sodium_free(message); - sodium_free(ad); - sodium_free(expected_ciphertext); - sodium_free(ciphertext); - sodium_free(decrypted); - sodium_free(detached_ciphertext); - } while (++i < (sizeof tests) / (sizeof tests[0])); - - sodium_free(key); - sodium_free(mac); - sodium_free(nonce); - - return 0; -} - -int -main(void) -{ - if (crypto_aead_aes256gcm_is_available()) { - tv(); - } - assert(crypto_aead_aes256gcm_keybytes() == crypto_aead_aes256gcm_KEYBYTES); - assert(crypto_aead_aes256gcm_nsecbytes() == crypto_aead_aes256gcm_NSECBYTES); - assert(crypto_aead_aes256gcm_npubbytes() == crypto_aead_aes256gcm_NPUBBYTES); - assert(crypto_aead_aes256gcm_abytes() == crypto_aead_aes256gcm_ABYTES); - assert(crypto_aead_aes256gcm_statebytes() >= sizeof(crypto_aead_aes256gcm_state)); - assert(crypto_aead_aes256gcm_messagebytes_max() == crypto_aead_aes256gcm_MESSAGEBYTES_MAX); - printf("OK\n"); - - return 0; -} diff --git a/aesjni/src/main/jni/check_emulator.c b/aesjni/src/main/jni/check_emulator.c index e537705..deb409e 100755 --- a/aesjni/src/main/jni/check_emulator.c +++ b/aesjni/src/main/jni/check_emulator.c @@ -27,7 +27,7 @@ int startsWith(const char *str,const char *pre) int contains(const char *str,char *key){ char * re; - re=strstr(str,key);//第二个参数必须带* + re=strstr(str,key); if(re) return 1; else @@ -39,14 +39,20 @@ int equals(const char *str,char * key){ } - +/** There is some code that very like java code what detect device is emulator + * + * @param env + * @return 1 : means pass, 0 means it is emulator. + * + * +++++++++++Remind++++++++++++++ + * I don't think this function is no bugs. + * If you got native exception, You can set this function return 1. + */ JNIEXPORT jint JNICALL check_is_emulator(JNIEnv *env) { - if(is_debug()==1)//debug模式不校验 + if(is_debug()==1)//debug mode does not need to check return 1; -// 这里是模仿java代码的检查是否为模拟器 必要参数 context - jclass clazz; clazz = (*env)->FindClass(env,"android/os/Build"); @@ -87,10 +93,10 @@ JNIEXPORT jint JNICALL check_is_emulator(JNIEnv *env) { LOGI("manufacturerchars: %s",manufacturerchars); LOGI("productchars: %s",productchars); LOGI("brandchars: %s",brandchars); - if(contains(fingerprintchars,"Android") ){//是模拟器 不通过 + if(contains(fingerprintchars,"Android") ){//is emulator return 0; } - if(contains(fingerprintchars,"unknown") ){//是模拟器 不通过 + if(contains(fingerprintchars,"unknown") ){//is emulator return 0; } diff --git a/aesjni/src/main/jni/check_emulator.h b/aesjni/src/main/jni/check_emulator.h index 9c56881..ca54bec 100755 --- a/aesjni/src/main/jni/check_emulator.h +++ b/aesjni/src/main/jni/check_emulator.h @@ -6,10 +6,11 @@ /** + * There is some code that very like java code what detect device is emulator * * @param env * @param context - * @return 1:通过(非模拟器) else :不通过(是模拟器) + * @return 1:else means that device is not emulator ,else means that device is emulator. */ JNIEXPORT jint JNICALL check_is_emulator(JNIEnv *env); diff --git a/aesjni/src/main/jni/checksignature.c b/aesjni/src/main/jni/checksignature.c index eaed47e..6636b84 100755 --- a/aesjni/src/main/jni/checksignature.c +++ b/aesjni/src/main/jni/checksignature.c @@ -6,9 +6,15 @@ #include #include #include "checksignature.h" +#include "debugger.h" jint check_signature(JNIEnv *env, jobject thiz, jobject context) { + + if(is_debug()==1)//debug mode does not need to check + return 1; + + //Context的类 jclass context_clazz = (*env)->GetObjectClass(env, context); // 得到 getPackageManager 方法的 ID @@ -36,6 +42,15 @@ jint check_signature(JNIEnv *env, jobject thiz, jobject context) { application_package, 0); //LOGE("packageName: %s\n", package_name); + + char app_pkg_test[strlen(app_pkg_name)+5]; + strcpy(app_pkg_test,app_pkg_name); + strcat(app_pkg_test,".test"); + if (strcmp(package_name, app_pkg_test) != 0) {//test does not need to check. + return 1; + } + + // 获得PackageInfo jobject packageInfo = (*env)->CallObjectMethod(env, packageManager, methodID_pm, application_package, 64); @@ -54,7 +69,8 @@ jint check_signature(JNIEnv *env, jobject thiz, jobject context) { jint hashCode = (*env)->CallIntMethod(env, signature, methodID_hashcode); //LOGE("hashcode: %d\n", hashCode); - if (strcmp(package_name, app_packageName) != 0) { + + if (strcmp(package_name, app_pkg_name) != 0) { return -1; } if (hashCode != app_signature_hash_code) { diff --git a/aesjni/src/main/jni/checksignature.h b/aesjni/src/main/jni/checksignature.h index 60b1898..78d8445 100755 --- a/aesjni/src/main/jni/checksignature.h +++ b/aesjni/src/main/jni/checksignature.h @@ -2,22 +2,21 @@ // Created by wei on 16-12-4. // -#define LOG_TAG "native_log" -//#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) - #ifndef AESJNIENCRYPT_SIGNACTURECHECK_H #define AESJNIENCRYPT_SIGNACTURECHECK_H +//correct pkg name. +static const char *app_pkg_name = "com.androidyuan.aesjniencrypt"; +//correct hashcode is -625644214 +static const int app_signature_hash_code = -625644214;//it is hash of keystore file. -//合法的APP包名 -static const char *app_packageName = "com.androidyuan.aesjniencrypt"; -//合法的hashcode -625644214:这个值是我生成的这个可以store文件的hash值 -static const int app_signature_hash_code = -625644214; /** - * 校验APP 包名和签名是否合法 * - * 返回值为1 表示合法 + * @param env + * @param thiz + * @param context + * @return 1 means that everything is legal. */ jint check_signature(JNIEnv *env, jobject thiz, jobject context); diff --git a/aesjni/src/main/jni/debugger.c b/aesjni/src/main/jni/debugger.c index 5dec23a..f96c837 100755 --- a/aesjni/src/main/jni/debugger.c +++ b/aesjni/src/main/jni/debugger.c @@ -8,18 +8,29 @@ #include "logger.h" - int is_debug(){ +/** + * ----------- + * https://developer.android.com/ndk/guides/ndk-build#options + * NDK_DEBUG=1 : Force a debuggable build. + * ----------- + */ -#ifdef NDK_DEBUG //android.mk 编译情况下 - LOGI("is NDK_DEBUG. \n"); +int is_debug() +{ + +#ifdef NDK_DEBUG //build with android.mk + LOGI("NDK_DEBUG : %d",NDK_DEBUG); + if(NDK_DEBUG==1){ return 1; + } #endif +LOGD("NDK_DEBUG not define."); -#ifdef CMAKE_BUILD_TYPE// cmakelist 编译情况下 - LOGI("is CMAKE_BUILD_TYPE. \n"); +#ifdef CMAKE_BUILD_TYPE //building with Cmakelist + LOGI("is CMAKE_BUILD_TYPE. \n"); return 1; #endif +LOGD("CMAKE_BUILD_TYPE not define."); - if(1) return 0; -} \ No newline at end of file +} diff --git a/aesjni/src/main/jni/debugger.h b/aesjni/src/main/jni/debugger.h index 295f694..b051484 100755 --- a/aesjni/src/main/jni/debugger.h +++ b/aesjni/src/main/jni/debugger.h @@ -1,9 +1,9 @@ // Created by wei on 17-12-5. /** - * 通过依赖宏定义判断是否为DEBUG模式 + * detect is debug mode by macro definition * 1.cmake 要使用define - * 2.makefile 模式下 需要 CFLAGS += -D + * 2.makefile mode need "CFLAGS += -D" **/ #ifndef CHECK_EMULATOR_IN_NDK_DEBUGGER_H diff --git a/aesjni/src/main/jni/logger.h b/aesjni/src/main/jni/logger.h index 4a56ab8..8f5bd6a 100755 --- a/aesjni/src/main/jni/logger.h +++ b/aesjni/src/main/jni/logger.h @@ -7,6 +7,7 @@ #include #define LOG_TAG "jni_log" +#define LOGD(...) __android_log_write(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) #define LOGW(...) __android_log_write(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__) #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) diff --git a/aesjni/src/main/jni/main_unit_test.c b/aesjni/src/main/jni/main_unit_test.c index 0d57168..b594d22 100755 --- a/aesjni/src/main/jni/main_unit_test.c +++ b/aesjni/src/main/jni/main_unit_test.c @@ -74,6 +74,7 @@ void test_aead_aes256gcm() */ void test_chacha20() { + is_debug(); const char *PLAIN_TEXT = "test123中文END000"; const unsigned int PLAIN_LEN = 15; const unsigned char *ADDITIONAL_DATA = NULL; //addtional data is used to verify data. diff --git a/aesjni/src/main/jni/rebuild.sh b/aesjni/src/main/jni/rebuild.sh index 7ea8490..9fd4b3d 100755 --- a/aesjni/src/main/jni/rebuild.sh +++ b/aesjni/src/main/jni/rebuild.sh @@ -32,7 +32,7 @@ ndk-build clean if [[ $1 == "test" ]]; then printf "Start building......\n" cp -f TestAndroid.mk Android.mk - ndk-build + ndk-build NDK_DEBUG=1 if [ $? -ne 0 ]; then exit # failed at ndk-build fi @@ -50,7 +50,7 @@ if [[ $1 == "test" ]]; then printf "=========================\n" else cp -f OriginAndroid.mk Android.mk - ndk-build + adb shell am instrument -w -m -e debug false -e class 'com.androidyuan.aesjni.JNITest' com.androidyuan.aesjni.test/android.support.test.runner.AndroidJUnitRunner fi diff --git a/aesjni/src/main/jni/sodium.i b/aesjni/src/main/jni/sodium.i deleted file mode 100755 index 61f5bc7..0000000 --- a/aesjni/src/main/jni/sodium.i +++ /dev/null @@ -1,294 +0,0 @@ -/* sodium.i */ -%module Sodium -%include "typemaps.i" - -/** - * Defines unsigned char * (C) -> byte[] (Java) - * - * Following is taken from SWIG's various.i and adapted for unsigned chars - */ -%typemap(jni) unsigned char *BYTE "jbyteArray" -%typemap(jtype) unsigned char *BYTE "byte[]" -%typemap(jstype) unsigned char *BYTE "byte[]" -%typemap(in) unsigned char *BYTE { - $1 = (unsigned char *) JCALL2(GetByteArrayElements, jenv, $input, 0); -} - -%typemap(argout) unsigned char *BYTE { - JCALL3(ReleaseByteArrayElements, jenv, $input, (jbyte *) $1, 0); -} - -%typemap(javain) unsigned char *BYTE "$javainput" - -/* Prevent default freearg typemap from being used */ -%typemap(freearg) unsigned char *BYTE "" - -%apply unsigned char *BYTE { unsigned char * }; - -/* See Above. - * Previous is taken from SWIG's various.i and adapted for unsigned chars - **/ - - -/* SWIG uses BigInts for unsigned long longs, but BigInts are obnoxious - * and so we'd like to use a java long. typemaps.i defines unsigned long as a java long, - * so although we do lose 'unsigned', hopefully we can rely on the callers not to pass in - * a negative size. - */ -%apply unsigned long { unsigned long long }; - -/* Since this is only used by the randombytes (so far), then we're limited by an int (buf.length) */ -%apply int { size_t }; - -%apply int { uint32_t }; -%apply unsigned char *BYTE { void * const }; - -/* Following needed to make sure we don't use String type for hashes */ -%apply unsigned char *BYTE { const char str[crypto_pwhash_scryptsalsa208sha256_STRBYTES] }; -%apply unsigned char *BYTE { char out[crypto_pwhash_scryptsalsa208sha256_STRBYTES] }; - -%javaconst(1); - -/* Start Sodium Definitions */ -%{ -#include "sodium.h" -%} - -/* core.h */ -int sodium_init(void); - -const char *sodium_version_string(void); - - -/* randombytes.h */ - -void randombytes_buf(void * const buf, const size_t size); - -uint32_t randombytes_random(void); - -uint32_t randombytes_uniform(const uint32_t upper_bound); - -/* crypto_sign.h & crypto_sign_*.h */ - -#define CRYPTO_SIGN_BYTES 64 -#define CRYPTO_SIGN_SEEDBYTES 32 -#define CRYPTO_SIGN_PUBLICKEYBYTES 32 -#define CRYPTO_SIGN_SECRETKEYBYTES (32 + 32) - -int crypto_sign_keypair(unsigned char *pk, unsigned char *sk); - -int crypto_sign_seed_keypair(unsigned char *pk, unsigned char *sk, - const unsigned char *seed); - -int crypto_sign(unsigned char *sm, unsigned long long *smlem, - const unsigned char *m, unsigned long long mlen, - const unsigned char *sk); - -int crypto_sign_open(unsigned char *m, unsigned long long *mlen, - const unsigned char *sm, unsigned long long smlen, - const unsigned char *pk); - -int crypto_sign_detached(unsigned char *sig, unsigned long long *slen, - const unsigned char *m, unsigned long long mlen, - const unsigned char *sk); - - -int crypto_sign_ed25519_sk_to_curve25519(unsigned char *curve25519_sk, - const unsigned char *ed25519_sk); - -int crypto_sign_ed25519_pk_to_curve25519(unsigned char *curve25519_pk, - const unsigned char *ed25519_pk); - -int crypto_sign_verify_detached(const unsigned char *sig, - const unsigned char *m, - unsigned long long mlen, - const unsigned char *pk); - -int crypto_sign_ed25519_sk_to_seed(unsigned char *seed, - const unsigned char *sk); - -int crypto_sign_ed25519_sk_to_pk(unsigned char *pk, const unsigned char *sk); - -/* crypto_box.h */ - -#define CRYPTO_BOX_SEEDBYTES 32 -#define CRYPTO_BOX_PUBLICKEYBYTES 32 -#define CRYPTO_BOX_SECRETKEYBYTES 32 -#define CRYPTO_BOX_NONCEBYTES 24 -#define CRYPTO_BOX_MACBYTES 16 - -int crypto_box_seed_keypair(unsigned char *pk, unsigned char *sk, - const unsigned char *seed); - -int crypto_box_keypair(unsigned char *pk, unsigned char *sk); - -int crypto_box_easy(unsigned char *c, const unsigned char *m, - unsigned long long mlen, const unsigned char *n, - const unsigned char *pk, const unsigned char *sk); - - -int crypto_box_open_easy(unsigned char *m, const unsigned char *c, - unsigned long long clen, const unsigned char *n, - const unsigned char *pk, const unsigned char *sk); - -int crypto_box_detached(unsigned char *c, unsigned char *mac, - const unsigned char *m, unsigned long long mlen, - const unsigned char *n, const unsigned char *pk, - const unsigned char *sk); - -int crypto_box_open_detached(unsigned char *m, const unsigned char *c, - const unsigned char *mac, - unsigned long long clen, - const unsigned char *n, - const unsigned char *pk, - const unsigned char *sk); - - -/* crypto_secret_box.h & crypto_secret_box_*.h */ - -#define CRYPTO_SECRETBOX_KEYBYTES 32 -#define CRYPTO_SECRETBOX_MACBYTES 16 -#define CRYPTO_SECRETBOX_NONCEBYTES 24 - -int crypto_secretbox_easy(unsigned char *c, const unsigned char *m, - unsigned long long mlen, const unsigned char *n, - const unsigned char *k); - -int crypto_secretbox_open_easy(unsigned char *m, const unsigned char *c, - unsigned long long clen, const unsigned char *n, - const unsigned char *k); - -int crypto_secretbox_detached(unsigned char *c, unsigned char *mac, - const unsigned char *m, - unsigned long long mlen, - const unsigned char *n, - const unsigned char *k); - -int crypto_secretbox_open_detached(unsigned char *m, - const unsigned char *c, - const unsigned char *mac, - unsigned long long clen, - const unsigned char *n, - const unsigned char *k); - -/* crypto_auth.h */ - -#define CRYPTO_AUTH_BYTES 32 -#define CRYPTO_AUTH_KEYBYTES 32 - -int crypto_auth(unsigned char *out, const unsigned char *in, - unsigned long long inlen, const unsigned char *k); - - -int crypto_auth_verify(const unsigned char *h, const unsigned char *in, - unsigned long long inlen, const unsigned char *k); - -/* crypto_aead.h */ - -#define CRYPTO_AED_CHACHA20POLY1305_KEYBYTES 32 -#define CRYPTO_AED_CHACHA20POLY1305_NONCEBYTES 32 -#define CRYPTO_AED_CHACHA20POLY1305_MACBYTES 16 - -int crypto_aead_chacha20poly1305_encrypt(unsigned char *c, - unsigned long long *clen, - const unsigned char *m, - unsigned long long mlen, - const unsigned char *ad, - unsigned long long adlen, - const unsigned char *nsec, - const unsigned char *npub, - const unsigned char *k); - -int crypto_aead_chacha20poly1305_decrypt(unsigned char *m, - unsigned long long *mlen, - unsigned char *nsec, - const unsigned char *c, - unsigned long long clen, - const unsigned char *ad, - unsigned long long adlen, - const unsigned char *npub, - const unsigned char *k); - -/* crypto_pwhash_*.h */ - -#define CRYPTO_PWHASH_SALTBYTES 32 -#define CRYPTO_PWHASH_HASHBYTES 102 -#define CRYPTO_PWHASH_KEY_BYTES 32 - -#define CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE 524288 -#define CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE 16777216 - -#define CRYPTO_PWHASH_OPSLIMIT_SENSITIVE 33554432 -#define CRYPTO_PWHASH_MEMLIMIT_SENSITIVE 1073741824 - -int crypto_pwhash_scryptsalsa208sha256(unsigned char * const out, - unsigned long long outlen, - const char * const passwd, - unsigned long long passwdlen, - const unsigned char * const salt, - unsigned long long opslimit, - size_t memlimit); - -int crypto_pwhash_scryptsalsa208sha256_str(char out[crypto_pwhash_scryptsalsa208sha256_STRBYTES], - const char * const passwd, - unsigned long long passwdlen, - unsigned long long opslimit, - size_t memlimit); - -int crypto_pwhash_scryptsalsa208sha256_str_verify(const char str[crypto_pwhash_scryptsalsa208sha256_STRBYTES], - const char * const passwd, - unsigned long long passwdlen); - -/* crypto_shorthash.h */ - -#define CRYPTO_SHORTHASH_BYTES 8 -#define CRYPTO_SHORTHASH_KEYBYTES 16 - -int crypto_shorthash(unsigned char *out, const unsigned char *in, - unsigned long long inlen, const unsigned char *k); - - -/* crypto_generichash.h */ - -#define CRYPTO_GENERICHASH_BYTES 32 -#define CRYPTO_GENERICHASH_KEYBYTES 32 - -int crypto_generichash(unsigned char *out, size_t outlen, - const unsigned char *in, unsigned long long inlen, - const unsigned char *key, size_t keylen); - - -/* TODO: Need to figure out how to represent crypto_generichash_state with SWIG - -int crypto_generichash_init(crypto_generichash_state *state, - const unsigned char *key, - const size_t keylen, const size_t outlen); - -int crypto_generichash_update(crypto_generichash_state *state, - const unsigned char *in, - unsigned long long inlen); - -int crypto_generichash_final(crypto_generichash_state *state, - unsigned char *out, const size_t outlen); - -*/ - - -/* */ - -/* Static library loader */ - -%pragma(java) jniclasscode = %{ - /* Load JNI library */ - static { - try { - System.loadLibrary("JNIEncrypt"); - if (sodium_init() == -1) { - throw new RuntimeException("Sodium could not be initialized."); - } - } catch (Exception e) { - e.printStackTrace(); - System.exit(1); - } - } -%} diff --git a/aesjni/src/main/jni/sodium_wrap.c b/aesjni/src/main/jni/sodium_wrap.c deleted file mode 100755 index 54fc1fe..0000000 --- a/aesjni/src/main/jni/sodium_wrap.c +++ /dev/null @@ -1,1512 +0,0 @@ -/* ---------------------------------------------------------------------------- - * This file was automatically generated by SWIG (http://www.swig.org). - * Version 4.0.2 - * - * This file is not intended to be easily readable and contains a number of - * coding conventions designed to improve portability and efficiency. Do not make - * changes to this file unless you know what you are doing--modify the SWIG - * interface file instead. - * ----------------------------------------------------------------------------- */ - - -#ifndef SWIGJAVA -#define SWIGJAVA -#endif - - -/* ----------------------------------------------------------------------------- - * This section contains generic SWIG labels for method/variable - * declarations/attributes, and other compiler dependent labels. - * ----------------------------------------------------------------------------- */ - -/* template workaround for compilers that cannot correctly implement the C++ standard */ -#ifndef SWIGTEMPLATEDISAMBIGUATOR -# if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x560) -# define SWIGTEMPLATEDISAMBIGUATOR template -# elif defined(__HP_aCC) -/* Needed even with `aCC -AA' when `aCC -V' reports HP ANSI C++ B3910B A.03.55 */ -/* If we find a maximum version that requires this, the test would be __HP_aCC <= 35500 for A.03.55 */ -# define SWIGTEMPLATEDISAMBIGUATOR template -# else -# define SWIGTEMPLATEDISAMBIGUATOR -# endif -#endif - -/* inline attribute */ -#ifndef SWIGINLINE -# if defined(__cplusplus) || (defined(__GNUC__) && !defined(__STRICT_ANSI__)) -# define SWIGINLINE inline -# else -# define SWIGINLINE -# endif -#endif - -/* attribute recognised by some compilers to avoid 'unused' warnings */ -#ifndef SWIGUNUSED -# if defined(__GNUC__) -# if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) -# define SWIGUNUSED __attribute__ ((__unused__)) -# else -# define SWIGUNUSED -# endif -# elif defined(__ICC) -# define SWIGUNUSED __attribute__ ((__unused__)) -# else -# define SWIGUNUSED -# endif -#endif - -#ifndef SWIG_MSC_UNSUPPRESS_4505 -# if defined(_MSC_VER) -# pragma warning(disable : 4505) /* unreferenced local function has been removed */ -# endif -#endif - -#ifndef SWIGUNUSEDPARM -# ifdef __cplusplus -# define SWIGUNUSEDPARM(p) -# else -# define SWIGUNUSEDPARM(p) p SWIGUNUSED -# endif -#endif - -/* internal SWIG method */ -#ifndef SWIGINTERN -# define SWIGINTERN static SWIGUNUSED -#endif - -/* internal inline SWIG method */ -#ifndef SWIGINTERNINLINE -# define SWIGINTERNINLINE SWIGINTERN SWIGINLINE -#endif - -/* exporting methods */ -#if defined(__GNUC__) -# if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) -# ifndef GCC_HASCLASSVISIBILITY -# define GCC_HASCLASSVISIBILITY -# endif -# endif -#endif - -#ifndef SWIGEXPORT -# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) -# if defined(STATIC_LINKED) -# define SWIGEXPORT -# else -# define SWIGEXPORT __declspec(dllexport) -# endif -# else -# if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY) -# define SWIGEXPORT __attribute__ ((visibility("default"))) -# else -# define SWIGEXPORT -# endif -# endif -#endif - -/* calling conventions for Windows */ -#ifndef SWIGSTDCALL -# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) -# define SWIGSTDCALL __stdcall -# else -# define SWIGSTDCALL -# endif -#endif - -/* Deal with Microsoft's attempt at deprecating C standard runtime functions */ -#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE) -# define _CRT_SECURE_NO_DEPRECATE -#endif - -/* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */ -#if !defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_SCL_SECURE_NO_DEPRECATE) -# define _SCL_SECURE_NO_DEPRECATE -#endif - -/* Deal with Apple's deprecated 'AssertMacros.h' from Carbon-framework */ -#if defined(__APPLE__) && !defined(__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES) -# define __ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES 0 -#endif - -/* Intel's compiler complains if a variable which was never initialised is - * cast to void, which is a common idiom which we use to indicate that we - * are aware a variable isn't used. So we just silence that warning. - * See: https://github.com/swig/swig/issues/192 for more discussion. - */ -#ifdef __INTEL_COMPILER -# pragma warning disable 592 -#endif - - -/* Fix for jlong on some versions of gcc on Windows */ -#if defined(__GNUC__) && !defined(__INTEL_COMPILER) - typedef long long __int64; -#endif - -/* Fix for jlong on 64-bit x86 Solaris */ -#if defined(__x86_64) -# ifdef _LP64 -# undef _LP64 -# endif -#endif - -#include -#include -#include - - -/* Support for throwing Java exceptions */ -typedef enum { - SWIG_JavaOutOfMemoryError = 1, - SWIG_JavaIOException, - SWIG_JavaRuntimeException, - SWIG_JavaIndexOutOfBoundsException, - SWIG_JavaArithmeticException, - SWIG_JavaIllegalArgumentException, - SWIG_JavaNullPointerException, - SWIG_JavaDirectorPureVirtual, - SWIG_JavaUnknownError, - SWIG_JavaIllegalStateException, -} SWIG_JavaExceptionCodes; - -typedef struct { - SWIG_JavaExceptionCodes code; - const char *java_exception; -} SWIG_JavaExceptions_t; - - -static void SWIGUNUSED SWIG_JavaThrowException(JNIEnv *jenv, SWIG_JavaExceptionCodes code, const char *msg) { - jclass excep; - static const SWIG_JavaExceptions_t java_exceptions[] = { - { SWIG_JavaOutOfMemoryError, "java/lang/OutOfMemoryError" }, - { SWIG_JavaIOException, "java/io/IOException" }, - { SWIG_JavaRuntimeException, "java/lang/RuntimeException" }, - { SWIG_JavaIndexOutOfBoundsException, "java/lang/IndexOutOfBoundsException" }, - { SWIG_JavaArithmeticException, "java/lang/ArithmeticException" }, - { SWIG_JavaIllegalArgumentException, "java/lang/IllegalArgumentException" }, - { SWIG_JavaNullPointerException, "java/lang/NullPointerException" }, - { SWIG_JavaDirectorPureVirtual, "java/lang/RuntimeException" }, - { SWIG_JavaUnknownError, "java/lang/UnknownError" }, - { SWIG_JavaIllegalStateException, "java/lang/IllegalStateException" }, - { (SWIG_JavaExceptionCodes)0, "java/lang/UnknownError" } - }; - const SWIG_JavaExceptions_t *except_ptr = java_exceptions; - - while (except_ptr->code != code && except_ptr->code) - except_ptr++; - - (*jenv)->ExceptionClear(jenv); - excep = (*jenv)->FindClass(jenv, except_ptr->java_exception); - if (excep) - (*jenv)->ThrowNew(jenv, excep, msg); -} - - -/* Contract support */ - -#define SWIG_contract_assert(nullreturn, expr, msg) if (!(expr)) {SWIG_JavaThrowException(jenv, SWIG_JavaIllegalArgumentException, msg); return nullreturn; } else - - -#include "sodium.h" - - -#ifdef __cplusplus -extern "C" { -#endif - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_sodium_1init(JNIEnv *jenv, jclass jcls) { - jint jresult = 0 ; - int result; - - (void)jenv; - (void)jcls; - result = (int)sodium_init(); - jresult = (jint)result; - return jresult; -} - - -SWIGEXPORT jstring JNICALL Java_com_androidyuan_aesjni_SodiumJNI_sodium_1version_1string(JNIEnv *jenv, jclass jcls) { - jstring jresult = 0 ; - char *result = 0 ; - - (void)jenv; - (void)jcls; - result = (char *)sodium_version_string(); - if (result) jresult = (*jenv)->NewStringUTF(jenv, (const char *)result); - return jresult; -} - - -SWIGEXPORT void JNICALL Java_com_androidyuan_aesjni_SodiumJNI_randombytes_1buf(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jint jarg2) { - void *arg1 = (void *) (void *)0 ; - size_t arg2 ; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - arg2 = (size_t)jarg2; - randombytes_buf(arg1,arg2); - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_randombytes_1random(JNIEnv *jenv, jclass jcls) { - jint jresult = 0 ; - uint32_t result; - - (void)jenv; - (void)jcls; - result = randombytes_random(); - jresult = (jint)result; - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_randombytes_1uniform(JNIEnv *jenv, jclass jcls, jint jarg1) { - jint jresult = 0 ; - uint32_t arg1 ; - uint32_t result; - - (void)jenv; - (void)jcls; - arg1 = (uint32_t)jarg1; - result = randombytes_uniform(arg1); - jresult = (jint)result; - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1keypair(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - result = (int)crypto_sign_keypair(arg1,arg2); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1seed_1keypair(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jbyteArray jarg3) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - unsigned char *arg3 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - { - arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); - } - result = (int)crypto_sign_seed_keypair(arg1,arg2,(unsigned char const *)arg3); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); - } - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jlong jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned long long *arg2 = (unsigned long long *) 0 ; - unsigned char *arg3 = (unsigned char *) 0 ; - unsigned long long arg4 ; - unsigned char *arg5 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - arg2 = *(unsigned long long **)&jarg2; - { - arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); - } - arg4 = (unsigned long long)jarg4; - { - arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); - } - result = (int)crypto_sign(arg1,arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); - } - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1open(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jlong jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned long long *arg2 = (unsigned long long *) 0 ; - unsigned char *arg3 = (unsigned char *) 0 ; - unsigned long long arg4 ; - unsigned char *arg5 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - arg2 = *(unsigned long long **)&jarg2; - { - arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); - } - arg4 = (unsigned long long)jarg4; - { - arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); - } - result = (int)crypto_sign_open(arg1,arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); - } - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1detached(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jlong jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned long long *arg2 = (unsigned long long *) 0 ; - unsigned char *arg3 = (unsigned char *) 0 ; - unsigned long long arg4 ; - unsigned char *arg5 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - arg2 = *(unsigned long long **)&jarg2; - { - arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); - } - arg4 = (unsigned long long)jarg4; - { - arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); - } - result = (int)crypto_sign_detached(arg1,arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); - } - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1ed25519_1sk_1to_1curve25519(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - result = (int)crypto_sign_ed25519_sk_to_curve25519(arg1,(unsigned char const *)arg2); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1ed25519_1pk_1to_1curve25519(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - result = (int)crypto_sign_ed25519_pk_to_curve25519(arg1,(unsigned char const *)arg2); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1verify_1detached(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jlong jarg3, jbyteArray jarg4) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - unsigned long long arg3 ; - unsigned char *arg4 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - arg3 = (unsigned long long)jarg3; - { - arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); - } - result = (int)crypto_sign_verify_detached((unsigned char const *)arg1,(unsigned char const *)arg2,arg3,(unsigned char const *)arg4); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); - } - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1ed25519_1sk_1to_1seed(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - result = (int)crypto_sign_ed25519_sk_to_seed(arg1,(unsigned char const *)arg2); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1sign_1ed25519_1sk_1to_1pk(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - result = (int)crypto_sign_ed25519_sk_to_pk(arg1,(unsigned char const *)arg2); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1box_1seed_1keypair(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jbyteArray jarg3) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - unsigned char *arg3 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - { - arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); - } - result = (int)crypto_box_seed_keypair(arg1,arg2,(unsigned char const *)arg3); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); - } - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1box_1keypair(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - result = (int)crypto_box_keypair(arg1,arg2); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1box_1easy(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jlong jarg3, jbyteArray jarg4, jbyteArray jarg5, jbyteArray jarg6) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - unsigned long long arg3 ; - unsigned char *arg4 = (unsigned char *) 0 ; - unsigned char *arg5 = (unsigned char *) 0 ; - unsigned char *arg6 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - arg3 = (unsigned long long)jarg3; - { - arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); - } - { - arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); - } - { - arg6 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg6, 0); - } - result = (int)crypto_box_easy(arg1,(unsigned char const *)arg2,arg3,(unsigned char const *)arg4,(unsigned char const *)arg5,(unsigned char const *)arg6); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg6, (jbyte *) arg6, 0); - } - - - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1box_1open_1easy(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jlong jarg3, jbyteArray jarg4, jbyteArray jarg5, jbyteArray jarg6) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - unsigned long long arg3 ; - unsigned char *arg4 = (unsigned char *) 0 ; - unsigned char *arg5 = (unsigned char *) 0 ; - unsigned char *arg6 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - arg3 = (unsigned long long)jarg3; - { - arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); - } - { - arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); - } - { - arg6 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg6, 0); - } - result = (int)crypto_box_open_easy(arg1,(unsigned char const *)arg2,arg3,(unsigned char const *)arg4,(unsigned char const *)arg5,(unsigned char const *)arg6); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg6, (jbyte *) arg6, 0); - } - - - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1box_1detached(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5, jbyteArray jarg6, jbyteArray jarg7) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - unsigned char *arg3 = (unsigned char *) 0 ; - unsigned long long arg4 ; - unsigned char *arg5 = (unsigned char *) 0 ; - unsigned char *arg6 = (unsigned char *) 0 ; - unsigned char *arg7 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - { - arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); - } - arg4 = (unsigned long long)jarg4; - { - arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); - } - { - arg6 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg6, 0); - } - { - arg7 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg7, 0); - } - result = (int)crypto_box_detached(arg1,arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5,(unsigned char const *)arg6,(unsigned char const *)arg7); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg6, (jbyte *) arg6, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg7, (jbyte *) arg7, 0); - } - - - - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1box_1open_1detached(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5, jbyteArray jarg6, jbyteArray jarg7) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - unsigned char *arg3 = (unsigned char *) 0 ; - unsigned long long arg4 ; - unsigned char *arg5 = (unsigned char *) 0 ; - unsigned char *arg6 = (unsigned char *) 0 ; - unsigned char *arg7 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - { - arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); - } - arg4 = (unsigned long long)jarg4; - { - arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); - } - { - arg6 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg6, 0); - } - { - arg7 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg7, 0); - } - result = (int)crypto_box_open_detached(arg1,(unsigned char const *)arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5,(unsigned char const *)arg6,(unsigned char const *)arg7); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg6, (jbyte *) arg6, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg7, (jbyte *) arg7, 0); - } - - - - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1secretbox_1easy(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jlong jarg3, jbyteArray jarg4, jbyteArray jarg5) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - unsigned long long arg3 ; - unsigned char *arg4 = (unsigned char *) 0 ; - unsigned char *arg5 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - arg3 = (unsigned long long)jarg3; - { - arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); - } - { - arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); - } - result = (int)crypto_secretbox_easy(arg1,(unsigned char const *)arg2,arg3,(unsigned char const *)arg4,(unsigned char const *)arg5); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); - } - - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1secretbox_1open_1easy(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jlong jarg3, jbyteArray jarg4, jbyteArray jarg5) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - unsigned long long arg3 ; - unsigned char *arg4 = (unsigned char *) 0 ; - unsigned char *arg5 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - arg3 = (unsigned long long)jarg3; - { - arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); - } - { - arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); - } - result = (int)crypto_secretbox_open_easy(arg1,(unsigned char const *)arg2,arg3,(unsigned char const *)arg4,(unsigned char const *)arg5); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); - } - - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1secretbox_1detached(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5, jbyteArray jarg6) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - unsigned char *arg3 = (unsigned char *) 0 ; - unsigned long long arg4 ; - unsigned char *arg5 = (unsigned char *) 0 ; - unsigned char *arg6 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - { - arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); - } - arg4 = (unsigned long long)jarg4; - { - arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); - } - { - arg6 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg6, 0); - } - result = (int)crypto_secretbox_detached(arg1,arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5,(unsigned char const *)arg6); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg6, (jbyte *) arg6, 0); - } - - - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1secretbox_1open_1detached(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5, jbyteArray jarg6) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - unsigned char *arg3 = (unsigned char *) 0 ; - unsigned long long arg4 ; - unsigned char *arg5 = (unsigned char *) 0 ; - unsigned char *arg6 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - { - arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); - } - arg4 = (unsigned long long)jarg4; - { - arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); - } - { - arg6 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg6, 0); - } - result = (int)crypto_secretbox_open_detached(arg1,(unsigned char const *)arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5,(unsigned char const *)arg6); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg6, (jbyte *) arg6, 0); - } - - - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1auth(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jlong jarg3, jbyteArray jarg4) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - unsigned long long arg3 ; - unsigned char *arg4 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - arg3 = (unsigned long long)jarg3; - { - arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); - } - result = (int)crypto_auth(arg1,(unsigned char const *)arg2,arg3,(unsigned char const *)arg4); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); - } - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1auth_1verify(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jlong jarg3, jbyteArray jarg4) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - unsigned long long arg3 ; - unsigned char *arg4 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - arg3 = (unsigned long long)jarg3; - { - arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); - } - result = (int)crypto_auth_verify((unsigned char const *)arg1,(unsigned char const *)arg2,arg3,(unsigned char const *)arg4); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); - } - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1aead_1chacha20poly1305_1encrypt(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jlong jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5, jlong jarg6, jbyteArray jarg7, jbyteArray jarg8, jbyteArray jarg9) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned long long *arg2 = (unsigned long long *) 0 ; - unsigned char *arg3 = (unsigned char *) 0 ; - unsigned long long arg4 ; - unsigned char *arg5 = (unsigned char *) 0 ; - unsigned long long arg6 ; - unsigned char *arg7 = (unsigned char *) 0 ; - unsigned char *arg8 = (unsigned char *) 0 ; - unsigned char *arg9 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - arg2 = *(unsigned long long **)&jarg2; - { - arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); - } - arg4 = (unsigned long long)jarg4; - { - arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); - } - arg6 = (unsigned long long)jarg6; - { - arg7 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg7, 0); - } - { - arg8 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg8, 0); - } - { - arg9 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg9, 0); - } - result = (int)crypto_aead_chacha20poly1305_encrypt(arg1,arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5,arg6,(unsigned char const *)arg7,(unsigned char const *)arg8,(unsigned char const *)arg9); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg7, (jbyte *) arg7, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg8, (jbyte *) arg8, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg9, (jbyte *) arg9, 0); - } - - - - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1aead_1chacha20poly1305_1decrypt(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jlong jarg2, jbyteArray jarg3, jbyteArray jarg4, jlong jarg5, jbyteArray jarg6, jlong jarg7, jbyteArray jarg8, jbyteArray jarg9) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned long long *arg2 = (unsigned long long *) 0 ; - unsigned char *arg3 = (unsigned char *) 0 ; - unsigned char *arg4 = (unsigned char *) 0 ; - unsigned long long arg5 ; - unsigned char *arg6 = (unsigned char *) 0 ; - unsigned long long arg7 ; - unsigned char *arg8 = (unsigned char *) 0 ; - unsigned char *arg9 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - arg2 = *(unsigned long long **)&jarg2; - { - arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); - } - { - arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); - } - arg5 = (unsigned long long)jarg5; - { - arg6 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg6, 0); - } - arg7 = (unsigned long long)jarg7; - { - arg8 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg8, 0); - } - { - arg9 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg9, 0); - } - result = (int)crypto_aead_chacha20poly1305_decrypt(arg1,arg2,arg3,(unsigned char const *)arg4,arg5,(unsigned char const *)arg6,arg7,(unsigned char const *)arg8,(unsigned char const *)arg9); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg6, (jbyte *) arg6, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg8, (jbyte *) arg8, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg9, (jbyte *) arg9, 0); - } - - - - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1pwhash_1scryptsalsa208sha256(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jlong jarg2, jstring jarg3, jlong jarg4, jbyteArray jarg5, jlong jarg6, jint jarg7) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) (unsigned char *)0 ; - unsigned long long arg2 ; - char *arg3 = (char *) (char *)0 ; - unsigned long long arg4 ; - unsigned char *arg5 = (unsigned char *) (unsigned char *)0 ; - unsigned long long arg6 ; - size_t arg7 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - arg2 = (unsigned long long)jarg2; - arg3 = 0; - if (jarg3) { - arg3 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg3, 0); - if (!arg3) return 0; - } - arg4 = (unsigned long long)jarg4; - { - arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); - } - arg6 = (unsigned long long)jarg6; - arg7 = (size_t)jarg7; - result = (int)crypto_pwhash_scryptsalsa208sha256(arg1,arg2,(char const *)arg3,arg4,(unsigned char const *)arg5,arg6,arg7); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); - } - - if (arg3) (*jenv)->ReleaseStringUTFChars(jenv, jarg3, (const char *)arg3); - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1pwhash_1scryptsalsa208sha256_1str(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jstring jarg2, jlong jarg3, jlong jarg4, jint jarg5) { - jint jresult = 0 ; - char *arg1 ; - char *arg2 = (char *) (char *)0 ; - unsigned long long arg3 ; - unsigned long long arg4 ; - size_t arg5 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - arg2 = 0; - if (jarg2) { - arg2 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg2, 0); - if (!arg2) return 0; - } - arg3 = (unsigned long long)jarg3; - arg4 = (unsigned long long)jarg4; - arg5 = (size_t)jarg5; - result = (int)crypto_pwhash_scryptsalsa208sha256_str(arg1,(char const *)arg2,arg3,arg4,arg5); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - - if (arg2) (*jenv)->ReleaseStringUTFChars(jenv, jarg2, (const char *)arg2); - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1pwhash_1scryptsalsa208sha256_1str_1verify(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jstring jarg2, jlong jarg3) { - jint jresult = 0 ; - char *arg1 ; - char *arg2 = (char *) (char *)0 ; - unsigned long long arg3 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - arg2 = 0; - if (jarg2) { - arg2 = (char *)(*jenv)->GetStringUTFChars(jenv, jarg2, 0); - if (!arg2) return 0; - } - arg3 = (unsigned long long)jarg3; - result = (int)crypto_pwhash_scryptsalsa208sha256_str_verify((char const (*))arg1,(char const *)arg2,arg3); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - - if (arg2) (*jenv)->ReleaseStringUTFChars(jenv, jarg2, (const char *)arg2); - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1shorthash(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jbyteArray jarg2, jlong jarg3, jbyteArray jarg4) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - unsigned char *arg2 = (unsigned char *) 0 ; - unsigned long long arg3 ; - unsigned char *arg4 = (unsigned char *) 0 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - { - arg2 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg2, 0); - } - arg3 = (unsigned long long)jarg3; - { - arg4 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg4, 0); - } - result = (int)crypto_shorthash(arg1,(unsigned char const *)arg2,arg3,(unsigned char const *)arg4); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg2, (jbyte *) arg2, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg4, (jbyte *) arg4, 0); - } - - - - return jresult; -} - - -SWIGEXPORT jint JNICALL Java_com_androidyuan_aesjni_SodiumJNI_crypto_1generichash(JNIEnv *jenv, jclass jcls, jbyteArray jarg1, jint jarg2, jbyteArray jarg3, jlong jarg4, jbyteArray jarg5, jint jarg6) { - jint jresult = 0 ; - unsigned char *arg1 = (unsigned char *) 0 ; - size_t arg2 ; - unsigned char *arg3 = (unsigned char *) 0 ; - unsigned long long arg4 ; - unsigned char *arg5 = (unsigned char *) 0 ; - size_t arg6 ; - int result; - - (void)jenv; - (void)jcls; - { - arg1 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg1, 0); - } - arg2 = (size_t)jarg2; - { - arg3 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg3, 0); - } - arg4 = (unsigned long long)jarg4; - { - arg5 = (unsigned char *) (*jenv)->GetByteArrayElements(jenv, jarg5, 0); - } - arg6 = (size_t)jarg6; - result = (int)crypto_generichash(arg1,arg2,(unsigned char const *)arg3,arg4,(unsigned char const *)arg5,arg6); - jresult = (jint)result; - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg1, (jbyte *) arg1, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg3, (jbyte *) arg3, 0); - } - { - (*jenv)->ReleaseByteArrayElements(jenv, jarg5, (jbyte *) arg5, 0); - } - - - - return jresult; -} - - -#ifdef __cplusplus -} -#endif - diff --git a/aesjni/src/main/jni/swig_gen.sh b/aesjni/src/main/jni/swig_gen.sh deleted file mode 100755 index f158536..0000000 --- a/aesjni/src/main/jni/swig_gen.sh +++ /dev/null @@ -1,59 +0,0 @@ -#!/bin/sh -# @Author: bruce - -command_exists() { - # check if command exists and fail otherwise - command -v "$1" >/dev/null 2>&1 - if [[ $? -ne 0 ]]; then #not found. - return 0 - else - return 1 - fi -} - -install_app_on_debian() { - echo "Debian try to install $1." - sudo apt update - sudo apt install $1 -} -install_app_on_mac() { - echo "This computer try to install $1." - brew install $1 -} - - -install_app() { - echo "try to install $1." - if [ -f /etc/debian_version ]; then - install_app_on_debian $1 - elif [[ "$OSTYPE" == "linux-gnu"* ]]; then - install_app_on_debian $1 - elif [[ "$OSTYPE" == "darwin"* ]]; then - install_app_on_mac $1 - else - echo "can't run on this system." - exit 1 - fi -} - - - - -try_install_app() { - command_exists $1 - is_app_installed=$? - if [ "$is_app_installed" -eq "0" ]; then - install_app $1 - command_exists $1 - is_app_installed=$? - if [ "$is_app_installed" -eq "0" ]; then - printf 'Error: %s has not been installed.\n' $1 - exit 1 - fi - fi -} - -# install swig if it has not been intalled. -try_install_app swig -# start generating to get c file and java class. -swig -java -package com.androidyuan.aesjni -outdir ../java/com/androidyuan/aesjni/ sodium.i \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle index 05f174c..8b1f8ec 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -13,8 +13,9 @@ android { } signingConfigs { + //you can regenerate it. release { - storeFile file("../keystore/androidyuan.keystore") //请看目录 + storeFile file("../keystore/androidyuan.keystore") storePassword "weizongwei" keyAlias "client1" keyPassword "weizongwei" @@ -36,11 +37,18 @@ android { dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) + implementation fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) - compile 'com.android.support:appcompat-v7:25.0.1' - compile project(':aesjni') - testCompile 'junit:junit:4.12' + implementation 'com.android.support:appcompat-v7:25.0.1' + implementation project(':aesjni') + + + // Required for local unit tests (JUnit 4 framework) + testImplementation 'junit:junit:4.12' + + // Required for instrumented tests + testImplementation 'com.android.support:support-annotations:24.0.0' + testImplementation 'com.android.support.test:runner:0.5' } diff --git a/app/src/main/java/com/androidyuan/aesjniencrypt/MainActivity.java b/app/src/main/java/com/androidyuan/aesjniencrypt/MainActivity.java index 3c8656b..2556820 100644 --- a/app/src/main/java/com/androidyuan/aesjniencrypt/MainActivity.java +++ b/app/src/main/java/com/androidyuan/aesjniencrypt/MainActivity.java @@ -5,7 +5,7 @@ import android.util.Log; import android.widget.Toast; -import com.androidyuan.aesjni.AESEncrypt; +import com.androidyuan.aesjni.EncryptEntry; public class MainActivity extends AppCompatActivity { @@ -19,12 +19,11 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); //下面的密文对应的原文:123abcABC&*(@#@#@)+_/中文测试 - final String code = AESEncrypt.encode(this, str); + final String code = EncryptEntry.encode(this, str); Log.i("code", code + ""); - final String decode = AESEncrypt.decode(this, encodeStr); + final String decode = EncryptEntry.decode(this, encodeStr); Log.i("decode", decode + ""); - } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c123a06..980be40 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -15,6 +15,6 @@ diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml new file mode 100644 index 0000000..22ecf14 --- /dev/null +++ b/app/src/main/res/values-zh/strings.xml @@ -0,0 +1,4 @@ + + AESJniEncrypt + 兄弟,这里是空的,加解密已经完成请看logcat! + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4a2bb0d..5085635 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,4 @@ AESJniEncrypt + There is nothing. Encryption and decryption has executed,Pls look at logcat. diff --git a/gradle.properties b/gradle.properties index 8f62aea..79af975 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,4 +15,4 @@ org.gradle.jvmargs=-Xmx1536m # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true -android.useDeprecatedNdk = true \ No newline at end of file +#android.useDeprecatedNdk = true \ No newline at end of file From 16f82b9fa107eaa22ac98a49d558cd967fc3b713 Mon Sep 17 00:00:00 2001 From: Bruce <6711309+BruceWind@users.noreply.github.com> Date: Mon, 22 Feb 2021 19:21:55 +0800 Subject: [PATCH 12/21] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 36cb136..d07b120 100644 --- a/README.md +++ b/README.md @@ -96,4 +96,4 @@ In case you would like to add information to this repository or suggest some ide ## Legal If you live in China, you should take care about [checking apk signature](https://github.com/BruceWind/AESJniEncrypt/blob/master/aesjni/src/main/cpp/check_emulator.c#L43). -I have called PackageManger ,it might be thought to read installed apps list,look at [工信部整治八项违规](http://www.miit.gov.cn/n1146295/n7281315/c7507241/part/7507297.docx). +I have called PackageManger ,it might be considered to read installed apps list,look at [工信部整治八项违规](http://www.miit.gov.cn/n1146295/n7281315/c7507241/part/7507297.docx). From 87968c7544bd788a029ab918b788d9651579785d Mon Sep 17 00:00:00 2001 From: hide Date: Tue, 23 Feb 2021 00:31:09 +0800 Subject: [PATCH 13/21] [fix]:{error from Ubuntu building.} --- aesjni/build.gradle | 15 ++++---------- aesjni/src/main/cpp/JNIEncrypt.c | 6 +++--- .../com/androidyuan/aesjni/EncryptEntry.java | 2 +- aesjni/src/main/jni/JNIEncrypt.c | 6 +++--- aesjni/src/main/jni/OriginAndroid.mk | 9 ++++----- aesjni/src/main/jni/keys_generator.c | 2 +- aesjni/src/main/jni/rebuild.sh | 20 ++++++++++++++----- app/build.gradle | 2 +- 8 files changed, 32 insertions(+), 30 deletions(-) diff --git a/aesjni/build.gradle b/aesjni/build.gradle index 4bc13e6..a25956b 100644 --- a/aesjni/build.gradle +++ b/aesjni/build.gradle @@ -3,11 +3,10 @@ apply plugin: 'com.android.library' android { compileSdkVersion 25 buildToolsVersion "25.0.3" - //ndkPath "/Users/bruce/Library/Android/sdk/ndk/16.1.4479499" -// ndkVersion '16.1.4479499' + ndkVersion '16.1.4479499' defaultConfig { minSdkVersion 21 - targetSdkVersion 25 + targetSdkVersion 22 versionCode 1 versionName "1.0" @@ -16,13 +15,7 @@ android { cmake { cppFlags "-frtti -fexceptions" } - } - ndk { - moduleName = "JNIEncrypt" - // Specifies the ABI configurations of your native - // libraries Gradle should build and package with your APK. - abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86' } // sourceSets { @@ -73,6 +66,6 @@ dependencies { implementation 'junit:junit:4.12' // Required for instrumented tests - androidTestCompile 'com.android.support:support-annotations:24.0.0' - androidTestCompile 'com.android.support.test:runner:0.5' + androidTestImplementation 'com.android.support:support-annotations:24.0.0' + androidTestImplementation 'com.android.support.test:runner:0.5' } diff --git a/aesjni/src/main/cpp/JNIEncrypt.c b/aesjni/src/main/cpp/JNIEncrypt.c index 5620439..dcf2c48 100644 --- a/aesjni/src/main/cpp/JNIEncrypt.c +++ b/aesjni/src/main/cpp/JNIEncrypt.c @@ -16,7 +16,7 @@ // 指定要注册的类,对应完整的java类名 #define JNIREG_CLASS "com/androidyuan/aesjni/EncryptEntry" -const char *UNSIGNATURE = "UNSIGNATURE"; +const char *WRONG_SIGNATURE = "WRONG_SIGNATURE"; jstring char2jstring(JNIEnv *envPtr, char *src) { JNIEnv env = *envPtr; @@ -69,7 +69,7 @@ JNIEXPORT jstring JNICALL encode(JNIEnv *env, jobject instance, jobject context, //先进行apk被 二次打包的校验 if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) { - char *str = UNSIGNATURE; + char *str = WRONG_SIGNATURE; // return (*env)->NewString(env, str, strlen(str)); return char2jstring(env,str); } @@ -91,7 +91,7 @@ JNIEXPORT jstring JNICALL decode(JNIEnv *env, jobject instance, jobject context, //先进行apk被 二次打包的校验 if (check_signature(env, instance, context) != 1|| check_is_emulator(env) != 1) { - char *str = UNSIGNATURE; + char *str = WRONG_SIGNATURE; // return (*env)->NewString(env, str, strlen(str)); return char2jstring(env,str); } diff --git a/aesjni/src/main/java/com/androidyuan/aesjni/EncryptEntry.java b/aesjni/src/main/java/com/androidyuan/aesjni/EncryptEntry.java index 21724d6..ec64715 100644 --- a/aesjni/src/main/java/com/androidyuan/aesjni/EncryptEntry.java +++ b/aesjni/src/main/java/com/androidyuan/aesjni/EncryptEntry.java @@ -32,7 +32,7 @@ public class EncryptEntry { * * @param context * @param str - * @return UNSIGNATURE : sign not pass . + * @return WRONG_SIGNATURE : sign not pass . */ public static native String decode(Object context, String str); diff --git a/aesjni/src/main/jni/JNIEncrypt.c b/aesjni/src/main/jni/JNIEncrypt.c index 19cca0d..0313d9b 100755 --- a/aesjni/src/main/jni/JNIEncrypt.c +++ b/aesjni/src/main/jni/JNIEncrypt.c @@ -13,7 +13,7 @@ // specific your Java class which jni entry. #define JNIREG_CLASS "com/androidyuan/aesjni/EncryptEntry" -const char *UNSIGNATURE = "UNSIGNATURE"; +const char *WRONG_SIGNATURE = "WRONG_SIGNATURE"; jstring char2jstring(JNIEnv *envPtr, char *src) { @@ -67,7 +67,7 @@ JNIEXPORT jstring JNICALL encode(JNIEnv *env, jobject instance, jobject context, //firstly, detect the apk is repackaged. if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) { - char *str = UNSIGNATURE; + char *str = WRONG_SIGNATURE; return char2jstring(env, str); } @@ -98,7 +98,7 @@ JNIEXPORT jstring JNICALL decode(JNIEnv *env, jobject instance, jobject context, //security checking. if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) { - char *str = UNSIGNATURE; + char *str = WRONG_SIGNATURE; return char2jstring(env, str); } diff --git a/aesjni/src/main/jni/OriginAndroid.mk b/aesjni/src/main/jni/OriginAndroid.mk index 5655abf..29da6d5 100755 --- a/aesjni/src/main/jni/OriginAndroid.mk +++ b/aesjni/src/main/jni/OriginAndroid.mk @@ -38,13 +38,12 @@ LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ check_emulator.c \ debugger.c \ logger.h -LOCAL_CFLAGS += -Wall -g -pedantic -std=c99 +LOCAL_CFLAGS += -Wall -g -pedantic -# if you want to print logs pls write liblog on this line. -LOCAL_STATIC_LIBRARIES := sodium liblog libcutils - -# LOCAL_SHARED_LIBRARIES := liblog libcutils +LOCAL_STATIC_LIBRARIES := sodium LOCAL_LDLIBS := -llog +LOCAL_LDLIBS += -Wl,--no-warn-shared-textrel + LOCAL_CPPFLAGS := -fexceptions include $(BUILD_SHARED_LIBRARY) diff --git a/aesjni/src/main/jni/keys_generator.c b/aesjni/src/main/jni/keys_generator.c index bcc1230..97ce89f 100644 --- a/aesjni/src/main/jni/keys_generator.c +++ b/aesjni/src/main/jni/keys_generator.c @@ -90,4 +90,4 @@ unsigned char *aes_noncehex2bin(char *hex) hex, aes_nonce_hex_len, NULL, NULL, NULL); return out_nonce; -} \ No newline at end of file +} diff --git a/aesjni/src/main/jni/rebuild.sh b/aesjni/src/main/jni/rebuild.sh index 9fd4b3d..e17be30 100755 --- a/aesjni/src/main/jni/rebuild.sh +++ b/aesjni/src/main/jni/rebuild.sh @@ -5,12 +5,18 @@ # And your computer must connect a device or emulator. # Pls modify BUILT_ABI for build compatible you device. # ------------------------------------------------------- -# Build success environmental system records: +# Build passed environments records: +# ********** 1."./rebuild.sh test" ******** # Mac OS: -# It has been built success with NDK r19c. +# NDK r19c,r13b. # Ubuntu 20.0 : -# It has been built success with ndk-bundle. You could download sdk,then use sdkmanger command to downlaod ndk-bundle. -# And NDK-r13b got failed due to google not supporting the old ndk to be with new system. +# any version of ndk. +# ********** 2."./rebuild.sh" ******** +# Mac OS: +# NDK-r13b. +# Ubuntu 20.0 : +# ndk-bundle. +# NDK-r13b. # #BUILT_ABI=x86 # make it compatible for emulator @@ -50,7 +56,11 @@ if [[ $1 == "test" ]]; then printf "=========================\n" else cp -f OriginAndroid.mk Android.mk - adb shell am instrument -w -m -e debug false -e class 'com.androidyuan.aesjni.JNITest' com.androidyuan.aesjni.test/android.support.test.runner.AndroidJUnitRunner + cd ../../../.. + ./gradlew installDebug --stacktrace + if [ $? == 0 ]; then # failed from pushing executable file. + adb shell am instrument -w -m -e debug false -e class 'com.androidyuan.aesjni.JNITest' com.androidyuan.aesjni.test/android.support.test.runner.AndroidJUnitRunner + fi fi diff --git a/app/build.gradle b/app/build.gradle index 8b1f8ec..d8c616d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -6,7 +6,7 @@ android { defaultConfig { applicationId "com.androidyuan.aesjniencrypt" minSdkVersion 21 - targetSdkVersion 25 + targetSdkVersion 22 //if targetSdkversion > =23,pls look at https://stackoverflow.com/questions/36114206/how-to-find-text-relocations-in-so-file versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" From 44ab5edee5c86114bc4d6de388ba389da1aeff76 Mon Sep 17 00:00:00 2001 From: bruce Date: Tue, 23 Feb 2021 14:22:41 +0800 Subject: [PATCH 14/21] [mk files]:{add more android.mk files for several version of ndk.} --- aesjni/build.gradle | 9 ++- aesjni/src/main/jni/OriginAndroid.mk | 5 +- aesjni/src/main/jni/OriginAndroid_r13_mac.mk | 50 ++++++++++++++ aesjni/src/main/jni/OriginAndroid_r16_mac.mk | 50 ++++++++++++++ .../src/main/jni/OriginAndroid_r16_ubuntu.mk | 49 ++++++++++++++ aesjni/src/main/jni/OriginAndroid_r22_mac.mk | 50 ++++++++++++++ aesjni/src/main/jni/debugger.c | 8 +-- aesjni/src/main/jni/rebuild.sh | 67 ------------------- aesjni/src/main/jni/test_in_executing.sh | 50 ++++++++++++++ 9 files changed, 263 insertions(+), 75 deletions(-) create mode 100755 aesjni/src/main/jni/OriginAndroid_r13_mac.mk create mode 100755 aesjni/src/main/jni/OriginAndroid_r16_mac.mk create mode 100755 aesjni/src/main/jni/OriginAndroid_r16_ubuntu.mk create mode 100755 aesjni/src/main/jni/OriginAndroid_r22_mac.mk delete mode 100755 aesjni/src/main/jni/rebuild.sh create mode 100755 aesjni/src/main/jni/test_in_executing.sh diff --git a/aesjni/build.gradle b/aesjni/build.gradle index a25956b..e4cf0ad 100644 --- a/aesjni/build.gradle +++ b/aesjni/build.gradle @@ -3,7 +3,7 @@ apply plugin: 'com.android.library' android { compileSdkVersion 25 buildToolsVersion "25.0.3" - ndkVersion '16.1.4479499' + //ndkVersion '16.1.4479499' defaultConfig { minSdkVersion 21 targetSdkVersion 22 @@ -42,7 +42,12 @@ android { if (USING_NDK == 1) { //selection 1 this used obfuscator to obfuscate the native code ndkBuild { - path file("src/main/jni/Android.mk") +// path file("src/main/jni/Android.mk") +// path file("src/main/jni/OriginAndroid.mk") +// path file("src/main/jni/OriginAndroid_r13_mac.mk") +// path file("src/main/jni/OriginAndroid_r16_mac.mk") + path file("src/main/jni/OriginAndroid_r22_mac.mk") +// path file("src/main/jni/OriginAndroid_r16_ubuntu.mk") } } else { //selection 2 the selection doesn't obfuscate native code **** i don't recommand using cmake***** diff --git a/aesjni/src/main/jni/OriginAndroid.mk b/aesjni/src/main/jni/OriginAndroid.mk index 29da6d5..b9b1fd2 100755 --- a/aesjni/src/main/jni/OriginAndroid.mk +++ b/aesjni/src/main/jni/OriginAndroid.mk @@ -40,10 +40,11 @@ LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ logger.h LOCAL_CFLAGS += -Wall -g -pedantic +# if you want to print logs pls write liblog on this line. LOCAL_STATIC_LIBRARIES := sodium -LOCAL_LDLIBS := -llog -LOCAL_LDLIBS += -Wl,--no-warn-shared-textrel +# LOCAL_SHARED_LIBRARIES := liblog libcutils +LOCAL_LDLIBS := -llog LOCAL_CPPFLAGS := -fexceptions include $(BUILD_SHARED_LIBRARY) diff --git a/aesjni/src/main/jni/OriginAndroid_r13_mac.mk b/aesjni/src/main/jni/OriginAndroid_r13_mac.mk new file mode 100755 index 0000000..bae67c0 --- /dev/null +++ b/aesjni/src/main/jni/OriginAndroid_r13_mac.mk @@ -0,0 +1,50 @@ +# ========== static sodium begin ================== +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) + ARCH_PREFIX := armv7-a +else ifeq ($(TARGET_ARCH_ABI),armeabi) + ARCH_PREFIX := armv6 +else ifeq ($(TARGET_ARCH_ABI),x86) + ARCH_PREFIX := i686 +else ifeq ($(TARGET_ARCH_ABI),mips) + ARCH_PREFIX := mips32 +else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) + ARCH_PREFIX := armv8-a +else + ARCH_PREFIX := $(TARGET_ARCH) +endif + +SODIUM_LIB_DIR := $(LOCAL_PATH)/sodium_include/libsodium-android-$(ARCH_PREFIX) +SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include +SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a +LOCAL_MODULE:= sodium +LOCAL_SRC_FILES:= $(SODIUM_LIB) +LOCAL_EXPORT_C_INCLUDES := $(SODIUM_INC_DIR) $(SODIUM_INC_DIR)/sodium +include $(PREBUILT_STATIC_LIBRARY) + +# ========== static sodium end ================== + +include $(CLEAR_VARS) + +LOCAL_MODULE := JNIEncrypt +LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ + base64.c \ + keys_generator.c \ + str_utils.cpp \ + checksignature.c \ + check_emulator.c \ + debugger.c \ + logger.h +LOCAL_CFLAGS += -Wall -g -pedantic -std=c99 + +# if you want to print logs pls write liblog on this line. +LOCAL_STATIC_LIBRARIES := sodium libcutils + +# LOCAL_SHARED_LIBRARIES := liblog libcutils +LOCAL_LDLIBS := -llog +LOCAL_CPPFLAGS := -fexceptions +include $(BUILD_SHARED_LIBRARY) + diff --git a/aesjni/src/main/jni/OriginAndroid_r16_mac.mk b/aesjni/src/main/jni/OriginAndroid_r16_mac.mk new file mode 100755 index 0000000..b9b1fd2 --- /dev/null +++ b/aesjni/src/main/jni/OriginAndroid_r16_mac.mk @@ -0,0 +1,50 @@ +# ========== static sodium begin ================== +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) + ARCH_PREFIX := armv7-a +else ifeq ($(TARGET_ARCH_ABI),armeabi) + ARCH_PREFIX := armv6 +else ifeq ($(TARGET_ARCH_ABI),x86) + ARCH_PREFIX := i686 +else ifeq ($(TARGET_ARCH_ABI),mips) + ARCH_PREFIX := mips32 +else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) + ARCH_PREFIX := armv8-a +else + ARCH_PREFIX := $(TARGET_ARCH) +endif + +SODIUM_LIB_DIR := $(LOCAL_PATH)/sodium_include/libsodium-android-$(ARCH_PREFIX) +SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include +SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a +LOCAL_MODULE:= sodium +LOCAL_SRC_FILES:= $(SODIUM_LIB) +LOCAL_EXPORT_C_INCLUDES := $(SODIUM_INC_DIR) $(SODIUM_INC_DIR)/sodium +include $(PREBUILT_STATIC_LIBRARY) + +# ========== static sodium end ================== + +include $(CLEAR_VARS) + +LOCAL_MODULE := JNIEncrypt +LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ + base64.c \ + keys_generator.c \ + str_utils.cpp \ + checksignature.c \ + check_emulator.c \ + debugger.c \ + logger.h +LOCAL_CFLAGS += -Wall -g -pedantic + +# if you want to print logs pls write liblog on this line. +LOCAL_STATIC_LIBRARIES := sodium + +# LOCAL_SHARED_LIBRARIES := liblog libcutils +LOCAL_LDLIBS := -llog +LOCAL_CPPFLAGS := -fexceptions +include $(BUILD_SHARED_LIBRARY) + diff --git a/aesjni/src/main/jni/OriginAndroid_r16_ubuntu.mk b/aesjni/src/main/jni/OriginAndroid_r16_ubuntu.mk new file mode 100755 index 0000000..29da6d5 --- /dev/null +++ b/aesjni/src/main/jni/OriginAndroid_r16_ubuntu.mk @@ -0,0 +1,49 @@ +# ========== static sodium begin ================== +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) + ARCH_PREFIX := armv7-a +else ifeq ($(TARGET_ARCH_ABI),armeabi) + ARCH_PREFIX := armv6 +else ifeq ($(TARGET_ARCH_ABI),x86) + ARCH_PREFIX := i686 +else ifeq ($(TARGET_ARCH_ABI),mips) + ARCH_PREFIX := mips32 +else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) + ARCH_PREFIX := armv8-a +else + ARCH_PREFIX := $(TARGET_ARCH) +endif + +SODIUM_LIB_DIR := $(LOCAL_PATH)/sodium_include/libsodium-android-$(ARCH_PREFIX) +SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include +SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a +LOCAL_MODULE:= sodium +LOCAL_SRC_FILES:= $(SODIUM_LIB) +LOCAL_EXPORT_C_INCLUDES := $(SODIUM_INC_DIR) $(SODIUM_INC_DIR)/sodium +include $(PREBUILT_STATIC_LIBRARY) + +# ========== static sodium end ================== + +include $(CLEAR_VARS) + +LOCAL_MODULE := JNIEncrypt +LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ + base64.c \ + keys_generator.c \ + str_utils.cpp \ + checksignature.c \ + check_emulator.c \ + debugger.c \ + logger.h +LOCAL_CFLAGS += -Wall -g -pedantic + +LOCAL_STATIC_LIBRARIES := sodium +LOCAL_LDLIBS := -llog +LOCAL_LDLIBS += -Wl,--no-warn-shared-textrel + +LOCAL_CPPFLAGS := -fexceptions +include $(BUILD_SHARED_LIBRARY) + diff --git a/aesjni/src/main/jni/OriginAndroid_r22_mac.mk b/aesjni/src/main/jni/OriginAndroid_r22_mac.mk new file mode 100755 index 0000000..b9b1fd2 --- /dev/null +++ b/aesjni/src/main/jni/OriginAndroid_r22_mac.mk @@ -0,0 +1,50 @@ +# ========== static sodium begin ================== +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) + ARCH_PREFIX := armv7-a +else ifeq ($(TARGET_ARCH_ABI),armeabi) + ARCH_PREFIX := armv6 +else ifeq ($(TARGET_ARCH_ABI),x86) + ARCH_PREFIX := i686 +else ifeq ($(TARGET_ARCH_ABI),mips) + ARCH_PREFIX := mips32 +else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) + ARCH_PREFIX := armv8-a +else + ARCH_PREFIX := $(TARGET_ARCH) +endif + +SODIUM_LIB_DIR := $(LOCAL_PATH)/sodium_include/libsodium-android-$(ARCH_PREFIX) +SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include +SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a +LOCAL_MODULE:= sodium +LOCAL_SRC_FILES:= $(SODIUM_LIB) +LOCAL_EXPORT_C_INCLUDES := $(SODIUM_INC_DIR) $(SODIUM_INC_DIR)/sodium +include $(PREBUILT_STATIC_LIBRARY) + +# ========== static sodium end ================== + +include $(CLEAR_VARS) + +LOCAL_MODULE := JNIEncrypt +LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ + base64.c \ + keys_generator.c \ + str_utils.cpp \ + checksignature.c \ + check_emulator.c \ + debugger.c \ + logger.h +LOCAL_CFLAGS += -Wall -g -pedantic + +# if you want to print logs pls write liblog on this line. +LOCAL_STATIC_LIBRARIES := sodium + +# LOCAL_SHARED_LIBRARIES := liblog libcutils +LOCAL_LDLIBS := -llog +LOCAL_CPPFLAGS := -fexceptions +include $(BUILD_SHARED_LIBRARY) + diff --git a/aesjni/src/main/jni/debugger.c b/aesjni/src/main/jni/debugger.c index f96c837..c496d06 100755 --- a/aesjni/src/main/jni/debugger.c +++ b/aesjni/src/main/jni/debugger.c @@ -18,13 +18,13 @@ int is_debug() { -#ifdef NDK_DEBUG //build with android.mk - LOGI("NDK_DEBUG : %d",NDK_DEBUG); - if(NDK_DEBUG==1){ +#ifdef MY_DEBUG //build with android.mk + LOGI("MY_DEBUG : %d",MY_DEBUG); + if(MY_DEBUG==1){ return 1; } #endif -LOGD("NDK_DEBUG not define."); +LOGD("MY_DEBUG not define."); #ifdef CMAKE_BUILD_TYPE //building with Cmakelist LOGI("is CMAKE_BUILD_TYPE. \n"); diff --git a/aesjni/src/main/jni/rebuild.sh b/aesjni/src/main/jni/rebuild.sh deleted file mode 100755 index e17be30..0000000 --- a/aesjni/src/main/jni/rebuild.sh +++ /dev/null @@ -1,67 +0,0 @@ -#!/bin/bash -# @author bruce: -# ========================Remind:======================== -# run this shell script must before, you computer must be set ANROID_NDK_HOME, -# And your computer must connect a device or emulator. -# Pls modify BUILT_ABI for build compatible you device. -# ------------------------------------------------------- -# Build passed environments records: -# ********** 1."./rebuild.sh test" ******** -# Mac OS: -# NDK r19c,r13b. -# Ubuntu 20.0 : -# any version of ndk. -# ********** 2."./rebuild.sh" ******** -# Mac OS: -# NDK-r13b. -# Ubuntu 20.0 : -# ndk-bundle. -# NDK-r13b. -# - -#BUILT_ABI=x86 # make it compatible for emulator -BUILT_ABI=armeabi-v7a # make it compatible for physical device -# detect whether build_libsodium_for_all_android_abi.sh has already been called. -if [ ! -d ./sodium_include/libsodium-android-armv7-a ]; then - printf "Please run build_libsodium_for_all_android_abi.sh before.\n" - exit -fi - -# clear cache. -clear -rm -r ../obj/ -rm -r ../libs/ -ndk-build clean - - -## run "./rebuild.sh test" -if [[ $1 == "test" ]]; then - printf "Start building......\n" - cp -f TestAndroid.mk Android.mk - ndk-build NDK_DEBUG=1 - if [ $? -ne 0 ]; then - exit # failed at ndk-build - fi - printf "adb push......\n" # push executable file into device or emulator. - adb push ../libs/$BUILT_ABI/test /data/local/tmp - - if [ $? -ne 0 ]; then # failed from pushing executable file. - printf "can't push executable file. Do you connect device or emulator? \n" - exit - fi - - printf "Here is executable file printed:\n" - printf "=========================\n" - adb shell /data/local/tmp/test - printf "=========================\n" -else - cp -f OriginAndroid.mk Android.mk - cd ../../../.. - ./gradlew installDebug --stacktrace - if [ $? == 0 ]; then # failed from pushing executable file. - adb shell am instrument -w -m -e debug false -e class 'com.androidyuan.aesjni.JNITest' com.androidyuan.aesjni.test/android.support.test.runner.AndroidJUnitRunner - fi -fi - - - diff --git a/aesjni/src/main/jni/test_in_executing.sh b/aesjni/src/main/jni/test_in_executing.sh new file mode 100755 index 0000000..6ba56f9 --- /dev/null +++ b/aesjni/src/main/jni/test_in_executing.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# @author bruce: +# ========================Remind:======================== +# run this shell script must before, you computer must be set ANROID_NDK_HOME, +# And your computer must connect a device or emulator. +# Pls modify BUILT_ABI for build compatible you device. +# ------------------------------------------------------- + +#BUILT_ABI=x86 # make it compatible for emulator +BUILT_ABI=armeabi-v7a # make it compatible for physical device + +#pls modify +TARGET_ANDROID_MK=OriginAndroid_r22_mac.mk +#TARGET_ANDROID_MK=OriginAndroid.mk +#TARGET_ANDROID_MK=OriginAndroid_r13_mac.mk # R13 is too old, ennable r13 might cause bugs. +#TARGET_ANDROID_MK=OriginAndroid_r16_mac.mk +#TARGET_ANDROID_MK=OriginAndroid_r16_ubuntu.mk +#TARGET_ANDROID_MK=OriginAndroid_r22_mac.mk + +# detect whether build_libsodium_for_all_android_abi.sh has already been called. +if [ ! -d ./sodium_include/libsodium-android-armv7-a ]; then + printf "Please run build_libsodium_for_all_android_abi.sh before.\n" + exit +fi + +# clear cache. +clear +rm -r ../obj/ +rm -r ../libs/ +ndk-build clean + +## run "./rebuild.sh test" +printf "Start building......\n" +cp -f TestAndroid.mk Android.mk +ndk-build NDK_DEBUG=1 +if [ $? -ne 0 ]; then + exit # failed at ndk-build +fi +printf "adb push......\n" # push executable file into device or emulator. +adb push ../libs/$BUILT_ABI/test /data/local/tmp + +if [ $? -ne 0 ]; then # failed from pushing executable file. + printf "can't push executable file. Do you connect device or emulator? \n" + exit +fi + +printf "Here is executable file printed:\n" +printf "=========================\n" +adb shell /data/local/tmp/test +printf "=========================\n" From f6859d6ec8d72c267b6701b34d1857d41e185787 Mon Sep 17 00:00:00 2001 From: bruce Date: Tue, 23 Feb 2021 16:32:41 +0800 Subject: [PATCH 15/21] [CMake]:{modify CMakeLists.txt and make it build suc.} remove cpp directory. --- aesjni/CMakeLists.txt | 90 ++-- aesjni/build.gradle | 8 +- aesjni/src/main/cpp/JNIEncrypt.c | 170 -------- aesjni/src/main/cpp/aes.c | 600 --------------------------- aesjni/src/main/cpp/aes.h | 54 --- aesjni/src/main/cpp/base64.c | 184 -------- aesjni/src/main/cpp/base64.h | 56 --- aesjni/src/main/cpp/check_emulator.c | 121 ------ aesjni/src/main/cpp/check_emulator.h | 19 - aesjni/src/main/cpp/checksignature.c | 64 --- aesjni/src/main/cpp/checksignature.h | 26 -- aesjni/src/main/cpp/debugger.c | 24 -- aesjni/src/main/cpp/debugger.h | 15 - aesjni/src/main/cpp/logger.h | 15 - aesjni/src/main/jni/TestAndroid.mk | 2 +- aesjni/src/main/jni/keys_generator.c | 2 +- 16 files changed, 61 insertions(+), 1389 deletions(-) delete mode 100644 aesjni/src/main/cpp/JNIEncrypt.c delete mode 100644 aesjni/src/main/cpp/aes.c delete mode 100644 aesjni/src/main/cpp/aes.h delete mode 100644 aesjni/src/main/cpp/base64.c delete mode 100644 aesjni/src/main/cpp/base64.h delete mode 100644 aesjni/src/main/cpp/check_emulator.c delete mode 100644 aesjni/src/main/cpp/check_emulator.h delete mode 100644 aesjni/src/main/cpp/checksignature.c delete mode 100644 aesjni/src/main/cpp/checksignature.h delete mode 100644 aesjni/src/main/cpp/debugger.c delete mode 100644 aesjni/src/main/cpp/debugger.h delete mode 100644 aesjni/src/main/cpp/logger.h diff --git a/aesjni/CMakeLists.txt b/aesjni/CMakeLists.txt index 5fe5f73..051c74a 100644 --- a/aesjni/CMakeLists.txt +++ b/aesjni/CMakeLists.txt @@ -1,41 +1,61 @@ cmake_minimum_required(VERSION 3.4.1) #普通版本的clang +# specify the C++ standard +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +# -------configure import libsodium begin------ +if (${ANDROID_ABI} STREQUAL "armeabi-v7a") + set(ARCH_PREFIX "armv7-a") +elseif (${ANDROID_ABI} STREQUAL "arm64-v8a") + set(ARCH_PREFIX "armv8-a") +elseif (${ANDROID_ABI} STREQUAL "armeabi") + set(ARCH_PREFIX "armv6") +elseif (${ANDROID_ABI} STREQUAL "x86") + set(ARCH_PREFIX "i686") +elseif (${ANDROID_ABI} STREQUAL "mips") + set(ARCH_PREFIX "mips32") +else () + #set(ARCH_PREFIX ${ANDROID_ABI}) + message(FATAL_ERROR "${ANDROID_ABI} static libraries might doesn't exist.") +endif () + +set(distribution_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jni/sodium_include/libsodium-android-${ARCH_PREFIX}) + +message(STATUS "Current build include : ${distribution_DIR} ") +add_library(libsodium STATIC IMPORTED) +set_target_properties(libsodium PROPERTIES IMPORTED_LOCATION + ${distribution_DIR}/lib/libsodium.a) +# include C export files. +include_directories(${distribution_DIR}/include) +# -------configure import libsodium end------ -#base on libsodium-1.0.18,which was downloaded from https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-stable.tar.gz; add_library( # Sets the name of the library. - JNIEncrypt - - # Sets the library as a shared library. - SHARED - - # Provides a relative path to your source file(s). - src/main/cpp/JNIEncrypt.c - src/main/cpp/checksignature.h - src/main/cpp/checksignature.c - src/main/cpp/check_emulator.h - src/main/cpp/check_emulator.c - src/main/cpp/debugger.h - src/main/cpp/debugger.c - src/main/cpp/aes.h - src/main/cpp/base64.h - src/main/cpp/aes.c - src/main/cpp/base64.c - ) - -# 搜索指定的预构建库并将路径存储为变量。 因为默认情况下,CMake在搜索路径中包含系统库, -# 所以您只需要指定公共NDK库的名称你想添加。 在完成构建之前,CMake验证库存在。 - -find_library( # Sets the name of the path variable. - log-lib - - # Specifies the name of the NDK library that - # you want CMake to locate. - log ) - -# 指定CMake应链接到目标库的库。 您可以链接多个库,例如在此构建脚本中定义的库,预构建的第三方库或系统库。 + JNIEncrypt + + # Sets the library as a shared library. + SHARED + + # Provides a relative path to your source file(s). + src/main/jni/checksignature.h + src/main/jni/checksignature.c + src/main/jni/check_emulator.h + src/main/jni/check_emulator.c + src/main/jni/debugger.h + src/main/jni/debugger.c + src/main/jni/logger.h + src/main/jni/keys_generator.h + src/main/jni/keys_generator.c + src/main/jni/base64.h + src/main/jni/base64.c + src/main/jni/str_utils.cpp + src/main/jni/JNIEncrypt.c + ) + +find_library(log-lib + log) target_link_libraries( # Specifies the target library. - JNIEncrypt - - # included in the NDK. - ${log-lib} ) + JNIEncrypt + libsodium + ${log-lib}) diff --git a/aesjni/build.gradle b/aesjni/build.gradle index e4cf0ad..8981eea 100644 --- a/aesjni/build.gradle +++ b/aesjni/build.gradle @@ -36,17 +36,17 @@ android { } } - def USING_NDK = 1 // if you want to build with NDK, set it to 1. + def USING_NDK = false // if you want to build with CMakelist,set false. externalNativeBuild { - if (USING_NDK == 1) { + if (USING_NDK) { //selection 1 this used obfuscator to obfuscate the native code ndkBuild { // path file("src/main/jni/Android.mk") // path file("src/main/jni/OriginAndroid.mk") -// path file("src/main/jni/OriginAndroid_r13_mac.mk") + path file("src/main/jni/OriginAndroid_r13_mac.mk") // path file("src/main/jni/OriginAndroid_r16_mac.mk") - path file("src/main/jni/OriginAndroid_r22_mac.mk") +// path file("src/main/jni/OriginAndroid_r22_mac.mk") // path file("src/main/jni/OriginAndroid_r16_ubuntu.mk") } } else { diff --git a/aesjni/src/main/cpp/JNIEncrypt.c b/aesjni/src/main/cpp/JNIEncrypt.c deleted file mode 100644 index dcf2c48..0000000 --- a/aesjni/src/main/cpp/JNIEncrypt.c +++ /dev/null @@ -1,170 +0,0 @@ -#include -#include "aes.h" -#include "checksignature.h" -#include "check_emulator.h" -#include -#include -#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) -#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) - -#define CBC 1 -#define ECB 1 - - -// 获取数组的大小 -# define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) -// 指定要注册的类,对应完整的java类名 -#define JNIREG_CLASS "com/androidyuan/aesjni/EncryptEntry" - -const char *WRONG_SIGNATURE = "WRONG_SIGNATURE"; - -jstring char2jstring(JNIEnv *envPtr, char *src) { - JNIEnv env = *envPtr; - - jsize len = strlen(src); - jclass clsstring = env->FindClass(envPtr, "java/lang/String"); - jstring strencode = env->NewStringUTF(envPtr, "UTF-8"); - jmethodID mid = env->GetMethodID(envPtr, clsstring, "", - "([BLjava/lang/String;)V"); - jbyteArray barr = env->NewByteArray(envPtr, len); - env->SetByteArrayRegion(envPtr, barr, 0, len, (jbyte *) src); - - return (jstring) env->NewObject(envPtr, clsstring, mid, barr, strencode); -} - -//__attribute__((section (".mytext")))//隐藏字符表 并没有什么卵用 只是针对初阶hacker的一个小方案而已 -char *getKey() { - int n = 0; - char s[23];//"NMTIzNDU2Nzg5MGFiY2RlZg"; - - s[n++] = 'N'; - s[n++] = 'M'; - s[n++] = 'T'; - s[n++] = 'I'; - s[n++] = 'z'; - s[n++] = 'N'; - s[n++] = 'D'; - s[n++] = 'U'; - s[n++] = '2'; - s[n++] = 'N'; - s[n++] = 'z'; - s[n++] = 'g'; - s[n++] = '5'; - s[n++] = 'M'; - s[n++] = 'G'; - s[n++] = 'F'; - s[n++] = 'i'; - s[n++] = 'Y'; - s[n++] = '2'; - s[n++] = 'R'; - s[n++] = 'l'; - s[n++] = 'Z'; - s[n++] = 'g'; - char *encode_str = s + 1; - return b64_decode(encode_str, strlen(encode_str)); - -} - -JNIEXPORT jstring JNICALL encode(JNIEnv *env, jobject instance, jobject context, jstring str_) { - - //先进行apk被 二次打包的校验 - if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) { - char *str = WRONG_SIGNATURE; -// return (*env)->NewString(env, str, strlen(str)); - return char2jstring(env,str); - } - - uint8_t *AES_KEY = (uint8_t *) getKey(); - const char *in = (*env)->GetStringUTFChars(env, str_, JNI_FALSE); - char *baseResult = AES_128_ECB_PKCS5Padding_Encrypt(in, AES_KEY); - (*env)->ReleaseStringUTFChars(env, str_, in); -// return (*env)->NewStringUTF(env, baseResult); - jstring result = (*env)->NewStringUTF(env, baseResult); - free(baseResult); - free(AES_KEY); - return result; -} - - -JNIEXPORT jstring JNICALL decode(JNIEnv *env, jobject instance, jobject context, jstring str_) { - - - //先进行apk被 二次打包的校验 - if (check_signature(env, instance, context) != 1|| check_is_emulator(env) != 1) { - char *str = WRONG_SIGNATURE; -// return (*env)->NewString(env, str, strlen(str)); - return char2jstring(env,str); - } - - uint8_t *AES_KEY = (uint8_t *) getKey(); - const char *str = (*env)->GetStringUTFChars(env, str_, JNI_FALSE); - char *desResult = AES_128_ECB_PKCS5Padding_Decrypt(str, AES_KEY); - (*env)->ReleaseStringUTFChars(env, str_, str); -// return (*env)->NewStringUTF(env, desResult); - //不用系统自带的方法NewStringUTF是因为如果desResult是乱码,会抛出异常 -// return char2jstring(env,desResult); - jstring result = char2jstring(env,desResult); - free(desResult); - free(AES_KEY); - return result; -} - - -/** - * if rerurn 1 ,is check pass. - */ -JNIEXPORT jint JNICALL -check_jni(JNIEnv *env, jobject instance, jobject con) { - return check_signature(env, instance, con); -} - - -// Java和JNI函数的绑定表 -static JNINativeMethod method_table[] = { - {"checkSignature", "(Ljava/lang/Object;)I", (void *) check_jni}, - {"decode", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/String;", (void *) decode}, - {"encode", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/String;", (void *) encode}, -}; - -// 注册native方法到java中 -static int registerNativeMethods(JNIEnv *env, const char *className, - JNINativeMethod *gMethods, int numMethods) { - jclass clazz; - clazz = (*env)->FindClass(env, className); - if (clazz == NULL) { - return JNI_FALSE; - } - if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) { - return JNI_FALSE; - } - - return JNI_TRUE; -} - -int register_ndk_load(JNIEnv *env) { - // 调用注册方法 - return registerNativeMethods(env, JNIREG_CLASS, - method_table, NELEM(method_table)); -} - -JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { - - ptrace(PTRACE_TRACEME, 0, 0, 0);//反调试 -//这是一种比较简单的防止被调试的方案 -// 有更复杂更高明的方案,比如:不用这个ptrace而是每次执行加密解密签先去判断是否被trace,目前的版本不做更多的负载方案,您想做可以fork之后,自己去做 - - - - JNIEnv *env = NULL; - jint result = -1; - - if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_4) != JNI_OK) { - return result; - } - - register_ndk_load(env); - -// 返回jni的版本 - return JNI_VERSION_1_4; -} - diff --git a/aesjni/src/main/cpp/aes.c b/aesjni/src/main/cpp/aes.c deleted file mode 100644 index 7b2c2b4..0000000 --- a/aesjni/src/main/cpp/aes.c +++ /dev/null @@ -1,600 +0,0 @@ -/* - -This is an implementation of the AES128 algorithm, specifically ECB and CBC mode. - -The implementation is verified against the test vectors in: - National Institute of Standards and Technology Special Publication 800-38A 2001 ED - -ECB-AES128 ----------- - - plain-text: - 6bc1bee22e409f96e93d7e117393172a - ae2d8a571e03ac9c9eb76fac45af8e51 - 30c81c46a35ce411e5fbc1191a0a52ef - f69f2445df4f9b17ad2b417be66c3710 - - key: - 2b7e151628aed2a6abf7158809cf4f3c - - resulting cipher - 3ad77bb40d7a3660a89ecaf32466ef97 - f5d3d58503b9699de785895a96fdbaaf - 43b1cd7f598ece23881b00e3ed030688 - 7b0c785e27e8ad3f8223207104725dd4 - - -NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0) - You should pad the end of the string with zeros if this is not the case. - -*/ - - -/*****************************************************************************/ -/* Includes: */ -/*****************************************************************************/ - -#include "aes.h" - - -/*****************************************************************************/ -/* Defines: */ -/*****************************************************************************/ -// The number of columns comprising a state in AES. This is a constant in AES. Value=4 -#define Nb 4 -// The number of 32 bit words in a key. -#define Nk 4 -// Key length in bytes [128 bit] -#define KEYLEN 16 -// The number of rounds in AES Cipher. -#define Nr 10 - -// jcallan@github points out that declaring Multiply as a function -// reduces code size considerably with the Keil ARM compiler. -// See this link for more information: https://github.com/kokke/tiny-AES128-C/pull/3 -#ifndef MULTIPLY_AS_A_FUNCTION - #define MULTIPLY_AS_A_FUNCTION 0 -#endif - - -/*****************************************************************************/ -/* Private variables: */ -/*****************************************************************************/ -// state - array holding the intermediate results during decryption. -typedef uint8_t state_t[4][4]; -static state_t* state; - -// The array that stores the round keys. -static uint8_t RoundKey[176]; - -// The Key input to the AES Program -static const uint8_t* Key; -#if defined(CBC) && CBC - // Initial Vector used only for CBC mode - static uint8_t* Iv; -#endif - -// The lookup-tables are marked const so they can be placed in read-only storage instead of RAM -// The numbers below can be computed dynamically trading ROM for RAM - -// This can be useful in (embedded) bootloader applications, where ROM is often limited. -static const uint8_t sbox[256] = { - //0 1 2 3 4 5 6 7 8 9 A B C D E F - 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, - 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, - 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, - 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, - 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, - 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, - 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, - 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, - 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, - 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, - 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, - 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, - 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, - 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, - 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, - 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 }; - -static const uint8_t rsbox[256] = -{ 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb, - 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb, - 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e, - 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25, - 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92, - 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84, - 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06, - 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b, - 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73, - 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e, - 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b, - 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4, - 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f, - 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef, - 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61, - 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d }; - - -// The round constant word array, Rcon[i], contains the values given by -// x to th e power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8) -// Note that i starts at 1, not 0). -static const uint8_t Rcon[255] = { - 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, - 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, - 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, - 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, - 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, - 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, - 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, - 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, - 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, - 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, - 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, - 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, - 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, - 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, - 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39, 0x72, 0xe4, 0xd3, 0xbd, - 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a, 0x74, 0xe8, 0xcb }; - - -/*****************************************************************************/ -/* Private functions: */ -/*****************************************************************************/ -static uint8_t getSBoxValue(uint8_t num) -{ - return sbox[num]; -} - -static uint8_t getSBoxInvert(uint8_t num) -{ - return rsbox[num]; -} - -// This function produces Nb(Nr+1) round keys. The round keys are used in each round to decrypt the states. -static void KeyExpansion(void) -{ - uint32_t i, j, k; - uint8_t tempa[4]; // Used for the column/row operations - - // The first round key is the key itself. - for(i = 0; i < Nk; ++i) - { - RoundKey[(i * 4) + 0] = Key[(i * 4) + 0]; - RoundKey[(i * 4) + 1] = Key[(i * 4) + 1]; - RoundKey[(i * 4) + 2] = Key[(i * 4) + 2]; - RoundKey[(i * 4) + 3] = Key[(i * 4) + 3]; - } - - // All other round keys are found from the previous round keys. - for(; (i < (Nb * (Nr + 1))); ++i) - { - for(j = 0; j < 4; ++j) - { - tempa[j]=RoundKey[(i-1) * 4 + j]; - } - if (i % Nk == 0) - { - // This function rotates the 4 bytes in a word to the left once. - // [a0,a1,a2,a3] becomes [a1,a2,a3,a0] - - // Function RotWord() - { - k = tempa[0]; - tempa[0] = tempa[1]; - tempa[1] = tempa[2]; - tempa[2] = tempa[3]; - tempa[3] = k; - } - - // SubWord() is a function that takes a four-byte input word and - // applies the S-box to each of the four bytes to produce an output word. - - // Function Subword() - { - tempa[0] = getSBoxValue(tempa[0]); - tempa[1] = getSBoxValue(tempa[1]); - tempa[2] = getSBoxValue(tempa[2]); - tempa[3] = getSBoxValue(tempa[3]); - } - - tempa[0] = tempa[0] ^ Rcon[i/Nk]; - } - else if (Nk > 6 && i % Nk == 4) - { - // Function Subword() - { - tempa[0] = getSBoxValue(tempa[0]); - tempa[1] = getSBoxValue(tempa[1]); - tempa[2] = getSBoxValue(tempa[2]); - tempa[3] = getSBoxValue(tempa[3]); - } - } - RoundKey[i * 4 + 0] = RoundKey[(i - Nk) * 4 + 0] ^ tempa[0]; - RoundKey[i * 4 + 1] = RoundKey[(i - Nk) * 4 + 1] ^ tempa[1]; - RoundKey[i * 4 + 2] = RoundKey[(i - Nk) * 4 + 2] ^ tempa[2]; - RoundKey[i * 4 + 3] = RoundKey[(i - Nk) * 4 + 3] ^ tempa[3]; - } -} - -// This function adds the round key to state. -// The round key is added to the state by an XOR function. -static void AddRoundKey(uint8_t round) -{ - uint8_t i,j; - for(i=0;i<4;++i) - { - for(j = 0; j < 4; ++j) - { - (*state)[i][j] ^= RoundKey[round * Nb * 4 + i * Nb + j]; - } - } -} - -// The SubBytes Function Substitutes the values in the -// state matrix with values in an S-box. -static void SubBytes(void) -{ - uint8_t i, j; - for(i = 0; i < 4; ++i) - { - for(j = 0; j < 4; ++j) - { - (*state)[j][i] = getSBoxValue((*state)[j][i]); - } - } -} - -// The ShiftRows() function shifts the rows in the state to the left. -// Each row is shifted with different offset. -// Offset = Row number. So the first row is not shifted. -static void ShiftRows(void) -{ - uint8_t temp; - - // Rotate first row 1 columns to left - temp = (*state)[0][1]; - (*state)[0][1] = (*state)[1][1]; - (*state)[1][1] = (*state)[2][1]; - (*state)[2][1] = (*state)[3][1]; - (*state)[3][1] = temp; - - // Rotate second row 2 columns to left - temp = (*state)[0][2]; - (*state)[0][2] = (*state)[2][2]; - (*state)[2][2] = temp; - - temp = (*state)[1][2]; - (*state)[1][2] = (*state)[3][2]; - (*state)[3][2] = temp; - - // Rotate third row 3 columns to left - temp = (*state)[0][3]; - (*state)[0][3] = (*state)[3][3]; - (*state)[3][3] = (*state)[2][3]; - (*state)[2][3] = (*state)[1][3]; - (*state)[1][3] = temp; -} - -static uint8_t xtime(uint8_t x) -{ - return ((x<<1) ^ (((x>>7) & 1) * 0x1b)); -} - -// MixColumns function mixes the columns of the state matrix -static void MixColumns(void) -{ - uint8_t i; - uint8_t Tmp,Tm,t; - for(i = 0; i < 4; ++i) - { - t = (*state)[i][0]; - Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ; - Tm = (*state)[i][0] ^ (*state)[i][1] ; Tm = xtime(Tm); (*state)[i][0] ^= Tm ^ Tmp ; - Tm = (*state)[i][1] ^ (*state)[i][2] ; Tm = xtime(Tm); (*state)[i][1] ^= Tm ^ Tmp ; - Tm = (*state)[i][2] ^ (*state)[i][3] ; Tm = xtime(Tm); (*state)[i][2] ^= Tm ^ Tmp ; - Tm = (*state)[i][3] ^ t ; Tm = xtime(Tm); (*state)[i][3] ^= Tm ^ Tmp ; - } -} - -// Multiply is used to multiply numbers in the field GF(2^8) -#if MULTIPLY_AS_A_FUNCTION -static uint8_t Multiply(uint8_t x, uint8_t y) -{ - return (((y & 1) * x) ^ - ((y>>1 & 1) * xtime(x)) ^ - ((y>>2 & 1) * xtime(xtime(x))) ^ - ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ - ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))); - } -#else -#define Multiply(x, y) \ - ( ((y & 1) * x) ^ \ - ((y>>1 & 1) * xtime(x)) ^ \ - ((y>>2 & 1) * xtime(xtime(x))) ^ \ - ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ \ - ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))) \ - -#endif - -// MixColumns function mixes the columns of the state matrix. -// The method used to multiply may be difficult to understand for the inexperienced. -// Please use the references to gain more information. -static void InvMixColumns(void) -{ - int i; - uint8_t a,b,c,d; - for(i=0;i<4;++i) - { - a = (*state)[i][0]; - b = (*state)[i][1]; - c = (*state)[i][2]; - d = (*state)[i][3]; - - (*state)[i][0] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09); - (*state)[i][1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d); - (*state)[i][2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b); - (*state)[i][3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e); - } -} - - -// The SubBytes Function Substitutes the values in the -// state matrix with values in an S-box. -static void InvSubBytes(void) -{ - uint8_t i,j; - for(i=0;i<4;++i) - { - for(j=0;j<4;++j) - { - (*state)[j][i] = getSBoxInvert((*state)[j][i]); - } - } -} - -static void InvShiftRows(void) -{ - uint8_t temp; - - // Rotate first row 1 columns to right - temp=(*state)[3][1]; - (*state)[3][1]=(*state)[2][1]; - (*state)[2][1]=(*state)[1][1]; - (*state)[1][1]=(*state)[0][1]; - (*state)[0][1]=temp; - - // Rotate second row 2 columns to right - temp=(*state)[0][2]; - (*state)[0][2]=(*state)[2][2]; - (*state)[2][2]=temp; - - temp=(*state)[1][2]; - (*state)[1][2]=(*state)[3][2]; - (*state)[3][2]=temp; - - // Rotate third row 3 columns to right - temp=(*state)[0][3]; - (*state)[0][3]=(*state)[1][3]; - (*state)[1][3]=(*state)[2][3]; - (*state)[2][3]=(*state)[3][3]; - (*state)[3][3]=temp; -} - - -// Cipher is the main function that encrypts the PlainText. -static void Cipher(void) -{ - uint8_t round = 0; - - // Add the First round key to the state before starting the rounds. - AddRoundKey(0); - - // There will be Nr rounds. - // The first Nr-1 rounds are identical. - // These Nr-1 rounds are executed in the loop below. - for(round = 1; round < Nr; ++round) - { - SubBytes(); - ShiftRows(); - MixColumns(); - AddRoundKey(round); - } - - // The last round is given below. - // The MixColumns function is not here in the last round. - SubBytes(); - ShiftRows(); - AddRoundKey(Nr); -} - -static void InvCipher(void) -{ - uint8_t round=0; - - // Add the First round key to the state before starting the rounds. - AddRoundKey(Nr); - - // There will be Nr rounds. - // The first Nr-1 rounds are identical. - // These Nr-1 rounds are executed in the loop below. - for(round=Nr-1;round>0;round--) - { - InvShiftRows(); - InvSubBytes(); - AddRoundKey(round); - InvMixColumns(); - } - - // The last round is given below. - // The MixColumns function is not here in the last round. - InvShiftRows(); - InvSubBytes(); - AddRoundKey(0); -} - -static void BlockCopy(uint8_t* output, uint8_t* input) -{ - uint8_t i; - for (i=0;istrlen) will crash. - memset(out+index, '\0', inputLength-index); - } - //去除结尾垃圾字符串 end - - - //LOGE("解密结果:"); - //LOGE(out); - free(inputDesBase64); - return (char *) out; -} - -/** - * 查找结果中的一些 多余字符串 - * @param str : 加密结果原文 - * @return int : 垃圾字符串的开始位置 - */ -int findPaddingIndex(uint8_t * out, int len) -{ - if(out[len - 1] > 0x10){ - return 0; - }else{ - return (int)(len - out[len - 1]); - } - - -} - - -/** - * - * 这里干掉了CBC 相关代码 ,这块代码是一个AES的一个带有向量的算法 - * 找寻这些代码 请移步 https://github.com/kokke/tiny-AES128-C - -#if defined(CBC) && CBC -#endif // #if defined(CBC) && CBC - -*/ - -#endif // #if defined(ECB) && ECB diff --git a/aesjni/src/main/cpp/aes.h b/aesjni/src/main/cpp/aes.h deleted file mode 100644 index f30acb2..0000000 --- a/aesjni/src/main/cpp/aes.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef _AES_H_ -#define _AES_H_ - -#include -#include -#include -#include -#include "base64.h" - -// #define the macros below to 1/0 to enable/disable the mode of operation. -// -// CBC enables AES128 encryption in CBC-mode of operation and handles 0-padding. -// ECB enables the basic ECB 16-byte block algorithm. Both can be enabled simultaneously. - -// The #ifndef-guard allows it to be configured before #include'ing or at compile time. -#ifndef CBC - #define CBC 1 -#endif - -#ifndef ECB - #define ECB 1 -#endif -#ifndef LOGE -#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,"native-jni",__VA_ARGS__) -#endif - -static const unsigned char HEX[16]={0x10,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}; - -//__attribute__((section (".mytext"))) -//static const uint8_t AES_KEY[]="1234567890abcdef"; - - -#if defined(ECB) && ECB - -void AES128_ECB_encrypt(uint8_t* input, const uint8_t* key, uint8_t *output); -void AES128_ECB_decrypt(uint8_t* input, const uint8_t* key, uint8_t *output); - -char* AES_128_ECB_PKCS5Padding_Encrypt(const char *in,const uint8_t *key); -char* AES_128_ECB_PKCS5Padding_Decrypt(const char *in, const uint8_t* key); - -int findPaddingIndex(uint8_t * out, int len); -#endif // #if defined(ECB) && ECB - - -/* - * CBC是向量模式 暂不采用 - 如果需要使用 ,请移步 https://github.com/kokke/tiny-AES128-C - -#if defined(CBC) && CBC - -#endif // #if defined(CBC) && CBC - -*/ -#endif //_AES_H_ diff --git a/aesjni/src/main/cpp/base64.c b/aesjni/src/main/cpp/base64.c deleted file mode 100644 index e8836de..0000000 --- a/aesjni/src/main/cpp/base64.c +++ /dev/null @@ -1,184 +0,0 @@ - -/** - * `encode.c' - b64 - * - * copyright (c) 2014 joseph werle - */ - -#include -#include -#include -#include "base64.h" - - - -char * -b64_encode (const unsigned char *src, size_t len) { - int i = 0; - int j = 0; - char *enc = NULL; - size_t size = 0; - unsigned char buf[4]; - unsigned char tmp[3]; - - // alloc - enc = (char *) malloc(0); - if (NULL == enc) { return NULL; } - - // parse until end of source - while (len--) { - // read up to 3 bytes at a time into `tmp' - tmp[i++] = *(src++); - - // if 3 bytes read then encode into `buf' - if (3 == i) { - buf[0] = (tmp[0] & 0xfc) >> 2; - buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4); - buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6); - buf[3] = tmp[2] & 0x3f; - - // allocate 4 new byts for `enc` and - // then translate each encoded buffer - // part by index from the base 64 index table - // into `enc' unsigned char array - enc = (char *) realloc(enc, size + 4); - for (i = 0; i < 4; ++i) { - enc[size++] = b64_table[buf[i]]; - } - - // reset index - i = 0; - } - } - - // remainder - if (i > 0) { - // fill `tmp' with `\0' at most 3 times - for (j = i; j < 3; ++j) { - tmp[j] = '\0'; - } - - // perform same codec as above - buf[0] = (tmp[0] & 0xfc) >> 2; - buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4); - buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6); - buf[3] = tmp[2] & 0x3f; - - // perform same write to `enc` with new allocation - for (j = 0; (j < i + 1); ++j) { - enc = (char *) realloc(enc, size + 1); - enc[size++] = b64_table[buf[j]]; - } - - // while there is still a remainder - // append `=' to `enc' - while ((i++ < 3)) { - enc = (char *) realloc(enc, size + 1); - enc[size++] = '='; - } - } - - // Make sure we have enough space to add '\0' character at end. - enc = (char *) realloc(enc, size + 1); - enc[size] = '\0'; - - return enc; -} - - -unsigned char * -b64_decode(const char *src, size_t len) { - return b64_decode_ex(src, len, NULL); -} - -unsigned char * -b64_decode_ex(const char *src, size_t len, size_t *decsize) { - int i = 0; - int j = 0; - int l = 0; - size_t size = 0; - unsigned char *dec = NULL; - unsigned char buf[3]; - unsigned char tmp[4]; - - // alloc - dec = (unsigned char *) malloc(0); - if (NULL == dec) { return NULL; } - - // parse until end of source - while (len--) { - // break if char is `=' or not base64 char - if ('=' == src[j]) { break; } - if (!(isalnum(src[j]) || '+' == src[j] || '/' == src[j])) { break; } - - // read up to 4 bytes at a time into `tmp' - tmp[i++] = src[j++]; - - // if 4 bytes read then decode into `buf' - if (4 == i) { - // translate values in `tmp' from table - for (i = 0; i < 4; ++i) { - // find translation char in `b64_table' - for (l = 0; l < 64; ++l) { - if (tmp[i] == b64_table[l]) { - tmp[i] = l; - break; - } - } - } - - // decode - buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4); - buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2); - buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3]; - - // write decoded buffer to `dec' - dec = (unsigned char *) realloc(dec, size + 3); - for (i = 0; i < 3; ++i) { - dec[size++] = buf[i]; - } - - // reset - i = 0; - } - } - - // remainder - if (i > 0) { - // fill `tmp' with `\0' at most 4 times - for (j = i; j < 4; ++j) { - tmp[j] = '\0'; - } - - // translate remainder - for (j = 0; j < 4; ++j) { - // find translation char in `b64_table' - for (l = 0; l < 64; ++l) { - if (tmp[j] == b64_table[l]) { - tmp[j] = l; - break; - } - } - } - - // decode remainder - buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4); - buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2); - buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3]; - - // write remainer decoded buffer to `dec' - dec = (unsigned char *) realloc(dec, size + (i - 1)); - for (j = 0; (j < i - 1); ++j) { - dec[size++] = buf[j]; - } - } - - // Make sure we have enough space to add '\0' character at end. - dec = (unsigned char *) realloc(dec, size + 1); - dec[size] = '\0'; - - // Return back the size of decoded string if demanded. - if (decsize != NULL) *decsize = size; - - return dec; -} diff --git a/aesjni/src/main/cpp/base64.h b/aesjni/src/main/cpp/base64.h deleted file mode 100644 index 264c6b6..0000000 --- a/aesjni/src/main/cpp/base64.h +++ /dev/null @@ -1,56 +0,0 @@ - -/** - * `b64.h' - b64 - * - * copyright (c) 2014 joseph werle - */ - -#ifndef B64_H -#define B64_H 1 - -/** - * Base64 index table. - */ - -static const char b64_table[] = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', - 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', - 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', - 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', - 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', - 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', - 'w', 'x', 'y', 'z', '0', '1', '2', '3', - '4', '5', '6', '7', '8', '9', '+', '/' -}; - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * Encode `unsigned char *' source with `size_t' size. - * Returns a `char *' base64 encoded string. - */ - -char * -b64_encode (const unsigned char *, size_t); - -/** - * Dencode `char *' source with `size_t' size. - * Returns a `unsigned char *' base64 decoded string. - */ -unsigned char * -b64_decode (const char *, size_t); - -/** - * Dencode `char *' source with `size_t' size. - * Returns a `unsigned char *' base64 decoded string + size of decoded string. - */ -unsigned char * -b64_decode_ex (const char *, size_t, size_t *); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/aesjni/src/main/cpp/check_emulator.c b/aesjni/src/main/cpp/check_emulator.c deleted file mode 100644 index 8ad151c..0000000 --- a/aesjni/src/main/cpp/check_emulator.c +++ /dev/null @@ -1,121 +0,0 @@ -// -// Created by wei on 17-12-4. -// -#include -#include "check_emulator.h" -#include "jni.h" -#include "logger.h" -#include - -/** - * - * @param pre - * @param str - * @return 1:yes 0:false - */ -int startsWith(const char *str,const char *pre) -{ - size_t lenpre = strlen(pre); - size_t lenstr = strlen(str); - int result = lenstr < lenpre ? -1 : strncmp(pre, str, lenpre); - if(result==0) - return 1; - else - return 0; -} - -int contains(const char *str,char *key){ - - char * re; - re=strstr(str,key);//第二个参数必须带* - if(re) - return 1; - else - return 0; -} - -int equals(const char *str,char * key){ - return strcmp(str,key)==0; -} - - - -JNIEXPORT jint JNICALL check_is_emulator(JNIEnv *env) { - - if(isdebug()==1)//debug模式不校验 - return 1; - -// 这里是模仿java代码的检查是否为模拟器 必要参数 context - - jclass clazz; - clazz = (*env)->FindClass(env,"android/os/Build"); - - //FINGERPRINT - jfieldID fid = (*env)->GetStaticFieldID(env, clazz, "FINGERPRINT", "Ljava/lang/String;"); - jstring fingerprintstr = (*env)->GetStaticObjectField(env, clazz, fid); - const char* fingerprintchars = (*env)->GetStringUTFChars(env, fingerprintstr, JNI_FALSE); - (*env)->ReleaseStringUTFChars(env,fingerprintstr,fingerprintchars); - //MODEL - jfieldID mid = (*env)->GetStaticFieldID(env, clazz, "MODEL", "Ljava/lang/String;"); - jstring modelstr = (*env)->GetStaticObjectField(env, clazz, mid); - const char* modelchars = (*env)->GetStringUTFChars(env, modelstr, JNI_FALSE); - (*env)->ReleaseStringUTFChars(env,modelstr,modelchars); - //MANUFACTURER - jfieldID manid = (*env)->GetStaticFieldID(env, clazz, "MANUFACTURER", "Ljava/lang/String;"); - jstring manufacturerstr = (*env)->GetStaticObjectField(env, clazz, manid); - const char* manufacturerchars = (*env)->GetStringUTFChars(env, manufacturerstr, JNI_FALSE); - (*env)->ReleaseStringUTFChars(env,manufacturerstr,manufacturerchars); - //PRODUCT - jfieldID pid = (*env)->GetStaticFieldID(env, clazz, "PRODUCT", "Ljava/lang/String;"); - jstring productstr = (*env)->GetStaticObjectField(env, clazz, pid); - const char* productchars = (*env)->GetStringUTFChars(env, productstr, JNI_FALSE); - (*env)->ReleaseStringUTFChars(env,productstr,productchars); - //BRAND - jfieldID bid = (*env)->GetStaticFieldID(env, clazz, "BRAND", "Ljava/lang/String;"); - jstring brandstr = (*env)->GetStaticObjectField(env, clazz, bid); - const char* brandchars = (*env)->GetStringUTFChars(env, brandstr, JNI_FALSE); - (*env)->ReleaseStringUTFChars(env,brandstr,brandchars); - //DEVICE - jfieldID did = (*env)->GetStaticFieldID(env, clazz, "DEVICE", "Ljava/lang/String;"); - jstring devicestr = (*env)->GetStaticObjectField(env, clazz, did); - const char* devicechars = (*env)->GetStringUTFChars(env, devicestr, JNI_FALSE); - (*env)->ReleaseStringUTFChars(env,devicestr,devicechars); - - - LOGI("fingerprintchars: %s",fingerprintchars); - LOGI("modelchars: %s",modelchars); - LOGI("manufacturerchars: %s",manufacturerchars); - LOGI("productchars: %s",productchars); - LOGI("brandchars: %s",brandchars); - if(contains(fingerprintchars,"Android") ){//是模拟器 不通过 - return 0; - } - if(contains(fingerprintchars,"unknown") ){//是模拟器 不通过 - return 0; - } - - if(contains(modelchars,"google_sdk")){ - return 0; - } - if(contains(modelchars,"Emulator")){ - return 0; - } - if(contains(modelchars,"Android SDK built for x86")){ - return 0; - } - - if(contains(manufacturerchars,"Genymotion")){ - return 0; - } - - if(contains(brandchars,"generic") && startsWith(devicechars,"generic")){ - return 0; - } - - if(contains(productchars,"google_sdk")){ - return 0; - } - - return 1; -} - diff --git a/aesjni/src/main/cpp/check_emulator.h b/aesjni/src/main/cpp/check_emulator.h deleted file mode 100644 index 9c56881..0000000 --- a/aesjni/src/main/cpp/check_emulator.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef CHECK_EMULATOR_IN_NDK_CHECK_EMULATOR_H -#define CHECK_EMULATOR_IN_NDK_CHECK_EMULATOR_H - -#include "jni.h" -#include "debugger.h" - - -/** - * - * @param env - * @param context - * @return 1:通过(非模拟器) else :不通过(是模拟器) - */ -JNIEXPORT jint JNICALL check_is_emulator(JNIEnv *env); - -#endif - - - diff --git a/aesjni/src/main/cpp/checksignature.c b/aesjni/src/main/cpp/checksignature.c deleted file mode 100644 index f41a147..0000000 --- a/aesjni/src/main/cpp/checksignature.c +++ /dev/null @@ -1,64 +0,0 @@ -// -// Created by wei on 16-12-4. -// - -#include -#include -#include -#include "checksignature.h" - - -jint check_signature(JNIEnv *env, jobject thiz, jobject context) { - //Context的类 - jclass context_clazz = (*env)->GetObjectClass(env, context); - // 得到 getPackageManager 方法的 ID - jmethodID methodID_getPackageManager = (*env)->GetMethodID(env, - context_clazz, "getPackageManager", - "()Landroid/content/pm/PackageManager;"); - - // 获得PackageManager对象 - jobject packageManager = (*env)->CallObjectMethod(env, context, - methodID_getPackageManager); -// // 获得 PackageManager 类 - jclass pm_clazz = (*env)->GetObjectClass(env, packageManager); - // 得到 getPackageInfo 方法的 ID - jmethodID methodID_pm = (*env)->GetMethodID(env, pm_clazz, "getPackageInfo", - "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;"); -// -// // 得到 getPackageName 方法的 ID - jmethodID methodID_pack = (*env)->GetMethodID(env, context_clazz, - "getPackageName", "()Ljava/lang/String;"); - - // 获得当前应用的包名 - jstring application_package = (*env)->CallObjectMethod(env, context, - methodID_pack); - const char *package_name = (*env)->GetStringUTFChars(env, - application_package, 0); - //LOGE("packageName: %s\n", package_name); - - // 获得PackageInfo - jobject packageInfo = (*env)->CallObjectMethod(env, packageManager, - methodID_pm, application_package, 64); - - jclass packageinfo_clazz = (*env)->GetObjectClass(env, packageInfo); - jfieldID fieldID_signatures = (*env)->GetFieldID(env, packageinfo_clazz, - "signatures", "[Landroid/content/pm/Signature;"); - jobjectArray signature_arr = (jobjectArray) (*env)->GetObjectField(env, - packageInfo, fieldID_signatures); - //Signature数组中取出第一个元素 - jobject signature = (*env)->GetObjectArrayElement(env, signature_arr, 0); - //读signature的hashcode - jclass signature_clazz = (*env)->GetObjectClass(env, signature); - jmethodID methodID_hashcode = (*env)->GetMethodID(env, signature_clazz, - "hashCode", "()I"); - jint hashCode = (*env)->CallIntMethod(env, signature, methodID_hashcode); - //LOGE("hashcode: %d\n", hashCode); - - if (strcmp(package_name, app_pkg_name) != 0) { - return -1; - } - if (hashCode != app_signature_hash_code) { - return -2; - } - return 1; -} \ No newline at end of file diff --git a/aesjni/src/main/cpp/checksignature.h b/aesjni/src/main/cpp/checksignature.h deleted file mode 100644 index 96d11ea..0000000 --- a/aesjni/src/main/cpp/checksignature.h +++ /dev/null @@ -1,26 +0,0 @@ -// -// Created by wei on 16-12-4. -// - -#define LOG_TAG "native_log" -//#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) - -#ifndef AESJNIENCRYPT_SIGNACTURECHECK_H -#define AESJNIENCRYPT_SIGNACTURECHECK_H - - -//合法的APP包名 -static const char *app_pkg_name = "com.androidyuan.aesjniencrypt"; - -//this value is generated by com.androidyuan.aesjni.SignatureTool.java. -//-625644214 is hash of keystore file. -static const int app_signature_hash_code = -625644214; - -/** - * 校验APP 包名和签名是否合法 - * - * 返回值为1 表示合法 - */ -jint check_signature(JNIEnv *env, jobject thiz, jobject context); - -#endif //AESJNIENCRYPT_SIGNACTURECHECK_H diff --git a/aesjni/src/main/cpp/debugger.c b/aesjni/src/main/cpp/debugger.c deleted file mode 100644 index e858a4a..0000000 --- a/aesjni/src/main/cpp/debugger.c +++ /dev/null @@ -1,24 +0,0 @@ -// -// Created by wei on 17-12-5. -// - -#include "debugger.h" -#include -#include "string.h" -#include "logger.h" - - - int isdebug(){ - -#ifdef NDK_DEBUG //android.mk 编译情况下 - LOGI("is NDK_DEBUG. \n"); - return 1; -#endif - -#ifdef CMAKE_BUILD_TYPE// cmakelist 编译情况下 - LOGI("is CMAKE_BUILD_TYPE. \n"); - return 1; -#endif - - return 0; -} \ No newline at end of file diff --git a/aesjni/src/main/cpp/debugger.h b/aesjni/src/main/cpp/debugger.h deleted file mode 100644 index b73ac90..0000000 --- a/aesjni/src/main/cpp/debugger.h +++ /dev/null @@ -1,15 +0,0 @@ -// Created by wei on 17-12-5. - -/** - * 通过依赖宏定义判断是否为DEBUG模式 - * 1.cmake 要使用define - * 2.makefile 模式下 需要 CFLAGS += -D - **/ - -#ifndef CHECK_EMULATOR_IN_NDK_DEBUGGER_H -#define CHECK_EMULATOR_IN_NDK_DEBUGGER_H - -// result 1: isdebug 0: not debug -int isdebug(); - -#endif \ No newline at end of file diff --git a/aesjni/src/main/cpp/logger.h b/aesjni/src/main/cpp/logger.h deleted file mode 100644 index 4a56ab8..0000000 --- a/aesjni/src/main/cpp/logger.h +++ /dev/null @@ -1,15 +0,0 @@ -// -// Created by wei on 17-12-27. -// - -#ifndef LOGGER -#define LOGGER - -#include -#define LOG_TAG "jni_log" -#define LOGW(...) __android_log_write(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__) -#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) -#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) - - -#endif diff --git a/aesjni/src/main/jni/TestAndroid.mk b/aesjni/src/main/jni/TestAndroid.mk index d23c759..aac7ed5 100755 --- a/aesjni/src/main/jni/TestAndroid.mk +++ b/aesjni/src/main/jni/TestAndroid.mk @@ -42,4 +42,4 @@ LOCAL_STATIC_LIBRARIES := sodium # tell ndk-build about the dependency LOCAL_LDLIBS := -llog #there is no need to specify path for liblog.so LOCAL_CPPFLAGS := -fexceptions -include $(BUILD_EXECUTABLE) # <-- Use this to build an executable. \ No newline at end of file +include $(BUILD_EXECUTABLE) # <-- Use this to build as executable file. \ No newline at end of file diff --git a/aesjni/src/main/jni/keys_generator.c b/aesjni/src/main/jni/keys_generator.c index 97ce89f..62aa15e 100644 --- a/aesjni/src/main/jni/keys_generator.c +++ b/aesjni/src/main/jni/keys_generator.c @@ -1,4 +1,4 @@ -#include +#include "keys_generator.h" void generate_chacah20_and_print() { From 5318b3790c4aff167e2acd6338cf7a3302c3ed24 Mon Sep 17 00:00:00 2001 From: bruce Date: Tue, 23 Feb 2021 18:54:48 +0800 Subject: [PATCH 16/21] [README]:{modify README.} --- README.md | 54 +++++++++++-------- README_zh.md | 52 +++++++++--------- .../com/androidyuan/aesjni/EncryptEntry.java | 5 +- aesjni/src/main/jni/JNIEncrypt.c | 2 + aesjni/src/main/jni/README.md | 2 - .../build_libsodium_for_all_android_abi.sh | 2 +- 6 files changed, 64 insertions(+), 53 deletions(-) delete mode 100755 aesjni/src/main/jni/README.md diff --git a/README.md b/README.md index d07b120..694b132 100644 --- a/README.md +++ b/README.md @@ -3,33 +3,41 @@ [中文](https://github.com/BruceWind/AESJniEncrypt/blob/master/README_zh.md) -# Reach high security in Android -- [x] ~~AES algorithms~~,it is low-performance,so it has been deprecated. -- [x] use chacha20 instead of AES. TLS1.3 used chacha20 on mobile device too,it is high-performance for arm. +# Reach high security with libsodium in Android +- [x] I have no longer supported ~~AES algorithms~~. If you still need it, check out tag: [v2.2](https://github.com/BruceWind/AESJniEncrypt/releases/tag/v2.2). +- [x] Use chacha20 instead of AES. TLS1.3 has been used **CHACHA20** on mobile device too.It is high-performance for ARM architecture. - [x] Hide native function in JniOnload - [x] Use signature verification to avoid being packaged again (It is prevents that hacker call your jni method directly.) -- [x] ~~key exists in the symbol table, and hides the character table~~ This scheme has been deprecated, [discard reason issues5](https://github.com/weizongwei5/AESJniEncrypt/issues/5), please see the next +- [x] ~~key exists in the symbol table, and hides the character table~~. This method has been deprecated due to [discard reason issues5](https://github.com/weizongwei5/AESJniEncrypt/issues/5) - [x] Get the key from a complex function, to hide the key, current function is a simple solution. (Complex solution: divide the Key into several pieces, store them in different C files, and finally splicing them together. This function should be complicated to write and increase the decompiling difficulty.) - [x] Use "obfuscator" to confuse C code, [how to deobfuscation?](https://blog.quarkslab.com/deobfuscation-recovering-an-ollvm-protected-program.html) - [x] Added support for x86 for obfucator. There is a link at the bottom of the tutorial for configuring obfucator. -- [x] Anti-debugging the so, the current code is a relatively simple solution, there are more complicated and more sophisticated solutions, such as: each time you perform encryption and decryption sign to determine whether it is traced, you want to write more complicated after your fork -- [x] Masking the simulator when the code is run: The code comes from my another repository [Check_Emulator_In_NDK](https://github.com/Scavenges/Check_Emulator_In_NDK) +- [x] Anti-debugging, the current code is a relatively simple solution, there are more complicated and more sophisticated solutions, such as: each time you perform encryption and decryption sign to determine whether it is traced, you want to write more complicated after your fork +- [x] Detect device is emulator in runtime : The code comes from my another repo [Check_Emulator_In_NDK](https://github.com/Scavenges/Check_Emulator_In_NDK) - [ ] TODO: Prevent SO file being code inject - -## Before you clone +## before you clone. install GIT-LFS: https://git-lfs.github.com/ -## Build & Integration +## build & run it. +1. preparation: + +run the shell : aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh +2. click run app from Android Studio. -a. Set ndk.dir in local.properties Requires that the ndk version must be 11-13b. High version ndk has not been tested, and may doesn't compiled. +## Integration +a. generating a chacha20 key: + +run `test_in_exexutaing.sh`, and look at logcat. It will generate key and nonce. You can paste it into **JNIEntry.c**. -b. Integrate into the project, please modify the class name and method name, dont expose the encryption algorithm, modify the key storage program into the code. +b. Set ndk.dir in local.properties. Some versions of ndk I have not tested. Maybe you will encounter build errors. -c. Generate and modify signatures. +c. Integrate into the project, please modify the class name and method name, dont expose the encryption algorithm, modify the key storage program into the code. -c.1.Generate keystore +d. Generate and modify signatures. + +d.1. Generate keystore ``` / / Then the current directory $ mkdir keystore @@ -50,7 +58,7 @@ c.2. Get the hash value of the current keystore and modify the package name and Base64 algorithm from: https://github.com/willemt/pearldb -AES128 Algorithm From: https://github.com/kokke/tiny-AES128-C +libsodium Algorithm From: https://github.com/jedisct1/libsodium Native code obfuscator: [obfuscation-o-llvm-ndk](https://fuzion24.github.io/android/obfuscation/ndk/llvm/o-llvm/2014/07/27/android-obfuscation-o-llvm-ndk) @@ -60,10 +68,11 @@ Native code obfuscator: [obfuscation-o-llvm-ndk](https://fuzion24.github.io/andr ![unconfused so](https://github.com/weizongwei5/AESJniEncrypt/raw/master/img/unobfscator_debugapk.png) ![confused so](https://github.com/weizongwei5/AESJniEncrypt/raw/master/img/obfscator_screen.png) -Contrast: So confused is three times the size before confusion. +Contrast: Confused SO file is three times the size before confusion. +If the size of SO file bother you, you can disable obfscator-lvvm. It is unnecessary. -### PS: -Because you need to do signature verification, i cann't provide jcenter dependencies, pls forgive me! +### In addition, I have to told you: +Because you need to do signature verification, I cann't provide jcenter dependencies, pls forgive me! Regardless of how secure the code, I still against that the key being stored in the code. @@ -76,7 +85,13 @@ This is my NDK configuration obfuscator tutorial: [Obfuscator-LLVM-4.0-BUILD-NDK If you want to ask me,please click [![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/askbruce/community). +If you encounter crash, you can look into **FigureOutJNICrash.md** to find where code crash at. + ------------------- +## Legal +If you live in China, you should take care about [checking apk signature](https://github.com/BruceWind/AESJniEncrypt/blob/master/aesjni/src/main/cpp/check_emulator.h#L15). +I have called PackageManger that may be misunderstood to collect list of installed apps. +You should look at [工信部整治八项违规](http://www.miit.gov.cn/n1146295/n7281315/c7507241/part/7507297.docx). ## Contributing @@ -92,8 +107,3 @@ In case you would like to add information to this repository or suggest some ide [https://github.com/zxp0505](https://github.com/zxp0505) [https://github.com/baoyongzhang](https://github.com/baoyongzhang) - - -## Legal -If you live in China, you should take care about [checking apk signature](https://github.com/BruceWind/AESJniEncrypt/blob/master/aesjni/src/main/cpp/check_emulator.c#L43). -I have called PackageManger ,it might be considered to read installed apps list,look at [工信部整治八项违规](http://www.miit.gov.cn/n1146295/n7281315/c7507241/part/7507297.docx). diff --git a/README_zh.md b/README_zh.md index 7e63063..aa0053b 100644 --- a/README_zh.md +++ b/README_zh.md @@ -4,27 +4,32 @@ # 追求极致的代码安全性保障 - [x] ~~ndk实现AES加密~~,性能不佳,已废弃此方式 -- [ ] 使用chacha20加密,TLS1.3在移动端都用了chacha20了,性能更好 -- [x] 使用JniOnload 隐藏c函数 +- [ ] 使用chacha20加密,TLS1.3在移动端都用了chacha20了,性能对ARM架构CPU更好。 +- [x] 使用JniOnload 隐藏C函数 - [x] 使用签名校验避免被再次打包(这是绕过破解加密算法直接调用你的jni函数) -- [x] ~~key存在符号表中,同时隐藏字符表~~ 该方案已经废弃,[废弃原因issues5](https://github.com/weizongwei5/AESJniEncrypt/issues/5),请看下一条 +- [x] ~~key存在符号表中,同时隐藏字符表~~ 该方案已经废弃,[废弃原因issues5](https://github.com/weizongwei5/AESJniEncrypt/issues/5) - [x] 手工处理隐藏key,最复杂的方案:将密钥分成不同的几段,存储在不同的代码中,最后将他们拼接起来,可以将整个操作写的很复杂,增加逆向难度。(目前代码里用的是稍微简单的方案) - [x] 使用obfuscator混淆C的代码,[关于破解obfuscator](https://blog.quarkslab.com/deobfuscation-recovering-an-ollvm-protected-program.html) - [x] 增加obfucator对x86的支持,具体配置obfucator的教程底部有链接。 - [x] 反动态调试 , 目前代码里是比较简单的方案, 有更复杂更高明的方案,比如:每次执行加密解密签先去判断是否被trace,想要更复杂的自己fork之后去写 -- [x] 代码run的时候屏蔽模拟器 :代码来自我的另外一个仓库[Check_Emulator_In_NDK](https://github.com/Scavenges/Check_Emulator_In_NDK) +- [x] 代码运行时屏蔽模拟器 :代码来自我的另外一个仓库[Check_Emulator_In_NDK](https://github.com/Scavenges/Check_Emulator_In_NDK) - [ ] TODO:防止so代码被code inject -## 克隆代码准备 -先安装GIT-LFS: -https://git-lfs.github.com/ - +## 尝试编译,并跑起来 +1.准备: +运行这个shell : aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh +2.打开AS运行app,并查看日志。 ## 集成 +先安装GIT-LFS:https://git-lfs.github.com/ a.先配置local.properties中ndk.dir 要求使用ndk版本必须11-13b,新版本ndk没有测试过,或许不能编译通过。 b.集成到项目中请修改类名方法名,不要暴露加密算法,自行修改key存储到代码里的方案. +b.1. 生成 chacha20 key: + +run `test_in_exexutaing.sh`,然后请看logcat. 随机生成的key & nonce会显示出来. 你需要粘贴到 **JNIEntry.c**. + c.生成和修改签名. **c.1.生成** @@ -38,8 +43,6 @@ $ keytool -genkey -alias client1 -keypass 123456 -keyalg RSA -keysize 1024 -vali ... ... - - ``` **c.2.取得当前keystore的hash值,并修改native代码中的包名和hash** @@ -50,18 +53,10 @@ $ keytool -genkey -alias client1 -keypass 123456 -keyalg RSA -keysize 1024 -vali ## 鸣谢 Base64 算法 来自:https://github.com/willemt/pearldb - -AES128 算法 来自:https://github.com/kokke/tiny-AES128-C - +Libsodium 算法 来自:https://github.com/jedisct1/libsodium Native代码混淆器:[obfuscation-o-llvm-ndk](https://fuzion24.github.io/android/obfuscation/ndk/llvm/o-llvm/2014/07/27/android-obfuscation-o-llvm-ndk) -### 贡献者 - -[https://github.com/larry19840909](https://github.com/larry19840909) -[https://github.com/zxp0505](https://github.com/zxp0505) - -[https://github.com/baoyongzhang](https://github.com/baoyongzhang) @@ -72,11 +67,14 @@ Native代码混淆器:[obfuscation-o-llvm-ndk](https://fuzion24.github.io/andr 对比: 混淆后的so是混淆前的三倍大小。 -### PS: +如果SO文件大小对您的项目有影响,你可以选择停用SO扰乱的,因为还有其他安全检查。 + +### 补充: 因为需要做签名校验,所以无法提供jcenter依赖了,望见谅!! 不管代码安全性多高,我依旧反对key存到代码里。 +有问题及时提:[new issues](https://github.com/weizongwei5/AESJniEncrypt/issues/new) 想要编译出混淆过native代码的so需要修改aesjni/build.gradle文件中的externalNativeBuild,并配置NDK下的Obfuscator-LLVM。 @@ -84,14 +82,20 @@ Native代码混淆器:[obfuscation-o-llvm-ndk](https://fuzion24.github.io/andr [其他语言怎么配合加解密?](https://github.com/weizongwei5/AESJniEncrypt/issues/8) -如果要直接问我,请点这里[![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/askbruce/community). - +如果你遇到了崩溃,请看**FigureOutJNICrash.md**,这个是个so崩溃定位的教程。 ------------------- -有问题及时提:[new issues](https://github.com/weizongwei5/AESJniEncrypt/issues/new) - ## 合规 如果你生活在中国,请注意[工信部整治八项违规](http://www.miit.gov.cn/n1146295/n7281315/c7507241/part/7507297.docx). 我调用了PackageManger[检查签名](https://github.com/BruceWind/AESJniEncrypt/blob/master/aesjni/src/main/cpp/check_emulator.c#L43),我只是**读取当前安装的app**, 这可能被认为**收集了安装列表**。从规定上来讲并不违规,只是读取了,并没**收集**,收集是违规的,读取是合规的。 只是目前有可能被误认为**收集**。 + + +### 贡献者 + +[https://github.com/larry19840909](https://github.com/larry19840909) + +[https://github.com/zxp0505](https://github.com/zxp0505) + +[https://github.com/baoyongzhang](https://github.com/baoyongzhang) \ No newline at end of file diff --git a/aesjni/src/main/java/com/androidyuan/aesjni/EncryptEntry.java b/aesjni/src/main/java/com/androidyuan/aesjni/EncryptEntry.java index ec64715..2d1c0a5 100644 --- a/aesjni/src/main/java/com/androidyuan/aesjni/EncryptEntry.java +++ b/aesjni/src/main/java/com/androidyuan/aesjni/EncryptEntry.java @@ -2,14 +2,11 @@ /** * Created by wei on 16-12-2. - * - * 秘文的key 在 C 的代码,如需修改请先修改JNIEncrypt.c - * 另外native做了签名校验 + * key is stored in JNIEncrypt.c, you may need to change it. * * @Date: 21-02-22: * AES algorithm has been instead with chacha20. * And algorithm is written by libsodium. - * */ public class EncryptEntry { diff --git a/aesjni/src/main/jni/JNIEncrypt.c b/aesjni/src/main/jni/JNIEncrypt.c index 0313d9b..ada0fb2 100755 --- a/aesjni/src/main/jni/JNIEncrypt.c +++ b/aesjni/src/main/jni/JNIEncrypt.c @@ -56,6 +56,7 @@ unsigned char *getNonce() //__attribute__((section (".mytext"))) unsigned char *getKey() { + //TODO I need to split this string. const char *key_hex = "9876c42f2f61bee24cc27ebd6155897c46950a83c9b0cc95a9650f9ae7421d07"; const unsigned char *KEY = chacha20_hexkey2bin(key_hex); return KEY; @@ -63,6 +64,7 @@ unsigned char *getKey() JNIEXPORT jstring JNICALL encode(JNIEnv *env, jobject instance, jobject context, jstring str_) { + //TODO sodium_init(); //firstly, detect the apk is repackaged. if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) diff --git a/aesjni/src/main/jni/README.md b/aesjni/src/main/jni/README.md deleted file mode 100755 index ec81c14..0000000 --- a/aesjni/src/main/jni/README.md +++ /dev/null @@ -1,2 +0,0 @@ -## 2019年 05月 24日 星期五 14:29:34 CST : -sync from cpp. \ No newline at end of file diff --git a/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh b/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh index 48f33d2..b2bd524 100755 --- a/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh +++ b/aesjni/src/main/jni/build_libsodium_for_all_android_abi.sh @@ -9,7 +9,7 @@ # Some error might be thrown. You may need to install certain applications # (such as python, libtool-bin) to resolve those errors. -LIBSODIUM_GIT_TAG=1.0.18 # it is the last stable version. +LIBSODIUM_GIT_TAG=1.0.18 # it is the last stable version that I have tested. #ROOT_DIR=$(pwd) From bbea7574ac07a9051f8e059586dd9d721b23d707 Mon Sep 17 00:00:00 2001 From: hide Date: Tue, 23 Feb 2021 23:45:02 +0800 Subject: [PATCH 17/21] [memory]:{recycle memory.} --- .../java/com/androidyuan/aesjni/JNITest.java | 41 ++++- aesjni/src/main/jni/JNIEncrypt.c | 141 ++++++++++-------- .../aesjniencrypt/MainActivity.java | 20 ++- 3 files changed, 123 insertions(+), 79 deletions(-) diff --git a/aesjni/src/androidTest/java/com/androidyuan/aesjni/JNITest.java b/aesjni/src/androidTest/java/com/androidyuan/aesjni/JNITest.java index 0b25df8..561b026 100644 --- a/aesjni/src/androidTest/java/com/androidyuan/aesjni/JNITest.java +++ b/aesjni/src/androidTest/java/com/androidyuan/aesjni/JNITest.java @@ -1,10 +1,14 @@ package com.androidyuan.aesjni; +import android.content.Context; +import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; +import java.util.concurrent.atomic.AtomicBoolean; + import static org.junit.Assert.assertEquals; /** @@ -21,13 +25,34 @@ public class JNITest { public void useAppContext() throws Exception { // Context of the app under test. -// Context appContext = InstrumentationRegistry.getTargetContext(); -// assertEquals(EncryptEntry.checkSignature(appContext),1); - - //plain:"123abcABC&*(@#@#@)+_/中文测试" - final String code = EncryptEntry.encode(this, PLAIN); - assertEquals(code,ENCODE_STR); - final String decode = EncryptEntry.decode(this, ENCODE_STR); - assertEquals(PLAIN,decode); + final Context appContext = InstrumentationRegistry.getTargetContext(); + assertEquals(EncryptEntry.checkSignature(appContext), 1); + + final AtomicBoolean atomicBoolean = new AtomicBoolean(); + atomicBoolean.set(false); + + + new Thread(new Runnable() { + @Override + public void run() { + for (int i = 0; i < 1000 * 1000; i++) { + + + //plain:"123abcABC&*(@#@#@)+_/中文测试" + final String code = EncryptEntry.encode(appContext, PLAIN); + assertEquals(code, ENCODE_STR); + final String decode = EncryptEntry.decode(appContext, ENCODE_STR); + assertEquals(PLAIN, decode); + + } + atomicBoolean.set(true); + } + }).start(); + + while (true) { + if (atomicBoolean.get()) { + break; + } + } } } diff --git a/aesjni/src/main/jni/JNIEncrypt.c b/aesjni/src/main/jni/JNIEncrypt.c index ada0fb2..ba5f1a8 100755 --- a/aesjni/src/main/jni/JNIEncrypt.c +++ b/aesjni/src/main/jni/JNIEncrypt.c @@ -13,10 +13,17 @@ // specific your Java class which jni entry. #define JNIREG_CLASS "com/androidyuan/aesjni/EncryptEntry" -const char *WRONG_SIGNATURE = "WRONG_SIGNATURE"; +static const char *WRONG_SIGNATURE = "WRONG_SIGNATURE"; -jstring char2jstring(JNIEnv *envPtr, char *src) -{ + +//TODO I need to split this string. +static const char *nonce_hex = "611dec2f53524315"; +static const char *key_hex = "9876c42f2f61bee24cc27ebd6155897c46950a83c9b0cc95a9650f9ae7421d07"; + +static unsigned char *key = NULL; +static unsigned char *nonce = NULL; + +jstring char2jstring(JNIEnv *envPtr, char *src) { JNIEnv env = *envPtr; jsize len = strlen(src); @@ -25,97 +32,97 @@ jstring char2jstring(JNIEnv *envPtr, char *src) jmethodID mid = env->GetMethodID(envPtr, clsstring, "", "([BLjava/lang/String;)V"); jbyteArray barr = env->NewByteArray(envPtr, len); - env->SetByteArrayRegion(envPtr, barr, 0, len, (jbyte *)src); + env->SetByteArrayRegion(envPtr, barr, 0, len, (jbyte *) src); - return (jstring)env->NewObject(envPtr, clsstring, mid, barr, strencode); + return (jstring) env->NewObject(envPtr, clsstring, mid, barr, strencode); } -jstring char2string_with_len(JNIEnv *envPtr, char *src, size_t str_len) -{ +jstring char2string_with_len(JNIEnv *envPtr, char *src, size_t str_len) { JNIEnv env = *envPtr; - jsize len = (jsize)str_len; + jsize len = (jsize) str_len; jclass clsstring = env->FindClass(envPtr, "java/lang/String"); jstring strencode = env->NewStringUTF(envPtr, "UTF-8"); jmethodID mid = env->GetMethodID(envPtr, clsstring, "", "([BLjava/lang/String;)V"); jbyteArray barr = env->NewByteArray(envPtr, len); - env->SetByteArrayRegion(envPtr, barr, 0, len, (jbyte *)src); + env->SetByteArrayRegion(envPtr, barr, 0, len, (jbyte *) src); - return (jstring)env->NewObject(envPtr, clsstring, mid, barr, strencode); + return (jstring) env->NewObject(envPtr, clsstring, mid, barr, strencode); } -unsigned char *getNonce() -{ - const char *nonce_hex = "611dec2f53524315"; - const unsigned char *NONCE = chacha20_hexnonce2bin(nonce_hex); - return NONCE; +unsigned char *getNonce() { + if (nonce == NULL) { + nonce = chacha20_hexnonce2bin(nonce_hex); + } + return nonce; } //hiding string table, it not work for my hack method, it just is a low-level defende way. //__attribute__((section (".mytext"))) -unsigned char *getKey() -{ - //TODO I need to split this string. - const char *key_hex = "9876c42f2f61bee24cc27ebd6155897c46950a83c9b0cc95a9650f9ae7421d07"; - const unsigned char *KEY = chacha20_hexkey2bin(key_hex); - return KEY; +unsigned char *getKey() { + if (key == NULL) { + key = chacha20_hexkey2bin(key_hex); + } + return key; } -JNIEXPORT jstring JNICALL encode(JNIEnv *env, jobject instance, jobject context, jstring str_) -{ - //TODO - sodium_init(); +JNIEXPORT jstring JNICALL encode(JNIEnv *env, jobject instance, jobject context, jstring str_) { //firstly, detect the apk is repackaged. - if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) - { - char *str = WRONG_SIGNATURE; - return char2jstring(env, str); - } +// if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) { +// char *str = WRONG_SIGNATURE; +// return char2jstring(env, str); +// } + const char *plain_str = (*env)->GetStringUTFChars(env, str_, JNI_FALSE); (*env)->ReleaseStringUTFChars(env, str_, plain_str); unsigned char *ciphertext; - ciphertext = (unsigned char *)sodium_malloc(strlen(plain_str) + crypto_aead_chacha20poly1305_ABYTES); + ciphertext = (unsigned char *) sodium_malloc( + strlen(plain_str) + crypto_aead_chacha20poly1305_ABYTES); unsigned long long ciphertext_len; crypto_aead_chacha20poly1305_encrypt(ciphertext, &ciphertext_len, plain_str, strlen(plain_str), NULL, 0, //additional data is NULL, you can change it. NULL, getNonce(), getKey()); - if (ciphertext_len == 0) - { + + if (ciphertext_len == 0) { abort(); } - char *result_hex = (char *)sodium_malloc(2 * ciphertext_len + 1); //return hex is easy to transport in internet. - sodium_bin2hex(result_hex, (size_t)(2 * ciphertext_len + 1), ciphertext, ciphertext_len); - return (*env)->NewStringUTF(env, result_hex); + + char *result_hex = (char *) sodium_malloc( + 2 * ciphertext_len + 1); //return hex is easy to transport in internet. + sodium_bin2hex(result_hex, (size_t) (2 * ciphertext_len + 1), ciphertext, ciphertext_len); + sodium_free(ciphertext); + jstring result = (*env)->NewStringUTF(env, result_hex); + sodium_free(result_hex); + return result; } -JNIEXPORT jstring JNICALL decode(JNIEnv *env, jobject instance, jobject context, jstring str_) -{ +JNIEXPORT jstring JNICALL decode(JNIEnv *env, jobject instance, jobject context, jstring str_) { //security checking. - if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) - { - char *str = WRONG_SIGNATURE; - return char2jstring(env, str); - } +// if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) { +// char *str = WRONG_SIGNATURE; +// return char2jstring(env, str); +// } //str_ must is hex. const char *hex_str = (*env)->GetStringUTFChars(env, str_, JNI_FALSE); (*env)->ReleaseStringUTFChars(env, str_, hex_str); int encrypt_len = strlen(hex_str) / 2; - unsigned char *encrypted_str = (unsigned char *)sodium_malloc(encrypt_len); + unsigned char *encrypted_str = (unsigned char *) sodium_malloc(encrypt_len); sodium_hex2bin(encrypted_str, encrypt_len, hex_str, strlen(hex_str), NULL, NULL, NULL); + unsigned char *decrypted; - decrypted = (unsigned char *)sodium_malloc(encrypt_len); + decrypted = (unsigned char *) sodium_malloc(encrypt_len); unsigned long long decrypted_len; crypto_aead_chacha20poly1305_decrypt(decrypted, &decrypted_len, NULL, @@ -123,68 +130,70 @@ JNIEXPORT jstring JNICALL decode(JNIEnv *env, jobject instance, jobject context, NULL, 0, getNonce(), getKey()); + sodium_free(encrypted_str); + //It cant use NewStringUTF, if decrypted is Garbled code ,use NewStringUTF will throw exception. - return char2string_with_len(env, decrypted, (size_t)decrypted_len); //decrypted doesnt has '\0',so I put decrypted_len. + jstring result = char2string_with_len(env, decrypted, + (size_t) decrypted_len); //decrypted doesnt has '\0',so I put decrypted_len. + + sodium_free(decrypted); + return result; } /** * if rerurn 1 ,is check pass. */ JNIEXPORT jint JNICALL -check_jni(JNIEnv *env, jobject instance, jobject con) -{ +check_jni(JNIEnv *env, jobject instance, jobject con) { return check_signature(env, instance, con); } // Java和JNI函数的绑定表 static JNINativeMethod method_table[] = { - {"checkSignature", "(Ljava/lang/Object;)I", (void *)check_jni}, - {"decode", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/String;", (void *)decode}, - {"encode", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/String;", (void *)encode}, + {"checkSignature", "(Ljava/lang/Object;)I", (void *) check_jni}, + {"decode", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/String;", (void *) decode}, + {"encode", "(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/String;", (void *) encode}, }; // 注册native方法到java中 static int registerNativeMethods(JNIEnv *env, const char *className, - JNINativeMethod *gMethods, int numMethods) -{ + JNINativeMethod *gMethods, int numMethods) { jclass clazz; clazz = (*env)->FindClass(env, className); - if (clazz == NULL) - { + if (clazz == NULL) { return JNI_FALSE; } - if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) - { + if ((*env)->RegisterNatives(env, clazz, gMethods, numMethods) < 0) { return JNI_FALSE; } return JNI_TRUE; } -int register_ndk_load(JNIEnv *env) -{ +int register_ndk_load(JNIEnv *env) { // 调用注册方法 return registerNativeMethods(env, JNIREG_CLASS, method_table, NELEM(method_table)); } -JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) -{ +JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { - ptrace(PTRACE_TRACEME, 0, 0, 0); //反调试 - //这是一种比较简单的防止被调试的方案 + ptrace(PTRACE_TRACEME, 0, 0, 0); //anti-debug + //this is low level anti-debug method. // 有更复杂更高明的方案,比如:不用这个ptrace而是每次执行加密解密签先去判断是否被trace,目前的版本不做更多的负载方案,您想做可以fork之后,自己去做 JNIEnv *env = NULL; jint result = -1; - if ((*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_4) != JNI_OK) - { + //this line is important to init libsodium. + sodium_init(); + + + if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_4) != JNI_OK) { return result; } register_ndk_load(env); - // 返回jni的版本 return JNI_VERSION_1_4; } diff --git a/app/src/main/java/com/androidyuan/aesjniencrypt/MainActivity.java b/app/src/main/java/com/androidyuan/aesjniencrypt/MainActivity.java index 2556820..bd7ed7f 100644 --- a/app/src/main/java/com/androidyuan/aesjniencrypt/MainActivity.java +++ b/app/src/main/java/com/androidyuan/aesjniencrypt/MainActivity.java @@ -18,11 +18,21 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - //下面的密文对应的原文:123abcABC&*(@#@#@)+_/中文测试 - final String code = EncryptEntry.encode(this, str); - Log.i("code", code + ""); - final String decode = EncryptEntry.decode(this, encodeStr); - Log.i("decode", decode + ""); + new Thread(new Runnable() { + @Override + public void run() { + for (int i = 0; i < 1000 * 1000; i++) { + //下面的密文对应的原文:123abcABC&*(@#@#@)+_/中文测试 + final String code = EncryptEntry.encode(MainActivity.this.getApplicationContext(), str); + if (i > 1000 * 990 || i < 100) Log.i("code", code + ""); + final String decode = EncryptEntry.decode(MainActivity.this.getApplicationContext(), encodeStr); + if (i > 1000 * 990 || i < 100) Log.i("decode " + i, decode + ""); + + } + + } + }).start(); + } From dde6b35c545117eaf47293a43b18b2727c9a4a18 Mon Sep 17 00:00:00 2001 From: bruce Date: Wed, 24 Feb 2021 14:57:25 +0800 Subject: [PATCH 18/21] [fix]:{debug mode in debugger.h has not worked.} --- FigureOutJNICrash.md | 6 ++-- aesjni/CMakeLists.txt | 10 +++++- aesjni/build.gradle | 4 +-- aesjni/src/main/jni/BaseAndroid.mk | 36 ++++++++++++++++++++ aesjni/src/main/jni/OriginAndroid_r13_mac.mk | 8 ++++- aesjni/src/main/jni/debugger.c | 15 -------- aesjni/src/main/jni/debugger.h | 2 +- aesjni/src/main/jni/debugger_test.c | 17 +++++++++ 8 files changed, 76 insertions(+), 22 deletions(-) create mode 100644 aesjni/src/main/jni/BaseAndroid.mk create mode 100755 aesjni/src/main/jni/debugger_test.c diff --git a/FigureOutJNICrash.md b/FigureOutJNICrash.md index a7be0b3..6aef94e 100644 --- a/FigureOutJNICrash.md +++ b/FigureOutJNICrash.md @@ -4,9 +4,11 @@ How to locate where native crash at? 2.run **ndk-stack** to analyze logcat and **symbol file** to find where crash printed. ``` -//or you use gradlew build command: +//gradlew build with ndkBuild: adb logcat | ndk-stack -sym ./aesjni/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/objs-debug/ -//or you using ndk-build command: +//gradlew build with CMake: +adb logcat | ndk-stack -sym ./aesjni/build/intermediates/cmake/debug/obj/arm64-v8a +//if you run ndk-build command: adb logcat | ndk-stack -sym ./aesjni/src/main/obj/local/x86/ ``` > **../obj/local/x86/** and **../obj/local/armeabi-v7a/** is symbol file directory. diff --git a/aesjni/CMakeLists.txt b/aesjni/CMakeLists.txt index 051c74a..c4bb8c7 100644 --- a/aesjni/CMakeLists.txt +++ b/aesjni/CMakeLists.txt @@ -20,6 +20,7 @@ else () message(FATAL_ERROR "${ANDROID_ABI} static libraries might doesn't exist.") endif () + set(distribution_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jni/sodium_include/libsodium-android-${ARCH_PREFIX}) message(STATUS "Current build include : ${distribution_DIR} ") @@ -30,6 +31,13 @@ set_target_properties(libsodium PROPERTIES IMPORTED_LOCATION include_directories(${distribution_DIR}/include) # -------configure import libsodium end------ + +set(DEBUG_FILE "src/main/jni/debugger.c") + +if (${CMAKE_BUILD_TYPE} STREQUAL "Debug") + set(DEBUG_FILE "src/main/jni/debugger_test.c") +endif () + add_library( # Sets the name of the library. JNIEncrypt @@ -42,7 +50,7 @@ add_library( # Sets the name of the library. src/main/jni/check_emulator.h src/main/jni/check_emulator.c src/main/jni/debugger.h - src/main/jni/debugger.c + ${DEBUG_FILE} src/main/jni/logger.h src/main/jni/keys_generator.h src/main/jni/keys_generator.c diff --git a/aesjni/build.gradle b/aesjni/build.gradle index 8981eea..817e49f 100644 --- a/aesjni/build.gradle +++ b/aesjni/build.gradle @@ -36,7 +36,7 @@ android { } } - def USING_NDK = false // if you want to build with CMakelist,set false. + def USING_NDK = true // if you want to build with CMakelist,set false. externalNativeBuild { if (USING_NDK) { @@ -50,7 +50,7 @@ android { // path file("src/main/jni/OriginAndroid_r16_ubuntu.mk") } } else { - //selection 2 the selection doesn't obfuscate native code **** i don't recommand using cmake***** + //selection 2 the selection doesn't obfuscate native code **** i don't recommend using cmake***** cmake { path "CMakeLists.txt" } diff --git a/aesjni/src/main/jni/BaseAndroid.mk b/aesjni/src/main/jni/BaseAndroid.mk new file mode 100644 index 0000000..7a12e74 --- /dev/null +++ b/aesjni/src/main/jni/BaseAndroid.mk @@ -0,0 +1,36 @@ +# ========== static sodium begin ================== +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) + ARCH_PREFIX := armv7-a +else ifeq ($(TARGET_ARCH_ABI),armeabi) + ARCH_PREFIX := armv6 +else ifeq ($(TARGET_ARCH_ABI),x86) + ARCH_PREFIX := i686 +else ifeq ($(TARGET_ARCH_ABI),mips) + ARCH_PREFIX := mips32 +else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) + ARCH_PREFIX := armv8-a +else + ARCH_PREFIX := $(TARGET_ARCH) +endif + +SODIUM_LIB_DIR := $(LOCAL_PATH)/sodium_include/libsodium-android-$(ARCH_PREFIX) +SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include +SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a +LOCAL_MODULE:= sodium +LOCAL_SRC_FILES:= $(SODIUM_LIB) +LOCAL_EXPORT_C_INCLUDES := $(SODIUM_INC_DIR) $(SODIUM_INC_DIR)/sodium +include $(PREBUILT_STATIC_LIBRARY) + +# ========== static sodium end ================== + +include $(CLEAR_VARS) + +DEBUG_FILE:=debugger.c +ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) + DEBUG_FILE := debugger_test.c +endif + diff --git a/aesjni/src/main/jni/OriginAndroid_r13_mac.mk b/aesjni/src/main/jni/OriginAndroid_r13_mac.mk index bae67c0..703ed7a 100755 --- a/aesjni/src/main/jni/OriginAndroid_r13_mac.mk +++ b/aesjni/src/main/jni/OriginAndroid_r13_mac.mk @@ -29,6 +29,12 @@ include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) +DEBUG_FILE:=debugger.c +ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) + DEBUG_FILE := debugger_test.c +endif + + LOCAL_MODULE := JNIEncrypt LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ base64.c \ @@ -36,7 +42,7 @@ LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ str_utils.cpp \ checksignature.c \ check_emulator.c \ - debugger.c \ + $(DEBUG_FILE) \ logger.h LOCAL_CFLAGS += -Wall -g -pedantic -std=c99 diff --git a/aesjni/src/main/jni/debugger.c b/aesjni/src/main/jni/debugger.c index c496d06..bfbd91a 100755 --- a/aesjni/src/main/jni/debugger.c +++ b/aesjni/src/main/jni/debugger.c @@ -17,20 +17,5 @@ int is_debug() { - -#ifdef MY_DEBUG //build with android.mk - LOGI("MY_DEBUG : %d",MY_DEBUG); - if(MY_DEBUG==1){ - return 1; - } -#endif -LOGD("MY_DEBUG not define."); - -#ifdef CMAKE_BUILD_TYPE //building with Cmakelist - LOGI("is CMAKE_BUILD_TYPE. \n"); - return 1; -#endif -LOGD("CMAKE_BUILD_TYPE not define."); - return 0; } diff --git a/aesjni/src/main/jni/debugger.h b/aesjni/src/main/jni/debugger.h index b051484..78dd4fb 100755 --- a/aesjni/src/main/jni/debugger.h +++ b/aesjni/src/main/jni/debugger.h @@ -9,7 +9,7 @@ #ifndef CHECK_EMULATOR_IN_NDK_DEBUGGER_H #define CHECK_EMULATOR_IN_NDK_DEBUGGER_H -// result 1: isdebug 0: not debug +// result 1: isdebug 0: release int is_debug(); #endif diff --git a/aesjni/src/main/jni/debugger_test.c b/aesjni/src/main/jni/debugger_test.c new file mode 100755 index 0000000..4b8f2a5 --- /dev/null +++ b/aesjni/src/main/jni/debugger_test.c @@ -0,0 +1,17 @@ +// +// Created by wei on 17-12-5. +// this file is used to avoid security checking. +// + +#include "debugger.h" +#include +#include "string.h" +#include "logger.h" + + +/** + * always return 1, + */ +int is_debug() { + return 1; +} From 6985142efdff9d24e179d6fe10bbfa9205640193 Mon Sep 17 00:00:00 2001 From: bruce Date: Wed, 24 Feb 2021 15:44:51 +0800 Subject: [PATCH 19/21] [refactor]:{refactor native build mk file.} --- aesjni/CMakeLists.txt | 6 +-- aesjni/build.gradle | 4 +- aesjni/src/main/jni/BaseAndroid.mk | 18 +++++-- aesjni/src/main/jni/OriginAndroid.mk | 50 ------------------- aesjni/src/main/jni/OriginAndroid_r13_mac.mk | 44 +--------------- aesjni/src/main/jni/OriginAndroid_r16_mac.mk | 39 +-------------- .../src/main/jni/OriginAndroid_r16_ubuntu.mk | 31 +----------- aesjni/src/main/jni/OriginAndroid_r22_mac.mk | 39 +-------------- aesjni/src/main/jni/debugger.c | 7 --- 9 files changed, 25 insertions(+), 213 deletions(-) delete mode 100755 aesjni/src/main/jni/OriginAndroid.mk diff --git a/aesjni/CMakeLists.txt b/aesjni/CMakeLists.txt index c4bb8c7..0d5137d 100644 --- a/aesjni/CMakeLists.txt +++ b/aesjni/CMakeLists.txt @@ -32,10 +32,10 @@ include_directories(${distribution_DIR}/include) # -------configure import libsodium end------ -set(DEBUG_FILE "src/main/jni/debugger.c") +set(DEBUG_FILE_C "src/main/jni/debugger.c") if (${CMAKE_BUILD_TYPE} STREQUAL "Debug") - set(DEBUG_FILE "src/main/jni/debugger_test.c") + set(DEBUG_FILE_C "src/main/jni/debugger_test.c") endif () add_library( # Sets the name of the library. @@ -50,7 +50,7 @@ add_library( # Sets the name of the library. src/main/jni/check_emulator.h src/main/jni/check_emulator.c src/main/jni/debugger.h - ${DEBUG_FILE} + ${DEBUG_FILE_C} src/main/jni/logger.h src/main/jni/keys_generator.h src/main/jni/keys_generator.c diff --git a/aesjni/build.gradle b/aesjni/build.gradle index 817e49f..bdb623e 100644 --- a/aesjni/build.gradle +++ b/aesjni/build.gradle @@ -44,9 +44,9 @@ android { ndkBuild { // path file("src/main/jni/Android.mk") // path file("src/main/jni/OriginAndroid.mk") - path file("src/main/jni/OriginAndroid_r13_mac.mk") +// path file("src/main/jni/OriginAndroid_r13_mac.mk") // path file("src/main/jni/OriginAndroid_r16_mac.mk") -// path file("src/main/jni/OriginAndroid_r22_mac.mk") + path file("src/main/jni/OriginAndroid_r22_mac.mk") // path file("src/main/jni/OriginAndroid_r16_ubuntu.mk") } } else { diff --git a/aesjni/src/main/jni/BaseAndroid.mk b/aesjni/src/main/jni/BaseAndroid.mk index 7a12e74..d047104 100644 --- a/aesjni/src/main/jni/BaseAndroid.mk +++ b/aesjni/src/main/jni/BaseAndroid.mk @@ -29,8 +29,20 @@ include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS) -DEBUG_FILE:=debugger.c -ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) - DEBUG_FILE := debugger_test.c +ifeq ($(NDK_DEBUG),1) + DEBUG_FILE_C:=debugger_test.c +else + DEBUG_FILE_C:=debugger.c endif +LOCAL_MODULE := JNIEncrypt +LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ + base64.c \ + keys_generator.c \ + str_utils.cpp \ + checksignature.c \ + check_emulator.c \ + $(DEBUG_FILE_C) \ + logger.h + + diff --git a/aesjni/src/main/jni/OriginAndroid.mk b/aesjni/src/main/jni/OriginAndroid.mk deleted file mode 100755 index b9b1fd2..0000000 --- a/aesjni/src/main/jni/OriginAndroid.mk +++ /dev/null @@ -1,50 +0,0 @@ -# ========== static sodium begin ================== -LOCAL_PATH := $(call my-dir) - -include $(CLEAR_VARS) - -ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) - ARCH_PREFIX := armv7-a -else ifeq ($(TARGET_ARCH_ABI),armeabi) - ARCH_PREFIX := armv6 -else ifeq ($(TARGET_ARCH_ABI),x86) - ARCH_PREFIX := i686 -else ifeq ($(TARGET_ARCH_ABI),mips) - ARCH_PREFIX := mips32 -else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) - ARCH_PREFIX := armv8-a -else - ARCH_PREFIX := $(TARGET_ARCH) -endif - -SODIUM_LIB_DIR := $(LOCAL_PATH)/sodium_include/libsodium-android-$(ARCH_PREFIX) -SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include -SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a -LOCAL_MODULE:= sodium -LOCAL_SRC_FILES:= $(SODIUM_LIB) -LOCAL_EXPORT_C_INCLUDES := $(SODIUM_INC_DIR) $(SODIUM_INC_DIR)/sodium -include $(PREBUILT_STATIC_LIBRARY) - -# ========== static sodium end ================== - -include $(CLEAR_VARS) - -LOCAL_MODULE := JNIEncrypt -LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ - base64.c \ - keys_generator.c \ - str_utils.cpp \ - checksignature.c \ - check_emulator.c \ - debugger.c \ - logger.h -LOCAL_CFLAGS += -Wall -g -pedantic - -# if you want to print logs pls write liblog on this line. -LOCAL_STATIC_LIBRARIES := sodium - -# LOCAL_SHARED_LIBRARIES := liblog libcutils -LOCAL_LDLIBS := -llog -LOCAL_CPPFLAGS := -fexceptions -include $(BUILD_SHARED_LIBRARY) - diff --git a/aesjni/src/main/jni/OriginAndroid_r13_mac.mk b/aesjni/src/main/jni/OriginAndroid_r13_mac.mk index 703ed7a..234ecfd 100755 --- a/aesjni/src/main/jni/OriginAndroid_r13_mac.mk +++ b/aesjni/src/main/jni/OriginAndroid_r13_mac.mk @@ -1,49 +1,7 @@ -# ========== static sodium begin ================== LOCAL_PATH := $(call my-dir) -include $(CLEAR_VARS) +include $(LOCAL_PATH)/BaseAndroid.mk -ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) - ARCH_PREFIX := armv7-a -else ifeq ($(TARGET_ARCH_ABI),armeabi) - ARCH_PREFIX := armv6 -else ifeq ($(TARGET_ARCH_ABI),x86) - ARCH_PREFIX := i686 -else ifeq ($(TARGET_ARCH_ABI),mips) - ARCH_PREFIX := mips32 -else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) - ARCH_PREFIX := armv8-a -else - ARCH_PREFIX := $(TARGET_ARCH) -endif - -SODIUM_LIB_DIR := $(LOCAL_PATH)/sodium_include/libsodium-android-$(ARCH_PREFIX) -SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include -SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a -LOCAL_MODULE:= sodium -LOCAL_SRC_FILES:= $(SODIUM_LIB) -LOCAL_EXPORT_C_INCLUDES := $(SODIUM_INC_DIR) $(SODIUM_INC_DIR)/sodium -include $(PREBUILT_STATIC_LIBRARY) - -# ========== static sodium end ================== - -include $(CLEAR_VARS) - -DEBUG_FILE:=debugger.c -ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) - DEBUG_FILE := debugger_test.c -endif - - -LOCAL_MODULE := JNIEncrypt -LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ - base64.c \ - keys_generator.c \ - str_utils.cpp \ - checksignature.c \ - check_emulator.c \ - $(DEBUG_FILE) \ - logger.h LOCAL_CFLAGS += -Wall -g -pedantic -std=c99 # if you want to print logs pls write liblog on this line. diff --git a/aesjni/src/main/jni/OriginAndroid_r16_mac.mk b/aesjni/src/main/jni/OriginAndroid_r16_mac.mk index b9b1fd2..3db61c0 100755 --- a/aesjni/src/main/jni/OriginAndroid_r16_mac.mk +++ b/aesjni/src/main/jni/OriginAndroid_r16_mac.mk @@ -1,43 +1,6 @@ -# ========== static sodium begin ================== LOCAL_PATH := $(call my-dir) +include $(LOCAL_PATH)/BaseAndroid.mk -include $(CLEAR_VARS) - -ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) - ARCH_PREFIX := armv7-a -else ifeq ($(TARGET_ARCH_ABI),armeabi) - ARCH_PREFIX := armv6 -else ifeq ($(TARGET_ARCH_ABI),x86) - ARCH_PREFIX := i686 -else ifeq ($(TARGET_ARCH_ABI),mips) - ARCH_PREFIX := mips32 -else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) - ARCH_PREFIX := armv8-a -else - ARCH_PREFIX := $(TARGET_ARCH) -endif - -SODIUM_LIB_DIR := $(LOCAL_PATH)/sodium_include/libsodium-android-$(ARCH_PREFIX) -SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include -SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a -LOCAL_MODULE:= sodium -LOCAL_SRC_FILES:= $(SODIUM_LIB) -LOCAL_EXPORT_C_INCLUDES := $(SODIUM_INC_DIR) $(SODIUM_INC_DIR)/sodium -include $(PREBUILT_STATIC_LIBRARY) - -# ========== static sodium end ================== - -include $(CLEAR_VARS) - -LOCAL_MODULE := JNIEncrypt -LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ - base64.c \ - keys_generator.c \ - str_utils.cpp \ - checksignature.c \ - check_emulator.c \ - debugger.c \ - logger.h LOCAL_CFLAGS += -Wall -g -pedantic # if you want to print logs pls write liblog on this line. diff --git a/aesjni/src/main/jni/OriginAndroid_r16_ubuntu.mk b/aesjni/src/main/jni/OriginAndroid_r16_ubuntu.mk index 29da6d5..8b260ab 100755 --- a/aesjni/src/main/jni/OriginAndroid_r16_ubuntu.mk +++ b/aesjni/src/main/jni/OriginAndroid_r16_ubuntu.mk @@ -1,33 +1,6 @@ -# ========== static sodium begin ================== LOCAL_PATH := $(call my-dir) +include $(LOCAL_PATH)/BaseAndroid.mk -include $(CLEAR_VARS) - -ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) - ARCH_PREFIX := armv7-a -else ifeq ($(TARGET_ARCH_ABI),armeabi) - ARCH_PREFIX := armv6 -else ifeq ($(TARGET_ARCH_ABI),x86) - ARCH_PREFIX := i686 -else ifeq ($(TARGET_ARCH_ABI),mips) - ARCH_PREFIX := mips32 -else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) - ARCH_PREFIX := armv8-a -else - ARCH_PREFIX := $(TARGET_ARCH) -endif - -SODIUM_LIB_DIR := $(LOCAL_PATH)/sodium_include/libsodium-android-$(ARCH_PREFIX) -SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include -SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a -LOCAL_MODULE:= sodium -LOCAL_SRC_FILES:= $(SODIUM_LIB) -LOCAL_EXPORT_C_INCLUDES := $(SODIUM_INC_DIR) $(SODIUM_INC_DIR)/sodium -include $(PREBUILT_STATIC_LIBRARY) - -# ========== static sodium end ================== - -include $(CLEAR_VARS) LOCAL_MODULE := JNIEncrypt LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ @@ -35,7 +8,7 @@ LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ keys_generator.c \ str_utils.cpp \ checksignature.c \ - check_emulator.c \ + $(DEBUG_FILE_C) \ debugger.c \ logger.h LOCAL_CFLAGS += -Wall -g -pedantic diff --git a/aesjni/src/main/jni/OriginAndroid_r22_mac.mk b/aesjni/src/main/jni/OriginAndroid_r22_mac.mk index b9b1fd2..3db61c0 100755 --- a/aesjni/src/main/jni/OriginAndroid_r22_mac.mk +++ b/aesjni/src/main/jni/OriginAndroid_r22_mac.mk @@ -1,43 +1,6 @@ -# ========== static sodium begin ================== LOCAL_PATH := $(call my-dir) +include $(LOCAL_PATH)/BaseAndroid.mk -include $(CLEAR_VARS) - -ifeq ($(TARGET_ARCH_ABI),armeabi-v7a) - ARCH_PREFIX := armv7-a -else ifeq ($(TARGET_ARCH_ABI),armeabi) - ARCH_PREFIX := armv6 -else ifeq ($(TARGET_ARCH_ABI),x86) - ARCH_PREFIX := i686 -else ifeq ($(TARGET_ARCH_ABI),mips) - ARCH_PREFIX := mips32 -else ifeq ($(TARGET_ARCH_ABI),arm64-v8a) - ARCH_PREFIX := armv8-a -else - ARCH_PREFIX := $(TARGET_ARCH) -endif - -SODIUM_LIB_DIR := $(LOCAL_PATH)/sodium_include/libsodium-android-$(ARCH_PREFIX) -SODIUM_INC_DIR := $(SODIUM_LIB_DIR)/include -SODIUM_LIB := $(SODIUM_LIB_DIR)/lib/libsodium.a -LOCAL_MODULE:= sodium -LOCAL_SRC_FILES:= $(SODIUM_LIB) -LOCAL_EXPORT_C_INCLUDES := $(SODIUM_INC_DIR) $(SODIUM_INC_DIR)/sodium -include $(PREBUILT_STATIC_LIBRARY) - -# ========== static sodium end ================== - -include $(CLEAR_VARS) - -LOCAL_MODULE := JNIEncrypt -LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ - base64.c \ - keys_generator.c \ - str_utils.cpp \ - checksignature.c \ - check_emulator.c \ - debugger.c \ - logger.h LOCAL_CFLAGS += -Wall -g -pedantic # if you want to print logs pls write liblog on this line. diff --git a/aesjni/src/main/jni/debugger.c b/aesjni/src/main/jni/debugger.c index bfbd91a..c6f23bd 100755 --- a/aesjni/src/main/jni/debugger.c +++ b/aesjni/src/main/jni/debugger.c @@ -8,13 +8,6 @@ #include "logger.h" -/** - * ----------- - * https://developer.android.com/ndk/guides/ndk-build#options - * NDK_DEBUG=1 : Force a debuggable build. - * ----------- - */ - int is_debug() { return 0; From 8101fdfd1e01b9f74328b6cd7ec8d6562c5a9233 Mon Sep 17 00:00:00 2001 From: bruce Date: Wed, 24 Feb 2021 15:46:36 +0800 Subject: [PATCH 20/21] [del]:{remove useless codes.} --- aesjni/src/main/jni/OriginAndroid_r16_ubuntu.mk | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/aesjni/src/main/jni/OriginAndroid_r16_ubuntu.mk b/aesjni/src/main/jni/OriginAndroid_r16_ubuntu.mk index 8b260ab..7ad3fb3 100755 --- a/aesjni/src/main/jni/OriginAndroid_r16_ubuntu.mk +++ b/aesjni/src/main/jni/OriginAndroid_r16_ubuntu.mk @@ -1,16 +1,6 @@ LOCAL_PATH := $(call my-dir) include $(LOCAL_PATH)/BaseAndroid.mk - -LOCAL_MODULE := JNIEncrypt -LOCAL_SRC_FILES := $(LOCAL_PATH)/JNIEncrypt.c \ - base64.c \ - keys_generator.c \ - str_utils.cpp \ - checksignature.c \ - $(DEBUG_FILE_C) \ - debugger.c \ - logger.h LOCAL_CFLAGS += -Wall -g -pedantic LOCAL_STATIC_LIBRARIES := sodium From 6d2c132020a2b55cc6e30a4869c741b76a6938d4 Mon Sep 17 00:00:00 2001 From: bruce Date: Wed, 24 Feb 2021 18:38:22 +0800 Subject: [PATCH 21/21] [Test]:{fix a native crash that pressure-test occured.} MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复一个压力测试爆出出来的bug --- FigureOutJNICrash.md | 82 ++++++++----------- aesjni/build.gradle | 4 +- .../java/com/androidyuan/aesjni/JNITest.java | 53 ++++++------ .../com/androidyuan/aesjni/EncryptEntry.java | 3 +- aesjni/src/main/jni/JNIEncrypt.c | 39 +++++---- aesjni/src/main/jni/TestAndroid.mk | 4 + aesjni/src/main/jni/debugger.c | 9 +- aesjni/src/main/jni/debugger.h | 2 + aesjni/src/main/jni/debugger_test.c | 2 + aesjni/src/main/jni/main_unit_test.c | 2 +- aesjni/src/main/jni/str_utils.cpp | 1 + aesjni/src/main/jni/str_utils.h | 1 - aesjni/src/main/jni/test_in_executing.sh | 4 +- .../aesjniencrypt/MainActivity.java | 29 ++----- 14 files changed, 120 insertions(+), 115 deletions(-) diff --git a/FigureOutJNICrash.md b/FigureOutJNICrash.md index 6aef94e..86aa6e2 100644 --- a/FigureOutJNICrash.md +++ b/FigureOutJNICrash.md @@ -19,56 +19,46 @@ adb logcat | ndk-stack -sym ./aesjni/src/main/obj/local/x86/ I got crash logs: ```logs -$ ./rebuild.sh test -2021-02-19 17:05:04.569 29543-29543/? A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x1 in tid 29543 (test), pid 29543 (test) -2021-02-19 17:05:04.621 29547-29547/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** -2021-02-19 17:05:04.621 29547-29547/? A/DEBUG: Build fingerprint: 'samsung/d1qzc/d1q:11/RP1A.200720.012/N9700ZCU3EUA6:user/release-keys' -2021-02-19 17:05:04.622 29547-29547/? A/DEBUG: Revision: '9' -2021-02-19 17:05:04.622 29547-29547/? A/DEBUG: ABI: 'arm' -2021-02-19 17:05:04.623 29547-29547/? A/DEBUG: Timestamp: 2021-02-19 17:05:04+0800 -2021-02-19 17:05:04.623 29547-29547/? A/DEBUG: pid: 29543, tid: 29543, name: test >>> /data/local/tmp/test <<< -2021-02-19 17:05:04.623 29547-29547/? A/DEBUG: uid: 2000 -2021-02-19 17:05:04.623 29547-29547/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x1 -2021-02-19 17:05:04.625 29547-29547/? A/DEBUG: Cause: null pointer dereference -2021-02-19 17:05:04.626 29547-29547/? A/DEBUG: r0 00000001 r1 ffdb37f3 r2 00000001 r3 0024c80e -2021-02-19 17:05:04.626 29547-29547/? A/DEBUG: r4 7fffffff r5 00000001 r6 00000001 r7 ffdb3800 -2021-02-19 17:05:04.626 29547-29547/? A/DEBUG: r8 ffdb3698 r9 ffdb3cac r10 ffdb37f3 r11 00000000 -2021-02-19 17:05:04.626 29547-29547/? A/DEBUG: ip 00000030 sp ffdb35e0 lr f65f212d pc f65b7ef0 -2021-02-19 17:05:04.646 29547-29547/? A/DEBUG: backtrace: -2021-02-19 17:05:04.647 29547-29547/? A/DEBUG: NOTE: Function names and BuildId information is missing for some frames due -2021-02-19 17:05:04.647 29547-29547/? A/DEBUG: NOTE: to unreadable libraries. For unwinds of apps, only shared libraries -2021-02-19 17:05:04.647 29547-29547/? A/DEBUG: NOTE: found under the lib/ directory are readable. -2021-02-19 17:05:04.647 29547-29547/? A/DEBUG: #00 pc 00036ef0 /apex/com.android.runtime/lib/bionic/libc.so (__memcpy_base_a55+488) (BuildId: 940ec10aefab667cc40b263303abf96e) -2021-02-19 17:05:04.647 29547-29547/? A/DEBUG: #01 pc 00071129 /apex/com.android.runtime/lib/bionic/libc.so (__sfvwrite+166) (BuildId: 940ec10aefab667cc40b263303abf96e) -2021-02-19 17:05:04.647 29547-29547/? A/DEBUG: #02 pc 0006af97 /apex/com.android.runtime/lib/bionic/libc.so (__vfprintf+6306) (BuildId: 940ec10aefab667cc40b263303abf96e) -2021-02-19 17:05:04.647 29547-29547/? A/DEBUG: #03 pc 0007fae1 /apex/com.android.runtime/lib/bionic/libc.so (sprintf+128) (BuildId: 940ec10aefab667cc40b263303abf96e) -2021-02-19 17:05:04.647 29547-29547/? A/DEBUG: #04 pc 00001045 /data/local/tmp/test -2021-02-19 17:05:04.648 29547-29547/? A/DEBUG: #05 pc 0000113d /data/local/tmp/test -2021-02-19 17:05:04.648 29547-29547/? A/DEBUG: #06 pc 0003382d /apex/com.android.runtime/lib/bionic/libc.so (__libc_init+68) (BuildId: 940ec10aefab667cc40b263303abf96e) - +$ ./test_in_executing.sh +2021-02-24 17:03:48.242 12059-12059/? A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 12059 (test), pid 12059 (test) +2021-02-24 17:03:48.251 12063-12063/? A/DEBUG: *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** +2021-02-24 17:03:48.251 12063-12063/? A/DEBUG: Build fingerprint: 'google/sdk_gphone_x86_arm/generic_x86_arm:11/RSR1.201013.001/6903271:userdebug/dev-keys' +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: Revision: '0' +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: ABI: 'x86' +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: Timestamp: 2021-02-24 17:03:48+0800 +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: pid: 12059, tid: 12059, name: test >>> /data/local/tmp/test <<< +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: uid: 2000 +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: Cause: null pointer dereference +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: eax 00000000 ebx 60915ef0 ecx f482efe0 edx f482efe0 +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: edi fff9e924 esi 608f0c20 +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: ebp fff9e848 esp fff9e78c eip 60857ef3 +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: backtrace: +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: NOTE: Function names and BuildId information is missing for some frames due +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: NOTE: to unreadable libraries. For unwinds of apps, only shared libraries +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: NOTE: found under the lib/ directory are readable. +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: NOTE: On this device, run setenforce 0 to make the libraries readable. +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: #00 pc 0000bef3 /data/local/tmp/test +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: #01 pc 0000532e /data/local/tmp/test +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: #02 pc 000058f7 /data/local/tmp/test +2021-02-24 17:03:48.252 12063-12063/? A/DEBUG: #03 pc 000522e3 /apex/com.android.runtime/lib/bionic/libc.so (__libc_init+115) (BuildId: 6e3a0180fa6637b68c0d181c343e6806) ``` - +I run ndk-stack: ```log -$ adb logcat | ndk-stack -sym ./obj/local/armeabi-v7a/ -Crash dump is completed - -********** Crash dump: ********** -Build fingerprint: 'samsung/d1qzc/d1q:11/RP1A.200720.012/N9700ZCU3EUA6:user/release-keys' -pid: 29543, tid: 29543, name: test >>> /data/local/tmp/test <<< -signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x1 -Stack frame #00 pc 00036ef0 /apex/com.android.runtime/lib/bionic/libc.so (__memcpy_base_a55+488) (BuildId: 940ec10aefab667cc40b263303abf96e) -Stack frame #01 pc 00071129 /apex/com.android.runtime/lib/bionic/libc.so (__sfvwrite+166) (BuildId: 940ec10aefab667cc40b263303abf96e) -Stack frame #02 pc 0006af97 /apex/com.android.runtime/lib/bionic/libc.so (__vfprintf+6306) (BuildId: 940ec10aefab667cc40b263303abf96e) -Stack frame #03 pc 0007fae1 /apex/com.android.runtime/lib/bionic/libc.so (sprintf+128) (BuildId: 940ec10aefab667cc40b263303abf96e) -Stack frame #04 pc 00001045 /data/local/tmp/test: Routine test_chacha20 at /Users/bruce/Documents/git/AESJniEncrypt/aesjni/src/main/jni/main_unit_test.c:146 -Stack frame #05 pc 0000113d /data/local/tmp/test: Routine main at /Users/bruce/Documents/git/AESJniEncrypt/aesjni/src/main/jni/main_unit_test.c:170 - +$ adb logcat | ndk-stack -sym ./aesjni/src/main/obj/local/x86 + ********** Crash dump: ********** + Build fingerprint: 'google/sdk_gphone_x86_arm/generic_x86_arm:11/RSR1.201013.001/6903271:userdebug/dev-keys' + pid: 12059, tid: 12059, name: test >>> /data/local/tmp/test <<< + signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 + Stack frame #00 pc 0000bef3 /data/local/tmp/test: Routine crypto_aead_aes256gcm_beforenm at ??:? + Stack frame #01 pc 0000532e /data/local/tmp/test: Routine test_aead_aes256gcm at ~/Documents/git/AESJniEncrypt/aesjni/src/main/jni/main_unit_test.c:48 + Stack frame #02 pc 000058f7 /data/local/tmp/test: Routine main at ~/Documents/git/AESJniEncrypt/aesjni/src/main/jni/main_unit_test.c:142 + Stack frame #03 pc 000522e3 /apex/com.android.runtime/lib/bionic/libc.so (__libc_init+115) (BuildId: 6e3a0180fa6637b68c0d181c343e6806) + ``` -`/Users/******/git/AESJniEncrypt/aesjni/src/main/jni/main_unit_test.c:146` show me that -executable file crash at `line 146 from main_unit_test.c`. +`at ~/Documents/git/AESJniEncrypt/aesjni/src/main/jni/main_unit_test.c:48` show me that +executable file crash at `line 48 from main_unit_test.c`. -## Remind -*ndk-stack* of NDK-R13 **only supports** symbol file of armeabi-v7a. diff --git a/aesjni/build.gradle b/aesjni/build.gradle index bdb623e..817e49f 100644 --- a/aesjni/build.gradle +++ b/aesjni/build.gradle @@ -44,9 +44,9 @@ android { ndkBuild { // path file("src/main/jni/Android.mk") // path file("src/main/jni/OriginAndroid.mk") -// path file("src/main/jni/OriginAndroid_r13_mac.mk") + path file("src/main/jni/OriginAndroid_r13_mac.mk") // path file("src/main/jni/OriginAndroid_r16_mac.mk") - path file("src/main/jni/OriginAndroid_r22_mac.mk") +// path file("src/main/jni/OriginAndroid_r22_mac.mk") // path file("src/main/jni/OriginAndroid_r16_ubuntu.mk") } } else { diff --git a/aesjni/src/androidTest/java/com/androidyuan/aesjni/JNITest.java b/aesjni/src/androidTest/java/com/androidyuan/aesjni/JNITest.java index 561b026..3381ad9 100644 --- a/aesjni/src/androidTest/java/com/androidyuan/aesjni/JNITest.java +++ b/aesjni/src/androidTest/java/com/androidyuan/aesjni/JNITest.java @@ -7,6 +7,8 @@ import org.junit.Test; import org.junit.runner.RunWith; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; import static org.junit.Assert.assertEquals; @@ -21,38 +23,43 @@ public class JNITest { private static final String PLAIN = "123abcABC*%!~#+_/中文测试"; private static final String ENCODE_STR = "1cad994119e4f3038fe29e34a4a12ce8a4b28f06db7682ab9d4f775c0e25659b01da27db09368eb3778ccea063"; + private Context appContext; + + //it is used avoid running useless test code. + private final AtomicBoolean atomicBoolean = new AtomicBoolean(); + //test in a thread pool. + private final ExecutorService executor = Executors.newFixedThreadPool(3); + + private final Runnable pressureTestRun = new Runnable() { + @Override + public void run() { + + for (int i = 0; i < 1000 * 100; i++) { + final String code = EncryptEntry.encode(appContext.getApplicationContext(), PLAIN); + assertEquals(ENCODE_STR, code); + final String decode = EncryptEntry.decode(appContext.getApplicationContext(), ENCODE_STR); + assertEquals(PLAIN, decode); + + } + atomicBoolean.set(false); + } + }; + @Test public void useAppContext() throws Exception { // Context of the app under test. - final Context appContext = InstrumentationRegistry.getTargetContext(); + appContext = InstrumentationRegistry.getTargetContext(); assertEquals(EncryptEntry.checkSignature(appContext), 1); - final AtomicBoolean atomicBoolean = new AtomicBoolean(); - atomicBoolean.set(false); - - - new Thread(new Runnable() { - @Override - public void run() { - for (int i = 0; i < 1000 * 1000; i++) { + atomicBoolean.set(true); + executor.submit(pressureTestRun); + executor.submit(pressureTestRun); + executor.submit(pressureTestRun); - //plain:"123abcABC&*(@#@#@)+_/中文测试" - final String code = EncryptEntry.encode(appContext, PLAIN); - assertEquals(code, ENCODE_STR); - final String decode = EncryptEntry.decode(appContext, ENCODE_STR); - assertEquals(PLAIN, decode); + while (atomicBoolean.get()) { - } - atomicBoolean.set(true); - } - }).start(); - - while (true) { - if (atomicBoolean.get()) { - break; - } } } } diff --git a/aesjni/src/main/java/com/androidyuan/aesjni/EncryptEntry.java b/aesjni/src/main/java/com/androidyuan/aesjni/EncryptEntry.java index 2d1c0a5..efd8510 100644 --- a/aesjni/src/main/java/com/androidyuan/aesjni/EncryptEntry.java +++ b/aesjni/src/main/java/com/androidyuan/aesjni/EncryptEntry.java @@ -25,10 +25,9 @@ public class EncryptEntry { /** - * AES 解密 + * chacha20 decrypt * * @param context - * @param str * @return WRONG_SIGNATURE : sign not pass . */ public static native String decode(Object context, String str); diff --git a/aesjni/src/main/jni/JNIEncrypt.c b/aesjni/src/main/jni/JNIEncrypt.c index ba5f1a8..9c9f61d 100755 --- a/aesjni/src/main/jni/JNIEncrypt.c +++ b/aesjni/src/main/jni/JNIEncrypt.c @@ -5,6 +5,7 @@ #include "checksignature.h" #include "check_emulator.h" #include "keys_generator.h" +#include "logger.h" #include #include @@ -34,6 +35,7 @@ jstring char2jstring(JNIEnv *envPtr, char *src) { jbyteArray barr = env->NewByteArray(envPtr, len); env->SetByteArrayRegion(envPtr, barr, 0, len, (jbyte *) src); + return (jstring) env->NewObject(envPtr, clsstring, mid, barr, strencode); } @@ -51,7 +53,7 @@ jstring char2string_with_len(JNIEnv *envPtr, char *src, size_t str_len) { return (jstring) env->NewObject(envPtr, clsstring, mid, barr, strencode); } -unsigned char *getNonce() { +const unsigned char *getNonce() { if (nonce == NULL) { nonce = chacha20_hexnonce2bin(nonce_hex); } @@ -60,7 +62,7 @@ unsigned char *getNonce() { //hiding string table, it not work for my hack method, it just is a low-level defende way. //__attribute__((section (".mytext"))) -unsigned char *getKey() { +const unsigned char *getKey() { if (key == NULL) { key = chacha20_hexkey2bin(key_hex); } @@ -69,25 +71,28 @@ unsigned char *getKey() { JNIEXPORT jstring JNICALL encode(JNIEnv *env, jobject instance, jobject context, jstring str_) { //firstly, detect the apk is repackaged. -// if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) { -// char *str = WRONG_SIGNATURE; -// return char2jstring(env, str); -// } - + if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) { + char *str = WRONG_SIGNATURE; + return char2jstring(env, str); + } const char *plain_str = (*env)->GetStringUTFChars(env, str_, JNI_FALSE); - (*env)->ReleaseStringUTFChars(env, str_, plain_str); + unsigned char *ciphertext; ciphertext = (unsigned char *) sodium_malloc( strlen(plain_str) + crypto_aead_chacha20poly1305_ABYTES); unsigned long long ciphertext_len; + char subbuff[5]; + memcpy(subbuff, getKey(), 4); + crypto_aead_chacha20poly1305_encrypt(ciphertext, &ciphertext_len, plain_str, strlen(plain_str), NULL, 0, //additional data is NULL, you can change it. NULL, getNonce(), getKey()); + if (ciphertext_len == 0) { abort(); } @@ -97,22 +102,25 @@ JNIEXPORT jstring JNICALL encode(JNIEnv *env, jobject instance, jobject context, 2 * ciphertext_len + 1); //return hex is easy to transport in internet. sodium_bin2hex(result_hex, (size_t) (2 * ciphertext_len + 1), ciphertext, ciphertext_len); sodium_free(ciphertext); + jstring result = (*env)->NewStringUTF(env, result_hex); sodium_free(result_hex); + + + (*env)->ReleaseStringUTFChars(env, str_, plain_str); return result; } JNIEXPORT jstring JNICALL decode(JNIEnv *env, jobject instance, jobject context, jstring str_) { //security checking. -// if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) { -// char *str = WRONG_SIGNATURE; -// return char2jstring(env, str); -// } + if (check_signature(env, instance, context) != 1 || check_is_emulator(env) != 1) { + char *str = WRONG_SIGNATURE; + return char2jstring(env, str); + } //str_ must is hex. const char *hex_str = (*env)->GetStringUTFChars(env, str_, JNI_FALSE); - (*env)->ReleaseStringUTFChars(env, str_, hex_str); int encrypt_len = strlen(hex_str) / 2; unsigned char *encrypted_str = (unsigned char *) sodium_malloc(encrypt_len); @@ -136,6 +144,7 @@ JNIEXPORT jstring JNICALL decode(JNIEnv *env, jobject instance, jobject context, jstring result = char2string_with_len(env, decrypted, (size_t) decrypted_len); //decrypted doesnt has '\0',so I put decrypted_len. + (*env)->ReleaseStringUTFChars(env, str_, hex_str); sodium_free(decrypted); return result; } @@ -177,9 +186,7 @@ int register_ndk_load(JNIEnv *env) { } JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { - - ptrace(PTRACE_TRACEME, 0, 0, 0); //anti-debug - //this is low level anti-debug method. + anti_debug(); // 有更复杂更高明的方案,比如:不用这个ptrace而是每次执行加密解密签先去判断是否被trace,目前的版本不做更多的负载方案,您想做可以fork之后,自己去做 JNIEnv *env = NULL; diff --git a/aesjni/src/main/jni/TestAndroid.mk b/aesjni/src/main/jni/TestAndroid.mk index aac7ed5..9bba3c2 100755 --- a/aesjni/src/main/jni/TestAndroid.mk +++ b/aesjni/src/main/jni/TestAndroid.mk @@ -37,9 +37,13 @@ LOCAL_SRC_FILES := main_unit_test.c \ debugger.c \ keys_generator.c \ base64.c \ + str_utils.h \ str_utils.cpp LOCAL_STATIC_LIBRARIES := sodium # tell ndk-build about the dependency LOCAL_LDLIBS := -llog #there is no need to specify path for liblog.so + + +LOCAL_CFLAGS += -Wall -g -pedantic -std=c99 # ndk13 on mac LOCAL_CPPFLAGS := -fexceptions include $(BUILD_EXECUTABLE) # <-- Use this to build as executable file. \ No newline at end of file diff --git a/aesjni/src/main/jni/debugger.c b/aesjni/src/main/jni/debugger.c index c6f23bd..3876adb 100755 --- a/aesjni/src/main/jni/debugger.c +++ b/aesjni/src/main/jni/debugger.c @@ -6,9 +6,14 @@ #include #include "string.h" #include "logger.h" +#include -int is_debug() -{ +int is_debug() { return 0; } + + +void anti_debug() { + ptrace(PTRACE_TRACEME, 0, 0, 0);//this is low level anti-debug method. +} \ No newline at end of file diff --git a/aesjni/src/main/jni/debugger.h b/aesjni/src/main/jni/debugger.h index 78dd4fb..8a16db2 100755 --- a/aesjni/src/main/jni/debugger.h +++ b/aesjni/src/main/jni/debugger.h @@ -12,4 +12,6 @@ // result 1: isdebug 0: release int is_debug(); +void anti_debug(); + #endif diff --git a/aesjni/src/main/jni/debugger_test.c b/aesjni/src/main/jni/debugger_test.c index 4b8f2a5..9013127 100755 --- a/aesjni/src/main/jni/debugger_test.c +++ b/aesjni/src/main/jni/debugger_test.c @@ -15,3 +15,5 @@ int is_debug() { return 1; } + +void anti_debug() {} diff --git a/aesjni/src/main/jni/main_unit_test.c b/aesjni/src/main/jni/main_unit_test.c index b594d22..1de3331 100755 --- a/aesjni/src/main/jni/main_unit_test.c +++ b/aesjni/src/main/jni/main_unit_test.c @@ -139,7 +139,7 @@ int main() printf("\n"); LOGI("-----------------------aead_aes256gcm----------------------"); - test_aead_aes256gcm(); + //test_aead_aes256gcm(); LOGI("-----------------------chacha20----------------------"); test_chacha20(); diff --git a/aesjni/src/main/jni/str_utils.cpp b/aesjni/src/main/jni/str_utils.cpp index e2ff2f1..2dff645 100644 --- a/aesjni/src/main/jni/str_utils.cpp +++ b/aesjni/src/main/jni/str_utils.cpp @@ -1,4 +1,5 @@ #include +#include // std::setw #include "str_utils.h" #ifdef __cplusplus extern "C" diff --git a/aesjni/src/main/jni/str_utils.h b/aesjni/src/main/jni/str_utils.h index e064b4f..3ed0e97 100644 --- a/aesjni/src/main/jni/str_utils.h +++ b/aesjni/src/main/jni/str_utils.h @@ -4,7 +4,6 @@ */ #include -#include #ifdef __cplusplus #define EXTERNC extern "C" diff --git a/aesjni/src/main/jni/test_in_executing.sh b/aesjni/src/main/jni/test_in_executing.sh index 6ba56f9..4f96b38 100755 --- a/aesjni/src/main/jni/test_in_executing.sh +++ b/aesjni/src/main/jni/test_in_executing.sh @@ -6,8 +6,8 @@ # Pls modify BUILT_ABI for build compatible you device. # ------------------------------------------------------- -#BUILT_ABI=x86 # make it compatible for emulator -BUILT_ABI=armeabi-v7a # make it compatible for physical device +BUILT_ABI=x86 # make it compatible for emulator +#BUILT_ABI=armeabi-v7a # make it compatible for physical device #pls modify TARGET_ANDROID_MK=OriginAndroid_r22_mac.mk diff --git a/app/src/main/java/com/androidyuan/aesjniencrypt/MainActivity.java b/app/src/main/java/com/androidyuan/aesjniencrypt/MainActivity.java index bd7ed7f..54f4cf1 100644 --- a/app/src/main/java/com/androidyuan/aesjniencrypt/MainActivity.java +++ b/app/src/main/java/com/androidyuan/aesjniencrypt/MainActivity.java @@ -8,9 +8,10 @@ import com.androidyuan.aesjni.EncryptEntry; public class MainActivity extends AppCompatActivity { + private static final String TAG = "CHACHA20-Test"; - private final String str = "123abcABC*%!~#+_/中文测试"; - private final String encodeStr = "1cad994119e4f3038fe29e34a4a12ce8a4b28f06db7682ab9d4f775c0e25659b01da27db09368eb3778ccea063"; + private final static String str = "123abcABC*%!~#+_/中文测试"; + private final static String encodeStr = "1cad994119e4f3038fe29e34a4a12ce8a4b28f06db7682ab9d4f775c0e25659b01da27db09368eb3778ccea063"; @Override protected void onCreate(Bundle savedInstanceState) { @@ -18,27 +19,15 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - new Thread(new Runnable() { - @Override - public void run() { - for (int i = 0; i < 1000 * 1000; i++) { - //下面的密文对应的原文:123abcABC&*(@#@#@)+_/中文测试 - final String code = EncryptEntry.encode(MainActivity.this.getApplicationContext(), str); - if (i > 1000 * 990 || i < 100) Log.i("code", code + ""); - final String decode = EncryptEntry.decode(MainActivity.this.getApplicationContext(), encodeStr); - if (i > 1000 * 990 || i < 100) Log.i("decode " + i, decode + ""); - - } - - } - }).start(); - + final String code = EncryptEntry.encode(MainActivity.this.getApplicationContext(), str); + Log.i(TAG, "code" + code + ""); + final String decode = EncryptEntry.decode(MainActivity.this.getApplicationContext(), encodeStr); + Log.i(TAG, "decode " + decode + ""); } - private void toast(String str) - { - Toast.makeText(this,str, Toast.LENGTH_LONG).show(); + private void toast(String str) { + Toast.makeText(this, str, Toast.LENGTH_LONG).show(); } }