tpm.c 25 KB

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