tpm2-cmd.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. TPM_HEADER_SIZE +
  301. offsetof(struct tpm2_get_random_out, buffer) +
  302. recd) {
  303. err = -EFAULT;
  304. goto out;
  305. }
  306. memcpy(dest_ptr, out->buffer, recd);
  307. dest_ptr += recd;
  308. total += recd;
  309. num_bytes -= recd;
  310. } while (retries-- && total < max);
  311. tpm_buf_destroy(&buf);
  312. return total ? total : -EIO;
  313. out:
  314. tpm_buf_destroy(&buf);
  315. return err;
  316. }
  317. /**
  318. * tpm2_flush_context_cmd() - execute a TPM2_FlushContext command
  319. * @chip: TPM chip to use
  320. * @payload: the key data in clear and encrypted form
  321. * @options: authentication values and other options
  322. *
  323. * Return: same as with tpm_transmit_cmd
  324. */
  325. void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
  326. unsigned int flags)
  327. {
  328. struct tpm_buf buf;
  329. int rc;
  330. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT);
  331. if (rc) {
  332. dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
  333. handle);
  334. return;
  335. }
  336. tpm_buf_append_u32(&buf, handle);
  337. (void) tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, flags,
  338. "flushing context");
  339. tpm_buf_destroy(&buf);
  340. }
  341. /**
  342. * tpm_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
  343. *
  344. * @buf: an allocated tpm_buf instance
  345. * @session_handle: session handle
  346. * @nonce: the session nonce, may be NULL if not used
  347. * @nonce_len: the session nonce length, may be 0 if not used
  348. * @attributes: the session attributes
  349. * @hmac: the session HMAC or password, may be NULL if not used
  350. * @hmac_len: the session HMAC or password length, maybe 0 if not used
  351. */
  352. static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,
  353. const u8 *nonce, u16 nonce_len,
  354. u8 attributes,
  355. const u8 *hmac, u16 hmac_len)
  356. {
  357. tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);
  358. tpm_buf_append_u32(buf, session_handle);
  359. tpm_buf_append_u16(buf, nonce_len);
  360. if (nonce && nonce_len)
  361. tpm_buf_append(buf, nonce, nonce_len);
  362. tpm_buf_append_u8(buf, attributes);
  363. tpm_buf_append_u16(buf, hmac_len);
  364. if (hmac && hmac_len)
  365. tpm_buf_append(buf, hmac, hmac_len);
  366. }
  367. /**
  368. * tpm2_seal_trusted() - seal the payload of a trusted key
  369. *
  370. * @chip: TPM chip to use
  371. * @payload: the key data in clear and encrypted form
  372. * @options: authentication values and other options
  373. *
  374. * Return: < 0 on error and 0 on success.
  375. */
  376. int tpm2_seal_trusted(struct tpm_chip *chip,
  377. struct trusted_key_payload *payload,
  378. struct trusted_key_options *options)
  379. {
  380. unsigned int blob_len;
  381. struct tpm_buf buf;
  382. u32 hash;
  383. int i;
  384. int rc;
  385. for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
  386. if (options->hash == tpm2_hash_map[i].crypto_id) {
  387. hash = tpm2_hash_map[i].tpm_id;
  388. break;
  389. }
  390. }
  391. if (i == ARRAY_SIZE(tpm2_hash_map))
  392. return -EINVAL;
  393. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
  394. if (rc)
  395. return rc;
  396. tpm_buf_append_u32(&buf, options->keyhandle);
  397. tpm2_buf_append_auth(&buf, TPM2_RS_PW,
  398. NULL /* nonce */, 0,
  399. 0 /* session_attributes */,
  400. options->keyauth /* hmac */,
  401. TPM_DIGEST_SIZE);
  402. /* sensitive */
  403. tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1);
  404. tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE);
  405. tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE);
  406. tpm_buf_append_u16(&buf, payload->key_len + 1);
  407. tpm_buf_append(&buf, payload->key, payload->key_len);
  408. tpm_buf_append_u8(&buf, payload->migratable);
  409. /* public */
  410. tpm_buf_append_u16(&buf, 14 + options->policydigest_len);
  411. tpm_buf_append_u16(&buf, TPM2_ALG_KEYEDHASH);
  412. tpm_buf_append_u16(&buf, hash);
  413. /* policy */
  414. if (options->policydigest_len) {
  415. tpm_buf_append_u32(&buf, 0);
  416. tpm_buf_append_u16(&buf, options->policydigest_len);
  417. tpm_buf_append(&buf, options->policydigest,
  418. options->policydigest_len);
  419. } else {
  420. tpm_buf_append_u32(&buf, TPM2_OA_USER_WITH_AUTH);
  421. tpm_buf_append_u16(&buf, 0);
  422. }
  423. /* public parameters */
  424. tpm_buf_append_u16(&buf, TPM2_ALG_NULL);
  425. tpm_buf_append_u16(&buf, 0);
  426. /* outside info */
  427. tpm_buf_append_u16(&buf, 0);
  428. /* creation PCR */
  429. tpm_buf_append_u32(&buf, 0);
  430. if (buf.flags & TPM_BUF_OVERFLOW) {
  431. rc = -E2BIG;
  432. goto out;
  433. }
  434. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, 0,
  435. "sealing data");
  436. if (rc)
  437. goto out;
  438. blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]);
  439. if (blob_len > MAX_BLOB_SIZE) {
  440. rc = -E2BIG;
  441. goto out;
  442. }
  443. if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 4 + blob_len) {
  444. rc = -EFAULT;
  445. goto out;
  446. }
  447. memcpy(payload->blob, &buf.data[TPM_HEADER_SIZE + 4], blob_len);
  448. payload->blob_len = blob_len;
  449. out:
  450. tpm_buf_destroy(&buf);
  451. if (rc > 0) {
  452. if (tpm2_rc_value(rc) == TPM2_RC_HASH)
  453. rc = -EINVAL;
  454. else
  455. rc = -EPERM;
  456. }
  457. return rc;
  458. }
  459. /**
  460. * tpm2_load_cmd() - execute a TPM2_Load command
  461. *
  462. * @chip: TPM chip to use
  463. * @payload: the key data in clear and encrypted form
  464. * @options: authentication values and other options
  465. * @blob_handle: returned blob handle
  466. * @flags: tpm transmit flags
  467. *
  468. * Return: 0 on success.
  469. * -E2BIG on wrong payload size.
  470. * -EPERM on tpm error status.
  471. * < 0 error from tpm_transmit_cmd.
  472. */
  473. static int tpm2_load_cmd(struct tpm_chip *chip,
  474. struct trusted_key_payload *payload,
  475. struct trusted_key_options *options,
  476. u32 *blob_handle, unsigned int flags)
  477. {
  478. struct tpm_buf buf;
  479. unsigned int private_len;
  480. unsigned int public_len;
  481. unsigned int blob_len;
  482. int rc;
  483. private_len = be16_to_cpup((__be16 *) &payload->blob[0]);
  484. if (private_len > (payload->blob_len - 2))
  485. return -E2BIG;
  486. public_len = be16_to_cpup((__be16 *) &payload->blob[2 + private_len]);
  487. blob_len = private_len + public_len + 4;
  488. if (blob_len > payload->blob_len)
  489. return -E2BIG;
  490. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
  491. if (rc)
  492. return rc;
  493. tpm_buf_append_u32(&buf, options->keyhandle);
  494. tpm2_buf_append_auth(&buf, TPM2_RS_PW,
  495. NULL /* nonce */, 0,
  496. 0 /* session_attributes */,
  497. options->keyauth /* hmac */,
  498. TPM_DIGEST_SIZE);
  499. tpm_buf_append(&buf, payload->blob, blob_len);
  500. if (buf.flags & TPM_BUF_OVERFLOW) {
  501. rc = -E2BIG;
  502. goto out;
  503. }
  504. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, flags,
  505. "loading blob");
  506. if (!rc)
  507. *blob_handle = be32_to_cpup(
  508. (__be32 *) &buf.data[TPM_HEADER_SIZE]);
  509. out:
  510. tpm_buf_destroy(&buf);
  511. if (rc > 0)
  512. rc = -EPERM;
  513. return rc;
  514. }
  515. /**
  516. * tpm2_unseal_cmd() - execute a TPM2_Unload command
  517. *
  518. * @chip: TPM chip to use
  519. * @payload: the key data in clear and encrypted form
  520. * @options: authentication values and other options
  521. * @blob_handle: blob handle
  522. * @flags: tpm_transmit_cmd flags
  523. *
  524. * Return: 0 on success
  525. * -EPERM on tpm error status
  526. * < 0 error from tpm_transmit_cmd
  527. */
  528. static int tpm2_unseal_cmd(struct tpm_chip *chip,
  529. struct trusted_key_payload *payload,
  530. struct trusted_key_options *options,
  531. u32 blob_handle, unsigned int flags)
  532. {
  533. struct tpm_buf buf;
  534. u16 data_len;
  535. u8 *data;
  536. int rc;
  537. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
  538. if (rc)
  539. return rc;
  540. tpm_buf_append_u32(&buf, blob_handle);
  541. tpm2_buf_append_auth(&buf,
  542. options->policyhandle ?
  543. options->policyhandle : TPM2_RS_PW,
  544. NULL /* nonce */, 0,
  545. TPM2_SA_CONTINUE_SESSION,
  546. options->blobauth /* hmac */,
  547. TPM_DIGEST_SIZE);
  548. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 6, flags,
  549. "unsealing");
  550. if (rc > 0)
  551. rc = -EPERM;
  552. if (!rc) {
  553. data_len = be16_to_cpup(
  554. (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
  555. if (data_len < MIN_KEY_SIZE || data_len > MAX_KEY_SIZE + 1) {
  556. rc = -EFAULT;
  557. goto out;
  558. }
  559. if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 6 + data_len) {
  560. rc = -EFAULT;
  561. goto out;
  562. }
  563. data = &buf.data[TPM_HEADER_SIZE + 6];
  564. memcpy(payload->key, data, data_len - 1);
  565. payload->key_len = data_len - 1;
  566. payload->migratable = data[data_len - 1];
  567. }
  568. out:
  569. tpm_buf_destroy(&buf);
  570. return rc;
  571. }
  572. /**
  573. * tpm2_unseal_trusted() - unseal the payload of a trusted key
  574. *
  575. * @chip: TPM chip to use
  576. * @payload: the key data in clear and encrypted form
  577. * @options: authentication values and other options
  578. *
  579. * Return: Same as with tpm_transmit_cmd.
  580. */
  581. int tpm2_unseal_trusted(struct tpm_chip *chip,
  582. struct trusted_key_payload *payload,
  583. struct trusted_key_options *options)
  584. {
  585. u32 blob_handle;
  586. int rc;
  587. mutex_lock(&chip->tpm_mutex);
  588. rc = tpm2_load_cmd(chip, payload, options, &blob_handle,
  589. TPM_TRANSMIT_UNLOCKED);
  590. if (rc)
  591. goto out;
  592. rc = tpm2_unseal_cmd(chip, payload, options, blob_handle,
  593. TPM_TRANSMIT_UNLOCKED);
  594. tpm2_flush_context_cmd(chip, blob_handle, TPM_TRANSMIT_UNLOCKED);
  595. out:
  596. mutex_unlock(&chip->tpm_mutex);
  597. return rc;
  598. }
  599. struct tpm2_get_cap_out {
  600. u8 more_data;
  601. __be32 subcap_id;
  602. __be32 property_cnt;
  603. __be32 property_id;
  604. __be32 value;
  605. } __packed;
  606. /**
  607. * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
  608. * @chip: a &tpm_chip instance
  609. * @property_id: property ID.
  610. * @value: output variable.
  611. * @desc: passed to tpm_transmit_cmd()
  612. *
  613. * Return:
  614. * 0 on success,
  615. * -errno or a TPM return code otherwise
  616. */
  617. ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
  618. const char *desc)
  619. {
  620. struct tpm2_get_cap_out *out;
  621. struct tpm_buf buf;
  622. int rc;
  623. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
  624. if (rc)
  625. return rc;
  626. tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
  627. tpm_buf_append_u32(&buf, property_id);
  628. tpm_buf_append_u32(&buf, 1);
  629. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, NULL);
  630. if (!rc) {
  631. out = (struct tpm2_get_cap_out *)
  632. &buf.data[TPM_HEADER_SIZE];
  633. *value = be32_to_cpu(out->value);
  634. }
  635. tpm_buf_destroy(&buf);
  636. return rc;
  637. }
  638. EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
  639. /**
  640. * tpm2_shutdown() - send a TPM shutdown command
  641. *
  642. * Sends a TPM shutdown command. The shutdown command is used in call
  643. * sites where the system is going down. If it fails, there is not much
  644. * that can be done except print an error message.
  645. *
  646. * @chip: a &tpm_chip instance
  647. * @shutdown_type: TPM_SU_CLEAR or TPM_SU_STATE.
  648. */
  649. void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
  650. {
  651. struct tpm_buf buf;
  652. int rc;
  653. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SHUTDOWN);
  654. if (rc)
  655. return;
  656. tpm_buf_append_u16(&buf, shutdown_type);
  657. tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
  658. "stopping the TPM");
  659. tpm_buf_destroy(&buf);
  660. }
  661. /*
  662. * tpm2_calc_ordinal_duration() - maximum duration for a command
  663. *
  664. * @chip: TPM chip to use.
  665. * @ordinal: command code number.
  666. *
  667. * Return: maximum duration for a command
  668. */
  669. unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
  670. {
  671. int index = TPM_UNDEFINED;
  672. int duration = 0;
  673. if (ordinal >= TPM2_CC_FIRST && ordinal <= TPM2_CC_LAST)
  674. index = tpm2_ordinal_duration[ordinal - TPM2_CC_FIRST];
  675. if (index != TPM_UNDEFINED)
  676. duration = chip->duration[index];
  677. if (duration <= 0)
  678. duration = msecs_to_jiffies(TPM2_DURATION_DEFAULT);
  679. return duration;
  680. }
  681. EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration);
  682. /**
  683. * tpm2_do_selftest() - ensure that all self tests have passed
  684. *
  685. * @chip: TPM chip to use
  686. *
  687. * Return: Same as with tpm_transmit_cmd.
  688. *
  689. * The TPM can either run all self tests synchronously and then return
  690. * RC_SUCCESS once all tests were successful. Or it can choose to run the tests
  691. * asynchronously and return RC_TESTING immediately while the self tests still
  692. * execute in the background. This function handles both cases and waits until
  693. * all tests have completed.
  694. */
  695. static int tpm2_do_selftest(struct tpm_chip *chip)
  696. {
  697. struct tpm_buf buf;
  698. int full;
  699. int rc;
  700. for (full = 0; full < 2; full++) {
  701. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SELF_TEST);
  702. if (rc)
  703. return rc;
  704. tpm_buf_append_u8(&buf, full);
  705. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
  706. "attempting the self test");
  707. tpm_buf_destroy(&buf);
  708. if (rc == TPM2_RC_TESTING)
  709. rc = TPM2_RC_SUCCESS;
  710. if (rc == TPM2_RC_INITIALIZE || rc == TPM2_RC_SUCCESS)
  711. return rc;
  712. }
  713. return rc;
  714. }
  715. /**
  716. * tpm2_probe() - probe for the TPM 2.0 protocol
  717. * @chip: a &tpm_chip instance
  718. *
  719. * Send an idempotent TPM 2.0 command and see whether there is TPM2 chip in the
  720. * other end based on the response tag. The flag TPM_CHIP_FLAG_TPM2 is set by
  721. * this function if this is the case.
  722. *
  723. * Return:
  724. * 0 on success,
  725. * -errno otherwise
  726. */
  727. int tpm2_probe(struct tpm_chip *chip)
  728. {
  729. struct tpm_output_header *out;
  730. struct tpm_buf buf;
  731. int rc;
  732. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
  733. if (rc)
  734. return rc;
  735. tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
  736. tpm_buf_append_u32(&buf, TPM_PT_TOTAL_COMMANDS);
  737. tpm_buf_append_u32(&buf, 1);
  738. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, NULL);
  739. /* We ignore TPM return codes on purpose. */
  740. if (rc >= 0) {
  741. out = (struct tpm_output_header *)buf.data;
  742. if (be16_to_cpu(out->tag) == TPM2_ST_NO_SESSIONS)
  743. chip->flags |= TPM_CHIP_FLAG_TPM2;
  744. }
  745. tpm_buf_destroy(&buf);
  746. return 0;
  747. }
  748. EXPORT_SYMBOL_GPL(tpm2_probe);
  749. struct tpm2_pcr_selection {
  750. __be16 hash_alg;
  751. u8 size_of_select;
  752. u8 pcr_select[3];
  753. } __packed;
  754. static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
  755. {
  756. struct tpm2_pcr_selection pcr_selection;
  757. struct tpm_buf buf;
  758. void *marker;
  759. void *end;
  760. void *pcr_select_offset;
  761. unsigned int count;
  762. u32 sizeof_pcr_selection;
  763. u32 rsp_len;
  764. int rc;
  765. int i = 0;
  766. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
  767. if (rc)
  768. return rc;
  769. tpm_buf_append_u32(&buf, TPM2_CAP_PCRS);
  770. tpm_buf_append_u32(&buf, 0);
  771. tpm_buf_append_u32(&buf, 1);
  772. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 9, 0,
  773. "get tpm pcr allocation");
  774. if (rc)
  775. goto out;
  776. count = be32_to_cpup(
  777. (__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);
  778. if (count > ARRAY_SIZE(chip->active_banks)) {
  779. rc = -ENODEV;
  780. goto out;
  781. }
  782. marker = &buf.data[TPM_HEADER_SIZE + 9];
  783. rsp_len = be32_to_cpup((__be32 *)&buf.data[2]);
  784. end = &buf.data[rsp_len];
  785. for (i = 0; i < count; i++) {
  786. pcr_select_offset = marker +
  787. offsetof(struct tpm2_pcr_selection, size_of_select);
  788. if (pcr_select_offset >= end) {
  789. rc = -EFAULT;
  790. break;
  791. }
  792. memcpy(&pcr_selection, marker, sizeof(pcr_selection));
  793. chip->active_banks[i] = be16_to_cpu(pcr_selection.hash_alg);
  794. sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
  795. sizeof(pcr_selection.size_of_select) +
  796. pcr_selection.size_of_select;
  797. marker = marker + sizeof_pcr_selection;
  798. }
  799. out:
  800. if (i < ARRAY_SIZE(chip->active_banks))
  801. chip->active_banks[i] = TPM2_ALG_ERROR;
  802. tpm_buf_destroy(&buf);
  803. return rc;
  804. }
  805. static int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip)
  806. {
  807. struct tpm_buf buf;
  808. u32 nr_commands;
  809. __be32 *attrs;
  810. u32 cc;
  811. int i;
  812. int rc;
  813. rc = tpm2_get_tpm_pt(chip, TPM_PT_TOTAL_COMMANDS, &nr_commands, NULL);
  814. if (rc)
  815. goto out;
  816. if (nr_commands > 0xFFFFF) {
  817. rc = -EFAULT;
  818. goto out;
  819. }
  820. chip->cc_attrs_tbl = devm_kcalloc(&chip->dev, 4, nr_commands,
  821. GFP_KERNEL);
  822. if (!chip->cc_attrs_tbl) {
  823. rc = -ENOMEM;
  824. goto out;
  825. }
  826. rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
  827. if (rc)
  828. goto out;
  829. tpm_buf_append_u32(&buf, TPM2_CAP_COMMANDS);
  830. tpm_buf_append_u32(&buf, TPM2_CC_FIRST);
  831. tpm_buf_append_u32(&buf, nr_commands);
  832. rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
  833. 9 + 4 * nr_commands, 0, NULL);
  834. if (rc) {
  835. tpm_buf_destroy(&buf);
  836. goto out;
  837. }
  838. if (nr_commands !=
  839. be32_to_cpup((__be32 *)&buf.data[TPM_HEADER_SIZE + 5])) {
  840. tpm_buf_destroy(&buf);
  841. goto out;
  842. }
  843. chip->nr_commands = nr_commands;
  844. attrs = (__be32 *)&buf.data[TPM_HEADER_SIZE + 9];
  845. for (i = 0; i < nr_commands; i++, attrs++) {
  846. chip->cc_attrs_tbl[i] = be32_to_cpup(attrs);
  847. cc = chip->cc_attrs_tbl[i] & 0xFFFF;
  848. if (cc == TPM2_CC_CONTEXT_SAVE || cc == TPM2_CC_FLUSH_CONTEXT) {
  849. chip->cc_attrs_tbl[i] &=
  850. ~(GENMASK(2, 0) << TPM2_CC_ATTR_CHANDLES);
  851. chip->cc_attrs_tbl[i] |= 1 << TPM2_CC_ATTR_CHANDLES;
  852. }
  853. }
  854. tpm_buf_destroy(&buf);
  855. out:
  856. if (rc > 0)
  857. rc = -ENODEV;
  858. return rc;
  859. }
  860. /**
  861. * tpm2_auto_startup - Perform the standard automatic TPM initialization
  862. * sequence
  863. * @chip: TPM chip to use
  864. *
  865. * Returns 0 on success, < 0 in case of fatal error.
  866. */
  867. int tpm2_auto_startup(struct tpm_chip *chip)
  868. {
  869. int rc;
  870. rc = tpm_get_timeouts(chip);
  871. if (rc)
  872. goto out;
  873. rc = tpm2_do_selftest(chip);
  874. if (rc && rc != TPM2_RC_INITIALIZE)
  875. goto out;
  876. if (rc == TPM2_RC_INITIALIZE) {
  877. rc = tpm_startup(chip);
  878. if (rc)
  879. goto out;
  880. rc = tpm2_do_selftest(chip);
  881. if (rc)
  882. goto out;
  883. }
  884. rc = tpm2_get_pcr_allocation(chip);
  885. if (rc)
  886. goto out;
  887. rc = tpm2_get_cc_attrs_tbl(chip);
  888. out:
  889. if (rc > 0)
  890. rc = -ENODEV;
  891. return rc;
  892. }
  893. int tpm2_find_cc(struct tpm_chip *chip, u32 cc)
  894. {
  895. int i;
  896. for (i = 0; i < chip->nr_commands; i++)
  897. if (cc == (chip->cc_attrs_tbl[i] & GENMASK(15, 0)))
  898. return i;
  899. return -1;
  900. }