VirtualBox

source: vbox/trunk/src/libs/libtpms-0.9.0/tests/tpm2_pcr_read.c@ 97642

Last change on this file since 97642 was 91612, checked in by vboxsync, 3 years ago

src/libs: Export libtpms-0.9.0, bugref:10078

File size: 10.2 KB
Line 
1#include <stdio.h>
2#include <string.h>
3#include <assert.h>
4#include <stdlib.h>
5
6#include <libtpms/tpm_library.h>
7#include <libtpms/tpm_error.h>
8#include <libtpms/tpm_memory.h>
9
10static void dump_array(const char *h, const unsigned char *d, size_t dlen)
11{
12 size_t i;
13
14 fprintf(stderr, "%s\n", h);
15 for (i = 0; i < dlen; i++) {
16 fprintf(stderr, "%02x ", d[i]);
17 if ((i & 0xf) == 0xf)
18 fprintf(stderr, "\n");
19 }
20 fprintf(stderr, "\n");
21}
22
23int main(void)
24{
25 unsigned char *rbuffer = NULL;
26 uint32_t rlength;
27 uint32_t rtotal = 0;
28 TPM_RESULT res;
29 int ret = 1;
30 unsigned char *perm = NULL;
31 uint32_t permlen = 0;
32 unsigned char *vol = NULL;
33 uint32_t vollen = 0;
34 unsigned char startup[] = {
35 0x80, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00,
36 0x01, 0x44, 0x00, 0x00
37 };
38
39 unsigned char tpm2_pcr_read[] = {
40 0x80, 0x01, // TPM_ST_NO_SESSIONS
41 0x00, 0x00, 0x00, 0x26, // command size
42 0x00, 0x00, 0x01, 0x7e, // TPM_CC_PCR_Read
43 0x00, 0x00, 0x00, 0x04, // TPML_PCR_SELECTION
44 0x00, 0x04, // TPMI_ALG_HASH, SHA1=4
45 0x03, // size of the select
46 0x01, 0x00, 0x10, // pcrSelect
47 0x00, 0x0b, // TPMI_ALG_HASH, SHA256=11
48 0x03, // size of the select
49 0x01, 0x00, 0x10, // pcrSelect
50 0x00, 0x0c, // TPMI_ALG_HASH, SHA384=12
51 0x03, // size of the select
52 0x01, 0x00, 0x10, // pcrSelect
53 0x00, 0x0d, // TPMI_ALG_HASH, SHA512=13
54 0x03, // size of the select
55 0x01, 0x00, 0x10 // pcrSelect
56 };
57 const unsigned char tpm2_pcr_read_exp_resp[] = {
58 0x80, 0x01, 0x00, 0x00, 0x01, 0x86, 0x00, 0x00,
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00,
60 0x00, 0x04, 0x00, 0x04, 0x03, 0x01, 0x00, 0x10,
61 0x00, 0x0b, 0x03, 0x01, 0x00, 0x10, 0x00, 0x0c,
62 0x03, 0x01, 0x00, 0x10, 0x00, 0x0d, 0x03, 0x01,
63 0x00, 0x10, 0x00, 0x00, 0x00, 0x08, 0x00, 0x14,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0xff, 0xff,
67 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
68 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
69 0xff, 0xff, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0xff, 0xff,
74 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
75 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
76 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
77 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x30,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
82 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
83 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
84 0x00, 0x30, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
85 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
86 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
87 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
88 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
89 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
90 0xff, 0xff, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
92 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
94 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
95 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
97 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0xff, 0xff,
99 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
100 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
101 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
103 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
104 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
106 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
107 };
108
109 res = TPMLIB_ChooseTPMVersion(TPMLIB_TPM_VERSION_2);
110 if (res) {
111 fprintf(stderr, "TPMLIB_ChooseTPMVersion() failed: 0x%02x\n", res);
112 goto exit;
113 }
114
115 res = TPMLIB_MainInit();
116 if (res) {
117 fprintf(stderr, "TPMLIB_MainInit() failed: 0x%02x\n", res);
118 goto exit;
119 }
120
121 res = TPMLIB_Process(&rbuffer, &rlength, &rtotal, startup, sizeof(startup));
122 if (res) {
123 fprintf(stderr, "TPMLIB_Process(Startup) failed: 0x%02x\n", res);
124 goto exit;
125 }
126
127 res = TPMLIB_Process(&rbuffer, &rlength, &rtotal,
128 tpm2_pcr_read, sizeof(tpm2_pcr_read));
129 if (res) {
130 fprintf(stderr, "TPMLIB_Process(TPM2_PCR_Read) failed: 0x%02x\n",
131 res);
132 goto exit;
133 }
134
135 if (rlength != sizeof(tpm2_pcr_read_exp_resp)) {
136 fprintf(stderr, "Expected response is %zu bytes, but got %u.\n",
137 sizeof(tpm2_pcr_read_exp_resp), rlength);
138 goto exit;
139 }
140
141 if (memcmp(rbuffer, tpm2_pcr_read_exp_resp, rlength)) {
142 fprintf(stderr, "Expected response is different than received one.\n");
143 dump_array("actual:", rbuffer, rlength);
144 dump_array("expected:", tpm2_pcr_read_exp_resp, sizeof(tpm2_pcr_read_exp_resp));
145 goto exit;
146 }
147
148 /* Extend PCR 10 with string '1234' */
149 unsigned char tpm2_pcr_extend[] = {
150 0x80, 0x02, 0x00, 0x00, 0x00, 0x41, 0x00, 0x00,
151 0x01, 0x82, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00,
152 0x00, 0x09, 0x40, 0x00, 0x00, 0x09, 0x00, 0x00,
153 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
154 0x0b, 0x31, 0x32, 0x33, 0x34, 0x00, 0x00, 0x00,
155 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
156 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
157 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
158 0x00
159 };
160
161 res = TPMLIB_Process(&rbuffer, &rlength, &rtotal,
162 tpm2_pcr_extend, sizeof(tpm2_pcr_extend));
163 if (res != TPM_SUCCESS) {
164 fprintf(stderr,
165 "TPMLIB_Process(TPM2_PCR_Extend) failed: 0x%02x\n", res);
166 goto exit;
167 }
168
169 /* Read value of PCR 10 */
170 unsigned char tpm2_pcr10_read[] = {
171 0x80, 0x01, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00,
172 0x01, 0x7e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0b,
173 0x03, 0x00, 0x04, 0x00
174 };
175
176 res = TPMLIB_Process(&rbuffer, &rlength, &rtotal,
177 tpm2_pcr10_read, sizeof(tpm2_pcr10_read));
178 if (res) {
179 fprintf(stderr, "TPMLIB_Process(PCR10 Read) failed: 0x%02x\n", res);
180 goto exit;
181 }
182
183 const unsigned char tpm2_pcr10_read_resp[] = {
184 0x80, 0x01, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00,
185 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00,
186 0x00, 0x01, 0x00, 0x0b, 0x03, 0x00, 0x04, 0x00,
187 0x00, 0x00, 0x00, 0x01, 0x00, 0x20, 0x1f, 0x7f,
188 0xb1, 0x00, 0xe1, 0xb2, 0xd1, 0x95, 0x19, 0x4b,
189 0x58, 0xe7, 0xc3, 0x09, 0xa5, 0x86, 0x30, 0x7c,
190 0x34, 0x64, 0x19, 0xdc, 0xb2, 0xd5, 0x9f, 0x52,
191 0x2b, 0xe7, 0xf0, 0x94, 0x51, 0x01
192 };
193
194 if (memcmp(tpm2_pcr10_read_resp, rbuffer, rlength)) {
195 fprintf(stderr, "TPM2_PCRRead(PCR10) did not return expected result\n");
196 dump_array("actual:", rbuffer, rlength);
197 dump_array("expected:", tpm2_pcr10_read_resp, sizeof(tpm2_pcr10_read_resp));
198 goto exit;
199 }
200
201 /* save permanent and volatile state */
202 res = TPMLIB_GetState(TPMLIB_STATE_PERMANENT, &perm, &permlen);
203 if (res) {
204 fprintf(stderr, "TPMLIB_GetState(PERMANENT) failed: 0x%02x\n", res);
205 goto exit;
206 }
207
208 res = TPMLIB_GetState(TPMLIB_STATE_VOLATILE, &vol, &vollen);
209 if (res) {
210 fprintf(stderr, "TPMLIB_GetState(VOLATILE) failed: 0x%02x\n", res);
211 goto exit;
212 }
213
214 /* terminate and resume where we left off */
215 TPMLIB_Terminate();
216
217 res = TPMLIB_SetState(TPMLIB_STATE_PERMANENT, perm, permlen);
218 if (res) {
219 fprintf(stderr, "TPMLIB_SetState(PERMANENT) failed: 0x%02x\n", res);
220 goto exit;
221 }
222
223 res = TPMLIB_SetState(TPMLIB_STATE_VOLATILE, vol, vollen);
224 if (res) {
225 fprintf(stderr, "TPMLIB_SetState(VOLATILE) failed: 0x%02x\n", res);
226 goto exit;
227 }
228
229 res = TPMLIB_MainInit();
230 if (res) {
231 fprintf(stderr, "TPMLIB_MainInit() after SetState failed: 0x%02x\n", res);
232 goto exit;
233 }
234
235 res = TPMLIB_Process(&rbuffer, &rlength, &rtotal,
236 tpm2_pcr10_read, sizeof(tpm2_pcr10_read));
237 if (res) {
238 fprintf(stderr,
239 "TPMLIB_Process(PCR10 Read) after SetState failedL 0x%02x\n",
240 res);
241 goto exit;
242 }
243
244 if (memcmp(tpm2_pcr10_read_resp, rbuffer, rlength)) {
245 fprintf(stderr,
246 "TPM2_PCRRead(PCR10) after SetState did not return expected "
247 "result\n");
248 goto exit;
249 }
250
251 unsigned char tpm2_shutdown[] = {
252 0x80, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00,
253 0x01, 0x45, 0x00, 0x00
254 };
255
256 res = TPMLIB_Process(&rbuffer, &rlength, &rtotal,
257 tpm2_shutdown, sizeof(tpm2_shutdown));
258 if (res) {
259 fprintf(stderr,
260 "TPMLIB_Process(Shutdown) after SetState failed: 0x%02x\n",
261 res);
262 goto exit;
263 }
264
265 unsigned char tpm2_shutdown_resp[] = {
266 0x80, 0x01, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00,
267 0x00, 0x00
268 };
269
270 if (memcmp(tpm2_shutdown_resp, rbuffer, rlength)) {
271 fprintf(stderr,
272 "TPM2_PCRRead(Shutdown) after SetState did not return expected "
273 "result\n");
274 goto exit;
275 }
276
277 ret = 0;
278
279 fprintf(stdout, "OK\n");
280
281exit:
282 free(perm);
283 free(vol);
284 TPMLIB_Terminate();
285 TPM_Free(rbuffer);
286
287 return ret;
288}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette