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