xref: /linux-6.15/include/linux/tpm.h (revision 228cd2db)
1 /*
2  * Copyright (C) 2004,2007,2008 IBM Corporation
3  *
4  * Authors:
5  * Leendert van Doorn <[email protected]>
6  * Dave Safford <[email protected]>
7  * Reiner Sailer <[email protected]>
8  * Kylene Hall <[email protected]>
9  * Debora Velarde <[email protected]>
10  *
11  * Maintained by: <[email protected]>
12  *
13  * Device driver for TCG/TCPA TPM (trusted platform module).
14  * 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  *
21  */
22 #ifndef __LINUX_TPM_H__
23 #define __LINUX_TPM_H__
24 
25 #include <linux/hw_random.h>
26 #include <linux/acpi.h>
27 #include <linux/cdev.h>
28 #include <linux/fs.h>
29 #include <crypto/hash_info.h>
30 
31 #define TPM_DIGEST_SIZE 20	/* Max TPM v1.2 PCR size */
32 #define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE
33 
34 struct tpm_chip;
35 struct trusted_key_payload;
36 struct trusted_key_options;
37 
38 enum tpm_algorithms {
39 	TPM_ALG_ERROR		= 0x0000,
40 	TPM_ALG_SHA1		= 0x0004,
41 	TPM_ALG_KEYEDHASH	= 0x0008,
42 	TPM_ALG_SHA256		= 0x000B,
43 	TPM_ALG_SHA384		= 0x000C,
44 	TPM_ALG_SHA512		= 0x000D,
45 	TPM_ALG_NULL		= 0x0010,
46 	TPM_ALG_SM3_256		= 0x0012,
47 };
48 
49 struct tpm_digest {
50 	u16 alg_id;
51 	u8 digest[TPM_MAX_DIGEST_SIZE];
52 } __packed;
53 
54 struct tpm_bank_info {
55 	u16 alg_id;
56 	u16 digest_size;
57 	u16 crypto_id;
58 };
59 
60 enum TPM_OPS_FLAGS {
61 	TPM_OPS_AUTO_STARTUP = BIT(0),
62 };
63 
64 struct tpm_class_ops {
65 	unsigned int flags;
66 	const u8 req_complete_mask;
67 	const u8 req_complete_val;
68 	bool (*req_canceled)(struct tpm_chip *chip, u8 status);
69 	int (*recv) (struct tpm_chip *chip, u8 *buf, size_t len);
70 	int (*send) (struct tpm_chip *chip, u8 *buf, size_t len);
71 	void (*cancel) (struct tpm_chip *chip);
72 	u8 (*status) (struct tpm_chip *chip);
73 	void (*update_timeouts)(struct tpm_chip *chip,
74 				unsigned long *timeout_cap);
75 	int (*go_idle)(struct tpm_chip *chip);
76 	int (*cmd_ready)(struct tpm_chip *chip);
77 	int (*request_locality)(struct tpm_chip *chip, int loc);
78 	int (*relinquish_locality)(struct tpm_chip *chip, int loc);
79 	void (*clk_enable)(struct tpm_chip *chip, bool value);
80 };
81 
82 #define TPM_NUM_EVENT_LOG_FILES		3
83 
84 /* Indexes the duration array */
85 enum tpm_duration {
86 	TPM_SHORT = 0,
87 	TPM_MEDIUM = 1,
88 	TPM_LONG = 2,
89 	TPM_LONG_LONG = 3,
90 	TPM_UNDEFINED,
91 	TPM_NUM_DURATIONS = TPM_UNDEFINED,
92 };
93 
94 #define TPM_PPI_VERSION_LEN		3
95 
96 struct tpm_space {
97 	u32 context_tbl[3];
98 	u8 *context_buf;
99 	u32 session_tbl[3];
100 	u8 *session_buf;
101 };
102 
103 struct tpm_bios_log {
104 	void *bios_event_log;
105 	void *bios_event_log_end;
106 };
107 
108 struct tpm_chip_seqops {
109 	struct tpm_chip *chip;
110 	const struct seq_operations *seqops;
111 };
112 
113 struct tpm_chip {
114 	struct device dev;
115 	struct device devs;
116 	struct cdev cdev;
117 	struct cdev cdevs;
118 
119 	/* A driver callback under ops cannot be run unless ops_sem is held
120 	 * (sometimes implicitly, eg for the sysfs code). ops becomes null
121 	 * when the driver is unregistered, see tpm_try_get_ops.
122 	 */
123 	struct rw_semaphore ops_sem;
124 	const struct tpm_class_ops *ops;
125 
126 	struct tpm_bios_log log;
127 	struct tpm_chip_seqops bin_log_seqops;
128 	struct tpm_chip_seqops ascii_log_seqops;
129 
130 	unsigned int flags;
131 
132 	int dev_num;		/* /dev/tpm# */
133 	unsigned long is_open;	/* only one allowed */
134 
135 	char hwrng_name[64];
136 	struct hwrng hwrng;
137 
138 	struct mutex tpm_mutex;	/* tpm is processing */
139 
140 	unsigned long timeout_a; /* jiffies */
141 	unsigned long timeout_b; /* jiffies */
142 	unsigned long timeout_c; /* jiffies */
143 	unsigned long timeout_d; /* jiffies */
144 	bool timeout_adjusted;
145 	unsigned long duration[TPM_NUM_DURATIONS]; /* jiffies */
146 	bool duration_adjusted;
147 
148 	struct dentry *bios_dir[TPM_NUM_EVENT_LOG_FILES];
149 
150 	const struct attribute_group *groups[3];
151 	unsigned int groups_cnt;
152 
153 	u32 nr_allocated_banks;
154 	struct tpm_bank_info *allocated_banks;
155 #ifdef CONFIG_ACPI
156 	acpi_handle acpi_dev_handle;
157 	char ppi_version[TPM_PPI_VERSION_LEN + 1];
158 #endif /* CONFIG_ACPI */
159 
160 	struct tpm_space work_space;
161 	u32 last_cc;
162 	u32 nr_commands;
163 	u32 *cc_attrs_tbl;
164 
165 	/* active locality */
166 	int locality;
167 };
168 
169 #if defined(CONFIG_TCG_TPM) || defined(CONFIG_TCG_TPM_MODULE)
170 
171 extern int tpm_is_tpm2(struct tpm_chip *chip);
172 extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx,
173 			struct tpm_digest *digest);
174 extern int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
175 			  struct tpm_digest *digests);
176 extern int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen);
177 extern int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max);
178 extern int tpm_seal_trusted(struct tpm_chip *chip,
179 			    struct trusted_key_payload *payload,
180 			    struct trusted_key_options *options);
181 extern int tpm_unseal_trusted(struct tpm_chip *chip,
182 			      struct trusted_key_payload *payload,
183 			      struct trusted_key_options *options);
184 extern struct tpm_chip *tpm_default_chip(void);
185 #else
186 static inline int tpm_is_tpm2(struct tpm_chip *chip)
187 {
188 	return -ENODEV;
189 }
190 
191 static inline int tpm_pcr_read(struct tpm_chip *chip, int pcr_idx,
192 			       struct tpm_digest *digest)
193 {
194 	return -ENODEV;
195 }
196 
197 static inline int tpm_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,
198 				 struct tpm_digest *digests)
199 {
200 	return -ENODEV;
201 }
202 
203 static inline int tpm_send(struct tpm_chip *chip, void *cmd, size_t buflen)
204 {
205 	return -ENODEV;
206 }
207 static inline int tpm_get_random(struct tpm_chip *chip, u8 *data, size_t max)
208 {
209 	return -ENODEV;
210 }
211 
212 static inline int tpm_seal_trusted(struct tpm_chip *chip,
213 				   struct trusted_key_payload *payload,
214 				   struct trusted_key_options *options)
215 {
216 	return -ENODEV;
217 }
218 static inline int tpm_unseal_trusted(struct tpm_chip *chip,
219 				     struct trusted_key_payload *payload,
220 				     struct trusted_key_options *options)
221 {
222 	return -ENODEV;
223 }
224 static inline struct tpm_chip *tpm_default_chip(void)
225 {
226 	return NULL;
227 }
228 #endif
229 #endif
230