//sm4算法库使用示例,
/*
unsigned char data[16] ={0x01, 0x02,0x03,0x04,0x05,0x06,0x07,0x08, 0x01, 0x02,0x03,0x04,0x05,0x06,0x07,0x08};
unsigned char key[16] = {0x00};
unsigned char iv[16] = {0x00};
unsigned char output1[128] = {0x00};
unsigned char output2[128] = {0x00};
sm4_context ctx;
//SM4_ECB加密
sm4_setkey_enc(&ctx,key);
sm4_crypt_ecb(&ctx,1,16,data,output1); //SM4_ECB加密,output1为密文
printf("sm4_crypt_ecb:\n");
for(i=0;i<16;i++)
{
printf("-%02x-",output1[i]);
}
printf("\n");
//SM4_CBC加密
sm4_setkey_enc(&ctx,key);
sm4_crypt_cbc(&ctx,1,16,iv, output1,output2); //对output1进行SM4_CBC计算,iv为初始向量,output2为结果
printf("sm4_crypt_cbc:\n");
for(i=0;i<16;i++)
{
printf("-%02x-",output2[i]);
}
printf("\n");
*/
/*
//测试点:生成随机数
unsigned char pOutRand[20] = {0};
unsigned int pulRandLen = 0;
SPI_Init_Config();
GenerateRandom(pOutRand, &pulRandLen);
*/
/*
//测试点:外部认证
//生成随机数
unsigned char pOutRand[20] = {0x00};
unsigned int pulRandLen = 0;
unsigned char key[16] = {0x00};
unsigned char output[16] = {0x00};
sm4_context ctx;
SPI_Init_Config();
GenerateRandom(pOutRand, &pulRandLen);
//sm4_ecb
sm4_setkey_enc(&ctx,key);
sm4_crypt_ecb(&ctx,1,16,pOutRand,output);
//外部认证
ExternalAuth(output);
*/
/*
//测试点:获取COS信息
SGD_UCHAR pSerialNum[]={0x00};
SGD_UCHAR pCosVer[]={0x00};
SGD_UCHAR pChipType[]={0x00};
SPI_Init_Config();
GetDeviceInfo(pSerialNum, pCosVer, pChipType);
*/
/*
//测试点:删除密钥、导出公钥、内部SM2签名、内部SM2验签 、外部SM2验签
unsigned char pOutRand[20] = {0x00}; //存放随机数
unsigned int pulRandLen = 0; //生成随机数长度
unsigned char rootkey[16] = {0x00}; //默认根密钥,16字节00
unsigned char output[16] = {0x00}; //外部认证数据
sm4_context ctx;
SGD_UINT32 uiKeyIndex = 0x01; //密钥索引
SGD_UCHAR ucHashFlag = 1;
SGD_UCHAR pucDataInput[32] = {0x01, 0x02, 0x03, 0x04,0x05, 0x06,0x07, 0x08,
0x01, 0x02, 0x03, 0x04,0x05, 0x06,0x07, 0x08,
0x01, 0x02, 0x03, 0x04,0x05, 0x06,0x07, 0x08,
0x01, 0x02, 0x03, 0x04,0x05, 0x06,0x07, 0x08}; //32字节哈希
SGD_UINT32 uiInputLength = 0x20; //签名数据长度
SGD_UCHAR pucSignature[64]={0x00};//存放签名值
SGD_UCHAR pucPublicKey[64]={0x00};存放导出的公钥
SGD_UCHAR pucDataInput1[96]; //存放内部SM2验签数据
SGD_UCHAR pucDataInput2[160];//存放外部SM2验签数据
SPI_Init_Config(); //SPI通讯初始化
//生成随机数
GenerateRandom(pOutRand, &pulRandLen);
sm4_setkey_enc(&ctx,rootkey);
sm4_crypt_ecb(&ctx,1,16,pOutRand,output);
//外部认证
ExternalAuth(output);
//删除密钥
DestroyKey(0x05, uiKeyIndex);
//生成SM2密钥
GenerateSM2KeyPair(uiKeyIndex);
//导出公钥
ExportSM2PublicKey(uiKeyIndex, pucPublicKey);
//内部SM2签名
Sign_SM2(uiKeyIndex, ucHashFlag, pucDataInput, uiInputLength, pucSignature);
//内部SM2验签
memcpy(pucDataInput1, pucSignature, 64);
memcpy(pucDataInput1+64, pucDataInput, 32);
Verify_SM2(uiKeyIndex, ucHashFlag, pucDataInput1, 0x60);
//外部SM2验签
memcpy(pucDataInput2, pucPublicKey, 64);
memcpy(pucDataInput2+64, pucSignature, 64);
memcpy(pucDataInput2+128, pucDataInput, 32);
Verify_SM2(0x00, ucHashFlag, pucDataInput2, 0xA0);
*/
/*
//测试点:导入sm4密钥、sm4加密、sm4解密
unsigned char pucIV[16]={0x00};//初始向量
unsigned char pOutRand[20] = {0x00}; //存放随机数
unsigned int pulRandLen = 0; //生成随机数长度
unsigned char rootkey[16] = {0x00}; //默认根密钥,16字节00
unsigned char pucEncSymmetryKey[32] = {0x00}; //存放密文
unsigned char macdata[32]={0x00}; //存放mac数据
unsigned char mac[4]={0x00}; //存放mac数据
unsigned char SymmetryKey[16] = {0x01, 0x02,0x03,0x04,0x05,0x06,0x07,0x08, 0x01, 0x02,0x03,0x04,0x05,0x06,0x07,0x08};//16字节密钥
unsigned char SymmetryKeydata[32] = {0x10,
0x01, 0x02,0x03,0x04,0x05,0x06,0x07,0x08, 0x01, 0x02,0x03,0x04,0x05,0x06,0x07,0x08,
0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//补位后数据
unsigned int uiEncSymmetryKeyLen = sizeof(pucEncSymmetryKey); //计算密文
unsigned char cleardata[16] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}; //加密明文
unsigned char EncData [32] ={0x00}; //存放加密结果
unsigned int EncDataLength = 0;//加密结果长度
unsigned char pucData [16] ={0x00}; //存放解密结果
unsigned int DataLength = 0;//解密结果长度
sm4_context ctx;
SPI_Init_Config(); //SPI通讯初始化
sm4_setkey_enc(&ctx,rootkey);
sm4_crypt_ecb(&ctx,1,32,SymmetryKeydata,pucEncSymmetryKey); //计算密文
GenerateRandom(pOutRand, &pulRandLen);
sm4_setkey_enc(&ctx,rootkey);
sm4_crypt_cbc(&ctx,1,32,pOutRand,pucEncSymmetryKey, macdata); //计算mac
memcpy(mac, macdata+16, 4);
//导入sm4密钥
ImportSymmetryKey(0x01, 0x01, pucEncSymmetryKey, uiEncSymmetryKeyLen, mac);
//sm4加密
Encrypt(0x01, SGD_SM4_ECB, pucIV, cleardata, 0x10, EncData, &EncDataLength);
//sm4解密
Decrypt(0x01, SGD_SM4_ECB, pucIV, EncData, EncDataLength, pucData, &DataLength);
*/