任务详情
两人一组完成下面任务
0. 分析代码接口和智能钥匙的接口的异同
- 编译运行附件中代码,提交运行结果截图
- 修改test.c完成包含小组内成员的学号姓名的文件的sm3 hash运算,sm4加密解密,提交代码和运行结果截图。
- 完成完成包含小组内成员的学号姓名的文件的签名验签名,提交代码和运行结果截图。
代码接口和智能钥匙接口是两种不同类型的接口,它们在功能、使用场景和目的上有一些异同点。
0. 异同分析:
1. 类型:
- 代码接口:主要指软件编程中的接口,用于实现软件的模块化、可重用性和可扩展性。
- 智能钥匙接口:主要用于实现车辆的安全和便捷性控制。
2. 目的和功能:
- 代码接口:用于软件开发,实现软件的模块化、可重用性和可扩展性,方便不同部分之间的交互和通信。
- 智能钥匙接口:主要用于车辆控制和安全,例如开启、关闭车辆、启动引擎等功能。
3. 使用场景:
- 代码接口:主要用于软件开发过程中,不同模块之间的通信和数据交换。
- 智能钥匙接口:主要用于汽车领域,与车辆的智能系统进行交互,实现对车辆的控制和管理。
1. 编译运行附件中代码,提交运行结果截图
2. 修改test.c完成包含小组内成员的学号姓名的文件的sm3 hash运算,sm4加密解密,提交代码和运行结果截图。
3. 完成完成包含小组内成员的学号姓名的文件的签名验签名,提交代码和运行结果截图。
/*************************************************************************
> File Name: test.c
> Author:
> Mail:
> Created Time: 2018年04月26日 星期四 16时01分52秒
************************************************************************/
#include<stdio.h>
#include <string.h>
#include"sdf.h"
#include<pthread.h>
#include<sys/time.h>
#include<sys/types.h>
#include <stdlib.h>
#define ROOTKEY "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10"
#define DEVSN "hs_0000000000001"
#define MAX (4000)
//#define MAX (1536)
#define LOOP (1000)
SGD_UCHAR pubKey[64] = {
0x8B, 0x7F, 0xB9, 0x6C, 0x09, 0x53, 0x27, 0x19, 0xE4, 0xEB, 0x79, 0x16, 0xC6, 0x2E, 0x20, 0xEA,
0x33, 0xCC, 0x98, 0x96, 0x43, 0xFA, 0x45, 0x09, 0x0C, 0x98, 0x4E, 0xA5, 0xDF, 0x76, 0xA1, 0xD4,
0xC1, 0xF8, 0x9C, 0x46, 0x67, 0x61, 0xCE, 0x07, 0x83, 0x26, 0xAF, 0x1C, 0xA0, 0x81, 0xC5, 0x89,
0x4E, 0x0C, 0xD5, 0x29, 0x88, 0x40, 0x96, 0x45, 0x50, 0xD9, 0x14, 0x17, 0xB7, 0x5C, 0xC5, 0x5E
};
SGD_UCHAR priKey[32] = {
0x08, 0x3B, 0xD8, 0xEA, 0xC2, 0x20, 0xE8, 0xC5, 0x98, 0x89, 0x83, 0xB4, 0x3E, 0x07, 0x13, 0x67,
0xE3, 0x0C, 0x02, 0xCE, 0xA8, 0xB9, 0x19, 0x19, 0xDD, 0x7F, 0xE8, 0xB8, 0xE6, 0xDC, 0x02, 0x5B
};
SGD_UCHAR pubKeyEnc[64] = {
0x19, 0x0e, 0x9e, 0x10, 0x5a, 0x12, 0xd5, 0x9b,
0xd5, 0x59, 0x5d, 0x7d, 0x06, 0xbe, 0xe0, 0x1b,
0x15, 0x44, 0xcc, 0x16, 0x1a, 0x34, 0xcc, 0x36,
0xe0, 0xbd, 0xa6, 0x83, 0x03, 0x97, 0xb5, 0x2e,
0x7f, 0xb5, 0x1a, 0xf6, 0x0e, 0xf9, 0xb7, 0x00,
0x88, 0x21, 0xdd, 0xda, 0xca, 0x2a, 0x18, 0xe9,
0x57, 0x49, 0xce, 0x49, 0xdc, 0x5f, 0xb9, 0x4d,
0xb8, 0xc8, 0x5c, 0xde, 0x96, 0xfb, 0x2b, 0x39,
};
SGD_UCHAR priKeyEnc[32] = {
0x3a, 0x7e, 0xff, 0x53, 0x3d, 0x23, 0xd1, 0x3a,
0xde, 0x97, 0x4b, 0xc3, 0x65, 0x3c, 0xd1, 0x43,
0x20, 0x31, 0x98, 0xe4, 0x48, 0x7f, 0x5b, 0xc0,
0x01, 0xd7, 0xe9, 0x5e, 0x20, 0xfc, 0xa1, 0xc5
};
SGD_UCHAR eccXYD[96] = {
0x19, 0x0e, 0x9e, 0x10, 0x5a, 0x12, 0xd5, 0x9b,
0xd5, 0x59, 0x5d, 0x7d, 0x06, 0xbe, 0xe0, 0x1b,
0x15, 0x44, 0xcc, 0x16, 0x1a, 0x34, 0xcc, 0x36,
0xe0, 0xbd, 0xa6, 0x83, 0x03, 0x97, 0xb5, 0x2e,
0x7f, 0xb5, 0x1a, 0xf6, 0x0e, 0xf9, 0xb7, 0x00,
0x88, 0x21, 0xdd, 0xda, 0xca, 0x2a, 0x18, 0xe9,
0x57, 0x49, 0xce, 0x49, 0xdc, 0x5f, 0xb9, 0x4d,
0xb8, 0xc8, 0x5c, 0xde, 0x96, 0xfb, 0x2b, 0x39,
0x3a, 0x7e, 0xff, 0x53, 0x3d, 0x23, 0xd1, 0x3a,
0xde, 0x97, 0x4b, 0xc3, 0x65, 0x3c, 0xd1, 0x43,
0x20, 0x31, 0x98, 0xe4, 0x48, 0x7f, 0x5b, 0xc0,
0x01, 0xd7, 0xe9, 0x5e, 0x20, 0xfc, 0xa1, 0xc5,
};
SGD_UCHAR eccXYDHash[32] = {
0x2D ,0xEA ,0x71 ,0x6F,0x3C,0x66,0x21, 0xB8,
0xE8 ,0x44 ,0xF6 ,0x49,0x9F,0xED,0x44, 0x27,
0x21 ,0x06 ,0x76 ,0xF7,0xFC,0xB7,0xEB, 0x59,
0x09 ,0x25 ,0x6C ,0xB0,0x47,0xBC,0xC7, 0x4E
};
SGD_UINT8 sm3HashData[32];
void myprintf(SGD_UCHAR *pucData, SGD_UINT32 uiDataLen)
{
int i = 0;
for(i = 0; i < uiDataLen; i++)
{
if(i != 0 && i % 4 == 0)
printf(" ");
if(i != 0 && i % 32 == 0)
printf("\n");
printf("%02x", pucData[i]);
}
printf("\n");
}
SGD_RV EccBackUpKeyPair(SGD_HANDLE phSessionHandle)
{
SGD_RV rv = SDR_OK;
SGD_UCHAR eccKeyPair[2048] = {0};
SGD_UINT32 eccKeyPairLen = 2048;
rv = SDF_ECCBackUp(phSessionHandle,1,eccKeyPair,&eccKeyPairLen);
if(SDR_OK != rv)
{
printf("Ecc Back Up failed --- \n");
}
return rv ;
}
SGD_RV ImportKeyPair(SGD_HANDLE phSessionHandle)
{
SGD_RV rv = SDR_OK;
SGD_UCHAR eccKeyPairEnc[96] = {0};
SGD_UINT32 eccKeyPairLen = 96;
memcpy(eccKeyPairEnc, pubKeyEnc, 64);
memcpy(eccKeyPairEnc + 64, priKeyEnc, 32);
rv = SDF_ImportECCKeyPair(phSessionHandle,1,(const SGD_UCHAR *)eccKeyPairEnc);
if(SDR_OK != rv)
{
return rv;
}
return rv;
}
SGD_RV SM1_ENC_DEC_ECB(SGD_HANDLE phSessionHandle,SGD_HANDLE phKeyHandle)
{
SGD_RV rv = SDR_OK;
int loop = LOOP, i = 0;
SGD_UCHAR pucIV[16] ={0};
memset(pucIV, 1, 16);
SGD_UCHAR *pucData = (SGD_UCHAR*)malloc(loop * MAX);
SGD_UINT32 uiDataLength = loop * MAX;
SGD_UCHAR *pucEncData = (SGD_UCHAR*)malloc(loop * MAX);
SGD_UINT32 puiEncDataLength = loop * MAX;
SGD_UCHAR pucKey[16];
memset(pucKey,2,16);
for(i = 0; i < loop * MAX; i++)
{
pucData[i] = i % 256;
}
unsigned long long L1,L2,L3, L4;
struct timeval tv;
gettimeofday(&tv, NULL);
L1 = tv.tv_sec*1000*1000 + tv.tv_usec;
for(i = 0;i < loop; i++)
{
rv = SDF_Encrypt( phSessionHandle,pucKey, SGD_SM1_ECB,pucIV, &pucData[i * MAX], MAX, &pucEncData[i * MAX], &puiEncDataLength);
if(SDR_OK != rv)
{
free(pucData);
free(pucEncData);
return rv;
}
}
gettimeofday(&tv, NULL);
L2 = tv.tv_sec*1000*1000 + tv.tv_usec;
printf("SGD_SM1_ECB Encrypt datasize: %d Bytes used time: %lld us\n",loop * MAX, L2 - L1);
printf("SGD_SM1_ECB Encrypt average speed: %d bps\n", (int)((long long)MAX*loop*8*1000000/(L2 - L1)));
SGD_UCHAR *pucTmpData = (SGD_UCHAR*)malloc(loop * MAX);
memset(pucIV,1,16);
gettimeofday(&tv, NULL);
L3 = tv.tv_sec*1000*1000 + tv.tv_usec;
for(i = 0; i < loop; i++)
{
rv = SDF_Decrypt(phSessionHandle,pucKey, SGD_SM1_ECB, pucIV, &pucEncData[i * MAX], MAX, &pucTmpData[i * MAX], &puiEncDataLength);
if(SDR_OK != rv)
{
free(pucData);
free(pucEncData);
free(pucTmpData);
return rv;
}
}
gettimeofday(&tv, NULL);
L4 = tv.tv_sec*1000*1000 + tv.tv_usec;
printf("SGD_SM1_ECB Decrypt datasize: %d Bytes used time: %lld us\n",loop * MAX, L4 - L3);
printf("SGD_SM1_ECB Decrypt average speed: %d bps\n", (int)((long long)MAX*loop*8*1000000/(L4 - L3)));
if(memcmp(pucData,pucTmpData,loop * MAX))
{
free(pucData);
free(pucEncData);
free(pucTmpData);
printf("memcmp diff\n");
return -1;
}
free(pucData);
free(pucEncData);
free(pucTmpData);
return SDR_OK;
}
SGD_RV SM1_ENC_DEC_CBC(SGD_HANDLE phSessionHandle,SGD_HANDLE phKeyHandle)
{
SGD_RV rv = SDR_OK;
int loop = LOOP, i = 0;
SGD_UCHAR pucIV[16] ={0};
memset(pucIV, 1, 16);
SGD_UCHAR *pucData = (SGD_UCHAR*)malloc(loop * MAX);
SGD_UINT32 uiDataLength = loop * MAX;
SGD_UCHAR *pucEncData = (SGD_UCHAR*)malloc(loop * MAX);
SGD_UINT32 puiEncDataLength = loop * MAX;
SGD_UCHAR pucKey[16];
memset(pucKey,2,16);
for(i = 0; i < loop * MAX; i++)
{
pucData[i] = i % 256;
}
unsigned long long L1,L2,L3, L4;
struct timeval tv;
gettimeofday(&tv, NULL);
L1 = tv.tv_sec*1000*1000 + tv.tv_usec;
for(i = 0;i < loop; i++)
{
rv = SDF_Encrypt( phSessionHandle,pucKey, SGD_SM1_CBC,pucIV, &pucData[i * MAX], MAX, &pucEncData[i * MAX], &puiEncDataLength);
if(SDR_OK != rv)
{
free(pucData);
free(pucEncData);
return rv;
}
}
gettimeofday(&tv, NULL);
L2 = tv.tv_sec*1000*1000 + tv.tv_usec;
printf("SGD_SM1_CBC Encrypt datasize: %d Bytes used time: %lld us\n",loop * MAX, L2 - L1);
printf("SGD_SM1_CBC Encrypt average speed: %d bps\n", (int)((long long)MAX*loop*8*1000000/(L2 - L1)));
SGD_UCHAR *pucTmpData = (SGD_UCHAR*)malloc(loop * MAX);
memset(pucIV,1,16);
gettimeofday(&tv, NULL);
L3 = tv.tv_sec*1000*1000 + tv.tv_usec;
for(i = 0; i < loop; i++)
{
rv = SDF_Decrypt(phSessionHandle,pucKey, SGD_SM1_CBC, pucIV, &pucEncData[i * MAX], MAX, &pucTmpData[i * MAX], &puiEncDataLength);
if(SDR_OK != rv)
{
free(pucData);
free(pucEncData);
free(pucTmpData);
return rv;
}
}
gettimeofday(&tv, NULL);
L4 = tv.tv_sec*1000*1000 + tv.tv_usec;
printf("SGD_SM1_CBC Decrypt datasize: %d Bytes used time: %lld us\n",loop * MAX, L4 - L3);
printf("SGD_SM1_CBC Decrypt average speed: %d bps\n", (int)((long long)MAX*loop*8*1000000/(L4 - L3)));
if(memcmp(pucData,pucTmpData,loop * MAX))
{
free(pucData);
free(pucEncData);
free(pucTmpData);
printf("memcmp diff\n");
return -1;
}
free(pucData);
free(pucEncData);
free(pucTmpData);
return SDR_OK;
}
SGD_RV SM1_ENC_DEC_OFB(SGD_HANDLE phSessionHandle,SGD_HANDLE phKeyHandle)
{
SGD_RV rv = SDR_OK;
int loop = LOOP, i = 0;
SGD_UCHAR pucIV[16] ={0};
memset(pucIV, 1, 16);
SGD_UCHAR *pucData = (SGD_UCHAR*)malloc(loop * MAX);
SGD_UINT32 uiDataLength = loop * MAX;
SGD_UCHAR *pucEncData = (SGD_UCHAR*)malloc(loop * MAX);
SGD_UINT32 puiEncDataLength = loop * MAX;
SGD_UCHAR pucKey[16];
memset(pucKey,2,16);
for(i = 0; i < loop * MAX; i++)
{
pucData[i] = i % 256;
}
unsigned long long L1,L2,L3, L4;
struct timeval tv;
gettimeofday(&tv, NULL);
L1 = tv.tv_sec*1000*1000 + tv.tv_usec;
for(i = 0;i < loop; i++)
{
rv = SDF_Encrypt( phSessionHandle,pucKey, SGD_SM1_OFB,pucIV, &pucData[i * MAX], MAX, &pucEncData[i * MAX], &puiEncDataLength);
if(SDR_OK != rv)
{
free(pucData);
free(pucEncData);
return rv;
}
}
gettimeofday(&tv, NULL);
L2 = tv.tv_sec*1000*1000 + tv.tv_usec;
printf("SGD_SM1_OFB Encrypt datasize: %d Bytes used time: %lld us\n",loop * MAX, L2 - L1);
printf("SGD_SM1_OFB Encrypt average speed: %d bps\n", (int)((long long)MAX*loop*8*1000000/(L2 - L1)));
SGD_UCHAR *pucTmpData = (SGD_UCHAR*)malloc(loop * MAX);
memset(pucIV,1,16);
gettimeofday(&tv, NULL);
L3 = tv.tv_sec*1000*1000 + tv.tv_usec;
for(i = 0; i < loop; i++)
{
rv = SDF_Decrypt(phSessionHandle,pucKey, SGD_SM1_OFB, pucIV, &pucEncData[i * MAX], MAX, &pucTmpData[i * MAX], &puiEncDataLength);
if(SDR_OK != rv)
{
free(pucData);
free(pucEncData);
free(pucTmpData);
return rv;
}
}
gettimeofday(&tv, NULL);
L4 = tv.tv_sec*1000*1000 + tv.tv_usec;
printf("SGD_SM1_OFB Decrypt datasize: %d Bytes used time: %lld us\n",loop * MAX, L4 - L3);
printf("SGD_SM1_OFB Decrypt average speed: %d bps\n", (int)((long long)MAX*loop*8*1000000/(L4 - L3)));
if(memcmp(pucData,pucTmpData,loop * MAX))
{
free(pucData);
free(pucEncData);
free(pucTmpData);
printf("memcmp diff\n");
return -1;
}
free(pucData);
free(pucEncData);
free(pucTmpData);
return SDR_OK;
}
SGD_RV SM4_EncDec(SGD_HANDLE phSessionHandle)
{
SGD_UCHAR pucKey[16] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10};
SGD_UCHAR pucIV[16] = {0}; // 对于 ECB 模式,IV 不使用
SGD_UCHAR data[32] = "20211104dd20211110lyx";
SGD_UCHAR encryptedData[32];
SGD_UCHAR decryptedData[32];
SGD_UINT32 dataLen = 32;
SGD_UINT32 encryptedDataLen = 32;
SGD_UINT32 decryptedDataLen = 32;
SGD_RV rv = SDF_Encrypt(phSessionHandle, pucKey, SGD_SM4_ECB, pucIV, data, dataLen, encryptedData, &encryptedDataLen);
if (rv != SDR_OK)
{
printf("SM4 Encryption failed\n");
return rv;
}
printf("Encrypted data: ");
myprintf(encryptedData, encryptedDataLen);
rv = SDF_Decrypt(phSessionHandle, pucKey, SGD_SM4_ECB, pucIV, encryptedData, encryptedDataLen, decryptedData, &decryptedDataLen);
if (rv != SDR_OK)
{
printf("SM4 Decryption failed\n");
return rv;
}
printf("Decrypted data: ");
myprintf(decryptedData, decryptedDataLen);
if (memcmp(data, decryptedData, dataLen) == 0)
{
printf("SM4 Encryption and Decryption are successful and match original data.\n");
}
else
{
printf("Data mismatch after decryption.\n");
}
return SDR_OK;
}
SGD_RV SM4_ENC_DEC_CBC(SGD_HANDLE phSessionHandle,SGD_HANDLE phKeyHandle)
{
SGD_RV rv = SDR_OK;
int loop = LOOP, i = 0;
SGD_UCHAR pucIV[16] ={0};
memset(pucIV, 1, 16);
SGD_UCHAR *pucData = (SGD_UCHAR*)malloc(loop * MAX);
SGD_UINT32 uiDataLength = loop * MAX;
SGD_UCHAR *pucEncData = (SGD_UCHAR*)malloc(loop * MAX);
SGD_UINT32 puiEncDataLength = loop * MAX;
SGD_UCHAR pucKey[16];
memset(pucKey,2,16);
int count = loop;
for(i = 0; i < loop * MAX; i++)
{
pucData[i] = i % 256;
}
unsigned long long L1,L2,L3, L4;
struct timeval tv;
gettimeofday(&tv, NULL);
L1 = tv.tv_sec*1000*1000 + tv.tv_usec;
for(i = 0;i < loop; i++)
{
rv = SDF_Encrypt( phSessionHandle,pucKey, SGD_SM4_CBC,pucIV, &pucData[i * MAX], MAX, &pucEncData[i * MAX], &puiEncDataLength);
if(SDR_OK != rv)
{
free(pucData);
free(pucEncData);
return rv;
}
}
gettimeofday(&tv, NULL);
L2 = tv.tv_sec*1000*1000 + tv.tv_usec;
printf("SGD_SM4_CBC Encrypt datasize: %d Bytes used time: %lld us\n",loop * MAX, L2 - L1);
printf("SGD_SM4_CBC Encrypt average speed: %d bps\n", (int)((long long)MAX*loop*8*1000000/(L2 - L1)));
SGD_UCHAR *pucTmpData = (SGD_UCHAR*)malloc(loop * MAX);
memset(pucIV,1,16);
gettimeofday(&tv, NULL);
L3 = tv.tv_sec*1000*1000 + tv.tv_usec;
for(i = 0; i < loop; i++)
{
rv = SDF_Decrypt(phSessionHandle,pucKey, SGD_SM4_CBC, pucIV, &pucEncData[i * MAX], MAX, &pucTmpData[i * MAX], &puiEncDataLength);
if(SDR_OK != rv)
{
free(pucData);
free(pucEncData);
free(pucTmpData);
return rv;
}
}
gettimeofday(&tv, NULL);
L4 = tv.tv_sec*1000*1000 + tv.tv_usec;
printf("SGD_SM4_CBC Decrypt datasize: %d Bytes used time: %lld us\n",loop * MAX, L4 - L3);
printf("SGD_SM4_CBC Decrypt average speed: %d bps\n", (int)((long long)MAX*loop*8*1000000/(L4 - L3)));
if(memcmp(pucData,pucTmpData,loop * MAX))
{
free(pucData);
free(pucEncData);
free(pucTmpData);
printf("memcmp diff\n");
return -1;
}
free(pucData);
free(pucEncData);
free(pucTmpData);
return SDR_OK;
}
SGD_RV SM4_ENC_DEC_OFB(SGD_HANDLE phSessionHandle,SGD_HANDLE phKeyHandle)
{
SGD_RV rv = SDR_OK;
int loop = LOOP, i = 0;
SGD_UCHAR pucIV[16] ={0};
memset(pucIV, 1, 16);
SGD_UCHAR *pucData = (SGD_UCHAR*)malloc(loop * MAX);
SGD_UINT32 uiDataLength = loop * MAX;
SGD_UCHAR *pucEncData = (SGD_UCHAR*)malloc(loop * MAX);
SGD_UINT32 puiEncDataLength = loop * MAX;
SGD_UCHAR pucKey[16];
memset(pucKey,2,16);
int count = loop;
for(i = 0; i < loop * MAX; i++)
{
pucData[i] = i % 256;
}
unsigned long long L1,L2,L3, L4;
struct timeval tv;
gettimeofday(&tv, NULL);
L1 = tv.tv_sec*1000*1000 + tv.tv_usec;
for(i = 0;i < loop; i++)
{
rv = SDF_Encrypt( phSessionHandle,pucKey, SGD_SM4_OFB,pucIV, &pucData[i * MAX], MAX, &pucEncData[i * MAX], &puiEncDataLength);
if(SDR_OK != rv)
{
free(pucData);
free(pucEncData);
return rv;
}
}
gettimeofday(&tv, NULL);
L2 = tv.tv_sec*1000*1000 + tv.tv_usec;
printf("SGD_SM4_OFB Encrypt datasize: %d Bytes used time: %lld us\n",loop * MAX, L2 - L1);
printf("SGD_SM4_OFB Encrypt average speed: %d bps\n", (int)((long long)MAX*loop*8*1000000/(L2 - L1)));
SGD_UCHAR *pucTmpData = (SGD_UCHAR*)malloc(loop * MAX);
memset(pucIV,1,16);
gettimeofday(&tv, NULL);
L3 = tv.tv_sec*1000*1000 + tv.tv_usec;
for(i = 0; i < loop; i++)
{
rv = SDF_Decrypt(phSessionHandle,pucKey, SGD_SM4_OFB, pucIV, &pucEncData[i * MAX], MAX, &pucTmpData[i * MAX], &puiEncDataLength);
if(SDR_OK != rv)
{
free(pucData);
free(pucEncData);
free(pucTmpData);
return rv;
}
}
gettimeofday(&tv, NULL);
L4 = tv.tv_sec*1000*1000 + tv.tv_usec;
printf("SGD_SM4_OFB Decrypt datasize: %d Bytes used time: %lld us\n",loop * MAX, L4 - L3);
printf("SGD_SM4_OFB Decrypt average speed: %d bps\n", (int)((long long)MAX*loop*8*1000000/(L4 - L3)));
if(memcmp(pucData,pucTmpData,loop * MAX))
{
free(pucData);
free(pucEncData);
free(pucTmpData);
printf("memcmp diff\n");
return -1;
}
free(pucData);
free(pucEncData);
free(pucTmpData);
return SDR_OK;
}
SGD_RV SM1_ENC_DEC_IPSEC(SGD_HANDLE phSessionHandle,SGD_HANDLE phKeyHandle)
{
SGD_RV rv = SDR_OK;
int loop = LOOP, i = 0;
SGD_UCHAR pucIV[16] ={0};
memset(pucIV, 1, 16);
SGD_UCHAR HMACKey[32] ={0};
memset(HMACKey, 3, 32);
SGD_UINT32 HMACKeyLen = 32;
SGD_UCHAR *pucData = (SGD_UCHAR*)malloc(loop * MAX);
SGD_UINT32 uiDataLength = loop * MAX;
SGD_UCHAR *pucEncData = (SGD_UCHAR*)malloc(loop * MAX);
SGD_UINT32 puiEncDataLength = loop * MAX;
SGD_UCHAR *pucMacData = (SGD_UCHAR*)malloc(loop * 32);
SGD_UINT32 puiMacDataLength = loop * 32;
SGD_UCHAR pucKey[16];
memset(pucKey,2,16);
SGD_UCHAR sendTmpbuf[5000] = {0};
SGD_UINT32 sendTmpbufLen = 5000;
SGD_UCHAR recvTmpbuf[5000] = {0};
SGD_UINT32 recvTmpbufLen = 5000;
int count = loop;
memset(sendTmpbuf, 4, 24);
for(i = 0; i < loop * MAX; i++)
{
pucData[i] = i % 256;
}
unsigned long long L1,L2,L3, L4;
struct timeval tv;
gettimeofday(&tv, NULL);
L1 = tv.tv_sec*1000*1000 + tv.tv_usec;
for(i = 0; i < loop; i++)
{
memcpy(sendTmpbuf + 24, pucData + i * MAX, MAX);
rv = SDF_Encrypt_IPSEC( phSessionHandle,pucKey, SGD_IPSEC_SM1, pucIV, HMACKey, HMACKeyLen, sendTmpbuf, 24 + MAX, recvTmpbuf, &recvTmpbufLen);
if(SDR_OK != rv)
{
free(pucData);
free(pucEncData);
free(pucMacData);
return rv;
}
memcpy(pucEncData + i * MAX, recvTmpbuf + 24, MAX);
memcpy(pucMacData + i * 32, recvTmpbuf + 24 + MAX, 32);
}
gettimeofday(&tv, NULL);
L2 = tv.tv_sec*1000*1000 + tv.tv_usec;
printf("SGD_IPSEC_SM1 Encrypt datasize: %d Bytes used time: %lld us\n",count * (MAX + 24), L2 - L1);
printf("SGD_IPSEC_SM1 Encrypt average speed: %d bps\n", (int)((long long)(MAX + 24)*count*8*1000000/(L2 - L1)));
SGD_UCHAR *pucTmpData = (SGD_UCHAR*)malloc(loop * MAX);
SGD_UCHAR *pucTmpMacData = (SGD_UCHAR*)malloc(loop * 32);
memset(pucIV,1,16);
loop = count;
gettimeofday(&tv, NULL);
L3 = tv.tv_sec*1000*1000 + tv.tv_usec;
for(i = 0; i < loop; i++)
{
memcpy(sendTmpbuf + 24, pucEncData + i * MAX, MAX);
rv = SDF_Decrypt_IPSEC(phSessionHandle,pucKey, SGD_IPSEC_SM1, pucIV, HMACKey, HMACKeyLen, sendTmpbuf, 24 + MAX, recvTmpbuf, &recvTmpbufLen);
if(SDR_OK != rv)
{
free(pucData);
free(pucEncData);
free(pucMacData);
free(pucTmpData);
free(pucTmpMacData);
return rv;
}
memcpy(pucTmpData + i * MAX, recvTmpbuf + 24, MAX);
memcpy(pucTmpMacData + i * 32, recvTmpbuf + 24 + MAX, 32);
}
gettimeofday(&tv, NULL);
L4 = tv.tv_sec*1000*1000 + tv.tv_usec;
printf("SGD_IPSEC_SM1 Decrypt datasize: %d Bytes used time: %lld us\n",count * (MAX + 24), L4 - L3);
printf("SGD_IPSEC_SM1 Decrypt average speed: %d bps\n", (int)((long long)(MAX + 24) * count * 8 * 1000000/(L4 - L3)));
if(memcmp(pucData,pucTmpData,count * MAX))
{
free(pucData);
free(pucEncData);
free(pucMacData);
free(pucTmpData);
free(pucTmpMacData);
printf("pucData pucTmpData memcmp diff\n");
return -1;
}
if(memcmp(pucMacData,pucTmpMacData,count * 32))
{
free(pucData);
free(pucEncData);
free(pucMacData);
free(pucTmpData);
free(pucTmpMacData);
printf("pucMacData pucTmpMacData memcmp diff\n");
return -1;
}
free(pucData);
free(pucEncData);
free(pucMacData);
free(pucTmpData);
free(pucTmpMacData);
return SDR_OK;
}
SGD_RV SM4_ENC_DEC_IPSEC(SGD_HANDLE phSessionHandle,SGD_HANDLE phKeyHandle)
{
SGD_RV rv = SDR_OK;
int loop = LOOP, i = 0;
SGD_UCHAR pucIV[16] ={0};
memset(pucIV, 1, 16);
SGD_UCHAR HMACKey[32] ={0};
memset(HMACKey, 3, 32);
SGD_UINT32 HMACKeyLen = 32;
SGD_UCHAR *pucData = (SGD_UCHAR*)malloc(loop * MAX);
SGD_UINT32 uiDataLength = loop * MAX;
SGD_UCHAR *pucEncData = (SGD_UCHAR*)malloc(loop * MAX);
SGD_UINT32 puiEncDataLength = loop * MAX;
SGD_UCHAR *pucMacData = (SGD_UCHAR*)malloc(loop * 32);
SGD_UINT32 puiMacDataLength = loop * 32;
SGD_UCHAR pucKey[16];
memset(pucKey,2,16);
SGD_UCHAR sendTmpbuf[5000] = {0};
SGD_UINT32 sendTmpbufLen = 5000;
SGD_UCHAR recvTmpbuf[5000] = {0};
SGD_UINT32 recvTmpbufLen = 5000;
int count = loop;
memset(sendTmpbuf, 4, 24);
for(i = 0; i < loop * MAX; i++)
{
pucData[i] = i / 256;
}
unsigned long long L1,L2,L3, L4;
struct timeval tv;
gettimeofday(&tv, NULL);
L1 = tv.tv_sec*1000*1000 + tv.tv_usec;
for(i = 0; i < loop; i++)
{
memcpy(sendTmpbuf + 24, pucData + i * MAX, MAX);
rv = SDF_Encrypt_IPSEC( phSessionHandle,pucKey, SGD_IPSEC_SM4, pucIV, HMACKey, HMACKeyLen, sendTmpbuf, 24 + MAX, recvTmpbuf, &recvTmpbufLen);
if(SDR_OK != rv)
{
free(pucData);
free(pucEncData);
free(pucMacData);
return rv;
}
memcpy(pucEncData + i * MAX, recvTmpbuf + 24, MAX);
memcpy(pucMacData + i * 32, recvTmpbuf + 24 + MAX, 32);
}
gettimeofday(&tv, NULL);
L2 = tv.tv_sec*1000*1000 + tv.tv_usec;
printf("SGD_IPSEC_SM4 Encrypt datasize: %d Bytes used time: %lld us\n",count * (MAX + 24), L2 - L1);
printf("SGD_IPSEC_SM4 Encrypt average speed: %d bps\n", (int)((long long)(MAX + 24)*count*8*1000000/(L2 - L1)));
SGD_UCHAR *pucTmpData = (SGD_UCHAR*)malloc(loop * MAX);
SGD_UCHAR *pucTmpMacData = (SGD_UCHAR*)malloc(loop * 32);
memset(pucIV,1,16);
loop = count;
gettimeofday(&tv, NULL);
L3 = tv.tv_sec*1000*1000 + tv.tv_usec;
for(i = 0; i < loop; i++)
{
memcpy(sendTmpbuf + 24, pucEncData + i * MAX, MAX);
rv = SDF_Decrypt_IPSEC(phSessionHandle,pucKey, SGD_IPSEC_SM4, pucIV, HMACKey, HMACKeyLen, sendTmpbuf, 24 + MAX, recvTmpbuf, &recvTmpbufLen);
if(SDR_OK != rv)
{
free(pucData);
free(pucEncData);
free(pucMacData);
free(pucTmpData);
free(pucTmpMacData);
return rv;
}
memcpy(pucTmpData + i * MAX, recvTmpbuf + 24, MAX);
memcpy(pucTmpMacData + i * 32, recvTmpbuf + 24 + MAX, 32);
}
gettimeofday(&tv, NULL);
L4 = tv.tv_sec*1000*1000 + tv.tv_usec;
printf("SGD_IPSEC_SM4 Decrypt datasize: %d Bytes used time: %lld us\n",count * (MAX + 24), L4 - L3);
printf("SGD_IPSEC_SM4 Decrypt average speed: %d bps\n", (int)((long long)(MAX + 24) * count * 8 * 1000000/(L4 - L3)));
if(memcmp(pucData,pucTmpData,count * MAX))
{
free(pucData);
free(pucEncData);
free(pucMacData);
free(pucTmpData);
free(pucTmpMacData);
printf("pucData pucTmpData memcmp diff\n");
return -1;
}
if(memcmp(pucMacData,pucTmpMacData,count * 32))
{
free(pucData);
free(pucEncData);
free(pucMacData);
free(pucTmpData);
free(pucTmpMacData);
printf("pucMacData pucTmpMacData memcmp diff\n");
return -1;
}
free(pucData);
free(pucEncData);
free(pucMacData);
free(pucTmpData);
free(pucTmpMacData);
return SDR_OK;
}
SGD_RV SGD_SM3Hash(SGD_HANDLE phSessionHandle)
{
SGD_RV rv = SDR_OK;
char *memberData = "20211104窦豆20211110梁艺轩"; // 小组成员学号和姓名
SGD_UINT32 dataLen = strlen(memberData);
rv = SDF_HashInit(phSessionHandle, SGD_SM3, NULL, NULL, 0);
if(SDR_OK != rv)
{
return rv;
}
rv = SDF_HashUpdate(phSessionHandle, (SGD_UCHAR*)memberData, dataLen);
if(SDR_OK != rv)
{
return rv;
}
rv = SDF_HashFinal(phSessionHandle, sm3HashData, &dataLen);
if(SDR_OK != rv)
{
return rv;
}
printf("SM3 Hash of group members' info: ");
myprintf(sm3HashData, dataLen);
return SDR_OK;
}
SGD_RV SM2EncDec(SGD_HANDLE phSessionHandle)
{
SGD_RV rv = SDR_OK;
SGD_UCHAR pucData[32] ={0};
SGD_UINT32 uiDataLen = sizeof(pucData);
memset(pucData,0x05,sizeof(pucData));
ECCCipher Cipher;
rv =SDF_InternalEncrypt_ECC(phSessionHandle, 1, SGD_SM2_3, pucData, uiDataLen, &Cipher);
if(SDR_OK != rv)
{
printf("SDF_InternalEncrypt_ECC failed rv = %08x\n", rv);
return rv;
}
SGD_UCHAR pucDecData[1025] ={0};
SGD_UINT32 uiDecDataLen = sizeof(pucDecData);
rv = SDF_InternalDecrypt_ECC(phSessionHandle,1,SGD_SM2_3,&Cipher,pucDecData,&uiDecDataLen);
if(SDR_OK != rv)
{
printf("SDF_InternalDecrypt_ECC failed rv = %08x\n", rv);
return rv;
}
if(memcmp(pucData,pucDecData,uiDecDataLen))
{
printf("memcpy diff \n");
return -1;
}
return SDR_OK;
}
SGD_RV SM2SignVer(SGD_HANDLE phSessionHandle)
{
SGD_RV rv = SDR_OK;
#define COUNT 100
ECCSignature Signature ;
int i = 0;
unsigned long long L1,L2,L3,L4;
struct timeval tv;
gettimeofday(&tv, NULL);
L1 = tv.tv_sec*1000*1000 + tv.tv_usec;
for(i = 0; i < COUNT; i++)
{
rv = SDF_InternalSign_ECC(phSessionHandle,1,sm3HashData,32,&Signature);
if(SDR_OK != rv)
{
printf("SDF_InternalSign_ECC failed rv = 0x%08x\n", rv);
return rv;
}
}
gettimeofday(&tv, NULL);
L2 = tv.tv_sec*1000*1000 + tv.tv_usec;
printf("SM2-Sign Times: %d 次; Spent time: %lld us\n",COUNT, L2 - L1);
printf("SM2-Sign average speed: %d us/次\n", (int)((L2 - L1)/(long long)COUNT));
gettimeofday(&tv, NULL);
L3 = tv.tv_sec*1000*1000 + tv.tv_usec;
for(i = 0; i < COUNT; i++)
{
rv = SDF_InternalVerify_ECC(phSessionHandle,1,sm3HashData,32,&Signature);
if(SDR_OK != rv)
{
printf("SDF_InternalVerify_ECC failed rv = 0x%08x\n", rv);
return rv;
}
}
gettimeofday(&tv, NULL);
L4 = tv.tv_sec*1000*1000 + tv.tv_usec;
printf("SM2-Verify Times: %d 次; Spent time: %lld us\n",COUNT, L4 - L3);
printf("SM2-Verify average speed: %d us/次\n", (int)((L4 - L3)/(long long)COUNT));
return SDR_OK;
}
SGD_RV ExportKeyPair(SGD_HANDLE phSessionHandle)
{
SGD_RV rv = SDR_OK;
SGD_UCHAR pucPubKey[64];
rv = SDF_ExportECCPubKey(phSessionHandle,1,pucPubKey);
if(SDR_OK != rv)
{
return rv;
}
if(memcmp(pucPubKey,pubKey,64))
{
printf("pubKey diff \n");
return -1;
}
SGD_UCHAR pucPriKey[32];
rv = SDF_ExportECCPriKey(phSessionHandle,1,pucPriKey);
if(SDR_OK != rv)
{
return rv;
}
SGD_UCHAR pucIV[16] ={0};
memset(pucIV, 0, 16);
SGD_UCHAR pucEncData[100] = {0};
SGD_UINT32 puiEncDataLength = 100;
rv = SDF_Encrypt(phSessionHandle,(SGD_UINT8 *)ROOTKEY, SGD_SM1_CBC,pucIV, priKey, 32, pucEncData, &puiEncDataLength);
if(SDR_OK != rv)
{
return rv;
}
if(memcmp(pucPriKey,pucEncData,32))
{
printf("priKey diff \n");
return -1;
}
return SDR_OK;
}
SGD_RV SM1_CBC(SGD_HANDLE phSessionHandle,SGD_HANDLE phKeyHandle)
{
SGD_RV rv = SDR_OK;
SGD_UCHAR pucIV[16] ={0};
memset(pucIV, 0, 16);
SGD_UCHAR pucData[64] = {
0x8B, 0x7F, 0xB9, 0x6C, 0x09, 0x53, 0x27, 0x19, 0xE4, 0xEB, 0x79, 0x16, 0xC6, 0x2E, 0x20, 0xEA,
0x33, 0xCC, 0x98, 0x96, 0x43, 0xFA, 0x45, 0x09, 0x0C, 0x98, 0x4E, 0xA5, 0xDF, 0x76, 0xA1, 0xD4,
0xC1, 0xF8, 0x9C, 0x46, 0x67, 0x61, 0xCE, 0x07, 0x83, 0x26, 0xAF, 0x1C, 0xA0, 0x81, 0xC5, 0x89,
0x4E, 0x0C, 0xD5, 0x29, 0x88, 0x40, 0x96, 0x45, 0x50, 0xD9, 0x14, 0x17, 0xB7, 0x5C, 0xC5, 0x5E
};
SGD_UINT32 uiDataLength = sizeof(pucData);
SGD_UCHAR pucEncData[64] = { 0 };
SGD_UINT32 puiEncDataLength = sizeof(pucEncData);
SGD_UCHAR pucKey[16] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10};
rv = SDF_Encrypt( phSessionHandle,pucKey, SGD_SM1_CBC,pucIV, pucData, uiDataLength, pucEncData, &puiEncDataLength);
if(SDR_OK != rv)
{
return rv;
}
printf("pucEncData:\n");
myprintf(pucEncData, puiEncDataLength);
SGD_UCHAR pucTmpData[64] = { 0 };
memset(pucIV,0,16);
rv = SDF_Decrypt(phSessionHandle,pucKey, SGD_SM1_CBC, pucIV, pucEncData, puiEncDataLength, pucTmpData, &puiEncDataLength);
if(SDR_OK != rv)
{
return rv;
}
if(memcmp(pucData,pucTmpData,64))
{
printf("memcmp diff\n");
return -1;
}
return SDR_OK;
}
int main(int argc, char *argv[]) {
SGD_HANDLE phDeviceHandle;
SGD_HANDLE phSessionHandle;
SGD_RV rv;
// 学号姓名
char *student_info = "学号姓名:\n20211104 窦豆\n20211110 梁艺轩";
printf("%s\n\n", student_info);
// 打开设备
rv = SDF_OpenDevice(&phDeviceHandle);
if (rv != SDR_OK) {
printf("Open device failed\n");
return -1;
}
// 开启会话
rv = SDF_OpenSession(phDeviceHandle, &phSessionHandle);
if (rv != SDR_OK) {
printf("Open session failed\n");
SDF_CloseDevice(phDeviceHandle);
return -1;
}
// 哈希计算
printf("Performing SM3 hash computation...\n");
rv = SGD_SM3Hash(phSessionHandle);
if (rv != SDR_OK) {
printf("SM3 hash computation failed\n");
} else {
printf("SM3 hash computation success\n");
}
// SM4 加解密
printf("Performing SM4 encryption and decryption...\n");
rv = SM4_EncDec(phSessionHandle);
if (rv != SDR_OK) {
printf("SM4 Encryption and Decryption failed\n");
} else {
printf("SM4 Encryption and Decryption success\n");
}
// 关闭会话和设备
SDF_CloseSession(phSessionHandle);
SDF_CloseDevice(phDeviceHandle);
return 0;
}
标签:rv,tv,MAX,接口,sdf,引擎,free,SGD,loop
From: https://www.cnblogs.com/20211110lyx/p/18140401