OpenSSL测试-随机数

任务详情

0. 在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务
1. 使用OpenSSL定义一个私有函数 static int getRandom(char *r, int length), 获取length个字节的随机数(5‘)
2. 把上述函数集成到src中的sdf.c中的SDF_GenerateRandom中(5')
3. 在test中的main.c调用SDF_GenerateRandom进行测试,至少测试1个字节,5个字节,20个字节三种情况。(5‘)
4. **提交代码(或代码链接)和运行结果截图**

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

sdf.c

点击查看代码
#include "sdf.h"
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <openssl/bn.h>
#include <stdio.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,"MJY");
	strcpy(di.DeviceName,"SDF20191329");
	strcpy(di.DeviceSerial,"20220421");
	di.DeviceVersion = 1;
    //...

    //pstDevicelnfo = &di;
    *pstDeviceInfo = di;

	return SDR_OK;
}

static int getRandom(char *r,int length)
{
    srand((unsigned)time(NULL));
    return rand();
}

int SDF_GenerateRandom (void * hSessionHandle,unsigned int uiLength,unsigned char * pucRandom)
{
 BIGNUM *bn;
        
        int i;
        bn = BN_new(); //生成一个BIGNUM结构
        //int bits = 20;
        int top = -1;
        int bottom = 1;
        BN_rand(bn, uiLength, top, bottom); //生成指定bits的随机数
        char *a = BN_bn2hex(bn); //转化成16进制字符串
        puts(a);
        printf("\n");
        for(i=0;*(a+i)!='\0';i++)
        {
            *(pucRandom+i)=*(a+i);
        }
        *(pucRandom+i)='\0';
        BN_free(bn); //释放BIGNUM结构
        return SDR_OK;
}

main.c

点击查看代码
#include "sdf.h"
#include <stdio.h>
#include <stdlib.h>

int main(){

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

    DEVICEINFO testdi;
    ret = SDF_GetDeviceInfo(pdh, &testdi);
	if(ret != SDR_OK)
	{
        printf("error!");
	}
        else 
	{
	 printf("success!\n");
        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);
	
	printf("random length is :\n");
	int m;
	scanf("%d",&m);
	char string[100];
	
	ret = SDF_GenerateRandom(*pdh,m,string);
	if(ret != SDR_OK)
	{
            printf("error!");
	}
        else 
            for(int i=0;i<1;i++)
	     printf("The random is :%s\n",string);

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

运行截图

  • 1个字节

  • 5个字节

  • 20个字节

posted @ 2022-04-28 16:00  修勾  阅读(212)  评论(0编辑  收藏  举报