blob: 026fec4e180050af6a4e8c6a414661aab4b16261 [file] [log] [blame]
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -08001/*
Jarkko Sakkinen954650e2015-05-30 08:09:04 +03002 * Copyright (C) 2014, 2015 Intel Corporation
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -08003 *
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
18#include "tpm.h"
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +020019#include <crypto/hash_info.h>
Jarkko Sakkinen954650e2015-05-30 08:09:04 +030020#include <keys/trusted-type.h>
21
22enum tpm2_object_attributes {
Jarkko Sakkinenc0b5eed2016-02-13 11:51:23 +020023 TPM2_OA_USER_WITH_AUTH = BIT(6),
24};
25
26enum tpm2_session_attributes {
27 TPM2_SA_CONTINUE_SESSION = BIT(0),
Jarkko Sakkinen954650e2015-05-30 08:09:04 +030028};
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -080029
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -080030struct tpm2_get_tpm_pt_in {
31 __be32 cap_id;
32 __be32 property_id;
33 __be32 property_cnt;
34} __packed;
35
36struct tpm2_get_tpm_pt_out {
37 u8 more_data;
38 __be32 subcap_id;
39 __be32 property_cnt;
40 __be32 property_id;
41 __be32 value;
42} __packed;
43
44struct tpm2_get_random_in {
45 __be16 size;
46} __packed;
47
48struct tpm2_get_random_out {
49 __be16 size;
50 u8 buffer[TPM_MAX_RNG_DATA];
51} __packed;
52
53union tpm2_cmd_params {
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -080054 struct tpm2_get_tpm_pt_in get_tpm_pt_in;
55 struct tpm2_get_tpm_pt_out get_tpm_pt_out;
56 struct tpm2_get_random_in getrandom_in;
57 struct tpm2_get_random_out getrandom_out;
58};
59
60struct tpm2_cmd {
61 tpm_cmd_header header;
62 union tpm2_cmd_params params;
63} __packed;
64
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +020065struct tpm2_hash {
66 unsigned int crypto_id;
67 unsigned int tpm_id;
68};
69
70static struct tpm2_hash tpm2_hash_map[] = {
71 {HASH_ALGO_SHA1, TPM2_ALG_SHA1},
72 {HASH_ALGO_SHA256, TPM2_ALG_SHA256},
73 {HASH_ALGO_SHA384, TPM2_ALG_SHA384},
74 {HASH_ALGO_SHA512, TPM2_ALG_SHA512},
75 {HASH_ALGO_SM3_256, TPM2_ALG_SM3_256},
76};
77
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -080078/*
79 * Array with one entry per ordinal defining the maximum amount
80 * of time the chip could take to return the result. The values
81 * of the SHORT, MEDIUM, and LONG durations are taken from the
82 * PC Client Profile (PTP) specification.
Tomas Winkler076d35642018-03-10 17:15:45 +020083 * LONG_LONG is for commands that generates keys which empirically
84 * takes longer time on some systems.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -080085 */
86static const u8 tpm2_ordinal_duration[TPM2_CC_LAST - TPM2_CC_FIRST + 1] = {
87 TPM_UNDEFINED, /* 11F */
88 TPM_UNDEFINED, /* 120 */
89 TPM_LONG, /* 121 */
90 TPM_UNDEFINED, /* 122 */
91 TPM_UNDEFINED, /* 123 */
92 TPM_UNDEFINED, /* 124 */
93 TPM_UNDEFINED, /* 125 */
94 TPM_UNDEFINED, /* 126 */
95 TPM_UNDEFINED, /* 127 */
96 TPM_UNDEFINED, /* 128 */
97 TPM_LONG, /* 129 */
98 TPM_UNDEFINED, /* 12a */
99 TPM_UNDEFINED, /* 12b */
100 TPM_UNDEFINED, /* 12c */
101 TPM_UNDEFINED, /* 12d */
102 TPM_UNDEFINED, /* 12e */
103 TPM_UNDEFINED, /* 12f */
104 TPM_UNDEFINED, /* 130 */
Tomas Winkler076d35642018-03-10 17:15:45 +0200105 TPM_LONG_LONG, /* 131 */
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800106 TPM_UNDEFINED, /* 132 */
107 TPM_UNDEFINED, /* 133 */
108 TPM_UNDEFINED, /* 134 */
109 TPM_UNDEFINED, /* 135 */
110 TPM_UNDEFINED, /* 136 */
111 TPM_UNDEFINED, /* 137 */
112 TPM_UNDEFINED, /* 138 */
113 TPM_UNDEFINED, /* 139 */
114 TPM_UNDEFINED, /* 13a */
115 TPM_UNDEFINED, /* 13b */
116 TPM_UNDEFINED, /* 13c */
117 TPM_UNDEFINED, /* 13d */
118 TPM_MEDIUM, /* 13e */
119 TPM_UNDEFINED, /* 13f */
120 TPM_UNDEFINED, /* 140 */
121 TPM_UNDEFINED, /* 141 */
122 TPM_UNDEFINED, /* 142 */
123 TPM_LONG, /* 143 */
124 TPM_MEDIUM, /* 144 */
125 TPM_UNDEFINED, /* 145 */
126 TPM_UNDEFINED, /* 146 */
127 TPM_UNDEFINED, /* 147 */
128 TPM_UNDEFINED, /* 148 */
129 TPM_UNDEFINED, /* 149 */
130 TPM_UNDEFINED, /* 14a */
131 TPM_UNDEFINED, /* 14b */
132 TPM_UNDEFINED, /* 14c */
133 TPM_UNDEFINED, /* 14d */
134 TPM_LONG, /* 14e */
135 TPM_UNDEFINED, /* 14f */
136 TPM_UNDEFINED, /* 150 */
137 TPM_UNDEFINED, /* 151 */
138 TPM_UNDEFINED, /* 152 */
Tomas Winkler076d35642018-03-10 17:15:45 +0200139 TPM_LONG_LONG, /* 153 */
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800140 TPM_UNDEFINED, /* 154 */
141 TPM_UNDEFINED, /* 155 */
142 TPM_UNDEFINED, /* 156 */
143 TPM_UNDEFINED, /* 157 */
144 TPM_UNDEFINED, /* 158 */
145 TPM_UNDEFINED, /* 159 */
146 TPM_UNDEFINED, /* 15a */
147 TPM_UNDEFINED, /* 15b */
148 TPM_MEDIUM, /* 15c */
149 TPM_UNDEFINED, /* 15d */
150 TPM_UNDEFINED, /* 15e */
151 TPM_UNDEFINED, /* 15f */
152 TPM_UNDEFINED, /* 160 */
153 TPM_UNDEFINED, /* 161 */
154 TPM_UNDEFINED, /* 162 */
155 TPM_UNDEFINED, /* 163 */
156 TPM_UNDEFINED, /* 164 */
157 TPM_UNDEFINED, /* 165 */
158 TPM_UNDEFINED, /* 166 */
159 TPM_UNDEFINED, /* 167 */
160 TPM_UNDEFINED, /* 168 */
161 TPM_UNDEFINED, /* 169 */
162 TPM_UNDEFINED, /* 16a */
163 TPM_UNDEFINED, /* 16b */
164 TPM_UNDEFINED, /* 16c */
165 TPM_UNDEFINED, /* 16d */
166 TPM_UNDEFINED, /* 16e */
167 TPM_UNDEFINED, /* 16f */
168 TPM_UNDEFINED, /* 170 */
169 TPM_UNDEFINED, /* 171 */
170 TPM_UNDEFINED, /* 172 */
171 TPM_UNDEFINED, /* 173 */
172 TPM_UNDEFINED, /* 174 */
173 TPM_UNDEFINED, /* 175 */
174 TPM_UNDEFINED, /* 176 */
175 TPM_LONG, /* 177 */
176 TPM_UNDEFINED, /* 178 */
177 TPM_UNDEFINED, /* 179 */
178 TPM_MEDIUM, /* 17a */
179 TPM_LONG, /* 17b */
180 TPM_UNDEFINED, /* 17c */
181 TPM_UNDEFINED, /* 17d */
182 TPM_UNDEFINED, /* 17e */
183 TPM_UNDEFINED, /* 17f */
184 TPM_UNDEFINED, /* 180 */
185 TPM_UNDEFINED, /* 181 */
186 TPM_MEDIUM, /* 182 */
187 TPM_UNDEFINED, /* 183 */
188 TPM_UNDEFINED, /* 184 */
189 TPM_MEDIUM, /* 185 */
190 TPM_MEDIUM, /* 186 */
191 TPM_UNDEFINED, /* 187 */
192 TPM_UNDEFINED, /* 188 */
193 TPM_UNDEFINED, /* 189 */
194 TPM_UNDEFINED, /* 18a */
195 TPM_UNDEFINED, /* 18b */
196 TPM_UNDEFINED, /* 18c */
197 TPM_UNDEFINED, /* 18d */
198 TPM_UNDEFINED, /* 18e */
199 TPM_UNDEFINED /* 18f */
200};
201
Roberto Sassu91f7f3d2017-06-23 15:41:56 +0200202struct tpm2_pcr_read_out {
203 __be32 update_cnt;
204 __be32 pcr_selects_cnt;
205 __be16 hash_alg;
206 u8 pcr_select_size;
207 u8 pcr_select[TPM2_PCR_SELECT_MIN];
208 __be32 digests_cnt;
209 __be16 digest_size;
210 u8 digest[];
211} __packed;
212
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800213/**
214 * tpm2_pcr_read() - read a PCR value
215 * @chip: TPM chip to use.
216 * @pcr_idx: index of the PCR to read.
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200217 * @res_buf: buffer to store the resulting hash.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800218 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200219 * Return: Same as with tpm_transmit_cmd.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800220 */
221int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
222{
223 int rc;
Roberto Sassu91f7f3d2017-06-23 15:41:56 +0200224 struct tpm_buf buf;
225 struct tpm2_pcr_read_out *out;
226 u8 pcr_select[TPM2_PCR_SELECT_MIN] = {0};
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800227
228 if (pcr_idx >= TPM2_PLATFORM_PCR)
229 return -EINVAL;
230
Roberto Sassu91f7f3d2017-06-23 15:41:56 +0200231 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_READ);
232 if (rc)
233 return rc;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800234
Roberto Sassu91f7f3d2017-06-23 15:41:56 +0200235 pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800236
Roberto Sassu91f7f3d2017-06-23 15:41:56 +0200237 tpm_buf_append_u32(&buf, 1);
238 tpm_buf_append_u16(&buf, TPM2_ALG_SHA1);
239 tpm_buf_append_u8(&buf, TPM2_PCR_SELECT_MIN);
240 tpm_buf_append(&buf, (const unsigned char *)pcr_select,
241 sizeof(pcr_select));
242
243 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
244 res_buf ? "attempting to read a pcr value" : NULL);
245 if (rc == 0 && res_buf) {
246 out = (struct tpm2_pcr_read_out *)&buf.data[TPM_HEADER_SIZE];
247 memcpy(res_buf, out->digest, SHA1_DIGEST_SIZE);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800248 }
249
Roberto Sassu91f7f3d2017-06-23 15:41:56 +0200250 tpm_buf_destroy(&buf);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800251 return rc;
252}
253
Nayna Jainc1f92b42017-01-30 04:59:41 -0500254struct tpm2_null_auth_area {
255 __be32 handle;
256 __be16 nonce_size;
257 u8 attributes;
258 __be16 auth_size;
259} __packed;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800260
261/**
262 * tpm2_pcr_extend() - extend a PCR value
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200263 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800264 * @chip: TPM chip to use.
265 * @pcr_idx: index of the PCR.
Nayna Jainc1f92b42017-01-30 04:59:41 -0500266 * @count: number of digests passed.
267 * @digests: list of pcr banks and corresponding digest values to extend.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800268 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200269 * Return: Same as with tpm_transmit_cmd.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800270 */
Nayna Jainc1f92b42017-01-30 04:59:41 -0500271int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
272 struct tpm2_digest *digests)
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800273{
Nayna Jainc1f92b42017-01-30 04:59:41 -0500274 struct tpm_buf buf;
275 struct tpm2_null_auth_area auth_area;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800276 int rc;
Nayna Jainc1f92b42017-01-30 04:59:41 -0500277 int i;
278 int j;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800279
Nayna Jainc1f92b42017-01-30 04:59:41 -0500280 if (count > ARRAY_SIZE(chip->active_banks))
281 return -EINVAL;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800282
Nayna Jainc1f92b42017-01-30 04:59:41 -0500283 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_EXTEND);
284 if (rc)
285 return rc;
286
287 tpm_buf_append_u32(&buf, pcr_idx);
288
289 auth_area.handle = cpu_to_be32(TPM2_RS_PW);
290 auth_area.nonce_size = 0;
291 auth_area.attributes = 0;
292 auth_area.auth_size = 0;
293
294 tpm_buf_append_u32(&buf, sizeof(struct tpm2_null_auth_area));
295 tpm_buf_append(&buf, (const unsigned char *)&auth_area,
296 sizeof(auth_area));
297 tpm_buf_append_u32(&buf, count);
298
299 for (i = 0; i < count; i++) {
300 for (j = 0; j < ARRAY_SIZE(tpm2_hash_map); j++) {
301 if (digests[i].alg_id != tpm2_hash_map[j].tpm_id)
302 continue;
303 tpm_buf_append_u16(&buf, digests[i].alg_id);
304 tpm_buf_append(&buf, (const unsigned char
305 *)&digests[i].digest,
306 hash_digest_size[tpm2_hash_map[j].crypto_id]);
307 }
308 }
309
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200310 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800311 "attempting extend a PCR value");
312
Nayna Jainc1f92b42017-01-30 04:59:41 -0500313 tpm_buf_destroy(&buf);
314
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800315 return rc;
316}
317
Nayna Jainc1f92b42017-01-30 04:59:41 -0500318
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800319#define TPM2_GETRANDOM_IN_SIZE \
320 (sizeof(struct tpm_input_header) + \
321 sizeof(struct tpm2_get_random_in))
322
323static const struct tpm_input_header tpm2_getrandom_header = {
324 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
325 .length = cpu_to_be32(TPM2_GETRANDOM_IN_SIZE),
326 .ordinal = cpu_to_be32(TPM2_CC_GET_RANDOM)
327};
328
329/**
330 * tpm2_get_random() - get random bytes from the TPM RNG
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200331 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800332 * @chip: TPM chip to use
333 * @out: destination buffer for the random bytes
334 * @max: the max number of bytes to write to @out
335 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200336 * Return:
337 * Size of the output buffer, or -EIO on error.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800338 */
339int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)
340{
341 struct tpm2_cmd cmd;
Stefan Bergerc659af72017-01-19 07:19:12 -0500342 u32 recd, rlength;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800343 u32 num_bytes;
344 int err;
345 int total = 0;
346 int retries = 5;
347 u8 *dest = out;
348
349 num_bytes = min_t(u32, max, sizeof(cmd.params.getrandom_out.buffer));
350
351 if (!out || !num_bytes ||
352 max > sizeof(cmd.params.getrandom_out.buffer))
353 return -EINVAL;
354
355 do {
356 cmd.header.in = tpm2_getrandom_header;
357 cmd.params.getrandom_in.size = cpu_to_be16(num_bytes);
358
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200359 err = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd),
Stefan Bergerc659af72017-01-19 07:19:12 -0500360 offsetof(struct tpm2_get_random_out,
361 buffer),
362 0, "attempting get random");
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800363 if (err)
364 break;
365
366 recd = min_t(u32, be16_to_cpu(cmd.params.getrandom_out.size),
367 num_bytes);
Stefan Bergerc659af72017-01-19 07:19:12 -0500368 rlength = be32_to_cpu(cmd.header.out.length);
369 if (rlength < offsetof(struct tpm2_get_random_out, buffer) +
370 recd)
371 return -EFAULT;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800372 memcpy(dest, cmd.params.getrandom_out.buffer, recd);
373
374 dest += recd;
375 total += recd;
376 num_bytes -= recd;
377 } while (retries-- && total < max);
378
379 return total ? total : -EIO;
380}
381
382#define TPM2_GET_TPM_PT_IN_SIZE \
383 (sizeof(struct tpm_input_header) + \
384 sizeof(struct tpm2_get_tpm_pt_in))
385
Stefan Bergerc659af72017-01-19 07:19:12 -0500386#define TPM2_GET_TPM_PT_OUT_BODY_SIZE \
387 sizeof(struct tpm2_get_tpm_pt_out)
388
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800389static const struct tpm_input_header tpm2_get_tpm_pt_header = {
390 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
391 .length = cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE),
392 .ordinal = cpu_to_be32(TPM2_CC_GET_CAPABILITY)
393};
394
395/**
Jarkko Sakkinen9aa36b32016-11-26 13:39:35 +0200396 * tpm2_flush_context_cmd() - execute a TPM2_FlushContext command
397 * @chip: TPM chip to use
398 * @payload: the key data in clear and encrypted form
399 * @options: authentication values and other options
400 *
401 * Return: same as with tpm_transmit_cmd
402 */
403void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
404 unsigned int flags)
405{
406 struct tpm_buf buf;
407 int rc;
408
409 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT);
410 if (rc) {
411 dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
412 handle);
413 return;
414 }
415
416 tpm_buf_append_u32(&buf, handle);
417
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200418 (void) tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, flags,
Jarkko Sakkinen9aa36b32016-11-26 13:39:35 +0200419 "flushing context");
420
421 tpm_buf_destroy(&buf);
422}
423
424/**
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200425 * tpm_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300426 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200427 * @buf: an allocated tpm_buf instance
428 * @session_handle: session handle
429 * @nonce: the session nonce, may be NULL if not used
430 * @nonce_len: the session nonce length, may be 0 if not used
431 * @attributes: the session attributes
432 * @hmac: the session HMAC or password, may be NULL if not used
433 * @hmac_len: the session HMAC or password length, maybe 0 if not used
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300434 */
435static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,
436 const u8 *nonce, u16 nonce_len,
437 u8 attributes,
438 const u8 *hmac, u16 hmac_len)
439{
440 tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);
441 tpm_buf_append_u32(buf, session_handle);
442 tpm_buf_append_u16(buf, nonce_len);
443
444 if (nonce && nonce_len)
445 tpm_buf_append(buf, nonce, nonce_len);
446
447 tpm_buf_append_u8(buf, attributes);
448 tpm_buf_append_u16(buf, hmac_len);
449
450 if (hmac && hmac_len)
451 tpm_buf_append(buf, hmac, hmac_len);
452}
453
454/**
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300455 * tpm2_seal_trusted() - seal the payload of a trusted key
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200456 *
457 * @chip: TPM chip to use
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300458 * @payload: the key data in clear and encrypted form
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300459 * @options: authentication values and other options
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300460 *
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300461 * Return: < 0 on error and 0 on success.
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300462 */
463int tpm2_seal_trusted(struct tpm_chip *chip,
464 struct trusted_key_payload *payload,
465 struct trusted_key_options *options)
466{
467 unsigned int blob_len;
468 struct tpm_buf buf;
Stefan Bergerc659af72017-01-19 07:19:12 -0500469 u32 hash, rlength;
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +0200470 int i;
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300471 int rc;
472
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +0200473 for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
474 if (options->hash == tpm2_hash_map[i].crypto_id) {
475 hash = tpm2_hash_map[i].tpm_id;
476 break;
477 }
478 }
479
480 if (i == ARRAY_SIZE(tpm2_hash_map))
481 return -EINVAL;
482
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300483 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
484 if (rc)
485 return rc;
486
487 tpm_buf_append_u32(&buf, options->keyhandle);
488 tpm2_buf_append_auth(&buf, TPM2_RS_PW,
489 NULL /* nonce */, 0,
490 0 /* session_attributes */,
491 options->keyauth /* hmac */,
492 TPM_DIGEST_SIZE);
493
494 /* sensitive */
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200495 tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300496
497 tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE);
498 tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE);
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200499 tpm_buf_append_u16(&buf, payload->key_len + 1);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300500 tpm_buf_append(&buf, payload->key, payload->key_len);
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200501 tpm_buf_append_u8(&buf, payload->migratable);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300502
503 /* public */
Jarkko Sakkinenf3c82ad2016-01-06 16:43:30 +0200504 tpm_buf_append_u16(&buf, 14 + options->policydigest_len);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300505 tpm_buf_append_u16(&buf, TPM2_ALG_KEYEDHASH);
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +0200506 tpm_buf_append_u16(&buf, hash);
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200507
508 /* policy */
Jarkko Sakkinenf3c82ad2016-01-06 16:43:30 +0200509 if (options->policydigest_len) {
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200510 tpm_buf_append_u32(&buf, 0);
Jarkko Sakkinenf3c82ad2016-01-06 16:43:30 +0200511 tpm_buf_append_u16(&buf, options->policydigest_len);
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200512 tpm_buf_append(&buf, options->policydigest,
Jarkko Sakkinenf3c82ad2016-01-06 16:43:30 +0200513 options->policydigest_len);
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200514 } else {
Jarkko Sakkinenc0b5eed2016-02-13 11:51:23 +0200515 tpm_buf_append_u32(&buf, TPM2_OA_USER_WITH_AUTH);
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200516 tpm_buf_append_u16(&buf, 0);
517 }
518
519 /* public parameters */
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300520 tpm_buf_append_u16(&buf, TPM2_ALG_NULL);
521 tpm_buf_append_u16(&buf, 0);
522
523 /* outside info */
524 tpm_buf_append_u16(&buf, 0);
525
526 /* creation PCR */
527 tpm_buf_append_u32(&buf, 0);
528
529 if (buf.flags & TPM_BUF_OVERFLOW) {
530 rc = -E2BIG;
531 goto out;
532 }
533
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200534 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, 0,
Stefan Bergerc659af72017-01-19 07:19:12 -0500535 "sealing data");
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300536 if (rc)
537 goto out;
538
539 blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]);
540 if (blob_len > MAX_BLOB_SIZE) {
541 rc = -E2BIG;
542 goto out;
543 }
Stefan Bergerc659af72017-01-19 07:19:12 -0500544 rlength = be32_to_cpu(((struct tpm2_cmd *)&buf)->header.out.length);
545 if (rlength < TPM_HEADER_SIZE + 4 + blob_len) {
546 rc = -EFAULT;
547 goto out;
548 }
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300549
550 memcpy(payload->blob, &buf.data[TPM_HEADER_SIZE + 4], blob_len);
551 payload->blob_len = blob_len;
552
553out:
554 tpm_buf_destroy(&buf);
555
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +0200556 if (rc > 0) {
Jarkko Sakkinen7d761112017-01-25 23:00:22 +0200557 if (tpm2_rc_value(rc) == TPM2_RC_HASH)
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +0200558 rc = -EINVAL;
559 else
560 rc = -EPERM;
561 }
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300562
563 return rc;
564}
565
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300566/**
567 * tpm2_load_cmd() - execute a TPM2_Load command
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200568 *
569 * @chip: TPM chip to use
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300570 * @payload: the key data in clear and encrypted form
571 * @options: authentication values and other options
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200572 * @blob_handle: returned blob handle
573 * @flags: tpm transmit flags
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300574 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200575 * Return: 0 on success.
576 * -E2BIG on wrong payload size.
577 * -EPERM on tpm error status.
578 * < 0 error from tpm_transmit_cmd.
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300579 */
580static int tpm2_load_cmd(struct tpm_chip *chip,
581 struct trusted_key_payload *payload,
582 struct trusted_key_options *options,
583 u32 *blob_handle, unsigned int flags)
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300584{
585 struct tpm_buf buf;
586 unsigned int private_len;
587 unsigned int public_len;
588 unsigned int blob_len;
589 int rc;
590
591 private_len = be16_to_cpup((__be16 *) &payload->blob[0]);
592 if (private_len > (payload->blob_len - 2))
593 return -E2BIG;
594
595 public_len = be16_to_cpup((__be16 *) &payload->blob[2 + private_len]);
596 blob_len = private_len + public_len + 4;
597 if (blob_len > payload->blob_len)
598 return -E2BIG;
599
600 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
601 if (rc)
602 return rc;
603
604 tpm_buf_append_u32(&buf, options->keyhandle);
605 tpm2_buf_append_auth(&buf, TPM2_RS_PW,
606 NULL /* nonce */, 0,
607 0 /* session_attributes */,
608 options->keyauth /* hmac */,
609 TPM_DIGEST_SIZE);
610
611 tpm_buf_append(&buf, payload->blob, blob_len);
612
613 if (buf.flags & TPM_BUF_OVERFLOW) {
614 rc = -E2BIG;
615 goto out;
616 }
617
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200618 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, flags,
Stefan Bergerc659af72017-01-19 07:19:12 -0500619 "loading blob");
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300620 if (!rc)
621 *blob_handle = be32_to_cpup(
622 (__be32 *) &buf.data[TPM_HEADER_SIZE]);
623
624out:
625 tpm_buf_destroy(&buf);
626
627 if (rc > 0)
628 rc = -EPERM;
629
630 return rc;
631}
632
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300633/**
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300634 * tpm2_unseal_cmd() - execute a TPM2_Unload command
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200635 *
636 * @chip: TPM chip to use
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300637 * @payload: the key data in clear and encrypted form
638 * @options: authentication values and other options
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200639 * @blob_handle: blob handle
640 * @flags: tpm_transmit_cmd flags
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300641 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200642 * Return: 0 on success
643 * -EPERM on tpm error status
644 * < 0 error from tpm_transmit_cmd
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300645 */
646static int tpm2_unseal_cmd(struct tpm_chip *chip,
647 struct trusted_key_payload *payload,
648 struct trusted_key_options *options,
649 u32 blob_handle, unsigned int flags)
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300650{
651 struct tpm_buf buf;
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200652 u16 data_len;
653 u8 *data;
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300654 int rc;
Stefan Bergerc659af72017-01-19 07:19:12 -0500655 u32 rlength;
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300656
657 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
658 if (rc)
659 return rc;
660
661 tpm_buf_append_u32(&buf, blob_handle);
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200662 tpm2_buf_append_auth(&buf,
663 options->policyhandle ?
664 options->policyhandle : TPM2_RS_PW,
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300665 NULL /* nonce */, 0,
Jarkko Sakkinenc0b5eed2016-02-13 11:51:23 +0200666 TPM2_SA_CONTINUE_SESSION,
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300667 options->blobauth /* hmac */,
668 TPM_DIGEST_SIZE);
669
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200670 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 6, flags,
Stefan Bergerc659af72017-01-19 07:19:12 -0500671 "unsealing");
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300672 if (rc > 0)
673 rc = -EPERM;
674
675 if (!rc) {
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200676 data_len = be16_to_cpup(
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300677 (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
Jeremy Boone3be23272018-02-08 12:28:08 -0800678 if (data_len < MIN_KEY_SIZE || data_len > MAX_KEY_SIZE + 1) {
679 rc = -EFAULT;
680 goto out;
681 }
Stefan Bergerc659af72017-01-19 07:19:12 -0500682
683 rlength = be32_to_cpu(((struct tpm2_cmd *)&buf)
684 ->header.out.length);
685 if (rlength < TPM_HEADER_SIZE + 6 + data_len) {
686 rc = -EFAULT;
687 goto out;
688 }
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200689 data = &buf.data[TPM_HEADER_SIZE + 6];
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300690
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200691 memcpy(payload->key, data, data_len - 1);
692 payload->key_len = data_len - 1;
693 payload->migratable = data[data_len - 1];
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300694 }
695
Stefan Bergerc659af72017-01-19 07:19:12 -0500696out:
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300697 tpm_buf_destroy(&buf);
698 return rc;
699}
700
701/**
Baruch Siachcbef69a2016-11-06 11:02:45 +0200702 * tpm2_unseal_trusted() - unseal the payload of a trusted key
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200703 *
704 * @chip: TPM chip to use
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300705 * @payload: the key data in clear and encrypted form
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300706 * @options: authentication values and other options
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300707 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200708 * Return: Same as with tpm_transmit_cmd.
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300709 */
710int tpm2_unseal_trusted(struct tpm_chip *chip,
711 struct trusted_key_payload *payload,
712 struct trusted_key_options *options)
713{
714 u32 blob_handle;
715 int rc;
716
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300717 mutex_lock(&chip->tpm_mutex);
718 rc = tpm2_load_cmd(chip, payload, options, &blob_handle,
719 TPM_TRANSMIT_UNLOCKED);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300720 if (rc)
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300721 goto out;
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300722
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300723 rc = tpm2_unseal_cmd(chip, payload, options, blob_handle,
724 TPM_TRANSMIT_UNLOCKED);
725 tpm2_flush_context_cmd(chip, blob_handle, TPM_TRANSMIT_UNLOCKED);
726out:
727 mutex_unlock(&chip->tpm_mutex);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300728 return rc;
729}
730
731/**
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800732 * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
733 * @chip: TPM chip to use.
734 * @property_id: property ID.
735 * @value: output variable.
736 * @desc: passed to tpm_transmit_cmd()
737 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200738 * Return: Same as with tpm_transmit_cmd.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800739 */
740ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
741 const char *desc)
742{
743 struct tpm2_cmd cmd;
744 int rc;
745
746 cmd.header.in = tpm2_get_tpm_pt_header;
747 cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
748 cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id);
749 cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
750
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200751 rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd),
Stefan Bergerc659af72017-01-19 07:19:12 -0500752 TPM2_GET_TPM_PT_OUT_BODY_SIZE, 0, desc);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800753 if (!rc)
apronin@chromium.org1b0612b2016-07-14 18:07:18 -0700754 *value = be32_to_cpu(cmd.params.get_tpm_pt_out.value);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800755
756 return rc;
757}
Jarkko Sakkineneb5854e2016-06-12 16:42:09 +0300758EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800759
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800760/**
Jarkko Sakkinencc26c6e2018-06-05 22:22:58 +0300761 * tpm2_shutdown() - send a TPM shutdown command
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200762 *
Jarkko Sakkinencc26c6e2018-06-05 22:22:58 +0300763 * Sends a TPM shutdown command. The shutdown command is used in call
764 * sites where the system is going down. If it fails, there is not much
765 * that can be done except print an error message.
766 *
767 * @chip: a &tpm_chip instance
768 * @shutdown_type: TPM_SU_CLEAR or TPM_SU_STATE.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800769 */
Jarkko Sakkinen74d6b3c2015-01-29 07:43:47 +0200770void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800771{
Jarkko Sakkinencc26c6e2018-06-05 22:22:58 +0300772 struct tpm_buf buf;
Jarkko Sakkinen74d6b3c2015-01-29 07:43:47 +0200773 int rc;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800774
Jarkko Sakkinencc26c6e2018-06-05 22:22:58 +0300775 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SHUTDOWN);
776 if (rc)
777 return;
778 tpm_buf_append_u16(&buf, shutdown_type);
779 tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
780 "stopping the TPM");
781 tpm_buf_destroy(&buf);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800782}
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800783
784/*
785 * tpm2_calc_ordinal_duration() - maximum duration for a command
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200786 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800787 * @chip: TPM chip to use.
788 * @ordinal: command code number.
789 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200790 * Return: maximum duration for a command
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800791 */
792unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
793{
794 int index = TPM_UNDEFINED;
795 int duration = 0;
796
797 if (ordinal >= TPM2_CC_FIRST && ordinal <= TPM2_CC_LAST)
798 index = tpm2_ordinal_duration[ordinal - TPM2_CC_FIRST];
799
800 if (index != TPM_UNDEFINED)
Christophe Ricardaf782f32016-03-31 22:56:59 +0200801 duration = chip->duration[index];
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800802
803 if (duration <= 0)
Tomas Winkler076d35642018-03-10 17:15:45 +0200804 duration = msecs_to_jiffies(TPM2_DURATION_DEFAULT);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800805
806 return duration;
807}
808EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration);
809
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800810/**
Alexander Steffen2482b1b2017-08-31 19:18:56 +0200811 * tpm2_do_selftest() - ensure that all self tests have passed
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200812 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800813 * @chip: TPM chip to use
814 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200815 * Return: Same as with tpm_transmit_cmd.
816 *
Alexander Steffen125a2212017-08-31 19:18:58 +0200817 * The TPM can either run all self tests synchronously and then return
818 * RC_SUCCESS once all tests were successful. Or it can choose to run the tests
819 * asynchronously and return RC_TESTING immediately while the self tests still
820 * execute in the background. This function handles both cases and waits until
821 * all tests have completed.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800822 */
Jason Gunthorpecae8b442016-07-12 11:41:49 -0600823static int tpm2_do_selftest(struct tpm_chip *chip)
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800824{
James Bottomley2be8ffe2018-03-22 17:32:20 +0200825 struct tpm_buf buf;
826 int full;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800827 int rc;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800828
James Bottomley2be8ffe2018-03-22 17:32:20 +0200829 for (full = 0; full < 2; full++) {
830 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SELF_TEST);
831 if (rc)
832 return rc;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800833
James Bottomley2be8ffe2018-03-22 17:32:20 +0200834 tpm_buf_append_u8(&buf, full);
835 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
836 "attempting the self test");
837 tpm_buf_destroy(&buf);
Alexander Steffen125a2212017-08-31 19:18:58 +0200838
James Bottomley2be8ffe2018-03-22 17:32:20 +0200839 if (rc == TPM2_RC_TESTING)
840 rc = TPM2_RC_SUCCESS;
841 if (rc == TPM2_RC_INITIALIZE || rc == TPM2_RC_SUCCESS)
842 return rc;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800843 }
844
845 return rc;
846}
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800847
848/**
Jarkko Sakkinen4d5f2052015-02-04 16:21:09 +0200849 * tpm2_probe() - probe TPM 2.0
850 * @chip: TPM chip to use
851 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200852 * Return: < 0 error and 0 on success.
853 *
Jarkko Sakkinen4d5f2052015-02-04 16:21:09 +0200854 * Send idempotent TPM 2.0 command and see whether TPM 2.0 chip replied based on
855 * the reply tag.
856 */
857int tpm2_probe(struct tpm_chip *chip)
858{
859 struct tpm2_cmd cmd;
860 int rc;
861
862 cmd.header.in = tpm2_get_tpm_pt_header;
863 cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
864 cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100);
865 cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
866
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200867 rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0, NULL);
Jarkko Sakkinen4d5f2052015-02-04 16:21:09 +0200868 if (rc < 0)
869 return rc;
Jarkko Sakkinen4d5f2052015-02-04 16:21:09 +0200870
871 if (be16_to_cpu(cmd.header.out.tag) == TPM2_ST_NO_SESSIONS)
872 chip->flags |= TPM_CHIP_FLAG_TPM2;
873
874 return 0;
875}
876EXPORT_SYMBOL_GPL(tpm2_probe);
Jason Gunthorpecae8b442016-07-12 11:41:49 -0600877
Nayna Jain1db15342017-01-30 04:59:40 -0500878struct tpm2_pcr_selection {
879 __be16 hash_alg;
880 u8 size_of_select;
881 u8 pcr_select[3];
882} __packed;
883
Jarkko Sakkinen61841be2017-02-15 20:02:28 +0200884static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
Nayna Jain1db15342017-01-30 04:59:40 -0500885{
886 struct tpm2_pcr_selection pcr_selection;
887 struct tpm_buf buf;
888 void *marker;
889 void *end;
890 void *pcr_select_offset;
891 unsigned int count;
892 u32 sizeof_pcr_selection;
893 u32 rsp_len;
894 int rc;
895 int i = 0;
896
897 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
898 if (rc)
899 return rc;
900
901 tpm_buf_append_u32(&buf, TPM2_CAP_PCRS);
902 tpm_buf_append_u32(&buf, 0);
903 tpm_buf_append_u32(&buf, 1);
904
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200905 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 9, 0,
Nayna Jain1db15342017-01-30 04:59:40 -0500906 "get tpm pcr allocation");
907 if (rc)
908 goto out;
909
910 count = be32_to_cpup(
911 (__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);
912
913 if (count > ARRAY_SIZE(chip->active_banks)) {
914 rc = -ENODEV;
915 goto out;
916 }
917
918 marker = &buf.data[TPM_HEADER_SIZE + 9];
919
920 rsp_len = be32_to_cpup((__be32 *)&buf.data[2]);
921 end = &buf.data[rsp_len];
922
923 for (i = 0; i < count; i++) {
924 pcr_select_offset = marker +
925 offsetof(struct tpm2_pcr_selection, size_of_select);
926 if (pcr_select_offset >= end) {
927 rc = -EFAULT;
928 break;
929 }
930
931 memcpy(&pcr_selection, marker, sizeof(pcr_selection));
932 chip->active_banks[i] = be16_to_cpu(pcr_selection.hash_alg);
933 sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
934 sizeof(pcr_selection.size_of_select) +
935 pcr_selection.size_of_select;
936 marker = marker + sizeof_pcr_selection;
937 }
938
939out:
940 if (i < ARRAY_SIZE(chip->active_banks))
941 chip->active_banks[i] = TPM2_ALG_ERROR;
942
943 tpm_buf_destroy(&buf);
944
945 return rc;
946}
Jarkko Sakkinen61841be2017-02-15 20:02:28 +0200947
Jarkko Sakkinen58472f52016-11-10 20:42:07 -0800948static int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip)
949{
950 struct tpm_buf buf;
951 u32 nr_commands;
Jarkko Sakkinen171360d2017-09-13 09:58:49 -0700952 __be32 *attrs;
Jarkko Sakkinen58472f52016-11-10 20:42:07 -0800953 u32 cc;
954 int i;
955 int rc;
956
957 rc = tpm2_get_tpm_pt(chip, TPM_PT_TOTAL_COMMANDS, &nr_commands, NULL);
958 if (rc)
959 goto out;
960
961 if (nr_commands > 0xFFFFF) {
962 rc = -EFAULT;
963 goto out;
964 }
965
Kees Cooka86854d2018-06-12 14:07:58 -0700966 chip->cc_attrs_tbl = devm_kcalloc(&chip->dev, 4, nr_commands,
Jarkko Sakkinen58472f52016-11-10 20:42:07 -0800967 GFP_KERNEL);
968
969 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
970 if (rc)
971 goto out;
972
973 tpm_buf_append_u32(&buf, TPM2_CAP_COMMANDS);
974 tpm_buf_append_u32(&buf, TPM2_CC_FIRST);
975 tpm_buf_append_u32(&buf, nr_commands);
976
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200977 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
978 9 + 4 * nr_commands, 0, NULL);
Jarkko Sakkinen58472f52016-11-10 20:42:07 -0800979 if (rc) {
980 tpm_buf_destroy(&buf);
981 goto out;
982 }
983
984 if (nr_commands !=
985 be32_to_cpup((__be32 *)&buf.data[TPM_HEADER_SIZE + 5])) {
986 tpm_buf_destroy(&buf);
987 goto out;
988 }
989
990 chip->nr_commands = nr_commands;
991
Jarkko Sakkinen171360d2017-09-13 09:58:49 -0700992 attrs = (__be32 *)&buf.data[TPM_HEADER_SIZE + 9];
Jarkko Sakkinen58472f52016-11-10 20:42:07 -0800993 for (i = 0; i < nr_commands; i++, attrs++) {
994 chip->cc_attrs_tbl[i] = be32_to_cpup(attrs);
995 cc = chip->cc_attrs_tbl[i] & 0xFFFF;
996
997 if (cc == TPM2_CC_CONTEXT_SAVE || cc == TPM2_CC_FLUSH_CONTEXT) {
998 chip->cc_attrs_tbl[i] &=
999 ~(GENMASK(2, 0) << TPM2_CC_ATTR_CHANDLES);
1000 chip->cc_attrs_tbl[i] |= 1 << TPM2_CC_ATTR_CHANDLES;
1001 }
1002 }
1003
1004 tpm_buf_destroy(&buf);
1005
1006out:
1007 if (rc > 0)
1008 rc = -ENODEV;
1009 return rc;
1010}
1011
Jarkko Sakkinen61841be2017-02-15 20:02:28 +02001012/**
1013 * tpm2_auto_startup - Perform the standard automatic TPM initialization
1014 * sequence
1015 * @chip: TPM chip to use
1016 *
Jarkko Sakkinen58472f52016-11-10 20:42:07 -08001017 * Returns 0 on success, < 0 in case of fatal error.
Jarkko Sakkinen61841be2017-02-15 20:02:28 +02001018 */
1019int tpm2_auto_startup(struct tpm_chip *chip)
1020{
1021 int rc;
1022
1023 rc = tpm_get_timeouts(chip);
1024 if (rc)
1025 goto out;
1026
1027 rc = tpm2_do_selftest(chip);
James Bottomley2be8ffe2018-03-22 17:32:20 +02001028 if (rc && rc != TPM2_RC_INITIALIZE)
Jarkko Sakkinen61841be2017-02-15 20:02:28 +02001029 goto out;
Jarkko Sakkinen61841be2017-02-15 20:02:28 +02001030
1031 if (rc == TPM2_RC_INITIALIZE) {
Jarkko Sakkinen19cbe4f2017-06-21 09:31:34 +02001032 rc = tpm_startup(chip);
Jarkko Sakkinen61841be2017-02-15 20:02:28 +02001033 if (rc)
1034 goto out;
1035
1036 rc = tpm2_do_selftest(chip);
James Bottomley2be8ffe2018-03-22 17:32:20 +02001037 if (rc)
Jarkko Sakkinen61841be2017-02-15 20:02:28 +02001038 goto out;
Jarkko Sakkinen61841be2017-02-15 20:02:28 +02001039 }
1040
1041 rc = tpm2_get_pcr_allocation(chip);
Jarkko Sakkinen58472f52016-11-10 20:42:07 -08001042 if (rc)
1043 goto out;
1044
1045 rc = tpm2_get_cc_attrs_tbl(chip);
Jarkko Sakkinen61841be2017-02-15 20:02:28 +02001046
1047out:
1048 if (rc > 0)
1049 rc = -ENODEV;
1050 return rc;
1051}
Jarkko Sakkinen58472f52016-11-10 20:42:07 -08001052
1053int tpm2_find_cc(struct tpm_chip *chip, u32 cc)
1054{
1055 int i;
1056
1057 for (i = 0; i < chip->nr_commands; i++)
1058 if (cc == (chip->cc_attrs_tbl[i] & GENMASK(15, 0)))
1059 return i;
1060
1061 return -1;
1062}