tpm.c 25 KB

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