tpm2-cmd.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /*
  2. * Copyright (C) 2014, 2015 Intel Corporation
  3. *
  4. * Authors:
  5. * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
  6. *
  7. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  8. *
  9. * This file contains TPM2 protocol implementations of the commands
  10. * used by the kernel internally.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; version 2
  15. * of the License.
  16. */
  17. #include "tpm.h"
  18. #include <crypto/hash_info.h>
  19. #include <keys/trusted-type.h>
  20. enum tpm2_object_attributes {
  21. TPM2_OA_USER_WITH_AUTH = BIT(6),
  22. };
  23. enum tpm2_session_attributes {
  24. TPM2_SA_CONTINUE_SESSION = BIT(0),
  25. };
  26. struct tpm2_startup_in {
  27. __be16 startup_type;
  28. } __packed;
  29. struct tpm2_self_test_in {
  30. u8 full_test;
  31. } __packed;
  32. struct tpm2_pcr_read_in {
  33. __be32 pcr_selects_cnt;
  34. __be16 hash_alg;
  35. u8 pcr_select_size;
  36. u8 pcr_select[TPM2_PCR_SELECT_MIN];
  37. } __packed;
  38. struct tpm2_pcr_read_out {
  39. __be32 update_cnt;
  40. __be32 pcr_selects_cnt;
  41. __be16 hash_alg;
  42. u8 pcr_select_size;
  43. u8 pcr_select[TPM2_PCR_SELECT_MIN];
  44. __be32 digests_cnt;
  45. __be16 digest_size;
  46. u8 digest[TPM_DIGEST_SIZE];
  47. } __packed;
  48. struct tpm2_get_tpm_pt_in {
  49. __be32 cap_id;
  50. __be32 property_id;
  51. __be32 property_cnt;
  52. } __packed;
  53. struct tpm2_get_tpm_pt_out {
  54. u8 more_data;
  55. __be32 subcap_id;
  56. __be32 property_cnt;
  57. __be32 property_id;
  58. __be32 value;
  59. } __packed;
  60. struct tpm2_get_random_in {
  61. __be16 size;
  62. } __packed;
  63. struct tpm2_get_random_out {
  64. __be16 size;
  65. u8 buffer[TPM_MAX_RNG_DATA];
  66. } __packed;
  67. union tpm2_cmd_params {
  68. struct tpm2_startup_in startup_in;
  69. struct tpm2_self_test_in selftest_in;
  70. struct tpm2_pcr_read_in pcrread_in;
  71. struct tpm2_pcr_read_out pcrread_out;
  72. struct tpm2_get_tpm_pt_in get_tpm_pt_in;
  73. struct tpm2_get_tpm_pt_out get_tpm_pt_out;
  74. struct tpm2_get_random_in getrandom_in;
  75. struct tpm2_get_random_out getrandom_out;
  76. };
  77. struct tpm2_cmd {
  78. tpm_cmd_header header;
  79. union tpm2_cmd_params params;
  80. } __packed;
  81. struct tpm2_hash {
  82. unsigned int crypto_id;
  83. unsigned int tpm_id;
  84. };
  85. static struct tpm2_hash tpm2_hash_map[] = {
  86. {HASH_ALGO_SHA1, TPM2_ALG_SHA1},
  87. {HASH_ALGO_SHA256, TPM2_ALG_SHA256},
  88. {HASH_ALGO_SHA384, TPM2_ALG_SHA384},
  89. {HASH_ALGO_SHA512, TPM2_ALG_SHA512},
  90. {HASH_ALGO_SM3_256, TPM2_ALG_SM3_256},
  91. };
  92. /*
  93. * Array with one entry per ordinal defining the maximum amount
  94. * of time the chip could take to return the result. The values
  95. * of the SHORT, MEDIUM, and LONG durations are taken from the
  96. * PC Client Profile (PTP) specification.
  97. */
  98. static const u8 tpm2_ordinal_duration[TPM2_CC_LAST - TPM2_CC_FIRST + 1] = {
  99. TPM_UNDEFINED, /* 11F */
  100. TPM_UNDEFINED, /* 120 */
  101. TPM_LONG, /* 121 */
  102. TPM_UNDEFINED, /* 122 */
  103. TPM_UNDEFINED, /* 123 */
  104. TPM_UNDEFINED, /* 124 */
  105. TPM_UNDEFINED, /* 125 */
  106. TPM_UNDEFINED, /* 126 */
  107. TPM_UNDEFINED, /* 127 */
  108. TPM_UNDEFINED, /* 128 */
  109. TPM_LONG, /* 129 */
  110. TPM_UNDEFINED, /* 12a */
  111. TPM_UNDEFINED, /* 12b */
  112. TPM_UNDEFINED, /* 12c */
  113. TPM_UNDEFINED, /* 12d */
  114. TPM_UNDEFINED, /* 12e */
  115. TPM_UNDEFINED, /* 12f */
  116. TPM_UNDEFINED, /* 130 */
  117. TPM_UNDEFINED, /* 131 */
  118. TPM_UNDEFINED, /* 132 */
  119. TPM_UNDEFINED, /* 133 */
  120. TPM_UNDEFINED, /* 134 */
  121. TPM_UNDEFINED, /* 135 */
  122. TPM_UNDEFINED, /* 136 */
  123. TPM_UNDEFINED, /* 137 */
  124. TPM_UNDEFINED, /* 138 */
  125. TPM_UNDEFINED, /* 139 */
  126. TPM_UNDEFINED, /* 13a */
  127. TPM_UNDEFINED, /* 13b */
  128. TPM_UNDEFINED, /* 13c */
  129. TPM_UNDEFINED, /* 13d */
  130. TPM_MEDIUM, /* 13e */
  131. TPM_UNDEFINED, /* 13f */
  132. TPM_UNDEFINED, /* 140 */
  133. TPM_UNDEFINED, /* 141 */
  134. TPM_UNDEFINED, /* 142 */
  135. TPM_LONG, /* 143 */
  136. TPM_MEDIUM, /* 144 */
  137. TPM_UNDEFINED, /* 145 */
  138. TPM_UNDEFINED, /* 146 */
  139. TPM_UNDEFINED, /* 147 */
  140. TPM_UNDEFINED, /* 148 */
  141. TPM_UNDEFINED, /* 149 */
  142. TPM_UNDEFINED, /* 14a */
  143. TPM_UNDEFINED, /* 14b */
  144. TPM_UNDEFINED, /* 14c */
  145. TPM_UNDEFINED, /* 14d */
  146. TPM_LONG, /* 14e */
  147. TPM_UNDEFINED, /* 14f */
  148. TPM_UNDEFINED, /* 150 */
  149. TPM_UNDEFINED, /* 151 */
  150. TPM_UNDEFINED, /* 152 */
  151. TPM_UNDEFINED, /* 153 */
  152. TPM_UNDEFINED, /* 154 */
  153. TPM_UNDEFINED, /* 155 */
  154. TPM_UNDEFINED, /* 156 */
  155. TPM_UNDEFINED, /* 157 */
  156. TPM_UNDEFINED, /* 158 */
  157. TPM_UNDEFINED, /* 159 */
  158. TPM_UNDEFINED, /* 15a */
  159. TPM_UNDEFINED, /* 15b */
  160. TPM_MEDIUM, /* 15c */
  161. TPM_UNDEFINED, /* 15d */
  162. TPM_UNDEFINED, /* 15e */
  163. TPM_UNDEFINED, /* 15f */
  164. TPM_UNDEFINED, /* 160 */
  165. TPM_UNDEFINED, /* 161 */
  166. TPM_UNDEFINED, /* 162 */
  167. TPM_UNDEFINED, /* 163 */
  168. TPM_UNDEFINED, /* 164 */
  169. TPM_UNDEFINED, /* 165 */
  170. TPM_UNDEFINED, /* 166 */
  171. TPM_UNDEFINED, /* 167 */
  172. TPM_UNDEFINED, /* 168 */
  173. TPM_UNDEFINED, /* 169 */
  174. TPM_UNDEFINED, /* 16a */
  175. TPM_UNDEFINED, /* 16b */
  176. TPM_UNDEFINED, /* 16c */
  177. TPM_UNDEFINED, /* 16d */
  178. TPM_UNDEFINED, /* 16e */
  179. TPM_UNDEFINED, /* 16f */
  180. TPM_UNDEFINED, /* 170 */
  181. TPM_UNDEFINED, /* 171 */
  182. TPM_UNDEFINED, /* 172 */
  183. TPM_UNDEFINED, /* 173 */
  184. TPM_UNDEFINED, /* 174 */
  185. TPM_UNDEFINED, /* 175 */
  186. TPM_UNDEFINED, /* 176 */
  187. TPM_LONG, /* 177 */
  188. TPM_UNDEFINED, /* 178 */
  189. TPM_UNDEFINED, /* 179 */
  190. TPM_MEDIUM, /* 17a */
  191. TPM_LONG, /* 17b */
  192. TPM_UNDEFINED, /* 17c */
  193. TPM_UNDEFINED, /* 17d */
  194. TPM_UNDEFINED, /* 17e */
  195. TPM_UNDEFINED, /* 17f */
  196. TPM_UNDEFINED, /* 180 */
  197. TPM_UNDEFINED, /* 181 */
  198. TPM_MEDIUM, /* 182 */
  199. TPM_UNDEFINED, /* 183 */
  200. TPM_UNDEFINED, /* 184 */
  201. TPM_MEDIUM, /* 185 */
  202. TPM_MEDIUM, /* 186 */
  203. TPM_UNDEFINED, /* 187 */
  204. TPM_UNDEFINED, /* 188 */
  205. TPM_UNDEFINED, /* 189 */
  206. TPM_UNDEFINED, /* 18a */
  207. TPM_UNDEFINED, /* 18b */
  208. TPM_UNDEFINED, /* 18c */
  209. TPM_UNDEFINED, /* 18d */
  210. TPM_UNDEFINED, /* 18e */
  211. TPM_UNDEFINED /* 18f */
  212. };
  213. #define TPM2_PCR_READ_IN_SIZE \
  214. (sizeof(struct tpm_input_header) + \
  215. sizeof(struct tpm2_pcr_read_in))
  216. #define TPM2_PCR_READ_RESP_BODY_SIZE \
  217. sizeof(struct tpm2_pcr_read_out)
  218. static const struct tpm_input_header tpm2_pcrread_header = {
  219. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  220. .length = cpu_to_be32(TPM2_PCR_READ_IN_SIZE),
  221. .ordinal = cpu_to_be32(TPM2_CC_PCR_READ)
  222. };
  223. /**
  224. * tpm2_pcr_read() - read a PCR value
  225. * @chip: TPM chip to use.
  226. * @pcr_idx: index of the PCR to read.
  227. * @res_buf: buffer to store the resulting hash.
  228. *
  229. * Return: Same as with tpm_transmit_cmd.
  230. */
  231. int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  232. {
  233. int rc;
  234. struct tpm2_cmd cmd;
  235. u8 *buf;
  236. if (pcr_idx >= TPM2_PLATFORM_PCR)
  237. return -EINVAL;
  238. cmd.header.in = tpm2_pcrread_header;
  239. cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
  240. cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
  241. cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
  242. memset(cmd.params.pcrread_in.pcr_select, 0,
  243. sizeof(cmd.params.pcrread_in.pcr_select));
  244. cmd.params.pcrread_in.pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7);
  245. rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd),
  246. TPM2_PCR_READ_RESP_BODY_SIZE,
  247. 0, "attempting to read a pcr value");
  248. if (rc == 0) {
  249. buf = cmd.params.pcrread_out.digest;
  250. memcpy(res_buf, buf, TPM_DIGEST_SIZE);
  251. }
  252. return rc;
  253. }
  254. struct tpm2_null_auth_area {
  255. __be32 handle;
  256. __be16 nonce_size;
  257. u8 attributes;
  258. __be16 auth_size;
  259. } __packed;
  260. /**
  261. * tpm2_pcr_extend() - extend a PCR value
  262. *
  263. * @chip: TPM chip to use.
  264. * @pcr_idx: index of the PCR.
  265. * @count: number of digests passed.
  266. * @digests: list of pcr banks and corresponding digest values to extend.
  267. *
  268. * Return: Same as with tpm_transmit_cmd.
  269. */
  270. int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
  271. struct tpm2_digest *digests)
  272. {
  273. struct tpm_buf buf;
  274. struct tpm2_null_auth_area auth_area;
  275. int rc;
  276. int i;
  277. int j;
  278. if (count > ARRAY_SIZE(chip->active_banks))
  279. return -EINVAL;
  280. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_EXTEND);
  281. if (rc)
  282. return rc;
  283. tpm_buf_append_u32(&buf, pcr_idx);
  284. auth_area.handle = cpu_to_be32(TPM2_RS_PW);
  285. auth_area.nonce_size = 0;
  286. auth_area.attributes = 0;
  287. auth_area.auth_size = 0;
  288. tpm_buf_append_u32(&buf, sizeof(struct tpm2_null_auth_area));
  289. tpm_buf_append(&buf, (const unsigned char *)&auth_area,
  290. sizeof(auth_area));
  291. tpm_buf_append_u32(&buf, count);
  292. for (i = 0; i < count; i++) {
  293. for (j = 0; j < ARRAY_SIZE(tpm2_hash_map); j++) {
  294. if (digests[i].alg_id != tpm2_hash_map[j].tpm_id)
  295. continue;
  296. tpm_buf_append_u16(&buf, digests[i].alg_id);
  297. tpm_buf_append(&buf, (const unsigned char
  298. *)&digests[i].digest,
  299. hash_digest_size[tpm2_hash_map[j].crypto_id]);
  300. }
  301. }
  302. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
  303. "attempting extend a PCR value");
  304. tpm_buf_destroy(&buf);
  305. return rc;
  306. }
  307. #define TPM2_GETRANDOM_IN_SIZE \
  308. (sizeof(struct tpm_input_header) + \
  309. sizeof(struct tpm2_get_random_in))
  310. static const struct tpm_input_header tpm2_getrandom_header = {
  311. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  312. .length = cpu_to_be32(TPM2_GETRANDOM_IN_SIZE),
  313. .ordinal = cpu_to_be32(TPM2_CC_GET_RANDOM)
  314. };
  315. /**
  316. * tpm2_get_random() - get random bytes from the TPM RNG
  317. *
  318. * @chip: TPM chip to use
  319. * @out: destination buffer for the random bytes
  320. * @max: the max number of bytes to write to @out
  321. *
  322. * Return:
  323. * Size of the output buffer, or -EIO on error.
  324. */
  325. int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)
  326. {
  327. struct tpm2_cmd cmd;
  328. u32 recd, rlength;
  329. u32 num_bytes;
  330. int err;
  331. int total = 0;
  332. int retries = 5;
  333. u8 *dest = out;
  334. num_bytes = min_t(u32, max, sizeof(cmd.params.getrandom_out.buffer));
  335. if (!out || !num_bytes ||
  336. max > sizeof(cmd.params.getrandom_out.buffer))
  337. return -EINVAL;
  338. do {
  339. cmd.header.in = tpm2_getrandom_header;
  340. cmd.params.getrandom_in.size = cpu_to_be16(num_bytes);
  341. err = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd),
  342. offsetof(struct tpm2_get_random_out,
  343. buffer),
  344. 0, "attempting get random");
  345. if (err)
  346. break;
  347. recd = min_t(u32, be16_to_cpu(cmd.params.getrandom_out.size),
  348. num_bytes);
  349. rlength = be32_to_cpu(cmd.header.out.length);
  350. if (rlength < offsetof(struct tpm2_get_random_out, buffer) +
  351. recd)
  352. return -EFAULT;
  353. memcpy(dest, cmd.params.getrandom_out.buffer, recd);
  354. dest += recd;
  355. total += recd;
  356. num_bytes -= recd;
  357. } while (retries-- && total < max);
  358. return total ? total : -EIO;
  359. }
  360. #define TPM2_GET_TPM_PT_IN_SIZE \
  361. (sizeof(struct tpm_input_header) + \
  362. sizeof(struct tpm2_get_tpm_pt_in))
  363. #define TPM2_GET_TPM_PT_OUT_BODY_SIZE \
  364. sizeof(struct tpm2_get_tpm_pt_out)
  365. static const struct tpm_input_header tpm2_get_tpm_pt_header = {
  366. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  367. .length = cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE),
  368. .ordinal = cpu_to_be32(TPM2_CC_GET_CAPABILITY)
  369. };
  370. /**
  371. * tpm2_flush_context_cmd() - execute a TPM2_FlushContext command
  372. * @chip: TPM chip to use
  373. * @payload: the key data in clear and encrypted form
  374. * @options: authentication values and other options
  375. *
  376. * Return: same as with tpm_transmit_cmd
  377. */
  378. void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
  379. unsigned int flags)
  380. {
  381. struct tpm_buf buf;
  382. int rc;
  383. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT);
  384. if (rc) {
  385. dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
  386. handle);
  387. return;
  388. }
  389. tpm_buf_append_u32(&buf, handle);
  390. (void) tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, flags,
  391. "flushing context");
  392. tpm_buf_destroy(&buf);
  393. }
  394. /**
  395. * tpm_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
  396. *
  397. * @buf: an allocated tpm_buf instance
  398. * @session_handle: session handle
  399. * @nonce: the session nonce, may be NULL if not used
  400. * @nonce_len: the session nonce length, may be 0 if not used
  401. * @attributes: the session attributes
  402. * @hmac: the session HMAC or password, may be NULL if not used
  403. * @hmac_len: the session HMAC or password length, maybe 0 if not used
  404. */
  405. static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,
  406. const u8 *nonce, u16 nonce_len,
  407. u8 attributes,
  408. const u8 *hmac, u16 hmac_len)
  409. {
  410. tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);
  411. tpm_buf_append_u32(buf, session_handle);
  412. tpm_buf_append_u16(buf, nonce_len);
  413. if (nonce && nonce_len)
  414. tpm_buf_append(buf, nonce, nonce_len);
  415. tpm_buf_append_u8(buf, attributes);
  416. tpm_buf_append_u16(buf, hmac_len);
  417. if (hmac && hmac_len)
  418. tpm_buf_append(buf, hmac, hmac_len);
  419. }
  420. /**
  421. * tpm2_seal_trusted() - seal the payload of a trusted key
  422. *
  423. * @chip: TPM chip to use
  424. * @payload: the key data in clear and encrypted form
  425. * @options: authentication values and other options
  426. *
  427. * Return: < 0 on error and 0 on success.
  428. */
  429. int tpm2_seal_trusted(struct tpm_chip *chip,
  430. struct trusted_key_payload *payload,
  431. struct trusted_key_options *options)
  432. {
  433. unsigned int blob_len;
  434. struct tpm_buf buf;
  435. u32 hash, rlength;
  436. int i;
  437. int rc;
  438. for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
  439. if (options->hash == tpm2_hash_map[i].crypto_id) {
  440. hash = tpm2_hash_map[i].tpm_id;
  441. break;
  442. }
  443. }
  444. if (i == ARRAY_SIZE(tpm2_hash_map))
  445. return -EINVAL;
  446. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
  447. if (rc)
  448. return rc;
  449. tpm_buf_append_u32(&buf, options->keyhandle);
  450. tpm2_buf_append_auth(&buf, TPM2_RS_PW,
  451. NULL /* nonce */, 0,
  452. 0 /* session_attributes */,
  453. options->keyauth /* hmac */,
  454. TPM_DIGEST_SIZE);
  455. /* sensitive */
  456. tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1);
  457. tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE);
  458. tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE);
  459. tpm_buf_append_u16(&buf, payload->key_len + 1);
  460. tpm_buf_append(&buf, payload->key, payload->key_len);
  461. tpm_buf_append_u8(&buf, payload->migratable);
  462. /* public */
  463. tpm_buf_append_u16(&buf, 14 + options->policydigest_len);
  464. tpm_buf_append_u16(&buf, TPM2_ALG_KEYEDHASH);
  465. tpm_buf_append_u16(&buf, hash);
  466. /* policy */
  467. if (options->policydigest_len) {
  468. tpm_buf_append_u32(&buf, 0);
  469. tpm_buf_append_u16(&buf, options->policydigest_len);
  470. tpm_buf_append(&buf, options->policydigest,
  471. options->policydigest_len);
  472. } else {
  473. tpm_buf_append_u32(&buf, TPM2_OA_USER_WITH_AUTH);
  474. tpm_buf_append_u16(&buf, 0);
  475. }
  476. /* public parameters */
  477. tpm_buf_append_u16(&buf, TPM2_ALG_NULL);
  478. tpm_buf_append_u16(&buf, 0);
  479. /* outside info */
  480. tpm_buf_append_u16(&buf, 0);
  481. /* creation PCR */
  482. tpm_buf_append_u32(&buf, 0);
  483. if (buf.flags & TPM_BUF_OVERFLOW) {
  484. rc = -E2BIG;
  485. goto out;
  486. }
  487. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, 0,
  488. "sealing data");
  489. if (rc)
  490. goto out;
  491. blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]);
  492. if (blob_len > MAX_BLOB_SIZE) {
  493. rc = -E2BIG;
  494. goto out;
  495. }
  496. rlength = be32_to_cpu(((struct tpm2_cmd *)&buf)->header.out.length);
  497. if (rlength < TPM_HEADER_SIZE + 4 + blob_len) {
  498. rc = -EFAULT;
  499. goto out;
  500. }
  501. memcpy(payload->blob, &buf.data[TPM_HEADER_SIZE + 4], blob_len);
  502. payload->blob_len = blob_len;
  503. out:
  504. tpm_buf_destroy(&buf);
  505. if (rc > 0) {
  506. if (tpm2_rc_value(rc) == TPM2_RC_HASH)
  507. rc = -EINVAL;
  508. else
  509. rc = -EPERM;
  510. }
  511. return rc;
  512. }
  513. /**
  514. * tpm2_load_cmd() - execute a TPM2_Load command
  515. *
  516. * @chip: TPM chip to use
  517. * @payload: the key data in clear and encrypted form
  518. * @options: authentication values and other options
  519. * @blob_handle: returned blob handle
  520. * @flags: tpm transmit flags
  521. *
  522. * Return: 0 on success.
  523. * -E2BIG on wrong payload size.
  524. * -EPERM on tpm error status.
  525. * < 0 error from tpm_transmit_cmd.
  526. */
  527. static int tpm2_load_cmd(struct tpm_chip *chip,
  528. struct trusted_key_payload *payload,
  529. struct trusted_key_options *options,
  530. u32 *blob_handle, unsigned int flags)
  531. {
  532. struct tpm_buf buf;
  533. unsigned int private_len;
  534. unsigned int public_len;
  535. unsigned int blob_len;
  536. int rc;
  537. private_len = be16_to_cpup((__be16 *) &payload->blob[0]);
  538. if (private_len > (payload->blob_len - 2))
  539. return -E2BIG;
  540. public_len = be16_to_cpup((__be16 *) &payload->blob[2 + private_len]);
  541. blob_len = private_len + public_len + 4;
  542. if (blob_len > payload->blob_len)
  543. return -E2BIG;
  544. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
  545. if (rc)
  546. return rc;
  547. tpm_buf_append_u32(&buf, options->keyhandle);
  548. tpm2_buf_append_auth(&buf, TPM2_RS_PW,
  549. NULL /* nonce */, 0,
  550. 0 /* session_attributes */,
  551. options->keyauth /* hmac */,
  552. TPM_DIGEST_SIZE);
  553. tpm_buf_append(&buf, payload->blob, blob_len);
  554. if (buf.flags & TPM_BUF_OVERFLOW) {
  555. rc = -E2BIG;
  556. goto out;
  557. }
  558. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, flags,
  559. "loading blob");
  560. if (!rc)
  561. *blob_handle = be32_to_cpup(
  562. (__be32 *) &buf.data[TPM_HEADER_SIZE]);
  563. out:
  564. tpm_buf_destroy(&buf);
  565. if (rc > 0)
  566. rc = -EPERM;
  567. return rc;
  568. }
  569. /**
  570. * tpm2_unseal_cmd() - execute a TPM2_Unload command
  571. *
  572. * @chip: TPM chip to use
  573. * @payload: the key data in clear and encrypted form
  574. * @options: authentication values and other options
  575. * @blob_handle: blob handle
  576. * @flags: tpm_transmit_cmd flags
  577. *
  578. * Return: 0 on success
  579. * -EPERM on tpm error status
  580. * < 0 error from tpm_transmit_cmd
  581. */
  582. static int tpm2_unseal_cmd(struct tpm_chip *chip,
  583. struct trusted_key_payload *payload,
  584. struct trusted_key_options *options,
  585. u32 blob_handle, unsigned int flags)
  586. {
  587. struct tpm_buf buf;
  588. u16 data_len;
  589. u8 *data;
  590. int rc;
  591. u32 rlength;
  592. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
  593. if (rc)
  594. return rc;
  595. tpm_buf_append_u32(&buf, blob_handle);
  596. tpm2_buf_append_auth(&buf,
  597. options->policyhandle ?
  598. options->policyhandle : TPM2_RS_PW,
  599. NULL /* nonce */, 0,
  600. TPM2_SA_CONTINUE_SESSION,
  601. options->blobauth /* hmac */,
  602. TPM_DIGEST_SIZE);
  603. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 6, flags,
  604. "unsealing");
  605. if (rc > 0)
  606. rc = -EPERM;
  607. if (!rc) {
  608. data_len = be16_to_cpup(
  609. (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
  610. rlength = be32_to_cpu(((struct tpm2_cmd *)&buf)
  611. ->header.out.length);
  612. if (rlength < TPM_HEADER_SIZE + 6 + data_len) {
  613. rc = -EFAULT;
  614. goto out;
  615. }
  616. data = &buf.data[TPM_HEADER_SIZE + 6];
  617. memcpy(payload->key, data, data_len - 1);
  618. payload->key_len = data_len - 1;
  619. payload->migratable = data[data_len - 1];
  620. }
  621. out:
  622. tpm_buf_destroy(&buf);
  623. return rc;
  624. }
  625. /**
  626. * tpm2_unseal_trusted() - unseal the payload of a trusted key
  627. *
  628. * @chip: TPM chip to use
  629. * @payload: the key data in clear and encrypted form
  630. * @options: authentication values and other options
  631. *
  632. * Return: Same as with tpm_transmit_cmd.
  633. */
  634. int tpm2_unseal_trusted(struct tpm_chip *chip,
  635. struct trusted_key_payload *payload,
  636. struct trusted_key_options *options)
  637. {
  638. u32 blob_handle;
  639. int rc;
  640. mutex_lock(&chip->tpm_mutex);
  641. rc = tpm2_load_cmd(chip, payload, options, &blob_handle,
  642. TPM_TRANSMIT_UNLOCKED);
  643. if (rc)
  644. goto out;
  645. rc = tpm2_unseal_cmd(chip, payload, options, blob_handle,
  646. TPM_TRANSMIT_UNLOCKED);
  647. tpm2_flush_context_cmd(chip, blob_handle, TPM_TRANSMIT_UNLOCKED);
  648. out:
  649. mutex_unlock(&chip->tpm_mutex);
  650. return rc;
  651. }
  652. /**
  653. * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
  654. * @chip: TPM chip to use.
  655. * @property_id: property ID.
  656. * @value: output variable.
  657. * @desc: passed to tpm_transmit_cmd()
  658. *
  659. * Return: Same as with tpm_transmit_cmd.
  660. */
  661. ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
  662. const char *desc)
  663. {
  664. struct tpm2_cmd cmd;
  665. int rc;
  666. cmd.header.in = tpm2_get_tpm_pt_header;
  667. cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
  668. cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id);
  669. cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
  670. rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd),
  671. TPM2_GET_TPM_PT_OUT_BODY_SIZE, 0, desc);
  672. if (!rc)
  673. *value = be32_to_cpu(cmd.params.get_tpm_pt_out.value);
  674. return rc;
  675. }
  676. EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
  677. #define TPM2_STARTUP_IN_SIZE \
  678. (sizeof(struct tpm_input_header) + \
  679. sizeof(struct tpm2_startup_in))
  680. static const struct tpm_input_header tpm2_startup_header = {
  681. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  682. .length = cpu_to_be32(TPM2_STARTUP_IN_SIZE),
  683. .ordinal = cpu_to_be32(TPM2_CC_STARTUP)
  684. };
  685. /**
  686. * tpm2_startup() - send startup command to the TPM chip
  687. *
  688. * @chip: TPM chip to use.
  689. * @startup_type: startup type. The value is either
  690. * TPM_SU_CLEAR or TPM_SU_STATE.
  691. *
  692. * Return: Same as with tpm_transmit_cmd.
  693. */
  694. static int tpm2_startup(struct tpm_chip *chip, u16 startup_type)
  695. {
  696. struct tpm2_cmd cmd;
  697. cmd.header.in = tpm2_startup_header;
  698. cmd.params.startup_in.startup_type = cpu_to_be16(startup_type);
  699. return tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0,
  700. "attempting to start the TPM");
  701. }
  702. #define TPM2_SHUTDOWN_IN_SIZE \
  703. (sizeof(struct tpm_input_header) + \
  704. sizeof(struct tpm2_startup_in))
  705. static const struct tpm_input_header tpm2_shutdown_header = {
  706. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  707. .length = cpu_to_be32(TPM2_SHUTDOWN_IN_SIZE),
  708. .ordinal = cpu_to_be32(TPM2_CC_SHUTDOWN)
  709. };
  710. /**
  711. * tpm2_shutdown() - send shutdown command to the TPM chip
  712. *
  713. * @chip: TPM chip to use.
  714. * @shutdown_type: shutdown type. The value is either
  715. * TPM_SU_CLEAR or TPM_SU_STATE.
  716. */
  717. void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
  718. {
  719. struct tpm2_cmd cmd;
  720. int rc;
  721. cmd.header.in = tpm2_shutdown_header;
  722. cmd.params.startup_in.startup_type = cpu_to_be16(shutdown_type);
  723. rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0,
  724. "stopping the TPM");
  725. /* In places where shutdown command is sent there's no much we can do
  726. * except print the error code on a system failure.
  727. */
  728. if (rc < 0)
  729. dev_warn(&chip->dev, "transmit returned %d while stopping the TPM",
  730. rc);
  731. }
  732. /*
  733. * tpm2_calc_ordinal_duration() - maximum duration for a command
  734. *
  735. * @chip: TPM chip to use.
  736. * @ordinal: command code number.
  737. *
  738. * Return: maximum duration for a command
  739. */
  740. unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
  741. {
  742. int index = TPM_UNDEFINED;
  743. int duration = 0;
  744. if (ordinal >= TPM2_CC_FIRST && ordinal <= TPM2_CC_LAST)
  745. index = tpm2_ordinal_duration[ordinal - TPM2_CC_FIRST];
  746. if (index != TPM_UNDEFINED)
  747. duration = chip->duration[index];
  748. if (duration <= 0)
  749. duration = 2 * 60 * HZ;
  750. return duration;
  751. }
  752. EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration);
  753. #define TPM2_SELF_TEST_IN_SIZE \
  754. (sizeof(struct tpm_input_header) + \
  755. sizeof(struct tpm2_self_test_in))
  756. static const struct tpm_input_header tpm2_selftest_header = {
  757. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  758. .length = cpu_to_be32(TPM2_SELF_TEST_IN_SIZE),
  759. .ordinal = cpu_to_be32(TPM2_CC_SELF_TEST)
  760. };
  761. /**
  762. * tpm2_continue_selftest() - start a self test
  763. *
  764. * @chip: TPM chip to use
  765. * @full: test all commands instead of testing only those that were not
  766. * previously tested.
  767. *
  768. * Return: Same as with tpm_transmit_cmd with exception of RC_TESTING.
  769. */
  770. static int tpm2_start_selftest(struct tpm_chip *chip, bool full)
  771. {
  772. int rc;
  773. struct tpm2_cmd cmd;
  774. cmd.header.in = tpm2_selftest_header;
  775. cmd.params.selftest_in.full_test = full;
  776. rc = tpm_transmit_cmd(chip, NULL, &cmd, TPM2_SELF_TEST_IN_SIZE, 0, 0,
  777. "continue selftest");
  778. /* At least some prototype chips seem to give RC_TESTING error
  779. * immediately. This is a workaround for that.
  780. */
  781. if (rc == TPM2_RC_TESTING) {
  782. dev_warn(&chip->dev, "Got RC_TESTING, ignoring\n");
  783. rc = 0;
  784. }
  785. return rc;
  786. }
  787. /**
  788. * tpm2_do_selftest() - run a full self test
  789. *
  790. * @chip: TPM chip to use
  791. *
  792. * Return: Same as with tpm_transmit_cmd.
  793. *
  794. * During the self test TPM2 commands return with the error code RC_TESTING.
  795. * Waiting is done by issuing PCR read until it executes successfully.
  796. */
  797. static int tpm2_do_selftest(struct tpm_chip *chip)
  798. {
  799. int rc;
  800. unsigned int loops;
  801. unsigned int delay_msec = 100;
  802. unsigned long duration;
  803. struct tpm2_cmd cmd;
  804. int i;
  805. duration = tpm2_calc_ordinal_duration(chip, TPM2_CC_SELF_TEST);
  806. loops = jiffies_to_msecs(duration) / delay_msec;
  807. rc = tpm2_start_selftest(chip, true);
  808. if (rc)
  809. return rc;
  810. for (i = 0; i < loops; i++) {
  811. /* Attempt to read a PCR value */
  812. cmd.header.in = tpm2_pcrread_header;
  813. cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
  814. cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
  815. cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
  816. cmd.params.pcrread_in.pcr_select[0] = 0x01;
  817. cmd.params.pcrread_in.pcr_select[1] = 0x00;
  818. cmd.params.pcrread_in.pcr_select[2] = 0x00;
  819. rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0,
  820. NULL);
  821. if (rc < 0)
  822. break;
  823. rc = be32_to_cpu(cmd.header.out.return_code);
  824. if (rc != TPM2_RC_TESTING)
  825. break;
  826. msleep(delay_msec);
  827. }
  828. return rc;
  829. }
  830. /**
  831. * tpm2_probe() - probe TPM 2.0
  832. * @chip: TPM chip to use
  833. *
  834. * Return: < 0 error and 0 on success.
  835. *
  836. * Send idempotent TPM 2.0 command and see whether TPM 2.0 chip replied based on
  837. * the reply tag.
  838. */
  839. int tpm2_probe(struct tpm_chip *chip)
  840. {
  841. struct tpm2_cmd cmd;
  842. int rc;
  843. cmd.header.in = tpm2_get_tpm_pt_header;
  844. cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
  845. cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100);
  846. cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
  847. rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0, NULL);
  848. if (rc < 0)
  849. return rc;
  850. if (be16_to_cpu(cmd.header.out.tag) == TPM2_ST_NO_SESSIONS)
  851. chip->flags |= TPM_CHIP_FLAG_TPM2;
  852. return 0;
  853. }
  854. EXPORT_SYMBOL_GPL(tpm2_probe);
  855. struct tpm2_pcr_selection {
  856. __be16 hash_alg;
  857. u8 size_of_select;
  858. u8 pcr_select[3];
  859. } __packed;
  860. static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
  861. {
  862. struct tpm2_pcr_selection pcr_selection;
  863. struct tpm_buf buf;
  864. void *marker;
  865. void *end;
  866. void *pcr_select_offset;
  867. unsigned int count;
  868. u32 sizeof_pcr_selection;
  869. u32 rsp_len;
  870. int rc;
  871. int i = 0;
  872. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
  873. if (rc)
  874. return rc;
  875. tpm_buf_append_u32(&buf, TPM2_CAP_PCRS);
  876. tpm_buf_append_u32(&buf, 0);
  877. tpm_buf_append_u32(&buf, 1);
  878. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 9, 0,
  879. "get tpm pcr allocation");
  880. if (rc)
  881. goto out;
  882. count = be32_to_cpup(
  883. (__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);
  884. if (count > ARRAY_SIZE(chip->active_banks)) {
  885. rc = -ENODEV;
  886. goto out;
  887. }
  888. marker = &buf.data[TPM_HEADER_SIZE + 9];
  889. rsp_len = be32_to_cpup((__be32 *)&buf.data[2]);
  890. end = &buf.data[rsp_len];
  891. for (i = 0; i < count; i++) {
  892. pcr_select_offset = marker +
  893. offsetof(struct tpm2_pcr_selection, size_of_select);
  894. if (pcr_select_offset >= end) {
  895. rc = -EFAULT;
  896. break;
  897. }
  898. memcpy(&pcr_selection, marker, sizeof(pcr_selection));
  899. chip->active_banks[i] = be16_to_cpu(pcr_selection.hash_alg);
  900. sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
  901. sizeof(pcr_selection.size_of_select) +
  902. pcr_selection.size_of_select;
  903. marker = marker + sizeof_pcr_selection;
  904. }
  905. out:
  906. if (i < ARRAY_SIZE(chip->active_banks))
  907. chip->active_banks[i] = TPM2_ALG_ERROR;
  908. tpm_buf_destroy(&buf);
  909. return rc;
  910. }
  911. static int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip)
  912. {
  913. struct tpm_buf buf;
  914. u32 nr_commands;
  915. u32 *attrs;
  916. u32 cc;
  917. int i;
  918. int rc;
  919. rc = tpm2_get_tpm_pt(chip, TPM_PT_TOTAL_COMMANDS, &nr_commands, NULL);
  920. if (rc)
  921. goto out;
  922. if (nr_commands > 0xFFFFF) {
  923. rc = -EFAULT;
  924. goto out;
  925. }
  926. chip->cc_attrs_tbl = devm_kzalloc(&chip->dev, 4 * nr_commands,
  927. GFP_KERNEL);
  928. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
  929. if (rc)
  930. goto out;
  931. tpm_buf_append_u32(&buf, TPM2_CAP_COMMANDS);
  932. tpm_buf_append_u32(&buf, TPM2_CC_FIRST);
  933. tpm_buf_append_u32(&buf, nr_commands);
  934. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
  935. 9 + 4 * nr_commands, 0, NULL);
  936. if (rc) {
  937. tpm_buf_destroy(&buf);
  938. goto out;
  939. }
  940. if (nr_commands !=
  941. be32_to_cpup((__be32 *)&buf.data[TPM_HEADER_SIZE + 5])) {
  942. tpm_buf_destroy(&buf);
  943. goto out;
  944. }
  945. chip->nr_commands = nr_commands;
  946. attrs = (u32 *)&buf.data[TPM_HEADER_SIZE + 9];
  947. for (i = 0; i < nr_commands; i++, attrs++) {
  948. chip->cc_attrs_tbl[i] = be32_to_cpup(attrs);
  949. cc = chip->cc_attrs_tbl[i] & 0xFFFF;
  950. if (cc == TPM2_CC_CONTEXT_SAVE || cc == TPM2_CC_FLUSH_CONTEXT) {
  951. chip->cc_attrs_tbl[i] &=
  952. ~(GENMASK(2, 0) << TPM2_CC_ATTR_CHANDLES);
  953. chip->cc_attrs_tbl[i] |= 1 << TPM2_CC_ATTR_CHANDLES;
  954. }
  955. }
  956. tpm_buf_destroy(&buf);
  957. out:
  958. if (rc > 0)
  959. rc = -ENODEV;
  960. return rc;
  961. }
  962. /**
  963. * tpm2_auto_startup - Perform the standard automatic TPM initialization
  964. * sequence
  965. * @chip: TPM chip to use
  966. *
  967. * Returns 0 on success, < 0 in case of fatal error.
  968. */
  969. int tpm2_auto_startup(struct tpm_chip *chip)
  970. {
  971. int rc;
  972. rc = tpm_get_timeouts(chip);
  973. if (rc)
  974. goto out;
  975. rc = tpm2_do_selftest(chip);
  976. if (rc != 0 && rc != TPM2_RC_INITIALIZE) {
  977. dev_err(&chip->dev, "TPM self test failed\n");
  978. goto out;
  979. }
  980. if (rc == TPM2_RC_INITIALIZE) {
  981. rc = tpm2_startup(chip, TPM2_SU_CLEAR);
  982. if (rc)
  983. goto out;
  984. rc = tpm2_do_selftest(chip);
  985. if (rc) {
  986. dev_err(&chip->dev, "TPM self test failed\n");
  987. goto out;
  988. }
  989. }
  990. rc = tpm2_get_pcr_allocation(chip);
  991. if (rc)
  992. goto out;
  993. rc = tpm2_get_cc_attrs_tbl(chip);
  994. out:
  995. if (rc > 0)
  996. rc = -ENODEV;
  997. return rc;
  998. }
  999. int tpm2_find_cc(struct tpm_chip *chip, u32 cc)
  1000. {
  1001. int i;
  1002. for (i = 0; i < chip->nr_commands; i++)
  1003. if (cc == (chip->cc_attrs_tbl[i] & GENMASK(15, 0)))
  1004. return i;
  1005. return -1;
  1006. }