#include <stdio.h>
#include <sys/stat.h>
#include "include/trousers/tss.h"
#define CKERR if(result != TSS_SUCCESS) {perror("erro/n"); return 1;}
int main(){
TSS_HCONTEXT hContext;
TSS_HTPM hTPM;
TSS_HPOLICY hOwnerPolicy;
TSS_HNVSTORE hNVStore;
TSS_RESULT result;
TSS_HHASH hHash;
int i = 0;
BYTE *data;
BYTE *digest;
UINT32 len, len1 = 20;
FILE *file;
BYTE *buffer;
struct stat st;
int ret;
result = Tspi_Context_Create(&hContext); CKERR
result = Tspi_Context_Connect(hContext, NULL); CKERR
//result = Tspi_Context_GetTpmObject(hContext, &hTPM); CKERR
//result = Tspi_GetPolicyObject(hTPM, TSS_POLICY_USAGE, &hOwnerPolicy); CKERR
//result = Tspi_Policy_SetSecret(hOwnerPolicy, TSS_SECRET_MODE_POPUP, 0, NULL); CKERR
result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_NV, 0, &hNVStore); CKERR
result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_HASH, TSS_HASH_SHA1, &hHash); CKERR
result = Tspi_SetAttribUint32(hNVStore, TSS_TSPATTRIB_NV_INDEX, 0, 1); CKERR
//result = Tspi_SetAttribUint32(hNVStore, TSS_TSPATTRIB_NV_PERMISSIONS, 0, TPM_NV_PER_AUTHWRITE); CKERR
result = Tspi_SetAttribUint32(hNVStore, TSS_TSPATTRIB_NV_DATASIZE, 0, 20); CKERR
file = fopen("/boot/vmlinuz-2.6.30", "rb");
if(!file) {
perror("Open the file error/n");
return 0;
}
ret = stat("/boot/vmlinuz-2.6.30", &st);
if(ret) perror("stat error!/n");
len = st.st_size;
buffer =(BYTE *) malloc(len * sizeof(BYTE));
if(!buffer) perror("malloc error/n");
ret = fread(buffer, len, 1, file);
result = Tspi_Hash_UpdateHashValue(hHash, len, buffer); CKERR
result = Tspi_Hash_GetHashValue(hHash, &len, &data); CKERR
printf("The hash value is:/n");
for (; i < len; i++)
printf("%3x", data[i]);
printf("/n");
digest = (BYTE *) malloc(20 * sizeof(BYTE));
result = Tspi_NV_WriteValue(hNVStore, 0, 20, data); CKERR
result = Tspi_NV_ReadValue(hNVStore, 0, &len1, &digest); CKERR
printf("The readed hash value is:/n");
for (i=0; i < 20; i++)
printf("%3x", digest[i]);
printf("/n");
return 1;
}
kernel to/from NV
最新推荐文章于 2021-05-01 23:10:36 发布