加密API实现与测试

下列缩略语适用于本部分: ECC:椭圆曲线算法(Elliptic Curve Cryptography) IPK:内部加密公钥(Internal Public Key) ISK:内部加密私钥(Internal Private Key) EPK:外部加密公钥(External Public Key) KEK:密钥加密密钥(Key Encrypt Key)

实际数字结构定义: 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;

GB/T 0018-2012 # define RSAref_MAX_BIT S2048 # define RSAref_MAX_LEN ((RSAref_MAX_BITS+7)/8) # define RSAref_MAX_PBITS ((RSAref_MAX_BITS+1)/2) #define RSAref_MAX_PLEN ((RSAref_MAX_PBITS+7)/8) typedef struct RSArefPublicKey_st unsigned int bits; unsigned char m[RSAref_MAX_LEN]; unsigned char e[RSAref_MAX_LEN]; }RSArefPublicKey; typedef struct RSArefPrivateKey_st { unsigned int bits; unsigned char m[RSAref_MAX_LEN]; unsigned char e[RSAref_MAX_LEN]; unsigned char d[RSAref_MAX_LEN]; unsigned char prime2; unsigned char pexp2; unsigned char coef RSArefMAX_PLEN]; }RSArefPrivateKey;

ECC加密如下:

# define ECCrcLMAX_BITS 512

# define ECCrcf_MAX_LEN ( (ECCref_MAX^BITS+7) / 8)

typedef struct ECCrcfPublicKey_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];

} ECXDrefPri vateKey ;

//************** //设备管理 //************** /* 功能:打开密码设备,返回设备句柄。 参数: 返回值:0(SDR OK) 成功 非0 失败,返回错误代码 / int SDF_OpenDevice(void** phDeviceHandle); / 功能:关闭密码设备,并释放相关资源。 参数:hDeviceHandle[in] 已打开的设备句柄 返回值:0(SDR OK) 成功 非0 失败,返回错误代码 / int SDF_CloseDevice(void hDeviceHandle); /* 功能: 创建与密码设备的会话。 已打开的设备句柄 hDeviceHandlein]参数:h phessionHiandle[out] 返回与密码设备建立的新会话句柄成功 返回值:0(SDR OK) 成功 非0 失败,返回错误代码 / int SDF_OpenSession(void hDeviceHandle, void** phSessionHandle);

/* 功能:关闭与密码设备已建立的会话,并释放相关资源。 参数:hSessionHandle[in] 与密码设备已建立的会话句柄 返回值:0(SDR OK) 成功 非0 失败,返回错误代码 / int SDF_CloseSesson(void hSessionHandle);

/* 功能:获取密码设备能力描述。 参数:hSessionHandle[in] 与设备建立的会话句柄 pstDevicelnfo[our] 设备能力描述信息,内容及格式见设备信息定义成功 返回值:0(SDR OK) 成功 非0 失败,返回错误代码 / int SDF_GetDeviceInfo( void hSessionHandle, DEVICEINFO* pstDeviceInfo);

/* 功能:获取指定长度的随机数。 参数: hSessonHandle[in] 与设备建立的会话句柄 uiLegth[in] 欲获取的随机数长度 pucRandom[out] 缓冲区指针,用于存放获取的随机数 返回值:0(SDR OK) 成功 非0 失败,返回错误代码 / int SDF_GenerateRandom( void hSessionHandle, unsigned int uiLength, unsigned char* pucRandom);

/* 功能:获取密码设备内部存储的指定索引私钥的使用权。 参数: hSessionHandle[in] 与设备建立的会话句柄 uiKeyIndex[in] 密码设备存储私钥的索引值 pucPassword[in] 使用私钥权限的标识码 uiPwdLength[in] 私钥访问控制码长度,不少于8 字节 返回值:0(SDR OK) 成功 非0 失败,返回错误代码 / int SDF_GetPrivateKeyAccessRight( void hSessionHandle, unsigned int uiKeyIndex, unsigned char* pucPassword, unsigned int uiPwdLength);

/* 功能:释放密码设备存储的指定索引私钥的使用授权。 参数: hSessonHandle[in] 与设备建立的会话句柄 uiKeyIndex[in] 密码设备存储私钥索引值成功 返回值∶0(SDR OK) 成功 非0 失败,返回错误代码 / int SDF_ReleasePrivateKeyAccessRight( void hSessionHandle, unsigned int uiKeyIndex);

posted @ 2021-04-25 19:00  20181333贺仁杰  阅读(127)  评论(0编辑  收藏  举报