Chinaunix首页 | 论坛 | 博客
  • 博客访问: 52716
  • 博文数量: 5
  • 博客积分: 1434
  • 博客等级: 上尉
  • 技术积分: 73
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-15 10:59
文章分类
文章存档

2011年(1)

2010年(1)

2009年(3)

我的朋友

分类: 网络与安全

2009-07-12 00:32:22

tpm emulator测试程序
作者:浪迹天涯
    一个简单的tpm emulator的测试程序,程序实现了通过软tpm产生随机数和求哈希值的功能。注意,此程序是基于tpm驱动层的,与trousers软件没有任何关系。可以通过这个程序验证你的tpm emulator是否安装成功。 
 
代码:tpmrandomsha1.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;
}

 

Makefile:

 

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

 

测试参考结果:

 

sizeof(random_cmd): 14
data in random_cmd: 00c10000000e0000004600000008
ret of read random tpm0: 22
read tpm0 random data: 00 c4 00 00 00 16 00 00 00 00 00 00 00 08 20 c2 10 97 bf cb c3 ec
sizeof(tpm_sha1start): 10
data in tpm_sha1start: 00c10000000a000000a0
ret of read tpm0 after tpm_sha1start : 14
read tpm0 tpm_sha1start data: 00 c4 00 00 00 0e 00 00 00 00 00 00 08 00
sizeof(tpm_sha1complete): 78
data in tpm_sha1complete:00c10000004e000000a2000000400102030405060708090a0b0c0d0e0f10111213141516171819

1a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f40
ret of read tpm0 after tpm_sha1complete : 30
read 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

阅读(3077) | 评论(3) | 转发(0) |
0

上一篇:selinux原理介绍

下一篇:傻瓜更新redhat系统

给主人留下些什么吧!~~

chinaunix网友2010-01-16 22:11:57

为什么经过TPM模拟器读出的数据和原始数据不同啊?谢谢! qq:16975944

chinaunix网友2010-01-06 14:04:57

你好,能把你这个测试程序大致解释下吗,有些地方不是看得很懂,比如说 fd = open("/dev/tpm0",O_RDWR);,其中tpm0,这个文件的含义,为什么会选择这个文件?谢谢!

chinaunix网友2009-12-07 21:53:36

你好,你的QQ号是多少啊、?