tpm-v1.c 26 KB

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