tpm-v1.c 26 KB

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