tpm2-cmd.c 27 KB

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