tpm-v1.c 26 KB

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