Linux Audio

Check our new training course

In-person Linux kernel drivers training

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