Move tpm_buf code to common include/linux/tpm.h header so that it can be reused via other subsystems like trusted keys etc. Also rename trusted keys and asymmetric keys usage of TPM 1.x buffer implementation to tpm1_buf to avoid any compilation errors.
Showing the changes to include/linux (1 of 5 files). Show all
Unified · Split
| @@ -21,6 +21,7 @@ · 20 unmodified lines | |||
| 21 | #include <linux/acpi.h> | 21 | #include <linux/acpi.h> |
| 22 | #include <linux/cdev.h> | 22 | #include <linux/cdev.h> |
| 23 | #include <linux/fs.h> | 23 | #include <linux/fs.h> |
| 24 | #include <linux/highmem.h> | ||
| 24 | #include <crypto/hash_info.h> | 25 | #include <crypto/hash_info.h> |
| 25 | 26 | ||
| 26 | #define TPM_DIGEST_SIZE 20 /* Max TPM v1.2 PCR size */ | 27 | #define TPM_DIGEST_SIZE 20 /* Max TPM v1.2 PCR size */ |
| @@ -163,6 +164,217 @@ struct tpm_chip { · 136 unmodified lines | |||
| 163 | int locality; | 164 | int locality; |
| 164 | }; | 165 | }; |
| 165 | 166 | ||
| 167 | #define TPM_HEADER_SIZE 10 | ||
| 168 | |||
| 169 | enum tpm2_const { | ||
| 170 | TPM2_PLATFORM_PCR = 24, | ||
| 171 | TPM2_PCR_SELECT_MIN = ((TPM2_PLATFORM_PCR + 7) / 8), | ||
| 172 | }; | ||
| 173 | |||
| 174 | enum tpm2_timeouts { | ||
| 175 | TPM2_TIMEOUT_A = 750, | ||
| 176 | TPM2_TIMEOUT_B = 2000, | ||
| 177 | TPM2_TIMEOUT_C = 200, | ||
| 178 | TPM2_TIMEOUT_D = 30, | ||
| 179 | TPM2_DURATION_SHORT = 20, | ||
| 180 | TPM2_DURATION_MEDIUM = 750, | ||
| 181 | TPM2_DURATION_LONG = 2000, | ||
| 182 | TPM2_DURATION_LONG_LONG = 300000, | ||
| 183 | TPM2_DURATION_DEFAULT = 120000, | ||
| 184 | }; | ||
| 185 | |||
| 186 | enum tpm2_structures { | ||
| 187 | TPM2_ST_NO_SESSIONS = 0x8001, | ||
| 188 | TPM2_ST_SESSIONS = 0x8002, | ||
| 189 | }; | ||
| 190 | |||
| 191 | /* Indicates from what layer of the software stack the error comes from */ | ||
| 192 | #define TSS2_RC_LAYER_SHIFT 16 | ||
| 193 | #define TSS2_RESMGR_TPM_RC_LAYER (11 << TSS2_RC_LAYER_SHIFT) | ||
| 194 | |||
| 195 | enum tpm2_return_codes { | ||
| 196 | TPM2_RC_SUCCESS = 0x0000, | ||
| 197 | TPM2_RC_HASH = 0x0083, /* RC_FMT1 */ | ||
| 198 | TPM2_RC_HANDLE = 0x008B, | ||
| 199 | TPM2_RC_INITIALIZE = 0x0100, /* RC_VER1 */ | ||
| 200 | TPM2_RC_FAILURE = 0x0101, | ||
| 201 | TPM2_RC_DISABLED = 0x0120, | ||
| 202 | TPM2_RC_COMMAND_CODE = 0x0143, | ||
| 203 | TPM2_RC_TESTING = 0x090A, /* RC_WARN */ | ||
| 204 | TPM2_RC_REFERENCE_H0 = 0x0910, | ||
| 205 | TPM2_RC_RETRY = 0x0922, | ||
| 206 | }; | ||
| 207 | |||
| 208 | enum tpm2_command_codes { | ||
| 209 | TPM2_CC_FIRST = 0x011F, | ||
| 210 | TPM2_CC_HIERARCHY_CONTROL = 0x0121, | ||
| 211 | TPM2_CC_HIERARCHY_CHANGE_AUTH = 0x0129, | ||
| 212 | TPM2_CC_CREATE_PRIMARY = 0x0131, | ||
| 213 | TPM2_CC_SEQUENCE_COMPLETE = 0x013E, | ||
| 214 | TPM2_CC_SELF_TEST = 0x0143, | ||
| 215 | TPM2_CC_STARTUP = 0x0144, | ||
| 216 | TPM2_CC_SHUTDOWN = 0x0145, | ||
| 217 | TPM2_CC_NV_READ = 0x014E, | ||
| 218 | TPM2_CC_CREATE = 0x0153, | ||
| 219 | TPM2_CC_LOAD = 0x0157, | ||
| 220 | TPM2_CC_SEQUENCE_UPDATE = 0x015C, | ||
| 221 | TPM2_CC_UNSEAL = 0x015E, | ||
| 222 | TPM2_CC_CONTEXT_LOAD = 0x0161, | ||
| 223 | TPM2_CC_CONTEXT_SAVE = 0x0162, | ||
| 224 | TPM2_CC_FLUSH_CONTEXT = 0x0165, | ||
| 225 | TPM2_CC_VERIFY_SIGNATURE = 0x0177, | ||
| 226 | TPM2_CC_GET_CAPABILITY = 0x017A, | ||
| 227 | TPM2_CC_GET_RANDOM = 0x017B, | ||
| 228 | TPM2_CC_PCR_READ = 0x017E, | ||
| 229 | TPM2_CC_PCR_EXTEND = 0x0182, | ||
| 230 | TPM2_CC_EVENT_SEQUENCE_COMPLETE = 0x0185, | ||
| 231 | TPM2_CC_HASH_SEQUENCE_START = 0x0186, | ||
| 232 | TPM2_CC_CREATE_LOADED = 0x0191, | ||
| 233 | TPM2_CC_LAST = 0x0193, /* Spec 1.36 */ | ||
| 234 | }; | ||
| 235 | |||
| 236 | enum tpm2_permanent_handles { | ||
| 237 | TPM2_RS_PW = 0x40000009, | ||
| 238 | }; | ||
| 239 | |||
| 240 | enum tpm2_capabilities { | ||
| 241 | TPM2_CAP_HANDLES = 1, | ||
| 242 | TPM2_CAP_COMMANDS = 2, | ||
| 243 | TPM2_CAP_PCRS = 5, | ||
| 244 | TPM2_CAP_TPM_PROPERTIES = 6, | ||
| 245 | }; | ||
| 246 | |||
| 247 | enum tpm2_properties { | ||
| 248 | TPM_PT_TOTAL_COMMANDS = 0x0129, | ||
| 249 | }; | ||
| 250 | |||
| 251 | enum tpm2_startup_types { | ||
| 252 | TPM2_SU_CLEAR = 0x0000, | ||
| 253 | TPM2_SU_STATE = 0x0001, | ||
| 254 | }; | ||
| 255 | |||
| 256 | enum tpm2_cc_attrs { | ||
| 257 | TPM2_CC_ATTR_CHANDLES = 25, | ||
| 258 | TPM2_CC_ATTR_RHANDLE = 28, | ||
| 259 | }; | ||
| 260 | |||
| 261 | #define TPM_VID_INTEL 0x8086 | ||
| 262 | #define TPM_VID_WINBOND 0x1050 | ||
| 263 | #define TPM_VID_STM 0x104A | ||
| 264 | |||
| 265 | enum tpm_chip_flags { | ||
| 266 | TPM_CHIP_FLAG_TPM2 = BIT(1), | ||
| 267 | TPM_CHIP_FLAG_IRQ = BIT(2), | ||
| 268 | TPM_CHIP_FLAG_VIRTUAL = BIT(3), | ||
| 269 | TPM_CHIP_FLAG_HAVE_TIMEOUTS = BIT(4), | ||
| 270 | TPM_CHIP_FLAG_ALWAYS_POWERED = BIT(5), | ||
| 271 | TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED = BIT(6), | ||
| 272 | }; | ||
| 273 | |||
| 274 | #define to_tpm_chip(d) container_of(d, struct tpm_chip, dev) | ||
| 275 | |||
| 276 | struct tpm_header { | ||
| 277 | __be16 tag; | ||
| 278 | __be32 length; | ||
| 279 | union { | ||
| 280 | __be32 ordinal; | ||
| 281 | __be32 return_code; | ||
| 282 | }; | ||
| 283 | } __packed; | ||
| 284 | |||
| 285 | /* A string buffer type for constructing TPM commands. This is based on the | ||
| 286 | * ideas of string buffer code in security/keys/trusted.h but is heap based | ||
| 287 | * in order to keep the stack usage minimal. | ||
| 288 | */ | ||
| 289 | |||
| 290 | enum tpm_buf_flags { | ||
| 291 | TPM_BUF_OVERFLOW = BIT(0), | ||
| 292 | }; | ||
| 293 | |||
| 294 | struct tpm_buf { | ||
| 295 | unsigned int flags; | ||
| 296 | u8 *data; | ||
| 297 | }; | ||
| 298 | |||
| 299 | static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal) | ||
| 300 | { | ||
| 301 | struct tpm_header *head = (struct tpm_header *)buf->data; | ||
| 302 | |||
| 303 | head->tag = cpu_to_be16(tag); | ||
| 304 | head->length = cpu_to_be32(sizeof(*head)); | ||
| 305 | head->ordinal = cpu_to_be32(ordinal); | ||
| 306 | } | ||
| 307 | |||
| 308 | static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal) | ||
| 309 | { | ||
| 310 | buf->data = (u8 *)__get_free_page(GFP_KERNEL); | ||
| 311 | if (!buf->data) | ||
| 312 | return -ENOMEM; | ||
| 313 | |||
| 314 | buf->flags = 0; | ||
| 315 | tpm_buf_reset(buf, tag, ordinal); | ||
| 316 | return 0; | ||
| 317 | } | ||
| 318 | |||
| 319 | static inline void tpm_buf_destroy(struct tpm_buf *buf) | ||
| 320 | { | ||
| 321 | free_page((unsigned long)buf->data); | ||
| 322 | } | ||
| 323 | |||
| 324 | static inline u32 tpm_buf_length(struct tpm_buf *buf) | ||
| 325 | { | ||
| 326 | struct tpm_header *head = (struct tpm_header *)buf->data; | ||
| 327 | |||
| 328 | return be32_to_cpu(head->length); | ||
| 329 | } | ||
| 330 | |||
| 331 | static inline u16 tpm_buf_tag(struct tpm_buf *buf) | ||
| 332 | { | ||
| 333 | struct tpm_header *head = (struct tpm_header *)buf->data; | ||
| 334 | |||
| 335 | return be16_to_cpu(head->tag); | ||
| 336 | } | ||
| 337 | |||
| 338 | static inline void tpm_buf_append(struct tpm_buf *buf, | ||
| 339 | const unsigned char *new_data, | ||
| 340 | unsigned int new_len) | ||
| 341 | { | ||
| 342 | struct tpm_header *head = (struct tpm_header *)buf->data; | ||
| 343 | u32 len = tpm_buf_length(buf); | ||
| 344 | |||
| 345 | /* Return silently if overflow has already happened. */ | ||
| 346 | if (buf->flags & TPM_BUF_OVERFLOW) | ||
| 347 | return; | ||
| 348 | |||
| 349 | if ((len + new_len) > PAGE_SIZE) { | ||
| 350 | WARN(1, "tpm_buf: overflow\n"); | ||
| 351 | buf->flags |= TPM_BUF_OVERFLOW; | ||
| 352 | return; | ||
| 353 | } | ||
| 354 | |||
| 355 | memcpy(&buf->data[len], new_data, new_len); | ||
| 356 | head->length = cpu_to_be32(len + new_len); | ||
| 357 | } | ||
| 358 | |||
| 359 | static inline void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value) | ||
| 360 | { | ||
| 361 | tpm_buf_append(buf, &value, 1); | ||
| 362 | } | ||
| 363 | |||
| 364 | static inline void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value) | ||
| 365 | { | ||
| 366 | __be16 value2 = cpu_to_be16(value); | ||
| 367 | |||
| 368 | tpm_buf_append(buf, (u8 *) &value2, 2); | ||
| 369 | } | ||
| 370 | |||
| 371 | static inline void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value) | ||
| 372 | { | ||
| 373 | __be32 value2 = cpu_to_be32(value); | ||
| 374 | |||
| 375 | tpm_buf_append(buf, (u8 *) &value2, 4); | ||
| 376 | } | ||
| 377 | |||
| 166 | #if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE) | 378 | #if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE) |
| 167 | 379 | ||
| 168 | extern int tpm_is_tpm2(struct tpm_chip *chip); | 380 | extern int tpm_is_tpm2(struct tpm_chip *chip); |