码迷,mamicode.com
首页 > 其他好文 > 详细

TPM中生成hash摘要-学习1

时间:2017-11-11 11:26:23      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:相同   clu   get   count   eth   typedef   思路   date   执行文件   

/*在 执行文件之前对其进行完整性度量的思路整理:
1、将源文件数据散列(SHA-1算法)
2、获取散列值对象的摘要(MAC)
3、将hash值-摘要存储到PCR中
4、对将要执行的文件进行1-2步操作,将得到的结果值(hash值-摘要值)与
第3步的PCR中的值进行比较:如果相同则可信,执行文件;否则文件有可能被篡改,拒绝执行!
*/
#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>  

#include "trousers/tss.h"

 
#define Debug(message, tResult) printf("%s : %s\n", message, (char *)Trspi_Error_String(result))  
 
int main(int argc, char **argv)  
{
    TSS_HCONTEXT    hContext;  
    TSS_HTPM        hTPM;  
    TSS_HPCRS       hPcrs;
    TSS_HHASH       hHash;
    BYTE        *digest, *data = "data to hash";
    UINT32        digestLen;

    TSS_HENCDATA    hEncData;  
    TSS_HENCDATA    hRetrieveData;  
    TSS_RESULT      result;  
    TSS_HKEY        hSRK = 0;  
    TSS_HPOLICY     hSRKPolicy = 0;  
    TSS_UUID        SRK_UUID = TSS_UUID_SRK;  
 
    BYTE            wks[20];  
    BYTE            *pubKey;  
    UINT32          pubKeySize;  
    BYTE            *rgbPcrValue;  
    UINT32          ulPcrLen;  
    BYTE            *encData;  
    UINT32          encDataSize;  
    BYTE            *outstring;  
    UINT32          outlength;  
    FILE            *fout, *fin;  
    int             i = 0;  
    UINT32          j = 0;  
    BYTE            valueToExtend[160];  
    int             count = 0;  
    int             pcrToExtend = 0;  

    memset(wks, 0, 20);  
    memset(valueToExtend, 0, 160);  

/*创建一个上下文对象,并连接到本地TCS提供者*/
//Pick the TPM you are talking to.   
    //In this case, it is the system TPM(indicated with NULL)  
    result = Tspi_Context_Create(&hContext);  
    Debug("Create Context", result);

    result = Tspi_Context_Connect(hContext, NULL);  
    Debug("Context Connect", result);   
/*获取隐式创建的TPM对象的句柄*/
//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);  

    /*********************/  

/*创建散列值对象*/
    Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_HASH,                     TSS_HASH_SHA1, &hHash);
/*将数据散列,由TSS用SHA-1算法——>160位输出*/
    Tspi_Hash_UpdateHashValue(hHash, strlen(data), data);
/*取回散列值对象的摘要*/
    result = Tspi_Hash_GetHashValue(hHash, &digestLen, &digest);
     Debug("Get the Hash Value", result);  
  //输出 摘要值  
    printf("HashValue: ");
    for (i = 0; i < 20; i++)  
            printf("%02x", *(digest + i));    
    printf("\n");
//输出 原数据值  
    printf("HashData: %s", data);
    printf("\n");  
 /*********************/  
  //输出 PCR0 寄存器内的初始值  
    /*********************/  
        result = Tspi_TPM_PcrRead(hTPM, j, &ulPcrLen, &rgbPcrValue);  
        printf("PCR 0 value: ");         
        for (i = 0; i < 20; i++)  
            printf("%02x", *(rgbPcrValue + i));  
        printf("\n");  
       
//扩展摘要值到PCR0
//Extend the value  
    result = Tspi_TPM_PcrExtend(hTPM, pcrToExtend, 20, digest, NULL, &ulPcrLen, &rgbPcrValue);  
    Debug("Extended the PCR0", result);  

//再次输出 PCR0 的值  
    /*********************/  
        result = Tspi_TPM_PcrRead(hTPM, j, &ulPcrLen, &rgbPcrValue);  
        printf("PCR 0 New value: ");         
        for (i = 0; i < 20; i++)  
            printf("%02x", *(rgbPcrValue + i));  
        printf("\n");  
       
    /*********************/  
 
    //Clean up  
    Tspi_Context_FreeMemory(hContext, NULL);  
    Tspi_Context_Close(hContext);  
      
    return 0;   
          
}

TPM中生成hash摘要-学习1

标签:相同   clu   get   count   eth   typedef   思路   date   执行文件   

原文地址:http://www.cnblogs.com/summer2017/p/7806465.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!