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