ikantech / gm Goto Github PK
View Code? Open in Web Editor NEW不依赖第三方库实现国密SM2、SM3、SM4算法
License: Apache License 2.0
不依赖第三方库实现国密SM2、SM3、SM4算法
License: Apache License 2.0
希望能有c#版的实现
/usr/bin/ld: /tmp/gm_test-0a45e1.o: in function test_bn': gm_test.c:(.text+0x46): undefined reference to
gm_bn_from_hex'
/usr/bin/ld: gm_test.c:(.text+0x70): undefined reference to gm_bn_from_hex' /usr/bin/ld: gm_test.c:(.text+0xb1): undefined reference to
GM_BN_P'
/usr/bin/ld: gm_test.c:(.text+0xc7): undefined reference to GM_BN_N' /usr/bin/ld: gm_test.c:(.text+0x103): undefined reference to
gm_bn_to_mont'
/usr/bin/ld: gm_test.c:(.text+0x139): undefined reference to gm_bn_from_mont' /usr/bin/ld: gm_test.c:(.text+0x28a): undefined reference to
gm_bn_add'
/usr/bin/ld: gm_test.c:(.text+0x301): undefined reference to gm_bn_sub' /usr/bin/ld: gm_test.c:(.text+0x345): undefined reference to
gm_bn_to_mont'
/usr/bin/ld: gm_test.c:(.text+0x35f): undefined reference to gm_bn_to_mont' /usr/bin/ld: gm_test.c:(.text+0x394): undefined reference to
gm_bn_mont_mul'
/usr/bin/ld: gm_test.c:(.text+0x3bc): undefined reference to `gm_bn_from_mont'
【问题页面】https://github.com/ikantech/gm/blob/main/c/sm4.c
【问题描述】使用sm4_cbc_pkcs7方式加密数据,原文数据长度为(16*k+1)字节时,加密出现段错误。其中k为大于0的正整数。
【问题分析】sm4.c中的函数gm_sm4_update和函数gm_sm4_done如下方2段代码。
(ctx->cur_buf_len == 16 && iLen > 0)
。iLen--
后,if判断为假,ctx->cur_buf_len
没有变为0,在执行ctx->buf[ctx->cur_buf_len++] = *input++;
之后,ctx->cur_buf_len
自增变为17;pad = 16 - ctx->cur_buf_len;
后,pad值为-1,之后再调用 memset(ctx->buf + ctx->cur_buf_len, pad, pad);
,出现段错误。int gm_sm4_update(gm_sm4_context * ctx, const unsigned char * input, unsigned int iLen, unsigned char * output) {
int rLen = 0;
while(iLen--) {
/* 是否满16个字节,这里要留一轮,要不调用gm_sm4_done时就不好处理填充了 */
if (ctx->cur_buf_len == 16 && iLen > 0) {
// 满了,则立即调用轮函数进行处理
update_one_round(ctx, output + rLen);
ctx->total_len += 16;
ctx->cur_buf_len = 0;
rLen += 16;
}
ctx->buf[ctx->cur_buf_len++] = *input++;
}
return rLen;
}
int gm_sm4_done(gm_sm4_context * ctx, unsigned char * output) {
int rLen = 0;
// 事先处理未满16字节加密时的填充
int pad = 0;
if((ctx->state & 0x01) && (ctx->state & 0x02) && (ctx->cur_buf_len != 16)) {
// 如果是加密,PKCS7Padding,并且是未满16字节,则填充
pad = 16 - ctx->cur_buf_len;
memset(ctx->buf + ctx->cur_buf_len, pad, pad);
ctx->cur_buf_len += pad;
}
......后续代码省略
【修改建议】
对gm_sm4_update的 iLen判断条件进行修改,修改为 iLen >= 0;对gm_sm4_done补充参数检查。提供以下参考代码:
int gm_sm4_update(gm_sm4_context * ctx, const unsigned char * input, unsigned int iLen, unsigned char * output) {
int rLen = 0;
while(iLen--) {
/* 是否满16个字节,这里要留一轮,要不调用gm_sm4_done时就不好处理填充了 */
if (ctx->cur_buf_len == 16 && iLen >= 0) {
// 满了,则立即调用轮函数进行处理
update_one_round(ctx, output + rLen);
ctx->total_len += 16;
ctx->cur_buf_len = 0;
rLen += 16;
}
ctx->buf[ctx->cur_buf_len++] = *input++;
}
return rLen;
}
int gm_sm4_done(gm_sm4_context * ctx, unsigned char * output) {
int rLen = 0;
// 事先处理未满16字节加密时的填充
int pad = 0;
if((ctx->state & 0x01) && (ctx->state & 0x02) && (ctx->cur_buf_len != 16)) {
// 如果是加密,PKCS7Padding,并且是未满16字节,则填充
pad = 16 - ctx->cur_buf_len;
if(pad < 0)
{
return -1;
}
memset(ctx->buf + ctx->cur_buf_len, pad, pad);
ctx->cur_buf_len += pad;
}
......后续代码省略
文件大小模64等于55的文件计算sm3时是错误的。
需要将gm_sm3_done中的 < 56修改为<= 56
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.