blob: 76b0c87b1288f9b7e1b73179c71a90320d0441a7 [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
30struct tpm2_startup_in {
31 __be16 startup_type;
32} __packed;
33
34struct tpm2_self_test_in {
35 u8 full_test;
36} __packed;
37
38struct tpm2_pcr_read_in {
39 __be32 pcr_selects_cnt;
40 __be16 hash_alg;
41 u8 pcr_select_size;
42 u8 pcr_select[TPM2_PCR_SELECT_MIN];
43} __packed;
44
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -080045struct tpm2_get_tpm_pt_in {
46 __be32 cap_id;
47 __be32 property_id;
48 __be32 property_cnt;
49} __packed;
50
51struct tpm2_get_tpm_pt_out {
52 u8 more_data;
53 __be32 subcap_id;
54 __be32 property_cnt;
55 __be32 property_id;
56 __be32 value;
57} __packed;
58
59struct tpm2_get_random_in {
60 __be16 size;
61} __packed;
62
63struct tpm2_get_random_out {
64 __be16 size;
65 u8 buffer[TPM_MAX_RNG_DATA];
66} __packed;
67
68union tpm2_cmd_params {
69 struct tpm2_startup_in startup_in;
70 struct tpm2_self_test_in selftest_in;
71 struct tpm2_pcr_read_in pcrread_in;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -080072 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
78struct tpm2_cmd {
79 tpm_cmd_header header;
80 union tpm2_cmd_params params;
81} __packed;
82
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +020083struct tpm2_hash {
84 unsigned int crypto_id;
85 unsigned int tpm_id;
86};
87
88static struct tpm2_hash tpm2_hash_map[] = {
89 {HASH_ALGO_SHA1, TPM2_ALG_SHA1},
90 {HASH_ALGO_SHA256, TPM2_ALG_SHA256},
91 {HASH_ALGO_SHA384, TPM2_ALG_SHA384},
92 {HASH_ALGO_SHA512, TPM2_ALG_SHA512},
93 {HASH_ALGO_SM3_256, TPM2_ALG_SM3_256},
94};
95
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -080096/*
97 * Array with one entry per ordinal defining the maximum amount
98 * of time the chip could take to return the result. The values
99 * of the SHORT, MEDIUM, and LONG durations are taken from the
100 * PC Client Profile (PTP) specification.
101 */
102static const u8 tpm2_ordinal_duration[TPM2_CC_LAST - TPM2_CC_FIRST + 1] = {
103 TPM_UNDEFINED, /* 11F */
104 TPM_UNDEFINED, /* 120 */
105 TPM_LONG, /* 121 */
106 TPM_UNDEFINED, /* 122 */
107 TPM_UNDEFINED, /* 123 */
108 TPM_UNDEFINED, /* 124 */
109 TPM_UNDEFINED, /* 125 */
110 TPM_UNDEFINED, /* 126 */
111 TPM_UNDEFINED, /* 127 */
112 TPM_UNDEFINED, /* 128 */
113 TPM_LONG, /* 129 */
114 TPM_UNDEFINED, /* 12a */
115 TPM_UNDEFINED, /* 12b */
116 TPM_UNDEFINED, /* 12c */
117 TPM_UNDEFINED, /* 12d */
118 TPM_UNDEFINED, /* 12e */
119 TPM_UNDEFINED, /* 12f */
120 TPM_UNDEFINED, /* 130 */
121 TPM_UNDEFINED, /* 131 */
122 TPM_UNDEFINED, /* 132 */
123 TPM_UNDEFINED, /* 133 */
124 TPM_UNDEFINED, /* 134 */
125 TPM_UNDEFINED, /* 135 */
126 TPM_UNDEFINED, /* 136 */
127 TPM_UNDEFINED, /* 137 */
128 TPM_UNDEFINED, /* 138 */
129 TPM_UNDEFINED, /* 139 */
130 TPM_UNDEFINED, /* 13a */
131 TPM_UNDEFINED, /* 13b */
132 TPM_UNDEFINED, /* 13c */
133 TPM_UNDEFINED, /* 13d */
134 TPM_MEDIUM, /* 13e */
135 TPM_UNDEFINED, /* 13f */
136 TPM_UNDEFINED, /* 140 */
137 TPM_UNDEFINED, /* 141 */
138 TPM_UNDEFINED, /* 142 */
139 TPM_LONG, /* 143 */
140 TPM_MEDIUM, /* 144 */
141 TPM_UNDEFINED, /* 145 */
142 TPM_UNDEFINED, /* 146 */
143 TPM_UNDEFINED, /* 147 */
144 TPM_UNDEFINED, /* 148 */
145 TPM_UNDEFINED, /* 149 */
146 TPM_UNDEFINED, /* 14a */
147 TPM_UNDEFINED, /* 14b */
148 TPM_UNDEFINED, /* 14c */
149 TPM_UNDEFINED, /* 14d */
150 TPM_LONG, /* 14e */
151 TPM_UNDEFINED, /* 14f */
152 TPM_UNDEFINED, /* 150 */
153 TPM_UNDEFINED, /* 151 */
154 TPM_UNDEFINED, /* 152 */
155 TPM_UNDEFINED, /* 153 */
156 TPM_UNDEFINED, /* 154 */
157 TPM_UNDEFINED, /* 155 */
158 TPM_UNDEFINED, /* 156 */
159 TPM_UNDEFINED, /* 157 */
160 TPM_UNDEFINED, /* 158 */
161 TPM_UNDEFINED, /* 159 */
162 TPM_UNDEFINED, /* 15a */
163 TPM_UNDEFINED, /* 15b */
164 TPM_MEDIUM, /* 15c */
165 TPM_UNDEFINED, /* 15d */
166 TPM_UNDEFINED, /* 15e */
167 TPM_UNDEFINED, /* 15f */
168 TPM_UNDEFINED, /* 160 */
169 TPM_UNDEFINED, /* 161 */
170 TPM_UNDEFINED, /* 162 */
171 TPM_UNDEFINED, /* 163 */
172 TPM_UNDEFINED, /* 164 */
173 TPM_UNDEFINED, /* 165 */
174 TPM_UNDEFINED, /* 166 */
175 TPM_UNDEFINED, /* 167 */
176 TPM_UNDEFINED, /* 168 */
177 TPM_UNDEFINED, /* 169 */
178 TPM_UNDEFINED, /* 16a */
179 TPM_UNDEFINED, /* 16b */
180 TPM_UNDEFINED, /* 16c */
181 TPM_UNDEFINED, /* 16d */
182 TPM_UNDEFINED, /* 16e */
183 TPM_UNDEFINED, /* 16f */
184 TPM_UNDEFINED, /* 170 */
185 TPM_UNDEFINED, /* 171 */
186 TPM_UNDEFINED, /* 172 */
187 TPM_UNDEFINED, /* 173 */
188 TPM_UNDEFINED, /* 174 */
189 TPM_UNDEFINED, /* 175 */
190 TPM_UNDEFINED, /* 176 */
191 TPM_LONG, /* 177 */
192 TPM_UNDEFINED, /* 178 */
193 TPM_UNDEFINED, /* 179 */
194 TPM_MEDIUM, /* 17a */
195 TPM_LONG, /* 17b */
196 TPM_UNDEFINED, /* 17c */
197 TPM_UNDEFINED, /* 17d */
198 TPM_UNDEFINED, /* 17e */
199 TPM_UNDEFINED, /* 17f */
200 TPM_UNDEFINED, /* 180 */
201 TPM_UNDEFINED, /* 181 */
202 TPM_MEDIUM, /* 182 */
203 TPM_UNDEFINED, /* 183 */
204 TPM_UNDEFINED, /* 184 */
205 TPM_MEDIUM, /* 185 */
206 TPM_MEDIUM, /* 186 */
207 TPM_UNDEFINED, /* 187 */
208 TPM_UNDEFINED, /* 188 */
209 TPM_UNDEFINED, /* 189 */
210 TPM_UNDEFINED, /* 18a */
211 TPM_UNDEFINED, /* 18b */
212 TPM_UNDEFINED, /* 18c */
213 TPM_UNDEFINED, /* 18d */
214 TPM_UNDEFINED, /* 18e */
215 TPM_UNDEFINED /* 18f */
216};
217
218#define TPM2_PCR_READ_IN_SIZE \
219 (sizeof(struct tpm_input_header) + \
220 sizeof(struct tpm2_pcr_read_in))
221
222static const struct tpm_input_header tpm2_pcrread_header = {
223 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
224 .length = cpu_to_be32(TPM2_PCR_READ_IN_SIZE),
225 .ordinal = cpu_to_be32(TPM2_CC_PCR_READ)
226};
227
Roberto Sassu91f7f3d2017-06-23 15:41:56 +0200228struct tpm2_pcr_read_out {
229 __be32 update_cnt;
230 __be32 pcr_selects_cnt;
231 __be16 hash_alg;
232 u8 pcr_select_size;
233 u8 pcr_select[TPM2_PCR_SELECT_MIN];
234 __be32 digests_cnt;
235 __be16 digest_size;
236 u8 digest[];
237} __packed;
238
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800239/**
240 * tpm2_pcr_read() - read a PCR value
241 * @chip: TPM chip to use.
242 * @pcr_idx: index of the PCR to read.
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200243 * @res_buf: buffer to store the resulting hash.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800244 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200245 * Return: Same as with tpm_transmit_cmd.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800246 */
247int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
248{
249 int rc;
Roberto Sassu91f7f3d2017-06-23 15:41:56 +0200250 struct tpm_buf buf;
251 struct tpm2_pcr_read_out *out;
252 u8 pcr_select[TPM2_PCR_SELECT_MIN] = {0};
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800253
254 if (pcr_idx >= TPM2_PLATFORM_PCR)
255 return -EINVAL;
256
Roberto Sassu91f7f3d2017-06-23 15:41:56 +0200257 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_READ);
258 if (rc)
259 return rc;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800260
Roberto Sassu91f7f3d2017-06-23 15:41:56 +0200261 pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800262
Roberto Sassu91f7f3d2017-06-23 15:41:56 +0200263 tpm_buf_append_u32(&buf, 1);
264 tpm_buf_append_u16(&buf, TPM2_ALG_SHA1);
265 tpm_buf_append_u8(&buf, TPM2_PCR_SELECT_MIN);
266 tpm_buf_append(&buf, (const unsigned char *)pcr_select,
267 sizeof(pcr_select));
268
269 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
270 res_buf ? "attempting to read a pcr value" : NULL);
271 if (rc == 0 && res_buf) {
272 out = (struct tpm2_pcr_read_out *)&buf.data[TPM_HEADER_SIZE];
273 memcpy(res_buf, out->digest, SHA1_DIGEST_SIZE);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800274 }
275
Roberto Sassu91f7f3d2017-06-23 15:41:56 +0200276 tpm_buf_destroy(&buf);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800277 return rc;
278}
279
Nayna Jainc1f92b42017-01-30 04:59:41 -0500280struct tpm2_null_auth_area {
281 __be32 handle;
282 __be16 nonce_size;
283 u8 attributes;
284 __be16 auth_size;
285} __packed;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800286
287/**
288 * tpm2_pcr_extend() - extend a PCR value
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200289 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800290 * @chip: TPM chip to use.
291 * @pcr_idx: index of the PCR.
Nayna Jainc1f92b42017-01-30 04:59:41 -0500292 * @count: number of digests passed.
293 * @digests: list of pcr banks and corresponding digest values to extend.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800294 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200295 * Return: Same as with tpm_transmit_cmd.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800296 */
Nayna Jainc1f92b42017-01-30 04:59:41 -0500297int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
298 struct tpm2_digest *digests)
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800299{
Nayna Jainc1f92b42017-01-30 04:59:41 -0500300 struct tpm_buf buf;
301 struct tpm2_null_auth_area auth_area;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800302 int rc;
Nayna Jainc1f92b42017-01-30 04:59:41 -0500303 int i;
304 int j;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800305
Nayna Jainc1f92b42017-01-30 04:59:41 -0500306 if (count > ARRAY_SIZE(chip->active_banks))
307 return -EINVAL;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800308
Nayna Jainc1f92b42017-01-30 04:59:41 -0500309 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_EXTEND);
310 if (rc)
311 return rc;
312
313 tpm_buf_append_u32(&buf, pcr_idx);
314
315 auth_area.handle = cpu_to_be32(TPM2_RS_PW);
316 auth_area.nonce_size = 0;
317 auth_area.attributes = 0;
318 auth_area.auth_size = 0;
319
320 tpm_buf_append_u32(&buf, sizeof(struct tpm2_null_auth_area));
321 tpm_buf_append(&buf, (const unsigned char *)&auth_area,
322 sizeof(auth_area));
323 tpm_buf_append_u32(&buf, count);
324
325 for (i = 0; i < count; i++) {
326 for (j = 0; j < ARRAY_SIZE(tpm2_hash_map); j++) {
327 if (digests[i].alg_id != tpm2_hash_map[j].tpm_id)
328 continue;
329 tpm_buf_append_u16(&buf, digests[i].alg_id);
330 tpm_buf_append(&buf, (const unsigned char
331 *)&digests[i].digest,
332 hash_digest_size[tpm2_hash_map[j].crypto_id]);
333 }
334 }
335
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200336 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800337 "attempting extend a PCR value");
338
Nayna Jainc1f92b42017-01-30 04:59:41 -0500339 tpm_buf_destroy(&buf);
340
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800341 return rc;
342}
343
Nayna Jainc1f92b42017-01-30 04:59:41 -0500344
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800345#define TPM2_GETRANDOM_IN_SIZE \
346 (sizeof(struct tpm_input_header) + \
347 sizeof(struct tpm2_get_random_in))
348
349static const struct tpm_input_header tpm2_getrandom_header = {
350 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
351 .length = cpu_to_be32(TPM2_GETRANDOM_IN_SIZE),
352 .ordinal = cpu_to_be32(TPM2_CC_GET_RANDOM)
353};
354
355/**
356 * tpm2_get_random() - get random bytes from the TPM RNG
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200357 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800358 * @chip: TPM chip to use
359 * @out: destination buffer for the random bytes
360 * @max: the max number of bytes to write to @out
361 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200362 * Return:
363 * Size of the output buffer, or -EIO on error.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800364 */
365int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)
366{
367 struct tpm2_cmd cmd;
Stefan Bergerc659af72017-01-19 07:19:12 -0500368 u32 recd, rlength;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800369 u32 num_bytes;
370 int err;
371 int total = 0;
372 int retries = 5;
373 u8 *dest = out;
374
375 num_bytes = min_t(u32, max, sizeof(cmd.params.getrandom_out.buffer));
376
377 if (!out || !num_bytes ||
378 max > sizeof(cmd.params.getrandom_out.buffer))
379 return -EINVAL;
380
381 do {
382 cmd.header.in = tpm2_getrandom_header;
383 cmd.params.getrandom_in.size = cpu_to_be16(num_bytes);
384
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200385 err = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd),
Stefan Bergerc659af72017-01-19 07:19:12 -0500386 offsetof(struct tpm2_get_random_out,
387 buffer),
388 0, "attempting get random");
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800389 if (err)
390 break;
391
392 recd = min_t(u32, be16_to_cpu(cmd.params.getrandom_out.size),
393 num_bytes);
Stefan Bergerc659af72017-01-19 07:19:12 -0500394 rlength = be32_to_cpu(cmd.header.out.length);
395 if (rlength < offsetof(struct tpm2_get_random_out, buffer) +
396 recd)
397 return -EFAULT;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800398 memcpy(dest, cmd.params.getrandom_out.buffer, recd);
399
400 dest += recd;
401 total += recd;
402 num_bytes -= recd;
403 } while (retries-- && total < max);
404
405 return total ? total : -EIO;
406}
407
408#define TPM2_GET_TPM_PT_IN_SIZE \
409 (sizeof(struct tpm_input_header) + \
410 sizeof(struct tpm2_get_tpm_pt_in))
411
Stefan Bergerc659af72017-01-19 07:19:12 -0500412#define TPM2_GET_TPM_PT_OUT_BODY_SIZE \
413 sizeof(struct tpm2_get_tpm_pt_out)
414
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800415static const struct tpm_input_header tpm2_get_tpm_pt_header = {
416 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
417 .length = cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE),
418 .ordinal = cpu_to_be32(TPM2_CC_GET_CAPABILITY)
419};
420
421/**
Jarkko Sakkinen9aa36b32016-11-26 13:39:35 +0200422 * tpm2_flush_context_cmd() - execute a TPM2_FlushContext command
423 * @chip: TPM chip to use
424 * @payload: the key data in clear and encrypted form
425 * @options: authentication values and other options
426 *
427 * Return: same as with tpm_transmit_cmd
428 */
429void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
430 unsigned int flags)
431{
432 struct tpm_buf buf;
433 int rc;
434
435 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT);
436 if (rc) {
437 dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
438 handle);
439 return;
440 }
441
442 tpm_buf_append_u32(&buf, handle);
443
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200444 (void) tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, flags,
Jarkko Sakkinen9aa36b32016-11-26 13:39:35 +0200445 "flushing context");
446
447 tpm_buf_destroy(&buf);
448}
449
450/**
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200451 * tpm_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300452 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200453 * @buf: an allocated tpm_buf instance
454 * @session_handle: session handle
455 * @nonce: the session nonce, may be NULL if not used
456 * @nonce_len: the session nonce length, may be 0 if not used
457 * @attributes: the session attributes
458 * @hmac: the session HMAC or password, may be NULL if not used
459 * @hmac_len: the session HMAC or password length, maybe 0 if not used
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300460 */
461static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,
462 const u8 *nonce, u16 nonce_len,
463 u8 attributes,
464 const u8 *hmac, u16 hmac_len)
465{
466 tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);
467 tpm_buf_append_u32(buf, session_handle);
468 tpm_buf_append_u16(buf, nonce_len);
469
470 if (nonce && nonce_len)
471 tpm_buf_append(buf, nonce, nonce_len);
472
473 tpm_buf_append_u8(buf, attributes);
474 tpm_buf_append_u16(buf, hmac_len);
475
476 if (hmac && hmac_len)
477 tpm_buf_append(buf, hmac, hmac_len);
478}
479
480/**
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300481 * tpm2_seal_trusted() - seal the payload of a trusted key
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200482 *
483 * @chip: TPM chip to use
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300484 * @payload: the key data in clear and encrypted form
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300485 * @options: authentication values and other options
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300486 *
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300487 * Return: < 0 on error and 0 on success.
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300488 */
489int tpm2_seal_trusted(struct tpm_chip *chip,
490 struct trusted_key_payload *payload,
491 struct trusted_key_options *options)
492{
493 unsigned int blob_len;
494 struct tpm_buf buf;
Stefan Bergerc659af72017-01-19 07:19:12 -0500495 u32 hash, rlength;
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +0200496 int i;
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300497 int rc;
498
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +0200499 for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
500 if (options->hash == tpm2_hash_map[i].crypto_id) {
501 hash = tpm2_hash_map[i].tpm_id;
502 break;
503 }
504 }
505
506 if (i == ARRAY_SIZE(tpm2_hash_map))
507 return -EINVAL;
508
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300509 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
510 if (rc)
511 return rc;
512
513 tpm_buf_append_u32(&buf, options->keyhandle);
514 tpm2_buf_append_auth(&buf, TPM2_RS_PW,
515 NULL /* nonce */, 0,
516 0 /* session_attributes */,
517 options->keyauth /* hmac */,
518 TPM_DIGEST_SIZE);
519
520 /* sensitive */
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200521 tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300522
523 tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE);
524 tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE);
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200525 tpm_buf_append_u16(&buf, payload->key_len + 1);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300526 tpm_buf_append(&buf, payload->key, payload->key_len);
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200527 tpm_buf_append_u8(&buf, payload->migratable);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300528
529 /* public */
Jarkko Sakkinenf3c82ad2016-01-06 16:43:30 +0200530 tpm_buf_append_u16(&buf, 14 + options->policydigest_len);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300531 tpm_buf_append_u16(&buf, TPM2_ALG_KEYEDHASH);
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +0200532 tpm_buf_append_u16(&buf, hash);
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200533
534 /* policy */
Jarkko Sakkinenf3c82ad2016-01-06 16:43:30 +0200535 if (options->policydigest_len) {
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200536 tpm_buf_append_u32(&buf, 0);
Jarkko Sakkinenf3c82ad2016-01-06 16:43:30 +0200537 tpm_buf_append_u16(&buf, options->policydigest_len);
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200538 tpm_buf_append(&buf, options->policydigest,
Jarkko Sakkinenf3c82ad2016-01-06 16:43:30 +0200539 options->policydigest_len);
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200540 } else {
Jarkko Sakkinenc0b5eed2016-02-13 11:51:23 +0200541 tpm_buf_append_u32(&buf, TPM2_OA_USER_WITH_AUTH);
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200542 tpm_buf_append_u16(&buf, 0);
543 }
544
545 /* public parameters */
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300546 tpm_buf_append_u16(&buf, TPM2_ALG_NULL);
547 tpm_buf_append_u16(&buf, 0);
548
549 /* outside info */
550 tpm_buf_append_u16(&buf, 0);
551
552 /* creation PCR */
553 tpm_buf_append_u32(&buf, 0);
554
555 if (buf.flags & TPM_BUF_OVERFLOW) {
556 rc = -E2BIG;
557 goto out;
558 }
559
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200560 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, 0,
Stefan Bergerc659af72017-01-19 07:19:12 -0500561 "sealing data");
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300562 if (rc)
563 goto out;
564
565 blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]);
566 if (blob_len > MAX_BLOB_SIZE) {
567 rc = -E2BIG;
568 goto out;
569 }
Stefan Bergerc659af72017-01-19 07:19:12 -0500570 rlength = be32_to_cpu(((struct tpm2_cmd *)&buf)->header.out.length);
571 if (rlength < TPM_HEADER_SIZE + 4 + blob_len) {
572 rc = -EFAULT;
573 goto out;
574 }
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300575
576 memcpy(payload->blob, &buf.data[TPM_HEADER_SIZE + 4], blob_len);
577 payload->blob_len = blob_len;
578
579out:
580 tpm_buf_destroy(&buf);
581
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +0200582 if (rc > 0) {
Jarkko Sakkinen7d761112017-01-25 23:00:22 +0200583 if (tpm2_rc_value(rc) == TPM2_RC_HASH)
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +0200584 rc = -EINVAL;
585 else
586 rc = -EPERM;
587 }
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300588
589 return rc;
590}
591
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300592/**
593 * tpm2_load_cmd() - execute a TPM2_Load command
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200594 *
595 * @chip: TPM chip to use
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300596 * @payload: the key data in clear and encrypted form
597 * @options: authentication values and other options
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200598 * @blob_handle: returned blob handle
599 * @flags: tpm transmit flags
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300600 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200601 * Return: 0 on success.
602 * -E2BIG on wrong payload size.
603 * -EPERM on tpm error status.
604 * < 0 error from tpm_transmit_cmd.
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300605 */
606static int tpm2_load_cmd(struct tpm_chip *chip,
607 struct trusted_key_payload *payload,
608 struct trusted_key_options *options,
609 u32 *blob_handle, unsigned int flags)
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300610{
611 struct tpm_buf buf;
612 unsigned int private_len;
613 unsigned int public_len;
614 unsigned int blob_len;
615 int rc;
616
617 private_len = be16_to_cpup((__be16 *) &payload->blob[0]);
618 if (private_len > (payload->blob_len - 2))
619 return -E2BIG;
620
621 public_len = be16_to_cpup((__be16 *) &payload->blob[2 + private_len]);
622 blob_len = private_len + public_len + 4;
623 if (blob_len > payload->blob_len)
624 return -E2BIG;
625
626 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
627 if (rc)
628 return rc;
629
630 tpm_buf_append_u32(&buf, options->keyhandle);
631 tpm2_buf_append_auth(&buf, TPM2_RS_PW,
632 NULL /* nonce */, 0,
633 0 /* session_attributes */,
634 options->keyauth /* hmac */,
635 TPM_DIGEST_SIZE);
636
637 tpm_buf_append(&buf, payload->blob, blob_len);
638
639 if (buf.flags & TPM_BUF_OVERFLOW) {
640 rc = -E2BIG;
641 goto out;
642 }
643
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200644 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, flags,
Stefan Bergerc659af72017-01-19 07:19:12 -0500645 "loading blob");
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300646 if (!rc)
647 *blob_handle = be32_to_cpup(
648 (__be32 *) &buf.data[TPM_HEADER_SIZE]);
649
650out:
651 tpm_buf_destroy(&buf);
652
653 if (rc > 0)
654 rc = -EPERM;
655
656 return rc;
657}
658
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300659/**
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300660 * tpm2_unseal_cmd() - execute a TPM2_Unload command
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200661 *
662 * @chip: TPM chip to use
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300663 * @payload: the key data in clear and encrypted form
664 * @options: authentication values and other options
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200665 * @blob_handle: blob handle
666 * @flags: tpm_transmit_cmd flags
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300667 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200668 * Return: 0 on success
669 * -EPERM on tpm error status
670 * < 0 error from tpm_transmit_cmd
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300671 */
672static int tpm2_unseal_cmd(struct tpm_chip *chip,
673 struct trusted_key_payload *payload,
674 struct trusted_key_options *options,
675 u32 blob_handle, unsigned int flags)
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300676{
677 struct tpm_buf buf;
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200678 u16 data_len;
679 u8 *data;
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300680 int rc;
Stefan Bergerc659af72017-01-19 07:19:12 -0500681 u32 rlength;
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300682
683 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
684 if (rc)
685 return rc;
686
687 tpm_buf_append_u32(&buf, blob_handle);
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200688 tpm2_buf_append_auth(&buf,
689 options->policyhandle ?
690 options->policyhandle : TPM2_RS_PW,
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300691 NULL /* nonce */, 0,
Jarkko Sakkinenc0b5eed2016-02-13 11:51:23 +0200692 TPM2_SA_CONTINUE_SESSION,
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300693 options->blobauth /* hmac */,
694 TPM_DIGEST_SIZE);
695
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200696 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 6, flags,
Stefan Bergerc659af72017-01-19 07:19:12 -0500697 "unsealing");
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300698 if (rc > 0)
699 rc = -EPERM;
700
701 if (!rc) {
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200702 data_len = be16_to_cpup(
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300703 (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
Stefan Bergerc659af72017-01-19 07:19:12 -0500704
705 rlength = be32_to_cpu(((struct tpm2_cmd *)&buf)
706 ->header.out.length);
707 if (rlength < TPM_HEADER_SIZE + 6 + data_len) {
708 rc = -EFAULT;
709 goto out;
710 }
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200711 data = &buf.data[TPM_HEADER_SIZE + 6];
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300712
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200713 memcpy(payload->key, data, data_len - 1);
714 payload->key_len = data_len - 1;
715 payload->migratable = data[data_len - 1];
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300716 }
717
Stefan Bergerc659af72017-01-19 07:19:12 -0500718out:
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300719 tpm_buf_destroy(&buf);
720 return rc;
721}
722
723/**
Baruch Siachcbef69a2016-11-06 11:02:45 +0200724 * tpm2_unseal_trusted() - unseal the payload of a trusted key
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200725 *
726 * @chip: TPM chip to use
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300727 * @payload: the key data in clear and encrypted form
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300728 * @options: authentication values and other options
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300729 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200730 * Return: Same as with tpm_transmit_cmd.
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300731 */
732int tpm2_unseal_trusted(struct tpm_chip *chip,
733 struct trusted_key_payload *payload,
734 struct trusted_key_options *options)
735{
736 u32 blob_handle;
737 int rc;
738
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300739 mutex_lock(&chip->tpm_mutex);
740 rc = tpm2_load_cmd(chip, payload, options, &blob_handle,
741 TPM_TRANSMIT_UNLOCKED);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300742 if (rc)
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300743 goto out;
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300744
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300745 rc = tpm2_unseal_cmd(chip, payload, options, blob_handle,
746 TPM_TRANSMIT_UNLOCKED);
747 tpm2_flush_context_cmd(chip, blob_handle, TPM_TRANSMIT_UNLOCKED);
748out:
749 mutex_unlock(&chip->tpm_mutex);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300750 return rc;
751}
752
753/**
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800754 * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
755 * @chip: TPM chip to use.
756 * @property_id: property ID.
757 * @value: output variable.
758 * @desc: passed to tpm_transmit_cmd()
759 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200760 * Return: Same as with tpm_transmit_cmd.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800761 */
762ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
763 const char *desc)
764{
765 struct tpm2_cmd cmd;
766 int rc;
767
768 cmd.header.in = tpm2_get_tpm_pt_header;
769 cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
770 cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id);
771 cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
772
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200773 rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd),
Stefan Bergerc659af72017-01-19 07:19:12 -0500774 TPM2_GET_TPM_PT_OUT_BODY_SIZE, 0, desc);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800775 if (!rc)
apronin@chromium.org1b0612b2016-07-14 18:07:18 -0700776 *value = be32_to_cpu(cmd.params.get_tpm_pt_out.value);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800777
778 return rc;
779}
Jarkko Sakkineneb5854e2016-06-12 16:42:09 +0300780EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800781
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800782#define TPM2_SHUTDOWN_IN_SIZE \
783 (sizeof(struct tpm_input_header) + \
784 sizeof(struct tpm2_startup_in))
785
786static const struct tpm_input_header tpm2_shutdown_header = {
787 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
788 .length = cpu_to_be32(TPM2_SHUTDOWN_IN_SIZE),
789 .ordinal = cpu_to_be32(TPM2_CC_SHUTDOWN)
790};
791
792/**
793 * tpm2_shutdown() - send shutdown command to the TPM chip
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200794 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800795 * @chip: TPM chip to use.
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200796 * @shutdown_type: shutdown type. The value is either
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800797 * TPM_SU_CLEAR or TPM_SU_STATE.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800798 */
Jarkko Sakkinen74d6b3c2015-01-29 07:43:47 +0200799void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800800{
801 struct tpm2_cmd cmd;
Jarkko Sakkinen74d6b3c2015-01-29 07:43:47 +0200802 int rc;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800803
804 cmd.header.in = tpm2_shutdown_header;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800805 cmd.params.startup_in.startup_type = cpu_to_be16(shutdown_type);
Jarkko Sakkinen74d6b3c2015-01-29 07:43:47 +0200806
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200807 rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0,
Stefan Bergerc659af72017-01-19 07:19:12 -0500808 "stopping the TPM");
Jarkko Sakkinen74d6b3c2015-01-29 07:43:47 +0200809
810 /* In places where shutdown command is sent there's no much we can do
811 * except print the error code on a system failure.
812 */
Stefan Berger402149c2017-05-25 18:29:13 -0400813 if (rc < 0 && rc != -EPIPE)
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500814 dev_warn(&chip->dev, "transmit returned %d while stopping the TPM",
Jarkko Sakkinen74d6b3c2015-01-29 07:43:47 +0200815 rc);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800816}
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800817
818/*
819 * tpm2_calc_ordinal_duration() - maximum duration for a command
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200820 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800821 * @chip: TPM chip to use.
822 * @ordinal: command code number.
823 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200824 * Return: maximum duration for a command
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800825 */
826unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
827{
828 int index = TPM_UNDEFINED;
829 int duration = 0;
830
831 if (ordinal >= TPM2_CC_FIRST && ordinal <= TPM2_CC_LAST)
832 index = tpm2_ordinal_duration[ordinal - TPM2_CC_FIRST];
833
834 if (index != TPM_UNDEFINED)
Christophe Ricardaf782f32016-03-31 22:56:59 +0200835 duration = chip->duration[index];
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800836
837 if (duration <= 0)
838 duration = 2 * 60 * HZ;
839
840 return duration;
841}
842EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration);
843
844#define TPM2_SELF_TEST_IN_SIZE \
845 (sizeof(struct tpm_input_header) + \
846 sizeof(struct tpm2_self_test_in))
847
848static const struct tpm_input_header tpm2_selftest_header = {
849 .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
850 .length = cpu_to_be32(TPM2_SELF_TEST_IN_SIZE),
851 .ordinal = cpu_to_be32(TPM2_CC_SELF_TEST)
852};
853
854/**
855 * tpm2_continue_selftest() - start a self test
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200856 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800857 * @chip: TPM chip to use
858 * @full: test all commands instead of testing only those that were not
859 * previously tested.
860 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200861 * Return: Same as with tpm_transmit_cmd with exception of RC_TESTING.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800862 */
863static int tpm2_start_selftest(struct tpm_chip *chip, bool full)
864{
865 int rc;
866 struct tpm2_cmd cmd;
867
868 cmd.header.in = tpm2_selftest_header;
869 cmd.params.selftest_in.full_test = full;
870
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200871 rc = tpm_transmit_cmd(chip, NULL, &cmd, TPM2_SELF_TEST_IN_SIZE, 0, 0,
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800872 "continue selftest");
873
874 /* At least some prototype chips seem to give RC_TESTING error
875 * immediately. This is a workaround for that.
876 */
877 if (rc == TPM2_RC_TESTING) {
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500878 dev_warn(&chip->dev, "Got RC_TESTING, ignoring\n");
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800879 rc = 0;
880 }
881
882 return rc;
883}
884
885/**
886 * tpm2_do_selftest() - run a full self test
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200887 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800888 * @chip: TPM chip to use
889 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200890 * Return: Same as with tpm_transmit_cmd.
891 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800892 * During the self test TPM2 commands return with the error code RC_TESTING.
893 * Waiting is done by issuing PCR read until it executes successfully.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800894 */
Jason Gunthorpecae8b442016-07-12 11:41:49 -0600895static int tpm2_do_selftest(struct tpm_chip *chip)
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800896{
897 int rc;
898 unsigned int loops;
899 unsigned int delay_msec = 100;
900 unsigned long duration;
901 struct tpm2_cmd cmd;
902 int i;
903
904 duration = tpm2_calc_ordinal_duration(chip, TPM2_CC_SELF_TEST);
905
906 loops = jiffies_to_msecs(duration) / delay_msec;
907
908 rc = tpm2_start_selftest(chip, true);
909 if (rc)
910 return rc;
911
912 for (i = 0; i < loops; i++) {
913 /* Attempt to read a PCR value */
914 cmd.header.in = tpm2_pcrread_header;
915 cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
916 cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
917 cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
918 cmd.params.pcrread_in.pcr_select[0] = 0x01;
919 cmd.params.pcrread_in.pcr_select[1] = 0x00;
920 cmd.params.pcrread_in.pcr_select[2] = 0x00;
921
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200922 rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0,
923 NULL);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800924 if (rc < 0)
925 break;
926
927 rc = be32_to_cpu(cmd.header.out.return_code);
928 if (rc != TPM2_RC_TESTING)
929 break;
930
931 msleep(delay_msec);
932 }
933
934 return rc;
935}
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800936
937/**
Jarkko Sakkinen4d5f2052015-02-04 16:21:09 +0200938 * tpm2_probe() - probe TPM 2.0
939 * @chip: TPM chip to use
940 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200941 * Return: < 0 error and 0 on success.
942 *
Jarkko Sakkinen4d5f2052015-02-04 16:21:09 +0200943 * Send idempotent TPM 2.0 command and see whether TPM 2.0 chip replied based on
944 * the reply tag.
945 */
946int tpm2_probe(struct tpm_chip *chip)
947{
948 struct tpm2_cmd cmd;
949 int rc;
950
951 cmd.header.in = tpm2_get_tpm_pt_header;
952 cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
953 cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100);
954 cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
955
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200956 rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0, NULL);
Jarkko Sakkinen4d5f2052015-02-04 16:21:09 +0200957 if (rc < 0)
958 return rc;
Jarkko Sakkinen4d5f2052015-02-04 16:21:09 +0200959
960 if (be16_to_cpu(cmd.header.out.tag) == TPM2_ST_NO_SESSIONS)
961 chip->flags |= TPM_CHIP_FLAG_TPM2;
962
963 return 0;
964}
965EXPORT_SYMBOL_GPL(tpm2_probe);
Jason Gunthorpecae8b442016-07-12 11:41:49 -0600966
Nayna Jain1db15342017-01-30 04:59:40 -0500967struct tpm2_pcr_selection {
968 __be16 hash_alg;
969 u8 size_of_select;
970 u8 pcr_select[3];
971} __packed;
972
Jarkko Sakkinen61841be2017-02-15 20:02:28 +0200973static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
Nayna Jain1db15342017-01-30 04:59:40 -0500974{
975 struct tpm2_pcr_selection pcr_selection;
976 struct tpm_buf buf;
977 void *marker;
978 void *end;
979 void *pcr_select_offset;
980 unsigned int count;
981 u32 sizeof_pcr_selection;
982 u32 rsp_len;
983 int rc;
984 int i = 0;
985
986 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
987 if (rc)
988 return rc;
989
990 tpm_buf_append_u32(&buf, TPM2_CAP_PCRS);
991 tpm_buf_append_u32(&buf, 0);
992 tpm_buf_append_u32(&buf, 1);
993
Jarkko Sakkinen745b3612017-01-06 14:03:45 +0200994 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 9, 0,
Nayna Jain1db15342017-01-30 04:59:40 -0500995 "get tpm pcr allocation");
996 if (rc)
997 goto out;
998
999 count = be32_to_cpup(
1000 (__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);
1001
1002 if (count > ARRAY_SIZE(chip->active_banks)) {
1003 rc = -ENODEV;
1004 goto out;
1005 }
1006
1007 marker = &buf.data[TPM_HEADER_SIZE + 9];
1008
1009 rsp_len = be32_to_cpup((__be32 *)&buf.data[2]);
1010 end = &buf.data[rsp_len];
1011
1012 for (i = 0; i < count; i++) {
1013 pcr_select_offset = marker +
1014 offsetof(struct tpm2_pcr_selection, size_of_select);
1015 if (pcr_select_offset >= end) {
1016 rc = -EFAULT;
1017 break;
1018 }
1019
1020 memcpy(&pcr_selection, marker, sizeof(pcr_selection));
1021 chip->active_banks[i] = be16_to_cpu(pcr_selection.hash_alg);
1022 sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
1023 sizeof(pcr_selection.size_of_select) +
1024 pcr_selection.size_of_select;
1025 marker = marker + sizeof_pcr_selection;
1026 }
1027
1028out:
1029 if (i < ARRAY_SIZE(chip->active_banks))
1030 chip->active_banks[i] = TPM2_ALG_ERROR;
1031
1032 tpm_buf_destroy(&buf);
1033
1034 return rc;
1035}
Jarkko Sakkinen61841be2017-02-15 20:02:28 +02001036
Jarkko Sakkinen58472f52016-11-10 20:42:07 -08001037static int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip)
1038{
1039 struct tpm_buf buf;
1040 u32 nr_commands;
1041 u32 *attrs;
1042 u32 cc;
1043 int i;
1044 int rc;
1045
1046 rc = tpm2_get_tpm_pt(chip, TPM_PT_TOTAL_COMMANDS, &nr_commands, NULL);
1047 if (rc)
1048 goto out;
1049
1050 if (nr_commands > 0xFFFFF) {
1051 rc = -EFAULT;
1052 goto out;
1053 }
1054
1055 chip->cc_attrs_tbl = devm_kzalloc(&chip->dev, 4 * nr_commands,
1056 GFP_KERNEL);
1057
1058 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
1059 if (rc)
1060 goto out;
1061
1062 tpm_buf_append_u32(&buf, TPM2_CAP_COMMANDS);
1063 tpm_buf_append_u32(&buf, TPM2_CC_FIRST);
1064 tpm_buf_append_u32(&buf, nr_commands);
1065
Jarkko Sakkinen745b3612017-01-06 14:03:45 +02001066 rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
1067 9 + 4 * nr_commands, 0, NULL);
Jarkko Sakkinen58472f52016-11-10 20:42:07 -08001068 if (rc) {
1069 tpm_buf_destroy(&buf);
1070 goto out;
1071 }
1072
1073 if (nr_commands !=
1074 be32_to_cpup((__be32 *)&buf.data[TPM_HEADER_SIZE + 5])) {
1075 tpm_buf_destroy(&buf);
1076 goto out;
1077 }
1078
1079 chip->nr_commands = nr_commands;
1080
1081 attrs = (u32 *)&buf.data[TPM_HEADER_SIZE + 9];
1082 for (i = 0; i < nr_commands; i++, attrs++) {
1083 chip->cc_attrs_tbl[i] = be32_to_cpup(attrs);
1084 cc = chip->cc_attrs_tbl[i] & 0xFFFF;
1085
1086 if (cc == TPM2_CC_CONTEXT_SAVE || cc == TPM2_CC_FLUSH_CONTEXT) {
1087 chip->cc_attrs_tbl[i] &=
1088 ~(GENMASK(2, 0) << TPM2_CC_ATTR_CHANDLES);
1089 chip->cc_attrs_tbl[i] |= 1 << TPM2_CC_ATTR_CHANDLES;
1090 }
1091 }
1092
1093 tpm_buf_destroy(&buf);
1094
1095out:
1096 if (rc > 0)
1097 rc = -ENODEV;
1098 return rc;
1099}
1100
Jarkko Sakkinen61841be2017-02-15 20:02:28 +02001101/**
1102 * tpm2_auto_startup - Perform the standard automatic TPM initialization
1103 * sequence
1104 * @chip: TPM chip to use
1105 *
Jarkko Sakkinen58472f52016-11-10 20:42:07 -08001106 * Returns 0 on success, < 0 in case of fatal error.
Jarkko Sakkinen61841be2017-02-15 20:02:28 +02001107 */
1108int tpm2_auto_startup(struct tpm_chip *chip)
1109{
1110 int rc;
1111
1112 rc = tpm_get_timeouts(chip);
1113 if (rc)
1114 goto out;
1115
1116 rc = tpm2_do_selftest(chip);
1117 if (rc != 0 && rc != TPM2_RC_INITIALIZE) {
1118 dev_err(&chip->dev, "TPM self test failed\n");
1119 goto out;
1120 }
1121
1122 if (rc == TPM2_RC_INITIALIZE) {
Jarkko Sakkinen19cbe4f2017-06-21 09:31:34 +02001123 rc = tpm_startup(chip);
Jarkko Sakkinen61841be2017-02-15 20:02:28 +02001124 if (rc)
1125 goto out;
1126
1127 rc = tpm2_do_selftest(chip);
1128 if (rc) {
1129 dev_err(&chip->dev, "TPM self test failed\n");
1130 goto out;
1131 }
1132 }
1133
1134 rc = tpm2_get_pcr_allocation(chip);
Jarkko Sakkinen58472f52016-11-10 20:42:07 -08001135 if (rc)
1136 goto out;
1137
1138 rc = tpm2_get_cc_attrs_tbl(chip);
Jarkko Sakkinen61841be2017-02-15 20:02:28 +02001139
1140out:
1141 if (rc > 0)
1142 rc = -ENODEV;
1143 return rc;
1144}
Jarkko Sakkinen58472f52016-11-10 20:42:07 -08001145
1146int tpm2_find_cc(struct tpm_chip *chip, u32 cc)
1147{
1148 int i;
1149
1150 for (i = 0; i < chip->nr_commands; i++)
1151 if (cc == (chip->cc_attrs_tbl[i] & GENMASK(15, 0)))
1152 return i;
1153
1154 return -1;
1155}