tlcl.c revision c3d488d155961d2849dfdaa4f0461df1aa95c2ca
1/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6/* A lightweight TPM command library.
7 *
8 * The general idea is that TPM commands are array of bytes whose
9 * fields are mostly compile-time constant.  The goal is to build much
10 * of the commands at compile time (or build time) and change some of
11 * the fields at run time as needed.  The code in
12 * utility/tlcl_generator.c builds structures containing the commands,
13 * as well as the offsets of the fields that need to be set at run
14 * time.
15 */
16
17#include "sysincludes.h"
18#include "tlcl.h"
19#include "tlcl_internal.h"
20#include "tlcl_structures.h"
21#include "utility.h"
22#include "vboot_api.h"
23
24#ifdef FOR_TEST
25/* Allow unit testing implementation of TlclSendReceive() */
26#undef CHROMEOS_ENVIRONMENT
27#endif
28
29/* Sets the size field of a TPM command. */
30static INLINE void SetTpmCommandSize(uint8_t* buffer, uint32_t size) {
31  ToTpmUint32(buffer + sizeof(uint16_t), size);
32}
33
34/* Gets the size field of a TPM command. */
35POSSIBLY_UNUSED static INLINE int TpmCommandSize(const uint8_t* buffer) {
36  uint32_t size;
37  FromTpmUint32(buffer + sizeof(uint16_t), &size);
38  return (int) size;
39}
40
41/* Gets the size field of a TPM request or response. */
42int TlclPacketSize(const uint8_t* packet) {
43  return TpmCommandSize(packet);
44}
45
46/* Gets the code field of a TPM command. */
47static INLINE int TpmCommandCode(const uint8_t* buffer) {
48  uint32_t code;
49  FromTpmUint32(buffer + sizeof(uint16_t) + sizeof(uint32_t), &code);
50  return code;
51}
52
53/* Gets the return code field of a TPM result. */
54static INLINE int TpmReturnCode(const uint8_t* buffer) {
55  return TpmCommandCode(buffer);
56}
57
58/* Like TlclSendReceive below, but do not retry if NEEDS_SELFTEST or
59 * DOING_SELFTEST errors are returned.
60 */
61static uint32_t TlclSendReceiveNoRetry(const uint8_t* request,
62                                       uint8_t* response, int max_length) {
63
64  uint32_t response_length = max_length;
65  uint32_t result;
66
67#ifdef EXTRA_LOGGING
68  VBDEBUG(("TPM: command: %x%x %x%x%x%x %x%x%x%x\n",
69           request[0], request[1],
70           request[2], request[3], request[4], request[5],
71           request[6], request[7], request[8], request[9]));
72#endif
73
74  result = VbExTpmSendReceive(request, TpmCommandSize(request),
75                              response, &response_length);
76  if (0 != result) {
77    /* Communication with TPM failed, so response is garbage */
78    VBDEBUG(("TPM: command 0x%x send/receive failed: 0x%x\n",
79             TpmCommandCode(request), result));
80    return result;
81  }
82  /* Otherwise, use the result code from the response */
83  result = TpmReturnCode(response);
84
85  /* TODO: add paranoia about returned response_length vs. max_length
86   * (and possibly expected length from the response header).  See
87   * crosbug.com/17017 */
88
89#ifdef EXTRA_LOGGING
90  VBDEBUG(("TPM: response: %x%x %x%x%x%x %x%x%x%x\n",
91           response[0], response[1],
92           response[2], response[3], response[4], response[5],
93           response[6], response[7], response[8], response[9]));
94#endif
95
96  VBDEBUG(("TPM: command 0x%x returned 0x%x\n",
97           TpmCommandCode(request), result));
98
99  return result;
100}
101
102
103/* Sends a TPM command and gets a response.  Returns 0 if success or the TPM
104 * error code if error. In the firmware, waits for the self test to complete
105 * if needed. In the host, reports the first error without retries. */
106uint32_t TlclSendReceive(const uint8_t* request, uint8_t* response,
107                         int max_length) {
108  uint32_t result = TlclSendReceiveNoRetry(request, response, max_length);
109  /* When compiling for the firmware, hide command failures due to the self
110   * test not having run or completed. */
111#ifndef CHROMEOS_ENVIRONMENT
112  /* If the command fails because the self test has not completed, try it
113   * again after attempting to ensure that the self test has completed. */
114  if (result == TPM_E_NEEDS_SELFTEST || result == TPM_E_DOING_SELFTEST) {
115    result = TlclContinueSelfTest();
116    if (result != TPM_SUCCESS) {
117      return result;
118    }
119#if defined(TPM_BLOCKING_CONTINUESELFTEST) || defined(VB_RECOVERY_MODE)
120    /* Retry only once */
121    result = TlclSendReceiveNoRetry(request, response, max_length);
122#else
123    /* This needs serious testing.  The TPM specification says: "iii. The
124     * caller MUST wait for the actions of TPM_ContinueSelfTest to complete
125     * before reissuing the command C1."  But, if ContinueSelfTest is
126     * non-blocking, how do we know that the actions have completed other than
127     * trying again? */
128    do {
129      result = TlclSendReceiveNoRetry(request, response, max_length);
130    } while (result == TPM_E_DOING_SELFTEST);
131#endif
132  }
133#endif  /* ! defined(CHROMEOS_ENVIRONMENT) */
134  return result;
135}
136
137/* Sends a command and returns the error code. */
138static uint32_t Send(const uint8_t* command) {
139  uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
140  return TlclSendReceive(command, response, sizeof(response));
141}
142
143/* Exported functions. */
144
145uint32_t TlclLibInit(void) {
146  return VbExTpmInit();
147}
148
149uint32_t TlclLibClose(void) {
150  return VbExTpmClose();
151}
152
153uint32_t TlclStartup(void) {
154  VBDEBUG(("TPM: Startup\n"));
155  return Send(tpm_startup_cmd.buffer);
156}
157
158uint32_t TlclSaveState(void) {
159  VBDEBUG(("TPM: SaveState\n"));
160  return Send(tpm_savestate_cmd.buffer);
161}
162
163uint32_t TlclResume(void) {
164  VBDEBUG(("TPM: Resume\n"));
165  return Send(tpm_resume_cmd.buffer);
166}
167
168uint32_t TlclSelfTestFull(void) {
169  VBDEBUG(("TPM: Self test full\n"));
170  return Send(tpm_selftestfull_cmd.buffer);
171}
172
173uint32_t TlclContinueSelfTest(void) {
174  uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
175  VBDEBUG(("TPM: Continue self test\n"));
176  /* Call the No Retry version of SendReceive to avoid recursion. */
177  return TlclSendReceiveNoRetry(tpm_continueselftest_cmd.buffer,
178                                response, sizeof(response));
179}
180
181uint32_t TlclDefineSpace(uint32_t index, uint32_t perm, uint32_t size) {
182  struct s_tpm_nv_definespace_cmd cmd;
183  VBDEBUG(("TPM: TlclDefineSpace(0x%x, 0x%x, %d)\n", index, perm, size));
184  Memcpy(&cmd, &tpm_nv_definespace_cmd, sizeof(cmd));
185  ToTpmUint32(cmd.buffer + tpm_nv_definespace_cmd.index, index);
186  ToTpmUint32(cmd.buffer + tpm_nv_definespace_cmd.perm, perm);
187  ToTpmUint32(cmd.buffer + tpm_nv_definespace_cmd.size, size);
188  return Send(cmd.buffer);
189}
190
191uint32_t TlclWrite(uint32_t index, const void* data, uint32_t length) {
192  struct s_tpm_nv_write_cmd cmd;
193  uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
194  const int total_length =
195    kTpmRequestHeaderLength + kWriteInfoLength + length;
196
197  VBDEBUG(("TPM: TlclWrite(0x%x, %d)\n", index, length));
198  Memcpy(&cmd, &tpm_nv_write_cmd, sizeof(cmd));
199  VbAssert(total_length <= TPM_LARGE_ENOUGH_COMMAND_SIZE);
200  SetTpmCommandSize(cmd.buffer, total_length);
201
202  ToTpmUint32(cmd.buffer + tpm_nv_write_cmd.index, index);
203  ToTpmUint32(cmd.buffer + tpm_nv_write_cmd.length, length);
204  Memcpy(cmd.buffer + tpm_nv_write_cmd.data, data, length);
205
206  return TlclSendReceive(cmd.buffer, response, sizeof(response));
207}
208
209uint32_t TlclRead(uint32_t index, void* data, uint32_t length) {
210  struct s_tpm_nv_read_cmd cmd;
211  uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
212  uint32_t result_length;
213  uint32_t result;
214
215  VBDEBUG(("TPM: TlclRead(0x%x, %d)\n", index, length));
216  Memcpy(&cmd, &tpm_nv_read_cmd, sizeof(cmd));
217  ToTpmUint32(cmd.buffer + tpm_nv_read_cmd.index, index);
218  ToTpmUint32(cmd.buffer + tpm_nv_read_cmd.length, length);
219
220  result = TlclSendReceive(cmd.buffer, response, sizeof(response));
221  if (result == TPM_SUCCESS && length > 0) {
222    uint8_t* nv_read_cursor = response + kTpmResponseHeaderLength;
223    FromTpmUint32(nv_read_cursor, &result_length);
224    nv_read_cursor += sizeof(uint32_t);
225    Memcpy(data, nv_read_cursor, result_length);
226  }
227
228  return result;
229}
230
231uint32_t TlclPCRRead(uint32_t index, void* data, uint32_t length) {
232  struct s_tpm_pcr_read_cmd cmd;
233  uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
234  uint32_t result;
235
236  VBDEBUG(("TPM: TlclPCRRead(0x%x, %d)\n", index, length));
237  if (length < kPcrDigestLength) {
238    return TPM_E_IOERROR;
239  }
240  Memcpy(&cmd, &tpm_pcr_read_cmd, sizeof(cmd));
241  ToTpmUint32(cmd.buffer + tpm_pcr_read_cmd.pcrNum, index);
242
243  result = TlclSendReceive(cmd.buffer, response, sizeof(response));
244  if (result == TPM_SUCCESS) {
245    uint8_t* pcr_read_cursor = response + kTpmResponseHeaderLength;
246    Memcpy(data, pcr_read_cursor, kPcrDigestLength);
247  }
248
249  return result;
250}
251
252uint32_t TlclWriteLock(uint32_t index) {
253  VBDEBUG(("TPM: Write lock 0x%x\n", index));
254  return TlclWrite(index, NULL, 0);
255}
256
257uint32_t TlclReadLock(uint32_t index) {
258  VBDEBUG(("TPM: Read lock 0x%x\n", index));
259  return TlclRead(index, NULL, 0);
260}
261
262uint32_t TlclAssertPhysicalPresence(void) {
263  VBDEBUG(("TPM: Asserting physical presence\n"));
264  return Send(tpm_ppassert_cmd.buffer);
265}
266
267uint32_t TlclPhysicalPresenceCMDEnable(void) {
268  VBDEBUG(("TPM: Enable the physical presence command\n"));
269  return Send(tpm_ppenable_cmd.buffer);
270}
271
272uint32_t TlclFinalizePhysicalPresence(void) {
273  VBDEBUG(("TPM: Enable PP cmd, disable HW pp, and set lifetime lock\n"));
274  return Send(tpm_finalizepp_cmd.buffer);
275}
276
277uint32_t TlclAssertPhysicalPresenceResult(void) {
278  uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
279  return TlclSendReceive(tpm_ppassert_cmd.buffer, response, sizeof(response));
280}
281
282uint32_t TlclLockPhysicalPresence(void) {
283  VBDEBUG(("TPM: Lock physical presence\n"));
284  return Send(tpm_pplock_cmd.buffer);
285}
286
287uint32_t TlclSetNvLocked(void) {
288  VBDEBUG(("TPM: Set NV locked\n"));
289  return TlclDefineSpace(TPM_NV_INDEX_LOCK, 0, 0);
290}
291
292int TlclIsOwned(void) {
293  uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE + TPM_PUBEK_SIZE];
294  uint32_t result;
295  result = TlclSendReceive(tpm_readpubek_cmd.buffer,
296                           response, sizeof(response));
297  return (result != TPM_SUCCESS);
298}
299
300uint32_t TlclForceClear(void) {
301  VBDEBUG(("TPM: Force clear\n"));
302  return Send(tpm_forceclear_cmd.buffer);
303}
304
305uint32_t TlclSetEnable(void) {
306  VBDEBUG(("TPM: Enabling TPM\n"));
307  return Send(tpm_physicalenable_cmd.buffer);
308}
309
310uint32_t TlclClearEnable(void) {
311  VBDEBUG(("TPM: Disabling TPM\n"));
312  return Send(tpm_physicaldisable_cmd.buffer);
313}
314
315uint32_t TlclSetDeactivated(uint8_t flag) {
316  struct s_tpm_physicalsetdeactivated_cmd cmd;
317  VBDEBUG(("TPM: SetDeactivated(%d)\n", flag));
318  Memcpy(&cmd, &tpm_physicalsetdeactivated_cmd, sizeof(cmd));
319  *(cmd.buffer + cmd.deactivated) = flag;
320  return Send(cmd.buffer);
321}
322
323uint32_t TlclGetPermanentFlags(TPM_PERMANENT_FLAGS* pflags) {
324  uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
325  uint32_t size;
326  uint32_t result =
327    TlclSendReceive(tpm_getflags_cmd.buffer, response, sizeof(response));
328  if (result != TPM_SUCCESS)
329    return result;
330  FromTpmUint32(response + kTpmResponseHeaderLength, &size);
331  VbAssert(size == sizeof(TPM_PERMANENT_FLAGS));
332  Memcpy(pflags,
333         response + kTpmResponseHeaderLength + sizeof(size),
334         sizeof(TPM_PERMANENT_FLAGS));
335  return result;
336}
337
338uint32_t TlclGetSTClearFlags(TPM_STCLEAR_FLAGS* vflags) {
339  uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
340  uint32_t size;
341  uint32_t result =
342    TlclSendReceive(tpm_getstclearflags_cmd.buffer, response, sizeof(response));
343  if (result != TPM_SUCCESS)
344    return result;
345  FromTpmUint32(response + kTpmResponseHeaderLength, &size);
346  /* Ugly assertion, but the struct is padded up by one byte. */
347  VbAssert(size == 7 && sizeof(TPM_STCLEAR_FLAGS) - 1 == 7);
348  Memcpy(vflags,
349         response + kTpmResponseHeaderLength + sizeof(size),
350         sizeof(TPM_STCLEAR_FLAGS));
351  return result;
352}
353
354uint32_t TlclGetFlags(uint8_t* disable,
355                      uint8_t* deactivated,
356                      uint8_t *nvlocked) {
357  TPM_PERMANENT_FLAGS pflags;
358  uint32_t result = TlclGetPermanentFlags(&pflags);
359  if (result == TPM_SUCCESS) {
360    if (disable)
361      *disable = pflags.disable;
362    if (deactivated)
363      *deactivated = pflags.deactivated;
364    if (nvlocked)
365      *nvlocked = pflags.nvLocked;
366    VBDEBUG(("TPM: Got flags disable=%d, deactivated=%d, nvlocked=%d\n",
367             pflags.disable, pflags.deactivated, pflags.nvLocked));
368  }
369  return result;
370}
371
372uint32_t TlclSetGlobalLock(void) {
373  uint32_t x;
374  VBDEBUG(("TPM: Set global lock\n"));
375  return TlclWrite(TPM_NV_INDEX0, (uint8_t*) &x, 0);
376}
377
378uint32_t TlclExtend(int pcr_num, const uint8_t* in_digest,
379                    uint8_t* out_digest) {
380  struct s_tpm_extend_cmd cmd;
381  uint8_t response[kTpmResponseHeaderLength + kPcrDigestLength];
382  uint32_t result;
383
384  Memcpy(&cmd, &tpm_extend_cmd, sizeof(cmd));
385  ToTpmUint32(cmd.buffer + tpm_extend_cmd.pcrNum, pcr_num);
386  Memcpy(cmd.buffer + cmd.inDigest, in_digest, kPcrDigestLength);
387
388  result = TlclSendReceive(cmd.buffer, response, sizeof(response));
389  if (result != TPM_SUCCESS)
390    return result;
391
392  Memcpy(out_digest, response + kTpmResponseHeaderLength, kPcrDigestLength);
393  return result;
394}
395
396uint32_t TlclGetPermissions(uint32_t index, uint32_t* permissions) {
397  struct s_tpm_getpermissions_cmd cmd;
398  uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
399  uint8_t* nvdata;
400  uint32_t result;
401  uint32_t size;
402
403  Memcpy(&cmd, &tpm_getpermissions_cmd, sizeof(cmd));
404  ToTpmUint32(cmd.buffer + tpm_getpermissions_cmd.index, index);
405  result = TlclSendReceive(cmd.buffer, response, sizeof(response));
406  if (result != TPM_SUCCESS)
407    return result;
408
409  nvdata = response + kTpmResponseHeaderLength + sizeof(size);
410  FromTpmUint32(nvdata + kNvDataPublicPermissionsOffset, permissions);
411  return result;
412}
413
414uint32_t TlclGetOwnership(uint8_t* owned) {
415  uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
416  uint32_t size;
417  uint32_t result =
418    TlclSendReceive(tpm_getownership_cmd.buffer, response, sizeof(response));
419  if (result != TPM_SUCCESS)
420    return result;
421  FromTpmUint32(response + kTpmResponseHeaderLength, &size);
422  VbAssert(size == sizeof(*owned));
423  Memcpy(owned,
424         response + kTpmResponseHeaderLength + sizeof(size),
425         sizeof(*owned));
426  return result;
427}
428
429uint32_t TlclGetRandom(uint8_t* data, uint32_t length, uint32_t *size) {
430  struct s_tpm_get_random_cmd cmd;
431  uint8_t response[TPM_LARGE_ENOUGH_COMMAND_SIZE];
432  uint32_t result;
433
434  VBDEBUG(("TPM: TlclGetRandom(%d)\n", length));
435  Memcpy(&cmd, &tpm_get_random_cmd, sizeof(cmd));
436  ToTpmUint32(cmd.buffer + tpm_get_random_cmd.bytesRequested, length);
437  /* There must be room in the response buffer for the bytes. */
438  if (length > TPM_LARGE_ENOUGH_COMMAND_SIZE - kTpmResponseHeaderLength
439               - sizeof(uint32_t)) {
440    return TPM_E_IOERROR;
441  }
442
443  result = TlclSendReceive(cmd.buffer, response, sizeof(response));
444  if (result == TPM_SUCCESS) {
445    uint8_t* get_random_cursor;
446    FromTpmUint32(response + kTpmResponseHeaderLength, size);
447
448    /* There must be room in the target buffer for the bytes. */
449    if (*size > length) {
450      return TPM_E_RESPONSE_TOO_LARGE;
451    }
452    get_random_cursor = response + kTpmResponseHeaderLength
453                                 + sizeof(uint32_t);
454    Memcpy(data, get_random_cursor, *size);
455  }
456
457  return result;
458}
459