代码说明:
在模拟的TPM环境下,tdd层计算文件散列值并将其扩展至PCR中。
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# define LENGTH 160
# define TPM_TAG_RQU_COMMAND 193
# define TPM_ORD_SHA1Start 160
# define TPM_ORD_SHA1Update 161
# define TPM_ORD_SHA1Complete 162
# define TPM_ORD_PCRRead 21
# define TPM_ORD_Extend 20
int calculate( char* filename, char *hash_buf)
{
unsigned int i,fd,fd1;
int res, ret;
unsigned char buf[64]={0,0} ;
int bytes_to_copy=0;
int filesize=0;
int j=0;
int buf_size = sizeof(buf);
// unsigned char hash_buf[20];
struct stat attribute;
unsigned char tpm_sha1start[ ] = { 0, TPM_TAG_RQU_COMMAND,
0, 0, 0, 10,
0, 0, 0, TPM_ORD_SHA1Start};
unsigned char tpm_sha1update[ ]={0,TPM_TAG_RQU_COMMAND,
0,0,0,22,
0,0,0,TPM_ORD_SHA1Update,
0,0,0,64,
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9],
buf[10], buf[11], buf[12], buf[13], buf[14], buf[15], buf[16], buf[17], buf[18],
buf[19],buf[20], buf[21], buf[22], buf[23], buf[24], buf[25], buf[26], buf[27], buf[28],
buf[29], buf[30], buf[31], buf[32], buf[33],buf[34],buf[35],buf[36],buf[37],buf[38],
buf[39],buf[40],buf[41],buf[42],buf[43],buf[44],buf[45],buf[46],buf[47],buf[48],buf[49],
buf[50],buf[51],buf[52],buf[53],buf[54],buf[55],buf[56],buf[57],buf[58],buf[59],buf[60],
buf[61],buf[62],buf[63]};
unsigned char tpm_sha1complete[ ] = { 0, TPM_TAG_RQU_COMMAND,
0, 0, 0, 78, 0, 0, 0, TPM_ORD_SHA1Complete,
0, 0, 0, 64};
fd = open ( "/dev/tpm" , O_RDWR) ;
if ( fd < 0) {
printf ( "Error: Open() failed: (%04x)\n " , fd) ;
return - 1;
}
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;
}
fd1=open(filename,O_RDWR);
if (fd1 < 0)
{
printf("Error opening file\n");
return -1;
}
stat(filename,&attribute);
filesize = attribute.st_size;
bytes_to_copy=filesize;
while (bytes_to_copy > 64)
{
read(fd1,buf,64);
memcpy(tpm_sha1update + 14, buf, 64);
res = write ( fd, tpm_sha1update, sizeof ( tpm_sha1update) ) ;
if ( res != sizeof ( tpm_sha1update) )
{
printf ( "Error: write tpm_sha1update failed: (%04x)\n " , res) ;
close ( fd) ;
return -1;
}
ret = read ( fd, & buf, buf_size) ;
bytes_to_copy = bytes_to_copy - 64;
}
memset(buf,0,64);
read(fd1,buf,bytes_to_copy);
unsigned char byteCnt = sizeof(tpm_sha1complete) + sizeof(unsigned char) * bytes_to_copy;
unsigned char *tmpBuf = (unsigned char*)malloc(byteCnt);//modify
memcpy(tmpBuf, tpm_sha1complete, sizeof(tpm_sha1complete));
tmpBuf[13] = (unsigned char)bytes_to_copy;
memcpy(tmpBuf + 14, buf,bytes_to_copy);
res = write ( fd, tmpBuf, byteCnt ) ;
if ( res != byteCnt ) {
printf ( "Error: write tpm_sha1complete failed: (%04x)\n " , res) ;
close ( fd) ;
return -1;
}
ret = read ( fd, & buf, 30) ;
for (i=10,j=0; i<ret,j<20; i++,j++)
hash_buf[j]=buf[i];
/* printf("hash result is: ");
for (j=0; j<20; j++)
printf( "%02x " ,hash_buf[j]);
printf ( "\n" ); */
close(fd1);
close (fd);
return 0;
}
int main( int argc, char * * argv)
{
unsigned char result[20];
unsigned char buf[64]={0,0};
unsigned char tpm_extend[ ]={0,TPM_TAG_RQU_COMMAND,
0,0,0,34,
0,0,0,TPM_ORD_Extend,
0,0,0,8,
1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,18,19,20};
int buf_size=sizeof(buf);
int i,fd,res,ret;
if (argc == 1)
{
printf("Missing arguments! Usage: %s {filename}\n \n",argv[0]);
return -1;
}
if (calculate(argv[1],result))
{
printf("Error during calculation\n");
return -1;
}
for(i=0;i<20;i++)
printf("%02x ",result[i]);
printf("\n");
//printf(" %s\n",argv[1]);
fd = open ( "/dev/tpm" , O_RDWR) ;
if ( fd < 0) {
printf ( "Error: Open() failed: (%04x)\n " , fd) ;
return - 1;
}
memcpy(tpm_extend + 14, result, 20);
printf ( "sizeof(tpm_extend): %d\n" , sizeof ( tpm_extend) ) ;
printf ( "data in tpm_extend: " ) ;
for ( i = 0; i < sizeof ( tpm_extend) ; i++ )
printf ( "%02x" , tpm_extend[i] ) ;
printf ( "\n" ) ;
res = write ( fd, tpm_extend, sizeof ( tpm_extend) ) ;
if ( res != sizeof ( tpm_extend) ) {
printf ( "Error: write tpm_extend failed: (%04x)\n " , res) ;
return -1;
}
ret = read ( fd, & buf, buf_size) ;
printf ( "ret of read tpm0 after tpm_extend : %d\n" , ret) ;
printf ( "read tpm0 data after tpm_extend : " ) ;
for ( i = 0; i < ret; i++ ) {
printf ( "%02x " , buf[i] );
}
printf ( "\n" );
close ( fd) ;
return 0;
}