1. 程式人生 > >TPM中生成hash摘要-學習1

TPM中生成hash摘要-學習1

iges ets with eem res str tle strlen 執行文件

/*在 執行文件之前對其進行完整性度量的思路整理:
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