Thomas Gleixner | b886d83c | 2019-06-01 10:08:55 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 2 | /* |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 3 | * Copyright (C) 2014, 2015 Intel Corporation |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 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. |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include "tpm.h" |
Jarkko Sakkinen | 5ca4c20 | 2015-11-05 21:43:06 +0200 | [diff] [blame] | 15 | #include <crypto/hash_info.h> |
Jarkko Sakkinen | 5ca4c20 | 2015-11-05 21:43:06 +0200 | [diff] [blame] | 16 | |
| 17 | static struct tpm2_hash tpm2_hash_map[] = { |
Roberto Sassu | aa04247 | 2019-02-06 17:24:48 +0100 | [diff] [blame] | 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}, |
Jarkko Sakkinen | 5ca4c20 | 2015-11-05 21:43:06 +0200 | [diff] [blame] | 23 | }; |
| 24 | |
Tomas Winkler | 70a3199 | 2018-10-19 21:22:52 +0300 | [diff] [blame] | 25 | int 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 | |
Tomas Winkler | 899102b | 2018-10-19 21:22:48 +0300 | [diff] [blame] | 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 |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 66 | */ |
Tomas Winkler | 899102b | 2018-10-19 21:22:48 +0300 | [diff] [blame] | 67 | static 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 */ |
Amir Mizinski | 5317677 | 2021-05-25 14:13:25 +0300 | [diff] [blame] | 90 | return TPM_LONG_LONG; |
Tomas Winkler | 899102b | 2018-10-19 21:22:48 +0300 | [diff] [blame] | 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 | */ |
| 128 | unsigned 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 | } |
Tomas Winkler | 899102b | 2018-10-19 21:22:48 +0300 | [diff] [blame] | 139 | |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 140 | |
Roberto Sassu | 91f7f3d | 2017-06-23 15:41:56 +0200 | [diff] [blame] | 141 | struct 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 | |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 152 | /** |
| 153 | * tpm2_pcr_read() - read a PCR value |
| 154 | * @chip: TPM chip to use. |
| 155 | * @pcr_idx: index of the PCR to read. |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 156 | * @digest: PCR bank and buffer current PCR value is written to. |
| 157 | * @digest_size_ptr: pointer to variable that stores the digest size. |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 158 | * |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 159 | * Return: Same as with tpm_transmit_cmd. |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 160 | */ |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 161 | int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, |
| 162 | struct tpm_digest *digest, u16 *digest_size_ptr) |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 163 | { |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 164 | int i; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 165 | int rc; |
Roberto Sassu | 91f7f3d | 2017-06-23 15:41:56 +0200 | [diff] [blame] | 166 | struct tpm_buf buf; |
| 167 | struct tpm2_pcr_read_out *out; |
| 168 | u8 pcr_select[TPM2_PCR_SELECT_MIN] = {0}; |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 169 | u16 digest_size; |
| 170 | u16 expected_digest_size = 0; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 171 | |
| 172 | if (pcr_idx >= TPM2_PLATFORM_PCR) |
| 173 | return -EINVAL; |
| 174 | |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 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 | |
Roberto Sassu | 91f7f3d | 2017-06-23 15:41:56 +0200 | [diff] [blame] | 186 | rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_READ); |
| 187 | if (rc) |
| 188 | return rc; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 189 | |
Roberto Sassu | 91f7f3d | 2017-06-23 15:41:56 +0200 | [diff] [blame] | 190 | pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 191 | |
Roberto Sassu | 91f7f3d | 2017-06-23 15:41:56 +0200 | [diff] [blame] | 192 | tpm_buf_append_u32(&buf, 1); |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 193 | tpm_buf_append_u16(&buf, digest->alg_id); |
Roberto Sassu | 91f7f3d | 2017-06-23 15:41:56 +0200 | [diff] [blame] | 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 | |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 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; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 210 | if (digest_size_ptr) |
| 211 | *digest_size_ptr = digest_size; |
| 212 | |
| 213 | memcpy(digest->digest, out->digest, digest_size); |
| 214 | out: |
Roberto Sassu | 91f7f3d | 2017-06-23 15:41:56 +0200 | [diff] [blame] | 215 | tpm_buf_destroy(&buf); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 216 | return rc; |
| 217 | } |
| 218 | |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 219 | struct tpm2_null_auth_area { |
| 220 | __be32 handle; |
| 221 | __be16 nonce_size; |
| 222 | u8 attributes; |
| 223 | __be16 auth_size; |
| 224 | } __packed; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 225 | |
| 226 | /** |
| 227 | * tpm2_pcr_extend() - extend a PCR value |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 228 | * |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 229 | * @chip: TPM chip to use. |
| 230 | * @pcr_idx: index of the PCR. |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 231 | * @digests: list of pcr banks and corresponding digest values to extend. |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 232 | * |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 233 | * Return: Same as with tpm_transmit_cmd. |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 234 | */ |
Roberto Sassu | 0b6cf6b | 2019-02-06 17:24:52 +0100 | [diff] [blame] | 235 | int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, |
Roberto Sassu | aa04247 | 2019-02-06 17:24:48 +0100 | [diff] [blame] | 236 | struct tpm_digest *digests) |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 237 | { |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 238 | struct tpm_buf buf; |
| 239 | struct tpm2_null_auth_area auth_area; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 240 | int rc; |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 241 | int i; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 242 | |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 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)); |
Roberto Sassu | 0b6cf6b | 2019-02-06 17:24:52 +0100 | [diff] [blame] | 257 | tpm_buf_append_u32(&buf, chip->nr_allocated_banks); |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 258 | |
Roberto Sassu | 0b6cf6b | 2019-02-06 17:24:52 +0100 | [diff] [blame] | 259 | for (i = 0; i < chip->nr_allocated_banks; i++) { |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 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); |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 263 | } |
| 264 | |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 265 | rc = tpm_transmit_cmd(chip, &buf, 0, "attempting extend a PCR value"); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 266 | |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 267 | tpm_buf_destroy(&buf); |
| 268 | |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 269 | return rc; |
| 270 | } |
| 271 | |
Jarkko Sakkinen | ce63c05 | 2018-03-26 15:14:06 +0300 | [diff] [blame] | 272 | struct tpm2_get_random_out { |
| 273 | __be16 size; |
| 274 | u8 buffer[TPM_MAX_RNG_DATA]; |
| 275 | } __packed; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 276 | |
| 277 | /** |
| 278 | * tpm2_get_random() - get random bytes from the TPM RNG |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 279 | * |
Jarkko Sakkinen | ce63c05 | 2018-03-26 15:14:06 +0300 | [diff] [blame] | 280 | * @chip: a &tpm_chip instance |
| 281 | * @dest: destination buffer |
| 282 | * @max: the max number of random bytes to pull |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 283 | * |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 284 | * Return: |
Jarkko Sakkinen | ce63c05 | 2018-03-26 15:14:06 +0300 | [diff] [blame] | 285 | * size of the buffer on success, |
Kees Cook | 782779b | 2019-04-01 12:06:07 -0700 | [diff] [blame] | 286 | * -errno otherwise (positive TPM return codes are masked to -EIO) |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 287 | */ |
Jarkko Sakkinen | ce63c05 | 2018-03-26 15:14:06 +0300 | [diff] [blame] | 288 | int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 289 | { |
Jarkko Sakkinen | ce63c05 | 2018-03-26 15:14:06 +0300 | [diff] [blame] | 290 | struct tpm2_get_random_out *out; |
| 291 | struct tpm_buf buf; |
| 292 | u32 recd; |
| 293 | u32 num_bytes = max; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 294 | int err; |
| 295 | int total = 0; |
| 296 | int retries = 5; |
Jarkko Sakkinen | ce63c05 | 2018-03-26 15:14:06 +0300 | [diff] [blame] | 297 | u8 *dest_ptr = dest; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 298 | |
Jarkko Sakkinen | ce63c05 | 2018-03-26 15:14:06 +0300 | [diff] [blame] | 299 | if (!num_bytes || max > TPM_MAX_RNG_DATA) |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 300 | return -EINVAL; |
| 301 | |
Jarkko Sakkinen | ce63c05 | 2018-03-26 15:14:06 +0300 | [diff] [blame] | 302 | err = tpm_buf_init(&buf, 0, 0); |
| 303 | if (err) |
| 304 | return err; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 305 | |
Jarkko Sakkinen | ce63c05 | 2018-03-26 15:14:06 +0300 | [diff] [blame] | 306 | do { |
| 307 | tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM); |
| 308 | tpm_buf_append_u16(&buf, num_bytes); |
Jarkko Sakkinen | 5faafba | 2018-11-03 15:15:07 +0200 | [diff] [blame] | 309 | err = tpm_transmit_cmd(chip, &buf, |
Stefan Berger | c659af7 | 2017-01-19 07:19:12 -0500 | [diff] [blame] | 310 | offsetof(struct tpm2_get_random_out, |
| 311 | buffer), |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 312 | "attempting get random"); |
Kees Cook | 782779b | 2019-04-01 12:06:07 -0700 | [diff] [blame] | 313 | if (err) { |
| 314 | if (err > 0) |
| 315 | err = -EIO; |
Jarkko Sakkinen | ce63c05 | 2018-03-26 15:14:06 +0300 | [diff] [blame] | 316 | goto out; |
Kees Cook | 782779b | 2019-04-01 12:06:07 -0700 | [diff] [blame] | 317 | } |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 318 | |
Jarkko Sakkinen | ce63c05 | 2018-03-26 15:14:06 +0300 | [diff] [blame] | 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) < |
Jarkko Sakkinen | 84b59f6 | 2018-09-03 04:01:26 +0300 | [diff] [blame] | 323 | TPM_HEADER_SIZE + |
| 324 | offsetof(struct tpm2_get_random_out, buffer) + |
| 325 | recd) { |
Jarkko Sakkinen | ce63c05 | 2018-03-26 15:14:06 +0300 | [diff] [blame] | 326 | err = -EFAULT; |
| 327 | goto out; |
| 328 | } |
| 329 | memcpy(dest_ptr, out->buffer, recd); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 330 | |
Jarkko Sakkinen | ce63c05 | 2018-03-26 15:14:06 +0300 | [diff] [blame] | 331 | dest_ptr += recd; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 332 | total += recd; |
| 333 | num_bytes -= recd; |
| 334 | } while (retries-- && total < max); |
| 335 | |
Jarkko Sakkinen | ce63c05 | 2018-03-26 15:14:06 +0300 | [diff] [blame] | 336 | tpm_buf_destroy(&buf); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 337 | return total ? total : -EIO; |
Jarkko Sakkinen | ce63c05 | 2018-03-26 15:14:06 +0300 | [diff] [blame] | 338 | out: |
| 339 | tpm_buf_destroy(&buf); |
| 340 | return err; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 343 | /** |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 344 | * tpm2_flush_context() - execute a TPM2_FlushContext command |
Tomas Winkler | 5122b5f | 2018-10-26 21:40:42 +0300 | [diff] [blame] | 345 | * @chip: TPM chip to use |
| 346 | * @handle: context handle |
Jarkko Sakkinen | 9aa36b3 | 2016-11-26 13:39:35 +0200 | [diff] [blame] | 347 | */ |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 348 | void tpm2_flush_context(struct tpm_chip *chip, u32 handle) |
Jarkko Sakkinen | 9aa36b3 | 2016-11-26 13:39:35 +0200 | [diff] [blame] | 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 | |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 362 | tpm_transmit_cmd(chip, &buf, 0, "flushing context"); |
Jarkko Sakkinen | 9aa36b3 | 2016-11-26 13:39:35 +0200 | [diff] [blame] | 363 | tpm_buf_destroy(&buf); |
| 364 | } |
James Bottomley | 45477b3 | 2019-12-12 12:58:35 -0500 | [diff] [blame] | 365 | EXPORT_SYMBOL_GPL(tpm2_flush_context); |
Jarkko Sakkinen | 9aa36b3 | 2016-11-26 13:39:35 +0200 | [diff] [blame] | 366 | |
Jarkko Sakkinen | 2ab3241 | 2018-03-26 15:14:05 +0300 | [diff] [blame] | 367 | struct 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 | |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 375 | /** |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 376 | * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property |
Jarkko Sakkinen | 2ab3241 | 2018-03-26 15:14:05 +0300 | [diff] [blame] | 377 | * @chip: a &tpm_chip instance |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 378 | * @property_id: property ID. |
| 379 | * @value: output variable. |
| 380 | * @desc: passed to tpm_transmit_cmd() |
| 381 | * |
Jarkko Sakkinen | 2ab3241 | 2018-03-26 15:14:05 +0300 | [diff] [blame] | 382 | * Return: |
| 383 | * 0 on success, |
| 384 | * -errno or a TPM return code otherwise |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 385 | */ |
| 386 | ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value, |
| 387 | const char *desc) |
| 388 | { |
Jarkko Sakkinen | 2ab3241 | 2018-03-26 15:14:05 +0300 | [diff] [blame] | 389 | struct tpm2_get_cap_out *out; |
| 390 | struct tpm_buf buf; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 391 | int rc; |
| 392 | |
Jarkko Sakkinen | 2ab3241 | 2018-03-26 15:14:05 +0300 | [diff] [blame] | 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); |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 399 | rc = tpm_transmit_cmd(chip, &buf, 0, NULL); |
Jarkko Sakkinen | 2ab3241 | 2018-03-26 15:14:05 +0300 | [diff] [blame] | 400 | if (!rc) { |
| 401 | out = (struct tpm2_get_cap_out *) |
| 402 | &buf.data[TPM_HEADER_SIZE]; |
| 403 | *value = be32_to_cpu(out->value); |
| 404 | } |
| 405 | tpm_buf_destroy(&buf); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 406 | return rc; |
| 407 | } |
Jarkko Sakkinen | eb5854e | 2016-06-12 16:42:09 +0300 | [diff] [blame] | 408 | EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 409 | |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 410 | /** |
Jarkko Sakkinen | cc26c6e | 2018-06-05 22:22:58 +0300 | [diff] [blame] | 411 | * tpm2_shutdown() - send a TPM shutdown command |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 412 | * |
Jarkko Sakkinen | cc26c6e | 2018-06-05 22:22:58 +0300 | [diff] [blame] | 413 | * Sends a TPM shutdown command. The shutdown command is used in call |
| 414 | * sites where the system is going down. If it fails, there is not much |
| 415 | * that can be done except print an error message. |
| 416 | * |
| 417 | * @chip: a &tpm_chip instance |
| 418 | * @shutdown_type: TPM_SU_CLEAR or TPM_SU_STATE. |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 419 | */ |
Jarkko Sakkinen | 74d6b3c | 2015-01-29 07:43:47 +0200 | [diff] [blame] | 420 | void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type) |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 421 | { |
Jarkko Sakkinen | cc26c6e | 2018-06-05 22:22:58 +0300 | [diff] [blame] | 422 | struct tpm_buf buf; |
Jarkko Sakkinen | 74d6b3c | 2015-01-29 07:43:47 +0200 | [diff] [blame] | 423 | int rc; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 424 | |
Jarkko Sakkinen | cc26c6e | 2018-06-05 22:22:58 +0300 | [diff] [blame] | 425 | rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SHUTDOWN); |
| 426 | if (rc) |
| 427 | return; |
| 428 | tpm_buf_append_u16(&buf, shutdown_type); |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 429 | tpm_transmit_cmd(chip, &buf, 0, "stopping the TPM"); |
Jarkko Sakkinen | cc26c6e | 2018-06-05 22:22:58 +0300 | [diff] [blame] | 430 | tpm_buf_destroy(&buf); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 431 | } |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 432 | |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 433 | /** |
Alexander Steffen | 2482b1b | 2017-08-31 19:18:56 +0200 | [diff] [blame] | 434 | * tpm2_do_selftest() - ensure that all self tests have passed |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 435 | * |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 436 | * @chip: TPM chip to use |
| 437 | * |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 438 | * Return: Same as with tpm_transmit_cmd. |
| 439 | * |
Alexander Steffen | 125a221 | 2017-08-31 19:18:58 +0200 | [diff] [blame] | 440 | * The TPM can either run all self tests synchronously and then return |
| 441 | * RC_SUCCESS once all tests were successful. Or it can choose to run the tests |
| 442 | * asynchronously and return RC_TESTING immediately while the self tests still |
| 443 | * execute in the background. This function handles both cases and waits until |
| 444 | * all tests have completed. |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 445 | */ |
Jason Gunthorpe | cae8b44 | 2016-07-12 11:41:49 -0600 | [diff] [blame] | 446 | static int tpm2_do_selftest(struct tpm_chip *chip) |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 447 | { |
James Bottomley | 2be8ffe | 2018-03-22 17:32:20 +0200 | [diff] [blame] | 448 | struct tpm_buf buf; |
| 449 | int full; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 450 | int rc; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 451 | |
James Bottomley | 2be8ffe | 2018-03-22 17:32:20 +0200 | [diff] [blame] | 452 | for (full = 0; full < 2; full++) { |
| 453 | rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SELF_TEST); |
| 454 | if (rc) |
| 455 | return rc; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 456 | |
James Bottomley | 2be8ffe | 2018-03-22 17:32:20 +0200 | [diff] [blame] | 457 | tpm_buf_append_u8(&buf, full); |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 458 | rc = tpm_transmit_cmd(chip, &buf, 0, |
James Bottomley | 2be8ffe | 2018-03-22 17:32:20 +0200 | [diff] [blame] | 459 | "attempting the self test"); |
| 460 | tpm_buf_destroy(&buf); |
Alexander Steffen | 125a221 | 2017-08-31 19:18:58 +0200 | [diff] [blame] | 461 | |
James Bottomley | 2be8ffe | 2018-03-22 17:32:20 +0200 | [diff] [blame] | 462 | if (rc == TPM2_RC_TESTING) |
| 463 | rc = TPM2_RC_SUCCESS; |
| 464 | if (rc == TPM2_RC_INITIALIZE || rc == TPM2_RC_SUCCESS) |
| 465 | return rc; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | return rc; |
| 469 | } |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 470 | |
| 471 | /** |
Jarkko Sakkinen | 94e266b | 2018-03-26 15:14:04 +0300 | [diff] [blame] | 472 | * tpm2_probe() - probe for the TPM 2.0 protocol |
| 473 | * @chip: a &tpm_chip instance |
Jarkko Sakkinen | 4d5f205 | 2015-02-04 16:21:09 +0200 | [diff] [blame] | 474 | * |
Jarkko Sakkinen | 94e266b | 2018-03-26 15:14:04 +0300 | [diff] [blame] | 475 | * Send an idempotent TPM 2.0 command and see whether there is TPM2 chip in the |
| 476 | * other end based on the response tag. The flag TPM_CHIP_FLAG_TPM2 is set by |
| 477 | * this function if this is the case. |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 478 | * |
Jarkko Sakkinen | 94e266b | 2018-03-26 15:14:04 +0300 | [diff] [blame] | 479 | * Return: |
| 480 | * 0 on success, |
| 481 | * -errno otherwise |
Jarkko Sakkinen | 4d5f205 | 2015-02-04 16:21:09 +0200 | [diff] [blame] | 482 | */ |
| 483 | int tpm2_probe(struct tpm_chip *chip) |
| 484 | { |
Jarkko Sakkinen | b34b77a | 2018-11-06 19:04:30 +0200 | [diff] [blame] | 485 | struct tpm_header *out; |
Jarkko Sakkinen | 94e266b | 2018-03-26 15:14:04 +0300 | [diff] [blame] | 486 | struct tpm_buf buf; |
Jarkko Sakkinen | 4d5f205 | 2015-02-04 16:21:09 +0200 | [diff] [blame] | 487 | int rc; |
| 488 | |
Jarkko Sakkinen | 94e266b | 2018-03-26 15:14:04 +0300 | [diff] [blame] | 489 | rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY); |
| 490 | if (rc) |
Jarkko Sakkinen | 4d5f205 | 2015-02-04 16:21:09 +0200 | [diff] [blame] | 491 | return rc; |
Jarkko Sakkinen | 94e266b | 2018-03-26 15:14:04 +0300 | [diff] [blame] | 492 | tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES); |
| 493 | tpm_buf_append_u32(&buf, TPM_PT_TOTAL_COMMANDS); |
| 494 | tpm_buf_append_u32(&buf, 1); |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 495 | rc = tpm_transmit_cmd(chip, &buf, 0, NULL); |
Jarkko Sakkinen | 94e266b | 2018-03-26 15:14:04 +0300 | [diff] [blame] | 496 | /* We ignore TPM return codes on purpose. */ |
| 497 | if (rc >= 0) { |
Jarkko Sakkinen | b34b77a | 2018-11-06 19:04:30 +0200 | [diff] [blame] | 498 | out = (struct tpm_header *)buf.data; |
Jarkko Sakkinen | 94e266b | 2018-03-26 15:14:04 +0300 | [diff] [blame] | 499 | if (be16_to_cpu(out->tag) == TPM2_ST_NO_SESSIONS) |
| 500 | chip->flags |= TPM_CHIP_FLAG_TPM2; |
| 501 | } |
| 502 | tpm_buf_destroy(&buf); |
Jarkko Sakkinen | 4d5f205 | 2015-02-04 16:21:09 +0200 | [diff] [blame] | 503 | return 0; |
| 504 | } |
| 505 | EXPORT_SYMBOL_GPL(tpm2_probe); |
Jason Gunthorpe | cae8b44 | 2016-07-12 11:41:49 -0600 | [diff] [blame] | 506 | |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 507 | static int tpm2_init_bank_info(struct tpm_chip *chip, u32 bank_index) |
| 508 | { |
| 509 | struct tpm_bank_info *bank = chip->allocated_banks + bank_index; |
| 510 | struct tpm_digest digest = { .alg_id = bank->alg_id }; |
| 511 | int i; |
| 512 | |
| 513 | /* |
| 514 | * Avoid unnecessary PCR read operations to reduce overhead |
| 515 | * and obtain identifiers of the crypto subsystem. |
| 516 | */ |
| 517 | for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) { |
| 518 | enum hash_algo crypto_algo = tpm2_hash_map[i].crypto_id; |
| 519 | |
| 520 | if (bank->alg_id != tpm2_hash_map[i].tpm_id) |
| 521 | continue; |
| 522 | |
| 523 | bank->digest_size = hash_digest_size[crypto_algo]; |
| 524 | bank->crypto_id = crypto_algo; |
| 525 | return 0; |
| 526 | } |
| 527 | |
Roberto Sassu | dc10e41 | 2020-02-10 11:00:41 +0100 | [diff] [blame] | 528 | bank->crypto_id = HASH_ALGO__LAST; |
| 529 | |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 530 | return tpm2_pcr_read(chip, 0, &digest, &bank->digest_size); |
| 531 | } |
| 532 | |
Nayna Jain | 1db1534 | 2017-01-30 04:59:40 -0500 | [diff] [blame] | 533 | struct tpm2_pcr_selection { |
| 534 | __be16 hash_alg; |
| 535 | u8 size_of_select; |
| 536 | u8 pcr_select[3]; |
| 537 | } __packed; |
| 538 | |
Nayna Jain | fa4f99c | 2019-07-11 12:13:35 -0400 | [diff] [blame] | 539 | ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) |
Nayna Jain | 1db1534 | 2017-01-30 04:59:40 -0500 | [diff] [blame] | 540 | { |
| 541 | struct tpm2_pcr_selection pcr_selection; |
| 542 | struct tpm_buf buf; |
| 543 | void *marker; |
| 544 | void *end; |
| 545 | void *pcr_select_offset; |
Nayna Jain | 1db1534 | 2017-01-30 04:59:40 -0500 | [diff] [blame] | 546 | u32 sizeof_pcr_selection; |
Roberto Sassu | bcfff83 | 2019-02-06 17:24:47 +0100 | [diff] [blame] | 547 | u32 nr_possible_banks; |
| 548 | u32 nr_alloc_banks = 0; |
| 549 | u16 hash_alg; |
Nayna Jain | 1db1534 | 2017-01-30 04:59:40 -0500 | [diff] [blame] | 550 | u32 rsp_len; |
| 551 | int rc; |
| 552 | int i = 0; |
| 553 | |
| 554 | rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY); |
| 555 | if (rc) |
| 556 | return rc; |
| 557 | |
| 558 | tpm_buf_append_u32(&buf, TPM2_CAP_PCRS); |
| 559 | tpm_buf_append_u32(&buf, 0); |
| 560 | tpm_buf_append_u32(&buf, 1); |
| 561 | |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 562 | rc = tpm_transmit_cmd(chip, &buf, 9, "get tpm pcr allocation"); |
Nayna Jain | 1db1534 | 2017-01-30 04:59:40 -0500 | [diff] [blame] | 563 | if (rc) |
| 564 | goto out; |
| 565 | |
Roberto Sassu | bcfff83 | 2019-02-06 17:24:47 +0100 | [diff] [blame] | 566 | nr_possible_banks = be32_to_cpup( |
Nayna Jain | 1db1534 | 2017-01-30 04:59:40 -0500 | [diff] [blame] | 567 | (__be32 *)&buf.data[TPM_HEADER_SIZE + 5]); |
| 568 | |
Roberto Sassu | bcfff83 | 2019-02-06 17:24:47 +0100 | [diff] [blame] | 569 | chip->allocated_banks = kcalloc(nr_possible_banks, |
| 570 | sizeof(*chip->allocated_banks), |
| 571 | GFP_KERNEL); |
| 572 | if (!chip->allocated_banks) { |
| 573 | rc = -ENOMEM; |
Nayna Jain | 1db1534 | 2017-01-30 04:59:40 -0500 | [diff] [blame] | 574 | goto out; |
| 575 | } |
| 576 | |
| 577 | marker = &buf.data[TPM_HEADER_SIZE + 9]; |
| 578 | |
| 579 | rsp_len = be32_to_cpup((__be32 *)&buf.data[2]); |
| 580 | end = &buf.data[rsp_len]; |
| 581 | |
Roberto Sassu | bcfff83 | 2019-02-06 17:24:47 +0100 | [diff] [blame] | 582 | for (i = 0; i < nr_possible_banks; i++) { |
Nayna Jain | 1db1534 | 2017-01-30 04:59:40 -0500 | [diff] [blame] | 583 | pcr_select_offset = marker + |
| 584 | offsetof(struct tpm2_pcr_selection, size_of_select); |
| 585 | if (pcr_select_offset >= end) { |
| 586 | rc = -EFAULT; |
| 587 | break; |
| 588 | } |
| 589 | |
| 590 | memcpy(&pcr_selection, marker, sizeof(pcr_selection)); |
Roberto Sassu | bcfff83 | 2019-02-06 17:24:47 +0100 | [diff] [blame] | 591 | hash_alg = be16_to_cpu(pcr_selection.hash_alg); |
| 592 | |
| 593 | pcr_select_offset = memchr_inv(pcr_selection.pcr_select, 0, |
| 594 | pcr_selection.size_of_select); |
| 595 | if (pcr_select_offset) { |
Roberto Sassu | 879b589 | 2019-02-06 17:24:49 +0100 | [diff] [blame] | 596 | chip->allocated_banks[nr_alloc_banks].alg_id = hash_alg; |
| 597 | |
| 598 | rc = tpm2_init_bank_info(chip, nr_alloc_banks); |
| 599 | if (rc < 0) |
| 600 | break; |
| 601 | |
Roberto Sassu | bcfff83 | 2019-02-06 17:24:47 +0100 | [diff] [blame] | 602 | nr_alloc_banks++; |
| 603 | } |
| 604 | |
Nayna Jain | 1db1534 | 2017-01-30 04:59:40 -0500 | [diff] [blame] | 605 | sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) + |
| 606 | sizeof(pcr_selection.size_of_select) + |
| 607 | pcr_selection.size_of_select; |
| 608 | marker = marker + sizeof_pcr_selection; |
| 609 | } |
| 610 | |
Roberto Sassu | bcfff83 | 2019-02-06 17:24:47 +0100 | [diff] [blame] | 611 | chip->nr_allocated_banks = nr_alloc_banks; |
Nayna Jain | 1db1534 | 2017-01-30 04:59:40 -0500 | [diff] [blame] | 612 | out: |
Nayna Jain | 1db1534 | 2017-01-30 04:59:40 -0500 | [diff] [blame] | 613 | tpm_buf_destroy(&buf); |
| 614 | |
| 615 | return rc; |
| 616 | } |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 617 | |
Stefan Berger | 18b3670 | 2020-03-12 11:53:32 -0400 | [diff] [blame] | 618 | int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip) |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 619 | { |
| 620 | struct tpm_buf buf; |
| 621 | u32 nr_commands; |
Jarkko Sakkinen | 171360d | 2017-09-13 09:58:49 -0700 | [diff] [blame] | 622 | __be32 *attrs; |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 623 | u32 cc; |
| 624 | int i; |
| 625 | int rc; |
| 626 | |
| 627 | rc = tpm2_get_tpm_pt(chip, TPM_PT_TOTAL_COMMANDS, &nr_commands, NULL); |
| 628 | if (rc) |
| 629 | goto out; |
| 630 | |
| 631 | if (nr_commands > 0xFFFFF) { |
| 632 | rc = -EFAULT; |
| 633 | goto out; |
| 634 | } |
| 635 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 636 | chip->cc_attrs_tbl = devm_kcalloc(&chip->dev, 4, nr_commands, |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 637 | GFP_KERNEL); |
Tadeusz Struk | f168911 | 2019-10-07 14:46:37 -0700 | [diff] [blame] | 638 | if (!chip->cc_attrs_tbl) { |
| 639 | rc = -ENOMEM; |
| 640 | goto out; |
| 641 | } |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 642 | |
| 643 | rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY); |
| 644 | if (rc) |
| 645 | goto out; |
| 646 | |
| 647 | tpm_buf_append_u32(&buf, TPM2_CAP_COMMANDS); |
| 648 | tpm_buf_append_u32(&buf, TPM2_CC_FIRST); |
| 649 | tpm_buf_append_u32(&buf, nr_commands); |
| 650 | |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 651 | rc = tpm_transmit_cmd(chip, &buf, 9 + 4 * nr_commands, NULL); |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 652 | if (rc) { |
| 653 | tpm_buf_destroy(&buf); |
| 654 | goto out; |
| 655 | } |
| 656 | |
| 657 | if (nr_commands != |
| 658 | be32_to_cpup((__be32 *)&buf.data[TPM_HEADER_SIZE + 5])) { |
Zhen Lei | 1df83992 | 2021-05-12 21:39:26 +0800 | [diff] [blame] | 659 | rc = -EFAULT; |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 660 | tpm_buf_destroy(&buf); |
| 661 | goto out; |
| 662 | } |
| 663 | |
| 664 | chip->nr_commands = nr_commands; |
| 665 | |
Jarkko Sakkinen | 171360d | 2017-09-13 09:58:49 -0700 | [diff] [blame] | 666 | attrs = (__be32 *)&buf.data[TPM_HEADER_SIZE + 9]; |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 667 | for (i = 0; i < nr_commands; i++, attrs++) { |
| 668 | chip->cc_attrs_tbl[i] = be32_to_cpup(attrs); |
| 669 | cc = chip->cc_attrs_tbl[i] & 0xFFFF; |
| 670 | |
| 671 | if (cc == TPM2_CC_CONTEXT_SAVE || cc == TPM2_CC_FLUSH_CONTEXT) { |
| 672 | chip->cc_attrs_tbl[i] &= |
| 673 | ~(GENMASK(2, 0) << TPM2_CC_ATTR_CHANDLES); |
| 674 | chip->cc_attrs_tbl[i] |= 1 << TPM2_CC_ATTR_CHANDLES; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | tpm_buf_destroy(&buf); |
| 679 | |
| 680 | out: |
| 681 | if (rc > 0) |
| 682 | rc = -ENODEV; |
| 683 | return rc; |
| 684 | } |
Stefan Berger | 684c6bd | 2020-03-18 21:00:17 -0400 | [diff] [blame] | 685 | EXPORT_SYMBOL_GPL(tpm2_get_cc_attrs_tbl); |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 686 | |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 687 | /** |
Tomas Winkler | 9db7fe1 | 2018-10-19 21:22:58 +0300 | [diff] [blame] | 688 | * tpm2_startup - turn on the TPM |
| 689 | * @chip: TPM chip to use |
| 690 | * |
| 691 | * Normally the firmware should start the TPM. This function is provided as a |
| 692 | * workaround if this does not happen. A legal case for this could be for |
| 693 | * example when a TPM emulator is used. |
| 694 | * |
| 695 | * Return: same as tpm_transmit_cmd() |
| 696 | */ |
| 697 | |
| 698 | static int tpm2_startup(struct tpm_chip *chip) |
| 699 | { |
| 700 | struct tpm_buf buf; |
| 701 | int rc; |
| 702 | |
| 703 | dev_info(&chip->dev, "starting up the TPM manually\n"); |
| 704 | |
| 705 | rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_STARTUP); |
| 706 | if (rc < 0) |
| 707 | return rc; |
| 708 | |
| 709 | tpm_buf_append_u16(&buf, TPM2_SU_CLEAR); |
Jarkko Sakkinen | 47a6c28 | 2018-11-05 03:02:38 +0200 | [diff] [blame] | 710 | rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to start the TPM"); |
Tomas Winkler | 9db7fe1 | 2018-10-19 21:22:58 +0300 | [diff] [blame] | 711 | tpm_buf_destroy(&buf); |
| 712 | |
| 713 | return rc; |
| 714 | } |
| 715 | |
| 716 | /** |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 717 | * tpm2_auto_startup - Perform the standard automatic TPM initialization |
| 718 | * sequence |
| 719 | * @chip: TPM chip to use |
| 720 | * |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 721 | * Returns 0 on success, < 0 in case of fatal error. |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 722 | */ |
| 723 | int tpm2_auto_startup(struct tpm_chip *chip) |
| 724 | { |
| 725 | int rc; |
| 726 | |
Tomas Winkler | 9db7fe1 | 2018-10-19 21:22:58 +0300 | [diff] [blame] | 727 | rc = tpm2_get_timeouts(chip); |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 728 | if (rc) |
| 729 | goto out; |
| 730 | |
| 731 | rc = tpm2_do_selftest(chip); |
James Bottomley | 2be8ffe | 2018-03-22 17:32:20 +0200 | [diff] [blame] | 732 | if (rc && rc != TPM2_RC_INITIALIZE) |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 733 | goto out; |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 734 | |
| 735 | if (rc == TPM2_RC_INITIALIZE) { |
Tomas Winkler | 9db7fe1 | 2018-10-19 21:22:58 +0300 | [diff] [blame] | 736 | rc = tpm2_startup(chip); |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 737 | if (rc) |
| 738 | goto out; |
| 739 | |
| 740 | rc = tpm2_do_selftest(chip); |
James Bottomley | 2be8ffe | 2018-03-22 17:32:20 +0200 | [diff] [blame] | 741 | if (rc) |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 742 | goto out; |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 743 | } |
| 744 | |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 745 | rc = tpm2_get_cc_attrs_tbl(chip); |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 746 | |
| 747 | out: |
axelj | 0aa6987 | 2021-12-13 08:09:25 +0100 | [diff] [blame] | 748 | if (rc == TPM2_RC_UPGRADE) { |
| 749 | dev_info(&chip->dev, "TPM in field upgrade mode, requires firmware upgrade\n"); |
| 750 | chip->flags |= TPM_CHIP_FLAG_FIRMWARE_UPGRADE; |
| 751 | rc = 0; |
| 752 | } |
| 753 | |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 754 | if (rc > 0) |
| 755 | rc = -ENODEV; |
| 756 | return rc; |
| 757 | } |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 758 | |
| 759 | int tpm2_find_cc(struct tpm_chip *chip, u32 cc) |
| 760 | { |
| 761 | int i; |
| 762 | |
| 763 | for (i = 0; i < chip->nr_commands; i++) |
| 764 | if (cc == (chip->cc_attrs_tbl[i] & GENMASK(15, 0))) |
| 765 | return i; |
| 766 | |
| 767 | return -1; |
| 768 | } |