sdf 测试-1-openssl

任务详情

在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务,参考网内容 和AI要给出详细过程,否则不得分。使用git管理你的代码。
  1. 根据gmt0018标准,推导sdf的接口调用模式,比如调用SDF_GenerateRandom,还应调用其他什么函数,调用顺序是什么,给出结论和推导过程。(5‘)
  2. 使用openssl定义一个私有函数 static int getRandom(char *r, int length), 获取length个字节的随机数(5‘)
  3. 把上述函数集成到src中的sdf.c中的SDF_GenerateRandom中,补充附件中的代码(5')
  4. 在test中的main.c调用SDF_GenerateRandom进行测试,至少测试1个字节,5个字节,20个字节三种情况。(5‘)
  5. 提交代码(或代码链接)和运行结果截图,给出git log截图(5‘)

作业提交要求使用Markdown格式,同时提交Markdown转化的PDF 或者使用Word

任务0

image

  1. 首先,需要初始化设备,这通常通过调用 SDF_OpenDevice 和 SDF_OpenSession 来完成。
  2. 然后,可以调用 SDF_GenerateRandom 生成随机数。这个函数需要会话句柄和随机数的长度。
  3. 其次,根据需要的操作,可能调用其他接口,
  4. 完成所有操作后,需要关闭会话和设备,调用 SDF_CloseSession 和 SDF_CloseDevice。

任务1

点击查看代码
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 SDF_OK;
}

任务2

点击查看代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"sdf.h"
#include<time.h>
#include<openssl/bn.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,"gjhSDF");
 strcpy(di.DeviceName,"SDFgjh20211316");
 strcpy(di.DeviceSerial,"20211316");
 di.DeviceVersion=1;
 (*pstDeviceInfo)= di; 
 
 return SDF_OK;
}

任务3

image

任务4

image

posted @ 2024-06-06 15:19  20211316郭佳昊  阅读(4)  评论(0编辑  收藏  举报