xref: /linux/include/linux/tpm.h (revision afe0579334f622c803f2864f22c04c20c320bd80)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2004,2007,2008 IBM Corporation
4  *
5  * Authors:
6  * Leendert van Doorn <leendert@watson.ibm.com>
7  * Dave Safford <safford@watson.ibm.com>
8  * Reiner Sailer <sailer@watson.ibm.com>
9  * Kylene Hall <kjhall@us.ibm.com>
10  * Debora Velarde <dvelarde@us.ibm.com>
11  *
12  * Maintained by: <tpmdd_devel@lists.sourceforge.net>
13  *
14  * Device driver for TCG/TCPA TPM (trusted platform module).
15  * Specifications at www.trustedcomputinggroup.org
16  */
17 #ifndef __LINUX_TPM_H__
18 #define __LINUX_TPM_H__
19 
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 #include <crypto/aes.h>
27 
28 #include <linux/tpm_command.h>
29 #include <linux/tpm_buf.h>
30 
31 struct tpm_chip;
32 struct trusted_key_payload;
33 struct trusted_key_options;
34 /* opaque structure, holds auth session parameters like the session key */
35 struct tpm2_auth;
36 
37 enum TPM_OPS_FLAGS {
38 	TPM_OPS_AUTO_STARTUP = BIT(0),
39 };
40 
41 struct tpm_class_ops {
42 	unsigned int flags;
43 	const u8 req_complete_mask;
44 	const u8 req_complete_val;
45 	bool (*req_canceled)(struct tpm_chip *chip, u8 status);
46 	int (*recv) (struct tpm_chip *chip, u8 *buf, size_t len);
47 	int (*send)(struct tpm_chip *chip, u8 *buf, size_t bufsiz,
48 		    size_t cmd_len);
49 	void (*cancel) (struct tpm_chip *chip);
50 	u8 (*status) (struct tpm_chip *chip);
51 	void (*update_timeouts)(struct tpm_chip *chip,
52 				unsigned long *timeout_cap);
53 	void (*update_durations)(struct tpm_chip *chip,
54 				 unsigned long *duration_cap);
55 	int (*go_idle)(struct tpm_chip *chip);
56 	int (*cmd_ready)(struct tpm_chip *chip);
57 	int (*request_locality)(struct tpm_chip *chip, int loc);
58 	int (*relinquish_locality)(struct tpm_chip *chip, int loc);
59 	void (*clk_enable)(struct tpm_chip *chip, bool value);
60 };
61 
62 #define TPM_NUM_EVENT_LOG_FILES		3
63 
64 /* Indexes the duration array */
65 enum tpm_duration {
66 	TPM_SHORT = 0,
67 	TPM_MEDIUM = 1,
68 	TPM_LONG = 2,
69 	TPM_LONG_LONG = 3,
70 	TPM_UNDEFINED,
71 	TPM_NUM_DURATIONS = TPM_UNDEFINED,
72 };
73 
74 #define TPM_PPI_VERSION_LEN		3
75 
76 struct tpm_space {
77 	u32 context_tbl[3];
78 	u8 *context_buf;
79 	u32 session_tbl[3];
80 	u8 *session_buf;
81 	u32 buf_size;
82 };
83 
84 struct tpm_bios_log {
85 	void *bios_event_log;
86 	void *bios_event_log_end;
87 };
88 
89 struct tpm_chip_seqops {
90 	struct tpm_chip *chip;
91 	const struct seq_operations *seqops;
92 };
93 
94 /* Fixed define for the curve we use which is NIST_P256 */
95 #define EC_PT_SZ	32
96 
97 /*
98  * fixed define for the size of a name.  This is actually HASHALG size
99  * plus 2, so 32 for SHA256
100  */
101 #define TPM2_NAME_SIZE	34
102 
103 /*
104  * The maximum size for an object context
105  */
106 #define TPM2_MAX_CONTEXT_SIZE 4096
107 
108 struct tpm_chip {
109 	struct device dev;
110 	struct device devs;
111 	struct cdev cdev;
112 	struct cdev cdevs;
113 
114 	/* A driver callback under ops cannot be run unless ops_sem is held
115 	 * (sometimes implicitly, eg for the sysfs code). ops becomes null
116 	 * when the driver is unregistered, see tpm_try_get_ops.
117 	 */
118 	struct rw_semaphore ops_sem;
119 	const struct tpm_class_ops *ops;
120 
121 	struct tpm_bios_log log;
122 	struct tpm_chip_seqops bin_log_seqops;
123 	struct tpm_chip_seqops ascii_log_seqops;
124 
125 	unsigned int flags;
126 
127 	int dev_num;		/* /dev/tpm# */
128 	unsigned long is_open;	/* only one allowed */
129 
130 	char hwrng_name[64];
131 	struct hwrng hwrng;
132 
133 	struct mutex tpm_mutex;	/* tpm is processing */
134 
135 	unsigned long timeout_a; /* jiffies */
136 	unsigned long timeout_b; /* jiffies */
137 	unsigned long timeout_c; /* jiffies */
138 	unsigned long timeout_d; /* jiffies */
139 	bool timeout_adjusted;
140 	unsigned long duration[TPM_NUM_DURATIONS]; /* jiffies */
141 	bool duration_adjusted;
142 
143 	struct dentry *bios_dir;
144 
145 	const struct attribute_group *groups[3 + TPM_MAX_HASHES];
146 	unsigned int groups_cnt;
147 
148 	u32 nr_allocated_banks;
149 	struct tpm_bank_info allocated_banks[TPM2_MAX_PCR_BANKS];
150 #ifdef CONFIG_ACPI
151 	acpi_handle acpi_dev_handle;
152 	char ppi_version[TPM_PPI_VERSION_LEN + 1];
153 #endif /* CONFIG_ACPI */
154 
155 	struct tpm_space work_space;
156 	u32 last_cc;
157 	u32 nr_commands;
158 	u32 *cc_attrs_tbl;
159 
160 	/* active locality */
161 	int locality;
162 
163 #ifdef CONFIG_TCG_TPM2_HMAC
164 	/* details for communication security via sessions */
165 
166 	/* saved context for NULL seed */
167 	u8 null_key_context[TPM2_MAX_CONTEXT_SIZE];
168 	 /* name of NULL seed */
169 	u8 null_key_name[TPM2_NAME_SIZE];
170 	u8 null_ec_key_x[EC_PT_SZ];
171 	u8 null_ec_key_y[EC_PT_SZ];
172 	struct tpm2_auth *auth;
173 #endif
174 };
175 
176 static inline enum tpm2_mso_type tpm2_handle_mso(u32 handle)
177 {
178 	return handle >> 24;
179 }
180 
181 #define TPM_VID_INTEL    0x8086
182 #define TPM_VID_WINBOND  0x1050
183 #define TPM_VID_STM      0x104A
184 #define TPM_VID_ATML     0x1114
185 #define TPM_VID_IFX      0x15D1
186 
187 enum tpm_chip_flags {
188 	TPM_CHIP_FLAG_BOOTSTRAPPED		= BIT(0),
189 	TPM_CHIP_FLAG_TPM2			= BIT(1),
190 	TPM_CHIP_FLAG_IRQ			= BIT(2),
191 	TPM_CHIP_FLAG_VIRTUAL			= BIT(3),
192 	TPM_CHIP_FLAG_HAVE_TIMEOUTS		= BIT(4),
193 	TPM_CHIP_FLAG_ALWAYS_POWERED		= BIT(5),
194 	TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED	= BIT(6),
195 	TPM_CHIP_FLAG_FIRMWARE_UPGRADE		= BIT(7),
196 	TPM_CHIP_FLAG_SUSPENDED			= BIT(8),
197 	TPM_CHIP_FLAG_HWRNG_DISABLED		= BIT(9),
198 	TPM_CHIP_FLAG_DISABLE			= BIT(10),
199 	TPM_CHIP_FLAG_SYNC			= BIT(11),
200 };
201 
202 #define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)
203 
204 struct tpm2_hash {
205 	unsigned int crypto_id;
206 	unsigned int tpm_id;
207 };
208 
209 /*
210  * Check if TPM device is in the firmware upgrade mode.
211  */
212 static inline bool tpm_is_firmware_upgrade(struct tpm_chip *chip)
213 {
214 	return chip->flags & TPM_CHIP_FLAG_FIRMWARE_UPGRADE;
215 }
216 
217 static inline u32 tpm2_rc_value(u32 rc)
218 {
219 	return (rc & BIT(7)) ? rc & 0xbf : rc;
220 }
221 
222 /*
223  * Convert a return value from tpm_transmit_cmd() to POSIX error code.
224  */
225 static inline ssize_t tpm_ret_to_err(ssize_t ret)
226 {
227 	if (ret < 0)
228 		return ret;
229 
230 	switch (tpm2_rc_value(ret)) {
231 	case TPM2_RC_SUCCESS:
232 		return 0;
233 	case TPM2_RC_SESSION_MEMORY:
234 		return -ENOMEM;
235 	case TPM2_RC_HASH:
236 		return -EINVAL;
237 	default:
238 		return -EPERM;
239 	}
240 }
241 
242 #if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE)
243 
244 extern int tpm_is_tpm2(struct tpm_chip *chip);
245 extern __must_check int tpm_try_get_ops(struct tpm_chip *chip);
246 extern void tpm_put_ops(struct tpm_chip *chip);
247 extern ssize_t tpm_transmit_cmd(struct tpm_chip *chip, struct tpm_buf *buf,
248 				size_t min_rsp_body_length, const char *desc);
249 extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
250 			struct tpm_digest *digest);
251 extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
252 			  struct tpm_digest *digests);
253 extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
254 extern struct tpm_chip *tpm_default_chip(void);
255 void tpm2_flush_context(struct tpm_chip *chip, u32 handle);
256 int tpm2_find_hash_alg(unsigned int crypto_id);
257 
258 static inline void tpm_buf_append_empty_auth(struct tpm_buf *buf, u32 handle)
259 {
260 	/* simple authorization for empty auth */
261 	tpm_buf_append_u32(buf, 9);		/* total length of auth */
262 	tpm_buf_append_u32(buf, handle);
263 	tpm_buf_append_u16(buf, 0);		/* nonce len */
264 	tpm_buf_append_u8(buf, 0);		/* attributes */
265 	tpm_buf_append_u16(buf, 0);		/* hmac len */
266 }
267 #else
268 static inline int tpm_is_tpm2(struct tpm_chip *chip)
269 {
270 	return -ENODEV;
271 }
272 static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx,
273 			       struct tpm_digest *digest)
274 {
275 	return -ENODEV;
276 }
277 
278 static inline int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
279 				 struct tpm_digest *digests)
280 {
281 	return -ENODEV;
282 }
283 
284 static inline int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max)
285 {
286 	return -ENODEV;
287 }
288 
289 static inline struct tpm_chip *tpm_default_chip(void)
290 {
291 	return NULL;
292 }
293 
294 static inline void tpm_buf_append_empty_auth(struct tpm_buf *buf, u32 handle)
295 {
296 }
297 #endif
298 
299 static inline struct tpm2_auth *tpm2_chip_auth(struct tpm_chip *chip)
300 {
301 #ifdef CONFIG_TCG_TPM2_HMAC
302 	return chip->auth;
303 #else
304 	return NULL;
305 #endif
306 }
307 
308 int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
309 			u32 handle, u8 *name);
310 void tpm_buf_append_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf,
311 				 u8 attributes, u8 *passphrase,
312 				 int passphraselen);
313 void tpm_buf_append_auth(struct tpm_chip *chip, struct tpm_buf *buf,
314 			 u8 *passphrase, int passphraselen);
315 
316 #ifdef CONFIG_TCG_TPM2_HMAC
317 
318 int tpm2_start_auth_session(struct tpm_chip *chip);
319 int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf);
320 int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
321 				int rc);
322 void tpm2_end_auth_session(struct tpm_chip *chip);
323 #else
324 #include <linux/unaligned.h>
325 
326 static inline int tpm2_start_auth_session(struct tpm_chip *chip)
327 {
328 	return 0;
329 }
330 static inline void tpm2_end_auth_session(struct tpm_chip *chip)
331 {
332 }
333 
334 static inline int tpm_buf_fill_hmac_session(struct tpm_chip *chip,
335 					    struct tpm_buf *buf)
336 {
337 	return 0;
338 }
339 
340 static inline int tpm_buf_check_hmac_response(struct tpm_chip *chip,
341 					      struct tpm_buf *buf,
342 					      int rc)
343 {
344 	return rc;
345 }
346 #endif	/* CONFIG_TCG_TPM2_HMAC */
347 
348 #endif
349