VirtualBox

source: vbox/trunk/src/libs/libtpms-0.10.0/tests/tpm2_pcr_read.c

Last change on this file was 108932, checked in by vboxsync, 5 weeks ago

libtpms-0.10.0: Applied and adjusted our libtpms changes to 0.9.6. jiraref:VBP-1320

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

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