diff --git a/include/SDF/config_acc.h b/include/SDF/config_acc.h new file mode 100644 index 0000000000000000000000000000000000000000..9941faee54dc3a22f1a0d3c6cffb0cf079358ffc --- /dev/null +++ b/include/SDF/config_acc.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. + * Licensed under the Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR + * PURPOSE. + * See the Mulan PSL v2 for more details. + * Description: SDF config + */ + +#ifndef CONFIG_ACC_H +#define CONFIG_ACC_H + +struct acc_params { + unsigned int producer_num; + unsigned int consumer_num; + unsigned int shared_mem_size; +}; + +int config_acc(struct acc_params *acc_param); + +#endif \ No newline at end of file diff --git a/include/SDF/sdf_sdk.h b/include/SDF/sdf_sdk.h new file mode 100644 index 0000000000000000000000000000000000000000..7789697f92e7d92381cd23879c3dd147f63d9568 --- /dev/null +++ b/include/SDF/sdf_sdk.h @@ -0,0 +1,257 @@ +/* + * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved. + * Licensed under the Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR + * PURPOSE. + * See the Mulan PSL v2 for more details. + * Description: SDF SDK + */ + +#ifndef SDF_SDK_H +#define SDF_SDK_H +#include +#include +#include +#include + +typedef signed char GM_CHAR; +typedef signed char GM_INT8; +typedef short GM_INT16; +typedef int GM_INT32; +typedef long long GM_INT64; +typedef unsigned char GM_UCHAR; +typedef unsigned char GM_UINT8; +typedef unsigned short GM_UINT16; +typedef unsigned int GM_UINT32; +typedef unsigned long long GM_UINT64; + +typedef unsigned int GM_RV; +typedef void * GM_OBJ; + +#define GM_TRUE 0x00000001 +#define GM_FALSE 0x00000000 + +#define KEY_TAG_TYPE_MOVE_BITS 28 +#define ECCref_MAX_LEN 64 +#define PASSWORD_LEN 128 +#define SM2_KEY_BYTES 32 +#define SM4_KEY_BYTES 16 + +typedef struct KeyHandle_st { + unsigned char *key_name; +} KeyHandle; + +typedef struct ECCrefPublicKey_st +{ + unsigned int bits; + unsigned char x[ECCref_MAX_LEN]; + unsigned char y[ECCref_MAX_LEN]; +} ECCrefPublicKey; + +typedef struct ECCrefPrivateKey_st +{ + unsigned int bits; + unsigned char K[ECCref_MAX_LEN]; +} ECCrefPrivateKey; + +typedef struct ECCrefKey_st +{ + unsigned int bits; + unsigned int valid; + ECCrefPublicKey pub_key; + ECCrefPrivateKey private_key; +} ECCrefKey; + +typedef struct ECCCipher_st +{ + unsigned char x[ECCref_MAX_LEN]; + unsigned char y[ECCref_MAX_LEN]; + unsigned char M[32]; + unsigned int L; + unsigned char C[]; +} ECCCipher; + +typedef struct ECCSignature_st +{ + unsigned char r[ECCref_MAX_LEN]; + unsigned char s[ECCref_MAX_LEN]; +} ECCSignature; + +#define AGREEMENT_ID_MAX_LEN 64 +typedef struct { + unsigned int key_index; + unsigned int derived_key_size; + unsigned char self_id[AGREEMENT_ID_MAX_LEN]; + unsigned int self_id_len; + ECCrefPublicKey self_pub_key; + ECCrefPublicKey self_temp_pub_key; + int self_temp_ecc_key_id; +} AGREEMENT_SELF_INFO; + +typedef struct { + unsigned char peer_id[AGREEMENT_ID_MAX_LEN]; + unsigned int peer_id_len; + ECCrefPublicKey peer_pub_key; + ECCrefPublicKey peer_temp_pub_key; +} AGREEMENT_PEER_INFO; + +typedef struct { + unsigned char Name[32]; + unsigned char version[32]; + unsigned int status; + unsigned int errorCode; +} ModuleStatus; + +typedef struct { + unsigned char password[PASSWORD_LEN]; + unsigned int key_size; + union { + struct { + unsigned char x[SM2_KEY_BYTES]; + unsigned char y[SM2_KEY_BYTES]; + unsigned char k[SM2_KEY_BYTES]; + } sm2_key; + struct { + unsigned char key[SM4_KEY_BYTES]; + } sm4_key; + }; +} crypto_key_struct; + +enum GmAlgorithm { + GM_SM4_ECB = 0x00000401, + GM_SM4_CBC = 0x00000402, + GM_SM4_GCM = 0x00000420, + GM_SM2_1 = 0x00020200, + GM_SM3_SM2 = 0x00020201, + GM_SM2_3 = 0x00020800, + GM_SM2_SIGN = 0x00020200, + GM_SM2_VERIFY = 0x00020201, + GM_SM2_ENCRYPT = 0x00020800, + GM_SM2_DECRYPT = 0x00020801, + GM_SM3 = 0x00000001, + GM_SM3_MAC = 0x00000008, +}; + +#define GM_NOPAD 0x00000001 +#define GM_PKCS7 0x00000002 + +#define GM_BASE 0x01000000 +enum GmErrorCode { + GM_OK = 0x0, + GM_UNKNOWERR = GM_BASE + 0x00000001, + GM_NOTSUPPORT = GM_BASE + 0x00000002, + GM_PARDENY = GM_BASE + 0x00000007, + GM_KEYNOTEXIST = GM_BASE + 0x00000008, + GM_ALGNOTSUPPORT = GM_BASE + 0x00000009, + GM_ALGMODNOTSUPPORT = GM_BASE + 0x0000000A, + GM_PKOPERR = GM_BASE + 0x0000000B, + GM_SKOPERR = GM_BASE + 0x0000000C, + GM_SIGNERR = GM_BASE + 0x0000000D, + GM_VERIFYERR = GM_BASE + 0x0000000E, + GM_SYMOPERR = GM_BASE + 0x0000000F, + GM_STEPERR = GM_BASE + 0x00000010, + GM_FILESIZEERR = GM_BASE + 0x00000011, + GM_FILENOEXIST = GM_BASE + 0x00000012, + GM_FILEOFSERR = GM_BASE + 0x00000013, + GM_KEYTYPEERR = GM_BASE + 0x00000014, + GM_KEYERR = GM_BASE + 0x00000015, + GM_ENCDATAERR = GM_BASE + 0x00000016, + GM_RANERR = GM_BASE + 0x00000017, + GM_PRKRERR = GM_BASE + 0x00000018, + GM_MACERR = GM_BASE + 0x00000019, + GM_FILEEXISTS = GM_BASE + 0x0000001A, + GM_EILEWERR = GM_BASE + 0x0000001B, + GM_NOBUFFER = GM_BASE + 0x0000001C, + GM_INARGERR = GM_BASE + 0x0000001D, + GM_OUTARGERR = GM_BASE + 0x0000001E, + GM_GENERICERR = GM_BASE + 0x0000001F, +}; + +enum SDFAlgorithm { + SDF_INVALID = 0x00000000, + SDF_SM2_SIGN = 0x00000001, + SDF_SM2_ENC = 0x00000002, + SDF_SM4 = 0x00000003, +}; +#define SM2_KEY_LEN 256 +#define SM4_KEY_LEN 128 + +GM_RV GM_GenerateKeyPair(unsigned int uiAlgID, unsigned int uiKeyBits, unsigned char *appID, unsigned char *keyID); +GM_RV GM_DestroyKey(unsigned char *keyID, unsigned char *appID); +GM_RV GM_GetStateAndVersion(ModuleStatus *uiStatus); + +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_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_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_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_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_InternalEncrypt_ECC(void *hSessionHandle, unsigned int uiISKIndex, unsigned char *pucData, + unsigned int uiDataLength, ECCCipher *pucEncData); +int SDF_InternalDecrypt_ECC(void *hSessionHandle, unsigned int uiISKIndex, ECCCipher *pucEncData, + unsigned char *pucData, unsigned int *puiDataLength); +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); + +GM_RV GM_Encrypt(unsigned char *keyID, unsigned char *appID, unsigned int uiAlgID, unsigned int paddingMode, + unsigned char *pucAADData, unsigned int uiAADDataLength, + unsigned char *pucData, unsigned int uiDataLength, unsigned char *pucEncData, unsigned int *puiEncDataLength, + unsigned char *pucTag, unsigned int *puiTagLength); +GM_RV GM_Decrypt(unsigned char *keyID, unsigned char *appID, unsigned int uiAlgID, unsigned int paddingMode, + unsigned char *pucAADData, unsigned int uiAADDataLength, + unsigned char *pucEncData, unsigned int uiEncDataLength, unsigned char *pucTag, unsigned int uiTagLength, + unsigned char *pucData, unsigned int *puiDataLength); +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); +GM_RV GM_CalculateMAC(unsigned char *keyID, unsigned char *appID, unsigned int uiAlgID, + unsigned char *pucData, unsigned int uiDataLength, unsigned char *pucMAC, unsigned int *puiMACLength); +int SDF_ExternalDecrypt_ECC(void *hSessionHandle, unsigned int uiAlgID, ECCrefPrivateKey *pucPrivateKey, + ECCCipher *pucEncData, unsigned char *pucData, unsigned int *puiDataLength); +int SDF_ExternalEncrypt_ECC(void *hSessionHandle, unsigned int uiAlgID, ECCrefPublicKey *pucPublicKey, + unsigned char *pucData, unsigned int uiDataLength, ECCCipher *pucEncature); +int SDF_GenerateKey(void *hSessionHandle, unsigned int uiKeyInfo, unsigned char *pucPassWord, unsigned int passwordLen); +#endif