国密SM1算法芯片用户手册说明,使用芯片算法时直接调用库函数即可实现SM1算法,或者通过芯片Uart口调用算法,使用方便、简单,节省开发时间。芯片交流QQ:1900109344SM1算法API接口说明1.1.数据类型定义typedefunsignedcharUCHAR;typedefunsignedintUINT;typedefunsignedlongULONG;typedefunsignedshortUSHORT;typedefunsignedcharU8;typedefunsignedintU16;typedefunsignedlongU32;typedefcharS8;typedefintS16;typedeflongS32;1.2.函数接口说明SM1算法库包含的函数列表如下:表1-1SM1算法库函数表函数描述voidSM1_Init(void)初始化SM1模块voidSm1Init(unsignedchar*key)SM1密钥初始化voidSM1_Close(void)关闭SM1模块voidSM1_Encrypt(U8*in,U16len,U8*out)SM1加密voidSM1_Decrypt(U8*in,U16len,U8*out)SM1解密1.2.1.初始化SM1模块SM1_Init初始化SM1模块函数原型voidSM1_Init(void)参数说明不带参数说明例程见附录一算法库函数调用例程。Sm1InitSM1密钥初始化函数原型voidSm1Init(unsignedchar*key)参数说明key输入,指向key的指针,key的长度为32字节说明例程见附录算法库函数调用例程。1.2.2.关闭SM1模块SM1_Close关闭SM1模块函数原型voidSM1_Close(void)参数说明不带参数说明例程见附录算法库函数调用例程。1.2.3.SM1加密SM1_EncryptSM1加密函数原型voidSM1_Encrypt(U8*in,U16len,U8*out)参数说明in输入,指向明文in的指针inLen输入,明文的长度out输出,指向密文out的指针说明明文的长度必须为16字节的整数倍。例程见附录算法库函数调用例程。1.2.4.SM1解密SM1_DecryptSM1解密函数原型voidSM1_Decrypt(U8*in,U16len,U8*out)参数说明in输入,指向输入密文的指针inLen输入,输入密文的长度out输出,指向解密得到的明文out的指针说明密文的长度必须为16字节的整数倍。例程见附录算法库函数调用例程。附录SM1算法函数调用例程#includestdio.h#includestring.h#includeuart.h#includeSM1.hvoidprint_buf(char*buf,intlen,char*name){inti;printf(%s:\r\n,name);for(i=0;ilen;i++){if((i&0x0f)==0xf)printf(0x%02bx,\r\n,buf[i]);elseprintf(0x%02bx,,buf[i]);}printf(\r\n);}intmain(void){unsignedcharxdatasm1_key[16]={0xCB,0x30,0x7D,0x46,0x87,0xAA,0xE3,0x2D,0x29,0xB6,0x64,0xC4,0x2B,0x80,0x5E,0xF0,};unsignedcharxdatain[16]={0x24,0xAF,0xEC,0xC4,0xA8,0xE7,0x27,0x7E,0x24,0xAF,0xEC,0xC4,0xA8,0xE7,0x27,0x7E};unsignedcharxdataout[16];unsignedcharxdatacipher[16];unsignedshortxdataklen=0;unsignedlongxdatatimes=0;uart_delay_time=0x0B;//115200printf(uart_delay_time=%02bx\r\n,uart_delay_time);klen=16;while(1){times++;SM1_Init();Sm1Init(sm1_key);SM1_Encrypt(in,klen,cipher);//SM1加密SM1_Close();SM1_Init();Sm1Init(sm1_key);SM1_Decrypt(cipher,klen,out);//SM1解密SM1_Close();if(memcmp(out,in,klen)){times++;print_buf(in,klen,right_plain);print_buf(out,klen,wrong_plain);}printf(times=%ld,times);}}