tlcl.c 15 KB

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