tpm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /*
  2. * Copyright (c) 2013 The Chromium OS Authors.
  3. * Coypright (c) 2013 Guntermann & Drunck GmbH
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <tis.h>
  10. #include <tpm.h>
  11. #include <asm/unaligned.h>
  12. #include <u-boot/sha1.h>
  13. /* Internal error of TPM command library */
  14. #define TPM_LIB_ERROR ((uint32_t)~0u)
  15. /* Useful constants */
  16. enum {
  17. COMMAND_BUFFER_SIZE = 256,
  18. TPM_REQUEST_HEADER_LENGTH = 10,
  19. TPM_RESPONSE_HEADER_LENGTH = 10,
  20. PCR_DIGEST_LENGTH = 20,
  21. DIGEST_LENGTH = 20,
  22. TPM_REQUEST_AUTH_LENGTH = 45,
  23. TPM_RESPONSE_AUTH_LENGTH = 41,
  24. /* some max lengths, valid for RSA keys <= 2048 bits */
  25. TPM_KEY12_MAX_LENGTH = 618,
  26. TPM_PUBKEY_MAX_LENGTH = 288,
  27. };
  28. #ifdef CONFIG_TPM_AUTH_SESSIONS
  29. #ifndef CONFIG_SHA1
  30. #error "TPM_AUTH_SESSIONS require SHA1 to be configured, too"
  31. #endif /* !CONFIG_SHA1 */
  32. struct session_data {
  33. int valid;
  34. uint32_t handle;
  35. uint8_t nonce_even[DIGEST_LENGTH];
  36. uint8_t nonce_odd[DIGEST_LENGTH];
  37. };
  38. static struct session_data oiap_session = {0, };
  39. #endif /* CONFIG_TPM_AUTH_SESSIONS */
  40. /**
  41. * Pack data into a byte string. The data types are specified in
  42. * the format string: 'b' means unsigned byte, 'w' unsigned word,
  43. * 'd' unsigned double word, and 's' byte string. The data are a
  44. * series of offsets and values (for type byte string there are also
  45. * lengths). The data values are packed into the byte string
  46. * sequentially, and so a latter value could over-write a former
  47. * value.
  48. *
  49. * @param str output string
  50. * @param size size of output string
  51. * @param format format string
  52. * @param ... data points
  53. * @return 0 on success, non-0 on error
  54. */
  55. int pack_byte_string(uint8_t *str, size_t size, const char *format, ...)
  56. {
  57. va_list args;
  58. size_t offset = 0, length = 0;
  59. uint8_t *data = NULL;
  60. uint32_t value = 0;
  61. va_start(args, format);
  62. for (; *format; format++) {
  63. switch (*format) {
  64. case 'b':
  65. offset = va_arg(args, size_t);
  66. value = va_arg(args, int);
  67. length = 1;
  68. break;
  69. case 'w':
  70. offset = va_arg(args, size_t);
  71. value = va_arg(args, int);
  72. length = 2;
  73. break;
  74. case 'd':
  75. offset = va_arg(args, size_t);
  76. value = va_arg(args, uint32_t);
  77. length = 4;
  78. break;
  79. case 's':
  80. offset = va_arg(args, size_t);
  81. data = va_arg(args, uint8_t *);
  82. length = va_arg(args, uint32_t);
  83. break;
  84. default:
  85. debug("Couldn't recognize format string\n");
  86. return -1;
  87. }
  88. if (offset + length > size)
  89. return -1;
  90. switch (*format) {
  91. case 'b':
  92. str[offset] = value;
  93. break;
  94. case 'w':
  95. put_unaligned_be16(value, str + offset);
  96. break;
  97. case 'd':
  98. put_unaligned_be32(value, str + offset);
  99. break;
  100. case 's':
  101. memcpy(str + offset, data, length);
  102. break;
  103. }
  104. }
  105. va_end(args);
  106. return 0;
  107. }
  108. /**
  109. * Unpack data from a byte string. The data types are specified in
  110. * the format string: 'b' means unsigned byte, 'w' unsigned word,
  111. * 'd' unsigned double word, and 's' byte string. The data are a
  112. * series of offsets and pointers (for type byte string there are also
  113. * lengths).
  114. *
  115. * @param str output string
  116. * @param size size of output string
  117. * @param format format string
  118. * @param ... data points
  119. * @return 0 on success, non-0 on error
  120. */
  121. int unpack_byte_string(const uint8_t *str, size_t size, const char *format, ...)
  122. {
  123. va_list args;
  124. size_t offset = 0, length = 0;
  125. uint8_t *ptr8 = NULL;
  126. uint16_t *ptr16 = NULL;
  127. uint32_t *ptr32 = NULL;
  128. va_start(args, format);
  129. for (; *format; format++) {
  130. switch (*format) {
  131. case 'b':
  132. offset = va_arg(args, size_t);
  133. ptr8 = va_arg(args, uint8_t *);
  134. length = 1;
  135. break;
  136. case 'w':
  137. offset = va_arg(args, size_t);
  138. ptr16 = va_arg(args, uint16_t *);
  139. length = 2;
  140. break;
  141. case 'd':
  142. offset = va_arg(args, size_t);
  143. ptr32 = va_arg(args, uint32_t *);
  144. length = 4;
  145. break;
  146. case 's':
  147. offset = va_arg(args, size_t);
  148. ptr8 = va_arg(args, uint8_t *);
  149. length = va_arg(args, uint32_t);
  150. break;
  151. default:
  152. debug("Couldn't recognize format string\n");
  153. return -1;
  154. }
  155. if (offset + length > size)
  156. return -1;
  157. switch (*format) {
  158. case 'b':
  159. *ptr8 = str[offset];
  160. break;
  161. case 'w':
  162. *ptr16 = get_unaligned_be16(str + offset);
  163. break;
  164. case 'd':
  165. *ptr32 = get_unaligned_be32(str + offset);
  166. break;
  167. case 's':
  168. memcpy(ptr8, str + offset, length);
  169. break;
  170. }
  171. }
  172. va_end(args);
  173. return 0;
  174. }
  175. /**
  176. * Get TPM command size.
  177. *
  178. * @param command byte string of TPM command
  179. * @return command size of the TPM command
  180. */
  181. static uint32_t tpm_command_size(const void *command)
  182. {
  183. const size_t command_size_offset = 2;
  184. return get_unaligned_be32(command + command_size_offset);
  185. }
  186. /**
  187. * Get TPM response return code, which is one of TPM_RESULT values.
  188. *
  189. * @param response byte string of TPM response
  190. * @return return code of the TPM response
  191. */
  192. static uint32_t tpm_return_code(const void *response)
  193. {
  194. const size_t return_code_offset = 6;
  195. return get_unaligned_be32(response + return_code_offset);
  196. }
  197. /**
  198. * Send a TPM command and return response's return code, and optionally
  199. * return response to caller.
  200. *
  201. * @param command byte string of TPM command
  202. * @param response output buffer for TPM response, or NULL if the
  203. * caller does not care about it
  204. * @param size_ptr output buffer size (input parameter) and TPM
  205. * response length (output parameter); this parameter
  206. * is a bidirectional
  207. * @return return code of the TPM response
  208. */
  209. static uint32_t tpm_sendrecv_command(const void *command,
  210. void *response, size_t *size_ptr)
  211. {
  212. uint8_t response_buffer[COMMAND_BUFFER_SIZE];
  213. size_t response_length;
  214. uint32_t err;
  215. if (response) {
  216. response_length = *size_ptr;
  217. } else {
  218. response = response_buffer;
  219. response_length = sizeof(response_buffer);
  220. }
  221. #ifdef CONFIG_DM_TPM
  222. struct udevice *dev;
  223. int ret;
  224. ret = uclass_first_device(UCLASS_TPM, &dev);
  225. if (ret)
  226. return ret;
  227. err = tpm_xfer(dev, command, tpm_command_size(command),
  228. response, &response_length);
  229. #else
  230. err = tis_sendrecv(command, tpm_command_size(command),
  231. response, &response_length);
  232. #endif
  233. if (err < 0)
  234. return TPM_LIB_ERROR;
  235. if (size_ptr)
  236. *size_ptr = response_length;
  237. return tpm_return_code(response);
  238. }
  239. int tpm_init(void)
  240. {
  241. int err;
  242. #ifdef CONFIG_DM_TPM
  243. struct udevice *dev;
  244. err = uclass_first_device(UCLASS_TPM, &dev);
  245. if (err)
  246. return err;
  247. return tpm_open(dev);
  248. #else
  249. err = tis_init();
  250. if (err)
  251. return err;
  252. return tis_open();
  253. #endif
  254. }
  255. uint32_t tpm_startup(enum tpm_startup_type mode)
  256. {
  257. const uint8_t command[12] = {
  258. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x99, 0x0, 0x0,
  259. };
  260. const size_t mode_offset = 10;
  261. uint8_t buf[COMMAND_BUFFER_SIZE];
  262. if (pack_byte_string(buf, sizeof(buf), "sw",
  263. 0, command, sizeof(command),
  264. mode_offset, mode))
  265. return TPM_LIB_ERROR;
  266. return tpm_sendrecv_command(buf, NULL, NULL);
  267. }
  268. uint32_t tpm_self_test_full(void)
  269. {
  270. const uint8_t command[10] = {
  271. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x50,
  272. };
  273. return tpm_sendrecv_command(command, NULL, NULL);
  274. }
  275. uint32_t tpm_continue_self_test(void)
  276. {
  277. const uint8_t command[10] = {
  278. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x53,
  279. };
  280. return tpm_sendrecv_command(command, NULL, NULL);
  281. }
  282. uint32_t tpm_nv_define_space(uint32_t index, uint32_t perm, uint32_t size)
  283. {
  284. const uint8_t command[101] = {
  285. 0x0, 0xc1, /* TPM_TAG */
  286. 0x0, 0x0, 0x0, 0x65, /* parameter size */
  287. 0x0, 0x0, 0x0, 0xcc, /* TPM_COMMAND_CODE */
  288. /* TPM_NV_DATA_PUBLIC->... */
  289. 0x0, 0x18, /* ...->TPM_STRUCTURE_TAG */
  290. 0, 0, 0, 0, /* ...->TPM_NV_INDEX */
  291. /* TPM_NV_DATA_PUBLIC->TPM_PCR_INFO_SHORT */
  292. 0x0, 0x3,
  293. 0, 0, 0,
  294. 0x1f,
  295. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  296. /* TPM_NV_DATA_PUBLIC->TPM_PCR_INFO_SHORT */
  297. 0x0, 0x3,
  298. 0, 0, 0,
  299. 0x1f,
  300. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  301. /* TPM_NV_ATTRIBUTES->... */
  302. 0x0, 0x17, /* ...->TPM_STRUCTURE_TAG */
  303. 0, 0, 0, 0, /* ...->attributes */
  304. /* End of TPM_NV_ATTRIBUTES */
  305. 0, /* bReadSTClear */
  306. 0, /* bWriteSTClear */
  307. 0, /* bWriteDefine */
  308. 0, 0, 0, 0, /* size */
  309. };
  310. const size_t index_offset = 12;
  311. const size_t perm_offset = 70;
  312. const size_t size_offset = 77;
  313. uint8_t buf[COMMAND_BUFFER_SIZE];
  314. if (pack_byte_string(buf, sizeof(buf), "sddd",
  315. 0, command, sizeof(command),
  316. index_offset, index,
  317. perm_offset, perm,
  318. size_offset, size))
  319. return TPM_LIB_ERROR;
  320. return tpm_sendrecv_command(buf, NULL, NULL);
  321. }
  322. uint32_t tpm_nv_read_value(uint32_t index, void *data, uint32_t count)
  323. {
  324. const uint8_t command[22] = {
  325. 0x0, 0xc1, 0x0, 0x0, 0x0, 0x16, 0x0, 0x0, 0x0, 0xcf,
  326. };
  327. const size_t index_offset = 10;
  328. const size_t length_offset = 18;
  329. const size_t data_size_offset = 10;
  330. const size_t data_offset = 14;
  331. uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
  332. size_t response_length = sizeof(response);
  333. uint32_t data_size;
  334. uint32_t err;
  335. if (pack_byte_string(buf, sizeof(buf), "sdd",
  336. 0, command, sizeof(command),
  337. index_offset, index,
  338. length_offset, count))
  339. return TPM_LIB_ERROR;
  340. err = tpm_sendrecv_command(buf, response, &response_length);
  341. if (err)
  342. return err;
  343. if (unpack_byte_string(response, response_length, "d",
  344. data_size_offset, &data_size))
  345. return TPM_LIB_ERROR;
  346. if (data_size > count)
  347. return TPM_LIB_ERROR;
  348. if (unpack_byte_string(response, response_length, "s",
  349. data_offset, data, data_size))
  350. return TPM_LIB_ERROR;
  351. return 0;
  352. }
  353. uint32_t tpm_nv_write_value(uint32_t index, const void *data, uint32_t length)
  354. {
  355. const uint8_t command[256] = {
  356. 0x0, 0xc1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xcd,
  357. };
  358. const size_t command_size_offset = 2;
  359. const size_t index_offset = 10;
  360. const size_t length_offset = 18;
  361. const size_t data_offset = 22;
  362. const size_t write_info_size = 12;
  363. const uint32_t total_length =
  364. TPM_REQUEST_HEADER_LENGTH + write_info_size + length;
  365. uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
  366. size_t response_length = sizeof(response);
  367. uint32_t err;
  368. if (pack_byte_string(buf, sizeof(buf), "sddds",
  369. 0, command, sizeof(command),
  370. command_size_offset, total_length,
  371. index_offset, index,
  372. length_offset, length,
  373. data_offset, data, length))
  374. return TPM_LIB_ERROR;
  375. err = tpm_sendrecv_command(buf, response, &response_length);
  376. if (err)
  377. return err;
  378. return 0;
  379. }
  380. uint32_t tpm_extend(uint32_t index, const void *in_digest, void *out_digest)
  381. {
  382. const uint8_t command[34] = {
  383. 0x0, 0xc1, 0x0, 0x0, 0x0, 0x22, 0x0, 0x0, 0x0, 0x14,
  384. };
  385. const size_t index_offset = 10;
  386. const size_t in_digest_offset = 14;
  387. const size_t out_digest_offset = 10;
  388. uint8_t buf[COMMAND_BUFFER_SIZE];
  389. uint8_t response[TPM_RESPONSE_HEADER_LENGTH + PCR_DIGEST_LENGTH];
  390. size_t response_length = sizeof(response);
  391. uint32_t err;
  392. if (pack_byte_string(buf, sizeof(buf), "sds",
  393. 0, command, sizeof(command),
  394. index_offset, index,
  395. in_digest_offset, in_digest,
  396. PCR_DIGEST_LENGTH))
  397. return TPM_LIB_ERROR;
  398. err = tpm_sendrecv_command(buf, response, &response_length);
  399. if (err)
  400. return err;
  401. if (unpack_byte_string(response, response_length, "s",
  402. out_digest_offset, out_digest,
  403. PCR_DIGEST_LENGTH))
  404. return TPM_LIB_ERROR;
  405. return 0;
  406. }
  407. uint32_t tpm_pcr_read(uint32_t index, void *data, size_t count)
  408. {
  409. const uint8_t command[14] = {
  410. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xe, 0x0, 0x0, 0x0, 0x15,
  411. };
  412. const size_t index_offset = 10;
  413. const size_t out_digest_offset = 10;
  414. uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
  415. size_t response_length = sizeof(response);
  416. uint32_t err;
  417. if (count < PCR_DIGEST_LENGTH)
  418. return TPM_LIB_ERROR;
  419. if (pack_byte_string(buf, sizeof(buf), "sd",
  420. 0, command, sizeof(command),
  421. index_offset, index))
  422. return TPM_LIB_ERROR;
  423. err = tpm_sendrecv_command(buf, response, &response_length);
  424. if (err)
  425. return err;
  426. if (unpack_byte_string(response, response_length, "s",
  427. out_digest_offset, data, PCR_DIGEST_LENGTH))
  428. return TPM_LIB_ERROR;
  429. return 0;
  430. }
  431. uint32_t tpm_tsc_physical_presence(uint16_t presence)
  432. {
  433. const uint8_t command[12] = {
  434. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x40, 0x0, 0x0, 0xa, 0x0, 0x0,
  435. };
  436. const size_t presence_offset = 10;
  437. uint8_t buf[COMMAND_BUFFER_SIZE];
  438. if (pack_byte_string(buf, sizeof(buf), "sw",
  439. 0, command, sizeof(command),
  440. presence_offset, presence))
  441. return TPM_LIB_ERROR;
  442. return tpm_sendrecv_command(buf, NULL, NULL);
  443. }
  444. uint32_t tpm_read_pubek(void *data, size_t count)
  445. {
  446. const uint8_t command[30] = {
  447. 0x0, 0xc1, 0x0, 0x0, 0x0, 0x1e, 0x0, 0x0, 0x0, 0x7c,
  448. };
  449. const size_t response_size_offset = 2;
  450. const size_t data_offset = 10;
  451. const size_t header_and_checksum_size = TPM_RESPONSE_HEADER_LENGTH + 20;
  452. uint8_t response[COMMAND_BUFFER_SIZE + TPM_PUBEK_SIZE];
  453. size_t response_length = sizeof(response);
  454. uint32_t data_size;
  455. uint32_t err;
  456. err = tpm_sendrecv_command(command, response, &response_length);
  457. if (err)
  458. return err;
  459. if (unpack_byte_string(response, response_length, "d",
  460. response_size_offset, &data_size))
  461. return TPM_LIB_ERROR;
  462. if (data_size < header_and_checksum_size)
  463. return TPM_LIB_ERROR;
  464. data_size -= header_and_checksum_size;
  465. if (data_size > count)
  466. return TPM_LIB_ERROR;
  467. if (unpack_byte_string(response, response_length, "s",
  468. data_offset, data, data_size))
  469. return TPM_LIB_ERROR;
  470. return 0;
  471. }
  472. uint32_t tpm_force_clear(void)
  473. {
  474. const uint8_t command[10] = {
  475. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x5d,
  476. };
  477. return tpm_sendrecv_command(command, NULL, NULL);
  478. }
  479. uint32_t tpm_physical_enable(void)
  480. {
  481. const uint8_t command[10] = {
  482. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x6f,
  483. };
  484. return tpm_sendrecv_command(command, NULL, NULL);
  485. }
  486. uint32_t tpm_physical_disable(void)
  487. {
  488. const uint8_t command[10] = {
  489. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x70,
  490. };
  491. return tpm_sendrecv_command(command, NULL, NULL);
  492. }
  493. uint32_t tpm_physical_set_deactivated(uint8_t state)
  494. {
  495. const uint8_t command[11] = {
  496. 0x0, 0xc1, 0x0, 0x0, 0x0, 0xb, 0x0, 0x0, 0x0, 0x72,
  497. };
  498. const size_t state_offset = 10;
  499. uint8_t buf[COMMAND_BUFFER_SIZE];
  500. if (pack_byte_string(buf, sizeof(buf), "sb",
  501. 0, command, sizeof(command),
  502. state_offset, state))
  503. return TPM_LIB_ERROR;
  504. return tpm_sendrecv_command(buf, NULL, NULL);
  505. }
  506. uint32_t tpm_get_capability(uint32_t cap_area, uint32_t sub_cap,
  507. void *cap, size_t count)
  508. {
  509. const uint8_t command[22] = {
  510. 0x0, 0xc1, /* TPM_TAG */
  511. 0x0, 0x0, 0x0, 0x16, /* parameter size */
  512. 0x0, 0x0, 0x0, 0x65, /* TPM_COMMAND_CODE */
  513. 0x0, 0x0, 0x0, 0x0, /* TPM_CAPABILITY_AREA */
  514. 0x0, 0x0, 0x0, 0x4, /* subcap size */
  515. 0x0, 0x0, 0x0, 0x0, /* subcap value */
  516. };
  517. const size_t cap_area_offset = 10;
  518. const size_t sub_cap_offset = 18;
  519. const size_t cap_offset = 14;
  520. const size_t cap_size_offset = 10;
  521. uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
  522. size_t response_length = sizeof(response);
  523. uint32_t cap_size;
  524. uint32_t err;
  525. if (pack_byte_string(buf, sizeof(buf), "sdd",
  526. 0, command, sizeof(command),
  527. cap_area_offset, cap_area,
  528. sub_cap_offset, sub_cap))
  529. return TPM_LIB_ERROR;
  530. err = tpm_sendrecv_command(buf, response, &response_length);
  531. if (err)
  532. return err;
  533. if (unpack_byte_string(response, response_length, "d",
  534. cap_size_offset, &cap_size))
  535. return TPM_LIB_ERROR;
  536. if (cap_size > response_length || cap_size > count)
  537. return TPM_LIB_ERROR;
  538. if (unpack_byte_string(response, response_length, "s",
  539. cap_offset, cap, cap_size))
  540. return TPM_LIB_ERROR;
  541. return 0;
  542. }
  543. uint32_t tpm_get_permanent_flags(struct tpm_permanent_flags *pflags)
  544. {
  545. const uint8_t command[22] = {
  546. 0x0, 0xc1, /* TPM_TAG */
  547. 0x0, 0x0, 0x0, 0x16, /* parameter size */
  548. 0x0, 0x0, 0x0, 0x65, /* TPM_COMMAND_CODE */
  549. 0x0, 0x0, 0x0, 0x4, /* TPM_CAP_FLAG_PERM */
  550. 0x0, 0x0, 0x0, 0x4, /* subcap size */
  551. 0x0, 0x0, 0x1, 0x8, /* subcap value */
  552. };
  553. uint8_t response[COMMAND_BUFFER_SIZE];
  554. size_t response_length = sizeof(response);
  555. uint32_t err;
  556. err = tpm_sendrecv_command(command, response, &response_length);
  557. if (err)
  558. return err;
  559. memcpy(pflags, response + TPM_HEADER_SIZE, sizeof(*pflags));
  560. return 0;
  561. }
  562. uint32_t tpm_get_permissions(uint32_t index, uint32_t *perm)
  563. {
  564. const uint8_t command[22] = {
  565. 0x0, 0xc1, /* TPM_TAG */
  566. 0x0, 0x0, 0x0, 0x16, /* parameter size */
  567. 0x0, 0x0, 0x0, 0x65, /* TPM_COMMAND_CODE */
  568. 0x0, 0x0, 0x0, 0x11,
  569. 0x0, 0x0, 0x0, 0x4,
  570. };
  571. const size_t index_offset = 18;
  572. const size_t perm_offset = 60;
  573. uint8_t buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
  574. size_t response_length = sizeof(response);
  575. uint32_t err;
  576. if (pack_byte_string(buf, sizeof(buf), "d", 0, command, sizeof(command),
  577. index_offset, index))
  578. return TPM_LIB_ERROR;
  579. err = tpm_sendrecv_command(buf, response, &response_length);
  580. if (err)
  581. return err;
  582. if (unpack_byte_string(response, response_length, "d",
  583. perm_offset, perm))
  584. return TPM_LIB_ERROR;
  585. return 0;
  586. }
  587. #ifdef CONFIG_TPM_AUTH_SESSIONS
  588. /**
  589. * Fill an authentication block in a request.
  590. * This func can create the first as well as the second auth block (for
  591. * double authorized commands).
  592. *
  593. * @param request pointer to the request (w/ uninitialised auth data)
  594. * @param request_len0 length of the request without auth data
  595. * @param handles_len length of the handles area in request
  596. * @param auth_session pointer to the (valid) auth session to be used
  597. * @param request_auth pointer to the auth block of the request to be filled
  598. * @param auth authentication data (HMAC key)
  599. */
  600. static uint32_t create_request_auth(const void *request, size_t request_len0,
  601. size_t handles_len,
  602. struct session_data *auth_session,
  603. void *request_auth, const void *auth)
  604. {
  605. uint8_t hmac_data[DIGEST_LENGTH * 3 + 1];
  606. sha1_context hash_ctx;
  607. const size_t command_code_offset = 6;
  608. const size_t auth_nonce_odd_offset = 4;
  609. const size_t auth_continue_offset = 24;
  610. const size_t auth_auth_offset = 25;
  611. if (!auth_session || !auth_session->valid)
  612. return TPM_LIB_ERROR;
  613. sha1_starts(&hash_ctx);
  614. sha1_update(&hash_ctx, request + command_code_offset, 4);
  615. if (request_len0 > TPM_REQUEST_HEADER_LENGTH + handles_len)
  616. sha1_update(&hash_ctx,
  617. request + TPM_REQUEST_HEADER_LENGTH + handles_len,
  618. request_len0 - TPM_REQUEST_HEADER_LENGTH
  619. - handles_len);
  620. sha1_finish(&hash_ctx, hmac_data);
  621. sha1_starts(&hash_ctx);
  622. sha1_update(&hash_ctx, auth_session->nonce_odd, DIGEST_LENGTH);
  623. sha1_update(&hash_ctx, hmac_data, sizeof(hmac_data));
  624. sha1_finish(&hash_ctx, auth_session->nonce_odd);
  625. if (pack_byte_string(request_auth, TPM_REQUEST_AUTH_LENGTH, "dsb",
  626. 0, auth_session->handle,
  627. auth_nonce_odd_offset, auth_session->nonce_odd,
  628. DIGEST_LENGTH,
  629. auth_continue_offset, 1))
  630. return TPM_LIB_ERROR;
  631. if (pack_byte_string(hmac_data, sizeof(hmac_data), "ss",
  632. DIGEST_LENGTH,
  633. auth_session->nonce_even,
  634. DIGEST_LENGTH,
  635. 2 * DIGEST_LENGTH,
  636. request_auth + auth_nonce_odd_offset,
  637. DIGEST_LENGTH + 1))
  638. return TPM_LIB_ERROR;
  639. sha1_hmac(auth, DIGEST_LENGTH, hmac_data, sizeof(hmac_data),
  640. request_auth + auth_auth_offset);
  641. return TPM_SUCCESS;
  642. }
  643. /**
  644. * Verify an authentication block in a response.
  645. * Since this func updates the nonce_even in the session data it has to be
  646. * called when receiving a succesfull AUTH response.
  647. * This func can verify the first as well as the second auth block (for
  648. * double authorized commands).
  649. *
  650. * @param command_code command code of the request
  651. * @param response pointer to the request (w/ uninitialised auth data)
  652. * @param handles_len length of the handles area in response
  653. * @param auth_session pointer to the (valid) auth session to be used
  654. * @param response_auth pointer to the auth block of the response to be verified
  655. * @param auth authentication data (HMAC key)
  656. */
  657. static uint32_t verify_response_auth(uint32_t command_code,
  658. const void *response, size_t response_len0,
  659. size_t handles_len,
  660. struct session_data *auth_session,
  661. const void *response_auth, const void *auth)
  662. {
  663. uint8_t hmac_data[DIGEST_LENGTH * 3 + 1];
  664. uint8_t computed_auth[DIGEST_LENGTH];
  665. sha1_context hash_ctx;
  666. const size_t return_code_offset = 6;
  667. const size_t auth_continue_offset = 20;
  668. const size_t auth_auth_offset = 21;
  669. uint8_t auth_continue;
  670. if (!auth_session || !auth_session->valid)
  671. return TPM_AUTHFAIL;
  672. if (pack_byte_string(hmac_data, sizeof(hmac_data), "d",
  673. 0, command_code))
  674. return TPM_LIB_ERROR;
  675. if (response_len0 < TPM_RESPONSE_HEADER_LENGTH)
  676. return TPM_LIB_ERROR;
  677. sha1_starts(&hash_ctx);
  678. sha1_update(&hash_ctx, response + return_code_offset, 4);
  679. sha1_update(&hash_ctx, hmac_data, 4);
  680. if (response_len0 > TPM_RESPONSE_HEADER_LENGTH + handles_len)
  681. sha1_update(&hash_ctx,
  682. response + TPM_RESPONSE_HEADER_LENGTH + handles_len,
  683. response_len0 - TPM_RESPONSE_HEADER_LENGTH
  684. - handles_len);
  685. sha1_finish(&hash_ctx, hmac_data);
  686. memcpy(auth_session->nonce_even, response_auth, DIGEST_LENGTH);
  687. auth_continue = ((uint8_t *)response_auth)[auth_continue_offset];
  688. if (pack_byte_string(hmac_data, sizeof(hmac_data), "ssb",
  689. DIGEST_LENGTH,
  690. response_auth,
  691. DIGEST_LENGTH,
  692. 2 * DIGEST_LENGTH,
  693. auth_session->nonce_odd,
  694. DIGEST_LENGTH,
  695. 3 * DIGEST_LENGTH,
  696. auth_continue))
  697. return TPM_LIB_ERROR;
  698. sha1_hmac(auth, DIGEST_LENGTH, hmac_data, sizeof(hmac_data),
  699. computed_auth);
  700. if (memcmp(computed_auth, response_auth + auth_auth_offset,
  701. DIGEST_LENGTH))
  702. return TPM_AUTHFAIL;
  703. return TPM_SUCCESS;
  704. }
  705. uint32_t tpm_terminate_auth_session(uint32_t auth_handle)
  706. {
  707. const uint8_t command[18] = {
  708. 0x00, 0xc1, /* TPM_TAG */
  709. 0x00, 0x00, 0x00, 0x00, /* parameter size */
  710. 0x00, 0x00, 0x00, 0xba, /* TPM_COMMAND_CODE */
  711. 0x00, 0x00, 0x00, 0x00, /* TPM_HANDLE */
  712. 0x00, 0x00, 0x00, 0x02, /* TPM_RESSOURCE_TYPE */
  713. };
  714. const size_t req_handle_offset = TPM_REQUEST_HEADER_LENGTH;
  715. uint8_t request[COMMAND_BUFFER_SIZE];
  716. if (pack_byte_string(request, sizeof(request), "sd",
  717. 0, command, sizeof(command),
  718. req_handle_offset, auth_handle))
  719. return TPM_LIB_ERROR;
  720. if (oiap_session.valid && oiap_session.handle == auth_handle)
  721. oiap_session.valid = 0;
  722. return tpm_sendrecv_command(request, NULL, NULL);
  723. }
  724. uint32_t tpm_end_oiap(void)
  725. {
  726. uint32_t err = TPM_SUCCESS;
  727. if (oiap_session.valid)
  728. err = tpm_terminate_auth_session(oiap_session.handle);
  729. return err;
  730. }
  731. uint32_t tpm_oiap(uint32_t *auth_handle)
  732. {
  733. const uint8_t command[10] = {
  734. 0x00, 0xc1, /* TPM_TAG */
  735. 0x00, 0x00, 0x00, 0x0a, /* parameter size */
  736. 0x00, 0x00, 0x00, 0x0a, /* TPM_COMMAND_CODE */
  737. };
  738. const size_t res_auth_handle_offset = TPM_RESPONSE_HEADER_LENGTH;
  739. const size_t res_nonce_even_offset = TPM_RESPONSE_HEADER_LENGTH + 4;
  740. uint8_t response[COMMAND_BUFFER_SIZE];
  741. size_t response_length = sizeof(response);
  742. uint32_t err;
  743. if (oiap_session.valid)
  744. tpm_terminate_auth_session(oiap_session.handle);
  745. err = tpm_sendrecv_command(command, response, &response_length);
  746. if (err)
  747. return err;
  748. if (unpack_byte_string(response, response_length, "ds",
  749. res_auth_handle_offset, &oiap_session.handle,
  750. res_nonce_even_offset, &oiap_session.nonce_even,
  751. (uint32_t)DIGEST_LENGTH))
  752. return TPM_LIB_ERROR;
  753. oiap_session.valid = 1;
  754. if (auth_handle)
  755. *auth_handle = oiap_session.handle;
  756. return 0;
  757. }
  758. uint32_t tpm_load_key2_oiap(uint32_t parent_handle,
  759. const void *key, size_t key_length,
  760. const void *parent_key_usage_auth,
  761. uint32_t *key_handle)
  762. {
  763. const uint8_t command[14] = {
  764. 0x00, 0xc2, /* TPM_TAG */
  765. 0x00, 0x00, 0x00, 0x00, /* parameter size */
  766. 0x00, 0x00, 0x00, 0x41, /* TPM_COMMAND_CODE */
  767. 0x00, 0x00, 0x00, 0x00, /* parent handle */
  768. };
  769. const size_t req_size_offset = 2;
  770. const size_t req_parent_handle_offset = TPM_REQUEST_HEADER_LENGTH;
  771. const size_t req_key_offset = TPM_REQUEST_HEADER_LENGTH + 4;
  772. const size_t res_handle_offset = TPM_RESPONSE_HEADER_LENGTH;
  773. uint8_t request[sizeof(command) + TPM_KEY12_MAX_LENGTH
  774. + TPM_REQUEST_AUTH_LENGTH];
  775. uint8_t response[COMMAND_BUFFER_SIZE];
  776. size_t response_length = sizeof(response);
  777. uint32_t err;
  778. if (!oiap_session.valid) {
  779. err = tpm_oiap(NULL);
  780. if (err)
  781. return err;
  782. }
  783. if (pack_byte_string(request, sizeof(request), "sdds",
  784. 0, command, sizeof(command),
  785. req_size_offset,
  786. sizeof(command) + key_length
  787. + TPM_REQUEST_AUTH_LENGTH,
  788. req_parent_handle_offset, parent_handle,
  789. req_key_offset, key, key_length
  790. ))
  791. return TPM_LIB_ERROR;
  792. err = create_request_auth(request, sizeof(command) + key_length, 4,
  793. &oiap_session,
  794. request + sizeof(command) + key_length,
  795. parent_key_usage_auth);
  796. if (err)
  797. return err;
  798. err = tpm_sendrecv_command(request, response, &response_length);
  799. if (err) {
  800. if (err == TPM_AUTHFAIL)
  801. oiap_session.valid = 0;
  802. return err;
  803. }
  804. err = verify_response_auth(0x00000041, response,
  805. response_length - TPM_RESPONSE_AUTH_LENGTH,
  806. 4, &oiap_session,
  807. response + response_length - TPM_RESPONSE_AUTH_LENGTH,
  808. parent_key_usage_auth);
  809. if (err)
  810. return err;
  811. if (key_handle) {
  812. if (unpack_byte_string(response, response_length, "d",
  813. res_handle_offset, key_handle))
  814. return TPM_LIB_ERROR;
  815. }
  816. return 0;
  817. }
  818. uint32_t tpm_get_pub_key_oiap(uint32_t key_handle, const void *usage_auth,
  819. void *pubkey, size_t *pubkey_len)
  820. {
  821. const uint8_t command[14] = {
  822. 0x00, 0xc2, /* TPM_TAG */
  823. 0x00, 0x00, 0x00, 0x00, /* parameter size */
  824. 0x00, 0x00, 0x00, 0x21, /* TPM_COMMAND_CODE */
  825. 0x00, 0x00, 0x00, 0x00, /* key handle */
  826. };
  827. const size_t req_size_offset = 2;
  828. const size_t req_key_handle_offset = TPM_REQUEST_HEADER_LENGTH;
  829. const size_t res_pubkey_offset = TPM_RESPONSE_HEADER_LENGTH;
  830. uint8_t request[sizeof(command) + TPM_REQUEST_AUTH_LENGTH];
  831. uint8_t response[TPM_RESPONSE_HEADER_LENGTH + TPM_PUBKEY_MAX_LENGTH
  832. + TPM_RESPONSE_AUTH_LENGTH];
  833. size_t response_length = sizeof(response);
  834. uint32_t err;
  835. if (!oiap_session.valid) {
  836. err = tpm_oiap(NULL);
  837. if (err)
  838. return err;
  839. }
  840. if (pack_byte_string(request, sizeof(request), "sdd",
  841. 0, command, sizeof(command),
  842. req_size_offset,
  843. (uint32_t)(sizeof(command)
  844. + TPM_REQUEST_AUTH_LENGTH),
  845. req_key_handle_offset, key_handle
  846. ))
  847. return TPM_LIB_ERROR;
  848. err = create_request_auth(request, sizeof(command), 4, &oiap_session,
  849. request + sizeof(command), usage_auth);
  850. if (err)
  851. return err;
  852. err = tpm_sendrecv_command(request, response, &response_length);
  853. if (err) {
  854. if (err == TPM_AUTHFAIL)
  855. oiap_session.valid = 0;
  856. return err;
  857. }
  858. err = verify_response_auth(0x00000021, response,
  859. response_length - TPM_RESPONSE_AUTH_LENGTH,
  860. 0, &oiap_session,
  861. response + response_length - TPM_RESPONSE_AUTH_LENGTH,
  862. usage_auth);
  863. if (err)
  864. return err;
  865. if (pubkey) {
  866. if ((response_length - TPM_RESPONSE_HEADER_LENGTH
  867. - TPM_RESPONSE_AUTH_LENGTH) > *pubkey_len)
  868. return TPM_LIB_ERROR;
  869. *pubkey_len = response_length - TPM_RESPONSE_HEADER_LENGTH
  870. - TPM_RESPONSE_AUTH_LENGTH;
  871. memcpy(pubkey, response + res_pubkey_offset,
  872. response_length - TPM_RESPONSE_HEADER_LENGTH
  873. - TPM_RESPONSE_AUTH_LENGTH);
  874. }
  875. return 0;
  876. }
  877. #endif /* CONFIG_TPM_AUTH_SESSIONS */