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

 

说明 :在某些安装成功的TPM_Emulator机器中,对于tpm硬件的模拟文件不是tpm0,而是tpm,所以此时需要修改源程序中的

   fd = open ( "/dev/tpm0" , O_RDWR) ;语句为    fd = open ( "/dev/tpm" , O_RDWR) 后才能正确运行。

猜你喜欢

转载自blog.csdn.net/cicisensy/article/details/5291543
今日推荐