]> git.proxmox.com Git - swtpm.git/blob - include/swtpm/tpm_ioctl.h
swtpm: add CMD_SHUTDOWN to control channel
[swtpm.git] / include / swtpm / tpm_ioctl.h
1 /*
2 * tpm_ioctl.h
3 *
4 * (c) Copyright IBM Corporation 2014, 2015.
5 *
6 * This file is licensed under the terms of the 3-clause BSD license
7 */
8 #ifndef _TPM_IOCTL_H_
9 #define _TPM_IOCTL_H_
10
11 #include <stdint.h>
12 #include <sys/uio.h>
13 #include <sys/types.h>
14 #include <sys/ioctl.h>
15
16 /*
17 * Every response from a command involving a TPM command execution must hold
18 * the ptm_res as the first element.
19 * ptm_res corresponds to the error code of a command executed by the TPM.
20 */
21
22 typedef uint32_t ptm_res;
23
24 /* PTM_GET_TPMESTABLISHED: get the establishment bit */
25 struct ptm_est {
26 union {
27 struct {
28 ptm_res tpm_result;
29 unsigned char bit; /* TPM established bit */
30 } resp; /* response */
31 } u;
32 };
33
34 /* PTM_RESET_TPMESTABLISHED: reset establishment bit */
35 struct ptm_reset_est {
36 union {
37 struct {
38 uint8_t loc; /* locality to use */
39 } req; /* request */
40 struct {
41 ptm_res tpm_result;
42 } resp; /* response */
43 } u;
44 };
45
46 /* PTM_INIT */
47 struct ptm_init {
48 union {
49 struct {
50 uint32_t init_flags; /* see definitions below */
51 } req; /* request */
52 struct {
53 ptm_res tpm_result;
54 } resp; /* response */
55 } u;
56 };
57
58 /* above init_flags */
59 #define PTM_INIT_FLAG_DELETE_VOLATILE (1 << 0)
60 /* delete volatile state file after reading it */
61
62 /* PTM_SET_LOCALITY */
63 struct ptm_loc {
64 union {
65 struct {
66 uint8_t loc; /* locality to set */
67 } req; /* request */
68 struct {
69 ptm_res tpm_result;
70 } resp; /* response */
71 } u;
72 };
73
74 /* PTM_HASH_DATA: hash given data */
75 struct ptm_hdata {
76 union {
77 struct {
78 uint32_t length;
79 uint8_t data[4096];
80 } req; /* request */
81 struct {
82 ptm_res tpm_result;
83 } resp; /* response */
84 } u;
85 };
86
87 /*
88 * size of the TPM state blob to transfer; x86_64 can handle 8k,
89 * ppc64le only ~7k; keep the response below a 4k page size
90 */
91 #define PTM_STATE_BLOB_SIZE (3 * 1024)
92
93 /*
94 * The following is the data structure to get state blobs from the TPM.
95 * If the size of the state blob exceeds the PTM_STATE_BLOB_SIZE, multiple reads
96 * with this ioctl and with adjusted offset are necessary. All bytes
97 * must be transferred and the transfer is done once the last byte has been
98 * returned.
99 * It is possible to use the read() interface for reading the data; however,
100 * the first bytes of the state blob will be part of the response to the ioctl();
101 * a subsequent read() is only necessary if the total length (totlength) exceeds
102 * the number of received bytes. seek() is not supported.
103 */
104 struct ptm_getstate {
105 union {
106 struct {
107 uint32_t state_flags; /* may be: PTM_STATE_FLAG_DECRYPTED */
108 uint32_t type; /* which blob to pull */
109 uint32_t offset; /* offset from where to read */
110 } req; /* request */
111 struct {
112 ptm_res tpm_result;
113 uint32_t state_flags; /* may be: PTM_STATE_FLAG_ENCRYPTED */
114 uint32_t totlength; /* total length that will be transferred */
115 uint32_t length; /* number of bytes in following buffer */
116 uint8_t data[PTM_STATE_BLOB_SIZE];
117 } resp; /* response */
118 } u;
119 };
120
121 /* TPM state blob types */
122 #define PTM_BLOB_TYPE_PERMANENT 1
123 #define PTM_BLOB_TYPE_VOLATILE 2
124 #define PTM_BLOB_TYPE_SAVESTATE 3
125
126 /* state_flags above : */
127 #define PTM_STATE_FLAG_DECRYPTED 1 /* on input: get decrypted state */
128 #define PTM_STATE_FLAG_ENCRYPTED 2 /* on output: state is encrypted */
129
130 /*
131 * The following is the data structure to set state blobs in the TPM.
132 * If the size of the state blob exceeds the PTM_STATE_BLOB_SIZE, multiple
133 * 'writes' using this ioctl are necessary. The last packet is indicated
134 * by the length being smaller than the PTM_STATE_BLOB_SIZE.
135 * The very first packet may have a length indicator of '0' enabling
136 * a write() with all the bytes from a buffer. If the write() interface
137 * is used, a final ioctl with a non-full buffer must be made to indicate
138 * that all data were transferred (a write with 0 bytes would not work).
139 */
140 struct ptm_setstate {
141 union {
142 struct {
143 uint32_t state_flags; /* may be PTM_STATE_FLAG_ENCRYPTED */
144 uint32_t type; /* which blob to set */
145 uint32_t length; /* length of the data;
146 use 0 on the first packet to
147 transfer using write() */
148 uint8_t data[PTM_STATE_BLOB_SIZE];
149 } req; /* request */
150 struct {
151 ptm_res tpm_result;
152 } resp; /* response */
153 } u;
154 };
155
156 /*
157 * PTM_GET_CONFIG: Data structure to get runtime configuration information
158 * such as which keys are applied.
159 */
160 struct ptm_getconfig {
161 union {
162 struct {
163 ptm_res tpm_result;
164 uint32_t flags;
165 } resp; /* response */
166 } u;
167 };
168
169 #define PTM_CONFIG_FLAG_FILE_KEY 0x1
170 #define PTM_CONFIG_FLAG_MIGRATION_KEY 0x2
171
172
173 typedef uint64_t ptm_cap;
174 typedef struct ptm_est ptm_est;
175 typedef struct ptm_reset_est ptm_reset_est;
176 typedef struct ptm_loc ptm_loc;
177 typedef struct ptm_hdata ptm_hdata;
178 typedef struct ptm_init ptm_init;
179 typedef struct ptm_getstate ptm_getstate;
180 typedef struct ptm_setstate ptm_setstate;
181 typedef struct ptm_getconfig ptm_getconfig;
182
183 /* capability flags returned by PTM_GET_CAPABILITY */
184 #define PTM_CAP_INIT (1)
185 #define PTM_CAP_SHUTDOWN (1<<1)
186 #define PTM_CAP_GET_TPMESTABLISHED (1<<2)
187 #define PTM_CAP_SET_LOCALITY (1<<3)
188 #define PTM_CAP_HASHING (1<<4)
189 #define PTM_CAP_CANCEL_TPM_CMD (1<<5)
190 #define PTM_CAP_STORE_VOLATILE (1<<6)
191 #define PTM_CAP_RESET_TPMESTABLISHED (1<<7)
192 #define PTM_CAP_GET_STATEBLOB (1<<8)
193 #define PTM_CAP_SET_STATEBLOB (1<<9)
194 #define PTM_CAP_STOP (1<<10)
195 #define PTM_CAP_GET_CONFIG (1<<11)
196
197 enum {
198 PTM_GET_CAPABILITY = _IOR('P', 0, ptm_cap),
199 PTM_INIT = _IOWR('P', 1, ptm_init),
200 PTM_SHUTDOWN = _IOR('P', 2, ptm_res),
201 PTM_GET_TPMESTABLISHED = _IOR('P', 3, ptm_est),
202 PTM_SET_LOCALITY = _IOWR('P', 4, ptm_loc),
203 PTM_HASH_START = _IOR('P', 5, ptm_res),
204 PTM_HASH_DATA = _IOWR('P', 6, ptm_hdata),
205 PTM_HASH_END = _IOR('P', 7, ptm_res),
206 PTM_CANCEL_TPM_CMD = _IOR('P', 8, ptm_res),
207 PTM_STORE_VOLATILE = _IOR('P', 9, ptm_res),
208 PTM_RESET_TPMESTABLISHED = _IOWR('P', 10, ptm_reset_est),
209 PTM_GET_STATEBLOB = _IOWR('P', 11, ptm_getstate),
210 PTM_SET_STATEBLOB = _IOWR('P', 12, ptm_setstate),
211 PTM_STOP = _IOR('P', 13, ptm_res),
212 PTM_GET_CONFIG = _IOR('P', 14, ptm_getconfig),
213 };
214
215 /*
216 * Comments used by the non-CUSE TPMs
217 */
218 enum {
219 CMD_GET_CAPABILITY = 1,
220 CMD_INIT,
221 CMD_SHUTDOWN,
222 };
223
224 #endif /* _TPM_IOCTL_H */