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