NDK开发 - 使用GMSSL库和OpenSSL库的注意点及编译

NDK开发 - 使用GMSSL库和OpenSSL库的注意点及编译

Devil_Chen
2019.08.05 11:17:00字数 591阅读 271

前言

  • GmSSL是一个开源的密码工具箱,支持SM2/SM3/SM4/SM9/ZUC等国密(国家商用密码)算法、SM2国密数字证书及基于SM2证书的SSL/TLS安全通信协议,支持国密硬件密码设备,提供符合国密规范的编程接口与命令行工具,可以用于构建PKI/CA、安全通信、数据加密等符合国密标准的安全应用。GmSSL项目是OpenSSL项目的分支,并与OpenSSL保持接口兼容。因此GmSSL可以替代应用中的OpenSSL组件,并使应用自动具备基于国密的安全能力。GmSSL项目采用对商业应用友好的类BSD开源许可证,开源且可以用于闭源的商业应用。
  • GmSSL项目由北京大学关志副研究员的密码学研究组开发维护,项目源码托管于GitHub

编译

一、OpenSSL编译

1、参考https://github.com/leenjewel/openssl_for_ios_and_android
2、使用NDK 14来编译。
3、32位的库可以使用Android 16来编译。
4、64位的库必须使用大于等于Android 21来编译。
5、不同OpenSSL版本,头文件可能不同。
PS:使用上面链接下载的脚本可在_shared.sh中修改Android API版本,在build-openssl4Android.sh中可以填写OpenSSL版本。

二、GMSSL编译

1、参考https://github.com/wangp8895/gmssl-for-android
2、使用NDK 14来编译。
3、32位的库可以使用Android 16来编译。
4、64位的库必须使用大于等于Android 21来编译。
5、不同GMSSL版本,头文件可能不同。
PS:使用上面链接下载的脚本可在_shared.sh中修改Android API版本,在build-openssl4Android.sh中可以填写GMSSL版本。

使用

  • 环境

1、Android Studio 3.1.2
2、NDK 16
3、minSDKVersion 16 编译32位。
4、minSDKVersion 21 编译64位。
5、使用CMake。

  • 编写CMakeLists文件
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

#设置JNI变量名的值为${PROJECT_SOURCE_DIR}/src/main/jni
#Mac
set(JNI /${PROJECT_SOURCE_DIR}/src/main/jni)
#windows
#set(JNI ${PROJECT_SOURCE_DIR}/src/main/jni)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
             nativeLib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/jni/nativeTest.cpp)

#静态方式加载
add_library(crypto STATIC IMPORTED )
#配置加载头文件
include_directories( ${JNI}/include)
#引入第三方.a库
set_target_properties(crypto PROPERTIES IMPORTED_LOCATION ${JNI}/libs/${ANDROID_ABI}/libcrypto.a)



# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

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 )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       nativeLib
                       crypto
                        -lz

                       # Links the target library to the log library
                       # included in the NDK.

                       ${log-lib})

 
image.png
  • 如果遇到下面的错误,在CMakeLists中的target_link_libraries中加入 -lz 就可以解决。


     
    image.png
  • 注意OpenSSL的版本需要和OpenSSL的头文件版本一致,在OpenSSL头文件的opensslv.h中可以看到OpenSSL头文件的版本。


     
    image.png
 
 
0人点赞
 

猜你喜欢

转载自www.cnblogs.com/bigben0123/p/12650536.html