tpm2-cmd.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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), 0,
  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), 0,
  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), 0,
  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 the payload of a trusted key
  389. * @chip_num: TPM chip to use
  390. * @payload: the key data in clear and encrypted form
  391. * @options: authentication values and other options
  392. *
  393. * Return: < 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, 0, "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 (tpm2_rc_value(rc) == TPM2_RC_HASH)
  467. rc = -EINVAL;
  468. else
  469. rc = -EPERM;
  470. }
  471. return rc;
  472. }
  473. /**
  474. * tpm2_load_cmd() - execute a TPM2_Load command
  475. * @chip_num: TPM chip to use
  476. * @payload: the key data in clear and encrypted form
  477. * @options: authentication values and other options
  478. *
  479. * Return: same as with tpm_transmit_cmd
  480. */
  481. static int tpm2_load_cmd(struct tpm_chip *chip,
  482. struct trusted_key_payload *payload,
  483. struct trusted_key_options *options,
  484. u32 *blob_handle, unsigned int flags)
  485. {
  486. struct tpm_buf buf;
  487. unsigned int private_len;
  488. unsigned int public_len;
  489. unsigned int blob_len;
  490. int rc;
  491. private_len = be16_to_cpup((__be16 *) &payload->blob[0]);
  492. if (private_len > (payload->blob_len - 2))
  493. return -E2BIG;
  494. public_len = be16_to_cpup((__be16 *) &payload->blob[2 + private_len]);
  495. blob_len = private_len + public_len + 4;
  496. if (blob_len > payload->blob_len)
  497. return -E2BIG;
  498. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
  499. if (rc)
  500. return rc;
  501. tpm_buf_append_u32(&buf, options->keyhandle);
  502. tpm2_buf_append_auth(&buf, TPM2_RS_PW,
  503. NULL /* nonce */, 0,
  504. 0 /* session_attributes */,
  505. options->keyauth /* hmac */,
  506. TPM_DIGEST_SIZE);
  507. tpm_buf_append(&buf, payload->blob, blob_len);
  508. if (buf.flags & TPM_BUF_OVERFLOW) {
  509. rc = -E2BIG;
  510. goto out;
  511. }
  512. rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, flags, "loading blob");
  513. if (!rc)
  514. *blob_handle = be32_to_cpup(
  515. (__be32 *) &buf.data[TPM_HEADER_SIZE]);
  516. out:
  517. tpm_buf_destroy(&buf);
  518. if (rc > 0)
  519. rc = -EPERM;
  520. return rc;
  521. }
  522. /**
  523. * tpm2_flush_context_cmd() - execute a TPM2_FlushContext command
  524. * @chip_num: TPM chip to use
  525. * @payload: the key data in clear and encrypted form
  526. * @options: authentication values and other options
  527. *
  528. * Return: same as with tpm_transmit_cmd
  529. */
  530. static void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
  531. unsigned int flags)
  532. {
  533. struct tpm_buf buf;
  534. int rc;
  535. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT);
  536. if (rc) {
  537. dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
  538. handle);
  539. return;
  540. }
  541. tpm_buf_append_u32(&buf, handle);
  542. rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, flags,
  543. "flushing context");
  544. if (rc)
  545. dev_warn(&chip->dev, "0x%08x was not flushed, rc=%d\n", handle,
  546. rc);
  547. tpm_buf_destroy(&buf);
  548. }
  549. /**
  550. * tpm2_unseal_cmd() - execute a TPM2_Unload command
  551. * @chip_num: TPM chip to use
  552. * @payload: the key data in clear and encrypted form
  553. * @options: authentication values and other options
  554. *
  555. * Return: same as with tpm_transmit_cmd
  556. */
  557. static int tpm2_unseal_cmd(struct tpm_chip *chip,
  558. struct trusted_key_payload *payload,
  559. struct trusted_key_options *options,
  560. u32 blob_handle, unsigned int flags)
  561. {
  562. struct tpm_buf buf;
  563. u16 data_len;
  564. u8 *data;
  565. int rc;
  566. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
  567. if (rc)
  568. return rc;
  569. tpm_buf_append_u32(&buf, blob_handle);
  570. tpm2_buf_append_auth(&buf,
  571. options->policyhandle ?
  572. options->policyhandle : TPM2_RS_PW,
  573. NULL /* nonce */, 0,
  574. TPM2_SA_CONTINUE_SESSION,
  575. options->blobauth /* hmac */,
  576. TPM_DIGEST_SIZE);
  577. rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, flags, "unsealing");
  578. if (rc > 0)
  579. rc = -EPERM;
  580. if (!rc) {
  581. data_len = be16_to_cpup(
  582. (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
  583. if (data_len < MIN_KEY_SIZE || data_len > MAX_KEY_SIZE + 1) {
  584. rc = -EFAULT;
  585. goto out;
  586. }
  587. data = &buf.data[TPM_HEADER_SIZE + 6];
  588. memcpy(payload->key, data, data_len - 1);
  589. payload->key_len = data_len - 1;
  590. payload->migratable = data[data_len - 1];
  591. }
  592. out:
  593. tpm_buf_destroy(&buf);
  594. return rc;
  595. }
  596. /**
  597. * tpm_unseal_trusted() - unseal the payload of a trusted key
  598. * @chip_num: TPM chip to use
  599. * @payload: the key data in clear and encrypted form
  600. * @options: authentication values and other options
  601. *
  602. * Return: < 0 on error and 0 on success.
  603. */
  604. int tpm2_unseal_trusted(struct tpm_chip *chip,
  605. struct trusted_key_payload *payload,
  606. struct trusted_key_options *options)
  607. {
  608. u32 blob_handle;
  609. int rc;
  610. mutex_lock(&chip->tpm_mutex);
  611. rc = tpm2_load_cmd(chip, payload, options, &blob_handle,
  612. TPM_TRANSMIT_UNLOCKED);
  613. if (rc)
  614. goto out;
  615. rc = tpm2_unseal_cmd(chip, payload, options, blob_handle,
  616. TPM_TRANSMIT_UNLOCKED);
  617. tpm2_flush_context_cmd(chip, blob_handle, TPM_TRANSMIT_UNLOCKED);
  618. out:
  619. mutex_unlock(&chip->tpm_mutex);
  620. return rc;
  621. }
  622. /**
  623. * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
  624. * @chip: TPM chip to use.
  625. * @property_id: property ID.
  626. * @value: output variable.
  627. * @desc: passed to tpm_transmit_cmd()
  628. *
  629. * 0 is returned when the operation is successful. If a negative number is
  630. * returned it remarks a POSIX error code. If a positive number is returned
  631. * it remarks a TPM error.
  632. */
  633. ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
  634. const char *desc)
  635. {
  636. struct tpm2_cmd cmd;
  637. int rc;
  638. cmd.header.in = tpm2_get_tpm_pt_header;
  639. cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
  640. cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id);
  641. cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
  642. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, desc);
  643. if (!rc)
  644. *value = be32_to_cpu(cmd.params.get_tpm_pt_out.value);
  645. return rc;
  646. }
  647. EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
  648. #define TPM2_STARTUP_IN_SIZE \
  649. (sizeof(struct tpm_input_header) + \
  650. sizeof(struct tpm2_startup_in))
  651. static const struct tpm_input_header tpm2_startup_header = {
  652. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  653. .length = cpu_to_be32(TPM2_STARTUP_IN_SIZE),
  654. .ordinal = cpu_to_be32(TPM2_CC_STARTUP)
  655. };
  656. /**
  657. * tpm2_startup() - send startup command to the TPM chip
  658. * @chip: TPM chip to use.
  659. * @startup_type startup type. The value is either
  660. * TPM_SU_CLEAR or TPM_SU_STATE.
  661. *
  662. * 0 is returned when the operation is successful. If a negative number is
  663. * returned it remarks a POSIX error code. If a positive number is returned
  664. * it remarks a TPM error.
  665. */
  666. static int tpm2_startup(struct tpm_chip *chip, u16 startup_type)
  667. {
  668. struct tpm2_cmd cmd;
  669. cmd.header.in = tpm2_startup_header;
  670. cmd.params.startup_in.startup_type = cpu_to_be16(startup_type);
  671. return tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0,
  672. "attempting to start the TPM");
  673. }
  674. #define TPM2_SHUTDOWN_IN_SIZE \
  675. (sizeof(struct tpm_input_header) + \
  676. sizeof(struct tpm2_startup_in))
  677. static const struct tpm_input_header tpm2_shutdown_header = {
  678. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  679. .length = cpu_to_be32(TPM2_SHUTDOWN_IN_SIZE),
  680. .ordinal = cpu_to_be32(TPM2_CC_SHUTDOWN)
  681. };
  682. /**
  683. * tpm2_shutdown() - send shutdown command to the TPM chip
  684. * @chip: TPM chip to use.
  685. * @shutdown_type shutdown type. The value is either
  686. * TPM_SU_CLEAR or TPM_SU_STATE.
  687. */
  688. void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
  689. {
  690. struct tpm2_cmd cmd;
  691. int rc;
  692. cmd.header.in = tpm2_shutdown_header;
  693. cmd.params.startup_in.startup_type = cpu_to_be16(shutdown_type);
  694. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, "stopping the TPM");
  695. /* In places where shutdown command is sent there's no much we can do
  696. * except print the error code on a system failure.
  697. */
  698. if (rc < 0)
  699. dev_warn(&chip->dev, "transmit returned %d while stopping the TPM",
  700. rc);
  701. }
  702. /*
  703. * tpm2_calc_ordinal_duration() - maximum duration for a command
  704. * @chip: TPM chip to use.
  705. * @ordinal: command code number.
  706. *
  707. * 0 is returned when the operation is successful. If a negative number is
  708. * returned it remarks a POSIX error code. If a positive number is returned
  709. * it remarks a TPM error.
  710. */
  711. unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
  712. {
  713. int index = TPM_UNDEFINED;
  714. int duration = 0;
  715. if (ordinal >= TPM2_CC_FIRST && ordinal <= TPM2_CC_LAST)
  716. index = tpm2_ordinal_duration[ordinal - TPM2_CC_FIRST];
  717. if (index != TPM_UNDEFINED)
  718. duration = chip->duration[index];
  719. if (duration <= 0)
  720. duration = 2 * 60 * HZ;
  721. return duration;
  722. }
  723. EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration);
  724. #define TPM2_SELF_TEST_IN_SIZE \
  725. (sizeof(struct tpm_input_header) + \
  726. sizeof(struct tpm2_self_test_in))
  727. static const struct tpm_input_header tpm2_selftest_header = {
  728. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  729. .length = cpu_to_be32(TPM2_SELF_TEST_IN_SIZE),
  730. .ordinal = cpu_to_be32(TPM2_CC_SELF_TEST)
  731. };
  732. /**
  733. * tpm2_continue_selftest() - start a self test
  734. * @chip: TPM chip to use
  735. * @full: test all commands instead of testing only those that were not
  736. * previously tested.
  737. *
  738. * 0 is returned when the operation is successful. If a negative number is
  739. * returned it remarks a POSIX error code. If a positive number is returned
  740. * it remarks a TPM error.
  741. */
  742. static int tpm2_start_selftest(struct tpm_chip *chip, bool full)
  743. {
  744. int rc;
  745. struct tpm2_cmd cmd;
  746. cmd.header.in = tpm2_selftest_header;
  747. cmd.params.selftest_in.full_test = full;
  748. rc = tpm_transmit_cmd(chip, &cmd, TPM2_SELF_TEST_IN_SIZE, 0,
  749. "continue selftest");
  750. /* At least some prototype chips seem to give RC_TESTING error
  751. * immediately. This is a workaround for that.
  752. */
  753. if (rc == TPM2_RC_TESTING) {
  754. dev_warn(&chip->dev, "Got RC_TESTING, ignoring\n");
  755. rc = 0;
  756. }
  757. return rc;
  758. }
  759. /**
  760. * tpm2_do_selftest() - run a full self test
  761. * @chip: TPM chip to use
  762. *
  763. * During the self test TPM2 commands return with the error code RC_TESTING.
  764. * Waiting is done by issuing PCR read until it executes successfully.
  765. *
  766. * 0 is returned when the operation is successful. If a negative number is
  767. * returned it remarks a POSIX error code. If a positive number is returned
  768. * it remarks a TPM error.
  769. */
  770. static int tpm2_do_selftest(struct tpm_chip *chip)
  771. {
  772. int rc;
  773. unsigned int loops;
  774. unsigned int delay_msec = 100;
  775. unsigned long duration;
  776. struct tpm2_cmd cmd;
  777. int i;
  778. duration = tpm2_calc_ordinal_duration(chip, TPM2_CC_SELF_TEST);
  779. loops = jiffies_to_msecs(duration) / delay_msec;
  780. rc = tpm2_start_selftest(chip, true);
  781. if (rc)
  782. return rc;
  783. for (i = 0; i < loops; i++) {
  784. /* Attempt to read a PCR value */
  785. cmd.header.in = tpm2_pcrread_header;
  786. cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
  787. cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
  788. cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
  789. cmd.params.pcrread_in.pcr_select[0] = 0x01;
  790. cmd.params.pcrread_in.pcr_select[1] = 0x00;
  791. cmd.params.pcrread_in.pcr_select[2] = 0x00;
  792. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, NULL);
  793. if (rc < 0)
  794. break;
  795. rc = be32_to_cpu(cmd.header.out.return_code);
  796. if (rc != TPM2_RC_TESTING)
  797. break;
  798. msleep(delay_msec);
  799. }
  800. return rc;
  801. }
  802. /**
  803. * tpm2_probe() - probe TPM 2.0
  804. * @chip: TPM chip to use
  805. *
  806. * Send idempotent TPM 2.0 command and see whether TPM 2.0 chip replied based on
  807. * the reply tag.
  808. */
  809. int tpm2_probe(struct tpm_chip *chip)
  810. {
  811. struct tpm2_cmd cmd;
  812. int rc;
  813. cmd.header.in = tpm2_get_tpm_pt_header;
  814. cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
  815. cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100);
  816. cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
  817. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, NULL);
  818. if (rc < 0)
  819. return rc;
  820. if (be16_to_cpu(cmd.header.out.tag) == TPM2_ST_NO_SESSIONS)
  821. chip->flags |= TPM_CHIP_FLAG_TPM2;
  822. return 0;
  823. }
  824. EXPORT_SYMBOL_GPL(tpm2_probe);
  825. /**
  826. * tpm2_auto_startup - Perform the standard automatic TPM initialization
  827. * sequence
  828. * @chip: TPM chip to use
  829. *
  830. * Returns 0 on success, < 0 in case of fatal error.
  831. */
  832. int tpm2_auto_startup(struct tpm_chip *chip)
  833. {
  834. int rc;
  835. rc = tpm_get_timeouts(chip);
  836. if (rc)
  837. goto out;
  838. rc = tpm2_do_selftest(chip);
  839. if (rc != 0 && rc != TPM2_RC_INITIALIZE) {
  840. dev_err(&chip->dev, "TPM self test failed\n");
  841. goto out;
  842. }
  843. if (rc == TPM2_RC_INITIALIZE) {
  844. rc = tpm2_startup(chip, TPM2_SU_CLEAR);
  845. if (rc)
  846. goto out;
  847. rc = tpm2_do_selftest(chip);
  848. if (rc) {
  849. dev_err(&chip->dev, "TPM self test failed\n");
  850. goto out;
  851. }
  852. }
  853. out:
  854. if (rc > 0)
  855. rc = -ENODEV;
  856. return rc;
  857. }