最近因工作需要使用到国密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
*
* 5. Products derived from this software maynot be called "GmSSL"
* normay "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoevermust retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSLPROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY ANDFITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTEGOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESSINTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*====================================================================
*/
/*
* Copyright 2012-2014 Luke Dashjr
*
* This program is free software; you canredistribute it and/or modify it
* under the terms of the standard MITlicense. See COPYING for more details.
*/
#include
#include
#include
#include
#include
#include
#include
static const int8_tb58digits_map[] = {
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8,-1,-1,-1,-1,-1,-1,
-1, 9,10,11,12,13,14,15, 16,-1,17,18,19,20,21,-1,
22,23,24,25,26,27,28,29,30,31,32,-1,-1,-1,-1,-1,
-1,33,34,35,36,37,38,39,40,41,42,43,-1,44,45,46,
47,48,49,50,51,52,53,54,55,56,57,-1,-1,-1,-1,-1,
};
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;
#if defined(_MSC_VER)
uint32_t *outi = (uint32_t*)malloc(outisz);//动态分配内存
#else
uint32_t outi[outisz];//使用C99语法 变长数组
#endif
uint64_t t;
uint32_t c;
size_t i, j;
uint8_t bytesleft = binsz % 4;
uint32_t zeromask = bytesleft ?(0xffffffff << (bytesleft * 8)) : 0;
unsigned zerocount = 0;
if (!b58sz)
b58sz = strlen(b58);
memset(outi, 0, outisz *sizeof(*outi));
// Leading zeros, just count
for (i = 0; i < b58sz &&b58u[i] == '1'; ++i)
++zerocount;
for ( ; i < b58sz; ++i)
{
if (b58u[i] & 0x80) {
// High-bit set oninvalid digit
BASE58err(BASE58_F_BASE58_DECODE,BASE58_R_HIGHBIT_SET_ON_INVALID_DIGIT);
#ifdefined(_MSC_VER)
free(outi);//释放动态分配内存
#endif
return 0;
}
if (b58digits_map[b58u[i]] ==-1)
{
// Invalid base58digit
#ifdefined(_MSC_VER)
free(outi);//释放动态分配内存
#endif
return 0;
}
c =(unsigned)b58digits_map[b58u[i]];
for (j = outisz; j--; )
{
t =((uint64_t)outi[j]) * 58 + c;
c = (t &0x3f00000000) >> 32;
outi[j] = t &0xffffffff;
}
if (c)
{
// Output number toobig (carry to the next int32)
#ifdefined(_MSC_VER)
free(outi);//释放动态分配内存
#endif
return 0;
}
if (outi[0] & zeromask)
{
// Output number toobig (last int32 filled too far)
#ifdefined(_MSC_VER)
free(outi);//释放动态分配内存
#endif
return 0;
}
}
j = 0;
switch (bytesleft) {
case 3:
*(binu++) = (outi[0]& 0xff0000) >> 16;
case 2:
*(binu++) = (outi[0]& 0xff00) >> 8;
case 1:
*(binu++) = (outi[0] & 0xff);
++j;
default:
break;
}
for (; j < outisz; ++j)
{
*(binu++) = (outi[j] >>0x18) & 0xff;
*(binu++) = (outi[j] >>0x10) & 0xff;
*(binu++) = (outi[j]>> 8) & 0xff;
*(binu++) = (outi[j]>> 0) & 0xff;
}
// Count canonical base58 byte count
binu = bin;
for (i = 0; i < binsz; ++i)
{
if (binu[i])
break;
--*binszp;
}
*binszp += zerocount;
#if defined(_MSC_VER)
free(outi);//释放动态分配内存
#endif
return 1;
}
static constchar b58digits_ordered[] ="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
intbase58_encode(const void *data, size_t binsz, char *b58, size_t *b58sz)
{
const uint8_t *bin = data;
int carry;
//ssize_t i, j, high, zcount = 0;后面出现有符号和无符号数比较 显然应该使用size_t
size_t i, j, high, zcount = 0;
size_t size;
while (zcount < binsz &&!bin[zcount])
++zcount;
size = (binsz - zcount) * 138 / 100 +1;
#if defined(_MSC_VER)
uint8_t *buf = (uint8_t*)malloc(size);//动态分配内存
#else
uint8_t buf[size];;//使用C99语法 变长数组
#endif
memset(buf, 0, size);
for (i = zcount, high = size - 1; i< binsz; ++i, high = j)
{
for (carry = bin[i], j = size- 1; (j > high) || carry; --j)
{
carry += 256 *buf[j];
buf[j] = carry % 58;
carry /= 58;
}
}
for (j = 0; j < size &&!buf[j]; ++j);
if (*b58sz <= zcount + size - j)
{
*b58sz = zcount + size - j +1;
#if defined(_MSC_VER)
free(buf);//释放动态分配内存
#endif
return 0;
}
if (zcount)
memset(b58, '1', zcount);
for (i = zcount; j < size; ++i, ++j)
b58[i] =b58digits_ordered[buf[j]];
b58[i] = '\0';
*b58sz = i + 1;
#if defined(_MSC_VER)
free(buf);//释放动态分配内存
#endif
return 1;
}
2、修改后的speck.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
*
* 5. Products derived from this software maynot be called "GmSSL"
* normay "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoevermust retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSLPROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY ANDFITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTEGOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESSINTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDINGNEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*/
#include
voidspeck_set_encrypt_key16(SPECK_TYPE16 const K[SPECK_KEY_LEN16], SPECK_TYPE16S[SPECK_ROUNDS16])
{
SPECK_TYPE16 i, b = K[0];
SPECK_TYPE16 a[SPECK_KEY_LEN16 - 1];
for (i = 0; i < (SPECK_KEY_LEN16 -1); i++)
{
a[i] = K[i + 1];
}
S[0] = b;
for (i = 0; i < SPECK_ROUNDS16 - 1;i++) {
R16(a[i % (SPECK_KEY_LEN16 -1)], b, i);
S[i + 1] = b;
}
}
voidspeck_set_decrypt_key16(SPECK_TYPE16 const K[SPECK_KEY_LEN16], SPECK_TYPE16S[SPECK_ROUNDS16])
{
SPECK_TYPE16 i, b = K[0];
SPECK_TYPE16 a[SPECK_KEY_LEN16 - 1];
for (i = 0; i < (SPECK_KEY_LEN16 -1); i++)
{
a[i] = K[i + 1];
}
S[0] = b;
for (i = 0; i < SPECK_ROUNDS16 - 1;i++) {
R16(a[i % (SPECK_KEY_LEN16 -1)], b, i);
S[i + 1] = b;
}
}
voidspeck_encrypt16(SPECK_TYPE16 const pt[2], SPECK_TYPE16 ct[2], SPECK_TYPE16const K[SPECK_ROUNDS16])
{
SPECK_TYPE16 i;
ct[0] = pt[0]; ct[1] = pt[1];
for (i = 0; i < SPECK_ROUNDS16; i++){
R16(ct[1], ct[0], K[i]);
}
}
voidspeck_decrypt16(SPECK_TYPE16 const ct[2], SPECK_TYPE16 pt[2], SPECK_TYPE16const K[SPECK_ROUNDS16])
{
SPECK_TYPE16 i;
pt[0] = ct[0]; pt[1] = ct[1];
for (i = 0; i < SPECK_ROUNDS16;i++){
RR16(pt[1], pt[0], K[(SPECK_ROUNDS16- 1) - i]);
}
}
voidspeck_set_encrypt_key32(SPECK_TYPE32 const K[SPECK_KEY_LEN32], SPECK_TYPE32S[SPECK_ROUNDS32])
{
SPECK_TYPE32 i, b = K[0];
SPECK_TYPE32 a[SPECK_KEY_LEN32 - 1];
for (i = 0; i < (SPECK_KEY_LEN32 -1); i++)
{
a[i] = K[i + 1];
}
S[0] = b;
for (i = 0; i < SPECK_ROUNDS32 - 1;i++) {
R32(a[i % (SPECK_KEY_LEN32 -1)], b, i);
S[i + 1] = b;
}
}
voidspeck_set_decrypt_key32(SPECK_TYPE32 const K[SPECK_KEY_LEN32], SPECK_TYPE32S[SPECK_ROUNDS32])
{
SPECK_TYPE32 i, b = K[0];
SPECK_TYPE32 a[SPECK_KEY_LEN32 - 1];
for (i = 0; i < (SPECK_KEY_LEN32 -1); i++)
{
a[i] = K[i + 1];
}
S[0] = b;
for (i = 0; i < SPECK_ROUNDS32 - 1;i++) {
R32(a[i % (SPECK_KEY_LEN32 -1)], b, i);
S[i + 1] = b;
}
}
voidspeck_encrypt32(SPECK_TYPE32 const pt[2], SPECK_TYPE32 ct[2], SPECK_TYPE32const K[SPECK_ROUNDS32])
{
SPECK_TYPE32 i;
ct[0] = pt[0]; ct[1] = pt[1];
for (i = 0; i < SPECK_ROUNDS32;i++){
R32(ct[1], ct[0], K[i]);
}
}
voidspeck_decrypt32(SPECK_TYPE32 const ct[2], SPECK_TYPE32 pt[2], SPECK_TYPE32const K[SPECK_ROUNDS32])
{
SPECK_TYPE32 i;
pt[0] = ct[0]; pt[1] = ct[1];
for (i = 0; i < SPECK_ROUNDS32;i++){
RR32(pt[1], pt[0],K[(SPECK_ROUNDS32 - 1) - i]);
}
}
voidspeck_set_encrypt_key64(SPECK_TYPE64 const K[SPECK_KEY_LEN64], SPECK_TYPE64S[SPECK_ROUNDS64])
{
SPECK_TYPE64 i, b = K[0];
SPECK_TYPE64 a[SPECK_KEY_LEN64 - 1];
for (i = 0; i < (SPECK_KEY_LEN64 -1); i++)
{
a[i] = K[i + 1];
}
S[0] = b;
for (i = 0; i < SPECK_ROUNDS64 - 1;i++) {
R64(a[i % (SPECK_KEY_LEN64 -1)], b, i);
S[i + 1] = b;
}
}
voidspeck_set_decrypt_key64(SPECK_TYPE64 const K[SPECK_KEY_LEN64], SPECK_TYPE64S[SPECK_ROUNDS64])
{
SPECK_TYPE64 i, b = K[0];
SPECK_TYPE64 a[SPECK_KEY_LEN64 - 1];
for (i = 0; i < (SPECK_KEY_LEN64 -1); i++)
{
a[i] = K[i + 1];
}
S[0] = b;
for (i = 0; i < SPECK_ROUNDS64 - 1;i++) {
R64(a[i % (SPECK_KEY_LEN64 -1)], b, i);
S[i + 1] = b;
}
}
voidspeck_encrypt64(SPECK_TYPE64 const pt[2], SPECK_TYPE64 ct[2], SPECK_TYPE64const K[SPECK_ROUNDS64])
{
SPECK_TYPE64 i;
ct[0] = pt[0]; ct[1] = pt[1];
for (i = 0; i < SPECK_ROUNDS64;i++){
R64(ct[1], ct[0], K[i]);
}
}
voidspeck_decrypt64(SPECK_TYPE64 const ct[2], SPECK_TYPE64 pt[2], SPECK_TYPE64const K[SPECK_ROUNDS64])
{
SPECK_TYPE64 i;
pt[0] = ct[0]; pt[1] = ct[1];
for (i = 0; i < SPECK_ROUNDS64;i++){
RR64(pt[1], pt[0],K[(SPECK_ROUNDS64 - 1) - i]);
}
}
voidspeck_expand16(SPECK_TYPE16 const K[SPECK_KEY_LEN16], SPECK_TYPE16S[SPECK_ROUNDS16])
{
}
voidspeck_expand32(SPECK_TYPE32 const K[SPECK_KEY_LEN32], SPECK_TYPE32S[SPECK_ROUNDS32])
{
}
voidspeck_expand64(SPECK_TYPE64 const K[SPECK_KEY_LEN64], SPECK_TYPE64S[SPECK_ROUNDS64])
{
}
3、gmsll的国密操作命令
SM4加密文件
$ gmssl sms4 -e-in
enter sms4-cbcencryption password:
Verifying -enter sms4-cbc encryption password:
解密
$ gmssl sms4 -d-in
enter sms4-cbcdecryption password:
生成SM3摘要
$ gmssl sm3
SM3(yourfile)=66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0
生成SM2密钥并签名
$ gmssl genpkey-algorithm EC -pkeyopt ec_paramgen_curve:sm2p256v1 \
-out signkey.pem
$ gmssl pkeyutl-sign -pkeyopt ec_sign_algor:sm2 -inkey signkey.pem \
-in
可以将公钥从signkey.pem中导出并发发布给验证签名的一方
$ gmssl pkey-pubout -in signkey.pem -out vrfykey.pem
$ gmssl pkeyutl-verify -pkeyopt ec_sign_algor:sm2 -pubin -inkey vrfykey.pem \
-in
生成SM2私钥及证书请求
gmssl ecparam-genkey -name sm2p256v1 -text -out user.key
gmssl req -new -key user.key -out user.req
查看证书请求内容:
gmssl req -in user.req -noout -text -subject