tpm2-cmd.c 24 KB

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