Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 1 | /* |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 2 | * Copyright (C) 2014, 2015 Intel Corporation |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 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" |
Jarkko Sakkinen | 5ca4c20 | 2015-11-05 21:43:06 +0200 | [diff] [blame] | 19 | #include <crypto/hash_info.h> |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 20 | #include <keys/trusted-type.h> |
| 21 | |
| 22 | enum tpm2_object_attributes { |
Jarkko Sakkinen | c0b5eed | 2016-02-13 11:51:23 +0200 | [diff] [blame] | 23 | TPM2_OA_USER_WITH_AUTH = BIT(6), |
| 24 | }; |
| 25 | |
| 26 | enum tpm2_session_attributes { |
| 27 | TPM2_SA_CONTINUE_SESSION = BIT(0), |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 28 | }; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 29 | |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 30 | struct tpm2_get_tpm_pt_in { |
| 31 | __be32 cap_id; |
| 32 | __be32 property_id; |
| 33 | __be32 property_cnt; |
| 34 | } __packed; |
| 35 | |
| 36 | struct tpm2_get_tpm_pt_out { |
| 37 | u8 more_data; |
| 38 | __be32 subcap_id; |
| 39 | __be32 property_cnt; |
| 40 | __be32 property_id; |
| 41 | __be32 value; |
| 42 | } __packed; |
| 43 | |
| 44 | struct tpm2_get_random_in { |
| 45 | __be16 size; |
| 46 | } __packed; |
| 47 | |
| 48 | struct tpm2_get_random_out { |
| 49 | __be16 size; |
| 50 | u8 buffer[TPM_MAX_RNG_DATA]; |
| 51 | } __packed; |
| 52 | |
| 53 | union tpm2_cmd_params { |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 54 | struct tpm2_get_tpm_pt_in get_tpm_pt_in; |
| 55 | struct tpm2_get_tpm_pt_out get_tpm_pt_out; |
| 56 | struct tpm2_get_random_in getrandom_in; |
| 57 | struct tpm2_get_random_out getrandom_out; |
| 58 | }; |
| 59 | |
| 60 | struct tpm2_cmd { |
| 61 | tpm_cmd_header header; |
| 62 | union tpm2_cmd_params params; |
| 63 | } __packed; |
| 64 | |
Jarkko Sakkinen | 5ca4c20 | 2015-11-05 21:43:06 +0200 | [diff] [blame] | 65 | struct tpm2_hash { |
| 66 | unsigned int crypto_id; |
| 67 | unsigned int tpm_id; |
| 68 | }; |
| 69 | |
| 70 | static struct tpm2_hash tpm2_hash_map[] = { |
| 71 | {HASH_ALGO_SHA1, TPM2_ALG_SHA1}, |
| 72 | {HASH_ALGO_SHA256, TPM2_ALG_SHA256}, |
| 73 | {HASH_ALGO_SHA384, TPM2_ALG_SHA384}, |
| 74 | {HASH_ALGO_SHA512, TPM2_ALG_SHA512}, |
| 75 | {HASH_ALGO_SM3_256, TPM2_ALG_SM3_256}, |
| 76 | }; |
| 77 | |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 78 | /* |
| 79 | * Array with one entry per ordinal defining the maximum amount |
| 80 | * of time the chip could take to return the result. The values |
| 81 | * of the SHORT, MEDIUM, and LONG durations are taken from the |
| 82 | * PC Client Profile (PTP) specification. |
Tomas Winkler | 076d3564 | 2018-03-10 17:15:45 +0200 | [diff] [blame] | 83 | * LONG_LONG is for commands that generates keys which empirically |
| 84 | * takes longer time on some systems. |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 85 | */ |
| 86 | static const u8 tpm2_ordinal_duration[TPM2_CC_LAST - TPM2_CC_FIRST + 1] = { |
| 87 | TPM_UNDEFINED, /* 11F */ |
| 88 | TPM_UNDEFINED, /* 120 */ |
| 89 | TPM_LONG, /* 121 */ |
| 90 | TPM_UNDEFINED, /* 122 */ |
| 91 | TPM_UNDEFINED, /* 123 */ |
| 92 | TPM_UNDEFINED, /* 124 */ |
| 93 | TPM_UNDEFINED, /* 125 */ |
| 94 | TPM_UNDEFINED, /* 126 */ |
| 95 | TPM_UNDEFINED, /* 127 */ |
| 96 | TPM_UNDEFINED, /* 128 */ |
| 97 | TPM_LONG, /* 129 */ |
| 98 | TPM_UNDEFINED, /* 12a */ |
| 99 | TPM_UNDEFINED, /* 12b */ |
| 100 | TPM_UNDEFINED, /* 12c */ |
| 101 | TPM_UNDEFINED, /* 12d */ |
| 102 | TPM_UNDEFINED, /* 12e */ |
| 103 | TPM_UNDEFINED, /* 12f */ |
| 104 | TPM_UNDEFINED, /* 130 */ |
Tomas Winkler | 076d3564 | 2018-03-10 17:15:45 +0200 | [diff] [blame] | 105 | TPM_LONG_LONG, /* 131 */ |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 106 | TPM_UNDEFINED, /* 132 */ |
| 107 | TPM_UNDEFINED, /* 133 */ |
| 108 | TPM_UNDEFINED, /* 134 */ |
| 109 | TPM_UNDEFINED, /* 135 */ |
| 110 | TPM_UNDEFINED, /* 136 */ |
| 111 | TPM_UNDEFINED, /* 137 */ |
| 112 | TPM_UNDEFINED, /* 138 */ |
| 113 | TPM_UNDEFINED, /* 139 */ |
| 114 | TPM_UNDEFINED, /* 13a */ |
| 115 | TPM_UNDEFINED, /* 13b */ |
| 116 | TPM_UNDEFINED, /* 13c */ |
| 117 | TPM_UNDEFINED, /* 13d */ |
| 118 | TPM_MEDIUM, /* 13e */ |
| 119 | TPM_UNDEFINED, /* 13f */ |
| 120 | TPM_UNDEFINED, /* 140 */ |
| 121 | TPM_UNDEFINED, /* 141 */ |
| 122 | TPM_UNDEFINED, /* 142 */ |
| 123 | TPM_LONG, /* 143 */ |
| 124 | TPM_MEDIUM, /* 144 */ |
| 125 | TPM_UNDEFINED, /* 145 */ |
| 126 | TPM_UNDEFINED, /* 146 */ |
| 127 | TPM_UNDEFINED, /* 147 */ |
| 128 | TPM_UNDEFINED, /* 148 */ |
| 129 | TPM_UNDEFINED, /* 149 */ |
| 130 | TPM_UNDEFINED, /* 14a */ |
| 131 | TPM_UNDEFINED, /* 14b */ |
| 132 | TPM_UNDEFINED, /* 14c */ |
| 133 | TPM_UNDEFINED, /* 14d */ |
| 134 | TPM_LONG, /* 14e */ |
| 135 | TPM_UNDEFINED, /* 14f */ |
| 136 | TPM_UNDEFINED, /* 150 */ |
| 137 | TPM_UNDEFINED, /* 151 */ |
| 138 | TPM_UNDEFINED, /* 152 */ |
Tomas Winkler | 076d3564 | 2018-03-10 17:15:45 +0200 | [diff] [blame] | 139 | TPM_LONG_LONG, /* 153 */ |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 140 | TPM_UNDEFINED, /* 154 */ |
| 141 | TPM_UNDEFINED, /* 155 */ |
| 142 | TPM_UNDEFINED, /* 156 */ |
| 143 | TPM_UNDEFINED, /* 157 */ |
| 144 | TPM_UNDEFINED, /* 158 */ |
| 145 | TPM_UNDEFINED, /* 159 */ |
| 146 | TPM_UNDEFINED, /* 15a */ |
| 147 | TPM_UNDEFINED, /* 15b */ |
| 148 | TPM_MEDIUM, /* 15c */ |
| 149 | TPM_UNDEFINED, /* 15d */ |
| 150 | TPM_UNDEFINED, /* 15e */ |
| 151 | TPM_UNDEFINED, /* 15f */ |
| 152 | TPM_UNDEFINED, /* 160 */ |
| 153 | TPM_UNDEFINED, /* 161 */ |
| 154 | TPM_UNDEFINED, /* 162 */ |
| 155 | TPM_UNDEFINED, /* 163 */ |
| 156 | TPM_UNDEFINED, /* 164 */ |
| 157 | TPM_UNDEFINED, /* 165 */ |
| 158 | TPM_UNDEFINED, /* 166 */ |
| 159 | TPM_UNDEFINED, /* 167 */ |
| 160 | TPM_UNDEFINED, /* 168 */ |
| 161 | TPM_UNDEFINED, /* 169 */ |
| 162 | TPM_UNDEFINED, /* 16a */ |
| 163 | TPM_UNDEFINED, /* 16b */ |
| 164 | TPM_UNDEFINED, /* 16c */ |
| 165 | TPM_UNDEFINED, /* 16d */ |
| 166 | TPM_UNDEFINED, /* 16e */ |
| 167 | TPM_UNDEFINED, /* 16f */ |
| 168 | TPM_UNDEFINED, /* 170 */ |
| 169 | TPM_UNDEFINED, /* 171 */ |
| 170 | TPM_UNDEFINED, /* 172 */ |
| 171 | TPM_UNDEFINED, /* 173 */ |
| 172 | TPM_UNDEFINED, /* 174 */ |
| 173 | TPM_UNDEFINED, /* 175 */ |
| 174 | TPM_UNDEFINED, /* 176 */ |
| 175 | TPM_LONG, /* 177 */ |
| 176 | TPM_UNDEFINED, /* 178 */ |
| 177 | TPM_UNDEFINED, /* 179 */ |
| 178 | TPM_MEDIUM, /* 17a */ |
| 179 | TPM_LONG, /* 17b */ |
| 180 | TPM_UNDEFINED, /* 17c */ |
| 181 | TPM_UNDEFINED, /* 17d */ |
| 182 | TPM_UNDEFINED, /* 17e */ |
| 183 | TPM_UNDEFINED, /* 17f */ |
| 184 | TPM_UNDEFINED, /* 180 */ |
| 185 | TPM_UNDEFINED, /* 181 */ |
| 186 | TPM_MEDIUM, /* 182 */ |
| 187 | TPM_UNDEFINED, /* 183 */ |
| 188 | TPM_UNDEFINED, /* 184 */ |
| 189 | TPM_MEDIUM, /* 185 */ |
| 190 | TPM_MEDIUM, /* 186 */ |
| 191 | TPM_UNDEFINED, /* 187 */ |
| 192 | TPM_UNDEFINED, /* 188 */ |
| 193 | TPM_UNDEFINED, /* 189 */ |
| 194 | TPM_UNDEFINED, /* 18a */ |
| 195 | TPM_UNDEFINED, /* 18b */ |
| 196 | TPM_UNDEFINED, /* 18c */ |
| 197 | TPM_UNDEFINED, /* 18d */ |
| 198 | TPM_UNDEFINED, /* 18e */ |
| 199 | TPM_UNDEFINED /* 18f */ |
| 200 | }; |
| 201 | |
Roberto Sassu | 91f7f3d | 2017-06-23 15:41:56 +0200 | [diff] [blame] | 202 | struct tpm2_pcr_read_out { |
| 203 | __be32 update_cnt; |
| 204 | __be32 pcr_selects_cnt; |
| 205 | __be16 hash_alg; |
| 206 | u8 pcr_select_size; |
| 207 | u8 pcr_select[TPM2_PCR_SELECT_MIN]; |
| 208 | __be32 digests_cnt; |
| 209 | __be16 digest_size; |
| 210 | u8 digest[]; |
| 211 | } __packed; |
| 212 | |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 213 | /** |
| 214 | * tpm2_pcr_read() - read a PCR value |
| 215 | * @chip: TPM chip to use. |
| 216 | * @pcr_idx: index of the PCR to read. |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 217 | * @res_buf: buffer to store the resulting hash. |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 218 | * |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 219 | * Return: Same as with tpm_transmit_cmd. |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 220 | */ |
| 221 | int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf) |
| 222 | { |
| 223 | int rc; |
Roberto Sassu | 91f7f3d | 2017-06-23 15:41:56 +0200 | [diff] [blame] | 224 | struct tpm_buf buf; |
| 225 | struct tpm2_pcr_read_out *out; |
| 226 | u8 pcr_select[TPM2_PCR_SELECT_MIN] = {0}; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 227 | |
| 228 | if (pcr_idx >= TPM2_PLATFORM_PCR) |
| 229 | return -EINVAL; |
| 230 | |
Roberto Sassu | 91f7f3d | 2017-06-23 15:41:56 +0200 | [diff] [blame] | 231 | rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_READ); |
| 232 | if (rc) |
| 233 | return rc; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 234 | |
Roberto Sassu | 91f7f3d | 2017-06-23 15:41:56 +0200 | [diff] [blame] | 235 | pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 236 | |
Roberto Sassu | 91f7f3d | 2017-06-23 15:41:56 +0200 | [diff] [blame] | 237 | tpm_buf_append_u32(&buf, 1); |
| 238 | tpm_buf_append_u16(&buf, TPM2_ALG_SHA1); |
| 239 | tpm_buf_append_u8(&buf, TPM2_PCR_SELECT_MIN); |
| 240 | tpm_buf_append(&buf, (const unsigned char *)pcr_select, |
| 241 | sizeof(pcr_select)); |
| 242 | |
| 243 | rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, |
| 244 | res_buf ? "attempting to read a pcr value" : NULL); |
| 245 | if (rc == 0 && res_buf) { |
| 246 | out = (struct tpm2_pcr_read_out *)&buf.data[TPM_HEADER_SIZE]; |
| 247 | memcpy(res_buf, out->digest, SHA1_DIGEST_SIZE); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Roberto Sassu | 91f7f3d | 2017-06-23 15:41:56 +0200 | [diff] [blame] | 250 | tpm_buf_destroy(&buf); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 251 | return rc; |
| 252 | } |
| 253 | |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 254 | struct tpm2_null_auth_area { |
| 255 | __be32 handle; |
| 256 | __be16 nonce_size; |
| 257 | u8 attributes; |
| 258 | __be16 auth_size; |
| 259 | } __packed; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | * tpm2_pcr_extend() - extend a PCR value |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 263 | * |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 264 | * @chip: TPM chip to use. |
| 265 | * @pcr_idx: index of the PCR. |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 266 | * @count: number of digests passed. |
| 267 | * @digests: list of pcr banks and corresponding digest values to extend. |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 268 | * |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 269 | * Return: Same as with tpm_transmit_cmd. |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 270 | */ |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 271 | int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, u32 count, |
| 272 | struct tpm2_digest *digests) |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 273 | { |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 274 | struct tpm_buf buf; |
| 275 | struct tpm2_null_auth_area auth_area; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 276 | int rc; |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 277 | int i; |
| 278 | int j; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 279 | |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 280 | if (count > ARRAY_SIZE(chip->active_banks)) |
| 281 | return -EINVAL; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 282 | |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 283 | rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_EXTEND); |
| 284 | if (rc) |
| 285 | return rc; |
| 286 | |
| 287 | tpm_buf_append_u32(&buf, pcr_idx); |
| 288 | |
| 289 | auth_area.handle = cpu_to_be32(TPM2_RS_PW); |
| 290 | auth_area.nonce_size = 0; |
| 291 | auth_area.attributes = 0; |
| 292 | auth_area.auth_size = 0; |
| 293 | |
| 294 | tpm_buf_append_u32(&buf, sizeof(struct tpm2_null_auth_area)); |
| 295 | tpm_buf_append(&buf, (const unsigned char *)&auth_area, |
| 296 | sizeof(auth_area)); |
| 297 | tpm_buf_append_u32(&buf, count); |
| 298 | |
| 299 | for (i = 0; i < count; i++) { |
| 300 | for (j = 0; j < ARRAY_SIZE(tpm2_hash_map); j++) { |
| 301 | if (digests[i].alg_id != tpm2_hash_map[j].tpm_id) |
| 302 | continue; |
| 303 | tpm_buf_append_u16(&buf, digests[i].alg_id); |
| 304 | tpm_buf_append(&buf, (const unsigned char |
| 305 | *)&digests[i].digest, |
| 306 | hash_digest_size[tpm2_hash_map[j].crypto_id]); |
| 307 | } |
| 308 | } |
| 309 | |
Jarkko Sakkinen | 745b361 | 2017-01-06 14:03:45 +0200 | [diff] [blame] | 310 | rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 311 | "attempting extend a PCR value"); |
| 312 | |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 313 | tpm_buf_destroy(&buf); |
| 314 | |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 315 | return rc; |
| 316 | } |
| 317 | |
Nayna Jain | c1f92b4 | 2017-01-30 04:59:41 -0500 | [diff] [blame] | 318 | |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 319 | #define TPM2_GETRANDOM_IN_SIZE \ |
| 320 | (sizeof(struct tpm_input_header) + \ |
| 321 | sizeof(struct tpm2_get_random_in)) |
| 322 | |
| 323 | static const struct tpm_input_header tpm2_getrandom_header = { |
| 324 | .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS), |
| 325 | .length = cpu_to_be32(TPM2_GETRANDOM_IN_SIZE), |
| 326 | .ordinal = cpu_to_be32(TPM2_CC_GET_RANDOM) |
| 327 | }; |
| 328 | |
| 329 | /** |
| 330 | * tpm2_get_random() - get random bytes from the TPM RNG |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 331 | * |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 332 | * @chip: TPM chip to use |
| 333 | * @out: destination buffer for the random bytes |
| 334 | * @max: the max number of bytes to write to @out |
| 335 | * |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 336 | * Return: |
| 337 | * Size of the output buffer, or -EIO on error. |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 338 | */ |
| 339 | int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max) |
| 340 | { |
| 341 | struct tpm2_cmd cmd; |
Stefan Berger | c659af7 | 2017-01-19 07:19:12 -0500 | [diff] [blame] | 342 | u32 recd, rlength; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 343 | u32 num_bytes; |
| 344 | int err; |
| 345 | int total = 0; |
| 346 | int retries = 5; |
| 347 | u8 *dest = out; |
| 348 | |
| 349 | num_bytes = min_t(u32, max, sizeof(cmd.params.getrandom_out.buffer)); |
| 350 | |
| 351 | if (!out || !num_bytes || |
| 352 | max > sizeof(cmd.params.getrandom_out.buffer)) |
| 353 | return -EINVAL; |
| 354 | |
| 355 | do { |
| 356 | cmd.header.in = tpm2_getrandom_header; |
| 357 | cmd.params.getrandom_in.size = cpu_to_be16(num_bytes); |
| 358 | |
Jarkko Sakkinen | 745b361 | 2017-01-06 14:03:45 +0200 | [diff] [blame] | 359 | err = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), |
Stefan Berger | c659af7 | 2017-01-19 07:19:12 -0500 | [diff] [blame] | 360 | offsetof(struct tpm2_get_random_out, |
| 361 | buffer), |
| 362 | 0, "attempting get random"); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 363 | if (err) |
| 364 | break; |
| 365 | |
| 366 | recd = min_t(u32, be16_to_cpu(cmd.params.getrandom_out.size), |
| 367 | num_bytes); |
Stefan Berger | c659af7 | 2017-01-19 07:19:12 -0500 | [diff] [blame] | 368 | rlength = be32_to_cpu(cmd.header.out.length); |
| 369 | if (rlength < offsetof(struct tpm2_get_random_out, buffer) + |
| 370 | recd) |
| 371 | return -EFAULT; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 372 | memcpy(dest, cmd.params.getrandom_out.buffer, recd); |
| 373 | |
| 374 | dest += recd; |
| 375 | total += recd; |
| 376 | num_bytes -= recd; |
| 377 | } while (retries-- && total < max); |
| 378 | |
| 379 | return total ? total : -EIO; |
| 380 | } |
| 381 | |
| 382 | #define TPM2_GET_TPM_PT_IN_SIZE \ |
| 383 | (sizeof(struct tpm_input_header) + \ |
| 384 | sizeof(struct tpm2_get_tpm_pt_in)) |
| 385 | |
Stefan Berger | c659af7 | 2017-01-19 07:19:12 -0500 | [diff] [blame] | 386 | #define TPM2_GET_TPM_PT_OUT_BODY_SIZE \ |
| 387 | sizeof(struct tpm2_get_tpm_pt_out) |
| 388 | |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 389 | static const struct tpm_input_header tpm2_get_tpm_pt_header = { |
| 390 | .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS), |
| 391 | .length = cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE), |
| 392 | .ordinal = cpu_to_be32(TPM2_CC_GET_CAPABILITY) |
| 393 | }; |
| 394 | |
| 395 | /** |
Jarkko Sakkinen | 9aa36b3 | 2016-11-26 13:39:35 +0200 | [diff] [blame] | 396 | * tpm2_flush_context_cmd() - execute a TPM2_FlushContext command |
| 397 | * @chip: TPM chip to use |
| 398 | * @payload: the key data in clear and encrypted form |
| 399 | * @options: authentication values and other options |
| 400 | * |
| 401 | * Return: same as with tpm_transmit_cmd |
| 402 | */ |
| 403 | void tpm2_flush_context_cmd(struct tpm_chip *chip, u32 handle, |
| 404 | unsigned int flags) |
| 405 | { |
| 406 | struct tpm_buf buf; |
| 407 | int rc; |
| 408 | |
| 409 | rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT); |
| 410 | if (rc) { |
| 411 | dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n", |
| 412 | handle); |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | tpm_buf_append_u32(&buf, handle); |
| 417 | |
Jarkko Sakkinen | 745b361 | 2017-01-06 14:03:45 +0200 | [diff] [blame] | 418 | (void) tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, flags, |
Jarkko Sakkinen | 9aa36b3 | 2016-11-26 13:39:35 +0200 | [diff] [blame] | 419 | "flushing context"); |
| 420 | |
| 421 | tpm_buf_destroy(&buf); |
| 422 | } |
| 423 | |
| 424 | /** |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 425 | * tpm_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer. |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 426 | * |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 427 | * @buf: an allocated tpm_buf instance |
| 428 | * @session_handle: session handle |
| 429 | * @nonce: the session nonce, may be NULL if not used |
| 430 | * @nonce_len: the session nonce length, may be 0 if not used |
| 431 | * @attributes: the session attributes |
| 432 | * @hmac: the session HMAC or password, may be NULL if not used |
| 433 | * @hmac_len: the session HMAC or password length, maybe 0 if not used |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 434 | */ |
| 435 | static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle, |
| 436 | const u8 *nonce, u16 nonce_len, |
| 437 | u8 attributes, |
| 438 | const u8 *hmac, u16 hmac_len) |
| 439 | { |
| 440 | tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len); |
| 441 | tpm_buf_append_u32(buf, session_handle); |
| 442 | tpm_buf_append_u16(buf, nonce_len); |
| 443 | |
| 444 | if (nonce && nonce_len) |
| 445 | tpm_buf_append(buf, nonce, nonce_len); |
| 446 | |
| 447 | tpm_buf_append_u8(buf, attributes); |
| 448 | tpm_buf_append_u16(buf, hmac_len); |
| 449 | |
| 450 | if (hmac && hmac_len) |
| 451 | tpm_buf_append(buf, hmac, hmac_len); |
| 452 | } |
| 453 | |
| 454 | /** |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 455 | * tpm2_seal_trusted() - seal the payload of a trusted key |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 456 | * |
| 457 | * @chip: TPM chip to use |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 458 | * @payload: the key data in clear and encrypted form |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 459 | * @options: authentication values and other options |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 460 | * |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 461 | * Return: < 0 on error and 0 on success. |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 462 | */ |
| 463 | int tpm2_seal_trusted(struct tpm_chip *chip, |
| 464 | struct trusted_key_payload *payload, |
| 465 | struct trusted_key_options *options) |
| 466 | { |
| 467 | unsigned int blob_len; |
| 468 | struct tpm_buf buf; |
Stefan Berger | c659af7 | 2017-01-19 07:19:12 -0500 | [diff] [blame] | 469 | u32 hash, rlength; |
Jarkko Sakkinen | 5ca4c20 | 2015-11-05 21:43:06 +0200 | [diff] [blame] | 470 | int i; |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 471 | int rc; |
| 472 | |
Jarkko Sakkinen | 5ca4c20 | 2015-11-05 21:43:06 +0200 | [diff] [blame] | 473 | for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) { |
| 474 | if (options->hash == tpm2_hash_map[i].crypto_id) { |
| 475 | hash = tpm2_hash_map[i].tpm_id; |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | if (i == ARRAY_SIZE(tpm2_hash_map)) |
| 481 | return -EINVAL; |
| 482 | |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 483 | rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE); |
| 484 | if (rc) |
| 485 | return rc; |
| 486 | |
| 487 | tpm_buf_append_u32(&buf, options->keyhandle); |
| 488 | tpm2_buf_append_auth(&buf, TPM2_RS_PW, |
| 489 | NULL /* nonce */, 0, |
| 490 | 0 /* session_attributes */, |
| 491 | options->keyauth /* hmac */, |
| 492 | TPM_DIGEST_SIZE); |
| 493 | |
| 494 | /* sensitive */ |
Jarkko Sakkinen | 2e31125 | 2015-10-30 14:57:02 +0200 | [diff] [blame] | 495 | tpm_buf_append_u16(&buf, 4 + TPM_DIGEST_SIZE + payload->key_len + 1); |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 496 | |
| 497 | tpm_buf_append_u16(&buf, TPM_DIGEST_SIZE); |
| 498 | tpm_buf_append(&buf, options->blobauth, TPM_DIGEST_SIZE); |
Jarkko Sakkinen | 2e31125 | 2015-10-30 14:57:02 +0200 | [diff] [blame] | 499 | tpm_buf_append_u16(&buf, payload->key_len + 1); |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 500 | tpm_buf_append(&buf, payload->key, payload->key_len); |
Jarkko Sakkinen | 2e31125 | 2015-10-30 14:57:02 +0200 | [diff] [blame] | 501 | tpm_buf_append_u8(&buf, payload->migratable); |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 502 | |
| 503 | /* public */ |
Jarkko Sakkinen | f3c82ad | 2016-01-06 16:43:30 +0200 | [diff] [blame] | 504 | tpm_buf_append_u16(&buf, 14 + options->policydigest_len); |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 505 | tpm_buf_append_u16(&buf, TPM2_ALG_KEYEDHASH); |
Jarkko Sakkinen | 5ca4c20 | 2015-11-05 21:43:06 +0200 | [diff] [blame] | 506 | tpm_buf_append_u16(&buf, hash); |
Jarkko Sakkinen | 5beb0c4 | 2015-10-31 17:53:44 +0200 | [diff] [blame] | 507 | |
| 508 | /* policy */ |
Jarkko Sakkinen | f3c82ad | 2016-01-06 16:43:30 +0200 | [diff] [blame] | 509 | if (options->policydigest_len) { |
Jarkko Sakkinen | 5beb0c4 | 2015-10-31 17:53:44 +0200 | [diff] [blame] | 510 | tpm_buf_append_u32(&buf, 0); |
Jarkko Sakkinen | f3c82ad | 2016-01-06 16:43:30 +0200 | [diff] [blame] | 511 | tpm_buf_append_u16(&buf, options->policydigest_len); |
Jarkko Sakkinen | 5beb0c4 | 2015-10-31 17:53:44 +0200 | [diff] [blame] | 512 | tpm_buf_append(&buf, options->policydigest, |
Jarkko Sakkinen | f3c82ad | 2016-01-06 16:43:30 +0200 | [diff] [blame] | 513 | options->policydigest_len); |
Jarkko Sakkinen | 5beb0c4 | 2015-10-31 17:53:44 +0200 | [diff] [blame] | 514 | } else { |
Jarkko Sakkinen | c0b5eed | 2016-02-13 11:51:23 +0200 | [diff] [blame] | 515 | tpm_buf_append_u32(&buf, TPM2_OA_USER_WITH_AUTH); |
Jarkko Sakkinen | 5beb0c4 | 2015-10-31 17:53:44 +0200 | [diff] [blame] | 516 | tpm_buf_append_u16(&buf, 0); |
| 517 | } |
| 518 | |
| 519 | /* public parameters */ |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 520 | tpm_buf_append_u16(&buf, TPM2_ALG_NULL); |
| 521 | tpm_buf_append_u16(&buf, 0); |
| 522 | |
| 523 | /* outside info */ |
| 524 | tpm_buf_append_u16(&buf, 0); |
| 525 | |
| 526 | /* creation PCR */ |
| 527 | tpm_buf_append_u32(&buf, 0); |
| 528 | |
| 529 | if (buf.flags & TPM_BUF_OVERFLOW) { |
| 530 | rc = -E2BIG; |
| 531 | goto out; |
| 532 | } |
| 533 | |
Jarkko Sakkinen | 745b361 | 2017-01-06 14:03:45 +0200 | [diff] [blame] | 534 | rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, 0, |
Stefan Berger | c659af7 | 2017-01-19 07:19:12 -0500 | [diff] [blame] | 535 | "sealing data"); |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 536 | if (rc) |
| 537 | goto out; |
| 538 | |
| 539 | blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]); |
| 540 | if (blob_len > MAX_BLOB_SIZE) { |
| 541 | rc = -E2BIG; |
| 542 | goto out; |
| 543 | } |
Stefan Berger | c659af7 | 2017-01-19 07:19:12 -0500 | [diff] [blame] | 544 | rlength = be32_to_cpu(((struct tpm2_cmd *)&buf)->header.out.length); |
| 545 | if (rlength < TPM_HEADER_SIZE + 4 + blob_len) { |
| 546 | rc = -EFAULT; |
| 547 | goto out; |
| 548 | } |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 549 | |
| 550 | memcpy(payload->blob, &buf.data[TPM_HEADER_SIZE + 4], blob_len); |
| 551 | payload->blob_len = blob_len; |
| 552 | |
| 553 | out: |
| 554 | tpm_buf_destroy(&buf); |
| 555 | |
Jarkko Sakkinen | 5ca4c20 | 2015-11-05 21:43:06 +0200 | [diff] [blame] | 556 | if (rc > 0) { |
Jarkko Sakkinen | 7d76111 | 2017-01-25 23:00:22 +0200 | [diff] [blame] | 557 | if (tpm2_rc_value(rc) == TPM2_RC_HASH) |
Jarkko Sakkinen | 5ca4c20 | 2015-11-05 21:43:06 +0200 | [diff] [blame] | 558 | rc = -EINVAL; |
| 559 | else |
| 560 | rc = -EPERM; |
| 561 | } |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 562 | |
| 563 | return rc; |
| 564 | } |
| 565 | |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 566 | /** |
| 567 | * tpm2_load_cmd() - execute a TPM2_Load command |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 568 | * |
| 569 | * @chip: TPM chip to use |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 570 | * @payload: the key data in clear and encrypted form |
| 571 | * @options: authentication values and other options |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 572 | * @blob_handle: returned blob handle |
| 573 | * @flags: tpm transmit flags |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 574 | * |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 575 | * Return: 0 on success. |
| 576 | * -E2BIG on wrong payload size. |
| 577 | * -EPERM on tpm error status. |
| 578 | * < 0 error from tpm_transmit_cmd. |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 579 | */ |
| 580 | static int tpm2_load_cmd(struct tpm_chip *chip, |
| 581 | struct trusted_key_payload *payload, |
| 582 | struct trusted_key_options *options, |
| 583 | u32 *blob_handle, unsigned int flags) |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 584 | { |
| 585 | struct tpm_buf buf; |
| 586 | unsigned int private_len; |
| 587 | unsigned int public_len; |
| 588 | unsigned int blob_len; |
| 589 | int rc; |
| 590 | |
| 591 | private_len = be16_to_cpup((__be16 *) &payload->blob[0]); |
| 592 | if (private_len > (payload->blob_len - 2)) |
| 593 | return -E2BIG; |
| 594 | |
| 595 | public_len = be16_to_cpup((__be16 *) &payload->blob[2 + private_len]); |
| 596 | blob_len = private_len + public_len + 4; |
| 597 | if (blob_len > payload->blob_len) |
| 598 | return -E2BIG; |
| 599 | |
| 600 | rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD); |
| 601 | if (rc) |
| 602 | return rc; |
| 603 | |
| 604 | tpm_buf_append_u32(&buf, options->keyhandle); |
| 605 | tpm2_buf_append_auth(&buf, TPM2_RS_PW, |
| 606 | NULL /* nonce */, 0, |
| 607 | 0 /* session_attributes */, |
| 608 | options->keyauth /* hmac */, |
| 609 | TPM_DIGEST_SIZE); |
| 610 | |
| 611 | tpm_buf_append(&buf, payload->blob, blob_len); |
| 612 | |
| 613 | if (buf.flags & TPM_BUF_OVERFLOW) { |
| 614 | rc = -E2BIG; |
| 615 | goto out; |
| 616 | } |
| 617 | |
Jarkko Sakkinen | 745b361 | 2017-01-06 14:03:45 +0200 | [diff] [blame] | 618 | rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 4, flags, |
Stefan Berger | c659af7 | 2017-01-19 07:19:12 -0500 | [diff] [blame] | 619 | "loading blob"); |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 620 | if (!rc) |
| 621 | *blob_handle = be32_to_cpup( |
| 622 | (__be32 *) &buf.data[TPM_HEADER_SIZE]); |
| 623 | |
| 624 | out: |
| 625 | tpm_buf_destroy(&buf); |
| 626 | |
| 627 | if (rc > 0) |
| 628 | rc = -EPERM; |
| 629 | |
| 630 | return rc; |
| 631 | } |
| 632 | |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 633 | /** |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 634 | * tpm2_unseal_cmd() - execute a TPM2_Unload command |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 635 | * |
| 636 | * @chip: TPM chip to use |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 637 | * @payload: the key data in clear and encrypted form |
| 638 | * @options: authentication values and other options |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 639 | * @blob_handle: blob handle |
| 640 | * @flags: tpm_transmit_cmd flags |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 641 | * |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 642 | * Return: 0 on success |
| 643 | * -EPERM on tpm error status |
| 644 | * < 0 error from tpm_transmit_cmd |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 645 | */ |
| 646 | static int tpm2_unseal_cmd(struct tpm_chip *chip, |
| 647 | struct trusted_key_payload *payload, |
| 648 | struct trusted_key_options *options, |
| 649 | u32 blob_handle, unsigned int flags) |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 650 | { |
| 651 | struct tpm_buf buf; |
Jarkko Sakkinen | 2e31125 | 2015-10-30 14:57:02 +0200 | [diff] [blame] | 652 | u16 data_len; |
| 653 | u8 *data; |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 654 | int rc; |
Stefan Berger | c659af7 | 2017-01-19 07:19:12 -0500 | [diff] [blame] | 655 | u32 rlength; |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 656 | |
| 657 | rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL); |
| 658 | if (rc) |
| 659 | return rc; |
| 660 | |
| 661 | tpm_buf_append_u32(&buf, blob_handle); |
Jarkko Sakkinen | 5beb0c4 | 2015-10-31 17:53:44 +0200 | [diff] [blame] | 662 | tpm2_buf_append_auth(&buf, |
| 663 | options->policyhandle ? |
| 664 | options->policyhandle : TPM2_RS_PW, |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 665 | NULL /* nonce */, 0, |
Jarkko Sakkinen | c0b5eed | 2016-02-13 11:51:23 +0200 | [diff] [blame] | 666 | TPM2_SA_CONTINUE_SESSION, |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 667 | options->blobauth /* hmac */, |
| 668 | TPM_DIGEST_SIZE); |
| 669 | |
Jarkko Sakkinen | 745b361 | 2017-01-06 14:03:45 +0200 | [diff] [blame] | 670 | rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 6, flags, |
Stefan Berger | c659af7 | 2017-01-19 07:19:12 -0500 | [diff] [blame] | 671 | "unsealing"); |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 672 | if (rc > 0) |
| 673 | rc = -EPERM; |
| 674 | |
| 675 | if (!rc) { |
Jarkko Sakkinen | 2e31125 | 2015-10-30 14:57:02 +0200 | [diff] [blame] | 676 | data_len = be16_to_cpup( |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 677 | (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]); |
Jeremy Boone | 3be2327 | 2018-02-08 12:28:08 -0800 | [diff] [blame] | 678 | if (data_len < MIN_KEY_SIZE || data_len > MAX_KEY_SIZE + 1) { |
| 679 | rc = -EFAULT; |
| 680 | goto out; |
| 681 | } |
Stefan Berger | c659af7 | 2017-01-19 07:19:12 -0500 | [diff] [blame] | 682 | |
| 683 | rlength = be32_to_cpu(((struct tpm2_cmd *)&buf) |
| 684 | ->header.out.length); |
| 685 | if (rlength < TPM_HEADER_SIZE + 6 + data_len) { |
| 686 | rc = -EFAULT; |
| 687 | goto out; |
| 688 | } |
Jarkko Sakkinen | 2e31125 | 2015-10-30 14:57:02 +0200 | [diff] [blame] | 689 | data = &buf.data[TPM_HEADER_SIZE + 6]; |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 690 | |
Jarkko Sakkinen | 2e31125 | 2015-10-30 14:57:02 +0200 | [diff] [blame] | 691 | memcpy(payload->key, data, data_len - 1); |
| 692 | payload->key_len = data_len - 1; |
| 693 | payload->migratable = data[data_len - 1]; |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 694 | } |
| 695 | |
Stefan Berger | c659af7 | 2017-01-19 07:19:12 -0500 | [diff] [blame] | 696 | out: |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 697 | tpm_buf_destroy(&buf); |
| 698 | return rc; |
| 699 | } |
| 700 | |
| 701 | /** |
Baruch Siach | cbef69a | 2016-11-06 11:02:45 +0200 | [diff] [blame] | 702 | * tpm2_unseal_trusted() - unseal the payload of a trusted key |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 703 | * |
| 704 | * @chip: TPM chip to use |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 705 | * @payload: the key data in clear and encrypted form |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 706 | * @options: authentication values and other options |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 707 | * |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 708 | * Return: Same as with tpm_transmit_cmd. |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 709 | */ |
| 710 | int tpm2_unseal_trusted(struct tpm_chip *chip, |
| 711 | struct trusted_key_payload *payload, |
| 712 | struct trusted_key_options *options) |
| 713 | { |
| 714 | u32 blob_handle; |
| 715 | int rc; |
| 716 | |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 717 | mutex_lock(&chip->tpm_mutex); |
| 718 | rc = tpm2_load_cmd(chip, payload, options, &blob_handle, |
| 719 | TPM_TRANSMIT_UNLOCKED); |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 720 | if (rc) |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 721 | goto out; |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 722 | |
Jarkko Sakkinen | d4816ed | 2016-08-16 22:00:38 +0300 | [diff] [blame] | 723 | rc = tpm2_unseal_cmd(chip, payload, options, blob_handle, |
| 724 | TPM_TRANSMIT_UNLOCKED); |
| 725 | tpm2_flush_context_cmd(chip, blob_handle, TPM_TRANSMIT_UNLOCKED); |
| 726 | out: |
| 727 | mutex_unlock(&chip->tpm_mutex); |
Jarkko Sakkinen | 954650e | 2015-05-30 08:09:04 +0300 | [diff] [blame] | 728 | return rc; |
| 729 | } |
| 730 | |
| 731 | /** |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 732 | * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property |
| 733 | * @chip: TPM chip to use. |
| 734 | * @property_id: property ID. |
| 735 | * @value: output variable. |
| 736 | * @desc: passed to tpm_transmit_cmd() |
| 737 | * |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 738 | * Return: Same as with tpm_transmit_cmd. |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 739 | */ |
| 740 | ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value, |
| 741 | const char *desc) |
| 742 | { |
| 743 | struct tpm2_cmd cmd; |
| 744 | int rc; |
| 745 | |
| 746 | cmd.header.in = tpm2_get_tpm_pt_header; |
| 747 | cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES); |
| 748 | cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id); |
| 749 | cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1); |
| 750 | |
Jarkko Sakkinen | 745b361 | 2017-01-06 14:03:45 +0200 | [diff] [blame] | 751 | rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), |
Stefan Berger | c659af7 | 2017-01-19 07:19:12 -0500 | [diff] [blame] | 752 | TPM2_GET_TPM_PT_OUT_BODY_SIZE, 0, desc); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 753 | if (!rc) |
apronin@chromium.org | 1b0612b | 2016-07-14 18:07:18 -0700 | [diff] [blame] | 754 | *value = be32_to_cpu(cmd.params.get_tpm_pt_out.value); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 755 | |
| 756 | return rc; |
| 757 | } |
Jarkko Sakkinen | eb5854e | 2016-06-12 16:42:09 +0300 | [diff] [blame] | 758 | EXPORT_SYMBOL_GPL(tpm2_get_tpm_pt); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 759 | |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 760 | /** |
Jarkko Sakkinen | cc26c6e | 2018-06-05 22:22:58 +0300 | [diff] [blame^] | 761 | * tpm2_shutdown() - send a TPM shutdown command |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 762 | * |
Jarkko Sakkinen | cc26c6e | 2018-06-05 22:22:58 +0300 | [diff] [blame^] | 763 | * Sends a TPM shutdown command. The shutdown command is used in call |
| 764 | * sites where the system is going down. If it fails, there is not much |
| 765 | * that can be done except print an error message. |
| 766 | * |
| 767 | * @chip: a &tpm_chip instance |
| 768 | * @shutdown_type: TPM_SU_CLEAR or TPM_SU_STATE. |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 769 | */ |
Jarkko Sakkinen | 74d6b3c | 2015-01-29 07:43:47 +0200 | [diff] [blame] | 770 | void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type) |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 771 | { |
Jarkko Sakkinen | cc26c6e | 2018-06-05 22:22:58 +0300 | [diff] [blame^] | 772 | struct tpm_buf buf; |
Jarkko Sakkinen | 74d6b3c | 2015-01-29 07:43:47 +0200 | [diff] [blame] | 773 | int rc; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 774 | |
Jarkko Sakkinen | cc26c6e | 2018-06-05 22:22:58 +0300 | [diff] [blame^] | 775 | rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SHUTDOWN); |
| 776 | if (rc) |
| 777 | return; |
| 778 | tpm_buf_append_u16(&buf, shutdown_type); |
| 779 | tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, |
| 780 | "stopping the TPM"); |
| 781 | tpm_buf_destroy(&buf); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 782 | } |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 783 | |
| 784 | /* |
| 785 | * tpm2_calc_ordinal_duration() - maximum duration for a command |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 786 | * |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 787 | * @chip: TPM chip to use. |
| 788 | * @ordinal: command code number. |
| 789 | * |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 790 | * Return: maximum duration for a command |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 791 | */ |
| 792 | unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal) |
| 793 | { |
| 794 | int index = TPM_UNDEFINED; |
| 795 | int duration = 0; |
| 796 | |
| 797 | if (ordinal >= TPM2_CC_FIRST && ordinal <= TPM2_CC_LAST) |
| 798 | index = tpm2_ordinal_duration[ordinal - TPM2_CC_FIRST]; |
| 799 | |
| 800 | if (index != TPM_UNDEFINED) |
Christophe Ricard | af782f3 | 2016-03-31 22:56:59 +0200 | [diff] [blame] | 801 | duration = chip->duration[index]; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 802 | |
| 803 | if (duration <= 0) |
Tomas Winkler | 076d3564 | 2018-03-10 17:15:45 +0200 | [diff] [blame] | 804 | duration = msecs_to_jiffies(TPM2_DURATION_DEFAULT); |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 805 | |
| 806 | return duration; |
| 807 | } |
| 808 | EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration); |
| 809 | |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 810 | /** |
Alexander Steffen | 2482b1b | 2017-08-31 19:18:56 +0200 | [diff] [blame] | 811 | * tpm2_do_selftest() - ensure that all self tests have passed |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 812 | * |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 813 | * @chip: TPM chip to use |
| 814 | * |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 815 | * Return: Same as with tpm_transmit_cmd. |
| 816 | * |
Alexander Steffen | 125a221 | 2017-08-31 19:18:58 +0200 | [diff] [blame] | 817 | * The TPM can either run all self tests synchronously and then return |
| 818 | * RC_SUCCESS once all tests were successful. Or it can choose to run the tests |
| 819 | * asynchronously and return RC_TESTING immediately while the self tests still |
| 820 | * execute in the background. This function handles both cases and waits until |
| 821 | * all tests have completed. |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 822 | */ |
Jason Gunthorpe | cae8b44 | 2016-07-12 11:41:49 -0600 | [diff] [blame] | 823 | static int tpm2_do_selftest(struct tpm_chip *chip) |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 824 | { |
James Bottomley | 2be8ffe | 2018-03-22 17:32:20 +0200 | [diff] [blame] | 825 | struct tpm_buf buf; |
| 826 | int full; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 827 | int rc; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 828 | |
James Bottomley | 2be8ffe | 2018-03-22 17:32:20 +0200 | [diff] [blame] | 829 | for (full = 0; full < 2; full++) { |
| 830 | rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SELF_TEST); |
| 831 | if (rc) |
| 832 | return rc; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 833 | |
James Bottomley | 2be8ffe | 2018-03-22 17:32:20 +0200 | [diff] [blame] | 834 | tpm_buf_append_u8(&buf, full); |
| 835 | rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 0, 0, |
| 836 | "attempting the self test"); |
| 837 | tpm_buf_destroy(&buf); |
Alexander Steffen | 125a221 | 2017-08-31 19:18:58 +0200 | [diff] [blame] | 838 | |
James Bottomley | 2be8ffe | 2018-03-22 17:32:20 +0200 | [diff] [blame] | 839 | if (rc == TPM2_RC_TESTING) |
| 840 | rc = TPM2_RC_SUCCESS; |
| 841 | if (rc == TPM2_RC_INITIALIZE || rc == TPM2_RC_SUCCESS) |
| 842 | return rc; |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | return rc; |
| 846 | } |
Jarkko Sakkinen | 7a1d7e6 | 2014-12-12 11:46:38 -0800 | [diff] [blame] | 847 | |
| 848 | /** |
Jarkko Sakkinen | 4d5f205 | 2015-02-04 16:21:09 +0200 | [diff] [blame] | 849 | * tpm2_probe() - probe TPM 2.0 |
| 850 | * @chip: TPM chip to use |
| 851 | * |
Winkler, Tomas | 794c6e1 | 2016-11-23 12:04:12 +0200 | [diff] [blame] | 852 | * Return: < 0 error and 0 on success. |
| 853 | * |
Jarkko Sakkinen | 4d5f205 | 2015-02-04 16:21:09 +0200 | [diff] [blame] | 854 | * Send idempotent TPM 2.0 command and see whether TPM 2.0 chip replied based on |
| 855 | * the reply tag. |
| 856 | */ |
| 857 | int tpm2_probe(struct tpm_chip *chip) |
| 858 | { |
| 859 | struct tpm2_cmd cmd; |
| 860 | int rc; |
| 861 | |
| 862 | cmd.header.in = tpm2_get_tpm_pt_header; |
| 863 | cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES); |
| 864 | cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100); |
| 865 | cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1); |
| 866 | |
Jarkko Sakkinen | 745b361 | 2017-01-06 14:03:45 +0200 | [diff] [blame] | 867 | rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, 0, NULL); |
Jarkko Sakkinen | 4d5f205 | 2015-02-04 16:21:09 +0200 | [diff] [blame] | 868 | if (rc < 0) |
| 869 | return rc; |
Jarkko Sakkinen | 4d5f205 | 2015-02-04 16:21:09 +0200 | [diff] [blame] | 870 | |
| 871 | if (be16_to_cpu(cmd.header.out.tag) == TPM2_ST_NO_SESSIONS) |
| 872 | chip->flags |= TPM_CHIP_FLAG_TPM2; |
| 873 | |
| 874 | return 0; |
| 875 | } |
| 876 | EXPORT_SYMBOL_GPL(tpm2_probe); |
Jason Gunthorpe | cae8b44 | 2016-07-12 11:41:49 -0600 | [diff] [blame] | 877 | |
Nayna Jain | 1db1534 | 2017-01-30 04:59:40 -0500 | [diff] [blame] | 878 | struct tpm2_pcr_selection { |
| 879 | __be16 hash_alg; |
| 880 | u8 size_of_select; |
| 881 | u8 pcr_select[3]; |
| 882 | } __packed; |
| 883 | |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 884 | static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) |
Nayna Jain | 1db1534 | 2017-01-30 04:59:40 -0500 | [diff] [blame] | 885 | { |
| 886 | struct tpm2_pcr_selection pcr_selection; |
| 887 | struct tpm_buf buf; |
| 888 | void *marker; |
| 889 | void *end; |
| 890 | void *pcr_select_offset; |
| 891 | unsigned int count; |
| 892 | u32 sizeof_pcr_selection; |
| 893 | u32 rsp_len; |
| 894 | int rc; |
| 895 | int i = 0; |
| 896 | |
| 897 | rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY); |
| 898 | if (rc) |
| 899 | return rc; |
| 900 | |
| 901 | tpm_buf_append_u32(&buf, TPM2_CAP_PCRS); |
| 902 | tpm_buf_append_u32(&buf, 0); |
| 903 | tpm_buf_append_u32(&buf, 1); |
| 904 | |
Jarkko Sakkinen | 745b361 | 2017-01-06 14:03:45 +0200 | [diff] [blame] | 905 | rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, 9, 0, |
Nayna Jain | 1db1534 | 2017-01-30 04:59:40 -0500 | [diff] [blame] | 906 | "get tpm pcr allocation"); |
| 907 | if (rc) |
| 908 | goto out; |
| 909 | |
| 910 | count = be32_to_cpup( |
| 911 | (__be32 *)&buf.data[TPM_HEADER_SIZE + 5]); |
| 912 | |
| 913 | if (count > ARRAY_SIZE(chip->active_banks)) { |
| 914 | rc = -ENODEV; |
| 915 | goto out; |
| 916 | } |
| 917 | |
| 918 | marker = &buf.data[TPM_HEADER_SIZE + 9]; |
| 919 | |
| 920 | rsp_len = be32_to_cpup((__be32 *)&buf.data[2]); |
| 921 | end = &buf.data[rsp_len]; |
| 922 | |
| 923 | for (i = 0; i < count; i++) { |
| 924 | pcr_select_offset = marker + |
| 925 | offsetof(struct tpm2_pcr_selection, size_of_select); |
| 926 | if (pcr_select_offset >= end) { |
| 927 | rc = -EFAULT; |
| 928 | break; |
| 929 | } |
| 930 | |
| 931 | memcpy(&pcr_selection, marker, sizeof(pcr_selection)); |
| 932 | chip->active_banks[i] = be16_to_cpu(pcr_selection.hash_alg); |
| 933 | sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) + |
| 934 | sizeof(pcr_selection.size_of_select) + |
| 935 | pcr_selection.size_of_select; |
| 936 | marker = marker + sizeof_pcr_selection; |
| 937 | } |
| 938 | |
| 939 | out: |
| 940 | if (i < ARRAY_SIZE(chip->active_banks)) |
| 941 | chip->active_banks[i] = TPM2_ALG_ERROR; |
| 942 | |
| 943 | tpm_buf_destroy(&buf); |
| 944 | |
| 945 | return rc; |
| 946 | } |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 947 | |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 948 | static int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip) |
| 949 | { |
| 950 | struct tpm_buf buf; |
| 951 | u32 nr_commands; |
Jarkko Sakkinen | 171360d | 2017-09-13 09:58:49 -0700 | [diff] [blame] | 952 | __be32 *attrs; |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 953 | u32 cc; |
| 954 | int i; |
| 955 | int rc; |
| 956 | |
| 957 | rc = tpm2_get_tpm_pt(chip, TPM_PT_TOTAL_COMMANDS, &nr_commands, NULL); |
| 958 | if (rc) |
| 959 | goto out; |
| 960 | |
| 961 | if (nr_commands > 0xFFFFF) { |
| 962 | rc = -EFAULT; |
| 963 | goto out; |
| 964 | } |
| 965 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 966 | chip->cc_attrs_tbl = devm_kcalloc(&chip->dev, 4, nr_commands, |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 967 | GFP_KERNEL); |
| 968 | |
| 969 | rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY); |
| 970 | if (rc) |
| 971 | goto out; |
| 972 | |
| 973 | tpm_buf_append_u32(&buf, TPM2_CAP_COMMANDS); |
| 974 | tpm_buf_append_u32(&buf, TPM2_CC_FIRST); |
| 975 | tpm_buf_append_u32(&buf, nr_commands); |
| 976 | |
Jarkko Sakkinen | 745b361 | 2017-01-06 14:03:45 +0200 | [diff] [blame] | 977 | rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, |
| 978 | 9 + 4 * nr_commands, 0, NULL); |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 979 | if (rc) { |
| 980 | tpm_buf_destroy(&buf); |
| 981 | goto out; |
| 982 | } |
| 983 | |
| 984 | if (nr_commands != |
| 985 | be32_to_cpup((__be32 *)&buf.data[TPM_HEADER_SIZE + 5])) { |
| 986 | tpm_buf_destroy(&buf); |
| 987 | goto out; |
| 988 | } |
| 989 | |
| 990 | chip->nr_commands = nr_commands; |
| 991 | |
Jarkko Sakkinen | 171360d | 2017-09-13 09:58:49 -0700 | [diff] [blame] | 992 | attrs = (__be32 *)&buf.data[TPM_HEADER_SIZE + 9]; |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 993 | for (i = 0; i < nr_commands; i++, attrs++) { |
| 994 | chip->cc_attrs_tbl[i] = be32_to_cpup(attrs); |
| 995 | cc = chip->cc_attrs_tbl[i] & 0xFFFF; |
| 996 | |
| 997 | if (cc == TPM2_CC_CONTEXT_SAVE || cc == TPM2_CC_FLUSH_CONTEXT) { |
| 998 | chip->cc_attrs_tbl[i] &= |
| 999 | ~(GENMASK(2, 0) << TPM2_CC_ATTR_CHANDLES); |
| 1000 | chip->cc_attrs_tbl[i] |= 1 << TPM2_CC_ATTR_CHANDLES; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | tpm_buf_destroy(&buf); |
| 1005 | |
| 1006 | out: |
| 1007 | if (rc > 0) |
| 1008 | rc = -ENODEV; |
| 1009 | return rc; |
| 1010 | } |
| 1011 | |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 1012 | /** |
| 1013 | * tpm2_auto_startup - Perform the standard automatic TPM initialization |
| 1014 | * sequence |
| 1015 | * @chip: TPM chip to use |
| 1016 | * |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 1017 | * Returns 0 on success, < 0 in case of fatal error. |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 1018 | */ |
| 1019 | int tpm2_auto_startup(struct tpm_chip *chip) |
| 1020 | { |
| 1021 | int rc; |
| 1022 | |
| 1023 | rc = tpm_get_timeouts(chip); |
| 1024 | if (rc) |
| 1025 | goto out; |
| 1026 | |
| 1027 | rc = tpm2_do_selftest(chip); |
James Bottomley | 2be8ffe | 2018-03-22 17:32:20 +0200 | [diff] [blame] | 1028 | if (rc && rc != TPM2_RC_INITIALIZE) |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 1029 | goto out; |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 1030 | |
| 1031 | if (rc == TPM2_RC_INITIALIZE) { |
Jarkko Sakkinen | 19cbe4f | 2017-06-21 09:31:34 +0200 | [diff] [blame] | 1032 | rc = tpm_startup(chip); |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 1033 | if (rc) |
| 1034 | goto out; |
| 1035 | |
| 1036 | rc = tpm2_do_selftest(chip); |
James Bottomley | 2be8ffe | 2018-03-22 17:32:20 +0200 | [diff] [blame] | 1037 | if (rc) |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 1038 | goto out; |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | rc = tpm2_get_pcr_allocation(chip); |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 1042 | if (rc) |
| 1043 | goto out; |
| 1044 | |
| 1045 | rc = tpm2_get_cc_attrs_tbl(chip); |
Jarkko Sakkinen | 61841be | 2017-02-15 20:02:28 +0200 | [diff] [blame] | 1046 | |
| 1047 | out: |
| 1048 | if (rc > 0) |
| 1049 | rc = -ENODEV; |
| 1050 | return rc; |
| 1051 | } |
Jarkko Sakkinen | 58472f5 | 2016-11-10 20:42:07 -0800 | [diff] [blame] | 1052 | |
| 1053 | int tpm2_find_cc(struct tpm_chip *chip, u32 cc) |
| 1054 | { |
| 1055 | int i; |
| 1056 | |
| 1057 | for (i = 0; i < chip->nr_commands; i++) |
| 1058 | if (cc == (chip->cc_attrs_tbl[i] & GENMASK(15, 0))) |
| 1059 | return i; |
| 1060 | |
| 1061 | return -1; |
| 1062 | } |