tpm2-cmd.c 26 KB

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