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人點贊
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM