tpm2-cmd.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. * @res_buf: buffer to store the resulting hash.
  241. *
  242. * Return: Same as with tpm_transmit_cmd.
  243. */
  244. int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  245. {
  246. int rc;
  247. struct tpm2_cmd cmd;
  248. u8 *buf;
  249. if (pcr_idx >= TPM2_PLATFORM_PCR)
  250. return -EINVAL;
  251. cmd.header.in = tpm2_pcrread_header;
  252. cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
  253. cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
  254. cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
  255. memset(cmd.params.pcrread_in.pcr_select, 0,
  256. sizeof(cmd.params.pcrread_in.pcr_select));
  257. cmd.params.pcrread_in.pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7);
  258. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0,
  259. "attempting to read a pcr value");
  260. if (rc == 0) {
  261. buf = cmd.params.pcrread_out.digest;
  262. memcpy(res_buf, buf, TPM_DIGEST_SIZE);
  263. }
  264. return rc;
  265. }
  266. #define TPM2_GET_PCREXTEND_IN_SIZE \
  267. (sizeof(struct tpm_input_header) + \
  268. sizeof(struct tpm2_pcr_extend_in))
  269. static const struct tpm_input_header tpm2_pcrextend_header = {
  270. .tag = cpu_to_be16(TPM2_ST_SESSIONS),
  271. .length = cpu_to_be32(TPM2_GET_PCREXTEND_IN_SIZE),
  272. .ordinal = cpu_to_be32(TPM2_CC_PCR_EXTEND)
  273. };
  274. /**
  275. * tpm2_pcr_extend() - extend a PCR value
  276. *
  277. * @chip: TPM chip to use.
  278. * @pcr_idx: index of the PCR.
  279. * @hash: hash value to use for the extend operation.
  280. *
  281. * Return: Same as with tpm_transmit_cmd.
  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), 0,
  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. *
  314. * @chip: TPM chip to use
  315. * @out: destination buffer for the random bytes
  316. * @max: the max number of bytes to write to @out
  317. *
  318. * Return:
  319. * Size of the output buffer, or -EIO on 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), 0,
  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. * tpm_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
  360. *
  361. * @buf: an allocated tpm_buf instance
  362. * @session_handle: session handle
  363. * @nonce: the session nonce, may be NULL if not used
  364. * @nonce_len: the session nonce length, may be 0 if not used
  365. * @attributes: the session attributes
  366. * @hmac: the session HMAC or password, may be NULL if not used
  367. * @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 the payload of a trusted key
  386. *
  387. * @chip: TPM chip to use
  388. * @payload: the key data in clear and encrypted form
  389. * @options: authentication values and other options
  390. *
  391. * Return: < 0 on error and 0 on success.
  392. */
  393. int tpm2_seal_trusted(struct tpm_chip *chip,
  394. struct trusted_key_payload *payload,
  395. struct trusted_key_options *options)
  396. {
  397. unsigned int blob_len;
  398. struct tpm_buf buf;
  399. u32 hash;
  400. int i;
  401. int rc;
  402. for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
  403. if (options->hash == tpm2_hash_map[i].crypto_id) {
  404. hash = tpm2_hash_map[i].tpm_id;
  405. break;
  406. }
  407. }
  408. if (i == ARRAY_SIZE(tpm2_hash_map))
  409. return -EINVAL;
  410. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
  411. if (rc)
  412. return rc;
  413. tpm_buf_append_u32(&buf, options->keyhandle);
  414. tpm2_buf_append_auth(&buf, TPM2_RS_PW,
  415. NULL /* nonce */, 0,
  416. 0 /* session_attributes */,
  417. options->keyauth /* hmac */,
  418. TPM_DIGEST_SIZE);
  419. /* sensitive */
  420. tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1);
  421. tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE);
  422. tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE);
  423. tpm_buf_append_u16(&buf, payload->key_len + 1);
  424. tpm_buf_append(&buf, payload->key, payload->key_len);
  425. tpm_buf_append_u8(&buf, payload->migratable);
  426. /* public */
  427. tpm_buf_append_u16(&buf, 14 + options->policydigest_len);
  428. tpm_buf_append_u16(&buf, TPM2_ALG_KEYEDHASH);
  429. tpm_buf_append_u16(&buf, hash);
  430. /* policy */
  431. if (options->policydigest_len) {
  432. tpm_buf_append_u32(&buf, 0);
  433. tpm_buf_append_u16(&buf, options->policydigest_len);
  434. tpm_buf_append(&buf, options->policydigest,
  435. options->policydigest_len);
  436. } else {
  437. tpm_buf_append_u32(&buf, TPM2_OA_USER_WITH_AUTH);
  438. tpm_buf_append_u16(&buf, 0);
  439. }
  440. /* public parameters */
  441. tpm_buf_append_u16(&buf, TPM2_ALG_NULL);
  442. tpm_buf_append_u16(&buf, 0);
  443. /* outside info */
  444. tpm_buf_append_u16(&buf, 0);
  445. /* creation PCR */
  446. tpm_buf_append_u32(&buf, 0);
  447. if (buf.flags & TPM_BUF_OVERFLOW) {
  448. rc = -E2BIG;
  449. goto out;
  450. }
  451. rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, 0, "sealing data");
  452. if (rc)
  453. goto out;
  454. blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]);
  455. if (blob_len > MAX_BLOB_SIZE) {
  456. rc = -E2BIG;
  457. goto out;
  458. }
  459. memcpy(payload->blob, &buf.data[TPM_HEADER_SIZE + 4], blob_len);
  460. payload->blob_len = blob_len;
  461. out:
  462. tpm_buf_destroy(&buf);
  463. if (rc > 0) {
  464. if ((rc & TPM2_RC_HASH) == TPM2_RC_HASH)
  465. rc = -EINVAL;
  466. else
  467. rc = -EPERM;
  468. }
  469. return rc;
  470. }
  471. /**
  472. * tpm2_load_cmd() - execute a TPM2_Load command
  473. *
  474. * @chip: TPM chip to use
  475. * @payload: the key data in clear and encrypted form
  476. * @options: authentication values and other options
  477. * @blob_handle: returned blob handle
  478. * @flags: tpm transmit flags
  479. *
  480. * Return: 0 on success.
  481. * -E2BIG on wrong payload size.
  482. * -EPERM on tpm error status.
  483. * < 0 error from tpm_transmit_cmd.
  484. */
  485. static int tpm2_load_cmd(struct tpm_chip *chip,
  486. struct trusted_key_payload *payload,
  487. struct trusted_key_options *options,
  488. u32 *blob_handle, unsigned int flags)
  489. {
  490. struct tpm_buf buf;
  491. unsigned int private_len;
  492. unsigned int public_len;
  493. unsigned int blob_len;
  494. int rc;
  495. private_len = be16_to_cpup((__be16 *) &payload->blob[0]);
  496. if (private_len > (payload->blob_len - 2))
  497. return -E2BIG;
  498. public_len = be16_to_cpup((__be16 *) &payload->blob[2 + private_len]);
  499. blob_len = private_len + public_len + 4;
  500. if (blob_len > payload->blob_len)
  501. return -E2BIG;
  502. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
  503. if (rc)
  504. return rc;
  505. tpm_buf_append_u32(&buf, options->keyhandle);
  506. tpm2_buf_append_auth(&buf, TPM2_RS_PW,
  507. NULL /* nonce */, 0,
  508. 0 /* session_attributes */,
  509. options->keyauth /* hmac */,
  510. TPM_DIGEST_SIZE);
  511. tpm_buf_append(&buf, payload->blob, blob_len);
  512. if (buf.flags & TPM_BUF_OVERFLOW) {
  513. rc = -E2BIG;
  514. goto out;
  515. }
  516. rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, flags, "loading blob");
  517. if (!rc)
  518. *blob_handle = be32_to_cpup(
  519. (__be32 *) &buf.data[TPM_HEADER_SIZE]);
  520. out:
  521. tpm_buf_destroy(&buf);
  522. if (rc > 0)
  523. rc = -EPERM;
  524. return rc;
  525. }
  526. /**
  527. * tpm2_flush_context_cmd() - execute a TPM2_FlushContext command
  528. *
  529. * @chip: TPM chip to use
  530. * @handle: the key data in clear and encrypted form
  531. * @flags: tpm transmit flags
  532. *
  533. * Return: Same as with tpm_transmit_cmd.
  534. */
  535. static void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
  536. unsigned int flags)
  537. {
  538. struct tpm_buf buf;
  539. int rc;
  540. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT);
  541. if (rc) {
  542. dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
  543. handle);
  544. return;
  545. }
  546. tpm_buf_append_u32(&buf, handle);
  547. rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, flags,
  548. "flushing context");
  549. if (rc)
  550. dev_warn(&chip->dev, "0x%08x was not flushed, rc=%d\n", handle,
  551. rc);
  552. tpm_buf_destroy(&buf);
  553. }
  554. /**
  555. * tpm2_unseal_cmd() - execute a TPM2_Unload command
  556. *
  557. * @chip: TPM chip to use
  558. * @payload: the key data in clear and encrypted form
  559. * @options: authentication values and other options
  560. * @blob_handle: blob handle
  561. * @flags: tpm_transmit_cmd flags
  562. *
  563. * Return: 0 on success
  564. * -EPERM on tpm error status
  565. * < 0 error from tpm_transmit_cmd
  566. */
  567. static int tpm2_unseal_cmd(struct tpm_chip *chip,
  568. struct trusted_key_payload *payload,
  569. struct trusted_key_options *options,
  570. u32 blob_handle, unsigned int flags)
  571. {
  572. struct tpm_buf buf;
  573. u16 data_len;
  574. u8 *data;
  575. int rc;
  576. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
  577. if (rc)
  578. return rc;
  579. tpm_buf_append_u32(&buf, blob_handle);
  580. tpm2_buf_append_auth(&buf,
  581. options->policyhandle ?
  582. options->policyhandle : TPM2_RS_PW,
  583. NULL /* nonce */, 0,
  584. TPM2_SA_CONTINUE_SESSION,
  585. options->blobauth /* hmac */,
  586. TPM_DIGEST_SIZE);
  587. rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, flags, "unsealing");
  588. if (rc > 0)
  589. rc = -EPERM;
  590. if (!rc) {
  591. data_len = be16_to_cpup(
  592. (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
  593. data = &buf.data[TPM_HEADER_SIZE + 6];
  594. memcpy(payload->key, data, data_len - 1);
  595. payload->key_len = data_len - 1;
  596. payload->migratable = data[data_len - 1];
  597. }
  598. tpm_buf_destroy(&buf);
  599. return rc;
  600. }
  601. /**
  602. * tpm2_unseal_trusted() - unseal the payload of a trusted key
  603. *
  604. * @chip: TPM chip to use
  605. * @payload: the key data in clear and encrypted form
  606. * @options: authentication values and other options
  607. *
  608. * Return: Same as with tpm_transmit_cmd.
  609. */
  610. int tpm2_unseal_trusted(struct tpm_chip *chip,
  611. struct trusted_key_payload *payload,
  612. struct trusted_key_options *options)
  613. {
  614. u32 blob_handle;
  615. int rc;
  616. mutex_lock(&chip->tpm_mutex);
  617. rc = tpm2_load_cmd(chip, payload, options, &blob_handle,
  618. TPM_TRANSMIT_UNLOCKED);
  619. if (rc)
  620. goto out;
  621. rc = tpm2_unseal_cmd(chip, payload, options, blob_handle,
  622. TPM_TRANSMIT_UNLOCKED);
  623. tpm2_flush_context_cmd(chip, blob_handle, TPM_TRANSMIT_UNLOCKED);
  624. out:
  625. mutex_unlock(&chip->tpm_mutex);
  626. return rc;
  627. }
  628. /**
  629. * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
  630. * @chip: TPM chip to use.
  631. * @property_id: property ID.
  632. * @value: output variable.
  633. * @desc: passed to tpm_transmit_cmd()
  634. *
  635. * Return: Same as with tpm_transmit_cmd.
  636. */
  637. ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
  638. const char *desc)
  639. {
  640. struct tpm2_cmd cmd;
  641. int rc;
  642. cmd.header.in = tpm2_get_tpm_pt_header;
  643. cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
  644. cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id);
  645. cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
  646. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, desc);
  647. if (!rc)
  648. *value = be32_to_cpu(cmd.params.get_tpm_pt_out.value);
  649. return rc;
  650. }
  651. EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
  652. #define TPM2_STARTUP_IN_SIZE \
  653. (sizeof(struct tpm_input_header) + \
  654. sizeof(struct tpm2_startup_in))
  655. static const struct tpm_input_header tpm2_startup_header = {
  656. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  657. .length = cpu_to_be32(TPM2_STARTUP_IN_SIZE),
  658. .ordinal = cpu_to_be32(TPM2_CC_STARTUP)
  659. };
  660. /**
  661. * tpm2_startup() - send startup command to the TPM chip
  662. *
  663. * @chip: TPM chip to use.
  664. * @startup_type: startup type. The value is either
  665. * TPM_SU_CLEAR or TPM_SU_STATE.
  666. *
  667. * Return: Same as with tpm_transmit_cmd.
  668. */
  669. static int tpm2_startup(struct tpm_chip *chip, u16 startup_type)
  670. {
  671. struct tpm2_cmd cmd;
  672. cmd.header.in = tpm2_startup_header;
  673. cmd.params.startup_in.startup_type = cpu_to_be16(startup_type);
  674. return tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0,
  675. "attempting to start the TPM");
  676. }
  677. #define TPM2_SHUTDOWN_IN_SIZE \
  678. (sizeof(struct tpm_input_header) + \
  679. sizeof(struct tpm2_startup_in))
  680. static const struct tpm_input_header tpm2_shutdown_header = {
  681. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  682. .length = cpu_to_be32(TPM2_SHUTDOWN_IN_SIZE),
  683. .ordinal = cpu_to_be32(TPM2_CC_SHUTDOWN)
  684. };
  685. /**
  686. * tpm2_shutdown() - send shutdown command to the TPM chip
  687. *
  688. * @chip: TPM chip to use.
  689. * @shutdown_type: shutdown type. The value is either
  690. * TPM_SU_CLEAR or TPM_SU_STATE.
  691. */
  692. void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
  693. {
  694. struct tpm2_cmd cmd;
  695. int rc;
  696. cmd.header.in = tpm2_shutdown_header;
  697. cmd.params.startup_in.startup_type = cpu_to_be16(shutdown_type);
  698. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, "stopping the TPM");
  699. /* In places where shutdown command is sent there's no much we can do
  700. * except print the error code on a system failure.
  701. */
  702. if (rc < 0)
  703. dev_warn(&chip->dev, "transmit returned %d while stopping the TPM",
  704. rc);
  705. }
  706. /*
  707. * tpm2_calc_ordinal_duration() - maximum duration for a command
  708. *
  709. * @chip: TPM chip to use.
  710. * @ordinal: command code number.
  711. *
  712. * Return: maximum duration for a command
  713. */
  714. unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
  715. {
  716. int index = TPM_UNDEFINED;
  717. int duration = 0;
  718. if (ordinal >= TPM2_CC_FIRST && ordinal <= TPM2_CC_LAST)
  719. index = tpm2_ordinal_duration[ordinal - TPM2_CC_FIRST];
  720. if (index != TPM_UNDEFINED)
  721. duration = chip->duration[index];
  722. if (duration <= 0)
  723. duration = 2 * 60 * HZ;
  724. return duration;
  725. }
  726. EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration);
  727. #define TPM2_SELF_TEST_IN_SIZE \
  728. (sizeof(struct tpm_input_header) + \
  729. sizeof(struct tpm2_self_test_in))
  730. static const struct tpm_input_header tpm2_selftest_header = {
  731. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  732. .length = cpu_to_be32(TPM2_SELF_TEST_IN_SIZE),
  733. .ordinal = cpu_to_be32(TPM2_CC_SELF_TEST)
  734. };
  735. /**
  736. * tpm2_continue_selftest() - start a self test
  737. *
  738. * @chip: TPM chip to use
  739. * @full: test all commands instead of testing only those that were not
  740. * previously tested.
  741. *
  742. * Return: Same as with tpm_transmit_cmd with exception of RC_TESTING.
  743. */
  744. static int tpm2_start_selftest(struct tpm_chip *chip, bool full)
  745. {
  746. int rc;
  747. struct tpm2_cmd cmd;
  748. cmd.header.in = tpm2_selftest_header;
  749. cmd.params.selftest_in.full_test = full;
  750. rc = tpm_transmit_cmd(chip, &cmd, TPM2_SELF_TEST_IN_SIZE, 0,
  751. "continue selftest");
  752. /* At least some prototype chips seem to give RC_TESTING error
  753. * immediately. This is a workaround for that.
  754. */
  755. if (rc == TPM2_RC_TESTING) {
  756. dev_warn(&chip->dev, "Got RC_TESTING, ignoring\n");
  757. rc = 0;
  758. }
  759. return rc;
  760. }
  761. /**
  762. * tpm2_do_selftest() - run a full self test
  763. *
  764. * @chip: TPM chip to use
  765. *
  766. * Return: Same as with tpm_transmit_cmd.
  767. *
  768. * During the self test TPM2 commands return with the error code RC_TESTING.
  769. * Waiting is done by issuing PCR read until it executes successfully.
  770. */
  771. static int tpm2_do_selftest(struct tpm_chip *chip)
  772. {
  773. int rc;
  774. unsigned int loops;
  775. unsigned int delay_msec = 100;
  776. unsigned long duration;
  777. struct tpm2_cmd cmd;
  778. int i;
  779. duration = tpm2_calc_ordinal_duration(chip, TPM2_CC_SELF_TEST);
  780. loops = jiffies_to_msecs(duration) / delay_msec;
  781. rc = tpm2_start_selftest(chip, true);
  782. if (rc)
  783. return rc;
  784. for (i = 0; i < loops; i++) {
  785. /* Attempt to read a PCR value */
  786. cmd.header.in = tpm2_pcrread_header;
  787. cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
  788. cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
  789. cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
  790. cmd.params.pcrread_in.pcr_select[0] = 0x01;
  791. cmd.params.pcrread_in.pcr_select[1] = 0x00;
  792. cmd.params.pcrread_in.pcr_select[2] = 0x00;
  793. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, NULL);
  794. if (rc < 0)
  795. break;
  796. rc = be32_to_cpu(cmd.header.out.return_code);
  797. if (rc != TPM2_RC_TESTING)
  798. break;
  799. msleep(delay_msec);
  800. }
  801. return rc;
  802. }
  803. /**
  804. * tpm2_probe() - probe TPM 2.0
  805. * @chip: TPM chip to use
  806. *
  807. * Return: < 0 error and 0 on success.
  808. *
  809. * Send idempotent TPM 2.0 command and see whether TPM 2.0 chip replied based on
  810. * the reply tag.
  811. */
  812. int tpm2_probe(struct tpm_chip *chip)
  813. {
  814. struct tpm2_cmd cmd;
  815. int rc;
  816. cmd.header.in = tpm2_get_tpm_pt_header;
  817. cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
  818. cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100);
  819. cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
  820. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, NULL);
  821. if (rc < 0)
  822. return rc;
  823. if (be16_to_cpu(cmd.header.out.tag) == TPM2_ST_NO_SESSIONS)
  824. chip->flags |= TPM_CHIP_FLAG_TPM2;
  825. return 0;
  826. }
  827. EXPORT_SYMBOL_GPL(tpm2_probe);
  828. /**
  829. * tpm2_auto_startup - Perform the standard automatic TPM initialization
  830. * sequence
  831. * @chip: TPM chip to use
  832. *
  833. * Returns 0 on success, < 0 in case of fatal error.
  834. */
  835. int tpm2_auto_startup(struct tpm_chip *chip)
  836. {
  837. int rc;
  838. rc = tpm_get_timeouts(chip);
  839. if (rc)
  840. goto out;
  841. rc = tpm2_do_selftest(chip);
  842. if (rc != 0 && rc != TPM2_RC_INITIALIZE) {
  843. dev_err(&chip->dev, "TPM self test failed\n");
  844. goto out;
  845. }
  846. if (rc == TPM2_RC_INITIALIZE) {
  847. rc = tpm2_startup(chip, TPM2_SU_CLEAR);
  848. if (rc)
  849. goto out;
  850. rc = tpm2_do_selftest(chip);
  851. if (rc) {
  852. dev_err(&chip->dev, "TPM self test failed\n");
  853. goto out;
  854. }
  855. }
  856. out:
  857. if (rc > 0)
  858. rc = -ENODEV;
  859. return rc;
  860. }