1. 参考GM/T0018 2023和实验代码,说明SDF接口调用的一般过程是什么
-
初始化:调用初始化函数,配置和初始化密码设备或模块。
-
加载密钥:如果需要,加载或生成密钥,并将其存储在密码设备中。
-
执行操作:调用SDF接口执行具体的密码学操作,如加密、解密、签名、验证等。
-
获取结果:从密码设备中获取操作结果。
-
释放资源:释放在初始化和操作过程中分配的资源。
-
销毁:如果不再需要,销毁密钥和关闭密码设备。
2 参考课程代码sdfproject,使用gmssl定义一个私有函数 static int getRandom(char *r, int length), 获取length个字节的随机数(7‘)
#include <gmssl/gmssl.h>static int getRandom(char *r, int length) {if (r == NULL || length <= 0) {return -1;}return gm_ssl_ecc_random_bytes(r, length);
}
3 把上述函数集成到src中的sdf.c中的SDF_GenerateRandom中,实现相关代码(10’)
sdf.c
#include "sdf.h"
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <gmssl/gmssl.h>
//********************************
//设备管理
//********************************int SDF_OpenDevice(void ** phDeviceHandle){return SDR_OK;
}int SDF_CloseDevice(void *hDeviceHandle){return SDR_OK;
}int SDF_GetDeviceInfo( void * hSessionHandle, DEVICEINFO * pstDeviceInfo) {DEVICEINFO di;strcpy(di.IssuerName,"RocSDF");strcpy(di.DeviceName,"SDFBESTI181x");strcpy(di.DeviceSerial,"2021040001");di.DeviceVersion = 1;//...//pstDevicelnfo = &di;*pstDeviceInfo = di;return SDR_OK;
}int SDF_GenerateRandom(void *hSessionHandle, unsigned int uiLength, unsigned char *pucRandom) {if (pucRandom == NULL) {return -1; // 或者定义一个错误码}if (gm_ssl_ecc_random_bytes(pucRandom, uiLength) != 0) {// 处理错误,可能需要设置一个错误码return -1;}return SDR_OK;
}
sdf.h
#ifndef __SDF_H
#define __SDF_H
//定义设备信息结构
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 SymAlgAbilty; unsigned int HashAlgAbility; unsigned int BufferSize;
}DEVICEINFO;// Error Code
#define SDR_OK 0x0 //操作成功//********************************
//设备管理
//********************************/*
功能:打开密码设备。
参数∶
phDeviceHandle[out] 返回设备句柄返回值∶0 成功非0 失败,返回错误代码
*/
int SDF_OpenDevice(void ** phDeviceHandle);/*
功能∶关闭密码设备,并释放相关资源。
参数∶
hDeviceHandle[in] 已打开的设备句柄
返回值∶ 0(SDR_OK) 成功非0 失败,返回错误代码
*/
int SDF_CloseDevice(void *hDeviceHandle);/*功能∶获取密码设备能力描述。;
参数∶
hSesionHandle[in]与设备建立的会话句柄
pstDevceInfo [out]设备能力描述信息,内容及格式见设备信息定义
返回值∶ 0(SDR_OK) 成功非0 失败,返回错误代码
*/
int SDF_GetDeviceInfo( void * hSessionHandle, DEVICEINFO * pstDeviceInfo);/*
功能:获取指定长度的随机数
参数:uiLength[in] 欲获取的随机数长度 pucRandom[ out] 缓冲区指针,用于存放获取的随机数返回值∶ 00(SDR_OK) 成功非0 失败,返回错误代码
*/
int SDF_GenerateRandom (void * hSessionHandle, unsigned int uiLength, unsigned char * pucRandom);
#endif
utils.h
#ifndef _UTILS_H_
#define _UTILS_H_char Hex2Char(int i);#endif
utils.c
#include <stdio.h>
#include "util.h"char HStr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
char Hex2Char(int i){
/* if(i>=0 && i<= 9)return i + 0x30;//return i + '0'if(i>=10 && i<=15)return i + 0x37;// return i + 'A' - 10;
*/return HStr[i];
}
4 在test中的main.c调用SDF_GenerateRandom进行测试,至少测试1个字节,5个字节,20个字节三种情况。(4‘)
main,c
#include "sdf.h"
#include <stdio.h>
#include <stdlib.h>int main() {void *pdh;int ret;// 打开设备ret = SDF_OpenDevice(&pdh);if (ret != SDR_OK) {printf("Failed to open device!\n");return -1;}printf("Device opened successfully.\n");// 测试生成1个字节的随机数unsigned char randomByte[1];ret = SDF_GenerateRandom(pdh, 1, randomByte);if (ret == SDR_OK) {printf("1 byte of random data: %02X\n", randomByte[0]);} else {printf("Failed to generate 1 byte of random data.\n");}// 测试生成5个字节的随机数unsigned char randomBytes5[5];ret = SDF_GenerateRandom(pdh, 5, randomBytes5);if (ret == SDR_OK) {printf("5 bytes of random data: ");for (int i = 0; i < 5; i++) {printf("%02X ", randomBytes5[i]);}printf("\n");} else {printf("Failed to generate 5 bytes of random data.\n");}// 测试生成20个字节的随机数unsigned char randomBytes20[20];ret = SDF_GenerateRandom(pdh, 20, randomBytes20);if (ret == SDR_OK) {printf("20 bytes of random data: ");for (int i = 0; i < 20; i++) {printf("%02X ", randomBytes20[i]);}printf("\n");} else {printf("Failed to generate 20 bytes of random data.\n");}// 关闭设备ret = SDF_CloseDevice(pdh);if (ret != SDR_OK) {printf("Failed to close device!\n");} else {printf("Device closed successfully.\n");}return 0;
}
实现过程
zxh@zxh-VirtualBox:~/zxh/cs2/2$ ./main
device opened!
Issuer Name: RocSDF
Device Name: SDFBESTI181x
Device Serial: 2021040001
Device Version: 1
1 byte random number: 112
5 bytes random number: -48 -47 -32 -153 114
20 bytes random number: -112 -96 -145 -41 23 127 24 -69 44 -16 -63 145 -29 126 -32 -89 0 63 12 -24
device closed!