tpm2-cmd.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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_hash {
  27. unsigned int crypto_id;
  28. unsigned int tpm_id;
  29. };
  30. static struct tpm2_hash tpm2_hash_map[] = {
  31. {HASH_ALGO_SHA1, TPM2_ALG_SHA1},
  32. {HASH_ALGO_SHA256, TPM2_ALG_SHA256},
  33. {HASH_ALGO_SHA384, TPM2_ALG_SHA384},
  34. {HASH_ALGO_SHA512, TPM2_ALG_SHA512},
  35. {HASH_ALGO_SM3_256, TPM2_ALG_SM3_256},
  36. };
  37. /*
  38. * Array with one entry per ordinal defining the maximum amount
  39. * of time the chip could take to return the result. The values
  40. * of the SHORT, MEDIUM, and LONG durations are taken from the
  41. * PC Client Profile (PTP) specification.
  42. * LONG_LONG is for commands that generates keys which empirically
  43. * takes longer time on some systems.
  44. */
  45. static const u8 tpm2_ordinal_duration[TPM2_CC_LAST - TPM2_CC_FIRST + 1] = {
  46. TPM_UNDEFINED, /* 11F */
  47. TPM_UNDEFINED, /* 120 */
  48. TPM_LONG, /* 121 */
  49. TPM_UNDEFINED, /* 122 */
  50. TPM_UNDEFINED, /* 123 */
  51. TPM_UNDEFINED, /* 124 */
  52. TPM_UNDEFINED, /* 125 */
  53. TPM_UNDEFINED, /* 126 */
  54. TPM_UNDEFINED, /* 127 */
  55. TPM_UNDEFINED, /* 128 */
  56. TPM_LONG, /* 129 */
  57. TPM_UNDEFINED, /* 12a */
  58. TPM_UNDEFINED, /* 12b */
  59. TPM_UNDEFINED, /* 12c */
  60. TPM_UNDEFINED, /* 12d */
  61. TPM_UNDEFINED, /* 12e */
  62. TPM_UNDEFINED, /* 12f */
  63. TPM_UNDEFINED, /* 130 */
  64. TPM_LONG_LONG, /* 131 */
  65. TPM_UNDEFINED, /* 132 */
  66. TPM_UNDEFINED, /* 133 */
  67. TPM_UNDEFINED, /* 134 */
  68. TPM_UNDEFINED, /* 135 */
  69. TPM_UNDEFINED, /* 136 */
  70. TPM_UNDEFINED, /* 137 */
  71. TPM_UNDEFINED, /* 138 */
  72. TPM_UNDEFINED, /* 139 */
  73. TPM_UNDEFINED, /* 13a */
  74. TPM_UNDEFINED, /* 13b */
  75. TPM_UNDEFINED, /* 13c */
  76. TPM_UNDEFINED, /* 13d */
  77. TPM_MEDIUM, /* 13e */
  78. TPM_UNDEFINED, /* 13f */
  79. TPM_UNDEFINED, /* 140 */
  80. TPM_UNDEFINED, /* 141 */
  81. TPM_UNDEFINED, /* 142 */
  82. TPM_LONG, /* 143 */
  83. TPM_MEDIUM, /* 144 */
  84. TPM_UNDEFINED, /* 145 */
  85. TPM_UNDEFINED, /* 146 */
  86. TPM_UNDEFINED, /* 147 */
  87. TPM_UNDEFINED, /* 148 */
  88. TPM_UNDEFINED, /* 149 */
  89. TPM_UNDEFINED, /* 14a */
  90. TPM_UNDEFINED, /* 14b */
  91. TPM_UNDEFINED, /* 14c */
  92. TPM_UNDEFINED, /* 14d */
  93. TPM_LONG, /* 14e */
  94. TPM_UNDEFINED, /* 14f */
  95. TPM_UNDEFINED, /* 150 */
  96. TPM_UNDEFINED, /* 151 */
  97. TPM_UNDEFINED, /* 152 */
  98. TPM_LONG_LONG, /* 153 */
  99. TPM_UNDEFINED, /* 154 */
  100. TPM_UNDEFINED, /* 155 */
  101. TPM_UNDEFINED, /* 156 */
  102. TPM_UNDEFINED, /* 157 */
  103. TPM_UNDEFINED, /* 158 */
  104. TPM_UNDEFINED, /* 159 */
  105. TPM_UNDEFINED, /* 15a */
  106. TPM_UNDEFINED, /* 15b */
  107. TPM_MEDIUM, /* 15c */
  108. TPM_UNDEFINED, /* 15d */
  109. TPM_UNDEFINED, /* 15e */
  110. TPM_UNDEFINED, /* 15f */
  111. TPM_UNDEFINED, /* 160 */
  112. TPM_UNDEFINED, /* 161 */
  113. TPM_UNDEFINED, /* 162 */
  114. TPM_UNDEFINED, /* 163 */
  115. TPM_UNDEFINED, /* 164 */
  116. TPM_UNDEFINED, /* 165 */
  117. TPM_UNDEFINED, /* 166 */
  118. TPM_UNDEFINED, /* 167 */
  119. TPM_UNDEFINED, /* 168 */
  120. TPM_UNDEFINED, /* 169 */
  121. TPM_UNDEFINED, /* 16a */
  122. TPM_UNDEFINED, /* 16b */
  123. TPM_UNDEFINED, /* 16c */
  124. TPM_UNDEFINED, /* 16d */
  125. TPM_UNDEFINED, /* 16e */
  126. TPM_UNDEFINED, /* 16f */
  127. TPM_UNDEFINED, /* 170 */
  128. TPM_UNDEFINED, /* 171 */
  129. TPM_UNDEFINED, /* 172 */
  130. TPM_UNDEFINED, /* 173 */
  131. TPM_UNDEFINED, /* 174 */
  132. TPM_UNDEFINED, /* 175 */
  133. TPM_UNDEFINED, /* 176 */
  134. TPM_LONG, /* 177 */
  135. TPM_UNDEFINED, /* 178 */
  136. TPM_UNDEFINED, /* 179 */
  137. TPM_MEDIUM, /* 17a */
  138. TPM_LONG, /* 17b */
  139. TPM_UNDEFINED, /* 17c */
  140. TPM_UNDEFINED, /* 17d */
  141. TPM_UNDEFINED, /* 17e */
  142. TPM_UNDEFINED, /* 17f */
  143. TPM_UNDEFINED, /* 180 */
  144. TPM_UNDEFINED, /* 181 */
  145. TPM_MEDIUM, /* 182 */
  146. TPM_UNDEFINED, /* 183 */
  147. TPM_UNDEFINED, /* 184 */
  148. TPM_MEDIUM, /* 185 */
  149. TPM_MEDIUM, /* 186 */
  150. TPM_UNDEFINED, /* 187 */
  151. TPM_UNDEFINED, /* 188 */
  152. TPM_UNDEFINED, /* 189 */
  153. TPM_UNDEFINED, /* 18a */
  154. TPM_UNDEFINED, /* 18b */
  155. TPM_UNDEFINED, /* 18c */
  156. TPM_UNDEFINED, /* 18d */
  157. TPM_UNDEFINED, /* 18e */
  158. TPM_UNDEFINED /* 18f */
  159. };
  160. struct tpm2_pcr_read_out {
  161. __be32 update_cnt;
  162. __be32 pcr_selects_cnt;
  163. __be16 hash_alg;
  164. u8 pcr_select_size;
  165. u8 pcr_select[TPM2_PCR_SELECT_MIN];
  166. __be32 digests_cnt;
  167. __be16 digest_size;
  168. u8 digest[];
  169. } __packed;
  170. /**
  171. * tpm2_pcr_read() - read a PCR value
  172. * @chip: TPM chip to use.
  173. * @pcr_idx: index of the PCR to read.
  174. * @res_buf: buffer to store the resulting hash.
  175. *
  176. * Return: Same as with tpm_transmit_cmd.
  177. */
  178. int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  179. {
  180. int rc;
  181. struct tpm_buf buf;
  182. struct tpm2_pcr_read_out *out;
  183. u8 pcr_select[TPM2_PCR_SELECT_MIN] = {0};
  184. if (pcr_idx >= TPM2_PLATFORM_PCR)
  185. return -EINVAL;
  186. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_READ);
  187. if (rc)
  188. return rc;
  189. pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7);
  190. tpm_buf_append_u32(&buf, 1);
  191. tpm_buf_append_u16(&buf, TPM2_ALG_SHA1);
  192. tpm_buf_append_u8(&buf, TPM2_PCR_SELECT_MIN);
  193. tpm_buf_append(&buf, (const unsigned char *)pcr_select,
  194. sizeof(pcr_select));
  195. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
  196. res_buf ? "attempting to read a pcr value" : NULL);
  197. if (rc == 0 && res_buf) {
  198. out = (struct tpm2_pcr_read_out *)&buf.data[TPM_HEADER_SIZE];
  199. memcpy(res_buf, out->digest, SHA1_DIGEST_SIZE);
  200. }
  201. tpm_buf_destroy(&buf);
  202. return rc;
  203. }
  204. struct tpm2_null_auth_area {
  205. __be32 handle;
  206. __be16 nonce_size;
  207. u8 attributes;
  208. __be16 auth_size;
  209. } __packed;
  210. /**
  211. * tpm2_pcr_extend() - extend a PCR value
  212. *
  213. * @chip: TPM chip to use.
  214. * @pcr_idx: index of the PCR.
  215. * @count: number of digests passed.
  216. * @digests: list of pcr banks and corresponding digest values to extend.
  217. *
  218. * Return: Same as with tpm_transmit_cmd.
  219. */
  220. int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
  221. struct tpm2_digest *digests)
  222. {
  223. struct tpm_buf buf;
  224. struct tpm2_null_auth_area auth_area;
  225. int rc;
  226. int i;
  227. int j;
  228. if (count > ARRAY_SIZE(chip->active_banks))
  229. return -EINVAL;
  230. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_EXTEND);
  231. if (rc)
  232. return rc;
  233. tpm_buf_append_u32(&buf, pcr_idx);
  234. auth_area.handle = cpu_to_be32(TPM2_RS_PW);
  235. auth_area.nonce_size = 0;
  236. auth_area.attributes = 0;
  237. auth_area.auth_size = 0;
  238. tpm_buf_append_u32(&buf, sizeof(struct tpm2_null_auth_area));
  239. tpm_buf_append(&buf, (const unsigned char *)&auth_area,
  240. sizeof(auth_area));
  241. tpm_buf_append_u32(&buf, count);
  242. for (i = 0; i < count; i++) {
  243. for (j = 0; j < ARRAY_SIZE(tpm2_hash_map); j++) {
  244. if (digests[i].alg_id != tpm2_hash_map[j].tpm_id)
  245. continue;
  246. tpm_buf_append_u16(&buf, digests[i].alg_id);
  247. tpm_buf_append(&buf, (const unsigned char
  248. *)&digests[i].digest,
  249. hash_digest_size[tpm2_hash_map[j].crypto_id]);
  250. }
  251. }
  252. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
  253. "attempting extend a PCR value");
  254. tpm_buf_destroy(&buf);
  255. return rc;
  256. }
  257. struct tpm2_get_random_out {
  258. __be16 size;
  259. u8 buffer[TPM_MAX_RNG_DATA];
  260. } __packed;
  261. /**
  262. * tpm2_get_random() - get random bytes from the TPM RNG
  263. *
  264. * @chip: a &tpm_chip instance
  265. * @dest: destination buffer
  266. * @max: the max number of random bytes to pull
  267. *
  268. * Return:
  269. * size of the buffer on success,
  270. * -errno otherwise
  271. */
  272. int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
  273. {
  274. struct tpm2_get_random_out *out;
  275. struct tpm_buf buf;
  276. u32 recd;
  277. u32 num_bytes = max;
  278. int err;
  279. int total = 0;
  280. int retries = 5;
  281. u8 *dest_ptr = dest;
  282. if (!num_bytes || max > TPM_MAX_RNG_DATA)
  283. return -EINVAL;
  284. err = tpm_buf_init(&buf, 0, 0);
  285. if (err)
  286. return err;
  287. do {
  288. tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM);
  289. tpm_buf_append_u16(&buf, num_bytes);
  290. err = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
  291. offsetof(struct tpm2_get_random_out,
  292. buffer),
  293. 0, "attempting get random");
  294. if (err)
  295. goto out;
  296. out = (struct tpm2_get_random_out *)
  297. &buf.data[TPM_HEADER_SIZE];
  298. recd = min_t(u32, be16_to_cpu(out->size), num_bytes);
  299. if (tpm_buf_length(&buf) <
  300. offsetof(struct tpm2_get_random_out, buffer) + recd) {
  301. err = -EFAULT;
  302. goto out;
  303. }
  304. memcpy(dest_ptr, out->buffer, recd);
  305. dest_ptr += recd;
  306. total += recd;
  307. num_bytes -= recd;
  308. } while (retries-- && total < max);
  309. tpm_buf_destroy(&buf);
  310. return total ? total : -EIO;
  311. out:
  312. tpm_buf_destroy(&buf);
  313. return err;
  314. }
  315. /**
  316. * tpm2_flush_context_cmd() - execute a TPM2_FlushContext command
  317. * @chip: TPM chip to use
  318. * @payload: the key data in clear and encrypted form
  319. * @options: authentication values and other options
  320. *
  321. * Return: same as with tpm_transmit_cmd
  322. */
  323. void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
  324. unsigned int flags)
  325. {
  326. struct tpm_buf buf;
  327. int rc;
  328. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT);
  329. if (rc) {
  330. dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
  331. handle);
  332. return;
  333. }
  334. tpm_buf_append_u32(&buf, handle);
  335. (void) tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, flags,
  336. "flushing context");
  337. tpm_buf_destroy(&buf);
  338. }
  339. /**
  340. * tpm_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
  341. *
  342. * @buf: an allocated tpm_buf instance
  343. * @session_handle: session handle
  344. * @nonce: the session nonce, may be NULL if not used
  345. * @nonce_len: the session nonce length, may be 0 if not used
  346. * @attributes: the session attributes
  347. * @hmac: the session HMAC or password, may be NULL if not used
  348. * @hmac_len: the session HMAC or password length, maybe 0 if not used
  349. */
  350. static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,
  351. const u8 *nonce, u16 nonce_len,
  352. u8 attributes,
  353. const u8 *hmac, u16 hmac_len)
  354. {
  355. tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);
  356. tpm_buf_append_u32(buf, session_handle);
  357. tpm_buf_append_u16(buf, nonce_len);
  358. if (nonce && nonce_len)
  359. tpm_buf_append(buf, nonce, nonce_len);
  360. tpm_buf_append_u8(buf, attributes);
  361. tpm_buf_append_u16(buf, hmac_len);
  362. if (hmac && hmac_len)
  363. tpm_buf_append(buf, hmac, hmac_len);
  364. }
  365. /**
  366. * tpm2_seal_trusted() - seal the payload of a trusted key
  367. *
  368. * @chip: TPM chip to use
  369. * @payload: the key data in clear and encrypted form
  370. * @options: authentication values and other options
  371. *
  372. * Return: < 0 on error and 0 on success.
  373. */
  374. int tpm2_seal_trusted(struct tpm_chip *chip,
  375. struct trusted_key_payload *payload,
  376. struct trusted_key_options *options)
  377. {
  378. unsigned int blob_len;
  379. struct tpm_buf buf;
  380. u32 hash;
  381. int i;
  382. int rc;
  383. for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
  384. if (options->hash == tpm2_hash_map[i].crypto_id) {
  385. hash = tpm2_hash_map[i].tpm_id;
  386. break;
  387. }
  388. }
  389. if (i == ARRAY_SIZE(tpm2_hash_map))
  390. return -EINVAL;
  391. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
  392. if (rc)
  393. return rc;
  394. tpm_buf_append_u32(&buf, options->keyhandle);
  395. tpm2_buf_append_auth(&buf, TPM2_RS_PW,
  396. NULL /* nonce */, 0,
  397. 0 /* session_attributes */,
  398. options->keyauth /* hmac */,
  399. TPM_DIGEST_SIZE);
  400. /* sensitive */
  401. tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1);
  402. tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE);
  403. tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE);
  404. tpm_buf_append_u16(&buf, payload->key_len + 1);
  405. tpm_buf_append(&buf, payload->key, payload->key_len);
  406. tpm_buf_append_u8(&buf, payload->migratable);
  407. /* public */
  408. tpm_buf_append_u16(&buf, 14 + options->policydigest_len);
  409. tpm_buf_append_u16(&buf, TPM2_ALG_KEYEDHASH);
  410. tpm_buf_append_u16(&buf, hash);
  411. /* policy */
  412. if (options->policydigest_len) {
  413. tpm_buf_append_u32(&buf, 0);
  414. tpm_buf_append_u16(&buf, options->policydigest_len);
  415. tpm_buf_append(&buf, options->policydigest,
  416. options->policydigest_len);
  417. } else {
  418. tpm_buf_append_u32(&buf, TPM2_OA_USER_WITH_AUTH);
  419. tpm_buf_append_u16(&buf, 0);
  420. }
  421. /* public parameters */
  422. tpm_buf_append_u16(&buf, TPM2_ALG_NULL);
  423. tpm_buf_append_u16(&buf, 0);
  424. /* outside info */
  425. tpm_buf_append_u16(&buf, 0);
  426. /* creation PCR */
  427. tpm_buf_append_u32(&buf, 0);
  428. if (buf.flags & TPM_BUF_OVERFLOW) {
  429. rc = -E2BIG;
  430. goto out;
  431. }
  432. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, 0,
  433. "sealing data");
  434. if (rc)
  435. goto out;
  436. blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]);
  437. if (blob_len > MAX_BLOB_SIZE) {
  438. rc = -E2BIG;
  439. goto out;
  440. }
  441. if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 4 + blob_len) {
  442. rc = -EFAULT;
  443. goto out;
  444. }
  445. memcpy(payload->blob, &buf.data[TPM_HEADER_SIZE + 4], blob_len);
  446. payload->blob_len = blob_len;
  447. out:
  448. tpm_buf_destroy(&buf);
  449. if (rc > 0) {
  450. if (tpm2_rc_value(rc) == TPM2_RC_HASH)
  451. rc = -EINVAL;
  452. else
  453. rc = -EPERM;
  454. }
  455. return rc;
  456. }
  457. /**
  458. * tpm2_load_cmd() - execute a TPM2_Load command
  459. *
  460. * @chip: TPM chip to use
  461. * @payload: the key data in clear and encrypted form
  462. * @options: authentication values and other options
  463. * @blob_handle: returned blob handle
  464. * @flags: tpm transmit flags
  465. *
  466. * Return: 0 on success.
  467. * -E2BIG on wrong payload size.
  468. * -EPERM on tpm error status.
  469. * < 0 error from tpm_transmit_cmd.
  470. */
  471. static int tpm2_load_cmd(struct tpm_chip *chip,
  472. struct trusted_key_payload *payload,
  473. struct trusted_key_options *options,
  474. u32 *blob_handle, unsigned int flags)
  475. {
  476. struct tpm_buf buf;
  477. unsigned int private_len;
  478. unsigned int public_len;
  479. unsigned int blob_len;
  480. int rc;
  481. private_len = be16_to_cpup((__be16 *) &payload->blob[0]);
  482. if (private_len > (payload->blob_len - 2))
  483. return -E2BIG;
  484. public_len = be16_to_cpup((__be16 *) &payload->blob[2 + private_len]);
  485. blob_len = private_len + public_len + 4;
  486. if (blob_len > payload->blob_len)
  487. return -E2BIG;
  488. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
  489. if (rc)
  490. return rc;
  491. tpm_buf_append_u32(&buf, options->keyhandle);
  492. tpm2_buf_append_auth(&buf, TPM2_RS_PW,
  493. NULL /* nonce */, 0,
  494. 0 /* session_attributes */,
  495. options->keyauth /* hmac */,
  496. TPM_DIGEST_SIZE);
  497. tpm_buf_append(&buf, payload->blob, blob_len);
  498. if (buf.flags & TPM_BUF_OVERFLOW) {
  499. rc = -E2BIG;
  500. goto out;
  501. }
  502. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, flags,
  503. "loading blob");
  504. if (!rc)
  505. *blob_handle = be32_to_cpup(
  506. (__be32 *) &buf.data[TPM_HEADER_SIZE]);
  507. out:
  508. tpm_buf_destroy(&buf);
  509. if (rc > 0)
  510. rc = -EPERM;
  511. return rc;
  512. }
  513. /**
  514. * tpm2_unseal_cmd() - execute a TPM2_Unload command
  515. *
  516. * @chip: TPM chip to use
  517. * @payload: the key data in clear and encrypted form
  518. * @options: authentication values and other options
  519. * @blob_handle: blob handle
  520. * @flags: tpm_transmit_cmd flags
  521. *
  522. * Return: 0 on success
  523. * -EPERM on tpm error status
  524. * < 0 error from tpm_transmit_cmd
  525. */
  526. static int tpm2_unseal_cmd(struct tpm_chip *chip,
  527. struct trusted_key_payload *payload,
  528. struct trusted_key_options *options,
  529. u32 blob_handle, unsigned int flags)
  530. {
  531. struct tpm_buf buf;
  532. u16 data_len;
  533. u8 *data;
  534. int rc;
  535. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
  536. if (rc)
  537. return rc;
  538. tpm_buf_append_u32(&buf, blob_handle);
  539. tpm2_buf_append_auth(&buf,
  540. options->policyhandle ?
  541. options->policyhandle : TPM2_RS_PW,
  542. NULL /* nonce */, 0,
  543. TPM2_SA_CONTINUE_SESSION,
  544. options->blobauth /* hmac */,
  545. TPM_DIGEST_SIZE);
  546. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 6, flags,
  547. "unsealing");
  548. if (rc > 0)
  549. rc = -EPERM;
  550. if (!rc) {
  551. data_len = be16_to_cpup(
  552. (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
  553. if (data_len < MIN_KEY_SIZE || data_len > MAX_KEY_SIZE + 1) {
  554. rc = -EFAULT;
  555. goto out;
  556. }
  557. if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 6 + data_len) {
  558. rc = -EFAULT;
  559. goto out;
  560. }
  561. data = &buf.data[TPM_HEADER_SIZE + 6];
  562. memcpy(payload->key, data, data_len - 1);
  563. payload->key_len = data_len - 1;
  564. payload->migratable = data[data_len - 1];
  565. }
  566. out:
  567. tpm_buf_destroy(&buf);
  568. return rc;
  569. }
  570. /**
  571. * tpm2_unseal_trusted() - unseal the payload of a trusted key
  572. *
  573. * @chip: TPM chip to use
  574. * @payload: the key data in clear and encrypted form
  575. * @options: authentication values and other options
  576. *
  577. * Return: Same as with tpm_transmit_cmd.
  578. */
  579. int tpm2_unseal_trusted(struct tpm_chip *chip,
  580. struct trusted_key_payload *payload,
  581. struct trusted_key_options *options)
  582. {
  583. u32 blob_handle;
  584. int rc;
  585. mutex_lock(&chip->tpm_mutex);
  586. rc = tpm2_load_cmd(chip, payload, options, &blob_handle,
  587. TPM_TRANSMIT_UNLOCKED);
  588. if (rc)
  589. goto out;
  590. rc = tpm2_unseal_cmd(chip, payload, options, blob_handle,
  591. TPM_TRANSMIT_UNLOCKED);
  592. tpm2_flush_context_cmd(chip, blob_handle, TPM_TRANSMIT_UNLOCKED);
  593. out:
  594. mutex_unlock(&chip->tpm_mutex);
  595. return rc;
  596. }
  597. struct tpm2_get_cap_out {
  598. u8 more_data;
  599. __be32 subcap_id;
  600. __be32 property_cnt;
  601. __be32 property_id;
  602. __be32 value;
  603. } __packed;
  604. /**
  605. * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
  606. * @chip: a &tpm_chip instance
  607. * @property_id: property ID.
  608. * @value: output variable.
  609. * @desc: passed to tpm_transmit_cmd()
  610. *
  611. * Return:
  612. * 0 on success,
  613. * -errno or a TPM return code otherwise
  614. */
  615. ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
  616. const char *desc)
  617. {
  618. struct tpm2_get_cap_out *out;
  619. struct tpm_buf buf;
  620. int rc;
  621. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
  622. if (rc)
  623. return rc;
  624. tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
  625. tpm_buf_append_u32(&buf, property_id);
  626. tpm_buf_append_u32(&buf, 1);
  627. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, NULL);
  628. if (!rc) {
  629. out = (struct tpm2_get_cap_out *)
  630. &buf.data[TPM_HEADER_SIZE];
  631. *value = be32_to_cpu(out->value);
  632. }
  633. tpm_buf_destroy(&buf);
  634. return rc;
  635. }
  636. EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
  637. /**
  638. * tpm2_shutdown() - send a TPM shutdown command
  639. *
  640. * Sends a TPM shutdown command. The shutdown command is used in call
  641. * sites where the system is going down. If it fails, there is not much
  642. * that can be done except print an error message.
  643. *
  644. * @chip: a &tpm_chip instance
  645. * @shutdown_type: TPM_SU_CLEAR or TPM_SU_STATE.
  646. */
  647. void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
  648. {
  649. struct tpm_buf buf;
  650. int rc;
  651. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SHUTDOWN);
  652. if (rc)
  653. return;
  654. tpm_buf_append_u16(&buf, shutdown_type);
  655. tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
  656. "stopping the TPM");
  657. tpm_buf_destroy(&buf);
  658. }
  659. /*
  660. * tpm2_calc_ordinal_duration() - maximum duration for a command
  661. *
  662. * @chip: TPM chip to use.
  663. * @ordinal: command code number.
  664. *
  665. * Return: maximum duration for a command
  666. */
  667. unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
  668. {
  669. int index = TPM_UNDEFINED;
  670. int duration = 0;
  671. if (ordinal >= TPM2_CC_FIRST && ordinal <= TPM2_CC_LAST)
  672. index = tpm2_ordinal_duration[ordinal - TPM2_CC_FIRST];
  673. if (index != TPM_UNDEFINED)
  674. duration = chip->duration[index];
  675. if (duration <= 0)
  676. duration = msecs_to_jiffies(TPM2_DURATION_DEFAULT);
  677. return duration;
  678. }
  679. EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration);
  680. /**
  681. * tpm2_do_selftest() - ensure that all self tests have passed
  682. *
  683. * @chip: TPM chip to use
  684. *
  685. * Return: Same as with tpm_transmit_cmd.
  686. *
  687. * The TPM can either run all self tests synchronously and then return
  688. * RC_SUCCESS once all tests were successful. Or it can choose to run the tests
  689. * asynchronously and return RC_TESTING immediately while the self tests still
  690. * execute in the background. This function handles both cases and waits until
  691. * all tests have completed.
  692. */
  693. static int tpm2_do_selftest(struct tpm_chip *chip)
  694. {
  695. struct tpm_buf buf;
  696. int full;
  697. int rc;
  698. for (full = 0; full < 2; full++) {
  699. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SELF_TEST);
  700. if (rc)
  701. return rc;
  702. tpm_buf_append_u8(&buf, full);
  703. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
  704. "attempting the self test");
  705. tpm_buf_destroy(&buf);
  706. if (rc == TPM2_RC_TESTING)
  707. rc = TPM2_RC_SUCCESS;
  708. if (rc == TPM2_RC_INITIALIZE || rc == TPM2_RC_SUCCESS)
  709. return rc;
  710. }
  711. return rc;
  712. }
  713. /**
  714. * tpm2_probe() - probe for the TPM 2.0 protocol
  715. * @chip: a &tpm_chip instance
  716. *
  717. * Send an idempotent TPM 2.0 command and see whether there is TPM2 chip in the
  718. * other end based on the response tag. The flag TPM_CHIP_FLAG_TPM2 is set by
  719. * this function if this is the case.
  720. *
  721. * Return:
  722. * 0 on success,
  723. * -errno otherwise
  724. */
  725. int tpm2_probe(struct tpm_chip *chip)
  726. {
  727. struct tpm_output_header *out;
  728. struct tpm_buf buf;
  729. int rc;
  730. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
  731. if (rc)
  732. return rc;
  733. tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
  734. tpm_buf_append_u32(&buf, TPM_PT_TOTAL_COMMANDS);
  735. tpm_buf_append_u32(&buf, 1);
  736. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, NULL);
  737. /* We ignore TPM return codes on purpose. */
  738. if (rc >= 0) {
  739. out = (struct tpm_output_header *)buf.data;
  740. if (be16_to_cpu(out->tag) == TPM2_ST_NO_SESSIONS)
  741. chip->flags |= TPM_CHIP_FLAG_TPM2;
  742. }
  743. tpm_buf_destroy(&buf);
  744. return 0;
  745. }
  746. EXPORT_SYMBOL_GPL(tpm2_probe);
  747. struct tpm2_pcr_selection {
  748. __be16 hash_alg;
  749. u8 size_of_select;
  750. u8 pcr_select[3];
  751. } __packed;
  752. static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
  753. {
  754. struct tpm2_pcr_selection pcr_selection;
  755. struct tpm_buf buf;
  756. void *marker;
  757. void *end;
  758. void *pcr_select_offset;
  759. unsigned int count;
  760. u32 sizeof_pcr_selection;
  761. u32 rsp_len;
  762. int rc;
  763. int i = 0;
  764. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
  765. if (rc)
  766. return rc;
  767. tpm_buf_append_u32(&buf, TPM2_CAP_PCRS);
  768. tpm_buf_append_u32(&buf, 0);
  769. tpm_buf_append_u32(&buf, 1);
  770. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 9, 0,
  771. "get tpm pcr allocation");
  772. if (rc)
  773. goto out;
  774. count = be32_to_cpup(
  775. (__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);
  776. if (count > ARRAY_SIZE(chip->active_banks)) {
  777. rc = -ENODEV;
  778. goto out;
  779. }
  780. marker = &buf.data[TPM_HEADER_SIZE + 9];
  781. rsp_len = be32_to_cpup((__be32 *)&buf.data[2]);
  782. end = &buf.data[rsp_len];
  783. for (i = 0; i < count; i++) {
  784. pcr_select_offset = marker +
  785. offsetof(struct tpm2_pcr_selection, size_of_select);
  786. if (pcr_select_offset >= end) {
  787. rc = -EFAULT;
  788. break;
  789. }
  790. memcpy(&pcr_selection, marker, sizeof(pcr_selection));
  791. chip->active_banks[i] = be16_to_cpu(pcr_selection.hash_alg);
  792. sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
  793. sizeof(pcr_selection.size_of_select) +
  794. pcr_selection.size_of_select;
  795. marker = marker + sizeof_pcr_selection;
  796. }
  797. out:
  798. if (i < ARRAY_SIZE(chip->active_banks))
  799. chip->active_banks[i] = TPM2_ALG_ERROR;
  800. tpm_buf_destroy(&buf);
  801. return rc;
  802. }
  803. static int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip)
  804. {
  805. struct tpm_buf buf;
  806. u32 nr_commands;
  807. __be32 *attrs;
  808. u32 cc;
  809. int i;
  810. int rc;
  811. rc = tpm2_get_tpm_pt(chip, TPM_PT_TOTAL_COMMANDS, &nr_commands, NULL);
  812. if (rc)
  813. goto out;
  814. if (nr_commands > 0xFFFFF) {
  815. rc = -EFAULT;
  816. goto out;
  817. }
  818. chip->cc_attrs_tbl = devm_kcalloc(&chip->dev, 4, nr_commands,
  819. GFP_KERNEL);
  820. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
  821. if (rc)
  822. goto out;
  823. tpm_buf_append_u32(&buf, TPM2_CAP_COMMANDS);
  824. tpm_buf_append_u32(&buf, TPM2_CC_FIRST);
  825. tpm_buf_append_u32(&buf, nr_commands);
  826. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
  827. 9 + 4 * nr_commands, 0, NULL);
  828. if (rc) {
  829. tpm_buf_destroy(&buf);
  830. goto out;
  831. }
  832. if (nr_commands !=
  833. be32_to_cpup((__be32 *)&buf.data[TPM_HEADER_SIZE + 5])) {
  834. tpm_buf_destroy(&buf);
  835. goto out;
  836. }
  837. chip->nr_commands = nr_commands;
  838. attrs = (__be32 *)&buf.data[TPM_HEADER_SIZE + 9];
  839. for (i = 0; i < nr_commands; i++, attrs++) {
  840. chip->cc_attrs_tbl[i] = be32_to_cpup(attrs);
  841. cc = chip->cc_attrs_tbl[i] & 0xFFFF;
  842. if (cc == TPM2_CC_CONTEXT_SAVE || cc == TPM2_CC_FLUSH_CONTEXT) {
  843. chip->cc_attrs_tbl[i] &=
  844. ~(GENMASK(2, 0) << TPM2_CC_ATTR_CHANDLES);
  845. chip->cc_attrs_tbl[i] |= 1 << TPM2_CC_ATTR_CHANDLES;
  846. }
  847. }
  848. tpm_buf_destroy(&buf);
  849. out:
  850. if (rc > 0)
  851. rc = -ENODEV;
  852. return rc;
  853. }
  854. /**
  855. * tpm2_auto_startup - Perform the standard automatic TPM initialization
  856. * sequence
  857. * @chip: TPM chip to use
  858. *
  859. * Returns 0 on success, < 0 in case of fatal error.
  860. */
  861. int tpm2_auto_startup(struct tpm_chip *chip)
  862. {
  863. int rc;
  864. rc = tpm_get_timeouts(chip);
  865. if (rc)
  866. goto out;
  867. rc = tpm2_do_selftest(chip);
  868. if (rc && rc != TPM2_RC_INITIALIZE)
  869. goto out;
  870. if (rc == TPM2_RC_INITIALIZE) {
  871. rc = tpm_startup(chip);
  872. if (rc)
  873. goto out;
  874. rc = tpm2_do_selftest(chip);
  875. if (rc)
  876. goto out;
  877. }
  878. rc = tpm2_get_pcr_allocation(chip);
  879. if (rc)
  880. goto out;
  881. rc = tpm2_get_cc_attrs_tbl(chip);
  882. out:
  883. if (rc > 0)
  884. rc = -ENODEV;
  885. return rc;
  886. }
  887. int tpm2_find_cc(struct tpm_chip *chip, u32 cc)
  888. {
  889. int i;
  890. for (i = 0; i < chip->nr_commands; i++)
  891. if (cc == (chip->cc_attrs_tbl[i] & GENMASK(15, 0)))
  892. return i;
  893. return -1;
  894. }