tpm-v1.c 24 KB

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