加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.cpp 9.22 KB
一键复制 编辑 原始数据 按行查看 历史
LMK 提交于 2023-12-08 19:20 . 第一次提交
#include "ndsec_sdf_client.h"
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
sdf_uint32_t rv;
sdf_uint32_t rv2;
sdf_handle_t hSessionHandle;
sdf_handle_t hDeviceHandle;
#define MAXNUMBER (sdf_uint32_t)1024
void sm1AndSm4Test();
void sm2Test();
void sm3Test();
int filterChar(char *p){
if ((*p >= 'a') && (*p <= 'f')) //当前字符为小写字母a~z时
return *p - 'a' + 10 ; //转为十六进制
else if ((*p >= 'A') && (*p <= 'F')) //当前字符为大写字母A~Z时
return *p - 'A' + 10;//转为十六进制
else if ((*p >= '0') && (*p <= '9')) //当前字符为数字0~9时
return *p - '0';//转为十六进制
else{
return -1;
}
}
int StringToHex(char *str, unsigned char *strhex)
{
int cnt = 0;
char *p = str; //直针p初始化为指向str
int len = strlen(str); //获取字符串中的字符个数
char b1,b2;
while(*p != '\0') { //结束符判断
if (*p == '\"'){
p++;
continue;
}
b1 = filterChar(p);
b2 = filterChar(p+1);
strhex[cnt] = b1 * 16 + b2;
cnt++;
p = p + 2;
}
return cnt;
}
int main() {
//开启设备
rv = SDF_OpenDevice(&hDeviceHandle);
if(rv != 0) {
printf("openDevice fail %08x \n",rv);
} else {
printf("openDevice right \n");
sm1AndSm4Test();
sm2Test();
sm3Test();
}
}
//SM1 AND SM4 test
void sm1AndSm4Test() {
sdf_uint8_t pucData_h[16];
sdf_uint32_t uiDataLength = 16;
sdf_handle_t hKeyHandle;
sdf_uint8_t cipherData[1024], decData[1024];
sdf_uint32_t cipherLength = 0,tempdatalength = 0;
sdf_uint8_t keyData[16];
sdf_uint32_t keyLength = 16;
sdf_uint8_t ivData[16];
sdf_uint32_t ivLength = 16;
//SM1 AND SM4 encryption and decryption
sdf_uint32_t sm1AndSm4UiAlgID[8] = {SGD_SM1_ECB, SGD_SM1_CBC, SGD_SM1_CFB, SGD_SM1_OFB,SGD_SM4_ECB,SGD_SM4_CBC,SGD_SM4_CFB,SGD_SM4_OFB};
char *sm1AndSm4UiAlgIDname[8] = {"SGD_SM1_ECB encryption and decryption right", "SGD_SM1_CBC encryption and decryption right", "SGD_SM1_CFB encryption and decryption right", "SGD_SM1_OFB encryption and decryption right","SGD_SM4_ECB encryption and decryption right","SGD_SM4_CBC encryption and decryption right","SGD_SM4_CFB encryption and decryption right","SGD_SM4_OFB encryption and decryption right"};
//SM1 AND SM4 MAC
sdf_uint32_t sm1AndSm4MacUiAlgID[2] = {SGD_SM1_MAC, SGD_SM4_MAC};
char *sm1AndSm4MacUiAlgIDname[2] = {"SGD_SM1_MAC right", "SGD_SM4_MAC right"};
uint8_t output_data[16]{};
sdf_uint32_t output_length;
rv = SDF_OpenSession(hDeviceHandle, &hSessionHandle);
if(rv != 0) {
printf("openSession fail %08x \n", rv);
} else {
printf("openSession right \n");
rv = SDF_GenerateRandom(hSessionHandle, uiDataLength, pucData_h);
if (rv != 0) {
printf("generateRandom fail %08x \n", rv);
} else {
printf("generateRandom right \n");
}
rv = SDF_GenerateRandom(hSessionHandle, keyLength, keyData);
if(rv != 0) {
printf("getKey fail %08x \n", rv);
} else {
printf("getKey right \n");
}
rv = SDF_GenerateRandom(hSessionHandle, ivLength, ivData);
if(rv != 0) {
printf("getIv fail %08x \n", rv);
} else {
printf("getIv right \n");
}
rv = SDF_ImportKey(hSessionHandle, keyData, keyLength, &hKeyHandle);
if(rv != 0) {
printf("importKey fail %08x \n", rv);
}
for(int i=0;i<8;i++) {
rv = SDF_Encrypt(hSessionHandle, hKeyHandle, sm1AndSm4UiAlgID[i], ivData, pucData_h, uiDataLength, cipherData, &cipherLength);
rv2 = SDF_Decrypt(hSessionHandle, hKeyHandle, sm1AndSm4UiAlgID[i], ivData, cipherData, cipherLength, decData, &tempdatalength);
if (rv != 0 && rv2 != 0) {
printf("sm1 encryption and decryption fail %08x\n",rv);
} else {
printf("%s\n",sm1AndSm4UiAlgIDname[i]);
}
}
for(int i=0;i<2;i++) {
rv = SDF_CalculateMAC(hSessionHandle, hKeyHandle, sm1AndSm4MacUiAlgID[i],
ivData, pucData_h, ivLength,output_data,
&output_length);
if (rv != 0) {
printf("sm1 mac fail %08x\n",rv);
} else {
printf("%s\n",sm1AndSm4MacUiAlgIDname[i]);
}
}
rv = SDF_CloseSession(hSessionHandle);
if(rv != 0) {
printf("closeSession fail %08x\n",rv);
}
}
}
//SM2
void sm2Test(){
sdf_uint8_t pucData_h[16];
sdf_uint32_t uiDataLength = 16;
ECCrefPublicKey public_key{};
ECCrefPrivateKey private_key{};
sdf_uint8_t *HexRandomData = new sdf_uint8_t[2048];
sdf_uint8_t *ReslutData = new sdf_uint8_t[2048];
sdf_uint32_t reslength;
int dataLength = 16;
ECCCipher *cipher = new ECCCipher[dataLength];
rv = SDF_OpenSession(hDeviceHandle, &hSessionHandle);
if (rv != 0) {
printf("openSession fail %08x \n",rv);
} else {
rv = SDF_GenerateRandom(hSessionHandle, uiDataLength, pucData_h);
if (rv != 0) {
printf("generateRandom fail %08x \n", rv);
}
rv = SDF_GenerateKeyPair_ECC(hSessionHandle, SGD_SM2_3, 256, &public_key, &private_key);
if (rv != 0) {
printf("generateKeyPair_ECC fail %08x\n", rv);
} else {
printf("generateKeyPair_ECC right\n");
}
rv = SDF_ExternalEncrypt_ECC(hSessionHandle, SGD_SM2_3, &public_key, HexRandomData, dataLength, cipher);
if (rv != 0) {
printf("SGD_SM2 encryption fail %08x\n", rv);
}else{
printf("SGD_SM2 encryption right \n");
}
rv = SDF_ExternalDecrypt_ECC(hSessionHandle, SGD_SM2_3, &private_key, cipher, ReslutData, &reslength);
if (rv != 0) {
printf("SGD_SM2 decryption fail %08x\n", rv);
}else{
printf("SGD_SM2 decryption right \n");
}
rv = SDF_CloseSession(hSessionHandle);
if(rv != 0) {
printf("closeSession fail %08x\n",rv);
}
}
}
//SM3
void sm3Test() {
int id;
sdf_uint32_t caseId;
sdf_uint32_t uiAlgld;
sdf_uint32_t eccpuckeybits;
char eccpuckeyX[ECCref_MAX_LEN*2+1];
sdf_uint8_t eccpuckeyX_h[ECCref_MAX_LEN];
char eccpuckeyY[ECCref_MAX_LEN*2+1];
sdf_uint8_t eccpuckeyY_h[ECCref_MAX_LEN];
char ucID[MAXNUMBER];
sdf_uint8_t ucID_h[MAXNUMBER];
sdf_uint32_t uiIDLength;
char pucData[MAXNUMBER];
sdf_uint8_t pucData_h[MAXNUMBER];
sdf_uint32_t uiDataLength;
char pucHash[MAXNUMBER];
sdf_uint8_t pucHash_h[MAXNUMBER];
sdf_uint32_t puiHashLength;
sdf_uint32_t hashbuffer = 128;
sdf_uint8_t hashResult[hashbuffer];
sdf_uint32_t hashResultLength;
sdf_uint32_t test_num;
memset(eccpuckeyX,0,129);
memset(eccpuckeyY,0,129);
memset(eccpuckeyX_h,0,64);
memset(eccpuckeyY_h,0,64);
rv = SDF_OpenSession(hDeviceHandle, &hSessionHandle);
if (rv != SDR_OK) {
printf("SDF_OpenSession failed, rv[0x%08x]\n", rv);
}
uiAlgld = 1;
memcpy(eccpuckeyX,"cb24ee87ab4b8e535090ed211d18ed4023255996217346b66544ed8edf1ed821",
strlen("cb24ee87ab4b8e535090ed211d18ed4023255996217346b66544ed8edf1ed821"));
memcpy(eccpuckeyY, "7df218dc72c410da679fe34bd5c33dddabdb13cf7a4c8f545e094f9688dd8edb",
strlen("7df218dc72c410da679fe34bd5c33dddabdb13cf7a4c8f545e094f9688dd8edb"));
memcpy(ucID, "21561ce746720e807d9786bb8ad7ed02", strlen("21561ce746720e807d9786bb8ad7ed02"));
uiIDLength = 16;
memcpy(pucData, "81e8b8a3f3d26ef28aa85653ccbd7ef7", strlen("81e8b8a3f3d26ef28aa85653ccbd7ef7"));
memcpy(pucHash, "0ea7481582d39174dc1d3540dfca0b610e1ca040e6e07e2d78d5c8e5cb35732f",
strlen("0ea7481582d39174dc1d3540dfca0b610e1ca040e6e07e2d78d5c8e5cb35732f"));
uiDataLength = 16;
puiHashLength = 32;
int i;
ECCrefPublicKey eccpublicKey;
StringToHex(eccpuckeyX, eccpuckeyX_h);
StringToHex(eccpuckeyY, eccpuckeyY_h);
StringToHex(ucID, ucID_h);
StringToHex(pucData, pucData_h);
StringToHex(pucHash, pucHash_h);
eccpublicKey.bits = 256;
memcpy(eccpublicKey.x, eccpuckeyX_h, ECCref_MAX_LEN);
memcpy(eccpublicKey.y, eccpuckeyY_h, ECCref_MAX_LEN);
for(i =0;i<32;i++){
eccpublicKey.x[i+32]=eccpublicKey.x[i];
eccpublicKey.y[i+32]=eccpublicKey.y[i];
eccpublicKey.x[i]=0;
eccpublicKey.y[i]=0;
}
rv = SDF_OpenSession(hDeviceHandle, &hSessionHandle);
if (rv != 0) {
printf("openSession fail %08x \n",rv);
} else {
rv = SDF_HashInit(hSessionHandle, uiAlgld, &eccpublicKey, ucID_h, uiIDLength);
if (rv != SDR_OK){
printf("SDF_HashInit failed, rv[0x%08x]\n", rv);
}
rv = SDF_HashUpdate(hSessionHandle, pucData_h, uiDataLength);
if (rv != SDR_OK){
printf("SDF_HashUpdate failed, rv[%08x]\n",rv);
}
rv = SDF_HashFinal(hSessionHandle, hashResult, &hashResultLength);
if (rv != SDR_OK){
printf("SDF_HashFinal failed, rv[%08x]\n",rv);
} else {
printf("SGD_SM3_Hash Test right\n");
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化