实验使用TPM_Emulator代替TPM硬件,原理是一样的。(学习网站:http://blog.csdn.net/buaa_shang/article/details/26157253)

1.登录系统后通过命令启动TPM模拟器:

sudo modprobe tpmd_dev

sudo tpmd -f -d clear

2.启动TrouSerS软件栈

sudo tcsd -e -f

//另外可创建一个文件夹,里面新建一个.c文件,可用vim编辑器编写,保存后,用gcc工具对.c文件进行编译,然后执行即可。

shm@shm-Junyi-M580:~$ mkdir myFiles
shm@shm-Junyi-M580:~$ cd myFiles/
shm@shm-Junyi-M580:~/myFiles$ vim tpm1.c

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <sys/stat.h>
  5 #include <sys/types.h>
  6 
  7 #include <tss/tss_error.h>
  8 #include <tss/platform.h>
  9 #include <tss/tss_defines.h>
 10 #include <tss/tss_typedef.h>
 11 #include <tss/tss_structs.h>
 12 #include <tss/tspi.h>
 13 #include <trousers/trousers.h>
 14 
 15 #define Debug(message, tResult) printf("%s : %s\n", message, (char *)Trspi_Error_String(result))
 16 void printMenu();
 17 
 18 int main(int argc, char **argv)
 19 {
 20     TSS_HCONTEXT     hContext;
 21     TSS_HTPM        hTPM;
 22     TSS_HPCRS        hPcrs;
 23     TSS_HENCDATA    hEncData;
 24     TSS_HENCDATA    hRetrieveData;
 25     TSS_RESULT         result;
 26     TSS_HKEY         hSRK = 0;
 27     TSS_HPOLICY        hSRKPolicy = 0;
 28     TSS_UUID        SRK_UUID = TSS_UUID_SRK;
 29 
 30     BYTE             wks[20];
 31     BYTE             *pubKey;
 32     UINT32            pubKeySize;
 33     BYTE            *rgbPcrValue;
 34     UINT32            ulPcrLen;
 35     BYTE            *encData;
 36     UINT32            encDataSize;
 37     BYTE            *outstring;
 38     UINT32            outlength;
 39     FILE            *fout, *fin;
 40     int             i;
 41     UINT32            j;
 42     BYTE            valueToExtend[250];
 43     int             count = 0;
 44     int             pcrToExtend = 0;
 45     
 46 
 47     memset(wks, 0, 20);
 48     memset(valueToExtend, 0, 250);
 49 
 50     //Pick the TPM you are talking to. 
 51     //In this case, it is the system TPM(indicated with NULL)
 52     result = Tspi_Context_Create(&hContext);
 53     Debug("Create Context", result);
 54 
 55     result = Tspi_Context_Connect(hContext, NULL);
 56     Debug("Context Connect", result);
 57 
 58     //Get the TPM handle
 59     result = Tspi_Context_GetTpmObject(hContext, &hTPM);
 60     Debug("Get TPM Handle", result);
 61 
 62     //Get the SRK handle
 63     result = Tspi_Context_LoadKeyByUUID(hContext, TSS_PS_TYPE_SYSTEM, SRK_UUID, &hSRK);
 64     Debug("Get the SRK handle", result);
 65 
 66     //Get the SRK policy
 67     result = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &hSRKPolicy);
 68     Debug("Get the SRK policy", result);
 69 
 70     //Then set the SRK policy to be the well known secret
 71     result = Tspi_Policy_SetSecret(hSRKPolicy, TSS_SECRET_MODE_SHA1, 20, wks);
 72 
 73 
 74     //输出所有PCR寄存器内的值
 75     /*********************/
 76     for (j = 0; j < 24; j++)
 77     {
 78         result = Tspi_TPM_PcrRead(hTPM, j, &ulPcrLen, &rgbPcrValue);
 79         printf("PCR %02d ", j);
 80         for (i = 0; i < 19; i++)
 81             printf("%02x", *(rgbPcrValue + i));
 82         printf("\n");
 83     }
 84     /*********************/
 85     
 86     //Display each command line argument.
 87     printf("\n Command line arguments:\n");
 88     for (count = 0; count <argc; count++)
 89         printf("argv[%d] : %s\n", count, argv[count]);
 90     
 91     //Examine command line arguments.
 92     if (argc >= 3)
 93     {
 94         if (strcmp(argv[1],"-p") == 0)
 95         {
 96             pcrToExtend = atoi(argv[2]);
 97             if (pcrToExtend < 0 || pcrToExtend > 23)
 98             {
 99                 printMenu();
100                 return 0;
101             }
102         }
103 
104         if (argc == 5)
105         {
106             if (strcmp(argv[3], "-v") == 0)
107                 memcpy(valueToExtend, argv[4], strlen(argv[4]));
108         }
109         else    //Use default value.
110         {
111             memcpy(valueToExtend, "abcdefghijklmnopqrst", 20);
112         }
113     }
114     else
115     {
116         printMenu();
117         return 0;
118     }
119 
120     //Extend the value
121     result = Tspi_TPM_PcrExtend(hTPM, pcrToExtend, 20, (BYTE *)valueToExtend, NULL, &ulPcrLen, &rgbPcrValue);
122     Debug("Extended the PCR", result);
123 
124     //输出所有PCR寄存器内的值
125     /*********************/
126     for (j = 0; j < 24; j++)
127     {
128         result = Tspi_TPM_PcrRead(hTPM, j, &ulPcrLen, &rgbPcrValue);
129         printf("PCR %02d ", j);
130         for (i = 0; i < 19; i++)
131             printf("%02x", *(rgbPcrValue + i));
132         printf("\n");
133     }
134     /*********************/
135     
136 
137     //Clean up
138     Tspi_Context_FreeMemory(hContext, NULL);
139     Tspi_Context_Close(hContext);
140     
141     return 0;
142 }
143 
144 void printMenu()
145 {
146     printf("\nChangePCRn Help Menu:\n");
147     printf("\t -p PCR regiter to extend(0-23)\n");
148     printf("\t -v Value to be extended into PCR(abc...)\n");
149     printf("\t Note: -v argument is optional and a default value will be used if no value is provided\n");
150     printf("\t Example: ChangePCRn -p 10 -v abcdef\n");
151 }
View Code

相关文章:

  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2022-02-21
  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
  • 2021-06-22
猜你喜欢
  • 2022-12-23
  • 2021-09-25
  • 2022-12-23
  • 2021-11-06
  • 2022-02-12
  • 2022-12-23
相关资源
相似解决方案