20221320—冯泰瑞—课上测试:商用密码接口实现

20221320—冯泰瑞—课上测试:商用密码接口实现

完成下面任务

1 在 Ubuntu 或 openEuler 中完成任务(推荐openEuler)

2 参考GM/T0018 2023和实验代码,说明SDF接口调用的一般过程是什么

SDF接口调用的一般过程涉及与密码设备进行交互的一系列步骤,包括设备管理、密钥管理、算法运算等。以下是根据GM/T0018-2023《密码设备应用接口规范》和实验三代码总结的SDF接口调用的一般过程:

  1. 打开设备(SDF_OpenDevice)

    • 调用SDF_OpenDevice函数打开密码设备,获取设备句柄。
    • 参数:void** phDeviceHandle,用于返回设备句柄。
    • 返回值:成功返回0,失败返回错误代码。
  2. 创建会话(SDF_OpenSession)

    • 使用设备句柄调用SDF_OpenSession函数创建与密码设备的会话,获取会话句柄。
    • 参数:void* hDeviceHandle,已打开的设备句柄;void** phSessionHandle,用于返回会话句柄。
    • 返回值:成功返回0,失败返回错误代码。
  3. 获取设备信息(SDF_GetDeviceInfo)

    • 使用会话句柄调用SDF_GetDeviceInfo函数获取密码设备的能力描述信息。
    • 参数:void* hSessionHandle,与设备建立的会话句柄;DEVICEINFO* pstDeviceInfo,设备能力描述信息。
    • 返回值:成功返回0,失败返回错误代码。
  4. 密钥管理

    • 根据需要执行密钥管理操作,如导出公钥、生成密钥对、导入密钥等。
  5. 算法运算

    • 执行所需的密码学算法运算,如加密、解密、签名、验证等。
    • 例如,使用SDF_Encrypt进行对称加密,SDF_Decrypt进行对称解密,SDF_InternalSign_ECC进行ECC签名等。
  6. 关闭会话(SDF_CloseSession)

    • 使用会话句柄调用SDF_CloseSession函数关闭与密码设备的会话。
    • 参数:void* hSessionHandle,与密码设备已建立的会话句柄。
    • 返回值:成功返回0,失败返回错误代码。
  7. 关闭设备(SDF_CloseDevice)

    • 使用设备句柄调用SDF_CloseDevice函数关闭密码设备,并释放相关资源。
    • 参数:void* hDeviceHandle,已打开的设备句柄。
    • 返回值:成功返回0,失败返回错误代码。
  8. 错误处理

    • 在每个步骤中,如果函数返回非0值,需要根据返回的错误代码进行错误处理。

3 参考课程代码sdfproject,使用gmssl定义一个私有函数 static int getRandom(char *r, int length), 获取length个字节的随机数

static int getRandom(char *r, int length) {
    rand_bytes((unsigned char *)r, length);
    return 0; // 成功
}

4 把上述函数集成到src中的sdf.c中的SDF_GenerateRandom中,实现相关代码

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);

#ifndef __SDF_H
#define __SDF_H

// ... 其他定义 ...

// 错误代码
#define SDR_OK 0x0
#define SDR_BASE 0x01000000
#define SDR_INARGERR SDR_BASE + 0x0000001 // 输入参数错误
#define SDR_RANDERR SDR_BASE + 0x00000017 // 随机数产生失败

// ... 其他定义 ...

#endif

sdf.c原码

// sdf.c
#include "sdf.h"
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <gmssl/rand.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;
}


static int getRandom(char *r, int length) {
    rand_bytes((unsigned char *)r, length);
    return 0; // 成功
}

int SDF_GenerateRandom(void *hSessionHandle, unsigned int uiLength, unsigned char *pucRandom) {
    return getRandom((char *)pucRandom, uiLength);
}

main.c原码

// main.c
#include "sdf.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>  

int main() {
    void **pdh;
    pdh = (void **)malloc(sizeof(void *));
    int ret;

    ret = SDF_OpenDevice(pdh);
    if (ret != SDR_OK) {
        printf("error!\n");
    } else {
        printf("device opened!\n");
    }

    DEVICEINFO testdi;
    ret = SDF_GetDeviceInfo(*pdh, &testdi);
    if (ret != SDR_OK) {
        printf("error!\n");
    } else {
        printf("Issuer Name: %s\n", testdi.IssuerName);
        printf("Device Name: %s\n", testdi.DeviceName);
        printf("Device Serial: %s\n", testdi.DeviceSerial);
        printf("Device Version: %d\n", testdi.DeviceVersion);
    }

    // 测试生成随机数
    unsigned char randomBytes[20]; // 用于存储随机数的缓冲区

    // 测试1个字节
    ret = SDF_GenerateRandom(*pdh, 1, randomBytes);
    if (ret == SDR_OK) {
        printf("1 byte of random: %x\n", randomBytes[0]);
    } else {
        printf("Generate random error!\n");
    }

    // 测试5个字节
    memset(randomBytes, 0, sizeof(randomBytes)); // 清空缓冲区
    ret = SDF_GenerateRandom(*pdh, 5, randomBytes);
    printf("5 byte of random: ");
    if (ret == SDR_OK) {
        for (int i = 0; i < 5; i++) {
            printf("%x ", randomBytes[i]);
        }
        printf("\n");
    } else {
        printf("Generate random error!\n");
    }

    // 测试20个字节
    memset(randomBytes, 0, sizeof(randomBytes)); // 清空缓冲区
    ret = SDF_GenerateRandom(*pdh, 20, randomBytes);
    printf("20 byte of random: ");
    if (ret == SDR_OK) {
        for (int i = 0; i < 20; i++) {
            printf("%x ", randomBytes[i]);
        }
        printf("\n");
    } else {
        printf("Generate random error!\n");
    }

    ret = SDF_CloseDevice(*pdh);
    if (ret != SDR_OK) {
        printf("error!\n");
    } else {
        free(pdh);
        printf("device closed!\n");
    }

    return 0;
}

5 在test中的main.c调用SDF_GenerateRandom进行测试,至少测试1个字节,5个字节,20个字节三种情况

fengtairui@fengtairui-virtual-machine:~$ gcc -o test main.c sdf.c utils.c -lgmssl
fengtairui@fengtairui-virtual-machine:~$ ./test
device opened!
Issuer Name: RocSDF
Device Name: SDFBESTI181x
Device Serial: 2021040001
Device Version: 1
1 byte of random: c9
5 byte of random: 8a da 8e 43 d4 
20 byte of random: 93 9d 59 de 7b fc ee b6 a8 dd ea 72 29 b7 a4 1a 78 b3 5f 85 
device closed!

6 提交git log结果

fengtairui@fengtairui-virtual-machine:~$ git add sdf.h sdf.c main.c test
fengtairui@fengtairui-virtual-machine:~$ git commit -m "random"
[master 3f9e4d5] random
 4 files changed, 171 insertions(+), 20 deletions(-)
 create mode 100644 main.c
 create mode 100755 sdf.h
 create mode 100755 test
fengtairui@fengtairui-virtual-machine:~$ git log
commit 3f9e4d5873486a68477fedd41291c655d9aa89ff (HEAD -> master)
Author: fengtairui <1978274655@qq.com>
Date:   Tue Dec 17 13:59:01 2024 +0800

    random
posted @ 2025-02-02 20:15  20221320冯泰瑞  阅读(77)  评论(0)    收藏  举报