tpm2-cmd.c 25.7 KB
Newer Older
J
Jarkko Sakkinen 已提交
1
/*
J
Jarkko Sakkinen 已提交
2
 * Copyright (C) 2014, 2015 Intel Corporation
J
Jarkko Sakkinen 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the License.
 */

#include "tpm.h"
19
#include <crypto/hash_info.h>
J
Jarkko Sakkinen 已提交
20 21 22
#include <keys/trusted-type.h>

enum tpm2_object_attributes {
23 24 25 26 27
	TPM2_OA_USER_WITH_AUTH		= BIT(6),
};

enum tpm2_session_attributes {
	TPM2_SA_CONTINUE_SESSION	= BIT(0),
J
Jarkko Sakkinen 已提交
28
};
J
Jarkko Sakkinen 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

struct tpm2_get_tpm_pt_in {
	__be32	cap_id;
	__be32	property_id;
	__be32	property_cnt;
} __packed;

struct tpm2_get_tpm_pt_out {
	u8	more_data;
	__be32	subcap_id;
	__be32	property_cnt;
	__be32	property_id;
	__be32	value;
} __packed;

struct tpm2_get_random_in {
	__be16	size;
} __packed;

struct tpm2_get_random_out {
	__be16	size;
	u8	buffer[TPM_MAX_RNG_DATA];
} __packed;

union tpm2_cmd_params {
	struct	tpm2_get_tpm_pt_in	get_tpm_pt_in;
	struct	tpm2_get_tpm_pt_out	get_tpm_pt_out;
	struct	tpm2_get_random_in	getrandom_in;
	struct	tpm2_get_random_out	getrandom_out;
};

struct tpm2_cmd {
	tpm_cmd_header		header;
	union tpm2_cmd_params	params;
} __packed;

65 66 67 68 69 70 71 72 73 74 75 76 77
struct tpm2_hash {
	unsigned int crypto_id;
	unsigned int tpm_id;
};

static struct tpm2_hash tpm2_hash_map[] = {
	{HASH_ALGO_SHA1, TPM2_ALG_SHA1},
	{HASH_ALGO_SHA256, TPM2_ALG_SHA256},
	{HASH_ALGO_SHA384, TPM2_ALG_SHA384},
	{HASH_ALGO_SHA512, TPM2_ALG_SHA512},
	{HASH_ALGO_SM3_256, TPM2_ALG_SM3_256},
};

J
Jarkko Sakkinen 已提交
78 79 80 81 82
/*
 * Array with one entry per ordinal defining the maximum amount
 * of time the chip could take to return the result. The values
 * of the SHORT, MEDIUM, and LONG durations are taken from the
 * PC Client Profile (PTP) specification.
83 84
 * LONG_LONG is for commands that generates keys which empirically
 * takes longer time on some systems.
J
Jarkko Sakkinen 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
 */
static const u8 tpm2_ordinal_duration[TPM2_CC_LAST - TPM2_CC_FIRST + 1] = {
	TPM_UNDEFINED,		/* 11F */
	TPM_UNDEFINED,		/* 120 */
	TPM_LONG,		/* 121 */
	TPM_UNDEFINED,		/* 122 */
	TPM_UNDEFINED,		/* 123 */
	TPM_UNDEFINED,		/* 124 */
	TPM_UNDEFINED,		/* 125 */
	TPM_UNDEFINED,		/* 126 */
	TPM_UNDEFINED,		/* 127 */
	TPM_UNDEFINED,		/* 128 */
	TPM_LONG,		/* 129 */
	TPM_UNDEFINED,		/* 12a */
	TPM_UNDEFINED,		/* 12b */
	TPM_UNDEFINED,		/* 12c */
	TPM_UNDEFINED,		/* 12d */
	TPM_UNDEFINED,		/* 12e */
	TPM_UNDEFINED,		/* 12f */
	TPM_UNDEFINED,		/* 130 */
105
	TPM_LONG_LONG,		/* 131 */
J
Jarkko Sakkinen 已提交
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
	TPM_UNDEFINED,		/* 132 */
	TPM_UNDEFINED,		/* 133 */
	TPM_UNDEFINED,		/* 134 */
	TPM_UNDEFINED,		/* 135 */
	TPM_UNDEFINED,		/* 136 */
	TPM_UNDEFINED,		/* 137 */
	TPM_UNDEFINED,		/* 138 */
	TPM_UNDEFINED,		/* 139 */
	TPM_UNDEFINED,		/* 13a */
	TPM_UNDEFINED,		/* 13b */
	TPM_UNDEFINED,		/* 13c */
	TPM_UNDEFINED,		/* 13d */
	TPM_MEDIUM,		/* 13e */
	TPM_UNDEFINED,		/* 13f */
	TPM_UNDEFINED,		/* 140 */
	TPM_UNDEFINED,		/* 141 */
	TPM_UNDEFINED,		/* 142 */
	TPM_LONG,		/* 143 */
	TPM_MEDIUM,		/* 144 */
	TPM_UNDEFINED,		/* 145 */
	TPM_UNDEFINED,		/* 146 */
	TPM_UNDEFINED,		/* 147 */
	TPM_UNDEFINED,		/* 148 */
	TPM_UNDEFINED,		/* 149 */
	TPM_UNDEFINED,		/* 14a */
	TPM_UNDEFINED,		/* 14b */
	TPM_UNDEFINED,		/* 14c */
	TPM_UNDEFINED,		/* 14d */
	TPM_LONG,		/* 14e */
	TPM_UNDEFINED,		/* 14f */
	TPM_UNDEFINED,		/* 150 */
	TPM_UNDEFINED,		/* 151 */
	TPM_UNDEFINED,		/* 152 */
139
	TPM_LONG_LONG,		/* 153 */
J
Jarkko Sakkinen 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	TPM_UNDEFINED,		/* 154 */
	TPM_UNDEFINED,		/* 155 */
	TPM_UNDEFINED,		/* 156 */
	TPM_UNDEFINED,		/* 157 */
	TPM_UNDEFINED,		/* 158 */
	TPM_UNDEFINED,		/* 159 */
	TPM_UNDEFINED,		/* 15a */
	TPM_UNDEFINED,		/* 15b */
	TPM_MEDIUM,		/* 15c */
	TPM_UNDEFINED,		/* 15d */
	TPM_UNDEFINED,		/* 15e */
	TPM_UNDEFINED,		/* 15f */
	TPM_UNDEFINED,		/* 160 */
	TPM_UNDEFINED,		/* 161 */
	TPM_UNDEFINED,		/* 162 */
	TPM_UNDEFINED,		/* 163 */
	TPM_UNDEFINED,		/* 164 */
	TPM_UNDEFINED,		/* 165 */
	TPM_UNDEFINED,		/* 166 */
	TPM_UNDEFINED,		/* 167 */
	TPM_UNDEFINED,		/* 168 */
	TPM_UNDEFINED,		/* 169 */
	TPM_UNDEFINED,		/* 16a */
	TPM_UNDEFINED,		/* 16b */
	TPM_UNDEFINED,		/* 16c */
	TPM_UNDEFINED,		/* 16d */
	TPM_UNDEFINED,		/* 16e */
	TPM_UNDEFINED,		/* 16f */
	TPM_UNDEFINED,		/* 170 */
	TPM_UNDEFINED,		/* 171 */
	TPM_UNDEFINED,		/* 172 */
	TPM_UNDEFINED,		/* 173 */
	TPM_UNDEFINED,		/* 174 */
	TPM_UNDEFINED,		/* 175 */
	TPM_UNDEFINED,		/* 176 */
	TPM_LONG,		/* 177 */
	TPM_UNDEFINED,		/* 178 */
	TPM_UNDEFINED,		/* 179 */
	TPM_MEDIUM,		/* 17a */
	TPM_LONG,		/* 17b */
	TPM_UNDEFINED,		/* 17c */
	TPM_UNDEFINED,		/* 17d */
	TPM_UNDEFINED,		/* 17e */
	TPM_UNDEFINED,		/* 17f */
	TPM_UNDEFINED,		/* 180 */
	TPM_UNDEFINED,		/* 181 */
	TPM_MEDIUM,		/* 182 */
	TPM_UNDEFINED,		/* 183 */
	TPM_UNDEFINED,		/* 184 */
	TPM_MEDIUM,		/* 185 */
	TPM_MEDIUM,		/* 186 */
	TPM_UNDEFINED,		/* 187 */
	TPM_UNDEFINED,		/* 188 */
	TPM_UNDEFINED,		/* 189 */
	TPM_UNDEFINED,		/* 18a */
	TPM_UNDEFINED,		/* 18b */
	TPM_UNDEFINED,		/* 18c */
	TPM_UNDEFINED,		/* 18d */
	TPM_UNDEFINED,		/* 18e */
	TPM_UNDEFINED		/* 18f */
};

202 203 204 205 206 207 208 209 210 211 212
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 已提交
213 214 215 216
/**
 * tpm2_pcr_read() - read a PCR value
 * @chip:	TPM chip to use.
 * @pcr_idx:	index of the PCR to read.
W
Winkler, Tomas 已提交
217
 * @res_buf:	buffer to store the resulting hash.
J
Jarkko Sakkinen 已提交
218
 *
W
Winkler, Tomas 已提交
219
 * Return: Same as with tpm_transmit_cmd.
J
Jarkko Sakkinen 已提交
220 221 222 223
 */
int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
{
	int rc;
224 225 226
	struct tpm_buf buf;
	struct tpm2_pcr_read_out *out;
	u8 pcr_select[TPM2_PCR_SELECT_MIN] = {0};
J
Jarkko Sakkinen 已提交
227 228 229 230

	if (pcr_idx >= TPM2_PLATFORM_PCR)
		return -EINVAL;

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	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);
	tpm_buf_append_u16(&buf, TPM2_ALG_SHA1);
	tpm_buf_append_u8(&buf, TPM2_PCR_SELECT_MIN);
	tpm_buf_append(&buf, (const unsigned char *)pcr_select,
		       sizeof(pcr_select));

	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
			res_buf ? "attempting to read a pcr value" : NULL);
	if (rc == 0 && res_buf) {
		out = (struct tpm2_pcr_read_out *)&buf.data[TPM_HEADER_SIZE];
		memcpy(res_buf, out->digest, SHA1_DIGEST_SIZE);
J
Jarkko Sakkinen 已提交
248 249
	}

250
	tpm_buf_destroy(&buf);
J
Jarkko Sakkinen 已提交
251 252 253
	return rc;
}

254 255 256 257 258 259
struct tpm2_null_auth_area {
	__be32  handle;
	__be16  nonce_size;
	u8  attributes;
	__be16  auth_size;
} __packed;
J
Jarkko Sakkinen 已提交
260 261 262

/**
 * tpm2_pcr_extend() - extend a PCR value
W
Winkler, Tomas 已提交
263
 *
J
Jarkko Sakkinen 已提交
264 265
 * @chip:	TPM chip to use.
 * @pcr_idx:	index of the PCR.
266 267
 * @count:	number of digests passed.
 * @digests:	list of pcr banks and corresponding digest values to extend.
J
Jarkko Sakkinen 已提交
268
 *
W
Winkler, Tomas 已提交
269
 * Return: Same as with tpm_transmit_cmd.
J
Jarkko Sakkinen 已提交
270
 */
271 272
int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count,
		    struct tpm2_digest *digests)
J
Jarkko Sakkinen 已提交
273
{
274 275
	struct tpm_buf buf;
	struct tpm2_null_auth_area auth_area;
J
Jarkko Sakkinen 已提交
276
	int rc;
277 278
	int i;
	int j;
J
Jarkko Sakkinen 已提交
279

280 281
	if (count > ARRAY_SIZE(chip->active_banks))
		return -EINVAL;
J
Jarkko Sakkinen 已提交
282

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	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));
	tpm_buf_append_u32(&buf, count);

	for (i = 0; i < count; i++) {
		for (j = 0; j < ARRAY_SIZE(tpm2_hash_map); j++) {
			if (digests[i].alg_id != tpm2_hash_map[j].tpm_id)
				continue;
			tpm_buf_append_u16(&buf, digests[i].alg_id);
			tpm_buf_append(&buf, (const unsigned char
					      *)&digests[i].digest,
			       hash_digest_size[tpm2_hash_map[j].crypto_id]);
		}
	}

310
	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
J
Jarkko Sakkinen 已提交
311 312
			      "attempting extend a PCR value");

313 314
	tpm_buf_destroy(&buf);

J
Jarkko Sakkinen 已提交
315 316 317
	return rc;
}

318

J
Jarkko Sakkinen 已提交
319 320 321 322 323 324 325 326 327 328 329 330
#define TPM2_GETRANDOM_IN_SIZE \
	(sizeof(struct tpm_input_header) + \
	 sizeof(struct tpm2_get_random_in))

static const struct tpm_input_header tpm2_getrandom_header = {
	.tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
	.length = cpu_to_be32(TPM2_GETRANDOM_IN_SIZE),
	.ordinal = cpu_to_be32(TPM2_CC_GET_RANDOM)
};

/**
 * tpm2_get_random() - get random bytes from the TPM RNG
W
Winkler, Tomas 已提交
331
 *
J
Jarkko Sakkinen 已提交
332 333 334 335
 * @chip: TPM chip to use
 * @out: destination buffer for the random bytes
 * @max: the max number of bytes to write to @out
 *
W
Winkler, Tomas 已提交
336 337
 * Return:
 *    Size of the output buffer, or -EIO on error.
J
Jarkko Sakkinen 已提交
338 339 340 341
 */
int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)
{
	struct tpm2_cmd cmd;
342
	u32 recd, rlength;
J
Jarkko Sakkinen 已提交
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	u32 num_bytes;
	int err;
	int total = 0;
	int retries = 5;
	u8 *dest = out;

	num_bytes = min_t(u32, max, sizeof(cmd.params.getrandom_out.buffer));

	if (!out || !num_bytes ||
	    max > sizeof(cmd.params.getrandom_out.buffer))
		return -EINVAL;

	do {
		cmd.header.in = tpm2_getrandom_header;
		cmd.params.getrandom_in.size = cpu_to_be16(num_bytes);

359
		err = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd),
360 361 362
				       offsetof(struct tpm2_get_random_out,
						buffer),
				       0, "attempting get random");
J
Jarkko Sakkinen 已提交
363 364 365 366 367
		if (err)
			break;

		recd = min_t(u32, be16_to_cpu(cmd.params.getrandom_out.size),
			     num_bytes);
368 369 370 371
		rlength = be32_to_cpu(cmd.header.out.length);
		if (rlength < offsetof(struct tpm2_get_random_out, buffer) +
			      recd)
			return -EFAULT;
J
Jarkko Sakkinen 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385
		memcpy(dest, cmd.params.getrandom_out.buffer, recd);

		dest += recd;
		total += recd;
		num_bytes -= recd;
	} while (retries-- && total < max);

	return total ? total : -EIO;
}

#define TPM2_GET_TPM_PT_IN_SIZE \
	(sizeof(struct tpm_input_header) + \
	 sizeof(struct tpm2_get_tpm_pt_in))

386 387 388
#define TPM2_GET_TPM_PT_OUT_BODY_SIZE \
	 sizeof(struct tpm2_get_tpm_pt_out)

J
Jarkko Sakkinen 已提交
389 390 391 392 393 394
static const struct tpm_input_header tpm2_get_tpm_pt_header = {
	.tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
	.length = cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE),
	.ordinal = cpu_to_be32(TPM2_CC_GET_CAPABILITY)
};

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
/**
 * tpm2_flush_context_cmd() - execute a TPM2_FlushContext command
 * @chip: TPM chip to use
 * @payload: the key data in clear and encrypted form
 * @options: authentication values and other options
 *
 * Return: same as with tpm_transmit_cmd
 */
void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle,
			    unsigned int flags)
{
	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);

418
	(void) tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, flags,
419 420 421 422 423
				"flushing context");

	tpm_buf_destroy(&buf);
}

J
Jarkko Sakkinen 已提交
424
/**
W
Winkler, Tomas 已提交
425 426 427 428 429 430 431 432 433
 * 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 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
 */
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);
}

/**
455
 * tpm2_seal_trusted() - seal the payload of a trusted key
W
Winkler, Tomas 已提交
456 457
 *
 * @chip: TPM chip to use
J
Jarkko Sakkinen 已提交
458
 * @payload: the key data in clear and encrypted form
459
 * @options: authentication values and other options
J
Jarkko Sakkinen 已提交
460
 *
461
 * Return: < 0 on error and 0 on success.
J
Jarkko Sakkinen 已提交
462 463 464 465 466 467 468
 */
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;
469
	u32 hash, rlength;
470
	int i;
J
Jarkko Sakkinen 已提交
471 472
	int rc;

473 474 475 476 477 478 479 480 481 482
	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 已提交
483 484 485 486 487 488 489 490 491 492 493 494
	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 */
495
	tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1);
J
Jarkko Sakkinen 已提交
496 497 498

	tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE);
	tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE);
499
	tpm_buf_append_u16(&buf, payload->key_len + 1);
J
Jarkko Sakkinen 已提交
500
	tpm_buf_append(&buf, payload->key, payload->key_len);
501
	tpm_buf_append_u8(&buf, payload->migratable);
J
Jarkko Sakkinen 已提交
502 503

	/* public */
504
	tpm_buf_append_u16(&buf, 14 + options->policydigest_len);
J
Jarkko Sakkinen 已提交
505
	tpm_buf_append_u16(&buf, TPM2_ALG_KEYEDHASH);
506
	tpm_buf_append_u16(&buf, hash);
507 508

	/* policy */
509
	if (options->policydigest_len) {
510
		tpm_buf_append_u32(&buf, 0);
511
		tpm_buf_append_u16(&buf, options->policydigest_len);
512
		tpm_buf_append(&buf, options->policydigest,
513
			       options->policydigest_len);
514
	} else {
515
		tpm_buf_append_u32(&buf, TPM2_OA_USER_WITH_AUTH);
516 517 518 519
		tpm_buf_append_u16(&buf, 0);
	}

	/* public parameters */
J
Jarkko Sakkinen 已提交
520 521 522 523 524 525 526 527 528 529 530 531 532 533
	tpm_buf_append_u16(&buf, TPM2_ALG_NULL);
	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;
	}

534
	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, 0,
535
			      "sealing data");
J
Jarkko Sakkinen 已提交
536 537 538 539 540 541 542 543
	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;
	}
544 545 546 547 548
	rlength = be32_to_cpu(((struct tpm2_cmd *)&buf)->header.out.length);
	if (rlength < TPM_HEADER_SIZE + 4 + blob_len) {
		rc = -EFAULT;
		goto out;
	}
J
Jarkko Sakkinen 已提交
549 550 551 552 553 554 555

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

out:
	tpm_buf_destroy(&buf);

556
	if (rc > 0) {
557
		if (tpm2_rc_value(rc) == TPM2_RC_HASH)
558 559 560 561
			rc = -EINVAL;
		else
			rc = -EPERM;
	}
J
Jarkko Sakkinen 已提交
562 563 564 565

	return rc;
}

566 567
/**
 * tpm2_load_cmd() - execute a TPM2_Load command
W
Winkler, Tomas 已提交
568 569
 *
 * @chip: TPM chip to use
570 571
 * @payload: the key data in clear and encrypted form
 * @options: authentication values and other options
W
Winkler, Tomas 已提交
572 573
 * @blob_handle: returned blob handle
 * @flags: tpm transmit flags
574
 *
W
Winkler, Tomas 已提交
575 576 577 578
 * Return: 0 on success.
 *        -E2BIG on wrong payload size.
 *        -EPERM on tpm error status.
 *        < 0 error from tpm_transmit_cmd.
579 580 581 582 583
 */
static int tpm2_load_cmd(struct tpm_chip *chip,
			 struct trusted_key_payload *payload,
			 struct trusted_key_options *options,
			 u32 *blob_handle, unsigned int flags)
J
Jarkko Sakkinen 已提交
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
{
	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;
	}

618
	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, flags,
619
			      "loading blob");
J
Jarkko Sakkinen 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632
	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;
}

633 634
/**
 * tpm2_unseal_cmd() - execute a TPM2_Unload command
W
Winkler, Tomas 已提交
635 636
 *
 * @chip: TPM chip to use
637 638
 * @payload: the key data in clear and encrypted form
 * @options: authentication values and other options
W
Winkler, Tomas 已提交
639 640
 * @blob_handle: blob handle
 * @flags: tpm_transmit_cmd flags
641
 *
W
Winkler, Tomas 已提交
642 643 644
 * Return: 0 on success
 *         -EPERM on tpm error status
 *         < 0 error from tpm_transmit_cmd
645 646 647 648 649
 */
static int tpm2_unseal_cmd(struct tpm_chip *chip,
			   struct trusted_key_payload *payload,
			   struct trusted_key_options *options,
			   u32 blob_handle, unsigned int flags)
J
Jarkko Sakkinen 已提交
650 651
{
	struct tpm_buf buf;
652 653
	u16 data_len;
	u8 *data;
J
Jarkko Sakkinen 已提交
654
	int rc;
655
	u32 rlength;
J
Jarkko Sakkinen 已提交
656 657 658 659 660 661

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

	tpm_buf_append_u32(&buf, blob_handle);
662 663 664
	tpm2_buf_append_auth(&buf,
			     options->policyhandle ?
			     options->policyhandle : TPM2_RS_PW,
J
Jarkko Sakkinen 已提交
665
			     NULL /* nonce */, 0,
666
			     TPM2_SA_CONTINUE_SESSION,
J
Jarkko Sakkinen 已提交
667 668 669
			     options->blobauth /* hmac */,
			     TPM_DIGEST_SIZE);

670
	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 6, flags,
671
			      "unsealing");
J
Jarkko Sakkinen 已提交
672 673 674 675
	if (rc > 0)
		rc = -EPERM;

	if (!rc) {
676
		data_len = be16_to_cpup(
J
Jarkko Sakkinen 已提交
677
			(__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
678 679 680 681
		if (data_len < MIN_KEY_SIZE ||  data_len > MAX_KEY_SIZE + 1) {
			rc = -EFAULT;
			goto out;
		}
682 683 684 685 686 687 688

		rlength = be32_to_cpu(((struct tpm2_cmd *)&buf)
					->header.out.length);
		if (rlength < TPM_HEADER_SIZE + 6 + data_len) {
			rc = -EFAULT;
			goto out;
		}
689
		data = &buf.data[TPM_HEADER_SIZE + 6];
J
Jarkko Sakkinen 已提交
690

691 692 693
		memcpy(payload->key, data, data_len - 1);
		payload->key_len = data_len - 1;
		payload->migratable = data[data_len - 1];
J
Jarkko Sakkinen 已提交
694 695
	}

696
out:
J
Jarkko Sakkinen 已提交
697 698 699 700 701
	tpm_buf_destroy(&buf);
	return rc;
}

/**
702
 * tpm2_unseal_trusted() - unseal the payload of a trusted key
W
Winkler, Tomas 已提交
703 704
 *
 * @chip: TPM chip to use
J
Jarkko Sakkinen 已提交
705
 * @payload: the key data in clear and encrypted form
706
 * @options: authentication values and other options
J
Jarkko Sakkinen 已提交
707
 *
W
Winkler, Tomas 已提交
708
 * Return: Same as with tpm_transmit_cmd.
J
Jarkko Sakkinen 已提交
709 710 711 712 713 714 715 716
 */
int tpm2_unseal_trusted(struct tpm_chip *chip,
			struct trusted_key_payload *payload,
			struct trusted_key_options *options)
{
	u32 blob_handle;
	int rc;

717 718 719
	mutex_lock(&chip->tpm_mutex);
	rc = tpm2_load_cmd(chip, payload, options, &blob_handle,
			   TPM_TRANSMIT_UNLOCKED);
J
Jarkko Sakkinen 已提交
720
	if (rc)
721
		goto out;
J
Jarkko Sakkinen 已提交
722

723 724 725 726 727
	rc = tpm2_unseal_cmd(chip, payload, options, blob_handle,
			     TPM_TRANSMIT_UNLOCKED);
	tpm2_flush_context_cmd(chip, blob_handle, TPM_TRANSMIT_UNLOCKED);
out:
	mutex_unlock(&chip->tpm_mutex);
J
Jarkko Sakkinen 已提交
728 729 730
	return rc;
}

J
Jarkko Sakkinen 已提交
731 732 733 734 735 736 737
/**
 * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
 * @chip:		TPM chip to use.
 * @property_id:	property ID.
 * @value:		output variable.
 * @desc:		passed to tpm_transmit_cmd()
 *
W
Winkler, Tomas 已提交
738
 * Return: Same as with tpm_transmit_cmd.
J
Jarkko Sakkinen 已提交
739 740 741 742 743 744 745 746 747 748 749 750
 */
ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,  u32 *value,
			const char *desc)
{
	struct tpm2_cmd cmd;
	int rc;

	cmd.header.in = tpm2_get_tpm_pt_header;
	cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
	cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id);
	cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);

751
	rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd),
752
			      TPM2_GET_TPM_PT_OUT_BODY_SIZE, 0, desc);
J
Jarkko Sakkinen 已提交
753
	if (!rc)
754
		*value = be32_to_cpu(cmd.params.get_tpm_pt_out.value);
J
Jarkko Sakkinen 已提交
755 756 757

	return rc;
}
758
EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
J
Jarkko Sakkinen 已提交
759 760

/**
761
 * tpm2_shutdown() - send a TPM shutdown command
W
Winkler, Tomas 已提交
762
 *
763 764 765 766 767 768
 * 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 已提交
769
 */
770
void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
J
Jarkko Sakkinen 已提交
771
{
772
	struct tpm_buf buf;
773
	int rc;
J
Jarkko Sakkinen 已提交
774

775 776 777 778 779 780 781
	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SHUTDOWN);
	if (rc)
		return;
	tpm_buf_append_u16(&buf, shutdown_type);
	tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
			 "stopping the TPM");
	tpm_buf_destroy(&buf);
J
Jarkko Sakkinen 已提交
782 783 784 785
}

/*
 * tpm2_calc_ordinal_duration() - maximum duration for a command
W
Winkler, Tomas 已提交
786
 *
J
Jarkko Sakkinen 已提交
787 788 789
 * @chip:	TPM chip to use.
 * @ordinal:	command code number.
 *
W
Winkler, Tomas 已提交
790
 * Return: maximum duration for a command
J
Jarkko Sakkinen 已提交
791 792 793 794 795 796 797 798 799 800
 */
unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
{
	int index = TPM_UNDEFINED;
	int duration = 0;

	if (ordinal >= TPM2_CC_FIRST && ordinal <= TPM2_CC_LAST)
		index = tpm2_ordinal_duration[ordinal - TPM2_CC_FIRST];

	if (index != TPM_UNDEFINED)
801
		duration = chip->duration[index];
J
Jarkko Sakkinen 已提交
802 803

	if (duration <= 0)
804
		duration = msecs_to_jiffies(TPM2_DURATION_DEFAULT);
J
Jarkko Sakkinen 已提交
805 806 807 808 809 810

	return duration;
}
EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration);

/**
811
 * tpm2_do_selftest() - ensure that all self tests have passed
W
Winkler, Tomas 已提交
812
 *
J
Jarkko Sakkinen 已提交
813 814
 * @chip: TPM chip to use
 *
W
Winkler, Tomas 已提交
815 816
 * Return: Same as with tpm_transmit_cmd.
 *
817 818 819 820 821
 * 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 已提交
822
 */
823
static int tpm2_do_selftest(struct tpm_chip *chip)
J
Jarkko Sakkinen 已提交
824
{
825 826
	struct tpm_buf buf;
	int full;
J
Jarkko Sakkinen 已提交
827 828
	int rc;

829 830 831 832
	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 已提交
833

834 835 836 837
		tpm_buf_append_u8(&buf, full);
		rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0,
				      "attempting the self test");
		tpm_buf_destroy(&buf);
J
Jarkko Sakkinen 已提交
838

839 840 841 842
		if (rc == TPM2_RC_TESTING)
			rc = TPM2_RC_SUCCESS;
		if (rc == TPM2_RC_INITIALIZE || rc == TPM2_RC_SUCCESS)
			return rc;
J
Jarkko Sakkinen 已提交
843 844 845 846 847
	}

	return rc;
}

848
/**
849 850
 * tpm2_probe() - probe for the TPM 2.0 protocol
 * @chip:	a &tpm_chip instance
851
 *
852 853 854
 * 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 已提交
855
 *
856 857 858
 * Return:
 *   0 on success,
 *   -errno otherwise
859 860 861
 */
int tpm2_probe(struct tpm_chip *chip)
{
862 863
	struct tpm_output_header *out;
	struct tpm_buf buf;
864 865
	int rc;

866 867
	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
	if (rc)
868
		return rc;
869 870 871 872 873 874 875 876 877 878 879
	tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
	tpm_buf_append_u32(&buf, TPM_PT_TOTAL_COMMANDS);
	tpm_buf_append_u32(&buf, 1);
	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, NULL);
	/* We ignore TPM return codes on purpose. */
	if (rc >=  0) {
		out = (struct tpm_output_header *)buf.data;
		if (be16_to_cpu(out->tag) == TPM2_ST_NO_SESSIONS)
			chip->flags |= TPM_CHIP_FLAG_TPM2;
	}
	tpm_buf_destroy(&buf);
880 881 882
	return 0;
}
EXPORT_SYMBOL_GPL(tpm2_probe);
883

884 885 886 887 888 889
struct tpm2_pcr_selection {
	__be16  hash_alg;
	u8  size_of_select;
	u8  pcr_select[3];
} __packed;

890
static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
{
	struct tpm2_pcr_selection pcr_selection;
	struct tpm_buf buf;
	void *marker;
	void *end;
	void *pcr_select_offset;
	unsigned int count;
	u32 sizeof_pcr_selection;
	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);

911
	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 9, 0,
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
			      "get tpm pcr allocation");
	if (rc)
		goto out;

	count = be32_to_cpup(
		(__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);

	if (count > ARRAY_SIZE(chip->active_banks)) {
		rc = -ENODEV;
		goto out;
	}

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

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

	for (i = 0; i < count; i++) {
		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));
		chip->active_banks[i] = be16_to_cpu(pcr_selection.hash_alg);
		sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
			sizeof(pcr_selection.size_of_select) +
			pcr_selection.size_of_select;
		marker = marker + sizeof_pcr_selection;
	}

out:
	if (i < ARRAY_SIZE(chip->active_banks))
		chip->active_banks[i] = TPM2_ALG_ERROR;

	tpm_buf_destroy(&buf);

	return rc;
}
953

J
Jarkko Sakkinen 已提交
954 955 956 957
static int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip)
{
	struct tpm_buf buf;
	u32 nr_commands;
958
	__be32 *attrs;
J
Jarkko Sakkinen 已提交
959 960 961 962 963 964 965 966 967 968 969 970 971
	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;
	}

972
	chip->cc_attrs_tbl = devm_kcalloc(&chip->dev, 4, nr_commands,
J
Jarkko Sakkinen 已提交
973 974 975 976 977 978 979 980 981 982
					  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);

983 984
	rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE,
			      9 + 4 * nr_commands, 0, NULL);
J
Jarkko Sakkinen 已提交
985 986 987 988 989 990 991 992 993 994 995 996 997
	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;

998
	attrs = (__be32 *)&buf.data[TPM_HEADER_SIZE + 9];
J
Jarkko Sakkinen 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
	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;
}

1018 1019 1020 1021 1022
/**
 * tpm2_auto_startup - Perform the standard automatic TPM initialization
 *                     sequence
 * @chip: TPM chip to use
 *
J
Jarkko Sakkinen 已提交
1023
 * Returns 0 on success, < 0 in case of fatal error.
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
 */
int tpm2_auto_startup(struct tpm_chip *chip)
{
	int rc;

	rc = tpm_get_timeouts(chip);
	if (rc)
		goto out;

	rc = tpm2_do_selftest(chip);
1034
	if (rc && rc != TPM2_RC_INITIALIZE)
1035 1036 1037
		goto out;

	if (rc == TPM2_RC_INITIALIZE) {
1038
		rc = tpm_startup(chip);
1039 1040 1041 1042
		if (rc)
			goto out;

		rc = tpm2_do_selftest(chip);
1043
		if (rc)
1044 1045 1046 1047
			goto out;
	}

	rc = tpm2_get_pcr_allocation(chip);
J
Jarkko Sakkinen 已提交
1048 1049 1050 1051
	if (rc)
		goto out;

	rc = tpm2_get_cc_attrs_tbl(chip);
1052 1053 1054 1055 1056 1057

out:
	if (rc > 0)
		rc = -ENODEV;
	return rc;
}
J
Jarkko Sakkinen 已提交
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068

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;
}