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