Giter VIP home page Giter VIP logo

smx's Issues

SM3计算结果和pdf文档不一致呢?

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输出没有问题。

my file to file example

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的补位问题,sm3的平台移植问题。

sm4 输入必须是16的倍数,如果不够就会补位,这里的补位逻辑不通用,我手动对输入进行了补位,如果是16的倍数,就补16个16,差8个到16的倍数就补8个8。
sm3 定义的变量全是long win是4字节 移植到linux long是8字节,会导致sm3计算的结果不一致。变量把long全改成int就可以了。

sm4_crypt_cbc加解密结果不对

您好,我使用例子中的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");

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.