tpm.c 25 KB

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