国产加密算法 SMx
椭圆曲线公钥加密算法
HASH摘要算法
分组对称加密算法
标准与说明文档
国家商用加密算法 SMx(SM2,SM3,SM4)
has a problem, how to trim the padding chars? and I do not want to pad files whose sizes are 16 times.
#include <string.h>
#include <stdio.h>
#include "sm4.h"
int main(int argc, char** argv)
{
unsigned char key[16] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
unsigned char input[16] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10};
unsigned char output[16];
sm4_context ctx;
unsigned long i,n;
FILE *fp,*fo;
//if (argc!=4 || argv[1][0]!='d' || argv[1][0]!='e' ) {printf("usage: a e|d inputfile outputfile\n%d %s %s %s",argc,argv[1],argv[2],argv[3]);return 0;}
fp = fopen(argv[2],"rb");
fo = fopen(argv[3],"wb");
i = 0;
if(argv[1][0]=='e')sm4_setkey_enc(&ctx, key);
if(argv[1][0]=='d')sm4_setkey_dec(&ctx, key);
while (!feof(fp))
{
n=fread(input,sizeof(char),16,fp);
//for (i = 0; i < 16; i++)printf("%c ", input[i]);
if (n==0) break;
if (n<16) for(i=0;i<16-n;i++)input[n+i]=16-n;
if(argv[1][0]=='e')
sm4_crypt_ecb(&ctx, 1, 16, input, output);
if(argv[1][0]=='d')
sm4_crypt_ecb(&ctx, 0, 16, input, output);
fwrite(output,sizeof(char),16,fo);
//i++;
}
fclose(fp);
fclose(fo);
printf("sm4 %s to %s .\n",argv[2],argv[3]);
return 0;
}
sm4 输入必须是16的倍数,如果不够就会补位,这里的补位逻辑不通用,我手动对输入进行了补位,如果是16的倍数,就补16个16,差8个到16的倍数就补8个8。
sm3 定义的变量全是long win是4字节 移植到linux long是8字节,会导致sm3计算的结果不一致。变量把long全改成int就可以了。
SM3/Linux/sm3.c第198行:
for (j = 16; j < 64; j++)
{
SS1 = ROTL((ROTL(A, 12) + E + ROTL(T[j], j)), 7);
................
SS1这行ROTL(T[j], j)这里好像和协议不太符合,协议上左移位数是jmod32,是要取模运算的,j>31之后不能直接取j吧?
支持过国家密码局的检测么?或者支持定制么?
你好,能在添加生成SM2密钥对的代码吗?非常感谢
(gdb) bt
#0 0x0009939a in sm4_one_round ()
Backtrace stopped: Cannot access memory at address 0x19b84582
看源码应该是移位的过程中发生的断错误
delete, nothing
SM3文档示例1
输入消息为“abc”,其ASCII码表示为616263 ... 杂凑值
66c7f0f4 62eeedd9 d1f2d46b dc10e4e2 4167c487 5cf2f7a2 297da02b 8f4ba8e0
在linux下编译输出结果为
Message:
abc
Hash:
37bc43d1 1cab393d 7899ef62 24f568ec 18a8fd85 1d165c50 0c375402 0f466a04
代码粗略看了下, 至少位移的宏看起来有问题
#define SHL(x,n) (((x) & 0xFFFFFFFF) << n)
#define ROTL(x,n) (SHL((x),n) | ((x) >> (32 - n)))
GMSSL里面的SM3代码, 位移宏为
#define ROTL(x,n) (((x)<<(n)) | ((x)>>(32-(n))))
GMSSL SM3输出没有问题。
您好,我使用例子中的sm4_crypt_ecb可以正常进行加解密,加密后进行解密可以得到原文。但是使用sm4_crypt_cbc就无法正常进行解密,解密出来的结果跟原文不一致。测试代码如下:
void Test1()
{
unsigned char key[16] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
unsigned char iv[16] = { 0x11, 0x33, 0x46, 0x69, 0x29, 0xac, 0xad, 0xdf, 0xfa, 0xfc, 0xab, 0x98, 0x76, 0x54, 0x32, 0x10 };
unsigned char input[16] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
unsigned char output[16];
sm4_context ctx;
unsigned long i;
//encrypt standard testing vector
sm4_setkey_enc(&ctx, key);
sm4_crypt_cbc(&ctx, 1, 16, iv,input, output);
for (i = 0; i < 16; i++)
printf("%02x ", output[i]);
printf("\n");
//decrypt testing
sm4_setkey_dec(&ctx, key);
sm4_crypt_cbc(&ctx, 0, 16, iv,output, output);
for (i = 0; i < 16; i++)
printf("%02x ", output[i]);
printf("\n");
}
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.