tpm.c 28 KB

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