tpm_ioctl.h 8.4 KB

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