2024-07-30 17:17:35 +08:00

231 lines
7.3 KiB
C
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 芯片库接口封装
*
* 芯片实现功能包括生成随机数、sm1对称加密算法、sm2签名加密算法、sm3杂凑算法、sm4对称加密算法、安全文件存储
*
* @author void_sora
* @date 2021/02/20
* @version 0.9
*/
#ifndef __AWSKF_H__
#define __AWSKF_H__
// PIN<49>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>
#define ADMIN_TYPE 0
#define USER_TYPE 1
#define ECCPRIVATEKEYBITLENGTH 256
#define ECCPRIVATEKEYLENGTH 32
//<2F><EFBFBD><E3B7A8>־
// SM1
#define SGD_SM1_ECB 0x00000101
#define SGD_SM1_CBC 0x00000102
#define SGD_SM1_CFB 0x00000104
#define SGD_SM1_OFB 0x00000108
#define SGD_SM1_MAC 0x00000110
// SM4
#define SGD_SMS4_ECB 0x00000401
#define SGD_SMS4_CBC 0x00000402
#define SGD_SMS4_CFB 0x00000404
#define SGD_SMS4_OFB 0x00000408
// AES
#define SGD_AES128_ECB 0x00000801
#define SGD_AES128_CBC 0x00000802
#define SGD_AES128_CFB 0x00000804
#define SGD_AES128_OFB 0x00000808
#define SGD_AES192_ECB 0x00000811
#define SGD_AES192_CBC 0x00000812
#define SGD_AES192_CFB 0x00000814
#define SGD_AES192_OFB 0x00000818
#define SGD_AES256_ECB 0x00000821
#define SGD_AES256_CBC 0x00000822
#define SGD_AES256_CFB 0x00000824
#define SGD_AES256_OFB 0x00000828
// DES
#define SGD_DES_ECB 0x00001001
#define SGD_DES_CBC 0x00001002
#define SGD_DES_CFB 0x00001004
#define SGD_DES_OFB 0x00001008
// 3DES_2KEY
#define SGD_D3DES_ECB 0x00001011
#define SGD_D3DES_CBC 0x00001012
#define SGD_D3DES_CFB 0x00001014
#define SGD_D3DES_OFB 0x00001018
// 3DES_3KEY
#define SGD_T3DES_ECB 0x00001021
#define SGD_T3DES_CBC 0x00001022
#define SGD_T3DES_CFB 0x00001024
#define SGD_T3DES_OFB 0x00001028
//<2F>ǶԳ<C7B6>
#define SGD_RSA 0x00010000
#define SGD_SM2_1 0x00020100 // <20><>Բ<EFBFBD><D4B2><EFBFBD><EFBFBD>ǩ<EFBFBD><C7A9><EFBFBD>
#define SGD_SM2_2 0x00020200 // <20><>Բ<EFBFBD><D4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Կ<EFBFBD><D4BF><EFBFBD><EFBFBD>Э<EFBFBD><D0AD>
#define SGD_SM2_3 0x00020400 // <20><>Բ<EFBFBD><D4B2><EFBFBD>߼<EFBFBD><DFBC><EFBFBD><EFBFBD>
//<2F>Ӵ<EFBFBD><D3B4><EFBFBD><E3B7A8>־
#define SGD_SM3 0x00000001
#define SGD_SHA1 0x00000002
#define SGD_SHA256 0x00000004
#define SKF_USE_ENCDEC 0x01 //<2F><><EFBFBD>ڼ<EFBFBD><DABC>ܽ<EFBFBD><DCBD><EFBFBD>
#define SKF_USE_SIGVER 0x02 //<2F><><EFBFBD><EFBFBD>ǩ<EFBFBD><C7A9><EFBFBD><EFBFBD>֤
/**
* @enum 对称加密算法模式
*/
enum PerformanceAlgMode
{
P_AlG_ECB,
P_AlG_CBC,
P_AlG_CFB,
P_AlG_OFB
};
enum EccKeyPairType
{
CryptKey,
SignatureKey
};
/**
* @struct AWSKF隐式结构。
*/
typedef struct AWSKF_S AWSKF_s, *pAWSKF_s;
/**
* @struct AWSKF方法结结构体
*/
typedef struct AWSKF_F
{
pAWSKF_s (*Malloc)();
void (*Free)(pAWSKF_s spAWSKF);
int (*ChangeDeviceAuthKey)(pAWSKF_s spAWSKF, unsigned char *OldAuthKey, unsigned char *NewAuthKey);
int (*Create)(pAWSKF_s spAWSKF, unsigned char *DeviceAuthKey, unsigned char *AdminPIN, unsigned char *PIN, unsigned int ContainerCount);
int (*Open)(pAWSKF_s spAWSKF, unsigned char *PIN);
int (*Close)(pAWSKF_s spAWSKF);
int (*Destroy)(unsigned char *DeviceAuthKey);
int (*GetContainerCount)(pAWSKF_s spAWSKF);
int (*ChangePIN)(pAWSKF_s spAWSKF, unsigned char *OldPIN, unsigned char *NewPIN);
int (*UnlockPIN)(pAWSKF_s spAWSKF);
int (*GenerateRandom)(pAWSKF_s spAWSKF, unsigned char *Random, unsigned int RandomLen);
int (*GenerateHashInit)(pAWSKF_s spAWSKF, unsigned char *Publickey);
int (*GenerateHashUpdate)(pAWSKF_s spAWSKF, unsigned char *SourceData, unsigned int SourceDataLen);
int (*GenerateHashFinal)(pAWSKF_s spAWSKF, unsigned char *Hash, unsigned int *HashLen);
int (*GenerateHash)(pAWSKF_s spAWSKF, unsigned char *SourceData, unsigned int SourceDataLen, unsigned char *Hash, unsigned int *HashLen, unsigned char *Publickey);
int (*GetHwcode)(pAWSKF_s spAWSKF, unsigned char *HwCode, unsigned int *HwCodeLen);
int (*GetDevInfo)(pAWSKF_s spAWSKF, unsigned char *DevInfo, unsigned int *DevInfoLen);
int (*FreeData)(unsigned char *Data);
int (*FileInit)(pAWSKF_s spAWSKF, char *FileName, unsigned int FileSize);
int (*GetFileNameList)(pAWSKF_s spAWSKF, char *FileList, unsigned int *FileListSize);
int (*FileWrite)(pAWSKF_s spAWSKF, char *FileName, unsigned int Offset, unsigned char *Data, unsigned int DataLen);
int (*FileRead)(pAWSKF_s spAWSKF, unsigned char *FileName, unsigned int Offset, unsigned char *Data, unsigned int *DataLen);
int (*FileDestroy)(pAWSKF_s spAWSKF, unsigned char *FileName);
int (*GenerateEccKeyPair)(pAWSKF_s spAWSKF, unsigned char *Prikey, unsigned char *Pubkey);
int (*ResetContainerEccKeyPair)(pAWSKF_s spAWSKF, unsigned int ContainerNumber);
int (*ImportPlaintextContainerECCKeyPair)(
pAWSKF_s spAWSKF, unsigned int ContainerNumber, unsigned int KeyPairtype,
unsigned char *Prikey, unsigned char *Pubkey);
int (*ImportContainerECCKeyPair)(
pAWSKF_s spAWSKF, unsigned int ContainerNumber,
unsigned char *KeyPairCiphertext, unsigned int KeyPairCiphertextLen);
int (*ExportContainerPubkey)(pAWSKF_s spAWSKF, unsigned int ContainerNumber, unsigned int KeyPairtype, unsigned char *Pubkey);
int (*GenerateSignatureExt)(
pAWSKF_s spAWSKF, unsigned char *Prikey,
unsigned char *Data, unsigned int DataLen, unsigned char *Signature, unsigned int *SignatureLen);
int (*GenerateSignatureInt)(
pAWSKF_s spAWSKF, unsigned int ContainerNumber,
unsigned char *Data, unsigned int DataLen, unsigned char *Signature, unsigned int *SignatureLen);
int (*VerifySignatureExt)(
pAWSKF_s spAWSKF, unsigned char *Pubkey,
unsigned char *Data, unsigned int DataLen, unsigned char *Signature, unsigned int SignatureLen);
int (*VerifySignatureInt)(
pAWSKF_s spAWSKF, unsigned int ContainerNumber,
unsigned char *Data, unsigned int DataLen, unsigned char *Signature, unsigned int SignatureLen);
int (*SM2EncryptExt)(
pAWSKF_s spAWSKF, unsigned char *Pubkey,
unsigned char *Data, unsigned int DataLen, unsigned char *Ciphertext, unsigned int *CiphertextLen);
int (*SM2EncryptInt)(
pAWSKF_s spAWSKF, unsigned int ContainerNumber, unsigned int KeyPairtype,
unsigned char *Data, unsigned int DataLen, unsigned char *Ciphertext, unsigned int *CiphertextLen);
int (*SM2DecryptExt)(
pAWSKF_s spAWSKF, unsigned char *PriKey,
unsigned char *Ciphertext, unsigned int CiphertextLen, unsigned char *Data, unsigned int *DataLen);
int (*SM2DecryptInt)(
pAWSKF_s spAWSKF, unsigned int ContainerNumber, unsigned int KeyPairtype,
unsigned char *Ciphertext, unsigned int CiphertextLen, unsigned char *Data, unsigned int *DataLen);
int (*SM1Encrypt)(
pAWSKF_s spAWSKF, int AlgMode, unsigned char *pIv, unsigned char *Key,
unsigned char *Data, unsigned int DataLen, unsigned char *Ciphertext, unsigned int *CiphertextLen);
int (*SM1Decrypt)(
pAWSKF_s spAWSKF, int AlgMode, unsigned char *pIv, unsigned char *Key,
unsigned char *Ciphertext, unsigned int CiphertextLen, unsigned char *Data, unsigned int *DataLen);
int (*SM4Encrypt)(
pAWSKF_s spAWSKF, int AlgMode, unsigned char *pIv, unsigned char *Key,
unsigned char *Data, unsigned int DataLen, unsigned char *Ciphertext, unsigned int *CiphertextLen);
int (*SM4Decrypt)(
pAWSKF_s spAWSKF, int AlgMode, unsigned char *pIv, unsigned char *Key,
unsigned char *Ciphertext, unsigned int CiphertextLen, unsigned char *Data, unsigned int *DataLen);
}AWSKF_f, *pAWSKF_f;
pAWSKF_f AWSKFFunInit();
void AWSKFFunDestroy(pAWSKF_f fpAWSKF);
#endif