#define PRIVATE_MAX_LINE_LENGTH 65
#define RAND_MAX_LINE_LENGTH 97
int main() {
FILE* privateKeyfile;
char privateKeyLine[PRIVATE_MAX_LINE_LENGTH];
int arrayLength = 32;
unsigned char expPrivateKey[32] = { 0 };
FILE* randfile;
char randLine[RAND_MAX_LINE_LENGTH];
unsigned char expRand[48] = { 0 };
// 打开文件
privateKeyfile = fopen("D:\\tpmtest\\privateKey.txt", "r");
if (privateKeyfile == NULL) {
printf("无法打开文件\n");
return 1;
}
randfile = fopen("D:\\tpmtest\\rand.txt", "r");
if (randfile == NULL) {
printf("无法打开文件\n");
return 1;
}
printf("\n");
CryptRandStartup();
// 创建线程
for (int i = 0; i < 900; i++)
{
PRIVATE_DATA* ptr = (PRIVATE_DATA*)malloc(sizeof(PRIVATE_DATA));
printf("ptr = %p \n",ptr);
fgets(privateKeyLine, PRIVATE_MAX_LINE_LENGTH, privateKeyfile);
printf("privateKeyLine:\n");
printf("%s\n", privateKeyLine);
for (int j = 0; j < arrayLength; j++) {
sscanf_s(&privateKeyLine[j * 2], "%2hhx", &ptr->expPrivateKey[j]);
}
memset(privateKeyLine, 0, PRIVATE_MAX_LINE_LENGTH);
fgets(randLine, RAND_MAX_LINE_LENGTH, randfile);
printf("randLine:\n");
printf("%s\n", randLine);
for (int k = 0; k < 48; k++) {
sscanf_s(&randLine[k * 2], "%2hhx", &ptr->expRand[k]);
}
memset(randLine, 0, RAND_MAX_LINE_LENGTH);
for (int i = 0; i < arrayLength; i++) {
printf("%02X", ptr->expPrivateKey[i]);
}
printf("\n");
for (int a = 0; a < 48; a++) {
printf("%02X", ptr->expRand[a]);
}
printf("\n");
HANDLE hThread = CreateThread(NULL, 0, ThreadProcECC256, (LPVOID *)ptr, 0, NULL);
fgets(privateKeyLine, PRIVATE_MAX_LINE_LENGTH, privateKeyfile);
fgets(randLine, RAND_MAX_LINE_LENGTH, randfile);
}
printf("=========end========\n");
printf("*****************!\n");
Sleep(20000);
// 关闭文件
return 0;
}
// 线程函数
DWORD WINAPI ThreadProcECC256(LPVOID lpParameter)
{
// UINT32 type, unsigned char* randSeed, unsigned char* expPrivateKey
PRIVATE_DATA* privateData = (PRIVATE_DATA *)(lpParameter);
/* UINT32 type = 1;*/
/* unsigned char privateKey[32] = {0};
unsigned char x[32] = { 0 };
unsigned char y[32] = { 0 };*/
/* memset(rand.drbg.seed.bytes, 0, 48);
memcpy(rand.drbg.seed.bytes, c, 48);*/
TPM_RC ret = genEccKey(
// IN ,ecc256:1 ecc384:2 sm2:3
1,
// IN
privateData->expRand,
// out
privateData->privateKey,
// out
privateData->x,
// out
privateData->y
);
// 比较
// 比较
for (int i = 0; i < 32; i++)
{
if (privateData->privateKey[i] != (privateData->expPrivateKey)[i]) {
printf("=============================error happen====================================\n");
printf("\n");
char name9[] = "error calcprivateKey";
myprintf3(name9, privateData->privateKey, 32);
printf("\n");
char name2[] = "error myprivateKey";
myprintf3(name2, privateData->expPrivateKey, 32);
printf("\n");
char name1[] = "error rand";
myprintf3(name1, privateData->expRand, 48);
printf("\n");
// 退出当前进程
exit(1);
}
}
printf("\n");
// printf("Hello from thread! ret = %d \n",ret);
return 0;
}
typedef struct {
UINT32 type;
unsigned char expPrivateKey[32];
unsigned char expRand[48];
unsigned char privateKey[32];
unsigned char x[32] ;
unsigned char y[32] ;
} PRIVATE_DATA;
HANDLE hThread = CreateThread(NULL, 0, ThreadProcECC256, (LPVOID *)ptr, 0, NULL);
中的第四个参数用于传递ThreadProcECC256的参数,参数不能直接放到 ThreadProcECC256 中
否则返回错误码