天天看点

交叉编译openssl、gmssl的正确方式

最近要在arm设备上使用国密sm2、sm3算法,经了解,gmssl(openssl分支)能够支持,而且最新的openssl-1.1.1预览版也开始支持上述国密算法。

此前在ubuntu上交叉编译过openssl-1.0.2k,使用如下脚本实现:

./config --prefix=/opt/openssl --cross-compile-prefix=arm-linux-gnueabihf- no-asm shared
sed -i "s/ -m64//g" Makefile
make
make install           

笔者所有开发环境均通过svn管控,包括库文件、工具等的建立过程,因此在用config生成Makefile文件后,使用脚本自动删除arm编译器不支持的m64选项。

当使用同样的方法编译gmssl后,在设备上生成秘钥时出现运行时错误;openssl-1.1.1预览版编译后运行则是停止响应。

经过多方摸索,终于找到了原因所在:应使用Configure配置目标平台!新的编译脚本如下:

./Configure --prefix=/opt/openssl --cross-compile-prefix=arm-linux-gnueabihf- no-asm shared linux-armv4
make
make install           

因为是arm设备,这里配置目标平台为linux-armv4,其它平台编译方式相似,使用 ./Configure --help 能够显示支持的所有平台。

此外,当指定arm平台时,Makefile中不会出现不支持的m64选项,也就无需删除。

使用新的编译方式后,gmssl和openssl-1.1.1均能在arm设备上正常工作。

可以不用make install,只需将当前目录的libcrypto.so.1.1、libssl.so.1.1(其它版本类似),以及apps目录下的openssl or gmssl、openssl.cnf拷贝到目标设备相应目录即可。

继续阅读