采用dlopen、dlsym、dlclose加载动态链接库
int load_library()
{
int ret = 0;
void * lib_handle = NULL;
char path[128] = {0};
P_SKF_GetFuncList get_func_list;
getcwd(path, sizeof(path));
strcat(path, "/libskf.so");
lib_handle = dlopen(path, RTLD_LAZY );
if (!lib_handle)
{
printf("Open Error:%s.\n", dlerror());
return 0;
}
get_func_list = dlsym(lib_handle, "SKF_GetFuncList");
if (get_func_list == NULL)
{
printf("Dlsym Error:%s.\n", dlerror());
return 0;
}
ret = get_func_list(&FunctionList);
if (ret)
{
printf("fnGetList ERROR 0x%x", ret);
return ret;
}
return ret;
}
参考链接:https://www.cnblogs.com/Anker/p/3746802.html
SM3摘要
#include "skf.h"
#include <string.h>
#include <windows.h>
#include <dlfcn.h>
#include <stdio.h>
PSKF_FUNCLIST FunctionList;
void print_data(char *info, unsigned char *data, unsigned int len)
{
int i = 0;
if (info) printf("%s\n", info);
for (i = 0; i < len; i++) {
if (i && i % 16 == 0) printf("\n");
printf("0x%02x ", data[i]);
}
printf("\n");
}
int sm3_dsgt(DEVHANDLE devHandle){
int ret = 0;
BYTE pbHashValue[32];
ULONG pulHashValueLen;
pulHashValueLen = 32;
unsigned char pbData[16] = {0x32,0x5a,0xd8,0x74,0x84,0x07,0xfd,0xdf,0x38,0x20,0x33,0x80,0x05,0x81,0xb4,0xed};
unsigned int ulDataLen = 16;
/*unsigned char pubKey[64] = {0x04,0x6b,0x4e,0x48,0x40,0x83,0xad,0xde,0x92,0x66,0x41,0xca,0xbe,0x40,0x8d,
0xb2,0x8b,0x67,0x06,0xd2,0xd8,0x23,0x49,0x7d,0xfc,0xb9,0x80,0x3b,0x74,0xda,
0xee,0xcd,0x20,0xad,0x0d,0xbe,0x42,0x51,0x22,0xdb,0x30,0xa7,0x61,0x9c,0x4f,
0xf4,0xdf,0x51,0xb6,0xb0,0x81,0xf1,0x5c,0x03,0x13,0xf3,0xfc,0x3d,0x53,0x77,0xfd,0x00,0x77,0xc3,0x9e};*/
unsigned char pub_X[32] = {0xB5, 0xA4, 0xE9, 0xE4, 0x56, 0x02, 0xF9, 0xAB,
0xE3, 0xD5, 0xB7, 0xCE, 0xD2, 0xF9, 0xBE, 0x24,
0x3E, 0x03, 0x0E, 0xD3, 0x81, 0xA1, 0x4E, 0x1A,
0xC8, 0x98, 0x57, 0x3A, 0x09, 0xC8, 0xEF, 0x18};
unsigned char pub_Y[32] = {0xD3, 0x51, 0xEE, 0x2A, 0xAE, 0xB2, 0x8A, 0xD0,
0x55, 0xB3, 0xE8, 0x02, 0x28, 0xE7, 0xDA, 0xA7,
0xF2, 0xCC, 0xD8, 0xC4, 0xC9, 0xB0, 0x4C, 0xE4,
0x89, 0x8B, 0x7E, 0x43, 0x31, 0x64, 0xDE, 0x31};
HANDLE hHash;
ECCPUBLICKEYBLOB pubKeyBlob;
memset(&pubKeyBlob,0,sizeof(pubKeyBlob));
printf("------------\n");
strncpy(&pubKeyBlob.XCoordinate[32],pub_X,32);
strncpy(&pubKeyBlob.YCoordinate[32],pub_Y,32);
printf("+++++++++++++\n");
pubKeyBlob.BitLen = 256;
print_data("XCoordinate",pubKeyBlob.XCoordinate,64);
print_data("YCoordinate",pubKeyBlob.YCoordinate,64);
printf("============\n");
#if 1
BYTE pbId[] = "1234567812345678";
ULONG ulIdLen = 16;
ret = FunctionList->SKF_DigestInit(devHandle, SGD_SM3, &pubKeyBlob, pbId, ulIdLen, &hHash);
//不做预处理
//ret = FunctionList->SKF_DigestInit(devHandle, SGD_SM3, NULL, NULL, NULL, &hHash);
printf("ret = [%d]\n",ret);
ret = FunctionList->SKF_DigestUpdate(hHash, pbData, ulDataLen);
printf("ret1 = [%d]\n",ret);
ret = FunctionList->SKF_DigestFinal(hHash, pbHashValue, &pulHashValueLen);
printf("ret2 = [%d]\n",ret);
print_data("pbHashValue",pbHashValue,pulHashValueLen);
return 0;
#endif
}