tpm emulator测试程序(转)

 

 Makefile文件内容为:

CC    : = gcc
all:     tpmrandom
tpm_getrandom:     tpmrandom.c
    $( CC) tpmrandom.c - o tpmrandom
clean: 
    rm - f tpmrandom


 

源代码为:tpmrandome.c

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define TPM_TAG_RQU_COMMAND 193
#define TPM_TAG_RQU_AUTH1_COMMAND 194
#define TPM_ORD_SHA1Start 160
#define TPM_ORD_SHA1Complete 162
#define TPM_ORD_GetRandom 70

/*********************first run "modprobe tpmd_dev""tpmd -f -d" and if there is a tcsd ,you cannot run it **********/

int main(int argc, char **argv)
{
    unsigned int i,j,fd;
    int res,ret;
    unsigned char buf[256];
    
    int buf_size = sizeof(buf);
    unsigned char random_cmd[] = {0, TPM_TAG_RQU_COMMAND,
                 0, 0, 0, 14,
                 0, 0, 0, TPM_ORD_GetRandom, 
                0, 0, 0, 8};//70 means TPM_ORD_GetRandom

    unsigned char tpm_sha1start[]={0,TPM_TAG_RQU_COMMAND,
                0,0,0,10,
                0,0,0,TPM_ORD_SHA1Start};
        
    unsigned char tpm_sha1complete[]={0,TPM_TAG_RQU_COMMAND,
                0,0,0,78,0,0,0,TPM_ORD_SHA1Complete,
                0,0,0,64,
                1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,
                           33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64};
    

    fd = open("/dev/tpm0",O_RDWR);
    if(fd < 0){
        printf("Error: Open() failed: (%04x)n ", fd);
        return -1;
    }    

    printf("sizeof(random_cmd): %d\n", sizeof(random_cmd));
    printf("data in random_cmd: ");
       for(i = 0; i < sizeof(random_cmd); i++)
                printf("%02x", random_cmd[i]);
       printf("n");

    res = write(fd, random_cmd, sizeof(random_cmd));
    
    if(res != sizeof(random_cmd)){
        printf("Error: write random command failed: (%04x)\n ", res);
        close(fd);
        return -1;
    }

    buf_size = 256;
       ret = read(fd, &buf, buf_size);

        printf("ret of read random tpm0: %d\n",ret);
        printf("read tpm0 random data: ");
        for(i = 0; i < ret; i++){
                printf("%02x ",buf[i] );
        }
        printf("n");
    

    
    buf_size = 256;//buf_size > 10

    printf("sizeof(tpm_sha1start): %d\n", sizeof(tpm_sha1start));
    printf("data in tpm_sha1start: ");
       for(i = 0; i < sizeof(tpm_sha1start); i++)
                printf("%02x", tpm_sha1start[i]);
       printf("n");
    res =write(fd, tpm_sha1start, sizeof(tpm_sha1start));
    
    if(res != sizeof(tpm_sha1start)){
        printf("Error: write tpm_sha1start failed: (%04x)\n ", res);
        close(fd);
        return -1;
    }
    
    buf_size = 256;
       ret = read(fd, &buf, buf_size);

        printf("ret of read tpm0 after tpm_sha1start : %d\n",ret);
        printf("read tpm0 tpm_sha1start data: ");
        for(i = 0; i < ret; i++){
                printf("%02x ",buf[i] );
        }
        printf("n");


    buf_size = 256;//buf_size > 10

    printf("sizeof(tpm_sha1complete): %d\n", sizeof(tpm_sha1complete));
    printf("data in tpm_sha1complete: ");
       for(i = 0; i < sizeof(tpm_sha1complete); i++)
                printf("%02x", tpm_sha1complete[i]);
       printf("n");


    res =write(fd, tpm_sha1complete, sizeof(tpm_sha1complete));
    
    if(res != sizeof(tpm_sha1complete)){
        printf("Error: write tpm_sha1complete failed: (%04x)\n ", res);
        close(fd);
        return -1;
    }
    
    buf_size = 256;
       ret = read(fd, &buf, buf_size);

        printf("ret of read tpm0 after tpm_sha1complete : %d\n",ret);
        printf("read tpm0 data after tpm_sha1complete : ");
        for(i = 0; i < ret; i++){
                printf("%02x ",buf[i] );
        }
        printf("\n");    
    
    close(fd);
    

    return 0;
}


 

运行过程:

初始化TPM模拟器#tpmd deactivated#killall tpmd#tpmd clear

启动软TPM#modprobe tpmd_dev

#tpmd -f -d tpmd -h查看启动参数)//如果遇到异常,则用tpmd –f –d clear启动

 

然后执行:

root@lmf-virtual-machine:/home/lmf/workspace/tpmtest# gcc -o tpmt tpmrandom.croot@lmf-virtual-machine:/home/lmf/workspace/tpmtest# ./tpmt

 

 

结果如下:sizeof(random_cmd): 14data in random_cmd: 00c10000000e0000004600000008nret of read random tpm0: 22read tpm0 random data: 00 c4 00 00 00 16 00 00 00 00 00 00 00 08 00 dd cd f9 fc 69 56 21 nsizeof(tpm_sha1start): 10data in tpm_sha1start: 00c10000000a000000a0nret of read tpm0 after tpm_sha1start : 14read tpm0 tpm_sha1start data: 00 c4 00 00 00 0e 00 00 00 00 00 00 08 00 nsizeof(tpm_sha1complete): 78data in tpm_sha1complete: 00c10000004e000000a2000000400102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f40nret of read tpm0 after tpm_sha1complete : 30read tpm0 data after tpm_sha1complete : 00 c4 00 00 00 1e 00 00 00 00 92 cb 89 df 62 d9 00 b3 50 d9 3e 42 25 ca 6f 08 1d 54 7a 28


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值