xref: /aosp_15_r20/external/vboot_reference/tests/tlcl_tests.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2013 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  *
5  * Tests for TPM lite library
6  */
7 
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 #include "2api.h"
14 #include "common/tests.h"
15 #include "host_common.h"
16 #include "tlcl.h"
17 #include "tlcl_internal.h"
18 
19 /* Mock data */
20 static char debug_info[4096];
21 static vb2_error_t mock_retval;
22 
23 /* Call to mocked vb2ex_tpm_send_recv() */
24 struct srcall
25 {
26 	const uint8_t *req;  /* Request */
27 	uint8_t *rsp;  /* Response */
28 	uint8_t rsp_buf[32];  /* Default response buffer, if not overridden */
29 	int req_size;  /* Request size */
30 	uint32_t req_cmd;  /* Request command code */
31 	int rsp_size;  /* Response size */
32 	vb2_error_t retval;  /* Value to return */
33 };
34 
35 #define MAXCALLS 8
36 static struct srcall calls[MAXCALLS];
37 static int ncalls;
38 
39 /**
40  * Reset mock data (for use before each test)
41  */
ResetMocks(void)42 static void ResetMocks(void)
43 {
44 	int i;
45 
46 	*debug_info = 0;
47 	mock_retval = VB2_SUCCESS;
48 
49 	memset(calls, 0, sizeof(calls));
50 	for (i = 0; i < MAXCALLS; i++)
51 		calls[i].rsp = calls[i].rsp_buf;
52 	ncalls = 0;
53 }
54 
55 /**
56  * Set response code and length for call <call_idx>.
57  */
SetResponse(int call_idx,uint32_t response_code,int rsp_size)58 static void SetResponse(int call_idx, uint32_t response_code, int rsp_size)
59 {
60 	struct srcall *c = calls + call_idx;
61 
62 	c->rsp_size = rsp_size;
63 	ToTpmUint32(c->rsp_buf + 6, response_code);
64 }
65 
66 /* Mocks */
67 
vb2ex_tpm_init(void)68 vb2_error_t vb2ex_tpm_init(void)
69 {
70 	return mock_retval;
71 }
72 
vb2ex_tpm_close(void)73 vb2_error_t vb2ex_tpm_close(void)
74 {
75 	return mock_retval;
76 }
77 
vb2ex_tpm_send_recv(const uint8_t * request,uint32_t request_length,uint8_t * response,uint32_t * response_length)78 uint32_t vb2ex_tpm_send_recv(const uint8_t *request, uint32_t request_length,
79 			     uint8_t *response, uint32_t *response_length)
80 {
81 	struct srcall *c = calls + ncalls++;
82 
83 	c->req = request;
84 	c->req_size = request_length;
85 
86 	/* Parse out the command code */
87 	FromTpmUint32(request + 6, &c->req_cmd);
88 
89 	// KLUDGE - remove
90 	printf("TSR [%d] %#x\n", ncalls-1, c->req_cmd);
91 
92 	memset(response, 0, *response_length);
93 	if (c->rsp_size)
94 		memcpy(response, c->rsp, c->rsp_size);
95 	*response_length = c->rsp_size;
96 
97 	return c->retval;
98 }
99 
vb2ex_tpm_get_random(uint8_t * buf,uint32_t length)100 vb2_error_t vb2ex_tpm_get_random(uint8_t *buf, uint32_t length)
101 {
102 	memset(buf, 0xa5, length);
103 	return VB2_SUCCESS;
104 }
105 
106 /**
107  * Test assorted tlcl functions
108  */
TlclTest(void)109 static void TlclTest(void)
110 {
111 	uint8_t buf[32], buf2[32];
112 
113 	ResetMocks();
114 	TEST_EQ(TlclLibInit(), VB2_SUCCESS, "Init");
115 
116 	ResetMocks();
117 	mock_retval = VB2_ERROR_MOCK;
118 	TEST_EQ(TlclLibInit(), mock_retval, "Init bad");
119 
120 	ResetMocks();
121 	TEST_EQ(TlclLibClose(), VB2_SUCCESS, "Close");
122 
123 	ResetMocks();
124 	mock_retval = VB2_ERROR_MOCK;
125 	TEST_EQ(TlclLibClose(), mock_retval, "Close bad");
126 
127 	ResetMocks();
128 	ToTpmUint32(buf + 2, 123);
129 	TEST_EQ(TlclPacketSize(buf), 123, "TlclPacketSize");
130 
131 	ResetMocks();
132 	ToTpmUint32(buf + 2, 10);
133 	TEST_EQ(TlclSendReceive(buf, buf2, sizeof(buf2)), 0, "SendReceive");
134 	TEST_PTR_EQ(calls[0].req, buf, "SendReceive req ptr");
135 	TEST_EQ(calls[0].req_size, 10, "SendReceive size");
136 
137 	ResetMocks();
138 	calls[0].retval = VB2_ERROR_MOCK;
139 	ToTpmUint32(buf + 2, 10);
140 	TEST_EQ(TlclSendReceive(buf, buf2, sizeof(buf2)), VB2_ERROR_MOCK,
141 		"SendReceive fail");
142 
143 	ResetMocks();
144 	SetResponse(0, 123, 10);
145 	ToTpmUint32(buf + 2, 10);
146 	TEST_EQ(TlclSendReceive(buf, buf2, sizeof(buf2)), 123,
147 		"SendReceive error response");
148 
149 	// TODO: continue self test (if needed or doing)
150 	// TODO: then retry doing self test
151 
152 }
153 
154 
155 /**
156  * Test send-command functions
157  */
SendCommandTest(void)158 static void SendCommandTest(void)
159 {
160 	ResetMocks();
161 	TEST_EQ(TlclStartup(), 0, "SaveState");
162 	TEST_EQ(calls[0].req_cmd, TPM_ORD_Startup, "  cmd");
163 
164 	ResetMocks();
165 	TEST_EQ(TlclSaveState(), 0, "SaveState");
166 	TEST_EQ(calls[0].req_cmd, TPM_ORD_SaveState, "  cmd");
167 
168 	ResetMocks();
169 	TEST_EQ(TlclResume(), 0, "Resume");
170 	TEST_EQ(calls[0].req_cmd, TPM_ORD_Startup, "  cmd");
171 
172 	ResetMocks();
173 	TEST_EQ(TlclSelfTestFull(), 0, "SelfTestFull");
174 	TEST_EQ(calls[0].req_cmd, TPM_ORD_SelfTestFull, "  cmd");
175 
176 	ResetMocks();
177 	TEST_EQ(TlclContinueSelfTest(), 0, "ContinueSelfTest");
178 	TEST_EQ(calls[0].req_cmd, TPM_ORD_ContinueSelfTest, "  cmd");
179 
180 	ResetMocks();
181 	TEST_EQ(TlclAssertPhysicalPresence(), 0,
182 		"AssertPhysicalPresence");
183 	TEST_EQ(calls[0].req_cmd, TSC_ORD_PhysicalPresence, "  cmd");
184 
185 	ResetMocks();
186 	TEST_EQ(TlclPhysicalPresenceCMDEnable(), 0,
187 		"PhysicalPresenceCMDEnable");
188 	TEST_EQ(calls[0].req_cmd, TSC_ORD_PhysicalPresence, "  cmd");
189 
190 	ResetMocks();
191 	TEST_EQ(TlclFinalizePhysicalPresence(), 0,
192 		"FinalizePhysicalPresence");
193 	TEST_EQ(calls[0].req_cmd, TSC_ORD_PhysicalPresence, "  cmd");
194 
195 	ResetMocks();
196 	TEST_EQ(TlclAssertPhysicalPresenceResult(), 0,
197 		"AssertPhysicalPresenceResult");
198 	TEST_EQ(calls[0].req_cmd, TSC_ORD_PhysicalPresence, "  cmd");
199 
200 	ResetMocks();
201 	TEST_EQ(TlclLockPhysicalPresence(), 0,
202 		"LockPhysicalPresence");
203 	TEST_EQ(calls[0].req_cmd, TSC_ORD_PhysicalPresence, "  cmd");
204 
205 	ResetMocks();
206 	TEST_EQ(TlclIsOwned(), 0, "IsOwned");
207 	TEST_EQ(calls[0].req_cmd, TPM_ORD_ReadPubek, "  cmd");
208 	ResetMocks();
209 	calls[0].retval = VB2_ERROR_MOCK;
210 	TEST_NEQ(TlclIsOwned(), 0, "IsOwned");
211 
212 	ResetMocks();
213 	TEST_EQ(TlclForceClear(), 0, "ForceClear");
214 	TEST_EQ(calls[0].req_cmd, TPM_ORD_ForceClear, "  cmd");
215 
216 	ResetMocks();
217 	TEST_EQ(TlclSetEnable(), 0, "SetEnable");
218 	TEST_EQ(calls[0].req_cmd, TPM_ORD_PhysicalEnable, "  cmd");
219 
220 	ResetMocks();
221 	TEST_EQ(TlclClearEnable(), 0, "ClearEnable");
222 	TEST_EQ(calls[0].req_cmd, TPM_ORD_PhysicalDisable, "  cmd");
223 
224 	ResetMocks();
225 	TEST_EQ(TlclSetDeactivated(0), 0, "SetDeactivated");
226 	TEST_EQ(calls[0].req_cmd, TPM_ORD_PhysicalSetDeactivated, "  cmd");
227 }
228 
229 /**
230  * NV spaces test
231  *
232  * TODO: check params/data read/written.
233  */
ReadWriteTest(void)234 static void ReadWriteTest(void)
235 {
236 	uint8_t buf[32];
237 
238 	ResetMocks();
239 	TEST_EQ(TlclDefineSpace(1, 2, 3), 0, "DefineSpace");
240 	TEST_EQ(calls[0].req_cmd, TPM_ORD_NV_DefineSpace, "  cmd");
241 
242 	ResetMocks();
243 	TEST_EQ(TlclUndefineSpace(1), 0, "UndefineSpace");
244 	// TPM1.2 use TPM_ORD_NV_DefineSpace with size 0 to delete space
245 	TEST_EQ(calls[0].req_cmd, TPM_ORD_NV_DefineSpace, "  cmd");
246 
247 	ResetMocks();
248 	TEST_EQ(TlclSetNvLocked(), 0, "SetNvLocked");
249 	TEST_EQ(calls[0].req_cmd, TPM_ORD_NV_DefineSpace, "  cmd");
250 
251 	ResetMocks();
252 	TEST_EQ(TlclWrite(1, buf, 3), 0, "Write");
253 	TEST_EQ(calls[0].req_cmd, TPM_ORD_NV_WriteValue, "  cmd");
254 
255 	ResetMocks();
256 	TEST_EQ(TlclRead(1, buf, 3), 0, "Read");
257 	TEST_EQ(calls[0].req_cmd, TPM_ORD_NV_ReadValue, "  cmd");
258 
259 	ResetMocks();
260 	TEST_EQ(TlclWriteLock(1), 0, "WriteLock");
261 	TEST_EQ(calls[0].req_cmd, TPM_ORD_NV_WriteValue, "  cmd");
262 
263 	ResetMocks();
264 	TEST_EQ(TlclReadLock(1), 0, "ReadLock");
265 	TEST_EQ(calls[0].req_cmd, TPM_ORD_NV_ReadValue, "  cmd");
266 
267 	ResetMocks();
268 	TEST_EQ(TlclSetGlobalLock(), 0, "SetGlobalLock");
269 	TEST_EQ(calls[0].req_cmd, TPM_ORD_NV_WriteValue, "  cmd");
270 }
271 
272 /**
273  * Test DefineSpaceEx
274  */
DefineSpaceExTest(void)275 static void DefineSpaceExTest(void) {
276 	uint8_t osap_response[] = {
277 		0x00, 0xc4, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00,
278 		0x00, 0x00, 0x02, 0x41, 0x3d, 0xce, 0x20, 0xa2,
279 		0x5a, 0xa5, 0x95, 0xbe, 0x26, 0xe8, 0x76, 0x74,
280 		0x6c, 0x61, 0xf7, 0xa7, 0x24, 0x17, 0xa1, 0x06,
281 		0xcf, 0x53, 0x6d, 0xd4, 0x26, 0x98, 0x68, 0x86,
282 		0xe6, 0xf6, 0x62, 0x58, 0xdb, 0xa2, 0x9f, 0x5b,
283 		0x18, 0xa6, 0xae, 0x36, 0x32, 0x5d,
284 	};
285 	uint8_t define_space_response[] = {
286 		0x00, 0xc5, 0x00, 0x00, 0x00, 0x33, 0x00, 0x00,
287 		0x00, 0x00, 0x42, 0xe6, 0x38, 0xc6, 0x37, 0x2a,
288 		0xf2, 0xfe, 0xb4, 0x01, 0x4b, 0x29, 0x63, 0x30,
289 		0x4e, 0x2f, 0x2e, 0x74, 0x58, 0xcd, 0x00, 0x40,
290 		0x42, 0x10, 0x40, 0xac, 0x93, 0x0c, 0xff, 0x8a,
291 		0xc4, 0x98, 0x78, 0xe3, 0xfe, 0x48, 0x5b, 0xb7,
292 		0xc8, 0x8d, 0xf4,
293 	};
294 	uint8_t owner_secret[TPM_AUTH_DATA_LEN] = { 0 };
295 	TPM_NV_AUTH_POLICY policy;
296 
297 	ResetMocks();
298 	calls[0].rsp = osap_response;
299 	calls[0].rsp_size = sizeof(osap_response);
300 	calls[1].rsp = define_space_response;
301 	calls[1].rsp_size = sizeof(define_space_response);
302 	TEST_EQ(TlclDefineSpaceEx(owner_secret, sizeof(owner_secret),
303 				  0x20000005, 0x2000, 0x10, NULL, 0),
304 		TPM_SUCCESS, "DefineSpace");
305 	TEST_EQ(calls[0].req_cmd, TPM_ORD_OSAP, "  osap cmd");
306 	TEST_EQ(calls[1].req_cmd, TPM_ORD_NV_DefineSpace, "  definespace cmd");
307 
308 	/* Pass an auth policy. */
309 	ResetMocks();
310 	calls[0].rsp = osap_response;
311 	calls[0].rsp_size = sizeof(osap_response);
312 	calls[1].rsp = define_space_response;
313 	calls[1].rsp_size = sizeof(define_space_response);
314 	TEST_EQ(TlclDefineSpaceEx(owner_secret, sizeof(owner_secret),
315 				  0x20000005, 0x2000, 0x10, &policy,
316 				  sizeof(policy)),
317 		TPM_SUCCESS, "DefineSpace");
318 	TEST_EQ(calls[0].req_cmd, TPM_ORD_OSAP, "  osap cmd");
319 	TEST_EQ(calls[1].req_cmd, TPM_ORD_NV_DefineSpace, "  definespace cmd");
320 
321 	/* Verify that the response gets authenticated. */
322 	ResetMocks();
323 	calls[0].rsp = osap_response;
324 	calls[0].rsp_size = sizeof(osap_response);
325 	calls[1].rsp = define_space_response;
326 	calls[1].rsp_size = sizeof(define_space_response);
327 	define_space_response[31] = 0;
328 	TEST_EQ(TlclDefineSpaceEx(owner_secret, sizeof(owner_secret),
329 				  0x20000005, 0x2000, 0x10, NULL, 0),
330 		TPM_E_AUTHFAIL, "DefineSpace - response auth");
331 	TEST_EQ(calls[0].req_cmd, TPM_ORD_OSAP, "  osap cmd");
332 	TEST_EQ(calls[1].req_cmd, TPM_ORD_NV_DefineSpace, "  definespace cmd");
333 	define_space_response[31] = 0x40;
334 
335 	/* Verify that a short OSAP response gets caught. */
336 	ResetMocks();
337 	calls[0].rsp = osap_response;
338 	calls[0].rsp_size = sizeof(osap_response);
339 	ToTpmUint32(osap_response + sizeof(uint16_t),
340 		    kTpmRequestHeaderLength + sizeof(uint32_t) +
341 		    2 * sizeof(TPM_NONCE) - 1);
342 	TEST_EQ(TlclDefineSpaceEx(owner_secret, sizeof(owner_secret),
343 				  0x20000005, 0x2000, 0x10, NULL, 0),
344 		TPM_E_INVALID_RESPONSE, "DefineSpace - short OSAP response");
345 	TEST_EQ(calls[0].req_cmd, TPM_ORD_OSAP, "  osap cmd");
346 	ToTpmUint32(osap_response + sizeof(uint16_t), sizeof(osap_response));
347 
348 	/* Verify that a short DefineSpace response gets caught. */
349 	ResetMocks();
350 	calls[0].rsp = osap_response;
351 	calls[0].rsp_size = sizeof(osap_response);
352 	calls[1].rsp = define_space_response;
353 	calls[1].rsp_size = sizeof(define_space_response);
354 	ToTpmUint32(define_space_response + sizeof(uint16_t),
355 		    kTpmResponseHeaderLength + kTpmResponseAuthBlockLength - 1);
356 	TEST_EQ(TlclDefineSpaceEx(owner_secret, sizeof(owner_secret),
357 				  0x20000005, 0x2000, 0x10, NULL, 0),
358 		TPM_E_INVALID_RESPONSE,
359 		"DefineSpace - short DefineSpace response");
360 	TEST_EQ(calls[0].req_cmd, TPM_ORD_OSAP, "  osap cmd");
361 	TEST_EQ(calls[1].req_cmd, TPM_ORD_NV_DefineSpace, "  definespace cmd");
362 	ToTpmUint32(define_space_response + sizeof(uint16_t),
363 		    sizeof(define_space_response));
364 }
365 
366 /**
367  * Test TlclInitNvAuthPolicy.
368  */
InitNvAuthPolicyTest(void)369 static void InitNvAuthPolicyTest(void) {
370 	const uint8_t empty_selection_digest[] = {
371 		0x79, 0xdd, 0xda, 0xfd, 0xc1, 0x97, 0xdc, 0xcc,
372 		0xe9, 0x98, 0x9a, 0xee, 0xf5, 0x52, 0x89, 0xee,
373 		0x24, 0x96, 0x4c, 0xac,
374 	};
375 	const uint8_t pcr0_selection_digest[] = {
376 		0xb3, 0x2b, 0x96, 0x30, 0xd3, 0x21, 0x1e, 0x99,
377 		0x78, 0x9e, 0xd3, 0x1f, 0x11, 0x8e, 0x96, 0xbc,
378 		0xf7, 0x7e, 0x7b, 0x06,
379 	};
380 	const uint8_t empty_selection_encoding[] = { 0x0, 0x0, 0x0 };
381 	const uint8_t pcr0_selection_encoding[] = { 0x1, 0x0, 0x0 };
382 	const uint8_t pcr_values[][TPM_PCR_DIGEST] = {
383 		{ 0x06, 0x4a, 0xec, 0x9b, 0xbd, 0x94, 0xde, 0xa1,
384 		  0x23, 0x1a, 0xe7, 0x57, 0x67, 0x64, 0x7f, 0x09,
385 		  0x8c, 0x39, 0x8e, 0x79, },
386 	};
387 	TPM_NV_AUTH_POLICY policy;
388 
389 	/* Test empty selection. */
390 	uint32_t policy_size = sizeof(policy);
391 	TlclInitNvAuthPolicy(0x0, NULL, &policy, &policy_size);
392 	TEST_EQ(policy_size, sizeof(policy), "policy size");
393 
394 	uint16_t size_of_select;
395 	FromTpmUint16(
396 		(uint8_t*)&policy.pcr_info_read.pcrSelection.sizeOfSelect,
397 		&size_of_select);
398 	TEST_EQ(size_of_select, 3, "empty PCR selection read size of select");
399 	TEST_EQ(memcmp(policy.pcr_info_read.pcrSelection.pcrSelect,
400 		       empty_selection_encoding,
401 		       sizeof(empty_selection_encoding)), 0,
402 		"empty PCR selection read selection encoding");
403 	TEST_EQ(policy.pcr_info_read.localityAtRelease,
404 		TPM_ALL_LOCALITIES & ~TPM_LOC_THREE,
405 		"empty PCR selection read locality");
406 	TEST_EQ(memcmp(empty_selection_digest,
407 		       policy.pcr_info_read.digestAtRelease.digest,
408 		       TPM_PCR_DIGEST),
409 		0, "empty PCR selection read digest");
410 
411 	FromTpmUint16(
412 		(uint8_t*)&policy.pcr_info_write.pcrSelection.sizeOfSelect,
413 		&size_of_select);
414 	TEST_EQ(size_of_select, 3, "empty PCR selection write size of select");
415 	TEST_EQ(memcmp(policy.pcr_info_write.pcrSelection.pcrSelect,
416 		       empty_selection_encoding,
417 		       sizeof(empty_selection_encoding)), 0,
418 		"empty PCR selection write selection encoding");
419 	TEST_EQ(policy.pcr_info_write.localityAtRelease,
420 		TPM_ALL_LOCALITIES & ~TPM_LOC_THREE,
421 		"empty PCR selection write locality");
422 	TEST_EQ(memcmp(empty_selection_digest,
423 		       policy.pcr_info_write.digestAtRelease.digest,
424 		       TPM_PCR_DIGEST),
425 		0, "empty PCR selection write digest");
426 
427 	/* Test PCR0 selection. */
428 	TlclInitNvAuthPolicy(0x1, pcr_values, &policy, &policy_size);
429 	TEST_EQ(policy_size, sizeof(policy), "policy size");
430 
431 	TEST_EQ(memcmp(policy.pcr_info_read.pcrSelection.pcrSelect,
432 		       pcr0_selection_encoding,
433 		       sizeof(pcr0_selection_encoding)), 0,
434 		"PCR0 selection read selection encoding");
435 	TEST_EQ(memcmp(pcr0_selection_digest,
436 		       policy.pcr_info_read.digestAtRelease.digest,
437 		       TPM_PCR_DIGEST),
438 		0, "PCR0 selection read digest");
439 
440 	TEST_EQ(memcmp(policy.pcr_info_write.pcrSelection.pcrSelect,
441 		       pcr0_selection_encoding,
442 		       sizeof(pcr0_selection_encoding)), 0,
443 		"PCR0 selection write selection encoding");
444 	TEST_EQ(memcmp(pcr0_selection_digest,
445 		       policy.pcr_info_write.digestAtRelease.digest,
446 		       TPM_PCR_DIGEST),
447 		0, "PCR0 selection write digest");
448 }
449 
450 /**
451  * Test PCR funcs
452  *
453  * TODO: check params/data read/written.
454  */
PcrTest(void)455 static void PcrTest(void)
456 {
457 	uint8_t buf[kPcrDigestLength], buf2[kPcrDigestLength];
458 
459 	ResetMocks();
460 	TEST_EQ(TlclPCRRead(1, buf, kPcrDigestLength), 0, "PCRRead");
461 	TEST_EQ(calls[0].req_cmd, TPM_ORD_PcrRead, "  cmd");
462 
463 	ResetMocks();
464 	TEST_EQ(TlclPCRRead(1, buf, kPcrDigestLength - 1), TPM_E_IOERROR,
465 		"PCRRead too small");
466 
467 	ResetMocks();
468 	TEST_EQ(TlclExtend(1, buf, buf2), 0, "Extend");
469 	TEST_EQ(calls[0].req_cmd, TPM_ORD_Extend, "  cmd");
470 }
471 
472 /**
473  * Test TlclGetSpaceInfo.
474  */
GetSpaceInfoTest(void)475 static void GetSpaceInfoTest(void)
476 {
477 	uint8_t response[] = {
478 		0x00, 0xc4, 0x00, 0x00, 0x00, 0x55, 0x00, 0x00,
479 		0x00, 0x00, 0x00, 0x00, 0x00, 0x47, 0x00, 0x18,
480 		0x20, 0x00, 0x00, 0x04, 0x00, 0x03, 0x01, 0x00,
481 		0x00, 0x01, 0xb3, 0x2b, 0x96, 0x30, 0xd3, 0x21,
482 		0x1e, 0x99, 0x78, 0x9e, 0xd3, 0x1f, 0x11, 0x8e,
483 		0x96, 0xbc, 0xf7, 0x7e, 0x7b, 0x06, 0x00, 0x03,
484 		0x20, 0x00, 0x00, 0x10, 0x3b, 0xb2, 0x69, 0x03,
485 		0x3d, 0x12, 0xe1, 0x99, 0x87, 0xe9, 0x3d, 0xf1,
486 		0x11, 0xe8, 0x69, 0xcb, 0x7f, 0xe7, 0xb7, 0x60,
487 		0x00, 0x17, 0x00, 0x00, 0x20, 0x00, 0x00, 0x01,
488 		0x01, 0x00, 0x00, 0x00, 0x45,
489 	};
490 
491 	uint32_t attributes = 0;
492 	uint32_t size = 0;
493 	TPM_NV_AUTH_POLICY policy;
494 	uint32_t policy_size = sizeof(policy);
495 
496 	/* Test successful parsing. */
497 	ResetMocks();
498 	calls[0].rsp = response;
499 	calls[0].rsp_size = sizeof(response);
500 	TEST_EQ(TlclGetSpaceInfo(0x20000004, &attributes, &size, &policy,
501 				 &policy_size),
502 		TPM_SUCCESS, "GetSpaceInfo");
503 	TEST_EQ(calls[0].req_cmd, TPM_ORD_GetCapability, "  cmd");
504 	TEST_EQ(policy_size, sizeof(policy), "  policy_size");
505 	TEST_EQ(attributes, TPM_NV_PER_WRITEDEFINE, "  attributes");
506 	TEST_EQ(size, 0x45, "  size");
507 	TEST_EQ(memcmp(&policy, response + 20, sizeof(policy)), 0, "  policy");
508 
509 	/* Test that GetPermissions returns the attributes as well. */
510 	ResetMocks();
511 	calls[0].rsp = response;
512 	calls[0].rsp_size = sizeof(response);
513 	TEST_EQ(TlclGetPermissions(0x20000004, &attributes),
514 		TPM_SUCCESS, "GetPermissions");
515 	TEST_EQ(calls[0].req_cmd, TPM_ORD_GetCapability, "  cmd");
516 	TEST_EQ(attributes, TPM_NV_PER_WRITEDEFINE, "  attributes");
517 
518 	/* Test whether a short response gets detected. */
519 	ResetMocks();
520 	calls[0].rsp = response;
521 	calls[0].rsp_size = sizeof(response);
522 	ToTpmUint32(response + 10, 0x46);
523 	TEST_EQ(TlclGetSpaceInfo(0x20000004, &attributes, &size, &policy,
524 				 &policy_size),
525 		TPM_E_INVALID_RESPONSE, "GetSpaceInfo short length");
526 	ToTpmUint32(response + 10, 0x47);
527 
528 	/* Test whether an overlong PCR selection length causes failure. */
529 	ResetMocks();
530 	calls[0].rsp = response;
531 	calls[0].rsp_size = sizeof(response);
532 	ToTpmUint16(response + 20, 4);
533 	TEST_EQ(TlclGetSpaceInfo(0x20000004, &attributes, &size, &policy,
534 				 &policy_size),
535 		TPM_E_INVALID_RESPONSE, "GetSpaceInfo overlong pcr selection");
536 	ToTpmUint16(response + 20, 3);
537 
538 	/* Test that a short policy buffer triggers an error. */
539 	ResetMocks();
540 	calls[0].rsp = response;
541 	calls[0].rsp_size = sizeof(response);
542 	policy_size = sizeof(policy) - 1;
543 	TEST_EQ(TlclGetSpaceInfo(0x20000004, &attributes, &size, &policy,
544 				 &policy_size),
545 		TPM_E_BUFFER_SIZE, "GetSpaceInfo short policy buffer");
546 	TEST_EQ(sizeof(policy), policy_size, "  policy_size");
547 }
548 
549 /**
550  * Test flags / capabilities
551  *
552  * TODO: check params/data read/written.
553  */
FlagsTest(void)554 static void FlagsTest(void)
555 {
556 	TPM_PERMANENT_FLAGS pflags;
557 	TPM_STCLEAR_FLAGS vflags;
558 	uint8_t disable = 0, deactivated = 0, nvlocked = 0;
559 	uint8_t buf[32];
560 
561 	ResetMocks();
562 	TEST_EQ(TlclGetPermanentFlags(&pflags), 0, "GetPermanentFlags");
563 	TEST_EQ(calls[0].req_cmd, TPM_ORD_GetCapability, "  cmd");
564 
565 	ResetMocks();
566 	TEST_EQ(TlclGetSTClearFlags(&vflags), 0, "GetSTClearFlags");
567 	TEST_EQ(calls[0].req_cmd, TPM_ORD_GetCapability, "  cmd");
568 
569 	ResetMocks();
570 	TEST_EQ(TlclGetFlags(NULL, NULL, NULL), 0, "GetFlags NULL");
571 	TEST_EQ(calls[0].req_cmd, TPM_ORD_GetCapability, "  cmd");
572 	ResetMocks();
573 	TEST_EQ(TlclGetFlags(&disable, &deactivated, &nvlocked), 0, "GetFlags");
574 
575 	ResetMocks();
576 	TEST_EQ(TlclGetOwnership(buf), 0, "GetOwnership");
577 	TEST_EQ(calls[0].req_cmd, TPM_ORD_GetCapability, "  cmd");
578 }
579 
580 /**
581  * Test random
582  *
583  * TODO: check params/data read/written.
584  * TODO: check overflow tests.
585  */
RandomTest(void)586 static void RandomTest(void)
587 {
588 	uint8_t buf[32];
589 	uint32_t size;
590 
591 	ResetMocks();
592 	size = sizeof(buf);
593 	TEST_EQ(TlclGetRandom(buf, sizeof(buf), &size), 0, "GetRandom");
594 	TEST_EQ(calls[0].req_cmd, TPM_ORD_GetRandom, "  cmd");
595 	TEST_EQ(size, 0, "  size 0");
596 }
597 
598 /**
599  * Test GetVersion
600  */
GetVersionTest(void)601 static void GetVersionTest(void)
602 {
603 	uint8_t response[] = {
604 		0x00, 0xc4, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00,
605 		0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x30,
606 		0x01, 0x02, 0x04, 0x20, 0x00, 0x02, 0x03, 0x49,
607 		0x46, 0x58, 0x00, 0x00, 0x0d, 0x04, 0x20, 0x03,
608 		0x6f, 0x00, 0x74, 0x70, 0x6d, 0x33, 0x38, 0xff,
609 		0xff, 0xff
610 	};
611 
612 	uint32_t vendor;
613 	uint64_t firmware_version;
614 	uint8_t vendor_specific[32];
615 	size_t vendor_specific_size;
616 
617 	ResetMocks();
618 	calls[0].rsp = response;
619 	calls[0].rsp_size = sizeof(response);
620 	TEST_EQ(TlclGetVersion(&vendor, &firmware_version, NULL, NULL), 0,
621 		"GetVersion");
622 	TEST_EQ(calls[0].req_cmd, TPM_ORD_GetCapability, "  cmd");
623 	TEST_EQ(vendor, 0x49465800, "  vendor");
624 	TEST_EQ(firmware_version, 0x420, "  firmware_version");
625 
626 	ResetMocks();
627 	calls[0].rsp = response;
628 	calls[0].rsp_size = sizeof(response);
629 	vendor_specific_size = 100;
630 	TEST_EQ(TlclGetVersion(&vendor, &firmware_version,
631 		NULL, &vendor_specific_size), 0,
632 		"GetVersion - vendor specific size");
633 	TEST_EQ(vendor_specific_size, 0xd, "  vendor specific size");
634 
635 	ResetMocks();
636 	calls[0].rsp = response;
637 	calls[0].rsp_size = sizeof(response);
638 	vendor_specific_size = sizeof(vendor_specific);
639 	TEST_EQ(TlclGetVersion(&vendor, &firmware_version,
640 		vendor_specific, &vendor_specific_size), 0,
641 		"GetVersion - vendor specific data");
642 	TEST_EQ(vendor_specific_size, 0xd, "  vendor specific size");
643 	TEST_EQ(memcmp(vendor_specific, response + 29, 0xd), 0,
644 		"  vendor specific data check");
645 
646 	ResetMocks();
647 	calls[0].rsp = response;
648 	calls[0].rsp_size = sizeof(response);
649 	vendor_specific_size = 4;
650 	TEST_EQ(TlclGetVersion(&vendor, &firmware_version,
651 		vendor_specific, &vendor_specific_size), 0,
652 		"GetVersion - vendor specific data, short buf");
653 	TEST_EQ(vendor_specific_size, 4,
654 		"  min(vendor specific size, buf size)");
655 	TEST_EQ(memcmp(vendor_specific, response + 29, 4), 0,
656 		"  vendor specific data check");
657 
658 	ResetMocks();
659 	SetResponse(0, TPM_E_IOERROR, 0);
660 	TEST_EQ(TlclGetVersion(&vendor, &firmware_version, NULL, NULL),
661 		TPM_E_IOERROR, "GetVersion - error");
662 	TEST_EQ(calls[0].req_cmd, TPM_ORD_GetCapability, "  cmd");
663 
664 	/* Adjust response to indicate a 1 byte too short payload size. */
665 	ToTpmUint32(response + kTpmResponseHeaderLength, 14);
666 	ResetMocks();
667 	calls[0].rsp = response;
668 	calls[0].rsp_size = sizeof(response);
669 	TEST_EQ(TlclGetVersion(&vendor, &firmware_version, NULL, NULL),
670 		TPM_E_IOERROR, "GetVersion -- short");
671 	TEST_EQ(calls[0].req_cmd, TPM_ORD_GetCapability, "  cmd");
672 
673 	/* Adjust response to indicate a payload size too long for the
674 	 * response buffer. */
675 	ToTpmUint32(response + kTpmResponseHeaderLength,
676 			TPM_LARGE_ENOUGH_COMMAND_SIZE - sizeof(uint32_t) -
677 			kTpmResponseHeaderLength + 1);
678 	ResetMocks();
679 	calls[0].rsp = response;
680 	calls[0].rsp_size = sizeof(response);
681 	TEST_EQ(TlclGetVersion(&vendor, &firmware_version, NULL, NULL),
682 		TPM_E_IOERROR, "GetVersion -- long");
683 	TEST_EQ(calls[0].req_cmd, TPM_ORD_GetCapability, "  cmd");
684 
685 	/* Restore the original payload length and adjust response to contain
686 	 * less vendor specific data than indicated in its size. */
687 	ToTpmUint32(response + kTpmResponseHeaderLength, 0x1c);
688 	ToTpmUint16(response + 27, 0xd + 1);
689 	ResetMocks();
690 	calls[0].rsp = response;
691 	calls[0].rsp_size = sizeof(response);
692 	TEST_EQ(TlclGetVersion(&vendor, &firmware_version,
693 		NULL, &vendor_specific_size), TPM_E_IOERROR,
694 		"GetVersion -- short with vendor specific");
695 	TEST_EQ(calls[0].req_cmd, TPM_ORD_GetCapability, "  cmd");
696 }
697 
698 /**
699  * Test IFX FieldUpgradeInfoRequest2
700  */
IFXFieldUpgradeInfoTest(void)701 static void IFXFieldUpgradeInfoTest(void)
702 {
703 	uint8_t version_response[] = {
704 		0x00, 0xc4, 0x00, 0x00, 0x00, 0x2a, 0x00, 0x00,
705 		0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x30,
706 		0x01, 0x02, 0x04, 0x20, 0x00, 0x02, 0x03, 0x49,
707 		0x46, 0x58, 0x00, 0x00, 0x0d, 0x04, 0x20, 0x03,
708 		0x6f, 0x00, 0x74, 0x70, 0x6d, 0x33, 0x38, 0xff,
709 		0xff, 0xff
710 	};
711 	uint8_t upgrade_info_response[] = {
712 		0x00, 0xc4, 0x00, 0x00, 0x00, 0x76, 0x00, 0x00,
713 		0x00, 0x00, 0x00, 0x6a, 0x03, 0x02, 0x04, 0x9c,
714 		0x04, 0x01, 0x00, 0x00, 0x01, 0x02, 0x00, 0x08,
715 		0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
716 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
717 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
718 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
719 		0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff,
720 		0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x04, 0x01,
721 		0x01, 0x00, 0x00, 0x00, 0x00, 0xbe, 0x00, 0x00,
722 		0x00, 0x00, 0x04, 0x01, 0x02, 0x00, 0x00, 0x00,
723 		0x00, 0x00, 0xff, 0xff, 0xee, 0xee, 0x5a, 0x3c,
724 		0x04, 0x01, 0x02, 0x00, 0x00, 0x00, 0x08, 0x32,
725 		0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00,
726 		0x00, 0x00, 0x00, 0x00, 0x00, 0x3f
727 	};
728 
729 	ResetMocks();
730 	calls[0].rsp = version_response;
731 	calls[0].rsp_size = sizeof(version_response);
732 	calls[1].rsp = upgrade_info_response;
733 	calls[1].rsp_size = sizeof(upgrade_info_response);
734 	TPM_IFX_FIELDUPGRADEINFO info;
735 	TEST_EQ(TlclIFXFieldUpgradeInfo(&info), 0, "IFXFieldUpgradeInfo");
736 	TEST_EQ(calls[1].req_cmd, TPM_ORD_FieldUpgrade, "  cmd");
737 	TEST_EQ(info.wMaxDataSize, 1180, "  wMaxDatasize");
738 	TEST_EQ(info.sBootloaderFirmwarePackage.FwPackageIdentifier, 0x50100,
739 		"  bootloader FWPackageIdeintifier");
740 	TEST_EQ(info.sBootloaderFirmwarePackage.Version, 0xffff,
741 		"  bootloader Version");
742 	TEST_EQ(info.sBootloaderFirmwarePackage.StaleVersion, 0x0,
743 		"  bootloader StaleVersion");
744 	TEST_EQ(info.sFirmwarePackages[0].FwPackageIdentifier, 0x4010100,
745 		"  fw[0] FWPackageIdeintifier");
746 	TEST_EQ(info.sFirmwarePackages[0].Version, 0xbe,
747 		"  fw[0] Version");
748 	TEST_EQ(info.sFirmwarePackages[0].StaleVersion, 0x0,
749 		"  fw[0] StaleVersion");
750 	TEST_EQ(info.sFirmwarePackages[1].FwPackageIdentifier, 0x4010200,
751 		"  fw[1] FWPackageIdeintifier");
752 	TEST_EQ(info.sFirmwarePackages[1].Version, 0x0,
753 		"  fw[1] Version");
754 	TEST_EQ(info.sFirmwarePackages[1].StaleVersion, 0xffffeeee,
755 		"  fw[1] StaleVersion");
756 	TEST_EQ(info.wSecurityModuleStatus, 0x5a3c, "  wSecurityModuleStatus");
757 	TEST_EQ(info.sProcessFirmwarePackage.FwPackageIdentifier, 0x4010200,
758 		"  process FWPackageIdeintifier");
759 	TEST_EQ(info.sProcessFirmwarePackage.Version, 0x832,
760 		"  process Version");
761 	TEST_EQ(info.sProcessFirmwarePackage.StaleVersion, 0x0,
762 		"  process StaleVersion");
763 	TEST_EQ(info.wFieldUpgradeCounter, 0x3f, "  wFieldUpgradeCounter");
764 
765 	ResetMocks();
766 	calls[0].rsp = version_response;
767 	calls[0].rsp_size = sizeof(version_response);
768 	SetResponse(1, TPM_E_IOERROR, sizeof(upgrade_info_response) - 1);
769 	TEST_EQ(TlclIFXFieldUpgradeInfo(&info), TPM_E_IOERROR,
770 		"IFXFieldUpgradeInfo - error");
771 	TEST_EQ(calls[1].req_cmd, TPM_ORD_FieldUpgrade, "  cmd");
772 
773 	/* Adjust response to indicate a 1 byte too short payload size. */
774 	ToTpmUint16(upgrade_info_response + kTpmRequestHeaderLength,
775 		    sizeof(upgrade_info_response) - kTpmRequestHeaderLength -
776 		    sizeof(uint16_t) - 1);
777 	ResetMocks();
778 	calls[0].rsp = version_response;
779 	calls[0].rsp_size = sizeof(version_response);
780 	calls[1].rsp = upgrade_info_response;
781 	calls[1].rsp_size = sizeof(upgrade_info_response);
782 	TEST_EQ(TlclIFXFieldUpgradeInfo(&info), TPM_E_INVALID_RESPONSE,
783 		"IFXFieldUpgradeInfo - short");
784 	TEST_EQ(calls[1].req_cmd, TPM_ORD_FieldUpgrade, "  cmd");
785 
786 	/* Adjust version response to claim a non-IFX vendor. */
787 	ToTpmUint32(version_response + kTpmResponseHeaderLength +
788 		    sizeof(uint32_t), 0);
789 	ResetMocks();
790 	calls[0].rsp = version_response;
791 	calls[0].rsp_size = sizeof(version_response);
792 	TEST_EQ(TlclIFXFieldUpgradeInfo(&info), TPM_E_INVALID_RESPONSE,
793 		"IFXFieldUpgradeInfo - bad vendor");
794 	TEST_EQ(calls[1].req_cmd, TPM_ORD_FieldUpgrade, "  cmd");
795 }
796 
797 /**
798  * Test ReadPubek
799  */
ReadPubekTest(void)800 static void ReadPubekTest(void) {
801 	uint8_t response[] = {
802 		0x00, 0xc4, 0x00, 0x00, 0x01, 0x3a, 0x00, 0x00,
803 		0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03,
804 		0x00, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00,
805 		0x08, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
806 		0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x9c, 0xa8,
807 		0x8f, 0x15, 0x6d, 0xbf, 0x37, 0x6f, 0x8d, 0xb7,
808 		0xb2, 0xe2, 0x79, 0x81, 0xf7, 0xc2, 0x3c, 0x84,
809 		0x64, 0x35, 0x38, 0x59, 0x6a, 0x87, 0x23, 0xe0,
810 		0x2a, 0xca, 0x40, 0x37, 0x78, 0x75, 0x18, 0xfc,
811 		0x2d, 0xbe, 0x2b, 0xd9, 0x77, 0x49, 0x4b, 0x8c,
812 		0xea, 0xe3, 0xcd, 0xa5, 0x27, 0x2b, 0x48, 0x2f,
813 		0xbe, 0x3b, 0x14, 0xca, 0xe5, 0x22, 0x31, 0xb8,
814 		0xa1, 0x14, 0xc6, 0x06, 0x8d, 0x14, 0xe8, 0x4a,
815 		0x36, 0x4d, 0xd3, 0x5f, 0xde, 0x23, 0xd1, 0x7b,
816 		0xec, 0x3b, 0xdd, 0x84, 0xa4, 0xd4, 0xe9, 0x9a,
817 		0x89, 0x94, 0x5c, 0xa0, 0x01, 0xfb, 0x95, 0x61,
818 		0x01, 0xaf, 0x6b, 0x17, 0x39, 0x44, 0x7e, 0x25,
819 		0x9b, 0x73, 0xfa, 0xde, 0x20, 0xb6, 0x92, 0x95,
820 		0x7b, 0x76, 0x9f, 0x44, 0xb0, 0xfb, 0x86, 0x9f,
821 		0xc5, 0xe2, 0x38, 0x9d, 0xcf, 0xce, 0x32, 0x53,
822 		0x3b, 0x3d, 0x10, 0xb7, 0x89, 0x53, 0x54, 0xc0,
823 		0x28, 0x52, 0xfc, 0x7a, 0xa9, 0x5b, 0x03, 0xd7,
824 		0x55, 0x2b, 0x07, 0xa7, 0x22, 0x5b, 0x1e, 0xe5,
825 		0x4f, 0x43, 0x70, 0x7d, 0x81, 0xbb, 0x3d, 0x56,
826 		0xfe, 0x5c, 0x47, 0xcc, 0xc0, 0x6e, 0xc8, 0xc7,
827 		0xd9, 0x96, 0x53, 0x2d, 0xd2, 0x28, 0xc7, 0xdf,
828 		0x5a, 0x07, 0x50, 0x3b, 0x17, 0x25, 0xe7, 0x51,
829 		0xed, 0xf7, 0x94, 0x02, 0x2a, 0x4c, 0x31, 0x57,
830 		0x34, 0x51, 0x05, 0x26, 0x43, 0xd4, 0x40, 0x47,
831 		0x3e, 0x02, 0xfe, 0xa5, 0x08, 0xc7, 0x94, 0xaa,
832 		0xd3, 0x14, 0x02, 0x1b, 0x41, 0x4c, 0xcd, 0xd6,
833 		0x8c, 0xad, 0x8e, 0x72, 0x1a, 0x36, 0xc7, 0x23,
834 		0xd1, 0x38, 0x83, 0x9f, 0xac, 0x66, 0xc5, 0x25,
835 		0x82, 0x9d, 0x18, 0x67, 0x78, 0xca, 0x15, 0x63,
836 		0x15, 0xd8, 0x83, 0xbd, 0xcc, 0xe7, 0xf7, 0xe9,
837 		0xba, 0xda, 0x23, 0xdf, 0x53, 0x30, 0x51, 0x1a,
838 		0xf1, 0x0c, 0x02, 0xe7, 0x65, 0x0d, 0x95, 0x52,
839 		0x76, 0xd1, 0x1b, 0xb0, 0x77, 0xba, 0x31, 0x0b,
840 		0x17, 0xb5, 0x63, 0x0d, 0x50, 0x7c, 0xbc, 0x63,
841 		0xbf, 0xc3,
842 	};
843 	uint32_t exponent = 0;
844 	uint8_t modulus[TPM_RSA_2048_LEN];
845 	uint32_t modulus_size = TPM_RSA_2048_LEN;
846 
847 	ResetMocks();
848 	calls[0].rsp = response;
849 	calls[0].rsp_size = sizeof(response);
850 	TEST_EQ(TlclReadPubek(&exponent, modulus, &modulus_size), TPM_SUCCESS,
851 		"ReadPubek");
852 	TEST_EQ(calls[0].req_cmd, TPM_ORD_ReadPubek, "  cmd");
853 	TEST_EQ(exponent, 0x10001, "  exponent");
854 	TEST_EQ(memcmp(modulus, response + 38, sizeof(modulus)), TPM_SUCCESS,
855 		"  modulus");
856 	TEST_EQ(modulus_size, 0x100, "  modulus_size");
857 
858 	/* Test that the command returns the full size of the modulus if the
859 	 * input buffer is too small. */
860 	ResetMocks();
861 	calls[0].rsp = response;
862 	calls[0].rsp_size = sizeof(response);
863 	modulus_size = 0;
864 	TEST_EQ(TlclReadPubek(&exponent, NULL, &modulus_size),
865 		TPM_E_BUFFER_SIZE, "ReadPubek - returns modulus size");
866 	TEST_EQ(calls[0].req_cmd, TPM_ORD_ReadPubek, "  cmd");
867 	TEST_EQ(modulus_size, 0x100, "  modulus_size");
868 
869 	/* Test that a too large parm_size value gets handled correctly. */
870 	ResetMocks();
871 	calls[0].rsp = response;
872 	calls[0].rsp_size = sizeof(response);
873 	ToTpmUint32(response + 18, 1 << 24);
874 	TEST_EQ(TlclReadPubek(&exponent, NULL, &modulus_size),
875 		TPM_E_INVALID_RESPONSE, "ReadPubek - large parm_size");
876 	TEST_EQ(calls[0].req_cmd, TPM_ORD_ReadPubek, "  cmd");
877 	ToTpmUint32(response + 18, 12);
878 
879 	/* Test that a too small parm_size value gets handled correctly. */
880 	ResetMocks();
881 	calls[0].rsp = response;
882 	calls[0].rsp_size = sizeof(response);
883 	ToTpmUint32(response + 18, 11);
884 	TEST_EQ(TlclReadPubek(&exponent, NULL, &modulus_size),
885 		TPM_E_INVALID_RESPONSE, "ReadPubek - small parm_size");
886 	TEST_EQ(calls[0].req_cmd, TPM_ORD_ReadPubek, "  cmd");
887 	ToTpmUint32(response + 18, 12);
888 
889 	/* Test that an overlong modulus size gets handled correctly. */
890 	ResetMocks();
891 	calls[0].rsp = response;
892 	calls[0].rsp_size = sizeof(response);
893 	ToTpmUint32(response + 34, 1 << 24);
894 	modulus_size = sizeof(modulus);
895 	TEST_EQ(TlclReadPubek(&exponent, NULL, &modulus_size),
896 		TPM_E_INVALID_RESPONSE, "ReadPubek - large modulus size");
897 	TEST_EQ(calls[0].req_cmd, TPM_ORD_ReadPubek, "  cmd");
898 	ToTpmUint32(response + 34, TPM_RSA_2048_LEN);
899 
900 	/* Test that a large exponent size gets handled correctly. */
901 	ResetMocks();
902 	calls[0].rsp = response;
903 	calls[0].rsp_size = sizeof(response);
904 	ToTpmUint32(response + 20, 1 << 24);
905 	TEST_EQ(TlclReadPubek(&exponent, NULL, &modulus_size),
906 		TPM_E_INVALID_RESPONSE, "ReadPubek - large exponent size");
907 	TEST_EQ(calls[0].req_cmd, TPM_ORD_ReadPubek, "  cmd");
908 	ToTpmUint32(response + 20, 0);
909 
910 	/* Test that an incorrect auth tag causes an error. */
911 	ResetMocks();
912 	calls[0].rsp = response;
913 	calls[0].rsp_size = sizeof(response);
914 	response[294] = 0;
915 	TEST_EQ(TlclReadPubek(&exponent, NULL, &modulus_size),
916 		TPM_E_INVALID_RESPONSE, "ReadPubek - incorrect auth");
917 	TEST_EQ(calls[0].req_cmd, TPM_ORD_ReadPubek, "  cmd");
918 	response[294] = 0x60;
919 }
920 
921 /**
922  * Test TakeOwnership
923  */
TakeOwnershipTest(void)924 static void TakeOwnershipTest(void) {
925 	uint8_t oiap_response[] = {
926 		0x00, 0xc4, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00,
927 		0x00, 0x00, 0x00, 0x4c, 0x04, 0x1a, 0x18, 0xa9,
928 		0xf7, 0x9b, 0x2e, 0xe1, 0xf2, 0x16, 0x99, 0xa0,
929 		0x27, 0x5f, 0x0c, 0x8f, 0x24, 0x55, 0x1d, 0xaf,
930 		0x96, 0x49,
931 	};
932 	uint8_t take_ownership_response[] = {
933 		0x00, 0xc5, 0x00, 0x00, 0x01, 0x62, 0x00, 0x00,
934 		0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x11,
935 		0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
936 		0x01, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
937 		0x0c, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
938 		0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
939 		0x00, 0x00, 0x00, 0x01, 0x00, 0x92, 0x61, 0xa5,
940 		0x30, 0x5f, 0x39, 0xb7, 0xc3, 0x51, 0x53, 0x84,
941 		0xaf, 0x51, 0x64, 0x65, 0xd7, 0x58, 0xda, 0x15,
942 		0xb0, 0xb8, 0xe8, 0xea, 0xf5, 0xb2, 0x21, 0x34,
943 		0x09, 0x71, 0xa0, 0xd5, 0x2b, 0x21, 0xd6, 0x16,
944 		0xbd, 0x03, 0xc3, 0x83, 0x7e, 0x48, 0x36, 0xd7,
945 		0xfa, 0xc7, 0x5e, 0x5e, 0xb4, 0xc3, 0x9f, 0x00,
946 		0xcc, 0x7a, 0x3e, 0x0a, 0x00, 0x34, 0xbd, 0xbc,
947 		0x7b, 0x28, 0x0e, 0x4a, 0xef, 0xa5, 0x86, 0x50,
948 		0xa5, 0xfe, 0x8f, 0x7d, 0xbc, 0x86, 0xf3, 0x3d,
949 		0x8c, 0x65, 0x4c, 0x3b, 0x29, 0x3b, 0x40, 0x8b,
950 		0xca, 0xf9, 0xa1, 0xc8, 0x62, 0x52, 0xe1, 0x1f,
951 		0x0d, 0x89, 0x71, 0xca, 0xbb, 0x64, 0xef, 0x3f,
952 		0x97, 0x97, 0xed, 0x57, 0xb3, 0xd8, 0x67, 0x4c,
953 		0x46, 0x1c, 0x35, 0x0c, 0xba, 0x12, 0xae, 0x2e,
954 		0x6d, 0xa7, 0x48, 0xd6, 0x9a, 0x8a, 0x60, 0x22,
955 		0xd9, 0xe5, 0x97, 0x50, 0xc1, 0x24, 0xaf, 0xb6,
956 		0x41, 0xfe, 0x6d, 0xfe, 0x28, 0x3f, 0xce, 0x35,
957 		0x9b, 0x77, 0xe9, 0xd5, 0x36, 0xdb, 0x70, 0x5e,
958 		0xd7, 0xb9, 0x89, 0xac, 0xae, 0x55, 0x59, 0x00,
959 		0x7d, 0x47, 0x5d, 0x73, 0x4f, 0x1b, 0x17, 0xfa,
960 		0xae, 0xb0, 0xf7, 0xb7, 0x63, 0x4d, 0xa9, 0x94,
961 		0x0c, 0x1e, 0x02, 0x59, 0x8d, 0x34, 0x1f, 0x01,
962 		0x6d, 0xa7, 0x05, 0xa7, 0xae, 0xbd, 0x9b, 0xfa,
963 		0xed, 0xe6, 0xe3, 0xf2, 0xc0, 0xa9, 0x16, 0xb6,
964 		0xd2, 0x23, 0x37, 0x2e, 0x43, 0x5e, 0x5f, 0xe6,
965 		0x77, 0x0d, 0x49, 0x48, 0x07, 0x57, 0x64, 0xd2,
966 		0xd9, 0x60, 0xff, 0xe3, 0x60, 0xb6, 0xd7, 0xa5,
967 		0xe3, 0xd8, 0xa3, 0x93, 0xb3, 0xe9, 0xeb, 0x1c,
968 		0x53, 0x42, 0x08, 0x9f, 0x0c, 0x13, 0x72, 0x3c,
969 		0x80, 0xf8, 0xa1, 0x8c, 0x4d, 0xe5, 0x1e, 0xe7,
970 		0xef, 0x2b, 0x33, 0x23, 0x1e, 0x5a, 0xf6, 0xc1,
971 		0x46, 0x78, 0x06, 0x7e, 0xe7, 0x00, 0x00, 0x00,
972 		0x00, 0xef, 0x84, 0x26, 0xd3, 0xb6, 0x27, 0x4a,
973 		0x4a, 0x0f, 0x84, 0x65, 0x4b, 0xff, 0x80, 0x7e,
974 		0xb5, 0xf6, 0xbe, 0x8c, 0xed, 0x00, 0xad, 0xd1,
975 		0x73, 0x8a, 0x55, 0x9f, 0x50, 0xb4, 0x34, 0xba,
976 		0x2d, 0x6d, 0x80, 0x3a, 0xdc, 0x82, 0x94, 0x3b,
977 		0x96, 0x58,
978 	};
979 	uint8_t encrypted_secret[] = {
980 		0x46, 0x9a, 0x17, 0x31, 0x04, 0x72, 0x58, 0xcd,
981 		0xac, 0xe7, 0xa4, 0x1f, 0x48, 0xa3, 0x89, 0x10,
982 		0xac, 0x40, 0xe2, 0x66, 0xfa, 0xfd, 0xe9, 0xab,
983 		0x7a, 0x55, 0xd3, 0xc0, 0x61, 0xca, 0x28, 0x0d,
984 		0x29, 0x4a, 0xe4, 0x9a, 0xbe, 0x62, 0x51, 0xe8,
985 		0x3f, 0xbf, 0x84, 0xae, 0x4e, 0x6c, 0x0e, 0x11,
986 		0x2b, 0xba, 0x62, 0x5d, 0xf5, 0x9d, 0xf8, 0xcd,
987 		0x5c, 0x9d, 0x5b, 0xee, 0x5e, 0xdc, 0xaf, 0xc1,
988 		0xbf, 0x22, 0x14, 0x0d, 0x68, 0xdf, 0xe1, 0x94,
989 		0x6b, 0x06, 0xc4, 0x5b, 0xdd, 0xee, 0xd3, 0xef,
990 		0x67, 0xb5, 0xb0, 0xee, 0x58, 0x88, 0x2d, 0x5c,
991 		0x7d, 0xda, 0x83, 0xd5, 0xb5, 0x72, 0x43, 0x33,
992 		0xf7, 0x9e, 0xf0, 0x52, 0x8c, 0xc1, 0xf1, 0xea,
993 		0xcf, 0x9f, 0x0e, 0xfb, 0xb3, 0x03, 0xfe, 0xb3,
994 		0xb4, 0x38, 0xa2, 0xfb, 0x2f, 0x64, 0xb6, 0x42,
995 		0x4c, 0x76, 0x70, 0xfa, 0x67, 0xc0, 0x48, 0x98,
996 		0x52, 0x3e, 0xdb, 0xe6, 0xfe, 0x44, 0x96, 0x14,
997 		0x5a, 0x6a, 0x19, 0x53, 0x46, 0x13, 0xe6, 0xc9,
998 		0x21, 0xee, 0x8e, 0xc2, 0xf2, 0x39, 0x2d, 0xba,
999 		0x6f, 0xeb, 0x80, 0x89, 0xf3, 0xea, 0xfa, 0x5c,
1000 		0x9c, 0x88, 0xe0, 0xb1, 0x53, 0xa0, 0xe5, 0xe0,
1001 		0x90, 0x33, 0x9d, 0x9d, 0x5f, 0xba, 0x6d, 0x68,
1002 		0xb2, 0x9f, 0x4f, 0xa1, 0x28, 0xf9, 0xc4, 0x53,
1003 		0x72, 0x51, 0x48, 0x4b, 0xb3, 0xf9, 0x18, 0x43,
1004 		0x3a, 0x85, 0xdc, 0x70, 0x46, 0x0c, 0x3c, 0xe1,
1005 		0x17, 0x1c, 0x18, 0x6f, 0xfd, 0xff, 0x77, 0x8d,
1006 		0x04, 0xfc, 0xb3, 0xc0, 0x9a, 0x03, 0x74, 0x1d,
1007 		0x06, 0x8f, 0xb6, 0x0a, 0x3e, 0xea, 0x91, 0x87,
1008 		0xa9, 0x68, 0x26, 0x91, 0x81, 0x02, 0xe4, 0x10,
1009 		0x66, 0xb6, 0x5f, 0x43, 0x47, 0x55, 0x25, 0xe0,
1010 		0xbd, 0xd3, 0xab, 0xbd, 0xfd, 0x15, 0x85, 0x39,
1011 		0x22, 0x93, 0xfc, 0x9d, 0x74, 0x0e, 0xcf, 0x5a,
1012 	};
1013 	uint8_t owner_secret[TPM_AUTH_DATA_LEN] = { 0 };
1014 
1015 	ResetMocks();
1016 	calls[0].rsp = oiap_response;
1017 	calls[0].rsp_size = sizeof(oiap_response);
1018 	calls[1].rsp = take_ownership_response;
1019 	calls[1].rsp_size = sizeof(take_ownership_response);
1020 	TEST_EQ(TlclTakeOwnership(encrypted_secret, encrypted_secret,
1021 				  owner_secret), TPM_SUCCESS, "TakeOwnership");
1022 	TEST_EQ(calls[0].req_cmd, TPM_ORD_OIAP, "  oiap cmd");
1023 	TEST_EQ(calls[1].req_cmd, TPM_ORD_TakeOwnership, "  takeownership cmd");
1024 
1025 	/* Verify that the response gets authenticated. */
1026 	ResetMocks();
1027 	calls[0].rsp = oiap_response;
1028 	calls[0].rsp_size = sizeof(oiap_response);
1029 	calls[1].rsp = take_ownership_response;
1030 	calls[1].rsp_size = sizeof(take_ownership_response);
1031 	take_ownership_response[334] = 0;
1032 	TEST_EQ(TlclTakeOwnership(encrypted_secret, encrypted_secret,
1033 				  owner_secret),
1034 		TPM_E_AUTHFAIL, "TakeOwnership - response auth");
1035 	TEST_EQ(calls[0].req_cmd, TPM_ORD_OIAP, "  oiap cmd");
1036 	TEST_EQ(calls[1].req_cmd, TPM_ORD_TakeOwnership, "  takeownership cmd");
1037 	take_ownership_response[334] = 0xad;
1038 
1039 	/* Verify that a short OIAP response gets caught. */
1040 	ResetMocks();
1041 	calls[0].rsp = oiap_response;
1042 	calls[0].rsp_size = sizeof(oiap_response);
1043 	ToTpmUint32(oiap_response + sizeof(uint16_t),
1044 		    kTpmRequestHeaderLength + sizeof(uint32_t) +
1045 		    sizeof(TPM_NONCE) - 1);
1046 	TEST_EQ(TlclTakeOwnership(encrypted_secret, encrypted_secret,
1047 				  owner_secret),
1048 		TPM_E_INVALID_RESPONSE, "TakeOwnership - short OIAP response");
1049 	TEST_EQ(calls[0].req_cmd, TPM_ORD_OIAP, "  oiap cmd");
1050 	ToTpmUint32(oiap_response + sizeof(uint16_t), sizeof(oiap_response));
1051 
1052 	/* Verify that a short TakeOwnership response gets caught. */
1053 	ResetMocks();
1054 	calls[0].rsp = oiap_response;
1055 	calls[0].rsp_size = sizeof(oiap_response);
1056 	calls[1].rsp = take_ownership_response;
1057 	calls[1].rsp_size = sizeof(take_ownership_response);
1058 	ToTpmUint32(take_ownership_response + sizeof(uint16_t),
1059 		    kTpmResponseHeaderLength + kTpmResponseAuthBlockLength - 1);
1060 	TEST_EQ(TlclTakeOwnership(encrypted_secret, encrypted_secret,
1061 				  owner_secret),
1062 		TPM_E_INVALID_RESPONSE,
1063 		"TakeOwnership - short TakeOwnership response");
1064 	TEST_EQ(calls[0].req_cmd, TPM_ORD_OIAP, "  oiap cmd");
1065 	TEST_EQ(calls[1].req_cmd, TPM_ORD_TakeOwnership, "  takeownership cmd");
1066 	ToTpmUint32(take_ownership_response + sizeof(uint16_t),
1067 		    sizeof(take_ownership_response));
1068 }
1069 
1070 /**
1071  * Test ReadDelegationFamilyTable
1072  */
ReadDelegationFamilyTableTest(void)1073 static void ReadDelegationFamilyTableTest(void) {
1074 	uint8_t response[] = {
1075 		0x00, 0xc4, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
1076 		0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x25,
1077 		0x17, 0x00, 0x00, 0x00, 0x4f, 0x00, 0x00, 0x00,
1078 		0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x25, 0x42,
1079 		0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00, 0x01,
1080 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1081 	};
1082 
1083 	TPM_FAMILY_TABLE_ENTRY table[20];
1084 	uint32_t table_size;
1085 
1086 	ResetMocks();
1087 	calls[0].rsp = response;
1088 	calls[0].rsp_size = sizeof(response);
1089 	table_size = 8;
1090 	TEST_EQ(TlclReadDelegationFamilyTable(table, &table_size),
1091 		TPM_SUCCESS, "ReadDelegationFamilyTable");
1092 	TEST_EQ(calls[0].req_cmd, TPM_ORD_Delegate_ReadTable, "  cmd");
1093 	TEST_EQ(table_size, 2, "  table_size");
1094 	TEST_EQ(table[0].tag, 0x25, "  table[0].tag");
1095 	TEST_EQ(table[0].familyLabel, 0x17, "  table[0].familyLabel");
1096 	TEST_EQ(table[0].familyID, 0x4f, "  table[0].familyID");
1097 	TEST_EQ(table[0].verificationCount, 0x1,
1098 		"  table[0].verificationCount");
1099 	TEST_EQ(table[0].flags, 0x2, "  table[0].flags");
1100 	TEST_EQ(table[1].tag, 0x25, "  table[1].tag");
1101 	TEST_EQ(table[1].familyLabel, 0x42, "  table[1].familyLabel");
1102 	TEST_EQ(table[1].familyID, 0x50, "  table[1].familyID");
1103 	TEST_EQ(table[1].verificationCount, 0x1,
1104 		"  table[1].verificationCount");
1105 	TEST_EQ(table[1].flags, 0x0, "  table[1].flags");
1106 
1107 	/* Test that required table size is returned if more space required. */
1108 	ResetMocks();
1109 	calls[0].rsp = response;
1110 	calls[0].rsp_size = sizeof(response);
1111 	table_size = 1;
1112 	TEST_EQ(TlclReadDelegationFamilyTable(table, &table_size),
1113 		TPM_E_BUFFER_SIZE, "ReadDelegationFamilyTable");
1114 	TEST_EQ(calls[0].req_cmd, TPM_ORD_Delegate_ReadTable, "  cmd");
1115 	TEST_EQ(table_size, 2, "  table_size");
1116 
1117 	/* Test that an overlong response gets caught. */
1118 	ResetMocks();
1119 	calls[0].rsp = response;
1120 	calls[0].rsp_size = sizeof(response);
1121 	ToTpmUint32(response + sizeof(uint16_t), TPM_LARGE_ENOUGH_COMMAND_SIZE +
1122 		    1);
1123 	TEST_EQ(TlclReadDelegationFamilyTable(table, &table_size),
1124 		TPM_E_INVALID_RESPONSE,
1125 		"ReadDelegationFamilyTable - too long response");
1126 	TEST_EQ(calls[0].req_cmd, TPM_ORD_Delegate_ReadTable, "  cmd");
1127 	ToTpmUint32(response + sizeof(uint16_t), sizeof(response));
1128 
1129 	/* Test that a short response gets caught. */
1130 	ResetMocks();
1131 	calls[0].rsp = response;
1132 	calls[0].rsp_size = sizeof(response);
1133 	ToTpmUint32(response + sizeof(uint16_t),
1134 		    kTpmRequestHeaderLength + sizeof(uint32_t) - 1);
1135 	TEST_EQ(TlclReadDelegationFamilyTable(table, &table_size),
1136 		TPM_E_INVALID_RESPONSE,
1137 		"ReadDelegationFamilyTable - too short response");
1138 	TEST_EQ(calls[0].req_cmd, TPM_ORD_Delegate_ReadTable, "  cmd");
1139 	ToTpmUint32(response + sizeof(uint16_t), sizeof(response));
1140 
1141 	/* Test that long table size in response gets caught. */
1142 	ResetMocks();
1143 	calls[0].rsp = response;
1144 	calls[0].rsp_size = sizeof(response);
1145 	table_size = 20;
1146 	ToTpmUint32(response + kTpmResponseHeaderLength,
1147 		    TPM_LARGE_ENOUGH_COMMAND_SIZE);
1148 	TEST_EQ(TlclReadDelegationFamilyTable(table, &table_size),
1149 		TPM_E_INVALID_RESPONSE,
1150 		"ReadDelegationFamilyTable - overlong family table");
1151 	TEST_EQ(calls[0].req_cmd, TPM_ORD_Delegate_ReadTable, "  cmd");
1152 	ToTpmUint32(response + kTpmResponseHeaderLength, 0x1e);
1153 }
1154 
main(void)1155 int main(void)
1156 {
1157 	TlclTest();
1158 	SendCommandTest();
1159 	ReadWriteTest();
1160 	DefineSpaceExTest();
1161 	InitNvAuthPolicyTest();
1162 	PcrTest();
1163 	GetSpaceInfoTest();
1164 	FlagsTest();
1165 	RandomTest();
1166 	GetVersionTest();
1167 	IFXFieldUpgradeInfoTest();
1168 	ReadPubekTest();
1169 	TakeOwnershipTest();
1170 	ReadDelegationFamilyTableTest();
1171 
1172 	return gTestSuccess ? 0 : 255;
1173 }
1174