tpm2-cmd.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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_null_auth_area {
  49. __be32 handle;
  50. __be16 nonce_size;
  51. u8 attributes;
  52. __be16 auth_size;
  53. } __packed;
  54. struct tpm2_pcr_extend_in {
  55. __be32 pcr_idx;
  56. __be32 auth_area_size;
  57. struct tpm2_null_auth_area auth_area;
  58. __be32 digest_cnt;
  59. __be16 hash_alg;
  60. u8 digest[TPM_DIGEST_SIZE];
  61. } __packed;
  62. struct tpm2_get_tpm_pt_in {
  63. __be32 cap_id;
  64. __be32 property_id;
  65. __be32 property_cnt;
  66. } __packed;
  67. struct tpm2_get_tpm_pt_out {
  68. u8 more_data;
  69. __be32 subcap_id;
  70. __be32 property_cnt;
  71. __be32 property_id;
  72. __be32 value;
  73. } __packed;
  74. struct tpm2_get_random_in {
  75. __be16 size;
  76. } __packed;
  77. struct tpm2_get_random_out {
  78. __be16 size;
  79. u8 buffer[TPM_MAX_RNG_DATA];
  80. } __packed;
  81. union tpm2_cmd_params {
  82. struct tpm2_startup_in startup_in;
  83. struct tpm2_self_test_in selftest_in;
  84. struct tpm2_pcr_read_in pcrread_in;
  85. struct tpm2_pcr_read_out pcrread_out;
  86. struct tpm2_pcr_extend_in pcrextend_in;
  87. struct tpm2_get_tpm_pt_in get_tpm_pt_in;
  88. struct tpm2_get_tpm_pt_out get_tpm_pt_out;
  89. struct tpm2_get_random_in getrandom_in;
  90. struct tpm2_get_random_out getrandom_out;
  91. };
  92. struct tpm2_cmd {
  93. tpm_cmd_header header;
  94. union tpm2_cmd_params params;
  95. } __packed;
  96. struct tpm2_hash {
  97. unsigned int crypto_id;
  98. unsigned int tpm_id;
  99. };
  100. static struct tpm2_hash tpm2_hash_map[] = {
  101. {HASH_ALGO_SHA1, TPM2_ALG_SHA1},
  102. {HASH_ALGO_SHA256, TPM2_ALG_SHA256},
  103. {HASH_ALGO_SHA384, TPM2_ALG_SHA384},
  104. {HASH_ALGO_SHA512, TPM2_ALG_SHA512},
  105. {HASH_ALGO_SM3_256, TPM2_ALG_SM3_256},
  106. };
  107. /*
  108. * Array with one entry per ordinal defining the maximum amount
  109. * of time the chip could take to return the result. The values
  110. * of the SHORT, MEDIUM, and LONG durations are taken from the
  111. * PC Client Profile (PTP) specification.
  112. */
  113. static const u8 tpm2_ordinal_duration[TPM2_CC_LAST - TPM2_CC_FIRST + 1] = {
  114. TPM_UNDEFINED, /* 11F */
  115. TPM_UNDEFINED, /* 120 */
  116. TPM_LONG, /* 121 */
  117. TPM_UNDEFINED, /* 122 */
  118. TPM_UNDEFINED, /* 123 */
  119. TPM_UNDEFINED, /* 124 */
  120. TPM_UNDEFINED, /* 125 */
  121. TPM_UNDEFINED, /* 126 */
  122. TPM_UNDEFINED, /* 127 */
  123. TPM_UNDEFINED, /* 128 */
  124. TPM_LONG, /* 129 */
  125. TPM_UNDEFINED, /* 12a */
  126. TPM_UNDEFINED, /* 12b */
  127. TPM_UNDEFINED, /* 12c */
  128. TPM_UNDEFINED, /* 12d */
  129. TPM_UNDEFINED, /* 12e */
  130. TPM_UNDEFINED, /* 12f */
  131. TPM_UNDEFINED, /* 130 */
  132. TPM_UNDEFINED, /* 131 */
  133. TPM_UNDEFINED, /* 132 */
  134. TPM_UNDEFINED, /* 133 */
  135. TPM_UNDEFINED, /* 134 */
  136. TPM_UNDEFINED, /* 135 */
  137. TPM_UNDEFINED, /* 136 */
  138. TPM_UNDEFINED, /* 137 */
  139. TPM_UNDEFINED, /* 138 */
  140. TPM_UNDEFINED, /* 139 */
  141. TPM_UNDEFINED, /* 13a */
  142. TPM_UNDEFINED, /* 13b */
  143. TPM_UNDEFINED, /* 13c */
  144. TPM_UNDEFINED, /* 13d */
  145. TPM_MEDIUM, /* 13e */
  146. TPM_UNDEFINED, /* 13f */
  147. TPM_UNDEFINED, /* 140 */
  148. TPM_UNDEFINED, /* 141 */
  149. TPM_UNDEFINED, /* 142 */
  150. TPM_LONG, /* 143 */
  151. TPM_MEDIUM, /* 144 */
  152. TPM_UNDEFINED, /* 145 */
  153. TPM_UNDEFINED, /* 146 */
  154. TPM_UNDEFINED, /* 147 */
  155. TPM_UNDEFINED, /* 148 */
  156. TPM_UNDEFINED, /* 149 */
  157. TPM_UNDEFINED, /* 14a */
  158. TPM_UNDEFINED, /* 14b */
  159. TPM_UNDEFINED, /* 14c */
  160. TPM_UNDEFINED, /* 14d */
  161. TPM_LONG, /* 14e */
  162. TPM_UNDEFINED, /* 14f */
  163. TPM_UNDEFINED, /* 150 */
  164. TPM_UNDEFINED, /* 151 */
  165. TPM_UNDEFINED, /* 152 */
  166. TPM_UNDEFINED, /* 153 */
  167. TPM_UNDEFINED, /* 154 */
  168. TPM_UNDEFINED, /* 155 */
  169. TPM_UNDEFINED, /* 156 */
  170. TPM_UNDEFINED, /* 157 */
  171. TPM_UNDEFINED, /* 158 */
  172. TPM_UNDEFINED, /* 159 */
  173. TPM_UNDEFINED, /* 15a */
  174. TPM_UNDEFINED, /* 15b */
  175. TPM_MEDIUM, /* 15c */
  176. TPM_UNDEFINED, /* 15d */
  177. TPM_UNDEFINED, /* 15e */
  178. TPM_UNDEFINED, /* 15f */
  179. TPM_UNDEFINED, /* 160 */
  180. TPM_UNDEFINED, /* 161 */
  181. TPM_UNDEFINED, /* 162 */
  182. TPM_UNDEFINED, /* 163 */
  183. TPM_UNDEFINED, /* 164 */
  184. TPM_UNDEFINED, /* 165 */
  185. TPM_UNDEFINED, /* 166 */
  186. TPM_UNDEFINED, /* 167 */
  187. TPM_UNDEFINED, /* 168 */
  188. TPM_UNDEFINED, /* 169 */
  189. TPM_UNDEFINED, /* 16a */
  190. TPM_UNDEFINED, /* 16b */
  191. TPM_UNDEFINED, /* 16c */
  192. TPM_UNDEFINED, /* 16d */
  193. TPM_UNDEFINED, /* 16e */
  194. TPM_UNDEFINED, /* 16f */
  195. TPM_UNDEFINED, /* 170 */
  196. TPM_UNDEFINED, /* 171 */
  197. TPM_UNDEFINED, /* 172 */
  198. TPM_UNDEFINED, /* 173 */
  199. TPM_UNDEFINED, /* 174 */
  200. TPM_UNDEFINED, /* 175 */
  201. TPM_UNDEFINED, /* 176 */
  202. TPM_LONG, /* 177 */
  203. TPM_UNDEFINED, /* 178 */
  204. TPM_UNDEFINED, /* 179 */
  205. TPM_MEDIUM, /* 17a */
  206. TPM_LONG, /* 17b */
  207. TPM_UNDEFINED, /* 17c */
  208. TPM_UNDEFINED, /* 17d */
  209. TPM_UNDEFINED, /* 17e */
  210. TPM_UNDEFINED, /* 17f */
  211. TPM_UNDEFINED, /* 180 */
  212. TPM_UNDEFINED, /* 181 */
  213. TPM_MEDIUM, /* 182 */
  214. TPM_UNDEFINED, /* 183 */
  215. TPM_UNDEFINED, /* 184 */
  216. TPM_MEDIUM, /* 185 */
  217. TPM_MEDIUM, /* 186 */
  218. TPM_UNDEFINED, /* 187 */
  219. TPM_UNDEFINED, /* 188 */
  220. TPM_UNDEFINED, /* 189 */
  221. TPM_UNDEFINED, /* 18a */
  222. TPM_UNDEFINED, /* 18b */
  223. TPM_UNDEFINED, /* 18c */
  224. TPM_UNDEFINED, /* 18d */
  225. TPM_UNDEFINED, /* 18e */
  226. TPM_UNDEFINED /* 18f */
  227. };
  228. #define TPM2_PCR_READ_IN_SIZE \
  229. (sizeof(struct tpm_input_header) + \
  230. sizeof(struct tpm2_pcr_read_in))
  231. static const struct tpm_input_header tpm2_pcrread_header = {
  232. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  233. .length = cpu_to_be32(TPM2_PCR_READ_IN_SIZE),
  234. .ordinal = cpu_to_be32(TPM2_CC_PCR_READ)
  235. };
  236. /**
  237. * tpm2_pcr_read() - read a PCR value
  238. * @chip: TPM chip to use.
  239. * @pcr_idx: index of the PCR to read.
  240. * @ref_buf: buffer to store the resulting hash,
  241. *
  242. * 0 is returned when the operation is successful. If a negative number is
  243. * returned it remarks a POSIX error code. If a positive number is returned
  244. * it remarks a TPM error.
  245. */
  246. int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  247. {
  248. int rc;
  249. struct tpm2_cmd cmd;
  250. u8 *buf;
  251. if (pcr_idx >= TPM2_PLATFORM_PCR)
  252. return -EINVAL;
  253. cmd.header.in = tpm2_pcrread_header;
  254. cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
  255. cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
  256. cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
  257. memset(cmd.params.pcrread_in.pcr_select, 0,
  258. sizeof(cmd.params.pcrread_in.pcr_select));
  259. cmd.params.pcrread_in.pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7);
  260. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
  261. "attempting to read a pcr value");
  262. if (rc == 0) {
  263. buf = cmd.params.pcrread_out.digest;
  264. memcpy(res_buf, buf, TPM_DIGEST_SIZE);
  265. }
  266. return rc;
  267. }
  268. #define TPM2_GET_PCREXTEND_IN_SIZE \
  269. (sizeof(struct tpm_input_header) + \
  270. sizeof(struct tpm2_pcr_extend_in))
  271. static const struct tpm_input_header tpm2_pcrextend_header = {
  272. .tag = cpu_to_be16(TPM2_ST_SESSIONS),
  273. .length = cpu_to_be32(TPM2_GET_PCREXTEND_IN_SIZE),
  274. .ordinal = cpu_to_be32(TPM2_CC_PCR_EXTEND)
  275. };
  276. /**
  277. * tpm2_pcr_extend() - extend a PCR value
  278. * @chip: TPM chip to use.
  279. * @pcr_idx: index of the PCR.
  280. * @hash: hash value to use for the extend operation.
  281. *
  282. * 0 is returned when the operation is successful. If a negative number is
  283. * returned it remarks a POSIX error code. If a positive number is returned
  284. * it remarks a TPM error.
  285. */
  286. int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash)
  287. {
  288. struct tpm2_cmd cmd;
  289. int rc;
  290. cmd.header.in = tpm2_pcrextend_header;
  291. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
  292. cmd.params.pcrextend_in.auth_area_size =
  293. cpu_to_be32(sizeof(struct tpm2_null_auth_area));
  294. cmd.params.pcrextend_in.auth_area.handle =
  295. cpu_to_be32(TPM2_RS_PW);
  296. cmd.params.pcrextend_in.auth_area.nonce_size = 0;
  297. cmd.params.pcrextend_in.auth_area.attributes = 0;
  298. cmd.params.pcrextend_in.auth_area.auth_size = 0;
  299. cmd.params.pcrextend_in.digest_cnt = cpu_to_be32(1);
  300. cmd.params.pcrextend_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
  301. memcpy(cmd.params.pcrextend_in.digest, hash, TPM_DIGEST_SIZE);
  302. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
  303. "attempting extend a PCR value");
  304. return rc;
  305. }
  306. #define TPM2_GETRANDOM_IN_SIZE \
  307. (sizeof(struct tpm_input_header) + \
  308. sizeof(struct tpm2_get_random_in))
  309. static const struct tpm_input_header tpm2_getrandom_header = {
  310. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  311. .length = cpu_to_be32(TPM2_GETRANDOM_IN_SIZE),
  312. .ordinal = cpu_to_be32(TPM2_CC_GET_RANDOM)
  313. };
  314. /**
  315. * tpm2_get_random() - get random bytes from the TPM RNG
  316. * @chip: TPM chip to use
  317. * @out: destination buffer for the random bytes
  318. * @max: the max number of bytes to write to @out
  319. *
  320. * 0 is returned when the operation is successful. If a negative number is
  321. * returned it remarks a POSIX error code. If a positive number is returned
  322. * it remarks a TPM error.
  323. */
  324. int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)
  325. {
  326. struct tpm2_cmd cmd;
  327. u32 recd;
  328. u32 num_bytes;
  329. int err;
  330. int total = 0;
  331. int retries = 5;
  332. u8 *dest = out;
  333. num_bytes = min_t(u32, max, sizeof(cmd.params.getrandom_out.buffer));
  334. if (!out || !num_bytes ||
  335. max > sizeof(cmd.params.getrandom_out.buffer))
  336. return -EINVAL;
  337. do {
  338. cmd.header.in = tpm2_getrandom_header;
  339. cmd.params.getrandom_in.size = cpu_to_be16(num_bytes);
  340. err = tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
  341. "attempting get random");
  342. if (err)
  343. break;
  344. recd = min_t(u32, be16_to_cpu(cmd.params.getrandom_out.size),
  345. num_bytes);
  346. memcpy(dest, cmd.params.getrandom_out.buffer, recd);
  347. dest += recd;
  348. total += recd;
  349. num_bytes -= recd;
  350. } while (retries-- && total < max);
  351. return total ? total : -EIO;
  352. }
  353. #define TPM2_GET_TPM_PT_IN_SIZE \
  354. (sizeof(struct tpm_input_header) + \
  355. sizeof(struct tpm2_get_tpm_pt_in))
  356. static const struct tpm_input_header tpm2_get_tpm_pt_header = {
  357. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  358. .length = cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE),
  359. .ordinal = cpu_to_be32(TPM2_CC_GET_CAPABILITY)
  360. };
  361. /**
  362. * Append TPMS_AUTH_COMMAND to the buffer. The buffer must be allocated with
  363. * tpm_buf_alloc().
  364. *
  365. * @param buf: an allocated tpm_buf instance
  366. * @param nonce: the session nonce, may be NULL if not used
  367. * @param nonce_len: the session nonce length, may be 0 if not used
  368. * @param attributes: the session attributes
  369. * @param hmac: the session HMAC or password, may be NULL if not used
  370. * @param hmac_len: the session HMAC or password length, maybe 0 if not used
  371. */
  372. static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,
  373. const u8 *nonce, u16 nonce_len,
  374. u8 attributes,
  375. const u8 *hmac, u16 hmac_len)
  376. {
  377. tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);
  378. tpm_buf_append_u32(buf, session_handle);
  379. tpm_buf_append_u16(buf, nonce_len);
  380. if (nonce && nonce_len)
  381. tpm_buf_append(buf, nonce, nonce_len);
  382. tpm_buf_append_u8(buf, attributes);
  383. tpm_buf_append_u16(buf, hmac_len);
  384. if (hmac && hmac_len)
  385. tpm_buf_append(buf, hmac, hmac_len);
  386. }
  387. /**
  388. * tpm2_seal_trusted() - seal a trusted key
  389. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  390. * @options: authentication values and other options
  391. * @payload: the key data in clear and encrypted form
  392. *
  393. * Returns < 0 on error and 0 on success.
  394. */
  395. int tpm2_seal_trusted(struct tpm_chip *chip,
  396. struct trusted_key_payload *payload,
  397. struct trusted_key_options *options)
  398. {
  399. unsigned int blob_len;
  400. struct tpm_buf buf;
  401. u32 hash;
  402. int i;
  403. int rc;
  404. for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
  405. if (options->hash == tpm2_hash_map[i].crypto_id) {
  406. hash = tpm2_hash_map[i].tpm_id;
  407. break;
  408. }
  409. }
  410. if (i == ARRAY_SIZE(tpm2_hash_map))
  411. return -EINVAL;
  412. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
  413. if (rc)
  414. return rc;
  415. tpm_buf_append_u32(&buf, options->keyhandle);
  416. tpm2_buf_append_auth(&buf, TPM2_RS_PW,
  417. NULL /* nonce */, 0,
  418. 0 /* session_attributes */,
  419. options->keyauth /* hmac */,
  420. TPM_DIGEST_SIZE);
  421. /* sensitive */
  422. tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1);
  423. tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE);
  424. tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE);
  425. tpm_buf_append_u16(&buf, payload->key_len + 1);
  426. tpm_buf_append(&buf, payload->key, payload->key_len);
  427. tpm_buf_append_u8(&buf, payload->migratable);
  428. /* public */
  429. tpm_buf_append_u16(&buf, 14 + options->policydigest_len);
  430. tpm_buf_append_u16(&buf, TPM2_ALG_KEYEDHASH);
  431. tpm_buf_append_u16(&buf, hash);
  432. /* policy */
  433. if (options->policydigest_len) {
  434. tpm_buf_append_u32(&buf, 0);
  435. tpm_buf_append_u16(&buf, options->policydigest_len);
  436. tpm_buf_append(&buf, options->policydigest,
  437. options->policydigest_len);
  438. } else {
  439. tpm_buf_append_u32(&buf, TPM2_OA_USER_WITH_AUTH);
  440. tpm_buf_append_u16(&buf, 0);
  441. }
  442. /* public parameters */
  443. tpm_buf_append_u16(&buf, TPM2_ALG_NULL);
  444. tpm_buf_append_u16(&buf, 0);
  445. /* outside info */
  446. tpm_buf_append_u16(&buf, 0);
  447. /* creation PCR */
  448. tpm_buf_append_u32(&buf, 0);
  449. if (buf.flags & TPM_BUF_OVERFLOW) {
  450. rc = -E2BIG;
  451. goto out;
  452. }
  453. rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, "sealing data");
  454. if (rc)
  455. goto out;
  456. blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]);
  457. if (blob_len > MAX_BLOB_SIZE) {
  458. rc = -E2BIG;
  459. goto out;
  460. }
  461. memcpy(payload->blob, &buf.data[TPM_HEADER_SIZE + 4], blob_len);
  462. payload->blob_len = blob_len;
  463. out:
  464. tpm_buf_destroy(&buf);
  465. if (rc > 0) {
  466. if ((rc & TPM2_RC_HASH) == TPM2_RC_HASH)
  467. rc = -EINVAL;
  468. else
  469. rc = -EPERM;
  470. }
  471. return rc;
  472. }
  473. static int tpm2_load(struct tpm_chip *chip,
  474. struct trusted_key_payload *payload,
  475. struct trusted_key_options *options,
  476. u32 *blob_handle)
  477. {
  478. struct tpm_buf buf;
  479. unsigned int private_len;
  480. unsigned int public_len;
  481. unsigned int blob_len;
  482. int rc;
  483. private_len = be16_to_cpup((__be16 *) &payload->blob[0]);
  484. if (private_len > (payload->blob_len - 2))
  485. return -E2BIG;
  486. public_len = be16_to_cpup((__be16 *) &payload->blob[2 + private_len]);
  487. blob_len = private_len + public_len + 4;
  488. if (blob_len > payload->blob_len)
  489. return -E2BIG;
  490. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
  491. if (rc)
  492. return rc;
  493. tpm_buf_append_u32(&buf, options->keyhandle);
  494. tpm2_buf_append_auth(&buf, TPM2_RS_PW,
  495. NULL /* nonce */, 0,
  496. 0 /* session_attributes */,
  497. options->keyauth /* hmac */,
  498. TPM_DIGEST_SIZE);
  499. tpm_buf_append(&buf, payload->blob, blob_len);
  500. if (buf.flags & TPM_BUF_OVERFLOW) {
  501. rc = -E2BIG;
  502. goto out;
  503. }
  504. rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, "loading blob");
  505. if (!rc)
  506. *blob_handle = be32_to_cpup(
  507. (__be32 *) &buf.data[TPM_HEADER_SIZE]);
  508. out:
  509. tpm_buf_destroy(&buf);
  510. if (rc > 0)
  511. rc = -EPERM;
  512. return rc;
  513. }
  514. static void tpm2_flush_context(struct tpm_chip *chip, u32 handle)
  515. {
  516. struct tpm_buf buf;
  517. int rc;
  518. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT);
  519. if (rc) {
  520. dev_warn(chip->pdev, "0x%08x was not flushed, out of memory\n",
  521. handle);
  522. return;
  523. }
  524. tpm_buf_append_u32(&buf, handle);
  525. rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, "flushing context");
  526. if (rc)
  527. dev_warn(chip->pdev, "0x%08x was not flushed, rc=%d\n", handle,
  528. rc);
  529. tpm_buf_destroy(&buf);
  530. }
  531. static int tpm2_unseal(struct tpm_chip *chip,
  532. struct trusted_key_payload *payload,
  533. struct trusted_key_options *options,
  534. u32 blob_handle)
  535. {
  536. struct tpm_buf buf;
  537. u16 data_len;
  538. u8 *data;
  539. int rc;
  540. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
  541. if (rc)
  542. return rc;
  543. tpm_buf_append_u32(&buf, blob_handle);
  544. tpm2_buf_append_auth(&buf,
  545. options->policyhandle ?
  546. options->policyhandle : TPM2_RS_PW,
  547. NULL /* nonce */, 0,
  548. TPM2_SA_CONTINUE_SESSION,
  549. options->blobauth /* hmac */,
  550. TPM_DIGEST_SIZE);
  551. rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, "unsealing");
  552. if (rc > 0)
  553. rc = -EPERM;
  554. if (!rc) {
  555. data_len = be16_to_cpup(
  556. (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
  557. data = &buf.data[TPM_HEADER_SIZE + 6];
  558. memcpy(payload->key, data, data_len - 1);
  559. payload->key_len = data_len - 1;
  560. payload->migratable = data[data_len - 1];
  561. }
  562. tpm_buf_destroy(&buf);
  563. return rc;
  564. }
  565. /**
  566. * tpm_unseal_trusted() - unseal a trusted key
  567. * @chip_num: A specific chip number for the request or TPM_ANY_NUM
  568. * @options: authentication values and other options
  569. * @payload: the key data in clear and encrypted form
  570. *
  571. * Returns < 0 on error and 0 on success.
  572. */
  573. int tpm2_unseal_trusted(struct tpm_chip *chip,
  574. struct trusted_key_payload *payload,
  575. struct trusted_key_options *options)
  576. {
  577. u32 blob_handle;
  578. int rc;
  579. rc = tpm2_load(chip, payload, options, &blob_handle);
  580. if (rc)
  581. return rc;
  582. rc = tpm2_unseal(chip, payload, options, blob_handle);
  583. tpm2_flush_context(chip, blob_handle);
  584. return rc;
  585. }
  586. /**
  587. * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
  588. * @chip: TPM chip to use.
  589. * @property_id: property ID.
  590. * @value: output variable.
  591. * @desc: passed to tpm_transmit_cmd()
  592. *
  593. * 0 is returned when the operation is successful. If a negative number is
  594. * returned it remarks a POSIX error code. If a positive number is returned
  595. * it remarks a TPM error.
  596. */
  597. ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
  598. const char *desc)
  599. {
  600. struct tpm2_cmd cmd;
  601. int rc;
  602. cmd.header.in = tpm2_get_tpm_pt_header;
  603. cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
  604. cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id);
  605. cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
  606. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), desc);
  607. if (!rc)
  608. *value = cmd.params.get_tpm_pt_out.value;
  609. return rc;
  610. }
  611. #define TPM2_STARTUP_IN_SIZE \
  612. (sizeof(struct tpm_input_header) + \
  613. sizeof(struct tpm2_startup_in))
  614. static const struct tpm_input_header tpm2_startup_header = {
  615. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  616. .length = cpu_to_be32(TPM2_STARTUP_IN_SIZE),
  617. .ordinal = cpu_to_be32(TPM2_CC_STARTUP)
  618. };
  619. /**
  620. * tpm2_startup() - send startup command to the TPM chip
  621. * @chip: TPM chip to use.
  622. * @startup_type startup type. The value is either
  623. * TPM_SU_CLEAR or TPM_SU_STATE.
  624. *
  625. * 0 is returned when the operation is successful. If a negative number is
  626. * returned it remarks a POSIX error code. If a positive number is returned
  627. * it remarks a TPM error.
  628. */
  629. int tpm2_startup(struct tpm_chip *chip, u16 startup_type)
  630. {
  631. struct tpm2_cmd cmd;
  632. cmd.header.in = tpm2_startup_header;
  633. cmd.params.startup_in.startup_type = cpu_to_be16(startup_type);
  634. return tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
  635. "attempting to start the TPM");
  636. }
  637. EXPORT_SYMBOL_GPL(tpm2_startup);
  638. #define TPM2_SHUTDOWN_IN_SIZE \
  639. (sizeof(struct tpm_input_header) + \
  640. sizeof(struct tpm2_startup_in))
  641. static const struct tpm_input_header tpm2_shutdown_header = {
  642. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  643. .length = cpu_to_be32(TPM2_SHUTDOWN_IN_SIZE),
  644. .ordinal = cpu_to_be32(TPM2_CC_SHUTDOWN)
  645. };
  646. /**
  647. * tpm2_shutdown() - send shutdown command to the TPM chip
  648. * @chip: TPM chip to use.
  649. * @shutdown_type shutdown type. The value is either
  650. * TPM_SU_CLEAR or TPM_SU_STATE.
  651. */
  652. void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
  653. {
  654. struct tpm2_cmd cmd;
  655. int rc;
  656. cmd.header.in = tpm2_shutdown_header;
  657. cmd.params.startup_in.startup_type = cpu_to_be16(shutdown_type);
  658. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), "stopping the TPM");
  659. /* In places where shutdown command is sent there's no much we can do
  660. * except print the error code on a system failure.
  661. */
  662. if (rc < 0)
  663. dev_warn(chip->pdev, "transmit returned %d while stopping the TPM",
  664. rc);
  665. }
  666. EXPORT_SYMBOL_GPL(tpm2_shutdown);
  667. /*
  668. * tpm2_calc_ordinal_duration() - maximum duration for a command
  669. * @chip: TPM chip to use.
  670. * @ordinal: command code number.
  671. *
  672. * 0 is returned when the operation is successful. If a negative number is
  673. * returned it remarks a POSIX error code. If a positive number is returned
  674. * it remarks a TPM error.
  675. */
  676. unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
  677. {
  678. int index = TPM_UNDEFINED;
  679. int duration = 0;
  680. if (ordinal >= TPM2_CC_FIRST && ordinal <= TPM2_CC_LAST)
  681. index = tpm2_ordinal_duration[ordinal - TPM2_CC_FIRST];
  682. if (index != TPM_UNDEFINED)
  683. duration = chip->vendor.duration[index];
  684. if (duration <= 0)
  685. duration = 2 * 60 * HZ;
  686. return duration;
  687. }
  688. EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration);
  689. #define TPM2_SELF_TEST_IN_SIZE \
  690. (sizeof(struct tpm_input_header) + \
  691. sizeof(struct tpm2_self_test_in))
  692. static const struct tpm_input_header tpm2_selftest_header = {
  693. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  694. .length = cpu_to_be32(TPM2_SELF_TEST_IN_SIZE),
  695. .ordinal = cpu_to_be32(TPM2_CC_SELF_TEST)
  696. };
  697. /**
  698. * tpm2_continue_selftest() - start a self test
  699. * @chip: TPM chip to use
  700. * @full: test all commands instead of testing only those that were not
  701. * previously tested.
  702. *
  703. * 0 is returned when the operation is successful. If a negative number is
  704. * returned it remarks a POSIX error code. If a positive number is returned
  705. * it remarks a TPM error.
  706. */
  707. static int tpm2_start_selftest(struct tpm_chip *chip, bool full)
  708. {
  709. int rc;
  710. struct tpm2_cmd cmd;
  711. cmd.header.in = tpm2_selftest_header;
  712. cmd.params.selftest_in.full_test = full;
  713. rc = tpm_transmit_cmd(chip, &cmd, TPM2_SELF_TEST_IN_SIZE,
  714. "continue selftest");
  715. /* At least some prototype chips seem to give RC_TESTING error
  716. * immediately. This is a workaround for that.
  717. */
  718. if (rc == TPM2_RC_TESTING) {
  719. dev_warn(chip->pdev, "Got RC_TESTING, ignoring\n");
  720. rc = 0;
  721. }
  722. return rc;
  723. }
  724. /**
  725. * tpm2_do_selftest() - run a full self test
  726. * @chip: TPM chip to use
  727. *
  728. * During the self test TPM2 commands return with the error code RC_TESTING.
  729. * Waiting is done by issuing PCR read until it executes successfully.
  730. *
  731. * 0 is returned when the operation is successful. If a negative number is
  732. * returned it remarks a POSIX error code. If a positive number is returned
  733. * it remarks a TPM error.
  734. */
  735. int tpm2_do_selftest(struct tpm_chip *chip)
  736. {
  737. int rc;
  738. unsigned int loops;
  739. unsigned int delay_msec = 100;
  740. unsigned long duration;
  741. struct tpm2_cmd cmd;
  742. int i;
  743. duration = tpm2_calc_ordinal_duration(chip, TPM2_CC_SELF_TEST);
  744. loops = jiffies_to_msecs(duration) / delay_msec;
  745. rc = tpm2_start_selftest(chip, true);
  746. if (rc)
  747. return rc;
  748. for (i = 0; i < loops; i++) {
  749. /* Attempt to read a PCR value */
  750. cmd.header.in = tpm2_pcrread_header;
  751. cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
  752. cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
  753. cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
  754. cmd.params.pcrread_in.pcr_select[0] = 0x01;
  755. cmd.params.pcrread_in.pcr_select[1] = 0x00;
  756. cmd.params.pcrread_in.pcr_select[2] = 0x00;
  757. rc = tpm_transmit_cmd(chip, (u8 *) &cmd, sizeof(cmd), NULL);
  758. if (rc < 0)
  759. break;
  760. rc = be32_to_cpu(cmd.header.out.return_code);
  761. if (rc != TPM2_RC_TESTING)
  762. break;
  763. msleep(delay_msec);
  764. }
  765. return rc;
  766. }
  767. EXPORT_SYMBOL_GPL(tpm2_do_selftest);
  768. /**
  769. * tpm2_gen_interrupt() - generate an interrupt
  770. * @chip: TPM chip to use
  771. *
  772. * 0 is returned when the operation is successful. If a negative number is
  773. * returned it remarks a POSIX error code. If a positive number is returned
  774. * it remarks a TPM error.
  775. */
  776. int tpm2_gen_interrupt(struct tpm_chip *chip)
  777. {
  778. u32 dummy;
  779. return tpm2_get_tpm_pt(chip, 0x100, &dummy,
  780. "attempting to generate an interrupt");
  781. }
  782. EXPORT_SYMBOL_GPL(tpm2_gen_interrupt);
  783. /**
  784. * tpm2_probe() - probe TPM 2.0
  785. * @chip: TPM chip to use
  786. *
  787. * Send idempotent TPM 2.0 command and see whether TPM 2.0 chip replied based on
  788. * the reply tag.
  789. */
  790. int tpm2_probe(struct tpm_chip *chip)
  791. {
  792. struct tpm2_cmd cmd;
  793. int rc;
  794. cmd.header.in = tpm2_get_tpm_pt_header;
  795. cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
  796. cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100);
  797. cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
  798. rc = tpm_transmit(chip, (const char *) &cmd, sizeof(cmd));
  799. if (rc < 0)
  800. return rc;
  801. else if (rc < TPM_HEADER_SIZE)
  802. return -EFAULT;
  803. if (be16_to_cpu(cmd.header.out.tag) == TPM2_ST_NO_SESSIONS)
  804. chip->flags |= TPM_CHIP_FLAG_TPM2;
  805. return 0;
  806. }
  807. EXPORT_SYMBOL_GPL(tpm2_probe);