概述
本文以代码的方式描述如何调用TPM生成随机数。
编译方式
gcc -Wall -Wextra -std=c99 -g xxx.c -o xxx
1.头文件
#ifndef _ELTT2_H_
#define _ELTT2_H_
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
//-------------"Defines"-------------
#define ERR_COMMUNICATION -1 ///< Return error check for read and write to the TPM.
#define PRINT_RESPONSE_WITHOUT_HEADER 12 ///< Prints the response buffer from byte 12.
static const uint8_t tpm2_getrandom[] = {
0x80, 0x01, // TPM_ST_NO_SESSIONS
0x00, 0x00, 0x00, 0x0C, // commandSize
0x00, 0x00, 0x01, 0x7B, // TPM_CC_GetRandom
0x00, 0x00 // bytesRequested (will be set later)
};
#endif /* _ELTT2_H_ */
2.具体源文件
#include "eltt2.h"
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdint.h>
int dev_tpm = -1;
unsigned char *path = "/dev/tpm0";
int mode = O_RDWR;
int tpm_test()
{
int ret_val = EXIT_SUCCESS;
int dev_tpm = -1; // TPM device handle.
ssize_t transmit_size = 0; // Amount of bytes sent to / received from the TPM.
uint8_t send_buf[1024];
uint8_t response[1024];
int send_buf_lenth = 0;
int i;
memset(response, 0, 1024);
memset(send_buf, 0, 1024);
send_buf_lenth = sizeof(tpm2_getrandom);
memcpy(send_buf, tpm2_getrandom, sizeof(tpm2_getrandom));
send_buf[sizeof(tpm2_getrandom) - 1] = 32;
dev_tpm = open("/dev/tpm0", O_RDWR);
if (dev_tpm < 0){
printf("open errorn");
return 0;
}
// Send request data to TPM.
transmit_size = write(dev_tpm, send_buf, send_buf_lenth);
if (transmit_size == ERR_COMMUNICATION || send_buf_lenth != transmit_size)
{
//ret_val = errno;
fprintf(stderr, "Error sending request to TPM.n");
return 0;
}
// Read the TPM response header.
transmit_size = read(dev_tpm, response, 1024);
if (transmit_size == ERR_COMMUNICATION)
{
//ret_val = errno;
fprintf(stderr, "Error reading response from TPM.n");
return 0;
}
if (-1 != dev_tpm)
{
close(dev_tpm);
}
return ret_val;
}
最后
以上就是辛勤小天鹅为你收集整理的调用TPM生成随机数1.头文件2.具体源文件的全部内容,希望文章能够帮你解决调用TPM生成随机数1.头文件2.具体源文件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复