tpm2-cmd.c 25.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
J
Jarkko Sakkinen 已提交
2
/*
J
Jarkko Sakkinen 已提交
3
 * Copyright (C) 2014, 2015 Intel Corporation
J
Jarkko Sakkinen 已提交
4 5 6 7 8 9 10 11 12 13 14
 *
 * Authors:
 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
 *
 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
 *
 * This file contains TPM2 protocol implementations of the commands
 * used by the kernel internally.
 */

#include "tpm.h"
15
#include <crypto/hash_info.h>
J
Jarkko Sakkinen 已提交
16 17 18
#include <keys/trusted-type.h>

enum tpm2_object_attributes {
19 20 21 22 23
	TPM2_OA_USER_WITH_AUTH		= BIT(6),
};

enum tpm2_session_attributes {
	TPM2_SA_CONTINUE_SESSION	= BIT(0),
J
Jarkko Sakkinen 已提交
24
};
J
Jarkko Sakkinen 已提交
25

26 27 28 29 30 31
struct tpm2_hash {
	unsigned int crypto_id;
	unsigned int tpm_id;
};

static struct tpm2_hash tpm2_hash_map[] = {
32 33 34 35 36
	{HASH_ALGO_SHA1, TPM_ALG_SHA1},
	{HASH_ALGO_SHA256, TPM_ALG_SHA256},
	{HASH_ALGO_SHA384, TPM_ALG_SHA384},
	{HASH_ALGO_SHA512, TPM_ALG_SHA512},
	{HASH_ALGO_SM3_256, TPM_ALG_SM3_256},
37 38
};

T
Tomas Winkler 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
int tpm2_get_timeouts(struct tpm_chip *chip)
{
	/* Fixed timeouts for TPM2 */
	chip->timeout_a = msecs_to_jiffies(TPM2_TIMEOUT_A);
	chip->timeout_b = msecs_to_jiffies(TPM2_TIMEOUT_B);
	chip->timeout_c = msecs_to_jiffies(TPM2_TIMEOUT_C);
	chip->timeout_d = msecs_to_jiffies(TPM2_TIMEOUT_D);

	/* PTP spec timeouts */
	chip->duration[TPM_SHORT] = msecs_to_jiffies(TPM2_DURATION_SHORT);
	chip->duration[TPM_MEDIUM] = msecs_to_jiffies(TPM2_DURATION_MEDIUM);
	chip->duration[TPM_LONG] = msecs_to_jiffies(TPM2_DURATION_LONG);

	/* Key creation commands long timeouts */
	chip->duration[TPM_LONG_LONG] =
		msecs_to_jiffies(TPM2_DURATION_LONG_LONG);

	chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;

	return 0;
}

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
/**
 * tpm2_ordinal_duration_index() - returns an index to the chip duration table
 * @ordinal: TPM command ordinal.
 *
 * The function returns an index to the chip duration table
 * (enum tpm_duration), that describes the maximum amount of
 * time the chip could take to return the result for a  particular ordinal.
 *
 * The values of the MEDIUM, and LONG durations are taken
 * from the PC Client Profile (PTP) specification (750, 2000 msec)
 *
 * LONG_LONG is for commands that generates keys which empirically takes
 * a longer time on some systems.
 *
 * Return:
 * * TPM_MEDIUM
 * * TPM_LONG
 * * TPM_LONG_LONG
 * * TPM_UNDEFINED
J
Jarkko Sakkinen 已提交
80
 */
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
static u8 tpm2_ordinal_duration_index(u32 ordinal)
{
	switch (ordinal) {
	/* Startup */
	case TPM2_CC_STARTUP:                 /* 144 */
		return TPM_MEDIUM;

	case TPM2_CC_SELF_TEST:               /* 143 */
		return TPM_LONG;

	case TPM2_CC_GET_RANDOM:              /* 17B */
		return TPM_LONG;

	case TPM2_CC_SEQUENCE_UPDATE:         /* 15C */
		return TPM_MEDIUM;
	case TPM2_CC_SEQUENCE_COMPLETE:       /* 13E */
		return TPM_MEDIUM;
	case TPM2_CC_EVENT_SEQUENCE_COMPLETE: /* 185 */
		return TPM_MEDIUM;
	case TPM2_CC_HASH_SEQUENCE_START:     /* 186 */
		return TPM_MEDIUM;

	case TPM2_CC_VERIFY_SIGNATURE:        /* 177 */
		return TPM_LONG;

	case TPM2_CC_PCR_EXTEND:              /* 182 */
		return TPM_MEDIUM;

	case TPM2_CC_HIERARCHY_CONTROL:       /* 121 */
		return TPM_LONG;
	case TPM2_CC_HIERARCHY_CHANGE_AUTH:   /* 129 */
		return TPM_LONG;

	case TPM2_CC_GET_CAPABILITY:          /* 17A */
		return TPM_MEDIUM;

	case TPM2_CC_NV_READ:                 /* 14E */
		return TPM_LONG;

	case TPM2_CC_CREATE_PRIMARY:          /* 131 */
		return TPM_LONG_LONG;
	case TPM2_CC_CREATE:                  /* 153 */
		return TPM_LONG_LONG;
	case TPM2_CC_CREATE_LOADED:           /* 191 */
		return TPM_LONG_LONG;

	default:
		return TPM_UNDEFINED;
	}
}

/**
 * tpm2_calc_ordinal_duration() - calculate the maximum command duration
 * @chip:    TPM chip to use.
 * @ordinal: TPM command ordinal.
 *
 * The function returns the maximum amount of time the chip could take
 * to return the result for a particular ordinal in jiffies.
 *
 * Return: A maximal duration time for an ordinal in jiffies.
 */
unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
{
	unsigned int index;

	index = tpm2_ordinal_duration_index(ordinal);

	if (index != TPM_UNDEFINED)
		return chip->duration[index];
	else
		return msecs_to_jiffies(TPM2_DURATION_DEFAULT);
}

J
Jarkko Sakkinen 已提交
154

155 156 157 158 159 160 161 162 163 164 165
struct tpm2_pcr_read_out {
	__be32	update_cnt;
	__be32	pcr_selects_cnt;
	__be16	hash_alg;
	u8	pcr_select_size;
	u8	pcr_select[TPM2_PCR_SELECT_MIN];
	__be32	digests_cnt;
	__be16	digest_size;
	u8	digest[];
} __packed;

J
Jarkko Sakkinen 已提交
166 167 168 169
/**
 * tpm2_pcr_read() - read a PCR value
 * @chip:	TPM chip to use.
 * @pcr_idx:	index of the PCR to read.
170 171
 * @digest:	PCR bank and buffer current PCR value is written to.
 * @digest_size_ptr:	pointer to variable that stores the digest size.
J
Jarkko Sakkinen 已提交
172
 *
W
Winkler, Tomas 已提交
173
 * Return: Same as with tpm_transmit_cmd.
J
Jarkko Sakkinen 已提交
174
 */
175 176
int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
		  struct tpm_digest *digest, u16 *digest_size_ptr)
J
Jarkko Sakkinen 已提交
177
{
178
	int i;
J
Jarkko Sakkinen 已提交
179
	int rc;
180 181 182
	struct tpm_buf buf;
	struct tpm2_pcr_read_out *out;
	u8 pcr_select[TPM2_PCR_SELECT_MIN] = {0};
183 184
	u16 digest_size;
	u16 expected_digest_size = 0;
J
Jarkko Sakkinen 已提交
185 186 187 188

	if (pcr_idx >= TPM2_PLATFORM_PCR)
		return -EINVAL;

189 190 191 192 193 194 195 196 197 198 199
	if (!digest_size_ptr) {
		for (i = 0; i < chip->nr_allocated_banks &&
		     chip->allocated_banks[i].alg_id != digest->alg_id; i++)
			;

		if (i == chip->nr_allocated_banks)
			return -EINVAL;

		expected_digest_size = chip->allocated_banks[i].digest_size;
	}

200 201 202 203 204 205 206
	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_READ);
	if (rc)
		return rc;

	pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7);

	tpm_buf_append_u32(&buf, 1);
207
	tpm_buf_append_u16(&buf, digest->alg_id);
208 209 210 211
	tpm_buf_append_u8(&buf, TPM2_PCR_SELECT_MIN);
	tpm_buf_append(&buf, (const unsigned char *)pcr_select,
		       sizeof(pcr_select));

212 213 214 215 216 217 218 219 220 221
	rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to read a pcr value");
	if (rc)
		goto out;

	out = (struct tpm2_pcr_read_out *)&buf.data[TPM_HEADER_SIZE];
	digest_size = be16_to_cpu(out->digest_size);
	if (digest_size > sizeof(digest->digest) ||
	    (!digest_size_ptr && digest_size != expected_digest_size)) {
		rc = -EINVAL;
		goto out;
J
Jarkko Sakkinen 已提交
222 223
	}

224 225 226 227 228
	if (digest_size_ptr)
		*digest_size_ptr = digest_size;

	memcpy(digest->digest, out->digest, digest_size);
out:
229
	tpm_buf_destroy(&buf);
J
Jarkko Sakkinen 已提交
230 231 232
	return rc;
}

233 234 235 236 237 238
struct tpm2_null_auth_area {
	__be32  handle;
	__be16  nonce_size;
	u8  attributes;
	__be16  auth_size;
} __packed;
J
Jarkko Sakkinen 已提交
239 240 241

/**
 * tpm2_pcr_extend() - extend a PCR value
W
Winkler, Tomas 已提交
242
 *
J
Jarkko Sakkinen 已提交
243 244
 * @chip:	TPM chip to use.
 * @pcr_idx:	index of the PCR.
245
 * @digests:	list of pcr banks and corresponding digest values to extend.
J
Jarkko Sakkinen 已提交
246
 *
W
Winkler, Tomas 已提交
247
 * Return: Same as with tpm_transmit_cmd.
J
Jarkko Sakkinen 已提交
248
 */
249
int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
250
		    struct tpm_digest *digests)
J
Jarkko Sakkinen 已提交
251
{
252 253
	struct tpm_buf buf;
	struct tpm2_null_auth_area auth_area;
J
Jarkko Sakkinen 已提交
254
	int rc;
255
	int i;
J
Jarkko Sakkinen 已提交
256

257 258 259 260 261 262 263 264 265 266 267 268 269 270
	rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_EXTEND);
	if (rc)
		return rc;

	tpm_buf_append_u32(&buf, pcr_idx);

	auth_area.handle = cpu_to_be32(TPM2_RS_PW);
	auth_area.nonce_size = 0;
	auth_area.attributes = 0;
	auth_area.auth_size = 0;

	tpm_buf_append_u32(&buf, sizeof(struct tpm2_null_auth_area));
	tpm_buf_append(&buf, (const unsigned char *)&auth_area,
		       sizeof(auth_area));
271
	tpm_buf_append_u32(&buf, chip->nr_allocated_banks);
272

273
	for (i = 0; i < chip->nr_allocated_banks; i++) {
274 275 276
		tpm_buf_append_u16(&buf, digests[i].alg_id);
		tpm_buf_append(&buf, (const unsigned char *)&digests[i].digest,
			       chip->allocated_banks[i].digest_size);
277 278
	}

279
	rc = tpm_transmit_cmd(chip, &buf, 0, "attempting extend a PCR value");
J
Jarkko Sakkinen 已提交
280

281 282
	tpm_buf_destroy(&buf);

J
Jarkko Sakkinen 已提交
283 284 285
	return rc;
}

286 287 288 289
struct tpm2_get_random_out {
	__be16 size;
	u8 buffer[TPM_MAX_RNG_DATA];
} __packed;
J
Jarkko Sakkinen 已提交
290 291 292

/**
 * tpm2_get_random() - get random bytes from the TPM RNG
W
Winkler, Tomas 已提交
293
 *
294 295 296
 * @chip:	a &tpm_chip instance
 * @dest:	destination buffer
 * @max:	the max number of random bytes to pull
J
Jarkko Sakkinen 已提交
297
 *
W
Winkler, Tomas 已提交
298
 * Return:
299
 *   size of the buffer on success,
300
 *   -errno otherwise (positive TPM return codes are masked to -EIO)
J
Jarkko Sakkinen 已提交
301
 */
302
int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
J
Jarkko Sakkinen 已提交
303
{
304 305 306 307
	struct tpm2_get_random_out *out;
	struct tpm_buf buf;
	u32 recd;
	u32 num_bytes = max;
J
Jarkko Sakkinen 已提交
308 309 310
	int err;
	int total = 0;
	int retries = 5;
311
	u8 *dest_ptr = dest;
J
Jarkko Sakkinen 已提交
312

313
	if (!num_bytes || max > TPM_MAX_RNG_DATA)
J
Jarkko Sakkinen 已提交
314 315
		return -EINVAL;

316 317 318
	err = tpm_buf_init(&buf, 0, 0);
	if (err)
		return err;
J
Jarkko Sakkinen 已提交
319

320 321 322
	do {
		tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM);
		tpm_buf_append_u16(&buf, num_bytes);
323
		err = tpm_transmit_cmd(chip, &buf,
324 325
				       offsetof(struct tpm2_get_random_out,
						buffer),
326
				       "attempting get random");
327 328 329
		if (err) {
			if (err > 0)
				err = -EIO;
330
			goto out;
331
		}
J
Jarkko Sakkinen 已提交
332

333 334 335 336
		out = (struct tpm2_get_random_out *)
			&buf.data[TPM_HEADER_SIZE];
		recd = min_t(u32, be16_to_cpu(out->size), num_bytes);
		if (tpm_buf_length(&buf) <
337 338 339
		    TPM_HEADER_SIZE +
		    offsetof(struct tpm2_get_random_out, buffer) +
		    recd) {
340 341 342 343
			err = -EFAULT;
			goto out;
		}
		memcpy(dest_ptr, out->buffer, recd);
J
Jarkko Sakkinen 已提交
344

345
		dest_ptr += recd;
J
Jarkko Sakkinen 已提交
346 347 348 349
		total += recd;
		num_bytes -= recd;
	} while (retries-- && total < max);

350
	tpm_buf_destroy(&buf);
J
Jarkko Sakkinen 已提交
351
	return total ? total : -EIO;
352 353 354
out:
	tpm_buf_destroy(&buf);
	return err;
J
Jarkko Sakkinen 已提交
355 356
}

357
/**
358
 * tpm2_flush_context() - execute a TPM2_FlushContext command
359 360
 * @chip:	TPM chip to use
 * @handle:	context handle
361
 */
362
void tpm2_flush_context(struct tpm_chip *chip, u32 handle)
363 364 365 366 367 368 369 370 371 372 373 374 375
{
	struct tpm_buf buf;
	int rc;

	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT);
	if (rc) {
		dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
			 handle);
		return;
	}

	tpm_buf_append_u32(&buf, handle);

376
	tpm_transmit_cmd(chip, &buf, 0, "flushing context");
377 378 379
	tpm_buf_destroy(&buf);
}

J
Jarkko Sakkinen 已提交
380
/**
W
Winkler, Tomas 已提交
381 382 383 384 385 386 387 388 389
 * tpm_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
 *
 * @buf: an allocated tpm_buf instance
 * @session_handle: session handle
 * @nonce: the session nonce, may be NULL if not used
 * @nonce_len: the session nonce length, may be 0 if not used
 * @attributes: the session attributes
 * @hmac: the session HMAC or password, may be NULL if not used
 * @hmac_len: the session HMAC or password length, maybe 0 if not used
J
Jarkko Sakkinen 已提交
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
 */
static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,
				 const u8 *nonce, u16 nonce_len,
				 u8 attributes,
				 const u8 *hmac, u16 hmac_len)
{
	tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);
	tpm_buf_append_u32(buf, session_handle);
	tpm_buf_append_u16(buf, nonce_len);

	if (nonce && nonce_len)
		tpm_buf_append(buf, nonce, nonce_len);

	tpm_buf_append_u8(buf, attributes);
	tpm_buf_append_u16(buf, hmac_len);

	if (hmac && hmac_len)
		tpm_buf_append(buf, hmac, hmac_len);
}

/**
411
 * tpm2_seal_trusted() - seal the payload of a trusted key
W
Winkler, Tomas 已提交
412 413
 *
 * @chip: TPM chip to use
J
Jarkko Sakkinen 已提交
414
 * @payload: the key data in clear and encrypted form
415
 * @options: authentication values and other options
J
Jarkko Sakkinen 已提交
416
 *
417
 * Return: < 0 on error and 0 on success.
J
Jarkko Sakkinen 已提交
418 419 420 421 422 423 424
 */
int tpm2_seal_trusted(struct tpm_chip *chip,
		      struct trusted_key_payload *payload,
		      struct trusted_key_options *options)
{
	unsigned int blob_len;
	struct tpm_buf buf;
425
	u32 hash;
426
	int i;
J
Jarkko Sakkinen 已提交
427 428
	int rc;

429 430 431 432 433 434 435 436 437 438
	for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
		if (options->hash == tpm2_hash_map[i].crypto_id) {
			hash = tpm2_hash_map[i].tpm_id;
			break;
		}
	}

	if (i == ARRAY_SIZE(tpm2_hash_map))
		return -EINVAL;

J
Jarkko Sakkinen 已提交
439 440 441 442 443 444 445 446 447 448 449 450
	rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
	if (rc)
		return rc;

	tpm_buf_append_u32(&buf, options->keyhandle);
	tpm2_buf_append_auth(&buf, TPM2_RS_PW,
			     NULL /* nonce */, 0,
			     0 /* session_attributes */,
			     options->keyauth /* hmac */,
			     TPM_DIGEST_SIZE);

	/* sensitive */
451
	tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1);
J
Jarkko Sakkinen 已提交
452 453 454

	tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE);
	tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE);
455
	tpm_buf_append_u16(&buf, payload->key_len + 1);
J
Jarkko Sakkinen 已提交
456
	tpm_buf_append(&buf, payload->key, payload->key_len);
457
	tpm_buf_append_u8(&buf, payload->migratable);
J
Jarkko Sakkinen 已提交
458 459

	/* public */
460
	tpm_buf_append_u16(&buf, 14 + options->policydigest_len);
461
	tpm_buf_append_u16(&buf, TPM_ALG_KEYEDHASH);
462
	tpm_buf_append_u16(&buf, hash);
463 464

	/* policy */
465
	if (options->policydigest_len) {
466
		tpm_buf_append_u32(&buf, 0);
467
		tpm_buf_append_u16(&buf, options->policydigest_len);
468
		tpm_buf_append(&buf, options->policydigest,
469
			       options->policydigest_len);
470
	} else {
471
		tpm_buf_append_u32(&buf, TPM2_OA_USER_WITH_AUTH);
472 473 474 475
		tpm_buf_append_u16(&buf, 0);
	}

	/* public parameters */
476
	tpm_buf_append_u16(&buf, TPM_ALG_NULL);
J
Jarkko Sakkinen 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489
	tpm_buf_append_u16(&buf, 0);

	/* outside info */
	tpm_buf_append_u16(&buf, 0);

	/* creation PCR */
	tpm_buf_append_u32(&buf, 0);

	if (buf.flags & TPM_BUF_OVERFLOW) {
		rc = -E2BIG;
		goto out;
	}

490
	rc = tpm_transmit_cmd(chip, &buf, 4, "sealing data");
J
Jarkko Sakkinen 已提交
491 492 493 494 495 496 497 498
	if (rc)
		goto out;

	blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]);
	if (blob_len > MAX_BLOB_SIZE) {
		rc = -E2BIG;
		goto out;
	}
499
	if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 4 + blob_len) {
500 501 502
		rc = -EFAULT;
		goto out;
	}
J
Jarkko Sakkinen 已提交
503 504 505 506 507 508 509

	memcpy(payload->blob, &buf.data[TPM_HEADER_SIZE + 4], blob_len);
	payload->blob_len = blob_len;

out:
	tpm_buf_destroy(&buf);

510
	if (rc > 0) {
511
		if (tpm2_rc_value(rc) == TPM2_RC_HASH)
512 513 514 515
			rc = -EINVAL;
		else
			rc = -EPERM;
	}
J
Jarkko Sakkinen 已提交
516 517 518 519

	return rc;
}

520 521
/**
 * tpm2_load_cmd() - execute a TPM2_Load command
W
Winkler, Tomas 已提交
522 523
 *
 * @chip: TPM chip to use
524 525
 * @payload: the key data in clear and encrypted form
 * @options: authentication values and other options
W
Winkler, Tomas 已提交
526
 * @blob_handle: returned blob handle
527
 *
W
Winkler, Tomas 已提交
528 529 530 531
 * Return: 0 on success.
 *        -E2BIG on wrong payload size.
 *        -EPERM on tpm error status.
 *        < 0 error from tpm_transmit_cmd.
532 533 534 535
 */
static int tpm2_load_cmd(struct tpm_chip *chip,
			 struct trusted_key_payload *payload,
			 struct trusted_key_options *options,
536
			 u32 *blob_handle)
J
Jarkko Sakkinen 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
{
	struct tpm_buf buf;
	unsigned int private_len;
	unsigned int public_len;
	unsigned int blob_len;
	int rc;

	private_len = be16_to_cpup((__be16 *) &payload->blob[0]);
	if (private_len > (payload->blob_len - 2))
		return -E2BIG;

	public_len = be16_to_cpup((__be16 *) &payload->blob[2 + private_len]);
	blob_len = private_len + public_len + 4;
	if (blob_len > payload->blob_len)
		return -E2BIG;

	rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
	if (rc)
		return rc;

	tpm_buf_append_u32(&buf, options->keyhandle);
	tpm2_buf_append_auth(&buf, TPM2_RS_PW,
			     NULL /* nonce */, 0,
			     0 /* session_attributes */,
			     options->keyauth /* hmac */,
			     TPM_DIGEST_SIZE);

	tpm_buf_append(&buf, payload->blob, blob_len);

	if (buf.flags & TPM_BUF_OVERFLOW) {
		rc = -E2BIG;
		goto out;
	}

571
	rc = tpm_transmit_cmd(chip, &buf, 4, "loading blob");
J
Jarkko Sakkinen 已提交
572 573 574 575 576 577 578 579 580 581 582 583 584
	if (!rc)
		*blob_handle = be32_to_cpup(
			(__be32 *) &buf.data[TPM_HEADER_SIZE]);

out:
	tpm_buf_destroy(&buf);

	if (rc > 0)
		rc = -EPERM;

	return rc;
}

585 586
/**
 * tpm2_unseal_cmd() - execute a TPM2_Unload command
W
Winkler, Tomas 已提交
587 588
 *
 * @chip: TPM chip to use
589 590
 * @payload: the key data in clear and encrypted form
 * @options: authentication values and other options
W
Winkler, Tomas 已提交
591
 * @blob_handle: blob handle
592
 *
W
Winkler, Tomas 已提交
593 594 595
 * Return: 0 on success
 *         -EPERM on tpm error status
 *         < 0 error from tpm_transmit_cmd
596 597 598 599
 */
static int tpm2_unseal_cmd(struct tpm_chip *chip,
			   struct trusted_key_payload *payload,
			   struct trusted_key_options *options,
600
			   u32 blob_handle)
J
Jarkko Sakkinen 已提交
601 602
{
	struct tpm_buf buf;
603 604
	u16 data_len;
	u8 *data;
J
Jarkko Sakkinen 已提交
605 606 607 608 609 610 611
	int rc;

	rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
	if (rc)
		return rc;

	tpm_buf_append_u32(&buf, blob_handle);
612 613 614
	tpm2_buf_append_auth(&buf,
			     options->policyhandle ?
			     options->policyhandle : TPM2_RS_PW,
J
Jarkko Sakkinen 已提交
615
			     NULL /* nonce */, 0,
616
			     TPM2_SA_CONTINUE_SESSION,
J
Jarkko Sakkinen 已提交
617 618 619
			     options->blobauth /* hmac */,
			     TPM_DIGEST_SIZE);

620
	rc = tpm_transmit_cmd(chip, &buf, 6, "unsealing");
J
Jarkko Sakkinen 已提交
621 622 623 624
	if (rc > 0)
		rc = -EPERM;

	if (!rc) {
625
		data_len = be16_to_cpup(
J
Jarkko Sakkinen 已提交
626
			(__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
627 628 629 630
		if (data_len < MIN_KEY_SIZE ||  data_len > MAX_KEY_SIZE + 1) {
			rc = -EFAULT;
			goto out;
		}
631

632
		if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 6 + data_len) {
633 634 635
			rc = -EFAULT;
			goto out;
		}
636
		data = &buf.data[TPM_HEADER_SIZE + 6];
J
Jarkko Sakkinen 已提交
637

638 639 640
		memcpy(payload->key, data, data_len - 1);
		payload->key_len = data_len - 1;
		payload->migratable = data[data_len - 1];
J
Jarkko Sakkinen 已提交
641 642
	}

643
out:
J
Jarkko Sakkinen 已提交
644 645 646 647 648
	tpm_buf_destroy(&buf);
	return rc;
}

/**
649
 * tpm2_unseal_trusted() - unseal the payload of a trusted key
W
Winkler, Tomas 已提交
650 651
 *
 * @chip: TPM chip to use
J
Jarkko Sakkinen 已提交
652
 * @payload: the key data in clear and encrypted form
653
 * @options: authentication values and other options
J
Jarkko Sakkinen 已提交
654
 *
W
Winkler, Tomas 已提交
655
 * Return: Same as with tpm_transmit_cmd.
J
Jarkko Sakkinen 已提交
656 657 658 659 660 661 662 663
 */
int tpm2_unseal_trusted(struct tpm_chip *chip,
			struct trusted_key_payload *payload,
			struct trusted_key_options *options)
{
	u32 blob_handle;
	int rc;

664
	rc = tpm2_load_cmd(chip, payload, options, &blob_handle);
J
Jarkko Sakkinen 已提交
665
	if (rc)
666
		return rc;
J
Jarkko Sakkinen 已提交
667

668 669
	rc = tpm2_unseal_cmd(chip, payload, options, blob_handle);
	tpm2_flush_context(chip, blob_handle);
J
Jarkko Sakkinen 已提交
670 671 672
	return rc;
}

673 674 675 676 677 678 679 680
struct tpm2_get_cap_out {
	u8 more_data;
	__be32 subcap_id;
	__be32 property_cnt;
	__be32 property_id;
	__be32 value;
} __packed;

J
Jarkko Sakkinen 已提交
681 682
/**
 * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
683
 * @chip:		a &tpm_chip instance
J
Jarkko Sakkinen 已提交
684 685 686 687
 * @property_id:	property ID.
 * @value:		output variable.
 * @desc:		passed to tpm_transmit_cmd()
 *
688 689 690
 * Return:
 *   0 on success,
 *   -errno or a TPM return code otherwise
J
Jarkko Sakkinen 已提交
691 692 693 694
 */
ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,  u32 *value,
			const char *desc)
{
695 696
	struct tpm2_get_cap_out *out;
	struct tpm_buf buf;
J
Jarkko Sakkinen 已提交
697 698
	int rc;

699 700 701 702 703 704
	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
	if (rc)
		return rc;
	tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
	tpm_buf_append_u32(&buf, property_id);
	tpm_buf_append_u32(&buf, 1);
705
	rc = tpm_transmit_cmd(chip, &buf, 0, NULL);
706 707 708 709 710 711
	if (!rc) {
		out = (struct tpm2_get_cap_out *)
			&buf.data[TPM_HEADER_SIZE];
		*value = be32_to_cpu(out->value);
	}
	tpm_buf_destroy(&buf);
J
Jarkko Sakkinen 已提交
712 713
	return rc;
}
714
EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
J
Jarkko Sakkinen 已提交
715 716

/**
717
 * tpm2_shutdown() - send a TPM shutdown command
W
Winkler, Tomas 已提交
718
 *
719 720 721 722 723 724
 * Sends a TPM shutdown command. The shutdown command is used in call
 * sites where the system is going down. If it fails, there is not much
 * that can be done except print an error message.
 *
 * @chip:		a &tpm_chip instance
 * @shutdown_type:	TPM_SU_CLEAR or TPM_SU_STATE.
J
Jarkko Sakkinen 已提交
725
 */
726
void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
J
Jarkko Sakkinen 已提交
727
{
728
	struct tpm_buf buf;
729
	int rc;
J
Jarkko Sakkinen 已提交
730

731 732 733 734
	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SHUTDOWN);
	if (rc)
		return;
	tpm_buf_append_u16(&buf, shutdown_type);
735
	tpm_transmit_cmd(chip, &buf, 0, "stopping the TPM");
736
	tpm_buf_destroy(&buf);
J
Jarkko Sakkinen 已提交
737 738 739
}

/**
740
 * tpm2_do_selftest() - ensure that all self tests have passed
W
Winkler, Tomas 已提交
741
 *
J
Jarkko Sakkinen 已提交
742 743
 * @chip: TPM chip to use
 *
W
Winkler, Tomas 已提交
744 745
 * Return: Same as with tpm_transmit_cmd.
 *
746 747 748 749 750
 * The TPM can either run all self tests synchronously and then return
 * RC_SUCCESS once all tests were successful. Or it can choose to run the tests
 * asynchronously and return RC_TESTING immediately while the self tests still
 * execute in the background. This function handles both cases and waits until
 * all tests have completed.
J
Jarkko Sakkinen 已提交
751
 */
752
static int tpm2_do_selftest(struct tpm_chip *chip)
J
Jarkko Sakkinen 已提交
753
{
754 755
	struct tpm_buf buf;
	int full;
J
Jarkko Sakkinen 已提交
756 757
	int rc;

758 759 760 761
	for (full = 0; full < 2; full++) {
		rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SELF_TEST);
		if (rc)
			return rc;
J
Jarkko Sakkinen 已提交
762

763
		tpm_buf_append_u8(&buf, full);
764
		rc = tpm_transmit_cmd(chip, &buf, 0,
765 766
				      "attempting the self test");
		tpm_buf_destroy(&buf);
J
Jarkko Sakkinen 已提交
767

768 769 770 771
		if (rc == TPM2_RC_TESTING)
			rc = TPM2_RC_SUCCESS;
		if (rc == TPM2_RC_INITIALIZE || rc == TPM2_RC_SUCCESS)
			return rc;
J
Jarkko Sakkinen 已提交
772 773 774 775 776
	}

	return rc;
}

777
/**
778 779
 * tpm2_probe() - probe for the TPM 2.0 protocol
 * @chip:	a &tpm_chip instance
780
 *
781 782 783
 * Send an idempotent TPM 2.0 command and see whether there is TPM2 chip in the
 * other end based on the response tag. The flag TPM_CHIP_FLAG_TPM2 is set by
 * this function if this is the case.
W
Winkler, Tomas 已提交
784
 *
785 786 787
 * Return:
 *   0 on success,
 *   -errno otherwise
788 789 790
 */
int tpm2_probe(struct tpm_chip *chip)
{
J
Jarkko Sakkinen 已提交
791
	struct tpm_header *out;
792
	struct tpm_buf buf;
793 794
	int rc;

795 796
	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
	if (rc)
797
		return rc;
798 799 800
	tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
	tpm_buf_append_u32(&buf, TPM_PT_TOTAL_COMMANDS);
	tpm_buf_append_u32(&buf, 1);
801
	rc = tpm_transmit_cmd(chip, &buf, 0, NULL);
802 803
	/* We ignore TPM return codes on purpose. */
	if (rc >=  0) {
J
Jarkko Sakkinen 已提交
804
		out = (struct tpm_header *)buf.data;
805 806 807 808
		if (be16_to_cpu(out->tag) == TPM2_ST_NO_SESSIONS)
			chip->flags |= TPM_CHIP_FLAG_TPM2;
	}
	tpm_buf_destroy(&buf);
809 810 811
	return 0;
}
EXPORT_SYMBOL_GPL(tpm2_probe);
812

813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
static int tpm2_init_bank_info(struct tpm_chip *chip, u32 bank_index)
{
	struct tpm_bank_info *bank = chip->allocated_banks + bank_index;
	struct tpm_digest digest = { .alg_id = bank->alg_id };
	int i;

	/*
	 * Avoid unnecessary PCR read operations to reduce overhead
	 * and obtain identifiers of the crypto subsystem.
	 */
	for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
		enum hash_algo crypto_algo = tpm2_hash_map[i].crypto_id;

		if (bank->alg_id != tpm2_hash_map[i].tpm_id)
			continue;

		bank->digest_size = hash_digest_size[crypto_algo];
		bank->crypto_id = crypto_algo;
		return 0;
	}

	return tpm2_pcr_read(chip, 0, &digest, &bank->digest_size);
}

837 838 839 840 841 842
struct tpm2_pcr_selection {
	__be16  hash_alg;
	u8  size_of_select;
	u8  pcr_select[3];
} __packed;

843
ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
844 845 846 847 848 849 850
{
	struct tpm2_pcr_selection pcr_selection;
	struct tpm_buf buf;
	void *marker;
	void *end;
	void *pcr_select_offset;
	u32 sizeof_pcr_selection;
851 852 853
	u32 nr_possible_banks;
	u32 nr_alloc_banks = 0;
	u16 hash_alg;
854 855 856 857 858 859 860 861 862 863 864 865
	u32 rsp_len;
	int rc;
	int i = 0;

	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
	if (rc)
		return rc;

	tpm_buf_append_u32(&buf, TPM2_CAP_PCRS);
	tpm_buf_append_u32(&buf, 0);
	tpm_buf_append_u32(&buf, 1);

866
	rc = tpm_transmit_cmd(chip, &buf, 9, "get tpm pcr allocation");
867 868 869
	if (rc)
		goto out;

870
	nr_possible_banks = be32_to_cpup(
871 872
		(__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);

873 874 875 876 877
	chip->allocated_banks = kcalloc(nr_possible_banks,
					sizeof(*chip->allocated_banks),
					GFP_KERNEL);
	if (!chip->allocated_banks) {
		rc = -ENOMEM;
878 879 880 881 882 883 884 885
		goto out;
	}

	marker = &buf.data[TPM_HEADER_SIZE + 9];

	rsp_len = be32_to_cpup((__be32 *)&buf.data[2]);
	end = &buf.data[rsp_len];

886
	for (i = 0; i < nr_possible_banks; i++) {
887 888 889 890 891 892 893 894
		pcr_select_offset = marker +
			offsetof(struct tpm2_pcr_selection, size_of_select);
		if (pcr_select_offset >= end) {
			rc = -EFAULT;
			break;
		}

		memcpy(&pcr_selection, marker, sizeof(pcr_selection));
895 896 897 898 899
		hash_alg = be16_to_cpu(pcr_selection.hash_alg);

		pcr_select_offset = memchr_inv(pcr_selection.pcr_select, 0,
					       pcr_selection.size_of_select);
		if (pcr_select_offset) {
900 901 902 903 904 905
			chip->allocated_banks[nr_alloc_banks].alg_id = hash_alg;

			rc = tpm2_init_bank_info(chip, nr_alloc_banks);
			if (rc < 0)
				break;

906 907 908
			nr_alloc_banks++;
		}

909 910 911 912 913 914
		sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
			sizeof(pcr_selection.size_of_select) +
			pcr_selection.size_of_select;
		marker = marker + sizeof_pcr_selection;
	}

915
	chip->nr_allocated_banks = nr_alloc_banks;
916 917 918 919 920
out:
	tpm_buf_destroy(&buf);

	return rc;
}
921

J
Jarkko Sakkinen 已提交
922 923 924 925
static int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip)
{
	struct tpm_buf buf;
	u32 nr_commands;
926
	__be32 *attrs;
J
Jarkko Sakkinen 已提交
927 928 929 930 931 932 933 934 935 936 937 938 939
	u32 cc;
	int i;
	int rc;

	rc = tpm2_get_tpm_pt(chip, TPM_PT_TOTAL_COMMANDS, &nr_commands, NULL);
	if (rc)
		goto out;

	if (nr_commands > 0xFFFFF) {
		rc = -EFAULT;
		goto out;
	}

940
	chip->cc_attrs_tbl = devm_kcalloc(&chip->dev, 4, nr_commands,
J
Jarkko Sakkinen 已提交
941 942 943 944 945 946 947 948 949 950
					  GFP_KERNEL);

	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
	if (rc)
		goto out;

	tpm_buf_append_u32(&buf, TPM2_CAP_COMMANDS);
	tpm_buf_append_u32(&buf, TPM2_CC_FIRST);
	tpm_buf_append_u32(&buf, nr_commands);

951
	rc = tpm_transmit_cmd(chip, &buf, 9 + 4 * nr_commands, NULL);
J
Jarkko Sakkinen 已提交
952 953 954 955 956 957 958 959 960 961 962 963 964
	if (rc) {
		tpm_buf_destroy(&buf);
		goto out;
	}

	if (nr_commands !=
	    be32_to_cpup((__be32 *)&buf.data[TPM_HEADER_SIZE + 5])) {
		tpm_buf_destroy(&buf);
		goto out;
	}

	chip->nr_commands = nr_commands;

965
	attrs = (__be32 *)&buf.data[TPM_HEADER_SIZE + 9];
J
Jarkko Sakkinen 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
	for (i = 0; i < nr_commands; i++, attrs++) {
		chip->cc_attrs_tbl[i] = be32_to_cpup(attrs);
		cc = chip->cc_attrs_tbl[i] & 0xFFFF;

		if (cc == TPM2_CC_CONTEXT_SAVE || cc == TPM2_CC_FLUSH_CONTEXT) {
			chip->cc_attrs_tbl[i] &=
				~(GENMASK(2, 0) << TPM2_CC_ATTR_CHANDLES);
			chip->cc_attrs_tbl[i] |= 1 << TPM2_CC_ATTR_CHANDLES;
		}
	}

	tpm_buf_destroy(&buf);

out:
	if (rc > 0)
		rc = -ENODEV;
	return rc;
}

985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
/**
 * tpm2_startup - turn on the TPM
 * @chip: TPM chip to use
 *
 * Normally the firmware should start the TPM. This function is provided as a
 * workaround if this does not happen. A legal case for this could be for
 * example when a TPM emulator is used.
 *
 * Return: same as tpm_transmit_cmd()
 */

static int tpm2_startup(struct tpm_chip *chip)
{
	struct tpm_buf buf;
	int rc;

	dev_info(&chip->dev, "starting up the TPM manually\n");

	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_STARTUP);
	if (rc < 0)
		return rc;

	tpm_buf_append_u16(&buf, TPM2_SU_CLEAR);
1008
	rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to start the TPM");
1009 1010 1011 1012 1013
	tpm_buf_destroy(&buf);

	return rc;
}

1014 1015 1016 1017 1018
/**
 * tpm2_auto_startup - Perform the standard automatic TPM initialization
 *                     sequence
 * @chip: TPM chip to use
 *
J
Jarkko Sakkinen 已提交
1019
 * Returns 0 on success, < 0 in case of fatal error.
1020 1021 1022 1023 1024
 */
int tpm2_auto_startup(struct tpm_chip *chip)
{
	int rc;

1025
	rc = tpm2_get_timeouts(chip);
1026 1027 1028 1029
	if (rc)
		goto out;

	rc = tpm2_do_selftest(chip);
1030
	if (rc && rc != TPM2_RC_INITIALIZE)
1031 1032 1033
		goto out;

	if (rc == TPM2_RC_INITIALIZE) {
1034
		rc = tpm2_startup(chip);
1035 1036 1037 1038
		if (rc)
			goto out;

		rc = tpm2_do_selftest(chip);
1039
		if (rc)
1040 1041 1042
			goto out;
	}

J
Jarkko Sakkinen 已提交
1043
	rc = tpm2_get_cc_attrs_tbl(chip);
1044 1045 1046 1047 1048 1049

out:
	if (rc > 0)
		rc = -ENODEV;
	return rc;
}
J
Jarkko Sakkinen 已提交
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

int tpm2_find_cc(struct tpm_chip *chip, u32 cc)
{
	int i;

	for (i = 0; i < chip->nr_commands; i++)
		if (cc == (chip->cc_attrs_tbl[i] & GENMASK(15, 0)))
			return i;

	return -1;
}