tpm-v2.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2018 Bootlin
  4. * Author: Miquel Raynal <miquel.raynal@bootlin.com>
  5. */
  6. #ifndef __TPM_V2_H
  7. #define __TPM_V2_H
  8. #include <tpm-common.h>
  9. #define TPM2_DIGEST_LEN 32
  10. /**
  11. * TPM2 Structure Tags for command/response buffers.
  12. *
  13. * @TPM2_ST_NO_SESSIONS: the command does not need an authentication.
  14. * @TPM2_ST_SESSIONS: the command needs an authentication.
  15. */
  16. enum tpm2_structures {
  17. TPM2_ST_NO_SESSIONS = 0x8001,
  18. TPM2_ST_SESSIONS = 0x8002,
  19. };
  20. /**
  21. * TPM2 type of boolean.
  22. */
  23. enum tpm2_yes_no {
  24. TPMI_YES = 1,
  25. TPMI_NO = 0,
  26. };
  27. /**
  28. * TPM2 startup values.
  29. *
  30. * @TPM2_SU_CLEAR: reset the internal state.
  31. * @TPM2_SU_STATE: restore saved state (if any).
  32. */
  33. enum tpm2_startup_types {
  34. TPM2_SU_CLEAR = 0x0000,
  35. TPM2_SU_STATE = 0x0001,
  36. };
  37. /**
  38. * TPM2 permanent handles.
  39. *
  40. * @TPM2_RH_OWNER: refers to the 'owner' hierarchy.
  41. * @TPM2_RS_PW: indicates a password.
  42. * @TPM2_RH_LOCKOUT: refers to the 'lockout' hierarchy.
  43. * @TPM2_RH_ENDORSEMENT: refers to the 'endorsement' hierarchy.
  44. * @TPM2_RH_PLATFORM: refers to the 'platform' hierarchy.
  45. */
  46. enum tpm2_handles {
  47. TPM2_RH_OWNER = 0x40000001,
  48. TPM2_RS_PW = 0x40000009,
  49. TPM2_RH_LOCKOUT = 0x4000000A,
  50. TPM2_RH_ENDORSEMENT = 0x4000000B,
  51. TPM2_RH_PLATFORM = 0x4000000C,
  52. };
  53. /**
  54. * TPM2 command codes used at the beginning of a buffer, gives the command.
  55. *
  56. * @TPM2_CC_STARTUP: TPM2_Startup().
  57. * @TPM2_CC_SELF_TEST: TPM2_SelfTest().
  58. * @TPM2_CC_CLEAR: TPM2_Clear().
  59. * @TPM2_CC_CLEARCONTROL: TPM2_ClearControl().
  60. * @TPM2_CC_HIERCHANGEAUTH: TPM2_HierarchyChangeAuth().
  61. * @TPM2_CC_PCR_SETAUTHPOL: TPM2_PCR_SetAuthPolicy().
  62. * @TPM2_CC_DAM_RESET: TPM2_DictionaryAttackLockReset().
  63. * @TPM2_CC_DAM_PARAMETERS: TPM2_DictionaryAttackParameters().
  64. * @TPM2_CC_GET_CAPABILITY: TPM2_GetCapibility().
  65. * @TPM2_CC_PCR_READ: TPM2_PCR_Read().
  66. * @TPM2_CC_PCR_EXTEND: TPM2_PCR_Extend().
  67. * @TPM2_CC_PCR_SETAUTHVAL: TPM2_PCR_SetAuthValue().
  68. */
  69. enum tpm2_command_codes {
  70. TPM2_CC_STARTUP = 0x0144,
  71. TPM2_CC_SELF_TEST = 0x0143,
  72. TPM2_CC_CLEAR = 0x0126,
  73. TPM2_CC_CLEARCONTROL = 0x0127,
  74. TPM2_CC_HIERCHANGEAUTH = 0x0129,
  75. TPM2_CC_PCR_SETAUTHPOL = 0x012C,
  76. TPM2_CC_DAM_RESET = 0x0139,
  77. TPM2_CC_DAM_PARAMETERS = 0x013A,
  78. TPM2_CC_GET_CAPABILITY = 0x017A,
  79. TPM2_CC_PCR_READ = 0x017E,
  80. TPM2_CC_PCR_EXTEND = 0x0182,
  81. TPM2_CC_PCR_SETAUTHVAL = 0x0183,
  82. };
  83. /**
  84. * TPM2 return codes.
  85. */
  86. enum tpm2_return_codes {
  87. TPM2_RC_SUCCESS = 0x0000,
  88. TPM2_RC_BAD_TAG = 0x001E,
  89. TPM2_RC_FMT1 = 0x0080,
  90. TPM2_RC_HASH = TPM2_RC_FMT1 + 0x0003,
  91. TPM2_RC_VALUE = TPM2_RC_FMT1 + 0x0004,
  92. TPM2_RC_SIZE = TPM2_RC_FMT1 + 0x0015,
  93. TPM2_RC_BAD_AUTH = TPM2_RC_FMT1 + 0x0022,
  94. TPM2_RC_HANDLE = TPM2_RC_FMT1 + 0x000B,
  95. TPM2_RC_VER1 = 0x0100,
  96. TPM2_RC_INITIALIZE = TPM2_RC_VER1 + 0x0000,
  97. TPM2_RC_FAILURE = TPM2_RC_VER1 + 0x0001,
  98. TPM2_RC_DISABLED = TPM2_RC_VER1 + 0x0020,
  99. TPM2_RC_AUTH_MISSING = TPM2_RC_VER1 + 0x0025,
  100. TPM2_RC_COMMAND_CODE = TPM2_RC_VER1 + 0x0043,
  101. TPM2_RC_AUTHSIZE = TPM2_RC_VER1 + 0x0044,
  102. TPM2_RC_AUTH_CONTEXT = TPM2_RC_VER1 + 0x0045,
  103. TPM2_RC_NEEDS_TEST = TPM2_RC_VER1 + 0x0053,
  104. TPM2_RC_WARN = 0x0900,
  105. TPM2_RC_TESTING = TPM2_RC_WARN + 0x000A,
  106. TPM2_RC_REFERENCE_H0 = TPM2_RC_WARN + 0x0010,
  107. TPM2_RC_LOCKOUT = TPM2_RC_WARN + 0x0021,
  108. };
  109. /**
  110. * TPM2 algorithms.
  111. */
  112. enum tpm2_algorithms {
  113. TPM2_ALG_XOR = 0x0A,
  114. TPM2_ALG_SHA256 = 0x0B,
  115. TPM2_ALG_SHA384 = 0x0C,
  116. TPM2_ALG_SHA512 = 0x0D,
  117. TPM2_ALG_NULL = 0x10,
  118. };
  119. /**
  120. * Issue a TPM2_Startup command.
  121. *
  122. * @mode TPM startup mode
  123. *
  124. * @return code of the operation
  125. */
  126. u32 tpm2_startup(enum tpm2_startup_types mode);
  127. /**
  128. * Issue a TPM2_SelfTest command.
  129. *
  130. * @full_test Asking to perform all tests or only the untested ones
  131. *
  132. * @return code of the operation
  133. */
  134. u32 tpm2_self_test(enum tpm2_yes_no full_test);
  135. /**
  136. * Issue a TPM2_Clear command.
  137. *
  138. * @handle Handle
  139. * @pw Password
  140. * @pw_sz Length of the password
  141. *
  142. * @return code of the operation
  143. */
  144. u32 tpm2_clear(u32 handle, const char *pw, const ssize_t pw_sz);
  145. /**
  146. * Issue a TPM2_PCR_Extend command.
  147. *
  148. * @index Index of the PCR
  149. * @digest Value representing the event to be recorded
  150. *
  151. * @return code of the operation
  152. */
  153. u32 tpm2_pcr_extend(u32 index, const uint8_t *digest);
  154. /**
  155. * Issue a TPM2_PCR_Read command.
  156. *
  157. * @idx Index of the PCR
  158. * @idx_min_sz Minimum size in bytes of the pcrSelect array
  159. * @data Output buffer for contents of the named PCR
  160. * @updates Optional out parameter: number of updates for this PCR
  161. *
  162. * @return code of the operation
  163. */
  164. u32 tpm2_pcr_read(u32 idx, unsigned int idx_min_sz, void *data,
  165. unsigned int *updates);
  166. /**
  167. * Issue a TPM2_GetCapability command. This implementation is limited
  168. * to query property index that is 4-byte wide.
  169. *
  170. * @capability Partition of capabilities
  171. * @property Further definition of capability, limited to be 4 bytes wide
  172. * @buf Output buffer for capability information
  173. * @prop_count Size of output buffer
  174. *
  175. * @return code of the operation
  176. */
  177. u32 tpm2_get_capability(u32 capability, u32 property, void *buf,
  178. size_t prop_count);
  179. /**
  180. * Issue a TPM2_DictionaryAttackLockReset command.
  181. *
  182. * @pw Password
  183. * @pw_sz Length of the password
  184. *
  185. * @return code of the operation
  186. */
  187. u32 tpm2_dam_reset(const char *pw, const ssize_t pw_sz);
  188. /**
  189. * Issue a TPM2_DictionaryAttackParameters command.
  190. *
  191. * @pw Password
  192. * @pw_sz Length of the password
  193. * @max_tries Count of authorizations before lockout
  194. * @recovery_time Time before decrementation of the failure count
  195. * @lockout_recovery Time to wait after a lockout
  196. *
  197. * @return code of the operation
  198. */
  199. u32 tpm2_dam_parameters(const char *pw, const ssize_t pw_sz,
  200. unsigned int max_tries, unsigned int recovery_time,
  201. unsigned int lockout_recovery);
  202. /**
  203. * Issue a TPM2_HierarchyChangeAuth command.
  204. *
  205. * @handle Handle
  206. * @newpw New password
  207. * @newpw_sz Length of the new password
  208. * @oldpw Old password
  209. * @oldpw_sz Length of the old password
  210. *
  211. * @return code of the operation
  212. */
  213. int tpm2_change_auth(u32 handle, const char *newpw, const ssize_t newpw_sz,
  214. const char *oldpw, const ssize_t oldpw_sz);
  215. /**
  216. * Issue a TPM_PCR_SetAuthPolicy command.
  217. *
  218. * @pw Platform password
  219. * @pw_sz Length of the password
  220. * @index Index of the PCR
  221. * @digest New key to access the PCR
  222. *
  223. * @return code of the operation
  224. */
  225. u32 tpm2_pcr_setauthpolicy(const char *pw, const ssize_t pw_sz, u32 index,
  226. const char *key);
  227. /**
  228. * Issue a TPM_PCR_SetAuthValue command.
  229. *
  230. * @pw Platform password
  231. * @pw_sz Length of the password
  232. * @index Index of the PCR
  233. * @digest New key to access the PCR
  234. * @key_sz Length of the new key
  235. *
  236. * @return code of the operation
  237. */
  238. u32 tpm2_pcr_setauthvalue(const char *pw, const ssize_t pw_sz, u32 index,
  239. const char *key, const ssize_t key_sz);
  240. #endif /* __TPM_V2_H */