Windows 下使用Visual Studio 2013编译国密算法库GMSSL
最近因工作需要使用到国密SM2、SM4算法,在网上搜了下,虽然有C的源码,但似乎都是在openssl上加的SM2,而且大多都没有像样的开源项目或者个人进行维护,找来找去,只发现一个GMSSL开源项目(http://gmssl.org/)看上去最正规,它是OpenSSL项目的分支,并与OpenSSL保持接口兼容,由北京大学关志副研究员的密码学研究组开发维护,项目源码托管于GitHub。于是就选择它,来进行国密的开发。
从项目官网http://gmssl.org/下载源码包GmSSL-master.zip,将源码包解压后,看到的工程目录基本跟openssl 一致。按用户手册描述,在Windows编译,需安装ActivePerl和Visual Studio。
我电脑上已经装有ActivePerl5.20和VisualStudio 2013,便按用户手册描述以管理员身份打开Visual Studio Tools下的Developer Command Prompt控制台,通过cd命令进入到GmSSL-master解压目录后,输入perlConfigure VC-WIN32。这一步进行的非常顺利,没有出现任何错误。
再按用户手册描述,输入nmake,这次就没那么顺利了,没编几个文件,就报了一大堆错,用户手册上对编译除了三条命令,别的什么都没有,只能靠自己了。看命令行出现的错误如下:
crypto\base58\base58.c(81): error C2057: 应输入常量表达式
crypto\base58\base58.c(81) : error C2466: 不能分配常量大小为 0 的数组
crypto\base58\base58.c(81) : error C2133: “outi”: 未知的大小
….
基本明白,base58.c出现语法错误了,找到源文件对应错误行,对应描述如下:
intbase58_decode(const char *b58, size_t b58sz, void *bin, size_t *binszp)
{
size_t binsz = *binszp;
const unsigned char *b58u = (void*)b58;
unsigned char *binu = bin;
size_t outisz = (binsz + 3) / 4;
uint32_t outi[outisz];
看上去语法没错啊,进入MSDN查看C2057的详细描述,突然想起来这是VS的通病,对C99只支持部分。这个变长数组的语法,到2013版还是没进行支持。换支持C99的编译器编译,需要修改makefile,这个看上去工作量更大,还是改语法相对简单点。于是开始动手,将编程数组改成malloc分配内存。
#ifdefined(_MSC_VER)
uint32_t *outi = (uint32_t *)malloc(outisz);//动态分配内存
#else
uint32_t outi[outisz];//使用C99语法变长数组
#endif
因GMSSL我还要移到AIX上使用,就使用了宏判断编译器类型,如果是MSC编译器,就使用修改的代码,否则使用原项目代码。
malloc修改后,还有一项重要的事,就是free,否则内存泄露以后找的头疼。便对函数内所有return前增加了
#ifdefined(_MSC_VER)
free(outi);//释放动态分配内存
#endif
接下来还有如下错误需要解决
int base58_encode(const void *data, size_tbinsz, char *b58, size_t *b58sz)
{
const uint8_t *bin = data;
int carry;
ssize_t i, j, high, zcount = 0;
crypto\base58\base58.c(173) : error C2065: “ssize_t”: 未声明的标识符
crypto\base58\base58.c(173) : error C2146: 语法错误: 缺少“;”(在标识符“i”的前面)
ssize_t这类型与size_t的区别就是,一个是有符号的,一个是无符号的,再看后面的代码,发现for (i = zcount, high = size - 1; i < binsz; ++i, high = j),这不明显的出现了有符号数和无符号数做比较的问题,通过增加ssize_t的类型定义可以消除未声明的标识符的语法错误,但肯定在循环的条件里会出警告,这显然是项目源码书写时不注意多按了一个s,因此将ssize_t i, j, high, zcount = 0;改为size_t i, j, high,zcount = 0;
再往后的代码出现crypto\base58\base58.c(185): error C2275: “uint8_t”: 将此类型用作表达式非法,这个跟前面的一样用malloc和free解决。
base58.c修改好后nmake顺利过了,但最后又出现了
libcrypto-1_1.def : error LNK2001: 无法解析的外部符号speck_expand16
libcrypto-1_1.def : error LNK2001: 无法解析的外部符号speck_expand32
libcrypto-1_1.def : error LNK2001: 无法解析的外部符号speck_expand64
libcrypto.lib : fatal error LNK1120: 3 个无法解析的外部命令
通过对整个工程进行搜索,发现这3个文件出现在speck.h和libcrypto.num中。在speck.h中,这3个函数只有定义,而里面的其他函数,在speck.c中都有定义。因没搞明白这3个speck_expand函数要实现什么功能,而且在工程中也没别的地方调用过,便简单的在speck.c文件中添加了3个什么内容都没有的空函。
附:
1、修改后的base58.c
/*====================================================================
* Copyright (c) 2014 - 2017 The GmSSLProject. All rights reserved.
*
* Redistribution and use in source and binaryforms, with or without
* modification, are permitted provided thatthe following conditions
* are met:
*
* 1. Redistributions of source code mustretain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form mustreproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* thedocumentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioningfeatures or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" mustnot be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* gu