xref: /qemu/backends/tpm/tpm_ioctl.h (revision ded5d78c1eada3cd07b22259305a9e2ae332839c)
1f4ede81eSAmarnath Valluri /*
2f4ede81eSAmarnath Valluri  * tpm_ioctl.h
3f4ede81eSAmarnath Valluri  *
4f4ede81eSAmarnath Valluri  * (c) Copyright IBM Corporation 2014, 2015.
5f4ede81eSAmarnath Valluri  *
6f4ede81eSAmarnath Valluri  * This file is licensed under the terms of the 3-clause BSD license
7f4ede81eSAmarnath Valluri  */
8a8b991b5SMarkus Armbruster 
9a8b991b5SMarkus Armbruster #ifndef TPM_IOCTL_H
10a8b991b5SMarkus Armbruster #define TPM_IOCTL_H
11f4ede81eSAmarnath Valluri 
12f4ede81eSAmarnath Valluri #include <sys/uio.h>
13f4ede81eSAmarnath Valluri #include <sys/ioctl.h>
14f4ede81eSAmarnath Valluri 
15*ded5d78cSThomas Huth #ifdef HAVE_SYS_IOCCOM_H
16*ded5d78cSThomas Huth #include <sys/ioccom.h>
17*ded5d78cSThomas Huth #endif
18*ded5d78cSThomas Huth 
19f4ede81eSAmarnath Valluri /*
20f4ede81eSAmarnath Valluri  * Every response from a command involving a TPM command execution must hold
21f4ede81eSAmarnath Valluri  * the ptm_res as the first element.
22f4ede81eSAmarnath Valluri  * ptm_res corresponds to the error code of a command executed by the TPM.
23f4ede81eSAmarnath Valluri  */
24f4ede81eSAmarnath Valluri 
25f4ede81eSAmarnath Valluri typedef uint32_t ptm_res;
26f4ede81eSAmarnath Valluri 
27f4ede81eSAmarnath Valluri /* PTM_GET_TPMESTABLISHED: get the establishment bit */
28f4ede81eSAmarnath Valluri struct ptm_est {
29f4ede81eSAmarnath Valluri     union {
30f4ede81eSAmarnath Valluri         struct {
31f4ede81eSAmarnath Valluri             ptm_res tpm_result;
32f4ede81eSAmarnath Valluri             unsigned char bit; /* TPM established bit */
33f4ede81eSAmarnath Valluri         } resp; /* response */
34f4ede81eSAmarnath Valluri     } u;
35f4ede81eSAmarnath Valluri };
36f4ede81eSAmarnath Valluri 
37f4ede81eSAmarnath Valluri /* PTM_RESET_TPMESTABLISHED: reset establishment bit */
38f4ede81eSAmarnath Valluri struct ptm_reset_est {
39f4ede81eSAmarnath Valluri     union {
40f4ede81eSAmarnath Valluri         struct {
41f4ede81eSAmarnath Valluri             uint8_t loc; /* locality to use */
42f4ede81eSAmarnath Valluri         } req; /* request */
43f4ede81eSAmarnath Valluri         struct {
44f4ede81eSAmarnath Valluri             ptm_res tpm_result;
45f4ede81eSAmarnath Valluri         } resp; /* response */
46f4ede81eSAmarnath Valluri     } u;
47f4ede81eSAmarnath Valluri };
48f4ede81eSAmarnath Valluri 
49f4ede81eSAmarnath Valluri /* PTM_INIT */
50f4ede81eSAmarnath Valluri struct ptm_init {
51f4ede81eSAmarnath Valluri     union {
52f4ede81eSAmarnath Valluri         struct {
53f4ede81eSAmarnath Valluri             uint32_t init_flags; /* see definitions below */
54f4ede81eSAmarnath Valluri         } req; /* request */
55f4ede81eSAmarnath Valluri         struct {
56f4ede81eSAmarnath Valluri             ptm_res tpm_result;
57f4ede81eSAmarnath Valluri         } resp; /* response */
58f4ede81eSAmarnath Valluri     } u;
59f4ede81eSAmarnath Valluri };
60f4ede81eSAmarnath Valluri 
61f4ede81eSAmarnath Valluri /* above init_flags */
62f4ede81eSAmarnath Valluri #define PTM_INIT_FLAG_DELETE_VOLATILE (1 << 0)
63f4ede81eSAmarnath Valluri     /* delete volatile state file after reading it */
64f4ede81eSAmarnath Valluri 
65f4ede81eSAmarnath Valluri /* PTM_SET_LOCALITY */
66f4ede81eSAmarnath Valluri struct ptm_loc {
67f4ede81eSAmarnath Valluri     union {
68f4ede81eSAmarnath Valluri         struct {
69f4ede81eSAmarnath Valluri             uint8_t loc; /* locality to set */
70f4ede81eSAmarnath Valluri         } req; /* request */
71f4ede81eSAmarnath Valluri         struct {
72f4ede81eSAmarnath Valluri             ptm_res tpm_result;
73f4ede81eSAmarnath Valluri         } resp; /* response */
74f4ede81eSAmarnath Valluri     } u;
75f4ede81eSAmarnath Valluri };
76f4ede81eSAmarnath Valluri 
77f4ede81eSAmarnath Valluri /* PTM_HASH_DATA: hash given data */
78f4ede81eSAmarnath Valluri struct ptm_hdata {
79f4ede81eSAmarnath Valluri     union {
80f4ede81eSAmarnath Valluri         struct {
81f4ede81eSAmarnath Valluri             uint32_t length;
82f4ede81eSAmarnath Valluri             uint8_t data[4096];
83f4ede81eSAmarnath Valluri         } req; /* request */
84f4ede81eSAmarnath Valluri         struct {
85f4ede81eSAmarnath Valluri             ptm_res tpm_result;
86f4ede81eSAmarnath Valluri         } resp; /* response */
87f4ede81eSAmarnath Valluri     } u;
88f4ede81eSAmarnath Valluri };
89f4ede81eSAmarnath Valluri 
90f4ede81eSAmarnath Valluri /*
91f4ede81eSAmarnath Valluri  * size of the TPM state blob to transfer; x86_64 can handle 8k,
92f4ede81eSAmarnath Valluri  * ppc64le only ~7k; keep the response below a 4k page size
93f4ede81eSAmarnath Valluri  */
94f4ede81eSAmarnath Valluri #define PTM_STATE_BLOB_SIZE (3 * 1024)
95f4ede81eSAmarnath Valluri 
96f4ede81eSAmarnath Valluri /*
97f4ede81eSAmarnath Valluri  * The following is the data structure to get state blobs from the TPM.
98f4ede81eSAmarnath Valluri  * If the size of the state blob exceeds the PTM_STATE_BLOB_SIZE, multiple reads
99f4ede81eSAmarnath Valluri  * with this ioctl and with adjusted offset are necessary. All bytes
100f4ede81eSAmarnath Valluri  * must be transferred and the transfer is done once the last byte has been
101f4ede81eSAmarnath Valluri  * returned.
102f4ede81eSAmarnath Valluri  * It is possible to use the read() interface for reading the data; however, the
103f4ede81eSAmarnath Valluri  * first bytes of the state blob will be part of the response to the ioctl(); a
104f4ede81eSAmarnath Valluri  * subsequent read() is only necessary if the total length (totlength) exceeds
105f4ede81eSAmarnath Valluri  * the number of received bytes. seek() is not supported.
106f4ede81eSAmarnath Valluri  */
107f4ede81eSAmarnath Valluri struct ptm_getstate {
108f4ede81eSAmarnath Valluri     union {
109f4ede81eSAmarnath Valluri         struct {
110f4ede81eSAmarnath Valluri             uint32_t state_flags; /* may be: PTM_STATE_FLAG_DECRYPTED */
111f4ede81eSAmarnath Valluri             uint32_t type;        /* which blob to pull */
112f4ede81eSAmarnath Valluri             uint32_t offset;      /* offset from where to read */
113f4ede81eSAmarnath Valluri         } req; /* request */
114f4ede81eSAmarnath Valluri         struct {
115f4ede81eSAmarnath Valluri             ptm_res tpm_result;
116f4ede81eSAmarnath Valluri             uint32_t state_flags; /* may be: PTM_STATE_FLAG_ENCRYPTED */
117f4ede81eSAmarnath Valluri             uint32_t totlength;   /* total length that will be transferred */
118f4ede81eSAmarnath Valluri             uint32_t length;      /* number of bytes in following buffer */
119f4ede81eSAmarnath Valluri             uint8_t  data[PTM_STATE_BLOB_SIZE];
120f4ede81eSAmarnath Valluri         } resp; /* response */
121f4ede81eSAmarnath Valluri     } u;
122f4ede81eSAmarnath Valluri };
123f4ede81eSAmarnath Valluri 
124f4ede81eSAmarnath Valluri /* TPM state blob types */
125f4ede81eSAmarnath Valluri #define PTM_BLOB_TYPE_PERMANENT  1
126f4ede81eSAmarnath Valluri #define PTM_BLOB_TYPE_VOLATILE   2
127f4ede81eSAmarnath Valluri #define PTM_BLOB_TYPE_SAVESTATE  3
128f4ede81eSAmarnath Valluri 
129f4ede81eSAmarnath Valluri /* state_flags above : */
130f4ede81eSAmarnath Valluri #define PTM_STATE_FLAG_DECRYPTED     1 /* on input:  get decrypted state */
131f4ede81eSAmarnath Valluri #define PTM_STATE_FLAG_ENCRYPTED     2 /* on output: state is encrypted */
132f4ede81eSAmarnath Valluri 
133f4ede81eSAmarnath Valluri /*
134f4ede81eSAmarnath Valluri  * The following is the data structure to set state blobs in the TPM.
135f4ede81eSAmarnath Valluri  * If the size of the state blob exceeds the PTM_STATE_BLOB_SIZE, multiple
136f4ede81eSAmarnath Valluri  * 'writes' using this ioctl are necessary. The last packet is indicated
137f4ede81eSAmarnath Valluri  * by the length being smaller than the PTM_STATE_BLOB_SIZE.
138f4ede81eSAmarnath Valluri  * The very first packet may have a length indicator of '0' enabling
139f4ede81eSAmarnath Valluri  * a write() with all the bytes from a buffer. If the write() interface
140f4ede81eSAmarnath Valluri  * is used, a final ioctl with a non-full buffer must be made to indicate
141f4ede81eSAmarnath Valluri  * that all data were transferred (a write with 0 bytes would not work).
142f4ede81eSAmarnath Valluri  */
143f4ede81eSAmarnath Valluri struct ptm_setstate {
144f4ede81eSAmarnath Valluri     union {
145f4ede81eSAmarnath Valluri         struct {
146f4ede81eSAmarnath Valluri             uint32_t state_flags; /* may be PTM_STATE_FLAG_ENCRYPTED */
147f4ede81eSAmarnath Valluri             uint32_t type;        /* which blob to set */
148f4ede81eSAmarnath Valluri             uint32_t length;      /* length of the data;
149f4ede81eSAmarnath Valluri                                      use 0 on the first packet to
150f4ede81eSAmarnath Valluri                                      transfer using write() */
151f4ede81eSAmarnath Valluri             uint8_t data[PTM_STATE_BLOB_SIZE];
152f4ede81eSAmarnath Valluri         } req; /* request */
153f4ede81eSAmarnath Valluri         struct {
154f4ede81eSAmarnath Valluri             ptm_res tpm_result;
155f4ede81eSAmarnath Valluri         } resp; /* response */
156f4ede81eSAmarnath Valluri     } u;
157f4ede81eSAmarnath Valluri };
158f4ede81eSAmarnath Valluri 
159f4ede81eSAmarnath Valluri /*
160f4ede81eSAmarnath Valluri  * PTM_GET_CONFIG: Data structure to get runtime configuration information
161f4ede81eSAmarnath Valluri  * such as which keys are applied.
162f4ede81eSAmarnath Valluri  */
163f4ede81eSAmarnath Valluri struct ptm_getconfig {
164f4ede81eSAmarnath Valluri     union {
165f4ede81eSAmarnath Valluri         struct {
166f4ede81eSAmarnath Valluri             ptm_res tpm_result;
167f4ede81eSAmarnath Valluri             uint32_t flags;
168f4ede81eSAmarnath Valluri         } resp; /* response */
169f4ede81eSAmarnath Valluri     } u;
170f4ede81eSAmarnath Valluri };
171f4ede81eSAmarnath Valluri 
172f4ede81eSAmarnath Valluri #define PTM_CONFIG_FLAG_FILE_KEY        0x1
173f4ede81eSAmarnath Valluri #define PTM_CONFIG_FLAG_MIGRATION_KEY   0x2
174f4ede81eSAmarnath Valluri 
1759375c44fSStefan Berger /*
1769375c44fSStefan Berger  * PTM_SET_BUFFERSIZE: Set the buffer size to be used by the TPM.
1779375c44fSStefan Berger  * A 0 on input queries for the current buffer size. Any other
1789375c44fSStefan Berger  * number will try to set the buffer size. The returned number is
1799375c44fSStefan Berger  * the buffer size that will be used, which can be larger than the
1809375c44fSStefan Berger  * requested one, if it was below the minimum, or smaller than the
1819375c44fSStefan Berger  * requested one, if it was above the maximum.
1829375c44fSStefan Berger  */
1839375c44fSStefan Berger struct ptm_setbuffersize {
1849375c44fSStefan Berger     union {
1859375c44fSStefan Berger         struct {
1869375c44fSStefan Berger             uint32_t buffersize; /* 0 to query for current buffer size */
1879375c44fSStefan Berger         } req; /* request */
1889375c44fSStefan Berger         struct {
1899375c44fSStefan Berger             ptm_res tpm_result;
1909375c44fSStefan Berger             uint32_t buffersize; /* buffer size in use */
1919375c44fSStefan Berger             uint32_t minsize; /* min. supported buffer size */
1929375c44fSStefan Berger             uint32_t maxsize; /* max. supported buffer size */
1939375c44fSStefan Berger         } resp; /* response */
1949375c44fSStefan Berger     } u;
1959375c44fSStefan Berger };
1969375c44fSStefan Berger 
197f4ede81eSAmarnath Valluri 
198f4ede81eSAmarnath Valluri typedef uint64_t ptm_cap;
199f4ede81eSAmarnath Valluri typedef struct ptm_est ptm_est;
200f4ede81eSAmarnath Valluri typedef struct ptm_reset_est ptm_reset_est;
201f4ede81eSAmarnath Valluri typedef struct ptm_loc ptm_loc;
202f4ede81eSAmarnath Valluri typedef struct ptm_hdata ptm_hdata;
203f4ede81eSAmarnath Valluri typedef struct ptm_init ptm_init;
204f4ede81eSAmarnath Valluri typedef struct ptm_getstate ptm_getstate;
205f4ede81eSAmarnath Valluri typedef struct ptm_setstate ptm_setstate;
206f4ede81eSAmarnath Valluri typedef struct ptm_getconfig ptm_getconfig;
2079375c44fSStefan Berger typedef struct ptm_setbuffersize ptm_setbuffersize;
208f4ede81eSAmarnath Valluri 
209f4ede81eSAmarnath Valluri /* capability flags returned by PTM_GET_CAPABILITY */
210f4ede81eSAmarnath Valluri #define PTM_CAP_INIT               (1)
211f4ede81eSAmarnath Valluri #define PTM_CAP_SHUTDOWN           (1 << 1)
212f4ede81eSAmarnath Valluri #define PTM_CAP_GET_TPMESTABLISHED (1 << 2)
213f4ede81eSAmarnath Valluri #define PTM_CAP_SET_LOCALITY       (1 << 3)
214f4ede81eSAmarnath Valluri #define PTM_CAP_HASHING            (1 << 4)
215f4ede81eSAmarnath Valluri #define PTM_CAP_CANCEL_TPM_CMD     (1 << 5)
216f4ede81eSAmarnath Valluri #define PTM_CAP_STORE_VOLATILE     (1 << 6)
217f4ede81eSAmarnath Valluri #define PTM_CAP_RESET_TPMESTABLISHED (1 << 7)
218f4ede81eSAmarnath Valluri #define PTM_CAP_GET_STATEBLOB      (1 << 8)
219f4ede81eSAmarnath Valluri #define PTM_CAP_SET_STATEBLOB      (1 << 9)
220f4ede81eSAmarnath Valluri #define PTM_CAP_STOP               (1 << 10)
221f4ede81eSAmarnath Valluri #define PTM_CAP_GET_CONFIG         (1 << 11)
222f4ede81eSAmarnath Valluri #define PTM_CAP_SET_DATAFD         (1 << 12)
2239375c44fSStefan Berger #define PTM_CAP_SET_BUFFERSIZE     (1 << 13)
224f4ede81eSAmarnath Valluri 
225f4ede81eSAmarnath Valluri enum {
226f4ede81eSAmarnath Valluri     PTM_GET_CAPABILITY     = _IOR('P', 0, ptm_cap),
227f4ede81eSAmarnath Valluri     PTM_INIT               = _IOWR('P', 1, ptm_init),
228f4ede81eSAmarnath Valluri     PTM_SHUTDOWN           = _IOR('P', 2, ptm_res),
229f4ede81eSAmarnath Valluri     PTM_GET_TPMESTABLISHED = _IOR('P', 3, ptm_est),
230f4ede81eSAmarnath Valluri     PTM_SET_LOCALITY       = _IOWR('P', 4, ptm_loc),
231f4ede81eSAmarnath Valluri     PTM_HASH_START         = _IOR('P', 5, ptm_res),
232f4ede81eSAmarnath Valluri     PTM_HASH_DATA          = _IOWR('P', 6, ptm_hdata),
233f4ede81eSAmarnath Valluri     PTM_HASH_END           = _IOR('P', 7, ptm_res),
234f4ede81eSAmarnath Valluri     PTM_CANCEL_TPM_CMD     = _IOR('P', 8, ptm_res),
235f4ede81eSAmarnath Valluri     PTM_STORE_VOLATILE     = _IOR('P', 9, ptm_res),
236f4ede81eSAmarnath Valluri     PTM_RESET_TPMESTABLISHED = _IOWR('P', 10, ptm_reset_est),
237f4ede81eSAmarnath Valluri     PTM_GET_STATEBLOB      = _IOWR('P', 11, ptm_getstate),
238f4ede81eSAmarnath Valluri     PTM_SET_STATEBLOB      = _IOWR('P', 12, ptm_setstate),
239f4ede81eSAmarnath Valluri     PTM_STOP               = _IOR('P', 13, ptm_res),
240f4ede81eSAmarnath Valluri     PTM_GET_CONFIG         = _IOR('P', 14, ptm_getconfig),
241f4ede81eSAmarnath Valluri     PTM_SET_DATAFD         = _IOR('P', 15, ptm_res),
2429375c44fSStefan Berger     PTM_SET_BUFFERSIZE     = _IOWR('P', 16, ptm_setbuffersize),
243f4ede81eSAmarnath Valluri };
244f4ede81eSAmarnath Valluri 
245f4ede81eSAmarnath Valluri /*
246f4ede81eSAmarnath Valluri  * Commands used by the non-CUSE TPMs
247f4ede81eSAmarnath Valluri  *
248f4ede81eSAmarnath Valluri  * All messages container big-endian data.
249f4ede81eSAmarnath Valluri  *
250f4ede81eSAmarnath Valluri  * The return messages only contain the 'resp' part of the unions
251f4ede81eSAmarnath Valluri  * in the data structures above. Besides that the limits in the
252f4ede81eSAmarnath Valluri  * buffers above (ptm_hdata:u.req.data and ptm_get_state:u.resp.data
253f4ede81eSAmarnath Valluri  * and ptm_set_state:u.req.data) are 0xffffffff.
254f4ede81eSAmarnath Valluri  */
255f4ede81eSAmarnath Valluri enum {
256f4ede81eSAmarnath Valluri     CMD_GET_CAPABILITY = 1,
257f4ede81eSAmarnath Valluri     CMD_INIT,
258f4ede81eSAmarnath Valluri     CMD_SHUTDOWN,
259f4ede81eSAmarnath Valluri     CMD_GET_TPMESTABLISHED,
260f4ede81eSAmarnath Valluri     CMD_SET_LOCALITY,
261f4ede81eSAmarnath Valluri     CMD_HASH_START,
262f4ede81eSAmarnath Valluri     CMD_HASH_DATA,
263f4ede81eSAmarnath Valluri     CMD_HASH_END,
264f4ede81eSAmarnath Valluri     CMD_CANCEL_TPM_CMD,
265f4ede81eSAmarnath Valluri     CMD_STORE_VOLATILE,
266f4ede81eSAmarnath Valluri     CMD_RESET_TPMESTABLISHED,
267f4ede81eSAmarnath Valluri     CMD_GET_STATEBLOB,
268f4ede81eSAmarnath Valluri     CMD_SET_STATEBLOB,
269f4ede81eSAmarnath Valluri     CMD_STOP,
270f4ede81eSAmarnath Valluri     CMD_GET_CONFIG,
2719375c44fSStefan Berger     CMD_SET_DATAFD,
2729375c44fSStefan Berger     CMD_SET_BUFFERSIZE,
273f4ede81eSAmarnath Valluri };
274f4ede81eSAmarnath Valluri 
275a8b991b5SMarkus Armbruster #endif /* TPM_IOCTL_H */
276