hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/include/linux/tpm.h
....@@ -1,3 +1,4 @@
1
+/* SPDX-License-Identifier: GPL-2.0-only */
12 /*
23 * Copyright (C) 2004,2007,2008 IBM Corporation
34 *
....@@ -12,21 +13,45 @@
1213 *
1314 * Device driver for TCG/TCPA TPM (trusted platform module).
1415 * Specifications at www.trustedcomputinggroup.org
15
- *
16
- * This program is free software; you can redistribute it and/or
17
- * modify it under the terms of the GNU General Public License as
18
- * published by the Free Software Foundation, version 2 of the
19
- * License.
20
- *
2116 */
2217 #ifndef __LINUX_TPM_H__
2318 #define __LINUX_TPM_H__
2419
20
+#include <linux/hw_random.h>
21
+#include <linux/acpi.h>
22
+#include <linux/cdev.h>
23
+#include <linux/fs.h>
24
+#include <linux/highmem.h>
25
+#include <crypto/hash_info.h>
26
+
2527 #define TPM_DIGEST_SIZE 20 /* Max TPM v1.2 PCR size */
28
+#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
2629
2730 struct tpm_chip;
2831 struct trusted_key_payload;
2932 struct trusted_key_options;
33
+
34
+enum tpm_algorithms {
35
+ TPM_ALG_ERROR = 0x0000,
36
+ TPM_ALG_SHA1 = 0x0004,
37
+ TPM_ALG_KEYEDHASH = 0x0008,
38
+ TPM_ALG_SHA256 = 0x000B,
39
+ TPM_ALG_SHA384 = 0x000C,
40
+ TPM_ALG_SHA512 = 0x000D,
41
+ TPM_ALG_NULL = 0x0010,
42
+ TPM_ALG_SM3_256 = 0x0012,
43
+};
44
+
45
+struct tpm_digest {
46
+ u16 alg_id;
47
+ u8 digest[TPM_MAX_DIGEST_SIZE];
48
+} __packed;
49
+
50
+struct tpm_bank_info {
51
+ u16 alg_id;
52
+ u16 digest_size;
53
+ u16 crypto_id;
54
+};
3055
3156 enum TPM_OPS_FLAGS {
3257 TPM_OPS_AUTO_STARTUP = BIT(0),
....@@ -41,8 +66,10 @@
4166 int (*send) (struct tpm_chip *chip, u8 *buf, size_t len);
4267 void (*cancel) (struct tpm_chip *chip);
4368 u8 (*status) (struct tpm_chip *chip);
44
- bool (*update_timeouts)(struct tpm_chip *chip,
69
+ void (*update_timeouts)(struct tpm_chip *chip,
4570 unsigned long *timeout_cap);
71
+ void (*update_durations)(struct tpm_chip *chip,
72
+ unsigned long *duration_cap);
4673 int (*go_idle)(struct tpm_chip *chip);
4774 int (*cmd_ready)(struct tpm_chip *chip);
4875 int (*request_locality)(struct tpm_chip *chip, int loc);
....@@ -50,34 +77,356 @@
5077 void (*clk_enable)(struct tpm_chip *chip, bool value);
5178 };
5279
80
+#define TPM_NUM_EVENT_LOG_FILES 3
81
+
82
+/* Indexes the duration array */
83
+enum tpm_duration {
84
+ TPM_SHORT = 0,
85
+ TPM_MEDIUM = 1,
86
+ TPM_LONG = 2,
87
+ TPM_LONG_LONG = 3,
88
+ TPM_UNDEFINED,
89
+ TPM_NUM_DURATIONS = TPM_UNDEFINED,
90
+};
91
+
92
+#define TPM_PPI_VERSION_LEN 3
93
+
94
+struct tpm_space {
95
+ u32 context_tbl[3];
96
+ u8 *context_buf;
97
+ u32 session_tbl[3];
98
+ u8 *session_buf;
99
+ u32 buf_size;
100
+};
101
+
102
+struct tpm_bios_log {
103
+ void *bios_event_log;
104
+ void *bios_event_log_end;
105
+};
106
+
107
+struct tpm_chip_seqops {
108
+ struct tpm_chip *chip;
109
+ const struct seq_operations *seqops;
110
+};
111
+
112
+struct tpm_chip {
113
+ struct device dev;
114
+ struct device devs;
115
+ struct cdev cdev;
116
+ struct cdev cdevs;
117
+
118
+ /* A driver callback under ops cannot be run unless ops_sem is held
119
+ * (sometimes implicitly, eg for the sysfs code). ops becomes null
120
+ * when the driver is unregistered, see tpm_try_get_ops.
121
+ */
122
+ struct rw_semaphore ops_sem;
123
+ const struct tpm_class_ops *ops;
124
+
125
+ struct tpm_bios_log log;
126
+ struct tpm_chip_seqops bin_log_seqops;
127
+ struct tpm_chip_seqops ascii_log_seqops;
128
+
129
+ unsigned int flags;
130
+
131
+ int dev_num; /* /dev/tpm# */
132
+ unsigned long is_open; /* only one allowed */
133
+
134
+ char hwrng_name[64];
135
+ struct hwrng hwrng;
136
+
137
+ struct mutex tpm_mutex; /* tpm is processing */
138
+
139
+ unsigned long timeout_a; /* jiffies */
140
+ unsigned long timeout_b; /* jiffies */
141
+ unsigned long timeout_c; /* jiffies */
142
+ unsigned long timeout_d; /* jiffies */
143
+ bool timeout_adjusted;
144
+ unsigned long duration[TPM_NUM_DURATIONS]; /* jiffies */
145
+ bool duration_adjusted;
146
+
147
+ struct dentry *bios_dir[TPM_NUM_EVENT_LOG_FILES];
148
+
149
+ const struct attribute_group *groups[3];
150
+ unsigned int groups_cnt;
151
+
152
+ u32 nr_allocated_banks;
153
+ struct tpm_bank_info *allocated_banks;
154
+#ifdef CONFIG_ACPI
155
+ acpi_handle acpi_dev_handle;
156
+ char ppi_version[TPM_PPI_VERSION_LEN + 1];
157
+#endif /* CONFIG_ACPI */
158
+
159
+ struct tpm_space work_space;
160
+ u32 last_cc;
161
+ u32 nr_commands;
162
+ u32 *cc_attrs_tbl;
163
+
164
+ /* active locality */
165
+ int locality;
166
+};
167
+
168
+#define TPM_HEADER_SIZE 10
169
+
170
+enum tpm2_const {
171
+ TPM2_PLATFORM_PCR = 24,
172
+ TPM2_PCR_SELECT_MIN = ((TPM2_PLATFORM_PCR + 7) / 8),
173
+};
174
+
175
+enum tpm2_timeouts {
176
+ TPM2_TIMEOUT_A = 750,
177
+ TPM2_TIMEOUT_B = 2000,
178
+ TPM2_TIMEOUT_C = 200,
179
+ TPM2_TIMEOUT_D = 30,
180
+ TPM2_DURATION_SHORT = 20,
181
+ TPM2_DURATION_MEDIUM = 750,
182
+ TPM2_DURATION_LONG = 2000,
183
+ TPM2_DURATION_LONG_LONG = 300000,
184
+ TPM2_DURATION_DEFAULT = 120000,
185
+};
186
+
187
+enum tpm2_structures {
188
+ TPM2_ST_NO_SESSIONS = 0x8001,
189
+ TPM2_ST_SESSIONS = 0x8002,
190
+};
191
+
192
+/* Indicates from what layer of the software stack the error comes from */
193
+#define TSS2_RC_LAYER_SHIFT 16
194
+#define TSS2_RESMGR_TPM_RC_LAYER (11 << TSS2_RC_LAYER_SHIFT)
195
+
196
+enum tpm2_return_codes {
197
+ TPM2_RC_SUCCESS = 0x0000,
198
+ TPM2_RC_HASH = 0x0083, /* RC_FMT1 */
199
+ TPM2_RC_HANDLE = 0x008B,
200
+ TPM2_RC_INITIALIZE = 0x0100, /* RC_VER1 */
201
+ TPM2_RC_FAILURE = 0x0101,
202
+ TPM2_RC_DISABLED = 0x0120,
203
+ TPM2_RC_COMMAND_CODE = 0x0143,
204
+ TPM2_RC_TESTING = 0x090A, /* RC_WARN */
205
+ TPM2_RC_REFERENCE_H0 = 0x0910,
206
+ TPM2_RC_RETRY = 0x0922,
207
+};
208
+
209
+enum tpm2_command_codes {
210
+ TPM2_CC_FIRST = 0x011F,
211
+ TPM2_CC_HIERARCHY_CONTROL = 0x0121,
212
+ TPM2_CC_HIERARCHY_CHANGE_AUTH = 0x0129,
213
+ TPM2_CC_CREATE_PRIMARY = 0x0131,
214
+ TPM2_CC_SEQUENCE_COMPLETE = 0x013E,
215
+ TPM2_CC_SELF_TEST = 0x0143,
216
+ TPM2_CC_STARTUP = 0x0144,
217
+ TPM2_CC_SHUTDOWN = 0x0145,
218
+ TPM2_CC_NV_READ = 0x014E,
219
+ TPM2_CC_CREATE = 0x0153,
220
+ TPM2_CC_LOAD = 0x0157,
221
+ TPM2_CC_SEQUENCE_UPDATE = 0x015C,
222
+ TPM2_CC_UNSEAL = 0x015E,
223
+ TPM2_CC_CONTEXT_LOAD = 0x0161,
224
+ TPM2_CC_CONTEXT_SAVE = 0x0162,
225
+ TPM2_CC_FLUSH_CONTEXT = 0x0165,
226
+ TPM2_CC_VERIFY_SIGNATURE = 0x0177,
227
+ TPM2_CC_GET_CAPABILITY = 0x017A,
228
+ TPM2_CC_GET_RANDOM = 0x017B,
229
+ TPM2_CC_PCR_READ = 0x017E,
230
+ TPM2_CC_PCR_EXTEND = 0x0182,
231
+ TPM2_CC_EVENT_SEQUENCE_COMPLETE = 0x0185,
232
+ TPM2_CC_HASH_SEQUENCE_START = 0x0186,
233
+ TPM2_CC_CREATE_LOADED = 0x0191,
234
+ TPM2_CC_LAST = 0x0193, /* Spec 1.36 */
235
+};
236
+
237
+enum tpm2_permanent_handles {
238
+ TPM2_RS_PW = 0x40000009,
239
+};
240
+
241
+enum tpm2_capabilities {
242
+ TPM2_CAP_HANDLES = 1,
243
+ TPM2_CAP_COMMANDS = 2,
244
+ TPM2_CAP_PCRS = 5,
245
+ TPM2_CAP_TPM_PROPERTIES = 6,
246
+};
247
+
248
+enum tpm2_properties {
249
+ TPM_PT_TOTAL_COMMANDS = 0x0129,
250
+};
251
+
252
+enum tpm2_startup_types {
253
+ TPM2_SU_CLEAR = 0x0000,
254
+ TPM2_SU_STATE = 0x0001,
255
+};
256
+
257
+enum tpm2_cc_attrs {
258
+ TPM2_CC_ATTR_CHANDLES = 25,
259
+ TPM2_CC_ATTR_RHANDLE = 28,
260
+};
261
+
262
+#define TPM_VID_INTEL 0x8086
263
+#define TPM_VID_WINBOND 0x1050
264
+#define TPM_VID_STM 0x104A
265
+#define TPM_VID_ATML 0x1114
266
+
267
+enum tpm_chip_flags {
268
+ TPM_CHIP_FLAG_TPM2 = BIT(1),
269
+ TPM_CHIP_FLAG_IRQ = BIT(2),
270
+ TPM_CHIP_FLAG_VIRTUAL = BIT(3),
271
+ TPM_CHIP_FLAG_HAVE_TIMEOUTS = BIT(4),
272
+ TPM_CHIP_FLAG_ALWAYS_POWERED = BIT(5),
273
+ TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED = BIT(6),
274
+};
275
+
276
+#define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)
277
+
278
+struct tpm_header {
279
+ __be16 tag;
280
+ __be32 length;
281
+ union {
282
+ __be32 ordinal;
283
+ __be32 return_code;
284
+ };
285
+} __packed;
286
+
287
+/* A string buffer type for constructing TPM commands. This is based on the
288
+ * ideas of string buffer code in security/keys/trusted.h but is heap based
289
+ * in order to keep the stack usage minimal.
290
+ */
291
+
292
+enum tpm_buf_flags {
293
+ TPM_BUF_OVERFLOW = BIT(0),
294
+};
295
+
296
+struct tpm_buf {
297
+ unsigned int flags;
298
+ u8 *data;
299
+};
300
+
301
+enum tpm2_object_attributes {
302
+ TPM2_OA_USER_WITH_AUTH = BIT(6),
303
+};
304
+
305
+enum tpm2_session_attributes {
306
+ TPM2_SA_CONTINUE_SESSION = BIT(0),
307
+};
308
+
309
+struct tpm2_hash {
310
+ unsigned int crypto_id;
311
+ unsigned int tpm_id;
312
+};
313
+
314
+static inline void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal)
315
+{
316
+ struct tpm_header *head = (struct tpm_header *)buf->data;
317
+
318
+ head->tag = cpu_to_be16(tag);
319
+ head->length = cpu_to_be32(sizeof(*head));
320
+ head->ordinal = cpu_to_be32(ordinal);
321
+}
322
+
323
+static inline int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal)
324
+{
325
+ buf->data = (u8 *)__get_free_page(GFP_KERNEL);
326
+ if (!buf->data)
327
+ return -ENOMEM;
328
+
329
+ buf->flags = 0;
330
+ tpm_buf_reset(buf, tag, ordinal);
331
+ return 0;
332
+}
333
+
334
+static inline void tpm_buf_destroy(struct tpm_buf *buf)
335
+{
336
+ free_page((unsigned long)buf->data);
337
+}
338
+
339
+static inline u32 tpm_buf_length(struct tpm_buf *buf)
340
+{
341
+ struct tpm_header *head = (struct tpm_header *)buf->data;
342
+
343
+ return be32_to_cpu(head->length);
344
+}
345
+
346
+static inline u16 tpm_buf_tag(struct tpm_buf *buf)
347
+{
348
+ struct tpm_header *head = (struct tpm_header *)buf->data;
349
+
350
+ return be16_to_cpu(head->tag);
351
+}
352
+
353
+static inline void tpm_buf_append(struct tpm_buf *buf,
354
+ const unsigned char *new_data,
355
+ unsigned int new_len)
356
+{
357
+ struct tpm_header *head = (struct tpm_header *)buf->data;
358
+ u32 len = tpm_buf_length(buf);
359
+
360
+ /* Return silently if overflow has already happened. */
361
+ if (buf->flags & TPM_BUF_OVERFLOW)
362
+ return;
363
+
364
+ if ((len + new_len) > PAGE_SIZE) {
365
+ WARN(1, "tpm_buf: overflow\n");
366
+ buf->flags |= TPM_BUF_OVERFLOW;
367
+ return;
368
+ }
369
+
370
+ memcpy(&buf->data[len], new_data, new_len);
371
+ head->length = cpu_to_be32(len + new_len);
372
+}
373
+
374
+static inline void tpm_buf_append_u8(struct tpm_buf *buf, const u8 value)
375
+{
376
+ tpm_buf_append(buf, &value, 1);
377
+}
378
+
379
+static inline void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value)
380
+{
381
+ __be16 value2 = cpu_to_be16(value);
382
+
383
+ tpm_buf_append(buf, (u8 *) &value2, 2);
384
+}
385
+
386
+static inline void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value)
387
+{
388
+ __be32 value2 = cpu_to_be32(value);
389
+
390
+ tpm_buf_append(buf, (u8 *) &value2, 4);
391
+}
392
+
393
+static inline u32 tpm2_rc_value(u32 rc)
394
+{
395
+ return (rc & BIT(7)) ? rc & 0xff : rc;
396
+}
397
+
53398 #if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE)
54399
55400 extern int tpm_is_tpm2(struct tpm_chip *chip);
56
-extern int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf);
57
-extern int tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash);
401
+extern __must_check int tpm_try_get_ops(struct tpm_chip *chip);
402
+extern void tpm_put_ops(struct tpm_chip *chip);
403
+extern ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf,
404
+ size_t min_rsp_body_length, const char *desc);
405
+extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
406
+ struct tpm_digest *digest);
407
+extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
408
+ struct tpm_digest *digests);
58409 extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen);
59410 extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
60
-extern int tpm_seal_trusted(struct tpm_chip *chip,
61
- struct trusted_key_payload *payload,
62
- struct trusted_key_options *options);
63
-extern int tpm_unseal_trusted(struct tpm_chip *chip,
64
- struct trusted_key_payload *payload,
65
- struct trusted_key_options *options);
66411 extern struct tpm_chip *tpm_default_chip(void);
412
+void tpm2_flush_context(struct tpm_chip *chip, u32 handle);
67413 #else
68414 static inline int tpm_is_tpm2(struct tpm_chip *chip)
69415 {
70416 return -ENODEV;
71417 }
72
-static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
418
+static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx,
419
+ struct tpm_digest *digest)
73420 {
74421 return -ENODEV;
75422 }
76
-static inline int tpm_pcr_extend(struct tpm_chip *chip, int pcr_idx,
77
- const u8 *hash)
423
+
424
+static inline int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
425
+ struct tpm_digest *digests)
78426 {
79427 return -ENODEV;
80428 }
429
+
81430 static inline int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
82431 {
83432 return -ENODEV;
....@@ -87,18 +436,6 @@
87436 return -ENODEV;
88437 }
89438
90
-static inline int tpm_seal_trusted(struct tpm_chip *chip,
91
- struct trusted_key_payload *payload,
92
- struct trusted_key_options *options)
93
-{
94
- return -ENODEV;
95
-}
96
-static inline int tpm_unseal_trusted(struct tpm_chip *chip,
97
- struct trusted_key_payload *payload,
98
- struct trusted_key_options *options)
99
-{
100
- return -ENODEV;
101
-}
102439 static inline struct tpm_chip *tpm_default_chip(void)
103440 {
104441 return NULL;