实验一-密码引擎-加密API实现与测试
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 SymAlgAbility;
unsigned int HashAlgAbility;
unsigned int BufferSize;
} DEVICEINFO;
#define SDR_OK 0x0
/*打开设备*/
int SDF_OpenDevice( void * * phDeviceHandle);
/*关闭设备*/
int SDF_CloseDevice( void * hDeviceHandle);
/*获取设备信息*/
int SDF_GetDeviceInfo(void * hSessionHandle,DEVICEINFO * pstDeviceInfo) ;
/*产生随机数*/
int SDF_GenerateRandom (void * hSessionHandle,unsigned int uiLength,unsigned char * pucRandom);
/*Error Code */
#define SDR_OK 0x0 //操作成功
#define SDR_BASE 0x01000000 //错误码基础值
# define SDR_UNKNOWERR SDR_BASE +Ox00000001 //未知错误
#define SDR_NOTSUPPORT SDR_BASE+0x00000002 //不支持的接口调用
#define SDR_COMMFAIL SDR_BASE+0x00000003 //与设备通信失败
#define SDR_HARDFAIL SDR_ BASE + 0x00000004 //运算模块无响应
#define SDR_OPENDEVICE SDR_ BASE +0x00000005 //打开设备失败
#define SDR_OPENSESSION SDR_BASE+ 0x00000006 //创建会话失败
#endif
sdf.c
#include<stdio.h>
#include<stdlib.h>
#include "sdf.h"
int main(){
void **pdh;
pdh = (void **)malloc(20);
int ret;
ret = SDF_OpenDevice(pdh);
if(ret != SDR_OK)
{
printf("打开设备失败! \n");
}
else
{
printf("成功打开设备!\n");
}
printf("查看设备信息\n");
DEVICEINFO a;
ret = SDF_GetDeviceInfo(*pdh,&a);
if(ret !=SDR_OK)
printf("查看设备信息失败!\n");
else
printf("查看设备信息成功!\n");
printf("设备名称:%s\n",a.DeviceName);
printf("设备版本号为%d\n",a.DeviceVersion);
printf("请输入随机数长度:\n");
int n;
scanf("%d",&n);
char string[100];
ret = SDF_GenerateRandom(*pdh,n,string);
if(ret !=SDR_OK)
printf("生成随机数失败!");
else
printf("生成的随机数为%s\n",string);
ret = SDF_CloseDevice(*pdh);
if(ret != SDR_OK)
{
printf("关闭设备失败!\n");
}
else
{
printf("成功关闭设备!\n");
}
}