#include "zayk_mmk_api.h"
#include <memory.h>
#include <time.h>
#include <stdio.h>
#include <sys/types.h>
#include <string>
#include <iostream>
using namespace std;
typedef unsigned char byte;
static const char hex2str[17]="0123456789abcdef";
uint HexEncode(const void* raws, uint len, char *hex){
const byte* raw = (const byte*)raws;
for (uint i = 0, k = 0; i < len; i++){
byte high = raw[i] >> 4, low = raw[i] &0xf;
hex[k++] = hex2str[high];
hex[k++] = hex2str[low];
}
return len << 1;
}
string HexEncodeToString(const void* raws, uint len){
int dstlen = len * 2;
char hex[dstlen];
auto r = HexEncode(raws, len, hex);
return string(hex, r);
}
void debug(const char *str){
fprintf(stderr, "%s\n", str);
}
int main(int argc, char *argv[])
{
void *session, *dev;
ECCrefPrivateKey priv = {};
ECCrefPublicKey pub = {};
ECCSignature sig = {};
DEVICEINFO devInfo;
void *keyHandle;
byte msg[32] = "ok,card.this is the test!";
byte msg2[32] = "no,card.";
byte iv[32] = "no,cardIV";
byte key[32] = "no,cardKey";
byte msgCopy[32] = {};
clock_t st, ed;
int sum = 0;
int index = 1;
unsigned int len;
int r = SDF_OpenDevice(&dev);
if (r != 0){
debug("open dev err");
SDF_CloseDevice(dev);
return -1;
}else{
debug("open dev ok");
}
r = SDF_OpenSession(dev, &session);
if (r != 0){
debug("open session err");
goto end;
}else{
debug("open session ok");
}
if (r = SDF_GetDeviceInfo(session, &devInfo)){
debug("get dev Info err"); goto end;
}
printf("AsymAlgAbility=%d %d\n", devInfo.AsymAlgAbility[0], devInfo.AsymAlgAbility[1]);
printf("BufferSize=%d\n", devInfo.BufferSize);
printf("DeviceName=%s\n", devInfo.DeviceName);
printf("DeviceSerial=%s\n", devInfo.DeviceSerial);
printf("DeviceVersion=%d\n", devInfo.DeviceVersion);
printf("HashAlgAbility=%d\n", devInfo.HashAlgAbility);
printf("IssuerName=%d\n", devInfo.IssuerName);
printf("StandardVersion=%d\n", devInfo.StandardVersion);
printf("SymAlgAbility=%d\n", devInfo.SymAlgAbility);
#if 0
r = SDF_InternalSign_ECC(session, index, msg, 32, &sig);
if (r){
debug("sign err"); goto end;
}
r = SDF_InternalVerify_ECC(session, index, msg, 32, &sig);
if (r){
debug("verify err"); printf("%d\n",r); goto end;
} else{
debug("verify ok");
}
#endif
r = SDF_HashInit(session, SGD_SHA256, 0, 0, 0);
if (r){
debug("hash init err"); goto end;
}
for (int i=0; i<0xff; i++){
r = SDF_HashUpdate(session, msg, 32);
if (r){
debug("hash update err"); goto end;
}
}
r = SDF_HashFinal(session, msg2, &len);
if (r){
debug("hash finish err"); goto end;
}
cout << HexEncodeToString(msg2, len) << "\n";
r = SDF_ImportKey(session, msg, 16, &keyHandle);
if (r){
debug("import err");goto end;
}
r = SDF_Encrypt(session, keyHandle, SGD_SMS4_CFB, iv, msg, 32, msg2, &len);
if (r){
debug("enc err");goto end;
}
cout<<len<<"\n";
r = SDF_Decrypt(session, keyHandle, SGD_SMS4_CFB, iv, msg2, 32, msgCopy, &len);
if (r){
debug("dec err");goto end;
}
cout<<len<<"\n";
if (memcmp(msg, msgCopy, 32)!= 0){
debug("not enc ok");
} else {
debug("enc dec ok");
}
r = SDF_DestoryKey(session, keyHandle);
if (r){
debug("import destroy");goto end;
}
r = SDF_GenerateKeyPair_ECC(session, SGD_SM2, 256, &pub, &priv);
if (r){
debug("ecc generate err"); goto end;
}
sum = 0;
st = clock();
for (int i=0; i<0xff; i++){
r = SDF_HashInit(session, SGD_SHA256, 0, 0, 0);
for (int i=0; i<0xff; i++){
SDF_HashUpdate(session, msg, 32);
}
SDF_HashFinal(session, msg2, &len);
if (msg2[2] != 0x7d){
sum++;
}
}
ed = clock();
cout<< "sha256: "<< double( ed - st)/CLOCKS_PER_SEC << " s\n" << sum << "\n";
sum = 0;
st = clock();
for (int i=0; i<0xff; i++){
r = SDF_HashInit(session, SGD_SM3, &pub, msg, 32);
for (int i=0; i<0xff; i++){
SDF_HashUpdate(session, msg, 32);
}
SDF_HashFinal(session, msg2, &len);
if (msg2[2] != 0x7d){
sum++;
}
}
ed = clock();
cout<< "sm3: "<< double( ed - st)/CLOCKS_PER_SEC << " s\n" << sum << "\n";
r = SDF_ImportKey(session, msg, 16, &keyHandle);
if (r){
debug("import err");goto end;
}
sum = 0;
st = clock();
for (int i=0; i<0xffff; i++){
r = SDF_Encrypt(session, keyHandle, SGD_SMS4_CFB, iv, msg, 32, msg2, &len);
r = SDF_Decrypt(session, keyHandle, SGD_SMS4_CFB, iv, msg2, 32, msgCopy, &len);
if (msgCopy[2] == byte(',')){
sum++;
}
}
ed = clock();
cout<< "sm4cfb: "<< double( ed - st)/CLOCKS_PER_SEC << " s\n" << sum << "\n";
sum = 0;
st = clock();
for (int i=0; i<0xffff; i++){
r = SDF_Encrypt(session, keyHandle, SGD_AES_CFB, iv, msg, 32, msg2, &len);
r = SDF_Decrypt(session, keyHandle, SGD_AES_CFB, iv, msg2, 32, msgCopy, &len);
if (msgCopy[2] == byte(',')){
sum++;
}
}
ed = clock();
cout<< "aescfb: "<< double( ed - st)/CLOCKS_PER_SEC << " s\n" << sum << "\n";
sum = 0;
st = clock();
for (int i=0; i<0xffff; i++){
r = SDF_Encrypt(session, keyHandle, SGD_SM1_CFB, iv, msg, 32, msg2, &len);
r = SDF_Decrypt(session, keyHandle, SGD_SM1_CFB, iv, msg2, 32, msgCopy, &len);
if (msgCopy[2] == byte(',')){
sum++;
}
}
ed = clock();
cout<< "sm1: "<< double( ed - st)/CLOCKS_PER_SEC << " s\n" << sum << "\n";
r = SDF_DestoryKey(session, keyHandle);
if (r){
debug("import destroy");goto end;
}
sum = 0;
st = clock();
for (int i=0; i<0xffff; i++){
r = SDF_GenerateRandom(session, 32, msg);
if (r){
debug("rand err"); goto end;
}
r = SDF_ExternalSign_ECC(session, SGD_SM2_1, &priv, msg, 32, &sig);
if (r){
debug("sign err"); goto end;
}
r = SDF_ExternalVerify_ECC(session, SGD_SM2_1, &pub, msg, 32, &sig);
if (r){
debug("verify err"); printf("%d\n",r);
} else{
sum++; //debug("verify ok");
}
}
ed = clock();
cout<<"sm2: " << double( ed - st)/CLOCKS_PER_SEC << " s\n" << sum << "\n";
end:
r = SDF_CloseSession(session);
if (r){
debug("close session err");
}else{
debug("close session ok");
}
r = SDF_CloseDevice(dev);
if (r){
debug("close dev err");
}else{
debug("close dev ok");
}
return 0;
}
#ifdef __cplusplus
extern "C" {
#endif
//对称算法标识
#define SGD_SM1_ECB 0x00000101 //SM1算法ECB加密模式
#define SGD_SM1_CBC 0x00000102 //SM1算法CBC加密模式
#define SGD_SM1_CFB 0x00000104 //SM1算法CFB加密模式
#define SGD_SM1_OFB 0x00000108 //SM1算法OFB加密模式
#define SGD_SM1_MAC 0x00000110 //SM1算法MAC加密模式
#define SGD_SM1_CTR 0x00000120 //SM1算法CTR加密模式
#define SGD_SM1_XTS 0x00000140 //SM1算法XTX加密模式
#define SGD_SSF33_ECB 0x00000201 //SSF33算法ECB加密模式
#define SGD_SSF33_CBC 0x00000202 //SSF33算法CBC加密模式
#define SGD_SSF33_CFB 0x00000204 //SSF33算法CFB加密模式
#define SGD_SSF33_OFB 0x00000208 //SSF33算法OFB加密模式
#define SGD_SSF33_MAC 0x00000210 //SSF33算法MAC加密模式
#define SGD_SMS4_ECB 0x00000401 //SMS4算法ECB加密模式
#define SGD_SMS4_CBC 0x00000402 //SMS4算法CBC加密模式
#define SGD_SMS4_CFB 0x00000404 //SMS4算法CFB加密模式
#define SGD_SMS4_OFB 0x00000408 //SMS4算法OFB加密模式
#define SGD_SMS4_MAC 0x00000410 //SMS4算法MAC运算
#define SGD_SMS4_XTS 0x00000440 //SM4算法XTS加密模式
//0x00000400—0x800000xx 为其它对称算法预留
#define SGD_DES_ECB 0x00001001
#define SGD_DES_CBC 0x00001002
#define SGD_DES_CFB 0x00001004
#define SGD_DES_OFB 0x00001008
#define SGD_DES_MAC 0x00001010
#define SGD_DES_XTS 0x00001020
#define SGD_3DES_ECB 0x00002001
#define SGD_3DES_CBC 0x00002002
#define SGD_3DES_CFB 0x00002004
#define SGD_3DES_OFB 0x00002008
#define SGD_3DES_MAC 0x00002010
#define SGD_3DES_XTS 0x00002020
#define SGD_AES_ECB 0x00004001
#define SGD_AES_CBC 0x00004002
#define SGD_AES_CFB 0x00004004
#define SGD_AES_OFB 0x00004008
#define SGD_AES_MAC 0x00004010
#define SGD_AES_XTS 0x00004040
#define SGD_AES128_ECB SGD_AES_ECB
#define SGD_AES128_CBC SGD_AES_CBC
#define SGD_AES128_CFB SGD_AES_CFB
#define SGD_AES128_OFB SGD_AES_OFB
#define SGD_AES128_MAC SGD_AES_MAC
#define SGD_AES128_XTS SGD_AES_XTS
#define SGD_AES256_ECB 0x00008001
#define SGD_AES256_CBC 0x00008002
#define SGD_AES256_CFB 0x00008004
#define SGD_AES256_OFB 0x00008008
#define SGD_AES256_MAC 0x00008010
#define SGD_AES256_XTS 0x00008020
//非对称算法标识
#define SGD_RSA 0x00010000
#define SGD_RSA_SIGN 0x00010100
#define SGD_RSA_ENC 0x00010200
#define SGD_SM2 0x00020100 //椭圆曲线算法
#define SGD_SM2_1 0x00020200 //椭圆曲线签名算法
#define SGD_SM2_2 0x00020400 //椭圆曲线密钥交换协议
#define SGD_SM2_3 0x00020800 //椭圆曲线加密算法
//杂凑算法标识
#define SGD_SM3 0x00000001 //SM3杂凑算法
#define SGD_SHA1 0x00000002 //SHA1杂凑算法
#define SGD_SHA256 0x00000004 //SHA256杂凑算法
//0x00000040~0x00000080 为其它杂凑算法预留
#define SGD_SHA512 0x00000008
#define SGD_SHA224 0x00000010
#define SGD_SHA384 0x00000020
#define SGD_MD5 0x00000040
//SM9算法定义
#define SGD_SM9 0x00040100
#define SGD_SM9_1 0x00040200
#define SGD_SM9_2 0x00040400
#define SGD_SM9_3 0x00040800
//公钥类型类型
#define SGD_PUBLIC_KEY_SIGN 0x00000103 //签名公钥
#define SGD_PUBLIC_KEY_ENCRYPT 0x00000104 //加密公钥
//密钥类型
#define SIGNKEY 0x10000
#define ENCKEY 0x20000
//-----------------------------------------------------------------------------
#if defined(_ZAYK_WINDOWS_)
#define __declspec(dllexport)
#define ZKSDF_FUNC_IMPORT extern __declspec(dllimport)
#define ZKSDF_FUNC_IMPLEMENT __declspec(dllexport)
#define ZKSDF_FUNC_STATIC static
#else
#define
#define ZKSDF_FUNC_IMPORT extern
#define ZKSDF_FUNC_IMPLEMENT
#define ZKSDF_FUNC_STATIC static
#endif
#define SYMM_KEY_LENGTH 16
#define KEY_ENC_KEY_LENGTH 16
//-----------------------------------------------------------------------------
#define RSA_MAX_BITS 2048
#define RSA_MAX_LEN ((RSA_MAX_BITS + 7) / 8)
#define RSA_MAX_PBITS ((RSA_MAX_BITS + 1) / 2)
#define RSA_MAX_PLEN ((RSA_MAX_PBITS + 7)/ 8)
typedef struct RSArefPublicKey_st
{
unsigned int bits;
unsigned char m[RSA_MAX_LEN];
unsigned char e[RSA_MAX_LEN];
} RSArefPublicKey;
typedef struct RSArefPrivateKey_st
{
unsigned int bits;
unsigned char m[RSA_MAX_LEN];
unsigned char e[RSA_MAX_LEN];
unsigned char d[RSA_MAX_LEN];
unsigned char prime[2][RSA_MAX_PLEN];
unsigned char pexp[2][RSA_MAX_PLEN];
unsigned char coef[RSA_MAX_PLEN];
} RSArefPrivateKey;
#define SM2_MAX_BITS 512 //256->512
#define SM2_MAX_LEN ((SM2_MAX_BITS+7) / 8)
typedef struct ECCrefPublicKey_st
{
unsigned int bits;
unsigned char x[SM2_MAX_LEN];
unsigned char y[SM2_MAX_LEN];
} ECCrefPublicKey;
typedef struct ECCrefPrivateKey_st
{
unsigned int bits;
unsigned char D[SM2_MAX_LEN];
} ECCrefPrivateKey;
//ECC Cipher structure
typedef struct ECCCipher_st
{
unsigned char x[SM2_MAX_LEN];
unsigned char y[SM2_MAX_LEN];
unsigned char M[32];
unsigned int L; //密文长度
unsigned char C[136]; //密文数据
} ECCCipher;
//ECC Signature structure
typedef struct ECCSignature_st
{
unsigned char r[SM2_MAX_LEN];
unsigned char s[SM2_MAX_LEN];
} ECCSignature;
typedef struct DeviceInfo_st{
unsigned char IssuerName[40];
unsigned char DeviceName[16];
unsigned char DeviceSerial[16];
unsigned int DeviceVersion;
unsigned int StandardVersion;
unsigned int AsymAlgAbility[2];
unsigned int SymAlgAbility;
unsigned int HashAlgAbility;
unsigned int BufferSize;
}DEVICEINFO;
//-----------------------------------------------------------------------------
//函数定义
//设备管理类函数
int SDF_OpenDevice(void **phDeviceHandle);
int SDF_CloseDevice(void *hDeviceHandle);
int SDF_OpenSession(void *hDeviceHandle, void **phSessionHandle);
int SDF_CloseSession(void *hSessionHandle);
int SDF_GetDeviceInfo(void *hSessionHandle, DEVICEINFO *pstDeviceInfo);
int SDF_GenerateRandom(void *hSessionHandle, unsigned int uiLength,unsigned char *pucRandom);
int SDF_GetPrivateKeyAccessRight(void *hSessionHandle, unsigned int uiKeyIndex,unsigned char *pucPassword,unsigned int uiPwdLength);
int SDF_ReleasePrivateKeyAccessRight( void *hSessionHandle, unsigned int uiKeyIndex);
//密钥管理类函数
int SDF_ExportSignPublicKey_RSA(void *hSessionHandle, unsigned int uiKeyIndex,RSArefPublicKey *pucPublicKey);
int SDF_ExportEncPublicKey_RSA(void *hSessionHandle, unsigned int uiKeyIndex, RSArefPublicKey *pucPublicKey);
int SDF_GenerateKeyPair_RSA(void *hSessionHandle, unsigned int uiKeyBits,RSArefPublicKey *pucPublicKey,RSArefPrivateKey *pucPrivateKey);
int SDF_GenerateKeyWithIPK_RSA(void *hSessionHandle, unsigned int uiIPKIndex,unsigned int uiKeyBits,unsigned char *pucKey,unsigned int *puiKeyLength,void **phKeyHandle);
int SDF_GenerateKeyWithEPK_RSA(void *hSessionHandle, unsigned int uiKeyBits,RSArefPublicKey *pucPublicKey,unsigned char *pucKey,unsigned int *puiKeyLength,void **phKeyHandle);
int SDF_ImportKeyWithISK_RSA(void *hSessionHandle, unsigned int uiISKIndex, unsigned char *pucKey,unsigned int puiKeyLength,void **phKeyHandle);
int SDF_ExchangeDigitEnvelopeBaseOnRSA(void *hSessionHandle, unsigned int uiKeyIndex,RSArefPublicKey *pucPublicKey,unsigned char *pucDEInput,unsigned int uiDELength, unsigned char *pucDEOutput,unsigned int *puiDELength);
int SDF_ExportSignPublicKey_ECC(void *hSessionHandle, unsigned int uiKeyIndex,ECCrefPublicKey *pucPublicKey);
int SDF_ExportEncPublicKey_ECC(void *hSessionHandle, unsigned int uiKeyIndex,ECCrefPublicKey *pucPublicKey);
int SDF_GenerateKeyPair_ECC(void *hSessionHandle, unsigned int uiAlgID,unsigned int uiKeyBits,ECCrefPublicKey *pucPublicKey,ECCrefPrivateKey *pucPrivateKey);
int SDF_GenerateKeyWithIPK_ECC(void *hSessionHandle, unsigned int uiIPKIndex,unsigned int uiKeyBits,ECCCipher *pucKey,void **phKeyHandle);
int SDF_GenerateKeyWithEPK_ECC(void *hSessionHandle, unsigned int uiKeyBits,unsigned int uiAlgID,ECCrefPublicKey *pucPublicKey,ECCCipher *pucKey,void **phKeyHandle);
int SDF_ImportKeyWithISK_ECC(void *hSessionHandle,unsigned int uiISKIndex,ECCCipher *pucKey,void **phKeyHandle);
int SDF_GenerateAgreementDataWithECC (void *hSessionHandle, unsigned int uiISKIndex,unsigned int uiKeyBits,unsigned char *pucSponsorID,unsigned int uiSponsorIDLength,ECCrefPublicKey *pucSponsorPublicKey,ECCrefPublicKey *pucSponsorTmpPublicKey,void **phAgreementHandle);
int SDF_GenerateKeyWithECC (void *hSessionHandle, unsigned char *pucResponseID,unsigned int uiResponseIDLength,ECCrefPublicKey *pucResponsePublicKey,ECCrefPublicKey *pucResponseTmpPublicKey,void *hAgreementHandle,void **phKeyHandle);
int SDF_GenerateAgreementDataAndKeyWithECC (void *hSessionHandle, unsigned int uiISKIndex,unsigned int uiKeyBits,unsigned char *pucResponseID,unsigned int uiResponseIDLength,unsigned char *pucSponsorID,unsigned int uiSponsorIDLength,\
ECCrefPublicKey *pucSponsorPublicKey,ECCrefPublicKey *pucSponsorTmpPublicKey,ECCrefPublicKey *pucResponsePublicKey,ECCrefPublicKey *pucResponseTmpPublicKey,void **phKeyHandle);
int SDF_ExchangeDigitEnvelopeBaseOnECC(void *hSessionHandle, unsigned int uiKeyIndex,unsigned int uiAlgID,ECCrefPublicKey *pucPublicKey,ECCCipher *pucEncDataIn, ECCCipher *pucEncDataOut);
int SDF_GenerateKeyWithKEK(void *hSessionHandle, unsigned int uiKeyBits,unsigned int uiAlgID,unsigned int uiKEKIndex, unsigned char *pucKey, unsigned int *puiKeyLength, void **phKeyHandle);
int SDF_ImportKeyWithKEK(void *hSessionHandle, unsigned int uiAlgID,unsigned int uiKEKIndex, unsigned char *pucKey, unsigned int puiKeyLength, void **phKeyHandle);
int SDF_ImportKey(void *hSessionHandle, unsigned char *pucKey, unsigned int uiKeyLength,void **phKeyHandle);
int SDF_DestroyKey(void *hSessionHandle, void *hKeyHandle);
int SDF_DestoryKey(void *hSessionHandle, void *hKeyHandle);
//非对称运算类函数
int SDF_ExternalPublicKeyOperation_RSA(void *hSessionHandle, RSArefPublicKey *pucPublicKey,unsigned char *pucDataInput,unsigned int uiInputLength,unsigned char *pucDataOutput,unsigned int *puiOutputLength);
int SDF_ExternalPrivateKeyOperation_RSA(void *hSessionHandle, RSArefPrivateKey *pucPrivateKey,unsigned char *pucDataInput,unsigned int uiInputLength,unsigned char *pucDataOutput,unsigned int *puiOutputLength);
int SDF_InternalPublicKeyOperation_RSA(void *hSessionHandle,unsigned int uiKeyIndex,unsigned char *pucDataInput,unsigned int uiInputLength,unsigned char *pucDataOutput,unsigned int *puiOutputLength);
int SDF_InternalPrivateKeyOperation_RSA(void *hSessionHandle,unsigned int uiKeyIndex,unsigned char *pucDataInput,unsigned int uiInputLength,unsigned char *pucDataOutput,unsigned int *puiOutputLength);
int SDF_ExternalSign_ECC(void *hSessionHandle,unsigned int uiAlgID,ECCrefPrivateKey *pucPrivateKey,unsigned char *pucData,unsigned int uiDataLength,ECCSignature *pucSignature);
int SDF_ExternalVerify_ECC( void *hSessionHandle,unsigned int uiAlgID,ECCrefPublicKey *pucPublicKey,unsigned char *pucDataInput,unsigned int uiInputLength, ECCSignature *pucSignature);
int SDF_InternalSign_ECC(void *hSessionHandle,unsigned int uiISKIndex, unsigned char *pucData,unsigned int uiDataLength,ECCSignature *pucSignature);
int SDF_InternalVerify_ECC(void *hSessionHandle,unsigned int uiISKIndex,unsigned char *pucData,unsigned int uiDataLength, ECCSignature *pucSignature);
int SDF_ExternalEncrypt_ECC(void *hSessionHandle,unsigned int uiAlgID,ECCrefPublicKey *pucPublicKey,unsigned char *pucData,unsigned int uiDataLength,ECCCipher *pucEncData);
int SDF_ExternalDecrypt_ECC(void *hSessionHandle,unsigned int uiAlgID,ECCrefPrivateKey *pucPrivateKey,ECCCipher *pucEncData,unsigned char *pucData,unsigned int *puiDataLength);
//对称运算类函数
int SDF_Encrypt(void *hSessionHandle,void *hKeyHandle,unsigned int uiAlgID,unsigned char *pucIV,unsigned char *pucData,unsigned int uiDataLength,unsigned char *pucEncData,unsigned int *puiEncDataLength);
int SDF_Decrypt(void *hSessionHandle,void *hKeyHandle,unsigned int uiAlgID,unsigned char *pucIV,unsigned char *pucEncData,unsigned int uiEncDataLength,unsigned char *pucData,unsigned int *puiDataLength);
int SDF_CalculateMAC(void *hSessionHandle,void *hKeyHandle,unsigned int uiAlgID,unsigned char *pucIV,unsigned char *pucData,unsigned int uiDataLength,unsigned char *pucMAC,unsigned int *puiMACLength);
//摘要类函数
int SDF_HashInit(void *hSessionHandle,unsigned int uiAlgID, ECCrefPublicKey *pucPublicKey,unsigned char *pucID,unsigned int uiIDLength);
int SDF_HashUpdate(void *hSessionHandle,unsigned char *pucData, unsigned int uiDataLength);
int SDF_HashFinal(void *hSessionHandle,unsigned char *pucHash, unsigned int *puiHashLength);
//文件类函数
int SDF_CreateFile(void *hSessionHandle, unsigned char *pucFileName,unsigned int uiNameLen,unsigned int uiFileSize);
int SDF_ReadFile(void *hSessionHandle,unsigned char *pucFileName,unsigned int uiNameLen,unsigned int uiOffset,unsigned int *puiFileLength,unsigned char *pucBuffer);
int SDF_WriteFile(void *hSessionHandle,unsigned char *pucFileName,unsigned int uiNameLen,unsigned int uiOffset,unsigned int uiFileLength,unsigned char *pucBuffer);
int SDF_DeleteFile(void *hSessionHandle,unsigned char *pucFileName,unsigned int uiNameLen);
//扩展函数
int SDF_InternalEncrypt_ECC(void *hSessionHandle, unsigned int uiISKIndex, unsigned int uiAlgID, unsigned char *pucData, unsigned int uiDataLength, ECCCipher *pucEncData);
int SDF_InternalDecrypt_ECC(void *hSessionHandle, unsigned int uiISKIndex, unsigned int uiAlgID, ECCCipher *pucEncData, unsigned char *pucData, unsigned int *puiDataLength);
int SDF_InternalPublicKeyOperation_RSA_Ex(void *hSessionHandle, unsigned int uiKeyIndex, unsigned int uiKeyUsage, unsigned char *pucDataInput, unsigned int uiInputLength, unsigned char *pucDataOutput, unsigned int *puiOutputLength);
int SDF_InternalPrivateKeyOperation_RSA_Ex(void *hSessionHandle, unsigned int uiKeyIndex, unsigned int uiKeyUsage, unsigned char *pucDataInput, unsigned int uiInputLength, unsigned char *pucDataOutput, unsigned int *puiOutputLength);
int SDF_InternalSign_ECC_Ex(void *hSessionHandle , unsigned int uiISKIndex, unsigned int uiAlgID, unsigned char *pucData, unsigned int uiDataLength, ECCSignature *pucSignature);
int SDF_InternalVerify_ECC_Ex(void *hSessionHandle , unsigned int uiISKIndex, unsigned int uiAlgID, unsigned char *pucData, unsigned int uiDataLength, ECCSignature *pucSignature);
int SDF_Encrypt_EX(void *hSessionHandle,unsigned int uiAlgID,int keyIndex, unsigned char *keyData, unsigned int keyDataLength, unsigned char *pucIV, unsigned char *pucData, unsigned int uiDataLength, unsigned char *pucEncData, unsigned int *puiEncDataLength);
int SDF_Decrypt_EX(void *hSessionHandle,unsigned int uiAlgID,int keyIndex, unsigned char *keyData, unsigned int keyDataLength, unsigned char *pucIV, unsigned char *pucEncData, unsigned int uiEncDataLength, unsigned char *pucData, unsigned int *puiDataLength);
int SDF_GenerateKeyPair_ECC_EX(void *hSessionHandle,unsigned int index,unsigned int uiAlgID);
int SDF_GenerateKeyPair_RSA_EX(void *hSessionHandle, unsigned int index, unsigned int uiAlgID, unsigned int uiKeyBitsy);
int SDF_GenerateSMKey(void *hSessionHandle, unsigned int uiKeyNumber, unsigned int uiKeyLen);
//GDCA扩展函数
int SDF_Ext_GenerateSignKeyPair_RSA(void* hSessionHandle, unsigned int uiKeyIndex, unsigned int uiKeyBits);
int SDF_Ext_GenerateEncKeyPair_RSA(void* hSessionHandle, unsigned int uiKeyIndex, unsigned int uiKeyBits);
int SDF_Ext_GenerateSignKeyPair_ECC(void* hSessionHandle, unsigned int uiKeyIndex, unsigned int uiAlgID, unsigned int uiKeyBits);
int SDF_Ext_GenerateEncKeyPair_ECC(void* hSessionHandle, unsigned int uiKeyIndex, unsigned int uiAlgID, unsigned int uiKeyBits);
int SDF_Ext_ImportSignKeyPair_RSA(void* hSessionHandle, unsigned int uiKeyIndex, RSArefPrivateKey *pucPrivateKey);
int SDF_Ext_ImportEncKeyPair_RSA(void* hSessionHandle, unsigned int uiKeyIndex, RSArefPrivateKey *pucPrivateKey);
int SDF_Ext_ImportSignKeyPair_ECC(void* hSessionHandle, unsigned int uiKeyIndex, ECCrefPrivateKey *pucPrivateKey, ECCrefPublicKey *pucPublicKey);
int SDF_Ext_ImportEncKeyPair_ECC(void* hSessionHandle, unsigned int uiKeyIndex, ECCrefPrivateKey *pucPrivateKey, ECCrefPublicKey *pucPublicKey);
int SDF_Ext_DeleteSignKeyPair_RSA(void* hSessionHandle, unsigned int uiKeyIndex);
int SDF_Ext_DeleteEncKeyPair_RSA(void* hSessionHandle, unsigned int uiKeyIndex);
int SDF_Ext_DeleteSignKeyPair_ECC(void* hSessionHandle, unsigned int uiKeyIndex);
int SDF_Ext_DeleteEncKeyPair_ECC(void* hSessionHandle, unsigned int uiKeyIndex);
int SDF_Ext_ModifyRSAKeyPIN(void* hSessionHandle, unsigned int uiKeyIndex, unsigned char *oldPIN, unsigned int oldPINLen, unsigned char *newPIN, unsigned int newPINLen);
int SDF_Ext_ModifyECCKeyPIN(void* hSessionHandle, unsigned int uiKeyIndex, unsigned char *oldPIN, unsigned int oldPINLen, unsigned char *newPIN, unsigned int newPINLen);
int SDF_Ext_EnumFiles(void* hSessionHandle, char *fileList, unsigned int *fileListLen);
int SDF_Ext_GetFileInfo(void* hSessionHandle, const char *fileName, unsigned int *fileLen);
int SDF_Ext_ExportSignPrivateKey_RSA(void* hSessionHandle, unsigned int uiKeyIndex, RSArefPrivateKey *pucPrivateKey); //不支持,返回SDR_KEYERR(0x00000015)
int SDF_Ext_ExportEncPrivateKey_RSA(void* hSessionHandle, unsigned int uiKeyIndex, RSArefPrivateKey *pucPrivateKey);//不支持,返回SDR_KEYERR(0x00000015)
int SDF_Ext_ExportSignPrivateKey_ECC(void* hSessionHandle, unsigned int uiKeyIndex, ECCrefPrivateKey *pucPrivateKey);//不支持,返回SDR_KEYERR(0x00000015)
int SDF_Ext_ExportEncPrivateKey_ECC(void* hSessionHandle, unsigned int uiKeyIndex, ECCrefPrivateKey *pucPrivateKey);//不支持,返回SDR_KEYERR(0x00000015)
int SDF_Ext_GenerateKeyPair(void* hSessionHandle, unsigned int uiAlgID, unsigned int uiKeyIndex, unsigned int uiKeyBits);
int SDF_Ext_ImportKeyPair_RSA(void* hSessionHandle, unsigned int uiAlgID, unsigned int uiKeyIndex, RSArefPrivateKey *pucPrivateKey);
int SDF_Ext_ImportKeyPair_ECC(void* hSessionHandle, unsigned int uiAlgID, unsigned int uiKeyIndex, ECCrefPrivateKey *pucPrivateKey, ECCrefPublicKey *pucPublicKey);
int SDF_Ext_DeleteKeyPair(void* hSessionHandle, unsigned int uiAlgID, unsigned int uiKeyIndex);
int SDF_Ext_ModifyKeyPIN(void* hSessionHandle, unsigned int uiAlgID, unsigned int uiKeyIndex, unsigned char *oldPIN, unsigned int oldPINLen, unsigned char *newPIN, unsigned int newPINLen);
int SDF_Ext_ExportPrivateKey_RSA(void* hSessionHandle, unsigned int uiAlgID, unsigned int uiKeyIndex, RSArefPrivateKey *pucPrivateKey); //不支持,返回SDR_KEYERR(0x00000015)
int SDF_Ext_ExportPrivateKey_ECC(void* hSessionHandle, unsigned int uiAlgID, unsigned int uiKeyIndex, ECCrefPrivateKey *pucPrivateKey); //不支持,返回SDR_KEYERR(0x00000015)
//-----------------------------------------------------------------------------
//错误代码标识
#define SDR_OK 0x0 //操作成功
#define SDR_BASE 0x01000000 //错误码基础值
#define SDR_UNKNOWERR SDR_BASE + 0x00000001 //未知错误
#define SDR_NOTSUPPORT SDR_BASE + 0x00000002 //不支持的接口调用
#define SDR_COMMFAIL SDR_BASE + 0x00000003 //与设备通信失败
#define SDR_HARDFAIL SDR_BASE + 0x00000004 //运算模块无响应
#define SDR_OPENDEVICE SDR_BASE + 0x00000005 //打开设备失败
#define SDR_OPENSESSION SDR_BASE + 0x00000006 //创建会话失败
#define SDR_PARDENY SDR_BASE + 0x00000007 //无私钥使用权限
#define SDR_KEYNOTEXIST SDR_BASE + 0x00000008 //不存在的密钥调用
#define SDR_ALGNOTSUPPORT SDR_BASE + 0x00000009 //不支持的算法调用
#define SDR_ALGMODNOTSUPPORT SDR_BASE + 0x0000000A //不支持的算法模式调用
#define SDR_PKOPERR SDR_BASE + 0x0000000B //公钥运算失败
#define SDR_SKOPERR SDR_BASE + 0x0000000C //私钥运算失败
#define SDR_SIGNERR SDR_BASE + 0x0000000D //签名运算失败
#define SDR_VERIFYERR SDR_BASE + 0x0000000E //验证签名失败
#define SDR_SYMOPERR SDR_BASE + 0x0000000F //对称算法运算失败
#define SDR_STEPERR SDR_BASE + 0x00000010 //多步运算步骤错误
#define SDR_FILESIZEERR SDR_BASE + 0x00000011 //文件长度超出限制
#define SDR_FILENOEXIST SDR_BASE + 0x00000012 //指定的文件不存在
#define SDR_FILEOFSERR SDR_BASE + 0x00000013 //文件起始位置错误
#define SDR_KEYTYPEERR SDR_BASE + 0x00000014 //密钥类型错误
#define SDR_KEYERR SDR_BASE + 0x00000015 //密钥错误
#define SDR_ENCDATAERR SDR_BASE + 0x00000016 //ECC加密数据错误
//… … SDR_BASE + 0x00000016至 SDR_BASE + 0x00FFFFFF 预留
#define SDR_SESSION_ERR SDR_BASE + 0x00000017 //
#define SDR_DATA_LENGTH_ERR SDR_BASE + 0x00000018 //数据长度错误
#define SDR_DATA_IS_NULL SDR_BASE + 0x00000019 //参数为空
#define SDR_HANDLE_FREED SDR_BASE + 0x0000001A //密钥已释放
#define SDR_MEMORY_ERR SDR_BASE + 0x0000001B //申请内存失败
#define SDR_PARAM_ERR SDR_BASE + 0x0000001C //参数错误
#define SDR_SEND_REC_ERR SDR_BASE + 0x0000001D //通讯错误
#define SDR_REC_CODE_ERR SDR_BASE + 0x0000001E //收到设备返回错误码
#define SDR_CLOSEDEVICE SDR_BASE + 0x0000001F //关闭设备句柄错误
#define SDR_INPUTDATA_ASN1_ERR SDR_BASE + 0x00000020 //输入数据编码错误码
#define SDR_RECDATAT_ASN1_ERR SDR_BASE + 0x00000021 //接收数据解码错误码
#define SDR_IMPORT_CHECK_ERR SDR_BASE + 0x00000022 //导入密钥后验证失败
#ifdef __cplusplus
}
#endif