tpm-v1.c 25 KB

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