sdf 测试-1-龙脉智能钥匙
#include "sdf.h"
#include <string.h>
#include <time.h>
#include <stdlib.h>
//********************************
//设备管理
//********************************
int SDF_OpenDevice(void **phDeviceHandle) {
// 模拟设备打开
*phDeviceHandle = malloc(1); // 简单模拟
return SDR_OK;
}
int SDF_CloseDevice(void *hDeviceHandle) {
// 模拟设备关闭
free(hDeviceHandle);
return SDR_OK;
}
int SDF_OpenSession(void *hDeviceHandle, void **phSessionHandle) {
*phSessionHandle = malloc(1); // 简单模拟
return SDR_OK;
}
int SDF_CloseSession(void *hSessionHandle) {
free(hSessionHandle);
return SDR_OK;
}
int SDF_GetDeviceInfo(void *hSessionHandle, DEVICEINFO *pstDeviceInfo) {
DEVICEINFO di;
strcpy(di.IssuerName, "RocSDF");
strcpy(di.DeviceName, "SDFhyj20211211");
strcpy(di.DeviceSerial, "20240520");
di.DeviceVersion = 1;
*pstDeviceInfo = di;
return SDR_OK;
}
// 私有函数,用于生成随机数
static int getRandom(char *r, int length) {
void *hDeviceHandle = NULL;
void *hSessionHandle = NULL;
int ret;
// 打开设备
ret = SDF_OpenDevice(&hDeviceHandle);
if (ret != SDR_OK) {
return ret;
}
// 打开会话
ret = SDF_OpenSession(hDeviceHandle, &hSessionHandle);
if (ret != SDR_OK) {
SDF_CloseDevice(hDeviceHandle);
return ret;
}
// 生成随机数
// 使用标准库生成随机数
for (int i = 0; i < length; i++) {
r[i] = rand() % 256; // 生成0到255的随机数
}
// 关闭会话
ret = SDF_CloseSession(hSessionHandle);
if (ret != SDR_OK) {
SDF_CloseDevice(hDeviceHandle);
return ret;
}
// 关闭设备
ret = SDF_CloseDevice(hDeviceHandle);
if (ret != SDR_OK) {
return ret;
}
return SDR_OK;
}
// 修改后的 SDF_GenerateRandom 函数
int SDF_GenerateRandom(void *hSessionHandle, unsigned int uiLength, unsigned char *pucRandom) {
int ret = getRandom((char *)pucRandom, uiLength);
if (ret != SDR_OK) {
// 处理错误
return ret;
}
return SDR_OK;
}
0. 根据gmt0018标准,推导sdf的接口调用模式,比如调用SDF_GenerateRandom,还应调用其他什么函数,调用顺序是什么,给出结论和推导过程。(10‘)
为了推导调用SDF接口的顺序,以 SDF_GenerateRandom 为例,我们可以参考标准中定义的常规操作步骤。通常情况下,在调用加密设备的API时,我们需要按照以下步骤进行:
初始化环境
打开设备
生成随机数
关闭设备
释放资源
具体步骤及推导过程
初始化环境:SDF_OpenDevice
首先需要初始化加密设备,打开设备连接。
void *hDeviceHandle;
int ret = SDF_OpenDevice(&hDeviceHandle);
if (ret != SDR_OK) {
// 处理错误
return ret;
}
打开会话:SDF_OpenSession
设备打开后,需要打开一个会话。
void *hSessionHandle;
ret = SDF_OpenSession(hDeviceHandle, &hSessionHandle);
if (ret != SDR_OK) {
// 处理错误
SDF_CloseDevice(hDeviceHandle);
return ret;
}
生成随机数:SDF_GenerateRandom
打开会话后,就可以调用生成随机数的接口。
unsigned int uiLength = 16; // 需要生成的随机数长度,单位字节
unsigned char pucRandom[16];
ret = SDF_GenerateRandom(hSessionHandle, uiLength, pucRandom);
if (ret != SDR_OK) {
// 处理错误
SDF_CloseSession(hSessionHandle);
SDF_CloseDevice(hDeviceHandle);
return ret;
}
关闭会话:SDF_CloseSession
完成操作后,需要关闭会话。
ret = SDF_CloseSession(hSessionHandle);
if (ret != SDR_OK) {
// 处理错误
SDF_CloseDevice(hDeviceHandle);
return ret;
}
关闭设备:SDF_CloseDevice
最后关闭设备连接。
ret = SDF_CloseDevice(hDeviceHandle);
if (ret != SDR_OK) {
// 处理错误
return ret;
}
总结
根据以上推导,完整的调用顺序和代码示例如下:
#include "sdf.h" // 假设SDF API的头文件为sdf.h
int generate_random_numbers() {
void *hDeviceHandle;
void *hSessionHandle;
int ret;
// 1. 初始化环境,打开设备
ret = SDF_OpenDevice(&hDeviceHandle);
if (ret != SDR_OK) {
// 处理错误
return ret;
}
// 2. 打开会话
ret = SDF_OpenSession(hDeviceHandle, &hSessionHandle);
if (ret != SDR_OK) {
// 处理错误
SDF_CloseDevice(hDeviceHandle);
return ret;
}
// 3. 生成随机数
unsigned int uiLength = 16; // 需要生成的随机数长度,单位字节
unsigned char pucRandom[16];
ret = SDF_GenerateRandom(hSessionHandle, uiLength, pucRandom);
if (ret != SDR_OK) {
// 处理错误
SDF_CloseSession(hSessionHandle);
SDF_CloseDevice(hDeviceHandle);
return ret;
}
// 随机数生成成功,pucRandom中保存了生成的随机数
// 4. 关闭会话
ret = SDF_CloseSession(hSessionHandle);
if (ret != SDR_OK) {
// 处理错误
SDF_CloseDevice(hDeviceHandle);
return ret;
}
// 5. 关闭设备
ret = SDF_CloseDevice(hDeviceHandle);
if (ret != SDR_OK) {
// 处理错误
return ret;
}
return SDR_OK; // 成功
}
1. 使用龙脉智能钥匙定义一个私有函数 static int getRandom(char *r, int length), 获取length个字节的随机数(5‘)
2.把上述函数集成到src中的sdf.c中的SDF_GenerateRandom中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/rand.h>
#include "sdf.h"
// 设备和会话接口函数
int SDF_OpenDevice(void **phDeviceHandle)
{
return SDF_OK;
}
int SDF_CloseDevice(void *hDeviceHandle)
{
return SDF_OK;
}
int SDF_GetDeviceInfo(void *hSessionHandle, DEVICEINFO *pstDeviceInfo)
{
DEVICEINFO di;
strcpy(di.IssuerName, "hzxSDF");
strcpy(di.DeviceName, "SDFhzx20201310");
strcpy(di.DeviceSerial, "20221026");
di.DeviceVersion = 1;
*pstDeviceInfo = di;
return SDF_OK;
}
// 私有函数,用于生成随机数
static int getRandom(char *r, int length)
{
if (RAND_bytes((unsigned char *)r, length) != 1) {
// 如果生成随机数失败
return SDF_ERROR;
}
return SDF_OK;
}
// SDF_GenerateRandom函数,调用getRandom生成随机数
int SDF_GenerateRandom(void *hSessionHandle, unsigned int uiLength, unsigned char *pucRandom)
{
int ret = getRandom((char *)pucRandom, uiLength);
if (ret != SDF_OK) {
// 处理错误
return SDF_ERROR;
}
return SDF_OK;
}
3. 在test中的main.c调用SDF_GenerateRandom进行测试,至少测试1个字节,5个字节,20个字节三种情况。(5‘)
- 提交代码(或代码链接)和运行结果截图
![]()
5.gpt截图
![]()


浙公网安备 33010602011771号