tpm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * Copyright (c) 2013 The Chromium OS Authors.
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <stdarg.h>
  24. #include <tpm.h>
  25. #include <asm/unaligned.h>
  26. /* Internal error of TPM command library */
  27. #define TPM_LIB_ERROR ((uint32_t)~0u)
  28. /* Useful constants */
  29. enum {
  30. COMMAND_BUFFER_SIZE = 256,
  31. TPM_PUBEK_SIZE = 256,
  32. TPM_REQUEST_HEADER_LENGTH = 10,
  33. TPM_RESPONSE_HEADER_LENGTH = 10,
  34. PCR_DIGEST_LENGTH = 20,
  35. };
  36. /**
  37. * Pack data into a byte string. The data types are specified in
  38. * the format string: 'b' means unsigned byte, 'w' unsigned word,
  39. * 'd' unsigned double word, and 's' byte string. The data are a
  40. * series of offsets and values (for type byte string there are also
  41. * lengths). The data values are packed into the byte string
  42. * sequentially, and so a latter value could over-write a former
  43. * value.
  44. *
  45. * @param str output string
  46. * @param size size of output string
  47. * @param format format string
  48. * @param ... data points
  49. * @return 0 on success, non-0 on error
  50. */
  51. int pack_byte_string(uint8_t *str, size_t size, const char *format, ...)
  52. {
  53. va_list args;
  54. size_t offset = 0, length = 0;
  55. uint8_t *data = NULL;
  56. uint32_t value = 0;
  57. va_start(args, format);
  58. for (; *format; format++) {
  59. switch (*format) {
  60. case 'b':
  61. offset = va_arg(args, size_t);
  62. value = va_arg(args, int);
  63. length = 1;
  64. break;
  65. case 'w':
  66. offset = va_arg(args, size_t);
  67. value = va_arg(args, int);
  68. length = 2;
  69. break;
  70. case 'd':
  71. offset = va_arg(args, size_t);
  72. value = va_arg(args, uint32_t);
  73. length = 4;
  74. break;
  75. case 's':
  76. offset = va_arg(args, size_t);
  77. data = va_arg(args, uint8_t *);
  78. length = va_arg(args, uint32_t);
  79. break;
  80. default:
  81. debug("Couldn't recognize format string\n");
  82. return -1;
  83. }
  84. if (offset + length > size)
  85. return -1;
  86. switch (*format) {
  87. case 'b':
  88. str[offset] = value;
  89. break;
  90. case 'w':
  91. put_unaligned_be16(value, str + offset);
  92. break;
  93. case 'd':
  94. put_unaligned_be32(value, str + offset);
  95. break;
  96. case 's':
  97. memcpy(str + offset, data, length);
  98. break;
  99. }
  100. }
  101. va_end(args);
  102. return 0;
  103. }
  104. /**
  105. * Unpack data from a byte string. The data types are specified in
  106. * the format string: 'b' means unsigned byte, 'w' unsigned word,
  107. * 'd' unsigned double word, and 's' byte string. The data are a
  108. * series of offsets and pointers (for type byte string there are also
  109. * lengths).
  110. *
  111. * @param str output string
  112. * @param size size of output string
  113. * @param format format string
  114. * @param ... data points
  115. * @return 0 on success, non-0 on error
  116. */
  117. int unpack_byte_string(const uint8_t *str, size_t size, const char *format, ...)
  118. {
  119. va_list args;
  120. size_t offset = 0, length = 0;
  121. uint8_t *ptr8 = NULL;
  122. uint16_t *ptr16 = NULL;
  123. uint32_t *ptr32 = NULL;
  124. va_start(args, format);
  125. for (; *format; format++) {
  126. switch (*format) {
  127. case 'b':
  128. offset = va_arg(args, size_t);
  129. ptr8 = va_arg(args, uint8_t *);
  130. length = 1;
  131. break;
  132. case 'w':
  133. offset = va_arg(args, size_t);
  134. ptr16 = va_arg(args, uint16_t *);
  135. length = 2;
  136. break;
  137. case 'd':
  138. offset = va_arg(args, size_t);
  139. ptr32 = va_arg(args, uint32_t *);
  140. length = 4;
  141. break;
  142. case 's':
  143. offset = va_arg(args, size_t);
  144. ptr8 = va_arg(args, uint8_t *);
  145. length = va_arg(args, uint32_t);
  146. break;
  147. default:
  148. debug("Couldn't recognize format string\n");
  149. return -1;
  150. }
  151. if (offset + length > size)
  152. return -1;
  153. switch (*format) {
  154. case 'b':
  155. *ptr8 = str[offset];
  156. break;
  157. case 'w':
  158. *ptr16 = get_unaligned_be16(str + offset);
  159. break;
  160. case 'd':
  161. *ptr32 = get_unaligned_be32(str + offset);
  162. break;
  163. case 's':
  164. memcpy(ptr8, str + offset, length);
  165. break;
  166. }
  167. }
  168. va_end(args);
  169. return 0;
  170. }
  171. /**
  172. * Get TPM command size.
  173. *
  174. * @param command byte string of TPM command
  175. * @return command size of the TPM command
  176. */
  177. static uint32_t tpm_command_size(const void *command)
  178. {
  179. const size_t command_size_offset = 2;
  180. return get_unaligned_be32(command + command_size_offset);
  181. }
  182. /**
  183. * Get TPM response return code, which is one of TPM_RESULT values.
  184. *
  185. * @param response byte string of TPM response
  186. * @return return code of the TPM response
  187. */
  188. static uint32_t tpm_return_code(const void *response)
  189. {
  190. const size_t return_code_offset = 6;
  191. return get_unaligned_be32(response + return_code_offset);
  192. }
  193. /**
  194. * Send a TPM command and return response's return code, and optionally
  195. * return response to caller.
  196. *
  197. * @param command byte string of TPM command
  198. * @param response output buffer for TPM response, or NULL if the
  199. * caller does not care about it
  200. * @param size_ptr output buffer size (input parameter) and TPM
  201. * response length (output parameter); this parameter
  202. * is a bidirectional
  203. * @return return code of the TPM response
  204. */
  205. static uint32_t tpm_sendrecv_command(const void *command,
  206. void *response, size_t *size_ptr)
  207. {
  208. uint8_t response_buffer[COMMAND_BUFFER_SIZE];
  209. size_t response_length;
  210. uint32_t err;
  211. if (response) {
  212. response_length = *size_ptr;
  213. } else {
  214. response = response_buffer;
  215. response_length = sizeof(response_buffer);
  216. }
  217. err = tis_sendrecv(command, tpm_command_size(command),
  218. response, &response_length);
  219. if (err)
  220. return TPM_LIB_ERROR;
  221. if (response)
  222. *size_ptr = response_length;
  223. return tpm_return_code(response);
  224. }
  225. uint32_t tpm_init(void)
  226. {
  227. uint32_t err;
  228. err = tis_init();
  229. if (err)
  230. return err;
  231. return tis_open();
  232. }
  233. uint32_t tpm_startup(enum tpm_startup_type mode)
  234. {
  235. const uint8_t command[12] = {
  236. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x99, 0x0, 0x0,
  237. };
  238. const size_t mode_offset = 10;
  239. uint8_t buf[COMMAND_BUFFER_SIZE];
  240. if (pack_byte_string(buf, sizeof(buf), "sw",
  241. 0, command, sizeof(command),
  242. mode_offset, mode))
  243. return TPM_LIB_ERROR;
  244. return tpm_sendrecv_command(buf, NULL, NULL);
  245. }
  246. uint32_t tpm_self_test_full(void)
  247. {
  248. const uint8_t command[10] = {
  249. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x50,
  250. };
  251. return tpm_sendrecv_command(command, NULL, NULL);
  252. }
  253. uint32_t tpm_continue_self_test(void)
  254. {
  255. const uint8_t command[10] = {
  256. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x53,
  257. };
  258. return tpm_sendrecv_command(command, NULL, NULL);
  259. }
  260. uint32_t tpm_nv_define_space(uint32_t index, uint32_t perm, uint32_t size)
  261. {
  262. const uint8_t command[101] = {
  263. 0x0, 0xc1, /* TPM_TAG */
  264. 0x0, 0x0, 0x0, 0x65, /* parameter size */
  265. 0x0, 0x0, 0x0, 0xcc, /* TPM_COMMAND_CODE */
  266. /* TPM_NV_DATA_PUBLIC->... */
  267. 0x0, 0x18, /* ...->TPM_STRUCTURE_TAG */
  268. 0, 0, 0, 0, /* ...->TPM_NV_INDEX */
  269. /* TPM_NV_DATA_PUBLIC->TPM_PCR_INFO_SHORT */
  270. 0x0, 0x3,
  271. 0, 0, 0,
  272. 0x1f,
  273. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  274. /* TPM_NV_DATA_PUBLIC->TPM_PCR_INFO_SHORT */
  275. 0x0, 0x3,
  276. 0, 0, 0,
  277. 0x1f,
  278. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  279. /* TPM_NV_ATTRIBUTES->... */
  280. 0x0, 0x17, /* ...->TPM_STRUCTURE_TAG */
  281. 0, 0, 0, 0, /* ...->attributes */
  282. /* End of TPM_NV_ATTRIBUTES */
  283. 0, /* bReadSTClear */
  284. 0, /* bWriteSTClear */
  285. 0, /* bWriteDefine */
  286. 0, 0, 0, 0, /* size */
  287. };
  288. const size_t index_offset = 12;
  289. const size_t perm_offset = 70;
  290. const size_t size_offset = 77;
  291. uint8_t buf[COMMAND_BUFFER_SIZE];
  292. if (pack_byte_string(buf, sizeof(buf), "sddd",
  293. 0, command, sizeof(command),
  294. index_offset, index,
  295. perm_offset, perm,
  296. size_offset, size))
  297. return TPM_LIB_ERROR;
  298. return tpm_sendrecv_command(buf, NULL, NULL);
  299. }
  300. uint32_t tpm_nv_read_value(uint32_t index, void *data, uint32_t count)
  301. {
  302. const uint8_t command[22] = {
  303. 0x0, 0xc1, 0x0, 0x0, 0x0, 0x16, 0x0, 0x0, 0x0, 0xcf,
  304. };
  305. const size_t index_offset = 10;
  306. const size_t length_offset = 18;
  307. const size_t data_size_offset = 10;
  308. const size_t data_offset = 14;
  309. uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
  310. size_t response_length = sizeof(response);
  311. uint32_t data_size;
  312. uint32_t err;
  313. if (pack_byte_string(buf, sizeof(buf), "sdd",
  314. 0, command, sizeof(command),
  315. index_offset, index,
  316. length_offset, count))
  317. return TPM_LIB_ERROR;
  318. err = tpm_sendrecv_command(buf, response, &response_length);
  319. if (err)
  320. return err;
  321. if (unpack_byte_string(response, response_length, "d",
  322. data_size_offset, &data_size))
  323. return TPM_LIB_ERROR;
  324. if (data_size > count)
  325. return TPM_LIB_ERROR;
  326. if (unpack_byte_string(response, response_length, "s",
  327. data_offset, data, data_size))
  328. return TPM_LIB_ERROR;
  329. return 0;
  330. }
  331. uint32_t tpm_nv_write_value(uint32_t index, const void *data, uint32_t length)
  332. {
  333. const uint8_t command[256] = {
  334. 0x0, 0xc1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xcd,
  335. };
  336. const size_t command_size_offset = 2;
  337. const size_t index_offset = 10;
  338. const size_t length_offset = 18;
  339. const size_t data_offset = 22;
  340. const size_t write_info_size = 12;
  341. const uint32_t total_length =
  342. TPM_REQUEST_HEADER_LENGTH + write_info_size + length;
  343. uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
  344. size_t response_length = sizeof(response);
  345. uint32_t err;
  346. if (pack_byte_string(buf, sizeof(buf), "sddds",
  347. 0, command, sizeof(command),
  348. command_size_offset, total_length,
  349. index_offset, index,
  350. length_offset, length,
  351. data_offset, data, length))
  352. return TPM_LIB_ERROR;
  353. err = tpm_sendrecv_command(buf, response, &response_length);
  354. if (err)
  355. return err;
  356. return 0;
  357. }
  358. uint32_t tpm_extend(uint32_t index, const void *in_digest, void *out_digest)
  359. {
  360. const uint8_t command[34] = {
  361. 0x0, 0xc1, 0x0, 0x0, 0x0, 0x22, 0x0, 0x0, 0x0, 0x14,
  362. };
  363. const size_t index_offset = 10;
  364. const size_t in_digest_offset = 14;
  365. const size_t out_digest_offset = 10;
  366. uint8_t buf[COMMAND_BUFFER_SIZE];
  367. uint8_t response[TPM_RESPONSE_HEADER_LENGTH + PCR_DIGEST_LENGTH];
  368. size_t response_length = sizeof(response);
  369. uint32_t err;
  370. if (pack_byte_string(buf, sizeof(buf), "sds",
  371. 0, command, sizeof(command),
  372. index_offset, index,
  373. in_digest_offset, in_digest,
  374. PCR_DIGEST_LENGTH))
  375. return TPM_LIB_ERROR;
  376. err = tpm_sendrecv_command(buf, response, &response_length);
  377. if (err)
  378. return err;
  379. if (unpack_byte_string(response, response_length, "s",
  380. out_digest_offset, out_digest,
  381. PCR_DIGEST_LENGTH))
  382. return TPM_LIB_ERROR;
  383. return 0;
  384. }
  385. uint32_t tpm_pcr_read(uint32_t index, void *data, size_t count)
  386. {
  387. const uint8_t command[14] = {
  388. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xe, 0x0, 0x0, 0x0, 0x15,
  389. };
  390. const size_t index_offset = 10;
  391. const size_t out_digest_offset = 10;
  392. uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
  393. size_t response_length = sizeof(response);
  394. uint32_t err;
  395. if (count < PCR_DIGEST_LENGTH)
  396. return TPM_LIB_ERROR;
  397. if (pack_byte_string(buf, sizeof(buf), "sd",
  398. 0, command, sizeof(command),
  399. index_offset, index))
  400. return TPM_LIB_ERROR;
  401. err = tpm_sendrecv_command(buf, response, &response_length);
  402. if (err)
  403. return err;
  404. if (unpack_byte_string(response, response_length, "s",
  405. out_digest_offset, data, PCR_DIGEST_LENGTH))
  406. return TPM_LIB_ERROR;
  407. return 0;
  408. }
  409. uint32_t tpm_tsc_physical_presence(uint16_t presence)
  410. {
  411. const uint8_t command[12] = {
  412. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x40, 0x0, 0x0, 0xa, 0x0, 0x0,
  413. };
  414. const size_t presence_offset = 10;
  415. uint8_t buf[COMMAND_BUFFER_SIZE];
  416. if (pack_byte_string(buf, sizeof(buf), "sw",
  417. 0, command, sizeof(command),
  418. presence_offset, presence))
  419. return TPM_LIB_ERROR;
  420. return tpm_sendrecv_command(buf, NULL, NULL);
  421. }
  422. uint32_t tpm_read_pubek(void *data, size_t count)
  423. {
  424. const uint8_t command[30] = {
  425. 0x0, 0xc1, 0x0, 0x0, 0x0, 0x1e, 0x0, 0x0, 0x0, 0x7c,
  426. };
  427. const size_t response_size_offset = 2;
  428. const size_t data_offset = 10;
  429. const size_t header_and_checksum_size = TPM_RESPONSE_HEADER_LENGTH + 20;
  430. uint8_t response[COMMAND_BUFFER_SIZE + TPM_PUBEK_SIZE];
  431. size_t response_length = sizeof(response);
  432. uint32_t data_size;
  433. uint32_t err;
  434. err = tpm_sendrecv_command(command, response, &response_length);
  435. if (err)
  436. return err;
  437. if (unpack_byte_string(response, response_length, "d",
  438. response_size_offset, &data_size))
  439. return TPM_LIB_ERROR;
  440. if (data_size < header_and_checksum_size)
  441. return TPM_LIB_ERROR;
  442. data_size -= header_and_checksum_size;
  443. if (data_size > count)
  444. return TPM_LIB_ERROR;
  445. if (unpack_byte_string(response, response_length, "s",
  446. data_offset, data, data_size))
  447. return TPM_LIB_ERROR;
  448. return 0;
  449. }
  450. uint32_t tpm_force_clear(void)
  451. {
  452. const uint8_t command[10] = {
  453. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x5d,
  454. };
  455. return tpm_sendrecv_command(command, NULL, NULL);
  456. }
  457. uint32_t tpm_physical_enable(void)
  458. {
  459. const uint8_t command[10] = {
  460. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x6f,
  461. };
  462. return tpm_sendrecv_command(command, NULL, NULL);
  463. }
  464. uint32_t tpm_physical_disable(void)
  465. {
  466. const uint8_t command[10] = {
  467. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x70,
  468. };
  469. return tpm_sendrecv_command(command, NULL, NULL);
  470. }
  471. uint32_t tpm_physical_set_deactivated(uint8_t state)
  472. {
  473. const uint8_t command[11] = {
  474. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xb, 0x0, 0x0, 0x0, 0x72,
  475. };
  476. const size_t state_offset = 10;
  477. uint8_t buf[COMMAND_BUFFER_SIZE];
  478. if (pack_byte_string(buf, sizeof(buf), "sb",
  479. 0, command, sizeof(command),
  480. state_offset, state))
  481. return TPM_LIB_ERROR;
  482. return tpm_sendrecv_command(buf, NULL, NULL);
  483. }
  484. uint32_t tpm_get_capability(uint32_t cap_area, uint32_t sub_cap,
  485. void *cap, size_t count)
  486. {
  487. const uint8_t command[22] = {
  488. 0x0, 0xc1, /* TPM_TAG */
  489. 0x0, 0x0, 0x0, 0x16, /* parameter size */
  490. 0x0, 0x0, 0x0, 0x65, /* TPM_COMMAND_CODE */
  491. 0x0, 0x0, 0x0, 0x0, /* TPM_CAPABILITY_AREA */
  492. 0x0, 0x0, 0x0, 0x4, /* subcap size */
  493. 0x0, 0x0, 0x0, 0x0, /* subcap value */
  494. };
  495. const size_t cap_area_offset = 10;
  496. const size_t sub_cap_offset = 18;
  497. const size_t cap_offset = 14;
  498. const size_t cap_size_offset = 10;
  499. uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
  500. size_t response_length = sizeof(response);
  501. uint32_t cap_size;
  502. uint32_t err;
  503. if (pack_byte_string(buf, sizeof(buf), "sdd",
  504. 0, command, sizeof(command),
  505. cap_area_offset, cap_area,
  506. sub_cap_offset, sub_cap))
  507. return TPM_LIB_ERROR;
  508. err = tpm_sendrecv_command(buf, response, &response_length);
  509. if (err)
  510. return err;
  511. if (unpack_byte_string(response, response_length, "d",
  512. cap_size_offset, &cap_size))
  513. return TPM_LIB_ERROR;
  514. if (cap_size > response_length || cap_size > count)
  515. return TPM_LIB_ERROR;
  516. if (unpack_byte_string(response, response_length, "s",
  517. cap_offset, cap, cap_size))
  518. return TPM_LIB_ERROR;
  519. return 0;
  520. }