1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2014, 2015 Intel Corporation
6 * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
8 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
10 * This file contains TPM2 protocol implementations of the commands
11 * used by the kernel internally.
15 #include <crypto/hash_info.h>
17 static 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},
25 int tpm2_get_timeouts(struct tpm_chip *chip)
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);
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);
38 /* Key creation commands long timeouts */
39 chip->duration[TPM_LONG_LONG] =
40 msecs_to_jiffies(TPM2_DURATION_LONG_LONG);
42 chip->flags |= TPM_CHIP_FLAG_HAVE_TIMEOUTS;
48 * tpm2_ordinal_duration_index() - returns an index to the chip duration table
49 * @ordinal: TPM command ordinal.
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.
55 * The values of the MEDIUM, and LONG durations are taken
56 * from the PC Client Profile (PTP) specification (750, 2000 msec)
58 * LONG_LONG is for commands that generates keys which empirically takes
59 * a longer time on some systems.
67 static u8 tpm2_ordinal_duration_index(u32 ordinal)
71 case TPM2_CC_STARTUP: /* 144 */
74 case TPM2_CC_SELF_TEST: /* 143 */
77 case TPM2_CC_GET_RANDOM: /* 17B */
80 case TPM2_CC_SEQUENCE_UPDATE: /* 15C */
82 case TPM2_CC_SEQUENCE_COMPLETE: /* 13E */
84 case TPM2_CC_EVENT_SEQUENCE_COMPLETE: /* 185 */
86 case TPM2_CC_HASH_SEQUENCE_START: /* 186 */
89 case TPM2_CC_VERIFY_SIGNATURE: /* 177 */
92 case TPM2_CC_PCR_EXTEND: /* 182 */
95 case TPM2_CC_HIERARCHY_CONTROL: /* 121 */
97 case TPM2_CC_HIERARCHY_CHANGE_AUTH: /* 129 */
100 case TPM2_CC_GET_CAPABILITY: /* 17A */
103 case TPM2_CC_NV_READ: /* 14E */
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;
114 return TPM_UNDEFINED;
119 * tpm2_calc_ordinal_duration() - calculate the maximum command duration
120 * @chip: TPM chip to use.
121 * @ordinal: TPM command ordinal.
123 * The function returns the maximum amount of time the chip could take
124 * to return the result for a particular ordinal in jiffies.
126 * Return: A maximal duration time for an ordinal in jiffies.
128 unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
132 index = tpm2_ordinal_duration_index(ordinal);
134 if (index != TPM_UNDEFINED)
135 return chip->duration[index];
137 return msecs_to_jiffies(TPM2_DURATION_DEFAULT);
141 struct tpm2_pcr_read_out {
143 __be32 pcr_selects_cnt;
146 u8 pcr_select[TPM2_PCR_SELECT_MIN];
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.
159 * Return: Same as with tpm_transmit_cmd.
161 int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
162 struct tpm_digest *digest, u16 *digest_size_ptr)
167 struct tpm2_pcr_read_out *out;
168 u8 pcr_select[TPM2_PCR_SELECT_MIN] = {0};
170 u16 expected_digest_size = 0;
172 if (pcr_idx >= TPM2_PLATFORM_PCR)
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++)
180 if (i == chip->nr_allocated_banks)
183 expected_digest_size = chip->allocated_banks[i].digest_size;
186 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_READ);
190 pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7);
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,
198 rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to read a pcr value");
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)) {
211 *digest_size_ptr = digest_size;
213 memcpy(digest->digest, out->digest, digest_size);
215 tpm_buf_destroy(&buf);
220 * tpm2_pcr_extend() - extend a PCR value
222 * @chip: TPM chip to use.
223 * @pcr_idx: index of the PCR.
224 * @digests: list of pcr banks and corresponding digest values to extend.
226 * Return: Same as with tpm_transmit_cmd.
228 int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
229 struct tpm_digest *digests)
235 rc = tpm2_start_auth_session(chip);
239 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_EXTEND);
241 tpm2_end_auth_session(chip);
245 tpm_buf_append_name(chip, &buf, pcr_idx, NULL);
246 tpm_buf_append_hmac_session(chip, &buf, 0, NULL, 0);
248 tpm_buf_append_u32(&buf, chip->nr_allocated_banks);
250 for (i = 0; i < chip->nr_allocated_banks; i++) {
251 tpm_buf_append_u16(&buf, digests[i].alg_id);
252 tpm_buf_append(&buf, (const unsigned char *)&digests[i].digest,
253 chip->allocated_banks[i].digest_size);
256 tpm_buf_fill_hmac_session(chip, &buf);
257 rc = tpm_transmit_cmd(chip, &buf, 0, "attempting extend a PCR value");
258 rc = tpm_buf_check_hmac_response(chip, &buf, rc);
260 tpm_buf_destroy(&buf);
265 struct tpm2_get_random_out {
267 u8 buffer[TPM_MAX_RNG_DATA];
271 * tpm2_get_random() - get random bytes from the TPM RNG
273 * @chip: a &tpm_chip instance
274 * @dest: destination buffer
275 * @max: the max number of random bytes to pull
278 * size of the buffer on success,
279 * -errno otherwise (positive TPM return codes are masked to -EIO)
281 int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
283 struct tpm2_get_random_out *out;
284 struct tpm_header *head;
294 if (!num_bytes || max > TPM_MAX_RNG_DATA)
297 err = tpm2_start_auth_session(chip);
301 err = tpm_buf_init(&buf, 0, 0);
303 tpm2_end_auth_session(chip);
308 tpm_buf_reset(&buf, TPM2_ST_SESSIONS, TPM2_CC_GET_RANDOM);
309 tpm_buf_append_hmac_session_opt(chip, &buf, TPM2_SA_ENCRYPT
310 | TPM2_SA_CONTINUE_SESSION,
312 tpm_buf_append_u16(&buf, num_bytes);
313 tpm_buf_fill_hmac_session(chip, &buf);
314 err = tpm_transmit_cmd(chip, &buf,
315 offsetof(struct tpm2_get_random_out,
317 "attempting get random");
318 err = tpm_buf_check_hmac_response(chip, &buf, err);
325 head = (struct tpm_header *)buf.data;
326 offset = TPM_HEADER_SIZE;
327 /* Skip the parameter size field: */
328 if (be16_to_cpu(head->tag) == TPM2_ST_SESSIONS)
331 out = (struct tpm2_get_random_out *)&buf.data[offset];
332 recd = min_t(u32, be16_to_cpu(out->size), num_bytes);
333 if (tpm_buf_length(&buf) <
335 offsetof(struct tpm2_get_random_out, buffer) +
340 memcpy(dest_ptr, out->buffer, recd);
345 } while (retries-- && total < max);
347 tpm_buf_destroy(&buf);
348 tpm2_end_auth_session(chip);
350 return total ? total : -EIO;
352 tpm_buf_destroy(&buf);
353 tpm2_end_auth_session(chip);
358 * tpm2_flush_context() - execute a TPM2_FlushContext command
359 * @chip: TPM chip to use
360 * @handle: context handle
362 void tpm2_flush_context(struct tpm_chip *chip, u32 handle)
367 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT);
369 dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n",
374 tpm_buf_append_u32(&buf, handle);
376 tpm_transmit_cmd(chip, &buf, 0, "flushing context");
377 tpm_buf_destroy(&buf);
379 EXPORT_SYMBOL_GPL(tpm2_flush_context);
381 struct tpm2_get_cap_out {
390 * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
391 * @chip: a &tpm_chip instance
392 * @property_id: property ID.
393 * @value: output variable.
394 * @desc: passed to tpm_transmit_cmd()
398 * -errno or a TPM return code otherwise
400 ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
403 struct tpm2_get_cap_out *out;
407 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
410 tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
411 tpm_buf_append_u32(&buf, property_id);
412 tpm_buf_append_u32(&buf, 1);
413 rc = tpm_transmit_cmd(chip, &buf, 0, NULL);
415 out = (struct tpm2_get_cap_out *)
416 &buf.data[TPM_HEADER_SIZE];
418 * To prevent failing boot up of some systems, Infineon TPM2.0
419 * returns SUCCESS on TPM2_Startup in field upgrade mode. Also
420 * the TPM2_Getcapability command returns a zero length list
421 * in field upgrade mode.
423 if (be32_to_cpu(out->property_cnt) > 0)
424 *value = be32_to_cpu(out->value);
428 tpm_buf_destroy(&buf);
431 EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt);
434 * tpm2_shutdown() - send a TPM shutdown command
436 * Sends a TPM shutdown command. The shutdown command is used in call
437 * sites where the system is going down. If it fails, there is not much
438 * that can be done except print an error message.
440 * @chip: a &tpm_chip instance
441 * @shutdown_type: TPM_SU_CLEAR or TPM_SU_STATE.
443 void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
448 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SHUTDOWN);
451 tpm_buf_append_u16(&buf, shutdown_type);
452 tpm_transmit_cmd(chip, &buf, 0, "stopping the TPM");
453 tpm_buf_destroy(&buf);
457 * tpm2_do_selftest() - ensure that all self tests have passed
459 * @chip: TPM chip to use
461 * Return: Same as with tpm_transmit_cmd.
463 * The TPM can either run all self tests synchronously and then return
464 * RC_SUCCESS once all tests were successful. Or it can choose to run the tests
465 * asynchronously and return RC_TESTING immediately while the self tests still
466 * execute in the background. This function handles both cases and waits until
467 * all tests have completed.
469 static int tpm2_do_selftest(struct tpm_chip *chip)
475 for (full = 0; full < 2; full++) {
476 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SELF_TEST);
480 tpm_buf_append_u8(&buf, full);
481 rc = tpm_transmit_cmd(chip, &buf, 0,
482 "attempting the self test");
483 tpm_buf_destroy(&buf);
485 if (rc == TPM2_RC_TESTING)
486 rc = TPM2_RC_SUCCESS;
487 if (rc == TPM2_RC_INITIALIZE || rc == TPM2_RC_SUCCESS)
495 * tpm2_probe() - probe for the TPM 2.0 protocol
496 * @chip: a &tpm_chip instance
498 * Send an idempotent TPM 2.0 command and see whether there is TPM2 chip in the
499 * other end based on the response tag. The flag TPM_CHIP_FLAG_TPM2 is set by
500 * this function if this is the case.
506 int tpm2_probe(struct tpm_chip *chip)
508 struct tpm_header *out;
512 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
515 tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES);
516 tpm_buf_append_u32(&buf, TPM_PT_TOTAL_COMMANDS);
517 tpm_buf_append_u32(&buf, 1);
518 rc = tpm_transmit_cmd(chip, &buf, 0, NULL);
519 /* We ignore TPM return codes on purpose. */
521 out = (struct tpm_header *)buf.data;
522 if (be16_to_cpu(out->tag) == TPM2_ST_NO_SESSIONS)
523 chip->flags |= TPM_CHIP_FLAG_TPM2;
525 tpm_buf_destroy(&buf);
528 EXPORT_SYMBOL_GPL(tpm2_probe);
530 static int tpm2_init_bank_info(struct tpm_chip *chip, u32 bank_index)
532 struct tpm_bank_info *bank = chip->allocated_banks + bank_index;
533 struct tpm_digest digest = { .alg_id = bank->alg_id };
537 * Avoid unnecessary PCR read operations to reduce overhead
538 * and obtain identifiers of the crypto subsystem.
540 for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
541 enum hash_algo crypto_algo = tpm2_hash_map[i].crypto_id;
543 if (bank->alg_id != tpm2_hash_map[i].tpm_id)
546 bank->digest_size = hash_digest_size[crypto_algo];
547 bank->crypto_id = crypto_algo;
551 bank->crypto_id = HASH_ALGO__LAST;
553 return tpm2_pcr_read(chip, 0, &digest, &bank->digest_size);
556 struct tpm2_pcr_selection {
562 ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
564 struct tpm2_pcr_selection pcr_selection;
568 void *pcr_select_offset;
569 u32 sizeof_pcr_selection;
570 u32 nr_possible_banks;
571 u32 nr_alloc_banks = 0;
577 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
581 tpm_buf_append_u32(&buf, TPM2_CAP_PCRS);
582 tpm_buf_append_u32(&buf, 0);
583 tpm_buf_append_u32(&buf, 1);
585 rc = tpm_transmit_cmd(chip, &buf, 9, "get tpm pcr allocation");
589 nr_possible_banks = be32_to_cpup(
590 (__be32 *)&buf.data[TPM_HEADER_SIZE + 5]);
592 chip->allocated_banks = kcalloc(nr_possible_banks,
593 sizeof(*chip->allocated_banks),
595 if (!chip->allocated_banks) {
600 marker = &buf.data[TPM_HEADER_SIZE + 9];
602 rsp_len = be32_to_cpup((__be32 *)&buf.data[2]);
603 end = &buf.data[rsp_len];
605 for (i = 0; i < nr_possible_banks; i++) {
606 pcr_select_offset = marker +
607 offsetof(struct tpm2_pcr_selection, size_of_select);
608 if (pcr_select_offset >= end) {
613 memcpy(&pcr_selection, marker, sizeof(pcr_selection));
614 hash_alg = be16_to_cpu(pcr_selection.hash_alg);
616 pcr_select_offset = memchr_inv(pcr_selection.pcr_select, 0,
617 pcr_selection.size_of_select);
618 if (pcr_select_offset) {
619 chip->allocated_banks[nr_alloc_banks].alg_id = hash_alg;
621 rc = tpm2_init_bank_info(chip, nr_alloc_banks);
628 sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
629 sizeof(pcr_selection.size_of_select) +
630 pcr_selection.size_of_select;
631 marker = marker + sizeof_pcr_selection;
634 chip->nr_allocated_banks = nr_alloc_banks;
636 tpm_buf_destroy(&buf);
641 int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip)
650 rc = tpm2_get_tpm_pt(chip, TPM_PT_TOTAL_COMMANDS, &nr_commands, NULL);
654 if (nr_commands > 0xFFFFF) {
659 chip->cc_attrs_tbl = devm_kcalloc(&chip->dev, 4, nr_commands,
661 if (!chip->cc_attrs_tbl) {
666 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
670 tpm_buf_append_u32(&buf, TPM2_CAP_COMMANDS);
671 tpm_buf_append_u32(&buf, TPM2_CC_FIRST);
672 tpm_buf_append_u32(&buf, nr_commands);
674 rc = tpm_transmit_cmd(chip, &buf, 9 + 4 * nr_commands, NULL);
676 tpm_buf_destroy(&buf);
681 be32_to_cpup((__be32 *)&buf.data[TPM_HEADER_SIZE + 5])) {
683 tpm_buf_destroy(&buf);
687 chip->nr_commands = nr_commands;
689 attrs = (__be32 *)&buf.data[TPM_HEADER_SIZE + 9];
690 for (i = 0; i < nr_commands; i++, attrs++) {
691 chip->cc_attrs_tbl[i] = be32_to_cpup(attrs);
692 cc = chip->cc_attrs_tbl[i] & 0xFFFF;
694 if (cc == TPM2_CC_CONTEXT_SAVE || cc == TPM2_CC_FLUSH_CONTEXT) {
695 chip->cc_attrs_tbl[i] &=
696 ~(GENMASK(2, 0) << TPM2_CC_ATTR_CHANDLES);
697 chip->cc_attrs_tbl[i] |= 1 << TPM2_CC_ATTR_CHANDLES;
701 tpm_buf_destroy(&buf);
708 EXPORT_SYMBOL_GPL(tpm2_get_cc_attrs_tbl);
711 * tpm2_startup - turn on the TPM
712 * @chip: TPM chip to use
714 * Normally the firmware should start the TPM. This function is provided as a
715 * workaround if this does not happen. A legal case for this could be for
716 * example when a TPM emulator is used.
718 * Return: same as tpm_transmit_cmd()
721 static int tpm2_startup(struct tpm_chip *chip)
726 dev_info(&chip->dev, "starting up the TPM manually\n");
728 rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_STARTUP);
732 tpm_buf_append_u16(&buf, TPM2_SU_CLEAR);
733 rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to start the TPM");
734 tpm_buf_destroy(&buf);
740 * tpm2_auto_startup - Perform the standard automatic TPM initialization
742 * @chip: TPM chip to use
744 * Returns 0 on success, < 0 in case of fatal error.
746 int tpm2_auto_startup(struct tpm_chip *chip)
750 rc = tpm2_get_timeouts(chip);
754 rc = tpm2_do_selftest(chip);
755 if (rc && rc != TPM2_RC_INITIALIZE)
758 if (rc == TPM2_RC_INITIALIZE) {
759 rc = tpm2_startup(chip);
763 rc = tpm2_do_selftest(chip);
768 rc = tpm2_get_cc_attrs_tbl(chip);
769 if (rc == TPM2_RC_FAILURE || (rc < 0 && rc != -ENOMEM)) {
771 "TPM in field failure mode, requires firmware upgrade\n");
772 chip->flags |= TPM_CHIP_FLAG_FIRMWARE_UPGRADE;
779 rc = tpm2_sessions_init(chip);
783 * Infineon TPM in field upgrade mode will return no data for the number
784 * of supported commands.
786 if (rc == TPM2_RC_UPGRADE || rc == -ENODATA) {
787 dev_info(&chip->dev, "TPM in field upgrade mode, requires firmware upgrade\n");
788 chip->flags |= TPM_CHIP_FLAG_FIRMWARE_UPGRADE;
797 int tpm2_find_cc(struct tpm_chip *chip, u32 cc)
802 cc_mask = 1 << TPM2_CC_ATTR_VENDOR | GENMASK(15, 0);
803 for (i = 0; i < chip->nr_commands; i++)
804 if (cc == (chip->cc_attrs_tbl[i] & cc_mask))