tpm2-cmd.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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 <keys/trusted-type.h>
  19. enum tpm2_object_attributes {
  20. TPM2_ATTR_USER_WITH_AUTH = BIT(6),
  21. };
  22. struct tpm2_startup_in {
  23. __be16 startup_type;
  24. } __packed;
  25. struct tpm2_self_test_in {
  26. u8 full_test;
  27. } __packed;
  28. struct tpm2_pcr_read_in {
  29. __be32 pcr_selects_cnt;
  30. __be16 hash_alg;
  31. u8 pcr_select_size;
  32. u8 pcr_select[TPM2_PCR_SELECT_MIN];
  33. } __packed;
  34. struct tpm2_pcr_read_out {
  35. __be32 update_cnt;
  36. __be32 pcr_selects_cnt;
  37. __be16 hash_alg;
  38. u8 pcr_select_size;
  39. u8 pcr_select[TPM2_PCR_SELECT_MIN];
  40. __be32 digests_cnt;
  41. __be16 digest_size;
  42. u8 digest[TPM_DIGEST_SIZE];
  43. } __packed;
  44. struct tpm2_null_auth_area {
  45. __be32 handle;
  46. __be16 nonce_size;
  47. u8 attributes;
  48. __be16 auth_size;
  49. } __packed;
  50. struct tpm2_pcr_extend_in {
  51. __be32 pcr_idx;
  52. __be32 auth_area_size;
  53. struct tpm2_null_auth_area auth_area;
  54. __be32 digest_cnt;
  55. __be16 hash_alg;
  56. u8 digest[TPM_DIGEST_SIZE];
  57. } __packed;
  58. struct tpm2_get_tpm_pt_in {
  59. __be32 cap_id;
  60. __be32 property_id;
  61. __be32 property_cnt;
  62. } __packed;
  63. struct tpm2_get_tpm_pt_out {
  64. u8 more_data;
  65. __be32 subcap_id;
  66. __be32 property_cnt;
  67. __be32 property_id;
  68. __be32 value;
  69. } __packed;
  70. struct tpm2_get_random_in {
  71. __be16 size;
  72. } __packed;
  73. struct tpm2_get_random_out {
  74. __be16 size;
  75. u8 buffer[TPM_MAX_RNG_DATA];
  76. } __packed;
  77. union tpm2_cmd_params {
  78. struct tpm2_startup_in startup_in;
  79. struct tpm2_self_test_in selftest_in;
  80. struct tpm2_pcr_read_in pcrread_in;
  81. struct tpm2_pcr_read_out pcrread_out;
  82. struct tpm2_pcr_extend_in pcrextend_in;
  83. struct tpm2_get_tpm_pt_in get_tpm_pt_in;
  84. struct tpm2_get_tpm_pt_out get_tpm_pt_out;
  85. struct tpm2_get_random_in getrandom_in;
  86. struct tpm2_get_random_out getrandom_out;
  87. };
  88. struct tpm2_cmd {
  89. tpm_cmd_header header;
  90. union tpm2_cmd_params params;
  91. } __packed;
  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. static const struct tpm_input_header tpm2_pcrread_header = {
  217. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  218. .length = cpu_to_be32(TPM2_PCR_READ_IN_SIZE),
  219. .ordinal = cpu_to_be32(TPM2_CC_PCR_READ)
  220. };
  221. /**
  222. * tpm2_pcr_read() - read a PCR value
  223. * @chip: TPM chip to use.
  224. * @pcr_idx: index of the PCR to read.
  225. * @ref_buf: buffer to store the resulting hash,
  226. *
  227. * 0 is returned when the operation is successful. If a negative number is
  228. * returned it remarks a POSIX error code. If a positive number is returned
  229. * it remarks a TPM error.
  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, &cmd, sizeof(cmd), 0,
  246. "attempting to read a pcr value");
  247. if (rc == 0) {
  248. buf = cmd.params.pcrread_out.digest;
  249. memcpy(res_buf, buf, TPM_DIGEST_SIZE);
  250. }
  251. return rc;
  252. }
  253. #define TPM2_GET_PCREXTEND_IN_SIZE \
  254. (sizeof(struct tpm_input_header) + \
  255. sizeof(struct tpm2_pcr_extend_in))
  256. static const struct tpm_input_header tpm2_pcrextend_header = {
  257. .tag = cpu_to_be16(TPM2_ST_SESSIONS),
  258. .length = cpu_to_be32(TPM2_GET_PCREXTEND_IN_SIZE),
  259. .ordinal = cpu_to_be32(TPM2_CC_PCR_EXTEND)
  260. };
  261. /**
  262. * tpm2_pcr_extend() - extend a PCR value
  263. * @chip: TPM chip to use.
  264. * @pcr_idx: index of the PCR.
  265. * @hash: hash value to use for the extend operation.
  266. *
  267. * 0 is returned when the operation is successful. If a negative number is
  268. * returned it remarks a POSIX error code. If a positive number is returned
  269. * it remarks a TPM error.
  270. */
  271. int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash)
  272. {
  273. struct tpm2_cmd cmd;
  274. int rc;
  275. cmd.header.in = tpm2_pcrextend_header;
  276. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
  277. cmd.params.pcrextend_in.auth_area_size =
  278. cpu_to_be32(sizeof(struct tpm2_null_auth_area));
  279. cmd.params.pcrextend_in.auth_area.handle =
  280. cpu_to_be32(TPM2_RS_PW);
  281. cmd.params.pcrextend_in.auth_area.nonce_size = 0;
  282. cmd.params.pcrextend_in.auth_area.attributes = 0;
  283. cmd.params.pcrextend_in.auth_area.auth_size = 0;
  284. cmd.params.pcrextend_in.digest_cnt = cpu_to_be32(1);
  285. cmd.params.pcrextend_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
  286. memcpy(cmd.params.pcrextend_in.digest, hash, TPM_DIGEST_SIZE);
  287. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0,
  288. "attempting extend a PCR value");
  289. return rc;
  290. }
  291. #define TPM2_GETRANDOM_IN_SIZE \
  292. (sizeof(struct tpm_input_header) + \
  293. sizeof(struct tpm2_get_random_in))
  294. static const struct tpm_input_header tpm2_getrandom_header = {
  295. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  296. .length = cpu_to_be32(TPM2_GETRANDOM_IN_SIZE),
  297. .ordinal = cpu_to_be32(TPM2_CC_GET_RANDOM)
  298. };
  299. /**
  300. * tpm2_get_random() - get random bytes from the TPM RNG
  301. * @chip: TPM chip to use
  302. * @out: destination buffer for the random bytes
  303. * @max: the max number of bytes to write to @out
  304. *
  305. * 0 is returned when the operation is successful. If a negative number is
  306. * returned it remarks a POSIX error code. If a positive number is returned
  307. * it remarks a TPM error.
  308. */
  309. int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)
  310. {
  311. struct tpm2_cmd cmd;
  312. u32 recd;
  313. u32 num_bytes;
  314. int err;
  315. int total = 0;
  316. int retries = 5;
  317. u8 *dest = out;
  318. num_bytes = min_t(u32, max, sizeof(cmd.params.getrandom_out.buffer));
  319. if (!out || !num_bytes ||
  320. max > sizeof(cmd.params.getrandom_out.buffer))
  321. return -EINVAL;
  322. do {
  323. cmd.header.in = tpm2_getrandom_header;
  324. cmd.params.getrandom_in.size = cpu_to_be16(num_bytes);
  325. err = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0,
  326. "attempting get random");
  327. if (err)
  328. break;
  329. recd = min_t(u32, be16_to_cpu(cmd.params.getrandom_out.size),
  330. num_bytes);
  331. memcpy(dest, cmd.params.getrandom_out.buffer, recd);
  332. dest += recd;
  333. total += recd;
  334. num_bytes -= recd;
  335. } while (retries-- && total < max);
  336. return total ? total : -EIO;
  337. }
  338. #define TPM2_GET_TPM_PT_IN_SIZE \
  339. (sizeof(struct tpm_input_header) + \
  340. sizeof(struct tpm2_get_tpm_pt_in))
  341. static const struct tpm_input_header tpm2_get_tpm_pt_header = {
  342. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  343. .length = cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE),
  344. .ordinal = cpu_to_be32(TPM2_CC_GET_CAPABILITY)
  345. };
  346. /**
  347. * Append TPMS_AUTH_COMMAND to the buffer. The buffer must be allocated with
  348. * tpm_buf_alloc().
  349. *
  350. * @param buf: an allocated tpm_buf instance
  351. * @param nonce: the session nonce, may be NULL if not used
  352. * @param nonce_len: the session nonce length, may be 0 if not used
  353. * @param attributes: the session attributes
  354. * @param hmac: the session HMAC or password, may be NULL if not used
  355. * @param hmac_len: the session HMAC or password length, maybe 0 if not used
  356. */
  357. static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,
  358. const u8 *nonce, u16 nonce_len,
  359. u8 attributes,
  360. const u8 *hmac, u16 hmac_len)
  361. {
  362. tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);
  363. tpm_buf_append_u32(buf, session_handle);
  364. tpm_buf_append_u16(buf, nonce_len);
  365. if (nonce && nonce_len)
  366. tpm_buf_append(buf, nonce, nonce_len);
  367. tpm_buf_append_u8(buf, attributes);
  368. tpm_buf_append_u16(buf, hmac_len);
  369. if (hmac && hmac_len)
  370. tpm_buf_append(buf, hmac, hmac_len);
  371. }
  372. /**
  373. * tpm2_seal_trusted() - seal the payload of a trusted key
  374. * @chip_num: TPM chip to use
  375. * @payload: the key data in clear and encrypted form
  376. * @options: authentication values and other options
  377. *
  378. * Return: < 0 on error and 0 on success.
  379. */
  380. int tpm2_seal_trusted(struct tpm_chip *chip,
  381. struct trusted_key_payload *payload,
  382. struct trusted_key_options *options)
  383. {
  384. unsigned int blob_len;
  385. struct tpm_buf buf;
  386. int rc;
  387. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
  388. if (rc)
  389. return rc;
  390. tpm_buf_append_u32(&buf, options->keyhandle);
  391. tpm2_buf_append_auth(&buf, TPM2_RS_PW,
  392. NULL /* nonce */, 0,
  393. 0 /* session_attributes */,
  394. options->keyauth /* hmac */,
  395. TPM_DIGEST_SIZE);
  396. /* sensitive */
  397. tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1);
  398. tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE);
  399. tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE);
  400. tpm_buf_append_u16(&buf, payload->key_len + 1);
  401. tpm_buf_append(&buf, payload->key, payload->key_len);
  402. tpm_buf_append_u8(&buf, payload->migratable);
  403. /* public */
  404. tpm_buf_append_u16(&buf, 14);
  405. tpm_buf_append_u16(&buf, TPM2_ALG_KEYEDHASH);
  406. tpm_buf_append_u16(&buf, TPM2_ALG_SHA256);
  407. tpm_buf_append_u32(&buf, TPM2_ATTR_USER_WITH_AUTH);
  408. tpm_buf_append_u16(&buf, 0); /* policy digest size */
  409. tpm_buf_append_u16(&buf, TPM2_ALG_NULL);
  410. tpm_buf_append_u16(&buf, 0);
  411. /* outside info */
  412. tpm_buf_append_u16(&buf, 0);
  413. /* creation PCR */
  414. tpm_buf_append_u32(&buf, 0);
  415. if (buf.flags & TPM_BUF_OVERFLOW) {
  416. rc = -E2BIG;
  417. goto out;
  418. }
  419. rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, 0, "sealing data");
  420. if (rc)
  421. goto out;
  422. blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]);
  423. if (blob_len > MAX_BLOB_SIZE) {
  424. rc = -E2BIG;
  425. goto out;
  426. }
  427. memcpy(payload->blob, &buf.data[TPM_HEADER_SIZE + 4], blob_len);
  428. payload->blob_len = blob_len;
  429. out:
  430. tpm_buf_destroy(&buf);
  431. if (rc > 0)
  432. rc = -EPERM;
  433. return rc;
  434. }
  435. /**
  436. * tpm2_load_cmd() - execute a TPM2_Load command
  437. * @chip_num: TPM chip to use
  438. * @payload: the key data in clear and encrypted form
  439. * @options: authentication values and other options
  440. *
  441. * Return: same as with tpm_transmit_cmd
  442. */
  443. static int tpm2_load_cmd(struct tpm_chip *chip,
  444. struct trusted_key_payload *payload,
  445. struct trusted_key_options *options,
  446. u32 *blob_handle, unsigned int flags)
  447. {
  448. struct tpm_buf buf;
  449. unsigned int private_len;
  450. unsigned int public_len;
  451. unsigned int blob_len;
  452. int rc;
  453. private_len = be16_to_cpup((__be16 *) &payload->blob[0]);
  454. if (private_len > (payload->blob_len - 2))
  455. return -E2BIG;
  456. public_len = be16_to_cpup((__be16 *) &payload->blob[2 + private_len]);
  457. blob_len = private_len + public_len + 4;
  458. if (blob_len > payload->blob_len)
  459. return -E2BIG;
  460. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
  461. if (rc)
  462. return rc;
  463. tpm_buf_append_u32(&buf, options->keyhandle);
  464. tpm2_buf_append_auth(&buf, TPM2_RS_PW,
  465. NULL /* nonce */, 0,
  466. 0 /* session_attributes */,
  467. options->keyauth /* hmac */,
  468. TPM_DIGEST_SIZE);
  469. tpm_buf_append(&buf, payload->blob, blob_len);
  470. if (buf.flags & TPM_BUF_OVERFLOW) {
  471. rc = -E2BIG;
  472. goto out;
  473. }
  474. rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, flags, "loading blob");
  475. if (!rc)
  476. *blob_handle = be32_to_cpup(
  477. (__be32 *) &buf.data[TPM_HEADER_SIZE]);
  478. out:
  479. tpm_buf_destroy(&buf);
  480. if (rc > 0)
  481. rc = -EPERM;
  482. return rc;
  483. }
  484. /**
  485. * tpm2_flush_context_cmd() - execute a TPM2_FlushContext command
  486. * @chip_num: TPM chip to use
  487. * @payload: the key data in clear and encrypted form
  488. * @options: authentication values and other options
  489. *
  490. * Return: same as with tpm_transmit_cmd
  491. */
  492. static void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
  493. unsigned int flags)
  494. {
  495. struct tpm_buf buf;
  496. int rc;
  497. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT);
  498. if (rc) {
  499. dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
  500. handle);
  501. return;
  502. }
  503. tpm_buf_append_u32(&buf, handle);
  504. rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, flags,
  505. "flushing context");
  506. if (rc)
  507. dev_warn(&chip->dev, "0x%08x was not flushed, rc=%d\n", handle,
  508. rc);
  509. tpm_buf_destroy(&buf);
  510. }
  511. /**
  512. * tpm2_unseal_cmd() - execute a TPM2_Unload command
  513. * @chip_num: TPM chip to use
  514. * @payload: the key data in clear and encrypted form
  515. * @options: authentication values and other options
  516. *
  517. * Return: same as with tpm_transmit_cmd
  518. */
  519. static int tpm2_unseal_cmd(struct tpm_chip *chip,
  520. struct trusted_key_payload *payload,
  521. struct trusted_key_options *options,
  522. u32 blob_handle, unsigned int flags)
  523. {
  524. struct tpm_buf buf;
  525. u16 data_len;
  526. u8 *data;
  527. int rc;
  528. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
  529. if (rc)
  530. return rc;
  531. tpm_buf_append_u32(&buf, blob_handle);
  532. tpm2_buf_append_auth(&buf, TPM2_RS_PW,
  533. NULL /* nonce */, 0,
  534. 0 /* session_attributes */,
  535. options->blobauth /* hmac */,
  536. TPM_DIGEST_SIZE);
  537. rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, flags, "unsealing");
  538. if (rc > 0)
  539. rc = -EPERM;
  540. if (!rc) {
  541. data_len = be16_to_cpup(
  542. (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
  543. if (data_len < MIN_KEY_SIZE || data_len > MAX_KEY_SIZE + 1) {
  544. rc = -EFAULT;
  545. goto out;
  546. }
  547. data = &buf.data[TPM_HEADER_SIZE + 6];
  548. memcpy(payload->key, data, data_len - 1);
  549. payload->key_len = data_len - 1;
  550. payload->migratable = data[data_len - 1];
  551. }
  552. out:
  553. tpm_buf_destroy(&buf);
  554. return rc;
  555. }
  556. /**
  557. * tpm_unseal_trusted() - unseal the payload of a trusted key
  558. * @chip_num: TPM chip to use
  559. * @payload: the key data in clear and encrypted form
  560. * @options: authentication values and other options
  561. *
  562. * Return: < 0 on error and 0 on success.
  563. */
  564. int tpm2_unseal_trusted(struct tpm_chip *chip,
  565. struct trusted_key_payload *payload,
  566. struct trusted_key_options *options)
  567. {
  568. u32 blob_handle;
  569. int rc;
  570. mutex_lock(&chip->tpm_mutex);
  571. rc = tpm2_load_cmd(chip, payload, options, &blob_handle,
  572. TPM_TRANSMIT_UNLOCKED);
  573. if (rc)
  574. goto out;
  575. rc = tpm2_unseal_cmd(chip, payload, options, blob_handle,
  576. TPM_TRANSMIT_UNLOCKED);
  577. tpm2_flush_context_cmd(chip, blob_handle, TPM_TRANSMIT_UNLOCKED);
  578. out:
  579. mutex_unlock(&chip->tpm_mutex);
  580. return rc;
  581. }
  582. /**
  583. * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
  584. * @chip: TPM chip to use.
  585. * @property_id: property ID.
  586. * @value: output variable.
  587. * @desc: passed to tpm_transmit_cmd()
  588. *
  589. * 0 is returned when the operation is successful. If a negative number is
  590. * returned it remarks a POSIX error code. If a positive number is returned
  591. * it remarks a TPM error.
  592. */
  593. ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
  594. const char *desc)
  595. {
  596. struct tpm2_cmd cmd;
  597. int rc;
  598. cmd.header.in = tpm2_get_tpm_pt_header;
  599. cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
  600. cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id);
  601. cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
  602. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, desc);
  603. if (!rc)
  604. *value = be32_to_cpu(cmd.params.get_tpm_pt_out.value);
  605. return rc;
  606. }
  607. #define TPM2_STARTUP_IN_SIZE \
  608. (sizeof(struct tpm_input_header) + \
  609. sizeof(struct tpm2_startup_in))
  610. static const struct tpm_input_header tpm2_startup_header = {
  611. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  612. .length = cpu_to_be32(TPM2_STARTUP_IN_SIZE),
  613. .ordinal = cpu_to_be32(TPM2_CC_STARTUP)
  614. };
  615. /**
  616. * tpm2_startup() - send startup command to the TPM chip
  617. * @chip: TPM chip to use.
  618. * @startup_type startup type. The value is either
  619. * TPM_SU_CLEAR or TPM_SU_STATE.
  620. *
  621. * 0 is returned when the operation is successful. If a negative number is
  622. * returned it remarks a POSIX error code. If a positive number is returned
  623. * it remarks a TPM error.
  624. */
  625. int tpm2_startup(struct tpm_chip *chip, u16 startup_type)
  626. {
  627. struct tpm2_cmd cmd;
  628. cmd.header.in = tpm2_startup_header;
  629. cmd.params.startup_in.startup_type = cpu_to_be16(startup_type);
  630. return tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0,
  631. "attempting to start the TPM");
  632. }
  633. EXPORT_SYMBOL_GPL(tpm2_startup);
  634. #define TPM2_SHUTDOWN_IN_SIZE \
  635. (sizeof(struct tpm_input_header) + \
  636. sizeof(struct tpm2_startup_in))
  637. static const struct tpm_input_header tpm2_shutdown_header = {
  638. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  639. .length = cpu_to_be32(TPM2_SHUTDOWN_IN_SIZE),
  640. .ordinal = cpu_to_be32(TPM2_CC_SHUTDOWN)
  641. };
  642. /**
  643. * tpm2_shutdown() - send shutdown command to the TPM chip
  644. * @chip: TPM chip to use.
  645. * @shutdown_type shutdown type. The value is either
  646. * TPM_SU_CLEAR or TPM_SU_STATE.
  647. */
  648. void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
  649. {
  650. struct tpm2_cmd cmd;
  651. int rc;
  652. cmd.header.in = tpm2_shutdown_header;
  653. cmd.params.startup_in.startup_type = cpu_to_be16(shutdown_type);
  654. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, "stopping the TPM");
  655. /* In places where shutdown command is sent there's no much we can do
  656. * except print the error code on a system failure.
  657. */
  658. if (rc < 0)
  659. dev_warn(&chip->dev, "transmit returned %d while stopping the TPM",
  660. rc);
  661. }
  662. EXPORT_SYMBOL_GPL(tpm2_shutdown);
  663. /*
  664. * tpm2_calc_ordinal_duration() - maximum duration for a command
  665. * @chip: TPM chip to use.
  666. * @ordinal: command code number.
  667. *
  668. * 0 is returned when the operation is successful. If a negative number is
  669. * returned it remarks a POSIX error code. If a positive number is returned
  670. * it remarks a TPM error.
  671. */
  672. unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
  673. {
  674. int index = TPM_UNDEFINED;
  675. int duration = 0;
  676. if (ordinal >= TPM2_CC_FIRST && ordinal <= TPM2_CC_LAST)
  677. index = tpm2_ordinal_duration[ordinal - TPM2_CC_FIRST];
  678. if (index != TPM_UNDEFINED)
  679. duration = chip->vendor.duration[index];
  680. if (duration <= 0)
  681. duration = 2 * 60 * HZ;
  682. return duration;
  683. }
  684. EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration);
  685. #define TPM2_SELF_TEST_IN_SIZE \
  686. (sizeof(struct tpm_input_header) + \
  687. sizeof(struct tpm2_self_test_in))
  688. static const struct tpm_input_header tpm2_selftest_header = {
  689. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  690. .length = cpu_to_be32(TPM2_SELF_TEST_IN_SIZE),
  691. .ordinal = cpu_to_be32(TPM2_CC_SELF_TEST)
  692. };
  693. /**
  694. * tpm2_continue_selftest() - start a self test
  695. * @chip: TPM chip to use
  696. * @full: test all commands instead of testing only those that were not
  697. * previously tested.
  698. *
  699. * 0 is returned when the operation is successful. If a negative number is
  700. * returned it remarks a POSIX error code. If a positive number is returned
  701. * it remarks a TPM error.
  702. */
  703. static int tpm2_start_selftest(struct tpm_chip *chip, bool full)
  704. {
  705. int rc;
  706. struct tpm2_cmd cmd;
  707. cmd.header.in = tpm2_selftest_header;
  708. cmd.params.selftest_in.full_test = full;
  709. rc = tpm_transmit_cmd(chip, &cmd, TPM2_SELF_TEST_IN_SIZE, 0,
  710. "continue selftest");
  711. /* At least some prototype chips seem to give RC_TESTING error
  712. * immediately. This is a workaround for that.
  713. */
  714. if (rc == TPM2_RC_TESTING) {
  715. dev_warn(&chip->dev, "Got RC_TESTING, ignoring\n");
  716. rc = 0;
  717. }
  718. return rc;
  719. }
  720. /**
  721. * tpm2_do_selftest() - run a full self test
  722. * @chip: TPM chip to use
  723. *
  724. * During the self test TPM2 commands return with the error code RC_TESTING.
  725. * Waiting is done by issuing PCR read until it executes successfully.
  726. *
  727. * 0 is returned when the operation is successful. If a negative number is
  728. * returned it remarks a POSIX error code. If a positive number is returned
  729. * it remarks a TPM error.
  730. */
  731. int tpm2_do_selftest(struct tpm_chip *chip)
  732. {
  733. int rc;
  734. unsigned int loops;
  735. unsigned int delay_msec = 100;
  736. unsigned long duration;
  737. struct tpm2_cmd cmd;
  738. int i;
  739. duration = tpm2_calc_ordinal_duration(chip, TPM2_CC_SELF_TEST);
  740. loops = jiffies_to_msecs(duration) / delay_msec;
  741. rc = tpm2_start_selftest(chip, true);
  742. if (rc)
  743. return rc;
  744. for (i = 0; i < loops; i++) {
  745. /* Attempt to read a PCR value */
  746. cmd.header.in = tpm2_pcrread_header;
  747. cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
  748. cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
  749. cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
  750. cmd.params.pcrread_in.pcr_select[0] = 0x01;
  751. cmd.params.pcrread_in.pcr_select[1] = 0x00;
  752. cmd.params.pcrread_in.pcr_select[2] = 0x00;
  753. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, NULL);
  754. if (rc < 0)
  755. break;
  756. rc = be32_to_cpu(cmd.header.out.return_code);
  757. if (rc != TPM2_RC_TESTING)
  758. break;
  759. msleep(delay_msec);
  760. }
  761. return rc;
  762. }
  763. EXPORT_SYMBOL_GPL(tpm2_do_selftest);
  764. /**
  765. * tpm2_gen_interrupt() - generate an interrupt
  766. * @chip: TPM chip to use
  767. *
  768. * 0 is returned when the operation is successful. If a negative number is
  769. * returned it remarks a POSIX error code. If a positive number is returned
  770. * it remarks a TPM error.
  771. */
  772. int tpm2_gen_interrupt(struct tpm_chip *chip)
  773. {
  774. u32 dummy;
  775. return tpm2_get_tpm_pt(chip, 0x100, &dummy,
  776. "attempting to generate an interrupt");
  777. }
  778. EXPORT_SYMBOL_GPL(tpm2_gen_interrupt);
  779. /**
  780. * tpm2_probe() - probe TPM 2.0
  781. * @chip: TPM chip to use
  782. *
  783. * Send idempotent TPM 2.0 command and see whether TPM 2.0 chip replied based on
  784. * the reply tag.
  785. */
  786. int tpm2_probe(struct tpm_chip *chip)
  787. {
  788. struct tpm2_cmd cmd;
  789. int rc;
  790. cmd.header.in = tpm2_get_tpm_pt_header;
  791. cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
  792. cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100);
  793. cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
  794. rc = tpm_transmit(chip, (const u8 *)&cmd, sizeof(cmd), 0);
  795. if (rc < 0)
  796. return rc;
  797. else if (rc < TPM_HEADER_SIZE)
  798. return -EFAULT;
  799. if (be16_to_cpu(cmd.header.out.tag) == TPM2_ST_NO_SESSIONS)
  800. chip->flags |= TPM_CHIP_FLAG_TPM2;
  801. return 0;
  802. }
  803. EXPORT_SYMBOL_GPL(tpm2_probe);