img_hash_tpm-pcr10.c

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

#include <tss/tss_error.h>
#include <tss/platform.h>
#include <tss/tss_defines.h>
#include <tss/tss_typedef.h>
#include <tss/tss_structs.h>
#include <tss/tspi.h>
#include <trousers/trousers.h>

#define Debug(message, tResult) printf("%s : %s\n", message, (char *)Trspi_Error_String(result))

int main()
{

    printf("#########程序开始运行:###########\n");
    TSS_HCONTEXT   hContext; //上下文
    TSS_RESULT  result;
    TSS_HTPM        hTPM;
    TSS_HPCRS       hPcrs;
    int       i;
    TSS_HKEY        hSRK = 0;
    TSS_HPOLICY     hSRKPolicy = 0;
    TSS_UUID        SRK_UUID = TSS_UUID_SRK;

    BYTE            wks[20];
    //UINT32            j;
    BYTE            valueToExtend[250];
    int             count = 0;



    FILE * sfp;
    char *fp = "/home/shm/tpm-test/test-tpm1/usr.img";
    char  *buf;

    TSS_HHASH   hHash;
    BYTE        *digest, *data;
    UINT32      digestLen;

    if((sfp=fopen(fp,"rb"))==NULL)        //以只读方式打开
   {
        printf("!Source file cannot be opened\n");
        return 0;
   }
   //获取文件的长度
   long int length;
   fseek(sfp,0,SEEK_END);
   length = ftell(sfp);
   fseek(sfp,0,SEEK_SET);
   printf("\n*the file's length = %ld \n",length);

   //以字符串形式输出该文件内容
   buf = (char *)malloc(length);
   fread(buf,length,1,sfp);
   //*(buf+length-1)='\0';
   printf("*the file's contests :\n %s\n",buf);

    /**********上下文及其初始化连接*********/
    //选择你正在与之通信的TPM,默认情况下是系统TPM(用NULL表示)
    result = Tspi_Context_Create(&hContext);
    Debug(" 1-Create Context",result);

    result = Tspi_Context_Connect(hContext,NULL);
    Debug(" 2-Context Connect", result);

    //Get the TPM handle
    result = Tspi_Context_GetTpmObject(hContext, &hTPM);
    Debug("Get TPM Handle", result);

    //Get the SRK handle
    result = Tspi_Context_LoadKeyByUUID(hContext, TSS_PS_TYPE_SYSTEM, SRK_UUID, &hSRK);
    Debug("Get the SRK handle", result);

    //Get the SRK policy
    result = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &hSRKPolicy);
    Debug("Get the SRK policy", result);

    //Then set the SRK policy to be the well known secret
    result = Tspi_Policy_SetSecret(hSRKPolicy, TSS_SECRET_MODE_SHA1, 20, wks);


    /*********散列对象*****************/
    data = buf;
    //创建散列值对象
    Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_HASH, TSS_HASH_SHA1, &hHash);
    //将数据散列,因为对象类型是 TSS_HASH_SHA1,所以TSS知道用SHA-1算法进行散列
    Tspi_Hash_UpdateHashValue(hHash, strlen(data), data);
    //取回散列值对象的摘要
    Tspi_Hash_GetHashValue(hHash, &digestLen, &digest);

    //输出被散列的源数据对象
    printf("----源数据的二进制形式: \n");
    for (i = 0; i < strlen(data); i++)
            printf("%02x", *(data + i));
    printf("\n*the file's length = %ld \n",length);

    //输出被散列后的hash值     //将散列后的hash值写入到文件dfile.txt中
    printf("\n----被散列后的hash值: \n");
    for (i = 0; i < digestLen; i++)
         printf("%02x", *(digest + i));
    printf("\n*the hashValue's length = %d \n",digestLen);

    /***************************************/
    //将得到的hash结果值扩展到TPM中
    BYTE        *rgbPcrValue;
    UINT32      ulPcrLen;
    //BYTE      valueToExtend[digestLen];  //扩展值
    //UINT32            j;
    UINT32      pcrToExtend = 10;  //要扩展的PCR序号

    //先输出要扩展的PCR的值
    result = Tspi_TPM_PcrRead(hTPM, pcrToExtend, &ulPcrLen, &rgbPcrValue);
        printf("PCR %02d ", pcrToExtend);
        for (i = 0; i < 20; i++)
            printf("%02x", *(rgbPcrValue + i));
        printf("\n");
    //扩展操作
    result = Tspi_TPM_PcrExtend(hTPM, pcrToExtend,20,digest, NULL, &ulPcrLen, &rgbPcrValue);
    Debug("*********Extend the PCR**********", result);

    //再查看扩展后的PCR的值
    result = Tspi_TPM_PcrRead(hTPM, pcrToExtend, &ulPcrLen, &rgbPcrValue);
        printf("PCR %02d ", pcrToExtend);
        for (i = 0; i < 20; i++)
            printf("%02x", *(rgbPcrValue + i));
        printf("\n");

 /*********释放资源************/ 
    //关闭文件     
      fclose(sfp);

    //清理上下文对象
    Tspi_Context_FreeMemory(hContext, NULL);
    Tspi_Context_Close(hContext);
    printf("\n######释放资源成功,程序结束。#########\n");

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值