blob: 42fe3dde0d8cf9c1345474595a8ac3dd459718db [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
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
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +0200112struct 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
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800125/*
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
Stefan Bergerc659af72017-01-19 07:19:12 -0500251#define TPM2_PCR_READ_RESP_BODY_SIZE \
252 sizeof(struct tpm2_pcr_read_out)
253
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800254static 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.
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200264 * @res_buf: buffer to store the resulting hash.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800265 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200266 * Return: Same as with tpm_transmit_cmd.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800267 */
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
Stefan Bergerc659af72017-01-19 07:19:12 -0500286 rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
287 TPM2_PCR_READ_RESP_BODY_SIZE,
288 0, "attempting to read a pcr value");
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800289 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
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200309 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800310 * @chip: TPM chip to use.
311 * @pcr_idx: index of the PCR.
312 * @hash: hash value to use for the extend operation.
313 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200314 * Return: Same as with tpm_transmit_cmd.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800315 */
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
Stefan Bergerc659af72017-01-19 07:19:12 -0500334 rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, 0,
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800335 "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
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200352 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800353 * @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 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200357 * Return:
358 * Size of the output buffer, or -EIO on error.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800359 */
360int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)
361{
362 struct tpm2_cmd cmd;
Stefan Bergerc659af72017-01-19 07:19:12 -0500363 u32 recd, rlength;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800364 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
Stefan Bergerc659af72017-01-19 07:19:12 -0500380 err = tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
381 offsetof(struct tpm2_get_random_out,
382 buffer),
383 0, "attempting get random");
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800384 if (err)
385 break;
386
387 recd = min_t(u32, be16_to_cpu(cmd.params.getrandom_out.size),
388 num_bytes);
Stefan Bergerc659af72017-01-19 07:19:12 -0500389 rlength = be32_to_cpu(cmd.header.out.length);
390 if (rlength < offsetof(struct tpm2_get_random_out, buffer) +
391 recd)
392 return -EFAULT;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800393 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
Stefan Bergerc659af72017-01-19 07:19:12 -0500407#define TPM2_GET_TPM_PT_OUT_BODY_SIZE \
408 sizeof(struct tpm2_get_tpm_pt_out)
409
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800410static 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
416/**
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200417 * tpm_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300418 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200419 * @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
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300426 */
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/**
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300447 * tpm2_seal_trusted() - seal the payload of a trusted key
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200448 *
449 * @chip: TPM chip to use
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300450 * @payload: the key data in clear and encrypted form
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300451 * @options: authentication values and other options
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300452 *
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300453 * Return: < 0 on error and 0 on success.
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300454 */
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;
Stefan Bergerc659af72017-01-19 07:19:12 -0500461 u32 hash, rlength;
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +0200462 int i;
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300463 int rc;
464
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +0200465 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
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300475 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 */
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200487 tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300488
489 tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE);
490 tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE);
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200491 tpm_buf_append_u16(&buf, payload->key_len + 1);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300492 tpm_buf_append(&buf, payload->key, payload->key_len);
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200493 tpm_buf_append_u8(&buf, payload->migratable);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300494
495 /* public */
Jarkko Sakkinenf3c82ad2016-01-06 16:43:30 +0200496 tpm_buf_append_u16(&buf, 14 + options->policydigest_len);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300497 tpm_buf_append_u16(&buf, TPM2_ALG_KEYEDHASH);
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +0200498 tpm_buf_append_u16(&buf, hash);
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200499
500 /* policy */
Jarkko Sakkinenf3c82ad2016-01-06 16:43:30 +0200501 if (options->policydigest_len) {
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200502 tpm_buf_append_u32(&buf, 0);
Jarkko Sakkinenf3c82ad2016-01-06 16:43:30 +0200503 tpm_buf_append_u16(&buf, options->policydigest_len);
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200504 tpm_buf_append(&buf, options->policydigest,
Jarkko Sakkinenf3c82ad2016-01-06 16:43:30 +0200505 options->policydigest_len);
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200506 } else {
Jarkko Sakkinenc0b5eed2016-02-13 11:51:23 +0200507 tpm_buf_append_u32(&buf, TPM2_OA_USER_WITH_AUTH);
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200508 tpm_buf_append_u16(&buf, 0);
509 }
510
511 /* public parameters */
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300512 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
Stefan Bergerc659af72017-01-19 07:19:12 -0500526 rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, 4, 0,
527 "sealing data");
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300528 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 }
Stefan Bergerc659af72017-01-19 07:19:12 -0500536 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 }
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300541
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
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +0200548 if (rc > 0) {
Jarkko Sakkinen7d761112017-01-25 23:00:22 +0200549 if (tpm2_rc_value(rc) == TPM2_RC_HASH)
Jarkko Sakkinen5ca4c202015-11-05 21:43:06 +0200550 rc = -EINVAL;
551 else
552 rc = -EPERM;
553 }
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300554
555 return rc;
556}
557
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300558/**
559 * tpm2_load_cmd() - execute a TPM2_Load command
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200560 *
561 * @chip: TPM chip to use
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300562 * @payload: the key data in clear and encrypted form
563 * @options: authentication values and other options
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200564 * @blob_handle: returned blob handle
565 * @flags: tpm transmit flags
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300566 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200567 * Return: 0 on success.
568 * -E2BIG on wrong payload size.
569 * -EPERM on tpm error status.
570 * < 0 error from tpm_transmit_cmd.
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300571 */
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)
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300576{
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
Stefan Bergerc659af72017-01-19 07:19:12 -0500610 rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, 4, flags,
611 "loading blob");
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300612 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
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300625/**
626 * tpm2_flush_context_cmd() - execute a TPM2_FlushContext command
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300627 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200628 * @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.
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300633 */
634static void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
635 unsigned int flags)
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300636{
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) {
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500642 dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300643 handle);
644 return;
645 }
646
647 tpm_buf_append_u32(&buf, handle);
648
Stefan Bergerc659af72017-01-19 07:19:12 -0500649 rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, 0, flags,
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300650 "flushing context");
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300651 if (rc)
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500652 dev_warn(&chip->dev, "0x%08x was not flushed, rc=%d\n", handle,
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300653 rc);
654
655 tpm_buf_destroy(&buf);
656}
657
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300658/**
659 * tpm2_unseal_cmd() - execute a TPM2_Unload command
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200660 *
661 * @chip: TPM chip to use
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300662 * @payload: the key data in clear and encrypted form
663 * @options: authentication values and other options
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200664 * @blob_handle: blob handle
665 * @flags: tpm_transmit_cmd flags
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300666 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200667 * Return: 0 on success
668 * -EPERM on tpm error status
669 * < 0 error from tpm_transmit_cmd
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300670 */
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)
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300675{
676 struct tpm_buf buf;
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200677 u16 data_len;
678 u8 *data;
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300679 int rc;
Stefan Bergerc659af72017-01-19 07:19:12 -0500680 u32 rlength;
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300681
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);
Jarkko Sakkinen5beb0c42015-10-31 17:53:44 +0200687 tpm2_buf_append_auth(&buf,
688 options->policyhandle ?
689 options->policyhandle : TPM2_RS_PW,
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300690 NULL /* nonce */, 0,
Jarkko Sakkinenc0b5eed2016-02-13 11:51:23 +0200691 TPM2_SA_CONTINUE_SESSION,
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300692 options->blobauth /* hmac */,
693 TPM_DIGEST_SIZE);
694
Stefan Bergerc659af72017-01-19 07:19:12 -0500695 rc = tpm_transmit_cmd(chip, buf.data, PAGE_SIZE, 6, flags,
696 "unsealing");
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300697 if (rc > 0)
698 rc = -EPERM;
699
700 if (!rc) {
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200701 data_len = be16_to_cpup(
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300702 (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
Stefan Bergerc659af72017-01-19 07:19:12 -0500703
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 }
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200710 data = &buf.data[TPM_HEADER_SIZE + 6];
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300711
Jarkko Sakkinen2e311252015-10-30 14:57:02 +0200712 memcpy(payload->key, data, data_len - 1);
713 payload->key_len = data_len - 1;
714 payload->migratable = data[data_len - 1];
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300715 }
716
Stefan Bergerc659af72017-01-19 07:19:12 -0500717out:
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300718 tpm_buf_destroy(&buf);
719 return rc;
720}
721
722/**
Baruch Siachcbef69a2016-11-06 11:02:45 +0200723 * tpm2_unseal_trusted() - unseal the payload of a trusted key
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200724 *
725 * @chip: TPM chip to use
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300726 * @payload: the key data in clear and encrypted form
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300727 * @options: authentication values and other options
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300728 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200729 * Return: Same as with tpm_transmit_cmd.
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300730 */
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
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300738 mutex_lock(&chip->tpm_mutex);
739 rc = tpm2_load_cmd(chip, payload, options, &blob_handle,
740 TPM_TRANSMIT_UNLOCKED);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300741 if (rc)
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300742 goto out;
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300743
Jarkko Sakkinend4816ed2016-08-16 22:00:38 +0300744 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);
Jarkko Sakkinen954650e2015-05-30 08:09:04 +0300749 return rc;
750}
751
752/**
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800753 * 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 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200759 * Return: Same as with tpm_transmit_cmd.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800760 */
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
Stefan Bergerc659af72017-01-19 07:19:12 -0500772 rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
773 TPM2_GET_TPM_PT_OUT_BODY_SIZE, 0, desc);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800774 if (!rc)
apronin@chromium.org1b0612b2016-07-14 18:07:18 -0700775 *value = be32_to_cpu(cmd.params.get_tpm_pt_out.value);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800776
777 return rc;
778}
Jarkko Sakkineneb5854e2016-06-12 16:42:09 +0300779EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800780
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
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200793 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800794 * @chip: TPM chip to use.
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200795 * @startup_type: startup type. The value is either
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800796 * TPM_SU_CLEAR or TPM_SU_STATE.
797 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200798 * Return: Same as with tpm_transmit_cmd.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800799 */
Jason Gunthorpecae8b442016-07-12 11:41:49 -0600800static int tpm2_startup(struct tpm_chip *chip, u16 startup_type)
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800801{
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);
Stefan Bergerc659af72017-01-19 07:19:12 -0500807 return tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, 0,
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800808 "attempting to start the TPM");
809}
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800810
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
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200823 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800824 * @chip: TPM chip to use.
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200825 * @shutdown_type: shutdown type. The value is either
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800826 * TPM_SU_CLEAR or TPM_SU_STATE.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800827 */
Jarkko Sakkinen74d6b3c2015-01-29 07:43:47 +0200828void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800829{
830 struct tpm2_cmd cmd;
Jarkko Sakkinen74d6b3c2015-01-29 07:43:47 +0200831 int rc;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800832
833 cmd.header.in = tpm2_shutdown_header;
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800834 cmd.params.startup_in.startup_type = cpu_to_be16(shutdown_type);
Jarkko Sakkinen74d6b3c2015-01-29 07:43:47 +0200835
Stefan Bergerc659af72017-01-19 07:19:12 -0500836 rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, 0,
837 "stopping the TPM");
Jarkko Sakkinen74d6b3c2015-01-29 07:43:47 +0200838
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)
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500843 dev_warn(&chip->dev, "transmit returned %d while stopping the TPM",
Jarkko Sakkinen74d6b3c2015-01-29 07:43:47 +0200844 rc);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800845}
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800846
847/*
848 * tpm2_calc_ordinal_duration() - maximum duration for a command
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200849 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800850 * @chip: TPM chip to use.
851 * @ordinal: command code number.
852 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200853 * Return: maximum duration for a command
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800854 */
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)
Christophe Ricardaf782f32016-03-31 22:56:59 +0200864 duration = chip->duration[index];
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800865
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
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200885 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800886 * @chip: TPM chip to use
887 * @full: test all commands instead of testing only those that were not
888 * previously tested.
889 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200890 * Return: Same as with tpm_transmit_cmd with exception of RC_TESTING.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800891 */
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
Stefan Bergerc659af72017-01-19 07:19:12 -0500900 rc = tpm_transmit_cmd(chip, &cmd, TPM2_SELF_TEST_IN_SIZE, 0, 0,
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800901 "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) {
Jason Gunthorpe8cfffc92016-02-29 12:29:47 -0500907 dev_warn(&chip->dev, "Got RC_TESTING, ignoring\n");
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800908 rc = 0;
909 }
910
911 return rc;
912}
913
914/**
915 * tpm2_do_selftest() - run a full self test
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200916 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800917 * @chip: TPM chip to use
918 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200919 * Return: Same as with tpm_transmit_cmd.
920 *
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800921 * 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.
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800923 */
Jason Gunthorpecae8b442016-07-12 11:41:49 -0600924static int tpm2_do_selftest(struct tpm_chip *chip)
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800925{
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
Stefan Bergerc659af72017-01-19 07:19:12 -0500951 rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, 0, NULL);
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800952 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}
Jarkko Sakkinen7a1d7e62014-12-12 11:46:38 -0800964
965/**
Jarkko Sakkinen4d5f2052015-02-04 16:21:09 +0200966 * tpm2_probe() - probe TPM 2.0
967 * @chip: TPM chip to use
968 *
Winkler, Tomas794c6e12016-11-23 12:04:12 +0200969 * Return: < 0 error and 0 on success.
970 *
Jarkko Sakkinen4d5f2052015-02-04 16:21:09 +0200971 * 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
Stefan Bergerc659af72017-01-19 07:19:12 -0500984 rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), 0, 0, NULL);
Jarkko Sakkinen4d5f2052015-02-04 16:21:09 +0200985 if (rc < 0)
986 return rc;
Jarkko Sakkinen4d5f2052015-02-04 16:21:09 +0200987
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);
Jason Gunthorpecae8b442016-07-12 11:41:49 -0600994
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);
Jarkko Sakkinenb7d7b282016-09-02 02:36:58 +03001011 if (rc != 0 && rc != TPM2_RC_INITIALIZE) {
Jason Gunthorpecae8b442016-07-12 11:41:49 -06001012 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
Jason Gunthorpecae8b442016-07-12 11:41:49 -06001028out:
1029 if (rc > 0)
1030 rc = -ENODEV;
1031 return rc;
1032}